2 * incoherent hypertransport enumeration
3 * originally written by Eric Biederman
5 * 2004.12 yhlu add multi ht chain dynamically support
6 * 2005.11 yhlu add let real sb to use small unitid
7 * 2006.03 stepan cleanups
9 #include <device/pci_def.h>
10 #include <device/pci_ids.h>
11 #include <device/hypertransport_def.h>
13 #ifndef K8_HT_FREQ_1G_SUPPORT
14 #define K8_HT_FREQ_1G_SUPPORT 0
17 #ifndef K8_SCAN_PCI_BUS
18 #define K8_SCAN_PCI_BUS 0
21 #ifndef K8_ALLOCATE_IO_RANGE
22 #define K8_ALLOCATE_IO_RANGE 0
25 /* Do we need to allocate MMIO? Currently we direct the last 64M
26 * to the southbridge link only. We have to remain access to the
27 * 4G-4M range for the southbridge (Flash ROM)
29 #ifndef K8_ALLOCATE_MMIO_RANGE
30 #define K8_ALLOCATE_MMIO_RANGE 0
33 static inline void print_linkn_in (const char *strval, uint8_t byteval)
36 printk_debug("%s%02x\r\n", strval, byteval);
38 print_debug(strval); print_debug_hex8(byteval); print_debug("\r\n");
42 static uint8_t ht_lookup_capability(device_t dev, uint16_t val)
47 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
51 if ((hdr_type == PCI_HEADER_TYPE_NORMAL) ||
52 (hdr_type == PCI_HEADER_TYPE_BRIDGE)) {
53 pos = PCI_CAPABILITY_LIST;
55 if (pos > PCI_CAP_LIST_NEXT) {
56 pos = pci_read_config8(dev, pos);
59 /* loop through the linked list */
62 cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
63 if (cap == PCI_CAP_ID_HT) {
66 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
67 if ((flags >> 13) == val) {
68 /* Entry is a slave or host , success... */
72 pos = pci_read_config8(dev, pos + PCI_CAP_LIST_NEXT);
77 static uint8_t ht_lookup_slave_capability(device_t dev)
79 return ht_lookup_capability(dev, 0); // Slave/Primary Interface Block Format
82 static uint8_t ht_lookup_host_capability(device_t dev)
84 return ht_lookup_capability(dev, 1); // Host/Secondary Interface Block Format
87 static void ht_collapse_previous_enumeration(uint8_t bus, unsigned offset_unitid)
92 // actually, only for one HT device HT chain, and unitid is 0
93 #if HT_CHAIN_UNITID_BASE == 0
99 /* Check if is already collapsed */
100 if((!offset_unitid) || (offset_unitid && (!((HT_CHAIN_END_UNITID_BASE == 0) && (HT_CHAIN_END_UNITID_BASE <HT_CHAIN_UNITID_BASE))))) {
101 dev = PCI_DEV(bus, 0, 0);
102 id = pci_read_config32(dev, PCI_VENDOR_ID);
103 if ( ! ( (id == 0xffffffff) || (id == 0x00000000) ||
104 (id == 0x0000ffff) || (id == 0xffff0000) ) ) {
109 /* Spin through the devices and collapse any previous
110 * hypertransport enumeration.
112 for(dev = PCI_DEV(bus, 1, 0); dev <= PCI_DEV(bus, 0x1f, 0x7); dev += PCI_DEV(0, 1, 0)) {
117 id = pci_read_config32(dev, PCI_VENDOR_ID);
118 if ((id == 0xffffffff) || (id == 0x00000000) ||
119 (id == 0x0000ffff) || (id == 0xffff0000)) {
123 pos = ht_lookup_slave_capability(dev);
128 /* Clear the unitid */
129 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
131 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
135 static uint16_t ht_read_freq_cap(device_t dev, uint8_t pos)
137 /* Handle bugs in valid hypertransport frequency reporting */
141 freq_cap = pci_read_config16(dev, pos);
142 freq_cap &= ~(1 << HT_FREQ_VENDOR); /* Ignore Vendor HT frequencies */
144 id = pci_read_config32(dev, 0);
146 /* AMD 8131 Errata 48 */
147 if (id == (PCI_VENDOR_ID_AMD | (PCI_DEVICE_ID_AMD_8131_PCIX << 16))) {
148 freq_cap &= ~(1 << HT_FREQ_800Mhz);
152 /* AMD 8151 Errata 23 */
153 if (id == (PCI_VENDOR_ID_AMD | (PCI_DEVICE_ID_AMD_8151_SYSCTRL << 16))) {
154 freq_cap &= ~(1 << HT_FREQ_800Mhz);
158 /* AMD K8 Unsupported 1Ghz? */
159 if (id == (PCI_VENDOR_ID_AMD | (0x1100 << 16))) {
160 #if K8_HT_FREQ_1G_SUPPORT == 1
161 if (is_cpu_pre_e0()) { // only E0 later support 1GHz
162 freq_cap &= ~(1 << HT_FREQ_1000Mhz);
165 freq_cap &= ~(1 << HT_FREQ_1000Mhz);
171 #define LINK_OFFS(CTRL, WIDTH,FREQ,FREQ_CAP) \
172 (((CTRL & 0xff) << 24) | ((WIDTH & 0xff) << 16) | ((FREQ & 0xff) << 8) | (FREQ_CAP & 0xFF))
174 #define LINK_CTRL(OFFS) ((OFFS >> 24) & 0xFF)
175 #define LINK_WIDTH(OFFS) ((OFFS >> 16) & 0xFF)
176 #define LINK_FREQ(OFFS) ((OFFS >> 8) & 0xFF)
177 #define LINK_FREQ_CAP(OFFS) ((OFFS) & 0xFF)
179 #define PCI_HT_HOST_OFFS LINK_OFFS( \
180 PCI_HT_CAP_HOST_CTRL, \
181 PCI_HT_CAP_HOST_WIDTH, \
182 PCI_HT_CAP_HOST_FREQ, \
183 PCI_HT_CAP_HOST_FREQ_CAP)
185 #define PCI_HT_SLAVE0_OFFS LINK_OFFS( \
186 PCI_HT_CAP_SLAVE_CTRL0, \
187 PCI_HT_CAP_SLAVE_WIDTH0, \
188 PCI_HT_CAP_SLAVE_FREQ0, \
189 PCI_HT_CAP_SLAVE_FREQ_CAP0)
191 #define PCI_HT_SLAVE1_OFFS LINK_OFFS( \
192 PCI_HT_CAP_SLAVE_CTRL1, \
193 PCI_HT_CAP_SLAVE_WIDTH1, \
194 PCI_HT_CAP_SLAVE_FREQ1, \
195 PCI_HT_CAP_SLAVE_FREQ_CAP1)
197 static int ht_optimize_link(
198 device_t dev1, uint8_t pos1, unsigned offs1,
199 device_t dev2, uint8_t pos2, unsigned offs2)
201 static const uint8_t link_width_to_pow2[]= { 3, 4, 0, 5, 1, 2, 0, 0 };
202 static const uint8_t pow2_to_link_width[] = { 0x7, 4, 5, 0, 1, 3 };
203 uint16_t freq_cap1, freq_cap2;
204 uint8_t width_cap1, width_cap2, width, old_width, ln_width1, ln_width2;
205 uint8_t freq, old_freq;
207 /* Set link width and frequency */
209 /* Initially assume everything is already optimized and I don't need a reset */
212 /* Get the frequency capabilities */
213 freq_cap1 = ht_read_freq_cap(dev1, pos1 + LINK_FREQ_CAP(offs1));
214 freq_cap2 = ht_read_freq_cap(dev2, pos2 + LINK_FREQ_CAP(offs2));
216 /* Calculate the highest possible frequency */
217 freq = log2(freq_cap1 & freq_cap2);
219 /* See if I am changing the link freqency */
220 old_freq = pci_read_config8(dev1, pos1 + LINK_FREQ(offs1));
222 needs_reset |= old_freq != freq;
223 old_freq = pci_read_config8(dev2, pos2 + LINK_FREQ(offs2));
225 needs_reset |= old_freq != freq;
227 /* Set the Calulcated link frequency */
228 pci_write_config8(dev1, pos1 + LINK_FREQ(offs1), freq);
229 pci_write_config8(dev2, pos2 + LINK_FREQ(offs2), freq);
231 /* Get the width capabilities */
232 width_cap1 = pci_read_config8(dev1, pos1 + LINK_WIDTH(offs1));
233 width_cap2 = pci_read_config8(dev2, pos2 + LINK_WIDTH(offs2));
235 /* Calculate dev1's input width */
236 ln_width1 = link_width_to_pow2[width_cap1 & 7];
237 ln_width2 = link_width_to_pow2[(width_cap2 >> 4) & 7];
238 if (ln_width1 > ln_width2) {
239 ln_width1 = ln_width2;
241 width = pow2_to_link_width[ln_width1];
242 /* Calculate dev1's output width */
243 ln_width1 = link_width_to_pow2[(width_cap1 >> 4) & 7];
244 ln_width2 = link_width_to_pow2[width_cap2 & 7];
245 if (ln_width1 > ln_width2) {
246 ln_width1 = ln_width2;
248 width |= pow2_to_link_width[ln_width1] << 4;
250 /* See if I am changing dev1's width */
251 old_width = pci_read_config8(dev1, pos1 + LINK_WIDTH(offs1) + 1);
253 needs_reset |= old_width != width;
255 /* Set dev1's widths */
256 pci_write_config8(dev1, pos1 + LINK_WIDTH(offs1) + 1, width);
258 /* Calculate dev2's width */
259 width = ((width & 0x70) >> 4) | ((width & 0x7) << 4);
261 /* See if I am changing dev2's width */
262 old_width = pci_read_config8(dev2, pos2 + LINK_WIDTH(offs2) + 1);
264 needs_reset |= old_width != width;
266 /* Set dev2's widths */
267 pci_write_config8(dev2, pos2 + LINK_WIDTH(offs2) + 1, width);
272 #if (USE_DCACHE_RAM == 1) && (K8_SCAN_PCI_BUS == 1)
274 #if RAMINIT_SYSINFO == 1
275 static void ht_setup_chainx(device_t udev, uint8_t upos, uint8_t bus,
276 unsigned offset_unitid, struct sys_info *sysinfo);
278 static int scan_pci_bus( unsigned bus , struct sys_info *sysinfo)
280 static int ht_setup_chainx(device_t udev, uint8_t upos, uint8_t bus,
281 unsigned offset_unitid);
283 static int scan_pci_bus( unsigned bus)
286 /* Here we already can access PCI_DEV(bus, 0, 0) to
287 * PCI_DEV(bus, 0x1f, 0x7).
289 * So scan these devices to find out whether there are more bridges.
291 * - If we find a pci bridge, set the bus number in the bridge, and
292 * continue with the next device.
294 * - For hypertransport bridges, set the bus number in the bridge and
295 * call ht_setup_chainx(), and scan_pci_bus()
302 new_bus = (bus & 0xff); // mask out the reset_needed
306 } else if (new_bus<0x80) {
308 } else if (new_bus<0xc0) {
317 #if CONFIG_USE_INIT == 1
318 printk_debug("bus_num=%02x\r\n", bus);
322 for (devfn = 0; devfn <= 0xff; devfn++) {
328 dev = PCI_DEV((bus & 0xff), ((devfn>>3) & 0x1f), (devfn & 0x7));
329 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
330 class = pci_read_config16(dev, PCI_CLASS_DEVICE);
333 #if CONFIG_USE_INIT == 1
334 if(hdr_type !=0xff ) {
335 printk_debug("dev=%02x fn=%02x hdr_type=%02x class=%04x\r\n",
336 (devfn>>3)& 0x1f, (devfn & 0x7), hdr_type, class);
340 switch(hdr_type & 0x7f) { /* header type */
341 case PCI_HEADER_TYPE_BRIDGE:
342 if (class != PCI_CLASS_BRIDGE_PCI) goto bad;
343 /* set the bus range dev */
345 /* Clear all status bits and turn off memory, I/O and master enables. */
346 cr = pci_read_config16(dev, PCI_COMMAND);
347 pci_write_config16(dev, PCI_COMMAND, 0x0000);
348 pci_write_config16(dev, PCI_STATUS, 0xffff);
350 buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
354 buses |= (((unsigned int) (bus & 0xff) << 0) |
355 ((unsigned int) (new_bus & 0xff) << 8) |
356 ((unsigned int) max_bus << 16));
357 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
359 /* Here we need to figure out if dev is a ht
360 * bridge. If it is, we need to call
361 * ht_setup_chainx() first
363 * Not verified --- yhlu
366 uint8_t upos; // is this valid C?
368 // one func one ht sub
369 upos = ht_lookup_host_capability(dev);
371 if (upos) { // sub ht chain
373 busn = (new_bus & 0xff);
374 /* Make certain the HT bus is not enumerated */
375 ht_collapse_previous_enumeration(busn, 0);
376 /* scan the ht chain */
377 #if RAMINIT_SYSINFO == 1
378 ht_setup_chainx(dev,upos,busn, 0, sysinfo); // don't need offset unitid
380 new_bus |= (ht_setup_chainx(dev, upos, busn, 0)<<16); // store reset_needed to upword
384 #if RAMINIT_SYSINFO == 1
385 new_bus = scan_pci_bus(new_bus, sysinfo);
387 new_bus = scan_pci_bus(new_bus);
389 /* set real max bus num in that */
391 buses = (buses & 0xff00ffff) |
392 ((unsigned int) (new_bus & 0xff) << 16);
393 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
395 pci_write_config16(dev, PCI_COMMAND, cr);
403 /* if this is not a multi function device,
404 * or the device is not present don't waste
405 * time probing another function.
406 * Skip to next device.
409 if ( ((devfn & 0x07) == 0x00) && ((hdr_type & 0x80) != 0x80))
419 #if RAMINIT_SYSINFO == 1
420 static void ht_setup_chainx(device_t udev, uint8_t upos, uint8_t bus, unsigned offset_unitid, struct sys_info *sysinfo)
422 static int ht_setup_chainx(device_t udev, uint8_t upos, uint8_t bus, unsigned offset_unitid)
425 // execute this function even with HT_CHAIN_UNITID_BASE == 0,
426 // because of the end_of_chain check, and we need it to
427 // optimize the links
429 uint8_t next_unitid, last_unitid;
432 #if RAMINIT_SYSINFO == 0
433 int reset_needed = 0;
436 #if HT_CHAIN_END_UNITID_BASE < HT_CHAIN_UNITID_BASE
437 // record the device id of last ht device, so we can set the
438 // unit id to HT_CHAIN_END_UNITID_BASE
439 unsigned real_last_unitid;
440 uint8_t real_last_pos;
444 uoffs = PCI_HT_HOST_OFFS;
445 next_unitid = (offset_unitid) ? HT_CHAIN_UNITID_BASE:1;
450 uint16_t flags, ctrl;
454 /* Wait until the link initialization is complete */
456 ctrl = pci_read_config16(udev, upos + LINK_CTRL(uoffs));
457 /* Is this the end of the hypertransport chain? */
458 if (ctrl & (1 << 6)) {
462 if (ctrl & ((1 << 4) | (1 << 8))) {
464 * Either the link has failed, or we have
466 * Sometimes this can happen due to link
467 * retrain, so lets knock it down and see
470 ctrl |= ((1 << 6) | (1 <<8)); // Link fail + Crc
471 pci_write_config16(udev, upos + LINK_CTRL(uoffs), ctrl);
472 ctrl = pci_read_config16(udev, upos + LINK_CTRL(uoffs));
473 if (ctrl & ((1 << 4) | (1 << 8))) {
474 print_err("Detected error on Hypertransport Link\n");
478 } while((ctrl & (1 << 5)) == 0);
480 device_t dev = PCI_DEV(bus, 0, 0);
481 last_unitid = next_unitid;
483 id = pci_read_config32(dev, PCI_VENDOR_ID);
485 /* If the chain is enumerated quit */
486 if ( (id == 0xffffffff) || (id == 0x00000000) ||
487 (id == 0x0000ffff) || (id == 0xffff0000))
492 pos = ht_lookup_slave_capability(dev);
494 print_err("udev="); print_err_hex32(udev);
495 print_err("\tupos="); print_err_hex32(upos);
496 print_err("\tuoffs="); print_err_hex32(uoffs);
497 print_err("\tHT link capability not found\r\n");
501 /* Update the Unitid of the current device */
502 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
503 flags &= ~0x1f; /* mask out the bse Unit ID */
504 flags |= next_unitid & 0x1f;
505 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
507 /* Note the change in device number */
508 dev = PCI_DEV(bus, next_unitid, 0);
510 /* Compute the number of unitids consumed */
511 count = (flags >> 5) & 0x1f;
512 #if HT_CHAIN_END_UNITID_BASE < HT_CHAIN_UNITID_BASE
514 real_last_unitid = next_unitid;
520 next_unitid += count;
522 /* Find which side of the ht link we are on, by reading
523 * which direction our last write to PCI_CAP_FLAGS came
526 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
527 offs = ((flags>>10) & 1) ? PCI_HT_SLAVE1_OFFS : PCI_HT_SLAVE0_OFFS;
529 #if RAMINIT_SYSINFO == 1
530 /* store the link pair here and we will setup the
531 * Hypertransport link later, after we get final FID/VID
534 struct link_pair_st *link_pair = &sysinfo->link_pair[sysinfo->link_pair_num];
535 link_pair->udev = udev;
536 link_pair->upos = upos;
537 link_pair->uoffs = uoffs;
538 link_pair->dev = dev;
539 link_pair->pos = pos;
540 link_pair->offs = offs;
541 sysinfo->link_pair_num++;
544 reset_needed |= ht_optimize_link(udev, upos, uoffs, dev, pos, offs);
547 /* Remember the location of the last device */
550 uoffs = ( offs != PCI_HT_SLAVE0_OFFS ) ? PCI_HT_SLAVE0_OFFS : PCI_HT_SLAVE1_OFFS;
552 } while((last_unitid != next_unitid) && (next_unitid <= 0x1f));
556 #if HT_CHAIN_END_UNITID_BASE < HT_CHAIN_UNITID_BASE
557 if(offset_unitid && (ht_dev_num>0) ) {
560 flags = pci_read_config16(PCI_DEV(bus,real_last_unitid,0), real_last_pos + PCI_CAP_FLAGS);
562 flags |= HT_CHAIN_END_UNITID_BASE & 0x1f;
563 pci_write_config16(PCI_DEV(bus, real_last_unitid, 0), real_last_pos + PCI_CAP_FLAGS, flags);
565 #if RAMINIT_SYSINFO == 1
566 // Here need to change the dev in the array
567 for(i=0;i<sysinfo->link_pair_num;i++)
569 struct link_pair_st *link_pair = &sysinfo->link_pair[i];
570 if(link_pair->udev == PCI_DEV(bus, real_last_unitid, 0)) {
571 link_pair->udev = PCI_DEV(bus, HT_CHAIN_END_UNITID_BASE, 0);
574 if(link_pair->dev == PCI_DEV(bus, real_last_unitid, 0)) {
575 link_pair->dev = PCI_DEV(bus, HT_CHAIN_END_UNITID_BASE, 0);
583 #if RAMINIT_SYSINFO == 0
591 #if RAMINIT_SYSINFO == 1
592 static void ht_setup_chain(device_t udev, unsigned upos, struct sys_info *sysinfo)
594 static int ht_setup_chain(device_t udev, unsigned upos)
597 unsigned offset_unitid = 0;
598 #if HT_CHAIN_UNITID_BASE != 1
602 /* Assumption the HT chain that is bus 0 has the HT I/O Hub on it.
603 * On most boards this just happens. If a cpu has multiple
604 * non Coherent links the appropriate bus registers for the
605 * links needs to be programed to point at bus 0.
608 /* Make certain the HT bus is not enumerated */
609 ht_collapse_previous_enumeration(0, 0);
611 #if HT_CHAIN_UNITID_BASE != 1
615 #if RAMINIT_SYSINFO == 1
616 ht_setup_chainx(udev, upos, 0, offset_unitid, sysinfo);
618 return ht_setup_chainx(udev, upos, 0, offset_unitid);
623 static int optimize_link_read_pointer(uint8_t node, uint8_t linkn,
624 uint8_t linkt, uint8_t val)
626 uint32_t dword, dword_old;
629 /* This works on an Athlon64 because unimplemented links return 0 */
630 dword = pci_read_config32(PCI_DEV(0,0x18+node,0), 0x98 + (linkn * 0x20));
631 link_type = dword & 0xff;
633 dword_old = dword = pci_read_config32(PCI_DEV(0,0x18+node,3), 0xdc);
635 /* coherent link only linkt = 3, non coherent = 7*/
636 if ( (link_type & 7) == linkt ) {
637 dword &= ~( 0xff<<(linkn *8) );
638 dword |= val << (linkn *8);
641 if (dword != dword_old) {
642 pci_write_config32(PCI_DEV(0,0x18+node,3), 0xdc, dword);
649 static int optimize_link_read_pointers_chain(uint8_t ht_c_num)
656 for (i = 0; i < ht_c_num; i++) {
658 uint8_t nodeid, linkn;
662 reg = pci_read_config32(PCI_DEV(0,0x18,1), 0xe0 + i * 4);
664 nodeid = ((reg & 0xf0)>>4); // nodeid
665 linkn = ((reg & 0xf00)>>8); // link n
666 busn = (reg & 0xff0000)>>16; //busn
668 reg = pci_read_config32( PCI_DEV(busn, 1, 0), PCI_VENDOR_ID);
669 if ( (reg & 0xffff) == PCI_VENDOR_ID_AMD) {
671 } else if ( (reg & 0xffff) == PCI_VENDOR_ID_NVIDIA ) {
677 reset_needed |= optimize_link_read_pointer(nodeid, linkn, 0x07, val);
684 #if RAMINIT_SYSINFO == 1
685 static void ht_setup_chains(uint8_t ht_c_num, struct sys_info *sysinfo)
687 static int ht_setup_chains(uint8_t ht_c_num)
690 /* Assumption: The HT chain that is bus 0 has the HT I/O Hub on it.
691 * On most boards this just happens. If a cpu has multiple
692 * non coherent links the appropriate bus registers for the
693 * links needs to be programed to point at bus 0.
700 #if RAMINIT_SYSINFO == 0
701 int reset_needed = 0;
703 sysinfo->link_pair_num = 0;
706 // first one is SB Chain
707 for (i = 0; i < ht_c_num; i++) {
713 #if (USE_DCACHE_RAM == 1) && (K8_SCAN_PCI_BUS == 1)
716 unsigned offset_unitid = 0;
718 reg = pci_read_config32(PCI_DEV(0,0x18,1), 0xe0 + i * 4);
720 // We need to setup 0x94, 0xb4, and 0xd4 according to reg
722 // nodeid; it will decide 0x18 or 0x19
723 devpos = ((reg & 0xf0)>>4)+0x18;
725 // link n; it will decide 0x94 or 0xb4, 0x0xd4;
726 regpos = ((reg & 0xf00)>>8) * 0x20 + 0x94;
728 busn = (reg & 0xff0000)>>16;
730 dword = pci_read_config32( PCI_DEV(0, devpos, 0), regpos) ;
731 dword &= ~(0xffff<<8);
732 dword |= (reg & 0xffff0000)>>8;
733 pci_write_config32( PCI_DEV(0, devpos,0), regpos , dword);
736 #if HT_CHAIN_UNITID_BASE != 1
737 #if SB_HT_CHAIN_UNITID_OFFSET_ONLY == 1
738 if(i==0) // to check if it is sb ht chain
743 /* Make certain the HT bus is not enumerated */
744 ht_collapse_previous_enumeration(busn, offset_unitid);
746 upos = ((reg & 0xf00)>>8) * 0x20 + 0x80;
747 udev = PCI_DEV(0, devpos, 0);
749 #if RAMINIT_SYSINFO == 1
750 ht_setup_chainx(udev,upos,busn, offset_unitid, sysinfo); // all not
752 reset_needed |= ht_setup_chainx(udev,upos,busn, offset_unitid); //all not
755 #if (USE_DCACHE_RAM == 1) && (K8_SCAN_PCI_BUS == 1)
756 /* You can not use use this in romcc, because recursive
757 * function calls in romcc will kill you
759 bus = busn; // we need 32 bit
760 #if RAMINIT_SYSINFO == 1
761 scan_pci_bus(bus, sysinfo);
763 // take out reset_needed that is stored in upword
764 reset_needed |= (scan_pci_bus(bus)>>16);
769 #if RAMINIT_SYSINFO == 0
770 reset_needed |= optimize_link_read_pointers_chain(ht_c_num);
777 static inline unsigned get_nodes(void)
779 return ((pci_read_config32(PCI_DEV(0, 0x18, 0), 0x60)>>4) & 7) + 1;
783 #if RAMINIT_SYSINFO == 1
784 static void ht_setup_chains_x(struct sys_info *sysinfo)
786 static int ht_setup_chains_x(void)
795 #if K8_ALLOCATE_IO_RANGE == 1
796 unsigned next_io_base;
801 /* read PCI_DEV(0,0x18,0) 0x64 bit [8:9] to find out SbLink m */
802 reg = pci_read_config32(PCI_DEV(0, 0x18, 0), 0x64);
803 /* update PCI_DEV(0, 0x18, 1) 0xe0 to 0x05000m03, and next_busn=0x3f+1 */
804 print_linkn_in("SBLink=", ((reg>>8) & 3) );
805 #if RAMINIT_SYSINFO == 1
806 sysinfo->sblnk = (reg>>8) & 3;
808 sysinfo->nodes = nodes;
810 tempreg = 3 | ( 0<<4) | (((reg>>8) & 3)<<8) | (0<<16)| (0x3f<<24);
811 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xe0, tempreg);
813 /* 0 will be used ht chain with SB we need to keep SB in bus 0 in auto stage */
816 #if K8_ALLOCATE_IO_RANGE == 1
817 /* io range allocation */
818 tempreg = 0 | (((reg>>8) & 0x3) << 4 )| (0x3<<12); //limit
819 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xC4, tempreg);
820 tempreg = 3 | ( 3<<4) | (0<<12); //base
821 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xC0, tempreg);
822 next_io_base = 0x3+0x1;
826 for(ht_c_num=1;ht_c_num<4; ht_c_num++) {
827 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4, 0);
828 /* io range allocation */
829 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xc4 + ht_c_num * 8, 0);
830 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xc0 + ht_c_num * 8, 0);
833 for(nodeid=0; nodeid<nodes; nodeid++) {
836 dev = PCI_DEV(0, 0x18+nodeid,0);
837 for(linkn = 0; linkn<3; linkn++) {
840 regpos = 0x98 + 0x20 * linkn;
841 reg = pci_read_config32(dev, regpos);
843 /* skip if link is non conherent or not connected*/
844 if ((reg & 0x17) != 7) continue;
846 print_linkn_in("NC node|link=",
847 ((nodeid & 0xf)<<4)|(linkn & 0xf));
849 tempreg = 3 | (nodeid <<4) | (linkn<<8);
851 /* compare (temp & 0xffff) with
852 * (PCI(0, 0x18, 1) 0xe0 to 0xec & 0xfffff)
854 for(ht_c_num=0;ht_c_num<4; ht_c_num++) {
855 reg = pci_read_config32( PCI_DEV(0, 0x18, 1),
856 0xe0 + ht_c_num * 4);
858 if ( ((reg & 0xffff) == (tempreg & 0xffff))
859 || ((reg & 0xffff) == 0x0000) ) {
865 /* used up the maximum allowed 4 non conherent links */
866 if(ht_c_num == 4) break;
868 /* update to 0xe0...*/
869 if((reg & 0xf) == 3) continue; /* SbLink so don't touch it */
871 print_linkn_in("\tbusn=", next_busn);
873 tempreg |= (next_busn<<16)|((next_busn+0x3f)<<24);
874 pci_write_config32(PCI_DEV(0, 0x18, 1),
875 0xe0 + ht_c_num * 4, tempreg);
879 #if K8_ALLOCATE_IO_RANGE == 1
880 /* io range allocation */
883 tempreg = nodeid | (linkn<<4) | ((next_io_base+0x3)<<12);
884 pci_write_config32( PCI_DEV(0, 0x18, 1),
885 0xC4 + ht_c_num * 8, tempreg);
887 // base :ISA and VGA ?
888 tempreg = 3 /*| ( 3<<4)*/ | (next_io_base<<12);
889 pci_write_config32(PCI_DEV(0, 0x18, 1),
890 0xC0 + ht_c_num * 8, tempreg);
892 next_io_base += 0x3+0x1;
897 /* update 0xe0, 0xe4, 0xe8, 0xec from PCI_DEV(0, 0x18,1)
898 * to PCI_DEV(0, 0x19,1) to PCI_DEV(0, 0x1f,1);
901 for(nodeid = 1; nodeid<nodes; nodeid++) {
904 dev = PCI_DEV(0, 0x18+nodeid,1);
905 for(i = 0; i< 4; i++) {
907 regpos = 0xe0 + i * 4;
908 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), regpos);
909 pci_write_config32(dev, regpos, reg);
912 #if K8_ALLOCATE_IO_RANGE == 1
913 /* io range allocation */
914 for(i = 0; i< 4; i++) {
916 regpos = 0xc4 + i * 8;
917 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), regpos);
918 pci_write_config32(dev, regpos, reg);
920 for(i = 0; i< 4; i++) {
922 regpos = 0xc0 + i * 8;
923 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), regpos);
924 pci_write_config32(dev, regpos, reg);
929 /* recount ht_c_num*/
931 for(ht_c_num=0;ht_c_num<4; ht_c_num++) {
932 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4);
933 if(((reg & 0xf) != 0x0)) {
938 #if RAMINIT_SYSINFO == 1
939 sysinfo->ht_c_num = i;
940 ht_setup_chains(i, sysinfo);
942 return ht_setup_chains(i);
947 #if RAMINIT_SYSINFO == 1
948 static int optimize_link_incoherent_ht(struct sys_info *sysinfo)
950 // We need to use the recorded link pair info to optimize the link
952 int reset_needed = 0;
954 unsigned link_pair_num = sysinfo->link_pair_num;
956 for(i=0; i< link_pair_num; i++) {
957 struct link_pair_st *link_pair= &sysinfo->link_pair[i];
958 reset_needed |= ht_optimize_link(link_pair->udev,
959 link_pair->upos, link_pair->uoffs,
960 link_pair->dev, link_pair->pos, link_pair->offs);
963 reset_needed |= optimize_link_read_pointers(sysinfo->ht_c_num);
971 static unsigned get_sblnk(void)
974 /* read PCI_DEV(0,0x18,0) 0x64 bit [8:9] to find out SbLink m */
975 reg = pci_read_config32(PCI_DEV(0, 0x18, 0), 0x64);
976 return ((reg>>8) & 3) ;
979 /* Look up a which bus a given node/link combination is on.
980 * return 0 when we can't find the answer.
983 static unsigned node_link_to_bus(unsigned node, unsigned link)
987 for(reg = 0xE0; reg < 0xF0; reg += 0x04) {
989 config_map = pci_read_config32(PCI_DEV(0, 0x18, 1), reg);
990 if ((config_map & 3) != 3) {
993 if ((((config_map >> 4) & 7) == node) &&
994 (((config_map >> 8) & 3) == link))
996 return (config_map >> 16) & 0xff;