amdgpu_atpx_handler.c 16 KB

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