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