amdgpu_atpx_handler.c 14 KB

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