1617 lines · 41.7 KB · C
RawDownload
// SPDX-License-Identifier: MIT/* * Copyright 2012 Advanced Micro Devices, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * */ #include <linux/pci.h>#include <linux/acpi.h>#include <linux/backlight.h>#include <linux/slab.h>#include <linux/xarray.h>#include <linux/power_supply.h>#include <linux/pm_runtime.h>#include <linux/suspend.h>#include <acpi/video.h>#include <acpi/actbl.h> #include "amdgpu.h"#include "amdgpu_pm.h"#include "amdgpu_display.h"#include "amd_acpi.h"#include "atom.h" /* Declare GUID for AMD _DSM method for XCCs */static const guid_t amd_xcc_dsm_guid = GUID_INIT(0x8267f5d5, 0xa556, 0x44f2,						 0xb8, 0xb4, 0x45, 0x56, 0x2e,						 0x8c, 0x5b, 0xec); #define AMD_XCC_HID_START 3000#define AMD_XCC_DSM_GET_NUM_FUNCS 0#define AMD_XCC_DSM_GET_SUPP_MODE 1#define AMD_XCC_DSM_GET_XCP_MODE 2#define AMD_XCC_DSM_GET_VF_XCC_MAPPING 4#define AMD_XCC_DSM_GET_TMR_INFO 5#define AMD_XCC_DSM_NUM_FUNCS 5 #define AMD_XCC_MAX_HID 24 struct xarray numa_info_xa; /* Encapsulates the XCD acpi object information */struct amdgpu_acpi_xcc_info {	struct list_head list;	struct amdgpu_numa_info *numa_info;	uint8_t xcp_node;	uint8_t phy_id;	acpi_handle handle;}; struct amdgpu_acpi_dev_info {	struct list_head list;	struct list_head xcc_list;	uint32_t sbdf;	uint16_t supp_xcp_mode;	uint16_t xcp_mode;	uint16_t mem_mode;	uint64_t tmr_base;	uint64_t tmr_size;}; struct list_head amdgpu_acpi_dev_list; struct amdgpu_atif_notification_cfg {	bool enabled;	int command_code;}; struct amdgpu_atif_notifications {	bool thermal_state;	bool forced_power_state;	bool system_power_state;	bool brightness_change;	bool dgpu_display_event;	bool gpu_package_power_limit;}; struct amdgpu_atif_functions {	bool system_params;	bool sbios_requests;	bool temperature_change;	bool query_backlight_transfer_characteristics;	bool ready_to_undock;	bool external_gpu_information;}; struct amdgpu_atif {	acpi_handle handle; 	struct amdgpu_atif_notifications notifications;	struct amdgpu_atif_functions functions;	struct amdgpu_atif_notification_cfg notification_cfg;	struct backlight_device *bd;	struct amdgpu_dm_backlight_caps backlight_caps;}; struct amdgpu_atcs_functions {	bool get_ext_state;	bool pcie_perf_req;	bool pcie_dev_rdy;	bool pcie_bus_width;	bool get_uma_size;	bool power_shift_control;	bool set_uma_allocation_size;}; struct amdgpu_atcs {	acpi_handle handle; 	struct amdgpu_atcs_functions functions;}; static struct amdgpu_acpi_priv {	struct amdgpu_atif atif;	struct amdgpu_atcs atcs;} amdgpu_acpi_priv; /* Call the ATIF method *//** * amdgpu_atif_call - call an ATIF method * * @atif: atif structure * @function: the ATIF function to execute * @params: ATIF function params * @min_size: minimum size of the expected output buffer in bytes * * Executes the requested ATIF function (all asics). * Returns a pointer to the acpi output buffer. */static union acpi_object *amdgpu_atif_call(struct amdgpu_atif *atif,					   int function,					   struct acpi_buffer *params,					   size_t min_size){	acpi_status status;	union acpi_object *obj;	union acpi_object atif_arg_elements[2];	struct acpi_object_list atif_arg;	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 	atif_arg.count = 2;	atif_arg.pointer = &atif_arg_elements[0]; 	atif_arg_elements[0].type = ACPI_TYPE_INTEGER;	atif_arg_elements[0].integer.value = function; 	if (params) {		atif_arg_elements[1].type = ACPI_TYPE_BUFFER;		atif_arg_elements[1].buffer.length = params->length;		atif_arg_elements[1].buffer.pointer = params->pointer;	} else {		/* We need a second fake parameter */		atif_arg_elements[1].type = ACPI_TYPE_INTEGER;		atif_arg_elements[1].integer.value = 0;	} 	status = acpi_evaluate_object(atif->handle, NULL, &atif_arg,				      &buffer);	obj = (union acpi_object *)buffer.pointer; 	/* Fail if calling the method fails */	if (ACPI_FAILURE(status)) {		DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",				 acpi_format_exception(status));		kfree(obj);		return NULL;	} 	if (obj->type != ACPI_TYPE_BUFFER) {		DRM_DEBUG_DRIVER("bad object returned from ATIF: %d\n",				 obj->type);		kfree(obj);		return NULL;	} 	if (obj->buffer.length < sizeof(u16)) {		DRM_DEBUG_DRIVER("ATIF buffer too small to hold size field: %u\n",				 obj->buffer.length);		kfree(obj);		return NULL;	} 	if (obj->buffer.length < *(u16 *)obj->buffer.pointer) {		DRM_DEBUG_DRIVER("ATIF buffer length mismatch: reported %u, actual %u\n",				 *(u16 *)obj->buffer.pointer,				 obj->buffer.length);		kfree(obj);		return NULL;	} 	if (*(u16 *)obj->buffer.pointer < min_size) {		DRM_DEBUG_DRIVER("ATIF buffer too small: expected %zu, got %u\n",				 min_size, *(u16 *)obj->buffer.pointer);		kfree(obj);		return NULL;	} 	return obj;} /** * amdgpu_atif_parse_notification - parse supported notifications * * @n: supported notifications struct * @mask: supported notifications mask from ATIF * * Use the supported notifications mask from ATIF function * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications * are supported (all asics). */static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask){	n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;	n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;	n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;	n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;	n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;	n->gpu_package_power_limit = mask & ATIF_GPU_PACKAGE_POWER_LIMIT_REQUEST_SUPPORTED;} /** * amdgpu_atif_parse_functions - parse supported functions * * @f: supported functions struct * @mask: supported functions mask from ATIF * * Use the supported functions mask from ATIF function * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions * are supported (all asics). */static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask){	f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;	f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;	f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;	f->query_backlight_transfer_characteristics =		mask & ATIF_QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS_SUPPORTED;	f->ready_to_undock = mask & ATIF_READY_TO_UNDOCK_NOTIFICATION_SUPPORTED;	f->external_gpu_information = mask & ATIF_GET_EXTERNAL_GPU_INFORMATION_SUPPORTED;} /** * amdgpu_atif_verify_interface - verify ATIF * * @atif: amdgpu atif struct * * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function * to initialize ATIF and determine what features are supported * (all asics). * returns 0 on success, error on failure. */static noinline_for_stackint amdgpu_atif_verify_interface(struct amdgpu_atif *atif){	union acpi_object *info;	struct atif_verify_interface output;	size_t size;	int err = 0; 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL,				sizeof(output));	if (!info)		return -EIO; 	memset(&output, 0, sizeof(output)); 	size = min(sizeof(output), (size_t)*(u16 *)info->buffer.pointer); 	memcpy(&output, info->buffer.pointer, size); 	/* TODO: check version? */	DRM_DEBUG_DRIVER("ATIF version %u\n", output.version); 	amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);	amdgpu_atif_parse_functions(&atif->functions, output.function_bits); 	kfree(info);	return err;} /** * amdgpu_atif_get_notification_params - determine notify configuration * * @atif: acpi handle * * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function * to determine if a notifier is used and if so which one * (all asics).  This is either Notify(VGA, 0x81) or Notify(VGA, n) * where n is specified in the result if a notifier is used. * Returns 0 on success, error on failure. */static noinline_for_stackint amdgpu_atif_get_notification_params(struct amdgpu_atif *atif){	union acpi_object *info;	struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg;	struct atif_system_params params;	size_t size;	int err = 0; 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS,				NULL, offsetof(struct atif_system_params, command_code));	if (!info) {		err = -EIO;		goto out;	} 	memset(&params, 0, sizeof(params));	size = min(sizeof(params), (size_t)*(u16 *)info->buffer.pointer);	memcpy(&params, info->buffer.pointer, size); 	DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",			params.flags, params.valid_mask);	params.flags = params.flags & params.valid_mask; 	if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {		n->enabled = false;		n->command_code = 0;	} else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {		n->enabled = true;		n->command_code = 0x81;	} else {		if (size < 11) {			err = -EINVAL;			goto out;		}		n->enabled = true;		n->command_code = params.command_code;	} out:	DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",			(n->enabled ? "enabled" : "disabled"),			n->command_code);	kfree(info);	return err;} /** * amdgpu_atif_query_backlight_caps - get min and max backlight input signal * * @atif: acpi handle * * Execute the QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS ATIF function * to determine the acceptable range of backlight values * * Backlight_caps.caps_valid will be set to true if the query is successful * * The input signals are in range 0-255 * * This function assumes the display with backlight is the first LCD * * Returns 0 on success, error on failure. */static noinline_for_stackint amdgpu_atif_query_backlight_caps(struct amdgpu_atif *atif){	union acpi_object *info;	struct atif_qbtc_output characteristics;	struct atif_qbtc_arguments arguments;	struct acpi_buffer params;	size_t size;	int err = 0; 	arguments.size = sizeof(arguments);	arguments.requested_display = ATIF_QBTC_REQUEST_LCD1; 	params.length = sizeof(arguments);	params.pointer = (void *)&arguments; 	info = amdgpu_atif_call(atif,		ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS,		&params, offsetof(struct atif_qbtc_output, data_points));	if (!info) {		err = -EIO;		goto out;	} 	memset(&characteristics, 0, sizeof(characteristics));	size = min(sizeof(characteristics), (size_t)*(u16 *)info->buffer.pointer);	memcpy(&characteristics, info->buffer.pointer, size); 	atif->backlight_caps.caps_valid = true;	atif->backlight_caps.min_input_signal =			characteristics.min_input_signal;	atif->backlight_caps.max_input_signal =			characteristics.max_input_signal;	atif->backlight_caps.ac_level = characteristics.ac_level;	atif->backlight_caps.dc_level = characteristics.dc_level;	atif->backlight_caps.data_points = characteristics.number_of_points;	memcpy(atif->backlight_caps.luminance_data,	       characteristics.data_points,	       sizeof(atif->backlight_caps.luminance_data));out:	kfree(info);	return err;} /** * amdgpu_atif_get_sbios_requests - get requested sbios event * * @atif: acpi handle * @req: atif sbios request struct * * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function * to determine what requests the sbios is making to the driver * (all asics). * Returns 0 on success, error on failure. */static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif,					  struct atif_sbios_requests *req){	union acpi_object *info;	size_t size;	int count = 0; 	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS,				NULL, sizeof(*req));	if (!info)		return -EIO; 	memset(req, 0, sizeof(*req)); 	size = min(sizeof(*req), (size_t)*(u16 *)info->buffer.pointer);	memcpy(req, info->buffer.pointer, size);	DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending); 	count = hweight32(req->pending); 	kfree(info);	return count;} /** * amdgpu_atif_handler - handle ATIF notify requests * * @adev: amdgpu_device pointer * @event: atif sbios request struct * * Checks the acpi event and if it matches an atif event, * handles it. * * Returns: * NOTIFY_BAD or NOTIFY_DONE, depending on the event. */static int amdgpu_atif_handler(struct amdgpu_device *adev,			       struct acpi_bus_event *event){	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;	int count; 	DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",			event->device_class, event->type); 	if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)		return NOTIFY_DONE; 	/* Is this actually our event? */	if (!atif->notification_cfg.enabled ||	    event->type != atif->notification_cfg.command_code) {		/* These events will generate keypresses otherwise */		if (event->type == ACPI_VIDEO_NOTIFY_PROBE)			return NOTIFY_BAD;		else			return NOTIFY_DONE;	} 	if (atif->functions.sbios_requests) {		struct atif_sbios_requests req; 		/* Check pending SBIOS requests */		count = amdgpu_atif_get_sbios_requests(atif, &req); 		if (count <= 0)			return NOTIFY_BAD; 		DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count); 		if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {			if (atif->bd) {				DRM_DEBUG_DRIVER("Changing brightness to %d\n",						 req.backlight_level);

Lines 1–500 of 1617.