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 CONFIG_K8_HT_FREQ_1G_SUPPORT
11 #define CONFIG_K8_HT_FREQ_1G_SUPPORT 0
14 #ifndef RAMINIT_SYSINFO
15 #define RAMINIT_SYSINFO 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)
29 printk(BIOS_DEBUG, "%s%02x\r\n", strval, byteval);
32 static uint8_t ht_lookup_capability(device_t dev, uint16_t val)
37 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
41 if ((hdr_type == PCI_HEADER_TYPE_NORMAL) ||
42 (hdr_type == PCI_HEADER_TYPE_BRIDGE)) {
43 pos = PCI_CAPABILITY_LIST;
45 if (pos > PCI_CAP_LIST_NEXT) {
46 pos = pci_read_config8(dev, pos);
48 while(pos != 0) { /* loop through the linked list */
50 cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
51 if (cap == PCI_CAP_ID_HT) {
54 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
55 if ((flags >> 13) == val) {
56 /* Entry is a slave or host , success... */
60 pos = pci_read_config8(dev, pos + PCI_CAP_LIST_NEXT);
65 static uint8_t ht_lookup_slave_capability(device_t dev)
67 return ht_lookup_capability(dev, 0); // Slave/Primary Interface Block Format
70 static uint8_t ht_lookup_host_capability(device_t dev)
72 return ht_lookup_capability(dev, 1); // Host/Secondary Interface Block Format
75 static void ht_collapse_previous_enumeration(uint8_t bus, unsigned offset_unitid)
79 //actually, only for one HT device HT chain, and unitid is 0
80 #if CONFIG_HT_CHAIN_UNITID_BASE == 0
86 /* Check if is already collapsed */
87 if((!offset_unitid) || (offset_unitid && (!((CONFIG_HT_CHAIN_END_UNITID_BASE == 0) && (CONFIG_HT_CHAIN_END_UNITID_BASE <CONFIG_HT_CHAIN_UNITID_BASE))))) {
89 dev = PCI_DEV(bus, 0, 0);
90 id = pci_read_config32(dev, PCI_VENDOR_ID);
91 if (!((id == 0xffffffff) || (id == 0x00000000) ||
92 (id == 0x0000ffff) || (id == 0xffff0000))) {
97 /* Spin through the devices and collapse any previous
98 * hypertransport enumeration.
100 for(dev = PCI_DEV(bus, 1, 0); dev <= PCI_DEV(bus, 0x1f, 0x7); dev += PCI_DEV(0, 1, 0)) {
105 id = pci_read_config32(dev, PCI_VENDOR_ID);
106 if ((id == 0xffffffff) || (id == 0x00000000) ||
107 (id == 0x0000ffff) || (id == 0xffff0000)) {
111 pos = ht_lookup_slave_capability(dev);
116 /* Clear the unitid */
117 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
119 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
123 static uint16_t ht_read_freq_cap(device_t dev, uint8_t pos)
125 /* Handle bugs in valid hypertransport frequency reporting */
129 freq_cap = pci_read_config16(dev, pos);
130 printk(BIOS_SPEW, "pos=0x%x, unfiltered freq_cap=0x%x\r\n", pos, freq_cap);
131 freq_cap &= ~(1 << HT_FREQ_VENDOR); /* Ignore Vendor HT frequencies */
133 id = pci_read_config32(dev, 0);
135 /* AMD 8131 Errata 48 */
136 if (id == (PCI_VENDOR_ID_AMD | (PCI_DEVICE_ID_AMD_8131_PCIX << 16))) {
137 freq_cap &= ~(1 << HT_FREQ_800Mhz);
141 /* AMD 8151 Errata 23 */
142 if (id == (PCI_VENDOR_ID_AMD | (PCI_DEVICE_ID_AMD_8151_SYSCTRL << 16))) {
143 freq_cap &= ~(1 << HT_FREQ_800Mhz);
147 /* AMD K8 Unsupported 1Ghz? */
148 if (id == (PCI_VENDOR_ID_AMD | (0x1100 << 16))) {
149 #if CONFIG_K8_HT_FREQ_1G_SUPPORT == 1
150 #if CONFIG_K8_REV_F_SUPPORT == 0
151 if (is_cpu_pre_e0()) { // only E0 later support 1GHz
152 freq_cap &= ~(1 << HT_FREQ_1000Mhz);
156 freq_cap &= ~(1 << HT_FREQ_1000Mhz);
160 printk(BIOS_SPEW, "pos=0x%x, filtered freq_cap=0x%x\r\n", pos, freq_cap);
161 //printk(BIOS_SPEW, "capping to 800/600/400/200 MHz\r\n");
166 static uint8_t ht_read_width_cap(device_t dev, uint8_t pos)
168 uint8_t width_cap = pci_read_config8(dev, pos);
172 id = pci_read_config32(dev, 0);
174 /* netlogic micro cap doesn't support 16 bit yet */
175 if (id == (0x184e | (0x0001 << 16))) {
176 if((width_cap & 0x77) == 0x11) {
185 #define LINK_OFFS(CTRL, WIDTH,FREQ,FREQ_CAP) \
186 (((CTRL & 0xff) << 24) | ((WIDTH & 0xff) << 16) | ((FREQ & 0xff) << 8) | (FREQ_CAP & 0xFF))
188 #define LINK_CTRL(OFFS) ((OFFS >> 24) & 0xFF)
189 #define LINK_WIDTH(OFFS) ((OFFS >> 16) & 0xFF)
190 #define LINK_FREQ(OFFS) ((OFFS >> 8) & 0xFF)
191 #define LINK_FREQ_CAP(OFFS) ((OFFS) & 0xFF)
193 #define PCI_HT_HOST_OFFS LINK_OFFS( \
194 PCI_HT_CAP_HOST_CTRL, \
195 PCI_HT_CAP_HOST_WIDTH, \
196 PCI_HT_CAP_HOST_FREQ, \
197 PCI_HT_CAP_HOST_FREQ_CAP)
199 #define PCI_HT_SLAVE0_OFFS LINK_OFFS( \
200 PCI_HT_CAP_SLAVE_CTRL0, \
201 PCI_HT_CAP_SLAVE_WIDTH0, \
202 PCI_HT_CAP_SLAVE_FREQ0, \
203 PCI_HT_CAP_SLAVE_FREQ_CAP0)
205 #define PCI_HT_SLAVE1_OFFS LINK_OFFS( \
206 PCI_HT_CAP_SLAVE_CTRL1, \
207 PCI_HT_CAP_SLAVE_WIDTH1, \
208 PCI_HT_CAP_SLAVE_FREQ1, \
209 PCI_HT_CAP_SLAVE_FREQ_CAP1)
211 static int ht_optimize_link(
212 device_t dev1, uint8_t pos1, unsigned offs1,
213 device_t dev2, uint8_t pos2, unsigned offs2)
215 static const uint8_t link_width_to_pow2[]= { 3, 4, 0, 5, 1, 2, 0, 0 };
216 static const uint8_t pow2_to_link_width[] = { 0x7, 4, 5, 0, 1, 3 };
217 uint16_t freq_cap1, freq_cap2;
218 uint8_t width_cap1, width_cap2, width, old_width, ln_width1, ln_width2;
219 uint8_t freq, old_freq;
221 /* Set link width and frequency */
223 printk(BIOS_SPEW, "entering ht_optimize_link\r\n");
224 /* Initially assume everything is already optimized and I don't need a reset */
227 /* Get the frequency capabilities */
228 freq_cap1 = ht_read_freq_cap(dev1, pos1 + LINK_FREQ_CAP(offs1));
229 freq_cap2 = ht_read_freq_cap(dev2, pos2 + LINK_FREQ_CAP(offs2));
230 printk(BIOS_SPEW, "freq_cap1=0x%x, freq_cap2=0x%x\r\n", freq_cap1, freq_cap2);
232 /* Calculate the highest possible frequency */
233 freq = log2(freq_cap1 & freq_cap2);
235 /* See if I am changing the link freqency */
236 old_freq = pci_read_config8(dev1, pos1 + LINK_FREQ(offs1));
238 needs_reset |= old_freq != freq;
239 printk(BIOS_SPEW, "dev1 old_freq=0x%x, freq=0x%x, needs_reset=0x%0x\r\n", old_freq, freq, needs_reset);
240 old_freq = pci_read_config8(dev2, pos2 + LINK_FREQ(offs2));
242 needs_reset |= old_freq != freq;
243 printk(BIOS_SPEW, "dev2 old_freq=0x%x, freq=0x%x, needs_reset=0x%0x\r\n", old_freq, freq, needs_reset);
245 /* Set the Calculated link frequency */
246 pci_write_config8(dev1, pos1 + LINK_FREQ(offs1), freq);
247 pci_write_config8(dev2, pos2 + LINK_FREQ(offs2), freq);
249 /* Get the width capabilities */
250 width_cap1 = ht_read_width_cap(dev1, pos1 + LINK_WIDTH(offs1));
251 width_cap2 = ht_read_width_cap(dev2, pos2 + LINK_WIDTH(offs2));
252 printk(BIOS_SPEW, "width_cap1=0x%x, width_cap2=0x%x\r\n", width_cap1, width_cap2);
254 /* Calculate dev1's input width */
255 ln_width1 = link_width_to_pow2[width_cap1 & 7];
256 ln_width2 = link_width_to_pow2[(width_cap2 >> 4) & 7];
257 printk(BIOS_SPEW, "dev1 input ln_width1=0x%x, ln_width2=0x%x\r\n", ln_width1, ln_width2);
258 if (ln_width1 > ln_width2) {
259 ln_width1 = ln_width2;
261 width = pow2_to_link_width[ln_width1];
262 printk(BIOS_SPEW, "dev1 input width=0x%x\r\n", width);
263 /* Calculate dev1's output width */
264 ln_width1 = link_width_to_pow2[(width_cap1 >> 4) & 7];
265 ln_width2 = link_width_to_pow2[width_cap2 & 7];
266 printk(BIOS_SPEW, "dev1 output ln_width1=0x%x, ln_width2=0x%x\r\n", ln_width1, ln_width2);
267 if (ln_width1 > ln_width2) {
268 ln_width1 = ln_width2;
270 width |= pow2_to_link_width[ln_width1] << 4;
271 printk(BIOS_SPEW, "dev1 input|output width=0x%x\r\n", width);
273 /* See if I am changing dev1's width */
274 old_width = pci_read_config8(dev1, pos1 + LINK_WIDTH(offs1) + 1);
276 needs_reset |= old_width != width;
277 printk(BIOS_SPEW, "old dev1 input|output width=0x%x\r\n", width);
279 /* Set dev1's widths */
280 pci_write_config8(dev1, pos1 + LINK_WIDTH(offs1) + 1, width);
282 /* Calculate dev2's width */
283 width = ((width & 0x70) >> 4) | ((width & 0x7) << 4);
284 printk(BIOS_SPEW, "dev2 input|output width=0x%x\r\n", width);
286 /* See if I am changing dev2's width */
287 old_width = pci_read_config8(dev2, pos2 + LINK_WIDTH(offs2) + 1);
289 needs_reset |= old_width != width;
290 printk(BIOS_SPEW, "old dev2 input|output width=0x%x\r\n", width);
292 /* Set dev2's widths */
293 pci_write_config8(dev2, pos2 + LINK_WIDTH(offs2) + 1, width);
298 #if RAMINIT_SYSINFO == 1
299 static void ht_setup_chainx(device_t udev, uint8_t upos, uint8_t bus, unsigned offset_unitid, struct sys_info *sysinfo)
301 static int ht_setup_chainx(device_t udev, uint8_t upos, uint8_t bus, unsigned offset_unitid)
304 //even CONFIG_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
306 uint8_t next_unitid, last_unitid;
309 #if RAMINIT_SYSINFO == 0
310 int reset_needed = 0;
313 #if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
314 //let't record the device of last ht device, So we can set the Unitid to CONFIG_HT_CHAIN_END_UNITID_BASE
315 unsigned real_last_unitid;
316 uint8_t real_last_pos;
318 uint8_t end_used = 0;
321 uoffs = PCI_HT_HOST_OFFS;
322 next_unitid = (offset_unitid) ? CONFIG_HT_CHAIN_UNITID_BASE:1;
327 uint16_t flags, ctrl;
331 /* Wait until the link initialization is complete */
333 ctrl = pci_read_config16(udev, upos + LINK_CTRL(uoffs));
334 /* Is this the end of the hypertransport chain? */
335 if (ctrl & (1 << 6)) {
339 if (ctrl & ((1 << 4) | (1 << 8))) {
341 * Either the link has failed, or we have
343 * Sometimes this can happen due to link
344 * retrain, so lets knock it down and see
347 ctrl |= ((1 << 4) | (1 <<8)); // Link fail + Crc
348 pci_write_config16(udev, upos + LINK_CTRL(uoffs), ctrl);
349 ctrl = pci_read_config16(udev, upos + LINK_CTRL(uoffs));
350 if (ctrl & ((1 << 4) | (1 << 8))) {
351 print_err("Detected error on Hypertransport Link\n");
355 } while((ctrl & (1 << 5)) == 0);
357 device_t dev = PCI_DEV(bus, 0, 0);
358 last_unitid = next_unitid;
360 id = pci_read_config32(dev, PCI_VENDOR_ID);
362 /* If the chain is enumerated quit */
363 if ((id == 0xffffffff) || (id == 0x00000000) ||
364 (id == 0x0000ffff) || (id == 0xffff0000))
369 pos = ht_lookup_slave_capability(dev);
371 print_err("udev="); print_err_hex32(udev);
372 print_err("\tupos="); print_err_hex32(upos);
373 print_err("\tuoffs="); print_err_hex32(uoffs);
374 print_err("\tHT link capability not found\r\n");
379 #if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
381 if(next_unitid>= (bus ? 0x20:0x18) ) {
383 next_unitid = CONFIG_HT_CHAIN_END_UNITID_BASE;
391 real_last_unitid = next_unitid;
395 /* Update the Unitid of the current device */
396 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
397 flags &= ~0x1f; /* mask out the base Unit ID */
398 flags |= next_unitid & 0x1f;
399 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
401 /* Compute the number of unitids consumed */
402 count = (flags >> 5) & 0x1f;
404 /* Note the change in device number */
405 dev = PCI_DEV(bus, next_unitid, 0);
407 next_unitid += count;
409 /* Find which side of the ht link we are on,
410 * by reading which direction our last write to PCI_CAP_FLAGS
413 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
414 offs = ((flags>>10) & 1) ? PCI_HT_SLAVE1_OFFS : PCI_HT_SLAVE0_OFFS;
416 #if RAMINIT_SYSINFO == 1
417 /* store the link pair here and we will Setup the Hypertransport link later, after we get final FID/VID */
419 struct link_pair_st *link_pair = &sysinfo->link_pair[sysinfo->link_pair_num];
420 link_pair->udev = udev;
421 link_pair->upos = upos;
422 link_pair->uoffs = uoffs;
423 link_pair->dev = dev;
424 link_pair->pos = pos;
425 link_pair->offs = offs;
426 sysinfo->link_pair_num++;
429 reset_needed |= ht_optimize_link(udev, upos, uoffs, dev, pos, offs);
432 /* Remeber the location of the last device */
435 uoffs = ( offs != PCI_HT_SLAVE0_OFFS ) ? PCI_HT_SLAVE0_OFFS : PCI_HT_SLAVE1_OFFS;
437 } while (last_unitid != next_unitid );
439 #if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
444 #if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
445 if(offset_unitid && (ht_dev_num>1) && (real_last_unitid != CONFIG_HT_CHAIN_END_UNITID_BASE) && !end_used ) {
448 flags = pci_read_config16(PCI_DEV(bus,real_last_unitid,0), real_last_pos + PCI_CAP_FLAGS);
450 flags |= CONFIG_HT_CHAIN_END_UNITID_BASE & 0x1f;
451 pci_write_config16(PCI_DEV(bus, real_last_unitid, 0), real_last_pos + PCI_CAP_FLAGS, flags);
453 #if RAMINIT_SYSINFO == 1
454 // Here need to change the dev in the array
455 for(i=0;i<sysinfo->link_pair_num;i++)
457 struct link_pair_st *link_pair = &sysinfo->link_pair[i];
458 if(link_pair->udev == PCI_DEV(bus, real_last_unitid, 0)) {
459 link_pair->udev = PCI_DEV(bus, CONFIG_HT_CHAIN_END_UNITID_BASE, 0);
462 if(link_pair->dev == PCI_DEV(bus, real_last_unitid, 0)) {
463 link_pair->dev = PCI_DEV(bus, CONFIG_HT_CHAIN_END_UNITID_BASE, 0);
471 #if RAMINIT_SYSINFO == 0
477 #if RAMINIT_SYSINFO == 1
478 static void ht_setup_chain(device_t udev, unsigned upos, struct sys_info *sysinfo)
480 static int ht_setup_chain(device_t udev, unsigned upos)
483 unsigned offset_unitid = 0;
484 #if ((CONFIG_HT_CHAIN_UNITID_BASE != 1) || (CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20))
488 /* Assumption the HT chain that is bus 0 has the HT I/O Hub on it.
489 * On most boards this just happens. If a cpu has multiple
490 * non Coherent links the appropriate bus registers for the
491 * links needs to be programed to point at bus 0.
494 /* Make certain the HT bus is not enumerated */
495 ht_collapse_previous_enumeration(0, 0);
497 #if ((CONFIG_HT_CHAIN_UNITID_BASE != 1) || (CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20))
501 #if RAMINIT_SYSINFO == 1
502 ht_setup_chainx(udev, upos, 0, offset_unitid, sysinfo);
504 return ht_setup_chainx(udev, upos, 0, offset_unitid);
507 static int optimize_link_read_pointer(uint8_t node, uint8_t linkn, uint8_t linkt, uint8_t val)
509 uint32_t dword, dword_old;
512 /* This works on an Athlon64 because unimplemented links return 0 */
513 dword = pci_read_config32(PCI_DEV(0,0x18+node,0), 0x98 + (linkn * 0x20));
514 link_type = dword & 0xff;
517 if ( (link_type & 7) == linkt ) { /* Coherent Link only linkt = 3, ncoherent = 7*/
518 dword_old = dword = pci_read_config32(PCI_DEV(0,0x18+node,3), 0xdc);
519 dword &= ~( 0xff<<(linkn *8) );
520 dword |= val << (linkn *8);
522 if (dword != dword_old) {
523 pci_write_config32(PCI_DEV(0,0x18+node,3), 0xdc, dword);
531 static int optimize_link_read_pointers_chain(uint8_t ht_c_num)
538 for (i = 0; i < ht_c_num; i++) {
540 uint8_t nodeid, linkn;
545 #if ((CONFIG_HT_CHAIN_UNITID_BASE != 1) || (CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20))
546 #if CONFIG_SB_HT_CHAIN_UNITID_OFFSET_ONLY == 1
547 if(i==0) // to check if it is sb ht chain
549 devn = CONFIG_HT_CHAIN_UNITID_BASE;
552 reg = pci_read_config32(PCI_DEV(0,0x18,1), 0xe0 + i * 4);
554 nodeid = ((reg & 0xf0)>>4); // nodeid
555 linkn = ((reg & 0xf00)>>8); // link n
556 busn = (reg & 0xff0000)>>16; //busn
558 reg = pci_read_config32( PCI_DEV(busn, devn, 0), PCI_VENDOR_ID); // ? the chain dev maybe offseted
559 if ( (reg & 0xffff) == PCI_VENDOR_ID_AMD) {
561 } else if ( (reg & 0xffff) == PCI_VENDOR_ID_NVIDIA ) {
567 reset_needed |= optimize_link_read_pointer(nodeid, linkn, 0x07, val);
574 static int set_ht_link_buffer_count(uint8_t node, uint8_t linkn, uint8_t linkt, unsigned val)
581 /* This works on an Athlon64 because unimplemented links return 0 */
582 regpos = 0x98 + (linkn * 0x20);
583 dev = PCI_DEV(0,0x18+node,0);
584 dword = pci_read_config32(dev, regpos);
585 link_type = dword & 0xff;
587 if ( (link_type & 0x7) == linkt ) { /* Coherent Link only linkt = 3, ncoherent = 7*/
588 regpos = 0x90 + (linkn * 0x20);
589 dword = pci_read_config32(dev, regpos );
592 pci_write_config32(dev, regpos, val);
599 static int set_ht_link_buffer_counts_chain(uint8_t ht_c_num, unsigned vendorid, unsigned val)
606 for (i = 0; i < ht_c_num; i++) {
608 uint8_t nodeid, linkn;
612 reg = pci_read_config32(PCI_DEV(0,0x18,1), 0xe0 + i * 4);
613 if((reg & 3) != 3) continue; // not enabled
615 nodeid = ((reg & 0xf0)>>4); // nodeid
616 linkn = ((reg & 0xf00)>>8); // link n
617 busn = (reg & 0xff0000)>>16; //busn
619 for(devn = 0; devn < 0x20; devn++) {
620 reg = pci_read_config32( PCI_DEV(busn, devn, 0), PCI_VENDOR_ID); //1?
621 if ( (reg & 0xffff) == vendorid ) {
622 reset_needed |= set_ht_link_buffer_count(nodeid, linkn, 0x07,val);
632 #if RAMINIT_SYSINFO == 1
633 static void ht_setup_chains(uint8_t ht_c_num, struct sys_info *sysinfo)
635 static int ht_setup_chains(uint8_t ht_c_num)
638 /* Assumption the HT chain that is bus 0 has the HT I/O Hub on it.
639 * On most boards this just happens. If a cpu has multiple
640 * non Coherent links the appropriate bus registers for the
641 * links needs to be programed to point at bus 0.
647 #if RAMINIT_SYSINFO == 0
648 int reset_needed = 0;
650 sysinfo->link_pair_num = 0;
653 // first one is SB Chain
654 for (i = 0; i < ht_c_num; i++) {
660 unsigned offset_unitid = 0;
662 reg = pci_read_config32(PCI_DEV(0,0x18,1), 0xe0 + i * 4);
664 //We need setup 0x94, 0xb4, and 0xd4 according to the reg
665 devpos = ((reg & 0xf0)>>4)+0x18; // nodeid; it will decide 0x18 or 0x19
666 regpos = ((reg & 0xf00)>>8) * 0x20 + 0x94; // link n; it will decide 0x94 or 0xb4, 0x0xd4;
667 busn = (reg & 0xff0000)>>16;
669 dword = pci_read_config32( PCI_DEV(0, devpos, 0), regpos) ;
670 dword &= ~(0xffff<<8);
671 dword |= (reg & 0xffff0000)>>8;
672 pci_write_config32( PCI_DEV(0, devpos,0), regpos , dword);
675 #if ((CONFIG_HT_CHAIN_UNITID_BASE != 1) || (CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20))
676 #if CONFIG_SB_HT_CHAIN_UNITID_OFFSET_ONLY == 1
677 if(i==0) // to check if it is sb ht chain
682 /* Make certain the HT bus is not enumerated */
683 ht_collapse_previous_enumeration(busn, offset_unitid);
685 upos = ((reg & 0xf00)>>8) * 0x20 + 0x80;
686 udev = PCI_DEV(0, devpos, 0);
688 #if RAMINIT_SYSINFO == 1
689 ht_setup_chainx(udev,upos,busn, offset_unitid, sysinfo); // all not
691 reset_needed |= ht_setup_chainx(udev,upos,busn, offset_unitid); //all not
696 #if RAMINIT_SYSINFO == 0
697 reset_needed |= optimize_link_read_pointers_chain(ht_c_num);
704 #if defined (__GNUC__)
705 static inline unsigned get_nodes(void);
708 #if RAMINIT_SYSINFO == 1
709 static void ht_setup_chains_x(struct sys_info *sysinfo)
711 static int ht_setup_chains_x(void)
720 #if K8_ALLOCATE_IO_RANGE == 1
721 unsigned next_io_base;
726 /* read PCI_DEV(0,0x18,0) 0x64 bit [8:9] to find out SbLink m */
727 reg = pci_read_config32(PCI_DEV(0, 0x18, 0), 0x64);
728 /* update PCI_DEV(0, 0x18, 1) 0xe0 to 0x05000m03, and next_busn=0x3f+1 */
729 print_linkn_in("SBLink=", ((reg>>8) & 3) );
730 #if RAMINIT_SYSINFO == 1
731 sysinfo->sblk = (reg>>8) & 3;
733 sysinfo->nodes = nodes;
735 tempreg = 3 | ( 0<<4) | (((reg>>8) & 3)<<8) | (0<<16)| (0x3f<<24);
736 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xe0, tempreg);
738 next_busn=0x3f+1; /* 0 will be used ht chain with SB we need to keep SB in bus0 in auto stage*/
740 #if K8_ALLOCATE_IO_RANGE == 1
741 /* io range allocation */
742 tempreg = 0 | (((reg>>8) & 0x3) << 4 )| (0x3<<12); //limit
743 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xC4, tempreg);
744 tempreg = 3 | ( 3<<4) | (0<<12); //base
745 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xC0, tempreg);
746 next_io_base = 0x3+0x1;
750 for(ht_c_num=1;ht_c_num<4; ht_c_num++) {
751 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4, 0);
753 #if K8_ALLOCATE_IO_RANGE == 1
754 /* io range allocation */
755 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xc4 + ht_c_num * 8, 0);
756 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xc0 + ht_c_num * 8, 0);
760 for(nodeid=0; nodeid<nodes; nodeid++) {
763 dev = PCI_DEV(0, 0x18+nodeid,0);
764 for(linkn = 0; linkn<3; linkn++) {
766 regpos = 0x98 + 0x20 * linkn;
767 reg = pci_read_config32(dev, regpos);
768 if ((reg & 0x17) != 7) continue; /* it is not non conherent or not connected*/
769 print_linkn_in("NC node|link=", ((nodeid & 0xf)<<4)|(linkn & 0xf));
770 tempreg = 3 | (nodeid <<4) | (linkn<<8);
771 /*compare (temp & 0xffff), with (PCI(0, 0x18, 1) 0xe0 to 0xec & 0xfffff) */
772 for(ht_c_num=0;ht_c_num<4; ht_c_num++) {
773 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4);
774 if(((reg & 0xffff) == (tempreg & 0xffff)) || ((reg & 0xffff) == 0x0000)) { /*we got it*/
778 if(ht_c_num == 4) break; /*used up only 4 non conherent allowed*/
779 /*update to 0xe0...*/
780 if((reg & 0xf) == 3) continue; /*SbLink so don't touch it */
781 print_linkn_in("\tbusn=", next_busn);
782 tempreg |= (next_busn<<16)|((next_busn+0x3f)<<24);
783 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4, tempreg);
786 #if K8_ALLOCATE_IO_RANGE == 1
787 /* io range allocation */
788 tempreg = nodeid | (linkn<<4) | ((next_io_base+0x3)<<12); //limit
789 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xC4 + ht_c_num * 8, tempreg);
790 tempreg = 3 /*| ( 3<<4)*/ | (next_io_base<<12); //base :ISA and VGA ?
791 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xC0 + ht_c_num * 8, tempreg);
792 next_io_base += 0x3+0x1;
797 /*update 0xe0, 0xe4, 0xe8, 0xec from PCI_DEV(0, 0x18,1) to PCI_DEV(0, 0x19,1) to PCI_DEV(0, 0x1f,1);*/
799 for(nodeid = 1; nodeid<nodes; nodeid++) {
802 dev = PCI_DEV(0, 0x18+nodeid,1);
803 for(i = 0; i< 4; i++) {
805 regpos = 0xe0 + i * 4;
806 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), regpos);
807 pci_write_config32(dev, regpos, reg);
810 #if K8_ALLOCATE_IO_RANGE == 1
811 /* io range allocation */
812 for(i = 0; i< 4; i++) {
814 regpos = 0xc4 + i * 8;
815 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), regpos);
816 pci_write_config32(dev, regpos, reg);
818 for(i = 0; i< 4; i++) {
820 regpos = 0xc0 + i * 8;
821 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), regpos);
822 pci_write_config32(dev, regpos, reg);
827 /* recount ht_c_num*/
829 for(ht_c_num=0;ht_c_num<4; ht_c_num++) {
830 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4);
831 if(((reg & 0xf) != 0x0)) {
836 #if RAMINIT_SYSINFO == 1
837 sysinfo->ht_c_num = i;
838 ht_setup_chains(i, sysinfo);
839 sysinfo->sbdn = get_sbdn(sysinfo->sbbusn);
841 return ht_setup_chains(i);
846 #if RAMINIT_SYSINFO == 1
847 static int optimize_link_incoherent_ht(struct sys_info *sysinfo)
849 // We need to use recorded link pair info to optimize the link
851 int reset_needed = 0;
853 unsigned link_pair_num = sysinfo->link_pair_num;
855 printk(BIOS_SPEW, "entering optimize_link_incoherent_ht\r\n");
856 printk(BIOS_SPEW, "sysinfo->link_pair_num=0x%x\r\n", link_pair_num);
857 for(i=0; i< link_pair_num; i++) {
858 struct link_pair_st *link_pair= &sysinfo->link_pair[i];
859 reset_needed |= ht_optimize_link(link_pair->udev, link_pair->upos, link_pair->uoffs, link_pair->dev, link_pair->pos, link_pair->offs);
860 printk(BIOS_SPEW, "after ht_optimize_link for link pair %d, reset_needed=0x%x\r\n", i, reset_needed);
863 reset_needed |= optimize_link_read_pointers_chain(sysinfo->ht_c_num);
864 printk(BIOS_SPEW, "after optimize_link_read_pointers_chain, reset_needed=0x%x\r\n", reset_needed);