Fix all warnings in the tree
[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\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 #if 0
71 static uint8_t ht_lookup_host_capability(device_t dev)
72 {
73         return ht_lookup_capability(dev, 1); // Host/Secondary Interface Block Format
74 }
75 #endif
76
77 static void ht_collapse_previous_enumeration(uint8_t bus, unsigned offset_unitid)
78 {
79         device_t dev;
80
81         //actually, only for one HT device HT chain, and unitid is 0
82 #if CONFIG_HT_CHAIN_UNITID_BASE == 0
83         if(offset_unitid) {
84                 return;
85         }
86 #endif
87
88         /* Check if is already collapsed */
89         if((!offset_unitid) || (offset_unitid && (!((CONFIG_HT_CHAIN_END_UNITID_BASE == 0) && (CONFIG_HT_CHAIN_END_UNITID_BASE <CONFIG_HT_CHAIN_UNITID_BASE))))) {
90                 uint32_t id;
91                 dev = PCI_DEV(bus, 0, 0);
92                 id = pci_read_config32(dev, PCI_VENDOR_ID);
93                 if (!((id == 0xffffffff) || (id == 0x00000000) ||
94                       (id == 0x0000ffff) || (id == 0xffff0000))) {
95                         return;
96                 }
97         }
98
99         /* Spin through the devices and collapse any previous
100          * hypertransport enumeration.
101          */
102         for(dev = PCI_DEV(bus, 1, 0); dev <= PCI_DEV(bus, 0x1f, 0x7); dev += PCI_DEV(0, 1, 0)) {
103                 uint32_t id;
104                 uint8_t pos;
105                 uint16_t flags;
106
107                 id = pci_read_config32(dev, PCI_VENDOR_ID);
108                 if ((id == 0xffffffff) || (id == 0x00000000) ||
109                     (id == 0x0000ffff) || (id == 0xffff0000)) {
110                         continue;
111                 }
112
113                 pos = ht_lookup_slave_capability(dev);
114                 if (!pos) {
115                         continue;
116                 }
117
118                 /* Clear the unitid */
119                 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
120                 flags &= ~0x1f;
121                 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
122         }
123 }
124
125 static uint16_t ht_read_freq_cap(device_t dev, uint8_t pos)
126 {
127         /* Handle bugs in valid hypertransport frequency reporting */
128         uint16_t freq_cap;
129         uint32_t id;
130
131         freq_cap = pci_read_config16(dev, pos);
132         printk(BIOS_SPEW, "pos=0x%x, unfiltered freq_cap=0x%x\n", pos, freq_cap);
133         freq_cap &= ~(1 << HT_FREQ_VENDOR); /* Ignore Vendor HT frequencies */
134
135         id = pci_read_config32(dev, 0);
136
137         /* AMD 8131 Errata 48 */
138         if (id == (PCI_VENDOR_ID_AMD | (PCI_DEVICE_ID_AMD_8131_PCIX << 16))) {
139                 freq_cap &= ~(1 << HT_FREQ_800Mhz);
140                 return freq_cap;
141         }
142
143         /* AMD 8151 Errata 23 */
144         if (id == (PCI_VENDOR_ID_AMD | (PCI_DEVICE_ID_AMD_8151_SYSCTRL << 16))) {
145                 freq_cap &= ~(1 << HT_FREQ_800Mhz);
146                 return freq_cap;
147         }
148
149         /* AMD K8 Unsupported 1Ghz? */
150         if (id == (PCI_VENDOR_ID_AMD | (0x1100 << 16))) {
151         #if CONFIG_K8_HT_FREQ_1G_SUPPORT == 1
152                 #if CONFIG_K8_REV_F_SUPPORT == 0
153                 if (is_cpu_pre_e0()) {  // only E0 later support 1GHz
154                         freq_cap &= ~(1 << HT_FREQ_1000Mhz);
155                 }
156                 #endif
157         #else
158                 freq_cap &= ~(1 << HT_FREQ_1000Mhz);
159         #endif
160         }
161
162         printk(BIOS_SPEW, "pos=0x%x, filtered freq_cap=0x%x\n", pos, freq_cap);
163         //printk(BIOS_SPEW, "capping to 800/600/400/200 MHz\n");
164         //freq_cap &= 0x3f;
165         return freq_cap;
166 }
167
168 static uint8_t ht_read_width_cap(device_t dev, uint8_t pos)
169 {
170         uint8_t width_cap = pci_read_config8(dev, pos);
171
172         uint32_t id;
173
174         id = pci_read_config32(dev, 0);
175
176         /* netlogic micro cap doesn't support 16 bit yet */
177         if (id == (0x184e | (0x0001 << 16))) {
178                 if((width_cap & 0x77) == 0x11) {
179                         width_cap &= 0x88;
180                 }
181         }
182
183         return width_cap;
184
185 }
186
187 #define LINK_OFFS(CTRL, WIDTH,FREQ,FREQ_CAP) \
188         (((CTRL & 0xff) << 24) | ((WIDTH & 0xff) << 16) | ((FREQ & 0xff) << 8) | (FREQ_CAP & 0xFF))
189
190 #define LINK_CTRL(OFFS)     ((OFFS >> 24) & 0xFF)
191 #define LINK_WIDTH(OFFS)    ((OFFS >> 16) & 0xFF)
192 #define LINK_FREQ(OFFS)     ((OFFS >> 8) & 0xFF)
193 #define LINK_FREQ_CAP(OFFS) ((OFFS) & 0xFF)
194
195 #define PCI_HT_HOST_OFFS LINK_OFFS(             \
196                 PCI_HT_CAP_HOST_CTRL,           \
197                 PCI_HT_CAP_HOST_WIDTH,          \
198                 PCI_HT_CAP_HOST_FREQ,           \
199                 PCI_HT_CAP_HOST_FREQ_CAP)
200
201 #define PCI_HT_SLAVE0_OFFS LINK_OFFS(           \
202                 PCI_HT_CAP_SLAVE_CTRL0,         \
203                 PCI_HT_CAP_SLAVE_WIDTH0,        \
204                 PCI_HT_CAP_SLAVE_FREQ0,         \
205                 PCI_HT_CAP_SLAVE_FREQ_CAP0)
206
207 #define PCI_HT_SLAVE1_OFFS LINK_OFFS(           \
208                 PCI_HT_CAP_SLAVE_CTRL1,         \
209                 PCI_HT_CAP_SLAVE_WIDTH1,        \
210                 PCI_HT_CAP_SLAVE_FREQ1,         \
211                 PCI_HT_CAP_SLAVE_FREQ_CAP1)
212
213 static int ht_optimize_link(
214         device_t dev1, uint8_t pos1, unsigned offs1,
215         device_t dev2, uint8_t pos2, unsigned offs2)
216 {
217         static const uint8_t link_width_to_pow2[]= { 3, 4, 0, 5, 1, 2, 0, 0 };
218         static const uint8_t pow2_to_link_width[] = { 0x7, 4, 5, 0, 1, 3 };
219         uint16_t freq_cap1, freq_cap2;
220         uint8_t width_cap1, width_cap2, width, old_width, ln_width1, ln_width2;
221         uint8_t freq, old_freq;
222         int needs_reset;
223         /* Set link width and frequency */
224
225         printk(BIOS_SPEW, "entering ht_optimize_link\n");
226         /* Initially assume everything is already optimized and I don't need a reset */
227         needs_reset = 0;
228
229         /* Get the frequency capabilities */
230         freq_cap1 = ht_read_freq_cap(dev1, pos1 + LINK_FREQ_CAP(offs1));
231         freq_cap2 = ht_read_freq_cap(dev2, pos2 + LINK_FREQ_CAP(offs2));
232         printk(BIOS_SPEW, "freq_cap1=0x%x, freq_cap2=0x%x\n", freq_cap1, freq_cap2);
233
234         /* Calculate the highest possible frequency */
235         freq = log2(freq_cap1 & freq_cap2);
236
237         /* See if I am changing the link freqency */
238         old_freq = pci_read_config8(dev1, pos1 + LINK_FREQ(offs1));
239         old_freq &= 0x0f;
240         needs_reset |= old_freq != freq;
241         printk(BIOS_SPEW, "dev1 old_freq=0x%x, freq=0x%x, needs_reset=0x%0x\n", old_freq, freq, needs_reset);
242         old_freq = pci_read_config8(dev2, pos2 + LINK_FREQ(offs2));
243         old_freq &= 0x0f;
244         needs_reset |= old_freq != freq;
245         printk(BIOS_SPEW, "dev2 old_freq=0x%x, freq=0x%x, needs_reset=0x%0x\n", old_freq, freq, needs_reset);
246
247         /* Set the Calculated link frequency */
248         pci_write_config8(dev1, pos1 + LINK_FREQ(offs1), freq);
249         pci_write_config8(dev2, pos2 + LINK_FREQ(offs2), freq);
250
251         /* Get the width capabilities */
252         width_cap1 = ht_read_width_cap(dev1, pos1 + LINK_WIDTH(offs1));
253         width_cap2 = ht_read_width_cap(dev2, pos2 + LINK_WIDTH(offs2));
254         printk(BIOS_SPEW, "width_cap1=0x%x, width_cap2=0x%x\n", width_cap1, width_cap2);
255
256         /* Calculate dev1's input width */
257         ln_width1 = link_width_to_pow2[width_cap1 & 7];
258         ln_width2 = link_width_to_pow2[(width_cap2 >> 4) & 7];
259         printk(BIOS_SPEW, "dev1 input ln_width1=0x%x, ln_width2=0x%x\n", ln_width1, ln_width2);
260         if (ln_width1 > ln_width2) {
261                 ln_width1 = ln_width2;
262         }
263         width = pow2_to_link_width[ln_width1];
264         printk(BIOS_SPEW, "dev1 input width=0x%x\n", width);
265         /* Calculate dev1's output width */
266         ln_width1 = link_width_to_pow2[(width_cap1 >> 4) & 7];
267         ln_width2 = link_width_to_pow2[width_cap2 & 7];
268         printk(BIOS_SPEW, "dev1 output ln_width1=0x%x, ln_width2=0x%x\n", ln_width1, ln_width2);
269         if (ln_width1 > ln_width2) {
270                 ln_width1 = ln_width2;
271         }
272         width |= pow2_to_link_width[ln_width1] << 4;
273         printk(BIOS_SPEW, "dev1 input|output width=0x%x\n", width);
274
275         /* See if I am changing dev1's width */
276         old_width = pci_read_config8(dev1, pos1 + LINK_WIDTH(offs1) + 1);
277         old_width &= 0x77;
278         needs_reset |= old_width != width;
279         printk(BIOS_SPEW, "old dev1 input|output width=0x%x\n", width);
280
281         /* Set dev1's widths */
282         pci_write_config8(dev1, pos1 + LINK_WIDTH(offs1) + 1, width);
283
284         /* Calculate dev2's width */
285         width = ((width & 0x70) >> 4) | ((width & 0x7) << 4);
286         printk(BIOS_SPEW, "dev2 input|output width=0x%x\n", width);
287
288         /* See if I am changing dev2's width */
289         old_width = pci_read_config8(dev2, pos2 + LINK_WIDTH(offs2) + 1);
290         old_width &= 0x77;
291         needs_reset |= old_width != width;
292         printk(BIOS_SPEW, "old dev2 input|output width=0x%x\n", width);
293
294         /* Set dev2's widths */
295         pci_write_config8(dev2, pos2 + LINK_WIDTH(offs2) + 1, width);
296
297         return needs_reset;
298 }
299
300 #if RAMINIT_SYSINFO == 1
301 static void ht_setup_chainx(device_t udev, uint8_t upos, uint8_t bus, unsigned offset_unitid, struct sys_info *sysinfo)
302 #else
303 static int ht_setup_chainx(device_t udev, uint8_t upos, uint8_t bus, unsigned offset_unitid)
304 #endif
305 {
306         //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
307
308         uint8_t next_unitid, last_unitid;
309         unsigned uoffs;
310
311 #if RAMINIT_SYSINFO == 0
312         int reset_needed = 0;
313 #endif
314
315 #if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
316         //let't record the device of last ht device, So we can set the Unitid to CONFIG_HT_CHAIN_END_UNITID_BASE
317         unsigned real_last_unitid;
318         uint8_t real_last_pos;
319         int ht_dev_num = 0;
320         uint8_t end_used = 0;
321 #endif
322
323         uoffs = PCI_HT_HOST_OFFS;
324         next_unitid = (offset_unitid) ? CONFIG_HT_CHAIN_UNITID_BASE:1;
325
326         do {
327                 uint32_t id;
328                 uint8_t pos;
329                 uint16_t flags, ctrl;
330                 uint8_t count;
331                 unsigned offs;
332
333                 /* Wait until the link initialization is complete */
334                 do {
335                         ctrl = pci_read_config16(udev, upos + LINK_CTRL(uoffs));
336                         /* Is this the end of the hypertransport chain? */
337                         if (ctrl & (1 << 6)) {
338                                 goto end_of_chain;
339                         }
340
341                         if (ctrl & ((1 << 4) | (1 << 8))) {
342                                 /*
343                                  * Either the link has failed, or we have
344                                  * a CRC error.
345                                  * Sometimes this can happen due to link
346                                  * retrain, so lets knock it down and see
347                                  * if its transient
348                                  */
349                                 ctrl |= ((1 << 4) | (1 <<8)); // Link fail + Crc
350                                 pci_write_config16(udev, upos + LINK_CTRL(uoffs), ctrl);
351                                 ctrl = pci_read_config16(udev, upos + LINK_CTRL(uoffs));
352                                 if (ctrl & ((1 << 4) | (1 << 8))) {
353                                         print_err("Detected error on Hypertransport Link\n");
354                                         break;
355                                 }
356                         }
357                 } while((ctrl & (1 << 5)) == 0);
358
359                 device_t dev = PCI_DEV(bus, 0, 0);
360                 last_unitid = next_unitid;
361
362                 id = pci_read_config32(dev, PCI_VENDOR_ID);
363
364                 /* If the chain is enumerated quit */
365                 if ((id == 0xffffffff) || (id == 0x00000000) ||
366                     (id == 0x0000ffff) || (id == 0xffff0000))
367                 {
368                         break;
369                 }
370
371                 pos = ht_lookup_slave_capability(dev);
372                 if (!pos) {
373                         print_err("udev="); print_err_hex32(udev);
374                         print_err("\tupos="); print_err_hex32(upos);
375                         print_err("\tuoffs="); print_err_hex32(uoffs);
376                         print_err("\tHT link capability not found\n");
377                         break;
378                 }
379
380
381 #if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
382                 if(offset_unitid) {
383                         if(next_unitid>= (bus ? 0x20:0x18) ) {
384                                 if(!end_used) {
385                                         next_unitid = CONFIG_HT_CHAIN_END_UNITID_BASE;
386                                         end_used = 1;
387                                 } else {
388                                         goto out;
389                                 }
390
391                         }
392                         real_last_pos = pos;
393                         real_last_unitid = next_unitid;
394                         ht_dev_num++;
395                 }
396 #endif
397                 /* Update the Unitid of the current device */
398                 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
399                 flags &= ~0x1f; /* mask out the base Unit ID */
400                 flags |= next_unitid & 0x1f;
401                 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
402
403                 /* Compute the number of unitids consumed */
404                 count = (flags >> 5) & 0x1f;
405
406                 /* Note the change in device number */
407                 dev = PCI_DEV(bus, next_unitid, 0);
408
409                 next_unitid += count;
410
411                 /* Find which side of the ht link we are on,
412                  * by reading which direction our last write to PCI_CAP_FLAGS
413                  * came from.
414                  */
415                 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
416                 offs = ((flags>>10) & 1) ? PCI_HT_SLAVE1_OFFS : PCI_HT_SLAVE0_OFFS;
417
418                 #if RAMINIT_SYSINFO == 1
419                 /* store the link pair here and we will Setup the Hypertransport link later, after we get final FID/VID */
420                 {
421                         struct link_pair_st *link_pair = &sysinfo->link_pair[sysinfo->link_pair_num];
422                         link_pair->udev = udev;
423                         link_pair->upos = upos;
424                         link_pair->uoffs = uoffs;
425                         link_pair->dev = dev;
426                         link_pair->pos = pos;
427                         link_pair->offs = offs;
428                         sysinfo->link_pair_num++;
429                 }
430                 #else
431                 reset_needed |= ht_optimize_link(udev, upos, uoffs, dev, pos, offs);
432                 #endif
433
434                 /* Remeber the location of the last device */
435                 udev = dev;
436                 upos = pos;
437                 uoffs = ( offs != PCI_HT_SLAVE0_OFFS ) ? PCI_HT_SLAVE0_OFFS : PCI_HT_SLAVE1_OFFS;
438
439         } while (last_unitid != next_unitid );
440
441 #if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
442 out:
443 #endif
444 end_of_chain: ;
445
446 #if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
447         if(offset_unitid && (ht_dev_num>1) && (real_last_unitid != CONFIG_HT_CHAIN_END_UNITID_BASE) && !end_used ) {
448                 uint16_t flags;
449                 flags = pci_read_config16(PCI_DEV(bus,real_last_unitid,0), real_last_pos + PCI_CAP_FLAGS);
450                 flags &= ~0x1f;
451                 flags |= CONFIG_HT_CHAIN_END_UNITID_BASE & 0x1f;
452                 pci_write_config16(PCI_DEV(bus, real_last_unitid, 0), real_last_pos + PCI_CAP_FLAGS, flags);
453
454                 #if RAMINIT_SYSINFO == 1
455                 // Here need to change the dev in the array
456                 int i;
457                 for(i=0;i<sysinfo->link_pair_num;i++)
458                 {
459                         struct link_pair_st *link_pair = &sysinfo->link_pair[i];
460                         if(link_pair->udev == PCI_DEV(bus, real_last_unitid, 0)) {
461                                 link_pair->udev = PCI_DEV(bus, CONFIG_HT_CHAIN_END_UNITID_BASE, 0);
462                                 continue;
463                         }
464                         if(link_pair->dev == PCI_DEV(bus, real_last_unitid, 0)) {
465                                 link_pair->dev = PCI_DEV(bus, CONFIG_HT_CHAIN_END_UNITID_BASE, 0);
466                         }
467                 }
468                 #endif
469
470         }
471 #endif
472
473 #if RAMINIT_SYSINFO == 0
474         return reset_needed;
475 #endif
476
477 }
478
479 #if 0
480 #if RAMINIT_SYSINFO == 1
481 static void ht_setup_chain(device_t udev, unsigned upos, struct sys_info *sysinfo)
482 #else
483 static int ht_setup_chain(device_t udev, unsigned upos)
484 #endif
485 {
486         unsigned offset_unitid = 0;
487 #if ((CONFIG_HT_CHAIN_UNITID_BASE != 1) || (CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20))
488         offset_unitid = 1;
489 #endif
490
491         /* Assumption the HT chain that is bus 0 has the HT I/O Hub on it.
492          * On most boards this just happens.  If a cpu has multiple
493          * non Coherent links the appropriate bus registers for the
494          * links needs to be programed to point at bus 0.
495          */
496
497         /* Make certain the HT bus is not enumerated */
498         ht_collapse_previous_enumeration(0, 0);
499
500 #if ((CONFIG_HT_CHAIN_UNITID_BASE != 1) || (CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20))
501         offset_unitid = 1;
502 #endif
503
504 #if RAMINIT_SYSINFO == 1
505         ht_setup_chainx(udev, upos, 0, offset_unitid, sysinfo);
506 #else
507         return ht_setup_chainx(udev, upos, 0, offset_unitid);
508 #endif
509 }
510 #endif
511
512 static int optimize_link_read_pointer(uint8_t node, uint8_t linkn, uint8_t linkt, uint8_t val)
513 {
514         uint32_t dword, dword_old;
515         uint8_t link_type;
516
517         /* This works on an Athlon64 because unimplemented links return 0 */
518         dword = pci_read_config32(PCI_DEV(0,0x18+node,0), 0x98 + (linkn * 0x20));
519         link_type = dword & 0xff;
520
521
522         if ( (link_type & 7) == linkt ) { /* Coherent Link only linkt = 3, ncoherent = 7*/
523                 dword_old = dword = pci_read_config32(PCI_DEV(0,0x18+node,3), 0xdc);
524                 dword &= ~( 0xff<<(linkn *8) );
525                 dword |= val << (linkn *8);
526
527                 if (dword != dword_old) {
528                         pci_write_config32(PCI_DEV(0,0x18+node,3), 0xdc, dword);
529                         return 1;
530                 }
531         }
532
533         return 0;
534 }
535
536 static int optimize_link_read_pointers_chain(uint8_t ht_c_num)
537 {
538         int reset_needed;
539         uint8_t i;
540
541         reset_needed = 0;
542
543         for (i = 0; i < ht_c_num; i++) {
544                 uint32_t reg;
545                 uint8_t nodeid, linkn;
546                 uint8_t busn;
547                 uint8_t val;
548                 unsigned devn = 1;
549
550         #if ((CONFIG_HT_CHAIN_UNITID_BASE != 1) || (CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20))
551                 #if CONFIG_SB_HT_CHAIN_UNITID_OFFSET_ONLY == 1
552                 if(i==0) // to check if it is sb ht chain
553                 #endif
554                         devn = CONFIG_HT_CHAIN_UNITID_BASE;
555         #endif
556
557                 reg = pci_read_config32(PCI_DEV(0,0x18,1), 0xe0 + i * 4);
558
559                 nodeid = ((reg & 0xf0)>>4); // nodeid
560                 linkn = ((reg & 0xf00)>>8); // link n
561                 busn = (reg & 0xff0000)>>16; //busn
562
563                 reg = pci_read_config32( PCI_DEV(busn, devn, 0), PCI_VENDOR_ID); // ? the chain dev maybe offseted
564                 if ( (reg & 0xffff) == PCI_VENDOR_ID_AMD) {
565                         val = 0x25;
566                 } else if ( (reg & 0xffff) == PCI_VENDOR_ID_NVIDIA ) {
567                         val = 0x25;//???
568                 } else {
569                         continue;
570                 }
571
572                 reset_needed |= optimize_link_read_pointer(nodeid, linkn, 0x07, val);
573
574         }
575
576         return reset_needed;
577 }
578
579 #if defined(CONFIG_SOUTHBRIDGE_NVIDIA_CK804) // || defined(CONFIG_SOUTHBRIDGE_NVIDIA_MCP55)
580 static int set_ht_link_buffer_count(uint8_t node, uint8_t linkn, uint8_t linkt, unsigned val)
581 {
582         uint32_t dword;
583         uint8_t link_type;
584         unsigned regpos;
585         device_t dev;
586
587         /* This works on an Athlon64 because unimplemented links return 0 */
588         regpos = 0x98 + (linkn * 0x20);
589         dev = PCI_DEV(0,0x18+node,0);
590         dword = pci_read_config32(dev, regpos);
591         link_type = dword & 0xff;
592
593         if ( (link_type & 0x7) == linkt ) { /* Coherent Link only linkt = 3, ncoherent = 7*/
594                 regpos = 0x90 + (linkn * 0x20);
595                 dword = pci_read_config32(dev, regpos );
596
597                 if (dword != val) {
598                         pci_write_config32(dev, regpos, val);
599                         return 1;
600                 }
601         }
602
603         return 0;
604 }
605
606 static int set_ht_link_buffer_counts_chain(uint8_t ht_c_num, unsigned vendorid,  unsigned val)
607 {
608         int reset_needed;
609         uint8_t i;
610
611         reset_needed = 0;
612
613         for (i = 0; i < ht_c_num; i++) {
614                 uint32_t reg;
615                 uint8_t nodeid, linkn;
616                 uint8_t busn;
617                 unsigned devn;
618
619                 reg = pci_read_config32(PCI_DEV(0,0x18,1), 0xe0 + i * 4);
620                 if((reg & 3) != 3) continue; // not enabled
621
622                 nodeid = ((reg & 0xf0)>>4); // nodeid
623                 linkn = ((reg & 0xf00)>>8); // link n
624                 busn = (reg & 0xff0000)>>16; //busn
625
626                 for(devn = 0; devn < 0x20; devn++) {
627                         reg = pci_read_config32( PCI_DEV(busn, devn, 0), PCI_VENDOR_ID); //1?
628                         if ( (reg & 0xffff) == vendorid ) {
629                                 reset_needed |= set_ht_link_buffer_count(nodeid, linkn, 0x07,val);
630                                 break;
631                         }
632                 }
633         }
634
635         return reset_needed;
636 }
637 #endif
638
639 #if RAMINIT_SYSINFO == 1
640 static void ht_setup_chains(uint8_t ht_c_num, struct sys_info *sysinfo)
641 #else
642 static int ht_setup_chains(uint8_t ht_c_num)
643 #endif
644 {
645         /* Assumption the HT chain that is bus 0 has the HT I/O Hub on it.
646          * On most boards this just happens.  If a cpu has multiple
647          * non Coherent links the appropriate bus registers for the
648          * links needs to be programed to point at bus 0.
649          */
650         uint8_t upos;
651         device_t udev;
652         uint8_t i;
653
654 #if RAMINIT_SYSINFO == 0
655         int reset_needed = 0;
656 #else
657         sysinfo->link_pair_num = 0;
658 #endif
659
660         // first one is SB Chain
661         for (i = 0; i < ht_c_num; i++) {
662                 uint32_t reg;
663                 uint8_t devpos;
664                 unsigned regpos;
665                 uint32_t dword;
666                 uint8_t busn;
667                 unsigned offset_unitid = 0;
668
669                 reg = pci_read_config32(PCI_DEV(0,0x18,1), 0xe0 + i * 4);
670
671                 //We need setup 0x94, 0xb4, and 0xd4 according to the reg
672                 devpos = ((reg & 0xf0)>>4)+0x18; // nodeid; it will decide 0x18 or 0x19
673                 regpos = ((reg & 0xf00)>>8) * 0x20 + 0x94; // link n; it will decide 0x94 or 0xb4, 0x0xd4;
674                 busn = (reg & 0xff0000)>>16;
675
676                 dword = pci_read_config32( PCI_DEV(0, devpos, 0), regpos) ;
677                 dword &= ~(0xffff<<8);
678                 dword |= (reg & 0xffff0000)>>8;
679                 pci_write_config32( PCI_DEV(0, devpos,0), regpos , dword);
680
681
682         #if ((CONFIG_HT_CHAIN_UNITID_BASE != 1) || (CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20))
683                 #if CONFIG_SB_HT_CHAIN_UNITID_OFFSET_ONLY == 1
684                 if(i==0) // to check if it is sb ht chain
685                 #endif
686                         offset_unitid = 1;
687         #endif
688
689                 /* Make certain the HT bus is not enumerated */
690                 ht_collapse_previous_enumeration(busn, offset_unitid);
691
692                 upos = ((reg & 0xf00)>>8) * 0x20 + 0x80;
693                 udev =  PCI_DEV(0, devpos, 0);
694
695 #if RAMINIT_SYSINFO == 1
696                 ht_setup_chainx(udev,upos,busn, offset_unitid, sysinfo); // all not
697 #else
698                 reset_needed |= ht_setup_chainx(udev,upos,busn, offset_unitid); //all not
699 #endif
700
701         }
702
703 #if RAMINIT_SYSINFO == 0
704         reset_needed |= optimize_link_read_pointers_chain(ht_c_num);
705
706         return reset_needed;
707 #endif
708
709 }
710
711 #if defined (__GNUC__)
712 static inline unsigned get_nodes(void);
713 #endif
714
715 #if RAMINIT_SYSINFO == 1
716 static void ht_setup_chains_x(struct sys_info *sysinfo)
717 #else
718 static int ht_setup_chains_x(void)
719 #endif
720 {
721         uint8_t nodeid;
722         uint32_t reg;
723         uint32_t tempreg;
724         uint8_t next_busn;
725         uint8_t ht_c_num;
726         uint8_t nodes;
727 #if K8_ALLOCATE_IO_RANGE == 1
728         unsigned next_io_base;
729 #endif
730
731         nodes = get_nodes();
732
733         /* read PCI_DEV(0,0x18,0) 0x64 bit [8:9] to find out SbLink m */
734         reg = pci_read_config32(PCI_DEV(0, 0x18, 0), 0x64);
735         /* update PCI_DEV(0, 0x18, 1) 0xe0 to 0x05000m03, and next_busn=0x3f+1 */
736         print_linkn_in("SBLink=", ((reg>>8) & 3) );
737 #if RAMINIT_SYSINFO == 1
738         sysinfo->sblk = (reg>>8) & 3;
739         sysinfo->sbbusn = 0;
740         sysinfo->nodes = nodes;
741 #endif
742         tempreg = 3 | ( 0<<4) | (((reg>>8) & 3)<<8) | (0<<16)| (0x3f<<24);
743         pci_write_config32(PCI_DEV(0, 0x18, 1), 0xe0, tempreg);
744
745         next_busn=0x3f+1; /* 0 will be used ht chain with SB we need to keep SB in bus0 in auto stage*/
746
747 #if K8_ALLOCATE_IO_RANGE == 1
748         /* io range allocation */
749         tempreg = 0 | (((reg>>8) & 0x3) << 4 )|  (0x3<<12); //limit
750         pci_write_config32(PCI_DEV(0, 0x18, 1), 0xC4, tempreg);
751         tempreg = 3 | ( 3<<4) | (0<<12);        //base
752         pci_write_config32(PCI_DEV(0, 0x18, 1), 0xC0, tempreg);
753         next_io_base = 0x3+0x1;
754 #endif
755
756         /* clean others */
757         for(ht_c_num=1;ht_c_num<4; ht_c_num++) {
758                 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4, 0);
759
760 #if K8_ALLOCATE_IO_RANGE == 1
761                 /* io range allocation */
762                 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xc4 + ht_c_num * 8, 0);
763                 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xc0 + ht_c_num * 8, 0);
764 #endif
765         }
766
767         for(nodeid=0; nodeid<nodes; nodeid++) {
768                 device_t dev;
769                 uint8_t linkn;
770                 dev = PCI_DEV(0, 0x18+nodeid,0);
771                 for(linkn = 0; linkn<3; linkn++) {
772                         unsigned regpos;
773                         regpos = 0x98 + 0x20 * linkn;
774                         reg = pci_read_config32(dev, regpos);
775                         if ((reg & 0x17) != 7) continue; /* it is not non conherent or not connected*/
776                         print_linkn_in("NC node|link=", ((nodeid & 0xf)<<4)|(linkn & 0xf));
777                         tempreg = 3 | (nodeid <<4) | (linkn<<8);
778                         /*compare (temp & 0xffff), with (PCI(0, 0x18, 1) 0xe0 to 0xec & 0xfffff) */
779                         for(ht_c_num=0;ht_c_num<4; ht_c_num++) {
780                                 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4);
781                                 if(((reg & 0xffff) == (tempreg & 0xffff)) || ((reg & 0xffff) == 0x0000)) {  /*we got it*/
782                                         break;
783                                 }
784                         }
785                         if(ht_c_num == 4) break; /*used up only 4 non conherent allowed*/
786                         /*update to 0xe0...*/
787                         if((reg & 0xf) == 3) continue; /*SbLink so don't touch it */
788                         print_linkn_in("\tbusn=", next_busn);
789                         tempreg |= (next_busn<<16)|((next_busn+0x3f)<<24);
790                         pci_write_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4, tempreg);
791                         next_busn+=0x3f+1;
792
793 #if K8_ALLOCATE_IO_RANGE == 1
794                         /* io range allocation */
795                         tempreg = nodeid | (linkn<<4) |  ((next_io_base+0x3)<<12); //limit
796                         pci_write_config32(PCI_DEV(0, 0x18, 1), 0xC4 + ht_c_num * 8, tempreg);
797                         tempreg = 3 /*| ( 3<<4)*/ | (next_io_base<<12); //base :ISA and VGA ?
798                         pci_write_config32(PCI_DEV(0, 0x18, 1), 0xC0 + ht_c_num * 8, tempreg);
799                         next_io_base += 0x3+0x1;
800 #endif
801
802                 }
803         }
804         /*update 0xe0, 0xe4, 0xe8, 0xec from PCI_DEV(0, 0x18,1) to PCI_DEV(0, 0x19,1) to PCI_DEV(0, 0x1f,1);*/
805
806         for(nodeid = 1; nodeid<nodes; nodeid++) {
807                 int i;
808                 device_t dev;
809                 dev = PCI_DEV(0, 0x18+nodeid,1);
810                 for(i = 0; i< 4; i++) {
811                         unsigned regpos;
812                         regpos = 0xe0 + i * 4;
813                         reg = pci_read_config32(PCI_DEV(0, 0x18, 1), regpos);
814                         pci_write_config32(dev, regpos, reg);
815                 }
816
817 #if K8_ALLOCATE_IO_RANGE == 1
818                 /* io range allocation */
819                 for(i = 0; i< 4; i++) {
820                         unsigned regpos;
821                         regpos = 0xc4 + i * 8;
822                         reg = pci_read_config32(PCI_DEV(0, 0x18, 1), regpos);
823                         pci_write_config32(dev, regpos, reg);
824                 }
825                 for(i = 0; i< 4; i++) {
826                         unsigned regpos;
827                         regpos = 0xc0 + i * 8;
828                         reg = pci_read_config32(PCI_DEV(0, 0x18, 1), regpos);
829                         pci_write_config32(dev, regpos, reg);
830                 }
831 #endif
832         }
833
834         /* recount ht_c_num*/
835         uint8_t i=0;
836         for(ht_c_num=0;ht_c_num<4; ht_c_num++) {
837                 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4);
838                 if(((reg & 0xf) != 0x0)) {
839                         i++;
840                 }
841         }
842
843 #if RAMINIT_SYSINFO == 1
844         sysinfo->ht_c_num = i;
845         ht_setup_chains(i, sysinfo);
846         sysinfo->sbdn = get_sbdn(sysinfo->sbbusn);
847 #else
848         return ht_setup_chains(i);
849 #endif
850
851 }
852
853 #if RAMINIT_SYSINFO == 1
854 static int optimize_link_incoherent_ht(struct sys_info *sysinfo)
855 {
856         // We need to use recorded link pair info to optimize the link
857         int i;
858         int reset_needed = 0;
859
860         unsigned link_pair_num = sysinfo->link_pair_num;
861
862         printk(BIOS_SPEW, "entering optimize_link_incoherent_ht\n");
863         printk(BIOS_SPEW, "sysinfo->link_pair_num=0x%x\n", link_pair_num);
864         for(i=0; i< link_pair_num; i++) {
865                 struct link_pair_st *link_pair= &sysinfo->link_pair[i];
866                 reset_needed |= ht_optimize_link(link_pair->udev, link_pair->upos, link_pair->uoffs, link_pair->dev, link_pair->pos, link_pair->offs);
867                 printk(BIOS_SPEW, "after ht_optimize_link for link pair %d, reset_needed=0x%x\n", i, reset_needed);
868         }
869
870         reset_needed |= optimize_link_read_pointers_chain(sysinfo->ht_c_num);
871         printk(BIOS_SPEW, "after optimize_link_read_pointers_chain, reset_needed=0x%x\n", reset_needed);
872
873         return reset_needed;
874
875 }
876 #endif
877
878
879