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