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