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.
24 #include <sys/cdefs.h>
25 __FBSDID("$FreeBSD$");
27 #include <dev/drm2/drmP.h>
28 #include <dev/drm2/drm_crtc_helper.h>
30 #include "radeon_acpi.h"
33 #define ACPI_AC_CLASS "ac_adapter"
35 struct atif_verify_interface {
36 u16 size; /* structure size in bytes (includes size field) */
37 u16 version; /* version */
38 u32 notification_mask; /* supported notifications mask */
39 u32 function_bits; /* supported functions bit vector */
42 struct atif_system_params {
43 u16 size; /* structure size in bytes (includes size field) */
44 u32 valid_mask; /* valid flags mask */
45 u32 flags; /* flags */
46 u8 command_code; /* notify command code */
49 struct atif_sbios_requests {
50 u16 size; /* structure size in bytes (includes size field) */
51 u32 pending; /* pending sbios requests */
52 u8 panel_exp_mode; /* panel expansion mode */
53 u8 thermal_gfx; /* thermal state: target gfx controller */
54 u8 thermal_state; /* thermal state: state id (0: exit state, non-0: state) */
55 u8 forced_power_gfx; /* forced power state: target gfx controller */
56 u8 forced_power_state; /* forced power state: state id */
57 u8 system_power_src; /* system power source */
58 u8 backlight_level; /* panel backlight level (0-255) */
61 #define ATIF_NOTIFY_MASK 0x3
62 #define ATIF_NOTIFY_NONE 0
63 #define ATIF_NOTIFY_81 1
64 #define ATIF_NOTIFY_N 2
66 struct atcs_verify_interface {
67 u16 size; /* structure size in bytes (includes size field) */
68 u16 version; /* version */
69 u32 function_bits; /* supported functions bit vector */
72 /* Call the ATIF method
75 * radeon_atif_call - call an ATIF method
77 * @handle: acpi handle
78 * @function: the ATIF function to execute
79 * @params: ATIF function params
81 * Executes the requested ATIF function (all asics).
82 * Returns a pointer to the acpi output buffer.
84 static ACPI_OBJECT *radeon_atif_call(ACPI_HANDLE handle, int function,
88 ACPI_OBJECT atif_arg_elements[2];
89 ACPI_OBJECT_LIST atif_arg;
90 ACPI_BUFFER buffer = { ACPI_ALLOCATE_BUFFER, NULL };
93 atif_arg.Pointer = &atif_arg_elements[0];
95 atif_arg_elements[0].Type = ACPI_TYPE_INTEGER;
96 atif_arg_elements[0].Integer.Value = function;
99 atif_arg_elements[1].Type = ACPI_TYPE_BUFFER;
100 atif_arg_elements[1].Buffer.Length = params->Length;
101 atif_arg_elements[1].Buffer.Pointer = params->Pointer;
103 /* We need a second fake parameter */
104 atif_arg_elements[1].Type = ACPI_TYPE_INTEGER;
105 atif_arg_elements[1].Integer.Value = 0;
108 status = AcpiEvaluateObject(handle, "ATIF", &atif_arg, &buffer);
110 /* Fail only if calling the method fails and ATIF is supported */
111 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
112 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
113 AcpiFormatException(status));
114 AcpiOsFree(buffer.Pointer);
118 return buffer.Pointer;
122 * radeon_atif_parse_notification - parse supported notifications
124 * @n: supported notifications struct
125 * @mask: supported notifications mask from ATIF
127 * Use the supported notifications mask from ATIF function
128 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
129 * are supported (all asics).
131 static void radeon_atif_parse_notification(struct radeon_atif_notifications *n, u32 mask)
133 n->display_switch = mask & ATIF_DISPLAY_SWITCH_REQUEST_SUPPORTED;
134 n->expansion_mode_change = mask & ATIF_EXPANSION_MODE_CHANGE_REQUEST_SUPPORTED;
135 n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
136 n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
137 n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
138 n->display_conf_change = mask & ATIF_DISPLAY_CONF_CHANGE_REQUEST_SUPPORTED;
139 n->px_gfx_switch = mask & ATIF_PX_GFX_SWITCH_REQUEST_SUPPORTED;
140 n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
141 n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
145 * radeon_atif_parse_functions - parse supported functions
147 * @f: supported functions struct
148 * @mask: supported functions mask from ATIF
150 * Use the supported functions mask from ATIF function
151 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
152 * are supported (all asics).
154 static void radeon_atif_parse_functions(struct radeon_atif_functions *f, u32 mask)
156 f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
157 f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
158 f->select_active_disp = mask & ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED;
159 f->lid_state = mask & ATIF_GET_LID_STATE_SUPPORTED;
160 f->get_tv_standard = mask & ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED;
161 f->set_tv_standard = mask & ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED;
162 f->get_panel_expansion_mode = mask & ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED;
163 f->set_panel_expansion_mode = mask & ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED;
164 f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
165 f->graphics_device_types = mask & ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED;
169 * radeon_atif_verify_interface - verify ATIF
171 * @handle: acpi handle
172 * @atif: radeon atif struct
174 * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
175 * to initialize ATIF and determine what features are supported
177 * returns 0 on success, error on failure.
179 static int radeon_atif_verify_interface(ACPI_HANDLE handle,
180 struct radeon_atif *atif)
183 struct atif_verify_interface output;
187 info = radeon_atif_call(handle, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
191 memset(&output, 0, sizeof(output));
193 size = *(u16 *) info->Buffer.Pointer;
195 DRM_INFO("ATIF buffer is too small: %zu\n", size);
199 size = min(sizeof(output), size);
201 memcpy(&output, info->Buffer.Pointer, size);
203 /* TODO: check version? */
204 DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
206 radeon_atif_parse_notification(&atif->notifications, output.notification_mask);
207 radeon_atif_parse_functions(&atif->functions, output.function_bits);
215 * radeon_atif_get_notification_params - determine notify configuration
217 * @handle: acpi handle
218 * @n: atif notification configuration struct
220 * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
221 * to determine if a notifier is used and if so which one
222 * (all asics). This is either Notify(VGA, 0x81) or Notify(VGA, n)
223 * where n is specified in the result if a notifier is used.
224 * Returns 0 on success, error on failure.
226 static int radeon_atif_get_notification_params(ACPI_HANDLE handle,
227 struct radeon_atif_notification_cfg *n)
230 struct atif_system_params params;
234 info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS, NULL);
240 size = *(u16 *) info->Buffer.Pointer;
246 memset(¶ms, 0, sizeof(params));
247 size = min(sizeof(params), size);
248 memcpy(¶ms, info->Buffer.Pointer, size);
250 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
251 params.flags, params.valid_mask);
252 params.flags = params.flags & params.valid_mask;
254 if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
257 } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
259 n->command_code = 0x81;
266 n->command_code = params.command_code;
270 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
271 (n->enabled ? "enabled" : "disabled"),
278 * radeon_atif_get_sbios_requests - get requested sbios event
280 * @handle: acpi handle
281 * @req: atif sbios request struct
283 * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
284 * to determine what requests the sbios is making to the driver
286 * Returns 0 on success, error on failure.
288 static int radeon_atif_get_sbios_requests(ACPI_HANDLE handle,
289 struct atif_sbios_requests *req)
295 info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS, NULL);
299 size = *(u16 *)info->Buffer.Pointer;
304 memset(req, 0, sizeof(*req));
306 size = min(sizeof(*req), size);
307 memcpy(req, info->Buffer.Pointer, size);
308 DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
310 count = hweight32(req->pending);
318 * radeon_atif_handler - handle ATIF notify requests
320 * @rdev: radeon_device pointer
321 * @event: atif sbios request struct
323 * Checks the acpi event and if it matches an atif event,
325 * Returns NOTIFY code
327 void radeon_atif_handler(struct radeon_device *rdev,
330 struct radeon_atif *atif = &rdev->atif;
331 struct atif_sbios_requests req;
335 DRM_DEBUG_DRIVER("event, type = %#x\n",
338 if (!atif->notification_cfg.enabled ||
339 type != atif->notification_cfg.command_code)
343 /* Check pending SBIOS requests */
344 handle = rdev->acpi.handle;
345 count = radeon_atif_get_sbios_requests(handle, &req);
350 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
352 if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
353 struct radeon_encoder *enc = atif->encoder_for_bl;
356 DRM_DEBUG_DRIVER("Changing brightness to %d\n",
357 req.backlight_level);
359 radeon_set_backlight_level(rdev, enc, req.backlight_level);
362 if (rdev->is_atom_bios) {
363 struct radeon_encoder_atom_dig *dig = enc->enc_priv;
364 backlight_force_update(dig->bl_dev,
365 BACKLIGHT_UPDATE_HOTKEY);
367 struct radeon_encoder_lvds *dig = enc->enc_priv;
368 backlight_force_update(dig->bl_dev,
369 BACKLIGHT_UPDATE_HOTKEY);
371 #endif /* DUMBBELL_WIP */
374 /* TODO: check other events */
376 /* We've handled the event, stop the notifier chain. The ACPI interface
377 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
378 * userspace if the event was generated only to signal a SBIOS
383 /* Call the ATCS method
386 * radeon_atcs_call - call an ATCS method
388 * @handle: acpi handle
389 * @function: the ATCS function to execute
390 * @params: ATCS function params
392 * Executes the requested ATCS function (all asics).
393 * Returns a pointer to the acpi output buffer.
395 static union acpi_object *radeon_atcs_call(ACPI_HANDLE handle, int function,
399 ACPI_OBJECT atcs_arg_elements[2];
400 ACPI_OBJECT_LIST atcs_arg;
401 ACPI_BUFFER buffer = { ACPI_ALLOCATE_BUFFER, NULL };
404 atcs_arg.Pointer = &atcs_arg_elements[0];
406 atcs_arg_elements[0].Type = ACPI_TYPE_INTEGER;
407 atcs_arg_elements[0].Integer.Value = function;
410 atcs_arg_elements[1].Type = ACPI_TYPE_BUFFER;
411 atcs_arg_elements[1].Buffer.Length = params->Length;
412 atcs_arg_elements[1].Buffer.Pointer = params->Pointer;
414 /* We need a second fake parameter */
415 atcs_arg_elements[1].Type = ACPI_TYPE_INTEGER;
416 atcs_arg_elements[1].Integer.Value = 0;
419 status = AcpiEvaluateObject(handle, "ATCS", &atcs_arg, &buffer);
421 /* Fail only if calling the method fails and ATIF is supported */
422 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
423 DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
424 AcpiFormatException(status));
425 AcpiOsFree(buffer.Pointer);
429 return buffer.Pointer;
433 * radeon_atcs_parse_functions - parse supported functions
435 * @f: supported functions struct
436 * @mask: supported functions mask from ATCS
438 * Use the supported functions mask from ATCS function
439 * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
440 * are supported (all asics).
442 static void radeon_atcs_parse_functions(struct radeon_atcs_functions *f, u32 mask)
444 f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
445 f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
446 f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
447 f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
451 * radeon_atcs_verify_interface - verify ATCS
453 * @handle: acpi handle
454 * @atcs: radeon atcs struct
456 * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
457 * to initialize ATCS and determine what features are supported
459 * returns 0 on success, error on failure.
461 static int radeon_atcs_verify_interface(ACPI_HANDLE handle,
462 struct radeon_atcs *atcs)
465 struct atcs_verify_interface output;
469 info = radeon_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
473 memset(&output, 0, sizeof(output));
475 size = *(u16 *) info->Buffer.Pointer;
477 DRM_INFO("ATCS buffer is too small: %zu\n", size);
481 size = min(sizeof(output), size);
483 memcpy(&output, info->Buffer.Pointer, size);
485 /* TODO: check version? */
486 DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
488 radeon_atcs_parse_functions(&atcs->functions, output.function_bits);
496 * radeon_acpi_event - handle notify events
498 * @nb: notifier block
502 * Calls relevant radeon functions in response to various
504 * Returns NOTIFY code
506 static void radeon_acpi_event(ACPI_HANDLE handle, UINT32 type,
509 struct radeon_device *rdev = (struct radeon_device *)context;
512 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
513 if (power_supply_is_system_supplied() > 0)
514 DRM_DEBUG_DRIVER("pm: AC\n");
516 DRM_DEBUG_DRIVER("pm: DC\n");
518 radeon_pm_acpi_event_handler(rdev);
520 #endif /* DUMBBELL_WIP */
522 /* Check for pending SBIOS requests */
523 radeon_atif_handler(rdev, type);
526 /* Call all ACPI methods here */
528 * radeon_acpi_init - init driver acpi support
530 * @rdev: radeon_device pointer
532 * Verifies the AMD ACPI interfaces and registers with the acpi
533 * notifier chain (all asics).
534 * Returns 0 on success, error on failure.
536 int radeon_acpi_init(struct radeon_device *rdev)
539 struct radeon_atif *atif = &rdev->atif;
540 struct radeon_atcs *atcs = &rdev->atcs;
543 /* Get the device handle */
544 handle = acpi_get_handle(rdev->dev);
546 /* No need to proceed if we're sure that ATIF is not supported */
547 if (!ASIC_IS_AVIVO(rdev) || !rdev->bios || !handle)
550 /* Call the ATCS method */
551 ret = radeon_atcs_verify_interface(handle, atcs);
553 DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
556 /* Call the ATIF method */
557 ret = radeon_atif_verify_interface(handle, atif);
559 DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
563 if (atif->notifications.brightness_change) {
564 struct drm_encoder *tmp;
565 struct radeon_encoder *target = NULL;
567 /* Find the encoder controlling the brightness */
568 list_for_each_entry(tmp, &rdev->ddev->mode_config.encoder_list,
570 struct radeon_encoder *enc = to_radeon_encoder(tmp);
572 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
574 if (rdev->is_atom_bios) {
575 struct radeon_encoder_atom_dig *dig = enc->enc_priv;
581 struct radeon_encoder_lvds *dig = enc->enc_priv;
590 atif->encoder_for_bl = target;
592 /* Brightness change notification is enabled, but we
593 * didn't find a backlight controller, this should
596 DRM_ERROR("Cannot find a backlight controller\n");
600 if (atif->functions.sbios_requests && !atif->functions.system_params) {
601 /* XXX check this workraround, if sbios request function is
602 * present we have to see how it's configured in the system
605 atif->functions.system_params = true;
608 if (atif->functions.system_params) {
609 ret = radeon_atif_get_notification_params(handle,
610 &atif->notification_cfg);
612 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
614 /* Disable notification */
615 atif->notification_cfg.enabled = false;
620 rdev->acpi.handle = handle;
621 rdev->acpi.notifier_call = radeon_acpi_event;
622 AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
623 rdev->acpi.notifier_call, rdev);
629 * radeon_acpi_fini - tear down driver acpi support
631 * @rdev: radeon_device pointer
633 * Unregisters with the acpi notifier chain (all asics).
635 void radeon_acpi_fini(struct radeon_device *rdev)
637 AcpiRemoveNotifyHandler(rdev->acpi.handle, ACPI_DEVICE_NOTIFY,
638 rdev->acpi.notifier_call);