nouveau_acpi.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include <linux/pci.h>
  2. #include <linux/acpi.h>
  3. #include <acpi/acpi_drivers.h>
  4. #include <acpi/acpi_bus.h>
  5. #include "drmP.h"
  6. #include "drm.h"
  7. #include "drm_sarea.h"
  8. #include "drm_crtc_helper.h"
  9. #include "nouveau_drv.h"
  10. #include "nouveau_drm.h"
  11. #include "nv50_display.h"
  12. #include <linux/vga_switcheroo.h>
  13. #define NOUVEAU_DSM_SUPPORTED 0x00
  14. #define NOUVEAU_DSM_SUPPORTED_FUNCTIONS 0x00
  15. #define NOUVEAU_DSM_ACTIVE 0x01
  16. #define NOUVEAU_DSM_ACTIVE_QUERY 0x00
  17. #define NOUVEAU_DSM_LED 0x02
  18. #define NOUVEAU_DSM_LED_STATE 0x00
  19. #define NOUVEAU_DSM_LED_OFF 0x10
  20. #define NOUVEAU_DSM_LED_STAMINA 0x11
  21. #define NOUVEAU_DSM_LED_SPEED 0x12
  22. #define NOUVEAU_DSM_POWER 0x03
  23. #define NOUVEAU_DSM_POWER_STATE 0x00
  24. #define NOUVEAU_DSM_POWER_SPEED 0x01
  25. #define NOUVEAU_DSM_POWER_STAMINA 0x02
  26. static struct nouveau_dsm_priv {
  27. bool dsm_detected;
  28. acpi_handle dhandle;
  29. acpi_handle dsm_handle;
  30. } nouveau_dsm_priv;
  31. static const char nouveau_dsm_muid[] = {
  32. 0xA0, 0xA0, 0x95, 0x9D, 0x60, 0x00, 0x48, 0x4D,
  33. 0xB3, 0x4D, 0x7E, 0x5F, 0xEA, 0x12, 0x9F, 0xD4,
  34. };
  35. static int nouveau_dsm(acpi_handle handle, int func, int arg, int *result)
  36. {
  37. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  38. struct acpi_object_list input;
  39. union acpi_object params[4];
  40. union acpi_object *obj;
  41. int err;
  42. input.count = 4;
  43. input.pointer = params;
  44. params[0].type = ACPI_TYPE_BUFFER;
  45. params[0].buffer.length = sizeof(nouveau_dsm_muid);
  46. params[0].buffer.pointer = (char *)nouveau_dsm_muid;
  47. params[1].type = ACPI_TYPE_INTEGER;
  48. params[1].integer.value = 0x00000102;
  49. params[2].type = ACPI_TYPE_INTEGER;
  50. params[2].integer.value = func;
  51. params[3].type = ACPI_TYPE_INTEGER;
  52. params[3].integer.value = arg;
  53. err = acpi_evaluate_object(handle, "_DSM", &input, &output);
  54. if (err) {
  55. printk(KERN_INFO "failed to evaluate _DSM: %d\n", err);
  56. return err;
  57. }
  58. obj = (union acpi_object *)output.pointer;
  59. if (obj->type == ACPI_TYPE_INTEGER)
  60. if (obj->integer.value == 0x80000002)
  61. return -ENODEV;
  62. if (obj->type == ACPI_TYPE_BUFFER) {
  63. if (obj->buffer.length == 4 && result) {
  64. *result = 0;
  65. *result |= obj->buffer.pointer[0];
  66. *result |= (obj->buffer.pointer[1] << 8);
  67. *result |= (obj->buffer.pointer[2] << 16);
  68. *result |= (obj->buffer.pointer[3] << 24);
  69. }
  70. }
  71. kfree(output.pointer);
  72. return 0;
  73. }
  74. static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id)
  75. {
  76. return nouveau_dsm(handle, NOUVEAU_DSM_LED, mux_id, NULL);
  77. }
  78. static int nouveau_dsm_set_discrete_state(acpi_handle handle, enum vga_switcheroo_state state)
  79. {
  80. int arg;
  81. if (state == VGA_SWITCHEROO_ON)
  82. arg = NOUVEAU_DSM_POWER_SPEED;
  83. else
  84. arg = NOUVEAU_DSM_POWER_STAMINA;
  85. nouveau_dsm(handle, NOUVEAU_DSM_POWER, arg, NULL);
  86. return 0;
  87. }
  88. static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id)
  89. {
  90. if (id == VGA_SWITCHEROO_IGD)
  91. return nouveau_dsm_switch_mux(nouveau_dsm_priv.dsm_handle, NOUVEAU_DSM_LED_STAMINA);
  92. else
  93. return nouveau_dsm_switch_mux(nouveau_dsm_priv.dsm_handle, NOUVEAU_DSM_LED_SPEED);
  94. }
  95. static int nouveau_dsm_power_state(enum vga_switcheroo_client_id id,
  96. enum vga_switcheroo_state state)
  97. {
  98. if (id == VGA_SWITCHEROO_IGD)
  99. return 0;
  100. return nouveau_dsm_set_discrete_state(nouveau_dsm_priv.dsm_handle, state);
  101. }
  102. static int nouveau_dsm_init(void)
  103. {
  104. return 0;
  105. }
  106. static int nouveau_dsm_get_client_id(struct pci_dev *pdev)
  107. {
  108. if (nouveau_dsm_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev))
  109. return VGA_SWITCHEROO_IGD;
  110. else
  111. return VGA_SWITCHEROO_DIS;
  112. }
  113. static struct vga_switcheroo_handler nouveau_dsm_handler = {
  114. .switchto = nouveau_dsm_switchto,
  115. .power_state = nouveau_dsm_power_state,
  116. .init = nouveau_dsm_init,
  117. .get_client_id = nouveau_dsm_get_client_id,
  118. };
  119. static bool nouveau_dsm_pci_probe(struct pci_dev *pdev)
  120. {
  121. acpi_handle dhandle, nvidia_handle;
  122. acpi_status status;
  123. int ret;
  124. uint32_t result;
  125. dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
  126. if (!dhandle)
  127. return false;
  128. status = acpi_get_handle(dhandle, "_DSM", &nvidia_handle);
  129. if (ACPI_FAILURE(status)) {
  130. return false;
  131. }
  132. ret= nouveau_dsm(nvidia_handle, NOUVEAU_DSM_SUPPORTED,
  133. NOUVEAU_DSM_SUPPORTED_FUNCTIONS, &result);
  134. if (ret < 0)
  135. return false;
  136. nouveau_dsm_priv.dhandle = dhandle;
  137. nouveau_dsm_priv.dsm_handle = nvidia_handle;
  138. return true;
  139. }
  140. static bool nouveau_dsm_detect(void)
  141. {
  142. char acpi_method_name[255] = { 0 };
  143. struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
  144. struct pci_dev *pdev = NULL;
  145. int has_dsm = 0;
  146. int vga_count = 0;
  147. while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
  148. vga_count++;
  149. has_dsm |= (nouveau_dsm_pci_probe(pdev) == true);
  150. }
  151. if (vga_count == 2 && has_dsm) {
  152. acpi_get_name(nouveau_dsm_priv.dsm_handle, ACPI_FULL_PATHNAME, &buffer);
  153. printk(KERN_INFO "VGA switcheroo: detected DSM switching method %s handle\n",
  154. acpi_method_name);
  155. nouveau_dsm_priv.dsm_detected = true;
  156. return true;
  157. }
  158. return false;
  159. }
  160. void nouveau_register_dsm_handler(void)
  161. {
  162. bool r;
  163. r = nouveau_dsm_detect();
  164. if (!r)
  165. return;
  166. vga_switcheroo_register_handler(&nouveau_dsm_handler);
  167. }
  168. void nouveau_unregister_dsm_handler(void)
  169. {
  170. vga_switcheroo_unregister_handler();
  171. }