amdgpu_atpx_handler.c 15 KB

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