blob: be7c1fb7c0c96fa4d19bda57bf465c836a630091 [file] [log] [blame]
Zhang, Yanmin69dcc992006-02-03 03:04:36 -08001/*
2 * driver/base/topology.c - Populate sysfs with cpu topology information
3 *
4 * Written by: Zhang Yanmin, Intel Corporation
5 *
6 * Copyright (C) 2006, Intel Corp.
7 *
8 * All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
18 * NON INFRINGEMENT. See the GNU General Public License for more
19 * details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 */
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080026#include <linux/mm.h>
27#include <linux/cpu.h>
28#include <linux/module.h>
David Millerb13d3722009-01-07 15:30:05 -080029#include <linux/hardirq.h>
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080030#include <linux/topology.h>
31
Rusty Russellfbd59a82009-01-10 21:58:08 -080032#define define_one_ro_named(_name, _func) \
Kay Sievers8a25a2f2011-12-21 14:29:42 -080033 static DEVICE_ATTR(_name, 0444, _func, NULL)
Rusty Russellfbd59a82009-01-10 21:58:08 -080034
35#define define_one_ro(_name) \
Kay Sievers8a25a2f2011-12-21 14:29:42 -080036 static DEVICE_ATTR(_name, 0444, show_##_name, NULL)
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080037
38#define define_id_show_func(name) \
Kay Sievers8a25a2f2011-12-21 14:29:42 -080039static ssize_t show_##name(struct device *dev, \
40 struct device_attribute *attr, char *buf) \
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080041{ \
Vincent Stehlé53974e02014-04-04 08:43:18 +020042 return sprintf(buf, "%d\n", topology_##name(dev->id)); \
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080043}
44
Heiko Carstensb40d8ed2010-08-31 10:28:17 +020045#if defined(topology_thread_cpumask) || defined(topology_core_cpumask) || \
46 defined(topology_book_cpumask)
Rusty Russellfbd59a82009-01-10 21:58:08 -080047static ssize_t show_cpumap(int type, const struct cpumask *mask, char *buf)
Mike Travis39106dc2008-04-08 11:43:03 -070048{
49 ptrdiff_t len = PTR_ALIGN(buf + PAGE_SIZE - 1, PAGE_SIZE) - buf;
50 int n = 0;
51
52 if (len > 1) {
53 n = type?
Rusty Russell29c01772008-12-13 21:20:25 +103054 cpulist_scnprintf(buf, len-2, mask) :
55 cpumask_scnprintf(buf, len-2, mask);
Mike Travis39106dc2008-04-08 11:43:03 -070056 buf[n++] = '\n';
57 buf[n] = '\0';
58 }
59 return n;
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080060}
Mike Travis23ca4bb2008-05-12 21:21:12 +020061#endif
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080062
Mike Travis23ca4bb2008-05-12 21:21:12 +020063#define define_siblings_show_map(name) \
Kay Sievers8a25a2f2011-12-21 14:29:42 -080064static ssize_t show_##name(struct device *dev, \
65 struct device_attribute *attr, char *buf) \
Mike Travis23ca4bb2008-05-12 21:21:12 +020066{ \
Rusty Russellfbd59a82009-01-10 21:58:08 -080067 return show_cpumap(0, topology_##name(dev->id), buf); \
Mike Travis23ca4bb2008-05-12 21:21:12 +020068}
69
70#define define_siblings_show_list(name) \
Kay Sievers8a25a2f2011-12-21 14:29:42 -080071static ssize_t show_##name##_list(struct device *dev, \
72 struct device_attribute *attr, \
Andi Kleen4a0b2b42008-07-01 18:48:41 +020073 char *buf) \
Mike Travis23ca4bb2008-05-12 21:21:12 +020074{ \
Rusty Russellfbd59a82009-01-10 21:58:08 -080075 return show_cpumap(1, topology_##name(dev->id), buf); \
Mike Travis23ca4bb2008-05-12 21:21:12 +020076}
Mike Travis23ca4bb2008-05-12 21:21:12 +020077
Mike Travis39106dc2008-04-08 11:43:03 -070078#define define_siblings_show_func(name) \
79 define_siblings_show_map(name); define_siblings_show_list(name)
80
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080081define_id_show_func(physical_package_id);
82define_one_ro(physical_package_id);
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080083
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080084define_id_show_func(core_id);
85define_one_ro(core_id);
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080086
Rusty Russellfbd59a82009-01-10 21:58:08 -080087define_siblings_show_func(thread_cpumask);
88define_one_ro_named(thread_siblings, show_thread_cpumask);
89define_one_ro_named(thread_siblings_list, show_thread_cpumask_list);
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080090
Rusty Russellfbd59a82009-01-10 21:58:08 -080091define_siblings_show_func(core_cpumask);
92define_one_ro_named(core_siblings, show_core_cpumask);
93define_one_ro_named(core_siblings_list, show_core_cpumask_list);
Zhang, Yanmin69dcc992006-02-03 03:04:36 -080094
Heiko Carstensb40d8ed2010-08-31 10:28:17 +020095#ifdef CONFIG_SCHED_BOOK
96define_id_show_func(book_id);
97define_one_ro(book_id);
98define_siblings_show_func(book_cpumask);
99define_one_ro_named(book_siblings, show_book_cpumask);
100define_one_ro_named(book_siblings_list, show_book_cpumask_list);
101#endif
102
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800103static struct attribute *default_attrs[] = {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800104 &dev_attr_physical_package_id.attr,
105 &dev_attr_core_id.attr,
106 &dev_attr_thread_siblings.attr,
107 &dev_attr_thread_siblings_list.attr,
108 &dev_attr_core_siblings.attr,
109 &dev_attr_core_siblings_list.attr,
Heiko Carstensb40d8ed2010-08-31 10:28:17 +0200110#ifdef CONFIG_SCHED_BOOK
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800111 &dev_attr_book_id.attr,
112 &dev_attr_book_siblings.attr,
113 &dev_attr_book_siblings_list.attr,
Heiko Carstensb40d8ed2010-08-31 10:28:17 +0200114#endif
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800115 NULL
116};
117
118static struct attribute_group topology_attr_group = {
119 .attrs = default_attrs,
120 .name = "topology"
121};
122
123/* Add/Remove cpu_topology interface for CPU device */
Paul Gortmakera83048e2013-06-19 15:22:41 -0400124static int topology_add_dev(unsigned int cpu)
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800125{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800126 struct device *dev = get_cpu_device(cpu);
Heiko Carstens06a4bca2006-11-08 19:46:09 -0800127
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800128 return sysfs_create_group(&dev->kobj, &topology_attr_group);
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800129}
130
Paul Gortmakera83048e2013-06-19 15:22:41 -0400131static void topology_remove_dev(unsigned int cpu)
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800132{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800133 struct device *dev = get_cpu_device(cpu);
Heiko Carstens06a4bca2006-11-08 19:46:09 -0800134
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800135 sysfs_remove_group(&dev->kobj, &topology_attr_group);
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800136}
137
Paul Gortmakera83048e2013-06-19 15:22:41 -0400138static int topology_cpu_callback(struct notifier_block *nfb,
139 unsigned long action, void *hcpu)
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800140{
141 unsigned int cpu = (unsigned long)hcpu;
Heiko Carstens06a4bca2006-11-08 19:46:09 -0800142 int rc = 0;
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800143
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800144 switch (action) {
Heiko Carstens06a4bca2006-11-08 19:46:09 -0800145 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700146 case CPU_UP_PREPARE_FROZEN:
Heiko Carstens06a4bca2006-11-08 19:46:09 -0800147 rc = topology_add_dev(cpu);
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800148 break;
Heiko Carstens06a4bca2006-11-08 19:46:09 -0800149 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700150 case CPU_UP_CANCELED_FROZEN:
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800151 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700152 case CPU_DEAD_FROZEN:
Heiko Carstens06a4bca2006-11-08 19:46:09 -0800153 topology_remove_dev(cpu);
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800154 break;
155 }
Akinobu Mitaad84bb52010-05-26 14:43:31 -0700156 return notifier_from_errno(rc);
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800157}
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800158
Paul Gortmakera83048e2013-06-19 15:22:41 -0400159static int topology_sysfs_init(void)
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800160{
Heiko Carstens06a4bca2006-11-08 19:46:09 -0800161 int cpu;
Srivatsa S. Bhate12b7112014-03-11 02:10:12 +0530162 int rc = 0;
163
164 cpu_notifier_register_begin();
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800165
Heiko Carstens06a4bca2006-11-08 19:46:09 -0800166 for_each_online_cpu(cpu) {
167 rc = topology_add_dev(cpu);
168 if (rc)
Srivatsa S. Bhate12b7112014-03-11 02:10:12 +0530169 goto out;
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800170 }
Srivatsa S. Bhate12b7112014-03-11 02:10:12 +0530171 __hotcpu_notifier(topology_cpu_callback, 0);
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800172
Srivatsa S. Bhate12b7112014-03-11 02:10:12 +0530173out:
174 cpu_notifier_register_done();
175 return rc;
Zhang, Yanmin69dcc992006-02-03 03:04:36 -0800176}
177
178device_initcall(topology_sysfs_init);