1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2020 Linaro Limited, All rights reserved.
4  * Author: Mike Leach <mike.leach@linaro.org>
5  */
6 
7 #include <linux/configfs.h>
8 
9 #include "coresight-syscfg-configfs.h"
10 
11 /* create a default ci_type. */
12 static inline struct config_item_type *cscfg_create_ci_type(void)
13 {
14 	struct config_item_type *ci_type;
15 
16 	ci_type = devm_kzalloc(cscfg_device(), sizeof(*ci_type), GFP_KERNEL);
17 	if (ci_type)
18 		ci_type->ct_owner = THIS_MODULE;
19 
20 	return ci_type;
21 }
22 
23 /* configurations sub-group */
24 
25 /* attributes for the config view group */
26 static ssize_t cscfg_cfg_description_show(struct config_item *item, char *page)
27 {
28 	struct cscfg_fs_config *fs_config = container_of(to_config_group(item),
29 							 struct cscfg_fs_config, group);
30 
31 	return scnprintf(page, PAGE_SIZE, "%s", fs_config->config_desc->description);
32 }
33 CONFIGFS_ATTR_RO(cscfg_cfg_, description);
34 
35 static ssize_t cscfg_cfg_feature_refs_show(struct config_item *item, char *page)
36 {
37 	struct cscfg_fs_config *fs_config = container_of(to_config_group(item),
38 							 struct cscfg_fs_config, group);
39 	const struct cscfg_config_desc *config_desc = fs_config->config_desc;
40 	ssize_t ch_used = 0;
41 	int i;
42 
43 	for (i = 0; i < config_desc->nr_feat_refs; i++)
44 		ch_used += scnprintf(page + ch_used, PAGE_SIZE - ch_used,
45 				     "%s\n", config_desc->feat_ref_names[i]);
46 	return ch_used;
47 }
48 CONFIGFS_ATTR_RO(cscfg_cfg_, feature_refs);
49 
50 /* list preset values in order of features and params */
51 static ssize_t cscfg_cfg_values_show(struct config_item *item, char *page)
52 {
53 	const struct cscfg_feature_desc *feat_desc;
54 	const struct cscfg_config_desc *config_desc;
55 	struct cscfg_fs_preset *fs_preset;
56 	int i, j, val_idx, preset_idx;
57 	ssize_t used = 0;
58 
59 	fs_preset = container_of(to_config_group(item), struct cscfg_fs_preset, group);
60 	config_desc = fs_preset->config_desc;
61 
62 	if (!config_desc->nr_presets)
63 		return 0;
64 
65 	preset_idx = fs_preset->preset_num - 1;
66 
67 	/* start index on the correct array line */
68 	val_idx = config_desc->nr_total_params * preset_idx;
69 
70 	/*
71 	 * A set of presets is the sum of all params in used features,
72 	 * in order of declaration of features and params in the features
73 	 */
74 	for (i = 0; i < config_desc->nr_feat_refs; i++) {
75 		feat_desc = cscfg_get_named_feat_desc(config_desc->feat_ref_names[i]);
76 		for (j = 0; j < feat_desc->nr_params; j++) {
77 			used += scnprintf(page + used, PAGE_SIZE - used,
78 					  "%s.%s = 0x%llx ",
79 					  feat_desc->name,
80 					  feat_desc->params_desc[j].name,
81 					  config_desc->presets[val_idx++]);
82 		}
83 	}
84 	used += scnprintf(page + used, PAGE_SIZE - used, "\n");
85 
86 	return used;
87 }
88 CONFIGFS_ATTR_RO(cscfg_cfg_, values);
89 
90 static struct configfs_attribute *cscfg_config_view_attrs[] = {
91 	&cscfg_cfg_attr_description,
92 	&cscfg_cfg_attr_feature_refs,
93 	NULL,
94 };
95 
96 static struct config_item_type cscfg_config_view_type = {
97 	.ct_owner = THIS_MODULE,
98 	.ct_attrs = cscfg_config_view_attrs,
99 };
100 
101 static struct configfs_attribute *cscfg_config_preset_attrs[] = {
102 	&cscfg_cfg_attr_values,
103 	NULL,
104 };
105 
106 static struct config_item_type cscfg_config_preset_type = {
107 	.ct_owner = THIS_MODULE,
108 	.ct_attrs = cscfg_config_preset_attrs,
109 };
110 
111 static int cscfg_add_preset_groups(struct cscfg_fs_config *cfg_view)
112 {
113 	int preset_num;
114 	struct cscfg_fs_preset *cfg_fs_preset;
115 	struct cscfg_config_desc *config_desc = cfg_view->config_desc;
116 	char name[CONFIGFS_ITEM_NAME_LEN];
117 
118 	if (!config_desc->nr_presets)
119 		return 0;
120 
121 	for (preset_num = 1; preset_num <= config_desc->nr_presets; preset_num++) {
122 		cfg_fs_preset = devm_kzalloc(cscfg_device(),
123 					     sizeof(struct cscfg_fs_preset), GFP_KERNEL);
124 
125 		if (!cfg_fs_preset)
126 			return -ENOMEM;
127 
128 		snprintf(name, CONFIGFS_ITEM_NAME_LEN, "preset%d", preset_num);
129 		cfg_fs_preset->preset_num = preset_num;
130 		cfg_fs_preset->config_desc = cfg_view->config_desc;
131 		config_group_init_type_name(&cfg_fs_preset->group, name,
132 					    &cscfg_config_preset_type);
133 		configfs_add_default_group(&cfg_fs_preset->group, &cfg_view->group);
134 	}
135 	return 0;
136 }
137 
138 static struct config_group *cscfg_create_config_group(struct cscfg_config_desc *config_desc)
139 {
140 	struct cscfg_fs_config *cfg_view;
141 	struct device *dev = cscfg_device();
142 	int err;
143 
144 	if (!dev)
145 		return ERR_PTR(-EINVAL);
146 
147 	cfg_view = devm_kzalloc(dev, sizeof(struct cscfg_fs_config), GFP_KERNEL);
148 	if (!cfg_view)
149 		return ERR_PTR(-ENOMEM);
150 
151 	cfg_view->config_desc = config_desc;
152 	config_group_init_type_name(&cfg_view->group, config_desc->name, &cscfg_config_view_type);
153 
154 	/* add in a preset<n> dir for each preset */
155 	err = cscfg_add_preset_groups(cfg_view);
156 	if (err)
157 		return ERR_PTR(err);
158 
159 	return &cfg_view->group;
160 }
161 
162 /* attributes for features view */
163 
164 static ssize_t cscfg_feat_description_show(struct config_item *item, char *page)
165 {
166 	struct cscfg_fs_feature *fs_feat = container_of(to_config_group(item),
167 							struct cscfg_fs_feature, group);
168 
169 	return scnprintf(page, PAGE_SIZE, "%s", fs_feat->feat_desc->description);
170 }
171 CONFIGFS_ATTR_RO(cscfg_feat_, description);
172 
173 static ssize_t cscfg_feat_matches_show(struct config_item *item, char *page)
174 {
175 	struct cscfg_fs_feature *fs_feat = container_of(to_config_group(item),
176 							struct cscfg_fs_feature, group);
177 	u32 match_flags = fs_feat->feat_desc->match_flags;
178 	int used = 0;
179 
180 	if (match_flags & CS_CFG_MATCH_CLASS_SRC_ALL)
181 		used = scnprintf(page, PAGE_SIZE, "SRC_ALL ");
182 
183 	if (match_flags & CS_CFG_MATCH_CLASS_SRC_ETM4)
184 		used += scnprintf(page + used, PAGE_SIZE - used, "SRC_ETMV4 ");
185 
186 	used += scnprintf(page + used, PAGE_SIZE - used, "\n");
187 	return used;
188 }
189 CONFIGFS_ATTR_RO(cscfg_feat_, matches);
190 
191 static ssize_t cscfg_feat_nr_params_show(struct config_item *item, char *page)
192 {
193 	struct cscfg_fs_feature *fs_feat = container_of(to_config_group(item),
194 							struct cscfg_fs_feature, group);
195 
196 	return scnprintf(page, PAGE_SIZE, "%d\n", fs_feat->feat_desc->nr_params);
197 }
198 CONFIGFS_ATTR_RO(cscfg_feat_, nr_params);
199 
200 /* base feature desc attrib structures */
201 static struct configfs_attribute *cscfg_feature_view_attrs[] = {
202 	&cscfg_feat_attr_description,
203 	&cscfg_feat_attr_matches,
204 	&cscfg_feat_attr_nr_params,
205 	NULL,
206 };
207 
208 static struct config_item_type cscfg_feature_view_type = {
209 	.ct_owner = THIS_MODULE,
210 	.ct_attrs = cscfg_feature_view_attrs,
211 };
212 
213 static ssize_t cscfg_param_value_show(struct config_item *item, char *page)
214 {
215 	struct cscfg_fs_param *param_item = container_of(to_config_group(item),
216 							 struct cscfg_fs_param, group);
217 	u64 value = param_item->feat_desc->params_desc[param_item->param_idx].value;
218 
219 	return scnprintf(page, PAGE_SIZE, "0x%llx\n", value);
220 }
221 
222 static ssize_t cscfg_param_value_store(struct config_item *item,
223 				       const char *page, size_t size)
224 {
225 	struct cscfg_fs_param *param_item = container_of(to_config_group(item),
226 							 struct cscfg_fs_param, group);
227 	struct cscfg_feature_desc *feat_desc = param_item->feat_desc;
228 	int param_idx = param_item->param_idx;
229 	u64 value;
230 	int err;
231 
232 	err = kstrtoull(page, 0, &value);
233 	if (!err)
234 		err = cscfg_update_feat_param_val(feat_desc, param_idx, value);
235 
236 	return err ? err : size;
237 }
238 CONFIGFS_ATTR(cscfg_param_, value);
239 
240 static struct configfs_attribute *cscfg_param_view_attrs[] = {
241 	&cscfg_param_attr_value,
242 	NULL,
243 };
244 
245 static struct config_item_type cscfg_param_view_type = {
246 	.ct_owner = THIS_MODULE,
247 	.ct_attrs = cscfg_param_view_attrs,
248 };
249 
250 /*
251  * configfs has far less functionality provided to add attributes dynamically than sysfs,
252  * and the show and store fns pass the enclosing config_item so the actual attribute cannot
253  * be determined. Therefore we add each item as a group directory, with a value attribute.
254  */
255 static int cscfg_create_params_group_items(struct cscfg_feature_desc *feat_desc,
256 					   struct config_group *params_group)
257 {
258 	struct device *dev = cscfg_device();
259 	struct cscfg_fs_param *param_item;
260 	int i;
261 
262 	/* parameter items - as groups with default_value attribute */
263 	for (i = 0; i < feat_desc->nr_params; i++) {
264 		param_item = devm_kzalloc(dev, sizeof(struct cscfg_fs_param), GFP_KERNEL);
265 		if (!param_item)
266 			return -ENOMEM;
267 		param_item->feat_desc = feat_desc;
268 		param_item->param_idx = i;
269 		config_group_init_type_name(&param_item->group,
270 					    feat_desc->params_desc[i].name,
271 					    &cscfg_param_view_type);
272 		configfs_add_default_group(&param_item->group, params_group);
273 	}
274 	return 0;
275 }
276 
277 static struct config_group *cscfg_create_feature_group(struct cscfg_feature_desc *feat_desc)
278 {
279 	struct cscfg_fs_feature *feat_view;
280 	struct config_item_type *params_group_type;
281 	struct config_group *params_group = NULL;
282 	struct device *dev = cscfg_device();
283 	int item_err;
284 
285 	if (!dev)
286 		return ERR_PTR(-EINVAL);
287 
288 	feat_view = devm_kzalloc(dev, sizeof(struct cscfg_fs_feature), GFP_KERNEL);
289 	if (!feat_view)
290 		return ERR_PTR(-ENOMEM);
291 
292 	if (feat_desc->nr_params) {
293 		params_group = devm_kzalloc(dev, sizeof(struct config_group), GFP_KERNEL);
294 		if (!params_group)
295 			return ERR_PTR(-ENOMEM);
296 
297 		params_group_type = cscfg_create_ci_type();
298 		if (!params_group_type)
299 			return ERR_PTR(-ENOMEM);
300 	}
301 
302 	feat_view->feat_desc = feat_desc;
303 	config_group_init_type_name(&feat_view->group,
304 				    feat_desc->name,
305 				    &cscfg_feature_view_type);
306 	if (params_group) {
307 		config_group_init_type_name(params_group, "params", params_group_type);
308 		configfs_add_default_group(params_group, &feat_view->group);
309 		item_err = cscfg_create_params_group_items(feat_desc, params_group);
310 		if (item_err)
311 			return ERR_PTR(item_err);
312 	}
313 	return &feat_view->group;
314 }
315 
316 static struct config_item_type cscfg_configs_type = {
317 	.ct_owner = THIS_MODULE,
318 };
319 
320 static struct config_group cscfg_configs_grp = {
321 	.cg_item = {
322 		.ci_namebuf = "configurations",
323 		.ci_type = &cscfg_configs_type,
324 	},
325 };
326 
327 /* add configuration to configurations group */
328 int cscfg_configfs_add_config(struct cscfg_config_desc *config_desc)
329 {
330 	struct config_group *new_group;
331 	int err;
332 
333 	new_group = cscfg_create_config_group(config_desc);
334 	if (IS_ERR(new_group))
335 		return PTR_ERR(new_group);
336 	err =  configfs_register_group(&cscfg_configs_grp, new_group);
337 	return err;
338 }
339 
340 static struct config_item_type cscfg_features_type = {
341 	.ct_owner = THIS_MODULE,
342 };
343 
344 static struct config_group cscfg_features_grp = {
345 	.cg_item = {
346 		.ci_namebuf = "features",
347 		.ci_type = &cscfg_features_type,
348 	},
349 };
350 
351 /* add feature to features group */
352 int cscfg_configfs_add_feature(struct cscfg_feature_desc *feat_desc)
353 {
354 	struct config_group *new_group;
355 	int err;
356 
357 	new_group = cscfg_create_feature_group(feat_desc);
358 	if (IS_ERR(new_group))
359 		return PTR_ERR(new_group);
360 	err =  configfs_register_group(&cscfg_features_grp, new_group);
361 	return err;
362 }
363 
364 int cscfg_configfs_init(struct cscfg_manager *cscfg_mgr)
365 {
366 	struct configfs_subsystem *subsys;
367 	struct config_item_type *ci_type;
368 
369 	if (!cscfg_mgr)
370 		return -EINVAL;
371 
372 	ci_type = cscfg_create_ci_type();
373 	if (!ci_type)
374 		return -ENOMEM;
375 
376 	subsys = &cscfg_mgr->cfgfs_subsys;
377 	config_item_set_name(&subsys->su_group.cg_item, CSCFG_FS_SUBSYS_NAME);
378 	subsys->su_group.cg_item.ci_type = ci_type;
379 
380 	config_group_init(&subsys->su_group);
381 	mutex_init(&subsys->su_mutex);
382 
383 	/* Add default groups to subsystem */
384 	config_group_init(&cscfg_configs_grp);
385 	configfs_add_default_group(&cscfg_configs_grp, &subsys->su_group);
386 
387 	config_group_init(&cscfg_features_grp);
388 	configfs_add_default_group(&cscfg_features_grp, &subsys->su_group);
389 
390 	return configfs_register_subsystem(subsys);
391 }
392 
393 void cscfg_configfs_release(struct cscfg_manager *cscfg_mgr)
394 {
395 	configfs_unregister_subsystem(&cscfg_mgr->cfgfs_subsys);
396 }
397