nouveau_acpi.c 11 KB

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