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