rockchip_drm_drv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
  4. * Author:Mark Yao <mark.yao@rock-chips.com>
  5. *
  6. * based on exynos_drm_drv.c
  7. */
  8. #include <linux/dma-mapping.h>
  9. #include <linux/dma-iommu.h>
  10. #include <linux/pm_runtime.h>
  11. #include <linux/module.h>
  12. #include <linux/of_graph.h>
  13. #include <linux/of_platform.h>
  14. #include <linux/component.h>
  15. #include <linux/console.h>
  16. #include <linux/iommu.h>
  17. #include <drm/drm_drv.h>
  18. #include <drm/drm_fb_helper.h>
  19. #include <drm/drm_gem_cma_helper.h>
  20. #include <drm/drm_of.h>
  21. #include <drm/drm_probe_helper.h>
  22. #include <drm/drm_vblank.h>
  23. #include "rockchip_drm_drv.h"
  24. #include "rockchip_drm_fb.h"
  25. #include "rockchip_drm_fbdev.h"
  26. #include "rockchip_drm_gem.h"
  27. #define DRIVER_NAME "rockchip"
  28. #define DRIVER_DESC "RockChip Soc DRM"
  29. #define DRIVER_DATE "20140818"
  30. #define DRIVER_MAJOR 1
  31. #define DRIVER_MINOR 0
  32. static bool is_support_iommu = true;
  33. static struct drm_driver rockchip_drm_driver;
  34. /*
  35. * Attach a (component) device to the shared drm dma mapping from master drm
  36. * device. This is used by the VOPs to map GEM buffers to a common DMA
  37. * mapping.
  38. */
  39. int rockchip_drm_dma_attach_device(struct drm_device *drm_dev,
  40. struct device *dev)
  41. {
  42. struct rockchip_drm_private *private = drm_dev->dev_private;
  43. int ret;
  44. if (!is_support_iommu)
  45. return 0;
  46. ret = iommu_attach_device(private->domain, dev);
  47. if (ret) {
  48. DRM_DEV_ERROR(dev, "Failed to attach iommu device\n");
  49. return ret;
  50. }
  51. return 0;
  52. }
  53. void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
  54. struct device *dev)
  55. {
  56. struct rockchip_drm_private *private = drm_dev->dev_private;
  57. struct iommu_domain *domain = private->domain;
  58. if (!is_support_iommu)
  59. return;
  60. iommu_detach_device(domain, dev);
  61. }
  62. static int rockchip_drm_init_iommu(struct drm_device *drm_dev)
  63. {
  64. struct rockchip_drm_private *private = drm_dev->dev_private;
  65. struct iommu_domain_geometry *geometry;
  66. u64 start, end;
  67. if (!is_support_iommu)
  68. return 0;
  69. private->domain = iommu_domain_alloc(&platform_bus_type);
  70. if (!private->domain)
  71. return -ENOMEM;
  72. geometry = &private->domain->geometry;
  73. start = geometry->aperture_start;
  74. end = geometry->aperture_end;
  75. DRM_DEBUG("IOMMU context initialized (aperture: %#llx-%#llx)\n",
  76. start, end);
  77. drm_mm_init(&private->mm, start, end - start + 1);
  78. mutex_init(&private->mm_lock);
  79. return 0;
  80. }
  81. static void rockchip_iommu_cleanup(struct drm_device *drm_dev)
  82. {
  83. struct rockchip_drm_private *private = drm_dev->dev_private;
  84. if (!is_support_iommu)
  85. return;
  86. drm_mm_takedown(&private->mm);
  87. iommu_domain_free(private->domain);
  88. }
  89. static int rockchip_drm_bind(struct device *dev)
  90. {
  91. struct drm_device *drm_dev;
  92. struct rockchip_drm_private *private;
  93. int ret;
  94. drm_dev = drm_dev_alloc(&rockchip_drm_driver, dev);
  95. if (IS_ERR(drm_dev))
  96. return PTR_ERR(drm_dev);
  97. dev_set_drvdata(dev, drm_dev);
  98. private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
  99. if (!private) {
  100. ret = -ENOMEM;
  101. goto err_free;
  102. }
  103. drm_dev->dev_private = private;
  104. INIT_LIST_HEAD(&private->psr_list);
  105. mutex_init(&private->psr_list_lock);
  106. ret = rockchip_drm_init_iommu(drm_dev);
  107. if (ret)
  108. goto err_free;
  109. ret = drmm_mode_config_init(drm_dev);
  110. if (ret)
  111. goto err_iommu_cleanup;
  112. rockchip_drm_mode_config_init(drm_dev);
  113. /* Try to bind all sub drivers. */
  114. ret = component_bind_all(dev, drm_dev);
  115. if (ret)
  116. goto err_iommu_cleanup;
  117. ret = drm_vblank_init(drm_dev, drm_dev->mode_config.num_crtc);
  118. if (ret)
  119. goto err_unbind_all;
  120. drm_mode_config_reset(drm_dev);
  121. /*
  122. * enable drm irq mode.
  123. * - with irq_enabled = true, we can use the vblank feature.
  124. */
  125. drm_dev->irq_enabled = true;
  126. ret = rockchip_drm_fbdev_init(drm_dev);
  127. if (ret)
  128. goto err_unbind_all;
  129. /* init kms poll for handling hpd */
  130. drm_kms_helper_poll_init(drm_dev);
  131. ret = drm_dev_register(drm_dev, 0);
  132. if (ret)
  133. goto err_kms_helper_poll_fini;
  134. return 0;
  135. err_kms_helper_poll_fini:
  136. drm_kms_helper_poll_fini(drm_dev);
  137. rockchip_drm_fbdev_fini(drm_dev);
  138. err_unbind_all:
  139. component_unbind_all(dev, drm_dev);
  140. err_iommu_cleanup:
  141. rockchip_iommu_cleanup(drm_dev);
  142. err_free:
  143. drm_dev_put(drm_dev);
  144. return ret;
  145. }
  146. static void rockchip_drm_unbind(struct device *dev)
  147. {
  148. struct drm_device *drm_dev = dev_get_drvdata(dev);
  149. drm_dev_unregister(drm_dev);
  150. rockchip_drm_fbdev_fini(drm_dev);
  151. drm_kms_helper_poll_fini(drm_dev);
  152. drm_atomic_helper_shutdown(drm_dev);
  153. component_unbind_all(dev, drm_dev);
  154. rockchip_iommu_cleanup(drm_dev);
  155. drm_dev_put(drm_dev);
  156. }
  157. static const struct file_operations rockchip_drm_driver_fops = {
  158. .owner = THIS_MODULE,
  159. .open = drm_open,
  160. .mmap = rockchip_gem_mmap,
  161. .poll = drm_poll,
  162. .read = drm_read,
  163. .unlocked_ioctl = drm_ioctl,
  164. .compat_ioctl = drm_compat_ioctl,
  165. .release = drm_release,
  166. };
  167. static struct drm_driver rockchip_drm_driver = {
  168. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
  169. .lastclose = drm_fb_helper_lastclose,
  170. .gem_vm_ops = &drm_gem_cma_vm_ops,
  171. .gem_free_object_unlocked = rockchip_gem_free_object,
  172. .dumb_create = rockchip_gem_dumb_create,
  173. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  174. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  175. .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table,
  176. .gem_prime_import_sg_table = rockchip_gem_prime_import_sg_table,
  177. .gem_prime_vmap = rockchip_gem_prime_vmap,
  178. .gem_prime_vunmap = rockchip_gem_prime_vunmap,
  179. .gem_prime_mmap = rockchip_gem_mmap_buf,
  180. .fops = &rockchip_drm_driver_fops,
  181. .name = DRIVER_NAME,
  182. .desc = DRIVER_DESC,
  183. .date = DRIVER_DATE,
  184. .major = DRIVER_MAJOR,
  185. .minor = DRIVER_MINOR,
  186. };
  187. #ifdef CONFIG_PM_SLEEP
  188. static int rockchip_drm_sys_suspend(struct device *dev)
  189. {
  190. struct drm_device *drm = dev_get_drvdata(dev);
  191. return drm_mode_config_helper_suspend(drm);
  192. }
  193. static int rockchip_drm_sys_resume(struct device *dev)
  194. {
  195. struct drm_device *drm = dev_get_drvdata(dev);
  196. return drm_mode_config_helper_resume(drm);
  197. }
  198. #endif
  199. static const struct dev_pm_ops rockchip_drm_pm_ops = {
  200. SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
  201. rockchip_drm_sys_resume)
  202. };
  203. #define MAX_ROCKCHIP_SUB_DRIVERS 16
  204. static struct platform_driver *rockchip_sub_drivers[MAX_ROCKCHIP_SUB_DRIVERS];
  205. static int num_rockchip_sub_drivers;
  206. /*
  207. * Check if a vop endpoint is leading to a rockchip subdriver or bridge.
  208. * Should be called from the component bind stage of the drivers
  209. * to ensure that all subdrivers are probed.
  210. *
  211. * @ep: endpoint of a rockchip vop
  212. *
  213. * returns true if subdriver, false if external bridge and -ENODEV
  214. * if remote port does not contain a device.
  215. */
  216. int rockchip_drm_endpoint_is_subdriver(struct device_node *ep)
  217. {
  218. struct device_node *node = of_graph_get_remote_port_parent(ep);
  219. struct platform_device *pdev;
  220. struct device_driver *drv;
  221. int i;
  222. if (!node)
  223. return -ENODEV;
  224. /* status disabled will prevent creation of platform-devices */
  225. pdev = of_find_device_by_node(node);
  226. of_node_put(node);
  227. if (!pdev)
  228. return -ENODEV;
  229. /*
  230. * All rockchip subdrivers have probed at this point, so
  231. * any device not having a driver now is an external bridge.
  232. */
  233. drv = pdev->dev.driver;
  234. if (!drv) {
  235. platform_device_put(pdev);
  236. return false;
  237. }
  238. for (i = 0; i < num_rockchip_sub_drivers; i++) {
  239. if (rockchip_sub_drivers[i] == to_platform_driver(drv)) {
  240. platform_device_put(pdev);
  241. return true;
  242. }
  243. }
  244. platform_device_put(pdev);
  245. return false;
  246. }
  247. static int compare_dev(struct device *dev, void *data)
  248. {
  249. return dev == (struct device *)data;
  250. }
  251. static void rockchip_drm_match_remove(struct device *dev)
  252. {
  253. struct device_link *link;
  254. list_for_each_entry(link, &dev->links.consumers, s_node)
  255. device_link_del(link);
  256. }
  257. static struct component_match *rockchip_drm_match_add(struct device *dev)
  258. {
  259. struct component_match *match = NULL;
  260. int i;
  261. for (i = 0; i < num_rockchip_sub_drivers; i++) {
  262. struct platform_driver *drv = rockchip_sub_drivers[i];
  263. struct device *p = NULL, *d;
  264. do {
  265. d = platform_find_device_by_driver(p, &drv->driver);
  266. put_device(p);
  267. p = d;
  268. if (!d)
  269. break;
  270. device_link_add(dev, d, DL_FLAG_STATELESS);
  271. component_match_add(dev, &match, compare_dev, d);
  272. } while (true);
  273. }
  274. if (IS_ERR(match))
  275. rockchip_drm_match_remove(dev);
  276. return match ?: ERR_PTR(-ENODEV);
  277. }
  278. static const struct component_master_ops rockchip_drm_ops = {
  279. .bind = rockchip_drm_bind,
  280. .unbind = rockchip_drm_unbind,
  281. };
  282. static int rockchip_drm_platform_of_probe(struct device *dev)
  283. {
  284. struct device_node *np = dev->of_node;
  285. struct device_node *port;
  286. bool found = false;
  287. int i;
  288. if (!np)
  289. return -ENODEV;
  290. for (i = 0;; i++) {
  291. struct device_node *iommu;
  292. port = of_parse_phandle(np, "ports", i);
  293. if (!port)
  294. break;
  295. if (!of_device_is_available(port->parent)) {
  296. of_node_put(port);
  297. continue;
  298. }
  299. iommu = of_parse_phandle(port->parent, "iommus", 0);
  300. if (!iommu || !of_device_is_available(iommu->parent)) {
  301. DRM_DEV_DEBUG(dev,
  302. "no iommu attached for %pOF, using non-iommu buffers\n",
  303. port->parent);
  304. /*
  305. * if there is a crtc not support iommu, force set all
  306. * crtc use non-iommu buffer.
  307. */
  308. is_support_iommu = false;
  309. }
  310. found = true;
  311. of_node_put(iommu);
  312. of_node_put(port);
  313. }
  314. if (i == 0) {
  315. DRM_DEV_ERROR(dev, "missing 'ports' property\n");
  316. return -ENODEV;
  317. }
  318. if (!found) {
  319. DRM_DEV_ERROR(dev,
  320. "No available vop found for display-subsystem.\n");
  321. return -ENODEV;
  322. }
  323. return 0;
  324. }
  325. static int rockchip_drm_platform_probe(struct platform_device *pdev)
  326. {
  327. struct device *dev = &pdev->dev;
  328. struct component_match *match = NULL;
  329. int ret;
  330. ret = rockchip_drm_platform_of_probe(dev);
  331. if (ret)
  332. return ret;
  333. match = rockchip_drm_match_add(dev);
  334. if (IS_ERR(match))
  335. return PTR_ERR(match);
  336. ret = component_master_add_with_match(dev, &rockchip_drm_ops, match);
  337. if (ret < 0) {
  338. rockchip_drm_match_remove(dev);
  339. return ret;
  340. }
  341. return 0;
  342. }
  343. static int rockchip_drm_platform_remove(struct platform_device *pdev)
  344. {
  345. component_master_del(&pdev->dev, &rockchip_drm_ops);
  346. rockchip_drm_match_remove(&pdev->dev);
  347. return 0;
  348. }
  349. static void rockchip_drm_platform_shutdown(struct platform_device *pdev)
  350. {
  351. struct drm_device *drm = platform_get_drvdata(pdev);
  352. if (drm)
  353. drm_atomic_helper_shutdown(drm);
  354. }
  355. static const struct of_device_id rockchip_drm_dt_ids[] = {
  356. { .compatible = "rockchip,display-subsystem", },
  357. { /* sentinel */ },
  358. };
  359. MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
  360. static struct platform_driver rockchip_drm_platform_driver = {
  361. .probe = rockchip_drm_platform_probe,
  362. .remove = rockchip_drm_platform_remove,
  363. .shutdown = rockchip_drm_platform_shutdown,
  364. .driver = {
  365. .name = "rockchip-drm",
  366. .of_match_table = rockchip_drm_dt_ids,
  367. .pm = &rockchip_drm_pm_ops,
  368. },
  369. };
  370. #define ADD_ROCKCHIP_SUB_DRIVER(drv, cond) { \
  371. if (IS_ENABLED(cond) && \
  372. !WARN_ON(num_rockchip_sub_drivers >= MAX_ROCKCHIP_SUB_DRIVERS)) \
  373. rockchip_sub_drivers[num_rockchip_sub_drivers++] = &drv; \
  374. }
  375. static int __init rockchip_drm_init(void)
  376. {
  377. int ret;
  378. num_rockchip_sub_drivers = 0;
  379. ADD_ROCKCHIP_SUB_DRIVER(vop_platform_driver, CONFIG_DRM_ROCKCHIP);
  380. ADD_ROCKCHIP_SUB_DRIVER(rockchip_lvds_driver,
  381. CONFIG_ROCKCHIP_LVDS);
  382. ADD_ROCKCHIP_SUB_DRIVER(rockchip_dp_driver,
  383. CONFIG_ROCKCHIP_ANALOGIX_DP);
  384. ADD_ROCKCHIP_SUB_DRIVER(cdn_dp_driver, CONFIG_ROCKCHIP_CDN_DP);
  385. ADD_ROCKCHIP_SUB_DRIVER(dw_hdmi_rockchip_pltfm_driver,
  386. CONFIG_ROCKCHIP_DW_HDMI);
  387. ADD_ROCKCHIP_SUB_DRIVER(dw_mipi_dsi_rockchip_driver,
  388. CONFIG_ROCKCHIP_DW_MIPI_DSI);
  389. ADD_ROCKCHIP_SUB_DRIVER(inno_hdmi_driver, CONFIG_ROCKCHIP_INNO_HDMI);
  390. ADD_ROCKCHIP_SUB_DRIVER(rk3066_hdmi_driver,
  391. CONFIG_ROCKCHIP_RK3066_HDMI);
  392. ret = platform_register_drivers(rockchip_sub_drivers,
  393. num_rockchip_sub_drivers);
  394. if (ret)
  395. return ret;
  396. ret = platform_driver_register(&rockchip_drm_platform_driver);
  397. if (ret)
  398. goto err_unreg_drivers;
  399. return 0;
  400. err_unreg_drivers:
  401. platform_unregister_drivers(rockchip_sub_drivers,
  402. num_rockchip_sub_drivers);
  403. return ret;
  404. }
  405. static void __exit rockchip_drm_fini(void)
  406. {
  407. platform_driver_unregister(&rockchip_drm_platform_driver);
  408. platform_unregister_drivers(rockchip_sub_drivers,
  409. num_rockchip_sub_drivers);
  410. }
  411. module_init(rockchip_drm_init);
  412. module_exit(rockchip_drm_fini);
  413. MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
  414. MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
  415. MODULE_LICENSE("GPL v2");