2 This should be done by Eric
3 2004.12 yhlu add multi ht chain dynamically support
4 2005.11 yhlu add let real sb to use small unitid
6 #include <device/pci_def.h>
7 #include <device/pci_ids.h>
8 #include <device/hypertransport_def.h>
10 #ifndef K8_HT_FREQ_1G_SUPPORT
11 #define K8_HT_FREQ_1G_SUPPORT 0
14 #ifndef K8_SCAN_PCI_BUS
15 #define K8_SCAN_PCI_BUS 0
18 #ifndef K8_ALLOCATE_IO_RANGE
19 #define K8_ALLOCATE_IO_RANGE 0
22 // Do we need allocate MMIO? Current We direct last 64M to sblink only, We can not lose access to last 4M range to ROM
23 #ifndef K8_ALLOCATE_MMIO_RANGE
24 #define K8_ALLOCATE_MMIO_RANGE 0
27 static inline void print_linkn_in (const char *strval, uint8_t byteval)
30 printk_debug("%s%02x\r\n", strval, byteval);
32 print_debug(strval); print_debug_hex8(byteval); print_debug("\r\n");
36 static uint8_t ht_lookup_capability(device_t dev, uint16_t val)
41 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
45 if ((hdr_type == PCI_HEADER_TYPE_NORMAL) ||
46 (hdr_type == PCI_HEADER_TYPE_BRIDGE)) {
47 pos = PCI_CAPABILITY_LIST;
49 if (pos > PCI_CAP_LIST_NEXT) {
50 pos = pci_read_config8(dev, pos);
52 while(pos != 0) { /* loop through the linked list */
54 cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
55 if (cap == PCI_CAP_ID_HT) {
58 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
59 if ((flags >> 13) == val) {
60 /* Entry is a slave or host , success... */
64 pos = pci_read_config8(dev, pos + PCI_CAP_LIST_NEXT);
69 static uint8_t ht_lookup_slave_capability(device_t dev)
71 return ht_lookup_capability(dev, 0); // Slave/Primary Interface Block Format
74 static uint8_t ht_lookup_host_capability(device_t dev)
76 return ht_lookup_capability(dev, 1); // Host/Secondary Interface Block Format
79 static void ht_collapse_previous_enumeration(uint8_t bus, unsigned offset_unitid)
84 //actually, only for one HT device HT chain, and unitid is 0
85 #if HT_CHAIN_UNITID_BASE == 0
91 /* Check if is already collapsed */
92 if((!offset_unitid) || (offset_unitid && (!((HT_CHAIN_END_UNITID_BASE == 0) && (HT_CHAIN_END_UNITID_BASE <HT_CHAIN_UNITID_BASE))))) {
93 dev = PCI_DEV(bus, 0, 0);
94 id = pci_read_config32(dev, PCI_VENDOR_ID);
95 if ( ! ( (id == 0xffffffff) || (id == 0x00000000) ||
96 (id == 0x0000ffff) || (id == 0xffff0000) ) ) {
101 /* Spin through the devices and collapse any previous
102 * hypertransport enumeration.
104 for(dev = PCI_DEV(bus, 1, 0); dev <= PCI_DEV(bus, 0x1f, 0x7); dev += PCI_DEV(0, 1, 0)) {
109 id = pci_read_config32(dev, PCI_VENDOR_ID);
110 if ((id == 0xffffffff) || (id == 0x00000000) ||
111 (id == 0x0000ffff) || (id == 0xffff0000)) {
115 #if CK804_DEVN_BASE==0
117 // CK804 UnitID changes not use
118 if(id == 0x005e10de) {
124 pos = ht_lookup_slave_capability(dev);
129 /* Clear the unitid */
130 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
132 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
136 static uint16_t ht_read_freq_cap(device_t dev, uint8_t pos)
138 /* Handle bugs in valid hypertransport frequency reporting */
142 freq_cap = pci_read_config16(dev, pos);
143 freq_cap &= ~(1 << HT_FREQ_VENDOR); /* Ignore Vendor HT frequencies */
145 id = pci_read_config32(dev, 0);
147 /* AMD 8131 Errata 48 */
148 if (id == (PCI_VENDOR_ID_AMD | (PCI_DEVICE_ID_AMD_8131_PCIX << 16))) {
149 freq_cap &= ~(1 << HT_FREQ_800Mhz);
153 /* AMD 8151 Errata 23 */
154 if (id == (PCI_VENDOR_ID_AMD | (PCI_DEVICE_ID_AMD_8151_SYSCTRL << 16))) {
155 freq_cap &= ~(1 << HT_FREQ_800Mhz);
159 /* AMD K8 Unsupported 1Ghz? */
160 if (id == (PCI_VENDOR_ID_AMD | (0x1100 << 16))) {
161 #if K8_HT_FREQ_1G_SUPPORT == 1
162 if (is_cpu_pre_e0()) { // only E0 later support 1GHz
163 freq_cap &= ~(1 << HT_FREQ_1000Mhz);
166 freq_cap &= ~(1 << HT_FREQ_1000Mhz);
172 #define LINK_OFFS(CTRL, WIDTH,FREQ,FREQ_CAP) \
173 (((CTRL & 0xff) << 24) | ((WIDTH & 0xff) << 16) | ((FREQ & 0xff) << 8) | (FREQ_CAP & 0xFF))
175 #define LINK_CTRL(OFFS) ((OFFS >> 24) & 0xFF)
176 #define LINK_WIDTH(OFFS) ((OFFS >> 16) & 0xFF)
177 #define LINK_FREQ(OFFS) ((OFFS >> 8) & 0xFF)
178 #define LINK_FREQ_CAP(OFFS) ((OFFS) & 0xFF)
180 #define PCI_HT_HOST_OFFS LINK_OFFS( \
181 PCI_HT_CAP_HOST_CTRL, \
182 PCI_HT_CAP_HOST_WIDTH, \
183 PCI_HT_CAP_HOST_FREQ, \
184 PCI_HT_CAP_HOST_FREQ_CAP)
186 #define PCI_HT_SLAVE0_OFFS LINK_OFFS( \
187 PCI_HT_CAP_SLAVE_CTRL0, \
188 PCI_HT_CAP_SLAVE_WIDTH0, \
189 PCI_HT_CAP_SLAVE_FREQ0, \
190 PCI_HT_CAP_SLAVE_FREQ_CAP0)
192 #define PCI_HT_SLAVE1_OFFS LINK_OFFS( \
193 PCI_HT_CAP_SLAVE_CTRL1, \
194 PCI_HT_CAP_SLAVE_WIDTH1, \
195 PCI_HT_CAP_SLAVE_FREQ1, \
196 PCI_HT_CAP_SLAVE_FREQ_CAP1)
198 static int ht_optimize_link(
199 device_t dev1, uint8_t pos1, unsigned offs1,
200 device_t dev2, uint8_t pos2, unsigned offs2)
202 static const uint8_t link_width_to_pow2[]= { 3, 4, 0, 5, 1, 2, 0, 0 };
203 static const uint8_t pow2_to_link_width[] = { 0x7, 4, 5, 0, 1, 3 };
204 uint16_t freq_cap1, freq_cap2;
205 uint8_t width_cap1, width_cap2, width, old_width, ln_width1, ln_width2;
206 uint8_t freq, old_freq;
208 /* Set link width and frequency */
210 /* Initially assume everything is already optimized and I don't need a reset */
213 /* Get the frequency capabilities */
214 freq_cap1 = ht_read_freq_cap(dev1, pos1 + LINK_FREQ_CAP(offs1));
215 freq_cap2 = ht_read_freq_cap(dev2, pos2 + LINK_FREQ_CAP(offs2));
217 /* Calculate the highest possible frequency */
218 freq = log2(freq_cap1 & freq_cap2);
220 /* See if I am changing the link freqency */
221 old_freq = pci_read_config8(dev1, pos1 + LINK_FREQ(offs1));
223 needs_reset |= old_freq != freq;
224 old_freq = pci_read_config8(dev2, pos2 + LINK_FREQ(offs2));
226 needs_reset |= old_freq != freq;
228 /* Set the Calulcated link frequency */
229 pci_write_config8(dev1, pos1 + LINK_FREQ(offs1), freq);
230 pci_write_config8(dev2, pos2 + LINK_FREQ(offs2), freq);
232 /* Get the width capabilities */
233 width_cap1 = pci_read_config8(dev1, pos1 + LINK_WIDTH(offs1));
234 width_cap2 = pci_read_config8(dev2, pos2 + LINK_WIDTH(offs2));
236 /* Calculate dev1's input width */
237 ln_width1 = link_width_to_pow2[width_cap1 & 7];
238 ln_width2 = link_width_to_pow2[(width_cap2 >> 4) & 7];
239 if (ln_width1 > ln_width2) {
240 ln_width1 = ln_width2;
242 width = pow2_to_link_width[ln_width1];
243 /* Calculate dev1's output width */
244 ln_width1 = link_width_to_pow2[(width_cap1 >> 4) & 7];
245 ln_width2 = link_width_to_pow2[width_cap2 & 7];
246 if (ln_width1 > ln_width2) {
247 ln_width1 = ln_width2;
249 width |= pow2_to_link_width[ln_width1] << 4;
251 /* See if I am changing dev1's width */
252 old_width = pci_read_config8(dev1, pos1 + LINK_WIDTH(offs1) + 1);
254 needs_reset |= old_width != width;
256 /* Set dev1's widths */
257 pci_write_config8(dev1, pos1 + LINK_WIDTH(offs1) + 1, width);
259 /* Calculate dev2's width */
260 width = ((width & 0x70) >> 4) | ((width & 0x7) << 4);
262 /* See if I am changing dev2's width */
263 old_width = pci_read_config8(dev2, pos2 + LINK_WIDTH(offs2) + 1);
265 needs_reset |= old_width != width;
267 /* Set dev2's widths */
268 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, unsigned offset_unitid, struct sys_info *sysinfo);
276 static int scan_pci_bus( unsigned bus , struct sys_info *sysinfo)
278 static int ht_setup_chainx(device_t udev, uint8_t upos, uint8_t bus, unsigned offset_unitid);
279 static int scan_pci_bus( unsigned bus)
283 here we already can access PCI_DEV(bus, 0, 0) to PCI_DEV(bus, 0x1f, 0x7)
284 So We can scan these devices to find out if they are bridge
285 If it is pci bridge, We need to set busn in bridge, and go on
286 For ht bridge, We need to set the busn in bridge and ht_setup_chainx, and the scan_pci_bus
292 new_bus = (bus & 0xff); // mask out the reset_needed
296 } else if (new_bus<0x80) {
298 } else if (new_bus<0xc0) {
307 #if CONFIG_USE_INIT == 1
308 printk_debug("bus_num=%02x\r\n", bus);
312 for (devfn = 0; devfn <= 0xff; devfn++) {
318 dev = PCI_DEV((bus & 0xff), ((devfn>>3) & 0x1f), (devfn & 0x7));
319 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
320 class = pci_read_config16(dev, PCI_CLASS_DEVICE);
323 #if CONFIG_USE_INIT == 1
324 if(hdr_type !=0xff ) {
325 printk_debug("dev=%02x fn=%02x hdr_type=%02x class=%04x\r\n",
326 (devfn>>3)& 0x1f, (devfn & 0x7), hdr_type, class);
330 switch(hdr_type & 0x7f) { /* header type */
331 case PCI_HEADER_TYPE_BRIDGE:
332 if (class != PCI_CLASS_BRIDGE_PCI) goto bad;
333 /* set the bus range dev */
335 /* Clear all status bits and turn off memory, I/O and master enables. */
336 cr = pci_read_config16(dev, PCI_COMMAND);
337 pci_write_config16(dev, PCI_COMMAND, 0x0000);
338 pci_write_config16(dev, PCI_STATUS, 0xffff);
340 buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
344 buses |= (((unsigned int) (bus & 0xff) << 0) |
345 ((unsigned int) (new_bus & 0xff) << 8) |
346 ((unsigned int) max_bus << 16));
347 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
349 /* here we need to figure out if dev is a ht bridge
350 if it is ht bridge, we need to call ht_setup_chainx at first
351 Not verified --- yhlu
354 upos = ht_lookup_host_capability(dev); // one func one ht sub
355 if (upos) { // sub ht chain
357 busn = (new_bus & 0xff);
358 /* Make certain the HT bus is not enumerated */
359 ht_collapse_previous_enumeration(busn, 0);
360 /* scan the ht chain */
361 #if RAMINIT_SYSINFO == 1
362 ht_setup_chainx(dev,upos,busn, 0, sysinfo); // don't need offset unitid
364 new_bus |= (ht_setup_chainx(dev, upos, busn, 0)<<16); // store reset_needed to upword
368 #if RAMINIT_SYSINFO == 1
369 new_bus = scan_pci_bus(new_bus, sysinfo);
371 new_bus = scan_pci_bus(new_bus);
373 /* set real max bus num in that */
375 buses = (buses & 0xff00ffff) |
376 ((unsigned int) (new_bus & 0xff) << 16);
377 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
379 pci_write_config16(dev, PCI_COMMAND, cr);
387 /* if this is not a multi function device,
388 * or the device is not present don't waste
389 * time probing another function.
390 * Skip to next device.
392 if ( ((devfn & 0x07) == 0x00) && ((hdr_type & 0x80) != 0x80))
402 #if RAMINIT_SYSINFO == 1
403 static void ht_setup_chainx(device_t udev, uint8_t upos, uint8_t bus, unsigned offset_unitid, struct sys_info *sysinfo)
405 static int ht_setup_chainx(device_t udev, uint8_t upos, uint8_t bus, unsigned offset_unitid)
408 //even HT_CHAIN_UNITID_BASE == 0, we still can go through this function, because of end_of_chain check, also We need it to optimize link
410 uint8_t next_unitid, last_unitid;
413 #if RAMINIT_SYSINFO == 0
414 int reset_needed = 0;
417 #if HT_CHAIN_END_UNITID_BASE < HT_CHAIN_UNITID_BASE
418 //let't record the device of last ht device, So we can set the Unitid to HT_CHAIN_END_UNITID_BASE
419 unsigned real_last_unitid;
420 uint8_t real_last_pos;
424 uoffs = PCI_HT_HOST_OFFS;
425 next_unitid = (offset_unitid) ? HT_CHAIN_UNITID_BASE:1;
430 uint16_t flags, ctrl;
434 /* Wait until the link initialization is complete */
436 ctrl = pci_read_config16(udev, upos + LINK_CTRL(uoffs));
437 /* Is this the end of the hypertransport chain? */
438 if (ctrl & (1 << 6)) {
442 if (ctrl & ((1 << 4) | (1 << 8))) {
444 * Either the link has failed, or we have
446 * Sometimes this can happen due to link
447 * retrain, so lets knock it down and see
450 ctrl |= ((1 << 6) | (1 <<8)); // Link fail + Crc
451 pci_write_config16(udev, upos + LINK_CTRL(uoffs), ctrl);
452 ctrl = pci_read_config16(udev, upos + LINK_CTRL(uoffs));
453 if (ctrl & ((1 << 4) | (1 << 8))) {
454 print_err("Detected error on Hypertransport Link\n");
458 } while((ctrl & (1 << 5)) == 0);
460 device_t dev = PCI_DEV(bus, 0, 0);
461 last_unitid = next_unitid;
463 id = pci_read_config32(dev, PCI_VENDOR_ID);
465 /* If the chain is enumerated quit */
466 if ( (id == 0xffffffff) || (id == 0x00000000) ||
467 (id == 0x0000ffff) || (id == 0xffff0000))
472 pos = ht_lookup_slave_capability(dev);
474 print_err("udev="); print_err_hex32(udev);
475 print_err("\tupos="); print_err_hex32(upos);
476 print_err("\tuoffs="); print_err_hex32(uoffs);
477 print_err("\tHT link capability not found\r\n");
481 #if CK804_DEVN_BASE==0
483 // CK804 UnitID changes not use
484 id = pci_read_config32(dev, PCI_VENDOR_ID);
485 if(id != 0x005e10de) {
488 /* Update the Unitid of the current device */
489 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
490 flags &= ~0x1f; /* mask out the bse Unit ID */
491 flags |= next_unitid & 0x1f;
492 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
494 /* Note the change in device number */
495 dev = PCI_DEV(bus, next_unitid, 0);
496 #if CK804_DEVN_BASE==0
499 dev = PCI_DEV(bus, 0, 0);
503 /* Compute the number of unitids consumed */
504 count = (flags >> 5) & 0x1f;
505 #if HT_CHAIN_END_UNITID_BASE < HT_CHAIN_UNITID_BASE
507 real_last_unitid = next_unitid;
513 next_unitid += count;
515 /* Find which side of the ht link we are on,
516 * by reading which direction our last write to PCI_CAP_FLAGS
519 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
520 offs = ((flags>>10) & 1) ? PCI_HT_SLAVE1_OFFS : PCI_HT_SLAVE0_OFFS;
522 #if RAMINIT_SYSINFO == 1
523 /* store the link pair here and we will Setup the Hypertransport link later, after we get final FID/VID */
525 struct link_pair_st *link_pair = &sysinfo->link_pair[sysinfo->link_pair_num];
526 link_pair->udev = udev;
527 link_pair->upos = upos;
528 link_pair->uoffs = uoffs;
529 link_pair->dev = dev;
530 link_pair->pos = pos;
531 link_pair->offs = offs;
532 sysinfo->link_pair_num++;
535 reset_needed |= ht_optimize_link(udev, upos, uoffs, dev, pos, offs);
538 #if CK804_DEVN_BASE==0
539 if(id == 0x005e10de) {
544 /* Remeber the location of the last device */
547 uoffs = ( offs != PCI_HT_SLAVE0_OFFS ) ? PCI_HT_SLAVE0_OFFS : PCI_HT_SLAVE1_OFFS;
549 } while((last_unitid != next_unitid) && (next_unitid <= 0x1f));
553 #if HT_CHAIN_END_UNITID_BASE < HT_CHAIN_UNITID_BASE
554 if(offset_unitid && (ht_dev_num>0) ) {
557 flags = pci_read_config16(PCI_DEV(bus,real_last_unitid,0), real_last_pos + PCI_CAP_FLAGS);
559 flags |= HT_CHAIN_END_UNITID_BASE & 0x1f;
560 pci_write_config16(PCI_DEV(bus, real_last_unitid, 0), real_last_pos + PCI_CAP_FLAGS, flags);
562 #if RAMINIT_SYSINFO == 1
563 // Here need to change the dev in the array
564 for(i=0;i<sysinfo->link_pair_num;i++)
566 struct link_pair_st *link_pair = &sysinfo->link_pair[i];
567 if(link_pair->udev == PCI_DEV(bus, real_last_unitid, 0)) {
568 link_pair->udev = PCI_DEV(bus, HT_CHAIN_END_UNITID_BASE, 0);
571 if(link_pair->dev == PCI_DEV(bus, real_last_unitid, 0)) {
572 link_pair->dev = PCI_DEV(bus, HT_CHAIN_END_UNITID_BASE, 0);
580 #if RAMINIT_SYSINFO == 0
586 #if RAMINIT_SYSINFO == 1
587 static void ht_setup_chain(device_t udev, unsigned upos, struct sys_info *sysinfo)
589 static int ht_setup_chain(device_t udev, unsigned upos)
592 unsigned offset_unitid = 0;
593 #if HT_CHAIN_UNITID_BASE != 1
597 /* Assumption the HT chain that is bus 0 has the HT I/O Hub on it.
598 * On most boards this just happens. If a cpu has multiple
599 * non Coherent links the appropriate bus registers for the
600 * links needs to be programed to point at bus 0.
603 /* Make certain the HT bus is not enumerated */
604 ht_collapse_previous_enumeration(0, 0);
606 #if HT_CHAIN_UNITID_BASE != 1
610 #if RAMINIT_SYSINFO == 1
611 ht_setup_chainx(udev, upos, 0, offset_unitid, sysinfo);
613 return ht_setup_chainx(udev, upos, 0, offset_unitid);
616 static int optimize_link_read_pointer(uint8_t node, uint8_t linkn, uint8_t linkt, uint8_t val)
618 uint32_t dword, dword_old;
621 /* This works on an Athlon64 because unimplemented links return 0 */
622 dword = pci_read_config32(PCI_DEV(0,0x18+node,0), 0x98 + (linkn * 0x20));
623 link_type = dword & 0xff;
625 dword_old = dword = pci_read_config32(PCI_DEV(0,0x18+node,3), 0xdc);
627 if ( (link_type & 7) == linkt ) { /* Coherent Link only linkt = 3, ncoherent = 7*/
628 dword &= ~( 0xff<<(linkn *8) );
629 dword |= val << (linkn *8);
632 if (dword != dword_old) {
633 pci_write_config32(PCI_DEV(0,0x18+node,3), 0xdc, dword);
640 static int optimize_link_read_pointers_chain(uint8_t ht_c_num)
647 for (i = 0; i < ht_c_num; i++) {
649 uint8_t nodeid, linkn;
653 reg = pci_read_config32(PCI_DEV(0,0x18,1), 0xe0 + i * 4);
655 nodeid = ((reg & 0xf0)>>4); // nodeid
656 linkn = ((reg & 0xf00)>>8); // link n
657 busn = (reg & 0xff0000)>>16; //busn
659 reg = pci_read_config32( PCI_DEV(busn, 1, 0), PCI_VENDOR_ID);
660 if ( (reg & 0xffff) == PCI_VENDOR_ID_AMD) {
662 } else if ( (reg & 0xffff) == PCI_VENDOR_ID_NVIDIA ) {
668 reset_needed |= optimize_link_read_pointer(nodeid, linkn, 0x07, val);
675 #if RAMINIT_SYSINFO == 1
676 static void ht_setup_chains(uint8_t ht_c_num, struct sys_info *sysinfo)
678 static int ht_setup_chains(uint8_t ht_c_num)
681 /* Assumption the HT chain that is bus 0 has the HT I/O Hub on it.
682 * On most boards this just happens. If a cpu has multiple
683 * non Coherent links the appropriate bus registers for the
684 * links needs to be programed to point at bus 0.
690 #if RAMINIT_SYSINFO == 0
691 int reset_needed = 0;
693 sysinfo->link_pair_num = 0;
696 // first one is SB Chain
697 for (i = 0; i < ht_c_num; i++) {
703 #if (USE_DCACHE_RAM == 1) && (K8_SCAN_PCI_BUS == 1)
706 unsigned offset_unitid = 0;
708 reg = pci_read_config32(PCI_DEV(0,0x18,1), 0xe0 + i * 4);
710 //We need setup 0x94, 0xb4, and 0xd4 according to the reg
711 devpos = ((reg & 0xf0)>>4)+0x18; // nodeid; it will decide 0x18 or 0x19
712 regpos = ((reg & 0xf00)>>8) * 0x20 + 0x94; // link n; it will decide 0x94 or 0xb4, 0x0xd4;
713 busn = (reg & 0xff0000)>>16;
715 dword = pci_read_config32( PCI_DEV(0, devpos, 0), regpos) ;
716 dword &= ~(0xffff<<8);
717 dword |= (reg & 0xffff0000)>>8;
718 pci_write_config32( PCI_DEV(0, devpos,0), regpos , dword);
721 #if HT_CHAIN_UNITID_BASE != 1
722 #if SB_HT_CHAIN_UNITID_OFFSET_ONLY == 1
723 if(i==0) // to check if it is sb ht chain
728 /* Make certain the HT bus is not enumerated */
729 ht_collapse_previous_enumeration(busn, offset_unitid);
731 upos = ((reg & 0xf00)>>8) * 0x20 + 0x80;
732 udev = PCI_DEV(0, devpos, 0);
734 #if RAMINIT_SYSINFO == 1
735 ht_setup_chainx(udev,upos,busn, offset_unitid, sysinfo); // all not
737 reset_needed |= ht_setup_chainx(udev,upos,busn, offset_unitid); //all not
740 #if (USE_DCACHE_RAM == 1) && (K8_SCAN_PCI_BUS == 1)
741 /* You can use use this in romcc, because there is function call in romcc, recursive will kill you */
742 bus = busn; // we need 32 bit
743 #if RAMINIT_SYSINFO == 1
744 scan_pci_bus(bus, sysinfo);
746 reset_needed |= (scan_pci_bus(bus)>>16); // take out reset_needed that stored in upword
751 #if RAMINIT_SYSINFO == 0
752 reset_needed |= optimize_link_read_pointers_chain(ht_c_num);
759 static inline unsigned get_nodes(void)
761 return ((pci_read_config32(PCI_DEV(0, 0x18, 0), 0x60)>>4) & 7) + 1;
765 #if RAMINIT_SYSINFO == 1
766 static void ht_setup_chains_x(struct sys_info *sysinfo)
768 static int ht_setup_chains_x(void)
777 #if K8_ALLOCATE_IO_RANGE == 1
778 unsigned next_io_base;
783 /* read PCI_DEV(0,0x18,0) 0x64 bit [8:9] to find out SbLink m */
784 reg = pci_read_config32(PCI_DEV(0, 0x18, 0), 0x64);
785 /* update PCI_DEV(0, 0x18, 1) 0xe0 to 0x05000m03, and next_busn=0x3f+1 */
786 print_linkn_in("SBLink=", ((reg>>8) & 3) );
787 #if RAMINIT_SYSINFO == 1
788 sysinfo->sblnk = (reg>>8) & 3;
790 sysinfo->nodes = nodes;
792 tempreg = 3 | ( 0<<4) | (((reg>>8) & 3)<<8) | (0<<16)| (0x3f<<24);
793 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xe0, tempreg);
795 next_busn=0x3f+1; /* 0 will be used ht chain with SB we need to keep SB in bus0 in auto stage*/
797 #if K8_ALLOCATE_IO_RANGE == 1
798 /* io range allocation */
799 tempreg = 0 | (((reg>>8) & 0x3) << 4 )| (0x3<<12); //limit
800 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xC4, tempreg);
801 tempreg = 3 | ( 3<<4) | (0<<12); //base
802 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xC0, tempreg);
803 next_io_base = 0x3+0x1;
807 for(ht_c_num=1;ht_c_num<4; ht_c_num++) {
808 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4, 0);
809 /* io range allocation */
810 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xc4 + ht_c_num * 8, 0);
811 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xc0 + ht_c_num * 8, 0);
814 for(nodeid=0; nodeid<nodes; nodeid++) {
817 dev = PCI_DEV(0, 0x18+nodeid,0);
818 for(linkn = 0; linkn<3; linkn++) {
820 regpos = 0x98 + 0x20 * linkn;
821 reg = pci_read_config32(dev, regpos);
822 if ((reg & 0x17) != 7) continue; /* it is not non conherent or not connected*/
823 print_linkn_in("NC node|link=", ((nodeid & 0xf)<<4)|(linkn & 0xf));
824 tempreg = 3 | (nodeid <<4) | (linkn<<8);
825 /*compare (temp & 0xffff), with (PCI(0, 0x18, 1) 0xe0 to 0xec & 0xfffff) */
826 for(ht_c_num=0;ht_c_num<4; ht_c_num++) {
827 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4);
828 if(((reg & 0xffff) == (tempreg & 0xffff)) || ((reg & 0xffff) == 0x0000)) { /*we got it*/
832 if(ht_c_num == 4) break; /*used up only 4 non conherent allowed*/
833 /*update to 0xe0...*/
834 if((reg & 0xf) == 3) continue; /*SbLink so don't touch it */
835 print_linkn_in("\tbusn=", next_busn);
836 tempreg |= (next_busn<<16)|((next_busn+0x3f)<<24);
837 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4, tempreg);
840 #if K8_ALLOCATE_IO_RANGE == 1
841 /* io range allocation */
842 tempreg = nodeid | (linkn<<4) | ((next_io_base+0x3)<<12); //limit
843 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xC4 + ht_c_num * 8, tempreg);
844 tempreg = 3 /*| ( 3<<4)*/ | (next_io_base<<12); //base :ISA and VGA ?
845 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xC0 + ht_c_num * 8, tempreg);
846 next_io_base += 0x3+0x1;
851 /*update 0xe0, 0xe4, 0xe8, 0xec from PCI_DEV(0, 0x18,1) to PCI_DEV(0, 0x19,1) to PCI_DEV(0, 0x1f,1);*/
853 for(nodeid = 1; nodeid<nodes; nodeid++) {
856 dev = PCI_DEV(0, 0x18+nodeid,1);
857 for(i = 0; i< 4; i++) {
859 regpos = 0xe0 + i * 4;
860 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), regpos);
861 pci_write_config32(dev, regpos, reg);
864 #if K8_ALLOCATE_IO_RANGE == 1
865 /* io range allocation */
866 for(i = 0; i< 4; i++) {
868 regpos = 0xc4 + i * 8;
869 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), regpos);
870 pci_write_config32(dev, regpos, reg);
872 for(i = 0; i< 4; i++) {
874 regpos = 0xc0 + i * 8;
875 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), regpos);
876 pci_write_config32(dev, regpos, reg);
881 /* recount ht_c_num*/
883 for(ht_c_num=0;ht_c_num<4; ht_c_num++) {
884 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4);
885 if(((reg & 0xf) != 0x0)) {
890 #if RAMINIT_SYSINFO == 1
891 sysinfo->ht_c_num = i;
892 ht_setup_chains(i, sysinfo);
894 return ht_setup_chains(i);
899 #if RAMINIT_SYSINFO == 1
900 static int optimize_link_incoherent_ht(struct sys_info *sysinfo)
902 // We need to use recorded link pair info to optimize the link
904 int reset_needed = 0;
906 unsigned link_pair_num = sysinfo->link_pair_num;
908 for(i=0; i< link_pair_num; i++) {
909 struct link_pair_st *link_pair= &sysinfo->link_pair[i];
910 reset_needed |= ht_optimize_link(link_pair->udev, link_pair->upos, link_pair->uoffs, link_pair->dev, link_pair->pos, link_pair->offs);
913 reset_needed |= optimize_link_read_pointers(sysinfo->ht_c_num);
921 static unsigned get_sblnk(void)
924 /* read PCI_DEV(0,0x18,0) 0x64 bit [8:9] to find out SbLink m */
925 reg = pci_read_config32(PCI_DEV(0, 0x18, 0), 0x64);
926 return ((reg>>8) & 3) ;
929 /* Look up a which bus a given node/link combination is on.
930 * return 0 when we can't find the answer.
932 static unsigned node_link_to_bus(unsigned node, unsigned link)
936 for(reg = 0xE0; reg < 0xF0; reg += 0x04) {
938 config_map = pci_read_config32(PCI_DEV(0, 0x18, 1), reg);
939 if ((config_map & 3) != 3) {
942 if ((((config_map >> 4) & 7) == node) &&
943 (((config_map >> 8) & 3) == link))
945 return (config_map >> 16) & 0xff;