1989d42e8SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds /* 38a25a2fdSKay Sievers * CPU subsystem support 41da177e4SLinus Torvalds */ 51da177e4SLinus Torvalds 6024f7846SBen Hutchings #include <linux/kernel.h> 71da177e4SLinus Torvalds #include <linux/module.h> 81da177e4SLinus Torvalds #include <linux/init.h> 9f6a57033SAl Viro #include <linux/sched.h> 101da177e4SLinus Torvalds #include <linux/cpu.h> 111da177e4SLinus Torvalds #include <linux/topology.h> 121da177e4SLinus Torvalds #include <linux/device.h> 1376b67ed9SKAMEZAWA Hiroyuki #include <linux/node.h> 145a0e3ad6STejun Heo #include <linux/gfp.h> 15fad12ac8SThomas Renninger #include <linux/slab.h> 169f13a1fdSBen Hutchings #include <linux/percpu.h> 17ac212b69SRafael J. Wysocki #include <linux/acpi.h> 18f86e4718SSudeep KarkadaNagesha #include <linux/of.h> 1967bad2fdSArd Biesheuvel #include <linux/cpufeature.h> 206570a9a1SRik van Riel #include <linux/tick.h> 2137efa4b4SAlex Shi #include <linux/pm_qos.h> 22edb93821SFrederic Weisbecker #include <linux/sched/isolation.h> 231da177e4SLinus Torvalds 24a1bdc7aaSBen Dooks #include "base.h" 251da177e4SLinus Torvalds 268a25a2fdSKay Sievers static DEFINE_PER_CPU(struct device *, cpu_sys_devices); 27ad74557aSAshok Raj 28ac212b69SRafael J. Wysocki static int cpu_subsys_match(struct device *dev, struct device_driver *drv) 29ac212b69SRafael J. Wysocki { 30ac212b69SRafael J. Wysocki /* ACPI style match is the only one that may succeed. */ 31ac212b69SRafael J. Wysocki if (acpi_driver_match_device(dev, drv)) 32ac212b69SRafael J. Wysocki return 1; 33ac212b69SRafael J. Wysocki 34ac212b69SRafael J. Wysocki return 0; 35ac212b69SRafael J. Wysocki } 36ac212b69SRafael J. Wysocki 371da177e4SLinus Torvalds #ifdef CONFIG_HOTPLUG_CPU 3834640468SYasuaki Ishimatsu static void change_cpu_under_node(struct cpu *cpu, 3934640468SYasuaki Ishimatsu unsigned int from_nid, unsigned int to_nid) 4034640468SYasuaki Ishimatsu { 4134640468SYasuaki Ishimatsu int cpuid = cpu->dev.id; 4234640468SYasuaki Ishimatsu unregister_cpu_under_node(cpuid, from_nid); 4334640468SYasuaki Ishimatsu register_cpu_under_node(cpuid, to_nid); 4434640468SYasuaki Ishimatsu cpu->node_id = to_nid; 4534640468SYasuaki Ishimatsu } 4634640468SYasuaki Ishimatsu 47eda5867bSMathias Krause static int cpu_subsys_online(struct device *dev) 481da177e4SLinus Torvalds { 498a25a2fdSKay Sievers struct cpu *cpu = container_of(dev, struct cpu, dev); 500902a904SRafael J. Wysocki int cpuid = dev->id; 5134640468SYasuaki Ishimatsu int from_nid, to_nid; 526dedcca6SToshi Kani int ret; 530902a904SRafael J. Wysocki 5434640468SYasuaki Ishimatsu from_nid = cpu_to_node(cpuid); 55c7991b0bSRafael J. Wysocki if (from_nid == NUMA_NO_NODE) 566dedcca6SToshi Kani return -ENODEV; 57c7991b0bSRafael J. Wysocki 5833c3736eSQais Yousef ret = cpu_device_up(dev); 5934640468SYasuaki Ishimatsu /* 6034640468SYasuaki Ishimatsu * When hot adding memory to memoryless node and enabling a cpu 6134640468SYasuaki Ishimatsu * on the node, node number of the cpu may internally change. 6234640468SYasuaki Ishimatsu */ 6334640468SYasuaki Ishimatsu to_nid = cpu_to_node(cpuid); 6434640468SYasuaki Ishimatsu if (from_nid != to_nid) 6534640468SYasuaki Ishimatsu change_cpu_under_node(cpu, from_nid, to_nid); 6634640468SYasuaki Ishimatsu 671da177e4SLinus Torvalds return ret; 681da177e4SLinus Torvalds } 691da177e4SLinus Torvalds 700902a904SRafael J. Wysocki static int cpu_subsys_offline(struct device *dev) 711da177e4SLinus Torvalds { 7233c3736eSQais Yousef return cpu_device_down(dev); 731da177e4SLinus Torvalds } 741c4e2d70SIgor Mammedov 7576b67ed9SKAMEZAWA Hiroyuki void unregister_cpu(struct cpu *cpu) 761da177e4SLinus Torvalds { 778a25a2fdSKay Sievers int logical_cpu = cpu->dev.id; 781da177e4SLinus Torvalds 7976b67ed9SKAMEZAWA Hiroyuki unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu)); 8076b67ed9SKAMEZAWA Hiroyuki 818a25a2fdSKay Sievers device_unregister(&cpu->dev); 82e37d05daSMike Travis per_cpu(cpu_sys_devices, logical_cpu) = NULL; 831da177e4SLinus Torvalds return; 841da177e4SLinus Torvalds } 8512633e80SNathan Fontenot 8612633e80SNathan Fontenot #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE 878a25a2fdSKay Sievers static ssize_t cpu_probe_store(struct device *dev, 888a25a2fdSKay Sievers struct device_attribute *attr, 8928812fe1SAndi Kleen const char *buf, 9012633e80SNathan Fontenot size_t count) 9112633e80SNathan Fontenot { 92574b851eSToshi Kani ssize_t cnt; 93574b851eSToshi Kani int ret; 94574b851eSToshi Kani 95574b851eSToshi Kani ret = lock_device_hotplug_sysfs(); 96574b851eSToshi Kani if (ret) 97574b851eSToshi Kani return ret; 98574b851eSToshi Kani 99574b851eSToshi Kani cnt = arch_cpu_probe(buf, count); 100574b851eSToshi Kani 101574b851eSToshi Kani unlock_device_hotplug(); 102574b851eSToshi Kani return cnt; 10312633e80SNathan Fontenot } 10412633e80SNathan Fontenot 1058a25a2fdSKay Sievers static ssize_t cpu_release_store(struct device *dev, 1068a25a2fdSKay Sievers struct device_attribute *attr, 10728812fe1SAndi Kleen const char *buf, 10812633e80SNathan Fontenot size_t count) 10912633e80SNathan Fontenot { 110574b851eSToshi Kani ssize_t cnt; 111574b851eSToshi Kani int ret; 112574b851eSToshi Kani 113574b851eSToshi Kani ret = lock_device_hotplug_sysfs(); 114574b851eSToshi Kani if (ret) 115574b851eSToshi Kani return ret; 116574b851eSToshi Kani 117574b851eSToshi Kani cnt = arch_cpu_release(buf, count); 118574b851eSToshi Kani 119574b851eSToshi Kani unlock_device_hotplug(); 120574b851eSToshi Kani return cnt; 12112633e80SNathan Fontenot } 12212633e80SNathan Fontenot 1238a25a2fdSKay Sievers static DEVICE_ATTR(probe, S_IWUSR, NULL, cpu_probe_store); 1248a25a2fdSKay Sievers static DEVICE_ATTR(release, S_IWUSR, NULL, cpu_release_store); 12512633e80SNathan Fontenot #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */ 1261da177e4SLinus Torvalds #endif /* CONFIG_HOTPLUG_CPU */ 1271da177e4SLinus Torvalds 1280902a904SRafael J. Wysocki struct bus_type cpu_subsys = { 1290902a904SRafael J. Wysocki .name = "cpu", 1300902a904SRafael J. Wysocki .dev_name = "cpu", 131ac212b69SRafael J. Wysocki .match = cpu_subsys_match, 1320902a904SRafael J. Wysocki #ifdef CONFIG_HOTPLUG_CPU 1330902a904SRafael J. Wysocki .online = cpu_subsys_online, 1340902a904SRafael J. Wysocki .offline = cpu_subsys_offline, 1350902a904SRafael J. Wysocki #endif 1360902a904SRafael J. Wysocki }; 1370902a904SRafael J. Wysocki EXPORT_SYMBOL_GPL(cpu_subsys); 1380902a904SRafael J. Wysocki 13951be5606SVivek Goyal #ifdef CONFIG_KEXEC 14051be5606SVivek Goyal #include <linux/kexec.h> 14151be5606SVivek Goyal 142948b3edbSJoe Perches static ssize_t crash_notes_show(struct device *dev, 143948b3edbSJoe Perches struct device_attribute *attr, 1444a0b2b4dSAndi Kleen char *buf) 14551be5606SVivek Goyal { 1468a25a2fdSKay Sievers struct cpu *cpu = container_of(dev, struct cpu, dev); 14751be5606SVivek Goyal unsigned long long addr; 14851be5606SVivek Goyal int cpunum; 14951be5606SVivek Goyal 1508a25a2fdSKay Sievers cpunum = cpu->dev.id; 15151be5606SVivek Goyal 15251be5606SVivek Goyal /* 15351be5606SVivek Goyal * Might be reading other cpu's data based on which cpu read thread 15451be5606SVivek Goyal * has been scheduled. But cpu data (memory) is allocated once during 15551be5606SVivek Goyal * boot up and this data does not change there after. Hence this 15651be5606SVivek Goyal * operation should be safe. No locking required. 15751be5606SVivek Goyal */ 1583b034b0dSVivek Goyal addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpunum)); 159eca4549fSZhang Yanfei 160948b3edbSJoe Perches return sysfs_emit(buf, "%llx\n", addr); 161948b3edbSJoe Perches } 162948b3edbSJoe Perches static DEVICE_ATTR_ADMIN_RO(crash_notes); 163948b3edbSJoe Perches 164948b3edbSJoe Perches static ssize_t crash_notes_size_show(struct device *dev, 165eca4549fSZhang Yanfei struct device_attribute *attr, 166eca4549fSZhang Yanfei char *buf) 167eca4549fSZhang Yanfei { 168948b3edbSJoe Perches return sysfs_emit(buf, "%zu\n", sizeof(note_buf_t)); 169eca4549fSZhang Yanfei } 170948b3edbSJoe Perches static DEVICE_ATTR_ADMIN_RO(crash_notes_size); 171c055da9fSIgor Mammedov 172c055da9fSIgor Mammedov static struct attribute *crash_note_cpu_attrs[] = { 173c055da9fSIgor Mammedov &dev_attr_crash_notes.attr, 174c055da9fSIgor Mammedov &dev_attr_crash_notes_size.attr, 175c055da9fSIgor Mammedov NULL 176c055da9fSIgor Mammedov }; 177c055da9fSIgor Mammedov 1785a576764SRikard Falkeborn static const struct attribute_group crash_note_cpu_attr_group = { 179c055da9fSIgor Mammedov .attrs = crash_note_cpu_attrs, 180c055da9fSIgor Mammedov }; 18151be5606SVivek Goyal #endif 18251be5606SVivek Goyal 183c055da9fSIgor Mammedov static const struct attribute_group *common_cpu_attr_groups[] = { 184c055da9fSIgor Mammedov #ifdef CONFIG_KEXEC 185c055da9fSIgor Mammedov &crash_note_cpu_attr_group, 186c055da9fSIgor Mammedov #endif 187c055da9fSIgor Mammedov NULL 188c055da9fSIgor Mammedov }; 189c055da9fSIgor Mammedov 1901c4e2d70SIgor Mammedov static const struct attribute_group *hotplugable_cpu_attr_groups[] = { 1911c4e2d70SIgor Mammedov #ifdef CONFIG_KEXEC 1921c4e2d70SIgor Mammedov &crash_note_cpu_attr_group, 1931c4e2d70SIgor Mammedov #endif 1941c4e2d70SIgor Mammedov NULL 1951c4e2d70SIgor Mammedov }; 1961c4e2d70SIgor Mammedov 1971da177e4SLinus Torvalds /* 1989d1fe323SMike Travis * Print cpu online, possible, present, and system maps 1999d1fe323SMike Travis */ 200265d2e2eSAndi Kleen 201265d2e2eSAndi Kleen struct cpu_attr { 2028a25a2fdSKay Sievers struct device_attribute attr; 203848e2391SRasmus Villemoes const struct cpumask *const map; 204265d2e2eSAndi Kleen }; 205265d2e2eSAndi Kleen 2068a25a2fdSKay Sievers static ssize_t show_cpus_attr(struct device *dev, 2078a25a2fdSKay Sievers struct device_attribute *attr, 208265d2e2eSAndi Kleen char *buf) 2099d1fe323SMike Travis { 210265d2e2eSAndi Kleen struct cpu_attr *ca = container_of(attr, struct cpu_attr, attr); 2119d1fe323SMike Travis 212848e2391SRasmus Villemoes return cpumap_print_to_pagebuf(true, buf, ca->map); 2139d1fe323SMike Travis } 2149d1fe323SMike Travis 215265d2e2eSAndi Kleen #define _CPU_ATTR(name, map) \ 2168a25a2fdSKay Sievers { __ATTR(name, 0444, show_cpus_attr, NULL), map } 2179d1fe323SMike Travis 2188a25a2fdSKay Sievers /* Keep in sync with cpu_subsys_attrs */ 219265d2e2eSAndi Kleen static struct cpu_attr cpu_attrs[] = { 220848e2391SRasmus Villemoes _CPU_ATTR(online, &__cpu_online_mask), 221848e2391SRasmus Villemoes _CPU_ATTR(possible, &__cpu_possible_mask), 222848e2391SRasmus Villemoes _CPU_ATTR(present, &__cpu_present_mask), 223265d2e2eSAndi Kleen }; 2249d1fe323SMike Travis 225e057d7aeSMike Travis /* 226e057d7aeSMike Travis * Print values for NR_CPUS and offlined cpus 227e057d7aeSMike Travis */ 2288a25a2fdSKay Sievers static ssize_t print_cpus_kernel_max(struct device *dev, 2298a25a2fdSKay Sievers struct device_attribute *attr, char *buf) 230e057d7aeSMike Travis { 231aa838896SJoe Perches return sysfs_emit(buf, "%d\n", NR_CPUS - 1); 232e057d7aeSMike Travis } 2338a25a2fdSKay Sievers static DEVICE_ATTR(kernel_max, 0444, print_cpus_kernel_max, NULL); 234e057d7aeSMike Travis 235e057d7aeSMike Travis /* arch-optional setting to enable display of offline cpus >= nr_cpu_ids */ 236e057d7aeSMike Travis unsigned int total_cpus; 237e057d7aeSMike Travis 2388a25a2fdSKay Sievers static ssize_t print_cpus_offline(struct device *dev, 2398a25a2fdSKay Sievers struct device_attribute *attr, char *buf) 240e057d7aeSMike Travis { 241948b3edbSJoe Perches int len = 0; 242e057d7aeSMike Travis cpumask_var_t offline; 243e057d7aeSMike Travis 244e057d7aeSMike Travis /* display offline cpus < nr_cpu_ids */ 245e057d7aeSMike Travis if (!alloc_cpumask_var(&offline, GFP_KERNEL)) 246e057d7aeSMike Travis return -ENOMEM; 247cdc6e3d3SJan Beulich cpumask_andnot(offline, cpu_possible_mask, cpu_online_mask); 248948b3edbSJoe Perches len += sysfs_emit_at(buf, len, "%*pbl", cpumask_pr_args(offline)); 249e057d7aeSMike Travis free_cpumask_var(offline); 250e057d7aeSMike Travis 251e057d7aeSMike Travis /* display offline cpus >= nr_cpu_ids */ 252e057d7aeSMike Travis if (total_cpus && nr_cpu_ids < total_cpus) { 253948b3edbSJoe Perches len += sysfs_emit_at(buf, len, ","); 254e057d7aeSMike Travis 255e057d7aeSMike Travis if (nr_cpu_ids == total_cpus-1) 256948b3edbSJoe Perches len += sysfs_emit_at(buf, len, "%u", nr_cpu_ids); 257e057d7aeSMike Travis else 258948b3edbSJoe Perches len += sysfs_emit_at(buf, len, "%u-%d", 259e057d7aeSMike Travis nr_cpu_ids, total_cpus - 1); 260e057d7aeSMike Travis } 261e057d7aeSMike Travis 262948b3edbSJoe Perches len += sysfs_emit_at(buf, len, "\n"); 263948b3edbSJoe Perches 264948b3edbSJoe Perches return len; 265e057d7aeSMike Travis } 2668a25a2fdSKay Sievers static DEVICE_ATTR(offline, 0444, print_cpus_offline, NULL); 267e057d7aeSMike Travis 26859f30abeSRik van Riel static ssize_t print_cpus_isolated(struct device *dev, 26959f30abeSRik van Riel struct device_attribute *attr, char *buf) 27059f30abeSRik van Riel { 271948b3edbSJoe Perches int len; 272edb93821SFrederic Weisbecker cpumask_var_t isolated; 27359f30abeSRik van Riel 274edb93821SFrederic Weisbecker if (!alloc_cpumask_var(&isolated, GFP_KERNEL)) 275edb93821SFrederic Weisbecker return -ENOMEM; 276edb93821SFrederic Weisbecker 277edb93821SFrederic Weisbecker cpumask_andnot(isolated, cpu_possible_mask, 278*04d4e665SFrederic Weisbecker housekeeping_cpumask(HK_TYPE_DOMAIN)); 279948b3edbSJoe Perches len = sysfs_emit(buf, "%*pbl\n", cpumask_pr_args(isolated)); 280edb93821SFrederic Weisbecker 281edb93821SFrederic Weisbecker free_cpumask_var(isolated); 28259f30abeSRik van Riel 283948b3edbSJoe Perches return len; 28459f30abeSRik van Riel } 28559f30abeSRik van Riel static DEVICE_ATTR(isolated, 0444, print_cpus_isolated, NULL); 28659f30abeSRik van Riel 2876570a9a1SRik van Riel #ifdef CONFIG_NO_HZ_FULL 2886570a9a1SRik van Riel static ssize_t print_cpus_nohz_full(struct device *dev, 2896570a9a1SRik van Riel struct device_attribute *attr, char *buf) 2906570a9a1SRik van Riel { 291aa838896SJoe Perches return sysfs_emit(buf, "%*pbl\n", cpumask_pr_args(tick_nohz_full_mask)); 2926570a9a1SRik van Riel } 2936570a9a1SRik van Riel static DEVICE_ATTR(nohz_full, 0444, print_cpus_nohz_full, NULL); 2946570a9a1SRik van Riel #endif 2956570a9a1SRik van Riel 2962885e25cSGreg Kroah-Hartman static void cpu_device_release(struct device *dev) 2972885e25cSGreg Kroah-Hartman { 2982885e25cSGreg Kroah-Hartman /* 2992885e25cSGreg Kroah-Hartman * This is an empty function to prevent the driver core from spitting a 3002885e25cSGreg Kroah-Hartman * warning at us. Yes, I know this is directly opposite of what the 3012885e25cSGreg Kroah-Hartman * documentation for the driver core and kobjects say, and the author 3022885e25cSGreg Kroah-Hartman * of this code has already been publically ridiculed for doing 3032885e25cSGreg Kroah-Hartman * something as foolish as this. However, at this point in time, it is 3042885e25cSGreg Kroah-Hartman * the only way to handle the issue of statically allocated cpu 3052885e25cSGreg Kroah-Hartman * devices. The different architectures will have their cpu device 3062885e25cSGreg Kroah-Hartman * code reworked to properly handle this in the near future, so this 3072885e25cSGreg Kroah-Hartman * function will then be changed to correctly free up the memory held 3082885e25cSGreg Kroah-Hartman * by the cpu device. 3092885e25cSGreg Kroah-Hartman * 3102885e25cSGreg Kroah-Hartman * Never copy this way of doing things, or you too will be made fun of 31130a4840aSRalf Baechle * on the linux-kernel list, you have been warned. 3122885e25cSGreg Kroah-Hartman */ 3132885e25cSGreg Kroah-Hartman } 3142885e25cSGreg Kroah-Hartman 31567bad2fdSArd Biesheuvel #ifdef CONFIG_GENERIC_CPU_AUTOPROBE 31667bad2fdSArd Biesheuvel static ssize_t print_cpu_modalias(struct device *dev, 31767bad2fdSArd Biesheuvel struct device_attribute *attr, 31867bad2fdSArd Biesheuvel char *buf) 31967bad2fdSArd Biesheuvel { 320948b3edbSJoe Perches int len = 0; 32167bad2fdSArd Biesheuvel u32 i; 32267bad2fdSArd Biesheuvel 323948b3edbSJoe Perches len += sysfs_emit_at(buf, len, 324948b3edbSJoe Perches "cpu:type:" CPU_FEATURE_TYPEFMT ":feature:", 32567bad2fdSArd Biesheuvel CPU_FEATURE_TYPEVAL); 32667bad2fdSArd Biesheuvel 32767bad2fdSArd Biesheuvel for (i = 0; i < MAX_CPU_FEATURES; i++) 32867bad2fdSArd Biesheuvel if (cpu_have_feature(i)) { 329948b3edbSJoe Perches if (len + sizeof(",XXXX\n") >= PAGE_SIZE) { 33067bad2fdSArd Biesheuvel WARN(1, "CPU features overflow page\n"); 33167bad2fdSArd Biesheuvel break; 33267bad2fdSArd Biesheuvel } 333948b3edbSJoe Perches len += sysfs_emit_at(buf, len, ",%04X", i); 33467bad2fdSArd Biesheuvel } 335948b3edbSJoe Perches len += sysfs_emit_at(buf, len, "\n"); 336948b3edbSJoe Perches return len; 33767bad2fdSArd Biesheuvel } 33867bad2fdSArd Biesheuvel 33967bad2fdSArd Biesheuvel static int cpu_uevent(struct device *dev, struct kobj_uevent_env *env) 34067bad2fdSArd Biesheuvel { 34167bad2fdSArd Biesheuvel char *buf = kzalloc(PAGE_SIZE, GFP_KERNEL); 34267bad2fdSArd Biesheuvel if (buf) { 34367bad2fdSArd Biesheuvel print_cpu_modalias(NULL, NULL, buf); 34467bad2fdSArd Biesheuvel add_uevent_var(env, "MODALIAS=%s", buf); 34567bad2fdSArd Biesheuvel kfree(buf); 34667bad2fdSArd Biesheuvel } 34767bad2fdSArd Biesheuvel return 0; 34867bad2fdSArd Biesheuvel } 34967bad2fdSArd Biesheuvel #endif 35067bad2fdSArd Biesheuvel 3519d1fe323SMike Travis /* 352405ae7d3SRobert P. J. Day * register_cpu - Setup a sysfs device for a CPU. 35372486f1fSSiddha, Suresh B * @cpu - cpu->hotpluggable field set to 1 will generate a control file in 35472486f1fSSiddha, Suresh B * sysfs for this CPU. 3551da177e4SLinus Torvalds * @num - CPU number to use when creating the device. 3561da177e4SLinus Torvalds * 3571da177e4SLinus Torvalds * Initialize and register the CPU device. 3581da177e4SLinus Torvalds */ 359a83048ebSPaul Gortmaker int register_cpu(struct cpu *cpu, int num) 3601da177e4SLinus Torvalds { 3611da177e4SLinus Torvalds int error; 3628a25a2fdSKay Sievers 3631da177e4SLinus Torvalds cpu->node_id = cpu_to_node(num); 36429bb5d4fSGreg Kroah-Hartman memset(&cpu->dev, 0x00, sizeof(struct device)); 3658a25a2fdSKay Sievers cpu->dev.id = num; 3668a25a2fdSKay Sievers cpu->dev.bus = &cpu_subsys; 3672885e25cSGreg Kroah-Hartman cpu->dev.release = cpu_device_release; 3680902a904SRafael J. Wysocki cpu->dev.offline_disabled = !cpu->hotpluggable; 3691001b4d4SToshi Kani cpu->dev.offline = !cpu_online(num); 370f86e4718SSudeep KarkadaNagesha cpu->dev.of_node = of_get_cpu_node(num, NULL); 3712b9c1f03SArd Biesheuvel #ifdef CONFIG_GENERIC_CPU_AUTOPROBE 37267bad2fdSArd Biesheuvel cpu->dev.bus->uevent = cpu_uevent; 373fad12ac8SThomas Renninger #endif 374c055da9fSIgor Mammedov cpu->dev.groups = common_cpu_attr_groups; 3751c4e2d70SIgor Mammedov if (cpu->hotpluggable) 3761c4e2d70SIgor Mammedov cpu->dev.groups = hotplugable_cpu_attr_groups; 3778a25a2fdSKay Sievers error = device_register(&cpu->dev); 3783aaba245SArvind Yadav if (error) { 3793aaba245SArvind Yadav put_device(&cpu->dev); 38059fffa34SAlex Shi return error; 3813aaba245SArvind Yadav } 38259fffa34SAlex Shi 3838a25a2fdSKay Sievers per_cpu(cpu_sys_devices, num) = &cpu->dev; 38476b67ed9SKAMEZAWA Hiroyuki register_cpu_under_node(num, cpu_to_node(num)); 3850759e80bSRafael J. Wysocki dev_pm_qos_expose_latency_limit(&cpu->dev, 3860759e80bSRafael J. Wysocki PM_QOS_RESUME_LATENCY_NO_CONSTRAINT); 38751be5606SVivek Goyal 38859fffa34SAlex Shi return 0; 3891da177e4SLinus Torvalds } 3901da177e4SLinus Torvalds 391e7deeb9dSJinchao Wang struct device *get_cpu_device(unsigned int cpu) 392ad74557aSAshok Raj { 393e37d05daSMike Travis if (cpu < nr_cpu_ids && cpu_possible(cpu)) 394e37d05daSMike Travis return per_cpu(cpu_sys_devices, cpu); 395ad74557aSAshok Raj else 396ad74557aSAshok Raj return NULL; 397ad74557aSAshok Raj } 3988a25a2fdSKay Sievers EXPORT_SYMBOL_GPL(get_cpu_device); 3998a25a2fdSKay Sievers 4003d52943bSSudeep Holla static void device_create_release(struct device *dev) 4013d52943bSSudeep Holla { 4023d52943bSSudeep Holla kfree(dev); 4033d52943bSSudeep Holla } 4043d52943bSSudeep Holla 405fa548d79SMathieu Malaterre __printf(4, 0) 4063d52943bSSudeep Holla static struct device * 4073d52943bSSudeep Holla __cpu_device_create(struct device *parent, void *drvdata, 4083d52943bSSudeep Holla const struct attribute_group **groups, 4093d52943bSSudeep Holla const char *fmt, va_list args) 4103d52943bSSudeep Holla { 4113d52943bSSudeep Holla struct device *dev = NULL; 4126b72cf12SColin Ian King int retval = -ENOMEM; 4133d52943bSSudeep Holla 4143d52943bSSudeep Holla dev = kzalloc(sizeof(*dev), GFP_KERNEL); 4156b72cf12SColin Ian King if (!dev) 4163d52943bSSudeep Holla goto error; 4173d52943bSSudeep Holla 4183d52943bSSudeep Holla device_initialize(dev); 4193d52943bSSudeep Holla dev->parent = parent; 4203d52943bSSudeep Holla dev->groups = groups; 4213d52943bSSudeep Holla dev->release = device_create_release; 42285945c28SSudeep Holla device_set_pm_not_required(dev); 4233d52943bSSudeep Holla dev_set_drvdata(dev, drvdata); 4243d52943bSSudeep Holla 4253d52943bSSudeep Holla retval = kobject_set_name_vargs(&dev->kobj, fmt, args); 4263d52943bSSudeep Holla if (retval) 4273d52943bSSudeep Holla goto error; 4283d52943bSSudeep Holla 4293d52943bSSudeep Holla retval = device_add(dev); 4303d52943bSSudeep Holla if (retval) 4313d52943bSSudeep Holla goto error; 4323d52943bSSudeep Holla 4333d52943bSSudeep Holla return dev; 4343d52943bSSudeep Holla 4353d52943bSSudeep Holla error: 4363d52943bSSudeep Holla put_device(dev); 4373d52943bSSudeep Holla return ERR_PTR(retval); 4383d52943bSSudeep Holla } 4393d52943bSSudeep Holla 4403d52943bSSudeep Holla struct device *cpu_device_create(struct device *parent, void *drvdata, 4413d52943bSSudeep Holla const struct attribute_group **groups, 4423d52943bSSudeep Holla const char *fmt, ...) 4433d52943bSSudeep Holla { 4443d52943bSSudeep Holla va_list vargs; 4453d52943bSSudeep Holla struct device *dev; 4463d52943bSSudeep Holla 4473d52943bSSudeep Holla va_start(vargs, fmt); 4483d52943bSSudeep Holla dev = __cpu_device_create(parent, drvdata, groups, fmt, vargs); 4493d52943bSSudeep Holla va_end(vargs); 4503d52943bSSudeep Holla return dev; 4513d52943bSSudeep Holla } 4523d52943bSSudeep Holla EXPORT_SYMBOL_GPL(cpu_device_create); 4533d52943bSSudeep Holla 4542b9c1f03SArd Biesheuvel #ifdef CONFIG_GENERIC_CPU_AUTOPROBE 45567bad2fdSArd Biesheuvel static DEVICE_ATTR(modalias, 0444, print_cpu_modalias, NULL); 456fad12ac8SThomas Renninger #endif 457fad12ac8SThomas Renninger 4588a25a2fdSKay Sievers static struct attribute *cpu_root_attrs[] = { 4598a25a2fdSKay Sievers #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE 4608a25a2fdSKay Sievers &dev_attr_probe.attr, 4618a25a2fdSKay Sievers &dev_attr_release.attr, 4628a25a2fdSKay Sievers #endif 4638a25a2fdSKay Sievers &cpu_attrs[0].attr.attr, 4648a25a2fdSKay Sievers &cpu_attrs[1].attr.attr, 4658a25a2fdSKay Sievers &cpu_attrs[2].attr.attr, 4668a25a2fdSKay Sievers &dev_attr_kernel_max.attr, 4678a25a2fdSKay Sievers &dev_attr_offline.attr, 46859f30abeSRik van Riel &dev_attr_isolated.attr, 4696570a9a1SRik van Riel #ifdef CONFIG_NO_HZ_FULL 4706570a9a1SRik van Riel &dev_attr_nohz_full.attr, 4716570a9a1SRik van Riel #endif 4722b9c1f03SArd Biesheuvel #ifdef CONFIG_GENERIC_CPU_AUTOPROBE 473fad12ac8SThomas Renninger &dev_attr_modalias.attr, 474fad12ac8SThomas Renninger #endif 4758a25a2fdSKay Sievers NULL 4768a25a2fdSKay Sievers }; 4778a25a2fdSKay Sievers 4785a576764SRikard Falkeborn static const struct attribute_group cpu_root_attr_group = { 4798a25a2fdSKay Sievers .attrs = cpu_root_attrs, 4808a25a2fdSKay Sievers }; 4818a25a2fdSKay Sievers 4828a25a2fdSKay Sievers static const struct attribute_group *cpu_root_attr_groups[] = { 4838a25a2fdSKay Sievers &cpu_root_attr_group, 4848a25a2fdSKay Sievers NULL, 4858a25a2fdSKay Sievers }; 4861da177e4SLinus Torvalds 487e7deeb9dSJinchao Wang bool cpu_is_hotpluggable(unsigned int cpu) 4882987557fSJosh Triplett { 4897affca35SLinus Torvalds struct device *dev = get_cpu_device(cpu); 4907affca35SLinus Torvalds return dev && container_of(dev, struct cpu, dev)->hotpluggable; 4912987557fSJosh Triplett } 4922987557fSJosh Triplett EXPORT_SYMBOL_GPL(cpu_is_hotpluggable); 4932987557fSJosh Triplett 4949f13a1fdSBen Hutchings #ifdef CONFIG_GENERIC_CPU_DEVICES 4959f13a1fdSBen Hutchings static DEFINE_PER_CPU(struct cpu, cpu_devices); 4969f13a1fdSBen Hutchings #endif 4979f13a1fdSBen Hutchings 4989f13a1fdSBen Hutchings static void __init cpu_dev_register_generic(void) 4999f13a1fdSBen Hutchings { 5009f13a1fdSBen Hutchings #ifdef CONFIG_GENERIC_CPU_DEVICES 5019f13a1fdSBen Hutchings int i; 5029f13a1fdSBen Hutchings 5039f13a1fdSBen Hutchings for_each_possible_cpu(i) { 5049f13a1fdSBen Hutchings if (register_cpu(&per_cpu(cpu_devices, i), i)) 5059f13a1fdSBen Hutchings panic("Failed to register CPU device"); 5069f13a1fdSBen Hutchings } 5079f13a1fdSBen Hutchings #endif 5089f13a1fdSBen Hutchings } 5099f13a1fdSBen Hutchings 51087590ce6SThomas Gleixner #ifdef CONFIG_GENERIC_CPU_VULNERABILITIES 51187590ce6SThomas Gleixner 51287590ce6SThomas Gleixner ssize_t __weak cpu_show_meltdown(struct device *dev, 51387590ce6SThomas Gleixner struct device_attribute *attr, char *buf) 51487590ce6SThomas Gleixner { 515aa838896SJoe Perches return sysfs_emit(buf, "Not affected\n"); 51687590ce6SThomas Gleixner } 51787590ce6SThomas Gleixner 51887590ce6SThomas Gleixner ssize_t __weak cpu_show_spectre_v1(struct device *dev, 51987590ce6SThomas Gleixner struct device_attribute *attr, char *buf) 52087590ce6SThomas Gleixner { 521aa838896SJoe Perches return sysfs_emit(buf, "Not affected\n"); 52287590ce6SThomas Gleixner } 52387590ce6SThomas Gleixner 52487590ce6SThomas Gleixner ssize_t __weak cpu_show_spectre_v2(struct device *dev, 52587590ce6SThomas Gleixner struct device_attribute *attr, char *buf) 52687590ce6SThomas Gleixner { 527aa838896SJoe Perches return sysfs_emit(buf, "Not affected\n"); 52887590ce6SThomas Gleixner } 52987590ce6SThomas Gleixner 530c456442cSKonrad Rzeszutek Wilk ssize_t __weak cpu_show_spec_store_bypass(struct device *dev, 531c456442cSKonrad Rzeszutek Wilk struct device_attribute *attr, char *buf) 532c456442cSKonrad Rzeszutek Wilk { 533aa838896SJoe Perches return sysfs_emit(buf, "Not affected\n"); 534c456442cSKonrad Rzeszutek Wilk } 535c456442cSKonrad Rzeszutek Wilk 53617dbca11SAndi Kleen ssize_t __weak cpu_show_l1tf(struct device *dev, 53717dbca11SAndi Kleen struct device_attribute *attr, char *buf) 53817dbca11SAndi Kleen { 539aa838896SJoe Perches return sysfs_emit(buf, "Not affected\n"); 54017dbca11SAndi Kleen } 54117dbca11SAndi Kleen 5428a4b06d3SThomas Gleixner ssize_t __weak cpu_show_mds(struct device *dev, 5438a4b06d3SThomas Gleixner struct device_attribute *attr, char *buf) 5448a4b06d3SThomas Gleixner { 545aa838896SJoe Perches return sysfs_emit(buf, "Not affected\n"); 5468a4b06d3SThomas Gleixner } 5478a4b06d3SThomas Gleixner 5486608b45aSPawan Gupta ssize_t __weak cpu_show_tsx_async_abort(struct device *dev, 5496608b45aSPawan Gupta struct device_attribute *attr, 5506608b45aSPawan Gupta char *buf) 5516608b45aSPawan Gupta { 552aa838896SJoe Perches return sysfs_emit(buf, "Not affected\n"); 5536608b45aSPawan Gupta } 5546608b45aSPawan Gupta 555db4d30fbSVineela Tummalapalli ssize_t __weak cpu_show_itlb_multihit(struct device *dev, 556db4d30fbSVineela Tummalapalli struct device_attribute *attr, char *buf) 557db4d30fbSVineela Tummalapalli { 558aa838896SJoe Perches return sysfs_emit(buf, "Not affected\n"); 559db4d30fbSVineela Tummalapalli } 560db4d30fbSVineela Tummalapalli 5617e5b3c26SMark Gross ssize_t __weak cpu_show_srbds(struct device *dev, 5627e5b3c26SMark Gross struct device_attribute *attr, char *buf) 5637e5b3c26SMark Gross { 564aa838896SJoe Perches return sysfs_emit(buf, "Not affected\n"); 5657e5b3c26SMark Gross } 5667e5b3c26SMark Gross 56787590ce6SThomas Gleixner static DEVICE_ATTR(meltdown, 0444, cpu_show_meltdown, NULL); 56887590ce6SThomas Gleixner static DEVICE_ATTR(spectre_v1, 0444, cpu_show_spectre_v1, NULL); 56987590ce6SThomas Gleixner static DEVICE_ATTR(spectre_v2, 0444, cpu_show_spectre_v2, NULL); 570c456442cSKonrad Rzeszutek Wilk static DEVICE_ATTR(spec_store_bypass, 0444, cpu_show_spec_store_bypass, NULL); 57117dbca11SAndi Kleen static DEVICE_ATTR(l1tf, 0444, cpu_show_l1tf, NULL); 5728a4b06d3SThomas Gleixner static DEVICE_ATTR(mds, 0444, cpu_show_mds, NULL); 5736608b45aSPawan Gupta static DEVICE_ATTR(tsx_async_abort, 0444, cpu_show_tsx_async_abort, NULL); 574db4d30fbSVineela Tummalapalli static DEVICE_ATTR(itlb_multihit, 0444, cpu_show_itlb_multihit, NULL); 5757e5b3c26SMark Gross static DEVICE_ATTR(srbds, 0444, cpu_show_srbds, NULL); 57687590ce6SThomas Gleixner 57787590ce6SThomas Gleixner static struct attribute *cpu_root_vulnerabilities_attrs[] = { 57887590ce6SThomas Gleixner &dev_attr_meltdown.attr, 57987590ce6SThomas Gleixner &dev_attr_spectre_v1.attr, 58087590ce6SThomas Gleixner &dev_attr_spectre_v2.attr, 581c456442cSKonrad Rzeszutek Wilk &dev_attr_spec_store_bypass.attr, 58217dbca11SAndi Kleen &dev_attr_l1tf.attr, 5838a4b06d3SThomas Gleixner &dev_attr_mds.attr, 5846608b45aSPawan Gupta &dev_attr_tsx_async_abort.attr, 585db4d30fbSVineela Tummalapalli &dev_attr_itlb_multihit.attr, 5867e5b3c26SMark Gross &dev_attr_srbds.attr, 58787590ce6SThomas Gleixner NULL 58887590ce6SThomas Gleixner }; 58987590ce6SThomas Gleixner 59087590ce6SThomas Gleixner static const struct attribute_group cpu_root_vulnerabilities_group = { 59187590ce6SThomas Gleixner .name = "vulnerabilities", 59287590ce6SThomas Gleixner .attrs = cpu_root_vulnerabilities_attrs, 59387590ce6SThomas Gleixner }; 59487590ce6SThomas Gleixner 59587590ce6SThomas Gleixner static void __init cpu_register_vulnerabilities(void) 59687590ce6SThomas Gleixner { 59787590ce6SThomas Gleixner if (sysfs_create_group(&cpu_subsys.dev_root->kobj, 59887590ce6SThomas Gleixner &cpu_root_vulnerabilities_group)) 59987590ce6SThomas Gleixner pr_err("Unable to register CPU vulnerabilities\n"); 60087590ce6SThomas Gleixner } 60187590ce6SThomas Gleixner 60287590ce6SThomas Gleixner #else 60387590ce6SThomas Gleixner static inline void cpu_register_vulnerabilities(void) { } 60487590ce6SThomas Gleixner #endif 60587590ce6SThomas Gleixner 606024f7846SBen Hutchings void __init cpu_dev_init(void) 6071da177e4SLinus Torvalds { 608024f7846SBen Hutchings if (subsys_system_register(&cpu_subsys, cpu_root_attr_groups)) 609024f7846SBen Hutchings panic("Failed to register CPU subsystem"); 6105c45bf27SSiddha, Suresh B 6119f13a1fdSBen Hutchings cpu_dev_register_generic(); 61287590ce6SThomas Gleixner cpu_register_vulnerabilities(); 6131da177e4SLinus Torvalds } 614