radeon_atpx_handler.c 14 KB

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