intel_opregion.c 15 KB

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