Merge tag 'drm-misc-next-2019-04-18' of git://anongit.freedesktop.org/drm/drm-misc...
[sfrench/cifs-2.6.git] / drivers / gpu / drm / drm_fb_helper.c
index 04d23cb430bf0bb8dffa03ea5fb0916f1a37c27e..4de4b9d59d49ccbdb5bfa3cc7921b9e86c71864c 100644 (file)
@@ -639,20 +639,19 @@ static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
 static void dpms_legacy(struct drm_fb_helper *fb_helper, int dpms_mode)
 {
        struct drm_device *dev = fb_helper->dev;
-       struct drm_crtc *crtc;
        struct drm_connector *connector;
+       struct drm_mode_set *modeset;
        int i, j;
 
        drm_modeset_lock_all(dev);
        for (i = 0; i < fb_helper->crtc_count; i++) {
-               crtc = fb_helper->crtc_info[i].mode_set.crtc;
+               modeset = &fb_helper->crtc_info[i].mode_set;
 
-               if (!crtc->enabled)
+               if (!modeset->crtc->enabled)
                        continue;
 
-               /* Walk the connectors & encoders on this fb turning them on/off */
-               drm_fb_helper_for_each_connector(fb_helper, j) {
-                       connector = fb_helper->connector_info[j]->connector;
+               for (j = 0; j < modeset->num_connectors; j++) {
+                       connector = modeset->connectors[j];
                        connector->funcs->dpms(connector, dpms_mode);
                        drm_object_property_set_value(&connector->base,
                                dev->mode_config.dpms_property, dpms_mode);
@@ -934,6 +933,7 @@ struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
        }
 
        fb_helper->fbdev = info;
+       info->skip_vt_switch = true;
 
        return info;
 
@@ -1873,7 +1873,6 @@ static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
        int crtc_count = 0;
        int i;
        struct drm_fb_helper_surface_size sizes;
-       int gamma_size = 0;
        int best_depth = 0;
 
        memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
@@ -1889,7 +1888,6 @@ static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
        if (preferred_bpp != sizes.surface_bpp)
                sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
 
-       /* first up get a count of crtcs now in use and new min/maxes width/heights */
        drm_fb_helper_for_each_connector(fb_helper, i) {
                struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
                struct drm_cmdline_mode *cmdline_mode;
@@ -1963,12 +1961,13 @@ static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
                                best_depth = fmt->depth;
                }
        }
-       if (sizes.surface_depth != best_depth) {
+       if (sizes.surface_depth != best_depth && best_depth) {
                DRM_INFO("requested bpp %d, scaled depth down to %d",
                         sizes.surface_bpp, best_depth);
                sizes.surface_depth = best_depth;
        }
 
+       /* first up get a count of crtcs now in use and new min/maxes width/heights */
        crtc_count = 0;
        for (i = 0; i < fb_helper->crtc_count; i++) {
                struct drm_display_mode *desired_mode;
@@ -1991,9 +1990,6 @@ static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
                x = fb_helper->crtc_info[i].x;
                y = fb_helper->crtc_info[i].y;
 
-               if (gamma_size == 0)
-                       gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
-
                sizes.surface_width  = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
                sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
 
@@ -2036,21 +2032,8 @@ static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
        return 0;
 }
 
-/**
- * drm_fb_helper_fill_fix - initializes fixed fbdev information
- * @info: fbdev registered by the helper
- * @pitch: desired pitch
- * @depth: desired depth
- *
- * Helper to fill in the fixed fbdev information useful for a non-accelerated
- * fbdev emulations. Drivers which support acceleration methods which impose
- * additional constraints need to set up their own limits.
- *
- * Drivers should call this (or their equivalent setup code) from their
- * &drm_fb_helper_funcs.fb_probe callback.
- */
-void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
-                           uint32_t depth)
+static void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
+                                  uint32_t depth)
 {
        info->fix.type = FB_TYPE_PACKED_PIXELS;
        info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
@@ -2065,24 +2048,10 @@ void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
 
        info->fix.line_length = pitch;
 }
-EXPORT_SYMBOL(drm_fb_helper_fill_fix);
 
-/**
- * drm_fb_helper_fill_var - initalizes variable fbdev information
- * @info: fbdev instance to set up
- * @fb_helper: fb helper instance to use as template
- * @fb_width: desired fb width
- * @fb_height: desired fb height
- *
- * Sets up the variable fbdev metainformation from the given fb helper instance
- * and the drm framebuffer allocated in &drm_fb_helper.fb.
- *
- * Drivers should call this (or their equivalent setup code) from their
- * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
- * backing storage framebuffer.
- */
-void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
-                           uint32_t fb_width, uint32_t fb_height)
+static void drm_fb_helper_fill_var(struct fb_info *info,
+                                  struct drm_fb_helper *fb_helper,
+                                  uint32_t fb_width, uint32_t fb_height)
 {
        struct drm_framebuffer *fb = fb_helper->fb;
 
@@ -2102,7 +2071,36 @@ void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helpe
        info->var.xres = fb_width;
        info->var.yres = fb_height;
 }
-EXPORT_SYMBOL(drm_fb_helper_fill_var);
+
+/**
+ * drm_fb_helper_fill_info - initializes fbdev information
+ * @info: fbdev instance to set up
+ * @fb_helper: fb helper instance to use as template
+ * @sizes: describes fbdev size and scanout surface size
+ *
+ * Sets up the variable and fixed fbdev metainformation from the given fb helper
+ * instance and the drm framebuffer allocated in &drm_fb_helper.fb.
+ *
+ * Drivers should call this (or their equivalent setup code) from their
+ * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
+ * backing storage framebuffer.
+ */
+void drm_fb_helper_fill_info(struct fb_info *info,
+                            struct drm_fb_helper *fb_helper,
+                            struct drm_fb_helper_surface_size *sizes)
+{
+       struct drm_framebuffer *fb = fb_helper->fb;
+
+       drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
+       drm_fb_helper_fill_var(info, fb_helper,
+                              sizes->fb_width, sizes->fb_height);
+
+       info->par = fb_helper;
+       snprintf(info->fix.id, sizeof(info->fix.id), "%sdrmfb",
+                fb_helper->dev->driver->name);
+
+}
+EXPORT_SYMBOL(drm_fb_helper_fill_info);
 
 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
                                                uint32_t maxX,
@@ -2561,6 +2559,194 @@ static void drm_setup_crtc_rotation(struct drm_fb_helper *fb_helper,
        fb_helper->sw_rotations |= DRM_MODE_ROTATE_0;
 }
 
+static struct drm_fb_helper_crtc *
+drm_fb_helper_crtc(struct drm_fb_helper *fb_helper, struct drm_crtc *crtc)
+{
+       int i;
+
+       for (i = 0; i < fb_helper->crtc_count; i++)
+               if (fb_helper->crtc_info[i].mode_set.crtc == crtc)
+                       return &fb_helper->crtc_info[i];
+
+       return NULL;
+}
+
+/* Try to read the BIOS display configuration and use it for the initial config */
+static bool drm_fb_helper_firmware_config(struct drm_fb_helper *fb_helper,
+                                         struct drm_fb_helper_crtc **crtcs,
+                                         struct drm_display_mode **modes,
+                                         struct drm_fb_offset *offsets,
+                                         bool *enabled, int width, int height)
+{
+       struct drm_device *dev = fb_helper->dev;
+       unsigned int count = min(fb_helper->connector_count, BITS_PER_LONG);
+       unsigned long conn_configured, conn_seq;
+       int i, j;
+       bool *save_enabled;
+       bool fallback = true, ret = true;
+       int num_connectors_enabled = 0;
+       int num_connectors_detected = 0;
+       struct drm_modeset_acquire_ctx ctx;
+
+       save_enabled = kcalloc(count, sizeof(bool), GFP_KERNEL);
+       if (!save_enabled)
+               return false;
+
+       drm_modeset_acquire_init(&ctx, 0);
+
+       while (drm_modeset_lock_all_ctx(dev, &ctx) != 0)
+               drm_modeset_backoff(&ctx);
+
+       memcpy(save_enabled, enabled, count);
+       conn_seq = GENMASK(count - 1, 0);
+       conn_configured = 0;
+retry:
+       for (i = 0; i < count; i++) {
+               struct drm_fb_helper_connector *fb_conn;
+               struct drm_connector *connector;
+               struct drm_encoder *encoder;
+               struct drm_fb_helper_crtc *new_crtc;
+
+               fb_conn = fb_helper->connector_info[i];
+               connector = fb_conn->connector;
+
+               if (conn_configured & BIT(i))
+                       continue;
+
+               /* First pass, only consider tiled connectors */
+               if (conn_seq == GENMASK(count - 1, 0) && !connector->has_tile)
+                       continue;
+
+               if (connector->status == connector_status_connected)
+                       num_connectors_detected++;
+
+               if (!enabled[i]) {
+                       DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
+                                     connector->name);
+                       conn_configured |= BIT(i);
+                       continue;
+               }
+
+               if (connector->force == DRM_FORCE_OFF) {
+                       DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
+                                     connector->name);
+                       enabled[i] = false;
+                       continue;
+               }
+
+               encoder = connector->state->best_encoder;
+               if (!encoder || WARN_ON(!connector->state->crtc)) {
+                       if (connector->force > DRM_FORCE_OFF)
+                               goto bail;
+
+                       DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
+                                     connector->name);
+                       enabled[i] = false;
+                       conn_configured |= BIT(i);
+                       continue;
+               }
+
+               num_connectors_enabled++;
+
+               new_crtc = drm_fb_helper_crtc(fb_helper, connector->state->crtc);
+
+               /*
+                * Make sure we're not trying to drive multiple connectors
+                * with a single CRTC, since our cloning support may not
+                * match the BIOS.
+                */
+               for (j = 0; j < count; j++) {
+                       if (crtcs[j] == new_crtc) {
+                               DRM_DEBUG_KMS("fallback: cloned configuration\n");
+                               goto bail;
+                       }
+               }
+
+               DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
+                             connector->name);
+
+               /* go for command line mode first */
+               modes[i] = drm_pick_cmdline_mode(fb_conn);
+
+               /* try for preferred next */
+               if (!modes[i]) {
+                       DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
+                                     connector->name, connector->has_tile);
+                       modes[i] = drm_has_preferred_mode(fb_conn, width,
+                                                         height);
+               }
+
+               /* No preferred mode marked by the EDID? Are there any modes? */
+               if (!modes[i] && !list_empty(&connector->modes)) {
+                       DRM_DEBUG_KMS("using first mode listed on connector %s\n",
+                                     connector->name);
+                       modes[i] = list_first_entry(&connector->modes,
+                                                   struct drm_display_mode,
+                                                   head);
+               }
+
+               /* last resort: use current mode */
+               if (!modes[i]) {
+                       /*
+                        * IMPORTANT: We want to use the adjusted mode (i.e.
+                        * after the panel fitter upscaling) as the initial
+                        * config, not the input mode, which is what crtc->mode
+                        * usually contains. But since our current
+                        * code puts a mode derived from the post-pfit timings
+                        * into crtc->mode this works out correctly.
+                        *
+                        * This is crtc->mode and not crtc->state->mode for the
+                        * fastboot check to work correctly.
+                        */
+                       DRM_DEBUG_KMS("looking for current mode on connector %s\n",
+                                     connector->name);
+                       modes[i] = &connector->state->crtc->mode;
+               }
+               crtcs[i] = new_crtc;
+
+               DRM_DEBUG_KMS("connector %s on [CRTC:%d:%s]: %dx%d%s\n",
+                             connector->name,
+                             connector->state->crtc->base.id,
+                             connector->state->crtc->name,
+                             modes[i]->hdisplay, modes[i]->vdisplay,
+                             modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" : "");
+
+               fallback = false;
+               conn_configured |= BIT(i);
+       }
+
+       if (conn_configured != conn_seq) { /* repeat until no more are found */
+               conn_seq = conn_configured;
+               goto retry;
+       }
+
+       /*
+        * If the BIOS didn't enable everything it could, fall back to have the
+        * same user experiencing of lighting up as much as possible like the
+        * fbdev helper library.
+        */
+       if (num_connectors_enabled != num_connectors_detected &&
+           num_connectors_enabled < dev->mode_config.num_crtc) {
+               DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
+               DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled,
+                             num_connectors_detected);
+               fallback = true;
+       }
+
+       if (fallback) {
+bail:
+               DRM_DEBUG_KMS("Not using firmware configuration\n");
+               memcpy(enabled, save_enabled, count);
+               ret = false;
+       }
+
+       drm_modeset_drop_locks(&ctx);
+       drm_modeset_acquire_fini(&ctx);
+
+       kfree(save_enabled);
+       return ret;
+}
+
 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper,
                            u32 width, u32 height)
 {
@@ -2593,10 +2779,8 @@ static void drm_setup_crtcs(struct drm_fb_helper *fb_helper,
                DRM_DEBUG_KMS("No connectors reported connected with modes\n");
        drm_enable_connectors(fb_helper, enabled);
 
-       if (!(fb_helper->funcs->initial_config &&
-             fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
-                                              offsets,
-                                              enabled, width, height))) {
+       if (!drm_fb_helper_firmware_config(fb_helper, crtcs, modes, offsets,
+                                          enabled, width, height)) {
                memset(modes, 0, fb_helper->connector_count*sizeof(modes[0]));
                memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0]));
                memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0]));
@@ -2780,9 +2964,8 @@ __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper,
  *
  * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
  * to let the driver allocate and initialize the fbdev info structure and the
- * drm framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
- * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
- * values for the fbdev info structure.
+ * drm framebuffer used to back the fbdev. drm_fb_helper_fill_info() is provided
+ * as a helper to setup simple default values for the fbdev info structure.
  *
  * HANG DEBUGGING:
  *
@@ -3151,7 +3334,6 @@ int drm_fb_helper_generic_probe(struct drm_fb_helper *fb_helper,
        if (IS_ERR(fbi))
                return PTR_ERR(fbi);
 
-       fbi->par = fb_helper;
        fbi->fbops = &drm_fbdev_fb_ops;
        fbi->screen_size = fb->height * fb->pitches[0];
        fbi->fix.smem_len = fbi->screen_size;
@@ -3162,10 +3344,7 @@ int drm_fb_helper_generic_probe(struct drm_fb_helper *fb_helper,
                fbi->fix.smem_start =
                        page_to_phys(virt_to_page(fbi->screen_buffer));
 #endif
-       strcpy(fbi->fix.id, "DRM emulated");
-
-       drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->format->depth);
-       drm_fb_helper_fill_var(fbi, fb_helper, sizes->fb_width, sizes->fb_height);
+       drm_fb_helper_fill_info(fbi, fb_helper, sizes);
 
        if (fb->funcs->dirty) {
                struct fb_ops *fbops;
@@ -3319,8 +3498,6 @@ int drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp)
                return ret;
        }
 
-       drm_client_add(&fb_helper->client);
-
        if (!preferred_bpp)
                preferred_bpp = dev->mode_config.preferred_depth;
        if (!preferred_bpp)
@@ -3331,6 +3508,8 @@ int drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp)
        if (ret)
                DRM_DEV_DEBUG(dev->dev, "client hotplug ret=%d\n", ret);
 
+       drm_client_register(&fb_helper->client);
+
        return 0;
 }
 EXPORT_SYMBOL(drm_fbdev_generic_setup);