nouveau_acpi.c 11 KB

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