intel_opregion.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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_i915_private *dev_priv,
  220. u32 function, u32 parm, u32 *parm_out)
  221. {
  222. struct opregion_swsci *swsci = dev_priv->opregion.swsci;
  223. struct pci_dev *pdev = dev_priv->dev->pdev;
  224. u32 main_function, sub_function, scic;
  225. u16 swsci_val;
  226. u32 dslp;
  227. if (!swsci)
  228. return -ENODEV;
  229. main_function = (function & SWSCI_SCIC_MAIN_FUNCTION_MASK) >>
  230. SWSCI_SCIC_MAIN_FUNCTION_SHIFT;
  231. sub_function = (function & SWSCI_SCIC_SUB_FUNCTION_MASK) >>
  232. SWSCI_SCIC_SUB_FUNCTION_SHIFT;
  233. /* Check if we can call the function. See swsci_setup for details. */
  234. if (main_function == SWSCI_SBCB) {
  235. if ((dev_priv->opregion.swsci_sbcb_sub_functions &
  236. (1 << sub_function)) == 0)
  237. return -EINVAL;
  238. } else if (main_function == SWSCI_GBDA) {
  239. if ((dev_priv->opregion.swsci_gbda_sub_functions &
  240. (1 << sub_function)) == 0)
  241. return -EINVAL;
  242. }
  243. /* Driver sleep timeout in ms. */
  244. dslp = swsci->dslp;
  245. if (!dslp) {
  246. /* The spec says 2ms should be the default, but it's too small
  247. * for some machines. */
  248. dslp = 50;
  249. } else if (dslp > MAX_DSLP) {
  250. /* Hey bios, trust must be earned. */
  251. DRM_INFO_ONCE("ACPI BIOS requests an excessive sleep of %u ms, "
  252. "using %u ms instead\n", dslp, MAX_DSLP);
  253. dslp = MAX_DSLP;
  254. }
  255. /* The spec tells us to do this, but we are the only user... */
  256. scic = swsci->scic;
  257. if (scic & SWSCI_SCIC_INDICATOR) {
  258. DRM_DEBUG_DRIVER("SWSCI request already in progress\n");
  259. return -EBUSY;
  260. }
  261. scic = function | SWSCI_SCIC_INDICATOR;
  262. swsci->parm = parm;
  263. swsci->scic = scic;
  264. /* Ensure SCI event is selected and event trigger is cleared. */
  265. pci_read_config_word(pdev, SWSCI, &swsci_val);
  266. if (!(swsci_val & SWSCI_SCISEL) || (swsci_val & SWSCI_GSSCIE)) {
  267. swsci_val |= SWSCI_SCISEL;
  268. swsci_val &= ~SWSCI_GSSCIE;
  269. pci_write_config_word(pdev, SWSCI, swsci_val);
  270. }
  271. /* Use event trigger to tell bios to check the mail. */
  272. swsci_val |= SWSCI_GSSCIE;
  273. pci_write_config_word(pdev, SWSCI, swsci_val);
  274. /* Poll for the result. */
  275. #define C (((scic = swsci->scic) & SWSCI_SCIC_INDICATOR) == 0)
  276. if (wait_for(C, dslp)) {
  277. DRM_DEBUG_DRIVER("SWSCI request timed out\n");
  278. return -ETIMEDOUT;
  279. }
  280. scic = (scic & SWSCI_SCIC_EXIT_STATUS_MASK) >>
  281. SWSCI_SCIC_EXIT_STATUS_SHIFT;
  282. /* Note: scic == 0 is an error! */
  283. if (scic != SWSCI_SCIC_EXIT_STATUS_SUCCESS) {
  284. DRM_DEBUG_DRIVER("SWSCI request error %u\n", scic);
  285. return -EIO;
  286. }
  287. if (parm_out)
  288. *parm_out = swsci->parm;
  289. return 0;
  290. #undef C
  291. }
  292. #define DISPLAY_TYPE_CRT 0
  293. #define DISPLAY_TYPE_TV 1
  294. #define DISPLAY_TYPE_EXTERNAL_FLAT_PANEL 2
  295. #define DISPLAY_TYPE_INTERNAL_FLAT_PANEL 3
  296. int intel_opregion_notify_encoder(struct intel_encoder *intel_encoder,
  297. bool enable)
  298. {
  299. struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
  300. u32 parm = 0;
  301. u32 type = 0;
  302. u32 port;
  303. /* don't care about old stuff for now */
  304. if (!HAS_DDI(dev_priv))
  305. return 0;
  306. if (intel_encoder->type == INTEL_OUTPUT_DSI)
  307. port = 0;
  308. else
  309. port = intel_ddi_get_encoder_port(intel_encoder);
  310. if (port == PORT_E) {
  311. port = 0;
  312. } else {
  313. parm |= 1 << port;
  314. port++;
  315. }
  316. if (!enable)
  317. parm |= 4 << 8;
  318. switch (intel_encoder->type) {
  319. case INTEL_OUTPUT_ANALOG:
  320. type = DISPLAY_TYPE_CRT;
  321. break;
  322. case INTEL_OUTPUT_UNKNOWN:
  323. case INTEL_OUTPUT_DISPLAYPORT:
  324. case INTEL_OUTPUT_HDMI:
  325. case INTEL_OUTPUT_DP_MST:
  326. type = DISPLAY_TYPE_EXTERNAL_FLAT_PANEL;
  327. break;
  328. case INTEL_OUTPUT_EDP:
  329. case INTEL_OUTPUT_DSI:
  330. type = DISPLAY_TYPE_INTERNAL_FLAT_PANEL;
  331. break;
  332. default:
  333. WARN_ONCE(1, "unsupported intel_encoder type %d\n",
  334. intel_encoder->type);
  335. return -EINVAL;
  336. }
  337. parm |= type << (16 + port * 3);
  338. return swsci(dev_priv, SWSCI_SBCB_DISPLAY_POWER_STATE, parm, NULL);
  339. }
  340. static const struct {
  341. pci_power_t pci_power_state;
  342. u32 parm;
  343. } power_state_map[] = {
  344. { PCI_D0, 0x00 },
  345. { PCI_D1, 0x01 },
  346. { PCI_D2, 0x02 },
  347. { PCI_D3hot, 0x04 },
  348. { PCI_D3cold, 0x04 },
  349. };
  350. int intel_opregion_notify_adapter(struct drm_i915_private *dev_priv,
  351. pci_power_t state)
  352. {
  353. int i;
  354. if (!HAS_DDI(dev_priv))
  355. return 0;
  356. for (i = 0; i < ARRAY_SIZE(power_state_map); i++) {
  357. if (state == power_state_map[i].pci_power_state)
  358. return swsci(dev_priv, SWSCI_SBCB_ADAPTER_POWER_STATE,
  359. power_state_map[i].parm, NULL);
  360. }
  361. return -EINVAL;
  362. }
  363. static u32 asle_set_backlight(struct drm_i915_private *dev_priv, u32 bclp)
  364. {
  365. struct intel_connector *connector;
  366. struct opregion_asle *asle = dev_priv->opregion.asle;
  367. struct drm_device *dev = dev_priv->dev;
  368. DRM_DEBUG_DRIVER("bclp = 0x%08x\n", bclp);
  369. if (acpi_video_get_backlight_type() == acpi_backlight_native) {
  370. DRM_DEBUG_KMS("opregion backlight request ignored\n");
  371. return 0;
  372. }
  373. if (!(bclp & ASLE_BCLP_VALID))
  374. return ASLC_BACKLIGHT_FAILED;
  375. bclp &= ASLE_BCLP_MSK;
  376. if (bclp > 255)
  377. return ASLC_BACKLIGHT_FAILED;
  378. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  379. /*
  380. * Update backlight on all connectors that support backlight (usually
  381. * only one).
  382. */
  383. DRM_DEBUG_KMS("updating opregion backlight %d/255\n", bclp);
  384. for_each_intel_connector(dev, connector)
  385. intel_panel_set_backlight_acpi(connector, bclp, 255);
  386. asle->cblv = DIV_ROUND_UP(bclp * 100, 255) | ASLE_CBLV_VALID;
  387. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  388. return 0;
  389. }
  390. static u32 asle_set_als_illum(struct drm_i915_private *dev_priv, u32 alsi)
  391. {
  392. /* alsi is the current ALS reading in lux. 0 indicates below sensor
  393. range, 0xffff indicates above sensor range. 1-0xfffe are valid */
  394. DRM_DEBUG_DRIVER("Illum is not supported\n");
  395. return ASLC_ALS_ILLUM_FAILED;
  396. }
  397. static u32 asle_set_pwm_freq(struct drm_i915_private *dev_priv, u32 pfmb)
  398. {
  399. DRM_DEBUG_DRIVER("PWM freq is not supported\n");
  400. return ASLC_PWM_FREQ_FAILED;
  401. }
  402. static u32 asle_set_pfit(struct drm_i915_private *dev_priv, u32 pfit)
  403. {
  404. /* Panel fitting is currently controlled by the X code, so this is a
  405. noop until modesetting support works fully */
  406. DRM_DEBUG_DRIVER("Pfit is not supported\n");
  407. return ASLC_PFIT_FAILED;
  408. }
  409. static u32 asle_set_supported_rotation_angles(struct drm_i915_private *dev_priv, u32 srot)
  410. {
  411. DRM_DEBUG_DRIVER("SROT is not supported\n");
  412. return ASLC_ROTATION_ANGLES_FAILED;
  413. }
  414. static u32 asle_set_button_array(struct drm_i915_private *dev_priv, u32 iuer)
  415. {
  416. if (!iuer)
  417. DRM_DEBUG_DRIVER("Button array event is not supported (nothing)\n");
  418. if (iuer & ASLE_IUER_ROTATION_LOCK_BTN)
  419. DRM_DEBUG_DRIVER("Button array event is not supported (rotation lock)\n");
  420. if (iuer & ASLE_IUER_VOLUME_DOWN_BTN)
  421. DRM_DEBUG_DRIVER("Button array event is not supported (volume down)\n");
  422. if (iuer & ASLE_IUER_VOLUME_UP_BTN)
  423. DRM_DEBUG_DRIVER("Button array event is not supported (volume up)\n");
  424. if (iuer & ASLE_IUER_WINDOWS_BTN)
  425. DRM_DEBUG_DRIVER("Button array event is not supported (windows)\n");
  426. if (iuer & ASLE_IUER_POWER_BTN)
  427. DRM_DEBUG_DRIVER("Button array event is not supported (power)\n");
  428. return ASLC_BUTTON_ARRAY_FAILED;
  429. }
  430. static u32 asle_set_convertible(struct drm_i915_private *dev_priv, u32 iuer)
  431. {
  432. if (iuer & ASLE_IUER_CONVERTIBLE)
  433. DRM_DEBUG_DRIVER("Convertible is not supported (clamshell)\n");
  434. else
  435. DRM_DEBUG_DRIVER("Convertible is not supported (slate)\n");
  436. return ASLC_CONVERTIBLE_FAILED;
  437. }
  438. static u32 asle_set_docking(struct drm_i915_private *dev_priv, u32 iuer)
  439. {
  440. if (iuer & ASLE_IUER_DOCKING)
  441. DRM_DEBUG_DRIVER("Docking is not supported (docked)\n");
  442. else
  443. DRM_DEBUG_DRIVER("Docking is not supported (undocked)\n");
  444. return ASLC_DOCKING_FAILED;
  445. }
  446. static u32 asle_isct_state(struct drm_i915_private *dev_priv)
  447. {
  448. DRM_DEBUG_DRIVER("ISCT is not supported\n");
  449. return ASLC_ISCT_STATE_FAILED;
  450. }
  451. static void asle_work(struct work_struct *work)
  452. {
  453. struct intel_opregion *opregion =
  454. container_of(work, struct intel_opregion, asle_work);
  455. struct drm_i915_private *dev_priv =
  456. container_of(opregion, struct drm_i915_private, opregion);
  457. struct opregion_asle *asle = dev_priv->opregion.asle;
  458. u32 aslc_stat = 0;
  459. u32 aslc_req;
  460. if (!asle)
  461. return;
  462. aslc_req = asle->aslc;
  463. if (!(aslc_req & ASLC_REQ_MSK)) {
  464. DRM_DEBUG_DRIVER("No request on ASLC interrupt 0x%08x\n",
  465. aslc_req);
  466. return;
  467. }
  468. if (aslc_req & ASLC_SET_ALS_ILLUM)
  469. aslc_stat |= asle_set_als_illum(dev_priv, asle->alsi);
  470. if (aslc_req & ASLC_SET_BACKLIGHT)
  471. aslc_stat |= asle_set_backlight(dev_priv, asle->bclp);
  472. if (aslc_req & ASLC_SET_PFIT)
  473. aslc_stat |= asle_set_pfit(dev_priv, asle->pfit);
  474. if (aslc_req & ASLC_SET_PWM_FREQ)
  475. aslc_stat |= asle_set_pwm_freq(dev_priv, asle->pfmb);
  476. if (aslc_req & ASLC_SUPPORTED_ROTATION_ANGLES)
  477. aslc_stat |= asle_set_supported_rotation_angles(dev_priv,
  478. asle->srot);
  479. if (aslc_req & ASLC_BUTTON_ARRAY)
  480. aslc_stat |= asle_set_button_array(dev_priv, asle->iuer);
  481. if (aslc_req & ASLC_CONVERTIBLE_INDICATOR)
  482. aslc_stat |= asle_set_convertible(dev_priv, asle->iuer);
  483. if (aslc_req & ASLC_DOCKING_INDICATOR)
  484. aslc_stat |= asle_set_docking(dev_priv, asle->iuer);
  485. if (aslc_req & ASLC_ISCT_STATE_CHANGE)
  486. aslc_stat |= asle_isct_state(dev_priv);
  487. asle->aslc = aslc_stat;
  488. }
  489. void intel_opregion_asle_intr(struct drm_i915_private *dev_priv)
  490. {
  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_i915_private *dev_priv)
  551. {
  552. struct intel_opregion *opregion = &dev_priv->opregion;
  553. struct pci_dev *pdev = dev_priv->dev->pdev;
  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(&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_priv->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_i915_private *dev_priv)
  645. {
  646. struct intel_opregion *opregion = &dev_priv->opregion;
  647. int i = 0;
  648. u32 disp_id;
  649. /* Initialize the CADL field by duplicating the DIDL values.
  650. * Technically, this is not always correct as display outputs may exist,
  651. * but not active. This initialization is necessary for some Clevo
  652. * laptops that check this field before processing the brightness and
  653. * display switching hotkeys. Just like DIDL, CADL is NULL-terminated if
  654. * there are less than eight devices. */
  655. do {
  656. disp_id = get_did(opregion, i);
  657. opregion->acpi->cadl[i] = disp_id;
  658. } while (++i < 8 && disp_id != 0);
  659. }
  660. void intel_opregion_register(struct drm_i915_private *dev_priv)
  661. {
  662. struct intel_opregion *opregion = &dev_priv->opregion;
  663. if (!opregion->header)
  664. return;
  665. if (opregion->acpi) {
  666. intel_didl_outputs(dev_priv);
  667. intel_setup_cadls(dev_priv);
  668. /* Notify BIOS we are ready to handle ACPI video ext notifs.
  669. * Right now, all the events are handled by the ACPI video module.
  670. * We don't actually need to do anything with them. */
  671. opregion->acpi->csts = 0;
  672. opregion->acpi->drdy = 1;
  673. system_opregion = opregion;
  674. register_acpi_notifier(&intel_opregion_notifier);
  675. }
  676. if (opregion->asle) {
  677. opregion->asle->tche = ASLE_TCHE_BLC_EN;
  678. opregion->asle->ardy = ASLE_ARDY_READY;
  679. }
  680. }
  681. void intel_opregion_unregister(struct drm_i915_private *dev_priv)
  682. {
  683. struct intel_opregion *opregion = &dev_priv->opregion;
  684. if (!opregion->header)
  685. return;
  686. if (opregion->asle)
  687. opregion->asle->ardy = ASLE_ARDY_NOT_READY;
  688. cancel_work_sync(&dev_priv->opregion.asle_work);
  689. if (opregion->acpi) {
  690. opregion->acpi->drdy = 0;
  691. system_opregion = NULL;
  692. unregister_acpi_notifier(&intel_opregion_notifier);
  693. }
  694. /* just clear all opregion memory pointers now */
  695. memunmap(opregion->header);
  696. if (opregion->rvda) {
  697. memunmap(opregion->rvda);
  698. opregion->rvda = NULL;
  699. }
  700. opregion->header = NULL;
  701. opregion->acpi = NULL;
  702. opregion->swsci = NULL;
  703. opregion->asle = NULL;
  704. opregion->vbt = NULL;
  705. opregion->lid_state = NULL;
  706. }
  707. static void swsci_setup(struct drm_i915_private *dev_priv)
  708. {
  709. struct intel_opregion *opregion = &dev_priv->opregion;
  710. bool requested_callbacks = false;
  711. u32 tmp;
  712. /* Sub-function code 0 is okay, let's allow them. */
  713. opregion->swsci_gbda_sub_functions = 1;
  714. opregion->swsci_sbcb_sub_functions = 1;
  715. /* We use GBDA to ask for supported GBDA calls. */
  716. if (swsci(dev_priv, SWSCI_GBDA_SUPPORTED_CALLS, 0, &tmp) == 0) {
  717. /* make the bits match the sub-function codes */
  718. tmp <<= 1;
  719. opregion->swsci_gbda_sub_functions |= tmp;
  720. }
  721. /*
  722. * We also use GBDA to ask for _requested_ SBCB callbacks. The driver
  723. * must not call interfaces that are not specifically requested by the
  724. * bios.
  725. */
  726. if (swsci(dev_priv, SWSCI_GBDA_REQUESTED_CALLBACKS, 0, &tmp) == 0) {
  727. /* here, the bits already match sub-function codes */
  728. opregion->swsci_sbcb_sub_functions |= tmp;
  729. requested_callbacks = true;
  730. }
  731. /*
  732. * But we use SBCB to ask for _supported_ SBCB calls. This does not mean
  733. * the callback is _requested_. But we still can't call interfaces that
  734. * are not requested.
  735. */
  736. if (swsci(dev_priv, SWSCI_SBCB_SUPPORTED_CALLBACKS, 0, &tmp) == 0) {
  737. /* make the bits match the sub-function codes */
  738. u32 low = tmp & 0x7ff;
  739. u32 high = tmp & ~0xfff; /* bit 11 is reserved */
  740. tmp = (high << 4) | (low << 1) | 1;
  741. /* best guess what to do with supported wrt requested */
  742. if (requested_callbacks) {
  743. u32 req = opregion->swsci_sbcb_sub_functions;
  744. if ((req & tmp) != req)
  745. DRM_DEBUG_DRIVER("SWSCI BIOS requested (%08x) SBCB callbacks that are not supported (%08x)\n", req, tmp);
  746. /* XXX: for now, trust the requested callbacks */
  747. /* opregion->swsci_sbcb_sub_functions &= tmp; */
  748. } else {
  749. opregion->swsci_sbcb_sub_functions |= tmp;
  750. }
  751. }
  752. DRM_DEBUG_DRIVER("SWSCI GBDA callbacks %08x, SBCB callbacks %08x\n",
  753. opregion->swsci_gbda_sub_functions,
  754. opregion->swsci_sbcb_sub_functions);
  755. }
  756. static int intel_no_opregion_vbt_callback(const struct dmi_system_id *id)
  757. {
  758. DRM_DEBUG_KMS("Falling back to manually reading VBT from "
  759. "VBIOS ROM for %s\n", id->ident);
  760. return 1;
  761. }
  762. static const struct dmi_system_id intel_no_opregion_vbt[] = {
  763. {
  764. .callback = intel_no_opregion_vbt_callback,
  765. .ident = "ThinkCentre A57",
  766. .matches = {
  767. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  768. DMI_MATCH(DMI_PRODUCT_NAME, "97027RG"),
  769. },
  770. },
  771. { }
  772. };
  773. int intel_opregion_setup(struct drm_i915_private *dev_priv)
  774. {
  775. struct intel_opregion *opregion = &dev_priv->opregion;
  776. struct pci_dev *pdev = dev_priv->dev->pdev;
  777. u32 asls, mboxes;
  778. char buf[sizeof(OPREGION_SIGNATURE)];
  779. int err = 0;
  780. void *base;
  781. BUILD_BUG_ON(sizeof(struct opregion_header) != 0x100);
  782. BUILD_BUG_ON(sizeof(struct opregion_acpi) != 0x100);
  783. BUILD_BUG_ON(sizeof(struct opregion_swsci) != 0x100);
  784. BUILD_BUG_ON(sizeof(struct opregion_asle) != 0x100);
  785. BUILD_BUG_ON(sizeof(struct opregion_asle_ext) != 0x400);
  786. pci_read_config_dword(pdev, ASLS, &asls);
  787. DRM_DEBUG_DRIVER("graphic opregion physical addr: 0x%x\n", asls);
  788. if (asls == 0) {
  789. DRM_DEBUG_DRIVER("ACPI OpRegion not supported!\n");
  790. return -ENOTSUPP;
  791. }
  792. INIT_WORK(&opregion->asle_work, asle_work);
  793. base = memremap(asls, OPREGION_SIZE, MEMREMAP_WB);
  794. if (!base)
  795. return -ENOMEM;
  796. memcpy(buf, base, sizeof(buf));
  797. if (memcmp(buf, OPREGION_SIGNATURE, 16)) {
  798. DRM_DEBUG_DRIVER("opregion signature mismatch\n");
  799. err = -EINVAL;
  800. goto err_out;
  801. }
  802. opregion->header = base;
  803. opregion->lid_state = base + ACPI_CLID;
  804. mboxes = opregion->header->mboxes;
  805. if (mboxes & MBOX_ACPI) {
  806. DRM_DEBUG_DRIVER("Public ACPI methods supported\n");
  807. opregion->acpi = base + OPREGION_ACPI_OFFSET;
  808. }
  809. if (mboxes & MBOX_SWSCI) {
  810. DRM_DEBUG_DRIVER("SWSCI supported\n");
  811. opregion->swsci = base + OPREGION_SWSCI_OFFSET;
  812. swsci_setup(dev_priv);
  813. }
  814. if (mboxes & MBOX_ASLE) {
  815. DRM_DEBUG_DRIVER("ASLE supported\n");
  816. opregion->asle = base + OPREGION_ASLE_OFFSET;
  817. opregion->asle->ardy = ASLE_ARDY_NOT_READY;
  818. }
  819. if (mboxes & MBOX_ASLE_EXT)
  820. DRM_DEBUG_DRIVER("ASLE extension supported\n");
  821. if (!dmi_check_system(intel_no_opregion_vbt)) {
  822. const void *vbt = NULL;
  823. u32 vbt_size = 0;
  824. if (opregion->header->opregion_ver >= 2 && opregion->asle &&
  825. opregion->asle->rvda && opregion->asle->rvds) {
  826. opregion->rvda = memremap(opregion->asle->rvda,
  827. opregion->asle->rvds,
  828. MEMREMAP_WB);
  829. vbt = opregion->rvda;
  830. vbt_size = opregion->asle->rvds;
  831. }
  832. if (intel_bios_is_valid_vbt(vbt, vbt_size)) {
  833. DRM_DEBUG_KMS("Found valid VBT in ACPI OpRegion (RVDA)\n");
  834. opregion->vbt = vbt;
  835. opregion->vbt_size = vbt_size;
  836. } else {
  837. vbt = base + OPREGION_VBT_OFFSET;
  838. vbt_size = OPREGION_ASLE_EXT_OFFSET - OPREGION_VBT_OFFSET;
  839. if (intel_bios_is_valid_vbt(vbt, vbt_size)) {
  840. DRM_DEBUG_KMS("Found valid VBT in ACPI OpRegion (Mailbox #4)\n");
  841. opregion->vbt = vbt;
  842. opregion->vbt_size = vbt_size;
  843. }
  844. }
  845. }
  846. return 0;
  847. err_out:
  848. memunmap(base);
  849. return err;
  850. }
  851. int
  852. intel_opregion_get_panel_type(struct drm_i915_private *dev_priv)
  853. {
  854. u32 panel_details;
  855. int ret;
  856. ret = swsci(dev_priv, SWSCI_GBDA_PANEL_DETAILS, 0x0, &panel_details);
  857. if (ret) {
  858. DRM_DEBUG_KMS("Failed to get panel details from OpRegion (%d)\n",
  859. ret);
  860. return ret;
  861. }
  862. ret = (panel_details >> 8) & 0xff;
  863. if (ret > 0x10) {
  864. DRM_DEBUG_KMS("Invalid OpRegion panel type 0x%x\n", ret);
  865. return -EINVAL;
  866. }
  867. /* fall back to VBT panel type? */
  868. if (ret == 0x0) {
  869. DRM_DEBUG_KMS("No panel type in OpRegion\n");
  870. return -ENODEV;
  871. }
  872. return ret - 1;
  873. }