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, widget, and device.
80 * This list is needed to implement the WAR - Flush DMA data on PIO Reads.
83 sal_get_device_dmaflush_list(u64 nasid, u64 widget_num, u64 device_num,
87 struct ia64_sal_retval ret_stuff;
91 SAL_CALL_NOLOCK(ret_stuff,
92 (u64) SN_SAL_IOIF_GET_DEVICE_DMAFLUSH_LIST,
93 (u64) nasid, (u64) widget_num,
94 (u64) device_num, (u64) address, 0, 0, 0);
95 return ret_stuff.status;
100 * Retrieve the hub device info structure for the given nasid.
102 static inline u64 sal_get_hubdev_info(u64 handle, u64 address)
105 struct ia64_sal_retval ret_stuff;
106 ret_stuff.status = 0;
109 SAL_CALL_NOLOCK(ret_stuff,
110 (u64) SN_SAL_IOIF_GET_HUBDEV_INFO,
111 (u64) handle, (u64) address, 0, 0, 0, 0, 0);
116 * Retrieve the pci bus information given the bus number.
118 static inline u64 sal_get_pcibus_info(u64 segment, u64 busnum, u64 address)
121 struct ia64_sal_retval ret_stuff;
122 ret_stuff.status = 0;
125 SAL_CALL_NOLOCK(ret_stuff,
126 (u64) SN_SAL_IOIF_GET_PCIBUS_INFO,
127 (u64) segment, (u64) busnum, (u64) address, 0, 0, 0, 0);
132 * Retrieve the pci device information given the bus and device|function number.
135 sal_get_pcidev_info(u64 segment, u64 bus_number, u64 devfn, u64 pci_dev,
138 struct ia64_sal_retval ret_stuff;
139 ret_stuff.status = 0;
142 SAL_CALL_NOLOCK(ret_stuff,
143 (u64) SN_SAL_IOIF_GET_PCIDEV_INFO,
144 (u64) segment, (u64) bus_number, (u64) devfn,
151 * sn_pcidev_info_get() - Retrieve the pcidev_info struct for the specified
154 inline struct pcidev_info *
155 sn_pcidev_info_get(struct pci_dev *dev)
157 struct pcidev_info *pcidev;
159 list_for_each_entry(pcidev,
160 &(SN_PCI_CONTROLLER(dev)->pcidev_info), pdi_list) {
161 if (pcidev->pdi_linux_pcidev == dev) {
169 * sn_fixup_ionodes() - This routine initializes the HUB data strcuture for
170 * each node in the system.
172 static void sn_fixup_ionodes(void)
174 struct sn_flush_device_kernel *sn_flush_device_kernel;
175 struct sn_flush_device_kernel *dev_entry;
176 struct hubdev_info *hubdev;
179 int i, widget, device;
182 * Get SGI Specific HUB chipset information.
183 * Inform Prom that this kernel can support domain bus numbering.
185 for (i = 0; i < num_cnodes; i++) {
186 hubdev = (struct hubdev_info *)(NODEPDA(i)->pdinfo);
187 nasid = cnodeid_to_nasid(i);
188 hubdev->max_segment_number = 0xffffffff;
189 hubdev->max_pcibus_number = 0xff;
190 status = sal_get_hubdev_info(nasid, (u64) __pa(hubdev));
194 /* Save the largest Domain and pcibus numbers found. */
195 if (hubdev->max_segment_number) {
197 * Dealing with a Prom that supports segments.
199 max_segment_number = hubdev->max_segment_number;
200 max_pcibus_number = hubdev->max_pcibus_number;
203 /* Attach the error interrupt handlers */
205 ice_error_init(hubdev);
207 hub_error_init(hubdev);
209 for (widget = 0; widget <= HUB_WIDGET_ID_MAX; widget++)
210 hubdev->hdi_xwidget_info[widget].xwi_hubinfo = hubdev;
212 if (!hubdev->hdi_flush_nasid_list.widget_p)
215 hubdev->hdi_flush_nasid_list.widget_p =
216 kmalloc((HUB_WIDGET_ID_MAX + 1) *
217 sizeof(struct sn_flush_device_kernel *),
219 memset(hubdev->hdi_flush_nasid_list.widget_p, 0x0,
220 (HUB_WIDGET_ID_MAX + 1) *
221 sizeof(struct sn_flush_device_kernel *));
223 for (widget = 0; widget <= HUB_WIDGET_ID_MAX; widget++) {
224 sn_flush_device_kernel = kmalloc(DEV_PER_WIDGET *
226 sn_flush_device_kernel),
228 if (!sn_flush_device_kernel)
230 memset(sn_flush_device_kernel, 0x0,
232 sizeof(struct sn_flush_device_kernel));
234 dev_entry = sn_flush_device_kernel;
235 for (device = 0; device < DEV_PER_WIDGET;
236 device++,dev_entry++) {
237 dev_entry->common = kmalloc(sizeof(struct
238 sn_flush_device_common),
240 if (!dev_entry->common)
242 memset(dev_entry->common, 0x0, sizeof(struct
243 sn_flush_device_common));
245 status = sal_get_device_dmaflush_list(nasid,
248 (u64)(dev_entry->common));
252 spin_lock_init(&dev_entry->sfdl_flush_lock);
255 if (sn_flush_device_kernel)
256 hubdev->hdi_flush_nasid_list.widget_p[widget] =
257 sn_flush_device_kernel;
263 * sn_pci_window_fixup() - Create a pci_window for each device resource.
264 * Until ACPI support is added, we need this code
265 * to setup pci_windows for use by
266 * pcibios_bus_to_resource(),
267 * pcibios_resource_to_bus(), etc.
270 sn_pci_window_fixup(struct pci_dev *dev, unsigned int count,
273 struct pci_controller *controller = PCI_CONTROLLER(dev->bus);
276 unsigned int new_count;
277 struct pci_window *new_window;
281 idx = controller->windows;
282 new_count = controller->windows + count;
283 new_window = kcalloc(new_count, sizeof(struct pci_window), GFP_KERNEL);
284 if (new_window == NULL)
286 if (controller->window) {
287 memcpy(new_window, controller->window,
288 sizeof(struct pci_window) * controller->windows);
289 kfree(controller->window);
292 /* Setup a pci_window for each device resource. */
293 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
294 if (pci_addrs[i] == -1)
297 new_window[idx].offset = dev->resource[i].start - pci_addrs[i];
298 new_window[idx].resource = dev->resource[i];
302 controller->windows = new_count;
303 controller->window = new_window;
306 void sn_pci_unfixup_slot(struct pci_dev *dev)
308 struct pci_dev *host_pci_dev = SN_PCIDEV_INFO(dev)->host_pci_dev;
311 pci_dev_put(host_pci_dev);
316 * sn_pci_fixup_slot() - This routine sets up a slot's resources
317 * consistent with the Linux PCI abstraction layer. Resources acquired
318 * from our PCI provider include PIO maps to BAR space and interrupt
321 void sn_pci_fixup_slot(struct pci_dev *dev)
323 unsigned int count = 0;
325 int segment = pci_domain_nr(dev->bus);
327 struct pcibus_bussoft *bs;
328 struct pci_bus *host_pci_bus;
329 struct pci_dev *host_pci_dev;
330 struct pcidev_info *pcidev_info;
331 s64 pci_addrs[PCI_ROM_RESOURCE + 1];
332 struct sn_irq_info *sn_irq_info;
334 unsigned int bus_no, devfn;
336 pci_dev_get(dev); /* for the sysdata pointer */
337 pcidev_info = kzalloc(sizeof(struct pcidev_info), GFP_KERNEL);
338 if (pcidev_info <= 0)
339 BUG(); /* Cannot afford to run out of memory */
341 sn_irq_info = kmalloc(sizeof(struct sn_irq_info), GFP_KERNEL);
342 if (sn_irq_info <= 0)
343 BUG(); /* Cannot afford to run out of memory */
344 memset(sn_irq_info, 0, sizeof(struct sn_irq_info));
346 /* Call to retrieve pci device information needed by kernel. */
347 status = sal_get_pcidev_info((u64) segment, (u64) dev->bus->number,
349 (u64) __pa(pcidev_info),
350 (u64) __pa(sn_irq_info));
352 BUG(); /* Cannot get platform pci device information */
354 /* Add pcidev_info to list in sn_pci_controller struct */
355 list_add_tail(&pcidev_info->pdi_list,
356 &(SN_PCI_CONTROLLER(dev->bus)->pcidev_info));
358 /* Copy over PIO Mapped Addresses */
359 for (idx = 0; idx <= PCI_ROM_RESOURCE; idx++) {
360 unsigned long start, end, addr;
362 if (!pcidev_info->pdi_pio_mapped_addr[idx]) {
367 start = dev->resource[idx].start;
368 end = dev->resource[idx].end;
374 pci_addrs[idx] = start;
376 addr = pcidev_info->pdi_pio_mapped_addr[idx];
377 addr = ((addr << 4) >> 4) | __IA64_UNCACHED_OFFSET;
378 dev->resource[idx].start = addr;
379 dev->resource[idx].end = addr + size;
380 if (dev->resource[idx].flags & IORESOURCE_IO)
381 dev->resource[idx].parent = &ioport_resource;
383 dev->resource[idx].parent = &iomem_resource;
385 /* Create a pci_window in the pci_controller struct for
386 * each device resource.
389 sn_pci_window_fixup(dev, count, pci_addrs);
392 * Using the PROMs values for the PCI host bus, get the Linux
393 * PCI host_pci_dev struct and set up host bus linkages
396 bus_no = (pcidev_info->pdi_slot_host_handle >> 32) & 0xff;
397 devfn = pcidev_info->pdi_slot_host_handle & 0xffffffff;
398 host_pci_bus = pci_find_bus(segment, bus_no);
399 host_pci_dev = pci_get_slot(host_pci_bus, devfn);
401 pcidev_info->host_pci_dev = host_pci_dev;
402 pcidev_info->pdi_linux_pcidev = dev;
403 pcidev_info->pdi_host_pcidev_info = SN_PCIDEV_INFO(host_pci_dev);
404 bs = SN_PCIBUS_BUSSOFT(dev->bus);
405 pcidev_info->pdi_pcibus_info = bs;
407 if (bs && bs->bs_asic_type < PCIIO_ASIC_MAX_TYPES) {
408 SN_PCIDEV_BUSPROVIDER(dev) = sn_pci_provider[bs->bs_asic_type];
410 SN_PCIDEV_BUSPROVIDER(dev) = &sn_pci_default_provider;
413 /* Only set up IRQ stuff if this device has a host bus context */
414 if (bs && sn_irq_info->irq_irq) {
415 pcidev_info->pdi_sn_irq_info = sn_irq_info;
416 dev->irq = pcidev_info->pdi_sn_irq_info->irq_irq;
417 sn_irq_fixup(dev, sn_irq_info);
419 pcidev_info->pdi_sn_irq_info = NULL;
425 * sn_pci_controller_fixup() - This routine sets up a bus's resources
426 * consistent with the Linux PCI abstraction layer.
428 void sn_pci_controller_fixup(int segment, int busnum, struct pci_bus *bus)
432 struct pci_controller *controller;
433 struct sn_pci_controller *sn_controller;
434 struct pcibus_bussoft *prom_bussoft_ptr;
435 struct hubdev_info *hubdev_info;
436 void *provider_soft = NULL;
437 struct sn_pcibus_provider *provider;
439 status = sal_get_pcibus_info((u64) segment, (u64) busnum,
440 (u64) ia64_tpa(&prom_bussoft_ptr));
442 return; /*bus # does not exist */
443 prom_bussoft_ptr = __va(prom_bussoft_ptr);
445 /* Allocate a sn_pci_controller, which has a pci_controller struct
446 * as the first member.
448 sn_controller = kzalloc(sizeof(struct sn_pci_controller), GFP_KERNEL);
451 INIT_LIST_HEAD(&sn_controller->pcidev_info);
452 controller = &sn_controller->pci_controller;
453 controller->segment = segment;
456 bus = pci_scan_bus(busnum, &pci_root_ops, controller);
458 goto error_return; /* error, or bus already scanned */
463 goto error_return; /* sysdata already alloc'd */
466 * Per-provider fixup. Copies the contents from prom to local
467 * area and links SN_PCIBUS_BUSSOFT().
470 if (prom_bussoft_ptr->bs_asic_type >= PCIIO_ASIC_MAX_TYPES)
471 goto error_return; /* unsupported asic type */
473 if (prom_bussoft_ptr->bs_asic_type == PCIIO_ASIC_TYPE_PPB)
474 goto error_return; /* no further fixup necessary */
476 provider = sn_pci_provider[prom_bussoft_ptr->bs_asic_type];
477 if (provider == NULL)
478 goto error_return; /* no provider registerd for this asic */
480 bus->sysdata = controller;
481 if (provider->bus_fixup)
482 provider_soft = (*provider->bus_fixup) (prom_bussoft_ptr, controller);
484 if (provider_soft == NULL) {
485 /* fixup failed or not applicable */
491 * Setup pci_windows for legacy IO and MEM space.
492 * (Temporary until ACPI support is in place.)
494 controller->window = kcalloc(2, sizeof(struct pci_window), GFP_KERNEL);
495 if (controller->window == NULL)
497 controller->window[0].offset = prom_bussoft_ptr->bs_legacy_io;
498 controller->window[0].resource.name = "legacy_io";
499 controller->window[0].resource.flags = IORESOURCE_IO;
500 controller->window[0].resource.start = prom_bussoft_ptr->bs_legacy_io;
501 controller->window[0].resource.end =
502 controller->window[0].resource.start + 0xffff;
503 controller->window[0].resource.parent = &ioport_resource;
504 controller->window[1].offset = prom_bussoft_ptr->bs_legacy_mem;
505 controller->window[1].resource.name = "legacy_mem";
506 controller->window[1].resource.flags = IORESOURCE_MEM;
507 controller->window[1].resource.start = prom_bussoft_ptr->bs_legacy_mem;
508 controller->window[1].resource.end =
509 controller->window[1].resource.start + (1024 * 1024) - 1;
510 controller->window[1].resource.parent = &iomem_resource;
511 controller->windows = 2;
514 * Generic bus fixup goes here. Don't reference prom_bussoft_ptr
518 PCI_CONTROLLER(bus)->platform_data = provider_soft;
519 nasid = NASID_GET(SN_PCIBUS_BUSSOFT(bus)->bs_base);
520 cnode = nasid_to_cnodeid(nasid);
521 hubdev_info = (struct hubdev_info *)(NODEPDA(cnode)->pdinfo);
522 SN_PCIBUS_BUSSOFT(bus)->bs_xwidget_info =
523 &(hubdev_info->hdi_xwidget_info[SN_PCIBUS_BUSSOFT(bus)->bs_xid]);
526 * If the node information we obtained during the fixup phase is invalid
527 * then set controller->node to -1 (undetermined)
529 if (controller->node >= num_online_nodes()) {
530 struct pcibus_bussoft *b = SN_PCIBUS_BUSSOFT(bus);
532 printk(KERN_WARNING "Device ASIC=%u XID=%u PBUSNUM=%u"
533 "L_IO=%lx L_MEM=%lx BASE=%lx\n",
534 b->bs_asic_type, b->bs_xid, b->bs_persist_busnum,
535 b->bs_legacy_io, b->bs_legacy_mem, b->bs_base);
536 printk(KERN_WARNING "on node %d but only %d nodes online."
537 "Association set to undetermined.\n",
538 controller->node, num_online_nodes());
539 controller->node = -1;
545 kfree(sn_controller);
549 void sn_bus_store_sysdata(struct pci_dev *dev)
551 struct sysdata_el *element;
553 element = kzalloc(sizeof(struct sysdata_el), GFP_KERNEL);
555 dev_dbg(dev, "%s: out of memory!\n", __FUNCTION__);
558 element->sysdata = SN_PCIDEV_INFO(dev);
559 list_add(&element->entry, &sn_sysdata_list);
562 void sn_bus_free_sysdata(void)
564 struct sysdata_el *element;
565 struct list_head *list;
567 sn_sysdata_free_start:
568 list_for_each(list, &sn_sysdata_list) {
569 element = list_entry(list, struct sysdata_el, entry);
570 list_del(&element->entry);
571 kfree(element->sysdata);
573 goto sn_sysdata_free_start;
579 * Ugly hack to get PCI setup until we have a proper ACPI namespace.
582 #define PCI_BUSES_TO_SCAN 256
584 static int __init sn_pci_init(void)
588 struct pci_dev *pci_dev = NULL;
589 extern void sn_init_cpei_timer(void);
590 #ifdef CONFIG_PROC_FS
591 extern void register_sn_procfs(void);
594 if (!ia64_platform_is("sn2") || IS_RUNNING_ON_FAKE_PROM())
598 * prime sn_pci_provider[]. Individial provider init routines will
599 * override their respective default entries.
602 for (i = 0; i < PCIIO_ASIC_MAX_TYPES; i++)
603 sn_pci_provider[i] = &sn_pci_default_provider;
605 pcibr_init_provider();
606 tioca_init_provider();
607 tioce_init_provider();
610 * This is needed to avoid bounce limit checks in the blk layer
612 ia64_max_iommu_merge_mask = ~PAGE_MASK;
615 INIT_LIST_HEAD(&sn_sysdata_list);
616 sn_init_cpei_timer();
618 #ifdef CONFIG_PROC_FS
619 register_sn_procfs();
622 /* busses are not known yet ... */
623 for (i = 0; i <= max_segment_number; i++)
624 for (j = 0; j <= max_pcibus_number; j++)
625 sn_pci_controller_fixup(i, j, NULL);
628 * Generic Linux PCI Layer has created the pci_bus and pci_dev
629 * structures - time for us to add our SN PLatform specific
634 pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pci_dev)) != NULL)
635 sn_pci_fixup_slot(pci_dev);
637 sn_ioif_inited = 1; /* sn I/O infrastructure now initialized */
643 * hubdev_init_node() - Creates the HUB data structure and link them to it's
644 * own NODE specific data area.
646 void hubdev_init_node(nodepda_t * npda, cnodeid_t node)
649 struct hubdev_info *hubdev_info;
651 if (node >= num_online_nodes()) /* Headless/memless IO nodes */
653 (struct hubdev_info *)alloc_bootmem_node(NODE_DATA(0),
658 (struct hubdev_info *)alloc_bootmem_node(NODE_DATA(node),
661 npda->pdinfo = (void *)hubdev_info;
666 cnodeid_get_geoid(cnodeid_t cnode)
669 struct hubdev_info *hubdev;
671 hubdev = (struct hubdev_info *)(NODEPDA(cnode)->pdinfo);
672 return hubdev->hdi_geoid;
676 subsys_initcall(sn_pci_init);
677 EXPORT_SYMBOL(sn_pci_fixup_slot);
678 EXPORT_SYMBOL(sn_pci_unfixup_slot);
679 EXPORT_SYMBOL(sn_pci_controller_fixup);
680 EXPORT_SYMBOL(sn_bus_store_sysdata);
681 EXPORT_SYMBOL(sn_bus_free_sysdata);