acpi.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #include <linux/pci.h>
  2. #include <linux/acpi.h>
  3. #include <linux/init.h>
  4. #include <linux/irq.h>
  5. #include <linux/dmi.h>
  6. #include <asm/numa.h>
  7. #include <asm/pci_x86.h>
  8. struct pci_root_info {
  9. char *name;
  10. unsigned int res_num;
  11. struct resource *res;
  12. struct pci_bus *bus;
  13. int busnum;
  14. };
  15. static acpi_status
  16. resource_to_addr(struct acpi_resource *resource,
  17. struct acpi_resource_address64 *addr)
  18. {
  19. acpi_status status;
  20. status = acpi_resource_to_address64(resource, addr);
  21. if (ACPI_SUCCESS(status) &&
  22. (addr->resource_type == ACPI_MEMORY_RANGE ||
  23. addr->resource_type == ACPI_IO_RANGE) &&
  24. addr->address_length > 0 &&
  25. addr->producer_consumer == ACPI_PRODUCER) {
  26. return AE_OK;
  27. }
  28. return AE_ERROR;
  29. }
  30. static acpi_status
  31. count_resource(struct acpi_resource *acpi_res, void *data)
  32. {
  33. struct pci_root_info *info = data;
  34. struct acpi_resource_address64 addr;
  35. acpi_status status;
  36. if (info->res_num >= PCI_BUS_NUM_RESOURCES)
  37. return AE_OK;
  38. status = resource_to_addr(acpi_res, &addr);
  39. if (ACPI_SUCCESS(status))
  40. info->res_num++;
  41. return AE_OK;
  42. }
  43. static acpi_status
  44. setup_resource(struct acpi_resource *acpi_res, void *data)
  45. {
  46. struct pci_root_info *info = data;
  47. struct resource *res;
  48. struct acpi_resource_address64 addr;
  49. acpi_status status;
  50. unsigned long flags;
  51. struct resource *root;
  52. if (info->res_num >= PCI_BUS_NUM_RESOURCES)
  53. return AE_OK;
  54. status = resource_to_addr(acpi_res, &addr);
  55. if (!ACPI_SUCCESS(status))
  56. return AE_OK;
  57. if (addr.resource_type == ACPI_MEMORY_RANGE) {
  58. root = &iomem_resource;
  59. flags = IORESOURCE_MEM;
  60. if (addr.info.mem.caching == ACPI_PREFETCHABLE_MEMORY)
  61. flags |= IORESOURCE_PREFETCH;
  62. } else if (addr.resource_type == ACPI_IO_RANGE) {
  63. root = &ioport_resource;
  64. flags = IORESOURCE_IO;
  65. } else
  66. return AE_OK;
  67. res = &info->res[info->res_num];
  68. res->name = info->name;
  69. res->flags = flags;
  70. res->start = addr.minimum + addr.translation_offset;
  71. res->end = res->start + addr.address_length - 1;
  72. res->child = NULL;
  73. if (insert_resource(root, res)) {
  74. printk(KERN_ERR "PCI: Failed to allocate 0x%lx-0x%lx "
  75. "from %s for %s\n", (unsigned long) res->start,
  76. (unsigned long) res->end, root->name, info->name);
  77. } else {
  78. info->bus->resource[info->res_num] = res;
  79. info->res_num++;
  80. }
  81. return AE_OK;
  82. }
  83. static void
  84. adjust_transparent_bridge_resources(struct pci_bus *bus)
  85. {
  86. struct pci_dev *dev;
  87. list_for_each_entry(dev, &bus->devices, bus_list) {
  88. int i;
  89. u16 class = dev->class >> 8;
  90. if (class == PCI_CLASS_BRIDGE_PCI && dev->transparent) {
  91. for(i = 3; i < PCI_BUS_NUM_RESOURCES; i++)
  92. dev->subordinate->resource[i] =
  93. dev->bus->resource[i - 3];
  94. }
  95. }
  96. }
  97. static void
  98. get_current_resources(struct acpi_device *device, int busnum,
  99. int domain, struct pci_bus *bus)
  100. {
  101. struct pci_root_info info;
  102. size_t size;
  103. info.bus = bus;
  104. info.res_num = 0;
  105. acpi_walk_resources(device->handle, METHOD_NAME__CRS, count_resource,
  106. &info);
  107. if (!info.res_num)
  108. return;
  109. size = sizeof(*info.res) * info.res_num;
  110. info.res = kmalloc(size, GFP_KERNEL);
  111. if (!info.res)
  112. goto res_alloc_fail;
  113. info.name = kmalloc(16, GFP_KERNEL);
  114. if (!info.name)
  115. goto name_alloc_fail;
  116. sprintf(info.name, "PCI Bus %04x:%02x", domain, busnum);
  117. info.res_num = 0;
  118. acpi_walk_resources(device->handle, METHOD_NAME__CRS, setup_resource,
  119. &info);
  120. if (info.res_num)
  121. adjust_transparent_bridge_resources(bus);
  122. return;
  123. name_alloc_fail:
  124. kfree(info.res);
  125. res_alloc_fail:
  126. return;
  127. }
  128. struct pci_bus * __devinit pci_acpi_scan_root(struct acpi_device *device, int domain, int busnum)
  129. {
  130. struct pci_bus *bus;
  131. struct pci_sysdata *sd;
  132. int node;
  133. #ifdef CONFIG_ACPI_NUMA
  134. int pxm;
  135. #endif
  136. if (domain && !pci_domains_supported) {
  137. printk(KERN_WARNING "PCI: Multiple domains not supported "
  138. "(dom %d, bus %d)\n", domain, busnum);
  139. return NULL;
  140. }
  141. node = -1;
  142. #ifdef CONFIG_ACPI_NUMA
  143. pxm = acpi_get_pxm(device->handle);
  144. if (pxm >= 0)
  145. node = pxm_to_node(pxm);
  146. if (node != -1)
  147. set_mp_bus_to_node(busnum, node);
  148. else
  149. #endif
  150. node = get_mp_bus_to_node(busnum);
  151. if (node != -1 && !node_online(node))
  152. node = -1;
  153. /* Allocate per-root-bus (not per bus) arch-specific data.
  154. * TODO: leak; this memory is never freed.
  155. * It's arguable whether it's worth the trouble to care.
  156. */
  157. sd = kzalloc(sizeof(*sd), GFP_KERNEL);
  158. if (!sd) {
  159. printk(KERN_ERR "PCI: OOM, not probing PCI bus %02x\n", busnum);
  160. return NULL;
  161. }
  162. sd->domain = domain;
  163. sd->node = node;
  164. /*
  165. * Maybe the desired pci bus has been already scanned. In such case
  166. * it is unnecessary to scan the pci bus with the given domain,busnum.
  167. */
  168. bus = pci_find_bus(domain, busnum);
  169. if (bus) {
  170. /*
  171. * If the desired bus exits, the content of bus->sysdata will
  172. * be replaced by sd.
  173. */
  174. memcpy(bus->sysdata, sd, sizeof(*sd));
  175. kfree(sd);
  176. } else
  177. bus = pci_scan_bus_parented(NULL, busnum, &pci_root_ops, sd);
  178. if (!bus)
  179. kfree(sd);
  180. if (bus && node != -1) {
  181. #ifdef CONFIG_ACPI_NUMA
  182. if (pxm >= 0)
  183. dev_printk(KERN_DEBUG, &bus->dev,
  184. "on NUMA node %d (pxm %d)\n", node, pxm);
  185. #else
  186. dev_printk(KERN_DEBUG, &bus->dev, "on NUMA node %d\n", node);
  187. #endif
  188. }
  189. if (bus && (pci_probe & PCI_USE__CRS))
  190. get_current_resources(device, busnum, domain, bus);
  191. return bus;
  192. }
  193. int __init pci_acpi_init(void)
  194. {
  195. struct pci_dev *dev = NULL;
  196. if (pcibios_scanned)
  197. return 0;
  198. if (acpi_noirq)
  199. return 0;
  200. printk(KERN_INFO "PCI: Using ACPI for IRQ routing\n");
  201. acpi_irq_penalty_init();
  202. pcibios_scanned++;
  203. pcibios_enable_irq = acpi_pci_irq_enable;
  204. pcibios_disable_irq = acpi_pci_irq_disable;
  205. if (pci_routeirq) {
  206. /*
  207. * PCI IRQ routing is set up by pci_enable_device(), but we
  208. * also do it here in case there are still broken drivers that
  209. * don't use pci_enable_device().
  210. */
  211. printk(KERN_INFO "PCI: Routing PCI interrupts for all devices because \"pci=routeirq\" specified\n");
  212. for_each_pci_dev(dev)
  213. acpi_pci_irq_enable(dev);
  214. }
  215. return 0;
  216. }