Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 2 | #ifndef _LINUX_LMB_H |
| 3 | #define _LINUX_LMB_H |
| 4 | #ifdef __KERNEL__ |
| 5 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 6 | #include <alist.h> |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 7 | #include <asm/types.h> |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 8 | #include <asm/u-boot.h> |
Sughosh Ganu | 26d7fa1 | 2024-08-26 17:29:17 +0530 | [diff] [blame] | 9 | #include <linux/bitops.h> |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 10 | |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 11 | /* |
| 12 | * Logical memory blocks. |
| 13 | * |
| 14 | * Copyright (C) 2001 Peter Bergner, IBM Corp. |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 15 | */ |
| 16 | |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 17 | /** |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 18 | * enum lmb_flags - definition of memory region attributes |
| 19 | * @LMB_NONE: no special request |
| 20 | * @LMB_NOMAP: don't add to mmu configuration |
| 21 | */ |
| 22 | enum lmb_flags { |
Sughosh Ganu | 26d7fa1 | 2024-08-26 17:29:17 +0530 | [diff] [blame] | 23 | LMB_NONE = 0, |
| 24 | LMB_NOMAP = BIT(1), |
Sughosh Ganu | 50e2727 | 2024-08-26 17:29:19 +0530 | [diff] [blame] | 25 | LMB_NOOVERWRITE = BIT(2), |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | /** |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 29 | * struct lmb_region - Description of one region. |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 30 | * |
Heinrich Schuchardt | e6923f0 | 2021-11-14 08:43:07 +0100 | [diff] [blame] | 31 | * @base: Base address of the region. |
| 32 | * @size: Size of the region |
| 33 | * @flags: memory region attributes |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 34 | */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 35 | struct lmb_region { |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 36 | phys_addr_t base; |
| 37 | phys_size_t size; |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 38 | enum lmb_flags flags; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 39 | }; |
| 40 | |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 41 | /** |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 42 | * struct lmb - The LMB structure |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 43 | * |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 44 | * @free_mem: List of free memory regions |
| 45 | * @used_mem: List of used/reserved memory regions |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 46 | */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 47 | struct lmb { |
| 48 | struct alist free_mem; |
| 49 | struct alist used_mem; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 50 | }; |
| 51 | |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 52 | /** |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 53 | * lmb_init() - Initialise the LMB module |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 54 | * |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 55 | * Initialise the LMB lists needed for keeping the memory map. There |
| 56 | * are two lists, in form of alloced list data structure. One for the |
| 57 | * available memory, and one for the used memory. Initialise the two |
| 58 | * lists as part of board init. Add memory to the available memory |
| 59 | * list and reserve common areas by adding them to the used memory |
| 60 | * list. |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 61 | * |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 62 | * Return: 0 on success, -ve on error |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 63 | */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 64 | int lmb_init(void); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 65 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 66 | void lmb_init_and_reserve_range(phys_addr_t base, phys_size_t size, |
| 67 | void *fdt_blob); |
Sughosh Ganu | 65597fa | 2024-08-26 17:29:23 +0530 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * lmb_add_memory() - Add memory range for LMB allocations |
| 71 | * |
| 72 | * Add the entire available memory range to the pool of memory that |
| 73 | * can be used by the LMB module for allocations. |
| 74 | * |
| 75 | * Return: None |
| 76 | */ |
| 77 | void lmb_add_memory(void); |
| 78 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 79 | long lmb_add(phys_addr_t base, phys_size_t size); |
| 80 | long lmb_reserve(phys_addr_t base, phys_size_t size); |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 81 | /** |
| 82 | * lmb_reserve_flags - Reserve one region with a specific flags bitfield. |
| 83 | * |
Heinrich Schuchardt | e6923f0 | 2021-11-14 08:43:07 +0100 | [diff] [blame] | 84 | * @base: base address of the memory region |
| 85 | * @size: size of the memory region |
| 86 | * @flags: flags for the memory region |
| 87 | * Return: 0 if OK, > 0 for coalesced region or a negative error code. |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 88 | */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 89 | long lmb_reserve_flags(phys_addr_t base, phys_size_t size, |
| 90 | enum lmb_flags flags); |
| 91 | phys_addr_t lmb_alloc(phys_size_t size, ulong align); |
| 92 | phys_addr_t lmb_alloc_base(phys_size_t size, ulong align, phys_addr_t max_addr); |
| 93 | phys_addr_t lmb_alloc_addr(phys_addr_t base, phys_size_t size); |
| 94 | phys_size_t lmb_get_free_size(phys_addr_t addr); |
Heinrich Schuchardt | 5411fb7 | 2023-08-12 19:09:32 +0200 | [diff] [blame] | 95 | |
| 96 | /** |
Heinrich Schuchardt | 5411fb7 | 2023-08-12 19:09:32 +0200 | [diff] [blame] | 97 | * lmb_is_reserved_flags() - test if address is in reserved region with flag bits set |
| 98 | * |
| 99 | * The function checks if a reserved region comprising @addr exists which has |
| 100 | * all flag bits set which are set in @flags. |
Patrick Delaunay | db2c9aa | 2021-05-07 14:50:30 +0200 | [diff] [blame] | 101 | * |
Heinrich Schuchardt | e6923f0 | 2021-11-14 08:43:07 +0100 | [diff] [blame] | 102 | * @addr: address to be tested |
Heinrich Schuchardt | 5411fb7 | 2023-08-12 19:09:32 +0200 | [diff] [blame] | 103 | * @flags: bitmap with bits to be tested |
| 104 | * Return: 1 if matching reservation exists, 0 otherwise |
Patrick Delaunay | db2c9aa | 2021-05-07 14:50:30 +0200 | [diff] [blame] | 105 | */ |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 106 | int lmb_is_reserved_flags(phys_addr_t addr, int flags); |
Heinrich Schuchardt | 5411fb7 | 2023-08-12 19:09:32 +0200 | [diff] [blame] | 107 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 108 | long lmb_free(phys_addr_t base, phys_size_t size); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 109 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 110 | void lmb_dump_all(void); |
| 111 | void lmb_dump_all_force(void); |
| 112 | |
| 113 | void board_lmb_reserve(void); |
| 114 | void arch_lmb_reserve(void); |
| 115 | void arch_lmb_reserve_generic(ulong sp, ulong end, ulong align); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 116 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 117 | struct lmb *lmb_get(void); |
| 118 | int lmb_push(struct lmb *store); |
| 119 | void lmb_pop(struct lmb *store); |
Mike Frysinger | a0dadf8 | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 120 | |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 121 | #endif /* __KERNEL__ */ |
| 122 | |
| 123 | #endif /* _LINUX_LMB_H */ |