| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * OF helpers for IOMMU |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. |
|---|
| 5 | | - * |
|---|
| 6 | | - * This program is free software; you can redistribute it and/or modify it |
|---|
| 7 | | - * under the terms and conditions of the GNU General Public License, |
|---|
| 8 | | - * version 2, as published by the Free Software Foundation. |
|---|
| 9 | | - * |
|---|
| 10 | | - * This program is distributed in the hope it will be useful, but WITHOUT |
|---|
| 11 | | - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|---|
| 12 | | - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
|---|
| 13 | | - * more details. |
|---|
| 14 | | - * |
|---|
| 15 | | - * You should have received a copy of the GNU General Public License along with |
|---|
| 16 | | - * this program; if not, write to the Free Software Foundation, Inc., |
|---|
| 17 | | - * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. |
|---|
| 18 | 6 | */ |
|---|
| 19 | 7 | |
|---|
| 20 | 8 | #include <linux/export.h> |
|---|
| 21 | 9 | #include <linux/iommu.h> |
|---|
| 22 | 10 | #include <linux/limits.h> |
|---|
| 11 | +#include <linux/module.h> |
|---|
| 12 | +#include <linux/msi.h> |
|---|
| 23 | 13 | #include <linux/of.h> |
|---|
| 24 | 14 | #include <linux/of_iommu.h> |
|---|
| 25 | 15 | #include <linux/of_pci.h> |
|---|
| 16 | +#include <linux/pci.h> |
|---|
| 26 | 17 | #include <linux/slab.h> |
|---|
| 18 | +#include <linux/fsl/mc.h> |
|---|
| 27 | 19 | |
|---|
| 28 | 20 | #define NO_IOMMU 1 |
|---|
| 29 | 21 | |
|---|
| .. | .. |
|---|
| 100 | 92 | { |
|---|
| 101 | 93 | const struct iommu_ops *ops; |
|---|
| 102 | 94 | struct fwnode_handle *fwnode = &iommu_spec->np->fwnode; |
|---|
| 103 | | - int err; |
|---|
| 95 | + int ret; |
|---|
| 104 | 96 | |
|---|
| 105 | 97 | ops = iommu_ops_from_fwnode(fwnode); |
|---|
| 106 | 98 | if ((ops && !ops->of_xlate) || |
|---|
| 107 | 99 | !of_device_is_available(iommu_spec->np)) |
|---|
| 108 | 100 | return NO_IOMMU; |
|---|
| 109 | 101 | |
|---|
| 110 | | - err = iommu_fwspec_init(dev, &iommu_spec->np->fwnode, ops); |
|---|
| 111 | | - if (err) |
|---|
| 112 | | - return err; |
|---|
| 102 | + ret = iommu_fwspec_init(dev, &iommu_spec->np->fwnode, ops); |
|---|
| 103 | + if (ret) |
|---|
| 104 | + return ret; |
|---|
| 113 | 105 | /* |
|---|
| 114 | 106 | * The otherwise-empty fwspec handily serves to indicate the specific |
|---|
| 115 | 107 | * IOMMU device we're waiting for, which will be useful if we ever get |
|---|
| .. | .. |
|---|
| 118 | 110 | if (!ops) |
|---|
| 119 | 111 | return driver_deferred_probe_check_state(dev); |
|---|
| 120 | 112 | |
|---|
| 121 | | - return ops->of_xlate(dev, iommu_spec); |
|---|
| 113 | + if (!try_module_get(ops->owner)) |
|---|
| 114 | + return -ENODEV; |
|---|
| 115 | + |
|---|
| 116 | + ret = ops->of_xlate(dev, iommu_spec); |
|---|
| 117 | + module_put(ops->owner); |
|---|
| 118 | + return ret; |
|---|
| 119 | +} |
|---|
| 120 | + |
|---|
| 121 | +static int of_iommu_configure_dev_id(struct device_node *master_np, |
|---|
| 122 | + struct device *dev, |
|---|
| 123 | + const u32 *id) |
|---|
| 124 | +{ |
|---|
| 125 | + struct of_phandle_args iommu_spec = { .args_count = 1 }; |
|---|
| 126 | + int err; |
|---|
| 127 | + |
|---|
| 128 | + err = of_map_id(master_np, *id, "iommu-map", |
|---|
| 129 | + "iommu-map-mask", &iommu_spec.np, |
|---|
| 130 | + iommu_spec.args); |
|---|
| 131 | + if (err) |
|---|
| 132 | + return err == -ENODEV ? NO_IOMMU : err; |
|---|
| 133 | + |
|---|
| 134 | + err = of_iommu_xlate(dev, &iommu_spec); |
|---|
| 135 | + of_node_put(iommu_spec.np); |
|---|
| 136 | + return err; |
|---|
| 137 | +} |
|---|
| 138 | + |
|---|
| 139 | +static int of_iommu_configure_dev(struct device_node *master_np, |
|---|
| 140 | + struct device *dev) |
|---|
| 141 | +{ |
|---|
| 142 | + struct of_phandle_args iommu_spec; |
|---|
| 143 | + int err = NO_IOMMU, idx = 0; |
|---|
| 144 | + |
|---|
| 145 | + while (!of_parse_phandle_with_args(master_np, "iommus", |
|---|
| 146 | + "#iommu-cells", |
|---|
| 147 | + idx, &iommu_spec)) { |
|---|
| 148 | + err = of_iommu_xlate(dev, &iommu_spec); |
|---|
| 149 | + of_node_put(iommu_spec.np); |
|---|
| 150 | + idx++; |
|---|
| 151 | + if (err) |
|---|
| 152 | + break; |
|---|
| 153 | + } |
|---|
| 154 | + |
|---|
| 155 | + return err; |
|---|
| 122 | 156 | } |
|---|
| 123 | 157 | |
|---|
| 124 | 158 | struct of_pci_iommu_alias_info { |
|---|
| .. | .. |
|---|
| 129 | 163 | static int of_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data) |
|---|
| 130 | 164 | { |
|---|
| 131 | 165 | struct of_pci_iommu_alias_info *info = data; |
|---|
| 132 | | - struct of_phandle_args iommu_spec = { .args_count = 1 }; |
|---|
| 133 | | - int err; |
|---|
| 166 | + u32 input_id = alias; |
|---|
| 134 | 167 | |
|---|
| 135 | | - err = of_pci_map_rid(info->np, alias, "iommu-map", |
|---|
| 136 | | - "iommu-map-mask", &iommu_spec.np, |
|---|
| 137 | | - iommu_spec.args); |
|---|
| 138 | | - if (err) |
|---|
| 139 | | - return err == -ENODEV ? NO_IOMMU : err; |
|---|
| 168 | + return of_iommu_configure_dev_id(info->np, info->dev, &input_id); |
|---|
| 169 | +} |
|---|
| 140 | 170 | |
|---|
| 141 | | - err = of_iommu_xlate(info->dev, &iommu_spec); |
|---|
| 142 | | - of_node_put(iommu_spec.np); |
|---|
| 143 | | - return err; |
|---|
| 171 | +static int of_iommu_configure_device(struct device_node *master_np, |
|---|
| 172 | + struct device *dev, const u32 *id) |
|---|
| 173 | +{ |
|---|
| 174 | + return (id) ? of_iommu_configure_dev_id(master_np, dev, id) : |
|---|
| 175 | + of_iommu_configure_dev(master_np, dev); |
|---|
| 144 | 176 | } |
|---|
| 145 | 177 | |
|---|
| 146 | 178 | const struct iommu_ops *of_iommu_configure(struct device *dev, |
|---|
| 147 | | - struct device_node *master_np) |
|---|
| 179 | + struct device_node *master_np, |
|---|
| 180 | + const u32 *id) |
|---|
| 148 | 181 | { |
|---|
| 149 | 182 | const struct iommu_ops *ops = NULL; |
|---|
| 150 | | - struct iommu_fwspec *fwspec = dev->iommu_fwspec; |
|---|
| 183 | + struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); |
|---|
| 151 | 184 | int err = NO_IOMMU; |
|---|
| 152 | 185 | |
|---|
| 153 | 186 | if (!master_np) |
|---|
| .. | .. |
|---|
| 172 | 205 | .np = master_np, |
|---|
| 173 | 206 | }; |
|---|
| 174 | 207 | |
|---|
| 208 | + pci_request_acs(); |
|---|
| 175 | 209 | err = pci_for_each_dma_alias(to_pci_dev(dev), |
|---|
| 176 | 210 | of_pci_iommu_init, &info); |
|---|
| 177 | 211 | } else { |
|---|
| 178 | | - struct of_phandle_args iommu_spec; |
|---|
| 179 | | - int idx = 0; |
|---|
| 212 | + err = of_iommu_configure_device(master_np, dev, id); |
|---|
| 180 | 213 | |
|---|
| 181 | | - while (!of_parse_phandle_with_args(master_np, "iommus", |
|---|
| 182 | | - "#iommu-cells", |
|---|
| 183 | | - idx, &iommu_spec)) { |
|---|
| 184 | | - err = of_iommu_xlate(dev, &iommu_spec); |
|---|
| 185 | | - of_node_put(iommu_spec.np); |
|---|
| 186 | | - idx++; |
|---|
| 187 | | - if (err) |
|---|
| 188 | | - break; |
|---|
| 189 | | - } |
|---|
| 214 | + fwspec = dev_iommu_fwspec_get(dev); |
|---|
| 215 | + if (!err && fwspec) |
|---|
| 216 | + of_property_read_u32(master_np, "pasid-num-bits", |
|---|
| 217 | + &fwspec->num_pasid_bits); |
|---|
| 190 | 218 | } |
|---|
| 191 | 219 | |
|---|
| 192 | 220 | /* |
|---|
| .. | .. |
|---|
| 195 | 223 | * 0 : we found an IOMMU, and dev->fwspec is initialised appropriately |
|---|
| 196 | 224 | * <0 : any actual error |
|---|
| 197 | 225 | */ |
|---|
| 198 | | - if (!err) |
|---|
| 199 | | - ops = dev->iommu_fwspec->ops; |
|---|
| 226 | + if (!err) { |
|---|
| 227 | + /* The fwspec pointer changed, read it again */ |
|---|
| 228 | + fwspec = dev_iommu_fwspec_get(dev); |
|---|
| 229 | + ops = fwspec->ops; |
|---|
| 230 | + } |
|---|
| 200 | 231 | /* |
|---|
| 201 | 232 | * If we have reason to believe the IOMMU driver missed the initial |
|---|
| 202 | | - * add_device callback for dev, replay it to get things in order. |
|---|
| 233 | + * probe for dev, replay it to get things in order. |
|---|
| 203 | 234 | */ |
|---|
| 204 | | - if (ops && ops->add_device && dev->bus && !dev->iommu_group) |
|---|
| 205 | | - err = ops->add_device(dev); |
|---|
| 235 | + if (!err && dev->bus && !device_iommu_mapped(dev)) |
|---|
| 236 | + err = iommu_probe_device(dev); |
|---|
| 206 | 237 | |
|---|
| 207 | 238 | /* Ignore all other errors apart from EPROBE_DEFER */ |
|---|
| 208 | 239 | if (err == -EPROBE_DEFER) { |
|---|