1494N/A/*
1494N/A * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
1494N/A */
1494N/A
1494N/A/*
1494N/A * Copyright (c) 2006-2008, 2013, Intel Corporation
1494N/A * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
1494N/A *
1494N/A * DRM core CRTC related 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 * Keith Packard
1494N/A * Eric Anholt <eric@anholt.net>
1494N/A * Dave Airlie <airlied@linux.ie>
1494N/A * Jesse Barnes <jesse.barnes@intel.com>
1494N/A */
1494N/A
1494N/A#include "drmP.h"
1494N/A#include "drm_crtc.h"
1494N/A#include "drm_fourcc.h"
1494N/A#include "drm_crtc_helper.h"
1494N/A#include "drm_fb_helper.h"
1494N/A#include "drm_edid.h"
1494N/A
1494N/A/**
1494N/A * drm_helper_move_panel_connectors_to_head() - move panels to the front in the
1494N/A * connector list
1494N/A * @dev: drm device to operate on
1494N/A *
1494N/A * Some userspace presumes that the first connected connector is the main
1494N/A * display, where it's supposed to display e.g. the login screen. For
1494N/A * laptops, this should be the main panel. Use this function to sort all
1494N/A * (eDP/LVDS) panels to the front of the connector list, instead of
1494N/A * painstakingly trying to initialize them in the right order.
1494N/A */
1494N/Avoid drm_helper_move_panel_connectors_to_head(struct drm_device *dev)
1494N/A{
1494N/A struct drm_connector *connector, *tmp;
1494N/A struct list_head panel_list;
1494N/A struct list_head *tmp2;
1494N/A
1494N/A INIT_LIST_HEAD(&panel_list);
1494N/A
1494N/A list_for_each_entry_safe(connector, tmp, struct drm_connector,
1494N/A &dev->mode_config.connector_list, head) {
1494N/A if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS ||
1494N/A connector->connector_type == DRM_MODE_CONNECTOR_eDP)
1494N/A list_move_tail(&connector->head, &panel_list, (caddr_t)connector);
1494N/A }
1494N/A tmp2 = dev->mode_config.connector_list.next;
1494N/A list_splice(&panel_list, &dev->mode_config.connector_list, tmp2);
1494N/A}
1494N/A
1494N/Astatic bool drm_kms_helper_poll = true;
1494N/A
1494N/Astatic void drm_mode_validate_flag(struct drm_connector *connector,
1494N/A int flags)
1494N/A{
1494N/A struct drm_display_mode *mode;
1494N/A
1494N/A if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE))
1494N/A return;
1494N/A
1494N/A list_for_each_entry(mode, struct drm_display_mode, &connector->modes, head) {
1494N/A if ((mode->flags & DRM_MODE_FLAG_INTERLACE) &&
1494N/A !(flags & DRM_MODE_FLAG_INTERLACE))
1494N/A mode->status = MODE_NO_INTERLACE;
1494N/A if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) &&
1494N/A !(flags & DRM_MODE_FLAG_DBLSCAN))
1494N/A mode->status = MODE_NO_DBLESCAN;
1494N/A }
1494N/A
1494N/A return;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_helper_probe_connector_modes - get complete set of display modes
1494N/A * @dev: DRM device
1494N/A * @maxX: max width for modes
1494N/A * @maxY: max height for modes
1494N/A *
1494N/A * LOCKING:
1494N/A * Caller must hold mode config lock.
1494N/A *
1494N/A * Based on @dev's mode_config layout, scan all the connectors and try to detect
1494N/A * modes on them. Modes will first be added to the connector's probed_modes
1494N/A * list, then culled (based on validity and the @maxX, @maxY parameters) and
1494N/A * put into the normal modes list.
1494N/A *
1494N/A * Intended to be used either at bootup time or when major configuration
1494N/A * changes have occurred.
1494N/A *
1494N/A * FIXME: take into account monitor limits
1494N/A *
1494N/A * RETURNS:
1494N/A * Number of modes found on @connector.
1494N/A */
1494N/Aint drm_helper_probe_single_connector_modes(struct drm_connector *connector,
1494N/A uint32_t maxX, uint32_t maxY)
1494N/A{
1494N/A struct drm_device *dev = connector->dev;
1494N/A struct drm_display_mode *mode;
1494N/A struct drm_connector_helper_funcs *connector_funcs =
1494N/A connector->helper_private;
1494N/A int count = 0;
1494N/A int mode_flags = 0;
1494N/A bool verbose_prune = true;
1494N/A
1494N/A DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id,
1494N/A drm_get_connector_name(connector));
1494N/A /* set all modes to the unverified state */
1494N/A list_for_each_entry(mode, struct drm_display_mode, &connector->modes, head)
1494N/A mode->status = MODE_UNVERIFIED;
1494N/A
1494N/A if (connector->force) {
1494N/A if (connector->force == DRM_FORCE_ON)
1494N/A connector->status = connector_status_connected;
1494N/A else
1494N/A connector->status = connector_status_disconnected;
1494N/A if (connector->funcs->force)
1494N/A connector->funcs->force(connector);
1494N/A } else {
1494N/A connector->status = connector->funcs->detect(connector, true);
1494N/A }
1494N/A /* Re-enable polling in case the global poll config changed. */
1494N/A if (drm_kms_helper_poll != dev->mode_config.poll_running)
1494N/A drm_kms_helper_poll_enable(dev);
1494N/A
1494N/A dev->mode_config.poll_running = drm_kms_helper_poll;
1494N/A
1494N/A if (connector->status == connector_status_disconnected) {
1494N/A DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n",
1494N/A connector->base.id, drm_get_connector_name(connector));
1494N/A (void) drm_mode_connector_update_edid_property(connector, NULL);
1494N/A verbose_prune = false;
1494N/A goto prune;
1494N/A }
1494N/A
1494N/A#ifdef CONFIG_DRM_LOAD_EDID_FIRMWARE
1494N/A count = drm_load_edid_firmware(connector);
1494N/A if (count == 0)
1494N/A#endif
1494N/A count = (*connector_funcs->get_modes)(connector);
1494N/A
1494N/A if (count == 0 && connector->status == connector_status_connected)
1494N/A count = drm_add_modes_noedid(connector, 1024, 768);
1494N/A if (count == 0)
1494N/A goto prune;
1494N/A
1494N/A drm_mode_connector_list_update(connector);
1494N/A
1494N/A if (maxX && maxY)
1494N/A drm_mode_validate_size(dev, &connector->modes, maxX,
1494N/A maxY, 0);
1494N/A
1494N/A if (connector->interlace_allowed)
1494N/A mode_flags |= DRM_MODE_FLAG_INTERLACE;
1494N/A if (connector->doublescan_allowed)
1494N/A mode_flags |= DRM_MODE_FLAG_DBLSCAN;
1494N/A drm_mode_validate_flag(connector, mode_flags);
1494N/A
1494N/A list_for_each_entry(mode, struct drm_display_mode, &connector->modes, head){
1494N/A if (mode->status == MODE_OK)
1494N/A mode->status = connector_funcs->mode_valid(connector,
1494N/A mode);
1494N/A }
1494N/A
1494N/Aprune:
1494N/A drm_mode_prune_invalid(dev, &connector->modes, verbose_prune);
1494N/A
1494N/A if (list_empty(&connector->modes))
1494N/A return 0;
1494N/A
1494N/A list_for_each_entry(mode, struct drm_display_mode, &connector->modes, head)
1494N/A mode->vrefresh = drm_mode_vrefresh(mode);
1494N/A
1494N/A drm_mode_sort(&connector->modes);
1494N/A
1494N/A DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id,
1494N/A drm_get_connector_name(connector));
1494N/A list_for_each_entry(mode, struct drm_display_mode, &connector->modes, head){
1494N/A drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1494N/A drm_mode_debug_printmodeline(mode);
1494N/A }
1494N/A
1494N/A return count;
1494N/A}
1494N/A
1494N/A
1494N/A/**
1494N/A * drm_helper_encoder_in_use - check if a given encoder is in use
1494N/A * @encoder: encoder to check
1494N/A *
1494N/A * LOCKING:
1494N/A * Caller must hold mode config lock.
1494N/A *
1494N/A * Walk @encoders's DRM device's mode_config and see if it's in use.
1494N/A *
1494N/A * RETURNS:
1494N/A * True if @encoder is part of the mode_config, false otherwise.
1494N/A */
1494N/Abool drm_helper_encoder_in_use(struct drm_encoder *encoder)
1494N/A{
1494N/A struct drm_connector *connector;
1494N/A struct drm_device *dev = encoder->dev;
1494N/A list_for_each_entry(connector, struct drm_connector, &dev->mode_config.connector_list, head)
1494N/A if (connector->encoder == encoder)
1494N/A return true;
1494N/A return false;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config
1494N/A * @crtc: CRTC to check
1494N/A *
1494N/A * LOCKING:
1494N/A * Caller must hold mode config lock.
1494N/A *
1494N/A * Walk @crtc's DRM device's mode_config and see if it's in use.
1494N/A *
1494N/A * RETURNS:
1494N/A * True if @crtc is part of the mode_config, false otherwise.
1494N/A */
1494N/Abool drm_helper_crtc_in_use(struct drm_crtc *crtc)
1494N/A{
1494N/A struct drm_encoder *encoder;
1494N/A struct drm_device *dev = crtc->dev;
1494N/A /* FIXME: Locking around list access? */
1494N/A list_for_each_entry(encoder, struct drm_encoder, &dev->mode_config.encoder_list, head)
1494N/A if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder))
1494N/A return true;
1494N/A return false;
1494N/A}
1494N/Astatic void
1494N/Adrm_encoder_disable(struct drm_encoder *encoder)
1494N/A{
1494N/A struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1494N/A
1494N/A if (encoder_funcs->disable)
1494N/A (*encoder_funcs->disable)(encoder);
1494N/A else
1494N/A (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF);
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_helper_disable_unused_functions - disable unused objects
1494N/A * @dev: DRM device
1494N/A *
1494N/A * LOCKING:
1494N/A * Caller must hold mode config lock.
1494N/A *
1494N/A * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled
1494N/A * by calling its dpms function, which should power it off.
1494N/A */
1494N/Avoid drm_helper_disable_unused_functions(struct drm_device *dev)
1494N/A{
1494N/A struct drm_encoder *encoder;
1494N/A struct drm_connector *connector;
1494N/A struct drm_crtc *crtc;
1494N/A
1494N/A list_for_each_entry(connector, struct drm_connector, &dev->mode_config.connector_list, head) {
1494N/A if (!connector->encoder)
1494N/A continue;
1494N/A if (connector->status == connector_status_disconnected)
1494N/A connector->encoder = NULL;
1494N/A }
1494N/A
1494N/A list_for_each_entry(encoder, struct drm_encoder, &dev->mode_config.encoder_list, head) {
1494N/A if (!drm_helper_encoder_in_use(encoder)) {
1494N/A drm_encoder_disable(encoder);
1494N/A /* disconnector encoder from any connector */
1494N/A encoder->crtc = NULL;
1494N/A }
1494N/A }
1494N/A
1494N/A list_for_each_entry(crtc, struct drm_crtc, &dev->mode_config.crtc_list, head) {
1494N/A struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1494N/A crtc->enabled = drm_helper_crtc_in_use(crtc);
1494N/A if (!crtc->enabled) {
1494N/A if (crtc_funcs->disable)
1494N/A (*crtc_funcs->disable)(crtc);
1494N/A else
1494N/A (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF);
1494N/A crtc->fb = NULL;
1494N/A }
1494N/A }
1494N/A}
1494N/A
1494N/A
1494N/A/**
1494N/A * drm_encoder_crtc_ok - can a given crtc drive a given encoder?
1494N/A * @encoder: encoder to test
1494N/A * @crtc: crtc to test
1494N/A *
1494N/A * Return false if @encoder can't be driven by @crtc, true otherwise.
1494N/A */
1494N/Astatic bool drm_encoder_crtc_ok(struct drm_encoder *encoder,
1494N/A struct drm_crtc *crtc)
1494N/A{
1494N/A struct drm_device *dev;
1494N/A struct drm_crtc *tmp;
1494N/A int crtc_mask = 1;
1494N/A
1494N/A if (crtc == NULL)
1494N/A DRM_ERROR("checking null crtc?\n");
1494N/A
1494N/A dev = crtc->dev;
1494N/A
1494N/A list_for_each_entry(tmp, struct drm_crtc, &dev->mode_config.crtc_list, head) {
1494N/A if (tmp == crtc)
1494N/A break;
1494N/A crtc_mask <<= 1;
1494N/A }
1494N/A
1494N/A if (encoder->possible_crtcs & crtc_mask)
1494N/A return true;
1494N/A return false;
1494N/A}
1494N/A
1494N/A/*
1494N/A * Check the CRTC we're going to map each output to vs. its current
1494N/A * CRTC. If they don't match, we have to disable the output and the CRTC
1494N/A * since the driver will have to re-route things.
1494N/A */
1494N/Astatic void
1494N/Adrm_crtc_prepare_encoders(struct drm_device *dev)
1494N/A{
1494N/A struct drm_encoder_helper_funcs *encoder_funcs;
1494N/A struct drm_encoder *encoder;
1494N/A
1494N/A list_for_each_entry(encoder, struct drm_encoder, &dev->mode_config.encoder_list, head) {
1494N/A encoder_funcs = encoder->helper_private;
1494N/A /* Disable unused encoders */
1494N/A if (encoder->crtc == NULL)
1494N/A drm_encoder_disable(encoder);
1494N/A /* Disable encoders whose CRTC is about to change */
1494N/A if (encoder_funcs->get_crtc &&
1494N/A encoder->crtc != (*encoder_funcs->get_crtc)(encoder))
1494N/A drm_encoder_disable(encoder);
1494N/A }
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_crtc_set_mode - set a mode
1494N/A * @crtc: CRTC to program
1494N/A * @mode: mode to use
1494N/A * @x: width of mode
1494N/A * @y: height of mode
1494N/A * @old_fb: old framebuffer, for cleanup
1494N/A *
1494N/A * LOCKING:
1494N/A * Caller must hold mode config lock.
1494N/A *
1494N/A * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance
1494N/A * to fixup or reject the mode prior to trying to set it. This is an internal
1494N/A * helper that drivers could e.g. use to update properties that require the
1494N/A * entire output pipe to be disabled and re-enabled in a new configuration. For
1494N/A * example for changing whether audio is enabled on a hdmi link or for changing
1494N/A * panel fitter or dither attributes. It is also called by the
1494N/A * drm_crtc_helper_set_config() helper function to drive the mode setting
1494N/A * sequence.
1494N/A *
1494N/A * RETURNS:
1494N/A * True if the mode was set successfully, or false otherwise.
1494N/A */
1494N/Abool drm_crtc_helper_set_mode(struct drm_crtc *crtc,
1494N/A struct drm_display_mode *mode,
1494N/A int x, int y,
1494N/A struct drm_framebuffer *old_fb)
1494N/A{
1494N/A struct drm_device *dev = crtc->dev;
1494N/A struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode;
1494N/A struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1494N/A struct drm_encoder_helper_funcs *encoder_funcs;
1494N/A int saved_x, saved_y;
1494N/A struct drm_encoder *encoder;
1494N/A bool ret = true;
1494N/A
1494N/A crtc->enabled = drm_helper_crtc_in_use(crtc);
1494N/A if (!crtc->enabled)
1494N/A return true;
1494N/A
1494N/A adjusted_mode = drm_mode_duplicate(dev, mode);
1494N/A if (!adjusted_mode)
1494N/A return false;
1494N/A
1494N/A saved_hwmode = crtc->hwmode;
1494N/A saved_mode = crtc->mode;
1494N/A saved_x = crtc->x;
1494N/A saved_y = crtc->y;
1494N/A
1494N/A /* Update crtc values up front so the driver can rely on them for mode
1494N/A * setting.
1494N/A */
1494N/A crtc->mode = *mode;
1494N/A crtc->x = x;
1494N/A crtc->y = y;
1494N/A
1494N/A /* Pass our mode to the connectors and the CRTC to give them a chance to
1494N/A * adjust it according to limitations or connector properties, and also
1494N/A * a chance to reject the mode entirely.
1494N/A */
1494N/A list_for_each_entry(encoder, struct drm_encoder, &dev->mode_config.encoder_list, head) {
1494N/A
1494N/A if (encoder->crtc != crtc)
1494N/A continue;
1494N/A encoder_funcs = encoder->helper_private;
1494N/A if (!(ret = encoder_funcs->mode_fixup(encoder, mode,
1494N/A adjusted_mode))) {
1494N/A DRM_DEBUG_KMS("Encoder fixup failed\n");
1494N/A goto done;
1494N/A }
1494N/A }
1494N/A
1494N/A if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) {
1494N/A DRM_DEBUG_KMS("CRTC fixup failed\n");
1494N/A goto done;
1494N/A }
1494N/A DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1494N/A
1494N/A /* Prepare the encoders and CRTCs before setting the mode. */
1494N/A list_for_each_entry(encoder, struct drm_encoder, &dev->mode_config.encoder_list, head) {
1494N/A
1494N/A if (encoder->crtc != crtc)
1494N/A continue;
1494N/A encoder_funcs = encoder->helper_private;
1494N/A /* Disable the encoders as the first thing we do. */
1494N/A encoder_funcs->prepare(encoder);
1494N/A }
1494N/A
1494N/A drm_crtc_prepare_encoders(dev);
1494N/A
1494N/A crtc_funcs->prepare(crtc);
1494N/A
1494N/A /* Set up the DPLL and any encoders state that needs to adjust or depend
1494N/A * on the DPLL.
1494N/A */
1494N/A ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb);
1494N/A if (!ret)
1494N/A goto done;
1494N/A
1494N/A list_for_each_entry(encoder, struct drm_encoder, &dev->mode_config.encoder_list, head) {
1494N/A
1494N/A if (encoder->crtc != crtc)
1494N/A continue;
1494N/A
1494N/A DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
1494N/A encoder->base.id, drm_get_encoder_name(encoder),
1494N/A mode->base.id, mode->name);
1494N/A encoder_funcs = encoder->helper_private;
1494N/A encoder_funcs->mode_set(encoder, mode, adjusted_mode);
1494N/A }
1494N/A
1494N/A /* Now enable the clocks, plane, pipe, and connectors that we set up. */
1494N/A crtc_funcs->commit(crtc);
1494N/A
1494N/A list_for_each_entry(encoder, struct drm_encoder, &dev->mode_config.encoder_list, head) {
1494N/A
1494N/A if (encoder->crtc != crtc)
1494N/A continue;
1494N/A
1494N/A encoder_funcs = encoder->helper_private;
1494N/A encoder_funcs->commit(encoder);
1494N/A
1494N/A }
1494N/A
1494N/A /* Store real post-adjustment hardware mode. */
1494N/A crtc->hwmode = *adjusted_mode;
1494N/A
1494N/A /* Calculate and store various constants which
1494N/A * are later needed by vblank and swap-completion
1494N/A * timestamping. They are derived from true hwmode.
1494N/A */
1494N/A drm_calc_timestamping_constants(crtc);
1494N/A
1494N/A /* FIXME: add subpixel order */
1494N/Adone:
1494N/A drm_mode_destroy(dev, adjusted_mode);
1494N/A if (!ret) {
1494N/A crtc->hwmode = saved_hwmode;
1494N/A crtc->mode = saved_mode;
1494N/A crtc->x = saved_x;
1494N/A crtc->y = saved_y;
1494N/A }
1494N/A
1494N/A return ret;
1494N/A}
1494N/A
1494N/Astatic int
1494N/Adrm_crtc_helper_disable(struct drm_crtc *crtc)
1494N/A{
1494N/A struct drm_device *dev = crtc->dev;
1494N/A struct drm_connector *connector;
1494N/A struct drm_encoder *encoder;
1494N/A
1494N/A /* Decouple all encoders and their attached connectors from this crtc */
1494N/A list_for_each_entry(encoder, struct drm_encoder, &dev->mode_config.encoder_list, head) {
1494N/A if (encoder->crtc != crtc)
1494N/A continue;
1494N/A
1494N/A list_for_each_entry(connector, struct drm_connector, &dev->mode_config.connector_list, head) {
1494N/A if (connector->encoder != encoder)
1494N/A continue;
1494N/A
1494N/A connector->encoder = NULL;
1494N/A }
1494N/A }
1494N/A
1494N/A drm_helper_disable_unused_functions(dev);
1494N/A return 0;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_crtc_helper_set_config - set a new config from userspace
1494N/A * @set: mode set configuration
1494N/A *
1494N/A * LOCKING:
1494N/A * Caller must hold mode config lock.
1494N/A *
1494N/A * Setup a new configuration, provided by the upper layers (either an ioctl call
1494N/A * from userspace or internally e.g. from the fbdev suppport code) in @set, and
1494N/A * enable it. This is the main helper functions for drivers that implement
1494N/A * kernel mode setting with the crtc helper functions and the assorted
1494N/A * ->prepare(), ->modeset() and ->commit() helper callbacks.
1494N/A *
1494N/A * RETURNS:
1494N/A * Zero. (FIXME)
1494N/A */
1494N/Aint drm_crtc_helper_set_config(struct drm_mode_set *set)
1494N/A{
1494N/A struct drm_device *dev;
1494N/A struct drm_crtc *save_crtcs, *new_crtc, *crtc;
1494N/A struct drm_encoder *save_encoders, *new_encoder, *encoder;
1494N/A struct drm_framebuffer *old_fb = NULL;
1494N/A bool mode_changed = false; /* if true do a full mode set */
1494N/A bool fb_changed = false; /* if true and !mode_changed just do a flip */
1494N/A struct drm_connector *save_connectors, *connector;
1494N/A int count = 0, ro, fail = 0;
1494N/A struct drm_crtc_helper_funcs *crtc_funcs;
1494N/A struct drm_mode_set save_set;
1494N/A int ret;
1494N/A int i;
1494N/A
1494N/A DRM_DEBUG_KMS("\n");
1494N/A
1494N/A BUG_ON(!set);
1494N/A BUG_ON(!set->crtc);
1494N/A BUG_ON(!set->crtc->helper_private);
1494N/A
1494N/A /* Enforce sane interface api - has been abused by the fb helper. */
1494N/A BUG_ON(!set->mode && set->fb);
1494N/A BUG_ON(set->fb && set->num_connectors == 0);
1494N/A
1494N/A crtc_funcs = set->crtc->helper_private;
1494N/A
1494N/A if (!set->mode)
1494N/A set->fb = NULL;
1494N/A
1494N/A if (set->fb) {
1494N/A DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
1494N/A set->crtc->base.id, set->fb->base.id,
1494N/A (int)set->num_connectors, set->x, set->y);
1494N/A } else {
1494N/A DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
1494N/A return drm_crtc_helper_disable(set->crtc);
1494N/A }
1494N/A
1494N/A dev = set->crtc->dev;
1494N/A
1494N/A /* Allocate space for the backup of all (non-pointer) crtc, encoder and
1494N/A * connector data. */
1494N/A save_crtcs = kzalloc(dev->mode_config.num_crtc *
1494N/A sizeof(struct drm_crtc), GFP_KERNEL);
1494N/A if (!save_crtcs)
1494N/A return -ENOMEM;
1494N/A
1494N/A save_encoders = kzalloc(dev->mode_config.num_encoder *
1494N/A sizeof(struct drm_encoder), GFP_KERNEL);
1494N/A if (!save_encoders) {
1494N/A kfree(save_crtcs, dev->mode_config.num_crtc * sizeof(struct drm_crtc));
1494N/A return -ENOMEM;
1494N/A }
1494N/A
1494N/A save_connectors = kzalloc(dev->mode_config.num_connector *
1494N/A sizeof(struct drm_connector), GFP_KERNEL);
1494N/A if (!save_connectors) {
1494N/A kfree(save_crtcs, dev->mode_config.num_crtc * sizeof(struct drm_crtc));
1494N/A kfree(save_encoders, dev->mode_config.num_encoder * sizeof(struct drm_encoder));
1494N/A return -ENOMEM;
1494N/A }
1494N/A
1494N/A /* Copy data. Note that driver private data is not affected.
1494N/A * Should anything bad happen only the expected state is
1494N/A * restored, not the drivers personal bookkeeping.
1494N/A */
1494N/A count = 0;
1494N/A list_for_each_entry(crtc, struct drm_crtc, &dev->mode_config.crtc_list, head) {
1494N/A save_crtcs[count++] = *crtc;
1494N/A }
1494N/A
1494N/A count = 0;
1494N/A list_for_each_entry(encoder, struct drm_encoder, &dev->mode_config.encoder_list, head) {
1494N/A save_encoders[count++] = *encoder;
1494N/A }
1494N/A
1494N/A count = 0;
1494N/A list_for_each_entry(connector, struct drm_connector, &dev->mode_config.connector_list, head) {
1494N/A save_connectors[count++] = *connector;
1494N/A }
1494N/A
1494N/A save_set.crtc = set->crtc;
1494N/A save_set.mode = &set->crtc->mode;
1494N/A save_set.x = set->crtc->x;
1494N/A save_set.y = set->crtc->y;
1494N/A save_set.fb = set->crtc->fb;
1494N/A
1494N/A /* We should be able to check here if the fb has the same properties
1494N/A * and then just flip_or_move it */
1494N/A if (set->crtc->fb != set->fb) {
1494N/A /* If we have no fb then treat it as a full mode set */
1494N/A if (set->crtc->fb == NULL) {
1494N/A DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
1494N/A mode_changed = true;
1494N/A } else if (set->fb == NULL) {
1494N/A mode_changed = true;
1494N/A } else if (set->fb->pixel_format !=
1494N/A set->crtc->fb->pixel_format) {
1494N/A mode_changed = true;
1494N/A } else
1494N/A fb_changed = true;
1494N/A }
1494N/A
1494N/A if (set->x != set->crtc->x || set->y != set->crtc->y)
1494N/A fb_changed = true;
1494N/A
1494N/A if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
1494N/A DRM_DEBUG_KMS("modes are different, full mode set\n");
1494N/A drm_mode_debug_printmodeline(&set->crtc->mode);
1494N/A drm_mode_debug_printmodeline(set->mode);
1494N/A mode_changed = true;
1494N/A }
1494N/A
1494N/A /* a) traverse passed in connector list and get encoders for them */
1494N/A count = 0;
1494N/A list_for_each_entry(connector, struct drm_connector, &dev->mode_config.connector_list, head) {
1494N/A struct drm_connector_helper_funcs *connector_funcs =
1494N/A connector->helper_private;
1494N/A new_encoder = connector->encoder;
1494N/A for (ro = 0; ro < set->num_connectors; ro++) {
1494N/A if (set->connectors[ro] == connector) {
1494N/A new_encoder = connector_funcs->best_encoder(connector);
1494N/A /* if we can't get an encoder for a connector
1494N/A we are setting now - then fail */
1494N/A if (new_encoder == NULL)
1494N/A /* don't break so fail path works correct */
1494N/A fail = 1;
1494N/A break;
1494N/A/*
1494N/A if (connector->dpms != DRM_MODE_DPMS_ON) {
1494N/A DRM_DEBUG_KMS("connector dpms not on, full mode switch\n");
1494N/A mode_changed = true;
1494N/A }
1494N/A*/
1494N/A }
1494N/A }
1494N/A
1494N/A if (new_encoder != connector->encoder) {
1494N/A DRM_DEBUG_KMS("encoder changed, full mode switch\n");
1494N/A mode_changed = true;
1494N/A /* If the encoder is reused for another connector, then
1494N/A * the appropriate crtc will be set later.
1494N/A */
1494N/A if (connector->encoder)
1494N/A connector->encoder->crtc = NULL;
1494N/A connector->encoder = new_encoder;
1494N/A }
1494N/A }
1494N/A
1494N/A if (fail) {
1494N/A ret = -EINVAL;
1494N/A goto fail;
1494N/A }
1494N/A
1494N/A count = 0;
1494N/A list_for_each_entry(connector, struct drm_connector, &dev->mode_config.connector_list, head) {
1494N/A if (!connector->encoder)
1494N/A continue;
1494N/A
1494N/A if (connector->encoder->crtc == set->crtc)
1494N/A new_crtc = NULL;
1494N/A else
1494N/A new_crtc = connector->encoder->crtc;
1494N/A
1494N/A for (ro = 0; ro < set->num_connectors; ro++) {
1494N/A if (set->connectors[ro] == connector)
1494N/A new_crtc = set->crtc;
1494N/A }
1494N/A
1494N/A /* Make sure the new CRTC will work with the encoder */
1494N/A if (new_crtc &&
1494N/A !drm_encoder_crtc_ok(connector->encoder, new_crtc)) {
1494N/A ret = -EINVAL;
1494N/A goto fail;
1494N/A }
1494N/A if (new_crtc != connector->encoder->crtc) {
1494N/A DRM_DEBUG_KMS("crtc changed, full mode switch\n");
1494N/A mode_changed = true;
1494N/A connector->encoder->crtc = new_crtc;
1494N/A }
1494N/A if (new_crtc) {
1494N/A DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
1494N/A connector->base.id, drm_get_connector_name(connector),
1494N/A new_crtc->base.id);
1494N/A } else {
1494N/A DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
1494N/A connector->base.id, drm_get_connector_name(connector));
1494N/A }
1494N/A }
1494N/A
1494N/A /* mode_set_base is not a required function */
1494N/A if (fb_changed && !crtc_funcs->mode_set_base)
1494N/A mode_changed = true;
1494N/A
1494N/A if (mode_changed) {
1494N/A set->crtc->enabled = drm_helper_crtc_in_use(set->crtc);
1494N/A if (set->crtc->enabled) {
1494N/A DRM_DEBUG_KMS("attempting to set mode from"
1494N/A " userspace\n");
1494N/A drm_mode_debug_printmodeline(set->mode);
1494N/A old_fb = set->crtc->fb;
1494N/A set->crtc->fb = set->fb;
1494N/A if (!drm_crtc_helper_set_mode(set->crtc, set->mode,
1494N/A set->x, set->y,
1494N/A old_fb)) {
1494N/A DRM_ERROR("failed to set mode on [CRTC:%d]\n",
1494N/A set->crtc->base.id);
1494N/A set->crtc->fb = old_fb;
1494N/A ret = -EINVAL;
1494N/A goto fail;
1494N/A }
1494N/A DRM_DEBUG_KMS("Setting connector DPMS state to on\n");
1494N/A for (i = 0; i < set->num_connectors; i++) {
1494N/A DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id,
1494N/A drm_get_connector_name(set->connectors[i]));
1494N/A set->connectors[i]->funcs->dpms(set->connectors[i], DRM_MODE_DPMS_ON);
1494N/A }
1494N/A }
1494N/A drm_helper_disable_unused_functions(dev);
1494N/A } else if (fb_changed) {
1494N/A set->crtc->x = set->x;
1494N/A set->crtc->y = set->y;
1494N/A
1494N/A old_fb = set->crtc->fb;
1494N/A if (set->crtc->fb != set->fb)
1494N/A set->crtc->fb = set->fb;
1494N/A ret = crtc_funcs->mode_set_base(set->crtc,
1494N/A set->x, set->y, old_fb);
1494N/A if (ret != 0) {
1494N/A set->crtc->fb = old_fb;
1494N/A goto fail;
1494N/A }
1494N/A }
1494N/A
1494N/A kfree(save_connectors, dev->mode_config.num_connector * sizeof(struct drm_connector));
1494N/A kfree(save_encoders, dev->mode_config.num_encoder * sizeof(struct drm_encoder));
1494N/A kfree(save_crtcs, dev->mode_config.num_crtc * sizeof(struct drm_crtc));
1494N/A return 0;
1494N/A
1494N/Afail:
1494N/A /* Restore all previous data. */
1494N/A count = 0;
1494N/A list_for_each_entry(crtc, struct drm_crtc, &dev->mode_config.crtc_list, head) {
1494N/A *crtc = save_crtcs[count++];
1494N/A }
1494N/A
1494N/A count = 0;
1494N/A list_for_each_entry(encoder, struct drm_encoder, &dev->mode_config.encoder_list, head) {
1494N/A *encoder = save_encoders[count++];
1494N/A }
1494N/A
1494N/A count = 0;
1494N/A list_for_each_entry(connector, struct drm_connector, &dev->mode_config.connector_list, head) {
1494N/A *connector = save_connectors[count++];
1494N/A }
1494N/A
1494N/A /* Try to restore the config */
1494N/A if (mode_changed &&
1494N/A !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x,
1494N/A save_set.y, save_set.fb))
1494N/A DRM_ERROR("failed to restore config after modeset failure\n");
1494N/A
1494N/A kfree(save_connectors, dev->mode_config.num_connector * sizeof(struct drm_connector));
1494N/A kfree(save_encoders, dev->mode_config.num_encoder * sizeof(struct drm_encoder));
1494N/A kfree(save_crtcs, dev->mode_config.num_crtc * sizeof(struct drm_crtc));
1494N/A return ret;
1494N/A}
1494N/A
1494N/A
1494N/Astatic int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder)
1494N/A{
1494N/A int dpms = DRM_MODE_DPMS_OFF;
1494N/A struct drm_connector *connector;
1494N/A struct drm_device *dev = encoder->dev;
1494N/A
1494N/A list_for_each_entry(connector, struct drm_connector, &dev->mode_config.connector_list, head)
1494N/A if (connector->encoder == encoder)
1494N/A if (connector->dpms < dpms)
1494N/A dpms = connector->dpms;
1494N/A return dpms;
1494N/A}
1494N/A
1494N/Astatic int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc)
1494N/A{
1494N/A int dpms = DRM_MODE_DPMS_OFF;
1494N/A struct drm_connector *connector;
1494N/A struct drm_device *dev = crtc->dev;
1494N/A
1494N/A list_for_each_entry(connector, struct drm_connector, &dev->mode_config.connector_list, head)
1494N/A if (connector->encoder && connector->encoder->crtc == crtc)
1494N/A if (connector->dpms < dpms)
1494N/A dpms = connector->dpms;
1494N/A return dpms;
1494N/A}
1494N/A
1494N/A/**
1494N/A * drm_helper_connector_dpms
1494N/A * @connector affected connector
1494N/A * @mode DPMS mode
1494N/A *
1494N/A * This is the main helper function provided by the crtc helper framework for
1494N/A * implementing the DPMS connector attribute. It computes the new desired DPMS
1494N/A * state for all encoders and crtcs in the output mesh and calls the ->dpms()
1494N/A * callback provided by the driver appropriately.
1494N/A */
1494N/Avoid drm_helper_connector_dpms(struct drm_connector *connector, int mode)
1494N/A{
1494N/A struct drm_encoder *encoder = connector->encoder;
1494N/A struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
1494N/A int old_dpms;
1494N/A
1494N/A if (mode == connector->dpms)
1494N/A return;
1494N/A
1494N/A old_dpms = connector->dpms;
1494N/A connector->dpms = mode;
1494N/A
1494N/A /* from off to on, do crtc then encoder */
1494N/A if (mode < old_dpms) {
1494N/A if (crtc) {
1494N/A struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1494N/A if (crtc_funcs->dpms)
1494N/A (*crtc_funcs->dpms) (crtc,
1494N/A drm_helper_choose_crtc_dpms(crtc));
1494N/A }
1494N/A if (encoder) {
1494N/A struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1494N/A if (encoder_funcs->dpms)
1494N/A (*encoder_funcs->dpms) (encoder,
1494N/A drm_helper_choose_encoder_dpms(encoder));
1494N/A }
1494N/A }
1494N/A
1494N/A /* from on to off, do encoder then crtc */
1494N/A if (mode > old_dpms) {
1494N/A if (encoder) {
1494N/A struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
1494N/A if (encoder_funcs->dpms)
1494N/A (*encoder_funcs->dpms) (encoder,
1494N/A drm_helper_choose_encoder_dpms(encoder));
1494N/A }
1494N/A if (crtc) {
1494N/A struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
1494N/A if (crtc_funcs->dpms)
1494N/A (*crtc_funcs->dpms) (crtc,
1494N/A drm_helper_choose_crtc_dpms(crtc));
1494N/A }
1494N/A }
1494N/A
1494N/A return;
1494N/A}
1494N/A
1494N/A
1494N/Aint drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb,
1494N/A struct drm_mode_fb_cmd2 *mode_cmd)
1494N/A{
1494N/A int i;
1494N/A
1494N/A fb->width = mode_cmd->width;
1494N/A fb->height = mode_cmd->height;
1494N/A for (i = 0; i < 4; i++) {
1494N/A fb->pitches[i] = mode_cmd->pitches[i];
1494N/A fb->offsets[i] = mode_cmd->offsets[i];
1494N/A }
1494N/A drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth,
1494N/A &fb->bits_per_pixel);
1494N/A fb->pixel_format = mode_cmd->pixel_format;
1494N/A
1494N/A return 0;
1494N/A}
1494N/A
1494N/Avoid drm_helper_resume_force_mode(struct drm_device *dev)
1494N/A{
1494N/A struct drm_crtc *crtc;
1494N/A struct drm_encoder *encoder;
1494N/A struct drm_encoder_helper_funcs *encoder_funcs;
1494N/A struct drm_crtc_helper_funcs *crtc_funcs;
1494N/A int ret;
1494N/A
1494N/A list_for_each_entry(crtc, struct drm_crtc, &dev->mode_config.crtc_list, head) {
1494N/A
1494N/A if (!crtc->enabled)
1494N/A continue;
1494N/A
1494N/A ret = drm_crtc_helper_set_mode(crtc, &crtc->mode,
1494N/A crtc->x, crtc->y, crtc->fb);
1494N/A
1494N/A if (ret == false)
1494N/A DRM_ERROR("failed to set mode on crtc %p\n", (void *)crtc);
1494N/A
1494N/A /* Turn off outputs that were already powered off */
1494N/A if (drm_helper_choose_crtc_dpms(crtc)) {
1494N/A list_for_each_entry(encoder, struct drm_encoder, &dev->mode_config.encoder_list, head) {
1494N/A
1494N/A if(encoder->crtc != crtc)
1494N/A continue;
1494N/A
1494N/A encoder_funcs = encoder->helper_private;
1494N/A if (encoder_funcs->dpms)
1494N/A (*encoder_funcs->dpms) (encoder,
1494N/A drm_helper_choose_encoder_dpms(encoder));
1494N/A }
1494N/A
1494N/A crtc_funcs = crtc->helper_private;
1494N/A if (crtc_funcs->dpms)
1494N/A (*crtc_funcs->dpms) (crtc,
1494N/A drm_helper_choose_crtc_dpms(crtc));
1494N/A }
1494N/A }
1494N/A /* disable the unused connectors while restoring the modesetting */
1494N/A drm_helper_disable_unused_functions(dev);
1494N/A}
1494N/A
1494N/Avoid drm_kms_helper_hotplug_event(struct drm_device *dev)
1494N/A{
1494N/A if (dev->mode_config.funcs->output_poll_changed)
1494N/A dev->mode_config.funcs->output_poll_changed(dev);
1494N/A}
1494N/A#define DRM_OUTPUT_POLL_PERIOD (10*DRM_HZ)
1494N/Astatic void
1494N/Aoutput_poll_execute(struct work_struct *work)
1494N/A{
1494N/A struct drm_device *dev = container_of(work, struct drm_device,
1494N/A output_poll_work);
1494N/A struct drm_connector *connector;
1494N/A enum drm_connector_status old_status;
1494N/A bool repoll = false, changed = false;
1494N/A
1494N/A if (!drm_kms_helper_poll)
1494N/A return;
1494N/A
1494N/A mutex_lock(&dev->mode_config.mutex);
1494N/A list_for_each_entry(connector, struct drm_connector, &dev->mode_config.connector_list, head) {
1494N/A
1494N/A /* Ignore forced connectors. */
1494N/A if (connector->force)
1494N/A continue;
1494N/A
1494N/A /* Ignore HPD capable connectors and connectors where we don't
1494N/A * want any hotplug detection at all for polling. */
1494N/A if (!connector->polled || connector->polled == DRM_CONNECTOR_POLL_HPD)
1494N/A continue;
1494N/A
1494N/A repoll = true;
1494N/A
1494N/A old_status = connector->status;
1494N/A /* if we are connected and don't want to poll for disconnect
1494N/A skip it */
1494N/A if (old_status == connector_status_connected &&
1494N/A !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT))
1494N/A continue;
1494N/A
1494N/A connector->status = connector->funcs->detect(connector, false);
1494N/A if (old_status != connector->status) {
1494N/A const char *old, *new;
1494N/A
1494N/A old = drm_get_connector_status_name(old_status);
1494N/A new = drm_get_connector_status_name(connector->status);
1494N/A
1494N/A DRM_DEBUG_KMS("[CONNECTOR:%d:%s] "
1494N/A "status updated from %s to %s\n",
1494N/A connector->base.id,
1494N/A drm_get_connector_name(connector),
1494N/A old, new);
1494N/A
1494N/A changed = true;
1494N/A }
1494N/A }
1494N/A
1494N/A mutex_unlock(&dev->mode_config.mutex);
1494N/A
1494N/A if (changed)
1494N/A drm_kms_helper_hotplug_event(dev);
1494N/A
1494N/A if (repoll)
1494N/A test_set_timer(&dev->output_poll_timer, DRM_OUTPUT_POLL_PERIOD);
1494N/A}
1494N/A
1494N/Avoid
1494N/Aoutput_poll_execute_timer(void *device)
1494N/A{
1494N/A struct drm_device *dev = (struct drm_device *)device;
1494N/A (void) queue_work(dev->drm_wq, &dev->output_poll_work);
1494N/A}
1494N/A
1494N/Avoid drm_kms_helper_poll_disable(struct drm_device *dev)
1494N/A{
1494N/A if (!dev->mode_config.poll_enabled)
1494N/A return;
1494N/A del_timer_sync(&dev->output_poll_timer);
1494N/A}
1494N/A
1494N/Avoid drm_kms_helper_poll_enable(struct drm_device *dev)
1494N/A{
1494N/A bool poll = false;
1494N/A struct drm_connector *connector;
1494N/A
1494N/A if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll)
1494N/A return;
1494N/A
1494N/A list_for_each_entry(connector, struct drm_connector, &dev->mode_config.connector_list, head) {
1494N/A if (connector->polled)
1494N/A poll = true;
1494N/A }
1494N/A
1494N/A if (poll)
1494N/A test_set_timer(&dev->output_poll_timer, DRM_OUTPUT_POLL_PERIOD);
1494N/A}
1494N/A
1494N/Avoid drm_kms_helper_poll_init(struct drm_device *dev)
1494N/A{
1494N/A INIT_WORK(&dev->output_poll_work, output_poll_execute);
1494N/A setup_timer(&dev->output_poll_timer, output_poll_execute_timer,
1494N/A (void *)dev);
1494N/A
1494N/A dev->mode_config.poll_enabled = true;
1494N/A
1494N/A drm_kms_helper_poll_enable(dev);
1494N/A}
1494N/A
1494N/Avoid drm_kms_helper_poll_fini(struct drm_device *dev)
1494N/A{
1494N/A drm_kms_helper_poll_disable(dev);
1494N/A}
1494N/A
1494N/Avoid drm_helper_hpd_irq_event(struct drm_device *dev)
1494N/A{
1494N/A if (!dev->mode_config.poll_enabled)
1494N/A return;
1494N/A
1494N/A /* kill timer and schedule immediate execution, this doesn't block */
1494N/A del_timer_sync(&dev->output_poll_timer);
1494N/A cancel_delayed_work(dev->drm_wq);
1494N/A if (drm_kms_helper_poll)
1494N/A test_set_timer(&dev->output_poll_timer, 0);
1494N/A}
1494N/A