^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * CPU subsystem support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/topology.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/node.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/percpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/cpufeature.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/tick.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/pm_qos.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/sched/isolation.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include "base.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static DEFINE_PER_CPU(struct device *, cpu_sys_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static int cpu_subsys_match(struct device *dev, struct device_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /* ACPI style match is the only one that may succeed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (acpi_driver_match_device(dev, drv))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #ifdef CONFIG_HOTPLUG_CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static void change_cpu_under_node(struct cpu *cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) unsigned int from_nid, unsigned int to_nid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) int cpuid = cpu->dev.id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) unregister_cpu_under_node(cpuid, from_nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) register_cpu_under_node(cpuid, to_nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) cpu->node_id = to_nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static int cpu_subsys_online(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct cpu *cpu = container_of(dev, struct cpu, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) int cpuid = dev->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int from_nid, to_nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) from_nid = cpu_to_node(cpuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (from_nid == NUMA_NO_NODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) ret = cpu_device_up(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * When hot adding memory to memoryless node and enabling a cpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * on the node, node number of the cpu may internally change.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) to_nid = cpu_to_node(cpuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (from_nid != to_nid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) change_cpu_under_node(cpu, from_nid, to_nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static int cpu_subsys_offline(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return cpu_device_down(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) void unregister_cpu(struct cpu *cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) int logical_cpu = cpu->dev.id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) device_unregister(&cpu->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) per_cpu(cpu_sys_devices, logical_cpu) = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static ssize_t cpu_probe_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) ssize_t cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) ret = lock_device_hotplug_sysfs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) cnt = arch_cpu_probe(buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) unlock_device_hotplug();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static ssize_t cpu_release_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) ssize_t cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) ret = lock_device_hotplug_sysfs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) cnt = arch_cpu_release(buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) unlock_device_hotplug();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static DEVICE_ATTR(probe, S_IWUSR, NULL, cpu_probe_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static DEVICE_ATTR(release, S_IWUSR, NULL, cpu_release_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) #endif /* CONFIG_HOTPLUG_CPU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct bus_type cpu_subsys = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) .name = "cpu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) .dev_name = "cpu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) .match = cpu_subsys_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) #ifdef CONFIG_HOTPLUG_CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) .online = cpu_subsys_online,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) .offline = cpu_subsys_offline,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) EXPORT_SYMBOL_GPL(cpu_subsys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) #ifdef CONFIG_KEXEC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) #include <linux/kexec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static ssize_t crash_notes_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct cpu *cpu = container_of(dev, struct cpu, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) unsigned long long addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) int cpunum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) cpunum = cpu->dev.id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * Might be reading other cpu's data based on which cpu read thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * has been scheduled. But cpu data (memory) is allocated once during
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * boot up and this data does not change there after. Hence this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * operation should be safe. No locking required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpunum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return sysfs_emit(buf, "%llx\n", addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static DEVICE_ATTR_ADMIN_RO(crash_notes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static ssize_t crash_notes_size_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return sysfs_emit(buf, "%zu\n", sizeof(note_buf_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static DEVICE_ATTR_ADMIN_RO(crash_notes_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static struct attribute *crash_note_cpu_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) &dev_attr_crash_notes.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) &dev_attr_crash_notes_size.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) NULL
^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) static struct attribute_group crash_note_cpu_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) .attrs = crash_note_cpu_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static const struct attribute_group *common_cpu_attr_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) #ifdef CONFIG_KEXEC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) &crash_note_cpu_attr_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static const struct attribute_group *hotplugable_cpu_attr_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) #ifdef CONFIG_KEXEC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) &crash_note_cpu_attr_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * Print cpu online, possible, present, and system maps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct cpu_attr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct device_attribute attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) const struct cpumask *const map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static ssize_t show_cpus_attr(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct cpu_attr *ca = container_of(attr, struct cpu_attr, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return cpumap_print_to_pagebuf(true, buf, ca->map);
^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) #define _CPU_ATTR(name, map) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) { __ATTR(name, 0444, show_cpus_attr, NULL), map }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) /* Keep in sync with cpu_subsys_attrs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static struct cpu_attr cpu_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) _CPU_ATTR(online, &__cpu_online_mask),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) _CPU_ATTR(possible, &__cpu_possible_mask),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) _CPU_ATTR(present, &__cpu_present_mask),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * Print values for NR_CPUS and offlined cpus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static ssize_t print_cpus_kernel_max(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) return sysfs_emit(buf, "%d\n", NR_CPUS - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static DEVICE_ATTR(kernel_max, 0444, print_cpus_kernel_max, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) /* arch-optional setting to enable display of offline cpus >= nr_cpu_ids */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) unsigned int total_cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) static ssize_t print_cpus_offline(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) int len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) cpumask_var_t offline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) /* display offline cpus < nr_cpu_ids */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (!alloc_cpumask_var(&offline, GFP_KERNEL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) cpumask_andnot(offline, cpu_possible_mask, cpu_online_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) len += sysfs_emit_at(buf, len, "%*pbl", cpumask_pr_args(offline));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) free_cpumask_var(offline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) /* display offline cpus >= nr_cpu_ids */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (total_cpus && nr_cpu_ids < total_cpus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) len += sysfs_emit_at(buf, len, ",");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) if (nr_cpu_ids == total_cpus-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) len += sysfs_emit_at(buf, len, "%u", nr_cpu_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) len += sysfs_emit_at(buf, len, "%u-%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) nr_cpu_ids, total_cpus - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) len += sysfs_emit_at(buf, len, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) static DEVICE_ATTR(offline, 0444, print_cpus_offline, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static ssize_t print_cpus_isolated(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) cpumask_var_t isolated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (!alloc_cpumask_var(&isolated, GFP_KERNEL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) cpumask_andnot(isolated, cpu_possible_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) housekeeping_cpumask(HK_FLAG_DOMAIN));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) len = sysfs_emit(buf, "%*pbl\n", cpumask_pr_args(isolated));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) free_cpumask_var(isolated);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) static DEVICE_ATTR(isolated, 0444, print_cpus_isolated, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) #ifdef CONFIG_NO_HZ_FULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) static ssize_t print_cpus_nohz_full(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return sysfs_emit(buf, "%*pbl\n", cpumask_pr_args(tick_nohz_full_mask));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) static DEVICE_ATTR(nohz_full, 0444, print_cpus_nohz_full, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) static void cpu_device_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * This is an empty function to prevent the driver core from spitting a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * warning at us. Yes, I know this is directly opposite of what the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * documentation for the driver core and kobjects say, and the author
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * of this code has already been publically ridiculed for doing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * something as foolish as this. However, at this point in time, it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * the only way to handle the issue of statically allocated cpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * devices. The different architectures will have their cpu device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * code reworked to properly handle this in the near future, so this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * function will then be changed to correctly free up the memory held
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * by the cpu device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) * Never copy this way of doing things, or you too will be made fun of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * on the linux-kernel list, you have been warned.
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) #ifdef CONFIG_GENERIC_CPU_AUTOPROBE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) static ssize_t print_cpu_modalias(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) int len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) u32 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) len += sysfs_emit_at(buf, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) "cpu:type:" CPU_FEATURE_TYPEFMT ":feature:",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) CPU_FEATURE_TYPEVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) for (i = 0; i < MAX_CPU_FEATURES; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (cpu_have_feature(i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) if (len + sizeof(",XXXX\n") >= PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) WARN(1, "CPU features overflow page\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) len += sysfs_emit_at(buf, len, ",%04X", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) len += sysfs_emit_at(buf, len, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) static int cpu_uevent(struct device *dev, struct kobj_uevent_env *env)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) char *buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) if (buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) print_cpu_modalias(NULL, NULL, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) add_uevent_var(env, "MODALIAS=%s", buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) * register_cpu - Setup a sysfs device for a CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * @cpu - cpu->hotpluggable field set to 1 will generate a control file in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * sysfs for this CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) * @num - CPU number to use when creating the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) * Initialize and register the CPU device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) int register_cpu(struct cpu *cpu, int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) cpu->node_id = cpu_to_node(num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) memset(&cpu->dev, 0x00, sizeof(struct device));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) cpu->dev.id = num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) cpu->dev.bus = &cpu_subsys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) cpu->dev.release = cpu_device_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) cpu->dev.offline_disabled = !cpu->hotpluggable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) cpu->dev.offline = !cpu_online(num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) cpu->dev.of_node = of_get_cpu_node(num, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) #ifdef CONFIG_GENERIC_CPU_AUTOPROBE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) cpu->dev.bus->uevent = cpu_uevent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) cpu->dev.groups = common_cpu_attr_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) if (cpu->hotpluggable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) cpu->dev.groups = hotplugable_cpu_attr_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) error = device_register(&cpu->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) put_device(&cpu->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) per_cpu(cpu_sys_devices, num) = &cpu->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) register_cpu_under_node(num, cpu_to_node(num));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) dev_pm_qos_expose_latency_limit(&cpu->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) PM_QOS_RESUME_LATENCY_NO_CONSTRAINT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) return 0;
^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) struct device *get_cpu_device(unsigned cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if (cpu < nr_cpu_ids && cpu_possible(cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return per_cpu(cpu_sys_devices, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) EXPORT_SYMBOL_GPL(get_cpu_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) static void device_create_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) __printf(4, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) static struct device *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) __cpu_device_create(struct device *parent, void *drvdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) const struct attribute_group **groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) const char *fmt, va_list args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) struct device *dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) int retval = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) dev = kzalloc(sizeof(*dev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) device_initialize(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) dev->parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) dev->groups = groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) dev->release = device_create_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) device_set_pm_not_required(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) dev_set_drvdata(dev, drvdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) retval = device_add(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) return ERR_PTR(retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) struct device *cpu_device_create(struct device *parent, void *drvdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) const struct attribute_group **groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) const char *fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) va_list vargs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) va_start(vargs, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) dev = __cpu_device_create(parent, drvdata, groups, fmt, vargs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) va_end(vargs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) EXPORT_SYMBOL_GPL(cpu_device_create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) #ifdef CONFIG_GENERIC_CPU_AUTOPROBE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) static DEVICE_ATTR(modalias, 0444, print_cpu_modalias, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) static struct attribute *cpu_root_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) &dev_attr_probe.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) &dev_attr_release.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) &cpu_attrs[0].attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) &cpu_attrs[1].attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) &cpu_attrs[2].attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) &dev_attr_kernel_max.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) &dev_attr_offline.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) &dev_attr_isolated.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) #ifdef CONFIG_NO_HZ_FULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) &dev_attr_nohz_full.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) #ifdef CONFIG_GENERIC_CPU_AUTOPROBE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) &dev_attr_modalias.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) static struct attribute_group cpu_root_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) .attrs = cpu_root_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) static const struct attribute_group *cpu_root_attr_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) &cpu_root_attr_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) bool cpu_is_hotpluggable(unsigned cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) struct device *dev = get_cpu_device(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) return dev && container_of(dev, struct cpu, dev)->hotpluggable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) EXPORT_SYMBOL_GPL(cpu_is_hotpluggable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) #ifdef CONFIG_GENERIC_CPU_DEVICES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) static DEFINE_PER_CPU(struct cpu, cpu_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) static void __init cpu_dev_register_generic(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) #ifdef CONFIG_GENERIC_CPU_DEVICES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) for_each_possible_cpu(i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) if (register_cpu(&per_cpu(cpu_devices, i), i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) panic("Failed to register CPU device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) #ifdef CONFIG_GENERIC_CPU_VULNERABILITIES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) ssize_t __weak cpu_show_meltdown(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) return sysfs_emit(buf, "Not affected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) ssize_t __weak cpu_show_spectre_v1(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) return sysfs_emit(buf, "Not affected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) ssize_t __weak cpu_show_spectre_v2(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) return sysfs_emit(buf, "Not affected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) ssize_t __weak cpu_show_spec_store_bypass(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) return sysfs_emit(buf, "Not affected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) ssize_t __weak cpu_show_l1tf(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) return sysfs_emit(buf, "Not affected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) ssize_t __weak cpu_show_mds(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) return sysfs_emit(buf, "Not affected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) ssize_t __weak cpu_show_tsx_async_abort(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) return sysfs_emit(buf, "Not affected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) ssize_t __weak cpu_show_itlb_multihit(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) return sysfs_emit(buf, "Not affected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) ssize_t __weak cpu_show_srbds(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) return sysfs_emit(buf, "Not affected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) static DEVICE_ATTR(meltdown, 0444, cpu_show_meltdown, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) static DEVICE_ATTR(spectre_v1, 0444, cpu_show_spectre_v1, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) static DEVICE_ATTR(spectre_v2, 0444, cpu_show_spectre_v2, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) static DEVICE_ATTR(spec_store_bypass, 0444, cpu_show_spec_store_bypass, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) static DEVICE_ATTR(l1tf, 0444, cpu_show_l1tf, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) static DEVICE_ATTR(mds, 0444, cpu_show_mds, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) static DEVICE_ATTR(tsx_async_abort, 0444, cpu_show_tsx_async_abort, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) static DEVICE_ATTR(itlb_multihit, 0444, cpu_show_itlb_multihit, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) static DEVICE_ATTR(srbds, 0444, cpu_show_srbds, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) static struct attribute *cpu_root_vulnerabilities_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) &dev_attr_meltdown.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) &dev_attr_spectre_v1.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) &dev_attr_spectre_v2.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) &dev_attr_spec_store_bypass.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) &dev_attr_l1tf.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) &dev_attr_mds.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) &dev_attr_tsx_async_abort.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) &dev_attr_itlb_multihit.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) &dev_attr_srbds.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) static const struct attribute_group cpu_root_vulnerabilities_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) .name = "vulnerabilities",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) .attrs = cpu_root_vulnerabilities_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) static void __init cpu_register_vulnerabilities(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) if (sysfs_create_group(&cpu_subsys.dev_root->kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) &cpu_root_vulnerabilities_group))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) pr_err("Unable to register CPU vulnerabilities\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) static inline void cpu_register_vulnerabilities(void) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) void __init cpu_dev_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) if (subsys_system_register(&cpu_subsys, cpu_root_attr_groups))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) panic("Failed to register CPU subsystem");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) cpu_dev_register_generic();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) cpu_register_vulnerabilities();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) }