1989d42e8SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
269dcc991SZhang, Yanmin /*
369dcc991SZhang, Yanmin * driver/base/topology.c - Populate sysfs with cpu topology information
469dcc991SZhang, Yanmin *
569dcc991SZhang, Yanmin * Written by: Zhang Yanmin, Intel Corporation
669dcc991SZhang, Yanmin *
769dcc991SZhang, Yanmin * Copyright (C) 2006, Intel Corp.
869dcc991SZhang, Yanmin *
969dcc991SZhang, Yanmin * All rights reserved.
1069dcc991SZhang, Yanmin */
1169dcc991SZhang, Yanmin #include <linux/mm.h>
1269dcc991SZhang, Yanmin #include <linux/cpu.h>
1369dcc991SZhang, Yanmin #include <linux/module.h>
14b13d3720SDavid Miller #include <linux/hardirq.h>
1569dcc991SZhang, Yanmin #include <linux/topology.h>
1669dcc991SZhang, Yanmin
17182ecfafSTony Luck #define define_id_show_func(name, fmt) \
18d6ea8d01SSudeep Holla static ssize_t name##_show(struct device *dev, \
198a25a2fdSKay Sievers struct device_attribute *attr, char *buf) \
2069dcc991SZhang, Yanmin { \
21182ecfafSTony Luck return sysfs_emit(buf, fmt "\n", topology_##name(dev->id)); \
2269dcc991SZhang, Yanmin }
2369dcc991SZhang, Yanmin
24bb9ec13dSTian Tao #define define_siblings_read_func(name, mask) \
25bb9ec13dSTian Tao static ssize_t name##_read(struct file *file, struct kobject *kobj, \
26bb9ec13dSTian Tao struct bin_attribute *attr, char *buf, \
27bb9ec13dSTian Tao loff_t off, size_t count) \
2823ca4bbaSMike Travis { \
29bb9ec13dSTian Tao struct device *dev = kobj_to_dev(kobj); \
30bb9ec13dSTian Tao \
31bb9ec13dSTian Tao return cpumap_print_bitmask_to_buf(buf, topology_##mask(dev->id), \
32bb9ec13dSTian Tao off, count); \
33bb9ec13dSTian Tao } \
34bb9ec13dSTian Tao \
35bb9ec13dSTian Tao static ssize_t name##_list_read(struct file *file, struct kobject *kobj, \
36bb9ec13dSTian Tao struct bin_attribute *attr, char *buf, \
37bb9ec13dSTian Tao loff_t off, size_t count) \
3823ca4bbaSMike Travis { \
39bb9ec13dSTian Tao struct device *dev = kobj_to_dev(kobj); \
40bb9ec13dSTian Tao \
41bb9ec13dSTian Tao return cpumap_print_list_to_buf(buf, topology_##mask(dev->id), \
42bb9ec13dSTian Tao off, count); \
4323ca4bbaSMike Travis }
4423ca4bbaSMike Travis
45182ecfafSTony Luck define_id_show_func(physical_package_id, "%d");
46d6ea8d01SSudeep Holla static DEVICE_ATTR_RO(physical_package_id);
4769dcc991SZhang, Yanmin
482c4dcd7fSHeiko Carstens #ifdef TOPOLOGY_DIE_SYSFS
49182ecfafSTony Luck define_id_show_func(die_id, "%d");
500e344d8cSLen Brown static DEVICE_ATTR_RO(die_id);
512c4dcd7fSHeiko Carstens #endif
520e344d8cSLen Brown
53e7957077SHeiko Carstens #ifdef TOPOLOGY_CLUSTER_SYSFS
54182ecfafSTony Luck define_id_show_func(cluster_id, "%d");
55c5e22fefSJonathan Cameron static DEVICE_ATTR_RO(cluster_id);
56e7957077SHeiko Carstens #endif
57c5e22fefSJonathan Cameron
58182ecfafSTony Luck define_id_show_func(core_id, "%d");
59d6ea8d01SSudeep Holla static DEVICE_ATTR_RO(core_id);
6069dcc991SZhang, Yanmin
61ab28e944STony Luck define_id_show_func(ppin, "0x%llx");
62ab28e944STony Luck static DEVICE_ATTR_ADMIN_RO(ppin);
63ab28e944STony Luck
64bb9ec13dSTian Tao define_siblings_read_func(thread_siblings, sibling_cpumask);
65*7ee951acSPhil Auld static BIN_ATTR_RO(thread_siblings, CPUMAP_FILE_MAX_BYTES);
66*7ee951acSPhil Auld static BIN_ATTR_RO(thread_siblings_list, CPULIST_FILE_MAX_BYTES);
6769dcc991SZhang, Yanmin
68bb9ec13dSTian Tao define_siblings_read_func(core_cpus, sibling_cpumask);
69*7ee951acSPhil Auld static BIN_ATTR_RO(core_cpus, CPUMAP_FILE_MAX_BYTES);
70*7ee951acSPhil Auld static BIN_ATTR_RO(core_cpus_list, CPULIST_FILE_MAX_BYTES);
712e4c54daSLen Brown
72bb9ec13dSTian Tao define_siblings_read_func(core_siblings, core_cpumask);
73*7ee951acSPhil Auld static BIN_ATTR_RO(core_siblings, CPUMAP_FILE_MAX_BYTES);
74*7ee951acSPhil Auld static BIN_ATTR_RO(core_siblings_list, CPULIST_FILE_MAX_BYTES);
7569dcc991SZhang, Yanmin
76e7957077SHeiko Carstens #ifdef TOPOLOGY_CLUSTER_SYSFS
77c5e22fefSJonathan Cameron define_siblings_read_func(cluster_cpus, cluster_cpumask);
78*7ee951acSPhil Auld static BIN_ATTR_RO(cluster_cpus, CPUMAP_FILE_MAX_BYTES);
79*7ee951acSPhil Auld static BIN_ATTR_RO(cluster_cpus_list, CPULIST_FILE_MAX_BYTES);
80e7957077SHeiko Carstens #endif
81c5e22fefSJonathan Cameron
822c4dcd7fSHeiko Carstens #ifdef TOPOLOGY_DIE_SYSFS
83bb9ec13dSTian Tao define_siblings_read_func(die_cpus, die_cpumask);
84*7ee951acSPhil Auld static BIN_ATTR_RO(die_cpus, CPUMAP_FILE_MAX_BYTES);
85*7ee951acSPhil Auld static BIN_ATTR_RO(die_cpus_list, CPULIST_FILE_MAX_BYTES);
862c4dcd7fSHeiko Carstens #endif
872e4c54daSLen Brown
88bb9ec13dSTian Tao define_siblings_read_func(package_cpus, core_cpumask);
89*7ee951acSPhil Auld static BIN_ATTR_RO(package_cpus, CPUMAP_FILE_MAX_BYTES);
90*7ee951acSPhil Auld static BIN_ATTR_RO(package_cpus_list, CPULIST_FILE_MAX_BYTES);
91b73ed8dcSLen Brown
92f1045056SHeiko Carstens #ifdef TOPOLOGY_BOOK_SYSFS
93182ecfafSTony Luck define_id_show_func(book_id, "%d");
94d6ea8d01SSudeep Holla static DEVICE_ATTR_RO(book_id);
95bb9ec13dSTian Tao define_siblings_read_func(book_siblings, book_cpumask);
96*7ee951acSPhil Auld static BIN_ATTR_RO(book_siblings, CPUMAP_FILE_MAX_BYTES);
97*7ee951acSPhil Auld static BIN_ATTR_RO(book_siblings_list, CPULIST_FILE_MAX_BYTES);
98b40d8ed4SHeiko Carstens #endif
99b40d8ed4SHeiko Carstens
100f1045056SHeiko Carstens #ifdef TOPOLOGY_DRAWER_SYSFS
101182ecfafSTony Luck define_id_show_func(drawer_id, "%d");
102a62247e1SHeiko Carstens static DEVICE_ATTR_RO(drawer_id);
103bb9ec13dSTian Tao define_siblings_read_func(drawer_siblings, drawer_cpumask);
104*7ee951acSPhil Auld static BIN_ATTR_RO(drawer_siblings, CPUMAP_FILE_MAX_BYTES);
105*7ee951acSPhil Auld static BIN_ATTR_RO(drawer_siblings_list, CPULIST_FILE_MAX_BYTES);
106a62247e1SHeiko Carstens #endif
107a62247e1SHeiko Carstens
108bb9ec13dSTian Tao static struct bin_attribute *bin_attrs[] = {
109bb9ec13dSTian Tao &bin_attr_core_cpus,
110bb9ec13dSTian Tao &bin_attr_core_cpus_list,
111bb9ec13dSTian Tao &bin_attr_thread_siblings,
112bb9ec13dSTian Tao &bin_attr_thread_siblings_list,
113bb9ec13dSTian Tao &bin_attr_core_siblings,
114bb9ec13dSTian Tao &bin_attr_core_siblings_list,
115e7957077SHeiko Carstens #ifdef TOPOLOGY_CLUSTER_SYSFS
116c5e22fefSJonathan Cameron &bin_attr_cluster_cpus,
117c5e22fefSJonathan Cameron &bin_attr_cluster_cpus_list,
118e7957077SHeiko Carstens #endif
1192c4dcd7fSHeiko Carstens #ifdef TOPOLOGY_DIE_SYSFS
120bb9ec13dSTian Tao &bin_attr_die_cpus,
121bb9ec13dSTian Tao &bin_attr_die_cpus_list,
1222c4dcd7fSHeiko Carstens #endif
123bb9ec13dSTian Tao &bin_attr_package_cpus,
124bb9ec13dSTian Tao &bin_attr_package_cpus_list,
125f1045056SHeiko Carstens #ifdef TOPOLOGY_BOOK_SYSFS
126bb9ec13dSTian Tao &bin_attr_book_siblings,
127bb9ec13dSTian Tao &bin_attr_book_siblings_list,
128bb9ec13dSTian Tao #endif
129f1045056SHeiko Carstens #ifdef TOPOLOGY_DRAWER_SYSFS
130bb9ec13dSTian Tao &bin_attr_drawer_siblings,
131bb9ec13dSTian Tao &bin_attr_drawer_siblings_list,
132bb9ec13dSTian Tao #endif
133bb9ec13dSTian Tao NULL
134bb9ec13dSTian Tao };
135bb9ec13dSTian Tao
13669dcc991SZhang, Yanmin static struct attribute *default_attrs[] = {
1378a25a2fdSKay Sievers &dev_attr_physical_package_id.attr,
1382c4dcd7fSHeiko Carstens #ifdef TOPOLOGY_DIE_SYSFS
1390e344d8cSLen Brown &dev_attr_die_id.attr,
1402c4dcd7fSHeiko Carstens #endif
141e7957077SHeiko Carstens #ifdef TOPOLOGY_CLUSTER_SYSFS
142c5e22fefSJonathan Cameron &dev_attr_cluster_id.attr,
143e7957077SHeiko Carstens #endif
1448a25a2fdSKay Sievers &dev_attr_core_id.attr,
145f1045056SHeiko Carstens #ifdef TOPOLOGY_BOOK_SYSFS
1468a25a2fdSKay Sievers &dev_attr_book_id.attr,
147b40d8ed4SHeiko Carstens #endif
148f1045056SHeiko Carstens #ifdef TOPOLOGY_DRAWER_SYSFS
149a62247e1SHeiko Carstens &dev_attr_drawer_id.attr,
150a62247e1SHeiko Carstens #endif
151ab28e944STony Luck &dev_attr_ppin.attr,
15269dcc991SZhang, Yanmin NULL
15369dcc991SZhang, Yanmin };
15469dcc991SZhang, Yanmin
topology_is_visible(struct kobject * kobj,struct attribute * attr,int unused)155aa63a74dSTony Luck static umode_t topology_is_visible(struct kobject *kobj,
156aa63a74dSTony Luck struct attribute *attr, int unused)
157aa63a74dSTony Luck {
158c95ce3a2SGreg Kroah-Hartman if (attr == &dev_attr_ppin.attr && !topology_ppin(kobj_to_dev(kobj)->id))
159aa63a74dSTony Luck return 0;
160aa63a74dSTony Luck
161aa63a74dSTony Luck return attr->mode;
162aa63a74dSTony Luck }
163aa63a74dSTony Luck
164df44d30dSArvind Yadav static const struct attribute_group topology_attr_group = {
16569dcc991SZhang, Yanmin .attrs = default_attrs,
166bb9ec13dSTian Tao .bin_attrs = bin_attrs,
167aa63a74dSTony Luck .is_visible = topology_is_visible,
16869dcc991SZhang, Yanmin .name = "topology"
16969dcc991SZhang, Yanmin };
17069dcc991SZhang, Yanmin
17169dcc991SZhang, Yanmin /* Add/Remove cpu_topology interface for CPU device */
topology_add_dev(unsigned int cpu)172a83048ebSPaul Gortmaker static int topology_add_dev(unsigned int cpu)
17369dcc991SZhang, Yanmin {
1748a25a2fdSKay Sievers struct device *dev = get_cpu_device(cpu);
17506a4bcaeSHeiko Carstens
1768a25a2fdSKay Sievers return sysfs_create_group(&dev->kobj, &topology_attr_group);
17769dcc991SZhang, Yanmin }
17869dcc991SZhang, Yanmin
topology_remove_dev(unsigned int cpu)17938643a0eSSebastian Andrzej Siewior static int topology_remove_dev(unsigned int cpu)
18069dcc991SZhang, Yanmin {
1818a25a2fdSKay Sievers struct device *dev = get_cpu_device(cpu);
18206a4bcaeSHeiko Carstens
1838a25a2fdSKay Sievers sysfs_remove_group(&dev->kobj, &topology_attr_group);
18438643a0eSSebastian Andrzej Siewior return 0;
18569dcc991SZhang, Yanmin }
18669dcc991SZhang, Yanmin
topology_sysfs_init(void)1870d989da3SChristophe JAILLET static int __init topology_sysfs_init(void)
18869dcc991SZhang, Yanmin {
18938643a0eSSebastian Andrzej Siewior return cpuhp_setup_state(CPUHP_TOPOLOGY_PREPARE,
19038643a0eSSebastian Andrzej Siewior "base/topology:prepare", topology_add_dev,
19138643a0eSSebastian Andrzej Siewior topology_remove_dev);
19269dcc991SZhang, Yanmin }
19369dcc991SZhang, Yanmin
19469dcc991SZhang, Yanmin device_initcall(topology_sysfs_init);
195