rockchip_drm_drv.c 12 KB

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