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