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 | } |
Ohad Ben-Cohen | a4d334f | 2011-09-26 09:11:46 -0400 | [diff] [blame^] | 55 | EXPORT_SYMBOL_GPL(iommu_set_fault_handler); |
Ohad Ben-Cohen | a1c6df3 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 56 | |
Stepan Moskovchenko | ff2d366 | 2011-08-31 17:13:32 -0700 | [diff] [blame] | 57 | struct iommu_domain *iommu_domain_alloc(int flags) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 58 | { |
| 59 | struct iommu_domain *domain; |
| 60 | int ret; |
| 61 | |
Stepan Moskovchenko | 452926c | 2011-10-31 16:00:47 -0700 | [diff] [blame] | 62 | if (!iommu_found()) |
| 63 | return NULL; |
| 64 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 65 | domain = kmalloc(sizeof(*domain), GFP_KERNEL); |
| 66 | if (!domain) |
| 67 | return NULL; |
| 68 | |
Stepan Moskovchenko | ff2d366 | 2011-08-31 17:13:32 -0700 | [diff] [blame] | 69 | ret = iommu_ops->domain_init(domain, flags); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 70 | if (ret) |
| 71 | goto out_free; |
| 72 | |
| 73 | return domain; |
| 74 | |
| 75 | out_free: |
| 76 | kfree(domain); |
| 77 | |
| 78 | return NULL; |
| 79 | } |
| 80 | EXPORT_SYMBOL_GPL(iommu_domain_alloc); |
| 81 | |
| 82 | void iommu_domain_free(struct iommu_domain *domain) |
| 83 | { |
Stepan Moskovchenko | 452926c | 2011-10-31 16:00:47 -0700 | [diff] [blame] | 84 | if (!iommu_found()) |
| 85 | return; |
| 86 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 87 | iommu_ops->domain_destroy(domain); |
| 88 | kfree(domain); |
| 89 | } |
| 90 | EXPORT_SYMBOL_GPL(iommu_domain_free); |
| 91 | |
| 92 | int iommu_attach_device(struct iommu_domain *domain, struct device *dev) |
| 93 | { |
Stepan Moskovchenko | 452926c | 2011-10-31 16:00:47 -0700 | [diff] [blame] | 94 | if (!iommu_found()) |
| 95 | return -ENODEV; |
| 96 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 97 | return iommu_ops->attach_dev(domain, dev); |
| 98 | } |
| 99 | EXPORT_SYMBOL_GPL(iommu_attach_device); |
| 100 | |
| 101 | void iommu_detach_device(struct iommu_domain *domain, struct device *dev) |
| 102 | { |
Stepan Moskovchenko | 452926c | 2011-10-31 16:00:47 -0700 | [diff] [blame] | 103 | if (!iommu_found()) |
| 104 | return; |
| 105 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 106 | iommu_ops->detach_dev(domain, dev); |
| 107 | } |
| 108 | EXPORT_SYMBOL_GPL(iommu_detach_device); |
| 109 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 110 | phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, |
| 111 | unsigned long iova) |
| 112 | { |
Stepan Moskovchenko | 452926c | 2011-10-31 16:00:47 -0700 | [diff] [blame] | 113 | if (!iommu_found()) |
| 114 | return 0; |
| 115 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 116 | return iommu_ops->iova_to_phys(domain, iova); |
| 117 | } |
| 118 | EXPORT_SYMBOL_GPL(iommu_iova_to_phys); |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 119 | |
| 120 | int iommu_domain_has_cap(struct iommu_domain *domain, |
| 121 | unsigned long cap) |
| 122 | { |
Stepan Moskovchenko | 452926c | 2011-10-31 16:00:47 -0700 | [diff] [blame] | 123 | if (!iommu_found()) |
| 124 | return -ENODEV; |
| 125 | |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 126 | return iommu_ops->domain_has_cap(domain, cap); |
| 127 | } |
| 128 | EXPORT_SYMBOL_GPL(iommu_domain_has_cap); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 129 | |
| 130 | int iommu_map(struct iommu_domain *domain, unsigned long iova, |
| 131 | phys_addr_t paddr, int gfp_order, int prot) |
| 132 | { |
| 133 | unsigned long invalid_mask; |
| 134 | size_t size; |
| 135 | |
Stepan Moskovchenko | 452926c | 2011-10-31 16:00:47 -0700 | [diff] [blame] | 136 | if (!iommu_found()) |
| 137 | return -ENODEV; |
| 138 | |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 139 | size = 0x1000UL << gfp_order; |
| 140 | invalid_mask = size - 1; |
| 141 | |
| 142 | BUG_ON((iova | paddr) & invalid_mask); |
| 143 | |
Joerg Roedel | 12c7389 | 2010-01-21 11:50:28 +0100 | [diff] [blame] | 144 | return iommu_ops->map(domain, iova, paddr, gfp_order, prot); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 145 | } |
| 146 | EXPORT_SYMBOL_GPL(iommu_map); |
| 147 | |
| 148 | int iommu_unmap(struct iommu_domain *domain, unsigned long iova, int gfp_order) |
| 149 | { |
| 150 | unsigned long invalid_mask; |
| 151 | size_t size; |
| 152 | |
Stepan Moskovchenko | 452926c | 2011-10-31 16:00:47 -0700 | [diff] [blame] | 153 | if (!iommu_found()) |
| 154 | return -ENODEV; |
| 155 | |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 156 | size = 0x1000UL << gfp_order; |
| 157 | invalid_mask = size - 1; |
| 158 | |
| 159 | BUG_ON(iova & invalid_mask); |
| 160 | |
Joerg Roedel | 12c7389 | 2010-01-21 11:50:28 +0100 | [diff] [blame] | 161 | return iommu_ops->unmap(domain, iova, gfp_order); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 162 | } |
| 163 | EXPORT_SYMBOL_GPL(iommu_unmap); |
Stepan Moskovchenko | 0b1345e | 2011-08-11 19:32:27 -0700 | [diff] [blame] | 164 | |
| 165 | int iommu_map_range(struct iommu_domain *domain, unsigned int iova, |
| 166 | struct scatterlist *sg, unsigned int len, int prot) |
| 167 | { |
Stepan Moskovchenko | 452926c | 2011-10-31 16:00:47 -0700 | [diff] [blame] | 168 | if (!iommu_found()) |
| 169 | return -ENODEV; |
| 170 | |
Stepan Moskovchenko | 0b1345e | 2011-08-11 19:32:27 -0700 | [diff] [blame] | 171 | BUG_ON(iova & (~PAGE_MASK)); |
| 172 | |
| 173 | return iommu_ops->map_range(domain, iova, sg, len, prot); |
| 174 | } |
| 175 | EXPORT_SYMBOL_GPL(iommu_map_range); |
| 176 | |
| 177 | int iommu_unmap_range(struct iommu_domain *domain, unsigned int iova, |
| 178 | unsigned int len) |
| 179 | { |
Stepan Moskovchenko | 452926c | 2011-10-31 16:00:47 -0700 | [diff] [blame] | 180 | if (!iommu_found()) |
| 181 | return -ENODEV; |
| 182 | |
Stepan Moskovchenko | 0b1345e | 2011-08-11 19:32:27 -0700 | [diff] [blame] | 183 | BUG_ON(iova & (~PAGE_MASK)); |
| 184 | |
| 185 | return iommu_ops->unmap_range(domain, iova, len); |
| 186 | } |
| 187 | EXPORT_SYMBOL_GPL(iommu_unmap_range); |
Shubhraprakash Das | 4c436f2 | 2011-12-02 18:01:57 -0700 | [diff] [blame] | 188 | |
| 189 | phys_addr_t iommu_get_pt_base_addr(struct iommu_domain *domain) |
| 190 | { |
| 191 | if (!iommu_found()) |
| 192 | return 0; |
| 193 | |
| 194 | return iommu_ops->get_pt_base_addr(domain); |
| 195 | } |
| 196 | EXPORT_SYMBOL_GPL(iommu_get_pt_base_addr); |