nouveau_acpi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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_init(void)
  182. {
  183. return 0;
  184. }
  185. static int nouveau_dsm_get_client_id(struct pci_dev *pdev)
  186. {
  187. /* easy option one - intel vendor ID means Integrated */
  188. if (pdev->vendor == PCI_VENDOR_ID_INTEL)
  189. return VGA_SWITCHEROO_IGD;
  190. /* is this device on Bus 0? - this may need improving */
  191. if (pdev->bus->number == 0)
  192. return VGA_SWITCHEROO_IGD;
  193. return VGA_SWITCHEROO_DIS;
  194. }
  195. static struct vga_switcheroo_handler nouveau_dsm_handler = {
  196. .switchto = nouveau_dsm_switchto,
  197. .power_state = nouveau_dsm_power_state,
  198. .init = nouveau_dsm_init,
  199. .get_client_id = nouveau_dsm_get_client_id,
  200. };
  201. static int nouveau_dsm_pci_probe(struct pci_dev *pdev)
  202. {
  203. acpi_handle dhandle, nvidia_handle;
  204. acpi_status status;
  205. int retval = 0;
  206. dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
  207. if (!dhandle)
  208. return false;
  209. status = acpi_get_handle(dhandle, "_DSM", &nvidia_handle);
  210. if (ACPI_FAILURE(status)) {
  211. return false;
  212. }
  213. if (nouveau_test_dsm(dhandle, nouveau_dsm, NOUVEAU_DSM_POWER))
  214. retval |= NOUVEAU_DSM_HAS_MUX;
  215. if (nouveau_test_dsm(dhandle, nouveau_optimus_dsm,
  216. NOUVEAU_DSM_OPTIMUS_FN))
  217. retval |= NOUVEAU_DSM_HAS_OPT;
  218. if (retval)
  219. nouveau_dsm_priv.dhandle = dhandle;
  220. return retval;
  221. }
  222. static bool nouveau_dsm_detect(void)
  223. {
  224. char acpi_method_name[255] = { 0 };
  225. struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
  226. struct pci_dev *pdev = NULL;
  227. int has_dsm = 0;
  228. int has_optimus;
  229. int vga_count = 0;
  230. bool guid_valid;
  231. int retval;
  232. bool ret = false;
  233. /* lookup the MXM GUID */
  234. guid_valid = mxm_wmi_supported();
  235. if (guid_valid)
  236. printk("MXM: GUID detected in BIOS\n");
  237. /* now do DSM detection */
  238. while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
  239. vga_count++;
  240. retval = nouveau_dsm_pci_probe(pdev);
  241. if (retval & NOUVEAU_DSM_HAS_MUX)
  242. has_dsm |= 1;
  243. if (retval & NOUVEAU_DSM_HAS_OPT)
  244. has_optimus = 1;
  245. }
  246. if (vga_count == 2 && has_dsm && guid_valid) {
  247. acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
  248. &buffer);
  249. printk(KERN_INFO "VGA switcheroo: detected DSM switching method %s handle\n",
  250. acpi_method_name);
  251. nouveau_dsm_priv.dsm_detected = true;
  252. ret = true;
  253. }
  254. if (has_optimus == 1) {
  255. acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
  256. &buffer);
  257. printk(KERN_INFO "VGA switcheroo: detected Optimus DSM method %s handle\n",
  258. acpi_method_name);
  259. nouveau_dsm_priv.optimus_detected = true;
  260. ret = true;
  261. }
  262. return ret;
  263. }
  264. void nouveau_register_dsm_handler(void)
  265. {
  266. bool r;
  267. r = nouveau_dsm_detect();
  268. if (!r)
  269. return;
  270. vga_switcheroo_register_handler(&nouveau_dsm_handler);
  271. }
  272. /* Must be called for Optimus models before the card can be turned off */
  273. void nouveau_switcheroo_optimus_dsm(void)
  274. {
  275. u32 result = 0;
  276. if (!nouveau_dsm_priv.optimus_detected)
  277. return;
  278. nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_FN,
  279. NOUVEAU_DSM_OPTIMUS_ARGS, &result);
  280. }
  281. void nouveau_unregister_dsm_handler(void)
  282. {
  283. vga_switcheroo_unregister_handler();
  284. }
  285. /* retrieve the ROM in 4k blocks */
  286. static int nouveau_rom_call(acpi_handle rom_handle, uint8_t *bios,
  287. int offset, int len)
  288. {
  289. acpi_status status;
  290. union acpi_object rom_arg_elements[2], *obj;
  291. struct acpi_object_list rom_arg;
  292. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
  293. rom_arg.count = 2;
  294. rom_arg.pointer = &rom_arg_elements[0];
  295. rom_arg_elements[0].type = ACPI_TYPE_INTEGER;
  296. rom_arg_elements[0].integer.value = offset;
  297. rom_arg_elements[1].type = ACPI_TYPE_INTEGER;
  298. rom_arg_elements[1].integer.value = len;
  299. status = acpi_evaluate_object(rom_handle, NULL, &rom_arg, &buffer);
  300. if (ACPI_FAILURE(status)) {
  301. printk(KERN_INFO "failed to evaluate ROM got %s\n", acpi_format_exception(status));
  302. return -ENODEV;
  303. }
  304. obj = (union acpi_object *)buffer.pointer;
  305. memcpy(bios+offset, obj->buffer.pointer, len);
  306. kfree(buffer.pointer);
  307. return len;
  308. }
  309. bool nouveau_acpi_rom_supported(struct pci_dev *pdev)
  310. {
  311. acpi_status status;
  312. acpi_handle dhandle, rom_handle;
  313. if (!nouveau_dsm_priv.dsm_detected && !nouveau_dsm_priv.optimus_detected)
  314. return false;
  315. dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
  316. if (!dhandle)
  317. return false;
  318. status = acpi_get_handle(dhandle, "_ROM", &rom_handle);
  319. if (ACPI_FAILURE(status))
  320. return false;
  321. nouveau_dsm_priv.rom_handle = rom_handle;
  322. return true;
  323. }
  324. int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len)
  325. {
  326. return nouveau_rom_call(nouveau_dsm_priv.rom_handle, bios, offset, len);
  327. }
  328. int
  329. nouveau_acpi_edid(struct drm_device *dev, struct drm_connector *connector)
  330. {
  331. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  332. struct acpi_device *acpidev;
  333. acpi_handle handle;
  334. int type, ret;
  335. void *edid;
  336. switch (connector->connector_type) {
  337. case DRM_MODE_CONNECTOR_LVDS:
  338. case DRM_MODE_CONNECTOR_eDP:
  339. type = ACPI_VIDEO_DISPLAY_LCD;
  340. break;
  341. default:
  342. return -EINVAL;
  343. }
  344. handle = DEVICE_ACPI_HANDLE(&dev->pdev->dev);
  345. if (!handle)
  346. return -ENODEV;
  347. ret = acpi_bus_get_device(handle, &acpidev);
  348. if (ret)
  349. return -ENODEV;
  350. ret = acpi_video_get_edid(acpidev, type, -1, &edid);
  351. if (ret < 0)
  352. return ret;
  353. nv_connector->edid = kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
  354. return 0;
  355. }