1263b2ba5SJacek Lawrynowicz // SPDX-License-Identifier: GPL-2.0-only
2263b2ba5SJacek Lawrynowicz /*
3263b2ba5SJacek Lawrynowicz * Copyright (C) 2020-2023 Intel Corporation
4263b2ba5SJacek Lawrynowicz */
5263b2ba5SJacek Lawrynowicz
6263b2ba5SJacek Lawrynowicz #include <linux/bitfield.h>
7263b2ba5SJacek Lawrynowicz #include <linux/highmem.h>
8263b2ba5SJacek Lawrynowicz
9263b2ba5SJacek Lawrynowicz #include "ivpu_drv.h"
10263b2ba5SJacek Lawrynowicz #include "ivpu_hw.h"
11263b2ba5SJacek Lawrynowicz #include "ivpu_mmu.h"
12263b2ba5SJacek Lawrynowicz #include "ivpu_mmu_context.h"
13263b2ba5SJacek Lawrynowicz
14*8f5ad367SWludzik, Jozef #define IVPU_MMU_VPU_ADDRESS_MASK GENMASK(47, 12)
15a2fd4a6fSKarol Wachowski #define IVPU_MMU_PGD_INDEX_MASK GENMASK(47, 39)
16a2fd4a6fSKarol Wachowski #define IVPU_MMU_PUD_INDEX_MASK GENMASK(38, 30)
17263b2ba5SJacek Lawrynowicz #define IVPU_MMU_PMD_INDEX_MASK GENMASK(29, 21)
18263b2ba5SJacek Lawrynowicz #define IVPU_MMU_PTE_INDEX_MASK GENMASK(20, 12)
1995d44018SKarol Wachowski #define IVPU_MMU_ENTRY_FLAGS_MASK (BIT(52) | GENMASK(11, 0))
2095d44018SKarol Wachowski #define IVPU_MMU_ENTRY_FLAG_CONT BIT(52)
21263b2ba5SJacek Lawrynowicz #define IVPU_MMU_ENTRY_FLAG_NG BIT(11)
22263b2ba5SJacek Lawrynowicz #define IVPU_MMU_ENTRY_FLAG_AF BIT(10)
23263b2ba5SJacek Lawrynowicz #define IVPU_MMU_ENTRY_FLAG_USER BIT(6)
24263b2ba5SJacek Lawrynowicz #define IVPU_MMU_ENTRY_FLAG_LLC_COHERENT BIT(2)
25263b2ba5SJacek Lawrynowicz #define IVPU_MMU_ENTRY_FLAG_TYPE_PAGE BIT(1)
26263b2ba5SJacek Lawrynowicz #define IVPU_MMU_ENTRY_FLAG_VALID BIT(0)
27263b2ba5SJacek Lawrynowicz
28263b2ba5SJacek Lawrynowicz #define IVPU_MMU_PAGE_SIZE SZ_4K
2995d44018SKarol Wachowski #define IVPU_MMU_CONT_PAGES_SIZE (IVPU_MMU_PAGE_SIZE * 16)
30263b2ba5SJacek Lawrynowicz #define IVPU_MMU_PTE_MAP_SIZE (IVPU_MMU_PGTABLE_ENTRIES * IVPU_MMU_PAGE_SIZE)
31263b2ba5SJacek Lawrynowicz #define IVPU_MMU_PMD_MAP_SIZE (IVPU_MMU_PGTABLE_ENTRIES * IVPU_MMU_PTE_MAP_SIZE)
32a2fd4a6fSKarol Wachowski #define IVPU_MMU_PUD_MAP_SIZE (IVPU_MMU_PGTABLE_ENTRIES * IVPU_MMU_PMD_MAP_SIZE)
33a2fd4a6fSKarol Wachowski #define IVPU_MMU_PGD_MAP_SIZE (IVPU_MMU_PGTABLE_ENTRIES * IVPU_MMU_PUD_MAP_SIZE)
34263b2ba5SJacek Lawrynowicz #define IVPU_MMU_PGTABLE_SIZE (IVPU_MMU_PGTABLE_ENTRIES * sizeof(u64))
35263b2ba5SJacek Lawrynowicz
36263b2ba5SJacek Lawrynowicz #define IVPU_MMU_DUMMY_ADDRESS 0xdeadb000
37263b2ba5SJacek Lawrynowicz #define IVPU_MMU_ENTRY_VALID (IVPU_MMU_ENTRY_FLAG_TYPE_PAGE | IVPU_MMU_ENTRY_FLAG_VALID)
38263b2ba5SJacek Lawrynowicz #define IVPU_MMU_ENTRY_INVALID (IVPU_MMU_DUMMY_ADDRESS & ~IVPU_MMU_ENTRY_FLAGS_MASK)
39263b2ba5SJacek Lawrynowicz #define IVPU_MMU_ENTRY_MAPPED (IVPU_MMU_ENTRY_FLAG_AF | IVPU_MMU_ENTRY_FLAG_USER | \
40263b2ba5SJacek Lawrynowicz IVPU_MMU_ENTRY_FLAG_NG | IVPU_MMU_ENTRY_VALID)
41263b2ba5SJacek Lawrynowicz
ivpu_mmu_pgtable_init(struct ivpu_device * vdev,struct ivpu_mmu_pgtable * pgtable)42263b2ba5SJacek Lawrynowicz static int ivpu_mmu_pgtable_init(struct ivpu_device *vdev, struct ivpu_mmu_pgtable *pgtable)
43263b2ba5SJacek Lawrynowicz {
44263b2ba5SJacek Lawrynowicz dma_addr_t pgd_dma;
45263b2ba5SJacek Lawrynowicz
46103d2ea1SKarol Wachowski pgtable->pgd_dma_ptr = dma_alloc_coherent(vdev->drm.dev, IVPU_MMU_PGTABLE_SIZE, &pgd_dma,
47103d2ea1SKarol Wachowski GFP_KERNEL);
48103d2ea1SKarol Wachowski if (!pgtable->pgd_dma_ptr)
49263b2ba5SJacek Lawrynowicz return -ENOMEM;
50263b2ba5SJacek Lawrynowicz
51263b2ba5SJacek Lawrynowicz pgtable->pgd_dma = pgd_dma;
52263b2ba5SJacek Lawrynowicz
53263b2ba5SJacek Lawrynowicz return 0;
54263b2ba5SJacek Lawrynowicz }
55263b2ba5SJacek Lawrynowicz
ivpu_mmu_pgtable_free(struct ivpu_device * vdev,u64 * cpu_addr,dma_addr_t dma_addr)56103d2ea1SKarol Wachowski static void ivpu_mmu_pgtable_free(struct ivpu_device *vdev, u64 *cpu_addr, dma_addr_t dma_addr)
57103d2ea1SKarol Wachowski {
58103d2ea1SKarol Wachowski if (cpu_addr)
59103d2ea1SKarol Wachowski dma_free_coherent(vdev->drm.dev, IVPU_MMU_PGTABLE_SIZE, cpu_addr,
60103d2ea1SKarol Wachowski dma_addr & ~IVPU_MMU_ENTRY_FLAGS_MASK);
61103d2ea1SKarol Wachowski }
62103d2ea1SKarol Wachowski
ivpu_mmu_pgtables_free(struct ivpu_device * vdev,struct ivpu_mmu_pgtable * pgtable)63103d2ea1SKarol Wachowski static void ivpu_mmu_pgtables_free(struct ivpu_device *vdev, struct ivpu_mmu_pgtable *pgtable)
64263b2ba5SJacek Lawrynowicz {
65a2fd4a6fSKarol Wachowski int pgd_idx, pud_idx, pmd_idx;
66103d2ea1SKarol Wachowski dma_addr_t pud_dma, pmd_dma, pte_dma;
67103d2ea1SKarol Wachowski u64 *pud_dma_ptr, *pmd_dma_ptr, *pte_dma_ptr;
68263b2ba5SJacek Lawrynowicz
69a2fd4a6fSKarol Wachowski for (pgd_idx = 0; pgd_idx < IVPU_MMU_PGTABLE_ENTRIES; ++pgd_idx) {
70103d2ea1SKarol Wachowski pud_dma_ptr = pgtable->pud_ptrs[pgd_idx];
71103d2ea1SKarol Wachowski pud_dma = pgtable->pgd_dma_ptr[pgd_idx];
72a2fd4a6fSKarol Wachowski
73103d2ea1SKarol Wachowski if (!pud_dma_ptr)
74a2fd4a6fSKarol Wachowski continue;
75a2fd4a6fSKarol Wachowski
76a2fd4a6fSKarol Wachowski for (pud_idx = 0; pud_idx < IVPU_MMU_PGTABLE_ENTRIES; ++pud_idx) {
77103d2ea1SKarol Wachowski pmd_dma_ptr = pgtable->pmd_ptrs[pgd_idx][pud_idx];
78103d2ea1SKarol Wachowski pmd_dma = pgtable->pud_ptrs[pgd_idx][pud_idx];
79263b2ba5SJacek Lawrynowicz
80103d2ea1SKarol Wachowski if (!pmd_dma_ptr)
81263b2ba5SJacek Lawrynowicz continue;
82263b2ba5SJacek Lawrynowicz
83a2fd4a6fSKarol Wachowski for (pmd_idx = 0; pmd_idx < IVPU_MMU_PGTABLE_ENTRIES; ++pmd_idx) {
84103d2ea1SKarol Wachowski pte_dma_ptr = pgtable->pte_ptrs[pgd_idx][pud_idx][pmd_idx];
85103d2ea1SKarol Wachowski pte_dma = pgtable->pmd_ptrs[pgd_idx][pud_idx][pmd_idx];
86103d2ea1SKarol Wachowski
87103d2ea1SKarol Wachowski ivpu_mmu_pgtable_free(vdev, pte_dma_ptr, pte_dma);
88263b2ba5SJacek Lawrynowicz }
89263b2ba5SJacek Lawrynowicz
90103d2ea1SKarol Wachowski kfree(pgtable->pte_ptrs[pgd_idx][pud_idx]);
91103d2ea1SKarol Wachowski ivpu_mmu_pgtable_free(vdev, pmd_dma_ptr, pmd_dma);
92a2fd4a6fSKarol Wachowski }
93a2fd4a6fSKarol Wachowski
94103d2ea1SKarol Wachowski kfree(pgtable->pmd_ptrs[pgd_idx]);
95103d2ea1SKarol Wachowski kfree(pgtable->pte_ptrs[pgd_idx]);
96103d2ea1SKarol Wachowski ivpu_mmu_pgtable_free(vdev, pud_dma_ptr, pud_dma);
97263b2ba5SJacek Lawrynowicz }
98263b2ba5SJacek Lawrynowicz
99103d2ea1SKarol Wachowski ivpu_mmu_pgtable_free(vdev, pgtable->pgd_dma_ptr, pgtable->pgd_dma);
100263b2ba5SJacek Lawrynowicz }
101263b2ba5SJacek Lawrynowicz
102263b2ba5SJacek Lawrynowicz static u64*
ivpu_mmu_ensure_pud(struct ivpu_device * vdev,struct ivpu_mmu_pgtable * pgtable,int pgd_idx)103a2fd4a6fSKarol Wachowski ivpu_mmu_ensure_pud(struct ivpu_device *vdev, struct ivpu_mmu_pgtable *pgtable, int pgd_idx)
104a2fd4a6fSKarol Wachowski {
105103d2ea1SKarol Wachowski u64 *pud_dma_ptr = pgtable->pud_ptrs[pgd_idx];
106a2fd4a6fSKarol Wachowski dma_addr_t pud_dma;
107a2fd4a6fSKarol Wachowski
108103d2ea1SKarol Wachowski if (pud_dma_ptr)
109103d2ea1SKarol Wachowski return pud_dma_ptr;
110a2fd4a6fSKarol Wachowski
111103d2ea1SKarol Wachowski pud_dma_ptr = dma_alloc_wc(vdev->drm.dev, IVPU_MMU_PGTABLE_SIZE, &pud_dma, GFP_KERNEL);
112103d2ea1SKarol Wachowski if (!pud_dma_ptr)
113a2fd4a6fSKarol Wachowski return NULL;
114a2fd4a6fSKarol Wachowski
115103d2ea1SKarol Wachowski drm_WARN_ON(&vdev->drm, pgtable->pmd_ptrs[pgd_idx]);
116103d2ea1SKarol Wachowski pgtable->pmd_ptrs[pgd_idx] = kzalloc(IVPU_MMU_PGTABLE_SIZE, GFP_KERNEL);
117103d2ea1SKarol Wachowski if (!pgtable->pmd_ptrs[pgd_idx])
118103d2ea1SKarol Wachowski goto err_free_pud_dma_ptr;
119a2fd4a6fSKarol Wachowski
120103d2ea1SKarol Wachowski drm_WARN_ON(&vdev->drm, pgtable->pte_ptrs[pgd_idx]);
121103d2ea1SKarol Wachowski pgtable->pte_ptrs[pgd_idx] = kzalloc(IVPU_MMU_PGTABLE_SIZE, GFP_KERNEL);
122103d2ea1SKarol Wachowski if (!pgtable->pte_ptrs[pgd_idx])
123103d2ea1SKarol Wachowski goto err_free_pmd_ptrs;
124a2fd4a6fSKarol Wachowski
125103d2ea1SKarol Wachowski pgtable->pud_ptrs[pgd_idx] = pud_dma_ptr;
126103d2ea1SKarol Wachowski pgtable->pgd_dma_ptr[pgd_idx] = pud_dma | IVPU_MMU_ENTRY_VALID;
127a2fd4a6fSKarol Wachowski
128103d2ea1SKarol Wachowski return pud_dma_ptr;
129a2fd4a6fSKarol Wachowski
130103d2ea1SKarol Wachowski err_free_pmd_ptrs:
131103d2ea1SKarol Wachowski kfree(pgtable->pmd_ptrs[pgd_idx]);
132a2fd4a6fSKarol Wachowski
133103d2ea1SKarol Wachowski err_free_pud_dma_ptr:
134103d2ea1SKarol Wachowski ivpu_mmu_pgtable_free(vdev, pud_dma_ptr, pud_dma);
135a2fd4a6fSKarol Wachowski return NULL;
136a2fd4a6fSKarol Wachowski }
137a2fd4a6fSKarol Wachowski
138a2fd4a6fSKarol Wachowski static u64*
ivpu_mmu_ensure_pmd(struct ivpu_device * vdev,struct ivpu_mmu_pgtable * pgtable,int pgd_idx,int pud_idx)139103d2ea1SKarol Wachowski ivpu_mmu_ensure_pmd(struct ivpu_device *vdev, struct ivpu_mmu_pgtable *pgtable, int pgd_idx,
140103d2ea1SKarol Wachowski int pud_idx)
141263b2ba5SJacek Lawrynowicz {
142103d2ea1SKarol Wachowski u64 *pmd_dma_ptr = pgtable->pmd_ptrs[pgd_idx][pud_idx];
143263b2ba5SJacek Lawrynowicz dma_addr_t pmd_dma;
144263b2ba5SJacek Lawrynowicz
145103d2ea1SKarol Wachowski if (pmd_dma_ptr)
146103d2ea1SKarol Wachowski return pmd_dma_ptr;
147263b2ba5SJacek Lawrynowicz
148103d2ea1SKarol Wachowski pmd_dma_ptr = dma_alloc_wc(vdev->drm.dev, IVPU_MMU_PGTABLE_SIZE, &pmd_dma, GFP_KERNEL);
149103d2ea1SKarol Wachowski if (!pmd_dma_ptr)
150263b2ba5SJacek Lawrynowicz return NULL;
151263b2ba5SJacek Lawrynowicz
152103d2ea1SKarol Wachowski drm_WARN_ON(&vdev->drm, pgtable->pte_ptrs[pgd_idx][pud_idx]);
153103d2ea1SKarol Wachowski pgtable->pte_ptrs[pgd_idx][pud_idx] = kzalloc(IVPU_MMU_PGTABLE_SIZE, GFP_KERNEL);
154103d2ea1SKarol Wachowski if (!pgtable->pte_ptrs[pgd_idx][pud_idx])
155103d2ea1SKarol Wachowski goto err_free_pmd_dma_ptr;
156263b2ba5SJacek Lawrynowicz
157103d2ea1SKarol Wachowski pgtable->pmd_ptrs[pgd_idx][pud_idx] = pmd_dma_ptr;
158103d2ea1SKarol Wachowski pgtable->pud_ptrs[pgd_idx][pud_idx] = pmd_dma | IVPU_MMU_ENTRY_VALID;
159263b2ba5SJacek Lawrynowicz
160103d2ea1SKarol Wachowski return pmd_dma_ptr;
161263b2ba5SJacek Lawrynowicz
162103d2ea1SKarol Wachowski err_free_pmd_dma_ptr:
163103d2ea1SKarol Wachowski ivpu_mmu_pgtable_free(vdev, pmd_dma_ptr, pmd_dma);
164263b2ba5SJacek Lawrynowicz return NULL;
165263b2ba5SJacek Lawrynowicz }
166263b2ba5SJacek Lawrynowicz
167263b2ba5SJacek Lawrynowicz static u64*
ivpu_mmu_ensure_pte(struct ivpu_device * vdev,struct ivpu_mmu_pgtable * pgtable,int pgd_idx,int pud_idx,int pmd_idx)168263b2ba5SJacek Lawrynowicz ivpu_mmu_ensure_pte(struct ivpu_device *vdev, struct ivpu_mmu_pgtable *pgtable,
169a2fd4a6fSKarol Wachowski int pgd_idx, int pud_idx, int pmd_idx)
170263b2ba5SJacek Lawrynowicz {
171103d2ea1SKarol Wachowski u64 *pte_dma_ptr = pgtable->pte_ptrs[pgd_idx][pud_idx][pmd_idx];
172263b2ba5SJacek Lawrynowicz dma_addr_t pte_dma;
173263b2ba5SJacek Lawrynowicz
174103d2ea1SKarol Wachowski if (pte_dma_ptr)
175103d2ea1SKarol Wachowski return pte_dma_ptr;
176263b2ba5SJacek Lawrynowicz
177103d2ea1SKarol Wachowski pte_dma_ptr = dma_alloc_wc(vdev->drm.dev, IVPU_MMU_PGTABLE_SIZE, &pte_dma, GFP_KERNEL);
178103d2ea1SKarol Wachowski if (!pte_dma_ptr)
179263b2ba5SJacek Lawrynowicz return NULL;
180263b2ba5SJacek Lawrynowicz
181103d2ea1SKarol Wachowski pgtable->pte_ptrs[pgd_idx][pud_idx][pmd_idx] = pte_dma_ptr;
182103d2ea1SKarol Wachowski pgtable->pmd_ptrs[pgd_idx][pud_idx][pmd_idx] = pte_dma | IVPU_MMU_ENTRY_VALID;
183263b2ba5SJacek Lawrynowicz
184103d2ea1SKarol Wachowski return pte_dma_ptr;
185263b2ba5SJacek Lawrynowicz }
186263b2ba5SJacek Lawrynowicz
187263b2ba5SJacek Lawrynowicz static int
ivpu_mmu_context_map_page(struct ivpu_device * vdev,struct ivpu_mmu_context * ctx,u64 vpu_addr,dma_addr_t dma_addr,u64 prot)188263b2ba5SJacek Lawrynowicz ivpu_mmu_context_map_page(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,
18995d44018SKarol Wachowski u64 vpu_addr, dma_addr_t dma_addr, u64 prot)
190263b2ba5SJacek Lawrynowicz {
191263b2ba5SJacek Lawrynowicz u64 *pte;
192a2fd4a6fSKarol Wachowski int pgd_idx = FIELD_GET(IVPU_MMU_PGD_INDEX_MASK, vpu_addr);
193a2fd4a6fSKarol Wachowski int pud_idx = FIELD_GET(IVPU_MMU_PUD_INDEX_MASK, vpu_addr);
194a2fd4a6fSKarol Wachowski int pmd_idx = FIELD_GET(IVPU_MMU_PMD_INDEX_MASK, vpu_addr);
195a2fd4a6fSKarol Wachowski int pte_idx = FIELD_GET(IVPU_MMU_PTE_INDEX_MASK, vpu_addr);
196a2fd4a6fSKarol Wachowski
197103d2ea1SKarol Wachowski /* Allocate PUD - second level page table if needed */
198a2fd4a6fSKarol Wachowski if (!ivpu_mmu_ensure_pud(vdev, &ctx->pgtable, pgd_idx))
199a2fd4a6fSKarol Wachowski return -ENOMEM;
200263b2ba5SJacek Lawrynowicz
201103d2ea1SKarol Wachowski /* Allocate PMD - third level page table if needed */
202a2fd4a6fSKarol Wachowski if (!ivpu_mmu_ensure_pmd(vdev, &ctx->pgtable, pgd_idx, pud_idx))
203263b2ba5SJacek Lawrynowicz return -ENOMEM;
204263b2ba5SJacek Lawrynowicz
205103d2ea1SKarol Wachowski /* Allocate PTE - fourth level page table if needed */
206a2fd4a6fSKarol Wachowski pte = ivpu_mmu_ensure_pte(vdev, &ctx->pgtable, pgd_idx, pud_idx, pmd_idx);
207263b2ba5SJacek Lawrynowicz if (!pte)
208263b2ba5SJacek Lawrynowicz return -ENOMEM;
209263b2ba5SJacek Lawrynowicz
210103d2ea1SKarol Wachowski /* Update PTE */
211a2fd4a6fSKarol Wachowski pte[pte_idx] = dma_addr | prot;
212263b2ba5SJacek Lawrynowicz
213263b2ba5SJacek Lawrynowicz return 0;
214263b2ba5SJacek Lawrynowicz }
215263b2ba5SJacek Lawrynowicz
21695d44018SKarol Wachowski static int
ivpu_mmu_context_map_cont_64k(struct ivpu_device * vdev,struct ivpu_mmu_context * ctx,u64 vpu_addr,dma_addr_t dma_addr,u64 prot)21795d44018SKarol Wachowski ivpu_mmu_context_map_cont_64k(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx, u64 vpu_addr,
21895d44018SKarol Wachowski dma_addr_t dma_addr, u64 prot)
21995d44018SKarol Wachowski {
22095d44018SKarol Wachowski size_t size = IVPU_MMU_CONT_PAGES_SIZE;
22195d44018SKarol Wachowski
22295d44018SKarol Wachowski drm_WARN_ON(&vdev->drm, !IS_ALIGNED(vpu_addr, size));
22395d44018SKarol Wachowski drm_WARN_ON(&vdev->drm, !IS_ALIGNED(dma_addr, size));
22495d44018SKarol Wachowski
22595d44018SKarol Wachowski prot |= IVPU_MMU_ENTRY_FLAG_CONT;
22695d44018SKarol Wachowski
22795d44018SKarol Wachowski while (size) {
22895d44018SKarol Wachowski int ret = ivpu_mmu_context_map_page(vdev, ctx, vpu_addr, dma_addr, prot);
22995d44018SKarol Wachowski
23095d44018SKarol Wachowski if (ret)
23195d44018SKarol Wachowski return ret;
23295d44018SKarol Wachowski
23395d44018SKarol Wachowski size -= IVPU_MMU_PAGE_SIZE;
23495d44018SKarol Wachowski vpu_addr += IVPU_MMU_PAGE_SIZE;
23595d44018SKarol Wachowski dma_addr += IVPU_MMU_PAGE_SIZE;
23695d44018SKarol Wachowski }
23795d44018SKarol Wachowski
23895d44018SKarol Wachowski return 0;
23995d44018SKarol Wachowski }
24095d44018SKarol Wachowski
ivpu_mmu_context_unmap_page(struct ivpu_mmu_context * ctx,u64 vpu_addr)241263b2ba5SJacek Lawrynowicz static void ivpu_mmu_context_unmap_page(struct ivpu_mmu_context *ctx, u64 vpu_addr)
242263b2ba5SJacek Lawrynowicz {
243a2fd4a6fSKarol Wachowski int pgd_idx = FIELD_GET(IVPU_MMU_PGD_INDEX_MASK, vpu_addr);
244a2fd4a6fSKarol Wachowski int pud_idx = FIELD_GET(IVPU_MMU_PUD_INDEX_MASK, vpu_addr);
245a2fd4a6fSKarol Wachowski int pmd_idx = FIELD_GET(IVPU_MMU_PMD_INDEX_MASK, vpu_addr);
246a2fd4a6fSKarol Wachowski int pte_idx = FIELD_GET(IVPU_MMU_PTE_INDEX_MASK, vpu_addr);
247263b2ba5SJacek Lawrynowicz
248263b2ba5SJacek Lawrynowicz /* Update PTE with dummy physical address and clear flags */
249103d2ea1SKarol Wachowski ctx->pgtable.pte_ptrs[pgd_idx][pud_idx][pmd_idx][pte_idx] = IVPU_MMU_ENTRY_INVALID;
250263b2ba5SJacek Lawrynowicz }
251263b2ba5SJacek Lawrynowicz
252263b2ba5SJacek Lawrynowicz static void
ivpu_mmu_context_flush_page_tables(struct ivpu_mmu_context * ctx,u64 vpu_addr,size_t size)253263b2ba5SJacek Lawrynowicz ivpu_mmu_context_flush_page_tables(struct ivpu_mmu_context *ctx, u64 vpu_addr, size_t size)
254263b2ba5SJacek Lawrynowicz {
255103d2ea1SKarol Wachowski struct ivpu_mmu_pgtable *pgtable = &ctx->pgtable;
256263b2ba5SJacek Lawrynowicz u64 end_addr = vpu_addr + size;
257263b2ba5SJacek Lawrynowicz
258263b2ba5SJacek Lawrynowicz /* Align to PMD entry (2 MB) */
259263b2ba5SJacek Lawrynowicz vpu_addr &= ~(IVPU_MMU_PTE_MAP_SIZE - 1);
260103d2ea1SKarol Wachowski
261263b2ba5SJacek Lawrynowicz while (vpu_addr < end_addr) {
262a2fd4a6fSKarol Wachowski int pgd_idx = FIELD_GET(IVPU_MMU_PGD_INDEX_MASK, vpu_addr);
263a2fd4a6fSKarol Wachowski u64 pud_end = (pgd_idx + 1) * (u64)IVPU_MMU_PUD_MAP_SIZE;
264a2fd4a6fSKarol Wachowski
265a2fd4a6fSKarol Wachowski while (vpu_addr < end_addr && vpu_addr < pud_end) {
266a2fd4a6fSKarol Wachowski int pud_idx = FIELD_GET(IVPU_MMU_PUD_INDEX_MASK, vpu_addr);
267a2fd4a6fSKarol Wachowski u64 pmd_end = (pud_idx + 1) * (u64)IVPU_MMU_PMD_MAP_SIZE;
268263b2ba5SJacek Lawrynowicz
269263b2ba5SJacek Lawrynowicz while (vpu_addr < end_addr && vpu_addr < pmd_end) {
270a2fd4a6fSKarol Wachowski int pmd_idx = FIELD_GET(IVPU_MMU_PMD_INDEX_MASK, vpu_addr);
271263b2ba5SJacek Lawrynowicz
272103d2ea1SKarol Wachowski clflush_cache_range(pgtable->pte_ptrs[pgd_idx][pud_idx][pmd_idx],
273103d2ea1SKarol Wachowski IVPU_MMU_PGTABLE_SIZE);
274263b2ba5SJacek Lawrynowicz vpu_addr += IVPU_MMU_PTE_MAP_SIZE;
275263b2ba5SJacek Lawrynowicz }
276103d2ea1SKarol Wachowski clflush_cache_range(pgtable->pmd_ptrs[pgd_idx][pud_idx],
277103d2ea1SKarol Wachowski IVPU_MMU_PGTABLE_SIZE);
278263b2ba5SJacek Lawrynowicz }
279103d2ea1SKarol Wachowski clflush_cache_range(pgtable->pud_ptrs[pgd_idx], IVPU_MMU_PGTABLE_SIZE);
280a2fd4a6fSKarol Wachowski }
281103d2ea1SKarol Wachowski clflush_cache_range(pgtable->pgd_dma_ptr, IVPU_MMU_PGTABLE_SIZE);
282263b2ba5SJacek Lawrynowicz }
283263b2ba5SJacek Lawrynowicz
284263b2ba5SJacek Lawrynowicz static int
ivpu_mmu_context_map_pages(struct ivpu_device * vdev,struct ivpu_mmu_context * ctx,u64 vpu_addr,dma_addr_t dma_addr,size_t size,u64 prot)285263b2ba5SJacek Lawrynowicz ivpu_mmu_context_map_pages(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,
28695d44018SKarol Wachowski u64 vpu_addr, dma_addr_t dma_addr, size_t size, u64 prot)
287263b2ba5SJacek Lawrynowicz {
28895d44018SKarol Wachowski int map_size;
28995d44018SKarol Wachowski int ret;
29095d44018SKarol Wachowski
291263b2ba5SJacek Lawrynowicz while (size) {
29295d44018SKarol Wachowski if (!ivpu_disable_mmu_cont_pages && size >= IVPU_MMU_CONT_PAGES_SIZE &&
29395d44018SKarol Wachowski IS_ALIGNED(vpu_addr | dma_addr, IVPU_MMU_CONT_PAGES_SIZE)) {
29495d44018SKarol Wachowski ret = ivpu_mmu_context_map_cont_64k(vdev, ctx, vpu_addr, dma_addr, prot);
29595d44018SKarol Wachowski map_size = IVPU_MMU_CONT_PAGES_SIZE;
29695d44018SKarol Wachowski } else {
29795d44018SKarol Wachowski ret = ivpu_mmu_context_map_page(vdev, ctx, vpu_addr, dma_addr, prot);
29895d44018SKarol Wachowski map_size = IVPU_MMU_PAGE_SIZE;
29995d44018SKarol Wachowski }
300263b2ba5SJacek Lawrynowicz
301263b2ba5SJacek Lawrynowicz if (ret)
302263b2ba5SJacek Lawrynowicz return ret;
303263b2ba5SJacek Lawrynowicz
30495d44018SKarol Wachowski vpu_addr += map_size;
30595d44018SKarol Wachowski dma_addr += map_size;
30695d44018SKarol Wachowski size -= map_size;
307263b2ba5SJacek Lawrynowicz }
308263b2ba5SJacek Lawrynowicz
309263b2ba5SJacek Lawrynowicz return 0;
310263b2ba5SJacek Lawrynowicz }
311263b2ba5SJacek Lawrynowicz
ivpu_mmu_context_unmap_pages(struct ivpu_mmu_context * ctx,u64 vpu_addr,size_t size)312263b2ba5SJacek Lawrynowicz static void ivpu_mmu_context_unmap_pages(struct ivpu_mmu_context *ctx, u64 vpu_addr, size_t size)
313263b2ba5SJacek Lawrynowicz {
314263b2ba5SJacek Lawrynowicz while (size) {
315263b2ba5SJacek Lawrynowicz ivpu_mmu_context_unmap_page(ctx, vpu_addr);
316263b2ba5SJacek Lawrynowicz vpu_addr += IVPU_MMU_PAGE_SIZE;
317263b2ba5SJacek Lawrynowicz size -= IVPU_MMU_PAGE_SIZE;
318263b2ba5SJacek Lawrynowicz }
319263b2ba5SJacek Lawrynowicz }
320263b2ba5SJacek Lawrynowicz
321263b2ba5SJacek Lawrynowicz int
ivpu_mmu_context_map_sgt(struct ivpu_device * vdev,struct ivpu_mmu_context * ctx,u64 vpu_addr,struct sg_table * sgt,bool llc_coherent)322263b2ba5SJacek Lawrynowicz ivpu_mmu_context_map_sgt(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,
323263b2ba5SJacek Lawrynowicz u64 vpu_addr, struct sg_table *sgt, bool llc_coherent)
324263b2ba5SJacek Lawrynowicz {
325263b2ba5SJacek Lawrynowicz struct scatterlist *sg;
326263b2ba5SJacek Lawrynowicz int ret;
32795d44018SKarol Wachowski u64 prot;
328263b2ba5SJacek Lawrynowicz u64 i;
329263b2ba5SJacek Lawrynowicz
330263b2ba5SJacek Lawrynowicz if (!IS_ALIGNED(vpu_addr, IVPU_MMU_PAGE_SIZE))
331263b2ba5SJacek Lawrynowicz return -EINVAL;
332*8f5ad367SWludzik, Jozef
333*8f5ad367SWludzik, Jozef if (vpu_addr & ~IVPU_MMU_VPU_ADDRESS_MASK)
334263b2ba5SJacek Lawrynowicz return -EINVAL;
335263b2ba5SJacek Lawrynowicz
336263b2ba5SJacek Lawrynowicz prot = IVPU_MMU_ENTRY_MAPPED;
337263b2ba5SJacek Lawrynowicz if (llc_coherent)
338263b2ba5SJacek Lawrynowicz prot |= IVPU_MMU_ENTRY_FLAG_LLC_COHERENT;
339263b2ba5SJacek Lawrynowicz
340263b2ba5SJacek Lawrynowicz mutex_lock(&ctx->lock);
341263b2ba5SJacek Lawrynowicz
342263b2ba5SJacek Lawrynowicz for_each_sgtable_dma_sg(sgt, sg, i) {
343103d2ea1SKarol Wachowski dma_addr_t dma_addr = sg_dma_address(sg) - sg->offset;
344263b2ba5SJacek Lawrynowicz size_t size = sg_dma_len(sg) + sg->offset;
345263b2ba5SJacek Lawrynowicz
346263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_context_map_pages(vdev, ctx, vpu_addr, dma_addr, size, prot);
347263b2ba5SJacek Lawrynowicz if (ret) {
348263b2ba5SJacek Lawrynowicz ivpu_err(vdev, "Failed to map context pages\n");
349263b2ba5SJacek Lawrynowicz mutex_unlock(&ctx->lock);
350263b2ba5SJacek Lawrynowicz return ret;
351263b2ba5SJacek Lawrynowicz }
352263b2ba5SJacek Lawrynowicz ivpu_mmu_context_flush_page_tables(ctx, vpu_addr, size);
353263b2ba5SJacek Lawrynowicz vpu_addr += size;
354263b2ba5SJacek Lawrynowicz }
355263b2ba5SJacek Lawrynowicz
356263b2ba5SJacek Lawrynowicz mutex_unlock(&ctx->lock);
357263b2ba5SJacek Lawrynowicz
358263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_invalidate_tlb(vdev, ctx->id);
359263b2ba5SJacek Lawrynowicz if (ret)
360263b2ba5SJacek Lawrynowicz ivpu_err(vdev, "Failed to invalidate TLB for ctx %u: %d\n", ctx->id, ret);
361263b2ba5SJacek Lawrynowicz return ret;
362263b2ba5SJacek Lawrynowicz }
363263b2ba5SJacek Lawrynowicz
364263b2ba5SJacek Lawrynowicz void
ivpu_mmu_context_unmap_sgt(struct ivpu_device * vdev,struct ivpu_mmu_context * ctx,u64 vpu_addr,struct sg_table * sgt)365263b2ba5SJacek Lawrynowicz ivpu_mmu_context_unmap_sgt(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx,
366263b2ba5SJacek Lawrynowicz u64 vpu_addr, struct sg_table *sgt)
367263b2ba5SJacek Lawrynowicz {
368263b2ba5SJacek Lawrynowicz struct scatterlist *sg;
369263b2ba5SJacek Lawrynowicz int ret;
370263b2ba5SJacek Lawrynowicz u64 i;
371263b2ba5SJacek Lawrynowicz
372263b2ba5SJacek Lawrynowicz if (!IS_ALIGNED(vpu_addr, IVPU_MMU_PAGE_SIZE))
373263b2ba5SJacek Lawrynowicz ivpu_warn(vdev, "Unaligned vpu_addr: 0x%llx\n", vpu_addr);
374263b2ba5SJacek Lawrynowicz
375263b2ba5SJacek Lawrynowicz mutex_lock(&ctx->lock);
376263b2ba5SJacek Lawrynowicz
377263b2ba5SJacek Lawrynowicz for_each_sgtable_dma_sg(sgt, sg, i) {
378263b2ba5SJacek Lawrynowicz size_t size = sg_dma_len(sg) + sg->offset;
379263b2ba5SJacek Lawrynowicz
380263b2ba5SJacek Lawrynowicz ivpu_mmu_context_unmap_pages(ctx, vpu_addr, size);
381263b2ba5SJacek Lawrynowicz ivpu_mmu_context_flush_page_tables(ctx, vpu_addr, size);
382263b2ba5SJacek Lawrynowicz vpu_addr += size;
383263b2ba5SJacek Lawrynowicz }
384263b2ba5SJacek Lawrynowicz
385263b2ba5SJacek Lawrynowicz mutex_unlock(&ctx->lock);
386263b2ba5SJacek Lawrynowicz
387263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_invalidate_tlb(vdev, ctx->id);
388263b2ba5SJacek Lawrynowicz if (ret)
389263b2ba5SJacek Lawrynowicz ivpu_warn(vdev, "Failed to invalidate TLB for ctx %u: %d\n", ctx->id, ret);
390263b2ba5SJacek Lawrynowicz }
391263b2ba5SJacek Lawrynowicz
392263b2ba5SJacek Lawrynowicz int
ivpu_mmu_context_insert_node_locked(struct ivpu_mmu_context * ctx,const struct ivpu_addr_range * range,u64 size,struct drm_mm_node * node)393263b2ba5SJacek Lawrynowicz ivpu_mmu_context_insert_node_locked(struct ivpu_mmu_context *ctx,
394263b2ba5SJacek Lawrynowicz const struct ivpu_addr_range *range,
395263b2ba5SJacek Lawrynowicz u64 size, struct drm_mm_node *node)
396263b2ba5SJacek Lawrynowicz {
397263b2ba5SJacek Lawrynowicz lockdep_assert_held(&ctx->lock);
398263b2ba5SJacek Lawrynowicz
39995d44018SKarol Wachowski if (!ivpu_disable_mmu_cont_pages && size >= IVPU_MMU_CONT_PAGES_SIZE) {
40095d44018SKarol Wachowski if (!drm_mm_insert_node_in_range(&ctx->mm, node, size, IVPU_MMU_CONT_PAGES_SIZE, 0,
40195d44018SKarol Wachowski range->start, range->end, DRM_MM_INSERT_BEST))
40295d44018SKarol Wachowski return 0;
40395d44018SKarol Wachowski }
40495d44018SKarol Wachowski
40595d44018SKarol Wachowski return drm_mm_insert_node_in_range(&ctx->mm, node, size, IVPU_MMU_PAGE_SIZE, 0,
40695d44018SKarol Wachowski range->start, range->end, DRM_MM_INSERT_BEST);
407263b2ba5SJacek Lawrynowicz }
408263b2ba5SJacek Lawrynowicz
409263b2ba5SJacek Lawrynowicz void
ivpu_mmu_context_remove_node_locked(struct ivpu_mmu_context * ctx,struct drm_mm_node * node)410263b2ba5SJacek Lawrynowicz ivpu_mmu_context_remove_node_locked(struct ivpu_mmu_context *ctx, struct drm_mm_node *node)
411263b2ba5SJacek Lawrynowicz {
412263b2ba5SJacek Lawrynowicz lockdep_assert_held(&ctx->lock);
413263b2ba5SJacek Lawrynowicz
414263b2ba5SJacek Lawrynowicz drm_mm_remove_node(node);
415263b2ba5SJacek Lawrynowicz }
416263b2ba5SJacek Lawrynowicz
417263b2ba5SJacek Lawrynowicz static int
ivpu_mmu_context_init(struct ivpu_device * vdev,struct ivpu_mmu_context * ctx,u32 context_id)418263b2ba5SJacek Lawrynowicz ivpu_mmu_context_init(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx, u32 context_id)
419263b2ba5SJacek Lawrynowicz {
420263b2ba5SJacek Lawrynowicz u64 start, end;
421263b2ba5SJacek Lawrynowicz int ret;
422263b2ba5SJacek Lawrynowicz
423263b2ba5SJacek Lawrynowicz mutex_init(&ctx->lock);
424263b2ba5SJacek Lawrynowicz INIT_LIST_HEAD(&ctx->bo_list);
425263b2ba5SJacek Lawrynowicz
426263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_pgtable_init(vdev, &ctx->pgtable);
427263b2ba5SJacek Lawrynowicz if (ret)
428263b2ba5SJacek Lawrynowicz return ret;
429263b2ba5SJacek Lawrynowicz
430263b2ba5SJacek Lawrynowicz if (!context_id) {
431162f17b2SKarol Wachowski start = vdev->hw->ranges.global.start;
432162f17b2SKarol Wachowski end = vdev->hw->ranges.shave.end;
433263b2ba5SJacek Lawrynowicz } else {
434162f17b2SKarol Wachowski start = vdev->hw->ranges.user.start;
435162f17b2SKarol Wachowski end = vdev->hw->ranges.dma.end;
436263b2ba5SJacek Lawrynowicz }
437263b2ba5SJacek Lawrynowicz
438263b2ba5SJacek Lawrynowicz drm_mm_init(&ctx->mm, start, end - start);
439263b2ba5SJacek Lawrynowicz ctx->id = context_id;
440263b2ba5SJacek Lawrynowicz
441263b2ba5SJacek Lawrynowicz return 0;
442263b2ba5SJacek Lawrynowicz }
443263b2ba5SJacek Lawrynowicz
ivpu_mmu_context_fini(struct ivpu_device * vdev,struct ivpu_mmu_context * ctx)444263b2ba5SJacek Lawrynowicz static void ivpu_mmu_context_fini(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx)
445263b2ba5SJacek Lawrynowicz {
446103d2ea1SKarol Wachowski if (drm_WARN_ON(&vdev->drm, !ctx->pgtable.pgd_dma_ptr))
447103d2ea1SKarol Wachowski return;
448263b2ba5SJacek Lawrynowicz
449263b2ba5SJacek Lawrynowicz mutex_destroy(&ctx->lock);
450103d2ea1SKarol Wachowski ivpu_mmu_pgtables_free(vdev, &ctx->pgtable);
451263b2ba5SJacek Lawrynowicz drm_mm_takedown(&ctx->mm);
452103d2ea1SKarol Wachowski
453103d2ea1SKarol Wachowski ctx->pgtable.pgd_dma_ptr = NULL;
454103d2ea1SKarol Wachowski ctx->pgtable.pgd_dma = 0;
455263b2ba5SJacek Lawrynowicz }
456263b2ba5SJacek Lawrynowicz
ivpu_mmu_global_context_init(struct ivpu_device * vdev)457263b2ba5SJacek Lawrynowicz int ivpu_mmu_global_context_init(struct ivpu_device *vdev)
458263b2ba5SJacek Lawrynowicz {
459263b2ba5SJacek Lawrynowicz return ivpu_mmu_context_init(vdev, &vdev->gctx, IVPU_GLOBAL_CONTEXT_MMU_SSID);
460263b2ba5SJacek Lawrynowicz }
461263b2ba5SJacek Lawrynowicz
ivpu_mmu_global_context_fini(struct ivpu_device * vdev)462263b2ba5SJacek Lawrynowicz void ivpu_mmu_global_context_fini(struct ivpu_device *vdev)
463263b2ba5SJacek Lawrynowicz {
464263b2ba5SJacek Lawrynowicz return ivpu_mmu_context_fini(vdev, &vdev->gctx);
465263b2ba5SJacek Lawrynowicz }
466263b2ba5SJacek Lawrynowicz
ivpu_mmu_user_context_mark_invalid(struct ivpu_device * vdev,u32 ssid)467263b2ba5SJacek Lawrynowicz void ivpu_mmu_user_context_mark_invalid(struct ivpu_device *vdev, u32 ssid)
468263b2ba5SJacek Lawrynowicz {
469263b2ba5SJacek Lawrynowicz struct ivpu_file_priv *file_priv;
470263b2ba5SJacek Lawrynowicz
471263b2ba5SJacek Lawrynowicz xa_lock(&vdev->context_xa);
472263b2ba5SJacek Lawrynowicz
473263b2ba5SJacek Lawrynowicz file_priv = xa_load(&vdev->context_xa, ssid);
474263b2ba5SJacek Lawrynowicz if (file_priv)
475263b2ba5SJacek Lawrynowicz file_priv->has_mmu_faults = true;
476263b2ba5SJacek Lawrynowicz
477263b2ba5SJacek Lawrynowicz xa_unlock(&vdev->context_xa);
478263b2ba5SJacek Lawrynowicz }
479263b2ba5SJacek Lawrynowicz
ivpu_mmu_user_context_init(struct ivpu_device * vdev,struct ivpu_mmu_context * ctx,u32 ctx_id)480263b2ba5SJacek Lawrynowicz int ivpu_mmu_user_context_init(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx, u32 ctx_id)
481263b2ba5SJacek Lawrynowicz {
482263b2ba5SJacek Lawrynowicz int ret;
483263b2ba5SJacek Lawrynowicz
484263b2ba5SJacek Lawrynowicz drm_WARN_ON(&vdev->drm, !ctx_id);
485263b2ba5SJacek Lawrynowicz
486263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_context_init(vdev, ctx, ctx_id);
487263b2ba5SJacek Lawrynowicz if (ret) {
488263b2ba5SJacek Lawrynowicz ivpu_err(vdev, "Failed to initialize context: %d\n", ret);
489263b2ba5SJacek Lawrynowicz return ret;
490263b2ba5SJacek Lawrynowicz }
491263b2ba5SJacek Lawrynowicz
492263b2ba5SJacek Lawrynowicz ret = ivpu_mmu_set_pgtable(vdev, ctx_id, &ctx->pgtable);
493263b2ba5SJacek Lawrynowicz if (ret) {
494263b2ba5SJacek Lawrynowicz ivpu_err(vdev, "Failed to set page table: %d\n", ret);
495263b2ba5SJacek Lawrynowicz goto err_context_fini;
496263b2ba5SJacek Lawrynowicz }
497263b2ba5SJacek Lawrynowicz
498263b2ba5SJacek Lawrynowicz return 0;
499263b2ba5SJacek Lawrynowicz
500263b2ba5SJacek Lawrynowicz err_context_fini:
501263b2ba5SJacek Lawrynowicz ivpu_mmu_context_fini(vdev, ctx);
502263b2ba5SJacek Lawrynowicz return ret;
503263b2ba5SJacek Lawrynowicz }
504263b2ba5SJacek Lawrynowicz
ivpu_mmu_user_context_fini(struct ivpu_device * vdev,struct ivpu_mmu_context * ctx)505263b2ba5SJacek Lawrynowicz void ivpu_mmu_user_context_fini(struct ivpu_device *vdev, struct ivpu_mmu_context *ctx)
506263b2ba5SJacek Lawrynowicz {
507263b2ba5SJacek Lawrynowicz drm_WARN_ON(&vdev->drm, !ctx->id);
508263b2ba5SJacek Lawrynowicz
509263b2ba5SJacek Lawrynowicz ivpu_mmu_clear_pgtable(vdev, ctx->id);
510263b2ba5SJacek Lawrynowicz ivpu_mmu_context_fini(vdev, ctx);
511263b2ba5SJacek Lawrynowicz }
512