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