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