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); \
30*b02cf1d2SLi Huafei cpumask_var_t mask; \
31*b02cf1d2SLi Huafei ssize_t n; \
32bb9ec13dSTian Tao \
33*b02cf1d2SLi Huafei if (!alloc_cpumask_var(&mask, GFP_KERNEL)) \
34*b02cf1d2SLi Huafei return -ENOMEM; \
35*b02cf1d2SLi Huafei \
36*b02cf1d2SLi Huafei cpumask_copy(mask, topology_##mask(dev->id)); \
37*b02cf1d2SLi Huafei n = cpumap_print_bitmask_to_buf(buf, mask, off, count); \
38*b02cf1d2SLi Huafei free_cpumask_var(mask); \
39*b02cf1d2SLi Huafei \
40*b02cf1d2SLi Huafei return n; \
41bb9ec13dSTian Tao } \
42bb9ec13dSTian Tao \
43bb9ec13dSTian Tao static ssize_t name##_list_read(struct file *file, struct kobject *kobj, \
44bb9ec13dSTian Tao struct bin_attribute *attr, char *buf, \
45bb9ec13dSTian Tao loff_t off, size_t count) \
4623ca4bbaSMike Travis { \
47bb9ec13dSTian Tao struct device *dev = kobj_to_dev(kobj); \
48*b02cf1d2SLi Huafei cpumask_var_t mask; \
49*b02cf1d2SLi Huafei ssize_t n; \
50bb9ec13dSTian Tao \
51*b02cf1d2SLi Huafei if (!alloc_cpumask_var(&mask, GFP_KERNEL)) \
52*b02cf1d2SLi Huafei return -ENOMEM; \
53*b02cf1d2SLi Huafei \
54*b02cf1d2SLi Huafei cpumask_copy(mask, topology_##mask(dev->id)); \
55*b02cf1d2SLi Huafei n = cpumap_print_list_to_buf(buf, mask, off, count); \
56*b02cf1d2SLi Huafei free_cpumask_var(mask); \
57*b02cf1d2SLi Huafei \
58*b02cf1d2SLi Huafei return n; \
5923ca4bbaSMike Travis }
6023ca4bbaSMike Travis
61182ecfafSTony Luck define_id_show_func(physical_package_id, "%d");
62d6ea8d01SSudeep Holla static DEVICE_ATTR_RO(physical_package_id);
6369dcc991SZhang, Yanmin
642c4dcd7fSHeiko Carstens #ifdef TOPOLOGY_DIE_SYSFS
65182ecfafSTony Luck define_id_show_func(die_id, "%d");
660e344d8cSLen Brown static DEVICE_ATTR_RO(die_id);
672c4dcd7fSHeiko Carstens #endif
680e344d8cSLen Brown
69e7957077SHeiko Carstens #ifdef TOPOLOGY_CLUSTER_SYSFS
70182ecfafSTony Luck define_id_show_func(cluster_id, "%d");
71c5e22fefSJonathan Cameron static DEVICE_ATTR_RO(cluster_id);
72e7957077SHeiko Carstens #endif
73c5e22fefSJonathan Cameron
74182ecfafSTony Luck define_id_show_func(core_id, "%d");
75d6ea8d01SSudeep Holla static DEVICE_ATTR_RO(core_id);
7669dcc991SZhang, Yanmin
77ab28e944STony Luck define_id_show_func(ppin, "0x%llx");
78ab28e944STony Luck static DEVICE_ATTR_ADMIN_RO(ppin);
79ab28e944STony Luck
80bb9ec13dSTian Tao define_siblings_read_func(thread_siblings, sibling_cpumask);
817ee951acSPhil Auld static BIN_ATTR_RO(thread_siblings, CPUMAP_FILE_MAX_BYTES);
827ee951acSPhil Auld static BIN_ATTR_RO(thread_siblings_list, CPULIST_FILE_MAX_BYTES);
8369dcc991SZhang, Yanmin
84bb9ec13dSTian Tao define_siblings_read_func(core_cpus, sibling_cpumask);
857ee951acSPhil Auld static BIN_ATTR_RO(core_cpus, CPUMAP_FILE_MAX_BYTES);
867ee951acSPhil Auld static BIN_ATTR_RO(core_cpus_list, CPULIST_FILE_MAX_BYTES);
872e4c54daSLen Brown
88bb9ec13dSTian Tao define_siblings_read_func(core_siblings, core_cpumask);
897ee951acSPhil Auld static BIN_ATTR_RO(core_siblings, CPUMAP_FILE_MAX_BYTES);
907ee951acSPhil Auld static BIN_ATTR_RO(core_siblings_list, CPULIST_FILE_MAX_BYTES);
9169dcc991SZhang, Yanmin
92e7957077SHeiko Carstens #ifdef TOPOLOGY_CLUSTER_SYSFS
93c5e22fefSJonathan Cameron define_siblings_read_func(cluster_cpus, cluster_cpumask);
947ee951acSPhil Auld static BIN_ATTR_RO(cluster_cpus, CPUMAP_FILE_MAX_BYTES);
957ee951acSPhil Auld static BIN_ATTR_RO(cluster_cpus_list, CPULIST_FILE_MAX_BYTES);
96e7957077SHeiko Carstens #endif
97c5e22fefSJonathan Cameron
982c4dcd7fSHeiko Carstens #ifdef TOPOLOGY_DIE_SYSFS
99bb9ec13dSTian Tao define_siblings_read_func(die_cpus, die_cpumask);
1007ee951acSPhil Auld static BIN_ATTR_RO(die_cpus, CPUMAP_FILE_MAX_BYTES);
1017ee951acSPhil Auld static BIN_ATTR_RO(die_cpus_list, CPULIST_FILE_MAX_BYTES);
1022c4dcd7fSHeiko Carstens #endif
1032e4c54daSLen Brown
104bb9ec13dSTian Tao define_siblings_read_func(package_cpus, core_cpumask);
1057ee951acSPhil Auld static BIN_ATTR_RO(package_cpus, CPUMAP_FILE_MAX_BYTES);
1067ee951acSPhil Auld static BIN_ATTR_RO(package_cpus_list, CPULIST_FILE_MAX_BYTES);
107b73ed8dcSLen Brown
108f1045056SHeiko Carstens #ifdef TOPOLOGY_BOOK_SYSFS
109182ecfafSTony Luck define_id_show_func(book_id, "%d");
110d6ea8d01SSudeep Holla static DEVICE_ATTR_RO(book_id);
111bb9ec13dSTian Tao define_siblings_read_func(book_siblings, book_cpumask);
1127ee951acSPhil Auld static BIN_ATTR_RO(book_siblings, CPUMAP_FILE_MAX_BYTES);
1137ee951acSPhil Auld static BIN_ATTR_RO(book_siblings_list, CPULIST_FILE_MAX_BYTES);
114b40d8ed4SHeiko Carstens #endif
115b40d8ed4SHeiko Carstens
116f1045056SHeiko Carstens #ifdef TOPOLOGY_DRAWER_SYSFS
117182ecfafSTony Luck define_id_show_func(drawer_id, "%d");
118a62247e1SHeiko Carstens static DEVICE_ATTR_RO(drawer_id);
119bb9ec13dSTian Tao define_siblings_read_func(drawer_siblings, drawer_cpumask);
1207ee951acSPhil Auld static BIN_ATTR_RO(drawer_siblings, CPUMAP_FILE_MAX_BYTES);
1217ee951acSPhil Auld static BIN_ATTR_RO(drawer_siblings_list, CPULIST_FILE_MAX_BYTES);
122a62247e1SHeiko Carstens #endif
123a62247e1SHeiko Carstens
124bb9ec13dSTian Tao static struct bin_attribute *bin_attrs[] = {
125bb9ec13dSTian Tao &bin_attr_core_cpus,
126bb9ec13dSTian Tao &bin_attr_core_cpus_list,
127bb9ec13dSTian Tao &bin_attr_thread_siblings,
128bb9ec13dSTian Tao &bin_attr_thread_siblings_list,
129bb9ec13dSTian Tao &bin_attr_core_siblings,
130bb9ec13dSTian Tao &bin_attr_core_siblings_list,
131e7957077SHeiko Carstens #ifdef TOPOLOGY_CLUSTER_SYSFS
132c5e22fefSJonathan Cameron &bin_attr_cluster_cpus,
133c5e22fefSJonathan Cameron &bin_attr_cluster_cpus_list,
134e7957077SHeiko Carstens #endif
1352c4dcd7fSHeiko Carstens #ifdef TOPOLOGY_DIE_SYSFS
136bb9ec13dSTian Tao &bin_attr_die_cpus,
137bb9ec13dSTian Tao &bin_attr_die_cpus_list,
1382c4dcd7fSHeiko Carstens #endif
139bb9ec13dSTian Tao &bin_attr_package_cpus,
140bb9ec13dSTian Tao &bin_attr_package_cpus_list,
141f1045056SHeiko Carstens #ifdef TOPOLOGY_BOOK_SYSFS
142bb9ec13dSTian Tao &bin_attr_book_siblings,
143bb9ec13dSTian Tao &bin_attr_book_siblings_list,
144bb9ec13dSTian Tao #endif
145f1045056SHeiko Carstens #ifdef TOPOLOGY_DRAWER_SYSFS
146bb9ec13dSTian Tao &bin_attr_drawer_siblings,
147bb9ec13dSTian Tao &bin_attr_drawer_siblings_list,
148bb9ec13dSTian Tao #endif
149bb9ec13dSTian Tao NULL
150bb9ec13dSTian Tao };
151bb9ec13dSTian Tao
15269dcc991SZhang, Yanmin static struct attribute *default_attrs[] = {
1538a25a2fdSKay Sievers &dev_attr_physical_package_id.attr,
1542c4dcd7fSHeiko Carstens #ifdef TOPOLOGY_DIE_SYSFS
1550e344d8cSLen Brown &dev_attr_die_id.attr,
1562c4dcd7fSHeiko Carstens #endif
157e7957077SHeiko Carstens #ifdef TOPOLOGY_CLUSTER_SYSFS
158c5e22fefSJonathan Cameron &dev_attr_cluster_id.attr,
159e7957077SHeiko Carstens #endif
1608a25a2fdSKay Sievers &dev_attr_core_id.attr,
161f1045056SHeiko Carstens #ifdef TOPOLOGY_BOOK_SYSFS
1628a25a2fdSKay Sievers &dev_attr_book_id.attr,
163b40d8ed4SHeiko Carstens #endif
164f1045056SHeiko Carstens #ifdef TOPOLOGY_DRAWER_SYSFS
165a62247e1SHeiko Carstens &dev_attr_drawer_id.attr,
166a62247e1SHeiko Carstens #endif
167ab28e944STony Luck &dev_attr_ppin.attr,
16869dcc991SZhang, Yanmin NULL
16969dcc991SZhang, Yanmin };
17069dcc991SZhang, Yanmin
topology_is_visible(struct kobject * kobj,struct attribute * attr,int unused)171aa63a74dSTony Luck static umode_t topology_is_visible(struct kobject *kobj,
172aa63a74dSTony Luck struct attribute *attr, int unused)
173aa63a74dSTony Luck {
174c95ce3a2SGreg Kroah-Hartman if (attr == &dev_attr_ppin.attr && !topology_ppin(kobj_to_dev(kobj)->id))
175aa63a74dSTony Luck return 0;
176aa63a74dSTony Luck
177aa63a74dSTony Luck return attr->mode;
178aa63a74dSTony Luck }
179aa63a74dSTony Luck
180df44d30dSArvind Yadav static const struct attribute_group topology_attr_group = {
18169dcc991SZhang, Yanmin .attrs = default_attrs,
182bb9ec13dSTian Tao .bin_attrs = bin_attrs,
183aa63a74dSTony Luck .is_visible = topology_is_visible,
18469dcc991SZhang, Yanmin .name = "topology"
18569dcc991SZhang, Yanmin };
18669dcc991SZhang, Yanmin
18769dcc991SZhang, Yanmin /* Add/Remove cpu_topology interface for CPU device */
topology_add_dev(unsigned int cpu)188a83048ebSPaul Gortmaker static int topology_add_dev(unsigned int cpu)
18969dcc991SZhang, Yanmin {
1908a25a2fdSKay Sievers struct device *dev = get_cpu_device(cpu);
19106a4bcaeSHeiko Carstens
1928a25a2fdSKay Sievers return sysfs_create_group(&dev->kobj, &topology_attr_group);
19369dcc991SZhang, Yanmin }
19469dcc991SZhang, Yanmin
topology_remove_dev(unsigned int cpu)19538643a0eSSebastian Andrzej Siewior static int topology_remove_dev(unsigned int cpu)
19669dcc991SZhang, Yanmin {
1978a25a2fdSKay Sievers struct device *dev = get_cpu_device(cpu);
19806a4bcaeSHeiko Carstens
1998a25a2fdSKay Sievers sysfs_remove_group(&dev->kobj, &topology_attr_group);
20038643a0eSSebastian Andrzej Siewior return 0;
20169dcc991SZhang, Yanmin }
20269dcc991SZhang, Yanmin
topology_sysfs_init(void)2030d989da3SChristophe JAILLET static int __init topology_sysfs_init(void)
20469dcc991SZhang, Yanmin {
20538643a0eSSebastian Andrzej Siewior return cpuhp_setup_state(CPUHP_TOPOLOGY_PREPARE,
20638643a0eSSebastian Andrzej Siewior "base/topology:prepare", topology_add_dev,
20738643a0eSSebastian Andrzej Siewior topology_remove_dev);
20869dcc991SZhang, Yanmin }
20969dcc991SZhang, Yanmin
21069dcc991SZhang, Yanmin device_initcall(topology_sysfs_init);
211