nouveau_acpi.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. acpi_handle rom_handle;
  32. } nouveau_dsm_priv;
  33. static const char nouveau_dsm_muid[] = {
  34. 0xA0, 0xA0, 0x95, 0x9D, 0x60, 0x00, 0x48, 0x4D,
  35. 0xB3, 0x4D, 0x7E, 0x5F, 0xEA, 0x12, 0x9F, 0xD4,
  36. };
  37. static int nouveau_dsm(acpi_handle handle, int func, int arg, int *result)
  38. {
  39. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  40. struct acpi_object_list input;
  41. union acpi_object params[4];
  42. union acpi_object *obj;
  43. int err;
  44. input.count = 4;
  45. input.pointer = params;
  46. params[0].type = ACPI_TYPE_BUFFER;
  47. params[0].buffer.length = sizeof(nouveau_dsm_muid);
  48. params[0].buffer.pointer = (char *)nouveau_dsm_muid;
  49. params[1].type = ACPI_TYPE_INTEGER;
  50. params[1].integer.value = 0x00000102;
  51. params[2].type = ACPI_TYPE_INTEGER;
  52. params[2].integer.value = func;
  53. params[3].type = ACPI_TYPE_INTEGER;
  54. params[3].integer.value = arg;
  55. err = acpi_evaluate_object(handle, "_DSM", &input, &output);
  56. if (err) {
  57. printk(KERN_INFO "failed to evaluate _DSM: %d\n", err);
  58. return err;
  59. }
  60. obj = (union acpi_object *)output.pointer;
  61. if (obj->type == ACPI_TYPE_INTEGER)
  62. if (obj->integer.value == 0x80000002)
  63. return -ENODEV;
  64. if (obj->type == ACPI_TYPE_BUFFER) {
  65. if (obj->buffer.length == 4 && result) {
  66. *result = 0;
  67. *result |= obj->buffer.pointer[0];
  68. *result |= (obj->buffer.pointer[1] << 8);
  69. *result |= (obj->buffer.pointer[2] << 16);
  70. *result |= (obj->buffer.pointer[3] << 24);
  71. }
  72. }
  73. kfree(output.pointer);
  74. return 0;
  75. }
  76. static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id)
  77. {
  78. return nouveau_dsm(handle, NOUVEAU_DSM_LED, mux_id, NULL);
  79. }
  80. static int nouveau_dsm_set_discrete_state(acpi_handle handle, enum vga_switcheroo_state state)
  81. {
  82. int arg;
  83. if (state == VGA_SWITCHEROO_ON)
  84. arg = NOUVEAU_DSM_POWER_SPEED;
  85. else
  86. arg = NOUVEAU_DSM_POWER_STAMINA;
  87. nouveau_dsm(handle, NOUVEAU_DSM_POWER, arg, NULL);
  88. return 0;
  89. }
  90. static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id)
  91. {
  92. if (id == VGA_SWITCHEROO_IGD)
  93. return nouveau_dsm_switch_mux(nouveau_dsm_priv.dsm_handle, NOUVEAU_DSM_LED_STAMINA);
  94. else
  95. return nouveau_dsm_switch_mux(nouveau_dsm_priv.dsm_handle, NOUVEAU_DSM_LED_SPEED);
  96. }
  97. static int nouveau_dsm_power_state(enum vga_switcheroo_client_id id,
  98. enum vga_switcheroo_state state)
  99. {
  100. if (id == VGA_SWITCHEROO_IGD)
  101. return 0;
  102. return nouveau_dsm_set_discrete_state(nouveau_dsm_priv.dsm_handle, state);
  103. }
  104. static int nouveau_dsm_init(void)
  105. {
  106. return 0;
  107. }
  108. static int nouveau_dsm_get_client_id(struct pci_dev *pdev)
  109. {
  110. if (nouveau_dsm_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev))
  111. return VGA_SWITCHEROO_IGD;
  112. else
  113. return VGA_SWITCHEROO_DIS;
  114. }
  115. static struct vga_switcheroo_handler nouveau_dsm_handler = {
  116. .switchto = nouveau_dsm_switchto,
  117. .power_state = nouveau_dsm_power_state,
  118. .init = nouveau_dsm_init,
  119. .get_client_id = nouveau_dsm_get_client_id,
  120. };
  121. static bool nouveau_dsm_pci_probe(struct pci_dev *pdev)
  122. {
  123. acpi_handle dhandle, nvidia_handle;
  124. acpi_status status;
  125. int ret;
  126. uint32_t result;
  127. dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
  128. if (!dhandle)
  129. return false;
  130. status = acpi_get_handle(dhandle, "_DSM", &nvidia_handle);
  131. if (ACPI_FAILURE(status)) {
  132. return false;
  133. }
  134. ret = nouveau_dsm(nvidia_handle, NOUVEAU_DSM_SUPPORTED,
  135. NOUVEAU_DSM_SUPPORTED_FUNCTIONS, &result);
  136. if (ret < 0)
  137. return false;
  138. nouveau_dsm_priv.dhandle = dhandle;
  139. nouveau_dsm_priv.dsm_handle = nvidia_handle;
  140. return true;
  141. }
  142. static bool nouveau_dsm_detect(void)
  143. {
  144. char acpi_method_name[255] = { 0 };
  145. struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
  146. struct pci_dev *pdev = NULL;
  147. int has_dsm = 0;
  148. int vga_count = 0;
  149. while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
  150. vga_count++;
  151. has_dsm |= (nouveau_dsm_pci_probe(pdev) == true);
  152. }
  153. if (vga_count == 2 && has_dsm) {
  154. acpi_get_name(nouveau_dsm_priv.dsm_handle, ACPI_FULL_PATHNAME, &buffer);
  155. printk(KERN_INFO "VGA switcheroo: detected DSM switching method %s handle\n",
  156. acpi_method_name);
  157. nouveau_dsm_priv.dsm_detected = true;
  158. return true;
  159. }
  160. return false;
  161. }
  162. void nouveau_register_dsm_handler(void)
  163. {
  164. bool r;
  165. r = nouveau_dsm_detect();
  166. if (!r)
  167. return;
  168. vga_switcheroo_register_handler(&nouveau_dsm_handler);
  169. }
  170. void nouveau_unregister_dsm_handler(void)
  171. {
  172. vga_switcheroo_unregister_handler();
  173. }
  174. /* retrieve the ROM in 4k blocks */
  175. static int nouveau_rom_call(acpi_handle rom_handle, uint8_t *bios,
  176. int offset, int len)
  177. {
  178. acpi_status status;
  179. union acpi_object rom_arg_elements[2], *obj;
  180. struct acpi_object_list rom_arg;
  181. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
  182. rom_arg.count = 2;
  183. rom_arg.pointer = &rom_arg_elements[0];
  184. rom_arg_elements[0].type = ACPI_TYPE_INTEGER;
  185. rom_arg_elements[0].integer.value = offset;
  186. rom_arg_elements[1].type = ACPI_TYPE_INTEGER;
  187. rom_arg_elements[1].integer.value = len;
  188. status = acpi_evaluate_object(rom_handle, NULL, &rom_arg, &buffer);
  189. if (ACPI_FAILURE(status)) {
  190. printk(KERN_INFO "failed to evaluate ROM got %s\n", acpi_format_exception(status));
  191. return -ENODEV;
  192. }
  193. obj = (union acpi_object *)buffer.pointer;
  194. memcpy(bios+offset, obj->buffer.pointer, len);
  195. kfree(buffer.pointer);
  196. return len;
  197. }
  198. bool nouveau_acpi_rom_supported(struct pci_dev *pdev)
  199. {
  200. acpi_status status;
  201. acpi_handle dhandle, rom_handle;
  202. if (!nouveau_dsm_priv.dsm_detected)
  203. return false;
  204. dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
  205. if (!dhandle)
  206. return false;
  207. status = acpi_get_handle(dhandle, "_ROM", &rom_handle);
  208. if (ACPI_FAILURE(status))
  209. return false;
  210. nouveau_dsm_priv.rom_handle = rom_handle;
  211. return true;
  212. }
  213. int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len)
  214. {
  215. return nouveau_rom_call(nouveau_dsm_priv.rom_handle, bios, offset, len);
  216. }