| 1 |
|
| 2 |
|
| 3 |
#ifndef CPU_X86_CACHE |
| 4 |
#define CPU_X86_CACHE |
| 5 |
|
| 6 |
#include <cpu/x86/cr.h> |
| 7 |
|
| 8 |
#define CR0_CacheDisable (CR0_CD) |
| 9 |
#define CR0_NoWriteThrough (CR0_NW) |
| 10 |
|
| 11 |
#define CPUID_FEATURE_CLFLUSH_BIT 19 |
| 12 |
#define CPUID_FEATURE_SELF_SNOOP_BIT 27 |
| 13 |
|
| 14 |
#if !defined(__ASSEMBLER__) |
| 15 |
|
| 16 |
#include <arch/cpuid.h> |
| 17 |
#include <stdbool.h> |
| 18 |
#include <stddef.h> |
| 19 |
|
| 20 |
static inline void wbinvd(void) |
| 21 |
{ |
| 22 |
asm volatile ("wbinvd" ::: "memory"); |
| 23 |
} |
| 24 |
|
| 25 |
static inline void invd(void) |
| 26 |
{ |
| 27 |
asm volatile("invd" ::: "memory"); |
| 28 |
} |
| 29 |
|
| 30 |
static inline void clflush(void *addr) |
| 31 |
{ |
| 32 |
asm volatile ("clflush (%0)"::"r" (addr)); |
| 33 |
} |
| 34 |
|
| 35 |
bool clflush_supported(void); |
| 36 |
void clflush_region(const uintptr_t start, const size_t size); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
static __always_inline void enable_cache(void) |
| 49 |
{ |
| 50 |
write_cr0(read_cr0() & ~(CR0_CD | CR0_NW)); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
static __always_inline bool self_snooping_supported(void) |
| 59 |
{ |
| 60 |
return (cpuid_edx(1) >> CPUID_FEATURE_SELF_SNOOP_BIT) & 1; |
| 61 |
} |
| 62 |
|
| 63 |
static __always_inline void disable_cache(void) |
| 64 |
{ |
| 65 |
|
| 66 |
write_cr0(read_cr0() | CR0_CD); |
| 67 |
if (!self_snooping_supported()) |
| 68 |
wbinvd(); |
| 69 |
} |
| 70 |
|
| 71 |
#endif |
| 72 |
#endif |