intel_opregion.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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. #include <linux/acpi.h>
  28. #include <linux/dmi.h>
  29. #include <acpi/video.h>
  30. #include <drm/drmP.h>
  31. #include <drm/i915_drm.h>
  32. #include "i915_drv.h"
  33. #include "intel_drv.h"
  34. #define OPREGION_HEADER_OFFSET 0
  35. #define OPREGION_ACPI_OFFSET 0x100
  36. #define ACPI_CLID 0x01ac /* current lid state indicator */
  37. #define ACPI_CDCK 0x01b0 /* current docking state indicator */
  38. #define OPREGION_SWSCI_OFFSET 0x200
  39. #define OPREGION_ASLE_OFFSET 0x300
  40. #define OPREGION_VBT_OFFSET 0x400
  41. #define OPREGION_ASLE_EXT_OFFSET 0x1C00
  42. #define OPREGION_SIGNATURE "IntelGraphicsMem"
  43. #define MBOX_ACPI (1<<0)
  44. #define MBOX_SWSCI (1<<1)
  45. #define MBOX_ASLE (1<<2)
  46. #define MBOX_ASLE_EXT (1<<4)
  47. struct opregion_header {
  48. u8 signature[16];
  49. u32 size;
  50. u32 opregion_ver;
  51. u8 bios_ver[32];
  52. u8 vbios_ver[16];
  53. u8 driver_ver[16];
  54. u32 mboxes;
  55. u32 driver_model;
  56. u32 pcon;
  57. u8 dver[32];
  58. u8 rsvd[124];
  59. } __packed;
  60. /* OpRegion mailbox #1: public ACPI methods */
  61. struct opregion_acpi {
  62. u32 drdy; /* driver readiness */
  63. u32 csts; /* notification status */
  64. u32 cevt; /* current event */
  65. u8 rsvd1[20];
  66. u32 didl[8]; /* supported display devices ID list */
  67. u32 cpdl[8]; /* currently presented display list */
  68. u32 cadl[8]; /* currently active display list */
  69. u32 nadl[8]; /* next active devices list */
  70. u32 aslp; /* ASL sleep time-out */
  71. u32 tidx; /* toggle table index */
  72. u32 chpd; /* current hotplug enable indicator */
  73. u32 clid; /* current lid state*/
  74. u32 cdck; /* current docking state */
  75. u32 sxsw; /* Sx state resume */
  76. u32 evts; /* ASL supported events */
  77. u32 cnot; /* current OS notification */
  78. u32 nrdy; /* driver status */
  79. u32 did2[7]; /* extended supported display devices ID list */
  80. u32 cpd2[7]; /* extended attached display devices list */
  81. u8 rsvd2[4];
  82. } __packed;
  83. /* OpRegion mailbox #2: SWSCI */
  84. struct opregion_swsci {
  85. u32 scic; /* SWSCI command|status|data */
  86. u32 parm; /* command parameters */
  87. u32 dslp; /* driver sleep time-out */
  88. u8 rsvd[244];
  89. } __packed;
  90. /* OpRegion mailbox #3: ASLE */
  91. struct opregion_asle {
  92. u32 ardy; /* driver readiness */
  93. u32 aslc; /* ASLE interrupt command */
  94. u32 tche; /* technology enabled indicator */
  95. u32 alsi; /* current ALS illuminance reading */
  96. u32 bclp; /* backlight brightness to set */
  97. u32 pfit; /* panel fitting state */
  98. u32 cblv; /* current brightness level */
  99. u16 bclm[20]; /* backlight level duty cycle mapping table */
  100. u32 cpfm; /* current panel fitting mode */
  101. u32 epfm; /* enabled panel fitting modes */
  102. u8 plut[74]; /* panel LUT and identifier */
  103. u32 pfmb; /* PWM freq and min brightness */
  104. u32 cddv; /* color correction default values */
  105. u32 pcft; /* power conservation features */
  106. u32 srot; /* supported rotation angles */
  107. u32 iuer; /* IUER events */
  108. u64 fdss;
  109. u32 fdsp;
  110. u32 stat;
  111. u64 rvda; /* Physical address of raw vbt data */
  112. u32 rvds; /* Size of raw vbt data */
  113. u8 rsvd[58];
  114. } __packed;
  115. /* OpRegion mailbox #5: ASLE ext */
  116. struct opregion_asle_ext {
  117. u32 phed; /* Panel Header */
  118. u8 bddc[256]; /* Panel EDID */
  119. u8 rsvd[764];
  120. } __packed;
  121. /* Driver readiness indicator */
  122. #define ASLE_ARDY_READY (1 << 0)
  123. #define ASLE_ARDY_NOT_READY (0 << 0)
  124. /* ASLE Interrupt Command (ASLC) bits */
  125. #define ASLC_SET_ALS_ILLUM (1 << 0)
  126. #define ASLC_SET_BACKLIGHT (1 << 1)
  127. #define ASLC_SET_PFIT (1 << 2)
  128. #define ASLC_SET_PWM_FREQ (1 << 3)
  129. #define ASLC_SUPPORTED_ROTATION_ANGLES (1 << 4)
  130. #define ASLC_BUTTON_ARRAY (1 << 5)
  131. #define ASLC_CONVERTIBLE_INDICATOR (1 << 6)
  132. #define ASLC_DOCKING_INDICATOR (1 << 7)
  133. #define ASLC_ISCT_STATE_CHANGE (1 << 8)
  134. #define ASLC_REQ_MSK 0x1ff
  135. /* response bits */
  136. #define ASLC_ALS_ILLUM_FAILED (1 << 10)
  137. #define ASLC_BACKLIGHT_FAILED (1 << 12)
  138. #define ASLC_PFIT_FAILED (1 << 14)
  139. #define ASLC_PWM_FREQ_FAILED (1 << 16)
  140. #define ASLC_ROTATION_ANGLES_FAILED (1 << 18)
  141. #define ASLC_BUTTON_ARRAY_FAILED (1 << 20)
  142. #define ASLC_CONVERTIBLE_FAILED (1 << 22)
  143. #define ASLC_DOCKING_FAILED (1 << 24)
  144. #define ASLC_ISCT_STATE_FAILED (1 << 26)
  145. /* Technology enabled indicator */
  146. #define ASLE_TCHE_ALS_EN (1 << 0)
  147. #define ASLE_TCHE_BLC_EN (1 << 1)
  148. #define ASLE_TCHE_PFIT_EN (1 << 2)
  149. #define ASLE_TCHE_PFMB_EN (1 << 3)
  150. /* ASLE backlight brightness to set */
  151. #define ASLE_BCLP_VALID (1<<31)
  152. #define ASLE_BCLP_MSK (~(1<<31))
  153. /* ASLE panel fitting request */
  154. #define ASLE_PFIT_VALID (1<<31)
  155. #define ASLE_PFIT_CENTER (1<<0)
  156. #define ASLE_PFIT_STRETCH_TEXT (1<<1)
  157. #define ASLE_PFIT_STRETCH_GFX (1<<2)
  158. /* PWM frequency and minimum brightness */
  159. #define ASLE_PFMB_BRIGHTNESS_MASK (0xff)
  160. #define ASLE_PFMB_BRIGHTNESS_VALID (1<<8)
  161. #define ASLE_PFMB_PWM_MASK (0x7ffffe00)
  162. #define ASLE_PFMB_PWM_VALID (1<<31)
  163. #define ASLE_CBLV_VALID (1<<31)
  164. /* IUER */
  165. #define ASLE_IUER_DOCKING (1 << 7)
  166. #define ASLE_IUER_CONVERTIBLE (1 << 6)
  167. #define ASLE_IUER_ROTATION_LOCK_BTN (1 << 4)
  168. #define ASLE_IUER_VOLUME_DOWN_BTN (1 << 3)
  169. #define ASLE_IUER_VOLUME_UP_BTN (1 << 2)
  170. #define ASLE_IUER_WINDOWS_BTN (1 << 1)
  171. #define ASLE_IUER_POWER_BTN (1 << 0)
  172. /* Software System Control Interrupt (SWSCI) */
  173. #define SWSCI_SCIC_INDICATOR (1 << 0)
  174. #define SWSCI_SCIC_MAIN_FUNCTION_SHIFT 1
  175. #define SWSCI_SCIC_MAIN_FUNCTION_MASK (0xf << 1)
  176. #define SWSCI_SCIC_SUB_FUNCTION_SHIFT 8
  177. #define SWSCI_SCIC_SUB_FUNCTION_MASK (0xff << 8)
  178. #define SWSCI_SCIC_EXIT_PARAMETER_SHIFT 8
  179. #define SWSCI_SCIC_EXIT_PARAMETER_MASK (0xff << 8)
  180. #define SWSCI_SCIC_EXIT_STATUS_SHIFT 5
  181. #define SWSCI_SCIC_EXIT_STATUS_MASK (7 << 5)
  182. #define SWSCI_SCIC_EXIT_STATUS_SUCCESS 1
  183. #define SWSCI_FUNCTION_CODE(main, sub) \
  184. ((main) << SWSCI_SCIC_MAIN_FUNCTION_SHIFT | \
  185. (sub) << SWSCI_SCIC_SUB_FUNCTION_SHIFT)
  186. /* SWSCI: Get BIOS Data (GBDA) */
  187. #define SWSCI_GBDA 4
  188. #define SWSCI_GBDA_SUPPORTED_CALLS SWSCI_FUNCTION_CODE(SWSCI_GBDA, 0)
  189. #define SWSCI_GBDA_REQUESTED_CALLBACKS SWSCI_FUNCTION_CODE(SWSCI_GBDA, 1)
  190. #define SWSCI_GBDA_BOOT_DISPLAY_PREF SWSCI_FUNCTION_CODE(SWSCI_GBDA, 4)
  191. #define SWSCI_GBDA_PANEL_DETAILS SWSCI_FUNCTION_CODE(SWSCI_GBDA, 5)
  192. #define SWSCI_GBDA_TV_STANDARD SWSCI_FUNCTION_CODE(SWSCI_GBDA, 6)
  193. #define SWSCI_GBDA_INTERNAL_GRAPHICS SWSCI_FUNCTION_CODE(SWSCI_GBDA, 7)
  194. #define SWSCI_GBDA_SPREAD_SPECTRUM SWSCI_FUNCTION_CODE(SWSCI_GBDA, 10)
  195. /* SWSCI: System BIOS Callbacks (SBCB) */
  196. #define SWSCI_SBCB 6
  197. #define SWSCI_SBCB_SUPPORTED_CALLBACKS SWSCI_FUNCTION_CODE(SWSCI_SBCB, 0)
  198. #define SWSCI_SBCB_INIT_COMPLETION SWSCI_FUNCTION_CODE(SWSCI_SBCB, 1)
  199. #define SWSCI_SBCB_PRE_HIRES_SET_MODE SWSCI_FUNCTION_CODE(SWSCI_SBCB, 3)
  200. #define SWSCI_SBCB_POST_HIRES_SET_MODE SWSCI_FUNCTION_CODE(SWSCI_SBCB, 4)
  201. #define SWSCI_SBCB_DISPLAY_SWITCH SWSCI_FUNCTION_CODE(SWSCI_SBCB, 5)
  202. #define SWSCI_SBCB_SET_TV_FORMAT SWSCI_FUNCTION_CODE(SWSCI_SBCB, 6)
  203. #define SWSCI_SBCB_ADAPTER_POWER_STATE SWSCI_FUNCTION_CODE(SWSCI_SBCB, 7)
  204. #define SWSCI_SBCB_DISPLAY_POWER_STATE SWSCI_FUNCTION_CODE(SWSCI_SBCB, 8)
  205. #define SWSCI_SBCB_SET_BOOT_DISPLAY SWSCI_FUNCTION_CODE(SWSCI_SBCB, 9)
  206. #define SWSCI_SBCB_SET_PANEL_DETAILS SWSCI_FUNCTION_CODE(SWSCI_SBCB, 10)
  207. #define SWSCI_SBCB_SET_INTERNAL_GFX SWSCI_FUNCTION_CODE(SWSCI_SBCB, 11)
  208. #define SWSCI_SBCB_POST_HIRES_TO_DOS_FS SWSCI_FUNCTION_CODE(SWSCI_SBCB, 16)
  209. #define SWSCI_SBCB_SUSPEND_RESUME SWSCI_FUNCTION_CODE(SWSCI_SBCB, 17)
  210. #define SWSCI_SBCB_SET_SPREAD_SPECTRUM SWSCI_FUNCTION_CODE(SWSCI_SBCB, 18)
  211. #define SWSCI_SBCB_POST_VBE_PM SWSCI_FUNCTION_CODE(SWSCI_SBCB, 19)
  212. #define SWSCI_SBCB_ENABLE_DISABLE_AUDIO SWSCI_FUNCTION_CODE(SWSCI_SBCB, 21)
  213. #define ACPI_OTHER_OUTPUT (0<<8)
  214. #define ACPI_VGA_OUTPUT (1<<8)
  215. #define ACPI_TV_OUTPUT (2<<8)
  216. #define ACPI_DIGITAL_OUTPUT (3<<8)
  217. #define ACPI_LVDS_OUTPUT (4<<8)
  218. #define MAX_DSLP 1500
  219. static int swsci(struct drm_device *dev, u32 function, u32 parm, u32 *parm_out)
  220. {
  221. struct drm_i915_private *dev_priv = dev->dev_private;
  222. struct opregion_swsci *swsci = dev_priv->opregion.swsci;
  223. u32 main_function, sub_function, scic;
  224. u16 swsci_val;
  225. u32 dslp;
  226. if (!swsci)
  227. return -ENODEV;
  228. main_function = (function & SWSCI_SCIC_MAIN_FUNCTION_MASK) >>
  229. SWSCI_SCIC_MAIN_FUNCTION_SHIFT;
  230. sub_function = (function & SWSCI_SCIC_SUB_FUNCTION_MASK) >>
  231. SWSCI_SCIC_SUB_FUNCTION_SHIFT;
  232. /* Check if we can call the function. See swsci_setup for details. */
  233. if (main_function == SWSCI_SBCB) {
  234. if ((dev_priv->opregion.swsci_sbcb_sub_functions &
  235. (1 << sub_function)) == 0)
  236. return -EINVAL;
  237. } else if (main_function == SWSCI_GBDA) {
  238. if ((dev_priv->opregion.swsci_gbda_sub_functions &
  239. (1 << sub_function)) == 0)
  240. return -EINVAL;
  241. }
  242. /* Driver sleep timeout in ms. */
  243. dslp = swsci->dslp;
  244. if (!dslp) {
  245. /* The spec says 2ms should be the default, but it's too small
  246. * for some machines. */
  247. dslp = 50;
  248. } else if (dslp > MAX_DSLP) {
  249. /* Hey bios, trust must be earned. */
  250. DRM_INFO_ONCE("ACPI BIOS requests an excessive sleep of %u ms, "
  251. "using %u ms instead\n", dslp, MAX_DSLP);
  252. dslp = MAX_DSLP;
  253. }
  254. /* The spec tells us to do this, but we are the only user... */
  255. scic = swsci->scic;
  256. if (scic & SWSCI_SCIC_INDICATOR) {
  257. DRM_DEBUG_DRIVER("SWSCI request already in progress\n");
  258. return -EBUSY;
  259. }
  260. scic = function | SWSCI_SCIC_INDICATOR;
  261. swsci->parm = parm;
  262. swsci->scic = scic;
  263. /* Ensure SCI event is selected and event trigger is cleared. */
  264. pci_read_config_word(dev->pdev, SWSCI, &swsci_val);
  265. if (!(swsci_val & SWSCI_SCISEL) || (swsci_val & SWSCI_GSSCIE)) {
  266. swsci_val |= SWSCI_SCISEL;
  267. swsci_val &= ~SWSCI_GSSCIE;
  268. pci_write_config_word(dev->pdev, SWSCI, swsci_val);
  269. }
  270. /* Use event trigger to tell bios to check the mail. */
  271. swsci_val |= SWSCI_GSSCIE;
  272. pci_write_config_word(dev->pdev, SWSCI, swsci_val);
  273. /* Poll for the result. */
  274. #define C (((scic = swsci->scic) & SWSCI_SCIC_INDICATOR) == 0)
  275. if (wait_for(C, dslp)) {
  276. DRM_DEBUG_DRIVER("SWSCI request timed out\n");
  277. return -ETIMEDOUT;
  278. }
  279. scic = (scic & SWSCI_SCIC_EXIT_STATUS_MASK) >>
  280. SWSCI_SCIC_EXIT_STATUS_SHIFT;
  281. /* Note: scic == 0 is an error! */
  282. if (scic != SWSCI_SCIC_EXIT_STATUS_SUCCESS) {
  283. DRM_DEBUG_DRIVER("SWSCI request error %u\n", scic);
  284. return -EIO;
  285. }
  286. if (parm_out)
  287. *parm_out = swsci->parm;
  288. return 0;
  289. #undef C
  290. }
  291. #define DISPLAY_TYPE_CRT 0
  292. #define DISPLAY_TYPE_TV 1
  293. #define DISPLAY_TYPE_EXTERNAL_FLAT_PANEL 2
  294. #define DISPLAY_TYPE_INTERNAL_FLAT_PANEL 3
  295. int intel_opregion_notify_encoder(struct intel_encoder *intel_encoder,
  296. bool enable)
  297. {
  298. struct drm_device *dev = intel_encoder->base.dev;
  299. u32 parm = 0;
  300. u32 type = 0;
  301. u32 port;
  302. /* don't care about old stuff for now */
  303. if (!HAS_DDI(dev))
  304. return 0;
  305. if (intel_encoder->type == INTEL_OUTPUT_DSI)
  306. port = 0;
  307. else
  308. port = intel_ddi_get_encoder_port(intel_encoder);
  309. if (port == PORT_E) {
  310. port = 0;
  311. } else {
  312. parm |= 1 << port;
  313. port++;
  314. }
  315. if (!enable)
  316. parm |= 4 << 8;
  317. switch (intel_encoder->type) {
  318. case INTEL_OUTPUT_ANALOG:
  319. type = DISPLAY_TYPE_CRT;
  320. break;
  321. case INTEL_OUTPUT_UNKNOWN:
  322. case INTEL_OUTPUT_DISPLAYPORT:
  323. case INTEL_OUTPUT_HDMI:
  324. case INTEL_OUTPUT_DP_MST:
  325. type = DISPLAY_TYPE_EXTERNAL_FLAT_PANEL;
  326. break;
  327. case INTEL_OUTPUT_EDP:
  328. case INTEL_OUTPUT_DSI:
  329. type = DISPLAY_TYPE_INTERNAL_FLAT_PANEL;
  330. break;
  331. default:
  332. WARN_ONCE(1, "unsupported intel_encoder type %d\n",
  333. intel_encoder->type);
  334. return -EINVAL;
  335. }
  336. parm |= type << (16 + port * 3);
  337. return swsci(dev, SWSCI_SBCB_DISPLAY_POWER_STATE, parm, NULL);
  338. }
  339. static const struct {
  340. pci_power_t pci_power_state;
  341. u32 parm;
  342. } power_state_map[] = {
  343. { PCI_D0, 0x00 },
  344. { PCI_D1, 0x01 },
  345. { PCI_D2, 0x02 },
  346. { PCI_D3hot, 0x04 },
  347. { PCI_D3cold, 0x04 },
  348. };
  349. int intel_opregion_notify_adapter(struct drm_device *dev, pci_power_t state)
  350. {
  351. int i;
  352. if (!HAS_DDI(dev))
  353. return 0;
  354. for (i = 0; i < ARRAY_SIZE(power_state_map); i++) {
  355. if (state == power_state_map[i].pci_power_state)
  356. return swsci(dev, SWSCI_SBCB_ADAPTER_POWER_STATE,
  357. power_state_map[i].parm, NULL);
  358. }
  359. return -EINVAL;
  360. }
  361. static u32 asle_set_backlight(struct drm_device *dev, u32 bclp)
  362. {
  363. struct drm_i915_private *dev_priv = dev->dev_private;
  364. struct intel_connector *connector;
  365. struct opregion_asle *asle = dev_priv->opregion.asle;
  366. DRM_DEBUG_DRIVER("bclp = 0x%08x\n", bclp);
  367. if (acpi_video_get_backlight_type() == acpi_backlight_native) {
  368. DRM_DEBUG_KMS("opregion backlight request ignored\n");
  369. return 0;
  370. }
  371. if (!(bclp & ASLE_BCLP_VALID))
  372. return ASLC_BACKLIGHT_FAILED;
  373. bclp &= ASLE_BCLP_MSK;
  374. if (bclp > 255)
  375. return ASLC_BACKLIGHT_FAILED;
  376. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  377. /*
  378. * Update backlight on all connectors that support backlight (usually
  379. * only one).
  380. */
  381. DRM_DEBUG_KMS("updating opregion backlight %d/255\n", bclp);
  382. for_each_intel_connector(dev, connector)
  383. intel_panel_set_backlight_acpi(connector, bclp, 255);
  384. asle->cblv = DIV_ROUND_UP(bclp * 100, 255) | ASLE_CBLV_VALID;
  385. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  386. return 0;
  387. }
  388. static u32 asle_set_als_illum(struct drm_device *dev, u32 alsi)
  389. {
  390. /* alsi is the current ALS reading in lux. 0 indicates below sensor
  391. range, 0xffff indicates above sensor range. 1-0xfffe are valid */
  392. DRM_DEBUG_DRIVER("Illum is not supported\n");
  393. return ASLC_ALS_ILLUM_FAILED;
  394. }
  395. static u32 asle_set_pwm_freq(struct drm_device *dev, u32 pfmb)
  396. {
  397. DRM_DEBUG_DRIVER("PWM freq is not supported\n");
  398. return ASLC_PWM_FREQ_FAILED;
  399. }
  400. static u32 asle_set_pfit(struct drm_device *dev, u32 pfit)
  401. {
  402. /* Panel fitting is currently controlled by the X code, so this is a
  403. noop until modesetting support works fully */
  404. DRM_DEBUG_DRIVER("Pfit is not supported\n");
  405. return ASLC_PFIT_FAILED;
  406. }
  407. static u32 asle_set_supported_rotation_angles(struct drm_device *dev, u32 srot)
  408. {
  409. DRM_DEBUG_DRIVER("SROT is not supported\n");
  410. return ASLC_ROTATION_ANGLES_FAILED;
  411. }
  412. static u32 asle_set_button_array(struct drm_device *dev, u32 iuer)
  413. {
  414. if (!iuer)
  415. DRM_DEBUG_DRIVER("Button array event is not supported (nothing)\n");
  416. if (iuer & ASLE_IUER_ROTATION_LOCK_BTN)
  417. DRM_DEBUG_DRIVER("Button array event is not supported (rotation lock)\n");
  418. if (iuer & ASLE_IUER_VOLUME_DOWN_BTN)
  419. DRM_DEBUG_DRIVER("Button array event is not supported (volume down)\n");
  420. if (iuer & ASLE_IUER_VOLUME_UP_BTN)
  421. DRM_DEBUG_DRIVER("Button array event is not supported (volume up)\n");
  422. if (iuer & ASLE_IUER_WINDOWS_BTN)
  423. DRM_DEBUG_DRIVER("Button array event is not supported (windows)\n");
  424. if (iuer & ASLE_IUER_POWER_BTN)
  425. DRM_DEBUG_DRIVER("Button array event is not supported (power)\n");
  426. return ASLC_BUTTON_ARRAY_FAILED;
  427. }
  428. static u32 asle_set_convertible(struct drm_device *dev, u32 iuer)
  429. {
  430. if (iuer & ASLE_IUER_CONVERTIBLE)
  431. DRM_DEBUG_DRIVER("Convertible is not supported (clamshell)\n");
  432. else
  433. DRM_DEBUG_DRIVER("Convertible is not supported (slate)\n");
  434. return ASLC_CONVERTIBLE_FAILED;
  435. }
  436. static u32 asle_set_docking(struct drm_device *dev, u32 iuer)
  437. {
  438. if (iuer & ASLE_IUER_DOCKING)
  439. DRM_DEBUG_DRIVER("Docking is not supported (docked)\n");
  440. else
  441. DRM_DEBUG_DRIVER("Docking is not supported (undocked)\n");
  442. return ASLC_DOCKING_FAILED;
  443. }
  444. static u32 asle_isct_state(struct drm_device *dev)
  445. {
  446. DRM_DEBUG_DRIVER("ISCT is not supported\n");
  447. return ASLC_ISCT_STATE_FAILED;
  448. }
  449. static void asle_work(struct work_struct *work)
  450. {
  451. struct intel_opregion *opregion =
  452. container_of(work, struct intel_opregion, asle_work);
  453. struct drm_i915_private *dev_priv =
  454. container_of(opregion, struct drm_i915_private, opregion);
  455. struct drm_device *dev = dev_priv->dev;
  456. struct opregion_asle *asle = dev_priv->opregion.asle;
  457. u32 aslc_stat = 0;
  458. u32 aslc_req;
  459. if (!asle)
  460. return;
  461. aslc_req = asle->aslc;
  462. if (!(aslc_req & ASLC_REQ_MSK)) {
  463. DRM_DEBUG_DRIVER("No request on ASLC interrupt 0x%08x\n",
  464. aslc_req);
  465. return;
  466. }
  467. if (aslc_req & ASLC_SET_ALS_ILLUM)
  468. aslc_stat |= asle_set_als_illum(dev, asle->alsi);
  469. if (aslc_req & ASLC_SET_BACKLIGHT)
  470. aslc_stat |= asle_set_backlight(dev, asle->bclp);
  471. if (aslc_req & ASLC_SET_PFIT)
  472. aslc_stat |= asle_set_pfit(dev, asle->pfit);
  473. if (aslc_req & ASLC_SET_PWM_FREQ)
  474. aslc_stat |= asle_set_pwm_freq(dev, asle->pfmb);
  475. if (aslc_req & ASLC_SUPPORTED_ROTATION_ANGLES)
  476. aslc_stat |= asle_set_supported_rotation_angles(dev,
  477. asle->srot);
  478. if (aslc_req & ASLC_BUTTON_ARRAY)
  479. aslc_stat |= asle_set_button_array(dev, asle->iuer);
  480. if (aslc_req & ASLC_CONVERTIBLE_INDICATOR)
  481. aslc_stat |= asle_set_convertible(dev, asle->iuer);
  482. if (aslc_req & ASLC_DOCKING_INDICATOR)
  483. aslc_stat |= asle_set_docking(dev, asle->iuer);
  484. if (aslc_req & ASLC_ISCT_STATE_CHANGE)
  485. aslc_stat |= asle_isct_state(dev);
  486. asle->aslc = aslc_stat;
  487. }
  488. void intel_opregion_asle_intr(struct drm_device *dev)
  489. {
  490. struct drm_i915_private *dev_priv = dev->dev_private;
  491. if (dev_priv->opregion.asle)
  492. schedule_work(&dev_priv->opregion.asle_work);
  493. }
  494. #define ACPI_EV_DISPLAY_SWITCH (1<<0)
  495. #define ACPI_EV_LID (1<<1)
  496. #define ACPI_EV_DOCK (1<<2)
  497. static struct intel_opregion *system_opregion;
  498. static int intel_opregion_video_event(struct notifier_block *nb,
  499. unsigned long val, void *data)
  500. {
  501. /* The only video events relevant to opregion are 0x80. These indicate
  502. either a docking event, lid switch or display switch request. In
  503. Linux, these are handled by the dock, button and video drivers.
  504. */
  505. struct acpi_bus_event *event = data;
  506. struct opregion_acpi *acpi;
  507. int ret = NOTIFY_OK;
  508. if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
  509. return NOTIFY_DONE;
  510. if (!system_opregion)
  511. return NOTIFY_DONE;
  512. acpi = system_opregion->acpi;
  513. if (event->type == 0x80 && ((acpi->cevt & 1) == 0))
  514. ret = NOTIFY_BAD;
  515. acpi->csts = 0;
  516. return ret;
  517. }
  518. static struct notifier_block intel_opregion_notifier = {
  519. .notifier_call = intel_opregion_video_event,
  520. };
  521. /*
  522. * Initialise the DIDL field in opregion. This passes a list of devices to
  523. * the firmware. Values are defined by section B.4.2 of the ACPI specification
  524. * (version 3)
  525. */
  526. static u32 get_did(struct intel_opregion *opregion, int i)
  527. {
  528. u32 did;
  529. if (i < ARRAY_SIZE(opregion->acpi->didl)) {
  530. did = opregion->acpi->didl[i];
  531. } else {
  532. i -= ARRAY_SIZE(opregion->acpi->didl);
  533. if (WARN_ON(i >= ARRAY_SIZE(opregion->acpi->did2)))
  534. return 0;
  535. did = opregion->acpi->did2[i];
  536. }
  537. return did;
  538. }
  539. static void set_did(struct intel_opregion *opregion, int i, u32 val)
  540. {
  541. if (i < ARRAY_SIZE(opregion->acpi->didl)) {
  542. opregion->acpi->didl[i] = val;
  543. } else {
  544. i -= ARRAY_SIZE(opregion->acpi->didl);
  545. if (WARN_ON(i >= ARRAY_SIZE(opregion->acpi->did2)))
  546. return;
  547. opregion->acpi->did2[i] = val;
  548. }
  549. }
  550. static void intel_didl_outputs(struct drm_device *dev)
  551. {
  552. struct drm_i915_private *dev_priv = dev->dev_private;
  553. struct intel_opregion *opregion = &dev_priv->opregion;
  554. struct drm_connector *connector;
  555. acpi_handle handle;
  556. struct acpi_device *acpi_dev, *acpi_cdev, *acpi_video_bus = NULL;
  557. unsigned long long device_id;
  558. acpi_status status;
  559. u32 temp, max_outputs;
  560. int i = 0;
  561. handle = ACPI_HANDLE(&dev->pdev->dev);
  562. if (!handle || acpi_bus_get_device(handle, &acpi_dev))
  563. return;
  564. if (acpi_is_video_device(handle))
  565. acpi_video_bus = acpi_dev;
  566. else {
  567. list_for_each_entry(acpi_cdev, &acpi_dev->children, node) {
  568. if (acpi_is_video_device(acpi_cdev->handle)) {
  569. acpi_video_bus = acpi_cdev;
  570. break;
  571. }
  572. }
  573. }
  574. if (!acpi_video_bus) {
  575. DRM_DEBUG_KMS("No ACPI video bus found\n");
  576. return;
  577. }
  578. /*
  579. * In theory, did2, the extended didl, gets added at opregion version
  580. * 3.0. In practice, however, we're supposed to set it for earlier
  581. * versions as well, since a BIOS that doesn't understand did2 should
  582. * not look at it anyway. Use a variable so we can tweak this if a need
  583. * arises later.
  584. */
  585. max_outputs = ARRAY_SIZE(opregion->acpi->didl) +
  586. ARRAY_SIZE(opregion->acpi->did2);
  587. list_for_each_entry(acpi_cdev, &acpi_video_bus->children, node) {
  588. if (i >= max_outputs) {
  589. DRM_DEBUG_KMS("More than %u outputs detected via ACPI\n",
  590. max_outputs);
  591. return;
  592. }
  593. status = acpi_evaluate_integer(acpi_cdev->handle, "_ADR",
  594. NULL, &device_id);
  595. if (ACPI_SUCCESS(status)) {
  596. if (!device_id)
  597. goto blind_set;
  598. set_did(opregion, i++, (u32)(device_id & 0x0f0f));
  599. }
  600. }
  601. end:
  602. DRM_DEBUG_KMS("%d outputs detected\n", i);
  603. /* If fewer than max outputs, the list must be null terminated */
  604. if (i < max_outputs)
  605. set_did(opregion, i, 0);
  606. return;
  607. blind_set:
  608. i = 0;
  609. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  610. int output_type = ACPI_OTHER_OUTPUT;
  611. if (i >= max_outputs) {
  612. DRM_DEBUG_KMS("More than %u outputs in connector list\n",
  613. max_outputs);
  614. return;
  615. }
  616. switch (connector->connector_type) {
  617. case DRM_MODE_CONNECTOR_VGA:
  618. case DRM_MODE_CONNECTOR_DVIA:
  619. output_type = ACPI_VGA_OUTPUT;
  620. break;
  621. case DRM_MODE_CONNECTOR_Composite:
  622. case DRM_MODE_CONNECTOR_SVIDEO:
  623. case DRM_MODE_CONNECTOR_Component:
  624. case DRM_MODE_CONNECTOR_9PinDIN:
  625. output_type = ACPI_TV_OUTPUT;
  626. break;
  627. case DRM_MODE_CONNECTOR_DVII:
  628. case DRM_MODE_CONNECTOR_DVID:
  629. case DRM_MODE_CONNECTOR_DisplayPort:
  630. case DRM_MODE_CONNECTOR_HDMIA:
  631. case DRM_MODE_CONNECTOR_HDMIB:
  632. output_type = ACPI_DIGITAL_OUTPUT;
  633. break;
  634. case DRM_MODE_CONNECTOR_LVDS:
  635. output_type = ACPI_LVDS_OUTPUT;
  636. break;
  637. }
  638. temp = get_did(opregion, i);
  639. set_did(opregion, i, temp | (1 << 31) | output_type | i);
  640. i++;
  641. }
  642. goto end;
  643. }
  644. static void intel_setup_cadls(struct drm_device *dev)
  645. {
  646. struct drm_i915_private *dev_priv = dev->dev_private;
  647. struct intel_opregion *opregion = &dev_priv->opregion;
  648. int i = 0;
  649. u32 disp_id;
  650. /* Initialize the CADL field by duplicating the DIDL values.
  651. * Technically, this is not always correct as display outputs may exist,
  652. * but not active. This initialization is necessary for some Clevo
  653. * laptops that check this field before processing the brightness and
  654. * display switching hotkeys. Just like DIDL, CADL is NULL-terminated if
  655. * there are less than eight devices. */
  656. do {
  657. disp_id = get_did(opregion, i);
  658. opregion->acpi->cadl[i] = disp_id;
  659. } while (++i < 8 && disp_id != 0);
  660. }
  661. void intel_opregion_init(struct drm_device *dev)
  662. {
  663. struct drm_i915_private *dev_priv = dev->dev_private;
  664. struct intel_opregion *opregion = &dev_priv->opregion;
  665. if (!opregion->header)
  666. return;
  667. if (opregion->acpi) {
  668. intel_didl_outputs(dev);
  669. intel_setup_cadls(dev);
  670. /* Notify BIOS we are ready to handle ACPI video ext notifs.
  671. * Right now, all the events are handled by the ACPI video module.
  672. * We don't actually need to do anything with them. */
  673. opregion->acpi->csts = 0;
  674. opregion->acpi->drdy = 1;
  675. system_opregion = opregion;
  676. register_acpi_notifier(&intel_opregion_notifier);
  677. }
  678. if (opregion->asle) {
  679. opregion->asle->tche = ASLE_TCHE_BLC_EN;
  680. opregion->asle->ardy = ASLE_ARDY_READY;
  681. }
  682. }
  683. void intel_opregion_fini(struct drm_device *dev)
  684. {
  685. struct drm_i915_private *dev_priv = dev->dev_private;
  686. struct intel_opregion *opregion = &dev_priv->opregion;
  687. if (!opregion->header)
  688. return;
  689. if (opregion->asle)
  690. opregion->asle->ardy = ASLE_ARDY_NOT_READY;
  691. cancel_work_sync(&dev_priv->opregion.asle_work);
  692. if (opregion->acpi) {
  693. opregion->acpi->drdy = 0;
  694. system_opregion = NULL;
  695. unregister_acpi_notifier(&intel_opregion_notifier);
  696. }
  697. /* just clear all opregion memory pointers now */
  698. memunmap(opregion->header);
  699. if (opregion->rvda) {
  700. memunmap(opregion->rvda);
  701. opregion->rvda = NULL;
  702. }
  703. opregion->header = NULL;
  704. opregion->acpi = NULL;
  705. opregion->swsci = NULL;
  706. opregion->asle = NULL;
  707. opregion->vbt = NULL;
  708. opregion->lid_state = NULL;
  709. }
  710. static void swsci_setup(struct drm_device *dev)
  711. {
  712. struct drm_i915_private *dev_priv = dev->dev_private;
  713. struct intel_opregion *opregion = &dev_priv->opregion;
  714. bool requested_callbacks = false;
  715. u32 tmp;
  716. /* Sub-function code 0 is okay, let's allow them. */
  717. opregion->swsci_gbda_sub_functions = 1;
  718. opregion->swsci_sbcb_sub_functions = 1;
  719. /* We use GBDA to ask for supported GBDA calls. */
  720. if (swsci(dev, SWSCI_GBDA_SUPPORTED_CALLS, 0, &tmp) == 0) {
  721. /* make the bits match the sub-function codes */
  722. tmp <<= 1;
  723. opregion->swsci_gbda_sub_functions |= tmp;
  724. }
  725. /*
  726. * We also use GBDA to ask for _requested_ SBCB callbacks. The driver
  727. * must not call interfaces that are not specifically requested by the
  728. * bios.
  729. */
  730. if (swsci(dev, SWSCI_GBDA_REQUESTED_CALLBACKS, 0, &tmp) == 0) {
  731. /* here, the bits already match sub-function codes */
  732. opregion->swsci_sbcb_sub_functions |= tmp;
  733. requested_callbacks = true;
  734. }
  735. /*
  736. * But we use SBCB to ask for _supported_ SBCB calls. This does not mean
  737. * the callback is _requested_. But we still can't call interfaces that
  738. * are not requested.
  739. */
  740. if (swsci(dev, SWSCI_SBCB_SUPPORTED_CALLBACKS, 0, &tmp) == 0) {
  741. /* make the bits match the sub-function codes */
  742. u32 low = tmp & 0x7ff;
  743. u32 high = tmp & ~0xfff; /* bit 11 is reserved */
  744. tmp = (high << 4) | (low << 1) | 1;
  745. /* best guess what to do with supported wrt requested */
  746. if (requested_callbacks) {
  747. u32 req = opregion->swsci_sbcb_sub_functions;
  748. if ((req & tmp) != req)
  749. DRM_DEBUG_DRIVER("SWSCI BIOS requested (%08x) SBCB callbacks that are not supported (%08x)\n", req, tmp);
  750. /* XXX: for now, trust the requested callbacks */
  751. /* opregion->swsci_sbcb_sub_functions &= tmp; */
  752. } else {
  753. opregion->swsci_sbcb_sub_functions |= tmp;
  754. }
  755. }
  756. DRM_DEBUG_DRIVER("SWSCI GBDA callbacks %08x, SBCB callbacks %08x\n",
  757. opregion->swsci_gbda_sub_functions,
  758. opregion->swsci_sbcb_sub_functions);
  759. }
  760. static int intel_no_opregion_vbt_callback(const struct dmi_system_id *id)
  761. {
  762. DRM_DEBUG_KMS("Falling back to manually reading VBT from "
  763. "VBIOS ROM for %s\n", id->ident);
  764. return 1;
  765. }
  766. static const struct dmi_system_id intel_no_opregion_vbt[] = {
  767. {
  768. .callback = intel_no_opregion_vbt_callback,
  769. .ident = "ThinkCentre A57",
  770. .matches = {
  771. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  772. DMI_MATCH(DMI_PRODUCT_NAME, "97027RG"),
  773. },
  774. },
  775. { }
  776. };
  777. int intel_opregion_setup(struct drm_device *dev)
  778. {
  779. struct drm_i915_private *dev_priv = dev->dev_private;
  780. struct intel_opregion *opregion = &dev_priv->opregion;
  781. u32 asls, mboxes;
  782. char buf[sizeof(OPREGION_SIGNATURE)];
  783. int err = 0;
  784. void *base;
  785. BUILD_BUG_ON(sizeof(struct opregion_header) != 0x100);
  786. BUILD_BUG_ON(sizeof(struct opregion_acpi) != 0x100);
  787. BUILD_BUG_ON(sizeof(struct opregion_swsci) != 0x100);
  788. BUILD_BUG_ON(sizeof(struct opregion_asle) != 0x100);
  789. BUILD_BUG_ON(sizeof(struct opregion_asle_ext) != 0x400);
  790. pci_read_config_dword(dev->pdev, ASLS, &asls);
  791. DRM_DEBUG_DRIVER("graphic opregion physical addr: 0x%x\n", asls);
  792. if (asls == 0) {
  793. DRM_DEBUG_DRIVER("ACPI OpRegion not supported!\n");
  794. return -ENOTSUPP;
  795. }
  796. INIT_WORK(&opregion->asle_work, asle_work);
  797. base = memremap(asls, OPREGION_SIZE, MEMREMAP_WB);
  798. if (!base)
  799. return -ENOMEM;
  800. memcpy(buf, base, sizeof(buf));
  801. if (memcmp(buf, OPREGION_SIGNATURE, 16)) {
  802. DRM_DEBUG_DRIVER("opregion signature mismatch\n");
  803. err = -EINVAL;
  804. goto err_out;
  805. }
  806. opregion->header = base;
  807. opregion->lid_state = base + ACPI_CLID;
  808. mboxes = opregion->header->mboxes;
  809. if (mboxes & MBOX_ACPI) {
  810. DRM_DEBUG_DRIVER("Public ACPI methods supported\n");
  811. opregion->acpi = base + OPREGION_ACPI_OFFSET;
  812. }
  813. if (mboxes & MBOX_SWSCI) {
  814. DRM_DEBUG_DRIVER("SWSCI supported\n");
  815. opregion->swsci = base + OPREGION_SWSCI_OFFSET;
  816. swsci_setup(dev);
  817. }
  818. if (mboxes & MBOX_ASLE) {
  819. DRM_DEBUG_DRIVER("ASLE supported\n");
  820. opregion->asle = base + OPREGION_ASLE_OFFSET;
  821. opregion->asle->ardy = ASLE_ARDY_NOT_READY;
  822. }
  823. if (mboxes & MBOX_ASLE_EXT)
  824. DRM_DEBUG_DRIVER("ASLE extension supported\n");
  825. if (!dmi_check_system(intel_no_opregion_vbt)) {
  826. const void *vbt = NULL;
  827. u32 vbt_size = 0;
  828. if (opregion->header->opregion_ver >= 2 && opregion->asle &&
  829. opregion->asle->rvda && opregion->asle->rvds) {
  830. opregion->rvda = memremap(opregion->asle->rvda,
  831. opregion->asle->rvds,
  832. MEMREMAP_WB);
  833. vbt = opregion->rvda;
  834. vbt_size = opregion->asle->rvds;
  835. }
  836. if (intel_bios_is_valid_vbt(vbt, vbt_size)) {
  837. DRM_DEBUG_KMS("Found valid VBT in ACPI OpRegion (RVDA)\n");
  838. opregion->vbt = vbt;
  839. opregion->vbt_size = vbt_size;
  840. } else {
  841. vbt = base + OPREGION_VBT_OFFSET;
  842. vbt_size = OPREGION_ASLE_EXT_OFFSET - OPREGION_VBT_OFFSET;
  843. if (intel_bios_is_valid_vbt(vbt, vbt_size)) {
  844. DRM_DEBUG_KMS("Found valid VBT in ACPI OpRegion (Mailbox #4)\n");
  845. opregion->vbt = vbt;
  846. opregion->vbt_size = vbt_size;
  847. }
  848. }
  849. }
  850. return 0;
  851. err_out:
  852. memunmap(base);
  853. return err;
  854. }
  855. int
  856. intel_opregion_get_panel_type(struct drm_device *dev)
  857. {
  858. u32 panel_details;
  859. int ret;
  860. ret = swsci(dev, SWSCI_GBDA_PANEL_DETAILS, 0x0, &panel_details);
  861. if (ret) {
  862. DRM_DEBUG_KMS("Failed to get panel details from OpRegion (%d)\n",
  863. ret);
  864. return ret;
  865. }
  866. ret = (panel_details >> 8) & 0xff;
  867. if (ret > 0x10) {
  868. DRM_DEBUG_KMS("Invalid OpRegion panel type 0x%x\n", ret);
  869. return -EINVAL;
  870. }
  871. /* fall back to VBT panel type? */
  872. if (ret == 0x0) {
  873. DRM_DEBUG_KMS("No panel type in OpRegion\n");
  874. return -ENODEV;
  875. }
  876. return ret - 1;
  877. }