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 */ 106d7b05582SRobin Murphy if (system_state > SYSTEM_BOOTING) 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) || 121*573d4175SSricharan 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 143b996444cSRobin Murphy static int __get_pci_rid(struct pci_dev *pdev, u16 alias, void *data) 144b996444cSRobin Murphy { 145b996444cSRobin Murphy struct of_phandle_args *iommu_spec = data; 146b996444cSRobin Murphy 147b996444cSRobin Murphy iommu_spec->args[0] = alias; 148b996444cSRobin Murphy return iommu_spec->np == pdev->bus->dev.of_node; 149b996444cSRobin Murphy } 150b996444cSRobin Murphy 151b996444cSRobin Murphy static const struct iommu_ops 1522a0c5754SRobin Murphy *of_pci_iommu_init(struct pci_dev *pdev, struct device_node *bridge_np) 153b996444cSRobin Murphy { 154b996444cSRobin Murphy const struct iommu_ops *ops; 155b996444cSRobin Murphy struct of_phandle_args iommu_spec; 1562a0c5754SRobin Murphy int err; 157b996444cSRobin Murphy 158b996444cSRobin Murphy /* 159b996444cSRobin Murphy * Start by tracing the RID alias down the PCI topology as 160b996444cSRobin Murphy * far as the host bridge whose OF node we have... 161b996444cSRobin Murphy * (we're not even attempting to handle multi-alias devices yet) 162b996444cSRobin Murphy */ 163b996444cSRobin Murphy iommu_spec.args_count = 1; 164b996444cSRobin Murphy iommu_spec.np = bridge_np; 165b996444cSRobin Murphy pci_for_each_dma_alias(pdev, __get_pci_rid, &iommu_spec); 166b996444cSRobin Murphy /* 167b996444cSRobin Murphy * ...then find out what that becomes once it escapes the PCI 168b996444cSRobin Murphy * bus into the system beyond, and which IOMMU it ends up at. 169b996444cSRobin Murphy */ 170b996444cSRobin Murphy iommu_spec.np = NULL; 1712a0c5754SRobin Murphy err = of_pci_map_rid(bridge_np, iommu_spec.args[0], "iommu-map", 1722a0c5754SRobin Murphy "iommu-map-mask", &iommu_spec.np, 1732a0c5754SRobin Murphy iommu_spec.args); 1742a0c5754SRobin Murphy if (err) 1752a0c5754SRobin Murphy return err == -ENODEV ? NULL : ERR_PTR(err); 176b996444cSRobin Murphy 1772a0c5754SRobin Murphy ops = of_iommu_xlate(&pdev->dev, &iommu_spec); 178b996444cSRobin Murphy 179b996444cSRobin Murphy of_node_put(iommu_spec.np); 180b996444cSRobin Murphy return ops; 181b996444cSRobin Murphy } 182b996444cSRobin Murphy 1832a0c5754SRobin Murphy static const struct iommu_ops 1842a0c5754SRobin Murphy *of_platform_iommu_init(struct device *dev, struct device_node *np) 1857eba1d51SWill Deacon { 1867eba1d51SWill Deacon struct of_phandle_args iommu_spec; 18753c92d79SRobin Murphy const struct iommu_ops *ops = NULL; 1887eba1d51SWill Deacon int idx = 0; 1897eba1d51SWill Deacon 1907eba1d51SWill Deacon /* 1917eba1d51SWill Deacon * We don't currently walk up the tree looking for a parent IOMMU. 1927eba1d51SWill Deacon * See the `Notes:' section of 1937eba1d51SWill Deacon * Documentation/devicetree/bindings/iommu/iommu.txt 1947eba1d51SWill Deacon */ 1952a0c5754SRobin Murphy while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", 1962a0c5754SRobin Murphy idx, &iommu_spec)) { 1972a0c5754SRobin Murphy ops = of_iommu_xlate(dev, &iommu_spec); 1982a0c5754SRobin Murphy of_node_put(iommu_spec.np); 1997eba1d51SWill Deacon idx++; 2002a0c5754SRobin Murphy if (IS_ERR_OR_NULL(ops)) 2012a0c5754SRobin Murphy break; 2027eba1d51SWill Deacon } 2037eba1d51SWill Deacon 2047eba1d51SWill Deacon return ops; 2052a0c5754SRobin Murphy } 2067eba1d51SWill Deacon 2072a0c5754SRobin Murphy const struct iommu_ops *of_iommu_configure(struct device *dev, 2082a0c5754SRobin Murphy struct device_node *master_np) 2092a0c5754SRobin Murphy { 2102a0c5754SRobin Murphy const struct iommu_ops *ops; 211d7b05582SRobin Murphy struct iommu_fwspec *fwspec = dev->iommu_fwspec; 2122a0c5754SRobin Murphy 2132a0c5754SRobin Murphy if (!master_np) 2147eba1d51SWill Deacon return NULL; 2152a0c5754SRobin Murphy 216d7b05582SRobin Murphy if (fwspec) { 217d7b05582SRobin Murphy if (fwspec->ops) 218d7b05582SRobin Murphy return fwspec->ops; 219d7b05582SRobin Murphy 220d7b05582SRobin Murphy /* In the deferred case, start again from scratch */ 221d7b05582SRobin Murphy iommu_fwspec_free(dev); 222d7b05582SRobin Murphy } 223d7b05582SRobin Murphy 2242a0c5754SRobin Murphy if (dev_is_pci(dev)) 2252a0c5754SRobin Murphy ops = of_pci_iommu_init(to_pci_dev(dev), master_np); 2262a0c5754SRobin Murphy else 2272a0c5754SRobin Murphy ops = of_platform_iommu_init(dev, master_np); 228d7b05582SRobin Murphy /* 229d7b05582SRobin Murphy * If we have reason to believe the IOMMU driver missed the initial 230d7b05582SRobin Murphy * add_device callback for dev, replay it to get things in order. 231d7b05582SRobin Murphy */ 232d7b05582SRobin Murphy if (!IS_ERR_OR_NULL(ops) && ops->add_device && 233d7b05582SRobin Murphy dev->bus && !dev->iommu_group) { 234d7b05582SRobin Murphy int err = ops->add_device(dev); 235d7b05582SRobin Murphy 236d7b05582SRobin Murphy if (err) 237d7b05582SRobin Murphy ops = ERR_PTR(err); 238d7b05582SRobin Murphy } 2392a0c5754SRobin Murphy 2407b07cbefSLaurent Pinchart return ops; 2417eba1d51SWill Deacon } 2427eba1d51SWill Deacon 243bb8e15d6SKefeng Wang static int __init of_iommu_init(void) 2441cd076bfSWill Deacon { 2451cd076bfSWill Deacon struct device_node *np; 2461cd076bfSWill Deacon const struct of_device_id *match, *matches = &__iommu_of_table; 2471cd076bfSWill Deacon 2481cd076bfSWill Deacon for_each_matching_node_and_match(np, matches, &match) { 2491cd076bfSWill Deacon const of_iommu_init_fn init_fn = match->data; 2501cd076bfSWill Deacon 2517b07cbefSLaurent Pinchart if (init_fn && init_fn(np)) 2521cd076bfSWill Deacon pr_err("Failed to initialise IOMMU %s\n", 2531cd076bfSWill Deacon of_node_full_name(np)); 2541cd076bfSWill Deacon } 255bb8e15d6SKefeng Wang 256bb8e15d6SKefeng Wang return 0; 2571cd076bfSWill Deacon } 258bb8e15d6SKefeng Wang postcore_initcall_sync(of_iommu_init); 259