blob: 9c1af294e8a3a9d1b95c404cea7a31ea38547122 [file] [log] [blame]
Stefan Reinauer8f2c6162010-04-06 21:50:21 +00001/*
2 * This file is part of the coreboot project.
Stefan Reinauer14e22772010-04-27 06:56:47 +00003 *
Stefan Reinauer8f2c6162010-04-06 21:50:21 +00004 * Copyright (C) 2004 Eric W. Biederman
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Stefan Reinauer8f2c6162010-04-06 21:50:21 +000014 */
15
Eric Biedermanc84c1902004-10-14 20:13:01 +000016#ifndef CPU_X86_CACHE
17#define CPU_X86_CACHE
18
Aaron Durbin029aaf62013-10-10 12:41:49 -050019#include <cpu/x86/cr.h>
20
21#define CR0_CacheDisable (CR0_CD)
22#define CR0_NoWriteThrough (CR0_NW)
Patrick Georgi05e740f2012-03-31 12:52:21 +020023
24#if !defined(__ASSEMBLER__)
25
Stefan Reinauer1c3c0fa2010-05-19 18:39:23 +000026/*
27 * Need two versions because ROMCC chokes on certain clobbers:
Stefan Reinauer5ff7c132011-10-31 12:56:45 -070028 * cache.h:29.71: cache.h:60.24: earlymtrr.c:117.23: romstage.c:144.33:
Stefan Reinauer1c3c0fa2010-05-19 18:39:23 +000029 * 0x1559920 asm Internal compiler error: lhs 1 regcm == 0
30 */
Rudolf Marekbeba9902010-05-16 21:51:34 +000031
Rudolf Marekfdddce32010-05-16 22:26:25 +000032#if defined(__GNUC__)
33
Rudolf Marek417e66b2010-05-16 22:32:58 +000034static inline void wbinvd(void)
35{
36 asm volatile ("wbinvd" ::: "memory");
37}
38
Rudolf Marekfdddce32010-05-16 22:26:25 +000039#else
40
Rudolf Marek417e66b2010-05-16 22:32:58 +000041static inline void wbinvd(void)
42{
43 asm volatile ("wbinvd");
44}
Rudolf Marekfdddce32010-05-16 22:26:25 +000045
Rudolf Marek417e66b2010-05-16 22:32:58 +000046#endif
Rudolf Marekfdddce32010-05-16 22:26:25 +000047
Eric Biedermanc84c1902004-10-14 20:13:01 +000048static inline void invd(void)
49{
50 asm volatile("invd" ::: "memory");
51}
Stefan Reinauer8f2c6162010-04-06 21:50:21 +000052
Scott Duplichan78301d02010-09-17 21:38:40 +000053/* The following functions require the always_inline due to AMD
54 * function STOP_CAR_AND_CPU that disables cache as
55 * ram, the cache as ram stack can no longer be used. Called
56 * functions must be inlined to avoid stack usage. Also, the
57 * compiler must keep local variables register based and not
58 * allocated them from the stack. With gcc 4.5.0, some functions
59 * declared as inline are not being inlined. This patch forces
60 * these functions to always be inlined by adding the qualifier
61 * __attribute__((always_inline)) to their declaration.
62 */
63static inline __attribute__((always_inline)) void enable_cache(void)
Eric Biedermanc84c1902004-10-14 20:13:01 +000064{
65 unsigned long cr0;
66 cr0 = read_cr0();
Aaron Durbin029aaf62013-10-10 12:41:49 -050067 cr0 &= ~(CR0_CD | CR0_NW);
Eric Biedermanc84c1902004-10-14 20:13:01 +000068 write_cr0(cr0);
69}
70
Scott Duplichan78301d02010-09-17 21:38:40 +000071static inline __attribute__((always_inline)) void disable_cache(void)
Eric Biedermanc84c1902004-10-14 20:13:01 +000072{
73 /* Disable and write back the cache */
74 unsigned long cr0;
75 cr0 = read_cr0();
Aaron Durbin029aaf62013-10-10 12:41:49 -050076 cr0 |= CR0_CD;
Eric Biedermanc84c1902004-10-14 20:13:01 +000077 wbinvd();
78 write_cr0(cr0);
79 wbinvd();
80}
81
Stefan Reinauer35b6bbb2010-03-28 21:26:54 +000082#if !defined(__PRE_RAM__)
Eric Biedermanc84c1902004-10-14 20:13:01 +000083void x86_enable_cache(void);
Stefan Reinauer35b6bbb2010-03-28 21:26:54 +000084#endif
Eric Biedermanc84c1902004-10-14 20:13:01 +000085
Patrick Georgi05e740f2012-03-31 12:52:21 +020086#endif /* !__ASSEMBLER__ */
Eric Biedermanc84c1902004-10-14 20:13:01 +000087#endif /* CPU_X86_CACHE */