blob: 531d65208ccf53d7acda532c847a0acc3b9bfb12 [file] [log] [blame]
Subrata Banik03e971c2017-03-07 14:02:23 +05301/*
2 * This file is part of the coreboot project.
3 *
Subrata Banik03e971c2017-03-07 14:02:23 +05304 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 */
15
16#include <commonlib/helpers.h>
17#include <cpu/x86/cache.h>
18#include <cpu/x86/cr.h>
Elyes HAOUAS419bfbc2018-10-01 08:47:51 +020019#include <cpu/x86/msr.h>
Subrata Banik03e971c2017-03-07 14:02:23 +053020#include <cpu/x86/mtrr.h>
21#include <cpu/x86/post_code.h>
22#include <rules.h>
23#include <intelblocks/msr.h>
24
25.global bootblock_pre_c_entry
26bootblock_pre_c_entry:
27
28 post_code(0x20)
29
Arthur Heymansc4772b92019-04-14 18:38:35 +020030 movl $no_reset, %esp /* return address */
31 jmp check_mtrr /* Check if CPU properly reset */
Subrata Banik03e971c2017-03-07 14:02:23 +053032
33no_reset:
34 post_code(0x21)
35
36 /* Clear/disable fixed MTRRs */
37 mov $fixed_mtrr_list_size, %ebx
38 xor %eax, %eax
39 xor %edx, %edx
40
41clear_fixed_mtrr:
42 add $-2, %ebx
43 movzwl fixed_mtrr_list(%ebx), %ecx
44 wrmsr
45 jnz clear_fixed_mtrr
46
47 post_code(0x22)
48
49 /* Figure put how many MTRRs we have, and clear them out */
50 mov $MTRR_CAP_MSR, %ecx
51 rdmsr
52 movzb %al, %ebx /* Number of variable MTRRs */
53 mov $MTRR_PHYS_BASE(0), %ecx
54 xor %eax, %eax
55 xor %edx, %edx
56
57clear_var_mtrr:
58 wrmsr
59 inc %ecx
60 wrmsr
61 inc %ecx
62 dec %ebx
63 jnz clear_var_mtrr
64
65 post_code(0x23)
66
67 /* Configure default memory type to uncacheable (UC) */
68 mov $MTRR_DEF_TYPE_MSR, %ecx
69 rdmsr
70 /* Clear enable bits and set default type to UC. */
71 and $~(MTRR_DEF_TYPE_MASK | MTRR_DEF_TYPE_EN | \
72 MTRR_DEF_TYPE_FIX_EN), %eax
73 wrmsr
74
75 /* Configure MTRR_PHYS_MASK_HIGH for proper addressing above 4GB
76 * based on the physical address size supported for this processor
77 * This is based on read from CPUID EAX = 080000008h, EAX bits [7:0]
78 *
79 * Examples:
80 * MTRR_PHYS_MASK_HIGH = 00000000Fh For 36 bit addressing
81 * MTRR_PHYS_MASK_HIGH = 0000000FFh For 40 bit addressing
82 */
83
Elyes HAOUAS05498a22018-05-28 16:26:43 +020084 movl $0x80000008, %eax /* Address sizes leaf */
Subrata Banik03e971c2017-03-07 14:02:23 +053085 cpuid
86 sub $32, %al
87 movzx %al, %eax
88 xorl %esi, %esi
89 bts %eax, %esi
90 dec %esi /* esi <- MTRR_PHYS_MASK_HIGH */
91
92 post_code(0x24)
93
94#if ((CONFIG_DCACHE_RAM_SIZE & (CONFIG_DCACHE_RAM_SIZE - 1)) == 0)
95 /* Configure CAR region as write-back (WB) */
96 mov $MTRR_PHYS_BASE(0), %ecx
97 mov $CONFIG_DCACHE_RAM_BASE, %eax
98 or $MTRR_TYPE_WRBACK, %eax
99 xor %edx,%edx
100 wrmsr
101
102 /* Configure the MTRR mask for the size region */
103 mov $MTRR_PHYS_MASK(0), %ecx
104 mov $CONFIG_DCACHE_RAM_SIZE, %eax /* size mask */
105 dec %eax
106 not %eax
107 or $MTRR_PHYS_MASK_VALID, %eax
108 movl %esi, %edx /* edx <- MTRR_PHYS_MASK_HIGH */
109 wrmsr
110#elif (CONFIG_DCACHE_RAM_SIZE == 768 * KiB) /* 768 KiB */
111 /* Configure CAR region as write-back (WB) */
112 mov $MTRR_PHYS_BASE(0), %ecx
113 mov $CONFIG_DCACHE_RAM_BASE, %eax
114 or $MTRR_TYPE_WRBACK, %eax
115 xor %edx,%edx
116 wrmsr
117
118 mov $MTRR_PHYS_MASK(0), %ecx
119 mov $(512 * KiB), %eax /* size mask */
120 dec %eax
121 not %eax
122 or $MTRR_PHYS_MASK_VALID, %eax
123 movl %esi, %edx /* edx <- MTRR_PHYS_MASK_HIGH */
124 wrmsr
125
126 mov $MTRR_PHYS_BASE(1), %ecx
127 mov $(CONFIG_DCACHE_RAM_BASE + 512 * KiB), %eax
128 or $MTRR_TYPE_WRBACK, %eax
129 xor %edx,%edx
130 wrmsr
131
132 mov $MTRR_PHYS_MASK(1), %ecx
133 mov $(256 * KiB), %eax /* size mask */
134 dec %eax
135 not %eax
136 or $MTRR_PHYS_MASK_VALID, %eax
137 movl %esi, %edx /* edx <- MTRR_PHYS_MASK_HIGH */
138 wrmsr
139#else
140#error "DCACHE_RAM_SIZE is not a power of 2 and setup code is missing"
141#endif
142 post_code(0x25)
143
144 /* Enable variable MTRRs */
145 mov $MTRR_DEF_TYPE_MSR, %ecx
146 rdmsr
147 or $MTRR_DEF_TYPE_EN, %eax
148 wrmsr
149
150 /* Enable caching */
151 mov %cr0, %eax
152 and $~(CR0_CD | CR0_NW), %eax
153 invd
154 mov %eax, %cr0
155
Julius Wernercd49cce2019-03-05 16:53:33 -0800156#if CONFIG(INTEL_CAR_NEM)
Subrata Banik03e971c2017-03-07 14:02:23 +0530157 jmp car_nem
Julius Wernercd49cce2019-03-05 16:53:33 -0800158#elif CONFIG(INTEL_CAR_CQOS)
Subrata Banik03e971c2017-03-07 14:02:23 +0530159 jmp car_cqos
Julius Wernercd49cce2019-03-05 16:53:33 -0800160#elif CONFIG(INTEL_CAR_NEM_ENHANCED)
Subrata Banik03e971c2017-03-07 14:02:23 +0530161 jmp car_nem_enhanced
162#else
163 jmp .halt_forever /* In case nothing has selected */
164#endif
165
166.global car_init_done
167car_init_done:
168
169 post_code(0x29)
170
171 /* Setup bootblock stack */
Arthur Heymansdf9cdcf2019-11-09 06:50:20 +0100172 mov $_ecar_stack, %esp
Subrata Banik03e971c2017-03-07 14:02:23 +0530173
Aaron Durbin028e18f2017-06-23 11:14:58 -0500174 /* Need to align stack to 16 bytes at call instruction. Account for
175 the two pushes below. */
176 andl $0xfffffff0, %esp
177 sub $8, %esp
178
Subrata Banik5885ffe2019-11-14 11:08:51 +0530179 /* push TSC value to stack */
Subrata Banik03e971c2017-03-07 14:02:23 +0530180 movd %mm2, %eax
181 pushl %eax /* tsc[63:32] */
182 movd %mm1, %eax
Elyes HAOUAS05498a22018-05-28 16:26:43 +0200183 pushl %eax /* tsc[31:0] */
Subrata Banik03e971c2017-03-07 14:02:23 +0530184
185before_carstage:
186 post_code(0x2A)
187
188 call bootblock_c_entry
189 /* Never reached */
190
191.halt_forever:
192 post_code(POST_DEAD_CODE)
193 hlt
194 jmp .halt_forever
195
196fixed_mtrr_list:
197 .word MTRR_FIX_64K_00000
198 .word MTRR_FIX_16K_80000
199 .word MTRR_FIX_16K_A0000
200 .word MTRR_FIX_4K_C0000
201 .word MTRR_FIX_4K_C8000
202 .word MTRR_FIX_4K_D0000
203 .word MTRR_FIX_4K_D8000
204 .word MTRR_FIX_4K_E0000
205 .word MTRR_FIX_4K_E8000
206 .word MTRR_FIX_4K_F0000
207 .word MTRR_FIX_4K_F8000
208fixed_mtrr_list_size = . - fixed_mtrr_list
209
Julius Wernercd49cce2019-03-05 16:53:33 -0800210#if CONFIG(INTEL_CAR_NEM)
Subrata Banik03e971c2017-03-07 14:02:23 +0530211.global car_nem
212car_nem:
213 /* Disable cache eviction (setup stage) */
214 mov $MSR_EVICT_CTL, %ecx
215 rdmsr
216 or $0x1, %eax
217 wrmsr
218
219 post_code(0x26)
220
221 /* Clear the cache memory region. This will also fill up the cache */
222 movl $CONFIG_DCACHE_RAM_BASE, %edi
223 movl $CONFIG_DCACHE_RAM_SIZE, %ecx
224 shr $0x02, %ecx
225 xor %eax, %eax
226 cld
227 rep stosl
228
229 post_code(0x27)
230
231 /* Disable cache eviction (run stage) */
232 mov $MSR_EVICT_CTL, %ecx
233 rdmsr
234 or $0x2, %eax
235 wrmsr
236
237 post_code(0x28)
238
239 jmp car_init_done
240
Julius Wernercd49cce2019-03-05 16:53:33 -0800241#elif CONFIG(INTEL_CAR_CQOS)
Subrata Banik03e971c2017-03-07 14:02:23 +0530242.global car_cqos
243car_cqos:
244 /*
Naresh G Solankif329f0c2017-09-27 14:21:18 +0530245 * Create CBM_LEN_MASK based on CBM_LEN
246 * Get CPUID.(EAX=10H, ECX=2H):EAX.CBM_LEN[bits 4:0]
247 */
248 mov $0x10, %eax
249 mov $0x2, %ecx
250 cpuid
251 and $0x1F, %eax
252 add $1, %al
253
254 mov $1, %ebx
255 mov %al, %cl
256 shl %cl, %ebx
257 sub $1, %ebx
258
259 /* Store the CBM_LEN_MASK in mm3 for later use. */
260 movd %ebx, %mm3
261
262 /*
Subrata Banik03e971c2017-03-07 14:02:23 +0530263 * Disable both L1 and L2 prefetcher. For yet-to-understood reason,
264 * prefetchers slow down filling cache with rep stos in CQOS mode.
265 */
266 mov $MSR_PREFETCH_CTL, %ecx
267 rdmsr
268 or $(PREFETCH_L1_DISABLE | PREFETCH_L2_DISABLE), %eax
269 wrmsr
270
271#if (CONFIG_DCACHE_RAM_SIZE == CONFIG_L2_CACHE_SIZE)
272/*
273 * If CAR size is set to full L2 size, mask is calculated as all-zeros.
274 * This is not supported by the CPU/uCode.
275 */
276#error "CQOS CAR may not use whole L2 cache area"
277#endif
278
279 /* Calculate how many bits to be used for CAR */
280 xor %edx, %edx
281 mov $CONFIG_DCACHE_RAM_SIZE, %eax /* dividend */
282 mov $CONFIG_CACHE_QOS_SIZE_PER_BIT, %ecx /* divisor */
283 div %ecx /* result is in eax */
284 mov %eax, %ecx /* save to ecx */
285 mov $1, %ebx
286 shl %cl, %ebx
287 sub $1, %ebx /* resulting mask is is in ebx */
288
289 /* Set this mask for initial cache fill */
290 mov $MSR_L2_QOS_MASK(0), %ecx
291 rdmsr
Naresh G Solankif329f0c2017-09-27 14:21:18 +0530292 mov %ebx, %eax
Subrata Banik03e971c2017-03-07 14:02:23 +0530293 wrmsr
294
295 /* Set CLOS selector to 0 */
Elyes HAOUAS419bfbc2018-10-01 08:47:51 +0200296 mov $IA32_PQR_ASSOC, %ecx
Subrata Banik03e971c2017-03-07 14:02:23 +0530297 rdmsr
298 and $~IA32_PQR_ASSOC_MASK, %edx /* select mask 0 */
299 wrmsr
300
301 /* We will need to block CAR region from evicts */
302 mov $MSR_L2_QOS_MASK(1), %ecx
303 rdmsr
304 /* Invert bits that are to be used for cache */
Naresh G Solankif329f0c2017-09-27 14:21:18 +0530305 mov %ebx, %eax
306 xor $~0, %eax /* invert 32 bits */
307
308 /*
309 * Use CBM_LEN_MASK stored in mm3 to set bits based on Capacity Bit
310 * Mask Length.
311 */
312 movd %mm3, %ebx
313 and %ebx, %eax
Subrata Banik03e971c2017-03-07 14:02:23 +0530314 wrmsr
315
316 post_code(0x26)
317
318 /* Clear the cache memory region. This will also fill up the cache */
319 movl $CONFIG_DCACHE_RAM_BASE, %edi
320 movl $CONFIG_DCACHE_RAM_SIZE, %ecx
321 shr $0x02, %ecx
322 xor %eax, %eax
323 cld
324 rep stosl
325
326 post_code(0x27)
327
328 /* Cache is populated. Use mask 1 that will block evicts */
Elyes HAOUAS419bfbc2018-10-01 08:47:51 +0200329 mov $IA32_PQR_ASSOC, %ecx
Subrata Banik03e971c2017-03-07 14:02:23 +0530330 rdmsr
331 and $~IA32_PQR_ASSOC_MASK, %edx /* clear index bits first */
332 or $1, %edx /* select mask 1 */
333 wrmsr
334
335 /* Enable prefetchers */
336 mov $MSR_PREFETCH_CTL, %ecx
337 rdmsr
338 and $~(PREFETCH_L1_DISABLE | PREFETCH_L2_DISABLE), %eax
339 wrmsr
340
341 post_code(0x28)
342
343 jmp car_init_done
344
Julius Wernercd49cce2019-03-05 16:53:33 -0800345#elif CONFIG(INTEL_CAR_NEM_ENHANCED)
Subrata Banik03e971c2017-03-07 14:02:23 +0530346.global car_nem_enhanced
347car_nem_enhanced:
348 /* Disable cache eviction (setup stage) */
349 mov $MSR_EVICT_CTL, %ecx
350 rdmsr
351 or $0x1, %eax
352 wrmsr
353 post_code(0x26)
354
355 /* Create n-way set associativity of cache */
356 xorl %edi, %edi
357find_llc_subleaf:
358 movl %edi, %ecx
359 movl $0x04, %eax
360 cpuid
361 inc %edi
362 and $0xe0, %al /* EAX[7:5] = Cache Level */
363 cmp $0x60, %al /* Check to see if it is LLC */
364 jnz find_llc_subleaf
365
366 /*
Subrata Banik8adaffc2019-08-10 21:43:12 +0530367 * Set MSR 0xC91 IA32_L3_MASK_1 = 0xE/0xFE/0xFFE/0xFFFE
Subrata Banik03e971c2017-03-07 14:02:23 +0530368 * for 4/8/16 way of LLC
369 */
370 shr $22, %ebx
371 inc %ebx
372 /* Calculate n-way associativity of LLC */
373 mov %bl, %cl
374
375 /*
376 * Maximizing RO cacheability while locking in the CAR to a
377 * single way since that particular way won't be victim candidate
378 * for evictions.
Jonathan Neuschäfer5268b762018-02-12 12:24:25 +0100379 * This has been done after programming LLC_WAY_MASK_1 MSR
Subrata Banik03e971c2017-03-07 14:02:23 +0530380 * with desired LLC way as mentioned below.
381 *
382 * Hence create Code and Data Size as per request
383 * Code Size (RO) : Up to 16M
384 * Data Size (RW) : Up to 256K
385 */
386 movl $0x01, %eax
387 /*
388 * LLC Ways -> LLC_WAY_MASK_1:
389 * 4: 0x000E
390 * 8: 0x00FE
391 * 12: 0x0FFE
392 * 16: 0xFFFE
393 *
394 * These MSRs contain one bit per each way of LLC
395 * - If this bit is '0' - the way is protected from eviction
396 * - If this bit is '1' - the way is not protected from eviction
397 */
398 shl %cl, %eax
399 subl $0x02, %eax
Elyes HAOUAS419bfbc2018-10-01 08:47:51 +0200400 movl $IA32_L3_MASK_1, %ecx
Subrata Banik03e971c2017-03-07 14:02:23 +0530401 xorl %edx, %edx
402 wrmsr
403 /*
404 * Set MSR 0xC92 IA32_L3_MASK_2 = 0x1
405 *
406 * For SKL SOC, data size remains 256K consistently.
407 * Hence, creating 1-way associative cache for Data
408 */
Elyes HAOUAS419bfbc2018-10-01 08:47:51 +0200409 mov $IA32_L3_MASK_2, %ecx
Subrata Banik03e971c2017-03-07 14:02:23 +0530410 mov $0x01, %eax
411 xorl %edx, %edx
412 wrmsr
413 /*
Elyes HAOUAS419bfbc2018-10-01 08:47:51 +0200414 * Set IA32_PQR_ASSOC = 0x02
Subrata Banik03e971c2017-03-07 14:02:23 +0530415 *
416 * Possible values:
417 * 0: Default value, no way mask should be applied
418 * 1: Apply way mask 1 to LLC
419 * 2: Apply way mask 2 to LLC
420 * 3: Shouldn't be use in NEM Mode
421 */
Elyes HAOUAS419bfbc2018-10-01 08:47:51 +0200422 movl $IA32_PQR_ASSOC, %ecx
Subrata Banik03e971c2017-03-07 14:02:23 +0530423 movl $0x02, %eax
424 xorl %edx, %edx
425 wrmsr
426
427 movl $CONFIG_DCACHE_RAM_BASE, %edi
428 movl $CONFIG_DCACHE_RAM_SIZE, %ecx
429 shr $0x02, %ecx
430 xor %eax, %eax
431 cld
432 rep stosl
433 /*
Elyes HAOUAS419bfbc2018-10-01 08:47:51 +0200434 * Set IA32_PQR_ASSOC = 0x01
Subrata Banik03e971c2017-03-07 14:02:23 +0530435 * At this stage we apply LLC_WAY_MASK_1 to the cache.
436 * i.e. way 0 is protected from eviction.
437 */
Elyes HAOUAS419bfbc2018-10-01 08:47:51 +0200438 movl $IA32_PQR_ASSOC, %ecx
Subrata Banik03e971c2017-03-07 14:02:23 +0530439 movl $0x01, %eax
440 xorl %edx, %edx
441 wrmsr
442
443 post_code(0x27)
444 /*
445 * Enable No-Eviction Mode Run State by setting
446 * NO_EVICT_MODE MSR 2E0h bit [1] = '1'.
447 */
448
449 movl $MSR_EVICT_CTL, %ecx
450 rdmsr
451 orl $0x02, %eax
452 wrmsr
453
454 post_code(0x28)
455
456 jmp car_init_done
457#endif