2 * Copyright (c) 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
4 * Copyright (c) 2013 Cisco Systems. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 #include <linux/dma-mapping.h>
37 #include <linux/sched.h>
38 #include <linux/sched/mm.h>
39 #include <linux/hugetlb.h>
40 #include <linux/iommu.h>
41 #include <linux/workqueue.h>
42 #include <linux/list.h>
43 #include <linux/pci.h>
45 #include "usnic_log.h"
46 #include "usnic_uiom.h"
47 #include "usnic_uiom_interval_tree.h"
49 static struct workqueue_struct *usnic_uiom_wq;
51 #define USNIC_UIOM_PAGE_CHUNK \
52 ((PAGE_SIZE - offsetof(struct usnic_uiom_chunk, page_list)) /\
53 ((void *) &((struct usnic_uiom_chunk *) 0)->page_list[1] - \
54 (void *) &((struct usnic_uiom_chunk *) 0)->page_list[0]))
56 static void usnic_uiom_reg_account(struct work_struct *work)
58 struct usnic_uiom_reg *umem = container_of(work,
59 struct usnic_uiom_reg, work);
61 down_write(&umem->mm->mmap_sem);
62 umem->mm->locked_vm -= umem->diff;
63 up_write(&umem->mm->mmap_sem);
68 static int usnic_uiom_dma_fault(struct iommu_domain *domain,
70 unsigned long iova, int flags,
73 usnic_err("Device %s iommu fault domain 0x%pK va 0x%lx flags 0x%x\n",
79 static void usnic_uiom_put_pages(struct list_head *chunk_list, int dirty)
81 struct usnic_uiom_chunk *chunk, *tmp;
83 struct scatterlist *sg;
87 list_for_each_entry_safe(chunk, tmp, chunk_list, list) {
88 for_each_sg(chunk->page_list, sg, chunk->nents, i) {
92 set_page_dirty_lock(page);
94 usnic_dbg("pa: %pa\n", &pa);
100 static int usnic_uiom_get_pages(unsigned long addr, size_t size, int writable,
101 int dmasync, struct list_head *chunk_list)
103 struct page **page_list;
104 struct scatterlist *sg;
105 struct usnic_uiom_chunk *chunk;
106 unsigned long locked;
107 unsigned long lock_limit;
108 unsigned long cur_base;
109 unsigned long npages;
115 unsigned int gup_flags;
120 INIT_LIST_HEAD(chunk_list);
122 page_list = (struct page **) __get_free_page(GFP_KERNEL);
126 npages = PAGE_ALIGN(size + (addr & ~PAGE_MASK)) >> PAGE_SHIFT;
128 down_write(¤t->mm->mmap_sem);
130 locked = npages + current->mm->locked_vm;
131 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
133 if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) {
138 flags = IOMMU_READ | IOMMU_CACHE;
139 flags |= (writable) ? IOMMU_WRITE : 0;
140 gup_flags = FOLL_WRITE;
141 gup_flags |= (writable) ? 0 : FOLL_FORCE;
142 cur_base = addr & PAGE_MASK;
146 ret = get_user_pages(cur_base,
147 min_t(unsigned long, npages,
148 PAGE_SIZE / sizeof(struct page *)),
149 gup_flags, page_list, NULL);
158 chunk = kmalloc(sizeof(*chunk) +
159 sizeof(struct scatterlist) *
160 min_t(int, ret, USNIC_UIOM_PAGE_CHUNK),
167 chunk->nents = min_t(int, ret, USNIC_UIOM_PAGE_CHUNK);
168 sg_init_table(chunk->page_list, chunk->nents);
169 for_each_sg(chunk->page_list, sg, chunk->nents, i) {
170 sg_set_page(sg, page_list[i + off],
173 usnic_dbg("va: 0x%lx pa: %pa\n",
174 cur_base + i*PAGE_SIZE, &pa);
176 cur_base += chunk->nents * PAGE_SIZE;
179 list_add_tail(&chunk->list, chunk_list);
187 usnic_uiom_put_pages(chunk_list, 0);
189 current->mm->locked_vm = locked;
191 up_write(¤t->mm->mmap_sem);
192 free_page((unsigned long) page_list);
196 static void usnic_uiom_unmap_sorted_intervals(struct list_head *intervals,
197 struct usnic_uiom_pd *pd)
199 struct usnic_uiom_interval_node *interval, *tmp;
200 long unsigned va, size;
202 list_for_each_entry_safe(interval, tmp, intervals, link) {
203 va = interval->start << PAGE_SHIFT;
204 size = ((interval->last - interval->start) + 1) << PAGE_SHIFT;
206 /* Workaround for RH 970401 */
207 usnic_dbg("va 0x%lx size 0x%lx", va, PAGE_SIZE);
208 iommu_unmap(pd->domain, va, PAGE_SIZE);
215 static void __usnic_uiom_reg_release(struct usnic_uiom_pd *pd,
216 struct usnic_uiom_reg *uiomr,
220 unsigned long vpn_start, vpn_last;
221 struct usnic_uiom_interval_node *interval, *tmp;
223 LIST_HEAD(rm_intervals);
225 npages = PAGE_ALIGN(uiomr->length + uiomr->offset) >> PAGE_SHIFT;
226 vpn_start = (uiomr->va & PAGE_MASK) >> PAGE_SHIFT;
227 vpn_last = vpn_start + npages - 1;
229 spin_lock(&pd->lock);
230 usnic_uiom_remove_interval(&pd->rb_root, vpn_start,
231 vpn_last, &rm_intervals);
232 usnic_uiom_unmap_sorted_intervals(&rm_intervals, pd);
234 list_for_each_entry_safe(interval, tmp, &rm_intervals, link) {
235 if (interval->flags & IOMMU_WRITE)
237 list_del(&interval->link);
241 usnic_uiom_put_pages(&uiomr->chunk_list, dirty & writable);
242 spin_unlock(&pd->lock);
245 static int usnic_uiom_map_sorted_intervals(struct list_head *intervals,
246 struct usnic_uiom_reg *uiomr)
250 struct usnic_uiom_chunk *chunk;
251 struct usnic_uiom_interval_node *interval_node;
253 dma_addr_t pa_start = 0;
254 dma_addr_t pa_end = 0;
255 long int va_start = -EINVAL;
256 struct usnic_uiom_pd *pd = uiomr->pd;
257 long int va = uiomr->va & PAGE_MASK;
258 int flags = IOMMU_READ | IOMMU_CACHE;
260 flags |= (uiomr->writable) ? IOMMU_WRITE : 0;
261 chunk = list_first_entry(&uiomr->chunk_list, struct usnic_uiom_chunk,
263 list_for_each_entry(interval_node, intervals, link) {
265 for (i = 0; i < chunk->nents; i++, va += PAGE_SIZE) {
266 pa = sg_phys(&chunk->page_list[i]);
267 if ((va >> PAGE_SHIFT) < interval_node->start)
270 if ((va >> PAGE_SHIFT) == interval_node->start) {
271 /* First page of the interval */
277 WARN_ON(va_start == -EINVAL);
279 if ((pa_end + PAGE_SIZE != pa) &&
281 /* PAs are not contiguous */
282 size = pa_end - pa_start + PAGE_SIZE;
283 usnic_dbg("va 0x%lx pa %pa size 0x%zx flags 0x%x",
284 va_start, &pa_start, size, flags);
285 err = iommu_map(pd->domain, va_start, pa_start,
288 usnic_err("Failed to map va 0x%lx pa %pa size 0x%zx with err %d\n",
289 va_start, &pa_start, size, err);
297 if ((va >> PAGE_SHIFT) == interval_node->last) {
298 /* Last page of the interval */
299 size = pa - pa_start + PAGE_SIZE;
300 usnic_dbg("va 0x%lx pa %pa size 0x%zx flags 0x%x\n",
301 va_start, &pa_start, size, flags);
302 err = iommu_map(pd->domain, va_start, pa_start,
305 usnic_err("Failed to map va 0x%lx pa %pa size 0x%zx with err %d\n",
306 va_start, &pa_start, size, err);
316 if (i == chunk->nents) {
318 * Hit last entry of the chunk,
319 * hence advance to next chunk
321 chunk = list_first_entry(&chunk->list,
322 struct usnic_uiom_chunk,
331 usnic_uiom_unmap_sorted_intervals(intervals, pd);
335 struct usnic_uiom_reg *usnic_uiom_reg_get(struct usnic_uiom_pd *pd,
336 unsigned long addr, size_t size,
337 int writable, int dmasync)
339 struct usnic_uiom_reg *uiomr;
340 unsigned long va_base, vpn_start, vpn_last;
341 unsigned long npages;
343 LIST_HEAD(sorted_diff_intervals);
346 * Intel IOMMU map throws an error if a translation entry is
347 * changed from read to write. This module may not unmap
348 * and then remap the entry after fixing the permission
349 * b/c this open up a small windows where hw DMA may page fault
350 * Hence, make all entries to be writable.
354 va_base = addr & PAGE_MASK;
355 offset = addr & ~PAGE_MASK;
356 npages = PAGE_ALIGN(size + offset) >> PAGE_SHIFT;
357 vpn_start = (addr & PAGE_MASK) >> PAGE_SHIFT;
358 vpn_last = vpn_start + npages - 1;
360 uiomr = kmalloc(sizeof(*uiomr), GFP_KERNEL);
362 return ERR_PTR(-ENOMEM);
365 uiomr->offset = offset;
366 uiomr->length = size;
367 uiomr->writable = writable;
370 err = usnic_uiom_get_pages(addr, size, writable, dmasync,
373 usnic_err("Failed get_pages vpn [0x%lx,0x%lx] err %d\n",
374 vpn_start, vpn_last, err);
378 spin_lock(&pd->lock);
379 err = usnic_uiom_get_intervals_diff(vpn_start, vpn_last,
380 (writable) ? IOMMU_WRITE : 0,
383 &sorted_diff_intervals);
385 usnic_err("Failed disjoint interval vpn [0x%lx,0x%lx] err %d\n",
386 vpn_start, vpn_last, err);
390 err = usnic_uiom_map_sorted_intervals(&sorted_diff_intervals, uiomr);
392 usnic_err("Failed map interval vpn [0x%lx,0x%lx] err %d\n",
393 vpn_start, vpn_last, err);
394 goto out_put_intervals;
398 err = usnic_uiom_insert_interval(&pd->rb_root, vpn_start, vpn_last,
399 (writable) ? IOMMU_WRITE : 0);
401 usnic_err("Failed insert interval vpn [0x%lx,0x%lx] err %d\n",
402 vpn_start, vpn_last, err);
403 goto out_unmap_intervals;
406 usnic_uiom_put_interval_set(&sorted_diff_intervals);
407 spin_unlock(&pd->lock);
412 usnic_uiom_unmap_sorted_intervals(&sorted_diff_intervals, pd);
414 usnic_uiom_put_interval_set(&sorted_diff_intervals);
416 usnic_uiom_put_pages(&uiomr->chunk_list, 0);
417 spin_unlock(&pd->lock);
423 void usnic_uiom_reg_release(struct usnic_uiom_reg *uiomr, int closing)
425 struct mm_struct *mm;
428 __usnic_uiom_reg_release(uiomr->pd, uiomr, 1);
430 mm = get_task_mm(current);
436 diff = PAGE_ALIGN(uiomr->length + uiomr->offset) >> PAGE_SHIFT;
439 * We may be called with the mm's mmap_sem already held. This
440 * can happen when a userspace munmap() is the call that drops
441 * the last reference to our file and calls our release
442 * method. If there are memory regions to destroy, we'll end
443 * up here and not be able to take the mmap_sem. In that case
444 * we defer the vm_locked accounting to the system workqueue.
447 if (!down_write_trylock(&mm->mmap_sem)) {
448 INIT_WORK(&uiomr->work, usnic_uiom_reg_account);
452 queue_work(usnic_uiom_wq, &uiomr->work);
456 down_write(&mm->mmap_sem);
458 current->mm->locked_vm -= diff;
459 up_write(&mm->mmap_sem);
464 struct usnic_uiom_pd *usnic_uiom_alloc_pd(void)
466 struct usnic_uiom_pd *pd;
469 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
471 return ERR_PTR(-ENOMEM);
473 pd->domain = domain = iommu_domain_alloc(&pci_bus_type);
475 usnic_err("Failed to allocate IOMMU domain");
477 return ERR_PTR(-ENOMEM);
480 iommu_set_fault_handler(pd->domain, usnic_uiom_dma_fault, NULL);
482 spin_lock_init(&pd->lock);
483 INIT_LIST_HEAD(&pd->devs);
488 void usnic_uiom_dealloc_pd(struct usnic_uiom_pd *pd)
490 iommu_domain_free(pd->domain);
494 int usnic_uiom_attach_dev_to_pd(struct usnic_uiom_pd *pd, struct device *dev)
496 struct usnic_uiom_dev *uiom_dev;
499 uiom_dev = kzalloc(sizeof(*uiom_dev), GFP_ATOMIC);
504 err = iommu_attach_device(pd->domain, dev);
508 if (!iommu_capable(dev->bus, IOMMU_CAP_CACHE_COHERENCY)) {
509 usnic_err("IOMMU of %s does not support cache coherency\n",
512 goto out_detach_device;
515 spin_lock(&pd->lock);
516 list_add_tail(&uiom_dev->link, &pd->devs);
518 spin_unlock(&pd->lock);
523 iommu_detach_device(pd->domain, dev);
529 void usnic_uiom_detach_dev_from_pd(struct usnic_uiom_pd *pd, struct device *dev)
531 struct usnic_uiom_dev *uiom_dev;
534 spin_lock(&pd->lock);
535 list_for_each_entry(uiom_dev, &pd->devs, link) {
536 if (uiom_dev->dev == dev) {
543 usnic_err("Unable to free dev %s - not found\n",
545 spin_unlock(&pd->lock);
549 list_del(&uiom_dev->link);
551 spin_unlock(&pd->lock);
553 return iommu_detach_device(pd->domain, dev);
556 struct device **usnic_uiom_get_dev_list(struct usnic_uiom_pd *pd)
558 struct usnic_uiom_dev *uiom_dev;
559 struct device **devs;
562 spin_lock(&pd->lock);
563 devs = kcalloc(pd->dev_cnt + 1, sizeof(*devs), GFP_ATOMIC);
565 devs = ERR_PTR(-ENOMEM);
569 list_for_each_entry(uiom_dev, &pd->devs, link) {
570 devs[i++] = uiom_dev->dev;
573 spin_unlock(&pd->lock);
577 void usnic_uiom_free_dev_list(struct device **devs)
582 int usnic_uiom_init(char *drv_name)
584 if (!iommu_present(&pci_bus_type)) {
585 usnic_err("IOMMU required but not present or enabled. USNIC QPs will not function w/o enabling IOMMU\n");
589 usnic_uiom_wq = create_workqueue(drv_name);
590 if (!usnic_uiom_wq) {
591 usnic_err("Unable to alloc wq for drv %s\n", drv_name);
598 void usnic_uiom_fini(void)
600 flush_workqueue(usnic_uiom_wq);
601 destroy_workqueue(usnic_uiom_wq);