amdgpu_atpx_handler.c 15 KB

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