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