Subversion Repositories Kolibri OS

Rev

Rev 6937 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright © 2007 David Airlie
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a
  5.  * copy of this software and associated documentation files (the "Software"),
  6.  * to deal in the Software without restriction, including without limitation
  7.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8.  * and/or sell copies of the Software, and to permit persons to whom the
  9.  * Software is furnished to do so, subject to the following conditions:
  10.  *
  11.  * The above copyright notice and this permission notice (including the next
  12.  * paragraph) shall be included in all copies or substantial portions of the
  13.  * Software.
  14.  *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  18.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21.  * DEALINGS IN THE SOFTWARE.
  22.  *
  23.  * Authors:
  24.  *     David Airlie
  25.  */
  26.  
  27. #include <linux/async.h>
  28. #include <linux/module.h>
  29. #include <linux/kernel.h>
  30. #include <linux/errno.h>
  31. #include <linux/string.h>
  32. #include <linux/mm.h>
  33. //#include <linux/tty.h>
  34. #include <linux/sysrq.h>
  35. #include <linux/delay.h>
  36. #include <linux/fb.h>
  37. #include <linux/init.h>
  38. #include <linux/vga_switcheroo.h>
  39.  
  40. #include <drm/drmP.h>
  41. #include <drm/drm_crtc.h>
  42. #include <drm/drm_fb_helper.h>
  43. #include "intel_drv.h"
  44. #include <drm/i915_drm.h>
  45. #include "i915_drv.h"
  46.  
  47. static int intel_fbdev_set_par(struct fb_info *info)
  48. {
  49.         struct drm_fb_helper *fb_helper = info->par;
  50.         struct intel_fbdev *ifbdev =
  51.                 container_of(fb_helper, struct intel_fbdev, helper);
  52.         int ret;
  53.  
  54.         ret = drm_fb_helper_set_par(info);
  55.  
  56.         if (ret == 0) {
  57.                 mutex_lock(&fb_helper->dev->struct_mutex);
  58.                 intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT);
  59.                 mutex_unlock(&fb_helper->dev->struct_mutex);
  60.         }
  61.  
  62.         return ret;
  63. }
  64.  
  65. static int intel_fbdev_blank(int blank, struct fb_info *info)
  66. {
  67.         struct drm_fb_helper *fb_helper = info->par;
  68.         struct intel_fbdev *ifbdev =
  69.                 container_of(fb_helper, struct intel_fbdev, helper);
  70.         int ret;
  71.  
  72.         ret = drm_fb_helper_blank(blank, info);
  73.  
  74.         if (ret == 0) {
  75.                 mutex_lock(&fb_helper->dev->struct_mutex);
  76.                 intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT);
  77.                 mutex_unlock(&fb_helper->dev->struct_mutex);
  78.         }
  79.  
  80.         return ret;
  81. }
  82.  
  83. static int intel_fbdev_pan_display(struct fb_var_screeninfo *var,
  84.                                    struct fb_info *info)
  85. {
  86.         struct drm_fb_helper *fb_helper = info->par;
  87.         struct intel_fbdev *ifbdev =
  88.                 container_of(fb_helper, struct intel_fbdev, helper);
  89.  
  90.         int ret;
  91.         ret = drm_fb_helper_pan_display(var, info);
  92.  
  93.         if (ret == 0) {
  94.                 mutex_lock(&fb_helper->dev->struct_mutex);
  95.                 intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT);
  96.                 mutex_unlock(&fb_helper->dev->struct_mutex);
  97.         }
  98.  
  99.         return ret;
  100. }
  101.  
  102. static struct fb_ops intelfb_ops = {
  103.         .owner = THIS_MODULE,
  104.         .fb_check_var = drm_fb_helper_check_var,
  105.         .fb_set_par = intel_fbdev_set_par,
  106. //   .fb_fillrect = cfb_fillrect,
  107. //   .fb_copyarea = cfb_copyarea,
  108. //   .fb_imageblit = cfb_imageblit,
  109.         .fb_pan_display = intel_fbdev_pan_display,
  110.         .fb_blank = intel_fbdev_blank,
  111. //   .fb_setcmap = drm_fb_helper_setcmap,
  112. //   .fb_debug_enter = drm_fb_helper_debug_enter,
  113. //   .fb_debug_leave = drm_fb_helper_debug_leave,
  114. };
  115.  
  116. static int intelfb_alloc(struct drm_fb_helper *helper,
  117.                          struct drm_fb_helper_surface_size *sizes)
  118. {
  119.         struct intel_fbdev *ifbdev =
  120.                 container_of(helper, struct intel_fbdev, helper);
  121.         struct drm_framebuffer *fb = NULL;
  122.         struct drm_device *dev = helper->dev;
  123.         struct drm_i915_private *dev_priv = to_i915(dev);
  124.         struct drm_mode_fb_cmd2 mode_cmd = {};
  125.         struct drm_i915_gem_object *obj = NULL;
  126.         int size, ret;
  127.  
  128.         /* we don't do packed 24bpp */
  129.         if (sizes->surface_bpp == 24)
  130.                 sizes->surface_bpp = 32;
  131.  
  132.         mode_cmd.width = sizes->surface_width;
  133.         mode_cmd.height = sizes->surface_height;
  134.  
  135.         mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
  136.                                     DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
  137.         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  138.                                                           sizes->surface_depth);
  139.  
  140.         mutex_lock(&dev->struct_mutex);
  141.  
  142.         size = mode_cmd.pitches[0] * mode_cmd.height;
  143.         size = PAGE_ALIGN(size);
  144.  
  145.         /* If the FB is too big, just don't use it since fbdev is not very
  146.          * important and we should probably use that space with FBC or other
  147.          * features. */
  148.         if (size * 2 < dev_priv->gtt.stolen_usable_size)
  149.                 obj = i915_gem_object_create_stolen(dev, size);
  150.         if (obj == NULL)
  151.                 obj = i915_gem_alloc_object(dev, size);
  152.         if (!obj) {
  153.                 DRM_ERROR("failed to allocate framebuffer\n");
  154.                 ret = -ENOMEM;
  155.                 goto out;
  156.         }
  157.  
  158.         fb = __intel_framebuffer_create(dev, &mode_cmd, obj);
  159.         if (IS_ERR(fb)) {
  160.                 drm_gem_object_unreference(&obj->base);
  161.                 ret = PTR_ERR(fb);
  162.                 goto out;
  163.         }
  164.  
  165.         mutex_unlock(&dev->struct_mutex);
  166.  
  167.         ifbdev->fb = to_intel_framebuffer(fb);
  168.  
  169.         return 0;
  170.  
  171. out:
  172.         mutex_unlock(&dev->struct_mutex);
  173.         return ret;
  174. }
  175.  
  176. static int intelfb_create(struct drm_fb_helper *helper,
  177.                           struct drm_fb_helper_surface_size *sizes)
  178. {
  179.         struct intel_fbdev *ifbdev =
  180.                 container_of(helper, struct intel_fbdev, helper);
  181.         struct intel_framebuffer *intel_fb = ifbdev->fb;
  182.         struct drm_device *dev = helper->dev;
  183.         struct drm_i915_private *dev_priv = dev->dev_private;
  184.         struct fb_info *info;
  185.         struct drm_framebuffer *fb;
  186.         struct drm_i915_gem_object *obj;
  187.         int size, ret;
  188.         bool prealloc = false;
  189.  
  190.         if (intel_fb &&
  191.             (sizes->fb_width > intel_fb->base.width ||
  192.              sizes->fb_height > intel_fb->base.height)) {
  193.                 DRM_DEBUG_KMS("BIOS fb too small (%dx%d), we require (%dx%d),"
  194.                               " releasing it\n",
  195.                               intel_fb->base.width, intel_fb->base.height,
  196.                               sizes->fb_width, sizes->fb_height);
  197.                 drm_framebuffer_unreference(&intel_fb->base);
  198.                 intel_fb = ifbdev->fb = NULL;
  199.         }
  200.         if (!intel_fb || WARN_ON(!intel_fb->obj)) {
  201.                 DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
  202.                 ret = intelfb_alloc(helper, sizes);
  203.                 if (ret)
  204.                         return ret;
  205.                 intel_fb = ifbdev->fb;
  206.         } else {
  207.                 DRM_DEBUG_KMS("re-using BIOS fb\n");
  208.                 prealloc = true;
  209.                 sizes->fb_width = intel_fb->base.width;
  210.                 sizes->fb_height = intel_fb->base.height;
  211.         }
  212.  
  213.         obj = intel_fb->obj;
  214.         size = obj->base.size;
  215.  
  216.         mutex_lock(&dev->struct_mutex);
  217.  
  218.         /* Pin the GGTT vma for our access via info->screen_base.
  219.          * This also validates that any existing fb inherited from the
  220.          * BIOS is suitable for own access.
  221.          */
  222.         ret = intel_pin_and_fence_fb_obj(NULL, &ifbdev->fb->base, NULL);
  223.         if (ret)
  224.                 goto out_unlock;
  225.  
  226.         info = drm_fb_helper_alloc_fbi(helper);
  227.         if (IS_ERR(info)) {
  228.                 DRM_ERROR("Failed to allocate fb_info\n");
  229.                 ret = PTR_ERR(info);
  230.                 goto out_unpin;
  231.         }
  232.  
  233.         info->par = helper;
  234.  
  235.         fb = &ifbdev->fb->base;
  236.  
  237.         ifbdev->helper.fb = fb;
  238.  
  239.         strcpy(info->fix.id, "inteldrmfb");
  240.  
  241.         info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
  242.         info->fbops = &intelfb_ops;
  243.  
  244.         /* setup aperture base/size for vesafb takeover */
  245.         info->apertures->ranges[0].base = dev->mode_config.fb_base;
  246.         info->apertures->ranges[0].size = dev_priv->gtt.mappable_end;
  247.  
  248.         info->fix.smem_start = dev->mode_config.fb_base + i915_gem_obj_ggtt_offset(obj);
  249.         info->fix.smem_len = size;
  250.  
  251.         info->screen_base = (void*) 0xFE000000;
  252.         info->screen_size = size;
  253.  
  254.         /* This driver doesn't need a VT switch to restore the mode on resume */
  255.         info->skip_vt_switch = true;
  256.  
  257.         drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  258.         drm_fb_helper_fill_var(info, &ifbdev->helper, sizes->fb_width, sizes->fb_height);
  259.  
  260.         /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
  261.  
  262.         DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08llx, bo %p\n",
  263.                       fb->width, fb->height,
  264.                       i915_gem_obj_ggtt_offset(obj), obj);
  265.  
  266.         mutex_unlock(&dev->struct_mutex);
  267.         return 0;
  268.  
  269. out_destroy_fbi:
  270.         drm_fb_helper_release_fbi(helper);
  271. out_unpin:
  272.         i915_gem_object_ggtt_unpin(obj);
  273. out_unlock:
  274.         mutex_unlock(&dev->struct_mutex);
  275.         return ret;
  276. }
  277.  
  278. /** Sets the color ramps on behalf of RandR */
  279. static void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  280.                                     u16 blue, int regno)
  281. {
  282.         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  283.  
  284.         intel_crtc->lut_r[regno] = red >> 8;
  285.         intel_crtc->lut_g[regno] = green >> 8;
  286.         intel_crtc->lut_b[regno] = blue >> 8;
  287. }
  288.  
  289. static void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  290.                                     u16 *blue, int regno)
  291. {
  292.         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  293.  
  294.         *red = intel_crtc->lut_r[regno] << 8;
  295.         *green = intel_crtc->lut_g[regno] << 8;
  296.         *blue = intel_crtc->lut_b[regno] << 8;
  297. }
  298.  
  299. static struct drm_fb_helper_crtc *
  300. intel_fb_helper_crtc(struct drm_fb_helper *fb_helper, struct drm_crtc *crtc)
  301. {
  302.         int i;
  303.  
  304.         for (i = 0; i < fb_helper->crtc_count; i++)
  305.                 if (fb_helper->crtc_info[i].mode_set.crtc == crtc)
  306.                         return &fb_helper->crtc_info[i];
  307.  
  308.         return NULL;
  309. }
  310.  
  311. /*
  312.  * Try to read the BIOS display configuration and use it for the initial
  313.  * fb configuration.
  314.  *
  315.  * The BIOS or boot loader will generally create an initial display
  316.  * configuration for us that includes some set of active pipes and displays.
  317.  * This routine tries to figure out which pipes and connectors are active
  318.  * and stuffs them into the crtcs and modes array given to us by the
  319.  * drm_fb_helper code.
  320.  *
  321.  * The overall sequence is:
  322.  *   intel_fbdev_init - from driver load
  323.  *     intel_fbdev_init_bios - initialize the intel_fbdev using BIOS data
  324.  *     drm_fb_helper_init - build fb helper structs
  325.  *     drm_fb_helper_single_add_all_connectors - more fb helper structs
  326.  *   intel_fbdev_initial_config - apply the config
  327.  *     drm_fb_helper_initial_config - call ->probe then register_framebuffer()
  328.  *         drm_setup_crtcs - build crtc config for fbdev
  329.  *           intel_fb_initial_config - find active connectors etc
  330.  *         drm_fb_helper_single_fb_probe - set up fbdev
  331.  *           intelfb_create - re-use or alloc fb, build out fbdev structs
  332.  *
  333.  * Note that we don't make special consideration whether we could actually
  334.  * switch to the selected modes without a full modeset. E.g. when the display
  335.  * is in VGA mode we need to recalculate watermarks and set a new high-res
  336.  * framebuffer anyway.
  337.  */
  338. static bool intel_fb_initial_config(struct drm_fb_helper *fb_helper,
  339.                                     struct drm_fb_helper_crtc **crtcs,
  340.                                     struct drm_display_mode **modes,
  341.                                     struct drm_fb_offset *offsets,
  342.                                     bool *enabled, int width, int height)
  343. {
  344.         struct drm_device *dev = fb_helper->dev;
  345.         int i, j;
  346.         bool *save_enabled;
  347.         bool fallback = true;
  348.         int num_connectors_enabled = 0;
  349.         int num_connectors_detected = 0;
  350.         uint64_t conn_configured = 0, mask;
  351.         int pass = 0;
  352.  
  353.         save_enabled = kcalloc(fb_helper->connector_count, sizeof(bool),
  354.                                GFP_KERNEL);
  355.         if (!save_enabled)
  356.                 return false;
  357.  
  358.         memcpy(save_enabled, enabled, fb_helper->connector_count);
  359.         mask = (1 << fb_helper->connector_count) - 1;
  360. retry:
  361.         for (i = 0; i < fb_helper->connector_count; i++) {
  362.                 struct drm_fb_helper_connector *fb_conn;
  363.                 struct drm_connector *connector;
  364.                 struct drm_encoder *encoder;
  365.                 struct drm_fb_helper_crtc *new_crtc;
  366.  
  367.                 fb_conn = fb_helper->connector_info[i];
  368.                 connector = fb_conn->connector;
  369.  
  370.                 if (conn_configured & (1 << i))
  371.                         continue;
  372.  
  373.                 if (pass == 0 && !connector->has_tile)
  374.                         continue;
  375.  
  376.                 if (connector->status == connector_status_connected)
  377.                         num_connectors_detected++;
  378.  
  379.                 if (!enabled[i]) {
  380.                         DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
  381.                                       connector->name);
  382.                         conn_configured |= (1 << i);
  383.                         continue;
  384.                 }
  385.  
  386.                 if (connector->force == DRM_FORCE_OFF) {
  387.                         DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
  388.                                       connector->name);
  389.                         enabled[i] = false;
  390.                         continue;
  391.                 }
  392.  
  393.                 encoder = connector->state->best_encoder;
  394.                 if (!encoder || WARN_ON(!connector->state->crtc)) {
  395.                         if (connector->force > DRM_FORCE_OFF)
  396.                                 goto bail;
  397.  
  398.                         DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
  399.                                       connector->name);
  400.                         enabled[i] = false;
  401.                         conn_configured |= (1 << i);
  402.                         continue;
  403.                 }
  404.  
  405.                 num_connectors_enabled++;
  406.  
  407.                 new_crtc = intel_fb_helper_crtc(fb_helper, connector->state->crtc);
  408.  
  409.                 /*
  410.                  * Make sure we're not trying to drive multiple connectors
  411.                  * with a single CRTC, since our cloning support may not
  412.                  * match the BIOS.
  413.                  */
  414.                 for (j = 0; j < fb_helper->connector_count; j++) {
  415.                         if (crtcs[j] == new_crtc) {
  416.                                 DRM_DEBUG_KMS("fallback: cloned configuration\n");
  417.                                 goto bail;
  418.                         }
  419.                 }
  420.  
  421.                 DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
  422.                               connector->name);
  423.  
  424.                 /* go for command line mode first */
  425.                 modes[i] = drm_pick_cmdline_mode(fb_conn, width, height);
  426.  
  427.                 /* try for preferred next */
  428.                 if (!modes[i]) {
  429.                         DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
  430.                                       connector->name, connector->has_tile);
  431.                         modes[i] = drm_has_preferred_mode(fb_conn, width,
  432.                                                           height);
  433.                 }
  434.  
  435.                 /* No preferred mode marked by the EDID? Are there any modes? */
  436.                 if (!modes[i] && !list_empty(&connector->modes)) {
  437.                         DRM_DEBUG_KMS("using first mode listed on connector %s\n",
  438.                                       connector->name);
  439.                         modes[i] = list_first_entry(&connector->modes,
  440.                                                     struct drm_display_mode,
  441.                                                     head);
  442.                 }
  443.  
  444.                 /* last resort: use current mode */
  445.                 if (!modes[i]) {
  446.                         /*
  447.                          * IMPORTANT: We want to use the adjusted mode (i.e.
  448.                          * after the panel fitter upscaling) as the initial
  449.                          * config, not the input mode, which is what crtc->mode
  450.                          * usually contains. But since our current
  451.                          * code puts a mode derived from the post-pfit timings
  452.                          * into crtc->mode this works out correctly.
  453.                          *
  454.                          * This is crtc->mode and not crtc->state->mode for the
  455.                          * fastboot check to work correctly. crtc_state->mode has
  456.                          * I915_MODE_FLAG_INHERITED, which we clear to force check
  457.                          * state.
  458.                          */
  459.                         DRM_DEBUG_KMS("looking for current mode on connector %s\n",
  460.                                       connector->name);
  461.                         modes[i] = &connector->state->crtc->mode;
  462.                 }
  463.                 crtcs[i] = new_crtc;
  464.  
  465.                 DRM_DEBUG_KMS("connector %s on pipe %c [CRTC:%d]: %dx%d%s\n",
  466.                               connector->name,
  467.                               pipe_name(to_intel_crtc(connector->state->crtc)->pipe),
  468.                               connector->state->crtc->base.id,
  469.                               modes[i]->hdisplay, modes[i]->vdisplay,
  470.                               modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" :"");
  471.  
  472.                 fallback = false;
  473.                 conn_configured |= (1 << i);
  474.         }
  475.  
  476.         if ((conn_configured & mask) != mask) {
  477.                 pass++;
  478.                 goto retry;
  479.         }
  480.  
  481.         /*
  482.          * If the BIOS didn't enable everything it could, fall back to have the
  483.          * same user experiencing of lighting up as much as possible like the
  484.          * fbdev helper library.
  485.          */
  486.         if (num_connectors_enabled != num_connectors_detected &&
  487.             num_connectors_enabled < INTEL_INFO(dev)->num_pipes) {
  488.                 DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
  489.                 DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled,
  490.                               num_connectors_detected);
  491.                 fallback = true;
  492.         }
  493.  
  494.         if (fallback) {
  495. bail:
  496.                 DRM_DEBUG_KMS("Not using firmware configuration\n");
  497.                 memcpy(enabled, save_enabled, fb_helper->connector_count);
  498.                 kfree(save_enabled);
  499.                 return false;
  500.         }
  501.  
  502.         kfree(save_enabled);
  503.         return true;
  504. }
  505.  
  506. static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
  507.         .initial_config = intel_fb_initial_config,
  508.         .gamma_set = intel_crtc_fb_gamma_set,
  509.         .gamma_get = intel_crtc_fb_gamma_get,
  510.         .fb_probe = intelfb_create,
  511. };
  512.  
  513. static void intel_fbdev_destroy(struct drm_device *dev,
  514.                                 struct intel_fbdev *ifbdev)
  515. {
  516.         /* We rely on the object-free to release the VMA pinning for
  517.          * the info->screen_base mmaping. Leaking the VMA is simpler than
  518.          * trying to rectify all the possible error paths leading here.
  519.          */
  520.  
  521.         drm_fb_helper_unregister_fbi(&ifbdev->helper);
  522.         drm_fb_helper_release_fbi(&ifbdev->helper);
  523.  
  524.         drm_fb_helper_fini(&ifbdev->helper);
  525.  
  526.         if (ifbdev->fb) {
  527.                 drm_framebuffer_unregister_private(&ifbdev->fb->base);
  528.                 drm_framebuffer_remove(&ifbdev->fb->base);
  529.         }
  530. }
  531.  
  532. /*
  533.  * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
  534.  * The core display code will have read out the current plane configuration,
  535.  * so we use that to figure out if there's an object for us to use as the
  536.  * fb, and if so, we re-use it for the fbdev configuration.
  537.  *
  538.  * Note we only support a single fb shared across pipes for boot (mostly for
  539.  * fbcon), so we just find the biggest and use that.
  540.  */
  541. static bool intel_fbdev_init_bios(struct drm_device *dev,
  542.                                  struct intel_fbdev *ifbdev)
  543. {
  544.         struct intel_framebuffer *fb = NULL;
  545.         struct drm_crtc *crtc;
  546.         struct intel_crtc *intel_crtc;
  547.         unsigned int max_size = 0;
  548.  
  549.         /* Find the largest fb */
  550.         for_each_crtc(dev, crtc) {
  551.                 struct drm_i915_gem_object *obj =
  552.                         intel_fb_obj(crtc->primary->state->fb);
  553.                 intel_crtc = to_intel_crtc(crtc);
  554.  
  555.                 if (!crtc->state->active || !obj) {
  556.                         DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
  557.                                       pipe_name(intel_crtc->pipe));
  558.                         continue;
  559.                 }
  560.  
  561.                 if (obj->base.size > max_size) {
  562.                         DRM_DEBUG_KMS("found possible fb from plane %c\n",
  563.                                       pipe_name(intel_crtc->pipe));
  564.                         fb = to_intel_framebuffer(crtc->primary->state->fb);
  565.                         max_size = obj->base.size;
  566.                 }
  567.         }
  568.  
  569.         if (!fb) {
  570.                 DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
  571.                 goto out;
  572.         }
  573.  
  574.         /* Now make sure all the pipes will fit into it */
  575.         for_each_crtc(dev, crtc) {
  576.                 unsigned int cur_size;
  577.  
  578.                 intel_crtc = to_intel_crtc(crtc);
  579.  
  580.                 if (!crtc->state->active) {
  581.                         DRM_DEBUG_KMS("pipe %c not active, skipping\n",
  582.                                       pipe_name(intel_crtc->pipe));
  583.                         continue;
  584.                 }
  585.  
  586.                 DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
  587.                               pipe_name(intel_crtc->pipe));
  588.  
  589.                 /*
  590.                  * See if the plane fb we found above will fit on this
  591.                  * pipe.  Note we need to use the selected fb's pitch and bpp
  592.                  * rather than the current pipe's, since they differ.
  593.                  */
  594.                 cur_size = intel_crtc->config->base.adjusted_mode.crtc_hdisplay;
  595.                 cur_size = cur_size * fb->base.bits_per_pixel / 8;
  596.                 if (fb->base.pitches[0] < cur_size) {
  597.                         DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
  598.                                       pipe_name(intel_crtc->pipe),
  599.                                       cur_size, fb->base.pitches[0]);
  600.                         fb = NULL;
  601.                         break;
  602.                 }
  603.  
  604.                 cur_size = intel_crtc->config->base.adjusted_mode.crtc_vdisplay;
  605.                 cur_size = intel_fb_align_height(dev, cur_size,
  606.                                                  fb->base.pixel_format,
  607.                                                  fb->base.modifier[0]);
  608.                 cur_size *= fb->base.pitches[0];
  609.                 DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
  610.                               pipe_name(intel_crtc->pipe),
  611.                               intel_crtc->config->base.adjusted_mode.crtc_hdisplay,
  612.                               intel_crtc->config->base.adjusted_mode.crtc_vdisplay,
  613.                               fb->base.bits_per_pixel,
  614.                               cur_size);
  615.  
  616.                 if (cur_size > max_size) {
  617.                         DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
  618.                                       pipe_name(intel_crtc->pipe),
  619.                                       cur_size, max_size);
  620.                         fb = NULL;
  621.                         break;
  622.                 }
  623.  
  624.                 DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
  625.                               pipe_name(intel_crtc->pipe),
  626.                               max_size, cur_size);
  627.         }
  628.  
  629.         if (!fb) {
  630.                 DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
  631.                 goto out;
  632.         }
  633.  
  634.         ifbdev->preferred_bpp = fb->base.bits_per_pixel;
  635.         ifbdev->fb = fb;
  636.  
  637.         drm_framebuffer_reference(&ifbdev->fb->base);
  638.  
  639.         /* Final pass to check if any active pipes don't have fbs */
  640.         for_each_crtc(dev, crtc) {
  641.                 intel_crtc = to_intel_crtc(crtc);
  642.  
  643.                 if (!crtc->state->active)
  644.                         continue;
  645.  
  646.                 WARN(!crtc->primary->fb,
  647.                      "re-used BIOS config but lost an fb on crtc %d\n",
  648.                      crtc->base.id);
  649.         }
  650.  
  651.  
  652.         DRM_DEBUG_KMS("using BIOS fb for initial console\n");
  653.         return true;
  654.  
  655. out:
  656.  
  657.         return false;
  658. }
  659.  
  660. int intel_fbdev_init(struct drm_device *dev)
  661. {
  662.         struct intel_fbdev *ifbdev;
  663.         struct drm_i915_private *dev_priv = dev->dev_private;
  664.         int ret;
  665.  
  666.         if (WARN_ON(INTEL_INFO(dev)->num_pipes == 0))
  667.                 return -ENODEV;
  668.  
  669.         ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
  670.         if (ifbdev == NULL)
  671.                 return -ENOMEM;
  672.  
  673.         drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
  674.  
  675.         if (!intel_fbdev_init_bios(dev, ifbdev))
  676.                 ifbdev->preferred_bpp = 32;
  677.  
  678.         ret = drm_fb_helper_init(dev, &ifbdev->helper,
  679.                                  INTEL_INFO(dev)->num_pipes, 4);
  680.         if (ret) {
  681.                 kfree(ifbdev);
  682.                 return ret;
  683.         }
  684.  
  685.         ifbdev->helper.atomic = true;
  686.  
  687.         dev_priv->fbdev = ifbdev;
  688.  
  689.         drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
  690.  
  691.         return 0;
  692. }
  693.  
  694. void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
  695. {
  696.         struct drm_i915_private *dev_priv = data;
  697.         struct intel_fbdev *ifbdev = dev_priv->fbdev;
  698.  
  699.         /* Due to peculiar init order wrt to hpd handling this is separate. */
  700.         drm_fb_helper_initial_config(&ifbdev->helper, ifbdev->preferred_bpp);
  701. }
  702.  
  703.  
  704. void intel_fbdev_output_poll_changed(struct drm_device *dev)
  705. {
  706.         struct drm_i915_private *dev_priv = dev->dev_private;
  707.         if (dev_priv->fbdev)
  708.                 drm_fb_helper_hotplug_event(&dev_priv->fbdev->helper);
  709. }
  710.