Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007-2008 Advanced Micro Devices, Inc. |
| 3 | * Author: Joerg Roedel <joerg.roedel@amd.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 as published |
| 7 | * by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | #include <linux/bug.h> |
| 20 | #include <linux/types.h> |
Andrew Morton | 60db402 | 2009-05-06 16:03:07 -0700 | [diff] [blame] | 21 | #include <linux/module.h> |
| 22 | #include <linux/slab.h> |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 23 | #include <linux/errno.h> |
| 24 | #include <linux/iommu.h> |
Stepan Moskovchenko | 0b1345e | 2011-08-11 19:32:27 -0700 | [diff] [blame] | 25 | #include <linux/scatterlist.h> |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 26 | |
| 27 | static struct iommu_ops *iommu_ops; |
| 28 | |
| 29 | void register_iommu(struct iommu_ops *ops) |
| 30 | { |
| 31 | if (iommu_ops) |
| 32 | BUG(); |
| 33 | |
| 34 | iommu_ops = ops; |
| 35 | } |
| 36 | |
Hannes Eder | ff2c8a4 | 2009-03-05 12:12:44 +0100 | [diff] [blame] | 37 | bool iommu_found(void) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 38 | { |
| 39 | return iommu_ops != NULL; |
| 40 | } |
| 41 | EXPORT_SYMBOL_GPL(iommu_found); |
| 42 | |
Ohad Ben-Cohen | a1c6df3 | 2011-09-13 15:25:23 -0400 | [diff] [blame^] | 43 | /** |
| 44 | * iommu_set_fault_handler() - set a fault handler for an iommu domain |
| 45 | * @domain: iommu domain |
| 46 | * @handler: fault handler |
| 47 | */ |
| 48 | void iommu_set_fault_handler(struct iommu_domain *domain, |
| 49 | iommu_fault_handler_t handler) |
| 50 | { |
| 51 | BUG_ON(!domain); |
| 52 | |
| 53 | domain->handler = handler; |
| 54 | } |
| 55 | |
Stepan Moskovchenko | ff2d366 | 2011-08-31 17:13:32 -0700 | [diff] [blame] | 56 | struct iommu_domain *iommu_domain_alloc(int flags) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 57 | { |
| 58 | struct iommu_domain *domain; |
| 59 | int ret; |
| 60 | |
Stepan Moskovchenko | 452926c | 2011-10-31 16:00:47 -0700 | [diff] [blame] | 61 | if (!iommu_found()) |
| 62 | return NULL; |
| 63 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 64 | domain = kmalloc(sizeof(*domain), GFP_KERNEL); |
| 65 | if (!domain) |
| 66 | return NULL; |
| 67 | |
Stepan Moskovchenko | ff2d366 | 2011-08-31 17:13:32 -0700 | [diff] [blame] | 68 | ret = iommu_ops->domain_init(domain, flags); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 69 | if (ret) |
| 70 | goto out_free; |
| 71 | |
| 72 | return domain; |
| 73 | |
| 74 | out_free: |
| 75 | kfree(domain); |
| 76 | |
| 77 | return NULL; |
| 78 | } |
| 79 | EXPORT_SYMBOL_GPL(iommu_domain_alloc); |
| 80 | |
| 81 | void iommu_domain_free(struct iommu_domain *domain) |
| 82 | { |
Stepan Moskovchenko | 452926c | 2011-10-31 16:00:47 -0700 | [diff] [blame] | 83 | if (!iommu_found()) |
| 84 | return; |
| 85 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 86 | iommu_ops->domain_destroy(domain); |
| 87 | kfree(domain); |
| 88 | } |
| 89 | EXPORT_SYMBOL_GPL(iommu_domain_free); |
| 90 | |
| 91 | int iommu_attach_device(struct iommu_domain *domain, struct device *dev) |
| 92 | { |
Stepan Moskovchenko | 452926c | 2011-10-31 16:00:47 -0700 | [diff] [blame] | 93 | if (!iommu_found()) |
| 94 | return -ENODEV; |
| 95 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 96 | return iommu_ops->attach_dev(domain, dev); |
| 97 | } |
| 98 | EXPORT_SYMBOL_GPL(iommu_attach_device); |
| 99 | |
| 100 | void iommu_detach_device(struct iommu_domain *domain, struct device *dev) |
| 101 | { |
Stepan Moskovchenko | 452926c | 2011-10-31 16:00:47 -0700 | [diff] [blame] | 102 | if (!iommu_found()) |
| 103 | return; |
| 104 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 105 | iommu_ops->detach_dev(domain, dev); |
| 106 | } |
| 107 | EXPORT_SYMBOL_GPL(iommu_detach_device); |
| 108 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 109 | phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, |
| 110 | unsigned long iova) |
| 111 | { |
Stepan Moskovchenko | 452926c | 2011-10-31 16:00:47 -0700 | [diff] [blame] | 112 | if (!iommu_found()) |
| 113 | return 0; |
| 114 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 115 | return iommu_ops->iova_to_phys(domain, iova); |
| 116 | } |
| 117 | EXPORT_SYMBOL_GPL(iommu_iova_to_phys); |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 118 | |
| 119 | int iommu_domain_has_cap(struct iommu_domain *domain, |
| 120 | unsigned long cap) |
| 121 | { |
Stepan Moskovchenko | 452926c | 2011-10-31 16:00:47 -0700 | [diff] [blame] | 122 | if (!iommu_found()) |
| 123 | return -ENODEV; |
| 124 | |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 125 | return iommu_ops->domain_has_cap(domain, cap); |
| 126 | } |
| 127 | EXPORT_SYMBOL_GPL(iommu_domain_has_cap); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 128 | |
| 129 | int iommu_map(struct iommu_domain *domain, unsigned long iova, |
| 130 | phys_addr_t paddr, int gfp_order, int prot) |
| 131 | { |
| 132 | unsigned long invalid_mask; |
| 133 | size_t size; |
| 134 | |
Stepan Moskovchenko | 452926c | 2011-10-31 16:00:47 -0700 | [diff] [blame] | 135 | if (!iommu_found()) |
| 136 | return -ENODEV; |
| 137 | |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 138 | size = 0x1000UL << gfp_order; |
| 139 | invalid_mask = size - 1; |
| 140 | |
| 141 | BUG_ON((iova | paddr) & invalid_mask); |
| 142 | |
Joerg Roedel | 12c7389 | 2010-01-21 11:50:28 +0100 | [diff] [blame] | 143 | return iommu_ops->map(domain, iova, paddr, gfp_order, prot); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 144 | } |
| 145 | EXPORT_SYMBOL_GPL(iommu_map); |
| 146 | |
| 147 | int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order) |
| 148 | { |
| 149 | unsigned long invalid_mask; |
| 150 | size_t size; |
| 151 | |
Stepan Moskovchenko | 452926c | 2011-10-31 16:00:47 -0700 | [diff] [blame] | 152 | if (!iommu_found()) |
| 153 | return -ENODEV; |
| 154 | |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 155 | size = 0x1000UL << gfp_order; |
| 156 | invalid_mask = size - 1; |
| 157 | |
| 158 | BUG_ON(iova & invalid_mask); |
| 159 | |
Joerg Roedel | 12c7389 | 2010-01-21 11:50:28 +0100 | [diff] [blame] | 160 | return iommu_ops->unmap(domain, iova, gfp_order); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 161 | } |
| 162 | EXPORT_SYMBOL_GPL(iommu_unmap); |
Stepan Moskovchenko | 0b1345e | 2011-08-11 19:32:27 -0700 | [diff] [blame] | 163 | |
| 164 | int iommu_map_range(struct iommu_domain *domain, unsigned int iova, |
| 165 | struct scatterlist *sg, unsigned int len, int prot) |
| 166 | { |
Stepan Moskovchenko | 452926c | 2011-10-31 16:00:47 -0700 | [diff] [blame] | 167 | if (!iommu_found()) |
| 168 | return -ENODEV; |
| 169 | |
Stepan Moskovchenko | 0b1345e | 2011-08-11 19:32:27 -0700 | [diff] [blame] | 170 | BUG_ON(iova & (~PAGE_MASK)); |
| 171 | |
| 172 | return iommu_ops->map_range(domain, iova, sg, len, prot); |
| 173 | } |
| 174 | EXPORT_SYMBOL_GPL(iommu_map_range); |
| 175 | |
| 176 | int iommu_unmap_range(struct iommu_domain *domain, unsigned int iova, |
| 177 | unsigned int len) |
| 178 | { |
Stepan Moskovchenko | 452926c | 2011-10-31 16:00:47 -0700 | [diff] [blame] | 179 | if (!iommu_found()) |
| 180 | return -ENODEV; |
| 181 | |
Stepan Moskovchenko | 0b1345e | 2011-08-11 19:32:27 -0700 | [diff] [blame] | 182 | BUG_ON(iova & (~PAGE_MASK)); |
| 183 | |
| 184 | return iommu_ops->unmap_range(domain, iova, len); |
| 185 | } |
| 186 | EXPORT_SYMBOL_GPL(iommu_unmap_range); |
Shubhraprakash Das | 4c436f2 | 2011-12-02 18:01:57 -0700 | [diff] [blame] | 187 | |
| 188 | phys_addr_t iommu_get_pt_base_addr(struct iommu_domain *domain) |
| 189 | { |
| 190 | if (!iommu_found()) |
| 191 | return 0; |
| 192 | |
| 193 | return iommu_ops->get_pt_base_addr(domain); |
| 194 | } |
| 195 | EXPORT_SYMBOL_GPL(iommu_get_pt_base_addr); |