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