Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /******************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * mcelog.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Driver for receiving and transferring machine check error infomation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 2012 Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Author: Liu, Jinsong <jinsong.liu@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author: Jiang, Yunhong <yunhong.jiang@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Author: Ke, Liping <liping.ke@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * This program is free software; you can redistribute it and/or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * modify it under the terms of the GNU General Public License version 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * as published by the Free Software Foundation; or, when distributed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * separately from the Linux kernel or incorporated into other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * software packages, subject to the following license:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * Permission is hereby granted, free of charge, to any person obtaining a copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * of this source file (the "Software"), to deal in the Software without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * restriction, including without limitation the rights to use, copy, modify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * and to permit persons to whom the Software is furnished to do so, subject to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * the following conditions:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * The above copyright notice and this permission notice shall be included in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * all copies or substantial portions of the Software.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * IN THE SOFTWARE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define pr_fmt(fmt) "xen_mcelog: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #include <xen/interface/xen.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #include <xen/events.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #include <xen/interface/vcpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #include <xen/xen.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #include <asm/xen/hypercall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #include <asm/xen/hypervisor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) static struct mc_info g_mi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static struct mcinfo_logical_cpu *g_physinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static uint32_t ncpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static DEFINE_MUTEX(mcelog_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static struct xen_mce_log xen_mcelog = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	.signature	= XEN_MCE_LOG_SIGNATURE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	.len		= XEN_MCE_LOG_LEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	.recordlen	= sizeof(struct xen_mce),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static DEFINE_SPINLOCK(xen_mce_chrdev_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) static int xen_mce_chrdev_open_count;	/* #times opened */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static int xen_mce_chrdev_open_exclu;	/* already open exclusive? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static DECLARE_WAIT_QUEUE_HEAD(xen_mce_chrdev_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) static int xen_mce_chrdev_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	spin_lock(&xen_mce_chrdev_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	if (xen_mce_chrdev_open_exclu ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	    (xen_mce_chrdev_open_count && (file->f_flags & O_EXCL))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		spin_unlock(&xen_mce_chrdev_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if (file->f_flags & O_EXCL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		xen_mce_chrdev_open_exclu = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	xen_mce_chrdev_open_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	spin_unlock(&xen_mce_chrdev_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	return nonseekable_open(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) static int xen_mce_chrdev_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	spin_lock(&xen_mce_chrdev_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	xen_mce_chrdev_open_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	xen_mce_chrdev_open_exclu = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	spin_unlock(&xen_mce_chrdev_state_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static ssize_t xen_mce_chrdev_read(struct file *filp, char __user *ubuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 				size_t usize, loff_t *off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	char __user *buf = ubuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	unsigned num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	int i, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	mutex_lock(&mcelog_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	num = xen_mcelog.next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	/* Only supports full reads right now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	if (*off != 0 || usize < XEN_MCE_LOG_LEN*sizeof(struct xen_mce))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	for (i = 0; i < num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		struct xen_mce *m = &xen_mcelog.entry[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		err |= copy_to_user(buf, m, sizeof(*m));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		buf += sizeof(*m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	memset(xen_mcelog.entry, 0, num * sizeof(struct xen_mce));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	xen_mcelog.next = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	mutex_unlock(&mcelog_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	return err ? err : buf - ubuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static __poll_t xen_mce_chrdev_poll(struct file *file, poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	poll_wait(file, &xen_mce_chrdev_wait, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	if (xen_mcelog.next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		return EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static long xen_mce_chrdev_ioctl(struct file *f, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 				unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	int __user *p = (int __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	case MCE_GET_RECORD_LEN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		return put_user(sizeof(struct xen_mce), p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	case MCE_GET_LOG_LEN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		return put_user(XEN_MCE_LOG_LEN, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	case MCE_GETCLEAR_FLAGS: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		unsigned flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			flags = xen_mcelog.flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		} while (cmpxchg(&xen_mcelog.flags, flags, 0) != flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		return put_user(flags, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static const struct file_operations xen_mce_chrdev_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	.open			= xen_mce_chrdev_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	.release		= xen_mce_chrdev_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	.read			= xen_mce_chrdev_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	.poll			= xen_mce_chrdev_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	.unlocked_ioctl		= xen_mce_chrdev_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	.llseek			= no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static struct miscdevice xen_mce_chrdev_device = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	MISC_MCELOG_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	"mcelog",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	&xen_mce_chrdev_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)  * Caller should hold the mcelog_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static void xen_mce_log(struct xen_mce *mce)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	unsigned entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	entry = xen_mcelog.next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	 * When the buffer fills up discard new entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	 * Assume that the earlier errors are the more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	 * interesting ones:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	if (entry >= XEN_MCE_LOG_LEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		set_bit(XEN_MCE_OVERFLOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			(unsigned long *)&xen_mcelog.flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	memcpy(xen_mcelog.entry + entry, mce, sizeof(struct xen_mce));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	xen_mcelog.next++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static int convert_log(struct mc_info *mi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	struct mcinfo_common *mic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	struct mcinfo_global *mc_global;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	struct mcinfo_bank *mc_bank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	struct xen_mce m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	unsigned int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	mic = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	x86_mcinfo_lookup(&mic, mi, MC_TYPE_GLOBAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	if (unlikely(!mic)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		pr_warn("Failed to find global error info\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	memset(&m, 0, sizeof(struct xen_mce));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	mc_global = (struct mcinfo_global *)mic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	m.mcgstatus = mc_global->mc_gstatus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	m.apicid = mc_global->mc_apicid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	for (i = 0; i < ncpus; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		if (g_physinfo[i].mc_apicid == m.apicid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (unlikely(i == ncpus)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		pr_warn("Failed to match cpu with apicid %d\n", m.apicid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	m.socketid = g_physinfo[i].mc_chipid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	m.cpu = m.extcpu = g_physinfo[i].mc_cpunr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	m.cpuvendor = (__u8)g_physinfo[i].mc_vendor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	for (j = 0; j < g_physinfo[i].mc_nmsrvals; ++j)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		switch (g_physinfo[i].mc_msrvalues[j].reg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		case MSR_IA32_MCG_CAP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 			m.mcgcap = g_physinfo[i].mc_msrvalues[j].value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		case MSR_PPIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		case MSR_AMD_PPIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 			m.ppin = g_physinfo[i].mc_msrvalues[j].value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	mic = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	x86_mcinfo_lookup(&mic, mi, MC_TYPE_BANK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	if (unlikely(!mic)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		pr_warn("Fail to find bank error info\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		if ((!mic) || (mic->size == 0) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		    (mic->type != MC_TYPE_GLOBAL   &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		     mic->type != MC_TYPE_BANK     &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		     mic->type != MC_TYPE_EXTENDED &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		     mic->type != MC_TYPE_RECOVERY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		if (mic->type == MC_TYPE_BANK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			mc_bank = (struct mcinfo_bank *)mic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 			m.misc = mc_bank->mc_misc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 			m.status = mc_bank->mc_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			m.addr = mc_bank->mc_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 			m.tsc = mc_bank->mc_tsc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			m.bank = mc_bank->mc_bank;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			m.finished = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 			/*log this record*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 			xen_mce_log(&m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		mic = x86_mcinfo_next(mic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	} while (1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) static int mc_queue_handle(uint32_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	struct xen_mc mc_op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	mc_op.cmd = XEN_MC_fetch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	set_xen_guest_handle(mc_op.u.mc_fetch.data, &g_mi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		mc_op.u.mc_fetch.flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		ret = HYPERVISOR_mca(&mc_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			pr_err("Failed to fetch %surgent error log\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 			       flags == XEN_MC_URGENT ? "" : "non");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		if (mc_op.u.mc_fetch.flags & XEN_MC_NODATA ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		    mc_op.u.mc_fetch.flags & XEN_MC_FETCHFAILED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 			ret = convert_log(&g_mi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 				pr_warn("Failed to convert this error log, continue acking it anyway\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			mc_op.u.mc_fetch.flags = flags | XEN_MC_ACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			ret = HYPERVISOR_mca(&mc_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 			if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 				pr_err("Failed to ack previous error log\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	} while (1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) /* virq handler for machine check error info*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static void xen_mce_work_fn(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	mutex_lock(&mcelog_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	/* urgent mc_info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	err = mc_queue_handle(XEN_MC_URGENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		pr_err("Failed to handle urgent mc_info queue, continue handling nonurgent mc_info queue anyway\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	/* nonurgent mc_info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	err = mc_queue_handle(XEN_MC_NONURGENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		pr_err("Failed to handle nonurgent mc_info queue\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	/* wake processes polling /dev/mcelog */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	wake_up_interruptible(&xen_mce_chrdev_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	mutex_unlock(&mcelog_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) static DECLARE_WORK(xen_mce_work, xen_mce_work_fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) static irqreturn_t xen_mce_interrupt(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	schedule_work(&xen_mce_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) static int bind_virq_for_mce(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	struct xen_mc mc_op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	memset(&mc_op, 0, sizeof(struct xen_mc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	/* Fetch physical CPU Numbers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	mc_op.cmd = XEN_MC_physcpuinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	set_xen_guest_handle(mc_op.u.mc_physcpuinfo.info, g_physinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	ret = HYPERVISOR_mca(&mc_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		pr_err("Failed to get CPU numbers\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	/* Fetch each CPU Physical Info for later reference*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	ncpus = mc_op.u.mc_physcpuinfo.ncpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	g_physinfo = kcalloc(ncpus, sizeof(struct mcinfo_logical_cpu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 			     GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	if (!g_physinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	set_xen_guest_handle(mc_op.u.mc_physcpuinfo.info, g_physinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	ret = HYPERVISOR_mca(&mc_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		pr_err("Failed to get CPU info\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		kfree(g_physinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	ret  = bind_virq_to_irqhandler(VIRQ_MCA, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 				       xen_mce_interrupt, 0, "mce", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		pr_err("Failed to bind virq\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		kfree(g_physinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) static int __init xen_late_init_mcelog(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	/* Only DOM0 is responsible for MCE logging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	if (!xen_initial_domain())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	/* register character device /dev/mcelog for xen mcelog */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	ret = misc_register(&xen_mce_chrdev_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	ret = bind_virq_for_mce();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		goto deregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	pr_info("/dev/mcelog registered by Xen\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) deregister:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	misc_deregister(&xen_mce_chrdev_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) device_initcall(xen_late_init_mcelog);