Add "flags" in lmp_property to save the "no-map" property of reserved region
and a new function lmb_reserve_flags() to check this flag.

The default allocation use flags = LMB_NONE.

The adjacent reserved memory region are merged only when they have
the same flags value.

This patch is partially based on flags support done in Linux kernel
mm/memblock .c (previously lmb.c); it is why LMB_NOMAP = 0x4, it is
aligned with MEMBLOCK_NOMAP value.

Signed-off-by: Patrick Delaunay <patrick.delau...@st.com>
---

 include/lmb.h | 20 ++++++++++++++++++++
 lib/lmb.c     | 52 ++++++++++++++++++++++++++++++++++++++++++---------
 2 files changed, 63 insertions(+), 9 deletions(-)

diff --git a/include/lmb.h b/include/lmb.h
index e9f19b16ea..1c73f4851b 100644
--- a/include/lmb.h
+++ b/include/lmb.h
@@ -14,9 +14,20 @@
 
 #define MAX_LMB_REGIONS 8
 
+/**
+ * enum lmb_flags - definition of memory region attributes
+ * @LMB_NONE: no special request
+ * @LMB_NOMAP: don't add to mmu configuration
+ */
+enum lmb_flags {
+       LMB_NONE                = 0x0,  /* No special request */
+       LMB_NOMAP               = 0x4,  /* don't add to mmu config */
+};
+
 struct lmb_property {
        phys_addr_t base;
        phys_size_t size;
+       enum lmb_flags flags;
 };
 
 struct lmb_region {
@@ -37,6 +48,8 @@ extern void lmb_init_and_reserve_range(struct lmb *lmb, 
phys_addr_t base,
                                       phys_size_t size, void *fdt_blob);
 extern long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size);
 extern long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size);
+extern long lmb_reserve_flags(struct lmb *lmb, phys_addr_t base,
+                             phys_size_t size, enum lmb_flags flags);
 extern phys_addr_t lmb_alloc(struct lmb *lmb, phys_size_t size, ulong align);
 extern phys_addr_t lmb_alloc_base(struct lmb *lmb, phys_size_t size, ulong 
align,
                            phys_addr_t max_addr);
@@ -60,6 +73,13 @@ lmb_size_bytes(struct lmb_region *type, unsigned long 
region_nr)
 void board_lmb_reserve(struct lmb *lmb);
 void arch_lmb_reserve(struct lmb *lmb);
 
+/* Low level functions */
+
+static inline bool lmb_is_nomap(struct lmb_property *m)
+{
+       return !!(m->flags & LMB_NOMAP);
+}
+
 #endif /* __KERNEL__ */
 
 #endif /* _LINUX_LMB_H */
diff --git a/lib/lmb.c b/lib/lmb.c
index 75082f3559..e85e08635c 100644
--- a/lib/lmb.c
+++ b/lib/lmb.c
@@ -27,6 +27,8 @@ void lmb_dump_all_force(struct lmb *lmb)
                       (unsigned long long)lmb->memory.region[i].base);
                printf("                   .size   = 0x%llx\n",
                       (unsigned long long)lmb->memory.region[i].size);
+               printf("                   .flags   = 0x%x\n",
+                      lmb->memory.region[i].flags);
        }
 
        printf("\n    reserved.cnt         = 0x%lx\n", lmb->reserved.cnt);
@@ -37,6 +39,8 @@ void lmb_dump_all_force(struct lmb *lmb)
                       (unsigned long long)lmb->reserved.region[i].base);
                printf("                     .size = 0x%llx\n",
                       (unsigned long long)lmb->reserved.region[i].size);
+               printf("                     .flags = 0x%x\n",
+                      lmb->reserved.region[i].flags);
        }
 }
 
@@ -85,6 +89,7 @@ static void lmb_remove_region(struct lmb_region *rgn, 
unsigned long r)
        for (i = r; i < rgn->cnt - 1; i++) {
                rgn->region[i].base = rgn->region[i + 1].base;
                rgn->region[i].size = rgn->region[i + 1].size;
+               rgn->region[i].flags = rgn->region[i + 1].flags;
        }
        rgn->cnt--;
 }
@@ -146,7 +151,8 @@ void lmb_init_and_reserve_range(struct lmb *lmb, 
phys_addr_t base,
 }
 
 /* This routine called with relocation disabled. */
-static long lmb_add_region(struct lmb_region *rgn, phys_addr_t base, 
phys_size_t size)
+static long lmb_add_region_flags(struct lmb_region *rgn, phys_addr_t base,
+                                phys_size_t size, enum lmb_flags flags)
 {
        unsigned long coalesced = 0;
        long adjacent, i;
@@ -154,6 +160,7 @@ static long lmb_add_region(struct lmb_region *rgn, 
phys_addr_t base, phys_size_t
        if (rgn->cnt == 0) {
                rgn->region[0].base = base;
                rgn->region[0].size = size;
+               rgn->region[0].flags = flags;
                rgn->cnt = 1;
                return 0;
        }
@@ -162,18 +169,27 @@ static long lmb_add_region(struct lmb_region *rgn, 
phys_addr_t base, phys_size_t
        for (i = 0; i < rgn->cnt; i++) {
                phys_addr_t rgnbase = rgn->region[i].base;
                phys_size_t rgnsize = rgn->region[i].size;
+               phys_size_t rgnflags = rgn->region[i].flags;
 
-               if ((rgnbase == base) && (rgnsize == size))
-                       /* Already have this region, so we're done */
-                       return 0;
+               if (rgnbase == base && rgnsize == size) {
+                       if (flags == rgnflags)
+                               /* Already have this region, so we're done */
+                               return 0;
+                       else
+                               return -1; /* regions with new flags */
+               }
 
                adjacent = lmb_addrs_adjacent(base, size, rgnbase, rgnsize);
                if (adjacent > 0) {
+                       if (flags != rgnflags)
+                               break;
                        rgn->region[i].base -= size;
                        rgn->region[i].size += size;
                        coalesced++;
                        break;
                } else if (adjacent < 0) {
+                       if (flags != rgnflags)
+                               break;
                        rgn->region[i].size += size;
                        coalesced++;
                        break;
@@ -184,8 +200,10 @@ static long lmb_add_region(struct lmb_region *rgn, 
phys_addr_t base, phys_size_t
        }
 
        if ((i < rgn->cnt - 1) && lmb_regions_adjacent(rgn, i, i + 1)) {
-               lmb_coalesce_regions(rgn, i, i + 1);
-               coalesced++;
+               if (rgn->region[i].flags == rgn->region[i + 1].flags) {
+                       lmb_coalesce_regions(rgn, i, i + 1);
+                       coalesced++;
+               }
        }
 
        if (coalesced)
@@ -198,9 +216,11 @@ static long lmb_add_region(struct lmb_region *rgn, 
phys_addr_t base, phys_size_t
                if (base < rgn->region[i].base) {
                        rgn->region[i + 1].base = rgn->region[i].base;
                        rgn->region[i + 1].size = rgn->region[i].size;
+                       rgn->region[i + 1].flags = rgn->region[i].flags;
                } else {
                        rgn->region[i + 1].base = base;
                        rgn->region[i + 1].size = size;
+                       rgn->region[i + 1].flags = flags;
                        break;
                }
        }
@@ -208,6 +228,7 @@ static long lmb_add_region(struct lmb_region *rgn, 
phys_addr_t base, phys_size_t
        if (base < rgn->region[0].base) {
                rgn->region[0].base = base;
                rgn->region[0].size = size;
+               rgn->region[0].flags = flags;
        }
 
        rgn->cnt++;
@@ -215,6 +236,12 @@ static long lmb_add_region(struct lmb_region *rgn, 
phys_addr_t base, phys_size_t
        return 0;
 }
 
+static long lmb_add_region(struct lmb_region *rgn, phys_addr_t base,
+                          phys_size_t size)
+{
+       return lmb_add_region_flags(rgn, base, size, LMB_NONE);
+}
+
 /* This routine may be called with relocation disabled. */
 long lmb_add(struct lmb *lmb, phys_addr_t base, phys_size_t size)
 {
@@ -269,14 +296,21 @@ long lmb_free(struct lmb *lmb, phys_addr_t base, 
phys_size_t size)
         * beginging of the hole and add the region after hole.
         */
        rgn->region[i].size = base - rgn->region[i].base;
-       return lmb_add_region(rgn, end + 1, rgnend - end);
+       return lmb_add_region_flags(rgn, end + 1, rgnend - end,
+                                   rgn->region[i].flags);
 }
 
-long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size)
+long lmb_reserve_flags(struct lmb *lmb, phys_addr_t base, phys_size_t size,
+                      enum lmb_flags flags)
 {
        struct lmb_region *_rgn = &(lmb->reserved);
 
-       return lmb_add_region(_rgn, base, size);
+       return lmb_add_region_flags(_rgn, base, size, flags);
+}
+
+long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size)
+{
+       return lmb_reserve_flags(lmb, base, size, LMB_NONE);
 }
 
 static long lmb_overlaps_region(struct lmb_region *rgn, phys_addr_t base,
-- 
2.17.1

Reply via email to