1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 Intel Corporation.
4 * Copyright 2018 Google LLC.
6 * Author: Tuukka Toivonen <tuukka.toivonen@intel.com>
7 * Author: Sakari Ailus <sakari.ailus@linux.intel.com>
8 * Author: Samu Onkalo <samu.onkalo@intel.com>
9 * Author: Tomasz Figa <tfiga@chromium.org>
13 #include <linux/dma-mapping.h>
14 #include <linux/iopoll.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/slab.h>
17 #include <linux/vmalloc.h>
19 #include <asm/set_memory.h>
23 #define IPU3_PAGE_SHIFT 12
24 #define IPU3_PAGE_SIZE (1UL << IPU3_PAGE_SHIFT)
26 #define IPU3_PT_BITS 10
27 #define IPU3_PT_PTES (1UL << IPU3_PT_BITS)
28 #define IPU3_PT_SIZE (IPU3_PT_PTES << 2)
29 #define IPU3_PT_ORDER (IPU3_PT_SIZE >> PAGE_SHIFT)
31 #define IPU3_ADDR2PTE(addr) ((addr) >> IPU3_PAGE_SHIFT)
32 #define IPU3_PTE2ADDR(pte) ((phys_addr_t)(pte) << IPU3_PAGE_SHIFT)
34 #define IPU3_L2PT_SHIFT IPU3_PT_BITS
35 #define IPU3_L2PT_MASK ((1UL << IPU3_L2PT_SHIFT) - 1)
37 #define IPU3_L1PT_SHIFT IPU3_PT_BITS
38 #define IPU3_L1PT_MASK ((1UL << IPU3_L1PT_SHIFT) - 1)
40 #define IPU3_MMU_ADDRESS_BITS (IPU3_PAGE_SHIFT + \
44 #define IMGU_REG_BASE 0x4000
45 #define REG_TLB_INVALIDATE (IMGU_REG_BASE + 0x300)
46 #define TLB_INVALIDATE 1
47 #define REG_L1_PHYS (IMGU_REG_BASE + 0x304) /* 27-bit pfn */
48 #define REG_GP_HALT (IMGU_REG_BASE + 0x5dc)
49 #define REG_GP_HALTED (IMGU_REG_BASE + 0x5e0)
54 /* protect access to l2pts, l1pt */
58 u32 dummy_page_pteval;
61 u32 dummy_l2pt_pteval;
66 struct ipu3_mmu_info geometry;
69 static inline struct ipu3_mmu *to_ipu3_mmu(struct ipu3_mmu_info *info)
71 return container_of(info, struct ipu3_mmu, geometry);
75 * ipu3_mmu_tlb_invalidate - invalidate translation look-aside buffer
76 * @mmu: MMU to perform the invalidate operation on
78 * This function invalidates the whole TLB. Must be called when the hardware
81 static void ipu3_mmu_tlb_invalidate(struct ipu3_mmu *mmu)
83 writel(TLB_INVALIDATE, mmu->base + REG_TLB_INVALIDATE);
86 static void call_if_ipu3_is_powered(struct ipu3_mmu *mmu,
87 void (*func)(struct ipu3_mmu *mmu))
89 if (!pm_runtime_get_if_in_use(mmu->dev))
93 pm_runtime_put(mmu->dev);
97 * ipu3_mmu_set_halt - set CIO gate halt bit
98 * @mmu: MMU to set the CIO gate bit in.
99 * @halt: Desired state of the gate bit.
101 * This function sets the CIO gate bit that controls whether external memory
102 * accesses are allowed. Must be called when the hardware is powered on.
104 static void ipu3_mmu_set_halt(struct ipu3_mmu *mmu, bool halt)
109 writel(halt, mmu->base + REG_GP_HALT);
110 ret = readl_poll_timeout(mmu->base + REG_GP_HALTED,
111 val, (val & 1) == halt, 1000, 100000);
114 dev_err(mmu->dev, "failed to %s CIO gate halt\n",
115 halt ? "set" : "clear");
119 * ipu3_mmu_alloc_page_table - allocate a pre-filled page table
120 * @pteval: Value to initialize for page table entries with.
122 * Return: Pointer to allocated page table or NULL on failure.
124 static u32 *ipu3_mmu_alloc_page_table(u32 pteval)
129 pt = (u32 *)__get_free_page(GFP_KERNEL);
133 for (pte = 0; pte < IPU3_PT_PTES; pte++)
136 set_memory_uc((unsigned long int)pt, IPU3_PT_ORDER);
142 * ipu3_mmu_free_page_table - free page table
143 * @pt: Page table to free.
145 static void ipu3_mmu_free_page_table(u32 *pt)
147 set_memory_wb((unsigned long int)pt, IPU3_PT_ORDER);
148 free_page((unsigned long)pt);
152 * address_to_pte_idx - split IOVA into L1 and L2 page table indices
153 * @iova: IOVA to split.
154 * @l1pt_idx: Output for the L1 page table index.
155 * @l2pt_idx: Output for the L2 page index.
157 static inline void address_to_pte_idx(unsigned long iova, u32 *l1pt_idx,
160 iova >>= IPU3_PAGE_SHIFT;
163 *l2pt_idx = iova & IPU3_L2PT_MASK;
165 iova >>= IPU3_L2PT_SHIFT;
168 *l1pt_idx = iova & IPU3_L1PT_MASK;
171 static u32 *ipu3_mmu_get_l2pt(struct ipu3_mmu *mmu, u32 l1pt_idx)
174 u32 *l2pt, *new_l2pt;
177 spin_lock_irqsave(&mmu->lock, flags);
179 l2pt = mmu->l2pts[l1pt_idx];
183 spin_unlock_irqrestore(&mmu->lock, flags);
185 new_l2pt = ipu3_mmu_alloc_page_table(mmu->dummy_page_pteval);
189 spin_lock_irqsave(&mmu->lock, flags);
191 dev_dbg(mmu->dev, "allocated page table %p for l1pt_idx %u\n",
194 l2pt = mmu->l2pts[l1pt_idx];
196 ipu3_mmu_free_page_table(new_l2pt);
201 mmu->l2pts[l1pt_idx] = new_l2pt;
203 pteval = IPU3_ADDR2PTE(virt_to_phys(new_l2pt));
204 mmu->l1pt[l1pt_idx] = pteval;
207 spin_unlock_irqrestore(&mmu->lock, flags);
211 static int __ipu3_mmu_map(struct ipu3_mmu *mmu, unsigned long iova,
214 u32 l1pt_idx, l2pt_idx;
221 address_to_pte_idx(iova, &l1pt_idx, &l2pt_idx);
223 l2pt = ipu3_mmu_get_l2pt(mmu, l1pt_idx);
227 spin_lock_irqsave(&mmu->lock, flags);
229 if (l2pt[l2pt_idx] != mmu->dummy_page_pteval) {
230 spin_unlock_irqrestore(&mmu->lock, flags);
234 l2pt[l2pt_idx] = IPU3_ADDR2PTE(paddr);
236 spin_unlock_irqrestore(&mmu->lock, flags);
242 * The following four functions are implemented based on iommu.c
243 * drivers/iommu/iommu.c/iommu_pgsize().
245 static size_t ipu3_mmu_pgsize(unsigned long pgsize_bitmap,
246 unsigned long addr_merge, size_t size)
248 unsigned int pgsize_idx;
251 /* Max page size that still fits into 'size' */
252 pgsize_idx = __fls(size);
254 /* need to consider alignment requirements ? */
255 if (likely(addr_merge)) {
256 /* Max page size allowed by address */
257 unsigned int align_pgsize_idx = __ffs(addr_merge);
259 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
262 /* build a mask of acceptable page sizes */
263 pgsize = (1UL << (pgsize_idx + 1)) - 1;
265 /* throw away page sizes not supported by the hardware */
266 pgsize &= pgsize_bitmap;
268 /* make sure we're still sane */
271 /* pick the biggest page */
272 pgsize_idx = __fls(pgsize);
273 pgsize = 1UL << pgsize_idx;
278 /* drivers/iommu/iommu.c/iommu_map() */
279 int ipu3_mmu_map(struct ipu3_mmu_info *info, unsigned long iova,
280 phys_addr_t paddr, size_t size)
282 struct ipu3_mmu *mmu = to_ipu3_mmu(info);
283 unsigned int min_pagesz;
286 /* find out the minimum page size supported */
287 min_pagesz = 1 << __ffs(mmu->geometry.pgsize_bitmap);
290 * both the virtual address and the physical one, as well as
291 * the size of the mapping, must be aligned (at least) to the
292 * size of the smallest page supported by the hardware
294 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
295 dev_err(mmu->dev, "unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
296 iova, &paddr, size, min_pagesz);
300 dev_dbg(mmu->dev, "map: iova 0x%lx pa %pa size 0x%zx\n",
304 size_t pgsize = ipu3_mmu_pgsize(mmu->geometry.pgsize_bitmap,
307 dev_dbg(mmu->dev, "mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
308 iova, &paddr, pgsize);
310 ret = __ipu3_mmu_map(mmu, iova, paddr);
319 call_if_ipu3_is_powered(mmu, ipu3_mmu_tlb_invalidate);
324 /* drivers/iommu/iommu.c/default_iommu_map_sg() */
325 size_t ipu3_mmu_map_sg(struct ipu3_mmu_info *info, unsigned long iova,
326 struct scatterlist *sg, unsigned int nents)
328 struct ipu3_mmu *mmu = to_ipu3_mmu(info);
329 struct scatterlist *s;
330 size_t s_length, mapped = 0;
331 unsigned int i, min_pagesz;
334 min_pagesz = 1 << __ffs(mmu->geometry.pgsize_bitmap);
336 for_each_sg(sg, s, nents, i) {
337 phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset;
339 s_length = s->length;
341 if (!IS_ALIGNED(s->offset, min_pagesz))
344 /* must be min_pagesz aligned to be mapped singlely */
345 if (i == nents - 1 && !IS_ALIGNED(s->length, min_pagesz))
346 s_length = PAGE_ALIGN(s->length);
348 ret = ipu3_mmu_map(info, iova + mapped, phys, s_length);
355 call_if_ipu3_is_powered(mmu, ipu3_mmu_tlb_invalidate);
360 /* undo mappings already done */
361 ipu3_mmu_unmap(info, iova, mapped);
366 static size_t __ipu3_mmu_unmap(struct ipu3_mmu *mmu,
367 unsigned long iova, size_t size)
369 u32 l1pt_idx, l2pt_idx;
377 address_to_pte_idx(iova, &l1pt_idx, &l2pt_idx);
379 spin_lock_irqsave(&mmu->lock, flags);
381 l2pt = mmu->l2pts[l1pt_idx];
383 spin_unlock_irqrestore(&mmu->lock, flags);
387 if (l2pt[l2pt_idx] == mmu->dummy_page_pteval)
390 l2pt[l2pt_idx] = mmu->dummy_page_pteval;
392 spin_unlock_irqrestore(&mmu->lock, flags);
397 /* drivers/iommu/iommu.c/iommu_unmap() */
398 size_t ipu3_mmu_unmap(struct ipu3_mmu_info *info, unsigned long iova,
401 struct ipu3_mmu *mmu = to_ipu3_mmu(info);
402 size_t unmapped_page, unmapped = 0;
403 unsigned int min_pagesz;
405 /* find out the minimum page size supported */
406 min_pagesz = 1 << __ffs(mmu->geometry.pgsize_bitmap);
409 * The virtual address, as well as the size of the mapping, must be
410 * aligned (at least) to the size of the smallest page supported
413 if (!IS_ALIGNED(iova | size, min_pagesz)) {
414 dev_err(mmu->dev, "unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
415 iova, size, min_pagesz);
419 dev_dbg(mmu->dev, "unmap this: iova 0x%lx size 0x%zx\n", iova, size);
422 * Keep iterating until we either unmap 'size' bytes (or more)
423 * or we hit an area that isn't mapped.
425 while (unmapped < size) {
426 size_t pgsize = ipu3_mmu_pgsize(mmu->geometry.pgsize_bitmap,
427 iova, size - unmapped);
429 unmapped_page = __ipu3_mmu_unmap(mmu, iova, pgsize);
433 dev_dbg(mmu->dev, "unmapped: iova 0x%lx size 0x%zx\n",
434 iova, unmapped_page);
436 iova += unmapped_page;
437 unmapped += unmapped_page;
440 call_if_ipu3_is_powered(mmu, ipu3_mmu_tlb_invalidate);
446 * ipu3_mmu_init() - initialize IPU3 MMU block
447 * @base: IOMEM base of hardware registers.
449 * Return: Pointer to IPU3 MMU private data pointer or ERR_PTR() on error.
451 struct ipu3_mmu_info *ipu3_mmu_init(struct device *parent, void __iomem *base)
453 struct ipu3_mmu *mmu;
456 mmu = kzalloc(sizeof(*mmu), GFP_KERNEL);
458 return ERR_PTR(-ENOMEM);
462 spin_lock_init(&mmu->lock);
464 /* Disallow external memory access when having no valid page tables. */
465 ipu3_mmu_set_halt(mmu, true);
468 * The MMU does not have a "valid" bit, so we have to use a dummy
469 * page for invalid entries.
471 mmu->dummy_page = (void *)__get_free_page(GFP_KERNEL);
472 if (!mmu->dummy_page)
474 pteval = IPU3_ADDR2PTE(virt_to_phys(mmu->dummy_page));
475 mmu->dummy_page_pteval = pteval;
478 * Allocate a dummy L2 page table with all entries pointing to
481 mmu->dummy_l2pt = ipu3_mmu_alloc_page_table(pteval);
482 if (!mmu->dummy_l2pt)
483 goto fail_dummy_page;
484 pteval = IPU3_ADDR2PTE(virt_to_phys(mmu->dummy_l2pt));
485 mmu->dummy_l2pt_pteval = pteval;
488 * Allocate the array of L2PT CPU pointers, initialized to zero,
489 * which means the dummy L2PT allocated above.
491 mmu->l2pts = vzalloc(IPU3_PT_PTES * sizeof(*mmu->l2pts));
495 /* Allocate the L1 page table. */
496 mmu->l1pt = ipu3_mmu_alloc_page_table(mmu->dummy_l2pt_pteval);
500 pteval = IPU3_ADDR2PTE(virt_to_phys(mmu->l1pt));
501 writel(pteval, mmu->base + REG_L1_PHYS);
502 ipu3_mmu_tlb_invalidate(mmu);
503 ipu3_mmu_set_halt(mmu, false);
505 mmu->geometry.aperture_start = 0;
506 mmu->geometry.aperture_end = DMA_BIT_MASK(IPU3_MMU_ADDRESS_BITS);
507 mmu->geometry.pgsize_bitmap = IPU3_PAGE_SIZE;
509 return &mmu->geometry;
514 ipu3_mmu_free_page_table(mmu->dummy_l2pt);
516 free_page((unsigned long)mmu->dummy_page);
520 return ERR_PTR(-ENOMEM);
524 * ipu3_mmu_exit() - clean up IPU3 MMU block
525 * @mmu: IPU3 MMU private data
527 void ipu3_mmu_exit(struct ipu3_mmu_info *info)
529 struct ipu3_mmu *mmu = to_ipu3_mmu(info);
531 /* We are going to free our page tables, no more memory access. */
532 ipu3_mmu_set_halt(mmu, true);
533 ipu3_mmu_tlb_invalidate(mmu);
535 ipu3_mmu_free_page_table(mmu->l1pt);
537 ipu3_mmu_free_page_table(mmu->dummy_l2pt);
538 free_page((unsigned long)mmu->dummy_page);
542 void ipu3_mmu_suspend(struct ipu3_mmu_info *info)
544 struct ipu3_mmu *mmu = to_ipu3_mmu(info);
546 ipu3_mmu_set_halt(mmu, true);
549 void ipu3_mmu_resume(struct ipu3_mmu_info *info)
551 struct ipu3_mmu *mmu = to_ipu3_mmu(info);
554 ipu3_mmu_set_halt(mmu, true);
556 pteval = IPU3_ADDR2PTE(virt_to_phys(mmu->l1pt));
557 writel(pteval, mmu->base + REG_L1_PHYS);
559 ipu3_mmu_tlb_invalidate(mmu);
560 ipu3_mmu_set_halt(mmu, false);