1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2019 Linaro, Ltd, Rob Herring <robh@kernel.org> */
3 #include <linux/atomic.h>
4 #include <linux/bitfield.h>
5 #include <linux/delay.h>
6 #include <linux/dma-mapping.h>
7 #include <linux/interrupt.h>
9 #include <linux/iopoll.h>
10 #include <linux/io-pgtable.h>
11 #include <linux/iommu.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/shmem_fs.h>
15 #include <linux/sizes.h>
17 #include "panfrost_device.h"
18 #include "panfrost_mmu.h"
19 #include "panfrost_gem.h"
20 #include "panfrost_features.h"
21 #include "panfrost_regs.h"
23 #define mmu_write(dev, reg, data) writel(data, dev->iomem + reg)
24 #define mmu_read(dev, reg) readl(dev->iomem + reg)
26 static int wait_ready(struct panfrost_device
*pfdev
, u32 as_nr
)
31 /* Wait for the MMU status to indicate there is no active command, in
32 * case one is pending. */
33 ret
= readl_relaxed_poll_timeout_atomic(pfdev
->iomem
+ AS_STATUS(as_nr
),
34 val
, !(val
& AS_STATUS_AS_ACTIVE
), 10, 1000);
37 dev_err(pfdev
->dev
, "AS_ACTIVE bit stuck\n");
42 static int write_cmd(struct panfrost_device
*pfdev
, u32 as_nr
, u32 cmd
)
46 /* write AS_COMMAND when MMU is ready to accept another command */
47 status
= wait_ready(pfdev
, as_nr
);
49 mmu_write(pfdev
, AS_COMMAND(as_nr
), cmd
);
54 static void lock_region(struct panfrost_device
*pfdev
, u32 as_nr
,
55 u64 iova
, size_t size
)
58 u64 region
= iova
& PAGE_MASK
;
64 * results in the range (11 .. 42)
67 size
= round_up(size
, PAGE_SIZE
);
69 region_width
= 10 + fls(size
>> PAGE_SHIFT
);
70 if ((size
>> PAGE_SHIFT
) != (1ul << (region_width
- 11))) {
71 /* not pow2, so must go up to the next pow2 */
74 region
|= region_width
;
76 /* Lock the region that needs to be updated */
77 mmu_write(pfdev
, AS_LOCKADDR_LO(as_nr
), region
& 0xFFFFFFFFUL
);
78 mmu_write(pfdev
, AS_LOCKADDR_HI(as_nr
), (region
>> 32) & 0xFFFFFFFFUL
);
79 write_cmd(pfdev
, as_nr
, AS_COMMAND_LOCK
);
83 static int mmu_hw_do_operation_locked(struct panfrost_device
*pfdev
, int as_nr
,
84 u64 iova
, size_t size
, u32 op
)
89 if (op
!= AS_COMMAND_UNLOCK
)
90 lock_region(pfdev
, as_nr
, iova
, size
);
92 /* Run the MMU operation */
93 write_cmd(pfdev
, as_nr
, op
);
95 /* Wait for the flush to complete */
96 return wait_ready(pfdev
, as_nr
);
99 static int mmu_hw_do_operation(struct panfrost_device
*pfdev
,
100 struct panfrost_mmu
*mmu
,
101 u64 iova
, size_t size
, u32 op
)
105 spin_lock(&pfdev
->as_lock
);
106 ret
= mmu_hw_do_operation_locked(pfdev
, mmu
->as
, iova
, size
, op
);
107 spin_unlock(&pfdev
->as_lock
);
111 static void panfrost_mmu_enable(struct panfrost_device
*pfdev
, struct panfrost_mmu
*mmu
)
114 struct io_pgtable_cfg
*cfg
= &mmu
->pgtbl_cfg
;
115 u64 transtab
= cfg
->arm_mali_lpae_cfg
.transtab
;
116 u64 memattr
= cfg
->arm_mali_lpae_cfg
.memattr
;
118 mmu_hw_do_operation_locked(pfdev
, as_nr
, 0, ~0UL, AS_COMMAND_FLUSH_MEM
);
120 mmu_write(pfdev
, AS_TRANSTAB_LO(as_nr
), transtab
& 0xffffffffUL
);
121 mmu_write(pfdev
, AS_TRANSTAB_HI(as_nr
), transtab
>> 32);
123 /* Need to revisit mem attrs.
124 * NC is the default, Mali driver is inner WT.
126 mmu_write(pfdev
, AS_MEMATTR_LO(as_nr
), memattr
& 0xffffffffUL
);
127 mmu_write(pfdev
, AS_MEMATTR_HI(as_nr
), memattr
>> 32);
129 write_cmd(pfdev
, as_nr
, AS_COMMAND_UPDATE
);
132 static void panfrost_mmu_disable(struct panfrost_device
*pfdev
, u32 as_nr
)
134 mmu_hw_do_operation_locked(pfdev
, as_nr
, 0, ~0UL, AS_COMMAND_FLUSH_MEM
);
136 mmu_write(pfdev
, AS_TRANSTAB_LO(as_nr
), 0);
137 mmu_write(pfdev
, AS_TRANSTAB_HI(as_nr
), 0);
139 mmu_write(pfdev
, AS_MEMATTR_LO(as_nr
), 0);
140 mmu_write(pfdev
, AS_MEMATTR_HI(as_nr
), 0);
142 write_cmd(pfdev
, as_nr
, AS_COMMAND_UPDATE
);
145 u32
panfrost_mmu_as_get(struct panfrost_device
*pfdev
, struct panfrost_mmu
*mmu
)
149 spin_lock(&pfdev
->as_lock
);
153 int en
= atomic_inc_return(&mmu
->as_count
);
154 WARN_ON(en
>= NUM_JOB_SLOTS
);
156 list_move(&mmu
->list
, &pfdev
->as_lru_list
);
160 /* Check for a free AS */
161 as
= ffz(pfdev
->as_alloc_mask
);
162 if (!(BIT(as
) & pfdev
->features
.as_present
)) {
163 struct panfrost_mmu
*lru_mmu
;
165 list_for_each_entry_reverse(lru_mmu
, &pfdev
->as_lru_list
, list
) {
166 if (!atomic_read(&lru_mmu
->as_count
))
169 WARN_ON(&lru_mmu
->list
== &pfdev
->as_lru_list
);
171 list_del_init(&lru_mmu
->list
);
178 /* Assign the free or reclaimed AS to the FD */
180 set_bit(as
, &pfdev
->as_alloc_mask
);
181 atomic_set(&mmu
->as_count
, 1);
182 list_add(&mmu
->list
, &pfdev
->as_lru_list
);
184 dev_dbg(pfdev
->dev
, "Assigned AS%d to mmu %p, alloc_mask=%lx", as
, mmu
, pfdev
->as_alloc_mask
);
186 panfrost_mmu_enable(pfdev
, mmu
);
189 spin_unlock(&pfdev
->as_lock
);
193 void panfrost_mmu_as_put(struct panfrost_device
*pfdev
, struct panfrost_mmu
*mmu
)
195 atomic_dec(&mmu
->as_count
);
196 WARN_ON(atomic_read(&mmu
->as_count
) < 0);
199 void panfrost_mmu_reset(struct panfrost_device
*pfdev
)
201 struct panfrost_mmu
*mmu
, *mmu_tmp
;
203 spin_lock(&pfdev
->as_lock
);
205 pfdev
->as_alloc_mask
= 0;
207 list_for_each_entry_safe(mmu
, mmu_tmp
, &pfdev
->as_lru_list
, list
) {
209 atomic_set(&mmu
->as_count
, 0);
210 list_del_init(&mmu
->list
);
213 spin_unlock(&pfdev
->as_lock
);
215 mmu_write(pfdev
, MMU_INT_CLEAR
, ~0);
216 mmu_write(pfdev
, MMU_INT_MASK
, ~0);
219 static size_t get_pgsize(u64 addr
, size_t size
)
221 if (addr
& (SZ_2M
- 1) || size
< SZ_2M
)
227 static void panfrost_mmu_flush_range(struct panfrost_device
*pfdev
,
228 struct panfrost_mmu
*mmu
,
229 u64 iova
, size_t size
)
234 pm_runtime_get_noresume(pfdev
->dev
);
236 /* Flush the PTs only if we're already awake */
237 if (pm_runtime_active(pfdev
->dev
))
238 mmu_hw_do_operation(pfdev
, mmu
, iova
, size
, AS_COMMAND_FLUSH_PT
);
240 pm_runtime_put_sync_autosuspend(pfdev
->dev
);
243 static int mmu_map_sg(struct panfrost_device
*pfdev
, struct panfrost_mmu
*mmu
,
244 u64 iova
, int prot
, struct sg_table
*sgt
)
247 struct scatterlist
*sgl
;
248 struct io_pgtable_ops
*ops
= mmu
->pgtbl_ops
;
249 u64 start_iova
= iova
;
251 for_each_sg(sgt
->sgl
, sgl
, sgt
->nents
, count
) {
252 unsigned long paddr
= sg_dma_address(sgl
);
253 size_t len
= sg_dma_len(sgl
);
255 dev_dbg(pfdev
->dev
, "map: as=%d, iova=%llx, paddr=%lx, len=%zx", mmu
->as
, iova
, paddr
, len
);
258 size_t pgsize
= get_pgsize(iova
| paddr
, len
);
260 ops
->map(ops
, iova
, paddr
, pgsize
, prot
);
267 panfrost_mmu_flush_range(pfdev
, mmu
, start_iova
, iova
- start_iova
);
272 int panfrost_mmu_map(struct panfrost_gem_mapping
*mapping
)
274 struct panfrost_gem_object
*bo
= mapping
->obj
;
275 struct drm_gem_object
*obj
= &bo
->base
.base
;
276 struct panfrost_device
*pfdev
= to_panfrost_device(obj
->dev
);
277 struct sg_table
*sgt
;
278 int prot
= IOMMU_READ
| IOMMU_WRITE
;
280 if (WARN_ON(mapping
->active
))
284 prot
|= IOMMU_NOEXEC
;
286 sgt
= drm_gem_shmem_get_pages_sgt(obj
);
287 if (WARN_ON(IS_ERR(sgt
)))
290 mmu_map_sg(pfdev
, mapping
->mmu
, mapping
->mmnode
.start
<< PAGE_SHIFT
,
292 mapping
->active
= true;
297 void panfrost_mmu_unmap(struct panfrost_gem_mapping
*mapping
)
299 struct panfrost_gem_object
*bo
= mapping
->obj
;
300 struct drm_gem_object
*obj
= &bo
->base
.base
;
301 struct panfrost_device
*pfdev
= to_panfrost_device(obj
->dev
);
302 struct io_pgtable_ops
*ops
= mapping
->mmu
->pgtbl_ops
;
303 u64 iova
= mapping
->mmnode
.start
<< PAGE_SHIFT
;
304 size_t len
= mapping
->mmnode
.size
<< PAGE_SHIFT
;
305 size_t unmapped_len
= 0;
307 if (WARN_ON(!mapping
->active
))
310 dev_dbg(pfdev
->dev
, "unmap: as=%d, iova=%llx, len=%zx",
311 mapping
->mmu
->as
, iova
, len
);
313 while (unmapped_len
< len
) {
314 size_t unmapped_page
;
315 size_t pgsize
= get_pgsize(iova
, len
- unmapped_len
);
317 if (ops
->iova_to_phys(ops
, iova
)) {
318 unmapped_page
= ops
->unmap(ops
, iova
, pgsize
, NULL
);
319 WARN_ON(unmapped_page
!= pgsize
);
322 unmapped_len
+= pgsize
;
325 panfrost_mmu_flush_range(pfdev
, mapping
->mmu
,
326 mapping
->mmnode
.start
<< PAGE_SHIFT
, len
);
327 mapping
->active
= false;
330 static void mmu_tlb_inv_context_s1(void *cookie
)
333 static void mmu_tlb_sync_context(void *cookie
)
335 //struct panfrost_device *pfdev = cookie;
336 // TODO: Wait 1000 GPU cycles for HW_ISSUE_6367/T60X
339 static void mmu_tlb_flush_walk(unsigned long iova
, size_t size
, size_t granule
,
342 mmu_tlb_sync_context(cookie
);
345 static void mmu_tlb_flush_leaf(unsigned long iova
, size_t size
, size_t granule
,
348 mmu_tlb_sync_context(cookie
);
351 static const struct iommu_flush_ops mmu_tlb_ops
= {
352 .tlb_flush_all
= mmu_tlb_inv_context_s1
,
353 .tlb_flush_walk
= mmu_tlb_flush_walk
,
354 .tlb_flush_leaf
= mmu_tlb_flush_leaf
,
357 int panfrost_mmu_pgtable_alloc(struct panfrost_file_priv
*priv
)
359 struct panfrost_mmu
*mmu
= &priv
->mmu
;
360 struct panfrost_device
*pfdev
= priv
->pfdev
;
362 INIT_LIST_HEAD(&mmu
->list
);
365 mmu
->pgtbl_cfg
= (struct io_pgtable_cfg
) {
366 .pgsize_bitmap
= SZ_4K
| SZ_2M
,
367 .ias
= FIELD_GET(0xff, pfdev
->features
.mmu_features
),
368 .oas
= FIELD_GET(0xff00, pfdev
->features
.mmu_features
),
370 .iommu_dev
= pfdev
->dev
,
373 mmu
->pgtbl_ops
= alloc_io_pgtable_ops(ARM_MALI_LPAE
, &mmu
->pgtbl_cfg
,
381 void panfrost_mmu_pgtable_free(struct panfrost_file_priv
*priv
)
383 struct panfrost_device
*pfdev
= priv
->pfdev
;
384 struct panfrost_mmu
*mmu
= &priv
->mmu
;
386 spin_lock(&pfdev
->as_lock
);
388 pm_runtime_get_noresume(pfdev
->dev
);
389 if (pm_runtime_active(pfdev
->dev
))
390 panfrost_mmu_disable(pfdev
, mmu
->as
);
391 pm_runtime_put_autosuspend(pfdev
->dev
);
393 clear_bit(mmu
->as
, &pfdev
->as_alloc_mask
);
394 clear_bit(mmu
->as
, &pfdev
->as_in_use_mask
);
395 list_del(&mmu
->list
);
397 spin_unlock(&pfdev
->as_lock
);
399 free_io_pgtable_ops(mmu
->pgtbl_ops
);
402 static struct panfrost_gem_mapping
*
403 addr_to_mapping(struct panfrost_device
*pfdev
, int as
, u64 addr
)
405 struct panfrost_gem_mapping
*mapping
= NULL
;
406 struct panfrost_file_priv
*priv
;
407 struct drm_mm_node
*node
;
408 u64 offset
= addr
>> PAGE_SHIFT
;
409 struct panfrost_mmu
*mmu
;
411 spin_lock(&pfdev
->as_lock
);
412 list_for_each_entry(mmu
, &pfdev
->as_lru_list
, list
) {
419 priv
= container_of(mmu
, struct panfrost_file_priv
, mmu
);
421 spin_lock(&priv
->mm_lock
);
423 drm_mm_for_each_node(node
, &priv
->mm
) {
424 if (offset
>= node
->start
&&
425 offset
< (node
->start
+ node
->size
)) {
426 mapping
= drm_mm_node_to_panfrost_mapping(node
);
428 kref_get(&mapping
->refcount
);
433 spin_unlock(&priv
->mm_lock
);
435 spin_unlock(&pfdev
->as_lock
);
439 #define NUM_FAULT_PAGES (SZ_2M / PAGE_SIZE)
441 static int panfrost_mmu_map_fault_addr(struct panfrost_device
*pfdev
, int as
,
445 struct panfrost_gem_mapping
*bomapping
;
446 struct panfrost_gem_object
*bo
;
447 struct address_space
*mapping
;
449 struct sg_table
*sgt
;
452 bomapping
= addr_to_mapping(pfdev
, as
, addr
);
458 dev_WARN(pfdev
->dev
, "matching BO is not heap type (GPU VA = %llx)",
459 bomapping
->mmnode
.start
<< PAGE_SHIFT
);
463 WARN_ON(bomapping
->mmu
->as
!= as
);
465 /* Assume 2MB alignment and size multiple */
466 addr
&= ~((u64
)SZ_2M
- 1);
467 page_offset
= addr
>> PAGE_SHIFT
;
468 page_offset
-= bomapping
->mmnode
.start
;
470 mutex_lock(&bo
->base
.pages_lock
);
472 if (!bo
->base
.pages
) {
473 bo
->sgts
= kvmalloc_array(bo
->base
.base
.size
/ SZ_2M
,
474 sizeof(struct sg_table
), GFP_KERNEL
| __GFP_ZERO
);
476 mutex_unlock(&bo
->base
.pages_lock
);
481 pages
= kvmalloc_array(bo
->base
.base
.size
>> PAGE_SHIFT
,
482 sizeof(struct page
*), GFP_KERNEL
| __GFP_ZERO
);
486 mutex_unlock(&bo
->base
.pages_lock
);
490 bo
->base
.pages
= pages
;
491 bo
->base
.pages_use_count
= 1;
493 pages
= bo
->base
.pages
;
495 mapping
= bo
->base
.base
.filp
->f_mapping
;
496 mapping_set_unevictable(mapping
);
498 for (i
= page_offset
; i
< page_offset
+ NUM_FAULT_PAGES
; i
++) {
499 pages
[i
] = shmem_read_mapping_page(mapping
, i
);
500 if (IS_ERR(pages
[i
])) {
501 mutex_unlock(&bo
->base
.pages_lock
);
502 ret
= PTR_ERR(pages
[i
]);
507 mutex_unlock(&bo
->base
.pages_lock
);
509 sgt
= &bo
->sgts
[page_offset
/ (SZ_2M
/ PAGE_SIZE
)];
510 ret
= sg_alloc_table_from_pages(sgt
, pages
+ page_offset
,
511 NUM_FAULT_PAGES
, 0, SZ_2M
, GFP_KERNEL
);
515 if (!dma_map_sg(pfdev
->dev
, sgt
->sgl
, sgt
->nents
, DMA_BIDIRECTIONAL
)) {
520 mmu_map_sg(pfdev
, bomapping
->mmu
, addr
,
521 IOMMU_WRITE
| IOMMU_READ
| IOMMU_NOEXEC
, sgt
);
523 bomapping
->active
= true;
525 dev_dbg(pfdev
->dev
, "mapped page fault @ AS%d %llx", as
, addr
);
527 panfrost_gem_mapping_put(bomapping
);
534 drm_gem_shmem_put_pages(&bo
->base
);
536 drm_gem_object_put_unlocked(&bo
->base
.base
);
540 static const char *access_type_name(struct panfrost_device
*pfdev
,
543 switch (fault_status
& AS_FAULTSTATUS_ACCESS_TYPE_MASK
) {
544 case AS_FAULTSTATUS_ACCESS_TYPE_ATOMIC
:
545 if (panfrost_has_hw_feature(pfdev
, HW_FEATURE_AARCH64_MMU
))
549 case AS_FAULTSTATUS_ACCESS_TYPE_READ
:
551 case AS_FAULTSTATUS_ACCESS_TYPE_WRITE
:
553 case AS_FAULTSTATUS_ACCESS_TYPE_EX
:
561 static irqreturn_t
panfrost_mmu_irq_handler(int irq
, void *data
)
563 struct panfrost_device
*pfdev
= data
;
565 if (!mmu_read(pfdev
, MMU_INT_STAT
))
568 mmu_write(pfdev
, MMU_INT_MASK
, 0);
569 return IRQ_WAKE_THREAD
;
572 static irqreturn_t
panfrost_mmu_irq_handler_thread(int irq
, void *data
)
574 struct panfrost_device
*pfdev
= data
;
575 u32 status
= mmu_read(pfdev
, MMU_INT_RAWSTAT
);
578 for (i
= 0; status
; i
++) {
579 u32 mask
= BIT(i
) | BIT(i
+ 16);
586 if (!(status
& mask
))
589 fault_status
= mmu_read(pfdev
, AS_FAULTSTATUS(i
));
590 addr
= mmu_read(pfdev
, AS_FAULTADDRESS_LO(i
));
591 addr
|= (u64
)mmu_read(pfdev
, AS_FAULTADDRESS_HI(i
)) << 32;
593 /* decode the fault status */
594 exception_type
= fault_status
& 0xFF;
595 access_type
= (fault_status
>> 8) & 0x3;
596 source_id
= (fault_status
>> 16);
598 /* Page fault only */
599 if ((status
& mask
) == BIT(i
)) {
600 WARN_ON(exception_type
< 0xC1 || exception_type
> 0xC4);
602 ret
= panfrost_mmu_map_fault_addr(pfdev
, i
, addr
);
604 mmu_write(pfdev
, MMU_INT_CLEAR
, BIT(i
));
610 /* terminal fault, print info about the fault */
612 "Unhandled Page fault in AS%d at VA 0x%016llX\n"
614 "raw fault status: 0x%X\n"
615 "decoded fault status: %s\n"
616 "exception type 0x%X: %s\n"
617 "access type 0x%X: %s\n"
622 (fault_status
& (1 << 10) ? "DECODER FAULT" : "SLAVE FAULT"),
623 exception_type
, panfrost_exception_name(pfdev
, exception_type
),
624 access_type
, access_type_name(pfdev
, fault_status
),
627 mmu_write(pfdev
, MMU_INT_CLEAR
, mask
);
632 mmu_write(pfdev
, MMU_INT_MASK
, ~0);
636 int panfrost_mmu_init(struct panfrost_device
*pfdev
)
640 irq
= platform_get_irq_byname(to_platform_device(pfdev
->dev
), "mmu");
644 err
= devm_request_threaded_irq(pfdev
->dev
, irq
, panfrost_mmu_irq_handler
,
645 panfrost_mmu_irq_handler_thread
,
646 IRQF_SHARED
, "mmu", pfdev
);
649 dev_err(pfdev
->dev
, "failed to request mmu irq");
656 void panfrost_mmu_fini(struct panfrost_device
*pfdev
)
658 mmu_write(pfdev
, MMU_INT_MASK
, 0);