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

pvpanic-mmio.c (2970B)


      1// SPDX-License-Identifier: GPL-2.0+
      2/*
      3 *  Pvpanic MMIO Device Support
      4 *
      5 *  Copyright (C) 2013 Fujitsu.
      6 *  Copyright (C) 2018 ZTE.
      7 *  Copyright (C) 2021 Oracle.
      8 */
      9
     10#include <linux/io.h>
     11#include <linux/kernel.h>
     12#include <linux/kexec.h>
     13#include <linux/mod_devicetable.h>
     14#include <linux/module.h>
     15#include <linux/platform_device.h>
     16#include <linux/types.h>
     17#include <linux/slab.h>
     18
     19#include <uapi/misc/pvpanic.h>
     20
     21#include "pvpanic.h"
     22
     23MODULE_AUTHOR("Hu Tao <hutao@cn.fujitsu.com>");
     24MODULE_DESCRIPTION("pvpanic-mmio device driver");
     25MODULE_LICENSE("GPL");
     26
     27static ssize_t capability_show(struct device *dev, struct device_attribute *attr, char *buf)
     28{
     29	struct pvpanic_instance *pi = dev_get_drvdata(dev);
     30
     31	return sysfs_emit(buf, "%x\n", pi->capability);
     32}
     33static DEVICE_ATTR_RO(capability);
     34
     35static ssize_t events_show(struct device *dev, struct device_attribute *attr, char *buf)
     36{
     37	struct pvpanic_instance *pi = dev_get_drvdata(dev);
     38
     39	return sysfs_emit(buf, "%x\n", pi->events);
     40}
     41
     42static ssize_t events_store(struct device *dev, struct device_attribute *attr,
     43			    const char *buf, size_t count)
     44{
     45	struct pvpanic_instance *pi = dev_get_drvdata(dev);
     46	unsigned int tmp;
     47	int err;
     48
     49	err = kstrtouint(buf, 16, &tmp);
     50	if (err)
     51		return err;
     52
     53	if ((tmp & pi->capability) != tmp)
     54		return -EINVAL;
     55
     56	pi->events = tmp;
     57
     58	return count;
     59}
     60static DEVICE_ATTR_RW(events);
     61
     62static struct attribute *pvpanic_mmio_dev_attrs[] = {
     63	&dev_attr_capability.attr,
     64	&dev_attr_events.attr,
     65	NULL
     66};
     67ATTRIBUTE_GROUPS(pvpanic_mmio_dev);
     68
     69static int pvpanic_mmio_probe(struct platform_device *pdev)
     70{
     71	struct device *dev = &pdev->dev;
     72	struct pvpanic_instance *pi;
     73	struct resource *res;
     74	void __iomem *base;
     75
     76	res = platform_get_mem_or_io(pdev, 0);
     77	if (!res)
     78		return -EINVAL;
     79
     80	switch (resource_type(res)) {
     81	case IORESOURCE_IO:
     82		base = devm_ioport_map(dev, res->start, resource_size(res));
     83		if (!base)
     84			return -ENOMEM;
     85		break;
     86	case IORESOURCE_MEM:
     87		base = devm_ioremap_resource(dev, res);
     88		if (IS_ERR(base))
     89			return PTR_ERR(base);
     90		break;
     91	default:
     92		return -EINVAL;
     93	}
     94
     95	pi = devm_kmalloc(dev, sizeof(*pi), GFP_KERNEL);
     96	if (!pi)
     97		return -ENOMEM;
     98
     99	pi->base = base;
    100	pi->capability = PVPANIC_PANICKED | PVPANIC_CRASH_LOADED;
    101
    102	/* initialize capability by RDPT */
    103	pi->capability &= ioread8(base);
    104	pi->events = pi->capability;
    105
    106	return devm_pvpanic_probe(dev, pi);
    107}
    108
    109static const struct of_device_id pvpanic_mmio_match[] = {
    110	{ .compatible = "qemu,pvpanic-mmio", },
    111	{}
    112};
    113MODULE_DEVICE_TABLE(of, pvpanic_mmio_match);
    114
    115static const struct acpi_device_id pvpanic_device_ids[] = {
    116	{ "QEMU0001", 0 },
    117	{ "", 0 }
    118};
    119MODULE_DEVICE_TABLE(acpi, pvpanic_device_ids);
    120
    121static struct platform_driver pvpanic_mmio_driver = {
    122	.driver = {
    123		.name = "pvpanic-mmio",
    124		.of_match_table = pvpanic_mmio_match,
    125		.acpi_match_table = pvpanic_device_ids,
    126		.dev_groups = pvpanic_mmio_dev_groups,
    127	},
    128	.probe = pvpanic_mmio_probe,
    129};
    130module_platform_driver(pvpanic_mmio_driver);