1e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi/* 2e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * Copyright (c) 2005 Topspin Communications. All rights reserved. 3e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * Copyright (c) 2005 Mellanox Technologies. All rights reserved. 4e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * Copyright (c) 2013 Cisco Systems. All rights reserved. 5e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * 6e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * This software is available to you under a choice of one of two 7e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * licenses. You may choose to be licensed under the terms of the GNU 8e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * General Public License (GPL) Version 2, available from the file 9e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * COPYING in the main directory of this source tree, or the 10e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * OpenIB.org BSD license below: 11e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * 12e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * Redistribution and use in source and binary forms, with or 13e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * without modification, are permitted provided that the following 14e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * conditions are met: 15e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * 16e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * - Redistributions of source code must retain the above 17e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * copyright notice, this list of conditions and the following 18e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * disclaimer. 19e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * 20e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * - Redistributions in binary form must reproduce the above 21e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * copyright notice, this list of conditions and the following 22e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * disclaimer in the documentation and/or other materials 23e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * provided with the distribution. 24e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * 25e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 26e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 29e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 30e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 31e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * SOFTWARE. 33e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi */ 34e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 35e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi#include <linux/mm.h> 36e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi#include <linux/dma-mapping.h> 37e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi#include <linux/sched.h> 38e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi#include <linux/hugetlb.h> 39e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi#include <linux/dma-attrs.h> 40e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi#include <linux/iommu.h> 41e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi#include <linux/workqueue.h> 42e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi#include <linux/list.h> 43e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi#include <linux/pci.h> 44e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 45e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi#include "usnic_log.h" 46e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi#include "usnic_uiom.h" 47e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi#include "usnic_uiom_interval_tree.h" 48e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 49e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhistatic struct workqueue_struct *usnic_uiom_wq; 50e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 51e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi#define USNIC_UIOM_PAGE_CHUNK \ 52e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi ((PAGE_SIZE - offsetof(struct usnic_uiom_chunk, page_list)) /\ 53e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi ((void *) &((struct usnic_uiom_chunk *) 0)->page_list[1] - \ 54e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi (void *) &((struct usnic_uiom_chunk *) 0)->page_list[0])) 55e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 56e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhistatic void usnic_uiom_reg_account(struct work_struct *work) 57e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi{ 58e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi struct usnic_uiom_reg *umem = container_of(work, 59e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi struct usnic_uiom_reg, work); 60e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 61e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi down_write(&umem->mm->mmap_sem); 62e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi umem->mm->locked_vm -= umem->diff; 63e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi up_write(&umem->mm->mmap_sem); 64e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi mmput(umem->mm); 65e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi kfree(umem); 66e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi} 67e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 68e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhistatic int usnic_uiom_dma_fault(struct iommu_domain *domain, 69e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi struct device *dev, 70e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi unsigned long iova, int flags, 71e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi void *token) 72e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi{ 73e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi usnic_err("Device %s iommu fault domain 0x%pK va 0x%lx flags 0x%x\n", 74e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi dev_name(dev), 75e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi domain, iova, flags); 76e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi return -ENOSYS; 77e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi} 78e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 79e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhistatic void usnic_uiom_put_pages(struct list_head *chunk_list, int dirty) 80e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi{ 81e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi struct usnic_uiom_chunk *chunk, *tmp; 82e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi struct page *page; 83c5f855e08a97edc107c4a3b73809ed629c1dcac1Upinder Malhi struct scatterlist *sg; 84e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi int i; 85e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi dma_addr_t pa; 86e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 87e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi list_for_each_entry_safe(chunk, tmp, chunk_list, list) { 88c5f855e08a97edc107c4a3b73809ed629c1dcac1Upinder Malhi for_each_sg(chunk->page_list, sg, chunk->nents, i) { 89c5f855e08a97edc107c4a3b73809ed629c1dcac1Upinder Malhi page = sg_page(sg); 90c5f855e08a97edc107c4a3b73809ed629c1dcac1Upinder Malhi pa = sg_phys(sg); 91e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (dirty) 92e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi set_page_dirty_lock(page); 93e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi put_page(page); 94e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi usnic_dbg("pa: %pa\n", &pa); 95e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 96e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi kfree(chunk); 97e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 98e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi} 99e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 100e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhistatic int usnic_uiom_get_pages(unsigned long addr, size_t size, int writable, 101e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi int dmasync, struct list_head *chunk_list) 102e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi{ 103e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi struct page **page_list; 104c5f855e08a97edc107c4a3b73809ed629c1dcac1Upinder Malhi struct scatterlist *sg; 105e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi struct usnic_uiom_chunk *chunk; 106e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi unsigned long locked; 107e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi unsigned long lock_limit; 108e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi unsigned long cur_base; 109e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi unsigned long npages; 110e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi int ret; 111e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi int off; 112e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi int i; 113e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi int flags; 114e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi dma_addr_t pa; 115e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi DEFINE_DMA_ATTRS(attrs); 116e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 117e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (dmasync) 118e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi dma_set_attr(DMA_ATTR_WRITE_BARRIER, &attrs); 119e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 120e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (!can_do_mlock()) 121e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi return -EPERM; 122e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 123e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi INIT_LIST_HEAD(chunk_list); 124e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 125e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi page_list = (struct page **) __get_free_page(GFP_KERNEL); 126e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (!page_list) 127e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi return -ENOMEM; 128e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 129e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi npages = PAGE_ALIGN(size + (addr & ~PAGE_MASK)) >> PAGE_SHIFT; 130e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 131e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi down_write(¤t->mm->mmap_sem); 132e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 133e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi locked = npages + current->mm->locked_vm; 134e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 135e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 136e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) { 137e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi ret = -ENOMEM; 138e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi goto out; 139e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 140e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 141e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi flags = IOMMU_READ | IOMMU_CACHE; 142e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi flags |= (writable) ? IOMMU_WRITE : 0; 143e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi cur_base = addr & PAGE_MASK; 144e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi ret = 0; 145e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 146e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi while (npages) { 147e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi ret = get_user_pages(current, current->mm, cur_base, 148e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi min_t(unsigned long, npages, 149e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi PAGE_SIZE / sizeof(struct page *)), 150e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 1, !writable, page_list, NULL); 151e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 152e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (ret < 0) 153e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi goto out; 154e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 155e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi npages -= ret; 156e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi off = 0; 157e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 158e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi while (ret) { 159e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi chunk = kmalloc(sizeof(*chunk) + 160e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi sizeof(struct scatterlist) * 161e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi min_t(int, ret, USNIC_UIOM_PAGE_CHUNK), 162e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi GFP_KERNEL); 163e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (!chunk) { 164e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi ret = -ENOMEM; 165e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi goto out; 166e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 167e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 168e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi chunk->nents = min_t(int, ret, USNIC_UIOM_PAGE_CHUNK); 169e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi sg_init_table(chunk->page_list, chunk->nents); 170c5f855e08a97edc107c4a3b73809ed629c1dcac1Upinder Malhi for_each_sg(chunk->page_list, sg, chunk->nents, i) { 171c5f855e08a97edc107c4a3b73809ed629c1dcac1Upinder Malhi sg_set_page(sg, page_list[i + off], 172c5f855e08a97edc107c4a3b73809ed629c1dcac1Upinder Malhi PAGE_SIZE, 0); 173c5f855e08a97edc107c4a3b73809ed629c1dcac1Upinder Malhi pa = sg_phys(sg); 174e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi usnic_dbg("va: 0x%lx pa: %pa\n", 175e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi cur_base + i*PAGE_SIZE, &pa); 176e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 177e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi cur_base += chunk->nents * PAGE_SIZE; 178e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi ret -= chunk->nents; 179e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi off += chunk->nents; 180e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi list_add_tail(&chunk->list, chunk_list); 181e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 182e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 183e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi ret = 0; 184e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 185e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 186e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhiout: 187e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (ret < 0) 188e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi usnic_uiom_put_pages(chunk_list, 0); 189e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi else 190e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi current->mm->locked_vm = locked; 191e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 192e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi up_write(¤t->mm->mmap_sem); 193e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi free_page((unsigned long) page_list); 194e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi return ret; 195e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi} 196e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 197e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhistatic void usnic_uiom_unmap_sorted_intervals(struct list_head *intervals, 198e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi struct usnic_uiom_pd *pd) 199e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi{ 200e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi struct usnic_uiom_interval_node *interval, *tmp; 201e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi long unsigned va, size; 202e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 203e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi list_for_each_entry_safe(interval, tmp, intervals, link) { 204e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi va = interval->start << PAGE_SHIFT; 205e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi size = ((interval->last - interval->start) + 1) << PAGE_SHIFT; 206e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi while (size > 0) { 207e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi /* Workaround for RH 970401 */ 208e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi usnic_dbg("va 0x%lx size 0x%lx", va, PAGE_SIZE); 209e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi iommu_unmap(pd->domain, va, PAGE_SIZE); 210e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi va += PAGE_SIZE; 211e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi size -= PAGE_SIZE; 212e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 213e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 214e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi} 215e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 216e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhistatic void __usnic_uiom_reg_release(struct usnic_uiom_pd *pd, 217e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi struct usnic_uiom_reg *uiomr, 218e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi int dirty) 219e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi{ 220e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi int npages; 221e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi unsigned long vpn_start, vpn_last; 222e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi struct usnic_uiom_interval_node *interval, *tmp; 223e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi int writable = 0; 224e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi LIST_HEAD(rm_intervals); 225e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 226e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi npages = PAGE_ALIGN(uiomr->length + uiomr->offset) >> PAGE_SHIFT; 227e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi vpn_start = (uiomr->va & PAGE_MASK) >> PAGE_SHIFT; 228e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi vpn_last = vpn_start + npages - 1; 229e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 230e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi spin_lock(&pd->lock); 231e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi usnic_uiom_remove_interval(&pd->rb_root, vpn_start, 232e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi vpn_last, &rm_intervals); 233e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi usnic_uiom_unmap_sorted_intervals(&rm_intervals, pd); 234e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 235e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi list_for_each_entry_safe(interval, tmp, &rm_intervals, link) { 236e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (interval->flags & IOMMU_WRITE) 237e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi writable = 1; 238e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi list_del(&interval->link); 239e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi kfree(interval); 240e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 241e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 242e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi usnic_uiom_put_pages(&uiomr->chunk_list, dirty & writable); 243e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi spin_unlock(&pd->lock); 244e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi} 245e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 246e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhistatic int usnic_uiom_map_sorted_intervals(struct list_head *intervals, 247e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi struct usnic_uiom_reg *uiomr) 248e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi{ 249e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi int i, err; 250e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi size_t size; 251e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi struct usnic_uiom_chunk *chunk; 252e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi struct usnic_uiom_interval_node *interval_node; 253e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi dma_addr_t pa; 254e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi dma_addr_t pa_start = 0; 255e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi dma_addr_t pa_end = 0; 256e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi long int va_start = -EINVAL; 257e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi struct usnic_uiom_pd *pd = uiomr->pd; 258e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi long int va = uiomr->va & PAGE_MASK; 259e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi int flags = IOMMU_READ | IOMMU_CACHE; 260e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 261e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi flags |= (uiomr->writable) ? IOMMU_WRITE : 0; 262e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi chunk = list_first_entry(&uiomr->chunk_list, struct usnic_uiom_chunk, 263e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi list); 264e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi list_for_each_entry(interval_node, intervals, link) { 265e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhiiter_chunk: 266e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi for (i = 0; i < chunk->nents; i++, va += PAGE_SIZE) { 267e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi pa = sg_phys(&chunk->page_list[i]); 268e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if ((va >> PAGE_SHIFT) < interval_node->start) 269e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi continue; 270e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 271e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if ((va >> PAGE_SHIFT) == interval_node->start) { 272e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi /* First page of the interval */ 273e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi va_start = va; 274e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi pa_start = pa; 275e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi pa_end = pa; 276e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 277e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 278e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi WARN_ON(va_start == -EINVAL); 279e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 280e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if ((pa_end + PAGE_SIZE != pa) && 281e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi (pa != pa_start)) { 282e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi /* PAs are not contiguous */ 283e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi size = pa_end - pa_start + PAGE_SIZE; 284e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi usnic_dbg("va 0x%lx pa %pa size 0x%zx flags 0x%x", 285e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi va_start, &pa_start, size, flags); 286e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi err = iommu_map(pd->domain, va_start, pa_start, 287e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi size, flags); 288e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (err) { 289970918b32b030e9b3966e5ccb5f4a5a5b515a5b1Fabio Estevam usnic_err("Failed to map va 0x%lx pa %pa size 0x%zx with err %d\n", 290e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi va_start, &pa_start, size, err); 291e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi goto err_out; 292e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 293e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi va_start = va; 294e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi pa_start = pa; 295e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi pa_end = pa; 296e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 297e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 298e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if ((va >> PAGE_SHIFT) == interval_node->last) { 299e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi /* Last page of the interval */ 300e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi size = pa - pa_start + PAGE_SIZE; 301e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi usnic_dbg("va 0x%lx pa %pa size 0x%zx flags 0x%x\n", 302e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi va_start, &pa_start, size, flags); 303e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi err = iommu_map(pd->domain, va_start, pa_start, 304e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi size, flags); 305e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (err) { 306e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi usnic_err("Failed to map va 0x%lx pa %pa size 0x%zx with err %d\n", 307e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi va_start, &pa_start, size, err); 308e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi goto err_out; 309e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 310e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi break; 311e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 312e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 313e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (pa != pa_start) 314e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi pa_end += PAGE_SIZE; 315e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 316e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 317e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (i == chunk->nents) { 318e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi /* 319e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * Hit last entry of the chunk, 320e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * hence advance to next chunk 321e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi */ 322e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi chunk = list_first_entry(&chunk->list, 323e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi struct usnic_uiom_chunk, 324e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi list); 325e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi goto iter_chunk; 326e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 327e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 328e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 329e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi return 0; 330e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 331e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhierr_out: 332e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi usnic_uiom_unmap_sorted_intervals(intervals, pd); 333e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi return err; 334e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi} 335e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 336e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhistruct usnic_uiom_reg *usnic_uiom_reg_get(struct usnic_uiom_pd *pd, 337e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi unsigned long addr, size_t size, 338e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi int writable, int dmasync) 339e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi{ 340e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi struct usnic_uiom_reg *uiomr; 341e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi unsigned long va_base, vpn_start, vpn_last; 342e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi unsigned long npages; 343e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi int offset, err; 344e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi LIST_HEAD(sorted_diff_intervals); 345e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 346e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi /* 347e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * Intel IOMMU map throws an error if a translation entry is 348e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * changed from read to write. This module may not unmap 349e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * and then remap the entry after fixing the permission 350e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * b/c this open up a small windows where hw DMA may page fault 351e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * Hence, make all entries to be writable. 352e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi */ 353e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi writable = 1; 354e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 355e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi va_base = addr & PAGE_MASK; 356e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi offset = addr & ~PAGE_MASK; 357e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi npages = PAGE_ALIGN(size + offset) >> PAGE_SHIFT; 358e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi vpn_start = (addr & PAGE_MASK) >> PAGE_SHIFT; 359e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi vpn_last = vpn_start + npages - 1; 360e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 361e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi uiomr = kmalloc(sizeof(*uiomr), GFP_KERNEL); 362e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (!uiomr) 363e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi return ERR_PTR(-ENOMEM); 364e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 365e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi uiomr->va = va_base; 366e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi uiomr->offset = offset; 367e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi uiomr->length = size; 368e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi uiomr->writable = writable; 369e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi uiomr->pd = pd; 370e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 371e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi err = usnic_uiom_get_pages(addr, size, writable, dmasync, 372e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi &uiomr->chunk_list); 373e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (err) { 374e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi usnic_err("Failed get_pages vpn [0x%lx,0x%lx] err %d\n", 375e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi vpn_start, vpn_last, err); 376e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi goto out_free_uiomr; 377e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 378e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 379e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi spin_lock(&pd->lock); 380e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi err = usnic_uiom_get_intervals_diff(vpn_start, vpn_last, 381e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi (writable) ? IOMMU_WRITE : 0, 382e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi IOMMU_WRITE, 383e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi &pd->rb_root, 384e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi &sorted_diff_intervals); 385e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (err) { 386e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi usnic_err("Failed disjoint interval vpn [0x%lx,0x%lx] err %d\n", 387e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi vpn_start, vpn_last, err); 388e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi goto out_put_pages; 389e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 390e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 391e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi err = usnic_uiom_map_sorted_intervals(&sorted_diff_intervals, uiomr); 392e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (err) { 393e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi usnic_err("Failed map interval vpn [0x%lx,0x%lx] err %d\n", 394e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi vpn_start, vpn_last, err); 395e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi goto out_put_intervals; 396e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 397e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 398e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 399e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi err = usnic_uiom_insert_interval(&pd->rb_root, vpn_start, vpn_last, 400e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi (writable) ? IOMMU_WRITE : 0); 401e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (err) { 402e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi usnic_err("Failed insert interval vpn [0x%lx,0x%lx] err %d\n", 403e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi vpn_start, vpn_last, err); 404e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi goto out_unmap_intervals; 405e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 406e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 407e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi usnic_uiom_put_interval_set(&sorted_diff_intervals); 408e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi spin_unlock(&pd->lock); 409e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 410e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi return uiomr; 411e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 412e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhiout_unmap_intervals: 413e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi usnic_uiom_unmap_sorted_intervals(&sorted_diff_intervals, pd); 414e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhiout_put_intervals: 415e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi usnic_uiom_put_interval_set(&sorted_diff_intervals); 416e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhiout_put_pages: 417e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi usnic_uiom_put_pages(&uiomr->chunk_list, 0); 418e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi spin_unlock(&pd->lock); 419e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhiout_free_uiomr: 420e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi kfree(uiomr); 421e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi return ERR_PTR(err); 422e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi} 423e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 424e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhivoid usnic_uiom_reg_release(struct usnic_uiom_reg *uiomr, int closing) 425e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi{ 426e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi struct mm_struct *mm; 427e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi unsigned long diff; 428e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 429e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi __usnic_uiom_reg_release(uiomr->pd, uiomr, 1); 430e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 431e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi mm = get_task_mm(current); 432e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (!mm) { 433e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi kfree(uiomr); 434e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi return; 435e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 436e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 437e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi diff = PAGE_ALIGN(uiomr->length + uiomr->offset) >> PAGE_SHIFT; 438e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 439e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi /* 440e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * We may be called with the mm's mmap_sem already held. This 441e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * can happen when a userspace munmap() is the call that drops 442e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * the last reference to our file and calls our release 443e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * method. If there are memory regions to destroy, we'll end 444e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * up here and not be able to take the mmap_sem. In that case 445e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi * we defer the vm_locked accounting to the system workqueue. 446e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi */ 447e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (closing) { 448e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (!down_write_trylock(&mm->mmap_sem)) { 449e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi INIT_WORK(&uiomr->work, usnic_uiom_reg_account); 450e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi uiomr->mm = mm; 451e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi uiomr->diff = diff; 452e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 453e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi queue_work(usnic_uiom_wq, &uiomr->work); 454e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi return; 455e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 456e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } else 457e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi down_write(&mm->mmap_sem); 458e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 459e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi current->mm->locked_vm -= diff; 460e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi up_write(&mm->mmap_sem); 461e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi mmput(mm); 462e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi kfree(uiomr); 463e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi} 464e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 465e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhistruct usnic_uiom_pd *usnic_uiom_alloc_pd(void) 466e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi{ 467e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi struct usnic_uiom_pd *pd; 468e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi void *domain; 469e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 470e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi pd = kzalloc(sizeof(*pd), GFP_KERNEL); 471e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (!pd) 472e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi return ERR_PTR(-ENOMEM); 473e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 474e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi pd->domain = domain = iommu_domain_alloc(&pci_bus_type); 475e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (IS_ERR_OR_NULL(domain)) { 476e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi usnic_err("Failed to allocate IOMMU domain with err %ld\n", 477e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi PTR_ERR(pd->domain)); 478e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi kfree(pd); 479e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi return ERR_PTR(domain ? PTR_ERR(domain) : -ENOMEM); 480e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 481e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 482e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi iommu_set_fault_handler(pd->domain, usnic_uiom_dma_fault, NULL); 483e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 484e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi spin_lock_init(&pd->lock); 485e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi INIT_LIST_HEAD(&pd->devs); 486e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 487e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi return pd; 488e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi} 489e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 490e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhivoid usnic_uiom_dealloc_pd(struct usnic_uiom_pd *pd) 491e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi{ 492e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi iommu_domain_free(pd->domain); 493e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi kfree(pd); 494e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi} 495e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 496e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhiint usnic_uiom_attach_dev_to_pd(struct usnic_uiom_pd *pd, struct device *dev) 497e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi{ 498e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi struct usnic_uiom_dev *uiom_dev; 499e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi int err; 500e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 5018ce96afa8239f13bdf5ab35839bac46c103bbedcDan Carpenter uiom_dev = kzalloc(sizeof(*uiom_dev), GFP_ATOMIC); 502e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (!uiom_dev) 503e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi return -ENOMEM; 504e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi uiom_dev->dev = dev; 505e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 506e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi err = iommu_attach_device(pd->domain, dev); 507e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (err) 508e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi goto out_free_dev; 509e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 5106f952710917cfb801664333a491e4a120451a9ffJoerg Roedel if (!iommu_capable(dev->bus, IOMMU_CAP_CACHE_COHERENCY)) { 511e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi usnic_err("IOMMU of %s does not support cache coherency\n", 512e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi dev_name(dev)); 513e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi err = -EINVAL; 514e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi goto out_detach_device; 515e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 516e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 517e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi spin_lock(&pd->lock); 518e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi list_add_tail(&uiom_dev->link, &pd->devs); 519e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi pd->dev_cnt++; 520e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi spin_unlock(&pd->lock); 521e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 522e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi return 0; 523e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 524e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhiout_detach_device: 525e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi iommu_detach_device(pd->domain, dev); 526e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhiout_free_dev: 527e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi kfree(uiom_dev); 528e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi return err; 529e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi} 530e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 531e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhivoid usnic_uiom_detach_dev_from_pd(struct usnic_uiom_pd *pd, struct device *dev) 532e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi{ 533e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi struct usnic_uiom_dev *uiom_dev; 534e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi int found = 0; 535e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 536e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi spin_lock(&pd->lock); 537e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi list_for_each_entry(uiom_dev, &pd->devs, link) { 538e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (uiom_dev->dev == dev) { 539e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi found = 1; 540e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi break; 541e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 542e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 543e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 544e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (!found) { 545e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi usnic_err("Unable to free dev %s - not found\n", 546e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi dev_name(dev)); 547e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi spin_unlock(&pd->lock); 548e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi return; 549e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 550e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 551e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi list_del(&uiom_dev->link); 552e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi pd->dev_cnt--; 553e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi spin_unlock(&pd->lock); 554e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 555e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi return iommu_detach_device(pd->domain, dev); 556e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi} 557e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 558e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhistruct device **usnic_uiom_get_dev_list(struct usnic_uiom_pd *pd) 559e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi{ 560e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi struct usnic_uiom_dev *uiom_dev; 561e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi struct device **devs; 562e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi int i = 0; 563e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 564e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi spin_lock(&pd->lock); 565e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi devs = kcalloc(pd->dev_cnt + 1, sizeof(*devs), GFP_ATOMIC); 566e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (!devs) { 567e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi devs = ERR_PTR(-ENOMEM); 568e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi goto out; 569e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 570e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 571e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi list_for_each_entry(uiom_dev, &pd->devs, link) { 572e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi devs[i++] = uiom_dev->dev; 573e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 574e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhiout: 575e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi spin_unlock(&pd->lock); 576e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi return devs; 577e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi} 578e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 579e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhivoid usnic_uiom_free_dev_list(struct device **devs) 580e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi{ 581e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi kfree(devs); 582e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi} 583e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 584e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhiint usnic_uiom_init(char *drv_name) 585e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi{ 586e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (!iommu_present(&pci_bus_type)) { 587e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi usnic_err("IOMMU required but not present or enabled. USNIC QPs will not function w/o enabling IOMMU\n"); 588e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi return -EPERM; 589e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 590e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 591e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi usnic_uiom_wq = create_workqueue(drv_name); 592e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi if (!usnic_uiom_wq) { 593e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi usnic_err("Unable to alloc wq for drv %s\n", drv_name); 594e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi return -ENOMEM; 595e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi } 596e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 597e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi return 0; 598e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi} 599e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi 600e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhivoid usnic_uiom_fini(void) 601e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi{ 602e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi flush_workqueue(usnic_uiom_wq); 603e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi destroy_workqueue(usnic_uiom_wq); 604e3cf00d0a87f025db5855a43a67c67a41fa79fefUpinder Malhi} 605