rockchip_drm_drv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 <asm/dma-iommu.h>
  17. #include <drm/drmP.h>
  18. #include <drm/drm_crtc_helper.h>
  19. #include <drm/drm_fb_helper.h>
  20. #include <drm/drm_gem_cma_helper.h>
  21. #include <drm/drm_of.h>
  22. #include <linux/dma-mapping.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 "rockchip_drm_drv.h"
  29. #include "rockchip_drm_fb.h"
  30. #include "rockchip_drm_fbdev.h"
  31. #include "rockchip_drm_gem.h"
  32. #define DRIVER_NAME "rockchip"
  33. #define DRIVER_DESC "RockChip Soc DRM"
  34. #define DRIVER_DATE "20140818"
  35. #define DRIVER_MAJOR 1
  36. #define DRIVER_MINOR 0
  37. static bool is_support_iommu = true;
  38. static struct drm_driver rockchip_drm_driver;
  39. /*
  40. * Attach a (component) device to the shared drm dma mapping from master drm
  41. * device. This is used by the VOPs to map GEM buffers to a common DMA
  42. * mapping.
  43. */
  44. int rockchip_drm_dma_attach_device(struct drm_device *drm_dev,
  45. struct device *dev)
  46. {
  47. struct dma_iommu_mapping *mapping = drm_dev->dev->archdata.mapping;
  48. int ret;
  49. if (!is_support_iommu)
  50. return 0;
  51. ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
  52. if (ret)
  53. return ret;
  54. dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
  55. return arm_iommu_attach_device(dev, mapping);
  56. }
  57. void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
  58. struct device *dev)
  59. {
  60. if (!is_support_iommu)
  61. return;
  62. arm_iommu_detach_device(dev);
  63. }
  64. int rockchip_register_crtc_funcs(struct drm_crtc *crtc,
  65. const struct rockchip_crtc_funcs *crtc_funcs)
  66. {
  67. int pipe = drm_crtc_index(crtc);
  68. struct rockchip_drm_private *priv = crtc->dev->dev_private;
  69. if (pipe >= ROCKCHIP_MAX_CRTC)
  70. return -EINVAL;
  71. priv->crtc_funcs[pipe] = crtc_funcs;
  72. return 0;
  73. }
  74. void rockchip_unregister_crtc_funcs(struct drm_crtc *crtc)
  75. {
  76. int pipe = drm_crtc_index(crtc);
  77. struct rockchip_drm_private *priv = crtc->dev->dev_private;
  78. if (pipe >= ROCKCHIP_MAX_CRTC)
  79. return;
  80. priv->crtc_funcs[pipe] = NULL;
  81. }
  82. static struct drm_crtc *rockchip_crtc_from_pipe(struct drm_device *drm,
  83. int pipe)
  84. {
  85. struct drm_crtc *crtc;
  86. int i = 0;
  87. list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
  88. if (i++ == pipe)
  89. return crtc;
  90. return NULL;
  91. }
  92. static int rockchip_drm_crtc_enable_vblank(struct drm_device *dev,
  93. unsigned int pipe)
  94. {
  95. struct rockchip_drm_private *priv = dev->dev_private;
  96. struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
  97. if (crtc && priv->crtc_funcs[pipe] &&
  98. priv->crtc_funcs[pipe]->enable_vblank)
  99. return priv->crtc_funcs[pipe]->enable_vblank(crtc);
  100. return 0;
  101. }
  102. static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev,
  103. unsigned int pipe)
  104. {
  105. struct rockchip_drm_private *priv = dev->dev_private;
  106. struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
  107. if (crtc && priv->crtc_funcs[pipe] &&
  108. priv->crtc_funcs[pipe]->enable_vblank)
  109. priv->crtc_funcs[pipe]->disable_vblank(crtc);
  110. }
  111. static int rockchip_drm_bind(struct device *dev)
  112. {
  113. struct drm_device *drm_dev;
  114. struct rockchip_drm_private *private;
  115. struct dma_iommu_mapping *mapping = NULL;
  116. int ret;
  117. drm_dev = drm_dev_alloc(&rockchip_drm_driver, dev);
  118. if (IS_ERR(drm_dev))
  119. return PTR_ERR(drm_dev);
  120. dev_set_drvdata(dev, drm_dev);
  121. private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
  122. if (!private) {
  123. ret = -ENOMEM;
  124. goto err_free;
  125. }
  126. drm_dev->dev_private = private;
  127. INIT_LIST_HEAD(&private->psr_list);
  128. spin_lock_init(&private->psr_list_lock);
  129. drm_mode_config_init(drm_dev);
  130. rockchip_drm_mode_config_init(drm_dev);
  131. dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms),
  132. GFP_KERNEL);
  133. if (!dev->dma_parms) {
  134. ret = -ENOMEM;
  135. goto err_config_cleanup;
  136. }
  137. if (is_support_iommu) {
  138. /* TODO(djkurtz): fetch the mapping start/size from somewhere */
  139. mapping = arm_iommu_create_mapping(&platform_bus_type,
  140. 0x00000000,
  141. SZ_2G);
  142. if (IS_ERR(mapping)) {
  143. ret = PTR_ERR(mapping);
  144. goto err_config_cleanup;
  145. }
  146. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  147. if (ret)
  148. goto err_release_mapping;
  149. dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
  150. ret = arm_iommu_attach_device(dev, mapping);
  151. if (ret)
  152. goto err_release_mapping;
  153. }
  154. /* Try to bind all sub drivers. */
  155. ret = component_bind_all(dev, drm_dev);
  156. if (ret)
  157. goto err_detach_device;
  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. if (is_support_iommu)
  176. arm_iommu_release_mapping(mapping);
  177. return 0;
  178. err_fbdev_fini:
  179. rockchip_drm_fbdev_fini(drm_dev);
  180. err_vblank_cleanup:
  181. drm_vblank_cleanup(drm_dev);
  182. err_kms_helper_poll_fini:
  183. drm_kms_helper_poll_fini(drm_dev);
  184. component_unbind_all(dev, drm_dev);
  185. err_detach_device:
  186. if (is_support_iommu)
  187. arm_iommu_detach_device(dev);
  188. err_release_mapping:
  189. if (is_support_iommu)
  190. arm_iommu_release_mapping(mapping);
  191. err_config_cleanup:
  192. drm_mode_config_cleanup(drm_dev);
  193. drm_dev->dev_private = NULL;
  194. err_free:
  195. drm_dev_unref(drm_dev);
  196. return ret;
  197. }
  198. static void rockchip_drm_unbind(struct device *dev)
  199. {
  200. struct drm_device *drm_dev = dev_get_drvdata(dev);
  201. rockchip_drm_fbdev_fini(drm_dev);
  202. drm_vblank_cleanup(drm_dev);
  203. drm_kms_helper_poll_fini(drm_dev);
  204. component_unbind_all(dev, drm_dev);
  205. if (is_support_iommu)
  206. arm_iommu_detach_device(dev);
  207. drm_mode_config_cleanup(drm_dev);
  208. drm_dev->dev_private = NULL;
  209. drm_dev_unregister(drm_dev);
  210. drm_dev_unref(drm_dev);
  211. dev_set_drvdata(dev, NULL);
  212. }
  213. static void rockchip_drm_lastclose(struct drm_device *dev)
  214. {
  215. struct rockchip_drm_private *priv = dev->dev_private;
  216. drm_fb_helper_restore_fbdev_mode_unlocked(&priv->fbdev_helper);
  217. }
  218. static const struct file_operations rockchip_drm_driver_fops = {
  219. .owner = THIS_MODULE,
  220. .open = drm_open,
  221. .mmap = rockchip_gem_mmap,
  222. .poll = drm_poll,
  223. .read = drm_read,
  224. .unlocked_ioctl = drm_ioctl,
  225. .compat_ioctl = drm_compat_ioctl,
  226. .release = drm_release,
  227. };
  228. static struct drm_driver rockchip_drm_driver = {
  229. .driver_features = DRIVER_MODESET | DRIVER_GEM |
  230. DRIVER_PRIME | DRIVER_ATOMIC,
  231. .lastclose = rockchip_drm_lastclose,
  232. .get_vblank_counter = drm_vblank_no_hw_counter,
  233. .enable_vblank = rockchip_drm_crtc_enable_vblank,
  234. .disable_vblank = rockchip_drm_crtc_disable_vblank,
  235. .gem_vm_ops = &drm_gem_cma_vm_ops,
  236. .gem_free_object_unlocked = rockchip_gem_free_object,
  237. .dumb_create = rockchip_gem_dumb_create,
  238. .dumb_map_offset = rockchip_gem_dumb_map_offset,
  239. .dumb_destroy = drm_gem_dumb_destroy,
  240. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  241. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  242. .gem_prime_import = drm_gem_prime_import,
  243. .gem_prime_export = drm_gem_prime_export,
  244. .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table,
  245. .gem_prime_vmap = rockchip_gem_prime_vmap,
  246. .gem_prime_vunmap = rockchip_gem_prime_vunmap,
  247. .gem_prime_mmap = rockchip_gem_mmap_buf,
  248. .fops = &rockchip_drm_driver_fops,
  249. .name = DRIVER_NAME,
  250. .desc = DRIVER_DESC,
  251. .date = DRIVER_DATE,
  252. .major = DRIVER_MAJOR,
  253. .minor = DRIVER_MINOR,
  254. };
  255. #ifdef CONFIG_PM_SLEEP
  256. static void rockchip_drm_fb_suspend(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, 1);
  261. console_unlock();
  262. }
  263. static void rockchip_drm_fb_resume(struct drm_device *drm)
  264. {
  265. struct rockchip_drm_private *priv = drm->dev_private;
  266. console_lock();
  267. drm_fb_helper_set_suspend(&priv->fbdev_helper, 0);
  268. console_unlock();
  269. }
  270. static int rockchip_drm_sys_suspend(struct device *dev)
  271. {
  272. struct drm_device *drm = dev_get_drvdata(dev);
  273. struct rockchip_drm_private *priv = drm->dev_private;
  274. drm_kms_helper_poll_disable(drm);
  275. rockchip_drm_fb_suspend(drm);
  276. priv->state = drm_atomic_helper_suspend(drm);
  277. if (IS_ERR(priv->state)) {
  278. rockchip_drm_fb_resume(drm);
  279. drm_kms_helper_poll_enable(drm);
  280. return PTR_ERR(priv->state);
  281. }
  282. return 0;
  283. }
  284. static int rockchip_drm_sys_resume(struct device *dev)
  285. {
  286. struct drm_device *drm = dev_get_drvdata(dev);
  287. struct rockchip_drm_private *priv = drm->dev_private;
  288. drm_atomic_helper_resume(drm, priv->state);
  289. rockchip_drm_fb_resume(drm);
  290. drm_kms_helper_poll_enable(drm);
  291. return 0;
  292. }
  293. #endif
  294. static const struct dev_pm_ops rockchip_drm_pm_ops = {
  295. SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
  296. rockchip_drm_sys_resume)
  297. };
  298. static int compare_of(struct device *dev, void *data)
  299. {
  300. struct device_node *np = data;
  301. return dev->of_node == np;
  302. }
  303. static void rockchip_add_endpoints(struct device *dev,
  304. struct component_match **match,
  305. struct device_node *port)
  306. {
  307. struct device_node *ep, *remote;
  308. for_each_child_of_node(port, ep) {
  309. remote = of_graph_get_remote_port_parent(ep);
  310. if (!remote || !of_device_is_available(remote)) {
  311. of_node_put(remote);
  312. continue;
  313. } else if (!of_device_is_available(remote->parent)) {
  314. dev_warn(dev, "parent device of %s is not available\n",
  315. remote->full_name);
  316. of_node_put(remote);
  317. continue;
  318. }
  319. drm_of_component_match_add(dev, match, compare_of, remote);
  320. of_node_put(remote);
  321. }
  322. }
  323. static const struct component_master_ops rockchip_drm_ops = {
  324. .bind = rockchip_drm_bind,
  325. .unbind = rockchip_drm_unbind,
  326. };
  327. static int rockchip_drm_platform_probe(struct platform_device *pdev)
  328. {
  329. struct device *dev = &pdev->dev;
  330. struct component_match *match = NULL;
  331. struct device_node *np = dev->of_node;
  332. struct device_node *port;
  333. int i;
  334. if (!np)
  335. return -ENODEV;
  336. /*
  337. * Bind the crtc ports first, so that
  338. * drm_of_find_possible_crtcs called from encoder .bind callbacks
  339. * works as expected.
  340. */
  341. for (i = 0;; i++) {
  342. struct device_node *iommu;
  343. port = of_parse_phandle(np, "ports", i);
  344. if (!port)
  345. break;
  346. if (!of_device_is_available(port->parent)) {
  347. of_node_put(port);
  348. continue;
  349. }
  350. iommu = of_parse_phandle(port->parent, "iommus", 0);
  351. if (!iommu || !of_device_is_available(iommu->parent)) {
  352. dev_dbg(dev, "no iommu attached for %s, using non-iommu buffers\n",
  353. port->parent->full_name);
  354. /*
  355. * if there is a crtc not support iommu, force set all
  356. * crtc use non-iommu buffer.
  357. */
  358. is_support_iommu = false;
  359. }
  360. of_node_put(iommu);
  361. drm_of_component_match_add(dev, &match, compare_of,
  362. port->parent);
  363. of_node_put(port);
  364. }
  365. if (i == 0) {
  366. dev_err(dev, "missing 'ports' property\n");
  367. return -ENODEV;
  368. }
  369. if (!match) {
  370. dev_err(dev, "No available vop found for display-subsystem.\n");
  371. return -ENODEV;
  372. }
  373. /*
  374. * For each bound crtc, bind the encoders attached to its
  375. * remote endpoint.
  376. */
  377. for (i = 0;; i++) {
  378. port = of_parse_phandle(np, "ports", i);
  379. if (!port)
  380. break;
  381. if (!of_device_is_available(port->parent)) {
  382. of_node_put(port);
  383. continue;
  384. }
  385. rockchip_add_endpoints(dev, &match, port);
  386. of_node_put(port);
  387. }
  388. return component_master_add_with_match(dev, &rockchip_drm_ops, match);
  389. }
  390. static int rockchip_drm_platform_remove(struct platform_device *pdev)
  391. {
  392. component_master_del(&pdev->dev, &rockchip_drm_ops);
  393. return 0;
  394. }
  395. static const struct of_device_id rockchip_drm_dt_ids[] = {
  396. { .compatible = "rockchip,display-subsystem", },
  397. { /* sentinel */ },
  398. };
  399. MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
  400. static struct platform_driver rockchip_drm_platform_driver = {
  401. .probe = rockchip_drm_platform_probe,
  402. .remove = rockchip_drm_platform_remove,
  403. .driver = {
  404. .name = "rockchip-drm",
  405. .of_match_table = rockchip_drm_dt_ids,
  406. .pm = &rockchip_drm_pm_ops,
  407. },
  408. };
  409. module_platform_driver(rockchip_drm_platform_driver);
  410. MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
  411. MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
  412. MODULE_LICENSE("GPL v2");