2 * Copyright 2012 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
22 * $FreeBSD: head/sys/dev/drm2/radeon/radeon_acpi.c 254885 2013-08-25 19:37:15Z dumbbell $
26 #include <drm/drm_crtc_helper.h>
28 #include "radeon_acpi.h"
31 #define ACPI_AC_CLASS "ac_adapter"
33 struct atif_verify_interface {
34 u16 size; /* structure size in bytes (includes size field) */
35 u16 version; /* version */
36 u32 notification_mask; /* supported notifications mask */
37 u32 function_bits; /* supported functions bit vector */
40 struct atif_system_params {
41 u16 size; /* structure size in bytes (includes size field) */
42 u32 valid_mask; /* valid flags mask */
43 u32 flags; /* flags */
44 u8 command_code; /* notify command code */
47 struct atif_sbios_requests {
48 u16 size; /* structure size in bytes (includes size field) */
49 u32 pending; /* pending sbios requests */
50 u8 panel_exp_mode; /* panel expansion mode */
51 u8 thermal_gfx; /* thermal state: target gfx controller */
52 u8 thermal_state; /* thermal state: state id (0: exit state, non-0: state) */
53 u8 forced_power_gfx; /* forced power state: target gfx controller */
54 u8 forced_power_state; /* forced power state: state id */
55 u8 system_power_src; /* system power source */
56 u8 backlight_level; /* panel backlight level (0-255) */
59 #define ATIF_NOTIFY_MASK 0x3
60 #define ATIF_NOTIFY_NONE 0
61 #define ATIF_NOTIFY_81 1
62 #define ATIF_NOTIFY_N 2
64 struct atcs_verify_interface {
65 u16 size; /* structure size in bytes (includes size field) */
66 u16 version; /* version */
67 u32 function_bits; /* supported functions bit vector */
70 /* Call the ATIF method
73 * radeon_atif_call - call an ATIF method
75 * @handle: acpi handle
76 * @function: the ATIF function to execute
77 * @params: ATIF function params
79 * Executes the requested ATIF function (all asics).
80 * Returns a pointer to the acpi output buffer.
82 static ACPI_OBJECT *radeon_atif_call(ACPI_HANDLE handle, int function,
86 ACPI_OBJECT atif_arg_elements[2];
87 ACPI_OBJECT_LIST atif_arg;
88 ACPI_BUFFER buffer = { ACPI_ALLOCATE_BUFFER, NULL };
91 atif_arg.Pointer = &atif_arg_elements[0];
93 atif_arg_elements[0].Type = ACPI_TYPE_INTEGER;
94 atif_arg_elements[0].Integer.Value = function;
97 atif_arg_elements[1].Type = ACPI_TYPE_BUFFER;
98 atif_arg_elements[1].Buffer.Length = params->Length;
99 atif_arg_elements[1].Buffer.Pointer = params->Pointer;
101 /* We need a second fake parameter */
102 atif_arg_elements[1].Type = ACPI_TYPE_INTEGER;
103 atif_arg_elements[1].Integer.Value = 0;
106 status = AcpiEvaluateObject(handle, "ATIF", &atif_arg, &buffer);
108 /* Fail only if calling the method fails and ATIF is supported */
109 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
110 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
111 AcpiFormatException(status));
112 AcpiOsFree(buffer.Pointer);
116 return buffer.Pointer;
120 * radeon_atif_parse_notification - parse supported notifications
122 * @n: supported notifications struct
123 * @mask: supported notifications mask from ATIF
125 * Use the supported notifications mask from ATIF function
126 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
127 * are supported (all asics).
129 static void radeon_atif_parse_notification(struct radeon_atif_notifications *n, u32 mask)
131 n->display_switch = mask & ATIF_DISPLAY_SWITCH_REQUEST_SUPPORTED;
132 n->expansion_mode_change = mask & ATIF_EXPANSION_MODE_CHANGE_REQUEST_SUPPORTED;
133 n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
134 n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
135 n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
136 n->display_conf_change = mask & ATIF_DISPLAY_CONF_CHANGE_REQUEST_SUPPORTED;
137 n->px_gfx_switch = mask & ATIF_PX_GFX_SWITCH_REQUEST_SUPPORTED;
138 n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
139 n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
143 * radeon_atif_parse_functions - parse supported functions
145 * @f: supported functions struct
146 * @mask: supported functions mask from ATIF
148 * Use the supported functions mask from ATIF function
149 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
150 * are supported (all asics).
152 static void radeon_atif_parse_functions(struct radeon_atif_functions *f, u32 mask)
154 f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
155 f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
156 f->select_active_disp = mask & ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED;
157 f->lid_state = mask & ATIF_GET_LID_STATE_SUPPORTED;
158 f->get_tv_standard = mask & ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED;
159 f->set_tv_standard = mask & ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED;
160 f->get_panel_expansion_mode = mask & ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED;
161 f->set_panel_expansion_mode = mask & ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED;
162 f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
163 f->graphics_device_types = mask & ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED;
167 * radeon_atif_verify_interface - verify ATIF
169 * @handle: acpi handle
170 * @atif: radeon atif struct
172 * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
173 * to initialize ATIF and determine what features are supported
175 * returns 0 on success, error on failure.
177 static int radeon_atif_verify_interface(ACPI_HANDLE handle,
178 struct radeon_atif *atif)
181 struct atif_verify_interface output;
185 info = radeon_atif_call(handle, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
189 memset(&output, 0, sizeof(output));
191 size = *(u16 *) info->Buffer.Pointer;
193 DRM_INFO("ATIF buffer is too small: %zu\n", size);
197 size = min(sizeof(output), size);
199 memcpy(&output, info->Buffer.Pointer, size);
201 /* TODO: check version? */
202 DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
204 radeon_atif_parse_notification(&atif->notifications, output.notification_mask);
205 radeon_atif_parse_functions(&atif->functions, output.function_bits);
213 * radeon_atif_get_notification_params - determine notify configuration
215 * @handle: acpi handle
216 * @n: atif notification configuration struct
218 * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
219 * to determine if a notifier is used and if so which one
220 * (all asics). This is either Notify(VGA, 0x81) or Notify(VGA, n)
221 * where n is specified in the result if a notifier is used.
222 * Returns 0 on success, error on failure.
224 static int radeon_atif_get_notification_params(ACPI_HANDLE handle,
225 struct radeon_atif_notification_cfg *n)
228 struct atif_system_params params;
232 info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS, NULL);
238 size = *(u16 *) info->Buffer.Pointer;
244 memset(¶ms, 0, sizeof(params));
245 size = min(sizeof(params), size);
246 memcpy(¶ms, info->Buffer.Pointer, size);
248 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
249 params.flags, params.valid_mask);
250 params.flags = params.flags & params.valid_mask;
252 if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
255 } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
257 n->command_code = 0x81;
264 n->command_code = params.command_code;
268 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
269 (n->enabled ? "enabled" : "disabled"),
276 * radeon_atif_get_sbios_requests - get requested sbios event
278 * @handle: acpi handle
279 * @req: atif sbios request struct
281 * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
282 * to determine what requests the sbios is making to the driver
284 * Returns 0 on success, error on failure.
286 static int radeon_atif_get_sbios_requests(ACPI_HANDLE handle,
287 struct atif_sbios_requests *req)
293 info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS, NULL);
297 size = *(u16 *)info->Buffer.Pointer;
302 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);
308 count = hweight32(req->pending);
316 * radeon_atif_handler - handle ATIF notify requests
318 * @rdev: radeon_device pointer
319 * @event: atif sbios request struct
321 * Checks the acpi event and if it matches an atif event,
323 * Returns NOTIFY code
325 void radeon_atif_handler(struct radeon_device *rdev,
328 struct radeon_atif *atif = &rdev->atif;
329 struct atif_sbios_requests req;
333 DRM_DEBUG_DRIVER("event, type = %#x\n",
336 if (!atif->notification_cfg.enabled ||
337 type != atif->notification_cfg.command_code)
341 /* Check pending SBIOS requests */
342 handle = rdev->acpi.handle;
343 count = radeon_atif_get_sbios_requests(handle, &req);
348 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
350 if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
351 struct radeon_encoder *enc = atif->encoder_for_bl;
354 DRM_DEBUG_DRIVER("Changing brightness to %d\n",
355 req.backlight_level);
357 radeon_set_backlight_level(rdev, enc, req.backlight_level);
360 if (rdev->is_atom_bios) {
361 struct radeon_encoder_atom_dig *dig = enc->enc_priv;
362 backlight_force_update(dig->bl_dev,
363 BACKLIGHT_UPDATE_HOTKEY);
365 struct radeon_encoder_lvds *dig = enc->enc_priv;
366 backlight_force_update(dig->bl_dev,
367 BACKLIGHT_UPDATE_HOTKEY);
369 #endif /* DUMBBELL_WIP */
372 /* TODO: check other events */
374 /* We've handled the event, stop the notifier chain. The ACPI interface
375 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
376 * userspace if the event was generated only to signal a SBIOS
381 /* Call the ATCS method
384 * radeon_atcs_call - call an ATCS method
386 * @handle: acpi handle
387 * @function: the ATCS function to execute
388 * @params: ATCS function params
390 * Executes the requested ATCS function (all asics).
391 * Returns a pointer to the acpi output buffer.
393 static union acpi_object *radeon_atcs_call(ACPI_HANDLE handle, int function,
397 ACPI_OBJECT atcs_arg_elements[2];
398 ACPI_OBJECT_LIST atcs_arg;
399 ACPI_BUFFER buffer = { ACPI_ALLOCATE_BUFFER, NULL };
402 atcs_arg.Pointer = &atcs_arg_elements[0];
404 atcs_arg_elements[0].Type = ACPI_TYPE_INTEGER;
405 atcs_arg_elements[0].Integer.Value = function;
408 atcs_arg_elements[1].Type = ACPI_TYPE_BUFFER;
409 atcs_arg_elements[1].Buffer.Length = params->Length;
410 atcs_arg_elements[1].Buffer.Pointer = params->Pointer;
412 /* We need a second fake parameter */
413 atcs_arg_elements[1].Type = ACPI_TYPE_INTEGER;
414 atcs_arg_elements[1].Integer.Value = 0;
417 status = AcpiEvaluateObject(handle, "ATCS", &atcs_arg, &buffer);
419 /* Fail only if calling the method fails and ATIF is supported */
420 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
421 DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
422 AcpiFormatException(status));
423 AcpiOsFree(buffer.Pointer);
427 return buffer.Pointer;
431 * radeon_atcs_parse_functions - parse supported functions
433 * @f: supported functions struct
434 * @mask: supported functions mask from ATCS
436 * Use the supported functions mask from ATCS function
437 * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
438 * are supported (all asics).
440 static void radeon_atcs_parse_functions(struct radeon_atcs_functions *f, u32 mask)
442 f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
443 f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
444 f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
445 f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
449 * radeon_atcs_verify_interface - verify ATCS
451 * @handle: acpi handle
452 * @atcs: radeon atcs struct
454 * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
455 * to initialize ATCS and determine what features are supported
457 * returns 0 on success, error on failure.
459 static int radeon_atcs_verify_interface(ACPI_HANDLE handle,
460 struct radeon_atcs *atcs)
463 struct atcs_verify_interface output;
467 info = radeon_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
471 memset(&output, 0, sizeof(output));
473 size = *(u16 *) info->Buffer.Pointer;
475 DRM_INFO("ATCS buffer is too small: %zu\n", size);
479 size = min(sizeof(output), size);
481 memcpy(&output, info->Buffer.Pointer, size);
483 /* TODO: check version? */
484 DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
486 radeon_atcs_parse_functions(&atcs->functions, output.function_bits);
494 * radeon_acpi_event - handle notify events
496 * @nb: notifier block
500 * Calls relevant radeon functions in response to various
502 * Returns NOTIFY code
504 static void radeon_acpi_event(ACPI_HANDLE handle, UINT32 type,
507 struct radeon_device *rdev = (struct radeon_device *)context;
510 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
511 if (power_supply_is_system_supplied() > 0)
512 DRM_DEBUG_DRIVER("pm: AC\n");
514 DRM_DEBUG_DRIVER("pm: DC\n");
516 radeon_pm_acpi_event_handler(rdev);
518 #endif /* DUMBBELL_WIP */
520 /* Check for pending SBIOS requests */
521 radeon_atif_handler(rdev, type);
524 /* Call all ACPI methods here */
526 * radeon_acpi_init - init driver acpi support
528 * @rdev: radeon_device pointer
530 * Verifies the AMD ACPI interfaces and registers with the acpi
531 * notifier chain (all asics).
532 * Returns 0 on success, error on failure.
534 int radeon_acpi_init(struct radeon_device *rdev)
537 struct radeon_atif *atif = &rdev->atif;
538 struct radeon_atcs *atcs = &rdev->atcs;
541 /* Get the device handle */
542 handle = acpi_get_handle(rdev->dev);
544 /* No need to proceed if we're sure that ATIF is not supported */
545 if (!ASIC_IS_AVIVO(rdev) || !rdev->bios || !handle)
548 /* Call the ATCS method */
549 ret = radeon_atcs_verify_interface(handle, atcs);
551 DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
554 /* Call the ATIF method */
555 ret = radeon_atif_verify_interface(handle, atif);
557 DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
561 if (atif->notifications.brightness_change) {
562 struct drm_encoder *tmp;
563 struct radeon_encoder *target = NULL;
565 /* Find the encoder controlling the brightness */
566 list_for_each_entry(tmp, &rdev->ddev->mode_config.encoder_list,
568 struct radeon_encoder *enc = to_radeon_encoder(tmp);
570 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
572 if (rdev->is_atom_bios) {
573 struct radeon_encoder_atom_dig *dig = enc->enc_priv;
579 struct radeon_encoder_lvds *dig = enc->enc_priv;
588 atif->encoder_for_bl = target;
590 /* Brightness change notification is enabled, but we
591 * didn't find a backlight controller, this should
594 DRM_ERROR("Cannot find a backlight controller\n");
598 if (atif->functions.sbios_requests && !atif->functions.system_params) {
599 /* XXX check this workraround, if sbios request function is
600 * present we have to see how it's configured in the system
603 atif->functions.system_params = true;
606 if (atif->functions.system_params) {
607 ret = radeon_atif_get_notification_params(handle,
608 &atif->notification_cfg);
610 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
612 /* Disable notification */
613 atif->notification_cfg.enabled = false;
618 rdev->acpi.handle = handle;
619 rdev->acpi.notifier_call = radeon_acpi_event;
620 AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
621 rdev->acpi.notifier_call, rdev);
627 * radeon_acpi_fini - tear down driver acpi support
629 * @rdev: radeon_device pointer
631 * Unregisters with the acpi notifier chain (all asics).
633 void radeon_acpi_fini(struct radeon_device *rdev)
635 AcpiRemoveNotifyHandler(rdev->acpi.handle, ACPI_DEVICE_NOTIFY,
636 rdev->acpi.notifier_call);