intel_opregion.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. /* ASLE irq request bits */
  103. #define ASLE_SET_ALS_ILLUM (1 << 0)
  104. #define ASLE_SET_BACKLIGHT (1 << 1)
  105. #define ASLE_SET_PFIT (1 << 2)
  106. #define ASLE_SET_PWM_FREQ (1 << 3)
  107. #define ASLE_REQ_MSK 0xf
  108. /* response bits of ASLE irq request */
  109. #define ASLE_ALS_ILLUM_FAILED (1<<10)
  110. #define ASLE_BACKLIGHT_FAILED (1<<12)
  111. #define ASLE_PFIT_FAILED (1<<14)
  112. #define ASLE_PWM_FREQ_FAILED (1<<16)
  113. /* ASLE backlight brightness to set */
  114. #define ASLE_BCLP_VALID (1<<31)
  115. #define ASLE_BCLP_MSK (~(1<<31))
  116. /* ASLE panel fitting request */
  117. #define ASLE_PFIT_VALID (1<<31)
  118. #define ASLE_PFIT_CENTER (1<<0)
  119. #define ASLE_PFIT_STRETCH_TEXT (1<<1)
  120. #define ASLE_PFIT_STRETCH_GFX (1<<2)
  121. /* PWM frequency and minimum brightness */
  122. #define ASLE_PFMB_BRIGHTNESS_MASK (0xff)
  123. #define ASLE_PFMB_BRIGHTNESS_VALID (1<<8)
  124. #define ASLE_PFMB_PWM_MASK (0x7ffffe00)
  125. #define ASLE_PFMB_PWM_VALID (1<<31)
  126. #define ASLE_CBLV_VALID (1<<31)
  127. #define ACPI_OTHER_OUTPUT (0<<8)
  128. #define ACPI_VGA_OUTPUT (1<<8)
  129. #define ACPI_TV_OUTPUT (2<<8)
  130. #define ACPI_DIGITAL_OUTPUT (3<<8)
  131. #define ACPI_LVDS_OUTPUT (4<<8)
  132. #ifdef CONFIG_ACPI
  133. static u32 asle_set_backlight(struct drm_device *dev, u32 bclp)
  134. {
  135. struct drm_i915_private *dev_priv = dev->dev_private;
  136. struct opregion_asle __iomem *asle = dev_priv->opregion.asle;
  137. DRM_DEBUG_DRIVER("bclp = 0x%08x\n", bclp);
  138. if (!(bclp & ASLE_BCLP_VALID))
  139. return ASLE_BACKLIGHT_FAILED;
  140. bclp &= ASLE_BCLP_MSK;
  141. if (bclp > 255)
  142. return ASLE_BACKLIGHT_FAILED;
  143. intel_panel_set_backlight(dev, bclp, 255);
  144. iowrite32((bclp*0x64)/0xff | ASLE_CBLV_VALID, &asle->cblv);
  145. return 0;
  146. }
  147. static u32 asle_set_als_illum(struct drm_device *dev, u32 alsi)
  148. {
  149. /* alsi is the current ALS reading in lux. 0 indicates below sensor
  150. range, 0xffff indicates above sensor range. 1-0xfffe are valid */
  151. DRM_DEBUG_DRIVER("Illum is not supported\n");
  152. return ASLE_ALS_ILLUM_FAILED;
  153. }
  154. static u32 asle_set_pwm_freq(struct drm_device *dev, u32 pfmb)
  155. {
  156. DRM_DEBUG_DRIVER("PWM freq is not supported\n");
  157. return ASLE_PWM_FREQ_FAILED;
  158. }
  159. static u32 asle_set_pfit(struct drm_device *dev, u32 pfit)
  160. {
  161. /* Panel fitting is currently controlled by the X code, so this is a
  162. noop until modesetting support works fully */
  163. DRM_DEBUG_DRIVER("Pfit is not supported\n");
  164. return ASLE_PFIT_FAILED;
  165. }
  166. void intel_opregion_asle_intr(struct drm_device *dev)
  167. {
  168. struct drm_i915_private *dev_priv = dev->dev_private;
  169. struct opregion_asle __iomem *asle = dev_priv->opregion.asle;
  170. u32 asle_stat = 0;
  171. u32 asle_req;
  172. if (!asle)
  173. return;
  174. asle_req = ioread32(&asle->aslc) & ASLE_REQ_MSK;
  175. if (!asle_req) {
  176. DRM_DEBUG_DRIVER("non asle set request??\n");
  177. return;
  178. }
  179. if (asle_req & ASLE_SET_ALS_ILLUM)
  180. asle_stat |= asle_set_als_illum(dev, ioread32(&asle->alsi));
  181. if (asle_req & ASLE_SET_BACKLIGHT)
  182. asle_stat |= asle_set_backlight(dev, ioread32(&asle->bclp));
  183. if (asle_req & ASLE_SET_PFIT)
  184. asle_stat |= asle_set_pfit(dev, ioread32(&asle->pfit));
  185. if (asle_req & ASLE_SET_PWM_FREQ)
  186. asle_stat |= asle_set_pwm_freq(dev, ioread32(&asle->pfmb));
  187. iowrite32(asle_stat, &asle->aslc);
  188. }
  189. #define ASLE_ALS_EN (1<<0)
  190. #define ASLE_BLC_EN (1<<1)
  191. #define ASLE_PFIT_EN (1<<2)
  192. #define ASLE_PFMB_EN (1<<3)
  193. void intel_opregion_enable_asle(struct drm_device *dev)
  194. {
  195. struct drm_i915_private *dev_priv = dev->dev_private;
  196. struct opregion_asle __iomem *asle = dev_priv->opregion.asle;
  197. if (asle) {
  198. if (IS_MOBILE(dev))
  199. intel_enable_asle(dev);
  200. iowrite32(ASLE_BLC_EN, &asle->tche);
  201. iowrite32(1, &asle->ardy);
  202. }
  203. }
  204. #define ACPI_EV_DISPLAY_SWITCH (1<<0)
  205. #define ACPI_EV_LID (1<<1)
  206. #define ACPI_EV_DOCK (1<<2)
  207. static struct intel_opregion *system_opregion;
  208. static int intel_opregion_video_event(struct notifier_block *nb,
  209. unsigned long val, void *data)
  210. {
  211. /* The only video events relevant to opregion are 0x80. These indicate
  212. either a docking event, lid switch or display switch request. In
  213. Linux, these are handled by the dock, button and video drivers.
  214. */
  215. struct opregion_acpi __iomem *acpi;
  216. struct acpi_bus_event *event = data;
  217. int ret = NOTIFY_OK;
  218. if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
  219. return NOTIFY_DONE;
  220. if (!system_opregion)
  221. return NOTIFY_DONE;
  222. acpi = system_opregion->acpi;
  223. if (event->type == 0x80 &&
  224. (ioread32(&acpi->cevt) & 1) == 0)
  225. ret = NOTIFY_BAD;
  226. iowrite32(0, &acpi->csts);
  227. return ret;
  228. }
  229. static struct notifier_block intel_opregion_notifier = {
  230. .notifier_call = intel_opregion_video_event,
  231. };
  232. /*
  233. * Initialise the DIDL field in opregion. This passes a list of devices to
  234. * the firmware. Values are defined by section B.4.2 of the ACPI specification
  235. * (version 3)
  236. */
  237. static void intel_didl_outputs(struct drm_device *dev)
  238. {
  239. struct drm_i915_private *dev_priv = dev->dev_private;
  240. struct intel_opregion *opregion = &dev_priv->opregion;
  241. struct drm_connector *connector;
  242. acpi_handle handle;
  243. struct acpi_device *acpi_dev, *acpi_cdev, *acpi_video_bus = NULL;
  244. unsigned long long device_id;
  245. acpi_status status;
  246. u32 temp;
  247. int i = 0;
  248. handle = DEVICE_ACPI_HANDLE(&dev->pdev->dev);
  249. if (!handle || acpi_bus_get_device(handle, &acpi_dev))
  250. return;
  251. if (acpi_is_video_device(acpi_dev))
  252. acpi_video_bus = acpi_dev;
  253. else {
  254. list_for_each_entry(acpi_cdev, &acpi_dev->children, node) {
  255. if (acpi_is_video_device(acpi_cdev)) {
  256. acpi_video_bus = acpi_cdev;
  257. break;
  258. }
  259. }
  260. }
  261. if (!acpi_video_bus) {
  262. pr_warn("No ACPI video bus found\n");
  263. return;
  264. }
  265. list_for_each_entry(acpi_cdev, &acpi_video_bus->children, node) {
  266. if (i >= 8) {
  267. dev_printk(KERN_ERR, &dev->pdev->dev,
  268. "More than 8 outputs detected\n");
  269. return;
  270. }
  271. status =
  272. acpi_evaluate_integer(acpi_cdev->handle, "_ADR",
  273. NULL, &device_id);
  274. if (ACPI_SUCCESS(status)) {
  275. if (!device_id)
  276. goto blind_set;
  277. iowrite32((u32)(device_id & 0x0f0f),
  278. &opregion->acpi->didl[i]);
  279. i++;
  280. }
  281. }
  282. end:
  283. /* If fewer than 8 outputs, the list must be null terminated */
  284. if (i < 8)
  285. iowrite32(0, &opregion->acpi->didl[i]);
  286. return;
  287. blind_set:
  288. i = 0;
  289. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  290. int output_type = ACPI_OTHER_OUTPUT;
  291. if (i >= 8) {
  292. dev_printk(KERN_ERR, &dev->pdev->dev,
  293. "More than 8 outputs detected\n");
  294. return;
  295. }
  296. switch (connector->connector_type) {
  297. case DRM_MODE_CONNECTOR_VGA:
  298. case DRM_MODE_CONNECTOR_DVIA:
  299. output_type = ACPI_VGA_OUTPUT;
  300. break;
  301. case DRM_MODE_CONNECTOR_Composite:
  302. case DRM_MODE_CONNECTOR_SVIDEO:
  303. case DRM_MODE_CONNECTOR_Component:
  304. case DRM_MODE_CONNECTOR_9PinDIN:
  305. output_type = ACPI_TV_OUTPUT;
  306. break;
  307. case DRM_MODE_CONNECTOR_DVII:
  308. case DRM_MODE_CONNECTOR_DVID:
  309. case DRM_MODE_CONNECTOR_DisplayPort:
  310. case DRM_MODE_CONNECTOR_HDMIA:
  311. case DRM_MODE_CONNECTOR_HDMIB:
  312. output_type = ACPI_DIGITAL_OUTPUT;
  313. break;
  314. case DRM_MODE_CONNECTOR_LVDS:
  315. output_type = ACPI_LVDS_OUTPUT;
  316. break;
  317. }
  318. temp = ioread32(&opregion->acpi->didl[i]);
  319. iowrite32(temp | (1<<31) | output_type | i,
  320. &opregion->acpi->didl[i]);
  321. i++;
  322. }
  323. goto end;
  324. }
  325. static void intel_setup_cadls(struct drm_device *dev)
  326. {
  327. struct drm_i915_private *dev_priv = dev->dev_private;
  328. struct intel_opregion *opregion = &dev_priv->opregion;
  329. int i = 0;
  330. u32 disp_id;
  331. /* Initialize the CADL field by duplicating the DIDL values.
  332. * Technically, this is not always correct as display outputs may exist,
  333. * but not active. This initialization is necessary for some Clevo
  334. * laptops that check this field before processing the brightness and
  335. * display switching hotkeys. Just like DIDL, CADL is NULL-terminated if
  336. * there are less than eight devices. */
  337. do {
  338. disp_id = ioread32(&opregion->acpi->didl[i]);
  339. iowrite32(disp_id, &opregion->acpi->cadl[i]);
  340. } while (++i < 8 && disp_id != 0);
  341. }
  342. void intel_opregion_init(struct drm_device *dev)
  343. {
  344. struct drm_i915_private *dev_priv = dev->dev_private;
  345. struct intel_opregion *opregion = &dev_priv->opregion;
  346. if (!opregion->header)
  347. return;
  348. if (opregion->acpi) {
  349. if (drm_core_check_feature(dev, DRIVER_MODESET)) {
  350. intel_didl_outputs(dev);
  351. intel_setup_cadls(dev);
  352. }
  353. /* Notify BIOS we are ready to handle ACPI video ext notifs.
  354. * Right now, all the events are handled by the ACPI video module.
  355. * We don't actually need to do anything with them. */
  356. iowrite32(0, &opregion->acpi->csts);
  357. iowrite32(1, &opregion->acpi->drdy);
  358. system_opregion = opregion;
  359. register_acpi_notifier(&intel_opregion_notifier);
  360. }
  361. if (opregion->asle)
  362. intel_opregion_enable_asle(dev);
  363. }
  364. void intel_opregion_fini(struct drm_device *dev)
  365. {
  366. struct drm_i915_private *dev_priv = dev->dev_private;
  367. struct intel_opregion *opregion = &dev_priv->opregion;
  368. if (!opregion->header)
  369. return;
  370. if (opregion->acpi) {
  371. iowrite32(0, &opregion->acpi->drdy);
  372. system_opregion = NULL;
  373. unregister_acpi_notifier(&intel_opregion_notifier);
  374. }
  375. /* just clear all opregion memory pointers now */
  376. iounmap(opregion->header);
  377. opregion->header = NULL;
  378. opregion->acpi = NULL;
  379. opregion->swsci = NULL;
  380. opregion->asle = NULL;
  381. opregion->vbt = NULL;
  382. }
  383. #endif
  384. int intel_opregion_setup(struct drm_device *dev)
  385. {
  386. struct drm_i915_private *dev_priv = dev->dev_private;
  387. struct intel_opregion *opregion = &dev_priv->opregion;
  388. void __iomem *base;
  389. u32 asls, mboxes;
  390. char buf[sizeof(OPREGION_SIGNATURE)];
  391. int err = 0;
  392. pci_read_config_dword(dev->pdev, PCI_ASLS, &asls);
  393. DRM_DEBUG_DRIVER("graphic opregion physical addr: 0x%x\n", asls);
  394. if (asls == 0) {
  395. DRM_DEBUG_DRIVER("ACPI OpRegion not supported!\n");
  396. return -ENOTSUPP;
  397. }
  398. base = acpi_os_ioremap(asls, OPREGION_SIZE);
  399. if (!base)
  400. return -ENOMEM;
  401. memcpy_fromio(buf, base, sizeof(buf));
  402. if (memcmp(buf, OPREGION_SIGNATURE, 16)) {
  403. DRM_DEBUG_DRIVER("opregion signature mismatch\n");
  404. err = -EINVAL;
  405. goto err_out;
  406. }
  407. opregion->header = base;
  408. opregion->vbt = base + OPREGION_VBT_OFFSET;
  409. opregion->lid_state = base + ACPI_CLID;
  410. mboxes = ioread32(&opregion->header->mboxes);
  411. if (mboxes & MBOX_ACPI) {
  412. DRM_DEBUG_DRIVER("Public ACPI methods supported\n");
  413. opregion->acpi = base + OPREGION_ACPI_OFFSET;
  414. }
  415. if (mboxes & MBOX_SWSCI) {
  416. DRM_DEBUG_DRIVER("SWSCI supported\n");
  417. opregion->swsci = base + OPREGION_SWSCI_OFFSET;
  418. }
  419. if (mboxes & MBOX_ASLE) {
  420. DRM_DEBUG_DRIVER("ASLE supported\n");
  421. opregion->asle = base + OPREGION_ASLE_OFFSET;
  422. }
  423. return 0;
  424. err_out:
  425. iounmap(base);
  426. return err;
  427. }