amdgpu_acpi.c 19 KB

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