mali_kbase_mmu.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791
  1. /*
  2. *
  3. * (C) COPYRIGHT 2010-2016 ARM Limited. All rights reserved.
  4. *
  5. * This program is free software and is provided to you under the terms of the
  6. * GNU General Public License version 2 as published by the Free Software
  7. * Foundation, and any use by you of this program is subject to the terms
  8. * of such GNU licence.
  9. *
  10. * A copy of the licence is included with the program, and can also be obtained
  11. * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  12. * Boston, MA 02110-1301, USA.
  13. *
  14. */
  15. /**
  16. * @file mali_kbase_mmu.c
  17. * Base kernel MMU management.
  18. */
  19. /* #define DEBUG 1 */
  20. #include <linux/kernel.h>
  21. #include <linux/dma-mapping.h>
  22. #include <mali_kbase.h>
  23. #include <mali_midg_regmap.h>
  24. #include <mali_kbase_tlstream.h>
  25. #include <mali_kbase_debug.h>
  26. #define beenthere(kctx, f, a...) dev_dbg(kctx->kbdev->dev, "%s:" f, __func__, ##a)
  27. #include <mali_kbase_defs.h>
  28. #include <mali_kbase_hw.h>
  29. #include <mali_kbase_mmu_hw.h>
  30. #include <mali_kbase_hwaccess_jm.h>
  31. #define KBASE_MMU_PAGE_ENTRIES 512
  32. /**
  33. * kbase_mmu_sync_pgd - sync page directory to memory
  34. * @kbdev: Device pointer.
  35. * @handle: Address of DMA region.
  36. * @size: Size of the region to sync.
  37. *
  38. * This should be called after each page directory update.
  39. */
  40. static void kbase_mmu_sync_pgd(struct kbase_device *kbdev,
  41. dma_addr_t handle, size_t size)
  42. {
  43. /* If page table is not coherent then ensure the gpu can read
  44. * the pages from memory
  45. */
  46. if (kbdev->system_coherency != COHERENCY_ACE)
  47. dma_sync_single_for_device(kbdev->dev, handle, size,
  48. DMA_TO_DEVICE);
  49. }
  50. /*
  51. * Definitions:
  52. * - PGD: Page Directory.
  53. * - PTE: Page Table Entry. A 64bit value pointing to the next
  54. * level of translation
  55. * - ATE: Address Transation Entry. A 64bit value pointing to
  56. * a 4kB physical page.
  57. */
  58. static void kbase_mmu_report_fault_and_kill(struct kbase_context *kctx,
  59. struct kbase_as *as, const char *reason_str);
  60. static size_t make_multiple(size_t minimum, size_t multiple)
  61. {
  62. size_t remainder = minimum % multiple;
  63. if (remainder == 0)
  64. return minimum;
  65. return minimum + multiple - remainder;
  66. }
  67. void page_fault_worker(struct work_struct *data)
  68. {
  69. u64 fault_pfn;
  70. u32 fault_status;
  71. size_t new_pages;
  72. size_t fault_rel_pfn;
  73. struct kbase_as *faulting_as;
  74. int as_no;
  75. struct kbase_context *kctx;
  76. struct kbase_device *kbdev;
  77. struct kbase_va_region *region;
  78. int err;
  79. bool grown = false;
  80. faulting_as = container_of(data, struct kbase_as, work_pagefault);
  81. fault_pfn = faulting_as->fault_addr >> PAGE_SHIFT;
  82. as_no = faulting_as->number;
  83. kbdev = container_of(faulting_as, struct kbase_device, as[as_no]);
  84. /* Grab the context that was already refcounted in kbase_mmu_interrupt().
  85. * Therefore, it cannot be scheduled out of this AS until we explicitly release it
  86. */
  87. kctx = kbasep_js_runpool_lookup_ctx_noretain(kbdev, as_no);
  88. if (WARN_ON(!kctx)) {
  89. atomic_dec(&kbdev->faults_pending);
  90. return;
  91. }
  92. KBASE_DEBUG_ASSERT(kctx->kbdev == kbdev);
  93. fault_status = faulting_as->fault_status;
  94. switch (fault_status & AS_FAULTSTATUS_EXCEPTION_CODE_MASK) {
  95. case AS_FAULTSTATUS_EXCEPTION_CODE_TRANSLATION_FAULT:
  96. /* need to check against the region to handle this one */
  97. break;
  98. case AS_FAULTSTATUS_EXCEPTION_CODE_PERMISSION_FAULT:
  99. kbase_mmu_report_fault_and_kill(kctx, faulting_as,
  100. "Permission failure");
  101. goto fault_done;
  102. case AS_FAULTSTATUS_EXCEPTION_CODE_TRANSTAB_BUS_FAULT:
  103. kbase_mmu_report_fault_and_kill(kctx, faulting_as,
  104. "Tranlation table bus fault");
  105. goto fault_done;
  106. case AS_FAULTSTATUS_EXCEPTION_CODE_ACCESS_FLAG:
  107. /* nothing to do, but we don't expect this fault currently */
  108. dev_warn(kbdev->dev, "Access flag unexpectedly set");
  109. goto fault_done;
  110. #ifdef CONFIG_MALI_GPU_MMU_AARCH64
  111. case AS_FAULTSTATUS_EXCEPTION_CODE_ADDRESS_SIZE_FAULT:
  112. kbase_mmu_report_fault_and_kill(kctx, faulting_as,
  113. "Address size fault");
  114. goto fault_done;
  115. case AS_FAULTSTATUS_EXCEPTION_CODE_MEMORY_ATTRIBUTES_FAULT:
  116. kbase_mmu_report_fault_and_kill(kctx, faulting_as,
  117. "Memory attributes fault");
  118. goto fault_done;
  119. #endif // ifdef CONFIG_MALI_GPU_MMU_AARCH64
  120. default:
  121. kbase_mmu_report_fault_and_kill(kctx, faulting_as,
  122. "Unknown fault code");
  123. goto fault_done;
  124. }
  125. /* so we have a translation fault, let's see if it is for growable
  126. * memory */
  127. kbase_gpu_vm_lock(kctx);
  128. region = kbase_region_tracker_find_region_enclosing_address(kctx,
  129. faulting_as->fault_addr);
  130. if (!region || region->flags & KBASE_REG_FREE) {
  131. kbase_gpu_vm_unlock(kctx);
  132. kbase_mmu_report_fault_and_kill(kctx, faulting_as,
  133. "Memory is not mapped on the GPU");
  134. goto fault_done;
  135. }
  136. if ((region->flags & GROWABLE_FLAGS_REQUIRED)
  137. != GROWABLE_FLAGS_REQUIRED) {
  138. kbase_gpu_vm_unlock(kctx);
  139. kbase_mmu_report_fault_and_kill(kctx, faulting_as,
  140. "Memory is not growable");
  141. goto fault_done;
  142. }
  143. if ((region->flags & KBASE_REG_DONT_NEED)) {
  144. kbase_gpu_vm_unlock(kctx);
  145. kbase_mmu_report_fault_and_kill(kctx, faulting_as,
  146. "Don't need memory can't be grown");
  147. goto fault_done;
  148. }
  149. /* find the size we need to grow it by */
  150. /* we know the result fit in a size_t due to kbase_region_tracker_find_region_enclosing_address
  151. * validating the fault_adress to be within a size_t from the start_pfn */
  152. fault_rel_pfn = fault_pfn - region->start_pfn;
  153. if (fault_rel_pfn < kbase_reg_current_backed_size(region)) {
  154. dev_dbg(kbdev->dev, "Page fault @ 0x%llx in allocated region 0x%llx-0x%llx of growable TMEM: Ignoring",
  155. faulting_as->fault_addr, region->start_pfn,
  156. region->start_pfn +
  157. kbase_reg_current_backed_size(region));
  158. kbase_mmu_hw_clear_fault(kbdev, faulting_as, kctx,
  159. KBASE_MMU_FAULT_TYPE_PAGE);
  160. /* [1] in case another page fault occurred while we were
  161. * handling the (duplicate) page fault we need to ensure we
  162. * don't loose the other page fault as result of us clearing
  163. * the MMU IRQ. Therefore, after we clear the MMU IRQ we send
  164. * an UNLOCK command that will retry any stalled memory
  165. * transaction (which should cause the other page fault to be
  166. * raised again).
  167. */
  168. kbase_mmu_hw_do_operation(kbdev, faulting_as, NULL, 0, 0,
  169. AS_COMMAND_UNLOCK, 1);
  170. kbase_mmu_hw_enable_fault(kbdev, faulting_as, kctx,
  171. KBASE_MMU_FAULT_TYPE_PAGE);
  172. kbase_gpu_vm_unlock(kctx);
  173. goto fault_done;
  174. }
  175. new_pages = make_multiple(fault_rel_pfn -
  176. kbase_reg_current_backed_size(region) + 1,
  177. region->extent);
  178. /* cap to max vsize */
  179. if (new_pages + kbase_reg_current_backed_size(region) >
  180. region->nr_pages)
  181. new_pages = region->nr_pages -
  182. kbase_reg_current_backed_size(region);
  183. if (new_pages == 0) {
  184. /* Duplicate of a fault we've already handled, nothing to do */
  185. kbase_mmu_hw_clear_fault(kbdev, faulting_as, kctx,
  186. KBASE_MMU_FAULT_TYPE_PAGE);
  187. /* See comment [1] about UNLOCK usage */
  188. kbase_mmu_hw_do_operation(kbdev, faulting_as, NULL, 0, 0,
  189. AS_COMMAND_UNLOCK, 1);
  190. kbase_mmu_hw_enable_fault(kbdev, faulting_as, kctx,
  191. KBASE_MMU_FAULT_TYPE_PAGE);
  192. kbase_gpu_vm_unlock(kctx);
  193. goto fault_done;
  194. }
  195. if (kbase_alloc_phy_pages_helper(region->gpu_alloc, new_pages) == 0) {
  196. if (region->gpu_alloc != region->cpu_alloc) {
  197. if (kbase_alloc_phy_pages_helper(
  198. region->cpu_alloc, new_pages) == 0) {
  199. grown = true;
  200. } else {
  201. kbase_free_phy_pages_helper(region->gpu_alloc,
  202. new_pages);
  203. }
  204. } else {
  205. grown = true;
  206. }
  207. }
  208. if (grown) {
  209. u32 op;
  210. /* alloc success */
  211. KBASE_DEBUG_ASSERT(kbase_reg_current_backed_size(region) <= region->nr_pages);
  212. /* set up the new pages */
  213. err = kbase_mmu_insert_pages(kctx, region->start_pfn + kbase_reg_current_backed_size(region) - new_pages, &kbase_get_gpu_phy_pages(region)[kbase_reg_current_backed_size(region) - new_pages], new_pages, region->flags);
  214. if (err) {
  215. kbase_free_phy_pages_helper(region->gpu_alloc, new_pages);
  216. if (region->gpu_alloc != region->cpu_alloc)
  217. kbase_free_phy_pages_helper(region->cpu_alloc,
  218. new_pages);
  219. kbase_gpu_vm_unlock(kctx);
  220. /* The locked VA region will be unlocked and the cache invalidated in here */
  221. kbase_mmu_report_fault_and_kill(kctx, faulting_as,
  222. "Page table update failure");
  223. goto fault_done;
  224. }
  225. kbase_tlstream_aux_pagefault(kctx->id, (u64)new_pages);
  226. /* AS transaction begin */
  227. mutex_lock(&faulting_as->transaction_mutex);
  228. /* flush L2 and unlock the VA (resumes the MMU) */
  229. if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_6367))
  230. op = AS_COMMAND_FLUSH;
  231. else
  232. op = AS_COMMAND_FLUSH_PT;
  233. /* clear MMU interrupt - this needs to be done after updating
  234. * the page tables but before issuing a FLUSH command. The
  235. * FLUSH cmd has a side effect that it restarts stalled memory
  236. * transactions in other address spaces which may cause
  237. * another fault to occur. If we didn't clear the interrupt at
  238. * this stage a new IRQ might not be raised when the GPU finds
  239. * a MMU IRQ is already pending.
  240. */
  241. kbase_mmu_hw_clear_fault(kbdev, faulting_as, kctx,
  242. KBASE_MMU_FAULT_TYPE_PAGE);
  243. kbase_mmu_hw_do_operation(kbdev, faulting_as, kctx,
  244. faulting_as->fault_addr >> PAGE_SHIFT,
  245. new_pages,
  246. op, 1);
  247. mutex_unlock(&faulting_as->transaction_mutex);
  248. /* AS transaction end */
  249. /* reenable this in the mask */
  250. kbase_mmu_hw_enable_fault(kbdev, faulting_as, kctx,
  251. KBASE_MMU_FAULT_TYPE_PAGE);
  252. kbase_gpu_vm_unlock(kctx);
  253. } else {
  254. /* failed to extend, handle as a normal PF */
  255. kbase_gpu_vm_unlock(kctx);
  256. kbase_mmu_report_fault_and_kill(kctx, faulting_as,
  257. "Page allocation failure");
  258. }
  259. fault_done:
  260. /*
  261. * By this point, the fault was handled in some way,
  262. * so release the ctx refcount
  263. */
  264. kbasep_js_runpool_release_ctx(kbdev, kctx);
  265. atomic_dec(&kbdev->faults_pending);
  266. }
  267. phys_addr_t kbase_mmu_alloc_pgd(struct kbase_context *kctx)
  268. {
  269. u64 *page;
  270. int i;
  271. struct page *p;
  272. int new_page_count __maybe_unused;
  273. KBASE_DEBUG_ASSERT(kctx != NULL);
  274. new_page_count = kbase_atomic_add_pages(1, &kctx->used_pages);
  275. kbase_atomic_add_pages(1, &kctx->kbdev->memdev.used_pages);
  276. p = kbase_mem_pool_alloc(&kctx->mem_pool);
  277. if (!p)
  278. goto sub_pages;
  279. kbase_tlstream_aux_pagesalloc(
  280. (u32)kctx->id,
  281. (u64)new_page_count);
  282. page = kmap(p);
  283. if (page == NULL)
  284. goto alloc_free;
  285. kbase_process_page_usage_inc(kctx, 1);
  286. for (i = 0; i < KBASE_MMU_PAGE_ENTRIES; i++)
  287. kctx->kbdev->mmu_mode->entry_invalidate(&page[i]);
  288. kbase_mmu_sync_pgd(kctx->kbdev, kbase_dma_addr(p), PAGE_SIZE);
  289. kunmap(p);
  290. return page_to_phys(p);
  291. alloc_free:
  292. kbase_mem_pool_free(&kctx->mem_pool, p, false);
  293. sub_pages:
  294. kbase_atomic_sub_pages(1, &kctx->used_pages);
  295. kbase_atomic_sub_pages(1, &kctx->kbdev->memdev.used_pages);
  296. return 0;
  297. }
  298. /* Given PGD PFN for level N, return PGD PFN for level N+1 */
  299. static phys_addr_t mmu_get_next_pgd(struct kbase_context *kctx, phys_addr_t pgd, u64 vpfn, int level)
  300. {
  301. u64 *page;
  302. phys_addr_t target_pgd;
  303. struct page *p;
  304. KBASE_DEBUG_ASSERT(pgd);
  305. KBASE_DEBUG_ASSERT(kctx != NULL);
  306. lockdep_assert_held(&kctx->mmu_lock);
  307. /*
  308. * Architecture spec defines level-0 as being the top-most.
  309. * This is a bit unfortunate here, but we keep the same convention.
  310. */
  311. vpfn >>= (3 - level) * 9;
  312. vpfn &= 0x1FF;
  313. p = pfn_to_page(PFN_DOWN(pgd));
  314. page = kmap(p);
  315. if (page == NULL) {
  316. dev_warn(kctx->kbdev->dev, "mmu_get_next_pgd: kmap failure\n");
  317. return 0;
  318. }
  319. target_pgd = kctx->kbdev->mmu_mode->pte_to_phy_addr(page[vpfn]);
  320. if (!target_pgd) {
  321. target_pgd = kbase_mmu_alloc_pgd(kctx);
  322. if (!target_pgd) {
  323. dev_warn(kctx->kbdev->dev, "mmu_get_next_pgd: kbase_mmu_alloc_pgd failure\n");
  324. kunmap(p);
  325. return 0;
  326. }
  327. kctx->kbdev->mmu_mode->entry_set_pte(&page[vpfn], target_pgd);
  328. kbase_mmu_sync_pgd(kctx->kbdev, kbase_dma_addr(p), PAGE_SIZE);
  329. /* Rely on the caller to update the address space flags. */
  330. }
  331. kunmap(p);
  332. return target_pgd;
  333. }
  334. static phys_addr_t mmu_get_bottom_pgd(struct kbase_context *kctx, u64 vpfn)
  335. {
  336. phys_addr_t pgd;
  337. int l;
  338. lockdep_assert_held(&kctx->mmu_lock);
  339. pgd = kctx->pgd;
  340. for (l = MIDGARD_MMU_TOPLEVEL; l < MIDGARD_MMU_BOTTOMLEVEL; l++) {
  341. pgd = mmu_get_next_pgd(kctx, pgd, vpfn, l);
  342. /* Handle failure condition */
  343. if (!pgd) {
  344. dev_warn(kctx->kbdev->dev, "mmu_get_bottom_pgd: mmu_get_next_pgd failure\n");
  345. return 0;
  346. }
  347. }
  348. return pgd;
  349. }
  350. static phys_addr_t mmu_insert_pages_recover_get_next_pgd(struct kbase_context *kctx, phys_addr_t pgd, u64 vpfn, int level)
  351. {
  352. u64 *page;
  353. phys_addr_t target_pgd;
  354. KBASE_DEBUG_ASSERT(pgd);
  355. KBASE_DEBUG_ASSERT(kctx != NULL);
  356. lockdep_assert_held(&kctx->mmu_lock);
  357. /*
  358. * Architecture spec defines level-0 as being the top-most.
  359. * This is a bit unfortunate here, but we keep the same convention.
  360. */
  361. vpfn >>= (3 - level) * 9;
  362. vpfn &= 0x1FF;
  363. page = kmap_atomic(pfn_to_page(PFN_DOWN(pgd)));
  364. /* kmap_atomic should NEVER fail */
  365. KBASE_DEBUG_ASSERT(page != NULL);
  366. target_pgd = kctx->kbdev->mmu_mode->pte_to_phy_addr(page[vpfn]);
  367. /* As we are recovering from what has already been set up, we should have a target_pgd */
  368. KBASE_DEBUG_ASSERT(target_pgd != 0);
  369. kunmap_atomic(page);
  370. return target_pgd;
  371. }
  372. static phys_addr_t mmu_insert_pages_recover_get_bottom_pgd(struct kbase_context *kctx, u64 vpfn)
  373. {
  374. phys_addr_t pgd;
  375. int l;
  376. lockdep_assert_held(&kctx->mmu_lock);
  377. pgd = kctx->pgd;
  378. for (l = MIDGARD_MMU_TOPLEVEL; l < MIDGARD_MMU_BOTTOMLEVEL; l++) {
  379. pgd = mmu_insert_pages_recover_get_next_pgd(kctx, pgd, vpfn, l);
  380. /* Should never fail */
  381. KBASE_DEBUG_ASSERT(pgd != 0);
  382. }
  383. return pgd;
  384. }
  385. static void mmu_insert_pages_failure_recovery(struct kbase_context *kctx, u64 vpfn,
  386. size_t nr)
  387. {
  388. phys_addr_t pgd;
  389. u64 *pgd_page;
  390. struct kbase_mmu_mode const *mmu_mode;
  391. KBASE_DEBUG_ASSERT(kctx != NULL);
  392. KBASE_DEBUG_ASSERT(vpfn != 0);
  393. /* 64-bit address range is the max */
  394. KBASE_DEBUG_ASSERT(vpfn <= (U64_MAX / PAGE_SIZE));
  395. lockdep_assert_held(&kctx->mmu_lock);
  396. mmu_mode = kctx->kbdev->mmu_mode;
  397. while (nr) {
  398. unsigned int i;
  399. unsigned int index = vpfn & 0x1FF;
  400. unsigned int count = KBASE_MMU_PAGE_ENTRIES - index;
  401. struct page *p;
  402. if (count > nr)
  403. count = nr;
  404. pgd = mmu_insert_pages_recover_get_bottom_pgd(kctx, vpfn);
  405. KBASE_DEBUG_ASSERT(pgd != 0);
  406. p = pfn_to_page(PFN_DOWN(pgd));
  407. pgd_page = kmap_atomic(p);
  408. KBASE_DEBUG_ASSERT(pgd_page != NULL);
  409. /* Invalidate the entries we added */
  410. for (i = 0; i < count; i++)
  411. mmu_mode->entry_invalidate(&pgd_page[index + i]);
  412. vpfn += count;
  413. nr -= count;
  414. kbase_mmu_sync_pgd(kctx->kbdev, kbase_dma_addr(p), PAGE_SIZE);
  415. kunmap_atomic(pgd_page);
  416. }
  417. }
  418. /*
  419. * Map the single page 'phys' 'nr' of times, starting at GPU PFN 'vpfn'
  420. */
  421. int kbase_mmu_insert_single_page(struct kbase_context *kctx, u64 vpfn,
  422. phys_addr_t phys, size_t nr,
  423. unsigned long flags)
  424. {
  425. phys_addr_t pgd;
  426. u64 *pgd_page;
  427. /* In case the insert_single_page only partially completes we need to be
  428. * able to recover */
  429. bool recover_required = false;
  430. u64 recover_vpfn = vpfn;
  431. size_t recover_count = 0;
  432. int err;
  433. KBASE_DEBUG_ASSERT(kctx != NULL);
  434. KBASE_DEBUG_ASSERT(vpfn != 0);
  435. /* 64-bit address range is the max */
  436. KBASE_DEBUG_ASSERT(vpfn <= (U64_MAX / PAGE_SIZE));
  437. mutex_lock(&kctx->mmu_lock);
  438. while (nr) {
  439. unsigned int i;
  440. unsigned int index = vpfn & 0x1FF;
  441. unsigned int count = KBASE_MMU_PAGE_ENTRIES - index;
  442. struct page *p;
  443. if (count > nr)
  444. count = nr;
  445. /*
  446. * Repeatedly calling mmu_get_bottom_pte() is clearly
  447. * suboptimal. We don't have to re-parse the whole tree
  448. * each time (just cache the l0-l2 sequence).
  449. * On the other hand, it's only a gain when we map more than
  450. * 256 pages at once (on average). Do we really care?
  451. */
  452. pgd = mmu_get_bottom_pgd(kctx, vpfn);
  453. if (!pgd) {
  454. dev_warn(kctx->kbdev->dev, "kbase_mmu_insert_pages: mmu_get_bottom_pgd failure\n");
  455. if (recover_required) {
  456. /* Invalidate the pages we have partially
  457. * completed */
  458. mmu_insert_pages_failure_recovery(kctx,
  459. recover_vpfn,
  460. recover_count);
  461. }
  462. err = -EINVAL;
  463. goto fail_unlock;
  464. }
  465. p = pfn_to_page(PFN_DOWN(pgd));
  466. pgd_page = kmap(p);
  467. if (!pgd_page) {
  468. dev_warn(kctx->kbdev->dev, "kbase_mmu_insert_pages: kmap failure\n");
  469. if (recover_required) {
  470. /* Invalidate the pages we have partially
  471. * completed */
  472. mmu_insert_pages_failure_recovery(kctx,
  473. recover_vpfn,
  474. recover_count);
  475. }
  476. err = -ENOMEM;
  477. goto fail_unlock;
  478. }
  479. for (i = 0; i < count; i++) {
  480. unsigned int ofs = index + i;
  481. KBASE_DEBUG_ASSERT(0 == (pgd_page[ofs] & 1UL));
  482. kctx->kbdev->mmu_mode->entry_set_ate(&pgd_page[ofs],
  483. phys, flags);
  484. }
  485. vpfn += count;
  486. nr -= count;
  487. kbase_mmu_sync_pgd(kctx->kbdev,
  488. kbase_dma_addr(p) + (index * sizeof(u64)),
  489. count * sizeof(u64));
  490. kunmap(p);
  491. /* We have started modifying the page table.
  492. * If further pages need inserting and fail we need to undo what
  493. * has already taken place */
  494. recover_required = true;
  495. recover_count += count;
  496. }
  497. mutex_unlock(&kctx->mmu_lock);
  498. return 0;
  499. fail_unlock:
  500. mutex_unlock(&kctx->mmu_lock);
  501. return err;
  502. }
  503. /*
  504. * Map 'nr' pages pointed to by 'phys' at GPU PFN 'vpfn'
  505. */
  506. int kbase_mmu_insert_pages(struct kbase_context *kctx, u64 vpfn,
  507. phys_addr_t *phys, size_t nr,
  508. unsigned long flags)
  509. {
  510. phys_addr_t pgd;
  511. u64 *pgd_page;
  512. /* In case the insert_pages only partially completes we need to be able
  513. * to recover */
  514. bool recover_required = false;
  515. u64 recover_vpfn = vpfn;
  516. size_t recover_count = 0;
  517. int err;
  518. KBASE_DEBUG_ASSERT(kctx != NULL);
  519. KBASE_DEBUG_ASSERT(vpfn != 0);
  520. /* 64-bit address range is the max */
  521. KBASE_DEBUG_ASSERT(vpfn <= (U64_MAX / PAGE_SIZE));
  522. mutex_lock(&kctx->mmu_lock);
  523. while (nr) {
  524. unsigned int i;
  525. unsigned int index = vpfn & 0x1FF;
  526. unsigned int count = KBASE_MMU_PAGE_ENTRIES - index;
  527. struct page *p;
  528. if (count > nr)
  529. count = nr;
  530. /*
  531. * Repeatedly calling mmu_get_bottom_pte() is clearly
  532. * suboptimal. We don't have to re-parse the whole tree
  533. * each time (just cache the l0-l2 sequence).
  534. * On the other hand, it's only a gain when we map more than
  535. * 256 pages at once (on average). Do we really care?
  536. */
  537. pgd = mmu_get_bottom_pgd(kctx, vpfn);
  538. if (!pgd) {
  539. dev_warn(kctx->kbdev->dev, "kbase_mmu_insert_pages: mmu_get_bottom_pgd failure\n");
  540. if (recover_required) {
  541. /* Invalidate the pages we have partially
  542. * completed */
  543. mmu_insert_pages_failure_recovery(kctx,
  544. recover_vpfn,
  545. recover_count);
  546. }
  547. err = -EINVAL;
  548. goto fail_unlock;
  549. }
  550. p = pfn_to_page(PFN_DOWN(pgd));
  551. pgd_page = kmap(p);
  552. if (!pgd_page) {
  553. dev_warn(kctx->kbdev->dev, "kbase_mmu_insert_pages: kmap failure\n");
  554. if (recover_required) {
  555. /* Invalidate the pages we have partially
  556. * completed */
  557. mmu_insert_pages_failure_recovery(kctx,
  558. recover_vpfn,
  559. recover_count);
  560. }
  561. err = -ENOMEM;
  562. goto fail_unlock;
  563. }
  564. for (i = 0; i < count; i++) {
  565. unsigned int ofs = index + i;
  566. KBASE_DEBUG_ASSERT(0 == (pgd_page[ofs] & 1UL));
  567. kctx->kbdev->mmu_mode->entry_set_ate(&pgd_page[ofs],
  568. phys[i], flags);
  569. }
  570. phys += count;
  571. vpfn += count;
  572. nr -= count;
  573. kbase_mmu_sync_pgd(kctx->kbdev,
  574. kbase_dma_addr(p) + (index * sizeof(u64)),
  575. count * sizeof(u64));
  576. kunmap(p);
  577. /* We have started modifying the page table. If further pages
  578. * need inserting and fail we need to undo what has already
  579. * taken place */
  580. recover_required = true;
  581. recover_count += count;
  582. }
  583. mutex_unlock(&kctx->mmu_lock);
  584. return 0;
  585. fail_unlock:
  586. mutex_unlock(&kctx->mmu_lock);
  587. return err;
  588. }
  589. /**
  590. * This function is responsible for validating the MMU PTs
  591. * triggering reguired flushes.
  592. *
  593. * * IMPORTANT: This uses kbasep_js_runpool_release_ctx() when the context is
  594. * currently scheduled into the runpool, and so potentially uses a lot of locks.
  595. * These locks must be taken in the correct order with respect to others
  596. * already held by the caller. Refer to kbasep_js_runpool_release_ctx() for more
  597. * information.
  598. */
  599. static void kbase_mmu_flush(struct kbase_context *kctx, u64 vpfn, size_t nr)
  600. {
  601. struct kbase_device *kbdev;
  602. bool ctx_is_in_runpool;
  603. KBASE_DEBUG_ASSERT(kctx != NULL);
  604. kbdev = kctx->kbdev;
  605. /* We must flush if we're currently running jobs. At the very least, we need to retain the
  606. * context to ensure it doesn't schedule out whilst we're trying to flush it */
  607. ctx_is_in_runpool = kbasep_js_runpool_retain_ctx(kbdev, kctx);
  608. if (ctx_is_in_runpool) {
  609. KBASE_DEBUG_ASSERT(kctx->as_nr != KBASEP_AS_NR_INVALID);
  610. /* Second level check is to try to only do this when jobs are running. The refcount is
  611. * a heuristic for this. */
  612. if (kbdev->js_data.runpool_irq.per_as_data[kctx->as_nr].as_busy_refcount >= 2) {
  613. if (!kbase_pm_context_active_handle_suspend(kbdev,
  614. KBASE_PM_SUSPEND_HANDLER_DONT_REACTIVATE)) {
  615. int ret;
  616. u32 op;
  617. /* AS transaction begin */
  618. mutex_lock(&kbdev->as[
  619. kctx->as_nr].transaction_mutex);
  620. if (kbase_hw_has_issue(kbdev,
  621. BASE_HW_ISSUE_6367))
  622. op = AS_COMMAND_FLUSH;
  623. else
  624. op = AS_COMMAND_FLUSH_MEM;
  625. ret = kbase_mmu_hw_do_operation(kbdev,
  626. &kbdev->as[kctx->as_nr],
  627. kctx, vpfn, nr,
  628. op, 0);
  629. #if KBASE_GPU_RESET_EN
  630. if (ret) {
  631. /* Flush failed to complete, assume the
  632. * GPU has hung and perform a reset to
  633. * recover */
  634. dev_err(kbdev->dev, "Flush for GPU page table update did not complete. Issueing GPU soft-reset to recover\n");
  635. if (kbase_prepare_to_reset_gpu(kbdev))
  636. kbase_reset_gpu(kbdev);
  637. }
  638. #endif // KBASE_GPU_RESET_EN
  639. mutex_unlock(&kbdev->as[
  640. kctx->as_nr].transaction_mutex);
  641. /* AS transaction end */
  642. kbase_pm_context_idle(kbdev);
  643. }
  644. }
  645. kbasep_js_runpool_release_ctx(kbdev, kctx);
  646. }
  647. }
  648. /*
  649. * We actually only discard the ATE, and not the page table
  650. * pages. There is a potential DoS here, as we'll leak memory by
  651. * having PTEs that are potentially unused. Will require physical
  652. * page accounting, so MMU pages are part of the process allocation.
  653. *
  654. * IMPORTANT: This uses kbasep_js_runpool_release_ctx() when the context is
  655. * currently scheduled into the runpool, and so potentially uses a lot of locks.
  656. * These locks must be taken in the correct order with respect to others
  657. * already held by the caller. Refer to kbasep_js_runpool_release_ctx() for more
  658. * information.
  659. */
  660. int kbase_mmu_teardown_pages(struct kbase_context *kctx, u64 vpfn, size_t nr)
  661. {
  662. phys_addr_t pgd;
  663. u64 *pgd_page;
  664. struct kbase_device *kbdev;
  665. size_t requested_nr = nr;
  666. struct kbase_mmu_mode const *mmu_mode;
  667. int err;
  668. KBASE_DEBUG_ASSERT(kctx != NULL);
  669. beenthere(kctx, "kctx %p vpfn %lx nr %zd", (void *)kctx, (unsigned long)vpfn, nr);
  670. if (nr == 0) {
  671. /* early out if nothing to do */
  672. return 0;
  673. }
  674. mutex_lock(&kctx->mmu_lock);
  675. kbdev = kctx->kbdev;
  676. mmu_mode = kbdev->mmu_mode;
  677. while (nr) {
  678. unsigned int i;
  679. unsigned int index = vpfn & 0x1FF;
  680. unsigned int count = KBASE_MMU_PAGE_ENTRIES - index;
  681. struct page *p;
  682. if (count > nr)
  683. count = nr;
  684. pgd = mmu_get_bottom_pgd(kctx, vpfn);
  685. if (!pgd) {
  686. dev_warn(kbdev->dev, "kbase_mmu_teardown_pages: mmu_get_bottom_pgd failure\n");
  687. err = -EINVAL;
  688. goto fail_unlock;
  689. }
  690. p = pfn_to_page(PFN_DOWN(pgd));
  691. pgd_page = kmap(p);
  692. if (!pgd_page) {
  693. dev_warn(kbdev->dev, "kbase_mmu_teardown_pages: kmap failure\n");
  694. err = -ENOMEM;
  695. goto fail_unlock;
  696. }
  697. for (i = 0; i < count; i++)
  698. mmu_mode->entry_invalidate(&pgd_page[index + i]);
  699. vpfn += count;
  700. nr -= count;
  701. kbase_mmu_sync_pgd(kctx->kbdev,
  702. kbase_dma_addr(p) + (index * sizeof(u64)),
  703. count * sizeof(u64));
  704. kunmap(p);
  705. }
  706. mutex_unlock(&kctx->mmu_lock);
  707. kbase_mmu_flush(kctx, vpfn, requested_nr);
  708. return 0;
  709. fail_unlock:
  710. mutex_unlock(&kctx->mmu_lock);
  711. return err;
  712. }
  713. /**
  714. * Update the entries for specified number of pages pointed to by 'phys' at GPU PFN 'vpfn'.
  715. * This call is being triggered as a response to the changes of the mem attributes
  716. *
  717. * @pre : The caller is responsible for validating the memory attributes
  718. *
  719. * IMPORTANT: This uses kbasep_js_runpool_release_ctx() when the context is
  720. * currently scheduled into the runpool, and so potentially uses a lot of locks.
  721. * These locks must be taken in the correct order with respect to others
  722. * already held by the caller. Refer to kbasep_js_runpool_release_ctx() for more
  723. * information.
  724. */
  725. int kbase_mmu_update_pages(struct kbase_context *kctx, u64 vpfn, phys_addr_t *phys, size_t nr, unsigned long flags)
  726. {
  727. phys_addr_t pgd;
  728. u64 *pgd_page;
  729. size_t requested_nr = nr;
  730. struct kbase_mmu_mode const *mmu_mode;
  731. int err;
  732. KBASE_DEBUG_ASSERT(kctx != NULL);
  733. KBASE_DEBUG_ASSERT(vpfn != 0);
  734. KBASE_DEBUG_ASSERT(vpfn <= (U64_MAX / PAGE_SIZE));
  735. mutex_lock(&kctx->mmu_lock);
  736. mmu_mode = kctx->kbdev->mmu_mode;
  737. dev_warn(kctx->kbdev->dev, "kbase_mmu_update_pages(): updating page share flags on GPU PFN 0x%llx from phys %p, %zu pages",
  738. vpfn, phys, nr);
  739. while (nr) {
  740. unsigned int i;
  741. unsigned int index = vpfn & 0x1FF;
  742. size_t count = KBASE_MMU_PAGE_ENTRIES - index;
  743. struct page *p;
  744. if (count > nr)
  745. count = nr;
  746. pgd = mmu_get_bottom_pgd(kctx, vpfn);
  747. if (!pgd) {
  748. dev_warn(kctx->kbdev->dev, "mmu_get_bottom_pgd failure\n");
  749. err = -EINVAL;
  750. goto fail_unlock;
  751. }
  752. p = pfn_to_page(PFN_DOWN(pgd));
  753. pgd_page = kmap(p);
  754. if (!pgd_page) {
  755. dev_warn(kctx->kbdev->dev, "kmap failure\n");
  756. err = -ENOMEM;
  757. goto fail_unlock;
  758. }
  759. for (i = 0; i < count; i++)
  760. mmu_mode->entry_set_ate(&pgd_page[index + i], phys[i],
  761. flags);
  762. phys += count;
  763. vpfn += count;
  764. nr -= count;
  765. kbase_mmu_sync_pgd(kctx->kbdev,
  766. kbase_dma_addr(p) + (index * sizeof(u64)),
  767. count * sizeof(u64));
  768. kunmap(pfn_to_page(PFN_DOWN(pgd)));
  769. }
  770. mutex_unlock(&kctx->mmu_lock);
  771. kbase_mmu_flush(kctx, vpfn, requested_nr);
  772. return 0;
  773. fail_unlock:
  774. mutex_unlock(&kctx->mmu_lock);
  775. return err;
  776. }
  777. /* This is a debug feature only */
  778. static void mmu_check_unused(struct kbase_context *kctx, phys_addr_t pgd)
  779. {
  780. u64 *page;
  781. int i;
  782. page = kmap_atomic(pfn_to_page(PFN_DOWN(pgd)));
  783. /* kmap_atomic should NEVER fail. */
  784. KBASE_DEBUG_ASSERT(page != NULL);
  785. for (i = 0; i < KBASE_MMU_PAGE_ENTRIES; i++) {
  786. if (kctx->kbdev->mmu_mode->ate_is_valid(page[i]))
  787. beenthere(kctx, "live pte %016lx", (unsigned long)page[i]);
  788. }
  789. kunmap_atomic(page);
  790. }
  791. static void mmu_teardown_level(struct kbase_context *kctx, phys_addr_t pgd, int level, int zap, u64 *pgd_page_buffer)
  792. {
  793. phys_addr_t target_pgd;
  794. u64 *pgd_page;
  795. int i;
  796. struct kbase_mmu_mode const *mmu_mode;
  797. KBASE_DEBUG_ASSERT(kctx != NULL);
  798. lockdep_assert_held(&kctx->mmu_lock);
  799. pgd_page = kmap_atomic(pfn_to_page(PFN_DOWN(pgd)));
  800. /* kmap_atomic should NEVER fail. */
  801. KBASE_DEBUG_ASSERT(pgd_page != NULL);
  802. /* Copy the page to our preallocated buffer so that we can minimize kmap_atomic usage */
  803. memcpy(pgd_page_buffer, pgd_page, PAGE_SIZE);
  804. kunmap_atomic(pgd_page);
  805. pgd_page = pgd_page_buffer;
  806. mmu_mode = kctx->kbdev->mmu_mode;
  807. for (i = 0; i < KBASE_MMU_PAGE_ENTRIES; i++) {
  808. target_pgd = mmu_mode->pte_to_phy_addr(pgd_page[i]);
  809. if (target_pgd) {
  810. if (level < (MIDGARD_MMU_BOTTOMLEVEL - 1)) {
  811. mmu_teardown_level(kctx, target_pgd, level + 1, zap, pgd_page_buffer + (PAGE_SIZE / sizeof(u64)));
  812. } else {
  813. /*
  814. * So target_pte is a level-3 page.
  815. * As a leaf, it is safe to free it.
  816. * Unless we have live pages attached to it!
  817. */
  818. mmu_check_unused(kctx, target_pgd);
  819. }
  820. beenthere(kctx, "pte %lx level %d", (unsigned long)target_pgd, level + 1);
  821. if (zap) {
  822. struct page *p = phys_to_page(target_pgd);
  823. kbase_mem_pool_free(&kctx->mem_pool, p, true);
  824. kbase_process_page_usage_dec(kctx, 1);
  825. kbase_atomic_sub_pages(1, &kctx->used_pages);
  826. kbase_atomic_sub_pages(1, &kctx->kbdev->memdev.used_pages);
  827. }
  828. }
  829. }
  830. }
  831. int kbase_mmu_init(struct kbase_context *kctx)
  832. {
  833. KBASE_DEBUG_ASSERT(kctx != NULL);
  834. KBASE_DEBUG_ASSERT(kctx->mmu_teardown_pages == NULL);
  835. mutex_init(&kctx->mmu_lock);
  836. /* Preallocate MMU depth of four pages for mmu_teardown_level to use */
  837. kctx->mmu_teardown_pages = kmalloc(PAGE_SIZE * 4, GFP_KERNEL);
  838. if (kctx->mmu_teardown_pages == NULL)
  839. return -ENOMEM;
  840. return 0;
  841. }
  842. void kbase_mmu_term(struct kbase_context *kctx)
  843. {
  844. KBASE_DEBUG_ASSERT(kctx != NULL);
  845. KBASE_DEBUG_ASSERT(kctx->mmu_teardown_pages != NULL);
  846. kfree(kctx->mmu_teardown_pages);
  847. kctx->mmu_teardown_pages = NULL;
  848. }
  849. void kbase_mmu_free_pgd(struct kbase_context *kctx)
  850. {
  851. int new_page_count __maybe_unused;
  852. KBASE_DEBUG_ASSERT(kctx != NULL);
  853. KBASE_DEBUG_ASSERT(kctx->mmu_teardown_pages != NULL);
  854. mutex_lock(&kctx->mmu_lock);
  855. mmu_teardown_level(kctx, kctx->pgd, MIDGARD_MMU_TOPLEVEL, 1, kctx->mmu_teardown_pages);
  856. mutex_unlock(&kctx->mmu_lock);
  857. beenthere(kctx, "pgd %lx", (unsigned long)kctx->pgd);
  858. kbase_mem_pool_free(&kctx->mem_pool, phys_to_page(kctx->pgd), true);
  859. kbase_process_page_usage_dec(kctx, 1);
  860. new_page_count = kbase_atomic_sub_pages(1, &kctx->used_pages);
  861. kbase_atomic_sub_pages(1, &kctx->kbdev->memdev.used_pages);
  862. kbase_tlstream_aux_pagesalloc(
  863. (u32)kctx->id,
  864. (u64)new_page_count);
  865. }
  866. static size_t kbasep_mmu_dump_level(struct kbase_context *kctx, phys_addr_t pgd, int level, char ** const buffer, size_t *size_left)
  867. {
  868. phys_addr_t target_pgd;
  869. u64 *pgd_page;
  870. int i;
  871. size_t size = KBASE_MMU_PAGE_ENTRIES * sizeof(u64) + sizeof(u64);
  872. size_t dump_size;
  873. struct kbase_mmu_mode const *mmu_mode;
  874. KBASE_DEBUG_ASSERT(kctx != NULL);
  875. lockdep_assert_held(&kctx->mmu_lock);
  876. mmu_mode = kctx->kbdev->mmu_mode;
  877. pgd_page = kmap(pfn_to_page(PFN_DOWN(pgd)));
  878. if (!pgd_page) {
  879. dev_warn(kctx->kbdev->dev, "kbasep_mmu_dump_level: kmap failure\n");
  880. return 0;
  881. }
  882. if (*size_left >= size) {
  883. /* A modified physical address that contains the page table level */
  884. u64 m_pgd = pgd | level;
  885. /* Put the modified physical address in the output buffer */
  886. memcpy(*buffer, &m_pgd, sizeof(m_pgd));
  887. *buffer += sizeof(m_pgd);
  888. /* Followed by the page table itself */
  889. memcpy(*buffer, pgd_page, sizeof(u64) * KBASE_MMU_PAGE_ENTRIES);
  890. *buffer += sizeof(u64) * KBASE_MMU_PAGE_ENTRIES;
  891. *size_left -= size;
  892. }
  893. if (level < MIDGARD_MMU_BOTTOMLEVEL) {
  894. for (i = 0; i < KBASE_MMU_PAGE_ENTRIES; i++) {
  895. if (mmu_mode->pte_is_valid(pgd_page[i])) {
  896. target_pgd = mmu_mode->pte_to_phy_addr(
  897. pgd_page[i]);
  898. dump_size = kbasep_mmu_dump_level(kctx,
  899. target_pgd, level + 1,
  900. buffer, size_left);
  901. if (!dump_size) {
  902. kunmap(pfn_to_page(PFN_DOWN(pgd)));
  903. return 0;
  904. }
  905. size += dump_size;
  906. }
  907. }
  908. }
  909. kunmap(pfn_to_page(PFN_DOWN(pgd)));
  910. return size;
  911. }
  912. void *kbase_mmu_dump(struct kbase_context *kctx, int nr_pages)
  913. {
  914. void *kaddr;
  915. size_t size_left;
  916. KBASE_DEBUG_ASSERT(kctx);
  917. if (nr_pages == 0) {
  918. /* can't dump in a 0 sized buffer, early out */
  919. return NULL;
  920. }
  921. mutex_lock(&kctx->mmu_lock);
  922. size_left = nr_pages * PAGE_SIZE;
  923. KBASE_DEBUG_ASSERT(size_left != 0);
  924. kaddr = vmalloc_user(size_left);
  925. if (kaddr) {
  926. u64 end_marker = 0xFFULL;
  927. char *buffer;
  928. char *mmu_dump_buffer;
  929. u64 config[3];
  930. size_t size;
  931. buffer = (char *)kaddr;
  932. mmu_dump_buffer = buffer;
  933. if (kctx->api_version >= KBASE_API_VERSION(8, 4)) {
  934. struct kbase_mmu_setup as_setup;
  935. kctx->kbdev->mmu_mode->get_as_setup(kctx, &as_setup);
  936. config[0] = as_setup.transtab;
  937. config[1] = as_setup.memattr;
  938. config[2] = as_setup.transcfg;
  939. memcpy(buffer, &config, sizeof(config));
  940. mmu_dump_buffer += sizeof(config);
  941. size_left -= sizeof(config);
  942. }
  943. size = kbasep_mmu_dump_level(kctx,
  944. kctx->pgd,
  945. MIDGARD_MMU_TOPLEVEL,
  946. &mmu_dump_buffer,
  947. &size_left);
  948. if (!size)
  949. goto fail_free;
  950. /* Add on the size for the end marker */
  951. size += sizeof(u64);
  952. /* Add on the size for the config */
  953. if (kctx->api_version >= KBASE_API_VERSION(8, 4))
  954. size += sizeof(config);
  955. if (size > nr_pages * PAGE_SIZE || size_left < sizeof(u64)) {
  956. /* The buffer isn't big enough - free the memory and return failure */
  957. goto fail_free;
  958. }
  959. /* Add the end marker */
  960. memcpy(mmu_dump_buffer, &end_marker, sizeof(u64));
  961. }
  962. mutex_unlock(&kctx->mmu_lock);
  963. return kaddr;
  964. fail_free:
  965. vfree(kaddr);
  966. mutex_unlock(&kctx->mmu_lock);
  967. return NULL;
  968. }
  969. void bus_fault_worker(struct work_struct *data)
  970. {
  971. struct kbase_as *faulting_as;
  972. int as_no;
  973. struct kbase_context *kctx;
  974. struct kbase_device *kbdev;
  975. #if KBASE_GPU_RESET_EN
  976. bool reset_status = false;
  977. #endif
  978. faulting_as = container_of(data, struct kbase_as, work_busfault);
  979. as_no = faulting_as->number;
  980. kbdev = container_of(faulting_as, struct kbase_device, as[as_no]);
  981. /* Grab the context that was already refcounted in kbase_mmu_interrupt().
  982. * Therefore, it cannot be scheduled out of this AS until we explicitly release it
  983. */
  984. kctx = kbasep_js_runpool_lookup_ctx_noretain(kbdev, as_no);
  985. if (WARN_ON(!kctx)) {
  986. atomic_dec(&kbdev->faults_pending);
  987. return;
  988. }
  989. #if KBASE_GPU_RESET_EN
  990. if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8245)) {
  991. /* Due to H/W issue 8245 we need to reset the GPU after using UNMAPPED mode.
  992. * We start the reset before switching to UNMAPPED to ensure that unrelated jobs
  993. * are evicted from the GPU before the switch.
  994. */
  995. dev_err(kbdev->dev, "GPU bus error occurred. For this GPU version we now soft-reset as part of bus error recovery\n");
  996. reset_status = kbase_prepare_to_reset_gpu(kbdev);
  997. }
  998. #endif // KBASE_GPU_RESET_EN
  999. /* NOTE: If GPU already powered off for suspend, we don't need to switch to unmapped */
  1000. if (!kbase_pm_context_active_handle_suspend(kbdev, KBASE_PM_SUSPEND_HANDLER_DONT_REACTIVATE)) {
  1001. /* switch to UNMAPPED mode, will abort all jobs and stop any hw counter dumping */
  1002. /* AS transaction begin */
  1003. mutex_lock(&kbdev->as[as_no].transaction_mutex);
  1004. /* Set the MMU into unmapped mode */
  1005. kbase_mmu_disable_as(kbdev, as_no);
  1006. mutex_unlock(&kbdev->as[as_no].transaction_mutex);
  1007. /* AS transaction end */
  1008. kbase_mmu_hw_clear_fault(kbdev, faulting_as, kctx,
  1009. KBASE_MMU_FAULT_TYPE_BUS_UNEXPECTED);
  1010. kbase_mmu_hw_enable_fault(kbdev, faulting_as, kctx,
  1011. KBASE_MMU_FAULT_TYPE_BUS_UNEXPECTED);
  1012. kbase_pm_context_idle(kbdev);
  1013. }
  1014. #if KBASE_GPU_RESET_EN
  1015. if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8245) && reset_status)
  1016. kbase_reset_gpu(kbdev);
  1017. #endif
  1018. kbasep_js_runpool_release_ctx(kbdev, kctx);
  1019. atomic_dec(&kbdev->faults_pending);
  1020. }
  1021. const char *kbase_exception_name(struct kbase_device *kbdev, u32 exception_code)
  1022. {
  1023. const char *e;
  1024. switch (exception_code) {
  1025. /* Non-Fault Status code */
  1026. case 0x00:
  1027. e = "NOT_STARTED/IDLE/OK";
  1028. break;
  1029. case 0x01:
  1030. e = "DONE";
  1031. break;
  1032. case 0x02:
  1033. e = "INTERRUPTED";
  1034. break;
  1035. case 0x03:
  1036. e = "STOPPED";
  1037. break;
  1038. case 0x04:
  1039. e = "TERMINATED";
  1040. break;
  1041. case 0x08:
  1042. e = "ACTIVE";
  1043. break;
  1044. /* Job exceptions */
  1045. case 0x40:
  1046. e = "JOB_CONFIG_FAULT";
  1047. break;
  1048. case 0x41:
  1049. e = "JOB_POWER_FAULT";
  1050. break;
  1051. case 0x42:
  1052. e = "JOB_READ_FAULT";
  1053. break;
  1054. case 0x43:
  1055. e = "JOB_WRITE_FAULT";
  1056. break;
  1057. case 0x44:
  1058. e = "JOB_AFFINITY_FAULT";
  1059. break;
  1060. case 0x48:
  1061. e = "JOB_BUS_FAULT";
  1062. break;
  1063. case 0x50:
  1064. e = "INSTR_INVALID_PC";
  1065. break;
  1066. case 0x51:
  1067. e = "INSTR_INVALID_ENC";
  1068. break;
  1069. case 0x52:
  1070. e = "INSTR_TYPE_MISMATCH";
  1071. break;
  1072. case 0x53:
  1073. e = "INSTR_OPERAND_FAULT";
  1074. break;
  1075. case 0x54:
  1076. e = "INSTR_TLS_FAULT";
  1077. break;
  1078. case 0x55:
  1079. e = "INSTR_BARRIER_FAULT";
  1080. break;
  1081. case 0x56:
  1082. e = "INSTR_ALIGN_FAULT";
  1083. break;
  1084. case 0x58:
  1085. e = "DATA_INVALID_FAULT";
  1086. break;
  1087. case 0x59:
  1088. e = "TILE_RANGE_FAULT";
  1089. break;
  1090. case 0x5A:
  1091. e = "ADDR_RANGE_FAULT";
  1092. break;
  1093. case 0x60:
  1094. e = "OUT_OF_MEMORY";
  1095. break;
  1096. /* GPU exceptions */
  1097. case 0x80:
  1098. e = "DELAYED_BUS_FAULT";
  1099. break;
  1100. case 0x88:
  1101. e = "SHAREABILITY_FAULT";
  1102. break;
  1103. /* MMU exceptions */
  1104. case 0xC0:
  1105. case 0xC1:
  1106. case 0xC2:
  1107. case 0xC3:
  1108. case 0xC4:
  1109. case 0xC5:
  1110. case 0xC6:
  1111. case 0xC7:
  1112. e = "TRANSLATION_FAULT";
  1113. break;
  1114. case 0xC8:
  1115. #ifdef CONFIG_MALI_GPU_MMU_AARCH64
  1116. case 0xC9:
  1117. case 0xCA:
  1118. case 0xCB:
  1119. case 0xCC:
  1120. case 0xCD:
  1121. case 0xCE:
  1122. case 0xCF:
  1123. #endif // ifdef CONFIG_MALI_GPU_MMU_AARCH64
  1124. e = "PERMISSION_FAULT";
  1125. break;
  1126. case 0xD0:
  1127. case 0xD1:
  1128. case 0xD2:
  1129. case 0xD3:
  1130. case 0xD4:
  1131. case 0xD5:
  1132. case 0xD6:
  1133. case 0xD7:
  1134. e = "TRANSTAB_BUS_FAULT";
  1135. break;
  1136. case 0xD8:
  1137. #ifdef CONFIG_MALI_GPU_MMU_AARCH64
  1138. case 0xD9:
  1139. case 0xDA:
  1140. case 0xDB:
  1141. case 0xDC:
  1142. case 0xDD:
  1143. case 0xDE:
  1144. case 0xDF:
  1145. #endif // ifdef CONFIG_MALI_GPU_MMU_AARCH64
  1146. e = "ACCESS_FLAG";
  1147. break;
  1148. #ifdef CONFIG_MALI_GPU_MMU_AARCH64
  1149. case 0xE0:
  1150. case 0xE1:
  1151. case 0xE2:
  1152. case 0xE3:
  1153. case 0xE4:
  1154. case 0xE5:
  1155. case 0xE6:
  1156. case 0xE7:
  1157. e = "ADDRESS_SIZE_FAULT";
  1158. break;
  1159. case 0xE8:
  1160. case 0xE9:
  1161. case 0xEA:
  1162. case 0xEB:
  1163. case 0xEC:
  1164. case 0xED:
  1165. case 0xEE:
  1166. case 0xEF:
  1167. e = "MEMORY_ATTRIBUTES_FAULT";
  1168. #endif // ifdef CONFIG_MALI_GPU_MMU_AARCH64
  1169. break;
  1170. default:
  1171. e = "UNKNOWN";
  1172. break;
  1173. };
  1174. return e;
  1175. }
  1176. static const char *access_type_name(struct kbase_device *kbdev,
  1177. u32 fault_status)
  1178. {
  1179. switch (fault_status & AS_FAULTSTATUS_ACCESS_TYPE_MASK) {
  1180. case AS_FAULTSTATUS_ACCESS_TYPE_ATOMIC:
  1181. #ifdef CONFIG_MALI_GPU_MMU_AARCH64
  1182. return "ATOMIC";
  1183. #else
  1184. return "UNKNOWN";
  1185. #endif
  1186. case AS_FAULTSTATUS_ACCESS_TYPE_READ:
  1187. return "READ";
  1188. case AS_FAULTSTATUS_ACCESS_TYPE_WRITE:
  1189. return "WRITE";
  1190. case AS_FAULTSTATUS_ACCESS_TYPE_EX:
  1191. return "EXECUTE";
  1192. default:
  1193. WARN_ON(1);
  1194. return NULL;
  1195. }
  1196. }
  1197. /**
  1198. * The caller must ensure it's retained the ctx to prevent it from being scheduled out whilst it's being worked on.
  1199. */
  1200. static void kbase_mmu_report_fault_and_kill(struct kbase_context *kctx,
  1201. struct kbase_as *as, const char *reason_str)
  1202. {
  1203. unsigned long flags;
  1204. int exception_type;
  1205. int access_type;
  1206. int source_id;
  1207. int as_no;
  1208. struct kbase_device *kbdev;
  1209. struct kbasep_js_device_data *js_devdata;
  1210. #if KBASE_GPU_RESET_EN
  1211. bool reset_status = false;
  1212. #endif
  1213. as_no = as->number;
  1214. kbdev = kctx->kbdev;
  1215. js_devdata = &kbdev->js_data;
  1216. /* ASSERT that the context won't leave the runpool */
  1217. KBASE_DEBUG_ASSERT(kbasep_js_debug_check_ctx_refcount(kbdev, kctx) > 0);
  1218. /* decode the fault status */
  1219. exception_type = as->fault_status & 0xFF;
  1220. access_type = (as->fault_status >> 8) & 0x3;
  1221. source_id = (as->fault_status >> 16);
  1222. /* terminal fault, print info about the fault */
  1223. dev_err(kbdev->dev,
  1224. "Unhandled Page fault in AS%d at VA 0x%016llX\n"
  1225. "Reason: %s\n"
  1226. "raw fault status 0x%X\n"
  1227. "decoded fault status: %s\n"
  1228. "exception type 0x%X: %s\n"
  1229. "access type 0x%X: %s\n"
  1230. "source id 0x%X\n"
  1231. "pid: %d\n",
  1232. as_no, as->fault_addr,
  1233. reason_str,
  1234. as->fault_status,
  1235. (as->fault_status & (1 << 10) ? "DECODER FAULT" : "SLAVE FAULT"),
  1236. exception_type, kbase_exception_name(kbdev, exception_type),
  1237. access_type, access_type_name(kbdev, as->fault_status),
  1238. source_id,
  1239. kctx->pid);
  1240. /* hardware counters dump fault handling */
  1241. if ((kbdev->hwcnt.kctx) && (kbdev->hwcnt.kctx->as_nr == as_no) &&
  1242. (kbdev->hwcnt.backend.state ==
  1243. KBASE_INSTR_STATE_DUMPING)) {
  1244. unsigned int num_core_groups = kbdev->gpu_props.num_core_groups;
  1245. if ((as->fault_addr >= kbdev->hwcnt.addr) &&
  1246. (as->fault_addr < (kbdev->hwcnt.addr +
  1247. (num_core_groups * 2048))))
  1248. kbdev->hwcnt.backend.state = KBASE_INSTR_STATE_FAULT;
  1249. }
  1250. /* Stop the kctx from submitting more jobs and cause it to be scheduled
  1251. * out/rescheduled - this will occur on releasing the context's refcount */
  1252. spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
  1253. kbasep_js_clear_submit_allowed(js_devdata, kctx);
  1254. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
  1255. /* Kill any running jobs from the context. Submit is disallowed, so no more jobs from this
  1256. * context can appear in the job slots from this point on */
  1257. kbase_backend_jm_kill_jobs_from_kctx(kctx);
  1258. /* AS transaction begin */
  1259. mutex_lock(&as->transaction_mutex);
  1260. #if KBASE_GPU_RESET_EN
  1261. if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8245)) {
  1262. /* Due to H/W issue 8245 we need to reset the GPU after using UNMAPPED mode.
  1263. * We start the reset before switching to UNMAPPED to ensure that unrelated jobs
  1264. * are evicted from the GPU before the switch.
  1265. */
  1266. dev_err(kbdev->dev, "Unhandled page fault. For this GPU version we now soft-reset the GPU as part of page fault recovery.");
  1267. reset_status = kbase_prepare_to_reset_gpu(kbdev);
  1268. }
  1269. #endif // KBASE_GPU_RESET_EN
  1270. /* switch to UNMAPPED mode, will abort all jobs and stop any hw counter dumping */
  1271. kbase_mmu_disable_as(kbdev, as_no);
  1272. mutex_unlock(&as->transaction_mutex);
  1273. /* AS transaction end */
  1274. /* Clear down the fault */
  1275. kbase_mmu_hw_clear_fault(kbdev, as, kctx,
  1276. KBASE_MMU_FAULT_TYPE_PAGE_UNEXPECTED);
  1277. kbase_mmu_hw_enable_fault(kbdev, as, kctx,
  1278. KBASE_MMU_FAULT_TYPE_PAGE_UNEXPECTED);
  1279. #if KBASE_GPU_RESET_EN
  1280. if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8245) && reset_status)
  1281. kbase_reset_gpu(kbdev);
  1282. #endif
  1283. }
  1284. void kbasep_as_do_poke(struct work_struct *work)
  1285. {
  1286. struct kbase_as *as;
  1287. struct kbase_device *kbdev;
  1288. struct kbase_context *kctx;
  1289. unsigned long flags;
  1290. KBASE_DEBUG_ASSERT(work);
  1291. as = container_of(work, struct kbase_as, poke_work);
  1292. kbdev = container_of(as, struct kbase_device, as[as->number]);
  1293. KBASE_DEBUG_ASSERT(as->poke_state & KBASE_AS_POKE_STATE_IN_FLIGHT);
  1294. /* GPU power will already be active by virtue of the caller holding a JS
  1295. * reference on the address space, and will not release it until this worker
  1296. * has finished */
  1297. /* Further to the comment above, we know that while this function is running
  1298. * the AS will not be released as before the atom is released this workqueue
  1299. * is flushed (in kbase_as_poking_timer_release_atom)
  1300. */
  1301. kctx = kbasep_js_runpool_lookup_ctx_noretain(kbdev, as->number);
  1302. /* AS transaction begin */
  1303. mutex_lock(&as->transaction_mutex);
  1304. /* Force a uTLB invalidate */
  1305. kbase_mmu_hw_do_operation(kbdev, as, kctx, 0, 0,
  1306. AS_COMMAND_UNLOCK, 0);
  1307. mutex_unlock(&as->transaction_mutex);
  1308. /* AS transaction end */
  1309. spin_lock_irqsave(&kbdev->js_data.runpool_irq.lock, flags);
  1310. if (as->poke_refcount &&
  1311. !(as->poke_state & KBASE_AS_POKE_STATE_KILLING_POKE)) {
  1312. /* Only queue up the timer if we need it, and we're not trying to kill it */
  1313. hrtimer_start(&as->poke_timer, HR_TIMER_DELAY_MSEC(5), HRTIMER_MODE_REL);
  1314. }
  1315. spin_unlock_irqrestore(&kbdev->js_data.runpool_irq.lock, flags);
  1316. }
  1317. enum hrtimer_restart kbasep_as_poke_timer_callback(struct hrtimer *timer)
  1318. {
  1319. struct kbase_as *as;
  1320. int queue_work_ret;
  1321. KBASE_DEBUG_ASSERT(timer != NULL);
  1322. as = container_of(timer, struct kbase_as, poke_timer);
  1323. KBASE_DEBUG_ASSERT(as->poke_state & KBASE_AS_POKE_STATE_IN_FLIGHT);
  1324. queue_work_ret = queue_work(as->poke_wq, &as->poke_work);
  1325. KBASE_DEBUG_ASSERT(queue_work_ret);
  1326. return HRTIMER_NORESTART;
  1327. }
  1328. /**
  1329. * Retain the poking timer on an atom's context (if the atom hasn't already
  1330. * done so), and start the timer (if it's not already started).
  1331. *
  1332. * This must only be called on a context that's scheduled in, and an atom
  1333. * that's running on the GPU.
  1334. *
  1335. * The caller must hold kbasep_js_device_data::runpool_irq::lock
  1336. *
  1337. * This can be called safely from atomic context
  1338. */
  1339. void kbase_as_poking_timer_retain_atom(struct kbase_device *kbdev, struct kbase_context *kctx, struct kbase_jd_atom *katom)
  1340. {
  1341. struct kbase_as *as;
  1342. KBASE_DEBUG_ASSERT(kbdev);
  1343. KBASE_DEBUG_ASSERT(kctx);
  1344. KBASE_DEBUG_ASSERT(katom);
  1345. KBASE_DEBUG_ASSERT(kctx->as_nr != KBASEP_AS_NR_INVALID);
  1346. lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
  1347. if (katom->poking)
  1348. return;
  1349. katom->poking = 1;
  1350. /* It's safe to work on the as/as_nr without an explicit reference,
  1351. * because the caller holds the runpool_irq lock, and the atom itself
  1352. * was also running and had already taken a reference */
  1353. as = &kbdev->as[kctx->as_nr];
  1354. if (++(as->poke_refcount) == 1) {
  1355. /* First refcount for poke needed: check if not already in flight */
  1356. if (!as->poke_state) {
  1357. /* need to start poking */
  1358. as->poke_state |= KBASE_AS_POKE_STATE_IN_FLIGHT;
  1359. queue_work(as->poke_wq, &as->poke_work);
  1360. }
  1361. }
  1362. }
  1363. /**
  1364. * If an atom holds a poking timer, release it and wait for it to finish
  1365. *
  1366. * This must only be called on a context that's scheduled in, and an atom
  1367. * that still has a JS reference on the context
  1368. *
  1369. * This must \b not be called from atomic context, since it can sleep.
  1370. */
  1371. void kbase_as_poking_timer_release_atom(struct kbase_device *kbdev, struct kbase_context *kctx, struct kbase_jd_atom *katom)
  1372. {
  1373. struct kbase_as *as;
  1374. unsigned long flags;
  1375. KBASE_DEBUG_ASSERT(kbdev);
  1376. KBASE_DEBUG_ASSERT(kctx);
  1377. KBASE_DEBUG_ASSERT(katom);
  1378. KBASE_DEBUG_ASSERT(kctx->as_nr != KBASEP_AS_NR_INVALID);
  1379. if (!katom->poking)
  1380. return;
  1381. as = &kbdev->as[kctx->as_nr];
  1382. spin_lock_irqsave(&kbdev->js_data.runpool_irq.lock, flags);
  1383. KBASE_DEBUG_ASSERT(as->poke_refcount > 0);
  1384. KBASE_DEBUG_ASSERT(as->poke_state & KBASE_AS_POKE_STATE_IN_FLIGHT);
  1385. if (--(as->poke_refcount) == 0) {
  1386. as->poke_state |= KBASE_AS_POKE_STATE_KILLING_POKE;
  1387. spin_unlock_irqrestore(&kbdev->js_data.runpool_irq.lock, flags);
  1388. hrtimer_cancel(&as->poke_timer);
  1389. flush_workqueue(as->poke_wq);
  1390. spin_lock_irqsave(&kbdev->js_data.runpool_irq.lock, flags);
  1391. /* Re-check whether it's still needed */
  1392. if (as->poke_refcount) {
  1393. int queue_work_ret;
  1394. /* Poking still needed:
  1395. * - Another retain will not be starting the timer or queueing work,
  1396. * because it's still marked as in-flight
  1397. * - The hrtimer has finished, and has not started a new timer or
  1398. * queued work because it's been marked as killing
  1399. *
  1400. * So whatever happens now, just queue the work again */
  1401. as->poke_state &= ~((kbase_as_poke_state)KBASE_AS_POKE_STATE_KILLING_POKE);
  1402. queue_work_ret = queue_work(as->poke_wq, &as->poke_work);
  1403. KBASE_DEBUG_ASSERT(queue_work_ret);
  1404. } else {
  1405. /* It isn't - so mark it as not in flight, and not killing */
  1406. as->poke_state = 0u;
  1407. /* The poke associated with the atom has now finished. If this is
  1408. * also the last atom on the context, then we can guarentee no more
  1409. * pokes (and thus no more poking register accesses) will occur on
  1410. * the context until new atoms are run */
  1411. }
  1412. }
  1413. spin_unlock_irqrestore(&kbdev->js_data.runpool_irq.lock, flags);
  1414. katom->poking = 0;
  1415. }
  1416. void kbase_mmu_interrupt_process(struct kbase_device *kbdev, struct kbase_context *kctx, struct kbase_as *as)
  1417. {
  1418. struct kbasep_js_device_data *js_devdata = &kbdev->js_data;
  1419. lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
  1420. if (!kctx) {
  1421. dev_warn(kbdev->dev, "%s in AS%d at 0x%016llx with no context present! Suprious IRQ or SW Design Error?\n",
  1422. kbase_as_has_bus_fault(as) ? "Bus error" : "Page fault",
  1423. as->number, as->fault_addr);
  1424. /* Since no ctx was found, the MMU must be disabled. */
  1425. WARN_ON(as->current_setup.transtab);
  1426. if (kbase_as_has_bus_fault(as)) {
  1427. kbase_mmu_hw_clear_fault(kbdev, as, kctx,
  1428. KBASE_MMU_FAULT_TYPE_BUS_UNEXPECTED);
  1429. kbase_mmu_hw_enable_fault(kbdev, as, kctx,
  1430. KBASE_MMU_FAULT_TYPE_BUS_UNEXPECTED);
  1431. } else if (kbase_as_has_page_fault(as)) {
  1432. kbase_mmu_hw_clear_fault(kbdev, as, kctx,
  1433. KBASE_MMU_FAULT_TYPE_PAGE_UNEXPECTED);
  1434. kbase_mmu_hw_enable_fault(kbdev, as, kctx,
  1435. KBASE_MMU_FAULT_TYPE_PAGE_UNEXPECTED);
  1436. }
  1437. #if KBASE_GPU_RESET_EN
  1438. if (kbase_as_has_bus_fault(as) &&
  1439. kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8245)) {
  1440. bool reset_status;
  1441. /*
  1442. * Reset the GPU, like in bus_fault_worker, in case an
  1443. * earlier error hasn't been properly cleared by this
  1444. * point.
  1445. */
  1446. dev_err(kbdev->dev, "GPU bus error occurred. For this GPU version we now soft-reset as part of bus error recovery\n");
  1447. reset_status = kbase_prepare_to_reset_gpu_locked(kbdev);
  1448. if (reset_status)
  1449. kbase_reset_gpu_locked(kbdev);
  1450. }
  1451. #endif // KBASE_GPU_RESET_EN
  1452. return;
  1453. }
  1454. if (kbase_as_has_bus_fault(as)) {
  1455. /*
  1456. * hw counters dumping in progress, signal the
  1457. * other thread that it failed
  1458. */
  1459. if ((kbdev->hwcnt.kctx == kctx) &&
  1460. (kbdev->hwcnt.backend.state ==
  1461. KBASE_INSTR_STATE_DUMPING))
  1462. kbdev->hwcnt.backend.state =
  1463. KBASE_INSTR_STATE_FAULT;
  1464. /*
  1465. * Stop the kctx from submitting more jobs and cause it
  1466. * to be scheduled out/rescheduled when all references
  1467. * to it are released
  1468. */
  1469. kbasep_js_clear_submit_allowed(js_devdata, kctx);
  1470. #ifdef CONFIG_MALI_GPU_MMU_AARCH64
  1471. dev_warn(kbdev->dev,
  1472. "Bus error in AS%d at VA=0x%016llx, IPA=0x%016llx\n",
  1473. as->number, as->fault_addr,
  1474. as->fault_extra_addr);
  1475. #else // ifdef CONFIG_MALI_GPU_MMU_AARCH64
  1476. dev_warn(kbdev->dev, "Bus error in AS%d at 0x%016llx\n",
  1477. as->number, as->fault_addr);
  1478. #endif
  1479. /*
  1480. * We need to switch to UNMAPPED mode - but we do this in a
  1481. * worker so that we can sleep
  1482. */
  1483. KBASE_DEBUG_ASSERT(object_is_on_stack(&as->work_busfault) == 0);
  1484. WARN_ON(work_pending(&as->work_busfault));
  1485. queue_work(as->pf_wq, &as->work_busfault);
  1486. atomic_inc(&kbdev->faults_pending);
  1487. } else {
  1488. KBASE_DEBUG_ASSERT(object_is_on_stack(&as->work_pagefault) == 0);
  1489. WARN_ON(work_pending(&as->work_pagefault));
  1490. queue_work(as->pf_wq, &as->work_pagefault);
  1491. atomic_inc(&kbdev->faults_pending);
  1492. }
  1493. }
  1494. void kbase_flush_mmu_wqs(struct kbase_device *kbdev)
  1495. {
  1496. int i;
  1497. for (i = 0; i < kbdev->nr_hw_address_spaces; i++) {
  1498. struct kbase_as *as = &kbdev->as[i];
  1499. flush_workqueue(as->pf_wq);
  1500. }
  1501. }