rockchip_drm_drv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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/component.h>
  27. #include <linux/console.h>
  28. #include <linux/iommu.h>
  29. #include "rockchip_drm_drv.h"
  30. #include "rockchip_drm_fb.h"
  31. #include "rockchip_drm_fbdev.h"
  32. #include "rockchip_drm_gem.h"
  33. #define DRIVER_NAME "rockchip"
  34. #define DRIVER_DESC "RockChip Soc DRM"
  35. #define DRIVER_DATE "20140818"
  36. #define DRIVER_MAJOR 1
  37. #define DRIVER_MINOR 0
  38. static bool is_support_iommu = true;
  39. static struct drm_driver rockchip_drm_driver;
  40. /*
  41. * Attach a (component) device to the shared drm dma mapping from master drm
  42. * device. This is used by the VOPs to map GEM buffers to a common DMA
  43. * mapping.
  44. */
  45. int rockchip_drm_dma_attach_device(struct drm_device *drm_dev,
  46. struct device *dev)
  47. {
  48. struct rockchip_drm_private *private = drm_dev->dev_private;
  49. int ret;
  50. if (!is_support_iommu)
  51. return 0;
  52. ret = iommu_attach_device(private->domain, dev);
  53. if (ret) {
  54. dev_err(dev, "Failed to attach iommu device\n");
  55. return ret;
  56. }
  57. return 0;
  58. }
  59. void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
  60. struct device *dev)
  61. {
  62. struct rockchip_drm_private *private = drm_dev->dev_private;
  63. struct iommu_domain *domain = private->domain;
  64. if (!is_support_iommu)
  65. return;
  66. iommu_detach_device(domain, dev);
  67. }
  68. static int rockchip_drm_init_iommu(struct drm_device *drm_dev)
  69. {
  70. struct rockchip_drm_private *private = drm_dev->dev_private;
  71. struct iommu_domain_geometry *geometry;
  72. u64 start, end;
  73. if (!is_support_iommu)
  74. return 0;
  75. private->domain = iommu_domain_alloc(&platform_bus_type);
  76. if (!private->domain)
  77. return -ENOMEM;
  78. geometry = &private->domain->geometry;
  79. start = geometry->aperture_start;
  80. end = geometry->aperture_end;
  81. DRM_DEBUG("IOMMU context initialized (aperture: %#llx-%#llx)\n",
  82. start, end);
  83. drm_mm_init(&private->mm, start, end - start + 1);
  84. mutex_init(&private->mm_lock);
  85. return 0;
  86. }
  87. static void rockchip_iommu_cleanup(struct drm_device *drm_dev)
  88. {
  89. struct rockchip_drm_private *private = drm_dev->dev_private;
  90. if (!is_support_iommu)
  91. return;
  92. drm_mm_takedown(&private->mm);
  93. iommu_domain_free(private->domain);
  94. }
  95. static int rockchip_drm_bind(struct device *dev)
  96. {
  97. struct drm_device *drm_dev;
  98. struct rockchip_drm_private *private;
  99. int ret;
  100. drm_dev = drm_dev_alloc(&rockchip_drm_driver, dev);
  101. if (IS_ERR(drm_dev))
  102. return PTR_ERR(drm_dev);
  103. dev_set_drvdata(dev, drm_dev);
  104. private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
  105. if (!private) {
  106. ret = -ENOMEM;
  107. goto err_free;
  108. }
  109. drm_dev->dev_private = private;
  110. INIT_LIST_HEAD(&private->psr_list);
  111. spin_lock_init(&private->psr_list_lock);
  112. ret = rockchip_drm_init_iommu(drm_dev);
  113. if (ret)
  114. goto err_free;
  115. drm_mode_config_init(drm_dev);
  116. rockchip_drm_mode_config_init(drm_dev);
  117. /* Try to bind all sub drivers. */
  118. ret = component_bind_all(dev, drm_dev);
  119. if (ret)
  120. goto err_mode_config_cleanup;
  121. ret = drm_vblank_init(drm_dev, drm_dev->mode_config.num_crtc);
  122. if (ret)
  123. goto err_unbind_all;
  124. drm_mode_config_reset(drm_dev);
  125. /*
  126. * enable drm irq mode.
  127. * - with irq_enabled = true, we can use the vblank feature.
  128. */
  129. drm_dev->irq_enabled = true;
  130. ret = rockchip_drm_fbdev_init(drm_dev);
  131. if (ret)
  132. goto err_unbind_all;
  133. /* init kms poll for handling hpd */
  134. drm_kms_helper_poll_init(drm_dev);
  135. ret = drm_dev_register(drm_dev, 0);
  136. if (ret)
  137. goto err_kms_helper_poll_fini;
  138. return 0;
  139. err_kms_helper_poll_fini:
  140. drm_kms_helper_poll_fini(drm_dev);
  141. rockchip_drm_fbdev_fini(drm_dev);
  142. err_unbind_all:
  143. component_unbind_all(dev, drm_dev);
  144. err_mode_config_cleanup:
  145. drm_mode_config_cleanup(drm_dev);
  146. rockchip_iommu_cleanup(drm_dev);
  147. err_free:
  148. drm_dev->dev_private = NULL;
  149. dev_set_drvdata(dev, NULL);
  150. drm_dev_unref(drm_dev);
  151. return ret;
  152. }
  153. static void rockchip_drm_unbind(struct device *dev)
  154. {
  155. struct drm_device *drm_dev = dev_get_drvdata(dev);
  156. drm_dev_unregister(drm_dev);
  157. rockchip_drm_fbdev_fini(drm_dev);
  158. drm_kms_helper_poll_fini(drm_dev);
  159. drm_atomic_helper_shutdown(drm_dev);
  160. component_unbind_all(dev, drm_dev);
  161. drm_mode_config_cleanup(drm_dev);
  162. rockchip_iommu_cleanup(drm_dev);
  163. drm_dev->dev_private = NULL;
  164. dev_set_drvdata(dev, NULL);
  165. drm_dev_unref(drm_dev);
  166. }
  167. static void rockchip_drm_lastclose(struct drm_device *dev)
  168. {
  169. struct rockchip_drm_private *priv = dev->dev_private;
  170. drm_fb_helper_restore_fbdev_mode_unlocked(&priv->fbdev_helper);
  171. }
  172. static const struct file_operations rockchip_drm_driver_fops = {
  173. .owner = THIS_MODULE,
  174. .open = drm_open,
  175. .mmap = rockchip_gem_mmap,
  176. .poll = drm_poll,
  177. .read = drm_read,
  178. .unlocked_ioctl = drm_ioctl,
  179. .compat_ioctl = drm_compat_ioctl,
  180. .release = drm_release,
  181. };
  182. static struct drm_driver rockchip_drm_driver = {
  183. .driver_features = DRIVER_MODESET | DRIVER_GEM |
  184. DRIVER_PRIME | DRIVER_ATOMIC,
  185. .lastclose = rockchip_drm_lastclose,
  186. .gem_vm_ops = &drm_gem_cma_vm_ops,
  187. .gem_free_object_unlocked = rockchip_gem_free_object,
  188. .dumb_create = rockchip_gem_dumb_create,
  189. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  190. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  191. .gem_prime_import = drm_gem_prime_import,
  192. .gem_prime_export = drm_gem_prime_export,
  193. .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table,
  194. .gem_prime_vmap = rockchip_gem_prime_vmap,
  195. .gem_prime_vunmap = rockchip_gem_prime_vunmap,
  196. .gem_prime_mmap = rockchip_gem_mmap_buf,
  197. .fops = &rockchip_drm_driver_fops,
  198. .name = DRIVER_NAME,
  199. .desc = DRIVER_DESC,
  200. .date = DRIVER_DATE,
  201. .major = DRIVER_MAJOR,
  202. .minor = DRIVER_MINOR,
  203. };
  204. #ifdef CONFIG_PM_SLEEP
  205. static void rockchip_drm_fb_suspend(struct drm_device *drm)
  206. {
  207. struct rockchip_drm_private *priv = drm->dev_private;
  208. console_lock();
  209. drm_fb_helper_set_suspend(&priv->fbdev_helper, 1);
  210. console_unlock();
  211. }
  212. static void rockchip_drm_fb_resume(struct drm_device *drm)
  213. {
  214. struct rockchip_drm_private *priv = drm->dev_private;
  215. console_lock();
  216. drm_fb_helper_set_suspend(&priv->fbdev_helper, 0);
  217. console_unlock();
  218. }
  219. static int rockchip_drm_sys_suspend(struct device *dev)
  220. {
  221. struct drm_device *drm = dev_get_drvdata(dev);
  222. struct rockchip_drm_private *priv;
  223. if (!drm)
  224. return 0;
  225. drm_kms_helper_poll_disable(drm);
  226. rockchip_drm_fb_suspend(drm);
  227. priv = drm->dev_private;
  228. priv->state = drm_atomic_helper_suspend(drm);
  229. if (IS_ERR(priv->state)) {
  230. rockchip_drm_fb_resume(drm);
  231. drm_kms_helper_poll_enable(drm);
  232. return PTR_ERR(priv->state);
  233. }
  234. return 0;
  235. }
  236. static int rockchip_drm_sys_resume(struct device *dev)
  237. {
  238. struct drm_device *drm = dev_get_drvdata(dev);
  239. struct rockchip_drm_private *priv;
  240. if (!drm)
  241. return 0;
  242. priv = drm->dev_private;
  243. drm_atomic_helper_resume(drm, priv->state);
  244. rockchip_drm_fb_resume(drm);
  245. drm_kms_helper_poll_enable(drm);
  246. return 0;
  247. }
  248. #endif
  249. static const struct dev_pm_ops rockchip_drm_pm_ops = {
  250. SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
  251. rockchip_drm_sys_resume)
  252. };
  253. #define MAX_ROCKCHIP_SUB_DRIVERS 16
  254. static struct platform_driver *rockchip_sub_drivers[MAX_ROCKCHIP_SUB_DRIVERS];
  255. static int num_rockchip_sub_drivers;
  256. static int compare_dev(struct device *dev, void *data)
  257. {
  258. return dev == (struct device *)data;
  259. }
  260. static struct component_match *rockchip_drm_match_add(struct device *dev)
  261. {
  262. struct component_match *match = NULL;
  263. int i;
  264. for (i = 0; i < num_rockchip_sub_drivers; i++) {
  265. struct platform_driver *drv = rockchip_sub_drivers[i];
  266. struct device *p = NULL, *d;
  267. do {
  268. d = bus_find_device(&platform_bus_type, p, &drv->driver,
  269. (void *)platform_bus_type.match);
  270. put_device(p);
  271. p = d;
  272. if (!d)
  273. break;
  274. component_match_add(dev, &match, compare_dev, d);
  275. } while (true);
  276. }
  277. return match ?: ERR_PTR(-ENODEV);
  278. }
  279. static const struct component_master_ops rockchip_drm_ops = {
  280. .bind = rockchip_drm_bind,
  281. .unbind = rockchip_drm_unbind,
  282. };
  283. static int rockchip_drm_platform_of_probe(struct device *dev)
  284. {
  285. struct device_node *np = dev->of_node;
  286. struct device_node *port;
  287. bool found = false;
  288. int i;
  289. if (!np)
  290. return -ENODEV;
  291. for (i = 0;; i++) {
  292. struct device_node *iommu;
  293. port = of_parse_phandle(np, "ports", i);
  294. if (!port)
  295. break;
  296. if (!of_device_is_available(port->parent)) {
  297. of_node_put(port);
  298. continue;
  299. }
  300. iommu = of_parse_phandle(port->parent, "iommus", 0);
  301. if (!iommu || !of_device_is_available(iommu->parent)) {
  302. dev_dbg(dev, "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. dev_err(dev, "missing 'ports' property\n");
  316. return -ENODEV;
  317. }
  318. if (!found) {
  319. dev_err(dev, "No available vop found for display-subsystem.\n");
  320. return -ENODEV;
  321. }
  322. return 0;
  323. }
  324. static int rockchip_drm_platform_probe(struct platform_device *pdev)
  325. {
  326. struct device *dev = &pdev->dev;
  327. struct component_match *match = NULL;
  328. int ret;
  329. ret = rockchip_drm_platform_of_probe(dev);
  330. if (ret)
  331. return ret;
  332. match = rockchip_drm_match_add(dev);
  333. if (IS_ERR(match))
  334. return PTR_ERR(match);
  335. return component_master_add_with_match(dev, &rockchip_drm_ops, match);
  336. }
  337. static int rockchip_drm_platform_remove(struct platform_device *pdev)
  338. {
  339. component_master_del(&pdev->dev, &rockchip_drm_ops);
  340. return 0;
  341. }
  342. static const struct of_device_id rockchip_drm_dt_ids[] = {
  343. { .compatible = "rockchip,display-subsystem", },
  344. { /* sentinel */ },
  345. };
  346. MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
  347. static struct platform_driver rockchip_drm_platform_driver = {
  348. .probe = rockchip_drm_platform_probe,
  349. .remove = rockchip_drm_platform_remove,
  350. .driver = {
  351. .name = "rockchip-drm",
  352. .of_match_table = rockchip_drm_dt_ids,
  353. .pm = &rockchip_drm_pm_ops,
  354. },
  355. };
  356. #define ADD_ROCKCHIP_SUB_DRIVER(drv, cond) { \
  357. if (IS_ENABLED(cond) && \
  358. !WARN_ON(num_rockchip_sub_drivers >= MAX_ROCKCHIP_SUB_DRIVERS)) \
  359. rockchip_sub_drivers[num_rockchip_sub_drivers++] = &drv; \
  360. }
  361. static int __init rockchip_drm_init(void)
  362. {
  363. int ret;
  364. num_rockchip_sub_drivers = 0;
  365. ADD_ROCKCHIP_SUB_DRIVER(vop_platform_driver, CONFIG_DRM_ROCKCHIP);
  366. ADD_ROCKCHIP_SUB_DRIVER(rockchip_dp_driver,
  367. CONFIG_ROCKCHIP_ANALOGIX_DP);
  368. ADD_ROCKCHIP_SUB_DRIVER(cdn_dp_driver, CONFIG_ROCKCHIP_CDN_DP);
  369. ADD_ROCKCHIP_SUB_DRIVER(dw_hdmi_rockchip_pltfm_driver,
  370. CONFIG_ROCKCHIP_DW_HDMI);
  371. ADD_ROCKCHIP_SUB_DRIVER(dw_mipi_dsi_driver,
  372. CONFIG_ROCKCHIP_DW_MIPI_DSI);
  373. ADD_ROCKCHIP_SUB_DRIVER(inno_hdmi_driver, CONFIG_ROCKCHIP_INNO_HDMI);
  374. ret = platform_register_drivers(rockchip_sub_drivers,
  375. num_rockchip_sub_drivers);
  376. if (ret)
  377. return ret;
  378. ret = platform_driver_register(&rockchip_drm_platform_driver);
  379. if (ret)
  380. goto err_unreg_drivers;
  381. return 0;
  382. err_unreg_drivers:
  383. platform_unregister_drivers(rockchip_sub_drivers,
  384. num_rockchip_sub_drivers);
  385. return ret;
  386. }
  387. static void __exit rockchip_drm_fini(void)
  388. {
  389. platform_driver_unregister(&rockchip_drm_platform_driver);
  390. platform_unregister_drivers(rockchip_sub_drivers,
  391. num_rockchip_sub_drivers);
  392. }
  393. module_init(rockchip_drm_init);
  394. module_exit(rockchip_drm_fini);
  395. MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
  396. MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
  397. MODULE_LICENSE("GPL v2");