vc4_drv.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (C) 2014-2015 Broadcom
  3. * Copyright (C) 2013 Red Hat
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/component.h>
  11. #include <linux/device.h>
  12. #include <linux/io.h>
  13. #include <linux/module.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/platform_device.h>
  16. #include "drm_fb_cma_helper.h"
  17. #include "uapi/drm/vc4_drm.h"
  18. #include "vc4_drv.h"
  19. #include "vc4_regs.h"
  20. #define DRIVER_NAME "vc4"
  21. #define DRIVER_DESC "Broadcom VC4 graphics"
  22. #define DRIVER_DATE "20140616"
  23. #define DRIVER_MAJOR 0
  24. #define DRIVER_MINOR 0
  25. #define DRIVER_PATCHLEVEL 0
  26. /* Helper function for mapping the regs on a platform device. */
  27. void __iomem *vc4_ioremap_regs(struct platform_device *dev, int index)
  28. {
  29. struct resource *res;
  30. void __iomem *map;
  31. res = platform_get_resource(dev, IORESOURCE_MEM, index);
  32. map = devm_ioremap_resource(&dev->dev, res);
  33. if (IS_ERR(map)) {
  34. DRM_ERROR("Failed to map registers: %ld\n", PTR_ERR(map));
  35. return map;
  36. }
  37. return map;
  38. }
  39. static void vc4_lastclose(struct drm_device *dev)
  40. {
  41. struct vc4_dev *vc4 = to_vc4_dev(dev);
  42. if (vc4->fbdev)
  43. drm_fbdev_cma_restore_mode(vc4->fbdev);
  44. }
  45. static const struct file_operations vc4_drm_fops = {
  46. .owner = THIS_MODULE,
  47. .open = drm_open,
  48. .release = drm_release,
  49. .unlocked_ioctl = drm_ioctl,
  50. .mmap = vc4_mmap,
  51. .poll = drm_poll,
  52. .read = drm_read,
  53. #ifdef CONFIG_COMPAT
  54. .compat_ioctl = drm_compat_ioctl,
  55. #endif
  56. .llseek = noop_llseek,
  57. };
  58. static const struct drm_ioctl_desc vc4_drm_ioctls[] = {
  59. DRM_IOCTL_DEF_DRV(VC4_SUBMIT_CL, vc4_submit_cl_ioctl, 0),
  60. DRM_IOCTL_DEF_DRV(VC4_WAIT_SEQNO, vc4_wait_seqno_ioctl, 0),
  61. DRM_IOCTL_DEF_DRV(VC4_WAIT_BO, vc4_wait_bo_ioctl, 0),
  62. DRM_IOCTL_DEF_DRV(VC4_CREATE_BO, vc4_create_bo_ioctl, 0),
  63. DRM_IOCTL_DEF_DRV(VC4_MMAP_BO, vc4_mmap_bo_ioctl, 0),
  64. DRM_IOCTL_DEF_DRV(VC4_CREATE_SHADER_BO, vc4_create_shader_bo_ioctl, 0),
  65. DRM_IOCTL_DEF_DRV(VC4_GET_HANG_STATE, vc4_get_hang_state_ioctl,
  66. DRM_ROOT_ONLY),
  67. };
  68. static struct drm_driver vc4_drm_driver = {
  69. .driver_features = (DRIVER_MODESET |
  70. DRIVER_ATOMIC |
  71. DRIVER_GEM |
  72. DRIVER_HAVE_IRQ |
  73. DRIVER_PRIME),
  74. .lastclose = vc4_lastclose,
  75. .irq_handler = vc4_irq,
  76. .irq_preinstall = vc4_irq_preinstall,
  77. .irq_postinstall = vc4_irq_postinstall,
  78. .irq_uninstall = vc4_irq_uninstall,
  79. .enable_vblank = vc4_enable_vblank,
  80. .disable_vblank = vc4_disable_vblank,
  81. .get_vblank_counter = drm_vblank_count,
  82. #if defined(CONFIG_DEBUG_FS)
  83. .debugfs_init = vc4_debugfs_init,
  84. .debugfs_cleanup = vc4_debugfs_cleanup,
  85. #endif
  86. .gem_create_object = vc4_create_object,
  87. .gem_free_object = vc4_free_object,
  88. .gem_vm_ops = &drm_gem_cma_vm_ops,
  89. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  90. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  91. .gem_prime_import = drm_gem_prime_import,
  92. .gem_prime_export = vc4_prime_export,
  93. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  94. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  95. .gem_prime_vmap = vc4_prime_vmap,
  96. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  97. .gem_prime_mmap = vc4_prime_mmap,
  98. .dumb_create = vc4_dumb_create,
  99. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  100. .dumb_destroy = drm_gem_dumb_destroy,
  101. .ioctls = vc4_drm_ioctls,
  102. .num_ioctls = ARRAY_SIZE(vc4_drm_ioctls),
  103. .fops = &vc4_drm_fops,
  104. .name = DRIVER_NAME,
  105. .desc = DRIVER_DESC,
  106. .date = DRIVER_DATE,
  107. .major = DRIVER_MAJOR,
  108. .minor = DRIVER_MINOR,
  109. .patchlevel = DRIVER_PATCHLEVEL,
  110. };
  111. static int compare_dev(struct device *dev, void *data)
  112. {
  113. return dev == data;
  114. }
  115. static void vc4_match_add_drivers(struct device *dev,
  116. struct component_match **match,
  117. struct platform_driver *const *drivers,
  118. int count)
  119. {
  120. int i;
  121. for (i = 0; i < count; i++) {
  122. struct device_driver *drv = &drivers[i]->driver;
  123. struct device *p = NULL, *d;
  124. while ((d = bus_find_device(&platform_bus_type, p, drv,
  125. (void *)platform_bus_type.match))) {
  126. put_device(p);
  127. component_match_add(dev, match, compare_dev, d);
  128. p = d;
  129. }
  130. put_device(p);
  131. }
  132. }
  133. static int vc4_drm_bind(struct device *dev)
  134. {
  135. struct platform_device *pdev = to_platform_device(dev);
  136. struct drm_device *drm;
  137. struct drm_connector *connector;
  138. struct vc4_dev *vc4;
  139. int ret = 0;
  140. dev->coherent_dma_mask = DMA_BIT_MASK(32);
  141. vc4 = devm_kzalloc(dev, sizeof(*vc4), GFP_KERNEL);
  142. if (!vc4)
  143. return -ENOMEM;
  144. drm = drm_dev_alloc(&vc4_drm_driver, dev);
  145. if (!drm)
  146. return -ENOMEM;
  147. platform_set_drvdata(pdev, drm);
  148. vc4->dev = drm;
  149. drm->dev_private = vc4;
  150. vc4_bo_cache_init(drm);
  151. drm_mode_config_init(drm);
  152. if (ret)
  153. goto unref;
  154. vc4_gem_init(drm);
  155. ret = component_bind_all(dev, drm);
  156. if (ret)
  157. goto gem_destroy;
  158. ret = drm_dev_register(drm, 0);
  159. if (ret < 0)
  160. goto unbind_all;
  161. /* Connector registration has to occur after DRM device
  162. * registration, because it creates sysfs entries based on the
  163. * DRM device.
  164. */
  165. list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
  166. ret = drm_connector_register(connector);
  167. if (ret)
  168. goto unregister;
  169. }
  170. vc4_kms_load(drm);
  171. return 0;
  172. unregister:
  173. drm_dev_unregister(drm);
  174. unbind_all:
  175. component_unbind_all(dev, drm);
  176. gem_destroy:
  177. vc4_gem_destroy(drm);
  178. unref:
  179. drm_dev_unref(drm);
  180. vc4_bo_cache_destroy(drm);
  181. return ret;
  182. }
  183. static void vc4_drm_unbind(struct device *dev)
  184. {
  185. struct platform_device *pdev = to_platform_device(dev);
  186. struct drm_device *drm = platform_get_drvdata(pdev);
  187. struct vc4_dev *vc4 = to_vc4_dev(drm);
  188. if (vc4->fbdev)
  189. drm_fbdev_cma_fini(vc4->fbdev);
  190. drm_mode_config_cleanup(drm);
  191. drm_put_dev(drm);
  192. }
  193. static const struct component_master_ops vc4_drm_ops = {
  194. .bind = vc4_drm_bind,
  195. .unbind = vc4_drm_unbind,
  196. };
  197. static struct platform_driver *const component_drivers[] = {
  198. &vc4_hdmi_driver,
  199. &vc4_crtc_driver,
  200. &vc4_hvs_driver,
  201. &vc4_v3d_driver,
  202. };
  203. static int vc4_platform_drm_probe(struct platform_device *pdev)
  204. {
  205. struct component_match *match = NULL;
  206. struct device *dev = &pdev->dev;
  207. vc4_match_add_drivers(dev, &match,
  208. component_drivers, ARRAY_SIZE(component_drivers));
  209. return component_master_add_with_match(dev, &vc4_drm_ops, match);
  210. }
  211. static int vc4_platform_drm_remove(struct platform_device *pdev)
  212. {
  213. component_master_del(&pdev->dev, &vc4_drm_ops);
  214. return 0;
  215. }
  216. static const struct of_device_id vc4_of_match[] = {
  217. { .compatible = "brcm,bcm2835-vc4", },
  218. {},
  219. };
  220. MODULE_DEVICE_TABLE(of, vc4_of_match);
  221. static struct platform_driver vc4_platform_driver = {
  222. .probe = vc4_platform_drm_probe,
  223. .remove = vc4_platform_drm_remove,
  224. .driver = {
  225. .name = "vc4-drm",
  226. .of_match_table = vc4_of_match,
  227. },
  228. };
  229. static int __init vc4_drm_register(void)
  230. {
  231. int i, ret;
  232. for (i = 0; i < ARRAY_SIZE(component_drivers); i++) {
  233. ret = platform_driver_register(component_drivers[i]);
  234. if (ret) {
  235. while (--i >= 0)
  236. platform_driver_unregister(component_drivers[i]);
  237. return ret;
  238. }
  239. }
  240. return platform_driver_register(&vc4_platform_driver);
  241. }
  242. static void __exit vc4_drm_unregister(void)
  243. {
  244. int i;
  245. for (i = ARRAY_SIZE(component_drivers) - 1; i >= 0; i--)
  246. platform_driver_unregister(component_drivers[i]);
  247. platform_driver_unregister(&vc4_platform_driver);
  248. }
  249. module_init(vc4_drm_register);
  250. module_exit(vc4_drm_unregister);
  251. MODULE_ALIAS("platform:vc4-drm");
  252. MODULE_DESCRIPTION("Broadcom VC4 DRM Driver");
  253. MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
  254. MODULE_LICENSE("GPL v2");