1234370Sjasone/* 2234370Sjasone * Copyright (c) 2006-2008 Intel Corporation 3234370Sjasone * Copyright (c) 2007 Dave Airlie <airlied@linux.ie> 4234370Sjasone * 5234370Sjasone * DRM core CRTC related functions 6234370Sjasone * 7234370Sjasone * Permission to use, copy, modify, distribute, and sell this software and its 8234370Sjasone * documentation for any purpose is hereby granted without fee, provided that 9234370Sjasone * the above copyright notice appear in all copies and that both that copyright 10234370Sjasone * notice and this permission notice appear in supporting documentation, and 11234370Sjasone * that the name of the copyright holders not be used in advertising or 12234370Sjasone * publicity pertaining to distribution of the software without specific, 13234370Sjasone * written prior permission. The copyright holders make no representations 14234370Sjasone * about the suitability of this software for any purpose. It is provided "as 15234370Sjasone * is" without express or implied warranty. 16234370Sjasone * 17234370Sjasone * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 18234370Sjasone * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 19234370Sjasone * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 20234370Sjasone * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 21234370Sjasone * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 22234370Sjasone * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 23234370Sjasone * OF THIS SOFTWARE. 24234370Sjasone * 25234370Sjasone * Authors: 26234370Sjasone * Keith Packard 27234370Sjasone * Eric Anholt <eric@anholt.net> 28234370Sjasone * Dave Airlie <airlied@linux.ie> 29234370Sjasone * Jesse Barnes <jesse.barnes@intel.com> 30234370Sjasone */ 31234370Sjasone 32234370Sjasone#include <sys/cdefs.h> 33234370Sjasone__FBSDID("$FreeBSD$"); 34234370Sjasone 35234370Sjasone#include <sys/param.h> 36234370Sjasone#include <sys/systm.h> 37234370Sjasone#include <dev/drm2/drmP.h> 38234370Sjasone#include <dev/drm2/drm_crtc.h> 39234370Sjasone#include <dev/drm2/drm_fourcc.h> 40234370Sjasone#include <dev/drm2/drm_crtc_helper.h> 41234370Sjasone#include <dev/drm2/drm_fb_helper.h> 42234370Sjasone 43234370Sjasonebool 44234370Sjasonedrm_fetch_cmdline_mode_from_kenv(struct drm_connector *connector, 45234370Sjasone struct drm_cmdline_mode *cmdline_mode) 46234370Sjasone{ 47234370Sjasone char *tun_var_name, *tun_mode; 48234370Sjasone static const char tun_prefix[] = "drm_mode."; 49234370Sjasone bool res; 50234370Sjasone 51234370Sjasone res = false; 52234370Sjasone tun_var_name = malloc(sizeof(tun_prefix) + 53234370Sjasone strlen(drm_get_connector_name(connector)), M_TEMP, M_WAITOK); 54234370Sjasone strcpy(tun_var_name, tun_prefix); 55234370Sjasone strcat(tun_var_name, drm_get_connector_name(connector)); 56234370Sjasone tun_mode = getenv(tun_var_name); 57234370Sjasone if (tun_mode != NULL) { 58234370Sjasone res = drm_mode_parse_command_line_for_connector(tun_mode, 59234370Sjasone connector, cmdline_mode); 60234370Sjasone freeenv(tun_mode); 61234370Sjasone } 62234370Sjasone free(tun_var_name, M_TEMP); 63234370Sjasone return (res); 64234370Sjasone} 65234370Sjasone 66234370Sjasonestatic bool drm_kms_helper_poll = true; 67234370Sjasone 68234370Sjasonestatic void drm_mode_validate_flag(struct drm_connector *connector, 69234370Sjasone int flags) 70234370Sjasone{ 71234370Sjasone struct drm_display_mode *mode, *t; 72234370Sjasone 73245868Sjasone if (flags == (DRM_MODE_FLAG_DBLSCAN | DRM_MODE_FLAG_INTERLACE)) 74234370Sjasone return; 75234370Sjasone 76234370Sjasone list_for_each_entry_safe(mode, t, &connector->modes, head) { 77245868Sjasone if ((mode->flags & DRM_MODE_FLAG_INTERLACE) && 78234370Sjasone !(flags & DRM_MODE_FLAG_INTERLACE)) 79234370Sjasone mode->status = MODE_NO_INTERLACE; 80245868Sjasone if ((mode->flags & DRM_MODE_FLAG_DBLSCAN) && 81234370Sjasone !(flags & DRM_MODE_FLAG_DBLSCAN)) 82234370Sjasone mode->status = MODE_NO_DBLESCAN; 83234370Sjasone } 84234370Sjasone 85234370Sjasone return; 86245868Sjasone} 87234370Sjasone 88234370Sjasone/** 89234370Sjasone * drm_helper_probe_single_connector_modes - get complete set of display modes 90234370Sjasone * @dev: DRM device 91234370Sjasone * @maxX: max width for modes 92234370Sjasone * @maxY: max height for modes 93234370Sjasone * 94234370Sjasone * LOCKING: 95234370Sjasone * Caller must hold mode config lock. 96234370Sjasone * 97234370Sjasone * Based on @dev's mode_config layout, scan all the connectors and try to detect 98234370Sjasone * modes on them. Modes will first be added to the connector's probed_modes 99234370Sjasone * list, then culled (based on validity and the @maxX, @maxY parameters) and 100234370Sjasone * put into the normal modes list. 101234370Sjasone * 102234370Sjasone * Intended to be used either at bootup time or when major configuration 103234370Sjasone * changes have occurred. 104234370Sjasone * 105234370Sjasone * FIXME: take into account monitor limits 106234370Sjasone * 107234370Sjasone * RETURNS: 108234370Sjasone * Number of modes found on @connector. 109234370Sjasone */ 110234370Sjasoneint drm_helper_probe_single_connector_modes(struct drm_connector *connector, 111234370Sjasone uint32_t maxX, uint32_t maxY) 112234370Sjasone{ 113234370Sjasone struct drm_device *dev = connector->dev; 114234370Sjasone struct drm_display_mode *mode, *t; 115234370Sjasone struct drm_connector_helper_funcs *connector_funcs = 116234370Sjasone connector->helper_private; 117234370Sjasone struct drm_cmdline_mode cmdline_mode; 118234370Sjasone int count = 0; 119234370Sjasone int mode_flags = 0; 120234370Sjasone 121234370Sjasone DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n", connector->base.id, 122234370Sjasone drm_get_connector_name(connector)); 123234370Sjasone /* set all modes to the unverified state */ 124234370Sjasone list_for_each_entry_safe(mode, t, &connector->modes, head) 125234370Sjasone mode->status = MODE_UNVERIFIED; 126234370Sjasone 127234370Sjasone if (connector->force) { 128234370Sjasone if (connector->force == DRM_FORCE_ON) 129245868Sjasone connector->status = connector_status_connected; 130234370Sjasone else 131234370Sjasone connector->status = connector_status_disconnected; 132234370Sjasone if (connector->funcs->force) 133234370Sjasone connector->funcs->force(connector); 134234370Sjasone } else { 135234370Sjasone connector->status = connector->funcs->detect(connector, true); 136234370Sjasone drm_kms_helper_poll_enable(dev); 137234370Sjasone } 138234370Sjasone 139234370Sjasone if (connector->status == connector_status_disconnected) { 140234370Sjasone DRM_DEBUG_KMS("[CONNECTOR:%d:%s] disconnected\n", 141234370Sjasone connector->base.id, drm_get_connector_name(connector)); 142234370Sjasone drm_mode_connector_update_edid_property(connector, NULL); 143234370Sjasone goto prune; 144234370Sjasone } 145234370Sjasone 146234370Sjasone count = (*connector_funcs->get_modes)(connector); 147234370Sjasone if (count == 0 && drm_fetch_cmdline_mode_from_kenv(connector, 148234370Sjasone &cmdline_mode)) { 149234370Sjasone mode = drm_mode_create_from_cmdline_mode(dev, 150234370Sjasone &cmdline_mode); 151234370Sjasone if (mode != NULL) { 152234370Sjasone DRM_DEBUG_KMS( 153234370Sjasone "[CONNECTOR:%d:%s] found manual override ", 154234370Sjasone connector->base.id, 155234370Sjasone drm_get_connector_name(connector)); 156234370Sjasone drm_mode_debug_printmodeline(mode); 157234370Sjasone drm_mode_probed_add(connector, mode); 158245868Sjasone count++; 159245868Sjasone } else { 160234370Sjasone DRM_ERROR( 161245868Sjasone "[CONNECTOR:%d:%s] manual override mode: parse error\n", 162245868Sjasone connector->base.id, 163234370Sjasone drm_get_connector_name(connector)); 164234370Sjasone } 165234370Sjasone } 166234370Sjasone if (count == 0 && connector->status == connector_status_connected) 167234370Sjasone count = drm_add_modes_noedid(connector, 1024, 768); 168234370Sjasone if (count == 0) 169234370Sjasone goto prune; 170234370Sjasone 171234370Sjasone drm_mode_connector_list_update(connector); 172234370Sjasone 173234370Sjasone if (maxX && maxY) 174234370Sjasone drm_mode_validate_size(dev, &connector->modes, maxX, 175234370Sjasone maxY, 0); 176234370Sjasone 177234370Sjasone if (connector->interlace_allowed) 178234370Sjasone mode_flags |= DRM_MODE_FLAG_INTERLACE; 179234370Sjasone if (connector->doublescan_allowed) 180234370Sjasone mode_flags |= DRM_MODE_FLAG_DBLSCAN; 181234370Sjasone drm_mode_validate_flag(connector, mode_flags); 182234370Sjasone 183234370Sjasone list_for_each_entry_safe(mode, t, &connector->modes, head) { 184234370Sjasone if (mode->status == MODE_OK) 185234370Sjasone mode->status = connector_funcs->mode_valid(connector, 186234370Sjasone mode); 187234370Sjasone } 188234370Sjasone 189234370Sjasoneprune: 190234370Sjasone drm_mode_prune_invalid(dev, &connector->modes, true); 191234370Sjasone 192234370Sjasone if (list_empty(&connector->modes)) 193234370Sjasone return 0; 194234370Sjasone 195234370Sjasone drm_mode_sort(&connector->modes); 196245868Sjasone 197234370Sjasone DRM_DEBUG_KMS("[CONNECTOR:%d:%s] probed modes :\n", connector->base.id, 198234370Sjasone drm_get_connector_name(connector)); 199234370Sjasone list_for_each_entry_safe(mode, t, &connector->modes, head) { 200245868Sjasone mode->vrefresh = drm_mode_vrefresh(mode); 201234370Sjasone 202234370Sjasone drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V); 203245868Sjasone drm_mode_debug_printmodeline(mode); 204234370Sjasone } 205234370Sjasone 206234370Sjasone return count; 207234370Sjasone} 208245868Sjasone 209234370Sjasone/** 210234370Sjasone * drm_helper_encoder_in_use - check if a given encoder is in use 211234370Sjasone * @encoder: encoder to check 212234370Sjasone * 213234370Sjasone * LOCKING: 214234370Sjasone * Caller must hold mode config lock. 215234370Sjasone * 216234370Sjasone * Walk @encoders's DRM device's mode_config and see if it's in use. 217234370Sjasone * 218234370Sjasone * RETURNS: 219234370Sjasone * True if @encoder is part of the mode_config, false otherwise. 220234370Sjasone */ 221234370Sjasonebool drm_helper_encoder_in_use(struct drm_encoder *encoder) 222234370Sjasone{ 223234370Sjasone struct drm_connector *connector; 224234370Sjasone struct drm_device *dev = encoder->dev; 225234370Sjasone list_for_each_entry(connector, &dev->mode_config.connector_list, head) 226234370Sjasone if (connector->encoder == encoder) 227234370Sjasone return true; 228234370Sjasone return false; 229234370Sjasone} 230234370Sjasone 231234370Sjasone/** 232234370Sjasone * drm_helper_crtc_in_use - check if a given CRTC is in a mode_config 233234370Sjasone * @crtc: CRTC to check 234234370Sjasone * 235234370Sjasone * LOCKING: 236234370Sjasone * Caller must hold mode config lock. 237234370Sjasone * 238234370Sjasone * Walk @crtc's DRM device's mode_config and see if it's in use. 239234370Sjasone * 240234370Sjasone * RETURNS: 241234370Sjasone * True if @crtc is part of the mode_config, false otherwise. 242234370Sjasone */ 243234370Sjasonebool drm_helper_crtc_in_use(struct drm_crtc *crtc) 244234370Sjasone{ 245234370Sjasone struct drm_encoder *encoder; 246234370Sjasone struct drm_device *dev = crtc->dev; 247234370Sjasone /* FIXME: Locking around list access? */ 248234370Sjasone list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) 249234370Sjasone if (encoder->crtc == crtc && drm_helper_encoder_in_use(encoder)) 250234370Sjasone return true; 251234370Sjasone return false; 252234370Sjasone} 253234370Sjasone 254234370Sjasonestatic void 255234370Sjasonedrm_encoder_disable(struct drm_encoder *encoder) 256234370Sjasone{ 257234370Sjasone struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private; 258234370Sjasone 259234370Sjasone if (encoder_funcs->disable) 260234370Sjasone (*encoder_funcs->disable)(encoder); 261234370Sjasone else 262234370Sjasone (*encoder_funcs->dpms)(encoder, DRM_MODE_DPMS_OFF); 263234370Sjasone} 264234370Sjasone 265234370Sjasone/** 266234370Sjasone * drm_helper_disable_unused_functions - disable unused objects 267234370Sjasone * @dev: DRM device 268234370Sjasone * 269234370Sjasone * LOCKING: 270234370Sjasone * Caller must hold mode config lock. 271234370Sjasone * 272234370Sjasone * If an connector or CRTC isn't part of @dev's mode_config, it can be disabled 273234370Sjasone * by calling its dpms function, which should power it off. 274234370Sjasone */ 275234370Sjasonevoid drm_helper_disable_unused_functions(struct drm_device *dev) 276234370Sjasone{ 277234370Sjasone struct drm_encoder *encoder; 278234370Sjasone struct drm_connector *connector; 279234370Sjasone struct drm_crtc *crtc; 280234370Sjasone 281234370Sjasone list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 282234370Sjasone if (!connector->encoder) 283234370Sjasone continue; 284234370Sjasone if (connector->status == connector_status_disconnected) 285234370Sjasone connector->encoder = NULL; 286234370Sjasone } 287234370Sjasone 288234370Sjasone list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { 289234370Sjasone if (!drm_helper_encoder_in_use(encoder)) { 290234370Sjasone drm_encoder_disable(encoder); 291234370Sjasone /* disconnector encoder from any connector */ 292234370Sjasone encoder->crtc = NULL; 293234370Sjasone } 294234370Sjasone } 295234370Sjasone 296234370Sjasone list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 297234370Sjasone struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; 298234370Sjasone crtc->enabled = drm_helper_crtc_in_use(crtc); 299234370Sjasone if (!crtc->enabled) { 300234370Sjasone if (crtc_funcs->disable) 301234370Sjasone (*crtc_funcs->disable)(crtc); 302234370Sjasone else 303234370Sjasone (*crtc_funcs->dpms)(crtc, DRM_MODE_DPMS_OFF); 304234370Sjasone crtc->fb = NULL; 305234370Sjasone } 306234370Sjasone } 307234370Sjasone} 308234370Sjasone 309234370Sjasone/** 310234370Sjasone * drm_encoder_crtc_ok - can a given crtc drive a given encoder? 311234370Sjasone * @encoder: encoder to test 312234370Sjasone * @crtc: crtc to test 313234370Sjasone * 314234370Sjasone * Return false if @encoder can't be driven by @crtc, true otherwise. 315234370Sjasone */ 316234370Sjasonestatic bool drm_encoder_crtc_ok(struct drm_encoder *encoder, 317234370Sjasone struct drm_crtc *crtc) 318234370Sjasone{ 319234370Sjasone struct drm_device *dev; 320234370Sjasone struct drm_crtc *tmp; 321234370Sjasone int crtc_mask = 1; 322234370Sjasone 323234370Sjasone if (crtc == NULL) 324234370Sjasone printf("checking null crtc?\n"); 325234370Sjasone 326234370Sjasone dev = crtc->dev; 327234370Sjasone 328234370Sjasone list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) { 329234370Sjasone if (tmp == crtc) 330234370Sjasone break; 331234370Sjasone crtc_mask <<= 1; 332234370Sjasone } 333234370Sjasone 334234370Sjasone if (encoder->possible_crtcs & crtc_mask) 335234370Sjasone return true; 336234370Sjasone return false; 337234370Sjasone} 338234370Sjasone 339234370Sjasone/* 340234370Sjasone * Check the CRTC we're going to map each output to vs. its current 341234370Sjasone * CRTC. If they don't match, we have to disable the output and the CRTC 342234370Sjasone * since the driver will have to re-route things. 343234370Sjasone */ 344234370Sjasonestatic void 345234370Sjasonedrm_crtc_prepare_encoders(struct drm_device *dev) 346234370Sjasone{ 347234370Sjasone struct drm_encoder_helper_funcs *encoder_funcs; 348234370Sjasone struct drm_encoder *encoder; 349234370Sjasone 350234370Sjasone list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { 351234370Sjasone encoder_funcs = encoder->helper_private; 352234370Sjasone /* Disable unused encoders */ 353234370Sjasone if (encoder->crtc == NULL) 354234370Sjasone drm_encoder_disable(encoder); 355234370Sjasone /* Disable encoders whose CRTC is about to change */ 356234370Sjasone if (encoder_funcs->get_crtc && 357234370Sjasone encoder->crtc != (*encoder_funcs->get_crtc)(encoder)) 358234370Sjasone drm_encoder_disable(encoder); 359234370Sjasone } 360234370Sjasone} 361234370Sjasone 362234370Sjasone/** 363234370Sjasone * drm_crtc_set_mode - set a mode 364234370Sjasone * @crtc: CRTC to program 365234370Sjasone * @mode: mode to use 366234370Sjasone * @x: width of mode 367234370Sjasone * @y: height of mode 368234370Sjasone * 369234370Sjasone * LOCKING: 370234370Sjasone * Caller must hold mode config lock. 371234370Sjasone * 372234370Sjasone * Try to set @mode on @crtc. Give @crtc and its associated connectors a chance 373234370Sjasone * to fixup or reject the mode prior to trying to set it. 374234370Sjasone * 375234370Sjasone * RETURNS: 376234370Sjasone * True if the mode was set successfully, or false otherwise. 377234370Sjasone */ 378234370Sjasonebool drm_crtc_helper_set_mode(struct drm_crtc *crtc, 379234370Sjasone struct drm_display_mode *mode, 380234370Sjasone int x, int y, 381234370Sjasone struct drm_framebuffer *old_fb) 382234370Sjasone{ 383234370Sjasone struct drm_device *dev = crtc->dev; 384234370Sjasone struct drm_display_mode *adjusted_mode, saved_mode, saved_hwmode; 385234370Sjasone struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; 386234370Sjasone struct drm_encoder_helper_funcs *encoder_funcs; 387234370Sjasone int saved_x, saved_y; 388234370Sjasone struct drm_encoder *encoder; 389234370Sjasone bool ret = true; 390234370Sjasone 391234370Sjasone crtc->enabled = drm_helper_crtc_in_use(crtc); 392234370Sjasone if (!crtc->enabled) 393234370Sjasone return true; 394234370Sjasone 395234370Sjasone adjusted_mode = drm_mode_duplicate(dev, mode); 396234370Sjasone if (!adjusted_mode) 397234370Sjasone return false; 398234370Sjasone 399234370Sjasone saved_hwmode = crtc->hwmode; 400234370Sjasone saved_mode = crtc->mode; 401234370Sjasone saved_x = crtc->x; 402234370Sjasone saved_y = crtc->y; 403234370Sjasone 404234370Sjasone /* Update crtc values up front so the driver can rely on them for mode 405234370Sjasone * setting. 406234370Sjasone */ 407234370Sjasone crtc->mode = *mode; 408234370Sjasone crtc->x = x; 409234370Sjasone crtc->y = y; 410234370Sjasone 411234370Sjasone /* Pass our mode to the connectors and the CRTC to give them a chance to 412234370Sjasone * adjust it according to limitations or connector properties, and also 413234370Sjasone * a chance to reject the mode entirely. 414234370Sjasone */ 415234370Sjasone list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { 416234370Sjasone 417234370Sjasone if (encoder->crtc != crtc) 418234370Sjasone continue; 419234370Sjasone encoder_funcs = encoder->helper_private; 420234370Sjasone if (!(ret = encoder_funcs->mode_fixup(encoder, mode, 421245868Sjasone adjusted_mode))) { 422245868Sjasone goto done; 423234370Sjasone } 424234370Sjasone } 425234370Sjasone 426234370Sjasone if (!(ret = crtc_funcs->mode_fixup(crtc, mode, adjusted_mode))) { 427234370Sjasone goto done; 428234370Sjasone } 429234370Sjasone DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id); 430234370Sjasone 431234370Sjasone /* Prepare the encoders and CRTCs before setting the mode. */ 432234370Sjasone list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { 433234370Sjasone 434234370Sjasone if (encoder->crtc != crtc) 435234370Sjasone continue; 436234370Sjasone encoder_funcs = encoder->helper_private; 437234370Sjasone /* Disable the encoders as the first thing we do. */ 438234370Sjasone encoder_funcs->prepare(encoder); 439234370Sjasone } 440234370Sjasone 441234370Sjasone drm_crtc_prepare_encoders(dev); 442234370Sjasone 443234370Sjasone crtc_funcs->prepare(crtc); 444234370Sjasone 445234370Sjasone /* Set up the DPLL and any encoders state that needs to adjust or depend 446234370Sjasone * on the DPLL. 447234370Sjasone */ 448234370Sjasone ret = !crtc_funcs->mode_set(crtc, mode, adjusted_mode, x, y, old_fb); 449234370Sjasone if (!ret) 450234370Sjasone goto done; 451234370Sjasone 452234370Sjasone list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { 453234370Sjasone 454234370Sjasone if (encoder->crtc != crtc) 455234370Sjasone continue; 456234370Sjasone 457234370Sjasone DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n", 458234370Sjasone encoder->base.id, drm_get_encoder_name(encoder), 459234370Sjasone mode->base.id, mode->name); 460234370Sjasone encoder_funcs = encoder->helper_private; 461234370Sjasone encoder_funcs->mode_set(encoder, mode, adjusted_mode); 462234370Sjasone } 463234370Sjasone 464234370Sjasone /* Now enable the clocks, plane, pipe, and connectors that we set up. */ 465234370Sjasone crtc_funcs->commit(crtc); 466234370Sjasone 467234370Sjasone list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { 468234370Sjasone 469234370Sjasone if (encoder->crtc != crtc) 470234370Sjasone continue; 471234370Sjasone 472234370Sjasone encoder_funcs = encoder->helper_private; 473234370Sjasone encoder_funcs->commit(encoder); 474234370Sjasone 475234370Sjasone } 476234370Sjasone 477234370Sjasone /* Store real post-adjustment hardware mode. */ 478234370Sjasone crtc->hwmode = *adjusted_mode; 479234370Sjasone 480234370Sjasone /* Calculate and store various constants which 481234370Sjasone * are later needed by vblank and swap-completion 482234370Sjasone * timestamping. They are derived from true hwmode. 483234370Sjasone */ 484234370Sjasone drm_calc_timestamping_constants(crtc); 485234370Sjasone 486234370Sjasone /* FIXME: add subpixel order */ 487234370Sjasonedone: 488234370Sjasone drm_mode_destroy(dev, adjusted_mode); 489234370Sjasone if (!ret) { 490234370Sjasone crtc->hwmode = saved_hwmode; 491234370Sjasone crtc->mode = saved_mode; 492234370Sjasone crtc->x = saved_x; 493234370Sjasone crtc->y = saved_y; 494234370Sjasone } 495234370Sjasone 496234370Sjasone return ret; 497234370Sjasone} 498234370Sjasone 499234370Sjasonestatic int 500234370Sjasonedrm_crtc_helper_disable(struct drm_crtc *crtc) 501234370Sjasone{ 502234370Sjasone struct drm_device *dev = crtc->dev; 503234370Sjasone struct drm_connector *connector; 504234370Sjasone struct drm_encoder *encoder; 505234370Sjasone 506234370Sjasone /* Decouple all encoders and their attached connectors from this crtc */ 507234370Sjasone list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { 508234370Sjasone if (encoder->crtc != crtc) 509234370Sjasone continue; 510234370Sjasone 511234370Sjasone list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 512234370Sjasone if (connector->encoder != encoder) 513234370Sjasone continue; 514234370Sjasone 515234370Sjasone connector->encoder = NULL; 516234370Sjasone } 517234370Sjasone } 518234370Sjasone 519234370Sjasone drm_helper_disable_unused_functions(dev); 520234370Sjasone return 0; 521234370Sjasone} 522234370Sjasone 523234370Sjasone/** 524234370Sjasone * drm_crtc_helper_set_config - set a new config from userspace 525234370Sjasone * @crtc: CRTC to setup 526234370Sjasone * @crtc_info: user provided configuration 527234370Sjasone * @new_mode: new mode to set 528234370Sjasone * @connector_set: set of connectors for the new config 529245868Sjasone * @fb: new framebuffer 530234370Sjasone * 531234370Sjasone * LOCKING: 532245868Sjasone * Caller must hold mode config lock. 533234370Sjasone * 534234370Sjasone * Setup a new configuration, provided by the user in @crtc_info, and enable 535234370Sjasone * it. 536234370Sjasone * 537234370Sjasone * RETURNS: 538234370Sjasone * Zero. (FIXME) 539234370Sjasone */ 540234370Sjasoneint drm_crtc_helper_set_config(struct drm_mode_set *set) 541234370Sjasone{ 542234370Sjasone struct drm_device *dev; 543234370Sjasone struct drm_crtc *save_crtcs, *new_crtc, *crtc; 544234370Sjasone struct drm_encoder *save_encoders, *new_encoder, *encoder; 545234370Sjasone struct drm_framebuffer *old_fb = NULL; 546245868Sjasone bool mode_changed = false; /* if true do a full mode set */ 547234370Sjasone bool fb_changed = false; /* if true and !mode_changed just do a flip */ 548234370Sjasone struct drm_connector *save_connectors, *connector; 549234370Sjasone int count = 0, ro, fail = 0; 550245868Sjasone struct drm_crtc_helper_funcs *crtc_funcs; 551234370Sjasone struct drm_mode_set save_set; 552234370Sjasone int ret = 0; 553234370Sjasone int i; 554234370Sjasone 555245868Sjasone DRM_DEBUG_KMS("\n"); 556234370Sjasone 557234370Sjasone if (!set) 558234370Sjasone return -EINVAL; 559234370Sjasone 560234370Sjasone if (!set->crtc) 561234370Sjasone return -EINVAL; 562234370Sjasone 563234370Sjasone if (!set->crtc->helper_private) 564 return -EINVAL; 565 566 crtc_funcs = set->crtc->helper_private; 567 568 if (!set->mode) 569 set->fb = NULL; 570 571 if (set->fb) { 572 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n", 573 set->crtc->base.id, set->fb->base.id, 574 (int)set->num_connectors, set->x, set->y); 575 } else { 576 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id); 577 return drm_crtc_helper_disable(set->crtc); 578 } 579 580 dev = set->crtc->dev; 581 582 /* Allocate space for the backup of all (non-pointer) crtc, encoder and 583 * connector data. */ 584 save_crtcs = malloc(dev->mode_config.num_crtc * sizeof(struct drm_crtc), 585 DRM_MEM_KMS, M_WAITOK | M_ZERO); 586 save_encoders = malloc(dev->mode_config.num_encoder * 587 sizeof(struct drm_encoder), DRM_MEM_KMS, M_WAITOK | M_ZERO); 588 save_connectors = malloc(dev->mode_config.num_connector * 589 sizeof(struct drm_connector), DRM_MEM_KMS, M_WAITOK | M_ZERO); 590 591 /* Copy data. Note that driver private data is not affected. 592 * Should anything bad happen only the expected state is 593 * restored, not the drivers personal bookkeeping. 594 */ 595 count = 0; 596 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 597 save_crtcs[count++] = *crtc; 598 } 599 600 count = 0; 601 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { 602 save_encoders[count++] = *encoder; 603 } 604 605 count = 0; 606 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 607 save_connectors[count++] = *connector; 608 } 609 610 save_set.crtc = set->crtc; 611 save_set.mode = &set->crtc->mode; 612 save_set.x = set->crtc->x; 613 save_set.y = set->crtc->y; 614 save_set.fb = set->crtc->fb; 615 616 /* We should be able to check here if the fb has the same properties 617 * and then just flip_or_move it */ 618 if (set->crtc->fb != set->fb) { 619 /* If we have no fb then treat it as a full mode set */ 620 if (set->crtc->fb == NULL) { 621 DRM_DEBUG_KMS("crtc has no fb, full mode set\n"); 622 mode_changed = true; 623 } else if (set->fb == NULL) { 624 mode_changed = true; 625 } else 626 fb_changed = true; 627 } 628 629 if (set->x != set->crtc->x || set->y != set->crtc->y) 630 fb_changed = true; 631 632 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) { 633 DRM_DEBUG_KMS("modes are different, full mode set\n"); 634 drm_mode_debug_printmodeline(&set->crtc->mode); 635 drm_mode_debug_printmodeline(set->mode); 636 mode_changed = true; 637 } 638 639 /* a) traverse passed in connector list and get encoders for them */ 640 count = 0; 641 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 642 struct drm_connector_helper_funcs *connector_funcs = 643 connector->helper_private; 644 new_encoder = connector->encoder; 645 for (ro = 0; ro < set->num_connectors; ro++) { 646 if (set->connectors[ro] == connector) { 647 new_encoder = connector_funcs->best_encoder(connector); 648 /* if we can't get an encoder for a connector 649 we are setting now - then fail */ 650 if (new_encoder == NULL) 651 /* don't break so fail path works correct */ 652 fail = 1; 653 break; 654 } 655 } 656 657 if (new_encoder != connector->encoder) { 658 DRM_DEBUG_KMS("encoder changed, full mode switch\n"); 659 mode_changed = true; 660 /* If the encoder is reused for another connector, then 661 * the appropriate crtc will be set later. 662 */ 663 if (connector->encoder) 664 connector->encoder->crtc = NULL; 665 connector->encoder = new_encoder; 666 } 667 } 668 669 if (fail) { 670 ret = -EINVAL; 671 goto fail; 672 } 673 674 count = 0; 675 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 676 if (!connector->encoder) 677 continue; 678 679 if (connector->encoder->crtc == set->crtc) 680 new_crtc = NULL; 681 else 682 new_crtc = connector->encoder->crtc; 683 684 for (ro = 0; ro < set->num_connectors; ro++) { 685 if (set->connectors[ro] == connector) 686 new_crtc = set->crtc; 687 } 688 689 /* Make sure the new CRTC will work with the encoder */ 690 if (new_crtc && 691 !drm_encoder_crtc_ok(connector->encoder, new_crtc)) { 692 ret = -EINVAL; 693 goto fail; 694 } 695 if (new_crtc != connector->encoder->crtc) { 696 DRM_DEBUG_KMS("crtc changed, full mode switch\n"); 697 mode_changed = true; 698 connector->encoder->crtc = new_crtc; 699 } 700 if (new_crtc) { 701 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n", 702 connector->base.id, drm_get_connector_name(connector), 703 new_crtc->base.id); 704 } else { 705 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n", 706 connector->base.id, drm_get_connector_name(connector)); 707 } 708 } 709 710 /* mode_set_base is not a required function */ 711 if (fb_changed && !crtc_funcs->mode_set_base) 712 mode_changed = true; 713 714 if (mode_changed) { 715 set->crtc->enabled = drm_helper_crtc_in_use(set->crtc); 716 if (set->crtc->enabled) { 717 DRM_DEBUG_KMS("attempting to set mode from" 718 " userspace\n"); 719 drm_mode_debug_printmodeline(set->mode); 720 old_fb = set->crtc->fb; 721 set->crtc->fb = set->fb; 722 if (!drm_crtc_helper_set_mode(set->crtc, set->mode, 723 set->x, set->y, 724 old_fb)) { 725 DRM_ERROR("failed to set mode on [CRTC:%d]\n", 726 set->crtc->base.id); 727 set->crtc->fb = old_fb; 728 ret = -EINVAL; 729 goto fail; 730 } 731 DRM_DEBUG_KMS("Setting connector DPMS state to on\n"); 732 for (i = 0; i < set->num_connectors; i++) { 733 DRM_DEBUG_KMS("\t[CONNECTOR:%d:%s] set DPMS on\n", set->connectors[i]->base.id, 734 drm_get_connector_name(set->connectors[i])); 735 set->connectors[i]->dpms = DRM_MODE_DPMS_ON; 736 } 737 } 738 drm_helper_disable_unused_functions(dev); 739 } else if (fb_changed) { 740 set->crtc->x = set->x; 741 set->crtc->y = set->y; 742 743 old_fb = set->crtc->fb; 744 if (set->crtc->fb != set->fb) 745 set->crtc->fb = set->fb; 746 ret = crtc_funcs->mode_set_base(set->crtc, 747 set->x, set->y, old_fb); 748 if (ret != 0) { 749 set->crtc->fb = old_fb; 750 goto fail; 751 } 752 } 753 754 free(save_connectors, DRM_MEM_KMS); 755 free(save_encoders, DRM_MEM_KMS); 756 free(save_crtcs, DRM_MEM_KMS); 757 return 0; 758 759fail: 760 /* Restore all previous data. */ 761 count = 0; 762 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 763 *crtc = save_crtcs[count++]; 764 } 765 766 count = 0; 767 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { 768 *encoder = save_encoders[count++]; 769 } 770 771 count = 0; 772 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 773 *connector = save_connectors[count++]; 774 } 775 776 /* Try to restore the config */ 777 if (mode_changed && 778 !drm_crtc_helper_set_mode(save_set.crtc, save_set.mode, save_set.x, 779 save_set.y, save_set.fb)) 780 DRM_ERROR("failed to restore config after modeset failure\n"); 781 782 free(save_connectors, DRM_MEM_KMS); 783 free(save_encoders, DRM_MEM_KMS); 784 free(save_crtcs, DRM_MEM_KMS); 785 return ret; 786} 787 788static int drm_helper_choose_encoder_dpms(struct drm_encoder *encoder) 789{ 790 int dpms = DRM_MODE_DPMS_OFF; 791 struct drm_connector *connector; 792 struct drm_device *dev = encoder->dev; 793 794 list_for_each_entry(connector, &dev->mode_config.connector_list, head) 795 if (connector->encoder == encoder) 796 if (connector->dpms < dpms) 797 dpms = connector->dpms; 798 return dpms; 799} 800 801static int drm_helper_choose_crtc_dpms(struct drm_crtc *crtc) 802{ 803 int dpms = DRM_MODE_DPMS_OFF; 804 struct drm_connector *connector; 805 struct drm_device *dev = crtc->dev; 806 807 list_for_each_entry(connector, &dev->mode_config.connector_list, head) 808 if (connector->encoder && connector->encoder->crtc == crtc) 809 if (connector->dpms < dpms) 810 dpms = connector->dpms; 811 return dpms; 812} 813 814/** 815 * drm_helper_connector_dpms 816 * @connector affected connector 817 * @mode DPMS mode 818 * 819 * Calls the low-level connector DPMS function, then 820 * calls appropriate encoder and crtc DPMS functions as well 821 */ 822void drm_helper_connector_dpms(struct drm_connector *connector, int mode) 823{ 824 struct drm_encoder *encoder = connector->encoder; 825 struct drm_crtc *crtc = encoder ? encoder->crtc : NULL; 826 int old_dpms; 827 828 if (mode == connector->dpms) 829 return; 830 831 old_dpms = connector->dpms; 832 connector->dpms = mode; 833 834 /* from off to on, do crtc then encoder */ 835 if (mode < old_dpms) { 836 if (crtc) { 837 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; 838 if (crtc_funcs->dpms) 839 (*crtc_funcs->dpms) (crtc, 840 drm_helper_choose_crtc_dpms(crtc)); 841 } 842 if (encoder) { 843 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private; 844 if (encoder_funcs->dpms) 845 (*encoder_funcs->dpms) (encoder, 846 drm_helper_choose_encoder_dpms(encoder)); 847 } 848 } 849 850 /* from on to off, do encoder then crtc */ 851 if (mode > old_dpms) { 852 if (encoder) { 853 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private; 854 if (encoder_funcs->dpms) 855 (*encoder_funcs->dpms) (encoder, 856 drm_helper_choose_encoder_dpms(encoder)); 857 } 858 if (crtc) { 859 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private; 860 if (crtc_funcs->dpms) 861 (*crtc_funcs->dpms) (crtc, 862 drm_helper_choose_crtc_dpms(crtc)); 863 } 864 } 865 866 return; 867} 868 869int drm_helper_mode_fill_fb_struct(struct drm_framebuffer *fb, 870 struct drm_mode_fb_cmd2 *mode_cmd) 871{ 872 int i; 873 874 fb->width = mode_cmd->width; 875 fb->height = mode_cmd->height; 876 for (i = 0; i < 4; i++) { 877 fb->pitches[i] = mode_cmd->pitches[i]; 878 fb->offsets[i] = mode_cmd->offsets[i]; 879 } 880 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &fb->depth, 881 &fb->bits_per_pixel); 882 fb->pixel_format = mode_cmd->pixel_format; 883 884 return 0; 885} 886 887int drm_helper_resume_force_mode(struct drm_device *dev) 888{ 889 struct drm_crtc *crtc; 890 struct drm_encoder *encoder; 891 struct drm_encoder_helper_funcs *encoder_funcs; 892 struct drm_crtc_helper_funcs *crtc_funcs; 893 int ret; 894 895 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 896 897 if (!crtc->enabled) 898 continue; 899 900 ret = drm_crtc_helper_set_mode(crtc, &crtc->mode, 901 crtc->x, crtc->y, crtc->fb); 902 903 if (!ret) 904 DRM_ERROR("failed to set mode on crtc %p\n", crtc); 905 906 /* Turn off outputs that were already powered off */ 907 if (drm_helper_choose_crtc_dpms(crtc)) { 908 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { 909 910 if(encoder->crtc != crtc) 911 continue; 912 913 encoder_funcs = encoder->helper_private; 914 if (encoder_funcs->dpms) 915 (*encoder_funcs->dpms) (encoder, 916 drm_helper_choose_encoder_dpms(encoder)); 917 } 918 919 crtc_funcs = crtc->helper_private; 920 if (crtc_funcs->dpms) 921 (*crtc_funcs->dpms) (crtc, 922 drm_helper_choose_crtc_dpms(crtc)); 923 } 924 } 925 /* disable the unused connectors while restoring the modesetting */ 926 drm_helper_disable_unused_functions(dev); 927 return 0; 928} 929 930#define DRM_OUTPUT_POLL_PERIOD (10 * hz) 931static void output_poll_execute(void *ctx, int pending) 932{ 933 struct drm_device *dev; 934 struct drm_connector *connector; 935 enum drm_connector_status old_status; 936 bool repoll = false, changed = false; 937 938 if (!drm_kms_helper_poll) 939 return; 940 941 dev = ctx; 942 943 sx_xlock(&dev->mode_config.mutex); 944 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 945 946 /* if this is HPD or polled don't check it - 947 TV out for instance */ 948 if (!connector->polled) 949 continue; 950 951 else if (connector->polled & (DRM_CONNECTOR_POLL_CONNECT | 952 DRM_CONNECTOR_POLL_DISCONNECT)) 953 repoll = true; 954 955 old_status = connector->status; 956 /* if we are connected and don't want to poll for disconnect 957 skip it */ 958 if (old_status == connector_status_connected && 959 !(connector->polled & DRM_CONNECTOR_POLL_DISCONNECT) && 960 !(connector->polled & DRM_CONNECTOR_POLL_HPD)) 961 continue; 962 963 connector->status = connector->funcs->detect(connector, false); 964 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %d to %d\n", 965 connector->base.id, 966 drm_get_connector_name(connector), 967 old_status, connector->status); 968 if (old_status != connector->status) 969 changed = true; 970 } 971 972 sx_xunlock(&dev->mode_config.mutex); 973 974 if (changed) { 975#if 0 976 /* send a uevent + call fbdev */ 977 drm_sysfs_hotplug_event(dev); 978#endif 979 if (dev->mode_config.funcs->output_poll_changed) 980 dev->mode_config.funcs->output_poll_changed(dev); 981 } 982 983 if (repoll) { 984 taskqueue_enqueue_timeout(taskqueue_thread, 985 &dev->mode_config.output_poll_task, 986 DRM_OUTPUT_POLL_PERIOD); 987 } 988} 989 990void drm_kms_helper_poll_disable(struct drm_device *dev) 991{ 992 if (!dev->mode_config.poll_enabled) 993 return; 994 taskqueue_cancel_timeout(taskqueue_thread, 995 &dev->mode_config.output_poll_task, NULL); 996} 997 998void drm_kms_helper_poll_enable(struct drm_device *dev) 999{ 1000 bool poll = false; 1001 struct drm_connector *connector; 1002 1003 if (!dev->mode_config.poll_enabled || !drm_kms_helper_poll) 1004 return; 1005 1006 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 1007 if (connector->polled) 1008 poll = true; 1009 } 1010 1011 if (poll) { 1012 taskqueue_enqueue_timeout(taskqueue_thread, 1013 &dev->mode_config.output_poll_task, DRM_OUTPUT_POLL_PERIOD); 1014 } 1015} 1016 1017void drm_kms_helper_poll_init(struct drm_device *dev) 1018{ 1019 1020 TIMEOUT_TASK_INIT(taskqueue_thread, &dev->mode_config.output_poll_task, 1021 0, output_poll_execute, dev); 1022 dev->mode_config.poll_enabled = true; 1023 1024 drm_kms_helper_poll_enable(dev); 1025} 1026 1027void drm_kms_helper_poll_fini(struct drm_device *dev) 1028{ 1029 drm_kms_helper_poll_disable(dev); 1030} 1031 1032void drm_helper_hpd_irq_event(struct drm_device *dev) 1033{ 1034 if (!dev->mode_config.poll_enabled) 1035 return; 1036 1037 /* kill timer and schedule immediate execution, this doesn't block */ 1038 taskqueue_cancel_timeout(taskqueue_thread, 1039 &dev->mode_config.output_poll_task, NULL); 1040 if (drm_kms_helper_poll) 1041 taskqueue_enqueue_timeout(taskqueue_thread, 1042 &dev->mode_config.output_poll_task, 0); 1043} 1044