radeon_atpx_handler.c 15 KB

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