Angel Pons | 0612b27 | 2020-04-05 15:46:56 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 2 | |
| 3 | #include <commonlib/helpers.h> |
| 4 | #include <cpu/x86/cache.h> |
| 5 | #include <cpu/x86/cr.h> |
Elyes HAOUAS | 419bfbc | 2018-10-01 08:47:51 +0200 | [diff] [blame] | 6 | #include <cpu/x86/msr.h> |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 7 | #include <cpu/x86/mtrr.h> |
| 8 | #include <cpu/x86/post_code.h> |
| 9 | #include <rules.h> |
| 10 | #include <intelblocks/msr.h> |
| 11 | |
Kyösti Mälkki | 7522a8f | 2020-11-20 16:47:38 +0200 | [diff] [blame] | 12 | .section .init, "ax", @progbits |
| 13 | |
Patrick Rudolph | 2b77112 | 2020-11-30 13:52:42 +0100 | [diff] [blame] | 14 | .code32 |
Arthur Heymans | 64c9c6d | 2019-11-25 09:45:40 +0100 | [diff] [blame^] | 15 | |
| 16 | /* |
| 17 | * macro: find_free_mtrr |
| 18 | * Clobbers: %eax, %ebx, %ecx, %edx. |
| 19 | * Returns: |
| 20 | * %ebx contains the number of freely available MTRR's. |
| 21 | * It should be checked against 0. |
| 22 | * %ecx holds the MTRR_BASE of the free MTRR. |
| 23 | */ |
| 24 | .macro find_free_mtrr |
| 25 | /* Figure out how many MTRRs we have */ |
| 26 | mov $MTRR_CAP_MSR, %ecx |
| 27 | rdmsr |
| 28 | movzb %al, %ebx /* Number of variable MTRRs */ |
| 29 | |
| 30 | /* Find a free variable MTRR */ |
| 31 | movl $MTRR_PHYS_MASK(0), %ecx |
| 32 | 1: |
| 33 | rdmsr |
| 34 | test $MTRR_PHYS_MASK_VALID, %eax |
| 35 | jz 2f |
| 36 | addl $2, %ecx |
| 37 | dec %ebx |
| 38 | jnz 1b |
| 39 | 2: |
| 40 | /* %ecx needs to hold the MTRR_BASE */ |
| 41 | decl %ecx |
| 42 | .endm |
| 43 | |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 44 | .global bootblock_pre_c_entry |
| 45 | bootblock_pre_c_entry: |
| 46 | |
| 47 | post_code(0x20) |
| 48 | |
Arthur Heymans | c4772b9 | 2019-04-14 18:38:35 +0200 | [diff] [blame] | 49 | movl $no_reset, %esp /* return address */ |
| 50 | jmp check_mtrr /* Check if CPU properly reset */ |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 51 | |
| 52 | no_reset: |
| 53 | post_code(0x21) |
| 54 | |
| 55 | /* Clear/disable fixed MTRRs */ |
| 56 | mov $fixed_mtrr_list_size, %ebx |
| 57 | xor %eax, %eax |
| 58 | xor %edx, %edx |
| 59 | |
| 60 | clear_fixed_mtrr: |
| 61 | add $-2, %ebx |
| 62 | movzwl fixed_mtrr_list(%ebx), %ecx |
| 63 | wrmsr |
| 64 | jnz clear_fixed_mtrr |
| 65 | |
| 66 | post_code(0x22) |
| 67 | |
Arthur Heymans | c57d303 | 2021-06-16 09:56:26 +0200 | [diff] [blame] | 68 | /* Figure out how many MTRRs we have, and clear them out */ |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 69 | mov $MTRR_CAP_MSR, %ecx |
| 70 | rdmsr |
| 71 | movzb %al, %ebx /* Number of variable MTRRs */ |
| 72 | mov $MTRR_PHYS_BASE(0), %ecx |
| 73 | xor %eax, %eax |
| 74 | xor %edx, %edx |
| 75 | |
| 76 | clear_var_mtrr: |
| 77 | wrmsr |
| 78 | inc %ecx |
| 79 | wrmsr |
| 80 | inc %ecx |
| 81 | dec %ebx |
| 82 | jnz clear_var_mtrr |
| 83 | |
| 84 | post_code(0x23) |
| 85 | |
| 86 | /* Configure default memory type to uncacheable (UC) */ |
| 87 | mov $MTRR_DEF_TYPE_MSR, %ecx |
| 88 | rdmsr |
| 89 | /* Clear enable bits and set default type to UC. */ |
| 90 | and $~(MTRR_DEF_TYPE_MASK | MTRR_DEF_TYPE_EN | \ |
| 91 | MTRR_DEF_TYPE_FIX_EN), %eax |
| 92 | wrmsr |
| 93 | |
| 94 | /* Configure MTRR_PHYS_MASK_HIGH for proper addressing above 4GB |
| 95 | * based on the physical address size supported for this processor |
| 96 | * This is based on read from CPUID EAX = 080000008h, EAX bits [7:0] |
| 97 | * |
| 98 | * Examples: |
| 99 | * MTRR_PHYS_MASK_HIGH = 00000000Fh For 36 bit addressing |
| 100 | * MTRR_PHYS_MASK_HIGH = 0000000FFh For 40 bit addressing |
| 101 | */ |
| 102 | |
Elyes HAOUAS | 05498a2 | 2018-05-28 16:26:43 +0200 | [diff] [blame] | 103 | movl $0x80000008, %eax /* Address sizes leaf */ |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 104 | cpuid |
| 105 | sub $32, %al |
| 106 | movzx %al, %eax |
| 107 | xorl %esi, %esi |
| 108 | bts %eax, %esi |
| 109 | dec %esi /* esi <- MTRR_PHYS_MASK_HIGH */ |
| 110 | |
| 111 | post_code(0x24) |
| 112 | |
| 113 | #if ((CONFIG_DCACHE_RAM_SIZE & (CONFIG_DCACHE_RAM_SIZE - 1)) == 0) |
Arthur Heymans | 64c9c6d | 2019-11-25 09:45:40 +0100 | [diff] [blame^] | 114 | find_free_mtrr |
| 115 | test %ebx, %ebx |
| 116 | jz .halt_forever |
| 117 | |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 118 | /* Configure CAR region as write-back (WB) */ |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 119 | mov $CONFIG_DCACHE_RAM_BASE, %eax |
| 120 | or $MTRR_TYPE_WRBACK, %eax |
| 121 | xor %edx,%edx |
| 122 | wrmsr |
| 123 | |
| 124 | /* Configure the MTRR mask for the size region */ |
Arthur Heymans | 64c9c6d | 2019-11-25 09:45:40 +0100 | [diff] [blame^] | 125 | inc %ecx |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 126 | mov $CONFIG_DCACHE_RAM_SIZE, %eax /* size mask */ |
| 127 | dec %eax |
| 128 | not %eax |
| 129 | or $MTRR_PHYS_MASK_VALID, %eax |
| 130 | movl %esi, %edx /* edx <- MTRR_PHYS_MASK_HIGH */ |
| 131 | wrmsr |
| 132 | #elif (CONFIG_DCACHE_RAM_SIZE == 768 * KiB) /* 768 KiB */ |
Arthur Heymans | 64c9c6d | 2019-11-25 09:45:40 +0100 | [diff] [blame^] | 133 | find_free_mtrr |
| 134 | test %ebx, %ebx |
| 135 | jz .halt_forever |
| 136 | |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 137 | /* Configure CAR region as write-back (WB) */ |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 138 | mov $CONFIG_DCACHE_RAM_BASE, %eax |
| 139 | or $MTRR_TYPE_WRBACK, %eax |
| 140 | xor %edx,%edx |
| 141 | wrmsr |
| 142 | |
Arthur Heymans | 64c9c6d | 2019-11-25 09:45:40 +0100 | [diff] [blame^] | 143 | incl %ecx |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 144 | mov $(512 * KiB), %eax /* size mask */ |
| 145 | dec %eax |
| 146 | not %eax |
| 147 | or $MTRR_PHYS_MASK_VALID, %eax |
| 148 | movl %esi, %edx /* edx <- MTRR_PHYS_MASK_HIGH */ |
| 149 | wrmsr |
| 150 | |
Arthur Heymans | 64c9c6d | 2019-11-25 09:45:40 +0100 | [diff] [blame^] | 151 | find_free_mtrr |
| 152 | test %ebx, %ebx |
| 153 | jz .halt_forever |
| 154 | 1: |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 155 | mov $(CONFIG_DCACHE_RAM_BASE + 512 * KiB), %eax |
| 156 | or $MTRR_TYPE_WRBACK, %eax |
| 157 | xor %edx,%edx |
| 158 | wrmsr |
| 159 | |
Arthur Heymans | 64c9c6d | 2019-11-25 09:45:40 +0100 | [diff] [blame^] | 160 | incl %ecx |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 161 | mov $(256 * KiB), %eax /* size mask */ |
| 162 | dec %eax |
| 163 | not %eax |
| 164 | or $MTRR_PHYS_MASK_VALID, %eax |
| 165 | movl %esi, %edx /* edx <- MTRR_PHYS_MASK_HIGH */ |
| 166 | wrmsr |
| 167 | #else |
| 168 | #error "DCACHE_RAM_SIZE is not a power of 2 and setup code is missing" |
| 169 | #endif |
| 170 | post_code(0x25) |
| 171 | |
| 172 | /* Enable variable MTRRs */ |
| 173 | mov $MTRR_DEF_TYPE_MSR, %ecx |
| 174 | rdmsr |
| 175 | or $MTRR_DEF_TYPE_EN, %eax |
| 176 | wrmsr |
| 177 | |
| 178 | /* Enable caching */ |
| 179 | mov %cr0, %eax |
| 180 | and $~(CR0_CD | CR0_NW), %eax |
| 181 | invd |
| 182 | mov %eax, %cr0 |
| 183 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 184 | #if CONFIG(INTEL_CAR_NEM) |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 185 | jmp car_nem |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 186 | #elif CONFIG(INTEL_CAR_CQOS) |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 187 | jmp car_cqos |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 188 | #elif CONFIG(INTEL_CAR_NEM_ENHANCED) |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 189 | jmp car_nem_enhanced |
| 190 | #else |
| 191 | jmp .halt_forever /* In case nothing has selected */ |
| 192 | #endif |
| 193 | |
| 194 | .global car_init_done |
| 195 | car_init_done: |
| 196 | |
| 197 | post_code(0x29) |
| 198 | |
| 199 | /* Setup bootblock stack */ |
Arthur Heymans | df9cdcf | 2019-11-09 06:50:20 +0100 | [diff] [blame] | 200 | mov $_ecar_stack, %esp |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 201 | |
Aaron Durbin | 028e18f | 2017-06-23 11:14:58 -0500 | [diff] [blame] | 202 | /* Need to align stack to 16 bytes at call instruction. Account for |
| 203 | the two pushes below. */ |
| 204 | andl $0xfffffff0, %esp |
Patrick Rudolph | 2b77112 | 2020-11-30 13:52:42 +0100 | [diff] [blame] | 205 | |
| 206 | #if ENV_X86_64 |
| 207 | #include <cpu/x86/64bit/entry64.inc> |
| 208 | movd %mm2, %rdi |
| 209 | shlq $32, %rdi |
| 210 | movd %mm1, %rsi |
| 211 | or %rsi, %rdi |
| 212 | movd %mm0, %rsi |
| 213 | #else |
Aaron Durbin | 028e18f | 2017-06-23 11:14:58 -0500 | [diff] [blame] | 214 | sub $8, %esp |
| 215 | |
Subrata Banik | 5885ffe | 2019-11-14 11:08:51 +0530 | [diff] [blame] | 216 | /* push TSC value to stack */ |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 217 | movd %mm2, %eax |
| 218 | pushl %eax /* tsc[63:32] */ |
| 219 | movd %mm1, %eax |
Elyes HAOUAS | 05498a2 | 2018-05-28 16:26:43 +0200 | [diff] [blame] | 220 | pushl %eax /* tsc[31:0] */ |
Patrick Rudolph | 2b77112 | 2020-11-30 13:52:42 +0100 | [diff] [blame] | 221 | #endif |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 222 | |
| 223 | before_carstage: |
| 224 | post_code(0x2A) |
| 225 | |
| 226 | call bootblock_c_entry |
| 227 | /* Never reached */ |
| 228 | |
| 229 | .halt_forever: |
| 230 | post_code(POST_DEAD_CODE) |
| 231 | hlt |
| 232 | jmp .halt_forever |
| 233 | |
| 234 | fixed_mtrr_list: |
| 235 | .word MTRR_FIX_64K_00000 |
| 236 | .word MTRR_FIX_16K_80000 |
| 237 | .word MTRR_FIX_16K_A0000 |
| 238 | .word MTRR_FIX_4K_C0000 |
| 239 | .word MTRR_FIX_4K_C8000 |
| 240 | .word MTRR_FIX_4K_D0000 |
| 241 | .word MTRR_FIX_4K_D8000 |
| 242 | .word MTRR_FIX_4K_E0000 |
| 243 | .word MTRR_FIX_4K_E8000 |
| 244 | .word MTRR_FIX_4K_F0000 |
| 245 | .word MTRR_FIX_4K_F8000 |
| 246 | fixed_mtrr_list_size = . - fixed_mtrr_list |
| 247 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 248 | #if CONFIG(INTEL_CAR_NEM) |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 249 | .global car_nem |
| 250 | car_nem: |
| 251 | /* Disable cache eviction (setup stage) */ |
| 252 | mov $MSR_EVICT_CTL, %ecx |
| 253 | rdmsr |
| 254 | or $0x1, %eax |
| 255 | wrmsr |
| 256 | |
| 257 | post_code(0x26) |
| 258 | |
| 259 | /* Clear the cache memory region. This will also fill up the cache */ |
| 260 | movl $CONFIG_DCACHE_RAM_BASE, %edi |
| 261 | movl $CONFIG_DCACHE_RAM_SIZE, %ecx |
| 262 | shr $0x02, %ecx |
| 263 | xor %eax, %eax |
| 264 | cld |
| 265 | rep stosl |
| 266 | |
| 267 | post_code(0x27) |
| 268 | |
| 269 | /* Disable cache eviction (run stage) */ |
| 270 | mov $MSR_EVICT_CTL, %ecx |
| 271 | rdmsr |
| 272 | or $0x2, %eax |
| 273 | wrmsr |
| 274 | |
| 275 | post_code(0x28) |
| 276 | |
| 277 | jmp car_init_done |
| 278 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 279 | #elif CONFIG(INTEL_CAR_CQOS) |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 280 | .global car_cqos |
| 281 | car_cqos: |
| 282 | /* |
Naresh G Solanki | f329f0c | 2017-09-27 14:21:18 +0530 | [diff] [blame] | 283 | * Create CBM_LEN_MASK based on CBM_LEN |
| 284 | * Get CPUID.(EAX=10H, ECX=2H):EAX.CBM_LEN[bits 4:0] |
| 285 | */ |
| 286 | mov $0x10, %eax |
| 287 | mov $0x2, %ecx |
| 288 | cpuid |
| 289 | and $0x1F, %eax |
| 290 | add $1, %al |
| 291 | |
| 292 | mov $1, %ebx |
| 293 | mov %al, %cl |
| 294 | shl %cl, %ebx |
| 295 | sub $1, %ebx |
| 296 | |
| 297 | /* Store the CBM_LEN_MASK in mm3 for later use. */ |
| 298 | movd %ebx, %mm3 |
| 299 | |
| 300 | /* |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 301 | * Disable both L1 and L2 prefetcher. For yet-to-understood reason, |
| 302 | * prefetchers slow down filling cache with rep stos in CQOS mode. |
| 303 | */ |
| 304 | mov $MSR_PREFETCH_CTL, %ecx |
| 305 | rdmsr |
| 306 | or $(PREFETCH_L1_DISABLE | PREFETCH_L2_DISABLE), %eax |
| 307 | wrmsr |
| 308 | |
| 309 | #if (CONFIG_DCACHE_RAM_SIZE == CONFIG_L2_CACHE_SIZE) |
| 310 | /* |
| 311 | * If CAR size is set to full L2 size, mask is calculated as all-zeros. |
| 312 | * This is not supported by the CPU/uCode. |
| 313 | */ |
| 314 | #error "CQOS CAR may not use whole L2 cache area" |
| 315 | #endif |
| 316 | |
| 317 | /* Calculate how many bits to be used for CAR */ |
| 318 | xor %edx, %edx |
| 319 | mov $CONFIG_DCACHE_RAM_SIZE, %eax /* dividend */ |
| 320 | mov $CONFIG_CACHE_QOS_SIZE_PER_BIT, %ecx /* divisor */ |
| 321 | div %ecx /* result is in eax */ |
| 322 | mov %eax, %ecx /* save to ecx */ |
| 323 | mov $1, %ebx |
| 324 | shl %cl, %ebx |
| 325 | sub $1, %ebx /* resulting mask is is in ebx */ |
| 326 | |
| 327 | /* Set this mask for initial cache fill */ |
| 328 | mov $MSR_L2_QOS_MASK(0), %ecx |
| 329 | rdmsr |
Naresh G Solanki | f329f0c | 2017-09-27 14:21:18 +0530 | [diff] [blame] | 330 | mov %ebx, %eax |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 331 | wrmsr |
| 332 | |
| 333 | /* Set CLOS selector to 0 */ |
Elyes HAOUAS | 419bfbc | 2018-10-01 08:47:51 +0200 | [diff] [blame] | 334 | mov $IA32_PQR_ASSOC, %ecx |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 335 | rdmsr |
| 336 | and $~IA32_PQR_ASSOC_MASK, %edx /* select mask 0 */ |
| 337 | wrmsr |
| 338 | |
| 339 | /* We will need to block CAR region from evicts */ |
| 340 | mov $MSR_L2_QOS_MASK(1), %ecx |
| 341 | rdmsr |
| 342 | /* Invert bits that are to be used for cache */ |
Naresh G Solanki | f329f0c | 2017-09-27 14:21:18 +0530 | [diff] [blame] | 343 | mov %ebx, %eax |
| 344 | xor $~0, %eax /* invert 32 bits */ |
| 345 | |
| 346 | /* |
| 347 | * Use CBM_LEN_MASK stored in mm3 to set bits based on Capacity Bit |
| 348 | * Mask Length. |
| 349 | */ |
| 350 | movd %mm3, %ebx |
| 351 | and %ebx, %eax |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 352 | wrmsr |
| 353 | |
| 354 | post_code(0x26) |
| 355 | |
| 356 | /* Clear the cache memory region. This will also fill up the cache */ |
| 357 | movl $CONFIG_DCACHE_RAM_BASE, %edi |
| 358 | movl $CONFIG_DCACHE_RAM_SIZE, %ecx |
| 359 | shr $0x02, %ecx |
| 360 | xor %eax, %eax |
| 361 | cld |
| 362 | rep stosl |
| 363 | |
| 364 | post_code(0x27) |
| 365 | |
| 366 | /* Cache is populated. Use mask 1 that will block evicts */ |
Elyes HAOUAS | 419bfbc | 2018-10-01 08:47:51 +0200 | [diff] [blame] | 367 | mov $IA32_PQR_ASSOC, %ecx |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 368 | rdmsr |
| 369 | and $~IA32_PQR_ASSOC_MASK, %edx /* clear index bits first */ |
| 370 | or $1, %edx /* select mask 1 */ |
| 371 | wrmsr |
| 372 | |
| 373 | /* Enable prefetchers */ |
| 374 | mov $MSR_PREFETCH_CTL, %ecx |
| 375 | rdmsr |
| 376 | and $~(PREFETCH_L1_DISABLE | PREFETCH_L2_DISABLE), %eax |
| 377 | wrmsr |
| 378 | |
| 379 | post_code(0x28) |
| 380 | |
| 381 | jmp car_init_done |
| 382 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 383 | #elif CONFIG(INTEL_CAR_NEM_ENHANCED) |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 384 | .global car_nem_enhanced |
| 385 | car_nem_enhanced: |
| 386 | /* Disable cache eviction (setup stage) */ |
| 387 | mov $MSR_EVICT_CTL, %ecx |
| 388 | rdmsr |
| 389 | or $0x1, %eax |
| 390 | wrmsr |
| 391 | post_code(0x26) |
| 392 | |
| 393 | /* Create n-way set associativity of cache */ |
| 394 | xorl %edi, %edi |
| 395 | find_llc_subleaf: |
| 396 | movl %edi, %ecx |
| 397 | movl $0x04, %eax |
| 398 | cpuid |
| 399 | inc %edi |
| 400 | and $0xe0, %al /* EAX[7:5] = Cache Level */ |
| 401 | cmp $0x60, %al /* Check to see if it is LLC */ |
| 402 | jnz find_llc_subleaf |
| 403 | |
| 404 | /* |
Aamir Bohra | c1d227d | 2020-07-16 09:03:06 +0530 | [diff] [blame] | 405 | * Calculate the total LLC size |
| 406 | * (Line_Size + 1) * (Sets + 1) * (Partitions + 1) * (Ways + 1) |
| 407 | * (EBX[11:0] + 1) * (ECX + 1) * (EBX[21:12] + 1) * EBX[31:22] + 1) |
| 408 | */ |
| 409 | |
| 410 | mov %ebx, %eax |
| 411 | and $0xFFF, %eax |
| 412 | inc %eax |
| 413 | inc %ecx |
| 414 | mul %ecx |
| 415 | mov %eax, %ecx |
| 416 | mov %ebx, %eax |
| 417 | shr $12, %eax |
| 418 | and $0x3FF, %eax |
| 419 | inc %eax |
| 420 | mul %ecx |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 421 | shr $22, %ebx |
| 422 | inc %ebx |
Aamir Bohra | c1d227d | 2020-07-16 09:03:06 +0530 | [diff] [blame] | 423 | mov %ebx, %edx |
| 424 | mul %ebx /* eax now holds total LLC size */ |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 425 | |
| 426 | /* |
Aamir Bohra | c1d227d | 2020-07-16 09:03:06 +0530 | [diff] [blame] | 427 | * The number of the ways that we want to protect from eviction |
| 428 | * can be calculated as RW data stack size / way size where way |
| 429 | * size is Total LLC size / Total number of LLC ways. |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 430 | */ |
Aamir Bohra | c1d227d | 2020-07-16 09:03:06 +0530 | [diff] [blame] | 431 | div %ebx /* way size */ |
| 432 | mov %eax, %ecx |
| 433 | |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 434 | /* |
Aamir Bohra | c1d227d | 2020-07-16 09:03:06 +0530 | [diff] [blame] | 435 | * Check if way size if bigger than the cache ram size. |
| 436 | * Then we need to allocate just one way for non-eviction |
| 437 | * of RW data. |
| 438 | */ |
Subrata Banik | 0603902 | 2021-03-09 14:40:39 +0530 | [diff] [blame] | 439 | movl $0x01, %eax |
| 440 | cmp $CONFIG_DCACHE_RAM_SIZE, %ecx |
| 441 | jnc set_eviction_mask |
Aamir Bohra | c1d227d | 2020-07-16 09:03:06 +0530 | [diff] [blame] | 442 | |
| 443 | /* |
| 444 | * RW data size / way size is equal to number of |
| 445 | * ways to be configured for non-eviction |
| 446 | */ |
Subrata Banik | 0603902 | 2021-03-09 14:40:39 +0530 | [diff] [blame] | 447 | mov $CONFIG_DCACHE_RAM_SIZE, %eax |
Aamir Bohra | c1d227d | 2020-07-16 09:03:06 +0530 | [diff] [blame] | 448 | div %ecx |
| 449 | mov %eax, %ecx |
| 450 | movl $0x01, %eax |
| 451 | shl %cl, %eax |
| 452 | subl $0x01, %eax |
| 453 | |
| 454 | set_eviction_mask: |
Shreesh Chhabbi | 860c684 | 2020-12-03 15:06:20 -0800 | [diff] [blame] | 455 | mov %ebx, %ecx /* back up number of ways */ |
| 456 | mov %eax, %ebx /* back up the non-eviction mask*/ |
| 457 | #if CONFIG(CAR_HAS_SF_MASKS) |
| 458 | mov %ecx, %edi /* use number of ways to prepare SF mask */ |
| 459 | /* |
| 460 | * SF mask is programmed with the double number of bits than |
| 461 | * the number of ways |
| 462 | */ |
| 463 | mov $0x01, %eax |
| 464 | shl %cl, %eax |
| 465 | shl %cl, %eax |
| 466 | subl $0x01, %eax /* contains SF mask */ |
| 467 | /* |
| 468 | * Program MSR 0x1891 IA32_CR_SF_QOS_MASK_1 with |
| 469 | * total number of LLC ways |
| 470 | */ |
| 471 | movl $IA32_CR_SF_QOS_MASK_1, %ecx |
| 472 | xorl %edx, %edx |
| 473 | wrmsr |
| 474 | mov %edi, %ecx /* restore number of ways */ |
| 475 | #endif |
Aamir Bohra | c1d227d | 2020-07-16 09:03:06 +0530 | [diff] [blame] | 476 | /* |
Shreesh Chhabbi | 87c7ec7 | 2020-12-03 14:07:15 -0800 | [diff] [blame] | 477 | * Program MSR 0xC91 IA32_L3_MASK_1 |
Aamir Bohra | c1d227d | 2020-07-16 09:03:06 +0530 | [diff] [blame] | 478 | * This MSR contain one bit per each way of LLC |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 479 | * - If this bit is '0' - the way is protected from eviction |
| 480 | * - If this bit is '1' - the way is not protected from eviction |
| 481 | */ |
Subrata Banik | 0603902 | 2021-03-09 14:40:39 +0530 | [diff] [blame] | 482 | mov $0x1, %eax |
| 483 | shl %cl, %eax |
| 484 | subl $0x01, %eax |
| 485 | mov %eax, %ecx |
| 486 | mov %ebx, %eax |
Aamir Bohra | c1d227d | 2020-07-16 09:03:06 +0530 | [diff] [blame] | 487 | |
| 488 | xor $~0, %eax /* invert 32 bits */ |
| 489 | and %ecx, %eax |
Elyes HAOUAS | 419bfbc | 2018-10-01 08:47:51 +0200 | [diff] [blame] | 490 | movl $IA32_L3_MASK_1, %ecx |
Aamir Bohra | c1d227d | 2020-07-16 09:03:06 +0530 | [diff] [blame] | 491 | xorl %edx, %edx |
| 492 | wrmsr |
Aamir Bohra | c1d227d | 2020-07-16 09:03:06 +0530 | [diff] [blame] | 493 | /* |
Shreesh Chhabbi | 87c7ec7 | 2020-12-03 14:07:15 -0800 | [diff] [blame] | 494 | * Program MSR 0xC92 IA32_L3_MASK_2 |
Aamir Bohra | c1d227d | 2020-07-16 09:03:06 +0530 | [diff] [blame] | 495 | * This MSR contain one bit per each way of LLC |
| 496 | * - If this bit is '0' - the way is protected from eviction |
| 497 | * - If this bit is '1' - the way is not protected from eviction |
| 498 | */ |
| 499 | mov %ebx, %eax |
Aamir Bohra | c1d227d | 2020-07-16 09:03:06 +0530 | [diff] [blame] | 500 | movl $IA32_L3_MASK_2, %ecx |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 501 | xorl %edx, %edx |
| 502 | wrmsr |
| 503 | /* |
Aamir Bohra | c1d227d | 2020-07-16 09:03:06 +0530 | [diff] [blame] | 504 | * Set IA32_PQR_ASSOC |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 505 | * |
| 506 | * Possible values: |
| 507 | * 0: Default value, no way mask should be applied |
| 508 | * 1: Apply way mask 1 to LLC |
| 509 | * 2: Apply way mask 2 to LLC |
| 510 | * 3: Shouldn't be use in NEM Mode |
| 511 | */ |
Elyes HAOUAS | 419bfbc | 2018-10-01 08:47:51 +0200 | [diff] [blame] | 512 | movl $IA32_PQR_ASSOC, %ecx |
Aamir Bohra | c1d227d | 2020-07-16 09:03:06 +0530 | [diff] [blame] | 513 | xorl %eax, %eax |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 514 | xorl %edx, %edx |
Aamir Bohra | c1d227d | 2020-07-16 09:03:06 +0530 | [diff] [blame] | 515 | #if CONFIG(COS_MAPPED_TO_MSB) |
| 516 | movl $0x02, %edx |
| 517 | #else |
| 518 | movl $0x02, %eax |
| 519 | #endif |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 520 | wrmsr |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 521 | movl $CONFIG_DCACHE_RAM_BASE, %edi |
| 522 | movl $CONFIG_DCACHE_RAM_SIZE, %ecx |
| 523 | shr $0x02, %ecx |
| 524 | xor %eax, %eax |
| 525 | cld |
| 526 | rep stosl |
| 527 | /* |
Aamir Bohra | c1d227d | 2020-07-16 09:03:06 +0530 | [diff] [blame] | 528 | * Set IA32_PQR_ASSOC |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 529 | * At this stage we apply LLC_WAY_MASK_1 to the cache. |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 530 | */ |
Elyes HAOUAS | 419bfbc | 2018-10-01 08:47:51 +0200 | [diff] [blame] | 531 | movl $IA32_PQR_ASSOC, %ecx |
Aamir Bohra | c1d227d | 2020-07-16 09:03:06 +0530 | [diff] [blame] | 532 | xorl %eax, %eax |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 533 | xorl %edx, %edx |
Aamir Bohra | c1d227d | 2020-07-16 09:03:06 +0530 | [diff] [blame] | 534 | #if CONFIG(COS_MAPPED_TO_MSB) |
| 535 | movl $0x01, %edx |
| 536 | #else |
| 537 | movl $0x01, %eax |
| 538 | #endif |
Subrata Banik | 03e971c | 2017-03-07 14:02:23 +0530 | [diff] [blame] | 539 | wrmsr |
| 540 | |
| 541 | post_code(0x27) |
| 542 | /* |
| 543 | * Enable No-Eviction Mode Run State by setting |
| 544 | * NO_EVICT_MODE MSR 2E0h bit [1] = '1'. |
| 545 | */ |
| 546 | |
| 547 | movl $MSR_EVICT_CTL, %ecx |
| 548 | rdmsr |
| 549 | orl $0x02, %eax |
| 550 | wrmsr |
| 551 | |
| 552 | post_code(0x28) |
| 553 | |
| 554 | jmp car_init_done |
| 555 | #endif |