io_acpi_init.c 14 KB

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