1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IOMMU helpers in MMU context.
5 * Copyright (C) 2015 IBM Corp. <aik@ozlabs.ru>
8 #include <linux/sched/signal.h>
9 #include <linux/slab.h>
10 #include <linux/rculist.h>
11 #include <linux/vmalloc.h>
12 #include <linux/mutex.h>
13 #include <linux/migrate.h>
14 #include <linux/hugetlb.h>
15 #include <linux/swap.h>
16 #include <linux/sizes.h>
18 #include <asm/mmu_context.h>
19 #include <asm/pte-walk.h>
20 #include <linux/mm_inline.h>
22 static DEFINE_MUTEX(mem_list_mutex);
24 #define MM_IOMMU_TABLE_GROUP_PAGE_DIRTY 0x1
25 #define MM_IOMMU_TABLE_GROUP_PAGE_MASK ~(SZ_4K - 1)
27 struct mm_iommu_table_group_mem_t {
28 struct list_head next;
32 unsigned int pageshift;
33 u64 ua; /* userspace address */
34 u64 entries; /* number of entries in hpas/hpages[] */
36 * in mm_iommu_get we temporarily use this to store
37 * struct page address.
39 * We need to convert ua to hpa in real mode. Make it
40 * simpler by storing physical address.
43 struct page **hpages; /* vmalloc'ed */
46 #define MM_IOMMU_TABLE_INVALID_HPA ((uint64_t)-1)
47 u64 dev_hpa; /* Device memory base address */
50 bool mm_iommu_preregistered(struct mm_struct *mm)
52 return !list_empty(&mm->context.iommu_group_mem_list);
54 EXPORT_SYMBOL_GPL(mm_iommu_preregistered);
56 static long mm_iommu_do_alloc(struct mm_struct *mm, unsigned long ua,
57 unsigned long entries, unsigned long dev_hpa,
58 struct mm_iommu_table_group_mem_t **pmem)
60 struct mm_iommu_table_group_mem_t *mem, *mem2;
61 long i, ret, locked_entries = 0, pinned = 0;
62 unsigned int pageshift;
63 unsigned long entry, chunk;
65 if (dev_hpa == MM_IOMMU_TABLE_INVALID_HPA) {
66 ret = account_locked_vm(mm, entries, true);
70 locked_entries = entries;
73 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
79 if (dev_hpa != MM_IOMMU_TABLE_INVALID_HPA) {
80 mem->pageshift = __ffs(dev_hpa | (entries << PAGE_SHIFT));
81 mem->dev_hpa = dev_hpa;
84 mem->dev_hpa = MM_IOMMU_TABLE_INVALID_HPA;
87 * For a starting point for a maximum page size calculation
88 * we use @ua and @entries natural alignment to allow IOMMU pages
89 * smaller than huge pages but still bigger than PAGE_SIZE.
91 mem->pageshift = __ffs(ua | (entries << PAGE_SHIFT));
92 mem->hpas = vzalloc(array_size(entries, sizeof(mem->hpas[0])));
100 chunk = (1UL << (PAGE_SHIFT + MAX_ORDER)) /
101 sizeof(struct vm_area_struct *);
102 chunk = min(chunk, entries);
103 for (entry = 0; entry < entries; entry += chunk) {
104 unsigned long n = min(entries - entry, chunk);
106 ret = pin_user_pages(ua + (entry << PAGE_SHIFT), n,
107 FOLL_WRITE | FOLL_LONGTERM,
108 mem->hpages + entry);
117 mmap_read_unlock(mm);
118 if (pinned != entries) {
125 atomic64_set(&mem->mapped, 1);
128 mem->entries = entries;
130 mutex_lock(&mem_list_mutex);
132 list_for_each_entry_rcu(mem2, &mm->context.iommu_group_mem_list, next,
133 lockdep_is_held(&mem_list_mutex)) {
135 if ((mem2->ua < (ua + (entries << PAGE_SHIFT))) &&
137 (mem2->entries << PAGE_SHIFT)))) {
139 mutex_unlock(&mem_list_mutex);
144 if (mem->dev_hpa == MM_IOMMU_TABLE_INVALID_HPA) {
146 * Allow to use larger than 64k IOMMU pages. Only do that
147 * if we are backed by hugetlb. Skip device memory as it is not
148 * backed with page structs.
150 pageshift = PAGE_SHIFT;
151 for (i = 0; i < entries; ++i) {
152 struct page *page = mem->hpages[i];
154 if ((mem->pageshift > PAGE_SHIFT) && PageHuge(page))
155 pageshift = page_shift(compound_head(page));
156 mem->pageshift = min(mem->pageshift, pageshift);
158 * We don't need struct page reference any more, switch
159 * to physical address.
161 mem->hpas[i] = page_to_pfn(page) << PAGE_SHIFT;
165 list_add_rcu(&mem->next, &mm->context.iommu_group_mem_list);
167 mutex_unlock(&mem_list_mutex);
174 /* free the references taken */
175 unpin_user_pages(mem->hpages, pinned);
181 account_locked_vm(mm, locked_entries, false);
186 long mm_iommu_new(struct mm_struct *mm, unsigned long ua, unsigned long entries,
187 struct mm_iommu_table_group_mem_t **pmem)
189 return mm_iommu_do_alloc(mm, ua, entries, MM_IOMMU_TABLE_INVALID_HPA,
192 EXPORT_SYMBOL_GPL(mm_iommu_new);
194 long mm_iommu_newdev(struct mm_struct *mm, unsigned long ua,
195 unsigned long entries, unsigned long dev_hpa,
196 struct mm_iommu_table_group_mem_t **pmem)
198 return mm_iommu_do_alloc(mm, ua, entries, dev_hpa, pmem);
200 EXPORT_SYMBOL_GPL(mm_iommu_newdev);
202 static void mm_iommu_unpin(struct mm_iommu_table_group_mem_t *mem)
205 struct page *page = NULL;
210 for (i = 0; i < mem->entries; ++i) {
214 page = pfn_to_page(mem->hpas[i] >> PAGE_SHIFT);
218 if (mem->hpas[i] & MM_IOMMU_TABLE_GROUP_PAGE_DIRTY)
221 unpin_user_page(page);
227 static void mm_iommu_do_free(struct mm_iommu_table_group_mem_t *mem)
235 static void mm_iommu_free(struct rcu_head *head)
237 struct mm_iommu_table_group_mem_t *mem = container_of(head,
238 struct mm_iommu_table_group_mem_t, rcu);
240 mm_iommu_do_free(mem);
243 static void mm_iommu_release(struct mm_iommu_table_group_mem_t *mem)
245 list_del_rcu(&mem->next);
246 call_rcu(&mem->rcu, mm_iommu_free);
249 long mm_iommu_put(struct mm_struct *mm, struct mm_iommu_table_group_mem_t *mem)
252 unsigned long unlock_entries = 0;
254 mutex_lock(&mem_list_mutex);
256 if (mem->used == 0) {
262 /* There are still users, exit */
266 /* Are there still mappings? */
267 if (atomic64_cmpxchg(&mem->mapped, 1, 0) != 1) {
273 if (mem->dev_hpa == MM_IOMMU_TABLE_INVALID_HPA)
274 unlock_entries = mem->entries;
276 /* @mapped became 0 so now mappings are disabled, release the region */
277 mm_iommu_release(mem);
280 mutex_unlock(&mem_list_mutex);
282 account_locked_vm(mm, unlock_entries, false);
286 EXPORT_SYMBOL_GPL(mm_iommu_put);
288 struct mm_iommu_table_group_mem_t *mm_iommu_lookup(struct mm_struct *mm,
289 unsigned long ua, unsigned long size)
291 struct mm_iommu_table_group_mem_t *mem, *ret = NULL;
294 list_for_each_entry_rcu(mem, &mm->context.iommu_group_mem_list, next) {
295 if ((mem->ua <= ua) &&
296 (ua + size <= mem->ua +
297 (mem->entries << PAGE_SHIFT))) {
306 EXPORT_SYMBOL_GPL(mm_iommu_lookup);
308 struct mm_iommu_table_group_mem_t *mm_iommu_get(struct mm_struct *mm,
309 unsigned long ua, unsigned long entries)
311 struct mm_iommu_table_group_mem_t *mem, *ret = NULL;
313 mutex_lock(&mem_list_mutex);
315 list_for_each_entry_rcu(mem, &mm->context.iommu_group_mem_list, next,
316 lockdep_is_held(&mem_list_mutex)) {
317 if ((mem->ua == ua) && (mem->entries == entries)) {
324 mutex_unlock(&mem_list_mutex);
328 EXPORT_SYMBOL_GPL(mm_iommu_get);
330 long mm_iommu_ua_to_hpa(struct mm_iommu_table_group_mem_t *mem,
331 unsigned long ua, unsigned int pageshift, unsigned long *hpa)
333 const long entry = (ua - mem->ua) >> PAGE_SHIFT;
336 if (entry >= mem->entries)
339 if (pageshift > mem->pageshift)
343 *hpa = mem->dev_hpa + (ua - mem->ua);
347 va = &mem->hpas[entry];
348 *hpa = (*va & MM_IOMMU_TABLE_GROUP_PAGE_MASK) | (ua & ~PAGE_MASK);
352 EXPORT_SYMBOL_GPL(mm_iommu_ua_to_hpa);
354 bool mm_iommu_is_devmem(struct mm_struct *mm, unsigned long hpa,
355 unsigned int pageshift, unsigned long *size)
357 struct mm_iommu_table_group_mem_t *mem;
361 list_for_each_entry_rcu(mem, &mm->context.iommu_group_mem_list, next) {
362 if (mem->dev_hpa == MM_IOMMU_TABLE_INVALID_HPA)
365 end = mem->dev_hpa + (mem->entries << PAGE_SHIFT);
366 if ((mem->dev_hpa <= hpa) && (hpa < end)) {
368 * Since the IOMMU page size might be bigger than
369 * PAGE_SIZE, the amount of preregistered memory
370 * starting from @hpa might be smaller than 1<<pageshift
371 * and the caller needs to distinguish this situation.
373 *size = min(1UL << pageshift, end - hpa);
381 EXPORT_SYMBOL_GPL(mm_iommu_is_devmem);
383 long mm_iommu_mapped_inc(struct mm_iommu_table_group_mem_t *mem)
385 if (atomic64_inc_not_zero(&mem->mapped))
388 /* Last mm_iommu_put() has been called, no more mappings allowed() */
391 EXPORT_SYMBOL_GPL(mm_iommu_mapped_inc);
393 void mm_iommu_mapped_dec(struct mm_iommu_table_group_mem_t *mem)
395 atomic64_add_unless(&mem->mapped, -1, 1);
397 EXPORT_SYMBOL_GPL(mm_iommu_mapped_dec);
399 void mm_iommu_init(struct mm_struct *mm)
401 INIT_LIST_HEAD_RCU(&mm->context.iommu_group_mem_list);