panthor_gpu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. // SPDX-License-Identifier: GPL-2.0 or MIT
  2. /* Copyright 2018 Marty E. Plummer <hanetzer@startmail.com> */
  3. /* Copyright 2019 Linaro, Ltd., Rob Herring <robh@kernel.org> */
  4. /* Copyright 2019 Collabora ltd. */
  5. #include <linux/bitfield.h>
  6. #include <linux/bitmap.h>
  7. #include <linux/delay.h>
  8. #include <linux/dma-mapping.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/io.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/pm_runtime.h>
  14. #include <drm/drm_drv.h>
  15. #include <drm/drm_managed.h>
  16. #include "panthor_device.h"
  17. #include "panthor_gpu.h"
  18. #include "panthor_regs.h"
  19. /**
  20. * struct panthor_gpu - GPU block management data.
  21. */
  22. struct panthor_gpu {
  23. /** @irq: GPU irq. */
  24. struct panthor_irq irq;
  25. /** @reqs_lock: Lock protecting access to pending_reqs. */
  26. spinlock_t reqs_lock;
  27. /** @pending_reqs: Pending GPU requests. */
  28. u32 pending_reqs;
  29. /** @reqs_acked: GPU request wait queue. */
  30. wait_queue_head_t reqs_acked;
  31. };
  32. /**
  33. * struct panthor_model - GPU model description
  34. */
  35. struct panthor_model {
  36. /** @name: Model name. */
  37. const char *name;
  38. /** @arch_major: Major version number of architecture. */
  39. u8 arch_major;
  40. /** @product_major: Major version number of product. */
  41. u8 product_major;
  42. };
  43. /**
  44. * GPU_MODEL() - Define a GPU model. A GPU product can be uniquely identified
  45. * by a combination of the major architecture version and the major product
  46. * version.
  47. * @_name: Name for the GPU model.
  48. * @_arch_major: Architecture major.
  49. * @_product_major: Product major.
  50. */
  51. #define GPU_MODEL(_name, _arch_major, _product_major) \
  52. {\
  53. .name = __stringify(_name), \
  54. .arch_major = _arch_major, \
  55. .product_major = _product_major, \
  56. }
  57. static const struct panthor_model gpu_models[] = {
  58. GPU_MODEL(g610, 10, 7),
  59. {},
  60. };
  61. #define GPU_INTERRUPTS_MASK \
  62. (GPU_IRQ_FAULT | \
  63. GPU_IRQ_PROTM_FAULT | \
  64. GPU_IRQ_RESET_COMPLETED | \
  65. GPU_IRQ_CLEAN_CACHES_COMPLETED)
  66. static void panthor_gpu_init_info(struct panthor_device *ptdev)
  67. {
  68. const struct panthor_model *model;
  69. u32 arch_major, product_major;
  70. u32 major, minor, status;
  71. unsigned int i;
  72. ptdev->gpu_info.gpu_id = gpu_read(ptdev, GPU_ID);
  73. ptdev->gpu_info.csf_id = gpu_read(ptdev, GPU_CSF_ID);
  74. ptdev->gpu_info.gpu_rev = gpu_read(ptdev, GPU_REVID);
  75. ptdev->gpu_info.core_features = gpu_read(ptdev, GPU_CORE_FEATURES);
  76. ptdev->gpu_info.l2_features = gpu_read(ptdev, GPU_L2_FEATURES);
  77. ptdev->gpu_info.tiler_features = gpu_read(ptdev, GPU_TILER_FEATURES);
  78. ptdev->gpu_info.mem_features = gpu_read(ptdev, GPU_MEM_FEATURES);
  79. ptdev->gpu_info.mmu_features = gpu_read(ptdev, GPU_MMU_FEATURES);
  80. ptdev->gpu_info.thread_features = gpu_read(ptdev, GPU_THREAD_FEATURES);
  81. ptdev->gpu_info.max_threads = gpu_read(ptdev, GPU_THREAD_MAX_THREADS);
  82. ptdev->gpu_info.thread_max_workgroup_size = gpu_read(ptdev, GPU_THREAD_MAX_WORKGROUP_SIZE);
  83. ptdev->gpu_info.thread_max_barrier_size = gpu_read(ptdev, GPU_THREAD_MAX_BARRIER_SIZE);
  84. ptdev->gpu_info.coherency_features = gpu_read(ptdev, GPU_COHERENCY_FEATURES);
  85. for (i = 0; i < 4; i++)
  86. ptdev->gpu_info.texture_features[i] = gpu_read(ptdev, GPU_TEXTURE_FEATURES(i));
  87. ptdev->gpu_info.as_present = gpu_read(ptdev, GPU_AS_PRESENT);
  88. ptdev->gpu_info.shader_present = gpu_read(ptdev, GPU_SHADER_PRESENT_LO);
  89. ptdev->gpu_info.shader_present |= (u64)gpu_read(ptdev, GPU_SHADER_PRESENT_HI) << 32;
  90. ptdev->gpu_info.tiler_present = gpu_read(ptdev, GPU_TILER_PRESENT_LO);
  91. ptdev->gpu_info.tiler_present |= (u64)gpu_read(ptdev, GPU_TILER_PRESENT_HI) << 32;
  92. ptdev->gpu_info.l2_present = gpu_read(ptdev, GPU_L2_PRESENT_LO);
  93. ptdev->gpu_info.l2_present |= (u64)gpu_read(ptdev, GPU_L2_PRESENT_HI) << 32;
  94. arch_major = GPU_ARCH_MAJOR(ptdev->gpu_info.gpu_id);
  95. product_major = GPU_PROD_MAJOR(ptdev->gpu_info.gpu_id);
  96. major = GPU_VER_MAJOR(ptdev->gpu_info.gpu_id);
  97. minor = GPU_VER_MINOR(ptdev->gpu_info.gpu_id);
  98. status = GPU_VER_STATUS(ptdev->gpu_info.gpu_id);
  99. for (model = gpu_models; model->name; model++) {
  100. if (model->arch_major == arch_major &&
  101. model->product_major == product_major)
  102. break;
  103. }
  104. drm_info(&ptdev->base,
  105. "mali-%s id 0x%x major 0x%x minor 0x%x status 0x%x",
  106. model->name ?: "unknown", ptdev->gpu_info.gpu_id >> 16,
  107. major, minor, status);
  108. drm_info(&ptdev->base,
  109. "Features: L2:%#x Tiler:%#x Mem:%#x MMU:%#x AS:%#x",
  110. ptdev->gpu_info.l2_features,
  111. ptdev->gpu_info.tiler_features,
  112. ptdev->gpu_info.mem_features,
  113. ptdev->gpu_info.mmu_features,
  114. ptdev->gpu_info.as_present);
  115. drm_info(&ptdev->base,
  116. "shader_present=0x%0llx l2_present=0x%0llx tiler_present=0x%0llx",
  117. ptdev->gpu_info.shader_present, ptdev->gpu_info.l2_present,
  118. ptdev->gpu_info.tiler_present);
  119. }
  120. static void panthor_gpu_irq_handler(struct panthor_device *ptdev, u32 status)
  121. {
  122. if (status & GPU_IRQ_FAULT) {
  123. u32 fault_status = gpu_read(ptdev, GPU_FAULT_STATUS);
  124. u64 address = ((u64)gpu_read(ptdev, GPU_FAULT_ADDR_HI) << 32) |
  125. gpu_read(ptdev, GPU_FAULT_ADDR_LO);
  126. drm_warn(&ptdev->base, "GPU Fault 0x%08x (%s) at 0x%016llx\n",
  127. fault_status, panthor_exception_name(ptdev, fault_status & 0xFF),
  128. address);
  129. }
  130. if (status & GPU_IRQ_PROTM_FAULT)
  131. drm_warn(&ptdev->base, "GPU Fault in protected mode\n");
  132. spin_lock(&ptdev->gpu->reqs_lock);
  133. if (status & ptdev->gpu->pending_reqs) {
  134. ptdev->gpu->pending_reqs &= ~status;
  135. wake_up_all(&ptdev->gpu->reqs_acked);
  136. }
  137. spin_unlock(&ptdev->gpu->reqs_lock);
  138. }
  139. PANTHOR_IRQ_HANDLER(gpu, GPU, panthor_gpu_irq_handler);
  140. /**
  141. * panthor_gpu_unplug() - Called when the GPU is unplugged.
  142. * @ptdev: Device to unplug.
  143. */
  144. void panthor_gpu_unplug(struct panthor_device *ptdev)
  145. {
  146. unsigned long flags;
  147. /* Make sure the IRQ handler is not running after that point. */
  148. panthor_gpu_irq_suspend(&ptdev->gpu->irq);
  149. /* Wake-up all waiters. */
  150. spin_lock_irqsave(&ptdev->gpu->reqs_lock, flags);
  151. ptdev->gpu->pending_reqs = 0;
  152. wake_up_all(&ptdev->gpu->reqs_acked);
  153. spin_unlock_irqrestore(&ptdev->gpu->reqs_lock, flags);
  154. }
  155. /**
  156. * panthor_gpu_init() - Initialize the GPU block
  157. * @ptdev: Device.
  158. *
  159. * Return: 0 on success, a negative error code otherwise.
  160. */
  161. int panthor_gpu_init(struct panthor_device *ptdev)
  162. {
  163. struct panthor_gpu *gpu;
  164. u32 pa_bits;
  165. int ret, irq;
  166. gpu = drmm_kzalloc(&ptdev->base, sizeof(*gpu), GFP_KERNEL);
  167. if (!gpu)
  168. return -ENOMEM;
  169. spin_lock_init(&gpu->reqs_lock);
  170. init_waitqueue_head(&gpu->reqs_acked);
  171. ptdev->gpu = gpu;
  172. panthor_gpu_init_info(ptdev);
  173. dma_set_max_seg_size(ptdev->base.dev, UINT_MAX);
  174. pa_bits = GPU_MMU_FEATURES_PA_BITS(ptdev->gpu_info.mmu_features);
  175. ret = dma_set_mask_and_coherent(ptdev->base.dev, DMA_BIT_MASK(pa_bits));
  176. if (ret)
  177. return ret;
  178. irq = platform_get_irq_byname(to_platform_device(ptdev->base.dev), "gpu");
  179. if (irq < 0)
  180. return irq;
  181. ret = panthor_request_gpu_irq(ptdev, &ptdev->gpu->irq, irq, GPU_INTERRUPTS_MASK);
  182. if (ret)
  183. return ret;
  184. return 0;
  185. }
  186. /**
  187. * panthor_gpu_block_power_off() - Power-off a specific block of the GPU
  188. * @ptdev: Device.
  189. * @blk_name: Block name.
  190. * @pwroff_reg: Power-off register for this block.
  191. * @pwrtrans_reg: Power transition register for this block.
  192. * @mask: Sub-elements to power-off.
  193. * @timeout_us: Timeout in microseconds.
  194. *
  195. * Return: 0 on success, a negative error code otherwise.
  196. */
  197. int panthor_gpu_block_power_off(struct panthor_device *ptdev,
  198. const char *blk_name,
  199. u32 pwroff_reg, u32 pwrtrans_reg,
  200. u64 mask, u32 timeout_us)
  201. {
  202. u32 val, i;
  203. int ret;
  204. for (i = 0; i < 2; i++) {
  205. u32 mask32 = mask >> (i * 32);
  206. if (!mask32)
  207. continue;
  208. ret = readl_relaxed_poll_timeout(ptdev->iomem + pwrtrans_reg + (i * 4),
  209. val, !(mask32 & val),
  210. 100, timeout_us);
  211. if (ret) {
  212. drm_err(&ptdev->base, "timeout waiting on %s:%llx power transition",
  213. blk_name, mask);
  214. return ret;
  215. }
  216. }
  217. if (mask & GENMASK(31, 0))
  218. gpu_write(ptdev, pwroff_reg, mask);
  219. if (mask >> 32)
  220. gpu_write(ptdev, pwroff_reg + 4, mask >> 32);
  221. for (i = 0; i < 2; i++) {
  222. u32 mask32 = mask >> (i * 32);
  223. if (!mask32)
  224. continue;
  225. ret = readl_relaxed_poll_timeout(ptdev->iomem + pwrtrans_reg + (i * 4),
  226. val, !(mask32 & val),
  227. 100, timeout_us);
  228. if (ret) {
  229. drm_err(&ptdev->base, "timeout waiting on %s:%llx power transition",
  230. blk_name, mask);
  231. return ret;
  232. }
  233. }
  234. return 0;
  235. }
  236. /**
  237. * panthor_gpu_block_power_on() - Power-on a specific block of the GPU
  238. * @ptdev: Device.
  239. * @blk_name: Block name.
  240. * @pwron_reg: Power-on register for this block.
  241. * @pwrtrans_reg: Power transition register for this block.
  242. * @rdy_reg: Power transition ready register.
  243. * @mask: Sub-elements to power-on.
  244. * @timeout_us: Timeout in microseconds.
  245. *
  246. * Return: 0 on success, a negative error code otherwise.
  247. */
  248. int panthor_gpu_block_power_on(struct panthor_device *ptdev,
  249. const char *blk_name,
  250. u32 pwron_reg, u32 pwrtrans_reg,
  251. u32 rdy_reg, u64 mask, u32 timeout_us)
  252. {
  253. u32 val, i;
  254. int ret;
  255. for (i = 0; i < 2; i++) {
  256. u32 mask32 = mask >> (i * 32);
  257. if (!mask32)
  258. continue;
  259. ret = readl_relaxed_poll_timeout(ptdev->iomem + pwrtrans_reg + (i * 4),
  260. val, !(mask32 & val),
  261. 100, timeout_us);
  262. if (ret) {
  263. drm_err(&ptdev->base, "timeout waiting on %s:%llx power transition",
  264. blk_name, mask);
  265. return ret;
  266. }
  267. }
  268. if (mask & GENMASK(31, 0))
  269. gpu_write(ptdev, pwron_reg, mask);
  270. if (mask >> 32)
  271. gpu_write(ptdev, pwron_reg + 4, mask >> 32);
  272. for (i = 0; i < 2; i++) {
  273. u32 mask32 = mask >> (i * 32);
  274. if (!mask32)
  275. continue;
  276. ret = readl_relaxed_poll_timeout(ptdev->iomem + rdy_reg + (i * 4),
  277. val, (mask32 & val) == mask32,
  278. 100, timeout_us);
  279. if (ret) {
  280. drm_err(&ptdev->base, "timeout waiting on %s:%llx readiness",
  281. blk_name, mask);
  282. return ret;
  283. }
  284. }
  285. return 0;
  286. }
  287. /**
  288. * panthor_gpu_l2_power_on() - Power-on the L2-cache
  289. * @ptdev: Device.
  290. *
  291. * Return: 0 on success, a negative error code otherwise.
  292. */
  293. int panthor_gpu_l2_power_on(struct panthor_device *ptdev)
  294. {
  295. if (ptdev->gpu_info.l2_present != 1) {
  296. /*
  297. * Only support one core group now.
  298. * ~(l2_present - 1) unsets all bits in l2_present except
  299. * the bottom bit. (l2_present - 2) has all the bits in
  300. * the first core group set. AND them together to generate
  301. * a mask of cores in the first core group.
  302. */
  303. u64 core_mask = ~(ptdev->gpu_info.l2_present - 1) &
  304. (ptdev->gpu_info.l2_present - 2);
  305. drm_info_once(&ptdev->base, "using only 1st core group (%lu cores from %lu)\n",
  306. hweight64(core_mask),
  307. hweight64(ptdev->gpu_info.shader_present));
  308. }
  309. return panthor_gpu_power_on(ptdev, L2, 1, 20000);
  310. }
  311. /**
  312. * panthor_gpu_flush_caches() - Flush caches
  313. * @ptdev: Device.
  314. * @l2: L2 flush type.
  315. * @lsc: LSC flush type.
  316. * @other: Other flush type.
  317. *
  318. * Return: 0 on success, a negative error code otherwise.
  319. */
  320. int panthor_gpu_flush_caches(struct panthor_device *ptdev,
  321. u32 l2, u32 lsc, u32 other)
  322. {
  323. bool timedout = false;
  324. unsigned long flags;
  325. spin_lock_irqsave(&ptdev->gpu->reqs_lock, flags);
  326. if (!drm_WARN_ON(&ptdev->base,
  327. ptdev->gpu->pending_reqs & GPU_IRQ_CLEAN_CACHES_COMPLETED)) {
  328. ptdev->gpu->pending_reqs |= GPU_IRQ_CLEAN_CACHES_COMPLETED;
  329. gpu_write(ptdev, GPU_CMD, GPU_FLUSH_CACHES(l2, lsc, other));
  330. }
  331. spin_unlock_irqrestore(&ptdev->gpu->reqs_lock, flags);
  332. if (!wait_event_timeout(ptdev->gpu->reqs_acked,
  333. !(ptdev->gpu->pending_reqs & GPU_IRQ_CLEAN_CACHES_COMPLETED),
  334. msecs_to_jiffies(100))) {
  335. spin_lock_irqsave(&ptdev->gpu->reqs_lock, flags);
  336. if ((ptdev->gpu->pending_reqs & GPU_IRQ_CLEAN_CACHES_COMPLETED) != 0 &&
  337. !(gpu_read(ptdev, GPU_INT_RAWSTAT) & GPU_IRQ_CLEAN_CACHES_COMPLETED))
  338. timedout = true;
  339. else
  340. ptdev->gpu->pending_reqs &= ~GPU_IRQ_CLEAN_CACHES_COMPLETED;
  341. spin_unlock_irqrestore(&ptdev->gpu->reqs_lock, flags);
  342. }
  343. if (timedout) {
  344. drm_err(&ptdev->base, "Flush caches timeout");
  345. return -ETIMEDOUT;
  346. }
  347. return 0;
  348. }
  349. /**
  350. * panthor_gpu_soft_reset() - Issue a soft-reset
  351. * @ptdev: Device.
  352. *
  353. * Return: 0 on success, a negative error code otherwise.
  354. */
  355. int panthor_gpu_soft_reset(struct panthor_device *ptdev)
  356. {
  357. bool timedout = false;
  358. unsigned long flags;
  359. spin_lock_irqsave(&ptdev->gpu->reqs_lock, flags);
  360. if (!drm_WARN_ON(&ptdev->base,
  361. ptdev->gpu->pending_reqs & GPU_IRQ_RESET_COMPLETED)) {
  362. ptdev->gpu->pending_reqs |= GPU_IRQ_RESET_COMPLETED;
  363. gpu_write(ptdev, GPU_INT_CLEAR, GPU_IRQ_RESET_COMPLETED);
  364. gpu_write(ptdev, GPU_CMD, GPU_SOFT_RESET);
  365. }
  366. spin_unlock_irqrestore(&ptdev->gpu->reqs_lock, flags);
  367. if (!wait_event_timeout(ptdev->gpu->reqs_acked,
  368. !(ptdev->gpu->pending_reqs & GPU_IRQ_RESET_COMPLETED),
  369. msecs_to_jiffies(100))) {
  370. spin_lock_irqsave(&ptdev->gpu->reqs_lock, flags);
  371. if ((ptdev->gpu->pending_reqs & GPU_IRQ_RESET_COMPLETED) != 0 &&
  372. !(gpu_read(ptdev, GPU_INT_RAWSTAT) & GPU_IRQ_RESET_COMPLETED))
  373. timedout = true;
  374. else
  375. ptdev->gpu->pending_reqs &= ~GPU_IRQ_RESET_COMPLETED;
  376. spin_unlock_irqrestore(&ptdev->gpu->reqs_lock, flags);
  377. }
  378. if (timedout) {
  379. drm_err(&ptdev->base, "Soft reset timeout");
  380. return -ETIMEDOUT;
  381. }
  382. return 0;
  383. }
  384. /**
  385. * panthor_gpu_suspend() - Suspend the GPU block.
  386. * @ptdev: Device.
  387. *
  388. * Suspend the GPU irq. This should be called last in the suspend procedure,
  389. * after all other blocks have been suspented.
  390. */
  391. void panthor_gpu_suspend(struct panthor_device *ptdev)
  392. {
  393. /*
  394. * It may be preferable to simply power down the L2, but for now just
  395. * soft-reset which will leave the L2 powered down.
  396. */
  397. panthor_gpu_soft_reset(ptdev);
  398. panthor_gpu_irq_suspend(&ptdev->gpu->irq);
  399. }
  400. /**
  401. * panthor_gpu_resume() - Resume the GPU block.
  402. * @ptdev: Device.
  403. *
  404. * Resume the IRQ handler and power-on the L2-cache.
  405. * The FW takes care of powering the other blocks.
  406. */
  407. void panthor_gpu_resume(struct panthor_device *ptdev)
  408. {
  409. panthor_gpu_irq_resume(&ptdev->gpu->irq, GPU_INTERRUPTS_MASK);
  410. panthor_gpu_l2_power_on(ptdev);
  411. }