amdgpu_atpx_handler.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. atpx->functions.power_cntl = false;
  173. atpx->is_hybrid = true;
  174. }
  175. return 0;
  176. }
  177. /**
  178. * amdgpu_atpx_verify_interface - verify ATPX
  179. *
  180. * @atpx: amdgpu atpx struct
  181. *
  182. * Execute the ATPX_FUNCTION_VERIFY_INTERFACE ATPX function
  183. * to initialize ATPX and determine what features are supported
  184. * (all asics).
  185. * returns 0 on success, error on failure.
  186. */
  187. static int amdgpu_atpx_verify_interface(struct amdgpu_atpx *atpx)
  188. {
  189. union acpi_object *info;
  190. struct atpx_verify_interface output;
  191. size_t size;
  192. int err = 0;
  193. info = amdgpu_atpx_call(atpx->handle, ATPX_FUNCTION_VERIFY_INTERFACE, NULL);
  194. if (!info)
  195. return -EIO;
  196. memset(&output, 0, sizeof(output));
  197. size = *(u16 *) info->buffer.pointer;
  198. if (size < 8) {
  199. printk("ATPX buffer is too small: %zu\n", size);
  200. err = -EINVAL;
  201. goto out;
  202. }
  203. size = min(sizeof(output), size);
  204. memcpy(&output, info->buffer.pointer, size);
  205. /* TODO: check version? */
  206. printk("ATPX version %u, functions 0x%08x\n",
  207. output.version, output.function_bits);
  208. amdgpu_atpx_parse_functions(&atpx->functions, output.function_bits);
  209. out:
  210. kfree(info);
  211. return err;
  212. }
  213. /**
  214. * amdgpu_atpx_set_discrete_state - power up/down discrete GPU
  215. *
  216. * @atpx: atpx info struct
  217. * @state: discrete GPU state (0 = power down, 1 = power up)
  218. *
  219. * Execute the ATPX_FUNCTION_POWER_CONTROL ATPX function to
  220. * power down/up the discrete GPU (all asics).
  221. * Returns 0 on success, error on failure.
  222. */
  223. static int amdgpu_atpx_set_discrete_state(struct amdgpu_atpx *atpx, u8 state)
  224. {
  225. struct acpi_buffer params;
  226. union acpi_object *info;
  227. struct atpx_power_control input;
  228. if (atpx->functions.power_cntl) {
  229. input.size = 3;
  230. input.dgpu_state = state;
  231. params.length = input.size;
  232. params.pointer = &input;
  233. info = amdgpu_atpx_call(atpx->handle,
  234. ATPX_FUNCTION_POWER_CONTROL,
  235. &params);
  236. if (!info)
  237. return -EIO;
  238. kfree(info);
  239. /* 200ms delay is required after off */
  240. if (state == 0)
  241. msleep(200);
  242. }
  243. return 0;
  244. }
  245. /**
  246. * amdgpu_atpx_switch_disp_mux - switch display mux
  247. *
  248. * @atpx: atpx info struct
  249. * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
  250. *
  251. * Execute the ATPX_FUNCTION_DISPLAY_MUX_CONTROL ATPX function to
  252. * switch the display mux between the discrete GPU and integrated GPU
  253. * (all asics).
  254. * Returns 0 on success, error on failure.
  255. */
  256. static int amdgpu_atpx_switch_disp_mux(struct amdgpu_atpx *atpx, u16 mux_id)
  257. {
  258. struct acpi_buffer params;
  259. union acpi_object *info;
  260. struct atpx_mux input;
  261. if (atpx->functions.disp_mux_cntl) {
  262. input.size = 4;
  263. input.mux = mux_id;
  264. params.length = input.size;
  265. params.pointer = &input;
  266. info = amdgpu_atpx_call(atpx->handle,
  267. ATPX_FUNCTION_DISPLAY_MUX_CONTROL,
  268. &params);
  269. if (!info)
  270. return -EIO;
  271. kfree(info);
  272. }
  273. return 0;
  274. }
  275. /**
  276. * amdgpu_atpx_switch_i2c_mux - switch i2c/hpd mux
  277. *
  278. * @atpx: atpx info struct
  279. * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
  280. *
  281. * Execute the ATPX_FUNCTION_I2C_MUX_CONTROL ATPX function to
  282. * switch the i2c/hpd mux between the discrete GPU and integrated GPU
  283. * (all asics).
  284. * Returns 0 on success, error on failure.
  285. */
  286. static int amdgpu_atpx_switch_i2c_mux(struct amdgpu_atpx *atpx, u16 mux_id)
  287. {
  288. struct acpi_buffer params;
  289. union acpi_object *info;
  290. struct atpx_mux input;
  291. if (atpx->functions.i2c_mux_cntl) {
  292. input.size = 4;
  293. input.mux = mux_id;
  294. params.length = input.size;
  295. params.pointer = &input;
  296. info = amdgpu_atpx_call(atpx->handle,
  297. ATPX_FUNCTION_I2C_MUX_CONTROL,
  298. &params);
  299. if (!info)
  300. return -EIO;
  301. kfree(info);
  302. }
  303. return 0;
  304. }
  305. /**
  306. * amdgpu_atpx_switch_start - notify the sbios of a GPU switch
  307. *
  308. * @atpx: atpx info struct
  309. * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
  310. *
  311. * Execute the ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION ATPX
  312. * function to notify the sbios that a switch between the discrete GPU and
  313. * integrated GPU has begun (all asics).
  314. * Returns 0 on success, error on failure.
  315. */
  316. static int amdgpu_atpx_switch_start(struct amdgpu_atpx *atpx, u16 mux_id)
  317. {
  318. struct acpi_buffer params;
  319. union acpi_object *info;
  320. struct atpx_mux input;
  321. if (atpx->functions.switch_start) {
  322. input.size = 4;
  323. input.mux = mux_id;
  324. params.length = input.size;
  325. params.pointer = &input;
  326. info = amdgpu_atpx_call(atpx->handle,
  327. ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION,
  328. &params);
  329. if (!info)
  330. return -EIO;
  331. kfree(info);
  332. }
  333. return 0;
  334. }
  335. /**
  336. * amdgpu_atpx_switch_end - notify the sbios of a GPU switch
  337. *
  338. * @atpx: atpx info struct
  339. * @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)
  340. *
  341. * Execute the ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION ATPX
  342. * function to notify the sbios that a switch between the discrete GPU and
  343. * integrated GPU has ended (all asics).
  344. * Returns 0 on success, error on failure.
  345. */
  346. static int amdgpu_atpx_switch_end(struct amdgpu_atpx *atpx, u16 mux_id)
  347. {
  348. struct acpi_buffer params;
  349. union acpi_object *info;
  350. struct atpx_mux input;
  351. if (atpx->functions.switch_end) {
  352. input.size = 4;
  353. input.mux = mux_id;
  354. params.length = input.size;
  355. params.pointer = &input;
  356. info = amdgpu_atpx_call(atpx->handle,
  357. ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION,
  358. &params);
  359. if (!info)
  360. return -EIO;
  361. kfree(info);
  362. }
  363. return 0;
  364. }
  365. /**
  366. * amdgpu_atpx_switchto - switch to the requested GPU
  367. *
  368. * @id: GPU to switch to
  369. *
  370. * Execute the necessary ATPX functions to switch between the discrete GPU and
  371. * integrated GPU (all asics).
  372. * Returns 0 on success, error on failure.
  373. */
  374. static int amdgpu_atpx_switchto(enum vga_switcheroo_client_id id)
  375. {
  376. u16 gpu_id;
  377. if (id == VGA_SWITCHEROO_IGD)
  378. gpu_id = ATPX_INTEGRATED_GPU;
  379. else
  380. gpu_id = ATPX_DISCRETE_GPU;
  381. amdgpu_atpx_switch_start(&amdgpu_atpx_priv.atpx, gpu_id);
  382. amdgpu_atpx_switch_disp_mux(&amdgpu_atpx_priv.atpx, gpu_id);
  383. amdgpu_atpx_switch_i2c_mux(&amdgpu_atpx_priv.atpx, gpu_id);
  384. amdgpu_atpx_switch_end(&amdgpu_atpx_priv.atpx, gpu_id);
  385. return 0;
  386. }
  387. /**
  388. * amdgpu_atpx_power_state - power down/up the requested GPU
  389. *
  390. * @id: GPU to power down/up
  391. * @state: requested power state (0 = off, 1 = on)
  392. *
  393. * Execute the necessary ATPX function to power down/up the discrete GPU
  394. * (all asics).
  395. * Returns 0 on success, error on failure.
  396. */
  397. static int amdgpu_atpx_power_state(enum vga_switcheroo_client_id id,
  398. enum vga_switcheroo_state state)
  399. {
  400. /* on w500 ACPI can't change intel gpu state */
  401. if (id == VGA_SWITCHEROO_IGD)
  402. return 0;
  403. amdgpu_atpx_set_discrete_state(&amdgpu_atpx_priv.atpx, state);
  404. return 0;
  405. }
  406. /**
  407. * amdgpu_atpx_pci_probe_handle - look up the ATPX handle
  408. *
  409. * @pdev: pci device
  410. *
  411. * Look up the ATPX handles (all asics).
  412. * Returns true if the handles are found, false if not.
  413. */
  414. static bool amdgpu_atpx_pci_probe_handle(struct pci_dev *pdev)
  415. {
  416. acpi_handle dhandle, atpx_handle;
  417. acpi_status status;
  418. dhandle = ACPI_HANDLE(&pdev->dev);
  419. if (!dhandle)
  420. return false;
  421. status = acpi_get_handle(dhandle, "ATPX", &atpx_handle);
  422. if (ACPI_FAILURE(status)) {
  423. amdgpu_atpx_priv.other_handle = dhandle;
  424. return false;
  425. }
  426. amdgpu_atpx_priv.dhandle = dhandle;
  427. amdgpu_atpx_priv.atpx.handle = atpx_handle;
  428. return true;
  429. }
  430. /**
  431. * amdgpu_atpx_init - verify the ATPX interface
  432. *
  433. * Verify the ATPX interface (all asics).
  434. * Returns 0 on success, error on failure.
  435. */
  436. static int amdgpu_atpx_init(void)
  437. {
  438. int r;
  439. /* set up the ATPX handle */
  440. r = amdgpu_atpx_verify_interface(&amdgpu_atpx_priv.atpx);
  441. if (r)
  442. return r;
  443. /* validate the atpx setup */
  444. r = amdgpu_atpx_validate(&amdgpu_atpx_priv.atpx);
  445. if (r)
  446. return r;
  447. return 0;
  448. }
  449. /**
  450. * amdgpu_atpx_get_client_id - get the client id
  451. *
  452. * @pdev: pci device
  453. *
  454. * look up whether we are the integrated or discrete GPU (all asics).
  455. * Returns the client id.
  456. */
  457. static int amdgpu_atpx_get_client_id(struct pci_dev *pdev)
  458. {
  459. if (amdgpu_atpx_priv.dhandle == ACPI_HANDLE(&pdev->dev))
  460. return VGA_SWITCHEROO_IGD;
  461. else
  462. return VGA_SWITCHEROO_DIS;
  463. }
  464. static const struct vga_switcheroo_handler amdgpu_atpx_handler = {
  465. .switchto = amdgpu_atpx_switchto,
  466. .power_state = amdgpu_atpx_power_state,
  467. .get_client_id = amdgpu_atpx_get_client_id,
  468. };
  469. /**
  470. * amdgpu_atpx_detect - detect whether we have PX
  471. *
  472. * Check if we have a PX system (all asics).
  473. * Returns true if we have a PX system, false if not.
  474. */
  475. static bool amdgpu_atpx_detect(void)
  476. {
  477. char acpi_method_name[255] = { 0 };
  478. struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
  479. struct pci_dev *pdev = NULL;
  480. bool has_atpx = false;
  481. int vga_count = 0;
  482. while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
  483. vga_count++;
  484. has_atpx |= (amdgpu_atpx_pci_probe_handle(pdev) == true);
  485. }
  486. while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) {
  487. vga_count++;
  488. has_atpx |= (amdgpu_atpx_pci_probe_handle(pdev) == true);
  489. }
  490. if (has_atpx && vga_count == 2) {
  491. acpi_get_name(amdgpu_atpx_priv.atpx.handle, ACPI_FULL_PATHNAME, &buffer);
  492. printk(KERN_INFO "vga_switcheroo: detected switching method %s handle\n",
  493. acpi_method_name);
  494. amdgpu_atpx_priv.atpx_detected = true;
  495. amdgpu_atpx_init();
  496. return true;
  497. }
  498. return false;
  499. }
  500. /**
  501. * amdgpu_register_atpx_handler - register with vga_switcheroo
  502. *
  503. * Register the PX callbacks with vga_switcheroo (all asics).
  504. */
  505. void amdgpu_register_atpx_handler(void)
  506. {
  507. bool r;
  508. enum vga_switcheroo_handler_flags_t handler_flags = 0;
  509. /* detect if we have any ATPX + 2 VGA in the system */
  510. r = amdgpu_atpx_detect();
  511. if (!r)
  512. return;
  513. vga_switcheroo_register_handler(&amdgpu_atpx_handler, handler_flags);
  514. }
  515. /**
  516. * amdgpu_unregister_atpx_handler - unregister with vga_switcheroo
  517. *
  518. * Unregister the PX callbacks with vga_switcheroo (all asics).
  519. */
  520. void amdgpu_unregister_atpx_handler(void)
  521. {
  522. vga_switcheroo_unregister_handler();
  523. }