habanalabs: separate common code to dedicated folders
authorOfir Bitton <obitton@habana.ai>
Mon, 28 Dec 2020 12:36:47 +0000 (14:36 +0200)
committerOded Gabbay <ogabbay@kernel.org>
Wed, 27 Jan 2021 19:03:50 +0000 (21:03 +0200)
We separate some of the common code source files to different
folders for a better maintainability and testability.

Signed-off-by: Ofir Bitton <obitton@habana.ai>
Reviewed-by: Oded Gabbay <ogabbay@kernel.org>
Signed-off-by: Oded Gabbay <ogabbay@kernel.org>
drivers/misc/habanalabs/common/Makefile
drivers/misc/habanalabs/common/mmu.c [deleted file]
drivers/misc/habanalabs/common/mmu/Makefile [new file with mode: 0644]
drivers/misc/habanalabs/common/mmu/mmu.c [new file with mode: 0644]
drivers/misc/habanalabs/common/mmu/mmu_v1.c [new file with mode: 0644]
drivers/misc/habanalabs/common/mmu_v1.c [deleted file]
drivers/misc/habanalabs/common/pci.c [deleted file]
drivers/misc/habanalabs/common/pci/Makefile [new file with mode: 0644]
drivers/misc/habanalabs/common/pci/pci.c [new file with mode: 0644]

index eccd8c7dc62d3bb9cc5da7271d80777fa89df1f5..5d8b48288cf4d6924fb4dbc80b700f6ebd62afba 100644 (file)
@@ -1,7 +1,13 @@
 # SPDX-License-Identifier: GPL-2.0-only
+
+include $(src)/common/mmu/Makefile
+habanalabs-y += $(HL_COMMON_MMU_FILES)
+
+include $(src)/common/pci/Makefile
+habanalabs-y += $(HL_COMMON_PCI_FILES)
+
 HL_COMMON_FILES := common/habanalabs_drv.o common/device.o common/context.o \
                common/asid.o common/habanalabs_ioctl.o \
                common/command_buffer.o common/hw_queue.o common/irq.o \
                common/sysfs.o common/hwmon.o common/memory.o \
-               common/command_submission.o common/mmu.o common/mmu_v1.o \
-               common/firmware_if.o common/pci.o
+               common/command_submission.o common/firmware_if.o
diff --git a/drivers/misc/habanalabs/common/mmu.c b/drivers/misc/habanalabs/common/mmu.c
deleted file mode 100644 (file)
index 38234c2..0000000
+++ /dev/null
@@ -1,560 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-
-/*
- * Copyright 2016-2020 HabanaLabs, Ltd.
- * All Rights Reserved.
- */
-
-#include <linux/slab.h>
-
-#include "habanalabs.h"
-
-bool hl_is_dram_va(struct hl_device *hdev, u64 virt_addr)
-{
-       struct asic_fixed_properties *prop = &hdev->asic_prop;
-
-       return hl_mem_area_inside_range(virt_addr, prop->dmmu.page_size,
-                                       prop->dmmu.start_addr,
-                                       prop->dmmu.end_addr);
-}
-
-/**
- * hl_mmu_init() - initialize the MMU module.
- * @hdev: habanalabs device structure.
- *
- * Return: 0 for success, non-zero for failure.
- */
-int hl_mmu_init(struct hl_device *hdev)
-{
-       int rc = -EOPNOTSUPP;
-
-       if (!hdev->mmu_enable)
-               return 0;
-
-       if (hdev->mmu_func[MMU_DR_PGT].init != NULL) {
-               rc = hdev->mmu_func[MMU_DR_PGT].init(hdev);
-               if (rc)
-                       return rc;
-       }
-
-       if (hdev->mmu_func[MMU_HR_PGT].init != NULL)
-               rc = hdev->mmu_func[MMU_HR_PGT].init(hdev);
-
-       return rc;
-}
-
-/**
- * hl_mmu_fini() - release the MMU module.
- * @hdev: habanalabs device structure.
- *
- * This function does the following:
- * - Disable MMU in H/W.
- * - Free the pgt_infos pool.
- *
- * All contexts should be freed before calling this function.
- */
-void hl_mmu_fini(struct hl_device *hdev)
-{
-       if (!hdev->mmu_enable)
-               return;
-
-       if (hdev->mmu_func[MMU_DR_PGT].fini != NULL)
-               hdev->mmu_func[MMU_DR_PGT].fini(hdev);
-
-       if (hdev->mmu_func[MMU_HR_PGT].fini != NULL)
-               hdev->mmu_func[MMU_HR_PGT].fini(hdev);
-}
-
-/**
- * hl_mmu_ctx_init() - initialize a context for using the MMU module.
- * @ctx: pointer to the context structure to initialize.
- *
- * Initialize a mutex to protect the concurrent mapping flow, a hash to hold all
- * page tables hops related to this context.
- * Return: 0 on success, non-zero otherwise.
- */
-int hl_mmu_ctx_init(struct hl_ctx *ctx)
-{
-       struct hl_device *hdev = ctx->hdev;
-       int rc = -EOPNOTSUPP;
-
-       if (!hdev->mmu_enable)
-               return 0;
-
-       mutex_init(&ctx->mmu_lock);
-
-       if (hdev->mmu_func[MMU_DR_PGT].ctx_init != NULL) {
-               rc = hdev->mmu_func[MMU_DR_PGT].ctx_init(ctx);
-               if (rc)
-                       return rc;
-       }
-
-       if (hdev->mmu_func[MMU_HR_PGT].ctx_init != NULL)
-               rc = hdev->mmu_func[MMU_HR_PGT].ctx_init(ctx);
-
-       return rc;
-}
-
-/*
- * hl_mmu_ctx_fini - disable a ctx from using the mmu module
- *
- * @ctx: pointer to the context structure
- *
- * This function does the following:
- * - Free any pgts which were not freed yet
- * - Free the mutex
- * - Free DRAM default page mapping hops
- */
-void hl_mmu_ctx_fini(struct hl_ctx *ctx)
-{
-       struct hl_device *hdev = ctx->hdev;
-
-       if (!hdev->mmu_enable)
-               return;
-
-       if (hdev->mmu_func[MMU_DR_PGT].ctx_fini != NULL)
-               hdev->mmu_func[MMU_DR_PGT].ctx_fini(ctx);
-
-       if (hdev->mmu_func[MMU_HR_PGT].ctx_fini != NULL)
-               hdev->mmu_func[MMU_HR_PGT].ctx_fini(ctx);
-
-       mutex_destroy(&ctx->mmu_lock);
-}
-
-/*
- * hl_mmu_unmap_page - unmaps a virtual addr
- *
- * @ctx: pointer to the context structure
- * @virt_addr: virt addr to map from
- * @page_size: size of the page to unmap
- * @flush_pte: whether to do a PCI flush
- *
- * This function does the following:
- * - Check that the virt addr is mapped
- * - Unmap the virt addr and frees pgts if possible
- * - Returns 0 on success, -EINVAL if the given addr is not mapped
- *
- * Because this function changes the page tables in the device and because it
- * changes the MMU hash, it must be protected by a lock.
- * However, because it maps only a single page, the lock should be implemented
- * in a higher level in order to protect the entire mapping of the memory area
- *
- * For optimization reasons PCI flush may be requested once after unmapping of
- * large area.
- */
-int hl_mmu_unmap_page(struct hl_ctx *ctx, u64 virt_addr, u32 page_size,
-               bool flush_pte)
-{
-       struct hl_device *hdev = ctx->hdev;
-       struct asic_fixed_properties *prop = &hdev->asic_prop;
-       struct hl_mmu_properties *mmu_prop;
-       u64 real_virt_addr;
-       u32 real_page_size, npages;
-       int i, rc = 0, pgt_residency;
-       bool is_dram_addr;
-
-       if (!hdev->mmu_enable)
-               return 0;
-
-       is_dram_addr = hl_is_dram_va(hdev, virt_addr);
-
-       if (is_dram_addr)
-               mmu_prop = &prop->dmmu;
-       else if ((page_size % prop->pmmu_huge.page_size) == 0)
-               mmu_prop = &prop->pmmu_huge;
-       else
-               mmu_prop = &prop->pmmu;
-
-       pgt_residency = mmu_prop->host_resident ? MMU_HR_PGT : MMU_DR_PGT;
-       /*
-        * The H/W handles mapping of specific page sizes. Hence if the page
-        * size is bigger, we break it to sub-pages and unmap them separately.
-        */
-       if ((page_size % mmu_prop->page_size) == 0) {
-               real_page_size = mmu_prop->page_size;
-       } else {
-               /*
-                * MMU page size may differ from DRAM page size.
-                * In such case work with the DRAM page size and let the MMU
-                * scrambling routine to handle this mismatch when
-                * calculating the address to remove from the MMU page table
-                */
-               if (is_dram_addr && ((page_size % prop->dram_page_size) == 0)) {
-                       real_page_size = prop->dram_page_size;
-               } else {
-                       dev_err(hdev->dev,
-                               "page size of %u is not %uKB aligned, can't unmap\n",
-                               page_size, mmu_prop->page_size >> 10);
-
-                       return -EFAULT;
-               }
-       }
-
-       npages = page_size / real_page_size;
-       real_virt_addr = virt_addr;
-
-       for (i = 0 ; i < npages ; i++) {
-               rc = hdev->mmu_func[pgt_residency].unmap(ctx,
-                                               real_virt_addr, is_dram_addr);
-               if (rc)
-                       break;
-
-               real_virt_addr += real_page_size;
-       }
-
-       if (flush_pte)
-               hdev->mmu_func[pgt_residency].flush(ctx);
-
-       return rc;
-}
-
-/*
- * hl_mmu_map_page - maps a virtual addr to physical addr
- *
- * @ctx: pointer to the context structure
- * @virt_addr: virt addr to map from
- * @phys_addr: phys addr to map to
- * @page_size: physical page size
- * @flush_pte: whether to do a PCI flush
- *
- * This function does the following:
- * - Check that the virt addr is not mapped
- * - Allocate pgts as necessary in order to map the virt addr to the phys
- * - Returns 0 on success, -EINVAL if addr is already mapped, or -ENOMEM.
- *
- * Because this function changes the page tables in the device and because it
- * changes the MMU hash, it must be protected by a lock.
- * However, because it maps only a single page, the lock should be implemented
- * in a higher level in order to protect the entire mapping of the memory area
- *
- * For optimization reasons PCI flush may be requested once after mapping of
- * large area.
- */
-int hl_mmu_map_page(struct hl_ctx *ctx, u64 virt_addr, u64 phys_addr,
-               u32 page_size, bool flush_pte)
-{
-       struct hl_device *hdev = ctx->hdev;
-       struct asic_fixed_properties *prop = &hdev->asic_prop;
-       struct hl_mmu_properties *mmu_prop;
-       u64 real_virt_addr, real_phys_addr;
-       u32 real_page_size, npages;
-       int i, rc, pgt_residency, mapped_cnt = 0;
-       bool is_dram_addr;
-
-
-       if (!hdev->mmu_enable)
-               return 0;
-
-       is_dram_addr = hl_is_dram_va(hdev, virt_addr);
-
-       if (is_dram_addr)
-               mmu_prop = &prop->dmmu;
-       else if ((page_size % prop->pmmu_huge.page_size) == 0)
-               mmu_prop = &prop->pmmu_huge;
-       else
-               mmu_prop = &prop->pmmu;
-
-       pgt_residency = mmu_prop->host_resident ? MMU_HR_PGT : MMU_DR_PGT;
-
-       /*
-        * The H/W handles mapping of specific page sizes. Hence if the page
-        * size is bigger, we break it to sub-pages and map them separately.
-        */
-       if ((page_size % mmu_prop->page_size) == 0) {
-               real_page_size = mmu_prop->page_size;
-       } else if (is_dram_addr && ((page_size % prop->dram_page_size) == 0) &&
-                       (prop->dram_page_size < mmu_prop->page_size)) {
-               /*
-                * MMU page size may differ from DRAM page size.
-                * In such case work with the DRAM page size and let the MMU
-                * scrambling routine handle this mismatch when calculating
-                * the address to place in the MMU page table. (in that case
-                * also make sure that the dram_page_size smaller than the
-                * mmu page size)
-                */
-               real_page_size = prop->dram_page_size;
-       } else {
-               dev_err(hdev->dev,
-                       "page size of %u is not %uKB aligned, can't map\n",
-                       page_size, mmu_prop->page_size >> 10);
-
-               return -EFAULT;
-       }
-
-       /*
-        * Verify that the phys and virt addresses are aligned with the
-        * MMU page size (in dram this means checking the address and MMU
-        * after scrambling)
-        */
-       if ((is_dram_addr &&
-                       ((hdev->asic_funcs->scramble_vaddr(hdev, phys_addr) &
-                               (mmu_prop->page_size - 1)) ||
-                       (hdev->asic_funcs->scramble_vaddr(hdev, virt_addr) &
-                               (mmu_prop->page_size - 1)))) ||
-               (!is_dram_addr && ((phys_addr & (real_page_size - 1)) ||
-                               (virt_addr & (real_page_size - 1)))))
-               dev_crit(hdev->dev,
-                       "Mapping address 0x%llx with virtual address 0x%llx and page size of 0x%x is erroneous! Addresses must be divisible by page size",
-                       phys_addr, virt_addr, real_page_size);
-
-       npages = page_size / real_page_size;
-       real_virt_addr = virt_addr;
-       real_phys_addr = phys_addr;
-
-       for (i = 0 ; i < npages ; i++) {
-               rc = hdev->mmu_func[pgt_residency].map(ctx,
-                                               real_virt_addr, real_phys_addr,
-                                               real_page_size, is_dram_addr);
-               if (rc)
-                       goto err;
-
-               real_virt_addr += real_page_size;
-               real_phys_addr += real_page_size;
-               mapped_cnt++;
-       }
-
-       if (flush_pte)
-               hdev->mmu_func[pgt_residency].flush(ctx);
-
-       return 0;
-
-err:
-       real_virt_addr = virt_addr;
-       for (i = 0 ; i < mapped_cnt ; i++) {
-               if (hdev->mmu_func[pgt_residency].unmap(ctx,
-                                               real_virt_addr, is_dram_addr))
-                       dev_warn_ratelimited(hdev->dev,
-                               "failed to unmap va: 0x%llx\n", real_virt_addr);
-
-               real_virt_addr += real_page_size;
-       }
-
-       hdev->mmu_func[pgt_residency].flush(ctx);
-
-       return rc;
-}
-
-/*
- * hl_mmu_map_contiguous - implements a wrapper for hl_mmu_map_page
- *                         for mapping contiguous physical memory
- *
- * @ctx: pointer to the context structure
- * @virt_addr: virt addr to map from
- * @phys_addr: phys addr to map to
- * @size: size to map
- *
- */
-int hl_mmu_map_contiguous(struct hl_ctx *ctx, u64 virt_addr,
-                                       u64 phys_addr, u32 size)
-{
-       struct hl_device *hdev = ctx->hdev;
-       struct asic_fixed_properties *prop = &hdev->asic_prop;
-       u64 curr_va, curr_pa;
-       u32 page_size;
-       bool flush_pte;
-       int rc = 0, off;
-
-       if (hl_mem_area_inside_range(virt_addr, size,
-                       prop->dmmu.start_addr, prop->dmmu.end_addr))
-               page_size = prop->dmmu.page_size;
-       else if (hl_mem_area_inside_range(virt_addr, size,
-                       prop->pmmu.start_addr, prop->pmmu.end_addr))
-               page_size = prop->pmmu.page_size;
-       else if (hl_mem_area_inside_range(virt_addr, size,
-                       prop->pmmu_huge.start_addr, prop->pmmu_huge.end_addr))
-               page_size = prop->pmmu_huge.page_size;
-       else
-               return -EINVAL;
-
-       for (off = 0 ; off < size ; off += page_size) {
-               curr_va = virt_addr + off;
-               curr_pa = phys_addr + off;
-               flush_pte = (off + page_size) >= size;
-               rc = hl_mmu_map_page(ctx, curr_va, curr_pa, page_size,
-                                                               flush_pte);
-               if (rc) {
-                       dev_err(hdev->dev,
-                               "Map failed for va 0x%llx to pa 0x%llx\n",
-                               curr_va, curr_pa);
-                       goto unmap;
-               }
-       }
-
-       return rc;
-
-unmap:
-       for (; off >= 0 ; off -= page_size) {
-               curr_va = virt_addr + off;
-               flush_pte = (off - (s32) page_size) < 0;
-               if (hl_mmu_unmap_page(ctx, curr_va, page_size, flush_pte))
-                       dev_warn_ratelimited(hdev->dev,
-                               "failed to unmap va 0x%llx\n", curr_va);
-       }
-
-       return rc;
-}
-
-/*
- * hl_mmu_unmap_contiguous - implements a wrapper for hl_mmu_unmap_page
- *                           for unmapping contiguous physical memory
- *
- * @ctx: pointer to the context structure
- * @virt_addr: virt addr to unmap
- * @size: size to unmap
- *
- */
-int hl_mmu_unmap_contiguous(struct hl_ctx *ctx, u64 virt_addr, u32 size)
-{
-       struct hl_device *hdev = ctx->hdev;
-       struct asic_fixed_properties *prop = &hdev->asic_prop;
-       u64 curr_va;
-       u32 page_size;
-       bool flush_pte;
-       int rc = 0, off;
-
-       if (hl_mem_area_inside_range(virt_addr, size,
-                       prop->dmmu.start_addr, prop->dmmu.end_addr))
-               page_size = prop->dmmu.page_size;
-       else if (hl_mem_area_inside_range(virt_addr, size,
-                       prop->pmmu.start_addr, prop->pmmu.end_addr))
-               page_size = prop->pmmu.page_size;
-       else if (hl_mem_area_inside_range(virt_addr, size,
-                       prop->pmmu_huge.start_addr, prop->pmmu_huge.end_addr))
-               page_size = prop->pmmu_huge.page_size;
-       else
-               return -EINVAL;
-
-       for (off = 0 ; off < size ; off += page_size) {
-               curr_va = virt_addr + off;
-               flush_pte = (off + page_size) >= size;
-               rc = hl_mmu_unmap_page(ctx, curr_va, page_size, flush_pte);
-               if (rc)
-                       dev_warn_ratelimited(hdev->dev,
-                               "Unmap failed for va 0x%llx\n", curr_va);
-       }
-
-       return rc;
-}
-
-/*
- * hl_mmu_swap_out - marks all mapping of the given ctx as swapped out
- *
- * @ctx: pointer to the context structure
- *
- */
-void hl_mmu_swap_out(struct hl_ctx *ctx)
-{
-       struct hl_device *hdev = ctx->hdev;
-
-       if (!hdev->mmu_enable)
-               return;
-
-       if (hdev->mmu_func[MMU_DR_PGT].swap_out != NULL)
-               hdev->mmu_func[MMU_DR_PGT].swap_out(ctx);
-
-       if (hdev->mmu_func[MMU_HR_PGT].swap_out != NULL)
-               hdev->mmu_func[MMU_HR_PGT].swap_out(ctx);
-}
-
-/*
- * hl_mmu_swap_in - marks all mapping of the given ctx as swapped in
- *
- * @ctx: pointer to the context structure
- *
- */
-void hl_mmu_swap_in(struct hl_ctx *ctx)
-{
-       struct hl_device *hdev = ctx->hdev;
-
-       if (!hdev->mmu_enable)
-               return;
-
-       if (hdev->mmu_func[MMU_DR_PGT].swap_in != NULL)
-               hdev->mmu_func[MMU_DR_PGT].swap_in(ctx);
-
-       if (hdev->mmu_func[MMU_HR_PGT].swap_in != NULL)
-               hdev->mmu_func[MMU_HR_PGT].swap_in(ctx);
-}
-
-int hl_mmu_va_to_pa(struct hl_ctx *ctx, u64 virt_addr, u64 *phys_addr)
-{
-       struct hl_mmu_hop_info hops;
-       u64 tmp_addr;
-       int rc;
-
-       rc = hl_mmu_get_tlb_info(ctx, virt_addr, &hops);
-       if (rc)
-               return rc;
-
-       /* last hop holds the phys address and flags */
-       tmp_addr = hops.hop_info[hops.used_hops - 1].hop_pte_val;
-       *phys_addr = (tmp_addr & HOP_PHYS_ADDR_MASK) | (virt_addr & FLAGS_MASK);
-
-       return 0;
-}
-
-int hl_mmu_get_tlb_info(struct hl_ctx *ctx, u64 virt_addr,
-                       struct hl_mmu_hop_info *hops)
-{
-       struct hl_device *hdev = ctx->hdev;
-       struct asic_fixed_properties *prop = &hdev->asic_prop;
-       struct hl_mmu_properties *mmu_prop;
-       int rc;
-       bool is_dram_addr;
-
-       if (!hdev->mmu_enable)
-               return -EOPNOTSUPP;
-
-       hops->scrambled_vaddr = virt_addr;      /* assume no scrambling */
-
-       is_dram_addr = hl_mem_area_inside_range(virt_addr, prop->dmmu.page_size,
-                                               prop->dmmu.start_addr,
-                                               prop->dmmu.end_addr);
-
-       /* host-residency is the same in PMMU and HPMMU, use one of them */
-       mmu_prop = is_dram_addr ? &prop->dmmu : &prop->pmmu;
-
-       mutex_lock(&ctx->mmu_lock);
-
-       if (mmu_prop->host_resident)
-               rc = hdev->mmu_func[MMU_HR_PGT].get_tlb_info(ctx,
-                                                       virt_addr, hops);
-       else
-               rc = hdev->mmu_func[MMU_DR_PGT].get_tlb_info(ctx,
-                                                       virt_addr, hops);
-
-       mutex_unlock(&ctx->mmu_lock);
-
-       return rc;
-}
-
-int hl_mmu_if_set_funcs(struct hl_device *hdev)
-{
-       if (!hdev->mmu_enable)
-               return 0;
-
-       switch (hdev->asic_type) {
-       case ASIC_GOYA:
-       case ASIC_GAUDI:
-               hl_mmu_v1_set_funcs(hdev, &hdev->mmu_func[MMU_DR_PGT]);
-               break;
-       default:
-               dev_err(hdev->dev, "Unrecognized ASIC type %d\n",
-                       hdev->asic_type);
-               return -EOPNOTSUPP;
-       }
-
-       return 0;
-}
-
-/**
- * hl_mmu_scramble_vaddr() - The generic mmu virtual address scrambling routine.
- * @hdev: pointer to device data.
- * @virt_addr: The virtual address to scramble.
- *
- * Return: The scrambled virtual address.
- */
-u64 hl_mmu_scramble_vaddr(struct hl_device *hdev, u64 virt_addr)
-{
-       return virt_addr;
-}
diff --git a/drivers/misc/habanalabs/common/mmu/Makefile b/drivers/misc/habanalabs/common/mmu/Makefile
new file mode 100644 (file)
index 0000000..d852c38
--- /dev/null
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+HL_COMMON_MMU_FILES := common/mmu/mmu.o common/mmu/mmu_v1.o
diff --git a/drivers/misc/habanalabs/common/mmu/mmu.c b/drivers/misc/habanalabs/common/mmu/mmu.c
new file mode 100644 (file)
index 0000000..97c5168
--- /dev/null
@@ -0,0 +1,560 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2016-2020 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ */
+
+#include <linux/slab.h>
+
+#include "../habanalabs.h"
+
+bool hl_is_dram_va(struct hl_device *hdev, u64 virt_addr)
+{
+       struct asic_fixed_properties *prop = &hdev->asic_prop;
+
+       return hl_mem_area_inside_range(virt_addr, prop->dmmu.page_size,
+                                       prop->dmmu.start_addr,
+                                       prop->dmmu.end_addr);
+}
+
+/**
+ * hl_mmu_init() - initialize the MMU module.
+ * @hdev: habanalabs device structure.
+ *
+ * Return: 0 for success, non-zero for failure.
+ */
+int hl_mmu_init(struct hl_device *hdev)
+{
+       int rc = -EOPNOTSUPP;
+
+       if (!hdev->mmu_enable)
+               return 0;
+
+       if (hdev->mmu_func[MMU_DR_PGT].init != NULL) {
+               rc = hdev->mmu_func[MMU_DR_PGT].init(hdev);
+               if (rc)
+                       return rc;
+       }
+
+       if (hdev->mmu_func[MMU_HR_PGT].init != NULL)
+               rc = hdev->mmu_func[MMU_HR_PGT].init(hdev);
+
+       return rc;
+}
+
+/**
+ * hl_mmu_fini() - release the MMU module.
+ * @hdev: habanalabs device structure.
+ *
+ * This function does the following:
+ * - Disable MMU in H/W.
+ * - Free the pgt_infos pool.
+ *
+ * All contexts should be freed before calling this function.
+ */
+void hl_mmu_fini(struct hl_device *hdev)
+{
+       if (!hdev->mmu_enable)
+               return;
+
+       if (hdev->mmu_func[MMU_DR_PGT].fini != NULL)
+               hdev->mmu_func[MMU_DR_PGT].fini(hdev);
+
+       if (hdev->mmu_func[MMU_HR_PGT].fini != NULL)
+               hdev->mmu_func[MMU_HR_PGT].fini(hdev);
+}
+
+/**
+ * hl_mmu_ctx_init() - initialize a context for using the MMU module.
+ * @ctx: pointer to the context structure to initialize.
+ *
+ * Initialize a mutex to protect the concurrent mapping flow, a hash to hold all
+ * page tables hops related to this context.
+ * Return: 0 on success, non-zero otherwise.
+ */
+int hl_mmu_ctx_init(struct hl_ctx *ctx)
+{
+       struct hl_device *hdev = ctx->hdev;
+       int rc = -EOPNOTSUPP;
+
+       if (!hdev->mmu_enable)
+               return 0;
+
+       mutex_init(&ctx->mmu_lock);
+
+       if (hdev->mmu_func[MMU_DR_PGT].ctx_init != NULL) {
+               rc = hdev->mmu_func[MMU_DR_PGT].ctx_init(ctx);
+               if (rc)
+                       return rc;
+       }
+
+       if (hdev->mmu_func[MMU_HR_PGT].ctx_init != NULL)
+               rc = hdev->mmu_func[MMU_HR_PGT].ctx_init(ctx);
+
+       return rc;
+}
+
+/*
+ * hl_mmu_ctx_fini - disable a ctx from using the mmu module
+ *
+ * @ctx: pointer to the context structure
+ *
+ * This function does the following:
+ * - Free any pgts which were not freed yet
+ * - Free the mutex
+ * - Free DRAM default page mapping hops
+ */
+void hl_mmu_ctx_fini(struct hl_ctx *ctx)
+{
+       struct hl_device *hdev = ctx->hdev;
+
+       if (!hdev->mmu_enable)
+               return;
+
+       if (hdev->mmu_func[MMU_DR_PGT].ctx_fini != NULL)
+               hdev->mmu_func[MMU_DR_PGT].ctx_fini(ctx);
+
+       if (hdev->mmu_func[MMU_HR_PGT].ctx_fini != NULL)
+               hdev->mmu_func[MMU_HR_PGT].ctx_fini(ctx);
+
+       mutex_destroy(&ctx->mmu_lock);
+}
+
+/*
+ * hl_mmu_unmap_page - unmaps a virtual addr
+ *
+ * @ctx: pointer to the context structure
+ * @virt_addr: virt addr to map from
+ * @page_size: size of the page to unmap
+ * @flush_pte: whether to do a PCI flush
+ *
+ * This function does the following:
+ * - Check that the virt addr is mapped
+ * - Unmap the virt addr and frees pgts if possible
+ * - Returns 0 on success, -EINVAL if the given addr is not mapped
+ *
+ * Because this function changes the page tables in the device and because it
+ * changes the MMU hash, it must be protected by a lock.
+ * However, because it maps only a single page, the lock should be implemented
+ * in a higher level in order to protect the entire mapping of the memory area
+ *
+ * For optimization reasons PCI flush may be requested once after unmapping of
+ * large area.
+ */
+int hl_mmu_unmap_page(struct hl_ctx *ctx, u64 virt_addr, u32 page_size,
+               bool flush_pte)
+{
+       struct hl_device *hdev = ctx->hdev;
+       struct asic_fixed_properties *prop = &hdev->asic_prop;
+       struct hl_mmu_properties *mmu_prop;
+       u64 real_virt_addr;
+       u32 real_page_size, npages;
+       int i, rc = 0, pgt_residency;
+       bool is_dram_addr;
+
+       if (!hdev->mmu_enable)
+               return 0;
+
+       is_dram_addr = hl_is_dram_va(hdev, virt_addr);
+
+       if (is_dram_addr)
+               mmu_prop = &prop->dmmu;
+       else if ((page_size % prop->pmmu_huge.page_size) == 0)
+               mmu_prop = &prop->pmmu_huge;
+       else
+               mmu_prop = &prop->pmmu;
+
+       pgt_residency = mmu_prop->host_resident ? MMU_HR_PGT : MMU_DR_PGT;
+       /*
+        * The H/W handles mapping of specific page sizes. Hence if the page
+        * size is bigger, we break it to sub-pages and unmap them separately.
+        */
+       if ((page_size % mmu_prop->page_size) == 0) {
+               real_page_size = mmu_prop->page_size;
+       } else {
+               /*
+                * MMU page size may differ from DRAM page size.
+                * In such case work with the DRAM page size and let the MMU
+                * scrambling routine to handle this mismatch when
+                * calculating the address to remove from the MMU page table
+                */
+               if (is_dram_addr && ((page_size % prop->dram_page_size) == 0)) {
+                       real_page_size = prop->dram_page_size;
+               } else {
+                       dev_err(hdev->dev,
+                               "page size of %u is not %uKB aligned, can't unmap\n",
+                               page_size, mmu_prop->page_size >> 10);
+
+                       return -EFAULT;
+               }
+       }
+
+       npages = page_size / real_page_size;
+       real_virt_addr = virt_addr;
+
+       for (i = 0 ; i < npages ; i++) {
+               rc = hdev->mmu_func[pgt_residency].unmap(ctx,
+                                               real_virt_addr, is_dram_addr);
+               if (rc)
+                       break;
+
+               real_virt_addr += real_page_size;
+       }
+
+       if (flush_pte)
+               hdev->mmu_func[pgt_residency].flush(ctx);
+
+       return rc;
+}
+
+/*
+ * hl_mmu_map_page - maps a virtual addr to physical addr
+ *
+ * @ctx: pointer to the context structure
+ * @virt_addr: virt addr to map from
+ * @phys_addr: phys addr to map to
+ * @page_size: physical page size
+ * @flush_pte: whether to do a PCI flush
+ *
+ * This function does the following:
+ * - Check that the virt addr is not mapped
+ * - Allocate pgts as necessary in order to map the virt addr to the phys
+ * - Returns 0 on success, -EINVAL if addr is already mapped, or -ENOMEM.
+ *
+ * Because this function changes the page tables in the device and because it
+ * changes the MMU hash, it must be protected by a lock.
+ * However, because it maps only a single page, the lock should be implemented
+ * in a higher level in order to protect the entire mapping of the memory area
+ *
+ * For optimization reasons PCI flush may be requested once after mapping of
+ * large area.
+ */
+int hl_mmu_map_page(struct hl_ctx *ctx, u64 virt_addr, u64 phys_addr,
+               u32 page_size, bool flush_pte)
+{
+       struct hl_device *hdev = ctx->hdev;
+       struct asic_fixed_properties *prop = &hdev->asic_prop;
+       struct hl_mmu_properties *mmu_prop;
+       u64 real_virt_addr, real_phys_addr;
+       u32 real_page_size, npages;
+       int i, rc, pgt_residency, mapped_cnt = 0;
+       bool is_dram_addr;
+
+
+       if (!hdev->mmu_enable)
+               return 0;
+
+       is_dram_addr = hl_is_dram_va(hdev, virt_addr);
+
+       if (is_dram_addr)
+               mmu_prop = &prop->dmmu;
+       else if ((page_size % prop->pmmu_huge.page_size) == 0)
+               mmu_prop = &prop->pmmu_huge;
+       else
+               mmu_prop = &prop->pmmu;
+
+       pgt_residency = mmu_prop->host_resident ? MMU_HR_PGT : MMU_DR_PGT;
+
+       /*
+        * The H/W handles mapping of specific page sizes. Hence if the page
+        * size is bigger, we break it to sub-pages and map them separately.
+        */
+       if ((page_size % mmu_prop->page_size) == 0) {
+               real_page_size = mmu_prop->page_size;
+       } else if (is_dram_addr && ((page_size % prop->dram_page_size) == 0) &&
+                       (prop->dram_page_size < mmu_prop->page_size)) {
+               /*
+                * MMU page size may differ from DRAM page size.
+                * In such case work with the DRAM page size and let the MMU
+                * scrambling routine handle this mismatch when calculating
+                * the address to place in the MMU page table. (in that case
+                * also make sure that the dram_page_size smaller than the
+                * mmu page size)
+                */
+               real_page_size = prop->dram_page_size;
+       } else {
+               dev_err(hdev->dev,
+                       "page size of %u is not %uKB aligned, can't map\n",
+                       page_size, mmu_prop->page_size >> 10);
+
+               return -EFAULT;
+       }
+
+       /*
+        * Verify that the phys and virt addresses are aligned with the
+        * MMU page size (in dram this means checking the address and MMU
+        * after scrambling)
+        */
+       if ((is_dram_addr &&
+                       ((hdev->asic_funcs->scramble_vaddr(hdev, phys_addr) &
+                               (mmu_prop->page_size - 1)) ||
+                       (hdev->asic_funcs->scramble_vaddr(hdev, virt_addr) &
+                               (mmu_prop->page_size - 1)))) ||
+               (!is_dram_addr && ((phys_addr & (real_page_size - 1)) ||
+                               (virt_addr & (real_page_size - 1)))))
+               dev_crit(hdev->dev,
+                       "Mapping address 0x%llx with virtual address 0x%llx and page size of 0x%x is erroneous! Addresses must be divisible by page size",
+                       phys_addr, virt_addr, real_page_size);
+
+       npages = page_size / real_page_size;
+       real_virt_addr = virt_addr;
+       real_phys_addr = phys_addr;
+
+       for (i = 0 ; i < npages ; i++) {
+               rc = hdev->mmu_func[pgt_residency].map(ctx,
+                                               real_virt_addr, real_phys_addr,
+                                               real_page_size, is_dram_addr);
+               if (rc)
+                       goto err;
+
+               real_virt_addr += real_page_size;
+               real_phys_addr += real_page_size;
+               mapped_cnt++;
+       }
+
+       if (flush_pte)
+               hdev->mmu_func[pgt_residency].flush(ctx);
+
+       return 0;
+
+err:
+       real_virt_addr = virt_addr;
+       for (i = 0 ; i < mapped_cnt ; i++) {
+               if (hdev->mmu_func[pgt_residency].unmap(ctx,
+                                               real_virt_addr, is_dram_addr))
+                       dev_warn_ratelimited(hdev->dev,
+                               "failed to unmap va: 0x%llx\n", real_virt_addr);
+
+               real_virt_addr += real_page_size;
+       }
+
+       hdev->mmu_func[pgt_residency].flush(ctx);
+
+       return rc;
+}
+
+/*
+ * hl_mmu_map_contiguous - implements a wrapper for hl_mmu_map_page
+ *                         for mapping contiguous physical memory
+ *
+ * @ctx: pointer to the context structure
+ * @virt_addr: virt addr to map from
+ * @phys_addr: phys addr to map to
+ * @size: size to map
+ *
+ */
+int hl_mmu_map_contiguous(struct hl_ctx *ctx, u64 virt_addr,
+                                       u64 phys_addr, u32 size)
+{
+       struct hl_device *hdev = ctx->hdev;
+       struct asic_fixed_properties *prop = &hdev->asic_prop;
+       u64 curr_va, curr_pa;
+       u32 page_size;
+       bool flush_pte;
+       int rc = 0, off;
+
+       if (hl_mem_area_inside_range(virt_addr, size,
+                       prop->dmmu.start_addr, prop->dmmu.end_addr))
+               page_size = prop->dmmu.page_size;
+       else if (hl_mem_area_inside_range(virt_addr, size,
+                       prop->pmmu.start_addr, prop->pmmu.end_addr))
+               page_size = prop->pmmu.page_size;
+       else if (hl_mem_area_inside_range(virt_addr, size,
+                       prop->pmmu_huge.start_addr, prop->pmmu_huge.end_addr))
+               page_size = prop->pmmu_huge.page_size;
+       else
+               return -EINVAL;
+
+       for (off = 0 ; off < size ; off += page_size) {
+               curr_va = virt_addr + off;
+               curr_pa = phys_addr + off;
+               flush_pte = (off + page_size) >= size;
+               rc = hl_mmu_map_page(ctx, curr_va, curr_pa, page_size,
+                                                               flush_pte);
+               if (rc) {
+                       dev_err(hdev->dev,
+                               "Map failed for va 0x%llx to pa 0x%llx\n",
+                               curr_va, curr_pa);
+                       goto unmap;
+               }
+       }
+
+       return rc;
+
+unmap:
+       for (; off >= 0 ; off -= page_size) {
+               curr_va = virt_addr + off;
+               flush_pte = (off - (s32) page_size) < 0;
+               if (hl_mmu_unmap_page(ctx, curr_va, page_size, flush_pte))
+                       dev_warn_ratelimited(hdev->dev,
+                               "failed to unmap va 0x%llx\n", curr_va);
+       }
+
+       return rc;
+}
+
+/*
+ * hl_mmu_unmap_contiguous - implements a wrapper for hl_mmu_unmap_page
+ *                           for unmapping contiguous physical memory
+ *
+ * @ctx: pointer to the context structure
+ * @virt_addr: virt addr to unmap
+ * @size: size to unmap
+ *
+ */
+int hl_mmu_unmap_contiguous(struct hl_ctx *ctx, u64 virt_addr, u32 size)
+{
+       struct hl_device *hdev = ctx->hdev;
+       struct asic_fixed_properties *prop = &hdev->asic_prop;
+       u64 curr_va;
+       u32 page_size;
+       bool flush_pte;
+       int rc = 0, off;
+
+       if (hl_mem_area_inside_range(virt_addr, size,
+                       prop->dmmu.start_addr, prop->dmmu.end_addr))
+               page_size = prop->dmmu.page_size;
+       else if (hl_mem_area_inside_range(virt_addr, size,
+                       prop->pmmu.start_addr, prop->pmmu.end_addr))
+               page_size = prop->pmmu.page_size;
+       else if (hl_mem_area_inside_range(virt_addr, size,
+                       prop->pmmu_huge.start_addr, prop->pmmu_huge.end_addr))
+               page_size = prop->pmmu_huge.page_size;
+       else
+               return -EINVAL;
+
+       for (off = 0 ; off < size ; off += page_size) {
+               curr_va = virt_addr + off;
+               flush_pte = (off + page_size) >= size;
+               rc = hl_mmu_unmap_page(ctx, curr_va, page_size, flush_pte);
+               if (rc)
+                       dev_warn_ratelimited(hdev->dev,
+                               "Unmap failed for va 0x%llx\n", curr_va);
+       }
+
+       return rc;
+}
+
+/*
+ * hl_mmu_swap_out - marks all mapping of the given ctx as swapped out
+ *
+ * @ctx: pointer to the context structure
+ *
+ */
+void hl_mmu_swap_out(struct hl_ctx *ctx)
+{
+       struct hl_device *hdev = ctx->hdev;
+
+       if (!hdev->mmu_enable)
+               return;
+
+       if (hdev->mmu_func[MMU_DR_PGT].swap_out != NULL)
+               hdev->mmu_func[MMU_DR_PGT].swap_out(ctx);
+
+       if (hdev->mmu_func[MMU_HR_PGT].swap_out != NULL)
+               hdev->mmu_func[MMU_HR_PGT].swap_out(ctx);
+}
+
+/*
+ * hl_mmu_swap_in - marks all mapping of the given ctx as swapped in
+ *
+ * @ctx: pointer to the context structure
+ *
+ */
+void hl_mmu_swap_in(struct hl_ctx *ctx)
+{
+       struct hl_device *hdev = ctx->hdev;
+
+       if (!hdev->mmu_enable)
+               return;
+
+       if (hdev->mmu_func[MMU_DR_PGT].swap_in != NULL)
+               hdev->mmu_func[MMU_DR_PGT].swap_in(ctx);
+
+       if (hdev->mmu_func[MMU_HR_PGT].swap_in != NULL)
+               hdev->mmu_func[MMU_HR_PGT].swap_in(ctx);
+}
+
+int hl_mmu_va_to_pa(struct hl_ctx *ctx, u64 virt_addr, u64 *phys_addr)
+{
+       struct hl_mmu_hop_info hops;
+       u64 tmp_addr;
+       int rc;
+
+       rc = hl_mmu_get_tlb_info(ctx, virt_addr, &hops);
+       if (rc)
+               return rc;
+
+       /* last hop holds the phys address and flags */
+       tmp_addr = hops.hop_info[hops.used_hops - 1].hop_pte_val;
+       *phys_addr = (tmp_addr & HOP_PHYS_ADDR_MASK) | (virt_addr & FLAGS_MASK);
+
+       return 0;
+}
+
+int hl_mmu_get_tlb_info(struct hl_ctx *ctx, u64 virt_addr,
+                       struct hl_mmu_hop_info *hops)
+{
+       struct hl_device *hdev = ctx->hdev;
+       struct asic_fixed_properties *prop = &hdev->asic_prop;
+       struct hl_mmu_properties *mmu_prop;
+       int rc;
+       bool is_dram_addr;
+
+       if (!hdev->mmu_enable)
+               return -EOPNOTSUPP;
+
+       hops->scrambled_vaddr = virt_addr;      /* assume no scrambling */
+
+       is_dram_addr = hl_mem_area_inside_range(virt_addr, prop->dmmu.page_size,
+                                               prop->dmmu.start_addr,
+                                               prop->dmmu.end_addr);
+
+       /* host-residency is the same in PMMU and HPMMU, use one of them */
+       mmu_prop = is_dram_addr ? &prop->dmmu : &prop->pmmu;
+
+       mutex_lock(&ctx->mmu_lock);
+
+       if (mmu_prop->host_resident)
+               rc = hdev->mmu_func[MMU_HR_PGT].get_tlb_info(ctx,
+                                                       virt_addr, hops);
+       else
+               rc = hdev->mmu_func[MMU_DR_PGT].get_tlb_info(ctx,
+                                                       virt_addr, hops);
+
+       mutex_unlock(&ctx->mmu_lock);
+
+       return rc;
+}
+
+int hl_mmu_if_set_funcs(struct hl_device *hdev)
+{
+       if (!hdev->mmu_enable)
+               return 0;
+
+       switch (hdev->asic_type) {
+       case ASIC_GOYA:
+       case ASIC_GAUDI:
+               hl_mmu_v1_set_funcs(hdev, &hdev->mmu_func[MMU_DR_PGT]);
+               break;
+       default:
+               dev_err(hdev->dev, "Unrecognized ASIC type %d\n",
+                       hdev->asic_type);
+               return -EOPNOTSUPP;
+       }
+
+       return 0;
+}
+
+/**
+ * hl_mmu_scramble_vaddr() - The generic mmu virtual address scrambling routine.
+ * @hdev: pointer to device data.
+ * @virt_addr: The virtual address to scramble.
+ *
+ * Return: The scrambled virtual address.
+ */
+u64 hl_mmu_scramble_vaddr(struct hl_device *hdev, u64 virt_addr)
+{
+       return virt_addr;
+}
diff --git a/drivers/misc/habanalabs/common/mmu/mmu_v1.c b/drivers/misc/habanalabs/common/mmu/mmu_v1.c
new file mode 100644 (file)
index 0000000..c5e93ff
--- /dev/null
@@ -0,0 +1,966 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2016-2019 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ */
+
+#include "../habanalabs.h"
+#include "../../include/hw_ip/mmu/mmu_general.h"
+
+#include <linux/slab.h>
+
+static inline u64 get_phys_addr(struct hl_ctx *ctx, u64 shadow_addr);
+
+static struct pgt_info *get_pgt_info(struct hl_ctx *ctx, u64 hop_addr)
+{
+       struct pgt_info *pgt_info = NULL;
+
+       hash_for_each_possible(ctx->mmu_shadow_hash, pgt_info, node,
+                               (unsigned long) hop_addr)
+               if (hop_addr == pgt_info->shadow_addr)
+                       break;
+
+       return pgt_info;
+}
+
+static void _free_hop(struct hl_ctx *ctx, struct pgt_info *pgt_info)
+{
+       struct hl_device *hdev = ctx->hdev;
+
+       gen_pool_free(hdev->mmu_priv.dr.mmu_pgt_pool, pgt_info->phys_addr,
+                       hdev->asic_prop.mmu_hop_table_size);
+       hash_del(&pgt_info->node);
+       kfree((u64 *) (uintptr_t) pgt_info->shadow_addr);
+       kfree(pgt_info);
+}
+
+static void free_hop(struct hl_ctx *ctx, u64 hop_addr)
+{
+       struct pgt_info *pgt_info = get_pgt_info(ctx, hop_addr);
+
+       _free_hop(ctx, pgt_info);
+}
+
+static u64 alloc_hop(struct hl_ctx *ctx)
+{
+       struct hl_device *hdev = ctx->hdev;
+       struct asic_fixed_properties *prop = &hdev->asic_prop;
+       struct pgt_info *pgt_info;
+       u64 phys_addr, shadow_addr;
+
+       pgt_info = kmalloc(sizeof(*pgt_info), GFP_KERNEL);
+       if (!pgt_info)
+               return ULLONG_MAX;
+
+       phys_addr = (u64) gen_pool_alloc(hdev->mmu_priv.dr.mmu_pgt_pool,
+                                       prop->mmu_hop_table_size);
+       if (!phys_addr) {
+               dev_err(hdev->dev, "failed to allocate page\n");
+               goto pool_add_err;
+       }
+
+       shadow_addr = (u64) (uintptr_t) kzalloc(prop->mmu_hop_table_size,
+                                               GFP_KERNEL);
+       if (!shadow_addr)
+               goto shadow_err;
+
+       pgt_info->phys_addr = phys_addr;
+       pgt_info->shadow_addr = shadow_addr;
+       pgt_info->ctx = ctx;
+       pgt_info->num_of_ptes = 0;
+       hash_add(ctx->mmu_shadow_hash, &pgt_info->node, shadow_addr);
+
+       return shadow_addr;
+
+shadow_err:
+       gen_pool_free(hdev->mmu_priv.dr.mmu_pgt_pool, phys_addr,
+                       prop->mmu_hop_table_size);
+pool_add_err:
+       kfree(pgt_info);
+
+       return ULLONG_MAX;
+}
+
+static inline u64 get_phys_hop0_addr(struct hl_ctx *ctx)
+{
+       return ctx->hdev->asic_prop.mmu_pgt_addr +
+                       (ctx->asid * ctx->hdev->asic_prop.mmu_hop_table_size);
+}
+
+static inline u64 get_hop0_addr(struct hl_ctx *ctx)
+{
+       return (u64) (uintptr_t) ctx->hdev->mmu_priv.dr.mmu_shadow_hop0 +
+                       (ctx->asid * ctx->hdev->asic_prop.mmu_hop_table_size);
+}
+
+static void flush(struct hl_ctx *ctx)
+{
+       /* flush all writes from all cores to reach PCI */
+       mb();
+       ctx->hdev->asic_funcs->read_pte(ctx->hdev, get_phys_hop0_addr(ctx));
+}
+
+/* transform the value to physical address when writing to H/W */
+static inline void write_pte(struct hl_ctx *ctx, u64 shadow_pte_addr, u64 val)
+{
+       /*
+        * The value to write is actually the address of the next shadow hop +
+        * flags at the 12 LSBs.
+        * Hence in order to get the value to write to the physical PTE, we
+        * clear the 12 LSBs and translate the shadow hop to its associated
+        * physical hop, and add back the original 12 LSBs.
+        */
+       u64 phys_val = get_phys_addr(ctx, val & HOP_PHYS_ADDR_MASK) |
+                               (val & FLAGS_MASK);
+
+       ctx->hdev->asic_funcs->write_pte(ctx->hdev,
+                                       get_phys_addr(ctx, shadow_pte_addr),
+                                       phys_val);
+
+       *(u64 *) (uintptr_t) shadow_pte_addr = val;
+}
+
+/* do not transform the value to physical address when writing to H/W */
+static inline void write_final_pte(struct hl_ctx *ctx, u64 shadow_pte_addr,
+                                       u64 val)
+{
+       ctx->hdev->asic_funcs->write_pte(ctx->hdev,
+                                       get_phys_addr(ctx, shadow_pte_addr),
+                                       val);
+       *(u64 *) (uintptr_t) shadow_pte_addr = val;
+}
+
+/* clear the last and present bits */
+static inline void clear_pte(struct hl_ctx *ctx, u64 pte_addr)
+{
+       /* no need to transform the value to physical address */
+       write_final_pte(ctx, pte_addr, 0);
+}
+
+static inline void get_pte(struct hl_ctx *ctx, u64 hop_addr)
+{
+       get_pgt_info(ctx, hop_addr)->num_of_ptes++;
+}
+
+/*
+ * put_pte - decrement the num of ptes and free the hop if possible
+ *
+ * @ctx: pointer to the context structure
+ * @hop_addr: addr of the hop
+ *
+ * This function returns the number of ptes left on this hop. If the number is
+ * 0, it means the pte was freed.
+ */
+static inline int put_pte(struct hl_ctx *ctx, u64 hop_addr)
+{
+       struct pgt_info *pgt_info = get_pgt_info(ctx, hop_addr);
+       int num_of_ptes_left;
+
+       pgt_info->num_of_ptes--;
+
+       /*
+        * Need to save the number of ptes left because free_hop might free
+        * the pgt_info
+        */
+       num_of_ptes_left = pgt_info->num_of_ptes;
+       if (!num_of_ptes_left)
+               _free_hop(ctx, pgt_info);
+
+       return num_of_ptes_left;
+}
+
+static inline u64 get_hopN_pte_addr(struct hl_ctx *ctx, u64 hop_addr,
+                                       u64 virt_addr, u64 mask, u64 shift)
+{
+       return hop_addr + ctx->hdev->asic_prop.mmu_pte_size *
+                       ((virt_addr & mask) >> shift);
+}
+
+static inline u64 get_hop0_pte_addr(struct hl_ctx *ctx,
+                                       struct hl_mmu_properties *mmu_prop,
+                                       u64 hop_addr, u64 vaddr)
+{
+       return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_prop->hop0_mask,
+                                       mmu_prop->hop0_shift);
+}
+
+static inline u64 get_hop1_pte_addr(struct hl_ctx *ctx,
+                                       struct hl_mmu_properties *mmu_prop,
+                                       u64 hop_addr, u64 vaddr)
+{
+       return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_prop->hop1_mask,
+                                       mmu_prop->hop1_shift);
+}
+
+static inline u64 get_hop2_pte_addr(struct hl_ctx *ctx,
+                                       struct hl_mmu_properties *mmu_prop,
+                                       u64 hop_addr, u64 vaddr)
+{
+       return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_prop->hop2_mask,
+                                       mmu_prop->hop2_shift);
+}
+
+static inline u64 get_hop3_pte_addr(struct hl_ctx *ctx,
+                                       struct hl_mmu_properties *mmu_prop,
+                                       u64 hop_addr, u64 vaddr)
+{
+       return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_prop->hop3_mask,
+                                       mmu_prop->hop3_shift);
+}
+
+static inline u64 get_hop4_pte_addr(struct hl_ctx *ctx,
+                                       struct hl_mmu_properties *mmu_prop,
+                                       u64 hop_addr, u64 vaddr)
+{
+       return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_prop->hop4_mask,
+                                       mmu_prop->hop4_shift);
+}
+
+static inline u64 get_next_hop_addr(struct hl_ctx *ctx, u64 curr_pte)
+{
+       if (curr_pte & PAGE_PRESENT_MASK)
+               return curr_pte & HOP_PHYS_ADDR_MASK;
+       else
+               return ULLONG_MAX;
+}
+
+static inline u64 get_alloc_next_hop_addr(struct hl_ctx *ctx, u64 curr_pte,
+                                               bool *is_new_hop)
+{
+       u64 hop_addr = get_next_hop_addr(ctx, curr_pte);
+
+       if (hop_addr == ULLONG_MAX) {
+               hop_addr = alloc_hop(ctx);
+               *is_new_hop = (hop_addr != ULLONG_MAX);
+       }
+
+       return hop_addr;
+}
+
+/* translates shadow address inside hop to a physical address */
+static inline u64 get_phys_addr(struct hl_ctx *ctx, u64 shadow_addr)
+{
+       u64 page_mask = (ctx->hdev->asic_prop.mmu_hop_table_size - 1);
+       u64 shadow_hop_addr = shadow_addr & ~page_mask;
+       u64 pte_offset = shadow_addr & page_mask;
+       u64 phys_hop_addr;
+
+       if (shadow_hop_addr != get_hop0_addr(ctx))
+               phys_hop_addr = get_pgt_info(ctx, shadow_hop_addr)->phys_addr;
+       else
+               phys_hop_addr = get_phys_hop0_addr(ctx);
+
+       return phys_hop_addr + pte_offset;
+}
+
+static int dram_default_mapping_init(struct hl_ctx *ctx)
+{
+       struct hl_device *hdev = ctx->hdev;
+       struct asic_fixed_properties *prop = &hdev->asic_prop;
+       u64 num_of_hop3, total_hops, hop0_addr, hop1_addr, hop2_addr,
+               hop2_pte_addr, hop3_pte_addr, pte_val;
+       int rc, i, j, hop3_allocated = 0;
+
+       if ((!prop->dram_supports_virtual_memory) ||
+                       (!hdev->dram_default_page_mapping) ||
+                       (ctx->asid == HL_KERNEL_ASID_ID))
+               return 0;
+
+       num_of_hop3 = prop->dram_size_for_default_page_mapping;
+       do_div(num_of_hop3, prop->dram_page_size);
+       do_div(num_of_hop3, PTE_ENTRIES_IN_HOP);
+
+       /* add hop1 and hop2 */
+       total_hops = num_of_hop3 + 2;
+
+       ctx->dram_default_hops = kzalloc(HL_PTE_SIZE * total_hops,  GFP_KERNEL);
+       if (!ctx->dram_default_hops)
+               return -ENOMEM;
+
+       hop0_addr = get_hop0_addr(ctx);
+
+       hop1_addr = alloc_hop(ctx);
+       if (hop1_addr == ULLONG_MAX) {
+               dev_err(hdev->dev, "failed to alloc hop 1\n");
+               rc = -ENOMEM;
+               goto hop1_err;
+       }
+
+       ctx->dram_default_hops[total_hops - 1] = hop1_addr;
+
+       hop2_addr = alloc_hop(ctx);
+       if (hop2_addr == ULLONG_MAX) {
+               dev_err(hdev->dev, "failed to alloc hop 2\n");
+               rc = -ENOMEM;
+               goto hop2_err;
+       }
+
+       ctx->dram_default_hops[total_hops - 2] = hop2_addr;
+
+       for (i = 0 ; i < num_of_hop3 ; i++) {
+               ctx->dram_default_hops[i] = alloc_hop(ctx);
+               if (ctx->dram_default_hops[i] == ULLONG_MAX) {
+                       dev_err(hdev->dev, "failed to alloc hop 3, i: %d\n", i);
+                       rc = -ENOMEM;
+                       goto hop3_err;
+               }
+               hop3_allocated++;
+       }
+
+       /* need only pte 0 in hops 0 and 1 */
+       pte_val = (hop1_addr & HOP_PHYS_ADDR_MASK) | PAGE_PRESENT_MASK;
+       write_pte(ctx, hop0_addr, pte_val);
+
+       pte_val = (hop2_addr & HOP_PHYS_ADDR_MASK) | PAGE_PRESENT_MASK;
+       write_pte(ctx, hop1_addr, pte_val);
+       get_pte(ctx, hop1_addr);
+
+       hop2_pte_addr = hop2_addr;
+       for (i = 0 ; i < num_of_hop3 ; i++) {
+               pte_val = (ctx->dram_default_hops[i] & HOP_PHYS_ADDR_MASK) |
+                               PAGE_PRESENT_MASK;
+               write_pte(ctx, hop2_pte_addr, pte_val);
+               get_pte(ctx, hop2_addr);
+               hop2_pte_addr += HL_PTE_SIZE;
+       }
+
+       pte_val = (prop->mmu_dram_default_page_addr & HOP_PHYS_ADDR_MASK) |
+                       LAST_MASK | PAGE_PRESENT_MASK;
+
+       for (i = 0 ; i < num_of_hop3 ; i++) {
+               hop3_pte_addr = ctx->dram_default_hops[i];
+               for (j = 0 ; j < PTE_ENTRIES_IN_HOP ; j++) {
+                       write_final_pte(ctx, hop3_pte_addr, pte_val);
+                       get_pte(ctx, ctx->dram_default_hops[i]);
+                       hop3_pte_addr += HL_PTE_SIZE;
+               }
+       }
+
+       flush(ctx);
+
+       return 0;
+
+hop3_err:
+       for (i = 0 ; i < hop3_allocated ; i++)
+               free_hop(ctx, ctx->dram_default_hops[i]);
+
+       free_hop(ctx, hop2_addr);
+hop2_err:
+       free_hop(ctx, hop1_addr);
+hop1_err:
+       kfree(ctx->dram_default_hops);
+
+       return rc;
+}
+
+static void dram_default_mapping_fini(struct hl_ctx *ctx)
+{
+       struct hl_device *hdev = ctx->hdev;
+       struct asic_fixed_properties *prop = &hdev->asic_prop;
+       u64 num_of_hop3, total_hops, hop0_addr, hop1_addr, hop2_addr,
+               hop2_pte_addr, hop3_pte_addr;
+       int i, j;
+
+       if ((!prop->dram_supports_virtual_memory) ||
+                       (!hdev->dram_default_page_mapping) ||
+                       (ctx->asid == HL_KERNEL_ASID_ID))
+               return;
+
+       num_of_hop3 = prop->dram_size_for_default_page_mapping;
+       do_div(num_of_hop3, prop->dram_page_size);
+       do_div(num_of_hop3, PTE_ENTRIES_IN_HOP);
+
+       hop0_addr = get_hop0_addr(ctx);
+       /* add hop1 and hop2 */
+       total_hops = num_of_hop3 + 2;
+       hop1_addr = ctx->dram_default_hops[total_hops - 1];
+       hop2_addr = ctx->dram_default_hops[total_hops - 2];
+
+       for (i = 0 ; i < num_of_hop3 ; i++) {
+               hop3_pte_addr = ctx->dram_default_hops[i];
+               for (j = 0 ; j < PTE_ENTRIES_IN_HOP ; j++) {
+                       clear_pte(ctx, hop3_pte_addr);
+                       put_pte(ctx, ctx->dram_default_hops[i]);
+                       hop3_pte_addr += HL_PTE_SIZE;
+               }
+       }
+
+       hop2_pte_addr = hop2_addr;
+       hop2_pte_addr = hop2_addr;
+       for (i = 0 ; i < num_of_hop3 ; i++) {
+               clear_pte(ctx, hop2_pte_addr);
+               put_pte(ctx, hop2_addr);
+               hop2_pte_addr += HL_PTE_SIZE;
+       }
+
+       clear_pte(ctx, hop1_addr);
+       put_pte(ctx, hop1_addr);
+       clear_pte(ctx, hop0_addr);
+
+       kfree(ctx->dram_default_hops);
+
+       flush(ctx);
+}
+
+/**
+ * hl_mmu_v1_init() - initialize the MMU module.
+ * @hdev: habanalabs device structure.
+ *
+ * This function does the following:
+ * - Create a pool of pages for pgt_infos.
+ * - Create a shadow table for pgt
+ *
+ * Return: 0 for success, non-zero for failure.
+ */
+static int hl_mmu_v1_init(struct hl_device *hdev)
+{
+       struct asic_fixed_properties *prop = &hdev->asic_prop;
+       int rc;
+
+       hdev->mmu_priv.dr.mmu_pgt_pool =
+                       gen_pool_create(__ffs(prop->mmu_hop_table_size), -1);
+
+       if (!hdev->mmu_priv.dr.mmu_pgt_pool) {
+               dev_err(hdev->dev, "Failed to create page gen pool\n");
+               return -ENOMEM;
+       }
+
+       rc = gen_pool_add(hdev->mmu_priv.dr.mmu_pgt_pool, prop->mmu_pgt_addr +
+                       prop->mmu_hop0_tables_total_size,
+                       prop->mmu_pgt_size - prop->mmu_hop0_tables_total_size,
+                       -1);
+       if (rc) {
+               dev_err(hdev->dev, "Failed to add memory to page gen pool\n");
+               goto err_pool_add;
+       }
+
+       hdev->mmu_priv.dr.mmu_shadow_hop0 = kvmalloc_array(prop->max_asid,
+                                               prop->mmu_hop_table_size,
+                                               GFP_KERNEL | __GFP_ZERO);
+       if (ZERO_OR_NULL_PTR(hdev->mmu_priv.dr.mmu_shadow_hop0)) {
+               rc = -ENOMEM;
+               goto err_pool_add;
+       }
+
+       /* MMU H/W init will be done in device hw_init() */
+
+       return 0;
+
+err_pool_add:
+       gen_pool_destroy(hdev->mmu_priv.dr.mmu_pgt_pool);
+
+       return rc;
+}
+
+/**
+ * hl_mmu_fini() - release the MMU module.
+ * @hdev: habanalabs device structure.
+ *
+ * This function does the following:
+ * - Disable MMU in H/W.
+ * - Free the pgt_infos pool.
+ *
+ * All contexts should be freed before calling this function.
+ */
+static void hl_mmu_v1_fini(struct hl_device *hdev)
+{
+       /* MMU H/W fini was already done in device hw_fini() */
+
+       if (!ZERO_OR_NULL_PTR(hdev->mmu_priv.hr.mmu_shadow_hop0)) {
+               kvfree(hdev->mmu_priv.dr.mmu_shadow_hop0);
+               gen_pool_destroy(hdev->mmu_priv.dr.mmu_pgt_pool);
+       }
+
+       /* Make sure that if we arrive here again without init was called we
+        * won't cause kernel panic. This can happen for example if we fail
+        * during hard reset code at certain points
+        */
+       hdev->mmu_priv.dr.mmu_shadow_hop0 = NULL;
+}
+
+/**
+ * hl_mmu_ctx_init() - initialize a context for using the MMU module.
+ * @ctx: pointer to the context structure to initialize.
+ *
+ * Initialize a mutex to protect the concurrent mapping flow, a hash to hold all
+ * page tables hops related to this context.
+ * Return: 0 on success, non-zero otherwise.
+ */
+static int hl_mmu_v1_ctx_init(struct hl_ctx *ctx)
+{
+       hash_init(ctx->mmu_shadow_hash);
+       return dram_default_mapping_init(ctx);
+}
+
+/*
+ * hl_mmu_ctx_fini - disable a ctx from using the mmu module
+ *
+ * @ctx: pointer to the context structure
+ *
+ * This function does the following:
+ * - Free any pgts which were not freed yet
+ * - Free the mutex
+ * - Free DRAM default page mapping hops
+ */
+static void hl_mmu_v1_ctx_fini(struct hl_ctx *ctx)
+{
+       struct hl_device *hdev = ctx->hdev;
+       struct pgt_info *pgt_info;
+       struct hlist_node *tmp;
+       int i;
+
+       dram_default_mapping_fini(ctx);
+
+       if (!hash_empty(ctx->mmu_shadow_hash))
+               dev_err(hdev->dev, "ctx %d is freed while it has pgts in use\n",
+                       ctx->asid);
+
+       hash_for_each_safe(ctx->mmu_shadow_hash, i, tmp, pgt_info, node) {
+               dev_err_ratelimited(hdev->dev,
+                       "pgt_info of addr 0x%llx of asid %d was not destroyed, num_ptes: %d\n",
+                       pgt_info->phys_addr, ctx->asid, pgt_info->num_of_ptes);
+               _free_hop(ctx, pgt_info);
+       }
+}
+
+static int _hl_mmu_v1_unmap(struct hl_ctx *ctx,
+                               u64 virt_addr, bool is_dram_addr)
+{
+       struct hl_device *hdev = ctx->hdev;
+       struct asic_fixed_properties *prop = &hdev->asic_prop;
+       struct hl_mmu_properties *mmu_prop;
+       u64 hop0_addr = 0, hop0_pte_addr = 0,
+               hop1_addr = 0, hop1_pte_addr = 0,
+               hop2_addr = 0, hop2_pte_addr = 0,
+               hop3_addr = 0, hop3_pte_addr = 0,
+               hop4_addr = 0, hop4_pte_addr = 0,
+               curr_pte;
+       bool is_huge, clear_hop3 = true;
+
+       /* shifts and masks are the same in PMMU and HPMMU, use one of them */
+       mmu_prop = is_dram_addr ? &prop->dmmu : &prop->pmmu;
+
+       hop0_addr = get_hop0_addr(ctx);
+       hop0_pte_addr = get_hop0_pte_addr(ctx, mmu_prop, hop0_addr, virt_addr);
+
+       curr_pte = *(u64 *) (uintptr_t) hop0_pte_addr;
+
+       hop1_addr = get_next_hop_addr(ctx, curr_pte);
+
+       if (hop1_addr == ULLONG_MAX)
+               goto not_mapped;
+
+       hop1_pte_addr = get_hop1_pte_addr(ctx, mmu_prop, hop1_addr, virt_addr);
+
+       curr_pte = *(u64 *) (uintptr_t) hop1_pte_addr;
+
+       hop2_addr = get_next_hop_addr(ctx, curr_pte);
+
+       if (hop2_addr == ULLONG_MAX)
+               goto not_mapped;
+
+       hop2_pte_addr = get_hop2_pte_addr(ctx, mmu_prop, hop2_addr, virt_addr);
+
+       curr_pte = *(u64 *) (uintptr_t) hop2_pte_addr;
+
+       hop3_addr = get_next_hop_addr(ctx, curr_pte);
+
+       if (hop3_addr == ULLONG_MAX)
+               goto not_mapped;
+
+       hop3_pte_addr = get_hop3_pte_addr(ctx, mmu_prop, hop3_addr, virt_addr);
+
+       curr_pte = *(u64 *) (uintptr_t) hop3_pte_addr;
+
+       is_huge = curr_pte & LAST_MASK;
+
+       if (is_dram_addr && !is_huge) {
+               dev_err(hdev->dev,
+                               "DRAM unmapping should use huge pages only\n");
+               return -EFAULT;
+       }
+
+       if (!is_huge) {
+               hop4_addr = get_next_hop_addr(ctx, curr_pte);
+
+               if (hop4_addr == ULLONG_MAX)
+                       goto not_mapped;
+
+               hop4_pte_addr = get_hop4_pte_addr(ctx, mmu_prop, hop4_addr,
+                                                       virt_addr);
+
+               curr_pte = *(u64 *) (uintptr_t) hop4_pte_addr;
+
+               clear_hop3 = false;
+       }
+
+       if (hdev->dram_default_page_mapping && is_dram_addr) {
+               u64 default_pte = (prop->mmu_dram_default_page_addr &
+                               HOP_PHYS_ADDR_MASK) | LAST_MASK |
+                                       PAGE_PRESENT_MASK;
+               if (curr_pte == default_pte) {
+                       dev_err(hdev->dev,
+                               "DRAM: hop3 PTE points to zero page, can't unmap, va: 0x%llx\n",
+                                       virt_addr);
+                       goto not_mapped;
+               }
+
+               if (!(curr_pte & PAGE_PRESENT_MASK)) {
+                       dev_err(hdev->dev,
+                               "DRAM: hop3 PTE is cleared! can't unmap, va: 0x%llx\n",
+                                       virt_addr);
+                       goto not_mapped;
+               }
+
+               write_final_pte(ctx, hop3_pte_addr, default_pte);
+               put_pte(ctx, hop3_addr);
+       } else {
+               if (!(curr_pte & PAGE_PRESENT_MASK))
+                       goto not_mapped;
+
+               if (hop4_addr)
+                       clear_pte(ctx, hop4_pte_addr);
+               else
+                       clear_pte(ctx, hop3_pte_addr);
+
+               if (hop4_addr && !put_pte(ctx, hop4_addr))
+                       clear_hop3 = true;
+
+               if (!clear_hop3)
+                       goto mapped;
+
+               clear_pte(ctx, hop3_pte_addr);
+
+               if (put_pte(ctx, hop3_addr))
+                       goto mapped;
+
+               clear_pte(ctx, hop2_pte_addr);
+
+               if (put_pte(ctx, hop2_addr))
+                       goto mapped;
+
+               clear_pte(ctx, hop1_pte_addr);
+
+               if (put_pte(ctx, hop1_addr))
+                       goto mapped;
+
+               clear_pte(ctx, hop0_pte_addr);
+       }
+
+mapped:
+       return 0;
+
+not_mapped:
+       dev_err(hdev->dev, "virt addr 0x%llx is not mapped to phys addr\n",
+               virt_addr);
+
+       return -EINVAL;
+}
+
+static int _hl_mmu_v1_map(struct hl_ctx *ctx, u64 virt_addr, u64 phys_addr,
+                       u32 page_size, bool is_dram_addr)
+{
+       struct hl_device *hdev = ctx->hdev;
+       struct asic_fixed_properties *prop = &hdev->asic_prop;
+       struct hl_mmu_properties *mmu_prop;
+       u64 hop0_addr = 0, hop0_pte_addr = 0,
+               hop1_addr = 0, hop1_pte_addr = 0,
+               hop2_addr = 0, hop2_pte_addr = 0,
+               hop3_addr = 0, hop3_pte_addr = 0,
+               hop4_addr = 0, hop4_pte_addr = 0,
+               curr_pte = 0;
+       bool hop1_new = false, hop2_new = false, hop3_new = false,
+               hop4_new = false, is_huge;
+       int rc = -ENOMEM;
+
+       /*
+        * This mapping function can map a page or a huge page. For huge page
+        * there are only 3 hops rather than 4. Currently the DRAM allocation
+        * uses huge pages only but user memory could have been allocated with
+        * one of the two page sizes. Since this is a common code for all the
+        * three cases, we need this hugs page check.
+        */
+       if (is_dram_addr) {
+               mmu_prop = &prop->dmmu;
+               is_huge = true;
+       } else if (page_size == prop->pmmu_huge.page_size) {
+               mmu_prop = &prop->pmmu_huge;
+               is_huge = true;
+       } else {
+               mmu_prop = &prop->pmmu;
+               is_huge = false;
+       }
+
+       hop0_addr = get_hop0_addr(ctx);
+       hop0_pte_addr = get_hop0_pte_addr(ctx, mmu_prop, hop0_addr, virt_addr);
+       curr_pte = *(u64 *) (uintptr_t) hop0_pte_addr;
+
+       hop1_addr = get_alloc_next_hop_addr(ctx, curr_pte, &hop1_new);
+       if (hop1_addr == ULLONG_MAX)
+               goto err;
+
+       hop1_pte_addr = get_hop1_pte_addr(ctx, mmu_prop, hop1_addr, virt_addr);
+       curr_pte = *(u64 *) (uintptr_t) hop1_pte_addr;
+
+       hop2_addr = get_alloc_next_hop_addr(ctx, curr_pte, &hop2_new);
+       if (hop2_addr == ULLONG_MAX)
+               goto err;
+
+       hop2_pte_addr = get_hop2_pte_addr(ctx, mmu_prop, hop2_addr, virt_addr);
+       curr_pte = *(u64 *) (uintptr_t) hop2_pte_addr;
+
+       hop3_addr = get_alloc_next_hop_addr(ctx, curr_pte, &hop3_new);
+       if (hop3_addr == ULLONG_MAX)
+               goto err;
+
+       hop3_pte_addr = get_hop3_pte_addr(ctx, mmu_prop, hop3_addr, virt_addr);
+       curr_pte = *(u64 *) (uintptr_t) hop3_pte_addr;
+
+       if (!is_huge) {
+               hop4_addr = get_alloc_next_hop_addr(ctx, curr_pte, &hop4_new);
+               if (hop4_addr == ULLONG_MAX)
+                       goto err;
+
+               hop4_pte_addr = get_hop4_pte_addr(ctx, mmu_prop, hop4_addr,
+                                                       virt_addr);
+               curr_pte = *(u64 *) (uintptr_t) hop4_pte_addr;
+       }
+
+       if (hdev->dram_default_page_mapping && is_dram_addr) {
+               u64 default_pte = (prop->mmu_dram_default_page_addr &
+                                       HOP_PHYS_ADDR_MASK) | LAST_MASK |
+                                               PAGE_PRESENT_MASK;
+
+               if (curr_pte != default_pte) {
+                       dev_err(hdev->dev,
+                               "DRAM: mapping already exists for virt_addr 0x%llx\n",
+                                       virt_addr);
+                       rc = -EINVAL;
+                       goto err;
+               }
+
+               if (hop1_new || hop2_new || hop3_new || hop4_new) {
+                       dev_err(hdev->dev,
+                               "DRAM mapping should not allocate more hops\n");
+                       rc = -EFAULT;
+                       goto err;
+               }
+       } else if (curr_pte & PAGE_PRESENT_MASK) {
+               dev_err(hdev->dev,
+                       "mapping already exists for virt_addr 0x%llx\n",
+                               virt_addr);
+
+               dev_dbg(hdev->dev, "hop0 pte: 0x%llx (0x%llx)\n",
+                       *(u64 *) (uintptr_t) hop0_pte_addr, hop0_pte_addr);
+               dev_dbg(hdev->dev, "hop1 pte: 0x%llx (0x%llx)\n",
+                       *(u64 *) (uintptr_t) hop1_pte_addr, hop1_pte_addr);
+               dev_dbg(hdev->dev, "hop2 pte: 0x%llx (0x%llx)\n",
+                       *(u64 *) (uintptr_t) hop2_pte_addr, hop2_pte_addr);
+               dev_dbg(hdev->dev, "hop3 pte: 0x%llx (0x%llx)\n",
+                       *(u64 *) (uintptr_t) hop3_pte_addr, hop3_pte_addr);
+
+               if (!is_huge)
+                       dev_dbg(hdev->dev, "hop4 pte: 0x%llx (0x%llx)\n",
+                               *(u64 *) (uintptr_t) hop4_pte_addr,
+                               hop4_pte_addr);
+
+               rc = -EINVAL;
+               goto err;
+       }
+
+       curr_pte = (phys_addr & HOP_PHYS_ADDR_MASK) | LAST_MASK
+                       | PAGE_PRESENT_MASK;
+
+       if (is_huge)
+               write_final_pte(ctx, hop3_pte_addr, curr_pte);
+       else
+               write_final_pte(ctx, hop4_pte_addr, curr_pte);
+
+       if (hop1_new) {
+               curr_pte =
+                       (hop1_addr & HOP_PHYS_ADDR_MASK) | PAGE_PRESENT_MASK;
+               write_pte(ctx, hop0_pte_addr, curr_pte);
+       }
+       if (hop2_new) {
+               curr_pte =
+                       (hop2_addr & HOP_PHYS_ADDR_MASK) | PAGE_PRESENT_MASK;
+               write_pte(ctx, hop1_pte_addr, curr_pte);
+               get_pte(ctx, hop1_addr);
+       }
+       if (hop3_new) {
+               curr_pte =
+                       (hop3_addr & HOP_PHYS_ADDR_MASK) | PAGE_PRESENT_MASK;
+               write_pte(ctx, hop2_pte_addr, curr_pte);
+               get_pte(ctx, hop2_addr);
+       }
+
+       if (!is_huge) {
+               if (hop4_new) {
+                       curr_pte = (hop4_addr & HOP_PHYS_ADDR_MASK) |
+                                       PAGE_PRESENT_MASK;
+                       write_pte(ctx, hop3_pte_addr, curr_pte);
+                       get_pte(ctx, hop3_addr);
+               }
+
+               get_pte(ctx, hop4_addr);
+       } else {
+               get_pte(ctx, hop3_addr);
+       }
+
+       return 0;
+
+err:
+       if (hop4_new)
+               free_hop(ctx, hop4_addr);
+       if (hop3_new)
+               free_hop(ctx, hop3_addr);
+       if (hop2_new)
+               free_hop(ctx, hop2_addr);
+       if (hop1_new)
+               free_hop(ctx, hop1_addr);
+
+       return rc;
+}
+
+/*
+ * hl_mmu_v1_swap_out - marks all mapping of the given ctx as swapped out
+ *
+ * @ctx: pointer to the context structure
+ *
+ */
+static void hl_mmu_v1_swap_out(struct hl_ctx *ctx)
+{
+
+}
+
+/*
+ * hl_mmu_v1_swap_in - marks all mapping of the given ctx as swapped in
+ *
+ * @ctx: pointer to the context structure
+ *
+ */
+static void hl_mmu_v1_swap_in(struct hl_ctx *ctx)
+{
+
+}
+
+static inline u64 get_hop_pte_addr(struct hl_ctx *ctx,
+                               struct hl_mmu_properties *mmu_prop,
+                               int hop_num, u64 hop_addr, u64 virt_addr)
+{
+       switch (hop_num) {
+       case 0:
+               return get_hop0_pte_addr(ctx, mmu_prop, hop_addr, virt_addr);
+       case 1:
+               return get_hop1_pte_addr(ctx, mmu_prop, hop_addr, virt_addr);
+       case 2:
+               return get_hop2_pte_addr(ctx, mmu_prop, hop_addr, virt_addr);
+       case 3:
+               return get_hop3_pte_addr(ctx, mmu_prop, hop_addr, virt_addr);
+       case 4:
+               return get_hop4_pte_addr(ctx, mmu_prop, hop_addr, virt_addr);
+       default:
+               break;
+       }
+       return U64_MAX;
+}
+
+static int hl_mmu_v1_get_tlb_info(struct hl_ctx *ctx, u64 virt_addr,
+                               struct hl_mmu_hop_info *hops)
+{
+       struct hl_device *hdev = ctx->hdev;
+       struct asic_fixed_properties *prop = &hdev->asic_prop;
+       struct hl_mmu_properties *mmu_prop;
+       bool is_dram_addr, is_pmmu_addr, is_pmmu_h_addr, is_huge;
+       int i, used_hops;
+
+       is_dram_addr = hl_mem_area_inside_range(virt_addr, prop->dmmu.page_size,
+                                               prop->dmmu.start_addr,
+                                               prop->dmmu.end_addr);
+       is_pmmu_addr = hl_mem_area_inside_range(virt_addr, prop->pmmu.page_size,
+                                               prop->pmmu.start_addr,
+                                               prop->pmmu.end_addr);
+       is_pmmu_h_addr = hl_mem_area_inside_range(virt_addr,
+                                               prop->pmmu_huge.page_size,
+                                               prop->pmmu_huge.start_addr,
+                                               prop->pmmu_huge.end_addr);
+       if (is_dram_addr) {
+               mmu_prop = &prop->dmmu;
+               is_huge = true;
+       } else if (is_pmmu_addr) {
+               mmu_prop = &prop->pmmu;
+               is_huge = false;
+       } else if (is_pmmu_h_addr) {
+               mmu_prop = &prop->pmmu_huge;
+               is_huge = true;
+       } else {
+               return -EINVAL;
+       }
+
+       used_hops = mmu_prop->num_hops;
+
+       /* huge pages use lesser hops */
+       if (is_huge)
+               used_hops--;
+
+       hops->hop_info[0].hop_addr = get_phys_hop0_addr(ctx);
+       hops->hop_info[0].hop_pte_addr =
+                       get_hop_pte_addr(ctx, mmu_prop, 0,
+                                       hops->hop_info[0].hop_addr, virt_addr);
+       hops->hop_info[0].hop_pte_val =
+                       hdev->asic_funcs->read_pte(hdev,
+                                               hops->hop_info[0].hop_pte_addr);
+
+       for (i = 1 ; i < used_hops ; i++) {
+               hops->hop_info[i].hop_addr =
+                       get_next_hop_addr(ctx,
+                                       hops->hop_info[i - 1].hop_pte_val);
+               if (hops->hop_info[i].hop_addr == ULLONG_MAX)
+                       return -EFAULT;
+
+               hops->hop_info[i].hop_pte_addr =
+                               get_hop_pte_addr(ctx, mmu_prop, i,
+                                               hops->hop_info[i].hop_addr,
+                                               virt_addr);
+               hops->hop_info[i].hop_pte_val =
+                               hdev->asic_funcs->read_pte(hdev,
+                                               hops->hop_info[i].hop_pte_addr);
+
+               if (!(hops->hop_info[i].hop_pte_val & PAGE_PRESENT_MASK))
+                       return -EFAULT;
+
+               if (hops->hop_info[i].hop_pte_val & LAST_MASK)
+                       break;
+       }
+
+       /* if passed over all hops then no last hop was found */
+       if (i == mmu_prop->num_hops)
+               return -EFAULT;
+
+       if (!(hops->hop_info[i].hop_pte_val & PAGE_PRESENT_MASK))
+               return -EFAULT;
+
+       hops->used_hops = i + 1;
+
+       return 0;
+}
+
+/*
+ * hl_mmu_v1_prepare - prepare mmu  for working with mmu v1
+ *
+ * @hdev: pointer to the device structure
+ */
+void hl_mmu_v1_set_funcs(struct hl_device *hdev, struct hl_mmu_funcs *mmu)
+{
+       mmu->init = hl_mmu_v1_init;
+       mmu->fini = hl_mmu_v1_fini;
+       mmu->ctx_init = hl_mmu_v1_ctx_init;
+       mmu->ctx_fini = hl_mmu_v1_ctx_fini;
+       mmu->map = _hl_mmu_v1_map;
+       mmu->unmap = _hl_mmu_v1_unmap;
+       mmu->flush = flush;
+       mmu->swap_out = hl_mmu_v1_swap_out;
+       mmu->swap_in = hl_mmu_v1_swap_in;
+       mmu->get_tlb_info = hl_mmu_v1_get_tlb_info;
+}
diff --git a/drivers/misc/habanalabs/common/mmu_v1.c b/drivers/misc/habanalabs/common/mmu_v1.c
deleted file mode 100644 (file)
index 06d8a44..0000000
+++ /dev/null
@@ -1,966 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-
-/*
- * Copyright 2016-2019 HabanaLabs, Ltd.
- * All Rights Reserved.
- */
-
-#include "habanalabs.h"
-#include "../include/hw_ip/mmu/mmu_general.h"
-
-#include <linux/slab.h>
-
-static inline u64 get_phys_addr(struct hl_ctx *ctx, u64 shadow_addr);
-
-static struct pgt_info *get_pgt_info(struct hl_ctx *ctx, u64 hop_addr)
-{
-       struct pgt_info *pgt_info = NULL;
-
-       hash_for_each_possible(ctx->mmu_shadow_hash, pgt_info, node,
-                               (unsigned long) hop_addr)
-               if (hop_addr == pgt_info->shadow_addr)
-                       break;
-
-       return pgt_info;
-}
-
-static void _free_hop(struct hl_ctx *ctx, struct pgt_info *pgt_info)
-{
-       struct hl_device *hdev = ctx->hdev;
-
-       gen_pool_free(hdev->mmu_priv.dr.mmu_pgt_pool, pgt_info->phys_addr,
-                       hdev->asic_prop.mmu_hop_table_size);
-       hash_del(&pgt_info->node);
-       kfree((u64 *) (uintptr_t) pgt_info->shadow_addr);
-       kfree(pgt_info);
-}
-
-static void free_hop(struct hl_ctx *ctx, u64 hop_addr)
-{
-       struct pgt_info *pgt_info = get_pgt_info(ctx, hop_addr);
-
-       _free_hop(ctx, pgt_info);
-}
-
-static u64 alloc_hop(struct hl_ctx *ctx)
-{
-       struct hl_device *hdev = ctx->hdev;
-       struct asic_fixed_properties *prop = &hdev->asic_prop;
-       struct pgt_info *pgt_info;
-       u64 phys_addr, shadow_addr;
-
-       pgt_info = kmalloc(sizeof(*pgt_info), GFP_KERNEL);
-       if (!pgt_info)
-               return ULLONG_MAX;
-
-       phys_addr = (u64) gen_pool_alloc(hdev->mmu_priv.dr.mmu_pgt_pool,
-                                       prop->mmu_hop_table_size);
-       if (!phys_addr) {
-               dev_err(hdev->dev, "failed to allocate page\n");
-               goto pool_add_err;
-       }
-
-       shadow_addr = (u64) (uintptr_t) kzalloc(prop->mmu_hop_table_size,
-                                               GFP_KERNEL);
-       if (!shadow_addr)
-               goto shadow_err;
-
-       pgt_info->phys_addr = phys_addr;
-       pgt_info->shadow_addr = shadow_addr;
-       pgt_info->ctx = ctx;
-       pgt_info->num_of_ptes = 0;
-       hash_add(ctx->mmu_shadow_hash, &pgt_info->node, shadow_addr);
-
-       return shadow_addr;
-
-shadow_err:
-       gen_pool_free(hdev->mmu_priv.dr.mmu_pgt_pool, phys_addr,
-                       prop->mmu_hop_table_size);
-pool_add_err:
-       kfree(pgt_info);
-
-       return ULLONG_MAX;
-}
-
-static inline u64 get_phys_hop0_addr(struct hl_ctx *ctx)
-{
-       return ctx->hdev->asic_prop.mmu_pgt_addr +
-                       (ctx->asid * ctx->hdev->asic_prop.mmu_hop_table_size);
-}
-
-static inline u64 get_hop0_addr(struct hl_ctx *ctx)
-{
-       return (u64) (uintptr_t) ctx->hdev->mmu_priv.dr.mmu_shadow_hop0 +
-                       (ctx->asid * ctx->hdev->asic_prop.mmu_hop_table_size);
-}
-
-static void flush(struct hl_ctx *ctx)
-{
-       /* flush all writes from all cores to reach PCI */
-       mb();
-       ctx->hdev->asic_funcs->read_pte(ctx->hdev, get_phys_hop0_addr(ctx));
-}
-
-/* transform the value to physical address when writing to H/W */
-static inline void write_pte(struct hl_ctx *ctx, u64 shadow_pte_addr, u64 val)
-{
-       /*
-        * The value to write is actually the address of the next shadow hop +
-        * flags at the 12 LSBs.
-        * Hence in order to get the value to write to the physical PTE, we
-        * clear the 12 LSBs and translate the shadow hop to its associated
-        * physical hop, and add back the original 12 LSBs.
-        */
-       u64 phys_val = get_phys_addr(ctx, val & HOP_PHYS_ADDR_MASK) |
-                               (val & FLAGS_MASK);
-
-       ctx->hdev->asic_funcs->write_pte(ctx->hdev,
-                                       get_phys_addr(ctx, shadow_pte_addr),
-                                       phys_val);
-
-       *(u64 *) (uintptr_t) shadow_pte_addr = val;
-}
-
-/* do not transform the value to physical address when writing to H/W */
-static inline void write_final_pte(struct hl_ctx *ctx, u64 shadow_pte_addr,
-                                       u64 val)
-{
-       ctx->hdev->asic_funcs->write_pte(ctx->hdev,
-                                       get_phys_addr(ctx, shadow_pte_addr),
-                                       val);
-       *(u64 *) (uintptr_t) shadow_pte_addr = val;
-}
-
-/* clear the last and present bits */
-static inline void clear_pte(struct hl_ctx *ctx, u64 pte_addr)
-{
-       /* no need to transform the value to physical address */
-       write_final_pte(ctx, pte_addr, 0);
-}
-
-static inline void get_pte(struct hl_ctx *ctx, u64 hop_addr)
-{
-       get_pgt_info(ctx, hop_addr)->num_of_ptes++;
-}
-
-/*
- * put_pte - decrement the num of ptes and free the hop if possible
- *
- * @ctx: pointer to the context structure
- * @hop_addr: addr of the hop
- *
- * This function returns the number of ptes left on this hop. If the number is
- * 0, it means the pte was freed.
- */
-static inline int put_pte(struct hl_ctx *ctx, u64 hop_addr)
-{
-       struct pgt_info *pgt_info = get_pgt_info(ctx, hop_addr);
-       int num_of_ptes_left;
-
-       pgt_info->num_of_ptes--;
-
-       /*
-        * Need to save the number of ptes left because free_hop might free
-        * the pgt_info
-        */
-       num_of_ptes_left = pgt_info->num_of_ptes;
-       if (!num_of_ptes_left)
-               _free_hop(ctx, pgt_info);
-
-       return num_of_ptes_left;
-}
-
-static inline u64 get_hopN_pte_addr(struct hl_ctx *ctx, u64 hop_addr,
-                                       u64 virt_addr, u64 mask, u64 shift)
-{
-       return hop_addr + ctx->hdev->asic_prop.mmu_pte_size *
-                       ((virt_addr & mask) >> shift);
-}
-
-static inline u64 get_hop0_pte_addr(struct hl_ctx *ctx,
-                                       struct hl_mmu_properties *mmu_prop,
-                                       u64 hop_addr, u64 vaddr)
-{
-       return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_prop->hop0_mask,
-                                       mmu_prop->hop0_shift);
-}
-
-static inline u64 get_hop1_pte_addr(struct hl_ctx *ctx,
-                                       struct hl_mmu_properties *mmu_prop,
-                                       u64 hop_addr, u64 vaddr)
-{
-       return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_prop->hop1_mask,
-                                       mmu_prop->hop1_shift);
-}
-
-static inline u64 get_hop2_pte_addr(struct hl_ctx *ctx,
-                                       struct hl_mmu_properties *mmu_prop,
-                                       u64 hop_addr, u64 vaddr)
-{
-       return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_prop->hop2_mask,
-                                       mmu_prop->hop2_shift);
-}
-
-static inline u64 get_hop3_pte_addr(struct hl_ctx *ctx,
-                                       struct hl_mmu_properties *mmu_prop,
-                                       u64 hop_addr, u64 vaddr)
-{
-       return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_prop->hop3_mask,
-                                       mmu_prop->hop3_shift);
-}
-
-static inline u64 get_hop4_pte_addr(struct hl_ctx *ctx,
-                                       struct hl_mmu_properties *mmu_prop,
-                                       u64 hop_addr, u64 vaddr)
-{
-       return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_prop->hop4_mask,
-                                       mmu_prop->hop4_shift);
-}
-
-static inline u64 get_next_hop_addr(struct hl_ctx *ctx, u64 curr_pte)
-{
-       if (curr_pte & PAGE_PRESENT_MASK)
-               return curr_pte & HOP_PHYS_ADDR_MASK;
-       else
-               return ULLONG_MAX;
-}
-
-static inline u64 get_alloc_next_hop_addr(struct hl_ctx *ctx, u64 curr_pte,
-                                               bool *is_new_hop)
-{
-       u64 hop_addr = get_next_hop_addr(ctx, curr_pte);
-
-       if (hop_addr == ULLONG_MAX) {
-               hop_addr = alloc_hop(ctx);
-               *is_new_hop = (hop_addr != ULLONG_MAX);
-       }
-
-       return hop_addr;
-}
-
-/* translates shadow address inside hop to a physical address */
-static inline u64 get_phys_addr(struct hl_ctx *ctx, u64 shadow_addr)
-{
-       u64 page_mask = (ctx->hdev->asic_prop.mmu_hop_table_size - 1);
-       u64 shadow_hop_addr = shadow_addr & ~page_mask;
-       u64 pte_offset = shadow_addr & page_mask;
-       u64 phys_hop_addr;
-
-       if (shadow_hop_addr != get_hop0_addr(ctx))
-               phys_hop_addr = get_pgt_info(ctx, shadow_hop_addr)->phys_addr;
-       else
-               phys_hop_addr = get_phys_hop0_addr(ctx);
-
-       return phys_hop_addr + pte_offset;
-}
-
-static int dram_default_mapping_init(struct hl_ctx *ctx)
-{
-       struct hl_device *hdev = ctx->hdev;
-       struct asic_fixed_properties *prop = &hdev->asic_prop;
-       u64 num_of_hop3, total_hops, hop0_addr, hop1_addr, hop2_addr,
-               hop2_pte_addr, hop3_pte_addr, pte_val;
-       int rc, i, j, hop3_allocated = 0;
-
-       if ((!prop->dram_supports_virtual_memory) ||
-                       (!hdev->dram_default_page_mapping) ||
-                       (ctx->asid == HL_KERNEL_ASID_ID))
-               return 0;
-
-       num_of_hop3 = prop->dram_size_for_default_page_mapping;
-       do_div(num_of_hop3, prop->dram_page_size);
-       do_div(num_of_hop3, PTE_ENTRIES_IN_HOP);
-
-       /* add hop1 and hop2 */
-       total_hops = num_of_hop3 + 2;
-
-       ctx->dram_default_hops = kzalloc(HL_PTE_SIZE * total_hops,  GFP_KERNEL);
-       if (!ctx->dram_default_hops)
-               return -ENOMEM;
-
-       hop0_addr = get_hop0_addr(ctx);
-
-       hop1_addr = alloc_hop(ctx);
-       if (hop1_addr == ULLONG_MAX) {
-               dev_err(hdev->dev, "failed to alloc hop 1\n");
-               rc = -ENOMEM;
-               goto hop1_err;
-       }
-
-       ctx->dram_default_hops[total_hops - 1] = hop1_addr;
-
-       hop2_addr = alloc_hop(ctx);
-       if (hop2_addr == ULLONG_MAX) {
-               dev_err(hdev->dev, "failed to alloc hop 2\n");
-               rc = -ENOMEM;
-               goto hop2_err;
-       }
-
-       ctx->dram_default_hops[total_hops - 2] = hop2_addr;
-
-       for (i = 0 ; i < num_of_hop3 ; i++) {
-               ctx->dram_default_hops[i] = alloc_hop(ctx);
-               if (ctx->dram_default_hops[i] == ULLONG_MAX) {
-                       dev_err(hdev->dev, "failed to alloc hop 3, i: %d\n", i);
-                       rc = -ENOMEM;
-                       goto hop3_err;
-               }
-               hop3_allocated++;
-       }
-
-       /* need only pte 0 in hops 0 and 1 */
-       pte_val = (hop1_addr & HOP_PHYS_ADDR_MASK) | PAGE_PRESENT_MASK;
-       write_pte(ctx, hop0_addr, pte_val);
-
-       pte_val = (hop2_addr & HOP_PHYS_ADDR_MASK) | PAGE_PRESENT_MASK;
-       write_pte(ctx, hop1_addr, pte_val);
-       get_pte(ctx, hop1_addr);
-
-       hop2_pte_addr = hop2_addr;
-       for (i = 0 ; i < num_of_hop3 ; i++) {
-               pte_val = (ctx->dram_default_hops[i] & HOP_PHYS_ADDR_MASK) |
-                               PAGE_PRESENT_MASK;
-               write_pte(ctx, hop2_pte_addr, pte_val);
-               get_pte(ctx, hop2_addr);
-               hop2_pte_addr += HL_PTE_SIZE;
-       }
-
-       pte_val = (prop->mmu_dram_default_page_addr & HOP_PHYS_ADDR_MASK) |
-                       LAST_MASK | PAGE_PRESENT_MASK;
-
-       for (i = 0 ; i < num_of_hop3 ; i++) {
-               hop3_pte_addr = ctx->dram_default_hops[i];
-               for (j = 0 ; j < PTE_ENTRIES_IN_HOP ; j++) {
-                       write_final_pte(ctx, hop3_pte_addr, pte_val);
-                       get_pte(ctx, ctx->dram_default_hops[i]);
-                       hop3_pte_addr += HL_PTE_SIZE;
-               }
-       }
-
-       flush(ctx);
-
-       return 0;
-
-hop3_err:
-       for (i = 0 ; i < hop3_allocated ; i++)
-               free_hop(ctx, ctx->dram_default_hops[i]);
-
-       free_hop(ctx, hop2_addr);
-hop2_err:
-       free_hop(ctx, hop1_addr);
-hop1_err:
-       kfree(ctx->dram_default_hops);
-
-       return rc;
-}
-
-static void dram_default_mapping_fini(struct hl_ctx *ctx)
-{
-       struct hl_device *hdev = ctx->hdev;
-       struct asic_fixed_properties *prop = &hdev->asic_prop;
-       u64 num_of_hop3, total_hops, hop0_addr, hop1_addr, hop2_addr,
-               hop2_pte_addr, hop3_pte_addr;
-       int i, j;
-
-       if ((!prop->dram_supports_virtual_memory) ||
-                       (!hdev->dram_default_page_mapping) ||
-                       (ctx->asid == HL_KERNEL_ASID_ID))
-               return;
-
-       num_of_hop3 = prop->dram_size_for_default_page_mapping;
-       do_div(num_of_hop3, prop->dram_page_size);
-       do_div(num_of_hop3, PTE_ENTRIES_IN_HOP);
-
-       hop0_addr = get_hop0_addr(ctx);
-       /* add hop1 and hop2 */
-       total_hops = num_of_hop3 + 2;
-       hop1_addr = ctx->dram_default_hops[total_hops - 1];
-       hop2_addr = ctx->dram_default_hops[total_hops - 2];
-
-       for (i = 0 ; i < num_of_hop3 ; i++) {
-               hop3_pte_addr = ctx->dram_default_hops[i];
-               for (j = 0 ; j < PTE_ENTRIES_IN_HOP ; j++) {
-                       clear_pte(ctx, hop3_pte_addr);
-                       put_pte(ctx, ctx->dram_default_hops[i]);
-                       hop3_pte_addr += HL_PTE_SIZE;
-               }
-       }
-
-       hop2_pte_addr = hop2_addr;
-       hop2_pte_addr = hop2_addr;
-       for (i = 0 ; i < num_of_hop3 ; i++) {
-               clear_pte(ctx, hop2_pte_addr);
-               put_pte(ctx, hop2_addr);
-               hop2_pte_addr += HL_PTE_SIZE;
-       }
-
-       clear_pte(ctx, hop1_addr);
-       put_pte(ctx, hop1_addr);
-       clear_pte(ctx, hop0_addr);
-
-       kfree(ctx->dram_default_hops);
-
-       flush(ctx);
-}
-
-/**
- * hl_mmu_v1_init() - initialize the MMU module.
- * @hdev: habanalabs device structure.
- *
- * This function does the following:
- * - Create a pool of pages for pgt_infos.
- * - Create a shadow table for pgt
- *
- * Return: 0 for success, non-zero for failure.
- */
-static int hl_mmu_v1_init(struct hl_device *hdev)
-{
-       struct asic_fixed_properties *prop = &hdev->asic_prop;
-       int rc;
-
-       hdev->mmu_priv.dr.mmu_pgt_pool =
-                       gen_pool_create(__ffs(prop->mmu_hop_table_size), -1);
-
-       if (!hdev->mmu_priv.dr.mmu_pgt_pool) {
-               dev_err(hdev->dev, "Failed to create page gen pool\n");
-               return -ENOMEM;
-       }
-
-       rc = gen_pool_add(hdev->mmu_priv.dr.mmu_pgt_pool, prop->mmu_pgt_addr +
-                       prop->mmu_hop0_tables_total_size,
-                       prop->mmu_pgt_size - prop->mmu_hop0_tables_total_size,
-                       -1);
-       if (rc) {
-               dev_err(hdev->dev, "Failed to add memory to page gen pool\n");
-               goto err_pool_add;
-       }
-
-       hdev->mmu_priv.dr.mmu_shadow_hop0 = kvmalloc_array(prop->max_asid,
-                                               prop->mmu_hop_table_size,
-                                               GFP_KERNEL | __GFP_ZERO);
-       if (ZERO_OR_NULL_PTR(hdev->mmu_priv.dr.mmu_shadow_hop0)) {
-               rc = -ENOMEM;
-               goto err_pool_add;
-       }
-
-       /* MMU H/W init will be done in device hw_init() */
-
-       return 0;
-
-err_pool_add:
-       gen_pool_destroy(hdev->mmu_priv.dr.mmu_pgt_pool);
-
-       return rc;
-}
-
-/**
- * hl_mmu_fini() - release the MMU module.
- * @hdev: habanalabs device structure.
- *
- * This function does the following:
- * - Disable MMU in H/W.
- * - Free the pgt_infos pool.
- *
- * All contexts should be freed before calling this function.
- */
-static void hl_mmu_v1_fini(struct hl_device *hdev)
-{
-       /* MMU H/W fini was already done in device hw_fini() */
-
-       if (!ZERO_OR_NULL_PTR(hdev->mmu_priv.hr.mmu_shadow_hop0)) {
-               kvfree(hdev->mmu_priv.dr.mmu_shadow_hop0);
-               gen_pool_destroy(hdev->mmu_priv.dr.mmu_pgt_pool);
-       }
-
-       /* Make sure that if we arrive here again without init was called we
-        * won't cause kernel panic. This can happen for example if we fail
-        * during hard reset code at certain points
-        */
-       hdev->mmu_priv.dr.mmu_shadow_hop0 = NULL;
-}
-
-/**
- * hl_mmu_ctx_init() - initialize a context for using the MMU module.
- * @ctx: pointer to the context structure to initialize.
- *
- * Initialize a mutex to protect the concurrent mapping flow, a hash to hold all
- * page tables hops related to this context.
- * Return: 0 on success, non-zero otherwise.
- */
-static int hl_mmu_v1_ctx_init(struct hl_ctx *ctx)
-{
-       hash_init(ctx->mmu_shadow_hash);
-       return dram_default_mapping_init(ctx);
-}
-
-/*
- * hl_mmu_ctx_fini - disable a ctx from using the mmu module
- *
- * @ctx: pointer to the context structure
- *
- * This function does the following:
- * - Free any pgts which were not freed yet
- * - Free the mutex
- * - Free DRAM default page mapping hops
- */
-static void hl_mmu_v1_ctx_fini(struct hl_ctx *ctx)
-{
-       struct hl_device *hdev = ctx->hdev;
-       struct pgt_info *pgt_info;
-       struct hlist_node *tmp;
-       int i;
-
-       dram_default_mapping_fini(ctx);
-
-       if (!hash_empty(ctx->mmu_shadow_hash))
-               dev_err(hdev->dev, "ctx %d is freed while it has pgts in use\n",
-                       ctx->asid);
-
-       hash_for_each_safe(ctx->mmu_shadow_hash, i, tmp, pgt_info, node) {
-               dev_err_ratelimited(hdev->dev,
-                       "pgt_info of addr 0x%llx of asid %d was not destroyed, num_ptes: %d\n",
-                       pgt_info->phys_addr, ctx->asid, pgt_info->num_of_ptes);
-               _free_hop(ctx, pgt_info);
-       }
-}
-
-static int _hl_mmu_v1_unmap(struct hl_ctx *ctx,
-                               u64 virt_addr, bool is_dram_addr)
-{
-       struct hl_device *hdev = ctx->hdev;
-       struct asic_fixed_properties *prop = &hdev->asic_prop;
-       struct hl_mmu_properties *mmu_prop;
-       u64 hop0_addr = 0, hop0_pte_addr = 0,
-               hop1_addr = 0, hop1_pte_addr = 0,
-               hop2_addr = 0, hop2_pte_addr = 0,
-               hop3_addr = 0, hop3_pte_addr = 0,
-               hop4_addr = 0, hop4_pte_addr = 0,
-               curr_pte;
-       bool is_huge, clear_hop3 = true;
-
-       /* shifts and masks are the same in PMMU and HPMMU, use one of them */
-       mmu_prop = is_dram_addr ? &prop->dmmu : &prop->pmmu;
-
-       hop0_addr = get_hop0_addr(ctx);
-       hop0_pte_addr = get_hop0_pte_addr(ctx, mmu_prop, hop0_addr, virt_addr);
-
-       curr_pte = *(u64 *) (uintptr_t) hop0_pte_addr;
-
-       hop1_addr = get_next_hop_addr(ctx, curr_pte);
-
-       if (hop1_addr == ULLONG_MAX)
-               goto not_mapped;
-
-       hop1_pte_addr = get_hop1_pte_addr(ctx, mmu_prop, hop1_addr, virt_addr);
-
-       curr_pte = *(u64 *) (uintptr_t) hop1_pte_addr;
-
-       hop2_addr = get_next_hop_addr(ctx, curr_pte);
-
-       if (hop2_addr == ULLONG_MAX)
-               goto not_mapped;
-
-       hop2_pte_addr = get_hop2_pte_addr(ctx, mmu_prop, hop2_addr, virt_addr);
-
-       curr_pte = *(u64 *) (uintptr_t) hop2_pte_addr;
-
-       hop3_addr = get_next_hop_addr(ctx, curr_pte);
-
-       if (hop3_addr == ULLONG_MAX)
-               goto not_mapped;
-
-       hop3_pte_addr = get_hop3_pte_addr(ctx, mmu_prop, hop3_addr, virt_addr);
-
-       curr_pte = *(u64 *) (uintptr_t) hop3_pte_addr;
-
-       is_huge = curr_pte & LAST_MASK;
-
-       if (is_dram_addr && !is_huge) {
-               dev_err(hdev->dev,
-                               "DRAM unmapping should use huge pages only\n");
-               return -EFAULT;
-       }
-
-       if (!is_huge) {
-               hop4_addr = get_next_hop_addr(ctx, curr_pte);
-
-               if (hop4_addr == ULLONG_MAX)
-                       goto not_mapped;
-
-               hop4_pte_addr = get_hop4_pte_addr(ctx, mmu_prop, hop4_addr,
-                                                       virt_addr);
-
-               curr_pte = *(u64 *) (uintptr_t) hop4_pte_addr;
-
-               clear_hop3 = false;
-       }
-
-       if (hdev->dram_default_page_mapping && is_dram_addr) {
-               u64 default_pte = (prop->mmu_dram_default_page_addr &
-                               HOP_PHYS_ADDR_MASK) | LAST_MASK |
-                                       PAGE_PRESENT_MASK;
-               if (curr_pte == default_pte) {
-                       dev_err(hdev->dev,
-                               "DRAM: hop3 PTE points to zero page, can't unmap, va: 0x%llx\n",
-                                       virt_addr);
-                       goto not_mapped;
-               }
-
-               if (!(curr_pte & PAGE_PRESENT_MASK)) {
-                       dev_err(hdev->dev,
-                               "DRAM: hop3 PTE is cleared! can't unmap, va: 0x%llx\n",
-                                       virt_addr);
-                       goto not_mapped;
-               }
-
-               write_final_pte(ctx, hop3_pte_addr, default_pte);
-               put_pte(ctx, hop3_addr);
-       } else {
-               if (!(curr_pte & PAGE_PRESENT_MASK))
-                       goto not_mapped;
-
-               if (hop4_addr)
-                       clear_pte(ctx, hop4_pte_addr);
-               else
-                       clear_pte(ctx, hop3_pte_addr);
-
-               if (hop4_addr && !put_pte(ctx, hop4_addr))
-                       clear_hop3 = true;
-
-               if (!clear_hop3)
-                       goto mapped;
-
-               clear_pte(ctx, hop3_pte_addr);
-
-               if (put_pte(ctx, hop3_addr))
-                       goto mapped;
-
-               clear_pte(ctx, hop2_pte_addr);
-
-               if (put_pte(ctx, hop2_addr))
-                       goto mapped;
-
-               clear_pte(ctx, hop1_pte_addr);
-
-               if (put_pte(ctx, hop1_addr))
-                       goto mapped;
-
-               clear_pte(ctx, hop0_pte_addr);
-       }
-
-mapped:
-       return 0;
-
-not_mapped:
-       dev_err(hdev->dev, "virt addr 0x%llx is not mapped to phys addr\n",
-               virt_addr);
-
-       return -EINVAL;
-}
-
-static int _hl_mmu_v1_map(struct hl_ctx *ctx, u64 virt_addr, u64 phys_addr,
-                       u32 page_size, bool is_dram_addr)
-{
-       struct hl_device *hdev = ctx->hdev;
-       struct asic_fixed_properties *prop = &hdev->asic_prop;
-       struct hl_mmu_properties *mmu_prop;
-       u64 hop0_addr = 0, hop0_pte_addr = 0,
-               hop1_addr = 0, hop1_pte_addr = 0,
-               hop2_addr = 0, hop2_pte_addr = 0,
-               hop3_addr = 0, hop3_pte_addr = 0,
-               hop4_addr = 0, hop4_pte_addr = 0,
-               curr_pte = 0;
-       bool hop1_new = false, hop2_new = false, hop3_new = false,
-               hop4_new = false, is_huge;
-       int rc = -ENOMEM;
-
-       /*
-        * This mapping function can map a page or a huge page. For huge page
-        * there are only 3 hops rather than 4. Currently the DRAM allocation
-        * uses huge pages only but user memory could have been allocated with
-        * one of the two page sizes. Since this is a common code for all the
-        * three cases, we need this hugs page check.
-        */
-       if (is_dram_addr) {
-               mmu_prop = &prop->dmmu;
-               is_huge = true;
-       } else if (page_size == prop->pmmu_huge.page_size) {
-               mmu_prop = &prop->pmmu_huge;
-               is_huge = true;
-       } else {
-               mmu_prop = &prop->pmmu;
-               is_huge = false;
-       }
-
-       hop0_addr = get_hop0_addr(ctx);
-       hop0_pte_addr = get_hop0_pte_addr(ctx, mmu_prop, hop0_addr, virt_addr);
-       curr_pte = *(u64 *) (uintptr_t) hop0_pte_addr;
-
-       hop1_addr = get_alloc_next_hop_addr(ctx, curr_pte, &hop1_new);
-       if (hop1_addr == ULLONG_MAX)
-               goto err;
-
-       hop1_pte_addr = get_hop1_pte_addr(ctx, mmu_prop, hop1_addr, virt_addr);
-       curr_pte = *(u64 *) (uintptr_t) hop1_pte_addr;
-
-       hop2_addr = get_alloc_next_hop_addr(ctx, curr_pte, &hop2_new);
-       if (hop2_addr == ULLONG_MAX)
-               goto err;
-
-       hop2_pte_addr = get_hop2_pte_addr(ctx, mmu_prop, hop2_addr, virt_addr);
-       curr_pte = *(u64 *) (uintptr_t) hop2_pte_addr;
-
-       hop3_addr = get_alloc_next_hop_addr(ctx, curr_pte, &hop3_new);
-       if (hop3_addr == ULLONG_MAX)
-               goto err;
-
-       hop3_pte_addr = get_hop3_pte_addr(ctx, mmu_prop, hop3_addr, virt_addr);
-       curr_pte = *(u64 *) (uintptr_t) hop3_pte_addr;
-
-       if (!is_huge) {
-               hop4_addr = get_alloc_next_hop_addr(ctx, curr_pte, &hop4_new);
-               if (hop4_addr == ULLONG_MAX)
-                       goto err;
-
-               hop4_pte_addr = get_hop4_pte_addr(ctx, mmu_prop, hop4_addr,
-                                                       virt_addr);
-               curr_pte = *(u64 *) (uintptr_t) hop4_pte_addr;
-       }
-
-       if (hdev->dram_default_page_mapping && is_dram_addr) {
-               u64 default_pte = (prop->mmu_dram_default_page_addr &
-                                       HOP_PHYS_ADDR_MASK) | LAST_MASK |
-                                               PAGE_PRESENT_MASK;
-
-               if (curr_pte != default_pte) {
-                       dev_err(hdev->dev,
-                               "DRAM: mapping already exists for virt_addr 0x%llx\n",
-                                       virt_addr);
-                       rc = -EINVAL;
-                       goto err;
-               }
-
-               if (hop1_new || hop2_new || hop3_new || hop4_new) {
-                       dev_err(hdev->dev,
-                               "DRAM mapping should not allocate more hops\n");
-                       rc = -EFAULT;
-                       goto err;
-               }
-       } else if (curr_pte & PAGE_PRESENT_MASK) {
-               dev_err(hdev->dev,
-                       "mapping already exists for virt_addr 0x%llx\n",
-                               virt_addr);
-
-               dev_dbg(hdev->dev, "hop0 pte: 0x%llx (0x%llx)\n",
-                       *(u64 *) (uintptr_t) hop0_pte_addr, hop0_pte_addr);
-               dev_dbg(hdev->dev, "hop1 pte: 0x%llx (0x%llx)\n",
-                       *(u64 *) (uintptr_t) hop1_pte_addr, hop1_pte_addr);
-               dev_dbg(hdev->dev, "hop2 pte: 0x%llx (0x%llx)\n",
-                       *(u64 *) (uintptr_t) hop2_pte_addr, hop2_pte_addr);
-               dev_dbg(hdev->dev, "hop3 pte: 0x%llx (0x%llx)\n",
-                       *(u64 *) (uintptr_t) hop3_pte_addr, hop3_pte_addr);
-
-               if (!is_huge)
-                       dev_dbg(hdev->dev, "hop4 pte: 0x%llx (0x%llx)\n",
-                               *(u64 *) (uintptr_t) hop4_pte_addr,
-                               hop4_pte_addr);
-
-               rc = -EINVAL;
-               goto err;
-       }
-
-       curr_pte = (phys_addr & HOP_PHYS_ADDR_MASK) | LAST_MASK
-                       | PAGE_PRESENT_MASK;
-
-       if (is_huge)
-               write_final_pte(ctx, hop3_pte_addr, curr_pte);
-       else
-               write_final_pte(ctx, hop4_pte_addr, curr_pte);
-
-       if (hop1_new) {
-               curr_pte =
-                       (hop1_addr & HOP_PHYS_ADDR_MASK) | PAGE_PRESENT_MASK;
-               write_pte(ctx, hop0_pte_addr, curr_pte);
-       }
-       if (hop2_new) {
-               curr_pte =
-                       (hop2_addr & HOP_PHYS_ADDR_MASK) | PAGE_PRESENT_MASK;
-               write_pte(ctx, hop1_pte_addr, curr_pte);
-               get_pte(ctx, hop1_addr);
-       }
-       if (hop3_new) {
-               curr_pte =
-                       (hop3_addr & HOP_PHYS_ADDR_MASK) | PAGE_PRESENT_MASK;
-               write_pte(ctx, hop2_pte_addr, curr_pte);
-               get_pte(ctx, hop2_addr);
-       }
-
-       if (!is_huge) {
-               if (hop4_new) {
-                       curr_pte = (hop4_addr & HOP_PHYS_ADDR_MASK) |
-                                       PAGE_PRESENT_MASK;
-                       write_pte(ctx, hop3_pte_addr, curr_pte);
-                       get_pte(ctx, hop3_addr);
-               }
-
-               get_pte(ctx, hop4_addr);
-       } else {
-               get_pte(ctx, hop3_addr);
-       }
-
-       return 0;
-
-err:
-       if (hop4_new)
-               free_hop(ctx, hop4_addr);
-       if (hop3_new)
-               free_hop(ctx, hop3_addr);
-       if (hop2_new)
-               free_hop(ctx, hop2_addr);
-       if (hop1_new)
-               free_hop(ctx, hop1_addr);
-
-       return rc;
-}
-
-/*
- * hl_mmu_v1_swap_out - marks all mapping of the given ctx as swapped out
- *
- * @ctx: pointer to the context structure
- *
- */
-static void hl_mmu_v1_swap_out(struct hl_ctx *ctx)
-{
-
-}
-
-/*
- * hl_mmu_v1_swap_in - marks all mapping of the given ctx as swapped in
- *
- * @ctx: pointer to the context structure
- *
- */
-static void hl_mmu_v1_swap_in(struct hl_ctx *ctx)
-{
-
-}
-
-static inline u64 get_hop_pte_addr(struct hl_ctx *ctx,
-                               struct hl_mmu_properties *mmu_prop,
-                               int hop_num, u64 hop_addr, u64 virt_addr)
-{
-       switch (hop_num) {
-       case 0:
-               return get_hop0_pte_addr(ctx, mmu_prop, hop_addr, virt_addr);
-       case 1:
-               return get_hop1_pte_addr(ctx, mmu_prop, hop_addr, virt_addr);
-       case 2:
-               return get_hop2_pte_addr(ctx, mmu_prop, hop_addr, virt_addr);
-       case 3:
-               return get_hop3_pte_addr(ctx, mmu_prop, hop_addr, virt_addr);
-       case 4:
-               return get_hop4_pte_addr(ctx, mmu_prop, hop_addr, virt_addr);
-       default:
-               break;
-       }
-       return U64_MAX;
-}
-
-static int hl_mmu_v1_get_tlb_info(struct hl_ctx *ctx, u64 virt_addr,
-                               struct hl_mmu_hop_info *hops)
-{
-       struct hl_device *hdev = ctx->hdev;
-       struct asic_fixed_properties *prop = &hdev->asic_prop;
-       struct hl_mmu_properties *mmu_prop;
-       bool is_dram_addr, is_pmmu_addr, is_pmmu_h_addr, is_huge;
-       int i, used_hops;
-
-       is_dram_addr = hl_mem_area_inside_range(virt_addr, prop->dmmu.page_size,
-                                               prop->dmmu.start_addr,
-                                               prop->dmmu.end_addr);
-       is_pmmu_addr = hl_mem_area_inside_range(virt_addr, prop->pmmu.page_size,
-                                               prop->pmmu.start_addr,
-                                               prop->pmmu.end_addr);
-       is_pmmu_h_addr = hl_mem_area_inside_range(virt_addr,
-                                               prop->pmmu_huge.page_size,
-                                               prop->pmmu_huge.start_addr,
-                                               prop->pmmu_huge.end_addr);
-       if (is_dram_addr) {
-               mmu_prop = &prop->dmmu;
-               is_huge = true;
-       } else if (is_pmmu_addr) {
-               mmu_prop = &prop->pmmu;
-               is_huge = false;
-       } else if (is_pmmu_h_addr) {
-               mmu_prop = &prop->pmmu_huge;
-               is_huge = true;
-       } else {
-               return -EINVAL;
-       }
-
-       used_hops = mmu_prop->num_hops;
-
-       /* huge pages use lesser hops */
-       if (is_huge)
-               used_hops--;
-
-       hops->hop_info[0].hop_addr = get_phys_hop0_addr(ctx);
-       hops->hop_info[0].hop_pte_addr =
-                       get_hop_pte_addr(ctx, mmu_prop, 0,
-                                       hops->hop_info[0].hop_addr, virt_addr);
-       hops->hop_info[0].hop_pte_val =
-                       hdev->asic_funcs->read_pte(hdev,
-                                               hops->hop_info[0].hop_pte_addr);
-
-       for (i = 1 ; i < used_hops ; i++) {
-               hops->hop_info[i].hop_addr =
-                       get_next_hop_addr(ctx,
-                                       hops->hop_info[i - 1].hop_pte_val);
-               if (hops->hop_info[i].hop_addr == ULLONG_MAX)
-                       return -EFAULT;
-
-               hops->hop_info[i].hop_pte_addr =
-                               get_hop_pte_addr(ctx, mmu_prop, i,
-                                               hops->hop_info[i].hop_addr,
-                                               virt_addr);
-               hops->hop_info[i].hop_pte_val =
-                               hdev->asic_funcs->read_pte(hdev,
-                                               hops->hop_info[i].hop_pte_addr);
-
-               if (!(hops->hop_info[i].hop_pte_val & PAGE_PRESENT_MASK))
-                       return -EFAULT;
-
-               if (hops->hop_info[i].hop_pte_val & LAST_MASK)
-                       break;
-       }
-
-       /* if passed over all hops then no last hop was found */
-       if (i == mmu_prop->num_hops)
-               return -EFAULT;
-
-       if (!(hops->hop_info[i].hop_pte_val & PAGE_PRESENT_MASK))
-               return -EFAULT;
-
-       hops->used_hops = i + 1;
-
-       return 0;
-}
-
-/*
- * hl_mmu_v1_prepare - prepare mmu  for working with mmu v1
- *
- * @hdev: pointer to the device structure
- */
-void hl_mmu_v1_set_funcs(struct hl_device *hdev, struct hl_mmu_funcs *mmu)
-{
-       mmu->init = hl_mmu_v1_init;
-       mmu->fini = hl_mmu_v1_fini;
-       mmu->ctx_init = hl_mmu_v1_ctx_init;
-       mmu->ctx_fini = hl_mmu_v1_ctx_fini;
-       mmu->map = _hl_mmu_v1_map;
-       mmu->unmap = _hl_mmu_v1_unmap;
-       mmu->flush = flush;
-       mmu->swap_out = hl_mmu_v1_swap_out;
-       mmu->swap_in = hl_mmu_v1_swap_in;
-       mmu->get_tlb_info = hl_mmu_v1_get_tlb_info;
-}
diff --git a/drivers/misc/habanalabs/common/pci.c b/drivers/misc/habanalabs/common/pci.c
deleted file mode 100644 (file)
index b4725e6..0000000
+++ /dev/null
@@ -1,407 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-
-/*
- * Copyright 2016-2019 HabanaLabs, Ltd.
- * All Rights Reserved.
- */
-
-#include "habanalabs.h"
-#include "../include/hw_ip/pci/pci_general.h"
-
-#include <linux/pci.h>
-
-#define HL_PLDM_PCI_ELBI_TIMEOUT_MSEC  (HL_PCI_ELBI_TIMEOUT_MSEC * 10)
-
-#define IATU_REGION_CTRL_REGION_EN_MASK                BIT(31)
-#define IATU_REGION_CTRL_MATCH_MODE_MASK       BIT(30)
-#define IATU_REGION_CTRL_NUM_MATCH_EN_MASK     BIT(19)
-#define IATU_REGION_CTRL_BAR_NUM_MASK          GENMASK(10, 8)
-
-/**
- * hl_pci_bars_map() - Map PCI BARs.
- * @hdev: Pointer to hl_device structure.
- * @name: Array of BAR names.
- * @is_wc: Array with flag per BAR whether a write-combined mapping is needed.
- *
- * Request PCI regions and map them to kernel virtual addresses.
- *
- * Return: 0 on success, non-zero for failure.
- */
-int hl_pci_bars_map(struct hl_device *hdev, const char * const name[3],
-                       bool is_wc[3])
-{
-       struct pci_dev *pdev = hdev->pdev;
-       int rc, i, bar;
-
-       rc = pci_request_regions(pdev, HL_NAME);
-       if (rc) {
-               dev_err(hdev->dev, "Cannot obtain PCI resources\n");
-               return rc;
-       }
-
-       for (i = 0 ; i < 3 ; i++) {
-               bar = i * 2; /* 64-bit BARs */
-               hdev->pcie_bar[bar] = is_wc[i] ?
-                               pci_ioremap_wc_bar(pdev, bar) :
-                               pci_ioremap_bar(pdev, bar);
-               if (!hdev->pcie_bar[bar]) {
-                       dev_err(hdev->dev, "pci_ioremap%s_bar failed for %s\n",
-                                       is_wc[i] ? "_wc" : "", name[i]);
-                       rc = -ENODEV;
-                       goto err;
-               }
-       }
-
-       return 0;
-
-err:
-       for (i = 2 ; i >= 0 ; i--) {
-               bar = i * 2; /* 64-bit BARs */
-               if (hdev->pcie_bar[bar])
-                       iounmap(hdev->pcie_bar[bar]);
-       }
-
-       pci_release_regions(pdev);
-
-       return rc;
-}
-
-/**
- * hl_pci_bars_unmap() - Unmap PCI BARS.
- * @hdev: Pointer to hl_device structure.
- *
- * Release all PCI BARs and unmap their virtual addresses.
- */
-static void hl_pci_bars_unmap(struct hl_device *hdev)
-{
-       struct pci_dev *pdev = hdev->pdev;
-       int i, bar;
-
-       for (i = 2 ; i >= 0 ; i--) {
-               bar = i * 2; /* 64-bit BARs */
-               iounmap(hdev->pcie_bar[bar]);
-       }
-
-       pci_release_regions(pdev);
-}
-
-/**
- * hl_pci_elbi_write() - Write through the ELBI interface.
- * @hdev: Pointer to hl_device structure.
- * @addr: Address to write to
- * @data: Data to write
- *
- * Return: 0 on success, negative value for failure.
- */
-static int hl_pci_elbi_write(struct hl_device *hdev, u64 addr, u32 data)
-{
-       struct pci_dev *pdev = hdev->pdev;
-       ktime_t timeout;
-       u64 msec;
-       u32 val;
-
-       if (hdev->pldm)
-               msec = HL_PLDM_PCI_ELBI_TIMEOUT_MSEC;
-       else
-               msec = HL_PCI_ELBI_TIMEOUT_MSEC;
-
-       /* Clear previous status */
-       pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_STS, 0);
-
-       pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_ADDR, (u32) addr);
-       pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_DATA, data);
-       pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_CTRL,
-                               PCI_CONFIG_ELBI_CTRL_WRITE);
-
-       timeout = ktime_add_ms(ktime_get(), msec);
-       for (;;) {
-               pci_read_config_dword(pdev, mmPCI_CONFIG_ELBI_STS, &val);
-               if (val & PCI_CONFIG_ELBI_STS_MASK)
-                       break;
-               if (ktime_compare(ktime_get(), timeout) > 0) {
-                       pci_read_config_dword(pdev, mmPCI_CONFIG_ELBI_STS,
-                                               &val);
-                       break;
-               }
-
-               usleep_range(300, 500);
-       }
-
-       if ((val & PCI_CONFIG_ELBI_STS_MASK) == PCI_CONFIG_ELBI_STS_DONE)
-               return 0;
-
-       if (val & PCI_CONFIG_ELBI_STS_ERR)
-               return -EIO;
-
-       if (!(val & PCI_CONFIG_ELBI_STS_MASK)) {
-               dev_err(hdev->dev, "ELBI write didn't finish in time\n");
-               return -EIO;
-       }
-
-       dev_err(hdev->dev, "ELBI write has undefined bits in status\n");
-       return -EIO;
-}
-
-/**
- * hl_pci_iatu_write() - iatu write routine.
- * @hdev: Pointer to hl_device structure.
- * @addr: Address to write to
- * @data: Data to write
- *
- * Return: 0 on success, negative value for failure.
- */
-int hl_pci_iatu_write(struct hl_device *hdev, u32 addr, u32 data)
-{
-       struct asic_fixed_properties *prop = &hdev->asic_prop;
-       u32 dbi_offset;
-       int rc;
-
-       dbi_offset = addr & 0xFFF;
-
-       /* Ignore result of writing to pcie_aux_dbi_reg_addr as it could fail
-        * in case the firmware security is enabled
-        */
-       hl_pci_elbi_write(hdev, prop->pcie_aux_dbi_reg_addr, 0x00300000);
-
-       rc = hl_pci_elbi_write(hdev, prop->pcie_dbi_base_address + dbi_offset,
-                               data);
-
-       if (rc)
-               return -EIO;
-
-       return 0;
-}
-
-/**
- * hl_pci_reset_link_through_bridge() - Reset PCI link.
- * @hdev: Pointer to hl_device structure.
- */
-static void hl_pci_reset_link_through_bridge(struct hl_device *hdev)
-{
-       struct pci_dev *pdev = hdev->pdev;
-       struct pci_dev *parent_port;
-       u16 val;
-
-       parent_port = pdev->bus->self;
-       pci_read_config_word(parent_port, PCI_BRIDGE_CONTROL, &val);
-       val |= PCI_BRIDGE_CTL_BUS_RESET;
-       pci_write_config_word(parent_port, PCI_BRIDGE_CONTROL, val);
-       ssleep(1);
-
-       val &= ~(PCI_BRIDGE_CTL_BUS_RESET);
-       pci_write_config_word(parent_port, PCI_BRIDGE_CONTROL, val);
-       ssleep(3);
-}
-
-/**
- * hl_pci_set_inbound_region() - Configure inbound region
- * @hdev: Pointer to hl_device structure.
- * @region: Inbound region number.
- * @pci_region: Inbound region parameters.
- *
- * Configure the iATU inbound region.
- *
- * Return: 0 on success, negative value for failure.
- */
-int hl_pci_set_inbound_region(struct hl_device *hdev, u8 region,
-               struct hl_inbound_pci_region *pci_region)
-{
-       struct asic_fixed_properties *prop = &hdev->asic_prop;
-       u64 bar_phys_base, region_base, region_end_address;
-       u32 offset, ctrl_reg_val;
-       int rc = 0;
-
-       /* region offset */
-       offset = (0x200 * region) + 0x100;
-
-       if (pci_region->mode == PCI_ADDRESS_MATCH_MODE) {
-               bar_phys_base = hdev->pcie_bar_phys[pci_region->bar];
-               region_base = bar_phys_base + pci_region->offset_in_bar;
-               region_end_address = region_base + pci_region->size - 1;
-
-               rc |= hl_pci_iatu_write(hdev, offset + 0x8,
-                               lower_32_bits(region_base));
-               rc |= hl_pci_iatu_write(hdev, offset + 0xC,
-                               upper_32_bits(region_base));
-               rc |= hl_pci_iatu_write(hdev, offset + 0x10,
-                               lower_32_bits(region_end_address));
-       }
-
-       /* Point to the specified address */
-       rc |= hl_pci_iatu_write(hdev, offset + 0x14,
-                       lower_32_bits(pci_region->addr));
-       rc |= hl_pci_iatu_write(hdev, offset + 0x18,
-                       upper_32_bits(pci_region->addr));
-       rc |= hl_pci_iatu_write(hdev, offset + 0x0, 0);
-
-       /* Enable + bar/address match + match enable + bar number */
-       ctrl_reg_val = FIELD_PREP(IATU_REGION_CTRL_REGION_EN_MASK, 1);
-       ctrl_reg_val |= FIELD_PREP(IATU_REGION_CTRL_MATCH_MODE_MASK,
-                       pci_region->mode);
-       ctrl_reg_val |= FIELD_PREP(IATU_REGION_CTRL_NUM_MATCH_EN_MASK, 1);
-
-       if (pci_region->mode == PCI_BAR_MATCH_MODE)
-               ctrl_reg_val |= FIELD_PREP(IATU_REGION_CTRL_BAR_NUM_MASK,
-                               pci_region->bar);
-
-       rc |= hl_pci_iatu_write(hdev, offset + 0x4, ctrl_reg_val);
-
-       /* Return the DBI window to the default location
-        * Ignore result of writing to pcie_aux_dbi_reg_addr as it could fail
-        * in case the firmware security is enabled
-        */
-       hl_pci_elbi_write(hdev, prop->pcie_aux_dbi_reg_addr, 0);
-
-       if (rc)
-               dev_err(hdev->dev, "failed to map bar %u to 0x%08llx\n",
-                               pci_region->bar, pci_region->addr);
-
-       return rc;
-}
-
-/**
- * hl_pci_set_outbound_region() - Configure outbound region 0
- * @hdev: Pointer to hl_device structure.
- * @pci_region: Outbound region parameters.
- *
- * Configure the iATU outbound region 0.
- *
- * Return: 0 on success, negative value for failure.
- */
-int hl_pci_set_outbound_region(struct hl_device *hdev,
-               struct hl_outbound_pci_region *pci_region)
-{
-       struct asic_fixed_properties *prop = &hdev->asic_prop;
-       u64 outbound_region_end_address;
-       int rc = 0;
-
-       /* Outbound Region 0 */
-       outbound_region_end_address =
-                       pci_region->addr + pci_region->size - 1;
-       rc |= hl_pci_iatu_write(hdev, 0x008,
-                               lower_32_bits(pci_region->addr));
-       rc |= hl_pci_iatu_write(hdev, 0x00C,
-                               upper_32_bits(pci_region->addr));
-       rc |= hl_pci_iatu_write(hdev, 0x010,
-                               lower_32_bits(outbound_region_end_address));
-       rc |= hl_pci_iatu_write(hdev, 0x014, 0);
-
-       if ((hdev->power9_64bit_dma_enable) && (hdev->dma_mask == 64))
-               rc |= hl_pci_iatu_write(hdev, 0x018, 0x08000000);
-       else
-               rc |= hl_pci_iatu_write(hdev, 0x018, 0);
-
-       rc |= hl_pci_iatu_write(hdev, 0x020,
-                               upper_32_bits(outbound_region_end_address));
-       /* Increase region size */
-       rc |= hl_pci_iatu_write(hdev, 0x000, 0x00002000);
-       /* Enable */
-       rc |= hl_pci_iatu_write(hdev, 0x004, 0x80000000);
-
-       /* Return the DBI window to the default location
-        * Ignore result of writing to pcie_aux_dbi_reg_addr as it could fail
-        * in case the firmware security is enabled
-        */
-       hl_pci_elbi_write(hdev, prop->pcie_aux_dbi_reg_addr, 0);
-
-       return rc;
-}
-
-/**
- * hl_pci_set_dma_mask() - Set DMA masks for the device.
- * @hdev: Pointer to hl_device structure.
- *
- * This function sets the DMA masks (regular and consistent) for a specified
- * value. If it doesn't succeed, it tries to set it to a fall-back value
- *
- * Return: 0 on success, non-zero for failure.
- */
-static int hl_pci_set_dma_mask(struct hl_device *hdev)
-{
-       struct pci_dev *pdev = hdev->pdev;
-       int rc;
-
-       /* set DMA mask */
-       rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(hdev->dma_mask));
-       if (rc) {
-               dev_err(hdev->dev,
-                       "Failed to set pci dma mask to %d bits, error %d\n",
-                       hdev->dma_mask, rc);
-               return rc;
-       }
-
-       rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(hdev->dma_mask));
-       if (rc) {
-               dev_err(hdev->dev,
-                       "Failed to set pci consistent dma mask to %d bits, error %d\n",
-                       hdev->dma_mask, rc);
-               return rc;
-       }
-
-       return 0;
-}
-
-/**
- * hl_pci_init() - PCI initialization code.
- * @hdev: Pointer to hl_device structure.
- *
- * Set DMA masks, initialize the PCI controller and map the PCI BARs.
- *
- * Return: 0 on success, non-zero for failure.
- */
-int hl_pci_init(struct hl_device *hdev)
-{
-       struct pci_dev *pdev = hdev->pdev;
-       int rc;
-
-       if (hdev->reset_pcilink)
-               hl_pci_reset_link_through_bridge(hdev);
-
-       rc = pci_enable_device_mem(pdev);
-       if (rc) {
-               dev_err(hdev->dev, "can't enable PCI device\n");
-               return rc;
-       }
-
-       pci_set_master(pdev);
-
-       rc = hdev->asic_funcs->pci_bars_map(hdev);
-       if (rc) {
-               dev_err(hdev->dev, "Failed to initialize PCI BARs\n");
-               goto disable_device;
-       }
-
-       rc = hdev->asic_funcs->init_iatu(hdev);
-       if (rc) {
-               dev_err(hdev->dev, "Failed to initialize iATU\n");
-               goto unmap_pci_bars;
-       }
-
-       rc = hl_pci_set_dma_mask(hdev);
-       if (rc)
-               goto unmap_pci_bars;
-
-       return 0;
-
-unmap_pci_bars:
-       hl_pci_bars_unmap(hdev);
-disable_device:
-       pci_clear_master(pdev);
-       pci_disable_device(pdev);
-
-       return rc;
-}
-
-/**
- * hl_fw_fini() - PCI finalization code.
- * @hdev: Pointer to hl_device structure
- *
- * Unmap PCI bars and disable PCI device.
- */
-void hl_pci_fini(struct hl_device *hdev)
-{
-       hl_pci_bars_unmap(hdev);
-
-       pci_clear_master(hdev->pdev);
-       pci_disable_device(hdev->pdev);
-}
diff --git a/drivers/misc/habanalabs/common/pci/Makefile b/drivers/misc/habanalabs/common/pci/Makefile
new file mode 100644 (file)
index 0000000..dc922a6
--- /dev/null
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+HL_COMMON_PCI_FILES := common/pci/pci.o
diff --git a/drivers/misc/habanalabs/common/pci/pci.c b/drivers/misc/habanalabs/common/pci/pci.c
new file mode 100644 (file)
index 0000000..c56ec15
--- /dev/null
@@ -0,0 +1,407 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2016-2019 HabanaLabs, Ltd.
+ * All Rights Reserved.
+ */
+
+#include "../habanalabs.h"
+#include "../../include/hw_ip/pci/pci_general.h"
+
+#include <linux/pci.h>
+
+#define HL_PLDM_PCI_ELBI_TIMEOUT_MSEC  (HL_PCI_ELBI_TIMEOUT_MSEC * 10)
+
+#define IATU_REGION_CTRL_REGION_EN_MASK                BIT(31)
+#define IATU_REGION_CTRL_MATCH_MODE_MASK       BIT(30)
+#define IATU_REGION_CTRL_NUM_MATCH_EN_MASK     BIT(19)
+#define IATU_REGION_CTRL_BAR_NUM_MASK          GENMASK(10, 8)
+
+/**
+ * hl_pci_bars_map() - Map PCI BARs.
+ * @hdev: Pointer to hl_device structure.
+ * @name: Array of BAR names.
+ * @is_wc: Array with flag per BAR whether a write-combined mapping is needed.
+ *
+ * Request PCI regions and map them to kernel virtual addresses.
+ *
+ * Return: 0 on success, non-zero for failure.
+ */
+int hl_pci_bars_map(struct hl_device *hdev, const char * const name[3],
+                       bool is_wc[3])
+{
+       struct pci_dev *pdev = hdev->pdev;
+       int rc, i, bar;
+
+       rc = pci_request_regions(pdev, HL_NAME);
+       if (rc) {
+               dev_err(hdev->dev, "Cannot obtain PCI resources\n");
+               return rc;
+       }
+
+       for (i = 0 ; i < 3 ; i++) {
+               bar = i * 2; /* 64-bit BARs */
+               hdev->pcie_bar[bar] = is_wc[i] ?
+                               pci_ioremap_wc_bar(pdev, bar) :
+                               pci_ioremap_bar(pdev, bar);
+               if (!hdev->pcie_bar[bar]) {
+                       dev_err(hdev->dev, "pci_ioremap%s_bar failed for %s\n",
+                                       is_wc[i] ? "_wc" : "", name[i]);
+                       rc = -ENODEV;
+                       goto err;
+               }
+       }
+
+       return 0;
+
+err:
+       for (i = 2 ; i >= 0 ; i--) {
+               bar = i * 2; /* 64-bit BARs */
+               if (hdev->pcie_bar[bar])
+                       iounmap(hdev->pcie_bar[bar]);
+       }
+
+       pci_release_regions(pdev);
+
+       return rc;
+}
+
+/**
+ * hl_pci_bars_unmap() - Unmap PCI BARS.
+ * @hdev: Pointer to hl_device structure.
+ *
+ * Release all PCI BARs and unmap their virtual addresses.
+ */
+static void hl_pci_bars_unmap(struct hl_device *hdev)
+{
+       struct pci_dev *pdev = hdev->pdev;
+       int i, bar;
+
+       for (i = 2 ; i >= 0 ; i--) {
+               bar = i * 2; /* 64-bit BARs */
+               iounmap(hdev->pcie_bar[bar]);
+       }
+
+       pci_release_regions(pdev);
+}
+
+/**
+ * hl_pci_elbi_write() - Write through the ELBI interface.
+ * @hdev: Pointer to hl_device structure.
+ * @addr: Address to write to
+ * @data: Data to write
+ *
+ * Return: 0 on success, negative value for failure.
+ */
+static int hl_pci_elbi_write(struct hl_device *hdev, u64 addr, u32 data)
+{
+       struct pci_dev *pdev = hdev->pdev;
+       ktime_t timeout;
+       u64 msec;
+       u32 val;
+
+       if (hdev->pldm)
+               msec = HL_PLDM_PCI_ELBI_TIMEOUT_MSEC;
+       else
+               msec = HL_PCI_ELBI_TIMEOUT_MSEC;
+
+       /* Clear previous status */
+       pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_STS, 0);
+
+       pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_ADDR, (u32) addr);
+       pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_DATA, data);
+       pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_CTRL,
+                               PCI_CONFIG_ELBI_CTRL_WRITE);
+
+       timeout = ktime_add_ms(ktime_get(), msec);
+       for (;;) {
+               pci_read_config_dword(pdev, mmPCI_CONFIG_ELBI_STS, &val);
+               if (val & PCI_CONFIG_ELBI_STS_MASK)
+                       break;
+               if (ktime_compare(ktime_get(), timeout) > 0) {
+                       pci_read_config_dword(pdev, mmPCI_CONFIG_ELBI_STS,
+                                               &val);
+                       break;
+               }
+
+               usleep_range(300, 500);
+       }
+
+       if ((val & PCI_CONFIG_ELBI_STS_MASK) == PCI_CONFIG_ELBI_STS_DONE)
+               return 0;
+
+       if (val & PCI_CONFIG_ELBI_STS_ERR)
+               return -EIO;
+
+       if (!(val & PCI_CONFIG_ELBI_STS_MASK)) {
+               dev_err(hdev->dev, "ELBI write didn't finish in time\n");
+               return -EIO;
+       }
+
+       dev_err(hdev->dev, "ELBI write has undefined bits in status\n");
+       return -EIO;
+}
+
+/**
+ * hl_pci_iatu_write() - iatu write routine.
+ * @hdev: Pointer to hl_device structure.
+ * @addr: Address to write to
+ * @data: Data to write
+ *
+ * Return: 0 on success, negative value for failure.
+ */
+int hl_pci_iatu_write(struct hl_device *hdev, u32 addr, u32 data)
+{
+       struct asic_fixed_properties *prop = &hdev->asic_prop;
+       u32 dbi_offset;
+       int rc;
+
+       dbi_offset = addr & 0xFFF;
+
+       /* Ignore result of writing to pcie_aux_dbi_reg_addr as it could fail
+        * in case the firmware security is enabled
+        */
+       hl_pci_elbi_write(hdev, prop->pcie_aux_dbi_reg_addr, 0x00300000);
+
+       rc = hl_pci_elbi_write(hdev, prop->pcie_dbi_base_address + dbi_offset,
+                               data);
+
+       if (rc)
+               return -EIO;
+
+       return 0;
+}
+
+/**
+ * hl_pci_reset_link_through_bridge() - Reset PCI link.
+ * @hdev: Pointer to hl_device structure.
+ */
+static void hl_pci_reset_link_through_bridge(struct hl_device *hdev)
+{
+       struct pci_dev *pdev = hdev->pdev;
+       struct pci_dev *parent_port;
+       u16 val;
+
+       parent_port = pdev->bus->self;
+       pci_read_config_word(parent_port, PCI_BRIDGE_CONTROL, &val);
+       val |= PCI_BRIDGE_CTL_BUS_RESET;
+       pci_write_config_word(parent_port, PCI_BRIDGE_CONTROL, val);
+       ssleep(1);
+
+       val &= ~(PCI_BRIDGE_CTL_BUS_RESET);
+       pci_write_config_word(parent_port, PCI_BRIDGE_CONTROL, val);
+       ssleep(3);
+}
+
+/**
+ * hl_pci_set_inbound_region() - Configure inbound region
+ * @hdev: Pointer to hl_device structure.
+ * @region: Inbound region number.
+ * @pci_region: Inbound region parameters.
+ *
+ * Configure the iATU inbound region.
+ *
+ * Return: 0 on success, negative value for failure.
+ */
+int hl_pci_set_inbound_region(struct hl_device *hdev, u8 region,
+               struct hl_inbound_pci_region *pci_region)
+{
+       struct asic_fixed_properties *prop = &hdev->asic_prop;
+       u64 bar_phys_base, region_base, region_end_address;
+       u32 offset, ctrl_reg_val;
+       int rc = 0;
+
+       /* region offset */
+       offset = (0x200 * region) + 0x100;
+
+       if (pci_region->mode == PCI_ADDRESS_MATCH_MODE) {
+               bar_phys_base = hdev->pcie_bar_phys[pci_region->bar];
+               region_base = bar_phys_base + pci_region->offset_in_bar;
+               region_end_address = region_base + pci_region->size - 1;
+
+               rc |= hl_pci_iatu_write(hdev, offset + 0x8,
+                               lower_32_bits(region_base));
+               rc |= hl_pci_iatu_write(hdev, offset + 0xC,
+                               upper_32_bits(region_base));
+               rc |= hl_pci_iatu_write(hdev, offset + 0x10,
+                               lower_32_bits(region_end_address));
+       }
+
+       /* Point to the specified address */
+       rc |= hl_pci_iatu_write(hdev, offset + 0x14,
+                       lower_32_bits(pci_region->addr));
+       rc |= hl_pci_iatu_write(hdev, offset + 0x18,
+                       upper_32_bits(pci_region->addr));
+       rc |= hl_pci_iatu_write(hdev, offset + 0x0, 0);
+
+       /* Enable + bar/address match + match enable + bar number */
+       ctrl_reg_val = FIELD_PREP(IATU_REGION_CTRL_REGION_EN_MASK, 1);
+       ctrl_reg_val |= FIELD_PREP(IATU_REGION_CTRL_MATCH_MODE_MASK,
+                       pci_region->mode);
+       ctrl_reg_val |= FIELD_PREP(IATU_REGION_CTRL_NUM_MATCH_EN_MASK, 1);
+
+       if (pci_region->mode == PCI_BAR_MATCH_MODE)
+               ctrl_reg_val |= FIELD_PREP(IATU_REGION_CTRL_BAR_NUM_MASK,
+                               pci_region->bar);
+
+       rc |= hl_pci_iatu_write(hdev, offset + 0x4, ctrl_reg_val);
+
+       /* Return the DBI window to the default location
+        * Ignore result of writing to pcie_aux_dbi_reg_addr as it could fail
+        * in case the firmware security is enabled
+        */
+       hl_pci_elbi_write(hdev, prop->pcie_aux_dbi_reg_addr, 0);
+
+       if (rc)
+               dev_err(hdev->dev, "failed to map bar %u to 0x%08llx\n",
+                               pci_region->bar, pci_region->addr);
+
+       return rc;
+}
+
+/**
+ * hl_pci_set_outbound_region() - Configure outbound region 0
+ * @hdev: Pointer to hl_device structure.
+ * @pci_region: Outbound region parameters.
+ *
+ * Configure the iATU outbound region 0.
+ *
+ * Return: 0 on success, negative value for failure.
+ */
+int hl_pci_set_outbound_region(struct hl_device *hdev,
+               struct hl_outbound_pci_region *pci_region)
+{
+       struct asic_fixed_properties *prop = &hdev->asic_prop;
+       u64 outbound_region_end_address;
+       int rc = 0;
+
+       /* Outbound Region 0 */
+       outbound_region_end_address =
+                       pci_region->addr + pci_region->size - 1;
+       rc |= hl_pci_iatu_write(hdev, 0x008,
+                               lower_32_bits(pci_region->addr));
+       rc |= hl_pci_iatu_write(hdev, 0x00C,
+                               upper_32_bits(pci_region->addr));
+       rc |= hl_pci_iatu_write(hdev, 0x010,
+                               lower_32_bits(outbound_region_end_address));
+       rc |= hl_pci_iatu_write(hdev, 0x014, 0);
+
+       if ((hdev->power9_64bit_dma_enable) && (hdev->dma_mask == 64))
+               rc |= hl_pci_iatu_write(hdev, 0x018, 0x08000000);
+       else
+               rc |= hl_pci_iatu_write(hdev, 0x018, 0);
+
+       rc |= hl_pci_iatu_write(hdev, 0x020,
+                               upper_32_bits(outbound_region_end_address));
+       /* Increase region size */
+       rc |= hl_pci_iatu_write(hdev, 0x000, 0x00002000);
+       /* Enable */
+       rc |= hl_pci_iatu_write(hdev, 0x004, 0x80000000);
+
+       /* Return the DBI window to the default location
+        * Ignore result of writing to pcie_aux_dbi_reg_addr as it could fail
+        * in case the firmware security is enabled
+        */
+       hl_pci_elbi_write(hdev, prop->pcie_aux_dbi_reg_addr, 0);
+
+       return rc;
+}
+
+/**
+ * hl_pci_set_dma_mask() - Set DMA masks for the device.
+ * @hdev: Pointer to hl_device structure.
+ *
+ * This function sets the DMA masks (regular and consistent) for a specified
+ * value. If it doesn't succeed, it tries to set it to a fall-back value
+ *
+ * Return: 0 on success, non-zero for failure.
+ */
+static int hl_pci_set_dma_mask(struct hl_device *hdev)
+{
+       struct pci_dev *pdev = hdev->pdev;
+       int rc;
+
+       /* set DMA mask */
+       rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(hdev->dma_mask));
+       if (rc) {
+               dev_err(hdev->dev,
+                       "Failed to set pci dma mask to %d bits, error %d\n",
+                       hdev->dma_mask, rc);
+               return rc;
+       }
+
+       rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(hdev->dma_mask));
+       if (rc) {
+               dev_err(hdev->dev,
+                       "Failed to set pci consistent dma mask to %d bits, error %d\n",
+                       hdev->dma_mask, rc);
+               return rc;
+       }
+
+       return 0;
+}
+
+/**
+ * hl_pci_init() - PCI initialization code.
+ * @hdev: Pointer to hl_device structure.
+ *
+ * Set DMA masks, initialize the PCI controller and map the PCI BARs.
+ *
+ * Return: 0 on success, non-zero for failure.
+ */
+int hl_pci_init(struct hl_device *hdev)
+{
+       struct pci_dev *pdev = hdev->pdev;
+       int rc;
+
+       if (hdev->reset_pcilink)
+               hl_pci_reset_link_through_bridge(hdev);
+
+       rc = pci_enable_device_mem(pdev);
+       if (rc) {
+               dev_err(hdev->dev, "can't enable PCI device\n");
+               return rc;
+       }
+
+       pci_set_master(pdev);
+
+       rc = hdev->asic_funcs->pci_bars_map(hdev);
+       if (rc) {
+               dev_err(hdev->dev, "Failed to initialize PCI BARs\n");
+               goto disable_device;
+       }
+
+       rc = hdev->asic_funcs->init_iatu(hdev);
+       if (rc) {
+               dev_err(hdev->dev, "Failed to initialize iATU\n");
+               goto unmap_pci_bars;
+       }
+
+       rc = hl_pci_set_dma_mask(hdev);
+       if (rc)
+               goto unmap_pci_bars;
+
+       return 0;
+
+unmap_pci_bars:
+       hl_pci_bars_unmap(hdev);
+disable_device:
+       pci_clear_master(pdev);
+       pci_disable_device(pdev);
+
+       return rc;
+}
+
+/**
+ * hl_fw_fini() - PCI finalization code.
+ * @hdev: Pointer to hl_device structure
+ *
+ * Unmap PCI bars and disable PCI device.
+ */
+void hl_pci_fini(struct hl_device *hdev)
+{
+       hl_pci_bars_unmap(hdev);
+
+       pci_clear_master(hdev->pdev);
+       pci_disable_device(hdev->pdev);
+}