exynos_drm_drv.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*
  2. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  3. * Authors:
  4. * Inki Dae <inki.dae@samsung.com>
  5. * Joonyoung Shim <jy0922.shim@samsung.com>
  6. * Seung-Woo Kim <sw0312.kim@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/pm_runtime.h>
  14. #include <drm/drmP.h>
  15. #include <drm/drm_atomic.h>
  16. #include <drm/drm_atomic_helper.h>
  17. #include <drm/drm_crtc_helper.h>
  18. #include <linux/component.h>
  19. #include <drm/exynos_drm.h>
  20. #include "exynos_drm_drv.h"
  21. #include "exynos_drm_fbdev.h"
  22. #include "exynos_drm_fb.h"
  23. #include "exynos_drm_gem.h"
  24. #include "exynos_drm_plane.h"
  25. #include "exynos_drm_vidi.h"
  26. #include "exynos_drm_g2d.h"
  27. #include "exynos_drm_ipp.h"
  28. #include "exynos_drm_iommu.h"
  29. #define DRIVER_NAME "exynos"
  30. #define DRIVER_DESC "Samsung SoC DRM"
  31. #define DRIVER_DATE "20110530"
  32. #define DRIVER_MAJOR 1
  33. #define DRIVER_MINOR 0
  34. static struct device *exynos_drm_get_dma_device(void);
  35. int exynos_atomic_check(struct drm_device *dev,
  36. struct drm_atomic_state *state)
  37. {
  38. int ret;
  39. ret = drm_atomic_helper_check_modeset(dev, state);
  40. if (ret)
  41. return ret;
  42. ret = drm_atomic_normalize_zpos(dev, state);
  43. if (ret)
  44. return ret;
  45. ret = drm_atomic_helper_check_planes(dev, state);
  46. if (ret)
  47. return ret;
  48. return ret;
  49. }
  50. static int exynos_drm_open(struct drm_device *dev, struct drm_file *file)
  51. {
  52. struct drm_exynos_file_private *file_priv;
  53. int ret;
  54. file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
  55. if (!file_priv)
  56. return -ENOMEM;
  57. file->driver_priv = file_priv;
  58. ret = exynos_drm_subdrv_open(dev, file);
  59. if (ret)
  60. goto err_file_priv_free;
  61. return ret;
  62. err_file_priv_free:
  63. kfree(file_priv);
  64. file->driver_priv = NULL;
  65. return ret;
  66. }
  67. static void exynos_drm_postclose(struct drm_device *dev, struct drm_file *file)
  68. {
  69. exynos_drm_subdrv_close(dev, file);
  70. kfree(file->driver_priv);
  71. file->driver_priv = NULL;
  72. }
  73. static void exynos_drm_lastclose(struct drm_device *dev)
  74. {
  75. exynos_drm_fbdev_restore_mode(dev);
  76. }
  77. static const struct vm_operations_struct exynos_drm_gem_vm_ops = {
  78. .fault = exynos_drm_gem_fault,
  79. .open = drm_gem_vm_open,
  80. .close = drm_gem_vm_close,
  81. };
  82. static const struct drm_ioctl_desc exynos_ioctls[] = {
  83. DRM_IOCTL_DEF_DRV(EXYNOS_GEM_CREATE, exynos_drm_gem_create_ioctl,
  84. DRM_AUTH | DRM_RENDER_ALLOW),
  85. DRM_IOCTL_DEF_DRV(EXYNOS_GEM_MAP, exynos_drm_gem_map_ioctl,
  86. DRM_AUTH | DRM_RENDER_ALLOW),
  87. DRM_IOCTL_DEF_DRV(EXYNOS_GEM_GET, exynos_drm_gem_get_ioctl,
  88. DRM_RENDER_ALLOW),
  89. DRM_IOCTL_DEF_DRV(EXYNOS_VIDI_CONNECTION, vidi_connection_ioctl,
  90. DRM_AUTH),
  91. DRM_IOCTL_DEF_DRV(EXYNOS_G2D_GET_VER, exynos_g2d_get_ver_ioctl,
  92. DRM_AUTH | DRM_RENDER_ALLOW),
  93. DRM_IOCTL_DEF_DRV(EXYNOS_G2D_SET_CMDLIST, exynos_g2d_set_cmdlist_ioctl,
  94. DRM_AUTH | DRM_RENDER_ALLOW),
  95. DRM_IOCTL_DEF_DRV(EXYNOS_G2D_EXEC, exynos_g2d_exec_ioctl,
  96. DRM_AUTH | DRM_RENDER_ALLOW),
  97. DRM_IOCTL_DEF_DRV(EXYNOS_IPP_GET_PROPERTY, exynos_drm_ipp_get_property,
  98. DRM_AUTH | DRM_RENDER_ALLOW),
  99. DRM_IOCTL_DEF_DRV(EXYNOS_IPP_SET_PROPERTY, exynos_drm_ipp_set_property,
  100. DRM_AUTH | DRM_RENDER_ALLOW),
  101. DRM_IOCTL_DEF_DRV(EXYNOS_IPP_QUEUE_BUF, exynos_drm_ipp_queue_buf,
  102. DRM_AUTH | DRM_RENDER_ALLOW),
  103. DRM_IOCTL_DEF_DRV(EXYNOS_IPP_CMD_CTRL, exynos_drm_ipp_cmd_ctrl,
  104. DRM_AUTH | DRM_RENDER_ALLOW),
  105. };
  106. static const struct file_operations exynos_drm_driver_fops = {
  107. .owner = THIS_MODULE,
  108. .open = drm_open,
  109. .mmap = exynos_drm_gem_mmap,
  110. .poll = drm_poll,
  111. .read = drm_read,
  112. .unlocked_ioctl = drm_ioctl,
  113. .compat_ioctl = drm_compat_ioctl,
  114. .release = drm_release,
  115. };
  116. static struct drm_driver exynos_drm_driver = {
  117. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME
  118. | DRIVER_ATOMIC | DRIVER_RENDER,
  119. .open = exynos_drm_open,
  120. .lastclose = exynos_drm_lastclose,
  121. .postclose = exynos_drm_postclose,
  122. .gem_free_object_unlocked = exynos_drm_gem_free_object,
  123. .gem_vm_ops = &exynos_drm_gem_vm_ops,
  124. .dumb_create = exynos_drm_gem_dumb_create,
  125. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  126. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  127. .gem_prime_export = drm_gem_prime_export,
  128. .gem_prime_import = drm_gem_prime_import,
  129. .gem_prime_get_sg_table = exynos_drm_gem_prime_get_sg_table,
  130. .gem_prime_import_sg_table = exynos_drm_gem_prime_import_sg_table,
  131. .gem_prime_vmap = exynos_drm_gem_prime_vmap,
  132. .gem_prime_vunmap = exynos_drm_gem_prime_vunmap,
  133. .gem_prime_mmap = exynos_drm_gem_prime_mmap,
  134. .ioctls = exynos_ioctls,
  135. .num_ioctls = ARRAY_SIZE(exynos_ioctls),
  136. .fops = &exynos_drm_driver_fops,
  137. .name = DRIVER_NAME,
  138. .desc = DRIVER_DESC,
  139. .date = DRIVER_DATE,
  140. .major = DRIVER_MAJOR,
  141. .minor = DRIVER_MINOR,
  142. };
  143. #ifdef CONFIG_PM_SLEEP
  144. static int exynos_drm_suspend(struct device *dev)
  145. {
  146. struct drm_device *drm_dev = dev_get_drvdata(dev);
  147. struct exynos_drm_private *private = drm_dev->dev_private;
  148. if (pm_runtime_suspended(dev) || !drm_dev)
  149. return 0;
  150. drm_kms_helper_poll_disable(drm_dev);
  151. exynos_drm_fbdev_suspend(drm_dev);
  152. private->suspend_state = drm_atomic_helper_suspend(drm_dev);
  153. if (IS_ERR(private->suspend_state)) {
  154. exynos_drm_fbdev_resume(drm_dev);
  155. drm_kms_helper_poll_enable(drm_dev);
  156. return PTR_ERR(private->suspend_state);
  157. }
  158. return 0;
  159. }
  160. static int exynos_drm_resume(struct device *dev)
  161. {
  162. struct drm_device *drm_dev = dev_get_drvdata(dev);
  163. struct exynos_drm_private *private = drm_dev->dev_private;
  164. if (pm_runtime_suspended(dev) || !drm_dev)
  165. return 0;
  166. drm_atomic_helper_resume(drm_dev, private->suspend_state);
  167. exynos_drm_fbdev_resume(drm_dev);
  168. drm_kms_helper_poll_enable(drm_dev);
  169. return 0;
  170. }
  171. #endif
  172. static const struct dev_pm_ops exynos_drm_pm_ops = {
  173. SET_SYSTEM_SLEEP_PM_OPS(exynos_drm_suspend, exynos_drm_resume)
  174. };
  175. /* forward declaration */
  176. static struct platform_driver exynos_drm_platform_driver;
  177. struct exynos_drm_driver_info {
  178. struct platform_driver *driver;
  179. unsigned int flags;
  180. };
  181. #define DRM_COMPONENT_DRIVER BIT(0) /* supports component framework */
  182. #define DRM_VIRTUAL_DEVICE BIT(1) /* create virtual platform device */
  183. #define DRM_DMA_DEVICE BIT(2) /* can be used for dma allocations */
  184. #define DRV_PTR(drv, cond) (IS_ENABLED(cond) ? &drv : NULL)
  185. /*
  186. * Connector drivers should not be placed before associated crtc drivers,
  187. * because connector requires pipe number of its crtc during initialization.
  188. */
  189. static struct exynos_drm_driver_info exynos_drm_drivers[] = {
  190. {
  191. DRV_PTR(fimd_driver, CONFIG_DRM_EXYNOS_FIMD),
  192. DRM_COMPONENT_DRIVER | DRM_DMA_DEVICE
  193. }, {
  194. DRV_PTR(exynos5433_decon_driver, CONFIG_DRM_EXYNOS5433_DECON),
  195. DRM_COMPONENT_DRIVER | DRM_DMA_DEVICE
  196. }, {
  197. DRV_PTR(decon_driver, CONFIG_DRM_EXYNOS7_DECON),
  198. DRM_COMPONENT_DRIVER | DRM_DMA_DEVICE
  199. }, {
  200. DRV_PTR(mixer_driver, CONFIG_DRM_EXYNOS_MIXER),
  201. DRM_COMPONENT_DRIVER | DRM_DMA_DEVICE
  202. }, {
  203. DRV_PTR(mic_driver, CONFIG_DRM_EXYNOS_MIC),
  204. DRM_COMPONENT_DRIVER
  205. }, {
  206. DRV_PTR(dp_driver, CONFIG_DRM_EXYNOS_DP),
  207. DRM_COMPONENT_DRIVER
  208. }, {
  209. DRV_PTR(dsi_driver, CONFIG_DRM_EXYNOS_DSI),
  210. DRM_COMPONENT_DRIVER
  211. }, {
  212. DRV_PTR(hdmi_driver, CONFIG_DRM_EXYNOS_HDMI),
  213. DRM_COMPONENT_DRIVER
  214. }, {
  215. DRV_PTR(vidi_driver, CONFIG_DRM_EXYNOS_VIDI),
  216. DRM_COMPONENT_DRIVER | DRM_VIRTUAL_DEVICE
  217. }, {
  218. DRV_PTR(g2d_driver, CONFIG_DRM_EXYNOS_G2D),
  219. }, {
  220. DRV_PTR(fimc_driver, CONFIG_DRM_EXYNOS_FIMC),
  221. }, {
  222. DRV_PTR(rotator_driver, CONFIG_DRM_EXYNOS_ROTATOR),
  223. }, {
  224. DRV_PTR(gsc_driver, CONFIG_DRM_EXYNOS_GSC),
  225. }, {
  226. DRV_PTR(ipp_driver, CONFIG_DRM_EXYNOS_IPP),
  227. DRM_VIRTUAL_DEVICE
  228. }, {
  229. &exynos_drm_platform_driver,
  230. DRM_VIRTUAL_DEVICE
  231. }
  232. };
  233. static int compare_dev(struct device *dev, void *data)
  234. {
  235. return dev == (struct device *)data;
  236. }
  237. static struct component_match *exynos_drm_match_add(struct device *dev)
  238. {
  239. struct component_match *match = NULL;
  240. int i;
  241. for (i = 0; i < ARRAY_SIZE(exynos_drm_drivers); ++i) {
  242. struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
  243. struct device *p = NULL, *d;
  244. if (!info->driver || !(info->flags & DRM_COMPONENT_DRIVER))
  245. continue;
  246. while ((d = bus_find_device(&platform_bus_type, p,
  247. &info->driver->driver,
  248. (void *)platform_bus_type.match))) {
  249. put_device(p);
  250. component_match_add(dev, &match, compare_dev, d);
  251. p = d;
  252. }
  253. put_device(p);
  254. }
  255. return match ?: ERR_PTR(-ENODEV);
  256. }
  257. static int exynos_drm_bind(struct device *dev)
  258. {
  259. struct exynos_drm_private *private;
  260. struct drm_encoder *encoder;
  261. struct drm_device *drm;
  262. unsigned int clone_mask;
  263. int cnt, ret;
  264. drm = drm_dev_alloc(&exynos_drm_driver, dev);
  265. if (IS_ERR(drm))
  266. return PTR_ERR(drm);
  267. private = kzalloc(sizeof(struct exynos_drm_private), GFP_KERNEL);
  268. if (!private) {
  269. ret = -ENOMEM;
  270. goto err_free_drm;
  271. }
  272. init_waitqueue_head(&private->wait);
  273. spin_lock_init(&private->lock);
  274. dev_set_drvdata(dev, drm);
  275. drm->dev_private = (void *)private;
  276. /* the first real CRTC device is used for all dma mapping operations */
  277. private->dma_dev = exynos_drm_get_dma_device();
  278. if (!private->dma_dev) {
  279. DRM_ERROR("no device found for DMA mapping operations.\n");
  280. ret = -ENODEV;
  281. goto err_free_private;
  282. }
  283. DRM_INFO("Exynos DRM: using %s device for DMA mapping operations\n",
  284. dev_name(private->dma_dev));
  285. /* create common IOMMU mapping for all devices attached to Exynos DRM */
  286. ret = drm_create_iommu_mapping(drm);
  287. if (ret < 0) {
  288. DRM_ERROR("failed to create iommu mapping.\n");
  289. goto err_free_private;
  290. }
  291. drm_mode_config_init(drm);
  292. exynos_drm_mode_config_init(drm);
  293. /* setup possible_clones. */
  294. cnt = 0;
  295. clone_mask = 0;
  296. list_for_each_entry(encoder, &drm->mode_config.encoder_list, head)
  297. clone_mask |= (1 << (cnt++));
  298. list_for_each_entry(encoder, &drm->mode_config.encoder_list, head)
  299. encoder->possible_clones = clone_mask;
  300. /* Try to bind all sub drivers. */
  301. ret = component_bind_all(drm->dev, drm);
  302. if (ret)
  303. goto err_mode_config_cleanup;
  304. ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
  305. if (ret)
  306. goto err_unbind_all;
  307. /* Probe non kms sub drivers and virtual display driver. */
  308. ret = exynos_drm_device_subdrv_probe(drm);
  309. if (ret)
  310. goto err_unbind_all;
  311. drm_mode_config_reset(drm);
  312. /*
  313. * enable drm irq mode.
  314. * - with irq_enabled = true, we can use the vblank feature.
  315. *
  316. * P.S. note that we wouldn't use drm irq handler but
  317. * just specific driver own one instead because
  318. * drm framework supports only one irq handler.
  319. */
  320. drm->irq_enabled = true;
  321. /* init kms poll for handling hpd */
  322. drm_kms_helper_poll_init(drm);
  323. ret = exynos_drm_fbdev_init(drm);
  324. if (ret)
  325. goto err_cleanup_poll;
  326. /* register the DRM device */
  327. ret = drm_dev_register(drm, 0);
  328. if (ret < 0)
  329. goto err_cleanup_fbdev;
  330. return 0;
  331. err_cleanup_fbdev:
  332. exynos_drm_fbdev_fini(drm);
  333. err_cleanup_poll:
  334. drm_kms_helper_poll_fini(drm);
  335. exynos_drm_device_subdrv_remove(drm);
  336. err_unbind_all:
  337. component_unbind_all(drm->dev, drm);
  338. err_mode_config_cleanup:
  339. drm_mode_config_cleanup(drm);
  340. drm_release_iommu_mapping(drm);
  341. err_free_private:
  342. kfree(private);
  343. err_free_drm:
  344. drm_dev_unref(drm);
  345. return ret;
  346. }
  347. static void exynos_drm_unbind(struct device *dev)
  348. {
  349. struct drm_device *drm = dev_get_drvdata(dev);
  350. drm_dev_unregister(drm);
  351. exynos_drm_device_subdrv_remove(drm);
  352. exynos_drm_fbdev_fini(drm);
  353. drm_kms_helper_poll_fini(drm);
  354. component_unbind_all(drm->dev, drm);
  355. drm_mode_config_cleanup(drm);
  356. drm_release_iommu_mapping(drm);
  357. kfree(drm->dev_private);
  358. drm->dev_private = NULL;
  359. drm_dev_unref(drm);
  360. }
  361. static const struct component_master_ops exynos_drm_ops = {
  362. .bind = exynos_drm_bind,
  363. .unbind = exynos_drm_unbind,
  364. };
  365. static int exynos_drm_platform_probe(struct platform_device *pdev)
  366. {
  367. struct component_match *match;
  368. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  369. match = exynos_drm_match_add(&pdev->dev);
  370. if (IS_ERR(match))
  371. return PTR_ERR(match);
  372. return component_master_add_with_match(&pdev->dev, &exynos_drm_ops,
  373. match);
  374. }
  375. static int exynos_drm_platform_remove(struct platform_device *pdev)
  376. {
  377. component_master_del(&pdev->dev, &exynos_drm_ops);
  378. return 0;
  379. }
  380. static struct platform_driver exynos_drm_platform_driver = {
  381. .probe = exynos_drm_platform_probe,
  382. .remove = exynos_drm_platform_remove,
  383. .driver = {
  384. .name = "exynos-drm",
  385. .pm = &exynos_drm_pm_ops,
  386. },
  387. };
  388. static struct device *exynos_drm_get_dma_device(void)
  389. {
  390. int i;
  391. for (i = 0; i < ARRAY_SIZE(exynos_drm_drivers); ++i) {
  392. struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
  393. struct device *dev;
  394. if (!info->driver || !(info->flags & DRM_DMA_DEVICE))
  395. continue;
  396. while ((dev = bus_find_device(&platform_bus_type, NULL,
  397. &info->driver->driver,
  398. (void *)platform_bus_type.match))) {
  399. put_device(dev);
  400. return dev;
  401. }
  402. }
  403. return NULL;
  404. }
  405. static void exynos_drm_unregister_devices(void)
  406. {
  407. int i;
  408. for (i = ARRAY_SIZE(exynos_drm_drivers) - 1; i >= 0; --i) {
  409. struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
  410. struct device *dev;
  411. if (!info->driver || !(info->flags & DRM_VIRTUAL_DEVICE))
  412. continue;
  413. while ((dev = bus_find_device(&platform_bus_type, NULL,
  414. &info->driver->driver,
  415. (void *)platform_bus_type.match))) {
  416. put_device(dev);
  417. platform_device_unregister(to_platform_device(dev));
  418. }
  419. }
  420. }
  421. static int exynos_drm_register_devices(void)
  422. {
  423. struct platform_device *pdev;
  424. int i;
  425. for (i = 0; i < ARRAY_SIZE(exynos_drm_drivers); ++i) {
  426. struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
  427. if (!info->driver || !(info->flags & DRM_VIRTUAL_DEVICE))
  428. continue;
  429. pdev = platform_device_register_simple(
  430. info->driver->driver.name, -1, NULL, 0);
  431. if (IS_ERR(pdev))
  432. goto fail;
  433. }
  434. return 0;
  435. fail:
  436. exynos_drm_unregister_devices();
  437. return PTR_ERR(pdev);
  438. }
  439. static void exynos_drm_unregister_drivers(void)
  440. {
  441. int i;
  442. for (i = ARRAY_SIZE(exynos_drm_drivers) - 1; i >= 0; --i) {
  443. struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
  444. if (!info->driver)
  445. continue;
  446. platform_driver_unregister(info->driver);
  447. }
  448. }
  449. static int exynos_drm_register_drivers(void)
  450. {
  451. int i, ret;
  452. for (i = 0; i < ARRAY_SIZE(exynos_drm_drivers); ++i) {
  453. struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
  454. if (!info->driver)
  455. continue;
  456. ret = platform_driver_register(info->driver);
  457. if (ret)
  458. goto fail;
  459. }
  460. return 0;
  461. fail:
  462. exynos_drm_unregister_drivers();
  463. return ret;
  464. }
  465. static int exynos_drm_init(void)
  466. {
  467. int ret;
  468. ret = exynos_drm_register_devices();
  469. if (ret)
  470. return ret;
  471. ret = exynos_drm_register_drivers();
  472. if (ret)
  473. goto err_unregister_pdevs;
  474. return 0;
  475. err_unregister_pdevs:
  476. exynos_drm_unregister_devices();
  477. return ret;
  478. }
  479. static void exynos_drm_exit(void)
  480. {
  481. exynos_drm_unregister_drivers();
  482. exynos_drm_unregister_devices();
  483. }
  484. module_init(exynos_drm_init);
  485. module_exit(exynos_drm_exit);
  486. MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
  487. MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
  488. MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
  489. MODULE_DESCRIPTION("Samsung SoC DRM Driver");
  490. MODULE_LICENSE("GPL");