rockchip_drm_drv.c 14 KB

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