nouveau_acpi.c 11 KB

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