amdgpu_atpx_handler.c 15 KB

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