amdgpu_atpx_handler.c 14 KB

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