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)  * pcpu.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Management physical cpu in dom0, get pcpu info and provide sys interface
^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)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * This program is free software; you can redistribute it and/or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * modify it under the terms of the GNU General Public License version 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * as published by the Free Software Foundation; or, when distributed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * separately from the Linux kernel or incorporated into other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * software packages, subject to the following license:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * Permission is hereby granted, free of charge, to any person obtaining a copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * of this source file (the "Software"), to deal in the Software without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * restriction, including without limitation the rights to use, copy, modify,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * and to permit persons to whom the Software is furnished to do so, subject to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * the following conditions:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * The above copyright notice and this permission notice shall be included in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * all copies or substantial portions of the Software.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * IN THE SOFTWARE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define pr_fmt(fmt) "xen_cpu: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #include <linux/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #include <xen/xen.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #include <xen/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #include <xen/xenbus.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #include <xen/events.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #include <xen/interface/platform.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #include <asm/xen/hypervisor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #include <asm/xen/hypercall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  * @cpu_id: Xen physical cpu logic number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  * @flags: Xen physical cpu status flag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  * - XEN_PCPU_FLAGS_ONLINE: cpu is online
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  * - XEN_PCPU_FLAGS_INVALID: cpu is not present
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) struct pcpu {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	struct device dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	uint32_t cpu_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	uint32_t flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static struct bus_type xen_pcpu_subsys = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	.name = "xen_cpu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	.dev_name = "xen_cpu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) static DEFINE_MUTEX(xen_pcpu_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static LIST_HEAD(xen_pcpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) static int xen_pcpu_down(uint32_t cpu_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	struct xen_platform_op op = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		.cmd			= XENPF_cpu_offline,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		.interface_version	= XENPF_INTERFACE_VERSION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		.u.cpu_ol.cpuid		= cpu_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	return HYPERVISOR_platform_op(&op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) static int xen_pcpu_up(uint32_t cpu_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	struct xen_platform_op op = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		.cmd			= XENPF_cpu_online,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		.interface_version	= XENPF_INTERFACE_VERSION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		.u.cpu_ol.cpuid		= cpu_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	return HYPERVISOR_platform_op(&op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) static ssize_t show_online(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			   struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			   char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	struct pcpu *cpu = container_of(dev, struct pcpu, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	return sprintf(buf, "%u\n", !!(cpu->flags & XEN_PCPU_FLAGS_ONLINE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static ssize_t __ref store_online(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 				  struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 				  const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	struct pcpu *pcpu = container_of(dev, struct pcpu, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	unsigned long long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (kstrtoull(buf, 0, &val) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	switch (val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		ret = xen_pcpu_down(pcpu->cpu_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		ret = xen_pcpu_up(pcpu->cpu_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static DEVICE_ATTR(online, S_IRUGO | S_IWUSR, show_online, store_online);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static struct attribute *pcpu_dev_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	&dev_attr_online.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static umode_t pcpu_dev_is_visible(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 				   struct attribute *attr, int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	struct device *dev = kobj_to_dev(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	 * Xen never offline cpu0 due to several restrictions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	 * and assumptions. This basically doesn't add a sys control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	 * to user, one cannot attempt to offline BSP.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	return dev->id ? attr->mode : 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 const struct attribute_group pcpu_dev_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	.attrs = pcpu_dev_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	.is_visible = pcpu_dev_is_visible,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static const struct attribute_group *pcpu_dev_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	&pcpu_dev_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static bool xen_pcpu_online(uint32_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	return !!(flags & XEN_PCPU_FLAGS_ONLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static void pcpu_online_status(struct xenpf_pcpuinfo *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			       struct pcpu *pcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	if (xen_pcpu_online(info->flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	   !xen_pcpu_online(pcpu->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		/* the pcpu is onlined */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		pcpu->flags |= XEN_PCPU_FLAGS_ONLINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		kobject_uevent(&pcpu->dev.kobj, KOBJ_ONLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	} else if (!xen_pcpu_online(info->flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		    xen_pcpu_online(pcpu->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		/* The pcpu is offlined */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		pcpu->flags &= ~XEN_PCPU_FLAGS_ONLINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		kobject_uevent(&pcpu->dev.kobj, KOBJ_OFFLINE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static struct pcpu *get_pcpu(uint32_t cpu_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	struct pcpu *pcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	list_for_each_entry(pcpu, &xen_pcpus, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		if (pcpu->cpu_id == cpu_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			return pcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	return NULL;
^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) static void pcpu_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	struct pcpu *pcpu = container_of(dev, struct pcpu, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	list_del(&pcpu->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	kfree(pcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static void unregister_and_remove_pcpu(struct pcpu *pcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (!pcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	dev = &pcpu->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	/* pcpu remove would be implicitly done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	device_unregister(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static int register_pcpu(struct pcpu *pcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	if (!pcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	dev = &pcpu->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	dev->bus = &xen_pcpu_subsys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	dev->id = pcpu->cpu_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	dev->release = pcpu_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	dev->groups = pcpu_dev_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	err = device_register(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		pcpu_release(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) static struct pcpu *create_and_register_pcpu(struct xenpf_pcpuinfo *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	struct pcpu *pcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (info->flags & XEN_PCPU_FLAGS_INVALID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	pcpu = kzalloc(sizeof(struct pcpu), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	if (!pcpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	INIT_LIST_HEAD(&pcpu->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	pcpu->cpu_id = info->xen_cpuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	pcpu->flags = info->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	/* Need hold on xen_pcpu_lock before pcpu list manipulations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	list_add_tail(&pcpu->list, &xen_pcpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	err = register_pcpu(pcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		pr_warn("Failed to register pcpu%u\n", info->xen_cpuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		return ERR_PTR(-ENOENT);
^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) 	return pcpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)  * Caller should hold the xen_pcpu_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static int sync_pcpu(uint32_t cpu, uint32_t *max_cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	struct pcpu *pcpu = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	struct xenpf_pcpuinfo *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	struct xen_platform_op op = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		.cmd                   = XENPF_get_cpuinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		.interface_version     = XENPF_INTERFACE_VERSION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		.u.pcpu_info.xen_cpuid = cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	ret = HYPERVISOR_platform_op(&op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	info = &op.u.pcpu_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	if (max_cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		*max_cpu = info->max_present;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	pcpu = get_pcpu(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	 * Only those at cpu present map has its sys interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	if (info->flags & XEN_PCPU_FLAGS_INVALID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		unregister_and_remove_pcpu(pcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	if (!pcpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		pcpu = create_and_register_pcpu(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		if (IS_ERR_OR_NULL(pcpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		pcpu_online_status(info, pcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)  * Sync dom0's pcpu information with xen hypervisor's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) static int xen_sync_pcpus(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	 * Boot cpu always have cpu_id 0 in xen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	uint32_t cpu = 0, max_cpu = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	struct pcpu *pcpu, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	mutex_lock(&xen_pcpu_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	while (!err && (cpu <= max_cpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		err = sync_pcpu(cpu, &max_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		cpu++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		list_for_each_entry_safe(pcpu, tmp, &xen_pcpus, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 			unregister_and_remove_pcpu(pcpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	mutex_unlock(&xen_pcpu_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) static void xen_pcpu_work_fn(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	xen_sync_pcpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) static DECLARE_WORK(xen_pcpu_work, xen_pcpu_work_fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) static irqreturn_t xen_pcpu_interrupt(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	schedule_work(&xen_pcpu_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) /* Sync with Xen hypervisor after cpu hotadded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) void xen_pcpu_hotplug_sync(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	schedule_work(&xen_pcpu_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) EXPORT_SYMBOL_GPL(xen_pcpu_hotplug_sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)  * For hypervisor presented cpu, return logic cpu id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)  * For hypervisor non-presented cpu, return -ENODEV.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) int xen_pcpu_id(uint32_t acpi_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	int cpu_id = 0, max_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	struct xen_platform_op op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	op.cmd = XENPF_get_cpuinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	while (cpu_id <= max_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		op.u.pcpu_info.xen_cpuid = cpu_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		if (HYPERVISOR_platform_op(&op)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 			cpu_id++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		if (acpi_id == op.u.pcpu_info.acpi_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 			return cpu_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		if (op.u.pcpu_info.max_present > max_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 			max_id = op.u.pcpu_info.max_present;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		cpu_id++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) EXPORT_SYMBOL_GPL(xen_pcpu_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) static int __init xen_pcpu_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	int irq, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	if (!xen_initial_domain())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	irq = bind_virq_to_irqhandler(VIRQ_PCPU_STATE, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 				      xen_pcpu_interrupt, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 				      "xen-pcpu", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	if (irq < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		pr_warn("Failed to bind pcpu virq\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		return irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	ret = subsys_system_register(&xen_pcpu_subsys, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		pr_warn("Failed to register pcpu subsys\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		goto err1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	ret = xen_sync_pcpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		pr_warn("Failed to sync pcpu info\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		goto err2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) err2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	bus_unregister(&xen_pcpu_subsys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) err1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	unbind_from_irqhandler(irq, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) arch_initcall(xen_pcpu_init);