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