File Index
Files and directories
Sign in
FILE

amdgpu_acpi.c

File preview with syntax highlighting.

linux-6.19.14/drivers/gpu/drm/amd/amdgpu/amdgpu_acpi.c C
   1// SPDX-License-Identifier: MIT
   2/*
   3 * Copyright 2012 Advanced Micro Devices, Inc.
   4 *
   5 * Permission is hereby granted, free of charge, to any person obtaining a
   6 * copy of this software and associated documentation files (the "Software"),
   7 * to deal in the Software without restriction, including without limitation
   8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
   9 * and/or sell copies of the Software, and to permit persons to whom the
  10 * Software is furnished to do so, subject to the following conditions:
  11 *
  12 * The above copyright notice and this permission notice shall be included in
  13 * all copies or substantial portions of the Software.
  14 *
  15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21 * OTHER DEALINGS IN THE SOFTWARE.
  22 *
  23 */
  24
  25#include <linux/pci.h>
  26#include <linux/acpi.h>
  27#include <linux/backlight.h>
  28#include <linux/slab.h>
  29#include <linux/xarray.h>
  30#include <linux/power_supply.h>
  31#include <linux/pm_runtime.h>
  32#include <linux/suspend.h>
  33#include <acpi/video.h>
  34#include <acpi/actbl.h>
  35
  36#include "amdgpu.h"
  37#include "amdgpu_pm.h"
  38#include "amdgpu_display.h"
  39#include "amd_acpi.h"
  40#include "atom.h"
  41
  42/* Declare GUID for AMD _DSM method for XCCs */
  43static const guid_t amd_xcc_dsm_guid = GUID_INIT(0x8267f5d5, 0xa556, 0x44f2,
  44						 0xb8, 0xb4, 0x45, 0x56, 0x2e,
  45						 0x8c, 0x5b, 0xec);
  46
  47#define AMD_XCC_HID_START 3000
  48#define AMD_XCC_DSM_GET_NUM_FUNCS 0
  49#define AMD_XCC_DSM_GET_SUPP_MODE 1
  50#define AMD_XCC_DSM_GET_XCP_MODE 2
  51#define AMD_XCC_DSM_GET_VF_XCC_MAPPING 4
  52#define AMD_XCC_DSM_GET_TMR_INFO 5
  53#define AMD_XCC_DSM_NUM_FUNCS 5
  54
  55#define AMD_XCC_MAX_HID 24
  56
  57struct xarray numa_info_xa;
  58
  59/* Encapsulates the XCD acpi object information */
  60struct amdgpu_acpi_xcc_info {
  61	struct list_head list;
  62	struct amdgpu_numa_info *numa_info;
  63	uint8_t xcp_node;
  64	uint8_t phy_id;
  65	acpi_handle handle;
  66};
  67
  68struct amdgpu_acpi_dev_info {
  69	struct list_head list;
  70	struct list_head xcc_list;
  71	uint32_t sbdf;
  72	uint16_t supp_xcp_mode;
  73	uint16_t xcp_mode;
  74	uint16_t mem_mode;
  75	uint64_t tmr_base;
  76	uint64_t tmr_size;
  77};
  78
  79struct list_head amdgpu_acpi_dev_list;
  80
  81struct amdgpu_atif_notification_cfg {
  82	bool enabled;
  83	int command_code;
  84};
  85
  86struct amdgpu_atif_notifications {
  87	bool thermal_state;
  88	bool forced_power_state;
  89	bool system_power_state;
  90	bool brightness_change;
  91	bool dgpu_display_event;
  92	bool gpu_package_power_limit;
  93};
  94
  95struct amdgpu_atif_functions {
  96	bool system_params;
  97	bool sbios_requests;
  98	bool temperature_change;
  99	bool query_backlight_transfer_characteristics;
 100	bool ready_to_undock;
 101	bool external_gpu_information;
 102};
 103
 104struct amdgpu_atif {
 105	acpi_handle handle;
 106
 107	struct amdgpu_atif_notifications notifications;
 108	struct amdgpu_atif_functions functions;
 109	struct amdgpu_atif_notification_cfg notification_cfg;
 110	struct backlight_device *bd;
 111	struct amdgpu_dm_backlight_caps backlight_caps;
 112};
 113
 114struct amdgpu_atcs_functions {
 115	bool get_ext_state;
 116	bool pcie_perf_req;
 117	bool pcie_dev_rdy;
 118	bool pcie_bus_width;
 119	bool power_shift_control;
 120};
 121
 122struct amdgpu_atcs {
 123	acpi_handle handle;
 124
 125	struct amdgpu_atcs_functions functions;
 126};
 127
 128static struct amdgpu_acpi_priv {
 129	struct amdgpu_atif atif;
 130	struct amdgpu_atcs atcs;
 131} amdgpu_acpi_priv;
 132
 133/* Call the ATIF method
 134 */
 135/**
 136 * amdgpu_atif_call - call an ATIF method
 137 *
 138 * @atif: atif structure
 139 * @function: the ATIF function to execute
 140 * @params: ATIF function params
 141 *
 142 * Executes the requested ATIF function (all asics).
 143 * Returns a pointer to the acpi output buffer.
 144 */
 145static union acpi_object *amdgpu_atif_call(struct amdgpu_atif *atif,
 146					   int function,
 147					   struct acpi_buffer *params)
 148{
 149	acpi_status status;
 150	union acpi_object *obj;
 151	union acpi_object atif_arg_elements[2];
 152	struct acpi_object_list atif_arg;
 153	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
 154
 155	atif_arg.count = 2;
 156	atif_arg.pointer = &atif_arg_elements[0];
 157
 158	atif_arg_elements[0].type = ACPI_TYPE_INTEGER;
 159	atif_arg_elements[0].integer.value = function;
 160
 161	if (params) {
 162		atif_arg_elements[1].type = ACPI_TYPE_BUFFER;
 163		atif_arg_elements[1].buffer.length = params->length;
 164		atif_arg_elements[1].buffer.pointer = params->pointer;
 165	} else {
 166		/* We need a second fake parameter */
 167		atif_arg_elements[1].type = ACPI_TYPE_INTEGER;
 168		atif_arg_elements[1].integer.value = 0;
 169	}
 170
 171	status = acpi_evaluate_object(atif->handle, NULL, &atif_arg,
 172				      &buffer);
 173	obj = (union acpi_object *)buffer.pointer;
 174
 175	/* Fail if calling the method fails */
 176	if (ACPI_FAILURE(status)) {
 177		DRM_DEBUG_DRIVER("failed to evaluate ATIF got %s\n",
 178				 acpi_format_exception(status));
 179		kfree(obj);
 180		return NULL;
 181	}
 182
 183	if (obj->type != ACPI_TYPE_BUFFER) {
 184		DRM_DEBUG_DRIVER("bad object returned from ATIF: %d\n",
 185				 obj->type);
 186		kfree(obj);
 187		return NULL;
 188	}
 189
 190	return obj;
 191}
 192
 193/**
 194 * amdgpu_atif_parse_notification - parse supported notifications
 195 *
 196 * @n: supported notifications struct
 197 * @mask: supported notifications mask from ATIF
 198 *
 199 * Use the supported notifications mask from ATIF function
 200 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what notifications
 201 * are supported (all asics).
 202 */
 203static void amdgpu_atif_parse_notification(struct amdgpu_atif_notifications *n, u32 mask)
 204{
 205	n->thermal_state = mask & ATIF_THERMAL_STATE_CHANGE_REQUEST_SUPPORTED;
 206	n->forced_power_state = mask & ATIF_FORCED_POWER_STATE_CHANGE_REQUEST_SUPPORTED;
 207	n->system_power_state = mask & ATIF_SYSTEM_POWER_SOURCE_CHANGE_REQUEST_SUPPORTED;
 208	n->brightness_change = mask & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST_SUPPORTED;
 209	n->dgpu_display_event = mask & ATIF_DGPU_DISPLAY_EVENT_SUPPORTED;
 210	n->gpu_package_power_limit = mask & ATIF_GPU_PACKAGE_POWER_LIMIT_REQUEST_SUPPORTED;
 211}
 212
 213/**
 214 * amdgpu_atif_parse_functions - parse supported functions
 215 *
 216 * @f: supported functions struct
 217 * @mask: supported functions mask from ATIF
 218 *
 219 * Use the supported functions mask from ATIF function
 220 * ATIF_FUNCTION_VERIFY_INTERFACE to determine what functions
 221 * are supported (all asics).
 222 */
 223static void amdgpu_atif_parse_functions(struct amdgpu_atif_functions *f, u32 mask)
 224{
 225	f->system_params = mask & ATIF_GET_SYSTEM_PARAMETERS_SUPPORTED;
 226	f->sbios_requests = mask & ATIF_GET_SYSTEM_BIOS_REQUESTS_SUPPORTED;
 227	f->temperature_change = mask & ATIF_TEMPERATURE_CHANGE_NOTIFICATION_SUPPORTED;
 228	f->query_backlight_transfer_characteristics =
 229		mask & ATIF_QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS_SUPPORTED;
 230	f->ready_to_undock = mask & ATIF_READY_TO_UNDOCK_NOTIFICATION_SUPPORTED;
 231	f->external_gpu_information = mask & ATIF_GET_EXTERNAL_GPU_INFORMATION_SUPPORTED;
 232}
 233
 234/**
 235 * amdgpu_atif_verify_interface - verify ATIF
 236 *
 237 * @atif: amdgpu atif struct
 238 *
 239 * Execute the ATIF_FUNCTION_VERIFY_INTERFACE ATIF function
 240 * to initialize ATIF and determine what features are supported
 241 * (all asics).
 242 * returns 0 on success, error on failure.
 243 */
 244static int amdgpu_atif_verify_interface(struct amdgpu_atif *atif)
 245{
 246	union acpi_object *info;
 247	struct atif_verify_interface output;
 248	size_t size;
 249	int err = 0;
 250
 251	info = amdgpu_atif_call(atif, ATIF_FUNCTION_VERIFY_INTERFACE, NULL);
 252	if (!info)
 253		return -EIO;
 254
 255	memset(&output, 0, sizeof(output));
 256
 257	size = *(u16 *) info->buffer.pointer;
 258	if (size < 12) {
 259		DRM_INFO("ATIF buffer is too small: %zu\n", size);
 260		err = -EINVAL;
 261		goto out;
 262	}
 263	size = min(sizeof(output), size);
 264
 265	memcpy(&output, info->buffer.pointer, size);
 266
 267	/* TODO: check version? */
 268	DRM_DEBUG_DRIVER("ATIF version %u\n", output.version);
 269
 270	amdgpu_atif_parse_notification(&atif->notifications, output.notification_mask);
 271	amdgpu_atif_parse_functions(&atif->functions, output.function_bits);
 272
 273out:
 274	kfree(info);
 275	return err;
 276}
 277
 278/**
 279 * amdgpu_atif_get_notification_params - determine notify configuration
 280 *
 281 * @atif: acpi handle
 282 *
 283 * Execute the ATIF_FUNCTION_GET_SYSTEM_PARAMETERS ATIF function
 284 * to determine if a notifier is used and if so which one
 285 * (all asics).  This is either Notify(VGA, 0x81) or Notify(VGA, n)
 286 * where n is specified in the result if a notifier is used.
 287 * Returns 0 on success, error on failure.
 288 */
 289static int amdgpu_atif_get_notification_params(struct amdgpu_atif *atif)
 290{
 291	union acpi_object *info;
 292	struct amdgpu_atif_notification_cfg *n = &atif->notification_cfg;
 293	struct atif_system_params params;
 294	size_t size;
 295	int err = 0;
 296
 297	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_PARAMETERS,
 298				NULL);
 299	if (!info) {
 300		err = -EIO;
 301		goto out;
 302	}
 303
 304	size = *(u16 *) info->buffer.pointer;
 305	if (size < 10) {
 306		err = -EINVAL;
 307		goto out;
 308	}
 309
 310	memset(&params, 0, sizeof(params));
 311	size = min(sizeof(params), size);
 312	memcpy(&params, info->buffer.pointer, size);
 313
 314	DRM_DEBUG_DRIVER("SYSTEM_PARAMS: mask = %#x, flags = %#x\n",
 315			params.flags, params.valid_mask);
 316	params.flags = params.flags & params.valid_mask;
 317
 318	if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_NONE) {
 319		n->enabled = false;
 320		n->command_code = 0;
 321	} else if ((params.flags & ATIF_NOTIFY_MASK) == ATIF_NOTIFY_81) {
 322		n->enabled = true;
 323		n->command_code = 0x81;
 324	} else {
 325		if (size < 11) {
 326			err = -EINVAL;
 327			goto out;
 328		}
 329		n->enabled = true;
 330		n->command_code = params.command_code;
 331	}
 332
 333out:
 334	DRM_DEBUG_DRIVER("Notification %s, command code = %#x\n",
 335			(n->enabled ? "enabled" : "disabled"),
 336			n->command_code);
 337	kfree(info);
 338	return err;
 339}
 340
 341/**
 342 * amdgpu_atif_query_backlight_caps - get min and max backlight input signal
 343 *
 344 * @atif: acpi handle
 345 *
 346 * Execute the QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS ATIF function
 347 * to determine the acceptable range of backlight values
 348 *
 349 * Backlight_caps.caps_valid will be set to true if the query is successful
 350 *
 351 * The input signals are in range 0-255
 352 *
 353 * This function assumes the display with backlight is the first LCD
 354 *
 355 * Returns 0 on success, error on failure.
 356 */
 357static int amdgpu_atif_query_backlight_caps(struct amdgpu_atif *atif)
 358{
 359	union acpi_object *info;
 360	struct atif_qbtc_output characteristics;
 361	struct atif_qbtc_arguments arguments;
 362	struct acpi_buffer params;
 363	size_t size;
 364	int err = 0;
 365
 366	arguments.size = sizeof(arguments);
 367	arguments.requested_display = ATIF_QBTC_REQUEST_LCD1;
 368
 369	params.length = sizeof(arguments);
 370	params.pointer = (void *)&arguments;
 371
 372	info = amdgpu_atif_call(atif,
 373		ATIF_FUNCTION_QUERY_BRIGHTNESS_TRANSFER_CHARACTERISTICS,
 374		&params);
 375	if (!info) {
 376		err = -EIO;
 377		goto out;
 378	}
 379
 380	size = *(u16 *) info->buffer.pointer;
 381	if (size < 10) {
 382		err = -EINVAL;
 383		goto out;
 384	}
 385
 386	memset(&characteristics, 0, sizeof(characteristics));
 387	size = min(sizeof(characteristics), size);
 388	memcpy(&characteristics, info->buffer.pointer, size);
 389
 390	atif->backlight_caps.caps_valid = true;
 391	atif->backlight_caps.min_input_signal =
 392			characteristics.min_input_signal;
 393	atif->backlight_caps.max_input_signal =
 394			characteristics.max_input_signal;
 395	atif->backlight_caps.ac_level = characteristics.ac_level;
 396	atif->backlight_caps.dc_level = characteristics.dc_level;
 397	atif->backlight_caps.data_points = characteristics.number_of_points;
 398	memcpy(atif->backlight_caps.luminance_data,
 399	       characteristics.data_points,
 400	       sizeof(atif->backlight_caps.luminance_data));
 401out:
 402	kfree(info);
 403	return err;
 404}
 405
 406/**
 407 * amdgpu_atif_get_sbios_requests - get requested sbios event
 408 *
 409 * @atif: acpi handle
 410 * @req: atif sbios request struct
 411 *
 412 * Execute the ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS ATIF function
 413 * to determine what requests the sbios is making to the driver
 414 * (all asics).
 415 * Returns 0 on success, error on failure.
 416 */
 417static int amdgpu_atif_get_sbios_requests(struct amdgpu_atif *atif,
 418					  struct atif_sbios_requests *req)
 419{
 420	union acpi_object *info;
 421	size_t size;
 422	int count = 0;
 423
 424	info = amdgpu_atif_call(atif, ATIF_FUNCTION_GET_SYSTEM_BIOS_REQUESTS,
 425				NULL);
 426	if (!info)
 427		return -EIO;
 428
 429	size = *(u16 *)info->buffer.pointer;
 430	if (size < 0xd) {
 431		count = -EINVAL;
 432		goto out;
 433	}
 434	memset(req, 0, sizeof(*req));
 435
 436	size = min(sizeof(*req), size);
 437	memcpy(req, info->buffer.pointer, size);
 438	DRM_DEBUG_DRIVER("SBIOS pending requests: %#x\n", req->pending);
 439
 440	count = hweight32(req->pending);
 441
 442out:
 443	kfree(info);
 444	return count;
 445}
 446
 447/**
 448 * amdgpu_atif_handler - handle ATIF notify requests
 449 *
 450 * @adev: amdgpu_device pointer
 451 * @event: atif sbios request struct
 452 *
 453 * Checks the acpi event and if it matches an atif event,
 454 * handles it.
 455 *
 456 * Returns:
 457 * NOTIFY_BAD or NOTIFY_DONE, depending on the event.
 458 */
 459static int amdgpu_atif_handler(struct amdgpu_device *adev,
 460			       struct acpi_bus_event *event)
 461{
 462	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
 463	int count;
 464
 465	DRM_DEBUG_DRIVER("event, device_class = %s, type = %#x\n",
 466			event->device_class, event->type);
 467
 468	if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
 469		return NOTIFY_DONE;
 470
 471	/* Is this actually our event? */
 472	if (!atif->notification_cfg.enabled ||
 473	    event->type != atif->notification_cfg.command_code) {
 474		/* These events will generate keypresses otherwise */
 475		if (event->type == ACPI_VIDEO_NOTIFY_PROBE)
 476			return NOTIFY_BAD;
 477		else
 478			return NOTIFY_DONE;
 479	}
 480
 481	if (atif->functions.sbios_requests) {
 482		struct atif_sbios_requests req;
 483
 484		/* Check pending SBIOS requests */
 485		count = amdgpu_atif_get_sbios_requests(atif, &req);
 486
 487		if (count <= 0)
 488			return NOTIFY_BAD;
 489
 490		DRM_DEBUG_DRIVER("ATIF: %d pending SBIOS requests\n", count);
 491
 492		if (req.pending & ATIF_PANEL_BRIGHTNESS_CHANGE_REQUEST) {
 493			if (atif->bd) {
 494				DRM_DEBUG_DRIVER("Changing brightness to %d\n",
 495						 req.backlight_level);
 496				/*
 497				 * XXX backlight_device_set_brightness() is
 498				 * hardwired to post BACKLIGHT_UPDATE_SYSFS.
 499				 * It probably should accept 'reason' parameter.
 500				 */
 501				backlight_device_set_brightness(atif->bd, req.backlight_level);
 502			}
 503		}
 504
 505		if (req.pending & ATIF_DGPU_DISPLAY_EVENT) {
 506			if (adev->flags & AMD_IS_PX) {
 507				pm_runtime_get_sync(adev_to_drm(adev)->dev);
 508				/* Just fire off a uevent and let userspace tell us what to do */
 509				drm_helper_hpd_irq_event(adev_to_drm(adev));
 510				pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
 511			}
 512		}
 513		/* TODO: check other events */
 514	}
 515
 516	/* We've handled the event, stop the notifier chain. The ACPI interface
 517	 * overloads ACPI_VIDEO_NOTIFY_PROBE, we don't want to send that to
 518	 * userspace if the event was generated only to signal a SBIOS
 519	 * request.
 520	 */
 521	return NOTIFY_BAD;
 522}
 523
 524/* Call the ATCS method
 525 */
 526/**
 527 * amdgpu_atcs_call - call an ATCS method
 528 *
 529 * @atcs: atcs structure
 530 * @function: the ATCS function to execute
 531 * @params: ATCS function params
 532 *
 533 * Executes the requested ATCS function (all asics).
 534 * Returns a pointer to the acpi output buffer.
 535 */
 536static union acpi_object *amdgpu_atcs_call(struct amdgpu_atcs *atcs,
 537					   int function,
 538					   struct acpi_buffer *params)
 539{
 540	acpi_status status;
 541	union acpi_object atcs_arg_elements[2];
 542	struct acpi_object_list atcs_arg;
 543	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
 544
 545	atcs_arg.count = 2;
 546	atcs_arg.pointer = &atcs_arg_elements[0];
 547
 548	atcs_arg_elements[0].type = ACPI_TYPE_INTEGER;
 549	atcs_arg_elements[0].integer.value = function;
 550
 551	if (params) {
 552		atcs_arg_elements[1].type = ACPI_TYPE_BUFFER;
 553		atcs_arg_elements[1].buffer.length = params->length;
 554		atcs_arg_elements[1].buffer.pointer = params->pointer;
 555	} else {
 556		/* We need a second fake parameter */
 557		atcs_arg_elements[1].type = ACPI_TYPE_INTEGER;
 558		atcs_arg_elements[1].integer.value = 0;
 559	}
 560
 561	status = acpi_evaluate_object(atcs->handle, NULL, &atcs_arg, &buffer);
 562
 563	/* Fail only if calling the method fails and ATIF is supported */
 564	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
 565		DRM_DEBUG_DRIVER("failed to evaluate ATCS got %s\n",
 566				 acpi_format_exception(status));
 567		kfree(buffer.pointer);
 568		return NULL;
 569	}
 570
 571	return buffer.pointer;
 572}
 573
 574/**
 575 * amdgpu_atcs_parse_functions - parse supported functions
 576 *
 577 * @f: supported functions struct
 578 * @mask: supported functions mask from ATCS
 579 *
 580 * Use the supported functions mask from ATCS function
 581 * ATCS_FUNCTION_VERIFY_INTERFACE to determine what functions
 582 * are supported (all asics).
 583 */
 584static void amdgpu_atcs_parse_functions(struct amdgpu_atcs_functions *f, u32 mask)
 585{
 586	f->get_ext_state = mask & ATCS_GET_EXTERNAL_STATE_SUPPORTED;
 587	f->pcie_perf_req = mask & ATCS_PCIE_PERFORMANCE_REQUEST_SUPPORTED;
 588	f->pcie_dev_rdy = mask & ATCS_PCIE_DEVICE_READY_NOTIFICATION_SUPPORTED;
 589	f->pcie_bus_width = mask & ATCS_SET_PCIE_BUS_WIDTH_SUPPORTED;
 590	f->power_shift_control = mask & ATCS_SET_POWER_SHIFT_CONTROL_SUPPORTED;
 591}
 592
 593/**
 594 * amdgpu_atcs_verify_interface - verify ATCS
 595 *
 596 * @atcs: amdgpu atcs struct
 597 *
 598 * Execute the ATCS_FUNCTION_VERIFY_INTERFACE ATCS function
 599 * to initialize ATCS and determine what features are supported
 600 * (all asics).
 601 * returns 0 on success, error on failure.
 602 */
 603static int amdgpu_atcs_verify_interface(struct amdgpu_atcs *atcs)
 604{
 605	union acpi_object *info;
 606	struct atcs_verify_interface output;
 607	size_t size;
 608	int err = 0;
 609
 610	info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_VERIFY_INTERFACE, NULL);
 611	if (!info)
 612		return -EIO;
 613
 614	memset(&output, 0, sizeof(output));
 615
 616	size = *(u16 *) info->buffer.pointer;
 617	if (size < 8) {
 618		DRM_INFO("ATCS buffer is too small: %zu\n", size);
 619		err = -EINVAL;
 620		goto out;
 621	}
 622	size = min(sizeof(output), size);
 623
 624	memcpy(&output, info->buffer.pointer, size);
 625
 626	/* TODO: check version? */
 627	DRM_DEBUG_DRIVER("ATCS version %u\n", output.version);
 628
 629	amdgpu_atcs_parse_functions(&atcs->functions, output.function_bits);
 630
 631out:
 632	kfree(info);
 633	return err;
 634}
 635
 636/**
 637 * amdgpu_acpi_is_pcie_performance_request_supported
 638 *
 639 * @adev: amdgpu_device pointer
 640 *
 641 * Check if the ATCS pcie_perf_req and pcie_dev_rdy methods
 642 * are supported (all asics).
 643 * returns true if supported, false if not.
 644 */
 645bool amdgpu_acpi_is_pcie_performance_request_supported(struct amdgpu_device *adev)
 646{
 647	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
 648
 649	if (atcs->functions.pcie_perf_req && atcs->functions.pcie_dev_rdy)
 650		return true;
 651
 652	return false;
 653}
 654
 655/**
 656 * amdgpu_acpi_is_power_shift_control_supported
 657 *
 658 * Check if the ATCS power shift control method
 659 * is supported.
 660 * returns true if supported, false if not.
 661 */
 662bool amdgpu_acpi_is_power_shift_control_supported(void)
 663{
 664	return amdgpu_acpi_priv.atcs.functions.power_shift_control;
 665}
 666
 667/**
 668 * amdgpu_acpi_pcie_notify_device_ready
 669 *
 670 * @adev: amdgpu_device pointer
 671 *
 672 * Executes the PCIE_DEVICE_READY_NOTIFICATION method
 673 * (all asics).
 674 * returns 0 on success, error on failure.
 675 */
 676int amdgpu_acpi_pcie_notify_device_ready(struct amdgpu_device *adev)
 677{
 678	union acpi_object *info;
 679	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
 680
 681	if (!atcs->functions.pcie_dev_rdy)
 682		return -EINVAL;
 683
 684	info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_DEVICE_READY_NOTIFICATION, NULL);
 685	if (!info)
 686		return -EIO;
 687
 688	kfree(info);
 689
 690	return 0;
 691}
 692
 693/**
 694 * amdgpu_acpi_pcie_performance_request
 695 *
 696 * @adev: amdgpu_device pointer
 697 * @perf_req: requested perf level (pcie gen speed)
 698 * @advertise: set advertise caps flag if set
 699 *
 700 * Executes the PCIE_PERFORMANCE_REQUEST method to
 701 * change the pcie gen speed (all asics).
 702 * returns 0 on success, error on failure.
 703 */
 704int amdgpu_acpi_pcie_performance_request(struct amdgpu_device *adev,
 705					 u8 perf_req, bool advertise)
 706{
 707	union acpi_object *info;
 708	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
 709	struct atcs_pref_req_input atcs_input;
 710	struct atcs_pref_req_output atcs_output;
 711	struct acpi_buffer params;
 712	size_t size;
 713	u32 retry = 3;
 714
 715	if (amdgpu_acpi_pcie_notify_device_ready(adev))
 716		return -EINVAL;
 717
 718	if (!atcs->functions.pcie_perf_req)
 719		return -EINVAL;
 720
 721	atcs_input.size = sizeof(struct atcs_pref_req_input);
 722	/* client id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
 723	atcs_input.client_id = pci_dev_id(adev->pdev);
 724	atcs_input.valid_flags_mask = ATCS_VALID_FLAGS_MASK;
 725	atcs_input.flags = ATCS_WAIT_FOR_COMPLETION;
 726	if (advertise)
 727		atcs_input.flags |= ATCS_ADVERTISE_CAPS;
 728	atcs_input.req_type = ATCS_PCIE_LINK_SPEED;
 729	atcs_input.perf_req = perf_req;
 730
 731	params.length = sizeof(struct atcs_pref_req_input);
 732	params.pointer = &atcs_input;
 733
 734	while (retry--) {
 735		info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_PCIE_PERFORMANCE_REQUEST, &params);
 736		if (!info)
 737			return -EIO;
 738
 739		memset(&atcs_output, 0, sizeof(atcs_output));
 740
 741		size = *(u16 *) info->buffer.pointer;
 742		if (size < 3) {
 743			DRM_INFO("ATCS buffer is too small: %zu\n", size);
 744			kfree(info);
 745			return -EINVAL;
 746		}
 747		size = min(sizeof(atcs_output), size);
 748
 749		memcpy(&atcs_output, info->buffer.pointer, size);
 750
 751		kfree(info);
 752
 753		switch (atcs_output.ret_val) {
 754		case ATCS_REQUEST_REFUSED:
 755		default:
 756			return -EINVAL;
 757		case ATCS_REQUEST_COMPLETE:
 758			return 0;
 759		case ATCS_REQUEST_IN_PROGRESS:
 760			udelay(10);
 761			break;
 762		}
 763	}
 764
 765	return 0;
 766}
 767
 768/**
 769 * amdgpu_acpi_power_shift_control
 770 *
 771 * @adev: amdgpu_device pointer
 772 * @dev_state: device acpi state
 773 * @drv_state: driver state
 774 *
 775 * Executes the POWER_SHIFT_CONTROL method to
 776 * communicate current dGPU device state and
 777 * driver state to APU/SBIOS.
 778 * returns 0 on success, error on failure.
 779 */
 780int amdgpu_acpi_power_shift_control(struct amdgpu_device *adev,
 781				    u8 dev_state, bool drv_state)
 782{
 783	union acpi_object *info;
 784	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
 785	struct atcs_pwr_shift_input atcs_input;
 786	struct acpi_buffer params;
 787
 788	if (!amdgpu_acpi_is_power_shift_control_supported())
 789		return -EINVAL;
 790
 791	atcs_input.size = sizeof(struct atcs_pwr_shift_input);
 792	/* dGPU id (bit 2-0: func num, 7-3: dev num, 15-8: bus num) */
 793	atcs_input.dgpu_id = pci_dev_id(adev->pdev);
 794	atcs_input.dev_acpi_state = dev_state;
 795	atcs_input.drv_state = drv_state;
 796
 797	params.length = sizeof(struct atcs_pwr_shift_input);
 798	params.pointer = &atcs_input;
 799
 800	info = amdgpu_atcs_call(atcs, ATCS_FUNCTION_POWER_SHIFT_CONTROL, &params);
 801	if (!info) {
 802		DRM_ERROR("ATCS PSC update failed\n");
 803		return -EIO;
 804	}
 805
 806	kfree(info);
 807	return 0;
 808}
 809
 810/**
 811 * amdgpu_acpi_smart_shift_update - update dGPU device state to SBIOS
 812 *
 813 * @adev: amdgpu device pointer
 814 * @ss_state: current smart shift event
 815 *
 816 * returns 0 on success,
 817 * otherwise return error number.
 818 */
 819int amdgpu_acpi_smart_shift_update(struct amdgpu_device *adev,
 820				   enum amdgpu_ss ss_state)
 821{
 822	int r;
 823
 824	if (!amdgpu_device_supports_smart_shift(adev))
 825		return 0;
 826
 827	switch (ss_state) {
 828	/* SBIOS trigger “stop”, “enable” and “start” at D0, Driver Operational.
 829	 * SBIOS trigger “stop” at D3, Driver Not Operational.
 830	 * SBIOS trigger “stop” and “disable” at D0, Driver NOT operational.
 831	 */
 832	case AMDGPU_SS_DRV_LOAD:
 833		r = amdgpu_acpi_power_shift_control(adev,
 834						    AMDGPU_ATCS_PSC_DEV_STATE_D0,
 835						    AMDGPU_ATCS_PSC_DRV_STATE_OPR);
 836		break;
 837	case AMDGPU_SS_DEV_D0:
 838		r = amdgpu_acpi_power_shift_control(adev,
 839						    AMDGPU_ATCS_PSC_DEV_STATE_D0,
 840						    AMDGPU_ATCS_PSC_DRV_STATE_OPR);
 841		break;
 842	case AMDGPU_SS_DEV_D3:
 843		r = amdgpu_acpi_power_shift_control(adev,
 844						    AMDGPU_ATCS_PSC_DEV_STATE_D3_HOT,
 845						    AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
 846		break;
 847	case AMDGPU_SS_DRV_UNLOAD:
 848		r = amdgpu_acpi_power_shift_control(adev,
 849						    AMDGPU_ATCS_PSC_DEV_STATE_D0,
 850						    AMDGPU_ATCS_PSC_DRV_STATE_NOT_OPR);
 851		break;
 852	default:
 853		return -EINVAL;
 854	}
 855
 856	return r;
 857}
 858
 859#ifdef CONFIG_ACPI_NUMA
 860static inline uint64_t amdgpu_acpi_get_numa_size(int nid)
 861{
 862	/* This is directly using si_meminfo_node implementation as the
 863	 * function is not exported.
 864	 */
 865	int zone_type;
 866	uint64_t managed_pages = 0;
 867
 868	pg_data_t *pgdat = NODE_DATA(nid);
 869
 870	for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
 871		managed_pages +=
 872			zone_managed_pages(&pgdat->node_zones[zone_type]);
 873	return managed_pages * PAGE_SIZE;
 874}
 875
 876static struct amdgpu_numa_info *amdgpu_acpi_get_numa_info(uint32_t pxm)
 877{
 878	struct amdgpu_numa_info *numa_info;
 879	int nid;
 880
 881	numa_info = xa_load(&numa_info_xa, pxm);
 882
 883	if (!numa_info) {
 884		struct sysinfo info;
 885
 886		numa_info = kzalloc(sizeof(*numa_info), GFP_KERNEL);
 887		if (!numa_info)
 888			return NULL;
 889
 890		nid = pxm_to_node(pxm);
 891		numa_info->pxm = pxm;
 892		numa_info->nid = nid;
 893
 894		if (numa_info->nid == NUMA_NO_NODE) {
 895			si_meminfo(&info);
 896			numa_info->size = info.totalram * info.mem_unit;
 897		} else {
 898			numa_info->size = amdgpu_acpi_get_numa_size(nid);
 899		}
 900		xa_store(&numa_info_xa, numa_info->pxm, numa_info, GFP_KERNEL);
 901	}
 902
 903	return numa_info;
 904}
 905#endif
 906
 907/**
 908 * amdgpu_acpi_get_node_id - obtain the NUMA node id for corresponding amdgpu
 909 * acpi device handle
 910 *
 911 * @handle: acpi handle
 912 * @numa_info: amdgpu_numa_info structure holding numa information
 913 *
 914 * Queries the ACPI interface to fetch the corresponding NUMA Node ID for a
 915 * given amdgpu acpi device.
 916 *
 917 * Returns ACPI STATUS OK with Node ID on success or the corresponding failure reason
 918 */
 919static acpi_status amdgpu_acpi_get_node_id(acpi_handle handle,
 920				    struct amdgpu_numa_info **numa_info)
 921{
 922#ifdef CONFIG_ACPI_NUMA
 923	u64 pxm;
 924	acpi_status status;
 925
 926	if (!numa_info)
 927		return_ACPI_STATUS(AE_ERROR);
 928
 929	status = acpi_evaluate_integer(handle, "_PXM", NULL, &pxm);
 930
 931	if (ACPI_FAILURE(status))
 932		return status;
 933
 934	*numa_info = amdgpu_acpi_get_numa_info(pxm);
 935
 936	if (!*numa_info)
 937		return_ACPI_STATUS(AE_ERROR);
 938
 939	return_ACPI_STATUS(AE_OK);
 940#else
 941	return_ACPI_STATUS(AE_NOT_EXIST);
 942#endif
 943}
 944
 945static struct amdgpu_acpi_dev_info *amdgpu_acpi_get_dev(u32 sbdf)
 946{
 947	struct amdgpu_acpi_dev_info *acpi_dev;
 948
 949	if (list_empty(&amdgpu_acpi_dev_list))
 950		return NULL;
 951
 952	list_for_each_entry(acpi_dev, &amdgpu_acpi_dev_list, list)
 953		if (acpi_dev->sbdf == sbdf)
 954			return acpi_dev;
 955
 956	return NULL;
 957}
 958
 959static int amdgpu_acpi_dev_init(struct amdgpu_acpi_dev_info **dev_info,
 960				struct amdgpu_acpi_xcc_info *xcc_info, u32 sbdf)
 961{
 962	struct amdgpu_acpi_dev_info *tmp;
 963	union acpi_object *obj;
 964	int ret = -ENOENT;
 965
 966	*dev_info = NULL;
 967	tmp = kzalloc(sizeof(struct amdgpu_acpi_dev_info), GFP_KERNEL);
 968	if (!tmp)
 969		return -ENOMEM;
 970
 971	INIT_LIST_HEAD(&tmp->xcc_list);
 972	INIT_LIST_HEAD(&tmp->list);
 973	tmp->sbdf = sbdf;
 974
 975	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
 976				      AMD_XCC_DSM_GET_SUPP_MODE, NULL,
 977				      ACPI_TYPE_INTEGER);
 978
 979	if (!obj) {
 980		acpi_handle_debug(xcc_info->handle,
 981				  "_DSM function %d evaluation failed",
 982				  AMD_XCC_DSM_GET_SUPP_MODE);
 983		ret = -ENOENT;
 984		goto out;
 985	}
 986
 987	tmp->supp_xcp_mode = obj->integer.value & 0xFFFF;
 988	ACPI_FREE(obj);
 989
 990	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
 991				      AMD_XCC_DSM_GET_XCP_MODE, NULL,
 992				      ACPI_TYPE_INTEGER);
 993
 994	if (!obj) {
 995		acpi_handle_debug(xcc_info->handle,
 996				  "_DSM function %d evaluation failed",
 997				  AMD_XCC_DSM_GET_XCP_MODE);
 998		ret = -ENOENT;
 999		goto out;
1000	}
1001
1002	tmp->xcp_mode = obj->integer.value & 0xFFFF;
1003	tmp->mem_mode = (obj->integer.value >> 32) & 0xFFFF;
1004	ACPI_FREE(obj);
1005
1006	/* Evaluate DSMs and fill XCC information */
1007	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
1008				      AMD_XCC_DSM_GET_TMR_INFO, NULL,
1009				      ACPI_TYPE_PACKAGE);
1010
1011	if (!obj || obj->package.count < 2) {
1012		acpi_handle_debug(xcc_info->handle,
1013				  "_DSM function %d evaluation failed",
1014				  AMD_XCC_DSM_GET_TMR_INFO);
1015		ret = -ENOENT;
1016		goto out;
1017	}
1018
1019	tmp->tmr_base = obj->package.elements[0].integer.value;
1020	tmp->tmr_size = obj->package.elements[1].integer.value;
1021	ACPI_FREE(obj);
1022
1023	DRM_DEBUG_DRIVER(
1024		"New dev(%x): Supported xcp mode: %x curr xcp_mode : %x mem mode : %x, tmr base: %llx tmr size: %llx  ",
1025		tmp->sbdf, tmp->supp_xcp_mode, tmp->xcp_mode, tmp->mem_mode,
1026		tmp->tmr_base, tmp->tmr_size);
1027	list_add_tail(&tmp->list, &amdgpu_acpi_dev_list);
1028	*dev_info = tmp;
1029
1030	return 0;
1031
1032out:
1033	if (obj)
1034		ACPI_FREE(obj);
1035	kfree(tmp);
1036
1037	return ret;
1038}
1039
1040static int amdgpu_acpi_get_xcc_info(struct amdgpu_acpi_xcc_info *xcc_info,
1041				    u32 *sbdf)
1042{
1043	union acpi_object *obj;
1044	acpi_status status;
1045	int ret = -ENOENT;
1046
1047	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
1048				      AMD_XCC_DSM_GET_NUM_FUNCS, NULL,
1049				      ACPI_TYPE_INTEGER);
1050
1051	if (!obj || obj->integer.value != AMD_XCC_DSM_NUM_FUNCS)
1052		goto out;
1053	ACPI_FREE(obj);
1054
1055	/* Evaluate DSMs and fill XCC information */
1056	obj = acpi_evaluate_dsm_typed(xcc_info->handle, &amd_xcc_dsm_guid, 0,
1057				      AMD_XCC_DSM_GET_VF_XCC_MAPPING, NULL,
1058				      ACPI_TYPE_INTEGER);
1059
1060	if (!obj) {
1061		acpi_handle_debug(xcc_info->handle,
1062				  "_DSM function %d evaluation failed",
1063				  AMD_XCC_DSM_GET_VF_XCC_MAPPING);
1064		ret = -EINVAL;
1065		goto out;
1066	}
1067
1068	/* PF xcc id [39:32] */
1069	xcc_info->phy_id = (obj->integer.value >> 32) & 0xFF;
1070	/* xcp node of this xcc [47:40] */
1071	xcc_info->xcp_node = (obj->integer.value >> 40) & 0xFF;
1072	/* PF domain of this xcc [31:16] */
1073	*sbdf = (obj->integer.value) & 0xFFFF0000;
1074	/* PF bus/dev/fn of this xcc [63:48] */
1075	*sbdf |= (obj->integer.value >> 48) & 0xFFFF;
1076	ACPI_FREE(obj);
1077	obj = NULL;
1078
1079	status =
1080		amdgpu_acpi_get_node_id(xcc_info->handle, &xcc_info->numa_info);
1081
1082	/* TODO: check if this check is required */
1083	if (ACPI_SUCCESS(status))
1084		ret = 0;
1085out:
1086	if (obj)
1087		ACPI_FREE(obj);
1088
1089	return ret;
1090}
1091
1092static int amdgpu_acpi_enumerate_xcc(void)
1093{
1094	struct amdgpu_acpi_dev_info *dev_info = NULL;
1095	struct amdgpu_acpi_xcc_info *xcc_info;
1096	struct acpi_device *acpi_dev;
1097	char hid[ACPI_ID_LEN];
1098	int ret, id;
1099	u32 sbdf;
1100
1101	INIT_LIST_HEAD(&amdgpu_acpi_dev_list);
1102	xa_init(&numa_info_xa);
1103
1104	for (id = 0; id < AMD_XCC_MAX_HID; id++) {
1105		sprintf(hid, "%s%d", "AMD", AMD_XCC_HID_START + id);
1106		acpi_dev = acpi_dev_get_first_match_dev(hid, NULL, -1);
1107		/* These ACPI objects are expected to be in sequential order. If
1108		 * one is not found, no need to check the rest.
1109		 */
1110		if (!acpi_dev) {
1111			DRM_DEBUG_DRIVER("No matching acpi device found for %s",
1112					 hid);
1113			break;
1114		}
1115
1116		xcc_info = kzalloc(sizeof(struct amdgpu_acpi_xcc_info),
1117				   GFP_KERNEL);
1118		if (!xcc_info) {
1119			DRM_ERROR("Failed to allocate memory for xcc info\n");
1120			return -ENOMEM;
1121		}
1122
1123		INIT_LIST_HEAD(&xcc_info->list);
1124		xcc_info->handle = acpi_device_handle(acpi_dev);
1125		acpi_dev_put(acpi_dev);
1126
1127		ret = amdgpu_acpi_get_xcc_info(xcc_info, &sbdf);
1128		if (ret) {
1129			kfree(xcc_info);
1130			continue;
1131		}
1132
1133		dev_info = amdgpu_acpi_get_dev(sbdf);
1134
1135		if (!dev_info)
1136			ret = amdgpu_acpi_dev_init(&dev_info, xcc_info, sbdf);
1137
1138		if (ret == -ENOMEM) {
1139			kfree(xcc_info);
1140			return ret;
1141		}
1142
1143		if (!dev_info) {
1144			kfree(xcc_info);
1145			continue;
1146		}
1147
1148		list_add_tail(&xcc_info->list, &dev_info->xcc_list);
1149	}
1150
1151	return 0;
1152}
1153
1154int amdgpu_acpi_get_tmr_info(struct amdgpu_device *adev, u64 *tmr_offset,
1155			     u64 *tmr_size)
1156{
1157	struct amdgpu_acpi_dev_info *dev_info;
1158	u32 sbdf;
1159
1160	if (!tmr_offset || !tmr_size)
1161		return -EINVAL;
1162
1163	sbdf = (pci_domain_nr(adev->pdev->bus) << 16);
1164	sbdf |= pci_dev_id(adev->pdev);
1165	dev_info = amdgpu_acpi_get_dev(sbdf);
1166	if (!dev_info)
1167		return -ENOENT;
1168
1169	*tmr_offset = dev_info->tmr_base;
1170	*tmr_size = dev_info->tmr_size;
1171
1172	return 0;
1173}
1174
1175int amdgpu_acpi_get_mem_info(struct amdgpu_device *adev, int xcc_id,
1176			     struct amdgpu_numa_info *numa_info)
1177{
1178	struct amdgpu_acpi_dev_info *dev_info;
1179	struct amdgpu_acpi_xcc_info *xcc_info;
1180	u32 sbdf;
1181
1182	if (!numa_info)
1183		return -EINVAL;
1184
1185	sbdf = (pci_domain_nr(adev->pdev->bus) << 16);
1186	sbdf |= pci_dev_id(adev->pdev);
1187	dev_info = amdgpu_acpi_get_dev(sbdf);
1188	if (!dev_info)
1189		return -ENOENT;
1190
1191	list_for_each_entry(xcc_info, &dev_info->xcc_list, list) {
1192		if (xcc_info->phy_id == xcc_id) {
1193			memcpy(numa_info, xcc_info->numa_info,
1194			       sizeof(*numa_info));
1195			return 0;
1196		}
1197	}
1198
1199	return -ENOENT;
1200}
1201
1202/**
1203 * amdgpu_acpi_event - handle notify events
1204 *
1205 * @nb: notifier block
1206 * @val: val
1207 * @data: acpi event
1208 *
1209 * Calls relevant amdgpu functions in response to various
1210 * acpi events.
1211 * Returns NOTIFY code
1212 */
1213static int amdgpu_acpi_event(struct notifier_block *nb,
1214			     unsigned long val,
1215			     void *data)
1216{
1217	struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, acpi_nb);
1218	struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
1219
1220	if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
1221		if (power_supply_is_system_supplied() > 0)
1222			DRM_DEBUG_DRIVER("pm: AC\n");
1223		else
1224			DRM_DEBUG_DRIVER("pm: DC\n");
1225
1226		amdgpu_pm_acpi_event_handler(adev);
1227	}
1228
1229	/* Check for pending SBIOS requests */
1230	return amdgpu_atif_handler(adev, entry);
1231}
1232
1233/* Call all ACPI methods here */
1234/**
1235 * amdgpu_acpi_init - init driver acpi support
1236 *
1237 * @adev: amdgpu_device pointer
1238 *
1239 * Verifies the AMD ACPI interfaces and registers with the acpi
1240 * notifier chain (all asics).
1241 * Returns 0 on success, error on failure.
1242 */
1243int amdgpu_acpi_init(struct amdgpu_device *adev)
1244{
1245	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1246
1247	if (atif->notifications.brightness_change) {
1248		if (adev->dc_enabled) {
1249#if defined(CONFIG_DRM_AMD_DC)
1250			struct amdgpu_display_manager *dm = &adev->dm;
1251
1252			if (dm->backlight_dev[0])
1253				atif->bd = dm->backlight_dev[0];
1254#endif
1255		} else {
1256			struct drm_encoder *tmp;
1257
1258			/* Find the encoder controlling the brightness */
1259			list_for_each_entry(tmp, &adev_to_drm(adev)->mode_config.encoder_list,
1260					    head) {
1261				struct amdgpu_encoder *enc = to_amdgpu_encoder(tmp);
1262
1263				if ((enc->devices & (ATOM_DEVICE_LCD_SUPPORT)) &&
1264				    enc->enc_priv) {
1265					struct amdgpu_encoder_atom_dig *dig = enc->enc_priv;
1266
1267					if (dig->bl_dev) {
1268						atif->bd = dig->bl_dev;
1269						break;
1270					}
1271				}
1272			}
1273		}
1274	}
1275	adev->acpi_nb.notifier_call = amdgpu_acpi_event;
1276	register_acpi_notifier(&adev->acpi_nb);
1277
1278	return 0;
1279}
1280
1281void amdgpu_acpi_get_backlight_caps(struct amdgpu_dm_backlight_caps *caps)
1282{
1283	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1284
1285	memcpy(caps, &atif->backlight_caps, sizeof(*caps));
1286}
1287
1288/**
1289 * amdgpu_acpi_fini - tear down driver acpi support
1290 *
1291 * @adev: amdgpu_device pointer
1292 *
1293 * Unregisters with the acpi notifier chain (all asics).
1294 */
1295void amdgpu_acpi_fini(struct amdgpu_device *adev)
1296{
1297	unregister_acpi_notifier(&adev->acpi_nb);
1298}
1299
1300/**
1301 * amdgpu_atif_pci_probe_handle - look up the ATIF handle
1302 *
1303 * @pdev: pci device
1304 *
1305 * Look up the ATIF handles (all asics).
1306 * Returns true if the handle is found, false if not.
1307 */
1308static bool amdgpu_atif_pci_probe_handle(struct pci_dev *pdev)
1309{
1310	char acpi_method_name[255] = { 0 };
1311	struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};
1312	acpi_handle dhandle, atif_handle;
1313	acpi_status status;
1314	int ret;
1315
1316	dhandle = ACPI_HANDLE(&pdev->dev);
1317	if (!dhandle)
1318		return false;
1319
1320	status = acpi_get_handle(dhandle, "ATIF", &atif_handle);
1321	if (ACPI_FAILURE(status))
1322		return false;
1323
1324	amdgpu_acpi_priv.atif.handle = atif_handle;
1325	acpi_get_name(amdgpu_acpi_priv.atif.handle, ACPI_FULL_PATHNAME, &buffer);
1326	DRM_DEBUG_DRIVER("Found ATIF handle %s\n", acpi_method_name);
1327	ret = amdgpu_atif_verify_interface(&amdgpu_acpi_priv.atif);
1328	if (ret) {
1329		amdgpu_acpi_priv.atif.handle = 0;
1330		return false;
1331	}
1332	return true;
1333}
1334
1335/**
1336 * amdgpu_atcs_pci_probe_handle - look up the ATCS handle
1337 *
1338 * @pdev: pci device
1339 *
1340 * Look up the ATCS handles (all asics).
1341 * Returns true if the handle is found, false if not.
1342 */
1343static bool amdgpu_atcs_pci_probe_handle(struct pci_dev *pdev)
1344{
1345	char acpi_method_name[255] = { 0 };
1346	struct acpi_buffer buffer = { sizeof(acpi_method_name), acpi_method_name };
1347	acpi_handle dhandle, atcs_handle;
1348	acpi_status status;
1349	int ret;
1350
1351	dhandle = ACPI_HANDLE(&pdev->dev);
1352	if (!dhandle)
1353		return false;
1354
1355	status = acpi_get_handle(dhandle, "ATCS", &atcs_handle);
1356	if (ACPI_FAILURE(status))
1357		return false;
1358
1359	amdgpu_acpi_priv.atcs.handle = atcs_handle;
1360	acpi_get_name(amdgpu_acpi_priv.atcs.handle, ACPI_FULL_PATHNAME, &buffer);
1361	DRM_DEBUG_DRIVER("Found ATCS handle %s\n", acpi_method_name);
1362	ret = amdgpu_atcs_verify_interface(&amdgpu_acpi_priv.atcs);
1363	if (ret) {
1364		amdgpu_acpi_priv.atcs.handle = 0;
1365		return false;
1366	}
1367	return true;
1368}
1369
1370
1371/**
1372 * amdgpu_acpi_should_gpu_reset
1373 *
1374 * @adev: amdgpu_device_pointer
1375 *
1376 * returns true if should reset GPU, false if not
1377 */
1378bool amdgpu_acpi_should_gpu_reset(struct amdgpu_device *adev)
1379{
1380	if ((adev->flags & AMD_IS_APU) &&
1381	    adev->gfx.imu.funcs) /* Not need to do mode2 reset for IMU enabled APUs */
1382		return false;
1383
1384	if ((adev->flags & AMD_IS_APU) &&
1385	    amdgpu_acpi_is_s3_active(adev))
1386		return false;
1387
1388	if (amdgpu_sriov_vf(adev))
1389		return false;
1390
1391#if IS_ENABLED(CONFIG_SUSPEND)
1392	return pm_suspend_target_state != PM_SUSPEND_TO_IDLE;
1393#else
1394	return true;
1395#endif
1396}
1397
1398/*
1399 * amdgpu_acpi_detect - detect ACPI ATIF/ATCS methods
1400 *
1401 * Check if we have the ATIF/ATCS methods and populate
1402 * the structures in the driver.
1403 */
1404void amdgpu_acpi_detect(void)
1405{
1406	struct amdgpu_atif *atif = &amdgpu_acpi_priv.atif;
1407	struct amdgpu_atcs *atcs = &amdgpu_acpi_priv.atcs;
1408	struct pci_dev *pdev = NULL;
1409	int ret;
1410
1411	while ((pdev = pci_get_base_class(PCI_BASE_CLASS_DISPLAY, pdev))) {
1412		if ((pdev->class != PCI_CLASS_DISPLAY_VGA << 8) &&
1413		    (pdev->class != PCI_CLASS_DISPLAY_OTHER << 8))
1414			continue;
1415
1416		if (!atif->handle)
1417			amdgpu_atif_pci_probe_handle(pdev);
1418		if (!atcs->handle)
1419			amdgpu_atcs_pci_probe_handle(pdev);
1420	}
1421
1422	if (atif->functions.sbios_requests && !atif->functions.system_params) {
1423		/* XXX check this workraround, if sbios request function is
1424		 * present we have to see how it's configured in the system
1425		 * params
1426		 */
1427		atif->functions.system_params = true;
1428	}
1429
1430	if (atif->functions.system_params) {
1431		ret = amdgpu_atif_get_notification_params(atif);
1432		if (ret) {
1433			DRM_DEBUG_DRIVER("Call to GET_SYSTEM_PARAMS failed: %d\n",
1434					ret);
1435			/* Disable notification */
1436			atif->notification_cfg.enabled = false;
1437		}
1438	}
1439
1440	if (atif->functions.query_backlight_transfer_characteristics) {
1441		ret = amdgpu_atif_query_backlight_caps(atif);
1442		if (ret) {
1443			DRM_DEBUG_DRIVER("Call to QUERY_BACKLIGHT_TRANSFER_CHARACTERISTICS failed: %d\n",
1444					ret);
1445			atif->backlight_caps.caps_valid = false;
1446		}
1447	} else {
1448		atif->backlight_caps.caps_valid = false;
1449	}
1450
1451	amdgpu_acpi_enumerate_xcc();
1452}
1453
1454void amdgpu_acpi_release(void)
1455{
1456	struct amdgpu_acpi_dev_info *dev_info, *dev_tmp;
1457	struct amdgpu_acpi_xcc_info *xcc_info, *xcc_tmp;
1458	struct amdgpu_numa_info *numa_info;
1459	unsigned long index;
1460
1461	xa_for_each(&numa_info_xa, index, numa_info) {
1462		kfree(numa_info);
1463		xa_erase(&numa_info_xa, index);
1464	}
1465
1466	if (list_empty(&amdgpu_acpi_dev_list))
1467		return;
1468
1469	list_for_each_entry_safe(dev_info, dev_tmp, &amdgpu_acpi_dev_list,
1470				 list) {
1471		list_for_each_entry_safe(xcc_info, xcc_tmp, &dev_info->xcc_list,
1472					 list) {
1473			list_del(&xcc_info->list);
1474			kfree(xcc_info);
1475		}
1476
1477		list_del(&dev_info->list);
1478		kfree(dev_info);
1479	}
1480}
1481
1482#if IS_ENABLED(CONFIG_SUSPEND)
1483/**
1484 * amdgpu_acpi_is_s3_active
1485 *
1486 * @adev: amdgpu_device_pointer
1487 *
1488 * returns true if supported, false if not.
1489 */
1490bool amdgpu_acpi_is_s3_active(struct amdgpu_device *adev)
1491{
1492	return !(adev->flags & AMD_IS_APU) ||
1493		(pm_suspend_target_state == PM_SUSPEND_MEM);
1494}
1495
1496/**
1497 * amdgpu_acpi_is_s0ix_active
1498 *
1499 * @adev: amdgpu_device_pointer
1500 *
1501 * returns true if supported, false if not.
1502 */
1503bool amdgpu_acpi_is_s0ix_active(struct amdgpu_device *adev)
1504{
1505	if (!(adev->flags & AMD_IS_APU) ||
1506	    (pm_suspend_target_state != PM_SUSPEND_TO_IDLE))
1507		return false;
1508
1509	if (adev->asic_type < CHIP_RAVEN)
1510		return false;
1511
1512	if (!(adev->pm.pp_feature & PP_GFXOFF_MASK))
1513		return false;
1514
1515	/*
1516	 * If ACPI_FADT_LOW_POWER_S0 is not set in the FADT, it is generally
1517	 * risky to do any special firmware-related preparations for entering
1518	 * S0ix even though the system is suspending to idle, so return false
1519	 * in that case.
1520	 */
1521	if (!(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)) {
1522		dev_err_once(adev->dev,
1523			      "Power consumption will be higher as BIOS has not been configured for suspend-to-idle.\n"
1524			      "To use suspend-to-idle change the sleep mode in BIOS setup.\n");
1525		return false;
1526	}
1527
1528#if !IS_ENABLED(CONFIG_AMD_PMC)
1529	dev_err_once(adev->dev,
1530		      "Power consumption will be higher as the kernel has not been compiled with CONFIG_AMD_PMC.\n");
1531	return false;
1532#else
1533	return true;
1534#endif /* CONFIG_AMD_PMC */
1535}
1536#endif /* CONFIG_SUSPEND */
1537
1538#if IS_ENABLED(CONFIG_DRM_AMD_ISP)
1539static const struct acpi_device_id isp_sensor_ids[] = {
1540	{ "OMNI5C10" },
1541	{ }
1542};
1543
1544static int isp_match_acpi_device_ids(struct device *dev, const void *data)
1545{
1546	return acpi_match_device(data, dev) ? 1 : 0;
1547}
1548
1549int amdgpu_acpi_get_isp4_dev(struct acpi_device **dev)
1550{
1551	struct device *pdev __free(put_device) = NULL;
1552	struct acpi_device *acpi_pdev;
1553
1554	pdev = bus_find_device(&platform_bus_type, NULL, isp_sensor_ids,
1555			       isp_match_acpi_device_ids);
1556	if (!pdev)
1557		return -EINVAL;
1558
1559	acpi_pdev = ACPI_COMPANION(pdev);
1560	if (!acpi_pdev)
1561		return -ENODEV;
1562
1563	*dev = acpi_pdev;
1564
1565	return 0;
1566}
1567#endif /* CONFIG_DRM_AMD_ISP */
~/ Read-only file preview.