radeon_atpx_handler.c 6.4 KB

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