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 28*da4b0275SRobin Murphy #define NO_IOMMU 1 29*da4b0275SRobin Murphy 301cd076bfSWill Deacon static const struct of_device_id __iommu_of_table_sentinel 311cd076bfSWill Deacon __used __section(__iommu_of_table_end); 321cd076bfSWill Deacon 334e0ee78fSHiroshi Doyu /** 344e0ee78fSHiroshi Doyu * of_get_dma_window - Parse *dma-window property and returns 0 if found. 354e0ee78fSHiroshi Doyu * 364e0ee78fSHiroshi Doyu * @dn: device node 374e0ee78fSHiroshi Doyu * @prefix: prefix for property name if any 384e0ee78fSHiroshi Doyu * @index: index to start to parse 394e0ee78fSHiroshi Doyu * @busno: Returns busno if supported. Otherwise pass NULL 404e0ee78fSHiroshi Doyu * @addr: Returns address that DMA starts 414e0ee78fSHiroshi Doyu * @size: Returns the range that DMA can handle 424e0ee78fSHiroshi Doyu * 434e0ee78fSHiroshi Doyu * This supports different formats flexibly. "prefix" can be 444e0ee78fSHiroshi Doyu * configured if any. "busno" and "index" are optionally 454e0ee78fSHiroshi Doyu * specified. Set 0(or NULL) if not used. 464e0ee78fSHiroshi Doyu */ 474e0ee78fSHiroshi Doyu int of_get_dma_window(struct device_node *dn, const char *prefix, int index, 484e0ee78fSHiroshi Doyu unsigned long *busno, dma_addr_t *addr, size_t *size) 494e0ee78fSHiroshi Doyu { 504e0ee78fSHiroshi Doyu const __be32 *dma_window, *end; 514e0ee78fSHiroshi Doyu int bytes, cur_index = 0; 524e0ee78fSHiroshi Doyu char propname[NAME_MAX], addrname[NAME_MAX], sizename[NAME_MAX]; 534e0ee78fSHiroshi Doyu 544e0ee78fSHiroshi Doyu if (!dn || !addr || !size) 554e0ee78fSHiroshi Doyu return -EINVAL; 564e0ee78fSHiroshi Doyu 574e0ee78fSHiroshi Doyu if (!prefix) 584e0ee78fSHiroshi Doyu prefix = ""; 594e0ee78fSHiroshi Doyu 604e0ee78fSHiroshi Doyu snprintf(propname, sizeof(propname), "%sdma-window", prefix); 614e0ee78fSHiroshi Doyu snprintf(addrname, sizeof(addrname), "%s#dma-address-cells", prefix); 624e0ee78fSHiroshi Doyu snprintf(sizename, sizeof(sizename), "%s#dma-size-cells", prefix); 634e0ee78fSHiroshi Doyu 644e0ee78fSHiroshi Doyu dma_window = of_get_property(dn, propname, &bytes); 654e0ee78fSHiroshi Doyu if (!dma_window) 664e0ee78fSHiroshi Doyu return -ENODEV; 674e0ee78fSHiroshi Doyu end = dma_window + bytes / sizeof(*dma_window); 684e0ee78fSHiroshi Doyu 694e0ee78fSHiroshi Doyu while (dma_window < end) { 704e0ee78fSHiroshi Doyu u32 cells; 714e0ee78fSHiroshi Doyu const void *prop; 724e0ee78fSHiroshi Doyu 734e0ee78fSHiroshi Doyu /* busno is one cell if supported */ 744e0ee78fSHiroshi Doyu if (busno) 754e0ee78fSHiroshi Doyu *busno = be32_to_cpup(dma_window++); 764e0ee78fSHiroshi Doyu 774e0ee78fSHiroshi Doyu prop = of_get_property(dn, addrname, NULL); 784e0ee78fSHiroshi Doyu if (!prop) 794e0ee78fSHiroshi Doyu prop = of_get_property(dn, "#address-cells", NULL); 804e0ee78fSHiroshi Doyu 814e0ee78fSHiroshi Doyu cells = prop ? be32_to_cpup(prop) : of_n_addr_cells(dn); 824e0ee78fSHiroshi Doyu if (!cells) 834e0ee78fSHiroshi Doyu return -EINVAL; 844e0ee78fSHiroshi Doyu *addr = of_read_number(dma_window, cells); 854e0ee78fSHiroshi Doyu dma_window += cells; 864e0ee78fSHiroshi Doyu 874e0ee78fSHiroshi Doyu prop = of_get_property(dn, sizename, NULL); 884e0ee78fSHiroshi Doyu cells = prop ? be32_to_cpup(prop) : of_n_size_cells(dn); 894e0ee78fSHiroshi Doyu if (!cells) 904e0ee78fSHiroshi Doyu return -EINVAL; 914e0ee78fSHiroshi Doyu *size = of_read_number(dma_window, cells); 924e0ee78fSHiroshi Doyu dma_window += cells; 934e0ee78fSHiroshi Doyu 944e0ee78fSHiroshi Doyu if (cur_index++ == index) 954e0ee78fSHiroshi Doyu break; 964e0ee78fSHiroshi Doyu } 974e0ee78fSHiroshi Doyu return 0; 984e0ee78fSHiroshi Doyu } 994e0ee78fSHiroshi Doyu EXPORT_SYMBOL_GPL(of_get_dma_window); 1001cd076bfSWill Deacon 101d7b05582SRobin Murphy static bool of_iommu_driver_present(struct device_node *np) 102d7b05582SRobin Murphy { 103d7b05582SRobin Murphy /* 104d7b05582SRobin Murphy * If the IOMMU still isn't ready by the time we reach init, assume 105d7b05582SRobin Murphy * it never will be. We don't want to defer indefinitely, nor attempt 106d7b05582SRobin Murphy * to dereference __iommu_of_table after it's been freed. 107d7b05582SRobin Murphy */ 108b903dfb2SThomas Gleixner if (system_state >= SYSTEM_RUNNING) 109d7b05582SRobin Murphy return false; 110d7b05582SRobin Murphy 111d7b05582SRobin Murphy return of_match_node(&__iommu_of_table, np); 112d7b05582SRobin Murphy } 113d7b05582SRobin Murphy 114*da4b0275SRobin Murphy static int of_iommu_xlate(struct device *dev, 115*da4b0275SRobin Murphy struct of_phandle_args *iommu_spec) 1162a0c5754SRobin Murphy { 1172a0c5754SRobin Murphy const struct iommu_ops *ops; 1182a0c5754SRobin Murphy struct fwnode_handle *fwnode = &iommu_spec->np->fwnode; 1192a0c5754SRobin Murphy int err; 1202a0c5754SRobin Murphy 1212a0c5754SRobin Murphy ops = iommu_ops_from_fwnode(fwnode); 122d7b05582SRobin Murphy if ((ops && !ops->of_xlate) || 123573d4175SSricharan R !of_device_is_available(iommu_spec->np) || 124d7b05582SRobin Murphy (!ops && !of_iommu_driver_present(iommu_spec->np))) 125*da4b0275SRobin Murphy return NO_IOMMU; 1262a0c5754SRobin Murphy 1272a0c5754SRobin Murphy err = iommu_fwspec_init(dev, &iommu_spec->np->fwnode, ops); 1282a0c5754SRobin Murphy if (err) 129*da4b0275SRobin Murphy return err; 130d7b05582SRobin Murphy /* 131d7b05582SRobin Murphy * The otherwise-empty fwspec handily serves to indicate the specific 132d7b05582SRobin Murphy * IOMMU device we're waiting for, which will be useful if we ever get 133d7b05582SRobin Murphy * a proper probe-ordering dependency mechanism in future. 134d7b05582SRobin Murphy */ 135d7b05582SRobin Murphy if (!ops) 136*da4b0275SRobin Murphy return -EPROBE_DEFER; 1372a0c5754SRobin Murphy 138*da4b0275SRobin Murphy return ops->of_xlate(dev, iommu_spec); 1392a0c5754SRobin Murphy } 1402a0c5754SRobin Murphy 141d87beb74SRobin Murphy struct of_pci_iommu_alias_info { 142d87beb74SRobin Murphy struct device *dev; 143d87beb74SRobin Murphy struct device_node *np; 144d87beb74SRobin Murphy }; 145b996444cSRobin Murphy 146d87beb74SRobin Murphy static int of_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data) 147b996444cSRobin Murphy { 148d87beb74SRobin Murphy struct of_pci_iommu_alias_info *info = data; 149d87beb74SRobin Murphy struct of_phandle_args iommu_spec = { .args_count = 1 }; 1502a0c5754SRobin Murphy int err; 151b996444cSRobin Murphy 152d87beb74SRobin Murphy err = of_pci_map_rid(info->np, alias, "iommu-map", 1532a0c5754SRobin Murphy "iommu-map-mask", &iommu_spec.np, 1542a0c5754SRobin Murphy iommu_spec.args); 1552a0c5754SRobin Murphy if (err) 156*da4b0275SRobin Murphy return err == -ENODEV ? NO_IOMMU : err; 157b996444cSRobin Murphy 158*da4b0275SRobin Murphy err = of_iommu_xlate(info->dev, &iommu_spec); 159b996444cSRobin Murphy of_node_put(iommu_spec.np); 160*da4b0275SRobin Murphy if (err) 161*da4b0275SRobin Murphy return err; 1627eba1d51SWill Deacon 163d87beb74SRobin Murphy return info->np == pdev->bus->dev.of_node; 1642a0c5754SRobin Murphy } 1657eba1d51SWill Deacon 1662a0c5754SRobin Murphy const struct iommu_ops *of_iommu_configure(struct device *dev, 1672a0c5754SRobin Murphy struct device_node *master_np) 1682a0c5754SRobin Murphy { 169d87beb74SRobin Murphy const struct iommu_ops *ops = NULL; 170d7b05582SRobin Murphy struct iommu_fwspec *fwspec = dev->iommu_fwspec; 171*da4b0275SRobin Murphy int err = NO_IOMMU; 1722a0c5754SRobin Murphy 1732a0c5754SRobin Murphy if (!master_np) 1747eba1d51SWill Deacon return NULL; 1752a0c5754SRobin Murphy 176d7b05582SRobin Murphy if (fwspec) { 177d7b05582SRobin Murphy if (fwspec->ops) 178d7b05582SRobin Murphy return fwspec->ops; 179d7b05582SRobin Murphy 180d7b05582SRobin Murphy /* In the deferred case, start again from scratch */ 181d7b05582SRobin Murphy iommu_fwspec_free(dev); 182d7b05582SRobin Murphy } 183d7b05582SRobin Murphy 184d87beb74SRobin Murphy /* 185d87beb74SRobin Murphy * We don't currently walk up the tree looking for a parent IOMMU. 186d87beb74SRobin Murphy * See the `Notes:' section of 187d87beb74SRobin Murphy * Documentation/devicetree/bindings/iommu/iommu.txt 188d87beb74SRobin Murphy */ 189d87beb74SRobin Murphy if (dev_is_pci(dev)) { 190d87beb74SRobin Murphy struct of_pci_iommu_alias_info info = { 191d87beb74SRobin Murphy .dev = dev, 192d87beb74SRobin Murphy .np = master_np, 193d87beb74SRobin Murphy }; 194d87beb74SRobin Murphy 195d87beb74SRobin Murphy err = pci_for_each_dma_alias(to_pci_dev(dev), 196d87beb74SRobin Murphy of_pci_iommu_init, &info); 197d87beb74SRobin Murphy } else { 198d87beb74SRobin Murphy struct of_phandle_args iommu_spec; 199d87beb74SRobin Murphy int idx = 0; 200d87beb74SRobin Murphy 201d87beb74SRobin Murphy while (!of_parse_phandle_with_args(master_np, "iommus", 202d87beb74SRobin Murphy "#iommu-cells", 203d87beb74SRobin Murphy idx, &iommu_spec)) { 204*da4b0275SRobin Murphy err = of_iommu_xlate(dev, &iommu_spec); 205d87beb74SRobin Murphy of_node_put(iommu_spec.np); 206d87beb74SRobin Murphy idx++; 207*da4b0275SRobin Murphy if (err) 208d87beb74SRobin Murphy break; 209d87beb74SRobin Murphy } 210d87beb74SRobin Murphy } 211*da4b0275SRobin Murphy 212*da4b0275SRobin Murphy /* 213*da4b0275SRobin Murphy * Two success conditions can be represented by non-negative err here: 214*da4b0275SRobin Murphy * >0 : there is no IOMMU, or one was unavailable for non-fatal reasons 215*da4b0275SRobin Murphy * 0 : we found an IOMMU, and dev->fwspec is initialised appropriately 216*da4b0275SRobin Murphy * <0 : any actual error 217*da4b0275SRobin Murphy */ 218*da4b0275SRobin Murphy if (!err) 219*da4b0275SRobin Murphy ops = dev->iommu_fwspec->ops; 220d7b05582SRobin Murphy /* 221d7b05582SRobin Murphy * If we have reason to believe the IOMMU driver missed the initial 222d7b05582SRobin Murphy * add_device callback for dev, replay it to get things in order. 223d7b05582SRobin Murphy */ 224*da4b0275SRobin Murphy if (ops && ops->add_device && dev->bus && !dev->iommu_group) 225d87beb74SRobin Murphy err = ops->add_device(dev); 2262a0c5754SRobin Murphy 227a37b19a3SSricharan R /* Ignore all other errors apart from EPROBE_DEFER */ 228*da4b0275SRobin Murphy if (err == -EPROBE_DEFER) { 229*da4b0275SRobin Murphy ops = ERR_PTR(err); 230*da4b0275SRobin Murphy } else if (err < 0) { 231*da4b0275SRobin Murphy dev_dbg(dev, "Adding to IOMMU failed: %d\n", err); 232a37b19a3SSricharan R ops = NULL; 233a37b19a3SSricharan R } 234a37b19a3SSricharan R 2357b07cbefSLaurent Pinchart return ops; 2367eba1d51SWill Deacon } 2377eba1d51SWill Deacon 238bb8e15d6SKefeng Wang static int __init of_iommu_init(void) 2391cd076bfSWill Deacon { 2401cd076bfSWill Deacon struct device_node *np; 2411cd076bfSWill Deacon const struct of_device_id *match, *matches = &__iommu_of_table; 2421cd076bfSWill Deacon 2431cd076bfSWill Deacon for_each_matching_node_and_match(np, matches, &match) { 2441cd076bfSWill Deacon const of_iommu_init_fn init_fn = match->data; 2451cd076bfSWill Deacon 2467b07cbefSLaurent Pinchart if (init_fn && init_fn(np)) 2476bd4f1c7SRob Herring pr_err("Failed to initialise IOMMU %pOF\n", np); 2481cd076bfSWill Deacon } 249bb8e15d6SKefeng Wang 250bb8e15d6SKefeng Wang return 0; 2511cd076bfSWill Deacon } 252bb8e15d6SKefeng Wang postcore_initcall_sync(of_iommu_init); 253