nouveau_acpi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. #include <linux/pci.h>
  2. #include <linux/acpi.h>
  3. #include <linux/slab.h>
  4. #include <linux/mxm-wmi.h>
  5. #include <linux/vga_switcheroo.h>
  6. #include <drm/drm_edid.h>
  7. #include <acpi/video.h>
  8. #include "nouveau_drm.h"
  9. #include "nouveau_acpi.h"
  10. #define NOUVEAU_DSM_LED 0x02
  11. #define NOUVEAU_DSM_LED_STATE 0x00
  12. #define NOUVEAU_DSM_LED_OFF 0x10
  13. #define NOUVEAU_DSM_LED_STAMINA 0x11
  14. #define NOUVEAU_DSM_LED_SPEED 0x12
  15. #define NOUVEAU_DSM_POWER 0x03
  16. #define NOUVEAU_DSM_POWER_STATE 0x00
  17. #define NOUVEAU_DSM_POWER_SPEED 0x01
  18. #define NOUVEAU_DSM_POWER_STAMINA 0x02
  19. #define NOUVEAU_DSM_OPTIMUS_CAPS 0x1A
  20. #define NOUVEAU_DSM_OPTIMUS_FLAGS 0x1B
  21. #define NOUVEAU_DSM_OPTIMUS_POWERDOWN_PS3 (3 << 24)
  22. #define NOUVEAU_DSM_OPTIMUS_NO_POWERDOWN_PS3 (2 << 24)
  23. #define NOUVEAU_DSM_OPTIMUS_FLAGS_CHANGED (1)
  24. #define NOUVEAU_DSM_OPTIMUS_SET_POWERDOWN (NOUVEAU_DSM_OPTIMUS_POWERDOWN_PS3 | NOUVEAU_DSM_OPTIMUS_FLAGS_CHANGED)
  25. /* result of the optimus caps function */
  26. #define OPTIMUS_ENABLED (1 << 0)
  27. #define OPTIMUS_STATUS_MASK (3 << 3)
  28. #define OPTIMUS_STATUS_OFF (0 << 3)
  29. #define OPTIMUS_STATUS_ON_ENABLED (1 << 3)
  30. #define OPTIMUS_STATUS_PWR_STABLE (3 << 3)
  31. #define OPTIMUS_DISPLAY_HOTPLUG (1 << 6)
  32. #define OPTIMUS_CAPS_MASK (7 << 24)
  33. #define OPTIMUS_DYNAMIC_PWR_CAP (1 << 24)
  34. #define OPTIMUS_AUDIO_CAPS_MASK (3 << 27)
  35. #define OPTIMUS_HDA_CODEC_MASK (2 << 27) /* hda bios control */
  36. static struct nouveau_dsm_priv {
  37. bool dsm_detected;
  38. bool optimus_detected;
  39. acpi_handle dhandle;
  40. acpi_handle other_handle;
  41. acpi_handle rom_handle;
  42. } nouveau_dsm_priv;
  43. bool nouveau_is_optimus(void) {
  44. return nouveau_dsm_priv.optimus_detected;
  45. }
  46. bool nouveau_is_v1_dsm(void) {
  47. return nouveau_dsm_priv.dsm_detected;
  48. }
  49. #define NOUVEAU_DSM_HAS_MUX 0x1
  50. #define NOUVEAU_DSM_HAS_OPT 0x2
  51. #ifdef CONFIG_VGA_SWITCHEROO
  52. static const char nouveau_dsm_muid[] = {
  53. 0xA0, 0xA0, 0x95, 0x9D, 0x60, 0x00, 0x48, 0x4D,
  54. 0xB3, 0x4D, 0x7E, 0x5F, 0xEA, 0x12, 0x9F, 0xD4,
  55. };
  56. static const char nouveau_op_dsm_muid[] = {
  57. 0xF8, 0xD8, 0x86, 0xA4, 0xDA, 0x0B, 0x1B, 0x47,
  58. 0xA7, 0x2B, 0x60, 0x42, 0xA6, 0xB5, 0xBE, 0xE0,
  59. };
  60. static int nouveau_optimus_dsm(acpi_handle handle, int func, int arg, uint32_t *result)
  61. {
  62. int i;
  63. union acpi_object *obj;
  64. char args_buff[4];
  65. union acpi_object argv4 = {
  66. .buffer.type = ACPI_TYPE_BUFFER,
  67. .buffer.length = 4,
  68. .buffer.pointer = args_buff
  69. };
  70. /* ACPI is little endian, AABBCCDD becomes {DD,CC,BB,AA} */
  71. for (i = 0; i < 4; i++)
  72. args_buff[i] = (arg >> i * 8) & 0xFF;
  73. *result = 0;
  74. obj = acpi_evaluate_dsm_typed(handle, nouveau_op_dsm_muid, 0x00000100,
  75. func, &argv4, ACPI_TYPE_BUFFER);
  76. if (!obj) {
  77. acpi_handle_info(handle, "failed to evaluate _DSM\n");
  78. return AE_ERROR;
  79. } else {
  80. if (obj->buffer.length == 4) {
  81. *result |= obj->buffer.pointer[0];
  82. *result |= (obj->buffer.pointer[1] << 8);
  83. *result |= (obj->buffer.pointer[2] << 16);
  84. *result |= (obj->buffer.pointer[3] << 24);
  85. }
  86. ACPI_FREE(obj);
  87. }
  88. return 0;
  89. }
  90. /*
  91. * On some platforms, _DSM(nouveau_op_dsm_muid, func0) has special
  92. * requirements on the fourth parameter, so a private implementation
  93. * instead of using acpi_check_dsm().
  94. */
  95. static int nouveau_check_optimus_dsm(acpi_handle handle)
  96. {
  97. int result;
  98. /*
  99. * Function 0 returns a Buffer containing available functions.
  100. * The args parameter is ignored for function 0, so just put 0 in it
  101. */
  102. if (nouveau_optimus_dsm(handle, 0, 0, &result))
  103. return 0;
  104. /*
  105. * ACPI Spec v4 9.14.1: if bit 0 is zero, no function is supported.
  106. * If the n-th bit is enabled, function n is supported
  107. */
  108. return result & 1 && result & (1 << NOUVEAU_DSM_OPTIMUS_CAPS);
  109. }
  110. static int nouveau_dsm(acpi_handle handle, int func, int arg)
  111. {
  112. int ret = 0;
  113. union acpi_object *obj;
  114. union acpi_object argv4 = {
  115. .integer.type = ACPI_TYPE_INTEGER,
  116. .integer.value = arg,
  117. };
  118. obj = acpi_evaluate_dsm_typed(handle, nouveau_dsm_muid, 0x00000102,
  119. func, &argv4, ACPI_TYPE_INTEGER);
  120. if (!obj) {
  121. acpi_handle_info(handle, "failed to evaluate _DSM\n");
  122. return AE_ERROR;
  123. } else {
  124. if (obj->integer.value == 0x80000002)
  125. ret = -ENODEV;
  126. ACPI_FREE(obj);
  127. }
  128. return ret;
  129. }
  130. static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id)
  131. {
  132. mxm_wmi_call_mxmx(mux_id == NOUVEAU_DSM_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0);
  133. mxm_wmi_call_mxds(mux_id == NOUVEAU_DSM_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0);
  134. return nouveau_dsm(handle, NOUVEAU_DSM_LED, mux_id);
  135. }
  136. static int nouveau_dsm_set_discrete_state(acpi_handle handle, enum vga_switcheroo_state state)
  137. {
  138. int arg;
  139. if (state == VGA_SWITCHEROO_ON)
  140. arg = NOUVEAU_DSM_POWER_SPEED;
  141. else
  142. arg = NOUVEAU_DSM_POWER_STAMINA;
  143. nouveau_dsm(handle, NOUVEAU_DSM_POWER, arg);
  144. return 0;
  145. }
  146. static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id)
  147. {
  148. if (!nouveau_dsm_priv.dsm_detected)
  149. return 0;
  150. if (id == VGA_SWITCHEROO_IGD)
  151. return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_STAMINA);
  152. else
  153. return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_SPEED);
  154. }
  155. static int nouveau_dsm_power_state(enum vga_switcheroo_client_id id,
  156. enum vga_switcheroo_state state)
  157. {
  158. if (id == VGA_SWITCHEROO_IGD)
  159. return 0;
  160. /* Optimus laptops have the card already disabled in
  161. * nouveau_switcheroo_set_state */
  162. if (!nouveau_dsm_priv.dsm_detected)
  163. return 0;
  164. return nouveau_dsm_set_discrete_state(nouveau_dsm_priv.dhandle, state);
  165. }
  166. static int nouveau_dsm_get_client_id(struct pci_dev *pdev)
  167. {
  168. /* easy option one - intel vendor ID means Integrated */
  169. if (pdev->vendor == PCI_VENDOR_ID_INTEL)
  170. return VGA_SWITCHEROO_IGD;
  171. /* is this device on Bus 0? - this may need improving */
  172. if (pdev->bus->number == 0)
  173. return VGA_SWITCHEROO_IGD;
  174. return VGA_SWITCHEROO_DIS;
  175. }
  176. static struct vga_switcheroo_handler nouveau_dsm_handler = {
  177. .switchto = nouveau_dsm_switchto,
  178. .power_state = nouveau_dsm_power_state,
  179. .get_client_id = nouveau_dsm_get_client_id,
  180. };
  181. static int nouveau_dsm_pci_probe(struct pci_dev *pdev)
  182. {
  183. acpi_handle dhandle;
  184. int retval = 0;
  185. dhandle = ACPI_HANDLE(&pdev->dev);
  186. if (!dhandle)
  187. return false;
  188. if (!acpi_has_method(dhandle, "_DSM")) {
  189. nouveau_dsm_priv.other_handle = dhandle;
  190. return false;
  191. }
  192. if (acpi_check_dsm(dhandle, nouveau_dsm_muid, 0x00000102,
  193. 1 << NOUVEAU_DSM_POWER))
  194. retval |= NOUVEAU_DSM_HAS_MUX;
  195. if (nouveau_check_optimus_dsm(dhandle))
  196. retval |= NOUVEAU_DSM_HAS_OPT;
  197. if (retval & NOUVEAU_DSM_HAS_OPT) {
  198. uint32_t result;
  199. nouveau_optimus_dsm(dhandle, NOUVEAU_DSM_OPTIMUS_CAPS, 0,
  200. &result);
  201. dev_info(&pdev->dev, "optimus capabilities: %s, status %s%s\n",
  202. (result & OPTIMUS_ENABLED) ? "enabled" : "disabled",
  203. (result & OPTIMUS_DYNAMIC_PWR_CAP) ? "dynamic power, " : "",
  204. (result & OPTIMUS_HDA_CODEC_MASK) ? "hda bios codec supported" : "");
  205. }
  206. if (retval)
  207. nouveau_dsm_priv.dhandle = dhandle;
  208. return retval;
  209. }
  210. static bool nouveau_dsm_detect(void)
  211. {
  212. char acpi_method_name[255] = { 0 };
  213. struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
  214. struct pci_dev *pdev = NULL;
  215. int has_dsm = 0;
  216. int has_optimus = 0;
  217. int vga_count = 0;
  218. bool guid_valid;
  219. int retval;
  220. bool ret = false;
  221. /* lookup the MXM GUID */
  222. guid_valid = mxm_wmi_supported();
  223. if (guid_valid)
  224. printk("MXM: GUID detected in BIOS\n");
  225. /* now do DSM detection */
  226. while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
  227. vga_count++;
  228. retval = nouveau_dsm_pci_probe(pdev);
  229. if (retval & NOUVEAU_DSM_HAS_MUX)
  230. has_dsm |= 1;
  231. if (retval & NOUVEAU_DSM_HAS_OPT)
  232. has_optimus = 1;
  233. }
  234. while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_3D << 8, pdev)) != NULL) {
  235. vga_count++;
  236. retval = nouveau_dsm_pci_probe(pdev);
  237. if (retval & NOUVEAU_DSM_HAS_MUX)
  238. has_dsm |= 1;
  239. if (retval & NOUVEAU_DSM_HAS_OPT)
  240. has_optimus = 1;
  241. }
  242. /* find the optimus DSM or the old v1 DSM */
  243. if (has_optimus == 1) {
  244. acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
  245. &buffer);
  246. printk(KERN_INFO "VGA switcheroo: detected Optimus DSM method %s handle\n",
  247. acpi_method_name);
  248. nouveau_dsm_priv.optimus_detected = true;
  249. ret = true;
  250. } else if (vga_count == 2 && has_dsm && guid_valid) {
  251. acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
  252. &buffer);
  253. printk(KERN_INFO "VGA switcheroo: detected DSM switching method %s handle\n",
  254. acpi_method_name);
  255. nouveau_dsm_priv.dsm_detected = true;
  256. /*
  257. * On some systems hotplug events are generated for the device
  258. * being switched off when _DSM is executed. They cause ACPI
  259. * hotplug to trigger and attempt to remove the device from
  260. * the system, which causes it to break down. Prevent that from
  261. * happening by setting the no_hotplug flag for the involved
  262. * ACPI device objects.
  263. */
  264. acpi_bus_no_hotplug(nouveau_dsm_priv.dhandle);
  265. acpi_bus_no_hotplug(nouveau_dsm_priv.other_handle);
  266. ret = true;
  267. }
  268. return ret;
  269. }
  270. void nouveau_register_dsm_handler(void)
  271. {
  272. bool r;
  273. r = nouveau_dsm_detect();
  274. if (!r)
  275. return;
  276. vga_switcheroo_register_handler(&nouveau_dsm_handler);
  277. }
  278. /* Must be called for Optimus models before the card can be turned off */
  279. void nouveau_switcheroo_optimus_dsm(void)
  280. {
  281. u32 result = 0;
  282. if (!nouveau_dsm_priv.optimus_detected)
  283. return;
  284. nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_FLAGS,
  285. 0x3, &result);
  286. nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_CAPS,
  287. NOUVEAU_DSM_OPTIMUS_SET_POWERDOWN, &result);
  288. }
  289. void nouveau_unregister_dsm_handler(void)
  290. {
  291. if (nouveau_dsm_priv.optimus_detected || nouveau_dsm_priv.dsm_detected)
  292. vga_switcheroo_unregister_handler();
  293. }
  294. #else
  295. void nouveau_register_dsm_handler(void) {}
  296. void nouveau_unregister_dsm_handler(void) {}
  297. void nouveau_switcheroo_optimus_dsm(void) {}
  298. #endif
  299. /* retrieve the ROM in 4k blocks */
  300. static int nouveau_rom_call(acpi_handle rom_handle, uint8_t *bios,
  301. int offset, int len)
  302. {
  303. acpi_status status;
  304. union acpi_object rom_arg_elements[2], *obj;
  305. struct acpi_object_list rom_arg;
  306. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
  307. rom_arg.count = 2;
  308. rom_arg.pointer = &rom_arg_elements[0];
  309. rom_arg_elements[0].type = ACPI_TYPE_INTEGER;
  310. rom_arg_elements[0].integer.value = offset;
  311. rom_arg_elements[1].type = ACPI_TYPE_INTEGER;
  312. rom_arg_elements[1].integer.value = len;
  313. status = acpi_evaluate_object(rom_handle, NULL, &rom_arg, &buffer);
  314. if (ACPI_FAILURE(status)) {
  315. printk(KERN_INFO "failed to evaluate ROM got %s\n", acpi_format_exception(status));
  316. return -ENODEV;
  317. }
  318. obj = (union acpi_object *)buffer.pointer;
  319. memcpy(bios+offset, obj->buffer.pointer, len);
  320. kfree(buffer.pointer);
  321. return len;
  322. }
  323. bool nouveau_acpi_rom_supported(struct pci_dev *pdev)
  324. {
  325. acpi_status status;
  326. acpi_handle dhandle, rom_handle;
  327. dhandle = ACPI_HANDLE(&pdev->dev);
  328. if (!dhandle)
  329. return false;
  330. status = acpi_get_handle(dhandle, "_ROM", &rom_handle);
  331. if (ACPI_FAILURE(status))
  332. return false;
  333. nouveau_dsm_priv.rom_handle = rom_handle;
  334. return true;
  335. }
  336. int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len)
  337. {
  338. return nouveau_rom_call(nouveau_dsm_priv.rom_handle, bios, offset, len);
  339. }
  340. void *
  341. nouveau_acpi_edid(struct drm_device *dev, struct drm_connector *connector)
  342. {
  343. struct acpi_device *acpidev;
  344. acpi_handle handle;
  345. int type, ret;
  346. void *edid;
  347. switch (connector->connector_type) {
  348. case DRM_MODE_CONNECTOR_LVDS:
  349. case DRM_MODE_CONNECTOR_eDP:
  350. type = ACPI_VIDEO_DISPLAY_LCD;
  351. break;
  352. default:
  353. return NULL;
  354. }
  355. handle = ACPI_HANDLE(&dev->pdev->dev);
  356. if (!handle)
  357. return NULL;
  358. ret = acpi_bus_get_device(handle, &acpidev);
  359. if (ret)
  360. return NULL;
  361. ret = acpi_video_get_edid(acpidev, type, -1, &edid);
  362. if (ret < 0)
  363. return NULL;
  364. return kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
  365. }