Path: blob/master/drivers/accel/habanalabs/common/mmu/mmu_v2_hr.c
26488 views
// SPDX-License-Identifier: GPL-2.012/*3* Copyright 2020-2022 HabanaLabs, Ltd.4* All Rights Reserved.5*/67#include "../habanalabs.h"8#include "../../include/hw_ip/mmu/mmu_general.h"910#include <linux/slab.h>1112static struct pgt_info *hl_mmu_v2_hr_get_pgt_info(struct hl_ctx *ctx, u64 phys_hop_addr)13{14struct pgt_info *pgt_info = NULL;1516hash_for_each_possible(ctx->hr_mmu_phys_hash, pgt_info, node,17(unsigned long) phys_hop_addr)18if (phys_hop_addr == pgt_info->phys_addr)19break;2021return pgt_info;22}2324static void hl_mmu_v2_hr_add_pgt_info(struct hl_ctx *ctx, struct pgt_info *pgt_info,25dma_addr_t phys_addr)26{27hash_add(ctx->hr_mmu_phys_hash, &pgt_info->node, phys_addr);28}2930static struct pgt_info *hl_mmu_v2_hr_get_hop0_pgt_info(struct hl_ctx *ctx)31{32return &ctx->hdev->mmu_priv.hr.mmu_asid_hop0[ctx->asid];33}3435/**36* hl_mmu_v2_hr_init() - initialize the MMU module.37* @hdev: habanalabs device structure.38*39* This function does the following:40* - Create a pool of pages for pgt_infos.41* - Create a shadow table for pgt42*43* Return: 0 for success, non-zero for failure.44*/45static inline int hl_mmu_v2_hr_init(struct hl_device *hdev)46{47struct asic_fixed_properties *prop = &hdev->asic_prop;4849return hl_mmu_hr_init(hdev, &hdev->mmu_priv.hr, prop->pmmu.hop_table_size,50prop->mmu_pgt_size);51}5253/**54* hl_mmu_v2_hr_fini() - release the MMU module.55* @hdev: habanalabs device structure.56*57* This function does the following:58* - Disable MMU in H/W.59* - Free the pgt_infos pool.60*61* All contexts should be freed before calling this function.62*/63static inline void hl_mmu_v2_hr_fini(struct hl_device *hdev)64{65struct asic_fixed_properties *prop = &hdev->asic_prop;6667hl_mmu_hr_fini(hdev, &hdev->mmu_priv.hr, prop->pmmu.hop_table_size);68}6970/**71* hl_mmu_v2_hr_ctx_init() - initialize a context for using the MMU module.72* @ctx: pointer to the context structure to initialize.73*74* Initialize a mutex to protect the concurrent mapping flow, a hash to hold all75* page tables hops related to this context.76* Return: 0 on success, non-zero otherwise.77*/78static int hl_mmu_v2_hr_ctx_init(struct hl_ctx *ctx)79{80hash_init(ctx->hr_mmu_phys_hash);81return 0;82}8384/*85* hl_mmu_v2_hr_ctx_fini - disable a ctx from using the mmu module86*87* @ctx: pointer to the context structure88*89* This function does the following:90* - Free any pgts which were not freed yet91* - Free the mutex92* - Free DRAM default page mapping hops93*/94static void hl_mmu_v2_hr_ctx_fini(struct hl_ctx *ctx)95{96struct hl_device *hdev = ctx->hdev;97struct pgt_info *pgt_info;98struct hlist_node *tmp;99int i;100101if (!hash_empty(ctx->hr_mmu_phys_hash))102dev_err(hdev->dev, "ctx %d is freed while it has pgts in use\n",103ctx->asid);104105hash_for_each_safe(ctx->hr_mmu_phys_hash, i, tmp, pgt_info, node) {106dev_err_ratelimited(hdev->dev,107"pgt_info of addr 0x%llx of asid %d was not destroyed, num_ptes: %d\n",108pgt_info->phys_addr, ctx->asid, pgt_info->num_of_ptes);109hl_mmu_hr_free_hop_remove_pgt(pgt_info, &ctx->hdev->mmu_priv.hr,110ctx->hdev->asic_prop.pmmu.hop_table_size);111}112}113114static int _hl_mmu_v2_hr_unmap(struct hl_ctx *ctx,115u64 virt_addr, bool is_dram_addr)116{117u64 curr_pte, scrambled_virt_addr, hop_pte_phys_addr[MMU_ARCH_6_HOPS] = { 0 };118struct pgt_info *hops_pgt_info[MMU_ARCH_6_HOPS] = { NULL };119struct hl_device *hdev = ctx->hdev;120struct asic_fixed_properties *prop;121struct hl_mmu_properties *mmu_prop;122bool is_huge = false;123int i, hop_last;124125prop = &hdev->asic_prop;126127/* shifts and masks are the same in PMMU and HMMU, use one of them */128mmu_prop = is_dram_addr ? &prop->dmmu : &prop->pmmu;129hop_last = mmu_prop->num_hops - 1;130131scrambled_virt_addr = hdev->asic_funcs->scramble_addr(hdev, virt_addr);132curr_pte = 0;133134for (i = 0 ; i < mmu_prop->num_hops ; i++) {135/* we get HOP0 differently, it doesn't need curr_pte */136if (i == 0)137hops_pgt_info[i] = hl_mmu_v2_hr_get_hop0_pgt_info(ctx);138else139hops_pgt_info[i] = hl_mmu_hr_get_next_hop_pgt_info(ctx,140&ctx->hdev->mmu_func[MMU_HR_PGT].hr_funcs, curr_pte);141if (!hops_pgt_info[i])142goto not_mapped;143144hop_pte_phys_addr[i] = hl_mmu_get_hop_pte_phys_addr(ctx, mmu_prop, i,145hops_pgt_info[i]->phys_addr,146scrambled_virt_addr);147if (hop_pte_phys_addr[i] == U64_MAX)148return -EFAULT;149150curr_pte = *(u64 *) (uintptr_t) hl_mmu_hr_pte_phys_to_virt(ctx, hops_pgt_info[i],151hop_pte_phys_addr[i],152ctx->hdev->asic_prop.pmmu.hop_table_size);153154if ((i < hop_last) && (curr_pte & mmu_prop->last_mask)) {155hop_last = i;156is_huge = true;157break;158}159}160161if (is_dram_addr && !is_huge) {162dev_err(hdev->dev, "DRAM unmapping should use huge pages only\n");163return -EFAULT;164}165166if (!(curr_pte & PAGE_PRESENT_MASK))167goto not_mapped;168169for (i = hop_last ; i > 0 ; i--) {170hl_mmu_hr_clear_pte(ctx, hops_pgt_info[i], hop_pte_phys_addr[i],171ctx->hdev->asic_prop.pmmu.hop_table_size);172173if (hl_mmu_hr_put_pte(ctx, hops_pgt_info[i], &ctx->hdev->mmu_priv.hr,174ctx->hdev->asic_prop.pmmu.hop_table_size))175goto mapped;176}177hl_mmu_hr_clear_pte(ctx, hops_pgt_info[0], hop_pte_phys_addr[0],178ctx->hdev->asic_prop.pmmu.hop_table_size);179180mapped:181return 0;182183not_mapped:184dev_err(hdev->dev, "virt addr 0x%llx is not mapped to phys addr\n", virt_addr);185186return -EINVAL;187}188189static int hl_mmu_v2_get_last_hop(struct hl_mmu_properties *mmu_prop, u32 page_size)190{191int hop;192193for (hop = (mmu_prop->num_hops - 1); hop; hop--) {194if (mmu_prop->hop_shifts[hop] == 0)195continue;196197if (page_size <= (1 << mmu_prop->hop_shifts[hop]))198break;199}200201return hop;202}203204static int _hl_mmu_v2_hr_map(struct hl_ctx *ctx,205u64 virt_addr, u64 phys_addr,206u32 page_size, bool is_dram_addr)207{208u64 hop_pte_phys_addr[MMU_ARCH_6_HOPS] = { 0 },209curr_pte = 0, scrambled_virt_addr, scrambled_phys_addr;210struct pgt_info *hops_pgt_info[MMU_ARCH_6_HOPS] = { NULL };211bool hop_new[MMU_ARCH_6_HOPS] = { false };212struct hl_device *hdev = ctx->hdev;213struct asic_fixed_properties *prop = &hdev->asic_prop;214struct hl_mmu_properties *mmu_prop;215int i, hop_last, rc = -ENOMEM;216217/*218* This mapping function can map a page or a huge page. For huge page219* there are only 4 hops rather than 5. Currently the DRAM allocation220* uses huge pages only but user memory could have been allocated with221* one of the two page sizes. Since this is a common code for all the222* three cases, we need this hugs page check.223*/224if (is_dram_addr)225mmu_prop = &prop->dmmu;226else if (page_size == prop->pmmu_huge.page_size)227mmu_prop = &prop->pmmu_huge;228else229mmu_prop = &prop->pmmu;230231hop_last = hl_mmu_v2_get_last_hop(mmu_prop, page_size);232if (hop_last <= 0) {233dev_err(ctx->hdev->dev, "Invalid last HOP %d\n", hop_last);234return -EFAULT;235}236237scrambled_virt_addr = hdev->asic_funcs->scramble_addr(hdev, virt_addr);238scrambled_phys_addr = hdev->asic_funcs->scramble_addr(hdev, phys_addr);239240for (i = 0 ; i <= hop_last ; i++) {241242if (i == 0)243hops_pgt_info[i] = hl_mmu_v2_hr_get_hop0_pgt_info(ctx);244else245hops_pgt_info[i] = hl_mmu_hr_get_alloc_next_hop(ctx,246&ctx->hdev->mmu_priv.hr,247&ctx->hdev->mmu_func[MMU_HR_PGT].hr_funcs,248mmu_prop, curr_pte, &hop_new[i]);249if (!hops_pgt_info[i])250goto err;251252hop_pte_phys_addr[i] = hl_mmu_get_hop_pte_phys_addr(ctx, mmu_prop, i,253hops_pgt_info[i]->phys_addr,254scrambled_virt_addr);255curr_pte = *(u64 *) (uintptr_t) hl_mmu_hr_pte_phys_to_virt(ctx, hops_pgt_info[i],256hop_pte_phys_addr[i],257ctx->hdev->asic_prop.pmmu.hop_table_size);258}259260if (curr_pte & PAGE_PRESENT_MASK) {261dev_err(hdev->dev, "mapping already exists for virt_addr 0x%llx\n",262scrambled_virt_addr);263264for (i = 0 ; i <= hop_last ; i++)265dev_dbg(hdev->dev, "hop%d pte: 0x%llx (0x%llx)\n",266i,267*(u64 *) (uintptr_t)268hl_mmu_hr_pte_phys_to_virt(ctx, hops_pgt_info[i],269hop_pte_phys_addr[i],270ctx->hdev->asic_prop.pmmu.hop_table_size),271hop_pte_phys_addr[i]);272rc = -EINVAL;273goto err;274}275276curr_pte = (scrambled_phys_addr & HOP_PHYS_ADDR_MASK) | mmu_prop->last_mask277| PAGE_PRESENT_MASK;278279/* Write the PTEs */280hl_mmu_hr_write_pte(ctx, hops_pgt_info[hop_last], hop_pte_phys_addr[hop_last], curr_pte,281ctx->hdev->asic_prop.pmmu.hop_table_size);282283/* for each new hop, add its address to the table of previous-hop */284for (i = 1 ; i <= hop_last ; i++) {285if (hop_new[i]) {286curr_pte = (hops_pgt_info[i]->phys_addr & HOP_PHYS_ADDR_MASK) |287PAGE_PRESENT_MASK;288hl_mmu_hr_write_pte(ctx, hops_pgt_info[i - 1], hop_pte_phys_addr[i - 1],289curr_pte, ctx->hdev->asic_prop.pmmu.hop_table_size);290if (i - 1)291hl_mmu_hr_get_pte(ctx, &ctx->hdev->mmu_func[MMU_HR_PGT].hr_funcs,292hops_pgt_info[i - 1]->phys_addr);293}294}295296hl_mmu_hr_get_pte(ctx, &ctx->hdev->mmu_func[MMU_HR_PGT].hr_funcs,297hops_pgt_info[hop_last]->phys_addr);298299return 0;300301err:302for (i = 1 ; i <= hop_last ; i++)303if (hop_new[i] && hops_pgt_info[i])304hl_mmu_hr_free_hop_remove_pgt(hops_pgt_info[i], &ctx->hdev->mmu_priv.hr,305ctx->hdev->asic_prop.pmmu.hop_table_size);306307return rc;308}309310/*311* hl_mmu_v2_swap_out - marks all mapping of the given ctx as swapped out312*313* @ctx: pointer to the context structure314*315*/316static void hl_mmu_v2_hr_swap_out(struct hl_ctx *ctx)317{318319}320321/*322* hl_mmu_v2_swap_in - marks all mapping of the given ctx as swapped in323*324* @ctx: pointer to the context structure325*326*/327static void hl_mmu_v2_hr_swap_in(struct hl_ctx *ctx)328{329330}331332static int hl_mmu_v2_hr_get_tlb_mapping_params(struct hl_device *hdev,333struct hl_mmu_properties **mmu_prop,334struct hl_mmu_hop_info *hops,335u64 virt_addr, bool *is_huge)336{337struct asic_fixed_properties *prop = &hdev->asic_prop;338bool is_dram_addr, is_pmmu_addr, is_pmmu_h_addr;339340is_dram_addr = hl_mem_area_inside_range(virt_addr, prop->dmmu.page_size,341prop->dmmu.start_addr,342prop->dmmu.end_addr);343is_pmmu_addr = hl_mem_area_inside_range(virt_addr, prop->pmmu.page_size,344prop->pmmu.start_addr,345prop->pmmu.end_addr);346is_pmmu_h_addr = hl_mem_area_inside_range(virt_addr,347prop->pmmu_huge.page_size,348prop->pmmu_huge.start_addr,349prop->pmmu_huge.end_addr);350if (is_dram_addr) {351*mmu_prop = &prop->dmmu;352*is_huge = true;353hops->range_type = HL_VA_RANGE_TYPE_DRAM;354} else if (is_pmmu_addr) {355*mmu_prop = &prop->pmmu;356*is_huge = false;357hops->range_type = HL_VA_RANGE_TYPE_HOST;358} else if (is_pmmu_h_addr) {359*mmu_prop = &prop->pmmu_huge;360*is_huge = true;361hops->range_type = HL_VA_RANGE_TYPE_HOST_HUGE;362} else {363return -EINVAL;364}365366return 0;367}368369static int hl_mmu_v2_hr_get_tlb_info(struct hl_ctx *ctx, u64 virt_addr,370struct hl_mmu_hop_info *hops)371{372return hl_mmu_hr_get_tlb_info(ctx, virt_addr, hops,373&ctx->hdev->mmu_func[MMU_HR_PGT].hr_funcs);374}375376/*377* hl_mmu_v2_prepare - prepare mmu_if for working with mmu v2378*379* @hdev: pointer to the device structure380* @mmu_if: pointer to the mmu interface structure381*/382void hl_mmu_v2_hr_set_funcs(struct hl_device *hdev, struct hl_mmu_funcs *mmu)383{384mmu->init = hl_mmu_v2_hr_init;385mmu->fini = hl_mmu_v2_hr_fini;386mmu->ctx_init = hl_mmu_v2_hr_ctx_init;387mmu->ctx_fini = hl_mmu_v2_hr_ctx_fini;388mmu->map = _hl_mmu_v2_hr_map;389mmu->unmap = _hl_mmu_v2_hr_unmap;390mmu->flush = hl_mmu_hr_flush;391mmu->swap_out = hl_mmu_v2_hr_swap_out;392mmu->swap_in = hl_mmu_v2_hr_swap_in;393mmu->get_tlb_info = hl_mmu_v2_hr_get_tlb_info;394mmu->hr_funcs.get_hop0_pgt_info = hl_mmu_v2_hr_get_hop0_pgt_info;395mmu->hr_funcs.get_pgt_info = hl_mmu_v2_hr_get_pgt_info;396mmu->hr_funcs.add_pgt_info = hl_mmu_v2_hr_add_pgt_info;397mmu->hr_funcs.get_tlb_mapping_params = hl_mmu_v2_hr_get_tlb_mapping_params;398}399400401