amdgpu_atpx_handler.c 15 KB

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