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