2 * IOMMU helpers in MMU context.
4 * Copyright (C) 2015 IBM Corp. <aik@ozlabs.ru>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
13 #include <linux/sched/signal.h>
14 #include <linux/slab.h>
15 #include <linux/rculist.h>
16 #include <linux/vmalloc.h>
17 #include <linux/mutex.h>
18 #include <linux/migrate.h>
19 #include <linux/hugetlb.h>
20 #include <linux/swap.h>
21 #include <linux/sizes.h>
22 #include <asm/mmu_context.h>
23 #include <asm/pte-walk.h>
24 #include <linux/mm_inline.h>
26 static DEFINE_MUTEX(mem_list_mutex);
28 #define MM_IOMMU_TABLE_GROUP_PAGE_DIRTY 0x1
29 #define MM_IOMMU_TABLE_GROUP_PAGE_MASK ~(SZ_4K - 1)
31 struct mm_iommu_table_group_mem_t {
32 struct list_head next;
36 unsigned int pageshift;
37 u64 ua; /* userspace address */
38 u64 entries; /* number of entries in hpas/hpages[] */
40 * in mm_iommu_get we temporarily use this to store
41 * struct page address.
43 * We need to convert ua to hpa in real mode. Make it
44 * simpler by storing physical address.
47 struct page **hpages; /* vmalloc'ed */
50 #define MM_IOMMU_TABLE_INVALID_HPA ((uint64_t)-1)
51 u64 dev_hpa; /* Device memory base address */
54 static long mm_iommu_adjust_locked_vm(struct mm_struct *mm,
55 unsigned long npages, bool incr)
57 long ret = 0, locked, lock_limit;
62 down_write(&mm->mmap_sem);
65 locked = mm->locked_vm + npages;
66 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
67 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
70 mm->locked_vm += npages;
72 if (WARN_ON_ONCE(npages > mm->locked_vm))
73 npages = mm->locked_vm;
74 mm->locked_vm -= npages;
77 pr_debug("[%d] RLIMIT_MEMLOCK HASH64 %c%ld %ld/%ld\n",
78 current ? current->pid : 0,
81 mm->locked_vm << PAGE_SHIFT,
82 rlimit(RLIMIT_MEMLOCK));
83 up_write(&mm->mmap_sem);
88 bool mm_iommu_preregistered(struct mm_struct *mm)
90 return !list_empty(&mm->context.iommu_group_mem_list);
92 EXPORT_SYMBOL_GPL(mm_iommu_preregistered);
94 static long mm_iommu_do_alloc(struct mm_struct *mm, unsigned long ua,
95 unsigned long entries, unsigned long dev_hpa,
96 struct mm_iommu_table_group_mem_t **pmem)
98 struct mm_iommu_table_group_mem_t *mem, *mem2;
99 long i, ret, locked_entries = 0, pinned = 0;
100 unsigned int pageshift;
101 unsigned long entry, chunk;
103 if (dev_hpa == MM_IOMMU_TABLE_INVALID_HPA) {
104 ret = mm_iommu_adjust_locked_vm(mm, entries, true);
108 locked_entries = entries;
111 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
117 if (dev_hpa != MM_IOMMU_TABLE_INVALID_HPA) {
118 mem->pageshift = __ffs(dev_hpa | (entries << PAGE_SHIFT));
119 mem->dev_hpa = dev_hpa;
122 mem->dev_hpa = MM_IOMMU_TABLE_INVALID_HPA;
125 * For a starting point for a maximum page size calculation
126 * we use @ua and @entries natural alignment to allow IOMMU pages
127 * smaller than huge pages but still bigger than PAGE_SIZE.
129 mem->pageshift = __ffs(ua | (entries << PAGE_SHIFT));
130 mem->hpas = vzalloc(array_size(entries, sizeof(mem->hpas[0])));
137 down_read(&mm->mmap_sem);
138 chunk = (1UL << (PAGE_SHIFT + MAX_ORDER - 1)) /
139 sizeof(struct vm_area_struct *);
140 chunk = min(chunk, entries);
141 for (entry = 0; entry < entries; entry += chunk) {
142 unsigned long n = min(entries - entry, chunk);
144 ret = get_user_pages_longterm(ua + (entry << PAGE_SHIFT), n,
145 FOLL_WRITE, mem->hpages + entry, NULL);
154 up_read(&mm->mmap_sem);
155 if (pinned != entries) {
161 pageshift = PAGE_SHIFT;
162 for (i = 0; i < entries; ++i) {
163 struct page *page = mem->hpages[i];
166 * Allow to use larger than 64k IOMMU pages. Only do that
167 * if we are backed by hugetlb.
169 if ((mem->pageshift > PAGE_SHIFT) && PageHuge(page)) {
170 struct page *head = compound_head(page);
172 pageshift = compound_order(head) + PAGE_SHIFT;
174 mem->pageshift = min(mem->pageshift, pageshift);
176 * We don't need struct page reference any more, switch
177 * to physical address.
179 mem->hpas[i] = page_to_pfn(page) << PAGE_SHIFT;
183 atomic64_set(&mem->mapped, 1);
186 mem->entries = entries;
188 mutex_lock(&mem_list_mutex);
190 list_for_each_entry_rcu(mem2, &mm->context.iommu_group_mem_list, next) {
192 if ((mem2->ua < (ua + (entries << PAGE_SHIFT))) &&
194 (mem2->entries << PAGE_SHIFT)))) {
196 mutex_unlock(&mem_list_mutex);
201 list_add_rcu(&mem->next, &mm->context.iommu_group_mem_list);
203 mutex_unlock(&mem_list_mutex);
210 /* free the reference taken */
211 for (i = 0; i < pinned; i++)
212 put_page(mem->hpages[i]);
218 mm_iommu_adjust_locked_vm(mm, locked_entries, false);
223 long mm_iommu_new(struct mm_struct *mm, unsigned long ua, unsigned long entries,
224 struct mm_iommu_table_group_mem_t **pmem)
226 return mm_iommu_do_alloc(mm, ua, entries, MM_IOMMU_TABLE_INVALID_HPA,
229 EXPORT_SYMBOL_GPL(mm_iommu_new);
231 long mm_iommu_newdev(struct mm_struct *mm, unsigned long ua,
232 unsigned long entries, unsigned long dev_hpa,
233 struct mm_iommu_table_group_mem_t **pmem)
235 return mm_iommu_do_alloc(mm, ua, entries, dev_hpa, pmem);
237 EXPORT_SYMBOL_GPL(mm_iommu_newdev);
239 static void mm_iommu_unpin(struct mm_iommu_table_group_mem_t *mem)
242 struct page *page = NULL;
247 for (i = 0; i < mem->entries; ++i) {
251 page = pfn_to_page(mem->hpas[i] >> PAGE_SHIFT);
255 if (mem->hpas[i] & MM_IOMMU_TABLE_GROUP_PAGE_DIRTY)
263 static void mm_iommu_do_free(struct mm_iommu_table_group_mem_t *mem)
271 static void mm_iommu_free(struct rcu_head *head)
273 struct mm_iommu_table_group_mem_t *mem = container_of(head,
274 struct mm_iommu_table_group_mem_t, rcu);
276 mm_iommu_do_free(mem);
279 static void mm_iommu_release(struct mm_iommu_table_group_mem_t *mem)
281 list_del_rcu(&mem->next);
282 call_rcu(&mem->rcu, mm_iommu_free);
285 long mm_iommu_put(struct mm_struct *mm, struct mm_iommu_table_group_mem_t *mem)
288 unsigned long unlock_entries = 0;
290 mutex_lock(&mem_list_mutex);
292 if (mem->used == 0) {
298 /* There are still users, exit */
302 /* Are there still mappings? */
303 if (atomic_cmpxchg(&mem->mapped, 1, 0) != 1) {
309 if (mem->dev_hpa == MM_IOMMU_TABLE_INVALID_HPA)
310 unlock_entries = mem->entries;
312 /* @mapped became 0 so now mappings are disabled, release the region */
313 mm_iommu_release(mem);
316 mutex_unlock(&mem_list_mutex);
318 mm_iommu_adjust_locked_vm(mm, unlock_entries, false);
322 EXPORT_SYMBOL_GPL(mm_iommu_put);
324 struct mm_iommu_table_group_mem_t *mm_iommu_lookup(struct mm_struct *mm,
325 unsigned long ua, unsigned long size)
327 struct mm_iommu_table_group_mem_t *mem, *ret = NULL;
329 list_for_each_entry_rcu(mem, &mm->context.iommu_group_mem_list, next) {
330 if ((mem->ua <= ua) &&
331 (ua + size <= mem->ua +
332 (mem->entries << PAGE_SHIFT))) {
340 EXPORT_SYMBOL_GPL(mm_iommu_lookup);
342 struct mm_iommu_table_group_mem_t *mm_iommu_lookup_rm(struct mm_struct *mm,
343 unsigned long ua, unsigned long size)
345 struct mm_iommu_table_group_mem_t *mem, *ret = NULL;
347 list_for_each_entry_lockless(mem, &mm->context.iommu_group_mem_list,
349 if ((mem->ua <= ua) &&
350 (ua + size <= mem->ua +
351 (mem->entries << PAGE_SHIFT))) {
360 struct mm_iommu_table_group_mem_t *mm_iommu_get(struct mm_struct *mm,
361 unsigned long ua, unsigned long entries)
363 struct mm_iommu_table_group_mem_t *mem, *ret = NULL;
365 mutex_lock(&mem_list_mutex);
367 list_for_each_entry_rcu(mem, &mm->context.iommu_group_mem_list, next) {
368 if ((mem->ua == ua) && (mem->entries == entries)) {
375 mutex_unlock(&mem_list_mutex);
379 EXPORT_SYMBOL_GPL(mm_iommu_get);
381 long mm_iommu_ua_to_hpa(struct mm_iommu_table_group_mem_t *mem,
382 unsigned long ua, unsigned int pageshift, unsigned long *hpa)
384 const long entry = (ua - mem->ua) >> PAGE_SHIFT;
387 if (entry >= mem->entries)
390 if (pageshift > mem->pageshift)
394 *hpa = mem->dev_hpa + (ua - mem->ua);
398 va = &mem->hpas[entry];
399 *hpa = (*va & MM_IOMMU_TABLE_GROUP_PAGE_MASK) | (ua & ~PAGE_MASK);
403 EXPORT_SYMBOL_GPL(mm_iommu_ua_to_hpa);
405 long mm_iommu_ua_to_hpa_rm(struct mm_iommu_table_group_mem_t *mem,
406 unsigned long ua, unsigned int pageshift, unsigned long *hpa)
408 const long entry = (ua - mem->ua) >> PAGE_SHIFT;
411 if (entry >= mem->entries)
414 if (pageshift > mem->pageshift)
418 *hpa = mem->dev_hpa + (ua - mem->ua);
422 pa = (void *) vmalloc_to_phys(&mem->hpas[entry]);
426 *hpa = (*pa & MM_IOMMU_TABLE_GROUP_PAGE_MASK) | (ua & ~PAGE_MASK);
431 extern void mm_iommu_ua_mark_dirty_rm(struct mm_struct *mm, unsigned long ua)
433 struct mm_iommu_table_group_mem_t *mem;
438 mem = mm_iommu_lookup_rm(mm, ua, PAGE_SIZE);
442 if (mem->dev_hpa != MM_IOMMU_TABLE_INVALID_HPA)
445 entry = (ua - mem->ua) >> PAGE_SHIFT;
446 va = &mem->hpas[entry];
448 pa = (void *) vmalloc_to_phys(va);
452 *pa |= MM_IOMMU_TABLE_GROUP_PAGE_DIRTY;
455 bool mm_iommu_is_devmem(struct mm_struct *mm, unsigned long hpa,
456 unsigned int pageshift, unsigned long *size)
458 struct mm_iommu_table_group_mem_t *mem;
461 list_for_each_entry_rcu(mem, &mm->context.iommu_group_mem_list, next) {
462 if (mem->dev_hpa == MM_IOMMU_TABLE_INVALID_HPA)
465 end = mem->dev_hpa + (mem->entries << PAGE_SHIFT);
466 if ((mem->dev_hpa <= hpa) && (hpa < end)) {
468 * Since the IOMMU page size might be bigger than
469 * PAGE_SIZE, the amount of preregistered memory
470 * starting from @hpa might be smaller than 1<<pageshift
471 * and the caller needs to distinguish this situation.
473 *size = min(1UL << pageshift, end - hpa);
480 EXPORT_SYMBOL_GPL(mm_iommu_is_devmem);
482 long mm_iommu_mapped_inc(struct mm_iommu_table_group_mem_t *mem)
484 if (atomic64_inc_not_zero(&mem->mapped))
487 /* Last mm_iommu_put() has been called, no more mappings allowed() */
490 EXPORT_SYMBOL_GPL(mm_iommu_mapped_inc);
492 void mm_iommu_mapped_dec(struct mm_iommu_table_group_mem_t *mem)
494 atomic64_add_unless(&mem->mapped, -1, 1);
496 EXPORT_SYMBOL_GPL(mm_iommu_mapped_dec);
498 void mm_iommu_init(struct mm_struct *mm)
500 INIT_LIST_HEAD_RCU(&mm->context.iommu_group_mem_list);