Sebastian Andrzej Siewior | 04bea68 | 2011-01-24 09:58:55 +0530 | [diff] [blame] | 1 | #include <linux/kernel.h> |
Paul Gortmaker | 9775913 | 2011-05-27 17:06:52 -0400 | [diff] [blame] | 2 | #include <linux/export.h> |
Grant Likely | a642285 | 2011-07-23 23:52:48 -0600 | [diff] [blame] | 3 | #include <linux/of.h> |
Liviu Dudau | cbe4097 | 2014-09-29 15:29:28 +0100 | [diff] [blame] | 4 | #include <linux/of_address.h> |
Murali Karicheri | c49b8fc | 2015-03-03 12:52:12 -0500 | [diff] [blame] | 5 | #include <linux/of_device.h> |
Sebastian Andrzej Siewior | 04bea68 | 2011-01-24 09:58:55 +0530 | [diff] [blame] | 6 | #include <linux/of_pci.h> |
Liviu Dudau | cbe4097 | 2014-09-29 15:29:28 +0100 | [diff] [blame] | 7 | #include <linux/slab.h> |
Marc Zyngier | f81c11a | 2015-09-04 17:50:08 +0100 | [diff] [blame^] | 8 | #include <asm-generic/pci-bridge.h> |
Sebastian Andrzej Siewior | 04bea68 | 2011-01-24 09:58:55 +0530 | [diff] [blame] | 9 | |
Benjamin Herrenschmidt | 98d9f30c8 | 2011-04-11 11:37:07 +1000 | [diff] [blame] | 10 | static inline int __of_pci_pci_compare(struct device_node *node, |
Thierry Reding | 45ab970 | 2013-05-16 17:55:18 +0200 | [diff] [blame] | 11 | unsigned int data) |
Sebastian Andrzej Siewior | 04bea68 | 2011-01-24 09:58:55 +0530 | [diff] [blame] | 12 | { |
Thierry Reding | 45ab970 | 2013-05-16 17:55:18 +0200 | [diff] [blame] | 13 | int devfn; |
Sebastian Andrzej Siewior | 04bea68 | 2011-01-24 09:58:55 +0530 | [diff] [blame] | 14 | |
Thierry Reding | 45ab970 | 2013-05-16 17:55:18 +0200 | [diff] [blame] | 15 | devfn = of_pci_get_devfn(node); |
| 16 | if (devfn < 0) |
Benjamin Herrenschmidt | 98d9f30c8 | 2011-04-11 11:37:07 +1000 | [diff] [blame] | 17 | return 0; |
Thierry Reding | 45ab970 | 2013-05-16 17:55:18 +0200 | [diff] [blame] | 18 | |
| 19 | return devfn == data; |
Sebastian Andrzej Siewior | 04bea68 | 2011-01-24 09:58:55 +0530 | [diff] [blame] | 20 | } |
Benjamin Herrenschmidt | 98d9f30c8 | 2011-04-11 11:37:07 +1000 | [diff] [blame] | 21 | |
| 22 | struct device_node *of_pci_find_child_device(struct device_node *parent, |
| 23 | unsigned int devfn) |
| 24 | { |
| 25 | struct device_node *node, *node2; |
| 26 | |
| 27 | for_each_child_of_node(parent, node) { |
| 28 | if (__of_pci_pci_compare(node, devfn)) |
| 29 | return node; |
| 30 | /* |
| 31 | * Some OFs create a parent node "multifunc-device" as |
| 32 | * a fake root for all functions of a multi-function |
| 33 | * device we go down them as well. |
| 34 | */ |
| 35 | if (!strcmp(node->name, "multifunc-device")) { |
| 36 | for_each_child_of_node(node, node2) { |
| 37 | if (__of_pci_pci_compare(node2, devfn)) { |
| 38 | of_node_put(node); |
| 39 | return node2; |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | return NULL; |
| 45 | } |
| 46 | EXPORT_SYMBOL_GPL(of_pci_find_child_device); |
Thierry Reding | 45ab970 | 2013-05-16 17:55:18 +0200 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * of_pci_get_devfn() - Get device and function numbers for a device node |
| 50 | * @np: device node |
| 51 | * |
| 52 | * Parses a standard 5-cell PCI resource and returns an 8-bit value that can |
| 53 | * be passed to the PCI_SLOT() and PCI_FUNC() macros to extract the device |
| 54 | * and function numbers respectively. On error a negative error code is |
| 55 | * returned. |
| 56 | */ |
| 57 | int of_pci_get_devfn(struct device_node *np) |
| 58 | { |
| 59 | unsigned int size; |
| 60 | const __be32 *reg; |
| 61 | |
| 62 | reg = of_get_property(np, "reg", &size); |
| 63 | |
| 64 | if (!reg || size < 5 * sizeof(__be32)) |
| 65 | return -EINVAL; |
| 66 | |
| 67 | return (be32_to_cpup(reg) >> 8) & 0xff; |
| 68 | } |
| 69 | EXPORT_SYMBOL_GPL(of_pci_get_devfn); |
Thierry Reding | 4e23d3f | 2013-05-16 17:55:19 +0200 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * of_pci_parse_bus_range() - parse the bus-range property of a PCI device |
| 73 | * @node: device node |
| 74 | * @res: address to a struct resource to return the bus-range |
| 75 | * |
| 76 | * Returns 0 on success or a negative error-code on failure. |
| 77 | */ |
| 78 | int of_pci_parse_bus_range(struct device_node *node, struct resource *res) |
| 79 | { |
| 80 | const __be32 *values; |
| 81 | int len; |
| 82 | |
| 83 | values = of_get_property(node, "bus-range", &len); |
| 84 | if (!values || len < sizeof(*values) * 2) |
| 85 | return -EINVAL; |
| 86 | |
| 87 | res->name = node->name; |
| 88 | res->start = be32_to_cpup(values++); |
| 89 | res->end = be32_to_cpup(values); |
| 90 | res->flags = IORESOURCE_BUS; |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | EXPORT_SYMBOL_GPL(of_pci_parse_bus_range); |
Thomas Petazzoni | 0d5a6db | 2013-08-09 22:27:09 +0200 | [diff] [blame] | 95 | |
Liviu Dudau | 41e5c0f | 2014-09-29 15:29:27 +0100 | [diff] [blame] | 96 | /** |
| 97 | * This function will try to obtain the host bridge domain number by |
| 98 | * finding a property called "linux,pci-domain" of the given device node. |
| 99 | * |
| 100 | * @node: device tree node with the domain information |
| 101 | * |
| 102 | * Returns the associated domain number from DT in the range [0-0xffff], or |
| 103 | * a negative value if the required property is not found. |
| 104 | */ |
| 105 | int of_get_pci_domain_nr(struct device_node *node) |
| 106 | { |
| 107 | const __be32 *value; |
| 108 | int len; |
| 109 | u16 domain; |
| 110 | |
| 111 | value = of_get_property(node, "linux,pci-domain", &len); |
| 112 | if (!value || len < sizeof(*value)) |
| 113 | return -EINVAL; |
| 114 | |
| 115 | domain = (u16)be32_to_cpup(value); |
| 116 | |
| 117 | return domain; |
| 118 | } |
| 119 | EXPORT_SYMBOL_GPL(of_get_pci_domain_nr); |
| 120 | |
Murali Karicheri | c49b8fc | 2015-03-03 12:52:12 -0500 | [diff] [blame] | 121 | /** |
Marc Zyngier | f81c11a | 2015-09-04 17:50:08 +0100 | [diff] [blame^] | 122 | * of_pci_check_probe_only - Setup probe only mode if linux,pci-probe-only |
| 123 | * is present and valid |
| 124 | */ |
| 125 | void of_pci_check_probe_only(void) |
| 126 | { |
| 127 | u32 val; |
| 128 | int ret; |
| 129 | |
| 130 | ret = of_property_read_u32(of_chosen, "linux,pci-probe-only", &val); |
| 131 | if (ret) { |
| 132 | if (ret == -ENODATA || ret == -EOVERFLOW) |
| 133 | pr_warn("linux,pci-probe-only without valid value, ignoring\n"); |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | if (val) |
| 138 | pci_add_flags(PCI_PROBE_ONLY); |
| 139 | else |
| 140 | pci_clear_flags(PCI_PROBE_ONLY); |
| 141 | |
| 142 | pr_info("PCI: PROBE_ONLY %sabled\n", val ? "en" : "dis"); |
| 143 | } |
| 144 | EXPORT_SYMBOL_GPL(of_pci_check_probe_only); |
| 145 | |
| 146 | /** |
Murali Karicheri | c49b8fc | 2015-03-03 12:52:12 -0500 | [diff] [blame] | 147 | * of_pci_dma_configure - Setup DMA configuration |
| 148 | * @dev: ptr to pci_dev struct of the PCI device |
| 149 | * |
| 150 | * Function to update PCI devices's DMA configuration using the same |
| 151 | * info from the OF node of host bridge's parent (if any). |
| 152 | */ |
| 153 | void of_pci_dma_configure(struct pci_dev *pci_dev) |
| 154 | { |
| 155 | struct device *dev = &pci_dev->dev; |
| 156 | struct device *bridge = pci_get_host_bridge_device(pci_dev); |
| 157 | |
| 158 | if (!bridge->parent) |
| 159 | return; |
| 160 | |
| 161 | of_dma_configure(dev, bridge->parent->of_node); |
| 162 | pci_put_host_bridge_device(bridge); |
| 163 | } |
| 164 | EXPORT_SYMBOL_GPL(of_pci_dma_configure); |
| 165 | |
Liviu Dudau | cbe4097 | 2014-09-29 15:29:28 +0100 | [diff] [blame] | 166 | #if defined(CONFIG_OF_ADDRESS) |
| 167 | /** |
| 168 | * of_pci_get_host_bridge_resources - Parse PCI host bridge resources from DT |
| 169 | * @dev: device node of the host bridge having the range property |
| 170 | * @busno: bus number associated with the bridge root bus |
| 171 | * @bus_max: maximum number of buses for this bridge |
| 172 | * @resources: list where the range of resources will be added after DT parsing |
| 173 | * @io_base: pointer to a variable that will contain on return the physical |
| 174 | * address for the start of the I/O range. Can be NULL if the caller doesn't |
| 175 | * expect IO ranges to be present in the device tree. |
| 176 | * |
| 177 | * It is the caller's job to free the @resources list. |
| 178 | * |
| 179 | * This function will parse the "ranges" property of a PCI host bridge device |
| 180 | * node and setup the resource mapping based on its content. It is expected |
| 181 | * that the property conforms with the Power ePAPR document. |
| 182 | * |
| 183 | * It returns zero if the range parsing has been successful or a standard error |
| 184 | * value if it failed. |
| 185 | */ |
| 186 | int of_pci_get_host_bridge_resources(struct device_node *dev, |
| 187 | unsigned char busno, unsigned char bus_max, |
| 188 | struct list_head *resources, resource_size_t *io_base) |
| 189 | { |
Rafael J. Wysocki | 5c493df | 2015-02-09 15:40:30 +0100 | [diff] [blame] | 190 | struct resource_entry *window; |
Liviu Dudau | cbe4097 | 2014-09-29 15:29:28 +0100 | [diff] [blame] | 191 | struct resource *res; |
| 192 | struct resource *bus_range; |
| 193 | struct of_pci_range range; |
| 194 | struct of_pci_range_parser parser; |
| 195 | char range_type[4]; |
| 196 | int err; |
| 197 | |
| 198 | if (io_base) |
| 199 | *io_base = (resource_size_t)OF_BAD_ADDR; |
| 200 | |
| 201 | bus_range = kzalloc(sizeof(*bus_range), GFP_KERNEL); |
| 202 | if (!bus_range) |
| 203 | return -ENOMEM; |
| 204 | |
| 205 | pr_info("PCI host bridge %s ranges:\n", dev->full_name); |
| 206 | |
| 207 | err = of_pci_parse_bus_range(dev, bus_range); |
| 208 | if (err) { |
| 209 | bus_range->start = busno; |
| 210 | bus_range->end = bus_max; |
| 211 | bus_range->flags = IORESOURCE_BUS; |
| 212 | pr_info(" No bus range found for %s, using %pR\n", |
| 213 | dev->full_name, bus_range); |
| 214 | } else { |
| 215 | if (bus_range->end > bus_range->start + bus_max) |
| 216 | bus_range->end = bus_range->start + bus_max; |
| 217 | } |
| 218 | pci_add_resource(resources, bus_range); |
| 219 | |
| 220 | /* Check for ranges property */ |
| 221 | err = of_pci_range_parser_init(&parser, dev); |
| 222 | if (err) |
| 223 | goto parse_failed; |
| 224 | |
| 225 | pr_debug("Parsing ranges property...\n"); |
| 226 | for_each_of_pci_range(&parser, &range) { |
| 227 | /* Read next ranges element */ |
| 228 | if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_IO) |
| 229 | snprintf(range_type, 4, " IO"); |
| 230 | else if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_MEM) |
| 231 | snprintf(range_type, 4, "MEM"); |
| 232 | else |
| 233 | snprintf(range_type, 4, "err"); |
| 234 | pr_info(" %s %#010llx..%#010llx -> %#010llx\n", range_type, |
| 235 | range.cpu_addr, range.cpu_addr + range.size - 1, |
| 236 | range.pci_addr); |
| 237 | |
| 238 | /* |
| 239 | * If we failed translation or got a zero-sized region |
| 240 | * then skip this range |
| 241 | */ |
| 242 | if (range.cpu_addr == OF_BAD_ADDR || range.size == 0) |
| 243 | continue; |
| 244 | |
| 245 | res = kzalloc(sizeof(struct resource), GFP_KERNEL); |
| 246 | if (!res) { |
| 247 | err = -ENOMEM; |
| 248 | goto parse_failed; |
| 249 | } |
| 250 | |
| 251 | err = of_pci_range_to_resource(&range, dev, res); |
| 252 | if (err) |
| 253 | goto conversion_failed; |
| 254 | |
| 255 | if (resource_type(res) == IORESOURCE_IO) { |
| 256 | if (!io_base) { |
| 257 | pr_err("I/O range found for %s. Please provide an io_base pointer to save CPU base address\n", |
| 258 | dev->full_name); |
| 259 | err = -EINVAL; |
| 260 | goto conversion_failed; |
| 261 | } |
| 262 | if (*io_base != (resource_size_t)OF_BAD_ADDR) |
| 263 | pr_warn("More than one I/O resource converted for %s. CPU base address for old range lost!\n", |
| 264 | dev->full_name); |
| 265 | *io_base = range.cpu_addr; |
| 266 | } |
| 267 | |
| 268 | pci_add_resource_offset(resources, res, res->start - range.pci_addr); |
| 269 | } |
| 270 | |
| 271 | return 0; |
| 272 | |
| 273 | conversion_failed: |
| 274 | kfree(res); |
| 275 | parse_failed: |
Rafael J. Wysocki | 5c493df | 2015-02-09 15:40:30 +0100 | [diff] [blame] | 276 | resource_list_for_each_entry(window, resources) |
Lorenzo Pieralisi | d2be00c | 2015-01-27 18:01:45 +0000 | [diff] [blame] | 277 | kfree(window->res); |
Liviu Dudau | cbe4097 | 2014-09-29 15:29:28 +0100 | [diff] [blame] | 278 | pci_free_resource_list(resources); |
| 279 | return err; |
| 280 | } |
| 281 | EXPORT_SYMBOL_GPL(of_pci_get_host_bridge_resources); |
| 282 | #endif /* CONFIG_OF_ADDRESS */ |
| 283 | |
Thomas Petazzoni | 0d5a6db | 2013-08-09 22:27:09 +0200 | [diff] [blame] | 284 | #ifdef CONFIG_PCI_MSI |
| 285 | |
| 286 | static LIST_HEAD(of_pci_msi_chip_list); |
| 287 | static DEFINE_MUTEX(of_pci_msi_chip_mutex); |
| 288 | |
Yijing Wang | c2791b8 | 2014-11-11 17:45:45 -0700 | [diff] [blame] | 289 | int of_pci_msi_chip_add(struct msi_controller *chip) |
Thomas Petazzoni | 0d5a6db | 2013-08-09 22:27:09 +0200 | [diff] [blame] | 290 | { |
| 291 | if (!of_property_read_bool(chip->of_node, "msi-controller")) |
| 292 | return -EINVAL; |
| 293 | |
| 294 | mutex_lock(&of_pci_msi_chip_mutex); |
| 295 | list_add(&chip->list, &of_pci_msi_chip_list); |
| 296 | mutex_unlock(&of_pci_msi_chip_mutex); |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | EXPORT_SYMBOL_GPL(of_pci_msi_chip_add); |
| 301 | |
Yijing Wang | c2791b8 | 2014-11-11 17:45:45 -0700 | [diff] [blame] | 302 | void of_pci_msi_chip_remove(struct msi_controller *chip) |
Thomas Petazzoni | 0d5a6db | 2013-08-09 22:27:09 +0200 | [diff] [blame] | 303 | { |
| 304 | mutex_lock(&of_pci_msi_chip_mutex); |
| 305 | list_del(&chip->list); |
| 306 | mutex_unlock(&of_pci_msi_chip_mutex); |
| 307 | } |
| 308 | EXPORT_SYMBOL_GPL(of_pci_msi_chip_remove); |
| 309 | |
Yijing Wang | c2791b8 | 2014-11-11 17:45:45 -0700 | [diff] [blame] | 310 | struct msi_controller *of_pci_find_msi_chip_by_node(struct device_node *of_node) |
Thomas Petazzoni | 0d5a6db | 2013-08-09 22:27:09 +0200 | [diff] [blame] | 311 | { |
Yijing Wang | c2791b8 | 2014-11-11 17:45:45 -0700 | [diff] [blame] | 312 | struct msi_controller *c; |
Thomas Petazzoni | 0d5a6db | 2013-08-09 22:27:09 +0200 | [diff] [blame] | 313 | |
| 314 | mutex_lock(&of_pci_msi_chip_mutex); |
| 315 | list_for_each_entry(c, &of_pci_msi_chip_list, list) { |
| 316 | if (c->of_node == of_node) { |
| 317 | mutex_unlock(&of_pci_msi_chip_mutex); |
| 318 | return c; |
| 319 | } |
| 320 | } |
| 321 | mutex_unlock(&of_pci_msi_chip_mutex); |
| 322 | |
| 323 | return NULL; |
| 324 | } |
| 325 | EXPORT_SYMBOL_GPL(of_pci_find_msi_chip_by_node); |
| 326 | |
| 327 | #endif /* CONFIG_PCI_MSI */ |