amdgpu_acpi.c 22 KB

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