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> 11386dce27SWill Deacon #include <linux/module.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> 16386dce27SWill Deacon #include <linux/pci.h> 17a42a7a1fSRobin Murphy #include <linux/slab.h> 18fa0656b4SNipun Gupta #include <linux/fsl/mc.h> 194e0ee78fSHiroshi Doyu 20da4b0275SRobin Murphy #define NO_IOMMU 1 21da4b0275SRobin Murphy 224e0ee78fSHiroshi Doyu /** 234e0ee78fSHiroshi Doyu * of_get_dma_window - Parse *dma-window property and returns 0 if found. 244e0ee78fSHiroshi Doyu * 254e0ee78fSHiroshi Doyu * @dn: device node 264e0ee78fSHiroshi Doyu * @prefix: prefix for property name if any 274e0ee78fSHiroshi Doyu * @index: index to start to parse 284e0ee78fSHiroshi Doyu * @busno: Returns busno if supported. Otherwise pass NULL 294e0ee78fSHiroshi Doyu * @addr: Returns address that DMA starts 304e0ee78fSHiroshi Doyu * @size: Returns the range that DMA can handle 314e0ee78fSHiroshi Doyu * 324e0ee78fSHiroshi Doyu * This supports different formats flexibly. "prefix" can be 334e0ee78fSHiroshi Doyu * configured if any. "busno" and "index" are optionally 344e0ee78fSHiroshi Doyu * specified. Set 0(or NULL) if not used. 354e0ee78fSHiroshi Doyu */ 364e0ee78fSHiroshi Doyu int of_get_dma_window(struct device_node *dn, const char *prefix, int index, 374e0ee78fSHiroshi Doyu unsigned long *busno, dma_addr_t *addr, size_t *size) 384e0ee78fSHiroshi Doyu { 394e0ee78fSHiroshi Doyu const __be32 *dma_window, *end; 404e0ee78fSHiroshi Doyu int bytes, cur_index = 0; 414e0ee78fSHiroshi Doyu char propname[NAME_MAX], addrname[NAME_MAX], sizename[NAME_MAX]; 424e0ee78fSHiroshi Doyu 434e0ee78fSHiroshi Doyu if (!dn || !addr || !size) 444e0ee78fSHiroshi Doyu return -EINVAL; 454e0ee78fSHiroshi Doyu 464e0ee78fSHiroshi Doyu if (!prefix) 474e0ee78fSHiroshi Doyu prefix = ""; 484e0ee78fSHiroshi Doyu 494e0ee78fSHiroshi Doyu snprintf(propname, sizeof(propname), "%sdma-window", prefix); 504e0ee78fSHiroshi Doyu snprintf(addrname, sizeof(addrname), "%s#dma-address-cells", prefix); 514e0ee78fSHiroshi Doyu snprintf(sizename, sizeof(sizename), "%s#dma-size-cells", prefix); 524e0ee78fSHiroshi Doyu 534e0ee78fSHiroshi Doyu dma_window = of_get_property(dn, propname, &bytes); 544e0ee78fSHiroshi Doyu if (!dma_window) 554e0ee78fSHiroshi Doyu return -ENODEV; 564e0ee78fSHiroshi Doyu end = dma_window + bytes / sizeof(*dma_window); 574e0ee78fSHiroshi Doyu 584e0ee78fSHiroshi Doyu while (dma_window < end) { 594e0ee78fSHiroshi Doyu u32 cells; 604e0ee78fSHiroshi Doyu const void *prop; 614e0ee78fSHiroshi Doyu 624e0ee78fSHiroshi Doyu /* busno is one cell if supported */ 634e0ee78fSHiroshi Doyu if (busno) 644e0ee78fSHiroshi Doyu *busno = be32_to_cpup(dma_window++); 654e0ee78fSHiroshi Doyu 664e0ee78fSHiroshi Doyu prop = of_get_property(dn, addrname, NULL); 674e0ee78fSHiroshi Doyu if (!prop) 684e0ee78fSHiroshi Doyu prop = of_get_property(dn, "#address-cells", NULL); 694e0ee78fSHiroshi Doyu 704e0ee78fSHiroshi Doyu cells = prop ? be32_to_cpup(prop) : of_n_addr_cells(dn); 714e0ee78fSHiroshi Doyu if (!cells) 724e0ee78fSHiroshi Doyu return -EINVAL; 734e0ee78fSHiroshi Doyu *addr = of_read_number(dma_window, cells); 744e0ee78fSHiroshi Doyu dma_window += cells; 754e0ee78fSHiroshi Doyu 764e0ee78fSHiroshi Doyu prop = of_get_property(dn, sizename, NULL); 774e0ee78fSHiroshi Doyu cells = prop ? be32_to_cpup(prop) : of_n_size_cells(dn); 784e0ee78fSHiroshi Doyu if (!cells) 794e0ee78fSHiroshi Doyu return -EINVAL; 804e0ee78fSHiroshi Doyu *size = of_read_number(dma_window, cells); 814e0ee78fSHiroshi Doyu dma_window += cells; 824e0ee78fSHiroshi Doyu 834e0ee78fSHiroshi Doyu if (cur_index++ == index) 844e0ee78fSHiroshi Doyu break; 854e0ee78fSHiroshi Doyu } 864e0ee78fSHiroshi Doyu return 0; 874e0ee78fSHiroshi Doyu } 884e0ee78fSHiroshi Doyu EXPORT_SYMBOL_GPL(of_get_dma_window); 891cd076bfSWill Deacon 90da4b0275SRobin Murphy static int of_iommu_xlate(struct device *dev, 91da4b0275SRobin Murphy struct of_phandle_args *iommu_spec) 922a0c5754SRobin Murphy { 932a0c5754SRobin Murphy const struct iommu_ops *ops; 942a0c5754SRobin Murphy struct fwnode_handle *fwnode = &iommu_spec->np->fwnode; 95386dce27SWill Deacon int ret; 962a0c5754SRobin Murphy 972a0c5754SRobin Murphy ops = iommu_ops_from_fwnode(fwnode); 98d7b05582SRobin Murphy if ((ops && !ops->of_xlate) || 99ac6bbf0cSRob Herring !of_device_is_available(iommu_spec->np)) 100da4b0275SRobin Murphy return NO_IOMMU; 1012a0c5754SRobin Murphy 102386dce27SWill Deacon ret = iommu_fwspec_init(dev, &iommu_spec->np->fwnode, ops); 103386dce27SWill Deacon if (ret) 104386dce27SWill Deacon return ret; 105d7b05582SRobin Murphy /* 106d7b05582SRobin Murphy * The otherwise-empty fwspec handily serves to indicate the specific 107d7b05582SRobin Murphy * IOMMU device we're waiting for, which will be useful if we ever get 108d7b05582SRobin Murphy * a proper probe-ordering dependency mechanism in future. 109d7b05582SRobin Murphy */ 110d7b05582SRobin Murphy if (!ops) 11178f307beSRob Herring return driver_deferred_probe_check_state(dev); 1122a0c5754SRobin Murphy 113386dce27SWill Deacon if (!try_module_get(ops->owner)) 114386dce27SWill Deacon return -ENODEV; 115386dce27SWill Deacon 116386dce27SWill Deacon ret = ops->of_xlate(dev, iommu_spec); 117386dce27SWill Deacon module_put(ops->owner); 118386dce27SWill Deacon return ret; 1192a0c5754SRobin Murphy } 1202a0c5754SRobin Murphy 121d87beb74SRobin Murphy struct of_pci_iommu_alias_info { 122d87beb74SRobin Murphy struct device *dev; 123d87beb74SRobin Murphy struct device_node *np; 124d87beb74SRobin Murphy }; 125b996444cSRobin Murphy 126d87beb74SRobin Murphy static int of_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data) 127b996444cSRobin Murphy { 128d87beb74SRobin Murphy struct of_pci_iommu_alias_info *info = data; 129d87beb74SRobin Murphy struct of_phandle_args iommu_spec = { .args_count = 1 }; 1302a0c5754SRobin Murphy int err; 131b996444cSRobin Murphy 1322a6db719SNipun Gupta err = of_map_rid(info->np, alias, "iommu-map", "iommu-map-mask", 1332a6db719SNipun Gupta &iommu_spec.np, iommu_spec.args); 1342a0c5754SRobin Murphy if (err) 135da4b0275SRobin Murphy return err == -ENODEV ? NO_IOMMU : err; 136b996444cSRobin Murphy 137da4b0275SRobin Murphy err = of_iommu_xlate(info->dev, &iommu_spec); 138b996444cSRobin Murphy of_node_put(iommu_spec.np); 139da4b0275SRobin Murphy return err; 1402a0c5754SRobin Murphy } 1417eba1d51SWill Deacon 142fa0656b4SNipun Gupta static int of_fsl_mc_iommu_init(struct fsl_mc_device *mc_dev, 143fa0656b4SNipun Gupta struct device_node *master_np) 144fa0656b4SNipun Gupta { 145fa0656b4SNipun Gupta struct of_phandle_args iommu_spec = { .args_count = 1 }; 146fa0656b4SNipun Gupta int err; 147fa0656b4SNipun Gupta 148fa0656b4SNipun Gupta err = of_map_rid(master_np, mc_dev->icid, "iommu-map", 149fa0656b4SNipun Gupta "iommu-map-mask", &iommu_spec.np, 150fa0656b4SNipun Gupta iommu_spec.args); 151fa0656b4SNipun Gupta if (err) 152fa0656b4SNipun Gupta return err == -ENODEV ? NO_IOMMU : err; 153fa0656b4SNipun Gupta 154fa0656b4SNipun Gupta err = of_iommu_xlate(&mc_dev->dev, &iommu_spec); 155fa0656b4SNipun Gupta of_node_put(iommu_spec.np); 156fa0656b4SNipun Gupta return err; 157fa0656b4SNipun Gupta } 158fa0656b4SNipun Gupta 1592a0c5754SRobin Murphy const struct iommu_ops *of_iommu_configure(struct device *dev, 1602a0c5754SRobin Murphy struct device_node *master_np) 1612a0c5754SRobin Murphy { 162d87beb74SRobin Murphy const struct iommu_ops *ops = NULL; 1635c7e6bd7SJoerg Roedel struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); 164da4b0275SRobin Murphy int err = NO_IOMMU; 1652a0c5754SRobin Murphy 1662a0c5754SRobin Murphy if (!master_np) 1677eba1d51SWill Deacon return NULL; 1682a0c5754SRobin Murphy 169d7b05582SRobin Murphy if (fwspec) { 170d7b05582SRobin Murphy if (fwspec->ops) 171d7b05582SRobin Murphy return fwspec->ops; 172d7b05582SRobin Murphy 173d7b05582SRobin Murphy /* In the deferred case, start again from scratch */ 174d7b05582SRobin Murphy iommu_fwspec_free(dev); 175d7b05582SRobin Murphy } 176d7b05582SRobin Murphy 177d87beb74SRobin Murphy /* 178d87beb74SRobin Murphy * We don't currently walk up the tree looking for a parent IOMMU. 179d87beb74SRobin Murphy * See the `Notes:' section of 180d87beb74SRobin Murphy * Documentation/devicetree/bindings/iommu/iommu.txt 181d87beb74SRobin Murphy */ 182d87beb74SRobin Murphy if (dev_is_pci(dev)) { 183d87beb74SRobin Murphy struct of_pci_iommu_alias_info info = { 184d87beb74SRobin Murphy .dev = dev, 185d87beb74SRobin Murphy .np = master_np, 186d87beb74SRobin Murphy }; 187d87beb74SRobin Murphy 1886bf6c247SWill Deacon pci_request_acs(); 189d87beb74SRobin Murphy err = pci_for_each_dma_alias(to_pci_dev(dev), 190d87beb74SRobin Murphy of_pci_iommu_init, &info); 191fa0656b4SNipun Gupta } else if (dev_is_fsl_mc(dev)) { 192fa0656b4SNipun Gupta err = of_fsl_mc_iommu_init(to_fsl_mc_device(dev), master_np); 193d87beb74SRobin Murphy } else { 194d87beb74SRobin Murphy struct of_phandle_args iommu_spec; 195d87beb74SRobin Murphy int idx = 0; 196d87beb74SRobin Murphy 197d87beb74SRobin Murphy while (!of_parse_phandle_with_args(master_np, "iommus", 198d87beb74SRobin Murphy "#iommu-cells", 199d87beb74SRobin Murphy idx, &iommu_spec)) { 200da4b0275SRobin Murphy err = of_iommu_xlate(dev, &iommu_spec); 201d87beb74SRobin Murphy of_node_put(iommu_spec.np); 202d87beb74SRobin Murphy idx++; 203da4b0275SRobin Murphy if (err) 204d87beb74SRobin Murphy break; 205d87beb74SRobin Murphy } 206da4b0275SRobin Murphy 207*89535821SJean-Philippe Brucker fwspec = dev_iommu_fwspec_get(dev); 208*89535821SJean-Philippe Brucker if (!err && fwspec) 209*89535821SJean-Philippe Brucker of_property_read_u32(master_np, "pasid-num-bits", 210*89535821SJean-Philippe Brucker &fwspec->num_pasid_bits); 211*89535821SJean-Philippe Brucker } 2125c7e6bd7SJoerg Roedel 213da4b0275SRobin Murphy /* 214da4b0275SRobin Murphy * Two success conditions can be represented by non-negative err here: 215da4b0275SRobin Murphy * >0 : there is no IOMMU, or one was unavailable for non-fatal reasons 216da4b0275SRobin Murphy * 0 : we found an IOMMU, and dev->fwspec is initialised appropriately 217da4b0275SRobin Murphy * <0 : any actual error 218da4b0275SRobin Murphy */ 2195c7e6bd7SJoerg Roedel if (!err) { 2205c7e6bd7SJoerg Roedel /* The fwspec pointer changed, read it again */ 2215c7e6bd7SJoerg Roedel fwspec = dev_iommu_fwspec_get(dev); 2225c7e6bd7SJoerg Roedel ops = fwspec->ops; 2235c7e6bd7SJoerg Roedel } 224d7b05582SRobin Murphy /* 225d7b05582SRobin Murphy * If we have reason to believe the IOMMU driver missed the initial 226641fb0efSJoerg Roedel * probe for dev, replay it to get things in order. 227d7b05582SRobin Murphy */ 228e8e683aeSRobin Murphy if (!err && dev->bus && !device_iommu_mapped(dev)) 229641fb0efSJoerg Roedel err = iommu_probe_device(dev); 2302a0c5754SRobin Murphy 231a37b19a3SSricharan R /* Ignore all other errors apart from EPROBE_DEFER */ 232da4b0275SRobin Murphy if (err == -EPROBE_DEFER) { 233da4b0275SRobin Murphy ops = ERR_PTR(err); 234da4b0275SRobin Murphy } else if (err < 0) { 235da4b0275SRobin Murphy dev_dbg(dev, "Adding to IOMMU failed: %d\n", err); 236a37b19a3SSricharan R ops = NULL; 237a37b19a3SSricharan R } 238a37b19a3SSricharan R 2397b07cbefSLaurent Pinchart return ops; 2407eba1d51SWill Deacon } 241