intel_opregion.c 15 KB

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