blob: 5fd9f167ecc10e68a53c29b0a0046206a5003879 [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
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080046define_id_show_func(core_id);
Sudeep Hollad6ea8d02014-09-30 14:48:23 +010047static DEVICE_ATTR_RO(core_id);
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080048
Bartosz Golaszewski06931e62015-05-26 15:11:28 +020049define_siblings_show_func(thread_siblings, sibling_cpumask);
Sudeep Hollad6ea8d02014-09-30 14:48:23 +010050static DEVICE_ATTR_RO(thread_siblings);
51static DEVICE_ATTR_RO(thread_siblings_list);
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080052
Sudeep Hollad6ea8d02014-09-30 14:48:23 +010053define_siblings_show_func(core_siblings, core_cpumask);
54static DEVICE_ATTR_RO(core_siblings);
55static DEVICE_ATTR_RO(core_siblings_list);
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080056
Heiko Carstensb40d8ed2010-08-31 10:28:17 +020057#ifdef CONFIG_SCHED_BOOK
58define_id_show_func(book_id);
Sudeep Hollad6ea8d02014-09-30 14:48:23 +010059static DEVICE_ATTR_RO(book_id);
60define_siblings_show_func(book_siblings, book_cpumask);
61static DEVICE_ATTR_RO(book_siblings);
62static DEVICE_ATTR_RO(book_siblings_list);
Heiko Carstensb40d8ed2010-08-31 10:28:17 +020063#endif
64
Heiko Carstensa62247e2016-05-21 11:10:13 +020065#ifdef CONFIG_SCHED_DRAWER
66define_id_show_func(drawer_id);
67static DEVICE_ATTR_RO(drawer_id);
68define_siblings_show_func(drawer_siblings, drawer_cpumask);
69static DEVICE_ATTR_RO(drawer_siblings);
70static DEVICE_ATTR_RO(drawer_siblings_list);
71#endif
72
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080073static struct attribute *default_attrs[] = {
Kay Sievers8a25a2f2011-12-21 14:29:42 -080074 &dev_attr_physical_package_id.attr,
75 &dev_attr_core_id.attr,
76 &dev_attr_thread_siblings.attr,
77 &dev_attr_thread_siblings_list.attr,
78 &dev_attr_core_siblings.attr,
79 &dev_attr_core_siblings_list.attr,
Heiko Carstensb40d8ed2010-08-31 10:28:17 +020080#ifdef CONFIG_SCHED_BOOK
Kay Sievers8a25a2f2011-12-21 14:29:42 -080081 &dev_attr_book_id.attr,
82 &dev_attr_book_siblings.attr,
83 &dev_attr_book_siblings_list.attr,
Heiko Carstensb40d8ed2010-08-31 10:28:17 +020084#endif
Heiko Carstensa62247e2016-05-21 11:10:13 +020085#ifdef CONFIG_SCHED_DRAWER
86 &dev_attr_drawer_id.attr,
87 &dev_attr_drawer_siblings.attr,
88 &dev_attr_drawer_siblings_list.attr,
89#endif
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080090 NULL
91};
92
Arvind Yadavdf44d302017-08-02 19:36:20 +053093static const struct attribute_group topology_attr_group = {
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080094 .attrs = default_attrs,
95 .name = "topology"
96};
97
98/* Add/Remove cpu_topology interface for CPU device */
Paul Gortmakera83048e2013-06-19 15:22:41 -040099static int topology_add_dev(unsigned int cpu)
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800100{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800101 struct device *dev = get_cpu_device(cpu);
Heiko Carstens06a4bca2006-11-08 19:46:09 -0800102
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800103 return sysfs_create_group(&dev->kobj, &topology_attr_group);
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800104}
105
Sebastian Andrzej Siewior38643a02016-11-03 15:50:09 +0100106static int topology_remove_dev(unsigned int cpu)
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800107{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800108 struct device *dev = get_cpu_device(cpu);
Heiko Carstens06a4bca2006-11-08 19:46:09 -0800109
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800110 sysfs_remove_group(&dev->kobj, &topology_attr_group);
Sebastian Andrzej Siewior38643a02016-11-03 15:50:09 +0100111 return 0;
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800112}
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800113
Paul Gortmakera83048e2013-06-19 15:22:41 -0400114static int topology_sysfs_init(void)
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800115{
Sebastian Andrzej Siewior38643a02016-11-03 15:50:09 +0100116 return cpuhp_setup_state(CPUHP_TOPOLOGY_PREPARE,
117 "base/topology:prepare", topology_add_dev,
118 topology_remove_dev);
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800119}
120
121device_initcall(topology_sysfs_init);