amdgpu_acpi.c 22 KB

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