exynos_drm_drv.c 15 KB

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