intel_opregion.c 27 KB

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