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