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