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 <linux/slab.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_acpi_hubdev_init() - This function is called by acpi_ns_get_device_callback()
57 * for all SGIHUB and SGITIO acpi devices defined in the
58 * DSDT. It obtains the hubdev_info pointer from the
59 * ACPI vendor resource, which the PROM setup, and sets up the
60 * hubdev_info in the pda.
63 static acpi_status __init
64 sn_acpi_hubdev_init(acpi_handle handle
, u32 depth
, void *context
, void **ret
)
66 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
67 struct acpi_buffer name_buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
69 struct hubdev_info
*hubdev
;
70 struct hubdev_info
*hubdev_ptr
;
73 struct acpi_resource
*resource
;
75 struct acpi_resource_vendor_typed
*vendor
;
76 extern void sn_common_hubdev_init(struct hubdev_info
*);
78 status
= acpi_get_vendor_resource(handle
, METHOD_NAME__CRS
,
80 if (ACPI_FAILURE(status
)) {
81 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &name_buffer
);
83 "sn_acpi_hubdev_init: acpi_get_vendor_resource() "
84 "(0x%x) failed for: %s\n", status
,
85 (char *)name_buffer
.pointer
);
86 kfree(name_buffer
.pointer
);
87 return AE_OK
; /* Continue walking namespace */
90 resource
= buffer
.pointer
;
91 vendor
= &resource
->data
.vendor_typed
;
92 if ((vendor
->byte_length
- sizeof(struct acpi_vendor_uuid
)) !=
93 sizeof(struct hubdev_info
*)) {
94 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &name_buffer
);
96 "sn_acpi_hubdev_init: Invalid vendor data length: "
98 vendor
->byte_length
, (char *)name_buffer
.pointer
);
99 kfree(name_buffer
.pointer
);
103 memcpy(&addr
, vendor
->byte_data
, sizeof(struct hubdev_info
*));
104 hubdev_ptr
= __va((struct hubdev_info
*) addr
);
106 nasid
= hubdev_ptr
->hdi_nasid
;
107 i
= nasid_to_cnodeid(nasid
);
108 hubdev
= (struct hubdev_info
*)(NODEPDA(i
)->pdinfo
);
109 *hubdev
= *hubdev_ptr
;
110 sn_common_hubdev_init(hubdev
);
113 kfree(buffer
.pointer
);
114 return AE_OK
; /* Continue walking namespace */
118 * sn_get_bussoft_ptr() - The pcibus_bussoft pointer is found in
119 * the ACPI Vendor resource for this bus.
121 static struct pcibus_bussoft
*
122 sn_get_bussoft_ptr(struct pci_bus
*bus
)
125 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
126 struct acpi_buffer name_buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
128 struct pcibus_bussoft
*prom_bussoft_ptr
;
129 struct acpi_resource
*resource
;
131 struct acpi_resource_vendor_typed
*vendor
;
134 handle
= PCI_CONTROLLER(bus
)->acpi_handle
;
135 status
= acpi_get_vendor_resource(handle
, METHOD_NAME__CRS
,
137 if (ACPI_FAILURE(status
)) {
138 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &name_buffer
);
139 printk(KERN_ERR
"%s: "
140 "acpi_get_vendor_resource() failed (0x%x) for: %s\n",
141 __func__
, status
, (char *)name_buffer
.pointer
);
142 kfree(name_buffer
.pointer
);
145 resource
= buffer
.pointer
;
146 vendor
= &resource
->data
.vendor_typed
;
148 if ((vendor
->byte_length
- sizeof(struct acpi_vendor_uuid
)) !=
149 sizeof(struct pcibus_bussoft
*)) {
151 "%s: Invalid vendor data length %d\n",
152 __func__
, vendor
->byte_length
);
153 kfree(buffer
.pointer
);
156 memcpy(&addr
, vendor
->byte_data
, sizeof(struct pcibus_bussoft
*));
157 prom_bussoft_ptr
= __va((struct pcibus_bussoft
*) addr
);
158 kfree(buffer
.pointer
);
160 return prom_bussoft_ptr
;
164 * sn_extract_device_info - Extract the pcidev_info and the sn_irq_info
165 * pointers from the vendor resource using the
166 * provided acpi handle, and copy the structures
167 * into the argument buffers.
170 sn_extract_device_info(acpi_handle handle
, struct pcidev_info
**pcidev_info
,
171 struct sn_irq_info
**sn_irq_info
)
174 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
175 struct acpi_buffer name_buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
176 struct sn_irq_info
*irq_info
, *irq_info_prom
;
177 struct pcidev_info
*pcidev_ptr
, *pcidev_prom_ptr
;
178 struct acpi_resource
*resource
;
181 struct acpi_resource_vendor_typed
*vendor
;
184 * The pointer to this device's pcidev_info structure in
185 * the PROM, is in the vendor resource.
187 status
= acpi_get_vendor_resource(handle
, METHOD_NAME__CRS
,
189 if (ACPI_FAILURE(status
)) {
190 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &name_buffer
);
192 "%s: acpi_get_vendor_resource() failed (0x%x) for: %s\n",
193 __func__
, status
, (char *)name_buffer
.pointer
);
194 kfree(name_buffer
.pointer
);
198 resource
= buffer
.pointer
;
199 vendor
= &resource
->data
.vendor_typed
;
200 if ((vendor
->byte_length
- sizeof(struct acpi_vendor_uuid
)) !=
201 sizeof(struct pci_devdev_info
*)) {
202 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &name_buffer
);
204 "%s: Invalid vendor data length: %d for: %s\n",
205 __func__
, vendor
->byte_length
,
206 (char *)name_buffer
.pointer
);
207 kfree(name_buffer
.pointer
);
212 pcidev_ptr
= kzalloc(sizeof(struct pcidev_info
), GFP_KERNEL
);
214 panic("%s: Unable to alloc memory for pcidev_info", __func__
);
216 memcpy(&addr
, vendor
->byte_data
, sizeof(struct pcidev_info
*));
217 pcidev_prom_ptr
= __va(addr
);
218 memcpy(pcidev_ptr
, pcidev_prom_ptr
, sizeof(struct pcidev_info
));
220 /* Get the IRQ info */
221 irq_info
= kzalloc(sizeof(struct sn_irq_info
), GFP_KERNEL
);
223 panic("%s: Unable to alloc memory for sn_irq_info", __func__
);
225 if (pcidev_ptr
->pdi_sn_irq_info
) {
226 irq_info_prom
= __va(pcidev_ptr
->pdi_sn_irq_info
);
227 memcpy(irq_info
, irq_info_prom
, sizeof(struct sn_irq_info
));
230 *pcidev_info
= pcidev_ptr
;
231 *sn_irq_info
= irq_info
;
234 kfree(buffer
.pointer
);
239 get_host_devfn(acpi_handle device_handle
, acpi_handle rootbus_handle
)
241 unsigned long long adr
;
248 struct acpi_buffer name_buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
250 acpi_get_name(device_handle
, ACPI_FULL_PATHNAME
, &name_buffer
);
253 * Do an upward search to find the root bus device, and
254 * obtain the host devfn from the previous child device.
256 child
= device_handle
;
258 status
= acpi_get_parent(child
, &parent
);
259 if (ACPI_FAILURE(status
)) {
260 printk(KERN_ERR
"%s: acpi_get_parent() failed "
261 "(0x%x) for: %s\n", __func__
, status
,
262 (char *)name_buffer
.pointer
);
263 panic("%s: Unable to find host devfn\n", __func__
);
265 if (parent
== rootbus_handle
)
270 printk(KERN_ERR
"%s: Unable to find root bus for: %s\n",
271 __func__
, (char *)name_buffer
.pointer
);
275 status
= acpi_evaluate_integer(child
, METHOD_NAME__ADR
, NULL
, &adr
);
276 if (ACPI_FAILURE(status
)) {
277 printk(KERN_ERR
"%s: Unable to get _ADR (0x%x) for: %s\n",
278 __func__
, status
, (char *)name_buffer
.pointer
);
279 panic("%s: Unable to find host devfn\n", __func__
);
282 kfree(name_buffer
.pointer
);
284 slot
= (adr
>> 16) & 0xffff;
285 function
= adr
& 0xffff;
286 devfn
= PCI_DEVFN(slot
, function
);
291 * find_matching_device - Callback routine to find the ACPI device
292 * that matches up with our pci_dev device.
293 * Matching is done on bus number and devfn.
294 * To find the bus number for a particular
295 * ACPI device, we must look at the _BBN method
299 find_matching_device(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
301 unsigned long long bbn
= -1;
302 unsigned long long adr
;
303 acpi_handle parent
= NULL
;
308 struct sn_pcidev_match
*info
= context
;
309 struct acpi_buffer name_buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
311 status
= acpi_evaluate_integer(handle
, METHOD_NAME__ADR
, NULL
,
313 if (ACPI_SUCCESS(status
)) {
314 status
= acpi_get_parent(handle
, &parent
);
315 if (ACPI_FAILURE(status
)) {
316 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &name_buffer
);
318 "%s: acpi_get_parent() failed (0x%x) for: %s\n",
319 __func__
, status
, (char *)name_buffer
.pointer
);
320 kfree(name_buffer
.pointer
);
323 status
= acpi_evaluate_integer(parent
, METHOD_NAME__BBN
,
325 if (ACPI_FAILURE(status
)) {
326 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &name_buffer
);
328 "%s: Failed to find _BBN in parent of: %s\n",
329 __func__
, (char *)name_buffer
.pointer
);
330 kfree(name_buffer
.pointer
);
334 slot
= (adr
>> 16) & 0xffff;
335 function
= adr
& 0xffff;
336 devfn
= PCI_DEVFN(slot
, function
);
337 if ((info
->devfn
== devfn
) && (info
->bus
== bbn
)) {
338 /* We have a match! */
339 info
->handle
= handle
;
347 * sn_acpi_get_pcidev_info - Search ACPI namespace for the acpi
348 * device matching the specified pci_dev,
349 * and return the pcidev info and irq info.
352 sn_acpi_get_pcidev_info(struct pci_dev
*dev
, struct pcidev_info
**pcidev_info
,
353 struct sn_irq_info
**sn_irq_info
)
355 unsigned int host_devfn
;
356 struct sn_pcidev_match pcidev_match
;
357 acpi_handle rootbus_handle
;
358 unsigned long long segment
;
360 struct acpi_buffer name_buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
362 rootbus_handle
= PCI_CONTROLLER(dev
)->acpi_handle
;
363 status
= acpi_evaluate_integer(rootbus_handle
, METHOD_NAME__SEG
, NULL
,
365 if (ACPI_SUCCESS(status
)) {
366 if (segment
!= pci_domain_nr(dev
)) {
367 acpi_get_name(rootbus_handle
, ACPI_FULL_PATHNAME
,
370 "%s: Segment number mismatch, 0x%llx vs 0x%x for: %s\n",
371 __func__
, segment
, pci_domain_nr(dev
),
372 (char *)name_buffer
.pointer
);
373 kfree(name_buffer
.pointer
);
377 acpi_get_name(rootbus_handle
, ACPI_FULL_PATHNAME
, &name_buffer
);
378 printk(KERN_ERR
"%s: Unable to get __SEG from: %s\n",
379 __func__
, (char *)name_buffer
.pointer
);
380 kfree(name_buffer
.pointer
);
385 * We want to search all devices in this segment/domain
386 * of the ACPI namespace for the matching ACPI device,
387 * which holds the pcidev_info pointer in its vendor resource.
389 pcidev_match
.bus
= dev
->bus
->number
;
390 pcidev_match
.devfn
= dev
->devfn
;
391 pcidev_match
.handle
= NULL
;
393 acpi_walk_namespace(ACPI_TYPE_DEVICE
, rootbus_handle
, ACPI_UINT32_MAX
,
394 find_matching_device
, NULL
, &pcidev_match
, NULL
);
396 if (!pcidev_match
.handle
) {
398 "%s: Could not find matching ACPI device for %s.\n",
399 __func__
, pci_name(dev
));
403 if (sn_extract_device_info(pcidev_match
.handle
, pcidev_info
, sn_irq_info
))
406 /* Build up the pcidev_info.pdi_slot_host_handle */
407 host_devfn
= get_host_devfn(pcidev_match
.handle
, rootbus_handle
);
408 (*pcidev_info
)->pdi_slot_host_handle
=
409 ((unsigned long) pci_domain_nr(dev
) << 40) |
416 * sn_acpi_slot_fixup - Obtain the pcidev_info and sn_irq_info.
417 * Perform any SN specific slot fixup.
418 * At present there does not appear to be
419 * any generic way to handle a ROM image
420 * that has been shadowed by the PROM, so
421 * we pass a pointer to it within the
422 * pcidev_info structure.
426 sn_acpi_slot_fixup(struct pci_dev
*dev
)
429 struct pcidev_info
*pcidev_info
= NULL
;
430 struct sn_irq_info
*sn_irq_info
= NULL
;
431 size_t image_size
, size
;
433 if (sn_acpi_get_pcidev_info(dev
, &pcidev_info
, &sn_irq_info
)) {
434 panic("%s: Failure obtaining pcidev_info for %s\n",
435 __func__
, pci_name(dev
));
438 if (pcidev_info
->pdi_pio_mapped_addr
[PCI_ROM_RESOURCE
]) {
440 * A valid ROM image exists and has been shadowed by the
441 * PROM. Setup the pci_dev ROM resource with the address
442 * of the shadowed copy, and the actual length of the ROM image.
444 size
= pci_resource_len(dev
, PCI_ROM_RESOURCE
);
445 addr
= ioremap(pcidev_info
->pdi_pio_mapped_addr
[PCI_ROM_RESOURCE
],
447 image_size
= pci_get_rom_size(dev
, addr
, size
);
448 dev
->resource
[PCI_ROM_RESOURCE
].start
= (unsigned long) addr
;
449 dev
->resource
[PCI_ROM_RESOURCE
].end
=
450 (unsigned long) addr
+ image_size
- 1;
451 dev
->resource
[PCI_ROM_RESOURCE
].flags
|= IORESOURCE_ROM_BIOS_COPY
;
453 sn_pci_fixup_slot(dev
, pcidev_info
, sn_irq_info
);
456 EXPORT_SYMBOL(sn_acpi_slot_fixup
);
460 * sn_acpi_bus_fixup - Perform SN specific setup of software structs
461 * (pcibus_bussoft, pcidev_info) and hardware
462 * registers, for the specified bus and devices under it.
465 sn_acpi_bus_fixup(struct pci_bus
*bus
)
467 struct pci_dev
*pci_dev
= NULL
;
468 struct pcibus_bussoft
*prom_bussoft_ptr
;
470 if (!bus
->parent
) { /* If root bus */
471 prom_bussoft_ptr
= sn_get_bussoft_ptr(bus
);
472 if (prom_bussoft_ptr
== NULL
) {
474 "%s: 0x%04x:0x%02x Unable to "
475 "obtain prom_bussoft_ptr\n",
476 __func__
, pci_domain_nr(bus
), bus
->number
);
479 sn_common_bus_fixup(bus
, prom_bussoft_ptr
);
481 list_for_each_entry(pci_dev
, &bus
->devices
, bus_list
) {
482 sn_acpi_slot_fixup(pci_dev
);
487 * sn_io_acpi_init - PROM has ACPI support for IO, defining at a minimum the
488 * nodes and root buses in the DSDT. As a result, bus scanning
489 * will be initiated by the Linux ACPI code.
493 sn_io_acpi_init(void)
498 /* SN Altix does not follow the IOSAPIC IRQ routing model */
499 acpi_irq_model
= ACPI_IRQ_MODEL_PLATFORM
;
501 /* Setup hubdev_info for all SGIHUB/SGITIO devices */
502 acpi_get_devices("SGIHUB", sn_acpi_hubdev_init
, NULL
, NULL
);
503 acpi_get_devices("SGITIO", sn_acpi_hubdev_init
, NULL
, NULL
);
505 status
= sal_ioif_init(&result
);
506 if (status
|| result
)
507 panic("sal_ioif_init failed: [%lx] %s\n",
508 status
, ia64_sal_strerror(status
));