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 | |
| 6 | #include <asm/types.h> |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 7 | #include <asm/u-boot.h> |
| 8 | |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 9 | /* |
| 10 | * Logical memory blocks. |
| 11 | * |
| 12 | * Copyright (C) 2001 Peter Bergner, IBM Corp. |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 13 | */ |
| 14 | |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 15 | /** |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 16 | * enum lmb_flags - definition of memory region attributes |
| 17 | * @LMB_NONE: no special request |
| 18 | * @LMB_NOMAP: don't add to mmu configuration |
| 19 | */ |
| 20 | enum lmb_flags { |
| 21 | LMB_NONE = 0x0, |
| 22 | LMB_NOMAP = 0x4, |
| 23 | }; |
| 24 | |
| 25 | /** |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 26 | * struct lmb_property - Description of one region. |
| 27 | * |
Heinrich Schuchardt | e6923f0 | 2021-11-14 08:43:07 +0100 | [diff] [blame] | 28 | * @base: Base address of the region. |
| 29 | * @size: Size of the region |
| 30 | * @flags: memory region attributes |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 31 | */ |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 32 | struct lmb_property { |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 33 | phys_addr_t base; |
| 34 | phys_size_t size; |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 35 | enum lmb_flags flags; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 36 | }; |
| 37 | |
Patrick Delaunay | 479de71 | 2023-03-22 19:12:25 +0100 | [diff] [blame] | 38 | /* |
| 39 | * For regions size management, see LMB configuration in KConfig |
| 40 | * all the #if test are done with CONFIG_LMB_USE_MAX_REGIONS (boolean) |
| 41 | * |
| 42 | * case 1. CONFIG_LMB_USE_MAX_REGIONS is defined (legacy mode) |
| 43 | * => CONFIG_LMB_MAX_REGIONS is used to configure the region size, |
| 44 | * directly in the array lmb_region.region[], with the same |
| 45 | * configuration for memory and reserved regions. |
| 46 | * |
| 47 | * case 2. CONFIG_LMB_USE_MAX_REGIONS is not defined, the size of each |
| 48 | * region is configurated *independently* with |
| 49 | * => CONFIG_LMB_MEMORY_REGIONS: struct lmb.memory_regions |
| 50 | * => CONFIG_LMB_RESERVED_REGIONS: struct lmb.reserved_regions |
| 51 | * lmb_region.region is only a pointer to the correct buffer, |
| 52 | * initialized in lmb_init(). This configuration is useful to manage |
| 53 | * more reserved memory regions with CONFIG_LMB_RESERVED_REGIONS. |
| 54 | */ |
| 55 | |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 56 | /** |
| 57 | * struct lmb_region - Description of a set of region. |
| 58 | * |
| 59 | * @cnt: Number of regions. |
| 60 | * @max: Size of the region array, max value of cnt. |
| 61 | * @region: Array of the region properties |
| 62 | */ |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 63 | struct lmb_region { |
| 64 | unsigned long cnt; |
Patrick Delaunay | 8c69fc2 | 2021-03-10 10:16:27 +0100 | [diff] [blame] | 65 | unsigned long max; |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 66 | #if IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS) |
Patrick Delaunay | 8b82930 | 2021-03-10 10:16:29 +0100 | [diff] [blame] | 67 | struct lmb_property region[CONFIG_LMB_MAX_REGIONS]; |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 68 | #else |
| 69 | struct lmb_property *region; |
| 70 | #endif |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 71 | }; |
| 72 | |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 73 | /** |
| 74 | * struct lmb - Logical memory block handle. |
| 75 | * |
| 76 | * Clients provide storage for Logical memory block (lmb) handles. |
| 77 | * The content of the structure is managed by the lmb library. |
| 78 | * A lmb struct is initialized by lmb_init() functions. |
| 79 | * The lmb struct is passed to all other lmb APIs. |
| 80 | * |
| 81 | * @memory: Description of memory regions. |
| 82 | * @reserved: Description of reserved regions. |
| 83 | * @memory_regions: Array of the memory regions (statically allocated) |
| 84 | * @reserved_regions: Array of the reserved regions (statically allocated) |
| 85 | */ |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 86 | struct lmb { |
| 87 | struct lmb_region memory; |
| 88 | struct lmb_region reserved; |
Patrick Delaunay | 479de71 | 2023-03-22 19:12:25 +0100 | [diff] [blame] | 89 | #if !IS_ENABLED(CONFIG_LMB_USE_MAX_REGIONS) |
Patrick Delaunay | 71cc9c5 | 2021-03-10 10:16:31 +0100 | [diff] [blame] | 90 | struct lmb_property memory_regions[CONFIG_LMB_MEMORY_REGIONS]; |
| 91 | struct lmb_property reserved_regions[CONFIG_LMB_RESERVED_REGIONS]; |
| 92 | #endif |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 93 | }; |
| 94 | |
Heinrich Schuchardt | 9dccccd | 2021-11-14 08:52:48 +0100 | [diff] [blame] | 95 | void lmb_init(struct lmb *lmb); |
| 96 | void lmb_init_and_reserve(struct lmb *lmb, struct bd_info *bd, void *fdt_blob); |
| 97 | void lmb_init_and_reserve_range(struct lmb *lmb, phys_addr_t base, |
| 98 | phys_size_t size, void *fdt_blob); |
| 99 | long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size); |
| 100 | long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size); |
Patrick Delaunay | e11c908 | 2021-05-07 14:50:29 +0200 | [diff] [blame] | 101 | /** |
| 102 | * lmb_reserve_flags - Reserve one region with a specific flags bitfield. |
| 103 | * |
Heinrich Schuchardt | e6923f0 | 2021-11-14 08:43:07 +0100 | [diff] [blame] | 104 | * @lmb: the logical memory block struct |
| 105 | * @base: base address of the memory region |
| 106 | * @size: size of the memory region |
| 107 | * @flags: flags for the memory region |
| 108 | * 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] | 109 | */ |
| 110 | long lmb_reserve_flags(struct lmb *lmb, phys_addr_t base, |
| 111 | phys_size_t size, enum lmb_flags flags); |
Heinrich Schuchardt | 9dccccd | 2021-11-14 08:52:48 +0100 | [diff] [blame] | 112 | phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align); |
| 113 | phys_addr_t lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align, |
| 114 | phys_addr_t max_addr); |
| 115 | phys_addr_t __lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align, |
| 116 | phys_addr_t max_addr); |
| 117 | phys_addr_t lmb_alloc_addr(struct lmb *lmb, phys_addr_t base, phys_size_t size); |
| 118 | phys_size_t lmb_get_free_size(struct lmb *lmb, phys_addr_t addr); |
Heinrich Schuchardt | 5411fb7 | 2023-08-12 19:09:32 +0200 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | * lmb_is_reserved() - test if address is in reserved region |
| 122 | * |
| 123 | * The function checks if a reserved region comprising @addr exists. |
| 124 | * |
| 125 | * @lmb: the logical memory block struct |
| 126 | * @addr: address to be tested |
| 127 | * Return: 1 if reservation exists, 0 otherwise |
| 128 | */ |
Heinrich Schuchardt | 9dccccd | 2021-11-14 08:52:48 +0100 | [diff] [blame] | 129 | int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr); |
Heinrich Schuchardt | 5411fb7 | 2023-08-12 19:09:32 +0200 | [diff] [blame] | 130 | |
Patrick Delaunay | db2c9aa | 2021-05-07 14:50:30 +0200 | [diff] [blame] | 131 | /** |
Heinrich Schuchardt | 5411fb7 | 2023-08-12 19:09:32 +0200 | [diff] [blame] | 132 | * lmb_is_reserved_flags() - test if address is in reserved region with flag bits set |
| 133 | * |
| 134 | * The function checks if a reserved region comprising @addr exists which has |
| 135 | * all flag bits set which are set in @flags. |
Patrick Delaunay | db2c9aa | 2021-05-07 14:50:30 +0200 | [diff] [blame] | 136 | * |
Heinrich Schuchardt | e6923f0 | 2021-11-14 08:43:07 +0100 | [diff] [blame] | 137 | * @lmb: the logical memory block struct |
| 138 | * @addr: address to be tested |
Heinrich Schuchardt | 5411fb7 | 2023-08-12 19:09:32 +0200 | [diff] [blame] | 139 | * @flags: bitmap with bits to be tested |
| 140 | * Return: 1 if matching reservation exists, 0 otherwise |
Patrick Delaunay | db2c9aa | 2021-05-07 14:50:30 +0200 | [diff] [blame] | 141 | */ |
| 142 | int lmb_is_reserved_flags(struct lmb *lmb, phys_addr_t addr, int flags); |
Heinrich Schuchardt | 5411fb7 | 2023-08-12 19:09:32 +0200 | [diff] [blame] | 143 | |
Heinrich Schuchardt | 9dccccd | 2021-11-14 08:52:48 +0100 | [diff] [blame] | 144 | long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 145 | |
Heinrich Schuchardt | 9dccccd | 2021-11-14 08:52:48 +0100 | [diff] [blame] | 146 | void lmb_dump_all(struct lmb *lmb); |
| 147 | void lmb_dump_all_force(struct lmb *lmb); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 148 | |
Mike Frysinger | a0dadf8 | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 149 | void board_lmb_reserve(struct lmb *lmb); |
| 150 | void arch_lmb_reserve(struct lmb *lmb); |
Marek Vasut | a2eec02 | 2021-09-10 22:47:09 +0200 | [diff] [blame] | 151 | void arch_lmb_reserve_generic(struct lmb *lmb, ulong sp, ulong end, ulong align); |
Mike Frysinger | a0dadf8 | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 152 | |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 153 | #endif /* __KERNEL__ */ |
| 154 | |
| 155 | #endif /* _LINUX_LMB_H */ |