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
6 * Copyright (C) 2006 Silicon Graphics, Inc. All rights reserved.
9 #include <asm/sn/types.h>
10 #include <asm/sn/addrs.h>
11 #include <asm/sn/pcidev.h>
12 #include <asm/sn/pcibus_provider_defs.h>
13 #include <asm/sn/sn_sal.h>
14 #include "xtalk/hubdev.h"
15 #include <linux/acpi.h>
16 #include <acpi/acnamesp.h>
20 * The code in this file will only be executed when running with
21 * a PROM that has ACPI IO support. (i.e., SN_ACPI_BASE_SUPPORT() == 1)
26 * This value must match the UUID the PROM uses
27 * (io/acpi/defblk.c) when building a vendor descriptor.
29 struct acpi_vendor_uuid sn_uuid = {
31 .data = { 0x2c, 0xc6, 0xa6, 0xfe, 0x9c, 0x44, 0xda, 0x11,
32 0xa2, 0x7c, 0x08, 0x00, 0x69, 0x13, 0xea, 0x51 },
35 struct sn_pcidev_match {
42 * Perform the early IO init in PROM.
45 sal_ioif_init(u64 *result)
47 struct ia64_sal_retval isrv = {0,0,0,0};
50 SN_SAL_IOIF_INIT, 0, 0, 0, 0, 0, 0, 0);
56 * sn_hubdev_add - The 'add' function of the acpi_sn_hubdev_driver.
57 * Called for every "SGIHUB" or "SGITIO" device defined
58 * in the ACPI namespace.
61 sn_hubdev_add(struct acpi_device *device)
63 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
65 struct hubdev_info *hubdev;
66 struct hubdev_info *hubdev_ptr;
69 struct acpi_resource *resource;
72 struct acpi_resource_vendor_typed *vendor;
73 extern void sn_common_hubdev_init(struct hubdev_info *);
75 status = acpi_get_vendor_resource(device->handle, METHOD_NAME__CRS,
77 if (ACPI_FAILURE(status)) {
79 "sn_hubdev_add: acpi_get_vendor_resource() failed: %d\n",
84 resource = buffer.pointer;
85 vendor = &resource->data.vendor_typed;
86 if ((vendor->byte_length - sizeof(struct acpi_vendor_uuid)) !=
87 sizeof(struct hubdev_info *)) {
89 "sn_hubdev_add: Invalid vendor data length: %d\n",
95 memcpy(&addr, vendor->byte_data, sizeof(struct hubdev_info *));
96 hubdev_ptr = __va((struct hubdev_info *) addr);
98 nasid = hubdev_ptr->hdi_nasid;
99 i = nasid_to_cnodeid(nasid);
100 hubdev = (struct hubdev_info *)(NODEPDA(i)->pdinfo);
101 *hubdev = *hubdev_ptr;
102 sn_common_hubdev_init(hubdev);
105 kfree(buffer.pointer);
110 * sn_get_bussoft_ptr() - The pcibus_bussoft pointer is found in
111 * the ACPI Vendor resource for this bus.
113 static struct pcibus_bussoft *
114 sn_get_bussoft_ptr(struct pci_bus *bus)
117 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
119 struct pcibus_bussoft *prom_bussoft_ptr;
120 struct acpi_resource *resource;
122 struct acpi_resource_vendor_typed *vendor;
125 handle = PCI_CONTROLLER(bus)->acpi_handle;
126 status = acpi_get_vendor_resource(handle, METHOD_NAME__CRS,
128 if (ACPI_FAILURE(status)) {
129 printk(KERN_ERR "%s: "
130 "acpi_get_vendor_resource() failed (0x%x) for: ",
131 __FUNCTION__, status);
132 acpi_ns_print_node_pathname(handle, NULL);
136 resource = buffer.pointer;
137 vendor = &resource->data.vendor_typed;
139 if ((vendor->byte_length - sizeof(struct acpi_vendor_uuid)) !=
140 sizeof(struct pcibus_bussoft *)) {
142 "%s: Invalid vendor data length %d\n",
143 __FUNCTION__, vendor->byte_length);
144 kfree(buffer.pointer);
147 memcpy(&addr, vendor->byte_data, sizeof(struct pcibus_bussoft *));
148 prom_bussoft_ptr = __va((struct pcibus_bussoft *) addr);
149 kfree(buffer.pointer);
151 return prom_bussoft_ptr;
155 * sn_extract_device_info - Extract the pcidev_info and the sn_irq_info
156 * pointers from the vendor resource using the
157 * provided acpi handle, and copy the structures
158 * into the argument buffers.
161 sn_extract_device_info(acpi_handle handle, struct pcidev_info **pcidev_info,
162 struct sn_irq_info **sn_irq_info)
165 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
166 struct sn_irq_info *irq_info, *irq_info_prom;
167 struct pcidev_info *pcidev_ptr, *pcidev_prom_ptr;
168 struct acpi_resource *resource;
171 struct acpi_resource_vendor_typed *vendor;
174 * The pointer to this device's pcidev_info structure in
175 * the PROM, is in the vendor resource.
177 status = acpi_get_vendor_resource(handle, METHOD_NAME__CRS,
179 if (ACPI_FAILURE(status)) {
181 "%s: acpi_get_vendor_resource() failed (0x%x) for: ",
182 __FUNCTION__, status);
183 acpi_ns_print_node_pathname(handle, NULL);
188 resource = buffer.pointer;
189 vendor = &resource->data.vendor_typed;
190 if ((vendor->byte_length - sizeof(struct acpi_vendor_uuid)) !=
191 sizeof(struct pci_devdev_info *)) {
193 "%s: Invalid vendor data length: %d for: ",
194 __FUNCTION__, vendor->byte_length);
195 acpi_ns_print_node_pathname(handle, NULL);
201 pcidev_ptr = kzalloc(sizeof(struct pcidev_info), GFP_KERNEL);
203 panic("%s: Unable to alloc memory for pcidev_info", __FUNCTION__);
205 memcpy(&addr, vendor->byte_data, sizeof(struct pcidev_info *));
206 pcidev_prom_ptr = __va(addr);
207 memcpy(pcidev_ptr, pcidev_prom_ptr, sizeof(struct pcidev_info));
209 /* Get the IRQ info */
210 irq_info = kzalloc(sizeof(struct sn_irq_info), GFP_KERNEL);
212 panic("%s: Unable to alloc memory for sn_irq_info", __FUNCTION__);
214 if (pcidev_ptr->pdi_sn_irq_info) {
215 irq_info_prom = __va(pcidev_ptr->pdi_sn_irq_info);
216 memcpy(irq_info, irq_info_prom, sizeof(struct sn_irq_info));
219 *pcidev_info = pcidev_ptr;
220 *sn_irq_info = irq_info;
223 kfree(buffer.pointer);
228 get_host_devfn(acpi_handle device_handle, acpi_handle rootbus_handle)
239 * Do an upward search to find the root bus device, and
240 * obtain the host devfn from the previous child device.
242 child = device_handle;
244 status = acpi_get_parent(child, &parent);
245 if (ACPI_FAILURE(status)) {
246 printk(KERN_ERR "%s: acpi_get_parent() failed "
247 "(0x%x) for: ", __FUNCTION__, status);
248 acpi_ns_print_node_pathname(child, NULL);
250 panic("%s: Unable to find host devfn\n", __FUNCTION__);
252 if (parent == rootbus_handle)
257 printk(KERN_ERR "%s: Unable to find root bus for: ",
259 acpi_ns_print_node_pathname(device_handle, NULL);
264 status = acpi_evaluate_integer(child, METHOD_NAME__ADR, NULL, &adr);
265 if (ACPI_FAILURE(status)) {
266 printk(KERN_ERR "%s: Unable to get _ADR (0x%x) for: ",
267 __FUNCTION__, status);
268 acpi_ns_print_node_pathname(child, NULL);
270 panic("%s: Unable to find host devfn\n", __FUNCTION__);
273 slot = (adr >> 16) & 0xffff;
274 function = adr & 0xffff;
275 devfn = PCI_DEVFN(slot, function);
280 * find_matching_device - Callback routine to find the ACPI device
281 * that matches up with our pci_dev device.
282 * Matching is done on bus number and devfn.
283 * To find the bus number for a particular
284 * ACPI device, we must look at the _BBN method
288 find_matching_device(acpi_handle handle, u32 lvl, void *context, void **rv)
290 unsigned long bbn = -1;
292 acpi_handle parent = NULL;
297 struct sn_pcidev_match *info = context;
299 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
301 if (ACPI_SUCCESS(status)) {
302 status = acpi_get_parent(handle, &parent);
303 if (ACPI_FAILURE(status)) {
305 "%s: acpi_get_parent() failed (0x%x) for: ",
306 __FUNCTION__, status);
307 acpi_ns_print_node_pathname(handle, NULL);
311 status = acpi_evaluate_integer(parent, METHOD_NAME__BBN,
313 if (ACPI_FAILURE(status)) {
315 "%s: Failed to find _BBN in parent of: ",
317 acpi_ns_print_node_pathname(handle, NULL);
322 slot = (adr >> 16) & 0xffff;
323 function = adr & 0xffff;
324 devfn = PCI_DEVFN(slot, function);
325 if ((info->devfn == devfn) && (info->bus == bbn)) {
326 /* We have a match! */
327 info->handle = handle;
335 * sn_acpi_get_pcidev_info - Search ACPI namespace for the acpi
336 * device matching the specified pci_dev,
337 * and return the pcidev info and irq info.
340 sn_acpi_get_pcidev_info(struct pci_dev *dev, struct pcidev_info **pcidev_info,
341 struct sn_irq_info **sn_irq_info)
343 unsigned int host_devfn;
344 struct sn_pcidev_match pcidev_match;
345 acpi_handle rootbus_handle;
346 unsigned long segment;
349 rootbus_handle = PCI_CONTROLLER(dev)->acpi_handle;
350 status = acpi_evaluate_integer(rootbus_handle, METHOD_NAME__SEG, NULL,
352 if (ACPI_SUCCESS(status)) {
353 if (segment != pci_domain_nr(dev)) {
355 "%s: Segment number mismatch, 0x%lx vs 0x%x for: ",
356 __FUNCTION__, segment, pci_domain_nr(dev));
357 acpi_ns_print_node_pathname(rootbus_handle, NULL);
362 printk(KERN_ERR "%s: Unable to get __SEG from: ",
364 acpi_ns_print_node_pathname(rootbus_handle, NULL);
370 * We want to search all devices in this segment/domain
371 * of the ACPI namespace for the matching ACPI device,
372 * which holds the pcidev_info pointer in its vendor resource.
374 pcidev_match.bus = dev->bus->number;
375 pcidev_match.devfn = dev->devfn;
376 pcidev_match.handle = NULL;
378 acpi_walk_namespace(ACPI_TYPE_DEVICE, rootbus_handle, ACPI_UINT32_MAX,
379 find_matching_device, &pcidev_match, NULL);
381 if (!pcidev_match.handle) {
383 "%s: Could not find matching ACPI device for %s.\n",
384 __FUNCTION__, pci_name(dev));
388 if (sn_extract_device_info(pcidev_match.handle, pcidev_info, sn_irq_info))
391 /* Build up the pcidev_info.pdi_slot_host_handle */
392 host_devfn = get_host_devfn(pcidev_match.handle, rootbus_handle);
393 (*pcidev_info)->pdi_slot_host_handle =
394 ((unsigned long) pci_domain_nr(dev) << 40) |
401 * sn_acpi_slot_fixup - Obtain the pcidev_info and sn_irq_info.
402 * Perform any SN specific slot fixup.
403 * At present there does not appear to be
404 * any generic way to handle a ROM image
405 * that has been shadowed by the PROM, so
406 * we pass a pointer to it within the
407 * pcidev_info structure.
411 sn_acpi_slot_fixup(struct pci_dev *dev)
414 struct pcidev_info *pcidev_info = NULL;
415 struct sn_irq_info *sn_irq_info = NULL;
418 if (sn_acpi_get_pcidev_info(dev, &pcidev_info, &sn_irq_info)) {
419 panic("%s: Failure obtaining pcidev_info for %s\n",
420 __FUNCTION__, pci_name(dev));
423 if (pcidev_info->pdi_pio_mapped_addr[PCI_ROM_RESOURCE]) {
425 * A valid ROM image exists and has been shadowed by the
426 * PROM. Setup the pci_dev ROM resource to point to
429 size = dev->resource[PCI_ROM_RESOURCE].end -
430 dev->resource[PCI_ROM_RESOURCE].start;
432 ioremap(pcidev_info->pdi_pio_mapped_addr[PCI_ROM_RESOURCE],
434 dev->resource[PCI_ROM_RESOURCE].start = (unsigned long) addr;
435 dev->resource[PCI_ROM_RESOURCE].end =
436 (unsigned long) addr + size;
437 dev->resource[PCI_ROM_RESOURCE].flags |= IORESOURCE_ROM_BIOS_COPY;
439 sn_pci_fixup_slot(dev, pcidev_info, sn_irq_info);
442 EXPORT_SYMBOL(sn_acpi_slot_fixup);
444 static struct acpi_driver acpi_sn_hubdev_driver = {
445 .name = "SGI HUBDEV Driver",
446 .ids = "SGIHUB,SGITIO",
448 .add = sn_hubdev_add,
454 * sn_acpi_bus_fixup - Perform SN specific setup of software structs
455 * (pcibus_bussoft, pcidev_info) and hardware
456 * registers, for the specified bus and devices under it.
459 sn_acpi_bus_fixup(struct pci_bus *bus)
461 struct pci_dev *pci_dev = NULL;
462 struct pcibus_bussoft *prom_bussoft_ptr;
464 if (!bus->parent) { /* If root bus */
465 prom_bussoft_ptr = sn_get_bussoft_ptr(bus);
466 if (prom_bussoft_ptr == NULL) {
468 "%s: 0x%04x:0x%02x Unable to "
469 "obtain prom_bussoft_ptr\n",
470 __FUNCTION__, pci_domain_nr(bus), bus->number);
473 sn_common_bus_fixup(bus, prom_bussoft_ptr);
475 list_for_each_entry(pci_dev, &bus->devices, bus_list) {
476 sn_acpi_slot_fixup(pci_dev);
481 * sn_io_acpi_init - PROM has ACPI support for IO, defining at a minimum the
482 * nodes and root buses in the DSDT. As a result, bus scanning
483 * will be initiated by the Linux ACPI code.
487 sn_io_acpi_init(void)
492 /* SN Altix does not follow the IOSAPIC IRQ routing model */
493 acpi_irq_model = ACPI_IRQ_MODEL_PLATFORM;
495 acpi_bus_register_driver(&acpi_sn_hubdev_driver);
496 status = sal_ioif_init(&result);
497 if (status || result)
498 panic("sal_ioif_init failed: [%lx] %s\n",
499 status, ia64_sal_strerror(status));