1a61127c2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 24e0ee78fSHiroshi Doyu /* 34e0ee78fSHiroshi Doyu * OF helpers for IOMMU 44e0ee78fSHiroshi Doyu * 54e0ee78fSHiroshi Doyu * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. 64e0ee78fSHiroshi Doyu */ 74e0ee78fSHiroshi Doyu 84e0ee78fSHiroshi Doyu #include <linux/export.h> 97eba1d51SWill Deacon #include <linux/iommu.h> 104e0ee78fSHiroshi Doyu #include <linux/limits.h> 11bbd8810dSKrzysztof Wilczynski #include <linux/pci.h> 12bbd8810dSKrzysztof Wilczynski #include <linux/msi.h> 134e0ee78fSHiroshi Doyu #include <linux/of.h> 14cbff5634SBrian Norris #include <linux/of_iommu.h> 15b996444cSRobin Murphy #include <linux/of_pci.h> 16a42a7a1fSRobin Murphy #include <linux/slab.h> 17fa0656b4SNipun Gupta #include <linux/fsl/mc.h> 184e0ee78fSHiroshi Doyu 19da4b0275SRobin Murphy #define NO_IOMMU 1 20da4b0275SRobin Murphy 214e0ee78fSHiroshi Doyu /** 224e0ee78fSHiroshi Doyu * of_get_dma_window - Parse *dma-window property and returns 0 if found. 234e0ee78fSHiroshi Doyu * 244e0ee78fSHiroshi Doyu * @dn: device node 254e0ee78fSHiroshi Doyu * @prefix: prefix for property name if any 264e0ee78fSHiroshi Doyu * @index: index to start to parse 274e0ee78fSHiroshi Doyu * @busno: Returns busno if supported. Otherwise pass NULL 284e0ee78fSHiroshi Doyu * @addr: Returns address that DMA starts 294e0ee78fSHiroshi Doyu * @size: Returns the range that DMA can handle 304e0ee78fSHiroshi Doyu * 314e0ee78fSHiroshi Doyu * This supports different formats flexibly. "prefix" can be 324e0ee78fSHiroshi Doyu * configured if any. "busno" and "index" are optionally 334e0ee78fSHiroshi Doyu * specified. Set 0(or NULL) if not used. 344e0ee78fSHiroshi Doyu */ 354e0ee78fSHiroshi Doyu int of_get_dma_window(struct device_node *dn, const char *prefix, int index, 364e0ee78fSHiroshi Doyu unsigned long *busno, dma_addr_t *addr, size_t *size) 374e0ee78fSHiroshi Doyu { 384e0ee78fSHiroshi Doyu const __be32 *dma_window, *end; 394e0ee78fSHiroshi Doyu int bytes, cur_index = 0; 404e0ee78fSHiroshi Doyu char propname[NAME_MAX], addrname[NAME_MAX], sizename[NAME_MAX]; 414e0ee78fSHiroshi Doyu 424e0ee78fSHiroshi Doyu if (!dn || !addr || !size) 434e0ee78fSHiroshi Doyu return -EINVAL; 444e0ee78fSHiroshi Doyu 454e0ee78fSHiroshi Doyu if (!prefix) 464e0ee78fSHiroshi Doyu prefix = ""; 474e0ee78fSHiroshi Doyu 484e0ee78fSHiroshi Doyu snprintf(propname, sizeof(propname), "%sdma-window", prefix); 494e0ee78fSHiroshi Doyu snprintf(addrname, sizeof(addrname), "%s#dma-address-cells", prefix); 504e0ee78fSHiroshi Doyu snprintf(sizename, sizeof(sizename), "%s#dma-size-cells", prefix); 514e0ee78fSHiroshi Doyu 524e0ee78fSHiroshi Doyu dma_window = of_get_property(dn, propname, &bytes); 534e0ee78fSHiroshi Doyu if (!dma_window) 544e0ee78fSHiroshi Doyu return -ENODEV; 554e0ee78fSHiroshi Doyu end = dma_window + bytes / sizeof(*dma_window); 564e0ee78fSHiroshi Doyu 574e0ee78fSHiroshi Doyu while (dma_window < end) { 584e0ee78fSHiroshi Doyu u32 cells; 594e0ee78fSHiroshi Doyu const void *prop; 604e0ee78fSHiroshi Doyu 614e0ee78fSHiroshi Doyu /* busno is one cell if supported */ 624e0ee78fSHiroshi Doyu if (busno) 634e0ee78fSHiroshi Doyu *busno = be32_to_cpup(dma_window++); 644e0ee78fSHiroshi Doyu 654e0ee78fSHiroshi Doyu prop = of_get_property(dn, addrname, NULL); 664e0ee78fSHiroshi Doyu if (!prop) 674e0ee78fSHiroshi Doyu prop = of_get_property(dn, "#address-cells", NULL); 684e0ee78fSHiroshi Doyu 694e0ee78fSHiroshi Doyu cells = prop ? be32_to_cpup(prop) : of_n_addr_cells(dn); 704e0ee78fSHiroshi Doyu if (!cells) 714e0ee78fSHiroshi Doyu return -EINVAL; 724e0ee78fSHiroshi Doyu *addr = of_read_number(dma_window, cells); 734e0ee78fSHiroshi Doyu dma_window += cells; 744e0ee78fSHiroshi Doyu 754e0ee78fSHiroshi Doyu prop = of_get_property(dn, sizename, NULL); 764e0ee78fSHiroshi Doyu cells = prop ? be32_to_cpup(prop) : of_n_size_cells(dn); 774e0ee78fSHiroshi Doyu if (!cells) 784e0ee78fSHiroshi Doyu return -EINVAL; 794e0ee78fSHiroshi Doyu *size = of_read_number(dma_window, cells); 804e0ee78fSHiroshi Doyu dma_window += cells; 814e0ee78fSHiroshi Doyu 824e0ee78fSHiroshi Doyu if (cur_index++ == index) 834e0ee78fSHiroshi Doyu break; 844e0ee78fSHiroshi Doyu } 854e0ee78fSHiroshi Doyu return 0; 864e0ee78fSHiroshi Doyu } 874e0ee78fSHiroshi Doyu EXPORT_SYMBOL_GPL(of_get_dma_window); 881cd076bfSWill Deacon 89da4b0275SRobin Murphy static int of_iommu_xlate(struct device *dev, 90da4b0275SRobin Murphy struct of_phandle_args *iommu_spec) 912a0c5754SRobin Murphy { 922a0c5754SRobin Murphy const struct iommu_ops *ops; 932a0c5754SRobin Murphy struct fwnode_handle *fwnode = &iommu_spec->np->fwnode; 942a0c5754SRobin Murphy int err; 952a0c5754SRobin Murphy 962a0c5754SRobin Murphy ops = iommu_ops_from_fwnode(fwnode); 97d7b05582SRobin Murphy if ((ops && !ops->of_xlate) || 98ac6bbf0cSRob Herring !of_device_is_available(iommu_spec->np)) 99da4b0275SRobin Murphy return NO_IOMMU; 1002a0c5754SRobin Murphy 1012a0c5754SRobin Murphy err = iommu_fwspec_init(dev, &iommu_spec->np->fwnode, ops); 1022a0c5754SRobin Murphy if (err) 103da4b0275SRobin Murphy return err; 104d7b05582SRobin Murphy /* 105d7b05582SRobin Murphy * The otherwise-empty fwspec handily serves to indicate the specific 106d7b05582SRobin Murphy * IOMMU device we're waiting for, which will be useful if we ever get 107d7b05582SRobin Murphy * a proper probe-ordering dependency mechanism in future. 108d7b05582SRobin Murphy */ 109d7b05582SRobin Murphy if (!ops) 11078f307beSRob Herring return driver_deferred_probe_check_state(dev); 1112a0c5754SRobin Murphy 112da4b0275SRobin Murphy return ops->of_xlate(dev, iommu_spec); 1132a0c5754SRobin Murphy } 1142a0c5754SRobin Murphy 115d87beb74SRobin Murphy struct of_pci_iommu_alias_info { 116d87beb74SRobin Murphy struct device *dev; 117d87beb74SRobin Murphy struct device_node *np; 118d87beb74SRobin Murphy }; 119b996444cSRobin Murphy 120d87beb74SRobin Murphy static int of_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data) 121b996444cSRobin Murphy { 122d87beb74SRobin Murphy struct of_pci_iommu_alias_info *info = data; 123d87beb74SRobin Murphy struct of_phandle_args iommu_spec = { .args_count = 1 }; 1242a0c5754SRobin Murphy int err; 125b996444cSRobin Murphy 1262a6db719SNipun Gupta err = of_map_rid(info->np, alias, "iommu-map", "iommu-map-mask", 1272a6db719SNipun Gupta &iommu_spec.np, iommu_spec.args); 1282a0c5754SRobin Murphy if (err) 129da4b0275SRobin Murphy return err == -ENODEV ? NO_IOMMU : err; 130b996444cSRobin Murphy 131da4b0275SRobin Murphy err = of_iommu_xlate(info->dev, &iommu_spec); 132b996444cSRobin Murphy of_node_put(iommu_spec.np); 133da4b0275SRobin Murphy return err; 1342a0c5754SRobin Murphy } 1357eba1d51SWill Deacon 136fa0656b4SNipun Gupta static int of_fsl_mc_iommu_init(struct fsl_mc_device *mc_dev, 137fa0656b4SNipun Gupta struct device_node *master_np) 138fa0656b4SNipun Gupta { 139fa0656b4SNipun Gupta struct of_phandle_args iommu_spec = { .args_count = 1 }; 140fa0656b4SNipun Gupta int err; 141fa0656b4SNipun Gupta 142fa0656b4SNipun Gupta err = of_map_rid(master_np, mc_dev->icid, "iommu-map", 143fa0656b4SNipun Gupta "iommu-map-mask", &iommu_spec.np, 144fa0656b4SNipun Gupta iommu_spec.args); 145fa0656b4SNipun Gupta if (err) 146fa0656b4SNipun Gupta return err == -ENODEV ? NO_IOMMU : err; 147fa0656b4SNipun Gupta 148fa0656b4SNipun Gupta err = of_iommu_xlate(&mc_dev->dev, &iommu_spec); 149fa0656b4SNipun Gupta of_node_put(iommu_spec.np); 150fa0656b4SNipun Gupta return err; 151fa0656b4SNipun Gupta } 152fa0656b4SNipun Gupta 1532a0c5754SRobin Murphy const struct iommu_ops *of_iommu_configure(struct device *dev, 1542a0c5754SRobin Murphy struct device_node *master_np) 1552a0c5754SRobin Murphy { 156d87beb74SRobin Murphy const struct iommu_ops *ops = NULL; 1575c7e6bd7SJoerg Roedel struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 158da4b0275SRobin Murphy int err = NO_IOMMU; 1592a0c5754SRobin Murphy 1602a0c5754SRobin Murphy if (!master_np) 1617eba1d51SWill Deacon return NULL; 1622a0c5754SRobin Murphy 163d7b05582SRobin Murphy if (fwspec) { 164d7b05582SRobin Murphy if (fwspec->ops) 165d7b05582SRobin Murphy return fwspec->ops; 166d7b05582SRobin Murphy 167d7b05582SRobin Murphy /* In the deferred case, start again from scratch */ 168d7b05582SRobin Murphy iommu_fwspec_free(dev); 169d7b05582SRobin Murphy } 170d7b05582SRobin Murphy 171d87beb74SRobin Murphy /* 172d87beb74SRobin Murphy * We don't currently walk up the tree looking for a parent IOMMU. 173d87beb74SRobin Murphy * See the `Notes:' section of 174d87beb74SRobin Murphy * Documentation/devicetree/bindings/iommu/iommu.txt 175d87beb74SRobin Murphy */ 176d87beb74SRobin Murphy if (dev_is_pci(dev)) { 177d87beb74SRobin Murphy struct of_pci_iommu_alias_info info = { 178d87beb74SRobin Murphy .dev = dev, 179d87beb74SRobin Murphy .np = master_np, 180d87beb74SRobin Murphy }; 181d87beb74SRobin Murphy 182*6bf6c247SWill Deacon pci_request_acs(); 183d87beb74SRobin Murphy err = pci_for_each_dma_alias(to_pci_dev(dev), 184d87beb74SRobin Murphy of_pci_iommu_init, &info); 185fa0656b4SNipun Gupta } else if (dev_is_fsl_mc(dev)) { 186fa0656b4SNipun Gupta err = of_fsl_mc_iommu_init(to_fsl_mc_device(dev), master_np); 187d87beb74SRobin Murphy } else { 188d87beb74SRobin Murphy struct of_phandle_args iommu_spec; 189d87beb74SRobin Murphy int idx = 0; 190d87beb74SRobin Murphy 191d87beb74SRobin Murphy while (!of_parse_phandle_with_args(master_np, "iommus", 192d87beb74SRobin Murphy "#iommu-cells", 193d87beb74SRobin Murphy idx, &iommu_spec)) { 194da4b0275SRobin Murphy err = of_iommu_xlate(dev, &iommu_spec); 195d87beb74SRobin Murphy of_node_put(iommu_spec.np); 196d87beb74SRobin Murphy idx++; 197da4b0275SRobin Murphy if (err) 198d87beb74SRobin Murphy break; 199d87beb74SRobin Murphy } 200d87beb74SRobin Murphy } 201da4b0275SRobin Murphy 2025c7e6bd7SJoerg Roedel 203da4b0275SRobin Murphy /* 204da4b0275SRobin Murphy * Two success conditions can be represented by non-negative err here: 205da4b0275SRobin Murphy * >0 : there is no IOMMU, or one was unavailable for non-fatal reasons 206da4b0275SRobin Murphy * 0 : we found an IOMMU, and dev->fwspec is initialised appropriately 207da4b0275SRobin Murphy * <0 : any actual error 208da4b0275SRobin Murphy */ 2095c7e6bd7SJoerg Roedel if (!err) { 2105c7e6bd7SJoerg Roedel /* The fwspec pointer changed, read it again */ 2115c7e6bd7SJoerg Roedel fwspec = dev_iommu_fwspec_get(dev); 2125c7e6bd7SJoerg Roedel ops = fwspec->ops; 2135c7e6bd7SJoerg Roedel } 214d7b05582SRobin Murphy /* 215d7b05582SRobin Murphy * If we have reason to believe the IOMMU driver missed the initial 216641fb0efSJoerg Roedel * probe for dev, replay it to get things in order. 217d7b05582SRobin Murphy */ 218e8e683aeSRobin Murphy if (!err && dev->bus && !device_iommu_mapped(dev)) 219641fb0efSJoerg Roedel err = iommu_probe_device(dev); 2202a0c5754SRobin Murphy 221a37b19a3SSricharan R /* Ignore all other errors apart from EPROBE_DEFER */ 222da4b0275SRobin Murphy if (err == -EPROBE_DEFER) { 223da4b0275SRobin Murphy ops = ERR_PTR(err); 224da4b0275SRobin Murphy } else if (err < 0) { 225da4b0275SRobin Murphy dev_dbg(dev, "Adding to IOMMU failed: %d\n", err); 226a37b19a3SSricharan R ops = NULL; 227a37b19a3SSricharan R } 228a37b19a3SSricharan R 2297b07cbefSLaurent Pinchart return ops; 2307eba1d51SWill Deacon } 231