summaryrefslogtreecommitdiff
path: root/src/arch/mips/cache.c
blob: 7d96426c9a269166e43ef75f80a8fc98d0f3935d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * This file is part of the coreboot project.
 *
 * Copyright (C) 2014 Imagination Technologies
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc.
 */

#include <arch/cache.h>
#include <arch/cpu.h>
#include <console/console.h>
#include <symbols.h>

/* cache_op: issues cache operation for specified address */
#define cache_op(op, addr)						\
({									\
	__asm__ __volatile__(						\
		".set push\n\t"						\
		".set noreorder\n\t"					\
		".set mips32\n\t"					\
		"cache %0, %1\n\t"					\
		".set mips0\n\t"					\
		".set pop\n\t"						\
		:							\
		: "i" (op), "R" (*(unsigned char *)(addr)));		\
})

#define MIPS_CONFIG1_DL_SHIFT	10
#define MIPS_CONFIG1_DL_MASK	(0x00000007)
#define MIPS_CONFIG1_IL_SHIFT	19
#define MIPS_CONFIG1_IL_MASK	(0x00000007)
#define MIPS_CONFIG2_SL_SHIFT	4
#define MIPS_CONFIG2_SL_MASK	(0x0000000F)

/*
 * get_cache_line_size:
 * Read config register
 * Isolate instruction cache line size
 * Interpret value as per MIPS manual: 2 << value
 * Return cache line size
 */
static int get_cache_line_size(uint8_t type)
{
	switch (type) {
	case ICACHE:
		return 2 << ((read_c0_config1() >> MIPS_CONFIG1_IL_SHIFT) &
				MIPS_CONFIG1_IL_MASK);
	case DCACHE:
		return 2 << ((read_c0_config1() >> MIPS_CONFIG1_DL_SHIFT) &
				MIPS_CONFIG1_DL_MASK);
	case L2CACHE:
		return 2 << ((read_c0_config2() >> MIPS_CONFIG2_SL_SHIFT) &
				MIPS_CONFIG2_SL_MASK);
	default:
		printk(BIOS_ERR, "%s: Error: unsupported cache type.\n",
				__func__);
		return 0;
	}
	return 0;
}

void perform_cache_operation(uintptr_t start, size_t size, uint8_t operation)
{
	u32 line_size, line_mask;
	uintptr_t end;

	line_size = get_cache_line_size((operation >> CACHE_TYPE_SHIFT) &
					CACHE_TYPE_MASK);
	if (!line_size)
		return;
	line_mask = ~(line_size-1);
	end = (start + (line_size - 1) + size) & line_mask;
	start &= line_mask;
	if ((operation & L2CACHE) == L2CACHE)
		write_c0_l23taglo(0);
	while (start < end) {
		switch (operation) {
		case CACHE_CODE(ICACHE, WB_INVD):
			cache_op(CACHE_CODE(ICACHE, WB_INVD), start);
			break;
		case CACHE_CODE(DCACHE, WB_INVD):
			cache_op(CACHE_CODE(DCACHE, WB_INVD), start);
			break;
		case CACHE_CODE(L2CACHE, WB_INVD):
			cache_op(CACHE_CODE(L2CACHE, WB_INVD), start);
			break;
		default:
			return;
		}
		start += line_size;
	}
	asm("sync");
}

void cache_invalidate_all(uintptr_t start, size_t size)
{
	perform_cache_operation(start, size, CACHE_CODE(ICACHE, WB_INVD));
	perform_cache_operation(start, size, CACHE_CODE(DCACHE, WB_INVD));
	perform_cache_operation(start, size, CACHE_CODE(L2CACHE, WB_INVD));
}