nouveau_acpi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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_drv.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. bool optimus_flags_detected;
  40. bool optimus_skip_dsm;
  41. acpi_handle dhandle;
  42. acpi_handle rom_handle;
  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 char nouveau_dsm_muid[] = {
  52. 0xA0, 0xA0, 0x95, 0x9D, 0x60, 0x00, 0x48, 0x4D,
  53. 0xB3, 0x4D, 0x7E, 0x5F, 0xEA, 0x12, 0x9F, 0xD4,
  54. };
  55. static const char nouveau_op_dsm_muid[] = {
  56. 0xF8, 0xD8, 0x86, 0xA4, 0xDA, 0x0B, 0x1B, 0x47,
  57. 0xA7, 0x2B, 0x60, 0x42, 0xA6, 0xB5, 0xBE, 0xE0,
  58. };
  59. static int nouveau_optimus_dsm(acpi_handle handle, int func, int arg, uint32_t *result)
  60. {
  61. int i;
  62. union acpi_object *obj;
  63. char args_buff[4];
  64. union acpi_object argv4 = {
  65. .buffer.type = ACPI_TYPE_BUFFER,
  66. .buffer.length = 4,
  67. .buffer.pointer = args_buff
  68. };
  69. /* ACPI is little endian, AABBCCDD becomes {DD,CC,BB,AA} */
  70. for (i = 0; i < 4; i++)
  71. args_buff[i] = (arg >> i * 8) & 0xFF;
  72. *result = 0;
  73. obj = acpi_evaluate_dsm_typed(handle, nouveau_op_dsm_muid, 0x00000100,
  74. func, &argv4, ACPI_TYPE_BUFFER);
  75. if (!obj) {
  76. acpi_handle_info(handle, "failed to evaluate _DSM\n");
  77. return AE_ERROR;
  78. } else {
  79. if (obj->buffer.length == 4) {
  80. *result |= obj->buffer.pointer[0];
  81. *result |= (obj->buffer.pointer[1] << 8);
  82. *result |= (obj->buffer.pointer[2] << 16);
  83. *result |= (obj->buffer.pointer[3] << 24);
  84. }
  85. ACPI_FREE(obj);
  86. }
  87. return 0;
  88. }
  89. /*
  90. * On some platforms, _DSM(nouveau_op_dsm_muid, func0) has special
  91. * requirements on the fourth parameter, so a private implementation
  92. * instead of using acpi_check_dsm().
  93. */
  94. static int nouveau_dsm_get_optimus_functions(acpi_handle handle)
  95. {
  96. int result;
  97. /*
  98. * Function 0 returns a Buffer containing available functions.
  99. * The args parameter is ignored for function 0, so just put 0 in it
  100. */
  101. if (nouveau_optimus_dsm(handle, 0, 0, &result))
  102. return 0;
  103. /*
  104. * ACPI Spec v4 9.14.1: if bit 0 is zero, no function is supported.
  105. * If the n-th bit is enabled, function n is supported
  106. */
  107. if (result & 1 && result & (1 << NOUVEAU_DSM_OPTIMUS_CAPS))
  108. return result;
  109. return 0;
  110. }
  111. static int nouveau_dsm(acpi_handle handle, int func, int arg)
  112. {
  113. int ret = 0;
  114. union acpi_object *obj;
  115. union acpi_object argv4 = {
  116. .integer.type = ACPI_TYPE_INTEGER,
  117. .integer.value = arg,
  118. };
  119. obj = acpi_evaluate_dsm_typed(handle, nouveau_dsm_muid, 0x00000102,
  120. func, &argv4, ACPI_TYPE_INTEGER);
  121. if (!obj) {
  122. acpi_handle_info(handle, "failed to evaluate _DSM\n");
  123. return AE_ERROR;
  124. } else {
  125. if (obj->integer.value == 0x80000002)
  126. ret = -ENODEV;
  127. ACPI_FREE(obj);
  128. }
  129. return ret;
  130. }
  131. static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id)
  132. {
  133. mxm_wmi_call_mxmx(mux_id == NOUVEAU_DSM_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0);
  134. mxm_wmi_call_mxds(mux_id == NOUVEAU_DSM_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0);
  135. return nouveau_dsm(handle, NOUVEAU_DSM_LED, mux_id);
  136. }
  137. static int nouveau_dsm_set_discrete_state(acpi_handle handle, enum vga_switcheroo_state state)
  138. {
  139. int arg;
  140. if (state == VGA_SWITCHEROO_ON)
  141. arg = NOUVEAU_DSM_POWER_SPEED;
  142. else
  143. arg = NOUVEAU_DSM_POWER_STAMINA;
  144. nouveau_dsm(handle, NOUVEAU_DSM_POWER, arg);
  145. return 0;
  146. }
  147. static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id)
  148. {
  149. if (!nouveau_dsm_priv.dsm_detected)
  150. return 0;
  151. if (id == VGA_SWITCHEROO_IGD)
  152. return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_STAMINA);
  153. else
  154. return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_SPEED);
  155. }
  156. static int nouveau_dsm_power_state(enum vga_switcheroo_client_id id,
  157. enum vga_switcheroo_state state)
  158. {
  159. if (id == VGA_SWITCHEROO_IGD)
  160. return 0;
  161. /* Optimus laptops have the card already disabled in
  162. * nouveau_switcheroo_set_state */
  163. if (!nouveau_dsm_priv.dsm_detected)
  164. return 0;
  165. return nouveau_dsm_set_discrete_state(nouveau_dsm_priv.dhandle, state);
  166. }
  167. static int nouveau_dsm_get_client_id(struct pci_dev *pdev)
  168. {
  169. /* easy option one - intel vendor ID means Integrated */
  170. if (pdev->vendor == PCI_VENDOR_ID_INTEL)
  171. return VGA_SWITCHEROO_IGD;
  172. /* is this device on Bus 0? - this may need improving */
  173. if (pdev->bus->number == 0)
  174. return VGA_SWITCHEROO_IGD;
  175. return VGA_SWITCHEROO_DIS;
  176. }
  177. static const struct vga_switcheroo_handler nouveau_dsm_handler = {
  178. .switchto = nouveau_dsm_switchto,
  179. .power_state = nouveau_dsm_power_state,
  180. .get_client_id = nouveau_dsm_get_client_id,
  181. };
  182. /*
  183. * Firmware supporting Windows 8 or later do not use _DSM to put the device into
  184. * D3cold, they instead rely on disabling power resources on the parent.
  185. */
  186. static bool nouveau_pr3_present(struct pci_dev *pdev)
  187. {
  188. struct pci_dev *parent_pdev = pci_upstream_bridge(pdev);
  189. struct acpi_device *parent_adev;
  190. if (!parent_pdev)
  191. return false;
  192. if (!parent_pdev->bridge_d3) {
  193. /*
  194. * Parent PCI bridge is currently not power managed.
  195. * Since userspace can change these afterwards to be on
  196. * the safe side we stick with _DSM and prevent usage of
  197. * _PR3 from the bridge.
  198. */
  199. pci_d3cold_disable(pdev);
  200. return false;
  201. }
  202. parent_adev = ACPI_COMPANION(&parent_pdev->dev);
  203. if (!parent_adev)
  204. return false;
  205. return parent_adev->power.flags.power_resources &&
  206. acpi_has_method(parent_adev->handle, "_PR3");
  207. }
  208. static void nouveau_dsm_pci_probe(struct pci_dev *pdev, acpi_handle *dhandle_out,
  209. bool *has_mux, bool *has_opt,
  210. bool *has_opt_flags, bool *has_pr3)
  211. {
  212. acpi_handle dhandle;
  213. bool supports_mux;
  214. int optimus_funcs;
  215. dhandle = ACPI_HANDLE(&pdev->dev);
  216. if (!dhandle)
  217. return;
  218. if (!acpi_has_method(dhandle, "_DSM"))
  219. return;
  220. supports_mux = acpi_check_dsm(dhandle, nouveau_dsm_muid, 0x00000102,
  221. 1 << NOUVEAU_DSM_POWER);
  222. optimus_funcs = nouveau_dsm_get_optimus_functions(dhandle);
  223. /* Does not look like a Nvidia device. */
  224. if (!supports_mux && !optimus_funcs)
  225. return;
  226. *dhandle_out = dhandle;
  227. *has_mux = supports_mux;
  228. *has_opt = !!optimus_funcs;
  229. *has_opt_flags = optimus_funcs & (1 << NOUVEAU_DSM_OPTIMUS_FLAGS);
  230. *has_pr3 = false;
  231. if (optimus_funcs) {
  232. uint32_t result;
  233. nouveau_optimus_dsm(dhandle, NOUVEAU_DSM_OPTIMUS_CAPS, 0,
  234. &result);
  235. dev_info(&pdev->dev, "optimus capabilities: %s, status %s%s\n",
  236. (result & OPTIMUS_ENABLED) ? "enabled" : "disabled",
  237. (result & OPTIMUS_DYNAMIC_PWR_CAP) ? "dynamic power, " : "",
  238. (result & OPTIMUS_HDA_CODEC_MASK) ? "hda bios codec supported" : "");
  239. *has_pr3 = nouveau_pr3_present(pdev);
  240. }
  241. }
  242. static bool nouveau_dsm_detect(void)
  243. {
  244. char acpi_method_name[255] = { 0 };
  245. struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
  246. struct pci_dev *pdev = NULL;
  247. acpi_handle dhandle = NULL;
  248. bool has_mux = false;
  249. bool has_optimus = false;
  250. bool has_optimus_flags = false;
  251. bool has_power_resources = false;
  252. int vga_count = 0;
  253. bool guid_valid;
  254. bool ret = false;
  255. /* lookup the MXM GUID */
  256. guid_valid = mxm_wmi_supported();
  257. if (guid_valid)
  258. printk("MXM: GUID detected in BIOS\n");
  259. /* now do DSM detection */
  260. while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
  261. vga_count++;
  262. nouveau_dsm_pci_probe(pdev, &dhandle, &has_mux, &has_optimus,
  263. &has_optimus_flags, &has_power_resources);
  264. }
  265. while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_3D << 8, pdev)) != NULL) {
  266. vga_count++;
  267. nouveau_dsm_pci_probe(pdev, &dhandle, &has_mux, &has_optimus,
  268. &has_optimus_flags, &has_power_resources);
  269. }
  270. /* find the optimus DSM or the old v1 DSM */
  271. if (has_optimus) {
  272. nouveau_dsm_priv.dhandle = dhandle;
  273. acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
  274. &buffer);
  275. printk(KERN_INFO "VGA switcheroo: detected Optimus DSM method %s handle\n",
  276. acpi_method_name);
  277. if (has_power_resources)
  278. pr_info("nouveau: detected PR support, will not use DSM\n");
  279. nouveau_dsm_priv.optimus_detected = true;
  280. nouveau_dsm_priv.optimus_flags_detected = has_optimus_flags;
  281. nouveau_dsm_priv.optimus_skip_dsm = has_power_resources;
  282. ret = true;
  283. } else if (vga_count == 2 && has_mux && guid_valid) {
  284. nouveau_dsm_priv.dhandle = dhandle;
  285. acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
  286. &buffer);
  287. printk(KERN_INFO "VGA switcheroo: detected DSM switching method %s handle\n",
  288. acpi_method_name);
  289. nouveau_dsm_priv.dsm_detected = true;
  290. ret = true;
  291. }
  292. return ret;
  293. }
  294. void nouveau_register_dsm_handler(void)
  295. {
  296. bool r;
  297. r = nouveau_dsm_detect();
  298. if (!r)
  299. return;
  300. vga_switcheroo_register_handler(&nouveau_dsm_handler, 0);
  301. }
  302. /* Must be called for Optimus models before the card can be turned off */
  303. void nouveau_switcheroo_optimus_dsm(void)
  304. {
  305. u32 result = 0;
  306. if (!nouveau_dsm_priv.optimus_detected || nouveau_dsm_priv.optimus_skip_dsm)
  307. return;
  308. if (nouveau_dsm_priv.optimus_flags_detected)
  309. nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_FLAGS,
  310. 0x3, &result);
  311. nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_CAPS,
  312. NOUVEAU_DSM_OPTIMUS_SET_POWERDOWN, &result);
  313. }
  314. void nouveau_unregister_dsm_handler(void)
  315. {
  316. if (nouveau_dsm_priv.optimus_detected || nouveau_dsm_priv.dsm_detected)
  317. vga_switcheroo_unregister_handler();
  318. }
  319. #else
  320. void nouveau_register_dsm_handler(void) {}
  321. void nouveau_unregister_dsm_handler(void) {}
  322. void nouveau_switcheroo_optimus_dsm(void) {}
  323. #endif
  324. /* retrieve the ROM in 4k blocks */
  325. static int nouveau_rom_call(acpi_handle rom_handle, uint8_t *bios,
  326. int offset, int len)
  327. {
  328. acpi_status status;
  329. union acpi_object rom_arg_elements[2], *obj;
  330. struct acpi_object_list rom_arg;
  331. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
  332. rom_arg.count = 2;
  333. rom_arg.pointer = &rom_arg_elements[0];
  334. rom_arg_elements[0].type = ACPI_TYPE_INTEGER;
  335. rom_arg_elements[0].integer.value = offset;
  336. rom_arg_elements[1].type = ACPI_TYPE_INTEGER;
  337. rom_arg_elements[1].integer.value = len;
  338. status = acpi_evaluate_object(rom_handle, NULL, &rom_arg, &buffer);
  339. if (ACPI_FAILURE(status)) {
  340. printk(KERN_INFO "failed to evaluate ROM got %s\n", acpi_format_exception(status));
  341. return -ENODEV;
  342. }
  343. obj = (union acpi_object *)buffer.pointer;
  344. len = min(len, (int)obj->buffer.length);
  345. memcpy(bios+offset, obj->buffer.pointer, len);
  346. kfree(buffer.pointer);
  347. return len;
  348. }
  349. bool nouveau_acpi_rom_supported(struct device *dev)
  350. {
  351. acpi_status status;
  352. acpi_handle dhandle, rom_handle;
  353. dhandle = ACPI_HANDLE(dev);
  354. if (!dhandle)
  355. return false;
  356. status = acpi_get_handle(dhandle, "_ROM", &rom_handle);
  357. if (ACPI_FAILURE(status))
  358. return false;
  359. nouveau_dsm_priv.rom_handle = rom_handle;
  360. return true;
  361. }
  362. int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len)
  363. {
  364. return nouveau_rom_call(nouveau_dsm_priv.rom_handle, bios, offset, len);
  365. }
  366. void *
  367. nouveau_acpi_edid(struct drm_device *dev, struct drm_connector *connector)
  368. {
  369. struct acpi_device *acpidev;
  370. acpi_handle handle;
  371. int type, ret;
  372. void *edid;
  373. switch (connector->connector_type) {
  374. case DRM_MODE_CONNECTOR_LVDS:
  375. case DRM_MODE_CONNECTOR_eDP:
  376. type = ACPI_VIDEO_DISPLAY_LCD;
  377. break;
  378. default:
  379. return NULL;
  380. }
  381. handle = ACPI_HANDLE(&dev->pdev->dev);
  382. if (!handle)
  383. return NULL;
  384. ret = acpi_bus_get_device(handle, &acpidev);
  385. if (ret)
  386. return NULL;
  387. ret = acpi_video_get_edid(acpidev, type, -1, &edid);
  388. if (ret < 0)
  389. return NULL;
  390. return kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
  391. }