Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branches 'pci/iommu' and 'pci/resource' into next

* pci/iommu:
of: Calculate device DMA masks based on DT dma-range size
arm: dma-mapping: limit IOMMU mapping size
PCI: Update DMA configuration from DT
of/pci: Add of_pci_dma_configure() to update DMA configuration
PCI: Add helper functions pci_get[put]_host_bridge_device()
of: Fix size when dma-range is not used
of: Move of_dma_configure() to device.c to help re-use
of: iommu: Add ptr to OF node arg to of_iommu_configure()

* pci/resource:
PCI: Fail pci_ioremap_bar() on unassigned resources
PCI: Show driver, BAR#, and resource on pci_ioremap_bar() failure
PCI: Mark invalid BARs as unassigned
PNP: Don't check for overlaps with unassigned PCI BARs

+164 -67
+7
arch/arm/mm/dma-mapping.c
··· 2027 2027 if (!iommu) 2028 2028 return false; 2029 2029 2030 + /* 2031 + * currently arm_iommu_create_mapping() takes a max of size_t 2032 + * for size param. So check this limit for now. 2033 + */ 2034 + if (size > SIZE_MAX) 2035 + return false; 2036 + 2030 2037 mapping = arm_iommu_create_mapping(dev->bus, dma_base, size); 2031 2038 if (IS_ERR(mapping)) { 2032 2039 pr_warn("Failed to create %llu-byte IOMMU mapping for device %s\n",
+8 -2
drivers/iommu/of_iommu.c
··· 133 133 return ops; 134 134 } 135 135 136 - struct iommu_ops *of_iommu_configure(struct device *dev) 136 + struct iommu_ops *of_iommu_configure(struct device *dev, 137 + struct device_node *master_np) 137 138 { 138 139 struct of_phandle_args iommu_spec; 139 140 struct device_node *np; 140 141 struct iommu_ops *ops = NULL; 141 142 int idx = 0; 142 143 144 + if (dev_is_pci(dev)) { 145 + dev_err(dev, "IOMMU is currently not supported for PCI\n"); 146 + return NULL; 147 + } 148 + 143 149 /* 144 150 * We don't currently walk up the tree looking for a parent IOMMU. 145 151 * See the `Notes:' section of 146 152 * Documentation/devicetree/bindings/iommu/iommu.txt 147 153 */ 148 - while (!of_parse_phandle_with_args(dev->of_node, "iommus", 154 + while (!of_parse_phandle_with_args(master_np, "iommus", 149 155 "#iommu-cells", idx, 150 156 &iommu_spec)) { 151 157 np = iommu_spec.np;
+84
drivers/of/device.c
··· 2 2 #include <linux/kernel.h> 3 3 #include <linux/of.h> 4 4 #include <linux/of_device.h> 5 + #include <linux/of_address.h> 6 + #include <linux/of_iommu.h> 7 + #include <linux/dma-mapping.h> 5 8 #include <linux/init.h> 6 9 #include <linux/module.h> 7 10 #include <linux/mod_devicetable.h> ··· 68 65 69 66 return device_add(&ofdev->dev); 70 67 } 68 + 69 + /** 70 + * of_dma_configure - Setup DMA configuration 71 + * @dev: Device to apply DMA configuration 72 + * @np: Pointer to OF node having DMA configuration 73 + * 74 + * Try to get devices's DMA configuration from DT and update it 75 + * accordingly. 76 + * 77 + * If platform code needs to use its own special DMA configuration, it 78 + * can use a platform bus notifier and handle BUS_NOTIFY_ADD_DEVICE events 79 + * to fix up DMA configuration. 80 + */ 81 + void of_dma_configure(struct device *dev, struct device_node *np) 82 + { 83 + u64 dma_addr, paddr, size; 84 + int ret; 85 + bool coherent; 86 + unsigned long offset; 87 + struct iommu_ops *iommu; 88 + 89 + /* 90 + * Set default coherent_dma_mask to 32 bit. Drivers are expected to 91 + * setup the correct supported mask. 92 + */ 93 + if (!dev->coherent_dma_mask) 94 + dev->coherent_dma_mask = DMA_BIT_MASK(32); 95 + 96 + /* 97 + * Set it to coherent_dma_mask by default if the architecture 98 + * code has not set it. 99 + */ 100 + if (!dev->dma_mask) 101 + dev->dma_mask = &dev->coherent_dma_mask; 102 + 103 + ret = of_dma_get_range(np, &dma_addr, &paddr, &size); 104 + if (ret < 0) { 105 + dma_addr = offset = 0; 106 + size = dev->coherent_dma_mask + 1; 107 + } else { 108 + offset = PFN_DOWN(paddr - dma_addr); 109 + 110 + /* 111 + * Add a work around to treat the size as mask + 1 in case 112 + * it is defined in DT as a mask. 113 + */ 114 + if (size & 1) { 115 + dev_warn(dev, "Invalid size 0x%llx for dma-range\n", 116 + size); 117 + size = size + 1; 118 + } 119 + 120 + if (!size) { 121 + dev_err(dev, "Adjusted size 0x%llx invalid\n", size); 122 + return; 123 + } 124 + dev_dbg(dev, "dma_pfn_offset(%#08lx)\n", offset); 125 + } 126 + 127 + dev->dma_pfn_offset = offset; 128 + 129 + /* 130 + * Limit coherent and dma mask based on size and default mask 131 + * set by the driver. 132 + */ 133 + dev->coherent_dma_mask = min(dev->coherent_dma_mask, 134 + DMA_BIT_MASK(ilog2(dma_addr + size))); 135 + *dev->dma_mask = min((*dev->dma_mask), 136 + DMA_BIT_MASK(ilog2(dma_addr + size))); 137 + 138 + coherent = of_dma_is_coherent(np); 139 + dev_dbg(dev, "device is%sdma coherent\n", 140 + coherent ? " " : " not "); 141 + 142 + iommu = of_iommu_configure(dev, np); 143 + dev_dbg(dev, "device is%sbehind an iommu\n", 144 + iommu ? " " : " not "); 145 + 146 + arch_setup_dma_ops(dev, dma_addr, size, iommu, coherent); 147 + } 148 + EXPORT_SYMBOL_GPL(of_dma_configure); 71 149 72 150 int of_device_register(struct platform_device *pdev) 73 151 {
+21
drivers/of/of_pci.c
··· 2 2 #include <linux/export.h> 3 3 #include <linux/of.h> 4 4 #include <linux/of_address.h> 5 + #include <linux/of_device.h> 5 6 #include <linux/of_pci.h> 6 7 #include <linux/slab.h> 7 8 ··· 116 115 return domain; 117 116 } 118 117 EXPORT_SYMBOL_GPL(of_get_pci_domain_nr); 118 + 119 + /** 120 + * of_pci_dma_configure - Setup DMA configuration 121 + * @dev: ptr to pci_dev struct of the PCI device 122 + * 123 + * Function to update PCI devices's DMA configuration using the same 124 + * info from the OF node of host bridge's parent (if any). 125 + */ 126 + void of_pci_dma_configure(struct pci_dev *pci_dev) 127 + { 128 + struct device *dev = &pci_dev->dev; 129 + struct device *bridge = pci_get_host_bridge_device(pci_dev); 130 + 131 + if (!bridge->parent) 132 + return; 133 + 134 + of_dma_configure(dev, bridge->parent->of_node); 135 + pci_put_host_bridge_device(bridge); 136 + } 137 + EXPORT_SYMBOL_GPL(of_pci_dma_configure); 119 138 120 139 #if defined(CONFIG_OF_ADDRESS) 121 140 /**
+2 -56
drivers/of/platform.c
··· 19 19 #include <linux/slab.h> 20 20 #include <linux/of_address.h> 21 21 #include <linux/of_device.h> 22 - #include <linux/of_iommu.h> 23 22 #include <linux/of_irq.h> 24 23 #include <linux/of_platform.h> 25 24 #include <linux/platform_device.h> ··· 149 150 } 150 151 EXPORT_SYMBOL(of_device_alloc); 151 152 152 - /** 153 - * of_dma_configure - Setup DMA configuration 154 - * @dev: Device to apply DMA configuration 155 - * 156 - * Try to get devices's DMA configuration from DT and update it 157 - * accordingly. 158 - * 159 - * In case if platform code need to use own special DMA configuration,it 160 - * can use Platform bus notifier and handle BUS_NOTIFY_ADD_DEVICE event 161 - * to fix up DMA configuration. 162 - */ 163 - static void of_dma_configure(struct device *dev) 164 - { 165 - u64 dma_addr, paddr, size; 166 - int ret; 167 - bool coherent; 168 - unsigned long offset; 169 - struct iommu_ops *iommu; 170 - 171 - /* 172 - * Set default dma-mask to 32 bit. Drivers are expected to setup 173 - * the correct supported dma_mask. 174 - */ 175 - dev->coherent_dma_mask = DMA_BIT_MASK(32); 176 - 177 - /* 178 - * Set it to coherent_dma_mask by default if the architecture 179 - * code has not set it. 180 - */ 181 - if (!dev->dma_mask) 182 - dev->dma_mask = &dev->coherent_dma_mask; 183 - 184 - ret = of_dma_get_range(dev->of_node, &dma_addr, &paddr, &size); 185 - if (ret < 0) { 186 - dma_addr = offset = 0; 187 - size = dev->coherent_dma_mask; 188 - } else { 189 - offset = PFN_DOWN(paddr - dma_addr); 190 - dev_dbg(dev, "dma_pfn_offset(%#08lx)\n", offset); 191 - } 192 - dev->dma_pfn_offset = offset; 193 - 194 - coherent = of_dma_is_coherent(dev->of_node); 195 - dev_dbg(dev, "device is%sdma coherent\n", 196 - coherent ? " " : " not "); 197 - 198 - iommu = of_iommu_configure(dev); 199 - dev_dbg(dev, "device is%sbehind an iommu\n", 200 - iommu ? " " : " not "); 201 - 202 - arch_setup_dma_ops(dev, dma_addr, size, iommu, coherent); 203 - } 204 - 205 153 static void of_dma_deconfigure(struct device *dev) 206 154 { 207 155 arch_teardown_dma_ops(dev); ··· 182 236 183 237 dev->dev.bus = &platform_bus_type; 184 238 dev->dev.platform_data = platform_data; 185 - of_dma_configure(&dev->dev); 239 + of_dma_configure(&dev->dev, dev->dev.of_node); 186 240 187 241 if (of_device_add(dev) != 0) { 188 242 of_dma_deconfigure(&dev->dev); ··· 245 299 dev_set_name(&dev->dev, "%s", bus_id); 246 300 else 247 301 of_device_make_bus_id(&dev->dev); 248 - of_dma_configure(&dev->dev); 302 + of_dma_configure(&dev->dev, dev->dev.of_node); 249 303 250 304 /* Allow the HW Peripheral ID to be overridden */ 251 305 prop = of_get_property(node, "arm,primecell-periphid", NULL);
+14
drivers/pci/host-bridge.c
··· 23 23 return to_pci_host_bridge(root_bus->bridge); 24 24 } 25 25 26 + struct device *pci_get_host_bridge_device(struct pci_dev *dev) 27 + { 28 + struct pci_bus *root_bus = find_pci_root_bus(dev->bus); 29 + struct device *bridge = root_bus->bridge; 30 + 31 + kobject_get(&bridge->kobj); 32 + return bridge; 33 + } 34 + 35 + void pci_put_host_bridge_device(struct device *dev) 36 + { 37 + kobject_put(&dev->kobj); 38 + } 39 + 26 40 void pci_set_host_bridge_release(struct pci_host_bridge *bridge, 27 41 void (*release_fn)(struct pci_host_bridge *), 28 42 void *release_data)
+5 -4
drivers/pci/pci.c
··· 126 126 #ifdef CONFIG_HAS_IOMEM 127 127 void __iomem *pci_ioremap_bar(struct pci_dev *pdev, int bar) 128 128 { 129 + struct resource *res = &pdev->resource[bar]; 130 + 129 131 /* 130 132 * Make sure the BAR is actually a memory resource, not an IO resource 131 133 */ 132 - if (!(pci_resource_flags(pdev, bar) & IORESOURCE_MEM)) { 133 - WARN_ON(1); 134 + if (res->flags & IORESOURCE_UNSET || !(res->flags & IORESOURCE_MEM)) { 135 + dev_warn(&pdev->dev, "can't ioremap BAR %d: %pR\n", bar, res); 134 136 return NULL; 135 137 } 136 - return ioremap_nocache(pci_resource_start(pdev, bar), 137 - pci_resource_len(pdev, bar)); 138 + return ioremap_nocache(res->start, resource_size(res)); 138 139 } 139 140 EXPORT_SYMBOL_GPL(pci_ioremap_bar); 140 141 #endif
+2
drivers/pci/probe.c
··· 6 6 #include <linux/delay.h> 7 7 #include <linux/init.h> 8 8 #include <linux/pci.h> 9 + #include <linux/of_pci.h> 9 10 #include <linux/pci_hotplug.h> 10 11 #include <linux/slab.h> 11 12 #include <linux/module.h> ··· 1521 1520 dev->dev.dma_mask = &dev->dma_mask; 1522 1521 dev->dev.dma_parms = &dev->dma_parms; 1523 1522 dev->dev.coherent_dma_mask = 0xffffffffull; 1523 + of_pci_dma_configure(dev); 1524 1524 1525 1525 pci_set_dma_max_seg_size(dev, 65536); 1526 1526 pci_set_dma_seg_boundary(dev, 0xffffffff);
+2
drivers/pci/setup-res.c
··· 120 120 if (!root) { 121 121 dev_info(&dev->dev, "can't claim BAR %d %pR: no compatible bridge window\n", 122 122 resource, res); 123 + res->flags |= IORESOURCE_UNSET; 123 124 return -EINVAL; 124 125 } 125 126 ··· 128 127 if (conflict) { 129 128 dev_info(&dev->dev, "can't claim BAR %d %pR: address conflict with %s %pR\n", 130 129 resource, res, conflict->name, conflict); 130 + res->flags |= IORESOURCE_UNSET; 131 131 return -EBUSY; 132 132 } 133 133
+6 -3
drivers/pnp/quirks.c
··· 246 246 */ 247 247 for_each_pci_dev(pdev) { 248 248 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { 249 - unsigned long type; 249 + unsigned long flags, type; 250 250 251 - type = pci_resource_flags(pdev, i) & 252 - (IORESOURCE_IO | IORESOURCE_MEM); 251 + flags = pci_resource_flags(pdev, i); 252 + type = flags & (IORESOURCE_IO | IORESOURCE_MEM); 253 253 if (!type || pci_resource_len(pdev, i) == 0) 254 + continue; 255 + 256 + if (flags & IORESOURCE_UNSET) 254 257 continue; 255 258 256 259 pci_start = pci_resource_start(pdev, i);
+3
include/linux/of_device.h
··· 53 53 return of_node_get(cpu_dev->of_node); 54 54 } 55 55 56 + void of_dma_configure(struct device *dev, struct device_node *np); 56 57 #else /* CONFIG_OF */ 57 58 58 59 static inline int of_driver_match_device(struct device *dev, ··· 91 90 { 92 91 return NULL; 93 92 } 93 + static inline void of_dma_configure(struct device *dev, struct device_node *np) 94 + {} 94 95 #endif /* CONFIG_OF */ 95 96 96 97 #endif /* _LINUX_OF_DEVICE_H */
+4 -2
include/linux/of_iommu.h
··· 12 12 size_t *size); 13 13 14 14 extern void of_iommu_init(void); 15 - extern struct iommu_ops *of_iommu_configure(struct device *dev); 15 + extern struct iommu_ops *of_iommu_configure(struct device *dev, 16 + struct device_node *master_np); 16 17 17 18 #else 18 19 ··· 25 24 } 26 25 27 26 static inline void of_iommu_init(void) { } 28 - static inline struct iommu_ops *of_iommu_configure(struct device *dev) 27 + static inline struct iommu_ops *of_iommu_configure(struct device *dev, 28 + struct device_node *master_np) 29 29 { 30 30 return NULL; 31 31 }
+3
include/linux/of_pci.h
··· 16 16 int of_irq_parse_and_map_pci(const struct pci_dev *dev, u8 slot, u8 pin); 17 17 int of_pci_parse_bus_range(struct device_node *node, struct resource *res); 18 18 int of_get_pci_domain_nr(struct device_node *node); 19 + void of_pci_dma_configure(struct pci_dev *pci_dev); 19 20 #else 20 21 static inline int of_irq_parse_pci(const struct pci_dev *pdev, struct of_phandle_args *out_irq) 21 22 { ··· 51 50 { 52 51 return -1; 53 52 } 53 + 54 + static inline void of_pci_dma_configure(struct pci_dev *pci_dev) { } 54 55 #endif 55 56 56 57 #if defined(CONFIG_OF_ADDRESS)
+3
include/linux/pci.h
··· 510 510 return dev->bus->self; 511 511 } 512 512 513 + struct device *pci_get_host_bridge_device(struct pci_dev *dev); 514 + void pci_put_host_bridge_device(struct device *dev); 515 + 513 516 #ifdef CONFIG_PCI_MSI 514 517 static inline bool pci_dev_msi_enabled(struct pci_dev *pci_dev) 515 518 {