LCOV - code coverage report
Current view: top level - core - mem_region.c (source / functions) Hit Total Coverage
Test: skiboot.info Lines: 467 731 63.9 %
Date: 2024-01-02 21:04:04 Functions: 39 51 76.5 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
       2                 :            : /*
       3                 :            :  * Simple memory allocator
       4                 :            :  *
       5                 :            :  * Copyright 2013-2018 IBM Corp.
       6                 :            :  */
       7                 :            : 
       8                 :            : #include <inttypes.h>
       9                 :            : #include <skiboot.h>
      10                 :            : #include <mem-map.h>
      11                 :            : #include <libfdt_env.h>
      12                 :            : #include <lock.h>
      13                 :            : #include <device.h>
      14                 :            : #include <cpu.h>
      15                 :            : #include <chip.h>
      16                 :            : #include <affinity.h>
      17                 :            : #include <types.h>
      18                 :            : #include <mem_region.h>
      19                 :            : #include <mem_region-malloc.h>
      20                 :            : 
      21                 :            : /* Memory poisoning on free (if POISON_MEM_REGION set to 1) */
      22                 :            : #ifdef DEBUG
      23                 :            : #define POISON_MEM_REGION       1
      24                 :            : #else
      25                 :            : #define POISON_MEM_REGION       0
      26                 :            : #endif
      27                 :            : #define POISON_MEM_REGION_WITH  0x99
      28                 :            : #define POISON_MEM_REGION_LIMIT 1*1024*1024*1024
      29                 :            : 
      30                 :            : /* Locking: The mem_region_lock protects the regions list from concurrent
      31                 :            :  * updates. Additions to, or removals from, the region list must be done
      32                 :            :  * with this lock held. This is typically done when we're establishing
      33                 :            :  * the memory & reserved regions.
      34                 :            :  *
      35                 :            :  * Each region has a lock (region->free_list_lock) to protect the free list
      36                 :            :  * from concurrent modification. This lock is used when we're allocating
      37                 :            :  * memory out of a specific region.
      38                 :            :  *
      39                 :            :  * If both locks are needed (eg, __local_alloc, where we need to find a region,
      40                 :            :  * then allocate from it), the mem_region_lock must be acquired before (and
      41                 :            :  * released after) the per-region lock.
      42                 :            :  */
      43                 :            : struct lock mem_region_lock = LOCK_UNLOCKED;
      44                 :            : 
      45                 :            : static struct list_head regions = LIST_HEAD_INIT(regions);
      46                 :            : static struct list_head early_reserves = LIST_HEAD_INIT(early_reserves);
      47                 :            : 
      48                 :            : static bool mem_region_init_done = false;
      49                 :            : static bool mem_regions_finalised = false;
      50                 :            : 
      51                 :            : unsigned long top_of_ram = SKIBOOT_BASE + SKIBOOT_SIZE;
      52                 :            : 
      53                 :            : static struct mem_region skiboot_os_reserve = {
      54                 :            :         .name           = "ibm,os-reserve",
      55                 :            :         .start          = 0,
      56                 :            :         .len            = SKIBOOT_BASE,
      57                 :            :         .type           = REGION_OS,
      58                 :            : };
      59                 :            : 
      60                 :            : struct mem_region skiboot_heap = {
      61                 :            :         .name           = "ibm,firmware-heap",
      62                 :            :         .start          = HEAP_BASE,
      63                 :            :         .len            = HEAP_SIZE,
      64                 :            :         .type           = REGION_SKIBOOT_HEAP,
      65                 :            : };
      66                 :            : 
      67                 :            : static struct mem_region skiboot_code_and_text = {
      68                 :            :         .name           = "ibm,firmware-code",
      69                 :            :         .start          = SKIBOOT_BASE,
      70                 :            :         .len            = HEAP_BASE - SKIBOOT_BASE,
      71                 :            :         .type           = REGION_SKIBOOT_FIRMWARE,
      72                 :            : };
      73                 :            : 
      74                 :            : static struct mem_region skiboot_after_heap = {
      75                 :            :         .name           = "ibm,firmware-data",
      76                 :            :         .start          = HEAP_BASE + HEAP_SIZE,
      77                 :            :         .len            = SKIBOOT_BASE + SKIBOOT_SIZE - (HEAP_BASE + HEAP_SIZE),
      78                 :            :         .type           = REGION_SKIBOOT_FIRMWARE,
      79                 :            : };
      80                 :            : 
      81                 :            : static struct mem_region skiboot_cpu_stacks = {
      82                 :            :         .name           = "ibm,firmware-stacks",
      83                 :            :         .start          = CPU_STACKS_BASE,
      84                 :            :         .len            = 0, /* TBA */
      85                 :            :         .type           = REGION_SKIBOOT_FIRMWARE,
      86                 :            : };
      87                 :            : 
      88                 :            : static struct mem_region skiboot_mambo_kernel = {
      89                 :            :         .name           = "ibm,firmware-mambo-kernel",
      90                 :            :         .start          = (unsigned long)KERNEL_LOAD_BASE,
      91                 :            :         .len            = KERNEL_LOAD_SIZE,
      92                 :            :         .type           = REGION_SKIBOOT_FIRMWARE,
      93                 :            : };
      94                 :            : 
      95                 :            : static struct mem_region skiboot_mambo_initramfs = {
      96                 :            :         .name           = "ibm,firmware-mambo-initramfs",
      97                 :            :         .start          = (unsigned long)INITRAMFS_LOAD_BASE,
      98                 :            :         .len            = INITRAMFS_LOAD_SIZE,
      99                 :            :         .type           = REGION_SKIBOOT_FIRMWARE,
     100                 :            : };
     101                 :            : 
     102                 :            : 
     103                 :            : struct alloc_hdr {
     104                 :            :         bool free : 1;
     105                 :            :         bool prev_free : 1;
     106                 :            :         bool printed : 1;
     107                 :            :         unsigned long num_longs : BITS_PER_LONG-3; /* Including header. */
     108                 :            :         const char *location;
     109                 :            : };
     110                 :            : 
     111                 :            : struct free_hdr {
     112                 :            :         struct alloc_hdr hdr;
     113                 :            :         struct list_node list;
     114                 :            :         /* ... unsigned long tailer; */
     115                 :            : };
     116                 :            : 
     117                 :            : #define ALLOC_HDR_LONGS (sizeof(struct alloc_hdr) / sizeof(long))
     118                 :            : #define ALLOC_MIN_LONGS (sizeof(struct free_hdr) / sizeof(long) + 1)
     119                 :            : 
     120                 :            : /* Avoid ugly casts. */
     121                 :     448495 : static void *region_start(const struct mem_region *region)
     122                 :            : {
     123                 :     448495 :         return (void *)(unsigned long)region->start;
     124                 :            : }
     125                 :            : 
     126                 :            : /* Each free block has a tailer, so we can walk backwards. */
     127                 :      30205 : static unsigned long *tailer(struct free_hdr *f)
     128                 :            : {
     129                 :      30205 :         return (unsigned long *)f + f->hdr.num_longs - 1;
     130                 :            : }
     131                 :            : 
     132                 :            : /* This walks forward to the next hdr (or NULL if at the end). */
     133                 :     308416 : static struct alloc_hdr *next_hdr(const struct mem_region *region,
     134                 :            :                                   const struct alloc_hdr *hdr)
     135                 :            : {
     136                 :            :         void *next;
     137                 :            : 
     138                 :     308416 :         next = ((unsigned long *)hdr + hdr->num_longs);
     139                 :     308416 :         if (next >= region_start(region) + region->len)
     140                 :     157924 :                 next = NULL;
     141                 :     308416 :         return next;
     142                 :            : }
     143                 :            : 
     144                 :            : #if POISON_MEM_REGION == 1
     145                 :       1670 : static void mem_poison(struct free_hdr *f)
     146                 :            : {
     147                 :       1670 :         size_t poison_size = (void*)tailer(f) - (void*)(f+1);
     148                 :            : 
     149                 :            :         /* We only poison up to a limit, as otherwise boot is
     150                 :            :          * kinda slow */
     151                 :       1670 :         if (poison_size > POISON_MEM_REGION_LIMIT)
     152                 :          0 :                 poison_size = POISON_MEM_REGION_LIMIT;
     153                 :            : 
     154                 :       1670 :         memset(f+1, POISON_MEM_REGION_WITH, poison_size);
     155                 :       1670 : }
     156                 :            : #endif
     157                 :            : 
     158                 :            : /* Creates free block covering entire region. */
     159                 :          8 : static void init_allocatable_region(struct mem_region *region)
     160                 :            : {
     161                 :          8 :         struct free_hdr *f = region_start(region);
     162                 :          8 :         assert(region->type == REGION_SKIBOOT_HEAP ||
     163                 :            :                region->type == REGION_MEMORY);
     164                 :          8 :         f->hdr.num_longs = region->len / sizeof(long);
     165                 :          8 :         f->hdr.free = true;
     166                 :          8 :         f->hdr.prev_free = false;
     167                 :          8 :         *tailer(f) = f->hdr.num_longs;
     168                 :          8 :         list_head_init(&region->free_list);
     169                 :          8 :         list_add(&region->free_list, &f->list);
     170                 :            : #if POISON_MEM_REGION == 1
     171                 :          8 :         mem_poison(f);
     172                 :            : #endif
     173                 :          8 : }
     174                 :            : 
     175                 :      15885 : static void make_free(struct mem_region *region, struct free_hdr *f,
     176                 :            :                       const char *location, bool skip_poison)
     177                 :            : {
     178                 :            :         struct alloc_hdr *next;
     179                 :            : 
     180                 :            : #if POISON_MEM_REGION == 1
     181                 :      15885 :         if (!skip_poison)
     182                 :       1662 :                 mem_poison(f);
     183                 :            : #else
     184                 :            :         (void)skip_poison;
     185                 :            : #endif
     186                 :            : 
     187                 :      15885 :         if (f->hdr.prev_free) {
     188                 :            :                 struct free_hdr *prev;
     189                 :       1641 :                 unsigned long *prev_tailer = (unsigned long *)f - 1;
     190                 :            : 
     191                 :       1641 :                 assert(*prev_tailer);
     192                 :       1641 :                 prev = (void *)((unsigned long *)f - *prev_tailer);
     193                 :       1641 :                 assert(prev->hdr.free);
     194                 :       1641 :                 assert(!prev->hdr.prev_free);
     195                 :            : 
     196                 :            :                 /* Expand to cover the one we just freed. */
     197                 :       1641 :                 prev->hdr.num_longs += f->hdr.num_longs;
     198                 :       1641 :                 f = prev;
     199                 :            :         } else {
     200                 :      14244 :                 f->hdr.free = true;
     201                 :      14244 :                 f->hdr.location = location;
     202                 :      14244 :                 list_add(&region->free_list, &f->list);
     203                 :            :         }
     204                 :            : 
     205                 :            :         /* Fix up tailer. */
     206                 :      15885 :         *tailer(f) = f->hdr.num_longs;
     207                 :            : 
     208                 :            :         /* If next is free, coalesce it */
     209                 :      15885 :         next = next_hdr(region, &f->hdr);
     210                 :      15885 :         if (next) {
     211                 :       1780 :                 next->prev_free = true;
     212                 :       1780 :                 if (next->free) {
     213                 :       1596 :                         struct free_hdr *next_free = (void *)next;
     214                 :       1596 :                         list_del_from(&region->free_list, &next_free->list);
     215                 :            :                         /* Maximum of one level of recursion */
     216                 :       1596 :                         make_free(region, next_free, location, true);
     217                 :            :                 }
     218                 :            :         }
     219                 :      15885 : }
     220                 :            : 
     221                 :            : /* Can we fit this many longs with this alignment in this free block? */
     222                 :       4469 : static bool fits(struct free_hdr *f, size_t longs, size_t align, size_t *offset)
     223                 :            : {
     224                 :       4469 :         *offset = 0;
     225                 :            : 
     226                 :      22133 :         while (f->hdr.num_longs >= *offset + longs) {
     227                 :            :                 size_t addr;
     228                 :            : 
     229                 :      22121 :                 addr = (unsigned long)f
     230                 :      22121 :                         + (*offset + ALLOC_HDR_LONGS) * sizeof(long);
     231                 :      22121 :                 if ((addr & (align - 1)) == 0)
     232                 :       4457 :                         return true;
     233                 :            : 
     234                 :            :                 /* Don't make tiny chunks! */
     235                 :      17664 :                 if (*offset == 0)
     236                 :         14 :                         *offset = ALLOC_MIN_LONGS;
     237                 :            :                 else
     238                 :      17650 :                         (*offset)++;
     239                 :            :         }
     240                 :         12 :         return false;
     241                 :            : }
     242                 :            : 
     243                 :     135502 : static void discard_excess(struct mem_region *region,
     244                 :            :                            struct alloc_hdr *hdr, size_t alloc_longs,
     245                 :            :                            const char *location, bool skip_poison)
     246                 :            : {
     247                 :            :         /* Do we have excess? */
     248                 :     135502 :         if (hdr->num_longs > alloc_longs + ALLOC_MIN_LONGS) {
     249                 :            :                 struct free_hdr *post;
     250                 :            : 
     251                 :            :                 /* Set up post block. */
     252                 :      13980 :                 post = (void *)hdr + alloc_longs * sizeof(long);
     253                 :      13980 :                 post->hdr.num_longs = hdr->num_longs - alloc_longs;
     254                 :      13980 :                 post->hdr.prev_free = false;
     255                 :            : 
     256                 :            :                 /* Trim our block. */
     257                 :      13980 :                 hdr->num_longs = alloc_longs;
     258                 :            : 
     259                 :            :                 /* This coalesces as required. */
     260                 :      13980 :                 make_free(region, post, location, skip_poison);
     261                 :            :         }
     262                 :     135502 : }
     263                 :            : 
     264                 :          6 : static const char *hdr_location(const struct alloc_hdr *hdr)
     265                 :            : {
     266                 :            :         /* Corrupt: step carefully! */
     267                 :          0 :         if (is_rodata(hdr->location))
     268                 :          6 :                 return hdr->location;
     269                 :          0 :         return "*CORRUPT*";
     270                 :            : }
     271                 :            : 
     272                 :          0 : static void bad_header(const struct mem_region *region,
     273                 :            :                        const struct alloc_hdr *hdr,
     274                 :            :                        const char *during,
     275                 :            :                        const char *location)
     276                 :            : {
     277                 :            :         /* Corrupt: step carefully! */
     278                 :          0 :         if (is_rodata(hdr->location))
     279                 :          0 :                 prerror("%p (in %s) %s at %s, previously %s\n",
     280                 :            :                         hdr-1, region->name, during, location, hdr->location);
     281                 :            :         else
     282                 :          0 :                 prerror("%p (in %s) %s at %s, previously %p\n",
     283                 :            :                         hdr-1, region->name, during, location, hdr->location);
     284                 :          0 :         abort();
     285                 :            : }
     286                 :            : 
     287                 :         28 : static bool region_is_reservable(struct mem_region *region)
     288                 :            : {
     289                 :         28 :         return region->type != REGION_OS;
     290                 :            : }
     291                 :            : 
     292                 :        128 : static bool region_is_reserved(struct mem_region *region)
     293                 :            : {
     294                 :        128 :         return region->type != REGION_OS && region->type != REGION_MEMORY;
     295                 :            : }
     296                 :            : 
     297                 :          3 : void mem_dump_allocs(void)
     298                 :            : {
     299                 :            :         struct mem_region *region;
     300                 :            :         struct alloc_hdr *h, *i;
     301                 :            : 
     302                 :            :         /* Second pass: populate property data */
     303                 :          3 :         prlog(PR_INFO, "Memory regions:\n");
     304                 :         13 :         list_for_each(&regions, region, list) {
     305                 :         10 :                 if (!(region->type == REGION_SKIBOOT_HEAP ||
     306                 :          9 :                       region->type == REGION_MEMORY))
     307                 :          4 :                         continue;
     308                 :          6 :                 prlog(PR_INFO, "  0x%012llx..%012llx : %s\n",
     309                 :            :                        (long long)region->start,
     310                 :            :                        (long long)(region->start + region->len - 1),
     311                 :            :                        region->name);
     312                 :          6 :                 if (region->free_list.n.next == NULL) {
     313                 :          5 :                         prlog(PR_INFO, "    no allocs\n");
     314                 :          5 :                         continue;
     315                 :            :                 }
     316                 :            : 
     317                 :            :                 /*
     318                 :            :                  * XXX: When dumping the allocation list we coalase allocations
     319                 :            :                  * with the same location and size into a single line. This is
     320                 :            :                  * quadratic, but it makes the dump human-readable and the raw
     321                 :            :                  * dump sometimes causes the log buffer to wrap.
     322                 :            :                  */
     323                 :         27 :                 for (h = region_start(region); h; h = next_hdr(region, h))
     324                 :         26 :                         h->printed = false;
     325                 :            : 
     326                 :         27 :                 for (h = region_start(region); h; h = next_hdr(region, h)) {
     327                 :            :                         unsigned long bytes;
     328                 :         26 :                         int count = 0;
     329                 :            : 
     330                 :         26 :                         if (h->free)
     331                 :          1 :                                 continue;
     332                 :         25 :                         if (h->printed)
     333                 :         19 :                                 continue;
     334                 :            : 
     335                 :        123 :                         for (i = h; i; i = next_hdr(region, i)) {
     336                 :        117 :                                 if (i->free)
     337                 :          6 :                                         continue;
     338                 :        111 :                                 if (i->num_longs != h->num_longs)
     339                 :         82 :                                         continue;
     340                 :         29 :                                 if (strcmp(i->location, h->location))
     341                 :          4 :                                         continue;
     342                 :            : 
     343                 :         25 :                                 i->printed = true;
     344                 :         25 :                                 count++;
     345                 :            :                         }
     346                 :            : 
     347                 :          6 :                         bytes = h->num_longs * sizeof(long);
     348                 :          6 :                         prlog(PR_NOTICE, " % 8d allocs of 0x%.8lx bytes at %s (total 0x%lx)\n",
     349                 :            :                                 count, bytes, hdr_location(h), bytes * count);
     350                 :            :                 }
     351                 :            :         }
     352                 :          3 : }
     353                 :            : 
     354                 :          2 : int64_t mem_dump_free(void)
     355                 :            : {
     356                 :            :         struct mem_region *region;
     357                 :            :         struct alloc_hdr *hdr;
     358                 :            :         int64_t total_free;
     359                 :            :         int64_t region_free;
     360                 :            : 
     361                 :          2 :         total_free = 0;
     362                 :            : 
     363                 :          2 :         prlog(PR_INFO, "Free space in HEAP memory regions:\n");
     364                 :          5 :         list_for_each(&regions, region, list) {
     365                 :          3 :                 if (!(region->type == REGION_SKIBOOT_HEAP ||
     366                 :          1 :                       region->type == REGION_MEMORY))
     367                 :          1 :                         continue;
     368                 :          2 :                 region_free = 0;
     369                 :            : 
     370                 :          2 :                 if (region->free_list.n.next == NULL) {
     371                 :          2 :                         continue;
     372                 :            :                 }
     373                 :          0 :                 for (hdr = region_start(region); hdr; hdr = next_hdr(region, hdr)) {
     374                 :          0 :                         if (!hdr->free)
     375                 :          0 :                                 continue;
     376                 :            : 
     377                 :          0 :                         region_free+= hdr->num_longs * sizeof(long);
     378                 :            :                 }
     379                 :          0 :                 prlog(PR_INFO, "Region %s free: %"PRIx64"\n",
     380                 :            :                        region->name, region_free);
     381                 :          0 :                 total_free += region_free;
     382                 :            :         }
     383                 :            : 
     384                 :          2 :         prlog(PR_INFO, "Total free: %"PRIu64"\n", total_free);
     385                 :            : 
     386                 :          2 :         return total_free;
     387                 :            : }
     388                 :            : 
     389                 :       4461 : static void *__mem_alloc(struct mem_region *region, size_t size, size_t align,
     390                 :            :                          const char *location)
     391                 :            : {
     392                 :            :         size_t alloc_longs, offset;
     393                 :            :         struct free_hdr *f;
     394                 :            :         struct alloc_hdr *next;
     395                 :            : 
     396                 :            :         /* Align must be power of 2. */
     397                 :       4461 :         assert(!((align - 1) & align));
     398                 :            : 
     399                 :            :         /* This should be a constant. */
     400                 :          0 :         assert(is_rodata(location));
     401                 :            : 
     402                 :            :         /* Unallocatable region? */
     403                 :       4461 :         if (!(region->type == REGION_SKIBOOT_HEAP ||
     404                 :          1 :               region->type == REGION_MEMORY))
     405                 :          0 :                 return NULL;
     406                 :            : 
     407                 :            :         /* First allocation? */
     408                 :       4461 :         if (region->free_list.n.next == NULL)
     409                 :          8 :                 init_allocatable_region(region);
     410                 :            : 
     411                 :            :         /* Don't do screwy sizes. */
     412                 :       4461 :         if (size > region->len)
     413                 :          2 :                 return NULL;
     414                 :            : 
     415                 :            :         /* Don't do tiny alignments, we deal in long increments. */
     416                 :       4459 :         if (align < sizeof(long))
     417                 :        128 :                 align = sizeof(long);
     418                 :            : 
     419                 :            :         /* Convert size to number of longs, too. */
     420                 :       4459 :         alloc_longs = (size + sizeof(long)-1) / sizeof(long) + ALLOC_HDR_LONGS;
     421                 :            : 
     422                 :            :         /* Can't be too small for when we free it, either. */
     423                 :       4459 :         if (alloc_longs < ALLOC_MIN_LONGS)
     424                 :        174 :                 alloc_longs = ALLOC_MIN_LONGS;
     425                 :            : 
     426                 :            :         /* Walk free list. */
     427                 :       4471 :         list_for_each(&region->free_list, f, list) {
     428                 :            :                 /* We may have to skip some to meet alignment. */
     429                 :       4469 :                 if (fits(f, alloc_longs, align, &offset))
     430                 :       4457 :                         goto found;
     431                 :            :         }
     432                 :            : 
     433                 :          2 :         return NULL;
     434                 :            : 
     435                 :       4457 : found:
     436                 :       4457 :         assert(f->hdr.free);
     437                 :       4457 :         assert(!f->hdr.prev_free);
     438                 :            : 
     439                 :            :         /* This block is no longer free. */
     440                 :       4457 :         list_del_from(&region->free_list, &f->list);
     441                 :       4457 :         f->hdr.free = false;
     442                 :       4457 :         f->hdr.location = location;
     443                 :            : 
     444                 :       4457 :         next = next_hdr(region, &f->hdr);
     445                 :       4457 :         if (next) {
     446                 :         65 :                 assert(next->prev_free);
     447                 :         65 :                 next->prev_free = false;
     448                 :            :         }
     449                 :            : 
     450                 :       4457 :         if (offset != 0) {
     451                 :         13 :                 struct free_hdr *pre = f;
     452                 :            : 
     453                 :         13 :                 f = (void *)f + offset * sizeof(long);
     454                 :         13 :                 assert(f >= pre + 1);
     455                 :            : 
     456                 :            :                 /* Set up new header. */
     457                 :         13 :                 f->hdr.num_longs = pre->hdr.num_longs - offset;
     458                 :            :                 /* f->hdr.prev_free will be set by make_free below. */
     459                 :         13 :                 f->hdr.free = false;
     460                 :         13 :                 f->hdr.location = location;
     461                 :            : 
     462                 :            :                 /* Fix up old header. */
     463                 :         13 :                 pre->hdr.num_longs = offset;
     464                 :         13 :                 pre->hdr.prev_free = false;
     465                 :            : 
     466                 :            :                 /* This coalesces as required. */
     467                 :         13 :                 make_free(region, pre, location, true);
     468                 :            :         }
     469                 :            : 
     470                 :            :         /* We might be too long; put the rest back. */
     471                 :       4457 :         discard_excess(region, &f->hdr, alloc_longs, location, true);
     472                 :            : 
     473                 :            :         /* Clear tailer for debugging */
     474                 :       4457 :         *tailer(f) = 0;
     475                 :            : 
     476                 :            :         /* Their pointer is immediately after header. */
     477                 :       4457 :         return &f->hdr + 1;
     478                 :            : }
     479                 :            : 
     480                 :       4461 : void *mem_alloc(struct mem_region *region, size_t size, size_t align,
     481                 :            :                 const char *location)
     482                 :            : {
     483                 :            :         static bool dumped = false;
     484                 :            :         void *r;
     485                 :            : 
     486                 :       4461 :         assert(lock_held_by_me(&region->free_list_lock));
     487                 :            : 
     488                 :       4461 :         r = __mem_alloc(region, size, align, location);
     489                 :       4461 :         if (r)
     490                 :       4457 :                 return r;
     491                 :            : 
     492                 :          4 :         prerror("mem_alloc(0x%lx, 0x%lx, \"%s\", %s) failed !\n",
     493                 :            :                 size, align, location, region->name);
     494                 :          4 :         if (!dumped) {
     495                 :          2 :                 mem_dump_allocs();
     496                 :          2 :                 dumped = true;
     497                 :            :         }
     498                 :            : 
     499                 :          4 :         return NULL;
     500                 :            : }
     501                 :            : 
     502                 :        297 : void mem_free(struct mem_region *region, void *mem, const char *location)
     503                 :            : {
     504                 :            :         struct alloc_hdr *hdr;
     505                 :            : 
     506                 :            :         /* This should be a constant. */
     507                 :          0 :         assert(is_rodata(location));
     508                 :            : 
     509                 :        297 :         assert(lock_held_by_me(&region->free_list_lock));
     510                 :            : 
     511                 :            :         /* Freeing NULL is always a noop. */
     512                 :        297 :         if (!mem)
     513                 :          1 :                 return;
     514                 :            : 
     515                 :            :         /* Your memory is in the region, right? */
     516                 :        296 :         assert(mem >= region_start(region) + sizeof(*hdr));
     517                 :        296 :         assert(mem < region_start(region) + region->len);
     518                 :            : 
     519                 :            :         /* Grab header. */
     520                 :        296 :         hdr = mem - sizeof(*hdr);
     521                 :            : 
     522                 :        296 :         if (hdr->free)
     523                 :          0 :                 bad_header(region, hdr, "re-freed", location);
     524                 :            : 
     525                 :        296 :         make_free(region, (struct free_hdr *)hdr, location, false);
     526                 :            : }
     527                 :            : 
     528                 :      65539 : size_t mem_allocated_size(const void *ptr)
     529                 :            : {
     530                 :      65539 :         const struct alloc_hdr *hdr = ptr - sizeof(*hdr);
     531                 :      65539 :         return hdr->num_longs * sizeof(long) - sizeof(struct alloc_hdr);
     532                 :            : }
     533                 :            : 
     534                 :     131048 : bool mem_resize(struct mem_region *region, void *mem, size_t len,
     535                 :            :                 const char *location)
     536                 :            : {
     537                 :            :         struct alloc_hdr *hdr, *next;
     538                 :            :         struct free_hdr *f;
     539                 :            : 
     540                 :            :         /* This should be a constant. */
     541                 :          0 :         assert(is_rodata(location));
     542                 :            : 
     543                 :     131048 :         assert(lock_held_by_me(&region->free_list_lock));
     544                 :            : 
     545                 :            :         /* Get header. */
     546                 :     131048 :         hdr = mem - sizeof(*hdr);
     547                 :     131048 :         if (hdr->free)
     548                 :          0 :                 bad_header(region, hdr, "resize", location);
     549                 :            : 
     550                 :            :         /* Round up size to multiple of longs. */
     551                 :     131048 :         len = (sizeof(*hdr) + len + sizeof(long) - 1) / sizeof(long);
     552                 :            : 
     553                 :            :         /* Can't be too small for when we free it, either. */
     554                 :     131048 :         if (len < ALLOC_MIN_LONGS)
     555                 :         34 :                 len = ALLOC_MIN_LONGS;
     556                 :            : 
     557                 :            :         /* Shrinking is simple. */
     558                 :     131048 :         if (len <= hdr->num_longs) {
     559                 :     122860 :                 hdr->location = location;
     560                 :     122860 :                 discard_excess(region, hdr, len, location, false);
     561                 :     122860 :                 return true;
     562                 :            :         }
     563                 :            : 
     564                 :            :         /* Check if we can expand. */
     565                 :       8188 :         next = next_hdr(region, hdr);
     566                 :       8188 :         if (!next || !next->free || hdr->num_longs + next->num_longs < len)
     567                 :          3 :                 return false;
     568                 :            : 
     569                 :            :         /* OK, it's free and big enough, absorb it. */
     570                 :       8185 :         f = (struct free_hdr *)next;
     571                 :       8185 :         list_del_from(&region->free_list, &f->list);
     572                 :       8185 :         hdr->num_longs += next->num_longs;
     573                 :       8185 :         hdr->location = location;
     574                 :            : 
     575                 :            :         /* Update next prev_free */
     576                 :       8185 :         next = next_hdr(region, &f->hdr);
     577                 :       8185 :         if (next) {
     578                 :          1 :                 assert(next->prev_free);
     579                 :          1 :                 next->prev_free = false;
     580                 :            :         }
     581                 :            : 
     582                 :            :         /* Clear tailer for debugging */
     583                 :       8185 :         *tailer(f) = 0;
     584                 :            : 
     585                 :            :         /* Now we might have *too* much. */
     586                 :       8185 :         discard_excess(region, hdr, len, location, true);
     587                 :       8185 :         return true;
     588                 :            : }
     589                 :            : 
     590                 :     131260 : bool mem_check(const struct mem_region *region)
     591                 :            : {
     592                 :     131260 :         size_t frees = 0;
     593                 :     131260 :         struct alloc_hdr *hdr, *prev_free = NULL;
     594                 :            :         struct free_hdr *f;
     595                 :            : 
     596                 :            :         /* Check it's sanely aligned. */
     597                 :     131260 :         if (region->start % sizeof(long)) {
     598                 :          0 :                 prerror("Region '%s' not sanely aligned (%llx)\n",
     599                 :            :                         region->name, (unsigned long long)region->start);
     600                 :          0 :                 return false;
     601                 :            :         }
     602                 :     131260 :         if ((long)region->len % sizeof(long)) {
     603                 :          0 :                 prerror("Region '%s' not sane length (%llu)\n",
     604                 :            :                         region->name, (unsigned long long)region->len);
     605                 :          0 :                 return false;
     606                 :            :         }
     607                 :            : 
     608                 :            :         /* Not ours to play with, or empty?  Don't do anything. */
     609                 :     131260 :         if (!(region->type == REGION_MEMORY ||
     610                 :     131253 :               region->type == REGION_SKIBOOT_HEAP) ||
     611                 :     131242 :             region->free_list.n.next == NULL)
     612                 :         30 :                 return true;
     613                 :            : 
     614                 :            :         /* Walk linearly. */
     615                 :     402760 :         for (hdr = region_start(region); hdr; hdr = next_hdr(region, hdr)) {
     616                 :     271530 :                 if (hdr->num_longs < ALLOC_MIN_LONGS) {
     617                 :          0 :                         prerror("Region '%s' %s %p (%s) size %zu\n",
     618                 :            :                                 region->name, hdr->free ? "free" : "alloc",
     619                 :            :                                 hdr, hdr_location(hdr),
     620                 :            :                                 hdr->num_longs * sizeof(long));
     621                 :          0 :                         return false;
     622                 :            :                 }
     623                 :     271530 :                 if ((unsigned long)hdr + hdr->num_longs * sizeof(long) >
     624                 :     271530 :                     region->start + region->len) {
     625                 :          0 :                         prerror("Region '%s' %s %p (%s) oversize %zu\n",
     626                 :            :                                 region->name, hdr->free ? "free" : "alloc",
     627                 :            :                                 hdr, hdr_location(hdr),
     628                 :            :                                 hdr->num_longs * sizeof(long));
     629                 :          0 :                         return false;
     630                 :            :                 }
     631                 :     271530 :                 if (hdr->free) {
     632                 :     131173 :                         if (hdr->prev_free || prev_free) {
     633                 :          0 :                                 prerror("Region '%s' free %p (%s) has prev_free"
     634                 :            :                                         " %p (%s) %sset?\n",
     635                 :            :                                         region->name, hdr, hdr_location(hdr),
     636                 :            :                                         prev_free,
     637                 :            :                                         prev_free ? hdr_location(prev_free)
     638                 :            :                                         : "NULL",
     639                 :            :                                         hdr->prev_free ? "" : "un");
     640                 :          0 :                                 return false;
     641                 :            :                         }
     642                 :     131173 :                         prev_free = hdr;
     643                 :     131173 :                         frees ^= (unsigned long)hdr - region->start;
     644                 :            :                 } else {
     645                 :     140357 :                         if (hdr->prev_free != (bool)prev_free) {
     646                 :          0 :                                 prerror("Region '%s' alloc %p (%s) has"
     647                 :            :                                         " prev_free %p %sset?\n",
     648                 :            :                                         region->name, hdr, hdr_location(hdr),
     649                 :            :                                         prev_free, hdr->prev_free ? "" : "un");
     650                 :          0 :                                 return false;
     651                 :            :                         }
     652                 :     140357 :                         prev_free = NULL;
     653                 :            :                 }
     654                 :            :         }
     655                 :            : 
     656                 :            :         /* Now walk free list. */
     657                 :     262403 :         list_for_each(&region->free_list, f, list)
     658                 :     131173 :                 frees ^= (unsigned long)f - region->start;
     659                 :            : 
     660                 :     131230 :         if (frees) {
     661                 :          0 :                 prerror("Region '%s' free list and walk do not match!\n",
     662                 :            :                         region->name);
     663                 :          0 :                 return false;
     664                 :            :         }
     665                 :     131230 :         return true;
     666                 :            : }
     667                 :            : 
     668                 :          0 : bool mem_check_all(void)
     669                 :            : {
     670                 :            :         struct mem_region *r;
     671                 :            : 
     672                 :          0 :         list_for_each(&regions, r, list) {
     673                 :          0 :                 if (!mem_check(r))
     674                 :          0 :                         return false;
     675                 :            :         }
     676                 :            : 
     677                 :          0 :         return true;
     678                 :            : }
     679                 :            : 
     680                 :         91 : static struct mem_region *new_region(const char *name,
     681                 :            :                                      uint64_t start, uint64_t len,
     682                 :            :                                      struct dt_node *node,
     683                 :            :                                      enum mem_region_type type)
     684                 :            : {
     685                 :            :         struct mem_region *region;
     686                 :            : 
     687                 :         91 :         region = malloc(sizeof(*region));
     688                 :         91 :         if (!region)
     689                 :          0 :                 return NULL;
     690                 :            : 
     691                 :         91 :         region->name = name;
     692                 :         91 :         region->start = start;
     693                 :         91 :         region->len = len;
     694                 :         91 :         region->node = node;
     695                 :         91 :         region->type = type;
     696                 :         91 :         region->free_list.n.next = NULL;
     697                 :         91 :         init_lock(&region->free_list_lock);
     698                 :            : 
     699                 :         91 :         return region;
     700                 :            : }
     701                 :            : 
     702                 :            : /* We always split regions, so we only have to replace one. */
     703                 :         37 : static struct mem_region *split_region(struct mem_region *head,
     704                 :            :                                        uint64_t split_at,
     705                 :            :                                        enum mem_region_type type)
     706                 :            : {
     707                 :            :         struct mem_region *tail;
     708                 :         37 :         uint64_t end = head->start + head->len;
     709                 :            : 
     710                 :         37 :         tail = new_region(head->name, split_at, end - split_at,
     711                 :            :                           head->node, type);
     712                 :            :         /* Original region becomes head. */
     713                 :         37 :         if (tail)
     714                 :         37 :                 head->len -= tail->len;
     715                 :            : 
     716                 :         37 :         return tail;
     717                 :            : }
     718                 :            : 
     719                 :       1094 : static bool intersects(const struct mem_region *region, uint64_t addr)
     720                 :            : {
     721                 :       1530 :         return addr > region->start &&
     722                 :        436 :                 addr < region->start + region->len;
     723                 :            : }
     724                 :            : 
     725                 :       1094 : static bool maybe_split(struct mem_region *r, uint64_t split_at)
     726                 :            : {
     727                 :            :         struct mem_region *tail;
     728                 :            : 
     729                 :       1094 :         if (!intersects(r, split_at))
     730                 :       1058 :                 return true;
     731                 :            : 
     732                 :         36 :         tail = split_region(r, split_at, r->type);
     733                 :         36 :         if (!tail)
     734                 :          0 :                 return false;
     735                 :            : 
     736                 :            :         /* Tail add is important: we may need to split again! */
     737                 :         36 :         list_add_after(&regions, &r->list, &tail->list);
     738                 :         36 :         return true;
     739                 :            : }
     740                 :            : 
     741                 :       1258 : static bool overlaps(const struct mem_region *r1, const struct mem_region *r2)
     742                 :            : {
     743                 :       1258 :         return (r1->start + r1->len > r2->start
     744                 :       1258 :                 && r1->start < r2->start + r2->len);
     745                 :            : }
     746                 :            : 
     747                 :         39 : static bool contains(const struct mem_region *r1, const struct mem_region *r2)
     748                 :            : {
     749                 :         39 :         u64 r1_end = r1->start + r1->len;
     750                 :         39 :         u64 r2_end = r2->start + r2->len;
     751                 :            : 
     752                 :         39 :         return (r1->start <= r2->start && r2_end <= r1_end);
     753                 :            : }
     754                 :            : 
     755                 :        152 : static struct mem_region *get_overlap(const struct mem_region *region)
     756                 :            : {
     757                 :            :         struct mem_region *i;
     758                 :            : 
     759                 :        748 :         list_for_each(&regions, i, list) {
     760                 :        621 :                 if (overlaps(region, i))
     761                 :         25 :                         return i;
     762                 :            :         }
     763                 :        127 :         return NULL;
     764                 :            : }
     765                 :            : 
     766                 :        148 : static void add_region_to_regions(struct mem_region *region)
     767                 :            : {
     768                 :            :         struct mem_region *r;
     769                 :            : 
     770                 :        355 :         list_for_each(&regions, r, list) {
     771                 :        331 :                 if (r->start < region->start)
     772                 :        207 :                         continue;
     773                 :            : 
     774                 :        124 :                 list_add_before(&regions, &r->list, &region->list);
     775                 :        124 :                 return;
     776                 :            :         }
     777                 :         24 :         list_add_tail(&regions, &region->list);
     778                 :            : }
     779                 :            : 
     780                 :        128 : static bool add_region(struct mem_region *region)
     781                 :            : {
     782                 :            :         struct mem_region *r;
     783                 :            : 
     784                 :        128 :         if (mem_regions_finalised) {
     785                 :          1 :                 prerror("MEM: add_region(%s@0x%"PRIx64") called after finalise!\n",
     786                 :            :                                 region->name, region->start);
     787                 :          1 :                 return false;
     788                 :            :         }
     789                 :            : 
     790                 :            :         /* First split any regions which intersect. */
     791                 :        674 :         list_for_each(&regions, r, list) {
     792                 :            :                 /*
     793                 :            :                  * The new region should be fully contained by an existing one.
     794                 :            :                  * If it's not then we have a problem where reservations
     795                 :            :                  * partially overlap which is probably broken.
     796                 :            :                  *
     797                 :            :                  * NB: There *might* be situations where this is legitimate,
     798                 :            :                  * but the region handling does not currently support this.
     799                 :            :                  */
     800                 :        547 :                 if (overlaps(r, region) && !contains(r, region)) {
     801                 :          0 :                         prerror("MEM: Partial overlap detected between regions:\n");
     802                 :          0 :                         prerror("MEM: %s [0x%"PRIx64"-0x%"PRIx64"] (new)\n",
     803                 :            :                                 region->name, region->start,
     804                 :            :                                 region->start + region->len);
     805                 :          0 :                         prerror("MEM: %s [0x%"PRIx64"-0x%"PRIx64"]\n",
     806                 :            :                                 r->name, r->start, r->start + r->len);
     807                 :          0 :                         return false;
     808                 :            :                 }
     809                 :            : 
     810                 :        547 :                 if (!maybe_split(r, region->start) ||
     811                 :        547 :                     !maybe_split(r, region->start + region->len))
     812                 :          0 :                         return false;
     813                 :            :         }
     814                 :            : 
     815                 :            :         /* Now we have only whole overlaps, if any. */
     816                 :        152 :         while ((r = get_overlap(region)) != NULL) {
     817                 :         25 :                 assert(r->start == region->start);
     818                 :         25 :                 assert(r->len == region->len);
     819                 :         25 :                 list_del_from(&regions, &r->list);
     820                 :         25 :                 free(r);
     821                 :            :         }
     822                 :            : 
     823                 :            :         /* Finally, add in our own region. */
     824                 :        127 :         add_region_to_regions(region);
     825                 :        127 :         return true;
     826                 :            : }
     827                 :            : 
     828                 :         25 : static void mem_reserve(enum mem_region_type type, const char *name,
     829                 :            :                 uint64_t start, uint64_t len)
     830                 :            : {
     831                 :            :         struct mem_region *region;
     832                 :         25 :         bool added = true;
     833                 :            : 
     834                 :         25 :         lock(&mem_region_lock);
     835                 :         25 :         region = new_region(name, start, len, NULL, type);
     836                 :         25 :         assert(region);
     837                 :            : 
     838                 :         25 :         if (!mem_region_init_done)
     839                 :          3 :                 list_add(&early_reserves, &region->list);
     840                 :            :         else
     841                 :         22 :                 added = add_region(region);
     842                 :            : 
     843                 :         25 :         assert(added);
     844                 :         25 :         unlock(&mem_region_lock);
     845                 :         25 : }
     846                 :            : 
     847                 :         25 : void mem_reserve_fw(const char *name, uint64_t start, uint64_t len)
     848                 :            : {
     849                 :         25 :         mem_reserve(REGION_FW_RESERVED, name, start, len);
     850                 :         25 : }
     851                 :            : 
     852                 :          0 : void mem_reserve_hwbuf(const char *name, uint64_t start, uint64_t len)
     853                 :            : {
     854                 :          0 :         mem_reserve(REGION_RESERVED, name, start, len);
     855                 :          0 : }
     856                 :            : 
     857                 :          0 : static bool matches_chip_id(const __be32 ids[], size_t num, u32 chip_id)
     858                 :            : {
     859                 :            :         size_t i;
     860                 :            : 
     861                 :          0 :         for (i = 0; i < num; i++)
     862                 :          0 :                 if (be32_to_cpu(ids[i]) == chip_id)
     863                 :          0 :                         return true;
     864                 :            : 
     865                 :          0 :         return false;
     866                 :            : }
     867                 :            : 
     868                 :          0 : void *__local_alloc(unsigned int chip_id, size_t size, size_t align,
     869                 :            :                     const char *location)
     870                 :            : {
     871                 :            :         struct mem_region *region;
     872                 :          0 :         void *p = NULL;
     873                 :          0 :         bool use_local = true;
     874                 :            : 
     875                 :          0 :         lock(&mem_region_lock);
     876                 :            : 
     877                 :          0 : restart:
     878                 :          0 :         list_for_each(&regions, region, list) {
     879                 :            :                 const struct dt_property *prop;
     880                 :            :                 const __be32 *ids;
     881                 :            : 
     882                 :          0 :                 if (!(region->type == REGION_SKIBOOT_HEAP ||
     883                 :          0 :                       region->type == REGION_MEMORY))
     884                 :          0 :                         continue;
     885                 :            : 
     886                 :            :                 /* Don't allocate from normal heap. */
     887                 :          0 :                 if (region == &skiboot_heap)
     888                 :          0 :                         continue;
     889                 :            : 
     890                 :            :                 /* First pass, only match node local regions */
     891                 :          0 :                 if (use_local) {
     892                 :          0 :                         if (!region->node)
     893                 :          0 :                                 continue;
     894                 :          0 :                         prop = dt_find_property(region->node, "ibm,chip-id");
     895                 :          0 :                         ids = (const __be32 *)prop->prop;
     896                 :          0 :                         if (!matches_chip_id(ids, prop->len/sizeof(u32),
     897                 :            :                                              chip_id))
     898                 :          0 :                                 continue;
     899                 :            :                 }
     900                 :            : 
     901                 :            :                 /* Second pass, match anything */
     902                 :          0 :                 lock(&region->free_list_lock);
     903                 :          0 :                 p = mem_alloc(region, size, align, location);
     904                 :          0 :                 unlock(&region->free_list_lock);
     905                 :          0 :                 if (p)
     906                 :          0 :                         break;
     907                 :            :         }
     908                 :            : 
     909                 :            :         /*
     910                 :            :          * If we can't allocate the memory block from the expected
     911                 :            :          * node, we bail to any one that can accommodate our request.
     912                 :            :          */
     913                 :          0 :         if (!p && use_local) {
     914                 :          0 :                 use_local = false;
     915                 :          0 :                 goto restart;
     916                 :            :         }
     917                 :            : 
     918                 :          0 :         unlock(&mem_region_lock);
     919                 :            : 
     920                 :          0 :         return p;
     921                 :            : }
     922                 :            : 
     923                 :          0 : static struct mem_region *mem_to_region(void *mem)
     924                 :            : {
     925                 :            :         struct mem_region *region;
     926                 :            : 
     927                 :          0 :         list_for_each(&regions, region, list) {
     928                 :          0 :                 if (mem < region_start(region))
     929                 :          0 :                         continue;
     930                 :          0 :                 if (mem >= region_start(region) + region->len)
     931                 :          0 :                         continue;
     932                 :          0 :                 return region;
     933                 :            :         }
     934                 :          0 :         return NULL;
     935                 :            : }
     936                 :            : 
     937                 :          0 : void __local_free(void *mem, const char *location)
     938                 :            : {
     939                 :            :         struct mem_region *region;
     940                 :            : 
     941                 :          0 :         lock(&mem_region_lock);
     942                 :            : 
     943                 :          0 :         region = mem_to_region(mem);
     944                 :          0 :         if (!region) {
     945                 :          0 :                 prerror("MEM: local_free mem=%p no matching region.\n", mem);
     946                 :          0 :                 unlock(&mem_region_lock);
     947                 :          0 :                 return;
     948                 :            :         }
     949                 :            : 
     950                 :          0 :         lock(&region->free_list_lock);
     951                 :          0 :         mem_free(region, mem, location);
     952                 :          0 :         unlock(&region->free_list_lock);
     953                 :            : 
     954                 :          0 :         unlock(&mem_region_lock);
     955                 :            : }
     956                 :            : 
     957                 :          0 : struct mem_region *find_mem_region(const char *name)
     958                 :            : {
     959                 :            :         struct mem_region *region;
     960                 :            : 
     961                 :          0 :         list_for_each(&regions, region, list) {
     962                 :          0 :                 if (streq(region->name, name))
     963                 :          0 :                         return region;
     964                 :            :         }
     965                 :          0 :         return NULL;
     966                 :            : }
     967                 :            : 
     968                 :         14 : bool mem_range_is_reserved(uint64_t start, uint64_t size)
     969                 :            : {
     970                 :         14 :         uint64_t end = start + size;
     971                 :            :         struct mem_region *region;
     972                 :            :         struct list_head *search;
     973                 :            : 
     974                 :            :         /* We may have the range covered by a number of regions, which could
     975                 :            :          * appear in any order. So, we look for a region that covers the
     976                 :            :          * start address, and bump start up to the end of that region.
     977                 :            :          *
     978                 :            :          * We repeat until we've either bumped past the end of the range,
     979                 :            :          * or we didn't find a matching region.
     980                 :            :          *
     981                 :            :          * This has a worst-case of O(n^2), but n is well bounded by the
     982                 :            :          * small number of reservations.
     983                 :            :          */
     984                 :            : 
     985                 :         14 :         if (!mem_region_init_done)
     986                 :          0 :                 search = &early_reserves;
     987                 :            :         else
     988                 :         14 :                 search = &regions;
     989                 :            : 
     990                 :          3 :         for (;;) {
     991                 :         17 :                 bool found = false;
     992                 :            : 
     993                 :        145 :                 list_for_each(search, region, list) {
     994                 :        128 :                         if (!region_is_reserved(region))
     995                 :         34 :                                 continue;
     996                 :            : 
     997                 :            :                         /* does this region overlap the start address, and
     998                 :            :                          * have a non-zero size? */
     999                 :         94 :                         if (region->start <= start &&
    1000                 :         20 :                                         region->start + region->len > start &&
    1001                 :         15 :                                         region->len) {
    1002                 :         15 :                                 start = region->start + region->len;
    1003                 :         15 :                                 found = true;
    1004                 :            :                         }
    1005                 :            :                 }
    1006                 :            : 
    1007                 :            :                 /* 'end' is the first byte outside of the range */
    1008                 :         17 :                 if (start >= end)
    1009                 :          8 :                         return true;
    1010                 :            : 
    1011                 :          9 :                 if (!found)
    1012                 :          6 :                         break;
    1013                 :            :         }
    1014                 :            : 
    1015                 :          6 :         return false;
    1016                 :            : }
    1017                 :            : 
    1018                 :         19 : static void mem_region_parse_reserved_properties(void)
    1019                 :            : {
    1020                 :            :         const struct dt_property *names, *ranges;
    1021                 :            :         struct mem_region *region;
    1022                 :            : 
    1023                 :         19 :         prlog(PR_DEBUG, "MEM: parsing reserved memory from "
    1024                 :            :                         "reserved-names/-ranges properties\n");
    1025                 :            : 
    1026                 :         19 :         names = dt_find_property(dt_root, "reserved-names");
    1027                 :         19 :         ranges = dt_find_property(dt_root, "reserved-ranges");
    1028                 :         20 :         if (names && ranges) {
    1029                 :            :                 const uint64_t *range;
    1030                 :            :                 int n, len;
    1031                 :            : 
    1032                 :          1 :                 range = (const void *)ranges->prop;
    1033                 :            : 
    1034                 :          4 :                 for (n = 0; n < names->len; n += len, range += 2) {
    1035                 :            :                         char *name;
    1036                 :            : 
    1037                 :          3 :                         len = strlen(names->prop + n) + 1;
    1038                 :          3 :                         name = strdup(names->prop + n);
    1039                 :            : 
    1040                 :          3 :                         region = new_region(name,
    1041                 :            :                                         dt_get_number(range, 2),
    1042                 :          3 :                                         dt_get_number(range + 1, 2),
    1043                 :            :                                         NULL, REGION_FW_RESERVED);
    1044                 :          3 :                         if (!add_region(region)) {
    1045                 :          0 :                                 prerror("Couldn't add mem_region %s\n", name);
    1046                 :          0 :                                 abort();
    1047                 :            :                         }
    1048                 :            :                 }
    1049                 :         18 :         } else if (names || ranges) {
    1050                 :          0 :                 prerror("Invalid properties: reserved-names=%p "
    1051                 :            :                                 "with reserved-ranges=%p\n",
    1052                 :            :                                 names, ranges);
    1053                 :          0 :                 abort();
    1054                 :            :         } else {
    1055                 :         18 :                 return;
    1056                 :            :         }
    1057                 :            : }
    1058                 :            : 
    1059                 :         38 : static bool mem_region_parse_reserved_nodes(const char *path)
    1060                 :            : {
    1061                 :            :         struct dt_node *parent, *node;
    1062                 :            : 
    1063                 :         38 :         parent = dt_find_by_path(dt_root, path);
    1064                 :         38 :         if (!parent)
    1065                 :         38 :                 return false;
    1066                 :            : 
    1067                 :          0 :         prlog(PR_INFO, "MEM: parsing reserved memory from node %s\n", path);
    1068                 :            : 
    1069                 :          0 :         dt_for_each_child(parent, node) {
    1070                 :            :                 const struct dt_property *reg;
    1071                 :            :                 struct mem_region *region;
    1072                 :            :                 int type;
    1073                 :            : 
    1074                 :          0 :                 reg = dt_find_property(node, "reg");
    1075                 :          0 :                 if (!reg) {
    1076                 :          0 :                         char *nodepath = dt_get_path(node);
    1077                 :          0 :                         prerror("node %s has no reg property, ignoring\n",
    1078                 :            :                                         nodepath);
    1079                 :          0 :                         free(nodepath);
    1080                 :          0 :                         continue;
    1081                 :            :                 }
    1082                 :            : 
    1083                 :          0 :                 if (dt_has_node_property(node, "no-map", NULL))
    1084                 :          0 :                         type = REGION_RESERVED;
    1085                 :            :                 else
    1086                 :          0 :                         type = REGION_FW_RESERVED;
    1087                 :            : 
    1088                 :          0 :                 region = new_region(strdup(node->name),
    1089                 :          0 :                                 dt_get_number(reg->prop, 2),
    1090                 :          0 :                                 dt_get_number(reg->prop + sizeof(u64), 2),
    1091                 :            :                                 node, type);
    1092                 :          0 :                 if (!add_region(region)) {
    1093                 :          0 :                         char *nodepath = dt_get_path(node);
    1094                 :          0 :                         prerror("node %s failed to add_region()\n", nodepath);
    1095                 :          0 :                         free(nodepath);
    1096                 :            :                 }
    1097                 :            :         }
    1098                 :            : 
    1099                 :          0 :         return true;
    1100                 :            : }
    1101                 :            : 
    1102                 :            : /* Trawl through device tree, create memory regions from nodes. */
    1103                 :         19 : void mem_region_init(void)
    1104                 :            : {
    1105                 :            :         struct mem_region *region, *next;
    1106                 :            :         struct dt_node *i;
    1107                 :            :         bool rc;
    1108                 :            : 
    1109                 :            :         /*
    1110                 :            :          * Add associativity properties outside of the lock
    1111                 :            :          * to avoid recursive locking caused by allocations
    1112                 :            :          * done by add_chip_dev_associativity()
    1113                 :            :          */
    1114                 :        118 :         dt_for_each_node(dt_root, i) {
    1115                 :         99 :                 if (!dt_has_node_property(i, "device_type", "memory") &&
    1116                 :         78 :                     !dt_has_node_property(i, "compatible", "pmem-region"))
    1117                 :         78 :                         continue;
    1118                 :            : 
    1119                 :            :                 /* Add associativity properties */
    1120                 :         21 :                 add_chip_dev_associativity(i);
    1121                 :            :         }
    1122                 :            : 
    1123                 :            :         /* Add each memory node. */
    1124                 :        118 :         dt_for_each_node(dt_root, i) {
    1125                 :            :                 uint64_t start, len;
    1126                 :            :                 char *rname;
    1127                 :            : #define NODE_REGION_PREFIX      "ibm,firmware-allocs-"
    1128                 :            : 
    1129                 :         99 :                 if (!dt_has_node_property(i, "device_type", "memory"))
    1130                 :         78 :                         continue;
    1131                 :         21 :                 rname = zalloc(strlen(i->name) + strlen(NODE_REGION_PREFIX) + 1);
    1132                 :         21 :                 assert(rname);
    1133                 :         21 :                 strcat(rname, NODE_REGION_PREFIX);
    1134                 :         21 :                 strcat(rname, i->name);
    1135                 :         21 :                 start = dt_get_address(i, 0, &len);
    1136                 :         21 :                 lock(&mem_region_lock);
    1137                 :         21 :                 region = new_region(rname, start, len, i, REGION_MEMORY);
    1138                 :         21 :                 if (!region) {
    1139                 :          0 :                         prerror("MEM: Could not add mem region %s!\n", i->name);
    1140                 :          0 :                         abort();
    1141                 :            :                 }
    1142                 :         21 :                 add_region_to_regions(region);
    1143                 :         21 :                 if ((start + len) > top_of_ram)
    1144                 :          8 :                         top_of_ram = start + len;
    1145                 :         21 :                 unlock(&mem_region_lock);
    1146                 :            :         }
    1147                 :            : 
    1148                 :            :         /*
    1149                 :            :          * This is called after we know the maximum PIR of all CPUs,
    1150                 :            :          * so we can dynamically set the stack length.
    1151                 :            :          */
    1152                 :         19 :         skiboot_cpu_stacks.len = (cpu_max_pir + 1) * STACK_SIZE;
    1153                 :            : 
    1154                 :         19 :         lock(&mem_region_lock);
    1155                 :            : 
    1156                 :            :         /* Now carve out our own reserved areas. */
    1157                 :         19 :         if (!add_region(&skiboot_os_reserve) ||
    1158                 :         19 :             !add_region(&skiboot_code_and_text) ||
    1159                 :         19 :             !add_region(&skiboot_heap) ||
    1160                 :         19 :             !add_region(&skiboot_after_heap) ||
    1161                 :         19 :             !add_region(&skiboot_cpu_stacks)) {
    1162                 :          0 :                 prerror("Out of memory adding skiboot reserved areas\n");
    1163                 :          0 :                 abort();
    1164                 :            :         }
    1165                 :            : 
    1166                 :         19 :         if (chip_quirk(QUIRK_MAMBO_CALLOUTS)) {
    1167                 :          0 :                 if (!add_region(&skiboot_mambo_kernel) ||
    1168                 :          0 :                     !add_region(&skiboot_mambo_initramfs)) {
    1169                 :          0 :                         prerror("Out of memory adding mambo payload\n");
    1170                 :          0 :                         abort();
    1171                 :            :                 }
    1172                 :            :         }
    1173                 :            : 
    1174                 :            :         /* Add reserved reanges from HDAT */
    1175                 :         22 :         list_for_each_safe(&early_reserves, region, next, list) {
    1176                 :            :                 bool added;
    1177                 :            : 
    1178                 :          3 :                 list_del(&region->list);
    1179                 :          3 :                 added = add_region(region);
    1180                 :          3 :                 assert(added);
    1181                 :            :         }
    1182                 :            : 
    1183                 :            :         /* Add reserved ranges from the DT */
    1184                 :         19 :         rc = mem_region_parse_reserved_nodes("/reserved-memory");
    1185                 :         19 :         if (!rc)
    1186                 :         19 :                 rc = mem_region_parse_reserved_nodes(
    1187                 :            :                                 "/ibm,hostboot/reserved-memory");
    1188                 :         19 :         if (!rc)
    1189                 :         19 :                 mem_region_parse_reserved_properties();
    1190                 :            : 
    1191                 :         19 :         mem_region_init_done = true;
    1192                 :         19 :         unlock(&mem_region_lock);
    1193                 :         19 : }
    1194                 :            : 
    1195                 :         12 : static uint64_t allocated_length(const struct mem_region *r)
    1196                 :            : {
    1197                 :         12 :         struct free_hdr *f, *last = NULL;
    1198                 :            : 
    1199                 :            :         /* No allocations at all? */
    1200                 :         12 :         if (r->free_list.n.next == NULL)
    1201                 :         10 :                 return 0;
    1202                 :            : 
    1203                 :            :         /* Find last free block. */
    1204                 :          6 :         list_for_each(&r->free_list, f, list)
    1205                 :          4 :                 if (f > last)
    1206                 :          3 :                         last = f;
    1207                 :            : 
    1208                 :            :         /* No free blocks? */
    1209                 :          2 :         if (!last)
    1210                 :          0 :                 return r->len;
    1211                 :            : 
    1212                 :            :         /* Last free block isn't at end? */
    1213                 :          2 :         if (next_hdr(r, &last->hdr))
    1214                 :          0 :                 return r->len;
    1215                 :          2 :         return (unsigned long)last - r->start;
    1216                 :            : }
    1217                 :            : 
    1218                 :            : /* Separate out allocated sections into their own region. */
    1219                 :          4 : void mem_region_release_unused(void)
    1220                 :            : {
    1221                 :            :         struct mem_region *r;
    1222                 :            : 
    1223                 :          4 :         lock(&mem_region_lock);
    1224                 :          4 :         assert(!mem_regions_finalised);
    1225                 :            : 
    1226                 :          4 :         prlog(PR_INFO, "Releasing unused memory:\n");
    1227                 :         43 :         list_for_each(&regions, r, list) {
    1228                 :            :                 uint64_t used_len;
    1229                 :            : 
    1230                 :            :                 /* If it's not allocatable, ignore it. */
    1231                 :         39 :                 if (!(r->type == REGION_SKIBOOT_HEAP ||
    1232                 :         35 :                       r->type == REGION_MEMORY))
    1233                 :         27 :                         continue;
    1234                 :            : 
    1235                 :         12 :                 used_len = allocated_length(r);
    1236                 :            : 
    1237                 :         12 :                 prlog(PR_INFO, "    %s: %llu/%llu used\n",
    1238                 :            :                        r->name, (long long)used_len, (long long)r->len);
    1239                 :            : 
    1240                 :            :                 /* We keep the skiboot heap. */
    1241                 :         12 :                 if (r == &skiboot_heap)
    1242                 :          4 :                         continue;
    1243                 :            : 
    1244                 :            :                 /* Nothing used?  Whole thing is for Linux. */
    1245                 :          8 :                 if (used_len == 0)
    1246                 :          7 :                         r->type = REGION_OS;
    1247                 :            :                 /* Partially used?  Split region. */
    1248                 :          1 :                 else if (used_len != r->len) {
    1249                 :            :                         struct mem_region *for_linux;
    1250                 :          1 :                         struct free_hdr *last = region_start(r) + used_len;
    1251                 :            : 
    1252                 :            :                         /* Remove the final free block. */
    1253                 :          1 :                         list_del_from(&r->free_list, &last->list);
    1254                 :            : 
    1255                 :          1 :                         for_linux = split_region(r, r->start + used_len,
    1256                 :            :                                                  REGION_OS);
    1257                 :          1 :                         if (!for_linux) {
    1258                 :          0 :                                 prerror("OOM splitting mem node %s for linux\n",
    1259                 :            :                                         r->name);
    1260                 :          0 :                                 abort();
    1261                 :            :                         }
    1262                 :          1 :                         list_add(&regions, &for_linux->list);
    1263                 :            :                 }
    1264                 :            :         }
    1265                 :          4 :         unlock(&mem_region_lock);
    1266                 :          4 : }
    1267                 :            : 
    1268                 :          0 : static void mem_clear_range(uint64_t s, uint64_t e)
    1269                 :            : {
    1270                 :            :         uint64_t res_start, res_end;
    1271                 :            : 
    1272                 :            :         /* Skip exception vectors */
    1273                 :          0 :         if (s < EXCEPTION_VECTORS_END)
    1274                 :          0 :                 s = EXCEPTION_VECTORS_END;
    1275                 :            : 
    1276                 :            :         /* Skip kernel preload area */
    1277                 :          0 :         res_start = (uint64_t)KERNEL_LOAD_BASE;
    1278                 :          0 :         res_end = res_start + KERNEL_LOAD_SIZE;
    1279                 :            : 
    1280                 :          0 :         if (s >= res_start && s < res_end)
    1281                 :          0 :                s = res_end;
    1282                 :          0 :         if (e > res_start && e <= res_end)
    1283                 :          0 :                e = res_start;
    1284                 :          0 :         if (e <= s)
    1285                 :          0 :                 return;
    1286                 :          0 :         if (s < res_start && e > res_end) {
    1287                 :          0 :                 mem_clear_range(s, res_start);
    1288                 :          0 :                 mem_clear_range(res_end, e);
    1289                 :          0 :                 return;
    1290                 :            :         }
    1291                 :            : 
    1292                 :            :         /* Skip initramfs preload area */
    1293                 :          0 :         res_start = (uint64_t)INITRAMFS_LOAD_BASE;
    1294                 :          0 :         res_end = res_start + INITRAMFS_LOAD_SIZE;
    1295                 :            : 
    1296                 :          0 :         if (s >= res_start && s < res_end)
    1297                 :          0 :                s = res_end;
    1298                 :          0 :         if (e > res_start && e <= res_end)
    1299                 :          0 :                e = res_start;
    1300                 :          0 :         if (e <= s)
    1301                 :          0 :                 return;
    1302                 :          0 :         if (s < res_start && e > res_end) {
    1303                 :          0 :                 mem_clear_range(s, res_start);
    1304                 :          0 :                 mem_clear_range(res_end, e);
    1305                 :          0 :                 return;
    1306                 :            :         }
    1307                 :            : 
    1308                 :          0 :         prlog(PR_DEBUG, "Clearing region %llx-%llx\n",
    1309                 :            :               (long long)s, (long long)e);
    1310                 :          0 :         memset((void *)s, 0, e - s);
    1311                 :            : }
    1312                 :            : 
    1313                 :            : struct mem_region_clear_job_args {
    1314                 :            :         char *job_name;
    1315                 :            :         uint64_t s,e;
    1316                 :            : };
    1317                 :            : 
    1318                 :          0 : static void mem_region_clear_job(void *data)
    1319                 :            : {
    1320                 :          0 :         struct mem_region_clear_job_args *arg = (struct mem_region_clear_job_args*)data;
    1321                 :          0 :         mem_clear_range(arg->s, arg->e);
    1322                 :          0 : }
    1323                 :            : 
    1324                 :            : #define MEM_REGION_CLEAR_JOB_SIZE (16ULL*(1<<30))
    1325                 :            : 
    1326                 :            : static struct cpu_job **mem_clear_jobs;
    1327                 :            : static struct mem_region_clear_job_args *mem_clear_job_args;
    1328                 :            : static int mem_clear_njobs = 0;
    1329                 :            : 
    1330                 :          0 : void start_mem_region_clear_unused(void)
    1331                 :            : {
    1332                 :            :         struct mem_region *r;
    1333                 :            :         uint64_t s,l;
    1334                 :          0 :         uint64_t total = 0;
    1335                 :            :         uint32_t chip_id;
    1336                 :            :         char *path;
    1337                 :            :         int i;
    1338                 :            :         struct cpu_job **jobs;
    1339                 :            :         struct mem_region_clear_job_args *job_args;
    1340                 :            : 
    1341                 :          0 :         lock(&mem_region_lock);
    1342                 :          0 :         assert(mem_regions_finalised);
    1343                 :            : 
    1344                 :          0 :         mem_clear_njobs = 0;
    1345                 :            : 
    1346                 :          0 :         list_for_each(&regions, r, list) {
    1347                 :          0 :                 if (!(r->type == REGION_OS))
    1348                 :          0 :                         continue;
    1349                 :          0 :                 mem_clear_njobs++;
    1350                 :            :                 /* One job per 16GB */
    1351                 :          0 :                 mem_clear_njobs += r->len / MEM_REGION_CLEAR_JOB_SIZE;
    1352                 :            :         }
    1353                 :            : 
    1354                 :          0 :         jobs = malloc(mem_clear_njobs * sizeof(struct cpu_job*));
    1355                 :          0 :         job_args = malloc(mem_clear_njobs * sizeof(struct mem_region_clear_job_args));
    1356                 :          0 :         mem_clear_jobs = jobs;
    1357                 :          0 :         mem_clear_job_args = job_args;
    1358                 :            : 
    1359                 :          0 :         prlog(PR_NOTICE, "Clearing unused memory:\n");
    1360                 :          0 :         i = 0;
    1361                 :          0 :         list_for_each(&regions, r, list) {
    1362                 :            :                 /* If it's not unused, ignore it. */
    1363                 :          0 :                 if (!(r->type == REGION_OS))
    1364                 :          0 :                         continue;
    1365                 :            : 
    1366                 :          0 :                 assert(r != &skiboot_heap);
    1367                 :            : 
    1368                 :          0 :                 s = r->start;
    1369                 :          0 :                 l = r->len;
    1370                 :          0 :                 while(l > MEM_REGION_CLEAR_JOB_SIZE) {
    1371                 :          0 :                         job_args[i].s = s+l - MEM_REGION_CLEAR_JOB_SIZE;
    1372                 :          0 :                         job_args[i].e = s+l;
    1373                 :          0 :                         l-=MEM_REGION_CLEAR_JOB_SIZE;
    1374                 :          0 :                         job_args[i].job_name = malloc(sizeof(char)*100);
    1375                 :          0 :                         total+=MEM_REGION_CLEAR_JOB_SIZE;
    1376                 :          0 :                         chip_id = __dt_get_chip_id(r->node);
    1377                 :          0 :                         if (chip_id == -1)
    1378                 :          0 :                                 chip_id = 0;
    1379                 :          0 :                         path = dt_get_path(r->node);
    1380                 :          0 :                         snprintf(job_args[i].job_name, 100,
    1381                 :            :                                  "clear %s, %s 0x%"PRIx64" len: %"PRIx64" on %d",
    1382                 :            :                                  r->name, path,
    1383                 :          0 :                                  job_args[i].s,
    1384                 :          0 :                                  (job_args[i].e - job_args[i].s),
    1385                 :            :                                  chip_id);
    1386                 :          0 :                         free(path);
    1387                 :          0 :                         jobs[i] = cpu_queue_job_on_node(chip_id,
    1388                 :          0 :                                                         job_args[i].job_name,
    1389                 :            :                                                         mem_region_clear_job,
    1390                 :          0 :                                                         &job_args[i]);
    1391                 :          0 :                         if (!jobs[i])
    1392                 :          0 :                                 jobs[i] = cpu_queue_job(NULL,
    1393                 :          0 :                                                         job_args[i].job_name,
    1394                 :            :                                                         mem_region_clear_job,
    1395                 :          0 :                                                         &job_args[i]);
    1396                 :          0 :                         assert(jobs[i]);
    1397                 :          0 :                         i++;
    1398                 :            :                 }
    1399                 :          0 :                 job_args[i].s = s;
    1400                 :          0 :                 job_args[i].e = s+l;
    1401                 :          0 :                 job_args[i].job_name = malloc(sizeof(char)*100);
    1402                 :          0 :                 total+=l;
    1403                 :          0 :                 chip_id = __dt_get_chip_id(r->node);
    1404                 :          0 :                 if (chip_id == -1)
    1405                 :          0 :                         chip_id = 0;
    1406                 :          0 :                 path = dt_get_path(r->node);
    1407                 :          0 :                 snprintf(job_args[i].job_name,100,
    1408                 :            :                          "clear %s, %s 0x%"PRIx64" len: 0x%"PRIx64" on %d",
    1409                 :            :                          r->name, path,
    1410                 :          0 :                          job_args[i].s,
    1411                 :          0 :                          (job_args[i].e - job_args[i].s),
    1412                 :            :                          chip_id);
    1413                 :          0 :                 free(path);
    1414                 :          0 :                 jobs[i] = cpu_queue_job_on_node(chip_id,
    1415                 :          0 :                                                 job_args[i].job_name,
    1416                 :            :                                                 mem_region_clear_job,
    1417                 :          0 :                                                 &job_args[i]);
    1418                 :          0 :                 if (!jobs[i])
    1419                 :          0 :                         jobs[i] = cpu_queue_job(NULL,
    1420                 :          0 :                                                 job_args[i].job_name,
    1421                 :            :                                                 mem_region_clear_job,
    1422                 :          0 :                                                 &job_args[i]);
    1423                 :          0 :                 assert(jobs[i]);
    1424                 :          0 :                 i++;
    1425                 :            :         }
    1426                 :          0 :         unlock(&mem_region_lock);
    1427                 :          0 :         cpu_process_local_jobs();
    1428                 :          0 : }
    1429                 :            : 
    1430                 :          0 : void wait_mem_region_clear_unused(void)
    1431                 :            : {
    1432                 :            :         uint64_t l;
    1433                 :          0 :         uint64_t total = 0;
    1434                 :            :         int i;
    1435                 :            : 
    1436                 :          0 :         for(i=0; i < mem_clear_njobs; i++) {
    1437                 :          0 :                 total += (mem_clear_job_args[i].e - mem_clear_job_args[i].s);
    1438                 :            :         }
    1439                 :            : 
    1440                 :          0 :         l = 0;
    1441                 :          0 :         for(i=0; i < mem_clear_njobs; i++) {
    1442                 :          0 :                 cpu_wait_job(mem_clear_jobs[i], true);
    1443                 :          0 :                 l += (mem_clear_job_args[i].e - mem_clear_job_args[i].s);
    1444                 :          0 :                 printf("Clearing memory... %"PRIu64"/%"PRIu64"GB done\n",
    1445                 :            :                        l>>30, total>>30);
    1446                 :          0 :                 free(mem_clear_job_args[i].job_name);
    1447                 :            :         }
    1448                 :          0 :         free(mem_clear_jobs);
    1449                 :          0 :         free(mem_clear_job_args);
    1450                 :          0 : }
    1451                 :            : 
    1452                 :          8 : static void mem_region_add_dt_reserved_node(struct dt_node *parent,
    1453                 :            :                 struct mem_region *region)
    1454                 :            : {
    1455                 :            :         char *name, *p;
    1456                 :            : 
    1457                 :            :         /* If a reserved region was established before skiboot, it may be
    1458                 :            :          * referenced by a device-tree node with extra data. In that case,
    1459                 :            :          * copy the node to /reserved-memory/, unless it's already there.
    1460                 :            :          *
    1461                 :            :          * We update region->node to the new copy here, as the prd code may
    1462                 :            :          * update regions' device-tree nodes, and we want those updates to
    1463                 :            :          * apply to the nodes in /reserved-memory/.
    1464                 :            :          */
    1465                 :          8 :         if (region->type == REGION_FW_RESERVED && region->node) {
    1466                 :          0 :                 if (region->node->parent != parent)
    1467                 :          0 :                         region->node = dt_copy(region->node, parent);
    1468                 :          0 :                 return;
    1469                 :            :         }
    1470                 :            : 
    1471                 :          8 :         name = strdup(region->name);
    1472                 :          8 :         assert(name);
    1473                 :            : 
    1474                 :            :         /* remove any cell addresses in the region name; we have our own cell
    1475                 :            :          * addresses here */
    1476                 :          8 :         p = strchr(name, '@');
    1477                 :          8 :         if (p)
    1478                 :          0 :                 *p = '\0';
    1479                 :            : 
    1480                 :          8 :         region->node = dt_new_addr(parent, name, region->start);
    1481                 :          8 :         assert(region->node);
    1482                 :          8 :         dt_add_property_u64s(region->node, "reg", region->start, region->len);
    1483                 :            : 
    1484                 :            :         /*
    1485                 :            :          * This memory is used by hardware and may need special handling. Ask
    1486                 :            :          * the host kernel not to map it by default.
    1487                 :            :          */
    1488                 :          8 :         if (region->type == REGION_RESERVED)
    1489                 :          0 :                 dt_add_property(region->node, "no-map", NULL, 0);
    1490                 :            : 
    1491                 :          8 :         free(name);
    1492                 :            : }
    1493                 :            : 
    1494                 :          1 : void mem_region_add_dt_reserved(void)
    1495                 :            : {
    1496                 :            :         int names_len, ranges_len, len;
    1497                 :            :         const struct dt_property *prop;
    1498                 :            :         struct mem_region *region;
    1499                 :            :         void *names, *ranges;
    1500                 :            :         struct dt_node *node;
    1501                 :            :         fdt64_t *range;
    1502                 :            :         char *name;
    1503                 :            : 
    1504                 :          1 :         names_len = 0;
    1505                 :          1 :         ranges_len = 0;
    1506                 :            : 
    1507                 :            :         /* Finalise the region list, so we know that the regions list won't be
    1508                 :            :          * altered after this point. The regions' free lists may change after
    1509                 :            :          * we drop the lock, but we don't access those. */
    1510                 :          1 :         lock(&mem_region_lock);
    1511                 :          1 :         mem_regions_finalised = true;
    1512                 :            : 
    1513                 :            :         /* establish top-level reservation node */
    1514                 :          1 :         node = dt_find_by_path(dt_root, "reserved-memory");
    1515                 :          1 :         if (!node) {
    1516                 :          1 :                 node = dt_new(dt_root, "reserved-memory");
    1517                 :          1 :                 dt_add_property_cells(node, "#address-cells", 2);
    1518                 :          1 :                 dt_add_property_cells(node, "#size-cells", 2);
    1519                 :          1 :                 dt_add_property(node, "ranges", NULL, 0);
    1520                 :            :         }
    1521                 :            : 
    1522                 :          1 :         prlog(PR_INFO, "Reserved regions:\n");
    1523                 :            : 
    1524                 :            :         /* First pass, create /reserved-memory/ nodes for each reservation,
    1525                 :            :          * and calculate the length for the /reserved-names and
    1526                 :            :          * /reserved-ranges properties */
    1527                 :         15 :         list_for_each(&regions, region, list) {
    1528                 :         14 :                 if (!region_is_reservable(region))
    1529                 :          6 :                         continue;
    1530                 :            : 
    1531                 :          8 :                 prlog(PR_INFO, "  0x%012llx..%012llx : %s\n",
    1532                 :            :                        (long long)region->start,
    1533                 :            :                        (long long)(region->start + region->len - 1),
    1534                 :            :                        region->name);
    1535                 :            : 
    1536                 :          8 :                 mem_region_add_dt_reserved_node(node, region);
    1537                 :            : 
    1538                 :            :                 /* calculate the size of the properties populated later */
    1539                 :          8 :                 names_len += strlen(region->node->name) + 1;
    1540                 :          8 :                 ranges_len += 2 * sizeof(uint64_t);
    1541                 :            :         }
    1542                 :            : 
    1543                 :          1 :         name = names = malloc(names_len);
    1544                 :          1 :         range = ranges = malloc(ranges_len);
    1545                 :            : 
    1546                 :            :         /* Second pass: populate the old-style reserved-names and
    1547                 :            :          * reserved-regions arrays based on the node data */
    1548                 :         15 :         list_for_each(&regions, region, list) {
    1549                 :         14 :                 if (!region_is_reservable(region))
    1550                 :          6 :                         continue;
    1551                 :            : 
    1552                 :          8 :                 len = strlen(region->node->name) + 1;
    1553                 :          8 :                 memcpy(name, region->node->name, len);
    1554                 :          8 :                 name += len;
    1555                 :            : 
    1556                 :          8 :                 range[0] = cpu_to_fdt64(region->start);
    1557                 :          8 :                 range[1] = cpu_to_fdt64(region->len);
    1558                 :          8 :                 range += 2;
    1559                 :            :         }
    1560                 :          1 :         unlock(&mem_region_lock);
    1561                 :            : 
    1562                 :          1 :         prop = dt_find_property(dt_root, "reserved-names");
    1563                 :          1 :         if (prop)
    1564                 :          0 :                 dt_del_property(dt_root, (struct dt_property *)prop);
    1565                 :            : 
    1566                 :          1 :         prop = dt_find_property(dt_root, "reserved-ranges");
    1567                 :          1 :         if (prop)
    1568                 :          0 :                 dt_del_property(dt_root, (struct dt_property *)prop);
    1569                 :            : 
    1570                 :          1 :         dt_add_property(dt_root, "reserved-names", names, names_len);
    1571                 :          1 :         dt_add_property(dt_root, "reserved-ranges", ranges, ranges_len);
    1572                 :            : 
    1573                 :          1 :         free(names);
    1574                 :          1 :         free(ranges);
    1575                 :          1 : }
    1576                 :            : 
    1577                 :          4 : struct mem_region *mem_region_next(struct mem_region *region)
    1578                 :            : {
    1579                 :            :         struct list_node *node;
    1580                 :            : 
    1581                 :          4 :         assert(lock_held_by_me(&mem_region_lock));
    1582                 :            : 
    1583                 :          4 :         node = region ? &region->list : &regions.n;
    1584                 :            : 
    1585                 :          4 :         if (node->next == &regions.n)
    1586                 :          2 :                 return NULL;
    1587                 :            : 
    1588                 :          2 :         return list_entry(node->next, struct mem_region, list);
    1589                 :            : }

Generated by: LCOV version 1.14