nouveau_acpi.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 "drmP.h"
  8. #include "drm.h"
  9. #include "drm_sarea.h"
  10. #include "drm_crtc_helper.h"
  11. #include "nouveau_drv.h"
  12. #include "nouveau_drm.h"
  13. #include "nv50_display.h"
  14. #include "nouveau_connector.h"
  15. #include <linux/vga_switcheroo.h>
  16. #define NOUVEAU_DSM_SUPPORTED 0x00
  17. #define NOUVEAU_DSM_SUPPORTED_FUNCTIONS 0x00
  18. #define NOUVEAU_DSM_ACTIVE 0x01
  19. #define NOUVEAU_DSM_ACTIVE_QUERY 0x00
  20. #define NOUVEAU_DSM_LED 0x02
  21. #define NOUVEAU_DSM_LED_STATE 0x00
  22. #define NOUVEAU_DSM_LED_OFF 0x10
  23. #define NOUVEAU_DSM_LED_STAMINA 0x11
  24. #define NOUVEAU_DSM_LED_SPEED 0x12
  25. #define NOUVEAU_DSM_POWER 0x03
  26. #define NOUVEAU_DSM_POWER_STATE 0x00
  27. #define NOUVEAU_DSM_POWER_SPEED 0x01
  28. #define NOUVEAU_DSM_POWER_STAMINA 0x02
  29. static struct nouveau_dsm_priv {
  30. bool dsm_detected;
  31. acpi_handle dhandle;
  32. acpi_handle rom_handle;
  33. } nouveau_dsm_priv;
  34. static const char nouveau_dsm_muid[] = {
  35. 0xA0, 0xA0, 0x95, 0x9D, 0x60, 0x00, 0x48, 0x4D,
  36. 0xB3, 0x4D, 0x7E, 0x5F, 0xEA, 0x12, 0x9F, 0xD4,
  37. };
  38. static int nouveau_dsm(acpi_handle handle, int func, int arg, uint32_t *result)
  39. {
  40. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  41. struct acpi_object_list input;
  42. union acpi_object params[4];
  43. union acpi_object *obj;
  44. int err;
  45. input.count = 4;
  46. input.pointer = params;
  47. params[0].type = ACPI_TYPE_BUFFER;
  48. params[0].buffer.length = sizeof(nouveau_dsm_muid);
  49. params[0].buffer.pointer = (char *)nouveau_dsm_muid;
  50. params[1].type = ACPI_TYPE_INTEGER;
  51. params[1].integer.value = 0x00000102;
  52. params[2].type = ACPI_TYPE_INTEGER;
  53. params[2].integer.value = func;
  54. params[3].type = ACPI_TYPE_INTEGER;
  55. params[3].integer.value = arg;
  56. err = acpi_evaluate_object(handle, "_DSM", &input, &output);
  57. if (err) {
  58. printk(KERN_INFO "failed to evaluate _DSM: %d\n", err);
  59. return err;
  60. }
  61. obj = (union acpi_object *)output.pointer;
  62. if (obj->type == ACPI_TYPE_INTEGER)
  63. if (obj->integer.value == 0x80000002)
  64. return -ENODEV;
  65. if (obj->type == ACPI_TYPE_BUFFER) {
  66. if (obj->buffer.length == 4 && result) {
  67. *result = 0;
  68. *result |= obj->buffer.pointer[0];
  69. *result |= (obj->buffer.pointer[1] << 8);
  70. *result |= (obj->buffer.pointer[2] << 16);
  71. *result |= (obj->buffer.pointer[3] << 24);
  72. }
  73. }
  74. kfree(output.pointer);
  75. return 0;
  76. }
  77. static int nouveau_dsm_switch_mux(acpi_handle handle, int mux_id)
  78. {
  79. return nouveau_dsm(handle, NOUVEAU_DSM_LED, mux_id, NULL);
  80. }
  81. static int nouveau_dsm_set_discrete_state(acpi_handle handle, enum vga_switcheroo_state state)
  82. {
  83. int arg;
  84. if (state == VGA_SWITCHEROO_ON)
  85. arg = NOUVEAU_DSM_POWER_SPEED;
  86. else
  87. arg = NOUVEAU_DSM_POWER_STAMINA;
  88. nouveau_dsm(handle, NOUVEAU_DSM_POWER, arg, NULL);
  89. return 0;
  90. }
  91. static int nouveau_dsm_switchto(enum vga_switcheroo_client_id id)
  92. {
  93. if (id == VGA_SWITCHEROO_IGD)
  94. return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_STAMINA);
  95. else
  96. return nouveau_dsm_switch_mux(nouveau_dsm_priv.dhandle, NOUVEAU_DSM_LED_SPEED);
  97. }
  98. static int nouveau_dsm_power_state(enum vga_switcheroo_client_id id,
  99. enum vga_switcheroo_state state)
  100. {
  101. if (id == VGA_SWITCHEROO_IGD)
  102. return 0;
  103. return nouveau_dsm_set_discrete_state(nouveau_dsm_priv.dhandle, state);
  104. }
  105. static int nouveau_dsm_init(void)
  106. {
  107. return 0;
  108. }
  109. static int nouveau_dsm_get_client_id(struct pci_dev *pdev)
  110. {
  111. /* easy option one - intel vendor ID means Integrated */
  112. if (pdev->vendor == PCI_VENDOR_ID_INTEL)
  113. return VGA_SWITCHEROO_IGD;
  114. /* is this device on Bus 0? - this may need improving */
  115. if (pdev->bus->number == 0)
  116. return VGA_SWITCHEROO_IGD;
  117. return VGA_SWITCHEROO_DIS;
  118. }
  119. static struct vga_switcheroo_handler nouveau_dsm_handler = {
  120. .switchto = nouveau_dsm_switchto,
  121. .power_state = nouveau_dsm_power_state,
  122. .init = nouveau_dsm_init,
  123. .get_client_id = nouveau_dsm_get_client_id,
  124. };
  125. static bool nouveau_dsm_pci_probe(struct pci_dev *pdev)
  126. {
  127. acpi_handle dhandle, nvidia_handle;
  128. acpi_status status;
  129. int ret;
  130. uint32_t result;
  131. dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
  132. if (!dhandle)
  133. return false;
  134. status = acpi_get_handle(dhandle, "_DSM", &nvidia_handle);
  135. if (ACPI_FAILURE(status)) {
  136. return false;
  137. }
  138. ret = nouveau_dsm(dhandle, NOUVEAU_DSM_SUPPORTED,
  139. NOUVEAU_DSM_SUPPORTED_FUNCTIONS, &result);
  140. if (ret < 0)
  141. return false;
  142. nouveau_dsm_priv.dhandle = dhandle;
  143. return true;
  144. }
  145. static bool nouveau_dsm_detect(void)
  146. {
  147. char acpi_method_name[255] = { 0 };
  148. struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
  149. struct pci_dev *pdev = NULL;
  150. int has_dsm = 0;
  151. int vga_count = 0;
  152. while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
  153. vga_count++;
  154. has_dsm |= (nouveau_dsm_pci_probe(pdev) == true);
  155. }
  156. if (vga_count == 2 && has_dsm) {
  157. acpi_get_name(nouveau_dsm_priv.dhandle, ACPI_FULL_PATHNAME, &buffer);
  158. printk(KERN_INFO "VGA switcheroo: detected DSM switching method %s handle\n",
  159. acpi_method_name);
  160. nouveau_dsm_priv.dsm_detected = true;
  161. return true;
  162. }
  163. return false;
  164. }
  165. void nouveau_register_dsm_handler(void)
  166. {
  167. bool r;
  168. r = nouveau_dsm_detect();
  169. if (!r)
  170. return;
  171. vga_switcheroo_register_handler(&nouveau_dsm_handler);
  172. }
  173. void nouveau_unregister_dsm_handler(void)
  174. {
  175. vga_switcheroo_unregister_handler();
  176. }
  177. /* retrieve the ROM in 4k blocks */
  178. static int nouveau_rom_call(acpi_handle rom_handle, uint8_t *bios,
  179. int offset, int len)
  180. {
  181. acpi_status status;
  182. union acpi_object rom_arg_elements[2], *obj;
  183. struct acpi_object_list rom_arg;
  184. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
  185. rom_arg.count = 2;
  186. rom_arg.pointer = &rom_arg_elements[0];
  187. rom_arg_elements[0].type = ACPI_TYPE_INTEGER;
  188. rom_arg_elements[0].integer.value = offset;
  189. rom_arg_elements[1].type = ACPI_TYPE_INTEGER;
  190. rom_arg_elements[1].integer.value = len;
  191. status = acpi_evaluate_object(rom_handle, NULL, &rom_arg, &buffer);
  192. if (ACPI_FAILURE(status)) {
  193. printk(KERN_INFO "failed to evaluate ROM got %s\n", acpi_format_exception(status));
  194. return -ENODEV;
  195. }
  196. obj = (union acpi_object *)buffer.pointer;
  197. memcpy(bios+offset, obj->buffer.pointer, len);
  198. kfree(buffer.pointer);
  199. return len;
  200. }
  201. bool nouveau_acpi_rom_supported(struct pci_dev *pdev)
  202. {
  203. acpi_status status;
  204. acpi_handle dhandle, rom_handle;
  205. if (!nouveau_dsm_priv.dsm_detected)
  206. return false;
  207. dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
  208. if (!dhandle)
  209. return false;
  210. status = acpi_get_handle(dhandle, "_ROM", &rom_handle);
  211. if (ACPI_FAILURE(status))
  212. return false;
  213. nouveau_dsm_priv.rom_handle = rom_handle;
  214. return true;
  215. }
  216. int nouveau_acpi_get_bios_chunk(uint8_t *bios, int offset, int len)
  217. {
  218. return nouveau_rom_call(nouveau_dsm_priv.rom_handle, bios, offset, len);
  219. }
  220. int
  221. nouveau_acpi_edid(struct drm_device *dev, struct drm_connector *connector)
  222. {
  223. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  224. struct acpi_device *acpidev;
  225. acpi_handle handle;
  226. int type, ret;
  227. void *edid;
  228. switch (connector->connector_type) {
  229. case DRM_MODE_CONNECTOR_LVDS:
  230. case DRM_MODE_CONNECTOR_eDP:
  231. type = ACPI_VIDEO_DISPLAY_LCD;
  232. break;
  233. default:
  234. return -EINVAL;
  235. }
  236. handle = DEVICE_ACPI_HANDLE(&dev->pdev->dev);
  237. if (!handle)
  238. return -ENODEV;
  239. ret = acpi_bus_get_device(handle, &acpidev);
  240. if (ret)
  241. return -ENODEV;
  242. ret = acpi_video_get_edid(acpidev, type, -1, &edid);
  243. if (ret < 0)
  244. return ret;
  245. nv_connector->edid = kmemdup(edid, EDID_LENGTH, GFP_KERNEL);
  246. return 0;
  247. }