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 <linux/pci.h>
25 #include <linux/acpi.h>
26 #include <linux/slab.h>
27 #include <linux/power_supply.h>
28 #include <acpi/acpi_drivers.h>
29 #include <acpi/acpi_bus.h>
30 #include <acpi/video.h>
34 #include "drm_sarea.h"
35 #include "drm_crtc_helper.h"
37 #include "radeon_acpi.h"
40 #include <linux/vga_switcheroo.h>
42 #define ACPI_AC_CLASS "ac_adapter"
44 extern void radeon_pm_acpi_event_handler(struct radeon_device
*rdev
);
46 struct atif_verify_interface
{
47 u16 size
; /* structure size in bytes (includes size field) */
48 u16 version
; /* version */
49 u32 notification_mask
; /* supported notifications mask */
50 u32 function_bits
; /* supported functions bit vector */
53 struct atif_system_params
{
54 u16 size
; /* structure size in bytes (includes size field) */
55 u32 valid_mask
; /* valid flags mask */
56 u32 flags
; /* flags */
57 u8 command_code
; /* notify command code */
60 struct atif_sbios_requests
{
61 u16 size
; /* structure size in bytes (includes size field) */
62 u32 pending
; /* pending sbios requests */
63 u8 panel_exp_mode
; /* panel expansion mode */
64 u8 thermal_gfx
; /* thermal state: target gfx controller */
65 u8 thermal_state
; /* thermal state: state id (0: exit state, non-0: state) */
66 u8 forced_power_gfx
; /* forced power state: target gfx controller */
67 u8 forced_power_state
; /* forced power state: state id */
68 u8 system_power_src
; /* system power source */
69 u8 backlight_level
; /* panel backlight level (0-255) */
72 #define ATIF_NOTIFY_MASK 0x3
73 #define ATIF_NOTIFY_NONE 0
74 #define ATIF_NOTIFY_81 1
75 #define ATIF_NOTIFY_N 2
77 /* Call the ATIF method
80 * radeon_atif_call - call an ATIF method
82 * @handle: acpi handle
83 * @function: the ATIF function to execute
84 * @params: ATIF function params
86 * Executes the requested ATIF function (all asics).
87 * Returns a pointer to the acpi output buffer.
89 static union acpi_object
*radeon_atif_call(acpi_handle handle
, int function
,
90 struct acpi_buffer
*params
)
93 union acpi_object atif_arg_elements
[2];
94 struct acpi_object_list atif_arg
;
95 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
98 atif_arg
.pointer
= &atif_arg_elements
[0];
100 atif_arg_elements
[0].type
= ACPI_TYPE_INTEGER
;
101 atif_arg_elements
[0].integer
.value
= function
;
104 atif_arg_elements
[1].type
= ACPI_TYPE_BUFFER
;
105 atif_arg_elements
[1].buffer
.length
= params
->length
;
106 atif_arg_elements
[1].buffer
.pointer
= params
->pointer
;
108 /* We need a second fake parameter */
109 atif_arg_elements
[1].type
= ACPI_TYPE_INTEGER
;
110 atif_arg_elements
[1].integer
.value
= 0;
113 status
= acpi_evaluate_object(handle
, "ATIF", &atif_arg
, &buffer
);
115 /* Fail only if calling the method fails and ATIF is supported */
116 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
117 DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
118 acpi_format_exception(status
));
119 kfree(buffer
.pointer
);
123 return buffer
.pointer
;
127 * radeon_atif_parse_notification - parse supported notifications
129 * @n: supported notifications struct
130 * @mask: supported notifications mask from ATIF
132 * Use the supported notifications mask from ATIF function
133 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
134 * are supported (all asics).
136 static void radeon_atif_parse_notification(struct radeon_atif_notifications
*n
, u32 mask
)
138 n
->display_switch
= mask
& ATIF_DISPLAY_SWITCH_REQUEST_SUPPORTED
;
139 n
->expansion_mode_change
= mask
& ATIF_EXPANSION_MODE_CHANGE_REQUEST_SUPPORTED
;
140 n
->thermal_state
= mask
& ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED
;
141 n
->forced_power_state
= mask
& ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED
;
142 n
->system_power_state
= mask
& ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED
;
143 n
->display_conf_change
= mask
& ATIF_DISPLAY_CONF_CHANGE_REQUEST_SUPPORTED
;
144 n
->px_gfx_switch
= mask
& ATIF_PX_GFX_SWITCH_REQUEST_SUPPORTED
;
145 n
->brightness_change
= mask
& ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED
;
146 n
->dgpu_display_event
= mask
& ATIF_DGPU_DISPLAY_EVENT_SUPPORTED
;
150 * radeon_atif_parse_functions - parse supported functions
152 * @f: supported functions struct
153 * @mask: supported functions mask from ATIF
155 * Use the supported functions mask from ATIF function
156 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
157 * are supported (all asics).
159 static void radeon_atif_parse_functions(struct radeon_atif_functions
*f
, u32 mask
)
161 f
->system_params
= mask
& ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED
;
162 f
->sbios_requests
= mask
& ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED
;
163 f
->select_active_disp
= mask
& ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED
;
164 f
->lid_state
= mask
& ATIF_GET_LID_STATE_SUPPORTED
;
165 f
->get_tv_standard
= mask
& ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED
;
166 f
->set_tv_standard
= mask
& ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED
;
167 f
->get_panel_expansion_mode
= mask
& ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED
;
168 f
->set_panel_expansion_mode
= mask
& ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED
;
169 f
->temperature_change
= mask
& ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED
;
170 f
->graphics_device_types
= mask
& ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED
;
174 * radeon_atif_verify_interface - verify ATIF
176 * @handle: acpi handle
177 * @atif: radeon atif struct
179 * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
180 * to initialize ATIF and determine what features are supported
182 * returns 0 on success, error on failure.
184 static int radeon_atif_verify_interface(acpi_handle handle
,
185 struct radeon_atif
*atif
)
187 union acpi_object
*info
;
188 struct atif_verify_interface output
;
192 info
= radeon_atif_call(handle
, ATIF_FUNCTION_VERIFY_INTERFACE
, NULL
);
196 memset(&output
, 0, sizeof(output
));
198 size
= *(u16
*) info
->buffer
.pointer
;
200 DRM_INFO("ATIF buffer is too small: %lu\n", size
);
204 size
= min(sizeof(output
), size
);
206 memcpy(&output
, info
->buffer
.pointer
, size
);
208 /* TODO: check version? */
209 DRM_DEBUG_DRIVER("ATIF version %u\n", output
.version
);
211 radeon_atif_parse_notification(&atif
->notifications
, output
.notification_mask
);
212 radeon_atif_parse_functions(&atif
->functions
, output
.function_bits
);
220 * radeon_atif_get_notification_params - determine notify configuration
222 * @handle: acpi handle
223 * @n: atif notification configuration struct
225 * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
226 * to determine if a notifier is used and if so which one
227 * (all asics). This is either Notify(VGA, 0x81) or Notify(VGA, n)
228 * where n is specified in the result if a notifier is used.
229 * Returns 0 on success, error on failure.
231 static int radeon_atif_get_notification_params(acpi_handle handle
,
232 struct radeon_atif_notification_cfg
*n
)
234 union acpi_object
*info
;
235 struct atif_system_params params
;
239 info
= radeon_atif_call(handle
, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS
, NULL
);
245 size
= *(u16
*) info
->buffer
.pointer
;
251 memset(¶ms
, 0, sizeof(params
));
252 size
= min(sizeof(params
), size
);
253 memcpy(¶ms
, info
->buffer
.pointer
, size
);
255 DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
256 params
.flags
, params
.valid_mask
);
257 params
.flags
= params
.flags
& params
.valid_mask
;
259 if ((params
.flags
& ATIF_NOTIFY_MASK
) == ATIF_NOTIFY_NONE
) {
262 } else if ((params
.flags
& ATIF_NOTIFY_MASK
) == ATIF_NOTIFY_81
) {
264 n
->command_code
= 0x81;
271 n
->command_code
= params
.command_code
;
275 DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
276 (n
->enabled
? "enabled" : "disabled"),
283 * radeon_atif_get_sbios_requests - get requested sbios event
285 * @handle: acpi handle
286 * @req: atif sbios request struct
288 * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
289 * to determine what requests the sbios is making to the driver
291 * Returns 0 on success, error on failure.
293 static int radeon_atif_get_sbios_requests(acpi_handle handle
,
294 struct atif_sbios_requests
*req
)
296 union acpi_object
*info
;
300 info
= radeon_atif_call(handle
, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS
, NULL
);
304 size
= *(u16
*)info
->buffer
.pointer
;
309 memset(req
, 0, sizeof(*req
));
311 size
= min(sizeof(*req
), size
);
312 memcpy(req
, info
->buffer
.pointer
, size
);
313 DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req
->pending
);
315 count
= hweight32(req
->pending
);
323 * radeon_atif_handler - handle ATIF notify requests
325 * @rdev: radeon_device pointer
326 * @event: atif sbios request struct
328 * Checks the acpi event and if it matches an atif event,
330 * Returns NOTIFY code
332 int radeon_atif_handler(struct radeon_device
*rdev
,
333 struct acpi_bus_event
*event
)
335 struct radeon_atif
*atif
= &rdev
->atif
;
336 struct atif_sbios_requests req
;
340 DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
341 event
->device_class
, event
->type
);
343 if (strcmp(event
->device_class
, ACPI_VIDEO_CLASS
) != 0)
346 if (!atif
->notification_cfg
.enabled
||
347 event
->type
!= atif
->notification_cfg
.command_code
)
351 /* Check pending SBIOS requests */
352 handle
= DEVICE_ACPI_HANDLE(&rdev
->pdev
->dev
);
353 count
= radeon_atif_get_sbios_requests(handle
, &req
);
358 DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count
);
360 if (req
.pending
& ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST
) {
361 struct radeon_encoder
*enc
= atif
->backlight_ctl
;
364 struct radeon_encoder_atom_dig
*dig
= enc
->enc_priv
;
365 dig
->backlight_level
= req
.backlight_level
;
367 DRM_DEBUG_DRIVER("Changing brightness to %d\n",
368 req
.backlight_level
);
370 atombios_set_panel_brightness(enc
);
372 backlight_force_update(dig
->bl_dev
,
373 BACKLIGHT_UPDATE_HOTKEY
);
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
387 * radeon_acpi_event - handle notify events
389 * @nb: notifier block
393 * Calls relevant radeon functions in response to various
395 * Returns NOTIFY code
397 static int radeon_acpi_event(struct notifier_block
*nb
,
401 struct radeon_device
*rdev
= container_of(nb
, struct radeon_device
, acpi_nb
);
402 struct acpi_bus_event
*entry
= (struct acpi_bus_event
*)data
;
404 if (strcmp(entry
->device_class
, ACPI_AC_CLASS
) == 0) {
405 if (power_supply_is_system_supplied() > 0)
406 DRM_DEBUG_DRIVER("pm: AC\n");
408 DRM_DEBUG_DRIVER("pm: DC\n");
410 radeon_pm_acpi_event_handler(rdev
);
413 /* Check for pending SBIOS requests */
414 return radeon_atif_handler(rdev
, entry
);
417 /* Call all ACPI methods here */
419 * radeon_acpi_init - init driver acpi support
421 * @rdev: radeon_device pointer
423 * Verifies the AMD ACPI interfaces and registers with the acpi
424 * notifier chain (all asics).
425 * Returns 0 on success, error on failure.
427 int radeon_acpi_init(struct radeon_device
*rdev
)
430 struct radeon_atif
*atif
= &rdev
->atif
;
433 /* Get the device handle */
434 handle
= DEVICE_ACPI_HANDLE(&rdev
->pdev
->dev
);
436 /* No need to proceed if we're sure that ATIF is not supported */
437 if (!ASIC_IS_AVIVO(rdev
) || !rdev
->bios
|| !handle
)
440 /* Call the ATIF method */
441 ret
= radeon_atif_verify_interface(handle
, atif
);
443 DRM_DEBUG_DRIVER("Call to verify_interface failed: %d\n", ret
);
447 if (atif
->notifications
.brightness_change
) {
448 struct drm_encoder
*tmp
;
449 struct radeon_encoder
*target
= NULL
;
451 /* Find the encoder controlling the brightness */
452 list_for_each_entry(tmp
, &rdev
->ddev
->mode_config
.encoder_list
,
454 struct radeon_encoder
*enc
= to_radeon_encoder(tmp
);
455 struct radeon_encoder_atom_dig
*dig
= enc
->enc_priv
;
457 if ((enc
->devices
& (ATOM_DEVICE_LCD_SUPPORT
)) &&
458 dig
->bl_dev
!= NULL
) {
464 atif
->backlight_ctl
= target
;
466 /* Brightness change notification is enabled, but we
467 * didn't find a backlight controller, this should
470 DRM_ERROR("Cannot find a backlight controller\n");
474 if (atif
->functions
.sbios_requests
&& !atif
->functions
.system_params
) {
475 /* XXX check this workraround, if sbios request function is
476 * present we have to see how it's configured in the system
479 atif
->functions
.system_params
= true;
482 if (atif
->functions
.system_params
) {
483 ret
= radeon_atif_get_notification_params(handle
,
484 &atif
->notification_cfg
);
486 DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
488 /* Disable notification */
489 atif
->notification_cfg
.enabled
= false;
494 rdev
->acpi_nb
.notifier_call
= radeon_acpi_event
;
495 register_acpi_notifier(&rdev
->acpi_nb
);
501 * radeon_acpi_fini - tear down driver acpi support
503 * @rdev: radeon_device pointer
505 * Unregisters with the acpi notifier chain (all asics).
507 void radeon_acpi_fini(struct radeon_device
*rdev
)
509 unregister_acpi_notifier(&rdev
->acpi_nb
);