printk_foo -> printk(BIOS_FOO, ...)
[coreboot.git] / src / northbridge / amd / amdk8 / incoherent_ht.c
1 /*
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
5 */
6 #include <device/pci_def.h>
7 #include <device/pci_ids.h>
8 #include <device/hypertransport_def.h>
9
10 #ifndef CONFIG_K8_HT_FREQ_1G_SUPPORT
11         #define CONFIG_K8_HT_FREQ_1G_SUPPORT 0
12 #endif
13
14 #ifndef RAMINIT_SYSINFO
15         #define RAMINIT_SYSINFO 0
16 #endif
17
18 #ifndef K8_ALLOCATE_IO_RANGE
19         #define K8_ALLOCATE_IO_RANGE 0
20 #endif
21
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
25 #endif
26
27 static inline void print_linkn_in (const char *strval, uint8_t byteval)
28 {
29         printk(BIOS_DEBUG, "%s%02x\r\n", strval, byteval);
30 }
31
32 static uint8_t ht_lookup_capability(device_t dev, uint16_t val)
33 {
34         uint8_t pos;
35         uint8_t hdr_type;
36
37         hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
38         pos = 0;
39         hdr_type &= 0x7f;
40
41         if ((hdr_type == PCI_HEADER_TYPE_NORMAL) ||
42             (hdr_type == PCI_HEADER_TYPE_BRIDGE)) {
43                 pos = PCI_CAPABILITY_LIST;
44         }
45         if (pos > PCI_CAP_LIST_NEXT) {
46                 pos = pci_read_config8(dev, pos);
47         }
48         while(pos != 0) { /* loop through the linked list */
49                 uint8_t cap;
50                 cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
51                 if (cap == PCI_CAP_ID_HT) {
52                         uint16_t flags;
53
54                         flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
55                         if ((flags >> 13) == val) {
56                                 /* Entry is a slave or host , success... */
57                                 break;
58                         }
59                 }
60                 pos = pci_read_config8(dev, pos + PCI_CAP_LIST_NEXT);
61         }
62         return pos;
63 }
64
65 static uint8_t ht_lookup_slave_capability(device_t dev)
66 {
67         return ht_lookup_capability(dev, 0); // Slave/Primary Interface Block Format
68 }
69
70 static uint8_t ht_lookup_host_capability(device_t dev)
71 {
72         return ht_lookup_capability(dev, 1); // Host/Secondary Interface Block Format
73 }
74
75 static void ht_collapse_previous_enumeration(uint8_t bus, unsigned offset_unitid)
76 {
77         device_t dev;
78
79         //actually, only for one HT device HT chain, and unitid is 0
80 #if CONFIG_HT_CHAIN_UNITID_BASE == 0
81         if(offset_unitid) {
82                 return;
83         }
84 #endif
85
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))))) {
88                 uint32_t id;
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))) {
93                         return;
94                 }
95         }
96
97         /* Spin through the devices and collapse any previous
98          * hypertransport enumeration.
99          */
100         for(dev = PCI_DEV(bus, 1, 0); dev <= PCI_DEV(bus, 0x1f, 0x7); dev += PCI_DEV(0, 1, 0)) {
101                 uint32_t id;
102                 uint8_t pos;
103                 uint16_t flags;
104
105                 id = pci_read_config32(dev, PCI_VENDOR_ID);
106                 if ((id == 0xffffffff) || (id == 0x00000000) ||
107                     (id == 0x0000ffff) || (id == 0xffff0000)) {
108                         continue;
109                 }
110
111                 pos = ht_lookup_slave_capability(dev);
112                 if (!pos) {
113                         continue;
114                 }
115
116                 /* Clear the unitid */
117                 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
118                 flags &= ~0x1f;
119                 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
120         }
121 }
122
123 static uint16_t ht_read_freq_cap(device_t dev, uint8_t pos)
124 {
125         /* Handle bugs in valid hypertransport frequency reporting */
126         uint16_t freq_cap;
127         uint32_t id;
128
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 */
132
133         id = pci_read_config32(dev, 0);
134
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);
138                 return freq_cap;
139         }
140
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);
144                 return freq_cap;
145         }
146
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);
153                 }
154                 #endif
155         #else
156                 freq_cap &= ~(1 << HT_FREQ_1000Mhz);
157         #endif
158         }
159
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");
162         //freq_cap &= 0x3f;
163         return freq_cap;
164 }
165
166 static uint8_t ht_read_width_cap(device_t dev, uint8_t pos)
167 {
168         uint8_t width_cap = pci_read_config8(dev, pos);
169
170         uint32_t id;
171
172         id = pci_read_config32(dev, 0);
173
174         /* netlogic micro cap doesn't support 16 bit yet */
175         if (id == (0x184e | (0x0001 << 16))) {
176                 if((width_cap & 0x77) == 0x11) {
177                         width_cap &= 0x88;
178                 }
179         }
180
181         return width_cap;
182
183 }
184
185 #define LINK_OFFS(CTRL, WIDTH,FREQ,FREQ_CAP) \
186         (((CTRL & 0xff) << 24) | ((WIDTH & 0xff) << 16) | ((FREQ & 0xff) << 8) | (FREQ_CAP & 0xFF))
187
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)
192
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)
198
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)
204
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)
210
211 static int ht_optimize_link(
212         device_t dev1, uint8_t pos1, unsigned offs1,
213         device_t dev2, uint8_t pos2, unsigned offs2)
214 {
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;
220         int needs_reset;
221         /* Set link width and frequency */
222
223         printk(BIOS_SPEW, "entering ht_optimize_link\r\n");
224         /* Initially assume everything is already optimized and I don't need a reset */
225         needs_reset = 0;
226
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);
231
232         /* Calculate the highest possible frequency */
233         freq = log2(freq_cap1 & freq_cap2);
234
235         /* See if I am changing the link freqency */
236         old_freq = pci_read_config8(dev1, pos1 + LINK_FREQ(offs1));
237         old_freq &= 0x0f;
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));
241         old_freq &= 0x0f;
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);
244
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);
248
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);
253
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;
260         }
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;
269         }
270         width |= pow2_to_link_width[ln_width1] << 4;
271         printk(BIOS_SPEW, "dev1 input|output width=0x%x\r\n", width);
272
273         /* See if I am changing dev1's width */
274         old_width = pci_read_config8(dev1, pos1 + LINK_WIDTH(offs1) + 1);
275         old_width &= 0x77;
276         needs_reset |= old_width != width;
277         printk(BIOS_SPEW, "old dev1 input|output width=0x%x\r\n", width);
278
279         /* Set dev1's widths */
280         pci_write_config8(dev1, pos1 + LINK_WIDTH(offs1) + 1, width);
281
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);
285
286         /* See if I am changing dev2's width */
287         old_width = pci_read_config8(dev2, pos2 + LINK_WIDTH(offs2) + 1);
288         old_width &= 0x77;
289         needs_reset |= old_width != width;
290         printk(BIOS_SPEW, "old dev2 input|output width=0x%x\r\n", width);
291
292         /* Set dev2's widths */
293         pci_write_config8(dev2, pos2 + LINK_WIDTH(offs2) + 1, width);
294
295         return needs_reset;
296 }
297
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)
300 #else
301 static int ht_setup_chainx(device_t udev, uint8_t upos, uint8_t bus, unsigned offset_unitid)
302 #endif
303 {
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
305
306         uint8_t next_unitid, last_unitid;
307         unsigned uoffs;
308
309 #if RAMINIT_SYSINFO == 0
310         int reset_needed = 0;
311 #endif
312
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;
317         int ht_dev_num = 0;
318         uint8_t end_used = 0;
319 #endif
320
321         uoffs = PCI_HT_HOST_OFFS;
322         next_unitid = (offset_unitid) ? CONFIG_HT_CHAIN_UNITID_BASE:1;
323
324         do {
325                 uint32_t id;
326                 uint8_t pos;
327                 uint16_t flags, ctrl;
328                 uint8_t count;
329                 unsigned offs;
330
331                 /* Wait until the link initialization is complete */
332                 do {
333                         ctrl = pci_read_config16(udev, upos + LINK_CTRL(uoffs));
334                         /* Is this the end of the hypertransport chain? */
335                         if (ctrl & (1 << 6)) {
336                                 goto end_of_chain;
337                         }
338
339                         if (ctrl & ((1 << 4) | (1 << 8))) {
340                                 /*
341                                  * Either the link has failed, or we have
342                                  * a CRC error.
343                                  * Sometimes this can happen due to link
344                                  * retrain, so lets knock it down and see
345                                  * if its transient
346                                  */
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");
352                                         break;
353                                 }
354                         }
355                 } while((ctrl & (1 << 5)) == 0);
356
357                 device_t dev = PCI_DEV(bus, 0, 0);
358                 last_unitid = next_unitid;
359
360                 id = pci_read_config32(dev, PCI_VENDOR_ID);
361
362                 /* If the chain is enumerated quit */
363                 if ((id == 0xffffffff) || (id == 0x00000000) ||
364                     (id == 0x0000ffff) || (id == 0xffff0000))
365                 {
366                         break;
367                 }
368
369                 pos = ht_lookup_slave_capability(dev);
370                 if (!pos) {
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");
375                         break;
376                 }
377
378
379 #if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
380                 if(offset_unitid) {
381                         if(next_unitid>= (bus ? 0x20:0x18) ) {
382                                 if(!end_used) {
383                                         next_unitid = CONFIG_HT_CHAIN_END_UNITID_BASE;
384                                         end_used = 1;
385                                 } else {
386                                         goto out;
387                                 }
388
389                         }
390                         real_last_pos = pos;
391                         real_last_unitid = next_unitid;
392                         ht_dev_num++;
393                 }
394 #endif
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);
400
401                 /* Compute the number of unitids consumed */
402                 count = (flags >> 5) & 0x1f;
403
404                 /* Note the change in device number */
405                 dev = PCI_DEV(bus, next_unitid, 0);
406
407                 next_unitid += count;
408
409                 /* Find which side of the ht link we are on,
410                  * by reading which direction our last write to PCI_CAP_FLAGS
411                  * came from.
412                  */
413                 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
414                 offs = ((flags>>10) & 1) ? PCI_HT_SLAVE1_OFFS : PCI_HT_SLAVE0_OFFS;
415
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 */
418                 {
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++;
427                 }
428                 #else
429                 reset_needed |= ht_optimize_link(udev, upos, uoffs, dev, pos, offs);
430                 #endif
431
432                 /* Remeber the location of the last device */
433                 udev = dev;
434                 upos = pos;
435                 uoffs = ( offs != PCI_HT_SLAVE0_OFFS ) ? PCI_HT_SLAVE0_OFFS : PCI_HT_SLAVE1_OFFS;
436
437         } while (last_unitid != next_unitid );
438
439 #if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
440 out:
441 #endif
442 end_of_chain: ;
443
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 ) {
446                 uint16_t flags;
447                 int i;
448                 flags = pci_read_config16(PCI_DEV(bus,real_last_unitid,0), real_last_pos + PCI_CAP_FLAGS);
449                 flags &= ~0x1f;
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);
452
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++)
456                 {
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);
460                                 continue;
461                         }
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);
464                         }
465                 }
466                 #endif
467
468         }
469 #endif
470
471 #if RAMINIT_SYSINFO == 0
472         return reset_needed;
473 #endif
474
475 }
476
477 #if RAMINIT_SYSINFO == 1
478 static void ht_setup_chain(device_t udev, unsigned upos, struct sys_info *sysinfo)
479 #else
480 static int ht_setup_chain(device_t udev, unsigned upos)
481 #endif
482 {
483         unsigned offset_unitid = 0;
484 #if ((CONFIG_HT_CHAIN_UNITID_BASE != 1) || (CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20))
485         offset_unitid = 1;
486 #endif
487
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.
492          */
493
494         /* Make certain the HT bus is not enumerated */
495         ht_collapse_previous_enumeration(0, 0);
496
497 #if ((CONFIG_HT_CHAIN_UNITID_BASE != 1) || (CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20))
498         offset_unitid = 1;
499 #endif
500
501 #if RAMINIT_SYSINFO == 1
502         ht_setup_chainx(udev, upos, 0, offset_unitid, sysinfo);
503 #else
504         return ht_setup_chainx(udev, upos, 0, offset_unitid);
505 #endif
506 }
507 static int optimize_link_read_pointer(uint8_t node, uint8_t linkn, uint8_t linkt, uint8_t val)
508 {
509         uint32_t dword, dword_old;
510         uint8_t link_type;
511
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;
515
516
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);
521
522                 if (dword != dword_old) {
523                         pci_write_config32(PCI_DEV(0,0x18+node,3), 0xdc, dword);
524                         return 1;
525                 }
526         }
527
528         return 0;
529 }
530
531 static int optimize_link_read_pointers_chain(uint8_t ht_c_num)
532 {
533         int reset_needed;
534         uint8_t i;
535
536         reset_needed = 0;
537
538         for (i = 0; i < ht_c_num; i++) {
539                 uint32_t reg;
540                 uint8_t nodeid, linkn;
541                 uint8_t busn;
542                 uint8_t val;
543                 unsigned devn = 1;
544
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
548                 #endif
549                         devn = CONFIG_HT_CHAIN_UNITID_BASE;
550         #endif
551
552                 reg = pci_read_config32(PCI_DEV(0,0x18,1), 0xe0 + i * 4);
553
554                 nodeid = ((reg & 0xf0)>>4); // nodeid
555                 linkn = ((reg & 0xf00)>>8); // link n
556                 busn = (reg & 0xff0000)>>16; //busn
557
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) {
560                         val = 0x25;
561                 } else if ( (reg & 0xffff) == PCI_VENDOR_ID_NVIDIA ) {
562                         val = 0x25;//???
563                 } else {
564                         continue;
565                 }
566
567                 reset_needed |= optimize_link_read_pointer(nodeid, linkn, 0x07, val);
568
569         }
570
571         return reset_needed;
572 }
573
574 static int set_ht_link_buffer_count(uint8_t node, uint8_t linkn, uint8_t linkt, unsigned val)
575 {
576         uint32_t dword;
577         uint8_t link_type;
578         unsigned regpos;
579         device_t dev;
580
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;
586
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 );
590
591                 if (dword != val) {
592                         pci_write_config32(dev, regpos, val);
593                         return 1;
594                 }
595         }
596
597         return 0;
598 }
599 static int set_ht_link_buffer_counts_chain(uint8_t ht_c_num, unsigned vendorid,  unsigned val)
600 {
601         int reset_needed;
602         uint8_t i;
603
604         reset_needed = 0;
605
606         for (i = 0; i < ht_c_num; i++) {
607                 uint32_t reg;
608                 uint8_t nodeid, linkn;
609                 uint8_t busn;
610                 unsigned devn;
611
612                 reg = pci_read_config32(PCI_DEV(0,0x18,1), 0xe0 + i * 4);
613                 if((reg & 3) != 3) continue; // not enabled
614
615                 nodeid = ((reg & 0xf0)>>4); // nodeid
616                 linkn = ((reg & 0xf00)>>8); // link n
617                 busn = (reg & 0xff0000)>>16; //busn
618
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);
623                                 break;
624                         }
625                 }
626         }
627
628         return reset_needed;
629 }
630
631
632 #if RAMINIT_SYSINFO == 1
633 static void ht_setup_chains(uint8_t ht_c_num, struct sys_info *sysinfo)
634 #else
635 static int ht_setup_chains(uint8_t ht_c_num)
636 #endif
637 {
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.
642          */
643         uint8_t upos;
644         device_t udev;
645         uint8_t i;
646
647 #if RAMINIT_SYSINFO == 0
648         int reset_needed = 0;
649 #else
650         sysinfo->link_pair_num = 0;
651 #endif
652
653         // first one is SB Chain
654         for (i = 0; i < ht_c_num; i++) {
655                 uint32_t reg;
656                 uint8_t devpos;
657                 unsigned regpos;
658                 uint32_t dword;
659                 uint8_t busn;
660                 unsigned offset_unitid = 0;
661
662                 reg = pci_read_config32(PCI_DEV(0,0x18,1), 0xe0 + i * 4);
663
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;
668
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);
673
674
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
678                 #endif
679                         offset_unitid = 1;
680         #endif
681
682                 /* Make certain the HT bus is not enumerated */
683                 ht_collapse_previous_enumeration(busn, offset_unitid);
684
685                 upos = ((reg & 0xf00)>>8) * 0x20 + 0x80;
686                 udev =  PCI_DEV(0, devpos, 0);
687
688 #if RAMINIT_SYSINFO == 1
689                 ht_setup_chainx(udev,upos,busn, offset_unitid, sysinfo); // all not
690 #else
691                 reset_needed |= ht_setup_chainx(udev,upos,busn, offset_unitid); //all not
692 #endif
693
694         }
695
696 #if RAMINIT_SYSINFO == 0
697         reset_needed |= optimize_link_read_pointers_chain(ht_c_num);
698
699         return reset_needed;
700 #endif
701
702 }
703
704 #if defined (__GNUC__)
705 static inline unsigned get_nodes(void);
706 #endif
707
708 #if RAMINIT_SYSINFO == 1
709 static void ht_setup_chains_x(struct sys_info *sysinfo)
710 #else
711 static int ht_setup_chains_x(void)
712 #endif
713 {
714         uint8_t nodeid;
715         uint32_t reg;
716         uint32_t tempreg;
717         uint8_t next_busn;
718         uint8_t ht_c_num;
719         uint8_t nodes;
720 #if K8_ALLOCATE_IO_RANGE == 1
721         unsigned next_io_base;
722 #endif
723
724         nodes = get_nodes();
725
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;
732         sysinfo->sbbusn = 0;
733         sysinfo->nodes = nodes;
734 #endif
735         tempreg = 3 | ( 0<<4) | (((reg>>8) & 3)<<8) | (0<<16)| (0x3f<<24);
736         pci_write_config32(PCI_DEV(0, 0x18, 1), 0xe0, tempreg);
737
738         next_busn=0x3f+1; /* 0 will be used ht chain with SB we need to keep SB in bus0 in auto stage*/
739
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;
747 #endif
748
749         /* clean others */
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);
752
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);
757 #endif
758         }
759
760         for(nodeid=0; nodeid<nodes; nodeid++) {
761                 device_t dev;
762                 uint8_t linkn;
763                 dev = PCI_DEV(0, 0x18+nodeid,0);
764                 for(linkn = 0; linkn<3; linkn++) {
765                         unsigned regpos;
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*/
775                                         break;
776                                 }
777                         }
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);
784                         next_busn+=0x3f+1;
785
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;
793 #endif
794
795                 }
796         }
797         /*update 0xe0, 0xe4, 0xe8, 0xec from PCI_DEV(0, 0x18,1) to PCI_DEV(0, 0x19,1) to PCI_DEV(0, 0x1f,1);*/
798
799         for(nodeid = 1; nodeid<nodes; nodeid++) {
800                 int i;
801                 device_t dev;
802                 dev = PCI_DEV(0, 0x18+nodeid,1);
803                 for(i = 0; i< 4; i++) {
804                         unsigned regpos;
805                         regpos = 0xe0 + i * 4;
806                         reg = pci_read_config32(PCI_DEV(0, 0x18, 1), regpos);
807                         pci_write_config32(dev, regpos, reg);
808                 }
809
810 #if K8_ALLOCATE_IO_RANGE == 1
811                 /* io range allocation */
812                 for(i = 0; i< 4; i++) {
813                         unsigned regpos;
814                         regpos = 0xc4 + i * 8;
815                         reg = pci_read_config32(PCI_DEV(0, 0x18, 1), regpos);
816                         pci_write_config32(dev, regpos, reg);
817                 }
818                 for(i = 0; i< 4; i++) {
819                         unsigned regpos;
820                         regpos = 0xc0 + i * 8;
821                         reg = pci_read_config32(PCI_DEV(0, 0x18, 1), regpos);
822                         pci_write_config32(dev, regpos, reg);
823                 }
824 #endif
825         }
826
827         /* recount ht_c_num*/
828         uint8_t i=0;
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)) {
832                         i++;
833                 }
834         }
835
836 #if RAMINIT_SYSINFO == 1
837         sysinfo->ht_c_num = i;
838         ht_setup_chains(i, sysinfo);
839         sysinfo->sbdn = get_sbdn(sysinfo->sbbusn);
840 #else
841         return ht_setup_chains(i);
842 #endif
843
844 }
845
846 #if RAMINIT_SYSINFO == 1
847 static int optimize_link_incoherent_ht(struct sys_info *sysinfo)
848 {
849         // We need to use recorded link pair info to optimize the link
850         int i;
851         int reset_needed = 0;
852
853         unsigned link_pair_num = sysinfo->link_pair_num;
854
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);
861         }
862
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);
865
866         return reset_needed;
867
868 }
869 #endif
870
871
872