ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ia64 / sn / io / machvec / pci_bus_cvlink.c
1 /*
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
4  * for more details.
5  *
6  * Copyright (C) 1992 - 1997, 2000-2003 Silicon Graphics, Inc. All rights reserved.
7  */
8
9 #include <linux/vmalloc.h>
10 #include <linux/slab.h>
11 #include <asm/sn/sgi.h>
12 #include <asm/sn/pci/pci_bus_cvlink.h>
13 #include <asm/sn/sn_cpuid.h>
14 #include <asm/sn/simulator.h>
15
16 extern int bridge_rev_b_data_check_disable;
17
18 vertex_hdl_t busnum_to_pcibr_vhdl[MAX_PCI_XWIDGET];
19 nasid_t busnum_to_nid[MAX_PCI_XWIDGET];
20 void * busnum_to_atedmamaps[MAX_PCI_XWIDGET];
21 unsigned char num_bridges;
22 static int done_probing;
23 extern irqpda_t *irqpdaindr;
24
25 static int pci_bus_map_create(struct pcibr_list_s *softlistp, moduleid_t io_moduleid);
26 vertex_hdl_t devfn_to_vertex(unsigned char busnum, unsigned int devfn);
27
28 extern void register_pcibr_intr(int irq, pcibr_intr_t intr);
29
30 static struct sn_flush_device_list *sn_dma_flush_init(unsigned long start,
31                                 unsigned long end,
32                                 int idx, int pin, int slot);
33 extern int cbrick_type_get_nasid(nasid_t);
34 extern void ioconfig_bus_new_entries(void);
35 extern void ioconfig_get_busnum(char *, int *);
36 extern int iomoduleid_get(nasid_t);
37 extern int pcibr_widget_to_bus(vertex_hdl_t);
38 extern int isIO9(int);
39
40 #define IS_OPUS(nasid) (cbrick_type_get_nasid(nasid) == MODULE_OPUSBRICK)
41 #define IS_ALTIX(nasid) (cbrick_type_get_nasid(nasid) == MODULE_CBRICK)
42
43 /*
44  * Init the provider asic for a given device
45  */
46
47 static inline void __init
48 set_pci_provider(struct sn_device_sysdata *device_sysdata)
49 {
50         pciio_info_t pciio_info = pciio_info_get(device_sysdata->vhdl);
51
52         device_sysdata->pci_provider = pciio_info_pops_get(pciio_info);
53 }
54
55 /*
56  * pci_bus_cvlink_init() - To be called once during initialization before
57  *      SGI IO Infrastructure init is called.
58  */
59 int
60 pci_bus_cvlink_init(void)
61 {
62
63         extern int ioconfig_bus_init(void);
64
65         memset(busnum_to_pcibr_vhdl, 0x0, sizeof(vertex_hdl_t) * MAX_PCI_XWIDGET);
66         memset(busnum_to_nid, 0x0, sizeof(nasid_t) * MAX_PCI_XWIDGET);
67
68         memset(busnum_to_atedmamaps, 0x0, sizeof(void *) * MAX_PCI_XWIDGET);
69
70         num_bridges = 0;
71
72         return ioconfig_bus_init();
73 }
74
75 /*
76  * pci_bus_to_vertex() - Given a logical Linux Bus Number returns the associated
77  *      pci bus vertex from the SGI IO Infrastructure.
78  */
79 static inline vertex_hdl_t
80 pci_bus_to_vertex(unsigned char busnum)
81 {
82
83         vertex_hdl_t    pci_bus = NULL;
84
85
86         /*
87          * First get the xwidget vertex.
88          */
89         pci_bus = busnum_to_pcibr_vhdl[busnum];
90         return(pci_bus);
91 }
92
93 /*
94  * devfn_to_vertex() - returns the vertex of the device given the bus, slot,
95  *      and function numbers.
96  */
97 vertex_hdl_t
98 devfn_to_vertex(unsigned char busnum, unsigned int devfn)
99 {
100
101         int slot = 0;
102         int func = 0;
103         char    name[16];
104         vertex_hdl_t  pci_bus = NULL;
105         vertex_hdl_t    device_vertex = (vertex_hdl_t)NULL;
106
107         /*
108          * Go get the pci bus vertex.
109          */
110         pci_bus = pci_bus_to_vertex(busnum);
111         if (!pci_bus) {
112                 /*
113                  * During probing, the Linux pci code invents non-existent
114                  * bus numbers and pci_dev structures and tries to access
115                  * them to determine existence. Don't crib during probing.
116                  */
117                 if (done_probing)
118                         printk("devfn_to_vertex: Invalid bus number %d given.\n", busnum);
119                 return(NULL);
120         }
121
122
123         /*
124          * Go get the slot&function vertex.
125          * Should call pciio_slot_func_to_name() when ready.
126          */
127         slot = PCI_SLOT(devfn);
128         func = PCI_FUNC(devfn);
129
130         /*
131          * For a NON Multi-function card the name of the device looks like:
132          * ../pci/1, ../pci/2 ..
133          */
134         if (func == 0) {
135                 sprintf(name, "%d", slot);
136                 if (hwgraph_traverse(pci_bus, name, &device_vertex) ==
137                         GRAPH_SUCCESS) {
138                         if (device_vertex) {
139                                 return(device_vertex);
140                         }
141                 }
142         }
143                         
144         /*
145          * This maybe a multifunction card.  It's names look like:
146          * ../pci/1a, ../pci/1b, etc.
147          */
148         sprintf(name, "%d%c", slot, 'a'+func);
149         if (hwgraph_traverse(pci_bus, name, &device_vertex) != GRAPH_SUCCESS) {
150                 if (!device_vertex) {
151                         return(NULL);
152                 }
153         }
154
155         return(device_vertex);
156 }
157
158 /*
159  * sn_alloc_pci_sysdata() - This routine allocates a pci controller
160  *      which is expected as the pci_dev and pci_bus sysdata by the Linux
161  *      PCI infrastructure.
162  */
163 static struct pci_controller *
164 sn_alloc_pci_sysdata(void)
165 {
166         struct pci_controller *pci_sysdata;
167
168         pci_sysdata = kmalloc(sizeof(*pci_sysdata), GFP_KERNEL);
169         if (!pci_sysdata)
170                 return NULL;
171
172         memset(pci_sysdata, 0, sizeof(*pci_sysdata));
173         return pci_sysdata;
174 }
175
176 /*
177  * sn_pci_fixup_bus() - This routine sets up a bus's resources
178  * consistent with the Linux PCI abstraction layer.
179  */
180 static int __init
181 sn_pci_fixup_bus(struct pci_bus *bus)
182 {
183         struct pci_controller *pci_sysdata;
184         struct sn_widget_sysdata *widget_sysdata;
185
186         pci_sysdata = sn_alloc_pci_sysdata();
187         if  (!pci_sysdata) {
188                 printk(KERN_WARNING "sn_pci_fixup_bus(): Unable to "
189                                "allocate memory for pci_sysdata\n");
190                 return -ENOMEM;
191         }
192         widget_sysdata = kmalloc(sizeof(struct sn_widget_sysdata),
193                                  GFP_KERNEL);
194         if (!widget_sysdata) {
195                 printk(KERN_WARNING "sn_pci_fixup_bus(): Unable to "
196                                "allocate memory for widget_sysdata\n");
197                 kfree(pci_sysdata);
198                 return -ENOMEM;
199         }
200
201         widget_sysdata->vhdl = pci_bus_to_vertex(bus->number);
202         pci_sysdata->platform_data = (void *)widget_sysdata;
203         bus->sysdata = pci_sysdata;
204         return 0;
205 }
206
207
208 /*
209  * sn_pci_fixup_slot() - This routine sets up a slot's resources
210  * consistent with the Linux PCI abstraction layer.  Resources acquired
211  * from our PCI provider include PIO maps to BAR space and interrupt
212  * objects.
213  */
214 static int
215 sn_pci_fixup_slot(struct pci_dev *dev)
216 {
217         extern int bit_pos_to_irq(int);
218         unsigned int irq;
219         int idx;
220         u16 cmd;
221         vertex_hdl_t vhdl;
222         unsigned long size;
223         struct pci_controller *pci_sysdata;
224         struct sn_device_sysdata *device_sysdata;
225         pciio_intr_line_t lines = 0;
226         vertex_hdl_t device_vertex;
227         pciio_provider_t *pci_provider;
228         pciio_intr_t intr_handle;
229
230         /* Allocate a controller structure */
231         pci_sysdata = sn_alloc_pci_sysdata();
232         if (!pci_sysdata) {
233                 printk(KERN_WARNING "sn_pci_fixup_slot: Unable to "
234                                "allocate memory for pci_sysdata\n");
235                 return -ENOMEM;
236         }
237
238         /* Set the device vertex */
239         device_sysdata = kmalloc(sizeof(struct sn_device_sysdata), GFP_KERNEL);
240         if (!device_sysdata) {
241                 printk(KERN_WARNING "sn_pci_fixup_slot: Unable to "
242                                "allocate memory for device_sysdata\n");
243                 kfree(pci_sysdata);
244                 return -ENOMEM;
245         }
246
247         device_sysdata->vhdl = devfn_to_vertex(dev->bus->number, dev->devfn);
248         pci_sysdata->platform_data = (void *) device_sysdata;
249         dev->sysdata = pci_sysdata;
250         set_pci_provider(device_sysdata);
251
252         pci_read_config_word(dev, PCI_COMMAND, &cmd);
253
254         /*
255          * Set the resources address correctly.  The assumption here
256          * is that the addresses in the resource structure has been
257          * read from the card and it was set in the card by our
258          * Infrastructure.  NOTE: PIC and TIOCP don't have big-window
259          * upport for PCI I/O space.  So by mapping the I/O space
260          * first we will attempt to use Device(x) registers for I/O
261          * BARs (which can't use big windows like MEM BARs can).
262          */
263         vhdl = device_sysdata->vhdl;
264
265         /* Allocate the IORESOURCE_IO space first */
266         for (idx = 0; idx < PCI_ROM_RESOURCE; idx++) {
267                 unsigned long start, end, addr;
268
269                 device_sysdata->pio_map[idx] = NULL;
270
271                 if (!(dev->resource[idx].flags & IORESOURCE_IO))
272                         continue;
273
274                 start = dev->resource[idx].start;
275                 end = dev->resource[idx].end;
276                 size = end - start;
277                 if (!size)
278                         continue;
279
280                 addr = (unsigned long)pciio_pio_addr(vhdl, 0,
281                 PCIIO_SPACE_WIN(idx), 0, size,
282                                 &device_sysdata->pio_map[idx], 0);
283
284                 if (!addr) {
285                         dev->resource[idx].start = 0;
286                         dev->resource[idx].end = 0;
287                         printk("sn_pci_fixup(): pio map failure for "
288                                 "%s bar%d\n", dev->slot_name, idx);
289                 } else {
290                         addr |= __IA64_UNCACHED_OFFSET;
291                         dev->resource[idx].start = addr;
292                         dev->resource[idx].end = addr + size;
293                 }
294
295                 if (dev->resource[idx].flags & IORESOURCE_IO)
296                         cmd |= PCI_COMMAND_IO;
297         }
298
299         /* Allocate the IORESOURCE_MEM space next */
300         for (idx = 0; idx < PCI_ROM_RESOURCE; idx++) {
301                 unsigned long start, end, addr;
302
303                 if ((dev->resource[idx].flags & IORESOURCE_IO))
304                         continue;
305
306                 start = dev->resource[idx].start;
307                 end = dev->resource[idx].end;
308                 size = end - start;
309                 if (!size)
310                         continue;
311
312                 addr = (unsigned long)pciio_pio_addr(vhdl, 0,
313                 PCIIO_SPACE_WIN(idx), 0, size,
314                                 &device_sysdata->pio_map[idx], 0);
315
316                 if (!addr) {
317                         dev->resource[idx].start = 0;
318                         dev->resource[idx].end = 0;
319                         printk("sn_pci_fixup(): pio map failure for "
320                                 "%s bar%d\n", dev->slot_name, idx);
321                 } else {
322                         addr |= __IA64_UNCACHED_OFFSET;
323                         dev->resource[idx].start = addr;
324                         dev->resource[idx].end = addr + size;
325                 }
326
327                 if (dev->resource[idx].flags & IORESOURCE_MEM)
328                         cmd |= PCI_COMMAND_MEMORY;
329         }
330
331         /*
332          * Update the Command Word on the Card.
333          */
334         cmd |= PCI_COMMAND_MASTER; /* If the device doesn't support */
335                                    /* bit gets dropped .. no harm */
336         pci_write_config_word(dev, PCI_COMMAND, cmd);
337
338         pci_read_config_byte(dev, PCI_INTERRUPT_PIN, (unsigned char *)&lines);
339         device_vertex = device_sysdata->vhdl;
340         pci_provider = device_sysdata->pci_provider;
341         device_sysdata->intr_handle = NULL;
342
343         if (!lines)
344                 return 0;
345
346         irqpdaindr->curr = dev;
347
348         intr_handle = (pci_provider->intr_alloc)(device_vertex, NULL, lines, device_vertex);
349         if (intr_handle == NULL) {
350                 printk(KERN_WARNING "sn_pci_fixup:  pcibr_intr_alloc() failed\n");
351                 kfree(pci_sysdata);
352                 kfree(device_sysdata);
353                 return -ENOMEM;
354         }
355
356         device_sysdata->intr_handle = intr_handle;
357         irq = intr_handle->pi_irq;
358         irqpdaindr->device_dev[irq] = dev;
359         (pci_provider->intr_connect)(intr_handle, (intr_func_t)0, (intr_arg_t)0);
360         dev->irq = irq;
361
362         register_pcibr_intr(irq, (pcibr_intr_t)intr_handle);
363
364         for (idx = 0; idx < PCI_ROM_RESOURCE; idx++) {
365                 int ibits = ((pcibr_intr_t)intr_handle)->bi_ibits;
366                 int i;
367
368                 size = dev->resource[idx].end -
369                         dev->resource[idx].start;
370                 if (size == 0) continue;
371
372                 for (i=0; i<8; i++) {
373                         if (ibits & (1 << i) ) {
374                                 extern pcibr_info_t pcibr_info_get(vertex_hdl_t);
375                                 device_sysdata->dma_flush_list =
376                                  sn_dma_flush_init(dev->resource[idx].start,
377                                                    dev->resource[idx].end,
378                                                    idx,
379                                                    i,
380                                                    PCIBR_INFO_SLOT_GET_EXT(pcibr_info_get(device_sysdata->vhdl)));
381                         }
382                 }
383         }
384         return 0;
385 }
386
387 #ifdef CONFIG_HOTPLUG_PCI_SGI
388
389 void
390 sn_dma_flush_clear(struct sn_flush_device_list *dma_flush_list,
391                    unsigned long start, unsigned long end)
392 {
393
394         int i;
395
396         dma_flush_list->pin = -1;
397         dma_flush_list->bus = -1;
398         dma_flush_list->slot = -1;
399
400         for (i = 0; i < PCI_ROM_RESOURCE; i++)
401                 if ((dma_flush_list->bar_list[i].start == start) &&
402                     (dma_flush_list->bar_list[i].end == end)) {
403                         dma_flush_list->bar_list[i].start = 0;
404                         dma_flush_list->bar_list[i].end = 0;
405                         break;
406                 }           
407
408 }
409
410 /*
411  * sn_pci_unfixup_slot() - This routine frees a slot's resources
412  * consistent with the Linux PCI abstraction layer.  Resources released
413  * back to our PCI provider include PIO maps to BAR space and interrupt
414  * objects.
415  */
416 void
417 sn_pci_unfixup_slot(struct pci_dev *dev)
418 {
419         struct sn_device_sysdata *device_sysdata;
420         vertex_hdl_t vhdl;
421         pciio_intr_t intr_handle;
422         unsigned int irq;
423         unsigned long size;
424         int idx;
425
426         device_sysdata = SN_DEVICE_SYSDATA(dev);
427
428         vhdl = device_sysdata->vhdl;
429
430         if (device_sysdata->dma_flush_list)
431                 for (idx = 0; idx < PCI_ROM_RESOURCE; idx++) {
432                         size = dev->resource[idx].end -
433                                 dev->resource[idx].start;
434                         if (size == 0) continue;
435
436                         sn_dma_flush_clear(device_sysdata->dma_flush_list,
437                                            dev->resource[idx].start,
438                                            dev->resource[idx].end);
439                 }
440
441         intr_handle = device_sysdata->intr_handle;
442         if (intr_handle) {
443                 extern void unregister_pcibr_intr(int, pcibr_intr_t);
444                 irq = intr_handle->pi_irq;
445                 irqpdaindr->device_dev[irq] = NULL;
446                 unregister_pcibr_intr(irq, (pcibr_intr_t) intr_handle);
447                 pciio_intr_disconnect(intr_handle);
448                 pciio_intr_free(intr_handle);
449         }
450
451         for (idx = 0; idx < PCI_ROM_RESOURCE; idx++) {
452                 if (device_sysdata->pio_map[idx]) {
453                         pciio_piomap_done (device_sysdata->pio_map[idx]);
454                         pciio_piomap_free (device_sysdata->pio_map[idx]);
455                 }
456         }
457
458 }
459 #endif /* CONFIG_HOTPLUG_PCI_SGI */
460
461 struct sn_flush_nasid_entry flush_nasid_list[MAX_NASIDS];
462
463 /* Initialize the data structures for flushing write buffers after a PIO read.
464  * The theory is:
465  * Take an unused int. pin and associate it with a pin that is in use.
466  * After a PIO read, force an interrupt on the unused pin, forcing a write buffer flush
467  * on the in use pin.  This will prevent the race condition between PIO read responses and
468  * DMA writes.
469  */
470 static struct sn_flush_device_list *
471 sn_dma_flush_init(unsigned long start, unsigned long end, int idx, int pin, int slot)
472 {
473         nasid_t nasid;
474         unsigned long dnasid;
475         int wid_num;
476         int bus;
477         struct sn_flush_device_list *p;
478         void *b;
479         int bwin;
480         int i;
481
482         nasid = NASID_GET(start);
483         wid_num = SWIN_WIDGETNUM(start);
484         bus = (start >> 23) & 0x1;
485         bwin = BWIN_WINDOWNUM(start);
486
487         if (flush_nasid_list[nasid].widget_p == NULL) {
488                 flush_nasid_list[nasid].widget_p = (struct sn_flush_device_list **)kmalloc((HUB_WIDGET_ID_MAX+1) *
489                         sizeof(struct sn_flush_device_list *), GFP_KERNEL);
490                 if (!flush_nasid_list[nasid].widget_p) {
491                         printk(KERN_WARNING "sn_dma_flush_init: Cannot allocate memory for nasid list\n");
492                         return NULL;
493                 }
494                 memset(flush_nasid_list[nasid].widget_p, 0, (HUB_WIDGET_ID_MAX+1) * sizeof(struct sn_flush_device_list *));
495         }
496         if (bwin > 0) {
497                 int itte_index = bwin - 1;
498                 unsigned long itte;
499
500                 itte = HUB_L(IIO_ITTE_GET(nasid, itte_index));
501                 flush_nasid_list[nasid].iio_itte[bwin] = itte;
502                 wid_num = (itte >> IIO_ITTE_WIDGET_SHIFT)
503                                 & IIO_ITTE_WIDGET_MASK;
504                 bus = itte & IIO_ITTE_OFFSET_MASK;
505                 if (bus == 0x4 || bus == 0x8) {
506                         bus = 0;
507                 } else {
508                         bus = 1;
509                 }
510         }
511
512         /* if it's IO9, bus 1, we don't care about slots 1 and 4.  This is
513          * because these are the IOC4 slots and we don't flush them.
514          */
515         if (isIO9(nasid) && bus == 0 && (slot == 1 || slot == 4)) {
516                 return NULL;
517         }
518         if (flush_nasid_list[nasid].widget_p[wid_num] == NULL) {
519                 flush_nasid_list[nasid].widget_p[wid_num] = (struct sn_flush_device_list *)kmalloc(
520                         DEV_PER_WIDGET * sizeof (struct sn_flush_device_list), GFP_KERNEL);
521                 if (!flush_nasid_list[nasid].widget_p[wid_num]) {
522                         printk(KERN_WARNING "sn_dma_flush_init: Cannot allocate memory for nasid sub-list\n");
523                         return NULL;
524                 }
525                 memset(flush_nasid_list[nasid].widget_p[wid_num], 0,
526                         DEV_PER_WIDGET * sizeof (struct sn_flush_device_list));
527                 p = &flush_nasid_list[nasid].widget_p[wid_num][0];
528                 for (i=0; i<DEV_PER_WIDGET;i++) {
529                         p->bus = -1;
530                         p->pin = -1;
531                         p->slot = -1;
532                         p++;
533                 }
534         }
535
536         p = &flush_nasid_list[nasid].widget_p[wid_num][0];
537         for (i=0;i<DEV_PER_WIDGET; i++) {
538                 if (p->pin == pin && p->bus == bus && p->slot == slot) break;
539                 if (p->pin < 0) {
540                         p->pin = pin;
541                         p->bus = bus;
542                         p->slot = slot;
543                         break;
544                 }
545                 p++;
546         }
547
548         for (i=0; i<PCI_ROM_RESOURCE; i++) {
549                 if (p->bar_list[i].start == 0) {
550                         p->bar_list[i].start = start;
551                         p->bar_list[i].end = end;
552                         break;
553                 }
554         }
555         b = (void *)(NODE_SWIN_BASE(nasid, wid_num) | (bus << 23) );
556
557         /* If it's IO9, then slot 2 maps to slot 7 and slot 6 maps to slot 8.
558          * To see this is non-trivial.  By drawing pictures and reading manuals and talking
559          * to HW guys, we can see that on IO9 bus 1, slots 7 and 8 are always unused.
560          * Further, since we short-circuit slots  1, 3, and 4 above, we only have to worry
561          * about the case when there is a card in slot 2.  A multifunction card will appear
562          * to be in slot 6 (from an interrupt point of view) also.  That's the  most we'll
563          * have to worry about.  A four function card will overload the interrupt lines in
564          * slot 2 and 6.
565          * We also need to special case the 12160 device in slot 3.  Fortunately, we have
566          * a spare intr. line for pin 4, so we'll use that for the 12160.
567          * All other buses have slot 3 and 4 and slots 7 and 8 unused.  Since we can only
568          * see slots 1 and 2 and slots 5 and 6 coming through here for those buses (this
569          * is true only on Pxbricks with 2 physical slots per bus), we just need to add
570          * 2 to the slot number to find an unused slot.
571          * We have convinced ourselves that we will never see a case where two different cards
572          * in two different slots will ever share an interrupt line, so there is no need to
573          * special case this.
574          */
575
576         if (isIO9(nasid) && ( (IS_ALTIX(nasid) && wid_num == 0xc)
577                                 || (IS_OPUS(nasid) && wid_num == 0xf) )
578                                 && bus == 0) {
579                 if (pin == 1) {
580                         p->force_int_addr = (unsigned long)pcireg_bridge_force_always_addr_get(b, 6);
581                         pcireg_bridge_intr_device_bit_set(b, (1<<18));
582                         dnasid = NASID_GET(virt_to_phys(&p->flush_addr));
583                         pcireg_bridge_intr_addr_set(b, 6, ((virt_to_phys(&p->flush_addr) & 0xfffffffff) |
584                                         (dnasid << 36) | (0xfUL << 48)));
585                 } else if (pin == 2) { /* 12160 SCSI device in IO9 */
586                         p->force_int_addr = (unsigned long)pcireg_bridge_force_always_addr_get(b, 4);
587                         pcireg_bridge_intr_device_bit_set(b, (2<<12));
588                         dnasid = NASID_GET(virt_to_phys(&p->flush_addr));
589                         pcireg_bridge_intr_addr_set(b, 4,
590                                         ((virt_to_phys(&p->flush_addr) & 0xfffffffff) |
591                                         (dnasid << 36) | (0xfUL << 48)));
592                 } else { /* slot == 6 */
593                         p->force_int_addr = (unsigned long)pcireg_bridge_force_always_addr_get(b, 7);
594                         pcireg_bridge_intr_device_bit_set(b, (5<<21));
595                         dnasid = NASID_GET(virt_to_phys(&p->flush_addr));
596                         pcireg_bridge_intr_addr_set(b, 7,
597                                         ((virt_to_phys(&p->flush_addr) & 0xfffffffff) |
598                                         (dnasid << 36) | (0xfUL << 48)));
599                 }
600         } else {
601                 p->force_int_addr = (unsigned long)pcireg_bridge_force_always_addr_get(b, (pin +2));
602                 pcireg_bridge_intr_device_bit_set(b, (pin << (pin * 3)));
603                 dnasid = NASID_GET(virt_to_phys(&p->flush_addr));
604                 pcireg_bridge_intr_addr_set(b, (pin + 2),
605                                 ((virt_to_phys(&p->flush_addr) & 0xfffffffff) |
606                                 (dnasid << 36) | (0xfUL << 48)));
607         }
608         return p;
609 }
610
611
612 /*
613  * linux_bus_cvlink() Creates a link between the Linux PCI Bus number
614  *      to the actual hardware component that it represents:
615  *      /dev/hw/linux/busnum/0 -> ../../../hw/module/001c01/slab/0/Ibrick/xtalk/15/pci
616  *
617  *      The bus vertex, when called to devfs_generate_path() returns:
618  *              hw/module/001c01/slab/0/Ibrick/xtalk/15/pci
619  *              hw/module/001c01/slab/1/Pbrick/xtalk/12/pci-x/0
620  *              hw/module/001c01/slab/1/Pbrick/xtalk/12/pci-x/1
621  */
622 void
623 linux_bus_cvlink(void)
624 {
625         char name[8];
626         int index;
627         
628         for (index=0; index < MAX_PCI_XWIDGET; index++) {
629                 if (!busnum_to_pcibr_vhdl[index])
630                         continue;
631
632                 sprintf(name, "%x", index);
633                 (void) hwgraph_edge_add(linux_busnum, busnum_to_pcibr_vhdl[index],
634                                 name);
635         }
636 }
637
638 /*
639  * pci_bus_map_create() - Called by pci_bus_to_hcl_cvlink() to finish the job.
640  *
641  *      Linux PCI Bus numbers are assigned from lowest module_id numbers
642  *      (rack/slot etc.)
643  */
644 static int
645 pci_bus_map_create(struct pcibr_list_s *softlistp, moduleid_t moduleid)
646 {
647         
648         int basebus_num, bus_number;
649         vertex_hdl_t pci_bus = softlistp->bl_vhdl;
650         char moduleid_str[16];
651
652         memset(moduleid_str, 0, 16);
653         format_module_id(moduleid_str, moduleid, MODULE_FORMAT_BRIEF);
654         (void) ioconfig_get_busnum((char *)moduleid_str, &basebus_num);
655
656         /*
657          * Assign the correct bus number and also the nasid of this
658          * pci Xwidget.
659          */
660         bus_number = basebus_num + pcibr_widget_to_bus(pci_bus);
661 #ifdef DEBUG
662         {
663         char hwpath[MAXDEVNAME] = "\0";
664         extern int hwgraph_vertex_name_get(vertex_hdl_t, char *, uint);
665
666         pcibr_soft_t pcibr_soft = softlistp->bl_soft;
667         hwgraph_vertex_name_get(pci_bus, hwpath, MAXDEVNAME);
668         printk("%s:\n\tbus_num %d, basebus_num %d, brick_bus %d, "
669                 "bus_vhdl 0x%lx, brick_type %d\n", hwpath, bus_number,
670                 basebus_num, pcibr_widget_to_bus(pci_bus),
671                 (uint64_t)pci_bus, pcibr_soft->bs_bricktype);
672         }
673 #endif
674         busnum_to_pcibr_vhdl[bus_number] = pci_bus;
675
676         /*
677          * Pre assign DMA maps needed for 32 Bits Page Map DMA.
678          */
679         busnum_to_atedmamaps[bus_number] = (void *) vmalloc(
680                         sizeof(struct pcibr_dmamap_s)*MAX_ATE_MAPS);
681         if (busnum_to_atedmamaps[bus_number] <= 0) {
682                 printk("pci_bus_map_create: Cannot allocate memory for ate maps\n");
683                 return -1;
684         }
685         memset(busnum_to_atedmamaps[bus_number], 0x0,
686                         sizeof(struct pcibr_dmamap_s) * MAX_ATE_MAPS);
687         return(0);
688 }
689
690 /*
691  * pci_bus_to_hcl_cvlink() - This routine is called after SGI IO Infrastructure
692  *      initialization has completed to set up the mappings between PCI BRIDGE
693  *      ASIC and logical pci bus numbers.
694  *
695  *      Must be called before pci_init() is invoked.
696  */
697 int
698 pci_bus_to_hcl_cvlink(void)
699 {
700         int i;
701         extern pcibr_list_p pcibr_list;
702
703         for (i = 0; i < nummodules; i++) {
704                 struct pcibr_list_s *softlistp = pcibr_list;
705                 struct pcibr_list_s *first_in_list = NULL;
706                 struct pcibr_list_s *last_in_list = NULL;
707
708                 /* Walk the list of pcibr_soft structs looking for matches */
709                 while (softlistp) {
710                         struct pcibr_soft_s *pcibr_soft = softlistp->bl_soft;
711                         moduleid_t moduleid;
712                         
713                         /* Is this PCI bus associated with this moduleid? */
714                         moduleid = NODE_MODULEID(
715                                 NASID_TO_COMPACT_NODEID(pcibr_soft->bs_nasid));
716                         if (modules[i]->id == moduleid) {
717                                 struct pcibr_list_s *new_element;
718
719                                 new_element = kmalloc(sizeof (struct pcibr_soft_s), GFP_KERNEL);
720                                 if (new_element == NULL) {
721                                         printk("%s: Couldn't allocate memory\n",__FUNCTION__);
722                                         return -ENOMEM;
723                                 }
724                                 new_element->bl_soft = softlistp->bl_soft;
725                                 new_element->bl_vhdl = softlistp->bl_vhdl;
726                                 new_element->bl_next = NULL;
727
728                                 /* list empty so just put it on the list */
729                                 if (first_in_list == NULL) {
730                                         first_in_list = new_element;
731                                         last_in_list = new_element;
732                                         softlistp = softlistp->bl_next;
733                                         continue;
734                                 }
735
736                                 /*
737                                  * BASEIO IObricks attached to a module have
738                                  * a higher priority than non BASEIO IOBricks
739                                  * when it comes to persistant pci bus
740                                  * numbering, so put them on the front of the
741                                  * list.
742                                  */
743                                 if (isIO9(pcibr_soft->bs_nasid)) {
744                                         new_element->bl_next = first_in_list;
745                                         first_in_list = new_element;
746                                 } else {
747                                         last_in_list->bl_next = new_element;
748                                         last_in_list = new_element;
749                                 }
750                         }
751                         softlistp = softlistp->bl_next;
752                 }
753                                 
754                 /*
755                  * We now have a list of all the pci bridges associated with
756                  * the module_id, modules[i].  Call pci_bus_map_create() for
757                  * each pci bridge
758                  */
759                 softlistp = first_in_list;
760                 while (softlistp) {
761                         moduleid_t iobrick;
762                         struct pcibr_list_s *next = softlistp->bl_next;
763                         iobrick = iomoduleid_get(softlistp->bl_soft->bs_nasid);
764                         pci_bus_map_create(softlistp, iobrick);
765                         kfree(softlistp);
766                         softlistp = next;
767                 }
768         }
769
770         /*
771          * Create the Linux PCI bus number vertex link.
772          */
773         (void)linux_bus_cvlink();
774         (void)ioconfig_bus_new_entries();
775
776         return(0);
777 }
778
779 /*
780  * Ugly hack to get PCI setup until we have a proper ACPI namespace.
781  */
782
783 #define PCI_BUSES_TO_SCAN 256
784
785 extern struct pci_ops sn_pci_ops;
786 int __init
787 sn_pci_init (void)
788 {
789         int i = 0;
790         struct pci_controller *controller;
791         struct list_head *ln;
792         struct pci_bus *pci_bus = NULL;
793         struct pci_dev *pci_dev = NULL;
794         extern int numnodes;
795         int cnode, ret;
796 #ifdef CONFIG_PROC_FS
797         extern void register_sn_procfs(void);
798 #endif
799         extern void sgi_master_io_infr_init(void);
800         extern void sn_init_cpei_timer(void);
801
802
803         if (!ia64_platform_is("sn2") || IS_RUNNING_ON_SIMULATOR())
804                 return 0;
805
806         /*
807          * This is needed to avoid bounce limit checks in the blk layer
808          */
809         ia64_max_iommu_merge_mask = ~PAGE_MASK;
810
811         /*
812          * set pci_raw_ops, etc.
813          */
814         sgi_master_io_infr_init();
815
816         sn_init_cpei_timer();
817
818 #ifdef CONFIG_PROC_FS
819         register_sn_procfs();
820 #endif
821
822         controller = kmalloc(sizeof(struct pci_controller), GFP_KERNEL);
823         if (!controller) {
824                 printk(KERN_WARNING "cannot allocate PCI controller\n");
825                 return 0;
826         }
827
828         memset(controller, 0, sizeof(struct pci_controller));
829
830         for (i = 0; i < PCI_BUSES_TO_SCAN; i++)
831                 if (pci_bus_to_vertex(i))
832                         pci_scan_bus(i, &sn_pci_ops, controller);
833
834         done_probing = 1;
835
836         /*
837          * Initialize the pci bus vertex in the pci_bus struct.
838          */
839         for( ln = pci_root_buses.next; ln != &pci_root_buses; ln = ln->next) {
840                 pci_bus = pci_bus_b(ln);
841                 ret = sn_pci_fixup_bus(pci_bus);
842                 if ( ret ) {
843                         printk(KERN_WARNING
844                                 "sn_pci_fixup: sn_pci_fixup_bus fails : error %d\n",
845                                         ret);
846                         return 0;
847                 }
848         }
849
850         /*
851          * set the root start and end so that drivers calling check_region()
852          * won't see a conflict
853          */
854         ioport_resource.start = 0xc000000000000000;
855         ioport_resource.end = 0xcfffffffffffffff;
856
857         /*
858          * Set the root start and end for Mem Resource.
859          */
860         iomem_resource.start = 0;
861         iomem_resource.end = 0xffffffffffffffff;
862
863         /*
864          * Initialize the device vertex in the pci_dev struct.
865          */
866         while ((pci_dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pci_dev)) != NULL) {
867                 ret = sn_pci_fixup_slot(pci_dev);
868                 if ( ret ) {
869                         printk(KERN_WARNING
870                                 "sn_pci_fixup: sn_pci_fixup_slot fails : error %d\n",
871                                         ret);
872                         return 0;
873                 }
874         }
875
876         return 0;
877 }
878
879 subsys_initcall(sn_pci_init);