1494N/A/*
1494N/A * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
1494N/A */
1494N/A
1494N/A/*
1494N/A * Copyright (c) 2006-2009 Red Hat Inc.
1494N/A * Copyright (c) 2006-2008, 2013, Intel Corporation
1494N/A * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
1494N/A *
1494N/A * DRM framebuffer helper functions
1494N/A *
1494N/A * Permission to use, copy, modify, distribute, and sell this software and its
1494N/A * documentation for any purpose is hereby granted without fee, provided that
1494N/A * the above copyright notice appear in all copies and that both that copyright
1494N/A * notice and this permission notice appear in supporting documentation, and
1494N/A * that the name of the copyright holders not be used in advertising or
1494N/A * publicity pertaining to distribution of the software without specific,
1494N/A * written prior permission. The copyright holders make no representations
1494N/A * about the suitability of this software for any purpose. It is provided "as
1494N/A * is" without express or implied warranty.
1494N/A *
1494N/A * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
1494N/A * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
1494N/A * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
1494N/A * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
1494N/A * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
1494N/A * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
1494N/A * OF THIS SOFTWARE.
1494N/A *
1494N/A * Authors:
1494N/A * Dave Airlie <airlied@linux.ie>
1494N/A * Jesse Barnes <jesse.barnes@intel.com>
1494N/A */
1494N/A#include "drmP.h"
1494N/A#include "drm_crtc.h"
1494N/A#include "drm_fb_helper.h"
1494N/A#include "drm_crtc_helper.h"
1494N/A
1494N/A
1494N/Astruct list_head kernel_fb_helper_list;
1494N/A/**
1494N/A * DOC: fbdev helpers
1494N/A *
1494N/A * The fb helper functions are useful to provide an fbdev on top of a drm kernel
1494N/A * mode setting driver. They can be used mostly independantely from the crtc
1494N/A * helper functions used by many drivers to implement the kernel mode setting
1494N/A * interfaces.
1494N/A *
1494N/A * Initialization is done as a three-step process with drm_fb_helper_init(),
1494N/A * drm_fb_helper_single_add_all_connectors() and drm_fb_helper_initial_config().
1494N/A * Drivers with fancier requirements than the default beheviour can override the
1494N/A * second step with their own code. Teardown is done with drm_fb_helper_fini().
1494N/A *
1494N/A * At runtime drivers should restore the fbdev console by calling
1494N/A * drm_fb_helper_restore_fbdev_mode() from their ->lastclose callback. They
1494N/A * should also notify the fb helper code from updates to the output
1494N/A * configuration by calling drm_fb_helper_hotplug_event(). For easier
1494N/A * integration with the output polling code in drm_crtc_helper.c the modeset
1494N/A * code proves a ->output_poll_changed callback.
1494N/A *
1494N/A * All other functions exported by the fb helper library can be used to
1494N/A * implement the fbdev driver interface by the driver.
1494N/A */
1494N/A
1494N/A/* simple single crtc case helper function
1494N/A * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
1494N/A * emulation helper
1494N/A * @fb_helper: fbdev initialized with drm_fb_helper_init
1494N/A *
1494N/A * This functions adds all the available connectors for use with the given
1494N/A * fb_helper. This is a separate step to allow drivers to freely assign
1494N/A * connectors to the fbdev, e.g. if some are reserved for special purposes or
1494N/A * not adequate to be used for the fbcon.
1494N/A *
1494N/A * Since this is part of the initial setup before the fbdev is published, no
1494N/A * locking is required.
1494N/A */
1494N/Aint drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
1494N/A{
1494N/A struct drm_device *dev = fb_helper->dev;
1494N/A struct drm_connector *connector;
1494N/A int i;
1494N/A
1494N/A list_for_each_entry(connector, struct drm_connector, &dev->mode_config.connector_list, head) {
1494N/A struct drm_fb_helper_connector *fb_helper_connector;
1494N/A
1494N/A fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
1494N/A if (!fb_helper_connector)
1494N/A goto fail;
1494N/A
1494N/A fb_helper_connector->connector = connector;
1494N/A fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
1494N/A }
1494N/A return 0;
1494N/Afail:
1494N/A for (i = 0; i < fb_helper->connector_count; i++) {
1494N/A kfree(fb_helper->connector_info[i], sizeof(struct drm_fb_helper_connector));
1494N/A fb_helper->connector_info[i] = NULL;
1494N/A }
1494N/A fb_helper->connector_count = 0;
1494N/A return -ENOMEM;
1494N/A}
1494N/A
1494N/Astatic int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
1494N/A{
1494N/A struct drm_device *dev = fb_helper->dev;
1494N/A struct drm_fb_helper_connector *fb_helper_conn;
1494N/A struct gfxp_bm_fb_info fb_info;
1494N/A int i;
1494N/A
1494N/A gfxp_bm_getfb_info(dev->vgatext->private, &fb_info);
1494N/A for (i = 0; i < fb_helper->connector_count; i++) {
1494N/A
1494N/A fb_helper_conn = fb_helper->connector_info[i];
1494N/A
1494N/A struct drm_cmdline_mode *cmdline_mode;
1494N/A cmdline_mode = &fb_helper_conn->cmdline_mode;
1494N/A cmdline_mode->specified = true;
1494N/A cmdline_mode->xres = fb_info.xres;
1494N/A cmdline_mode->yres = fb_info.yres;
1494N/A cmdline_mode->refresh_specified = true;
1494N/A cmdline_mode->refresh = 60;
1494N/A cmdline_mode->bpp_specified = true;
1494N/A cmdline_mode->bpp = fb_info.depth;
1494N/A }
1494N/A return 0;
1494N/A}
1494N/A
1494N/Astatic void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
1494N/A{
1494N/A uint16_t *r_base, *g_base, *b_base;
1494N/A int i;
1494N/A
1494N/A if (helper->funcs->gamma_get == NULL)
1494N/A return;
1494N/A
1494N/A r_base = crtc->gamma_store;
1494N/A g_base = r_base + crtc->gamma_size;
1494N/A b_base = g_base + crtc->gamma_size;
1494N/A
1494N/A for (i = 0; i < crtc->gamma_size; i++)
1494N/A helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
1494N/A}
1494N/A
1494N/Astatic void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
1494N/A{
1494N/A uint16_t *r_base, *g_base, *b_base;
1494N/A
1494N/A if (crtc->funcs->gamma_set == NULL)
1494N/A return;
1494N/A
1494N/A r_base = crtc->gamma_store;
1494N/A g_base = r_base + crtc->gamma_size;
1494N/A b_base = g_base + crtc->gamma_size;
1494N/A
1494N/A crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
1494N/A}
1494N/A
1494N/A/* Find the real fb for a given fb helper CRTC */
1494N/Astatic struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
1494N/A{
1494N/A struct drm_device *dev = crtc->dev;
1494N/A struct drm_crtc *c;
1494N/A
1494N/A list_for_each_entry(c, struct drm_crtc, &dev->mode_config.crtc_list, head) {
1494N/A if (crtc->base.id == c->base.id)
1494N/A return c->fb;
1494N/A }
1494N/A
1494N/A return NULL;
1494N/A}
1494N/A
1494N/Abool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
1494N/A{
1494N/A struct drm_device *dev;
1494N/A struct drm_plane *plane;
1494N/A bool error = false;
1494N/A int i;
1494N/A
1494N/A if (fb_helper == NULL)
1494N/A return error;
1494N/A
1494N/A dev = fb_helper->dev;
1494N/A
1494N/A list_for_each_entry(plane, struct drm_plane, &dev->mode_config.plane_list, head)
1494N/A drm_plane_force_disable(plane);
1494N/A
1494N/A for (i = 0; i < fb_helper->crtc_count; i++) {
1494N/A struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
1494N/A struct drm_crtc *crtc = mode_set->crtc;
1494N/A int ret;
1494N/A
1494N/A if (crtc->funcs->cursor_set) {
1494N/A ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
1494N/A if (ret)
1494N/A error = true;
1494N/A }
1494N/A
1494N/A ret = drm_mode_set_config_internal(mode_set);
1494N/A if (ret)
1494N/A error = true;
1494N/A }
1494N/A return error;
1494N/A}
1494N/A
1494N/Abool drm_fb_helper_force_kernel_mode(void)
1494N/A{
1494N/A bool ret, error = false;
1494N/A struct drm_fb_helper *helper;
1494N/A
1494N/A if (list_empty(&kernel_fb_helper_list))
1494N/A return false;
1494N/A
1494N/A list_for_each_entry(helper, struct drm_fb_helper, &kernel_fb_helper_list, kernel_fb_list) {
1494N/A if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1494N/A continue;
1494N/A
1494N/A ret = drm_fb_helper_restore_fbdev_mode(helper);
1494N/A if (ret)
1494N/A error = true;
1494N/A }
1494N/A return error;
1494N/A}
1494N/A
1494N/Astatic bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
1494N/A{
1494N/A struct drm_device *dev = fb_helper->dev;
1494N/A struct drm_crtc *crtc;
1494N/A int bound = 0, crtcs_bound = 0;
1494N/A
1494N/A list_for_each_entry(crtc, struct drm_crtc, &dev->mode_config.crtc_list, head) {
1494N/A if (crtc->fb)
1494N/A crtcs_bound++;
1494N/A if (crtc->fb == fb_helper->fb)
1494N/A bound++;
1494N/A }
1494N/A
1494N/A if (bound < crtcs_bound)
1494N/A return false;
1494N/A return true;
1494N/A}
1494N/A
1494N/A/* maximum connectors per crtcs in the mode set */
1494N/A#define INTELFB_CONN_LIMIT 4
1494N/A
1494N/Astatic void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
1494N/A{
1494N/A int i;
1494N/A struct drm_device *dev = helper->dev;
1494N/A
1494N/A kfree(helper->connector_info, dev->mode_config.num_connector * sizeof(struct drm_fb_helper_connector *));
1494N/A for (i = 0; i < helper->crtc_count; i++) {
1494N/A kfree(helper->crtc_info[i].mode_set.connectors, INTELFB_CONN_LIMIT * sizeof(struct drm_connector *));
1494N/A if (helper->crtc_info[i].mode_set.mode)
1494N/A drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
1494N/A }
1494N/A kfree(helper->crtc_info, helper->crtc_count * sizeof(struct drm_fb_helper_crtc));
1494N/A}
1494N/A
1494N/Aint drm_fb_helper_init(struct drm_device *dev,
1494N/A struct drm_fb_helper *fb_helper,
1494N/A int crtc_count, int max_conn_count)
1494N/A{
1494N/A struct drm_crtc *crtc;
1494N/A int i;
1494N/A
1494N/A fb_helper->dev = dev;
1494N/A
1494N/A INIT_LIST_HEAD(&kernel_fb_helper_list);
1494N/A INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
1494N/A
1494N/A fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
1494N/A if (!fb_helper->crtc_info)
1494N/A return -ENOMEM;
1494N/A
1494N/A fb_helper->crtc_count = crtc_count;
1494N/A fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
1494N/A if (!fb_helper->connector_info) {
1494N/A kfree(fb_helper->crtc_info, sizeof(struct drm_fb_helper_crtc));
1494N/A return -ENOMEM;
1494N/A }
1494N/A fb_helper->connector_count = 0;
1494N/A
1494N/A for (i = 0; i < crtc_count; i++) {
1494N/A fb_helper->crtc_info[i].mode_set.connectors =
1494N/A kcalloc(max_conn_count,
1494N/A sizeof(struct drm_connector *),
1494N/A GFP_KERNEL);
1494N/A
1494N/A if (!fb_helper->crtc_info[i].mode_set.connectors)
1494N/A goto out_free;
1494N/A fb_helper->crtc_info[i].mode_set.num_connectors = 0;
1494N/A }
1494N/A
1494N/A i = 0;
1494N/A list_for_each_entry(crtc, struct drm_crtc, &dev->mode_config.crtc_list, head) {
1494N/A fb_helper->crtc_info[i].mode_set.crtc = crtc;
1494N/A i++;
1494N/A }
1494N/A
1494N/A return 0;
1494N/Aout_free:
1494N/A drm_fb_helper_crtc_free(fb_helper);
1494N/A return -ENOMEM;
1494N/A}
1494N/A
1494N/Avoid drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
1494N/A{
1494N/A if (!list_empty(&fb_helper->kernel_fb_list)) {
1494N/A list_del(&fb_helper->kernel_fb_list);
1494N/A if (list_empty(&kernel_fb_helper_list)) {
1494N/A DRM_INFO("drm: unregistered panic notifier");
1494N/A }
1494N/A }
1494N/A
1494N/A drm_fb_helper_crtc_free(fb_helper);
1494N/A
1494N/A}
1494N/A
1494N/Aint drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
1494N/A int preferred_bpp)
1494N/A{
1494N/A int ret = 0;
1494N/A int crtc_count = 0;
1494N/A int i;
1494N/A struct drm_fb_helper_surface_size sizes;
1494N/A int gamma_size = 0;
1494N/A
1494N/A (void) memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
1494N/A sizes.surface_depth = 24;
1494N/A sizes.surface_bpp = 32;
1494N/A sizes.fb_width = (unsigned)-1;
1494N/A sizes.fb_height = (unsigned)-1;
1494N/A
1494N/A /* if driver picks 8 or 16 by default use that
1494N/A for both depth/bpp */
1494N/A if (preferred_bpp != sizes.surface_bpp)
1494N/A sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
1494N/A
1494N/A /* first up get a count of crtcs now in use and new min/maxes width/heights */
1494N/A for (i = 0; i < fb_helper->connector_count; i++) {
1494N/A struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
1494N/A struct drm_cmdline_mode *cmdline_mode;
1494N/A
1494N/A cmdline_mode = &fb_helper_conn->cmdline_mode;
1494N/A
1494N/A if (cmdline_mode->bpp_specified) {
1494N/A switch (cmdline_mode->bpp) {
1494N/A case 8:
1494N/A sizes.surface_depth = sizes.surface_bpp = 8;
1494N/A break;
1494N/A case 15:
1494N/A sizes.surface_depth = 15;
1494N/A sizes.surface_bpp = 16;
1494N/A break;
1494N/A case 16:
1494N/A sizes.surface_depth = sizes.surface_bpp = 16;
1494N/A break;
1494N/A case 24:
1494N/A sizes.surface_depth = sizes.surface_bpp = 24;
1494N/A break;
1494N/A case 32:
1494N/A sizes.surface_depth = 24;
1494N/A sizes.surface_bpp = 32;
1494N/A break;
1494N/A }
1494N/A break;
1494N/A }
1494N/A }
1494N/A
1494N/A crtc_count = 0;
1494N/A for (i = 0; i < fb_helper->crtc_count; i++) {
1494N/A struct drm_display_mode *desired_mode;
1494N/A desired_mode = fb_helper->crtc_info[i].desired_mode;
1494N/A
1494N/A if (desired_mode) {
1494N/A if (gamma_size == 0)
1494N/A gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
1494N/A if (desired_mode->hdisplay < sizes.fb_width)
1494N/A sizes.fb_width = desired_mode->hdisplay;
1494N/A if (desired_mode->vdisplay < sizes.fb_height)
1494N/A sizes.fb_height = desired_mode->vdisplay;
1494N/A if (desired_mode->hdisplay > sizes.surface_width)
1494N/A sizes.surface_width = desired_mode->hdisplay;
1494N/A if (desired_mode->vdisplay > sizes.surface_height)
1494N/A sizes.surface_height = desired_mode->vdisplay;
1494N/A crtc_count++;
1494N/A }
1494N/A }
1494N/A
1494N/A if (crtc_count == 0 || sizes.fb_width == (unsigned)-1 || sizes.fb_height == (unsigned)-1) {
1494N/A /* hmm everyone went away - assume VGA cable just fell out
1494N/A and will come back later. */
1494N/A DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
1494N/A sizes.fb_width = sizes.surface_width = 1024;
1494N/A sizes.fb_height = sizes.surface_height = 768;
1494N/A }
1494N/A
1494N/A /* push down into drivers */
1494N/A ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
1494N/A if (ret < 0)
1494N/A return ret;
1494N/A
1494N/A /*
1494N/A * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
1494N/A * events, but at init time drm_setup_crtcs needs to be called before
1494N/A * the fb is allocated (since we need to figure out the desired size of
1494N/A * the fb before we can allocate it ...). Hence we need to fix things up
1494N/A * here again.
1494N/A */
1494N/A for (i = 0; i < fb_helper->crtc_count; i++)
1494N/A if (fb_helper->crtc_info[i].mode_set.num_connectors)
1494N/A fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
1494N/A
1494N/A
1494N/A /* Switch back to kernel console on panic */
1494N/A /* multi card linked list maybe */
1494N/A if (list_empty(&kernel_fb_helper_list)) {
1494N/A DRM_INFO("registered panic notifier");
1494N/A }
1494N/A
1494N/A list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list, (caddr_t)fb_helper);
1494N/A
1494N/A return 0;
1494N/A}
1494N/A
1494N/Astatic int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
1494N/A uint32_t maxX,
1494N/A uint32_t maxY)
1494N/A{
1494N/A struct drm_connector *connector;
1494N/A int count = 0;
1494N/A int i;
1494N/A
1494N/A for (i = 0; i < fb_helper->connector_count; i++) {
1494N/A connector = fb_helper->connector_info[i]->connector;
1494N/A count += connector->funcs->fill_modes(connector, maxX, maxY);
1494N/A }
1494N/A
1494N/A return count;
1494N/A}
1494N/A
1494N/Astatic struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
1494N/A{
1494N/A struct drm_display_mode *mode;
1494N/A
1494N/A list_for_each_entry(mode, struct drm_display_mode, &fb_connector->connector->modes, head) {
1494N/A if (drm_mode_width(mode) > width ||
1494N/A drm_mode_height(mode) > height)
1494N/A continue;
1494N/A if (mode->type & DRM_MODE_TYPE_PREFERRED)
1494N/A return mode;
1494N/A }
1494N/A return NULL;
1494N/A}
1494N/A
1494N/Astatic bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
1494N/A{
1494N/A struct drm_cmdline_mode *cmdline_mode;
1494N/A cmdline_mode = &fb_connector->cmdline_mode;
1494N/A return cmdline_mode->specified;
1494N/A}
1494N/A
1494N/Astatic struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
1494N/A /* LINTED E_FUNC_ARG_UNUSED */
1494N/A int width, int height)
1494N/A{
1494N/A struct drm_cmdline_mode *cmdline_mode;
1494N/A struct drm_display_mode *mode = NULL;
1494N/A
1494N/A cmdline_mode = &fb_helper_conn->cmdline_mode;
1494N/A if (cmdline_mode->specified == false)
1494N/A return mode;
1494N/A
1494N/A /* attempt to find a matching mode in the list of modes
1494N/A * we have gotten so far, if not add a CVT mode that conforms
1494N/A */
1494N/A if (cmdline_mode->rb || cmdline_mode->margins)
1494N/A goto create_mode;
1494N/A
1494N/A list_for_each_entry(mode, struct drm_display_mode, &fb_helper_conn->connector->modes, head) {
1494N/A /* check width/height */
1494N/A if (mode->hdisplay != cmdline_mode->xres ||
1494N/A mode->vdisplay != cmdline_mode->yres)
1494N/A continue;
1494N/A
1494N/A if (cmdline_mode->refresh_specified) {
1494N/A if (mode->vrefresh != cmdline_mode->refresh)
1494N/A continue;
1494N/A }
1494N/A
1494N/A if (cmdline_mode->interlace) {
1494N/A if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
1494N/A continue;
1494N/A }
1494N/A return mode;
1494N/A }
1494N/A
1494N/Acreate_mode:
1494N/A mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
1494N/A cmdline_mode);
1494N/A if (mode)
1494N/A list_add(&mode->head, &fb_helper_conn->connector->modes,
1494N/A (caddr_t)mode);
1494N/A return mode;
1494N/A}
1494N/A
1494N/Astatic bool drm_connector_enabled(struct drm_connector *connector, bool strict)
1494N/A{
1494N/A bool enable;
1494N/A
1494N/A if (strict)
1494N/A enable = connector->status == connector_status_connected;
1494N/A else
1494N/A enable = connector->status != connector_status_disconnected;
1494N/A
1494N/A return enable;
1494N/A}
1494N/A
1494N/Astatic void drm_enable_connectors(struct drm_fb_helper *fb_helper,
1494N/A bool *enabled)
1494N/A{
1494N/A bool any_enabled = false;
1494N/A struct drm_connector *connector;
1494N/A int i = 0;
1494N/A
1494N/A for (i = 0; i < fb_helper->connector_count; i++) {
1494N/A connector = fb_helper->connector_info[i]->connector;
1494N/A enabled[i] = drm_connector_enabled(connector, true);
1494N/A DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
1494N/A enabled[i] ? "yes" : "no");
1494N/A any_enabled |= enabled[i];
1494N/A }
1494N/A
1494N/A if (any_enabled)
1494N/A return;
1494N/A
1494N/A for (i = 0; i < fb_helper->connector_count; i++) {
1494N/A connector = fb_helper->connector_info[i]->connector;
1494N/A enabled[i] = drm_connector_enabled(connector, false);
1494N/A }
1494N/A}
1494N/A
1494N/Astatic bool drm_target_cloned(struct drm_fb_helper *fb_helper,
1494N/A struct drm_display_mode **modes,
1494N/A bool *enabled, int width, int height)
1494N/A{
1494N/A int count, i, j;
1494N/A bool can_clone = false;
1494N/A struct drm_fb_helper_connector *fb_helper_conn;
1494N/A struct drm_display_mode *dmt_mode, *mode;
1494N/A
1494N/A /* only contemplate cloning in the single crtc case */
1494N/A if (fb_helper->crtc_count > 1)
1494N/A return false;
1494N/A
1494N/A count = 0;
1494N/A for (i = 0; i < fb_helper->connector_count; i++) {
1494N/A if (enabled[i])
1494N/A count++;
1494N/A }
1494N/A
1494N/A /* only contemplate cloning if more than one connector is enabled */
1494N/A if (count <= 1)
1494N/A return false;
1494N/A
1494N/A /* check the command line or if nothing common pick 1024x768 */
1494N/A can_clone = true;
1494N/A for (i = 0; i < fb_helper->connector_count; i++) {
1494N/A if (!enabled[i])
1494N/A continue;
1494N/A fb_helper_conn = fb_helper->connector_info[i];
1494N/A modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1494N/A if (!modes[i]) {
1494N/A can_clone = false;
1494N/A break;
1494N/A }
1494N/A for (j = 0; j < i; j++) {
1494N/A if (!enabled[j])
1494N/A continue;
1494N/A if (!drm_mode_equal(modes[j], modes[i]))
1494N/A can_clone = false;
1494N/A }
1494N/A }
1494N/A
1494N/A if (can_clone) {
1494N/A DRM_DEBUG_KMS("can clone using command line\n");
1494N/A return true;
1494N/A }
1494N/A
1494N/A /* try and find a 1024x768 mode on each connector */
1494N/A can_clone = true;
1494N/A dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
1494N/A
1494N/A for (i = 0; i < fb_helper->connector_count; i++) {
1494N/A
1494N/A if (!enabled[i])
1494N/A continue;
1494N/A
1494N/A fb_helper_conn = fb_helper->connector_info[i];
1494N/A list_for_each_entry(mode, struct drm_display_mode, &fb_helper_conn->connector->modes, head) {
1494N/A if (drm_mode_equal(mode, dmt_mode))
1494N/A modes[i] = mode;
1494N/A }
1494N/A if (!modes[i])
1494N/A can_clone = false;
1494N/A }
1494N/A
1494N/A if (can_clone) {
1494N/A DRM_DEBUG_KMS("can clone using 1024x768\n");
1494N/A return true;
1494N/A }
1494N/A DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
1494N/A return false;
1494N/A}
1494N/A
1494N/Astatic bool drm_target_preferred(struct drm_fb_helper *fb_helper,
1494N/A struct drm_display_mode **modes,
1494N/A bool *enabled, int width, int height)
1494N/A{
1494N/A struct drm_fb_helper_connector *fb_helper_conn;
1494N/A int i;
1494N/A
1494N/A for (i = 0; i < fb_helper->connector_count; i++) {
1494N/A fb_helper_conn = fb_helper->connector_info[i];
1494N/A
1494N/A if (enabled[i] == false)
1494N/A continue;
1494N/A
1494N/A DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
1494N/A fb_helper_conn->connector->base.id);
1494N/A
1494N/A /* got for command line mode first */
1494N/A modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
1494N/A if (!modes[i]) {
1494N/A DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
1494N/A fb_helper_conn->connector->base.id);
1494N/A modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
1494N/A }
1494N/A /* No preferred modes, pick one off the list */
1494N/A if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
1494N/A list_for_each_entry(modes[i], struct drm_display_mode, &fb_helper_conn->connector->modes, head)
1494N/A /* LINTED */
1494N/A { if (1) break; }
1494N/A }
1494N/A DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
1494N/A "none");
1494N/A }
1494N/A return true;
1494N/A}
1494N/A
1494N/Astatic int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
1494N/A struct drm_fb_helper_crtc **best_crtcs,
1494N/A struct drm_display_mode **modes,
1494N/A int n, int width, int height)
1494N/A{
1494N/A int c, o;
1494N/A struct drm_device *dev = fb_helper->dev;
1494N/A struct drm_connector *connector;
1494N/A struct drm_connector_helper_funcs *connector_funcs;
1494N/A struct drm_encoder *encoder;
1494N/A int my_score, best_score, score;
1494N/A struct drm_fb_helper_crtc **crtcs, *crtc;
1494N/A struct drm_fb_helper_connector *fb_helper_conn;
1494N/A int num_connector = dev->mode_config.num_connector;
1494N/A
1494N/A if (n == fb_helper->connector_count)
1494N/A return 0;
1494N/A
1494N/A ASSERT(n <= num_connector);
1494N/A
1494N/A fb_helper_conn = fb_helper->connector_info[n];
1494N/A connector = fb_helper_conn->connector;
1494N/A
1494N/A best_crtcs[n] = NULL;
1494N/A best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
1494N/A if (modes[n] == NULL)
1494N/A return best_score;
1494N/A
1494N/A crtcs = kzalloc(num_connector * sizeof(struct drm_fb_helper_crtc *),
1494N/A GFP_KERNEL);
1494N/A if (!crtcs)
1494N/A return best_score;
1494N/A
1494N/A my_score = 1;
1494N/A if (connector->status == connector_status_connected)
1494N/A my_score++;
1494N/A if (drm_has_cmdline_mode(fb_helper_conn))
1494N/A my_score++;
1494N/A if (drm_has_preferred_mode(fb_helper_conn, width, height))
1494N/A my_score++;
1494N/A
1494N/A connector_funcs = connector->helper_private;
1494N/A encoder = connector_funcs->best_encoder(connector);
1494N/A if (!encoder)
1494N/A goto out;
1494N/A
1494N/A /* select a crtc for this connector and then attempt to configure
1494N/A remaining connectors */
1494N/A for (c = 0; c < fb_helper->crtc_count; c++) {
1494N/A crtc = &fb_helper->crtc_info[c];
1494N/A
1494N/A if ((encoder->possible_crtcs & (1 << c)) == 0)
1494N/A continue;
1494N/A
1494N/A for (o = 0; o < n; o++)
1494N/A if (best_crtcs[o] == crtc)
1494N/A break;
1494N/A
1494N/A if (o < n) {
1494N/A /* ignore cloning unless only a single crtc */
1494N/A if (fb_helper->crtc_count > 1)
1494N/A continue;
1494N/A
1494N/A if (!drm_mode_equal(modes[o], modes[n]))
1494N/A continue;
1494N/A }
1494N/A
1494N/A crtcs[n] = crtc;
1494N/A (void) memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
1494N/A score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
1494N/A width, height);
1494N/A if (score > best_score) {
1494N/A best_score = score;
1494N/A (void) memcpy(best_crtcs, crtcs,
1494N/A num_connector *
1494N/A sizeof(struct drm_fb_helper_crtc *));
1494N/A }
1494N/A }
1494N/Aout:
1494N/A kfree(crtcs, (num_connector * sizeof(struct drm_fb_helper_crtc *)));
1494N/A return best_score;
1494N/A}
1494N/A
1494N/Astatic void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
1494N/A{
1494N/A struct drm_device *dev = fb_helper->dev;
1494N/A struct drm_fb_helper_crtc **crtcs;
1494N/A struct drm_display_mode **modes;
1494N/A struct drm_mode_set *modeset;
1494N/A bool *enabled;
1494N/A int width, height;
1494N/A int i, num_connector = dev->mode_config.num_connector;
1494N/A
1494N/A DRM_DEBUG_KMS("\n");
1494N/A
1494N/A width = dev->mode_config.max_width;
1494N/A height = dev->mode_config.max_height;
1494N/A
1494N/A if ((crtcs = kcalloc(num_connector,
1494N/A sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL)) == NULL) {
1494N/A DRM_ERROR("Memory allocation failed for crtcs\n");
1494N/A return;
1494N/A }
1494N/A if ((modes = kcalloc(num_connector,
1494N/A sizeof(struct drm_display_mode *), GFP_KERNEL)) == NULL) {
1494N/A DRM_ERROR("Memory allocation failed for modes\n");
1494N/A goto errout1;
1494N/A }
1494N/A if ((enabled = kcalloc(num_connector,
1494N/A sizeof(bool), GFP_KERNEL)) == NULL) {
1494N/A DRM_ERROR("Memory allocation failed for enabled\n");
1494N/A goto errout2;
1494N/A }
1494N/A
1494N/A
1494N/A drm_enable_connectors(fb_helper, enabled);
1494N/A
1494N/A if (!(fb_helper->funcs->initial_config &&
1494N/A fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
1494N/A enabled, width, height))) {
1494N/A (void) memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0]));
1494N/A (void) memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0]));
1494N/A
1494N/A if (!drm_target_cloned(fb_helper,
1494N/A modes, enabled, width, height) &&
1494N/A !drm_target_preferred(fb_helper,
1494N/A modes, enabled, width, height))
1494N/A DRM_ERROR("Unable to find initial modes\n");
1494N/A
1494N/A DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
1494N/A width, height);
1494N/A
1494N/A (void) drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
1494N/A }
1494N/A
1494N/A /* need to set the modesets up here for use later */
1494N/A /* fill out the connector<->crtc mappings into the modesets */
1494N/A for (i = 0; i < fb_helper->crtc_count; i++) {
1494N/A modeset = &fb_helper->crtc_info[i].mode_set;
1494N/A modeset->num_connectors = 0;
1494N/A modeset->fb = NULL;
1494N/A }
1494N/A
1494N/A for (i = 0; i < fb_helper->connector_count; i++) {
1494N/A struct drm_display_mode *mode = modes[i];
1494N/A struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
1494N/A modeset = &fb_crtc->mode_set;
1494N/A
1494N/A if (mode && fb_crtc) {
1494N/A DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
1494N/A mode->name, fb_crtc->mode_set.crtc->base.id);
1494N/A fb_crtc->desired_mode = mode;
1494N/A if (modeset->mode)
1494N/A drm_mode_destroy(dev, modeset->mode);
1494N/A modeset->mode = drm_mode_duplicate(dev,
1494N/A fb_crtc->desired_mode);
1494N/A modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
1494N/A modeset->fb = fb_helper->fb;
1494N/A }
1494N/A }
1494N/A
1494N/A /* Clear out any old modes if there are no more connected outputs. */
1494N/A for (i = 0; i < fb_helper->crtc_count; i++) {
1494N/A modeset = &fb_helper->crtc_info[i].mode_set;
1494N/A if (modeset->num_connectors == 0) {
1494N/A BUG_ON(modeset->fb);
1494N/A BUG_ON(modeset->num_connectors);
1494N/A if (modeset->mode)
1494N/A drm_mode_destroy(dev, modeset->mode);
1494N/A modeset->mode = NULL;
1494N/A }
1494N/A }
1494N/A
1494N/A kfree(enabled, num_connector * sizeof(bool));
1494N/Aerrout2:
1494N/A kfree(modes, num_connector * sizeof(struct drm_display_mode *));
1494N/Aerrout1:
1494N/A kfree(crtcs, num_connector * sizeof(struct drm_fb_helper_crtc *));
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_helper_initial_config - setup a sane initial connector configuration
1494N/A * @dev: DRM device
1494N/A *
1494N/A *
1494N/A * Scan the CRTCs and connectors and try to put together an initial setup.
1494N/A * At the moment, this is a cloned configuration across all heads with
1494N/A * a new framebuffer object as the backing store.
1494N/A *
1494N/A * Note that this also registers the fbdev and so allows userspace to call into
1494N/A * the driver through the fbdev interfaces.
1494N/A *
1494N/A * This function will call down into the ->fb_probe callback to let
1494N/A * the driver allocate and initialize the fbdev info structure and the drm
1494N/A * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
1494N/A * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
1494N/A * values for the fbdev info structure.
1494N/A *
1494N/A * RETURNS:
1494N/A * Zero if everything went ok, nonzero otherwise.
1494N/A */
1494N/Abool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
1494N/A{
1494N/A struct drm_device *dev = fb_helper->dev;
1494N/A int count = 0;
1494N/A
1494N/A (void) drm_fb_helper_parse_command_line(fb_helper);
1494N/A
1494N/A count = drm_fb_helper_probe_connector_modes(fb_helper,
1494N/A dev->mode_config.max_width,
1494N/A dev->mode_config.max_height);
1494N/A /*
1494N/A * we shouldn't end up with no modes here.
1494N/A */
1494N/A if (count == 0) {
1494N/A DRM_INFO("No connectors reported connected with modes");
1494N/A }
1494N/A drm_setup_crtcs(fb_helper);
1494N/A
1494N/A return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1494N/A}
1494N/A
1494N/A
1494N/A/**
1494N/A * drm_fb_helper_hotplug_event - respond to a hotplug notification by
1494N/A * probing all the outputs attached to the fb.
1494N/A * @fb_helper: the drm_fb_helper
1494N/A *
1494N/A * Scan the connectors attached to the fb_helper and try to put together a
1494N/A * setup after *notification of a change in output configuration.
1494N/A *
1494N/A * Called at runtime, takes the mode config locks to be able to check/change the
1494N/A * modeset configuration. Must be run from process context (which usually means
1494N/A * either the output polling work or a work item launched from the driver's
1494N/A * hotplug interrupt).
1494N/A *
1494N/A * Scan the connectors attached to the fb_helper and try to put together a
1494N/A * setup after *notification of a change in output configuration.
1494N/A *
1494N/A * RETURNS:
1494N/A * 0 on success and a non-zero error code otherwise.
1494N/A */
1494N/Aint drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
1494N/A{
1494N/A struct drm_device *dev = fb_helper->dev;
1494N/A u32 max_width, max_height, bpp_sel;
1494N/A
1494N/A if (!fb_helper->fb)
1494N/A return 0;
1494N/A
1494N/A mutex_lock(&fb_helper->dev->mode_config.mutex);
1494N/A if (!drm_fb_helper_is_bound(fb_helper)) {
1494N/A fb_helper->delayed_hotplug = true;
1494N/A mutex_unlock(&fb_helper->dev->mode_config.mutex);
1494N/A return 0;
1494N/A }
1494N/A DRM_DEBUG_KMS("\n");
1494N/A
1494N/A max_width = fb_helper->fb->width;
1494N/A max_height = fb_helper->fb->height;
1494N/A bpp_sel = fb_helper->fb->bits_per_pixel;
1494N/A
1494N/A (void) drm_fb_helper_probe_connector_modes(fb_helper, max_width,
1494N/A max_height);
1494N/A mutex_unlock(&fb_helper->dev->mode_config.mutex);
1494N/A
1494N/A drm_modeset_lock_all(dev);
1494N/A drm_setup_crtcs(fb_helper);
1494N/A drm_modeset_unlock_all(dev);
1494N/A
1494N/A return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
1494N/A}
1494N/A
1494N/Aint drm_gfxp_setmode(int mode)
1494N/A{
1494N/A bool ret = 0;
1494N/A if (mode == 0) {
1494N/A ret = drm_fb_helper_force_kernel_mode();
1494N/A if (ret == true)
1494N/A DRM_ERROR("Failed to restore crtc configuration\n");
1494N/A }
1494N/A if (mode == 1)
1494N/A DRM_DEBUG_KMS("do nothing in entervt");
1494N/A return ret;
1494N/A}
1494N/A
1494N/Astruct gfxp_blt_ops drm_gfxp_ops = {
1494N/A NULL, /* blt */
1494N/A NULL, /* copy */
1494N/A NULL, /* clear */
1494N/A drm_gfxp_setmode, /* setmode */
1494N/A};
1494N/A
1494N/Avoid drm_register_fbops(struct drm_device *dev)
1494N/A{
1494N/A gfxp_bm_register_fbops(dev->vgatext->private, &drm_gfxp_ops);
1494N/A}
1494N/A
1494N/Aint drm_getfb_size(struct drm_device *dev)
1494N/A{
1494N/A struct gfxp_bm_fb_info fb_info;
1494N/A int size, pitch;
1494N/A gfxp_bm_getfb_info(dev->vgatext->private, &fb_info);
1494N/A pitch = ALIGN(fb_info.xres * ((fb_info.depth + 7) / 8), 64);
1494N/A size = ALIGN(pitch *fb_info.yres, PAGE_SIZE);
1494N/A return size;
1494N/A}