MALI: rockchip: upgrade midgard DDK to r9p0-05rel0
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / midgard / mali_kbase_core_linux.c
index 426947fd76a202d37e1f880077fbd2adca77b6d5..ab6f5e45eaccec113c021f5e04f78afc629c0da3 100644 (file)
@@ -66,7 +66,9 @@
 #include <linux/io.h>
 #include <linux/mm.h>
 #include <linux/compat.h>      /* is_compat_task */
+#include <linux/mman.h>
 #include <linux/version.h>
+#include <linux/security.h>
 #ifdef CONFIG_MALI_PLATFORM_DEVICETREE
 #include <linux/pm_runtime.h>
 #endif /* CONFIG_MALI_PLATFORM_DEVICETREE */
@@ -1483,99 +1485,154 @@ static int kbase_check_flags(int flags)
        return 0;
 }
 
-static unsigned long kbase_get_unmapped_area(struct file *filp,
-               const unsigned long addr, const unsigned long len,
-               const unsigned long pgoff, const unsigned long flags)
-{
 #ifdef CONFIG_64BIT
-       /* based on get_unmapped_area, but simplified slightly due to that some
-        * values are known in advance */
-       struct kbase_context *kctx = filp->private_data;
+/* The following function is taken from the kernel and just
+ * renamed. As it's not exported to modules we must copy-paste it here.
+ */
 
-       if (!kctx->is_compat && !addr &&
-               kbase_hw_has_feature(kctx->kbdev, BASE_HW_FEATURE_33BIT_VA)) {
-               struct mm_struct *mm = current->mm;
-               struct vm_area_struct *vma;
-               unsigned long low_limit, high_limit, gap_start, gap_end;
+static unsigned long kbase_unmapped_area_topdown(struct vm_unmapped_area_info
+               *info)
+{
+       struct mm_struct *mm = current->mm;
+       struct vm_area_struct *vma;
+       unsigned long length, low_limit, high_limit, gap_start, gap_end;
 
-               /* Hardware has smaller VA than userspace, ensure the page
-                * comes from a VA which can be used on the GPU */
+       /* Adjust search length to account for worst case alignment overhead */
+       length = info->length + info->align_mask;
+       if (length < info->length)
+               return -ENOMEM;
 
-               gap_end = (1UL<<33);
-               if (gap_end < len)
-                       return -ENOMEM;
-               high_limit = gap_end - len;
-               low_limit = PAGE_SIZE + len;
+       /*
+        * Adjust search limits by the desired length.
+        * See implementation comment at top of unmapped_area().
+        */
+       gap_end = info->high_limit;
+       if (gap_end < length)
+               return -ENOMEM;
+       high_limit = gap_end - length;
 
-               gap_start = mm->highest_vm_end;
-               if (gap_start <= high_limit)
-                       goto found_highest;
+       if (info->low_limit > high_limit)
+               return -ENOMEM;
+       low_limit = info->low_limit + length;
 
-               if (RB_EMPTY_ROOT(&mm->mm_rb))
-                       return -ENOMEM;
-               vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
-               if (vma->rb_subtree_gap < len)
-                       return -ENOMEM;
+       /* Check highest gap, which does not precede any rbtree node */
+       gap_start = mm->highest_vm_end;
+       if (gap_start <= high_limit)
+               goto found_highest;
 
-               while (true) {
-                       gap_start = vma->vm_prev ? vma->vm_prev->vm_end : 0;
-                       if (gap_start <= high_limit && vma->vm_rb.rb_right) {
-                               struct vm_area_struct *right =
-                                       rb_entry(vma->vm_rb.rb_right,
-                                                struct vm_area_struct, vm_rb);
-                               if (right->rb_subtree_gap >= len) {
-                                       vma = right;
-                                       continue;
-                               }
+       /* Check if rbtree root looks promising */
+       if (RB_EMPTY_ROOT(&mm->mm_rb))
+               return -ENOMEM;
+       vma = rb_entry(mm->mm_rb.rb_node, struct vm_area_struct, vm_rb);
+       if (vma->rb_subtree_gap < length)
+               return -ENOMEM;
+
+       while (true) {
+               /* Visit right subtree if it looks promising */
+               gap_start = vma->vm_prev ? vma->vm_prev->vm_end : 0;
+               if (gap_start <= high_limit && vma->vm_rb.rb_right) {
+                       struct vm_area_struct *right =
+                               rb_entry(vma->vm_rb.rb_right,
+                                        struct vm_area_struct, vm_rb);
+                       if (right->rb_subtree_gap >= length) {
+                               vma = right;
+                               continue;
                        }
+               }
+
 check_current:
-                       gap_end = vma->vm_start;
-                       if (gap_end < low_limit)
-                               return -ENOMEM;
-                       if (gap_start <= high_limit &&
-                           gap_end - gap_start >= len)
-                               goto found;
-
-                       if (vma->vm_rb.rb_left) {
-                               struct vm_area_struct *left =
-                                       rb_entry(vma->vm_rb.rb_left,
-                                                struct vm_area_struct, vm_rb);
-
-                               if (left->rb_subtree_gap >= len) {
-                                       vma = left;
-                                       continue;
-                               }
+               /* Check if current node has a suitable gap */
+               gap_end = vma->vm_start;
+               if (gap_end < low_limit)
+                       return -ENOMEM;
+               if (gap_start <= high_limit && gap_end - gap_start >= length)
+                       goto found;
+
+               /* Visit left subtree if it looks promising */
+               if (vma->vm_rb.rb_left) {
+                       struct vm_area_struct *left =
+                               rb_entry(vma->vm_rb.rb_left,
+                                        struct vm_area_struct, vm_rb);
+                       if (left->rb_subtree_gap >= length) {
+                               vma = left;
+                               continue;
                        }
-                       while (true) {
-                               struct rb_node *prev = &vma->vm_rb;
-
-                               if (!rb_parent(prev))
-                                       return -ENOMEM;
-                               vma = rb_entry(rb_parent(prev),
-                                               struct vm_area_struct, vm_rb);
-                               if (prev == vma->vm_rb.rb_right) {
-                                       gap_start = vma->vm_prev ?
-                                               vma->vm_prev->vm_end : 0;
-                                       goto check_current;
-                               }
+               }
+
+               /* Go back up the rbtree to find next candidate node */
+               while (true) {
+                       struct rb_node *prev = &vma->vm_rb;
+                       if (!rb_parent(prev))
+                               return -ENOMEM;
+                       vma = rb_entry(rb_parent(prev),
+                                      struct vm_area_struct, vm_rb);
+                       if (prev == vma->vm_rb.rb_right) {
+                               gap_start = vma->vm_prev ?
+                                       vma->vm_prev->vm_end : 0;
+                               goto check_current;
                        }
                }
+       }
 
 found:
-               if (gap_end > (1UL<<33))
-                       gap_end = (1UL<<33);
+       /* We found a suitable gap. Clip it with the original high_limit. */
+       if (gap_end > info->high_limit)
+               gap_end = info->high_limit;
 
 found_highest:
-               gap_end -= len;
+       /* Compute highest gap address at the desired alignment */
+       gap_end -= info->length;
+       gap_end -= (gap_end - info->align_offset) & info->align_mask;
+
+       VM_BUG_ON(gap_end < info->low_limit);
+       VM_BUG_ON(gap_end < gap_start);
+       return gap_end;
+}
+
+
+static unsigned long kbase_get_unmapped_area(struct file *filp,
+               const unsigned long addr, const unsigned long len,
+               const unsigned long pgoff, const unsigned long flags)
+{
+       /* based on get_unmapped_area, but simplified slightly due to that some
+        * values are known in advance */
+       struct kbase_context *kctx = filp->private_data;
+       struct mm_struct *mm = current->mm;
+       struct vm_unmapped_area_info info;
 
-               VM_BUG_ON(gap_end < PAGE_SIZE);
-               VM_BUG_ON(gap_end < gap_start);
-               return gap_end;
+       /* err on fixed address */
+       if ((flags & MAP_FIXED) || addr)
+               return -EINVAL;
+
+       /* too big? */
+       if (len > TASK_SIZE - SZ_2M)
+               return -ENOMEM;
+
+       if (kctx->is_compat)
+               return current->mm->get_unmapped_area(filp, addr, len, pgoff,
+                               flags);
+
+       if (kbase_hw_has_feature(kctx->kbdev, BASE_HW_FEATURE_33BIT_VA)) {
+               info.high_limit = 1ul << 33;
+               info.align_mask = 0;
+               info.align_offset = 0;
+       } else {
+               info.high_limit = mm->mmap_base;
+               if (len >= SZ_2M) {
+                       info.align_offset = SZ_2M;
+                       info.align_mask = SZ_2M - 1;
+               } else {
+                       info.align_mask = 0;
+                       info.align_offset = 0;
+               }
        }
-#endif
-       /* No special requirements - fallback to the default version */
-       return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags);
+
+       info.flags = 0;
+       info.length = len;
+       info.low_limit = SZ_2M;
+       return kbase_unmapped_area_topdown(&info);
 }
+#endif
 
 static const struct file_operations kbase_fops = {
        .owner = THIS_MODULE,
@@ -1587,7 +1644,9 @@ static const struct file_operations kbase_fops = {
        .compat_ioctl = kbase_ioctl,
        .mmap = kbase_mmap,
        .check_flags = kbase_check_flags,
+#ifdef CONFIG_64BIT
        .get_unmapped_area = kbase_get_unmapped_area,
+#endif
 };
 
 #ifndef CONFIG_MALI_NO_MALI
@@ -3876,7 +3935,7 @@ static int kbase_device_runtime_suspend(struct device *dev)
  */
 
 #ifdef KBASE_PM_RUNTIME
-int kbase_device_runtime_resume(struct device *dev)
+static int kbase_device_runtime_resume(struct device *dev)
 {
        int ret = 0;
        struct kbase_device *kbdev = to_kbase_device(dev);