Path: blob/master/drivers/gpu/drm/amd/amdgpu/amdgpu_atpx_handler.c
26517 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* Copyright (c) 2010 Red Hat Inc.3* Author : Dave Airlie <[email protected]>4*5* ATPX support for both Intel/ATI6*/7#include <linux/vga_switcheroo.h>8#include <linux/slab.h>9#include <linux/acpi.h>10#include <linux/pci.h>11#include <linux/delay.h>1213#include "amdgpu.h"14#include "amd_acpi.h"1516#define AMDGPU_PX_QUIRK_FORCE_ATPX (1 << 0)1718struct amdgpu_px_quirk {19u32 chip_vendor;20u32 chip_device;21u32 subsys_vendor;22u32 subsys_device;23u32 px_quirk_flags;24};2526struct amdgpu_atpx_functions {27bool px_params;28bool power_cntl;29bool disp_mux_cntl;30bool i2c_mux_cntl;31bool switch_start;32bool switch_end;33bool disp_connectors_mapping;34bool disp_detection_ports;35};3637struct amdgpu_atpx {38acpi_handle handle;39struct amdgpu_atpx_functions functions;40bool is_hybrid;41bool dgpu_req_power_for_displays;42};4344static struct amdgpu_atpx_priv {45bool atpx_detected;46bool bridge_pm_usable;47unsigned int quirks;48/* handle for device - and atpx */49acpi_handle dhandle;50acpi_handle other_handle;51struct amdgpu_atpx atpx;52} amdgpu_atpx_priv;5354struct atpx_verify_interface {55u16 size; /* structure size in bytes (includes size field) */56u16 version; /* version */57u32 function_bits; /* supported functions bit vector */58} __packed;5960struct atpx_px_params {61u16 size; /* structure size in bytes (includes size field) */62u32 valid_flags; /* which flags are valid */63u32 flags; /* flags */64} __packed;6566struct atpx_power_control {67u16 size;68u8 dgpu_state;69} __packed;7071struct atpx_mux {72u16 size;73u16 mux;74} __packed;7576bool amdgpu_has_atpx(void)77{78return amdgpu_atpx_priv.atpx_detected;79}8081bool amdgpu_has_atpx_dgpu_power_cntl(void)82{83return amdgpu_atpx_priv.atpx.functions.power_cntl;84}8586bool amdgpu_is_atpx_hybrid(void)87{88return amdgpu_atpx_priv.atpx.is_hybrid;89}9091/**92* amdgpu_atpx_call - call an ATPX method93*94* @handle: acpi handle95* @function: the ATPX function to execute96* @params: ATPX function params97*98* Executes the requested ATPX function (all asics).99* Returns a pointer to the acpi output buffer.100*/101static union acpi_object *amdgpu_atpx_call(acpi_handle handle, int function,102struct acpi_buffer *params)103{104acpi_status status;105union acpi_object atpx_arg_elements[2];106struct acpi_object_list atpx_arg;107struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };108109atpx_arg.count = 2;110atpx_arg.pointer = &atpx_arg_elements[0];111112atpx_arg_elements[0].type = ACPI_TYPE_INTEGER;113atpx_arg_elements[0].integer.value = function;114115if (params) {116atpx_arg_elements[1].type = ACPI_TYPE_BUFFER;117atpx_arg_elements[1].buffer.length = params->length;118atpx_arg_elements[1].buffer.pointer = params->pointer;119} else {120/* We need a second fake parameter */121atpx_arg_elements[1].type = ACPI_TYPE_INTEGER;122atpx_arg_elements[1].integer.value = 0;123}124125status = acpi_evaluate_object(handle, NULL, &atpx_arg, &buffer);126127/* Fail only if calling the method fails and ATPX is supported */128if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {129pr_err("failed to evaluate ATPX got %s\n",130acpi_format_exception(status));131kfree(buffer.pointer);132return NULL;133}134135return buffer.pointer;136}137138/**139* amdgpu_atpx_parse_functions - parse supported functions140*141* @f: supported functions struct142* @mask: supported functions mask from ATPX143*144* Use the supported functions mask from ATPX function145* ATPX_FUNCTION_VERIFY_INTERFACE to determine what functions146* are supported (all asics).147*/148static void amdgpu_atpx_parse_functions(struct amdgpu_atpx_functions *f, u32 mask)149{150f->px_params = mask & ATPX_GET_PX_PARAMETERS_SUPPORTED;151f->power_cntl = mask & ATPX_POWER_CONTROL_SUPPORTED;152f->disp_mux_cntl = mask & ATPX_DISPLAY_MUX_CONTROL_SUPPORTED;153f->i2c_mux_cntl = mask & ATPX_I2C_MUX_CONTROL_SUPPORTED;154f->switch_start = mask & ATPX_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION_SUPPORTED;155f->switch_end = mask & ATPX_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION_SUPPORTED;156f->disp_connectors_mapping = mask & ATPX_GET_DISPLAY_CONNECTORS_MAPPING_SUPPORTED;157f->disp_detection_ports = mask & ATPX_GET_DISPLAY_DETECTION_PORTS_SUPPORTED;158}159160/**161* amdgpu_atpx_validate - validate ATPX functions162*163* @atpx: amdgpu atpx struct164*165* Validate that required functions are enabled (all asics).166* returns 0 on success, error on failure.167*/168static int amdgpu_atpx_validate(struct amdgpu_atpx *atpx)169{170u32 valid_bits = 0;171172if (atpx->functions.px_params) {173union acpi_object *info;174struct atpx_px_params output;175size_t size;176177info = amdgpu_atpx_call(atpx->handle, ATPX_FUNCTION_GET_PX_PARAMETERS, NULL);178if (!info)179return -EIO;180181memset(&output, 0, sizeof(output));182183size = *(u16 *) info->buffer.pointer;184if (size < 10) {185pr_err("ATPX buffer is too small: %zu\n", size);186kfree(info);187return -EINVAL;188}189size = min(sizeof(output), size);190191memcpy(&output, info->buffer.pointer, size);192193valid_bits = output.flags & output.valid_flags;194195kfree(info);196}197198/* if separate mux flag is set, mux controls are required */199if (valid_bits & ATPX_SEPARATE_MUX_FOR_I2C) {200atpx->functions.i2c_mux_cntl = true;201atpx->functions.disp_mux_cntl = true;202}203/* if any outputs are muxed, mux controls are required */204if (valid_bits & (ATPX_CRT1_RGB_SIGNAL_MUXED |205ATPX_TV_SIGNAL_MUXED |206ATPX_DFP_SIGNAL_MUXED))207atpx->functions.disp_mux_cntl = true;208209210/* some bioses set these bits rather than flagging power_cntl as supported */211if (valid_bits & (ATPX_DYNAMIC_PX_SUPPORTED |212ATPX_DYNAMIC_DGPU_POWER_OFF_SUPPORTED))213atpx->functions.power_cntl = true;214215atpx->is_hybrid = false;216if (valid_bits & ATPX_MS_HYBRID_GFX_SUPPORTED) {217if (amdgpu_atpx_priv.quirks & AMDGPU_PX_QUIRK_FORCE_ATPX) {218pr_warn("ATPX Hybrid Graphics, forcing to ATPX\n");219atpx->functions.power_cntl = true;220atpx->is_hybrid = false;221} else {222pr_notice("ATPX Hybrid Graphics\n");223/*224* Disable legacy PM methods only when pcie port PM is usable,225* otherwise the device might fail to power off or power on.226*/227atpx->functions.power_cntl = !amdgpu_atpx_priv.bridge_pm_usable;228atpx->is_hybrid = true;229}230}231232atpx->dgpu_req_power_for_displays = false;233if (valid_bits & ATPX_DGPU_REQ_POWER_FOR_DISPLAYS)234atpx->dgpu_req_power_for_displays = true;235236return 0;237}238239/**240* amdgpu_atpx_verify_interface - verify ATPX241*242* @atpx: amdgpu atpx struct243*244* Execute the ATPX_FUNCTION_VERIFY_INTERFACE ATPX function245* to initialize ATPX and determine what features are supported246* (all asics).247* returns 0 on success, error on failure.248*/249static int amdgpu_atpx_verify_interface(struct amdgpu_atpx *atpx)250{251union acpi_object *info;252struct atpx_verify_interface output;253size_t size;254int err = 0;255256info = amdgpu_atpx_call(atpx->handle, ATPX_FUNCTION_VERIFY_INTERFACE, NULL);257if (!info)258return -EIO;259260memset(&output, 0, sizeof(output));261262size = *(u16 *) info->buffer.pointer;263if (size < 8) {264pr_err("ATPX buffer is too small: %zu\n", size);265err = -EINVAL;266goto out;267}268size = min(sizeof(output), size);269270memcpy(&output, info->buffer.pointer, size);271272/* TODO: check version? */273pr_notice("ATPX version %u, functions 0x%08x\n",274output.version, output.function_bits);275276amdgpu_atpx_parse_functions(&atpx->functions, output.function_bits);277278out:279kfree(info);280return err;281}282283/**284* amdgpu_atpx_set_discrete_state - power up/down discrete GPU285*286* @atpx: atpx info struct287* @state: discrete GPU state (0 = power down, 1 = power up)288*289* Execute the ATPX_FUNCTION_POWER_CONTROL ATPX function to290* power down/up the discrete GPU (all asics).291* Returns 0 on success, error on failure.292*/293static int amdgpu_atpx_set_discrete_state(struct amdgpu_atpx *atpx, u8 state)294{295struct acpi_buffer params;296union acpi_object *info;297struct atpx_power_control input;298299if (atpx->functions.power_cntl) {300input.size = 3;301input.dgpu_state = state;302params.length = input.size;303params.pointer = &input;304info = amdgpu_atpx_call(atpx->handle,305ATPX_FUNCTION_POWER_CONTROL,306¶ms);307if (!info)308return -EIO;309kfree(info);310311/* 200ms delay is required after off */312if (state == 0)313msleep(200);314}315return 0;316}317318/**319* amdgpu_atpx_switch_disp_mux - switch display mux320*321* @atpx: atpx info struct322* @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)323*324* Execute the ATPX_FUNCTION_DISPLAY_MUX_CONTROL ATPX function to325* switch the display mux between the discrete GPU and integrated GPU326* (all asics).327* Returns 0 on success, error on failure.328*/329static int amdgpu_atpx_switch_disp_mux(struct amdgpu_atpx *atpx, u16 mux_id)330{331struct acpi_buffer params;332union acpi_object *info;333struct atpx_mux input;334335if (atpx->functions.disp_mux_cntl) {336input.size = 4;337input.mux = mux_id;338params.length = input.size;339params.pointer = &input;340info = amdgpu_atpx_call(atpx->handle,341ATPX_FUNCTION_DISPLAY_MUX_CONTROL,342¶ms);343if (!info)344return -EIO;345kfree(info);346}347return 0;348}349350/**351* amdgpu_atpx_switch_i2c_mux - switch i2c/hpd mux352*353* @atpx: atpx info struct354* @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)355*356* Execute the ATPX_FUNCTION_I2C_MUX_CONTROL ATPX function to357* switch the i2c/hpd mux between the discrete GPU and integrated GPU358* (all asics).359* Returns 0 on success, error on failure.360*/361static int amdgpu_atpx_switch_i2c_mux(struct amdgpu_atpx *atpx, u16 mux_id)362{363struct acpi_buffer params;364union acpi_object *info;365struct atpx_mux input;366367if (atpx->functions.i2c_mux_cntl) {368input.size = 4;369input.mux = mux_id;370params.length = input.size;371params.pointer = &input;372info = amdgpu_atpx_call(atpx->handle,373ATPX_FUNCTION_I2C_MUX_CONTROL,374¶ms);375if (!info)376return -EIO;377kfree(info);378}379return 0;380}381382/**383* amdgpu_atpx_switch_start - notify the sbios of a GPU switch384*385* @atpx: atpx info struct386* @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)387*388* Execute the ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION ATPX389* function to notify the sbios that a switch between the discrete GPU and390* integrated GPU has begun (all asics).391* Returns 0 on success, error on failure.392*/393static int amdgpu_atpx_switch_start(struct amdgpu_atpx *atpx, u16 mux_id)394{395struct acpi_buffer params;396union acpi_object *info;397struct atpx_mux input;398399if (atpx->functions.switch_start) {400input.size = 4;401input.mux = mux_id;402params.length = input.size;403params.pointer = &input;404info = amdgpu_atpx_call(atpx->handle,405ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_START_NOTIFICATION,406¶ms);407if (!info)408return -EIO;409kfree(info);410}411return 0;412}413414/**415* amdgpu_atpx_switch_end - notify the sbios of a GPU switch416*417* @atpx: atpx info struct418* @mux_id: mux state (0 = integrated GPU, 1 = discrete GPU)419*420* Execute the ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION ATPX421* function to notify the sbios that a switch between the discrete GPU and422* integrated GPU has ended (all asics).423* Returns 0 on success, error on failure.424*/425static int amdgpu_atpx_switch_end(struct amdgpu_atpx *atpx, u16 mux_id)426{427struct acpi_buffer params;428union acpi_object *info;429struct atpx_mux input;430431if (atpx->functions.switch_end) {432input.size = 4;433input.mux = mux_id;434params.length = input.size;435params.pointer = &input;436info = amdgpu_atpx_call(atpx->handle,437ATPX_FUNCTION_GRAPHICS_DEVICE_SWITCH_END_NOTIFICATION,438¶ms);439if (!info)440return -EIO;441kfree(info);442}443return 0;444}445446/**447* amdgpu_atpx_switchto - switch to the requested GPU448*449* @id: GPU to switch to450*451* Execute the necessary ATPX functions to switch between the discrete GPU and452* integrated GPU (all asics).453* Returns 0 on success, error on failure.454*/455static int amdgpu_atpx_switchto(enum vga_switcheroo_client_id id)456{457u16 gpu_id;458459if (id == VGA_SWITCHEROO_IGD)460gpu_id = ATPX_INTEGRATED_GPU;461else462gpu_id = ATPX_DISCRETE_GPU;463464amdgpu_atpx_switch_start(&amdgpu_atpx_priv.atpx, gpu_id);465amdgpu_atpx_switch_disp_mux(&amdgpu_atpx_priv.atpx, gpu_id);466amdgpu_atpx_switch_i2c_mux(&amdgpu_atpx_priv.atpx, gpu_id);467amdgpu_atpx_switch_end(&amdgpu_atpx_priv.atpx, gpu_id);468469return 0;470}471472/**473* amdgpu_atpx_power_state - power down/up the requested GPU474*475* @id: GPU to power down/up476* @state: requested power state (0 = off, 1 = on)477*478* Execute the necessary ATPX function to power down/up the discrete GPU479* (all asics).480* Returns 0 on success, error on failure.481*/482static int amdgpu_atpx_power_state(enum vga_switcheroo_client_id id,483enum vga_switcheroo_state state)484{485/* on w500 ACPI can't change intel gpu state */486if (id == VGA_SWITCHEROO_IGD)487return 0;488489amdgpu_atpx_set_discrete_state(&amdgpu_atpx_priv.atpx, state);490return 0;491}492493/**494* amdgpu_atpx_pci_probe_handle - look up the ATPX handle495*496* @pdev: pci device497*498* Look up the ATPX handles (all asics).499* Returns true if the handles are found, false if not.500*/501static bool amdgpu_atpx_pci_probe_handle(struct pci_dev *pdev)502{503acpi_handle dhandle, atpx_handle;504acpi_status status;505506dhandle = ACPI_HANDLE(&pdev->dev);507if (!dhandle)508return false;509510status = acpi_get_handle(dhandle, "ATPX", &atpx_handle);511if (ACPI_FAILURE(status)) {512amdgpu_atpx_priv.other_handle = dhandle;513return false;514}515amdgpu_atpx_priv.dhandle = dhandle;516amdgpu_atpx_priv.atpx.handle = atpx_handle;517return true;518}519520/**521* amdgpu_atpx_init - verify the ATPX interface522*523* Verify the ATPX interface (all asics).524* Returns 0 on success, error on failure.525*/526static int amdgpu_atpx_init(void)527{528int r;529530/* set up the ATPX handle */531r = amdgpu_atpx_verify_interface(&amdgpu_atpx_priv.atpx);532if (r)533return r;534535/* validate the atpx setup */536r = amdgpu_atpx_validate(&amdgpu_atpx_priv.atpx);537if (r)538return r;539540return 0;541}542543/**544* amdgpu_atpx_get_client_id - get the client id545*546* @pdev: pci device547*548* look up whether we are the integrated or discrete GPU (all asics).549* Returns the client id.550*/551static enum vga_switcheroo_client_id amdgpu_atpx_get_client_id(struct pci_dev *pdev)552{553if (amdgpu_atpx_priv.dhandle == ACPI_HANDLE(&pdev->dev))554return VGA_SWITCHEROO_IGD;555else556return VGA_SWITCHEROO_DIS;557}558559static const struct vga_switcheroo_handler amdgpu_atpx_handler = {560.switchto = amdgpu_atpx_switchto,561.power_state = amdgpu_atpx_power_state,562.get_client_id = amdgpu_atpx_get_client_id,563};564565static const struct amdgpu_px_quirk amdgpu_px_quirk_list[] = {566/* HG _PR3 doesn't seem to work on this A+A weston board */567{ 0x1002, 0x6900, 0x1002, 0x0124, AMDGPU_PX_QUIRK_FORCE_ATPX },568{ 0x1002, 0x6900, 0x1028, 0x0812, AMDGPU_PX_QUIRK_FORCE_ATPX },569{ 0x1002, 0x6900, 0x1028, 0x0813, AMDGPU_PX_QUIRK_FORCE_ATPX },570{ 0x1002, 0x699f, 0x1028, 0x0814, AMDGPU_PX_QUIRK_FORCE_ATPX },571{ 0x1002, 0x6900, 0x1025, 0x125A, AMDGPU_PX_QUIRK_FORCE_ATPX },572{ 0x1002, 0x6900, 0x17AA, 0x3806, AMDGPU_PX_QUIRK_FORCE_ATPX },573{ 0, 0, 0, 0, 0 },574};575576static void amdgpu_atpx_get_quirks(struct pci_dev *pdev)577{578const struct amdgpu_px_quirk *p = amdgpu_px_quirk_list;579580/* Apply PX quirks */581while (p && p->chip_device != 0) {582if (pdev->vendor == p->chip_vendor &&583pdev->device == p->chip_device &&584pdev->subsystem_vendor == p->subsys_vendor &&585pdev->subsystem_device == p->subsys_device) {586amdgpu_atpx_priv.quirks |= p->px_quirk_flags;587break;588}589++p;590}591}592593/**594* amdgpu_atpx_detect - detect whether we have PX595*596* Check if we have a PX system (all asics).597* Returns true if we have a PX system, false if not.598*/599static bool amdgpu_atpx_detect(void)600{601char acpi_method_name[255] = { 0 };602struct acpi_buffer buffer = {sizeof(acpi_method_name), acpi_method_name};603struct pci_dev *pdev = NULL;604bool has_atpx = false;605int vga_count = 0;606bool d3_supported = false;607struct pci_dev *parent_pdev;608609while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {610vga_count++;611612has_atpx |= amdgpu_atpx_pci_probe_handle(pdev);613614parent_pdev = pci_upstream_bridge(pdev);615d3_supported |= parent_pdev && parent_pdev->bridge_d3;616amdgpu_atpx_get_quirks(pdev);617}618619while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_OTHER << 8, pdev)) != NULL) {620vga_count++;621622has_atpx |= amdgpu_atpx_pci_probe_handle(pdev);623624parent_pdev = pci_upstream_bridge(pdev);625d3_supported |= parent_pdev && parent_pdev->bridge_d3;626amdgpu_atpx_get_quirks(pdev);627}628629if (has_atpx && vga_count == 2) {630acpi_get_name(amdgpu_atpx_priv.atpx.handle, ACPI_FULL_PATHNAME, &buffer);631pr_info("vga_switcheroo: detected switching method %s handle\n",632acpi_method_name);633amdgpu_atpx_priv.atpx_detected = true;634amdgpu_atpx_priv.bridge_pm_usable = d3_supported;635amdgpu_atpx_init();636return true;637}638return false;639}640641/**642* amdgpu_register_atpx_handler - register with vga_switcheroo643*644* Register the PX callbacks with vga_switcheroo (all asics).645*/646void amdgpu_register_atpx_handler(void)647{648bool r;649enum vga_switcheroo_handler_flags_t handler_flags = 0;650651/* detect if we have any ATPX + 2 VGA in the system */652r = amdgpu_atpx_detect();653if (!r)654return;655656vga_switcheroo_register_handler(&amdgpu_atpx_handler, handler_flags);657}658659/**660* amdgpu_unregister_atpx_handler - unregister with vga_switcheroo661*662* Unregister the PX callbacks with vga_switcheroo (all asics).663*/664void amdgpu_unregister_atpx_handler(void)665{666vga_switcheroo_unregister_handler();667}668669670