summaryrefslogtreecommitdiff
path: root/core/cortex-m/mpu.c
blob: 8513f972bdae2b216bf70ca13e942760cef96a5c (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* MPU module for Chrome EC */

#include "mpu.h"
#include "console.h"
#include "registers.h"
#include "task.h"
#include "util.h"

/* Region assignment. 7 as the highest, a higher index has a higher priority.
 * For example, using 7 for .iram.text allows us to mark entire RAM XN except
 * .iram.text, which is used for hibernation. */
enum mpu_region {
	REGION_IRAM = 0,          /* For internal RAM */
	REGION_FLASH_MEMORY = 1,  /* For flash memory */
	REGION_IRAM_TEXT = 7      /* For *.(iram.text) */
};

/**
 * Update a memory region.
 *
 * region: index of the region to update
 * addr: base address of the region
 * size_bit: size of the region in power of two.
 * attr: attribute bits. Current value will be overwritten if enable is true.
 * enable: enables the region if non zero. Otherwise, disables the region.
 *
 * Based on 3.1.4.1 'Updating an MPU Region' of Stellaris LM4F232H5QC Datasheet
 */
static void mpu_update_region(uint8_t region, uint32_t addr, uint8_t size_bit,
			      uint16_t attr, uint8_t enable)
{
	asm volatile("isb; dsb;");

	MPU_NUMBER = region;
	MPU_SIZE &= ~1;					/* Disable */
	if (enable) {
		MPU_BASE = addr;
		MPU_ATTR = attr;
		MPU_SIZE = (size_bit - 1) << 1 | 1;	/* Enable */
	}

	asm volatile("isb; dsb;");
}

/**
 * Configure a region
 *
 * region: index of the region to update
 * addr: Base address of the region
 * size: Size of the region in bytes
 * attr: Attribute bits. Current value will be overwritten if enable is set.
 * enable: Enables the region if non zero. Otherwise, disables the region.
 *
 * Returns EC_SUCCESS on success or EC_ERROR_INVAL if a parameter is invalid.
 */
static int mpu_config_region(uint8_t region, uint32_t addr, uint32_t size,
			     uint16_t attr, uint8_t enable)
{
	int size_bit = 0;

	if (!size)
		return -EC_ERROR_INVAL;
	while (!(size & 1)) {
		size_bit++;
		size >>= 1;
	}
	/* Region size must be a power of 2 (size == 0) and equal or larger than
	 * 32 (size_bit >= 5) */
	if (size > 1 || size_bit < 5)
		return -EC_ERROR_INVAL;

	mpu_update_region(region, addr, size_bit, attr, enable);

	return EC_SUCCESS;
}

/**
 * Set a region non-executable and read-write.
 *
 * region: index of the region
 * addr: base address of the region
 * size: size of the region in bytes
 * texscb: TEX and SCB bit field
 */
static int mpu_lock_region(uint8_t region, uint32_t addr, uint32_t size,
			   uint8_t texscb)
{
	return mpu_config_region(region, addr, size,
				 MPU_ATTR_XN | MPU_ATTR_RW_RW | texscb, 1);
}

/**
 * Set a region executable and read-write.
 *
 * region: index of the region
 * addr: base address of the region
 * size: size of the region in bytes
 * texscb: TEX and SCB bit field
 */
static int mpu_unlock_region(uint8_t region, uint32_t addr, uint32_t size,
			     uint8_t texscb)
{
	return mpu_config_region(region, addr, size,
				 MPU_ATTR_RW_RW | texscb, 1);
}

void mpu_enable(void)
{
	MPU_CTRL |= MPU_CTRL_PRIVDEFEN | MPU_CTRL_HFNMIENA | MPU_CTRL_ENABLE;
}

void mpu_disable(void)
{
	MPU_CTRL &= ~(MPU_CTRL_PRIVDEFEN | MPU_CTRL_HFNMIENA | MPU_CTRL_ENABLE);
}

uint32_t mpu_get_type(void)
{
	return MPU_TYPE;
}

int mpu_protect_ram(void)
{
	int ret;
	ret = mpu_lock_region(REGION_IRAM, CONFIG_RAM_BASE,
			      CONFIG_RAM_SIZE, MPU_ATTR_INTERNAL_SRAM);
	if (ret != EC_SUCCESS)
		return ret;
	ret = mpu_unlock_region(
		REGION_IRAM_TEXT, (uint32_t)&__iram_text_start,
		(uint32_t)(&__iram_text_end - &__iram_text_start),
		MPU_ATTR_INTERNAL_SRAM);
	return ret;
}

int mpu_lock_ro_flash(void)
{
	return mpu_lock_region(REGION_FLASH_MEMORY, CONFIG_FW_RO_OFF,
			       CONFIG_FW_IMAGE_SIZE, MPU_ATTR_FLASH_MEMORY);
}

int mpu_lock_rw_flash(void)
{
	return mpu_lock_region(REGION_FLASH_MEMORY, CONFIG_FW_RW_OFF,
			       CONFIG_FW_RW_SIZE, MPU_ATTR_FLASH_MEMORY);
}

int mpu_pre_init(void)
{
	int i;

	if (mpu_get_type() != 0x00000800)
		return EC_ERROR_UNIMPLEMENTED;

	mpu_disable();
	for (i = 0; i < 8; ++i)
		mpu_config_region(i, CONFIG_RAM_BASE, CONFIG_RAM_SIZE, 0, 0);

	return EC_SUCCESS;
}