exynos_drm_drv.c 14 KB

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