Subversion Repositories Kolibri OS

Rev

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