1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Device tree based initialization code for reserved memory. 4 * 5 * Copyright (c) 2013, 2015 The Linux Foundation. All Rights Reserved. 6 * Copyright (c) 2013,2014 Samsung Electronics Co., Ltd. 7 * http://www.samsung.com 8 * Author: Marek Szyprowski <m.szyprowski@samsung.com> 9 * Author: Josh Cartwright <joshc@codeaurora.org> 10 */ 11 12 #define pr_fmt(fmt) "OF: reserved mem: " fmt 13 14 #include <linux/err.h> 15 #include <linux/of.h> 16 #include <linux/of_fdt.h> 17 #include <linux/of_platform.h> 18 #include <linux/mm.h> 19 #include <linux/sizes.h> 20 #include <linux/of_reserved_mem.h> 21 #include <linux/sort.h> 22 #include <linux/slab.h> 23 24 #define MAX_RESERVED_REGIONS 32 25 static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS]; 26 static int reserved_mem_count; 27 28 #if defined(CONFIG_HAVE_MEMBLOCK) 29 #include <linux/memblock.h> 30 int __init __weak early_init_dt_alloc_reserved_memory_arch(phys_addr_t size, 31 phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap, 32 phys_addr_t *res_base) 33 { 34 phys_addr_t base; 35 /* 36 * We use __memblock_alloc_base() because memblock_alloc_base() 37 * panic()s on allocation failure. 38 */ 39 end = !end ? MEMBLOCK_ALLOC_ANYWHERE : end; 40 base = __memblock_alloc_base(size, align, end); 41 if (!base) 42 return -ENOMEM; 43 44 /* 45 * Check if the allocated region fits in to start..end window 46 */ 47 if (base < start) { 48 memblock_free(base, size); 49 return -ENOMEM; 50 } 51 52 *res_base = base; 53 if (nomap) 54 return memblock_remove(base, size); 55 return 0; 56 } 57 #else 58 int __init __weak early_init_dt_alloc_reserved_memory_arch(phys_addr_t size, 59 phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap, 60 phys_addr_t *res_base) 61 { 62 pr_err("Reserved memory not supported, ignoring region 0x%llx%s\n", 63 size, nomap ? " (nomap)" : ""); 64 return -ENOSYS; 65 } 66 #endif 67 68 /** 69 * res_mem_save_node() - save fdt node for second pass initialization 70 */ 71 void __init fdt_reserved_mem_save_node(unsigned long node, const char *uname, 72 phys_addr_t base, phys_addr_t size) 73 { 74 struct reserved_mem *rmem = &reserved_mem[reserved_mem_count]; 75 76 if (reserved_mem_count == ARRAY_SIZE(reserved_mem)) { 77 pr_err("not enough space all defined regions.\n"); 78 return; 79 } 80 81 rmem->fdt_node = node; 82 rmem->name = uname; 83 rmem->base = base; 84 rmem->size = size; 85 86 reserved_mem_count++; 87 return; 88 } 89 90 /** 91 * res_mem_alloc_size() - allocate reserved memory described by 'size', 'align' 92 * and 'alloc-ranges' properties 93 */ 94 static int __init __reserved_mem_alloc_size(unsigned long node, 95 const char *uname, phys_addr_t *res_base, phys_addr_t *res_size) 96 { 97 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32); 98 phys_addr_t start = 0, end = 0; 99 phys_addr_t base = 0, align = 0, size; 100 int len; 101 const __be32 *prop; 102 int nomap; 103 int ret; 104 105 prop = of_get_flat_dt_prop(node, "size", &len); 106 if (!prop) 107 return -EINVAL; 108 109 if (len != dt_root_size_cells * sizeof(__be32)) { 110 pr_err("invalid size property in '%s' node.\n", uname); 111 return -EINVAL; 112 } 113 size = dt_mem_next_cell(dt_root_size_cells, &prop); 114 115 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL; 116 117 prop = of_get_flat_dt_prop(node, "alignment", &len); 118 if (prop) { 119 if (len != dt_root_addr_cells * sizeof(__be32)) { 120 pr_err("invalid alignment property in '%s' node.\n", 121 uname); 122 return -EINVAL; 123 } 124 align = dt_mem_next_cell(dt_root_addr_cells, &prop); 125 } 126 127 /* Need adjust the alignment to satisfy the CMA requirement */ 128 if (IS_ENABLED(CONFIG_CMA) 129 && of_flat_dt_is_compatible(node, "shared-dma-pool") 130 && of_get_flat_dt_prop(node, "reusable", NULL) 131 && !of_get_flat_dt_prop(node, "no-map", NULL)) { 132 unsigned long order = 133 max_t(unsigned long, MAX_ORDER - 1, pageblock_order); 134 135 align = max(align, (phys_addr_t)PAGE_SIZE << order); 136 } 137 138 prop = of_get_flat_dt_prop(node, "alloc-ranges", &len); 139 if (prop) { 140 141 if (len % t_len != 0) { 142 pr_err("invalid alloc-ranges property in '%s', skipping node.\n", 143 uname); 144 return -EINVAL; 145 } 146 147 base = 0; 148 149 while (len > 0) { 150 start = dt_mem_next_cell(dt_root_addr_cells, &prop); 151 end = start + dt_mem_next_cell(dt_root_size_cells, 152 &prop); 153 154 ret = early_init_dt_alloc_reserved_memory_arch(size, 155 align, start, end, nomap, &base); 156 if (ret == 0) { 157 pr_debug("allocated memory for '%s' node: base %pa, size %ld MiB\n", 158 uname, &base, 159 (unsigned long)size / SZ_1M); 160 break; 161 } 162 len -= t_len; 163 } 164 165 } else { 166 ret = early_init_dt_alloc_reserved_memory_arch(size, align, 167 0, 0, nomap, &base); 168 if (ret == 0) 169 pr_debug("allocated memory for '%s' node: base %pa, size %ld MiB\n", 170 uname, &base, (unsigned long)size / SZ_1M); 171 } 172 173 if (base == 0) { 174 pr_info("failed to allocate memory for node '%s'\n", uname); 175 return -ENOMEM; 176 } 177 178 *res_base = base; 179 *res_size = size; 180 181 return 0; 182 } 183 184 static const struct of_device_id __rmem_of_table_sentinel 185 __used __section(__reservedmem_of_table_end); 186 187 /** 188 * res_mem_init_node() - call region specific reserved memory init code 189 */ 190 static int __init __reserved_mem_init_node(struct reserved_mem *rmem) 191 { 192 extern const struct of_device_id __reservedmem_of_table[]; 193 const struct of_device_id *i; 194 195 for (i = __reservedmem_of_table; i < &__rmem_of_table_sentinel; i++) { 196 reservedmem_of_init_fn initfn = i->data; 197 const char *compat = i->compatible; 198 199 if (!of_flat_dt_is_compatible(rmem->fdt_node, compat)) 200 continue; 201 202 if (initfn(rmem) == 0) { 203 pr_info("initialized node %s, compatible id %s\n", 204 rmem->name, compat); 205 return 0; 206 } 207 } 208 return -ENOENT; 209 } 210 211 static int __init __rmem_cmp(const void *a, const void *b) 212 { 213 const struct reserved_mem *ra = a, *rb = b; 214 215 if (ra->base < rb->base) 216 return -1; 217 218 if (ra->base > rb->base) 219 return 1; 220 221 return 0; 222 } 223 224 static void __init __rmem_check_for_overlap(void) 225 { 226 int i; 227 228 if (reserved_mem_count < 2) 229 return; 230 231 sort(reserved_mem, reserved_mem_count, sizeof(reserved_mem[0]), 232 __rmem_cmp, NULL); 233 for (i = 0; i < reserved_mem_count - 1; i++) { 234 struct reserved_mem *this, *next; 235 236 this = &reserved_mem[i]; 237 next = &reserved_mem[i + 1]; 238 if (!(this->base && next->base)) 239 continue; 240 if (this->base + this->size > next->base) { 241 phys_addr_t this_end, next_end; 242 243 this_end = this->base + this->size; 244 next_end = next->base + next->size; 245 pr_err("OVERLAP DETECTED!\n%s (%pa--%pa) overlaps with %s (%pa--%pa)\n", 246 this->name, &this->base, &this_end, 247 next->name, &next->base, &next_end); 248 } 249 } 250 } 251 252 /** 253 * fdt_init_reserved_mem - allocate and init all saved reserved memory regions 254 */ 255 void __init fdt_init_reserved_mem(void) 256 { 257 int i; 258 259 /* check for overlapping reserved regions */ 260 __rmem_check_for_overlap(); 261 262 for (i = 0; i < reserved_mem_count; i++) { 263 struct reserved_mem *rmem = &reserved_mem[i]; 264 unsigned long node = rmem->fdt_node; 265 int len; 266 const __be32 *prop; 267 int err = 0; 268 269 prop = of_get_flat_dt_prop(node, "phandle", &len); 270 if (!prop) 271 prop = of_get_flat_dt_prop(node, "linux,phandle", &len); 272 if (prop) 273 rmem->phandle = of_read_number(prop, len/4); 274 275 if (rmem->size == 0) 276 err = __reserved_mem_alloc_size(node, rmem->name, 277 &rmem->base, &rmem->size); 278 if (err == 0) 279 __reserved_mem_init_node(rmem); 280 } 281 } 282 283 static inline struct reserved_mem *__find_rmem(struct device_node *node) 284 { 285 unsigned int i; 286 287 if (!node->phandle) 288 return NULL; 289 290 for (i = 0; i < reserved_mem_count; i++) 291 if (reserved_mem[i].phandle == node->phandle) 292 return &reserved_mem[i]; 293 return NULL; 294 } 295 296 struct rmem_assigned_device { 297 struct device *dev; 298 struct reserved_mem *rmem; 299 struct list_head list; 300 }; 301 302 static LIST_HEAD(of_rmem_assigned_device_list); 303 static DEFINE_MUTEX(of_rmem_assigned_device_mutex); 304 305 /** 306 * of_reserved_mem_device_init_by_idx() - assign reserved memory region to 307 * given device 308 * @dev: Pointer to the device to configure 309 * @np: Pointer to the device_node with 'reserved-memory' property 310 * @idx: Index of selected region 311 * 312 * This function assigns respective DMA-mapping operations based on reserved 313 * memory region specified by 'memory-region' property in @np node to the @dev 314 * device. When driver needs to use more than one reserved memory region, it 315 * should allocate child devices and initialize regions by name for each of 316 * child device. 317 * 318 * Returns error code or zero on success. 319 */ 320 int of_reserved_mem_device_init_by_idx(struct device *dev, 321 struct device_node *np, int idx) 322 { 323 struct rmem_assigned_device *rd; 324 struct device_node *target; 325 struct reserved_mem *rmem; 326 int ret; 327 328 if (!np || !dev) 329 return -EINVAL; 330 331 target = of_parse_phandle(np, "memory-region", idx); 332 if (!target) 333 return -ENODEV; 334 335 rmem = __find_rmem(target); 336 of_node_put(target); 337 338 if (!rmem || !rmem->ops || !rmem->ops->device_init) 339 return -EINVAL; 340 341 rd = kmalloc(sizeof(struct rmem_assigned_device), GFP_KERNEL); 342 if (!rd) 343 return -ENOMEM; 344 345 ret = rmem->ops->device_init(rmem, dev); 346 if (ret == 0) { 347 rd->dev = dev; 348 rd->rmem = rmem; 349 350 mutex_lock(&of_rmem_assigned_device_mutex); 351 list_add(&rd->list, &of_rmem_assigned_device_list); 352 mutex_unlock(&of_rmem_assigned_device_mutex); 353 /* ensure that dma_ops is set for virtual devices 354 * using reserved memory 355 */ 356 of_dma_configure(dev, np); 357 358 dev_info(dev, "assigned reserved memory node %s\n", rmem->name); 359 } else { 360 kfree(rd); 361 } 362 363 return ret; 364 } 365 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_idx); 366 367 /** 368 * of_reserved_mem_device_release() - release reserved memory device structures 369 * @dev: Pointer to the device to deconfigure 370 * 371 * This function releases structures allocated for memory region handling for 372 * the given device. 373 */ 374 void of_reserved_mem_device_release(struct device *dev) 375 { 376 struct rmem_assigned_device *rd; 377 struct reserved_mem *rmem = NULL; 378 379 mutex_lock(&of_rmem_assigned_device_mutex); 380 list_for_each_entry(rd, &of_rmem_assigned_device_list, list) { 381 if (rd->dev == dev) { 382 rmem = rd->rmem; 383 list_del(&rd->list); 384 kfree(rd); 385 break; 386 } 387 } 388 mutex_unlock(&of_rmem_assigned_device_mutex); 389 390 if (!rmem || !rmem->ops || !rmem->ops->device_release) 391 return; 392 393 rmem->ops->device_release(rmem, dev); 394 } 395 EXPORT_SYMBOL_GPL(of_reserved_mem_device_release); 396 397 /** 398 * of_reserved_mem_lookup() - acquire reserved_mem from a device node 399 * @np: node pointer of the desired reserved-memory region 400 * 401 * This function allows drivers to acquire a reference to the reserved_mem 402 * struct based on a device node handle. 403 * 404 * Returns a reserved_mem reference, or NULL on error. 405 */ 406 struct reserved_mem *of_reserved_mem_lookup(struct device_node *np) 407 { 408 const char *name; 409 int i; 410 411 if (!np->full_name) 412 return NULL; 413 414 name = kbasename(np->full_name); 415 for (i = 0; i < reserved_mem_count; i++) 416 if (!strcmp(reserved_mem[i].name, name)) 417 return &reserved_mem[i]; 418 419 return NULL; 420 } 421 EXPORT_SYMBOL_GPL(of_reserved_mem_lookup); 422