nouveau_acpi.c 5.0 KB

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