radeon_atpx_handler.c 14 KB

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