Thomas Gleixner | 4505153 | 2019-05-29 16:57:47 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2007-2008 Advanced Micro Devices, Inc. |
Joerg Roedel | 63ce3ae | 2015-02-04 16:12:55 +0100 | [diff] [blame] | 4 | * Author: Joerg Roedel <jroedel@suse.de> |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Joerg Roedel | 92e7066 | 2015-05-28 18:41:24 +0200 | [diff] [blame] | 7 | #define pr_fmt(fmt) "iommu: " fmt |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 8 | |
Joerg Roedel | 905d66c | 2011-09-06 16:03:26 +0200 | [diff] [blame] | 9 | #include <linux/device.h> |
Ohad Ben-Cohen | 4099818 | 2011-09-02 13:32:32 -0400 | [diff] [blame] | 10 | #include <linux/kernel.h> |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 11 | #include <linux/bug.h> |
| 12 | #include <linux/types.h> |
Paul Gortmaker | c1af7b4 | 2018-12-01 14:19:09 -0500 | [diff] [blame] | 13 | #include <linux/init.h> |
| 14 | #include <linux/export.h> |
Andrew Morton | 60db402 | 2009-05-06 16:03:07 -0700 | [diff] [blame] | 15 | #include <linux/slab.h> |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 16 | #include <linux/errno.h> |
| 17 | #include <linux/iommu.h> |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 18 | #include <linux/idr.h> |
| 19 | #include <linux/notifier.h> |
| 20 | #include <linux/err.h> |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 21 | #include <linux/pci.h> |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 22 | #include <linux/bitops.h> |
Robin Murphy | 57f98d2 | 2016-09-13 10:54:14 +0100 | [diff] [blame] | 23 | #include <linux/property.h> |
Nipun Gupta | eab03e2 | 2018-09-10 19:19:18 +0530 | [diff] [blame] | 24 | #include <linux/fsl/mc.h> |
Shuah Khan | 7f6db17 | 2013-08-15 11:59:23 -0600 | [diff] [blame] | 25 | #include <trace/events/iommu.h> |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 26 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 27 | static struct kset *iommu_group_kset; |
Heiner Kallweit | e38d1f1 | 2016-06-28 20:38:36 +0200 | [diff] [blame] | 28 | static DEFINE_IDA(iommu_group_ida); |
Joerg Roedel | 22bb182 | 2019-08-19 15:22:54 +0200 | [diff] [blame] | 29 | |
| 30 | static unsigned int iommu_def_domain_type __read_mostly; |
Zhen Lei | 68a6efe | 2018-09-20 17:10:23 +0100 | [diff] [blame] | 31 | static bool iommu_dma_strict __read_mostly = true; |
Joerg Roedel | faf1498 | 2019-08-19 15:22:46 +0200 | [diff] [blame] | 32 | static u32 iommu_cmd_line __read_mostly; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 33 | |
| 34 | struct iommu_group { |
| 35 | struct kobject kobj; |
| 36 | struct kobject *devices_kobj; |
| 37 | struct list_head devices; |
| 38 | struct mutex mutex; |
| 39 | struct blocking_notifier_head notifier; |
| 40 | void *iommu_data; |
| 41 | void (*iommu_data_release)(void *iommu_data); |
| 42 | char *name; |
| 43 | int id; |
Joerg Roedel | 53723dc | 2015-05-28 18:41:29 +0200 | [diff] [blame] | 44 | struct iommu_domain *default_domain; |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 45 | struct iommu_domain *domain; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 46 | }; |
| 47 | |
Joerg Roedel | c09e22d | 2017-02-01 12:19:46 +0100 | [diff] [blame] | 48 | struct group_device { |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 49 | struct list_head list; |
| 50 | struct device *dev; |
| 51 | char *name; |
| 52 | }; |
| 53 | |
| 54 | struct iommu_group_attribute { |
| 55 | struct attribute attr; |
| 56 | ssize_t (*show)(struct iommu_group *group, char *buf); |
| 57 | ssize_t (*store)(struct iommu_group *group, |
| 58 | const char *buf, size_t count); |
| 59 | }; |
| 60 | |
Eric Auger | bc7d12b9 | 2017-01-19 20:57:52 +0000 | [diff] [blame] | 61 | static const char * const iommu_group_resv_type_string[] = { |
Eric Auger | adfd373 | 2019-06-03 08:53:35 +0200 | [diff] [blame] | 62 | [IOMMU_RESV_DIRECT] = "direct", |
| 63 | [IOMMU_RESV_DIRECT_RELAXABLE] = "direct-relaxable", |
| 64 | [IOMMU_RESV_RESERVED] = "reserved", |
| 65 | [IOMMU_RESV_MSI] = "msi", |
| 66 | [IOMMU_RESV_SW_MSI] = "msi", |
Eric Auger | bc7d12b9 | 2017-01-19 20:57:52 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
Joerg Roedel | faf1498 | 2019-08-19 15:22:46 +0200 | [diff] [blame] | 69 | #define IOMMU_CMD_LINE_DMA_API BIT(0) |
| 70 | |
| 71 | static void iommu_set_cmd_line_dma_api(void) |
| 72 | { |
| 73 | iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API; |
| 74 | } |
| 75 | |
Joerg Roedel | 22bb182 | 2019-08-19 15:22:54 +0200 | [diff] [blame] | 76 | static bool iommu_cmd_line_dma_api(void) |
Joerg Roedel | faf1498 | 2019-08-19 15:22:46 +0200 | [diff] [blame] | 77 | { |
| 78 | return !!(iommu_cmd_line & IOMMU_CMD_LINE_DMA_API); |
| 79 | } |
| 80 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 81 | #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \ |
| 82 | struct iommu_group_attribute iommu_group_attr_##_name = \ |
| 83 | __ATTR(_name, _mode, _show, _store) |
| 84 | |
| 85 | #define to_iommu_group_attr(_attr) \ |
| 86 | container_of(_attr, struct iommu_group_attribute, attr) |
| 87 | #define to_iommu_group(_kobj) \ |
| 88 | container_of(_kobj, struct iommu_group, kobj) |
| 89 | |
Joerg Roedel | b0119e8 | 2017-02-01 13:23:08 +0100 | [diff] [blame] | 90 | static LIST_HEAD(iommu_device_list); |
| 91 | static DEFINE_SPINLOCK(iommu_device_lock); |
| 92 | |
Joerg Roedel | 5fa9e7c | 2019-08-19 15:22:53 +0200 | [diff] [blame] | 93 | /* |
| 94 | * Use a function instead of an array here because the domain-type is a |
| 95 | * bit-field, so an array would waste memory. |
| 96 | */ |
| 97 | static const char *iommu_domain_type_str(unsigned int t) |
| 98 | { |
| 99 | switch (t) { |
| 100 | case IOMMU_DOMAIN_BLOCKED: |
| 101 | return "Blocked"; |
| 102 | case IOMMU_DOMAIN_IDENTITY: |
| 103 | return "Passthrough"; |
| 104 | case IOMMU_DOMAIN_UNMANAGED: |
| 105 | return "Unmanaged"; |
| 106 | case IOMMU_DOMAIN_DMA: |
| 107 | return "Translated"; |
| 108 | default: |
| 109 | return "Unknown"; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | static int __init iommu_subsys_init(void) |
| 114 | { |
Joerg Roedel | 22bb182 | 2019-08-19 15:22:54 +0200 | [diff] [blame] | 115 | bool cmd_line = iommu_cmd_line_dma_api(); |
| 116 | |
| 117 | if (!cmd_line) { |
| 118 | if (IS_ENABLED(CONFIG_IOMMU_DEFAULT_PASSTHROUGH)) |
| 119 | iommu_set_default_passthrough(false); |
| 120 | else |
| 121 | iommu_set_default_translated(false); |
Joerg Roedel | 2cc13bb | 2019-08-19 15:22:55 +0200 | [diff] [blame] | 122 | |
Joerg Roedel | 2896ba4 | 2019-09-03 15:15:44 +0200 | [diff] [blame] | 123 | if (iommu_default_passthrough() && mem_encrypt_active()) { |
| 124 | pr_info("Memory encryption detected - Disabling default IOMMU Passthrough\n"); |
Joerg Roedel | 2cc13bb | 2019-08-19 15:22:55 +0200 | [diff] [blame] | 125 | iommu_set_default_translated(false); |
| 126 | } |
Joerg Roedel | 22bb182 | 2019-08-19 15:22:54 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | pr_info("Default domain type: %s %s\n", |
| 130 | iommu_domain_type_str(iommu_def_domain_type), |
| 131 | cmd_line ? "(set via kernel command line)" : ""); |
Joerg Roedel | 5fa9e7c | 2019-08-19 15:22:53 +0200 | [diff] [blame] | 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | subsys_initcall(iommu_subsys_init); |
| 136 | |
Joerg Roedel | b0119e8 | 2017-02-01 13:23:08 +0100 | [diff] [blame] | 137 | int iommu_device_register(struct iommu_device *iommu) |
| 138 | { |
| 139 | spin_lock(&iommu_device_lock); |
| 140 | list_add_tail(&iommu->list, &iommu_device_list); |
| 141 | spin_unlock(&iommu_device_lock); |
Joerg Roedel | b0119e8 | 2017-02-01 13:23:08 +0100 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | void iommu_device_unregister(struct iommu_device *iommu) |
| 146 | { |
| 147 | spin_lock(&iommu_device_lock); |
| 148 | list_del(&iommu->list); |
| 149 | spin_unlock(&iommu_device_lock); |
| 150 | } |
| 151 | |
Jacob Pan | 0c830e6 | 2019-06-03 15:57:48 +0100 | [diff] [blame] | 152 | static struct iommu_param *iommu_get_dev_param(struct device *dev) |
| 153 | { |
| 154 | struct iommu_param *param = dev->iommu_param; |
| 155 | |
| 156 | if (param) |
| 157 | return param; |
| 158 | |
| 159 | param = kzalloc(sizeof(*param), GFP_KERNEL); |
| 160 | if (!param) |
| 161 | return NULL; |
| 162 | |
| 163 | mutex_init(¶m->lock); |
| 164 | dev->iommu_param = param; |
| 165 | return param; |
| 166 | } |
| 167 | |
| 168 | static void iommu_free_dev_param(struct device *dev) |
| 169 | { |
| 170 | kfree(dev->iommu_param); |
| 171 | dev->iommu_param = NULL; |
| 172 | } |
| 173 | |
Joerg Roedel | cc5aed4 | 2018-11-30 10:31:59 +0100 | [diff] [blame] | 174 | int iommu_probe_device(struct device *dev) |
| 175 | { |
| 176 | const struct iommu_ops *ops = dev->bus->iommu_ops; |
Jacob Pan | 0c830e6 | 2019-06-03 15:57:48 +0100 | [diff] [blame] | 177 | int ret; |
Joerg Roedel | cc5aed4 | 2018-11-30 10:31:59 +0100 | [diff] [blame] | 178 | |
| 179 | WARN_ON(dev->iommu_group); |
Jacob Pan | 0c830e6 | 2019-06-03 15:57:48 +0100 | [diff] [blame] | 180 | if (!ops) |
| 181 | return -EINVAL; |
Joerg Roedel | cc5aed4 | 2018-11-30 10:31:59 +0100 | [diff] [blame] | 182 | |
Jacob Pan | 0c830e6 | 2019-06-03 15:57:48 +0100 | [diff] [blame] | 183 | if (!iommu_get_dev_param(dev)) |
| 184 | return -ENOMEM; |
| 185 | |
| 186 | ret = ops->add_device(dev); |
| 187 | if (ret) |
| 188 | iommu_free_dev_param(dev); |
Joerg Roedel | dc9de8a | 2018-12-20 10:02:20 +0100 | [diff] [blame] | 189 | |
| 190 | return ret; |
Joerg Roedel | cc5aed4 | 2018-11-30 10:31:59 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void iommu_release_device(struct device *dev) |
| 194 | { |
| 195 | const struct iommu_ops *ops = dev->bus->iommu_ops; |
| 196 | |
| 197 | if (dev->iommu_group) |
| 198 | ops->remove_device(dev); |
Jacob Pan | 0c830e6 | 2019-06-03 15:57:48 +0100 | [diff] [blame] | 199 | |
| 200 | iommu_free_dev_param(dev); |
Joerg Roedel | cc5aed4 | 2018-11-30 10:31:59 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Joerg Roedel | 53723dc | 2015-05-28 18:41:29 +0200 | [diff] [blame] | 203 | static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus, |
| 204 | unsigned type); |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 205 | static int __iommu_attach_device(struct iommu_domain *domain, |
| 206 | struct device *dev); |
| 207 | static int __iommu_attach_group(struct iommu_domain *domain, |
| 208 | struct iommu_group *group); |
| 209 | static void __iommu_detach_group(struct iommu_domain *domain, |
| 210 | struct iommu_group *group); |
Joerg Roedel | 53723dc | 2015-05-28 18:41:29 +0200 | [diff] [blame] | 211 | |
Will Deacon | fccb4e3 | 2017-01-05 18:38:26 +0000 | [diff] [blame] | 212 | static int __init iommu_set_def_domain_type(char *str) |
| 213 | { |
| 214 | bool pt; |
Andy Shevchenko | 7f9584d | 2018-05-14 19:22:25 +0300 | [diff] [blame] | 215 | int ret; |
Will Deacon | fccb4e3 | 2017-01-05 18:38:26 +0000 | [diff] [blame] | 216 | |
Andy Shevchenko | 7f9584d | 2018-05-14 19:22:25 +0300 | [diff] [blame] | 217 | ret = kstrtobool(str, &pt); |
| 218 | if (ret) |
| 219 | return ret; |
Will Deacon | fccb4e3 | 2017-01-05 18:38:26 +0000 | [diff] [blame] | 220 | |
Joerg Roedel | adab0b0 | 2019-08-19 15:22:48 +0200 | [diff] [blame] | 221 | if (pt) |
| 222 | iommu_set_default_passthrough(true); |
| 223 | else |
| 224 | iommu_set_default_translated(true); |
Joerg Roedel | faf1498 | 2019-08-19 15:22:46 +0200 | [diff] [blame] | 225 | |
Will Deacon | fccb4e3 | 2017-01-05 18:38:26 +0000 | [diff] [blame] | 226 | return 0; |
| 227 | } |
| 228 | early_param("iommu.passthrough", iommu_set_def_domain_type); |
| 229 | |
Zhen Lei | 68a6efe | 2018-09-20 17:10:23 +0100 | [diff] [blame] | 230 | static int __init iommu_dma_setup(char *str) |
| 231 | { |
| 232 | return kstrtobool(str, &iommu_dma_strict); |
| 233 | } |
| 234 | early_param("iommu.strict", iommu_dma_setup); |
| 235 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 236 | static ssize_t iommu_group_attr_show(struct kobject *kobj, |
| 237 | struct attribute *__attr, char *buf) |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 238 | { |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 239 | struct iommu_group_attribute *attr = to_iommu_group_attr(__attr); |
| 240 | struct iommu_group *group = to_iommu_group(kobj); |
| 241 | ssize_t ret = -EIO; |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 242 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 243 | if (attr->show) |
| 244 | ret = attr->show(group, buf); |
| 245 | return ret; |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 246 | } |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 247 | |
| 248 | static ssize_t iommu_group_attr_store(struct kobject *kobj, |
| 249 | struct attribute *__attr, |
| 250 | const char *buf, size_t count) |
| 251 | { |
| 252 | struct iommu_group_attribute *attr = to_iommu_group_attr(__attr); |
| 253 | struct iommu_group *group = to_iommu_group(kobj); |
| 254 | ssize_t ret = -EIO; |
| 255 | |
| 256 | if (attr->store) |
| 257 | ret = attr->store(group, buf, count); |
| 258 | return ret; |
| 259 | } |
| 260 | |
| 261 | static const struct sysfs_ops iommu_group_sysfs_ops = { |
| 262 | .show = iommu_group_attr_show, |
| 263 | .store = iommu_group_attr_store, |
| 264 | }; |
| 265 | |
| 266 | static int iommu_group_create_file(struct iommu_group *group, |
| 267 | struct iommu_group_attribute *attr) |
| 268 | { |
| 269 | return sysfs_create_file(&group->kobj, &attr->attr); |
| 270 | } |
| 271 | |
| 272 | static void iommu_group_remove_file(struct iommu_group *group, |
| 273 | struct iommu_group_attribute *attr) |
| 274 | { |
| 275 | sysfs_remove_file(&group->kobj, &attr->attr); |
| 276 | } |
| 277 | |
| 278 | static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf) |
| 279 | { |
| 280 | return sprintf(buf, "%s\n", group->name); |
| 281 | } |
| 282 | |
Eric Auger | 6c65fb3 | 2017-01-19 20:57:51 +0000 | [diff] [blame] | 283 | /** |
| 284 | * iommu_insert_resv_region - Insert a new region in the |
| 285 | * list of reserved regions. |
| 286 | * @new: new region to insert |
| 287 | * @regions: list of regions |
| 288 | * |
Eric Auger | 4dbd258 | 2019-08-21 14:09:40 +0200 | [diff] [blame] | 289 | * Elements are sorted by start address and overlapping segments |
| 290 | * of the same type are merged. |
Eric Auger | 6c65fb3 | 2017-01-19 20:57:51 +0000 | [diff] [blame] | 291 | */ |
Eric Auger | 4dbd258 | 2019-08-21 14:09:40 +0200 | [diff] [blame] | 292 | int iommu_insert_resv_region(struct iommu_resv_region *new, |
| 293 | struct list_head *regions) |
Eric Auger | 6c65fb3 | 2017-01-19 20:57:51 +0000 | [diff] [blame] | 294 | { |
Eric Auger | 4dbd258 | 2019-08-21 14:09:40 +0200 | [diff] [blame] | 295 | struct iommu_resv_region *iter, *tmp, *nr, *top; |
| 296 | LIST_HEAD(stack); |
Eric Auger | 6c65fb3 | 2017-01-19 20:57:51 +0000 | [diff] [blame] | 297 | |
Eric Auger | 4dbd258 | 2019-08-21 14:09:40 +0200 | [diff] [blame] | 298 | nr = iommu_alloc_resv_region(new->start, new->length, |
| 299 | new->prot, new->type); |
| 300 | if (!nr) |
Eric Auger | 6c65fb3 | 2017-01-19 20:57:51 +0000 | [diff] [blame] | 301 | return -ENOMEM; |
| 302 | |
Eric Auger | 4dbd258 | 2019-08-21 14:09:40 +0200 | [diff] [blame] | 303 | /* First add the new element based on start address sorting */ |
| 304 | list_for_each_entry(iter, regions, list) { |
| 305 | if (nr->start < iter->start || |
| 306 | (nr->start == iter->start && nr->type <= iter->type)) |
| 307 | break; |
| 308 | } |
| 309 | list_add_tail(&nr->list, &iter->list); |
| 310 | |
| 311 | /* Merge overlapping segments of type nr->type in @regions, if any */ |
| 312 | list_for_each_entry_safe(iter, tmp, regions, list) { |
| 313 | phys_addr_t top_end, iter_end = iter->start + iter->length - 1; |
| 314 | |
Eric Auger | 4c80ba3 | 2019-11-26 18:54:13 +0100 | [diff] [blame] | 315 | /* no merge needed on elements of different types than @new */ |
| 316 | if (iter->type != new->type) { |
Eric Auger | 4dbd258 | 2019-08-21 14:09:40 +0200 | [diff] [blame] | 317 | list_move_tail(&iter->list, &stack); |
| 318 | continue; |
| 319 | } |
| 320 | |
| 321 | /* look for the last stack element of same type as @iter */ |
| 322 | list_for_each_entry_reverse(top, &stack, list) |
| 323 | if (top->type == iter->type) |
| 324 | goto check_overlap; |
| 325 | |
| 326 | list_move_tail(&iter->list, &stack); |
| 327 | continue; |
| 328 | |
| 329 | check_overlap: |
| 330 | top_end = top->start + top->length - 1; |
| 331 | |
| 332 | if (iter->start > top_end + 1) { |
| 333 | list_move_tail(&iter->list, &stack); |
| 334 | } else { |
| 335 | top->length = max(top_end, iter_end) - top->start + 1; |
| 336 | list_del(&iter->list); |
| 337 | kfree(iter); |
| 338 | } |
| 339 | } |
| 340 | list_splice(&stack, regions); |
Eric Auger | 6c65fb3 | 2017-01-19 20:57:51 +0000 | [diff] [blame] | 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static int |
| 345 | iommu_insert_device_resv_regions(struct list_head *dev_resv_regions, |
| 346 | struct list_head *group_resv_regions) |
| 347 | { |
| 348 | struct iommu_resv_region *entry; |
Eric Auger | a514a6e | 2017-02-06 10:11:38 +0100 | [diff] [blame] | 349 | int ret = 0; |
Eric Auger | 6c65fb3 | 2017-01-19 20:57:51 +0000 | [diff] [blame] | 350 | |
| 351 | list_for_each_entry(entry, dev_resv_regions, list) { |
| 352 | ret = iommu_insert_resv_region(entry, group_resv_regions); |
| 353 | if (ret) |
| 354 | break; |
| 355 | } |
| 356 | return ret; |
| 357 | } |
| 358 | |
| 359 | int iommu_get_group_resv_regions(struct iommu_group *group, |
| 360 | struct list_head *head) |
| 361 | { |
Joerg Roedel | 8d2932d | 2017-02-10 15:13:10 +0100 | [diff] [blame] | 362 | struct group_device *device; |
Eric Auger | 6c65fb3 | 2017-01-19 20:57:51 +0000 | [diff] [blame] | 363 | int ret = 0; |
| 364 | |
| 365 | mutex_lock(&group->mutex); |
| 366 | list_for_each_entry(device, &group->devices, list) { |
| 367 | struct list_head dev_resv_regions; |
| 368 | |
| 369 | INIT_LIST_HEAD(&dev_resv_regions); |
| 370 | iommu_get_resv_regions(device->dev, &dev_resv_regions); |
| 371 | ret = iommu_insert_device_resv_regions(&dev_resv_regions, head); |
| 372 | iommu_put_resv_regions(device->dev, &dev_resv_regions); |
| 373 | if (ret) |
| 374 | break; |
| 375 | } |
| 376 | mutex_unlock(&group->mutex); |
| 377 | return ret; |
| 378 | } |
| 379 | EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions); |
| 380 | |
Eric Auger | bc7d12b9 | 2017-01-19 20:57:52 +0000 | [diff] [blame] | 381 | static ssize_t iommu_group_show_resv_regions(struct iommu_group *group, |
| 382 | char *buf) |
| 383 | { |
| 384 | struct iommu_resv_region *region, *next; |
| 385 | struct list_head group_resv_regions; |
| 386 | char *str = buf; |
| 387 | |
| 388 | INIT_LIST_HEAD(&group_resv_regions); |
| 389 | iommu_get_group_resv_regions(group, &group_resv_regions); |
| 390 | |
| 391 | list_for_each_entry_safe(region, next, &group_resv_regions, list) { |
| 392 | str += sprintf(str, "0x%016llx 0x%016llx %s\n", |
| 393 | (long long int)region->start, |
| 394 | (long long int)(region->start + |
| 395 | region->length - 1), |
| 396 | iommu_group_resv_type_string[region->type]); |
| 397 | kfree(region); |
| 398 | } |
| 399 | |
| 400 | return (str - buf); |
| 401 | } |
| 402 | |
Olof Johansson | c52c72d | 2018-07-11 13:59:36 -0700 | [diff] [blame] | 403 | static ssize_t iommu_group_show_type(struct iommu_group *group, |
| 404 | char *buf) |
| 405 | { |
| 406 | char *type = "unknown\n"; |
| 407 | |
| 408 | if (group->default_domain) { |
| 409 | switch (group->default_domain->type) { |
| 410 | case IOMMU_DOMAIN_BLOCKED: |
| 411 | type = "blocked\n"; |
| 412 | break; |
| 413 | case IOMMU_DOMAIN_IDENTITY: |
| 414 | type = "identity\n"; |
| 415 | break; |
| 416 | case IOMMU_DOMAIN_UNMANAGED: |
| 417 | type = "unmanaged\n"; |
| 418 | break; |
| 419 | case IOMMU_DOMAIN_DMA: |
Lu Baolu | 24f307d | 2019-05-24 14:30:56 +0800 | [diff] [blame] | 420 | type = "DMA\n"; |
Olof Johansson | c52c72d | 2018-07-11 13:59:36 -0700 | [diff] [blame] | 421 | break; |
| 422 | } |
| 423 | } |
| 424 | strcpy(buf, type); |
| 425 | |
| 426 | return strlen(type); |
| 427 | } |
| 428 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 429 | static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL); |
| 430 | |
Eric Auger | bc7d12b9 | 2017-01-19 20:57:52 +0000 | [diff] [blame] | 431 | static IOMMU_GROUP_ATTR(reserved_regions, 0444, |
| 432 | iommu_group_show_resv_regions, NULL); |
| 433 | |
Olof Johansson | c52c72d | 2018-07-11 13:59:36 -0700 | [diff] [blame] | 434 | static IOMMU_GROUP_ATTR(type, 0444, iommu_group_show_type, NULL); |
| 435 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 436 | static void iommu_group_release(struct kobject *kobj) |
| 437 | { |
| 438 | struct iommu_group *group = to_iommu_group(kobj); |
| 439 | |
Joerg Roedel | 269aa80 | 2015-05-28 18:41:25 +0200 | [diff] [blame] | 440 | pr_debug("Releasing group %d\n", group->id); |
| 441 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 442 | if (group->iommu_data_release) |
| 443 | group->iommu_data_release(group->iommu_data); |
| 444 | |
Heiner Kallweit | feccf39 | 2016-06-29 21:13:59 +0200 | [diff] [blame] | 445 | ida_simple_remove(&iommu_group_ida, group->id); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 446 | |
Joerg Roedel | 53723dc | 2015-05-28 18:41:29 +0200 | [diff] [blame] | 447 | if (group->default_domain) |
| 448 | iommu_domain_free(group->default_domain); |
| 449 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 450 | kfree(group->name); |
| 451 | kfree(group); |
| 452 | } |
| 453 | |
| 454 | static struct kobj_type iommu_group_ktype = { |
| 455 | .sysfs_ops = &iommu_group_sysfs_ops, |
| 456 | .release = iommu_group_release, |
| 457 | }; |
| 458 | |
| 459 | /** |
| 460 | * iommu_group_alloc - Allocate a new group |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 461 | * |
| 462 | * This function is called by an iommu driver to allocate a new iommu |
| 463 | * group. The iommu group represents the minimum granularity of the iommu. |
| 464 | * Upon successful return, the caller holds a reference to the supplied |
| 465 | * group in order to hold the group until devices are added. Use |
| 466 | * iommu_group_put() to release this extra reference count, allowing the |
| 467 | * group to be automatically reclaimed once it has no devices or external |
| 468 | * references. |
| 469 | */ |
| 470 | struct iommu_group *iommu_group_alloc(void) |
| 471 | { |
| 472 | struct iommu_group *group; |
| 473 | int ret; |
| 474 | |
| 475 | group = kzalloc(sizeof(*group), GFP_KERNEL); |
| 476 | if (!group) |
| 477 | return ERR_PTR(-ENOMEM); |
| 478 | |
| 479 | group->kobj.kset = iommu_group_kset; |
| 480 | mutex_init(&group->mutex); |
| 481 | INIT_LIST_HEAD(&group->devices); |
| 482 | BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier); |
| 483 | |
Heiner Kallweit | feccf39 | 2016-06-29 21:13:59 +0200 | [diff] [blame] | 484 | ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL); |
| 485 | if (ret < 0) { |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 486 | kfree(group); |
Heiner Kallweit | feccf39 | 2016-06-29 21:13:59 +0200 | [diff] [blame] | 487 | return ERR_PTR(ret); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 488 | } |
Heiner Kallweit | feccf39 | 2016-06-29 21:13:59 +0200 | [diff] [blame] | 489 | group->id = ret; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 490 | |
| 491 | ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype, |
| 492 | NULL, "%d", group->id); |
| 493 | if (ret) { |
Heiner Kallweit | feccf39 | 2016-06-29 21:13:59 +0200 | [diff] [blame] | 494 | ida_simple_remove(&iommu_group_ida, group->id); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 495 | kfree(group); |
| 496 | return ERR_PTR(ret); |
| 497 | } |
| 498 | |
| 499 | group->devices_kobj = kobject_create_and_add("devices", &group->kobj); |
| 500 | if (!group->devices_kobj) { |
| 501 | kobject_put(&group->kobj); /* triggers .release & free */ |
| 502 | return ERR_PTR(-ENOMEM); |
| 503 | } |
| 504 | |
| 505 | /* |
| 506 | * The devices_kobj holds a reference on the group kobject, so |
| 507 | * as long as that exists so will the group. We can therefore |
| 508 | * use the devices_kobj for reference counting. |
| 509 | */ |
| 510 | kobject_put(&group->kobj); |
| 511 | |
Eric Auger | bc7d12b9 | 2017-01-19 20:57:52 +0000 | [diff] [blame] | 512 | ret = iommu_group_create_file(group, |
| 513 | &iommu_group_attr_reserved_regions); |
| 514 | if (ret) |
| 515 | return ERR_PTR(ret); |
| 516 | |
Olof Johansson | c52c72d | 2018-07-11 13:59:36 -0700 | [diff] [blame] | 517 | ret = iommu_group_create_file(group, &iommu_group_attr_type); |
| 518 | if (ret) |
| 519 | return ERR_PTR(ret); |
| 520 | |
Joerg Roedel | 269aa80 | 2015-05-28 18:41:25 +0200 | [diff] [blame] | 521 | pr_debug("Allocated group %d\n", group->id); |
| 522 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 523 | return group; |
| 524 | } |
| 525 | EXPORT_SYMBOL_GPL(iommu_group_alloc); |
| 526 | |
Alexey Kardashevskiy | aa16bea | 2013-03-25 10:23:49 +1100 | [diff] [blame] | 527 | struct iommu_group *iommu_group_get_by_id(int id) |
| 528 | { |
| 529 | struct kobject *group_kobj; |
| 530 | struct iommu_group *group; |
| 531 | const char *name; |
| 532 | |
| 533 | if (!iommu_group_kset) |
| 534 | return NULL; |
| 535 | |
| 536 | name = kasprintf(GFP_KERNEL, "%d", id); |
| 537 | if (!name) |
| 538 | return NULL; |
| 539 | |
| 540 | group_kobj = kset_find_obj(iommu_group_kset, name); |
| 541 | kfree(name); |
| 542 | |
| 543 | if (!group_kobj) |
| 544 | return NULL; |
| 545 | |
| 546 | group = container_of(group_kobj, struct iommu_group, kobj); |
| 547 | BUG_ON(group->id != id); |
| 548 | |
| 549 | kobject_get(group->devices_kobj); |
| 550 | kobject_put(&group->kobj); |
| 551 | |
| 552 | return group; |
| 553 | } |
| 554 | EXPORT_SYMBOL_GPL(iommu_group_get_by_id); |
| 555 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 556 | /** |
| 557 | * iommu_group_get_iommudata - retrieve iommu_data registered for a group |
| 558 | * @group: the group |
| 559 | * |
| 560 | * iommu drivers can store data in the group for use when doing iommu |
| 561 | * operations. This function provides a way to retrieve it. Caller |
| 562 | * should hold a group reference. |
| 563 | */ |
| 564 | void *iommu_group_get_iommudata(struct iommu_group *group) |
| 565 | { |
| 566 | return group->iommu_data; |
| 567 | } |
| 568 | EXPORT_SYMBOL_GPL(iommu_group_get_iommudata); |
| 569 | |
| 570 | /** |
| 571 | * iommu_group_set_iommudata - set iommu_data for a group |
| 572 | * @group: the group |
| 573 | * @iommu_data: new data |
| 574 | * @release: release function for iommu_data |
| 575 | * |
| 576 | * iommu drivers can store data in the group for use when doing iommu |
| 577 | * operations. This function provides a way to set the data after |
| 578 | * the group has been allocated. Caller should hold a group reference. |
| 579 | */ |
| 580 | void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data, |
| 581 | void (*release)(void *iommu_data)) |
| 582 | { |
| 583 | group->iommu_data = iommu_data; |
| 584 | group->iommu_data_release = release; |
| 585 | } |
| 586 | EXPORT_SYMBOL_GPL(iommu_group_set_iommudata); |
| 587 | |
| 588 | /** |
| 589 | * iommu_group_set_name - set name for a group |
| 590 | * @group: the group |
| 591 | * @name: name |
| 592 | * |
| 593 | * Allow iommu driver to set a name for a group. When set it will |
| 594 | * appear in a name attribute file under the group in sysfs. |
| 595 | */ |
| 596 | int iommu_group_set_name(struct iommu_group *group, const char *name) |
| 597 | { |
| 598 | int ret; |
| 599 | |
| 600 | if (group->name) { |
| 601 | iommu_group_remove_file(group, &iommu_group_attr_name); |
| 602 | kfree(group->name); |
| 603 | group->name = NULL; |
| 604 | if (!name) |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | group->name = kstrdup(name, GFP_KERNEL); |
| 609 | if (!group->name) |
| 610 | return -ENOMEM; |
| 611 | |
| 612 | ret = iommu_group_create_file(group, &iommu_group_attr_name); |
| 613 | if (ret) { |
| 614 | kfree(group->name); |
| 615 | group->name = NULL; |
| 616 | return ret; |
| 617 | } |
| 618 | |
| 619 | return 0; |
| 620 | } |
| 621 | EXPORT_SYMBOL_GPL(iommu_group_set_name); |
| 622 | |
Joerg Roedel | beed282 | 2015-05-28 18:41:34 +0200 | [diff] [blame] | 623 | static int iommu_group_create_direct_mappings(struct iommu_group *group, |
| 624 | struct device *dev) |
| 625 | { |
| 626 | struct iommu_domain *domain = group->default_domain; |
Eric Auger | e5b5234 | 2017-01-19 20:57:47 +0000 | [diff] [blame] | 627 | struct iommu_resv_region *entry; |
Joerg Roedel | beed282 | 2015-05-28 18:41:34 +0200 | [diff] [blame] | 628 | struct list_head mappings; |
| 629 | unsigned long pg_size; |
| 630 | int ret = 0; |
| 631 | |
| 632 | if (!domain || domain->type != IOMMU_DOMAIN_DMA) |
| 633 | return 0; |
| 634 | |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 635 | BUG_ON(!domain->pgsize_bitmap); |
Joerg Roedel | beed282 | 2015-05-28 18:41:34 +0200 | [diff] [blame] | 636 | |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 637 | pg_size = 1UL << __ffs(domain->pgsize_bitmap); |
Joerg Roedel | beed282 | 2015-05-28 18:41:34 +0200 | [diff] [blame] | 638 | INIT_LIST_HEAD(&mappings); |
| 639 | |
Eric Auger | e5b5234 | 2017-01-19 20:57:47 +0000 | [diff] [blame] | 640 | iommu_get_resv_regions(dev, &mappings); |
Joerg Roedel | beed282 | 2015-05-28 18:41:34 +0200 | [diff] [blame] | 641 | |
| 642 | /* We need to consider overlapping regions for different devices */ |
| 643 | list_for_each_entry(entry, &mappings, list) { |
| 644 | dma_addr_t start, end, addr; |
| 645 | |
Eric Auger | e5b5234 | 2017-01-19 20:57:47 +0000 | [diff] [blame] | 646 | if (domain->ops->apply_resv_region) |
| 647 | domain->ops->apply_resv_region(dev, domain, entry); |
Joerg Roedel | 33b21a6 | 2016-07-05 13:07:53 +0200 | [diff] [blame] | 648 | |
Joerg Roedel | beed282 | 2015-05-28 18:41:34 +0200 | [diff] [blame] | 649 | start = ALIGN(entry->start, pg_size); |
| 650 | end = ALIGN(entry->start + entry->length, pg_size); |
| 651 | |
Eric Auger | adfd373 | 2019-06-03 08:53:35 +0200 | [diff] [blame] | 652 | if (entry->type != IOMMU_RESV_DIRECT && |
| 653 | entry->type != IOMMU_RESV_DIRECT_RELAXABLE) |
Eric Auger | 544a25d | 2017-01-19 20:57:50 +0000 | [diff] [blame] | 654 | continue; |
| 655 | |
Joerg Roedel | beed282 | 2015-05-28 18:41:34 +0200 | [diff] [blame] | 656 | for (addr = start; addr < end; addr += pg_size) { |
| 657 | phys_addr_t phys_addr; |
| 658 | |
| 659 | phys_addr = iommu_iova_to_phys(domain, addr); |
| 660 | if (phys_addr) |
| 661 | continue; |
| 662 | |
| 663 | ret = iommu_map(domain, addr, addr, pg_size, entry->prot); |
| 664 | if (ret) |
| 665 | goto out; |
| 666 | } |
| 667 | |
| 668 | } |
| 669 | |
Joerg Roedel | add02cfd | 2017-08-23 15:50:04 +0200 | [diff] [blame] | 670 | iommu_flush_tlb_all(domain); |
| 671 | |
Joerg Roedel | beed282 | 2015-05-28 18:41:34 +0200 | [diff] [blame] | 672 | out: |
Eric Auger | e5b5234 | 2017-01-19 20:57:47 +0000 | [diff] [blame] | 673 | iommu_put_resv_regions(dev, &mappings); |
Joerg Roedel | beed282 | 2015-05-28 18:41:34 +0200 | [diff] [blame] | 674 | |
| 675 | return ret; |
| 676 | } |
| 677 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 678 | /** |
| 679 | * iommu_group_add_device - add a device to an iommu group |
| 680 | * @group: the group into which to add the device (reference should be held) |
| 681 | * @dev: the device |
| 682 | * |
| 683 | * This function is called by an iommu driver to add a device into a |
| 684 | * group. Adding a device increments the group reference count. |
| 685 | */ |
| 686 | int iommu_group_add_device(struct iommu_group *group, struct device *dev) |
| 687 | { |
| 688 | int ret, i = 0; |
Joerg Roedel | c09e22d | 2017-02-01 12:19:46 +0100 | [diff] [blame] | 689 | struct group_device *device; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 690 | |
| 691 | device = kzalloc(sizeof(*device), GFP_KERNEL); |
| 692 | if (!device) |
| 693 | return -ENOMEM; |
| 694 | |
| 695 | device->dev = dev; |
| 696 | |
| 697 | ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group"); |
Robin Murphy | 797a8b4 | 2017-01-16 12:58:07 +0000 | [diff] [blame] | 698 | if (ret) |
| 699 | goto err_free_device; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 700 | |
| 701 | device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj)); |
| 702 | rename: |
| 703 | if (!device->name) { |
Robin Murphy | 797a8b4 | 2017-01-16 12:58:07 +0000 | [diff] [blame] | 704 | ret = -ENOMEM; |
| 705 | goto err_remove_link; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | ret = sysfs_create_link_nowarn(group->devices_kobj, |
| 709 | &dev->kobj, device->name); |
| 710 | if (ret) { |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 711 | if (ret == -EEXIST && i >= 0) { |
| 712 | /* |
| 713 | * Account for the slim chance of collision |
| 714 | * and append an instance to the name. |
| 715 | */ |
Robin Murphy | 797a8b4 | 2017-01-16 12:58:07 +0000 | [diff] [blame] | 716 | kfree(device->name); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 717 | device->name = kasprintf(GFP_KERNEL, "%s.%d", |
| 718 | kobject_name(&dev->kobj), i++); |
| 719 | goto rename; |
| 720 | } |
Robin Murphy | 797a8b4 | 2017-01-16 12:58:07 +0000 | [diff] [blame] | 721 | goto err_free_name; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | kobject_get(group->devices_kobj); |
| 725 | |
| 726 | dev->iommu_group = group; |
| 727 | |
Joerg Roedel | beed282 | 2015-05-28 18:41:34 +0200 | [diff] [blame] | 728 | iommu_group_create_direct_mappings(group, dev); |
| 729 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 730 | mutex_lock(&group->mutex); |
| 731 | list_add_tail(&device->list, &group->devices); |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 732 | if (group->domain) |
Robin Murphy | 797a8b4 | 2017-01-16 12:58:07 +0000 | [diff] [blame] | 733 | ret = __iommu_attach_device(group->domain, dev); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 734 | mutex_unlock(&group->mutex); |
Robin Murphy | 797a8b4 | 2017-01-16 12:58:07 +0000 | [diff] [blame] | 735 | if (ret) |
| 736 | goto err_put_group; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 737 | |
| 738 | /* Notify any listeners about change to group. */ |
| 739 | blocking_notifier_call_chain(&group->notifier, |
| 740 | IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev); |
Shuah Khan | d1cf7e8 | 2013-08-15 11:59:24 -0600 | [diff] [blame] | 741 | |
| 742 | trace_add_device_to_group(group->id, dev); |
Joerg Roedel | 269aa80 | 2015-05-28 18:41:25 +0200 | [diff] [blame] | 743 | |
Bjorn Helgaas | 780da9e | 2019-02-08 16:05:45 -0600 | [diff] [blame] | 744 | dev_info(dev, "Adding to iommu group %d\n", group->id); |
Joerg Roedel | 269aa80 | 2015-05-28 18:41:25 +0200 | [diff] [blame] | 745 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 746 | return 0; |
Robin Murphy | 797a8b4 | 2017-01-16 12:58:07 +0000 | [diff] [blame] | 747 | |
| 748 | err_put_group: |
| 749 | mutex_lock(&group->mutex); |
| 750 | list_del(&device->list); |
| 751 | mutex_unlock(&group->mutex); |
| 752 | dev->iommu_group = NULL; |
| 753 | kobject_put(group->devices_kobj); |
| 754 | err_free_name: |
| 755 | kfree(device->name); |
| 756 | err_remove_link: |
| 757 | sysfs_remove_link(&dev->kobj, "iommu_group"); |
| 758 | err_free_device: |
| 759 | kfree(device); |
Bjorn Helgaas | 780da9e | 2019-02-08 16:05:45 -0600 | [diff] [blame] | 760 | dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret); |
Robin Murphy | 797a8b4 | 2017-01-16 12:58:07 +0000 | [diff] [blame] | 761 | return ret; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 762 | } |
| 763 | EXPORT_SYMBOL_GPL(iommu_group_add_device); |
| 764 | |
| 765 | /** |
| 766 | * iommu_group_remove_device - remove a device from it's current group |
| 767 | * @dev: device to be removed |
| 768 | * |
| 769 | * This function is called by an iommu driver to remove the device from |
| 770 | * it's current group. This decrements the iommu group reference count. |
| 771 | */ |
| 772 | void iommu_group_remove_device(struct device *dev) |
| 773 | { |
| 774 | struct iommu_group *group = dev->iommu_group; |
Joerg Roedel | c09e22d | 2017-02-01 12:19:46 +0100 | [diff] [blame] | 775 | struct group_device *tmp_device, *device = NULL; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 776 | |
Bjorn Helgaas | 780da9e | 2019-02-08 16:05:45 -0600 | [diff] [blame] | 777 | dev_info(dev, "Removing from iommu group %d\n", group->id); |
Joerg Roedel | 269aa80 | 2015-05-28 18:41:25 +0200 | [diff] [blame] | 778 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 779 | /* Pre-notify listeners that a device is being removed. */ |
| 780 | blocking_notifier_call_chain(&group->notifier, |
| 781 | IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev); |
| 782 | |
| 783 | mutex_lock(&group->mutex); |
| 784 | list_for_each_entry(tmp_device, &group->devices, list) { |
| 785 | if (tmp_device->dev == dev) { |
| 786 | device = tmp_device; |
| 787 | list_del(&device->list); |
| 788 | break; |
| 789 | } |
| 790 | } |
| 791 | mutex_unlock(&group->mutex); |
| 792 | |
| 793 | if (!device) |
| 794 | return; |
| 795 | |
| 796 | sysfs_remove_link(group->devices_kobj, device->name); |
| 797 | sysfs_remove_link(&dev->kobj, "iommu_group"); |
| 798 | |
Shuah Khan | 2e75708 | 2013-08-15 11:59:25 -0600 | [diff] [blame] | 799 | trace_remove_device_from_group(group->id, dev); |
| 800 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 801 | kfree(device->name); |
| 802 | kfree(device); |
| 803 | dev->iommu_group = NULL; |
| 804 | kobject_put(group->devices_kobj); |
| 805 | } |
| 806 | EXPORT_SYMBOL_GPL(iommu_group_remove_device); |
| 807 | |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 808 | static int iommu_group_device_count(struct iommu_group *group) |
| 809 | { |
Joerg Roedel | c09e22d | 2017-02-01 12:19:46 +0100 | [diff] [blame] | 810 | struct group_device *entry; |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 811 | int ret = 0; |
| 812 | |
| 813 | list_for_each_entry(entry, &group->devices, list) |
| 814 | ret++; |
| 815 | |
| 816 | return ret; |
| 817 | } |
| 818 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 819 | /** |
| 820 | * iommu_group_for_each_dev - iterate over each device in the group |
| 821 | * @group: the group |
| 822 | * @data: caller opaque data to be passed to callback function |
| 823 | * @fn: caller supplied callback function |
| 824 | * |
| 825 | * This function is called by group users to iterate over group devices. |
| 826 | * Callers should hold a reference count to the group during callback. |
| 827 | * The group->mutex is held across callbacks, which will block calls to |
| 828 | * iommu_group_add/remove_device. |
| 829 | */ |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 830 | static int __iommu_group_for_each_dev(struct iommu_group *group, void *data, |
| 831 | int (*fn)(struct device *, void *)) |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 832 | { |
Joerg Roedel | c09e22d | 2017-02-01 12:19:46 +0100 | [diff] [blame] | 833 | struct group_device *device; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 834 | int ret = 0; |
| 835 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 836 | list_for_each_entry(device, &group->devices, list) { |
| 837 | ret = fn(device->dev, data); |
| 838 | if (ret) |
| 839 | break; |
| 840 | } |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 841 | return ret; |
| 842 | } |
| 843 | |
| 844 | |
| 845 | int iommu_group_for_each_dev(struct iommu_group *group, void *data, |
| 846 | int (*fn)(struct device *, void *)) |
| 847 | { |
| 848 | int ret; |
| 849 | |
| 850 | mutex_lock(&group->mutex); |
| 851 | ret = __iommu_group_for_each_dev(group, data, fn); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 852 | mutex_unlock(&group->mutex); |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 853 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 854 | return ret; |
| 855 | } |
| 856 | EXPORT_SYMBOL_GPL(iommu_group_for_each_dev); |
| 857 | |
| 858 | /** |
| 859 | * iommu_group_get - Return the group for a device and increment reference |
| 860 | * @dev: get the group that this device belongs to |
| 861 | * |
| 862 | * This function is called by iommu drivers and users to get the group |
| 863 | * for the specified device. If found, the group is returned and the group |
| 864 | * reference in incremented, else NULL. |
| 865 | */ |
| 866 | struct iommu_group *iommu_group_get(struct device *dev) |
| 867 | { |
| 868 | struct iommu_group *group = dev->iommu_group; |
| 869 | |
| 870 | if (group) |
| 871 | kobject_get(group->devices_kobj); |
| 872 | |
| 873 | return group; |
| 874 | } |
| 875 | EXPORT_SYMBOL_GPL(iommu_group_get); |
| 876 | |
| 877 | /** |
Robin Murphy | 13f59a7 | 2016-11-11 17:59:21 +0000 | [diff] [blame] | 878 | * iommu_group_ref_get - Increment reference on a group |
| 879 | * @group: the group to use, must not be NULL |
| 880 | * |
| 881 | * This function is called by iommu drivers to take additional references on an |
| 882 | * existing group. Returns the given group for convenience. |
| 883 | */ |
| 884 | struct iommu_group *iommu_group_ref_get(struct iommu_group *group) |
| 885 | { |
| 886 | kobject_get(group->devices_kobj); |
| 887 | return group; |
| 888 | } |
| 889 | |
| 890 | /** |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 891 | * iommu_group_put - Decrement group reference |
| 892 | * @group: the group to use |
| 893 | * |
| 894 | * This function is called by iommu drivers and users to release the |
| 895 | * iommu group. Once the reference count is zero, the group is released. |
| 896 | */ |
| 897 | void iommu_group_put(struct iommu_group *group) |
| 898 | { |
| 899 | if (group) |
| 900 | kobject_put(group->devices_kobj); |
| 901 | } |
| 902 | EXPORT_SYMBOL_GPL(iommu_group_put); |
| 903 | |
| 904 | /** |
| 905 | * iommu_group_register_notifier - Register a notifier for group changes |
| 906 | * @group: the group to watch |
| 907 | * @nb: notifier block to signal |
| 908 | * |
| 909 | * This function allows iommu group users to track changes in a group. |
| 910 | * See include/linux/iommu.h for actions sent via this notifier. Caller |
| 911 | * should hold a reference to the group throughout notifier registration. |
| 912 | */ |
| 913 | int iommu_group_register_notifier(struct iommu_group *group, |
| 914 | struct notifier_block *nb) |
| 915 | { |
| 916 | return blocking_notifier_chain_register(&group->notifier, nb); |
| 917 | } |
| 918 | EXPORT_SYMBOL_GPL(iommu_group_register_notifier); |
| 919 | |
| 920 | /** |
| 921 | * iommu_group_unregister_notifier - Unregister a notifier |
| 922 | * @group: the group to watch |
| 923 | * @nb: notifier block to signal |
| 924 | * |
| 925 | * Unregister a previously registered group notifier block. |
| 926 | */ |
| 927 | int iommu_group_unregister_notifier(struct iommu_group *group, |
| 928 | struct notifier_block *nb) |
| 929 | { |
| 930 | return blocking_notifier_chain_unregister(&group->notifier, nb); |
| 931 | } |
| 932 | EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier); |
| 933 | |
| 934 | /** |
Jacob Pan | 0c830e6 | 2019-06-03 15:57:48 +0100 | [diff] [blame] | 935 | * iommu_register_device_fault_handler() - Register a device fault handler |
| 936 | * @dev: the device |
| 937 | * @handler: the fault handler |
| 938 | * @data: private data passed as argument to the handler |
| 939 | * |
| 940 | * When an IOMMU fault event is received, this handler gets called with the |
Jean-Philippe Brucker | bf3255b | 2019-06-03 15:57:49 +0100 | [diff] [blame] | 941 | * fault event and data as argument. The handler should return 0 on success. If |
| 942 | * the fault is recoverable (IOMMU_FAULT_PAGE_REQ), the consumer should also |
| 943 | * complete the fault by calling iommu_page_response() with one of the following |
| 944 | * response code: |
| 945 | * - IOMMU_PAGE_RESP_SUCCESS: retry the translation |
| 946 | * - IOMMU_PAGE_RESP_INVALID: terminate the fault |
| 947 | * - IOMMU_PAGE_RESP_FAILURE: terminate the fault and stop reporting |
| 948 | * page faults if possible. |
Jacob Pan | 0c830e6 | 2019-06-03 15:57:48 +0100 | [diff] [blame] | 949 | * |
| 950 | * Return 0 if the fault handler was installed successfully, or an error. |
| 951 | */ |
| 952 | int iommu_register_device_fault_handler(struct device *dev, |
| 953 | iommu_dev_fault_handler_t handler, |
| 954 | void *data) |
| 955 | { |
| 956 | struct iommu_param *param = dev->iommu_param; |
| 957 | int ret = 0; |
| 958 | |
| 959 | if (!param) |
| 960 | return -EINVAL; |
| 961 | |
| 962 | mutex_lock(¶m->lock); |
| 963 | /* Only allow one fault handler registered for each device */ |
| 964 | if (param->fault_param) { |
| 965 | ret = -EBUSY; |
| 966 | goto done_unlock; |
| 967 | } |
| 968 | |
| 969 | get_device(dev); |
| 970 | param->fault_param = kzalloc(sizeof(*param->fault_param), GFP_KERNEL); |
| 971 | if (!param->fault_param) { |
| 972 | put_device(dev); |
| 973 | ret = -ENOMEM; |
| 974 | goto done_unlock; |
| 975 | } |
| 976 | param->fault_param->handler = handler; |
| 977 | param->fault_param->data = data; |
Jean-Philippe Brucker | bf3255b | 2019-06-03 15:57:49 +0100 | [diff] [blame] | 978 | mutex_init(¶m->fault_param->lock); |
| 979 | INIT_LIST_HEAD(¶m->fault_param->faults); |
Jacob Pan | 0c830e6 | 2019-06-03 15:57:48 +0100 | [diff] [blame] | 980 | |
| 981 | done_unlock: |
| 982 | mutex_unlock(¶m->lock); |
| 983 | |
| 984 | return ret; |
| 985 | } |
| 986 | EXPORT_SYMBOL_GPL(iommu_register_device_fault_handler); |
| 987 | |
| 988 | /** |
| 989 | * iommu_unregister_device_fault_handler() - Unregister the device fault handler |
| 990 | * @dev: the device |
| 991 | * |
| 992 | * Remove the device fault handler installed with |
| 993 | * iommu_register_device_fault_handler(). |
| 994 | * |
| 995 | * Return 0 on success, or an error. |
| 996 | */ |
| 997 | int iommu_unregister_device_fault_handler(struct device *dev) |
| 998 | { |
| 999 | struct iommu_param *param = dev->iommu_param; |
| 1000 | int ret = 0; |
| 1001 | |
| 1002 | if (!param) |
| 1003 | return -EINVAL; |
| 1004 | |
| 1005 | mutex_lock(¶m->lock); |
| 1006 | |
| 1007 | if (!param->fault_param) |
| 1008 | goto unlock; |
| 1009 | |
Jean-Philippe Brucker | bf3255b | 2019-06-03 15:57:49 +0100 | [diff] [blame] | 1010 | /* we cannot unregister handler if there are pending faults */ |
| 1011 | if (!list_empty(¶m->fault_param->faults)) { |
| 1012 | ret = -EBUSY; |
| 1013 | goto unlock; |
| 1014 | } |
| 1015 | |
Jacob Pan | 0c830e6 | 2019-06-03 15:57:48 +0100 | [diff] [blame] | 1016 | kfree(param->fault_param); |
| 1017 | param->fault_param = NULL; |
| 1018 | put_device(dev); |
| 1019 | unlock: |
| 1020 | mutex_unlock(¶m->lock); |
| 1021 | |
| 1022 | return ret; |
| 1023 | } |
| 1024 | EXPORT_SYMBOL_GPL(iommu_unregister_device_fault_handler); |
| 1025 | |
| 1026 | /** |
| 1027 | * iommu_report_device_fault() - Report fault event to device driver |
| 1028 | * @dev: the device |
| 1029 | * @evt: fault event data |
| 1030 | * |
| 1031 | * Called by IOMMU drivers when a fault is detected, typically in a threaded IRQ |
Jean-Philippe Brucker | bf3255b | 2019-06-03 15:57:49 +0100 | [diff] [blame] | 1032 | * handler. When this function fails and the fault is recoverable, it is the |
| 1033 | * caller's responsibility to complete the fault. |
Jacob Pan | 0c830e6 | 2019-06-03 15:57:48 +0100 | [diff] [blame] | 1034 | * |
| 1035 | * Return 0 on success, or an error. |
| 1036 | */ |
| 1037 | int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt) |
| 1038 | { |
| 1039 | struct iommu_param *param = dev->iommu_param; |
Jean-Philippe Brucker | bf3255b | 2019-06-03 15:57:49 +0100 | [diff] [blame] | 1040 | struct iommu_fault_event *evt_pending = NULL; |
Jacob Pan | 0c830e6 | 2019-06-03 15:57:48 +0100 | [diff] [blame] | 1041 | struct iommu_fault_param *fparam; |
| 1042 | int ret = 0; |
| 1043 | |
| 1044 | if (!param || !evt) |
| 1045 | return -EINVAL; |
| 1046 | |
| 1047 | /* we only report device fault if there is a handler registered */ |
| 1048 | mutex_lock(¶m->lock); |
| 1049 | fparam = param->fault_param; |
| 1050 | if (!fparam || !fparam->handler) { |
| 1051 | ret = -EINVAL; |
| 1052 | goto done_unlock; |
| 1053 | } |
Jean-Philippe Brucker | bf3255b | 2019-06-03 15:57:49 +0100 | [diff] [blame] | 1054 | |
| 1055 | if (evt->fault.type == IOMMU_FAULT_PAGE_REQ && |
| 1056 | (evt->fault.prm.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) { |
| 1057 | evt_pending = kmemdup(evt, sizeof(struct iommu_fault_event), |
| 1058 | GFP_KERNEL); |
| 1059 | if (!evt_pending) { |
| 1060 | ret = -ENOMEM; |
| 1061 | goto done_unlock; |
| 1062 | } |
| 1063 | mutex_lock(&fparam->lock); |
| 1064 | list_add_tail(&evt_pending->list, &fparam->faults); |
| 1065 | mutex_unlock(&fparam->lock); |
| 1066 | } |
| 1067 | |
Jacob Pan | 0c830e6 | 2019-06-03 15:57:48 +0100 | [diff] [blame] | 1068 | ret = fparam->handler(&evt->fault, fparam->data); |
Jean-Philippe Brucker | bf3255b | 2019-06-03 15:57:49 +0100 | [diff] [blame] | 1069 | if (ret && evt_pending) { |
| 1070 | mutex_lock(&fparam->lock); |
| 1071 | list_del(&evt_pending->list); |
| 1072 | mutex_unlock(&fparam->lock); |
| 1073 | kfree(evt_pending); |
| 1074 | } |
Jacob Pan | 0c830e6 | 2019-06-03 15:57:48 +0100 | [diff] [blame] | 1075 | done_unlock: |
| 1076 | mutex_unlock(¶m->lock); |
| 1077 | return ret; |
| 1078 | } |
| 1079 | EXPORT_SYMBOL_GPL(iommu_report_device_fault); |
| 1080 | |
Jean-Philippe Brucker | bf3255b | 2019-06-03 15:57:49 +0100 | [diff] [blame] | 1081 | int iommu_page_response(struct device *dev, |
| 1082 | struct iommu_page_response *msg) |
| 1083 | { |
| 1084 | bool pasid_valid; |
| 1085 | int ret = -EINVAL; |
| 1086 | struct iommu_fault_event *evt; |
| 1087 | struct iommu_fault_page_request *prm; |
| 1088 | struct iommu_param *param = dev->iommu_param; |
| 1089 | struct iommu_domain *domain = iommu_get_domain_for_dev(dev); |
| 1090 | |
| 1091 | if (!domain || !domain->ops->page_response) |
| 1092 | return -ENODEV; |
| 1093 | |
| 1094 | if (!param || !param->fault_param) |
| 1095 | return -EINVAL; |
| 1096 | |
| 1097 | if (msg->version != IOMMU_PAGE_RESP_VERSION_1 || |
| 1098 | msg->flags & ~IOMMU_PAGE_RESP_PASID_VALID) |
| 1099 | return -EINVAL; |
| 1100 | |
| 1101 | /* Only send response if there is a fault report pending */ |
| 1102 | mutex_lock(¶m->fault_param->lock); |
| 1103 | if (list_empty(¶m->fault_param->faults)) { |
| 1104 | dev_warn_ratelimited(dev, "no pending PRQ, drop response\n"); |
| 1105 | goto done_unlock; |
| 1106 | } |
| 1107 | /* |
| 1108 | * Check if we have a matching page request pending to respond, |
| 1109 | * otherwise return -EINVAL |
| 1110 | */ |
| 1111 | list_for_each_entry(evt, ¶m->fault_param->faults, list) { |
| 1112 | prm = &evt->fault.prm; |
| 1113 | pasid_valid = prm->flags & IOMMU_FAULT_PAGE_REQUEST_PASID_VALID; |
| 1114 | |
| 1115 | if ((pasid_valid && prm->pasid != msg->pasid) || |
| 1116 | prm->grpid != msg->grpid) |
| 1117 | continue; |
| 1118 | |
| 1119 | /* Sanitize the reply */ |
| 1120 | msg->flags = pasid_valid ? IOMMU_PAGE_RESP_PASID_VALID : 0; |
| 1121 | |
| 1122 | ret = domain->ops->page_response(dev, evt, msg); |
| 1123 | list_del(&evt->list); |
| 1124 | kfree(evt); |
| 1125 | break; |
| 1126 | } |
| 1127 | |
| 1128 | done_unlock: |
| 1129 | mutex_unlock(¶m->fault_param->lock); |
| 1130 | return ret; |
| 1131 | } |
| 1132 | EXPORT_SYMBOL_GPL(iommu_page_response); |
| 1133 | |
Jacob Pan | 0c830e6 | 2019-06-03 15:57:48 +0100 | [diff] [blame] | 1134 | /** |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1135 | * iommu_group_id - Return ID for a group |
| 1136 | * @group: the group to ID |
| 1137 | * |
| 1138 | * Return the unique ID for the group matching the sysfs group number. |
| 1139 | */ |
| 1140 | int iommu_group_id(struct iommu_group *group) |
| 1141 | { |
| 1142 | return group->id; |
| 1143 | } |
| 1144 | EXPORT_SYMBOL_GPL(iommu_group_id); |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 1145 | |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 1146 | static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev, |
| 1147 | unsigned long *devfns); |
| 1148 | |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 1149 | /* |
| 1150 | * To consider a PCI device isolated, we require ACS to support Source |
| 1151 | * Validation, Request Redirection, Completer Redirection, and Upstream |
| 1152 | * Forwarding. This effectively means that devices cannot spoof their |
| 1153 | * requester ID, requests and completions cannot be redirected, and all |
| 1154 | * transactions are forwarded upstream, even as it passes through a |
| 1155 | * bridge where the target device is downstream. |
| 1156 | */ |
| 1157 | #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF) |
| 1158 | |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 1159 | /* |
| 1160 | * For multifunction devices which are not isolated from each other, find |
| 1161 | * all the other non-isolated functions and look for existing groups. For |
| 1162 | * each function, we also need to look for aliases to or from other devices |
| 1163 | * that may already have a group. |
| 1164 | */ |
| 1165 | static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev, |
| 1166 | unsigned long *devfns) |
| 1167 | { |
| 1168 | struct pci_dev *tmp = NULL; |
| 1169 | struct iommu_group *group; |
| 1170 | |
| 1171 | if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS)) |
| 1172 | return NULL; |
| 1173 | |
| 1174 | for_each_pci_dev(tmp) { |
| 1175 | if (tmp == pdev || tmp->bus != pdev->bus || |
| 1176 | PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) || |
| 1177 | pci_acs_enabled(tmp, REQ_ACS_FLAGS)) |
| 1178 | continue; |
| 1179 | |
| 1180 | group = get_pci_alias_group(tmp, devfns); |
| 1181 | if (group) { |
| 1182 | pci_dev_put(tmp); |
| 1183 | return group; |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | return NULL; |
| 1188 | } |
| 1189 | |
| 1190 | /* |
Jacek Lawrynowicz | 338c314 | 2016-03-03 15:38:02 +0100 | [diff] [blame] | 1191 | * Look for aliases to or from the given device for existing groups. DMA |
| 1192 | * aliases are only supported on the same bus, therefore the search |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 1193 | * space is quite small (especially since we're really only looking at pcie |
| 1194 | * device, and therefore only expect multiple slots on the root complex or |
| 1195 | * downstream switch ports). It's conceivable though that a pair of |
| 1196 | * multifunction devices could have aliases between them that would cause a |
| 1197 | * loop. To prevent this, we use a bitmap to track where we've been. |
| 1198 | */ |
| 1199 | static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev, |
| 1200 | unsigned long *devfns) |
| 1201 | { |
| 1202 | struct pci_dev *tmp = NULL; |
| 1203 | struct iommu_group *group; |
| 1204 | |
| 1205 | if (test_and_set_bit(pdev->devfn & 0xff, devfns)) |
| 1206 | return NULL; |
| 1207 | |
| 1208 | group = iommu_group_get(&pdev->dev); |
| 1209 | if (group) |
| 1210 | return group; |
| 1211 | |
| 1212 | for_each_pci_dev(tmp) { |
| 1213 | if (tmp == pdev || tmp->bus != pdev->bus) |
| 1214 | continue; |
| 1215 | |
| 1216 | /* We alias them or they alias us */ |
Jacek Lawrynowicz | 338c314 | 2016-03-03 15:38:02 +0100 | [diff] [blame] | 1217 | if (pci_devs_are_dma_aliases(pdev, tmp)) { |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 1218 | group = get_pci_alias_group(tmp, devfns); |
| 1219 | if (group) { |
| 1220 | pci_dev_put(tmp); |
| 1221 | return group; |
| 1222 | } |
| 1223 | |
| 1224 | group = get_pci_function_alias_group(tmp, devfns); |
| 1225 | if (group) { |
| 1226 | pci_dev_put(tmp); |
| 1227 | return group; |
| 1228 | } |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | return NULL; |
| 1233 | } |
| 1234 | |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 1235 | struct group_for_pci_data { |
| 1236 | struct pci_dev *pdev; |
| 1237 | struct iommu_group *group; |
| 1238 | }; |
| 1239 | |
| 1240 | /* |
| 1241 | * DMA alias iterator callback, return the last seen device. Stop and return |
| 1242 | * the IOMMU group if we find one along the way. |
| 1243 | */ |
| 1244 | static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque) |
| 1245 | { |
| 1246 | struct group_for_pci_data *data = opaque; |
| 1247 | |
| 1248 | data->pdev = pdev; |
| 1249 | data->group = iommu_group_get(&pdev->dev); |
| 1250 | |
| 1251 | return data->group != NULL; |
| 1252 | } |
| 1253 | |
| 1254 | /* |
Joerg Roedel | 6eab556 | 2015-10-21 23:51:38 +0200 | [diff] [blame] | 1255 | * Generic device_group call-back function. It just allocates one |
| 1256 | * iommu-group per device. |
| 1257 | */ |
| 1258 | struct iommu_group *generic_device_group(struct device *dev) |
| 1259 | { |
Joerg Roedel | 7f7a230 | 2017-06-28 12:45:31 +0200 | [diff] [blame] | 1260 | return iommu_group_alloc(); |
Joerg Roedel | 6eab556 | 2015-10-21 23:51:38 +0200 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | /* |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 1264 | * Use standard PCI bus topology, isolation features, and DMA alias quirks |
| 1265 | * to find or create an IOMMU group for a device. |
| 1266 | */ |
Joerg Roedel | 5e62292 | 2015-10-21 23:51:37 +0200 | [diff] [blame] | 1267 | struct iommu_group *pci_device_group(struct device *dev) |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 1268 | { |
Joerg Roedel | 5e62292 | 2015-10-21 23:51:37 +0200 | [diff] [blame] | 1269 | struct pci_dev *pdev = to_pci_dev(dev); |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 1270 | struct group_for_pci_data data; |
| 1271 | struct pci_bus *bus; |
| 1272 | struct iommu_group *group = NULL; |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 1273 | u64 devfns[4] = { 0 }; |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 1274 | |
Joerg Roedel | 5e62292 | 2015-10-21 23:51:37 +0200 | [diff] [blame] | 1275 | if (WARN_ON(!dev_is_pci(dev))) |
| 1276 | return ERR_PTR(-EINVAL); |
| 1277 | |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 1278 | /* |
| 1279 | * Find the upstream DMA alias for the device. A device must not |
| 1280 | * be aliased due to topology in order to have its own IOMMU group. |
| 1281 | * If we find an alias along the way that already belongs to a |
| 1282 | * group, use it. |
| 1283 | */ |
| 1284 | if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data)) |
| 1285 | return data.group; |
| 1286 | |
| 1287 | pdev = data.pdev; |
| 1288 | |
| 1289 | /* |
| 1290 | * Continue upstream from the point of minimum IOMMU granularity |
| 1291 | * due to aliases to the point where devices are protected from |
| 1292 | * peer-to-peer DMA by PCI ACS. Again, if we find an existing |
| 1293 | * group, use it. |
| 1294 | */ |
| 1295 | for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) { |
| 1296 | if (!bus->self) |
| 1297 | continue; |
| 1298 | |
| 1299 | if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS)) |
| 1300 | break; |
| 1301 | |
| 1302 | pdev = bus->self; |
| 1303 | |
| 1304 | group = iommu_group_get(&pdev->dev); |
| 1305 | if (group) |
| 1306 | return group; |
| 1307 | } |
| 1308 | |
| 1309 | /* |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 1310 | * Look for existing groups on device aliases. If we alias another |
| 1311 | * device or another device aliases us, use the same group. |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 1312 | */ |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 1313 | group = get_pci_alias_group(pdev, (unsigned long *)devfns); |
| 1314 | if (group) |
| 1315 | return group; |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 1316 | |
| 1317 | /* |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 1318 | * Look for existing groups on non-isolated functions on the same |
| 1319 | * slot and aliases of those funcions, if any. No need to clear |
| 1320 | * the search bitmap, the tested devfns are still valid. |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 1321 | */ |
Alex Williamson | f096c06 | 2014-09-19 10:03:06 -0600 | [diff] [blame] | 1322 | group = get_pci_function_alias_group(pdev, (unsigned long *)devfns); |
| 1323 | if (group) |
| 1324 | return group; |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 1325 | |
| 1326 | /* No shared group found, allocate new */ |
Joerg Roedel | 7f7a230 | 2017-06-28 12:45:31 +0200 | [diff] [blame] | 1327 | return iommu_group_alloc(); |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 1328 | } |
| 1329 | |
Nipun Gupta | eab03e2 | 2018-09-10 19:19:18 +0530 | [diff] [blame] | 1330 | /* Get the IOMMU group for device on fsl-mc bus */ |
| 1331 | struct iommu_group *fsl_mc_device_group(struct device *dev) |
| 1332 | { |
| 1333 | struct device *cont_dev = fsl_mc_cont_dev(dev); |
| 1334 | struct iommu_group *group; |
| 1335 | |
| 1336 | group = iommu_group_get(cont_dev); |
| 1337 | if (!group) |
| 1338 | group = iommu_group_alloc(); |
| 1339 | return group; |
| 1340 | } |
| 1341 | |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 1342 | /** |
| 1343 | * iommu_group_get_for_dev - Find or create the IOMMU group for a device |
| 1344 | * @dev: target device |
| 1345 | * |
| 1346 | * This function is intended to be called by IOMMU drivers and extended to |
| 1347 | * support common, bus-defined algorithms when determining or creating the |
| 1348 | * IOMMU group for a device. On success, the caller will hold a reference |
| 1349 | * to the returned IOMMU group, which will already include the provided |
| 1350 | * device. The reference should be released with iommu_group_put(). |
| 1351 | */ |
| 1352 | struct iommu_group *iommu_group_get_for_dev(struct device *dev) |
| 1353 | { |
Joerg Roedel | 46c6b2b | 2015-10-21 23:51:36 +0200 | [diff] [blame] | 1354 | const struct iommu_ops *ops = dev->bus->iommu_ops; |
Joerg Roedel | c4a783b | 2014-08-21 22:32:08 +0200 | [diff] [blame] | 1355 | struct iommu_group *group; |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 1356 | int ret; |
| 1357 | |
| 1358 | group = iommu_group_get(dev); |
| 1359 | if (group) |
| 1360 | return group; |
| 1361 | |
Robin Murphy | 05f80300 | 2017-07-21 13:12:38 +0100 | [diff] [blame] | 1362 | if (!ops) |
| 1363 | return ERR_PTR(-EINVAL); |
Joerg Roedel | c4a783b | 2014-08-21 22:32:08 +0200 | [diff] [blame] | 1364 | |
Robin Murphy | 05f80300 | 2017-07-21 13:12:38 +0100 | [diff] [blame] | 1365 | group = ops->device_group(dev); |
Joerg Roedel | 72dcac6 | 2017-06-28 12:52:48 +0200 | [diff] [blame] | 1366 | if (WARN_ON_ONCE(group == NULL)) |
| 1367 | return ERR_PTR(-EINVAL); |
| 1368 | |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 1369 | if (IS_ERR(group)) |
| 1370 | return group; |
| 1371 | |
Joerg Roedel | 1228236 | 2015-10-21 23:51:43 +0200 | [diff] [blame] | 1372 | /* |
| 1373 | * Try to allocate a default domain - needs support from the |
| 1374 | * IOMMU driver. |
| 1375 | */ |
| 1376 | if (!group->default_domain) { |
Will Deacon | fccb4e3 | 2017-01-05 18:38:26 +0000 | [diff] [blame] | 1377 | struct iommu_domain *dom; |
| 1378 | |
| 1379 | dom = __iommu_domain_alloc(dev->bus, iommu_def_domain_type); |
| 1380 | if (!dom && iommu_def_domain_type != IOMMU_DOMAIN_DMA) { |
Will Deacon | fccb4e3 | 2017-01-05 18:38:26 +0000 | [diff] [blame] | 1381 | dom = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_DMA); |
Joerg Roedel | 8bc32a2 | 2019-03-22 16:52:17 +0100 | [diff] [blame] | 1382 | if (dom) { |
| 1383 | dev_warn(dev, |
| 1384 | "failed to allocate default IOMMU domain of type %u; falling back to IOMMU_DOMAIN_DMA", |
| 1385 | iommu_def_domain_type); |
| 1386 | } |
Will Deacon | fccb4e3 | 2017-01-05 18:38:26 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | group->default_domain = dom; |
Joerg Roedel | eebb803 | 2016-04-04 15:47:48 +0200 | [diff] [blame] | 1390 | if (!group->domain) |
Will Deacon | fccb4e3 | 2017-01-05 18:38:26 +0000 | [diff] [blame] | 1391 | group->domain = dom; |
Zhen Lei | 68a6efe | 2018-09-20 17:10:23 +0100 | [diff] [blame] | 1392 | |
| 1393 | if (dom && !iommu_dma_strict) { |
| 1394 | int attr = 1; |
| 1395 | iommu_domain_set_attr(dom, |
| 1396 | DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE, |
| 1397 | &attr); |
| 1398 | } |
Joerg Roedel | 1228236 | 2015-10-21 23:51:43 +0200 | [diff] [blame] | 1399 | } |
| 1400 | |
Alex Williamson | 104a1c1 | 2014-07-03 09:51:18 -0600 | [diff] [blame] | 1401 | ret = iommu_group_add_device(group, dev); |
| 1402 | if (ret) { |
| 1403 | iommu_group_put(group); |
| 1404 | return ERR_PTR(ret); |
| 1405 | } |
| 1406 | |
| 1407 | return group; |
| 1408 | } |
| 1409 | |
Joerg Roedel | 6827ca8 | 2015-05-28 18:41:35 +0200 | [diff] [blame] | 1410 | struct iommu_domain *iommu_group_default_domain(struct iommu_group *group) |
| 1411 | { |
| 1412 | return group->default_domain; |
| 1413 | } |
| 1414 | |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 1415 | static int add_iommu_group(struct device *dev, void *data) |
| 1416 | { |
Joerg Roedel | cc5aed4 | 2018-11-30 10:31:59 +0100 | [diff] [blame] | 1417 | int ret = iommu_probe_device(dev); |
Joerg Roedel | 38667f1 | 2015-06-29 10:16:08 +0200 | [diff] [blame] | 1418 | |
| 1419 | /* |
| 1420 | * We ignore -ENODEV errors for now, as they just mean that the |
| 1421 | * device is not translated by an IOMMU. We still care about |
| 1422 | * other errors and fail to initialize when they happen. |
| 1423 | */ |
| 1424 | if (ret == -ENODEV) |
| 1425 | ret = 0; |
| 1426 | |
| 1427 | return ret; |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 1428 | } |
| 1429 | |
Joerg Roedel | 8da3014 | 2015-05-28 18:41:27 +0200 | [diff] [blame] | 1430 | static int remove_iommu_group(struct device *dev, void *data) |
| 1431 | { |
Joerg Roedel | cc5aed4 | 2018-11-30 10:31:59 +0100 | [diff] [blame] | 1432 | iommu_release_device(dev); |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 1433 | |
| 1434 | return 0; |
| 1435 | } |
| 1436 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1437 | static int iommu_bus_notifier(struct notifier_block *nb, |
| 1438 | unsigned long action, void *data) |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 1439 | { |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1440 | unsigned long group_action = 0; |
Joerg Roedel | cc5aed4 | 2018-11-30 10:31:59 +0100 | [diff] [blame] | 1441 | struct device *dev = data; |
| 1442 | struct iommu_group *group; |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 1443 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1444 | /* |
| 1445 | * ADD/DEL call into iommu driver ops if provided, which may |
| 1446 | * result in ADD/DEL notifiers to group->notifier |
| 1447 | */ |
| 1448 | if (action == BUS_NOTIFY_ADD_DEVICE) { |
Joerg Roedel | cc5aed4 | 2018-11-30 10:31:59 +0100 | [diff] [blame] | 1449 | int ret; |
zhichang.yuan | 3ba8775 | 2017-04-18 20:51:48 +0800 | [diff] [blame] | 1450 | |
Joerg Roedel | cc5aed4 | 2018-11-30 10:31:59 +0100 | [diff] [blame] | 1451 | ret = iommu_probe_device(dev); |
| 1452 | return (ret) ? NOTIFY_DONE : NOTIFY_OK; |
Joerg Roedel | 843cb6d | 2015-05-28 18:41:28 +0200 | [diff] [blame] | 1453 | } else if (action == BUS_NOTIFY_REMOVED_DEVICE) { |
Joerg Roedel | cc5aed4 | 2018-11-30 10:31:59 +0100 | [diff] [blame] | 1454 | iommu_release_device(dev); |
| 1455 | return NOTIFY_OK; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1456 | } |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 1457 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1458 | /* |
| 1459 | * Remaining BUS_NOTIFYs get filtered and republished to the |
| 1460 | * group, if anyone is listening |
| 1461 | */ |
| 1462 | group = iommu_group_get(dev); |
| 1463 | if (!group) |
| 1464 | return 0; |
| 1465 | |
| 1466 | switch (action) { |
| 1467 | case BUS_NOTIFY_BIND_DRIVER: |
| 1468 | group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER; |
| 1469 | break; |
| 1470 | case BUS_NOTIFY_BOUND_DRIVER: |
| 1471 | group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER; |
| 1472 | break; |
| 1473 | case BUS_NOTIFY_UNBIND_DRIVER: |
| 1474 | group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER; |
| 1475 | break; |
| 1476 | case BUS_NOTIFY_UNBOUND_DRIVER: |
| 1477 | group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER; |
| 1478 | break; |
| 1479 | } |
| 1480 | |
| 1481 | if (group_action) |
| 1482 | blocking_notifier_call_chain(&group->notifier, |
| 1483 | group_action, dev); |
| 1484 | |
| 1485 | iommu_group_put(group); |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 1486 | return 0; |
| 1487 | } |
| 1488 | |
Mark Salter | fb3e306 | 2014-09-21 13:58:24 -0400 | [diff] [blame] | 1489 | static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1490 | { |
Mark Salter | fb3e306 | 2014-09-21 13:58:24 -0400 | [diff] [blame] | 1491 | int err; |
| 1492 | struct notifier_block *nb; |
Thierry Reding | b22f643 | 2014-06-27 09:03:12 +0200 | [diff] [blame] | 1493 | |
Mark Salter | fb3e306 | 2014-09-21 13:58:24 -0400 | [diff] [blame] | 1494 | nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL); |
| 1495 | if (!nb) |
| 1496 | return -ENOMEM; |
| 1497 | |
| 1498 | nb->notifier_call = iommu_bus_notifier; |
| 1499 | |
| 1500 | err = bus_register_notifier(bus, nb); |
Joerg Roedel | 8da3014 | 2015-05-28 18:41:27 +0200 | [diff] [blame] | 1501 | if (err) |
| 1502 | goto out_free; |
Heiko Stübner | d7da6bd | 2014-10-29 01:22:56 +0100 | [diff] [blame] | 1503 | |
Lu Baolu | 8cec63e | 2019-03-20 09:40:24 +0800 | [diff] [blame] | 1504 | err = bus_for_each_dev(bus, NULL, NULL, add_iommu_group); |
Joerg Roedel | 8da3014 | 2015-05-28 18:41:27 +0200 | [diff] [blame] | 1505 | if (err) |
| 1506 | goto out_err; |
| 1507 | |
Heiko Stübner | d7da6bd | 2014-10-29 01:22:56 +0100 | [diff] [blame] | 1508 | |
| 1509 | return 0; |
Joerg Roedel | 8da3014 | 2015-05-28 18:41:27 +0200 | [diff] [blame] | 1510 | |
| 1511 | out_err: |
| 1512 | /* Clean up */ |
Lu Baolu | 8cec63e | 2019-03-20 09:40:24 +0800 | [diff] [blame] | 1513 | bus_for_each_dev(bus, NULL, NULL, remove_iommu_group); |
Joerg Roedel | 8da3014 | 2015-05-28 18:41:27 +0200 | [diff] [blame] | 1514 | bus_unregister_notifier(bus, nb); |
| 1515 | |
| 1516 | out_free: |
| 1517 | kfree(nb); |
| 1518 | |
| 1519 | return err; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1520 | } |
| 1521 | |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 1522 | /** |
| 1523 | * bus_set_iommu - set iommu-callbacks for the bus |
| 1524 | * @bus: bus. |
| 1525 | * @ops: the callbacks provided by the iommu-driver |
| 1526 | * |
| 1527 | * This function is called by an iommu driver to set the iommu methods |
| 1528 | * used for a particular bus. Drivers for devices on that bus can use |
| 1529 | * the iommu-api after these ops are registered. |
| 1530 | * This special function is needed because IOMMUs are usually devices on |
| 1531 | * the bus itself, so the iommu drivers are not initialized when the bus |
| 1532 | * is set up. With this function the iommu-driver can set the iommu-ops |
| 1533 | * afterwards. |
| 1534 | */ |
Thierry Reding | b22f643 | 2014-06-27 09:03:12 +0200 | [diff] [blame] | 1535 | int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1536 | { |
Heiko Stübner | d7da6bd | 2014-10-29 01:22:56 +0100 | [diff] [blame] | 1537 | int err; |
| 1538 | |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 1539 | if (bus->iommu_ops != NULL) |
| 1540 | return -EBUSY; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1541 | |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 1542 | bus->iommu_ops = ops; |
| 1543 | |
| 1544 | /* Do IOMMU specific setup for this bus-type */ |
Heiko Stübner | d7da6bd | 2014-10-29 01:22:56 +0100 | [diff] [blame] | 1545 | err = iommu_bus_init(bus, ops); |
| 1546 | if (err) |
| 1547 | bus->iommu_ops = NULL; |
| 1548 | |
| 1549 | return err; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1550 | } |
Joerg Roedel | ff21776 | 2011-08-26 16:48:26 +0200 | [diff] [blame] | 1551 | EXPORT_SYMBOL_GPL(bus_set_iommu); |
| 1552 | |
Joerg Roedel | a1b60c1 | 2011-09-06 18:46:34 +0200 | [diff] [blame] | 1553 | bool iommu_present(struct bus_type *bus) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1554 | { |
Joerg Roedel | 94441c3 | 2011-09-06 18:58:54 +0200 | [diff] [blame] | 1555 | return bus->iommu_ops != NULL; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1556 | } |
Joerg Roedel | a1b60c1 | 2011-09-06 18:46:34 +0200 | [diff] [blame] | 1557 | EXPORT_SYMBOL_GPL(iommu_present); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1558 | |
Joerg Roedel | 3c0e0ca | 2014-09-03 18:47:25 +0200 | [diff] [blame] | 1559 | bool iommu_capable(struct bus_type *bus, enum iommu_cap cap) |
| 1560 | { |
| 1561 | if (!bus->iommu_ops || !bus->iommu_ops->capable) |
| 1562 | return false; |
| 1563 | |
| 1564 | return bus->iommu_ops->capable(cap); |
| 1565 | } |
| 1566 | EXPORT_SYMBOL_GPL(iommu_capable); |
| 1567 | |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 1568 | /** |
| 1569 | * iommu_set_fault_handler() - set a fault handler for an iommu domain |
| 1570 | * @domain: iommu domain |
| 1571 | * @handler: fault handler |
Ohad Ben-Cohen | 77ca233 | 2012-05-21 20:20:05 +0300 | [diff] [blame] | 1572 | * @token: user data, will be passed back to the fault handler |
Ohad Ben-Cohen | 0ed6d2d | 2011-09-27 07:36:40 -0400 | [diff] [blame] | 1573 | * |
| 1574 | * This function should be used by IOMMU users which want to be notified |
| 1575 | * whenever an IOMMU fault happens. |
| 1576 | * |
| 1577 | * The fault handler itself should return 0 on success, and an appropriate |
| 1578 | * error code otherwise. |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 1579 | */ |
| 1580 | void iommu_set_fault_handler(struct iommu_domain *domain, |
Ohad Ben-Cohen | 77ca233 | 2012-05-21 20:20:05 +0300 | [diff] [blame] | 1581 | iommu_fault_handler_t handler, |
| 1582 | void *token) |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 1583 | { |
| 1584 | BUG_ON(!domain); |
| 1585 | |
| 1586 | domain->handler = handler; |
Ohad Ben-Cohen | 77ca233 | 2012-05-21 20:20:05 +0300 | [diff] [blame] | 1587 | domain->handler_token = token; |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 1588 | } |
Ohad Ben-Cohen | 30bd918 | 2011-09-26 09:11:46 -0400 | [diff] [blame] | 1589 | EXPORT_SYMBOL_GPL(iommu_set_fault_handler); |
Ohad Ben-Cohen | 4f3f8d9 | 2011-09-13 15:25:23 -0400 | [diff] [blame] | 1590 | |
Joerg Roedel | 53723dc | 2015-05-28 18:41:29 +0200 | [diff] [blame] | 1591 | static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus, |
| 1592 | unsigned type) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1593 | { |
| 1594 | struct iommu_domain *domain; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1595 | |
Joerg Roedel | 94441c3 | 2011-09-06 18:58:54 +0200 | [diff] [blame] | 1596 | if (bus == NULL || bus->iommu_ops == NULL) |
Joerg Roedel | 905d66c | 2011-09-06 16:03:26 +0200 | [diff] [blame] | 1597 | return NULL; |
| 1598 | |
Joerg Roedel | 53723dc | 2015-05-28 18:41:29 +0200 | [diff] [blame] | 1599 | domain = bus->iommu_ops->domain_alloc(type); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1600 | if (!domain) |
| 1601 | return NULL; |
| 1602 | |
Joerg Roedel | 8539c7c | 2015-03-26 13:43:05 +0100 | [diff] [blame] | 1603 | domain->ops = bus->iommu_ops; |
Joerg Roedel | 53723dc | 2015-05-28 18:41:29 +0200 | [diff] [blame] | 1604 | domain->type = type; |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 1605 | /* Assume all sizes by default; the driver may override this later */ |
| 1606 | domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap; |
Joerg Roedel | 905d66c | 2011-09-06 16:03:26 +0200 | [diff] [blame] | 1607 | |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1608 | return domain; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1609 | } |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1610 | |
Joerg Roedel | 53723dc | 2015-05-28 18:41:29 +0200 | [diff] [blame] | 1611 | struct iommu_domain *iommu_domain_alloc(struct bus_type *bus) |
| 1612 | { |
| 1613 | return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1614 | } |
| 1615 | EXPORT_SYMBOL_GPL(iommu_domain_alloc); |
| 1616 | |
| 1617 | void iommu_domain_free(struct iommu_domain *domain) |
| 1618 | { |
Joerg Roedel | 89be34a | 2015-03-26 13:43:19 +0100 | [diff] [blame] | 1619 | domain->ops->domain_free(domain); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1620 | } |
| 1621 | EXPORT_SYMBOL_GPL(iommu_domain_free); |
| 1622 | |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1623 | static int __iommu_attach_device(struct iommu_domain *domain, |
| 1624 | struct device *dev) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1625 | { |
Shuah Khan | b54db77 | 2013-08-15 11:59:26 -0600 | [diff] [blame] | 1626 | int ret; |
Baoquan He | e01d191 | 2017-08-09 16:33:40 +0800 | [diff] [blame] | 1627 | if ((domain->ops->is_attach_deferred != NULL) && |
| 1628 | domain->ops->is_attach_deferred(domain, dev)) |
| 1629 | return 0; |
| 1630 | |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 1631 | if (unlikely(domain->ops->attach_dev == NULL)) |
| 1632 | return -ENODEV; |
| 1633 | |
Shuah Khan | b54db77 | 2013-08-15 11:59:26 -0600 | [diff] [blame] | 1634 | ret = domain->ops->attach_dev(domain, dev); |
| 1635 | if (!ret) |
| 1636 | trace_attach_device_to_domain(dev); |
| 1637 | return ret; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1638 | } |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1639 | |
| 1640 | int iommu_attach_device(struct iommu_domain *domain, struct device *dev) |
| 1641 | { |
| 1642 | struct iommu_group *group; |
| 1643 | int ret; |
| 1644 | |
| 1645 | group = iommu_group_get(dev); |
Jordan Crouse | 9ae9df0 | 2017-12-20 09:48:36 -0700 | [diff] [blame] | 1646 | if (!group) |
| 1647 | return -ENODEV; |
| 1648 | |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1649 | /* |
Robin Murphy | 05f80300 | 2017-07-21 13:12:38 +0100 | [diff] [blame] | 1650 | * Lock the group to make sure the device-count doesn't |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1651 | * change while we are attaching |
| 1652 | */ |
| 1653 | mutex_lock(&group->mutex); |
| 1654 | ret = -EINVAL; |
| 1655 | if (iommu_group_device_count(group) != 1) |
| 1656 | goto out_unlock; |
| 1657 | |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 1658 | ret = __iommu_attach_group(domain, group); |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1659 | |
| 1660 | out_unlock: |
| 1661 | mutex_unlock(&group->mutex); |
| 1662 | iommu_group_put(group); |
| 1663 | |
| 1664 | return ret; |
| 1665 | } |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1666 | EXPORT_SYMBOL_GPL(iommu_attach_device); |
| 1667 | |
Yi L Liu | 4c7c171 | 2019-10-02 12:42:40 -0700 | [diff] [blame] | 1668 | int iommu_cache_invalidate(struct iommu_domain *domain, struct device *dev, |
| 1669 | struct iommu_cache_invalidate_info *inv_info) |
| 1670 | { |
| 1671 | if (unlikely(!domain->ops->cache_invalidate)) |
| 1672 | return -ENODEV; |
| 1673 | |
| 1674 | return domain->ops->cache_invalidate(domain, dev, inv_info); |
| 1675 | } |
| 1676 | EXPORT_SYMBOL_GPL(iommu_cache_invalidate); |
| 1677 | |
Jacob Pan | 808be0a | 2019-10-02 12:42:43 -0700 | [diff] [blame] | 1678 | int iommu_sva_bind_gpasid(struct iommu_domain *domain, |
| 1679 | struct device *dev, struct iommu_gpasid_bind_data *data) |
| 1680 | { |
| 1681 | if (unlikely(!domain->ops->sva_bind_gpasid)) |
| 1682 | return -ENODEV; |
| 1683 | |
| 1684 | return domain->ops->sva_bind_gpasid(domain, dev, data); |
| 1685 | } |
| 1686 | EXPORT_SYMBOL_GPL(iommu_sva_bind_gpasid); |
| 1687 | |
| 1688 | int iommu_sva_unbind_gpasid(struct iommu_domain *domain, struct device *dev, |
| 1689 | ioasid_t pasid) |
| 1690 | { |
| 1691 | if (unlikely(!domain->ops->sva_unbind_gpasid)) |
| 1692 | return -ENODEV; |
| 1693 | |
| 1694 | return domain->ops->sva_unbind_gpasid(dev, pasid); |
| 1695 | } |
| 1696 | EXPORT_SYMBOL_GPL(iommu_sva_unbind_gpasid); |
| 1697 | |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1698 | static void __iommu_detach_device(struct iommu_domain *domain, |
| 1699 | struct device *dev) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1700 | { |
Baoquan He | e01d191 | 2017-08-09 16:33:40 +0800 | [diff] [blame] | 1701 | if ((domain->ops->is_attach_deferred != NULL) && |
| 1702 | domain->ops->is_attach_deferred(domain, dev)) |
| 1703 | return; |
| 1704 | |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 1705 | if (unlikely(domain->ops->detach_dev == NULL)) |
| 1706 | return; |
| 1707 | |
| 1708 | domain->ops->detach_dev(domain, dev); |
Shuah Khan | 6998063 | 2013-08-15 11:59:27 -0600 | [diff] [blame] | 1709 | trace_detach_device_from_domain(dev); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1710 | } |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1711 | |
| 1712 | void iommu_detach_device(struct iommu_domain *domain, struct device *dev) |
| 1713 | { |
| 1714 | struct iommu_group *group; |
| 1715 | |
| 1716 | group = iommu_group_get(dev); |
Jordan Crouse | 9ae9df0 | 2017-12-20 09:48:36 -0700 | [diff] [blame] | 1717 | if (!group) |
| 1718 | return; |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1719 | |
| 1720 | mutex_lock(&group->mutex); |
| 1721 | if (iommu_group_device_count(group) != 1) { |
| 1722 | WARN_ON(1); |
| 1723 | goto out_unlock; |
| 1724 | } |
| 1725 | |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 1726 | __iommu_detach_group(domain, group); |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1727 | |
| 1728 | out_unlock: |
| 1729 | mutex_unlock(&group->mutex); |
| 1730 | iommu_group_put(group); |
| 1731 | } |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1732 | EXPORT_SYMBOL_GPL(iommu_detach_device); |
| 1733 | |
Joerg Roedel | 2c1296d | 2015-05-28 18:41:32 +0200 | [diff] [blame] | 1734 | struct iommu_domain *iommu_get_domain_for_dev(struct device *dev) |
| 1735 | { |
| 1736 | struct iommu_domain *domain; |
| 1737 | struct iommu_group *group; |
| 1738 | |
| 1739 | group = iommu_group_get(dev); |
Robin Murphy | 1464d0b | 2017-08-17 11:40:08 +0100 | [diff] [blame] | 1740 | if (!group) |
Joerg Roedel | 2c1296d | 2015-05-28 18:41:32 +0200 | [diff] [blame] | 1741 | return NULL; |
| 1742 | |
| 1743 | domain = group->domain; |
| 1744 | |
| 1745 | iommu_group_put(group); |
| 1746 | |
| 1747 | return domain; |
| 1748 | } |
| 1749 | EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev); |
| 1750 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1751 | /* |
Robin Murphy | 6af588f | 2018-09-12 16:24:12 +0100 | [diff] [blame] | 1752 | * For IOMMU_DOMAIN_DMA implementations which already provide their own |
| 1753 | * guarantees that the group and its default domain are valid and correct. |
| 1754 | */ |
| 1755 | struct iommu_domain *iommu_get_dma_domain(struct device *dev) |
| 1756 | { |
| 1757 | return dev->iommu_group->default_domain; |
| 1758 | } |
| 1759 | |
| 1760 | /* |
Rami Rosen | 35449ad | 2018-09-18 17:38:49 +0300 | [diff] [blame] | 1761 | * IOMMU groups are really the natural working unit of the IOMMU, but |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1762 | * the IOMMU API works on domains and devices. Bridge that gap by |
| 1763 | * iterating over the devices in a group. Ideally we'd have a single |
| 1764 | * device which represents the requestor ID of the group, but we also |
| 1765 | * allow IOMMU drivers to create policy defined minimum sets, where |
| 1766 | * the physical hardware may be able to distiguish members, but we |
| 1767 | * wish to group them at a higher level (ex. untrusted multi-function |
| 1768 | * PCI devices). Thus we attach each device. |
| 1769 | */ |
| 1770 | static int iommu_group_do_attach_device(struct device *dev, void *data) |
| 1771 | { |
| 1772 | struct iommu_domain *domain = data; |
| 1773 | |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1774 | return __iommu_attach_device(domain, dev); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1775 | } |
| 1776 | |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 1777 | static int __iommu_attach_group(struct iommu_domain *domain, |
| 1778 | struct iommu_group *group) |
| 1779 | { |
| 1780 | int ret; |
| 1781 | |
| 1782 | if (group->default_domain && group->domain != group->default_domain) |
| 1783 | return -EBUSY; |
| 1784 | |
| 1785 | ret = __iommu_group_for_each_dev(group, domain, |
| 1786 | iommu_group_do_attach_device); |
| 1787 | if (ret == 0) |
| 1788 | group->domain = domain; |
| 1789 | |
| 1790 | return ret; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group) |
| 1794 | { |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 1795 | int ret; |
| 1796 | |
| 1797 | mutex_lock(&group->mutex); |
| 1798 | ret = __iommu_attach_group(domain, group); |
| 1799 | mutex_unlock(&group->mutex); |
| 1800 | |
| 1801 | return ret; |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1802 | } |
| 1803 | EXPORT_SYMBOL_GPL(iommu_attach_group); |
| 1804 | |
| 1805 | static int iommu_group_do_detach_device(struct device *dev, void *data) |
| 1806 | { |
| 1807 | struct iommu_domain *domain = data; |
| 1808 | |
Joerg Roedel | 426a273 | 2015-05-28 18:41:30 +0200 | [diff] [blame] | 1809 | __iommu_detach_device(domain, dev); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1810 | |
| 1811 | return 0; |
| 1812 | } |
| 1813 | |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 1814 | static void __iommu_detach_group(struct iommu_domain *domain, |
| 1815 | struct iommu_group *group) |
| 1816 | { |
| 1817 | int ret; |
| 1818 | |
| 1819 | if (!group->default_domain) { |
| 1820 | __iommu_group_for_each_dev(group, domain, |
| 1821 | iommu_group_do_detach_device); |
| 1822 | group->domain = NULL; |
| 1823 | return; |
| 1824 | } |
| 1825 | |
| 1826 | if (group->domain == group->default_domain) |
| 1827 | return; |
| 1828 | |
| 1829 | /* Detach by re-attaching to the default domain */ |
| 1830 | ret = __iommu_group_for_each_dev(group, group->default_domain, |
| 1831 | iommu_group_do_attach_device); |
| 1832 | if (ret != 0) |
| 1833 | WARN_ON(1); |
| 1834 | else |
| 1835 | group->domain = group->default_domain; |
| 1836 | } |
| 1837 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1838 | void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group) |
| 1839 | { |
Joerg Roedel | e39cb8a | 2015-05-28 18:41:31 +0200 | [diff] [blame] | 1840 | mutex_lock(&group->mutex); |
| 1841 | __iommu_detach_group(domain, group); |
| 1842 | mutex_unlock(&group->mutex); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 1843 | } |
| 1844 | EXPORT_SYMBOL_GPL(iommu_detach_group); |
| 1845 | |
Varun Sethi | bb5547a | 2013-03-29 01:23:58 +0530 | [diff] [blame] | 1846 | phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova) |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1847 | { |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 1848 | if (unlikely(domain->ops->iova_to_phys == NULL)) |
| 1849 | return 0; |
| 1850 | |
| 1851 | return domain->ops->iova_to_phys(domain, iova); |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 1852 | } |
| 1853 | EXPORT_SYMBOL_GPL(iommu_iova_to_phys); |
Sheng Yang | dbb9fd8 | 2009-03-18 15:33:06 +0800 | [diff] [blame] | 1854 | |
Alex Williamson | bd13969 | 2013-06-17 19:57:34 -0600 | [diff] [blame] | 1855 | static size_t iommu_pgsize(struct iommu_domain *domain, |
| 1856 | unsigned long addr_merge, size_t size) |
| 1857 | { |
| 1858 | unsigned int pgsize_idx; |
| 1859 | size_t pgsize; |
| 1860 | |
| 1861 | /* Max page size that still fits into 'size' */ |
| 1862 | pgsize_idx = __fls(size); |
| 1863 | |
| 1864 | /* need to consider alignment requirements ? */ |
| 1865 | if (likely(addr_merge)) { |
| 1866 | /* Max page size allowed by address */ |
| 1867 | unsigned int align_pgsize_idx = __ffs(addr_merge); |
| 1868 | pgsize_idx = min(pgsize_idx, align_pgsize_idx); |
| 1869 | } |
| 1870 | |
| 1871 | /* build a mask of acceptable page sizes */ |
| 1872 | pgsize = (1UL << (pgsize_idx + 1)) - 1; |
| 1873 | |
| 1874 | /* throw away page sizes not supported by the hardware */ |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 1875 | pgsize &= domain->pgsize_bitmap; |
Alex Williamson | bd13969 | 2013-06-17 19:57:34 -0600 | [diff] [blame] | 1876 | |
| 1877 | /* make sure we're still sane */ |
| 1878 | BUG_ON(!pgsize); |
| 1879 | |
| 1880 | /* pick the biggest page */ |
| 1881 | pgsize_idx = __fls(pgsize); |
| 1882 | pgsize = 1UL << pgsize_idx; |
| 1883 | |
| 1884 | return pgsize; |
| 1885 | } |
| 1886 | |
Tom Murphy | 781ca2d | 2019-09-08 09:56:38 -0700 | [diff] [blame] | 1887 | int __iommu_map(struct iommu_domain *domain, unsigned long iova, |
| 1888 | phys_addr_t paddr, size_t size, int prot, gfp_t gfp) |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1889 | { |
Dmitry Osipenko | 1d7ae53 | 2018-12-12 23:38:47 +0300 | [diff] [blame] | 1890 | const struct iommu_ops *ops = domain->ops; |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1891 | unsigned long orig_iova = iova; |
| 1892 | unsigned int min_pagesz; |
| 1893 | size_t orig_size = size; |
Yoshihiro Shimoda | 06bfcaa | 2016-02-10 10:18:04 +0900 | [diff] [blame] | 1894 | phys_addr_t orig_paddr = paddr; |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1895 | int ret = 0; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1896 | |
Dmitry Osipenko | 1d7ae53 | 2018-12-12 23:38:47 +0300 | [diff] [blame] | 1897 | if (unlikely(ops->map == NULL || |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 1898 | domain->pgsize_bitmap == 0UL)) |
Joerg Roedel | e5aa7f0 | 2011-09-06 16:44:29 +0200 | [diff] [blame] | 1899 | return -ENODEV; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1900 | |
Joerg Roedel | a10315e | 2015-03-26 13:43:06 +0100 | [diff] [blame] | 1901 | if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING))) |
| 1902 | return -EINVAL; |
| 1903 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1904 | /* find out the minimum page size supported */ |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 1905 | min_pagesz = 1 << __ffs(domain->pgsize_bitmap); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1906 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1907 | /* |
| 1908 | * both the virtual address and the physical one, as well as |
| 1909 | * the size of the mapping, must be aligned (at least) to the |
| 1910 | * size of the smallest page supported by the hardware |
| 1911 | */ |
| 1912 | if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) { |
Fabio Estevam | abedb04 | 2013-08-22 10:25:42 -0300 | [diff] [blame] | 1913 | pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n", |
Joe Perches | 6197ca8 | 2013-06-23 12:29:04 -0700 | [diff] [blame] | 1914 | iova, &paddr, size, min_pagesz); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1915 | return -EINVAL; |
| 1916 | } |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1917 | |
Fabio Estevam | abedb04 | 2013-08-22 10:25:42 -0300 | [diff] [blame] | 1918 | pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1919 | |
| 1920 | while (size) { |
Alex Williamson | bd13969 | 2013-06-17 19:57:34 -0600 | [diff] [blame] | 1921 | size_t pgsize = iommu_pgsize(domain, iova | paddr, size); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1922 | |
Fabio Estevam | abedb04 | 2013-08-22 10:25:42 -0300 | [diff] [blame] | 1923 | pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n", |
Joe Perches | 6197ca8 | 2013-06-23 12:29:04 -0700 | [diff] [blame] | 1924 | iova, &paddr, pgsize); |
Tom Murphy | 781ca2d | 2019-09-08 09:56:38 -0700 | [diff] [blame] | 1925 | ret = ops->map(domain, iova, paddr, pgsize, prot, gfp); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1926 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1927 | if (ret) |
| 1928 | break; |
| 1929 | |
| 1930 | iova += pgsize; |
| 1931 | paddr += pgsize; |
| 1932 | size -= pgsize; |
| 1933 | } |
| 1934 | |
Dmitry Osipenko | 1d7ae53 | 2018-12-12 23:38:47 +0300 | [diff] [blame] | 1935 | if (ops->iotlb_sync_map) |
| 1936 | ops->iotlb_sync_map(domain); |
| 1937 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1938 | /* unroll mapping in case something went wrong */ |
| 1939 | if (ret) |
| 1940 | iommu_unmap(domain, orig_iova, orig_size - size); |
Shuah Khan | e0be7c8 | 2013-08-15 11:59:28 -0600 | [diff] [blame] | 1941 | else |
Yoshihiro Shimoda | 06bfcaa | 2016-02-10 10:18:04 +0900 | [diff] [blame] | 1942 | trace_map(orig_iova, orig_paddr, orig_size); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1943 | |
| 1944 | return ret; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1945 | } |
Tom Murphy | 781ca2d | 2019-09-08 09:56:38 -0700 | [diff] [blame] | 1946 | |
| 1947 | int iommu_map(struct iommu_domain *domain, unsigned long iova, |
| 1948 | phys_addr_t paddr, size_t size, int prot) |
| 1949 | { |
| 1950 | might_sleep(); |
| 1951 | return __iommu_map(domain, iova, paddr, size, prot, GFP_KERNEL); |
| 1952 | } |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1953 | EXPORT_SYMBOL_GPL(iommu_map); |
| 1954 | |
Tom Murphy | 781ca2d | 2019-09-08 09:56:38 -0700 | [diff] [blame] | 1955 | int iommu_map_atomic(struct iommu_domain *domain, unsigned long iova, |
| 1956 | phys_addr_t paddr, size_t size, int prot) |
| 1957 | { |
| 1958 | return __iommu_map(domain, iova, paddr, size, prot, GFP_ATOMIC); |
| 1959 | } |
| 1960 | EXPORT_SYMBOL_GPL(iommu_map_atomic); |
| 1961 | |
Joerg Roedel | add02cfd | 2017-08-23 15:50:04 +0200 | [diff] [blame] | 1962 | static size_t __iommu_unmap(struct iommu_domain *domain, |
| 1963 | unsigned long iova, size_t size, |
Will Deacon | a7d20dc | 2019-07-02 16:43:48 +0100 | [diff] [blame] | 1964 | struct iommu_iotlb_gather *iotlb_gather) |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1965 | { |
Joerg Roedel | add02cfd | 2017-08-23 15:50:04 +0200 | [diff] [blame] | 1966 | const struct iommu_ops *ops = domain->ops; |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1967 | size_t unmapped_page, unmapped = 0; |
Shuah Khan | 6fd492f | 2015-01-16 16:47:19 -0700 | [diff] [blame] | 1968 | unsigned long orig_iova = iova; |
Joerg Roedel | add02cfd | 2017-08-23 15:50:04 +0200 | [diff] [blame] | 1969 | unsigned int min_pagesz; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1970 | |
Joerg Roedel | add02cfd | 2017-08-23 15:50:04 +0200 | [diff] [blame] | 1971 | if (unlikely(ops->unmap == NULL || |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 1972 | domain->pgsize_bitmap == 0UL)) |
Suravee Suthikulpanit | c5611a8 | 2018-02-05 05:45:53 -0500 | [diff] [blame] | 1973 | return 0; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1974 | |
Joerg Roedel | a10315e | 2015-03-26 13:43:06 +0100 | [diff] [blame] | 1975 | if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING))) |
Suravee Suthikulpanit | c5611a8 | 2018-02-05 05:45:53 -0500 | [diff] [blame] | 1976 | return 0; |
Joerg Roedel | a10315e | 2015-03-26 13:43:06 +0100 | [diff] [blame] | 1977 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1978 | /* find out the minimum page size supported */ |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 1979 | min_pagesz = 1 << __ffs(domain->pgsize_bitmap); |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1980 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1981 | /* |
| 1982 | * The virtual address, as well as the size of the mapping, must be |
| 1983 | * aligned (at least) to the size of the smallest page supported |
| 1984 | * by the hardware |
| 1985 | */ |
| 1986 | if (!IS_ALIGNED(iova | size, min_pagesz)) { |
Joe Perches | 6197ca8 | 2013-06-23 12:29:04 -0700 | [diff] [blame] | 1987 | pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n", |
| 1988 | iova, size, min_pagesz); |
Suravee Suthikulpanit | c5611a8 | 2018-02-05 05:45:53 -0500 | [diff] [blame] | 1989 | return 0; |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1990 | } |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 1991 | |
Joe Perches | 6197ca8 | 2013-06-23 12:29:04 -0700 | [diff] [blame] | 1992 | pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size); |
Ohad Ben-Cohen | 5009065 | 2011-11-10 11:32:25 +0200 | [diff] [blame] | 1993 | |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 1994 | /* |
| 1995 | * Keep iterating until we either unmap 'size' bytes (or more) |
| 1996 | * or we hit an area that isn't mapped. |
| 1997 | */ |
| 1998 | while (unmapped < size) { |
Alex Williamson | bd13969 | 2013-06-17 19:57:34 -0600 | [diff] [blame] | 1999 | size_t pgsize = iommu_pgsize(domain, iova, size - unmapped); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 2000 | |
Will Deacon | 56f8af5 | 2019-07-02 16:44:06 +0100 | [diff] [blame] | 2001 | unmapped_page = ops->unmap(domain, iova, pgsize, iotlb_gather); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 2002 | if (!unmapped_page) |
| 2003 | break; |
| 2004 | |
Joe Perches | 6197ca8 | 2013-06-23 12:29:04 -0700 | [diff] [blame] | 2005 | pr_debug("unmapped: iova 0x%lx size 0x%zx\n", |
| 2006 | iova, unmapped_page); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 2007 | |
| 2008 | iova += unmapped_page; |
| 2009 | unmapped += unmapped_page; |
| 2010 | } |
| 2011 | |
Shuah Khan | db8614d | 2015-01-16 20:53:17 -0700 | [diff] [blame] | 2012 | trace_unmap(orig_iova, size, unmapped); |
Ohad Ben-Cohen | 7d3002c | 2011-11-10 11:32:26 +0200 | [diff] [blame] | 2013 | return unmapped; |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 2014 | } |
Joerg Roedel | add02cfd | 2017-08-23 15:50:04 +0200 | [diff] [blame] | 2015 | |
| 2016 | size_t iommu_unmap(struct iommu_domain *domain, |
| 2017 | unsigned long iova, size_t size) |
| 2018 | { |
Will Deacon | a7d20dc | 2019-07-02 16:43:48 +0100 | [diff] [blame] | 2019 | struct iommu_iotlb_gather iotlb_gather; |
| 2020 | size_t ret; |
| 2021 | |
| 2022 | iommu_iotlb_gather_init(&iotlb_gather); |
| 2023 | ret = __iommu_unmap(domain, iova, size, &iotlb_gather); |
| 2024 | iommu_tlb_sync(domain, &iotlb_gather); |
| 2025 | |
| 2026 | return ret; |
Joerg Roedel | add02cfd | 2017-08-23 15:50:04 +0200 | [diff] [blame] | 2027 | } |
Joerg Roedel | cefc53c | 2010-01-08 13:35:09 +0100 | [diff] [blame] | 2028 | EXPORT_SYMBOL_GPL(iommu_unmap); |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 2029 | |
Joerg Roedel | add02cfd | 2017-08-23 15:50:04 +0200 | [diff] [blame] | 2030 | size_t iommu_unmap_fast(struct iommu_domain *domain, |
Will Deacon | a7d20dc | 2019-07-02 16:43:48 +0100 | [diff] [blame] | 2031 | unsigned long iova, size_t size, |
| 2032 | struct iommu_iotlb_gather *iotlb_gather) |
Joerg Roedel | add02cfd | 2017-08-23 15:50:04 +0200 | [diff] [blame] | 2033 | { |
Will Deacon | a7d20dc | 2019-07-02 16:43:48 +0100 | [diff] [blame] | 2034 | return __iommu_unmap(domain, iova, size, iotlb_gather); |
Joerg Roedel | add02cfd | 2017-08-23 15:50:04 +0200 | [diff] [blame] | 2035 | } |
| 2036 | EXPORT_SYMBOL_GPL(iommu_unmap_fast); |
| 2037 | |
Tom Murphy | 781ca2d | 2019-09-08 09:56:38 -0700 | [diff] [blame] | 2038 | size_t __iommu_map_sg(struct iommu_domain *domain, unsigned long iova, |
| 2039 | struct scatterlist *sg, unsigned int nents, int prot, |
| 2040 | gfp_t gfp) |
Olav Haugan | 315786e | 2014-10-25 09:55:16 -0700 | [diff] [blame] | 2041 | { |
Robin Murphy | 5d95f40 | 2018-10-11 16:56:42 +0100 | [diff] [blame] | 2042 | size_t len = 0, mapped = 0; |
| 2043 | phys_addr_t start; |
| 2044 | unsigned int i = 0; |
Joerg Roedel | 38ec010 | 2014-11-04 14:53:51 +0100 | [diff] [blame] | 2045 | int ret; |
Olav Haugan | 315786e | 2014-10-25 09:55:16 -0700 | [diff] [blame] | 2046 | |
Robin Murphy | 5d95f40 | 2018-10-11 16:56:42 +0100 | [diff] [blame] | 2047 | while (i <= nents) { |
| 2048 | phys_addr_t s_phys = sg_phys(sg); |
Olav Haugan | 315786e | 2014-10-25 09:55:16 -0700 | [diff] [blame] | 2049 | |
Robin Murphy | 5d95f40 | 2018-10-11 16:56:42 +0100 | [diff] [blame] | 2050 | if (len && s_phys != start + len) { |
Tom Murphy | 781ca2d | 2019-09-08 09:56:38 -0700 | [diff] [blame] | 2051 | ret = __iommu_map(domain, iova + mapped, start, |
| 2052 | len, prot, gfp); |
| 2053 | |
Robin Murphy | 5d95f40 | 2018-10-11 16:56:42 +0100 | [diff] [blame] | 2054 | if (ret) |
| 2055 | goto out_err; |
Robin Murphy | 18f2340 | 2014-11-25 17:50:55 +0000 | [diff] [blame] | 2056 | |
Robin Murphy | 5d95f40 | 2018-10-11 16:56:42 +0100 | [diff] [blame] | 2057 | mapped += len; |
| 2058 | len = 0; |
| 2059 | } |
Robin Murphy | 18f2340 | 2014-11-25 17:50:55 +0000 | [diff] [blame] | 2060 | |
Robin Murphy | 5d95f40 | 2018-10-11 16:56:42 +0100 | [diff] [blame] | 2061 | if (len) { |
| 2062 | len += sg->length; |
| 2063 | } else { |
| 2064 | len = sg->length; |
| 2065 | start = s_phys; |
| 2066 | } |
Joerg Roedel | 38ec010 | 2014-11-04 14:53:51 +0100 | [diff] [blame] | 2067 | |
Robin Murphy | 5d95f40 | 2018-10-11 16:56:42 +0100 | [diff] [blame] | 2068 | if (++i < nents) |
| 2069 | sg = sg_next(sg); |
Olav Haugan | 315786e | 2014-10-25 09:55:16 -0700 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | return mapped; |
Joerg Roedel | 38ec010 | 2014-11-04 14:53:51 +0100 | [diff] [blame] | 2073 | |
| 2074 | out_err: |
| 2075 | /* undo mappings already done */ |
| 2076 | iommu_unmap(domain, iova, mapped); |
| 2077 | |
| 2078 | return 0; |
| 2079 | |
Olav Haugan | 315786e | 2014-10-25 09:55:16 -0700 | [diff] [blame] | 2080 | } |
Tom Murphy | 781ca2d | 2019-09-08 09:56:38 -0700 | [diff] [blame] | 2081 | |
| 2082 | size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova, |
| 2083 | struct scatterlist *sg, unsigned int nents, int prot) |
| 2084 | { |
| 2085 | might_sleep(); |
| 2086 | return __iommu_map_sg(domain, iova, sg, nents, prot, GFP_KERNEL); |
| 2087 | } |
Christoph Hellwig | d88e61f | 2018-07-30 09:36:26 +0200 | [diff] [blame] | 2088 | EXPORT_SYMBOL_GPL(iommu_map_sg); |
Joerg Roedel | d7787d5 | 2013-01-29 14:26:20 +0100 | [diff] [blame] | 2089 | |
Tom Murphy | 781ca2d | 2019-09-08 09:56:38 -0700 | [diff] [blame] | 2090 | size_t iommu_map_sg_atomic(struct iommu_domain *domain, unsigned long iova, |
| 2091 | struct scatterlist *sg, unsigned int nents, int prot) |
| 2092 | { |
| 2093 | return __iommu_map_sg(domain, iova, sg, nents, prot, GFP_ATOMIC); |
| 2094 | } |
| 2095 | EXPORT_SYMBOL_GPL(iommu_map_sg_atomic); |
| 2096 | |
Joerg Roedel | d7787d5 | 2013-01-29 14:26:20 +0100 | [diff] [blame] | 2097 | int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr, |
Varun Sethi | 80f97f0 | 2013-03-29 01:24:00 +0530 | [diff] [blame] | 2098 | phys_addr_t paddr, u64 size, int prot) |
Joerg Roedel | d7787d5 | 2013-01-29 14:26:20 +0100 | [diff] [blame] | 2099 | { |
| 2100 | if (unlikely(domain->ops->domain_window_enable == NULL)) |
| 2101 | return -ENODEV; |
| 2102 | |
Varun Sethi | 80f97f0 | 2013-03-29 01:24:00 +0530 | [diff] [blame] | 2103 | return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size, |
| 2104 | prot); |
Joerg Roedel | d7787d5 | 2013-01-29 14:26:20 +0100 | [diff] [blame] | 2105 | } |
| 2106 | EXPORT_SYMBOL_GPL(iommu_domain_window_enable); |
| 2107 | |
| 2108 | void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr) |
| 2109 | { |
| 2110 | if (unlikely(domain->ops->domain_window_disable == NULL)) |
| 2111 | return; |
| 2112 | |
| 2113 | return domain->ops->domain_window_disable(domain, wnd_nr); |
| 2114 | } |
| 2115 | EXPORT_SYMBOL_GPL(iommu_domain_window_disable); |
| 2116 | |
Joerg Roedel | 207c6e3 | 2017-04-26 15:39:28 +0200 | [diff] [blame] | 2117 | /** |
| 2118 | * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework |
| 2119 | * @domain: the iommu domain where the fault has happened |
| 2120 | * @dev: the device where the fault has happened |
| 2121 | * @iova: the faulting address |
| 2122 | * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...) |
| 2123 | * |
| 2124 | * This function should be called by the low-level IOMMU implementations |
| 2125 | * whenever IOMMU faults happen, to allow high-level users, that are |
| 2126 | * interested in such events, to know about them. |
| 2127 | * |
| 2128 | * This event may be useful for several possible use cases: |
| 2129 | * - mere logging of the event |
| 2130 | * - dynamic TLB/PTE loading |
| 2131 | * - if restarting of the faulting device is required |
| 2132 | * |
| 2133 | * Returns 0 on success and an appropriate error code otherwise (if dynamic |
| 2134 | * PTE/TLB loading will one day be supported, implementations will be able |
| 2135 | * to tell whether it succeeded or not according to this return value). |
| 2136 | * |
| 2137 | * Specifically, -ENOSYS is returned if a fault handler isn't installed |
| 2138 | * (though fault handlers can also return -ENOSYS, in case they want to |
| 2139 | * elicit the default behavior of the IOMMU drivers). |
| 2140 | */ |
| 2141 | int report_iommu_fault(struct iommu_domain *domain, struct device *dev, |
| 2142 | unsigned long iova, int flags) |
| 2143 | { |
| 2144 | int ret = -ENOSYS; |
| 2145 | |
| 2146 | /* |
| 2147 | * if upper layers showed interest and installed a fault handler, |
| 2148 | * invoke it. |
| 2149 | */ |
| 2150 | if (domain->handler) |
| 2151 | ret = domain->handler(domain, dev, iova, flags, |
| 2152 | domain->handler_token); |
| 2153 | |
| 2154 | trace_io_page_fault(dev, iova, flags); |
| 2155 | return ret; |
| 2156 | } |
| 2157 | EXPORT_SYMBOL_GPL(report_iommu_fault); |
| 2158 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 2159 | static int __init iommu_init(void) |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 2160 | { |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 2161 | iommu_group_kset = kset_create_and_add("iommu_groups", |
| 2162 | NULL, kernel_kobj); |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 2163 | BUG_ON(!iommu_group_kset); |
| 2164 | |
Gary R Hook | bad614b | 2018-06-12 16:41:21 -0500 | [diff] [blame] | 2165 | iommu_debugfs_setup(); |
| 2166 | |
Alex Williamson | d72e31c | 2012-05-30 14:18:53 -0600 | [diff] [blame] | 2167 | return 0; |
Alex Williamson | 1460432 | 2011-10-21 15:56:05 -0400 | [diff] [blame] | 2168 | } |
Marek Szyprowski | d7ef999 | 2015-05-19 15:20:23 +0200 | [diff] [blame] | 2169 | core_initcall(iommu_init); |
Joerg Roedel | 0cd76dd | 2012-01-26 19:40:52 +0100 | [diff] [blame] | 2170 | |
| 2171 | int iommu_domain_get_attr(struct iommu_domain *domain, |
| 2172 | enum iommu_attr attr, void *data) |
| 2173 | { |
Joerg Roedel | 0ff64f8 | 2012-01-26 19:40:53 +0100 | [diff] [blame] | 2174 | struct iommu_domain_geometry *geometry; |
Joerg Roedel | d2e1216 | 2013-01-29 13:49:04 +0100 | [diff] [blame] | 2175 | bool *paging; |
Joerg Roedel | 0ff64f8 | 2012-01-26 19:40:53 +0100 | [diff] [blame] | 2176 | int ret = 0; |
Joerg Roedel | 0cd76dd | 2012-01-26 19:40:52 +0100 | [diff] [blame] | 2177 | |
Joerg Roedel | 0ff64f8 | 2012-01-26 19:40:53 +0100 | [diff] [blame] | 2178 | switch (attr) { |
| 2179 | case DOMAIN_ATTR_GEOMETRY: |
| 2180 | geometry = data; |
| 2181 | *geometry = domain->geometry; |
| 2182 | |
| 2183 | break; |
Joerg Roedel | d2e1216 | 2013-01-29 13:49:04 +0100 | [diff] [blame] | 2184 | case DOMAIN_ATTR_PAGING: |
| 2185 | paging = data; |
Robin Murphy | d16e0fa | 2016-04-07 18:42:06 +0100 | [diff] [blame] | 2186 | *paging = (domain->pgsize_bitmap != 0UL); |
Joerg Roedel | d2e1216 | 2013-01-29 13:49:04 +0100 | [diff] [blame] | 2187 | break; |
Joerg Roedel | 0ff64f8 | 2012-01-26 19:40:53 +0100 | [diff] [blame] | 2188 | default: |
| 2189 | if (!domain->ops->domain_get_attr) |
| 2190 | return -EINVAL; |
| 2191 | |
| 2192 | ret = domain->ops->domain_get_attr(domain, attr, data); |
| 2193 | } |
| 2194 | |
| 2195 | return ret; |
Joerg Roedel | 0cd76dd | 2012-01-26 19:40:52 +0100 | [diff] [blame] | 2196 | } |
| 2197 | EXPORT_SYMBOL_GPL(iommu_domain_get_attr); |
| 2198 | |
| 2199 | int iommu_domain_set_attr(struct iommu_domain *domain, |
| 2200 | enum iommu_attr attr, void *data) |
| 2201 | { |
Joerg Roedel | 6935671 | 2013-02-04 14:00:01 +0100 | [diff] [blame] | 2202 | int ret = 0; |
Joerg Roedel | 0cd76dd | 2012-01-26 19:40:52 +0100 | [diff] [blame] | 2203 | |
Joerg Roedel | 6935671 | 2013-02-04 14:00:01 +0100 | [diff] [blame] | 2204 | switch (attr) { |
Joerg Roedel | 6935671 | 2013-02-04 14:00:01 +0100 | [diff] [blame] | 2205 | default: |
| 2206 | if (domain->ops->domain_set_attr == NULL) |
| 2207 | return -EINVAL; |
| 2208 | |
| 2209 | ret = domain->ops->domain_set_attr(domain, attr, data); |
| 2210 | } |
| 2211 | |
| 2212 | return ret; |
Joerg Roedel | 0cd76dd | 2012-01-26 19:40:52 +0100 | [diff] [blame] | 2213 | } |
| 2214 | EXPORT_SYMBOL_GPL(iommu_domain_set_attr); |
Joerg Roedel | a1015c2 | 2015-05-28 18:41:33 +0200 | [diff] [blame] | 2215 | |
Eric Auger | e5b5234 | 2017-01-19 20:57:47 +0000 | [diff] [blame] | 2216 | void iommu_get_resv_regions(struct device *dev, struct list_head *list) |
Joerg Roedel | a1015c2 | 2015-05-28 18:41:33 +0200 | [diff] [blame] | 2217 | { |
| 2218 | const struct iommu_ops *ops = dev->bus->iommu_ops; |
| 2219 | |
Eric Auger | e5b5234 | 2017-01-19 20:57:47 +0000 | [diff] [blame] | 2220 | if (ops && ops->get_resv_regions) |
| 2221 | ops->get_resv_regions(dev, list); |
Joerg Roedel | a1015c2 | 2015-05-28 18:41:33 +0200 | [diff] [blame] | 2222 | } |
| 2223 | |
Eric Auger | e5b5234 | 2017-01-19 20:57:47 +0000 | [diff] [blame] | 2224 | void iommu_put_resv_regions(struct device *dev, struct list_head *list) |
Joerg Roedel | a1015c2 | 2015-05-28 18:41:33 +0200 | [diff] [blame] | 2225 | { |
| 2226 | const struct iommu_ops *ops = dev->bus->iommu_ops; |
| 2227 | |
Eric Auger | e5b5234 | 2017-01-19 20:57:47 +0000 | [diff] [blame] | 2228 | if (ops && ops->put_resv_regions) |
| 2229 | ops->put_resv_regions(dev, list); |
Joerg Roedel | a1015c2 | 2015-05-28 18:41:33 +0200 | [diff] [blame] | 2230 | } |
Joerg Roedel | d290f1e | 2015-05-28 18:41:36 +0200 | [diff] [blame] | 2231 | |
Thierry Reding | f9f6971 | 2019-12-18 14:42:01 +0100 | [diff] [blame^] | 2232 | /** |
| 2233 | * generic_iommu_put_resv_regions - Reserved region driver helper |
| 2234 | * @dev: device for which to free reserved regions |
| 2235 | * @list: reserved region list for device |
| 2236 | * |
| 2237 | * IOMMU drivers can use this to implement their .put_resv_regions() callback |
| 2238 | * for simple reservations. Memory allocated for each reserved region will be |
| 2239 | * freed. If an IOMMU driver allocates additional resources per region, it is |
| 2240 | * going to have to implement a custom callback. |
| 2241 | */ |
| 2242 | void generic_iommu_put_resv_regions(struct device *dev, struct list_head *list) |
| 2243 | { |
| 2244 | struct iommu_resv_region *entry, *next; |
| 2245 | |
| 2246 | list_for_each_entry_safe(entry, next, list, list) |
| 2247 | kfree(entry); |
| 2248 | } |
| 2249 | EXPORT_SYMBOL(generic_iommu_put_resv_regions); |
| 2250 | |
Eric Auger | 2b20cbb | 2017-01-19 20:57:49 +0000 | [diff] [blame] | 2251 | struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start, |
Robin Murphy | 9d3a4de | 2017-03-16 17:00:16 +0000 | [diff] [blame] | 2252 | size_t length, int prot, |
| 2253 | enum iommu_resv_type type) |
Eric Auger | 2b20cbb | 2017-01-19 20:57:49 +0000 | [diff] [blame] | 2254 | { |
| 2255 | struct iommu_resv_region *region; |
| 2256 | |
| 2257 | region = kzalloc(sizeof(*region), GFP_KERNEL); |
| 2258 | if (!region) |
| 2259 | return NULL; |
| 2260 | |
| 2261 | INIT_LIST_HEAD(®ion->list); |
| 2262 | region->start = start; |
| 2263 | region->length = length; |
| 2264 | region->prot = prot; |
| 2265 | region->type = type; |
| 2266 | return region; |
Joerg Roedel | fc2100e | 2008-11-26 17:21:24 +0100 | [diff] [blame] | 2267 | } |
Joerg Roedel | d290f1e | 2015-05-28 18:41:36 +0200 | [diff] [blame] | 2268 | |
Lu Baolu | 7423e01 | 2019-05-25 13:41:22 +0800 | [diff] [blame] | 2269 | static int |
| 2270 | request_default_domain_for_dev(struct device *dev, unsigned long type) |
Joerg Roedel | d290f1e | 2015-05-28 18:41:36 +0200 | [diff] [blame] | 2271 | { |
Lu Baolu | 7423e01 | 2019-05-25 13:41:22 +0800 | [diff] [blame] | 2272 | struct iommu_domain *domain; |
Joerg Roedel | d290f1e | 2015-05-28 18:41:36 +0200 | [diff] [blame] | 2273 | struct iommu_group *group; |
| 2274 | int ret; |
| 2275 | |
| 2276 | /* Device must already be in a group before calling this function */ |
Lu Baolu | 57274ea | 2019-05-21 15:27:35 +0800 | [diff] [blame] | 2277 | group = iommu_group_get(dev); |
| 2278 | if (!group) |
| 2279 | return -EINVAL; |
Joerg Roedel | d290f1e | 2015-05-28 18:41:36 +0200 | [diff] [blame] | 2280 | |
| 2281 | mutex_lock(&group->mutex); |
| 2282 | |
Joerg Roedel | d290f1e | 2015-05-28 18:41:36 +0200 | [diff] [blame] | 2283 | ret = 0; |
Lu Baolu | 7423e01 | 2019-05-25 13:41:22 +0800 | [diff] [blame] | 2284 | if (group->default_domain && group->default_domain->type == type) |
Joerg Roedel | d290f1e | 2015-05-28 18:41:36 +0200 | [diff] [blame] | 2285 | goto out; |
| 2286 | |
| 2287 | /* Don't change mappings of existing devices */ |
| 2288 | ret = -EBUSY; |
| 2289 | if (iommu_group_device_count(group) != 1) |
| 2290 | goto out; |
| 2291 | |
Joerg Roedel | d290f1e | 2015-05-28 18:41:36 +0200 | [diff] [blame] | 2292 | ret = -ENOMEM; |
Lu Baolu | 7423e01 | 2019-05-25 13:41:22 +0800 | [diff] [blame] | 2293 | domain = __iommu_domain_alloc(dev->bus, type); |
| 2294 | if (!domain) |
Joerg Roedel | d290f1e | 2015-05-28 18:41:36 +0200 | [diff] [blame] | 2295 | goto out; |
| 2296 | |
| 2297 | /* Attach the device to the domain */ |
Lu Baolu | 7423e01 | 2019-05-25 13:41:22 +0800 | [diff] [blame] | 2298 | ret = __iommu_attach_group(domain, group); |
Joerg Roedel | d290f1e | 2015-05-28 18:41:36 +0200 | [diff] [blame] | 2299 | if (ret) { |
Lu Baolu | 7423e01 | 2019-05-25 13:41:22 +0800 | [diff] [blame] | 2300 | iommu_domain_free(domain); |
Joerg Roedel | d290f1e | 2015-05-28 18:41:36 +0200 | [diff] [blame] | 2301 | goto out; |
| 2302 | } |
| 2303 | |
Tom Murphy | d127bc9 | 2019-08-26 05:48:21 +0100 | [diff] [blame] | 2304 | /* Make the domain the default for this group */ |
Joerg Roedel | d290f1e | 2015-05-28 18:41:36 +0200 | [diff] [blame] | 2305 | if (group->default_domain) |
| 2306 | iommu_domain_free(group->default_domain); |
Lu Baolu | 7423e01 | 2019-05-25 13:41:22 +0800 | [diff] [blame] | 2307 | group->default_domain = domain; |
Joerg Roedel | d290f1e | 2015-05-28 18:41:36 +0200 | [diff] [blame] | 2308 | |
Jerry Snitselaar | d360211 | 2019-12-10 11:56:06 -0700 | [diff] [blame] | 2309 | iommu_group_create_direct_mappings(group, dev); |
| 2310 | |
Lu Baolu | 7423e01 | 2019-05-25 13:41:22 +0800 | [diff] [blame] | 2311 | dev_info(dev, "Using iommu %s mapping\n", |
| 2312 | type == IOMMU_DOMAIN_DMA ? "dma" : "direct"); |
Joerg Roedel | d290f1e | 2015-05-28 18:41:36 +0200 | [diff] [blame] | 2313 | |
| 2314 | ret = 0; |
| 2315 | out: |
| 2316 | mutex_unlock(&group->mutex); |
| 2317 | iommu_group_put(group); |
| 2318 | |
| 2319 | return ret; |
| 2320 | } |
Robin Murphy | 57f98d2 | 2016-09-13 10:54:14 +0100 | [diff] [blame] | 2321 | |
Lu Baolu | 7423e01 | 2019-05-25 13:41:22 +0800 | [diff] [blame] | 2322 | /* Request that a device is direct mapped by the IOMMU */ |
| 2323 | int iommu_request_dm_for_dev(struct device *dev) |
| 2324 | { |
| 2325 | return request_default_domain_for_dev(dev, IOMMU_DOMAIN_IDENTITY); |
| 2326 | } |
| 2327 | |
| 2328 | /* Request that a device can't be direct mapped by the IOMMU */ |
| 2329 | int iommu_request_dma_domain_for_dev(struct device *dev) |
| 2330 | { |
| 2331 | return request_default_domain_for_dev(dev, IOMMU_DOMAIN_DMA); |
| 2332 | } |
| 2333 | |
Joerg Roedel | 8a69961 | 2019-08-19 15:22:47 +0200 | [diff] [blame] | 2334 | void iommu_set_default_passthrough(bool cmd_line) |
| 2335 | { |
| 2336 | if (cmd_line) |
| 2337 | iommu_set_cmd_line_dma_api(); |
| 2338 | |
| 2339 | iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY; |
| 2340 | } |
| 2341 | |
| 2342 | void iommu_set_default_translated(bool cmd_line) |
| 2343 | { |
| 2344 | if (cmd_line) |
| 2345 | iommu_set_cmd_line_dma_api(); |
| 2346 | |
| 2347 | iommu_def_domain_type = IOMMU_DOMAIN_DMA; |
| 2348 | } |
| 2349 | |
| 2350 | bool iommu_default_passthrough(void) |
| 2351 | { |
| 2352 | return iommu_def_domain_type == IOMMU_DOMAIN_IDENTITY; |
| 2353 | } |
| 2354 | EXPORT_SYMBOL_GPL(iommu_default_passthrough); |
| 2355 | |
Joerg Roedel | 534766d | 2017-01-31 16:58:42 +0100 | [diff] [blame] | 2356 | const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode) |
Lorenzo Pieralisi | e4f10ff | 2016-11-21 10:01:36 +0000 | [diff] [blame] | 2357 | { |
Lorenzo Pieralisi | e4f10ff | 2016-11-21 10:01:36 +0000 | [diff] [blame] | 2358 | const struct iommu_ops *ops = NULL; |
Joerg Roedel | d0f6f58 | 2017-02-02 12:19:12 +0100 | [diff] [blame] | 2359 | struct iommu_device *iommu; |
Lorenzo Pieralisi | e4f10ff | 2016-11-21 10:01:36 +0000 | [diff] [blame] | 2360 | |
Joerg Roedel | d0f6f58 | 2017-02-02 12:19:12 +0100 | [diff] [blame] | 2361 | spin_lock(&iommu_device_lock); |
| 2362 | list_for_each_entry(iommu, &iommu_device_list, list) |
| 2363 | if (iommu->fwnode == fwnode) { |
| 2364 | ops = iommu->ops; |
Lorenzo Pieralisi | e4f10ff | 2016-11-21 10:01:36 +0000 | [diff] [blame] | 2365 | break; |
| 2366 | } |
Joerg Roedel | d0f6f58 | 2017-02-02 12:19:12 +0100 | [diff] [blame] | 2367 | spin_unlock(&iommu_device_lock); |
Lorenzo Pieralisi | e4f10ff | 2016-11-21 10:01:36 +0000 | [diff] [blame] | 2368 | return ops; |
| 2369 | } |
| 2370 | |
Robin Murphy | 57f98d2 | 2016-09-13 10:54:14 +0100 | [diff] [blame] | 2371 | int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode, |
| 2372 | const struct iommu_ops *ops) |
| 2373 | { |
Joerg Roedel | b4ef725 | 2018-11-28 13:35:24 +0100 | [diff] [blame] | 2374 | struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); |
Robin Murphy | 57f98d2 | 2016-09-13 10:54:14 +0100 | [diff] [blame] | 2375 | |
| 2376 | if (fwspec) |
| 2377 | return ops == fwspec->ops ? 0 : -EINVAL; |
| 2378 | |
| 2379 | fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL); |
| 2380 | if (!fwspec) |
| 2381 | return -ENOMEM; |
| 2382 | |
| 2383 | of_node_get(to_of_node(iommu_fwnode)); |
| 2384 | fwspec->iommu_fwnode = iommu_fwnode; |
| 2385 | fwspec->ops = ops; |
Joerg Roedel | b4ef725 | 2018-11-28 13:35:24 +0100 | [diff] [blame] | 2386 | dev_iommu_fwspec_set(dev, fwspec); |
Robin Murphy | 57f98d2 | 2016-09-13 10:54:14 +0100 | [diff] [blame] | 2387 | return 0; |
| 2388 | } |
| 2389 | EXPORT_SYMBOL_GPL(iommu_fwspec_init); |
| 2390 | |
| 2391 | void iommu_fwspec_free(struct device *dev) |
| 2392 | { |
Joerg Roedel | b4ef725 | 2018-11-28 13:35:24 +0100 | [diff] [blame] | 2393 | struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); |
Robin Murphy | 57f98d2 | 2016-09-13 10:54:14 +0100 | [diff] [blame] | 2394 | |
| 2395 | if (fwspec) { |
| 2396 | fwnode_handle_put(fwspec->iommu_fwnode); |
| 2397 | kfree(fwspec); |
Joerg Roedel | b4ef725 | 2018-11-28 13:35:24 +0100 | [diff] [blame] | 2398 | dev_iommu_fwspec_set(dev, NULL); |
Robin Murphy | 57f98d2 | 2016-09-13 10:54:14 +0100 | [diff] [blame] | 2399 | } |
| 2400 | } |
| 2401 | EXPORT_SYMBOL_GPL(iommu_fwspec_free); |
| 2402 | |
| 2403 | int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids) |
| 2404 | { |
Joerg Roedel | b4ef725 | 2018-11-28 13:35:24 +0100 | [diff] [blame] | 2405 | struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); |
Robin Murphy | 57f98d2 | 2016-09-13 10:54:14 +0100 | [diff] [blame] | 2406 | size_t size; |
| 2407 | int i; |
| 2408 | |
| 2409 | if (!fwspec) |
| 2410 | return -EINVAL; |
| 2411 | |
| 2412 | size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]); |
| 2413 | if (size > sizeof(*fwspec)) { |
Joerg Roedel | b4ef725 | 2018-11-28 13:35:24 +0100 | [diff] [blame] | 2414 | fwspec = krealloc(fwspec, size, GFP_KERNEL); |
Robin Murphy | 57f98d2 | 2016-09-13 10:54:14 +0100 | [diff] [blame] | 2415 | if (!fwspec) |
| 2416 | return -ENOMEM; |
Zhen Lei | 909111b | 2017-02-03 17:35:02 +0800 | [diff] [blame] | 2417 | |
Joerg Roedel | b4ef725 | 2018-11-28 13:35:24 +0100 | [diff] [blame] | 2418 | dev_iommu_fwspec_set(dev, fwspec); |
Robin Murphy | 57f98d2 | 2016-09-13 10:54:14 +0100 | [diff] [blame] | 2419 | } |
| 2420 | |
| 2421 | for (i = 0; i < num_ids; i++) |
| 2422 | fwspec->ids[fwspec->num_ids + i] = ids[i]; |
| 2423 | |
| 2424 | fwspec->num_ids += num_ids; |
Robin Murphy | 57f98d2 | 2016-09-13 10:54:14 +0100 | [diff] [blame] | 2425 | return 0; |
| 2426 | } |
| 2427 | EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids); |
Lu Baolu | a3a1959 | 2019-03-25 09:30:28 +0800 | [diff] [blame] | 2428 | |
| 2429 | /* |
| 2430 | * Per device IOMMU features. |
| 2431 | */ |
| 2432 | bool iommu_dev_has_feature(struct device *dev, enum iommu_dev_features feat) |
| 2433 | { |
| 2434 | const struct iommu_ops *ops = dev->bus->iommu_ops; |
| 2435 | |
| 2436 | if (ops && ops->dev_has_feat) |
| 2437 | return ops->dev_has_feat(dev, feat); |
| 2438 | |
| 2439 | return false; |
| 2440 | } |
| 2441 | EXPORT_SYMBOL_GPL(iommu_dev_has_feature); |
| 2442 | |
| 2443 | int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat) |
| 2444 | { |
| 2445 | const struct iommu_ops *ops = dev->bus->iommu_ops; |
| 2446 | |
| 2447 | if (ops && ops->dev_enable_feat) |
| 2448 | return ops->dev_enable_feat(dev, feat); |
| 2449 | |
| 2450 | return -ENODEV; |
| 2451 | } |
| 2452 | EXPORT_SYMBOL_GPL(iommu_dev_enable_feature); |
| 2453 | |
| 2454 | /* |
| 2455 | * The device drivers should do the necessary cleanups before calling this. |
| 2456 | * For example, before disabling the aux-domain feature, the device driver |
| 2457 | * should detach all aux-domains. Otherwise, this will return -EBUSY. |
| 2458 | */ |
| 2459 | int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat) |
| 2460 | { |
| 2461 | const struct iommu_ops *ops = dev->bus->iommu_ops; |
| 2462 | |
| 2463 | if (ops && ops->dev_disable_feat) |
| 2464 | return ops->dev_disable_feat(dev, feat); |
| 2465 | |
| 2466 | return -EBUSY; |
| 2467 | } |
| 2468 | EXPORT_SYMBOL_GPL(iommu_dev_disable_feature); |
| 2469 | |
| 2470 | bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat) |
| 2471 | { |
| 2472 | const struct iommu_ops *ops = dev->bus->iommu_ops; |
| 2473 | |
| 2474 | if (ops && ops->dev_feat_enabled) |
| 2475 | return ops->dev_feat_enabled(dev, feat); |
| 2476 | |
| 2477 | return false; |
| 2478 | } |
| 2479 | EXPORT_SYMBOL_GPL(iommu_dev_feature_enabled); |
| 2480 | |
| 2481 | /* |
| 2482 | * Aux-domain specific attach/detach. |
| 2483 | * |
| 2484 | * Only works if iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX) returns |
| 2485 | * true. Also, as long as domains are attached to a device through this |
| 2486 | * interface, any tries to call iommu_attach_device() should fail |
| 2487 | * (iommu_detach_device() can't fail, so we fail when trying to re-attach). |
| 2488 | * This should make us safe against a device being attached to a guest as a |
| 2489 | * whole while there are still pasid users on it (aux and sva). |
| 2490 | */ |
| 2491 | int iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev) |
| 2492 | { |
| 2493 | int ret = -ENODEV; |
| 2494 | |
| 2495 | if (domain->ops->aux_attach_dev) |
| 2496 | ret = domain->ops->aux_attach_dev(domain, dev); |
| 2497 | |
| 2498 | if (!ret) |
| 2499 | trace_attach_device_to_domain(dev); |
| 2500 | |
| 2501 | return ret; |
| 2502 | } |
| 2503 | EXPORT_SYMBOL_GPL(iommu_aux_attach_device); |
| 2504 | |
| 2505 | void iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev) |
| 2506 | { |
| 2507 | if (domain->ops->aux_detach_dev) { |
| 2508 | domain->ops->aux_detach_dev(domain, dev); |
| 2509 | trace_detach_device_from_domain(dev); |
| 2510 | } |
| 2511 | } |
| 2512 | EXPORT_SYMBOL_GPL(iommu_aux_detach_device); |
| 2513 | |
| 2514 | int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev) |
| 2515 | { |
| 2516 | int ret = -ENODEV; |
| 2517 | |
| 2518 | if (domain->ops->aux_get_pasid) |
| 2519 | ret = domain->ops->aux_get_pasid(domain, dev); |
| 2520 | |
| 2521 | return ret; |
| 2522 | } |
| 2523 | EXPORT_SYMBOL_GPL(iommu_aux_get_pasid); |
Jean-Philippe Brucker | 26b25a2 | 2019-04-10 16:15:16 +0100 | [diff] [blame] | 2524 | |
| 2525 | /** |
| 2526 | * iommu_sva_bind_device() - Bind a process address space to a device |
| 2527 | * @dev: the device |
| 2528 | * @mm: the mm to bind, caller must hold a reference to it |
| 2529 | * |
| 2530 | * Create a bond between device and address space, allowing the device to access |
| 2531 | * the mm using the returned PASID. If a bond already exists between @device and |
| 2532 | * @mm, it is returned and an additional reference is taken. Caller must call |
| 2533 | * iommu_sva_unbind_device() to release each reference. |
| 2534 | * |
| 2535 | * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to |
| 2536 | * initialize the required SVA features. |
| 2537 | * |
| 2538 | * On error, returns an ERR_PTR value. |
| 2539 | */ |
| 2540 | struct iommu_sva * |
| 2541 | iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata) |
| 2542 | { |
| 2543 | struct iommu_group *group; |
| 2544 | struct iommu_sva *handle = ERR_PTR(-EINVAL); |
| 2545 | const struct iommu_ops *ops = dev->bus->iommu_ops; |
| 2546 | |
| 2547 | if (!ops || !ops->sva_bind) |
| 2548 | return ERR_PTR(-ENODEV); |
| 2549 | |
| 2550 | group = iommu_group_get(dev); |
| 2551 | if (!group) |
| 2552 | return ERR_PTR(-ENODEV); |
| 2553 | |
| 2554 | /* Ensure device count and domain don't change while we're binding */ |
| 2555 | mutex_lock(&group->mutex); |
| 2556 | |
| 2557 | /* |
| 2558 | * To keep things simple, SVA currently doesn't support IOMMU groups |
| 2559 | * with more than one device. Existing SVA-capable systems are not |
| 2560 | * affected by the problems that required IOMMU groups (lack of ACS |
| 2561 | * isolation, device ID aliasing and other hardware issues). |
| 2562 | */ |
| 2563 | if (iommu_group_device_count(group) != 1) |
| 2564 | goto out_unlock; |
| 2565 | |
| 2566 | handle = ops->sva_bind(dev, mm, drvdata); |
| 2567 | |
| 2568 | out_unlock: |
| 2569 | mutex_unlock(&group->mutex); |
| 2570 | iommu_group_put(group); |
| 2571 | |
| 2572 | return handle; |
| 2573 | } |
| 2574 | EXPORT_SYMBOL_GPL(iommu_sva_bind_device); |
| 2575 | |
| 2576 | /** |
| 2577 | * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device |
| 2578 | * @handle: the handle returned by iommu_sva_bind_device() |
| 2579 | * |
| 2580 | * Put reference to a bond between device and address space. The device should |
| 2581 | * not be issuing any more transaction for this PASID. All outstanding page |
| 2582 | * requests for this PASID must have been flushed to the IOMMU. |
| 2583 | * |
| 2584 | * Returns 0 on success, or an error value |
| 2585 | */ |
| 2586 | void iommu_sva_unbind_device(struct iommu_sva *handle) |
| 2587 | { |
| 2588 | struct iommu_group *group; |
| 2589 | struct device *dev = handle->dev; |
| 2590 | const struct iommu_ops *ops = dev->bus->iommu_ops; |
| 2591 | |
| 2592 | if (!ops || !ops->sva_unbind) |
| 2593 | return; |
| 2594 | |
| 2595 | group = iommu_group_get(dev); |
| 2596 | if (!group) |
| 2597 | return; |
| 2598 | |
| 2599 | mutex_lock(&group->mutex); |
| 2600 | ops->sva_unbind(handle); |
| 2601 | mutex_unlock(&group->mutex); |
| 2602 | |
| 2603 | iommu_group_put(group); |
| 2604 | } |
| 2605 | EXPORT_SYMBOL_GPL(iommu_sva_unbind_device); |
| 2606 | |
| 2607 | int iommu_sva_set_ops(struct iommu_sva *handle, |
| 2608 | const struct iommu_sva_ops *sva_ops) |
| 2609 | { |
| 2610 | if (handle->ops && handle->ops != sva_ops) |
| 2611 | return -EEXIST; |
| 2612 | |
| 2613 | handle->ops = sva_ops; |
| 2614 | return 0; |
| 2615 | } |
| 2616 | EXPORT_SYMBOL_GPL(iommu_sva_set_ops); |
| 2617 | |
| 2618 | int iommu_sva_get_pasid(struct iommu_sva *handle) |
| 2619 | { |
| 2620 | const struct iommu_ops *ops = handle->dev->bus->iommu_ops; |
| 2621 | |
| 2622 | if (!ops || !ops->sva_get_pasid) |
| 2623 | return IOMMU_PASID_INVALID; |
| 2624 | |
| 2625 | return ops->sva_get_pasid(handle); |
| 2626 | } |
| 2627 | EXPORT_SYMBOL_GPL(iommu_sva_get_pasid); |