Subversion Repositories Kolibri OS

Rev

Rev 5367 | Rev 6088 | 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. struct fb_info *framebuffer_alloc(size_t size, struct device *dev)
  48. {
  49. #define BYTES_PER_LONG (BITS_PER_LONG/8)
  50. #define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG))
  51.     int fb_info_size = sizeof(struct fb_info);
  52.     struct fb_info *info;
  53.     char *p;
  54.  
  55.     if (size)
  56.         fb_info_size += PADDING;
  57.  
  58.     p = kzalloc(fb_info_size + size, GFP_KERNEL);
  59.  
  60.     if (!p)
  61.         return NULL;
  62.  
  63.     info = (struct fb_info *) p;
  64.  
  65.     if (size)
  66.         info->par = p + fb_info_size;
  67.  
  68.     return info;
  69. #undef PADDING
  70. #undef BYTES_PER_LONG
  71. }
  72.  
  73. static int intel_fbdev_set_par(struct fb_info *info)
  74. {
  75.         struct drm_fb_helper *fb_helper = info->par;
  76.         struct intel_fbdev *ifbdev =
  77.                 container_of(fb_helper, struct intel_fbdev, helper);
  78.         int ret;
  79.  
  80.         ret = drm_fb_helper_set_par(info);
  81.  
  82.         if (ret == 0) {
  83.                 mutex_lock(&fb_helper->dev->struct_mutex);
  84.                 intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT);
  85.                 mutex_unlock(&fb_helper->dev->struct_mutex);
  86.         }
  87.  
  88.         return ret;
  89. }
  90.  
  91. static int intel_fbdev_blank(int blank, struct fb_info *info)
  92. {
  93.         struct drm_fb_helper *fb_helper = info->par;
  94.         struct intel_fbdev *ifbdev =
  95.                 container_of(fb_helper, struct intel_fbdev, helper);
  96.         int ret;
  97.  
  98.         ret = drm_fb_helper_blank(blank, info);
  99.  
  100.         if (ret == 0) {
  101.                 mutex_lock(&fb_helper->dev->struct_mutex);
  102.                 intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT);
  103.                 mutex_unlock(&fb_helper->dev->struct_mutex);
  104.         }
  105.  
  106.         return ret;
  107. }
  108.  
  109. static int intel_fbdev_pan_display(struct fb_var_screeninfo *var,
  110.                                    struct fb_info *info)
  111. {
  112.         struct drm_fb_helper *fb_helper = info->par;
  113.         struct intel_fbdev *ifbdev =
  114.                 container_of(fb_helper, struct intel_fbdev, helper);
  115.  
  116.         int ret;
  117.         ret = drm_fb_helper_pan_display(var, info);
  118.  
  119.         if (ret == 0) {
  120.                 mutex_lock(&fb_helper->dev->struct_mutex);
  121.                 intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT);
  122.                 mutex_unlock(&fb_helper->dev->struct_mutex);
  123.         }
  124.  
  125.         return ret;
  126. }
  127.  
  128. static struct fb_ops intelfb_ops = {
  129.         .owner = THIS_MODULE,
  130.         .fb_check_var = drm_fb_helper_check_var,
  131.         .fb_set_par = intel_fbdev_set_par,
  132. //   .fb_fillrect = cfb_fillrect,
  133. //   .fb_copyarea = cfb_copyarea,
  134. //   .fb_imageblit = cfb_imageblit,
  135.         .fb_pan_display = intel_fbdev_pan_display,
  136.         .fb_blank = intel_fbdev_blank,
  137. //   .fb_setcmap = drm_fb_helper_setcmap,
  138. //   .fb_debug_enter = drm_fb_helper_debug_enter,
  139. //   .fb_debug_leave = drm_fb_helper_debug_leave,
  140. };
  141.  
  142. static int intelfb_alloc(struct drm_fb_helper *helper,
  143.                          struct drm_fb_helper_surface_size *sizes)
  144. {
  145.         struct intel_fbdev *ifbdev =
  146.                 container_of(helper, struct intel_fbdev, helper);
  147.         struct drm_framebuffer *fb;
  148.         struct drm_device *dev = helper->dev;
  149.         struct drm_i915_private *dev_priv = to_i915(dev);
  150.         struct drm_mode_fb_cmd2 mode_cmd = {};
  151.         struct drm_i915_gem_object *obj = NULL;
  152.         int size, ret;
  153.  
  154.         /* we don't do packed 24bpp */
  155.         if (sizes->surface_bpp == 24)
  156.                 sizes->surface_bpp = 32;
  157.  
  158.         mode_cmd.width = sizes->surface_width;
  159.         mode_cmd.height = sizes->surface_height;
  160.  
  161.         mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
  162.                                     DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
  163.         mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  164.                                                           sizes->surface_depth);
  165.  
  166.         size = mode_cmd.pitches[0] * mode_cmd.height;
  167.         size = PAGE_ALIGN(size);
  168.         obj = main_fb_obj;
  169.         obj->map_and_fenceable=true;
  170.         if (!obj) {
  171.                 DRM_ERROR("failed to allocate framebuffer\n");
  172.                 ret = -ENOMEM;
  173.                 goto out;
  174.         }
  175.  
  176.         fb = __intel_framebuffer_create(dev, &mode_cmd, obj);
  177.         if (IS_ERR(fb)) {
  178.                 ret = PTR_ERR(fb);
  179.                 goto out_unref;
  180.         }
  181.  
  182.         /* Flush everything out, we'll be doing GTT only from now on */
  183.         ret = intel_pin_and_fence_fb_obj(NULL, fb, NULL, NULL, NULL);
  184.         if (ret) {
  185.                 DRM_ERROR("failed to pin obj: %d\n", ret);
  186.                 goto out_fb;
  187.         }
  188.  
  189.         ifbdev->fb = to_intel_framebuffer(fb);
  190.  
  191.         return 0;
  192.  
  193. out_fb:
  194.         drm_framebuffer_remove(fb);
  195. out_unref:
  196.         drm_gem_object_unreference(&obj->base);
  197. out:
  198.         return ret;
  199. }
  200.  
  201. static int intelfb_create(struct drm_fb_helper *helper,
  202.                           struct drm_fb_helper_surface_size *sizes)
  203. {
  204.         struct intel_fbdev *ifbdev =
  205.                 container_of(helper, struct intel_fbdev, helper);
  206.         struct intel_framebuffer *intel_fb = ifbdev->fb;
  207.         struct drm_device *dev = helper->dev;
  208.         struct drm_i915_private *dev_priv = dev->dev_private;
  209.         struct fb_info *info;
  210.         struct drm_framebuffer *fb;
  211.         struct drm_i915_gem_object *obj;
  212.         int size, ret;
  213.         bool prealloc = false;
  214.  
  215.         mutex_lock(&dev->struct_mutex);
  216.  
  217. #if 0
  218.         if (intel_fb &&
  219.             (sizes->fb_width > intel_fb->base.width ||
  220.              sizes->fb_height > intel_fb->base.height)) {
  221.                 DRM_DEBUG_KMS("BIOS fb too small (%dx%d), we require (%dx%d),"
  222.                               " releasing it\n",
  223.                               intel_fb->base.width, intel_fb->base.height,
  224.                               sizes->fb_width, sizes->fb_height);
  225.                 drm_framebuffer_unreference(&intel_fb->base);
  226.                 intel_fb = ifbdev->fb = NULL;
  227.         }
  228. #endif
  229.  
  230.         if (!intel_fb || WARN_ON(!intel_fb->obj)) {
  231.                 DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
  232.                 ret = intelfb_alloc(helper, sizes);
  233.                 if (ret)
  234.                         goto out_unlock;
  235.                 intel_fb = ifbdev->fb;
  236.         } else {
  237.                 DRM_DEBUG_KMS("re-using BIOS fb\n");
  238.                 prealloc = true;
  239.                 sizes->fb_width = intel_fb->base.width;
  240.                 sizes->fb_height = intel_fb->base.height;
  241.         }
  242.  
  243.         obj = intel_fb->obj;
  244.         size = obj->base.size;
  245.  
  246.         info = drm_fb_helper_alloc_fbi(helper);
  247.         if (IS_ERR(info)) {
  248.                 ret = PTR_ERR(info);
  249.                 goto out_unpin;
  250.         }
  251.  
  252.         info->par = helper;
  253.  
  254.         fb = &ifbdev->fb->base;
  255.  
  256.     if(main_framebuffer == NULL)
  257.         main_framebuffer = fb;
  258.  
  259.         ifbdev->helper.fb = fb;
  260.  
  261.         strcpy(info->fix.id, "inteldrmfb");
  262.  
  263.         info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
  264.         info->fbops = &intelfb_ops;
  265.  
  266.         /* setup aperture base/size for vesafb takeover */
  267.         info->apertures->ranges[0].base = dev->mode_config.fb_base;
  268.         info->apertures->ranges[0].size = dev_priv->gtt.mappable_end;
  269.  
  270.  
  271.         info->screen_base = (void*) 0xFE000000;
  272.         info->screen_size = size;
  273.  
  274.         /* This driver doesn't need a VT switch to restore the mode on resume */
  275.         info->skip_vt_switch = true;
  276.  
  277.         drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  278.         drm_fb_helper_fill_var(info, &ifbdev->helper, sizes->fb_width, sizes->fb_height);
  279.  
  280.         /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
  281.  
  282.         DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08llx, bo %p\n",
  283.                       fb->width, fb->height,
  284.                       i915_gem_obj_ggtt_offset(obj), obj);
  285.  
  286.         mutex_unlock(&dev->struct_mutex);
  287.         return 0;
  288.  
  289. out_destroy_fbi:
  290.         drm_fb_helper_release_fbi(helper);
  291. out_unpin:
  292.         i915_gem_object_ggtt_unpin(obj);
  293.         drm_gem_object_unreference(&obj->base);
  294. out_unlock:
  295.         mutex_unlock(&dev->struct_mutex);
  296.         return ret;
  297. }
  298.  
  299. /** Sets the color ramps on behalf of RandR */
  300. static void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  301.                                     u16 blue, int regno)
  302. {
  303.         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  304.  
  305.         intel_crtc->lut_r[regno] = red >> 8;
  306.         intel_crtc->lut_g[regno] = green >> 8;
  307.         intel_crtc->lut_b[regno] = blue >> 8;
  308. }
  309.  
  310. static void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  311.                                     u16 *blue, int regno)
  312. {
  313.         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  314.  
  315.         *red = intel_crtc->lut_r[regno] << 8;
  316.         *green = intel_crtc->lut_g[regno] << 8;
  317.         *blue = intel_crtc->lut_b[regno] << 8;
  318. }
  319.  
  320. static struct drm_fb_helper_crtc *
  321. intel_fb_helper_crtc(struct drm_fb_helper *fb_helper, struct drm_crtc *crtc)
  322. {
  323.         int i;
  324.  
  325.         for (i = 0; i < fb_helper->crtc_count; i++)
  326.                 if (fb_helper->crtc_info[i].mode_set.crtc == crtc)
  327.                         return &fb_helper->crtc_info[i];
  328.  
  329.         return NULL;
  330. }
  331.  
  332. /*
  333.  * Try to read the BIOS display configuration and use it for the initial
  334.  * fb configuration.
  335.  *
  336.  * The BIOS or boot loader will generally create an initial display
  337.  * configuration for us that includes some set of active pipes and displays.
  338.  * This routine tries to figure out which pipes and connectors are active
  339.  * and stuffs them into the crtcs and modes array given to us by the
  340.  * drm_fb_helper code.
  341.  *
  342.  * The overall sequence is:
  343.  *   intel_fbdev_init - from driver load
  344.  *     intel_fbdev_init_bios - initialize the intel_fbdev using BIOS data
  345.  *     drm_fb_helper_init - build fb helper structs
  346.  *     drm_fb_helper_single_add_all_connectors - more fb helper structs
  347.  *   intel_fbdev_initial_config - apply the config
  348.  *     drm_fb_helper_initial_config - call ->probe then register_framebuffer()
  349.  *         drm_setup_crtcs - build crtc config for fbdev
  350.  *           intel_fb_initial_config - find active connectors etc
  351.  *         drm_fb_helper_single_fb_probe - set up fbdev
  352.  *           intelfb_create - re-use or alloc fb, build out fbdev structs
  353.  *
  354.  * Note that we don't make special consideration whether we could actually
  355.  * switch to the selected modes without a full modeset. E.g. when the display
  356.  * is in VGA mode we need to recalculate watermarks and set a new high-res
  357.  * framebuffer anyway.
  358.  */
  359. static bool intel_fb_initial_config(struct drm_fb_helper *fb_helper,
  360.                                     struct drm_fb_helper_crtc **crtcs,
  361.                                     struct drm_display_mode **modes,
  362.                                     struct drm_fb_offset *offsets,
  363.                                     bool *enabled, int width, int height)
  364. {
  365.         struct drm_device *dev = fb_helper->dev;
  366.         int i, j;
  367.         bool *save_enabled;
  368.         bool fallback = true;
  369.         int num_connectors_enabled = 0;
  370.         int num_connectors_detected = 0;
  371.         uint64_t conn_configured = 0, mask;
  372.         int pass = 0;
  373.  
  374.         save_enabled = kcalloc(dev->mode_config.num_connector, sizeof(bool),
  375.                                GFP_KERNEL);
  376.         if (!save_enabled)
  377.                 return false;
  378.  
  379.         memcpy(save_enabled, enabled, dev->mode_config.num_connector);
  380.         mask = (1 << fb_helper->connector_count) - 1;
  381. retry:
  382.         for (i = 0; i < fb_helper->connector_count; i++) {
  383.                 struct drm_fb_helper_connector *fb_conn;
  384.                 struct drm_connector *connector;
  385.                 struct drm_encoder *encoder;
  386.                 struct drm_fb_helper_crtc *new_crtc;
  387.  
  388.                 fb_conn = fb_helper->connector_info[i];
  389.                 connector = fb_conn->connector;
  390.  
  391.                 if (conn_configured & (1 << i))
  392.                         continue;
  393.  
  394.                 if (pass == 0 && !connector->has_tile)
  395.                         continue;
  396.  
  397.                 if (connector->status == connector_status_connected)
  398.                         num_connectors_detected++;
  399.  
  400.                 if (!enabled[i]) {
  401.                         DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
  402.                                       connector->name);
  403.                         conn_configured |= (1 << i);
  404.                         continue;
  405.                 }
  406.  
  407.                 if (connector->force == DRM_FORCE_OFF) {
  408.                         DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
  409.                                       connector->name);
  410.                         enabled[i] = false;
  411.                         continue;
  412.                 }
  413.  
  414.                 encoder = connector->encoder;
  415.                 if (!encoder || WARN_ON(!encoder->crtc)) {
  416.                         if (connector->force > DRM_FORCE_OFF)
  417.                                 goto bail;
  418.  
  419.                         DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
  420.                                       connector->name);
  421.                         enabled[i] = false;
  422.                         conn_configured |= (1 << i);
  423.                         continue;
  424.                 }
  425.  
  426.                 num_connectors_enabled++;
  427.  
  428.                 new_crtc = intel_fb_helper_crtc(fb_helper, encoder->crtc);
  429.  
  430.                 /*
  431.                  * Make sure we're not trying to drive multiple connectors
  432.                  * with a single CRTC, since our cloning support may not
  433.                  * match the BIOS.
  434.                  */
  435.                 for (j = 0; j < fb_helper->connector_count; j++) {
  436.                         if (crtcs[j] == new_crtc) {
  437.                                 DRM_DEBUG_KMS("fallback: cloned configuration\n");
  438.                                 goto bail;
  439.                         }
  440.                 }
  441.  
  442.                 DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
  443.                               connector->name);
  444.  
  445.                 /* go for command line mode first */
  446.                 modes[i] = drm_pick_cmdline_mode(fb_conn, width, height);
  447.  
  448.                 /* try for preferred next */
  449.                 if (!modes[i]) {
  450.                         DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
  451.                                       connector->name, connector->has_tile);
  452.                         modes[i] = drm_has_preferred_mode(fb_conn, width,
  453.                                                           height);
  454.                 }
  455.  
  456.                 /* No preferred mode marked by the EDID? Are there any modes? */
  457.                 if (!modes[i] && !list_empty(&connector->modes)) {
  458.                         DRM_DEBUG_KMS("using first mode listed on connector %s\n",
  459.                                       connector->name);
  460.                         modes[i] = list_first_entry(&connector->modes,
  461.                                                     struct drm_display_mode,
  462.                                                     head);
  463.                 }
  464.  
  465.                 /* last resort: use current mode */
  466.                 if (!modes[i]) {
  467.                         /*
  468.                          * IMPORTANT: We want to use the adjusted mode (i.e.
  469.                          * after the panel fitter upscaling) as the initial
  470.                          * config, not the input mode, which is what crtc->mode
  471.                          * usually contains. But since our current
  472.                          * code puts a mode derived from the post-pfit timings
  473.                          * into crtc->mode this works out correctly.
  474.                          */
  475.                         DRM_DEBUG_KMS("looking for current mode on connector %s\n",
  476.                                       connector->name);
  477.                         modes[i] = &encoder->crtc->mode;
  478.                 }
  479.                 crtcs[i] = new_crtc;
  480.  
  481.                 DRM_DEBUG_KMS("connector %s on pipe %c [CRTC:%d]: %dx%d%s\n",
  482.                               connector->name,
  483.                               pipe_name(to_intel_crtc(encoder->crtc)->pipe),
  484.                               encoder->crtc->base.id,
  485.                               modes[i]->hdisplay, modes[i]->vdisplay,
  486.                               modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" :"");
  487.  
  488.                 fallback = false;
  489.                 conn_configured |= (1 << i);
  490.         }
  491.  
  492.         if ((conn_configured & mask) != mask) {
  493.                 pass++;
  494.                 goto retry;
  495.         }
  496.  
  497.         /*
  498.          * If the BIOS didn't enable everything it could, fall back to have the
  499.          * same user experiencing of lighting up as much as possible like the
  500.          * fbdev helper library.
  501.          */
  502.         if (num_connectors_enabled != num_connectors_detected &&
  503.             num_connectors_enabled < INTEL_INFO(dev)->num_pipes) {
  504.                 DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
  505.                 DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled,
  506.                               num_connectors_detected);
  507.                 fallback = true;
  508.         }
  509.  
  510.         if (fallback) {
  511. bail:
  512.                 DRM_DEBUG_KMS("Not using firmware configuration\n");
  513.                 memcpy(enabled, save_enabled, dev->mode_config.num_connector);
  514.                 kfree(save_enabled);
  515.                 return false;
  516.         }
  517.  
  518.         kfree(save_enabled);
  519.         return true;
  520. }
  521.  
  522. static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
  523.         .initial_config = intel_fb_initial_config,
  524.         .gamma_set = intel_crtc_fb_gamma_set,
  525.         .gamma_get = intel_crtc_fb_gamma_get,
  526.         .fb_probe = intelfb_create,
  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.