rockchip_drm_drv.c 12 KB

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