vc4_v3d.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * Copyright (c) 2014 The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2013 Red Hat
  4. * Author: Rob Clark <robdclark@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/component.h>
  20. #include <linux/pm_runtime.h>
  21. #include "vc4_drv.h"
  22. #include "vc4_regs.h"
  23. #ifdef CONFIG_DEBUG_FS
  24. #define REGDEF(reg) { reg, #reg }
  25. static const struct {
  26. uint32_t reg;
  27. const char *name;
  28. } vc4_reg_defs[] = {
  29. REGDEF(V3D_IDENT0),
  30. REGDEF(V3D_IDENT1),
  31. REGDEF(V3D_IDENT2),
  32. REGDEF(V3D_SCRATCH),
  33. REGDEF(V3D_L2CACTL),
  34. REGDEF(V3D_SLCACTL),
  35. REGDEF(V3D_INTCTL),
  36. REGDEF(V3D_INTENA),
  37. REGDEF(V3D_INTDIS),
  38. REGDEF(V3D_CT0CS),
  39. REGDEF(V3D_CT1CS),
  40. REGDEF(V3D_CT0EA),
  41. REGDEF(V3D_CT1EA),
  42. REGDEF(V3D_CT0CA),
  43. REGDEF(V3D_CT1CA),
  44. REGDEF(V3D_CT00RA0),
  45. REGDEF(V3D_CT01RA0),
  46. REGDEF(V3D_CT0LC),
  47. REGDEF(V3D_CT1LC),
  48. REGDEF(V3D_CT0PC),
  49. REGDEF(V3D_CT1PC),
  50. REGDEF(V3D_PCS),
  51. REGDEF(V3D_BFC),
  52. REGDEF(V3D_RFC),
  53. REGDEF(V3D_BPCA),
  54. REGDEF(V3D_BPCS),
  55. REGDEF(V3D_BPOA),
  56. REGDEF(V3D_BPOS),
  57. REGDEF(V3D_BXCF),
  58. REGDEF(V3D_SQRSV0),
  59. REGDEF(V3D_SQRSV1),
  60. REGDEF(V3D_SQCNTL),
  61. REGDEF(V3D_SRQPC),
  62. REGDEF(V3D_SRQUA),
  63. REGDEF(V3D_SRQUL),
  64. REGDEF(V3D_SRQCS),
  65. REGDEF(V3D_VPACNTL),
  66. REGDEF(V3D_VPMBASE),
  67. REGDEF(V3D_PCTRC),
  68. REGDEF(V3D_PCTRE),
  69. REGDEF(V3D_PCTR(0)),
  70. REGDEF(V3D_PCTRS(0)),
  71. REGDEF(V3D_PCTR(1)),
  72. REGDEF(V3D_PCTRS(1)),
  73. REGDEF(V3D_PCTR(2)),
  74. REGDEF(V3D_PCTRS(2)),
  75. REGDEF(V3D_PCTR(3)),
  76. REGDEF(V3D_PCTRS(3)),
  77. REGDEF(V3D_PCTR(4)),
  78. REGDEF(V3D_PCTRS(4)),
  79. REGDEF(V3D_PCTR(5)),
  80. REGDEF(V3D_PCTRS(5)),
  81. REGDEF(V3D_PCTR(6)),
  82. REGDEF(V3D_PCTRS(6)),
  83. REGDEF(V3D_PCTR(7)),
  84. REGDEF(V3D_PCTRS(7)),
  85. REGDEF(V3D_PCTR(8)),
  86. REGDEF(V3D_PCTRS(8)),
  87. REGDEF(V3D_PCTR(9)),
  88. REGDEF(V3D_PCTRS(9)),
  89. REGDEF(V3D_PCTR(10)),
  90. REGDEF(V3D_PCTRS(10)),
  91. REGDEF(V3D_PCTR(11)),
  92. REGDEF(V3D_PCTRS(11)),
  93. REGDEF(V3D_PCTR(12)),
  94. REGDEF(V3D_PCTRS(12)),
  95. REGDEF(V3D_PCTR(13)),
  96. REGDEF(V3D_PCTRS(13)),
  97. REGDEF(V3D_PCTR(14)),
  98. REGDEF(V3D_PCTRS(14)),
  99. REGDEF(V3D_PCTR(15)),
  100. REGDEF(V3D_PCTRS(15)),
  101. REGDEF(V3D_DBGE),
  102. REGDEF(V3D_FDBGO),
  103. REGDEF(V3D_FDBGB),
  104. REGDEF(V3D_FDBGR),
  105. REGDEF(V3D_FDBGS),
  106. REGDEF(V3D_ERRSTAT),
  107. };
  108. int vc4_v3d_debugfs_regs(struct seq_file *m, void *unused)
  109. {
  110. struct drm_info_node *node = (struct drm_info_node *)m->private;
  111. struct drm_device *dev = node->minor->dev;
  112. struct vc4_dev *vc4 = to_vc4_dev(dev);
  113. int i;
  114. for (i = 0; i < ARRAY_SIZE(vc4_reg_defs); i++) {
  115. seq_printf(m, "%s (0x%04x): 0x%08x\n",
  116. vc4_reg_defs[i].name, vc4_reg_defs[i].reg,
  117. V3D_READ(vc4_reg_defs[i].reg));
  118. }
  119. return 0;
  120. }
  121. int vc4_v3d_debugfs_ident(struct seq_file *m, void *unused)
  122. {
  123. struct drm_info_node *node = (struct drm_info_node *)m->private;
  124. struct drm_device *dev = node->minor->dev;
  125. struct vc4_dev *vc4 = to_vc4_dev(dev);
  126. uint32_t ident1 = V3D_READ(V3D_IDENT1);
  127. uint32_t nslc = VC4_GET_FIELD(ident1, V3D_IDENT1_NSLC);
  128. uint32_t tups = VC4_GET_FIELD(ident1, V3D_IDENT1_TUPS);
  129. uint32_t qups = VC4_GET_FIELD(ident1, V3D_IDENT1_QUPS);
  130. seq_printf(m, "Revision: %d\n",
  131. VC4_GET_FIELD(ident1, V3D_IDENT1_REV));
  132. seq_printf(m, "Slices: %d\n", nslc);
  133. seq_printf(m, "TMUs: %d\n", nslc * tups);
  134. seq_printf(m, "QPUs: %d\n", nslc * qups);
  135. seq_printf(m, "Semaphores: %d\n",
  136. VC4_GET_FIELD(ident1, V3D_IDENT1_NSEM));
  137. return 0;
  138. }
  139. #endif /* CONFIG_DEBUG_FS */
  140. static void vc4_v3d_init_hw(struct drm_device *dev)
  141. {
  142. struct vc4_dev *vc4 = to_vc4_dev(dev);
  143. /* Take all the memory that would have been reserved for user
  144. * QPU programs, since we don't have an interface for running
  145. * them, anyway.
  146. */
  147. V3D_WRITE(V3D_VPMBASE, 0);
  148. }
  149. int vc4_v3d_get_bin_slot(struct vc4_dev *vc4)
  150. {
  151. struct drm_device *dev = vc4->dev;
  152. unsigned long irqflags;
  153. int slot;
  154. uint64_t seqno = 0;
  155. struct vc4_exec_info *exec;
  156. try_again:
  157. spin_lock_irqsave(&vc4->job_lock, irqflags);
  158. slot = ffs(~vc4->bin_alloc_used);
  159. if (slot != 0) {
  160. /* Switch from ffs() bit index to a 0-based index. */
  161. slot--;
  162. vc4->bin_alloc_used |= BIT(slot);
  163. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  164. return slot;
  165. }
  166. /* Couldn't find an open slot. Wait for render to complete
  167. * and try again.
  168. */
  169. exec = vc4_last_render_job(vc4);
  170. if (exec)
  171. seqno = exec->seqno;
  172. spin_unlock_irqrestore(&vc4->job_lock, irqflags);
  173. if (seqno) {
  174. int ret = vc4_wait_for_seqno(dev, seqno, ~0ull, true);
  175. if (ret == 0)
  176. goto try_again;
  177. return ret;
  178. }
  179. return -ENOMEM;
  180. }
  181. /**
  182. * vc4_allocate_bin_bo() - allocates the memory that will be used for
  183. * tile binning.
  184. *
  185. * The binner has a limitation that the addresses in the tile state
  186. * buffer that point into the tile alloc buffer or binner overflow
  187. * memory only have 28 bits (256MB), and the top 4 on the bus for
  188. * tile alloc references end up coming from the tile state buffer's
  189. * address.
  190. *
  191. * To work around this, we allocate a single large buffer while V3D is
  192. * in use, make sure that it has the top 4 bits constant across its
  193. * entire extent, and then put the tile state, tile alloc, and binner
  194. * overflow memory inside that buffer.
  195. *
  196. * This creates a limitation where we may not be able to execute a job
  197. * if it doesn't fit within the buffer that we allocated up front.
  198. * However, it turns out that 16MB is "enough for anybody", and
  199. * real-world applications run into allocation failures from the
  200. * overall CMA pool before they make scenes complicated enough to run
  201. * out of bin space.
  202. */
  203. static int vc4_allocate_bin_bo(struct drm_device *drm)
  204. {
  205. struct vc4_dev *vc4 = to_vc4_dev(drm);
  206. struct vc4_v3d *v3d = vc4->v3d;
  207. uint32_t size = 16 * 1024 * 1024;
  208. int ret = 0;
  209. struct list_head list;
  210. /* We may need to try allocating more than once to get a BO
  211. * that doesn't cross 256MB. Track the ones we've allocated
  212. * that failed so far, so that we can free them when we've got
  213. * one that succeeded (if we freed them right away, our next
  214. * allocation would probably be the same chunk of memory).
  215. */
  216. INIT_LIST_HEAD(&list);
  217. while (true) {
  218. struct vc4_bo *bo = vc4_bo_create(drm, size, true,
  219. VC4_BO_TYPE_BIN);
  220. if (IS_ERR(bo)) {
  221. ret = PTR_ERR(bo);
  222. dev_err(&v3d->pdev->dev,
  223. "Failed to allocate memory for tile binning: "
  224. "%d. You may need to enable CMA or give it "
  225. "more memory.",
  226. ret);
  227. break;
  228. }
  229. /* Check if this BO won't trigger the addressing bug. */
  230. if ((bo->base.paddr & 0xf0000000) ==
  231. ((bo->base.paddr + bo->base.base.size - 1) & 0xf0000000)) {
  232. vc4->bin_bo = bo;
  233. /* Set up for allocating 512KB chunks of
  234. * binner memory. The biggest allocation we
  235. * need to do is for the initial tile alloc +
  236. * tile state buffer. We can render to a
  237. * maximum of ((2048*2048) / (32*32) = 4096
  238. * tiles in a frame (until we do floating
  239. * point rendering, at which point it would be
  240. * 8192). Tile state is 48b/tile (rounded to
  241. * a page), and tile alloc is 32b/tile
  242. * (rounded to a page), plus a page of extra,
  243. * for a total of 320kb for our worst-case.
  244. * We choose 512kb so that it divides evenly
  245. * into our 16MB, and the rest of the 512kb
  246. * will be used as storage for the overflow
  247. * from the initial 32b CL per bin.
  248. */
  249. vc4->bin_alloc_size = 512 * 1024;
  250. vc4->bin_alloc_used = 0;
  251. vc4->bin_alloc_overflow = 0;
  252. WARN_ON_ONCE(sizeof(vc4->bin_alloc_used) * 8 !=
  253. bo->base.base.size / vc4->bin_alloc_size);
  254. break;
  255. }
  256. /* Put it on the list to free later, and try again. */
  257. list_add(&bo->unref_head, &list);
  258. }
  259. /* Free all the BOs we allocated but didn't choose. */
  260. while (!list_empty(&list)) {
  261. struct vc4_bo *bo = list_last_entry(&list,
  262. struct vc4_bo, unref_head);
  263. list_del(&bo->unref_head);
  264. drm_gem_object_put_unlocked(&bo->base.base);
  265. }
  266. return ret;
  267. }
  268. #ifdef CONFIG_PM
  269. static int vc4_v3d_runtime_suspend(struct device *dev)
  270. {
  271. struct vc4_v3d *v3d = dev_get_drvdata(dev);
  272. struct vc4_dev *vc4 = v3d->vc4;
  273. vc4_irq_uninstall(vc4->dev);
  274. drm_gem_object_put_unlocked(&vc4->bin_bo->base.base);
  275. vc4->bin_bo = NULL;
  276. clk_disable_unprepare(v3d->clk);
  277. return 0;
  278. }
  279. static int vc4_v3d_runtime_resume(struct device *dev)
  280. {
  281. struct vc4_v3d *v3d = dev_get_drvdata(dev);
  282. struct vc4_dev *vc4 = v3d->vc4;
  283. int ret;
  284. ret = vc4_allocate_bin_bo(vc4->dev);
  285. if (ret)
  286. return ret;
  287. ret = clk_prepare_enable(v3d->clk);
  288. if (ret != 0)
  289. return ret;
  290. vc4_v3d_init_hw(vc4->dev);
  291. /* We disabled the IRQ as part of vc4_irq_uninstall in suspend. */
  292. enable_irq(vc4->dev->irq);
  293. vc4_irq_postinstall(vc4->dev);
  294. return 0;
  295. }
  296. #endif
  297. static int vc4_v3d_bind(struct device *dev, struct device *master, void *data)
  298. {
  299. struct platform_device *pdev = to_platform_device(dev);
  300. struct drm_device *drm = dev_get_drvdata(master);
  301. struct vc4_dev *vc4 = to_vc4_dev(drm);
  302. struct vc4_v3d *v3d = NULL;
  303. int ret;
  304. v3d = devm_kzalloc(&pdev->dev, sizeof(*v3d), GFP_KERNEL);
  305. if (!v3d)
  306. return -ENOMEM;
  307. dev_set_drvdata(dev, v3d);
  308. v3d->pdev = pdev;
  309. v3d->regs = vc4_ioremap_regs(pdev, 0);
  310. if (IS_ERR(v3d->regs))
  311. return PTR_ERR(v3d->regs);
  312. vc4->v3d = v3d;
  313. v3d->vc4 = vc4;
  314. v3d->clk = devm_clk_get(dev, NULL);
  315. if (IS_ERR(v3d->clk)) {
  316. int ret = PTR_ERR(v3d->clk);
  317. if (ret == -ENOENT) {
  318. /* bcm2835 didn't have a clock reference in the DT. */
  319. ret = 0;
  320. v3d->clk = NULL;
  321. } else {
  322. if (ret != -EPROBE_DEFER)
  323. dev_err(dev, "Failed to get V3D clock: %d\n",
  324. ret);
  325. return ret;
  326. }
  327. }
  328. if (V3D_READ(V3D_IDENT0) != V3D_EXPECTED_IDENT0) {
  329. DRM_ERROR("V3D_IDENT0 read 0x%08x instead of 0x%08x\n",
  330. V3D_READ(V3D_IDENT0), V3D_EXPECTED_IDENT0);
  331. return -EINVAL;
  332. }
  333. ret = clk_prepare_enable(v3d->clk);
  334. if (ret != 0)
  335. return ret;
  336. ret = vc4_allocate_bin_bo(drm);
  337. if (ret) {
  338. clk_disable_unprepare(v3d->clk);
  339. return ret;
  340. }
  341. /* Reset the binner overflow address/size at setup, to be sure
  342. * we don't reuse an old one.
  343. */
  344. V3D_WRITE(V3D_BPOA, 0);
  345. V3D_WRITE(V3D_BPOS, 0);
  346. vc4_v3d_init_hw(drm);
  347. ret = drm_irq_install(drm, platform_get_irq(pdev, 0));
  348. if (ret) {
  349. DRM_ERROR("Failed to install IRQ handler\n");
  350. return ret;
  351. }
  352. pm_runtime_set_active(dev);
  353. pm_runtime_use_autosuspend(dev);
  354. pm_runtime_set_autosuspend_delay(dev, 40); /* a little over 2 frames. */
  355. pm_runtime_enable(dev);
  356. return 0;
  357. }
  358. static void vc4_v3d_unbind(struct device *dev, struct device *master,
  359. void *data)
  360. {
  361. struct drm_device *drm = dev_get_drvdata(master);
  362. struct vc4_dev *vc4 = to_vc4_dev(drm);
  363. pm_runtime_disable(dev);
  364. drm_irq_uninstall(drm);
  365. /* Disable the binner's overflow memory address, so the next
  366. * driver probe (if any) doesn't try to reuse our old
  367. * allocation.
  368. */
  369. V3D_WRITE(V3D_BPOA, 0);
  370. V3D_WRITE(V3D_BPOS, 0);
  371. vc4->v3d = NULL;
  372. }
  373. static const struct dev_pm_ops vc4_v3d_pm_ops = {
  374. SET_RUNTIME_PM_OPS(vc4_v3d_runtime_suspend, vc4_v3d_runtime_resume, NULL)
  375. };
  376. static const struct component_ops vc4_v3d_ops = {
  377. .bind = vc4_v3d_bind,
  378. .unbind = vc4_v3d_unbind,
  379. };
  380. static int vc4_v3d_dev_probe(struct platform_device *pdev)
  381. {
  382. return component_add(&pdev->dev, &vc4_v3d_ops);
  383. }
  384. static int vc4_v3d_dev_remove(struct platform_device *pdev)
  385. {
  386. component_del(&pdev->dev, &vc4_v3d_ops);
  387. return 0;
  388. }
  389. static const struct of_device_id vc4_v3d_dt_match[] = {
  390. { .compatible = "brcm,bcm2835-v3d" },
  391. { .compatible = "brcm,cygnus-v3d" },
  392. { .compatible = "brcm,vc4-v3d" },
  393. {}
  394. };
  395. struct platform_driver vc4_v3d_driver = {
  396. .probe = vc4_v3d_dev_probe,
  397. .remove = vc4_v3d_dev_remove,
  398. .driver = {
  399. .name = "vc4_v3d",
  400. .of_match_table = vc4_v3d_dt_match,
  401. .pm = &vc4_v3d_pm_ops,
  402. },
  403. };