rockchip_drm_drv.c 14 KB

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