Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #include <linux/pci.h> |
| 2 | #include <linux/acpi.h> |
| 3 | #include <linux/init.h> |
Nick Piggin | b33fa1f | 2005-10-01 02:34:42 +1000 | [diff] [blame] | 4 | #include <linux/irq.h> |
Gary Hade | 036fff4 | 2007-10-03 15:56:14 -0700 | [diff] [blame] | 5 | #include <linux/dmi.h> |
Andi Kleen | 69e1a33 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 6 | #include <asm/numa.h> |
Jaswinder Singh Rajput | 8248771 | 2008-12-27 18:32:28 +0530 | [diff] [blame] | 7 | #include <asm/pci_x86.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | |
Gary Hade | 62f420f | 2007-10-03 15:56:51 -0700 | [diff] [blame] | 9 | struct pci_root_info { |
Bjorn Helgaas | 42887b2 | 2009-10-06 15:33:49 -0600 | [diff] [blame] | 10 | struct acpi_device *bridge; |
Gary Hade | 62f420f | 2007-10-03 15:56:51 -0700 | [diff] [blame] | 11 | char *name; |
| 12 | unsigned int res_num; |
| 13 | struct resource *res; |
| 14 | struct pci_bus *bus; |
| 15 | int busnum; |
| 16 | }; |
| 17 | |
| 18 | static acpi_status |
| 19 | resource_to_addr(struct acpi_resource *resource, |
| 20 | struct acpi_resource_address64 *addr) |
| 21 | { |
| 22 | acpi_status status; |
| 23 | |
| 24 | status = acpi_resource_to_address64(resource, addr); |
| 25 | if (ACPI_SUCCESS(status) && |
| 26 | (addr->resource_type == ACPI_MEMORY_RANGE || |
| 27 | addr->resource_type == ACPI_IO_RANGE) && |
| 28 | addr->address_length > 0 && |
| 29 | addr->producer_consumer == ACPI_PRODUCER) { |
| 30 | return AE_OK; |
| 31 | } |
| 32 | return AE_ERROR; |
| 33 | } |
| 34 | |
| 35 | static acpi_status |
| 36 | count_resource(struct acpi_resource *acpi_res, void *data) |
| 37 | { |
| 38 | struct pci_root_info *info = data; |
| 39 | struct acpi_resource_address64 addr; |
| 40 | acpi_status status; |
| 41 | |
| 42 | status = resource_to_addr(acpi_res, &addr); |
| 43 | if (ACPI_SUCCESS(status)) |
| 44 | info->res_num++; |
| 45 | return AE_OK; |
| 46 | } |
| 47 | |
Gary Hade | f9cde5f | 2009-05-27 12:41:44 -0700 | [diff] [blame] | 48 | static int |
| 49 | bus_has_transparent_bridge(struct pci_bus *bus) |
| 50 | { |
| 51 | struct pci_dev *dev; |
| 52 | |
| 53 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 54 | u16 class = dev->class >> 8; |
| 55 | |
| 56 | if (class == PCI_CLASS_BRIDGE_PCI && dev->transparent) |
| 57 | return true; |
| 58 | } |
| 59 | return false; |
| 60 | } |
| 61 | |
Bjorn Helgaas | 03db42a | 2009-11-04 10:39:18 -0700 | [diff] [blame] | 62 | static void |
| 63 | align_resource(struct acpi_device *bridge, struct resource *res) |
| 64 | { |
| 65 | int align = (res->flags & IORESOURCE_MEM) ? 16 : 4; |
| 66 | |
| 67 | /* |
| 68 | * Host bridge windows are not BARs, but the decoders on the PCI side |
| 69 | * that claim this address space have starting alignment and length |
| 70 | * constraints, so fix any obvious BIOS goofs. |
| 71 | */ |
Bjorn Helgaas | ea7f1b6 | 2009-11-05 11:17:11 -0600 | [diff] [blame] | 72 | if (!IS_ALIGNED(res->start, align)) { |
Bjorn Helgaas | 03db42a | 2009-11-04 10:39:18 -0700 | [diff] [blame] | 73 | dev_printk(KERN_DEBUG, &bridge->dev, |
| 74 | "host bridge window %pR invalid; " |
| 75 | "aligning start to %d-byte boundary\n", res, align); |
| 76 | res->start &= ~(align - 1); |
| 77 | } |
Bjorn Helgaas | ea7f1b6 | 2009-11-05 11:17:11 -0600 | [diff] [blame] | 78 | if (!IS_ALIGNED(res->end + 1, align)) { |
Bjorn Helgaas | 03db42a | 2009-11-04 10:39:18 -0700 | [diff] [blame] | 79 | dev_printk(KERN_DEBUG, &bridge->dev, |
| 80 | "host bridge window %pR invalid; " |
| 81 | "aligning end to %d-byte boundary\n", res, align); |
Bjorn Helgaas | ea7f1b6 | 2009-11-05 11:17:11 -0600 | [diff] [blame] | 82 | res->end = ALIGN(res->end, align) - 1; |
Bjorn Helgaas | 03db42a | 2009-11-04 10:39:18 -0700 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Gary Hade | 62f420f | 2007-10-03 15:56:51 -0700 | [diff] [blame] | 86 | static acpi_status |
| 87 | setup_resource(struct acpi_resource *acpi_res, void *data) |
| 88 | { |
| 89 | struct pci_root_info *info = data; |
| 90 | struct resource *res; |
| 91 | struct acpi_resource_address64 addr; |
| 92 | acpi_status status; |
| 93 | unsigned long flags; |
| 94 | struct resource *root; |
Gary Hade | f9cde5f | 2009-05-27 12:41:44 -0700 | [diff] [blame] | 95 | int max_root_bus_resources = PCI_BUS_NUM_RESOURCES; |
Yinghai Lu | 2cdb3f1 | 2009-06-24 19:01:19 -0700 | [diff] [blame] | 96 | u64 start, end; |
| 97 | |
| 98 | if (bus_has_transparent_bridge(info->bus)) |
| 99 | max_root_bus_resources -= 3; |
Yinghai Lu | 3d9befd | 2007-11-17 16:27:01 +0100 | [diff] [blame] | 100 | |
Gary Hade | 62f420f | 2007-10-03 15:56:51 -0700 | [diff] [blame] | 101 | status = resource_to_addr(acpi_res, &addr); |
| 102 | if (!ACPI_SUCCESS(status)) |
| 103 | return AE_OK; |
| 104 | |
| 105 | if (addr.resource_type == ACPI_MEMORY_RANGE) { |
| 106 | root = &iomem_resource; |
| 107 | flags = IORESOURCE_MEM; |
| 108 | if (addr.info.mem.caching == ACPI_PREFETCHABLE_MEMORY) |
| 109 | flags |= IORESOURCE_PREFETCH; |
| 110 | } else if (addr.resource_type == ACPI_IO_RANGE) { |
| 111 | root = &ioport_resource; |
| 112 | flags = IORESOURCE_IO; |
| 113 | } else |
| 114 | return AE_OK; |
| 115 | |
Yinghai Lu | 2cdb3f1 | 2009-06-24 19:01:19 -0700 | [diff] [blame] | 116 | start = addr.minimum + addr.translation_offset; |
| 117 | end = start + addr.address_length - 1; |
Gary Hade | f9cde5f | 2009-05-27 12:41:44 -0700 | [diff] [blame] | 118 | if (info->res_num >= max_root_bus_resources) { |
Bjorn Helgaas | f1db6fd | 2009-11-04 10:39:13 -0700 | [diff] [blame] | 119 | if (pci_probe & PCI_USE__CRS) |
| 120 | printk(KERN_WARNING "PCI: Failed to allocate " |
| 121 | "0x%lx-0x%lx from %s for %s due to _CRS " |
| 122 | "returning more than %d resource descriptors\n", |
| 123 | (unsigned long) start, (unsigned long) end, |
| 124 | root->name, info->name, max_root_bus_resources); |
Gary Hade | f9cde5f | 2009-05-27 12:41:44 -0700 | [diff] [blame] | 125 | return AE_OK; |
| 126 | } |
| 127 | |
Yinghai Lu | 2cdb3f1 | 2009-06-24 19:01:19 -0700 | [diff] [blame] | 128 | res = &info->res[info->res_num]; |
| 129 | res->name = info->name; |
| 130 | res->flags = flags; |
| 131 | res->start = start; |
| 132 | res->end = end; |
| 133 | res->child = NULL; |
Bjorn Helgaas | 03db42a | 2009-11-04 10:39:18 -0700 | [diff] [blame] | 134 | align_resource(info->bridge, res); |
Yinghai Lu | 2cdb3f1 | 2009-06-24 19:01:19 -0700 | [diff] [blame] | 135 | |
Bjorn Helgaas | f1db6fd | 2009-11-04 10:39:13 -0700 | [diff] [blame] | 136 | if (!(pci_probe & PCI_USE__CRS)) { |
| 137 | dev_printk(KERN_DEBUG, &info->bridge->dev, |
| 138 | "host bridge window %pR (ignored)\n", res); |
| 139 | return AE_OK; |
| 140 | } |
| 141 | |
Gary Hade | 62f420f | 2007-10-03 15:56:51 -0700 | [diff] [blame] | 142 | if (insert_resource(root, res)) { |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 143 | dev_err(&info->bridge->dev, |
| 144 | "can't allocate host bridge window %pR\n", res); |
Gary Hade | 62f420f | 2007-10-03 15:56:51 -0700 | [diff] [blame] | 145 | } else { |
| 146 | info->bus->resource[info->res_num] = res; |
| 147 | info->res_num++; |
Bjorn Helgaas | 42887b2 | 2009-10-06 15:33:49 -0600 | [diff] [blame] | 148 | if (addr.translation_offset) |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 149 | dev_info(&info->bridge->dev, "host bridge window %pR " |
Bjorn Helgaas | 42887b2 | 2009-10-06 15:33:49 -0600 | [diff] [blame] | 150 | "(PCI address [%#llx-%#llx])\n", |
| 151 | res, res->start - addr.translation_offset, |
| 152 | res->end - addr.translation_offset); |
| 153 | else |
| 154 | dev_info(&info->bridge->dev, |
Bjorn Helgaas | c7dabef | 2009-10-27 13:26:47 -0600 | [diff] [blame] | 155 | "host bridge window %pR\n", res); |
Gary Hade | 62f420f | 2007-10-03 15:56:51 -0700 | [diff] [blame] | 156 | } |
| 157 | return AE_OK; |
| 158 | } |
| 159 | |
| 160 | static void |
Gary Hade | 62f420f | 2007-10-03 15:56:51 -0700 | [diff] [blame] | 161 | get_current_resources(struct acpi_device *device, int busnum, |
Gary Hade | cb3576f | 2008-02-08 14:00:52 -0800 | [diff] [blame] | 162 | int domain, struct pci_bus *bus) |
Gary Hade | 62f420f | 2007-10-03 15:56:51 -0700 | [diff] [blame] | 163 | { |
| 164 | struct pci_root_info info; |
| 165 | size_t size; |
| 166 | |
Bjorn Helgaas | f1db6fd | 2009-11-04 10:39:13 -0700 | [diff] [blame] | 167 | if (!(pci_probe & PCI_USE__CRS)) |
| 168 | dev_info(&device->dev, |
| 169 | "ignoring host bridge windows from ACPI; " |
| 170 | "boot with \"pci=use_crs\" to use them\n"); |
| 171 | |
Bjorn Helgaas | 42887b2 | 2009-10-06 15:33:49 -0600 | [diff] [blame] | 172 | info.bridge = device; |
Gary Hade | 62f420f | 2007-10-03 15:56:51 -0700 | [diff] [blame] | 173 | info.bus = bus; |
| 174 | info.res_num = 0; |
| 175 | acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource, |
| 176 | &info); |
| 177 | if (!info.res_num) |
| 178 | return; |
| 179 | |
| 180 | size = sizeof(*info.res) * info.res_num; |
| 181 | info.res = kmalloc(size, GFP_KERNEL); |
| 182 | if (!info.res) |
| 183 | goto res_alloc_fail; |
| 184 | |
Gary Hade | cb3576f | 2008-02-08 14:00:52 -0800 | [diff] [blame] | 185 | info.name = kmalloc(16, GFP_KERNEL); |
Gary Hade | 62f420f | 2007-10-03 15:56:51 -0700 | [diff] [blame] | 186 | if (!info.name) |
| 187 | goto name_alloc_fail; |
Gary Hade | cb3576f | 2008-02-08 14:00:52 -0800 | [diff] [blame] | 188 | sprintf(info.name, "PCI Bus %04x:%02x", domain, busnum); |
Gary Hade | 62f420f | 2007-10-03 15:56:51 -0700 | [diff] [blame] | 189 | |
| 190 | info.res_num = 0; |
| 191 | acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource, |
| 192 | &info); |
Gary Hade | 62f420f | 2007-10-03 15:56:51 -0700 | [diff] [blame] | 193 | |
| 194 | return; |
| 195 | |
| 196 | name_alloc_fail: |
| 197 | kfree(info.res); |
| 198 | res_alloc_fail: |
| 199 | return; |
| 200 | } |
| 201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_device *device, int domain, int busnum) |
| 203 | { |
Andi Kleen | 69e1a33 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 204 | struct pci_bus *bus; |
Muli Ben-Yehuda | 08f1c19 | 2007-07-22 00:23:39 +0300 | [diff] [blame] | 205 | struct pci_sysdata *sd; |
Yinghai Lu | 871d5f8 | 2008-02-19 03:20:09 -0800 | [diff] [blame] | 206 | int node; |
| 207 | #ifdef CONFIG_ACPI_NUMA |
Muli Ben-Yehuda | 08f1c19 | 2007-07-22 00:23:39 +0300 | [diff] [blame] | 208 | int pxm; |
Yinghai Lu | 871d5f8 | 2008-02-19 03:20:09 -0800 | [diff] [blame] | 209 | #endif |
Andi Kleen | 69e1a33 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 210 | |
Jeff Garzik | a79e419 | 2007-10-11 16:58:30 -0400 | [diff] [blame] | 211 | if (domain && !pci_domains_supported) { |
Bjorn Helgaas | 2a6bed8 | 2009-11-04 10:32:47 -0700 | [diff] [blame] | 212 | printk(KERN_WARNING "pci_bus %04x:%02x: " |
| 213 | "ignored (multiple domains not supported)\n", |
| 214 | domain, busnum); |
Jeff Garzik | a79e419 | 2007-10-11 16:58:30 -0400 | [diff] [blame] | 215 | return NULL; |
| 216 | } |
| 217 | |
Yinghai Lu | 871d5f8 | 2008-02-19 03:20:09 -0800 | [diff] [blame] | 218 | node = -1; |
| 219 | #ifdef CONFIG_ACPI_NUMA |
| 220 | pxm = acpi_get_pxm(device->handle); |
| 221 | if (pxm >= 0) |
| 222 | node = pxm_to_node(pxm); |
| 223 | if (node != -1) |
| 224 | set_mp_bus_to_node(busnum, node); |
| 225 | else |
Yinghai Lu | 871d5f8 | 2008-02-19 03:20:09 -0800 | [diff] [blame] | 226 | #endif |
Yinghai Lu | 871d5f8 | 2008-02-19 03:20:09 -0800 | [diff] [blame] | 227 | node = get_mp_bus_to_node(busnum); |
Yinghai Lu | b755de8 | 2008-02-20 12:41:52 -0800 | [diff] [blame] | 228 | |
| 229 | if (node != -1 && !node_online(node)) |
| 230 | node = -1; |
Yinghai Lu | 871d5f8 | 2008-02-19 03:20:09 -0800 | [diff] [blame] | 231 | |
Muli Ben-Yehuda | 08f1c19 | 2007-07-22 00:23:39 +0300 | [diff] [blame] | 232 | /* Allocate per-root-bus (not per bus) arch-specific data. |
| 233 | * TODO: leak; this memory is never freed. |
| 234 | * It's arguable whether it's worth the trouble to care. |
| 235 | */ |
| 236 | sd = kzalloc(sizeof(*sd), GFP_KERNEL); |
| 237 | if (!sd) { |
Bjorn Helgaas | 2a6bed8 | 2009-11-04 10:32:47 -0700 | [diff] [blame] | 238 | printk(KERN_WARNING "pci_bus %04x:%02x: " |
| 239 | "ignored (out of memory)\n", domain, busnum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | return NULL; |
| 241 | } |
| 242 | |
Jeff Garzik | a79e419 | 2007-10-11 16:58:30 -0400 | [diff] [blame] | 243 | sd->domain = domain; |
Yinghai Lu | 871d5f8 | 2008-02-19 03:20:09 -0800 | [diff] [blame] | 244 | sd->node = node; |
yakui.zhao@intel.com | b87e81e | 2008-04-15 14:34:49 -0700 | [diff] [blame] | 245 | /* |
| 246 | * Maybe the desired pci bus has been already scanned. In such case |
| 247 | * it is unnecessary to scan the pci bus with the given domain,busnum. |
| 248 | */ |
| 249 | bus = pci_find_bus(domain, busnum); |
| 250 | if (bus) { |
| 251 | /* |
| 252 | * If the desired bus exits, the content of bus->sysdata will |
| 253 | * be replaced by sd. |
| 254 | */ |
| 255 | memcpy(bus->sysdata, sd, sizeof(*sd)); |
| 256 | kfree(sd); |
Yinghai Lu | 626fdfe | 2009-06-24 20:00:12 -0700 | [diff] [blame] | 257 | } else { |
| 258 | bus = pci_create_bus(NULL, busnum, &pci_root_ops, sd); |
| 259 | if (bus) { |
Bjorn Helgaas | f1db6fd | 2009-11-04 10:39:13 -0700 | [diff] [blame] | 260 | get_current_resources(device, busnum, domain, bus); |
Yinghai Lu | 626fdfe | 2009-06-24 20:00:12 -0700 | [diff] [blame] | 261 | bus->subordinate = pci_scan_child_bus(bus); |
| 262 | } |
| 263 | } |
Muli Ben-Yehuda | 08f1c19 | 2007-07-22 00:23:39 +0300 | [diff] [blame] | 264 | |
Muli Ben-Yehuda | 08f1c19 | 2007-07-22 00:23:39 +0300 | [diff] [blame] | 265 | if (!bus) |
| 266 | kfree(sd); |
| 267 | |
Yinghai Lu | dbb6152 | 2008-04-19 01:30:16 -0700 | [diff] [blame] | 268 | if (bus && node != -1) { |
Andi Kleen | 69e1a33 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 269 | #ifdef CONFIG_ACPI_NUMA |
Yinghai Lu | dbb6152 | 2008-04-19 01:30:16 -0700 | [diff] [blame] | 270 | if (pxm >= 0) |
Bjorn Helgaas | 2b8c2ef | 2008-12-18 16:34:51 -0700 | [diff] [blame] | 271 | dev_printk(KERN_DEBUG, &bus->dev, |
| 272 | "on NUMA node %d (pxm %d)\n", node, pxm); |
Yinghai Lu | dbb6152 | 2008-04-19 01:30:16 -0700 | [diff] [blame] | 273 | #else |
Bjorn Helgaas | 2b8c2ef | 2008-12-18 16:34:51 -0700 | [diff] [blame] | 274 | dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node); |
Andi Kleen | 69e1a33 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 275 | #endif |
Yinghai Lu | dbb6152 | 2008-04-19 01:30:16 -0700 | [diff] [blame] | 276 | } |
Gary Hade | 62f420f | 2007-10-03 15:56:51 -0700 | [diff] [blame] | 277 | |
Andi Kleen | 69e1a33 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 278 | return bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Robert Richter | 8dd779b | 2008-07-02 22:50:29 +0200 | [diff] [blame] | 281 | int __init pci_acpi_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | { |
| 283 | struct pci_dev *dev = NULL; |
| 284 | |
| 285 | if (pcibios_scanned) |
| 286 | return 0; |
| 287 | |
| 288 | if (acpi_noirq) |
| 289 | return 0; |
| 290 | |
| 291 | printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n"); |
| 292 | acpi_irq_penalty_init(); |
| 293 | pcibios_scanned++; |
| 294 | pcibios_enable_irq = acpi_pci_irq_enable; |
David Shaohua Li | 87bec66 | 2005-07-27 23:02:00 -0400 | [diff] [blame] | 295 | pcibios_disable_irq = acpi_pci_irq_disable; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | |
| 297 | if (pci_routeirq) { |
| 298 | /* |
| 299 | * PCI IRQ routing is set up by pci_enable_device(), but we |
| 300 | * also do it here in case there are still broken drivers that |
| 301 | * don't use pci_enable_device(). |
| 302 | */ |
| 303 | printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n"); |
Hanna Linder | fb37fb9 | 2005-11-06 23:39:36 -0800 | [diff] [blame] | 304 | for_each_pci_dev(dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | acpi_pci_irq_enable(dev); |
Bjorn Helgaas | 657472e9 | 2008-02-18 09:44:13 -0700 | [diff] [blame] | 306 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | return 0; |
| 309 | } |