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 | /* |
| 3 | * Procedures for maintaining information about logical memory blocks. |
| 4 | * |
| 5 | * Peter Bergner, IBM Corp. June 2001. |
| 6 | * Copyright (C) 2001 Peter Bergner. |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <lmb.h> |
| 11 | |
| 12 | #define LMB_ALLOC_ANYWHERE 0 |
| 13 | |
| 14 | void lmb_dump_all(struct lmb *lmb) |
| 15 | { |
| 16 | #ifdef DEBUG |
| 17 | unsigned long i; |
| 18 | |
| 19 | debug("lmb_dump_all:\n"); |
| 20 | debug(" memory.cnt = 0x%lx\n", lmb->memory.cnt); |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 21 | debug(" memory.size = 0x%llx\n", |
| 22 | (unsigned long long)lmb->memory.size); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 23 | for (i=0; i < lmb->memory.cnt ;i++) { |
Wolfgang Denk | 12cec0a | 2008-07-11 01:16:00 +0200 | [diff] [blame] | 24 | debug(" memory.reg[0x%lx].base = 0x%llx\n", i, |
| 25 | (long long unsigned)lmb->memory.region[i].base); |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 26 | debug(" .size = 0x%llx\n", |
Wolfgang Denk | 12cec0a | 2008-07-11 01:16:00 +0200 | [diff] [blame] | 27 | (long long unsigned)lmb->memory.region[i].size); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 28 | } |
| 29 | |
Wolfgang Denk | 12cec0a | 2008-07-11 01:16:00 +0200 | [diff] [blame] | 30 | debug("\n reserved.cnt = 0x%lx\n", |
| 31 | lmb->reserved.cnt); |
| 32 | debug(" reserved.size = 0x%llx\n", |
| 33 | (long long unsigned)lmb->reserved.size); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 34 | for (i=0; i < lmb->reserved.cnt ;i++) { |
Wolfgang Denk | 12cec0a | 2008-07-11 01:16:00 +0200 | [diff] [blame] | 35 | debug(" reserved.reg[0x%lx].base = 0x%llx\n", i, |
| 36 | (long long unsigned)lmb->reserved.region[i].base); |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 37 | debug(" .size = 0x%llx\n", |
Wolfgang Denk | 12cec0a | 2008-07-11 01:16:00 +0200 | [diff] [blame] | 38 | (long long unsigned)lmb->reserved.region[i].size); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 39 | } |
| 40 | #endif /* DEBUG */ |
| 41 | } |
| 42 | |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 43 | static long lmb_addrs_overlap(phys_addr_t base1, |
| 44 | phys_size_t size1, phys_addr_t base2, phys_size_t size2) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 45 | { |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 46 | const phys_addr_t base1_end = base1 + size1 - 1; |
| 47 | const phys_addr_t base2_end = base2 + size2 - 1; |
| 48 | |
| 49 | return ((base1 <= base2_end) && (base2 <= base1_end)); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 50 | } |
| 51 | |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 52 | static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1, |
| 53 | phys_addr_t base2, phys_size_t size2) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 54 | { |
| 55 | if (base2 == base1 + size1) |
| 56 | return 1; |
| 57 | else if (base1 == base2 + size2) |
| 58 | return -1; |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static long lmb_regions_adjacent(struct lmb_region *rgn, |
| 64 | unsigned long r1, unsigned long r2) |
| 65 | { |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 66 | phys_addr_t base1 = rgn->region[r1].base; |
| 67 | phys_size_t size1 = rgn->region[r1].size; |
| 68 | phys_addr_t base2 = rgn->region[r2].base; |
| 69 | phys_size_t size2 = rgn->region[r2].size; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 70 | |
| 71 | return lmb_addrs_adjacent(base1, size1, base2, size2); |
| 72 | } |
| 73 | |
| 74 | static void lmb_remove_region(struct lmb_region *rgn, unsigned long r) |
| 75 | { |
| 76 | unsigned long i; |
| 77 | |
| 78 | for (i = r; i < rgn->cnt - 1; i++) { |
| 79 | rgn->region[i].base = rgn->region[i + 1].base; |
| 80 | rgn->region[i].size = rgn->region[i + 1].size; |
| 81 | } |
| 82 | rgn->cnt--; |
| 83 | } |
| 84 | |
| 85 | /* Assumption: base addr of region 1 < base addr of region 2 */ |
| 86 | static void lmb_coalesce_regions(struct lmb_region *rgn, |
| 87 | unsigned long r1, unsigned long r2) |
| 88 | { |
| 89 | rgn->region[r1].size += rgn->region[r2].size; |
| 90 | lmb_remove_region(rgn, r2); |
| 91 | } |
| 92 | |
| 93 | void lmb_init(struct lmb *lmb) |
| 94 | { |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 95 | lmb->memory.cnt = 0; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 96 | lmb->memory.size = 0; |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 97 | lmb->reserved.cnt = 0; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 98 | lmb->reserved.size = 0; |
| 99 | } |
| 100 | |
| 101 | /* This routine called with relocation disabled. */ |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 102 | static long lmb_add_region(struct lmb_region *rgn, phys_addr_t base, phys_size_t size) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 103 | { |
| 104 | unsigned long coalesced = 0; |
| 105 | long adjacent, i; |
| 106 | |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 107 | if (rgn->cnt == 0) { |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 108 | rgn->region[0].base = base; |
| 109 | rgn->region[0].size = size; |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 110 | rgn->cnt = 1; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | /* First try and coalesce this LMB with another. */ |
| 115 | for (i=0; i < rgn->cnt; i++) { |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 116 | phys_addr_t rgnbase = rgn->region[i].base; |
| 117 | phys_size_t rgnsize = rgn->region[i].size; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 118 | |
| 119 | if ((rgnbase == base) && (rgnsize == size)) |
| 120 | /* Already have this region, so we're done */ |
| 121 | return 0; |
| 122 | |
| 123 | adjacent = lmb_addrs_adjacent(base,size,rgnbase,rgnsize); |
| 124 | if ( adjacent > 0 ) { |
| 125 | rgn->region[i].base -= size; |
| 126 | rgn->region[i].size += size; |
| 127 | coalesced++; |
| 128 | break; |
| 129 | } |
| 130 | else if ( adjacent < 0 ) { |
| 131 | rgn->region[i].size += size; |
| 132 | coalesced++; |
| 133 | break; |
Simon Goldschmidt | cb57d13 | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 134 | } else if (lmb_addrs_overlap(base, size, rgnbase, rgnsize)) { |
| 135 | /* regions overlap */ |
| 136 | return -1; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | if ((i < rgn->cnt-1) && lmb_regions_adjacent(rgn, i, i+1) ) { |
| 141 | lmb_coalesce_regions(rgn, i, i+1); |
| 142 | coalesced++; |
| 143 | } |
| 144 | |
| 145 | if (coalesced) |
| 146 | return coalesced; |
| 147 | if (rgn->cnt >= MAX_LMB_REGIONS) |
| 148 | return -1; |
| 149 | |
| 150 | /* Couldn't coalesce the LMB, so add it to the sorted table. */ |
| 151 | for (i = rgn->cnt-1; i >= 0; i--) { |
| 152 | if (base < rgn->region[i].base) { |
| 153 | rgn->region[i+1].base = rgn->region[i].base; |
| 154 | rgn->region[i+1].size = rgn->region[i].size; |
| 155 | } else { |
| 156 | rgn->region[i+1].base = base; |
| 157 | rgn->region[i+1].size = size; |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | if (base < rgn->region[0].base) { |
| 163 | rgn->region[0].base = base; |
| 164 | rgn->region[0].size = size; |
| 165 | } |
| 166 | |
| 167 | rgn->cnt++; |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | /* This routine may be called with relocation disabled. */ |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 173 | long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 174 | { |
| 175 | struct lmb_region *_rgn = &(lmb->memory); |
| 176 | |
| 177 | return lmb_add_region(_rgn, base, size); |
| 178 | } |
| 179 | |
Andy Fleming | 09d2a71 | 2008-07-07 14:24:39 -0500 | [diff] [blame] | 180 | long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size) |
Andy Fleming | 1ae346c | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 181 | { |
| 182 | struct lmb_region *rgn = &(lmb->reserved); |
Andy Fleming | 09d2a71 | 2008-07-07 14:24:39 -0500 | [diff] [blame] | 183 | phys_addr_t rgnbegin, rgnend; |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 184 | phys_addr_t end = base + size - 1; |
Andy Fleming | 1ae346c | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 185 | int i; |
| 186 | |
| 187 | rgnbegin = rgnend = 0; /* supress gcc warnings */ |
| 188 | |
| 189 | /* Find the region where (base, size) belongs to */ |
| 190 | for (i=0; i < rgn->cnt; i++) { |
| 191 | rgnbegin = rgn->region[i].base; |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 192 | rgnend = rgnbegin + rgn->region[i].size - 1; |
Andy Fleming | 1ae346c | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 193 | |
| 194 | if ((rgnbegin <= base) && (end <= rgnend)) |
| 195 | break; |
| 196 | } |
| 197 | |
| 198 | /* Didn't find the region */ |
| 199 | if (i == rgn->cnt) |
| 200 | return -1; |
| 201 | |
| 202 | /* Check to see if we are removing entire region */ |
| 203 | if ((rgnbegin == base) && (rgnend == end)) { |
| 204 | lmb_remove_region(rgn, i); |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | /* Check to see if region is matching at the front */ |
| 209 | if (rgnbegin == base) { |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 210 | rgn->region[i].base = end + 1; |
Andy Fleming | 1ae346c | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 211 | rgn->region[i].size -= size; |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | /* Check to see if the region is matching at the end */ |
| 216 | if (rgnend == end) { |
| 217 | rgn->region[i].size -= size; |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * We need to split the entry - adjust the current one to the |
| 223 | * beginging of the hole and add the region after hole. |
| 224 | */ |
| 225 | rgn->region[i].size = base - rgn->region[i].base; |
Simon Goldschmidt | 6402d9b | 2019-01-14 22:38:15 +0100 | [diff] [blame] | 226 | return lmb_add_region(rgn, end + 1, rgnend - end); |
Andy Fleming | 1ae346c | 2008-06-16 13:58:54 -0500 | [diff] [blame] | 227 | } |
| 228 | |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 229 | long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 230 | { |
| 231 | struct lmb_region *_rgn = &(lmb->reserved); |
| 232 | |
| 233 | return lmb_add_region(_rgn, base, size); |
| 234 | } |
| 235 | |
Jeroen Hofstee | a020972 | 2014-10-08 22:57:39 +0200 | [diff] [blame] | 236 | static long lmb_overlaps_region(struct lmb_region *rgn, phys_addr_t base, |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 237 | phys_size_t size) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 238 | { |
| 239 | unsigned long i; |
| 240 | |
| 241 | for (i=0; i < rgn->cnt; i++) { |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 242 | phys_addr_t rgnbase = rgn->region[i].base; |
| 243 | phys_size_t rgnsize = rgn->region[i].size; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 244 | if ( lmb_addrs_overlap(base,size,rgnbase,rgnsize) ) { |
| 245 | break; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | return (i < rgn->cnt) ? i : -1; |
| 250 | } |
| 251 | |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 252 | phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 253 | { |
| 254 | return lmb_alloc_base(lmb, size, align, LMB_ALLOC_ANYWHERE); |
| 255 | } |
| 256 | |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 257 | phys_addr_t lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align, phys_addr_t max_addr) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 258 | { |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 259 | phys_addr_t alloc; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 260 | |
| 261 | alloc = __lmb_alloc_base(lmb, size, align, max_addr); |
| 262 | |
| 263 | if (alloc == 0) |
| 264 | printf("ERROR: Failed to allocate 0x%lx bytes below 0x%lx.\n", |
Wolfgang Denk | 12cec0a | 2008-07-11 01:16:00 +0200 | [diff] [blame] | 265 | (ulong)size, (ulong)max_addr); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 266 | |
| 267 | return alloc; |
| 268 | } |
| 269 | |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 270 | static phys_addr_t lmb_align_down(phys_addr_t addr, phys_size_t size) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 271 | { |
| 272 | return addr & ~(size - 1); |
| 273 | } |
| 274 | |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 275 | phys_addr_t __lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong align, phys_addr_t max_addr) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 276 | { |
| 277 | long i, j; |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 278 | phys_addr_t base = 0; |
Andy Fleming | 78bd5a7 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 279 | phys_addr_t res_base; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 280 | |
| 281 | for (i = lmb->memory.cnt-1; i >= 0; i--) { |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 282 | phys_addr_t lmbbase = lmb->memory.region[i].base; |
| 283 | phys_size_t lmbsize = lmb->memory.region[i].size; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 284 | |
Andy Fleming | 78bd5a7 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 285 | if (lmbsize < size) |
| 286 | continue; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 287 | if (max_addr == LMB_ALLOC_ANYWHERE) |
| 288 | base = lmb_align_down(lmbbase + lmbsize - size, align); |
| 289 | else if (lmbbase < max_addr) { |
Stephen Warren | b6a010b | 2014-07-31 13:40:07 -0600 | [diff] [blame] | 290 | base = lmbbase + lmbsize; |
| 291 | if (base < lmbbase) |
| 292 | base = -1; |
| 293 | base = min(base, max_addr); |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 294 | base = lmb_align_down(base - size, align); |
| 295 | } else |
| 296 | continue; |
| 297 | |
Andy Fleming | 78bd5a7 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 298 | while (base && lmbbase <= base) { |
| 299 | j = lmb_overlaps_region(&lmb->reserved, base, size); |
| 300 | if (j < 0) { |
| 301 | /* This area isn't reserved, take it */ |
| 302 | if (lmb_add_region(&lmb->reserved, base, |
Simon Goldschmidt | cb57d13 | 2019-01-14 22:38:16 +0100 | [diff] [blame] | 303 | size) < 0) |
Andy Fleming | 78bd5a7 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 304 | return 0; |
| 305 | return base; |
| 306 | } |
| 307 | res_base = lmb->reserved.region[j].base; |
| 308 | if (res_base < size) |
| 309 | break; |
| 310 | base = lmb_align_down(res_base - size, align); |
| 311 | } |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 312 | } |
Andy Fleming | 78bd5a7 | 2008-06-16 13:58:55 -0500 | [diff] [blame] | 313 | return 0; |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 314 | } |
| 315 | |
Simon Goldschmidt | 7a6ee46 | 2019-01-14 22:38:18 +0100 | [diff] [blame^] | 316 | /* |
| 317 | * Try to allocate a specific address range: must be in defined memory but not |
| 318 | * reserved |
| 319 | */ |
| 320 | phys_addr_t lmb_alloc_addr(struct lmb *lmb, phys_addr_t base, phys_size_t size) |
| 321 | { |
| 322 | long j; |
| 323 | |
| 324 | /* Check if the requested address is in one of the memory regions */ |
| 325 | j = lmb_overlaps_region(&lmb->memory, base, size); |
| 326 | if (j >= 0) { |
| 327 | /* |
| 328 | * Check if the requested end address is in the same memory |
| 329 | * region we found. |
| 330 | */ |
| 331 | if (lmb_addrs_overlap(lmb->memory.region[j].base, |
| 332 | lmb->memory.region[j].size, base + size - |
| 333 | 1, 1)) { |
| 334 | /* ok, reserve the memory */ |
| 335 | if (lmb_reserve(lmb, base, size) >= 0) |
| 336 | return base; |
| 337 | } |
| 338 | } |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | /* Return number of bytes from a given address that are free */ |
| 343 | phys_size_t lmb_get_unreserved_size(struct lmb *lmb, phys_addr_t addr) |
| 344 | { |
| 345 | int i; |
| 346 | long j; |
| 347 | |
| 348 | /* check if the requested address is in the memory regions */ |
| 349 | j = lmb_overlaps_region(&lmb->memory, addr, 1); |
| 350 | if (j >= 0) { |
| 351 | for (i = 0; i < lmb->reserved.cnt; i++) { |
| 352 | if (addr < lmb->reserved.region[i].base) { |
| 353 | /* first reserved range > requested address */ |
| 354 | return lmb->reserved.region[i].base - addr; |
| 355 | } |
| 356 | if (lmb->reserved.region[i].base + |
| 357 | lmb->reserved.region[i].size > addr) { |
| 358 | /* requested addr is in this reserved range */ |
| 359 | return 0; |
| 360 | } |
| 361 | } |
| 362 | /* if we come here: no reserved ranges above requested addr */ |
| 363 | return lmb->memory.region[lmb->memory.cnt - 1].base + |
| 364 | lmb->memory.region[lmb->memory.cnt - 1].size - addr; |
| 365 | } |
| 366 | return 0; |
| 367 | } |
| 368 | |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 369 | int lmb_is_reserved(struct lmb *lmb, phys_addr_t addr) |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 370 | { |
| 371 | int i; |
| 372 | |
| 373 | for (i = 0; i < lmb->reserved.cnt; i++) { |
Becky Bruce | d26d67c | 2008-06-09 20:37:18 -0500 | [diff] [blame] | 374 | phys_addr_t upper = lmb->reserved.region[i].base + |
Kumar Gala | 6d7bfa8 | 2008-02-27 21:51:47 -0600 | [diff] [blame] | 375 | lmb->reserved.region[i].size - 1; |
| 376 | if ((addr >= lmb->reserved.region[i].base) && (addr <= upper)) |
| 377 | return 1; |
| 378 | } |
| 379 | return 0; |
| 380 | } |
Mike Frysinger | a0dadf8 | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 381 | |
Jeroen Hofstee | 0461dda | 2014-06-26 20:04:37 +0200 | [diff] [blame] | 382 | __weak void board_lmb_reserve(struct lmb *lmb) |
Mike Frysinger | a0dadf8 | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 383 | { |
| 384 | /* please define platform specific board_lmb_reserve() */ |
| 385 | } |
Mike Frysinger | a0dadf8 | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 386 | |
Jeroen Hofstee | 0461dda | 2014-06-26 20:04:37 +0200 | [diff] [blame] | 387 | __weak void arch_lmb_reserve(struct lmb *lmb) |
Mike Frysinger | a0dadf8 | 2009-11-03 11:35:59 -0500 | [diff] [blame] | 388 | { |
| 389 | /* please define platform specific arch_lmb_reserve() */ |
| 390 | } |