Subversion Repositories Kolibri OS

Rev

Rev 6660 | Go to most recent revision | 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.         if (!IS_ERR_OR_NULL(fb))
  174.                 drm_framebuffer_unreference(fb);
  175.         return ret;
  176. }
  177.  
  178. static int intelfb_create(struct drm_fb_helper *helper,
  179.                           struct drm_fb_helper_surface_size *sizes)
  180. {
  181.         struct intel_fbdev *ifbdev =
  182.                 container_of(helper, struct intel_fbdev, helper);
  183.         struct intel_framebuffer *intel_fb = ifbdev->fb;
  184.         struct drm_device *dev = helper->dev;
  185.         struct drm_i915_private *dev_priv = dev->dev_private;
  186.         struct fb_info *info;
  187.         struct drm_framebuffer *fb;
  188.         struct drm_i915_gem_object *obj;
  189.         int size, ret;
  190.         bool prealloc = false;
  191.  
  192.         if (intel_fb &&
  193.             (sizes->fb_width > intel_fb->base.width ||
  194.              sizes->fb_height > intel_fb->base.height)) {
  195.                 DRM_DEBUG_KMS("BIOS fb too small (%dx%d), we require (%dx%d),"
  196.                               " releasing it\n",
  197.                               intel_fb->base.width, intel_fb->base.height,
  198.                               sizes->fb_width, sizes->fb_height);
  199.                 drm_framebuffer_unreference(&intel_fb->base);
  200.                 intel_fb = ifbdev->fb = NULL;
  201.         }
  202.         if (!intel_fb || WARN_ON(!intel_fb->obj)) {
  203.                 DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
  204.                 ret = intelfb_alloc(helper, sizes);
  205.                 if (ret)
  206.                         return ret;
  207.                 intel_fb = ifbdev->fb;
  208.         } else {
  209.                 DRM_DEBUG_KMS("re-using BIOS fb\n");
  210.                 prealloc = true;
  211.                 sizes->fb_width = intel_fb->base.width;
  212.                 sizes->fb_height = intel_fb->base.height;
  213.         }
  214.  
  215.         obj = intel_fb->obj;
  216.         size = obj->base.size;
  217.  
  218.         mutex_lock(&dev->struct_mutex);
  219.  
  220.         /* Pin the GGTT vma for our access via info->screen_base.
  221.          * This also validates that any existing fb inherited from the
  222.          * BIOS is suitable for own access.
  223.          */
  224.         ret = intel_pin_and_fence_fb_obj(NULL, &ifbdev->fb->base, NULL);
  225.         if (ret)
  226.                 goto out_unlock;
  227.  
  228.         info = drm_fb_helper_alloc_fbi(helper);
  229.         if (IS_ERR(info)) {
  230.                 DRM_ERROR("Failed to allocate fb_info\n");
  231.                 ret = PTR_ERR(info);
  232.                 goto out_unpin;
  233.         }
  234.  
  235.         info->par = helper;
  236.  
  237.         fb = &ifbdev->fb->base;
  238.  
  239.         ifbdev->helper.fb = fb;
  240.  
  241.         strcpy(info->fix.id, "inteldrmfb");
  242.  
  243.         info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
  244.         info->fbops = &intelfb_ops;
  245.  
  246.         /* setup aperture base/size for vesafb takeover */
  247.         info->apertures->ranges[0].base = dev->mode_config.fb_base;
  248.         info->apertures->ranges[0].size = dev_priv->gtt.mappable_end;
  249.  
  250.         info->fix.smem_start = dev->mode_config.fb_base + i915_gem_obj_ggtt_offset(obj);
  251.         info->fix.smem_len = size;
  252.  
  253.         info->screen_base = (void*) 0xFE000000;
  254.         info->screen_size = size;
  255.  
  256.         /* This driver doesn't need a VT switch to restore the mode on resume */
  257.         info->skip_vt_switch = true;
  258.  
  259.         drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  260.         drm_fb_helper_fill_var(info, &ifbdev->helper, sizes->fb_width, sizes->fb_height);
  261.  
  262.         /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
  263.  
  264.         DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08llx, bo %p\n",
  265.                       fb->width, fb->height,
  266.                       i915_gem_obj_ggtt_offset(obj), obj);
  267.  
  268.         mutex_unlock(&dev->struct_mutex);
  269.         return 0;
  270.  
  271. out_destroy_fbi:
  272.         drm_fb_helper_release_fbi(helper);
  273. out_unpin:
  274.         i915_gem_object_ggtt_unpin(obj);
  275. out_unlock:
  276.         mutex_unlock(&dev->struct_mutex);
  277.         return ret;
  278. }
  279.  
  280. /** Sets the color ramps on behalf of RandR */
  281. static void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  282.                                     u16 blue, int regno)
  283. {
  284.         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  285.  
  286.         intel_crtc->lut_r[regno] = red >> 8;
  287.         intel_crtc->lut_g[regno] = green >> 8;
  288.         intel_crtc->lut_b[regno] = blue >> 8;
  289. }
  290.  
  291. static void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  292.                                     u16 *blue, int regno)
  293. {
  294.         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  295.  
  296.         *red = intel_crtc->lut_r[regno] << 8;
  297.         *green = intel_crtc->lut_g[regno] << 8;
  298.         *blue = intel_crtc->lut_b[regno] << 8;
  299. }
  300.  
  301. static struct drm_fb_helper_crtc *
  302. intel_fb_helper_crtc(struct drm_fb_helper *fb_helper, struct drm_crtc *crtc)
  303. {
  304.         int i;
  305.  
  306.         for (i = 0; i < fb_helper->crtc_count; i++)
  307.                 if (fb_helper->crtc_info[i].mode_set.crtc == crtc)
  308.                         return &fb_helper->crtc_info[i];
  309.  
  310.         return NULL;
  311. }
  312.  
  313. /*
  314.  * Try to read the BIOS display configuration and use it for the initial
  315.  * fb configuration.
  316.  *
  317.  * The BIOS or boot loader will generally create an initial display
  318.  * configuration for us that includes some set of active pipes and displays.
  319.  * This routine tries to figure out which pipes and connectors are active
  320.  * and stuffs them into the crtcs and modes array given to us by the
  321.  * drm_fb_helper code.
  322.  *
  323.  * The overall sequence is:
  324.  *   intel_fbdev_init - from driver load
  325.  *     intel_fbdev_init_bios - initialize the intel_fbdev using BIOS data
  326.  *     drm_fb_helper_init - build fb helper structs
  327.  *     drm_fb_helper_single_add_all_connectors - more fb helper structs
  328.  *   intel_fbdev_initial_config - apply the config
  329.  *     drm_fb_helper_initial_config - call ->probe then register_framebuffer()
  330.  *         drm_setup_crtcs - build crtc config for fbdev
  331.  *           intel_fb_initial_config - find active connectors etc
  332.  *         drm_fb_helper_single_fb_probe - set up fbdev
  333.  *           intelfb_create - re-use or alloc fb, build out fbdev structs
  334.  *
  335.  * Note that we don't make special consideration whether we could actually
  336.  * switch to the selected modes without a full modeset. E.g. when the display
  337.  * is in VGA mode we need to recalculate watermarks and set a new high-res
  338.  * framebuffer anyway.
  339.  */
  340. static bool intel_fb_initial_config(struct drm_fb_helper *fb_helper,
  341.                                     struct drm_fb_helper_crtc **crtcs,
  342.                                     struct drm_display_mode **modes,
  343.                                     struct drm_fb_offset *offsets,
  344.                                     bool *enabled, int width, int height)
  345. {
  346.         struct drm_device *dev = fb_helper->dev;
  347.         int i, j;
  348.         bool *save_enabled;
  349.         bool fallback = true;
  350.         int num_connectors_enabled = 0;
  351.         int num_connectors_detected = 0;
  352.         uint64_t conn_configured = 0, mask;
  353.         int pass = 0;
  354.  
  355.         save_enabled = kcalloc(fb_helper->connector_count, sizeof(bool),
  356.                                GFP_KERNEL);
  357.         if (!save_enabled)
  358.                 return false;
  359.  
  360.         memcpy(save_enabled, enabled, fb_helper->connector_count);
  361.         mask = (1 << fb_helper->connector_count) - 1;
  362. retry:
  363.         for (i = 0; i < fb_helper->connector_count; i++) {
  364.                 struct drm_fb_helper_connector *fb_conn;
  365.                 struct drm_connector *connector;
  366.                 struct drm_encoder *encoder;
  367.                 struct drm_fb_helper_crtc *new_crtc;
  368.  
  369.                 fb_conn = fb_helper->connector_info[i];
  370.                 connector = fb_conn->connector;
  371.  
  372.                 if (conn_configured & (1 << i))
  373.                         continue;
  374.  
  375.                 if (pass == 0 && !connector->has_tile)
  376.                         continue;
  377.  
  378.                 if (connector->status == connector_status_connected)
  379.                         num_connectors_detected++;
  380.  
  381.                 if (!enabled[i]) {
  382.                         DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
  383.                                       connector->name);
  384.                         conn_configured |= (1 << i);
  385.                         continue;
  386.                 }
  387.  
  388.                 if (connector->force == DRM_FORCE_OFF) {
  389.                         DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
  390.                                       connector->name);
  391.                         enabled[i] = false;
  392.                         continue;
  393.                 }
  394.  
  395.                 encoder = connector->encoder;
  396.                 if (!encoder || WARN_ON(!encoder->crtc)) {
  397.                         if (connector->force > DRM_FORCE_OFF)
  398.                                 goto bail;
  399.  
  400.                         DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
  401.                                       connector->name);
  402.                         enabled[i] = false;
  403.                         conn_configured |= (1 << i);
  404.                         continue;
  405.                 }
  406.  
  407.                 num_connectors_enabled++;
  408.  
  409.                 new_crtc = intel_fb_helper_crtc(fb_helper, encoder->crtc);
  410.  
  411.                 /*
  412.                  * Make sure we're not trying to drive multiple connectors
  413.                  * with a single CRTC, since our cloning support may not
  414.                  * match the BIOS.
  415.                  */
  416.                 for (j = 0; j < fb_helper->connector_count; j++) {
  417.                         if (crtcs[j] == new_crtc) {
  418.                                 DRM_DEBUG_KMS("fallback: cloned configuration\n");
  419.                                 goto bail;
  420.                         }
  421.                 }
  422.  
  423.                 DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
  424.                               connector->name);
  425.  
  426.                 /* go for command line mode first */
  427.                 modes[i] = drm_pick_cmdline_mode(fb_conn, width, height);
  428.  
  429.                 /* try for preferred next */
  430.                 if (!modes[i]) {
  431.                         DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
  432.                                       connector->name, connector->has_tile);
  433.                         modes[i] = drm_has_preferred_mode(fb_conn, width,
  434.                                                           height);
  435.                 }
  436.  
  437.                 /* No preferred mode marked by the EDID? Are there any modes? */
  438.                 if (!modes[i] && !list_empty(&connector->modes)) {
  439.                         DRM_DEBUG_KMS("using first mode listed on connector %s\n",
  440.                                       connector->name);
  441.                         modes[i] = list_first_entry(&connector->modes,
  442.                                                     struct drm_display_mode,
  443.                                                     head);
  444.                 }
  445.  
  446.                 /* last resort: use current mode */
  447.                 if (!modes[i]) {
  448.                         /*
  449.                          * IMPORTANT: We want to use the adjusted mode (i.e.
  450.                          * after the panel fitter upscaling) as the initial
  451.                          * config, not the input mode, which is what crtc->mode
  452.                          * usually contains. But since our current
  453.                          * code puts a mode derived from the post-pfit timings
  454.                          * into crtc->mode this works out correctly.
  455.                          */
  456.                         DRM_DEBUG_KMS("looking for current mode on connector %s\n",
  457.                                       connector->name);
  458.                         modes[i] = &encoder->crtc->mode;
  459.                 }
  460.                 crtcs[i] = new_crtc;
  461.  
  462.                 DRM_DEBUG_KMS("connector %s on pipe %c [CRTC:%d]: %dx%d%s\n",
  463.                               connector->name,
  464.                               pipe_name(to_intel_crtc(encoder->crtc)->pipe),
  465.                               encoder->crtc->base.id,
  466.                               modes[i]->hdisplay, modes[i]->vdisplay,
  467.                               modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" :"");
  468.  
  469.                 fallback = false;
  470.                 conn_configured |= (1 << i);
  471.         }
  472.  
  473.         if ((conn_configured & mask) != mask) {
  474.                 pass++;
  475.                 goto retry;
  476.         }
  477.  
  478.         /*
  479.          * If the BIOS didn't enable everything it could, fall back to have the
  480.          * same user experiencing of lighting up as much as possible like the
  481.          * fbdev helper library.
  482.          */
  483.         if (num_connectors_enabled != num_connectors_detected &&
  484.             num_connectors_enabled < INTEL_INFO(dev)->num_pipes) {
  485.                 DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
  486.                 DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled,
  487.                               num_connectors_detected);
  488.                 fallback = true;
  489.         }
  490.  
  491.         if (fallback) {
  492. bail:
  493.                 DRM_DEBUG_KMS("Not using firmware configuration\n");
  494.                 memcpy(enabled, save_enabled, fb_helper->connector_count);
  495.                 kfree(save_enabled);
  496.                 return false;
  497.         }
  498.  
  499.         kfree(save_enabled);
  500.         return true;
  501. }
  502.  
  503. static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
  504.         .initial_config = intel_fb_initial_config,
  505.         .gamma_set = intel_crtc_fb_gamma_set,
  506.         .gamma_get = intel_crtc_fb_gamma_get,
  507.         .fb_probe = intelfb_create,
  508. };
  509.  
  510. static void intel_fbdev_destroy(struct drm_device *dev,
  511.                                 struct intel_fbdev *ifbdev)
  512. {
  513.         /* We rely on the object-free to release the VMA pinning for
  514.          * the info->screen_base mmaping. Leaking the VMA is simpler than
  515.          * trying to rectify all the possible error paths leading here.
  516.          */
  517.  
  518.         drm_fb_helper_unregister_fbi(&ifbdev->helper);
  519.         drm_fb_helper_release_fbi(&ifbdev->helper);
  520.  
  521.         drm_fb_helper_fini(&ifbdev->helper);
  522.  
  523.         if (ifbdev->fb) {
  524.         drm_framebuffer_unregister_private(&ifbdev->fb->base);
  525.         drm_framebuffer_remove(&ifbdev->fb->base);
  526. }
  527. }
  528.  
  529. /*
  530.  * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
  531.  * The core display code will have read out the current plane configuration,
  532.  * so we use that to figure out if there's an object for us to use as the
  533.  * fb, and if so, we re-use it for the fbdev configuration.
  534.  *
  535.  * Note we only support a single fb shared across pipes for boot (mostly for
  536.  * fbcon), so we just find the biggest and use that.
  537.  */
  538. static bool intel_fbdev_init_bios(struct drm_device *dev,
  539.                                  struct intel_fbdev *ifbdev)
  540. {
  541.         struct intel_framebuffer *fb = NULL;
  542.         struct drm_crtc *crtc;
  543.         struct intel_crtc *intel_crtc;
  544.         unsigned int max_size = 0;
  545.  
  546.         /* Find the largest fb */
  547.         for_each_crtc(dev, crtc) {
  548.                 struct drm_i915_gem_object *obj =
  549.                         intel_fb_obj(crtc->primary->state->fb);
  550.                 intel_crtc = to_intel_crtc(crtc);
  551.  
  552.                 if (!crtc->state->active || !obj) {
  553.                         DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
  554.                                       pipe_name(intel_crtc->pipe));
  555.                         continue;
  556.                 }
  557.  
  558.                 if (obj->base.size > max_size) {
  559.                         DRM_DEBUG_KMS("found possible fb from plane %c\n",
  560.                                       pipe_name(intel_crtc->pipe));
  561.                         fb = to_intel_framebuffer(crtc->primary->state->fb);
  562.                         max_size = obj->base.size;
  563.                 }
  564.         }
  565.  
  566.         if (!fb) {
  567.                 DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
  568.                 goto out;
  569.         }
  570.  
  571.         /* Now make sure all the pipes will fit into it */
  572.         for_each_crtc(dev, crtc) {
  573.                 unsigned int cur_size;
  574.  
  575.                 intel_crtc = to_intel_crtc(crtc);
  576.  
  577.                 if (!crtc->state->active) {
  578.                         DRM_DEBUG_KMS("pipe %c not active, skipping\n",
  579.                                       pipe_name(intel_crtc->pipe));
  580.                         continue;
  581.                 }
  582.  
  583.                 DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
  584.                               pipe_name(intel_crtc->pipe));
  585.  
  586.                 /*
  587.                  * See if the plane fb we found above will fit on this
  588.                  * pipe.  Note we need to use the selected fb's pitch and bpp
  589.                  * rather than the current pipe's, since they differ.
  590.                  */
  591.                 cur_size = intel_crtc->config->base.adjusted_mode.crtc_hdisplay;
  592.                 cur_size = cur_size * fb->base.bits_per_pixel / 8;
  593.                 if (fb->base.pitches[0] < cur_size) {
  594.                         DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
  595.                                       pipe_name(intel_crtc->pipe),
  596.                                       cur_size, fb->base.pitches[0]);
  597.                         fb = NULL;
  598.                         break;
  599.                 }
  600.  
  601.                 cur_size = intel_crtc->config->base.adjusted_mode.crtc_vdisplay;
  602.                 cur_size = intel_fb_align_height(dev, cur_size,
  603.                                                  fb->base.pixel_format,
  604.                                                  fb->base.modifier[0]);
  605.                 cur_size *= fb->base.pitches[0];
  606.                 DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
  607.                               pipe_name(intel_crtc->pipe),
  608.                               intel_crtc->config->base.adjusted_mode.crtc_hdisplay,
  609.                               intel_crtc->config->base.adjusted_mode.crtc_vdisplay,
  610.                               fb->base.bits_per_pixel,
  611.                               cur_size);
  612.  
  613.                 if (cur_size > max_size) {
  614.                         DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
  615.                                       pipe_name(intel_crtc->pipe),
  616.                                       cur_size, max_size);
  617.                         fb = NULL;
  618.                         break;
  619.                 }
  620.  
  621.                 DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
  622.                               pipe_name(intel_crtc->pipe),
  623.                               max_size, cur_size);
  624.         }
  625.  
  626.         if (!fb) {
  627.                 DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
  628.                 goto out;
  629.         }
  630.  
  631.         ifbdev->preferred_bpp = fb->base.bits_per_pixel;
  632.         ifbdev->fb = fb;
  633.  
  634.         drm_framebuffer_reference(&ifbdev->fb->base);
  635.  
  636.         /* Final pass to check if any active pipes don't have fbs */
  637.         for_each_crtc(dev, crtc) {
  638.                 intel_crtc = to_intel_crtc(crtc);
  639.  
  640.                 if (!crtc->state->active)
  641.                         continue;
  642.  
  643.                 WARN(!crtc->primary->fb,
  644.                      "re-used BIOS config but lost an fb on crtc %d\n",
  645.                      crtc->base.id);
  646.         }
  647.  
  648.  
  649.         DRM_DEBUG_KMS("using BIOS fb for initial console\n");
  650.         return true;
  651.  
  652. out:
  653.  
  654.         return false;
  655. }
  656.  
  657. int intel_fbdev_init(struct drm_device *dev)
  658. {
  659.         struct intel_fbdev *ifbdev;
  660.         struct drm_i915_private *dev_priv = dev->dev_private;
  661.         int ret;
  662.  
  663.         if (WARN_ON(INTEL_INFO(dev)->num_pipes == 0))
  664.                 return -ENODEV;
  665.  
  666.         ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
  667.         if (ifbdev == NULL)
  668.                 return -ENOMEM;
  669.  
  670.         drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
  671.  
  672.         if (!intel_fbdev_init_bios(dev, ifbdev))
  673.                 ifbdev->preferred_bpp = 32;
  674.  
  675.         ret = drm_fb_helper_init(dev, &ifbdev->helper,
  676.                                  INTEL_INFO(dev)->num_pipes, 4);
  677.         if (ret) {
  678.                 kfree(ifbdev);
  679.                 return ret;
  680.         }
  681.  
  682.         ifbdev->helper.atomic = true;
  683.  
  684.         dev_priv->fbdev = ifbdev;
  685.         drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
  686.  
  687.         return 0;
  688. }
  689.  
  690. void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
  691. {
  692.         struct drm_i915_private *dev_priv = data;
  693.         struct intel_fbdev *ifbdev = dev_priv->fbdev;
  694.  
  695.         /* Due to peculiar init order wrt to hpd handling this is separate. */
  696.         drm_fb_helper_initial_config(&ifbdev->helper, ifbdev->preferred_bpp);
  697. }
  698.  
  699.  
  700. void intel_fbdev_output_poll_changed(struct drm_device *dev)
  701. {
  702.         struct drm_i915_private *dev_priv = dev->dev_private;
  703.         if (dev_priv->fbdev)
  704.                 drm_fb_helper_hotplug_event(&dev_priv->fbdev->helper);
  705. }
  706.