aboutsummaryrefslogtreecommitdiff
path: root/sys/arm/broadcom/bcm2835/bcm2835_vcbus.c
blob: 5a350b74108075e16cd7b46b24ec55705e8af222 (plain) (blame)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2019 Kyle Evans <kevans@FreeBSD.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

/*
 * This file contains facilities for runtime determination of address space
 * mappings for use in DMA/mailbox interactions.  This is only used for the
 * arm64 SoC because the 32-bit SoC used the same mappings.
 */

#include <sys/types.h>
#include <sys/systm.h>

#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>

#include <machine/bus.h>

#include <arm/broadcom/bcm2835/bcm2835_vcbus.h>

/*
 * This structure describes mappings that need to take place when transforming
 * ARM core addresses into vcbus addresses for use with the DMA/mailbox
 * interfaces.  Currently, we only deal with peripheral/SDRAM address spaces
 * here.
 *
 * The SDRAM address space is consistently mapped starting at 0 and extends to
 * the size of the installed SDRAM.
 *
 * Peripherals are mapped further up at spots that vary per-SOC.
 */
struct bcm283x_memory_mapping {
	vm_paddr_t	armc_start;
	vm_paddr_t	armc_size;
	vm_paddr_t	vcbus_start;
};

static struct bcm283x_memory_mapping bcm2835_memmap[] = {
	{
		/* SDRAM */
		.armc_start = 0x00000000,
		.armc_size = BCM2835_ARM_IO_BASE,
		.vcbus_start = BCM2835_VCBUS_SDRAM_BASE,
	},
	{
		/* Peripherals */
		.armc_start = BCM2835_ARM_IO_BASE,
		.armc_size  = BCM28XX_ARM_IO_SIZE,
		.vcbus_start = BCM2835_VCBUS_IO_BASE,
	},
	{ 0, 0, 0 },
};

static struct bcm283x_memory_mapping bcm2836_memmap[] = {
	{
		/* SDRAM */
		.armc_start = 0x00000000,
		.armc_size = BCM2836_ARM_IO_BASE,
		.vcbus_start = BCM2836_VCBUS_SDRAM_BASE,
	},
	{
		/* Peripherals */
		.armc_start = BCM2836_ARM_IO_BASE,
		.armc_size  = BCM28XX_ARM_IO_SIZE,
		.vcbus_start = BCM2836_VCBUS_IO_BASE,
	},
	{ 0, 0, 0 },
};

static struct bcm283x_memory_mapping bcm2837_memmap[] = {
	{
		/* SDRAM */
		.armc_start = 0x00000000,
		.armc_size = BCM2837_ARM_IO_BASE,
		.vcbus_start = BCM2837_VCBUS_SDRAM_BASE,
	},
	{
		/* Peripherals */
		.armc_start = BCM2837_ARM_IO_BASE,
		.armc_size  = BCM28XX_ARM_IO_SIZE,
		.vcbus_start = BCM2837_VCBUS_IO_BASE,
	},
	{ 0, 0, 0 },
};

/*
 * The BCM2838 supports up to 4GB of SDRAM, but unfortunately we can still only
 * map the first 1GB into the "legacy master view" (vcbus) address space.  Thus,
 * peripherals can still only access the lower end of SDRAM.  For this reason,
 * we also capture the main-peripheral busdma restriction below.
 */
static struct bcm283x_memory_mapping bcm2838_memmap[] = {
	{
		/* SDRAM */
		.armc_start = 0x00000000,
		.armc_size = 0x40000000,
		.vcbus_start = BCM2838_VCBUS_SDRAM_BASE,
	},
	{
		/* Main peripherals */
		.armc_start = BCM2838_ARM_IO_BASE,
		.armc_size = BCM28XX_ARM_IO_SIZE,
		.vcbus_start = BCM2838_VCBUS_IO_BASE,
	},
	{ 0, 0, 0 },
};

static struct bcm283x_memory_soc_cfg {
	struct bcm283x_memory_mapping	*memmap;
	const char			*soc_compat;
	bus_addr_t			 busdma_lowaddr;
} bcm283x_memory_configs[] = {
	/* Legacy */
	{
		.memmap = bcm2835_memmap,
		.soc_compat = "raspberrypi,model-b",
		.busdma_lowaddr = BUS_SPACE_MAXADDR_32BIT,
	},
	/* Modern */
	{
		.memmap = bcm2835_memmap,
		.soc_compat = "brcm,bcm2835",
		.busdma_lowaddr = BUS_SPACE_MAXADDR_32BIT,
	},
	/* Legacy */
	{
		.memmap = bcm2836_memmap,
		.soc_compat = "brcm,bcm2709",
		.busdma_lowaddr = BUS_SPACE_MAXADDR_32BIT,
	},
	/* Modern */
	{
		.memmap = bcm2836_memmap,
		.soc_compat = "brcm,bcm2836",
		.busdma_lowaddr = BUS_SPACE_MAXADDR_32BIT,
	},
	{
		.memmap = bcm2837_memmap,
		.soc_compat = "brcm,bcm2837",
		.busdma_lowaddr = BUS_SPACE_MAXADDR_32BIT,
	},
	{
		.memmap = bcm2838_memmap,
		.soc_compat = "brcm,bcm2711",
		.busdma_lowaddr = BCM2838_PERIPH_MAXADDR,
	},
	{
		.memmap = bcm2838_memmap,
		.soc_compat = "brcm,bcm2838",
		.busdma_lowaddr = BCM2838_PERIPH_MAXADDR,
	},
};

static struct bcm283x_memory_soc_cfg *booted_soc_memcfg;

static struct bcm283x_memory_soc_cfg *
bcm283x_get_current_memcfg(void)
{
	phandle_t root;
	int i;

	/* We'll cache it once we decide, because it won't change per-boot. */
	if (booted_soc_memcfg != NULL)
		return (booted_soc_memcfg);

	KASSERT(nitems(bcm283x_memory_configs) != 0,
	    ("No SOC memory configurations enabled!"));

	root = OF_finddevice("/");
	for (i = 0; i < nitems(bcm283x_memory_configs); ++i) {
		booted_soc_memcfg = &bcm283x_memory_configs[i];
		if (bootverbose)
			printf("Checking root against %s\n",
			    booted_soc_memcfg->soc_compat);
		if (ofw_bus_node_is_compatible(root,
		    booted_soc_memcfg->soc_compat))
			return (booted_soc_memcfg);
	}

	/*
	 * The kernel doesn't fit the board; we can't really make a reasonable
	 * guess, as these SOC are different enough that something will blow up
	 * later.
	 */
	panic("No suitable SOC memory configuration found.");
}

#define	BCM283X_MEMMAP_ISTERM(ent)	\
    ((ent)->armc_start == 0 && (ent)->armc_size == 0 && \
    (ent)->vcbus_start == 0)

vm_paddr_t
bcm283x_armc_to_vcbus(vm_paddr_t pa)
{
	struct bcm283x_memory_soc_cfg *cfg;
	struct bcm283x_memory_mapping *map, *ment;

	/* Guaranteed not NULL if we haven't panicked yet. */
	cfg = bcm283x_get_current_memcfg();
	map = cfg->memmap;
	for (ment = map; !BCM283X_MEMMAP_ISTERM(ment); ++ment) {
		if (pa >= ment->armc_start &&
		    pa < ment->armc_start + ment->armc_size) {
			return (pa - ment->armc_start) + ment->vcbus_start;
		}
	}

	/*
	 * Assume 1:1 mapping for anything else, but complain about it on
	 * verbose boots.
	 */
	if (bootverbose)
		printf("bcm283x_vcbus: No armc -> vcbus mapping found: %jx\n",
		    (uintmax_t)pa);
	return (pa);
}

vm_paddr_t
bcm283x_vcbus_to_armc(vm_paddr_t vca)
{
	struct bcm283x_memory_soc_cfg *cfg;
	struct bcm283x_memory_mapping *map, *ment;

	/* Guaranteed not NULL if we haven't panicked yet. */
	cfg = bcm283x_get_current_memcfg();
	map = cfg->memmap;
	for (ment = map; !BCM283X_MEMMAP_ISTERM(ment); ++ment) {
		if (vca >= ment->vcbus_start &&
		    vca < ment->vcbus_start + ment->armc_size) {
			return (vca - ment->vcbus_start) + ment->armc_start;
		}
	}

	/*
	 * Assume 1:1 mapping for anything else, but complain about it on
	 * verbose boots.
	 */
	if (bootverbose)
		printf("bcm283x_vcbus: No vcbus -> armc mapping found: %jx\n",
		    (uintmax_t)vca);
	return (vca);
}

bus_addr_t
bcm283x_dmabus_peripheral_lowaddr(void)
{
	struct bcm283x_memory_soc_cfg *cfg;

	cfg = bcm283x_get_current_memcfg();
	return (cfg->busdma_lowaddr);
}