v3d_drv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* Copyright (C) 2014-2018 Broadcom */
  3. /**
  4. * DOC: Broadcom V3D Graphics Driver
  5. *
  6. * This driver supports the Broadcom V3D 3.3 and 4.1 OpenGL ES GPUs.
  7. * For V3D 2.x support, see the VC4 driver.
  8. *
  9. * The V3D GPU includes a tiled render (composed of a bin and render
  10. * pipelines), the TFU (texture formatting unit), and the CSD (compute
  11. * shader dispatch).
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/device.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/sched/clock.h>
  21. #include <linux/reset.h>
  22. #include <drm/drm_drv.h>
  23. #include <drm/drm_managed.h>
  24. #include <uapi/drm/v3d_drm.h>
  25. #include "v3d_drv.h"
  26. #include "v3d_regs.h"
  27. #define DRIVER_NAME "v3d"
  28. #define DRIVER_DESC "Broadcom V3D graphics"
  29. #define DRIVER_DATE "20180419"
  30. #define DRIVER_MAJOR 1
  31. #define DRIVER_MINOR 0
  32. #define DRIVER_PATCHLEVEL 0
  33. static int v3d_get_param_ioctl(struct drm_device *dev, void *data,
  34. struct drm_file *file_priv)
  35. {
  36. struct v3d_dev *v3d = to_v3d_dev(dev);
  37. struct drm_v3d_get_param *args = data;
  38. static const u32 reg_map[] = {
  39. [DRM_V3D_PARAM_V3D_UIFCFG] = V3D_HUB_UIFCFG,
  40. [DRM_V3D_PARAM_V3D_HUB_IDENT1] = V3D_HUB_IDENT1,
  41. [DRM_V3D_PARAM_V3D_HUB_IDENT2] = V3D_HUB_IDENT2,
  42. [DRM_V3D_PARAM_V3D_HUB_IDENT3] = V3D_HUB_IDENT3,
  43. [DRM_V3D_PARAM_V3D_CORE0_IDENT0] = V3D_CTL_IDENT0,
  44. [DRM_V3D_PARAM_V3D_CORE0_IDENT1] = V3D_CTL_IDENT1,
  45. [DRM_V3D_PARAM_V3D_CORE0_IDENT2] = V3D_CTL_IDENT2,
  46. };
  47. if (args->pad != 0)
  48. return -EINVAL;
  49. /* Note that DRM_V3D_PARAM_V3D_CORE0_IDENT0 is 0, so we need
  50. * to explicitly allow it in the "the register in our
  51. * parameter map" check.
  52. */
  53. if (args->param < ARRAY_SIZE(reg_map) &&
  54. (reg_map[args->param] ||
  55. args->param == DRM_V3D_PARAM_V3D_CORE0_IDENT0)) {
  56. u32 offset = reg_map[args->param];
  57. if (args->value != 0)
  58. return -EINVAL;
  59. if (args->param >= DRM_V3D_PARAM_V3D_CORE0_IDENT0 &&
  60. args->param <= DRM_V3D_PARAM_V3D_CORE0_IDENT2) {
  61. args->value = V3D_CORE_READ(0, offset);
  62. } else {
  63. args->value = V3D_READ(offset);
  64. }
  65. return 0;
  66. }
  67. switch (args->param) {
  68. case DRM_V3D_PARAM_SUPPORTS_TFU:
  69. args->value = 1;
  70. return 0;
  71. case DRM_V3D_PARAM_SUPPORTS_CSD:
  72. args->value = v3d_has_csd(v3d);
  73. return 0;
  74. case DRM_V3D_PARAM_SUPPORTS_CACHE_FLUSH:
  75. args->value = 1;
  76. return 0;
  77. case DRM_V3D_PARAM_SUPPORTS_PERFMON:
  78. args->value = (v3d->ver >= 40);
  79. return 0;
  80. case DRM_V3D_PARAM_SUPPORTS_MULTISYNC_EXT:
  81. args->value = 1;
  82. return 0;
  83. case DRM_V3D_PARAM_SUPPORTS_CPU_QUEUE:
  84. args->value = 1;
  85. return 0;
  86. case DRM_V3D_PARAM_MAX_PERF_COUNTERS:
  87. args->value = v3d->perfmon_info.max_counters;
  88. return 0;
  89. default:
  90. DRM_DEBUG("Unknown parameter %d\n", args->param);
  91. return -EINVAL;
  92. }
  93. }
  94. static int
  95. v3d_open(struct drm_device *dev, struct drm_file *file)
  96. {
  97. struct v3d_dev *v3d = to_v3d_dev(dev);
  98. struct v3d_file_priv *v3d_priv;
  99. struct drm_gpu_scheduler *sched;
  100. int i;
  101. v3d_priv = kzalloc(sizeof(*v3d_priv), GFP_KERNEL);
  102. if (!v3d_priv)
  103. return -ENOMEM;
  104. v3d_priv->v3d = v3d;
  105. for (i = 0; i < V3D_MAX_QUEUES; i++) {
  106. sched = &v3d->queue[i].sched;
  107. drm_sched_entity_init(&v3d_priv->sched_entity[i],
  108. DRM_SCHED_PRIORITY_NORMAL, &sched,
  109. 1, NULL);
  110. memset(&v3d_priv->stats[i], 0, sizeof(v3d_priv->stats[i]));
  111. seqcount_init(&v3d_priv->stats[i].lock);
  112. }
  113. v3d_perfmon_open_file(v3d_priv);
  114. file->driver_priv = v3d_priv;
  115. return 0;
  116. }
  117. static void
  118. v3d_postclose(struct drm_device *dev, struct drm_file *file)
  119. {
  120. struct v3d_file_priv *v3d_priv = file->driver_priv;
  121. enum v3d_queue q;
  122. for (q = 0; q < V3D_MAX_QUEUES; q++)
  123. drm_sched_entity_destroy(&v3d_priv->sched_entity[q]);
  124. v3d_perfmon_close_file(v3d_priv);
  125. kfree(v3d_priv);
  126. }
  127. void v3d_get_stats(const struct v3d_stats *stats, u64 timestamp,
  128. u64 *active_runtime, u64 *jobs_completed)
  129. {
  130. unsigned int seq;
  131. do {
  132. seq = read_seqcount_begin(&stats->lock);
  133. *active_runtime = stats->enabled_ns;
  134. if (stats->start_ns)
  135. *active_runtime += timestamp - stats->start_ns;
  136. *jobs_completed = stats->jobs_completed;
  137. } while (read_seqcount_retry(&stats->lock, seq));
  138. }
  139. static void v3d_show_fdinfo(struct drm_printer *p, struct drm_file *file)
  140. {
  141. struct v3d_file_priv *file_priv = file->driver_priv;
  142. u64 timestamp = local_clock();
  143. enum v3d_queue queue;
  144. for (queue = 0; queue < V3D_MAX_QUEUES; queue++) {
  145. struct v3d_stats *stats = &file_priv->stats[queue];
  146. u64 active_runtime, jobs_completed;
  147. v3d_get_stats(stats, timestamp, &active_runtime, &jobs_completed);
  148. /* Note that, in case of a GPU reset, the time spent during an
  149. * attempt of executing the job is not computed in the runtime.
  150. */
  151. drm_printf(p, "drm-engine-%s: \t%llu ns\n",
  152. v3d_queue_to_string(queue), active_runtime);
  153. /* Note that we only count jobs that completed. Therefore, jobs
  154. * that were resubmitted due to a GPU reset are not computed.
  155. */
  156. drm_printf(p, "v3d-jobs-%s: \t%llu jobs\n",
  157. v3d_queue_to_string(queue), jobs_completed);
  158. }
  159. drm_show_memory_stats(p, file);
  160. }
  161. static const struct file_operations v3d_drm_fops = {
  162. .owner = THIS_MODULE,
  163. DRM_GEM_FOPS,
  164. .show_fdinfo = drm_show_fdinfo,
  165. };
  166. /* DRM_AUTH is required on SUBMIT_CL for now, while we don't have GMP
  167. * protection between clients. Note that render nodes would be
  168. * able to submit CLs that could access BOs from clients authenticated
  169. * with the master node. The TFU doesn't use the GMP, so it would
  170. * need to stay DRM_AUTH until we do buffer size/offset validation.
  171. */
  172. static const struct drm_ioctl_desc v3d_drm_ioctls[] = {
  173. DRM_IOCTL_DEF_DRV(V3D_SUBMIT_CL, v3d_submit_cl_ioctl, DRM_RENDER_ALLOW | DRM_AUTH),
  174. DRM_IOCTL_DEF_DRV(V3D_WAIT_BO, v3d_wait_bo_ioctl, DRM_RENDER_ALLOW),
  175. DRM_IOCTL_DEF_DRV(V3D_CREATE_BO, v3d_create_bo_ioctl, DRM_RENDER_ALLOW),
  176. DRM_IOCTL_DEF_DRV(V3D_MMAP_BO, v3d_mmap_bo_ioctl, DRM_RENDER_ALLOW),
  177. DRM_IOCTL_DEF_DRV(V3D_GET_PARAM, v3d_get_param_ioctl, DRM_RENDER_ALLOW),
  178. DRM_IOCTL_DEF_DRV(V3D_GET_BO_OFFSET, v3d_get_bo_offset_ioctl, DRM_RENDER_ALLOW),
  179. DRM_IOCTL_DEF_DRV(V3D_SUBMIT_TFU, v3d_submit_tfu_ioctl, DRM_RENDER_ALLOW | DRM_AUTH),
  180. DRM_IOCTL_DEF_DRV(V3D_SUBMIT_CSD, v3d_submit_csd_ioctl, DRM_RENDER_ALLOW | DRM_AUTH),
  181. DRM_IOCTL_DEF_DRV(V3D_PERFMON_CREATE, v3d_perfmon_create_ioctl, DRM_RENDER_ALLOW),
  182. DRM_IOCTL_DEF_DRV(V3D_PERFMON_DESTROY, v3d_perfmon_destroy_ioctl, DRM_RENDER_ALLOW),
  183. DRM_IOCTL_DEF_DRV(V3D_PERFMON_GET_VALUES, v3d_perfmon_get_values_ioctl, DRM_RENDER_ALLOW),
  184. DRM_IOCTL_DEF_DRV(V3D_SUBMIT_CPU, v3d_submit_cpu_ioctl, DRM_RENDER_ALLOW | DRM_AUTH),
  185. DRM_IOCTL_DEF_DRV(V3D_PERFMON_GET_COUNTER, v3d_perfmon_get_counter_ioctl, DRM_RENDER_ALLOW),
  186. };
  187. static const struct drm_driver v3d_drm_driver = {
  188. .driver_features = (DRIVER_GEM |
  189. DRIVER_RENDER |
  190. DRIVER_SYNCOBJ),
  191. .open = v3d_open,
  192. .postclose = v3d_postclose,
  193. #if defined(CONFIG_DEBUG_FS)
  194. .debugfs_init = v3d_debugfs_init,
  195. #endif
  196. .gem_create_object = v3d_create_object,
  197. .gem_prime_import_sg_table = v3d_prime_import_sg_table,
  198. .ioctls = v3d_drm_ioctls,
  199. .num_ioctls = ARRAY_SIZE(v3d_drm_ioctls),
  200. .fops = &v3d_drm_fops,
  201. .show_fdinfo = v3d_show_fdinfo,
  202. .name = DRIVER_NAME,
  203. .desc = DRIVER_DESC,
  204. .date = DRIVER_DATE,
  205. .major = DRIVER_MAJOR,
  206. .minor = DRIVER_MINOR,
  207. .patchlevel = DRIVER_PATCHLEVEL,
  208. };
  209. static const struct of_device_id v3d_of_match[] = {
  210. { .compatible = "brcm,2711-v3d" },
  211. { .compatible = "brcm,2712-v3d" },
  212. { .compatible = "brcm,7268-v3d" },
  213. { .compatible = "brcm,7278-v3d" },
  214. {},
  215. };
  216. MODULE_DEVICE_TABLE(of, v3d_of_match);
  217. static int
  218. map_regs(struct v3d_dev *v3d, void __iomem **regs, const char *name)
  219. {
  220. *regs = devm_platform_ioremap_resource_byname(v3d_to_pdev(v3d), name);
  221. return PTR_ERR_OR_ZERO(*regs);
  222. }
  223. static int v3d_platform_drm_probe(struct platform_device *pdev)
  224. {
  225. struct device *dev = &pdev->dev;
  226. struct drm_device *drm;
  227. struct v3d_dev *v3d;
  228. int ret;
  229. u32 mmu_debug;
  230. u32 ident1, ident3;
  231. u64 mask;
  232. v3d = devm_drm_dev_alloc(dev, &v3d_drm_driver, struct v3d_dev, drm);
  233. if (IS_ERR(v3d))
  234. return PTR_ERR(v3d);
  235. drm = &v3d->drm;
  236. platform_set_drvdata(pdev, drm);
  237. ret = map_regs(v3d, &v3d->hub_regs, "hub");
  238. if (ret)
  239. return ret;
  240. ret = map_regs(v3d, &v3d->core_regs[0], "core0");
  241. if (ret)
  242. return ret;
  243. v3d->clk = devm_clk_get_optional(dev, NULL);
  244. if (IS_ERR(v3d->clk))
  245. return dev_err_probe(dev, PTR_ERR(v3d->clk), "Failed to get V3D clock\n");
  246. ret = clk_prepare_enable(v3d->clk);
  247. if (ret) {
  248. dev_err(&pdev->dev, "Couldn't enable the V3D clock\n");
  249. return ret;
  250. }
  251. mmu_debug = V3D_READ(V3D_MMU_DEBUG_INFO);
  252. mask = DMA_BIT_MASK(30 + V3D_GET_FIELD(mmu_debug, V3D_MMU_PA_WIDTH));
  253. ret = dma_set_mask_and_coherent(dev, mask);
  254. if (ret)
  255. goto clk_disable;
  256. v3d->va_width = 30 + V3D_GET_FIELD(mmu_debug, V3D_MMU_VA_WIDTH);
  257. ident1 = V3D_READ(V3D_HUB_IDENT1);
  258. v3d->ver = (V3D_GET_FIELD(ident1, V3D_HUB_IDENT1_TVER) * 10 +
  259. V3D_GET_FIELD(ident1, V3D_HUB_IDENT1_REV));
  260. v3d->cores = V3D_GET_FIELD(ident1, V3D_HUB_IDENT1_NCORES);
  261. WARN_ON(v3d->cores > 1); /* multicore not yet implemented */
  262. ident3 = V3D_READ(V3D_HUB_IDENT3);
  263. v3d->rev = V3D_GET_FIELD(ident3, V3D_HUB_IDENT3_IPREV);
  264. v3d_perfmon_init(v3d);
  265. v3d->reset = devm_reset_control_get_exclusive(dev, NULL);
  266. if (IS_ERR(v3d->reset)) {
  267. ret = PTR_ERR(v3d->reset);
  268. if (ret == -EPROBE_DEFER)
  269. goto clk_disable;
  270. v3d->reset = NULL;
  271. ret = map_regs(v3d, &v3d->bridge_regs, "bridge");
  272. if (ret) {
  273. dev_err(dev,
  274. "Failed to get reset control or bridge regs\n");
  275. goto clk_disable;
  276. }
  277. }
  278. if (v3d->ver < 41) {
  279. ret = map_regs(v3d, &v3d->gca_regs, "gca");
  280. if (ret)
  281. goto clk_disable;
  282. }
  283. v3d->mmu_scratch = dma_alloc_wc(dev, 4096, &v3d->mmu_scratch_paddr,
  284. GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO);
  285. if (!v3d->mmu_scratch) {
  286. dev_err(dev, "Failed to allocate MMU scratch page\n");
  287. ret = -ENOMEM;
  288. goto clk_disable;
  289. }
  290. ret = v3d_gem_init(drm);
  291. if (ret)
  292. goto dma_free;
  293. ret = v3d_irq_init(v3d);
  294. if (ret)
  295. goto gem_destroy;
  296. ret = drm_dev_register(drm, 0);
  297. if (ret)
  298. goto irq_disable;
  299. ret = v3d_sysfs_init(dev);
  300. if (ret)
  301. goto drm_unregister;
  302. return 0;
  303. drm_unregister:
  304. drm_dev_unregister(drm);
  305. irq_disable:
  306. v3d_irq_disable(v3d);
  307. gem_destroy:
  308. v3d_gem_destroy(drm);
  309. dma_free:
  310. dma_free_wc(dev, 4096, v3d->mmu_scratch, v3d->mmu_scratch_paddr);
  311. clk_disable:
  312. clk_disable_unprepare(v3d->clk);
  313. return ret;
  314. }
  315. static void v3d_platform_drm_remove(struct platform_device *pdev)
  316. {
  317. struct drm_device *drm = platform_get_drvdata(pdev);
  318. struct v3d_dev *v3d = to_v3d_dev(drm);
  319. struct device *dev = &pdev->dev;
  320. v3d_sysfs_destroy(dev);
  321. drm_dev_unregister(drm);
  322. v3d_gem_destroy(drm);
  323. dma_free_wc(v3d->drm.dev, 4096, v3d->mmu_scratch,
  324. v3d->mmu_scratch_paddr);
  325. clk_disable_unprepare(v3d->clk);
  326. }
  327. static struct platform_driver v3d_platform_driver = {
  328. .probe = v3d_platform_drm_probe,
  329. .remove_new = v3d_platform_drm_remove,
  330. .driver = {
  331. .name = "v3d",
  332. .of_match_table = v3d_of_match,
  333. },
  334. };
  335. module_platform_driver(v3d_platform_driver);
  336. MODULE_ALIAS("platform:v3d-drm");
  337. MODULE_DESCRIPTION("Broadcom V3D DRM Driver");
  338. MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
  339. MODULE_LICENSE("GPL v2");