blob: b6ab26ec79c03d98a50f9ad26a4b54799653d589 [file] [log] [blame]
Sandrine Bailleux7659a262016-07-05 09:55:03 +01001/*
Roberto Vargas550eb082018-01-05 16:00:05 +00002 * Copyright (c) 2016-2018, ARM Limited and Contributors. All rights reserved.
Sandrine Bailleux7659a262016-07-05 09:55:03 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Sandrine Bailleux7659a262016-07-05 09:55:03 +01005 */
6
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +00007#ifndef UTILS_H
8#define UTILS_H
Sandrine Bailleux7659a262016-07-05 09:55:03 +01009
Douglas Raillard21362a92016-12-02 13:51:54 +000010/*
11 * C code should be put in this part of the header to avoid breaking ASM files
12 * or linker scripts including it.
13 */
14#if !(defined(__LINKER__) || defined(__ASSEMBLY__))
15
Antonio Nino Diaz4b32e622018-08-16 16:52:57 +010016#include <stdint.h>
Douglas Raillard21362a92016-12-02 13:51:54 +000017
Masahiro Yamadaff3ecb62018-01-16 23:15:38 +090018typedef struct mem_region {
Roberto Vargasc5907702017-08-03 08:56:38 +010019 uintptr_t base;
20 size_t nbytes;
21} mem_region_t;
22
23/*
24 * zero_normalmem all the regions defined in tbl.
25 */
26void clear_mem_regions(mem_region_t *tbl, size_t nregions);
27
Roberto Vargas550eb082018-01-05 16:00:05 +000028/*
29 * zero_normalmem all the regions defined in region. It dynamically
30 * maps chunks of 'chunk_size' in 'va' virtual address and clears them.
31 * For this reason memory regions must be multiple of chunk_size and
32 * must be aligned to it as well. chunk_size and va can be selected
33 * in a way that they minimize the number of entries used in the
34 * translation tables.
35 */
Roberto Vargas85664f52018-02-12 12:36:17 +000036void clear_map_dyn_mem_regions(struct mem_region *regions,
Roberto Vargas550eb082018-01-05 16:00:05 +000037 size_t nregions,
38 uintptr_t va,
Roberto Vargas85664f52018-02-12 12:36:17 +000039 size_t chunk);
Roberto Vargasc5907702017-08-03 08:56:38 +010040
41/*
42 * checks that a region (addr + nbytes-1) of memory is totally covered by
43 * one of the regions defined in tbl. Caller must ensure that (addr+nbytes-1)
44 * doesn't overflow.
45 */
46int mem_region_in_array_chk(mem_region_t *tbl, size_t nregions,
47 uintptr_t addr, size_t nbytes);
48
Douglas Raillard21362a92016-12-02 13:51:54 +000049/*
50 * Fill a region of normal memory of size "length" in bytes with zero bytes.
51 *
52 * WARNING: This function can only operate on normal memory. This means that
53 * the MMU must be enabled when using this function. Otherwise, use
54 * zeromem.
55 */
56void zero_normalmem(void *mem, u_register_t length);
57
58/*
59 * Fill a region of memory of size "length" in bytes with null bytes.
60 *
61 * Unlike zero_normalmem, this function has no restriction on the type of
62 * memory targeted and can be used for any device memory as well as normal
63 * memory. This function must be used instead of zero_normalmem when MMU is
64 * disabled.
65 *
66 * NOTE: When data cache and MMU are enabled, prefer zero_normalmem for faster
67 * zeroing.
68 */
69void zeromem(void *mem, u_register_t length);
Soby Mathew12cdcd22018-10-12 16:26:20 +010070
71/*
72 * Utility function to return the address of a symbol. By default, the
73 * compiler generates adr/adrp instruction pair to return the reference
74 * to the symbol and this utility is used to override this compiler
75 * generated to code to use `ldr` instruction.
76 *
77 * This helps when Position Independent Executable needs to reference a symbol
78 * which is constant and does not depend on the execute address of the binary.
79 */
80#define DEFINE_LOAD_SYM_ADDR(_name) \
81static inline u_register_t load_addr_## _name(void) \
82{ \
83 u_register_t v; \
84 /* Create a void reference to silence compiler */ \
85 (void) _name; \
86 __asm__ volatile ("ldr %0, =" #_name : "=r" (v)); \
87 return v; \
88}
89
90/* Helper to invoke the function defined by DEFINE_LOAD_SYM_ADDR() */
91#define LOAD_ADDR_OF(_name) (typeof(_name) *) load_addr_## _name()
92
Douglas Raillard21362a92016-12-02 13:51:54 +000093#endif /* !(defined(__LINKER__) || defined(__ASSEMBLY__)) */
94
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +000095#endif /* UTILS_H */