e348b67286098d7514944847f5a917edf06e3865
[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 */
5 #include <device/pci_def.h>
6 #include <device/pci_ids.h>
7 #include <device/hypertransport_def.h>
8
9 static inline void print_linkn_in (const char *strval, uint8_t byteval)
10 {
11         print_debug(strval); print_debug_hex8(byteval); print_debug("\r\n");
12 }
13
14 static uint8_t ht_lookup_slave_capability(device_t dev)
15 {
16         uint8_t pos;
17         uint8_t hdr_type;
18
19         hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
20         pos = 0;
21         hdr_type &= 0x7f;
22
23         if ((hdr_type == PCI_HEADER_TYPE_NORMAL) ||
24             (hdr_type == PCI_HEADER_TYPE_BRIDGE)) {
25                 pos = PCI_CAPABILITY_LIST;
26         }
27         if (pos > PCI_CAP_LIST_NEXT) {
28                 pos = pci_read_config8(dev, pos);
29         }
30         while(pos != 0) { /* loop through the linked list */
31                 uint8_t cap;
32                 cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
33                 if (cap == PCI_CAP_ID_HT) {
34                         uint16_t flags;
35
36                         flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
37                         if ((flags >> 13) == 0) {
38                                 /* Entry is a Slave secondary, success... */
39                                 break;
40                         }
41                 }
42                 pos = pci_read_config8(dev, pos + PCI_CAP_LIST_NEXT);
43         }
44         return pos;
45 }
46
47 static void ht_collapse_previous_enumeration(uint8_t bus)
48 {
49         device_t dev;
50         uint32_t id;
51
52         /* Check if is already collapsed */
53         dev = PCI_DEV(bus, 0, 0);
54         id = pci_read_config32(dev, PCI_VENDOR_ID);
55         if ( ! ( (id == 0xffffffff) || (id == 0x00000000) ||
56             (id == 0x0000ffff) || (id == 0xffff0000) ) ) {
57                      return;
58         }
59
60         /* Spin through the devices and collapse any previous
61          * hypertransport enumeration.
62          */
63         for(dev = PCI_DEV(bus, 1, 0); dev <= PCI_DEV(bus, 0x1f, 0x7); dev += PCI_DEV(0, 1, 0)) {
64                 uint32_t id;
65                 uint8_t pos;
66                 uint16_t flags;
67                 
68                 id = pci_read_config32(dev, PCI_VENDOR_ID);
69                 if ((id == 0xffffffff) || (id == 0x00000000) ||
70                     (id == 0x0000ffff) || (id == 0xffff0000)) {
71                         continue;
72                 }
73                 
74                 pos = ht_lookup_slave_capability(dev);
75                 if (!pos) {
76                         continue;
77                 }
78
79                 /* Clear the unitid */
80                 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
81                 flags &= ~0x1f;
82                 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
83         }
84 }
85
86 static uint16_t ht_read_freq_cap(device_t dev, uint8_t pos)
87 {
88         /* Handle bugs in valid hypertransport frequency reporting */
89         uint16_t freq_cap;
90         uint32_t id;
91
92         freq_cap = pci_read_config16(dev, pos);
93         freq_cap &= ~(1 << HT_FREQ_VENDOR); /* Ignore Vendor HT frequencies */
94
95         id = pci_read_config32(dev, 0);
96
97         /* AMD 8131 Errata 48 */
98         if (id == (PCI_VENDOR_ID_AMD | (PCI_DEVICE_ID_AMD_8131_PCIX << 16))) {
99                 freq_cap &= ~(1 << HT_FREQ_800Mhz);
100         }
101         /* AMD 8151 Errata 23 */
102         if (id == (PCI_VENDOR_ID_AMD | (PCI_DEVICE_ID_AMD_8151_SYSCTRL << 16))) {
103                 freq_cap &= ~(1 << HT_FREQ_800Mhz);
104         }
105         /* AMD K8 Unsupported 1Ghz? */
106         if (id == (PCI_VENDOR_ID_AMD | (0x1100 << 16))) {
107                 freq_cap &= ~(1 << HT_FREQ_1000Mhz);
108         }
109         return freq_cap;
110 }
111
112 #define LINK_OFFS(WIDTH,FREQ,FREQ_CAP)                                  \
113         (((WIDTH & 0xff) << 16) | ((FREQ & 0xff) << 8) | (FREQ_CAP & 0xFF))
114
115 #define LINK_WIDTH(OFFS)    ((OFFS >> 16) & 0xFF)
116 #define LINK_FREQ(OFFS)     ((OFFS >> 8) & 0xFF)
117 #define LINK_FREQ_CAP(OFFS) ((OFFS) & 0xFF)
118
119 #define PCI_HT_HOST_OFFS LINK_OFFS(             \
120                 PCI_HT_CAP_HOST_WIDTH,          \
121                 PCI_HT_CAP_HOST_FREQ,           \
122                 PCI_HT_CAP_HOST_FREQ_CAP)
123
124 #define PCI_HT_SLAVE0_OFFS LINK_OFFS(           \
125                 PCI_HT_CAP_SLAVE_WIDTH0,        \
126                 PCI_HT_CAP_SLAVE_FREQ0,         \
127                 PCI_HT_CAP_SLAVE_FREQ_CAP0)
128
129 #define PCI_HT_SLAVE1_OFFS LINK_OFFS(           \
130                 PCI_HT_CAP_SLAVE_WIDTH1,        \
131                 PCI_HT_CAP_SLAVE_FREQ1,         \
132                 PCI_HT_CAP_SLAVE_FREQ_CAP1)
133
134 static int ht_optimize_link(
135         device_t dev1, uint8_t pos1, unsigned offs1,
136         device_t dev2, uint8_t pos2, unsigned offs2)
137 {
138         static const uint8_t link_width_to_pow2[]= { 3, 4, 0, 5, 1, 2, 0, 0 };
139         static const uint8_t pow2_to_link_width[] = { 0x7, 4, 5, 0, 1, 3 };
140         uint16_t freq_cap1, freq_cap2, freq_cap, freq_mask;
141         uint8_t width_cap1, width_cap2, width_cap, width, old_width, ln_width1, ln_width2;
142         uint8_t freq, old_freq;
143         int needs_reset;
144         /* Set link width and frequency */
145
146         /* Initially assume everything is already optimized and I don't need a reset */
147         needs_reset = 0;
148
149         /* Get the frequency capabilities */
150         freq_cap1 = ht_read_freq_cap(dev1, pos1 + LINK_FREQ_CAP(offs1));
151         freq_cap2 = ht_read_freq_cap(dev2, pos2 + LINK_FREQ_CAP(offs2));
152
153         /* Calculate the highest possible frequency */
154         freq = log2(freq_cap1 & freq_cap2);
155
156         /* See if I am changing the link freqency */
157         old_freq = pci_read_config8(dev1, pos1 + LINK_FREQ(offs1));
158         needs_reset |= old_freq != freq;
159         old_freq = pci_read_config8(dev2, pos2 + LINK_FREQ(offs2));
160         needs_reset |= old_freq != freq;
161
162         /* Set the Calulcated link frequency */
163         pci_write_config8(dev1, pos1 + LINK_FREQ(offs1), freq);
164         pci_write_config8(dev2, pos2 + LINK_FREQ(offs2), freq);
165
166         /* Get the width capabilities */
167         width_cap1 = pci_read_config8(dev1, pos1 + LINK_WIDTH(offs1));
168         width_cap2 = pci_read_config8(dev2, pos2 + LINK_WIDTH(offs2));
169
170         /* Calculate dev1's input width */
171         ln_width1 = link_width_to_pow2[width_cap1 & 7];
172         ln_width2 = link_width_to_pow2[(width_cap2 >> 4) & 7];
173         if (ln_width1 > ln_width2) {
174                 ln_width1 = ln_width2;
175         }
176         width = pow2_to_link_width[ln_width1];
177         /* Calculate dev1's output width */
178         ln_width1 = link_width_to_pow2[(width_cap1 >> 4) & 7];
179         ln_width2 = link_width_to_pow2[width_cap2 & 7];
180         if (ln_width1 > ln_width2) {
181                 ln_width1 = ln_width2;
182         }
183         width |= pow2_to_link_width[ln_width1] << 4;
184
185         /* See if I am changing dev1's width */
186         old_width = pci_read_config8(dev1, pos1 + LINK_WIDTH(offs1) + 1);
187         needs_reset |= old_width != width;
188
189         /* Set dev1's widths */
190         pci_write_config8(dev1, pos1 + LINK_WIDTH(offs1) + 1, width);
191
192         /* Calculate dev2's width */
193         width = ((width & 0x70) >> 4) | ((width & 0x7) << 4);
194
195         /* See if I am changing dev2's width */
196         old_width = pci_read_config8(dev2, pos2 + LINK_WIDTH(offs2) + 1);
197         needs_reset |= old_width != width;
198
199         /* Set dev2's widths */
200         pci_write_config8(dev2, pos2 + LINK_WIDTH(offs2) + 1, width);
201
202         return needs_reset;
203 }
204 static int ht_setup_chain(device_t udev, uint8_t upos)
205 {
206         /* Assumption the HT chain that is bus 0 has the HT I/O Hub on it.
207          * On most boards this just happens.  If a cpu has multiple
208          * non Coherent links the appropriate bus registers for the
209          * links needs to be programed to point at bus 0.
210          */
211         unsigned next_unitid, last_unitid;
212         int reset_needed;
213         unsigned uoffs;
214
215         /* Make certain the HT bus is not enumerated */
216         ht_collapse_previous_enumeration(0);
217
218         reset_needed = 0;
219         uoffs = PCI_HT_HOST_OFFS;
220         next_unitid = 1;
221         do {
222                 uint32_t id;
223                 uint8_t pos;
224                 uint16_t flags, count;
225                 unsigned offs;
226
227                 device_t dev = PCI_DEV(0, 0, 0);
228                 last_unitid = next_unitid;
229
230                 id = pci_read_config32(dev, PCI_VENDOR_ID);
231                 /* If the chain is enumerated quit */
232                 if (((id & 0xffff) == 0x0000) || ((id & 0xffff) == 0xffff) ||
233                     (((id >> 16) & 0xffff) == 0xffff) ||
234                     (((id >> 16) & 0xffff) == 0x0000)) {
235                         break;
236                 }
237
238                 pos = ht_lookup_slave_capability(dev);
239                 if (!pos) {
240                         print_err("HT link capability not found\r\n");
241                         break;
242                 }
243
244                 /* get ht direction */
245                 offs = (pci_read_config16(dev, pos + PCI_CAP_FLAGS) & (1<<10)) ? PCI_HT_SLAVE1_OFFS : PCI_HT_SLAVE0_OFFS;
246
247                 /* Setup the Hypertransport link */
248                 reset_needed |= ht_optimize_link(udev, upos, uoffs, dev, pos, offs);
249
250                 /* Update the Unitid of the current device */
251                 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
252                 flags &= ~0x1f; /* mask out the bse Unit ID */
253                 flags |= next_unitid & 0x1f;
254                 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
255
256                 /* Remeber the location of the last device */
257                 udev = PCI_DEV(0, next_unitid, 0);
258                 upos = pos;
259                 uoffs = (offs != PCI_HT_SLAVE0_OFFS) ? PCI_HT_SLAVE0_OFFS : PCI_HT_SLAVE1_OFFS;
260
261                 /* Compute the number of unitids consumed */
262                 count = (flags >> 5) & 0x1f;
263                 next_unitid += count;
264
265         } while((last_unitid != next_unitid) && (next_unitid <= 0x1f));
266         return reset_needed;
267 }
268
269 static int ht_setup_chainx(device_t udev, uint8_t upos, uint8_t bus)
270 {
271         unsigned next_unitid, last_unitid;
272         unsigned uoffs;
273         int reset_needed=0;
274
275         uoffs = PCI_HT_HOST_OFFS;
276         next_unitid = 1;
277
278         do {
279                 uint32_t id;
280                 uint8_t pos;
281                 uint16_t flags, count;
282                 unsigned offs;
283                 
284                 device_t dev = PCI_DEV(bus, 0, 0);
285                 last_unitid = next_unitid;
286
287                 id = pci_read_config32(dev, PCI_VENDOR_ID);
288                 /* If the chain is enumerated quit */
289                 if (((id & 0xffff) == 0x0000) || ((id & 0xffff) == 0xffff) ||
290                     (((id >> 16) & 0xffff) == 0xffff) ||
291                     (((id >> 16) & 0xffff) == 0x0000)) {
292                         break;
293                 }
294
295                 pos = ht_lookup_slave_capability(dev);
296                 if (!pos) {
297                         print_err("HT link capability not found\r\n");
298                         break;
299                 }
300
301                 /* get ht direction */
302                 offs = (pci_read_config16(dev, pos + PCI_CAP_FLAGS) & (1<<10)) ? PCI_HT_SLAVE1_OFFS : PCI_HT_SLAVE0_OFFS;
303
304                 /* Setup the Hypertransport link */
305                 reset_needed |= ht_optimize_link(udev, upos, uoffs, dev, pos, offs);
306
307                 /* Update the Unitid of the current device */
308                 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
309                 flags &= ~0x1f; /* mask out the bse Unit ID */
310                 flags |= next_unitid & 0x1f;
311                 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
312
313                 /* Remeber the location of the last device */
314                 udev = PCI_DEV(bus, next_unitid, 0);
315                 upos = pos;
316                 uoffs = ( offs != PCI_HT_SLAVE0_OFFS ) ? PCI_HT_SLAVE0_OFFS : PCI_HT_SLAVE1_OFFS;
317
318                 /* Compute the number of unitids consumed */
319                 count = (flags >> 5) & 0x1f;
320                 next_unitid += count;
321
322         } while((last_unitid != next_unitid) && (next_unitid <= 0x1f));
323         return reset_needed;
324 }
325
326 static int optimize_link_read_pointer(uint8_t node, uint8_t linkn, uint8_t linkt, uint8_t val)
327 {
328         uint32_t dword, dword_old;
329         uint8_t link_type;
330         
331         /* This works on an Athlon64 because unimplemented links return 0 */
332         dword = pci_read_config32(PCI_DEV(0,0x18+node,0), 0x98 + (linkn * 0x20));
333         link_type = dword & 0xff;
334         
335         dword_old = dword = pci_read_config32(PCI_DEV(0,0x18+node,3), 0xdc);
336         
337         if ( (link_type & 7) == linkt ) { /* Coherent Link only linkt = 3, ncoherent = 7*/
338                 dword &= ~( 0xff<<(linkn *8) );
339                 dword |= val << (linkn *8);
340         }
341         
342         if (dword != dword_old) {
343                 pci_write_config32(PCI_DEV(0,0x18+node,3), 0xdc, dword);
344                 return 1;
345         }
346         
347         return 0;
348 }
349
350 static int optimize_link_in_coherent(uint8_t ht_c_num)
351 {
352         int reset_needed; 
353         uint8_t i;
354
355         reset_needed = 0;
356
357         for (i = 0; i < ht_c_num; i++) {
358                 uint32_t reg;
359                 uint8_t nodeid, linkn;
360                 uint8_t busn;
361                 uint8_t val;
362
363                 reg = pci_read_config32(PCI_DEV(0,0x18,1), 0xe0 + i * 4);
364                 
365                 nodeid = ((reg & 0xf0)>>4); // nodeid
366                 linkn = ((reg & 0xf00)>>8); // link n
367                 busn = (reg & 0xff0000)>>16; //busn
368         
369                 reg = pci_read_config32( PCI_DEV(busn, 1, 0), PCI_VENDOR_ID);
370                 if ( (reg & 0xffff) == PCI_VENDOR_ID_AMD) {
371                         val = 0x25;
372                 } else if ( (reg & 0xffff) == 0x10de ) {
373                         val = 0x25;//???
374                 } else {
375                         continue;
376                 }
377
378                 reset_needed |= optimize_link_read_pointer(nodeid, linkn, 0x07, val);
379
380         }
381
382         return reset_needed;
383 }
384
385 static int ht_setup_chains(uint8_t ht_c_num)
386 {
387         /* Assumption the HT chain that is bus 0 has the HT I/O Hub on it. 
388          * On most boards this just happens.  If a cpu has multiple
389          * non Coherent links the appropriate bus registers for the
390          * links needs to be programed to point at bus 0.
391          */
392         int reset_needed; 
393         uint8_t upos;
394         device_t udev;
395         uint8_t i;
396
397         reset_needed = 0;
398
399         for (i = 0; i < ht_c_num; i++) {
400                 uint32_t reg;
401                 uint8_t devpos;
402                 unsigned regpos;
403                 uint32_t dword;
404                 uint8_t busn;
405                 
406                 reg = pci_read_config32(PCI_DEV(0,0x18,1), 0xe0 + i * 4);
407
408                 //We need setup 0x94, 0xb4, and 0xd4 according to the reg
409                 devpos = ((reg & 0xf0)>>4)+0x18; // nodeid; it will decide 0x18 or 0x19
410                 regpos = ((reg & 0xf00)>>8) * 0x20 + 0x94; // link n; it will decide 0x94 or 0xb4, 0x0xd4;
411                 busn = (reg & 0xff0000)>>16;
412                 
413                 dword = pci_read_config32( PCI_DEV(0, devpos, 0), regpos) ;
414                 dword &= ~(0xffff<<8);
415                 dword |= (reg & 0xffff0000)>>8;
416                 pci_write_config32( PCI_DEV(0, devpos,0), regpos , dword);
417                 
418                 /* Make certain the HT bus is not enumerated */
419                 ht_collapse_previous_enumeration(busn);
420
421                 upos = ((reg & 0xf00)>>8) * 0x20 + 0x80;
422                 udev =  PCI_DEV(0, devpos, 0);
423                 
424                 reset_needed |= ht_setup_chainx(udev,upos,busn);
425
426
427         }
428
429         reset_needed |= optimize_link_in_coherent(ht_c_num);            
430         
431         return reset_needed;
432 }
433
434 static int ht_setup_chains_x(void)
435 {               
436         uint8_t nodeid;
437         uint32_t reg; 
438         uint32_t tempreg;
439         uint8_t next_busn;
440         uint8_t ht_c_num;
441         uint8_t nodes;
442       
443         /* read PCI_DEV(0,0x18,0) 0x64 bit [8:9] to find out SbLink m */
444         reg = pci_read_config32(PCI_DEV(0, 0x18, 0), 0x64);
445         /* update PCI_DEV(0, 0x18, 1) 0xe0 to 0x05000m03, and next_busn=5+1 */
446         print_linkn_in("SBLink=", ((reg>>8) & 3) );
447         tempreg = 3 | ( 0<<4) | (((reg>>8) & 3)<<8) | (0<<16)| (5<<24);
448         pci_write_config32(PCI_DEV(0, 0x18, 1), 0xe0, tempreg);
449
450         next_busn=5+1; /* 0 will be used ht chain with SB we need to keep SB in bus0 in auto stage*/
451         /* clean others */
452         for(ht_c_num=1;ht_c_num<4; ht_c_num++) {
453                 pci_write_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4, 0);
454         }
455  
456         nodes = ((pci_read_config32(PCI_DEV(0, 0x18, 0), 0x60)>>4) & 7) + 1;
457
458         for(nodeid=0; nodeid<nodes; nodeid++) {
459                 device_t dev; 
460                 uint8_t linkn;
461                 dev = PCI_DEV(0, 0x18+nodeid,0);
462                 for(linkn = 0; linkn<3; linkn++) {
463                         unsigned regpos;
464                         regpos = 0x98 + 0x20 * linkn;
465                         reg = pci_read_config32(dev, regpos);
466                         if ((reg & 0x17) != 7) continue; /* it is not non conherent or not connected*/
467                         print_linkn_in("NC node|link=", ((nodeid & 0xf)<<4)|(linkn & 0xf));
468                         tempreg = 3 | (nodeid <<4) | (linkn<<8);
469                         /*compare (temp & 0xffff), with (PCI(0, 0x18, 1) 0xe0 to 0xec & 0xfffff) */
470                         for(ht_c_num=0;ht_c_num<4; ht_c_num++) {
471                                 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4);
472                                 if(((reg & 0xffff) == (tempreg & 0xffff)) || ((reg & 0xffff) == 0x0000)) {  /*we got it*/
473                                         break;
474                                 }
475                         }
476                         if(ht_c_num == 4) break; /*used up onle 4 non conherent allowed*/
477                         /*update to 0xe0...*/
478                         if((reg & 0xf) == 3) continue; /*SbLink so don't touch it */
479                         print_linkn_in("\tbusn=", next_busn);
480                         tempreg |= (next_busn<<16)|((next_busn+5)<<24);
481                         pci_write_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4, tempreg);
482                         next_busn+=5+1;
483                 }
484         }
485         /*update 0xe0, 0xe4, 0xe8, 0xec from PCI_DEV(0, 0x18,1) to PCI_DEV(0, 0x19,1) to PCI_DEV(0, 0x1f,1);*/
486
487         for(nodeid = 1; nodeid<nodes; nodeid++) {
488                 int i;
489                 device_t dev;
490                 dev = PCI_DEV(0, 0x18+nodeid,1);
491                 for(i = 0; i< 4; i++) {
492                         unsigned regpos;
493                         regpos = 0xe0 + i * 4;
494                         reg = pci_read_config32(PCI_DEV(0, 0x18, 1), regpos);
495                         pci_write_config32(dev, regpos, reg);
496
497                 }
498         }
499         
500         /* recount ht_c_num*/
501         uint8_t i=0;
502         for(ht_c_num=0;ht_c_num<4; ht_c_num++) {
503                 reg = pci_read_config32(PCI_DEV(0, 0x18, 1), 0xe0 + ht_c_num * 4);
504                 if(((reg & 0xf) != 0x0)) {
505                         i++;
506                 }
507         }
508
509         return ht_setup_chains(i);
510
511 }