2020-05-24 15:30:55 -05:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#ifndef CACHE_H
|
|
|
|
#define CACHE_H
|
2022-02-19 10:17:40 -06:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
*
|
2021-12-23 05:00:10 -06:00
|
|
|
* Provides functions to enable, disable, and flush the CPU caches.
|
2020-05-24 15:30:55 -05:00
|
|
|
*
|
2022-02-19 13:56:55 -06:00
|
|
|
*//*
|
2022-02-19 10:17:40 -06:00
|
|
|
* Copyright (C) 2020-2022 Martin Whitaker.
|
2020-05-24 15:30:55 -05:00
|
|
|
*/
|
|
|
|
|
2022-02-19 10:17:40 -06:00
|
|
|
/**
|
2020-05-24 15:30:55 -05:00
|
|
|
* Disable the CPU caches.
|
|
|
|
*/
|
|
|
|
static inline void cache_off(void)
|
|
|
|
{
|
|
|
|
#ifdef __x86_64__
|
|
|
|
__asm__ __volatile__ ("\t"
|
|
|
|
"movq %%cr0, %%rax \n\t"
|
|
|
|
"orl $0x40000000, %%eax \n\t" /* Set CD */
|
|
|
|
"movq %%rax, %%cr0 \n\t"
|
|
|
|
"wbinvd \n"
|
|
|
|
: /* no outputs */
|
|
|
|
: /* no inputs */
|
2021-12-23 05:00:10 -06:00
|
|
|
: "rax", "memory"
|
2020-05-24 15:30:55 -05:00
|
|
|
);
|
|
|
|
#else
|
|
|
|
__asm__ __volatile__ ("\t"
|
|
|
|
"movl %%cr0, %%eax \n\t"
|
|
|
|
"orl $0x40000000, %%eax \n\t" /* Set CD */
|
|
|
|
"movl %%eax, %%cr0 \n\t"
|
|
|
|
"wbinvd \n"
|
|
|
|
: /* no outputs */
|
|
|
|
: /* no inputs */
|
2021-12-23 05:00:10 -06:00
|
|
|
: "eax", "memory"
|
2020-05-24 15:30:55 -05:00
|
|
|
);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-02-19 10:17:40 -06:00
|
|
|
/**
|
2020-05-24 15:30:55 -05:00
|
|
|
* Enable the CPU caches.
|
|
|
|
*/
|
|
|
|
static inline void cache_on(void)
|
|
|
|
{
|
|
|
|
#ifdef __x86_64__
|
|
|
|
__asm__ __volatile__ ("\t"
|
|
|
|
"movq %%cr0, %%rax \n\t"
|
|
|
|
"andl $0x9fffffff, %%eax \n\t" /* Clear CD and NW */
|
|
|
|
"movq %%rax, %%cr0 \n"
|
|
|
|
: /* no outputs */
|
|
|
|
: /* no inputs */
|
2021-12-23 05:00:10 -06:00
|
|
|
: "rax", "memory"
|
2020-05-24 15:30:55 -05:00
|
|
|
);
|
|
|
|
#else
|
|
|
|
__asm__ __volatile__ ("\t"
|
|
|
|
"movl %%cr0, %%eax \n\t"
|
|
|
|
"andl $0x9fffffff, %%eax \n\t" /* Clear CD and NW */
|
|
|
|
"movl %%eax, %%cr0 \n"
|
|
|
|
: /* no outputs */
|
|
|
|
: /* no inputs */
|
2021-12-23 05:00:10 -06:00
|
|
|
: "eax", "memory"
|
2020-05-24 15:30:55 -05:00
|
|
|
);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-02-19 10:17:40 -06:00
|
|
|
/**
|
2021-12-23 05:00:10 -06:00
|
|
|
* Flush the CPU caches.
|
|
|
|
*/
|
|
|
|
static inline void cache_flush(void)
|
|
|
|
{
|
|
|
|
__asm__ __volatile__ ("\t"
|
|
|
|
"wbinvd\n"
|
|
|
|
: /* no outputs */
|
|
|
|
: /* no inputs */
|
|
|
|
: "memory"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-24 15:30:55 -05:00
|
|
|
#endif // CACHE_H
|