1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2019 Kyle Evans <kevans@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 /* 32 * This file contains facilities for runtime determination of address space 33 * mappings for use in DMA/mailbox interactions. This is only used for the 34 * arm64 SoC because the 32-bit SoC used the same mappings. 35 */ 36 37 #include <sys/types.h> 38 #include <sys/systm.h> 39 40 #include <dev/ofw/openfirm.h> 41 #include <dev/ofw/ofw_bus.h> 42 #include <dev/ofw/ofw_bus_subr.h> 43 44 #include <machine/bus.h> 45 46 #include <arm/broadcom/bcm2835/bcm2835_vcbus.h> 47 48 /* 49 * This structure describes mappings that need to take place when transforming 50 * ARM core addresses into vcbus addresses for use with the DMA/mailbox 51 * interfaces. Currently, we only deal with peripheral/SDRAM address spaces 52 * here. 53 * 54 * The SDRAM address space is consistently mapped starting at 0 and extends to 55 * the size of the installed SDRAM. 56 * 57 * Peripherals are mapped further up at spots that vary per-SOC. 58 */ 59 struct bcm283x_memory_mapping { 60 vm_paddr_t armc_start; 61 vm_paddr_t armc_size; 62 vm_paddr_t vcbus_start; 63 }; 64 65 static struct bcm283x_memory_mapping bcm2835_memmap[] = { 66 { 67 /* SDRAM */ 68 .armc_start = 0x00000000, 69 .armc_size = BCM2835_ARM_IO_BASE, 70 .vcbus_start = BCM2835_VCBUS_SDRAM_BASE, 71 }, 72 { 73 /* Peripherals */ 74 .armc_start = BCM2835_ARM_IO_BASE, 75 .armc_size = BCM28XX_ARM_IO_SIZE, 76 .vcbus_start = BCM2835_VCBUS_IO_BASE, 77 }, 78 { 0, 0, 0 }, 79 }; 80 81 static struct bcm283x_memory_mapping bcm2836_memmap[] = { 82 { 83 /* SDRAM */ 84 .armc_start = 0x00000000, 85 .armc_size = BCM2836_ARM_IO_BASE, 86 .vcbus_start = BCM2836_VCBUS_SDRAM_BASE, 87 }, 88 { 89 /* Peripherals */ 90 .armc_start = BCM2836_ARM_IO_BASE, 91 .armc_size = BCM28XX_ARM_IO_SIZE, 92 .vcbus_start = BCM2836_VCBUS_IO_BASE, 93 }, 94 { 0, 0, 0 }, 95 }; 96 97 static struct bcm283x_memory_mapping bcm2837_memmap[] = { 98 { 99 /* SDRAM */ 100 .armc_start = 0x00000000, 101 .armc_size = BCM2837_ARM_IO_BASE, 102 .vcbus_start = BCM2837_VCBUS_SDRAM_BASE, 103 }, 104 { 105 /* Peripherals */ 106 .armc_start = BCM2837_ARM_IO_BASE, 107 .armc_size = BCM28XX_ARM_IO_SIZE, 108 .vcbus_start = BCM2837_VCBUS_IO_BASE, 109 }, 110 { 0, 0, 0 }, 111 }; 112 113 /* 114 * The BCM2838 supports up to 4GB of SDRAM, but unfortunately we can still only 115 * map the first 1GB into the "legacy master view" (vcbus) address space. Thus, 116 * peripherals can still only access the lower end of SDRAM. For this reason, 117 * we also capture the main-peripheral busdma restriction below. 118 */ 119 static struct bcm283x_memory_mapping bcm2838_memmap[] = { 120 { 121 /* SDRAM */ 122 .armc_start = 0x00000000, 123 .armc_size = 0x40000000, 124 .vcbus_start = BCM2838_VCBUS_SDRAM_BASE, 125 }, 126 { 127 /* Main peripherals */ 128 .armc_start = BCM2838_ARM_IO_BASE, 129 .armc_size = BCM28XX_ARM_IO_SIZE, 130 .vcbus_start = BCM2838_VCBUS_IO_BASE, 131 }, 132 { 0, 0, 0 }, 133 }; 134 135 static struct bcm283x_memory_soc_cfg { 136 struct bcm283x_memory_mapping *memmap; 137 const char *soc_compat; 138 bus_addr_t busdma_lowaddr; 139 } bcm283x_memory_configs[] = { 140 /* Legacy */ 141 { 142 .memmap = bcm2835_memmap, 143 .soc_compat = "raspberrypi,model-b", 144 .busdma_lowaddr = BUS_SPACE_MAXADDR_32BIT, 145 }, 146 /* Modern */ 147 { 148 .memmap = bcm2835_memmap, 149 .soc_compat = "brcm,bcm2835", 150 .busdma_lowaddr = BUS_SPACE_MAXADDR_32BIT, 151 }, 152 /* Legacy */ 153 { 154 .memmap = bcm2836_memmap, 155 .soc_compat = "brcm,bcm2709", 156 .busdma_lowaddr = BUS_SPACE_MAXADDR_32BIT, 157 }, 158 /* Modern */ 159 { 160 .memmap = bcm2836_memmap, 161 .soc_compat = "brcm,bcm2836", 162 .busdma_lowaddr = BUS_SPACE_MAXADDR_32BIT, 163 }, 164 { 165 .memmap = bcm2837_memmap, 166 .soc_compat = "brcm,bcm2837", 167 .busdma_lowaddr = BUS_SPACE_MAXADDR_32BIT, 168 }, 169 { 170 .memmap = bcm2838_memmap, 171 .soc_compat = "brcm,bcm2711", 172 .busdma_lowaddr = BCM2838_PERIPH_MAXADDR, 173 }, 174 { 175 .memmap = bcm2838_memmap, 176 .soc_compat = "brcm,bcm2838", 177 .busdma_lowaddr = BCM2838_PERIPH_MAXADDR, 178 }, 179 }; 180 181 static struct bcm283x_memory_soc_cfg *booted_soc_memcfg; 182 183 static struct bcm283x_memory_soc_cfg * 184 bcm283x_get_current_memcfg(void) 185 { 186 phandle_t root; 187 int i; 188 189 /* We'll cache it once we decide, because it won't change per-boot. */ 190 if (booted_soc_memcfg != NULL) 191 return (booted_soc_memcfg); 192 193 KASSERT(nitems(bcm283x_memory_configs) != 0, 194 ("No SOC memory configurations enabled!")); 195 196 root = OF_finddevice("/"); 197 for (i = 0; i < nitems(bcm283x_memory_configs); ++i) { 198 booted_soc_memcfg = &bcm283x_memory_configs[i]; 199 if (bootverbose) 200 printf("Checking root against %s\n", 201 booted_soc_memcfg->soc_compat); 202 if (ofw_bus_node_is_compatible(root, 203 booted_soc_memcfg->soc_compat)) 204 return (booted_soc_memcfg); 205 } 206 207 /* 208 * The kernel doesn't fit the board; we can't really make a reasonable 209 * guess, as these SOC are different enough that something will blow up 210 * later. 211 */ 212 panic("No suitable SOC memory configuration found."); 213 } 214 215 #define BCM283X_MEMMAP_ISTERM(ent) \ 216 ((ent)->armc_start == 0 && (ent)->armc_size == 0 && \ 217 (ent)->vcbus_start == 0) 218 219 vm_paddr_t 220 bcm283x_armc_to_vcbus(vm_paddr_t pa) 221 { 222 struct bcm283x_memory_soc_cfg *cfg; 223 struct bcm283x_memory_mapping *map, *ment; 224 225 /* Guaranteed not NULL if we haven't panicked yet. */ 226 cfg = bcm283x_get_current_memcfg(); 227 map = cfg->memmap; 228 for (ment = map; !BCM283X_MEMMAP_ISTERM(ment); ++ment) { 229 if (pa >= ment->armc_start && 230 pa < ment->armc_start + ment->armc_size) { 231 return (pa - ment->armc_start) + ment->vcbus_start; 232 } 233 } 234 235 /* 236 * Assume 1:1 mapping for anything else, but complain about it on 237 * verbose boots. 238 */ 239 if (bootverbose) 240 printf("bcm283x_vcbus: No armc -> vcbus mapping found: %jx\n", 241 (uintmax_t)pa); 242 return (pa); 243 } 244 245 vm_paddr_t 246 bcm283x_vcbus_to_armc(vm_paddr_t vca) 247 { 248 struct bcm283x_memory_soc_cfg *cfg; 249 struct bcm283x_memory_mapping *map, *ment; 250 251 /* Guaranteed not NULL if we haven't panicked yet. */ 252 cfg = bcm283x_get_current_memcfg(); 253 map = cfg->memmap; 254 for (ment = map; !BCM283X_MEMMAP_ISTERM(ment); ++ment) { 255 if (vca >= ment->vcbus_start && 256 vca < ment->vcbus_start + ment->armc_size) { 257 return (vca - ment->vcbus_start) + ment->armc_start; 258 } 259 } 260 261 /* 262 * Assume 1:1 mapping for anything else, but complain about it on 263 * verbose boots. 264 */ 265 if (bootverbose) 266 printf("bcm283x_vcbus: No vcbus -> armc mapping found: %jx\n", 267 (uintmax_t)vca); 268 return (vca); 269 } 270 271 bus_addr_t 272 bcm283x_dmabus_peripheral_lowaddr(void) 273 { 274 struct bcm283x_memory_soc_cfg *cfg; 275 276 cfg = bcm283x_get_current_memcfg(); 277 return (cfg->busdma_lowaddr); 278 } 279