1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * AMD 3D V-Cache Performance Optimizer Driver
5 * Copyright (c) 2024, Advanced Micro Devices, Inc.
8 * Authors: Basavaraj Natikar <Basavaraj.Natikar@amd.com>
9 * Perry Yuan <perry.yuan@amd.com>
10 * Mario Limonciello <mario.limonciello@amd.com>
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/acpi.h>
16 #include <linux/array_size.h>
17 #include <linux/device.h>
18 #include <linux/errno.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <linux/platform_device.h>
23 #include <linux/sysfs.h>
24 #include <linux/uuid.h>
26 static char *x3d_mode
= "frequency";
27 module_param(x3d_mode
, charp
, 0);
28 MODULE_PARM_DESC(x3d_mode
, "Initial 3D-VCache mode; 'frequency' (default) or 'cache'");
30 #define DSM_REVISION_ID 0
31 #define DSM_SET_X3D_MODE 1
33 static guid_t x3d_guid
= GUID_INIT(0xdff8e55f, 0xbcfd, 0x46fb, 0xba, 0x0a,
34 0xef, 0xd0, 0x45, 0x0f, 0x34, 0xee);
36 enum amd_x3d_mode_type
{
41 static const char * const amd_x3d_mode_strings
[] = {
42 [MODE_INDEX_FREQ
] = "frequency",
43 [MODE_INDEX_CACHE
] = "cache",
49 /* To protect x3d mode setting */
51 enum amd_x3d_mode_type curr_mode
;
54 static int amd_x3d_get_mode(struct amd_x3d_dev
*data
)
56 guard(mutex
)(&data
->lock
);
58 return data
->curr_mode
;
61 static int amd_x3d_mode_switch(struct amd_x3d_dev
*data
, int new_state
)
63 union acpi_object
*out
, argv
;
65 guard(mutex
)(&data
->lock
);
66 argv
.type
= ACPI_TYPE_INTEGER
;
67 argv
.integer
.value
= new_state
;
69 out
= acpi_evaluate_dsm(data
->ahandle
, &x3d_guid
, DSM_REVISION_ID
,
70 DSM_SET_X3D_MODE
, &argv
);
72 dev_err(data
->dev
, "failed to evaluate _DSM\n");
76 data
->curr_mode
= new_state
;
83 static ssize_t
amd_x3d_mode_store(struct device
*dev
, struct device_attribute
*attr
,
84 const char *buf
, size_t count
)
86 struct amd_x3d_dev
*data
= dev_get_drvdata(dev
);
89 ret
= sysfs_match_string(amd_x3d_mode_strings
, buf
);
93 ret
= amd_x3d_mode_switch(data
, ret
);
100 static ssize_t
amd_x3d_mode_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
102 struct amd_x3d_dev
*data
= dev_get_drvdata(dev
);
103 int mode
= amd_x3d_get_mode(data
);
105 return sysfs_emit(buf
, "%s\n", amd_x3d_mode_strings
[mode
]);
107 static DEVICE_ATTR_RW(amd_x3d_mode
);
109 static struct attribute
*amd_x3d_attrs
[] = {
110 &dev_attr_amd_x3d_mode
.attr
,
113 ATTRIBUTE_GROUPS(amd_x3d
);
115 static int amd_x3d_resume_handler(struct device
*dev
)
117 struct amd_x3d_dev
*data
= dev_get_drvdata(dev
);
118 int ret
= amd_x3d_get_mode(data
);
120 return amd_x3d_mode_switch(data
, ret
);
123 static DEFINE_SIMPLE_DEV_PM_OPS(amd_x3d_pm
, NULL
, amd_x3d_resume_handler
);
125 static const struct acpi_device_id amd_x3d_acpi_ids
[] = {
129 MODULE_DEVICE_TABLE(acpi
, amd_x3d_acpi_ids
);
131 static int amd_x3d_probe(struct platform_device
*pdev
)
133 struct amd_x3d_dev
*data
;
137 handle
= ACPI_HANDLE(&pdev
->dev
);
141 if (!acpi_check_dsm(handle
, &x3d_guid
, DSM_REVISION_ID
, BIT(DSM_SET_X3D_MODE
)))
144 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
148 data
->dev
= &pdev
->dev
;
150 ret
= devm_mutex_init(data
->dev
, &data
->lock
);
154 data
->ahandle
= handle
;
155 platform_set_drvdata(pdev
, data
);
157 ret
= match_string(amd_x3d_mode_strings
, ARRAY_SIZE(amd_x3d_mode_strings
), x3d_mode
);
159 return dev_err_probe(&pdev
->dev
, -EINVAL
, "invalid mode %s\n", x3d_mode
);
161 return amd_x3d_mode_switch(data
, ret
);
164 static struct platform_driver amd_3d_vcache_driver
= {
166 .name
= "amd_x3d_vcache",
167 .dev_groups
= amd_x3d_groups
,
168 .acpi_match_table
= amd_x3d_acpi_ids
,
169 .pm
= pm_sleep_ptr(&amd_x3d_pm
),
171 .probe
= amd_x3d_probe
,
173 module_platform_driver(amd_3d_vcache_driver
);
175 MODULE_DESCRIPTION("AMD 3D V-Cache Performance Optimizer Driver");
176 MODULE_LICENSE("GPL");