amdgpu_acpi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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. * Returns NOTIFY code
  322. */
  323. static int amdgpu_atif_handler(struct amdgpu_device *adev,
  324. struct acpi_bus_event *event)
  325. {
  326. struct amdgpu_atif *atif = adev->atif;
  327. int count;
  328. DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
  329. event->device_class, event->type);
  330. if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
  331. return NOTIFY_DONE;
  332. if (!atif ||
  333. !atif->notification_cfg.enabled ||
  334. event->type != atif->notification_cfg.command_code)
  335. /* Not our event */
  336. return NOTIFY_DONE;
  337. if (atif->functions.sbios_requests) {
  338. struct atif_sbios_requests req;
  339. /* Check pending SBIOS requests */
  340. count = amdgpu_atif_get_sbios_requests(atif, &req);
  341. if (count <= 0)
  342. return NOTIFY_DONE;
  343. DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
  344. /* todo: add DC handling */
  345. if ((req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) &&
  346. !amdgpu_device_has_dc_support(adev)) {
  347. struct amdgpu_encoder *enc = atif->encoder_for_bl;
  348. if (enc) {
  349. struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
  350. DRM_DEBUG_DRIVER("Changing brightness to %d\n",
  351. req.backlight_level);
  352. amdgpu_display_backlight_set_level(adev, enc, req.backlight_level);
  353. #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
  354. backlight_force_update(dig->bl_dev,
  355. BACKLIGHT_UPDATE_HOTKEY);
  356. #endif
  357. }
  358. }
  359. if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
  360. if ((adev->flags & AMD_IS_PX) &&
  361. amdgpu_atpx_dgpu_req_power_for_displays()) {
  362. pm_runtime_get_sync(adev->ddev->dev);
  363. /* Just fire off a uevent and let userspace tell us what to do */
  364. drm_helper_hpd_irq_event(adev->ddev);
  365. pm_runtime_mark_last_busy(adev->ddev->dev);
  366. pm_runtime_put_autosuspend(adev->ddev->dev);
  367. }
  368. }
  369. /* TODO: check other events */
  370. }
  371. /* We've handled the event, stop the notifier chain. The ACPI interface
  372. * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
  373. * userspace if the event was generated only to signal a SBIOS
  374. * request.
  375. */
  376. return NOTIFY_BAD;
  377. }
  378. /* Call the ATCS method
  379. */
  380. /**
  381. * amdgpu_atcs_call - call an ATCS method
  382. *
  383. * @handle: acpi handle
  384. * @function: the ATCS function to execute
  385. * @params: ATCS function params
  386. *
  387. * Executes the requested ATCS function (all asics).
  388. * Returns a pointer to the acpi output buffer.
  389. */
  390. static union acpi_object *amdgpu_atcs_call(acpi_handle handle, int function,
  391. struct acpi_buffer *params)
  392. {
  393. acpi_status status;
  394. union acpi_object atcs_arg_elements[2];
  395. struct acpi_object_list atcs_arg;
  396. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  397. atcs_arg.count = 2;
  398. atcs_arg.pointer = &atcs_arg_elements[0];
  399. atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
  400. atcs_arg_elements[0].integer.value = function;
  401. if (params) {
  402. atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
  403. atcs_arg_elements[1].buffer.length = params->length;
  404. atcs_arg_elements[1].buffer.pointer = params->pointer;
  405. } else {
  406. /* We need a second fake parameter */
  407. atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
  408. atcs_arg_elements[1].integer.value = 0;
  409. }
  410. status = acpi_evaluate_object(handle, "ATCS", &atcs_arg, &buffer);
  411. /* Fail only if calling the method fails and ATIF is supported */
  412. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  413. DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
  414. acpi_format_exception(status));
  415. kfree(buffer.pointer);
  416. return NULL;
  417. }
  418. return buffer.pointer;
  419. }
  420. /**
  421. * amdgpu_atcs_parse_functions - parse supported functions
  422. *
  423. * @f: supported functions struct
  424. * @mask: supported functions mask from ATCS
  425. *
  426. * Use the supported functions mask from ATCS function
  427. * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
  428. * are supported (all asics).
  429. */
  430. static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
  431. {
  432. f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
  433. f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
  434. f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
  435. f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
  436. }
  437. /**
  438. * amdgpu_atcs_verify_interface - verify ATCS
  439. *
  440. * @handle: acpi handle
  441. * @atcs: amdgpu atcs struct
  442. *
  443. * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
  444. * to initialize ATCS and determine what features are supported
  445. * (all asics).
  446. * returns 0 on success, error on failure.
  447. */
  448. static int amdgpu_atcs_verify_interface(acpi_handle handle,
  449. struct amdgpu_atcs *atcs)
  450. {
  451. union acpi_object *info;
  452. struct atcs_verify_interface output;
  453. size_t size;
  454. int err = 0;
  455. info = amdgpu_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
  456. if (!info)
  457. return -EIO;
  458. memset(&output, 0, sizeof(output));
  459. size = *(u16 *) info->buffer.pointer;
  460. if (size < 8) {
  461. DRM_INFO("ATCS buffer is too small: %zu\n", size);
  462. err = -EINVAL;
  463. goto out;
  464. }
  465. size = min(sizeof(output), size);
  466. memcpy(&output, info->buffer.pointer, size);
  467. /* TODO: check version? */
  468. DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
  469. amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
  470. out:
  471. kfree(info);
  472. return err;
  473. }
  474. /**
  475. * amdgpu_acpi_is_pcie_performance_request_supported
  476. *
  477. * @adev: amdgpu_device pointer
  478. *
  479. * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
  480. * are supported (all asics).
  481. * returns true if supported, false if not.
  482. */
  483. bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
  484. {
  485. struct amdgpu_atcs *atcs = &adev->atcs;
  486. if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
  487. return true;
  488. return false;
  489. }
  490. /**
  491. * amdgpu_acpi_pcie_notify_device_ready
  492. *
  493. * @adev: amdgpu_device pointer
  494. *
  495. * Executes the PCIE_DEVICE_READY_NOTIFICATION method
  496. * (all asics).
  497. * returns 0 on success, error on failure.
  498. */
  499. int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
  500. {
  501. acpi_handle handle;
  502. union acpi_object *info;
  503. struct amdgpu_atcs *atcs = &adev->atcs;
  504. /* Get the device handle */
  505. handle = ACPI_HANDLE(&adev->pdev->dev);
  506. if (!handle)
  507. return -EINVAL;
  508. if (!atcs->functions.pcie_dev_rdy)
  509. return -EINVAL;
  510. info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
  511. if (!info)
  512. return -EIO;
  513. kfree(info);
  514. return 0;
  515. }
  516. /**
  517. * amdgpu_acpi_pcie_performance_request
  518. *
  519. * @adev: amdgpu_device pointer
  520. * @perf_req: requested perf level (pcie gen speed)
  521. * @advertise: set advertise caps flag if set
  522. *
  523. * Executes the PCIE_PERFORMANCE_REQUEST method to
  524. * change the pcie gen speed (all asics).
  525. * returns 0 on success, error on failure.
  526. */
  527. int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
  528. u8 perf_req, bool advertise)
  529. {
  530. acpi_handle handle;
  531. union acpi_object *info;
  532. struct amdgpu_atcs *atcs = &adev->atcs;
  533. struct atcs_pref_req_input atcs_input;
  534. struct atcs_pref_req_output atcs_output;
  535. struct acpi_buffer params;
  536. size_t size;
  537. u32 retry = 3;
  538. if (amdgpu_acpi_pcie_notify_device_ready(adev))
  539. return -EINVAL;
  540. /* Get the device handle */
  541. handle = ACPI_HANDLE(&adev->pdev->dev);
  542. if (!handle)
  543. return -EINVAL;
  544. if (!atcs->functions.pcie_perf_req)
  545. return -EINVAL;
  546. atcs_input.size = sizeof(struct atcs_pref_req_input);
  547. /* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
  548. atcs_input.client_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
  549. atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
  550. atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
  551. if (advertise)
  552. atcs_input.flags |= ATCS_ADVERTISE_CAPS;
  553. atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
  554. atcs_input.perf_req = perf_req;
  555. params.length = sizeof(struct atcs_pref_req_input);
  556. params.pointer = &atcs_input;
  557. while (retry--) {
  558. info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, &params);
  559. if (!info)
  560. return -EIO;
  561. memset(&atcs_output, 0, sizeof(atcs_output));
  562. size = *(u16 *) info->buffer.pointer;
  563. if (size < 3) {
  564. DRM_INFO("ATCS buffer is too small: %zu\n", size);
  565. kfree(info);
  566. return -EINVAL;
  567. }
  568. size = min(sizeof(atcs_output), size);
  569. memcpy(&atcs_output, info->buffer.pointer, size);
  570. kfree(info);
  571. switch (atcs_output.ret_val) {
  572. case ATCS_REQUEST_REFUSED:
  573. default:
  574. return -EINVAL;
  575. case ATCS_REQUEST_COMPLETE:
  576. return 0;
  577. case ATCS_REQUEST_IN_PROGRESS:
  578. udelay(10);
  579. break;
  580. }
  581. }
  582. return 0;
  583. }
  584. /**
  585. * amdgpu_acpi_event - handle notify events
  586. *
  587. * @nb: notifier block
  588. * @val: val
  589. * @data: acpi event
  590. *
  591. * Calls relevant amdgpu functions in response to various
  592. * acpi events.
  593. * Returns NOTIFY code
  594. */
  595. static int amdgpu_acpi_event(struct notifier_block *nb,
  596. unsigned long val,
  597. void *data)
  598. {
  599. struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
  600. struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
  601. if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
  602. if (power_supply_is_system_supplied() > 0)
  603. DRM_DEBUG_DRIVER("pm: AC\n");
  604. else
  605. DRM_DEBUG_DRIVER("pm: DC\n");
  606. amdgpu_pm_acpi_event_handler(adev);
  607. }
  608. /* Check for pending SBIOS requests */
  609. return amdgpu_atif_handler(adev, entry);
  610. }
  611. /* Call all ACPI methods here */
  612. /**
  613. * amdgpu_acpi_init - init driver acpi support
  614. *
  615. * @adev: amdgpu_device pointer
  616. *
  617. * Verifies the AMD ACPI interfaces and registers with the acpi
  618. * notifier chain (all asics).
  619. * Returns 0 on success, error on failure.
  620. */
  621. int amdgpu_acpi_init(struct amdgpu_device *adev)
  622. {
  623. acpi_handle handle, atif_handle;
  624. struct amdgpu_atif *atif;
  625. struct amdgpu_atcs *atcs = &adev->atcs;
  626. int ret;
  627. /* Get the device handle */
  628. handle = ACPI_HANDLE(&adev->pdev->dev);
  629. if (!adev->bios || !handle)
  630. return 0;
  631. /* Call the ATCS method */
  632. ret = amdgpu_atcs_verify_interface(handle, atcs);
  633. if (ret) {
  634. DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
  635. }
  636. /* Probe for ATIF, and initialize it if found */
  637. atif_handle = amdgpu_atif_probe_handle(handle);
  638. if (!atif_handle)
  639. goto out;
  640. atif = kzalloc(sizeof(*atif), GFP_KERNEL);
  641. if (!atif) {
  642. DRM_WARN("Not enough memory to initialize ATIF\n");
  643. goto out;
  644. }
  645. atif->handle = atif_handle;
  646. /* Call the ATIF method */
  647. ret = amdgpu_atif_verify_interface(atif);
  648. if (ret) {
  649. DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
  650. kfree(atif);
  651. goto out;
  652. }
  653. adev->atif = atif;
  654. if (atif->notifications.brightness_change) {
  655. struct drm_encoder *tmp;
  656. /* Find the encoder controlling the brightness */
  657. list_for_each_entry(tmp, &adev->ddev->mode_config.encoder_list,
  658. head) {
  659. struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
  660. if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
  661. enc->enc_priv) {
  662. struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
  663. if (dig->bl_dev) {
  664. atif->encoder_for_bl = enc;
  665. break;
  666. }
  667. }
  668. }
  669. }
  670. if (atif->functions.sbios_requests && !atif->functions.system_params) {
  671. /* XXX check this workraround, if sbios request function is
  672. * present we have to see how it's configured in the system
  673. * params
  674. */
  675. atif->functions.system_params = true;
  676. }
  677. if (atif->functions.system_params) {
  678. ret = amdgpu_atif_get_notification_params(atif);
  679. if (ret) {
  680. DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
  681. ret);
  682. /* Disable notification */
  683. atif->notification_cfg.enabled = false;
  684. }
  685. }
  686. out:
  687. adev->acpi_nb.notifier_call = amdgpu_acpi_event;
  688. register_acpi_notifier(&adev->acpi_nb);
  689. return ret;
  690. }
  691. /**
  692. * amdgpu_acpi_fini - tear down driver acpi support
  693. *
  694. * @adev: amdgpu_device pointer
  695. *
  696. * Unregisters with the acpi notifier chain (all asics).
  697. */
  698. void amdgpu_acpi_fini(struct amdgpu_device *adev)
  699. {
  700. unregister_acpi_notifier(&adev->acpi_nb);
  701. if (adev->atif)
  702. kfree(adev->atif);
  703. }