55c4440a691212100bfb56a1adac576b7eb12f33
[coreboot.git] / src / mainboard / arima / hdama / mptable.c
1 #include <console/console.h>
2 #include <arch/smp/mpspec.h>
3 #include <arch/ioapic.h>
4 #include <device/pci.h>
5 #include <string.h>
6 #include <stdint.h>
7 #include <cpu/x86/lapic.h>
8 #include <arch/cpu.h>
9 #include <arch/io.h>
10
11 #define HT_INIT_CONTROL 0x6c
12 #define HTIC_BIOSR_Detect  (1<<5)
13
14 /* If we assume a symmetric processor configuration we can
15  * get all of the information we need to write the processor
16  * entry from the bootstrap processor.
17  * Plus I don't think linux really even cares.
18  * Having the proper apicid's in the table so the non-bootstrap
19  *  processors can be woken up should be enough.  Linux-2.6.11 work-around.
20  */
21 static void smp_write_processors_inorder(struct mp_config_table *mc)
22 {
23         int boot_apic_id;
24         int order_id;
25         unsigned apic_version;
26         unsigned cpu_features;
27         unsigned cpu_feature_flags;
28         struct cpuid_result result;
29         device_t cpu;
30
31         boot_apic_id = lapicid();
32         apic_version = lapic_read(LAPIC_LVR) & 0xff;
33         result = cpuid(1);
34         cpu_features = result.eax;
35         cpu_feature_flags = result.edx;
36         /* order the output of the cpus to fix a bug in kernel 6 11 */
37         for(order_id = 0;order_id <256; order_id++) {
38             for(cpu = all_devices; cpu; cpu = cpu->next) {
39                 unsigned long cpu_flag;
40                 if ((cpu->path.type != DEVICE_PATH_APIC) ||
41                         (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER))
42                 {
43                         continue;
44                 }
45                 if (!cpu->enabled) {
46                         continue;
47                 }
48                 cpu_flag = MPC_CPU_ENABLED;
49                 if (boot_apic_id == cpu->path.apic.apic_id) {
50                         cpu_flag = MPC_CPU_ENABLED | MPC_CPU_BOOTPROCESSOR;
51                 }
52                 if(cpu->path.apic.apic_id == order_id) {
53                     smp_write_processor(mc,
54                         cpu->path.apic.apic_id, apic_version,
55                         cpu_flag, cpu_features, cpu_feature_flags);
56                     break;
57                 }
58             }
59         }
60 }
61
62 static unsigned node_link_to_bus(unsigned node, unsigned link)
63 {
64         device_t dev;
65         unsigned reg;
66
67         dev = dev_find_slot(0, PCI_DEVFN(0x18, 1));
68         if (!dev) {
69                 return 0xff;
70         }
71         for(reg = 0xE0; reg < 0xF0; reg += 0x04) {
72                 uint32_t config_map;
73                 unsigned dst_node;
74                 unsigned dst_link;
75                 unsigned bus_base;
76                 config_map = pci_read_config32(dev, reg);
77                 if ((config_map & 3) != 3) {
78                         continue;
79                 }
80                 dst_node = (config_map >> 4) & 7;
81                 dst_link = (config_map >> 8) & 3;
82                 bus_base = (config_map >> 16) & 0xff;
83 #if 0
84                 printk(BIOS_DEBUG, "node.link=bus: %d.%d=%d 0x%2x->0x%08x\n",
85                         dst_node, dst_link, bus_base,
86                         reg, config_map);
87 #endif
88                 if ((dst_node == node) && (dst_link == link))
89                 {
90                         return bus_base;
91                 }
92         }
93         return 0xff;
94 }
95
96 static unsigned max_apicid(void)
97 {
98         unsigned max;
99         device_t dev;
100         max = 0;
101         for(dev = all_devices; dev; dev = dev->next) {
102                 if (dev->path.type != DEVICE_PATH_APIC)
103                         continue;
104                 if (dev->path.apic.apic_id > max) {
105                         max = dev->path.apic.apic_id;
106                 }
107         }
108         return max;
109 }
110
111 static void *smp_write_config_table(void *v)
112 {
113         static const char sig[4] = "PCMP";
114         static const char oem[8] = "COREBOOT";
115         static const char productid[12] = "HDAMA       ";
116         struct mp_config_table *mc;
117         unsigned char bus_num;
118         unsigned char bus_isa;
119         unsigned char bus_chain_0;
120         unsigned char bus_8131_1;
121         unsigned char bus_8131_2;
122         unsigned char bus_8111_1;
123         unsigned apicid_base;
124         unsigned apicid_8111;
125         unsigned apicid_8131_1;
126         unsigned apicid_8131_2;
127
128         mc = (void *)(((char *)v) + SMP_FLOATING_TABLE_LEN);
129         memset(mc, 0, sizeof(*mc));
130
131         memcpy(mc->mpc_signature, sig, sizeof(sig));
132         mc->mpc_length = sizeof(*mc); /* initially just the header */
133         mc->mpc_spec = 0x04;
134         mc->mpc_checksum = 0; /* not yet computed */
135         memcpy(mc->mpc_oem, oem, sizeof(oem));
136         memcpy(mc->mpc_productid, productid, sizeof(productid));
137         mc->mpc_oemptr = 0;
138         mc->mpc_oemsize = 0;
139         mc->mpc_entry_count = 0; /* No entries yet... */
140         mc->mpc_lapic = LAPIC_ADDR;
141         mc->mpe_length = 0;
142         mc->mpe_checksum = 0;
143         mc->reserved = 0;
144
145         smp_write_processors_inorder(mc);
146
147         apicid_base = max_apicid() + 1;
148         apicid_8111 = apicid_base;
149         apicid_8131_1 = apicid_base + 1;
150         apicid_8131_2 = apicid_base + 2;
151         {
152                 device_t dev;
153
154                 /* HT chain 0 */
155                 bus_chain_0 = node_link_to_bus(0, 0);
156                 if (bus_chain_0 == 0xff) {
157                         printk(BIOS_DEBUG, "ERROR - cound not find bus for node 0 chain 0, using defaults\n");
158                         bus_chain_0 = 0;
159                 }
160
161                 /* 8111 */
162                 dev = dev_find_slot(bus_chain_0, PCI_DEVFN(0x03,0));
163                 if (dev) {
164                         bus_8111_1 = pci_read_config8(dev, PCI_SECONDARY_BUS);
165                         bus_isa    = pci_read_config8(dev, PCI_SUBORDINATE_BUS);
166                         bus_isa++;
167                 }
168                 else {
169                         printk(BIOS_DEBUG, "ERROR - could not find PCI %02x:03.0, using defaults\n", bus_chain_0);
170
171                         bus_8111_1 = 4;
172                         bus_isa = 5;
173                 }
174                 /* 8131-1 */
175                 dev = dev_find_slot(bus_chain_0, PCI_DEVFN(0x01,0));
176                 if (dev) {
177                         bus_8131_1 = pci_read_config8(dev, PCI_SECONDARY_BUS);
178
179                 }
180                 else {
181                         printk(BIOS_DEBUG, "ERROR - could not find PCI %02x:01.0, using defaults\n", bus_chain_0);
182
183                         bus_8131_1 = 2;
184                 }
185                 /* 8131-2 */
186                 dev = dev_find_slot(bus_chain_0, PCI_DEVFN(0x02,0));
187                 if (dev) {
188                         bus_8131_2 = pci_read_config8(dev, PCI_SECONDARY_BUS);
189
190                 }
191                 else {
192                         printk(BIOS_DEBUG, "ERROR - could not find PCI %02x:02.0, using defaults\n", bus_chain_0);
193
194                         bus_8131_2 = 3;
195                 }
196         }
197
198         /* define bus and isa numbers */
199         for(bus_num = 0; bus_num < bus_isa; bus_num++) {
200                 smp_write_bus(mc, bus_num, "PCI   ");
201         }
202         smp_write_bus(mc, bus_isa, "ISA   ");
203
204         /* IOAPIC handling */
205         smp_write_ioapic(mc, apicid_8111, 0x11, IO_APIC_ADDR);
206         {
207                 device_t dev;
208                 struct resource *res;
209                 /* 8131 apic 3 */
210                 dev = dev_find_slot(bus_chain_0, PCI_DEVFN(0x01,1));
211                 if (dev) {
212                         res = find_resource(dev, PCI_BASE_ADDRESS_0);
213                         if (res) {
214                                 smp_write_ioapic(mc, apicid_8131_1, 0x11, res->base);
215                         }
216                 }
217                 /* 8131 apic 4 */
218                 dev = dev_find_slot(bus_chain_0, PCI_DEVFN(0x02,1));
219                 if (dev) {
220                         res = find_resource(dev, PCI_BASE_ADDRESS_0);
221                         if (res) {
222                                 smp_write_ioapic(mc, apicid_8131_2, 0x11, res->base);
223                         }
224                 }
225         }
226
227         mptable_add_isa_interrupts(mc, bus_isa, apicid_8111, 0);
228
229         /* Standard local interrupt assignments */
230         smp_write_lintsrc(mc, mp_ExtINT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT,
231                 bus_isa, 0x00, MP_APIC_ALL, 0x00);
232         smp_write_lintsrc(mc, mp_NMI, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT,
233                 bus_isa, 0x00, MP_APIC_ALL, 0x01);
234
235         /* PCI Ints:         Type    Trigger                Polarity                 Bus ID      PCIDEVNUM|IRQ  APIC ID PIN# */
236         /* On board nics */
237         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8131_1, (0x03<<2)|0, apicid_8111, 0x13);
238         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8131_1, (0x04<<2)|0, apicid_8111, 0x13);
239         /* On board SATA */
240         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8131_1, (0x05<<2)|0, apicid_8111, 0x11);
241
242         /* PCI Slot 1 */
243         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8131_2, (0x01<<2)|0, apicid_8111, 0x11);
244         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8131_2, (0x01<<2)|1, apicid_8111, 0x12);
245         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8131_2, (0x01<<2)|2, apicid_8111, 0x13);
246         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8131_2, (0x01<<2)|3, apicid_8111, 0x10);
247
248         /* PCI Slot 2 */
249         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8131_2, (0x02<<2)|0, apicid_8111, 0x12);
250         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8131_2, (0x02<<2)|1, apicid_8111, 0x13);
251         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8131_2, (0x02<<2)|2, apicid_8111, 0x10);
252         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8131_2, (0x02<<2)|3, apicid_8111, 0x11);
253
254         /* PCI Slot 3 */
255         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8131_1, (0x01<<2)|0, apicid_8111, 0x11);
256         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8131_1, (0x01<<2)|1, apicid_8111, 0x12);
257         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8131_1, (0x01<<2)|2, apicid_8111, 0x13);
258         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8131_1, (0x01<<2)|3, apicid_8111, 0x10);
259
260         /* PCI Slot 4 */
261         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8131_1, (0x02<<2)|0, apicid_8111, 0x12);
262         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8131_1, (0x02<<2)|1, apicid_8111, 0x13);
263         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8131_1, (0x02<<2)|2, apicid_8111, 0x10);
264         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8131_1, (0x02<<2)|3, apicid_8111, 0x11);
265
266         /* PCI Slot 5 */
267         // FIXME get the irqs right, it's just hacked to work for now
268         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8111_1, (0x05<<2)|0, apicid_8111, 0x11);
269         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8111_1, (0x05<<2)|1, apicid_8111, 0x12);
270         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8111_1, (0x05<<2)|2, apicid_8111, 0x13);
271         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8111_1, (0x05<<2)|3, apicid_8111, 0x10);
272
273         /* PCI Slot 6 */
274         // FIXME get the irqs right, it's just hacked to work for now
275         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8111_1, (0x04<<2)|0, apicid_8111, 0x10);
276         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8111_1, (0x04<<2)|1, apicid_8111, 0x11);
277         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8111_1, (0x04<<2)|2, apicid_8111, 0x12);
278         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_DEFAULT|MP_IRQ_POLARITY_DEFAULT, bus_8111_1, (0x04<<2)|3, apicid_8111, 0x13);
279
280         /* There is no extension information... */
281
282         /* Compute the checksums */
283         mc->mpe_checksum = smp_compute_checksum(smp_next_mpc_entry(mc), mc->mpe_length);
284         mc->mpc_checksum = smp_compute_checksum(mc, mc->mpc_length);
285         printk(BIOS_DEBUG, "Wrote the mp table end at: %p - %p\n",
286                 mc, smp_next_mpe_entry(mc));
287         return smp_next_mpe_entry(mc);
288 }
289
290 static void reboot_if_hotswap(void)
291 {
292         /* Hack patch work around for hot swap enable 33mhz problem */
293         device_t dev;
294         uint32_t data;
295         unsigned long htic;
296         int reset;
297
298         unsigned bus_chain_0 = node_link_to_bus(0, 0);
299
300         reset = 0;
301         printk(BIOS_DEBUG, "Looking for bad PCIX MHz input\n");
302         dev = dev_find_slot(bus_chain_0, PCI_DEVFN(0x02,0));
303         if (!dev)
304                 printk(BIOS_DEBUG, "Couldn't find %02x:02.0 \n", bus_chain_0);
305         else {
306                 data = pci_read_config32(dev, 0xa0);
307                 if(!(((data>>16)&0x03)==0x03)) {
308                         reset=1;
309                         printk(BIOS_DEBUG, "Bad PCIX MHz - Reset\n");
310                 }
311         }
312         printk(BIOS_DEBUG, "Looking for bad Hot Swap Enable\n");
313         dev = dev_find_slot(bus_chain_0, PCI_DEVFN(0x01,0));
314         if (!dev)
315                 printk(BIOS_DEBUG, "Couldn't find %02x:01.0 \n", bus_chain_0);
316         else {
317                 data = pci_read_config32(dev, 0x48);
318                 if(data & 0x0c) {
319                         reset=1;
320                         printk(BIOS_DEBUG, "Bad Hot Swap start - Reset\n");
321                 }
322         }
323         if(reset) {
324                 /* enable cf9 */
325                 dev = dev_find_slot(node_link_to_bus(0, 0), PCI_DEVFN(0x04,3));
326                 pci_write_config8(dev, 0x41, 0xf1);
327                 /* reset */
328                 dev = dev_find_slot(0, PCI_DEVFN(0x18,0));
329                 htic = pci_read_config32(dev, HT_INIT_CONTROL);
330                 htic &= ~HTIC_BIOSR_Detect;
331                 pci_write_config32(dev, HT_INIT_CONTROL, htic);
332                 outb(0x0e, 0x0cf9);
333         }
334         else {
335                 printk(BIOS_DEBUG, "OK 133MHz & Hot Swap is off\n");
336         }
337 }
338
339 unsigned long write_smp_table(unsigned long addr)
340 {
341         void *v;
342         reboot_if_hotswap();
343
344         v = smp_write_floating_table(addr);
345         return (unsigned long)smp_write_config_table(v);
346 }
347