panthor_gpu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 <drm/drm_print.h>
  17. #include "panthor_device.h"
  18. #include "panthor_gpu.h"
  19. #include "panthor_hw.h"
  20. #include "panthor_regs.h"
  21. /**
  22. * struct panthor_gpu - GPU block management data.
  23. */
  24. struct panthor_gpu {
  25. /** @irq: GPU irq. */
  26. struct panthor_irq irq;
  27. /** @reqs_lock: Lock protecting access to pending_reqs. */
  28. spinlock_t reqs_lock;
  29. /** @pending_reqs: Pending GPU requests. */
  30. u32 pending_reqs;
  31. /** @reqs_acked: GPU request wait queue. */
  32. wait_queue_head_t reqs_acked;
  33. /** @cache_flush_lock: Lock to serialize cache flushes */
  34. struct mutex cache_flush_lock;
  35. };
  36. #define GPU_INTERRUPTS_MASK \
  37. (GPU_IRQ_FAULT | \
  38. GPU_IRQ_PROTM_FAULT | \
  39. GPU_IRQ_RESET_COMPLETED | \
  40. GPU_IRQ_CLEAN_CACHES_COMPLETED)
  41. static void panthor_gpu_coherency_set(struct panthor_device *ptdev)
  42. {
  43. gpu_write(ptdev, GPU_COHERENCY_PROTOCOL,
  44. ptdev->gpu_info.selected_coherency);
  45. }
  46. static void panthor_gpu_l2_config_set(struct panthor_device *ptdev)
  47. {
  48. const struct panthor_soc_data *data = ptdev->soc_data;
  49. u32 l2_config;
  50. u32 i;
  51. if (!data || !data->asn_hash_enable)
  52. return;
  53. if (GPU_ARCH_MAJOR(ptdev->gpu_info.gpu_id) < 11) {
  54. drm_err(&ptdev->base, "Custom ASN hash not supported by the device");
  55. return;
  56. }
  57. for (i = 0; i < ARRAY_SIZE(data->asn_hash); i++)
  58. gpu_write(ptdev, GPU_ASN_HASH(i), data->asn_hash[i]);
  59. l2_config = gpu_read(ptdev, GPU_L2_CONFIG);
  60. l2_config |= GPU_L2_CONFIG_ASN_HASH_ENABLE;
  61. gpu_write(ptdev, GPU_L2_CONFIG, l2_config);
  62. }
  63. static void panthor_gpu_irq_handler(struct panthor_device *ptdev, u32 status)
  64. {
  65. gpu_write(ptdev, GPU_INT_CLEAR, status);
  66. if (status & GPU_IRQ_FAULT) {
  67. u32 fault_status = gpu_read(ptdev, GPU_FAULT_STATUS);
  68. u64 address = gpu_read64(ptdev, GPU_FAULT_ADDR);
  69. drm_warn(&ptdev->base, "GPU Fault 0x%08x (%s) at 0x%016llx\n",
  70. fault_status, panthor_exception_name(ptdev, fault_status & 0xFF),
  71. address);
  72. }
  73. if (status & GPU_IRQ_PROTM_FAULT)
  74. drm_warn(&ptdev->base, "GPU Fault in protected mode\n");
  75. spin_lock(&ptdev->gpu->reqs_lock);
  76. if (status & ptdev->gpu->pending_reqs) {
  77. ptdev->gpu->pending_reqs &= ~status;
  78. wake_up_all(&ptdev->gpu->reqs_acked);
  79. }
  80. spin_unlock(&ptdev->gpu->reqs_lock);
  81. }
  82. PANTHOR_IRQ_HANDLER(gpu, GPU, panthor_gpu_irq_handler);
  83. /**
  84. * panthor_gpu_unplug() - Called when the GPU is unplugged.
  85. * @ptdev: Device to unplug.
  86. */
  87. void panthor_gpu_unplug(struct panthor_device *ptdev)
  88. {
  89. unsigned long flags;
  90. /* Make sure the IRQ handler is not running after that point. */
  91. if (!IS_ENABLED(CONFIG_PM) || pm_runtime_active(ptdev->base.dev))
  92. panthor_gpu_irq_suspend(&ptdev->gpu->irq);
  93. /* Wake-up all waiters. */
  94. spin_lock_irqsave(&ptdev->gpu->reqs_lock, flags);
  95. ptdev->gpu->pending_reqs = 0;
  96. wake_up_all(&ptdev->gpu->reqs_acked);
  97. spin_unlock_irqrestore(&ptdev->gpu->reqs_lock, flags);
  98. }
  99. /**
  100. * panthor_gpu_init() - Initialize the GPU block
  101. * @ptdev: Device.
  102. *
  103. * Return: 0 on success, a negative error code otherwise.
  104. */
  105. int panthor_gpu_init(struct panthor_device *ptdev)
  106. {
  107. struct panthor_gpu *gpu;
  108. u32 pa_bits;
  109. int ret, irq;
  110. gpu = drmm_kzalloc(&ptdev->base, sizeof(*gpu), GFP_KERNEL);
  111. if (!gpu)
  112. return -ENOMEM;
  113. spin_lock_init(&gpu->reqs_lock);
  114. init_waitqueue_head(&gpu->reqs_acked);
  115. mutex_init(&gpu->cache_flush_lock);
  116. ptdev->gpu = gpu;
  117. dma_set_max_seg_size(ptdev->base.dev, UINT_MAX);
  118. pa_bits = GPU_MMU_FEATURES_PA_BITS(ptdev->gpu_info.mmu_features);
  119. ret = dma_set_mask_and_coherent(ptdev->base.dev, DMA_BIT_MASK(pa_bits));
  120. if (ret)
  121. return ret;
  122. irq = platform_get_irq_byname(to_platform_device(ptdev->base.dev), "gpu");
  123. if (irq < 0)
  124. return irq;
  125. ret = panthor_request_gpu_irq(ptdev, &ptdev->gpu->irq, irq, GPU_INTERRUPTS_MASK);
  126. if (ret)
  127. return ret;
  128. return 0;
  129. }
  130. /**
  131. * panthor_gpu_block_power_off() - Power-off a specific block of the GPU
  132. * @ptdev: Device.
  133. * @blk_name: Block name.
  134. * @pwroff_reg: Power-off register for this block.
  135. * @pwrtrans_reg: Power transition register for this block.
  136. * @mask: Sub-elements to power-off.
  137. * @timeout_us: Timeout in microseconds.
  138. *
  139. * Return: 0 on success, a negative error code otherwise.
  140. */
  141. int panthor_gpu_block_power_off(struct panthor_device *ptdev,
  142. const char *blk_name,
  143. u32 pwroff_reg, u32 pwrtrans_reg,
  144. u64 mask, u32 timeout_us)
  145. {
  146. u32 val;
  147. int ret;
  148. ret = gpu_read64_relaxed_poll_timeout(ptdev, pwrtrans_reg, val,
  149. !(mask & val), 100, timeout_us);
  150. if (ret) {
  151. drm_err(&ptdev->base,
  152. "timeout waiting on %s:%llx power transition", blk_name,
  153. mask);
  154. return ret;
  155. }
  156. gpu_write64(ptdev, pwroff_reg, mask);
  157. ret = gpu_read64_relaxed_poll_timeout(ptdev, pwrtrans_reg, val,
  158. !(mask & val), 100, timeout_us);
  159. if (ret) {
  160. drm_err(&ptdev->base,
  161. "timeout waiting on %s:%llx power transition", blk_name,
  162. mask);
  163. return ret;
  164. }
  165. return 0;
  166. }
  167. /**
  168. * panthor_gpu_block_power_on() - Power-on a specific block of the GPU
  169. * @ptdev: Device.
  170. * @blk_name: Block name.
  171. * @pwron_reg: Power-on register for this block.
  172. * @pwrtrans_reg: Power transition register for this block.
  173. * @rdy_reg: Power transition ready register.
  174. * @mask: Sub-elements to power-on.
  175. * @timeout_us: Timeout in microseconds.
  176. *
  177. * Return: 0 on success, a negative error code otherwise.
  178. */
  179. int panthor_gpu_block_power_on(struct panthor_device *ptdev,
  180. const char *blk_name,
  181. u32 pwron_reg, u32 pwrtrans_reg,
  182. u32 rdy_reg, u64 mask, u32 timeout_us)
  183. {
  184. u32 val;
  185. int ret;
  186. ret = gpu_read64_relaxed_poll_timeout(ptdev, pwrtrans_reg, val,
  187. !(mask & val), 100, timeout_us);
  188. if (ret) {
  189. drm_err(&ptdev->base,
  190. "timeout waiting on %s:%llx power transition", blk_name,
  191. mask);
  192. return ret;
  193. }
  194. gpu_write64(ptdev, pwron_reg, mask);
  195. ret = gpu_read64_relaxed_poll_timeout(ptdev, rdy_reg, val,
  196. (mask & val) == val,
  197. 100, timeout_us);
  198. if (ret) {
  199. drm_err(&ptdev->base, "timeout waiting on %s:%llx readiness",
  200. blk_name, mask);
  201. return ret;
  202. }
  203. return 0;
  204. }
  205. void panthor_gpu_l2_power_off(struct panthor_device *ptdev)
  206. {
  207. panthor_gpu_power_off(ptdev, L2, ptdev->gpu_info.l2_present, 20000);
  208. }
  209. /**
  210. * panthor_gpu_l2_power_on() - Power-on the L2-cache
  211. * @ptdev: Device.
  212. *
  213. * Return: 0 on success, a negative error code otherwise.
  214. */
  215. int panthor_gpu_l2_power_on(struct panthor_device *ptdev)
  216. {
  217. if (ptdev->gpu_info.l2_present != 1) {
  218. /*
  219. * Only support one core group now.
  220. * ~(l2_present - 1) unsets all bits in l2_present except
  221. * the bottom bit. (l2_present - 2) has all the bits in
  222. * the first core group set. AND them together to generate
  223. * a mask of cores in the first core group.
  224. */
  225. u64 core_mask = ~(ptdev->gpu_info.l2_present - 1) &
  226. (ptdev->gpu_info.l2_present - 2);
  227. drm_info_once(&ptdev->base, "using only 1st core group (%lu cores from %lu)\n",
  228. hweight64(core_mask),
  229. hweight64(ptdev->gpu_info.shader_present));
  230. }
  231. /* Set the desired coherency mode and L2 config before the power up of L2 */
  232. panthor_gpu_coherency_set(ptdev);
  233. panthor_gpu_l2_config_set(ptdev);
  234. return panthor_gpu_power_on(ptdev, L2, 1, 20000);
  235. }
  236. /**
  237. * panthor_gpu_flush_caches() - Flush caches
  238. * @ptdev: Device.
  239. * @l2: L2 flush type.
  240. * @lsc: LSC flush type.
  241. * @other: Other flush type.
  242. *
  243. * Return: 0 on success, a negative error code otherwise.
  244. */
  245. int panthor_gpu_flush_caches(struct panthor_device *ptdev,
  246. u32 l2, u32 lsc, u32 other)
  247. {
  248. unsigned long flags;
  249. int ret = 0;
  250. /* Serialize cache flush operations. */
  251. guard(mutex)(&ptdev->gpu->cache_flush_lock);
  252. spin_lock_irqsave(&ptdev->gpu->reqs_lock, flags);
  253. if (!(ptdev->gpu->pending_reqs & GPU_IRQ_CLEAN_CACHES_COMPLETED)) {
  254. ptdev->gpu->pending_reqs |= GPU_IRQ_CLEAN_CACHES_COMPLETED;
  255. gpu_write(ptdev, GPU_CMD, GPU_FLUSH_CACHES(l2, lsc, other));
  256. } else {
  257. ret = -EIO;
  258. }
  259. spin_unlock_irqrestore(&ptdev->gpu->reqs_lock, flags);
  260. if (ret)
  261. return ret;
  262. if (!wait_event_timeout(ptdev->gpu->reqs_acked,
  263. !(ptdev->gpu->pending_reqs & GPU_IRQ_CLEAN_CACHES_COMPLETED),
  264. msecs_to_jiffies(100))) {
  265. spin_lock_irqsave(&ptdev->gpu->reqs_lock, flags);
  266. if ((ptdev->gpu->pending_reqs & GPU_IRQ_CLEAN_CACHES_COMPLETED) != 0 &&
  267. !(gpu_read(ptdev, GPU_INT_RAWSTAT) & GPU_IRQ_CLEAN_CACHES_COMPLETED))
  268. ret = -ETIMEDOUT;
  269. else
  270. ptdev->gpu->pending_reqs &= ~GPU_IRQ_CLEAN_CACHES_COMPLETED;
  271. spin_unlock_irqrestore(&ptdev->gpu->reqs_lock, flags);
  272. }
  273. if (ret) {
  274. panthor_device_schedule_reset(ptdev);
  275. drm_err(&ptdev->base, "Flush caches timeout");
  276. }
  277. return ret;
  278. }
  279. /**
  280. * panthor_gpu_soft_reset() - Issue a soft-reset
  281. * @ptdev: Device.
  282. *
  283. * Return: 0 on success, a negative error code otherwise.
  284. */
  285. int panthor_gpu_soft_reset(struct panthor_device *ptdev)
  286. {
  287. bool timedout = false;
  288. unsigned long flags;
  289. spin_lock_irqsave(&ptdev->gpu->reqs_lock, flags);
  290. if (!drm_WARN_ON(&ptdev->base,
  291. ptdev->gpu->pending_reqs & GPU_IRQ_RESET_COMPLETED)) {
  292. ptdev->gpu->pending_reqs |= GPU_IRQ_RESET_COMPLETED;
  293. gpu_write(ptdev, GPU_INT_CLEAR, GPU_IRQ_RESET_COMPLETED);
  294. gpu_write(ptdev, GPU_CMD, GPU_SOFT_RESET);
  295. }
  296. spin_unlock_irqrestore(&ptdev->gpu->reqs_lock, flags);
  297. if (!wait_event_timeout(ptdev->gpu->reqs_acked,
  298. !(ptdev->gpu->pending_reqs & GPU_IRQ_RESET_COMPLETED),
  299. msecs_to_jiffies(100))) {
  300. spin_lock_irqsave(&ptdev->gpu->reqs_lock, flags);
  301. if ((ptdev->gpu->pending_reqs & GPU_IRQ_RESET_COMPLETED) != 0 &&
  302. !(gpu_read(ptdev, GPU_INT_RAWSTAT) & GPU_IRQ_RESET_COMPLETED))
  303. timedout = true;
  304. else
  305. ptdev->gpu->pending_reqs &= ~GPU_IRQ_RESET_COMPLETED;
  306. spin_unlock_irqrestore(&ptdev->gpu->reqs_lock, flags);
  307. }
  308. if (timedout) {
  309. drm_err(&ptdev->base, "Soft reset timeout");
  310. return -ETIMEDOUT;
  311. }
  312. ptdev->gpu->pending_reqs = 0;
  313. return 0;
  314. }
  315. /**
  316. * panthor_gpu_suspend() - Suspend the GPU block.
  317. * @ptdev: Device.
  318. *
  319. * Suspend the GPU irq. This should be called last in the suspend procedure,
  320. * after all other blocks have been suspented.
  321. */
  322. void panthor_gpu_suspend(struct panthor_device *ptdev)
  323. {
  324. /* On a fast reset, simply power down the L2. */
  325. if (!ptdev->reset.fast)
  326. panthor_hw_soft_reset(ptdev);
  327. else
  328. panthor_hw_l2_power_off(ptdev);
  329. panthor_gpu_irq_suspend(&ptdev->gpu->irq);
  330. }
  331. /**
  332. * panthor_gpu_resume() - Resume the GPU block.
  333. * @ptdev: Device.
  334. *
  335. * Resume the IRQ handler and power-on the L2-cache.
  336. * The FW takes care of powering the other blocks.
  337. */
  338. void panthor_gpu_resume(struct panthor_device *ptdev)
  339. {
  340. panthor_gpu_irq_resume(&ptdev->gpu->irq, GPU_INTERRUPTS_MASK);
  341. panthor_hw_l2_power_on(ptdev);
  342. }