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