Home | History | Annotate | Line # | Download | only in amdgpu
      1  1.6  riastrad /*	$NetBSD: amdgpu_acpi.c,v 1.6 2024/04/16 14:34:01 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2012 Advanced Micro Devices, Inc.
      5  1.1  riastrad  *
      6  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      8  1.1  riastrad  * to deal in the Software without restriction, including without limitation
      9  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     11  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     12  1.1  riastrad  *
     13  1.1  riastrad  * The above copyright notice and this permission notice shall be included in
     14  1.1  riastrad  * all copies or substantial portions of the Software.
     15  1.1  riastrad  *
     16  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  1.1  riastrad  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     23  1.1  riastrad  *
     24  1.1  riastrad  */
     25  1.1  riastrad 
     26  1.1  riastrad #include <sys/cdefs.h>
     27  1.6  riastrad __KERNEL_RCSID(0, "$NetBSD: amdgpu_acpi.c,v 1.6 2024/04/16 14:34:01 riastradh Exp $");
     28  1.1  riastrad 
     29  1.1  riastrad #include <linux/pci.h>
     30  1.1  riastrad #include <linux/acpi.h>
     31  1.1  riastrad #include <linux/slab.h>
     32  1.1  riastrad #include <linux/power_supply.h>
     33  1.4  riastrad #include <linux/pm_runtime.h>
     34  1.1  riastrad #include <acpi/video.h>
     35  1.4  riastrad 
     36  1.1  riastrad #include <drm/drm_crtc_helper.h>
     37  1.1  riastrad #include "amdgpu.h"
     38  1.3  riastrad #include "amdgpu_pm.h"
     39  1.4  riastrad #include "amdgpu_display.h"
     40  1.4  riastrad #include "amd_acpi.h"
     41  1.1  riastrad #include "atom.h"
     42  1.1  riastrad 
     43  1.6  riastrad #ifdef __NetBSD__
     44  1.6  riastrad #include <dev/acpi/acpi_pci.h>
     45  1.6  riastrad #include <dev/acpi/acpireg.h>
     46  1.6  riastrad #define	_COMPONENT	ACPI_DISPLAY_COMPONENT
     47  1.6  riastrad ACPI_MODULE_NAME("radeon_acpi")
     48  1.6  riastrad #include <linux/nbsd-namespace-acpi.h>
     49  1.6  riastrad #endif
     50  1.6  riastrad 
     51  1.4  riastrad struct amdgpu_atif_notification_cfg {
     52  1.4  riastrad 	bool enabled;
     53  1.4  riastrad 	int command_code;
     54  1.4  riastrad };
     55  1.4  riastrad 
     56  1.4  riastrad struct amdgpu_atif_notifications {
     57  1.4  riastrad 	bool thermal_state;
     58  1.4  riastrad 	bool forced_power_state;
     59  1.4  riastrad 	bool system_power_state;
     60  1.4  riastrad 	bool brightness_change;
     61  1.4  riastrad 	bool dgpu_display_event;
     62  1.4  riastrad 	bool gpu_package_power_limit;
     63  1.4  riastrad };
     64  1.4  riastrad 
     65  1.4  riastrad struct amdgpu_atif_functions {
     66  1.4  riastrad 	bool system_params;
     67  1.4  riastrad 	bool sbios_requests;
     68  1.4  riastrad 	bool temperature_change;
     69  1.4  riastrad 	bool query_backlight_transfer_characteristics;
     70  1.4  riastrad 	bool ready_to_undock;
     71  1.4  riastrad 	bool external_gpu_information;
     72  1.4  riastrad };
     73  1.4  riastrad 
     74  1.4  riastrad struct amdgpu_atif {
     75  1.4  riastrad 	acpi_handle handle;
     76  1.1  riastrad 
     77  1.4  riastrad 	struct amdgpu_atif_notifications notifications;
     78  1.4  riastrad 	struct amdgpu_atif_functions functions;
     79  1.4  riastrad 	struct amdgpu_atif_notification_cfg notification_cfg;
     80  1.4  riastrad 	struct amdgpu_encoder *encoder_for_bl;
     81  1.4  riastrad 	struct amdgpu_dm_backlight_caps backlight_caps;
     82  1.4  riastrad };
     83  1.1  riastrad 
     84  1.1  riastrad /* Call the ATIF method
     85  1.1  riastrad  */
     86  1.1  riastrad /**
     87  1.1  riastrad  * amdgpu_atif_call - call an ATIF method
     88  1.1  riastrad  *
     89  1.1  riastrad  * @handle: acpi handle
     90  1.1  riastrad  * @function: the ATIF function to execute
     91  1.1  riastrad  * @params: ATIF function params
     92  1.1  riastrad  *
     93  1.1  riastrad  * Executes the requested ATIF function (all asics).
     94  1.1  riastrad  * Returns a pointer to the acpi output buffer.
     95  1.1  riastrad  */
     96  1.4  riastrad static union acpi_object *amdgpu_atif_call(struct amdgpu_atif *atif,
     97  1.4  riastrad 					   int function,
     98  1.4  riastrad 					   struct acpi_buffer *params)
     99  1.1  riastrad {
    100  1.1  riastrad 	acpi_status status;
    101  1.1  riastrad 	union acpi_object atif_arg_elements[2];
    102  1.1  riastrad 	struct acpi_object_list atif_arg;
    103  1.1  riastrad 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
    104  1.1  riastrad 
    105  1.1  riastrad 	atif_arg.count = 2;
    106  1.1  riastrad 	atif_arg.pointer = &atif_arg_elements[0];
    107  1.1  riastrad 
    108  1.1  riastrad 	atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
    109  1.1  riastrad 	atif_arg_elements[0].integer.value = function;
    110  1.1  riastrad 
    111  1.1  riastrad 	if (params) {
    112  1.1  riastrad 		atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
    113  1.1  riastrad 		atif_arg_elements[1].buffer.length = params->length;
    114  1.1  riastrad 		atif_arg_elements[1].buffer.pointer = params->pointer;
    115  1.1  riastrad 	} else {
    116  1.1  riastrad 		/* We need a second fake parameter */
    117  1.1  riastrad 		atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
    118  1.1  riastrad 		atif_arg_elements[1].integer.value = 0;
    119  1.1  riastrad 	}
    120  1.1  riastrad 
    121  1.4  riastrad 	status = acpi_evaluate_object(atif->handle, NULL, &atif_arg,
    122  1.4  riastrad 				      &buffer);
    123  1.1  riastrad 
    124  1.1  riastrad 	/* Fail only if calling the method fails and ATIF is supported */
    125  1.1  riastrad 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
    126  1.1  riastrad 		DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
    127  1.1  riastrad 				 acpi_format_exception(status));
    128  1.5  riastrad 		ACPI_FREE(buffer.pointer);
    129  1.1  riastrad 		return NULL;
    130  1.1  riastrad 	}
    131  1.1  riastrad 
    132  1.1  riastrad 	return buffer.pointer;
    133  1.1  riastrad }
    134  1.1  riastrad 
    135  1.1  riastrad /**
    136  1.1  riastrad  * amdgpu_atif_parse_notification - parse supported notifications
    137  1.1  riastrad  *
    138  1.1  riastrad  * @n: supported notifications struct
    139  1.1  riastrad  * @mask: supported notifications mask from ATIF
    140  1.1  riastrad  *
    141  1.1  riastrad  * Use the supported notifications mask from ATIF function
    142  1.1  riastrad  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
    143  1.1  riastrad  * are supported (all asics).
    144  1.1  riastrad  */
    145  1.1  riastrad static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
    146  1.1  riastrad {
    147  1.1  riastrad 	n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
    148  1.1  riastrad 	n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
    149  1.1  riastrad 	n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
    150  1.1  riastrad 	n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
    151  1.1  riastrad 	n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
    152  1.4  riastrad 	n->gpu_package_power_limit = mask & ATIF_GPU_PACKAGE_POWER_LIMIT_REQUEST_SUPPORTED;
    153  1.1  riastrad }
    154  1.1  riastrad 
    155  1.1  riastrad /**
    156  1.1  riastrad  * amdgpu_atif_parse_functions - parse supported functions
    157  1.1  riastrad  *
    158  1.1  riastrad  * @f: supported functions struct
    159  1.1  riastrad  * @mask: supported functions mask from ATIF
    160  1.1  riastrad  *
    161  1.1  riastrad  * Use the supported functions mask from ATIF function
    162  1.1  riastrad  * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
    163  1.1  riastrad  * are supported (all asics).
    164  1.1  riastrad  */
    165  1.1  riastrad static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
    166  1.1  riastrad {
    167  1.1  riastrad 	f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
    168  1.1  riastrad 	f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
    169  1.1  riastrad 	f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
    170  1.4  riastrad 	f->query_backlight_transfer_characteristics =
    171  1.4  riastrad 		mask & ATIF_QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS_SUPPORTED;
    172  1.4  riastrad 	f->ready_to_undock = mask & ATIF_READY_TO_UNDOCK_NOTIFICATION_SUPPORTED;
    173  1.4  riastrad 	f->external_gpu_information = mask & ATIF_GET_EXTERNAL_GPU_INFORMATION_SUPPORTED;
    174  1.1  riastrad }
    175  1.1  riastrad 
    176  1.1  riastrad /**
    177  1.1  riastrad  * amdgpu_atif_verify_interface - verify ATIF
    178  1.1  riastrad  *
    179  1.1  riastrad  * @handle: acpi handle
    180  1.1  riastrad  * @atif: amdgpu atif struct
    181  1.1  riastrad  *
    182  1.1  riastrad  * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
    183  1.1  riastrad  * to initialize ATIF and determine what features are supported
    184  1.1  riastrad  * (all asics).
    185  1.1  riastrad  * returns 0 on success, error on failure.
    186  1.1  riastrad  */
    187  1.4  riastrad static int amdgpu_atif_verify_interface(struct amdgpu_atif *atif)
    188  1.1  riastrad {
    189  1.1  riastrad 	union acpi_object *info;
    190  1.1  riastrad 	struct atif_verify_interface output;
    191  1.1  riastrad 	size_t size;
    192  1.1  riastrad 	int err = 0;
    193  1.1  riastrad 
    194  1.4  riastrad 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
    195  1.1  riastrad 	if (!info)
    196  1.1  riastrad 		return -EIO;
    197  1.1  riastrad 
    198  1.1  riastrad 	memset(&output, 0, sizeof(output));
    199  1.1  riastrad 
    200  1.1  riastrad 	size = *(u16 *) info->buffer.pointer;
    201  1.1  riastrad 	if (size < 12) {
    202  1.1  riastrad 		DRM_INFO("ATIF buffer is too small: %zu\n", size);
    203  1.1  riastrad 		err = -EINVAL;
    204  1.1  riastrad 		goto out;
    205  1.1  riastrad 	}
    206  1.1  riastrad 	size = min(sizeof(output), size);
    207  1.1  riastrad 
    208  1.1  riastrad 	memcpy(&output, info->buffer.pointer, size);
    209  1.1  riastrad 
    210  1.1  riastrad 	/* TODO: check version? */
    211  1.1  riastrad 	DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
    212  1.1  riastrad 
    213  1.1  riastrad 	amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
    214  1.1  riastrad 	amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
    215  1.1  riastrad 
    216  1.1  riastrad out:
    217  1.5  riastrad 	ACPI_FREE(info);
    218  1.1  riastrad 	return err;
    219  1.1  riastrad }
    220  1.1  riastrad 
    221  1.4  riastrad static acpi_handle amdgpu_atif_probe_handle(acpi_handle dhandle)
    222  1.4  riastrad {
    223  1.4  riastrad 	acpi_handle handle = NULL;
    224  1.4  riastrad 	char acpi_method_name[255] = { 0 };
    225  1.4  riastrad 	struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
    226  1.4  riastrad 	acpi_status status;
    227  1.4  riastrad 
    228  1.4  riastrad 	/* For PX/HG systems, ATIF and ATPX are in the iGPU's namespace, on dGPU only
    229  1.4  riastrad 	 * systems, ATIF is in the dGPU's namespace.
    230  1.4  riastrad 	 */
    231  1.4  riastrad 	status = acpi_get_handle(dhandle, "ATIF", &handle);
    232  1.4  riastrad 	if (ACPI_SUCCESS(status))
    233  1.4  riastrad 		goto out;
    234  1.4  riastrad 
    235  1.4  riastrad 	if (amdgpu_has_atpx()) {
    236  1.4  riastrad 		status = acpi_get_handle(amdgpu_atpx_get_dhandle(), "ATIF",
    237  1.4  riastrad 					 &handle);
    238  1.4  riastrad 		if (ACPI_SUCCESS(status))
    239  1.4  riastrad 			goto out;
    240  1.4  riastrad 	}
    241  1.4  riastrad 
    242  1.4  riastrad 	DRM_DEBUG_DRIVER("No ATIF handle found\n");
    243  1.4  riastrad 	return NULL;
    244  1.4  riastrad out:
    245  1.4  riastrad 	acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
    246  1.4  riastrad 	DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
    247  1.4  riastrad 	return handle;
    248  1.4  riastrad }
    249  1.4  riastrad 
    250  1.1  riastrad /**
    251  1.1  riastrad  * amdgpu_atif_get_notification_params - determine notify configuration
    252  1.1  riastrad  *
    253  1.1  riastrad  * @handle: acpi handle
    254  1.1  riastrad  * @n: atif notification configuration struct
    255  1.1  riastrad  *
    256  1.1  riastrad  * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
    257  1.1  riastrad  * to determine if a notifier is used and if so which one
    258  1.1  riastrad  * (all asics).  This is either Notify(VGA, 0x81) or Notify(VGA, n)
    259  1.1  riastrad  * where n is specified in the result if a notifier is used.
    260  1.1  riastrad  * Returns 0 on success, error on failure.
    261  1.1  riastrad  */
    262  1.4  riastrad static int amdgpu_atif_get_notification_params(struct amdgpu_atif *atif)
    263  1.1  riastrad {
    264  1.1  riastrad 	union acpi_object *info;
    265  1.4  riastrad 	struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg;
    266  1.1  riastrad 	struct atif_system_params params;
    267  1.1  riastrad 	size_t size;
    268  1.1  riastrad 	int err = 0;
    269  1.1  riastrad 
    270  1.4  riastrad 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS,
    271  1.4  riastrad 				NULL);
    272  1.1  riastrad 	if (!info) {
    273  1.1  riastrad 		err = -EIO;
    274  1.1  riastrad 		goto out;
    275  1.1  riastrad 	}
    276  1.1  riastrad 
    277  1.1  riastrad 	size = *(u16 *) info->buffer.pointer;
    278  1.1  riastrad 	if (size < 10) {
    279  1.1  riastrad 		err = -EINVAL;
    280  1.1  riastrad 		goto out;
    281  1.1  riastrad 	}
    282  1.1  riastrad 
    283  1.1  riastrad 	memset(&params, 0, sizeof(params));
    284  1.1  riastrad 	size = min(sizeof(params), size);
    285  1.1  riastrad 	memcpy(&params, info->buffer.pointer, size);
    286  1.1  riastrad 
    287  1.1  riastrad 	DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
    288  1.1  riastrad 			params.flags, params.valid_mask);
    289  1.1  riastrad 	params.flags = params.flags & params.valid_mask;
    290  1.1  riastrad 
    291  1.1  riastrad 	if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
    292  1.1  riastrad 		n->enabled = false;
    293  1.1  riastrad 		n->command_code = 0;
    294  1.1  riastrad 	} else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
    295  1.1  riastrad 		n->enabled = true;
    296  1.1  riastrad 		n->command_code = 0x81;
    297  1.1  riastrad 	} else {
    298  1.1  riastrad 		if (size < 11) {
    299  1.1  riastrad 			err = -EINVAL;
    300  1.1  riastrad 			goto out;
    301  1.1  riastrad 		}
    302  1.1  riastrad 		n->enabled = true;
    303  1.1  riastrad 		n->command_code = params.command_code;
    304  1.1  riastrad 	}
    305  1.1  riastrad 
    306  1.1  riastrad out:
    307  1.1  riastrad 	DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
    308  1.1  riastrad 			(n->enabled ? "enabled" : "disabled"),
    309  1.1  riastrad 			n->command_code);
    310  1.5  riastrad 	ACPI_FREE(info);
    311  1.1  riastrad 	return err;
    312  1.1  riastrad }
    313  1.1  riastrad 
    314  1.1  riastrad /**
    315  1.4  riastrad  * amdgpu_atif_query_backlight_caps - get min and max backlight input signal
    316  1.4  riastrad  *
    317  1.4  riastrad  * @handle: acpi handle
    318  1.4  riastrad  *
    319  1.4  riastrad  * Execute the QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS ATIF function
    320  1.4  riastrad  * to determine the acceptable range of backlight values
    321  1.4  riastrad  *
    322  1.4  riastrad  * Backlight_caps.caps_valid will be set to true if the query is successful
    323  1.4  riastrad  *
    324  1.4  riastrad  * The input signals are in range 0-255
    325  1.4  riastrad  *
    326  1.4  riastrad  * This function assumes the display with backlight is the first LCD
    327  1.4  riastrad  *
    328  1.4  riastrad  * Returns 0 on success, error on failure.
    329  1.4  riastrad  */
    330  1.4  riastrad static int amdgpu_atif_query_backlight_caps(struct amdgpu_atif *atif)
    331  1.4  riastrad {
    332  1.4  riastrad 	union acpi_object *info;
    333  1.4  riastrad 	struct atif_qbtc_output characteristics;
    334  1.4  riastrad 	struct atif_qbtc_arguments arguments;
    335  1.4  riastrad 	struct acpi_buffer params;
    336  1.4  riastrad 	size_t size;
    337  1.4  riastrad 	int err = 0;
    338  1.4  riastrad 
    339  1.4  riastrad 	arguments.size = sizeof(arguments);
    340  1.4  riastrad 	arguments.requested_display = ATIF_QBTC_REQUEST_LCD1;
    341  1.4  riastrad 
    342  1.4  riastrad 	params.length = sizeof(arguments);
    343  1.4  riastrad 	params.pointer = (void *)&arguments;
    344  1.4  riastrad 
    345  1.4  riastrad 	info = amdgpu_atif_call(atif,
    346  1.4  riastrad 		ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS,
    347  1.4  riastrad 		&params);
    348  1.4  riastrad 	if (!info) {
    349  1.4  riastrad 		err = -EIO;
    350  1.4  riastrad 		goto out;
    351  1.4  riastrad 	}
    352  1.4  riastrad 
    353  1.4  riastrad 	size = *(u16 *) info->buffer.pointer;
    354  1.4  riastrad 	if (size < 10) {
    355  1.4  riastrad 		err = -EINVAL;
    356  1.4  riastrad 		goto out;
    357  1.4  riastrad 	}
    358  1.4  riastrad 
    359  1.4  riastrad 	memset(&characteristics, 0, sizeof(characteristics));
    360  1.4  riastrad 	size = min(sizeof(characteristics), size);
    361  1.4  riastrad 	memcpy(&characteristics, info->buffer.pointer, size);
    362  1.4  riastrad 
    363  1.4  riastrad 	atif->backlight_caps.caps_valid = true;
    364  1.4  riastrad 	atif->backlight_caps.min_input_signal =
    365  1.4  riastrad 			characteristics.min_input_signal;
    366  1.4  riastrad 	atif->backlight_caps.max_input_signal =
    367  1.4  riastrad 			characteristics.max_input_signal;
    368  1.4  riastrad out:
    369  1.5  riastrad 	ACPI_FREE(info);
    370  1.4  riastrad 	return err;
    371  1.4  riastrad }
    372  1.4  riastrad 
    373  1.6  riastrad #ifndef __NetBSD__		/* XXX amdgpu acpi */
    374  1.6  riastrad 
    375  1.4  riastrad /**
    376  1.1  riastrad  * amdgpu_atif_get_sbios_requests - get requested sbios event
    377  1.1  riastrad  *
    378  1.1  riastrad  * @handle: acpi handle
    379  1.1  riastrad  * @req: atif sbios request struct
    380  1.1  riastrad  *
    381  1.1  riastrad  * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
    382  1.1  riastrad  * to determine what requests the sbios is making to the driver
    383  1.1  riastrad  * (all asics).
    384  1.1  riastrad  * Returns 0 on success, error on failure.
    385  1.1  riastrad  */
    386  1.4  riastrad static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif,
    387  1.4  riastrad 					  struct atif_sbios_requests *req)
    388  1.1  riastrad {
    389  1.1  riastrad 	union acpi_object *info;
    390  1.1  riastrad 	size_t size;
    391  1.1  riastrad 	int count = 0;
    392  1.1  riastrad 
    393  1.4  riastrad 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS,
    394  1.4  riastrad 				NULL);
    395  1.1  riastrad 	if (!info)
    396  1.1  riastrad 		return -EIO;
    397  1.1  riastrad 
    398  1.1  riastrad 	size = *(u16 *)info->buffer.pointer;
    399  1.1  riastrad 	if (size < 0xd) {
    400  1.1  riastrad 		count = -EINVAL;
    401  1.1  riastrad 		goto out;
    402  1.1  riastrad 	}
    403  1.1  riastrad 	memset(req, 0, sizeof(*req));
    404  1.1  riastrad 
    405  1.1  riastrad 	size = min(sizeof(*req), size);
    406  1.1  riastrad 	memcpy(req, info->buffer.pointer, size);
    407  1.1  riastrad 	DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
    408  1.1  riastrad 
    409  1.1  riastrad 	count = hweight32(req->pending);
    410  1.1  riastrad 
    411  1.1  riastrad out:
    412  1.5  riastrad 	ACPI_FREE(info);
    413  1.1  riastrad 	return count;
    414  1.1  riastrad }
    415  1.1  riastrad 
    416  1.1  riastrad /**
    417  1.1  riastrad  * amdgpu_atif_handler - handle ATIF notify requests
    418  1.1  riastrad  *
    419  1.1  riastrad  * @adev: amdgpu_device pointer
    420  1.1  riastrad  * @event: atif sbios request struct
    421  1.1  riastrad  *
    422  1.1  riastrad  * Checks the acpi event and if it matches an atif event,
    423  1.1  riastrad  * handles it.
    424  1.4  riastrad  *
    425  1.4  riastrad  * Returns:
    426  1.4  riastrad  * NOTIFY_BAD or NOTIFY_DONE, depending on the event.
    427  1.1  riastrad  */
    428  1.4  riastrad static int amdgpu_atif_handler(struct amdgpu_device *adev,
    429  1.4  riastrad 			       struct acpi_bus_event *event)
    430  1.1  riastrad {
    431  1.4  riastrad 	struct amdgpu_atif *atif = adev->atif;
    432  1.1  riastrad 	int count;
    433  1.1  riastrad 
    434  1.1  riastrad 	DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
    435  1.1  riastrad 			event->device_class, event->type);
    436  1.1  riastrad 
    437  1.1  riastrad 	if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
    438  1.1  riastrad 		return NOTIFY_DONE;
    439  1.1  riastrad 
    440  1.4  riastrad 	/* Is this actually our event? */
    441  1.4  riastrad 	if (!atif ||
    442  1.4  riastrad 	    !atif->notification_cfg.enabled ||
    443  1.4  riastrad 	    event->type != atif->notification_cfg.command_code) {
    444  1.4  riastrad 		/* These events will generate keypresses otherwise */
    445  1.4  riastrad 		if (event->type == ACPI_VIDEO_NOTIFY_PROBE)
    446  1.4  riastrad 			return NOTIFY_BAD;
    447  1.4  riastrad 		else
    448  1.4  riastrad 			return NOTIFY_DONE;
    449  1.4  riastrad 	}
    450  1.4  riastrad 
    451  1.4  riastrad 	if (atif->functions.sbios_requests) {
    452  1.4  riastrad 		struct atif_sbios_requests req;
    453  1.1  riastrad 
    454  1.4  riastrad 		/* Check pending SBIOS requests */
    455  1.4  riastrad 		count = amdgpu_atif_get_sbios_requests(atif, &req);
    456  1.1  riastrad 
    457  1.4  riastrad 		if (count <= 0)
    458  1.4  riastrad 			return NOTIFY_BAD;
    459  1.1  riastrad 
    460  1.4  riastrad 		DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
    461  1.1  riastrad 
    462  1.4  riastrad 		/* todo: add DC handling */
    463  1.4  riastrad 		if ((req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) &&
    464  1.4  riastrad 		    !amdgpu_device_has_dc_support(adev)) {
    465  1.4  riastrad 			struct amdgpu_encoder *enc = atif->encoder_for_bl;
    466  1.1  riastrad 
    467  1.4  riastrad 			if (enc) {
    468  1.4  riastrad 				struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
    469  1.1  riastrad 
    470  1.4  riastrad 				DRM_DEBUG_DRIVER("Changing brightness to %d\n",
    471  1.4  riastrad 						 req.backlight_level);
    472  1.1  riastrad 
    473  1.4  riastrad 				amdgpu_display_backlight_set_level(adev, enc, req.backlight_level);
    474  1.1  riastrad 
    475  1.1  riastrad #if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE)
    476  1.4  riastrad 				backlight_force_update(dig->bl_dev,
    477  1.4  riastrad 						       BACKLIGHT_UPDATE_HOTKEY);
    478  1.1  riastrad #endif
    479  1.4  riastrad 			}
    480  1.1  riastrad 		}
    481  1.4  riastrad 		if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
    482  1.4  riastrad 			if (adev->flags & AMD_IS_PX) {
    483  1.4  riastrad 				pm_runtime_get_sync(adev->ddev->dev);
    484  1.4  riastrad 				/* Just fire off a uevent and let userspace tell us what to do */
    485  1.4  riastrad 				drm_helper_hpd_irq_event(adev->ddev);
    486  1.4  riastrad 				pm_runtime_mark_last_busy(adev->ddev->dev);
    487  1.4  riastrad 				pm_runtime_put_autosuspend(adev->ddev->dev);
    488  1.4  riastrad 			}
    489  1.4  riastrad 		}
    490  1.4  riastrad 		/* TODO: check other events */
    491  1.1  riastrad 	}
    492  1.1  riastrad 
    493  1.1  riastrad 	/* We've handled the event, stop the notifier chain. The ACPI interface
    494  1.1  riastrad 	 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
    495  1.1  riastrad 	 * userspace if the event was generated only to signal a SBIOS
    496  1.1  riastrad 	 * request.
    497  1.1  riastrad 	 */
    498  1.1  riastrad 	return NOTIFY_BAD;
    499  1.1  riastrad }
    500  1.1  riastrad 
    501  1.6  riastrad #endif	/* __NetBSD__ */
    502  1.6  riastrad 
    503  1.1  riastrad /* Call the ATCS method
    504  1.1  riastrad  */
    505  1.1  riastrad /**
    506  1.1  riastrad  * amdgpu_atcs_call - call an ATCS method
    507  1.1  riastrad  *
    508  1.1  riastrad  * @handle: acpi handle
    509  1.1  riastrad  * @function: the ATCS function to execute
    510  1.1  riastrad  * @params: ATCS function params
    511  1.1  riastrad  *
    512  1.1  riastrad  * Executes the requested ATCS function (all asics).
    513  1.1  riastrad  * Returns a pointer to the acpi output buffer.
    514  1.1  riastrad  */
    515  1.1  riastrad static union acpi_object *amdgpu_atcs_call(acpi_handle handle, int function,
    516  1.1  riastrad 					   struct acpi_buffer *params)
    517  1.1  riastrad {
    518  1.1  riastrad 	acpi_status status;
    519  1.1  riastrad 	union acpi_object atcs_arg_elements[2];
    520  1.1  riastrad 	struct acpi_object_list atcs_arg;
    521  1.1  riastrad 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
    522  1.1  riastrad 
    523  1.1  riastrad 	atcs_arg.count = 2;
    524  1.1  riastrad 	atcs_arg.pointer = &atcs_arg_elements[0];
    525  1.1  riastrad 
    526  1.1  riastrad 	atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
    527  1.1  riastrad 	atcs_arg_elements[0].integer.value = function;
    528  1.1  riastrad 
    529  1.1  riastrad 	if (params) {
    530  1.1  riastrad 		atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
    531  1.1  riastrad 		atcs_arg_elements[1].buffer.length = params->length;
    532  1.1  riastrad 		atcs_arg_elements[1].buffer.pointer = params->pointer;
    533  1.1  riastrad 	} else {
    534  1.1  riastrad 		/* We need a second fake parameter */
    535  1.1  riastrad 		atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
    536  1.1  riastrad 		atcs_arg_elements[1].integer.value = 0;
    537  1.1  riastrad 	}
    538  1.1  riastrad 
    539  1.1  riastrad 	status = acpi_evaluate_object(handle, "ATCS", &atcs_arg, &buffer);
    540  1.1  riastrad 
    541  1.1  riastrad 	/* Fail only if calling the method fails and ATIF is supported */
    542  1.1  riastrad 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
    543  1.1  riastrad 		DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
    544  1.1  riastrad 				 acpi_format_exception(status));
    545  1.5  riastrad 		ACPI_FREE(buffer.pointer);
    546  1.1  riastrad 		return NULL;
    547  1.1  riastrad 	}
    548  1.1  riastrad 
    549  1.1  riastrad 	return buffer.pointer;
    550  1.1  riastrad }
    551  1.1  riastrad 
    552  1.1  riastrad /**
    553  1.1  riastrad  * amdgpu_atcs_parse_functions - parse supported functions
    554  1.1  riastrad  *
    555  1.1  riastrad  * @f: supported functions struct
    556  1.1  riastrad  * @mask: supported functions mask from ATCS
    557  1.1  riastrad  *
    558  1.1  riastrad  * Use the supported functions mask from ATCS function
    559  1.1  riastrad  * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
    560  1.1  riastrad  * are supported (all asics).
    561  1.1  riastrad  */
    562  1.1  riastrad static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
    563  1.1  riastrad {
    564  1.1  riastrad 	f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
    565  1.1  riastrad 	f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
    566  1.1  riastrad 	f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
    567  1.1  riastrad 	f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
    568  1.1  riastrad }
    569  1.1  riastrad 
    570  1.1  riastrad /**
    571  1.1  riastrad  * amdgpu_atcs_verify_interface - verify ATCS
    572  1.1  riastrad  *
    573  1.1  riastrad  * @handle: acpi handle
    574  1.1  riastrad  * @atcs: amdgpu atcs struct
    575  1.1  riastrad  *
    576  1.1  riastrad  * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
    577  1.1  riastrad  * to initialize ATCS and determine what features are supported
    578  1.1  riastrad  * (all asics).
    579  1.1  riastrad  * returns 0 on success, error on failure.
    580  1.1  riastrad  */
    581  1.1  riastrad static int amdgpu_atcs_verify_interface(acpi_handle handle,
    582  1.1  riastrad 					struct amdgpu_atcs *atcs)
    583  1.1  riastrad {
    584  1.1  riastrad 	union acpi_object *info;
    585  1.1  riastrad 	struct atcs_verify_interface output;
    586  1.1  riastrad 	size_t size;
    587  1.1  riastrad 	int err = 0;
    588  1.1  riastrad 
    589  1.1  riastrad 	info = amdgpu_atcs_call(handle, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
    590  1.1  riastrad 	if (!info)
    591  1.1  riastrad 		return -EIO;
    592  1.1  riastrad 
    593  1.1  riastrad 	memset(&output, 0, sizeof(output));
    594  1.1  riastrad 
    595  1.1  riastrad 	size = *(u16 *) info->buffer.pointer;
    596  1.1  riastrad 	if (size < 8) {
    597  1.1  riastrad 		DRM_INFO("ATCS buffer is too small: %zu\n", size);
    598  1.1  riastrad 		err = -EINVAL;
    599  1.1  riastrad 		goto out;
    600  1.1  riastrad 	}
    601  1.1  riastrad 	size = min(sizeof(output), size);
    602  1.1  riastrad 
    603  1.1  riastrad 	memcpy(&output, info->buffer.pointer, size);
    604  1.1  riastrad 
    605  1.1  riastrad 	/* TODO: check version? */
    606  1.1  riastrad 	DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
    607  1.1  riastrad 
    608  1.1  riastrad 	amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
    609  1.1  riastrad 
    610  1.1  riastrad out:
    611  1.5  riastrad 	ACPI_FREE(info);
    612  1.1  riastrad 	return err;
    613  1.1  riastrad }
    614  1.1  riastrad 
    615  1.1  riastrad /**
    616  1.1  riastrad  * amdgpu_acpi_is_pcie_performance_request_supported
    617  1.1  riastrad  *
    618  1.1  riastrad  * @adev: amdgpu_device pointer
    619  1.1  riastrad  *
    620  1.1  riastrad  * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
    621  1.1  riastrad  * are supported (all asics).
    622  1.1  riastrad  * returns true if supported, false if not.
    623  1.1  riastrad  */
    624  1.1  riastrad bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
    625  1.1  riastrad {
    626  1.1  riastrad 	struct amdgpu_atcs *atcs = &adev->atcs;
    627  1.1  riastrad 
    628  1.1  riastrad 	if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
    629  1.1  riastrad 		return true;
    630  1.1  riastrad 
    631  1.1  riastrad 	return false;
    632  1.1  riastrad }
    633  1.1  riastrad 
    634  1.1  riastrad /**
    635  1.1  riastrad  * amdgpu_acpi_pcie_notify_device_ready
    636  1.1  riastrad  *
    637  1.1  riastrad  * @adev: amdgpu_device pointer
    638  1.1  riastrad  *
    639  1.1  riastrad  * Executes the PCIE_DEVICE_READY_NOTIFICATION method
    640  1.1  riastrad  * (all asics).
    641  1.1  riastrad  * returns 0 on success, error on failure.
    642  1.1  riastrad  */
    643  1.1  riastrad int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
    644  1.1  riastrad {
    645  1.1  riastrad 	acpi_handle handle;
    646  1.1  riastrad 	union acpi_object *info;
    647  1.1  riastrad 	struct amdgpu_atcs *atcs = &adev->atcs;
    648  1.1  riastrad 
    649  1.1  riastrad 	/* Get the device handle */
    650  1.6  riastrad #ifdef __NetBSD__
    651  1.6  riastrad 	const struct pci_attach_args *pa = &adev->pdev->pd_pa;
    652  1.6  riastrad 	struct acpi_devnode *const d =
    653  1.6  riastrad 	    acpi_pcidev_find(pci_get_segment(pa->pa_pc),
    654  1.6  riastrad 		pa->pa_bus, pa->pa_device, pa->pa_function);
    655  1.6  riastrad 	if (d == NULL)
    656  1.6  riastrad 		return -EINVAL;
    657  1.6  riastrad 	handle = d->ad_handle;
    658  1.6  riastrad #else
    659  1.1  riastrad 	handle = ACPI_HANDLE(&adev->pdev->dev);
    660  1.6  riastrad #endif
    661  1.1  riastrad 	if (!handle)
    662  1.1  riastrad 		return -EINVAL;
    663  1.1  riastrad 
    664  1.1  riastrad 	if (!atcs->functions.pcie_dev_rdy)
    665  1.1  riastrad 		return -EINVAL;
    666  1.1  riastrad 
    667  1.1  riastrad 	info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
    668  1.1  riastrad 	if (!info)
    669  1.1  riastrad 		return -EIO;
    670  1.1  riastrad 
    671  1.5  riastrad 	ACPI_FREE(info);
    672  1.1  riastrad 
    673  1.1  riastrad 	return 0;
    674  1.1  riastrad }
    675  1.1  riastrad 
    676  1.1  riastrad /**
    677  1.1  riastrad  * amdgpu_acpi_pcie_performance_request
    678  1.1  riastrad  *
    679  1.1  riastrad  * @adev: amdgpu_device pointer
    680  1.1  riastrad  * @perf_req: requested perf level (pcie gen speed)
    681  1.1  riastrad  * @advertise: set advertise caps flag if set
    682  1.1  riastrad  *
    683  1.1  riastrad  * Executes the PCIE_PERFORMANCE_REQUEST method to
    684  1.1  riastrad  * change the pcie gen speed (all asics).
    685  1.1  riastrad  * returns 0 on success, error on failure.
    686  1.1  riastrad  */
    687  1.1  riastrad int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
    688  1.1  riastrad 					 u8 perf_req, bool advertise)
    689  1.1  riastrad {
    690  1.1  riastrad 	acpi_handle handle;
    691  1.1  riastrad 	union acpi_object *info;
    692  1.1  riastrad 	struct amdgpu_atcs *atcs = &adev->atcs;
    693  1.1  riastrad 	struct atcs_pref_req_input atcs_input;
    694  1.1  riastrad 	struct atcs_pref_req_output atcs_output;
    695  1.1  riastrad 	struct acpi_buffer params;
    696  1.1  riastrad 	size_t size;
    697  1.1  riastrad 	u32 retry = 3;
    698  1.1  riastrad 
    699  1.1  riastrad 	if (amdgpu_acpi_pcie_notify_device_ready(adev))
    700  1.1  riastrad 		return -EINVAL;
    701  1.1  riastrad 
    702  1.1  riastrad 	/* Get the device handle */
    703  1.6  riastrad #ifdef __NetBSD__
    704  1.6  riastrad 	const struct pci_attach_args *pa = &adev->pdev->pd_pa;
    705  1.6  riastrad 	struct acpi_devnode *const d =
    706  1.6  riastrad 	    acpi_pcidev_find(pci_get_segment(pa->pa_pc),
    707  1.6  riastrad 		pa->pa_bus, pa->pa_device, pa->pa_function);
    708  1.6  riastrad 	if (d == NULL)
    709  1.6  riastrad 		return -EINVAL;
    710  1.6  riastrad 	handle = d->ad_handle;
    711  1.6  riastrad #else
    712  1.1  riastrad 	handle = ACPI_HANDLE(&adev->pdev->dev);
    713  1.6  riastrad #endif
    714  1.1  riastrad 	if (!handle)
    715  1.1  riastrad 		return -EINVAL;
    716  1.1  riastrad 
    717  1.1  riastrad 	if (!atcs->functions.pcie_perf_req)
    718  1.1  riastrad 		return -EINVAL;
    719  1.1  riastrad 
    720  1.1  riastrad 	atcs_input.size = sizeof(struct atcs_pref_req_input);
    721  1.1  riastrad 	/* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
    722  1.1  riastrad 	atcs_input.client_id = adev->pdev->devfn | (adev->pdev->bus->number << 8);
    723  1.1  riastrad 	atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
    724  1.1  riastrad 	atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
    725  1.1  riastrad 	if (advertise)
    726  1.1  riastrad 		atcs_input.flags |= ATCS_ADVERTISE_CAPS;
    727  1.1  riastrad 	atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
    728  1.1  riastrad 	atcs_input.perf_req = perf_req;
    729  1.1  riastrad 
    730  1.1  riastrad 	params.length = sizeof(struct atcs_pref_req_input);
    731  1.1  riastrad 	params.pointer = &atcs_input;
    732  1.1  riastrad 
    733  1.1  riastrad 	while (retry--) {
    734  1.1  riastrad 		info = amdgpu_atcs_call(handle, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, &params);
    735  1.1  riastrad 		if (!info)
    736  1.1  riastrad 			return -EIO;
    737  1.1  riastrad 
    738  1.1  riastrad 		memset(&atcs_output, 0, sizeof(atcs_output));
    739  1.1  riastrad 
    740  1.1  riastrad 		size = *(u16 *) info->buffer.pointer;
    741  1.1  riastrad 		if (size < 3) {
    742  1.1  riastrad 			DRM_INFO("ATCS buffer is too small: %zu\n", size);
    743  1.5  riastrad 			ACPI_FREE(info);
    744  1.1  riastrad 			return -EINVAL;
    745  1.1  riastrad 		}
    746  1.1  riastrad 		size = min(sizeof(atcs_output), size);
    747  1.1  riastrad 
    748  1.1  riastrad 		memcpy(&atcs_output, info->buffer.pointer, size);
    749  1.1  riastrad 
    750  1.5  riastrad 		ACPI_FREE(info);
    751  1.1  riastrad 
    752  1.1  riastrad 		switch (atcs_output.ret_val) {
    753  1.1  riastrad 		case ATCS_REQUEST_REFUSED:
    754  1.1  riastrad 		default:
    755  1.1  riastrad 			return -EINVAL;
    756  1.1  riastrad 		case ATCS_REQUEST_COMPLETE:
    757  1.1  riastrad 			return 0;
    758  1.1  riastrad 		case ATCS_REQUEST_IN_PROGRESS:
    759  1.1  riastrad 			udelay(10);
    760  1.1  riastrad 			break;
    761  1.1  riastrad 		}
    762  1.1  riastrad 	}
    763  1.1  riastrad 
    764  1.1  riastrad 	return 0;
    765  1.1  riastrad }
    766  1.1  riastrad 
    767  1.1  riastrad /**
    768  1.1  riastrad  * amdgpu_acpi_event - handle notify events
    769  1.1  riastrad  *
    770  1.1  riastrad  * @nb: notifier block
    771  1.1  riastrad  * @val: val
    772  1.1  riastrad  * @data: acpi event
    773  1.1  riastrad  *
    774  1.1  riastrad  * Calls relevant amdgpu functions in response to various
    775  1.1  riastrad  * acpi events.
    776  1.1  riastrad  * Returns NOTIFY code
    777  1.1  riastrad  */
    778  1.6  riastrad #ifndef __NetBSD__		/* XXX amdgpu acpi */
    779  1.1  riastrad static int amdgpu_acpi_event(struct notifier_block *nb,
    780  1.1  riastrad 			     unsigned long val,
    781  1.1  riastrad 			     void *data)
    782  1.1  riastrad {
    783  1.1  riastrad 	struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
    784  1.1  riastrad 	struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
    785  1.1  riastrad 
    786  1.1  riastrad 	if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
    787  1.1  riastrad 		if (power_supply_is_system_supplied() > 0)
    788  1.1  riastrad 			DRM_DEBUG_DRIVER("pm: AC\n");
    789  1.1  riastrad 		else
    790  1.1  riastrad 			DRM_DEBUG_DRIVER("pm: DC\n");
    791  1.1  riastrad 
    792  1.1  riastrad 		amdgpu_pm_acpi_event_handler(adev);
    793  1.1  riastrad 	}
    794  1.1  riastrad 
    795  1.1  riastrad 	/* Check for pending SBIOS requests */
    796  1.1  riastrad 	return amdgpu_atif_handler(adev, entry);
    797  1.1  riastrad }
    798  1.6  riastrad #endif
    799  1.1  riastrad 
    800  1.1  riastrad /* Call all ACPI methods here */
    801  1.1  riastrad /**
    802  1.1  riastrad  * amdgpu_acpi_init - init driver acpi support
    803  1.1  riastrad  *
    804  1.1  riastrad  * @adev: amdgpu_device pointer
    805  1.1  riastrad  *
    806  1.1  riastrad  * Verifies the AMD ACPI interfaces and registers with the acpi
    807  1.1  riastrad  * notifier chain (all asics).
    808  1.1  riastrad  * Returns 0 on success, error on failure.
    809  1.1  riastrad  */
    810  1.1  riastrad int amdgpu_acpi_init(struct amdgpu_device *adev)
    811  1.1  riastrad {
    812  1.4  riastrad 	acpi_handle handle, atif_handle;
    813  1.4  riastrad 	struct amdgpu_atif *atif;
    814  1.1  riastrad 	struct amdgpu_atcs *atcs = &adev->atcs;
    815  1.1  riastrad 	int ret;
    816  1.1  riastrad 
    817  1.1  riastrad 	/* Get the device handle */
    818  1.6  riastrad #ifdef __NetBSD__
    819  1.6  riastrad 	const struct pci_attach_args *pa = &adev->pdev->pd_pa;
    820  1.6  riastrad 	struct acpi_devnode *const d =
    821  1.6  riastrad 	    acpi_pcidev_find(pci_get_segment(pa->pa_pc),
    822  1.6  riastrad 		pa->pa_bus, pa->pa_device, pa->pa_function);
    823  1.6  riastrad 	if (d == NULL)
    824  1.6  riastrad 		return -EINVAL;
    825  1.6  riastrad 	handle = d->ad_handle;
    826  1.6  riastrad #else
    827  1.1  riastrad 	handle = ACPI_HANDLE(&adev->pdev->dev);
    828  1.6  riastrad #endif
    829  1.1  riastrad 
    830  1.1  riastrad 	if (!adev->bios || !handle)
    831  1.1  riastrad 		return 0;
    832  1.1  riastrad 
    833  1.1  riastrad 	/* Call the ATCS method */
    834  1.1  riastrad 	ret = amdgpu_atcs_verify_interface(handle, atcs);
    835  1.1  riastrad 	if (ret) {
    836  1.1  riastrad 		DRM_DEBUG_DRIVER("Call to ATCS verify_interface failed: %d\n", ret);
    837  1.1  riastrad 	}
    838  1.1  riastrad 
    839  1.4  riastrad 	/* Probe for ATIF, and initialize it if found */
    840  1.4  riastrad 	atif_handle = amdgpu_atif_probe_handle(handle);
    841  1.4  riastrad 	if (!atif_handle)
    842  1.4  riastrad 		goto out;
    843  1.4  riastrad 
    844  1.4  riastrad 	atif = kzalloc(sizeof(*atif), GFP_KERNEL);
    845  1.4  riastrad 	if (!atif) {
    846  1.4  riastrad 		DRM_WARN("Not enough memory to initialize ATIF\n");
    847  1.4  riastrad 		goto out;
    848  1.4  riastrad 	}
    849  1.4  riastrad 	atif->handle = atif_handle;
    850  1.4  riastrad 
    851  1.1  riastrad 	/* Call the ATIF method */
    852  1.4  riastrad 	ret = amdgpu_atif_verify_interface(atif);
    853  1.1  riastrad 	if (ret) {
    854  1.1  riastrad 		DRM_DEBUG_DRIVER("Call to ATIF verify_interface failed: %d\n", ret);
    855  1.4  riastrad 		kfree(atif);
    856  1.1  riastrad 		goto out;
    857  1.1  riastrad 	}
    858  1.4  riastrad 	adev->atif = atif;
    859  1.1  riastrad 
    860  1.1  riastrad 	if (atif->notifications.brightness_change) {
    861  1.1  riastrad 		struct drm_encoder *tmp;
    862  1.1  riastrad 
    863  1.1  riastrad 		/* Find the encoder controlling the brightness */
    864  1.1  riastrad 		list_for_each_entry(tmp, &adev->ddev->mode_config.encoder_list,
    865  1.1  riastrad 				head) {
    866  1.1  riastrad 			struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
    867  1.1  riastrad 
    868  1.1  riastrad 			if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
    869  1.1  riastrad 			    enc->enc_priv) {
    870  1.4  riastrad 				struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
    871  1.4  riastrad 				if (dig->bl_dev) {
    872  1.4  riastrad 					atif->encoder_for_bl = enc;
    873  1.4  riastrad 					break;
    874  1.1  riastrad 				}
    875  1.1  riastrad 			}
    876  1.1  riastrad 		}
    877  1.1  riastrad 	}
    878  1.1  riastrad 
    879  1.1  riastrad 	if (atif->functions.sbios_requests && !atif->functions.system_params) {
    880  1.1  riastrad 		/* XXX check this workraround, if sbios request function is
    881  1.1  riastrad 		 * present we have to see how it's configured in the system
    882  1.1  riastrad 		 * params
    883  1.1  riastrad 		 */
    884  1.1  riastrad 		atif->functions.system_params = true;
    885  1.1  riastrad 	}
    886  1.1  riastrad 
    887  1.1  riastrad 	if (atif->functions.system_params) {
    888  1.4  riastrad 		ret = amdgpu_atif_get_notification_params(atif);
    889  1.1  riastrad 		if (ret) {
    890  1.1  riastrad 			DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
    891  1.1  riastrad 					ret);
    892  1.1  riastrad 			/* Disable notification */
    893  1.1  riastrad 			atif->notification_cfg.enabled = false;
    894  1.1  riastrad 		}
    895  1.1  riastrad 	}
    896  1.1  riastrad 
    897  1.4  riastrad 	if (atif->functions.query_backlight_transfer_characteristics) {
    898  1.4  riastrad 		ret = amdgpu_atif_query_backlight_caps(atif);
    899  1.4  riastrad 		if (ret) {
    900  1.4  riastrad 			DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n",
    901  1.4  riastrad 					ret);
    902  1.4  riastrad 			atif->backlight_caps.caps_valid = false;
    903  1.4  riastrad 		}
    904  1.4  riastrad 	} else {
    905  1.4  riastrad 		atif->backlight_caps.caps_valid = false;
    906  1.4  riastrad 	}
    907  1.4  riastrad 
    908  1.1  riastrad out:
    909  1.6  riastrad #ifndef __NetBSD__		/* XXX amdgpu acpi */
    910  1.1  riastrad 	adev->acpi_nb.notifier_call = amdgpu_acpi_event;
    911  1.1  riastrad 	register_acpi_notifier(&adev->acpi_nb);
    912  1.6  riastrad #endif
    913  1.1  riastrad 
    914  1.1  riastrad 	return ret;
    915  1.1  riastrad }
    916  1.1  riastrad 
    917  1.4  riastrad void amdgpu_acpi_get_backlight_caps(struct amdgpu_device *adev,
    918  1.4  riastrad 		struct amdgpu_dm_backlight_caps *caps)
    919  1.4  riastrad {
    920  1.4  riastrad 	if (!adev->atif) {
    921  1.4  riastrad 		caps->caps_valid = false;
    922  1.4  riastrad 		return;
    923  1.4  riastrad 	}
    924  1.4  riastrad 	caps->caps_valid = adev->atif->backlight_caps.caps_valid;
    925  1.4  riastrad 	caps->min_input_signal = adev->atif->backlight_caps.min_input_signal;
    926  1.4  riastrad 	caps->max_input_signal = adev->atif->backlight_caps.max_input_signal;
    927  1.4  riastrad }
    928  1.4  riastrad 
    929  1.1  riastrad /**
    930  1.1  riastrad  * amdgpu_acpi_fini - tear down driver acpi support
    931  1.1  riastrad  *
    932  1.1  riastrad  * @adev: amdgpu_device pointer
    933  1.1  riastrad  *
    934  1.1  riastrad  * Unregisters with the acpi notifier chain (all asics).
    935  1.1  riastrad  */
    936  1.1  riastrad void amdgpu_acpi_fini(struct amdgpu_device *adev)
    937  1.1  riastrad {
    938  1.6  riastrad #ifndef __NetBSD__		/* XXX radeon acpi */
    939  1.1  riastrad 	unregister_acpi_notifier(&adev->acpi_nb);
    940  1.6  riastrad #endif
    941  1.4  riastrad 	kfree(adev->atif);
    942  1.1  riastrad }
    943