rockchip_drm_drv.c 14 KB

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