Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1 | #include <console/console.h> |
Eric Biederman | 7003ba4 | 2004-10-16 06:20:29 +0000 | [diff] [blame] | 2 | #include <device/device.h> |
| 3 | #include <device/path.h> |
Yinghai Lu | f327d9f | 2008-02-20 17:41:38 +0000 | [diff] [blame] | 4 | #include <device/pci_ids.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 5 | #include <cpu/cpu.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 6 | #include <arch/smp/mpspec.h> |
| 7 | #include <string.h> |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 8 | #include <arch/cpu.h> |
| 9 | #include <cpu/x86/lapic.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 10 | |
| 11 | unsigned char smp_compute_checksum(void *v, int len) |
| 12 | { |
| 13 | unsigned char *bytes; |
| 14 | unsigned char checksum; |
| 15 | int i; |
| 16 | bytes = v; |
| 17 | checksum = 0; |
| 18 | for(i = 0; i < len; i++) { |
| 19 | checksum -= bytes[i]; |
| 20 | } |
| 21 | return checksum; |
| 22 | } |
| 23 | |
| 24 | void *smp_write_floating_table(unsigned long addr) |
| 25 | { |
| 26 | struct intel_mp_floating *mf; |
| 27 | void *v; |
| 28 | |
| 29 | /* 16 byte align the table address */ |
Yinghai Lu | f327d9f | 2008-02-20 17:41:38 +0000 | [diff] [blame] | 30 | addr = (addr + 0xf) & (~0xf); |
Patrick Georgi | e914903 | 2009-04-24 06:27:31 +0000 | [diff] [blame^] | 31 | return smp_write_floating_table_physaddr(addr, addr + SMP_FLOATING_TABLE_LEN); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Stefan Reinauer | 4f1cb23 | 2006-07-19 15:32:49 +0000 | [diff] [blame] | 34 | void *smp_write_floating_table_physaddr(unsigned long addr, unsigned long mpf_physptr) |
| 35 | { |
| 36 | struct intel_mp_floating *mf; |
| 37 | void *v; |
Yinghai Lu | f327d9f | 2008-02-20 17:41:38 +0000 | [diff] [blame] | 38 | |
| 39 | v = (void *)addr; |
Stefan Reinauer | 4f1cb23 | 2006-07-19 15:32:49 +0000 | [diff] [blame] | 40 | mf = v; |
| 41 | mf->mpf_signature[0] = '_'; |
| 42 | mf->mpf_signature[1] = 'M'; |
| 43 | mf->mpf_signature[2] = 'P'; |
| 44 | mf->mpf_signature[3] = '_'; |
| 45 | mf->mpf_physptr = mpf_physptr; |
| 46 | mf->mpf_length = 1; |
| 47 | mf->mpf_specification = 4; |
| 48 | mf->mpf_checksum = 0; |
| 49 | mf->mpf_feature1 = 0; |
| 50 | mf->mpf_feature2 = 0; |
| 51 | mf->mpf_feature3 = 0; |
| 52 | mf->mpf_feature4 = 0; |
| 53 | mf->mpf_feature5 = 0; |
| 54 | mf->mpf_checksum = smp_compute_checksum(mf, mf->mpf_length*16); |
| 55 | return v; |
| 56 | } |
| 57 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 58 | void *smp_next_mpc_entry(struct mp_config_table *mc) |
| 59 | { |
| 60 | void *v; |
| 61 | v = (void *)(((char *)mc) + mc->mpc_length); |
| 62 | return v; |
| 63 | } |
| 64 | static void smp_add_mpc_entry(struct mp_config_table *mc, unsigned length) |
| 65 | { |
| 66 | mc->mpc_length += length; |
| 67 | mc->mpc_entry_count++; |
| 68 | } |
| 69 | |
| 70 | void *smp_next_mpe_entry(struct mp_config_table *mc) |
| 71 | { |
| 72 | void *v; |
| 73 | v = (void *)(((char *)mc) + mc->mpc_length + mc->mpe_length); |
| 74 | return v; |
| 75 | } |
| 76 | static void smp_add_mpe_entry(struct mp_config_table *mc, mpe_t mpe) |
| 77 | { |
| 78 | mc->mpe_length += mpe->mpe_length; |
| 79 | } |
| 80 | |
| 81 | void smp_write_processor(struct mp_config_table *mc, |
| 82 | unsigned char apicid, unsigned char apicver, |
| 83 | unsigned char cpuflag, unsigned int cpufeature, |
| 84 | unsigned int featureflag) |
| 85 | { |
| 86 | struct mpc_config_processor *mpc; |
| 87 | mpc = smp_next_mpc_entry(mc); |
| 88 | memset(mpc, '\0', sizeof(*mpc)); |
| 89 | mpc->mpc_type = MP_PROCESSOR; |
| 90 | mpc->mpc_apicid = apicid; |
| 91 | mpc->mpc_apicver = apicver; |
| 92 | mpc->mpc_cpuflag = cpuflag; |
| 93 | mpc->mpc_cpufeature = cpufeature; |
| 94 | mpc->mpc_featureflag = featureflag; |
| 95 | smp_add_mpc_entry(mc, sizeof(*mpc)); |
| 96 | } |
| 97 | |
| 98 | /* If we assume a symmetric processor configuration we can |
| 99 | * get all of the information we need to write the processor |
| 100 | * entry from the bootstrap processor. |
| 101 | * Plus I don't think linux really even cares. |
| 102 | * Having the proper apicid's in the table so the non-bootstrap |
| 103 | * processors can be woken up should be enough. |
| 104 | */ |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 105 | void smp_write_processors(struct mp_config_table *mc) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 106 | { |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 107 | int boot_apic_id; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 108 | unsigned apic_version; |
| 109 | unsigned cpu_features; |
| 110 | unsigned cpu_feature_flags; |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 111 | struct cpuid_result result; |
| 112 | device_t cpu; |
Eric Biederman | 7003ba4 | 2004-10-16 06:20:29 +0000 | [diff] [blame] | 113 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 114 | boot_apic_id = lapicid(); |
| 115 | apic_version = lapic_read(LAPIC_LVR) & 0xff; |
| 116 | result = cpuid(1); |
| 117 | cpu_features = result.eax; |
| 118 | cpu_feature_flags = result.edx; |
Eric Biederman | 7003ba4 | 2004-10-16 06:20:29 +0000 | [diff] [blame] | 119 | for(cpu = all_devices; cpu; cpu = cpu->next) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 120 | unsigned long cpu_flag; |
Eric Biederman | 7003ba4 | 2004-10-16 06:20:29 +0000 | [diff] [blame] | 121 | if ((cpu->path.type != DEVICE_PATH_APIC) || |
| 122 | (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER)) |
| 123 | { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 124 | continue; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 125 | } |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 126 | if (!cpu->enabled) { |
| 127 | continue; |
| 128 | } |
| 129 | cpu_flag = MPC_CPU_ENABLED; |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 130 | if (boot_apic_id == cpu->path.apic.apic_id) { |
Eric Biederman | f3ed1cf | 2004-10-16 08:38:58 +0000 | [diff] [blame] | 131 | cpu_flag = MPC_CPU_ENABLED | MPC_CPU_BOOTPROCESSOR; |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 132 | } |
| 133 | smp_write_processor(mc, |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 134 | cpu->path.apic.apic_id, apic_version, |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 135 | cpu_flag, cpu_features, cpu_feature_flags |
| 136 | ); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | void smp_write_bus(struct mp_config_table *mc, |
Stefan Reinauer | 7a4f688 | 2008-08-01 11:31:08 +0000 | [diff] [blame] | 141 | unsigned char id, char *bustype) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 142 | { |
| 143 | struct mpc_config_bus *mpc; |
| 144 | mpc = smp_next_mpc_entry(mc); |
| 145 | memset(mpc, '\0', sizeof(*mpc)); |
| 146 | mpc->mpc_type = MP_BUS; |
| 147 | mpc->mpc_busid = id; |
| 148 | memcpy(mpc->mpc_bustype, bustype, sizeof(mpc->mpc_bustype)); |
| 149 | smp_add_mpc_entry(mc, sizeof(*mpc)); |
| 150 | } |
| 151 | |
| 152 | void smp_write_ioapic(struct mp_config_table *mc, |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 153 | unsigned char id, unsigned char ver, |
| 154 | unsigned long apicaddr) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 155 | { |
| 156 | struct mpc_config_ioapic *mpc; |
| 157 | mpc = smp_next_mpc_entry(mc); |
| 158 | memset(mpc, '\0', sizeof(*mpc)); |
| 159 | mpc->mpc_type = MP_IOAPIC; |
| 160 | mpc->mpc_apicid = id; |
| 161 | mpc->mpc_apicver = ver; |
| 162 | mpc->mpc_flags = MPC_APIC_USABLE; |
| 163 | mpc->mpc_apicaddr = apicaddr; |
| 164 | smp_add_mpc_entry(mc, sizeof(*mpc)); |
| 165 | } |
| 166 | |
| 167 | void smp_write_intsrc(struct mp_config_table *mc, |
| 168 | unsigned char irqtype, unsigned short irqflag, |
| 169 | unsigned char srcbus, unsigned char srcbusirq, |
| 170 | unsigned char dstapic, unsigned char dstirq) |
| 171 | { |
| 172 | struct mpc_config_intsrc *mpc; |
| 173 | mpc = smp_next_mpc_entry(mc); |
| 174 | memset(mpc, '\0', sizeof(*mpc)); |
| 175 | mpc->mpc_type = MP_INTSRC; |
| 176 | mpc->mpc_irqtype = irqtype; |
| 177 | mpc->mpc_irqflag = irqflag; |
| 178 | mpc->mpc_srcbus = srcbus; |
| 179 | mpc->mpc_srcbusirq = srcbusirq; |
| 180 | mpc->mpc_dstapic = dstapic; |
| 181 | mpc->mpc_dstirq = dstirq; |
| 182 | smp_add_mpc_entry(mc, sizeof(*mpc)); |
Stefan Reinauer | 5bba752 | 2009-04-21 23:00:14 +0000 | [diff] [blame] | 183 | #ifdef DEBUG_MPTABLE |
| 184 | printk_debug("add intsrc srcbus 0x%x srcbusirq 0x%x, dstapic 0x%x, dstirq 0x%x\n", |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 185 | srcbus, srcbusirq, dstapic, dstirq); |
Myles Watson | 552b327 | 2009-02-12 21:30:06 +0000 | [diff] [blame] | 186 | hexdump(__func__, mpc, sizeof(*mpc)); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 187 | #endif |
| 188 | } |
| 189 | |
Yinghai Lu | f327d9f | 2008-02-20 17:41:38 +0000 | [diff] [blame] | 190 | void smp_write_intsrc_pci_bridge(struct mp_config_table *mc, |
| 191 | unsigned char irqtype, unsigned short irqflag, |
| 192 | struct device *dev, |
| 193 | unsigned char dstapic, unsigned char *dstirq) |
| 194 | { |
| 195 | struct device *child; |
| 196 | |
| 197 | int linkn; |
| 198 | int i; |
| 199 | int srcbus; |
| 200 | int slot; |
| 201 | |
| 202 | struct bus *link; |
| 203 | unsigned char dstirq_x[4]; |
| 204 | |
| 205 | for (linkn = 0; linkn < dev->links; linkn++) { |
| 206 | |
| 207 | link = &dev->link[linkn]; |
| 208 | child = link->children; |
| 209 | srcbus = link->secondary; |
| 210 | |
| 211 | while (child) { |
| 212 | if (child->path.type != DEVICE_PATH_PCI) |
| 213 | goto next; |
| 214 | |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 215 | slot = (child->path.pci.devfn >> 3); |
Yinghai Lu | f327d9f | 2008-02-20 17:41:38 +0000 | [diff] [blame] | 216 | /* round pins */ |
| 217 | for (i = 0; i < 4; i++) |
| 218 | dstirq_x[i] = dstirq[(i + slot) % 4]; |
| 219 | |
| 220 | if ((child->class >> 16) != PCI_BASE_CLASS_BRIDGE) { |
| 221 | /* pci device */ |
Myles Watson | c4ddbff | 2009-02-09 17:52:54 +0000 | [diff] [blame] | 222 | printk_debug("route irq: %s\n", dev_path(child)); |
Yinghai Lu | f327d9f | 2008-02-20 17:41:38 +0000 | [diff] [blame] | 223 | for (i = 0; i < 4; i++) |
| 224 | smp_write_intsrc(mc, irqtype, irqflag, srcbus, (slot<<2)|i, dstapic, dstirq_x[i]); |
| 225 | goto next; |
| 226 | } |
| 227 | |
| 228 | switch (child->class>>8) { |
| 229 | case PCI_CLASS_BRIDGE_PCI: |
| 230 | case PCI_CLASS_BRIDGE_PCMCIA: |
| 231 | case PCI_CLASS_BRIDGE_CARDBUS: |
Myles Watson | c4ddbff | 2009-02-09 17:52:54 +0000 | [diff] [blame] | 232 | printk_debug("route irq bridge: %s\n", dev_path(child)); |
Yinghai Lu | f327d9f | 2008-02-20 17:41:38 +0000 | [diff] [blame] | 233 | smp_write_intsrc_pci_bridge(mc, irqtype, irqflag, child, dstapic, dstirq_x); |
| 234 | } |
| 235 | |
| 236 | next: |
| 237 | child = child->sibling; |
| 238 | } |
| 239 | |
| 240 | } |
| 241 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 242 | |
| 243 | void smp_write_lintsrc(struct mp_config_table *mc, |
| 244 | unsigned char irqtype, unsigned short irqflag, |
| 245 | unsigned char srcbusid, unsigned char srcbusirq, |
| 246 | unsigned char destapic, unsigned char destapiclint) |
| 247 | { |
| 248 | struct mpc_config_lintsrc *mpc; |
| 249 | mpc = smp_next_mpc_entry(mc); |
| 250 | memset(mpc, '\0', sizeof(*mpc)); |
| 251 | mpc->mpc_type = MP_LINTSRC; |
| 252 | mpc->mpc_irqtype = irqtype; |
| 253 | mpc->mpc_irqflag = irqflag; |
| 254 | mpc->mpc_srcbusid = srcbusid; |
| 255 | mpc->mpc_srcbusirq = srcbusirq; |
| 256 | mpc->mpc_destapic = destapic; |
| 257 | mpc->mpc_destapiclint = destapiclint; |
| 258 | smp_add_mpc_entry(mc, sizeof(*mpc)); |
| 259 | } |
| 260 | |
| 261 | void smp_write_address_space(struct mp_config_table *mc, |
| 262 | unsigned char busid, unsigned char address_type, |
| 263 | unsigned int address_base_low, unsigned int address_base_high, |
| 264 | unsigned int address_length_low, unsigned int address_length_high) |
| 265 | { |
| 266 | struct mp_exten_system_address_space *mpe; |
| 267 | mpe = smp_next_mpe_entry(mc); |
| 268 | memset(mpe, '\0', sizeof(*mpe)); |
| 269 | mpe->mpe_type = MPE_SYSTEM_ADDRESS_SPACE; |
| 270 | mpe->mpe_length = sizeof(*mpe); |
| 271 | mpe->mpe_busid = busid; |
| 272 | mpe->mpe_address_type = address_type; |
| 273 | mpe->mpe_address_base_low = address_base_low; |
| 274 | mpe->mpe_address_base_high = address_base_high; |
| 275 | mpe->mpe_address_length_low = address_length_low; |
| 276 | mpe->mpe_address_length_high = address_length_high; |
| 277 | smp_add_mpe_entry(mc, (mpe_t)mpe); |
| 278 | } |
| 279 | |
| 280 | |
| 281 | void smp_write_bus_hierarchy(struct mp_config_table *mc, |
| 282 | unsigned char busid, unsigned char bus_info, |
| 283 | unsigned char parent_busid) |
| 284 | { |
| 285 | struct mp_exten_bus_hierarchy *mpe; |
| 286 | mpe = smp_next_mpe_entry(mc); |
| 287 | memset(mpe, '\0', sizeof(*mpe)); |
| 288 | mpe->mpe_type = MPE_BUS_HIERARCHY; |
| 289 | mpe->mpe_length = sizeof(*mpe); |
| 290 | mpe->mpe_busid = busid; |
| 291 | mpe->mpe_bus_info = bus_info; |
| 292 | mpe->mpe_parent_busid = parent_busid; |
| 293 | smp_add_mpe_entry(mc, (mpe_t)mpe); |
| 294 | } |
| 295 | |
| 296 | void smp_write_compatibility_address_space(struct mp_config_table *mc, |
| 297 | unsigned char busid, unsigned char address_modifier, |
| 298 | unsigned int range_list) |
| 299 | { |
| 300 | struct mp_exten_compatibility_address_space *mpe; |
| 301 | mpe = smp_next_mpe_entry(mc); |
| 302 | memset(mpe, '\0', sizeof(*mpe)); |
| 303 | mpe->mpe_type = MPE_COMPATIBILITY_ADDRESS_SPACE; |
| 304 | mpe->mpe_length = sizeof(*mpe); |
| 305 | mpe->mpe_busid = busid; |
| 306 | mpe->mpe_address_modifier = address_modifier; |
| 307 | mpe->mpe_range_list = range_list; |
| 308 | smp_add_mpe_entry(mc, (mpe_t)mpe); |
| 309 | } |
| 310 | |