Upgrade to 4.4.50-rt62
[kvmfornfv.git] / kernel / drivers / of / of_reserved_mem.c
index 726ebe7..07dd815 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Device tree based initialization code for reserved memory.
  *
- * Copyright (c) 2013, The Linux Foundation. All Rights Reserved.
+ * Copyright (c) 2013, 2015 The Linux Foundation. All Rights Reserved.
  * Copyright (c) 2013,2014 Samsung Electronics Co., Ltd.
  *             http://www.samsung.com
  * Author: Marek Szyprowski <m.szyprowski@samsung.com>
@@ -20,6 +20,7 @@
 #include <linux/mm.h>
 #include <linux/sizes.h>
 #include <linux/of_reserved_mem.h>
+#include <linux/sort.h>
 
 #define MAX_RESERVED_REGIONS   16
 static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS];
@@ -31,11 +32,13 @@ int __init __weak early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
        phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
        phys_addr_t *res_base)
 {
+       phys_addr_t base;
        /*
         * We use __memblock_alloc_base() because memblock_alloc_base()
         * panic()s on allocation failure.
         */
-       phys_addr_t base = __memblock_alloc_base(size, align, end);
+       end = !end ? MEMBLOCK_ALLOC_ANYWHERE : end;
+       base = __memblock_alloc_base(size, align, end);
        if (!base)
                return -ENOMEM;
 
@@ -123,6 +126,14 @@ static int __init __reserved_mem_alloc_size(unsigned long node,
                align = dt_mem_next_cell(dt_root_addr_cells, &prop);
        }
 
+       /* Need adjust the alignment to satisfy the CMA requirement */
+       if (IS_ENABLED(CONFIG_CMA) && of_flat_dt_is_compatible(node, "shared-dma-pool")) {
+               unsigned long order =
+                       max_t(unsigned long, MAX_ORDER - 1, pageblock_order);
+
+               align = max(align, (phys_addr_t)PAGE_SIZE << order);
+       }
+
        prop = of_get_flat_dt_prop(node, "alloc-ranges", &len);
        if (prop) {
 
@@ -197,12 +208,57 @@ static int __init __reserved_mem_init_node(struct reserved_mem *rmem)
        return -ENOENT;
 }
 
+static int __init __rmem_cmp(const void *a, const void *b)
+{
+       const struct reserved_mem *ra = a, *rb = b;
+
+       if (ra->base < rb->base)
+               return -1;
+
+       if (ra->base > rb->base)
+               return 1;
+
+       return 0;
+}
+
+static void __init __rmem_check_for_overlap(void)
+{
+       int i;
+
+       if (reserved_mem_count < 2)
+               return;
+
+       sort(reserved_mem, reserved_mem_count, sizeof(reserved_mem[0]),
+            __rmem_cmp, NULL);
+       for (i = 0; i < reserved_mem_count - 1; i++) {
+               struct reserved_mem *this, *next;
+
+               this = &reserved_mem[i];
+               next = &reserved_mem[i + 1];
+               if (!(this->base && next->base))
+                       continue;
+               if (this->base + this->size > next->base) {
+                       phys_addr_t this_end, next_end;
+
+                       this_end = this->base + this->size;
+                       next_end = next->base + next->size;
+                       pr_err("Reserved memory: OVERLAP DETECTED!\n%s (%pa--%pa) overlaps with %s (%pa--%pa)\n",
+                              this->name, &this->base, &this_end,
+                              next->name, &next->base, &next_end);
+               }
+       }
+}
+
 /**
  * fdt_init_reserved_mem - allocate and init all saved reserved memory regions
  */
 void __init fdt_init_reserved_mem(void)
 {
        int i;
+
+       /* check for overlapping reserved regions */
+       __rmem_check_for_overlap();
+
        for (i = 0; i < reserved_mem_count; i++) {
                struct reserved_mem *rmem = &reserved_mem[i];
                unsigned long node = rmem->fdt_node;