aboutsummaryrefslogtreecommitdiff
path: root/src/arch/mips/mmu.c
blob: 5ef276d2739c3cdeba92ef6336a756db12bdcdf6 (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
/*
 * This file is part of the coreboot project.
 *
 * 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.
 */

#include <arch/mmu.h>
#include <console/console.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>

#define MIN_PAGE_SIZE (4 * KiB)

static int add_wired_tlb_entry(uint32_t entrylo0, uint32_t entrylo1,
			       uint32_t entryhi, uint32_t pgsize)
{
	uint32_t tlbindex;

	tlbindex = read_c0_wired();
	if (tlbindex >= get_tlb_size() || tlbindex >= C0_WIRED_MASK) {
		printk(BIOS_ERR, "Ran out of TLB entries\n");
		return -1;
	}
	write_c0_wired(tlbindex + 1);
	write_c0_index(tlbindex);
	write_c0_pagemask(((pgsize / MIN_PAGE_SIZE) - 1) << C0_PAGEMASK_SHIFT);
	write_c0_entryhi(entryhi);
	write_c0_entrylo0(entrylo0);
	write_c0_entrylo1(entrylo1);
	mtc0_tlbw_hazard();
	tlb_write_indexed();
	tlbw_use_hazard();

	return 0;
}

static uint32_t pick_pagesize(uint32_t start, uint32_t len)
{
	uint32_t pgsize, max_pgsize;

	max_pgsize = get_max_pagesize();
	for (pgsize = max_pgsize;
	     pgsize >= MIN_PAGE_SIZE;
	     pgsize = pgsize / 4) {
		/*
		 * Each TLB entry maps a pair of virtual pages.  To avoid
		 * aliasing, pick the largest page size that is at most
		 * half the size of the region we're trying to map.
		 */
		if (IS_ALIGNED(start, 2 * pgsize) && (2 * pgsize <= len))
			break;
	}

	return pgsize;
}

/*
 * Identity map the memory from [start,start+len] in the TLB using the
 * largest suitable page size so as to conserve TLB entries.
 */
int identity_map(uint32_t start, size_t len, uint32_t coherency)
{
	uint32_t pgsize, pfn, entryhi, entrylo0, entrylo1;

	coherency &= C0_ENTRYLO_COHERENCY_MASK;
	while (len > 0) {
		pgsize = pick_pagesize(start, len);
		entryhi = start;
		pfn = start >> 12;
		entrylo0 = (pfn << C0_ENTRYLO_PFN_SHIFT) | coherency |
			C0_ENTRYLO_D | C0_ENTRYLO_V | C0_ENTRYLO_G;
		start += pgsize;
		len -= MIN(len, pgsize);
		if (len >= pgsize) {
			pfn = start >> 12;
			entrylo1 = (pfn << C0_ENTRYLO_PFN_SHIFT) |
				coherency | C0_ENTRYLO_D | C0_ENTRYLO_V |
				C0_ENTRYLO_G;
			start += pgsize;
			len -= MIN(len, pgsize);
		} else {
			entrylo1 = 0;
		}
		if (add_wired_tlb_entry(entrylo0, entrylo1, entryhi, pgsize))
			return -1;
	}

	return 0;
}