amdgpu_acpi.c 20 KB

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