radeon_atpx_handler.c 14 KB

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