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