amdgpu_acpi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /*
  2. * Copyright 2012 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. */
  23. #include <linux/pci.h>
  24. #include <linux/acpi.h>
  25. #include <linux/slab.h>
  26. #include <linux/power_supply.h>
  27. #include <linux/pm_runtime.h>
  28. #include <acpi/video.h>
  29. #include <drm/drmP.h>
  30. #include <drm/drm_crtc_helper.h>
  31. #include "amdgpu.h"
  32. #include "amdgpu_pm.h"
  33. #include "amd_acpi.h"
  34. #include "atom.h"
  35. struct amdgpu_atif_notification_cfg {
  36. bool enabled;
  37. int command_code;
  38. };
  39. struct amdgpu_atif_notifications {
  40. bool display_switch;
  41. bool expansion_mode_change;
  42. bool thermal_state;
  43. bool forced_power_state;
  44. bool system_power_state;
  45. bool display_conf_change;
  46. bool px_gfx_switch;
  47. bool brightness_change;
  48. bool dgpu_display_event;
  49. };
  50. struct amdgpu_atif_functions {
  51. bool system_params;
  52. bool sbios_requests;
  53. bool select_active_disp;
  54. bool lid_state;
  55. bool get_tv_standard;
  56. bool set_tv_standard;
  57. bool get_panel_expansion_mode;
  58. bool set_panel_expansion_mode;
  59. bool temperature_change;
  60. bool graphics_device_types;
  61. };
  62. struct amdgpu_atif {
  63. struct amdgpu_atif_notifications notifications;
  64. struct amdgpu_atif_functions functions;
  65. struct amdgpu_atif_notification_cfg notification_cfg;
  66. struct amdgpu_encoder *encoder_for_bl;
  67. };
  68. /* Call the ATIF method
  69. */
  70. /**
  71. * amdgpu_atif_call - call an ATIF method
  72. *
  73. * @handle: acpi handle
  74. * @function: the ATIF function to execute
  75. * @params: ATIF function params
  76. *
  77. * Executes the requested ATIF function (all asics).
  78. * Returns a pointer to the acpi output buffer.
  79. */
  80. static union acpi_object *amdgpu_atif_call(acpi_handle handle, int function,
  81. struct acpi_buffer *params)
  82. {
  83. acpi_status status;
  84. union acpi_object atif_arg_elements[2];
  85. struct acpi_object_list atif_arg;
  86. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  87. atif_arg.count = 2;
  88. atif_arg.pointer = &atif_arg_elements[0];
  89. atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
  90. atif_arg_elements[0].integer.value = function;
  91. if (params) {
  92. atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
  93. atif_arg_elements[1].buffer.length = params->length;
  94. atif_arg_elements[1].buffer.pointer = params->pointer;
  95. } else {
  96. /* We need a second fake parameter */
  97. atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
  98. atif_arg_elements[1].integer.value = 0;
  99. }
  100. status = acpi_evaluate_object(handle, "ATIF", &atif_arg, &buffer);
  101. /* Fail only if calling the method fails and ATIF is supported */
  102. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  103. DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
  104. acpi_format_exception(status));
  105. kfree(buffer.pointer);
  106. return NULL;
  107. }
  108. return buffer.pointer;
  109. }
  110. /**
  111. * amdgpu_atif_parse_notification - parse supported notifications
  112. *
  113. * @n: supported notifications struct
  114. * @mask: supported notifications mask from ATIF
  115. *
  116. * Use the supported notifications mask from ATIF function
  117. * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
  118. * are supported (all asics).
  119. */
  120. static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
  121. {
  122. n->display_switch = mask & ATIF_DISPLAY_SWITCH_REQUEST_SUPPORTED;
  123. n->expansion_mode_change = mask & ATIF_EXPANSION_MODE_CHANGE_REQUEST_SUPPORTED;
  124. n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
  125. n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
  126. n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
  127. n->display_conf_change = mask & ATIF_DISPLAY_CONF_CHANGE_REQUEST_SUPPORTED;
  128. n->px_gfx_switch = mask & ATIF_PX_GFX_SWITCH_REQUEST_SUPPORTED;
  129. n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
  130. n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
  131. }
  132. /**
  133. * amdgpu_atif_parse_functions - parse supported functions
  134. *
  135. * @f: supported functions struct
  136. * @mask: supported functions mask from ATIF
  137. *
  138. * Use the supported functions mask from ATIF function
  139. * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
  140. * are supported (all asics).
  141. */
  142. static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
  143. {
  144. f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
  145. f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
  146. f->select_active_disp = mask & ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED;
  147. f->lid_state = mask & ATIF_GET_LID_STATE_SUPPORTED;
  148. f->get_tv_standard = mask & ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED;
  149. f->set_tv_standard = mask & ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED;
  150. f->get_panel_expansion_mode = mask & ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED;
  151. f->set_panel_expansion_mode = mask & ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED;
  152. f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
  153. f->graphics_device_types = mask & ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED;
  154. }
  155. /**
  156. * amdgpu_atif_verify_interface - verify ATIF
  157. *
  158. * @handle: acpi handle
  159. * @atif: amdgpu atif struct
  160. *
  161. * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
  162. * to initialize ATIF and determine what features are supported
  163. * (all asics).
  164. * returns 0 on success, error on failure.
  165. */
  166. static int amdgpu_atif_verify_interface(acpi_handle handle,
  167. struct amdgpu_atif *atif)
  168. {
  169. union acpi_object *info;
  170. struct atif_verify_interface output;
  171. size_t size;
  172. int err = 0;
  173. info = amdgpu_atif_call(handle, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
  174. if (!info)
  175. return -EIO;
  176. memset(&output, 0, sizeof(output));
  177. size = *(u16 *) info->buffer.pointer;
  178. if (size < 12) {
  179. DRM_INFO("ATIF buffer is too small: %zu\n", size);
  180. err = -EINVAL;
  181. goto out;
  182. }
  183. size = min(sizeof(output), size);
  184. memcpy(&output, info->buffer.pointer, size);
  185. /* TODO: check version? */
  186. DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
  187. amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
  188. amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
  189. out:
  190. kfree(info);
  191. return err;
  192. }
  193. /**
  194. * amdgpu_atif_get_notification_params - determine notify configuration
  195. *
  196. * @handle: acpi handle
  197. * @n: atif notification configuration struct
  198. *
  199. * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
  200. * to determine if a notifier is used and if so which one
  201. * (all asics). This is either Notify(VGA, 0x81) or Notify(VGA, n)
  202. * where n is specified in the result if a notifier is used.
  203. * Returns 0 on success, error on failure.
  204. */
  205. static int amdgpu_atif_get_notification_params(acpi_handle handle,
  206. struct amdgpu_atif_notification_cfg *n)
  207. {
  208. union acpi_object *info;
  209. struct atif_system_params params;
  210. size_t size;
  211. int err = 0;
  212. info = amdgpu_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS, NULL);
  213. if (!info) {
  214. err = -EIO;
  215. goto out;
  216. }
  217. size = *(u16 *) info->buffer.pointer;
  218. if (size < 10) {
  219. err = -EINVAL;
  220. goto out;
  221. }
  222. memset(&params, 0, sizeof(params));
  223. size = min(sizeof(params), size);
  224. memcpy(&params, info->buffer.pointer, size);
  225. DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
  226. params.flags, params.valid_mask);
  227. params.flags = params.flags & params.valid_mask;
  228. if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
  229. n->enabled = false;
  230. n->command_code = 0;
  231. } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
  232. n->enabled = true;
  233. n->command_code = 0x81;
  234. } else {
  235. if (size < 11) {
  236. err = -EINVAL;
  237. goto out;
  238. }
  239. n->enabled = true;
  240. n->command_code = params.command_code;
  241. }
  242. out:
  243. DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
  244. (n->enabled ? "enabled" : "disabled"),
  245. n->command_code);
  246. kfree(info);
  247. return err;
  248. }
  249. /**
  250. * amdgpu_atif_get_sbios_requests - get requested sbios event
  251. *
  252. * @handle: acpi handle
  253. * @req: atif sbios request struct
  254. *
  255. * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
  256. * to determine what requests the sbios is making to the driver
  257. * (all asics).
  258. * Returns 0 on success, error on failure.
  259. */
  260. static int amdgpu_atif_get_sbios_requests(acpi_handle handle,
  261. struct atif_sbios_requests *req)
  262. {
  263. union acpi_object *info;
  264. size_t size;
  265. int count = 0;
  266. info = amdgpu_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS, NULL);
  267. if (!info)
  268. return -EIO;
  269. size = *(u16 *)info->buffer.pointer;
  270. if (size < 0xd) {
  271. count = -EINVAL;
  272. goto out;
  273. }
  274. memset(req, 0, sizeof(*req));
  275. size = min(sizeof(*req), size);
  276. memcpy(req, info->buffer.pointer, size);
  277. DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
  278. count = hweight32(req->pending);
  279. out:
  280. kfree(info);
  281. return count;
  282. }
  283. /**
  284. * amdgpu_atif_handler - handle ATIF notify requests
  285. *
  286. * @adev: amdgpu_device pointer
  287. * @event: atif sbios request struct
  288. *
  289. * Checks the acpi event and if it matches an atif event,
  290. * handles it.
  291. * Returns NOTIFY code
  292. */
  293. static int amdgpu_atif_handler(struct amdgpu_device *adev,
  294. struct acpi_bus_event *event)
  295. {
  296. struct amdgpu_atif *atif = adev->atif;
  297. struct atif_sbios_requests req;
  298. acpi_handle handle;
  299. int count;
  300. DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
  301. event->device_class, event->type);
  302. if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
  303. return NOTIFY_DONE;
  304. if (!atif ||
  305. !atif->notification_cfg.enabled ||
  306. event->type != atif->notification_cfg.command_code)
  307. /* Not our event */
  308. return NOTIFY_DONE;
  309. /* Check pending SBIOS requests */
  310. handle = ACPI_HANDLE(&adev->pdev->dev);
  311. count = amdgpu_atif_get_sbios_requests(handle, &req);
  312. if (count <= 0)
  313. return NOTIFY_DONE;
  314. DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
  315. if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
  316. struct amdgpu_encoder *enc = atif->encoder_for_bl;
  317. if (enc) {
  318. struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
  319. DRM_DEBUG_DRIVER("Changing brightness to %d\n",
  320. req.backlight_level);
  321. amdgpu_display_backlight_set_level(adev, enc, req.backlight_level);
  322. #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
  323. backlight_force_update(dig->bl_dev,
  324. BACKLIGHT_UPDATE_HOTKEY);
  325. #endif
  326. }
  327. }
  328. if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
  329. if ((adev->flags & AMD_IS_PX) &&
  330. amdgpu_atpx_dgpu_req_power_for_displays()) {
  331. pm_runtime_get_sync(adev->ddev->dev);
  332. /* Just fire off a uevent and let userspace tell us what to do */
  333. drm_helper_hpd_irq_event(adev->ddev);
  334. pm_runtime_mark_last_busy(adev->ddev->dev);
  335. pm_runtime_put_autosuspend(adev->ddev->dev);
  336. }
  337. }
  338. /* TODO: check other events */
  339. /* We've handled the event, stop the notifier chain. The ACPI interface
  340. * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
  341. * userspace if the event was generated only to signal a SBIOS
  342. * request.
  343. */
  344. return NOTIFY_BAD;
  345. }
  346. /* Call the ATCS method
  347. */
  348. /**
  349. * amdgpu_atcs_call - call an ATCS method
  350. *
  351. * @handle: acpi handle
  352. * @function: the ATCS function to execute
  353. * @params: ATCS function params
  354. *
  355. * Executes the requested ATCS function (all asics).
  356. * Returns a pointer to the acpi output buffer.
  357. */
  358. static union acpi_object *amdgpu_atcs_call(acpi_handle handle, int function,
  359. struct acpi_buffer *params)
  360. {
  361. acpi_status status;
  362. union acpi_object atcs_arg_elements[2];
  363. struct acpi_object_list atcs_arg;
  364. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  365. atcs_arg.count = 2;
  366. atcs_arg.pointer = &atcs_arg_elements[0];
  367. atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
  368. atcs_arg_elements[0].integer.value = function;
  369. if (params) {
  370. atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
  371. atcs_arg_elements[1].buffer.length = params->length;
  372. atcs_arg_elements[1].buffer.pointer = params->pointer;
  373. } else {
  374. /* We need a second fake parameter */
  375. atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
  376. atcs_arg_elements[1].integer.value = 0;
  377. }
  378. status = acpi_evaluate_object(handle, "ATCS", &atcs_arg, &buffer);
  379. /* Fail only if calling the method fails and ATIF is supported */
  380. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  381. DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
  382. acpi_format_exception(status));
  383. kfree(buffer.pointer);
  384. return NULL;
  385. }
  386. return buffer.pointer;
  387. }
  388. /**
  389. * amdgpu_atcs_parse_functions - parse supported functions
  390. *
  391. * @f: supported functions struct
  392. * @mask: supported functions mask from ATCS
  393. *
  394. * Use the supported functions mask from ATCS function
  395. * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
  396. * are supported (all asics).
  397. */
  398. static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
  399. {
  400. f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
  401. f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
  402. f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
  403. f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
  404. }
  405. /**
  406. * amdgpu_atcs_verify_interface - verify ATCS
  407. *
  408. * @handle: acpi handle
  409. * @atcs: amdgpu atcs struct
  410. *
  411. * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
  412. * to initialize ATCS and determine what features are supported
  413. * (all asics).
  414. * returns 0 on success, error on failure.
  415. */
  416. static int amdgpu_atcs_verify_interface(acpi_handle handle,
  417. struct amdgpu_atcs *atcs)
  418. {
  419. union acpi_object *info;
  420. struct atcs_verify_interface output;
  421. size_t size;
  422. int err = 0;
  423. info = amdgpu_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
  424. if (!info)
  425. return -EIO;
  426. memset(&output, 0, sizeof(output));
  427. size = *(u16 *) info->buffer.pointer;
  428. if (size < 8) {
  429. DRM_INFO("ATCS buffer is too small: %zu\n", size);
  430. err = -EINVAL;
  431. goto out;
  432. }
  433. size = min(sizeof(output), size);
  434. memcpy(&output, info->buffer.pointer, size);
  435. /* TODO: check version? */
  436. DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
  437. amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
  438. out:
  439. kfree(info);
  440. return err;
  441. }
  442. /**
  443. * amdgpu_acpi_is_pcie_performance_request_supported
  444. *
  445. * @adev: amdgpu_device pointer
  446. *
  447. * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
  448. * are supported (all asics).
  449. * returns true if supported, false if not.
  450. */
  451. bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
  452. {
  453. struct amdgpu_atcs *atcs = &adev->atcs;
  454. if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
  455. return true;
  456. return false;
  457. }
  458. /**
  459. * amdgpu_acpi_pcie_notify_device_ready
  460. *
  461. * @adev: amdgpu_device pointer
  462. *
  463. * Executes the PCIE_DEVICE_READY_NOTIFICATION method
  464. * (all asics).
  465. * returns 0 on success, error on failure.
  466. */
  467. int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
  468. {
  469. acpi_handle handle;
  470. union acpi_object *info;
  471. struct amdgpu_atcs *atcs = &adev->atcs;
  472. /* Get the device handle */
  473. handle = ACPI_HANDLE(&adev->pdev->dev);
  474. if (!handle)
  475. return -EINVAL;
  476. if (!atcs->functions.pcie_dev_rdy)
  477. return -EINVAL;
  478. info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
  479. if (!info)
  480. return -EIO;
  481. kfree(info);
  482. return 0;
  483. }
  484. /**
  485. * amdgpu_acpi_pcie_performance_request
  486. *
  487. * @adev: amdgpu_device pointer
  488. * @perf_req: requested perf level (pcie gen speed)
  489. * @advertise: set advertise caps flag if set
  490. *
  491. * Executes the PCIE_PERFORMANCE_REQUEST method to
  492. * change the pcie gen speed (all asics).
  493. * returns 0 on success, error on failure.
  494. */
  495. int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
  496. u8 perf_req, bool advertise)
  497. {
  498. acpi_handle handle;
  499. union acpi_object *info;
  500. struct amdgpu_atcs *atcs = &adev->atcs;
  501. struct atcs_pref_req_input atcs_input;
  502. struct atcs_pref_req_output atcs_output;
  503. struct acpi_buffer params;
  504. size_t size;
  505. u32 retry = 3;
  506. if (amdgpu_acpi_pcie_notify_device_ready(adev))
  507. return -EINVAL;
  508. /* Get the device handle */
  509. handle = ACPI_HANDLE(&adev->pdev->dev);
  510. if (!handle)
  511. return -EINVAL;
  512. if (!atcs->functions.pcie_perf_req)
  513. return -EINVAL;
  514. atcs_input.size = sizeof(struct atcs_pref_req_input);
  515. /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
  516. atcs_input.client_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
  517. atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
  518. atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
  519. if (advertise)
  520. atcs_input.flags |= ATCS_ADVERTISE_CAPS;
  521. atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
  522. atcs_input.perf_req = perf_req;
  523. params.length = sizeof(struct atcs_pref_req_input);
  524. params.pointer = &atcs_input;
  525. while (retry--) {
  526. info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, &params);
  527. if (!info)
  528. return -EIO;
  529. memset(&atcs_output, 0, sizeof(atcs_output));
  530. size = *(u16 *) info->buffer.pointer;
  531. if (size < 3) {
  532. DRM_INFO("ATCS buffer is too small: %zu\n", size);
  533. kfree(info);
  534. return -EINVAL;
  535. }
  536. size = min(sizeof(atcs_output), size);
  537. memcpy(&atcs_output, info->buffer.pointer, size);
  538. kfree(info);
  539. switch (atcs_output.ret_val) {
  540. case ATCS_REQUEST_REFUSED:
  541. default:
  542. return -EINVAL;
  543. case ATCS_REQUEST_COMPLETE:
  544. return 0;
  545. case ATCS_REQUEST_IN_PROGRESS:
  546. udelay(10);
  547. break;
  548. }
  549. }
  550. return 0;
  551. }
  552. /**
  553. * amdgpu_acpi_event - handle notify events
  554. *
  555. * @nb: notifier block
  556. * @val: val
  557. * @data: acpi event
  558. *
  559. * Calls relevant amdgpu functions in response to various
  560. * acpi events.
  561. * Returns NOTIFY code
  562. */
  563. static int amdgpu_acpi_event(struct notifier_block *nb,
  564. unsigned long val,
  565. void *data)
  566. {
  567. struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
  568. struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
  569. if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
  570. if (power_supply_is_system_supplied() > 0)
  571. DRM_DEBUG_DRIVER("pm: AC\n");
  572. else
  573. DRM_DEBUG_DRIVER("pm: DC\n");
  574. amdgpu_pm_acpi_event_handler(adev);
  575. }
  576. /* Check for pending SBIOS requests */
  577. return amdgpu_atif_handler(adev, entry);
  578. }
  579. /* Call all ACPI methods here */
  580. /**
  581. * amdgpu_acpi_init - init driver acpi support
  582. *
  583. * @adev: amdgpu_device pointer
  584. *
  585. * Verifies the AMD ACPI interfaces and registers with the acpi
  586. * notifier chain (all asics).
  587. * Returns 0 on success, error on failure.
  588. */
  589. int amdgpu_acpi_init(struct amdgpu_device *adev)
  590. {
  591. acpi_handle handle;
  592. struct amdgpu_atif *atif;
  593. struct amdgpu_atcs *atcs = &adev->atcs;
  594. int ret;
  595. /* Get the device handle */
  596. handle = ACPI_HANDLE(&adev->pdev->dev);
  597. if (!adev->bios || !handle)
  598. return 0;
  599. /* Call the ATCS method */
  600. ret = amdgpu_atcs_verify_interface(handle, atcs);
  601. if (ret) {
  602. DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
  603. }
  604. /* Call the ATIF method */
  605. atif = kzalloc(sizeof(*atif), GFP_KERNEL);
  606. if (!atif) {
  607. DRM_WARN("Not enough memory to initialize ATIF\n");
  608. goto out;
  609. }
  610. ret = amdgpu_atif_verify_interface(handle, atif);
  611. if (ret) {
  612. DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
  613. kfree(atif);
  614. goto out;
  615. }
  616. adev->atif = atif;
  617. if (atif->notifications.brightness_change) {
  618. struct drm_encoder *tmp;
  619. /* Find the encoder controlling the brightness */
  620. list_for_each_entry(tmp, &adev->ddev->mode_config.encoder_list,
  621. head) {
  622. struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
  623. if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
  624. enc->enc_priv) {
  625. struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
  626. if (dig->bl_dev) {
  627. atif->encoder_for_bl = enc;
  628. break;
  629. }
  630. }
  631. }
  632. }
  633. if (atif->functions.sbios_requests && !atif->functions.system_params) {
  634. /* XXX check this workraround, if sbios request function is
  635. * present we have to see how it's configured in the system
  636. * params
  637. */
  638. atif->functions.system_params = true;
  639. }
  640. if (atif->functions.system_params) {
  641. ret = amdgpu_atif_get_notification_params(handle,
  642. &atif->notification_cfg);
  643. if (ret) {
  644. DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
  645. ret);
  646. /* Disable notification */
  647. atif->notification_cfg.enabled = false;
  648. }
  649. }
  650. out:
  651. adev->acpi_nb.notifier_call = amdgpu_acpi_event;
  652. register_acpi_notifier(&adev->acpi_nb);
  653. return ret;
  654. }
  655. /**
  656. * amdgpu_acpi_fini - tear down driver acpi support
  657. *
  658. * @adev: amdgpu_device pointer
  659. *
  660. * Unregisters with the acpi notifier chain (all asics).
  661. */
  662. void amdgpu_acpi_fini(struct amdgpu_device *adev)
  663. {
  664. unregister_acpi_notifier(&adev->acpi_nb);
  665. if (adev->atif)
  666. kfree(adev->atif);
  667. }