Home | History | Annotate | Line # | Download | only in radeon
radeon_acpi.c revision 1.1.1.1.2.2
      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 
     24 #include <linux/pci.h>
     25 #include <linux/acpi.h>
     26 #include <linux/slab.h>
     27 #include <linux/power_supply.h>
     28 #include <linux/vga_switcheroo.h>
     29 #include <acpi/video.h>
     30 #include <drm/drmP.h>
     31 #include <drm/drm_crtc_helper.h>
     32 #include "radeon.h"
     33 #include "radeon_acpi.h"
     34 #include "atom.h"
     35 
     36 #define ACPI_AC_CLASS           "ac_adapter"
     37 
     38 extern void radeon_pm_acpi_event_handler(struct radeon_device *rdev);
     39 
     40 struct atif_verify_interface {
     41 	u16 size;		/* structure size in bytes (includes size field) */
     42 	u16 version;		/* version */
     43 	u32 notification_mask;	/* supported notifications mask */
     44 	u32 function_bits;	/* supported functions bit vector */
     45 } __packed;
     46 
     47 struct atif_system_params {
     48 	u16 size;		/* structure size in bytes (includes size field) */
     49 	u32 valid_mask;		/* valid flags mask */
     50 	u32 flags;		/* flags */
     51 	u8 command_code;	/* notify command code */
     52 } __packed;
     53 
     54 struct atif_sbios_requests {
     55 	u16 size;		/* structure size in bytes (includes size field) */
     56 	u32 pending;		/* pending sbios requests */
     57 	u8 panel_exp_mode;	/* panel expansion mode */
     58 	u8 thermal_gfx;		/* thermal state: target gfx controller */
     59 	u8 thermal_state;	/* thermal state: state id (0: exit state, non-0: state) */
     60 	u8 forced_power_gfx;	/* forced power state: target gfx controller */
     61 	u8 forced_power_state;	/* forced power state: state id */
     62 	u8 system_power_src;	/* system power source */
     63 	u8 backlight_level;	/* panel backlight level (0-255) */
     64 } __packed;
     65 
     66 #define ATIF_NOTIFY_MASK	0x3
     67 #define ATIF_NOTIFY_NONE	0
     68 #define ATIF_NOTIFY_81		1
     69 #define ATIF_NOTIFY_N		2
     70 
     71 struct atcs_verify_interface {
     72 	u16 size;		/* structure size in bytes (includes size field) */
     73 	u16 version;		/* version */
     74 	u32 function_bits;	/* supported functions bit vector */
     75 } __packed;
     76 
     77 #define ATCS_VALID_FLAGS_MASK	0x3
     78 
     79 struct atcs_pref_req_input {
     80 	u16 size;		/* structure size in bytes (includes size field) */
     81 	u16 client_id;		/* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
     82 	u16 valid_flags_mask;	/* valid flags mask */
     83 	u16 flags;		/* flags */
     84 	u8 req_type;		/* request type */
     85 	u8 perf_req;		/* performance request */
     86 } __packed;
     87 
     88 struct atcs_pref_req_output {
     89 	u16 size;		/* structure size in bytes (includes size field) */
     90 	u8 ret_val;		/* return value */
     91 } __packed;
     92 
     93 /* Call the ATIF method
     94  */
     95 /**
     96  * radeon_atif_call - call an ATIF method
     97  *
     98  * @handle: acpi handle
     99  * @function: the ATIF function to execute
    100  * @params: ATIF function params
    101  *
    102  * Executes the requested ATIF function (all asics).
    103  * Returns a pointer to the acpi output buffer.
    104  */
    105 static union acpi_object *radeon_atif_call(acpi_handle handle, int function,
    106 		struct acpi_buffer *params)
    107 {
    108 	acpi_status status;
    109 	union acpi_object atif_arg_elements[2];
    110 	struct acpi_object_list atif_arg;
    111 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
    112 
    113 	atif_arg.count = 2;
    114 	atif_arg.pointer = &atif_arg_elements[0];
    115 
    116 	atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
    117 	atif_arg_elements[0].integer.value = function;
    118 
    119 	if (params) {
    120 		atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
    121 		atif_arg_elements[1].buffer.length = params->length;
    122 		atif_arg_elements[1].buffer.pointer = params->pointer;
    123 	} else {
    124 		/* We need a second fake parameter */
    125 		atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
    126 		atif_arg_elements[1].integer.value = 0;
    127 	}
    128 
    129 	status = acpi_evaluate_object(handle, "ATIF", &atif_arg, &buffer);
    130 
    131 	/* Fail only if calling the method fails and ATIF is supported */
    132 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
    133 		DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
    134 				 acpi_format_exception(status));
    135 		kfree(buffer.pointer);
    136 		return NULL;
    137 	}
    138 
    139 	return buffer.pointer;
    140 }
    141 
    142 /**
    143  * radeon_atif_parse_notification - parse supported notifications
    144  *
    145  * @n: supported notifications struct
    146  * @mask: supported notifications mask from ATIF
    147  *
    148  * Use the supported notifications mask from ATIF function
    149  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
    150  * are supported (all asics).
    151  */
    152 static void radeon_atif_parse_notification(struct radeon_atif_notifications *n, u32 mask)
    153 {
    154 	n->display_switch = mask & ATIF_DISPLAY_SWITCH_REQUEST_SUPPORTED;
    155 	n->expansion_mode_change = mask & ATIF_EXPANSION_MODE_CHANGE_REQUEST_SUPPORTED;
    156 	n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
    157 	n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
    158 	n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
    159 	n->display_conf_change = mask & ATIF_DISPLAY_CONF_CHANGE_REQUEST_SUPPORTED;
    160 	n->px_gfx_switch = mask & ATIF_PX_GFX_SWITCH_REQUEST_SUPPORTED;
    161 	n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
    162 	n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
    163 }
    164 
    165 /**
    166  * radeon_atif_parse_functions - parse supported functions
    167  *
    168  * @f: supported functions struct
    169  * @mask: supported functions mask from ATIF
    170  *
    171  * Use the supported functions mask from ATIF function
    172  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
    173  * are supported (all asics).
    174  */
    175 static void radeon_atif_parse_functions(struct radeon_atif_functions *f, u32 mask)
    176 {
    177 	f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
    178 	f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
    179 	f->select_active_disp = mask & ATIF_SELECT_ACTIVE_DISPLAYS_SUPPORTED;
    180 	f->lid_state = mask & ATIF_GET_LID_STATE_SUPPORTED;
    181 	f->get_tv_standard = mask & ATIF_GET_TV_STANDARD_FROM_CMOS_SUPPORTED;
    182 	f->set_tv_standard = mask & ATIF_SET_TV_STANDARD_IN_CMOS_SUPPORTED;
    183 	f->get_panel_expansion_mode = mask & ATIF_GET_PANEL_EXPANSION_MODE_FROM_CMOS_SUPPORTED;
    184 	f->set_panel_expansion_mode = mask & ATIF_SET_PANEL_EXPANSION_MODE_IN_CMOS_SUPPORTED;
    185 	f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
    186 	f->graphics_device_types = mask & ATIF_GET_GRAPHICS_DEVICE_TYPES_SUPPORTED;
    187 }
    188 
    189 /**
    190  * radeon_atif_verify_interface - verify ATIF
    191  *
    192  * @handle: acpi handle
    193  * @atif: radeon atif struct
    194  *
    195  * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
    196  * to initialize ATIF and determine what features are supported
    197  * (all asics).
    198  * returns 0 on success, error on failure.
    199  */
    200 static int radeon_atif_verify_interface(acpi_handle handle,
    201 		struct radeon_atif *atif)
    202 {
    203 	union acpi_object *info;
    204 	struct atif_verify_interface output;
    205 	size_t size;
    206 	int err = 0;
    207 
    208 	info = radeon_atif_call(handle, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
    209 	if (!info)
    210 		return -EIO;
    211 
    212 	memset(&output, 0, sizeof(output));
    213 
    214 	size = *(u16 *) info->buffer.pointer;
    215 	if (size < 12) {
    216 		DRM_INFO("ATIF buffer is too small: %zu\n", size);
    217 		err = -EINVAL;
    218 		goto out;
    219 	}
    220 	size = min(sizeof(output), size);
    221 
    222 	memcpy(&output, info->buffer.pointer, size);
    223 
    224 	/* TODO: check version? */
    225 	DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
    226 
    227 	radeon_atif_parse_notification(&atif->notifications, output.notification_mask);
    228 	radeon_atif_parse_functions(&atif->functions, output.function_bits);
    229 
    230 out:
    231 	kfree(info);
    232 	return err;
    233 }
    234 
    235 /**
    236  * radeon_atif_get_notification_params - determine notify configuration
    237  *
    238  * @handle: acpi handle
    239  * @n: atif notification configuration struct
    240  *
    241  * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
    242  * to determine if a notifier is used and if so which one
    243  * (all asics).  This is either Notify(VGA, 0x81) or Notify(VGA, n)
    244  * where n is specified in the result if a notifier is used.
    245  * Returns 0 on success, error on failure.
    246  */
    247 static int radeon_atif_get_notification_params(acpi_handle handle,
    248 		struct radeon_atif_notification_cfg *n)
    249 {
    250 	union acpi_object *info;
    251 	struct atif_system_params params;
    252 	size_t size;
    253 	int err = 0;
    254 
    255 	info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS, NULL);
    256 	if (!info) {
    257 		err = -EIO;
    258 		goto out;
    259 	}
    260 
    261 	size = *(u16 *) info->buffer.pointer;
    262 	if (size < 10) {
    263 		err = -EINVAL;
    264 		goto out;
    265 	}
    266 
    267 	memset(&params, 0, sizeof(params));
    268 	size = min(sizeof(params), size);
    269 	memcpy(&params, info->buffer.pointer, size);
    270 
    271 	DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
    272 			params.flags, params.valid_mask);
    273 	params.flags = params.flags & params.valid_mask;
    274 
    275 	if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
    276 		n->enabled = false;
    277 		n->command_code = 0;
    278 	} else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
    279 		n->enabled = true;
    280 		n->command_code = 0x81;
    281 	} else {
    282 		if (size < 11) {
    283 			err = -EINVAL;
    284 			goto out;
    285 		}
    286 		n->enabled = true;
    287 		n->command_code = params.command_code;
    288 	}
    289 
    290 out:
    291 	DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
    292 			(n->enabled ? "enabled" : "disabled"),
    293 			n->command_code);
    294 	kfree(info);
    295 	return err;
    296 }
    297 
    298 /**
    299  * radeon_atif_get_sbios_requests - get requested sbios event
    300  *
    301  * @handle: acpi handle
    302  * @req: atif sbios request struct
    303  *
    304  * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
    305  * to determine what requests the sbios is making to the driver
    306  * (all asics).
    307  * Returns 0 on success, error on failure.
    308  */
    309 static int radeon_atif_get_sbios_requests(acpi_handle handle,
    310 		struct atif_sbios_requests *req)
    311 {
    312 	union acpi_object *info;
    313 	size_t size;
    314 	int count = 0;
    315 
    316 	info = radeon_atif_call(handle, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS, NULL);
    317 	if (!info)
    318 		return -EIO;
    319 
    320 	size = *(u16 *)info->buffer.pointer;
    321 	if (size < 0xd) {
    322 		count = -EINVAL;
    323 		goto out;
    324 	}
    325 	memset(req, 0, sizeof(*req));
    326 
    327 	size = min(sizeof(*req), size);
    328 	memcpy(req, info->buffer.pointer, size);
    329 	DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
    330 
    331 	count = hweight32(req->pending);
    332 
    333 out:
    334 	kfree(info);
    335 	return count;
    336 }
    337 
    338 /**
    339  * radeon_atif_handler - handle ATIF notify requests
    340  *
    341  * @rdev: radeon_device pointer
    342  * @event: atif sbios request struct
    343  *
    344  * Checks the acpi event and if it matches an atif event,
    345  * handles it.
    346  * Returns NOTIFY code
    347  */
    348 int radeon_atif_handler(struct radeon_device *rdev,
    349 		struct acpi_bus_event *event)
    350 {
    351 	struct radeon_atif *atif = &rdev->atif;
    352 	struct atif_sbios_requests req;
    353 	acpi_handle handle;
    354 	int count;
    355 
    356 	DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
    357 			event->device_class, event->type);
    358 
    359 	if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
    360 		return NOTIFY_DONE;
    361 
    362 	if (!atif->notification_cfg.enabled ||
    363 			event->type != atif->notification_cfg.command_code)
    364 		/* Not our event */
    365 		return NOTIFY_DONE;
    366 
    367 	/* Check pending SBIOS requests */
    368 	handle = ACPI_HANDLE(&rdev->pdev->dev);
    369 	count = radeon_atif_get_sbios_requests(handle, &req);
    370 
    371 	if (count <= 0)
    372 		return NOTIFY_DONE;
    373 
    374 	DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
    375 
    376 	if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
    377 		struct radeon_encoder *enc = atif->encoder_for_bl;
    378 
    379 		if (enc) {
    380 			DRM_DEBUG_DRIVER("Changing brightness to %d\n",
    381 					req.backlight_level);
    382 
    383 			radeon_set_backlight_level(rdev, enc, req.backlight_level);
    384 
    385 #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
    386 			if (rdev->is_atom_bios) {
    387 				struct radeon_encoder_atom_dig *dig = enc->enc_priv;
    388 				backlight_force_update(dig->bl_dev,
    389 						       BACKLIGHT_UPDATE_HOTKEY);
    390 			} else {
    391 				struct radeon_encoder_lvds *dig = enc->enc_priv;
    392 				backlight_force_update(dig->bl_dev,
    393 						       BACKLIGHT_UPDATE_HOTKEY);
    394 			}
    395 #endif
    396 		}
    397 	}
    398 	/* TODO: check other events */
    399 
    400 	/* We've handled the event, stop the notifier chain. The ACPI interface
    401 	 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
    402 	 * userspace if the event was generated only to signal a SBIOS
    403 	 * request.
    404 	 */
    405 	return NOTIFY_BAD;
    406 }
    407 
    408 /* Call the ATCS method
    409  */
    410 /**
    411  * radeon_atcs_call - call an ATCS method
    412  *
    413  * @handle: acpi handle
    414  * @function: the ATCS function to execute
    415  * @params: ATCS function params
    416  *
    417  * Executes the requested ATCS function (all asics).
    418  * Returns a pointer to the acpi output buffer.
    419  */
    420 static union acpi_object *radeon_atcs_call(acpi_handle handle, int function,
    421 					   struct acpi_buffer *params)
    422 {
    423 	acpi_status status;
    424 	union acpi_object atcs_arg_elements[2];
    425 	struct acpi_object_list atcs_arg;
    426 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
    427 
    428 	atcs_arg.count = 2;
    429 	atcs_arg.pointer = &atcs_arg_elements[0];
    430 
    431 	atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
    432 	atcs_arg_elements[0].integer.value = function;
    433 
    434 	if (params) {
    435 		atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
    436 		atcs_arg_elements[1].buffer.length = params->length;
    437 		atcs_arg_elements[1].buffer.pointer = params->pointer;
    438 	} else {
    439 		/* We need a second fake parameter */
    440 		atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
    441 		atcs_arg_elements[1].integer.value = 0;
    442 	}
    443 
    444 	status = acpi_evaluate_object(handle, "ATCS", &atcs_arg, &buffer);
    445 
    446 	/* Fail only if calling the method fails and ATIF is supported */
    447 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
    448 		DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
    449 				 acpi_format_exception(status));
    450 		kfree(buffer.pointer);
    451 		return NULL;
    452 	}
    453 
    454 	return buffer.pointer;
    455 }
    456 
    457 /**
    458  * radeon_atcs_parse_functions - parse supported functions
    459  *
    460  * @f: supported functions struct
    461  * @mask: supported functions mask from ATCS
    462  *
    463  * Use the supported functions mask from ATCS function
    464  * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
    465  * are supported (all asics).
    466  */
    467 static void radeon_atcs_parse_functions(struct radeon_atcs_functions *f, u32 mask)
    468 {
    469 	f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
    470 	f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
    471 	f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
    472 	f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
    473 }
    474 
    475 /**
    476  * radeon_atcs_verify_interface - verify ATCS
    477  *
    478  * @handle: acpi handle
    479  * @atcs: radeon atcs struct
    480  *
    481  * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
    482  * to initialize ATCS and determine what features are supported
    483  * (all asics).
    484  * returns 0 on success, error on failure.
    485  */
    486 static int radeon_atcs_verify_interface(acpi_handle handle,
    487 					struct radeon_atcs *atcs)
    488 {
    489 	union acpi_object *info;
    490 	struct atcs_verify_interface output;
    491 	size_t size;
    492 	int err = 0;
    493 
    494 	info = radeon_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
    495 	if (!info)
    496 		return -EIO;
    497 
    498 	memset(&output, 0, sizeof(output));
    499 
    500 	size = *(u16 *) info->buffer.pointer;
    501 	if (size < 8) {
    502 		DRM_INFO("ATCS buffer is too small: %zu\n", size);
    503 		err = -EINVAL;
    504 		goto out;
    505 	}
    506 	size = min(sizeof(output), size);
    507 
    508 	memcpy(&output, info->buffer.pointer, size);
    509 
    510 	/* TODO: check version? */
    511 	DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
    512 
    513 	radeon_atcs_parse_functions(&atcs->functions, output.function_bits);
    514 
    515 out:
    516 	kfree(info);
    517 	return err;
    518 }
    519 
    520 /**
    521  * radeon_acpi_is_pcie_performance_request_supported
    522  *
    523  * @rdev: radeon_device pointer
    524  *
    525  * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
    526  * are supported (all asics).
    527  * returns true if supported, false if not.
    528  */
    529 bool radeon_acpi_is_pcie_performance_request_supported(struct radeon_device *rdev)
    530 {
    531 	struct radeon_atcs *atcs = &rdev->atcs;
    532 
    533 	if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
    534 		return true;
    535 
    536 	return false;
    537 }
    538 
    539 /**
    540  * radeon_acpi_pcie_notify_device_ready
    541  *
    542  * @rdev: radeon_device pointer
    543  *
    544  * Executes the PCIE_DEVICE_READY_NOTIFICATION method
    545  * (all asics).
    546  * returns 0 on success, error on failure.
    547  */
    548 int radeon_acpi_pcie_notify_device_ready(struct radeon_device *rdev)
    549 {
    550 	acpi_handle handle;
    551 	union acpi_object *info;
    552 	struct radeon_atcs *atcs = &rdev->atcs;
    553 
    554 	/* Get the device handle */
    555 	handle = ACPI_HANDLE(&rdev->pdev->dev);
    556 	if (!handle)
    557 		return -EINVAL;
    558 
    559 	if (!atcs->functions.pcie_dev_rdy)
    560 		return -EINVAL;
    561 
    562 	info = radeon_atcs_call(handle, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
    563 	if (!info)
    564 		return -EIO;
    565 
    566 	kfree(info);
    567 
    568 	return 0;
    569 }
    570 
    571 /**
    572  * radeon_acpi_pcie_performance_request
    573  *
    574  * @rdev: radeon_device pointer
    575  * @perf_req: requested perf level (pcie gen speed)
    576  * @advertise: set advertise caps flag if set
    577  *
    578  * Executes the PCIE_PERFORMANCE_REQUEST method to
    579  * change the pcie gen speed (all asics).
    580  * returns 0 on success, error on failure.
    581  */
    582 int radeon_acpi_pcie_performance_request(struct radeon_device *rdev,
    583 					 u8 perf_req, bool advertise)
    584 {
    585 	acpi_handle handle;
    586 	union acpi_object *info;
    587 	struct radeon_atcs *atcs = &rdev->atcs;
    588 	struct atcs_pref_req_input atcs_input;
    589 	struct atcs_pref_req_output atcs_output;
    590 	struct acpi_buffer params;
    591 	size_t size;
    592 	u32 retry = 3;
    593 
    594 	/* Get the device handle */
    595 	handle = ACPI_HANDLE(&rdev->pdev->dev);
    596 	if (!handle)
    597 		return -EINVAL;
    598 
    599 	if (!atcs->functions.pcie_perf_req)
    600 		return -EINVAL;
    601 
    602 	atcs_input.size = sizeof(struct atcs_pref_req_input);
    603 	/* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
    604 	atcs_input.client_id = rdev->pdev->devfn | (rdev->pdev->bus->number << 8);
    605 	atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
    606 	atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
    607 	if (advertise)
    608 		atcs_input.flags |= ATCS_ADVERTISE_CAPS;
    609 	atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
    610 	atcs_input.perf_req = perf_req;
    611 
    612 	params.length = sizeof(struct atcs_pref_req_input);
    613 	params.pointer = &atcs_input;
    614 
    615 	while (retry--) {
    616 		info = radeon_atcs_call(handle, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, &params);
    617 		if (!info)
    618 			return -EIO;
    619 
    620 		memset(&atcs_output, 0, sizeof(atcs_output));
    621 
    622 		size = *(u16 *) info->buffer.pointer;
    623 		if (size < 3) {
    624 			DRM_INFO("ATCS buffer is too small: %zu\n", size);
    625 			kfree(info);
    626 			return -EINVAL;
    627 		}
    628 		size = min(sizeof(atcs_output), size);
    629 
    630 		memcpy(&atcs_output, info->buffer.pointer, size);
    631 
    632 		kfree(info);
    633 
    634 		switch (atcs_output.ret_val) {
    635 		case ATCS_REQUEST_REFUSED:
    636 		default:
    637 			return -EINVAL;
    638 		case ATCS_REQUEST_COMPLETE:
    639 			return 0;
    640 		case ATCS_REQUEST_IN_PROGRESS:
    641 			udelay(10);
    642 			break;
    643 		}
    644 	}
    645 
    646 	return 0;
    647 }
    648 
    649 /**
    650  * radeon_acpi_event - handle notify events
    651  *
    652  * @nb: notifier block
    653  * @val: val
    654  * @data: acpi event
    655  *
    656  * Calls relevant radeon functions in response to various
    657  * acpi events.
    658  * Returns NOTIFY code
    659  */
    660 static int radeon_acpi_event(struct notifier_block *nb,
    661 			     unsigned long val,
    662 			     void *data)
    663 {
    664 	struct radeon_device *rdev = container_of(nb, struct radeon_device, acpi_nb);
    665 	struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
    666 
    667 	if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
    668 		if (power_supply_is_system_supplied() > 0)
    669 			DRM_DEBUG_DRIVER("pm: AC\n");
    670 		else
    671 			DRM_DEBUG_DRIVER("pm: DC\n");
    672 
    673 		radeon_pm_acpi_event_handler(rdev);
    674 	}
    675 
    676 	/* Check for pending SBIOS requests */
    677 	return radeon_atif_handler(rdev, entry);
    678 }
    679 
    680 /* Call all ACPI methods here */
    681 /**
    682  * radeon_acpi_init - init driver acpi support
    683  *
    684  * @rdev: radeon_device pointer
    685  *
    686  * Verifies the AMD ACPI interfaces and registers with the acpi
    687  * notifier chain (all asics).
    688  * Returns 0 on success, error on failure.
    689  */
    690 int radeon_acpi_init(struct radeon_device *rdev)
    691 {
    692 	acpi_handle handle;
    693 	struct radeon_atif *atif = &rdev->atif;
    694 	struct radeon_atcs *atcs = &rdev->atcs;
    695 	int ret;
    696 
    697 	/* Get the device handle */
    698 	handle = ACPI_HANDLE(&rdev->pdev->dev);
    699 
    700 	/* No need to proceed if we're sure that ATIF is not supported */
    701 	if (!ASIC_IS_AVIVO(rdev) || !rdev->bios || !handle)
    702 		return 0;
    703 
    704 	/* Call the ATCS method */
    705 	ret = radeon_atcs_verify_interface(handle, atcs);
    706 	if (ret) {
    707 		DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
    708 	}
    709 
    710 	/* Call the ATIF method */
    711 	ret = radeon_atif_verify_interface(handle, atif);
    712 	if (ret) {
    713 		DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
    714 		goto out;
    715 	}
    716 
    717 	if (atif->notifications.brightness_change) {
    718 		struct drm_encoder *tmp;
    719 		struct radeon_encoder *target = NULL;
    720 
    721 		/* Find the encoder controlling the brightness */
    722 		list_for_each_entry(tmp, &rdev->ddev->mode_config.encoder_list,
    723 				head) {
    724 			struct radeon_encoder *enc = to_radeon_encoder(tmp);
    725 
    726 			if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
    727 			    enc->enc_priv) {
    728 				if (rdev->is_atom_bios) {
    729 					struct radeon_encoder_atom_dig *dig = enc->enc_priv;
    730 					if (dig->bl_dev) {
    731 						target = enc;
    732 						break;
    733 					}
    734 				} else {
    735 					struct radeon_encoder_lvds *dig = enc->enc_priv;
    736 					if (dig->bl_dev) {
    737 						target = enc;
    738 						break;
    739 					}
    740 				}
    741 			}
    742 		}
    743 
    744 		atif->encoder_for_bl = target;
    745 		if (!target) {
    746 			/* Brightness change notification is enabled, but we
    747 			 * didn't find a backlight controller, this should
    748 			 * never happen.
    749 			 */
    750 			DRM_ERROR("Cannot find a backlight controller\n");
    751 		}
    752 	}
    753 
    754 	if (atif->functions.sbios_requests && !atif->functions.system_params) {
    755 		/* XXX check this workraround, if sbios request function is
    756 		 * present we have to see how it's configured in the system
    757 		 * params
    758 		 */
    759 		atif->functions.system_params = true;
    760 	}
    761 
    762 	if (atif->functions.system_params) {
    763 		ret = radeon_atif_get_notification_params(handle,
    764 				&atif->notification_cfg);
    765 		if (ret) {
    766 			DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
    767 					ret);
    768 			/* Disable notification */
    769 			atif->notification_cfg.enabled = false;
    770 		}
    771 	}
    772 
    773 out:
    774 	rdev->acpi_nb.notifier_call = radeon_acpi_event;
    775 	register_acpi_notifier(&rdev->acpi_nb);
    776 
    777 	return ret;
    778 }
    779 
    780 /**
    781  * radeon_acpi_fini - tear down driver acpi support
    782  *
    783  * @rdev: radeon_device pointer
    784  *
    785  * Unregisters with the acpi notifier chain (all asics).
    786  */
    787 void radeon_acpi_fini(struct radeon_device *rdev)
    788 {
    789 	unregister_acpi_notifier(&rdev->acpi_nb);
    790 }
    791