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