radeon_atpx_handler.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (c) 2010 Red Hat Inc.
  3. * Author : Dave Airlie <airlied@redhat.com>
  4. *
  5. * Licensed under GPLv2
  6. *
  7. * ATPX support for both Intel/ATI
  8. */
  9. #include <linux/vga_switcheroo.h>
  10. #include <linux/slab.h>
  11. #include <acpi/acpi.h>
  12. #include <acpi/acpi_bus.h>
  13. #include <linux/pci.h>
  14. #define ATPX_VERSION 0
  15. #define ATPX_GPU_PWR 2
  16. #define ATPX_MUX_SELECT 3
  17. #define ATPX_I2C_MUX_SELECT 4
  18. #define ATPX_SWITCH_START 5
  19. #define ATPX_SWITCH_END 6
  20. #define ATPX_INTEGRATED 0
  21. #define ATPX_DISCRETE 1
  22. #define ATPX_MUX_IGD 0
  23. #define ATPX_MUX_DISCRETE 1
  24. static struct radeon_atpx_priv {
  25. bool atpx_detected;
  26. /* handle for device - and atpx */
  27. acpi_handle dhandle;
  28. acpi_handle atpx_handle;
  29. } radeon_atpx_priv;
  30. static int radeon_atpx_get_version(acpi_handle handle)
  31. {
  32. acpi_status status;
  33. union acpi_object atpx_arg_elements[2], *obj;
  34. struct acpi_object_list atpx_arg;
  35. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  36. atpx_arg.count = 2;
  37. atpx_arg.pointer = &atpx_arg_elements[0];
  38. atpx_arg_elements[0].type = ACPI_TYPE_INTEGER;
  39. atpx_arg_elements[0].integer.value = ATPX_VERSION;
  40. atpx_arg_elements[1].type = ACPI_TYPE_INTEGER;
  41. atpx_arg_elements[1].integer.value = ATPX_VERSION;
  42. status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer);
  43. if (ACPI_FAILURE(status)) {
  44. printk("%s: failed to call ATPX: %s\n", __func__, acpi_format_exception(status));
  45. return -ENOSYS;
  46. }
  47. obj = (union acpi_object *)buffer.pointer;
  48. if (obj && (obj->type == ACPI_TYPE_BUFFER))
  49. printk(KERN_INFO "radeon atpx: version is %d\n", *((u8 *)(obj->buffer.pointer) + 2));
  50. kfree(buffer.pointer);
  51. return 0;
  52. }
  53. static int radeon_atpx_execute(acpi_handle handle, int cmd_id, u16 value)
  54. {
  55. acpi_status status;
  56. union acpi_object atpx_arg_elements[2];
  57. struct acpi_object_list atpx_arg;
  58. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  59. uint8_t buf[4] = {0};
  60. if (!handle)
  61. return -EINVAL;
  62. atpx_arg.count = 2;
  63. atpx_arg.pointer = &atpx_arg_elements[0];
  64. atpx_arg_elements[0].type = ACPI_TYPE_INTEGER;
  65. atpx_arg_elements[0].integer.value = cmd_id;
  66. buf[2] = value & 0xff;
  67. buf[3] = (value >> 8) & 0xff;
  68. atpx_arg_elements[1].type = ACPI_TYPE_BUFFER;
  69. atpx_arg_elements[1].buffer.length = 4;
  70. atpx_arg_elements[1].buffer.pointer = buf;
  71. status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer);
  72. if (ACPI_FAILURE(status)) {
  73. printk("%s: failed to call ATPX: %s\n", __func__, acpi_format_exception(status));
  74. return -ENOSYS;
  75. }
  76. kfree(buffer.pointer);
  77. return 0;
  78. }
  79. static int radeon_atpx_set_discrete_state(acpi_handle handle, int state)
  80. {
  81. return radeon_atpx_execute(handle, ATPX_GPU_PWR, state);
  82. }
  83. static int radeon_atpx_switch_mux(acpi_handle handle, int mux_id)
  84. {
  85. return radeon_atpx_execute(handle, ATPX_MUX_SELECT, mux_id);
  86. }
  87. static int radeon_atpx_switch_i2c_mux(acpi_handle handle, int mux_id)
  88. {
  89. return radeon_atpx_execute(handle, ATPX_I2C_MUX_SELECT, mux_id);
  90. }
  91. static int radeon_atpx_switch_start(acpi_handle handle, int gpu_id)
  92. {
  93. return radeon_atpx_execute(handle, ATPX_SWITCH_START, gpu_id);
  94. }
  95. static int radeon_atpx_switch_end(acpi_handle handle, int gpu_id)
  96. {
  97. return radeon_atpx_execute(handle, ATPX_SWITCH_END, gpu_id);
  98. }
  99. static int radeon_atpx_switchto(enum vga_switcheroo_client_id id)
  100. {
  101. int gpu_id;
  102. if (id == VGA_SWITCHEROO_IGD)
  103. gpu_id = ATPX_INTEGRATED;
  104. else
  105. gpu_id = ATPX_DISCRETE;
  106. radeon_atpx_switch_start(radeon_atpx_priv.atpx_handle, gpu_id);
  107. radeon_atpx_switch_mux(radeon_atpx_priv.atpx_handle, gpu_id);
  108. radeon_atpx_switch_i2c_mux(radeon_atpx_priv.atpx_handle, gpu_id);
  109. radeon_atpx_switch_end(radeon_atpx_priv.atpx_handle, gpu_id);
  110. return 0;
  111. }
  112. static int radeon_atpx_power_state(enum vga_switcheroo_client_id id,
  113. enum vga_switcheroo_state state)
  114. {
  115. /* on w500 ACPI can't change intel gpu state */
  116. if (id == VGA_SWITCHEROO_IGD)
  117. return 0;
  118. radeon_atpx_set_discrete_state(radeon_atpx_priv.atpx_handle, state);
  119. return 0;
  120. }
  121. static bool radeon_atpx_pci_probe_handle(struct pci_dev *pdev)
  122. {
  123. acpi_handle dhandle, atpx_handle;
  124. acpi_status status;
  125. dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
  126. if (!dhandle)
  127. return false;
  128. status = acpi_get_handle(dhandle, "ATPX", &atpx_handle);
  129. if (ACPI_FAILURE(status))
  130. return false;
  131. radeon_atpx_priv.dhandle = dhandle;
  132. radeon_atpx_priv.atpx_handle = atpx_handle;
  133. return true;
  134. }
  135. static int radeon_atpx_init(void)
  136. {
  137. /* set up the ATPX handle */
  138. radeon_atpx_get_version(radeon_atpx_priv.atpx_handle);
  139. return 0;
  140. }
  141. static int radeon_atpx_get_client_id(struct pci_dev *pdev)
  142. {
  143. if (radeon_atpx_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev))
  144. return VGA_SWITCHEROO_IGD;
  145. else
  146. return VGA_SWITCHEROO_DIS;
  147. }
  148. static struct vga_switcheroo_handler radeon_atpx_handler = {
  149. .switchto = radeon_atpx_switchto,
  150. .power_state = radeon_atpx_power_state,
  151. .init = radeon_atpx_init,
  152. .get_client_id = radeon_atpx_get_client_id,
  153. };
  154. static bool radeon_atpx_detect(void)
  155. {
  156. char acpi_method_name[255] = { 0 };
  157. struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
  158. struct pci_dev *pdev = NULL;
  159. bool has_atpx = false;
  160. int vga_count = 0;
  161. while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
  162. vga_count++;
  163. has_atpx |= (radeon_atpx_pci_probe_handle(pdev) == true);
  164. }
  165. /* some newer PX laptops mark the dGPU as a non-VGA display device */
  166. while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) {
  167. vga_count++;
  168. has_atpx |= (radeon_atpx_pci_probe_handle(pdev) == true);
  169. }
  170. if (has_atpx && vga_count == 2) {
  171. acpi_get_name(radeon_atpx_priv.atpx_handle, ACPI_FULL_PATHNAME, &buffer);
  172. printk(KERN_INFO "VGA switcheroo: detected switching method %s handle\n",
  173. acpi_method_name);
  174. radeon_atpx_priv.atpx_detected = true;
  175. return true;
  176. }
  177. return false;
  178. }
  179. void radeon_register_atpx_handler(void)
  180. {
  181. bool r;
  182. /* detect if we have any ATPX + 2 VGA in the system */
  183. r = radeon_atpx_detect();
  184. if (!r)
  185. return;
  186. vga_switcheroo_register_handler(&radeon_atpx_handler);
  187. }
  188. void radeon_unregister_atpx_handler(void)
  189. {
  190. vga_switcheroo_unregister_handler();
  191. }