We define IO_APIC_ADDR in <arch/ioapic.h>, let's use it.
[coreboot.git] / src / mainboard / tyan / s4882 / 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 #if CONFIG_LOGICAL_CPUS==1
8 #include <cpu/amd/multicore.h>
9 #endif
10
11
12 static unsigned node_link_to_bus(unsigned node, unsigned link)
13 {
14         device_t dev;
15         unsigned reg;
16
17         dev = dev_find_slot(0, PCI_DEVFN(0x18, 1));
18         if (!dev) {
19                 return 0;
20         }
21         for(reg = 0xE0; reg < 0xF0; reg += 0x04) {
22                 uint32_t config_map;
23                 unsigned dst_node;
24                 unsigned dst_link;
25                 unsigned bus_base;
26                 config_map = pci_read_config32(dev, reg);
27                 if ((config_map & 3) != 3) {
28                         continue;
29                 }
30                 dst_node = (config_map >> 4) & 7;
31                 dst_link = (config_map >> 8) & 3;
32                 bus_base = (config_map >> 16) & 0xff;
33 #if 0
34                 printk(BIOS_DEBUG, "node.link=bus: %d.%d=%d 0x%2x->0x%08x\n",
35                         dst_node, dst_link, bus_base,
36                         reg, config_map);
37 #endif
38                 if ((dst_node == node) && (dst_link == link))
39                 {
40                         return bus_base;
41                 }
42         }
43         return 0;
44 }
45
46 static void *smp_write_config_table(void *v)
47 {
48         static const char sig[4] = "PCMP";
49         static const char oem[8] = "COREBOOT";
50         static const char productid[12] = "S4882       ";
51         struct mp_config_table *mc;
52
53         int bus_isa;
54         unsigned char bus_chain_0;
55         unsigned char bus_8131_1;
56         unsigned char bus_8131_2;
57         unsigned char bus_8111_1;
58         unsigned apicid_base;
59         unsigned apicid_8111;
60         unsigned apicid_8131_1;
61         unsigned apicid_8131_2;
62
63         mc = (void *)(((char *)v) + SMP_FLOATING_TABLE_LEN);
64         memset(mc, 0, sizeof(*mc));
65
66         memcpy(mc->mpc_signature, sig, sizeof(sig));
67         mc->mpc_length = sizeof(*mc); /* initially just the header */
68         mc->mpc_spec = 0x04;
69         mc->mpc_checksum = 0; /* not yet computed */
70         memcpy(mc->mpc_oem, oem, sizeof(oem));
71         memcpy(mc->mpc_productid, productid, sizeof(productid));
72         mc->mpc_oemptr = 0;
73         mc->mpc_oemsize = 0;
74         mc->mpc_entry_count = 0; /* No entries yet... */
75         mc->mpc_lapic = LAPIC_ADDR;
76         mc->mpe_length = 0;
77         mc->mpe_checksum = 0;
78         mc->reserved = 0;
79
80         smp_write_processors(mc);
81
82
83         {
84                 device_t dev;
85
86                 /* HT chain 0 */
87                 bus_chain_0 = node_link_to_bus(0, 1);
88                 if (bus_chain_0 == 0) {
89                         printk(BIOS_DEBUG, "ERROR - cound not find bus for node 0 chain 0, using defaults\n");
90                         bus_chain_0 = 1;
91                 }
92
93                 /* 8111 */
94                 dev = dev_find_slot(bus_chain_0, PCI_DEVFN(0x03,0));
95                 if (dev) {
96                         bus_8111_1 = pci_read_config8(dev, PCI_SECONDARY_BUS);
97                 }
98                 else {
99                         printk(BIOS_DEBUG, "ERROR - could not find PCI 1:03.0, using defaults\n");
100
101                         bus_8111_1 = 4;
102                 }
103                 /* 8131-1 */
104                 dev = dev_find_slot(bus_chain_0, PCI_DEVFN(0x01,0));
105                 if (dev) {
106                         bus_8131_1 = pci_read_config8(dev, PCI_SECONDARY_BUS);
107
108                 }
109                 else {
110                         printk(BIOS_DEBUG, "ERROR - could not find PCI 1:01.0, using defaults\n");
111
112                         bus_8131_1 = 2;
113                 }
114                 /* 8131-2 */
115                 dev = dev_find_slot(bus_chain_0, PCI_DEVFN(0x02,0));
116                 if (dev) {
117                         bus_8131_2 = pci_read_config8(dev, PCI_SECONDARY_BUS);
118
119                 }
120                 else {
121                         printk(BIOS_DEBUG, "ERROR - could not find PCI 1:02.0, using defaults\n");
122
123                         bus_8131_2 = 3;
124                 }
125         }
126
127 /*Bus:          Bus ID  Type*/
128         mptable_write_buses(mc, NULL, &bus_isa);
129
130 /*I/O APICs:    APIC ID Version State           Address*/
131 #if CONFIG_LOGICAL_CPUS==1
132         apicid_base = get_apicid_base(3);
133 #else
134         apicid_base = CONFIG_MAX_PHYSICAL_CPUS;
135 #endif
136         apicid_8111 = apicid_base+0;
137         apicid_8131_1 = apicid_base+1;
138         apicid_8131_2 = apicid_base+2;
139         smp_write_ioapic(mc, apicid_8111, 0x11, IO_APIC_ADDR);
140         {
141                 device_t dev;
142                 struct resource *res;
143                 dev = dev_find_slot(bus_chain_0, PCI_DEVFN(0x1,1));
144                 if (dev) {
145                         res = find_resource(dev, PCI_BASE_ADDRESS_0);
146                         if (res) {
147                                 smp_write_ioapic(mc, apicid_8131_1, 0x11, res->base);
148                         }
149                 }
150                 dev = dev_find_slot(bus_chain_0, PCI_DEVFN(0x2,1));
151                 if (dev) {
152                         res = find_resource(dev, PCI_BASE_ADDRESS_0);
153                         if (res) {
154                                 smp_write_ioapic(mc, apicid_8131_2, 0x11, res->base);
155                         }
156                 }
157
158         }
159
160         mptable_add_isa_interrupts(mc, bus_isa, apicid_8111, 0);
161
162 /*I/O Ints:     Type    Polarity    Trigger     Bus ID   IRQ    APIC ID PIN# */
163         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_chain_0, (4<<2)|0, apicid_8111, 0x13);
164
165
166 //On Board AMD USB
167         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8111_1, (0<<2)|3, apicid_8111, 0x13);
168
169 //On Board Via USB 1.1 and 2
170         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8111_1, (3<<2)|0, apicid_8111, 0x11); //1.1
171         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8111_1, (3<<2)|1, apicid_8111, 0x10); //1.1
172         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8111_1, (3<<2)|2, apicid_8111, 0x12); //2
173
174 //Slot 5 PCI 32
175         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8111_1, (4<<2)|0, apicid_8111, 0x10);
176         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8111_1, (4<<2)|1, apicid_8111, 0x11);
177         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8111_1, (4<<2)|2, apicid_8111, 0x12); //
178         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8111_1, (4<<2)|3, apicid_8111, 0x13); //
179
180
181 //On Board SI Serial ATA
182         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8111_1, (5<<2)|0, apicid_8111, 0x13);
183 //On Board ATI Display Adapter
184         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8111_1, (6<<2)|0, apicid_8111, 0x12);
185
186
187 //Slot 4 PCIX 100/66
188         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8131_1, (3<<2)|0, apicid_8131_1, 0x3);
189         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8131_1, (3<<2)|1, apicid_8131_1, 0x0);
190         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8131_1, (3<<2)|2, apicid_8131_1, 0x1);//
191         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8131_1, (3<<2)|3, apicid_8131_1, 0x2);//
192
193 //Slot 3 PCIX 100/66
194         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8131_1, (2<<2)|0, apicid_8131_1, 0x2);
195         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8131_1, (2<<2)|1, apicid_8131_1, 0x3);//
196         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8131_1, (2<<2)|2, apicid_8131_1, 0x0);//
197         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8131_1, (2<<2)|3, apicid_8131_1, 0x1);//
198
199 //On Board LSI scsi and NIC
200         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8131_1, (4<<2)|0, apicid_8131_1, 0x0);
201         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8131_1, (4<<2)|1, apicid_8131_1, 0x1);
202         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8131_1, (9<<2)|0, apicid_8131_1, 0x0);
203         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8131_1, (9<<2)|1, apicid_8131_1, 0x1);
204
205 //Slot 2 PCI-X 133/100/66
206         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8131_2, (3<<2)|0, apicid_8131_2, 0x0);
207         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8131_2, (3<<2)|1, apicid_8131_2, 0x1);
208         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8131_2, (3<<2)|2, apicid_8131_2, 0x2); //
209         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8131_2, (3<<2)|3, apicid_8131_2, 0x3); //
210
211 //Slot 1 PCI-X 133/100/66
212         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8131_2, (1<<2)|0, apicid_8131_2, 0x1);
213         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8131_2, (1<<2)|1, apicid_8131_2, 0x2);
214         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8131_2, (1<<2)|2, apicid_8131_2, 0x3);//
215         smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_LEVEL|MP_IRQ_POLARITY_LOW, bus_8131_2, (1<<2)|3, apicid_8131_2, 0x0);//
216
217 /*Local Ints:   Type    Polarity    Trigger     Bus ID   IRQ    APIC ID PIN#*/
218         smp_write_intsrc(mc, mp_ExtINT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0x0, MP_APIC_ALL, 0x0);
219         smp_write_intsrc(mc, mp_NMI, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0x0, MP_APIC_ALL, 0x1);
220         /* There is no extension information... */
221
222         /* Compute the checksums */
223         mc->mpe_checksum = smp_compute_checksum(smp_next_mpc_entry(mc), mc->mpe_length);
224         mc->mpc_checksum = smp_compute_checksum(mc, mc->mpc_length);
225         printk(BIOS_DEBUG, "Wrote the mp table end at: %p - %p\n",
226                 mc, smp_next_mpe_entry(mc));
227         return smp_next_mpe_entry(mc);
228 }
229
230 unsigned long write_smp_table(unsigned long addr)
231 {
232         void *v;
233         v = smp_write_floating_table(addr);
234         return (unsigned long)smp_write_config_table(v);
235 }