io_acpi_init.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2006 Silicon Graphics, Inc. All rights reserved.
  7. */
  8. #include <asm/sn/types.h>
  9. #include <asm/sn/addrs.h>
  10. #include <asm/sn/pcidev.h>
  11. #include <asm/sn/pcibus_provider_defs.h>
  12. #include <asm/sn/sn_sal.h>
  13. #include "xtalk/hubdev.h"
  14. #include <linux/acpi.h>
  15. /*
  16. * The code in this file will only be executed when running with
  17. * a PROM that has ACPI IO support. (i.e., SN_ACPI_BASE_SUPPORT() == 1)
  18. */
  19. /*
  20. * This value must match the UUID the PROM uses
  21. * (io/acpi/defblk.c) when building a vendor descriptor.
  22. */
  23. struct acpi_vendor_uuid sn_uuid = {
  24. .subtype = 0,
  25. .data = { 0x2c, 0xc6, 0xa6, 0xfe, 0x9c, 0x44, 0xda, 0x11,
  26. 0xa2, 0x7c, 0x08, 0x00, 0x69, 0x13, 0xea, 0x51 },
  27. };
  28. struct sn_pcidev_match {
  29. u8 bus;
  30. unsigned int devfn;
  31. acpi_handle handle;
  32. };
  33. /*
  34. * Perform the early IO init in PROM.
  35. */
  36. static s64
  37. sal_ioif_init(u64 *result)
  38. {
  39. struct ia64_sal_retval isrv = {0,0,0,0};
  40. SAL_CALL_NOLOCK(isrv,
  41. SN_SAL_IOIF_INIT, 0, 0, 0, 0, 0, 0, 0);
  42. *result = isrv.v0;
  43. return isrv.status;
  44. }
  45. /*
  46. * sn_acpi_hubdev_init() - This function is called by acpi_ns_get_device_callback()
  47. * for all SGIHUB and SGITIO acpi devices defined in the
  48. * DSDT. It obtains the hubdev_info pointer from the
  49. * ACPI vendor resource, which the PROM setup, and sets up the
  50. * hubdev_info in the pda.
  51. */
  52. static acpi_status __init
  53. sn_acpi_hubdev_init(acpi_handle handle, u32 depth, void *context, void **ret)
  54. {
  55. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  56. struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  57. u64 addr;
  58. struct hubdev_info *hubdev;
  59. struct hubdev_info *hubdev_ptr;
  60. int i;
  61. u64 nasid;
  62. struct acpi_resource *resource;
  63. acpi_status status;
  64. struct acpi_resource_vendor_typed *vendor;
  65. extern void sn_common_hubdev_init(struct hubdev_info *);
  66. status = acpi_get_vendor_resource(handle, METHOD_NAME__CRS,
  67. &sn_uuid, &buffer);
  68. if (ACPI_FAILURE(status)) {
  69. acpi_get_name(handle, ACPI_FULL_PATHNAME, &name_buffer);
  70. printk(KERN_ERR
  71. "sn_acpi_hubdev_init: acpi_get_vendor_resource() "
  72. "(0x%x) failed for: %s\n", status,
  73. (char *)name_buffer.pointer);
  74. kfree(name_buffer.pointer);
  75. return AE_OK; /* Continue walking namespace */
  76. }
  77. resource = buffer.pointer;
  78. vendor = &resource->data.vendor_typed;
  79. if ((vendor->byte_length - sizeof(struct acpi_vendor_uuid)) !=
  80. sizeof(struct hubdev_info *)) {
  81. acpi_get_name(handle, ACPI_FULL_PATHNAME, &name_buffer);
  82. printk(KERN_ERR
  83. "sn_acpi_hubdev_init: Invalid vendor data length: "
  84. "%d for: %s\n",
  85. vendor->byte_length, (char *)name_buffer.pointer);
  86. kfree(name_buffer.pointer);
  87. goto exit;
  88. }
  89. memcpy(&addr, vendor->byte_data, sizeof(struct hubdev_info *));
  90. hubdev_ptr = __va((struct hubdev_info *) addr);
  91. nasid = hubdev_ptr->hdi_nasid;
  92. i = nasid_to_cnodeid(nasid);
  93. hubdev = (struct hubdev_info *)(NODEPDA(i)->pdinfo);
  94. *hubdev = *hubdev_ptr;
  95. sn_common_hubdev_init(hubdev);
  96. exit:
  97. kfree(buffer.pointer);
  98. return AE_OK; /* Continue walking namespace */
  99. }
  100. /*
  101. * sn_get_bussoft_ptr() - The pcibus_bussoft pointer is found in
  102. * the ACPI Vendor resource for this bus.
  103. */
  104. static struct pcibus_bussoft *
  105. sn_get_bussoft_ptr(struct pci_bus *bus)
  106. {
  107. u64 addr;
  108. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  109. struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  110. acpi_handle handle;
  111. struct pcibus_bussoft *prom_bussoft_ptr;
  112. struct acpi_resource *resource;
  113. acpi_status status;
  114. struct acpi_resource_vendor_typed *vendor;
  115. handle = PCI_CONTROLLER(bus)->acpi_handle;
  116. status = acpi_get_vendor_resource(handle, METHOD_NAME__CRS,
  117. &sn_uuid, &buffer);
  118. if (ACPI_FAILURE(status)) {
  119. acpi_get_name(handle, ACPI_FULL_PATHNAME, &name_buffer);
  120. printk(KERN_ERR "%s: "
  121. "acpi_get_vendor_resource() failed (0x%x) for: %s\n",
  122. __func__, status, (char *)name_buffer.pointer);
  123. kfree(name_buffer.pointer);
  124. return NULL;
  125. }
  126. resource = buffer.pointer;
  127. vendor = &resource->data.vendor_typed;
  128. if ((vendor->byte_length - sizeof(struct acpi_vendor_uuid)) !=
  129. sizeof(struct pcibus_bussoft *)) {
  130. printk(KERN_ERR
  131. "%s: Invalid vendor data length %d\n",
  132. __func__, vendor->byte_length);
  133. kfree(buffer.pointer);
  134. return NULL;
  135. }
  136. memcpy(&addr, vendor->byte_data, sizeof(struct pcibus_bussoft *));
  137. prom_bussoft_ptr = __va((struct pcibus_bussoft *) addr);
  138. kfree(buffer.pointer);
  139. return prom_bussoft_ptr;
  140. }
  141. /*
  142. * sn_extract_device_info - Extract the pcidev_info and the sn_irq_info
  143. * pointers from the vendor resource using the
  144. * provided acpi handle, and copy the structures
  145. * into the argument buffers.
  146. */
  147. static int
  148. sn_extract_device_info(acpi_handle handle, struct pcidev_info **pcidev_info,
  149. struct sn_irq_info **sn_irq_info)
  150. {
  151. u64 addr;
  152. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  153. struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  154. struct sn_irq_info *irq_info, *irq_info_prom;
  155. struct pcidev_info *pcidev_ptr, *pcidev_prom_ptr;
  156. struct acpi_resource *resource;
  157. int ret = 0;
  158. acpi_status status;
  159. struct acpi_resource_vendor_typed *vendor;
  160. /*
  161. * The pointer to this device's pcidev_info structure in
  162. * the PROM, is in the vendor resource.
  163. */
  164. status = acpi_get_vendor_resource(handle, METHOD_NAME__CRS,
  165. &sn_uuid, &buffer);
  166. if (ACPI_FAILURE(status)) {
  167. acpi_get_name(handle, ACPI_FULL_PATHNAME, &name_buffer);
  168. printk(KERN_ERR
  169. "%s: acpi_get_vendor_resource() failed (0x%x) for: %s\n",
  170. __func__, status, (char *)name_buffer.pointer);
  171. kfree(name_buffer.pointer);
  172. return 1;
  173. }
  174. resource = buffer.pointer;
  175. vendor = &resource->data.vendor_typed;
  176. if ((vendor->byte_length - sizeof(struct acpi_vendor_uuid)) !=
  177. sizeof(struct pci_devdev_info *)) {
  178. acpi_get_name(handle, ACPI_FULL_PATHNAME, &name_buffer);
  179. printk(KERN_ERR
  180. "%s: Invalid vendor data length: %d for: %s\n",
  181. __func__, vendor->byte_length,
  182. (char *)name_buffer.pointer);
  183. kfree(name_buffer.pointer);
  184. ret = 1;
  185. goto exit;
  186. }
  187. pcidev_ptr = kzalloc(sizeof(struct pcidev_info), GFP_KERNEL);
  188. if (!pcidev_ptr)
  189. panic("%s: Unable to alloc memory for pcidev_info", __func__);
  190. memcpy(&addr, vendor->byte_data, sizeof(struct pcidev_info *));
  191. pcidev_prom_ptr = __va(addr);
  192. memcpy(pcidev_ptr, pcidev_prom_ptr, sizeof(struct pcidev_info));
  193. /* Get the IRQ info */
  194. irq_info = kzalloc(sizeof(struct sn_irq_info), GFP_KERNEL);
  195. if (!irq_info)
  196. panic("%s: Unable to alloc memory for sn_irq_info", __func__);
  197. if (pcidev_ptr->pdi_sn_irq_info) {
  198. irq_info_prom = __va(pcidev_ptr->pdi_sn_irq_info);
  199. memcpy(irq_info, irq_info_prom, sizeof(struct sn_irq_info));
  200. }
  201. *pcidev_info = pcidev_ptr;
  202. *sn_irq_info = irq_info;
  203. exit:
  204. kfree(buffer.pointer);
  205. return ret;
  206. }
  207. static unsigned int
  208. get_host_devfn(acpi_handle device_handle, acpi_handle rootbus_handle)
  209. {
  210. unsigned long long adr;
  211. acpi_handle child;
  212. unsigned int devfn;
  213. int function;
  214. acpi_handle parent;
  215. int slot;
  216. acpi_status status;
  217. struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  218. acpi_get_name(device_handle, ACPI_FULL_PATHNAME, &name_buffer);
  219. /*
  220. * Do an upward search to find the root bus device, and
  221. * obtain the host devfn from the previous child device.
  222. */
  223. child = device_handle;
  224. while (child) {
  225. status = acpi_get_parent(child, &parent);
  226. if (ACPI_FAILURE(status)) {
  227. printk(KERN_ERR "%s: acpi_get_parent() failed "
  228. "(0x%x) for: %s\n", __func__, status,
  229. (char *)name_buffer.pointer);
  230. panic("%s: Unable to find host devfn\n", __func__);
  231. }
  232. if (parent == rootbus_handle)
  233. break;
  234. child = parent;
  235. }
  236. if (!child) {
  237. printk(KERN_ERR "%s: Unable to find root bus for: %s\n",
  238. __func__, (char *)name_buffer.pointer);
  239. BUG();
  240. }
  241. status = acpi_evaluate_integer(child, METHOD_NAME__ADR, NULL, &adr);
  242. if (ACPI_FAILURE(status)) {
  243. printk(KERN_ERR "%s: Unable to get _ADR (0x%x) for: %s\n",
  244. __func__, status, (char *)name_buffer.pointer);
  245. panic("%s: Unable to find host devfn\n", __func__);
  246. }
  247. kfree(name_buffer.pointer);
  248. slot = (adr >> 16) & 0xffff;
  249. function = adr & 0xffff;
  250. devfn = PCI_DEVFN(slot, function);
  251. return devfn;
  252. }
  253. /*
  254. * find_matching_device - Callback routine to find the ACPI device
  255. * that matches up with our pci_dev device.
  256. * Matching is done on bus number and devfn.
  257. * To find the bus number for a particular
  258. * ACPI device, we must look at the _BBN method
  259. * of its parent.
  260. */
  261. static acpi_status
  262. find_matching_device(acpi_handle handle, u32 lvl, void *context, void **rv)
  263. {
  264. unsigned long long bbn = -1;
  265. unsigned long long adr;
  266. acpi_handle parent = NULL;
  267. acpi_status status;
  268. unsigned int devfn;
  269. int function;
  270. int slot;
  271. struct sn_pcidev_match *info = context;
  272. struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  273. status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
  274. &adr);
  275. if (ACPI_SUCCESS(status)) {
  276. status = acpi_get_parent(handle, &parent);
  277. if (ACPI_FAILURE(status)) {
  278. acpi_get_name(handle, ACPI_FULL_PATHNAME, &name_buffer);
  279. printk(KERN_ERR
  280. "%s: acpi_get_parent() failed (0x%x) for: %s\n",
  281. __func__, status, (char *)name_buffer.pointer);
  282. kfree(name_buffer.pointer);
  283. return AE_OK;
  284. }
  285. status = acpi_evaluate_integer(parent, METHOD_NAME__BBN,
  286. NULL, &bbn);
  287. if (ACPI_FAILURE(status)) {
  288. acpi_get_name(handle, ACPI_FULL_PATHNAME, &name_buffer);
  289. printk(KERN_ERR
  290. "%s: Failed to find _BBN in parent of: %s\n",
  291. __func__, (char *)name_buffer.pointer);
  292. kfree(name_buffer.pointer);
  293. return AE_OK;
  294. }
  295. slot = (adr >> 16) & 0xffff;
  296. function = adr & 0xffff;
  297. devfn = PCI_DEVFN(slot, function);
  298. if ((info->devfn == devfn) && (info->bus == bbn)) {
  299. /* We have a match! */
  300. info->handle = handle;
  301. return 1;
  302. }
  303. }
  304. return AE_OK;
  305. }
  306. /*
  307. * sn_acpi_get_pcidev_info - Search ACPI namespace for the acpi
  308. * device matching the specified pci_dev,
  309. * and return the pcidev info and irq info.
  310. */
  311. int
  312. sn_acpi_get_pcidev_info(struct pci_dev *dev, struct pcidev_info **pcidev_info,
  313. struct sn_irq_info **sn_irq_info)
  314. {
  315. unsigned int host_devfn;
  316. struct sn_pcidev_match pcidev_match;
  317. acpi_handle rootbus_handle;
  318. unsigned long long segment;
  319. acpi_status status;
  320. struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  321. rootbus_handle = PCI_CONTROLLER(dev)->acpi_handle;
  322. status = acpi_evaluate_integer(rootbus_handle, METHOD_NAME__SEG, NULL,
  323. &segment);
  324. if (ACPI_SUCCESS(status)) {
  325. if (segment != pci_domain_nr(dev)) {
  326. acpi_get_name(rootbus_handle, ACPI_FULL_PATHNAME,
  327. &name_buffer);
  328. printk(KERN_ERR
  329. "%s: Segment number mismatch, 0x%llx vs 0x%x for: %s\n",
  330. __func__, segment, pci_domain_nr(dev),
  331. (char *)name_buffer.pointer);
  332. kfree(name_buffer.pointer);
  333. return 1;
  334. }
  335. } else {
  336. acpi_get_name(rootbus_handle, ACPI_FULL_PATHNAME, &name_buffer);
  337. printk(KERN_ERR "%s: Unable to get __SEG from: %s\n",
  338. __func__, (char *)name_buffer.pointer);
  339. kfree(name_buffer.pointer);
  340. return 1;
  341. }
  342. /*
  343. * We want to search all devices in this segment/domain
  344. * of the ACPI namespace for the matching ACPI device,
  345. * which holds the pcidev_info pointer in its vendor resource.
  346. */
  347. pcidev_match.bus = dev->bus->number;
  348. pcidev_match.devfn = dev->devfn;
  349. pcidev_match.handle = NULL;
  350. acpi_walk_namespace(ACPI_TYPE_DEVICE, rootbus_handle, ACPI_UINT32_MAX,
  351. find_matching_device, &pcidev_match, NULL);
  352. if (!pcidev_match.handle) {
  353. printk(KERN_ERR
  354. "%s: Could not find matching ACPI device for %s.\n",
  355. __func__, pci_name(dev));
  356. return 1;
  357. }
  358. if (sn_extract_device_info(pcidev_match.handle, pcidev_info, sn_irq_info))
  359. return 1;
  360. /* Build up the pcidev_info.pdi_slot_host_handle */
  361. host_devfn = get_host_devfn(pcidev_match.handle, rootbus_handle);
  362. (*pcidev_info)->pdi_slot_host_handle =
  363. ((unsigned long) pci_domain_nr(dev) << 40) |
  364. /* bus == 0 */
  365. host_devfn;
  366. return 0;
  367. }
  368. /*
  369. * sn_acpi_slot_fixup - Obtain the pcidev_info and sn_irq_info.
  370. * Perform any SN specific slot fixup.
  371. * At present there does not appear to be
  372. * any generic way to handle a ROM image
  373. * that has been shadowed by the PROM, so
  374. * we pass a pointer to it within the
  375. * pcidev_info structure.
  376. */
  377. void
  378. sn_acpi_slot_fixup(struct pci_dev *dev)
  379. {
  380. void __iomem *addr;
  381. struct pcidev_info *pcidev_info = NULL;
  382. struct sn_irq_info *sn_irq_info = NULL;
  383. size_t image_size, size;
  384. if (sn_acpi_get_pcidev_info(dev, &pcidev_info, &sn_irq_info)) {
  385. panic("%s: Failure obtaining pcidev_info for %s\n",
  386. __func__, pci_name(dev));
  387. }
  388. if (pcidev_info->pdi_pio_mapped_addr[PCI_ROM_RESOURCE]) {
  389. /*
  390. * A valid ROM image exists and has been shadowed by the
  391. * PROM. Setup the pci_dev ROM resource with the address
  392. * of the shadowed copy, and the actual length of the ROM image.
  393. */
  394. size = pci_resource_len(dev, PCI_ROM_RESOURCE);
  395. addr = ioremap(pcidev_info->pdi_pio_mapped_addr[PCI_ROM_RESOURCE],
  396. size);
  397. image_size = pci_get_rom_size(dev, addr, size);
  398. dev->resource[PCI_ROM_RESOURCE].start = (unsigned long) addr;
  399. dev->resource[PCI_ROM_RESOURCE].end =
  400. (unsigned long) addr + image_size - 1;
  401. dev->resource[PCI_ROM_RESOURCE].flags |= IORESOURCE_ROM_BIOS_COPY;
  402. }
  403. sn_pci_fixup_slot(dev, pcidev_info, sn_irq_info);
  404. }
  405. EXPORT_SYMBOL(sn_acpi_slot_fixup);
  406. /*
  407. * sn_acpi_bus_fixup - Perform SN specific setup of software structs
  408. * (pcibus_bussoft, pcidev_info) and hardware
  409. * registers, for the specified bus and devices under it.
  410. */
  411. void
  412. sn_acpi_bus_fixup(struct pci_bus *bus)
  413. {
  414. struct pci_dev *pci_dev = NULL;
  415. struct pcibus_bussoft *prom_bussoft_ptr;
  416. if (!bus->parent) { /* If root bus */
  417. prom_bussoft_ptr = sn_get_bussoft_ptr(bus);
  418. if (prom_bussoft_ptr == NULL) {
  419. printk(KERN_ERR
  420. "%s: 0x%04x:0x%02x Unable to "
  421. "obtain prom_bussoft_ptr\n",
  422. __func__, pci_domain_nr(bus), bus->number);
  423. return;
  424. }
  425. sn_common_bus_fixup(bus, prom_bussoft_ptr);
  426. }
  427. list_for_each_entry(pci_dev, &bus->devices, bus_list) {
  428. sn_acpi_slot_fixup(pci_dev);
  429. }
  430. }
  431. /*
  432. * sn_io_acpi_init - PROM has ACPI support for IO, defining at a minimum the
  433. * nodes and root buses in the DSDT. As a result, bus scanning
  434. * will be initiated by the Linux ACPI code.
  435. */
  436. void __init
  437. sn_io_acpi_init(void)
  438. {
  439. u64 result;
  440. s64 status;
  441. /* SN Altix does not follow the IOSAPIC IRQ routing model */
  442. acpi_irq_model = ACPI_IRQ_MODEL_PLATFORM;
  443. /* Setup hubdev_info for all SGIHUB/SGITIO devices */
  444. acpi_get_devices("SGIHUB", sn_acpi_hubdev_init, NULL, NULL);
  445. acpi_get_devices("SGITIO", sn_acpi_hubdev_init, NULL, NULL);
  446. status = sal_ioif_init(&result);
  447. if (status || result)
  448. panic("sal_ioif_init failed: [%lx] %s\n",
  449. status, ia64_sal_strerror(status));
  450. }