1 /* 2 * Copyright 2008 Intel Corporation <hong.liu@intel.com> 3 * Copyright 2008 Red Hat <mjg@redhat.com> 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining 6 * a copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sub license, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the 14 * next paragraph) shall be included in all copies or substantial 15 * portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 * NON-INFRINGEMENT. IN NO EVENT SHALL INTEL AND/OR ITS SUPPLIERS BE 21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 * SOFTWARE. 25 * 26 */ 27 28 #include <linux/acpi.h> 29 #include <linux/dmi.h> 30 #include <linux/firmware.h> 31 #include <acpi/video.h> 32 33 #include "i915_drv.h" 34 #include "intel_acpi.h" 35 #include "intel_backlight.h" 36 #include "intel_display_types.h" 37 #include "intel_opregion.h" 38 39 #define OPREGION_HEADER_OFFSET 0 40 #define OPREGION_ACPI_OFFSET 0x100 41 #define ACPI_CLID 0x01ac /* current lid state indicator */ 42 #define ACPI_CDCK 0x01b0 /* current docking state indicator */ 43 #define OPREGION_SWSCI_OFFSET 0x200 44 #define OPREGION_ASLE_OFFSET 0x300 45 #define OPREGION_VBT_OFFSET 0x400 46 #define OPREGION_ASLE_EXT_OFFSET 0x1C00 47 48 #define OPREGION_SIGNATURE "IntelGraphicsMem" 49 #define MBOX_ACPI (1<<0) 50 #define MBOX_SWSCI (1<<1) 51 #define MBOX_ASLE (1<<2) 52 #define MBOX_ASLE_EXT (1<<4) 53 54 struct opregion_header { 55 u8 signature[16]; 56 u32 size; 57 struct { 58 u8 rsvd; 59 u8 revision; 60 u8 minor; 61 u8 major; 62 } __packed over; 63 u8 bios_ver[32]; 64 u8 vbios_ver[16]; 65 u8 driver_ver[16]; 66 u32 mboxes; 67 u32 driver_model; 68 u32 pcon; 69 u8 dver[32]; 70 u8 rsvd[124]; 71 } __packed; 72 73 /* OpRegion mailbox #1: public ACPI methods */ 74 struct opregion_acpi { 75 u32 drdy; /* driver readiness */ 76 u32 csts; /* notification status */ 77 u32 cevt; /* current event */ 78 u8 rsvd1[20]; 79 u32 didl[8]; /* supported display devices ID list */ 80 u32 cpdl[8]; /* currently presented display list */ 81 u32 cadl[8]; /* currently active display list */ 82 u32 nadl[8]; /* next active devices list */ 83 u32 aslp; /* ASL sleep time-out */ 84 u32 tidx; /* toggle table index */ 85 u32 chpd; /* current hotplug enable indicator */ 86 u32 clid; /* current lid state*/ 87 u32 cdck; /* current docking state */ 88 u32 sxsw; /* Sx state resume */ 89 u32 evts; /* ASL supported events */ 90 u32 cnot; /* current OS notification */ 91 u32 nrdy; /* driver status */ 92 u32 did2[7]; /* extended supported display devices ID list */ 93 u32 cpd2[7]; /* extended attached display devices list */ 94 u8 rsvd2[4]; 95 } __packed; 96 97 /* OpRegion mailbox #2: SWSCI */ 98 struct opregion_swsci { 99 u32 scic; /* SWSCI command|status|data */ 100 u32 parm; /* command parameters */ 101 u32 dslp; /* driver sleep time-out */ 102 u8 rsvd[244]; 103 } __packed; 104 105 /* OpRegion mailbox #3: ASLE */ 106 struct opregion_asle { 107 u32 ardy; /* driver readiness */ 108 u32 aslc; /* ASLE interrupt command */ 109 u32 tche; /* technology enabled indicator */ 110 u32 alsi; /* current ALS illuminance reading */ 111 u32 bclp; /* backlight brightness to set */ 112 u32 pfit; /* panel fitting state */ 113 u32 cblv; /* current brightness level */ 114 u16 bclm[20]; /* backlight level duty cycle mapping table */ 115 u32 cpfm; /* current panel fitting mode */ 116 u32 epfm; /* enabled panel fitting modes */ 117 u8 plut[74]; /* panel LUT and identifier */ 118 u32 pfmb; /* PWM freq and min brightness */ 119 u32 cddv; /* color correction default values */ 120 u32 pcft; /* power conservation features */ 121 u32 srot; /* supported rotation angles */ 122 u32 iuer; /* IUER events */ 123 u64 fdss; 124 u32 fdsp; 125 u32 stat; 126 u64 rvda; /* Physical (2.0) or relative from opregion (2.1+) 127 * address of raw VBT data. */ 128 u32 rvds; /* Size of raw vbt data */ 129 u8 rsvd[58]; 130 } __packed; 131 132 /* OpRegion mailbox #5: ASLE ext */ 133 struct opregion_asle_ext { 134 u32 phed; /* Panel Header */ 135 u8 bddc[256]; /* Panel EDID */ 136 u8 rsvd[764]; 137 } __packed; 138 139 /* Driver readiness indicator */ 140 #define ASLE_ARDY_READY (1 << 0) 141 #define ASLE_ARDY_NOT_READY (0 << 0) 142 143 /* ASLE Interrupt Command (ASLC) bits */ 144 #define ASLC_SET_ALS_ILLUM (1 << 0) 145 #define ASLC_SET_BACKLIGHT (1 << 1) 146 #define ASLC_SET_PFIT (1 << 2) 147 #define ASLC_SET_PWM_FREQ (1 << 3) 148 #define ASLC_SUPPORTED_ROTATION_ANGLES (1 << 4) 149 #define ASLC_BUTTON_ARRAY (1 << 5) 150 #define ASLC_CONVERTIBLE_INDICATOR (1 << 6) 151 #define ASLC_DOCKING_INDICATOR (1 << 7) 152 #define ASLC_ISCT_STATE_CHANGE (1 << 8) 153 #define ASLC_REQ_MSK 0x1ff 154 /* response bits */ 155 #define ASLC_ALS_ILLUM_FAILED (1 << 10) 156 #define ASLC_BACKLIGHT_FAILED (1 << 12) 157 #define ASLC_PFIT_FAILED (1 << 14) 158 #define ASLC_PWM_FREQ_FAILED (1 << 16) 159 #define ASLC_ROTATION_ANGLES_FAILED (1 << 18) 160 #define ASLC_BUTTON_ARRAY_FAILED (1 << 20) 161 #define ASLC_CONVERTIBLE_FAILED (1 << 22) 162 #define ASLC_DOCKING_FAILED (1 << 24) 163 #define ASLC_ISCT_STATE_FAILED (1 << 26) 164 165 /* Technology enabled indicator */ 166 #define ASLE_TCHE_ALS_EN (1 << 0) 167 #define ASLE_TCHE_BLC_EN (1 << 1) 168 #define ASLE_TCHE_PFIT_EN (1 << 2) 169 #define ASLE_TCHE_PFMB_EN (1 << 3) 170 171 /* ASLE backlight brightness to set */ 172 #define ASLE_BCLP_VALID (1<<31) 173 #define ASLE_BCLP_MSK (~(1<<31)) 174 175 /* ASLE panel fitting request */ 176 #define ASLE_PFIT_VALID (1<<31) 177 #define ASLE_PFIT_CENTER (1<<0) 178 #define ASLE_PFIT_STRETCH_TEXT (1<<1) 179 #define ASLE_PFIT_STRETCH_GFX (1<<2) 180 181 /* PWM frequency and minimum brightness */ 182 #define ASLE_PFMB_BRIGHTNESS_MASK (0xff) 183 #define ASLE_PFMB_BRIGHTNESS_VALID (1<<8) 184 #define ASLE_PFMB_PWM_MASK (0x7ffffe00) 185 #define ASLE_PFMB_PWM_VALID (1<<31) 186 187 #define ASLE_CBLV_VALID (1<<31) 188 189 /* IUER */ 190 #define ASLE_IUER_DOCKING (1 << 7) 191 #define ASLE_IUER_CONVERTIBLE (1 << 6) 192 #define ASLE_IUER_ROTATION_LOCK_BTN (1 << 4) 193 #define ASLE_IUER_VOLUME_DOWN_BTN (1 << 3) 194 #define ASLE_IUER_VOLUME_UP_BTN (1 << 2) 195 #define ASLE_IUER_WINDOWS_BTN (1 << 1) 196 #define ASLE_IUER_POWER_BTN (1 << 0) 197 198 /* Software System Control Interrupt (SWSCI) */ 199 #define SWSCI_SCIC_INDICATOR (1 << 0) 200 #define SWSCI_SCIC_MAIN_FUNCTION_SHIFT 1 201 #define SWSCI_SCIC_MAIN_FUNCTION_MASK (0xf << 1) 202 #define SWSCI_SCIC_SUB_FUNCTION_SHIFT 8 203 #define SWSCI_SCIC_SUB_FUNCTION_MASK (0xff << 8) 204 #define SWSCI_SCIC_EXIT_PARAMETER_SHIFT 8 205 #define SWSCI_SCIC_EXIT_PARAMETER_MASK (0xff << 8) 206 #define SWSCI_SCIC_EXIT_STATUS_SHIFT 5 207 #define SWSCI_SCIC_EXIT_STATUS_MASK (7 << 5) 208 #define SWSCI_SCIC_EXIT_STATUS_SUCCESS 1 209 210 #define SWSCI_FUNCTION_CODE(main, sub) \ 211 ((main) << SWSCI_SCIC_MAIN_FUNCTION_SHIFT | \ 212 (sub) << SWSCI_SCIC_SUB_FUNCTION_SHIFT) 213 214 /* SWSCI: Get BIOS Data (GBDA) */ 215 #define SWSCI_GBDA 4 216 #define SWSCI_GBDA_SUPPORTED_CALLS SWSCI_FUNCTION_CODE(SWSCI_GBDA, 0) 217 #define SWSCI_GBDA_REQUESTED_CALLBACKS SWSCI_FUNCTION_CODE(SWSCI_GBDA, 1) 218 #define SWSCI_GBDA_BOOT_DISPLAY_PREF SWSCI_FUNCTION_CODE(SWSCI_GBDA, 4) 219 #define SWSCI_GBDA_PANEL_DETAILS SWSCI_FUNCTION_CODE(SWSCI_GBDA, 5) 220 #define SWSCI_GBDA_TV_STANDARD SWSCI_FUNCTION_CODE(SWSCI_GBDA, 6) 221 #define SWSCI_GBDA_INTERNAL_GRAPHICS SWSCI_FUNCTION_CODE(SWSCI_GBDA, 7) 222 #define SWSCI_GBDA_SPREAD_SPECTRUM SWSCI_FUNCTION_CODE(SWSCI_GBDA, 10) 223 224 /* SWSCI: System BIOS Callbacks (SBCB) */ 225 #define SWSCI_SBCB 6 226 #define SWSCI_SBCB_SUPPORTED_CALLBACKS SWSCI_FUNCTION_CODE(SWSCI_SBCB, 0) 227 #define SWSCI_SBCB_INIT_COMPLETION SWSCI_FUNCTION_CODE(SWSCI_SBCB, 1) 228 #define SWSCI_SBCB_PRE_HIRES_SET_MODE SWSCI_FUNCTION_CODE(SWSCI_SBCB, 3) 229 #define SWSCI_SBCB_POST_HIRES_SET_MODE SWSCI_FUNCTION_CODE(SWSCI_SBCB, 4) 230 #define SWSCI_SBCB_DISPLAY_SWITCH SWSCI_FUNCTION_CODE(SWSCI_SBCB, 5) 231 #define SWSCI_SBCB_SET_TV_FORMAT SWSCI_FUNCTION_CODE(SWSCI_SBCB, 6) 232 #define SWSCI_SBCB_ADAPTER_POWER_STATE SWSCI_FUNCTION_CODE(SWSCI_SBCB, 7) 233 #define SWSCI_SBCB_DISPLAY_POWER_STATE SWSCI_FUNCTION_CODE(SWSCI_SBCB, 8) 234 #define SWSCI_SBCB_SET_BOOT_DISPLAY SWSCI_FUNCTION_CODE(SWSCI_SBCB, 9) 235 #define SWSCI_SBCB_SET_PANEL_DETAILS SWSCI_FUNCTION_CODE(SWSCI_SBCB, 10) 236 #define SWSCI_SBCB_SET_INTERNAL_GFX SWSCI_FUNCTION_CODE(SWSCI_SBCB, 11) 237 #define SWSCI_SBCB_POST_HIRES_TO_DOS_FS SWSCI_FUNCTION_CODE(SWSCI_SBCB, 16) 238 #define SWSCI_SBCB_SUSPEND_RESUME SWSCI_FUNCTION_CODE(SWSCI_SBCB, 17) 239 #define SWSCI_SBCB_SET_SPREAD_SPECTRUM SWSCI_FUNCTION_CODE(SWSCI_SBCB, 18) 240 #define SWSCI_SBCB_POST_VBE_PM SWSCI_FUNCTION_CODE(SWSCI_SBCB, 19) 241 #define SWSCI_SBCB_ENABLE_DISABLE_AUDIO SWSCI_FUNCTION_CODE(SWSCI_SBCB, 21) 242 243 #define MAX_DSLP 1500 244 245 static int swsci(struct drm_i915_private *dev_priv, 246 u32 function, u32 parm, u32 *parm_out) 247 { 248 struct opregion_swsci *swsci = dev_priv->opregion.swsci; 249 struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev); 250 u32 main_function, sub_function, scic; 251 u16 swsci_val; 252 u32 dslp; 253 254 if (!swsci) 255 return -ENODEV; 256 257 main_function = (function & SWSCI_SCIC_MAIN_FUNCTION_MASK) >> 258 SWSCI_SCIC_MAIN_FUNCTION_SHIFT; 259 sub_function = (function & SWSCI_SCIC_SUB_FUNCTION_MASK) >> 260 SWSCI_SCIC_SUB_FUNCTION_SHIFT; 261 262 /* Check if we can call the function. See swsci_setup for details. */ 263 if (main_function == SWSCI_SBCB) { 264 if ((dev_priv->opregion.swsci_sbcb_sub_functions & 265 (1 << sub_function)) == 0) 266 return -EINVAL; 267 } else if (main_function == SWSCI_GBDA) { 268 if ((dev_priv->opregion.swsci_gbda_sub_functions & 269 (1 << sub_function)) == 0) 270 return -EINVAL; 271 } 272 273 /* Driver sleep timeout in ms. */ 274 dslp = swsci->dslp; 275 if (!dslp) { 276 /* The spec says 2ms should be the default, but it's too small 277 * for some machines. */ 278 dslp = 50; 279 } else if (dslp > MAX_DSLP) { 280 /* Hey bios, trust must be earned. */ 281 DRM_INFO_ONCE("ACPI BIOS requests an excessive sleep of %u ms, " 282 "using %u ms instead\n", dslp, MAX_DSLP); 283 dslp = MAX_DSLP; 284 } 285 286 /* The spec tells us to do this, but we are the only user... */ 287 scic = swsci->scic; 288 if (scic & SWSCI_SCIC_INDICATOR) { 289 drm_dbg(&dev_priv->drm, "SWSCI request already in progress\n"); 290 return -EBUSY; 291 } 292 293 scic = function | SWSCI_SCIC_INDICATOR; 294 295 swsci->parm = parm; 296 swsci->scic = scic; 297 298 /* Ensure SCI event is selected and event trigger is cleared. */ 299 pci_read_config_word(pdev, SWSCI, &swsci_val); 300 if (!(swsci_val & SWSCI_SCISEL) || (swsci_val & SWSCI_GSSCIE)) { 301 swsci_val |= SWSCI_SCISEL; 302 swsci_val &= ~SWSCI_GSSCIE; 303 pci_write_config_word(pdev, SWSCI, swsci_val); 304 } 305 306 /* Use event trigger to tell bios to check the mail. */ 307 swsci_val |= SWSCI_GSSCIE; 308 pci_write_config_word(pdev, SWSCI, swsci_val); 309 310 /* Poll for the result. */ 311 #define C (((scic = swsci->scic) & SWSCI_SCIC_INDICATOR) == 0) 312 if (wait_for(C, dslp)) { 313 drm_dbg(&dev_priv->drm, "SWSCI request timed out\n"); 314 return -ETIMEDOUT; 315 } 316 317 scic = (scic & SWSCI_SCIC_EXIT_STATUS_MASK) >> 318 SWSCI_SCIC_EXIT_STATUS_SHIFT; 319 320 /* Note: scic == 0 is an error! */ 321 if (scic != SWSCI_SCIC_EXIT_STATUS_SUCCESS) { 322 drm_dbg(&dev_priv->drm, "SWSCI request error %u\n", scic); 323 return -EIO; 324 } 325 326 if (parm_out) 327 *parm_out = swsci->parm; 328 329 return 0; 330 331 #undef C 332 } 333 334 #define DISPLAY_TYPE_CRT 0 335 #define DISPLAY_TYPE_TV 1 336 #define DISPLAY_TYPE_EXTERNAL_FLAT_PANEL 2 337 #define DISPLAY_TYPE_INTERNAL_FLAT_PANEL 3 338 339 int intel_opregion_notify_encoder(struct intel_encoder *intel_encoder, 340 bool enable) 341 { 342 struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev); 343 u32 parm = 0; 344 u32 type = 0; 345 u32 port; 346 347 /* don't care about old stuff for now */ 348 if (!HAS_DDI(dev_priv)) 349 return 0; 350 351 if (intel_encoder->type == INTEL_OUTPUT_DSI) 352 port = 0; 353 else 354 port = intel_encoder->port; 355 356 if (port == PORT_E) { 357 port = 0; 358 } else { 359 parm |= 1 << port; 360 port++; 361 } 362 363 if (!enable) 364 parm |= 4 << 8; 365 366 switch (intel_encoder->type) { 367 case INTEL_OUTPUT_ANALOG: 368 type = DISPLAY_TYPE_CRT; 369 break; 370 case INTEL_OUTPUT_DDI: 371 case INTEL_OUTPUT_DP: 372 case INTEL_OUTPUT_HDMI: 373 case INTEL_OUTPUT_DP_MST: 374 type = DISPLAY_TYPE_EXTERNAL_FLAT_PANEL; 375 break; 376 case INTEL_OUTPUT_EDP: 377 case INTEL_OUTPUT_DSI: 378 type = DISPLAY_TYPE_INTERNAL_FLAT_PANEL; 379 break; 380 default: 381 drm_WARN_ONCE(&dev_priv->drm, 1, 382 "unsupported intel_encoder type %d\n", 383 intel_encoder->type); 384 return -EINVAL; 385 } 386 387 parm |= type << (16 + port * 3); 388 389 return swsci(dev_priv, SWSCI_SBCB_DISPLAY_POWER_STATE, parm, NULL); 390 } 391 392 static const struct { 393 pci_power_t pci_power_state; 394 u32 parm; 395 } power_state_map[] = { 396 { PCI_D0, 0x00 }, 397 { PCI_D1, 0x01 }, 398 { PCI_D2, 0x02 }, 399 { PCI_D3hot, 0x04 }, 400 { PCI_D3cold, 0x04 }, 401 }; 402 403 int intel_opregion_notify_adapter(struct drm_i915_private *dev_priv, 404 pci_power_t state) 405 { 406 int i; 407 408 if (!HAS_DDI(dev_priv)) 409 return 0; 410 411 for (i = 0; i < ARRAY_SIZE(power_state_map); i++) { 412 if (state == power_state_map[i].pci_power_state) 413 return swsci(dev_priv, SWSCI_SBCB_ADAPTER_POWER_STATE, 414 power_state_map[i].parm, NULL); 415 } 416 417 return -EINVAL; 418 } 419 420 static u32 asle_set_backlight(struct drm_i915_private *dev_priv, u32 bclp) 421 { 422 struct intel_connector *connector; 423 struct drm_connector_list_iter conn_iter; 424 struct opregion_asle *asle = dev_priv->opregion.asle; 425 struct drm_device *dev = &dev_priv->drm; 426 427 drm_dbg(&dev_priv->drm, "bclp = 0x%08x\n", bclp); 428 429 if (acpi_video_get_backlight_type() == acpi_backlight_native) { 430 drm_dbg_kms(&dev_priv->drm, 431 "opregion backlight request ignored\n"); 432 return 0; 433 } 434 435 if (!(bclp & ASLE_BCLP_VALID)) 436 return ASLC_BACKLIGHT_FAILED; 437 438 bclp &= ASLE_BCLP_MSK; 439 if (bclp > 255) 440 return ASLC_BACKLIGHT_FAILED; 441 442 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL); 443 444 /* 445 * Update backlight on all connectors that support backlight (usually 446 * only one). 447 */ 448 drm_dbg_kms(&dev_priv->drm, "updating opregion backlight %d/255\n", 449 bclp); 450 drm_connector_list_iter_begin(dev, &conn_iter); 451 for_each_intel_connector_iter(connector, &conn_iter) 452 intel_backlight_set_acpi(connector->base.state, bclp, 255); 453 drm_connector_list_iter_end(&conn_iter); 454 asle->cblv = DIV_ROUND_UP(bclp * 100, 255) | ASLE_CBLV_VALID; 455 456 drm_modeset_unlock(&dev->mode_config.connection_mutex); 457 458 459 return 0; 460 } 461 462 static u32 asle_set_als_illum(struct drm_i915_private *dev_priv, u32 alsi) 463 { 464 /* alsi is the current ALS reading in lux. 0 indicates below sensor 465 range, 0xffff indicates above sensor range. 1-0xfffe are valid */ 466 drm_dbg(&dev_priv->drm, "Illum is not supported\n"); 467 return ASLC_ALS_ILLUM_FAILED; 468 } 469 470 static u32 asle_set_pwm_freq(struct drm_i915_private *dev_priv, u32 pfmb) 471 { 472 drm_dbg(&dev_priv->drm, "PWM freq is not supported\n"); 473 return ASLC_PWM_FREQ_FAILED; 474 } 475 476 static u32 asle_set_pfit(struct drm_i915_private *dev_priv, u32 pfit) 477 { 478 /* Panel fitting is currently controlled by the X code, so this is a 479 noop until modesetting support works fully */ 480 drm_dbg(&dev_priv->drm, "Pfit is not supported\n"); 481 return ASLC_PFIT_FAILED; 482 } 483 484 static u32 asle_set_supported_rotation_angles(struct drm_i915_private *dev_priv, u32 srot) 485 { 486 drm_dbg(&dev_priv->drm, "SROT is not supported\n"); 487 return ASLC_ROTATION_ANGLES_FAILED; 488 } 489 490 static u32 asle_set_button_array(struct drm_i915_private *dev_priv, u32 iuer) 491 { 492 if (!iuer) 493 drm_dbg(&dev_priv->drm, 494 "Button array event is not supported (nothing)\n"); 495 if (iuer & ASLE_IUER_ROTATION_LOCK_BTN) 496 drm_dbg(&dev_priv->drm, 497 "Button array event is not supported (rotation lock)\n"); 498 if (iuer & ASLE_IUER_VOLUME_DOWN_BTN) 499 drm_dbg(&dev_priv->drm, 500 "Button array event is not supported (volume down)\n"); 501 if (iuer & ASLE_IUER_VOLUME_UP_BTN) 502 drm_dbg(&dev_priv->drm, 503 "Button array event is not supported (volume up)\n"); 504 if (iuer & ASLE_IUER_WINDOWS_BTN) 505 drm_dbg(&dev_priv->drm, 506 "Button array event is not supported (windows)\n"); 507 if (iuer & ASLE_IUER_POWER_BTN) 508 drm_dbg(&dev_priv->drm, 509 "Button array event is not supported (power)\n"); 510 511 return ASLC_BUTTON_ARRAY_FAILED; 512 } 513 514 static u32 asle_set_convertible(struct drm_i915_private *dev_priv, u32 iuer) 515 { 516 if (iuer & ASLE_IUER_CONVERTIBLE) 517 drm_dbg(&dev_priv->drm, 518 "Convertible is not supported (clamshell)\n"); 519 else 520 drm_dbg(&dev_priv->drm, 521 "Convertible is not supported (slate)\n"); 522 523 return ASLC_CONVERTIBLE_FAILED; 524 } 525 526 static u32 asle_set_docking(struct drm_i915_private *dev_priv, u32 iuer) 527 { 528 if (iuer & ASLE_IUER_DOCKING) 529 drm_dbg(&dev_priv->drm, "Docking is not supported (docked)\n"); 530 else 531 drm_dbg(&dev_priv->drm, 532 "Docking is not supported (undocked)\n"); 533 534 return ASLC_DOCKING_FAILED; 535 } 536 537 static u32 asle_isct_state(struct drm_i915_private *dev_priv) 538 { 539 drm_dbg(&dev_priv->drm, "ISCT is not supported\n"); 540 return ASLC_ISCT_STATE_FAILED; 541 } 542 543 static void asle_work(struct work_struct *work) 544 { 545 struct intel_opregion *opregion = 546 container_of(work, struct intel_opregion, asle_work); 547 struct drm_i915_private *dev_priv = 548 container_of(opregion, struct drm_i915_private, opregion); 549 struct opregion_asle *asle = dev_priv->opregion.asle; 550 u32 aslc_stat = 0; 551 u32 aslc_req; 552 553 if (!asle) 554 return; 555 556 aslc_req = asle->aslc; 557 558 if (!(aslc_req & ASLC_REQ_MSK)) { 559 drm_dbg(&dev_priv->drm, 560 "No request on ASLC interrupt 0x%08x\n", aslc_req); 561 return; 562 } 563 564 if (aslc_req & ASLC_SET_ALS_ILLUM) 565 aslc_stat |= asle_set_als_illum(dev_priv, asle->alsi); 566 567 if (aslc_req & ASLC_SET_BACKLIGHT) 568 aslc_stat |= asle_set_backlight(dev_priv, asle->bclp); 569 570 if (aslc_req & ASLC_SET_PFIT) 571 aslc_stat |= asle_set_pfit(dev_priv, asle->pfit); 572 573 if (aslc_req & ASLC_SET_PWM_FREQ) 574 aslc_stat |= asle_set_pwm_freq(dev_priv, asle->pfmb); 575 576 if (aslc_req & ASLC_SUPPORTED_ROTATION_ANGLES) 577 aslc_stat |= asle_set_supported_rotation_angles(dev_priv, 578 asle->srot); 579 580 if (aslc_req & ASLC_BUTTON_ARRAY) 581 aslc_stat |= asle_set_button_array(dev_priv, asle->iuer); 582 583 if (aslc_req & ASLC_CONVERTIBLE_INDICATOR) 584 aslc_stat |= asle_set_convertible(dev_priv, asle->iuer); 585 586 if (aslc_req & ASLC_DOCKING_INDICATOR) 587 aslc_stat |= asle_set_docking(dev_priv, asle->iuer); 588 589 if (aslc_req & ASLC_ISCT_STATE_CHANGE) 590 aslc_stat |= asle_isct_state(dev_priv); 591 592 asle->aslc = aslc_stat; 593 } 594 595 void intel_opregion_asle_intr(struct drm_i915_private *dev_priv) 596 { 597 if (dev_priv->opregion.asle) 598 schedule_work(&dev_priv->opregion.asle_work); 599 } 600 601 #define ACPI_EV_DISPLAY_SWITCH (1<<0) 602 #define ACPI_EV_LID (1<<1) 603 #define ACPI_EV_DOCK (1<<2) 604 605 /* 606 * The only video events relevant to opregion are 0x80. These indicate either a 607 * docking event, lid switch or display switch request. In Linux, these are 608 * handled by the dock, button and video drivers. 609 */ 610 static int intel_opregion_video_event(struct notifier_block *nb, 611 unsigned long val, void *data) 612 { 613 struct intel_opregion *opregion = container_of(nb, struct intel_opregion, 614 acpi_notifier); 615 struct acpi_bus_event *event = data; 616 struct opregion_acpi *acpi; 617 int ret = NOTIFY_OK; 618 619 if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0) 620 return NOTIFY_DONE; 621 622 acpi = opregion->acpi; 623 624 if (event->type == 0x80 && ((acpi->cevt & 1) == 0)) 625 ret = NOTIFY_BAD; 626 627 acpi->csts = 0; 628 629 return ret; 630 } 631 632 /* 633 * Initialise the DIDL field in opregion. This passes a list of devices to 634 * the firmware. Values are defined by section B.4.2 of the ACPI specification 635 * (version 3) 636 */ 637 638 static void set_did(struct intel_opregion *opregion, int i, u32 val) 639 { 640 if (i < ARRAY_SIZE(opregion->acpi->didl)) { 641 opregion->acpi->didl[i] = val; 642 } else { 643 i -= ARRAY_SIZE(opregion->acpi->didl); 644 645 if (WARN_ON(i >= ARRAY_SIZE(opregion->acpi->did2))) 646 return; 647 648 opregion->acpi->did2[i] = val; 649 } 650 } 651 652 static void intel_didl_outputs(struct drm_i915_private *dev_priv) 653 { 654 struct intel_opregion *opregion = &dev_priv->opregion; 655 struct intel_connector *connector; 656 struct drm_connector_list_iter conn_iter; 657 int i = 0, max_outputs; 658 659 /* 660 * In theory, did2, the extended didl, gets added at opregion version 661 * 3.0. In practice, however, we're supposed to set it for earlier 662 * versions as well, since a BIOS that doesn't understand did2 should 663 * not look at it anyway. Use a variable so we can tweak this if a need 664 * arises later. 665 */ 666 max_outputs = ARRAY_SIZE(opregion->acpi->didl) + 667 ARRAY_SIZE(opregion->acpi->did2); 668 669 intel_acpi_device_id_update(dev_priv); 670 671 drm_connector_list_iter_begin(&dev_priv->drm, &conn_iter); 672 for_each_intel_connector_iter(connector, &conn_iter) { 673 if (i < max_outputs) 674 set_did(opregion, i, connector->acpi_device_id); 675 i++; 676 } 677 drm_connector_list_iter_end(&conn_iter); 678 679 drm_dbg_kms(&dev_priv->drm, "%d outputs detected\n", i); 680 681 if (i > max_outputs) 682 drm_err(&dev_priv->drm, 683 "More than %d outputs in connector list\n", 684 max_outputs); 685 686 /* If fewer than max outputs, the list must be null terminated */ 687 if (i < max_outputs) 688 set_did(opregion, i, 0); 689 } 690 691 static void intel_setup_cadls(struct drm_i915_private *dev_priv) 692 { 693 struct intel_opregion *opregion = &dev_priv->opregion; 694 struct intel_connector *connector; 695 struct drm_connector_list_iter conn_iter; 696 int i = 0; 697 698 /* 699 * Initialize the CADL field from the connector device ids. This is 700 * essentially the same as copying from the DIDL. Technically, this is 701 * not always correct as display outputs may exist, but not active. This 702 * initialization is necessary for some Clevo laptops that check this 703 * field before processing the brightness and display switching hotkeys. 704 * 705 * Note that internal panels should be at the front of the connector 706 * list already, ensuring they're not left out. 707 */ 708 drm_connector_list_iter_begin(&dev_priv->drm, &conn_iter); 709 for_each_intel_connector_iter(connector, &conn_iter) { 710 if (i >= ARRAY_SIZE(opregion->acpi->cadl)) 711 break; 712 opregion->acpi->cadl[i++] = connector->acpi_device_id; 713 } 714 drm_connector_list_iter_end(&conn_iter); 715 716 /* If fewer than 8 active devices, the list must be null terminated */ 717 if (i < ARRAY_SIZE(opregion->acpi->cadl)) 718 opregion->acpi->cadl[i] = 0; 719 } 720 721 static void swsci_setup(struct drm_i915_private *dev_priv) 722 { 723 struct intel_opregion *opregion = &dev_priv->opregion; 724 bool requested_callbacks = false; 725 u32 tmp; 726 727 /* Sub-function code 0 is okay, let's allow them. */ 728 opregion->swsci_gbda_sub_functions = 1; 729 opregion->swsci_sbcb_sub_functions = 1; 730 731 /* We use GBDA to ask for supported GBDA calls. */ 732 if (swsci(dev_priv, SWSCI_GBDA_SUPPORTED_CALLS, 0, &tmp) == 0) { 733 /* make the bits match the sub-function codes */ 734 tmp <<= 1; 735 opregion->swsci_gbda_sub_functions |= tmp; 736 } 737 738 /* 739 * We also use GBDA to ask for _requested_ SBCB callbacks. The driver 740 * must not call interfaces that are not specifically requested by the 741 * bios. 742 */ 743 if (swsci(dev_priv, SWSCI_GBDA_REQUESTED_CALLBACKS, 0, &tmp) == 0) { 744 /* here, the bits already match sub-function codes */ 745 opregion->swsci_sbcb_sub_functions |= tmp; 746 requested_callbacks = true; 747 } 748 749 /* 750 * But we use SBCB to ask for _supported_ SBCB calls. This does not mean 751 * the callback is _requested_. But we still can't call interfaces that 752 * are not requested. 753 */ 754 if (swsci(dev_priv, SWSCI_SBCB_SUPPORTED_CALLBACKS, 0, &tmp) == 0) { 755 /* make the bits match the sub-function codes */ 756 u32 low = tmp & 0x7ff; 757 u32 high = tmp & ~0xfff; /* bit 11 is reserved */ 758 tmp = (high << 4) | (low << 1) | 1; 759 760 /* best guess what to do with supported wrt requested */ 761 if (requested_callbacks) { 762 u32 req = opregion->swsci_sbcb_sub_functions; 763 if ((req & tmp) != req) 764 drm_dbg(&dev_priv->drm, 765 "SWSCI BIOS requested (%08x) SBCB callbacks that are not supported (%08x)\n", 766 req, tmp); 767 /* XXX: for now, trust the requested callbacks */ 768 /* opregion->swsci_sbcb_sub_functions &= tmp; */ 769 } else { 770 opregion->swsci_sbcb_sub_functions |= tmp; 771 } 772 } 773 774 drm_dbg(&dev_priv->drm, 775 "SWSCI GBDA callbacks %08x, SBCB callbacks %08x\n", 776 opregion->swsci_gbda_sub_functions, 777 opregion->swsci_sbcb_sub_functions); 778 } 779 780 static int intel_no_opregion_vbt_callback(const struct dmi_system_id *id) 781 { 782 DRM_DEBUG_KMS("Falling back to manually reading VBT from " 783 "VBIOS ROM for %s\n", id->ident); 784 return 1; 785 } 786 787 static const struct dmi_system_id intel_no_opregion_vbt[] = { 788 { 789 .callback = intel_no_opregion_vbt_callback, 790 .ident = "ThinkCentre A57", 791 .matches = { 792 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), 793 DMI_MATCH(DMI_PRODUCT_NAME, "97027RG"), 794 }, 795 }, 796 { } 797 }; 798 799 static int intel_load_vbt_firmware(struct drm_i915_private *dev_priv) 800 { 801 struct intel_opregion *opregion = &dev_priv->opregion; 802 const struct firmware *fw = NULL; 803 const char *name = dev_priv->params.vbt_firmware; 804 int ret; 805 806 if (!name || !*name) 807 return -ENOENT; 808 809 ret = request_firmware(&fw, name, dev_priv->drm.dev); 810 if (ret) { 811 drm_err(&dev_priv->drm, 812 "Requesting VBT firmware \"%s\" failed (%d)\n", 813 name, ret); 814 return ret; 815 } 816 817 if (intel_bios_is_valid_vbt(fw->data, fw->size)) { 818 opregion->vbt_firmware = kmemdup(fw->data, fw->size, GFP_KERNEL); 819 if (opregion->vbt_firmware) { 820 drm_dbg_kms(&dev_priv->drm, 821 "Found valid VBT firmware \"%s\"\n", name); 822 opregion->vbt = opregion->vbt_firmware; 823 opregion->vbt_size = fw->size; 824 ret = 0; 825 } else { 826 ret = -ENOMEM; 827 } 828 } else { 829 drm_dbg_kms(&dev_priv->drm, "Invalid VBT firmware \"%s\"\n", 830 name); 831 ret = -EINVAL; 832 } 833 834 release_firmware(fw); 835 836 return ret; 837 } 838 839 int intel_opregion_setup(struct drm_i915_private *dev_priv) 840 { 841 struct intel_opregion *opregion = &dev_priv->opregion; 842 struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev); 843 u32 asls, mboxes; 844 char buf[sizeof(OPREGION_SIGNATURE)]; 845 int err = 0; 846 void *base; 847 const void *vbt; 848 u32 vbt_size; 849 850 BUILD_BUG_ON(sizeof(struct opregion_header) != 0x100); 851 BUILD_BUG_ON(sizeof(struct opregion_acpi) != 0x100); 852 BUILD_BUG_ON(sizeof(struct opregion_swsci) != 0x100); 853 BUILD_BUG_ON(sizeof(struct opregion_asle) != 0x100); 854 BUILD_BUG_ON(sizeof(struct opregion_asle_ext) != 0x400); 855 856 pci_read_config_dword(pdev, ASLS, &asls); 857 drm_dbg(&dev_priv->drm, "graphic opregion physical addr: 0x%x\n", 858 asls); 859 if (asls == 0) { 860 drm_dbg(&dev_priv->drm, "ACPI OpRegion not supported!\n"); 861 return -ENOTSUPP; 862 } 863 864 INIT_WORK(&opregion->asle_work, asle_work); 865 866 base = memremap(asls, OPREGION_SIZE, MEMREMAP_WB); 867 if (!base) 868 return -ENOMEM; 869 870 memcpy(buf, base, sizeof(buf)); 871 872 if (memcmp(buf, OPREGION_SIGNATURE, 16)) { 873 drm_dbg(&dev_priv->drm, "opregion signature mismatch\n"); 874 err = -EINVAL; 875 goto err_out; 876 } 877 opregion->header = base; 878 opregion->lid_state = base + ACPI_CLID; 879 880 drm_dbg(&dev_priv->drm, "ACPI OpRegion version %u.%u.%u\n", 881 opregion->header->over.major, 882 opregion->header->over.minor, 883 opregion->header->over.revision); 884 885 mboxes = opregion->header->mboxes; 886 if (mboxes & MBOX_ACPI) { 887 drm_dbg(&dev_priv->drm, "Public ACPI methods supported\n"); 888 opregion->acpi = base + OPREGION_ACPI_OFFSET; 889 /* 890 * Indicate we handle monitor hotplug events ourselves so we do 891 * not need ACPI notifications for them. Disabling these avoids 892 * triggering the AML code doing the notifation, which may be 893 * broken as Windows also seems to disable these. 894 */ 895 opregion->acpi->chpd = 1; 896 } 897 898 if (mboxes & MBOX_SWSCI) { 899 drm_dbg(&dev_priv->drm, "SWSCI supported\n"); 900 opregion->swsci = base + OPREGION_SWSCI_OFFSET; 901 swsci_setup(dev_priv); 902 } 903 904 if (mboxes & MBOX_ASLE) { 905 drm_dbg(&dev_priv->drm, "ASLE supported\n"); 906 opregion->asle = base + OPREGION_ASLE_OFFSET; 907 908 opregion->asle->ardy = ASLE_ARDY_NOT_READY; 909 } 910 911 if (mboxes & MBOX_ASLE_EXT) 912 drm_dbg(&dev_priv->drm, "ASLE extension supported\n"); 913 914 if (intel_load_vbt_firmware(dev_priv) == 0) 915 goto out; 916 917 if (dmi_check_system(intel_no_opregion_vbt)) 918 goto out; 919 920 if (opregion->header->over.major >= 2 && opregion->asle && 921 opregion->asle->rvda && opregion->asle->rvds) { 922 resource_size_t rvda = opregion->asle->rvda; 923 924 /* 925 * opregion 2.0: rvda is the physical VBT address. 926 * 927 * opregion 2.1+: rvda is unsigned, relative offset from 928 * opregion base, and should never point within opregion. 929 */ 930 if (opregion->header->over.major > 2 || 931 opregion->header->over.minor >= 1) { 932 drm_WARN_ON(&dev_priv->drm, rvda < OPREGION_SIZE); 933 934 rvda += asls; 935 } 936 937 opregion->rvda = memremap(rvda, opregion->asle->rvds, 938 MEMREMAP_WB); 939 940 vbt = opregion->rvda; 941 vbt_size = opregion->asle->rvds; 942 if (intel_bios_is_valid_vbt(vbt, vbt_size)) { 943 drm_dbg_kms(&dev_priv->drm, 944 "Found valid VBT in ACPI OpRegion (RVDA)\n"); 945 opregion->vbt = vbt; 946 opregion->vbt_size = vbt_size; 947 goto out; 948 } else { 949 drm_dbg_kms(&dev_priv->drm, 950 "Invalid VBT in ACPI OpRegion (RVDA)\n"); 951 memunmap(opregion->rvda); 952 opregion->rvda = NULL; 953 } 954 } 955 956 vbt = base + OPREGION_VBT_OFFSET; 957 /* 958 * The VBT specification says that if the ASLE ext mailbox is not used 959 * its area is reserved, but on some CHT boards the VBT extends into the 960 * ASLE ext area. Allow this even though it is against the spec, so we 961 * do not end up rejecting the VBT on those boards (and end up not 962 * finding the LCD panel because of this). 963 */ 964 vbt_size = (mboxes & MBOX_ASLE_EXT) ? 965 OPREGION_ASLE_EXT_OFFSET : OPREGION_SIZE; 966 vbt_size -= OPREGION_VBT_OFFSET; 967 if (intel_bios_is_valid_vbt(vbt, vbt_size)) { 968 drm_dbg_kms(&dev_priv->drm, 969 "Found valid VBT in ACPI OpRegion (Mailbox #4)\n"); 970 opregion->vbt = vbt; 971 opregion->vbt_size = vbt_size; 972 } else { 973 drm_dbg_kms(&dev_priv->drm, 974 "Invalid VBT in ACPI OpRegion (Mailbox #4)\n"); 975 } 976 977 out: 978 return 0; 979 980 err_out: 981 memunmap(base); 982 return err; 983 } 984 985 static int intel_use_opregion_panel_type_callback(const struct dmi_system_id *id) 986 { 987 DRM_INFO("Using panel type from OpRegion on %s\n", id->ident); 988 return 1; 989 } 990 991 static const struct dmi_system_id intel_use_opregion_panel_type[] = { 992 { 993 .callback = intel_use_opregion_panel_type_callback, 994 .ident = "Conrac GmbH IX45GM2", 995 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "Conrac GmbH"), 996 DMI_MATCH(DMI_PRODUCT_NAME, "IX45GM2"), 997 }, 998 }, 999 { } 1000 }; 1001 1002 int 1003 intel_opregion_get_panel_type(struct drm_i915_private *dev_priv) 1004 { 1005 u32 panel_details; 1006 int ret; 1007 1008 ret = swsci(dev_priv, SWSCI_GBDA_PANEL_DETAILS, 0x0, &panel_details); 1009 if (ret) 1010 return ret; 1011 1012 ret = (panel_details >> 8) & 0xff; 1013 if (ret > 0x10) { 1014 drm_dbg_kms(&dev_priv->drm, 1015 "Invalid OpRegion panel type 0x%x\n", ret); 1016 return -EINVAL; 1017 } 1018 1019 /* fall back to VBT panel type? */ 1020 if (ret == 0x0) { 1021 drm_dbg_kms(&dev_priv->drm, "No panel type in OpRegion\n"); 1022 return -ENODEV; 1023 } 1024 1025 /* 1026 * So far we know that some machined must use it, others must not use it. 1027 * There doesn't seem to be any way to determine which way to go, except 1028 * via a quirk list :( 1029 */ 1030 if (!dmi_check_system(intel_use_opregion_panel_type)) { 1031 drm_dbg_kms(&dev_priv->drm, 1032 "Ignoring OpRegion panel type (%d)\n", ret - 1); 1033 return -ENODEV; 1034 } 1035 1036 return ret - 1; 1037 } 1038 1039 void intel_opregion_register(struct drm_i915_private *i915) 1040 { 1041 struct intel_opregion *opregion = &i915->opregion; 1042 1043 if (!opregion->header) 1044 return; 1045 1046 if (opregion->acpi) { 1047 opregion->acpi_notifier.notifier_call = 1048 intel_opregion_video_event; 1049 register_acpi_notifier(&opregion->acpi_notifier); 1050 } 1051 1052 intel_opregion_resume(i915); 1053 } 1054 1055 void intel_opregion_resume(struct drm_i915_private *i915) 1056 { 1057 struct intel_opregion *opregion = &i915->opregion; 1058 1059 if (!opregion->header) 1060 return; 1061 1062 if (opregion->acpi) { 1063 intel_didl_outputs(i915); 1064 intel_setup_cadls(i915); 1065 1066 /* 1067 * Notify BIOS we are ready to handle ACPI video ext notifs. 1068 * Right now, all the events are handled by the ACPI video 1069 * module. We don't actually need to do anything with them. 1070 */ 1071 opregion->acpi->csts = 0; 1072 opregion->acpi->drdy = 1; 1073 } 1074 1075 if (opregion->asle) { 1076 opregion->asle->tche = ASLE_TCHE_BLC_EN; 1077 opregion->asle->ardy = ASLE_ARDY_READY; 1078 } 1079 1080 /* Some platforms abuse the _DSM to enable MUX */ 1081 intel_dsm_get_bios_data_funcs_supported(i915); 1082 1083 intel_opregion_notify_adapter(i915, PCI_D0); 1084 } 1085 1086 void intel_opregion_suspend(struct drm_i915_private *i915, pci_power_t state) 1087 { 1088 struct intel_opregion *opregion = &i915->opregion; 1089 1090 if (!opregion->header) 1091 return; 1092 1093 intel_opregion_notify_adapter(i915, state); 1094 1095 if (opregion->asle) 1096 opregion->asle->ardy = ASLE_ARDY_NOT_READY; 1097 1098 cancel_work_sync(&i915->opregion.asle_work); 1099 1100 if (opregion->acpi) 1101 opregion->acpi->drdy = 0; 1102 } 1103 1104 void intel_opregion_unregister(struct drm_i915_private *i915) 1105 { 1106 struct intel_opregion *opregion = &i915->opregion; 1107 1108 intel_opregion_suspend(i915, PCI_D1); 1109 1110 if (!opregion->header) 1111 return; 1112 1113 if (opregion->acpi_notifier.notifier_call) { 1114 unregister_acpi_notifier(&opregion->acpi_notifier); 1115 opregion->acpi_notifier.notifier_call = NULL; 1116 } 1117 1118 /* just clear all opregion memory pointers now */ 1119 memunmap(opregion->header); 1120 if (opregion->rvda) { 1121 memunmap(opregion->rvda); 1122 opregion->rvda = NULL; 1123 } 1124 if (opregion->vbt_firmware) { 1125 kfree(opregion->vbt_firmware); 1126 opregion->vbt_firmware = NULL; 1127 } 1128 opregion->header = NULL; 1129 opregion->acpi = NULL; 1130 opregion->swsci = NULL; 1131 opregion->asle = NULL; 1132 opregion->vbt = NULL; 1133 opregion->lid_state = NULL; 1134 } 1135