pci_bus_cvlink.c 23 KB
Newer Older
Jesse Barnes's avatar
Jesse Barnes committed
1
/*
Jesse Barnes's avatar
Jesse Barnes committed
2 3 4 5 6 7 8
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 * Copyright (C) 1992 - 1997, 2000-2003 Silicon Graphics, Inc. All rights reserved.
 */

9
#include <linux/vmalloc.h>
Jesse Barnes's avatar
Jesse Barnes committed
10 11 12 13 14 15 16
#include <asm/sn/sgi.h>
#include <asm/sn/iograph.h>
#include <asm/sn/pci/pci_bus_cvlink.h>
#include <asm/sn/sn_cpuid.h>

extern int bridge_rev_b_data_check_disable;

Jesse Barnes's avatar
Jesse Barnes committed
17
vertex_hdl_t busnum_to_pcibr_vhdl[MAX_PCI_XWIDGET];
Jesse Barnes's avatar
Jesse Barnes committed
18 19 20
nasid_t busnum_to_nid[MAX_PCI_XWIDGET];
void * busnum_to_atedmamaps[MAX_PCI_XWIDGET];
unsigned char num_bridges;
Jesse Barnes's avatar
Jesse Barnes committed
21 22
static int done_probing;
extern irqpda_t *irqpdaindr;
Jesse Barnes's avatar
Jesse Barnes committed
23

Jesse Barnes's avatar
Jesse Barnes committed
24 25
static int pci_bus_map_create(vertex_hdl_t xtalk, char * io_moduleid);
vertex_hdl_t devfn_to_vertex(unsigned char busnum, unsigned int devfn);
Jesse Barnes's avatar
Jesse Barnes committed
26 27 28

extern void register_pcibr_intr(int irq, pcibr_intr_t intr);

Jesse Barnes's avatar
Jesse Barnes committed
29
void sn_dma_flush_init(unsigned long start, unsigned long end, int idx, int pin, int slot);
30
extern int cbrick_type_get_nasid(nasid_t);
Jesse Barnes's avatar
Jesse Barnes committed
31

32 33
#define IS_OPUS(nasid) (cbrick_type_get_nasid(nasid) == MODULE_OPUSBRICK)
#define IS_ALTIX(nasid) (cbrick_type_get_nasid(nasid) == MODULE_CBRICK)
Jesse Barnes's avatar
Jesse Barnes committed
34

35 36 37 38 39 40 41 42 43 44 45 46
/*
 * Init the provider asic for a given device
 */

static void
set_pci_provider(struct sn_device_sysdata *device_sysdata)
{
	pciio_info_t pciio_info = pciio_info_get(device_sysdata->vhdl);

	device_sysdata->pci_provider = pciio_info_pops_get(pciio_info);
}

Jesse Barnes's avatar
Jesse Barnes committed
47 48 49 50
/*
 * pci_bus_cvlink_init() - To be called once during initialization before 
 *	SGI IO Infrastructure init is called.
 */
51
int
Jesse Barnes's avatar
Jesse Barnes committed
52 53 54
pci_bus_cvlink_init(void)
{

55
	extern int ioconfig_bus_init(void);
Jesse Barnes's avatar
Jesse Barnes committed
56

Jesse Barnes's avatar
Jesse Barnes committed
57
	memset(busnum_to_pcibr_vhdl, 0x0, sizeof(vertex_hdl_t) * MAX_PCI_XWIDGET);
Jesse Barnes's avatar
Jesse Barnes committed
58 59 60 61 62 63
	memset(busnum_to_nid, 0x0, sizeof(nasid_t) * MAX_PCI_XWIDGET);

	memset(busnum_to_atedmamaps, 0x0, sizeof(void *) * MAX_PCI_XWIDGET);

	num_bridges = 0;

64
	return ioconfig_bus_init();
Jesse Barnes's avatar
Jesse Barnes committed
65 66 67 68 69 70
}

/*
 * pci_bus_to_vertex() - Given a logical Linux Bus Number returns the associated 
 *	pci bus vertex from the SGI IO Infrastructure.
 */
Jesse Barnes's avatar
Jesse Barnes committed
71
vertex_hdl_t
Jesse Barnes's avatar
Jesse Barnes committed
72 73 74
pci_bus_to_vertex(unsigned char busnum)
{

Jesse Barnes's avatar
Jesse Barnes committed
75
	vertex_hdl_t	pci_bus = NULL;
Jesse Barnes's avatar
Jesse Barnes committed
76 77 78 79 80 81 82 83 84 85 86 87 88


	/*
	 * First get the xwidget vertex.
	 */
	pci_bus = busnum_to_pcibr_vhdl[busnum];
	return(pci_bus);
}

/*
 * devfn_to_vertex() - returns the vertex of the device given the bus, slot, 
 *	and function numbers.
 */
Jesse Barnes's avatar
Jesse Barnes committed
89
vertex_hdl_t
Jesse Barnes's avatar
Jesse Barnes committed
90 91 92 93 94 95
devfn_to_vertex(unsigned char busnum, unsigned int devfn)
{

	int slot = 0;
	int func = 0;
	char	name[16];
Jesse Barnes's avatar
Jesse Barnes committed
96 97
	vertex_hdl_t  pci_bus = NULL;
	vertex_hdl_t	device_vertex = (vertex_hdl_t)NULL;
Jesse Barnes's avatar
Jesse Barnes committed
98 99 100 101 102 103 104

	/*
	 * Go get the pci bus vertex.
	 */
	pci_bus = pci_bus_to_vertex(busnum);
	if (!pci_bus) {
		/*
Jesse Barnes's avatar
Jesse Barnes committed
105
		 * During probing, the Linux pci code invents non-existent
Jesse Barnes's avatar
Jesse Barnes committed
106
		 * bus numbers and pci_dev structures and tries to access
Jesse Barnes's avatar
Jesse Barnes committed
107
		 * them to determine existence. Don't crib during probing.
Jesse Barnes's avatar
Jesse Barnes committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
		 */
		if (done_probing)
			printk("devfn_to_vertex: Invalid bus number %d given.\n", busnum);
		return(NULL);
	}


	/*
	 * Go get the slot&function vertex.
	 * Should call pciio_slot_func_to_name() when ready.
	 */
	slot = PCI_SLOT(devfn);
	func = PCI_FUNC(devfn);

	/*
	 * For a NON Multi-function card the name of the device looks like:
	 * ../pci/1, ../pci/2 ..
	 */
	if (func == 0) {
        	sprintf(name, "%d", slot);
		if (hwgraph_traverse(pci_bus, name, &device_vertex) == 
			GRAPH_SUCCESS) {
			if (device_vertex) {
				return(device_vertex);
			}
		}
	}
			
	/*
	 * This maybe a multifunction card.  It's names look like:
	 * ../pci/1a, ../pci/1b, etc.
	 */
	sprintf(name, "%d%c", slot, 'a'+func);
	if (hwgraph_traverse(pci_bus, name, &device_vertex) != GRAPH_SUCCESS) {
		if (!device_vertex) {
			return(NULL);
		}
	}

	return(device_vertex);
}

/*
 * For the given device, initialize the addresses for both the Device(x) Flush 
 * Write Buffer register and the Xbow Flush Register for the port the PCI bus 
 * is connected.
 */
static void
set_flush_addresses(struct pci_dev *device_dev, 
	struct sn_device_sysdata *device_sysdata)
{
	pciio_info_t pciio_info = pciio_info_get(device_sysdata->vhdl);
	pciio_slot_t pciio_slot = pciio_info_slot_get(pciio_info);
	pcibr_soft_t pcibr_soft = (pcibr_soft_t) pciio_info_mfast_get(pciio_info);
    	bridge_t               *bridge = pcibr_soft->bs_base;
	nasid_t			nasid;

	/*
	 * Get the nasid from the bridge.
	 */
	nasid = NASID_GET(device_sysdata->dma_buf_sync);
169 170 171 172 173
	device_sysdata->dma_buf_sync = (volatile unsigned int *)
		&bridge->b_wr_req_buf[pciio_slot].reg;
	device_sysdata->xbow_buf_sync = (volatile unsigned int *)
		XBOW_PRIO_LINKREGS_PTR(NODE_SWIN_BASE(nasid, 0),
		pcibr_soft->bs_xid);
Jesse Barnes's avatar
Jesse Barnes committed
174 175 176 177 178 179 180 181 182 183 184 185
#ifdef DEBUG
	printk("set_flush_addresses: dma_buf_sync %p xbow_buf_sync %p\n", 
		device_sysdata->dma_buf_sync, device_sysdata->xbow_buf_sync);

printk("set_flush_addresses: dma_buf_sync\n");
	while((volatile unsigned int )*device_sysdata->dma_buf_sync);
printk("set_flush_addresses: xbow_buf_sync\n");
	while((volatile unsigned int )*device_sysdata->xbow_buf_sync);
#endif

}

Jesse Barnes's avatar
Jesse Barnes committed
186 187
struct sn_flush_nasid_entry flush_nasid_list[MAX_NASIDS];

188 189 190 191 192 193 194
/* Initialize the data structures for flushing write buffers after a PIO read.
 * The theory is: 
 * Take an unused int. pin and associate it with a pin that is in use.
 * After a PIO read, force an interrupt on the unused pin, forcing a write buffer flush
 * on the in use pin.  This will prevent the race condition between PIO read responses and 
 * DMA writes.
 */
Jesse Barnes's avatar
Jesse Barnes committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
void
sn_dma_flush_init(unsigned long start, unsigned long end, int idx, int pin, int slot) {
	nasid_t nasid; 
	unsigned long dnasid;
	int wid_num;
	int bus;
	struct sn_flush_device_list *p;
	bridge_t *b;
	bridgereg_t dev_sel;
	extern int isIO9(int);
	int bwin;
	int i;

	nasid = NASID_GET(start);
	wid_num = SWIN_WIDGETNUM(start);
	bus = (start >> 23) & 0x1;
	bwin = BWIN_WINDOWNUM(start);

	if (flush_nasid_list[nasid].widget_p == NULL) {
		flush_nasid_list[nasid].widget_p = (struct sn_flush_device_list **)kmalloc((HUB_WIDGET_ID_MAX+1) *
			sizeof(struct sn_flush_device_list *), GFP_KERNEL);
		memset(flush_nasid_list[nasid].widget_p, 0, (HUB_WIDGET_ID_MAX+1) * sizeof(struct sn_flush_device_list *));
	}
	if (bwin > 0) {
219 220 221 222 223 224 225 226 227 228 229 230
		int itte_index = bwin - 1;
		unsigned long itte;

		itte = HUB_L(IIO_ITTE_GET(nasid, itte_index));
		flush_nasid_list[nasid].iio_itte[bwin] = itte;
		wid_num = (itte >> IIO_ITTE_WIDGET_SHIFT) & 
			  IIO_ITTE_WIDGET_MASK;
		bus = itte & IIO_ITTE_OFFSET_MASK;
		if (bus == 0x4 || bus == 0x8) {
			bus = 0;
		} else {
			bus = 1;
Jesse Barnes's avatar
Jesse Barnes committed
231 232 233
		}
	}

234 235 236
	/* if it's IO9, bus 1, we don't care about slots 1 and 4.  This is
	 * because these are the IOC4 slots and we don't flush them.
	 */
Jesse Barnes's avatar
Jesse Barnes committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
	if (isIO9(nasid) && bus == 0 && (slot == 1 || slot == 4)) {
		return;
	}
	if (flush_nasid_list[nasid].widget_p[wid_num] == NULL) {
		flush_nasid_list[nasid].widget_p[wid_num] = (struct sn_flush_device_list *)kmalloc(
			DEV_PER_WIDGET * sizeof (struct sn_flush_device_list), GFP_KERNEL);
		memset(flush_nasid_list[nasid].widget_p[wid_num], 0, 
			DEV_PER_WIDGET * sizeof (struct sn_flush_device_list));
		p = &flush_nasid_list[nasid].widget_p[wid_num][0];
		for (i=0; i<DEV_PER_WIDGET;i++) {
			p->bus = -1;
			p->pin = -1;
			p++;
		}
	}

	p = &flush_nasid_list[nasid].widget_p[wid_num][0];
	for (i=0;i<DEV_PER_WIDGET; i++) {
		if (p->pin == pin && p->bus == bus) break;
		if (p->pin < 0) {
			p->pin = pin;
			p->bus = bus;
			break;
		}
		p++;
	}

	for (i=0; i<PCI_ROM_RESOURCE; i++) {
		if (p->bar_list[i].start == 0) {
			p->bar_list[i].start = start;
			p->bar_list[i].end = end;
			break;
		}
	}
	b = (bridge_t *)(NODE_SWIN_BASE(nasid, wid_num) | (bus << 23) );

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
	/* If it's IO9, then slot 2 maps to slot 7 and slot 6 maps to slot 8.
	 * To see this is non-trivial.  By drawing pictures and reading manuals and talking
	 * to HW guys, we can see that on IO9 bus 1, slots 7 and 8 are always unused.
	 * Further, since we short-circuit slots  1, 3, and 4 above, we only have to worry
	 * about the case when there is a card in slot 2.  A multifunction card will appear
	 * to be in slot 6 (from an interrupt point of view) also.  That's the  most we'll
	 * have to worry about.  A four function card will overload the interrupt lines in
	 * slot 2 and 6.  
	 * We also need to special case the 12160 device in slot 3.  Fortunately, we have
	 * a spare intr. line for pin 4, so we'll use that for the 12160.
	 * All other buses have slot 3 and 4 and slots 7 and 8 unused.  Since we can only
	 * see slots 1 and 2 and slots 5 and 6 coming through here for those buses (this
	 * is true only on Pxbricks with 2 physical slots per bus), we just need to add
	 * 2 to the slot number to find an unused slot.
	 * We have convinced ourselves that we will never see a case where two different cards
	 * in two different slots will ever share an interrupt line, so there is no need to
	 * special case this.
	 */
Jesse Barnes's avatar
Jesse Barnes committed
291

292 293 294
	if (isIO9(nasid) && ( (IS_ALTIX(nasid) && wid_num == 0xc)
				|| (IS_OPUS(nasid) && wid_num == 0xf) )
				&& bus == 0) {
Jesse Barnes's avatar
Jesse Barnes committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
		if (slot == 2) {
			p->force_int_addr = (unsigned long)&b->b_force_always[6].intr;
			dev_sel = b->b_int_device;
			dev_sel |= (1<<18);
			b->b_int_device = dev_sel;
			dnasid = NASID_GET(virt_to_phys(&p->flush_addr));
			b->p_int_addr_64[6] = (virt_to_phys(&p->flush_addr) & 0xfffffffff) | 
				(dnasid << 36) | (0xfUL << 48);
		} else  if (slot == 3) { /* 12160 SCSI device in IO9 */
			p->force_int_addr = (unsigned long)&b->b_force_always[4].intr;
			dev_sel = b->b_int_device;
			dev_sel |= (2<<12);
			b->b_int_device = dev_sel;
			dnasid = NASID_GET(virt_to_phys(&p->flush_addr));
			b->p_int_addr_64[4] = (virt_to_phys(&p->flush_addr) & 0xfffffffff) | 
				(dnasid << 36) | (0xfUL << 48);
		} else { /* slot == 6 */
			p->force_int_addr = (unsigned long)&b->b_force_always[7].intr;
			dev_sel = b->b_int_device;
			dev_sel |= (5<<21);
			b->b_int_device = dev_sel;
			dnasid = NASID_GET(virt_to_phys(&p->flush_addr));
			b->p_int_addr_64[7] = (virt_to_phys(&p->flush_addr) & 0xfffffffff) | 
				(dnasid << 36) | (0xfUL << 48);
		}
	} else {
		p->force_int_addr = (unsigned long)&b->b_force_always[pin + 2].intr;
		dev_sel = b->b_int_device;
		dev_sel |= ((slot - 1) << ( pin * 3) );
		b->b_int_device = dev_sel;
		dnasid = NASID_GET(virt_to_phys(&p->flush_addr));
		b->p_int_addr_64[pin + 2] = (virt_to_phys(&p->flush_addr) & 0xfffffffff) | 
			(dnasid << 36) | (0xfUL << 48);
	}
}

Jesse Barnes's avatar
Jesse Barnes committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
/*
 * sn_pci_fixup() - This routine is called when platform_pci_fixup() is 
 *	invoked at the end of pcibios_init() to link the Linux pci 
 *	infrastructure to SGI IO Infrasturcture - ia64/kernel/pci.c
 *
 *	Other platform specific fixup can also be done here.
 */
void
sn_pci_fixup(int arg)
{
	struct list_head *ln;
	struct pci_bus *pci_bus = NULL;
	struct pci_dev *device_dev = NULL;
	struct sn_widget_sysdata *widget_sysdata;
	struct sn_device_sysdata *device_sysdata;
346
	pcibr_intr_t intr_handle;
347
	pciio_provider_t *pci_provider;
Jesse Barnes's avatar
Jesse Barnes committed
348
	vertex_hdl_t device_vertex;
Jesse Barnes's avatar
Jesse Barnes committed
349 350 351 352 353 354 355 356
	pciio_intr_line_t lines;
	extern int numnodes;
	int cnode;

	if (arg == 0) {
#ifdef CONFIG_PROC_FS
		extern void register_sn_procfs(void);
#endif
357
		extern void irix_io_init(void);
358
		extern void sn_init_cpei_timer(void);
359 360 361 362
		
		init_hcl();
		irix_io_init();
		
Jesse Barnes's avatar
Jesse Barnes committed
363
		for (cnode = 0; cnode < numnodes; cnode++) {
364 365
			extern void intr_init_vecblk(cnodeid_t);
			intr_init_vecblk(cnode);
Jesse Barnes's avatar
Jesse Barnes committed
366
		} 
367 368 369

		sn_init_cpei_timer();

Jesse Barnes's avatar
Jesse Barnes committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
#ifdef CONFIG_PROC_FS
		register_sn_procfs();
#endif
		return;
	}


	done_probing = 1;

	/*
	 * Initialize the pci bus vertex in the pci_bus struct.
	 */
	for( ln = pci_root_buses.next; ln != &pci_root_buses; ln = ln->next) {
		pci_bus = pci_bus_b(ln);
		widget_sysdata = kmalloc(sizeof(struct sn_widget_sysdata), 
					GFP_KERNEL);
		widget_sysdata->vhdl = pci_bus_to_vertex(pci_bus->number);
		pci_bus->sysdata = (void *)widget_sysdata;
	}

	/*
 	 * set the root start and end so that drivers calling check_region()
	 * won't see a conflict
	 */
	ioport_resource.start  = 0xc000000000000000;
	ioport_resource.end =    0xcfffffffffffffff;

Jesse Barnes's avatar
Jesse Barnes committed
397 398 399 400 401 402
	/*
	 * Set the root start and end for Mem Resource.
	 */
	iomem_resource.start = 0;
	iomem_resource.end = 0xffffffffffffffff;

Jesse Barnes's avatar
Jesse Barnes committed
403 404 405
	/*
	 * Initialize the device vertex in the pci_dev struct.
	 */
406
	while ((device_dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, device_dev)) != NULL) {
Jesse Barnes's avatar
Jesse Barnes committed
407 408 409 410 411 412 413 414 415
		unsigned int irq;
		int idx;
		u16 cmd;
		unsigned long size;
		extern int bit_pos_to_irq(int);

		/* Set the device vertex */

		device_sysdata = kmalloc(sizeof(struct sn_device_sysdata),
416
					 GFP_KERNEL);
417 418 419 420 421
		if (device_sysdata <= 0) {
			printk("sn_pci_fixup: Cannot allocate memory for device sysdata\n");
			return;
		}

Jesse Barnes's avatar
Jesse Barnes committed
422 423
		device_sysdata->vhdl = devfn_to_vertex(device_dev->bus->number, device_dev->devfn);
		device_sysdata->isa64 = 0;
424
		device_vertex = device_sysdata->vhdl;
Jesse Barnes's avatar
Jesse Barnes committed
425 426

		device_dev->sysdata = (void *) device_sysdata;
427
		set_pci_provider(device_sysdata);
Jesse Barnes's avatar
Jesse Barnes committed
428

429 430 431 432 433
		/*
		 * Set the xbridge Device(X) Write Buffer Flush and Xbow Flush 
		 * register addresses.
		 */
		set_flush_addresses(device_dev, device_sysdata);
Jesse Barnes's avatar
Jesse Barnes committed
434 435 436 437 438 439 440 441 442 443 444 445 446
		pci_read_config_word(device_dev, PCI_COMMAND, &cmd);

		/*
		 * Set the resources address correctly.  The assumption here 
		 * is that the addresses in the resource structure has been
		 * read from the card and it was set in the card by our
		 * Infrastructure ..
		 */
		for (idx = 0; idx < PCI_ROM_RESOURCE; idx++) {
			size = 0;
			size = device_dev->resource[idx].end -
				device_dev->resource[idx].start;
			if (size) {
447
				device_dev->resource[idx].start = (unsigned long)pciio_pio_addr(device_vertex, 0, PCIIO_SPACE_WIN(idx), 0, size, 0, 0);
Jesse Barnes's avatar
Jesse Barnes committed
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
				device_dev->resource[idx].start |= __IA64_UNCACHED_OFFSET;
			}
			else
				continue;

			device_dev->resource[idx].end = 
				device_dev->resource[idx].start + size;

			if (device_dev->resource[idx].flags & IORESOURCE_IO)
				cmd |= PCI_COMMAND_IO;

			if (device_dev->resource[idx].flags & IORESOURCE_MEM)
				cmd |= PCI_COMMAND_MEMORY;
		}

		/*
		 * Update the Command Word on the Card.
		 */
		cmd |= PCI_COMMAND_MASTER; /* If the device doesn't support */
					   /* bit gets dropped .. no harm */
		pci_write_config_word(device_dev, PCI_COMMAND, cmd);
469

470 471
		pci_read_config_byte(device_dev, PCI_INTERRUPT_PIN,
				     (unsigned char *)&lines);
472 473 474 475
		device_sysdata = (struct sn_device_sysdata *)device_dev->sysdata;
		device_vertex = device_sysdata->vhdl;
		pci_provider = device_sysdata->pci_provider;
 
476
		irqpdaindr->curr = device_dev;
477
		intr_handle = (pci_provider->intr_alloc)(device_vertex, NULL, lines, device_vertex);
Jesse Barnes's avatar
Jesse Barnes committed
478

479
		irq = intr_handle->bi_irq;
Jesse Barnes's avatar
Jesse Barnes committed
480
		irqpdaindr->device_dev[irq] = device_dev;
481
		(pci_provider->intr_connect)(intr_handle, (intr_func_t)0, (intr_arg_t)0);
Jesse Barnes's avatar
Jesse Barnes committed
482
		device_dev->irq = irq;
483
		register_pcibr_intr(irq, intr_handle);
Jesse Barnes's avatar
Jesse Barnes committed
484 485

		for (idx = 0; idx < PCI_ROM_RESOURCE; idx++) {
486
			int ibits = intr_handle->bi_ibits;
Jesse Barnes's avatar
Jesse Barnes committed
487 488 489 490
			int i;

			size = device_dev->resource[idx].end -
				device_dev->resource[idx].start;
491 492
			if (size == 0)
				continue;
Jesse Barnes's avatar
Jesse Barnes committed
493 494 495 496 497 498 499 500 501 502 503 504 505

			for (i=0; i<8; i++) {
				if (ibits & (1 << i) ) {
					sn_dma_flush_init(device_dev->resource[idx].start, 
							device_dev->resource[idx].end,
							idx,
							i,
							PCI_SLOT(device_dev->devfn));
				}
			}
		}

	}
Jesse Barnes's avatar
Jesse Barnes committed
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
}

/*
 * linux_bus_cvlink() Creates a link between the Linux PCI Bus number 
 *	to the actual hardware component that it represents:
 *	/dev/hw/linux/busnum/0 -> ../../../hw/module/001c01/slab/0/Ibrick/xtalk/15/pci
 *
 *	The bus vertex, when called to devfs_generate_path() returns:
 *		hw/module/001c01/slab/0/Ibrick/xtalk/15/pci
 *		hw/module/001c01/slab/1/Pbrick/xtalk/12/pci-x/0
 *		hw/module/001c01/slab/1/Pbrick/xtalk/12/pci-x/1
 */
void
linux_bus_cvlink(void)
{
	char name[8];
	int index;
	
	for (index=0; index < MAX_PCI_XWIDGET; index++) {
		if (!busnum_to_pcibr_vhdl[index])
			continue;

		sprintf(name, "%x", index);
		(void) hwgraph_edge_add(linux_busnum, busnum_to_pcibr_vhdl[index], 
				name);
	}
}

/*
 * pci_bus_map_create() - Called by pci_bus_to_hcl_cvlink() to finish the job.
 *
 *	Linux PCI Bus numbers are assigned from lowest module_id numbers
 *	(rack/slot etc.) starting from HUB_WIDGET_ID_MAX down to 
 *	HUB_WIDGET_ID_MIN:
 *		widgetnum 15 gets lower Bus Number than widgetnum 14 etc.
 *
 *	Given 2 modules 001c01 and 001c02 we get the following mappings:
 *		001c01, widgetnum 15 = Bus number 0
 *		001c01, widgetnum 14 = Bus number 1
 *		001c02, widgetnum 15 = Bus number 3
 *		001c02, widgetnum 14 = Bus number 4
 *		etc.
 *
 * The rational for starting Bus Number 0 with Widget number 15 is because 
 * the system boot disks are always connected via Widget 15 Slot 0 of the 
 * I-brick.  Linux creates /dev/sd* devices(naming) strating from Bus Number 0 
 * Therefore, /dev/sda1 will be the first disk, on Widget 15 of the lowest 
 * module id(Master Cnode) of the system.
 *	
 */
static int 
Jesse Barnes's avatar
Jesse Barnes committed
557
pci_bus_map_create(vertex_hdl_t xtalk, char * io_moduleid)
Jesse Barnes's avatar
Jesse Barnes committed
558 559
{

Jesse Barnes's avatar
Jesse Barnes committed
560 561 562
	vertex_hdl_t master_node_vertex = NULL;
	vertex_hdl_t xwidget = NULL;
	vertex_hdl_t pci_bus = NULL;
Jesse Barnes's avatar
Jesse Barnes committed
563 564 565 566 567 568
	hubinfo_t hubinfo = NULL;
	xwidgetnum_t widgetnum;
	char pathname[128];
	graph_error_t rv;
	int bus;
	int basebus_num;
Jesse Barnes's avatar
Jesse Barnes committed
569 570
	extern void ioconfig_get_busnum(char *, int *);

Jesse Barnes's avatar
Jesse Barnes committed
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
	int bus_number;

	/*
	 * Loop throught this vertex and get the Xwidgets ..
	 */


	/* PCI devices */

	for (widgetnum = HUB_WIDGET_ID_MAX; widgetnum >= HUB_WIDGET_ID_MIN; widgetnum--) {
		sprintf(pathname, "%d", widgetnum);
		xwidget = NULL;
		
		/*
		 * Example - /hw/module/001c16/Pbrick/xtalk/8 is the xwidget
		 *	     /hw/module/001c16/Pbrick/xtalk/8/pci/1 is device
		 */
		rv = hwgraph_traverse(xtalk, pathname, &xwidget);
		if ( (rv != GRAPH_SUCCESS) ) {
			if (!xwidget) {
				continue;
			}
		}

		sprintf(pathname, "%d/"EDGE_LBL_PCI, widgetnum);
		pci_bus = NULL;
		if (hwgraph_traverse(xtalk, pathname, &pci_bus) != GRAPH_SUCCESS)
			if (!pci_bus) {
				continue;
}

		/*
		 * Assign the correct bus number and also the nasid of this 
		 * pci Xwidget.
		 * 
		 * Should not be any race here ...
		 */
		num_bridges++;
		busnum_to_pcibr_vhdl[num_bridges - 1] = pci_bus;

		/*
		 * Get the master node and from there get the NASID.
		 */
		master_node_vertex = device_master_get(xwidget);
		if (!master_node_vertex) {
			printk("WARNING: pci_bus_map_create: Unable to get .master for vertex 0x%p\n", (void *)xwidget);
		}
	
		hubinfo_get(master_node_vertex, &hubinfo);
		if (!hubinfo) {
			printk("WARNING: pci_bus_map_create: Unable to get hubinfo for master node vertex 0x%p\n", (void *)master_node_vertex);
			return(1);
		} else {
			busnum_to_nid[num_bridges - 1] = hubinfo->h_nasid;
		}

		/*
		 * Pre assign DMA maps needed for 32 Bits Page Map DMA.
		 */
		busnum_to_atedmamaps[num_bridges - 1] = (void *) kmalloc(
631
			sizeof(struct pcibr_dmamap_s) * MAX_ATE_MAPS, GFP_KERNEL);
Jesse Barnes's avatar
Jesse Barnes committed
632 633 634 635
		if (!busnum_to_atedmamaps[num_bridges - 1])
			printk("WARNING: pci_bus_map_create: Unable to precreate ATE DMA Maps for busnum %d vertex 0x%p\n", num_bridges - 1, (void *)xwidget);

		memset(busnum_to_atedmamaps[num_bridges - 1], 0x0, 
636
			sizeof(struct pcibr_dmamap_s) * MAX_ATE_MAPS);
Jesse Barnes's avatar
Jesse Barnes committed
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694

	}

	/*
	 * PCIX devices
	 * We number busses differently for PCI-X devices.
	 * We start from Lowest Widget on up ..
	 */

        (void) ioconfig_get_busnum((char *)io_moduleid, &basebus_num);

	for (widgetnum = HUB_WIDGET_ID_MIN; widgetnum <= HUB_WIDGET_ID_MAX; widgetnum++) {

		/* Do both buses */
		for ( bus = 0; bus < 2; bus++ ) {
			sprintf(pathname, "%d", widgetnum);
			xwidget = NULL;
			
			/*
			 * Example - /hw/module/001c16/Pbrick/xtalk/8 is the xwidget
			 *	     /hw/module/001c16/Pbrick/xtalk/8/pci-x/0 is the bus
			 *	     /hw/module/001c16/Pbrick/xtalk/8/pci-x/0/1 is device
			 */
			rv = hwgraph_traverse(xtalk, pathname, &xwidget);
			if ( (rv != GRAPH_SUCCESS) ) {
				if (!xwidget) {
					continue;
				}
			}
	
			if ( bus == 0 )
				sprintf(pathname, "%d/"EDGE_LBL_PCIX_0, widgetnum);
			else
				sprintf(pathname, "%d/"EDGE_LBL_PCIX_1, widgetnum);
			pci_bus = NULL;
			if (hwgraph_traverse(xtalk, pathname, &pci_bus) != GRAPH_SUCCESS)
				if (!pci_bus) {
					continue;
				}
	
			/*
			 * Assign the correct bus number and also the nasid of this 
			 * pci Xwidget.
			 * 
			 * Should not be any race here ...
			 */
			bus_number = basebus_num + bus + io_brick_map_widget(MODULE_PXBRICK, widgetnum);
#ifdef DEBUG
			printk("bus_number %d basebus_num %d bus %d io %d\n", 
				bus_number, basebus_num, bus, 
				io_brick_map_widget(MODULE_PXBRICK, widgetnum));
#endif
			busnum_to_pcibr_vhdl[bus_number] = pci_bus;
	
			/*
			 * Pre assign DMA maps needed for 32 Bits Page Map DMA.
			 */
			busnum_to_atedmamaps[bus_number] = (void *) kmalloc(
695
				sizeof(struct pcibr_dmamap_s) * MAX_ATE_MAPS, GFP_KERNEL);
Jesse Barnes's avatar
Jesse Barnes committed
696 697 698 699
			if (!busnum_to_atedmamaps[bus_number])
				printk("WARNING: pci_bus_map_create: Unable to precreate ATE DMA Maps for busnum %d vertex 0x%p\n", num_bridges - 1, (void *)xwidget);
	
			memset(busnum_to_atedmamaps[bus_number], 0x0, 
700
				sizeof(struct pcibr_dmamap_s) * MAX_ATE_MAPS);
Jesse Barnes's avatar
Jesse Barnes committed
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
		}
	}

        return(0);
}

/*
 * pci_bus_to_hcl_cvlink() - This routine is called after SGI IO Infrastructure   
 *      initialization has completed to set up the mappings between Xbridge
 *      and logical pci bus numbers.  We also set up the NASID for each of these
 *      xbridges.
 *
 *      Must be called before pci_init() is invoked.
 */
int
pci_bus_to_hcl_cvlink(void) 
{

Jesse Barnes's avatar
Jesse Barnes committed
719 720
	vertex_hdl_t devfs_hdl = NULL;
	vertex_hdl_t xtalk = NULL;
Jesse Barnes's avatar
Jesse Barnes committed
721 722 723
	int rv = 0;
	char name[256];
	char tmp_name[256];
Jesse Barnes's avatar
Jesse Barnes committed
724 725 726
	int i, ii, j;
	char *brick_name;
	extern void ioconfig_bus_new_entries(void);
Jesse Barnes's avatar
Jesse Barnes committed
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749

	/*
	 * Figure out which IO Brick is connected to the Compute Bricks.
	 */
	for (i = 0; i < nummodules; i++) {
		extern int iomoduleid_get(nasid_t);
		moduleid_t iobrick_id;
		nasid_t nasid = -1;
		int nodecnt;
		int n = 0;

		nodecnt = modules[i]->nodecnt;
		for ( n = 0; n < nodecnt; n++ ) {
			nasid = cnodeid_to_nasid(modules[i]->nodes[n]);
			iobrick_id = iomoduleid_get(nasid);
			if ((int)iobrick_id > 0) { /* Valid module id */
				char name[12];
				memset(name, 0, 12);
				format_module_id((char *)&(modules[i]->io[n].moduleid), iobrick_id, MODULE_FORMAT_BRIEF);
			}
		}
	}
				
Jesse Barnes's avatar
Jesse Barnes committed
750
	devfs_hdl = hwgraph_path_to_vertex("hw/module");
Jesse Barnes's avatar
Jesse Barnes committed
751
	for (i = 0; i < nummodules ; i++) {
752
	    for ( j = 0; j < 2; j++ ) {
Jesse Barnes's avatar
Jesse Barnes committed
753 754 755 756 757
		if ( j == 0 )
			brick_name = EDGE_LBL_PXBRICK;
		else
			brick_name = EDGE_LBL_IXBRICK;

Jesse Barnes's avatar
Jesse Barnes committed
758 759 760 761
		for ( ii = 0; ii < 2 ; ii++ ) {
			memset(name, 0, 256);
			memset(tmp_name, 0, 256);
			format_module_id(name, modules[i]->id, MODULE_FORMAT_BRIEF);
Jesse Barnes's avatar
Jesse Barnes committed
762
			sprintf(tmp_name, "/slab/%d/%s/xtalk", geo_slab(modules[i]->geoid[ii]), brick_name);
Jesse Barnes's avatar
Jesse Barnes committed
763 764 765
			strcat(name, tmp_name);
			xtalk = NULL;
			rv = hwgraph_edge_get(devfs_hdl, name, &xtalk);
Jesse Barnes's avatar
Jesse Barnes committed
766 767
			if ( rv == 0 ) 
				pci_bus_map_create(xtalk, (char *)&(modules[i]->io[ii].moduleid));
Jesse Barnes's avatar
Jesse Barnes committed
768
		}
Jesse Barnes's avatar
Jesse Barnes committed
769
	    }
Jesse Barnes's avatar
Jesse Barnes committed
770 771 772 773 774 775 776 777 778 779
	}

	/*
	 * Create the Linux PCI bus number vertex link.
	 */
	(void)linux_bus_cvlink();
	(void)ioconfig_bus_new_entries();

	return(0);
}
780 781 782 783 784 785 786 787 788 789 790 791

/*
 * Ugly hack to get PCI setup until we have a proper ACPI namespace.
 */
extern struct pci_ops sn_pci_ops;
int __init
sn_pci_init (void)
{
#	define PCI_BUSES_TO_SCAN 256
	int i = 0;
	struct pci_controller *controller;

792 793 794
	if (!ia64_platform_is("sn2"))
	    return 0;

795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
	/*
	 * set pci_raw_ops, etc.
	 */
	sn_pci_fixup(0);

	controller = kmalloc(sizeof(struct pci_controller), GFP_KERNEL);
	if (controller) {
		memset(controller, 0, sizeof(struct pci_controller));
		/* just allocate some devices and fill in the pci_dev structs */
		for (i = 0; i < PCI_BUSES_TO_SCAN; i++)
			pci_scan_bus(i, &sn_pci_ops, controller);
	}

	/*
	 * actually find devices and fill in hwgraph structs
	 */
	sn_pci_fixup(1);

	return 0;
}

subsys_initcall(sn_pci_init);