intel_opregion.c 29 KB

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