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