radeon_atpx_handler.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. acpi_handle atrm_handle;
  30. } radeon_atpx_priv;
  31. /* retrieve the ROM in 4k blocks */
  32. static int radeon_atrm_call(acpi_handle atrm_handle, uint8_t *bios,
  33. int offset, int len)
  34. {
  35. acpi_status status;
  36. union acpi_object atrm_arg_elements[2], *obj;
  37. struct acpi_object_list atrm_arg;
  38. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL};
  39. atrm_arg.count = 2;
  40. atrm_arg.pointer = &atrm_arg_elements[0];
  41. atrm_arg_elements[0].type = ACPI_TYPE_INTEGER;
  42. atrm_arg_elements[0].integer.value = offset;
  43. atrm_arg_elements[1].type = ACPI_TYPE_INTEGER;
  44. atrm_arg_elements[1].integer.value = len;
  45. status = acpi_evaluate_object(atrm_handle, NULL, &atrm_arg, &buffer);
  46. if (ACPI_FAILURE(status)) {
  47. printk("failed to evaluate ATRM got %s\n", acpi_format_exception(status));
  48. return -ENODEV;
  49. }
  50. obj = (union acpi_object *)buffer.pointer;
  51. memcpy(bios+offset, obj->buffer.pointer, obj->buffer.length);
  52. len = obj->buffer.length;
  53. kfree(buffer.pointer);
  54. return len;
  55. }
  56. bool radeon_atrm_supported(struct pci_dev *pdev)
  57. {
  58. /* get the discrete ROM only via ATRM */
  59. if (!radeon_atpx_priv.atpx_detected)
  60. return false;
  61. if (radeon_atpx_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev))
  62. return false;
  63. return true;
  64. }
  65. int radeon_atrm_get_bios_chunk(uint8_t *bios, int offset, int len)
  66. {
  67. return radeon_atrm_call(radeon_atpx_priv.atrm_handle, bios, offset, len);
  68. }
  69. static int radeon_atpx_get_version(acpi_handle handle)
  70. {
  71. acpi_status status;
  72. union acpi_object atpx_arg_elements[2], *obj;
  73. struct acpi_object_list atpx_arg;
  74. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  75. atpx_arg.count = 2;
  76. atpx_arg.pointer = &atpx_arg_elements[0];
  77. atpx_arg_elements[0].type = ACPI_TYPE_INTEGER;
  78. atpx_arg_elements[0].integer.value = ATPX_VERSION;
  79. atpx_arg_elements[1].type = ACPI_TYPE_INTEGER;
  80. atpx_arg_elements[1].integer.value = ATPX_VERSION;
  81. status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer);
  82. if (ACPI_FAILURE(status)) {
  83. printk("%s: failed to call ATPX: %s\n", __func__, acpi_format_exception(status));
  84. return -ENOSYS;
  85. }
  86. obj = (union acpi_object *)buffer.pointer;
  87. if (obj && (obj->type == ACPI_TYPE_BUFFER))
  88. printk(KERN_INFO "radeon atpx: version is %d\n", *((u8 *)(obj->buffer.pointer) + 2));
  89. kfree(buffer.pointer);
  90. return 0;
  91. }
  92. static int radeon_atpx_execute(acpi_handle handle, int cmd_id, u16 value)
  93. {
  94. acpi_status status;
  95. union acpi_object atpx_arg_elements[2];
  96. struct acpi_object_list atpx_arg;
  97. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  98. uint8_t buf[4] = {0};
  99. if (!handle)
  100. return -EINVAL;
  101. atpx_arg.count = 2;
  102. atpx_arg.pointer = &atpx_arg_elements[0];
  103. atpx_arg_elements[0].type = ACPI_TYPE_INTEGER;
  104. atpx_arg_elements[0].integer.value = cmd_id;
  105. buf[2] = value & 0xff;
  106. buf[3] = (value >> 8) & 0xff;
  107. atpx_arg_elements[1].type = ACPI_TYPE_BUFFER;
  108. atpx_arg_elements[1].buffer.length = 4;
  109. atpx_arg_elements[1].buffer.pointer = buf;
  110. status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer);
  111. if (ACPI_FAILURE(status)) {
  112. printk("%s: failed to call ATPX: %s\n", __func__, acpi_format_exception(status));
  113. return -ENOSYS;
  114. }
  115. kfree(buffer.pointer);
  116. return 0;
  117. }
  118. static int radeon_atpx_set_discrete_state(acpi_handle handle, int state)
  119. {
  120. return radeon_atpx_execute(handle, ATPX_GPU_PWR, state);
  121. }
  122. static int radeon_atpx_switch_mux(acpi_handle handle, int mux_id)
  123. {
  124. return radeon_atpx_execute(handle, ATPX_MUX_SELECT, mux_id);
  125. }
  126. static int radeon_atpx_switch_i2c_mux(acpi_handle handle, int mux_id)
  127. {
  128. return radeon_atpx_execute(handle, ATPX_I2C_MUX_SELECT, mux_id);
  129. }
  130. static int radeon_atpx_switch_start(acpi_handle handle, int gpu_id)
  131. {
  132. return radeon_atpx_execute(handle, ATPX_SWITCH_START, gpu_id);
  133. }
  134. static int radeon_atpx_switch_end(acpi_handle handle, int gpu_id)
  135. {
  136. return radeon_atpx_execute(handle, ATPX_SWITCH_END, gpu_id);
  137. }
  138. static int radeon_atpx_switchto(enum vga_switcheroo_client_id id)
  139. {
  140. int gpu_id;
  141. if (id == VGA_SWITCHEROO_IGD)
  142. gpu_id = ATPX_INTEGRATED;
  143. else
  144. gpu_id = ATPX_DISCRETE;
  145. radeon_atpx_switch_start(radeon_atpx_priv.atpx_handle, gpu_id);
  146. radeon_atpx_switch_mux(radeon_atpx_priv.atpx_handle, gpu_id);
  147. radeon_atpx_switch_i2c_mux(radeon_atpx_priv.atpx_handle, gpu_id);
  148. radeon_atpx_switch_end(radeon_atpx_priv.atpx_handle, gpu_id);
  149. return 0;
  150. }
  151. static int radeon_atpx_power_state(enum vga_switcheroo_client_id id,
  152. enum vga_switcheroo_state state)
  153. {
  154. /* on w500 ACPI can't change intel gpu state */
  155. if (id == VGA_SWITCHEROO_IGD)
  156. return 0;
  157. radeon_atpx_set_discrete_state(radeon_atpx_priv.atpx_handle, state);
  158. return 0;
  159. }
  160. static bool radeon_atpx_pci_probe_handle(struct pci_dev *pdev)
  161. {
  162. acpi_handle dhandle, atpx_handle, atrm_handle;
  163. acpi_status status;
  164. dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
  165. if (!dhandle)
  166. return false;
  167. status = acpi_get_handle(dhandle, "ATPX", &atpx_handle);
  168. if (ACPI_FAILURE(status))
  169. return false;
  170. status = acpi_get_handle(dhandle, "ATRM", &atrm_handle);
  171. if (ACPI_FAILURE(status))
  172. return false;
  173. radeon_atpx_priv.dhandle = dhandle;
  174. radeon_atpx_priv.atpx_handle = atpx_handle;
  175. radeon_atpx_priv.atrm_handle = atrm_handle;
  176. return true;
  177. }
  178. static int radeon_atpx_init(void)
  179. {
  180. /* set up the ATPX handle */
  181. radeon_atpx_get_version(radeon_atpx_priv.atpx_handle);
  182. return 0;
  183. }
  184. static int radeon_atpx_get_client_id(struct pci_dev *pdev)
  185. {
  186. if (radeon_atpx_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev))
  187. return VGA_SWITCHEROO_IGD;
  188. else
  189. return VGA_SWITCHEROO_DIS;
  190. }
  191. static struct vga_switcheroo_handler radeon_atpx_handler = {
  192. .switchto = radeon_atpx_switchto,
  193. .power_state = radeon_atpx_power_state,
  194. .init = radeon_atpx_init,
  195. .get_client_id = radeon_atpx_get_client_id,
  196. };
  197. static bool radeon_atpx_detect(void)
  198. {
  199. char acpi_method_name[255] = { 0 };
  200. struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
  201. struct pci_dev *pdev = NULL;
  202. bool has_atpx = false;
  203. int vga_count = 0;
  204. while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
  205. vga_count++;
  206. has_atpx |= (radeon_atpx_pci_probe_handle(pdev) == true);
  207. }
  208. if (has_atpx && vga_count == 2) {
  209. acpi_get_name(radeon_atpx_priv.atpx_handle, ACPI_FULL_PATHNAME, &buffer);
  210. printk(KERN_INFO "VGA switcheroo: detected switching method %s handle\n",
  211. acpi_method_name);
  212. radeon_atpx_priv.atpx_detected = true;
  213. return true;
  214. }
  215. return false;
  216. }
  217. void radeon_register_atpx_handler(void)
  218. {
  219. bool r;
  220. /* detect if we have any ATPX + 2 VGA in the system */
  221. r = radeon_atpx_detect();
  222. if (!r)
  223. return;
  224. vga_switcheroo_register_handler(&radeon_atpx_handler);
  225. }
  226. void radeon_unregister_atpx_handler(void)
  227. {
  228. vga_switcheroo_unregister_handler();
  229. }