ARM: sunxi_defconfig: enable CONFIG_REGULATOR
[cascardo/linux.git] / mm / cma.c
index c17751c..963bc4a 100644 (file)
--- a/mm/cma.c
+++ b/mm/cma.c
@@ -32,6 +32,7 @@
 #include <linux/slab.h>
 #include <linux/log2.h>
 #include <linux/cma.h>
+#include <linux/highmem.h>
 
 struct cma {
        unsigned long   base_pfn;
@@ -57,7 +58,9 @@ unsigned long cma_get_size(struct cma *cma)
 
 static unsigned long cma_bitmap_aligned_mask(struct cma *cma, int align_order)
 {
-       return (1UL << (align_order >> cma->order_per_bit)) - 1;
+       if (align_order <= cma->order_per_bit)
+               return 0;
+       return (1UL << (align_order - cma->order_per_bit)) - 1;
 }
 
 static unsigned long cma_bitmap_maxno(struct cma *cma)
@@ -139,6 +142,54 @@ static int __init cma_init_reserved_areas(void)
 }
 core_initcall(cma_init_reserved_areas);
 
+/**
+ * cma_init_reserved_mem() - create custom contiguous area from reserved memory
+ * @base: Base address of the reserved area
+ * @size: Size of the reserved area (in bytes),
+ * @order_per_bit: Order of pages represented by one bit on bitmap.
+ * @res_cma: Pointer to store the created cma region.
+ *
+ * This function creates custom contiguous area from already reserved memory.
+ */
+int __init cma_init_reserved_mem(phys_addr_t base, phys_addr_t size,
+                                int order_per_bit, struct cma **res_cma)
+{
+       struct cma *cma;
+       phys_addr_t alignment;
+
+       /* Sanity checks */
+       if (cma_area_count == ARRAY_SIZE(cma_areas)) {
+               pr_err("Not enough slots for CMA reserved regions!\n");
+               return -ENOSPC;
+       }
+
+       if (!size || !memblock_is_region_reserved(base, size))
+               return -EINVAL;
+
+       /* ensure minimal alignment requied by mm core */
+       alignment = PAGE_SIZE << max(MAX_ORDER - 1, pageblock_order);
+
+       /* alignment should be aligned with order_per_bit */
+       if (!IS_ALIGNED(alignment >> PAGE_SHIFT, 1 << order_per_bit))
+               return -EINVAL;
+
+       if (ALIGN(base, alignment) != base || ALIGN(size, alignment) != size)
+               return -EINVAL;
+
+       /*
+        * Each reserved area must be initialised later, when more kernel
+        * subsystems (like slab allocator) are available.
+        */
+       cma = &cma_areas[cma_area_count];
+       cma->base_pfn = PFN_DOWN(base);
+       cma->count = size >> PAGE_SHIFT;
+       cma->order_per_bit = order_per_bit;
+       *res_cma = cma;
+       cma_area_count++;
+
+       return 0;
+}
+
 /**
  * cma_declare_contiguous() - reserve custom contiguous area
  * @base: Base address of the reserved area optional, use 0 for any
@@ -162,7 +213,8 @@ int __init cma_declare_contiguous(phys_addr_t base,
                        phys_addr_t alignment, unsigned int order_per_bit,
                        bool fixed, struct cma **res_cma)
 {
-       struct cma *cma;
+       phys_addr_t memblock_end = memblock_end_of_DRAM();
+       phys_addr_t highmem_start = __pa(high_memory);
        int ret = 0;
 
        pr_debug("%s(size %lx, base %08lx, limit %08lx alignment %08lx)\n",
@@ -196,6 +248,24 @@ int __init cma_declare_contiguous(phys_addr_t base,
        if (!IS_ALIGNED(size >> PAGE_SHIFT, 1 << order_per_bit))
                return -EINVAL;
 
+       /*
+        * adjust limit to avoid crossing low/high memory boundary for
+        * automatically allocated regions
+        */
+       if (((limit == 0 || limit > memblock_end) &&
+            (memblock_end - size < highmem_start &&
+             memblock_end > highmem_start)) ||
+           (!fixed && limit > highmem_start && limit - size < highmem_start)) {
+               limit = highmem_start;
+       }
+
+       if (fixed && base < highmem_start && base+size > highmem_start) {
+               ret = -EINVAL;
+               pr_err("Region at %08lx defined on low/high memory boundary (%08lx)\n",
+                       (unsigned long)base, (unsigned long)highmem_start);
+               goto err;
+       }
+
        /* Reserve memory */
        if (base && fixed) {
                if (memblock_is_region_reserved(base, size) ||
@@ -214,16 +284,9 @@ int __init cma_declare_contiguous(phys_addr_t base,
                }
        }
 
-       /*
-        * Each reserved area must be initialised later, when more kernel
-        * subsystems (like slab allocator) are available.
-        */
-       cma = &cma_areas[cma_area_count];
-       cma->base_pfn = PFN_DOWN(base);
-       cma->count = size >> PAGE_SHIFT;
-       cma->order_per_bit = order_per_bit;
-       *res_cma = cma;
-       cma_area_count++;
+       ret = cma_init_reserved_mem(base, size, order_per_bit, res_cma);
+       if (ret)
+               goto err;
 
        pr_info("Reserved %ld MiB at %08lx\n", (unsigned long)size / SZ_1M,
                (unsigned long)base);