Remove warnings from compilation of the s2892 with and without CBFS.
[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 <arch/acpigen.h>
44 #include <device/pci.h>
45 #include <cpu/x86/msr.h>
46 #include <cpu/amd/mtrr.h>
47 #include <cpu/amd/amdk8_sysconf.h>
48
49 //it seems some functions can be moved arch/i386/boot/acpi.c
50
51 unsigned long acpi_create_madt_lapic_nmis(unsigned long current, u16 flags, u8 lint)
52 {
53         device_t cpu;
54         int cpu_index = 0;
55
56         for(cpu = all_devices; cpu; cpu = cpu->next) {
57                 if ((cpu->path.type != DEVICE_PATH_APIC) ||
58                     (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER)) {
59                         continue;
60                 }
61                 if (!cpu->enabled) {
62                         continue;
63                 }
64                 current += acpi_create_madt_lapic_nmi((acpi_madt_lapic_nmi_t *)current, cpu_index, flags, lint);
65                 cpu_index++;
66         }
67         return current;
68 }
69
70 unsigned long acpi_create_srat_lapics(unsigned long current)
71 {
72         device_t cpu;
73         int cpu_index = 0;
74
75         for(cpu = all_devices; cpu; cpu = cpu->next) {
76                 if ((cpu->path.type != DEVICE_PATH_APIC) ||
77                     (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER)) {
78                         continue;
79                 }
80                 if (!cpu->enabled) {
81                         continue;
82                 }
83                 printk_debug("SRAT: lapic cpu_index=%02x, node_id=%02x, apic_id=%02x\n", cpu_index, cpu->path.apic.node_id, cpu->path.apic.apic_id);
84                 current += acpi_create_srat_lapic((acpi_srat_lapic_t *)current, cpu->path.apic.node_id, cpu->path.apic.apic_id);
85                 cpu_index++;
86         }
87         return current;
88 }
89
90 static unsigned long resk(uint64_t value)
91 {
92         unsigned long resultk;
93         if (value < (1ULL << 42)) {
94                 resultk = value >> 10;
95         } else {
96                 resultk = 0xffffffff;
97         }
98         return resultk;
99 }
100
101 struct acpi_srat_mem_state {
102         unsigned long current;
103 };
104
105 void set_srat_mem(void *gp, struct device *dev, struct resource *res)
106 {
107         struct acpi_srat_mem_state *state = gp;
108         unsigned long basek, sizek;
109         basek = resk(res->base);
110         sizek = resk(res->size);
111
112         printk_debug("set_srat_mem: dev %s, res->index=%04lx startk=%08lx, sizek=%08lx\n",
113                      dev_path(dev), res->index, basek, sizek);
114         /*
115          * 0-640K must be on node 0
116          * next range is from 1M---
117          * So will cut off before 1M in the mem range
118          */
119         if((basek+sizek)<1024) return;
120
121         if(basek<1024) {
122                 sizek -= 1024 - basek;
123                 basek = 1024;
124         }
125
126         // need to figure out NV
127         state->current += acpi_create_srat_mem((acpi_srat_mem_t *)state->current, (res->index & 0xf), basek, sizek, 1);
128 }
129
130 unsigned long acpi_fill_srat(unsigned long current)
131 {
132         struct acpi_srat_mem_state srat_mem_state;
133
134         /* create all subtables for processors */
135         current = acpi_create_srat_lapics(current);
136
137         /* create all subteble for memory range */
138
139         /* 0-640K must be on node 0 */
140         current += acpi_create_srat_mem((acpi_srat_mem_t *)current, 0, 0, 640, 1);//enable
141
142         srat_mem_state.current = current;
143         search_global_resources(
144                 IORESOURCE_MEM | IORESOURCE_CACHEABLE, IORESOURCE_MEM | IORESOURCE_CACHEABLE,
145                 set_srat_mem, &srat_mem_state);
146
147         current = srat_mem_state.current;
148         return current;
149 }
150
151 unsigned long acpi_fill_slit(unsigned long current)
152 {
153         /* need to find out the node num at first */
154         /* fill the first 8 byte with that num */
155         /* fill the next num*num byte with distance, local is 10, 1 hop mean 20, and 2 hop with 30.... */
156
157         /* because We has assume that we know the topology of the HT connection, So we can have set if we know the node_num */
158         static u8 hops_8[] = {   0, 1, 1, 2, 2, 3, 3, 4,
159                                       1, 0, 2, 1, 3, 2, 4, 3,
160                                       1, 2, 0, 1, 1, 2, 2, 3,
161                                       2, 1, 1, 0, 2, 1, 3, 2,
162                                       2, 3, 1, 2, 0, 1, 1, 2,
163                                       3, 2, 2, 1, 1, 0, 2, 1,
164                                       3, 4, 2, 3, 1, 2, 0, 1,
165                                       4, 4, 3, 2, 2, 1, 1, 0 };
166
167 //      u8 outer_node[8];
168
169         u8 *p = (u8 *)current;
170         int nodes = sysconf.nodes;
171         int i,j;
172         memset(p, 0, 8+nodes*nodes);
173 //      memset((u8 *)outer_node, 0, 8);
174         *p = (u8) nodes;
175         p += 8;
176
177 #if 0
178         for(i=0;i<sysconf.hc_possible_num;i++) {
179                 if((sysconf.pci1234[i]&1) !=1 ) continue;
180                 outer_node[(sysconf.pci1234[i] >> 4) & 0xf] = 1; // mark the outer node
181         }
182 #endif
183
184         for(i=0;i<nodes;i++) {
185                 for(j=0;j<nodes; j++) {
186                         if(i==j) {
187                                 p[i*nodes+j] = 10;
188                         } else {
189 #if 0
190                                 int k;
191                                 u8 latency_factor = 0;
192                                 int k_start, k_end;
193                                 if(i<j) {
194                                         k_start = i;
195                                         k_end = j;
196                                 } else {
197                                         k_start = j;
198                                         k_end = i;
199                                 }
200                                 for(k=k_start;k<=k_end; k++) {
201                                         if(outer_node[k]) {
202                                                 latency_factor = 1;
203                                                 break;
204                                         }
205                                 }
206                                 p[i*nodes+j] = hops_8[i*nodes+j] * 2 + latency_factor + 10;
207 #else
208                                 p[i*nodes+j] = hops_8[i*nodes+j] * 2 + 10;
209 #endif
210
211                         }
212                 }
213         }
214
215         current += 8+nodes*nodes;
216
217         return current;
218 }
219
220 static int k8acpi_write_HT(void) {
221         int len, lenp, i;
222
223         len = acpigen_write_name("HCLK");
224         lenp = acpigen_write_package(HC_POSSIBLE_NUM);
225
226         for(i=0;i<sysconf.hc_possible_num;i++) {
227                 lenp += acpigen_write_dword(sysconf.pci1234[i]);
228         }
229         for(i=sysconf.hc_possible_num; i<HC_POSSIBLE_NUM; i++) { // in case we set array size to other than 8
230                 lenp += acpigen_write_dword(0x0);
231         }
232
233         acpigen_patch_len(lenp - 1);
234         len += lenp;
235
236         len += acpigen_write_name("HCDN");
237         lenp = acpigen_write_package(HC_POSSIBLE_NUM);
238
239         for(i=0;i<sysconf.hc_possible_num;i++) {
240                 lenp += acpigen_write_dword(sysconf.hcdn[i]);
241         }
242         for(i=sysconf.hc_possible_num; i<HC_POSSIBLE_NUM; i++) { // in case we set array size to other than 8
243                 lenp += acpigen_write_dword(0x20202020);
244         }
245         acpigen_patch_len(lenp - 1);
246         len += lenp;
247
248         return len;
249 }
250
251 static int k8acpi_write_pci_data(int dlen, char *name, int offset) {
252         device_t dev;
253         uint32_t dword;
254         int len, lenp, i;
255
256         dev = dev_find_slot(0, PCI_DEVFN(0x18, 1));
257
258         len = acpigen_write_name(name);
259         lenp = acpigen_write_package(dlen);
260         for(i=0; i<dlen; i++) {
261                 dword = pci_read_config32(dev, offset+i*4);
262                 lenp += acpigen_write_dword(dword);
263         }
264         // minus the opcode
265         acpigen_patch_len(lenp - 1);
266         return len + lenp;
267 }
268
269 int k8acpi_write_vars(void)
270 {
271         int lens;
272         msr_t msr;
273         char pscope[] = "\\._SB_PCI0";
274
275         lens = acpigen_write_scope(pscope);
276         lens += k8acpi_write_pci_data(4, "BUSN", 0xe0);
277         lens += k8acpi_write_pci_data(8, "PCIO", 0xc0);
278         lens += k8acpi_write_pci_data(16, "MMIO", 0x80);
279         lens += acpigen_write_name_byte("SBLK", sysconf.sblk);
280         lens += acpigen_write_name_byte("CBST",
281             ((sysconf.pci1234[0] >> 12) & 0xff) ? 0xf : 0x0);
282         lens += acpigen_write_name_dword("SBDN", sysconf.sbdn);
283         msr = rdmsr(TOP_MEM);
284         lens += acpigen_write_name_dword("TOM1", msr.lo);
285         msr = rdmsr(TOP_MEM2);
286         lens += acpigen_write_name_qword("TOM2", (((uint64_t) msr.hi) << 32) | msr.lo);
287
288         lens += k8acpi_write_HT();
289         //minus opcode
290         acpigen_patch_len(lens - 1);
291         return lens;
292 }