nouveau_acpi.c 12 KB

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