panthor_device.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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 2023 Collabora ltd. */
  5. #include <linux/clk.h>
  6. #include <linux/mm.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/pm_domain.h>
  9. #include <linux/pm_runtime.h>
  10. #include <linux/regulator/consumer.h>
  11. #include <linux/reset.h>
  12. #include <drm/drm_drv.h>
  13. #include <drm/drm_managed.h>
  14. #include <drm/drm_print.h>
  15. #include "panthor_devfreq.h"
  16. #include "panthor_device.h"
  17. #include "panthor_fw.h"
  18. #include "panthor_gem.h"
  19. #include "panthor_gpu.h"
  20. #include "panthor_hw.h"
  21. #include "panthor_mmu.h"
  22. #include "panthor_pwr.h"
  23. #include "panthor_regs.h"
  24. #include "panthor_sched.h"
  25. static int panthor_gpu_coherency_init(struct panthor_device *ptdev)
  26. {
  27. BUILD_BUG_ON(GPU_COHERENCY_NONE != DRM_PANTHOR_GPU_COHERENCY_NONE);
  28. BUILD_BUG_ON(GPU_COHERENCY_ACE_LITE != DRM_PANTHOR_GPU_COHERENCY_ACE_LITE);
  29. BUILD_BUG_ON(GPU_COHERENCY_ACE != DRM_PANTHOR_GPU_COHERENCY_ACE);
  30. /* Start with no coherency, and update it if the device is flagged coherent. */
  31. ptdev->gpu_info.selected_coherency = GPU_COHERENCY_NONE;
  32. ptdev->coherent = device_get_dma_attr(ptdev->base.dev) == DEV_DMA_COHERENT;
  33. if (!ptdev->coherent)
  34. return 0;
  35. /* Check if the ACE-Lite coherency protocol is actually supported by the GPU.
  36. * ACE protocol has never been supported for command stream frontend GPUs.
  37. */
  38. if ((gpu_read(ptdev, GPU_COHERENCY_FEATURES) &
  39. GPU_COHERENCY_PROT_BIT(ACE_LITE))) {
  40. ptdev->gpu_info.selected_coherency = GPU_COHERENCY_ACE_LITE;
  41. return 0;
  42. }
  43. drm_err(&ptdev->base, "Coherency not supported by the device");
  44. return -ENOTSUPP;
  45. }
  46. static int panthor_clk_init(struct panthor_device *ptdev)
  47. {
  48. ptdev->clks.core = devm_clk_get(ptdev->base.dev, NULL);
  49. if (IS_ERR(ptdev->clks.core))
  50. return dev_err_probe(ptdev->base.dev,
  51. PTR_ERR(ptdev->clks.core),
  52. "get 'core' clock failed");
  53. ptdev->clks.stacks = devm_clk_get_optional(ptdev->base.dev, "stacks");
  54. if (IS_ERR(ptdev->clks.stacks))
  55. return dev_err_probe(ptdev->base.dev,
  56. PTR_ERR(ptdev->clks.stacks),
  57. "get 'stacks' clock failed");
  58. ptdev->clks.coregroup = devm_clk_get_optional(ptdev->base.dev, "coregroup");
  59. if (IS_ERR(ptdev->clks.coregroup))
  60. return dev_err_probe(ptdev->base.dev,
  61. PTR_ERR(ptdev->clks.coregroup),
  62. "get 'coregroup' clock failed");
  63. drm_info(&ptdev->base, "clock rate = %lu\n", clk_get_rate(ptdev->clks.core));
  64. return 0;
  65. }
  66. static int panthor_init_power(struct device *dev)
  67. {
  68. struct dev_pm_domain_list *pd_list = NULL;
  69. if (dev->pm_domain)
  70. return 0;
  71. return devm_pm_domain_attach_list(dev, NULL, &pd_list);
  72. }
  73. void panthor_device_unplug(struct panthor_device *ptdev)
  74. {
  75. /* This function can be called from two different path: the reset work
  76. * and the platform device remove callback. drm_dev_unplug() doesn't
  77. * deal with concurrent callers, so we have to protect drm_dev_unplug()
  78. * calls with our own lock, and bail out if the device is already
  79. * unplugged.
  80. */
  81. mutex_lock(&ptdev->unplug.lock);
  82. if (drm_dev_is_unplugged(&ptdev->base)) {
  83. /* Someone beat us, release the lock and wait for the unplug
  84. * operation to be reported as done.
  85. **/
  86. mutex_unlock(&ptdev->unplug.lock);
  87. wait_for_completion(&ptdev->unplug.done);
  88. return;
  89. }
  90. drm_WARN_ON(&ptdev->base, pm_runtime_get_sync(ptdev->base.dev) < 0);
  91. /* Call drm_dev_unplug() so any access to HW blocks happening after
  92. * that point get rejected.
  93. */
  94. drm_dev_unplug(&ptdev->base);
  95. /* We do the rest of the unplug with the unplug lock released,
  96. * future callers will wait on ptdev->unplug.done anyway.
  97. */
  98. mutex_unlock(&ptdev->unplug.lock);
  99. /* Now, try to cleanly shutdown the GPU before the device resources
  100. * get reclaimed.
  101. */
  102. panthor_sched_unplug(ptdev);
  103. panthor_fw_unplug(ptdev);
  104. panthor_mmu_unplug(ptdev);
  105. panthor_gpu_unplug(ptdev);
  106. panthor_pwr_unplug(ptdev);
  107. pm_runtime_dont_use_autosuspend(ptdev->base.dev);
  108. pm_runtime_put_sync_suspend(ptdev->base.dev);
  109. /* If PM is disabled, we need to call the suspend handler manually. */
  110. if (!IS_ENABLED(CONFIG_PM))
  111. panthor_device_suspend(ptdev->base.dev);
  112. /* Report the unplug operation as done to unblock concurrent
  113. * panthor_device_unplug() callers.
  114. */
  115. complete_all(&ptdev->unplug.done);
  116. }
  117. static void panthor_device_reset_cleanup(struct drm_device *ddev, void *data)
  118. {
  119. struct panthor_device *ptdev = container_of(ddev, struct panthor_device, base);
  120. disable_work_sync(&ptdev->reset.work);
  121. destroy_workqueue(ptdev->reset.wq);
  122. }
  123. static void panthor_device_reset_work(struct work_struct *work)
  124. {
  125. struct panthor_device *ptdev = container_of(work, struct panthor_device, reset.work);
  126. int ret = 0, cookie;
  127. /* If the device is entering suspend, we don't reset. A slow reset will
  128. * be forced at resume time instead.
  129. */
  130. if (atomic_read(&ptdev->pm.state) != PANTHOR_DEVICE_PM_STATE_ACTIVE)
  131. return;
  132. if (!drm_dev_enter(&ptdev->base, &cookie))
  133. return;
  134. panthor_sched_pre_reset(ptdev);
  135. panthor_fw_pre_reset(ptdev, true);
  136. panthor_mmu_pre_reset(ptdev);
  137. panthor_hw_soft_reset(ptdev);
  138. panthor_hw_l2_power_on(ptdev);
  139. panthor_mmu_post_reset(ptdev);
  140. ret = panthor_fw_post_reset(ptdev);
  141. atomic_set(&ptdev->reset.pending, 0);
  142. panthor_sched_post_reset(ptdev, ret != 0);
  143. drm_dev_exit(cookie);
  144. if (ret) {
  145. panthor_device_unplug(ptdev);
  146. drm_err(&ptdev->base, "Failed to boot MCU after reset, making device unusable.");
  147. }
  148. }
  149. static bool panthor_device_is_initialized(struct panthor_device *ptdev)
  150. {
  151. return !!ptdev->scheduler;
  152. }
  153. static void panthor_device_free_page(struct drm_device *ddev, void *data)
  154. {
  155. __free_page(data);
  156. }
  157. int panthor_device_init(struct panthor_device *ptdev)
  158. {
  159. u32 *dummy_page_virt;
  160. struct resource *res;
  161. struct page *p;
  162. int ret;
  163. ptdev->soc_data = of_device_get_match_data(ptdev->base.dev);
  164. init_completion(&ptdev->unplug.done);
  165. ret = drmm_mutex_init(&ptdev->base, &ptdev->unplug.lock);
  166. if (ret)
  167. return ret;
  168. ret = drmm_mutex_init(&ptdev->base, &ptdev->pm.mmio_lock);
  169. if (ret)
  170. return ret;
  171. #ifdef CONFIG_DEBUG_FS
  172. drmm_mutex_init(&ptdev->base, &ptdev->gems.lock);
  173. INIT_LIST_HEAD(&ptdev->gems.node);
  174. #endif
  175. atomic_set(&ptdev->pm.state, PANTHOR_DEVICE_PM_STATE_SUSPENDED);
  176. p = alloc_page(GFP_KERNEL | __GFP_ZERO);
  177. if (!p)
  178. return -ENOMEM;
  179. ptdev->pm.dummy_latest_flush = p;
  180. dummy_page_virt = page_address(p);
  181. ret = drmm_add_action_or_reset(&ptdev->base, panthor_device_free_page,
  182. ptdev->pm.dummy_latest_flush);
  183. if (ret)
  184. return ret;
  185. /*
  186. * Set the dummy page holding the latest flush to 1. This will cause the
  187. * flush to avoided as we know it isn't necessary if the submission
  188. * happens while the dummy page is mapped. Zero cannot be used because
  189. * that means 'always flush'.
  190. */
  191. *dummy_page_virt = 1;
  192. INIT_WORK(&ptdev->reset.work, panthor_device_reset_work);
  193. ptdev->reset.wq = alloc_ordered_workqueue("panthor-reset-wq", 0);
  194. if (!ptdev->reset.wq)
  195. return -ENOMEM;
  196. ret = drmm_add_action_or_reset(&ptdev->base, panthor_device_reset_cleanup, NULL);
  197. if (ret)
  198. return ret;
  199. ret = panthor_clk_init(ptdev);
  200. if (ret)
  201. return ret;
  202. ret = panthor_init_power(ptdev->base.dev);
  203. if (ret < 0) {
  204. drm_err(&ptdev->base, "init power domains failed, ret=%d", ret);
  205. return ret;
  206. }
  207. ret = panthor_devfreq_init(ptdev);
  208. if (ret)
  209. return ret;
  210. ptdev->iomem = devm_platform_get_and_ioremap_resource(to_platform_device(ptdev->base.dev),
  211. 0, &res);
  212. if (IS_ERR(ptdev->iomem))
  213. return PTR_ERR(ptdev->iomem);
  214. ptdev->phys_addr = res->start;
  215. ret = devm_pm_runtime_enable(ptdev->base.dev);
  216. if (ret)
  217. return ret;
  218. ret = pm_runtime_resume_and_get(ptdev->base.dev);
  219. if (ret)
  220. return ret;
  221. /* If PM is disabled, we need to call panthor_device_resume() manually. */
  222. if (!IS_ENABLED(CONFIG_PM)) {
  223. ret = panthor_device_resume(ptdev->base.dev);
  224. if (ret)
  225. return ret;
  226. }
  227. ret = panthor_hw_init(ptdev);
  228. if (ret)
  229. goto err_rpm_put;
  230. ret = panthor_pwr_init(ptdev);
  231. if (ret)
  232. goto err_rpm_put;
  233. ret = panthor_gpu_init(ptdev);
  234. if (ret)
  235. goto err_unplug_pwr;
  236. ret = panthor_gpu_coherency_init(ptdev);
  237. if (ret)
  238. goto err_unplug_gpu;
  239. ret = panthor_mmu_init(ptdev);
  240. if (ret)
  241. goto err_unplug_gpu;
  242. ret = panthor_fw_init(ptdev);
  243. if (ret)
  244. goto err_unplug_mmu;
  245. ret = panthor_sched_init(ptdev);
  246. if (ret)
  247. goto err_unplug_fw;
  248. panthor_gem_init(ptdev);
  249. /* ~3 frames */
  250. pm_runtime_set_autosuspend_delay(ptdev->base.dev, 50);
  251. pm_runtime_use_autosuspend(ptdev->base.dev);
  252. ret = drm_dev_register(&ptdev->base, 0);
  253. if (ret)
  254. goto err_disable_autosuspend;
  255. pm_runtime_put_autosuspend(ptdev->base.dev);
  256. return 0;
  257. err_disable_autosuspend:
  258. pm_runtime_dont_use_autosuspend(ptdev->base.dev);
  259. panthor_sched_unplug(ptdev);
  260. err_unplug_fw:
  261. panthor_fw_unplug(ptdev);
  262. err_unplug_mmu:
  263. panthor_mmu_unplug(ptdev);
  264. err_unplug_gpu:
  265. panthor_gpu_unplug(ptdev);
  266. err_unplug_pwr:
  267. panthor_pwr_unplug(ptdev);
  268. err_rpm_put:
  269. pm_runtime_put_sync_suspend(ptdev->base.dev);
  270. return ret;
  271. }
  272. #define PANTHOR_EXCEPTION(id) \
  273. [DRM_PANTHOR_EXCEPTION_ ## id] = { \
  274. .name = #id, \
  275. }
  276. struct panthor_exception_info {
  277. const char *name;
  278. };
  279. static const struct panthor_exception_info panthor_exception_infos[] = {
  280. PANTHOR_EXCEPTION(OK),
  281. PANTHOR_EXCEPTION(TERMINATED),
  282. PANTHOR_EXCEPTION(KABOOM),
  283. PANTHOR_EXCEPTION(EUREKA),
  284. PANTHOR_EXCEPTION(ACTIVE),
  285. PANTHOR_EXCEPTION(CS_RES_TERM),
  286. PANTHOR_EXCEPTION(CS_CONFIG_FAULT),
  287. PANTHOR_EXCEPTION(CS_UNRECOVERABLE),
  288. PANTHOR_EXCEPTION(CS_ENDPOINT_FAULT),
  289. PANTHOR_EXCEPTION(CS_BUS_FAULT),
  290. PANTHOR_EXCEPTION(CS_INSTR_INVALID),
  291. PANTHOR_EXCEPTION(CS_CALL_STACK_OVERFLOW),
  292. PANTHOR_EXCEPTION(CS_INHERIT_FAULT),
  293. PANTHOR_EXCEPTION(INSTR_INVALID_PC),
  294. PANTHOR_EXCEPTION(INSTR_INVALID_ENC),
  295. PANTHOR_EXCEPTION(INSTR_BARRIER_FAULT),
  296. PANTHOR_EXCEPTION(DATA_INVALID_FAULT),
  297. PANTHOR_EXCEPTION(TILE_RANGE_FAULT),
  298. PANTHOR_EXCEPTION(ADDR_RANGE_FAULT),
  299. PANTHOR_EXCEPTION(IMPRECISE_FAULT),
  300. PANTHOR_EXCEPTION(OOM),
  301. PANTHOR_EXCEPTION(CSF_FW_INTERNAL_ERROR),
  302. PANTHOR_EXCEPTION(CSF_RES_EVICTION_TIMEOUT),
  303. PANTHOR_EXCEPTION(GPU_BUS_FAULT),
  304. PANTHOR_EXCEPTION(GPU_SHAREABILITY_FAULT),
  305. PANTHOR_EXCEPTION(SYS_SHAREABILITY_FAULT),
  306. PANTHOR_EXCEPTION(GPU_CACHEABILITY_FAULT),
  307. PANTHOR_EXCEPTION(TRANSLATION_FAULT_0),
  308. PANTHOR_EXCEPTION(TRANSLATION_FAULT_1),
  309. PANTHOR_EXCEPTION(TRANSLATION_FAULT_2),
  310. PANTHOR_EXCEPTION(TRANSLATION_FAULT_3),
  311. PANTHOR_EXCEPTION(TRANSLATION_FAULT_4),
  312. PANTHOR_EXCEPTION(PERM_FAULT_0),
  313. PANTHOR_EXCEPTION(PERM_FAULT_1),
  314. PANTHOR_EXCEPTION(PERM_FAULT_2),
  315. PANTHOR_EXCEPTION(PERM_FAULT_3),
  316. PANTHOR_EXCEPTION(ACCESS_FLAG_1),
  317. PANTHOR_EXCEPTION(ACCESS_FLAG_2),
  318. PANTHOR_EXCEPTION(ACCESS_FLAG_3),
  319. PANTHOR_EXCEPTION(ADDR_SIZE_FAULT_IN),
  320. PANTHOR_EXCEPTION(ADDR_SIZE_FAULT_OUT0),
  321. PANTHOR_EXCEPTION(ADDR_SIZE_FAULT_OUT1),
  322. PANTHOR_EXCEPTION(ADDR_SIZE_FAULT_OUT2),
  323. PANTHOR_EXCEPTION(ADDR_SIZE_FAULT_OUT3),
  324. PANTHOR_EXCEPTION(MEM_ATTR_FAULT_0),
  325. PANTHOR_EXCEPTION(MEM_ATTR_FAULT_1),
  326. PANTHOR_EXCEPTION(MEM_ATTR_FAULT_2),
  327. PANTHOR_EXCEPTION(MEM_ATTR_FAULT_3),
  328. };
  329. const char *panthor_exception_name(struct panthor_device *ptdev, u32 exception_code)
  330. {
  331. if (exception_code >= ARRAY_SIZE(panthor_exception_infos) ||
  332. !panthor_exception_infos[exception_code].name)
  333. return "Unknown exception type";
  334. return panthor_exception_infos[exception_code].name;
  335. }
  336. static vm_fault_t panthor_mmio_vm_fault(struct vm_fault *vmf)
  337. {
  338. struct vm_area_struct *vma = vmf->vma;
  339. struct panthor_device *ptdev = vma->vm_private_data;
  340. u64 offset = (u64)vma->vm_pgoff << PAGE_SHIFT;
  341. unsigned long pfn;
  342. pgprot_t pgprot;
  343. vm_fault_t ret;
  344. bool active;
  345. int cookie;
  346. if (!drm_dev_enter(&ptdev->base, &cookie))
  347. return VM_FAULT_SIGBUS;
  348. mutex_lock(&ptdev->pm.mmio_lock);
  349. active = atomic_read(&ptdev->pm.state) == PANTHOR_DEVICE_PM_STATE_ACTIVE;
  350. switch (offset) {
  351. case DRM_PANTHOR_USER_FLUSH_ID_MMIO_OFFSET:
  352. if (active)
  353. pfn = __phys_to_pfn(ptdev->phys_addr + CSF_GPU_LATEST_FLUSH_ID);
  354. else
  355. pfn = page_to_pfn(ptdev->pm.dummy_latest_flush);
  356. break;
  357. default:
  358. ret = VM_FAULT_SIGBUS;
  359. goto out_unlock;
  360. }
  361. pgprot = vma->vm_page_prot;
  362. if (active)
  363. pgprot = pgprot_noncached(pgprot);
  364. ret = vmf_insert_pfn_prot(vma, vmf->address, pfn, pgprot);
  365. out_unlock:
  366. mutex_unlock(&ptdev->pm.mmio_lock);
  367. drm_dev_exit(cookie);
  368. return ret;
  369. }
  370. static const struct vm_operations_struct panthor_mmio_vm_ops = {
  371. .fault = panthor_mmio_vm_fault,
  372. };
  373. int panthor_device_mmap_io(struct panthor_device *ptdev, struct vm_area_struct *vma)
  374. {
  375. u64 offset = (u64)vma->vm_pgoff << PAGE_SHIFT;
  376. if ((vma->vm_flags & VM_SHARED) == 0)
  377. return -EINVAL;
  378. switch (offset) {
  379. case DRM_PANTHOR_USER_FLUSH_ID_MMIO_OFFSET:
  380. if (vma->vm_end - vma->vm_start != PAGE_SIZE ||
  381. (vma->vm_flags & (VM_WRITE | VM_EXEC)))
  382. return -EINVAL;
  383. vm_flags_clear(vma, VM_MAYWRITE);
  384. break;
  385. default:
  386. return -EINVAL;
  387. }
  388. /* Defer actual mapping to the fault handler. */
  389. vma->vm_private_data = ptdev;
  390. vma->vm_ops = &panthor_mmio_vm_ops;
  391. vm_flags_set(vma,
  392. VM_IO | VM_DONTCOPY | VM_DONTEXPAND |
  393. VM_NORESERVE | VM_DONTDUMP | VM_PFNMAP);
  394. return 0;
  395. }
  396. static int panthor_device_resume_hw_components(struct panthor_device *ptdev)
  397. {
  398. int ret;
  399. panthor_pwr_resume(ptdev);
  400. panthor_gpu_resume(ptdev);
  401. panthor_mmu_resume(ptdev);
  402. ret = panthor_fw_resume(ptdev);
  403. if (!ret)
  404. return 0;
  405. panthor_mmu_suspend(ptdev);
  406. panthor_gpu_suspend(ptdev);
  407. panthor_pwr_suspend(ptdev);
  408. return ret;
  409. }
  410. int panthor_device_resume(struct device *dev)
  411. {
  412. struct panthor_device *ptdev = dev_get_drvdata(dev);
  413. int ret, cookie;
  414. if (atomic_read(&ptdev->pm.state) != PANTHOR_DEVICE_PM_STATE_SUSPENDED)
  415. return -EINVAL;
  416. atomic_set(&ptdev->pm.state, PANTHOR_DEVICE_PM_STATE_RESUMING);
  417. ret = clk_prepare_enable(ptdev->clks.core);
  418. if (ret)
  419. goto err_set_suspended;
  420. ret = clk_prepare_enable(ptdev->clks.stacks);
  421. if (ret)
  422. goto err_disable_core_clk;
  423. ret = clk_prepare_enable(ptdev->clks.coregroup);
  424. if (ret)
  425. goto err_disable_stacks_clk;
  426. panthor_devfreq_resume(ptdev);
  427. if (panthor_device_is_initialized(ptdev) &&
  428. drm_dev_enter(&ptdev->base, &cookie)) {
  429. /* If there was a reset pending at the time we suspended the
  430. * device, we force a slow reset.
  431. */
  432. if (atomic_read(&ptdev->reset.pending)) {
  433. ptdev->reset.fast = false;
  434. atomic_set(&ptdev->reset.pending, 0);
  435. }
  436. ret = panthor_device_resume_hw_components(ptdev);
  437. if (ret && ptdev->reset.fast) {
  438. drm_err(&ptdev->base, "Fast reset failed, trying a slow reset");
  439. ptdev->reset.fast = false;
  440. ret = panthor_device_resume_hw_components(ptdev);
  441. }
  442. if (!ret)
  443. panthor_sched_resume(ptdev);
  444. drm_dev_exit(cookie);
  445. if (ret)
  446. goto err_suspend_devfreq;
  447. }
  448. /* Clear all IOMEM mappings pointing to this device after we've
  449. * resumed. This way the fake mappings pointing to the dummy pages
  450. * are removed and the real iomem mapping will be restored on next
  451. * access.
  452. */
  453. mutex_lock(&ptdev->pm.mmio_lock);
  454. unmap_mapping_range(ptdev->base.anon_inode->i_mapping,
  455. DRM_PANTHOR_USER_MMIO_OFFSET, 0, 1);
  456. atomic_set(&ptdev->pm.state, PANTHOR_DEVICE_PM_STATE_ACTIVE);
  457. mutex_unlock(&ptdev->pm.mmio_lock);
  458. return 0;
  459. err_suspend_devfreq:
  460. panthor_devfreq_suspend(ptdev);
  461. clk_disable_unprepare(ptdev->clks.coregroup);
  462. err_disable_stacks_clk:
  463. clk_disable_unprepare(ptdev->clks.stacks);
  464. err_disable_core_clk:
  465. clk_disable_unprepare(ptdev->clks.core);
  466. err_set_suspended:
  467. atomic_set(&ptdev->pm.state, PANTHOR_DEVICE_PM_STATE_SUSPENDED);
  468. atomic_set(&ptdev->pm.recovery_needed, 1);
  469. return ret;
  470. }
  471. int panthor_device_suspend(struct device *dev)
  472. {
  473. struct panthor_device *ptdev = dev_get_drvdata(dev);
  474. int cookie;
  475. if (atomic_read(&ptdev->pm.state) != PANTHOR_DEVICE_PM_STATE_ACTIVE)
  476. return -EINVAL;
  477. /* Clear all IOMEM mappings pointing to this device before we
  478. * shutdown the power-domain and clocks. Failing to do that results
  479. * in external aborts when the process accesses the iomem region.
  480. * We change the state and call unmap_mapping_range() with the
  481. * mmio_lock held to make sure the vm_fault handler won't set up
  482. * invalid mappings.
  483. */
  484. mutex_lock(&ptdev->pm.mmio_lock);
  485. atomic_set(&ptdev->pm.state, PANTHOR_DEVICE_PM_STATE_SUSPENDING);
  486. unmap_mapping_range(ptdev->base.anon_inode->i_mapping,
  487. DRM_PANTHOR_USER_MMIO_OFFSET, 0, 1);
  488. mutex_unlock(&ptdev->pm.mmio_lock);
  489. if (panthor_device_is_initialized(ptdev) &&
  490. drm_dev_enter(&ptdev->base, &cookie)) {
  491. cancel_work_sync(&ptdev->reset.work);
  492. /* We prepare everything as if we were resetting the GPU.
  493. * The end of the reset will happen in the resume path though.
  494. */
  495. panthor_sched_suspend(ptdev);
  496. panthor_fw_suspend(ptdev);
  497. panthor_mmu_suspend(ptdev);
  498. panthor_gpu_suspend(ptdev);
  499. panthor_pwr_suspend(ptdev);
  500. drm_dev_exit(cookie);
  501. }
  502. panthor_devfreq_suspend(ptdev);
  503. clk_disable_unprepare(ptdev->clks.coregroup);
  504. clk_disable_unprepare(ptdev->clks.stacks);
  505. clk_disable_unprepare(ptdev->clks.core);
  506. atomic_set(&ptdev->pm.state, PANTHOR_DEVICE_PM_STATE_SUSPENDED);
  507. return 0;
  508. }