m5a99x-evo: ugly quirks, but WOOT: ohai seabios :-)
[coreboot.git] / src / devices / hypertransport.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2003-2004 Linux Networx
5  * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
6  * Copyright (C) 2004 David Hendricks <sc@flagen.com>
7  * Copyright (C) 2004 Li-Ta Lo <ollie@lanl.gov>
8  * Copyright (C) 2005-2006 Tyan
9  * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
10  * Copyright (C) 2005-2006 Stefan Reinauer <stepan@openbios.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; version 2 of the License.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
24  */
25
26 #include <bitops.h>
27 #include <console/console.h>
28 #include <device/device.h>
29 #include <device/path.h>
30 #include <device/pci.h>
31 #include <device/pci_ids.h>
32 #include <device/hypertransport.h>
33
34 /*
35  * The hypertransport link is already optimized in pre-RAM code so don't do
36  * it again.
37  */
38 #define OPT_HT_LINK 0
39
40 #if OPT_HT_LINK == 1
41 #include <cpu/amd/model_fxx_rev.h>
42 #endif
43
44 static device_t ht_scan_get_devs(device_t *old_devices)
45 {
46         device_t first, last;
47
48         first = *old_devices;
49         last = first;
50
51         /*
52          * Extract the chain of devices to (first through last) for the next
53          * hypertransport device.
54          */
55         while (last && last->sibling &&
56                (last->sibling->path.type == DEVICE_PATH_PCI) &&
57                (last->sibling->path.pci.devfn > last->path.pci.devfn))
58         {
59                 last = last->sibling;
60         }
61
62         if (first) {
63                 device_t child;
64
65                 /* Unlink the chain from the list of old devices. */
66                 *old_devices = last->sibling;
67                 last->sibling = 0;
68
69                 /* Now add the device to the list of devices on the bus. */
70                 /* Find the last child of our parent. */
71                 for (child = first->bus->children; child && child->sibling; )
72                         child = child->sibling;
73
74                 /* Place the chain on the list of children of their parent. */
75                 if (child)
76                         child->sibling = first;
77                 else
78                         first->bus->children = first;
79         }
80         return first;
81 }
82
83 #if OPT_HT_LINK == 1
84 static unsigned ht_read_freq_cap(device_t dev, unsigned pos)
85 {
86         /* Handle bugs in valid hypertransport frequency reporting. */
87         unsigned freq_cap;
88
89         freq_cap = pci_read_config16(dev, pos);
90         freq_cap &= ~(1 << HT_FREQ_VENDOR); /* Ignore Vendor HT frequencies. */
91
92         /* AMD 8131 Errata 48. */
93         if ((dev->vendor == PCI_VENDOR_ID_AMD) &&
94             (dev->device == PCI_DEVICE_ID_AMD_8131_PCIX)) {
95                 freq_cap &= ~(1 << HT_FREQ_800Mhz);
96         }
97
98         /* AMD 8151 Errata 23. */
99         if ((dev->vendor == PCI_VENDOR_ID_AMD) &&
100             (dev->device == PCI_DEVICE_ID_AMD_8151_SYSCTRL)) {
101                 freq_cap &= ~(1 << HT_FREQ_800Mhz);
102         }
103
104         /* AMD K8 unsupported 1GHz? */
105         if ((dev->vendor == PCI_VENDOR_ID_AMD) && (dev->device == 0x1100)) {
106 #if CONFIG_K8_HT_FREQ_1G_SUPPORT == 1
107
108 #if CONFIG_K8_REV_F_SUPPORT == 0
109                 /* Only e0 later suupport 1GHz HT. */
110                 if (is_cpu_pre_e0())
111                         freq_cap &= ~(1 << HT_FREQ_1000Mhz);
112 #endif
113
114 #else
115                 freq_cap &= ~(1 << HT_FREQ_1000Mhz);
116 #endif
117         }
118
119         return freq_cap;
120 }
121 #endif
122
123 struct ht_link {
124         struct device *dev;
125         unsigned pos;
126         unsigned char ctrl_off, config_off, freq_off, freq_cap_off;
127 };
128
129 static int ht_setup_link(struct ht_link *prev, device_t dev, unsigned pos)
130 {
131 #if OPT_HT_LINK == 1
132         static const u8 link_width_to_pow2[] = { 3, 4, 0, 5, 1, 2, 0, 0 };
133         static const u8 pow2_to_link_width[] = { 7, 4, 5, 0, 1, 3 };
134         unsigned present_width_cap, upstream_width_cap;
135         unsigned present_freq_cap, upstream_freq_cap;
136         unsigned ln_present_width_in, ln_upstream_width_in;
137         unsigned ln_present_width_out, ln_upstream_width_out;
138         unsigned freq, old_freq;
139         unsigned present_width, upstream_width, old_width;
140 #endif
141         struct ht_link cur[1];
142         int reset_needed;
143         int linkb_to_host;
144
145         /* Set the hypertransport link width and frequency. */
146         reset_needed = 0;
147         /*
148          * See which side of the device our previous write to set the unitid
149          * came from.
150          */
151         cur->dev = dev;
152         cur->pos = pos;
153         linkb_to_host =
154           (pci_read_config16(cur->dev, cur->pos + PCI_CAP_FLAGS) >> 10) & 1;
155
156         if (!linkb_to_host) {
157                 cur->ctrl_off     = PCI_HT_CAP_SLAVE_CTRL0;
158                 cur->config_off   = PCI_HT_CAP_SLAVE_WIDTH0;
159                 cur->freq_off     = PCI_HT_CAP_SLAVE_FREQ0;
160                 cur->freq_cap_off = PCI_HT_CAP_SLAVE_FREQ_CAP0;
161         } else {
162                 cur->ctrl_off     = PCI_HT_CAP_SLAVE_CTRL1;
163                 cur->config_off   = PCI_HT_CAP_SLAVE_WIDTH1;
164                 cur->freq_off     = PCI_HT_CAP_SLAVE_FREQ1;
165                 cur->freq_cap_off = PCI_HT_CAP_SLAVE_FREQ_CAP1;
166         }
167
168 #if OPT_HT_LINK == 1
169         /* Read the capabilities. */
170         present_freq_cap =
171                 ht_read_freq_cap(cur->dev, cur->pos + cur->freq_cap_off);
172         upstream_freq_cap =
173                 ht_read_freq_cap(prev->dev, prev->pos + prev->freq_cap_off);
174         present_width_cap =
175                 pci_read_config8(cur->dev, cur->pos + cur->config_off);
176         upstream_width_cap =
177                 pci_read_config8(prev->dev, prev->pos + prev->config_off);
178
179         /* Calculate the highest useable frequency. */
180         freq = log2(present_freq_cap & upstream_freq_cap);
181
182         /* Calculate the highest width. */
183         ln_upstream_width_in = link_width_to_pow2[upstream_width_cap & 7];
184         ln_present_width_out = link_width_to_pow2[(present_width_cap >> 4) & 7];
185         if (ln_upstream_width_in > ln_present_width_out)
186                 ln_upstream_width_in = ln_present_width_out;
187         upstream_width = pow2_to_link_width[ln_upstream_width_in];
188         present_width  = pow2_to_link_width[ln_upstream_width_in] << 4;
189
190         ln_upstream_width_out =
191                 link_width_to_pow2[(upstream_width_cap >> 4) & 7];
192         ln_present_width_in = link_width_to_pow2[present_width_cap & 7];
193         if (ln_upstream_width_out > ln_present_width_in)
194                 ln_upstream_width_out = ln_present_width_in;
195         upstream_width |= pow2_to_link_width[ln_upstream_width_out] << 4;
196         present_width  |= pow2_to_link_width[ln_upstream_width_out];
197
198         /* Set the current device. */
199         old_freq = pci_read_config8(cur->dev, cur->pos + cur->freq_off);
200         old_freq &= 0x0f;
201         if (freq != old_freq) {
202                 unsigned new_freq;
203                 pci_write_config8(cur->dev, cur->pos + cur->freq_off, freq);
204                 reset_needed = 1;
205                 printk(BIOS_SPEW, "HyperT FreqP old %x new %x\n",old_freq,freq);
206                 new_freq = pci_read_config8(cur->dev, cur->pos + cur->freq_off);
207                 new_freq &= 0x0f;
208                 if (new_freq != freq) {
209                         printk(BIOS_ERR, "%s Hypertransport frequency would "
210                                "not set. Wanted: %x, got: %x\n",
211                                dev_path(dev), freq, new_freq);
212                 }
213         }
214         old_width = pci_read_config8(cur->dev, cur->pos + cur->config_off + 1);
215         if (present_width != old_width) {
216                 unsigned new_width;
217                 pci_write_config8(cur->dev, cur->pos + cur->config_off + 1,
218                                   present_width);
219                 reset_needed = 1;
220                 printk(BIOS_SPEW, "HyperT widthP old %x new %x\n",
221                        old_width, present_width);
222                 new_width = pci_read_config8(cur->dev,
223                                              cur->pos + cur->config_off + 1);
224                 if (new_width != present_width) {
225                         printk(BIOS_ERR, "%s Hypertransport width would not "
226                                "set. Wanted: %x, got: %x\n",
227                                dev_path(dev), present_width, new_width);
228                 }
229         }
230
231         /* Set the upstream device. */
232         old_freq = pci_read_config8(prev->dev, prev->pos + prev->freq_off);
233         old_freq &= 0x0f;
234         if (freq != old_freq) {
235                 unsigned new_freq;
236                 pci_write_config8(prev->dev, prev->pos + prev->freq_off, freq);
237                 reset_needed = 1;
238                 printk(BIOS_SPEW, "HyperT freqU old %x new %x\n",
239                        old_freq, freq);
240                 new_freq =
241                   pci_read_config8(prev->dev, prev->pos + prev->freq_off);
242                 new_freq &= 0x0f;
243                 if (new_freq != freq) {
244                         printk(BIOS_ERR, "%s Hypertransport frequency would "
245                                "not set. Wanted: %x, got: %x\n",
246                                dev_path(prev->dev), freq, new_freq);
247                 }
248         }
249         old_width =
250                 pci_read_config8(prev->dev, prev->pos + prev->config_off + 1);
251         if (upstream_width != old_width) {
252                 unsigned new_width;
253                 pci_write_config8(prev->dev, prev->pos + prev->config_off + 1,
254                                   upstream_width);
255                 reset_needed = 1;
256                 printk(BIOS_SPEW, "HyperT widthU old %x new %x\n", old_width,
257                        upstream_width);
258                 new_width = pci_read_config8(prev->dev,
259                                              prev->pos + prev->config_off + 1);
260                 if (new_width != upstream_width) {
261                         printk(BIOS_ERR, "%s Hypertransport width would not "
262                                "set. Wanted: %x, got: %x\n",
263                                dev_path(prev->dev), upstream_width, new_width);
264                 }
265         }
266 #endif
267
268         /*
269          * Remember the current link as the previous link, but look at the
270          * other offsets.
271          */
272         prev->dev = cur->dev;
273         prev->pos = cur->pos;
274         if (cur->ctrl_off == PCI_HT_CAP_SLAVE_CTRL0) {
275                 prev->ctrl_off     = PCI_HT_CAP_SLAVE_CTRL1;
276                 prev->config_off   = PCI_HT_CAP_SLAVE_WIDTH1;
277                 prev->freq_off     = PCI_HT_CAP_SLAVE_FREQ1;
278                 prev->freq_cap_off = PCI_HT_CAP_SLAVE_FREQ_CAP1;
279         } else {
280                 prev->ctrl_off     = PCI_HT_CAP_SLAVE_CTRL0;
281                 prev->config_off   = PCI_HT_CAP_SLAVE_WIDTH0;
282                 prev->freq_off     = PCI_HT_CAP_SLAVE_FREQ0;
283                 prev->freq_cap_off = PCI_HT_CAP_SLAVE_FREQ_CAP0;
284         }
285
286         return reset_needed;
287 }
288
289 static unsigned ht_lookup_slave_capability(struct device *dev)
290 {
291         unsigned pos;
292
293         pos = 0;
294         do {
295                 pos = pci_find_next_capability(dev, PCI_CAP_ID_HT, pos);
296                 if (pos) {
297                         u16 flags;
298                         flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
299                         printk(BIOS_SPEW, "flags: 0x%04x\n", flags);
300                         if ((flags >> 13) == 0) {
301                                 /* Entry is a slave secondary, success... */
302                                 break;
303                         }
304                 }
305         } while (pos);
306
307         return pos;
308 }
309
310 static void ht_collapse_early_enumeration(struct bus *bus,
311                                           unsigned offset_unitid)
312 {
313         unsigned int devfn;
314         struct ht_link prev;
315         u16 ctrl;
316
317         /* Initialize the hypertransport enumeration state. */
318         prev.dev = bus->dev;
319         prev.pos = bus->cap;
320         prev.ctrl_off     = PCI_HT_CAP_HOST_CTRL;
321         prev.config_off   = PCI_HT_CAP_HOST_WIDTH;
322         prev.freq_off     = PCI_HT_CAP_HOST_FREQ;
323         prev.freq_cap_off = PCI_HT_CAP_HOST_FREQ_CAP;
324
325         /* Wait until the link initialization is complete. */
326         do {
327                 ctrl = pci_read_config16(prev.dev, prev.pos + prev.ctrl_off);
328
329                 /* Is this the end of the hypertransport chain? */
330                 if (ctrl & (1 << 6))
331                         return;
332
333                 /* Has the link failed? */
334                 if (ctrl & (1 << 4)) {
335                         /*
336                          * Either the link has failed, or we have a CRC error.
337                          * Sometimes this can happen due to link retrain, so
338                          * lets knock it down and see if its transient.
339                          */
340                         ctrl |= ((1 << 4) | (1 << 8)); /* Link fail + CRC */
341                         pci_write_config16(prev.dev, prev.pos + prev.ctrl_off,
342                                            ctrl);
343                         ctrl = pci_read_config16(prev.dev,
344                                                  prev.pos + prev.ctrl_off);
345                         if (ctrl & ((1 << 4) | (1 << 8))) {
346                                 printk(BIOS_ALERT, "Detected error on "
347                                        "Hypertransport link\n");
348                                 return;
349                         }
350                 }
351         } while ((ctrl & (1 << 5)) == 0);
352
353         /* Actually, only for one HT device HT chain, and unitid is 0. */
354 #if CONFIG_HT_CHAIN_UNITID_BASE == 0
355         if (offset_unitid)
356                 return;
357 #endif
358
359         /* Check if is already collapsed. */
360         if ((!offset_unitid) || (offset_unitid
361             && (!((CONFIG_HT_CHAIN_END_UNITID_BASE == 0)
362             && (CONFIG_HT_CHAIN_END_UNITID_BASE
363             < CONFIG_HT_CHAIN_UNITID_BASE))))) {
364
365                 struct device dummy;
366                 u32 id;
367
368                 dummy.bus = bus;
369                 dummy.path.type = DEVICE_PATH_PCI;
370                 dummy.path.pci.devfn = PCI_DEVFN(0, 0);
371
372                 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
373                 if (!((id == 0xffffffff) || (id == 0x00000000)
374                     || (id == 0x0000ffff) || (id == 0xffff0000))) {
375                         return;
376                 }
377         }
378
379         /* Spin through the devices and collapse any early HT enumeration. */
380         for (devfn = PCI_DEVFN(1, 0); devfn <= 0xff; devfn += 8) {
381                 struct device dummy;
382                 u32 id;
383                 unsigned pos, flags;
384
385                 dummy.bus = bus;
386                 dummy.path.type = DEVICE_PATH_PCI;
387                 dummy.path.pci.devfn = devfn;
388
389                 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
390                 if ((id == 0xffffffff) || (id == 0x00000000)
391                     || (id == 0x0000ffff) || (id == 0xffff0000)) {
392                         continue;
393                 }
394
395                 dummy.vendor = id & 0xffff;
396                 dummy.device = (id >> 16) & 0xffff;
397                 dummy.hdr_type = pci_read_config8(&dummy, PCI_HEADER_TYPE);
398
399                 pos = ht_lookup_slave_capability(&dummy);
400                 if (!pos)
401                         continue;
402
403                 /* Clear the unitid. */
404                 flags = pci_read_config16(&dummy, pos + PCI_CAP_FLAGS);
405                 flags &= ~0x1f;
406                 pci_write_config16(&dummy, pos + PCI_CAP_FLAGS, flags);
407                 printk(BIOS_SPEW, "Collapsing %s [%04x/%04x]\n",
408                        dev_path(&dummy), dummy.vendor, dummy.device);
409         }
410 }
411
412 unsigned int hypertransport_scan_chain(struct bus *bus, unsigned min_devfn,
413                                        unsigned max_devfn, unsigned int max,
414                                        unsigned *ht_unitid_base,
415                                        unsigned offset_unitid)
416 {
417         /*
418          * Even CONFIG_HT_CHAIN_UNITID_BASE == 0, we still can go through this
419          * function, because of end_of_chain check. Also, we need it to
420          * optimize link.
421          */
422         unsigned int next_unitid, last_unitid, min_unitid, max_unitid;
423         device_t old_devices, dev, func, last_func = 0;
424         struct ht_link prev;
425         int ht_dev_num = 0;
426
427         min_unitid = (offset_unitid) ? CONFIG_HT_CHAIN_UNITID_BASE : 1;
428
429 #if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
430         /*
431          * Let's record the device of last HT device, so we can set the unitid
432          * to CONFIG_HT_CHAIN_END_UNITID_BASE.
433          */
434         unsigned int real_last_unitid = 0, end_used = 0;
435         u8 real_last_pos = 0;
436         device_t real_last_dev = NULL;
437 #endif
438
439         printk(BIOS_INFO, "%s: before ht_collapse_early_enumeration\n", __func__);
440         /* Restore the hypertransport chain to it's unitialized state. */
441         ht_collapse_early_enumeration(bus, offset_unitid);
442         printk(BIOS_INFO, "%s: after  ht_collapse_early_enumeration\n", __func__);
443
444         /* See which static device nodes I have. */
445         old_devices = bus->children;
446         bus->children = 0;
447
448         /* Initialize the hypertransport enumeration state. */
449         prev.dev = bus->dev;
450         prev.pos = bus->cap;
451
452         prev.ctrl_off     = PCI_HT_CAP_HOST_CTRL;
453         prev.config_off   = PCI_HT_CAP_HOST_WIDTH;
454         prev.freq_off     = PCI_HT_CAP_HOST_FREQ;
455         prev.freq_cap_off = PCI_HT_CAP_HOST_FREQ_CAP;
456
457         /* If present, assign unitid to a hypertransport chain. */
458         last_unitid = min_unitid -1;
459         max_unitid = next_unitid = min_unitid;
460         do {
461                 u8 pos;
462                 u16 flags, ctrl;
463                 unsigned int count, static_count;
464
465                 last_unitid = next_unitid;
466
467                 /* Wait until the link initialization is complete. */
468                 do {
469                         ctrl = pci_read_config16(prev.dev,
470                                                  prev.pos + prev.ctrl_off);
471
472                         /* End of chain? */
473                         if (ctrl & (1 << 6))
474                                 goto end_of_chain;
475
476                         if (ctrl & ((1 << 4) | (1 << 8))) {
477                                 /*
478                                  * Either the link has failed, or we have a CRC
479                                  * error. Sometimes this can happen due to link
480                                  * retrain, so lets knock it down and see if
481                                  * it's transient.
482                                  */
483                                 ctrl |= ((1 << 4) | (1 <<8)); // Link fail + CRC
484                                 pci_write_config16(prev.dev,
485                                         prev.pos + prev.ctrl_off, ctrl);
486                                 ctrl = pci_read_config16(prev.dev,
487                                                 prev.pos + prev.ctrl_off);
488                                 if (ctrl & ((1 << 4) | (1 << 8))) {
489                                         printk(BIOS_ALERT, "Detected error on "
490                                                "hypertransport link\n");
491                                         goto end_of_chain;
492                                 }
493                         }
494                 } while ((ctrl & (1 << 5)) == 0);
495
496
497                 printk(BIOS_INFO, "%s: before ht_scan_get_devs\n", __func__);
498                 /* Get and setup the device_structure. */
499                 dev = ht_scan_get_devs(&old_devices);
500                 printk(BIOS_INFO, "%s: after  ht_scan_get_devs\n", __func__);
501
502                 /* See if a device is present and setup the device structure. */
503                 printk(BIOS_INFO, "%s: before pci_probe_dev\n", __func__);
504                 dev = pci_probe_dev(dev, bus, 0);
505                 printk(BIOS_INFO, "%s: after  pci_probe_dev\n", __func__);
506                 if (!dev || !dev->enabled)
507                         break;
508
509                 /* Find the hypertransport link capability. */
510                 printk(BIOS_INFO, "%s: before ht_lookup_slave_capability\n", __func__);
511                 pos = ht_lookup_slave_capability(dev);
512                 printk(BIOS_INFO, "%s: after  ht_lookup_slave_capability\n", __func__);
513                 if (pos == 0) {
514                         printk(BIOS_ERR, "%s Hypertransport link capability "
515                                "not found", dev_path(dev));
516                         break;
517                 }
518
519                 /* Update the unitid of the current device. */
520                 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
521
522                 /*
523                  * If the devices has a unitid set and is at devfn 0 we are
524                  * done. This can happen with shadow hypertransport devices,
525                  * or if we have reached the bottom of a HT device chain.
526                  */
527                 if (flags & 0x1f)
528                         break;
529
530                 flags &= ~0x1f; /* Mask out base Unit ID. */
531
532                 count = (flags >> 5) & 0x1f; /* Het unit count. */
533
534 #if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
535                 if (offset_unitid) {
536                         /* max_devfn will be (0x17<<3)|7 or (0x1f<<3)|7. */
537                         if (next_unitid > (max_devfn >> 3)) {
538                                 if (!end_used) {
539                                         next_unitid =
540                                           CONFIG_HT_CHAIN_END_UNITID_BASE;
541                                         end_used = 1;
542                                 } else {
543                                         goto end_of_chain;
544                                 }
545                         }
546                 }
547 #endif
548
549                 flags |= next_unitid & 0x1f;
550                 pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags);
551
552                 /* Update the unitid in the device structure. */
553                 static_count = 1;
554                 for (func = dev; func; func = func->sibling) {
555                         func->path.pci.devfn += (next_unitid << 3);
556                         static_count = (func->path.pci.devfn >> 3)
557                                         - (dev->path.pci.devfn >> 3) + 1;
558                         last_func = func;
559                 }
560
561                 /* Compute the number of unitids consumed. */
562                 printk(BIOS_SPEW, "%s count: %04x static_count: %04x\n",
563                        dev_path(dev), count, static_count);
564                 if (count < static_count)
565                         count = static_count;
566
567                 /* Update the unitid of the next device. */
568                 ht_unitid_base[ht_dev_num] = next_unitid;
569                 ht_dev_num++;
570
571 #if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
572                 if (offset_unitid) {
573                         real_last_pos = pos;
574                         real_last_unitid = next_unitid;
575                         real_last_dev = dev;
576                 }
577 #endif
578                 next_unitid += count;
579                 if (next_unitid > max_unitid)
580                         max_unitid = next_unitid;
581
582                 /* Setup the hypetransport link. */
583                 printk(BIOS_INFO, "%s: before ht_setup_link\n", __func__);
584                 bus->reset_needed |= ht_setup_link(&prev, dev, pos);
585                 printk(BIOS_INFO, "%s: after  ht_setup_link\n", __func__);
586
587                 printk(BIOS_DEBUG, "%s [%04x/%04x] %s next_unitid: %04x\n",
588                        dev_path(dev), dev->vendor, dev->device,
589                        (dev->enabled? "enabled" : "disabled"), next_unitid);
590
591         } while (last_unitid != next_unitid);
592
593 end_of_chain:
594
595         printk(BIOS_INFO, "%s: end_of_chain.  w00t!\n", __func__);
596 #if OPT_HT_LINK == 1
597         if (bus->reset_needed)
598                 printk(BIOS_INFO, "HyperT reset needed\n");
599         else
600                 printk(BIOS_DEBUG, "HyperT reset not needed\n");
601 #endif
602
603 #if CONFIG_HT_CHAIN_END_UNITID_BASE != 0x20
604         if (offset_unitid && (ht_dev_num > 1)
605             && (real_last_unitid != CONFIG_HT_CHAIN_END_UNITID_BASE)
606             && !end_used) {
607                 u16 flags;
608                 flags = pci_read_config16(real_last_dev,
609                                           real_last_pos + PCI_CAP_FLAGS);
610                 flags &= ~0x1f;
611                 flags |= CONFIG_HT_CHAIN_END_UNITID_BASE & 0x1f;
612                 pci_write_config16(real_last_dev,
613                                    real_last_pos + PCI_CAP_FLAGS, flags);
614
615                 for (func = real_last_dev; func; func = func->sibling) {
616                         func->path.pci.devfn -= ((real_last_unitid
617                                 - CONFIG_HT_CHAIN_END_UNITID_BASE) << 3);
618                         last_func = func;
619                 }
620
621                 /* Update last one. */
622                 ht_unitid_base[ht_dev_num-1] = CONFIG_HT_CHAIN_END_UNITID_BASE;
623
624                 printk(BIOS_DEBUG, " unitid: %04x --> %04x\n",
625                        real_last_unitid, CONFIG_HT_CHAIN_END_UNITID_BASE);
626         }
627 #endif
628         next_unitid = max_unitid;
629
630         if (next_unitid > 0x20)
631                 next_unitid = 0x20;
632
633         if ((bus->secondary == 0) && (next_unitid > 0x18))
634                 next_unitid = 0x18; /* Avoid K8 on bus 0. */
635
636         /*
637          * Die if any leftover static devices are are found. There's probably
638          * a problem in devicetree.cb.
639          */
640         if (old_devices) {
641                 device_t left;
642                 for (left = old_devices; left; left = left->sibling)
643                         printk(BIOS_DEBUG, "%s\n", dev_path(left));
644
645                 printk(BIOS_ERR, "HT: Leftover static devices. "
646                        "Check your devicetree.cb\n");
647
648                 /*
649                  * Put back the leftover static device, and let pci_scan_bus()
650                  * disable it.
651                  */
652                 if (last_func && !last_func->sibling)
653                         last_func->sibling = old_devices;
654         }
655
656         printk(BIOS_INFO, "%s: before pci_scan_bus!\n", __func__);
657         /* Now that nothing is overlapping it is safe to scan the children. */
658         max = pci_scan_bus(bus, 0x00, ((next_unitid - 1) << 3) | 7, max);
659         printk(BIOS_INFO, "%s: after  pci_scan_bus!\n", __func__);
660         return max;
661 }
662
663 /**
664  * Scan a PCI bridge and the buses behind the bridge.
665  *
666  * Determine the existence of buses behind the bridge. Set up the bridge
667  * according to the result of the scan.
668  *
669  * This function is the default scan_bus() method for PCI bridge devices.
670  *
671  * @param bus TODO
672  * @param min_devfn TODO
673  * @param max_devfn TODO
674  * @param max The highest bus number assgined up to now.
675  * @return The maximum bus number found, after scanning all subordinate busses.
676  */
677 static unsigned int hypertransport_scan_chain_x(struct bus *bus,
678         unsigned int min_devfn, unsigned int max_devfn, unsigned int max)
679 {
680         unsigned int ht_unitid_base[4];
681         unsigned int offset_unitid = 1;
682         return hypertransport_scan_chain(bus, min_devfn, max_devfn, max,
683                                          ht_unitid_base, offset_unitid);
684 }
685
686 unsigned int ht_scan_bridge(struct device *dev, unsigned int max)
687 {
688         return do_pci_scan_bridge(dev, max, hypertransport_scan_chain_x);
689 }
690
691 /** Default device operations for hypertransport bridges */
692 static struct pci_operations ht_bus_ops_pci = {
693         .set_subsystem = 0,
694 };
695
696 struct device_operations default_ht_ops_bus = {
697         .read_resources   = pci_bus_read_resources,
698         .set_resources    = pci_dev_set_resources,
699         .enable_resources = pci_bus_enable_resources,
700         .init             = 0,
701         .scan_bus         = ht_scan_bridge,
702         .enable           = 0,
703         .reset_bus        = pci_bus_reset,
704         .ops_pci          = &ht_bus_ops_pci,
705 };