nouveau_acpi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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;
  213. int retval = 0;
  214. dhandle = ACPI_HANDLE(&pdev->dev);
  215. if (!dhandle)
  216. return false;
  217. if (!acpi_has_method(dhandle, "_DSM"))
  218. return false;
  219. if (nouveau_test_dsm(dhandle, nouveau_dsm, NOUVEAU_DSM_POWER))
  220. retval |= NOUVEAU_DSM_HAS_MUX;
  221. if (nouveau_test_dsm(dhandle, nouveau_optimus_dsm,
  222. NOUVEAU_DSM_OPTIMUS_CAPS))
  223. retval |= NOUVEAU_DSM_HAS_OPT;
  224. if (retval & NOUVEAU_DSM_HAS_OPT) {
  225. uint32_t result;
  226. nouveau_optimus_dsm(dhandle, NOUVEAU_DSM_OPTIMUS_CAPS, 0,
  227. &result);
  228. dev_info(&pdev->dev, "optimus capabilities: %s, status %s%s\n",
  229. (result & OPTIMUS_ENABLED) ? "enabled" : "disabled",
  230. (result & OPTIMUS_DYNAMIC_PWR_CAP) ? "dynamic power, " : "",
  231. (result & OPTIMUS_HDA_CODEC_MASK) ? "hda bios codec supported" : "");
  232. }
  233. if (retval)
  234. nouveau_dsm_priv.dhandle = dhandle;
  235. return retval;
  236. }
  237. static bool nouveau_dsm_detect(void)
  238. {
  239. char acpi_method_name[255] = { 0 };
  240. struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
  241. struct pci_dev *pdev = NULL;
  242. int has_dsm = 0;
  243. int has_optimus = 0;
  244. int vga_count = 0;
  245. bool guid_valid;
  246. int retval;
  247. bool ret = false;
  248. /* lookup the MXM GUID */
  249. guid_valid = mxm_wmi_supported();
  250. if (guid_valid)
  251. printk("MXM: GUID detected in BIOS\n");
  252. /* now do DSM detection */
  253. while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
  254. vga_count++;
  255. retval = nouveau_dsm_pci_probe(pdev);
  256. if (retval & NOUVEAU_DSM_HAS_MUX)
  257. has_dsm |= 1;
  258. if (retval & NOUVEAU_DSM_HAS_OPT)
  259. has_optimus = 1;
  260. }
  261. /* find the optimus DSM or the old v1 DSM */
  262. if (has_optimus == 1) {
  263. acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
  264. &buffer);
  265. printk(KERN_INFO "VGA switcheroo: detected Optimus DSM method %s handle\n",
  266. acpi_method_name);
  267. nouveau_dsm_priv.optimus_detected = true;
  268. ret = true;
  269. } else if (vga_count == 2 && has_dsm && guid_valid) {
  270. acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME,
  271. &buffer);
  272. printk(KERN_INFO "VGA switcheroo: detected DSM switching method %s handle\n",
  273. acpi_method_name);
  274. nouveau_dsm_priv.dsm_detected = true;
  275. ret = true;
  276. }
  277. return ret;
  278. }
  279. void nouveau_register_dsm_handler(void)
  280. {
  281. bool r;
  282. r = nouveau_dsm_detect();
  283. if (!r)
  284. return;
  285. vga_switcheroo_register_handler(&nouveau_dsm_handler);
  286. }
  287. /* Must be called for Optimus models before the card can be turned off */
  288. void nouveau_switcheroo_optimus_dsm(void)
  289. {
  290. u32 result = 0;
  291. if (!nouveau_dsm_priv.optimus_detected)
  292. return;
  293. nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_FLAGS,
  294. 0x3, &result);
  295. nouveau_optimus_dsm(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_OPTIMUS_CAPS,
  296. NOUVEAU_DSM_OPTIMUS_SET_POWERDOWN, &result);
  297. }
  298. void nouveau_unregister_dsm_handler(void)
  299. {
  300. if (nouveau_dsm_priv.optimus_detected || nouveau_dsm_priv.dsm_detected)
  301. vga_switcheroo_unregister_handler();
  302. }
  303. /* retrieve the ROM in 4k blocks */
  304. static int nouveau_rom_call(acpi_handle rom_handle, uint8_t *bios,
  305. int offset, int len)
  306. {
  307. acpi_status status;
  308. union acpi_object rom_arg_elements[2], *obj;
  309. struct acpi_object_list rom_arg;
  310. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
  311. rom_arg.count = 2;
  312. rom_arg.pointer = &rom_arg_elements[0];
  313. rom_arg_elements[0].type = ACPI_TYPE_INTEGER;
  314. rom_arg_elements[0].integer.value = offset;
  315. rom_arg_elements[1].type = ACPI_TYPE_INTEGER;
  316. rom_arg_elements[1].integer.value = len;
  317. status = acpi_evaluate_object(rom_handle, NULL, &rom_arg, &buffer);
  318. if (ACPI_FAILURE(status)) {
  319. printk(KERN_INFO "failed to evaluate ROM got %s\n", acpi_format_exception(status));
  320. return -ENODEV;
  321. }
  322. obj = (union acpi_object *)buffer.pointer;
  323. memcpy(bios+offset, obj->buffer.pointer, len);
  324. kfree(buffer.pointer);
  325. return len;
  326. }
  327. bool nouveau_acpi_rom_supported(struct pci_dev *pdev)
  328. {
  329. acpi_status status;
  330. acpi_handle dhandle, rom_handle;
  331. if (!nouveau_dsm_priv.dsm_detected && !nouveau_dsm_priv.optimus_detected)
  332. return false;
  333. dhandle = ACPI_HANDLE(&pdev->dev);
  334. if (!dhandle)
  335. return false;
  336. status = acpi_get_handle(dhandle, "_ROM", &rom_handle);
  337. if (ACPI_FAILURE(status))
  338. return false;
  339. nouveau_dsm_priv.rom_handle = rom_handle;
  340. return true;
  341. }
  342. int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len)
  343. {
  344. return nouveau_rom_call(nouveau_dsm_priv.rom_handle, bios, offset, len);
  345. }
  346. void *
  347. nouveau_acpi_edid(struct drm_device *dev, struct drm_connector *connector)
  348. {
  349. struct acpi_device *acpidev;
  350. acpi_handle handle;
  351. int type, ret;
  352. void *edid;
  353. switch (connector->connector_type) {
  354. case DRM_MODE_CONNECTOR_LVDS:
  355. case DRM_MODE_CONNECTOR_eDP:
  356. type = ACPI_VIDEO_DISPLAY_LCD;
  357. break;
  358. default:
  359. return NULL;
  360. }
  361. handle = ACPI_HANDLE(&dev->pdev->dev);
  362. if (!handle)
  363. return NULL;
  364. ret = acpi_bus_get_device(handle, &acpidev);
  365. if (ret)
  366. return NULL;
  367. ret = acpi_video_get_edid(acpidev, type, -1, &edid);
  368. if (ret < 0)
  369. return NULL;
  370. return kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
  371. }