14e0ee78fSHiroshi Doyu /* 24e0ee78fSHiroshi Doyu * OF helpers for IOMMU 34e0ee78fSHiroshi Doyu * 44e0ee78fSHiroshi Doyu * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. 54e0ee78fSHiroshi Doyu * 64e0ee78fSHiroshi Doyu * This program is free software; you can redistribute it and/or modify it 74e0ee78fSHiroshi Doyu * under the terms and conditions of the GNU General Public License, 84e0ee78fSHiroshi Doyu * version 2, as published by the Free Software Foundation. 94e0ee78fSHiroshi Doyu * 104e0ee78fSHiroshi Doyu * This program is distributed in the hope it will be useful, but WITHOUT 114e0ee78fSHiroshi Doyu * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 124e0ee78fSHiroshi Doyu * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 134e0ee78fSHiroshi Doyu * more details. 144e0ee78fSHiroshi Doyu * 154e0ee78fSHiroshi Doyu * You should have received a copy of the GNU General Public License along with 164e0ee78fSHiroshi Doyu * this program; if not, write to the Free Software Foundation, Inc., 174e0ee78fSHiroshi Doyu * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. 184e0ee78fSHiroshi Doyu */ 194e0ee78fSHiroshi Doyu 204e0ee78fSHiroshi Doyu #include <linux/export.h> 217eba1d51SWill Deacon #include <linux/iommu.h> 224e0ee78fSHiroshi Doyu #include <linux/limits.h> 234e0ee78fSHiroshi Doyu #include <linux/of.h> 24cbff5634SBrian Norris #include <linux/of_iommu.h> 25b996444cSRobin Murphy #include <linux/of_pci.h> 26a42a7a1fSRobin Murphy #include <linux/slab.h> 274e0ee78fSHiroshi Doyu 281cd076bfSWill Deacon static const struct of_device_id __iommu_of_table_sentinel 291cd076bfSWill Deacon __used __section(__iommu_of_table_end); 301cd076bfSWill Deacon 314e0ee78fSHiroshi Doyu /** 324e0ee78fSHiroshi Doyu * of_get_dma_window - Parse *dma-window property and returns 0 if found. 334e0ee78fSHiroshi Doyu * 344e0ee78fSHiroshi Doyu * @dn: device node 354e0ee78fSHiroshi Doyu * @prefix: prefix for property name if any 364e0ee78fSHiroshi Doyu * @index: index to start to parse 374e0ee78fSHiroshi Doyu * @busno: Returns busno if supported. Otherwise pass NULL 384e0ee78fSHiroshi Doyu * @addr: Returns address that DMA starts 394e0ee78fSHiroshi Doyu * @size: Returns the range that DMA can handle 404e0ee78fSHiroshi Doyu * 414e0ee78fSHiroshi Doyu * This supports different formats flexibly. "prefix" can be 424e0ee78fSHiroshi Doyu * configured if any. "busno" and "index" are optionally 434e0ee78fSHiroshi Doyu * specified. Set 0(or NULL) if not used. 444e0ee78fSHiroshi Doyu */ 454e0ee78fSHiroshi Doyu int of_get_dma_window(struct device_node *dn, const char *prefix, int index, 464e0ee78fSHiroshi Doyu unsigned long *busno, dma_addr_t *addr, size_t *size) 474e0ee78fSHiroshi Doyu { 484e0ee78fSHiroshi Doyu const __be32 *dma_window, *end; 494e0ee78fSHiroshi Doyu int bytes, cur_index = 0; 504e0ee78fSHiroshi Doyu char propname[NAME_MAX], addrname[NAME_MAX], sizename[NAME_MAX]; 514e0ee78fSHiroshi Doyu 524e0ee78fSHiroshi Doyu if (!dn || !addr || !size) 534e0ee78fSHiroshi Doyu return -EINVAL; 544e0ee78fSHiroshi Doyu 554e0ee78fSHiroshi Doyu if (!prefix) 564e0ee78fSHiroshi Doyu prefix = ""; 574e0ee78fSHiroshi Doyu 584e0ee78fSHiroshi Doyu snprintf(propname, sizeof(propname), "%sdma-window", prefix); 594e0ee78fSHiroshi Doyu snprintf(addrname, sizeof(addrname), "%s#dma-address-cells", prefix); 604e0ee78fSHiroshi Doyu snprintf(sizename, sizeof(sizename), "%s#dma-size-cells", prefix); 614e0ee78fSHiroshi Doyu 624e0ee78fSHiroshi Doyu dma_window = of_get_property(dn, propname, &bytes); 634e0ee78fSHiroshi Doyu if (!dma_window) 644e0ee78fSHiroshi Doyu return -ENODEV; 654e0ee78fSHiroshi Doyu end = dma_window + bytes / sizeof(*dma_window); 664e0ee78fSHiroshi Doyu 674e0ee78fSHiroshi Doyu while (dma_window < end) { 684e0ee78fSHiroshi Doyu u32 cells; 694e0ee78fSHiroshi Doyu const void *prop; 704e0ee78fSHiroshi Doyu 714e0ee78fSHiroshi Doyu /* busno is one cell if supported */ 724e0ee78fSHiroshi Doyu if (busno) 734e0ee78fSHiroshi Doyu *busno = be32_to_cpup(dma_window++); 744e0ee78fSHiroshi Doyu 754e0ee78fSHiroshi Doyu prop = of_get_property(dn, addrname, NULL); 764e0ee78fSHiroshi Doyu if (!prop) 774e0ee78fSHiroshi Doyu prop = of_get_property(dn, "#address-cells", NULL); 784e0ee78fSHiroshi Doyu 794e0ee78fSHiroshi Doyu cells = prop ? be32_to_cpup(prop) : of_n_addr_cells(dn); 804e0ee78fSHiroshi Doyu if (!cells) 814e0ee78fSHiroshi Doyu return -EINVAL; 824e0ee78fSHiroshi Doyu *addr = of_read_number(dma_window, cells); 834e0ee78fSHiroshi Doyu dma_window += cells; 844e0ee78fSHiroshi Doyu 854e0ee78fSHiroshi Doyu prop = of_get_property(dn, sizename, NULL); 864e0ee78fSHiroshi Doyu cells = prop ? be32_to_cpup(prop) : of_n_size_cells(dn); 874e0ee78fSHiroshi Doyu if (!cells) 884e0ee78fSHiroshi Doyu return -EINVAL; 894e0ee78fSHiroshi Doyu *size = of_read_number(dma_window, cells); 904e0ee78fSHiroshi Doyu dma_window += cells; 914e0ee78fSHiroshi Doyu 924e0ee78fSHiroshi Doyu if (cur_index++ == index) 934e0ee78fSHiroshi Doyu break; 944e0ee78fSHiroshi Doyu } 954e0ee78fSHiroshi Doyu return 0; 964e0ee78fSHiroshi Doyu } 974e0ee78fSHiroshi Doyu EXPORT_SYMBOL_GPL(of_get_dma_window); 981cd076bfSWill Deacon 99d7b05582SRobin Murphy static bool of_iommu_driver_present(struct device_node *np) 100d7b05582SRobin Murphy { 101d7b05582SRobin Murphy /* 102d7b05582SRobin Murphy * If the IOMMU still isn't ready by the time we reach init, assume 103d7b05582SRobin Murphy * it never will be. We don't want to defer indefinitely, nor attempt 104d7b05582SRobin Murphy * to dereference __iommu_of_table after it's been freed. 105d7b05582SRobin Murphy */ 106b903dfb2SThomas Gleixner if (system_state >= SYSTEM_RUNNING) 107d7b05582SRobin Murphy return false; 108d7b05582SRobin Murphy 109d7b05582SRobin Murphy return of_match_node(&__iommu_of_table, np); 110d7b05582SRobin Murphy } 111d7b05582SRobin Murphy 1122a0c5754SRobin Murphy static const struct iommu_ops 1132a0c5754SRobin Murphy *of_iommu_xlate(struct device *dev, struct of_phandle_args *iommu_spec) 1142a0c5754SRobin Murphy { 1152a0c5754SRobin Murphy const struct iommu_ops *ops; 1162a0c5754SRobin Murphy struct fwnode_handle *fwnode = &iommu_spec->np->fwnode; 1172a0c5754SRobin Murphy int err; 1182a0c5754SRobin Murphy 1192a0c5754SRobin Murphy ops = iommu_ops_from_fwnode(fwnode); 120d7b05582SRobin Murphy if ((ops && !ops->of_xlate) || 121573d4175SSricharan R !of_device_is_available(iommu_spec->np) || 122d7b05582SRobin Murphy (!ops && !of_iommu_driver_present(iommu_spec->np))) 1232a0c5754SRobin Murphy return NULL; 1242a0c5754SRobin Murphy 1252a0c5754SRobin Murphy err = iommu_fwspec_init(dev, &iommu_spec->np->fwnode, ops); 1262a0c5754SRobin Murphy if (err) 1272a0c5754SRobin Murphy return ERR_PTR(err); 128d7b05582SRobin Murphy /* 129d7b05582SRobin Murphy * The otherwise-empty fwspec handily serves to indicate the specific 130d7b05582SRobin Murphy * IOMMU device we're waiting for, which will be useful if we ever get 131d7b05582SRobin Murphy * a proper probe-ordering dependency mechanism in future. 132d7b05582SRobin Murphy */ 133d7b05582SRobin Murphy if (!ops) 134d7b05582SRobin Murphy return ERR_PTR(-EPROBE_DEFER); 1352a0c5754SRobin Murphy 1362a0c5754SRobin Murphy err = ops->of_xlate(dev, iommu_spec); 1372a0c5754SRobin Murphy if (err) 1382a0c5754SRobin Murphy return ERR_PTR(err); 1392a0c5754SRobin Murphy 1402a0c5754SRobin Murphy return ops; 1412a0c5754SRobin Murphy } 1422a0c5754SRobin Murphy 143d87beb74SRobin Murphy struct of_pci_iommu_alias_info { 144d87beb74SRobin Murphy struct device *dev; 145d87beb74SRobin Murphy struct device_node *np; 146d87beb74SRobin Murphy }; 147b996444cSRobin Murphy 148d87beb74SRobin Murphy static int of_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data) 149b996444cSRobin Murphy { 150d87beb74SRobin Murphy struct of_pci_iommu_alias_info *info = data; 151b996444cSRobin Murphy const struct iommu_ops *ops; 152d87beb74SRobin Murphy struct of_phandle_args iommu_spec = { .args_count = 1 }; 1532a0c5754SRobin Murphy int err; 154b996444cSRobin Murphy 155d87beb74SRobin Murphy err = of_pci_map_rid(info->np, alias, "iommu-map", 1562a0c5754SRobin Murphy "iommu-map-mask", &iommu_spec.np, 1572a0c5754SRobin Murphy iommu_spec.args); 1582a0c5754SRobin Murphy if (err) 159d87beb74SRobin Murphy return err == -ENODEV ? 1 : err; 160b996444cSRobin Murphy 161d87beb74SRobin Murphy ops = of_iommu_xlate(info->dev, &iommu_spec); 162b996444cSRobin Murphy of_node_put(iommu_spec.np); 163b996444cSRobin Murphy 164d87beb74SRobin Murphy if (IS_ERR(ops)) 165d87beb74SRobin Murphy return PTR_ERR(ops); 1667eba1d51SWill Deacon 167d87beb74SRobin Murphy return info->np == pdev->bus->dev.of_node; 1682a0c5754SRobin Murphy } 1697eba1d51SWill Deacon 1702a0c5754SRobin Murphy const struct iommu_ops *of_iommu_configure(struct device *dev, 1712a0c5754SRobin Murphy struct device_node *master_np) 1722a0c5754SRobin Murphy { 173d87beb74SRobin Murphy const struct iommu_ops *ops = NULL; 174d7b05582SRobin Murphy struct iommu_fwspec *fwspec = dev->iommu_fwspec; 175d87beb74SRobin Murphy int err; 1762a0c5754SRobin Murphy 1772a0c5754SRobin Murphy if (!master_np) 1787eba1d51SWill Deacon return NULL; 1792a0c5754SRobin Murphy 180d7b05582SRobin Murphy if (fwspec) { 181d7b05582SRobin Murphy if (fwspec->ops) 182d7b05582SRobin Murphy return fwspec->ops; 183d7b05582SRobin Murphy 184d7b05582SRobin Murphy /* In the deferred case, start again from scratch */ 185d7b05582SRobin Murphy iommu_fwspec_free(dev); 186d7b05582SRobin Murphy } 187d7b05582SRobin Murphy 188d87beb74SRobin Murphy /* 189d87beb74SRobin Murphy * We don't currently walk up the tree looking for a parent IOMMU. 190d87beb74SRobin Murphy * See the `Notes:' section of 191d87beb74SRobin Murphy * Documentation/devicetree/bindings/iommu/iommu.txt 192d87beb74SRobin Murphy */ 193d87beb74SRobin Murphy if (dev_is_pci(dev)) { 194d87beb74SRobin Murphy struct of_pci_iommu_alias_info info = { 195d87beb74SRobin Murphy .dev = dev, 196d87beb74SRobin Murphy .np = master_np, 197d87beb74SRobin Murphy }; 198d87beb74SRobin Murphy 199d87beb74SRobin Murphy err = pci_for_each_dma_alias(to_pci_dev(dev), 200d87beb74SRobin Murphy of_pci_iommu_init, &info); 201d87beb74SRobin Murphy if (err) /* err > 0 means the walk stopped, but non-fatally */ 202d87beb74SRobin Murphy ops = ERR_PTR(min(err, 0)); 203d87beb74SRobin Murphy else /* success implies both fwspec and ops are now valid */ 204d87beb74SRobin Murphy ops = dev->iommu_fwspec->ops; 205d87beb74SRobin Murphy } else { 206d87beb74SRobin Murphy struct of_phandle_args iommu_spec; 207d87beb74SRobin Murphy int idx = 0; 208d87beb74SRobin Murphy 209d87beb74SRobin Murphy while (!of_parse_phandle_with_args(master_np, "iommus", 210d87beb74SRobin Murphy "#iommu-cells", 211d87beb74SRobin Murphy idx, &iommu_spec)) { 212d87beb74SRobin Murphy ops = of_iommu_xlate(dev, &iommu_spec); 213d87beb74SRobin Murphy of_node_put(iommu_spec.np); 214d87beb74SRobin Murphy idx++; 215d87beb74SRobin Murphy if (IS_ERR_OR_NULL(ops)) 216d87beb74SRobin Murphy break; 217d87beb74SRobin Murphy } 218d87beb74SRobin Murphy } 219d7b05582SRobin Murphy /* 220d7b05582SRobin Murphy * If we have reason to believe the IOMMU driver missed the initial 221d7b05582SRobin Murphy * add_device callback for dev, replay it to get things in order. 222d7b05582SRobin Murphy */ 223d7b05582SRobin Murphy if (!IS_ERR_OR_NULL(ops) && ops->add_device && 224d7b05582SRobin Murphy dev->bus && !dev->iommu_group) { 225d87beb74SRobin Murphy err = ops->add_device(dev); 226d7b05582SRobin Murphy if (err) 227d7b05582SRobin Murphy ops = ERR_PTR(err); 228d7b05582SRobin Murphy } 2292a0c5754SRobin Murphy 230a37b19a3SSricharan R /* Ignore all other errors apart from EPROBE_DEFER */ 231a37b19a3SSricharan R if (IS_ERR(ops) && (PTR_ERR(ops) != -EPROBE_DEFER)) { 232a37b19a3SSricharan R dev_dbg(dev, "Adding to IOMMU failed: %ld\n", PTR_ERR(ops)); 233a37b19a3SSricharan R ops = NULL; 234a37b19a3SSricharan R } 235a37b19a3SSricharan R 2367b07cbefSLaurent Pinchart return ops; 2377eba1d51SWill Deacon } 2387eba1d51SWill Deacon 239bb8e15d6SKefeng Wang static int __init of_iommu_init(void) 2401cd076bfSWill Deacon { 2411cd076bfSWill Deacon struct device_node *np; 2421cd076bfSWill Deacon const struct of_device_id *match, *matches = &__iommu_of_table; 2431cd076bfSWill Deacon 2441cd076bfSWill Deacon for_each_matching_node_and_match(np, matches, &match) { 2451cd076bfSWill Deacon const of_iommu_init_fn init_fn = match->data; 2461cd076bfSWill Deacon 2477b07cbefSLaurent Pinchart if (init_fn && init_fn(np)) 248*6bd4f1c7SRob Herring pr_err("Failed to initialise IOMMU %pOF\n", np); 2491cd076bfSWill Deacon } 250bb8e15d6SKefeng Wang 251bb8e15d6SKefeng Wang return 0; 2521cd076bfSWill Deacon } 253bb8e15d6SKefeng Wang postcore_initcall_sync(of_iommu_init); 254