2021-03-24 07:49:14 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
/*
|
|
|
|
* Pvpanic Device Support
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Fujitsu.
|
|
|
|
* Copyright (C) 2018 ZTE.
|
|
|
|
* Copyright (C) 2021 Oracle.
|
|
|
|
*/
|
|
|
|
|
2023-10-11 00:18:27 -07:00
|
|
|
#include <linux/device.h>
|
2023-12-21 07:44:47 -07:00
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/gfp_types.h>
|
2021-03-24 07:49:14 -07:00
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/kexec.h>
|
2023-12-21 07:44:47 -07:00
|
|
|
#include <linux/kstrtox.h>
|
|
|
|
#include <linux/limits.h>
|
|
|
|
#include <linux/list.h>
|
2021-03-24 07:49:14 -07:00
|
|
|
#include <linux/mod_devicetable.h>
|
|
|
|
#include <linux/module.h>
|
2021-06-30 18:54:59 -07:00
|
|
|
#include <linux/panic_notifier.h>
|
2023-12-21 07:44:47 -07:00
|
|
|
#include <linux/platform_device.h>
|
2024-04-11 23:26:40 -07:00
|
|
|
#include <linux/reboot.h>
|
2023-12-21 07:44:47 -07:00
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/sysfs.h>
|
2021-03-24 07:49:14 -07:00
|
|
|
#include <linux/types.h>
|
|
|
|
|
|
|
|
#include <uapi/misc/pvpanic.h>
|
|
|
|
|
|
|
|
#include "pvpanic.h"
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Mihai Carabas <mihai.carabas@oracle.com>");
|
2021-08-29 05:43:54 -07:00
|
|
|
MODULE_DESCRIPTION("pvpanic device driver");
|
2021-03-24 07:49:14 -07:00
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|
2023-10-11 00:18:27 -07:00
|
|
|
struct pvpanic_instance {
|
|
|
|
void __iomem *base;
|
|
|
|
unsigned int capability;
|
|
|
|
unsigned int events;
|
2024-04-11 23:26:40 -07:00
|
|
|
struct sys_off_handler *sys_off;
|
2023-10-11 00:18:27 -07:00
|
|
|
struct list_head list;
|
|
|
|
};
|
|
|
|
|
2021-03-31 05:17:06 -07:00
|
|
|
static struct list_head pvpanic_list;
|
|
|
|
static spinlock_t pvpanic_lock;
|
2021-03-24 07:49:14 -07:00
|
|
|
|
|
|
|
static void
|
|
|
|
pvpanic_send_event(unsigned int event)
|
|
|
|
{
|
2021-03-24 07:49:15 -07:00
|
|
|
struct pvpanic_instance *pi_cur;
|
|
|
|
|
2022-04-27 15:48:59 -07:00
|
|
|
if (!spin_trylock(&pvpanic_lock))
|
|
|
|
return;
|
|
|
|
|
2021-03-24 07:49:15 -07:00
|
|
|
list_for_each_entry(pi_cur, &pvpanic_list, list) {
|
|
|
|
if (event & pi_cur->capability & pi_cur->events)
|
|
|
|
iowrite8(event, pi_cur->base);
|
|
|
|
}
|
|
|
|
spin_unlock(&pvpanic_lock);
|
2021-03-24 07:49:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-08-29 05:43:54 -07:00
|
|
|
pvpanic_panic_notify(struct notifier_block *nb, unsigned long code, void *unused)
|
2021-03-24 07:49:14 -07:00
|
|
|
{
|
|
|
|
unsigned int event = PVPANIC_PANICKED;
|
|
|
|
|
|
|
|
if (kexec_crash_loaded())
|
|
|
|
event = PVPANIC_CRASH_LOADED;
|
|
|
|
|
|
|
|
pvpanic_send_event(event);
|
|
|
|
|
|
|
|
return NOTIFY_DONE;
|
|
|
|
}
|
|
|
|
|
2022-04-27 15:48:59 -07:00
|
|
|
/*
|
|
|
|
* Call our notifier very early on panic, deferring the
|
|
|
|
* action taken to the hypervisor.
|
|
|
|
*/
|
2021-03-24 07:49:14 -07:00
|
|
|
static struct notifier_block pvpanic_panic_nb = {
|
|
|
|
.notifier_call = pvpanic_panic_notify,
|
2022-04-27 15:48:59 -07:00
|
|
|
.priority = INT_MAX,
|
2021-03-24 07:49:14 -07:00
|
|
|
};
|
|
|
|
|
2024-04-11 23:26:40 -07:00
|
|
|
static int pvpanic_sys_off(struct sys_off_data *data)
|
|
|
|
{
|
|
|
|
pvpanic_send_event(PVPANIC_SHUTDOWN);
|
|
|
|
|
|
|
|
return NOTIFY_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pvpanic_synchronize_sys_off_handler(struct device *dev, struct pvpanic_instance *pi)
|
|
|
|
{
|
|
|
|
/* The kernel core has logic to fall back to system halt if no
|
|
|
|
* sys_off_handler is registered.
|
|
|
|
* When the pvpanic sys_off_handler is disabled via sysfs the kernel
|
|
|
|
* should use that fallback logic, so the handler needs to be unregistered.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct sys_off_handler *sys_off;
|
|
|
|
|
|
|
|
if (!(pi->events & PVPANIC_SHUTDOWN) == !pi->sys_off)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!pi->sys_off) {
|
|
|
|
sys_off = register_sys_off_handler(SYS_OFF_MODE_POWER_OFF, SYS_OFF_PRIO_LOW,
|
|
|
|
pvpanic_sys_off, NULL);
|
|
|
|
if (IS_ERR(sys_off))
|
|
|
|
dev_warn(dev, "Could not register sys_off_handler: %pe\n", sys_off);
|
|
|
|
else
|
|
|
|
pi->sys_off = sys_off;
|
|
|
|
} else {
|
|
|
|
unregister_sys_off_handler(pi->sys_off);
|
|
|
|
pi->sys_off = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-21 23:55:20 -07:00
|
|
|
static void pvpanic_remove(void *param)
|
2021-03-24 07:49:14 -07:00
|
|
|
{
|
2021-03-24 07:49:15 -07:00
|
|
|
struct pvpanic_instance *pi_cur, *pi_next;
|
2021-05-21 23:55:20 -07:00
|
|
|
struct pvpanic_instance *pi = param;
|
2021-03-24 07:49:15 -07:00
|
|
|
|
|
|
|
spin_lock(&pvpanic_lock);
|
|
|
|
list_for_each_entry_safe(pi_cur, pi_next, &pvpanic_list, list) {
|
|
|
|
if (pi_cur == pi) {
|
|
|
|
list_del(&pi_cur->list);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spin_unlock(&pvpanic_lock);
|
2024-04-11 23:26:40 -07:00
|
|
|
|
|
|
|
unregister_sys_off_handler(pi->sys_off);
|
2021-03-24 07:49:14 -07:00
|
|
|
}
|
2021-05-21 23:55:20 -07:00
|
|
|
|
2023-10-11 00:18:27 -07:00
|
|
|
static ssize_t capability_show(struct device *dev, struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct pvpanic_instance *pi = dev_get_drvdata(dev);
|
|
|
|
|
|
|
|
return sysfs_emit(buf, "%x\n", pi->capability);
|
|
|
|
}
|
|
|
|
static DEVICE_ATTR_RO(capability);
|
|
|
|
|
|
|
|
static ssize_t events_show(struct device *dev, struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct pvpanic_instance *pi = dev_get_drvdata(dev);
|
|
|
|
|
|
|
|
return sysfs_emit(buf, "%x\n", pi->events);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t events_store(struct device *dev, struct device_attribute *attr,
|
|
|
|
const char *buf, size_t count)
|
|
|
|
{
|
|
|
|
struct pvpanic_instance *pi = dev_get_drvdata(dev);
|
|
|
|
unsigned int tmp;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = kstrtouint(buf, 16, &tmp);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if ((tmp & pi->capability) != tmp)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
pi->events = tmp;
|
2024-04-11 23:26:40 -07:00
|
|
|
pvpanic_synchronize_sys_off_handler(dev, pi);
|
2023-10-11 00:18:27 -07:00
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
static DEVICE_ATTR_RW(events);
|
|
|
|
|
|
|
|
static struct attribute *pvpanic_dev_attrs[] = {
|
|
|
|
&dev_attr_capability.attr,
|
|
|
|
&dev_attr_events.attr,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct attribute_group pvpanic_dev_group = {
|
|
|
|
.attrs = pvpanic_dev_attrs,
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct attribute_group *pvpanic_dev_groups[] = {
|
|
|
|
&pvpanic_dev_group,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
EXPORT_SYMBOL_GPL(pvpanic_dev_groups);
|
|
|
|
|
|
|
|
int devm_pvpanic_probe(struct device *dev, void __iomem *base)
|
2021-05-21 23:55:20 -07:00
|
|
|
{
|
2023-10-11 00:18:27 -07:00
|
|
|
struct pvpanic_instance *pi;
|
|
|
|
|
|
|
|
if (!base)
|
2021-05-21 23:55:20 -07:00
|
|
|
return -EINVAL;
|
|
|
|
|
2023-10-11 00:18:27 -07:00
|
|
|
pi = devm_kmalloc(dev, sizeof(*pi), GFP_KERNEL);
|
|
|
|
if (!pi)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
pi->base = base;
|
2024-04-11 23:26:40 -07:00
|
|
|
pi->capability = PVPANIC_PANICKED | PVPANIC_CRASH_LOADED | PVPANIC_SHUTDOWN;
|
2023-10-11 00:18:27 -07:00
|
|
|
|
|
|
|
/* initlize capability by RDPT */
|
|
|
|
pi->capability &= ioread8(base);
|
|
|
|
pi->events = pi->capability;
|
|
|
|
|
2024-04-11 23:26:40 -07:00
|
|
|
pi->sys_off = NULL;
|
|
|
|
pvpanic_synchronize_sys_off_handler(dev, pi);
|
|
|
|
|
2021-05-21 23:55:20 -07:00
|
|
|
spin_lock(&pvpanic_lock);
|
|
|
|
list_add(&pi->list, &pvpanic_list);
|
|
|
|
spin_unlock(&pvpanic_lock);
|
|
|
|
|
2021-08-19 08:12:26 -07:00
|
|
|
dev_set_drvdata(dev, pi);
|
|
|
|
|
2021-05-21 23:55:20 -07:00
|
|
|
return devm_add_action_or_reset(dev, pvpanic_remove, pi);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(devm_pvpanic_probe);
|
2021-03-24 07:49:14 -07:00
|
|
|
|
2021-03-24 07:49:15 -07:00
|
|
|
static int pvpanic_init(void)
|
2021-03-24 07:49:14 -07:00
|
|
|
{
|
2021-03-24 07:49:15 -07:00
|
|
|
INIT_LIST_HEAD(&pvpanic_list);
|
|
|
|
spin_lock_init(&pvpanic_lock);
|
|
|
|
|
2021-08-29 05:43:54 -07:00
|
|
|
atomic_notifier_chain_register(&panic_notifier_list, &pvpanic_panic_nb);
|
2021-03-24 07:49:15 -07:00
|
|
|
|
|
|
|
return 0;
|
2021-03-24 07:49:14 -07:00
|
|
|
}
|
2021-08-29 05:43:52 -07:00
|
|
|
module_init(pvpanic_init);
|
2021-03-24 07:49:15 -07:00
|
|
|
|
|
|
|
static void pvpanic_exit(void)
|
|
|
|
{
|
2021-08-29 05:43:54 -07:00
|
|
|
atomic_notifier_chain_unregister(&panic_notifier_list, &pvpanic_panic_nb);
|
2021-03-24 07:49:15 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
module_exit(pvpanic_exit);
|