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 extern void radeon_pm_acpi_event_handler(struct radeon_device *rdev);
37 struct atif_verify_interface {
38 u16 size; /* structure size in bytes (includes size field) */
39 u16 version; /* version */
40 u32 notification_mask; /* supported notifications mask */
41 u32 function_bits; /* supported functions bit vector */
44 struct atif_system_params {
45 u16 size; /* structure size in bytes (includes size field) */
46 u32 valid_mask; /* valid flags mask */
47 u32 flags; /* flags */
48 u8 command_code; /* notify command code */
51 struct atif_sbios_requests {
52 u16 size; /* structure size in bytes (includes size field) */
53 u32 pending; /* pending sbios requests */
54 u8 panel_exp_mode; /* panel expansion mode */
55 u8 thermal_gfx; /* thermal state: target gfx controller */
56 u8 thermal_state; /* thermal state: state id (0: exit state, non-0: state) */
57 u8 forced_power_gfx; /* forced power state: target gfx controller */
58 u8 forced_power_state; /* forced power state: state id */
59 u8 system_power_src; /* system power source */
60 u8 backlight_level; /* panel backlight level (0-255) */
63 #define ATIF_NOTIFY_MASK 0x3
64 #define ATIF_NOTIFY_NONE 0
65 #define ATIF_NOTIFY_81 1
66 #define ATIF_NOTIFY_N 2
68 struct atcs_verify_interface {
69 u16 size; /* structure size in bytes (includes size field) */
70 u16 version; /* version */
71 u32 function_bits; /* supported functions bit vector */
74 /* Call the ATIF method
77 * radeon_atif_call - call an ATIF method
79 * @handle: acpi handle
80 * @function: the ATIF function to execute
81 * @params: ATIF function params
83 * Executes the requested ATIF function (all asics).
84 * Returns a pointer to the acpi output buffer.
86 static ACPI_OBJECT *radeon_atif_call(ACPI_HANDLE handle, int function,
90 ACPI_OBJECT atif_arg_elements[2];
91 ACPI_OBJECT_LIST atif_arg;
92 ACPI_BUFFER buffer = { ACPI_ALLOCATE_BUFFER, NULL };
95 atif_arg.Pointer = &atif_arg_elements[0];
97 atif_arg_elements[0].Type = ACPI_TYPE_INTEGER;
98 atif_arg_elements[0].Integer.Value = function;
101 atif_arg_elements[1].Type = ACPI_TYPE_BUFFER;
102 atif_arg_elements[1].Buffer.Length = params->Length;
103 atif_arg_elements[1].Buffer.Pointer = params->Pointer;
105 /* We need a second fake parameter */
106 atif_arg_elements[1].Type = ACPI_TYPE_INTEGER;
107 atif_arg_elements[1].Integer.Value = 0;
110 status = AcpiEvaluateObject(handle, "ATIF", &atif_arg, &buffer);
112 /* Fail only if calling the method fails and ATIF is supported */
113 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
114 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
115 AcpiFormatException(status));
116 AcpiOsFree(buffer.Pointer);
120 return buffer.Pointer;
124 * radeon_atif_parse_notification - parse supported notifications
126 * @n: supported notifications struct
127 * @mask: supported notifications mask from ATIF
129 * Use the supported notifications mask from ATIF function
130 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
131 * are supported (all asics).
133 static void radeon_atif_parse_notification(struct radeon_atif_notifications *n, u32 mask)
135 n->display_switch = mask & ATIF_DISPLAY_SWITCH_REQUEST_SUPPORTED;
136 n->expansion_mode_change = mask & ATIF_EXPANSION_MODE_CHANGE_REQUEST_SUPPORTED;
137 n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
138 n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
139 n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
140 n->display_conf_change = mask & ATIF_DISPLAY_CONF_CHANGE_REQUEST_SUPPORTED;
141 n->px_gfx_switch = mask & ATIF_PX_GFX_SWITCH_REQUEST_SUPPORTED;
142 n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
143 n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
147 * radeon_atif_parse_functions - parse supported functions
149 * @f: supported functions struct
150 * @mask: supported functions mask from ATIF
152 * Use the supported functions mask from ATIF function
153 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
154 * are supported (all asics).
156 static void radeon_atif_parse_functions(struct radeon_atif_functions *f, u32 mask)
158 f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
159 f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
160 f->select_active_disp = mask & ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED;
161 f->lid_state = mask & ATIF_GET_LID_STATE_SUPPORTED;
162 f->get_tv_standard = mask & ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED;
163 f->set_tv_standard = mask & ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED;
164 f->get_panel_expansion_mode = mask & ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED;
165 f->set_panel_expansion_mode = mask & ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED;
166 f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
167 f->graphics_device_types = mask & ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED;
171 * radeon_atif_verify_interface - verify ATIF
173 * @handle: acpi handle
174 * @atif: radeon atif struct
176 * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
177 * to initialize ATIF and determine what features are supported
179 * returns 0 on success, error on failure.
181 static int radeon_atif_verify_interface(ACPI_HANDLE handle,
182 struct radeon_atif *atif)
185 struct atif_verify_interface output;
189 info = radeon_atif_call(handle, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
193 memset(&output, 0, sizeof(output));
195 size = *(u16 *) info->Buffer.Pointer;
197 DRM_INFO("ATIF buffer is too small: %zu\n", size);
201 size = min(sizeof(output), size);
203 memcpy(&output, info->Buffer.Pointer, size);
205 /* TODO: check version? */
206 DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
208 radeon_atif_parse_notification(&atif->notifications, output.notification_mask);
209 radeon_atif_parse_functions(&atif->functions, output.function_bits);
217 * radeon_atif_get_notification_params - determine notify configuration
219 * @handle: acpi handle
220 * @n: atif notification configuration struct
222 * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
223 * to determine if a notifier is used and if so which one
224 * (all asics). This is either Notify(VGA, 0x81) or Notify(VGA, n)
225 * where n is specified in the result if a notifier is used.
226 * Returns 0 on success, error on failure.
228 static int radeon_atif_get_notification_params(ACPI_HANDLE handle,
229 struct radeon_atif_notification_cfg *n)
232 struct atif_system_params params;
236 info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS, NULL);
242 size = *(u16 *) info->Buffer.Pointer;
248 memset(¶ms, 0, sizeof(params));
249 size = min(sizeof(params), size);
250 memcpy(¶ms, info->Buffer.Pointer, size);
252 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
253 params.flags, params.valid_mask);
254 params.flags = params.flags & params.valid_mask;
256 if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
259 } else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
261 n->command_code = 0x81;
268 n->command_code = params.command_code;
272 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
273 (n->enabled ? "enabled" : "disabled"),
280 * radeon_atif_get_sbios_requests - get requested sbios event
282 * @handle: acpi handle
283 * @req: atif sbios request struct
285 * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
286 * to determine what requests the sbios is making to the driver
288 * Returns 0 on success, error on failure.
290 static int radeon_atif_get_sbios_requests(ACPI_HANDLE handle,
291 struct atif_sbios_requests *req)
297 info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS, NULL);
301 size = *(u16 *)info->Buffer.Pointer;
306 memset(req, 0, sizeof(*req));
308 size = min(sizeof(*req), size);
309 memcpy(req, info->Buffer.Pointer, size);
310 DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
312 count = hweight32(req->pending);
320 * radeon_atif_handler - handle ATIF notify requests
322 * @rdev: radeon_device pointer
323 * @event: atif sbios request struct
325 * Checks the acpi event and if it matches an atif event,
327 * Returns NOTIFY code
329 void radeon_atif_handler(struct radeon_device *rdev,
332 struct radeon_atif *atif = &rdev->atif;
333 struct atif_sbios_requests req;
337 DRM_DEBUG_DRIVER("event, type = %#x\n",
340 if (!atif->notification_cfg.enabled ||
341 type != atif->notification_cfg.command_code)
345 /* Check pending SBIOS requests */
346 handle = rdev->acpi.handle;
347 count = radeon_atif_get_sbios_requests(handle, &req);
352 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
354 if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
355 struct radeon_encoder *enc = atif->encoder_for_bl;
358 DRM_DEBUG_DRIVER("Changing brightness to %d\n",
359 req.backlight_level);
361 radeon_set_backlight_level(rdev, enc, req.backlight_level);
364 if (rdev->is_atom_bios) {
365 struct radeon_encoder_atom_dig *dig = enc->enc_priv;
366 backlight_force_update(dig->bl_dev,
367 BACKLIGHT_UPDATE_HOTKEY);
369 struct radeon_encoder_lvds *dig = enc->enc_priv;
370 backlight_force_update(dig->bl_dev,
371 BACKLIGHT_UPDATE_HOTKEY);
373 #endif /* FREEBSD_WIP */
376 /* TODO: check other events */
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
385 /* Call the ATCS method
388 * radeon_atcs_call - call an ATCS method
390 * @handle: acpi handle
391 * @function: the ATCS function to execute
392 * @params: ATCS function params
394 * Executes the requested ATCS function (all asics).
395 * Returns a pointer to the acpi output buffer.
397 static union acpi_object *radeon_atcs_call(ACPI_HANDLE handle, int function,
401 ACPI_OBJECT atcs_arg_elements[2];
402 ACPI_OBJECT_LIST atcs_arg;
403 ACPI_BUFFER buffer = { ACPI_ALLOCATE_BUFFER, NULL };
406 atcs_arg.Pointer = &atcs_arg_elements[0];
408 atcs_arg_elements[0].Type = ACPI_TYPE_INTEGER;
409 atcs_arg_elements[0].Integer.Value = function;
412 atcs_arg_elements[1].Type = ACPI_TYPE_BUFFER;
413 atcs_arg_elements[1].Buffer.Length = params->Length;
414 atcs_arg_elements[1].Buffer.Pointer = params->Pointer;
416 /* We need a second fake parameter */
417 atcs_arg_elements[1].Type = ACPI_TYPE_INTEGER;
418 atcs_arg_elements[1].Integer.Value = 0;
421 status = AcpiEvaluateObject(handle, "ATCS", &atcs_arg, &buffer);
423 /* Fail only if calling the method fails and ATIF is supported */
424 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
425 DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
426 AcpiFormatException(status));
427 AcpiOsFree(buffer.Pointer);
431 return buffer.Pointer;
435 * radeon_atcs_parse_functions - parse supported functions
437 * @f: supported functions struct
438 * @mask: supported functions mask from ATCS
440 * Use the supported functions mask from ATCS function
441 * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
442 * are supported (all asics).
444 static void radeon_atcs_parse_functions(struct radeon_atcs_functions *f, u32 mask)
446 f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
447 f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
448 f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
449 f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
453 * radeon_atcs_verify_interface - verify ATCS
455 * @handle: acpi handle
456 * @atcs: radeon atcs struct
458 * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
459 * to initialize ATCS and determine what features are supported
461 * returns 0 on success, error on failure.
463 static int radeon_atcs_verify_interface(ACPI_HANDLE handle,
464 struct radeon_atcs *atcs)
467 struct atcs_verify_interface output;
471 info = radeon_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
475 memset(&output, 0, sizeof(output));
477 size = *(u16 *) info->Buffer.Pointer;
479 DRM_INFO("ATCS buffer is too small: %zu\n", size);
483 size = min(sizeof(output), size);
485 memcpy(&output, info->Buffer.Pointer, size);
487 /* TODO: check version? */
488 DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
490 radeon_atcs_parse_functions(&atcs->functions, output.function_bits);
498 * radeon_acpi_event - handle notify events
500 * @nb: notifier block
504 * Calls relevant radeon functions in response to various
506 * Returns NOTIFY code
508 static void radeon_acpi_event(ACPI_HANDLE handle, UINT32 type,
511 struct radeon_device *rdev = (struct radeon_device *)context;
514 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
515 if (power_supply_is_system_supplied() > 0)
516 DRM_DEBUG_DRIVER("pm: AC\n");
518 DRM_DEBUG_DRIVER("pm: DC\n");
520 radeon_pm_acpi_event_handler(rdev);
522 #endif /* FREEBSD_WIP */
524 /* Check for pending SBIOS requests */
525 radeon_atif_handler(rdev, type);
528 /* Call all ACPI methods here */
530 * radeon_acpi_init - init driver acpi support
532 * @rdev: radeon_device pointer
534 * Verifies the AMD ACPI interfaces and registers with the acpi
535 * notifier chain (all asics).
536 * Returns 0 on success, error on failure.
538 int radeon_acpi_init(struct radeon_device *rdev)
541 struct radeon_atif *atif = &rdev->atif;
542 struct radeon_atcs *atcs = &rdev->atcs;
545 /* Get the device handle */
546 handle = acpi_get_handle(rdev->dev);
548 /* No need to proceed if we're sure that ATIF is not supported */
549 if (!ASIC_IS_AVIVO(rdev) || !rdev->bios || !handle)
552 /* Call the ATCS method */
553 ret = radeon_atcs_verify_interface(handle, atcs);
555 DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
558 /* Call the ATIF method */
559 ret = radeon_atif_verify_interface(handle, atif);
561 DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
565 if (atif->notifications.brightness_change) {
566 struct drm_encoder *tmp;
567 struct radeon_encoder *target = NULL;
569 /* Find the encoder controlling the brightness */
570 list_for_each_entry(tmp, &rdev->ddev->mode_config.encoder_list,
572 struct radeon_encoder *enc = to_radeon_encoder(tmp);
574 if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
576 if (rdev->is_atom_bios) {
577 struct radeon_encoder_atom_dig *dig = enc->enc_priv;
583 struct radeon_encoder_lvds *dig = enc->enc_priv;
592 atif->encoder_for_bl = target;
594 /* Brightness change notification is enabled, but we
595 * didn't find a backlight controller, this should
598 DRM_ERROR("Cannot find a backlight controller\n");
602 if (atif->functions.sbios_requests && !atif->functions.system_params) {
603 /* XXX check this workraround, if sbios request function is
604 * present we have to see how it's configured in the system
607 atif->functions.system_params = true;
610 if (atif->functions.system_params) {
611 ret = radeon_atif_get_notification_params(handle,
612 &atif->notification_cfg);
614 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
616 /* Disable notification */
617 atif->notification_cfg.enabled = false;
622 rdev->acpi.handle = handle;
623 rdev->acpi.notifier_call = radeon_acpi_event;
624 AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
625 rdev->acpi.notifier_call, rdev);
631 * radeon_acpi_fini - tear down driver acpi support
633 * @rdev: radeon_device pointer
635 * Unregisters with the acpi notifier chain (all asics).
637 void radeon_acpi_fini(struct radeon_device *rdev)
639 AcpiRemoveNotifyHandler(rdev->acpi.handle, ACPI_DEVICE_NOTIFY,
640 rdev->acpi.notifier_call);