| Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright 2011 Tilera Corporation. All Rights Reserved. | 
 | 3 |  * | 
 | 4 |  *   This program is free software; you can redistribute it and/or | 
 | 5 |  *   modify it under the terms of the GNU General Public License | 
 | 6 |  *   as published by the Free Software Foundation, version 2. | 
 | 7 |  * | 
 | 8 |  *   This program is distributed in the hope that it will be useful, but | 
 | 9 |  *   WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 10 |  *   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or | 
 | 11 |  *   NON INFRINGEMENT.  See the GNU General Public License for | 
 | 12 |  *   more details. | 
 | 13 |  * | 
 | 14 |  * /sys entry support. | 
 | 15 |  */ | 
 | 16 |  | 
| Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 17 | #include <linux/device.h> | 
| Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 18 | #include <linux/cpu.h> | 
 | 19 | #include <linux/slab.h> | 
 | 20 | #include <linux/smp.h> | 
| Chris Metcalf | 3989efb | 2011-12-01 11:37:20 -0500 | [diff] [blame] | 21 | #include <linux/stat.h> | 
| Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 22 | #include <hv/hypervisor.h> | 
 | 23 |  | 
 | 24 | /* Return a string queried from the hypervisor, truncated to page size. */ | 
 | 25 | static ssize_t get_hv_confstr(char *page, int query) | 
 | 26 | { | 
 | 27 | 	ssize_t n = hv_confstr(query, (unsigned long)page, PAGE_SIZE - 1); | 
 | 28 | 	n = n < 0 ? 0 : min(n, (ssize_t)PAGE_SIZE - 1) - 1; | 
 | 29 | 	if (n) | 
 | 30 | 		page[n++] = '\n'; | 
 | 31 | 	page[n] = '\0'; | 
 | 32 | 	return n; | 
 | 33 | } | 
 | 34 |  | 
| Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 35 | static ssize_t chip_width_show(struct device *dev, | 
 | 36 | 			       struct device_attribute *attr, | 
| Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 37 | 			       char *page) | 
 | 38 | { | 
 | 39 | 	return sprintf(page, "%u\n", smp_width); | 
 | 40 | } | 
| Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 41 | static DEVICE_ATTR(chip_width, 0444, chip_width_show, NULL); | 
| Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 42 |  | 
| Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 43 | static ssize_t chip_height_show(struct device *dev, | 
 | 44 | 				struct device_attribute *attr, | 
| Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 45 | 				char *page) | 
 | 46 | { | 
 | 47 | 	return sprintf(page, "%u\n", smp_height); | 
 | 48 | } | 
| Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 49 | static DEVICE_ATTR(chip_height, 0444, chip_height_show, NULL); | 
| Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 50 |  | 
| Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 51 | static ssize_t chip_serial_show(struct device *dev, | 
 | 52 | 				struct device_attribute *attr, | 
| Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 53 | 				char *page) | 
 | 54 | { | 
 | 55 | 	return get_hv_confstr(page, HV_CONFSTR_CHIP_SERIAL_NUM); | 
 | 56 | } | 
| Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 57 | static DEVICE_ATTR(chip_serial, 0444, chip_serial_show, NULL); | 
| Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 58 |  | 
| Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 59 | static ssize_t chip_revision_show(struct device *dev, | 
 | 60 | 				  struct device_attribute *attr, | 
| Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 61 | 				  char *page) | 
 | 62 | { | 
 | 63 | 	return get_hv_confstr(page, HV_CONFSTR_CHIP_REV); | 
 | 64 | } | 
| Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 65 | static DEVICE_ATTR(chip_revision, 0444, chip_revision_show, NULL); | 
| Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 66 |  | 
 | 67 |  | 
| Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 68 | static ssize_t type_show(struct device *dev, | 
 | 69 | 			    struct device_attribute *attr, | 
| Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 70 | 			    char *page) | 
 | 71 | { | 
 | 72 | 	return sprintf(page, "tilera\n"); | 
 | 73 | } | 
| Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 74 | static DEVICE_ATTR(type, 0444, type_show, NULL); | 
| Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 75 |  | 
 | 76 | #define HV_CONF_ATTR(name, conf)					\ | 
| Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 77 | 	static ssize_t name ## _show(struct device *dev,		\ | 
 | 78 | 				     struct device_attribute *attr, \ | 
| Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 79 | 				     char *page)			\ | 
 | 80 | 	{								\ | 
 | 81 | 		return get_hv_confstr(page, conf);			\ | 
 | 82 | 	}								\ | 
| Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 83 | 	static DEVICE_ATTR(name, 0444, name ## _show, NULL); | 
| Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 84 |  | 
 | 85 | HV_CONF_ATTR(version,		HV_CONFSTR_HV_SW_VER) | 
 | 86 | HV_CONF_ATTR(config_version,	HV_CONFSTR_HV_CONFIG_VER) | 
 | 87 |  | 
 | 88 | HV_CONF_ATTR(board_part,	HV_CONFSTR_BOARD_PART_NUM) | 
 | 89 | HV_CONF_ATTR(board_serial,	HV_CONFSTR_BOARD_SERIAL_NUM) | 
 | 90 | HV_CONF_ATTR(board_revision,	HV_CONFSTR_BOARD_REV) | 
 | 91 | HV_CONF_ATTR(board_description,	HV_CONFSTR_BOARD_DESC) | 
 | 92 | HV_CONF_ATTR(mezz_part,		HV_CONFSTR_MEZZ_PART_NUM) | 
 | 93 | HV_CONF_ATTR(mezz_serial,	HV_CONFSTR_MEZZ_SERIAL_NUM) | 
 | 94 | HV_CONF_ATTR(mezz_revision,	HV_CONFSTR_MEZZ_REV) | 
 | 95 | HV_CONF_ATTR(mezz_description,	HV_CONFSTR_MEZZ_DESC) | 
 | 96 | HV_CONF_ATTR(switch_control,	HV_CONFSTR_SWITCH_CONTROL) | 
 | 97 |  | 
 | 98 | static struct attribute *board_attrs[] = { | 
| Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 99 | 	&dev_attr_board_part.attr, | 
 | 100 | 	&dev_attr_board_serial.attr, | 
 | 101 | 	&dev_attr_board_revision.attr, | 
 | 102 | 	&dev_attr_board_description.attr, | 
 | 103 | 	&dev_attr_mezz_part.attr, | 
 | 104 | 	&dev_attr_mezz_serial.attr, | 
 | 105 | 	&dev_attr_mezz_revision.attr, | 
 | 106 | 	&dev_attr_mezz_description.attr, | 
 | 107 | 	&dev_attr_switch_control.attr, | 
| Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 108 | 	NULL | 
 | 109 | }; | 
 | 110 |  | 
 | 111 | static struct attribute_group board_attr_group = { | 
 | 112 | 	.name   = "board", | 
 | 113 | 	.attrs  = board_attrs, | 
 | 114 | }; | 
 | 115 |  | 
 | 116 |  | 
 | 117 | static struct bin_attribute hvconfig_bin; | 
 | 118 |  | 
 | 119 | static ssize_t | 
 | 120 | hvconfig_bin_read(struct file *filp, struct kobject *kobj, | 
 | 121 | 		  struct bin_attribute *bin_attr, | 
 | 122 | 		  char *buf, loff_t off, size_t count) | 
 | 123 | { | 
 | 124 | 	static size_t size; | 
 | 125 |  | 
 | 126 | 	/* Lazily learn the true size (minus the trailing NUL). */ | 
 | 127 | 	if (size == 0) | 
 | 128 | 		size = hv_confstr(HV_CONFSTR_HV_CONFIG, 0, 0) - 1; | 
 | 129 |  | 
 | 130 | 	/* Check and adjust input parameters. */ | 
 | 131 | 	if (off > size) | 
 | 132 | 		return -EINVAL; | 
 | 133 | 	if (count > size - off) | 
 | 134 | 		count = size - off; | 
 | 135 |  | 
 | 136 | 	if (count) { | 
 | 137 | 		/* Get a copy of the hvc and copy out the relevant portion. */ | 
 | 138 | 		char *hvc; | 
 | 139 |  | 
 | 140 | 		size = off + count; | 
 | 141 | 		hvc = kmalloc(size, GFP_KERNEL); | 
 | 142 | 		if (hvc == NULL) | 
 | 143 | 			return -ENOMEM; | 
 | 144 | 		hv_confstr(HV_CONFSTR_HV_CONFIG, (unsigned long)hvc, size); | 
 | 145 | 		memcpy(buf, hvc + off, count); | 
 | 146 | 		kfree(hvc); | 
 | 147 | 	} | 
 | 148 |  | 
 | 149 | 	return count; | 
 | 150 | } | 
 | 151 |  | 
 | 152 | static int __init create_sysfs_entries(void) | 
 | 153 | { | 
| Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 154 | 	int err = 0; | 
 | 155 |  | 
 | 156 | #define create_cpu_attr(name)						\ | 
 | 157 | 	if (!err)							\ | 
| Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 158 | 		err = device_create_file(cpu_subsys.dev_root, &dev_attr_##name); | 
| Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 159 | 	create_cpu_attr(chip_width); | 
 | 160 | 	create_cpu_attr(chip_height); | 
 | 161 | 	create_cpu_attr(chip_serial); | 
 | 162 | 	create_cpu_attr(chip_revision); | 
 | 163 |  | 
 | 164 | #define create_hv_attr(name)						\ | 
 | 165 | 	if (!err)							\ | 
| Chris Metcalf | 688b4db | 2012-03-12 15:15:21 -0400 | [diff] [blame] | 166 | 		err = sysfs_create_file(hypervisor_kobj, &dev_attr_##name.attr); | 
| Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 167 | 	create_hv_attr(type); | 
 | 168 | 	create_hv_attr(version); | 
 | 169 | 	create_hv_attr(config_version); | 
 | 170 |  | 
 | 171 | 	if (!err) | 
 | 172 | 		err = sysfs_create_group(hypervisor_kobj, &board_attr_group); | 
 | 173 |  | 
 | 174 | 	if (!err) { | 
 | 175 | 		sysfs_bin_attr_init(&hvconfig_bin); | 
 | 176 | 		hvconfig_bin.attr.name = "hvconfig"; | 
 | 177 | 		hvconfig_bin.attr.mode = S_IRUGO; | 
 | 178 | 		hvconfig_bin.read = hvconfig_bin_read; | 
 | 179 | 		hvconfig_bin.size = PAGE_SIZE; | 
 | 180 | 		err = sysfs_create_bin_file(hypervisor_kobj, &hvconfig_bin); | 
 | 181 | 	} | 
 | 182 |  | 
 | 183 | 	return err; | 
 | 184 | } | 
 | 185 | subsys_initcall(create_sysfs_entries); |