11da177e4SLinus Torvalds /* 28a25a2fdSKay Sievers * CPU subsystem support 31da177e4SLinus Torvalds */ 41da177e4SLinus Torvalds 5024f7846SBen Hutchings #include <linux/kernel.h> 61da177e4SLinus Torvalds #include <linux/module.h> 71da177e4SLinus Torvalds #include <linux/init.h> 8f6a57033SAl Viro #include <linux/sched.h> 91da177e4SLinus Torvalds #include <linux/cpu.h> 101da177e4SLinus Torvalds #include <linux/topology.h> 111da177e4SLinus Torvalds #include <linux/device.h> 1276b67ed9SKAMEZAWA Hiroyuki #include <linux/node.h> 135a0e3ad6STejun Heo #include <linux/gfp.h> 14fad12ac8SThomas Renninger #include <linux/slab.h> 159f13a1fdSBen Hutchings #include <linux/percpu.h> 16ac212b69SRafael J. Wysocki #include <linux/acpi.h> 17f86e4718SSudeep KarkadaNagesha #include <linux/of.h> 1867bad2fdSArd Biesheuvel #include <linux/cpufeature.h> 196570a9a1SRik van Riel #include <linux/tick.h> 201da177e4SLinus Torvalds 21a1bdc7aaSBen Dooks #include "base.h" 221da177e4SLinus Torvalds 238a25a2fdSKay Sievers static DEFINE_PER_CPU(struct device *, cpu_sys_devices); 24ad74557aSAshok Raj 25ac212b69SRafael J. Wysocki static int cpu_subsys_match(struct device *dev, struct device_driver *drv) 26ac212b69SRafael J. Wysocki { 27ac212b69SRafael J. Wysocki /* ACPI style match is the only one that may succeed. */ 28ac212b69SRafael J. Wysocki if (acpi_driver_match_device(dev, drv)) 29ac212b69SRafael J. Wysocki return 1; 30ac212b69SRafael J. Wysocki 31ac212b69SRafael J. Wysocki return 0; 32ac212b69SRafael J. Wysocki } 33ac212b69SRafael J. Wysocki 341da177e4SLinus Torvalds #ifdef CONFIG_HOTPLUG_CPU 3534640468SYasuaki Ishimatsu static void change_cpu_under_node(struct cpu *cpu, 3634640468SYasuaki Ishimatsu unsigned int from_nid, unsigned int to_nid) 3734640468SYasuaki Ishimatsu { 3834640468SYasuaki Ishimatsu int cpuid = cpu->dev.id; 3934640468SYasuaki Ishimatsu unregister_cpu_under_node(cpuid, from_nid); 4034640468SYasuaki Ishimatsu register_cpu_under_node(cpuid, to_nid); 4134640468SYasuaki Ishimatsu cpu->node_id = to_nid; 4234640468SYasuaki Ishimatsu } 4334640468SYasuaki Ishimatsu 440902a904SRafael J. Wysocki static int __ref cpu_subsys_online(struct device *dev) 451da177e4SLinus Torvalds { 468a25a2fdSKay Sievers struct cpu *cpu = container_of(dev, struct cpu, dev); 470902a904SRafael J. Wysocki int cpuid = dev->id; 4834640468SYasuaki Ishimatsu int from_nid, to_nid; 496dedcca6SToshi Kani int ret; 500902a904SRafael J. Wysocki 5134640468SYasuaki Ishimatsu from_nid = cpu_to_node(cpuid); 52c7991b0bSRafael J. Wysocki if (from_nid == NUMA_NO_NODE) 536dedcca6SToshi Kani return -ENODEV; 54c7991b0bSRafael J. Wysocki 5534640468SYasuaki Ishimatsu ret = cpu_up(cpuid); 5634640468SYasuaki Ishimatsu /* 5734640468SYasuaki Ishimatsu * When hot adding memory to memoryless node and enabling a cpu 5834640468SYasuaki Ishimatsu * on the node, node number of the cpu may internally change. 5934640468SYasuaki Ishimatsu */ 6034640468SYasuaki Ishimatsu to_nid = cpu_to_node(cpuid); 6134640468SYasuaki Ishimatsu if (from_nid != to_nid) 6234640468SYasuaki Ishimatsu change_cpu_under_node(cpu, from_nid, to_nid); 6334640468SYasuaki Ishimatsu 641da177e4SLinus Torvalds return ret; 651da177e4SLinus Torvalds } 661da177e4SLinus Torvalds 670902a904SRafael J. Wysocki static int cpu_subsys_offline(struct device *dev) 681da177e4SLinus Torvalds { 696dedcca6SToshi Kani return cpu_down(dev->id); 701da177e4SLinus Torvalds } 711c4e2d70SIgor Mammedov 7276b67ed9SKAMEZAWA Hiroyuki void unregister_cpu(struct cpu *cpu) 731da177e4SLinus Torvalds { 748a25a2fdSKay Sievers int logical_cpu = cpu->dev.id; 751da177e4SLinus Torvalds 7676b67ed9SKAMEZAWA Hiroyuki unregister_cpu_under_node(logical_cpu, cpu_to_node(logical_cpu)); 7776b67ed9SKAMEZAWA Hiroyuki 788a25a2fdSKay Sievers device_unregister(&cpu->dev); 79e37d05daSMike Travis per_cpu(cpu_sys_devices, logical_cpu) = NULL; 801da177e4SLinus Torvalds return; 811da177e4SLinus Torvalds } 8212633e80SNathan Fontenot 8312633e80SNathan Fontenot #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE 848a25a2fdSKay Sievers static ssize_t cpu_probe_store(struct device *dev, 858a25a2fdSKay Sievers struct device_attribute *attr, 8628812fe1SAndi Kleen const char *buf, 8712633e80SNathan Fontenot size_t count) 8812633e80SNathan Fontenot { 89574b851eSToshi Kani ssize_t cnt; 90574b851eSToshi Kani int ret; 91574b851eSToshi Kani 92574b851eSToshi Kani ret = lock_device_hotplug_sysfs(); 93574b851eSToshi Kani if (ret) 94574b851eSToshi Kani return ret; 95574b851eSToshi Kani 96574b851eSToshi Kani cnt = arch_cpu_probe(buf, count); 97574b851eSToshi Kani 98574b851eSToshi Kani unlock_device_hotplug(); 99574b851eSToshi Kani return cnt; 10012633e80SNathan Fontenot } 10112633e80SNathan Fontenot 1028a25a2fdSKay Sievers static ssize_t cpu_release_store(struct device *dev, 1038a25a2fdSKay Sievers struct device_attribute *attr, 10428812fe1SAndi Kleen const char *buf, 10512633e80SNathan Fontenot size_t count) 10612633e80SNathan Fontenot { 107574b851eSToshi Kani ssize_t cnt; 108574b851eSToshi Kani int ret; 109574b851eSToshi Kani 110574b851eSToshi Kani ret = lock_device_hotplug_sysfs(); 111574b851eSToshi Kani if (ret) 112574b851eSToshi Kani return ret; 113574b851eSToshi Kani 114574b851eSToshi Kani cnt = arch_cpu_release(buf, count); 115574b851eSToshi Kani 116574b851eSToshi Kani unlock_device_hotplug(); 117574b851eSToshi Kani return cnt; 11812633e80SNathan Fontenot } 11912633e80SNathan Fontenot 1208a25a2fdSKay Sievers static DEVICE_ATTR(probe, S_IWUSR, NULL, cpu_probe_store); 1218a25a2fdSKay Sievers static DEVICE_ATTR(release, S_IWUSR, NULL, cpu_release_store); 12212633e80SNathan Fontenot #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */ 1231da177e4SLinus Torvalds #endif /* CONFIG_HOTPLUG_CPU */ 1241da177e4SLinus Torvalds 1250902a904SRafael J. Wysocki struct bus_type cpu_subsys = { 1260902a904SRafael J. Wysocki .name = "cpu", 1270902a904SRafael J. Wysocki .dev_name = "cpu", 128ac212b69SRafael J. Wysocki .match = cpu_subsys_match, 1290902a904SRafael J. Wysocki #ifdef CONFIG_HOTPLUG_CPU 1300902a904SRafael J. Wysocki .online = cpu_subsys_online, 1310902a904SRafael J. Wysocki .offline = cpu_subsys_offline, 1320902a904SRafael J. Wysocki #endif 1330902a904SRafael J. Wysocki }; 1340902a904SRafael J. Wysocki EXPORT_SYMBOL_GPL(cpu_subsys); 1350902a904SRafael J. Wysocki 13651be5606SVivek Goyal #ifdef CONFIG_KEXEC 13751be5606SVivek Goyal #include <linux/kexec.h> 13851be5606SVivek Goyal 1398a25a2fdSKay Sievers static ssize_t show_crash_notes(struct device *dev, struct device_attribute *attr, 1404a0b2b4dSAndi Kleen char *buf) 14151be5606SVivek Goyal { 1428a25a2fdSKay Sievers struct cpu *cpu = container_of(dev, struct cpu, dev); 14351be5606SVivek Goyal ssize_t rc; 14451be5606SVivek Goyal unsigned long long addr; 14551be5606SVivek Goyal int cpunum; 14651be5606SVivek Goyal 1478a25a2fdSKay Sievers cpunum = cpu->dev.id; 14851be5606SVivek Goyal 14951be5606SVivek Goyal /* 15051be5606SVivek Goyal * Might be reading other cpu's data based on which cpu read thread 15151be5606SVivek Goyal * has been scheduled. But cpu data (memory) is allocated once during 15251be5606SVivek Goyal * boot up and this data does not change there after. Hence this 15351be5606SVivek Goyal * operation should be safe. No locking required. 15451be5606SVivek Goyal */ 1553b034b0dSVivek Goyal addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpunum)); 15651be5606SVivek Goyal rc = sprintf(buf, "%Lx\n", addr); 15751be5606SVivek Goyal return rc; 15851be5606SVivek Goyal } 1598a25a2fdSKay Sievers static DEVICE_ATTR(crash_notes, 0400, show_crash_notes, NULL); 160eca4549fSZhang Yanfei 161eca4549fSZhang Yanfei static ssize_t show_crash_notes_size(struct device *dev, 162eca4549fSZhang Yanfei struct device_attribute *attr, 163eca4549fSZhang Yanfei char *buf) 164eca4549fSZhang Yanfei { 165eca4549fSZhang Yanfei ssize_t rc; 166eca4549fSZhang Yanfei 167bcfb87fbSArnd Bergmann rc = sprintf(buf, "%zu\n", sizeof(note_buf_t)); 168eca4549fSZhang Yanfei return rc; 169eca4549fSZhang Yanfei } 170eca4549fSZhang Yanfei static DEVICE_ATTR(crash_notes_size, 0400, show_crash_notes_size, NULL); 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 178c055da9fSIgor Mammedov static 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; 203265d2e2eSAndi Kleen const struct cpumask *const * 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 2125aaba363SSudeep Holla 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[] = { 220265d2e2eSAndi Kleen _CPU_ATTR(online, &cpu_online_mask), 221265d2e2eSAndi Kleen _CPU_ATTR(possible, &cpu_possible_mask), 222265d2e2eSAndi Kleen _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 { 2318fd2d2d5SMike Travis int n = snprintf(buf, PAGE_SIZE-2, "%d\n", NR_CPUS - 1); 232e057d7aeSMike Travis return n; 233e057d7aeSMike Travis } 2348a25a2fdSKay Sievers static DEVICE_ATTR(kernel_max, 0444, print_cpus_kernel_max, NULL); 235e057d7aeSMike Travis 236e057d7aeSMike Travis /* arch-optional setting to enable display of offline cpus >= nr_cpu_ids */ 237e057d7aeSMike Travis unsigned int total_cpus; 238e057d7aeSMike Travis 2398a25a2fdSKay Sievers static ssize_t print_cpus_offline(struct device *dev, 2408a25a2fdSKay Sievers struct device_attribute *attr, char *buf) 241e057d7aeSMike Travis { 242e057d7aeSMike Travis int n = 0, len = PAGE_SIZE-2; 243e057d7aeSMike Travis cpumask_var_t offline; 244e057d7aeSMike Travis 245e057d7aeSMike Travis /* display offline cpus < nr_cpu_ids */ 246e057d7aeSMike Travis if (!alloc_cpumask_var(&offline, GFP_KERNEL)) 247e057d7aeSMike Travis return -ENOMEM; 248cdc6e3d3SJan Beulich cpumask_andnot(offline, cpu_possible_mask, cpu_online_mask); 249f799b1a7STejun Heo n = scnprintf(buf, len, "%*pbl", cpumask_pr_args(offline)); 250e057d7aeSMike Travis free_cpumask_var(offline); 251e057d7aeSMike Travis 252e057d7aeSMike Travis /* display offline cpus >= nr_cpu_ids */ 253e057d7aeSMike Travis if (total_cpus && nr_cpu_ids < total_cpus) { 254e057d7aeSMike Travis if (n && n < len) 255e057d7aeSMike Travis buf[n++] = ','; 256e057d7aeSMike Travis 257e057d7aeSMike Travis if (nr_cpu_ids == total_cpus-1) 258e057d7aeSMike Travis n += snprintf(&buf[n], len - n, "%d", nr_cpu_ids); 259e057d7aeSMike Travis else 260e057d7aeSMike Travis n += snprintf(&buf[n], len - n, "%d-%d", 261e057d7aeSMike Travis nr_cpu_ids, total_cpus-1); 262e057d7aeSMike Travis } 263e057d7aeSMike Travis 264e057d7aeSMike Travis n += snprintf(&buf[n], len - n, "\n"); 265e057d7aeSMike Travis return n; 266e057d7aeSMike Travis } 2678a25a2fdSKay Sievers static DEVICE_ATTR(offline, 0444, print_cpus_offline, NULL); 268e057d7aeSMike Travis 26959f30abeSRik van Riel static ssize_t print_cpus_isolated(struct device *dev, 27059f30abeSRik van Riel struct device_attribute *attr, char *buf) 27159f30abeSRik van Riel { 27259f30abeSRik van Riel int n = 0, len = PAGE_SIZE-2; 27359f30abeSRik van Riel 27459f30abeSRik van Riel n = scnprintf(buf, len, "%*pbl\n", cpumask_pr_args(cpu_isolated_map)); 27559f30abeSRik van Riel 27659f30abeSRik van Riel return n; 27759f30abeSRik van Riel } 27859f30abeSRik van Riel static DEVICE_ATTR(isolated, 0444, print_cpus_isolated, NULL); 27959f30abeSRik van Riel 2806570a9a1SRik van Riel #ifdef CONFIG_NO_HZ_FULL 2816570a9a1SRik van Riel static ssize_t print_cpus_nohz_full(struct device *dev, 2826570a9a1SRik van Riel struct device_attribute *attr, char *buf) 2836570a9a1SRik van Riel { 2846570a9a1SRik van Riel int n = 0, len = PAGE_SIZE-2; 2856570a9a1SRik van Riel 2866570a9a1SRik van Riel n = scnprintf(buf, len, "%*pbl\n", cpumask_pr_args(tick_nohz_full_mask)); 2876570a9a1SRik van Riel 2886570a9a1SRik van Riel return n; 2896570a9a1SRik van Riel } 2906570a9a1SRik van Riel static DEVICE_ATTR(nohz_full, 0444, print_cpus_nohz_full, NULL); 2916570a9a1SRik van Riel #endif 2926570a9a1SRik van Riel 2932885e25cSGreg Kroah-Hartman static void cpu_device_release(struct device *dev) 2942885e25cSGreg Kroah-Hartman { 2952885e25cSGreg Kroah-Hartman /* 2962885e25cSGreg Kroah-Hartman * This is an empty function to prevent the driver core from spitting a 2972885e25cSGreg Kroah-Hartman * warning at us. Yes, I know this is directly opposite of what the 2982885e25cSGreg Kroah-Hartman * documentation for the driver core and kobjects say, and the author 2992885e25cSGreg Kroah-Hartman * of this code has already been publically ridiculed for doing 3002885e25cSGreg Kroah-Hartman * something as foolish as this. However, at this point in time, it is 3012885e25cSGreg Kroah-Hartman * the only way to handle the issue of statically allocated cpu 3022885e25cSGreg Kroah-Hartman * devices. The different architectures will have their cpu device 3032885e25cSGreg Kroah-Hartman * code reworked to properly handle this in the near future, so this 3042885e25cSGreg Kroah-Hartman * function will then be changed to correctly free up the memory held 3052885e25cSGreg Kroah-Hartman * by the cpu device. 3062885e25cSGreg Kroah-Hartman * 3072885e25cSGreg Kroah-Hartman * Never copy this way of doing things, or you too will be made fun of 30830a4840aSRalf Baechle * on the linux-kernel list, you have been warned. 3092885e25cSGreg Kroah-Hartman */ 3102885e25cSGreg Kroah-Hartman } 3112885e25cSGreg Kroah-Hartman 31267bad2fdSArd Biesheuvel #ifdef CONFIG_GENERIC_CPU_AUTOPROBE 31367bad2fdSArd Biesheuvel static ssize_t print_cpu_modalias(struct device *dev, 31467bad2fdSArd Biesheuvel struct device_attribute *attr, 31567bad2fdSArd Biesheuvel char *buf) 31667bad2fdSArd Biesheuvel { 31767bad2fdSArd Biesheuvel ssize_t n; 31867bad2fdSArd Biesheuvel u32 i; 31967bad2fdSArd Biesheuvel 32067bad2fdSArd Biesheuvel n = sprintf(buf, "cpu:type:" CPU_FEATURE_TYPEFMT ":feature:", 32167bad2fdSArd Biesheuvel CPU_FEATURE_TYPEVAL); 32267bad2fdSArd Biesheuvel 32367bad2fdSArd Biesheuvel for (i = 0; i < MAX_CPU_FEATURES; i++) 32467bad2fdSArd Biesheuvel if (cpu_have_feature(i)) { 32567bad2fdSArd Biesheuvel if (PAGE_SIZE < n + sizeof(",XXXX\n")) { 32667bad2fdSArd Biesheuvel WARN(1, "CPU features overflow page\n"); 32767bad2fdSArd Biesheuvel break; 32867bad2fdSArd Biesheuvel } 32967bad2fdSArd Biesheuvel n += sprintf(&buf[n], ",%04X", i); 33067bad2fdSArd Biesheuvel } 33167bad2fdSArd Biesheuvel buf[n++] = '\n'; 33267bad2fdSArd Biesheuvel return n; 33367bad2fdSArd Biesheuvel } 33467bad2fdSArd Biesheuvel 33567bad2fdSArd Biesheuvel static int cpu_uevent(struct device *dev, struct kobj_uevent_env *env) 33667bad2fdSArd Biesheuvel { 33767bad2fdSArd Biesheuvel char *buf = kzalloc(PAGE_SIZE, GFP_KERNEL); 33867bad2fdSArd Biesheuvel if (buf) { 33967bad2fdSArd Biesheuvel print_cpu_modalias(NULL, NULL, buf); 34067bad2fdSArd Biesheuvel add_uevent_var(env, "MODALIAS=%s", buf); 34167bad2fdSArd Biesheuvel kfree(buf); 34267bad2fdSArd Biesheuvel } 34367bad2fdSArd Biesheuvel return 0; 34467bad2fdSArd Biesheuvel } 34567bad2fdSArd Biesheuvel #endif 34667bad2fdSArd Biesheuvel 3479d1fe323SMike Travis /* 348405ae7d3SRobert P. J. Day * register_cpu - Setup a sysfs device for a CPU. 34972486f1fSSiddha, Suresh B * @cpu - cpu->hotpluggable field set to 1 will generate a control file in 35072486f1fSSiddha, Suresh B * sysfs for this CPU. 3511da177e4SLinus Torvalds * @num - CPU number to use when creating the device. 3521da177e4SLinus Torvalds * 3531da177e4SLinus Torvalds * Initialize and register the CPU device. 3541da177e4SLinus Torvalds */ 355a83048ebSPaul Gortmaker int register_cpu(struct cpu *cpu, int num) 3561da177e4SLinus Torvalds { 3571da177e4SLinus Torvalds int error; 3588a25a2fdSKay Sievers 3591da177e4SLinus Torvalds cpu->node_id = cpu_to_node(num); 36029bb5d4fSGreg Kroah-Hartman memset(&cpu->dev, 0x00, sizeof(struct device)); 3618a25a2fdSKay Sievers cpu->dev.id = num; 3628a25a2fdSKay Sievers cpu->dev.bus = &cpu_subsys; 3632885e25cSGreg Kroah-Hartman cpu->dev.release = cpu_device_release; 3640902a904SRafael J. Wysocki cpu->dev.offline_disabled = !cpu->hotpluggable; 3651001b4d4SToshi Kani cpu->dev.offline = !cpu_online(num); 366f86e4718SSudeep KarkadaNagesha cpu->dev.of_node = of_get_cpu_node(num, NULL); 3672b9c1f03SArd Biesheuvel #ifdef CONFIG_GENERIC_CPU_AUTOPROBE 36867bad2fdSArd Biesheuvel cpu->dev.bus->uevent = cpu_uevent; 369fad12ac8SThomas Renninger #endif 370c055da9fSIgor Mammedov cpu->dev.groups = common_cpu_attr_groups; 3711c4e2d70SIgor Mammedov if (cpu->hotpluggable) 3721c4e2d70SIgor Mammedov cpu->dev.groups = hotplugable_cpu_attr_groups; 3738a25a2fdSKay Sievers error = device_register(&cpu->dev); 374ad74557aSAshok Raj if (!error) 3758a25a2fdSKay Sievers per_cpu(cpu_sys_devices, num) = &cpu->dev; 37676b67ed9SKAMEZAWA Hiroyuki if (!error) 37776b67ed9SKAMEZAWA Hiroyuki register_cpu_under_node(num, cpu_to_node(num)); 37851be5606SVivek Goyal 3791da177e4SLinus Torvalds return error; 3801da177e4SLinus Torvalds } 3811da177e4SLinus Torvalds 3828a25a2fdSKay Sievers struct device *get_cpu_device(unsigned cpu) 383ad74557aSAshok Raj { 384e37d05daSMike Travis if (cpu < nr_cpu_ids && cpu_possible(cpu)) 385e37d05daSMike Travis return per_cpu(cpu_sys_devices, cpu); 386ad74557aSAshok Raj else 387ad74557aSAshok Raj return NULL; 388ad74557aSAshok Raj } 3898a25a2fdSKay Sievers EXPORT_SYMBOL_GPL(get_cpu_device); 3908a25a2fdSKay Sievers 3913d52943bSSudeep Holla static void device_create_release(struct device *dev) 3923d52943bSSudeep Holla { 3933d52943bSSudeep Holla kfree(dev); 3943d52943bSSudeep Holla } 3953d52943bSSudeep Holla 3963d52943bSSudeep Holla static struct device * 3973d52943bSSudeep Holla __cpu_device_create(struct device *parent, void *drvdata, 3983d52943bSSudeep Holla const struct attribute_group **groups, 3993d52943bSSudeep Holla const char *fmt, va_list args) 4003d52943bSSudeep Holla { 4013d52943bSSudeep Holla struct device *dev = NULL; 4023d52943bSSudeep Holla int retval = -ENODEV; 4033d52943bSSudeep Holla 4043d52943bSSudeep Holla dev = kzalloc(sizeof(*dev), GFP_KERNEL); 4053d52943bSSudeep Holla if (!dev) { 4063d52943bSSudeep Holla retval = -ENOMEM; 4073d52943bSSudeep Holla goto error; 4083d52943bSSudeep Holla } 4093d52943bSSudeep Holla 4103d52943bSSudeep Holla device_initialize(dev); 4113d52943bSSudeep Holla dev->parent = parent; 4123d52943bSSudeep Holla dev->groups = groups; 4133d52943bSSudeep Holla dev->release = device_create_release; 4143d52943bSSudeep Holla dev_set_drvdata(dev, drvdata); 4153d52943bSSudeep Holla 4163d52943bSSudeep Holla retval = kobject_set_name_vargs(&dev->kobj, fmt, args); 4173d52943bSSudeep Holla if (retval) 4183d52943bSSudeep Holla goto error; 4193d52943bSSudeep Holla 4203d52943bSSudeep Holla retval = device_add(dev); 4213d52943bSSudeep Holla if (retval) 4223d52943bSSudeep Holla goto error; 4233d52943bSSudeep Holla 4243d52943bSSudeep Holla return dev; 4253d52943bSSudeep Holla 4263d52943bSSudeep Holla error: 4273d52943bSSudeep Holla put_device(dev); 4283d52943bSSudeep Holla return ERR_PTR(retval); 4293d52943bSSudeep Holla } 4303d52943bSSudeep Holla 4313d52943bSSudeep Holla struct device *cpu_device_create(struct device *parent, void *drvdata, 4323d52943bSSudeep Holla const struct attribute_group **groups, 4333d52943bSSudeep Holla const char *fmt, ...) 4343d52943bSSudeep Holla { 4353d52943bSSudeep Holla va_list vargs; 4363d52943bSSudeep Holla struct device *dev; 4373d52943bSSudeep Holla 4383d52943bSSudeep Holla va_start(vargs, fmt); 4393d52943bSSudeep Holla dev = __cpu_device_create(parent, drvdata, groups, fmt, vargs); 4403d52943bSSudeep Holla va_end(vargs); 4413d52943bSSudeep Holla return dev; 4423d52943bSSudeep Holla } 4433d52943bSSudeep Holla EXPORT_SYMBOL_GPL(cpu_device_create); 4443d52943bSSudeep Holla 4452b9c1f03SArd Biesheuvel #ifdef CONFIG_GENERIC_CPU_AUTOPROBE 44667bad2fdSArd Biesheuvel static DEVICE_ATTR(modalias, 0444, print_cpu_modalias, NULL); 447fad12ac8SThomas Renninger #endif 448fad12ac8SThomas Renninger 4498a25a2fdSKay Sievers static struct attribute *cpu_root_attrs[] = { 4508a25a2fdSKay Sievers #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE 4518a25a2fdSKay Sievers &dev_attr_probe.attr, 4528a25a2fdSKay Sievers &dev_attr_release.attr, 4538a25a2fdSKay Sievers #endif 4548a25a2fdSKay Sievers &cpu_attrs[0].attr.attr, 4558a25a2fdSKay Sievers &cpu_attrs[1].attr.attr, 4568a25a2fdSKay Sievers &cpu_attrs[2].attr.attr, 4578a25a2fdSKay Sievers &dev_attr_kernel_max.attr, 4588a25a2fdSKay Sievers &dev_attr_offline.attr, 45959f30abeSRik van Riel &dev_attr_isolated.attr, 4606570a9a1SRik van Riel #ifdef CONFIG_NO_HZ_FULL 4616570a9a1SRik van Riel &dev_attr_nohz_full.attr, 4626570a9a1SRik van Riel #endif 4632b9c1f03SArd Biesheuvel #ifdef CONFIG_GENERIC_CPU_AUTOPROBE 464fad12ac8SThomas Renninger &dev_attr_modalias.attr, 465fad12ac8SThomas Renninger #endif 4668a25a2fdSKay Sievers NULL 4678a25a2fdSKay Sievers }; 4688a25a2fdSKay Sievers 4698a25a2fdSKay Sievers static struct attribute_group cpu_root_attr_group = { 4708a25a2fdSKay Sievers .attrs = cpu_root_attrs, 4718a25a2fdSKay Sievers }; 4728a25a2fdSKay Sievers 4738a25a2fdSKay Sievers static const struct attribute_group *cpu_root_attr_groups[] = { 4748a25a2fdSKay Sievers &cpu_root_attr_group, 4758a25a2fdSKay Sievers NULL, 4768a25a2fdSKay Sievers }; 4771da177e4SLinus Torvalds 4782987557fSJosh Triplett bool cpu_is_hotpluggable(unsigned cpu) 4792987557fSJosh Triplett { 4807affca35SLinus Torvalds struct device *dev = get_cpu_device(cpu); 4817affca35SLinus Torvalds return dev && container_of(dev, struct cpu, dev)->hotpluggable; 4822987557fSJosh Triplett } 4832987557fSJosh Triplett EXPORT_SYMBOL_GPL(cpu_is_hotpluggable); 4842987557fSJosh Triplett 4859f13a1fdSBen Hutchings #ifdef CONFIG_GENERIC_CPU_DEVICES 4869f13a1fdSBen Hutchings static DEFINE_PER_CPU(struct cpu, cpu_devices); 4879f13a1fdSBen Hutchings #endif 4889f13a1fdSBen Hutchings 4899f13a1fdSBen Hutchings static void __init cpu_dev_register_generic(void) 4909f13a1fdSBen Hutchings { 4919f13a1fdSBen Hutchings #ifdef CONFIG_GENERIC_CPU_DEVICES 4929f13a1fdSBen Hutchings int i; 4939f13a1fdSBen Hutchings 4949f13a1fdSBen Hutchings for_each_possible_cpu(i) { 4959f13a1fdSBen Hutchings if (register_cpu(&per_cpu(cpu_devices, i), i)) 4969f13a1fdSBen Hutchings panic("Failed to register CPU device"); 4979f13a1fdSBen Hutchings } 4989f13a1fdSBen Hutchings #endif 4999f13a1fdSBen Hutchings } 5009f13a1fdSBen Hutchings 501024f7846SBen Hutchings void __init cpu_dev_init(void) 5021da177e4SLinus Torvalds { 503024f7846SBen Hutchings if (subsys_system_register(&cpu_subsys, cpu_root_attr_groups)) 504024f7846SBen Hutchings panic("Failed to register CPU subsystem"); 5055c45bf27SSiddha, Suresh B 5069f13a1fdSBen Hutchings cpu_dev_register_generic(); 5071da177e4SLinus Torvalds } 508