nouveau_acpi.c 9.6 KB

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