Merge tag 'kvm-arm-for-4.8-take2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[cascardo/linux.git] / drivers / gpu / drm / drm_fb_helper.c
index 855108e..ce54e98 100644 (file)
@@ -84,6 +84,15 @@ static LIST_HEAD(kernel_fb_helper_list);
  * and set up an initial configuration using the detected hardware, drivers
  * should call drm_fb_helper_single_add_all_connectors() followed by
  * drm_fb_helper_initial_config().
+ *
+ * If &drm_framebuffer_funcs ->dirty is set, the
+ * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will
+ * accumulate changes and schedule &drm_fb_helper ->dirty_work to run right
+ * away. This worker then calls the dirty() function ensuring that it will
+ * always run in process context since the fb_*() function could be running in
+ * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io
+ * callback it will also schedule dirty_work with the damage collected from the
+ * mmap page writes.
  */
 
 /**
@@ -153,40 +162,13 @@ int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_
        if (!fb_helper_connector)
                return -ENOMEM;
 
+       drm_connector_reference(connector);
        fb_helper_connector->connector = connector;
        fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
        return 0;
 }
 EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
 
-static void remove_from_modeset(struct drm_mode_set *set,
-               struct drm_connector *connector)
-{
-       int i, j;
-
-       for (i = 0; i < set->num_connectors; i++) {
-               if (set->connectors[i] == connector)
-                       break;
-       }
-
-       if (i == set->num_connectors)
-               return;
-
-       for (j = i + 1; j < set->num_connectors; j++) {
-               set->connectors[j - 1] = set->connectors[j];
-       }
-       set->num_connectors--;
-
-       /*
-        * TODO maybe need to makes sure we set it back to !=NULL somewhere?
-        */
-       if (set->num_connectors == 0) {
-               set->fb = NULL;
-               drm_mode_destroy(connector->dev, set->mode);
-               set->mode = NULL;
-       }
-}
-
 int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
                                       struct drm_connector *connector)
 {
@@ -206,6 +188,7 @@ int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
        if (i == fb_helper->connector_count)
                return -EINVAL;
        fb_helper_connector = fb_helper->connector_info[i];
+       drm_connector_unreference(fb_helper_connector->connector);
 
        for (j = i + 1; j < fb_helper->connector_count; j++) {
                fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
@@ -213,10 +196,6 @@ int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
        fb_helper->connector_count--;
        kfree(fb_helper_connector);
 
-       /* also cleanup dangling references to the connector: */
-       for (i = 0; i < fb_helper->crtc_count; i++)
-               remove_from_modeset(&fb_helper->crtc_info[i].mode_set, connector);
-
        return 0;
 }
 EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
@@ -248,7 +227,7 @@ static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
        g_base = r_base + crtc->gamma_size;
        b_base = g_base + crtc->gamma_size;
 
-       crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
+       crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
 }
 
 /**
@@ -406,7 +385,7 @@ static int restore_fbdev_mode(struct drm_fb_helper *fb_helper)
 
        drm_warn_on_modeset_not_all_locked(dev);
 
-       if (fb_helper->atomic)
+       if (dev->mode_config.funcs->atomic_commit)
                return restore_fbdev_mode_atomic(fb_helper);
 
        drm_for_each_plane(plane, dev) {
@@ -485,7 +464,7 @@ static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
 
        /* Sometimes user space wants everything disabled, so don't steal the
         * display if there's a master. */
-       if (dev->primary->master)
+       if (lockless_dereference(dev->master))
                return false;
 
        drm_for_each_crtc(crtc, dev) {
@@ -626,8 +605,10 @@ static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
 {
        int i;
 
-       for (i = 0; i < helper->connector_count; i++)
+       for (i = 0; i < helper->connector_count; i++) {
+               drm_connector_unreference(helper->connector_info[i]->connector);
                kfree(helper->connector_info[i]);
+       }
        kfree(helper->connector_info);
        for (i = 0; i < helper->crtc_count; i++) {
                kfree(helper->crtc_info[i].mode_set.connectors);
@@ -637,6 +618,23 @@ static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
        kfree(helper->crtc_info);
 }
 
+static void drm_fb_helper_dirty_work(struct work_struct *work)
+{
+       struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
+                                                   dirty_work);
+       struct drm_clip_rect *clip = &helper->dirty_clip;
+       struct drm_clip_rect clip_copy;
+       unsigned long flags;
+
+       spin_lock_irqsave(&helper->dirty_lock, flags);
+       clip_copy = *clip;
+       clip->x1 = clip->y1 = ~0;
+       clip->x2 = clip->y2 = 0;
+       spin_unlock_irqrestore(&helper->dirty_lock, flags);
+
+       helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
+}
+
 /**
  * drm_fb_helper_prepare - setup a drm_fb_helper structure
  * @dev: DRM device
@@ -650,6 +648,9 @@ void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
                           const struct drm_fb_helper_funcs *funcs)
 {
        INIT_LIST_HEAD(&helper->kernel_fb_list);
+       spin_lock_init(&helper->dirty_lock);
+       INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work);
+       helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0;
        helper->funcs = funcs;
        helper->dev = dev;
 }
@@ -715,8 +716,6 @@ int drm_fb_helper_init(struct drm_device *dev,
                i++;
        }
 
-       fb_helper->atomic = !!drm_core_check_feature(dev, DRIVER_ATOMIC);
-
        return 0;
 out_free:
        drm_fb_helper_crtc_free(fb_helper);
@@ -834,6 +833,59 @@ void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
 }
 EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
 
+static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
+                               u32 width, u32 height)
+{
+       struct drm_fb_helper *helper = info->par;
+       struct drm_clip_rect *clip = &helper->dirty_clip;
+       unsigned long flags;
+
+       if (!helper->fb->funcs->dirty)
+               return;
+
+       spin_lock_irqsave(&helper->dirty_lock, flags);
+       clip->x1 = min_t(u32, clip->x1, x);
+       clip->y1 = min_t(u32, clip->y1, y);
+       clip->x2 = max_t(u32, clip->x2, x + width);
+       clip->y2 = max_t(u32, clip->y2, y + height);
+       spin_unlock_irqrestore(&helper->dirty_lock, flags);
+
+       schedule_work(&helper->dirty_work);
+}
+
+/**
+ * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
+ * @info: fb_info struct pointer
+ * @pagelist: list of dirty mmap framebuffer pages
+ *
+ * This function is used as the &fb_deferred_io ->deferred_io
+ * callback function for flushing the fbdev mmap writes.
+ */
+void drm_fb_helper_deferred_io(struct fb_info *info,
+                              struct list_head *pagelist)
+{
+       unsigned long start, end, min, max;
+       struct page *page;
+       u32 y1, y2;
+
+       min = ULONG_MAX;
+       max = 0;
+       list_for_each_entry(page, pagelist, lru) {
+               start = page->index << PAGE_SHIFT;
+               end = start + PAGE_SIZE - 1;
+               min = min(min, start);
+               max = max(max, end);
+       }
+
+       if (min < max) {
+               y1 = min / info->fix.line_length;
+               y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
+                          info->var.yres);
+               drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
+       }
+}
+EXPORT_SYMBOL(drm_fb_helper_deferred_io);
+
 /**
  * drm_fb_helper_sys_read - wrapper around fb_sys_read
  * @info: fb_info struct pointer
@@ -862,7 +914,14 @@ EXPORT_SYMBOL(drm_fb_helper_sys_read);
 ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
                                size_t count, loff_t *ppos)
 {
-       return fb_sys_write(info, buf, count, ppos);
+       ssize_t ret;
+
+       ret = fb_sys_write(info, buf, count, ppos);
+       if (ret > 0)
+               drm_fb_helper_dirty(info, 0, 0, info->var.xres,
+                                   info->var.yres);
+
+       return ret;
 }
 EXPORT_SYMBOL(drm_fb_helper_sys_write);
 
@@ -877,6 +936,8 @@ void drm_fb_helper_sys_fillrect(struct fb_info *info,
                                const struct fb_fillrect *rect)
 {
        sys_fillrect(info, rect);
+       drm_fb_helper_dirty(info, rect->dx, rect->dy,
+                           rect->width, rect->height);
 }
 EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
 
@@ -891,6 +952,8 @@ void drm_fb_helper_sys_copyarea(struct fb_info *info,
                                const struct fb_copyarea *area)
 {
        sys_copyarea(info, area);
+       drm_fb_helper_dirty(info, area->dx, area->dy,
+                           area->width, area->height);
 }
 EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
 
@@ -905,6 +968,8 @@ void drm_fb_helper_sys_imageblit(struct fb_info *info,
                                 const struct fb_image *image)
 {
        sys_imageblit(info, image);
+       drm_fb_helper_dirty(info, image->dx, image->dy,
+                           image->width, image->height);
 }
 EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
 
@@ -919,6 +984,8 @@ void drm_fb_helper_cfb_fillrect(struct fb_info *info,
                                const struct fb_fillrect *rect)
 {
        cfb_fillrect(info, rect);
+       drm_fb_helper_dirty(info, rect->dx, rect->dy,
+                           rect->width, rect->height);
 }
 EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
 
@@ -933,6 +1000,8 @@ void drm_fb_helper_cfb_copyarea(struct fb_info *info,
                                const struct fb_copyarea *area)
 {
        cfb_copyarea(info, area);
+       drm_fb_helper_dirty(info, area->dx, area->dy,
+                           area->width, area->height);
 }
 EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
 
@@ -947,6 +1016,8 @@ void drm_fb_helper_cfb_imageblit(struct fb_info *info,
                                 const struct fb_image *image)
 {
        cfb_imageblit(info, image);
+       drm_fb_helper_dirty(info, image->dx, image->dy,
+                           image->width, image->height);
 }
 EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
 
@@ -969,7 +1040,6 @@ static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
 {
        struct drm_fb_helper *fb_helper = info->par;
        struct drm_framebuffer *fb = fb_helper->fb;
-       int pindex;
 
        if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
                u32 *palette;
@@ -1001,38 +1071,10 @@ static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
                    !fb_helper->funcs->gamma_get))
                return -EINVAL;
 
-       pindex = regno;
+       WARN_ON(fb->bits_per_pixel != 8);
 
-       if (fb->bits_per_pixel == 16) {
-               pindex = regno << 3;
+       fb_helper->funcs->gamma_set(crtc, red, green, blue, regno);
 
-               if (fb->depth == 16 && regno > 63)
-                       return -EINVAL;
-               if (fb->depth == 15 && regno > 31)
-                       return -EINVAL;
-
-               if (fb->depth == 16) {
-                       u16 r, g, b;
-                       int i;
-                       if (regno < 32) {
-                               for (i = 0; i < 8; i++)
-                                       fb_helper->funcs->gamma_set(crtc, red,
-                                               green, blue, pindex + i);
-                       }
-
-                       fb_helper->funcs->gamma_get(crtc, &r,
-                                                   &g, &b,
-                                                   pindex >> 1);
-
-                       for (i = 0; i < 4; i++)
-                               fb_helper->funcs->gamma_set(crtc, r,
-                                                           green, b,
-                                                           (pindex >> 1) + i);
-               }
-       }
-
-       if (fb->depth != 16)
-               fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
        return 0;
 }
 
@@ -1300,7 +1342,7 @@ int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
                return -EBUSY;
        }
 
-       if (fb_helper->atomic) {
+       if (dev->mode_config.funcs->atomic_commit) {
                ret = pan_display_atomic(var, info);
                goto unlock;
        }
@@ -1895,7 +1937,6 @@ static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
                          int n, int width, int height)
 {
        int c, o;
-       struct drm_device *dev = fb_helper->dev;
        struct drm_connector *connector;
        const struct drm_connector_helper_funcs *connector_funcs;
        struct drm_encoder *encoder;
@@ -1914,7 +1955,7 @@ static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
        if (modes[n] == NULL)
                return best_score;
 
-       crtcs = kzalloc(dev->mode_config.num_connector *
+       crtcs = kzalloc(fb_helper->connector_count *
                        sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
        if (!crtcs)
                return best_score;
@@ -1928,7 +1969,18 @@ static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
                my_score++;
 
        connector_funcs = connector->helper_private;
-       encoder = connector_funcs->best_encoder(connector);
+
+       /*
+        * If the DRM device implements atomic hooks and ->best_encoder() is
+        * NULL we fallback to the default drm_atomic_helper_best_encoder()
+        * helper.
+        */
+       if (fb_helper->dev->mode_config.funcs->atomic_commit &&
+           !connector_funcs->best_encoder)
+               encoder = drm_atomic_helper_best_encoder(connector);
+       else
+               encoder = connector_funcs->best_encoder(connector);
+
        if (!encoder)
                goto out;
 
@@ -1960,7 +2012,7 @@ static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
                if (score > best_score) {
                        best_score = score;
                        memcpy(best_crtcs, crtcs,
-                              dev->mode_config.num_connector *
+                              fb_helper->connector_count *
                               sizeof(struct drm_fb_helper_crtc *));
                }
        }
@@ -2104,8 +2156,8 @@ out:
  * cmdline option.
  *
  * The other option is to just disable fbdev emulation since very likely the
- * first modest from userspace will crash in the same way, and is even easier to
- * debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
+ * first modeset from userspace will crash in the same way, and is even easier
+ * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
  * kernel cmdline option.
  *
  * RETURNS:
@@ -2150,7 +2202,7 @@ EXPORT_SYMBOL(drm_fb_helper_initial_config);
  * hotplug interrupt).
  *
  * Note that drivers may call this even before calling
- * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows
+ * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
  * for a race-free fbcon setup and will make sure that the fbdev emulation will
  * not miss any hotplug events.
  *