123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791 |
- /*
- *
- * (C) COPYRIGHT 2010-2016 ARM Limited. All rights reserved.
- *
- * This program is free software and is provided to you under the terms of the
- * GNU General Public License version 2 as published by the Free Software
- * Foundation, and any use by you of this program is subject to the terms
- * of such GNU licence.
- *
- * A copy of the licence is included with the program, and can also be obtained
- * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301, USA.
- *
- */
- /**
- * @file mali_kbase_mmu.c
- * Base kernel MMU management.
- */
- /* #define DEBUG 1 */
- #include <linux/kernel.h>
- #include <linux/dma-mapping.h>
- #include <mali_kbase.h>
- #include <mali_midg_regmap.h>
- #include <mali_kbase_tlstream.h>
- #include <mali_kbase_debug.h>
- #define beenthere(kctx, f, a...) dev_dbg(kctx->kbdev->dev, "%s:" f, __func__, ##a)
- #include <mali_kbase_defs.h>
- #include <mali_kbase_hw.h>
- #include <mali_kbase_mmu_hw.h>
- #include <mali_kbase_hwaccess_jm.h>
- #define KBASE_MMU_PAGE_ENTRIES 512
- /**
- * kbase_mmu_sync_pgd - sync page directory to memory
- * @kbdev: Device pointer.
- * @handle: Address of DMA region.
- * @size: Size of the region to sync.
- *
- * This should be called after each page directory update.
- */
- static void kbase_mmu_sync_pgd(struct kbase_device *kbdev,
- dma_addr_t handle, size_t size)
- {
- /* If page table is not coherent then ensure the gpu can read
- * the pages from memory
- */
- if (kbdev->system_coherency != COHERENCY_ACE)
- dma_sync_single_for_device(kbdev->dev, handle, size,
- DMA_TO_DEVICE);
- }
- /*
- * Definitions:
- * - PGD: Page Directory.
- * - PTE: Page Table Entry. A 64bit value pointing to the next
- * level of translation
- * - ATE: Address Transation Entry. A 64bit value pointing to
- * a 4kB physical page.
- */
- static void kbase_mmu_report_fault_and_kill(struct kbase_context *kctx,
- struct kbase_as *as, const char *reason_str);
- static size_t make_multiple(size_t minimum, size_t multiple)
- {
- size_t remainder = minimum % multiple;
- if (remainder == 0)
- return minimum;
- return minimum + multiple - remainder;
- }
- void page_fault_worker(struct work_struct *data)
- {
- u64 fault_pfn;
- u32 fault_status;
- size_t new_pages;
- size_t fault_rel_pfn;
- struct kbase_as *faulting_as;
- int as_no;
- struct kbase_context *kctx;
- struct kbase_device *kbdev;
- struct kbase_va_region *region;
- int err;
- bool grown = false;
- faulting_as = container_of(data, struct kbase_as, work_pagefault);
- fault_pfn = faulting_as->fault_addr >> PAGE_SHIFT;
- as_no = faulting_as->number;
- kbdev = container_of(faulting_as, struct kbase_device, as[as_no]);
- /* Grab the context that was already refcounted in kbase_mmu_interrupt().
- * Therefore, it cannot be scheduled out of this AS until we explicitly release it
- */
- kctx = kbasep_js_runpool_lookup_ctx_noretain(kbdev, as_no);
- if (WARN_ON(!kctx)) {
- atomic_dec(&kbdev->faults_pending);
- return;
- }
- KBASE_DEBUG_ASSERT(kctx->kbdev == kbdev);
- fault_status = faulting_as->fault_status;
- switch (fault_status & AS_FAULTSTATUS_EXCEPTION_CODE_MASK) {
- case AS_FAULTSTATUS_EXCEPTION_CODE_TRANSLATION_FAULT:
- /* need to check against the region to handle this one */
- break;
- case AS_FAULTSTATUS_EXCEPTION_CODE_PERMISSION_FAULT:
- kbase_mmu_report_fault_and_kill(kctx, faulting_as,
- "Permission failure");
- goto fault_done;
- case AS_FAULTSTATUS_EXCEPTION_CODE_TRANSTAB_BUS_FAULT:
- kbase_mmu_report_fault_and_kill(kctx, faulting_as,
- "Tranlation table bus fault");
- goto fault_done;
- case AS_FAULTSTATUS_EXCEPTION_CODE_ACCESS_FLAG:
- /* nothing to do, but we don't expect this fault currently */
- dev_warn(kbdev->dev, "Access flag unexpectedly set");
- goto fault_done;
- #ifdef CONFIG_MALI_GPU_MMU_AARCH64
- case AS_FAULTSTATUS_EXCEPTION_CODE_ADDRESS_SIZE_FAULT:
- kbase_mmu_report_fault_and_kill(kctx, faulting_as,
- "Address size fault");
- goto fault_done;
- case AS_FAULTSTATUS_EXCEPTION_CODE_MEMORY_ATTRIBUTES_FAULT:
- kbase_mmu_report_fault_and_kill(kctx, faulting_as,
- "Memory attributes fault");
- goto fault_done;
- #endif // ifdef CONFIG_MALI_GPU_MMU_AARCH64
- default:
- kbase_mmu_report_fault_and_kill(kctx, faulting_as,
- "Unknown fault code");
- goto fault_done;
- }
- /* so we have a translation fault, let's see if it is for growable
- * memory */
- kbase_gpu_vm_lock(kctx);
- region = kbase_region_tracker_find_region_enclosing_address(kctx,
- faulting_as->fault_addr);
- if (!region || region->flags & KBASE_REG_FREE) {
- kbase_gpu_vm_unlock(kctx);
- kbase_mmu_report_fault_and_kill(kctx, faulting_as,
- "Memory is not mapped on the GPU");
- goto fault_done;
- }
- if ((region->flags & GROWABLE_FLAGS_REQUIRED)
- != GROWABLE_FLAGS_REQUIRED) {
- kbase_gpu_vm_unlock(kctx);
- kbase_mmu_report_fault_and_kill(kctx, faulting_as,
- "Memory is not growable");
- goto fault_done;
- }
- if ((region->flags & KBASE_REG_DONT_NEED)) {
- kbase_gpu_vm_unlock(kctx);
- kbase_mmu_report_fault_and_kill(kctx, faulting_as,
- "Don't need memory can't be grown");
- goto fault_done;
- }
- /* find the size we need to grow it by */
- /* we know the result fit in a size_t due to kbase_region_tracker_find_region_enclosing_address
- * validating the fault_adress to be within a size_t from the start_pfn */
- fault_rel_pfn = fault_pfn - region->start_pfn;
- if (fault_rel_pfn < kbase_reg_current_backed_size(region)) {
- dev_dbg(kbdev->dev, "Page fault @ 0x%llx in allocated region 0x%llx-0x%llx of growable TMEM: Ignoring",
- faulting_as->fault_addr, region->start_pfn,
- region->start_pfn +
- kbase_reg_current_backed_size(region));
- kbase_mmu_hw_clear_fault(kbdev, faulting_as, kctx,
- KBASE_MMU_FAULT_TYPE_PAGE);
- /* [1] in case another page fault occurred while we were
- * handling the (duplicate) page fault we need to ensure we
- * don't loose the other page fault as result of us clearing
- * the MMU IRQ. Therefore, after we clear the MMU IRQ we send
- * an UNLOCK command that will retry any stalled memory
- * transaction (which should cause the other page fault to be
- * raised again).
- */
- kbase_mmu_hw_do_operation(kbdev, faulting_as, NULL, 0, 0,
- AS_COMMAND_UNLOCK, 1);
- kbase_mmu_hw_enable_fault(kbdev, faulting_as, kctx,
- KBASE_MMU_FAULT_TYPE_PAGE);
- kbase_gpu_vm_unlock(kctx);
- goto fault_done;
- }
- new_pages = make_multiple(fault_rel_pfn -
- kbase_reg_current_backed_size(region) + 1,
- region->extent);
- /* cap to max vsize */
- if (new_pages + kbase_reg_current_backed_size(region) >
- region->nr_pages)
- new_pages = region->nr_pages -
- kbase_reg_current_backed_size(region);
- if (new_pages == 0) {
- /* Duplicate of a fault we've already handled, nothing to do */
- kbase_mmu_hw_clear_fault(kbdev, faulting_as, kctx,
- KBASE_MMU_FAULT_TYPE_PAGE);
- /* See comment [1] about UNLOCK usage */
- kbase_mmu_hw_do_operation(kbdev, faulting_as, NULL, 0, 0,
- AS_COMMAND_UNLOCK, 1);
- kbase_mmu_hw_enable_fault(kbdev, faulting_as, kctx,
- KBASE_MMU_FAULT_TYPE_PAGE);
- kbase_gpu_vm_unlock(kctx);
- goto fault_done;
- }
- if (kbase_alloc_phy_pages_helper(region->gpu_alloc, new_pages) == 0) {
- if (region->gpu_alloc != region->cpu_alloc) {
- if (kbase_alloc_phy_pages_helper(
- region->cpu_alloc, new_pages) == 0) {
- grown = true;
- } else {
- kbase_free_phy_pages_helper(region->gpu_alloc,
- new_pages);
- }
- } else {
- grown = true;
- }
- }
- if (grown) {
- u32 op;
- /* alloc success */
- KBASE_DEBUG_ASSERT(kbase_reg_current_backed_size(region) <= region->nr_pages);
- /* set up the new pages */
- 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);
- if (err) {
- kbase_free_phy_pages_helper(region->gpu_alloc, new_pages);
- if (region->gpu_alloc != region->cpu_alloc)
- kbase_free_phy_pages_helper(region->cpu_alloc,
- new_pages);
- kbase_gpu_vm_unlock(kctx);
- /* The locked VA region will be unlocked and the cache invalidated in here */
- kbase_mmu_report_fault_and_kill(kctx, faulting_as,
- "Page table update failure");
- goto fault_done;
- }
- kbase_tlstream_aux_pagefault(kctx->id, (u64)new_pages);
- /* AS transaction begin */
- mutex_lock(&faulting_as->transaction_mutex);
- /* flush L2 and unlock the VA (resumes the MMU) */
- if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_6367))
- op = AS_COMMAND_FLUSH;
- else
- op = AS_COMMAND_FLUSH_PT;
- /* clear MMU interrupt - this needs to be done after updating
- * the page tables but before issuing a FLUSH command. The
- * FLUSH cmd has a side effect that it restarts stalled memory
- * transactions in other address spaces which may cause
- * another fault to occur. If we didn't clear the interrupt at
- * this stage a new IRQ might not be raised when the GPU finds
- * a MMU IRQ is already pending.
- */
- kbase_mmu_hw_clear_fault(kbdev, faulting_as, kctx,
- KBASE_MMU_FAULT_TYPE_PAGE);
- kbase_mmu_hw_do_operation(kbdev, faulting_as, kctx,
- faulting_as->fault_addr >> PAGE_SHIFT,
- new_pages,
- op, 1);
- mutex_unlock(&faulting_as->transaction_mutex);
- /* AS transaction end */
- /* reenable this in the mask */
- kbase_mmu_hw_enable_fault(kbdev, faulting_as, kctx,
- KBASE_MMU_FAULT_TYPE_PAGE);
- kbase_gpu_vm_unlock(kctx);
- } else {
- /* failed to extend, handle as a normal PF */
- kbase_gpu_vm_unlock(kctx);
- kbase_mmu_report_fault_and_kill(kctx, faulting_as,
- "Page allocation failure");
- }
- fault_done:
- /*
- * By this point, the fault was handled in some way,
- * so release the ctx refcount
- */
- kbasep_js_runpool_release_ctx(kbdev, kctx);
- atomic_dec(&kbdev->faults_pending);
- }
- phys_addr_t kbase_mmu_alloc_pgd(struct kbase_context *kctx)
- {
- u64 *page;
- int i;
- struct page *p;
- int new_page_count __maybe_unused;
- KBASE_DEBUG_ASSERT(kctx != NULL);
- new_page_count = kbase_atomic_add_pages(1, &kctx->used_pages);
- kbase_atomic_add_pages(1, &kctx->kbdev->memdev.used_pages);
- p = kbase_mem_pool_alloc(&kctx->mem_pool);
- if (!p)
- goto sub_pages;
- kbase_tlstream_aux_pagesalloc(
- (u32)kctx->id,
- (u64)new_page_count);
- page = kmap(p);
- if (page == NULL)
- goto alloc_free;
- kbase_process_page_usage_inc(kctx, 1);
- for (i = 0; i < KBASE_MMU_PAGE_ENTRIES; i++)
- kctx->kbdev->mmu_mode->entry_invalidate(&page[i]);
- kbase_mmu_sync_pgd(kctx->kbdev, kbase_dma_addr(p), PAGE_SIZE);
- kunmap(p);
- return page_to_phys(p);
- alloc_free:
- kbase_mem_pool_free(&kctx->mem_pool, p, false);
- sub_pages:
- kbase_atomic_sub_pages(1, &kctx->used_pages);
- kbase_atomic_sub_pages(1, &kctx->kbdev->memdev.used_pages);
- return 0;
- }
- /* Given PGD PFN for level N, return PGD PFN for level N+1 */
- static phys_addr_t mmu_get_next_pgd(struct kbase_context *kctx, phys_addr_t pgd, u64 vpfn, int level)
- {
- u64 *page;
- phys_addr_t target_pgd;
- struct page *p;
- KBASE_DEBUG_ASSERT(pgd);
- KBASE_DEBUG_ASSERT(kctx != NULL);
- lockdep_assert_held(&kctx->mmu_lock);
- /*
- * Architecture spec defines level-0 as being the top-most.
- * This is a bit unfortunate here, but we keep the same convention.
- */
- vpfn >>= (3 - level) * 9;
- vpfn &= 0x1FF;
- p = pfn_to_page(PFN_DOWN(pgd));
- page = kmap(p);
- if (page == NULL) {
- dev_warn(kctx->kbdev->dev, "mmu_get_next_pgd: kmap failure\n");
- return 0;
- }
- target_pgd = kctx->kbdev->mmu_mode->pte_to_phy_addr(page[vpfn]);
- if (!target_pgd) {
- target_pgd = kbase_mmu_alloc_pgd(kctx);
- if (!target_pgd) {
- dev_warn(kctx->kbdev->dev, "mmu_get_next_pgd: kbase_mmu_alloc_pgd failure\n");
- kunmap(p);
- return 0;
- }
- kctx->kbdev->mmu_mode->entry_set_pte(&page[vpfn], target_pgd);
- kbase_mmu_sync_pgd(kctx->kbdev, kbase_dma_addr(p), PAGE_SIZE);
- /* Rely on the caller to update the address space flags. */
- }
- kunmap(p);
- return target_pgd;
- }
- static phys_addr_t mmu_get_bottom_pgd(struct kbase_context *kctx, u64 vpfn)
- {
- phys_addr_t pgd;
- int l;
- lockdep_assert_held(&kctx->mmu_lock);
- pgd = kctx->pgd;
- for (l = MIDGARD_MMU_TOPLEVEL; l < MIDGARD_MMU_BOTTOMLEVEL; l++) {
- pgd = mmu_get_next_pgd(kctx, pgd, vpfn, l);
- /* Handle failure condition */
- if (!pgd) {
- dev_warn(kctx->kbdev->dev, "mmu_get_bottom_pgd: mmu_get_next_pgd failure\n");
- return 0;
- }
- }
- return pgd;
- }
- static phys_addr_t mmu_insert_pages_recover_get_next_pgd(struct kbase_context *kctx, phys_addr_t pgd, u64 vpfn, int level)
- {
- u64 *page;
- phys_addr_t target_pgd;
- KBASE_DEBUG_ASSERT(pgd);
- KBASE_DEBUG_ASSERT(kctx != NULL);
- lockdep_assert_held(&kctx->mmu_lock);
- /*
- * Architecture spec defines level-0 as being the top-most.
- * This is a bit unfortunate here, but we keep the same convention.
- */
- vpfn >>= (3 - level) * 9;
- vpfn &= 0x1FF;
- page = kmap_atomic(pfn_to_page(PFN_DOWN(pgd)));
- /* kmap_atomic should NEVER fail */
- KBASE_DEBUG_ASSERT(page != NULL);
- target_pgd = kctx->kbdev->mmu_mode->pte_to_phy_addr(page[vpfn]);
- /* As we are recovering from what has already been set up, we should have a target_pgd */
- KBASE_DEBUG_ASSERT(target_pgd != 0);
- kunmap_atomic(page);
- return target_pgd;
- }
- static phys_addr_t mmu_insert_pages_recover_get_bottom_pgd(struct kbase_context *kctx, u64 vpfn)
- {
- phys_addr_t pgd;
- int l;
- lockdep_assert_held(&kctx->mmu_lock);
- pgd = kctx->pgd;
- for (l = MIDGARD_MMU_TOPLEVEL; l < MIDGARD_MMU_BOTTOMLEVEL; l++) {
- pgd = mmu_insert_pages_recover_get_next_pgd(kctx, pgd, vpfn, l);
- /* Should never fail */
- KBASE_DEBUG_ASSERT(pgd != 0);
- }
- return pgd;
- }
- static void mmu_insert_pages_failure_recovery(struct kbase_context *kctx, u64 vpfn,
- size_t nr)
- {
- phys_addr_t pgd;
- u64 *pgd_page;
- struct kbase_mmu_mode const *mmu_mode;
- KBASE_DEBUG_ASSERT(kctx != NULL);
- KBASE_DEBUG_ASSERT(vpfn != 0);
- /* 64-bit address range is the max */
- KBASE_DEBUG_ASSERT(vpfn <= (U64_MAX / PAGE_SIZE));
- lockdep_assert_held(&kctx->mmu_lock);
- mmu_mode = kctx->kbdev->mmu_mode;
- while (nr) {
- unsigned int i;
- unsigned int index = vpfn & 0x1FF;
- unsigned int count = KBASE_MMU_PAGE_ENTRIES - index;
- struct page *p;
- if (count > nr)
- count = nr;
- pgd = mmu_insert_pages_recover_get_bottom_pgd(kctx, vpfn);
- KBASE_DEBUG_ASSERT(pgd != 0);
- p = pfn_to_page(PFN_DOWN(pgd));
- pgd_page = kmap_atomic(p);
- KBASE_DEBUG_ASSERT(pgd_page != NULL);
- /* Invalidate the entries we added */
- for (i = 0; i < count; i++)
- mmu_mode->entry_invalidate(&pgd_page[index + i]);
- vpfn += count;
- nr -= count;
- kbase_mmu_sync_pgd(kctx->kbdev, kbase_dma_addr(p), PAGE_SIZE);
- kunmap_atomic(pgd_page);
- }
- }
- /*
- * Map the single page 'phys' 'nr' of times, starting at GPU PFN 'vpfn'
- */
- int kbase_mmu_insert_single_page(struct kbase_context *kctx, u64 vpfn,
- phys_addr_t phys, size_t nr,
- unsigned long flags)
- {
- phys_addr_t pgd;
- u64 *pgd_page;
- /* In case the insert_single_page only partially completes we need to be
- * able to recover */
- bool recover_required = false;
- u64 recover_vpfn = vpfn;
- size_t recover_count = 0;
- int err;
- KBASE_DEBUG_ASSERT(kctx != NULL);
- KBASE_DEBUG_ASSERT(vpfn != 0);
- /* 64-bit address range is the max */
- KBASE_DEBUG_ASSERT(vpfn <= (U64_MAX / PAGE_SIZE));
- mutex_lock(&kctx->mmu_lock);
- while (nr) {
- unsigned int i;
- unsigned int index = vpfn & 0x1FF;
- unsigned int count = KBASE_MMU_PAGE_ENTRIES - index;
- struct page *p;
- if (count > nr)
- count = nr;
- /*
- * Repeatedly calling mmu_get_bottom_pte() is clearly
- * suboptimal. We don't have to re-parse the whole tree
- * each time (just cache the l0-l2 sequence).
- * On the other hand, it's only a gain when we map more than
- * 256 pages at once (on average). Do we really care?
- */
- pgd = mmu_get_bottom_pgd(kctx, vpfn);
- if (!pgd) {
- dev_warn(kctx->kbdev->dev, "kbase_mmu_insert_pages: mmu_get_bottom_pgd failure\n");
- if (recover_required) {
- /* Invalidate the pages we have partially
- * completed */
- mmu_insert_pages_failure_recovery(kctx,
- recover_vpfn,
- recover_count);
- }
- err = -EINVAL;
- goto fail_unlock;
- }
- p = pfn_to_page(PFN_DOWN(pgd));
- pgd_page = kmap(p);
- if (!pgd_page) {
- dev_warn(kctx->kbdev->dev, "kbase_mmu_insert_pages: kmap failure\n");
- if (recover_required) {
- /* Invalidate the pages we have partially
- * completed */
- mmu_insert_pages_failure_recovery(kctx,
- recover_vpfn,
- recover_count);
- }
- err = -ENOMEM;
- goto fail_unlock;
- }
- for (i = 0; i < count; i++) {
- unsigned int ofs = index + i;
- KBASE_DEBUG_ASSERT(0 == (pgd_page[ofs] & 1UL));
- kctx->kbdev->mmu_mode->entry_set_ate(&pgd_page[ofs],
- phys, flags);
- }
- vpfn += count;
- nr -= count;
- kbase_mmu_sync_pgd(kctx->kbdev,
- kbase_dma_addr(p) + (index * sizeof(u64)),
- count * sizeof(u64));
- kunmap(p);
- /* We have started modifying the page table.
- * If further pages need inserting and fail we need to undo what
- * has already taken place */
- recover_required = true;
- recover_count += count;
- }
- mutex_unlock(&kctx->mmu_lock);
- return 0;
- fail_unlock:
- mutex_unlock(&kctx->mmu_lock);
- return err;
- }
- /*
- * Map 'nr' pages pointed to by 'phys' at GPU PFN 'vpfn'
- */
- int kbase_mmu_insert_pages(struct kbase_context *kctx, u64 vpfn,
- phys_addr_t *phys, size_t nr,
- unsigned long flags)
- {
- phys_addr_t pgd;
- u64 *pgd_page;
- /* In case the insert_pages only partially completes we need to be able
- * to recover */
- bool recover_required = false;
- u64 recover_vpfn = vpfn;
- size_t recover_count = 0;
- int err;
- KBASE_DEBUG_ASSERT(kctx != NULL);
- KBASE_DEBUG_ASSERT(vpfn != 0);
- /* 64-bit address range is the max */
- KBASE_DEBUG_ASSERT(vpfn <= (U64_MAX / PAGE_SIZE));
- mutex_lock(&kctx->mmu_lock);
- while (nr) {
- unsigned int i;
- unsigned int index = vpfn & 0x1FF;
- unsigned int count = KBASE_MMU_PAGE_ENTRIES - index;
- struct page *p;
- if (count > nr)
- count = nr;
- /*
- * Repeatedly calling mmu_get_bottom_pte() is clearly
- * suboptimal. We don't have to re-parse the whole tree
- * each time (just cache the l0-l2 sequence).
- * On the other hand, it's only a gain when we map more than
- * 256 pages at once (on average). Do we really care?
- */
- pgd = mmu_get_bottom_pgd(kctx, vpfn);
- if (!pgd) {
- dev_warn(kctx->kbdev->dev, "kbase_mmu_insert_pages: mmu_get_bottom_pgd failure\n");
- if (recover_required) {
- /* Invalidate the pages we have partially
- * completed */
- mmu_insert_pages_failure_recovery(kctx,
- recover_vpfn,
- recover_count);
- }
- err = -EINVAL;
- goto fail_unlock;
- }
- p = pfn_to_page(PFN_DOWN(pgd));
- pgd_page = kmap(p);
- if (!pgd_page) {
- dev_warn(kctx->kbdev->dev, "kbase_mmu_insert_pages: kmap failure\n");
- if (recover_required) {
- /* Invalidate the pages we have partially
- * completed */
- mmu_insert_pages_failure_recovery(kctx,
- recover_vpfn,
- recover_count);
- }
- err = -ENOMEM;
- goto fail_unlock;
- }
- for (i = 0; i < count; i++) {
- unsigned int ofs = index + i;
- KBASE_DEBUG_ASSERT(0 == (pgd_page[ofs] & 1UL));
- kctx->kbdev->mmu_mode->entry_set_ate(&pgd_page[ofs],
- phys[i], flags);
- }
- phys += count;
- vpfn += count;
- nr -= count;
- kbase_mmu_sync_pgd(kctx->kbdev,
- kbase_dma_addr(p) + (index * sizeof(u64)),
- count * sizeof(u64));
- kunmap(p);
- /* We have started modifying the page table. If further pages
- * need inserting and fail we need to undo what has already
- * taken place */
- recover_required = true;
- recover_count += count;
- }
- mutex_unlock(&kctx->mmu_lock);
- return 0;
- fail_unlock:
- mutex_unlock(&kctx->mmu_lock);
- return err;
- }
- /**
- * This function is responsible for validating the MMU PTs
- * triggering reguired flushes.
- *
- * * IMPORTANT: This uses kbasep_js_runpool_release_ctx() when the context is
- * currently scheduled into the runpool, and so potentially uses a lot of locks.
- * These locks must be taken in the correct order with respect to others
- * already held by the caller. Refer to kbasep_js_runpool_release_ctx() for more
- * information.
- */
- static void kbase_mmu_flush(struct kbase_context *kctx, u64 vpfn, size_t nr)
- {
- struct kbase_device *kbdev;
- bool ctx_is_in_runpool;
- KBASE_DEBUG_ASSERT(kctx != NULL);
- kbdev = kctx->kbdev;
- /* We must flush if we're currently running jobs. At the very least, we need to retain the
- * context to ensure it doesn't schedule out whilst we're trying to flush it */
- ctx_is_in_runpool = kbasep_js_runpool_retain_ctx(kbdev, kctx);
- if (ctx_is_in_runpool) {
- KBASE_DEBUG_ASSERT(kctx->as_nr != KBASEP_AS_NR_INVALID);
- /* Second level check is to try to only do this when jobs are running. The refcount is
- * a heuristic for this. */
- if (kbdev->js_data.runpool_irq.per_as_data[kctx->as_nr].as_busy_refcount >= 2) {
- if (!kbase_pm_context_active_handle_suspend(kbdev,
- KBASE_PM_SUSPEND_HANDLER_DONT_REACTIVATE)) {
- int ret;
- u32 op;
- /* AS transaction begin */
- mutex_lock(&kbdev->as[
- kctx->as_nr].transaction_mutex);
- if (kbase_hw_has_issue(kbdev,
- BASE_HW_ISSUE_6367))
- op = AS_COMMAND_FLUSH;
- else
- op = AS_COMMAND_FLUSH_MEM;
- ret = kbase_mmu_hw_do_operation(kbdev,
- &kbdev->as[kctx->as_nr],
- kctx, vpfn, nr,
- op, 0);
- #if KBASE_GPU_RESET_EN
- if (ret) {
- /* Flush failed to complete, assume the
- * GPU has hung and perform a reset to
- * recover */
- dev_err(kbdev->dev, "Flush for GPU page table update did not complete. Issueing GPU soft-reset to recover\n");
- if (kbase_prepare_to_reset_gpu(kbdev))
- kbase_reset_gpu(kbdev);
- }
- #endif // KBASE_GPU_RESET_EN
- mutex_unlock(&kbdev->as[
- kctx->as_nr].transaction_mutex);
- /* AS transaction end */
- kbase_pm_context_idle(kbdev);
- }
- }
- kbasep_js_runpool_release_ctx(kbdev, kctx);
- }
- }
- /*
- * We actually only discard the ATE, and not the page table
- * pages. There is a potential DoS here, as we'll leak memory by
- * having PTEs that are potentially unused. Will require physical
- * page accounting, so MMU pages are part of the process allocation.
- *
- * IMPORTANT: This uses kbasep_js_runpool_release_ctx() when the context is
- * currently scheduled into the runpool, and so potentially uses a lot of locks.
- * These locks must be taken in the correct order with respect to others
- * already held by the caller. Refer to kbasep_js_runpool_release_ctx() for more
- * information.
- */
- int kbase_mmu_teardown_pages(struct kbase_context *kctx, u64 vpfn, size_t nr)
- {
- phys_addr_t pgd;
- u64 *pgd_page;
- struct kbase_device *kbdev;
- size_t requested_nr = nr;
- struct kbase_mmu_mode const *mmu_mode;
- int err;
- KBASE_DEBUG_ASSERT(kctx != NULL);
- beenthere(kctx, "kctx %p vpfn %lx nr %zd", (void *)kctx, (unsigned long)vpfn, nr);
- if (nr == 0) {
- /* early out if nothing to do */
- return 0;
- }
- mutex_lock(&kctx->mmu_lock);
- kbdev = kctx->kbdev;
- mmu_mode = kbdev->mmu_mode;
- while (nr) {
- unsigned int i;
- unsigned int index = vpfn & 0x1FF;
- unsigned int count = KBASE_MMU_PAGE_ENTRIES - index;
- struct page *p;
- if (count > nr)
- count = nr;
- pgd = mmu_get_bottom_pgd(kctx, vpfn);
- if (!pgd) {
- dev_warn(kbdev->dev, "kbase_mmu_teardown_pages: mmu_get_bottom_pgd failure\n");
- err = -EINVAL;
- goto fail_unlock;
- }
- p = pfn_to_page(PFN_DOWN(pgd));
- pgd_page = kmap(p);
- if (!pgd_page) {
- dev_warn(kbdev->dev, "kbase_mmu_teardown_pages: kmap failure\n");
- err = -ENOMEM;
- goto fail_unlock;
- }
- for (i = 0; i < count; i++)
- mmu_mode->entry_invalidate(&pgd_page[index + i]);
- vpfn += count;
- nr -= count;
- kbase_mmu_sync_pgd(kctx->kbdev,
- kbase_dma_addr(p) + (index * sizeof(u64)),
- count * sizeof(u64));
- kunmap(p);
- }
- mutex_unlock(&kctx->mmu_lock);
- kbase_mmu_flush(kctx, vpfn, requested_nr);
- return 0;
- fail_unlock:
- mutex_unlock(&kctx->mmu_lock);
- return err;
- }
- /**
- * Update the entries for specified number of pages pointed to by 'phys' at GPU PFN 'vpfn'.
- * This call is being triggered as a response to the changes of the mem attributes
- *
- * @pre : The caller is responsible for validating the memory attributes
- *
- * IMPORTANT: This uses kbasep_js_runpool_release_ctx() when the context is
- * currently scheduled into the runpool, and so potentially uses a lot of locks.
- * These locks must be taken in the correct order with respect to others
- * already held by the caller. Refer to kbasep_js_runpool_release_ctx() for more
- * information.
- */
- int kbase_mmu_update_pages(struct kbase_context *kctx, u64 vpfn, phys_addr_t *phys, size_t nr, unsigned long flags)
- {
- phys_addr_t pgd;
- u64 *pgd_page;
- size_t requested_nr = nr;
- struct kbase_mmu_mode const *mmu_mode;
- int err;
- KBASE_DEBUG_ASSERT(kctx != NULL);
- KBASE_DEBUG_ASSERT(vpfn != 0);
- KBASE_DEBUG_ASSERT(vpfn <= (U64_MAX / PAGE_SIZE));
- mutex_lock(&kctx->mmu_lock);
- mmu_mode = kctx->kbdev->mmu_mode;
- dev_warn(kctx->kbdev->dev, "kbase_mmu_update_pages(): updating page share flags on GPU PFN 0x%llx from phys %p, %zu pages",
- vpfn, phys, nr);
- while (nr) {
- unsigned int i;
- unsigned int index = vpfn & 0x1FF;
- size_t count = KBASE_MMU_PAGE_ENTRIES - index;
- struct page *p;
- if (count > nr)
- count = nr;
- pgd = mmu_get_bottom_pgd(kctx, vpfn);
- if (!pgd) {
- dev_warn(kctx->kbdev->dev, "mmu_get_bottom_pgd failure\n");
- err = -EINVAL;
- goto fail_unlock;
- }
- p = pfn_to_page(PFN_DOWN(pgd));
- pgd_page = kmap(p);
- if (!pgd_page) {
- dev_warn(kctx->kbdev->dev, "kmap failure\n");
- err = -ENOMEM;
- goto fail_unlock;
- }
- for (i = 0; i < count; i++)
- mmu_mode->entry_set_ate(&pgd_page[index + i], phys[i],
- flags);
- phys += count;
- vpfn += count;
- nr -= count;
- kbase_mmu_sync_pgd(kctx->kbdev,
- kbase_dma_addr(p) + (index * sizeof(u64)),
- count * sizeof(u64));
- kunmap(pfn_to_page(PFN_DOWN(pgd)));
- }
- mutex_unlock(&kctx->mmu_lock);
- kbase_mmu_flush(kctx, vpfn, requested_nr);
- return 0;
- fail_unlock:
- mutex_unlock(&kctx->mmu_lock);
- return err;
- }
- /* This is a debug feature only */
- static void mmu_check_unused(struct kbase_context *kctx, phys_addr_t pgd)
- {
- u64 *page;
- int i;
- page = kmap_atomic(pfn_to_page(PFN_DOWN(pgd)));
- /* kmap_atomic should NEVER fail. */
- KBASE_DEBUG_ASSERT(page != NULL);
- for (i = 0; i < KBASE_MMU_PAGE_ENTRIES; i++) {
- if (kctx->kbdev->mmu_mode->ate_is_valid(page[i]))
- beenthere(kctx, "live pte %016lx", (unsigned long)page[i]);
- }
- kunmap_atomic(page);
- }
- static void mmu_teardown_level(struct kbase_context *kctx, phys_addr_t pgd, int level, int zap, u64 *pgd_page_buffer)
- {
- phys_addr_t target_pgd;
- u64 *pgd_page;
- int i;
- struct kbase_mmu_mode const *mmu_mode;
- KBASE_DEBUG_ASSERT(kctx != NULL);
- lockdep_assert_held(&kctx->mmu_lock);
- pgd_page = kmap_atomic(pfn_to_page(PFN_DOWN(pgd)));
- /* kmap_atomic should NEVER fail. */
- KBASE_DEBUG_ASSERT(pgd_page != NULL);
- /* Copy the page to our preallocated buffer so that we can minimize kmap_atomic usage */
- memcpy(pgd_page_buffer, pgd_page, PAGE_SIZE);
- kunmap_atomic(pgd_page);
- pgd_page = pgd_page_buffer;
- mmu_mode = kctx->kbdev->mmu_mode;
- for (i = 0; i < KBASE_MMU_PAGE_ENTRIES; i++) {
- target_pgd = mmu_mode->pte_to_phy_addr(pgd_page[i]);
- if (target_pgd) {
- if (level < (MIDGARD_MMU_BOTTOMLEVEL - 1)) {
- mmu_teardown_level(kctx, target_pgd, level + 1, zap, pgd_page_buffer + (PAGE_SIZE / sizeof(u64)));
- } else {
- /*
- * So target_pte is a level-3 page.
- * As a leaf, it is safe to free it.
- * Unless we have live pages attached to it!
- */
- mmu_check_unused(kctx, target_pgd);
- }
- beenthere(kctx, "pte %lx level %d", (unsigned long)target_pgd, level + 1);
- if (zap) {
- struct page *p = phys_to_page(target_pgd);
- kbase_mem_pool_free(&kctx->mem_pool, p, true);
- kbase_process_page_usage_dec(kctx, 1);
- kbase_atomic_sub_pages(1, &kctx->used_pages);
- kbase_atomic_sub_pages(1, &kctx->kbdev->memdev.used_pages);
- }
- }
- }
- }
- int kbase_mmu_init(struct kbase_context *kctx)
- {
- KBASE_DEBUG_ASSERT(kctx != NULL);
- KBASE_DEBUG_ASSERT(kctx->mmu_teardown_pages == NULL);
- mutex_init(&kctx->mmu_lock);
- /* Preallocate MMU depth of four pages for mmu_teardown_level to use */
- kctx->mmu_teardown_pages = kmalloc(PAGE_SIZE * 4, GFP_KERNEL);
- if (kctx->mmu_teardown_pages == NULL)
- return -ENOMEM;
- return 0;
- }
- void kbase_mmu_term(struct kbase_context *kctx)
- {
- KBASE_DEBUG_ASSERT(kctx != NULL);
- KBASE_DEBUG_ASSERT(kctx->mmu_teardown_pages != NULL);
- kfree(kctx->mmu_teardown_pages);
- kctx->mmu_teardown_pages = NULL;
- }
- void kbase_mmu_free_pgd(struct kbase_context *kctx)
- {
- int new_page_count __maybe_unused;
- KBASE_DEBUG_ASSERT(kctx != NULL);
- KBASE_DEBUG_ASSERT(kctx->mmu_teardown_pages != NULL);
- mutex_lock(&kctx->mmu_lock);
- mmu_teardown_level(kctx, kctx->pgd, MIDGARD_MMU_TOPLEVEL, 1, kctx->mmu_teardown_pages);
- mutex_unlock(&kctx->mmu_lock);
- beenthere(kctx, "pgd %lx", (unsigned long)kctx->pgd);
- kbase_mem_pool_free(&kctx->mem_pool, phys_to_page(kctx->pgd), true);
- kbase_process_page_usage_dec(kctx, 1);
- new_page_count = kbase_atomic_sub_pages(1, &kctx->used_pages);
- kbase_atomic_sub_pages(1, &kctx->kbdev->memdev.used_pages);
- kbase_tlstream_aux_pagesalloc(
- (u32)kctx->id,
- (u64)new_page_count);
- }
- static size_t kbasep_mmu_dump_level(struct kbase_context *kctx, phys_addr_t pgd, int level, char ** const buffer, size_t *size_left)
- {
- phys_addr_t target_pgd;
- u64 *pgd_page;
- int i;
- size_t size = KBASE_MMU_PAGE_ENTRIES * sizeof(u64) + sizeof(u64);
- size_t dump_size;
- struct kbase_mmu_mode const *mmu_mode;
- KBASE_DEBUG_ASSERT(kctx != NULL);
- lockdep_assert_held(&kctx->mmu_lock);
- mmu_mode = kctx->kbdev->mmu_mode;
- pgd_page = kmap(pfn_to_page(PFN_DOWN(pgd)));
- if (!pgd_page) {
- dev_warn(kctx->kbdev->dev, "kbasep_mmu_dump_level: kmap failure\n");
- return 0;
- }
- if (*size_left >= size) {
- /* A modified physical address that contains the page table level */
- u64 m_pgd = pgd | level;
- /* Put the modified physical address in the output buffer */
- memcpy(*buffer, &m_pgd, sizeof(m_pgd));
- *buffer += sizeof(m_pgd);
- /* Followed by the page table itself */
- memcpy(*buffer, pgd_page, sizeof(u64) * KBASE_MMU_PAGE_ENTRIES);
- *buffer += sizeof(u64) * KBASE_MMU_PAGE_ENTRIES;
- *size_left -= size;
- }
- if (level < MIDGARD_MMU_BOTTOMLEVEL) {
- for (i = 0; i < KBASE_MMU_PAGE_ENTRIES; i++) {
- if (mmu_mode->pte_is_valid(pgd_page[i])) {
- target_pgd = mmu_mode->pte_to_phy_addr(
- pgd_page[i]);
- dump_size = kbasep_mmu_dump_level(kctx,
- target_pgd, level + 1,
- buffer, size_left);
- if (!dump_size) {
- kunmap(pfn_to_page(PFN_DOWN(pgd)));
- return 0;
- }
- size += dump_size;
- }
- }
- }
- kunmap(pfn_to_page(PFN_DOWN(pgd)));
- return size;
- }
- void *kbase_mmu_dump(struct kbase_context *kctx, int nr_pages)
- {
- void *kaddr;
- size_t size_left;
- KBASE_DEBUG_ASSERT(kctx);
- if (nr_pages == 0) {
- /* can't dump in a 0 sized buffer, early out */
- return NULL;
- }
- mutex_lock(&kctx->mmu_lock);
- size_left = nr_pages * PAGE_SIZE;
- KBASE_DEBUG_ASSERT(size_left != 0);
- kaddr = vmalloc_user(size_left);
- if (kaddr) {
- u64 end_marker = 0xFFULL;
- char *buffer;
- char *mmu_dump_buffer;
- u64 config[3];
- size_t size;
- buffer = (char *)kaddr;
- mmu_dump_buffer = buffer;
- if (kctx->api_version >= KBASE_API_VERSION(8, 4)) {
- struct kbase_mmu_setup as_setup;
- kctx->kbdev->mmu_mode->get_as_setup(kctx, &as_setup);
- config[0] = as_setup.transtab;
- config[1] = as_setup.memattr;
- config[2] = as_setup.transcfg;
- memcpy(buffer, &config, sizeof(config));
- mmu_dump_buffer += sizeof(config);
- size_left -= sizeof(config);
- }
- size = kbasep_mmu_dump_level(kctx,
- kctx->pgd,
- MIDGARD_MMU_TOPLEVEL,
- &mmu_dump_buffer,
- &size_left);
- if (!size)
- goto fail_free;
- /* Add on the size for the end marker */
- size += sizeof(u64);
- /* Add on the size for the config */
- if (kctx->api_version >= KBASE_API_VERSION(8, 4))
- size += sizeof(config);
- if (size > nr_pages * PAGE_SIZE || size_left < sizeof(u64)) {
- /* The buffer isn't big enough - free the memory and return failure */
- goto fail_free;
- }
- /* Add the end marker */
- memcpy(mmu_dump_buffer, &end_marker, sizeof(u64));
- }
- mutex_unlock(&kctx->mmu_lock);
- return kaddr;
- fail_free:
- vfree(kaddr);
- mutex_unlock(&kctx->mmu_lock);
- return NULL;
- }
- void bus_fault_worker(struct work_struct *data)
- {
- struct kbase_as *faulting_as;
- int as_no;
- struct kbase_context *kctx;
- struct kbase_device *kbdev;
- #if KBASE_GPU_RESET_EN
- bool reset_status = false;
- #endif
- faulting_as = container_of(data, struct kbase_as, work_busfault);
- as_no = faulting_as->number;
- kbdev = container_of(faulting_as, struct kbase_device, as[as_no]);
- /* Grab the context that was already refcounted in kbase_mmu_interrupt().
- * Therefore, it cannot be scheduled out of this AS until we explicitly release it
- */
- kctx = kbasep_js_runpool_lookup_ctx_noretain(kbdev, as_no);
- if (WARN_ON(!kctx)) {
- atomic_dec(&kbdev->faults_pending);
- return;
- }
- #if KBASE_GPU_RESET_EN
- if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8245)) {
- /* Due to H/W issue 8245 we need to reset the GPU after using UNMAPPED mode.
- * We start the reset before switching to UNMAPPED to ensure that unrelated jobs
- * are evicted from the GPU before the switch.
- */
- dev_err(kbdev->dev, "GPU bus error occurred. For this GPU version we now soft-reset as part of bus error recovery\n");
- reset_status = kbase_prepare_to_reset_gpu(kbdev);
- }
- #endif // KBASE_GPU_RESET_EN
- /* NOTE: If GPU already powered off for suspend, we don't need to switch to unmapped */
- if (!kbase_pm_context_active_handle_suspend(kbdev, KBASE_PM_SUSPEND_HANDLER_DONT_REACTIVATE)) {
- /* switch to UNMAPPED mode, will abort all jobs and stop any hw counter dumping */
- /* AS transaction begin */
- mutex_lock(&kbdev->as[as_no].transaction_mutex);
- /* Set the MMU into unmapped mode */
- kbase_mmu_disable_as(kbdev, as_no);
- mutex_unlock(&kbdev->as[as_no].transaction_mutex);
- /* AS transaction end */
- kbase_mmu_hw_clear_fault(kbdev, faulting_as, kctx,
- KBASE_MMU_FAULT_TYPE_BUS_UNEXPECTED);
- kbase_mmu_hw_enable_fault(kbdev, faulting_as, kctx,
- KBASE_MMU_FAULT_TYPE_BUS_UNEXPECTED);
- kbase_pm_context_idle(kbdev);
- }
- #if KBASE_GPU_RESET_EN
- if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8245) && reset_status)
- kbase_reset_gpu(kbdev);
- #endif
- kbasep_js_runpool_release_ctx(kbdev, kctx);
- atomic_dec(&kbdev->faults_pending);
- }
- const char *kbase_exception_name(struct kbase_device *kbdev, u32 exception_code)
- {
- const char *e;
- switch (exception_code) {
- /* Non-Fault Status code */
- case 0x00:
- e = "NOT_STARTED/IDLE/OK";
- break;
- case 0x01:
- e = "DONE";
- break;
- case 0x02:
- e = "INTERRUPTED";
- break;
- case 0x03:
- e = "STOPPED";
- break;
- case 0x04:
- e = "TERMINATED";
- break;
- case 0x08:
- e = "ACTIVE";
- break;
- /* Job exceptions */
- case 0x40:
- e = "JOB_CONFIG_FAULT";
- break;
- case 0x41:
- e = "JOB_POWER_FAULT";
- break;
- case 0x42:
- e = "JOB_READ_FAULT";
- break;
- case 0x43:
- e = "JOB_WRITE_FAULT";
- break;
- case 0x44:
- e = "JOB_AFFINITY_FAULT";
- break;
- case 0x48:
- e = "JOB_BUS_FAULT";
- break;
- case 0x50:
- e = "INSTR_INVALID_PC";
- break;
- case 0x51:
- e = "INSTR_INVALID_ENC";
- break;
- case 0x52:
- e = "INSTR_TYPE_MISMATCH";
- break;
- case 0x53:
- e = "INSTR_OPERAND_FAULT";
- break;
- case 0x54:
- e = "INSTR_TLS_FAULT";
- break;
- case 0x55:
- e = "INSTR_BARRIER_FAULT";
- break;
- case 0x56:
- e = "INSTR_ALIGN_FAULT";
- break;
- case 0x58:
- e = "DATA_INVALID_FAULT";
- break;
- case 0x59:
- e = "TILE_RANGE_FAULT";
- break;
- case 0x5A:
- e = "ADDR_RANGE_FAULT";
- break;
- case 0x60:
- e = "OUT_OF_MEMORY";
- break;
- /* GPU exceptions */
- case 0x80:
- e = "DELAYED_BUS_FAULT";
- break;
- case 0x88:
- e = "SHAREABILITY_FAULT";
- break;
- /* MMU exceptions */
- case 0xC0:
- case 0xC1:
- case 0xC2:
- case 0xC3:
- case 0xC4:
- case 0xC5:
- case 0xC6:
- case 0xC7:
- e = "TRANSLATION_FAULT";
- break;
- case 0xC8:
- #ifdef CONFIG_MALI_GPU_MMU_AARCH64
- case 0xC9:
- case 0xCA:
- case 0xCB:
- case 0xCC:
- case 0xCD:
- case 0xCE:
- case 0xCF:
- #endif // ifdef CONFIG_MALI_GPU_MMU_AARCH64
- e = "PERMISSION_FAULT";
- break;
- case 0xD0:
- case 0xD1:
- case 0xD2:
- case 0xD3:
- case 0xD4:
- case 0xD5:
- case 0xD6:
- case 0xD7:
- e = "TRANSTAB_BUS_FAULT";
- break;
- case 0xD8:
- #ifdef CONFIG_MALI_GPU_MMU_AARCH64
- case 0xD9:
- case 0xDA:
- case 0xDB:
- case 0xDC:
- case 0xDD:
- case 0xDE:
- case 0xDF:
- #endif // ifdef CONFIG_MALI_GPU_MMU_AARCH64
- e = "ACCESS_FLAG";
- break;
- #ifdef CONFIG_MALI_GPU_MMU_AARCH64
- case 0xE0:
- case 0xE1:
- case 0xE2:
- case 0xE3:
- case 0xE4:
- case 0xE5:
- case 0xE6:
- case 0xE7:
- e = "ADDRESS_SIZE_FAULT";
- break;
- case 0xE8:
- case 0xE9:
- case 0xEA:
- case 0xEB:
- case 0xEC:
- case 0xED:
- case 0xEE:
- case 0xEF:
- e = "MEMORY_ATTRIBUTES_FAULT";
- #endif // ifdef CONFIG_MALI_GPU_MMU_AARCH64
- break;
- default:
- e = "UNKNOWN";
- break;
- };
- return e;
- }
- static const char *access_type_name(struct kbase_device *kbdev,
- u32 fault_status)
- {
- switch (fault_status & AS_FAULTSTATUS_ACCESS_TYPE_MASK) {
- case AS_FAULTSTATUS_ACCESS_TYPE_ATOMIC:
- #ifdef CONFIG_MALI_GPU_MMU_AARCH64
- return "ATOMIC";
- #else
- return "UNKNOWN";
- #endif
- case AS_FAULTSTATUS_ACCESS_TYPE_READ:
- return "READ";
- case AS_FAULTSTATUS_ACCESS_TYPE_WRITE:
- return "WRITE";
- case AS_FAULTSTATUS_ACCESS_TYPE_EX:
- return "EXECUTE";
- default:
- WARN_ON(1);
- return NULL;
- }
- }
- /**
- * The caller must ensure it's retained the ctx to prevent it from being scheduled out whilst it's being worked on.
- */
- static void kbase_mmu_report_fault_and_kill(struct kbase_context *kctx,
- struct kbase_as *as, const char *reason_str)
- {
- unsigned long flags;
- int exception_type;
- int access_type;
- int source_id;
- int as_no;
- struct kbase_device *kbdev;
- struct kbasep_js_device_data *js_devdata;
- #if KBASE_GPU_RESET_EN
- bool reset_status = false;
- #endif
- as_no = as->number;
- kbdev = kctx->kbdev;
- js_devdata = &kbdev->js_data;
- /* ASSERT that the context won't leave the runpool */
- KBASE_DEBUG_ASSERT(kbasep_js_debug_check_ctx_refcount(kbdev, kctx) > 0);
- /* decode the fault status */
- exception_type = as->fault_status & 0xFF;
- access_type = (as->fault_status >> 8) & 0x3;
- source_id = (as->fault_status >> 16);
- /* terminal fault, print info about the fault */
- dev_err(kbdev->dev,
- "Unhandled Page fault in AS%d at VA 0x%016llX\n"
- "Reason: %s\n"
- "raw fault status 0x%X\n"
- "decoded fault status: %s\n"
- "exception type 0x%X: %s\n"
- "access type 0x%X: %s\n"
- "source id 0x%X\n"
- "pid: %d\n",
- as_no, as->fault_addr,
- reason_str,
- as->fault_status,
- (as->fault_status & (1 << 10) ? "DECODER FAULT" : "SLAVE FAULT"),
- exception_type, kbase_exception_name(kbdev, exception_type),
- access_type, access_type_name(kbdev, as->fault_status),
- source_id,
- kctx->pid);
- /* hardware counters dump fault handling */
- if ((kbdev->hwcnt.kctx) && (kbdev->hwcnt.kctx->as_nr == as_no) &&
- (kbdev->hwcnt.backend.state ==
- KBASE_INSTR_STATE_DUMPING)) {
- unsigned int num_core_groups = kbdev->gpu_props.num_core_groups;
- if ((as->fault_addr >= kbdev->hwcnt.addr) &&
- (as->fault_addr < (kbdev->hwcnt.addr +
- (num_core_groups * 2048))))
- kbdev->hwcnt.backend.state = KBASE_INSTR_STATE_FAULT;
- }
- /* Stop the kctx from submitting more jobs and cause it to be scheduled
- * out/rescheduled - this will occur on releasing the context's refcount */
- spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
- kbasep_js_clear_submit_allowed(js_devdata, kctx);
- spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
- /* Kill any running jobs from the context. Submit is disallowed, so no more jobs from this
- * context can appear in the job slots from this point on */
- kbase_backend_jm_kill_jobs_from_kctx(kctx);
- /* AS transaction begin */
- mutex_lock(&as->transaction_mutex);
- #if KBASE_GPU_RESET_EN
- if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8245)) {
- /* Due to H/W issue 8245 we need to reset the GPU after using UNMAPPED mode.
- * We start the reset before switching to UNMAPPED to ensure that unrelated jobs
- * are evicted from the GPU before the switch.
- */
- dev_err(kbdev->dev, "Unhandled page fault. For this GPU version we now soft-reset the GPU as part of page fault recovery.");
- reset_status = kbase_prepare_to_reset_gpu(kbdev);
- }
- #endif // KBASE_GPU_RESET_EN
- /* switch to UNMAPPED mode, will abort all jobs and stop any hw counter dumping */
- kbase_mmu_disable_as(kbdev, as_no);
- mutex_unlock(&as->transaction_mutex);
- /* AS transaction end */
- /* Clear down the fault */
- kbase_mmu_hw_clear_fault(kbdev, as, kctx,
- KBASE_MMU_FAULT_TYPE_PAGE_UNEXPECTED);
- kbase_mmu_hw_enable_fault(kbdev, as, kctx,
- KBASE_MMU_FAULT_TYPE_PAGE_UNEXPECTED);
- #if KBASE_GPU_RESET_EN
- if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8245) && reset_status)
- kbase_reset_gpu(kbdev);
- #endif
- }
- void kbasep_as_do_poke(struct work_struct *work)
- {
- struct kbase_as *as;
- struct kbase_device *kbdev;
- struct kbase_context *kctx;
- unsigned long flags;
- KBASE_DEBUG_ASSERT(work);
- as = container_of(work, struct kbase_as, poke_work);
- kbdev = container_of(as, struct kbase_device, as[as->number]);
- KBASE_DEBUG_ASSERT(as->poke_state & KBASE_AS_POKE_STATE_IN_FLIGHT);
- /* GPU power will already be active by virtue of the caller holding a JS
- * reference on the address space, and will not release it until this worker
- * has finished */
- /* Further to the comment above, we know that while this function is running
- * the AS will not be released as before the atom is released this workqueue
- * is flushed (in kbase_as_poking_timer_release_atom)
- */
- kctx = kbasep_js_runpool_lookup_ctx_noretain(kbdev, as->number);
- /* AS transaction begin */
- mutex_lock(&as->transaction_mutex);
- /* Force a uTLB invalidate */
- kbase_mmu_hw_do_operation(kbdev, as, kctx, 0, 0,
- AS_COMMAND_UNLOCK, 0);
- mutex_unlock(&as->transaction_mutex);
- /* AS transaction end */
- spin_lock_irqsave(&kbdev->js_data.runpool_irq.lock, flags);
- if (as->poke_refcount &&
- !(as->poke_state & KBASE_AS_POKE_STATE_KILLING_POKE)) {
- /* Only queue up the timer if we need it, and we're not trying to kill it */
- hrtimer_start(&as->poke_timer, HR_TIMER_DELAY_MSEC(5), HRTIMER_MODE_REL);
- }
- spin_unlock_irqrestore(&kbdev->js_data.runpool_irq.lock, flags);
- }
- enum hrtimer_restart kbasep_as_poke_timer_callback(struct hrtimer *timer)
- {
- struct kbase_as *as;
- int queue_work_ret;
- KBASE_DEBUG_ASSERT(timer != NULL);
- as = container_of(timer, struct kbase_as, poke_timer);
- KBASE_DEBUG_ASSERT(as->poke_state & KBASE_AS_POKE_STATE_IN_FLIGHT);
- queue_work_ret = queue_work(as->poke_wq, &as->poke_work);
- KBASE_DEBUG_ASSERT(queue_work_ret);
- return HRTIMER_NORESTART;
- }
- /**
- * Retain the poking timer on an atom's context (if the atom hasn't already
- * done so), and start the timer (if it's not already started).
- *
- * This must only be called on a context that's scheduled in, and an atom
- * that's running on the GPU.
- *
- * The caller must hold kbasep_js_device_data::runpool_irq::lock
- *
- * This can be called safely from atomic context
- */
- void kbase_as_poking_timer_retain_atom(struct kbase_device *kbdev, struct kbase_context *kctx, struct kbase_jd_atom *katom)
- {
- struct kbase_as *as;
- KBASE_DEBUG_ASSERT(kbdev);
- KBASE_DEBUG_ASSERT(kctx);
- KBASE_DEBUG_ASSERT(katom);
- KBASE_DEBUG_ASSERT(kctx->as_nr != KBASEP_AS_NR_INVALID);
- lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
- if (katom->poking)
- return;
- katom->poking = 1;
- /* It's safe to work on the as/as_nr without an explicit reference,
- * because the caller holds the runpool_irq lock, and the atom itself
- * was also running and had already taken a reference */
- as = &kbdev->as[kctx->as_nr];
- if (++(as->poke_refcount) == 1) {
- /* First refcount for poke needed: check if not already in flight */
- if (!as->poke_state) {
- /* need to start poking */
- as->poke_state |= KBASE_AS_POKE_STATE_IN_FLIGHT;
- queue_work(as->poke_wq, &as->poke_work);
- }
- }
- }
- /**
- * If an atom holds a poking timer, release it and wait for it to finish
- *
- * This must only be called on a context that's scheduled in, and an atom
- * that still has a JS reference on the context
- *
- * This must \b not be called from atomic context, since it can sleep.
- */
- void kbase_as_poking_timer_release_atom(struct kbase_device *kbdev, struct kbase_context *kctx, struct kbase_jd_atom *katom)
- {
- struct kbase_as *as;
- unsigned long flags;
- KBASE_DEBUG_ASSERT(kbdev);
- KBASE_DEBUG_ASSERT(kctx);
- KBASE_DEBUG_ASSERT(katom);
- KBASE_DEBUG_ASSERT(kctx->as_nr != KBASEP_AS_NR_INVALID);
- if (!katom->poking)
- return;
- as = &kbdev->as[kctx->as_nr];
- spin_lock_irqsave(&kbdev->js_data.runpool_irq.lock, flags);
- KBASE_DEBUG_ASSERT(as->poke_refcount > 0);
- KBASE_DEBUG_ASSERT(as->poke_state & KBASE_AS_POKE_STATE_IN_FLIGHT);
- if (--(as->poke_refcount) == 0) {
- as->poke_state |= KBASE_AS_POKE_STATE_KILLING_POKE;
- spin_unlock_irqrestore(&kbdev->js_data.runpool_irq.lock, flags);
- hrtimer_cancel(&as->poke_timer);
- flush_workqueue(as->poke_wq);
- spin_lock_irqsave(&kbdev->js_data.runpool_irq.lock, flags);
- /* Re-check whether it's still needed */
- if (as->poke_refcount) {
- int queue_work_ret;
- /* Poking still needed:
- * - Another retain will not be starting the timer or queueing work,
- * because it's still marked as in-flight
- * - The hrtimer has finished, and has not started a new timer or
- * queued work because it's been marked as killing
- *
- * So whatever happens now, just queue the work again */
- as->poke_state &= ~((kbase_as_poke_state)KBASE_AS_POKE_STATE_KILLING_POKE);
- queue_work_ret = queue_work(as->poke_wq, &as->poke_work);
- KBASE_DEBUG_ASSERT(queue_work_ret);
- } else {
- /* It isn't - so mark it as not in flight, and not killing */
- as->poke_state = 0u;
- /* The poke associated with the atom has now finished. If this is
- * also the last atom on the context, then we can guarentee no more
- * pokes (and thus no more poking register accesses) will occur on
- * the context until new atoms are run */
- }
- }
- spin_unlock_irqrestore(&kbdev->js_data.runpool_irq.lock, flags);
- katom->poking = 0;
- }
- void kbase_mmu_interrupt_process(struct kbase_device *kbdev, struct kbase_context *kctx, struct kbase_as *as)
- {
- struct kbasep_js_device_data *js_devdata = &kbdev->js_data;
- lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
- if (!kctx) {
- dev_warn(kbdev->dev, "%s in AS%d at 0x%016llx with no context present! Suprious IRQ or SW Design Error?\n",
- kbase_as_has_bus_fault(as) ? "Bus error" : "Page fault",
- as->number, as->fault_addr);
- /* Since no ctx was found, the MMU must be disabled. */
- WARN_ON(as->current_setup.transtab);
- if (kbase_as_has_bus_fault(as)) {
- kbase_mmu_hw_clear_fault(kbdev, as, kctx,
- KBASE_MMU_FAULT_TYPE_BUS_UNEXPECTED);
- kbase_mmu_hw_enable_fault(kbdev, as, kctx,
- KBASE_MMU_FAULT_TYPE_BUS_UNEXPECTED);
- } else if (kbase_as_has_page_fault(as)) {
- kbase_mmu_hw_clear_fault(kbdev, as, kctx,
- KBASE_MMU_FAULT_TYPE_PAGE_UNEXPECTED);
- kbase_mmu_hw_enable_fault(kbdev, as, kctx,
- KBASE_MMU_FAULT_TYPE_PAGE_UNEXPECTED);
- }
- #if KBASE_GPU_RESET_EN
- if (kbase_as_has_bus_fault(as) &&
- kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8245)) {
- bool reset_status;
- /*
- * Reset the GPU, like in bus_fault_worker, in case an
- * earlier error hasn't been properly cleared by this
- * point.
- */
- dev_err(kbdev->dev, "GPU bus error occurred. For this GPU version we now soft-reset as part of bus error recovery\n");
- reset_status = kbase_prepare_to_reset_gpu_locked(kbdev);
- if (reset_status)
- kbase_reset_gpu_locked(kbdev);
- }
- #endif // KBASE_GPU_RESET_EN
- return;
- }
- if (kbase_as_has_bus_fault(as)) {
- /*
- * hw counters dumping in progress, signal the
- * other thread that it failed
- */
- if ((kbdev->hwcnt.kctx == kctx) &&
- (kbdev->hwcnt.backend.state ==
- KBASE_INSTR_STATE_DUMPING))
- kbdev->hwcnt.backend.state =
- KBASE_INSTR_STATE_FAULT;
- /*
- * Stop the kctx from submitting more jobs and cause it
- * to be scheduled out/rescheduled when all references
- * to it are released
- */
- kbasep_js_clear_submit_allowed(js_devdata, kctx);
- #ifdef CONFIG_MALI_GPU_MMU_AARCH64
- dev_warn(kbdev->dev,
- "Bus error in AS%d at VA=0x%016llx, IPA=0x%016llx\n",
- as->number, as->fault_addr,
- as->fault_extra_addr);
- #else // ifdef CONFIG_MALI_GPU_MMU_AARCH64
- dev_warn(kbdev->dev, "Bus error in AS%d at 0x%016llx\n",
- as->number, as->fault_addr);
- #endif
- /*
- * We need to switch to UNMAPPED mode - but we do this in a
- * worker so that we can sleep
- */
- KBASE_DEBUG_ASSERT(object_is_on_stack(&as->work_busfault) == 0);
- WARN_ON(work_pending(&as->work_busfault));
- queue_work(as->pf_wq, &as->work_busfault);
- atomic_inc(&kbdev->faults_pending);
- } else {
- KBASE_DEBUG_ASSERT(object_is_on_stack(&as->work_pagefault) == 0);
- WARN_ON(work_pending(&as->work_pagefault));
- queue_work(as->pf_wq, &as->work_pagefault);
- atomic_inc(&kbdev->faults_pending);
- }
- }
- void kbase_flush_mmu_wqs(struct kbase_device *kbdev)
- {
- int i;
- for (i = 0; i < kbdev->nr_hw_address_spaces; i++) {
- struct kbase_as *as = &kbdev->as[i];
- flush_workqueue(as->pf_wq);
- }
- }
|