cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

dptf_pch_fivr.c (4322B)


      1// SPDX-License-Identifier: GPL-2.0-only
      2/*
      3 * dptf_pch_fivr:  DPTF PCH FIVR Participant driver
      4 * Copyright (c) 2020, Intel Corporation.
      5 */
      6
      7#include <linux/acpi.h>
      8#include <linux/kernel.h>
      9#include <linux/module.h>
     10#include <linux/platform_device.h>
     11
     12struct pch_fivr_resp {
     13	u64 status;
     14	u64 result;
     15};
     16
     17static int pch_fivr_read(acpi_handle handle, char *method, struct pch_fivr_resp *fivr_resp)
     18{
     19	struct acpi_buffer resp = { sizeof(struct pch_fivr_resp), fivr_resp};
     20	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
     21	struct acpi_buffer format = { sizeof("NN"), "NN" };
     22	union acpi_object *obj;
     23	acpi_status status;
     24	int ret = -EFAULT;
     25
     26	status = acpi_evaluate_object(handle, method, NULL, &buffer);
     27	if (ACPI_FAILURE(status))
     28		return ret;
     29
     30	obj = buffer.pointer;
     31	if (!obj || obj->type != ACPI_TYPE_PACKAGE)
     32		goto release_buffer;
     33
     34	status = acpi_extract_package(obj, &format, &resp);
     35	if (ACPI_FAILURE(status))
     36		goto release_buffer;
     37
     38	if (fivr_resp->status)
     39		goto release_buffer;
     40
     41	ret = 0;
     42
     43release_buffer:
     44	kfree(buffer.pointer);
     45	return ret;
     46}
     47
     48/*
     49 * Presentation of attributes which are defined for INTC10xx
     50 * They are:
     51 * freq_mhz_low_clock : Set PCH FIVR switching freq for
     52 *			FIVR clock 19.2MHz and 24MHz
     53 * freq_mhz_high_clock : Set PCH FIVR switching freq for
     54 *			FIVR clock 38.4MHz
     55 */
     56#define PCH_FIVR_SHOW(name, method) \
     57static ssize_t name##_show(struct device *dev,\
     58			   struct device_attribute *attr,\
     59			   char *buf)\
     60{\
     61	struct acpi_device *acpi_dev = dev_get_drvdata(dev);\
     62	struct pch_fivr_resp fivr_resp;\
     63	int status;\
     64\
     65	status = pch_fivr_read(acpi_dev->handle, #method, &fivr_resp);\
     66	if (status)\
     67		return status;\
     68\
     69	return sprintf(buf, "%llu\n", fivr_resp.result);\
     70}
     71
     72#define PCH_FIVR_STORE(name, method) \
     73static ssize_t name##_store(struct device *dev,\
     74			    struct device_attribute *attr,\
     75			    const char *buf, size_t count)\
     76{\
     77	struct acpi_device *acpi_dev = dev_get_drvdata(dev);\
     78	acpi_status status;\
     79	u32 val;\
     80\
     81	if (kstrtouint(buf, 0, &val) < 0)\
     82		return -EINVAL;\
     83\
     84	status = acpi_execute_simple_method(acpi_dev->handle, #method, val);\
     85	if (ACPI_SUCCESS(status))\
     86		return count;\
     87\
     88	return -EINVAL;\
     89}
     90
     91PCH_FIVR_SHOW(freq_mhz_low_clock, GFC0)
     92PCH_FIVR_SHOW(freq_mhz_high_clock, GFC1)
     93PCH_FIVR_SHOW(ssc_clock_info, GEMI)
     94PCH_FIVR_SHOW(fivr_switching_freq_mhz, GFCS)
     95PCH_FIVR_SHOW(fivr_switching_fault_status, GFFS)
     96PCH_FIVR_STORE(freq_mhz_low_clock, RFC0)
     97PCH_FIVR_STORE(freq_mhz_high_clock, RFC1)
     98
     99static DEVICE_ATTR_RW(freq_mhz_low_clock);
    100static DEVICE_ATTR_RW(freq_mhz_high_clock);
    101static DEVICE_ATTR_RO(ssc_clock_info);
    102static DEVICE_ATTR_RO(fivr_switching_freq_mhz);
    103static DEVICE_ATTR_RO(fivr_switching_fault_status);
    104
    105static struct attribute *fivr_attrs[] = {
    106	&dev_attr_freq_mhz_low_clock.attr,
    107	&dev_attr_freq_mhz_high_clock.attr,
    108	&dev_attr_ssc_clock_info.attr,
    109	&dev_attr_fivr_switching_freq_mhz.attr,
    110	&dev_attr_fivr_switching_fault_status.attr,
    111	NULL
    112};
    113
    114static const struct attribute_group pch_fivr_attribute_group = {
    115	.attrs = fivr_attrs,
    116	.name = "pch_fivr_switch_frequency"
    117};
    118
    119static int pch_fivr_add(struct platform_device *pdev)
    120{
    121	struct acpi_device *acpi_dev;
    122	unsigned long long ptype;
    123	acpi_status status;
    124	int result;
    125
    126	acpi_dev = ACPI_COMPANION(&(pdev->dev));
    127	if (!acpi_dev)
    128		return -ENODEV;
    129
    130	status = acpi_evaluate_integer(acpi_dev->handle, "PTYP", NULL, &ptype);
    131	if (ACPI_FAILURE(status) || ptype != 0x05)
    132		return -ENODEV;
    133
    134	result = sysfs_create_group(&pdev->dev.kobj,
    135				    &pch_fivr_attribute_group);
    136	if (result)
    137		return result;
    138
    139	platform_set_drvdata(pdev, acpi_dev);
    140
    141	return 0;
    142}
    143
    144static int pch_fivr_remove(struct platform_device *pdev)
    145{
    146	sysfs_remove_group(&pdev->dev.kobj, &pch_fivr_attribute_group);
    147
    148	return 0;
    149}
    150
    151static const struct acpi_device_id pch_fivr_device_ids[] = {
    152	{"INTC1045", 0},
    153	{"INTC1049", 0},
    154	{"INTC1064", 0},
    155	{"INTC10A3", 0},
    156	{"", 0},
    157};
    158MODULE_DEVICE_TABLE(acpi, pch_fivr_device_ids);
    159
    160static struct platform_driver pch_fivr_driver = {
    161	.probe = pch_fivr_add,
    162	.remove = pch_fivr_remove,
    163	.driver = {
    164		.name = "dptf_pch_fivr",
    165		.acpi_match_table = pch_fivr_device_ids,
    166	},
    167};
    168
    169module_platform_driver(pch_fivr_driver);
    170
    171MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
    172MODULE_LICENSE("GPL v2");
    173MODULE_DESCRIPTION("ACPI DPTF PCH FIVR driver");