Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame^] | 1 | /* |
| 2 | 2005.11 yhlu add let the real sb to use small uintid |
| 3 | |
| 4 | */ |
| 5 | |
| 6 | |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 7 | #include <bitops.h> |
| 8 | #include <console/console.h> |
| 9 | #include <device/device.h> |
| 10 | #include <device/path.h> |
| 11 | #include <device/pci.h> |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 12 | #include <device/pci_ids.h> |
Eric Biederman | ff0e846 | 2003-09-04 03:00:54 +0000 | [diff] [blame] | 13 | #include <device/hypertransport.h> |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 14 | #include <part/hard_reset.h> |
| 15 | #include <part/fallback_boot.h> |
| 16 | |
Yinghai Lu | 90b3e09 | 2005-01-26 22:00:20 +0000 | [diff] [blame] | 17 | #define OPT_HT_LINK 0 |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 18 | |
arch import user (historical) | ef03afa | 2005-07-06 17:15:30 +0000 | [diff] [blame] | 19 | #if OPT_HT_LINK == 1 |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame^] | 20 | #include <cpu/amd/model_fxx_rev.h> |
arch import user (historical) | ef03afa | 2005-07-06 17:15:30 +0000 | [diff] [blame] | 21 | #endif |
| 22 | |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 23 | static device_t ht_scan_get_devs(device_t *old_devices) |
| 24 | { |
| 25 | device_t first, last; |
| 26 | first = *old_devices; |
| 27 | last = first; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 28 | /* Extract the chain of devices to (first through last) |
| 29 | * for the next hypertransport device. |
| 30 | */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 31 | while(last && last->sibling && |
| 32 | (last->sibling->path.type == DEVICE_PATH_PCI) && |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 33 | (last->sibling->path.u.pci.devfn > last->path.u.pci.devfn)) |
| 34 | { |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 35 | last = last->sibling; |
| 36 | } |
| 37 | if (first) { |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 38 | device_t child; |
| 39 | /* Unlink the chain from the list of old devices */ |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 40 | *old_devices = last->sibling; |
| 41 | last->sibling = 0; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 42 | |
| 43 | /* Now add the device to the list of devices on the bus. |
| 44 | */ |
| 45 | /* Find the last child of our parent */ |
| 46 | for(child = first->bus->children; child && child->sibling; ) { |
| 47 | child = child->sibling; |
| 48 | } |
| 49 | /* Place the chain on the list of children of their parent. */ |
| 50 | if (child) { |
| 51 | child->sibling = first; |
| 52 | } else { |
| 53 | first->bus->children = first; |
| 54 | } |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 55 | } |
| 56 | return first; |
| 57 | } |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 58 | |
Yinghai Lu | 90b3e09 | 2005-01-26 22:00:20 +0000 | [diff] [blame] | 59 | #if OPT_HT_LINK == 1 |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 60 | static unsigned ht_read_freq_cap(device_t dev, unsigned pos) |
| 61 | { |
| 62 | /* Handle bugs in valid hypertransport frequency reporting */ |
| 63 | unsigned freq_cap; |
| 64 | |
| 65 | freq_cap = pci_read_config16(dev, pos); |
| 66 | freq_cap &= ~(1 << HT_FREQ_VENDOR); /* Ignore Vendor HT frequencies */ |
| 67 | |
| 68 | /* AMD 8131 Errata 48 */ |
| 69 | if ((dev->vendor == PCI_VENDOR_ID_AMD) && |
| 70 | (dev->device == PCI_DEVICE_ID_AMD_8131_PCIX)) { |
| 71 | freq_cap &= ~(1 << HT_FREQ_800Mhz); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 72 | } |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 73 | /* AMD 8151 Errata 23 */ |
| 74 | if ((dev->vendor == PCI_VENDOR_ID_AMD) && |
| 75 | (dev->device == PCI_DEVICE_ID_AMD_8151_SYSCTRL)) { |
| 76 | freq_cap &= ~(1 << HT_FREQ_800Mhz); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 77 | } |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 78 | /* AMD K8 Unsupported 1Ghz? */ |
| 79 | if ((dev->vendor == PCI_VENDOR_ID_AMD) && (dev->device == 0x1100)) { |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame^] | 80 | #if K8_HT_FREQ_1G_SUPPORT == 1 |
| 81 | if (is_cpu_pre_e0()) { // only e0 later suupport 1GHz HT |
Yinghai Lu | 90b3e09 | 2005-01-26 22:00:20 +0000 | [diff] [blame] | 82 | freq_cap &= ~(1 << HT_FREQ_1000Mhz); |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame^] | 83 | } |
| 84 | #else |
| 85 | freq_cap &= ~(1 << HT_FREQ_1000Mhz); |
| 86 | #endif |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 87 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 88 | } |
| 89 | return freq_cap; |
| 90 | } |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 91 | #endif |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 92 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 93 | struct ht_link { |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 94 | struct device *dev; |
| 95 | unsigned pos; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 96 | unsigned char ctrl_off, config_off, freq_off, freq_cap_off; |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 99 | static int ht_setup_link(struct ht_link *prev, device_t dev, unsigned pos) |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 100 | { |
| 101 | static const uint8_t link_width_to_pow2[]= { 3, 4, 0, 5, 1, 2, 0, 0 }; |
| 102 | static const uint8_t pow2_to_link_width[] = { 0x7, 4, 5, 0, 1, 3 }; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 103 | struct ht_link cur[1]; |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 104 | unsigned present_width_cap, upstream_width_cap; |
| 105 | unsigned present_freq_cap, upstream_freq_cap; |
| 106 | unsigned ln_present_width_in, ln_upstream_width_in; |
| 107 | unsigned ln_present_width_out, ln_upstream_width_out; |
| 108 | unsigned freq, old_freq; |
| 109 | unsigned present_width, upstream_width, old_width; |
| 110 | int reset_needed; |
Yinghai Lu | 1f1085b | 2005-01-17 21:37:12 +0000 | [diff] [blame] | 111 | int linkb_to_host; |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 112 | |
| 113 | /* Set the hypertransport link width and frequency */ |
| 114 | reset_needed = 0; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 115 | /* See which side of the device our previous write to |
| 116 | * set the unitid came from. |
| 117 | */ |
| 118 | cur->dev = dev; |
| 119 | cur->pos = pos; |
| 120 | linkb_to_host = (pci_read_config16(cur->dev, cur->pos + PCI_CAP_FLAGS) >> 10) & 1; |
| 121 | if (!linkb_to_host) { |
| 122 | cur->ctrl_off = PCI_HT_CAP_SLAVE_CTRL0; |
| 123 | cur->config_off = PCI_HT_CAP_SLAVE_WIDTH0; |
| 124 | cur->freq_off = PCI_HT_CAP_SLAVE_FREQ0; |
| 125 | cur->freq_cap_off = PCI_HT_CAP_SLAVE_FREQ_CAP0; |
| 126 | } |
| 127 | else { |
| 128 | cur->ctrl_off = PCI_HT_CAP_SLAVE_CTRL1; |
| 129 | cur->config_off = PCI_HT_CAP_SLAVE_WIDTH1; |
| 130 | cur->freq_off = PCI_HT_CAP_SLAVE_FREQ1; |
| 131 | cur->freq_cap_off = PCI_HT_CAP_SLAVE_FREQ_CAP1; |
| 132 | } |
| 133 | #if OPT_HT_LINK == 1 |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 134 | /* Read the capabilities */ |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 135 | present_freq_cap = ht_read_freq_cap(cur->dev, cur->pos + cur->freq_cap_off); |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 136 | upstream_freq_cap = ht_read_freq_cap(prev->dev, prev->pos + prev->freq_cap_off); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 137 | present_width_cap = pci_read_config8(cur->dev, cur->pos + cur->config_off); |
| 138 | upstream_width_cap = pci_read_config8(prev->dev, prev->pos + prev->config_off); |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 139 | |
| 140 | /* Calculate the highest useable frequency */ |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 141 | freq = log2(present_freq_cap & upstream_freq_cap); |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 142 | |
| 143 | /* Calculate the highest width */ |
| 144 | ln_upstream_width_in = link_width_to_pow2[upstream_width_cap & 7]; |
| 145 | ln_present_width_out = link_width_to_pow2[(present_width_cap >> 4) & 7]; |
| 146 | if (ln_upstream_width_in > ln_present_width_out) { |
| 147 | ln_upstream_width_in = ln_present_width_out; |
| 148 | } |
| 149 | upstream_width = pow2_to_link_width[ln_upstream_width_in]; |
| 150 | present_width = pow2_to_link_width[ln_upstream_width_in] << 4; |
| 151 | |
| 152 | ln_upstream_width_out = link_width_to_pow2[(upstream_width_cap >> 4) & 7]; |
| 153 | ln_present_width_in = link_width_to_pow2[present_width_cap & 7]; |
| 154 | if (ln_upstream_width_out > ln_present_width_in) { |
| 155 | ln_upstream_width_out = ln_present_width_in; |
| 156 | } |
| 157 | upstream_width |= pow2_to_link_width[ln_upstream_width_out] << 4; |
| 158 | present_width |= pow2_to_link_width[ln_upstream_width_out]; |
| 159 | |
| 160 | /* Set the current device */ |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 161 | old_freq = pci_read_config8(cur->dev, cur->pos + cur->freq_off); |
| 162 | old_freq &= 0x0f; |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 163 | if (freq != old_freq) { |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 164 | unsigned new_freq; |
| 165 | pci_write_config8(cur->dev, cur->pos + cur->freq_off, freq); |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 166 | reset_needed = 1; |
| 167 | printk_spew("HyperT FreqP old %x new %x\n",old_freq,freq); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 168 | new_freq = pci_read_config8(cur->dev, cur->pos + cur->freq_off); |
| 169 | new_freq &= 0x0f; |
| 170 | if (new_freq != freq) { |
| 171 | printk_err("%s Hypertransport frequency would not set wanted: %x got: %x\n", |
| 172 | dev_path(dev), freq, new_freq); |
| 173 | } |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 174 | } |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 175 | old_width = pci_read_config8(cur->dev, cur->pos + cur->config_off + 1); |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 176 | if (present_width != old_width) { |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 177 | unsigned new_width; |
| 178 | pci_write_config8(cur->dev, cur->pos + cur->config_off + 1, |
| 179 | present_width); |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 180 | reset_needed = 1; |
| 181 | printk_spew("HyperT widthP old %x new %x\n",old_width, present_width); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 182 | new_width = pci_read_config8(cur->dev, cur->pos + cur->config_off + 1); |
| 183 | if (new_width != present_width) { |
| 184 | printk_err("%s Hypertransport width would not set wanted: %x got: %x\n", |
| 185 | dev_path(dev), present_width, new_width); |
| 186 | } |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* Set the upstream device */ |
| 190 | old_freq = pci_read_config8(prev->dev, prev->pos + prev->freq_off); |
| 191 | old_freq &= 0x0f; |
| 192 | if (freq != old_freq) { |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 193 | unsigned new_freq; |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 194 | pci_write_config8(prev->dev, prev->pos + prev->freq_off, freq); |
| 195 | reset_needed = 1; |
| 196 | printk_spew("HyperT freqU old %x new %x\n", old_freq, freq); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 197 | new_freq = pci_read_config8(prev->dev, prev->pos + prev->freq_off); |
| 198 | new_freq &= 0x0f; |
| 199 | if (new_freq != freq) { |
| 200 | printk_err("%s Hypertransport frequency would not set wanted: %x got: %x\n", |
| 201 | dev_path(prev->dev), freq, new_freq); |
| 202 | } |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 203 | } |
| 204 | old_width = pci_read_config8(prev->dev, prev->pos + prev->config_off + 1); |
| 205 | if (upstream_width != old_width) { |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 206 | unsigned new_width; |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 207 | pci_write_config8(prev->dev, prev->pos + prev->config_off + 1, upstream_width); |
| 208 | reset_needed = 1; |
| 209 | printk_spew("HyperT widthU old %x new %x\n", old_width, upstream_width); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 210 | new_width = pci_read_config8(prev->dev, prev->pos + prev->config_off + 1); |
| 211 | if (new_width != upstream_width) { |
| 212 | printk_err("%s Hypertransport width would not set wanted: %x got: %x\n", |
| 213 | dev_path(prev->dev), upstream_width, new_width); |
| 214 | } |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 215 | } |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 216 | #endif |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 217 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 218 | /* Remember the current link as the previous link, |
| 219 | * But look at the other offsets. |
| 220 | */ |
| 221 | prev->dev = cur->dev; |
| 222 | prev->pos = cur->pos; |
| 223 | if (cur->ctrl_off == PCI_HT_CAP_SLAVE_CTRL0) { |
| 224 | prev->ctrl_off = PCI_HT_CAP_SLAVE_CTRL1; |
| 225 | prev->config_off = PCI_HT_CAP_SLAVE_WIDTH1; |
| 226 | prev->freq_off = PCI_HT_CAP_SLAVE_FREQ1; |
| 227 | prev->freq_cap_off = PCI_HT_CAP_SLAVE_FREQ_CAP1; |
| 228 | } else { |
| 229 | prev->ctrl_off = PCI_HT_CAP_SLAVE_CTRL0; |
| 230 | prev->config_off = PCI_HT_CAP_SLAVE_WIDTH0; |
| 231 | prev->freq_off = PCI_HT_CAP_SLAVE_FREQ0; |
| 232 | prev->freq_cap_off = PCI_HT_CAP_SLAVE_FREQ_CAP0; |
| 233 | } |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 234 | |
| 235 | return reset_needed; |
| 236 | |
| 237 | } |
| 238 | |
| 239 | static unsigned ht_lookup_slave_capability(struct device *dev) |
| 240 | { |
| 241 | unsigned pos; |
| 242 | pos = 0; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 243 | do { |
| 244 | pos = pci_find_next_capability(dev, PCI_CAP_ID_HT, pos); |
| 245 | if (pos) { |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 246 | unsigned flags; |
| 247 | flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 248 | printk_spew("flags: 0x%04x\n", flags); |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 249 | if ((flags >> 13) == 0) { |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 250 | /* Entry is a Slave secondary, success... */ |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 251 | break; |
| 252 | } |
| 253 | } |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 254 | } while(pos); |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 255 | return pos; |
| 256 | } |
| 257 | |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame^] | 258 | static void ht_collapse_early_enumeration(struct bus *bus, unsigned offset_unitid) |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 259 | { |
| 260 | unsigned int devfn; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 261 | struct ht_link prev; |
| 262 | unsigned ctrl; |
| 263 | |
| 264 | /* Initialize the hypertransport enumeration state */ |
| 265 | prev.dev = bus->dev; |
| 266 | prev.pos = bus->cap; |
| 267 | prev.ctrl_off = PCI_HT_CAP_HOST_CTRL; |
| 268 | prev.config_off = PCI_HT_CAP_HOST_WIDTH; |
| 269 | prev.freq_off = PCI_HT_CAP_HOST_FREQ; |
| 270 | prev.freq_cap_off = PCI_HT_CAP_HOST_FREQ_CAP; |
| 271 | |
| 272 | /* Wait until the link initialization is complete */ |
| 273 | do { |
| 274 | ctrl = pci_read_config16(prev.dev, prev.pos + prev.ctrl_off); |
| 275 | /* Is this the end of the hypertransport chain */ |
| 276 | if (ctrl & (1 << 6)) { |
| 277 | return; |
| 278 | } |
| 279 | /* Has the link failed? */ |
| 280 | if (ctrl & (1 << 4)) { |
| 281 | return; |
| 282 | } |
| 283 | } while((ctrl & (1 << 5)) == 0); |
| 284 | |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame^] | 285 | //actually, only for one HT device HT chain, and unitid is 0 |
| 286 | #if HT_CHAIN_UNITID_BASE == 0 |
| 287 | if(offset_unitid) { |
| 288 | return; |
| 289 | } |
| 290 | #endif |
| 291 | |
| 292 | /* Check if is already collapsed */ |
| 293 | if((!offset_unitid)|| (offset_unitid && (!((HT_CHAIN_END_UNITID_BASE == 0) && (HT_CHAIN_END_UNITID_BASE <HT_CHAIN_UNITID_BASE))))) { |
| 294 | struct device dummy; |
| 295 | uint32_t id; |
| 296 | dummy.bus = bus; |
| 297 | dummy.path.type = DEVICE_PATH_PCI; |
| 298 | dummy.path.u.pci.devfn = PCI_DEVFN(0, 0); |
| 299 | id = pci_read_config32(&dummy, PCI_VENDOR_ID); |
| 300 | if ( ! ( (id == 0xffffffff) || (id == 0x00000000) || |
| 301 | (id == 0x0000ffff) || (id == 0xffff0000) ) ) { |
| 302 | return; |
| 303 | } |
| 304 | } |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 305 | |
| 306 | /* Spin through the devices and collapse any early |
| 307 | * hypertransport enumeration. |
| 308 | */ |
Yinghai Lu | 1f1085b | 2005-01-17 21:37:12 +0000 | [diff] [blame] | 309 | for(devfn = PCI_DEVFN(1, 0); devfn <= 0xff; devfn += 8) { |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 310 | struct device dummy; |
| 311 | uint32_t id; |
| 312 | unsigned pos, flags; |
| 313 | dummy.bus = bus; |
| 314 | dummy.path.type = DEVICE_PATH_PCI; |
| 315 | dummy.path.u.pci.devfn = devfn; |
| 316 | id = pci_read_config32(&dummy, PCI_VENDOR_ID); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 317 | if ( (id == 0xffffffff) || (id == 0x00000000) || |
| 318 | (id == 0x0000ffff) || (id == 0xffff0000)) { |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 319 | continue; |
| 320 | } |
| 321 | dummy.vendor = id & 0xffff; |
| 322 | dummy.device = (id >> 16) & 0xffff; |
| 323 | dummy.hdr_type = pci_read_config8(&dummy, PCI_HEADER_TYPE); |
| 324 | pos = ht_lookup_slave_capability(&dummy); |
| 325 | if (!pos){ |
| 326 | continue; |
| 327 | } |
| 328 | |
| 329 | /* Clear the unitid */ |
| 330 | flags = pci_read_config16(&dummy, pos + PCI_CAP_FLAGS); |
| 331 | flags &= ~0x1f; |
| 332 | pci_write_config16(&dummy, pos + PCI_CAP_FLAGS, flags); |
| 333 | printk_spew("Collapsing %s [%04x/%04x]\n", |
| 334 | dev_path(&dummy), dummy.vendor, dummy.device); |
| 335 | } |
| 336 | } |
| 337 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 338 | unsigned int hypertransport_scan_chain(struct bus *bus, |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame^] | 339 | unsigned min_devfn, unsigned max_devfn, unsigned int max, unsigned offset_unitid) |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 340 | { |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame^] | 341 | //even HT_CHAIN_UNITID_BASE == 0, we still can go through this function, because of end_of_chain check, also We need it to optimize link |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 342 | unsigned next_unitid, last_unitid; |
| 343 | device_t old_devices, dev, func; |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame^] | 344 | unsigned min_unitid = (offset_unitid) ? HT_CHAIN_UNITID_BASE:1; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 345 | struct ht_link prev; |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame^] | 346 | device_t last_func = 0; |
| 347 | |
| 348 | #if HT_CHAIN_END_UNITID_BASE < HT_CHAIN_UNITID_BASE |
| 349 | //let't record the device of last ht device, So we can set the Unitid to HT_CHAIN_END_UNITID_BASE |
| 350 | unsigned real_last_unitid; |
| 351 | uint8_t real_last_pos; |
| 352 | device_t real_last_dev; |
| 353 | int ht_dev_num = 0; |
| 354 | #endif |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 355 | |
| 356 | /* Restore the hypertransport chain to it's unitialized state */ |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame^] | 357 | ht_collapse_early_enumeration(bus, offset_unitid); |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 358 | |
| 359 | /* See which static device nodes I have */ |
| 360 | old_devices = bus->children; |
| 361 | bus->children = 0; |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 362 | |
| 363 | /* Initialize the hypertransport enumeration state */ |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 364 | prev.dev = bus->dev; |
| 365 | prev.pos = bus->cap; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 366 | prev.ctrl_off = PCI_HT_CAP_HOST_CTRL; |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 367 | prev.config_off = PCI_HT_CAP_HOST_WIDTH; |
| 368 | prev.freq_off = PCI_HT_CAP_HOST_FREQ; |
| 369 | prev.freq_cap_off = PCI_HT_CAP_HOST_FREQ_CAP; |
| 370 | |
| 371 | /* If present assign unitid to a hypertransport chain */ |
| 372 | last_unitid = min_unitid -1; |
| 373 | next_unitid = min_unitid; |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 374 | do { |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 375 | uint8_t pos; |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 376 | uint16_t flags; |
| 377 | unsigned count, static_count; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 378 | unsigned ctrl; |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 379 | |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 380 | last_unitid = next_unitid; |
| 381 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 382 | /* Wait until the link initialization is complete */ |
| 383 | do { |
| 384 | ctrl = pci_read_config16(prev.dev, prev.pos + prev.ctrl_off); |
| 385 | /* Is this the end of the hypertransport chain? |
| 386 | * Has the link failed? |
| 387 | * If so further scanning is pointless. |
| 388 | */ |
| 389 | if (ctrl & ((1 << 6) | (1 << 4))) { |
| 390 | goto end_of_chain; |
| 391 | } |
| 392 | } while((ctrl & (1 << 5)) == 0); |
| 393 | |
| 394 | |
| 395 | /* Get and setup the device_structure */ |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 396 | dev = ht_scan_get_devs(&old_devices); |
| 397 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 398 | /* See if a device is present and setup the |
| 399 | * device structure. |
| 400 | */ |
| 401 | dev = pci_probe_dev(dev, bus, 0); |
| 402 | if (!dev || !dev->enabled) { |
| 403 | break; |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 404 | } |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 405 | |
| 406 | /* Find the hypertransport link capability */ |
| 407 | pos = ht_lookup_slave_capability(dev); |
| 408 | if (pos == 0) { |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 409 | printk_err("%s Hypertransport link capability not found", |
| 410 | dev_path(dev)); |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 411 | break; |
| 412 | } |
| 413 | |
| 414 | /* Update the Unitid of the current device */ |
| 415 | flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 416 | |
| 417 | /* If the devices has a unitid set and is at devfn 0 we are done. |
| 418 | * This can happen with shadow hypertransport devices, |
| 419 | * or if we have reached the bottom of a |
| 420 | * hypertransport device chain. |
| 421 | */ |
| 422 | if (flags & 0x1f) { |
| 423 | break; |
| 424 | } |
| 425 | |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 426 | flags &= ~0x1f; /* mask out base Unit ID */ |
arch import user (historical) | 98d0d30 | 2005-07-06 17:13:46 +0000 | [diff] [blame] | 427 | #if CK804_DEVN_BASE==0 |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 428 | if((dev->vendor == 0x10de) && (dev->device == 0x005e)) { |
| 429 | next_unitid = 0; |
| 430 | } |
| 431 | else { |
arch import user (historical) | 98d0d30 | 2005-07-06 17:13:46 +0000 | [diff] [blame] | 432 | #endif |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 433 | flags |= next_unitid & 0x1f; |
| 434 | pci_write_config16(dev, pos + PCI_CAP_FLAGS, flags); |
arch import user (historical) | 98d0d30 | 2005-07-06 17:13:46 +0000 | [diff] [blame] | 435 | #if CK804_DEVN_BASE==0 |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 436 | } |
arch import user (historical) | 98d0d30 | 2005-07-06 17:13:46 +0000 | [diff] [blame] | 437 | #endif |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 438 | |
| 439 | /* Update the Unitd id in the device structure */ |
| 440 | static_count = 1; |
| 441 | for(func = dev; func; func = func->sibling) { |
| 442 | func->path.u.pci.devfn += (next_unitid << 3); |
| 443 | static_count = (func->path.u.pci.devfn >> 3) |
| 444 | - (dev->path.u.pci.devfn >> 3) + 1; |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame^] | 445 | last_func = func; |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /* Compute the number of unitids consumed */ |
| 449 | count = (flags >> 5) & 0x1f; /* get unit count */ |
| 450 | printk_spew("%s count: %04x static_count: %04x\n", |
| 451 | dev_path(dev), count, static_count); |
| 452 | if (count < static_count) { |
| 453 | count = static_count; |
| 454 | } |
| 455 | |
| 456 | /* Update the Unitid of the next device */ |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame^] | 457 | #if HT_CHAIN_END_UNITID_BASE < HT_CHAIN_UNITID_BASE |
| 458 | if(offset_unitid) { |
| 459 | real_last_unitid = next_unitid; |
| 460 | real_last_pos = pos; |
| 461 | real_last_dev = dev; |
| 462 | ht_dev_num++; |
| 463 | } |
| 464 | #endif |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 465 | next_unitid += count; |
| 466 | |
| 467 | /* Setup the hypetransport link */ |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 468 | bus->reset_needed |= ht_setup_link(&prev, dev, pos); |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 469 | |
| 470 | printk_debug("%s [%04x/%04x] %s next_unitid: %04x\n", |
| 471 | dev_path(dev), |
| 472 | dev->vendor, dev->device, |
Li-Ta Lo | 69c5a90 | 2004-04-29 20:08:54 +0000 | [diff] [blame] | 473 | (dev->enabled? "enabled": "disabled"), next_unitid); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 474 | |
arch import user (historical) | 98d0d30 | 2005-07-06 17:13:46 +0000 | [diff] [blame] | 475 | #if CK804_DEVN_BASE==0 |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 476 | if ((dev->vendor == 0x10de) && (dev->device == 0x005e)) { |
| 477 | break; // CK804 can not change unitid, so it only can be alone in the link |
| 478 | } |
arch import user (historical) | 98d0d30 | 2005-07-06 17:13:46 +0000 | [diff] [blame] | 479 | #endif |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 480 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 481 | } while((last_unitid != next_unitid) && (next_unitid <= (max_devfn >> 3))); |
| 482 | end_of_chain: |
Yinghai Lu | 90b3e09 | 2005-01-26 22:00:20 +0000 | [diff] [blame] | 483 | #if OPT_HT_LINK == 1 |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 484 | if(bus->reset_needed) { |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 485 | printk_info("HyperT reset needed\n"); |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 486 | } |
| 487 | else { |
| 488 | printk_debug("HyperT reset not needed\n"); |
| 489 | } |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 490 | #endif |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame^] | 491 | |
| 492 | #if HT_CHAIN_END_UNITID_BASE < HT_CHAIN_UNITID_BASE |
| 493 | if(offset_unitid && (ht_dev_num>0)) { |
| 494 | uint16_t flags; |
| 495 | int i; |
| 496 | device_t last_func = 0; |
| 497 | flags = pci_read_config16(real_last_dev, real_last_pos + PCI_CAP_FLAGS); |
| 498 | flags &= ~0x1f; |
| 499 | flags |= HT_CHAIN_END_UNITID_BASE & 0x1f; |
| 500 | pci_write_config16(real_last_dev, real_last_pos + PCI_CAP_FLAGS, flags); |
| 501 | |
| 502 | for(func = real_last_dev; func; func = func->sibling) { |
| 503 | func->path.u.pci.devfn -= ((real_last_unitid - HT_CHAIN_END_UNITID_BASE) << 3); |
| 504 | last_func = func; |
| 505 | } |
| 506 | |
| 507 | next_unitid = real_last_unitid; |
| 508 | } |
| 509 | #endif |
| 510 | |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 511 | if (next_unitid > 0x1f) { |
| 512 | next_unitid = 0x1f; |
| 513 | } |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 514 | |
| 515 | /* Die if any leftover Static devices are are found. |
| 516 | * There's probably a problem in the Config.lb. |
| 517 | */ |
| 518 | if(old_devices) { |
| 519 | device_t left; |
| 520 | for(left = old_devices; left; left = left->sibling) { |
| 521 | printk_debug("%s\n", dev_path(left)); |
| 522 | } |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame^] | 523 | printk_err("HT: Left over static devices. Check your Config.lb\n"); |
| 524 | if(last_func && !last_func->sibling) // put back the left over static device, and let pci_scan_bus disable it |
| 525 | last_func->sibling = old_devices; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 526 | } |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame^] | 527 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 528 | /* Now that nothing is overlapping it is safe to scan the |
| 529 | * children. |
| 530 | */ |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame^] | 531 | max = pci_scan_bus(bus, 0x00, (next_unitid << 3)|7, max); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 532 | return max; |
Eric Biederman | 0ac6b41 | 2003-09-02 17:16:48 +0000 | [diff] [blame] | 533 | } |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 534 | |
| 535 | /** |
| 536 | * @brief Scan a PCI bridge and the buses behind the bridge. |
| 537 | * |
| 538 | * Determine the existence of buses behind the bridge. Set up the bridge |
| 539 | * according to the result of the scan. |
| 540 | * |
| 541 | * This function is the default scan_bus() method for PCI bridge devices. |
| 542 | * |
| 543 | * @param dev pointer to the bridge device |
| 544 | * @param max the highest bus number assgined up to now |
| 545 | * |
| 546 | * @return The maximum bus number found, after scanning all subordinate busses |
| 547 | */ |
| 548 | unsigned int ht_scan_bridge(struct device *dev, unsigned int max) |
| 549 | { |
| 550 | return do_pci_scan_bridge(dev, max, hypertransport_scan_chain); |
| 551 | } |
| 552 | |
| 553 | |
| 554 | /** Default device operations for hypertransport bridges */ |
| 555 | static struct pci_operations ht_bus_ops_pci = { |
| 556 | .set_subsystem = 0, |
| 557 | }; |
| 558 | |
| 559 | struct device_operations default_ht_ops_bus = { |
| 560 | .read_resources = pci_bus_read_resources, |
| 561 | .set_resources = pci_dev_set_resources, |
| 562 | .enable_resources = pci_bus_enable_resources, |
| 563 | .init = 0, |
| 564 | .scan_bus = ht_scan_bridge, |
| 565 | .enable = 0, |
| 566 | .reset_bus = pci_bus_reset, |
| 567 | .ops_pci = &ht_bus_ops_pci, |
| 568 | }; |