exynos_drm_drv.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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;
  148. if (pm_runtime_suspended(dev) || !drm_dev)
  149. return 0;
  150. private = drm_dev->dev_private;
  151. drm_kms_helper_poll_disable(drm_dev);
  152. exynos_drm_fbdev_suspend(drm_dev);
  153. private->suspend_state = drm_atomic_helper_suspend(drm_dev);
  154. if (IS_ERR(private->suspend_state)) {
  155. exynos_drm_fbdev_resume(drm_dev);
  156. drm_kms_helper_poll_enable(drm_dev);
  157. return PTR_ERR(private->suspend_state);
  158. }
  159. return 0;
  160. }
  161. static int exynos_drm_resume(struct device *dev)
  162. {
  163. struct drm_device *drm_dev = dev_get_drvdata(dev);
  164. struct exynos_drm_private *private;
  165. if (pm_runtime_suspended(dev) || !drm_dev)
  166. return 0;
  167. private = drm_dev->dev_private;
  168. drm_atomic_helper_resume(drm_dev, private->suspend_state);
  169. exynos_drm_fbdev_resume(drm_dev);
  170. drm_kms_helper_poll_enable(drm_dev);
  171. return 0;
  172. }
  173. #endif
  174. static const struct dev_pm_ops exynos_drm_pm_ops = {
  175. SET_SYSTEM_SLEEP_PM_OPS(exynos_drm_suspend, exynos_drm_resume)
  176. };
  177. /* forward declaration */
  178. static struct platform_driver exynos_drm_platform_driver;
  179. struct exynos_drm_driver_info {
  180. struct platform_driver *driver;
  181. unsigned int flags;
  182. };
  183. #define DRM_COMPONENT_DRIVER BIT(0) /* supports component framework */
  184. #define DRM_VIRTUAL_DEVICE BIT(1) /* create virtual platform device */
  185. #define DRM_DMA_DEVICE BIT(2) /* can be used for dma allocations */
  186. #define DRV_PTR(drv, cond) (IS_ENABLED(cond) ? &drv : NULL)
  187. /*
  188. * Connector drivers should not be placed before associated crtc drivers,
  189. * because connector requires pipe number of its crtc during initialization.
  190. */
  191. static struct exynos_drm_driver_info exynos_drm_drivers[] = {
  192. {
  193. DRV_PTR(fimd_driver, CONFIG_DRM_EXYNOS_FIMD),
  194. DRM_COMPONENT_DRIVER | DRM_DMA_DEVICE
  195. }, {
  196. DRV_PTR(exynos5433_decon_driver, CONFIG_DRM_EXYNOS5433_DECON),
  197. DRM_COMPONENT_DRIVER | DRM_DMA_DEVICE
  198. }, {
  199. DRV_PTR(decon_driver, CONFIG_DRM_EXYNOS7_DECON),
  200. DRM_COMPONENT_DRIVER | DRM_DMA_DEVICE
  201. }, {
  202. DRV_PTR(mixer_driver, CONFIG_DRM_EXYNOS_MIXER),
  203. DRM_COMPONENT_DRIVER | DRM_DMA_DEVICE
  204. }, {
  205. DRV_PTR(mic_driver, CONFIG_DRM_EXYNOS_MIC),
  206. DRM_COMPONENT_DRIVER
  207. }, {
  208. DRV_PTR(dp_driver, CONFIG_DRM_EXYNOS_DP),
  209. DRM_COMPONENT_DRIVER
  210. }, {
  211. DRV_PTR(dsi_driver, CONFIG_DRM_EXYNOS_DSI),
  212. DRM_COMPONENT_DRIVER
  213. }, {
  214. DRV_PTR(hdmi_driver, CONFIG_DRM_EXYNOS_HDMI),
  215. DRM_COMPONENT_DRIVER
  216. }, {
  217. DRV_PTR(vidi_driver, CONFIG_DRM_EXYNOS_VIDI),
  218. DRM_COMPONENT_DRIVER | DRM_VIRTUAL_DEVICE
  219. }, {
  220. DRV_PTR(g2d_driver, CONFIG_DRM_EXYNOS_G2D),
  221. }, {
  222. DRV_PTR(fimc_driver, CONFIG_DRM_EXYNOS_FIMC),
  223. }, {
  224. DRV_PTR(rotator_driver, CONFIG_DRM_EXYNOS_ROTATOR),
  225. }, {
  226. DRV_PTR(gsc_driver, CONFIG_DRM_EXYNOS_GSC),
  227. }, {
  228. DRV_PTR(ipp_driver, CONFIG_DRM_EXYNOS_IPP),
  229. DRM_VIRTUAL_DEVICE
  230. }, {
  231. &exynos_drm_platform_driver,
  232. DRM_VIRTUAL_DEVICE
  233. }
  234. };
  235. static int compare_dev(struct device *dev, void *data)
  236. {
  237. return dev == (struct device *)data;
  238. }
  239. static struct component_match *exynos_drm_match_add(struct device *dev)
  240. {
  241. struct component_match *match = NULL;
  242. int i;
  243. for (i = 0; i < ARRAY_SIZE(exynos_drm_drivers); ++i) {
  244. struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
  245. struct device *p = NULL, *d;
  246. if (!info->driver || !(info->flags & DRM_COMPONENT_DRIVER))
  247. continue;
  248. while ((d = bus_find_device(&platform_bus_type, p,
  249. &info->driver->driver,
  250. (void *)platform_bus_type.match))) {
  251. put_device(p);
  252. component_match_add(dev, &match, compare_dev, d);
  253. p = d;
  254. }
  255. put_device(p);
  256. }
  257. return match ?: ERR_PTR(-ENODEV);
  258. }
  259. static int exynos_drm_bind(struct device *dev)
  260. {
  261. struct exynos_drm_private *private;
  262. struct drm_encoder *encoder;
  263. struct drm_device *drm;
  264. unsigned int clone_mask;
  265. int cnt, ret;
  266. drm = drm_dev_alloc(&exynos_drm_driver, dev);
  267. if (IS_ERR(drm))
  268. return PTR_ERR(drm);
  269. private = kzalloc(sizeof(struct exynos_drm_private), GFP_KERNEL);
  270. if (!private) {
  271. ret = -ENOMEM;
  272. goto err_free_drm;
  273. }
  274. init_waitqueue_head(&private->wait);
  275. spin_lock_init(&private->lock);
  276. dev_set_drvdata(dev, drm);
  277. drm->dev_private = (void *)private;
  278. /* the first real CRTC device is used for all dma mapping operations */
  279. private->dma_dev = exynos_drm_get_dma_device();
  280. if (!private->dma_dev) {
  281. DRM_ERROR("no device found for DMA mapping operations.\n");
  282. ret = -ENODEV;
  283. goto err_free_private;
  284. }
  285. DRM_INFO("Exynos DRM: using %s device for DMA mapping operations\n",
  286. dev_name(private->dma_dev));
  287. /* create common IOMMU mapping for all devices attached to Exynos DRM */
  288. ret = drm_create_iommu_mapping(drm);
  289. if (ret < 0) {
  290. DRM_ERROR("failed to create iommu mapping.\n");
  291. goto err_free_private;
  292. }
  293. drm_mode_config_init(drm);
  294. exynos_drm_mode_config_init(drm);
  295. /* setup possible_clones. */
  296. cnt = 0;
  297. clone_mask = 0;
  298. list_for_each_entry(encoder, &drm->mode_config.encoder_list, head)
  299. clone_mask |= (1 << (cnt++));
  300. list_for_each_entry(encoder, &drm->mode_config.encoder_list, head)
  301. encoder->possible_clones = clone_mask;
  302. /* Try to bind all sub drivers. */
  303. ret = component_bind_all(drm->dev, drm);
  304. if (ret)
  305. goto err_mode_config_cleanup;
  306. ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
  307. if (ret)
  308. goto err_unbind_all;
  309. /* Probe non kms sub drivers and virtual display driver. */
  310. ret = exynos_drm_device_subdrv_probe(drm);
  311. if (ret)
  312. goto err_unbind_all;
  313. drm_mode_config_reset(drm);
  314. /*
  315. * enable drm irq mode.
  316. * - with irq_enabled = true, we can use the vblank feature.
  317. *
  318. * P.S. note that we wouldn't use drm irq handler but
  319. * just specific driver own one instead because
  320. * drm framework supports only one irq handler.
  321. */
  322. drm->irq_enabled = true;
  323. /* init kms poll for handling hpd */
  324. drm_kms_helper_poll_init(drm);
  325. ret = exynos_drm_fbdev_init(drm);
  326. if (ret)
  327. goto err_cleanup_poll;
  328. /* register the DRM device */
  329. ret = drm_dev_register(drm, 0);
  330. if (ret < 0)
  331. goto err_cleanup_fbdev;
  332. return 0;
  333. err_cleanup_fbdev:
  334. exynos_drm_fbdev_fini(drm);
  335. err_cleanup_poll:
  336. drm_kms_helper_poll_fini(drm);
  337. exynos_drm_device_subdrv_remove(drm);
  338. err_unbind_all:
  339. component_unbind_all(drm->dev, drm);
  340. err_mode_config_cleanup:
  341. drm_mode_config_cleanup(drm);
  342. drm_release_iommu_mapping(drm);
  343. err_free_private:
  344. kfree(private);
  345. err_free_drm:
  346. drm_dev_unref(drm);
  347. return ret;
  348. }
  349. static void exynos_drm_unbind(struct device *dev)
  350. {
  351. struct drm_device *drm = dev_get_drvdata(dev);
  352. drm_dev_unregister(drm);
  353. exynos_drm_device_subdrv_remove(drm);
  354. exynos_drm_fbdev_fini(drm);
  355. drm_kms_helper_poll_fini(drm);
  356. component_unbind_all(drm->dev, drm);
  357. drm_mode_config_cleanup(drm);
  358. drm_release_iommu_mapping(drm);
  359. kfree(drm->dev_private);
  360. drm->dev_private = NULL;
  361. dev_set_drvdata(dev, NULL);
  362. drm_dev_unref(drm);
  363. }
  364. static const struct component_master_ops exynos_drm_ops = {
  365. .bind = exynos_drm_bind,
  366. .unbind = exynos_drm_unbind,
  367. };
  368. static int exynos_drm_platform_probe(struct platform_device *pdev)
  369. {
  370. struct component_match *match;
  371. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  372. match = exynos_drm_match_add(&pdev->dev);
  373. if (IS_ERR(match))
  374. return PTR_ERR(match);
  375. return component_master_add_with_match(&pdev->dev, &exynos_drm_ops,
  376. match);
  377. }
  378. static int exynos_drm_platform_remove(struct platform_device *pdev)
  379. {
  380. component_master_del(&pdev->dev, &exynos_drm_ops);
  381. return 0;
  382. }
  383. static struct platform_driver exynos_drm_platform_driver = {
  384. .probe = exynos_drm_platform_probe,
  385. .remove = exynos_drm_platform_remove,
  386. .driver = {
  387. .name = "exynos-drm",
  388. .pm = &exynos_drm_pm_ops,
  389. },
  390. };
  391. static struct device *exynos_drm_get_dma_device(void)
  392. {
  393. int i;
  394. for (i = 0; i < ARRAY_SIZE(exynos_drm_drivers); ++i) {
  395. struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
  396. struct device *dev;
  397. if (!info->driver || !(info->flags & DRM_DMA_DEVICE))
  398. continue;
  399. while ((dev = bus_find_device(&platform_bus_type, NULL,
  400. &info->driver->driver,
  401. (void *)platform_bus_type.match))) {
  402. put_device(dev);
  403. return dev;
  404. }
  405. }
  406. return NULL;
  407. }
  408. static void exynos_drm_unregister_devices(void)
  409. {
  410. int i;
  411. for (i = ARRAY_SIZE(exynos_drm_drivers) - 1; i >= 0; --i) {
  412. struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
  413. struct device *dev;
  414. if (!info->driver || !(info->flags & DRM_VIRTUAL_DEVICE))
  415. continue;
  416. while ((dev = bus_find_device(&platform_bus_type, NULL,
  417. &info->driver->driver,
  418. (void *)platform_bus_type.match))) {
  419. put_device(dev);
  420. platform_device_unregister(to_platform_device(dev));
  421. }
  422. }
  423. }
  424. static int exynos_drm_register_devices(void)
  425. {
  426. struct platform_device *pdev;
  427. int i;
  428. for (i = 0; i < ARRAY_SIZE(exynos_drm_drivers); ++i) {
  429. struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
  430. if (!info->driver || !(info->flags & DRM_VIRTUAL_DEVICE))
  431. continue;
  432. pdev = platform_device_register_simple(
  433. info->driver->driver.name, -1, NULL, 0);
  434. if (IS_ERR(pdev))
  435. goto fail;
  436. }
  437. return 0;
  438. fail:
  439. exynos_drm_unregister_devices();
  440. return PTR_ERR(pdev);
  441. }
  442. static void exynos_drm_unregister_drivers(void)
  443. {
  444. int i;
  445. for (i = ARRAY_SIZE(exynos_drm_drivers) - 1; i >= 0; --i) {
  446. struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
  447. if (!info->driver)
  448. continue;
  449. platform_driver_unregister(info->driver);
  450. }
  451. }
  452. static int exynos_drm_register_drivers(void)
  453. {
  454. int i, ret;
  455. for (i = 0; i < ARRAY_SIZE(exynos_drm_drivers); ++i) {
  456. struct exynos_drm_driver_info *info = &exynos_drm_drivers[i];
  457. if (!info->driver)
  458. continue;
  459. ret = platform_driver_register(info->driver);
  460. if (ret)
  461. goto fail;
  462. }
  463. return 0;
  464. fail:
  465. exynos_drm_unregister_drivers();
  466. return ret;
  467. }
  468. static int exynos_drm_init(void)
  469. {
  470. int ret;
  471. ret = exynos_drm_register_devices();
  472. if (ret)
  473. return ret;
  474. ret = exynos_drm_register_drivers();
  475. if (ret)
  476. goto err_unregister_pdevs;
  477. return 0;
  478. err_unregister_pdevs:
  479. exynos_drm_unregister_devices();
  480. return ret;
  481. }
  482. static void exynos_drm_exit(void)
  483. {
  484. exynos_drm_unregister_drivers();
  485. exynos_drm_unregister_devices();
  486. }
  487. module_init(exynos_drm_init);
  488. module_exit(exynos_drm_exit);
  489. MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
  490. MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
  491. MODULE_AUTHOR("Seung-Woo Kim <sw0312.kim@samsung.com>");
  492. MODULE_DESCRIPTION("Samsung SoC DRM Driver");
  493. MODULE_LICENSE("GPL");