blob: 4e033d4cc0dcffcaf3f201e68100afa0f7abcfbb [file] [log] [blame]
Greg Kroah-Hartman989d42e2017-11-07 17:30:07 +01001// SPDX-License-Identifier: GPL-2.0+
Zhang, Yanmin69dcc992006-02-03 03:04:36 -08002/*
3 * driver/base/topology.c - Populate sysfs with cpu topology information
4 *
5 * Written by: Zhang Yanmin, Intel Corporation
6 *
7 * Copyright (C) 2006, Intel Corp.
8 *
9 * All rights reserved.
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080010 */
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080011#include <linux/mm.h>
12#include <linux/cpu.h>
13#include <linux/module.h>
David Millerb13d3722009-01-07 15:30:05 -080014#include <linux/hardirq.h>
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080015#include <linux/topology.h>
16
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080017#define define_id_show_func(name) \
Sudeep Hollad6ea8d02014-09-30 14:48:23 +010018static ssize_t name##_show(struct device *dev, \
Kay Sievers8a25a2f2011-12-21 14:29:42 -080019 struct device_attribute *attr, char *buf) \
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080020{ \
Vincent Stehlé53974e02014-04-04 08:43:18 +020021 return sprintf(buf, "%d\n", topology_##name(dev->id)); \
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080022}
23
Sudeep Hollad6ea8d02014-09-30 14:48:23 +010024#define define_siblings_show_map(name, mask) \
25static ssize_t name##_show(struct device *dev, \
Kay Sievers8a25a2f2011-12-21 14:29:42 -080026 struct device_attribute *attr, char *buf) \
Mike Travis23ca4bb2008-05-12 21:21:12 +020027{ \
Sudeep Hollad6ea8d02014-09-30 14:48:23 +010028 return cpumap_print_to_pagebuf(false, buf, topology_##mask(dev->id));\
Mike Travis23ca4bb2008-05-12 21:21:12 +020029}
30
Sudeep Hollad6ea8d02014-09-30 14:48:23 +010031#define define_siblings_show_list(name, mask) \
32static ssize_t name##_list_show(struct device *dev, \
33 struct device_attribute *attr, \
34 char *buf) \
Mike Travis23ca4bb2008-05-12 21:21:12 +020035{ \
Sudeep Hollad6ea8d02014-09-30 14:48:23 +010036 return cpumap_print_to_pagebuf(true, buf, topology_##mask(dev->id));\
Mike Travis23ca4bb2008-05-12 21:21:12 +020037}
Mike Travis23ca4bb2008-05-12 21:21:12 +020038
Sudeep Hollad6ea8d02014-09-30 14:48:23 +010039#define define_siblings_show_func(name, mask) \
40 define_siblings_show_map(name, mask); \
41 define_siblings_show_list(name, mask)
Mike Travis39106dc2008-04-08 11:43:03 -070042
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080043define_id_show_func(physical_package_id);
Sudeep Hollad6ea8d02014-09-30 14:48:23 +010044static DEVICE_ATTR_RO(physical_package_id);
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080045
Len Brown0e344d82019-05-13 13:58:47 -040046define_id_show_func(die_id);
47static DEVICE_ATTR_RO(die_id);
48
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080049define_id_show_func(core_id);
Sudeep Hollad6ea8d02014-09-30 14:48:23 +010050static DEVICE_ATTR_RO(core_id);
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080051
Bartosz Golaszewski06931e62015-05-26 15:11:28 +020052define_siblings_show_func(thread_siblings, sibling_cpumask);
Sudeep Hollad6ea8d02014-09-30 14:48:23 +010053static DEVICE_ATTR_RO(thread_siblings);
54static DEVICE_ATTR_RO(thread_siblings_list);
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080055
Len Brown2e4c54d2019-05-13 13:58:56 -040056define_siblings_show_func(core_cpus, sibling_cpumask);
57static DEVICE_ATTR_RO(core_cpus);
58static DEVICE_ATTR_RO(core_cpus_list);
59
Sudeep Hollad6ea8d02014-09-30 14:48:23 +010060define_siblings_show_func(core_siblings, core_cpumask);
61static DEVICE_ATTR_RO(core_siblings);
62static DEVICE_ATTR_RO(core_siblings_list);
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080063
Len Brown2e4c54d2019-05-13 13:58:56 -040064define_siblings_show_func(die_cpus, die_cpumask);
65static DEVICE_ATTR_RO(die_cpus);
66static DEVICE_ATTR_RO(die_cpus_list);
67
Len Brownb73ed8d2019-05-13 13:58:55 -040068define_siblings_show_func(package_cpus, core_cpumask);
69static DEVICE_ATTR_RO(package_cpus);
70static DEVICE_ATTR_RO(package_cpus_list);
71
Heiko Carstensb40d8ed2010-08-31 10:28:17 +020072#ifdef CONFIG_SCHED_BOOK
73define_id_show_func(book_id);
Sudeep Hollad6ea8d02014-09-30 14:48:23 +010074static DEVICE_ATTR_RO(book_id);
75define_siblings_show_func(book_siblings, book_cpumask);
76static DEVICE_ATTR_RO(book_siblings);
77static DEVICE_ATTR_RO(book_siblings_list);
Heiko Carstensb40d8ed2010-08-31 10:28:17 +020078#endif
79
Heiko Carstensa62247e2016-05-21 11:10:13 +020080#ifdef CONFIG_SCHED_DRAWER
81define_id_show_func(drawer_id);
82static DEVICE_ATTR_RO(drawer_id);
83define_siblings_show_func(drawer_siblings, drawer_cpumask);
84static DEVICE_ATTR_RO(drawer_siblings);
85static DEVICE_ATTR_RO(drawer_siblings_list);
86#endif
87
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080088static struct attribute *default_attrs[] = {
Kay Sievers8a25a2f2011-12-21 14:29:42 -080089 &dev_attr_physical_package_id.attr,
Len Brown0e344d82019-05-13 13:58:47 -040090 &dev_attr_die_id.attr,
Kay Sievers8a25a2f2011-12-21 14:29:42 -080091 &dev_attr_core_id.attr,
92 &dev_attr_thread_siblings.attr,
93 &dev_attr_thread_siblings_list.attr,
Len Brown2e4c54d2019-05-13 13:58:56 -040094 &dev_attr_core_cpus.attr,
95 &dev_attr_core_cpus_list.attr,
Kay Sievers8a25a2f2011-12-21 14:29:42 -080096 &dev_attr_core_siblings.attr,
97 &dev_attr_core_siblings_list.attr,
Len Brown2e4c54d2019-05-13 13:58:56 -040098 &dev_attr_die_cpus.attr,
99 &dev_attr_die_cpus_list.attr,
Len Brownb73ed8d2019-05-13 13:58:55 -0400100 &dev_attr_package_cpus.attr,
101 &dev_attr_package_cpus_list.attr,
Heiko Carstensb40d8ed2010-08-31 10:28:17 +0200102#ifdef CONFIG_SCHED_BOOK
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800103 &dev_attr_book_id.attr,
104 &dev_attr_book_siblings.attr,
105 &dev_attr_book_siblings_list.attr,
Heiko Carstensb40d8ed2010-08-31 10:28:17 +0200106#endif
Heiko Carstensa62247e2016-05-21 11:10:13 +0200107#ifdef CONFIG_SCHED_DRAWER
108 &dev_attr_drawer_id.attr,
109 &dev_attr_drawer_siblings.attr,
110 &dev_attr_drawer_siblings_list.attr,
111#endif
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800112 NULL
113};
114
Arvind Yadavdf44d302017-08-02 19:36:20 +0530115static const struct attribute_group topology_attr_group = {
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800116 .attrs = default_attrs,
117 .name = "topology"
118};
119
120/* Add/Remove cpu_topology interface for CPU device */
Paul Gortmakera83048e2013-06-19 15:22:41 -0400121static int topology_add_dev(unsigned int cpu)
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800122{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800123 struct device *dev = get_cpu_device(cpu);
Heiko Carstens06a4bca2006-11-08 19:46:09 -0800124
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800125 return sysfs_create_group(&dev->kobj, &topology_attr_group);
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800126}
127
Sebastian Andrzej Siewior38643a02016-11-03 15:50:09 +0100128static int topology_remove_dev(unsigned int cpu)
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800129{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800130 struct device *dev = get_cpu_device(cpu);
Heiko Carstens06a4bca2006-11-08 19:46:09 -0800131
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800132 sysfs_remove_group(&dev->kobj, &topology_attr_group);
Sebastian Andrzej Siewior38643a02016-11-03 15:50:09 +0100133 return 0;
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800134}
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800135
Paul Gortmakera83048e2013-06-19 15:22:41 -0400136static int topology_sysfs_init(void)
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800137{
Sebastian Andrzej Siewior38643a02016-11-03 15:50:09 +0100138 return cpuhp_setup_state(CPUHP_TOPOLOGY_PREPARE,
139 "base/topology:prepare", topology_add_dev,
140 topology_remove_dev);
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800141}
142
143device_initcall(topology_sysfs_init);