0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/mm.h>
0012 #include <linux/cpu.h>
0013 #include <linux/module.h>
0014 #include <linux/hardirq.h>
0015 #include <linux/topology.h>
0016
0017 #define define_id_show_func(name, fmt) \
0018 static ssize_t name##_show(struct device *dev, \
0019 struct device_attribute *attr, char *buf) \
0020 { \
0021 return sysfs_emit(buf, fmt "\n", topology_##name(dev->id)); \
0022 }
0023
0024 #define define_siblings_read_func(name, mask) \
0025 static ssize_t name##_read(struct file *file, struct kobject *kobj, \
0026 struct bin_attribute *attr, char *buf, \
0027 loff_t off, size_t count) \
0028 { \
0029 struct device *dev = kobj_to_dev(kobj); \
0030 \
0031 return cpumap_print_bitmask_to_buf(buf, topology_##mask(dev->id), \
0032 off, count); \
0033 } \
0034 \
0035 static ssize_t name##_list_read(struct file *file, struct kobject *kobj, \
0036 struct bin_attribute *attr, char *buf, \
0037 loff_t off, size_t count) \
0038 { \
0039 struct device *dev = kobj_to_dev(kobj); \
0040 \
0041 return cpumap_print_list_to_buf(buf, topology_##mask(dev->id), \
0042 off, count); \
0043 }
0044
0045 define_id_show_func(physical_package_id, "%d");
0046 static DEVICE_ATTR_RO(physical_package_id);
0047
0048 #ifdef TOPOLOGY_DIE_SYSFS
0049 define_id_show_func(die_id, "%d");
0050 static DEVICE_ATTR_RO(die_id);
0051 #endif
0052
0053 #ifdef TOPOLOGY_CLUSTER_SYSFS
0054 define_id_show_func(cluster_id, "%d");
0055 static DEVICE_ATTR_RO(cluster_id);
0056 #endif
0057
0058 define_id_show_func(core_id, "%d");
0059 static DEVICE_ATTR_RO(core_id);
0060
0061 define_id_show_func(ppin, "0x%llx");
0062 static DEVICE_ATTR_ADMIN_RO(ppin);
0063
0064 define_siblings_read_func(thread_siblings, sibling_cpumask);
0065 static BIN_ATTR_RO(thread_siblings, CPUMAP_FILE_MAX_BYTES);
0066 static BIN_ATTR_RO(thread_siblings_list, CPULIST_FILE_MAX_BYTES);
0067
0068 define_siblings_read_func(core_cpus, sibling_cpumask);
0069 static BIN_ATTR_RO(core_cpus, CPUMAP_FILE_MAX_BYTES);
0070 static BIN_ATTR_RO(core_cpus_list, CPULIST_FILE_MAX_BYTES);
0071
0072 define_siblings_read_func(core_siblings, core_cpumask);
0073 static BIN_ATTR_RO(core_siblings, CPUMAP_FILE_MAX_BYTES);
0074 static BIN_ATTR_RO(core_siblings_list, CPULIST_FILE_MAX_BYTES);
0075
0076 #ifdef TOPOLOGY_CLUSTER_SYSFS
0077 define_siblings_read_func(cluster_cpus, cluster_cpumask);
0078 static BIN_ATTR_RO(cluster_cpus, CPUMAP_FILE_MAX_BYTES);
0079 static BIN_ATTR_RO(cluster_cpus_list, CPULIST_FILE_MAX_BYTES);
0080 #endif
0081
0082 #ifdef TOPOLOGY_DIE_SYSFS
0083 define_siblings_read_func(die_cpus, die_cpumask);
0084 static BIN_ATTR_RO(die_cpus, CPUMAP_FILE_MAX_BYTES);
0085 static BIN_ATTR_RO(die_cpus_list, CPULIST_FILE_MAX_BYTES);
0086 #endif
0087
0088 define_siblings_read_func(package_cpus, core_cpumask);
0089 static BIN_ATTR_RO(package_cpus, CPUMAP_FILE_MAX_BYTES);
0090 static BIN_ATTR_RO(package_cpus_list, CPULIST_FILE_MAX_BYTES);
0091
0092 #ifdef TOPOLOGY_BOOK_SYSFS
0093 define_id_show_func(book_id, "%d");
0094 static DEVICE_ATTR_RO(book_id);
0095 define_siblings_read_func(book_siblings, book_cpumask);
0096 static BIN_ATTR_RO(book_siblings, CPUMAP_FILE_MAX_BYTES);
0097 static BIN_ATTR_RO(book_siblings_list, CPULIST_FILE_MAX_BYTES);
0098 #endif
0099
0100 #ifdef TOPOLOGY_DRAWER_SYSFS
0101 define_id_show_func(drawer_id, "%d");
0102 static DEVICE_ATTR_RO(drawer_id);
0103 define_siblings_read_func(drawer_siblings, drawer_cpumask);
0104 static BIN_ATTR_RO(drawer_siblings, CPUMAP_FILE_MAX_BYTES);
0105 static BIN_ATTR_RO(drawer_siblings_list, CPULIST_FILE_MAX_BYTES);
0106 #endif
0107
0108 static struct bin_attribute *bin_attrs[] = {
0109 &bin_attr_core_cpus,
0110 &bin_attr_core_cpus_list,
0111 &bin_attr_thread_siblings,
0112 &bin_attr_thread_siblings_list,
0113 &bin_attr_core_siblings,
0114 &bin_attr_core_siblings_list,
0115 #ifdef TOPOLOGY_CLUSTER_SYSFS
0116 &bin_attr_cluster_cpus,
0117 &bin_attr_cluster_cpus_list,
0118 #endif
0119 #ifdef TOPOLOGY_DIE_SYSFS
0120 &bin_attr_die_cpus,
0121 &bin_attr_die_cpus_list,
0122 #endif
0123 &bin_attr_package_cpus,
0124 &bin_attr_package_cpus_list,
0125 #ifdef TOPOLOGY_BOOK_SYSFS
0126 &bin_attr_book_siblings,
0127 &bin_attr_book_siblings_list,
0128 #endif
0129 #ifdef TOPOLOGY_DRAWER_SYSFS
0130 &bin_attr_drawer_siblings,
0131 &bin_attr_drawer_siblings_list,
0132 #endif
0133 NULL
0134 };
0135
0136 static struct attribute *default_attrs[] = {
0137 &dev_attr_physical_package_id.attr,
0138 #ifdef TOPOLOGY_DIE_SYSFS
0139 &dev_attr_die_id.attr,
0140 #endif
0141 #ifdef TOPOLOGY_CLUSTER_SYSFS
0142 &dev_attr_cluster_id.attr,
0143 #endif
0144 &dev_attr_core_id.attr,
0145 #ifdef TOPOLOGY_BOOK_SYSFS
0146 &dev_attr_book_id.attr,
0147 #endif
0148 #ifdef TOPOLOGY_DRAWER_SYSFS
0149 &dev_attr_drawer_id.attr,
0150 #endif
0151 &dev_attr_ppin.attr,
0152 NULL
0153 };
0154
0155 static umode_t topology_is_visible(struct kobject *kobj,
0156 struct attribute *attr, int unused)
0157 {
0158 if (attr == &dev_attr_ppin.attr && !topology_ppin(kobj_to_dev(kobj)->id))
0159 return 0;
0160
0161 return attr->mode;
0162 }
0163
0164 static const struct attribute_group topology_attr_group = {
0165 .attrs = default_attrs,
0166 .bin_attrs = bin_attrs,
0167 .is_visible = topology_is_visible,
0168 .name = "topology"
0169 };
0170
0171
0172 static int topology_add_dev(unsigned int cpu)
0173 {
0174 struct device *dev = get_cpu_device(cpu);
0175
0176 return sysfs_create_group(&dev->kobj, &topology_attr_group);
0177 }
0178
0179 static int topology_remove_dev(unsigned int cpu)
0180 {
0181 struct device *dev = get_cpu_device(cpu);
0182
0183 sysfs_remove_group(&dev->kobj, &topology_attr_group);
0184 return 0;
0185 }
0186
0187 static int __init topology_sysfs_init(void)
0188 {
0189 return cpuhp_setup_state(CPUHP_TOPOLOGY_PREPARE,
0190 "base/topology:prepare", topology_add_dev,
0191 topology_remove_dev);
0192 }
0193
0194 device_initcall(topology_sysfs_init);