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/geo.h>
14 #include <asm/sn/io.h>
15 #include <asm/sn/pcibr_provider.h>
16 #include <asm/sn/pcibus_provider_defs.h>
17 #include <asm/sn/pcidev.h>
18 #include <asm/sn/simulator.h>
19 #include <asm/sn/sn_sal.h>
20 #include <asm/sn/tioca_provider.h>
21 #include <asm/sn/tioce_provider.h>
22 #include "xtalk/hubdev.h"
23 #include "xtalk/xwidgetdev.h"
25 static struct list_head sn_sysdata_list;
27 /* sysdata list struct */
29 struct list_head entry;
34 struct hubdev_info hubdev;
38 moduleid_t id; /* Module ID of this module */
39 struct slab_info slab_info[MAX_SLABS + 1];
42 int sn_ioif_inited = 0; /* SN I/O infrastructure initialized? */
44 struct sn_pcibus_provider *sn_pci_provider[PCIIO_ASIC_MAX_TYPES]; /* indexed by asic type */
46 static int max_segment_number = 0; /* Default highest segment number */
47 static int max_pcibus_number = 255; /* Default highest pci bus number */
50 * Hooks and struct for unsupported pci providers
54 sn_default_pci_map(struct pci_dev *pdev, unsigned long paddr, size_t size)
60 sn_default_pci_unmap(struct pci_dev *pdev, dma_addr_t addr, int direction)
66 sn_default_pci_bus_fixup(struct pcibus_bussoft *soft, struct pci_controller *controller)
71 static struct sn_pcibus_provider sn_pci_default_provider = {
72 .dma_map = sn_default_pci_map,
73 .dma_map_consistent = sn_default_pci_map,
74 .dma_unmap = sn_default_pci_unmap,
75 .bus_fixup = sn_default_pci_bus_fixup,
79 * Retrieve the DMA Flush List given nasid. This list is needed
80 * to implement the WAR - Flush DMA data on PIO Reads.
82 static inline uint64_t
83 sal_get_widget_dmaflush_list(u64 nasid, u64 widget_num, u64 address)
86 struct ia64_sal_retval ret_stuff;
90 SAL_CALL_NOLOCK(ret_stuff,
91 (u64) SN_SAL_IOIF_GET_WIDGET_DMAFLUSH_LIST,
92 (u64) nasid, (u64) widget_num, (u64) address, 0, 0, 0,
99 * Retrieve the hub device info structure for the given nasid.
101 static inline uint64_t sal_get_hubdev_info(u64 handle, u64 address)
104 struct ia64_sal_retval ret_stuff;
105 ret_stuff.status = 0;
108 SAL_CALL_NOLOCK(ret_stuff,
109 (u64) SN_SAL_IOIF_GET_HUBDEV_INFO,
110 (u64) handle, (u64) address, 0, 0, 0, 0, 0);
115 * Retrieve the pci bus information given the bus number.
117 static inline uint64_t sal_get_pcibus_info(u64 segment, u64 busnum, u64 address)
120 struct ia64_sal_retval ret_stuff;
121 ret_stuff.status = 0;
124 SAL_CALL_NOLOCK(ret_stuff,
125 (u64) SN_SAL_IOIF_GET_PCIBUS_INFO,
126 (u64) segment, (u64) busnum, (u64) address, 0, 0, 0, 0);
131 * Retrieve the pci device information given the bus and device|function number.
133 static inline uint64_t
134 sal_get_pcidev_info(u64 segment, u64 bus_number, u64 devfn, u64 pci_dev,
137 struct ia64_sal_retval ret_stuff;
138 ret_stuff.status = 0;
141 SAL_CALL_NOLOCK(ret_stuff,
142 (u64) SN_SAL_IOIF_GET_PCIDEV_INFO,
143 (u64) segment, (u64) bus_number, (u64) devfn,
150 * sn_pcidev_info_get() - Retrieve the pcidev_info struct for the specified
153 inline struct pcidev_info *
154 sn_pcidev_info_get(struct pci_dev *dev)
156 struct pcidev_info *pcidev;
158 list_for_each_entry(pcidev,
159 &(SN_PCI_CONTROLLER(dev)->pcidev_info), pdi_list) {
160 if (pcidev->pdi_linux_pcidev == dev) {
168 * sn_fixup_ionodes() - This routine initializes the HUB data strcuture for
169 * each node in the system.
171 static void sn_fixup_ionodes(void)
174 struct sn_flush_device_list *sn_flush_device_list;
175 struct hubdev_info *hubdev;
181 * Get SGI Specific HUB chipset information.
182 * Inform Prom that this kernel can support domain bus numbering.
184 for (i = 0; i < num_cnodes; i++) {
185 hubdev = (struct hubdev_info *)(NODEPDA(i)->pdinfo);
186 nasid = cnodeid_to_nasid(i);
187 hubdev->max_segment_number = 0xffffffff;
188 hubdev->max_pcibus_number = 0xff;
189 status = sal_get_hubdev_info(nasid, (uint64_t) __pa(hubdev));
193 /* Save the largest Domain and pcibus numbers found. */
194 if (hubdev->max_segment_number) {
196 * Dealing with a Prom that supports segments.
198 max_segment_number = hubdev->max_segment_number;
199 max_pcibus_number = hubdev->max_pcibus_number;
202 /* Attach the error interrupt handlers */
204 ice_error_init(hubdev);
206 hub_error_init(hubdev);
208 for (widget = 0; widget <= HUB_WIDGET_ID_MAX; widget++)
209 hubdev->hdi_xwidget_info[widget].xwi_hubinfo = hubdev;
211 if (!hubdev->hdi_flush_nasid_list.widget_p)
214 hubdev->hdi_flush_nasid_list.widget_p =
215 kmalloc((HUB_WIDGET_ID_MAX + 1) *
216 sizeof(struct sn_flush_device_list *), GFP_KERNEL);
218 memset(hubdev->hdi_flush_nasid_list.widget_p, 0x0,
219 (HUB_WIDGET_ID_MAX + 1) *
220 sizeof(struct sn_flush_device_list *));
222 for (widget = 0; widget <= HUB_WIDGET_ID_MAX; widget++) {
223 sn_flush_device_list = kmalloc(DEV_PER_WIDGET *
225 sn_flush_device_list),
227 memset(sn_flush_device_list, 0x0,
229 sizeof(struct sn_flush_device_list));
232 sal_get_widget_dmaflush_list(nasid, widget,
235 (sn_flush_device_list));
237 kfree(sn_flush_device_list);
241 spin_lock_init(&sn_flush_device_list->sfdl_flush_lock);
242 hubdev->hdi_flush_nasid_list.widget_p[widget] =
243 sn_flush_device_list;
251 * sn_pci_window_fixup() - Create a pci_window for each device resource.
252 * Until ACPI support is added, we need this code
253 * to setup pci_windows for use by
254 * pcibios_bus_to_resource(),
255 * pcibios_resource_to_bus(), etc.
258 sn_pci_window_fixup(struct pci_dev *dev, unsigned int count,
261 struct pci_controller *controller = PCI_CONTROLLER(dev->bus);
264 unsigned int new_count;
265 struct pci_window *new_window;
269 idx = controller->windows;
270 new_count = controller->windows + count;
271 new_window = kcalloc(new_count, sizeof(struct pci_window), GFP_KERNEL);
272 if (new_window == NULL)
274 if (controller->window) {
275 memcpy(new_window, controller->window,
276 sizeof(struct pci_window) * controller->windows);
277 kfree(controller->window);
280 /* Setup a pci_window for each device resource. */
281 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
282 if (pci_addrs[i] == -1)
285 new_window[idx].offset = dev->resource[i].start - pci_addrs[i];
286 new_window[idx].resource = dev->resource[i];
290 controller->windows = new_count;
291 controller->window = new_window;
294 void sn_pci_unfixup_slot(struct pci_dev *dev)
296 struct pci_dev *host_pci_dev = SN_PCIDEV_INFO(dev)->host_pci_dev;
299 pci_dev_put(host_pci_dev);
304 * sn_pci_fixup_slot() - This routine sets up a slot's resources
305 * consistent with the Linux PCI abstraction layer. Resources acquired
306 * from our PCI provider include PIO maps to BAR space and interrupt
309 void sn_pci_fixup_slot(struct pci_dev *dev)
311 unsigned int count = 0;
313 int segment = pci_domain_nr(dev->bus);
315 struct pcibus_bussoft *bs;
316 struct pci_bus *host_pci_bus;
317 struct pci_dev *host_pci_dev;
318 struct pcidev_info *pcidev_info;
319 int64_t pci_addrs[PCI_ROM_RESOURCE + 1];
320 struct sn_irq_info *sn_irq_info;
322 unsigned int bus_no, devfn;
324 pci_dev_get(dev); /* for the sysdata pointer */
325 pcidev_info = kzalloc(sizeof(struct pcidev_info), GFP_KERNEL);
326 if (pcidev_info <= 0)
327 BUG(); /* Cannot afford to run out of memory */
329 sn_irq_info = kmalloc(sizeof(struct sn_irq_info), GFP_KERNEL);
330 if (sn_irq_info <= 0)
331 BUG(); /* Cannot afford to run out of memory */
332 memset(sn_irq_info, 0, sizeof(struct sn_irq_info));
334 /* Call to retrieve pci device information needed by kernel. */
335 status = sal_get_pcidev_info((u64) segment, (u64) dev->bus->number,
337 (u64) __pa(pcidev_info),
338 (u64) __pa(sn_irq_info));
340 BUG(); /* Cannot get platform pci device information */
342 /* Add pcidev_info to list in sn_pci_controller struct */
343 list_add_tail(&pcidev_info->pdi_list,
344 &(SN_PCI_CONTROLLER(dev->bus)->pcidev_info));
346 /* Copy over PIO Mapped Addresses */
347 for (idx = 0; idx <= PCI_ROM_RESOURCE; idx++) {
348 unsigned long start, end, addr;
350 if (!pcidev_info->pdi_pio_mapped_addr[idx]) {
355 start = dev->resource[idx].start;
356 end = dev->resource[idx].end;
362 pci_addrs[idx] = start;
364 addr = pcidev_info->pdi_pio_mapped_addr[idx];
365 addr = ((addr << 4) >> 4) | __IA64_UNCACHED_OFFSET;
366 dev->resource[idx].start = addr;
367 dev->resource[idx].end = addr + size;
368 if (dev->resource[idx].flags & IORESOURCE_IO)
369 dev->resource[idx].parent = &ioport_resource;
371 dev->resource[idx].parent = &iomem_resource;
373 /* Create a pci_window in the pci_controller struct for
374 * each device resource.
377 sn_pci_window_fixup(dev, count, pci_addrs);
380 * Using the PROMs values for the PCI host bus, get the Linux
381 * PCI host_pci_dev struct and set up host bus linkages
384 bus_no = (pcidev_info->pdi_slot_host_handle >> 32) & 0xff;
385 devfn = pcidev_info->pdi_slot_host_handle & 0xffffffff;
386 host_pci_bus = pci_find_bus(segment, bus_no);
387 host_pci_dev = pci_get_slot(host_pci_bus, devfn);
389 pcidev_info->host_pci_dev = host_pci_dev;
390 pcidev_info->pdi_linux_pcidev = dev;
391 pcidev_info->pdi_host_pcidev_info = SN_PCIDEV_INFO(host_pci_dev);
392 bs = SN_PCIBUS_BUSSOFT(dev->bus);
393 pcidev_info->pdi_pcibus_info = bs;
395 if (bs && bs->bs_asic_type < PCIIO_ASIC_MAX_TYPES) {
396 SN_PCIDEV_BUSPROVIDER(dev) = sn_pci_provider[bs->bs_asic_type];
398 SN_PCIDEV_BUSPROVIDER(dev) = &sn_pci_default_provider;
401 /* Only set up IRQ stuff if this device has a host bus context */
402 if (bs && sn_irq_info->irq_irq) {
403 pcidev_info->pdi_sn_irq_info = sn_irq_info;
404 dev->irq = pcidev_info->pdi_sn_irq_info->irq_irq;
405 sn_irq_fixup(dev, sn_irq_info);
407 pcidev_info->pdi_sn_irq_info = NULL;
413 * sn_pci_controller_fixup() - This routine sets up a bus's resources
414 * consistent with the Linux PCI abstraction layer.
416 void sn_pci_controller_fixup(int segment, int busnum, struct pci_bus *bus)
420 struct pci_controller *controller;
421 struct sn_pci_controller *sn_controller;
422 struct pcibus_bussoft *prom_bussoft_ptr;
423 struct hubdev_info *hubdev_info;
424 void *provider_soft = NULL;
425 struct sn_pcibus_provider *provider;
427 status = sal_get_pcibus_info((u64) segment, (u64) busnum,
428 (u64) ia64_tpa(&prom_bussoft_ptr));
430 return; /*bus # does not exist */
431 prom_bussoft_ptr = __va(prom_bussoft_ptr);
433 /* Allocate a sn_pci_controller, which has a pci_controller struct
434 * as the first member.
436 sn_controller = kzalloc(sizeof(struct sn_pci_controller), GFP_KERNEL);
439 INIT_LIST_HEAD(&sn_controller->pcidev_info);
440 controller = &sn_controller->pci_controller;
441 controller->segment = segment;
444 bus = pci_scan_bus(busnum, &pci_root_ops, controller);
446 goto error_return; /* error, or bus already scanned */
451 goto error_return; /* sysdata already alloc'd */
454 * Per-provider fixup. Copies the contents from prom to local
455 * area and links SN_PCIBUS_BUSSOFT().
458 if (prom_bussoft_ptr->bs_asic_type >= PCIIO_ASIC_MAX_TYPES)
459 goto error_return; /* unsupported asic type */
461 if (prom_bussoft_ptr->bs_asic_type == PCIIO_ASIC_TYPE_PPB)
462 goto error_return; /* no further fixup necessary */
464 provider = sn_pci_provider[prom_bussoft_ptr->bs_asic_type];
465 if (provider == NULL)
466 goto error_return; /* no provider registerd for this asic */
468 bus->sysdata = controller;
469 if (provider->bus_fixup)
470 provider_soft = (*provider->bus_fixup) (prom_bussoft_ptr, controller);
472 if (provider_soft == NULL) {
473 /* fixup failed or not applicable */
479 * Setup pci_windows for legacy IO and MEM space.
480 * (Temporary until ACPI support is in place.)
482 controller->window = kcalloc(2, sizeof(struct pci_window), GFP_KERNEL);
483 if (controller->window == NULL)
485 controller->window[0].offset = prom_bussoft_ptr->bs_legacy_io;
486 controller->window[0].resource.name = "legacy_io";
487 controller->window[0].resource.flags = IORESOURCE_IO;
488 controller->window[0].resource.start = prom_bussoft_ptr->bs_legacy_io;
489 controller->window[0].resource.end =
490 controller->window[0].resource.start + 0xffff;
491 controller->window[0].resource.parent = &ioport_resource;
492 controller->window[1].offset = prom_bussoft_ptr->bs_legacy_mem;
493 controller->window[1].resource.name = "legacy_mem";
494 controller->window[1].resource.flags = IORESOURCE_MEM;
495 controller->window[1].resource.start = prom_bussoft_ptr->bs_legacy_mem;
496 controller->window[1].resource.end =
497 controller->window[1].resource.start + (1024 * 1024) - 1;
498 controller->window[1].resource.parent = &iomem_resource;
499 controller->windows = 2;
502 * Generic bus fixup goes here. Don't reference prom_bussoft_ptr
506 PCI_CONTROLLER(bus)->platform_data = provider_soft;
507 nasid = NASID_GET(SN_PCIBUS_BUSSOFT(bus)->bs_base);
508 cnode = nasid_to_cnodeid(nasid);
509 hubdev_info = (struct hubdev_info *)(NODEPDA(cnode)->pdinfo);
510 SN_PCIBUS_BUSSOFT(bus)->bs_xwidget_info =
511 &(hubdev_info->hdi_xwidget_info[SN_PCIBUS_BUSSOFT(bus)->bs_xid]);
514 * If the node information we obtained during the fixup phase is invalid
515 * then set controller->node to -1 (undetermined)
517 if (controller->node >= num_online_nodes()) {
518 struct pcibus_bussoft *b = SN_PCIBUS_BUSSOFT(bus);
520 printk(KERN_WARNING "Device ASIC=%u XID=%u PBUSNUM=%u"
521 "L_IO=%lx L_MEM=%lx BASE=%lx\n",
522 b->bs_asic_type, b->bs_xid, b->bs_persist_busnum,
523 b->bs_legacy_io, b->bs_legacy_mem, b->bs_base);
524 printk(KERN_WARNING "on node %d but only %d nodes online."
525 "Association set to undetermined.\n",
526 controller->node, num_online_nodes());
527 controller->node = -1;
533 kfree(sn_controller);
537 void sn_bus_store_sysdata(struct pci_dev *dev)
539 struct sysdata_el *element;
541 element = kzalloc(sizeof(struct sysdata_el), GFP_KERNEL);
543 dev_dbg(dev, "%s: out of memory!\n", __FUNCTION__);
546 element->sysdata = SN_PCIDEV_INFO(dev);
547 list_add(&element->entry, &sn_sysdata_list);
550 void sn_bus_free_sysdata(void)
552 struct sysdata_el *element;
553 struct list_head *list;
555 sn_sysdata_free_start:
556 list_for_each(list, &sn_sysdata_list) {
557 element = list_entry(list, struct sysdata_el, entry);
558 list_del(&element->entry);
559 kfree(element->sysdata);
561 goto sn_sysdata_free_start;
567 * Ugly hack to get PCI setup until we have a proper ACPI namespace.
570 #define PCI_BUSES_TO_SCAN 256
572 static int __init sn_pci_init(void)
576 struct pci_dev *pci_dev = NULL;
577 extern void sn_init_cpei_timer(void);
578 #ifdef CONFIG_PROC_FS
579 extern void register_sn_procfs(void);
582 if (!ia64_platform_is("sn2") || IS_RUNNING_ON_FAKE_PROM())
586 * prime sn_pci_provider[]. Individial provider init routines will
587 * override their respective default entries.
590 for (i = 0; i < PCIIO_ASIC_MAX_TYPES; i++)
591 sn_pci_provider[i] = &sn_pci_default_provider;
593 pcibr_init_provider();
594 tioca_init_provider();
595 tioce_init_provider();
598 * This is needed to avoid bounce limit checks in the blk layer
600 ia64_max_iommu_merge_mask = ~PAGE_MASK;
603 INIT_LIST_HEAD(&sn_sysdata_list);
604 sn_init_cpei_timer();
606 #ifdef CONFIG_PROC_FS
607 register_sn_procfs();
610 /* busses are not known yet ... */
611 for (i = 0; i <= max_segment_number; i++)
612 for (j = 0; j <= max_pcibus_number; j++)
613 sn_pci_controller_fixup(i, j, NULL);
616 * Generic Linux PCI Layer has created the pci_bus and pci_dev
617 * structures - time for us to add our SN PLatform specific
622 pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pci_dev)) != NULL)
623 sn_pci_fixup_slot(pci_dev);
625 sn_ioif_inited = 1; /* sn I/O infrastructure now initialized */
631 * hubdev_init_node() - Creates the HUB data structure and link them to it's
632 * own NODE specific data area.
634 void hubdev_init_node(nodepda_t * npda, cnodeid_t node)
637 struct hubdev_info *hubdev_info;
639 if (node >= num_online_nodes()) /* Headless/memless IO nodes */
641 (struct hubdev_info *)alloc_bootmem_node(NODE_DATA(0),
646 (struct hubdev_info *)alloc_bootmem_node(NODE_DATA(node),
649 npda->pdinfo = (void *)hubdev_info;
654 cnodeid_get_geoid(cnodeid_t cnode)
657 struct hubdev_info *hubdev;
659 hubdev = (struct hubdev_info *)(NODEPDA(cnode)->pdinfo);
660 return hubdev->hdi_geoid;
664 subsys_initcall(sn_pci_init);
665 EXPORT_SYMBOL(sn_pci_fixup_slot);
666 EXPORT_SYMBOL(sn_pci_unfixup_slot);
667 EXPORT_SYMBOL(sn_pci_controller_fixup);
668 EXPORT_SYMBOL(sn_bus_store_sysdata);
669 EXPORT_SYMBOL(sn_bus_free_sysdata);