Greg Kroah-Hartman | 989d42e | 2017-11-07 17:30:07 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Zhang, Yanmin | 69dcc99 | 2006-02-03 03:04:36 -0800 | [diff] [blame] | 2 | /* |
| 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, Yanmin | 69dcc99 | 2006-02-03 03:04:36 -0800 | [diff] [blame] | 10 | */ |
Zhang, Yanmin | 69dcc99 | 2006-02-03 03:04:36 -0800 | [diff] [blame] | 11 | #include <linux/mm.h> |
| 12 | #include <linux/cpu.h> |
| 13 | #include <linux/module.h> |
David Miller | b13d372 | 2009-01-07 15:30:05 -0800 | [diff] [blame] | 14 | #include <linux/hardirq.h> |
Zhang, Yanmin | 69dcc99 | 2006-02-03 03:04:36 -0800 | [diff] [blame] | 15 | #include <linux/topology.h> |
| 16 | |
Zhang, Yanmin | 69dcc99 | 2006-02-03 03:04:36 -0800 | [diff] [blame] | 17 | #define define_id_show_func(name) \ |
Sudeep Holla | d6ea8d0 | 2014-09-30 14:48:23 +0100 | [diff] [blame] | 18 | static ssize_t name##_show(struct device *dev, \ |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 19 | struct device_attribute *attr, char *buf) \ |
Zhang, Yanmin | 69dcc99 | 2006-02-03 03:04:36 -0800 | [diff] [blame] | 20 | { \ |
Vincent Stehlé | 53974e0 | 2014-04-04 08:43:18 +0200 | [diff] [blame] | 21 | return sprintf(buf, "%d\n", topology_##name(dev->id)); \ |
Zhang, Yanmin | 69dcc99 | 2006-02-03 03:04:36 -0800 | [diff] [blame] | 22 | } |
| 23 | |
Sudeep Holla | d6ea8d0 | 2014-09-30 14:48:23 +0100 | [diff] [blame] | 24 | #define define_siblings_show_map(name, mask) \ |
| 25 | static ssize_t name##_show(struct device *dev, \ |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 26 | struct device_attribute *attr, char *buf) \ |
Mike Travis | 23ca4bb | 2008-05-12 21:21:12 +0200 | [diff] [blame] | 27 | { \ |
Sudeep Holla | d6ea8d0 | 2014-09-30 14:48:23 +0100 | [diff] [blame] | 28 | return cpumap_print_to_pagebuf(false, buf, topology_##mask(dev->id));\ |
Mike Travis | 23ca4bb | 2008-05-12 21:21:12 +0200 | [diff] [blame] | 29 | } |
| 30 | |
Sudeep Holla | d6ea8d0 | 2014-09-30 14:48:23 +0100 | [diff] [blame] | 31 | #define define_siblings_show_list(name, mask) \ |
| 32 | static ssize_t name##_list_show(struct device *dev, \ |
| 33 | struct device_attribute *attr, \ |
| 34 | char *buf) \ |
Mike Travis | 23ca4bb | 2008-05-12 21:21:12 +0200 | [diff] [blame] | 35 | { \ |
Sudeep Holla | d6ea8d0 | 2014-09-30 14:48:23 +0100 | [diff] [blame] | 36 | return cpumap_print_to_pagebuf(true, buf, topology_##mask(dev->id));\ |
Mike Travis | 23ca4bb | 2008-05-12 21:21:12 +0200 | [diff] [blame] | 37 | } |
Mike Travis | 23ca4bb | 2008-05-12 21:21:12 +0200 | [diff] [blame] | 38 | |
Sudeep Holla | d6ea8d0 | 2014-09-30 14:48:23 +0100 | [diff] [blame] | 39 | #define define_siblings_show_func(name, mask) \ |
| 40 | define_siblings_show_map(name, mask); \ |
| 41 | define_siblings_show_list(name, mask) |
Mike Travis | 39106dc | 2008-04-08 11:43:03 -0700 | [diff] [blame] | 42 | |
Zhang, Yanmin | 69dcc99 | 2006-02-03 03:04:36 -0800 | [diff] [blame] | 43 | define_id_show_func(physical_package_id); |
Sudeep Holla | d6ea8d0 | 2014-09-30 14:48:23 +0100 | [diff] [blame] | 44 | static DEVICE_ATTR_RO(physical_package_id); |
Zhang, Yanmin | 69dcc99 | 2006-02-03 03:04:36 -0800 | [diff] [blame] | 45 | |
Zhang, Yanmin | 69dcc99 | 2006-02-03 03:04:36 -0800 | [diff] [blame] | 46 | define_id_show_func(core_id); |
Sudeep Holla | d6ea8d0 | 2014-09-30 14:48:23 +0100 | [diff] [blame] | 47 | static DEVICE_ATTR_RO(core_id); |
Zhang, Yanmin | 69dcc99 | 2006-02-03 03:04:36 -0800 | [diff] [blame] | 48 | |
Bartosz Golaszewski | 06931e6 | 2015-05-26 15:11:28 +0200 | [diff] [blame] | 49 | define_siblings_show_func(thread_siblings, sibling_cpumask); |
Sudeep Holla | d6ea8d0 | 2014-09-30 14:48:23 +0100 | [diff] [blame] | 50 | static DEVICE_ATTR_RO(thread_siblings); |
| 51 | static DEVICE_ATTR_RO(thread_siblings_list); |
Zhang, Yanmin | 69dcc99 | 2006-02-03 03:04:36 -0800 | [diff] [blame] | 52 | |
Sudeep Holla | d6ea8d0 | 2014-09-30 14:48:23 +0100 | [diff] [blame] | 53 | define_siblings_show_func(core_siblings, core_cpumask); |
| 54 | static DEVICE_ATTR_RO(core_siblings); |
| 55 | static DEVICE_ATTR_RO(core_siblings_list); |
Zhang, Yanmin | 69dcc99 | 2006-02-03 03:04:36 -0800 | [diff] [blame] | 56 | |
Heiko Carstens | b40d8ed | 2010-08-31 10:28:17 +0200 | [diff] [blame] | 57 | #ifdef CONFIG_SCHED_BOOK |
| 58 | define_id_show_func(book_id); |
Sudeep Holla | d6ea8d0 | 2014-09-30 14:48:23 +0100 | [diff] [blame] | 59 | static DEVICE_ATTR_RO(book_id); |
| 60 | define_siblings_show_func(book_siblings, book_cpumask); |
| 61 | static DEVICE_ATTR_RO(book_siblings); |
| 62 | static DEVICE_ATTR_RO(book_siblings_list); |
Heiko Carstens | b40d8ed | 2010-08-31 10:28:17 +0200 | [diff] [blame] | 63 | #endif |
| 64 | |
Heiko Carstens | a62247e | 2016-05-21 11:10:13 +0200 | [diff] [blame] | 65 | #ifdef CONFIG_SCHED_DRAWER |
| 66 | define_id_show_func(drawer_id); |
| 67 | static DEVICE_ATTR_RO(drawer_id); |
| 68 | define_siblings_show_func(drawer_siblings, drawer_cpumask); |
| 69 | static DEVICE_ATTR_RO(drawer_siblings); |
| 70 | static DEVICE_ATTR_RO(drawer_siblings_list); |
| 71 | #endif |
| 72 | |
Zhang, Yanmin | 69dcc99 | 2006-02-03 03:04:36 -0800 | [diff] [blame] | 73 | static struct attribute *default_attrs[] = { |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 74 | &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 Carstens | b40d8ed | 2010-08-31 10:28:17 +0200 | [diff] [blame] | 80 | #ifdef CONFIG_SCHED_BOOK |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 81 | &dev_attr_book_id.attr, |
| 82 | &dev_attr_book_siblings.attr, |
| 83 | &dev_attr_book_siblings_list.attr, |
Heiko Carstens | b40d8ed | 2010-08-31 10:28:17 +0200 | [diff] [blame] | 84 | #endif |
Heiko Carstens | a62247e | 2016-05-21 11:10:13 +0200 | [diff] [blame] | 85 | #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, Yanmin | 69dcc99 | 2006-02-03 03:04:36 -0800 | [diff] [blame] | 90 | NULL |
| 91 | }; |
| 92 | |
Arvind Yadav | df44d30 | 2017-08-02 19:36:20 +0530 | [diff] [blame] | 93 | static const struct attribute_group topology_attr_group = { |
Zhang, Yanmin | 69dcc99 | 2006-02-03 03:04:36 -0800 | [diff] [blame] | 94 | .attrs = default_attrs, |
| 95 | .name = "topology" |
| 96 | }; |
| 97 | |
| 98 | /* Add/Remove cpu_topology interface for CPU device */ |
Paul Gortmaker | a83048e | 2013-06-19 15:22:41 -0400 | [diff] [blame] | 99 | static int topology_add_dev(unsigned int cpu) |
Zhang, Yanmin | 69dcc99 | 2006-02-03 03:04:36 -0800 | [diff] [blame] | 100 | { |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 101 | struct device *dev = get_cpu_device(cpu); |
Heiko Carstens | 06a4bca | 2006-11-08 19:46:09 -0800 | [diff] [blame] | 102 | |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 103 | return sysfs_create_group(&dev->kobj, &topology_attr_group); |
Zhang, Yanmin | 69dcc99 | 2006-02-03 03:04:36 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Sebastian Andrzej Siewior | 38643a0 | 2016-11-03 15:50:09 +0100 | [diff] [blame] | 106 | static int topology_remove_dev(unsigned int cpu) |
Zhang, Yanmin | 69dcc99 | 2006-02-03 03:04:36 -0800 | [diff] [blame] | 107 | { |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 108 | struct device *dev = get_cpu_device(cpu); |
Heiko Carstens | 06a4bca | 2006-11-08 19:46:09 -0800 | [diff] [blame] | 109 | |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 110 | sysfs_remove_group(&dev->kobj, &topology_attr_group); |
Sebastian Andrzej Siewior | 38643a0 | 2016-11-03 15:50:09 +0100 | [diff] [blame] | 111 | return 0; |
Zhang, Yanmin | 69dcc99 | 2006-02-03 03:04:36 -0800 | [diff] [blame] | 112 | } |
Zhang, Yanmin | 69dcc99 | 2006-02-03 03:04:36 -0800 | [diff] [blame] | 113 | |
Paul Gortmaker | a83048e | 2013-06-19 15:22:41 -0400 | [diff] [blame] | 114 | static int topology_sysfs_init(void) |
Zhang, Yanmin | 69dcc99 | 2006-02-03 03:04:36 -0800 | [diff] [blame] | 115 | { |
Sebastian Andrzej Siewior | 38643a0 | 2016-11-03 15:50:09 +0100 | [diff] [blame] | 116 | return cpuhp_setup_state(CPUHP_TOPOLOGY_PREPARE, |
| 117 | "base/topology:prepare", topology_add_dev, |
| 118 | topology_remove_dev); |
Zhang, Yanmin | 69dcc99 | 2006-02-03 03:04:36 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | device_initcall(topology_sysfs_init); |