/kern_oII/arch/x86/pci/acpi.c

http://omnia2droid.googlecode.com/ · C · 263 lines · 209 code · 37 blank · 17 comment · 44 complexity · 0417a5d7394bd080bf9e93d28b17ec35 MD5 · raw file

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