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. int rockchip_register_crtc_funcs(struct drm_crtc *crtc,
  69. const struct rockchip_crtc_funcs *crtc_funcs)
  70. {
  71. int pipe = drm_crtc_index(crtc);
  72. struct rockchip_drm_private *priv = crtc->dev->dev_private;
  73. if (pipe >= ROCKCHIP_MAX_CRTC)
  74. return -EINVAL;
  75. priv->crtc_funcs[pipe] = crtc_funcs;
  76. return 0;
  77. }
  78. void rockchip_unregister_crtc_funcs(struct drm_crtc *crtc)
  79. {
  80. int pipe = drm_crtc_index(crtc);
  81. struct rockchip_drm_private *priv = crtc->dev->dev_private;
  82. if (pipe >= ROCKCHIP_MAX_CRTC)
  83. return;
  84. priv->crtc_funcs[pipe] = NULL;
  85. }
  86. static int rockchip_drm_crtc_enable_vblank(struct drm_device *dev,
  87. unsigned int pipe)
  88. {
  89. struct rockchip_drm_private *priv = dev->dev_private;
  90. struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
  91. if (crtc && priv->crtc_funcs[pipe] &&
  92. priv->crtc_funcs[pipe]->enable_vblank)
  93. return priv->crtc_funcs[pipe]->enable_vblank(crtc);
  94. return 0;
  95. }
  96. static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev,
  97. unsigned int pipe)
  98. {
  99. struct rockchip_drm_private *priv = dev->dev_private;
  100. struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
  101. if (crtc && priv->crtc_funcs[pipe] &&
  102. priv->crtc_funcs[pipe]->enable_vblank)
  103. priv->crtc_funcs[pipe]->disable_vblank(crtc);
  104. }
  105. static int rockchip_drm_init_iommu(struct drm_device *drm_dev)
  106. {
  107. struct rockchip_drm_private *private = drm_dev->dev_private;
  108. struct iommu_domain_geometry *geometry;
  109. u64 start, end;
  110. if (!is_support_iommu)
  111. return 0;
  112. private->domain = iommu_domain_alloc(&platform_bus_type);
  113. if (!private->domain)
  114. return -ENOMEM;
  115. geometry = &private->domain->geometry;
  116. start = geometry->aperture_start;
  117. end = geometry->aperture_end;
  118. DRM_DEBUG("IOMMU context initialized (aperture: %#llx-%#llx)\n",
  119. start, end);
  120. drm_mm_init(&private->mm, start, end - start + 1);
  121. mutex_init(&private->mm_lock);
  122. return 0;
  123. }
  124. static void rockchip_iommu_cleanup(struct drm_device *drm_dev)
  125. {
  126. struct rockchip_drm_private *private = drm_dev->dev_private;
  127. if (!is_support_iommu)
  128. return;
  129. drm_mm_takedown(&private->mm);
  130. iommu_domain_free(private->domain);
  131. }
  132. static int rockchip_drm_bind(struct device *dev)
  133. {
  134. struct drm_device *drm_dev;
  135. struct rockchip_drm_private *private;
  136. int ret;
  137. drm_dev = drm_dev_alloc(&rockchip_drm_driver, dev);
  138. if (IS_ERR(drm_dev))
  139. return PTR_ERR(drm_dev);
  140. dev_set_drvdata(dev, drm_dev);
  141. private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
  142. if (!private) {
  143. ret = -ENOMEM;
  144. goto err_free;
  145. }
  146. drm_dev->dev_private = private;
  147. INIT_LIST_HEAD(&private->psr_list);
  148. spin_lock_init(&private->psr_list_lock);
  149. drm_mode_config_init(drm_dev);
  150. rockchip_drm_mode_config_init(drm_dev);
  151. ret = rockchip_drm_init_iommu(drm_dev);
  152. if (ret)
  153. goto err_config_cleanup;
  154. /* Try to bind all sub drivers. */
  155. ret = component_bind_all(dev, drm_dev);
  156. if (ret)
  157. goto err_iommu_cleanup;
  158. /* init kms poll for handling hpd */
  159. drm_kms_helper_poll_init(drm_dev);
  160. /*
  161. * enable drm irq mode.
  162. * - with irq_enabled = true, we can use the vblank feature.
  163. */
  164. drm_dev->irq_enabled = true;
  165. ret = drm_vblank_init(drm_dev, ROCKCHIP_MAX_CRTC);
  166. if (ret)
  167. goto err_kms_helper_poll_fini;
  168. drm_mode_config_reset(drm_dev);
  169. ret = rockchip_drm_fbdev_init(drm_dev);
  170. if (ret)
  171. goto err_vblank_cleanup;
  172. ret = drm_dev_register(drm_dev, 0);
  173. if (ret)
  174. goto err_fbdev_fini;
  175. return 0;
  176. err_fbdev_fini:
  177. rockchip_drm_fbdev_fini(drm_dev);
  178. err_vblank_cleanup:
  179. drm_vblank_cleanup(drm_dev);
  180. err_kms_helper_poll_fini:
  181. drm_kms_helper_poll_fini(drm_dev);
  182. component_unbind_all(dev, drm_dev);
  183. err_iommu_cleanup:
  184. rockchip_iommu_cleanup(drm_dev);
  185. err_config_cleanup:
  186. drm_mode_config_cleanup(drm_dev);
  187. drm_dev->dev_private = NULL;
  188. err_free:
  189. drm_dev_unref(drm_dev);
  190. return ret;
  191. }
  192. static void rockchip_drm_unbind(struct device *dev)
  193. {
  194. struct drm_device *drm_dev = dev_get_drvdata(dev);
  195. rockchip_drm_fbdev_fini(drm_dev);
  196. drm_vblank_cleanup(drm_dev);
  197. drm_kms_helper_poll_fini(drm_dev);
  198. component_unbind_all(dev, drm_dev);
  199. rockchip_iommu_cleanup(drm_dev);
  200. drm_mode_config_cleanup(drm_dev);
  201. drm_dev->dev_private = NULL;
  202. drm_dev_unregister(drm_dev);
  203. drm_dev_unref(drm_dev);
  204. dev_set_drvdata(dev, NULL);
  205. }
  206. static void rockchip_drm_lastclose(struct drm_device *dev)
  207. {
  208. struct rockchip_drm_private *priv = dev->dev_private;
  209. drm_fb_helper_restore_fbdev_mode_unlocked(&priv->fbdev_helper);
  210. }
  211. static const struct file_operations rockchip_drm_driver_fops = {
  212. .owner = THIS_MODULE,
  213. .open = drm_open,
  214. .mmap = rockchip_gem_mmap,
  215. .poll = drm_poll,
  216. .read = drm_read,
  217. .unlocked_ioctl = drm_ioctl,
  218. .compat_ioctl = drm_compat_ioctl,
  219. .release = drm_release,
  220. };
  221. static struct drm_driver rockchip_drm_driver = {
  222. .driver_features = DRIVER_MODESET | DRIVER_GEM |
  223. DRIVER_PRIME | DRIVER_ATOMIC,
  224. .lastclose = rockchip_drm_lastclose,
  225. .get_vblank_counter = drm_vblank_no_hw_counter,
  226. .enable_vblank = rockchip_drm_crtc_enable_vblank,
  227. .disable_vblank = rockchip_drm_crtc_disable_vblank,
  228. .gem_vm_ops = &drm_gem_cma_vm_ops,
  229. .gem_free_object_unlocked = rockchip_gem_free_object,
  230. .dumb_create = rockchip_gem_dumb_create,
  231. .dumb_map_offset = rockchip_gem_dumb_map_offset,
  232. .dumb_destroy = drm_gem_dumb_destroy,
  233. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  234. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  235. .gem_prime_import = drm_gem_prime_import,
  236. .gem_prime_export = drm_gem_prime_export,
  237. .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table,
  238. .gem_prime_vmap = rockchip_gem_prime_vmap,
  239. .gem_prime_vunmap = rockchip_gem_prime_vunmap,
  240. .gem_prime_mmap = rockchip_gem_mmap_buf,
  241. .fops = &rockchip_drm_driver_fops,
  242. .name = DRIVER_NAME,
  243. .desc = DRIVER_DESC,
  244. .date = DRIVER_DATE,
  245. .major = DRIVER_MAJOR,
  246. .minor = DRIVER_MINOR,
  247. };
  248. #ifdef CONFIG_PM_SLEEP
  249. static void rockchip_drm_fb_suspend(struct drm_device *drm)
  250. {
  251. struct rockchip_drm_private *priv = drm->dev_private;
  252. console_lock();
  253. drm_fb_helper_set_suspend(&priv->fbdev_helper, 1);
  254. console_unlock();
  255. }
  256. static void rockchip_drm_fb_resume(struct drm_device *drm)
  257. {
  258. struct rockchip_drm_private *priv = drm->dev_private;
  259. console_lock();
  260. drm_fb_helper_set_suspend(&priv->fbdev_helper, 0);
  261. console_unlock();
  262. }
  263. static int rockchip_drm_sys_suspend(struct device *dev)
  264. {
  265. struct drm_device *drm = dev_get_drvdata(dev);
  266. struct rockchip_drm_private *priv = drm->dev_private;
  267. drm_kms_helper_poll_disable(drm);
  268. rockchip_drm_fb_suspend(drm);
  269. priv->state = drm_atomic_helper_suspend(drm);
  270. if (IS_ERR(priv->state)) {
  271. rockchip_drm_fb_resume(drm);
  272. drm_kms_helper_poll_enable(drm);
  273. return PTR_ERR(priv->state);
  274. }
  275. return 0;
  276. }
  277. static int rockchip_drm_sys_resume(struct device *dev)
  278. {
  279. struct drm_device *drm = dev_get_drvdata(dev);
  280. struct rockchip_drm_private *priv = drm->dev_private;
  281. drm_atomic_helper_resume(drm, priv->state);
  282. rockchip_drm_fb_resume(drm);
  283. drm_kms_helper_poll_enable(drm);
  284. return 0;
  285. }
  286. #endif
  287. static const struct dev_pm_ops rockchip_drm_pm_ops = {
  288. SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
  289. rockchip_drm_sys_resume)
  290. };
  291. static int compare_of(struct device *dev, void *data)
  292. {
  293. struct device_node *np = data;
  294. return dev->of_node == np;
  295. }
  296. static void rockchip_add_endpoints(struct device *dev,
  297. struct component_match **match,
  298. struct device_node *port)
  299. {
  300. struct device_node *ep, *remote;
  301. for_each_child_of_node(port, ep) {
  302. remote = of_graph_get_remote_port_parent(ep);
  303. if (!remote || !of_device_is_available(remote)) {
  304. of_node_put(remote);
  305. continue;
  306. } else if (!of_device_is_available(remote->parent)) {
  307. dev_warn(dev, "parent device of %s is not available\n",
  308. remote->full_name);
  309. of_node_put(remote);
  310. continue;
  311. }
  312. drm_of_component_match_add(dev, match, compare_of, remote);
  313. of_node_put(remote);
  314. }
  315. }
  316. static const struct component_master_ops rockchip_drm_ops = {
  317. .bind = rockchip_drm_bind,
  318. .unbind = rockchip_drm_unbind,
  319. };
  320. static int rockchip_drm_platform_probe(struct platform_device *pdev)
  321. {
  322. struct device *dev = &pdev->dev;
  323. struct component_match *match = NULL;
  324. struct device_node *np = dev->of_node;
  325. struct device_node *port;
  326. int i;
  327. if (!np)
  328. return -ENODEV;
  329. /*
  330. * Bind the crtc ports first, so that
  331. * drm_of_find_possible_crtcs called from encoder .bind callbacks
  332. * works as expected.
  333. */
  334. for (i = 0;; i++) {
  335. struct device_node *iommu;
  336. port = of_parse_phandle(np, "ports", i);
  337. if (!port)
  338. break;
  339. if (!of_device_is_available(port->parent)) {
  340. of_node_put(port);
  341. continue;
  342. }
  343. iommu = of_parse_phandle(port->parent, "iommus", 0);
  344. if (!iommu || !of_device_is_available(iommu->parent)) {
  345. dev_dbg(dev, "no iommu attached for %s, using non-iommu buffers\n",
  346. port->parent->full_name);
  347. /*
  348. * if there is a crtc not support iommu, force set all
  349. * crtc use non-iommu buffer.
  350. */
  351. is_support_iommu = false;
  352. }
  353. of_node_put(iommu);
  354. drm_of_component_match_add(dev, &match, compare_of,
  355. port->parent);
  356. of_node_put(port);
  357. }
  358. if (i == 0) {
  359. dev_err(dev, "missing 'ports' property\n");
  360. return -ENODEV;
  361. }
  362. if (!match) {
  363. dev_err(dev, "No available vop found for display-subsystem.\n");
  364. return -ENODEV;
  365. }
  366. /*
  367. * For each bound crtc, bind the encoders attached to its
  368. * remote endpoint.
  369. */
  370. for (i = 0;; i++) {
  371. port = of_parse_phandle(np, "ports", i);
  372. if (!port)
  373. break;
  374. if (!of_device_is_available(port->parent)) {
  375. of_node_put(port);
  376. continue;
  377. }
  378. rockchip_add_endpoints(dev, &match, port);
  379. of_node_put(port);
  380. }
  381. return component_master_add_with_match(dev, &rockchip_drm_ops, match);
  382. }
  383. static int rockchip_drm_platform_remove(struct platform_device *pdev)
  384. {
  385. component_master_del(&pdev->dev, &rockchip_drm_ops);
  386. return 0;
  387. }
  388. static const struct of_device_id rockchip_drm_dt_ids[] = {
  389. { .compatible = "rockchip,display-subsystem", },
  390. { /* sentinel */ },
  391. };
  392. MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
  393. static struct platform_driver rockchip_drm_platform_driver = {
  394. .probe = rockchip_drm_platform_probe,
  395. .remove = rockchip_drm_platform_remove,
  396. .driver = {
  397. .name = "rockchip-drm",
  398. .of_match_table = rockchip_drm_dt_ids,
  399. .pm = &rockchip_drm_pm_ops,
  400. },
  401. };
  402. module_platform_driver(rockchip_drm_platform_driver);
  403. MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
  404. MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
  405. MODULE_LICENSE("GPL v2");