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) 1992 - 1997, 2000-2005 Silicon Graphics, Inc. All rights reserved.
9 #include <linux/bootmem.h>
10 #include <linux/nodemask.h>
11 #include <asm/sn/types.h>
12 #include <asm/sn/addrs.h>
13 #include <asm/sn/sn_feature_sets.h>
14 #include <asm/sn/geo.h>
15 #include <asm/sn/io.h>
16 #include <asm/sn/l1.h>
17 #include <asm/sn/module.h>
18 #include <asm/sn/pcibr_provider.h>
19 #include <asm/sn/pcibus_provider_defs.h>
20 #include <asm/sn/pcidev.h>
21 #include <asm/sn/simulator.h>
22 #include <asm/sn/sn_sal.h>
23 #include <asm/sn/tioca_provider.h>
24 #include <asm/sn/tioce_provider.h>
25 #include "xtalk/hubdev.h"
26 #include "xtalk/xwidgetdev.h"
29 extern void sn_init_cpei_timer(void);
30 extern void register_sn_procfs(void);
32 static struct list_head sn_sysdata_list;
34 /* sysdata list struct */
36 struct list_head entry;
41 struct hubdev_info hubdev;
45 moduleid_t id; /* Module ID of this module */
46 struct slab_info slab_info[MAX_SLABS + 1];
49 int sn_ioif_inited; /* SN I/O infrastructure initialized? */
51 struct sn_pcibus_provider *sn_pci_provider[PCIIO_ASIC_MAX_TYPES]; /* indexed by asic type */
53 static int max_segment_number; /* Default highest segment number */
54 static int max_pcibus_number = 255; /* Default highest pci bus number */
57 * Hooks and struct for unsupported pci providers
61 sn_default_pci_map(struct pci_dev *pdev, unsigned long paddr, size_t size)
67 sn_default_pci_unmap(struct pci_dev *pdev, dma_addr_t addr, int direction)
73 sn_default_pci_bus_fixup(struct pcibus_bussoft *soft, struct pci_controller *controller)
78 static struct sn_pcibus_provider sn_pci_default_provider = {
79 .dma_map = sn_default_pci_map,
80 .dma_map_consistent = sn_default_pci_map,
81 .dma_unmap = sn_default_pci_unmap,
82 .bus_fixup = sn_default_pci_bus_fixup,
86 * Retrieve the DMA Flush List given nasid, widget, and device.
87 * This list is needed to implement the WAR - Flush DMA data on PIO Reads.
90 sal_get_device_dmaflush_list(u64 nasid, u64 widget_num, u64 device_num,
93 struct ia64_sal_retval ret_stuff;
97 SAL_CALL_NOLOCK(ret_stuff,
98 (u64) SN_SAL_IOIF_GET_DEVICE_DMAFLUSH_LIST,
99 (u64) nasid, (u64) widget_num,
100 (u64) device_num, (u64) address, 0, 0, 0);
101 return ret_stuff.status;
105 * Retrieve the hub device info structure for the given nasid.
107 static inline u64 sal_get_hubdev_info(u64 handle, u64 address)
109 struct ia64_sal_retval ret_stuff;
110 ret_stuff.status = 0;
113 SAL_CALL_NOLOCK(ret_stuff,
114 (u64) SN_SAL_IOIF_GET_HUBDEV_INFO,
115 (u64) handle, (u64) address, 0, 0, 0, 0, 0);
120 * Retrieve the pci bus information given the bus number.
122 static inline u64 sal_get_pcibus_info(u64 segment, u64 busnum, u64 address)
124 struct ia64_sal_retval ret_stuff;
125 ret_stuff.status = 0;
128 SAL_CALL_NOLOCK(ret_stuff,
129 (u64) SN_SAL_IOIF_GET_PCIBUS_INFO,
130 (u64) segment, (u64) busnum, (u64) address, 0, 0, 0, 0);
135 * Retrieve the pci device information given the bus and device|function number.
138 sal_get_pcidev_info(u64 segment, u64 bus_number, u64 devfn, u64 pci_dev,
141 struct ia64_sal_retval ret_stuff;
142 ret_stuff.status = 0;
145 SAL_CALL_NOLOCK(ret_stuff,
146 (u64) SN_SAL_IOIF_GET_PCIDEV_INFO,
147 (u64) segment, (u64) bus_number, (u64) devfn,
154 * sn_pcidev_info_get() - Retrieve the pcidev_info struct for the specified
157 inline struct pcidev_info *
158 sn_pcidev_info_get(struct pci_dev *dev)
160 struct pcidev_info *pcidev;
162 list_for_each_entry(pcidev,
163 &(SN_PCI_CONTROLLER(dev)->pcidev_info), pdi_list) {
164 if (pcidev->pdi_linux_pcidev == dev) {
171 /* Older PROM flush WAR
173 * 01/16/06 -- This war will be in place until a new official PROM is released.
174 * Additionally note that the struct sn_flush_device_war also has to be
175 * removed from arch/ia64/sn/include/xtalk/hubdev.h
177 static u8 war_implemented = 0;
179 static s64 sn_device_fixup_war(u64 nasid, u64 widget, int device,
180 struct sn_flush_device_common *common)
182 struct sn_flush_device_war *war_list;
183 struct sn_flush_device_war *dev_entry;
184 struct ia64_sal_retval isrv = {0,0,0,0};
186 if (!war_implemented) {
187 printk(KERN_WARNING "PROM version < 4.50 -- implementing old "
192 war_list = kzalloc(DEV_PER_WIDGET * sizeof(*war_list), GFP_KERNEL);
196 SAL_CALL_NOLOCK(isrv, SN_SAL_IOIF_GET_WIDGET_DMAFLUSH_LIST,
197 nasid, widget, __pa(war_list), 0, 0, 0 ,0);
199 panic("sn_device_fixup_war failed: %s\n",
200 ia64_sal_strerror(isrv.status));
202 dev_entry = war_list + device;
203 memcpy(common,dev_entry, sizeof(*common));
210 * sn_fixup_ionodes() - This routine initializes the HUB data strcuture for
211 * each node in the system.
213 static void __init sn_fixup_ionodes(void)
215 struct sn_flush_device_kernel *sn_flush_device_kernel;
216 struct sn_flush_device_kernel *dev_entry;
217 struct hubdev_info *hubdev;
220 int i, widget, device, size;
223 * Get SGI Specific HUB chipset information.
224 * Inform Prom that this kernel can support domain bus numbering.
226 for (i = 0; i < num_cnodes; i++) {
227 hubdev = (struct hubdev_info *)(NODEPDA(i)->pdinfo);
228 nasid = cnodeid_to_nasid(i);
229 hubdev->max_segment_number = 0xffffffff;
230 hubdev->max_pcibus_number = 0xff;
231 status = sal_get_hubdev_info(nasid, (u64) __pa(hubdev));
235 /* Save the largest Domain and pcibus numbers found. */
236 if (hubdev->max_segment_number) {
238 * Dealing with a Prom that supports segments.
240 max_segment_number = hubdev->max_segment_number;
241 max_pcibus_number = hubdev->max_pcibus_number;
244 /* Attach the error interrupt handlers */
246 ice_error_init(hubdev);
248 hub_error_init(hubdev);
250 for (widget = 0; widget <= HUB_WIDGET_ID_MAX; widget++)
251 hubdev->hdi_xwidget_info[widget].xwi_hubinfo = hubdev;
253 if (!hubdev->hdi_flush_nasid_list.widget_p)
256 size = (HUB_WIDGET_ID_MAX + 1) *
257 sizeof(struct sn_flush_device_kernel *);
258 hubdev->hdi_flush_nasid_list.widget_p =
259 kzalloc(size, GFP_KERNEL);
260 if (!hubdev->hdi_flush_nasid_list.widget_p)
263 for (widget = 0; widget <= HUB_WIDGET_ID_MAX; widget++) {
264 size = DEV_PER_WIDGET *
265 sizeof(struct sn_flush_device_kernel);
266 sn_flush_device_kernel = kzalloc(size, GFP_KERNEL);
267 if (!sn_flush_device_kernel)
270 dev_entry = sn_flush_device_kernel;
271 for (device = 0; device < DEV_PER_WIDGET;
272 device++,dev_entry++) {
273 size = sizeof(struct sn_flush_device_common);
274 dev_entry->common = kzalloc(size, GFP_KERNEL);
275 if (!dev_entry->common)
278 if (sn_prom_feature_available(
279 PRF_DEVICE_FLUSH_LIST))
280 status = sal_get_device_dmaflush_list(
281 nasid, widget, device,
282 (u64)(dev_entry->common));
284 status = sn_device_fixup_war(nasid,
287 if (status != SALRET_OK)
288 panic("SAL call failed: %s\n",
289 ia64_sal_strerror(status));
291 spin_lock_init(&dev_entry->sfdl_flush_lock);
294 if (sn_flush_device_kernel)
295 hubdev->hdi_flush_nasid_list.widget_p[widget] =
296 sn_flush_device_kernel;
302 * sn_pci_window_fixup() - Create a pci_window for each device resource.
303 * Until ACPI support is added, we need this code
304 * to setup pci_windows for use by
305 * pcibios_bus_to_resource(),
306 * pcibios_resource_to_bus(), etc.
309 sn_pci_window_fixup(struct pci_dev *dev, unsigned int count,
312 struct pci_controller *controller = PCI_CONTROLLER(dev->bus);
315 unsigned int new_count;
316 struct pci_window *new_window;
320 idx = controller->windows;
321 new_count = controller->windows + count;
322 new_window = kcalloc(new_count, sizeof(struct pci_window), GFP_KERNEL);
323 if (new_window == NULL)
325 if (controller->window) {
326 memcpy(new_window, controller->window,
327 sizeof(struct pci_window) * controller->windows);
328 kfree(controller->window);
331 /* Setup a pci_window for each device resource. */
332 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
333 if (pci_addrs[i] == -1)
336 new_window[idx].offset = dev->resource[i].start - pci_addrs[i];
337 new_window[idx].resource = dev->resource[i];
341 controller->windows = new_count;
342 controller->window = new_window;
345 void sn_pci_unfixup_slot(struct pci_dev *dev)
347 struct pci_dev *host_pci_dev = SN_PCIDEV_INFO(dev)->host_pci_dev;
350 pci_dev_put(host_pci_dev);
355 * sn_pci_fixup_slot() - This routine sets up a slot's resources
356 * consistent with the Linux PCI abstraction layer. Resources acquired
357 * from our PCI provider include PIO maps to BAR space and interrupt
360 void sn_pci_fixup_slot(struct pci_dev *dev)
362 unsigned int count = 0;
364 int segment = pci_domain_nr(dev->bus);
366 struct pcibus_bussoft *bs;
367 struct pci_bus *host_pci_bus;
368 struct pci_dev *host_pci_dev;
369 struct pcidev_info *pcidev_info;
370 s64 pci_addrs[PCI_ROM_RESOURCE + 1];
371 struct sn_irq_info *sn_irq_info;
373 unsigned int bus_no, devfn;
375 pci_dev_get(dev); /* for the sysdata pointer */
376 pcidev_info = kzalloc(sizeof(struct pcidev_info), GFP_KERNEL);
378 BUG(); /* Cannot afford to run out of memory */
380 sn_irq_info = kzalloc(sizeof(struct sn_irq_info), GFP_KERNEL);
382 BUG(); /* Cannot afford to run out of memory */
384 /* Call to retrieve pci device information needed by kernel. */
385 status = sal_get_pcidev_info((u64) segment, (u64) dev->bus->number,
387 (u64) __pa(pcidev_info),
388 (u64) __pa(sn_irq_info));
390 BUG(); /* Cannot get platform pci device information */
392 /* Add pcidev_info to list in sn_pci_controller struct */
393 list_add_tail(&pcidev_info->pdi_list,
394 &(SN_PCI_CONTROLLER(dev->bus)->pcidev_info));
396 /* Copy over PIO Mapped Addresses */
397 for (idx = 0; idx <= PCI_ROM_RESOURCE; idx++) {
398 unsigned long start, end, addr;
400 if (!pcidev_info->pdi_pio_mapped_addr[idx]) {
405 start = dev->resource[idx].start;
406 end = dev->resource[idx].end;
412 pci_addrs[idx] = start;
414 addr = pcidev_info->pdi_pio_mapped_addr[idx];
415 addr = ((addr << 4) >> 4) | __IA64_UNCACHED_OFFSET;
416 dev->resource[idx].start = addr;
417 dev->resource[idx].end = addr + size;
418 if (dev->resource[idx].flags & IORESOURCE_IO)
419 dev->resource[idx].parent = &ioport_resource;
421 dev->resource[idx].parent = &iomem_resource;
423 /* Create a pci_window in the pci_controller struct for
424 * each device resource.
427 sn_pci_window_fixup(dev, count, pci_addrs);
430 * Using the PROMs values for the PCI host bus, get the Linux
431 * PCI host_pci_dev struct and set up host bus linkages
434 bus_no = (pcidev_info->pdi_slot_host_handle >> 32) & 0xff;
435 devfn = pcidev_info->pdi_slot_host_handle & 0xffffffff;
436 host_pci_bus = pci_find_bus(segment, bus_no);
437 host_pci_dev = pci_get_slot(host_pci_bus, devfn);
439 pcidev_info->host_pci_dev = host_pci_dev;
440 pcidev_info->pdi_linux_pcidev = dev;
441 pcidev_info->pdi_host_pcidev_info = SN_PCIDEV_INFO(host_pci_dev);
442 bs = SN_PCIBUS_BUSSOFT(dev->bus);
443 pcidev_info->pdi_pcibus_info = bs;
445 if (bs && bs->bs_asic_type < PCIIO_ASIC_MAX_TYPES) {
446 SN_PCIDEV_BUSPROVIDER(dev) = sn_pci_provider[bs->bs_asic_type];
448 SN_PCIDEV_BUSPROVIDER(dev) = &sn_pci_default_provider;
451 /* Only set up IRQ stuff if this device has a host bus context */
452 if (bs && sn_irq_info->irq_irq) {
453 pcidev_info->pdi_sn_irq_info = sn_irq_info;
454 dev->irq = pcidev_info->pdi_sn_irq_info->irq_irq;
455 sn_irq_fixup(dev, sn_irq_info);
457 pcidev_info->pdi_sn_irq_info = NULL;
462 * MSI currently not supported on altix. Remove this when
463 * the MSI abstraction patches are integrated into the kernel
464 * (sometime after 2.6.16 releases)
470 * sn_pci_controller_fixup() - This routine sets up a bus's resources
471 * consistent with the Linux PCI abstraction layer.
473 void sn_pci_controller_fixup(int segment, int busnum, struct pci_bus *bus)
477 struct pci_controller *controller;
478 struct sn_pci_controller *sn_controller;
479 struct pcibus_bussoft *prom_bussoft_ptr;
480 struct hubdev_info *hubdev_info;
482 struct sn_pcibus_provider *provider;
484 status = sal_get_pcibus_info((u64) segment, (u64) busnum,
485 (u64) ia64_tpa(&prom_bussoft_ptr));
487 return; /*bus # does not exist */
488 prom_bussoft_ptr = __va(prom_bussoft_ptr);
490 /* Allocate a sn_pci_controller, which has a pci_controller struct
491 * as the first member.
493 sn_controller = kzalloc(sizeof(struct sn_pci_controller), GFP_KERNEL);
496 INIT_LIST_HEAD(&sn_controller->pcidev_info);
497 controller = &sn_controller->pci_controller;
498 controller->segment = segment;
501 bus = pci_scan_bus(busnum, &pci_root_ops, controller);
503 goto error_return; /* error, or bus already scanned */
508 goto error_return; /* sysdata already alloc'd */
511 * Per-provider fixup. Copies the contents from prom to local
512 * area and links SN_PCIBUS_BUSSOFT().
515 if (prom_bussoft_ptr->bs_asic_type >= PCIIO_ASIC_MAX_TYPES)
516 goto error_return; /* unsupported asic type */
518 if (prom_bussoft_ptr->bs_asic_type == PCIIO_ASIC_TYPE_PPB)
519 goto error_return; /* no further fixup necessary */
521 provider = sn_pci_provider[prom_bussoft_ptr->bs_asic_type];
522 if (provider == NULL)
523 goto error_return; /* no provider registerd for this asic */
525 bus->sysdata = controller;
526 if (provider->bus_fixup)
527 provider_soft = (*provider->bus_fixup) (prom_bussoft_ptr, controller);
529 provider_soft = NULL;
531 if (provider_soft == NULL) {
532 /* fixup failed or not applicable */
538 * Setup pci_windows for legacy IO and MEM space.
539 * (Temporary until ACPI support is in place.)
541 controller->window = kcalloc(2, sizeof(struct pci_window), GFP_KERNEL);
542 if (controller->window == NULL)
544 controller->window[0].offset = prom_bussoft_ptr->bs_legacy_io;
545 controller->window[0].resource.name = "legacy_io";
546 controller->window[0].resource.flags = IORESOURCE_IO;
547 controller->window[0].resource.start = prom_bussoft_ptr->bs_legacy_io;
548 controller->window[0].resource.end =
549 controller->window[0].resource.start + 0xffff;
550 controller->window[0].resource.parent = &ioport_resource;
551 controller->window[1].offset = prom_bussoft_ptr->bs_legacy_mem;
552 controller->window[1].resource.name = "legacy_mem";
553 controller->window[1].resource.flags = IORESOURCE_MEM;
554 controller->window[1].resource.start = prom_bussoft_ptr->bs_legacy_mem;
555 controller->window[1].resource.end =
556 controller->window[1].resource.start + (1024 * 1024) - 1;
557 controller->window[1].resource.parent = &iomem_resource;
558 controller->windows = 2;
561 * Generic bus fixup goes here. Don't reference prom_bussoft_ptr
565 PCI_CONTROLLER(bus)->platform_data = provider_soft;
566 nasid = NASID_GET(SN_PCIBUS_BUSSOFT(bus)->bs_base);
567 cnode = nasid_to_cnodeid(nasid);
568 hubdev_info = (struct hubdev_info *)(NODEPDA(cnode)->pdinfo);
569 SN_PCIBUS_BUSSOFT(bus)->bs_xwidget_info =
570 &(hubdev_info->hdi_xwidget_info[SN_PCIBUS_BUSSOFT(bus)->bs_xid]);
573 * If the node information we obtained during the fixup phase is invalid
574 * then set controller->node to -1 (undetermined)
576 if (controller->node >= num_online_nodes()) {
577 struct pcibus_bussoft *b = SN_PCIBUS_BUSSOFT(bus);
579 printk(KERN_WARNING "Device ASIC=%u XID=%u PBUSNUM=%u"
580 "L_IO=%lx L_MEM=%lx BASE=%lx\n",
581 b->bs_asic_type, b->bs_xid, b->bs_persist_busnum,
582 b->bs_legacy_io, b->bs_legacy_mem, b->bs_base);
583 printk(KERN_WARNING "on node %d but only %d nodes online."
584 "Association set to undetermined.\n",
585 controller->node, num_online_nodes());
586 controller->node = -1;
592 kfree(sn_controller);
596 void sn_bus_store_sysdata(struct pci_dev *dev)
598 struct sysdata_el *element;
600 element = kzalloc(sizeof(struct sysdata_el), GFP_KERNEL);
602 dev_dbg(dev, "%s: out of memory!\n", __FUNCTION__);
605 element->sysdata = SN_PCIDEV_INFO(dev);
606 list_add(&element->entry, &sn_sysdata_list);
609 void sn_bus_free_sysdata(void)
611 struct sysdata_el *element;
612 struct list_head *list, *safe;
614 list_for_each_safe(list, safe, &sn_sysdata_list) {
615 element = list_entry(list, struct sysdata_el, entry);
616 list_del(&element->entry);
617 list_del(&(((struct pcidev_info *)
618 (element->sysdata))->pdi_list));
619 kfree(element->sysdata);
626 * Ugly hack to get PCI setup until we have a proper ACPI namespace.
629 #define PCI_BUSES_TO_SCAN 256
631 static int __init sn_pci_init(void)
634 struct pci_dev *pci_dev = NULL;
636 if (!ia64_platform_is("sn2") || IS_RUNNING_ON_FAKE_PROM())
640 * prime sn_pci_provider[]. Individial provider init routines will
641 * override their respective default entries.
644 for (i = 0; i < PCIIO_ASIC_MAX_TYPES; i++)
645 sn_pci_provider[i] = &sn_pci_default_provider;
647 pcibr_init_provider();
648 tioca_init_provider();
649 tioce_init_provider();
652 * This is needed to avoid bounce limit checks in the blk layer
654 ia64_max_iommu_merge_mask = ~PAGE_MASK;
657 INIT_LIST_HEAD(&sn_sysdata_list);
658 sn_init_cpei_timer();
660 #ifdef CONFIG_PROC_FS
661 register_sn_procfs();
664 /* busses are not known yet ... */
665 for (i = 0; i <= max_segment_number; i++)
666 for (j = 0; j <= max_pcibus_number; j++)
667 sn_pci_controller_fixup(i, j, NULL);
670 * Generic Linux PCI Layer has created the pci_bus and pci_dev
671 * structures - time for us to add our SN PLatform specific
676 pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pci_dev)) != NULL)
677 sn_pci_fixup_slot(pci_dev);
679 sn_ioif_inited = 1; /* sn I/O infrastructure now initialized */
685 * hubdev_init_node() - Creates the HUB data structure and link them to it's
686 * own NODE specific data area.
688 void hubdev_init_node(nodepda_t * npda, cnodeid_t node)
690 struct hubdev_info *hubdev_info;
694 size = sizeof(struct hubdev_info);
696 if (node >= num_online_nodes()) /* Headless/memless IO nodes */
699 pg = NODE_DATA(node);
701 hubdev_info = (struct hubdev_info *)alloc_bootmem_node(pg, size);
703 npda->pdinfo = (void *)hubdev_info;
707 cnodeid_get_geoid(cnodeid_t cnode)
709 struct hubdev_info *hubdev;
711 hubdev = (struct hubdev_info *)(NODEPDA(cnode)->pdinfo);
712 return hubdev->hdi_geoid;
715 void sn_generate_path(struct pci_bus *pci_bus, char *address)
723 nasid = NASID_GET(SN_PCIBUS_BUSSOFT(pci_bus)->bs_base);
724 cnode = nasid_to_cnodeid(nasid);
725 geoid = cnodeid_get_geoid(cnode);
726 moduleid = geo_module(geoid);
728 sprintf(address, "module_%c%c%c%c%.2d",
729 '0'+RACK_GET_CLASS(MODULE_GET_RACK(moduleid)),
730 '0'+RACK_GET_GROUP(MODULE_GET_RACK(moduleid)),
731 '0'+RACK_GET_NUM(MODULE_GET_RACK(moduleid)),
732 MODULE_GET_BTCHAR(moduleid), MODULE_GET_BPOS(moduleid));
734 /* Tollhouse requires slot id to be displayed */
735 bricktype = MODULE_GET_BTYPE(moduleid);
736 if ((bricktype == L1_BRICKTYPE_191010) ||
737 (bricktype == L1_BRICKTYPE_1932))
738 sprintf(address, "%s^%d", address, geo_slot(geoid));
741 subsys_initcall(sn_pci_init);
742 EXPORT_SYMBOL(sn_pci_fixup_slot);
743 EXPORT_SYMBOL(sn_pci_unfixup_slot);
744 EXPORT_SYMBOL(sn_pci_controller_fixup);
745 EXPORT_SYMBOL(sn_bus_store_sysdata);
746 EXPORT_SYMBOL(sn_bus_free_sysdata);
747 EXPORT_SYMBOL(sn_generate_path);