amdgpu_atpx_handler.c 17 KB

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