radeon_atpx_handler.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. #include "radeon_acpi.h"
  15. struct radeon_atpx_functions {
  16. bool px_params;
  17. bool power_cntl;
  18. bool disp_mux_cntl;
  19. bool i2c_mux_cntl;
  20. bool switch_start;
  21. bool switch_end;
  22. bool disp_connectors_mapping;
  23. bool disp_detetion_ports;
  24. };
  25. struct radeon_atpx {
  26. acpi_handle handle;
  27. struct radeon_atpx_functions functions;
  28. };
  29. static struct radeon_atpx_priv {
  30. bool atpx_detected;
  31. /* handle for device - and atpx */
  32. acpi_handle dhandle;
  33. struct radeon_atpx atpx;
  34. } radeon_atpx_priv;
  35. struct atpx_verify_interface {
  36. u16 size; /* structure size in bytes (includes size field) */
  37. u16 version; /* version */
  38. u32 function_bits; /* supported functions bit vector */
  39. } __packed;
  40. struct atpx_power_control {
  41. u16 size;
  42. u8 dgpu_state;
  43. } __packed;
  44. struct atpx_mux {
  45. u16 size;
  46. u16 mux;
  47. } __packed;
  48. /**
  49. * radeon_atpx_call - call an ATPX method
  50. *
  51. * @handle: acpi handle
  52. * @function: the ATPX function to execute
  53. * @params: ATPX function params
  54. *
  55. * Executes the requested ATPX function (all asics).
  56. * Returns a pointer to the acpi output buffer.
  57. */
  58. static union acpi_object *radeon_atpx_call(acpi_handle handle, int function,
  59. struct acpi_buffer *params)
  60. {
  61. acpi_status status;
  62. union acpi_object atpx_arg_elements[2];
  63. struct acpi_object_list atpx_arg;
  64. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  65. atpx_arg.count = 2;
  66. atpx_arg.pointer = &atpx_arg_elements[0];
  67. atpx_arg_elements[0].type = ACPI_TYPE_INTEGER;
  68. atpx_arg_elements[0].integer.value = function;
  69. if (params) {
  70. atpx_arg_elements[1].type = ACPI_TYPE_BUFFER;
  71. atpx_arg_elements[1].buffer.length = params->length;
  72. atpx_arg_elements[1].buffer.pointer = params->pointer;
  73. } else {
  74. /* We need a second fake parameter */
  75. atpx_arg_elements[1].type = ACPI_TYPE_INTEGER;
  76. atpx_arg_elements[1].integer.value = 0;
  77. }
  78. status = acpi_evaluate_object(handle, "ATPX", &atpx_arg, &buffer);
  79. /* Fail only if calling the method fails and ATPX is supported */
  80. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  81. printk("failed to evaluate ATPX got %s\n",
  82. acpi_format_exception(status));
  83. kfree(buffer.pointer);
  84. return NULL;
  85. }
  86. return buffer.pointer;
  87. }
  88. /**
  89. * radeon_atpx_parse_functions - parse supported functions
  90. *
  91. * @f: supported functions struct
  92. * @mask: supported functions mask from ATPX
  93. *
  94. * Use the supported functions mask from ATPX function
  95. * ATPX_FUNCTION_VERIFY_INTERFACE to determine what functions
  96. * are supported (all asics).
  97. */
  98. static void radeon_atpx_parse_functions(struct radeon_atpx_functions *f, u32 mask)
  99. {
  100. f->px_params = mask & ATPX_GET_PX_PARAMETERS_SUPPORTED;
  101. f->power_cntl = mask & ATPX_POWER_CONTROL_SUPPORTED;
  102. f->disp_mux_cntl = mask & ATPX_DISPLAY_MUX_CONTROL_SUPPORTED;
  103. f->i2c_mux_cntl = mask & ATPX_I2C_MUX_CONTROL_SUPPORTED;
  104. f->switch_start = mask & ATPX_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION_SUPPORTED;
  105. f->switch_end = mask & ATPX_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION_SUPPORTED;
  106. f->disp_connectors_mapping = mask & ATPX_GET_DISPLAY_CONNECTORS_MAPPING_SUPPORTED;
  107. f->disp_detetion_ports = mask & ATPX_GET_DISPLAY_DETECTION_PORTS_SUPPORTED;
  108. }
  109. /**
  110. * radeon_atpx_verify_interface - verify ATPX
  111. *
  112. * @handle: acpi handle
  113. * @atpx: radeon atpx struct
  114. *
  115. * Execute the ATPX_FUNCTION_VERIFY_INTERFACE ATPX function
  116. * to initialize ATPX and determine what features are supported
  117. * (all asics).
  118. * returns 0 on success, error on failure.
  119. */
  120. static int radeon_atpx_verify_interface(struct radeon_atpx *atpx)
  121. {
  122. union acpi_object *info;
  123. struct atpx_verify_interface output;
  124. size_t size;
  125. int err = 0;
  126. info = radeon_atpx_call(atpx->handle, ATPX_FUNCTION_VERIFY_INTERFACE, NULL);
  127. if (!info)
  128. return -EIO;
  129. memset(&output, 0, sizeof(output));
  130. size = *(u16 *) info->buffer.pointer;
  131. if (size < 8) {
  132. printk("ATPX buffer is too small: %lu\n", size);
  133. err = -EINVAL;
  134. goto out;
  135. }
  136. size = min(sizeof(output), size);
  137. memcpy(&output, info->buffer.pointer, size);
  138. /* TODO: check version? */
  139. printk("ATPX version %u\n", output.version);
  140. radeon_atpx_parse_functions(&atpx->functions, output.function_bits);
  141. out:
  142. kfree(info);
  143. return err;
  144. }
  145. /**
  146. * radeon_atpx_set_discrete_state - power up/down discrete GPU
  147. *
  148. * @atpx: atpx info struct
  149. * @state: discrete GPU state (0 = power down, 1 = power up)
  150. *
  151. * Execute the ATPX_FUNCTION_POWER_CONTROL ATPX function to
  152. * power down/up the discrete GPU (all asics).
  153. * Returns 0 on success, error on failure.
  154. */
  155. static int radeon_atpx_set_discrete_state(struct radeon_atpx *atpx, u8 state)
  156. {
  157. struct acpi_buffer params;
  158. union acpi_object *info;
  159. struct atpx_power_control input;
  160. if (atpx->functions.power_cntl) {
  161. input.size = 3;
  162. input.dgpu_state = state;
  163. params.length = input.size;
  164. params.pointer = &input;
  165. info = radeon_atpx_call(atpx->handle,
  166. ATPX_FUNCTION_POWER_CONTROL,
  167. &params);
  168. if (!info)
  169. return -EIO;
  170. kfree(info);
  171. }
  172. return 0;
  173. }
  174. /**
  175. * radeon_atpx_switch_disp_mux - switch display mux
  176. *
  177. * @atpx: atpx info struct
  178. * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
  179. *
  180. * Execute the ATPX_FUNCTION_DISPLAY_MUX_CONTROL ATPX function to
  181. * switch the display mux between the discrete GPU and integrated GPU
  182. * (all asics).
  183. * Returns 0 on success, error on failure.
  184. */
  185. static int radeon_atpx_switch_disp_mux(struct radeon_atpx *atpx, u16 mux_id)
  186. {
  187. struct acpi_buffer params;
  188. union acpi_object *info;
  189. struct atpx_mux input;
  190. if (atpx->functions.disp_mux_cntl) {
  191. input.size = 4;
  192. input.mux = mux_id;
  193. params.length = input.size;
  194. params.pointer = &input;
  195. info = radeon_atpx_call(atpx->handle,
  196. ATPX_FUNCTION_DISPLAY_MUX_CONTROL,
  197. &params);
  198. if (!info)
  199. return -EIO;
  200. kfree(info);
  201. }
  202. return 0;
  203. }
  204. /**
  205. * radeon_atpx_switch_i2c_mux - switch i2c/hpd mux
  206. *
  207. * @atpx: atpx info struct
  208. * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
  209. *
  210. * Execute the ATPX_FUNCTION_I2C_MUX_CONTROL ATPX function to
  211. * switch the i2c/hpd mux between the discrete GPU and integrated GPU
  212. * (all asics).
  213. * Returns 0 on success, error on failure.
  214. */
  215. static int radeon_atpx_switch_i2c_mux(struct radeon_atpx *atpx, u16 mux_id)
  216. {
  217. struct acpi_buffer params;
  218. union acpi_object *info;
  219. struct atpx_mux input;
  220. if (atpx->functions.i2c_mux_cntl) {
  221. input.size = 4;
  222. input.mux = mux_id;
  223. params.length = input.size;
  224. params.pointer = &input;
  225. info = radeon_atpx_call(atpx->handle,
  226. ATPX_FUNCTION_I2C_MUX_CONTROL,
  227. &params);
  228. if (!info)
  229. return -EIO;
  230. kfree(info);
  231. }
  232. return 0;
  233. }
  234. /**
  235. * radeon_atpx_switch_start - notify the sbios of a GPU switch
  236. *
  237. * @atpx: atpx info struct
  238. * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
  239. *
  240. * Execute the ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION ATPX
  241. * function to notify the sbios that a switch between the discrete GPU and
  242. * integrated GPU has begun (all asics).
  243. * Returns 0 on success, error on failure.
  244. */
  245. static int radeon_atpx_switch_start(struct radeon_atpx *atpx, u16 mux_id)
  246. {
  247. struct acpi_buffer params;
  248. union acpi_object *info;
  249. struct atpx_mux input;
  250. if (atpx->functions.switch_start) {
  251. input.size = 4;
  252. input.mux = mux_id;
  253. params.length = input.size;
  254. params.pointer = &input;
  255. info = radeon_atpx_call(atpx->handle,
  256. ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION,
  257. &params);
  258. if (!info)
  259. return -EIO;
  260. kfree(info);
  261. }
  262. return 0;
  263. }
  264. /**
  265. * radeon_atpx_switch_end - notify the sbios of a GPU switch
  266. *
  267. * @atpx: atpx info struct
  268. * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
  269. *
  270. * Execute the ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION ATPX
  271. * function to notify the sbios that a switch between the discrete GPU and
  272. * integrated GPU has ended (all asics).
  273. * Returns 0 on success, error on failure.
  274. */
  275. static int radeon_atpx_switch_end(struct radeon_atpx *atpx, u16 mux_id)
  276. {
  277. struct acpi_buffer params;
  278. union acpi_object *info;
  279. struct atpx_mux input;
  280. if (atpx->functions.switch_end) {
  281. input.size = 4;
  282. input.mux = mux_id;
  283. params.length = input.size;
  284. params.pointer = &input;
  285. info = radeon_atpx_call(atpx->handle,
  286. ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION,
  287. &params);
  288. if (!info)
  289. return -EIO;
  290. kfree(info);
  291. }
  292. return 0;
  293. }
  294. /**
  295. * radeon_atpx_switchto - switch to the requested GPU
  296. *
  297. * @id: GPU to switch to
  298. *
  299. * Execute the necessary ATPX functions to switch between the discrete GPU and
  300. * integrated GPU (all asics).
  301. * Returns 0 on success, error on failure.
  302. */
  303. static int radeon_atpx_switchto(enum vga_switcheroo_client_id id)
  304. {
  305. u16 gpu_id;
  306. if (id == VGA_SWITCHEROO_IGD)
  307. gpu_id = ATPX_INTEGRATED_GPU;
  308. else
  309. gpu_id = ATPX_DISCRETE_GPU;
  310. radeon_atpx_switch_start(&radeon_atpx_priv.atpx, gpu_id);
  311. radeon_atpx_switch_disp_mux(&radeon_atpx_priv.atpx, gpu_id);
  312. radeon_atpx_switch_i2c_mux(&radeon_atpx_priv.atpx, gpu_id);
  313. radeon_atpx_switch_end(&radeon_atpx_priv.atpx, gpu_id);
  314. return 0;
  315. }
  316. /**
  317. * radeon_atpx_switchto - switch to the requested GPU
  318. *
  319. * @id: GPU to switch to
  320. * @state: requested power state (0 = off, 1 = on)
  321. *
  322. * Execute the necessary ATPX function to power down/up the discrete GPU
  323. * (all asics).
  324. * Returns 0 on success, error on failure.
  325. */
  326. static int radeon_atpx_power_state(enum vga_switcheroo_client_id id,
  327. enum vga_switcheroo_state state)
  328. {
  329. /* on w500 ACPI can't change intel gpu state */
  330. if (id == VGA_SWITCHEROO_IGD)
  331. return 0;
  332. radeon_atpx_set_discrete_state(&radeon_atpx_priv.atpx, state);
  333. return 0;
  334. }
  335. /**
  336. * radeon_atpx_pci_probe_handle - look up the ATRM and ATPX handles
  337. *
  338. * @pdev: pci device
  339. *
  340. * Look up the ATPX and ATRM handles (all asics).
  341. * Returns true if the handles are found, false if not.
  342. */
  343. static bool radeon_atpx_pci_probe_handle(struct pci_dev *pdev)
  344. {
  345. acpi_handle dhandle, atpx_handle;
  346. acpi_status status;
  347. dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
  348. if (!dhandle)
  349. return false;
  350. status = acpi_get_handle(dhandle, "ATPX", &atpx_handle);
  351. if (ACPI_FAILURE(status))
  352. return false;
  353. radeon_atpx_priv.dhandle = dhandle;
  354. radeon_atpx_priv.atpx.handle = atpx_handle;
  355. return true;
  356. }
  357. /**
  358. * radeon_atpx_init - verify the ATPX interface
  359. *
  360. * Verify the ATPX interface (all asics).
  361. * Returns 0 on success, error on failure.
  362. */
  363. static int radeon_atpx_init(void)
  364. {
  365. /* set up the ATPX handle */
  366. return radeon_atpx_verify_interface(&radeon_atpx_priv.atpx);
  367. }
  368. /**
  369. * radeon_atpx_get_client_id - get the client id
  370. *
  371. * @pdev: pci device
  372. *
  373. * look up whether we are the integrated or discrete GPU (all asics).
  374. * Returns the client id.
  375. */
  376. static int radeon_atpx_get_client_id(struct pci_dev *pdev)
  377. {
  378. if (radeon_atpx_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev))
  379. return VGA_SWITCHEROO_IGD;
  380. else
  381. return VGA_SWITCHEROO_DIS;
  382. }
  383. static struct vga_switcheroo_handler radeon_atpx_handler = {
  384. .switchto = radeon_atpx_switchto,
  385. .power_state = radeon_atpx_power_state,
  386. .init = radeon_atpx_init,
  387. .get_client_id = radeon_atpx_get_client_id,
  388. };
  389. /**
  390. * radeon_atpx_detect - detect whether we have PX
  391. *
  392. * Check if we have a PX system (all asics).
  393. * Returns true if we have a PX system, false if not.
  394. */
  395. static bool radeon_atpx_detect(void)
  396. {
  397. char acpi_method_name[255] = { 0 };
  398. struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
  399. struct pci_dev *pdev = NULL;
  400. bool has_atpx = false;
  401. int vga_count = 0;
  402. while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
  403. vga_count++;
  404. has_atpx |= (radeon_atpx_pci_probe_handle(pdev) == true);
  405. }
  406. if (has_atpx && vga_count == 2) {
  407. acpi_get_name(radeon_atpx_priv.atpx.handle, ACPI_FULL_PATHNAME, &buffer);
  408. printk(KERN_INFO "VGA switcheroo: detected switching method %s handle\n",
  409. acpi_method_name);
  410. radeon_atpx_priv.atpx_detected = true;
  411. return true;
  412. }
  413. return false;
  414. }
  415. /**
  416. * radeon_register_atpx_handler - register with vga_switcheroo
  417. *
  418. * Register the PX callbacks with vga_switcheroo (all asics).
  419. */
  420. void radeon_register_atpx_handler(void)
  421. {
  422. bool r;
  423. /* detect if we have any ATPX + 2 VGA in the system */
  424. r = radeon_atpx_detect();
  425. if (!r)
  426. return;
  427. vga_switcheroo_register_handler(&radeon_atpx_handler);
  428. }
  429. /**
  430. * radeon_unregister_atpx_handler - unregister with vga_switcheroo
  431. *
  432. * Unregister the PX callbacks with vga_switcheroo (all asics).
  433. */
  434. void radeon_unregister_atpx_handler(void)
  435. {
  436. vga_switcheroo_unregister_handler();
  437. }