nouveau_acpi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 "drmP.h"
  10. #include "drm.h"
  11. #include "drm_sarea.h"
  12. #include "drm_crtc_helper.h"
  13. #include "nouveau_drv.h"
  14. #include <nouveau_drm.h>
  15. #include "nouveau_connector.h"
  16. #include <linux/vga_switcheroo.h>
  17. #define NOUVEAU_DSM_LED 0x02
  18. #define NOUVEAU_DSM_LED_STATE 0x00
  19. #define NOUVEAU_DSM_LED_OFF 0x10
  20. #define NOUVEAU_DSM_LED_STAMINA 0x11
  21. #define NOUVEAU_DSM_LED_SPEED 0x12
  22. #define NOUVEAU_DSM_POWER 0x03
  23. #define NOUVEAU_DSM_POWER_STATE 0x00
  24. #define NOUVEAU_DSM_POWER_SPEED 0x01
  25. #define NOUVEAU_DSM_POWER_STAMINA 0x02
  26. #define NOUVEAU_DSM_OPTIMUS_FN 0x1A
  27. #define NOUVEAU_DSM_OPTIMUS_ARGS 0x03000001
  28. static struct nouveau_dsm_priv {
  29. bool dsm_detected;
  30. bool optimus_detected;
  31. acpi_handle dhandle;
  32. acpi_handle rom_handle;
  33. } nouveau_dsm_priv;
  34. #define NOUVEAU_DSM_HAS_MUX 0x1
  35. #define NOUVEAU_DSM_HAS_OPT 0x2
  36. static const char nouveau_dsm_muid[] = {
  37. 0xA0, 0xA0, 0x95, 0x9D, 0x60, 0x00, 0x48, 0x4D,
  38. 0xB3, 0x4D, 0x7E, 0x5F, 0xEA, 0x12, 0x9F, 0xD4,
  39. };
  40. static const char nouveau_op_dsm_muid[] = {
  41. 0xF8, 0xD8, 0x86, 0xA4, 0xDA, 0x0B, 0x1B, 0x47,
  42. 0xA7, 0x2B, 0x60, 0x42, 0xA6, 0xB5, 0xBE, 0xE0,
  43. };
  44. static int nouveau_optimus_dsm(acpi_handle handle, int func, int arg, uint32_t *result)
  45. {
  46. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  47. struct acpi_object_list input;
  48. union acpi_object params[4];
  49. union acpi_object *obj;
  50. int i, err;
  51. char args_buff[4];
  52. input.count = 4;
  53. input.pointer = params;
  54. params[0].type = ACPI_TYPE_BUFFER;
  55. params[0].buffer.length = sizeof(nouveau_op_dsm_muid);
  56. params[0].buffer.pointer = (char *)nouveau_op_dsm_muid;
  57. params[1].type = ACPI_TYPE_INTEGER;
  58. params[1].integer.value = 0x00000100;
  59. params[2].type = ACPI_TYPE_INTEGER;
  60. params[2].integer.value = func;
  61. params[3].type = ACPI_TYPE_BUFFER;
  62. params[3].buffer.length = 4;
  63. /* ACPI is little endian, AABBCCDD becomes {DD,CC,BB,AA} */
  64. for (i = 0; i < 4; i++)
  65. args_buff[i] = (arg >> i * 8) & 0xFF;
  66. params[3].buffer.pointer = args_buff;
  67. err = acpi_evaluate_object(handle, "_DSM", &input, &output);
  68. if (err) {
  69. printk(KERN_INFO "failed to evaluate _DSM: %d\n", err);
  70. return err;
  71. }
  72. obj = (union acpi_object *)output.pointer;
  73. if (obj->type == ACPI_TYPE_INTEGER)
  74. if (obj->integer.value == 0x80000002) {
  75. return -ENODEV;
  76. }
  77. if (obj->type == ACPI_TYPE_BUFFER) {
  78. if (obj->buffer.length == 4 && result) {
  79. *result = 0;
  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. }
  86. kfree(output.pointer);
  87. return 0;
  88. }
  89. static int nouveau_dsm(acpi_handle handle, int func, int arg, uint32_t *result)
  90. {
  91. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  92. struct acpi_object_list input;
  93. union acpi_object params[4];
  94. union acpi_object *obj;
  95. int err;
  96. input.count = 4;
  97. input.pointer = params;
  98. params[0].type = ACPI_TYPE_BUFFER;
  99. params[0].buffer.length = sizeof(nouveau_dsm_muid);
  100. params[0].buffer.pointer = (char *)nouveau_dsm_muid;
  101. params[1].type = ACPI_TYPE_INTEGER;
  102. params[1].integer.value = 0x00000102;
  103. params[2].type = ACPI_TYPE_INTEGER;
  104. params[2].integer.value = func;
  105. params[3].type = ACPI_TYPE_INTEGER;
  106. params[3].integer.value = arg;
  107. err = acpi_evaluate_object(handle, "_DSM", &input, &output);
  108. if (err) {
  109. printk(KERN_INFO "failed to evaluate _DSM: %d\n", err);
  110. return err;
  111. }
  112. obj = (union acpi_object *)output.pointer;
  113. if (obj->type == ACPI_TYPE_INTEGER)
  114. if (obj->integer.value == 0x80000002)
  115. return -ENODEV;
  116. if (obj->type == ACPI_TYPE_BUFFER) {
  117. if (obj->buffer.length == 4 && result) {
  118. *result = 0;
  119. *result |= obj->buffer.pointer[0];
  120. *result |= (obj->buffer.pointer[1] << 8);
  121. *result |= (obj->buffer.pointer[2] << 16);
  122. *result |= (obj->buffer.pointer[3] << 24);
  123. }
  124. }
  125. kfree(output.pointer);
  126. return 0;
  127. }
  128. /* Returns 1 if a DSM function is usable and 0 otherwise */
  129. static int nouveau_test_dsm(acpi_handle test_handle,
  130. int (*dsm_func)(acpi_handle, int, int, uint32_t *),
  131. int sfnc)
  132. {
  133. u32 result = 0;
  134. /* Function 0 returns a Buffer containing available functions. The args
  135. * parameter is ignored for function 0, so just put 0 in it */
  136. if (dsm_func(test_handle, 0, 0, &result))
  137. return 0;
  138. /* ACPI Spec v4 9.14.1: if bit 0 is zero, no function is supported. If
  139. * the n-th bit is enabled, function n is supported */
  140. return result & 1 && result & (1 << sfnc);
  141. }
  142. static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id)
  143. {
  144. mxm_wmi_call_mxmx(mux_id == NOUVEAU_DSM_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0);
  145. mxm_wmi_call_mxds(mux_id == NOUVEAU_DSM_LED_STAMINA ? MXM_MXDS_ADAPTER_IGD : MXM_MXDS_ADAPTER_0);
  146. return nouveau_dsm(handle, NOUVEAU_DSM_LED, mux_id, NULL);
  147. }
  148. static int nouveau_dsm_set_discrete_state(acpi_handle handle, enum vga_switcheroo_state state)
  149. {
  150. int arg;
  151. if (state == VGA_SWITCHEROO_ON)
  152. arg = NOUVEAU_DSM_POWER_SPEED;
  153. else
  154. arg = NOUVEAU_DSM_POWER_STAMINA;
  155. nouveau_dsm(handle, NOUVEAU_DSM_POWER, arg, NULL);
  156. return 0;
  157. }
  158. static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id)
  159. {
  160. /* perhaps the _DSM functions are mutually exclusive, but prepare for
  161. * the future */
  162. if (!nouveau_dsm_priv.dsm_detected && nouveau_dsm_priv.optimus_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 && nouveau_dsm_priv.optimus_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. if (vga_count == 2 && has_dsm && guid_valid) {
  241. acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
  242. &buffer);
  243. printk(KERN_INFO "VGA switcheroo: detected DSM switching method %s handle\n",
  244. acpi_method_name);
  245. nouveau_dsm_priv.dsm_detected = true;
  246. ret = true;
  247. }
  248. if (has_optimus == 1) {
  249. acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
  250. &buffer);
  251. printk(KERN_INFO "VGA switcheroo: detected Optimus DSM method %s handle\n",
  252. acpi_method_name);
  253. nouveau_dsm_priv.optimus_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. int
  324. nouveau_acpi_edid(struct drm_device *dev, struct drm_connector *connector)
  325. {
  326. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  327. struct acpi_device *acpidev;
  328. acpi_handle handle;
  329. int type, ret;
  330. void *edid;
  331. switch (connector->connector_type) {
  332. case DRM_MODE_CONNECTOR_LVDS:
  333. case DRM_MODE_CONNECTOR_eDP:
  334. type = ACPI_VIDEO_DISPLAY_LCD;
  335. break;
  336. default:
  337. return -EINVAL;
  338. }
  339. handle = DEVICE_ACPI_HANDLE(&dev->pdev->dev);
  340. if (!handle)
  341. return -ENODEV;
  342. ret = acpi_bus_get_device(handle, &acpidev);
  343. if (ret)
  344. return -ENODEV;
  345. ret = acpi_video_get_edid(acpidev, type, -1, &edid);
  346. if (ret < 0)
  347. return ret;
  348. nv_connector->edid = kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
  349. return 0;
  350. }