Following patch converts the run-time SSDT patching via update_ssdt funtion to
[coreboot.git] / src / northbridge / amd / amdk8 / amdk8_acpi.c
1 /*============================================================================
2 Copyright 2005 ADVANCED MICRO DEVICES, INC. All Rights Reserved.
3 This software and any related documentation (the "Materials") are the
4 confidential proprietary information of AMD. Unless otherwise provided in a
5 software agreement specifically licensing the Materials, the Materials are
6 provided in confidence and may not be distributed, modified, or reproduced in
7 whole or in part by any means.
8 LIMITATION OF LIABILITY: THE MATERIALS ARE PROVIDED "AS IS" WITHOUT ANY
9 EXPRESS OR IMPLIED WARRANTY OF ANY KIND, INCLUDING BUT NOT LIMITED TO
10 WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT, TITLE, FITNESS FOR ANY
11 PARTICULAR PURPOSE, OR WARRANTIES ARISING FROM CONDUCT, COURSE OF DEALING, OR
12 USAGE OF TRADE. IN NO EVENT SHALL AMD OR ITS LICENSORS BE LIABLE FOR ANY
13 DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS,
14 BUSINESS INTERRUPTION, OR LOSS OF INFORMATION) ARISING OUT OF THE USE OF OR
15 INABILITY TO USE THE MATERIALS, EVEN IF AMD HAS BEEN ADVISED OF THE
16 POSSIBILITY OF SUCH DAMAGES. BECAUSE SOME JURISDICTIONS PROHIBIT THE EXCLUSION
17 OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES, THE ABOVE
18 LIMITATION MAY NOT APPLY TO YOU.
19 AMD does not assume any responsibility for any errors which may appear in the
20 Materials nor any responsibility to support or update the Materials. AMD
21 retains the right to modify the Materials at any time, without notice, and is
22 not obligated to provide such modified Materials to you.
23 NO SUPPORT OBLIGATION: AMD is not obligated to furnish, support, or make any
24 further information, software, technical information, know-how, or show-how
25 available to you.
26 U.S. GOVERNMENT RESTRICTED RIGHTS: The Materials are provided with "RESTRICTED
27 RIGHTS." Use, duplication, or disclosure by the Government is subject to the
28 restrictions as set forth in FAR 52.227-14 and DFAR 252.227-7013, et seq., or
29 its successor. Use of the Materials by the Government constitutes
30 acknowledgement of AMD's proprietary rights in them.
31 ============================================================================*/
32 // 2005.9 serengeti support
33 // by yhlu
34 //
35
36 /*
37  * 2005.9 yhlu add madt lapic creat dynamically and SRAT related
38  */
39
40 #include <console/console.h>
41 #include <string.h>
42 #include <arch/acpi.h>
43 #include <device/pci.h>
44 #include <cpu/x86/msr.h>
45 #include <cpu/amd/mtrr.h>
46 #include <cpu/amd/amdk8_sysconf.h>
47
48 //it seems some functions can be moved arch/i386/boot/acpi.c
49
50 unsigned long acpi_create_madt_lapics(unsigned long current)
51 {
52         device_t cpu;
53         int cpu_index = 0;
54
55         for(cpu = all_devices; cpu; cpu = cpu->next) {
56                 if ((cpu->path.type != DEVICE_PATH_APIC) ||
57                     (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER)) {
58                         continue;
59                 }
60                 if (!cpu->enabled) {
61                         continue;
62                 }
63                 current += acpi_create_madt_lapic((acpi_madt_lapic_t *)current, cpu_index, cpu->path.u.apic.apic_id);
64                 cpu_index++;
65         }
66         return current;
67 }
68
69 unsigned long acpi_create_madt_lapic_nmis(unsigned long current, u16 flags, u8 lint)
70 {
71         device_t cpu;
72         int cpu_index = 0;
73
74         for(cpu = all_devices; cpu; cpu = cpu->next) {
75                 if ((cpu->path.type != DEVICE_PATH_APIC) ||
76                     (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER)) {
77                         continue;
78                 }
79                 if (!cpu->enabled) {
80                         continue;
81                 }
82                 current += acpi_create_madt_lapic_nmi((acpi_madt_lapic_nmi_t *)current, cpu_index, flags, lint);
83                 cpu_index++;
84         }
85         return current;
86 }
87
88 unsigned long acpi_create_srat_lapics(unsigned long current)
89 {
90         device_t cpu;
91         int cpu_index = 0;
92
93         for(cpu = all_devices; cpu; cpu = cpu->next) {
94                 if ((cpu->path.type != DEVICE_PATH_APIC) ||
95                     (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER)) {
96                         continue;
97                 }
98                 if (!cpu->enabled) {
99                         continue;
100                 }
101                 printk_debug("SRAT: lapic cpu_index=%02x, node_id=%02x, apic_id=%02x\n", cpu_index, cpu->path.u.apic.node_id, cpu->path.u.apic.apic_id);
102                 current += acpi_create_srat_lapic((acpi_srat_lapic_t *)current, cpu->path.u.apic.node_id, cpu->path.u.apic.apic_id);
103                 cpu_index++;
104         }
105         return current;
106 }
107
108 static unsigned long resk(uint64_t value)
109 {
110         unsigned long resultk;
111         if (value < (1ULL << 42)) {
112                 resultk = value >> 10;
113         } else {
114                 resultk = 0xffffffff;
115         }
116         return resultk;
117 }
118
119 struct acpi_srat_mem_state {
120         unsigned long current;
121 };
122
123 void set_srat_mem(void *gp, struct device *dev, struct resource *res)
124 {
125         struct acpi_srat_mem_state *state = gp;
126         unsigned long basek, sizek;
127         basek = resk(res->base);
128         sizek = resk(res->size);
129
130         printk_debug("set_srat_mem: dev %s, res->index=%04x startk=%08x, sizek=%08x\n",
131                      dev_path(dev), res->index, basek, sizek);
132         /*
133          * 0-640K must be on node 0
134          * next range is from 1M---
135          * So will cut off before 1M in the mem range
136          */
137         if((basek+sizek)<1024) return;
138
139         if(basek<1024) {
140                 sizek -= 1024 - basek;
141                 basek = 1024;
142         }
143
144         // need to figure out NV
145         state->current += acpi_create_srat_mem((acpi_srat_mem_t *)state->current, (res->index & 0xf), basek, sizek, 1);
146 }
147
148 unsigned long acpi_fill_srat(unsigned long current)
149 {
150         struct acpi_srat_mem_state srat_mem_state;
151
152         /* create all subtables for processors */
153         current = acpi_create_srat_lapics(current);
154
155         /* create all subteble for memory range */
156
157         /* 0-640K must be on node 0 */
158         current += acpi_create_srat_mem((acpi_srat_mem_t *)current, 0, 0, 640, 1);//enable
159
160         srat_mem_state.current = current;
161         search_global_resources(
162                 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
163                 set_srat_mem, &srat_mem_state);
164
165         current = srat_mem_state.current;
166         return current;
167 }
168
169 unsigned long acpi_fill_slit(unsigned long current)
170 {
171         /* need to find out the node num at first */
172         /* fill the first 8 byte with that num */
173         /* fill the next num*num byte with distance, local is 10, 1 hop mean 20, and 2 hop with 30.... */
174
175         /* because We has assume that we know the topology of the HT connection, So we can have set if we know the node_num */
176         static u8 hops_8[] = {   0, 1, 1, 2, 2, 3, 3, 4,
177                                       1, 0, 2, 1, 3, 2, 4, 3,
178                                       1, 2, 0, 1, 1, 2, 2, 3,
179                                       2, 1, 1, 0, 2, 1, 3, 2,
180                                       2, 3, 1, 2, 0, 1, 1, 2,
181                                       3, 2, 2, 1, 1, 0, 2, 1,
182                                       3, 4, 2, 3, 1, 2, 0, 1,
183                                       4, 4, 3, 2, 2, 1, 1, 0 };
184
185 //      u8 outer_node[8];
186
187         u8 *p = (u8 *)current;
188         int nodes = sysconf.nodes;
189         int i,j;
190         memset(p, 0, 8+nodes*nodes);
191 //      memset((u8 *)outer_node, 0, 8);
192         *p = (u8) nodes;
193         p += 8;
194
195 #if 0
196         for(i=0;i<sysconf.hc_possible_num;i++) {
197                 if((sysconf.pci1234[i]&1) !=1 ) continue;
198                 outer_node[(sysconf.pci1234[i] >> 4) & 0xf] = 1; // mark the outer node
199         }
200 #endif
201
202         for(i=0;i<nodes;i++) {
203                 for(j=0;j<nodes; j++) {
204                         if(i==j) {
205                                 p[i*nodes+j] = 10;
206                         } else {
207 #if 0
208                                 int k;
209                                 u8 latency_factor = 0;
210                                 int k_start, k_end;
211                                 if(i<j) {
212                                         k_start = i;
213                                         k_end = j;
214                                 } else {
215                                         k_start = j;
216                                         k_end = i;
217                                 }
218                                 for(k=k_start;k<=k_end; k++) {
219                                         if(outer_node[k]) {
220                                                 latency_factor = 1;
221                                                 break;
222                                         }
223                                 }
224                                 p[i*nodes+j] = hops_8[i*nodes+j] * 2 + latency_factor + 10;
225 #else
226                                 p[i*nodes+j] = hops_8[i*nodes+j] * 2 + 10;
227 #endif
228
229                         }
230                 }
231         }
232
233         current += 8+nodes*nodes;
234
235         return current;
236 }
237
238 static int k8acpi_write_HT(void) {
239         device_t dev;
240         uint32_t dword;
241         int len, lenp, i;
242
243         len = acpigen_write_name("HCLK");
244         lenp = acpigen_write_package(HC_POSSIBLE_NUM);
245
246         for(i=0;i<sysconf.hc_possible_num;i++) {
247                 lenp += acpigen_write_dword(sysconf.pci1234[i]);
248         }
249         for(i=sysconf.hc_possible_num; i<HC_POSSIBLE_NUM; i++) { // in case we set array size to other than 8
250                 lenp += acpigen_write_dword(0x0);
251         }
252
253         acpigen_patch_len(lenp - 1);
254         len += lenp;
255
256         len += acpigen_write_name("HCDN");
257         lenp = acpigen_write_package(HC_POSSIBLE_NUM);
258
259         for(i=0;i<sysconf.hc_possible_num;i++) {
260                 lenp += acpigen_write_dword(sysconf.hcdn[i]);
261         }
262         for(i=sysconf.hc_possible_num; i<HC_POSSIBLE_NUM; i++) { // in case we set array size to other than 8
263                 lenp += acpigen_write_dword(0x20202020);
264         }
265         acpigen_patch_len(lenp - 1);
266         len += lenp;
267
268         return len;
269 }
270
271 static int k8acpi_write_pci_data(int dlen, char *name, int offset) {
272         device_t dev;
273         uint32_t dword;
274         int len, lenp, i;
275
276         dev = dev_find_slot(0, PCI_DEVFN(0x18, 1));
277
278         len = acpigen_write_name(name);
279         lenp = acpigen_write_package(dlen);
280         for(i=0; i<dlen; i++) {
281                 dword = pci_read_config32(dev, offset+i*4);
282                 lenp += acpigen_write_dword(dword);
283         }
284         // minus the opcode
285         acpigen_patch_len(lenp - 1);
286         return len + lenp;
287 }
288
289 int k8acpi_write_vars(void)
290 {
291         int lens;
292         msr_t msr;
293         char pscope[] = "\\._SB_PCI0";
294
295         lens = acpigen_write_scope(pscope);
296         lens += k8acpi_write_pci_data(4, "BUSN", 0xe0);
297         lens += k8acpi_write_pci_data(8, "PCIO", 0xc0);
298         lens += k8acpi_write_pci_data(16, "MMIO", 0x80);
299         lens += acpigen_write_name_byte("SBLK", sysconf.sblk);
300         lens += acpigen_write_name_byte("CBST",
301             ((sysconf.pci1234[0] >> 12) & 0xff) ? 0xf : 0x0);
302         lens += acpigen_write_name_dword("SBDN", sysconf.sbdn);
303         msr = rdmsr(TOP_MEM);
304         lens += acpigen_write_name_dword("TOM1", msr.lo);
305
306         lens += k8acpi_write_HT();
307         //minus opcode
308         acpigen_patch_len(lens - 1);
309         return lens;
310 }