xref: /rk3399_rockchip-uboot/lib/sysmem.c (revision 556bbbe4367a4bc302d7b99160ecfc4eefa35414)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd
4  */
5 
6 #include <common.h>
7 #include <sysmem.h>
8 #include <lmb.h>
9 #include <malloc.h>
10 #include <asm/io.h>
11 
12 DECLARE_GLOBAL_DATA_PTR;
13 
14 #define SYSMEM_MAGIC		0x4D454D53	/* "SMEM" */
15 
16 #define LMB_ALLOC_ANYWHERE	0		/* sync with lmb.c */
17 #define SYSMEM_ALLOC_NO_ALIGN	1
18 #define SYSMEM_ALLOC_ANYWHERE	2
19 
20 #define SYSMEM_I(fmt, args...)	printf("Sysmem: "fmt, ##args)
21 #define SYSMEM_W(fmt, args...)	printf("Sysmem Warn: "fmt, ##args)
22 #define SYSMEM_E(fmt, args...)	printf("Sysmem Error: "fmt, ##args)
23 #define SYSMEM_D(fmt, args...)	 debug("Sysmem Debug: "fmt, ##args)
24 
25 struct memcheck {
26 	uint32_t magic;
27 };
28 
29 /* Global for platform, must in data section */
30 struct sysmem plat_sysmem __section(".data") = {
31 	.has_initf = false,
32 	.has_initr = false,
33 };
34 
35 bool sysmem_has_init(void)
36 {
37 	return gd->flags & GD_FLG_RELOC ?
38 	       plat_sysmem.has_initr : plat_sysmem.has_initf;
39 }
40 
41 void sysmem_dump(void)
42 {
43 	struct sysmem *sysmem = &plat_sysmem;
44 	struct lmb *lmb = &sysmem->lmb;
45 	struct memblock *mem;
46 	struct memcheck *check;
47 	struct list_head *node;
48 	ulong memory_size = 0;
49 	ulong reserved_size = 0;
50 	ulong allocated_size = 0;
51 	bool overflow = false;
52 	ulong i;
53 
54 	if (!sysmem_has_init())
55 		return;
56 
57 	printf("\nsysmem_dump_all:\n");
58 
59 	/* Memory pool */
60 	printf("    --------------------------------------------------------------------\n");
61 	for (i = 0; i < lmb->memory.cnt; i++) {
62 		memory_size += lmb->memory.region[i].size;
63 		printf("    memory.rgn[%ld].addr     = 0x%08lx - 0x%08lx (size: 0x%08lx)\n", i,
64 		       (ulong)lmb->memory.region[i].base,
65 		       (ulong)lmb->memory.region[i].base +
66 		       (ulong)lmb->memory.region[i].size,
67 		       (ulong)lmb->memory.region[i].size);
68 	}
69 	printf("\n    memory.total	   = 0x%08lx (%ld MiB. %ld KiB)\n",
70 	       (ulong)memory_size,
71 	       SIZE_MB((ulong)memory_size),
72 	       SIZE_KB((ulong)memory_size));
73 
74 	/* Allocated */
75 	i = 0;
76 	printf("    --------------------------------------------------------------------\n");
77 	list_for_each(node, &sysmem->allocated_head) {
78 		mem = list_entry(node, struct memblock, node);
79 		allocated_size += mem->size;
80 		if (mem->attr.flags & M_ATTR_OFC) {
81 			check = (struct memcheck *)
82 				(mem->base + mem->size - sizeof(*check));
83 			overflow = (check->magic != SYSMEM_MAGIC);
84 		} else if (mem->attr.flags & M_ATTR_HOFC) {
85 			check = (struct memcheck *)
86 				(mem->base - sizeof(*check));
87 			overflow = (check->magic != SYSMEM_MAGIC);
88 		} else {
89 			overflow = false;
90 		}
91 
92 		printf("    allocated.rgn[%ld].name  = \"%s\" %s\n",
93 		       i, mem->attr.name, overflow ? "	   <Overflow!>" : "");
94 		printf("		    .addr  = 0x%08lx - 0x%08lx (size: 0x%08lx)\n",
95 		       (ulong)mem->base, (ulong)(mem->base + mem->size),
96 		       (ulong)mem->size);
97 		i++;
98 	}
99 
100 	printf("\n    malloc_r: %d MiB, malloc_f: %d KiB\n",
101 	       SIZE_MB(CONFIG_SYS_MALLOC_LEN), SIZE_KB(CONFIG_SYS_MALLOC_F_LEN));
102 	printf("\n    allocated.total	   = 0x%08lx (%ld MiB. %ld KiB)\n",
103 	       (ulong)allocated_size,
104 	       SIZE_MB((ulong)allocated_size),
105 	       SIZE_KB((ulong)allocated_size));
106 
107 	/* LMB core reserved */
108 	printf("    --------------------------------------------------------------------\n");
109 	reserved_size = 0;
110 	for (i = 0; i < lmb->reserved.cnt; i++) {
111 		reserved_size += lmb->reserved.region[i].size;
112 		printf("    LMB.reserved[%ld].addr   = 0x%08lx - 0x%08lx (size: 0x%08lx)\n", i,
113 		       (ulong)lmb->reserved.region[i].base,
114 		       (ulong)lmb->reserved.region[i].base +
115 		       (ulong)lmb->reserved.region[i].size,
116 		       (ulong)lmb->reserved.region[i].size);
117 	}
118 
119 	printf("\n    reserved.core.total	   = 0x%08lx (%ld MiB. %ld KiB)\n",
120 	       (ulong)reserved_size,
121 	       SIZE_MB((ulong)reserved_size),
122 	       SIZE_KB((ulong)reserved_size));
123 	printf("    --------------------------------------------------------------------\n\n");
124 }
125 
126 static inline int sysmem_is_overlap(phys_addr_t base1, phys_size_t size1,
127 				    phys_addr_t base2, phys_size_t size2)
128 {
129 	return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
130 }
131 
132 static int sysmem_add(phys_addr_t base, phys_size_t size)
133 {
134 	struct sysmem *sysmem = &plat_sysmem;
135 	int ret;
136 
137 	if (!size)
138 		return -EINVAL;
139 
140 	ret = lmb_add(&sysmem->lmb, base, size);
141 	if (ret < 0)
142 		SYSMEM_E("Failed to add sysmem at 0x%08lx for 0x%08lx size\n",
143 			 (ulong)base, (ulong)size);
144 
145 	return (ret >= 0) ? 0 : ret;
146 }
147 
148 static const char *sysmem_alias2name(const char *name, int *id)
149 {
150 	const char *alias;
151 	int n, i, j;
152 	int match = 0;
153 
154 	for (i = 0; i < MEMBLK_ID_MAX; i++) {
155 		/* Pirmary name */
156 		if (mem_attr[i].name && !strcasecmp(mem_attr[i].name, name)) {
157 			match = 1;
158 			goto finish;
159 		}
160 
161 		/* Alias name */
162 		alias = mem_attr[i].alias[0];
163 		if (!alias)
164 			continue;
165 
166 		n = ARRAY_SIZE(mem_attr[i].alias);
167 		for (j = 0; j < n; j++, alias++) {
168 			if (alias && !strcasecmp(alias, name)) {
169 				match = 1;
170 				goto finish;
171 			}
172 		}
173 	}
174 
175 finish:
176 	if (match) {
177 		*id = i;
178 		return mem_attr[i].name;
179 	}
180 
181 	return name;
182 }
183 
184 static void *sysmem_alloc_align_base(enum memblk_id id,
185 				     const char *mem_name,
186 				     phys_addr_t base,
187 				     phys_size_t size,
188 				     ulong align)
189 {
190 	struct sysmem *sysmem = &plat_sysmem;
191 	struct memblk_attr attr;
192 	struct memblock *mem;
193 	struct memcheck *check;
194 	struct list_head *node;
195 	const char *name;
196 	phys_addr_t paddr;
197 	phys_addr_t alloc_base;
198 	phys_size_t alloc_size;
199 	phys_addr_t bank_base;
200 	phys_size_t bank_size;
201 	bool req_overlap = false; /* Only for kernel reserved-memory */
202 	int i;
203 
204 	if (!sysmem_has_init())
205 		goto out;
206 
207 	if (id == MEMBLK_ID_BY_NAME || id == MEMBLK_ID_FDT_RESV) {
208 		if (!mem_name) {
209 			SYSMEM_E("NULL name for alloc sysmem\n");
210 			goto out;
211 		} else if (id == MEMBLK_ID_FDT_RESV) {
212 
213 			/*
214 			 * Allow fdt reserved memory to overlap with the region
215 			 * only used in U-Boot, like: stack, fastboot, u-boot...
216 			 * these regions are marked as M_ATTR_OVERLAP in flags.
217 			 *
218 			 * Here we check whether it overlaps with others, if
219 			 * so, set req_overlap as true.
220 			 */
221 			for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
222 				if (!gd->bd->bi_dram[i].size)
223 					continue;
224 
225 				bank_base = gd->bd->bi_dram[i].start;
226 				bank_size = gd->bd->bi_dram[i].size;
227 				if (sysmem_is_overlap(base, size,
228 						      bank_base, bank_size)) {
229 					req_overlap = true;
230 					break;
231 				}
232 			}
233 
234 			/*
235 			 * If this request region is out size of all available
236 			 * region, ignore and return success.
237 			 */
238 			if (!req_overlap)
239 				return (void *)base;
240 		}
241 
242 		/* Find name, id and attr by outer mem_name */
243 		name = sysmem_alias2name(mem_name, (int *)&id);
244 		attr = mem_attr[id];
245 		if (!attr.name)
246 			attr.name = strdup(name);
247 	} else if (id > MEMBLK_ID_UNK && id < MEMBLK_ID_MAX) {
248 		attr = mem_attr[id];
249 		name = attr.name;
250 
251 		/*
252 		 * Fixup base and place right after U-Boot stack, adding a lot
253 		 * of space(4KB) maybe safer.
254 		 */
255 		if ((id == MEMBLK_ID_AVB_ANDROID) &&
256 		    (base == SYSMEM_ALLOC_ANYWHERE)) {
257 			base = gd->start_addr_sp -
258 					CONFIG_SYS_STACK_SIZE - size - 0x1000;
259 		/*
260 		 * So far, we use M_ATTR_PEEK for uncompress kernel alloc, and
261 		 * for ARMv8 enabling AArch32 mode, the ATF is still AArch64
262 		 * and ocuppies 0~1MB and shmem 1~2M. So let's ignore the region
263 		 * which overlap with them.
264 		 */
265 		} else if (attr.flags & M_ATTR_PEEK) {
266 			if (base <= gd->bd->bi_dram[0].start)
267 				base = gd->bd->bi_dram[0].start;
268 		}
269 	} else {
270 		SYSMEM_E("Unsupport memblk id %d for alloc sysmem\n", id);
271 		goto out;
272 	}
273 
274 	if (!size) {
275 		SYSMEM_E("\"%s\" size is 0 for alloc sysmem\n", name);
276 		goto out;
277 	}
278 
279 	/*
280 	 * Some modules use "sysmem_alloc()" to alloc region for storage
281 	 * read/write buffer, it should be aligned to cacheline size. eg: AVB.
282 	 *
283 	 * Aligned down to cacheline size if not aligned, otherwise the tail
284 	 * of region maybe overflow.
285 	 */
286 	if (attr.flags & M_ATTR_CACHELINE_ALIGN &&
287 	    !IS_ALIGNED(base, ARCH_DMA_MINALIGN)) {
288 		base = ALIGN(base, ARCH_DMA_MINALIGN);
289 		base -= ARCH_DMA_MINALIGN;
290 	}
291 
292 	if (!IS_ALIGNED(base, 4)) {
293 		SYSMEM_E("\"%s\" base=0x%08lx is not 4-byte aligned\n",
294 			 name, (ulong)base);
295 		goto out;
296 	}
297 
298 	/* Must be 4-byte aligned */
299 	size = ALIGN(size, 4);
300 
301 	SYSMEM_D("Enter alloc: \"%s\" 0x%08lx - 0x%08lx\n",
302 		 name, (ulong)base, (ulong)(base + size));
303 
304 	/* Already allocated ? */
305 	list_for_each(node, &sysmem->allocated_head) {
306 		mem = list_entry(node, struct memblock, node);
307 		SYSMEM_D("Has allcated: %s, 0x%08lx - 0x%08lx\n",
308 			 mem->attr.name, (ulong)mem->base,
309 			 (ulong)(mem->base + mem->size));
310 		if (!strcmp(mem->attr.name, name)) {
311 			/* Allow double alloc for same but smaller region */
312 			if (mem->base <= base && mem->size >= size)
313 				return (void *)base;
314 
315 			SYSMEM_E("Failed to double alloc for existence \"%s\"\n", name);
316 			goto out;
317 		} else if (sysmem_is_overlap(mem->base, mem->size, base, size)) {
318 			/*
319 			 * If this new alloc region expects overlap and the old
320 			 * region is also allowed to be overlap, just do reserve.
321 			 */
322 			if (req_overlap && mem->attr.flags & M_ATTR_OVERLAP) {
323 				if (lmb_reserve(&sysmem->lmb, base, size))
324 					SYSMEM_E("Failed to overlap alloc \"%s\" "
325 						 "at 0x%08lx - 0x%08lx\n",
326 						 name, (ulong)base,
327 						 (ulong)(base + size));
328 				return (void *)base;
329 			}
330 
331 			SYSMEM_E("\"%s\" (0x%08lx - 0x%08lx) alloc is "
332 				 "overlap with existence \"%s\" (0x%08lx - "
333 				 "0x%08lx)\n",
334 				 name, (ulong)base, (ulong)(base + size),
335 				 mem->attr.name, (ulong)mem->base,
336 				 (ulong)(mem->base + mem->size));
337 			goto out;
338 		}
339 	}
340 
341 	/* Add overflow check magic ? */
342 	if (attr.flags & M_ATTR_OFC)
343 		alloc_size = size + sizeof(*check);
344 	else
345 		alloc_size = size;
346 
347 	/* Alloc anywhere ? */
348 	if (base == SYSMEM_ALLOC_ANYWHERE)
349 		alloc_base = LMB_ALLOC_ANYWHERE;
350 	else
351 		alloc_base = base + alloc_size;	/* LMB is align down alloc mechanism */
352 
353 	paddr = lmb_alloc_base(&sysmem->lmb, alloc_size, align, alloc_base);
354 	if (paddr) {
355 		if ((paddr == base) || (base == SYSMEM_ALLOC_ANYWHERE)) {
356 			mem = malloc(sizeof(*mem));
357 			if (!mem) {
358 				SYSMEM_E("No memory for \"%s\" alloc sysmem\n", name);
359 				goto out;
360 			}
361 
362 			mem->base = paddr;
363 			mem->size = alloc_size;
364 			mem->attr = attr;
365 			sysmem->allocated_cnt++;
366 			list_add_tail(&mem->node, &sysmem->allocated_head);
367 
368 			/* Add overflow check magic */
369 			if (mem->attr.flags & M_ATTR_OFC) {
370 				check = (struct memcheck *)(paddr + size);
371 				check->magic = SYSMEM_MAGIC;
372 			} else if (mem->attr.flags & M_ATTR_HOFC) {
373 				check = (struct memcheck *)(paddr - sizeof(*check));
374 				check->magic = SYSMEM_MAGIC;
375 			}
376 		} else {
377 			SYSMEM_E("Failed to alloc \"%s\" expect at 0x%08lx - 0x%08lx "
378 				 "but at 0x%08lx - x%08lx\n",
379 				 name, (ulong)base, (ulong)(base + size),
380 				 (ulong)paddr, (ulong)(paddr + size));
381 			/* Free what we don't want allocated region */
382 			if (lmb_free(&sysmem->lmb, paddr, alloc_size) < 0)
383 				SYSMEM_E("Failed to free \"%s\"\n", name);
384 
385 			goto out;
386 		}
387 	} else {
388 		SYSMEM_E("Failed to alloc \"%s\" at 0x%08lx - 0x%08lx\n",
389 			 name, (ulong)base, (ulong)(base + size));
390 		goto out;
391 	}
392 
393 	SYSMEM_D("Exit alloc: \"%s\", paddr=0x%08lx, size=0x%08lx, align=0x%x, anywhere=%d\n",
394 		 name, (ulong)paddr, (ulong)size, (u32)align, !base);
395 
396 	return (void *)paddr;
397 
398 out:
399 	/*
400 	 * Why: base + sizeof(ulong) ?
401 	 * It's a not standard way to handle the case: the input base is 0.
402 	 */
403 	if (base == 0)
404 		base = base + sizeof(ulong);
405 
406 	return (attr.flags & M_ATTR_PEEK) ? (void *)base : NULL;
407 }
408 
409 void *sysmem_alloc(enum memblk_id id, phys_size_t size)
410 {
411 	void *paddr;
412 
413 	paddr = sysmem_alloc_align_base(id,
414 					NULL,
415 					SYSMEM_ALLOC_ANYWHERE,
416 					size,
417 					SYSMEM_ALLOC_NO_ALIGN);
418 	if (!paddr)
419 		sysmem_dump();
420 
421 	return paddr;
422 }
423 
424 void *sysmem_alloc_base(enum memblk_id id, phys_addr_t base, phys_size_t size)
425 {
426 	void *paddr;
427 
428 	paddr = sysmem_alloc_align_base(id,
429 					NULL,
430 					base,
431 					size,
432 					SYSMEM_ALLOC_NO_ALIGN);
433 	if (!paddr)
434 		sysmem_dump();
435 
436 	return paddr;
437 }
438 
439 void *sysmem_alloc_base_by_name(const char *name,
440 				phys_addr_t base, phys_size_t size)
441 {
442 	void *paddr;
443 
444 	paddr = sysmem_alloc_align_base(MEMBLK_ID_BY_NAME,
445 					name,
446 					base,
447 					size,
448 					SYSMEM_ALLOC_NO_ALIGN);
449 	if (!paddr)
450 		sysmem_dump();
451 
452 	return paddr;
453 }
454 
455 void *sysmem_fdt_reserve_alloc_base(const char *name,
456 				    phys_addr_t base, phys_size_t size)
457 {
458 	void *paddr;
459 
460 	paddr = sysmem_alloc_align_base(MEMBLK_ID_FDT_RESV,
461 					name,
462 					base,
463 					size,
464 					SYSMEM_ALLOC_NO_ALIGN);
465 	if (!paddr)
466 		sysmem_dump();
467 
468 	return paddr;
469 }
470 
471 bool sysmem_can_alloc(phys_size_t base, phys_size_t size)
472 {
473 	struct sysmem *sysmem = &plat_sysmem;
474 	phys_addr_t alloc_base;
475 	phys_addr_t paddr;
476 	int ret;
477 
478 	if (!sysmem_has_init())
479 		return false;
480 
481 	/* LMB is align down alloc mechanism */
482 	alloc_base = base + size;
483 	paddr = __lmb_alloc_base(&sysmem->lmb,
484 				 size,
485 				 SYSMEM_ALLOC_NO_ALIGN,
486 				 alloc_base);
487 	if (paddr) {
488 		/* If free failed, return false */
489 		ret = lmb_free(&sysmem->lmb, base, size);
490 		if (ret < 0) {
491 			SYSMEM_E("Can't free at 0x%08lx - 0x%08lx, ret=%d\n",
492 				 (ulong)base, (ulong)(base + size), ret);
493 			return false;
494 		}
495 	} else {
496 		SYSMEM_D("Can't alloc at 0x%08lx - 0x%08lx\n",
497 			 (ulong)base, (ulong)(base + size));
498 	}
499 
500 	return (paddr == base) ? true : false;
501 }
502 
503 int sysmem_free(phys_addr_t base)
504 {
505 	struct sysmem *sysmem = &plat_sysmem;
506 	struct memblock *mem;
507 	struct list_head *node;
508 	int ret, found = 0;
509 
510 	if (!sysmem_has_init())
511 		return -ENOSYS;
512 
513 	/* Find existence */
514 	list_for_each(node, &sysmem->allocated_head) {
515 		mem = list_entry(node, struct memblock, node);
516 		if (mem->base == base) {
517 			found = 1;
518 			break;
519 		}
520 	}
521 
522 	if (!found) {
523 		SYSMEM_E("Failed to free no allocated sysmem at 0x%08lx\n",
524 			 (ulong)base);
525 		return -EINVAL;
526 	}
527 
528 	ret = lmb_free(&sysmem->lmb, mem->base, mem->size);
529 	if (ret >= 0) {
530 		SYSMEM_D("Free: \"%s\" 0x%08lx - 0x%08lx\n",
531 			 mem->attr.name, (ulong)mem->base,
532 			 (ulong)(mem->base + mem->size));
533 		sysmem->allocated_cnt--;
534 		list_del(&mem->node);
535 		free(mem);
536 	} else {
537 		SYSMEM_E("Failed to free \"%s\" at 0x%08lx\n",
538 			 mem->attr.name, (ulong)base);
539 	}
540 
541 	return (ret >= 0) ? 0 : ret;
542 }
543 
544 int sysmem_initr(void)
545 {
546 	return sysmem_init();
547 }
548 
549 int sysmem_init(void)
550 {
551 	struct sysmem *sysmem = &plat_sysmem;
552 	phys_addr_t mem_start;
553 	phys_size_t mem_size;
554 	int ret;
555 
556 	lmb_init(&sysmem->lmb);
557 	INIT_LIST_HEAD(&sysmem->allocated_head);
558 	sysmem->allocated_cnt = 0;
559 	if (gd->flags & GD_FLG_RELOC) {
560 		sysmem->has_initr = true;
561 	} else {
562 		SYSMEM_I("init\n");
563 		sysmem->has_initf = true;
564 	}
565 
566 	/* Add all available system memory */
567 #ifdef CONFIG_NR_DRAM_BANKS
568 	int i;
569 
570 	for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
571 		if (!gd->bd->bi_dram[i].size)
572 			continue;
573 
574 		ret = sysmem_add(gd->bd->bi_dram[i].start,
575 				 gd->bd->bi_dram[i].size);
576 		if (ret) {
577 			SYSMEM_E("Failed to add sysmem from bi_dram[%d]\n", i);
578 			goto fail;
579 		}
580 	}
581 #else
582 	mem_start = env_get_bootm_low();
583 	mem_size = env_get_bootm_size();
584 	ret = sysmem_add(mem_start, mem_size);
585 	if (ret) {
586 		SYSMEM_E("Failed to add sysmem from bootm_low/size\n");
587 		goto fail;
588 	}
589 #endif
590 	/* Reserved for board */
591 	ret = board_sysmem_reserve(sysmem);
592 	if (ret) {
593 		SYSMEM_E("Failed to reserve sysmem for board\n");
594 		goto fail;
595 	}
596 
597 	/* Reserved for U-boot framework: 'reserve_xxx()' */
598 	mem_start = gd->start_addr_sp;
599 	mem_size = gd->ram_top - mem_start;
600 	if (!sysmem_alloc_base(MEMBLK_ID_UBOOT, mem_start, mem_size)) {
601 		SYSMEM_E("Failed to reserve sysmem for U-Boot framework\n");
602 		ret = -ENOMEM;
603 		goto fail;
604 	}
605 
606 	/* Reserved for U-Boot stack */
607 	mem_start = gd->start_addr_sp - CONFIG_SYS_STACK_SIZE;
608 	mem_size = CONFIG_SYS_STACK_SIZE;
609 	if (!sysmem_alloc_base(MEMBLK_ID_STACK, mem_start, mem_size)) {
610 		SYSMEM_E("Failed to reserve sysmem for stack\n");
611 		ret = -ENOMEM;
612 		goto fail;
613 	}
614 
615 	return 0;
616 
617 fail:
618 	if (ret && !(gd->flags & GD_FLG_RELOC)) {
619 		sysmem_dump();
620 		SYSMEM_W("Maybe malloc size %d MiB is too large?\n\n",
621 			 SIZE_MB(CONFIG_SYS_MALLOC_LEN));
622 	}
623 
624 	return ret;
625 }
626 
627 __weak int board_sysmem_reserve(struct sysmem *sysmem)
628 {
629 	/* please define platform specific board_sysmem_reserve() */
630 	return 0;
631 }
632 
633 static int do_dump_sysmem(cmd_tbl_t *cmdtp, int flag,
634 			  int argc, char *const argv[])
635 {
636 	sysmem_dump();
637 	return 0;
638 }
639 
640 U_BOOT_CMD(
641 	dump_sysmem, 1, 1, do_dump_sysmem,
642 	"Dump sysmem layout",
643 	""
644 );
645