xref: /rk3399_ARM-atf/lib/utils/mem_region.c (revision 466bb285c6985027c75a230e39f2ae246fd07971)
143cbaf06SRoberto Vargas /*
2*466bb285SZelalem  * Copyright (c) 2017-2020, ARM Limited and Contributors. All rights reserved.
343cbaf06SRoberto Vargas  *
443cbaf06SRoberto Vargas  * SPDX-License-Identifier: BSD-3-Clause
543cbaf06SRoberto Vargas  */
643cbaf06SRoberto Vargas 
743cbaf06SRoberto Vargas #include <assert.h>
809d40e0eSAntonio Nino Diaz 
9bd9344f6SAntonio Nino Diaz #include <common/debug.h>
1009d40e0eSAntonio Nino Diaz #include <lib/utils.h>
11bd9344f6SAntonio Nino Diaz #include <lib/xlat_tables/xlat_tables_compat.h>
1243cbaf06SRoberto Vargas 
1343cbaf06SRoberto Vargas /*
1443cbaf06SRoberto Vargas  * All the regions defined in mem_region_t must have the following properties
1543cbaf06SRoberto Vargas  *
1643cbaf06SRoberto Vargas  * - Any contiguous regions must be merged into a single entry.
1743cbaf06SRoberto Vargas  * - The number of bytes of each region must be greater than zero.
1843cbaf06SRoberto Vargas  * - The calculation of the highest address within the region (base + nbytes-1)
1943cbaf06SRoberto Vargas  *   doesn't produce an overflow.
2043cbaf06SRoberto Vargas  *
2143cbaf06SRoberto Vargas  * These conditions must be fulfilled by the caller and they aren't checked
2243cbaf06SRoberto Vargas  * at runtime.
2343cbaf06SRoberto Vargas  */
2443cbaf06SRoberto Vargas 
2543cbaf06SRoberto Vargas /*
2643cbaf06SRoberto Vargas  * zero_normalmem all the regions defined in tbl.
2743cbaf06SRoberto Vargas  * It assumes that MMU is enabled and the memory is Normal memory.
2843cbaf06SRoberto Vargas  * tbl must be a valid pointer to a memory mem_region_t array,
2943cbaf06SRoberto Vargas  * nregions is the size of the array.
3043cbaf06SRoberto Vargas  */
3143cbaf06SRoberto Vargas void clear_mem_regions(mem_region_t *tbl, size_t nregions)
3243cbaf06SRoberto Vargas {
3343cbaf06SRoberto Vargas 	size_t i;
3443cbaf06SRoberto Vargas 
35*466bb285SZelalem 	assert(tbl != NULL);
3643cbaf06SRoberto Vargas 	assert(nregions > 0);
3743cbaf06SRoberto Vargas 
3843cbaf06SRoberto Vargas 	for (i = 0; i < nregions; i++) {
3943cbaf06SRoberto Vargas 		assert(tbl->nbytes > 0);
4043cbaf06SRoberto Vargas 		assert(!check_uptr_overflow(tbl->base, tbl->nbytes-1));
4143cbaf06SRoberto Vargas 		zero_normalmem((void *) (tbl->base), tbl->nbytes);
4243cbaf06SRoberto Vargas 		tbl++;
4343cbaf06SRoberto Vargas 	}
4443cbaf06SRoberto Vargas }
4543cbaf06SRoberto Vargas 
46638b034cSRoberto Vargas #if defined(PLAT_XLAT_TABLES_DYNAMIC)
47638b034cSRoberto Vargas /*
48638b034cSRoberto Vargas  * zero_normalmem all the regions defined in regions.
49638b034cSRoberto Vargas  * It assumes that MMU is enabled and the memory is Normal memory.
50638b034cSRoberto Vargas  * regions must be a valid pointer to a memory mem_region_t array,
51638b034cSRoberto Vargas  * nregions is the size of the array. va is the virtual address
52638b034cSRoberto Vargas  * where we want to map the physical pages that are going to
53638b034cSRoberto Vargas  * be cleared, and chunk is the amount of memory mapped and
54638b034cSRoberto Vargas  * cleared in every iteration.
55638b034cSRoberto Vargas  */
56c96f297fSRoberto Vargas void clear_map_dyn_mem_regions(struct mem_region *regions,
57638b034cSRoberto Vargas 			       size_t nregions,
58638b034cSRoberto Vargas 			       uintptr_t va,
59638b034cSRoberto Vargas 			       size_t chunk)
60638b034cSRoberto Vargas {
61638b034cSRoberto Vargas 	uintptr_t begin;
62638b034cSRoberto Vargas 	int r;
63638b034cSRoberto Vargas 	size_t size;
643a1b7b10SAntonio Nino Diaz 	const unsigned int attr = MT_MEMORY | MT_RW | MT_NS;
65638b034cSRoberto Vargas 
66638b034cSRoberto Vargas 	assert(regions != NULL);
67638b034cSRoberto Vargas 	assert(nregions > 0 && chunk > 0);
68638b034cSRoberto Vargas 
69638b034cSRoberto Vargas 	for ( ; nregions--; regions++) {
70638b034cSRoberto Vargas 		begin = regions->base;
71638b034cSRoberto Vargas 		size = regions->nbytes;
72638b034cSRoberto Vargas 		if ((begin & (chunk-1)) != 0 || (size & (chunk-1)) != 0) {
73638b034cSRoberto Vargas 			INFO("PSCI: Not correctly aligned region\n");
74638b034cSRoberto Vargas 			panic();
75638b034cSRoberto Vargas 		}
76638b034cSRoberto Vargas 
77638b034cSRoberto Vargas 		while (size > 0) {
78638b034cSRoberto Vargas 			r = mmap_add_dynamic_region(begin, va, chunk, attr);
79638b034cSRoberto Vargas 			if (r != 0) {
80638b034cSRoberto Vargas 				INFO("PSCI: mmap_add_dynamic_region failed with %d\n", r);
81638b034cSRoberto Vargas 				panic();
82638b034cSRoberto Vargas 			}
83638b034cSRoberto Vargas 
84638b034cSRoberto Vargas 			zero_normalmem((void *) va, chunk);
85638b034cSRoberto Vargas 
86638b034cSRoberto Vargas 			r = mmap_remove_dynamic_region(va, chunk);
87638b034cSRoberto Vargas 			if (r != 0) {
88638b034cSRoberto Vargas 				INFO("PSCI: mmap_remove_dynamic_region failed with %d\n", r);
89638b034cSRoberto Vargas 				panic();
90638b034cSRoberto Vargas 			}
91638b034cSRoberto Vargas 
92638b034cSRoberto Vargas 			begin += chunk;
93638b034cSRoberto Vargas 			size -= chunk;
94638b034cSRoberto Vargas 		}
95638b034cSRoberto Vargas 	}
96638b034cSRoberto Vargas }
97638b034cSRoberto Vargas #endif
98638b034cSRoberto Vargas 
9943cbaf06SRoberto Vargas /*
10043cbaf06SRoberto Vargas  * This function checks that a region (addr + nbytes-1) of memory is totally
10143cbaf06SRoberto Vargas  * covered by one of the regions defined in tbl.
10243cbaf06SRoberto Vargas  * tbl must be a valid pointer to a memory mem_region_t array, nregions
10343cbaf06SRoberto Vargas  * is the size of the array and the region described by addr and nbytes must
10443cbaf06SRoberto Vargas  * not generate an overflow.
10543cbaf06SRoberto Vargas  * Returns:
10643cbaf06SRoberto Vargas  *  -1 means that the region is not covered by any of the regions
10743cbaf06SRoberto Vargas  *     described in tbl.
10843cbaf06SRoberto Vargas  *   0 the region (addr + nbytes-1) is covered by one of the regions described
10943cbaf06SRoberto Vargas  *     in tbl
11043cbaf06SRoberto Vargas  */
11143cbaf06SRoberto Vargas int mem_region_in_array_chk(mem_region_t *tbl, size_t nregions,
11243cbaf06SRoberto Vargas 			    uintptr_t addr, size_t nbytes)
11343cbaf06SRoberto Vargas {
11443cbaf06SRoberto Vargas 	uintptr_t region_start, region_end, start, end;
11543cbaf06SRoberto Vargas 	size_t i;
11643cbaf06SRoberto Vargas 
117*466bb285SZelalem 	assert(tbl != NULL);
11843cbaf06SRoberto Vargas 	assert(nbytes > 0);
11943cbaf06SRoberto Vargas 	assert(!check_uptr_overflow(addr, nbytes-1));
12043cbaf06SRoberto Vargas 
12143cbaf06SRoberto Vargas 	region_start = addr;
12243cbaf06SRoberto Vargas 	region_end = addr + (nbytes - 1);
12343cbaf06SRoberto Vargas 	for (i = 0; i < nregions; i++) {
12443cbaf06SRoberto Vargas 		assert(tbl->nbytes > 0);
12543cbaf06SRoberto Vargas 		assert(!check_uptr_overflow(tbl->base, tbl->nbytes-1));
12643cbaf06SRoberto Vargas 		start = tbl->base;
12743cbaf06SRoberto Vargas 		end = start + (tbl->nbytes - 1);
12843cbaf06SRoberto Vargas 		if (region_start >= start && region_end <= end)
12943cbaf06SRoberto Vargas 			return 0;
13043cbaf06SRoberto Vargas 		tbl++;
13143cbaf06SRoberto Vargas 	}
13243cbaf06SRoberto Vargas 
13343cbaf06SRoberto Vargas 	return -1;
13443cbaf06SRoberto Vargas }
135