intel_opregion.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 <linux/acpi_io.h>
  30. #include <acpi/video.h>
  31. #include <drm/drmP.h>
  32. #include <drm/i915_drm.h>
  33. #include "i915_drv.h"
  34. #include "intel_drv.h"
  35. #define PCI_ASLE 0xe4
  36. #define PCI_ASLS 0xfc
  37. #define OPREGION_HEADER_OFFSET 0
  38. #define OPREGION_ACPI_OFFSET 0x100
  39. #define ACPI_CLID 0x01ac /* current lid state indicator */
  40. #define ACPI_CDCK 0x01b0 /* current docking state indicator */
  41. #define OPREGION_SWSCI_OFFSET 0x200
  42. #define OPREGION_ASLE_OFFSET 0x300
  43. #define OPREGION_VBT_OFFSET 0x400
  44. #define OPREGION_SIGNATURE "IntelGraphicsMem"
  45. #define MBOX_ACPI (1<<0)
  46. #define MBOX_SWSCI (1<<1)
  47. #define MBOX_ASLE (1<<2)
  48. struct opregion_header {
  49. u8 signature[16];
  50. u32 size;
  51. u32 opregion_ver;
  52. u8 bios_ver[32];
  53. u8 vbios_ver[16];
  54. u8 driver_ver[16];
  55. u32 mboxes;
  56. u8 reserved[164];
  57. } __attribute__((packed));
  58. /* OpRegion mailbox #1: public ACPI methods */
  59. struct opregion_acpi {
  60. u32 drdy; /* driver readiness */
  61. u32 csts; /* notification status */
  62. u32 cevt; /* current event */
  63. u8 rsvd1[20];
  64. u32 didl[8]; /* supported display devices ID list */
  65. u32 cpdl[8]; /* currently presented display list */
  66. u32 cadl[8]; /* currently active display list */
  67. u32 nadl[8]; /* next active devices list */
  68. u32 aslp; /* ASL sleep time-out */
  69. u32 tidx; /* toggle table index */
  70. u32 chpd; /* current hotplug enable indicator */
  71. u32 clid; /* current lid state*/
  72. u32 cdck; /* current docking state */
  73. u32 sxsw; /* Sx state resume */
  74. u32 evts; /* ASL supported events */
  75. u32 cnot; /* current OS notification */
  76. u32 nrdy; /* driver status */
  77. u8 rsvd2[60];
  78. } __attribute__((packed));
  79. /* OpRegion mailbox #2: SWSCI */
  80. struct opregion_swsci {
  81. u32 scic; /* SWSCI command|status|data */
  82. u32 parm; /* command parameters */
  83. u32 dslp; /* driver sleep time-out */
  84. u8 rsvd[244];
  85. } __attribute__((packed));
  86. /* OpRegion mailbox #3: ASLE */
  87. struct opregion_asle {
  88. u32 ardy; /* driver readiness */
  89. u32 aslc; /* ASLE interrupt command */
  90. u32 tche; /* technology enabled indicator */
  91. u32 alsi; /* current ALS illuminance reading */
  92. u32 bclp; /* backlight brightness to set */
  93. u32 pfit; /* panel fitting state */
  94. u32 cblv; /* current brightness level */
  95. u16 bclm[20]; /* backlight level duty cycle mapping table */
  96. u32 cpfm; /* current panel fitting mode */
  97. u32 epfm; /* enabled panel fitting modes */
  98. u8 plut[74]; /* panel LUT and identifier */
  99. u32 pfmb; /* PWM freq and min brightness */
  100. u8 rsvd[102];
  101. } __attribute__((packed));
  102. /* Driver readiness indicator */
  103. #define ASLE_ARDY_READY (1 << 0)
  104. #define ASLE_ARDY_NOT_READY (0 << 0)
  105. /* ASLE irq request bits */
  106. #define ASLE_SET_ALS_ILLUM (1 << 0)
  107. #define ASLE_SET_BACKLIGHT (1 << 1)
  108. #define ASLE_SET_PFIT (1 << 2)
  109. #define ASLE_SET_PWM_FREQ (1 << 3)
  110. #define ASLE_REQ_MSK 0xf
  111. /* response bits of ASLE irq request */
  112. #define ASLE_ALS_ILLUM_FAILED (1<<10)
  113. #define ASLE_BACKLIGHT_FAILED (1<<12)
  114. #define ASLE_PFIT_FAILED (1<<14)
  115. #define ASLE_PWM_FREQ_FAILED (1<<16)
  116. /* Technology enabled indicator */
  117. #define ASLE_TCHE_ALS_EN (1 << 0)
  118. #define ASLE_TCHE_BLC_EN (1 << 1)
  119. #define ASLE_TCHE_PFIT_EN (1 << 2)
  120. #define ASLE_TCHE_PFMB_EN (1 << 3)
  121. /* ASLE backlight brightness to set */
  122. #define ASLE_BCLP_VALID (1<<31)
  123. #define ASLE_BCLP_MSK (~(1<<31))
  124. /* ASLE panel fitting request */
  125. #define ASLE_PFIT_VALID (1<<31)
  126. #define ASLE_PFIT_CENTER (1<<0)
  127. #define ASLE_PFIT_STRETCH_TEXT (1<<1)
  128. #define ASLE_PFIT_STRETCH_GFX (1<<2)
  129. /* PWM frequency and minimum brightness */
  130. #define ASLE_PFMB_BRIGHTNESS_MASK (0xff)
  131. #define ASLE_PFMB_BRIGHTNESS_VALID (1<<8)
  132. #define ASLE_PFMB_PWM_MASK (0x7ffffe00)
  133. #define ASLE_PFMB_PWM_VALID (1<<31)
  134. #define ASLE_CBLV_VALID (1<<31)
  135. #define ACPI_OTHER_OUTPUT (0<<8)
  136. #define ACPI_VGA_OUTPUT (1<<8)
  137. #define ACPI_TV_OUTPUT (2<<8)
  138. #define ACPI_DIGITAL_OUTPUT (3<<8)
  139. #define ACPI_LVDS_OUTPUT (4<<8)
  140. #ifdef CONFIG_ACPI
  141. static u32 asle_set_backlight(struct drm_device *dev, u32 bclp)
  142. {
  143. struct drm_i915_private *dev_priv = dev->dev_private;
  144. struct opregion_asle __iomem *asle = dev_priv->opregion.asle;
  145. DRM_DEBUG_DRIVER("bclp = 0x%08x\n", bclp);
  146. if (!(bclp & ASLE_BCLP_VALID))
  147. return ASLE_BACKLIGHT_FAILED;
  148. bclp &= ASLE_BCLP_MSK;
  149. if (bclp > 255)
  150. return ASLE_BACKLIGHT_FAILED;
  151. intel_panel_set_backlight(dev, bclp, 255);
  152. iowrite32((bclp*0x64)/0xff | ASLE_CBLV_VALID, &asle->cblv);
  153. return 0;
  154. }
  155. static u32 asle_set_als_illum(struct drm_device *dev, u32 alsi)
  156. {
  157. /* alsi is the current ALS reading in lux. 0 indicates below sensor
  158. range, 0xffff indicates above sensor range. 1-0xfffe are valid */
  159. DRM_DEBUG_DRIVER("Illum is not supported\n");
  160. return ASLE_ALS_ILLUM_FAILED;
  161. }
  162. static u32 asle_set_pwm_freq(struct drm_device *dev, u32 pfmb)
  163. {
  164. DRM_DEBUG_DRIVER("PWM freq is not supported\n");
  165. return ASLE_PWM_FREQ_FAILED;
  166. }
  167. static u32 asle_set_pfit(struct drm_device *dev, u32 pfit)
  168. {
  169. /* Panel fitting is currently controlled by the X code, so this is a
  170. noop until modesetting support works fully */
  171. DRM_DEBUG_DRIVER("Pfit is not supported\n");
  172. return ASLE_PFIT_FAILED;
  173. }
  174. void intel_opregion_asle_intr(struct drm_device *dev)
  175. {
  176. struct drm_i915_private *dev_priv = dev->dev_private;
  177. struct opregion_asle __iomem *asle = dev_priv->opregion.asle;
  178. u32 asle_stat = 0;
  179. u32 asle_req;
  180. if (!asle)
  181. return;
  182. asle_req = ioread32(&asle->aslc) & ASLE_REQ_MSK;
  183. if (!asle_req) {
  184. DRM_DEBUG_DRIVER("non asle set request??\n");
  185. return;
  186. }
  187. if (asle_req & ASLE_SET_ALS_ILLUM)
  188. asle_stat |= asle_set_als_illum(dev, ioread32(&asle->alsi));
  189. if (asle_req & ASLE_SET_BACKLIGHT)
  190. asle_stat |= asle_set_backlight(dev, ioread32(&asle->bclp));
  191. if (asle_req & ASLE_SET_PFIT)
  192. asle_stat |= asle_set_pfit(dev, ioread32(&asle->pfit));
  193. if (asle_req & ASLE_SET_PWM_FREQ)
  194. asle_stat |= asle_set_pwm_freq(dev, ioread32(&asle->pfmb));
  195. iowrite32(asle_stat, &asle->aslc);
  196. }
  197. #define ACPI_EV_DISPLAY_SWITCH (1<<0)
  198. #define ACPI_EV_LID (1<<1)
  199. #define ACPI_EV_DOCK (1<<2)
  200. static struct intel_opregion *system_opregion;
  201. static int intel_opregion_video_event(struct notifier_block *nb,
  202. unsigned long val, void *data)
  203. {
  204. /* The only video events relevant to opregion are 0x80. These indicate
  205. either a docking event, lid switch or display switch request. In
  206. Linux, these are handled by the dock, button and video drivers.
  207. */
  208. struct opregion_acpi __iomem *acpi;
  209. struct acpi_bus_event *event = data;
  210. int ret = NOTIFY_OK;
  211. if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
  212. return NOTIFY_DONE;
  213. if (!system_opregion)
  214. return NOTIFY_DONE;
  215. acpi = system_opregion->acpi;
  216. if (event->type == 0x80 &&
  217. (ioread32(&acpi->cevt) & 1) == 0)
  218. ret = NOTIFY_BAD;
  219. iowrite32(0, &acpi->csts);
  220. return ret;
  221. }
  222. static struct notifier_block intel_opregion_notifier = {
  223. .notifier_call = intel_opregion_video_event,
  224. };
  225. /*
  226. * Initialise the DIDL field in opregion. This passes a list of devices to
  227. * the firmware. Values are defined by section B.4.2 of the ACPI specification
  228. * (version 3)
  229. */
  230. static void intel_didl_outputs(struct drm_device *dev)
  231. {
  232. struct drm_i915_private *dev_priv = dev->dev_private;
  233. struct intel_opregion *opregion = &dev_priv->opregion;
  234. struct drm_connector *connector;
  235. acpi_handle handle;
  236. struct acpi_device *acpi_dev, *acpi_cdev, *acpi_video_bus = NULL;
  237. unsigned long long device_id;
  238. acpi_status status;
  239. u32 temp;
  240. int i = 0;
  241. handle = DEVICE_ACPI_HANDLE(&dev->pdev->dev);
  242. if (!handle || acpi_bus_get_device(handle, &acpi_dev))
  243. return;
  244. if (acpi_is_video_device(handle))
  245. acpi_video_bus = acpi_dev;
  246. else {
  247. list_for_each_entry(acpi_cdev, &acpi_dev->children, node) {
  248. if (acpi_is_video_device(acpi_cdev->handle)) {
  249. acpi_video_bus = acpi_cdev;
  250. break;
  251. }
  252. }
  253. }
  254. if (!acpi_video_bus) {
  255. pr_warn("No ACPI video bus found\n");
  256. return;
  257. }
  258. list_for_each_entry(acpi_cdev, &acpi_video_bus->children, node) {
  259. if (i >= 8) {
  260. dev_dbg(&dev->pdev->dev,
  261. "More than 8 outputs detected via ACPI\n");
  262. return;
  263. }
  264. status =
  265. acpi_evaluate_integer(acpi_cdev->handle, "_ADR",
  266. NULL, &device_id);
  267. if (ACPI_SUCCESS(status)) {
  268. if (!device_id)
  269. goto blind_set;
  270. iowrite32((u32)(device_id & 0x0f0f),
  271. &opregion->acpi->didl[i]);
  272. i++;
  273. }
  274. }
  275. end:
  276. /* If fewer than 8 outputs, the list must be null terminated */
  277. if (i < 8)
  278. iowrite32(0, &opregion->acpi->didl[i]);
  279. return;
  280. blind_set:
  281. i = 0;
  282. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  283. int output_type = ACPI_OTHER_OUTPUT;
  284. if (i >= 8) {
  285. dev_dbg(&dev->pdev->dev,
  286. "More than 8 outputs in connector list\n");
  287. return;
  288. }
  289. switch (connector->connector_type) {
  290. case DRM_MODE_CONNECTOR_VGA:
  291. case DRM_MODE_CONNECTOR_DVIA:
  292. output_type = ACPI_VGA_OUTPUT;
  293. break;
  294. case DRM_MODE_CONNECTOR_Composite:
  295. case DRM_MODE_CONNECTOR_SVIDEO:
  296. case DRM_MODE_CONNECTOR_Component:
  297. case DRM_MODE_CONNECTOR_9PinDIN:
  298. output_type = ACPI_TV_OUTPUT;
  299. break;
  300. case DRM_MODE_CONNECTOR_DVII:
  301. case DRM_MODE_CONNECTOR_DVID:
  302. case DRM_MODE_CONNECTOR_DisplayPort:
  303. case DRM_MODE_CONNECTOR_HDMIA:
  304. case DRM_MODE_CONNECTOR_HDMIB:
  305. output_type = ACPI_DIGITAL_OUTPUT;
  306. break;
  307. case DRM_MODE_CONNECTOR_LVDS:
  308. output_type = ACPI_LVDS_OUTPUT;
  309. break;
  310. }
  311. temp = ioread32(&opregion->acpi->didl[i]);
  312. iowrite32(temp | (1<<31) | output_type | i,
  313. &opregion->acpi->didl[i]);
  314. i++;
  315. }
  316. goto end;
  317. }
  318. static void intel_setup_cadls(struct drm_device *dev)
  319. {
  320. struct drm_i915_private *dev_priv = dev->dev_private;
  321. struct intel_opregion *opregion = &dev_priv->opregion;
  322. int i = 0;
  323. u32 disp_id;
  324. /* Initialize the CADL field by duplicating the DIDL values.
  325. * Technically, this is not always correct as display outputs may exist,
  326. * but not active. This initialization is necessary for some Clevo
  327. * laptops that check this field before processing the brightness and
  328. * display switching hotkeys. Just like DIDL, CADL is NULL-terminated if
  329. * there are less than eight devices. */
  330. do {
  331. disp_id = ioread32(&opregion->acpi->didl[i]);
  332. iowrite32(disp_id, &opregion->acpi->cadl[i]);
  333. } while (++i < 8 && disp_id != 0);
  334. }
  335. void intel_opregion_init(struct drm_device *dev)
  336. {
  337. struct drm_i915_private *dev_priv = dev->dev_private;
  338. struct intel_opregion *opregion = &dev_priv->opregion;
  339. if (!opregion->header)
  340. return;
  341. if (opregion->acpi) {
  342. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  343. intel_didl_outputs(dev);
  344. intel_setup_cadls(dev);
  345. }
  346. /* Notify BIOS we are ready to handle ACPI video ext notifs.
  347. * Right now, all the events are handled by the ACPI video module.
  348. * We don't actually need to do anything with them. */
  349. iowrite32(0, &opregion->acpi->csts);
  350. iowrite32(1, &opregion->acpi->drdy);
  351. system_opregion = opregion;
  352. register_acpi_notifier(&intel_opregion_notifier);
  353. }
  354. if (opregion->asle) {
  355. iowrite32(ASLE_TCHE_BLC_EN, &opregion->asle->tche);
  356. iowrite32(ASLE_ARDY_READY, &opregion->asle->ardy);
  357. }
  358. }
  359. void intel_opregion_fini(struct drm_device *dev)
  360. {
  361. struct drm_i915_private *dev_priv = dev->dev_private;
  362. struct intel_opregion *opregion = &dev_priv->opregion;
  363. if (!opregion->header)
  364. return;
  365. if (opregion->asle)
  366. iowrite32(ASLE_ARDY_NOT_READY, &opregion->asle->ardy);
  367. if (opregion->acpi) {
  368. iowrite32(0, &opregion->acpi->drdy);
  369. system_opregion = NULL;
  370. unregister_acpi_notifier(&intel_opregion_notifier);
  371. }
  372. /* just clear all opregion memory pointers now */
  373. iounmap(opregion->header);
  374. opregion->header = NULL;
  375. opregion->acpi = NULL;
  376. opregion->swsci = NULL;
  377. opregion->asle = NULL;
  378. opregion->vbt = NULL;
  379. }
  380. #endif
  381. int intel_opregion_setup(struct drm_device *dev)
  382. {
  383. struct drm_i915_private *dev_priv = dev->dev_private;
  384. struct intel_opregion *opregion = &dev_priv->opregion;
  385. void __iomem *base;
  386. u32 asls, mboxes;
  387. char buf[sizeof(OPREGION_SIGNATURE)];
  388. int err = 0;
  389. pci_read_config_dword(dev->pdev, PCI_ASLS, &asls);
  390. DRM_DEBUG_DRIVER("graphic opregion physical addr: 0x%x\n", asls);
  391. if (asls == 0) {
  392. DRM_DEBUG_DRIVER("ACPI OpRegion not supported!\n");
  393. return -ENOTSUPP;
  394. }
  395. base = acpi_os_ioremap(asls, OPREGION_SIZE);
  396. if (!base)
  397. return -ENOMEM;
  398. memcpy_fromio(buf, base, sizeof(buf));
  399. if (memcmp(buf, OPREGION_SIGNATURE, 16)) {
  400. DRM_DEBUG_DRIVER("opregion signature mismatch\n");
  401. err = -EINVAL;
  402. goto err_out;
  403. }
  404. opregion->header = base;
  405. opregion->vbt = base + OPREGION_VBT_OFFSET;
  406. opregion->lid_state = base + ACPI_CLID;
  407. mboxes = ioread32(&opregion->header->mboxes);
  408. if (mboxes & MBOX_ACPI) {
  409. DRM_DEBUG_DRIVER("Public ACPI methods supported\n");
  410. opregion->acpi = base + OPREGION_ACPI_OFFSET;
  411. }
  412. if (mboxes & MBOX_SWSCI) {
  413. DRM_DEBUG_DRIVER("SWSCI supported\n");
  414. opregion->swsci = base + OPREGION_SWSCI_OFFSET;
  415. }
  416. if (mboxes & MBOX_ASLE) {
  417. DRM_DEBUG_DRIVER("ASLE supported\n");
  418. opregion->asle = base + OPREGION_ASLE_OFFSET;
  419. iowrite32(ASLE_ARDY_NOT_READY, &opregion->asle->ardy);
  420. }
  421. return 0;
  422. err_out:
  423. iounmap(base);
  424. return err;
  425. }