intel_opregion.c 28 KB

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