xref: /openbmc/linux/drivers/hwtracing/stm/core.c (revision 8e8e69d6)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * System Trace Module (STM) infrastructure
4  * Copyright (c) 2014, Intel Corporation.
5  *
6  * STM class implements generic infrastructure for  System Trace Module devices
7  * as defined in MIPI STPv2 specification.
8  */
9 
10 #include <linux/pm_runtime.h>
11 #include <linux/uaccess.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/device.h>
15 #include <linux/compat.h>
16 #include <linux/kdev_t.h>
17 #include <linux/srcu.h>
18 #include <linux/slab.h>
19 #include <linux/stm.h>
20 #include <linux/fs.h>
21 #include <linux/mm.h>
22 #include <linux/vmalloc.h>
23 #include "stm.h"
24 
25 #include <uapi/linux/stm.h>
26 
27 static unsigned int stm_core_up;
28 
29 /*
30  * The SRCU here makes sure that STM device doesn't disappear from under a
31  * stm_source_write() caller, which may want to have as little overhead as
32  * possible.
33  */
34 static struct srcu_struct stm_source_srcu;
35 
36 static ssize_t masters_show(struct device *dev,
37 			    struct device_attribute *attr,
38 			    char *buf)
39 {
40 	struct stm_device *stm = to_stm_device(dev);
41 	int ret;
42 
43 	ret = sprintf(buf, "%u %u\n", stm->data->sw_start, stm->data->sw_end);
44 
45 	return ret;
46 }
47 
48 static DEVICE_ATTR_RO(masters);
49 
50 static ssize_t channels_show(struct device *dev,
51 			     struct device_attribute *attr,
52 			     char *buf)
53 {
54 	struct stm_device *stm = to_stm_device(dev);
55 	int ret;
56 
57 	ret = sprintf(buf, "%u\n", stm->data->sw_nchannels);
58 
59 	return ret;
60 }
61 
62 static DEVICE_ATTR_RO(channels);
63 
64 static ssize_t hw_override_show(struct device *dev,
65 				struct device_attribute *attr,
66 				char *buf)
67 {
68 	struct stm_device *stm = to_stm_device(dev);
69 	int ret;
70 
71 	ret = sprintf(buf, "%u\n", stm->data->hw_override);
72 
73 	return ret;
74 }
75 
76 static DEVICE_ATTR_RO(hw_override);
77 
78 static struct attribute *stm_attrs[] = {
79 	&dev_attr_masters.attr,
80 	&dev_attr_channels.attr,
81 	&dev_attr_hw_override.attr,
82 	NULL,
83 };
84 
85 ATTRIBUTE_GROUPS(stm);
86 
87 static struct class stm_class = {
88 	.name		= "stm",
89 	.dev_groups	= stm_groups,
90 };
91 
92 static int stm_dev_match(struct device *dev, const void *data)
93 {
94 	const char *name = data;
95 
96 	return sysfs_streq(name, dev_name(dev));
97 }
98 
99 /**
100  * stm_find_device() - find stm device by name
101  * @buf:	character buffer containing the name
102  *
103  * This is called when either policy gets assigned to an stm device or an
104  * stm_source device gets linked to an stm device.
105  *
106  * This grabs device's reference (get_device()) and module reference, both
107  * of which the calling path needs to make sure to drop with stm_put_device().
108  *
109  * Return:	stm device pointer or null if lookup failed.
110  */
111 struct stm_device *stm_find_device(const char *buf)
112 {
113 	struct stm_device *stm;
114 	struct device *dev;
115 
116 	if (!stm_core_up)
117 		return NULL;
118 
119 	dev = class_find_device(&stm_class, NULL, buf, stm_dev_match);
120 	if (!dev)
121 		return NULL;
122 
123 	stm = to_stm_device(dev);
124 	if (!try_module_get(stm->owner)) {
125 		/* matches class_find_device() above */
126 		put_device(dev);
127 		return NULL;
128 	}
129 
130 	return stm;
131 }
132 
133 /**
134  * stm_put_device() - drop references on the stm device
135  * @stm:	stm device, previously acquired by stm_find_device()
136  *
137  * This drops the module reference and device reference taken by
138  * stm_find_device() or stm_char_open().
139  */
140 void stm_put_device(struct stm_device *stm)
141 {
142 	module_put(stm->owner);
143 	put_device(&stm->dev);
144 }
145 
146 /*
147  * Internally we only care about software-writable masters here, that is the
148  * ones in the range [stm_data->sw_start..stm_data..sw_end], however we need
149  * original master numbers to be visible externally, since they are the ones
150  * that will appear in the STP stream. Thus, the internal bookkeeping uses
151  * $master - stm_data->sw_start to reference master descriptors and such.
152  */
153 
154 #define __stm_master(_s, _m)				\
155 	((_s)->masters[(_m) - (_s)->data->sw_start])
156 
157 static inline struct stp_master *
158 stm_master(struct stm_device *stm, unsigned int idx)
159 {
160 	if (idx < stm->data->sw_start || idx > stm->data->sw_end)
161 		return NULL;
162 
163 	return __stm_master(stm, idx);
164 }
165 
166 static int stp_master_alloc(struct stm_device *stm, unsigned int idx)
167 {
168 	struct stp_master *master;
169 
170 	master = kzalloc(struct_size(master, chan_map,
171 				     BITS_TO_LONGS(stm->data->sw_nchannels)),
172 			 GFP_ATOMIC);
173 	if (!master)
174 		return -ENOMEM;
175 
176 	master->nr_free = stm->data->sw_nchannels;
177 	__stm_master(stm, idx) = master;
178 
179 	return 0;
180 }
181 
182 static void stp_master_free(struct stm_device *stm, unsigned int idx)
183 {
184 	struct stp_master *master = stm_master(stm, idx);
185 
186 	if (!master)
187 		return;
188 
189 	__stm_master(stm, idx) = NULL;
190 	kfree(master);
191 }
192 
193 static void stm_output_claim(struct stm_device *stm, struct stm_output *output)
194 {
195 	struct stp_master *master = stm_master(stm, output->master);
196 
197 	lockdep_assert_held(&stm->mc_lock);
198 	lockdep_assert_held(&output->lock);
199 
200 	if (WARN_ON_ONCE(master->nr_free < output->nr_chans))
201 		return;
202 
203 	bitmap_allocate_region(&master->chan_map[0], output->channel,
204 			       ilog2(output->nr_chans));
205 
206 	master->nr_free -= output->nr_chans;
207 }
208 
209 static void
210 stm_output_disclaim(struct stm_device *stm, struct stm_output *output)
211 {
212 	struct stp_master *master = stm_master(stm, output->master);
213 
214 	lockdep_assert_held(&stm->mc_lock);
215 	lockdep_assert_held(&output->lock);
216 
217 	bitmap_release_region(&master->chan_map[0], output->channel,
218 			      ilog2(output->nr_chans));
219 
220 	master->nr_free += output->nr_chans;
221 	output->nr_chans = 0;
222 }
223 
224 /*
225  * This is like bitmap_find_free_region(), except it can ignore @start bits
226  * at the beginning.
227  */
228 static int find_free_channels(unsigned long *bitmap, unsigned int start,
229 			      unsigned int end, unsigned int width)
230 {
231 	unsigned int pos;
232 	int i;
233 
234 	for (pos = start; pos < end + 1; pos = ALIGN(pos, width)) {
235 		pos = find_next_zero_bit(bitmap, end + 1, pos);
236 		if (pos + width > end + 1)
237 			break;
238 
239 		if (pos & (width - 1))
240 			continue;
241 
242 		for (i = 1; i < width && !test_bit(pos + i, bitmap); i++)
243 			;
244 		if (i == width)
245 			return pos;
246 
247 		/* step over [pos..pos+i) to continue search */
248 		pos += i;
249 	}
250 
251 	return -1;
252 }
253 
254 static int
255 stm_find_master_chan(struct stm_device *stm, unsigned int width,
256 		     unsigned int *mstart, unsigned int mend,
257 		     unsigned int *cstart, unsigned int cend)
258 {
259 	struct stp_master *master;
260 	unsigned int midx;
261 	int pos, err;
262 
263 	for (midx = *mstart; midx <= mend; midx++) {
264 		if (!stm_master(stm, midx)) {
265 			err = stp_master_alloc(stm, midx);
266 			if (err)
267 				return err;
268 		}
269 
270 		master = stm_master(stm, midx);
271 
272 		if (!master->nr_free)
273 			continue;
274 
275 		pos = find_free_channels(master->chan_map, *cstart, cend,
276 					 width);
277 		if (pos < 0)
278 			continue;
279 
280 		*mstart = midx;
281 		*cstart = pos;
282 		return 0;
283 	}
284 
285 	return -ENOSPC;
286 }
287 
288 static int stm_output_assign(struct stm_device *stm, unsigned int width,
289 			     struct stp_policy_node *policy_node,
290 			     struct stm_output *output)
291 {
292 	unsigned int midx, cidx, mend, cend;
293 	int ret = -EINVAL;
294 
295 	if (width > stm->data->sw_nchannels)
296 		return -EINVAL;
297 
298 	/* We no longer accept policy_node==NULL here */
299 	if (WARN_ON_ONCE(!policy_node))
300 		return -EINVAL;
301 
302 	/*
303 	 * Also, the caller holds reference to policy_node, so it won't
304 	 * disappear on us.
305 	 */
306 	stp_policy_node_get_ranges(policy_node, &midx, &mend, &cidx, &cend);
307 
308 	spin_lock(&stm->mc_lock);
309 	spin_lock(&output->lock);
310 	/* output is already assigned -- shouldn't happen */
311 	if (WARN_ON_ONCE(output->nr_chans))
312 		goto unlock;
313 
314 	ret = stm_find_master_chan(stm, width, &midx, mend, &cidx, cend);
315 	if (ret < 0)
316 		goto unlock;
317 
318 	output->master = midx;
319 	output->channel = cidx;
320 	output->nr_chans = width;
321 	if (stm->pdrv->output_open) {
322 		void *priv = stp_policy_node_priv(policy_node);
323 
324 		if (WARN_ON_ONCE(!priv))
325 			goto unlock;
326 
327 		/* configfs subsys mutex is held by the caller */
328 		ret = stm->pdrv->output_open(priv, output);
329 		if (ret)
330 			goto unlock;
331 	}
332 
333 	stm_output_claim(stm, output);
334 	dev_dbg(&stm->dev, "assigned %u:%u (+%u)\n", midx, cidx, width);
335 
336 	ret = 0;
337 unlock:
338 	if (ret)
339 		output->nr_chans = 0;
340 
341 	spin_unlock(&output->lock);
342 	spin_unlock(&stm->mc_lock);
343 
344 	return ret;
345 }
346 
347 static void stm_output_free(struct stm_device *stm, struct stm_output *output)
348 {
349 	spin_lock(&stm->mc_lock);
350 	spin_lock(&output->lock);
351 	if (output->nr_chans)
352 		stm_output_disclaim(stm, output);
353 	if (stm->pdrv && stm->pdrv->output_close)
354 		stm->pdrv->output_close(output);
355 	spin_unlock(&output->lock);
356 	spin_unlock(&stm->mc_lock);
357 }
358 
359 static void stm_output_init(struct stm_output *output)
360 {
361 	spin_lock_init(&output->lock);
362 }
363 
364 static int major_match(struct device *dev, const void *data)
365 {
366 	unsigned int major = *(unsigned int *)data;
367 
368 	return MAJOR(dev->devt) == major;
369 }
370 
371 /*
372  * Framing protocol management
373  * Modules can implement STM protocol drivers and (un-)register them
374  * with the STM class framework.
375  */
376 static struct list_head stm_pdrv_head;
377 static struct mutex stm_pdrv_mutex;
378 
379 struct stm_pdrv_entry {
380 	struct list_head			entry;
381 	const struct stm_protocol_driver	*pdrv;
382 	const struct config_item_type		*node_type;
383 };
384 
385 static const struct stm_pdrv_entry *
386 __stm_lookup_protocol(const char *name)
387 {
388 	struct stm_pdrv_entry *pe;
389 
390 	/*
391 	 * If no name is given (NULL or ""), fall back to "p_basic".
392 	 */
393 	if (!name || !*name)
394 		name = "p_basic";
395 
396 	list_for_each_entry(pe, &stm_pdrv_head, entry) {
397 		if (!strcmp(name, pe->pdrv->name))
398 			return pe;
399 	}
400 
401 	return NULL;
402 }
403 
404 int stm_register_protocol(const struct stm_protocol_driver *pdrv)
405 {
406 	struct stm_pdrv_entry *pe = NULL;
407 	int ret = -ENOMEM;
408 
409 	mutex_lock(&stm_pdrv_mutex);
410 
411 	if (__stm_lookup_protocol(pdrv->name)) {
412 		ret = -EEXIST;
413 		goto unlock;
414 	}
415 
416 	pe = kzalloc(sizeof(*pe), GFP_KERNEL);
417 	if (!pe)
418 		goto unlock;
419 
420 	if (pdrv->policy_attr) {
421 		pe->node_type = get_policy_node_type(pdrv->policy_attr);
422 		if (!pe->node_type)
423 			goto unlock;
424 	}
425 
426 	list_add_tail(&pe->entry, &stm_pdrv_head);
427 	pe->pdrv = pdrv;
428 
429 	ret = 0;
430 unlock:
431 	mutex_unlock(&stm_pdrv_mutex);
432 
433 	if (ret)
434 		kfree(pe);
435 
436 	return ret;
437 }
438 EXPORT_SYMBOL_GPL(stm_register_protocol);
439 
440 void stm_unregister_protocol(const struct stm_protocol_driver *pdrv)
441 {
442 	struct stm_pdrv_entry *pe, *iter;
443 
444 	mutex_lock(&stm_pdrv_mutex);
445 
446 	list_for_each_entry_safe(pe, iter, &stm_pdrv_head, entry) {
447 		if (pe->pdrv == pdrv) {
448 			list_del(&pe->entry);
449 
450 			if (pe->node_type) {
451 				kfree(pe->node_type->ct_attrs);
452 				kfree(pe->node_type);
453 			}
454 			kfree(pe);
455 			break;
456 		}
457 	}
458 
459 	mutex_unlock(&stm_pdrv_mutex);
460 }
461 EXPORT_SYMBOL_GPL(stm_unregister_protocol);
462 
463 static bool stm_get_protocol(const struct stm_protocol_driver *pdrv)
464 {
465 	return try_module_get(pdrv->owner);
466 }
467 
468 void stm_put_protocol(const struct stm_protocol_driver *pdrv)
469 {
470 	module_put(pdrv->owner);
471 }
472 
473 int stm_lookup_protocol(const char *name,
474 			const struct stm_protocol_driver **pdrv,
475 			const struct config_item_type **node_type)
476 {
477 	const struct stm_pdrv_entry *pe;
478 
479 	mutex_lock(&stm_pdrv_mutex);
480 
481 	pe = __stm_lookup_protocol(name);
482 	if (pe && pe->pdrv && stm_get_protocol(pe->pdrv)) {
483 		*pdrv = pe->pdrv;
484 		*node_type = pe->node_type;
485 	}
486 
487 	mutex_unlock(&stm_pdrv_mutex);
488 
489 	return pe ? 0 : -ENOENT;
490 }
491 
492 static int stm_char_open(struct inode *inode, struct file *file)
493 {
494 	struct stm_file *stmf;
495 	struct device *dev;
496 	unsigned int major = imajor(inode);
497 	int err = -ENOMEM;
498 
499 	dev = class_find_device(&stm_class, NULL, &major, major_match);
500 	if (!dev)
501 		return -ENODEV;
502 
503 	stmf = kzalloc(sizeof(*stmf), GFP_KERNEL);
504 	if (!stmf)
505 		goto err_put_device;
506 
507 	err = -ENODEV;
508 	stm_output_init(&stmf->output);
509 	stmf->stm = to_stm_device(dev);
510 
511 	if (!try_module_get(stmf->stm->owner))
512 		goto err_free;
513 
514 	file->private_data = stmf;
515 
516 	return nonseekable_open(inode, file);
517 
518 err_free:
519 	kfree(stmf);
520 err_put_device:
521 	/* matches class_find_device() above */
522 	put_device(dev);
523 
524 	return err;
525 }
526 
527 static int stm_char_release(struct inode *inode, struct file *file)
528 {
529 	struct stm_file *stmf = file->private_data;
530 	struct stm_device *stm = stmf->stm;
531 
532 	if (stm->data->unlink)
533 		stm->data->unlink(stm->data, stmf->output.master,
534 				  stmf->output.channel);
535 
536 	stm_output_free(stm, &stmf->output);
537 
538 	/*
539 	 * matches the stm_char_open()'s
540 	 * class_find_device() + try_module_get()
541 	 */
542 	stm_put_device(stm);
543 	kfree(stmf);
544 
545 	return 0;
546 }
547 
548 static int
549 stm_assign_first_policy(struct stm_device *stm, struct stm_output *output,
550 			char **ids, unsigned int width)
551 {
552 	struct stp_policy_node *pn;
553 	int err, n;
554 
555 	/*
556 	 * On success, stp_policy_node_lookup() will return holding the
557 	 * configfs subsystem mutex, which is then released in
558 	 * stp_policy_node_put(). This allows the pdrv->output_open() in
559 	 * stm_output_assign() to serialize against the attribute accessors.
560 	 */
561 	for (n = 0, pn = NULL; ids[n] && !pn; n++)
562 		pn = stp_policy_node_lookup(stm, ids[n]);
563 
564 	if (!pn)
565 		return -EINVAL;
566 
567 	err = stm_output_assign(stm, width, pn, output);
568 
569 	stp_policy_node_put(pn);
570 
571 	return err;
572 }
573 
574 /**
575  * stm_data_write() - send the given payload as data packets
576  * @data:	stm driver's data
577  * @m:		STP master
578  * @c:		STP channel
579  * @ts_first:	timestamp the first packet
580  * @buf:	data payload buffer
581  * @count:	data payload size
582  */
583 ssize_t notrace stm_data_write(struct stm_data *data, unsigned int m,
584 			       unsigned int c, bool ts_first, const void *buf,
585 			       size_t count)
586 {
587 	unsigned int flags = ts_first ? STP_PACKET_TIMESTAMPED : 0;
588 	ssize_t sz;
589 	size_t pos;
590 
591 	for (pos = 0, sz = 0; pos < count; pos += sz) {
592 		sz = min_t(unsigned int, count - pos, 8);
593 		sz = data->packet(data, m, c, STP_PACKET_DATA, flags, sz,
594 				  &((u8 *)buf)[pos]);
595 		if (sz <= 0)
596 			break;
597 
598 		if (ts_first) {
599 			flags = 0;
600 			ts_first = false;
601 		}
602 	}
603 
604 	return sz < 0 ? sz : pos;
605 }
606 EXPORT_SYMBOL_GPL(stm_data_write);
607 
608 static ssize_t notrace
609 stm_write(struct stm_device *stm, struct stm_output *output,
610 	  unsigned int chan, const char *buf, size_t count)
611 {
612 	int err;
613 
614 	/* stm->pdrv is serialized against policy_mutex */
615 	if (!stm->pdrv)
616 		return -ENODEV;
617 
618 	err = stm->pdrv->write(stm->data, output, chan, buf, count);
619 	if (err < 0)
620 		return err;
621 
622 	return err;
623 }
624 
625 static ssize_t stm_char_write(struct file *file, const char __user *buf,
626 			      size_t count, loff_t *ppos)
627 {
628 	struct stm_file *stmf = file->private_data;
629 	struct stm_device *stm = stmf->stm;
630 	char *kbuf;
631 	int err;
632 
633 	if (count + 1 > PAGE_SIZE)
634 		count = PAGE_SIZE - 1;
635 
636 	/*
637 	 * If no m/c have been assigned to this writer up to this
638 	 * point, try to use the task name and "default" policy entries.
639 	 */
640 	if (!stmf->output.nr_chans) {
641 		char comm[sizeof(current->comm)];
642 		char *ids[] = { comm, "default", NULL };
643 
644 		get_task_comm(comm, current);
645 
646 		err = stm_assign_first_policy(stmf->stm, &stmf->output, ids, 1);
647 		/*
648 		 * EBUSY means that somebody else just assigned this
649 		 * output, which is just fine for write()
650 		 */
651 		if (err)
652 			return err;
653 	}
654 
655 	kbuf = kmalloc(count + 1, GFP_KERNEL);
656 	if (!kbuf)
657 		return -ENOMEM;
658 
659 	err = copy_from_user(kbuf, buf, count);
660 	if (err) {
661 		kfree(kbuf);
662 		return -EFAULT;
663 	}
664 
665 	pm_runtime_get_sync(&stm->dev);
666 
667 	count = stm_write(stm, &stmf->output, 0, kbuf, count);
668 
669 	pm_runtime_mark_last_busy(&stm->dev);
670 	pm_runtime_put_autosuspend(&stm->dev);
671 	kfree(kbuf);
672 
673 	return count;
674 }
675 
676 static void stm_mmap_open(struct vm_area_struct *vma)
677 {
678 	struct stm_file *stmf = vma->vm_file->private_data;
679 	struct stm_device *stm = stmf->stm;
680 
681 	pm_runtime_get(&stm->dev);
682 }
683 
684 static void stm_mmap_close(struct vm_area_struct *vma)
685 {
686 	struct stm_file *stmf = vma->vm_file->private_data;
687 	struct stm_device *stm = stmf->stm;
688 
689 	pm_runtime_mark_last_busy(&stm->dev);
690 	pm_runtime_put_autosuspend(&stm->dev);
691 }
692 
693 static const struct vm_operations_struct stm_mmap_vmops = {
694 	.open	= stm_mmap_open,
695 	.close	= stm_mmap_close,
696 };
697 
698 static int stm_char_mmap(struct file *file, struct vm_area_struct *vma)
699 {
700 	struct stm_file *stmf = file->private_data;
701 	struct stm_device *stm = stmf->stm;
702 	unsigned long size, phys;
703 
704 	if (!stm->data->mmio_addr)
705 		return -EOPNOTSUPP;
706 
707 	if (vma->vm_pgoff)
708 		return -EINVAL;
709 
710 	size = vma->vm_end - vma->vm_start;
711 
712 	if (stmf->output.nr_chans * stm->data->sw_mmiosz != size)
713 		return -EINVAL;
714 
715 	phys = stm->data->mmio_addr(stm->data, stmf->output.master,
716 				    stmf->output.channel,
717 				    stmf->output.nr_chans);
718 
719 	if (!phys)
720 		return -EINVAL;
721 
722 	pm_runtime_get_sync(&stm->dev);
723 
724 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
725 	vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
726 	vma->vm_ops = &stm_mmap_vmops;
727 	vm_iomap_memory(vma, phys, size);
728 
729 	return 0;
730 }
731 
732 static int stm_char_policy_set_ioctl(struct stm_file *stmf, void __user *arg)
733 {
734 	struct stm_device *stm = stmf->stm;
735 	struct stp_policy_id *id;
736 	char *ids[] = { NULL, NULL };
737 	int ret = -EINVAL, wlimit = 1;
738 	u32 size;
739 
740 	if (stmf->output.nr_chans)
741 		return -EBUSY;
742 
743 	if (copy_from_user(&size, arg, sizeof(size)))
744 		return -EFAULT;
745 
746 	if (size < sizeof(*id) || size >= PATH_MAX + sizeof(*id))
747 		return -EINVAL;
748 
749 	/*
750 	 * size + 1 to make sure the .id string at the bottom is terminated,
751 	 * which is also why memdup_user() is not useful here
752 	 */
753 	id = kzalloc(size + 1, GFP_KERNEL);
754 	if (!id)
755 		return -ENOMEM;
756 
757 	if (copy_from_user(id, arg, size)) {
758 		ret = -EFAULT;
759 		goto err_free;
760 	}
761 
762 	if (id->__reserved_0 || id->__reserved_1)
763 		goto err_free;
764 
765 	if (stm->data->sw_mmiosz)
766 		wlimit = PAGE_SIZE / stm->data->sw_mmiosz;
767 
768 	if (id->width < 1 || id->width > wlimit)
769 		goto err_free;
770 
771 	ids[0] = id->id;
772 	ret = stm_assign_first_policy(stmf->stm, &stmf->output, ids,
773 				      id->width);
774 	if (ret)
775 		goto err_free;
776 
777 	if (stm->data->link)
778 		ret = stm->data->link(stm->data, stmf->output.master,
779 				      stmf->output.channel);
780 
781 	if (ret)
782 		stm_output_free(stmf->stm, &stmf->output);
783 
784 err_free:
785 	kfree(id);
786 
787 	return ret;
788 }
789 
790 static int stm_char_policy_get_ioctl(struct stm_file *stmf, void __user *arg)
791 {
792 	struct stp_policy_id id = {
793 		.size		= sizeof(id),
794 		.master		= stmf->output.master,
795 		.channel	= stmf->output.channel,
796 		.width		= stmf->output.nr_chans,
797 		.__reserved_0	= 0,
798 		.__reserved_1	= 0,
799 	};
800 
801 	return copy_to_user(arg, &id, id.size) ? -EFAULT : 0;
802 }
803 
804 static long
805 stm_char_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
806 {
807 	struct stm_file *stmf = file->private_data;
808 	struct stm_data *stm_data = stmf->stm->data;
809 	int err = -ENOTTY;
810 	u64 options;
811 
812 	switch (cmd) {
813 	case STP_POLICY_ID_SET:
814 		err = stm_char_policy_set_ioctl(stmf, (void __user *)arg);
815 		if (err)
816 			return err;
817 
818 		return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
819 
820 	case STP_POLICY_ID_GET:
821 		return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
822 
823 	case STP_SET_OPTIONS:
824 		if (copy_from_user(&options, (u64 __user *)arg, sizeof(u64)))
825 			return -EFAULT;
826 
827 		if (stm_data->set_options)
828 			err = stm_data->set_options(stm_data,
829 						    stmf->output.master,
830 						    stmf->output.channel,
831 						    stmf->output.nr_chans,
832 						    options);
833 
834 		break;
835 	default:
836 		break;
837 	}
838 
839 	return err;
840 }
841 
842 #ifdef CONFIG_COMPAT
843 static long
844 stm_char_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
845 {
846 	return stm_char_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
847 }
848 #else
849 #define stm_char_compat_ioctl	NULL
850 #endif
851 
852 static const struct file_operations stm_fops = {
853 	.open		= stm_char_open,
854 	.release	= stm_char_release,
855 	.write		= stm_char_write,
856 	.mmap		= stm_char_mmap,
857 	.unlocked_ioctl	= stm_char_ioctl,
858 	.compat_ioctl	= stm_char_compat_ioctl,
859 	.llseek		= no_llseek,
860 };
861 
862 static void stm_device_release(struct device *dev)
863 {
864 	struct stm_device *stm = to_stm_device(dev);
865 
866 	vfree(stm);
867 }
868 
869 int stm_register_device(struct device *parent, struct stm_data *stm_data,
870 			struct module *owner)
871 {
872 	struct stm_device *stm;
873 	unsigned int nmasters;
874 	int err = -ENOMEM;
875 
876 	if (!stm_core_up)
877 		return -EPROBE_DEFER;
878 
879 	if (!stm_data->packet || !stm_data->sw_nchannels)
880 		return -EINVAL;
881 
882 	nmasters = stm_data->sw_end - stm_data->sw_start + 1;
883 	stm = vzalloc(sizeof(*stm) + nmasters * sizeof(void *));
884 	if (!stm)
885 		return -ENOMEM;
886 
887 	stm->major = register_chrdev(0, stm_data->name, &stm_fops);
888 	if (stm->major < 0)
889 		goto err_free;
890 
891 	device_initialize(&stm->dev);
892 	stm->dev.devt = MKDEV(stm->major, 0);
893 	stm->dev.class = &stm_class;
894 	stm->dev.parent = parent;
895 	stm->dev.release = stm_device_release;
896 
897 	mutex_init(&stm->link_mutex);
898 	spin_lock_init(&stm->link_lock);
899 	INIT_LIST_HEAD(&stm->link_list);
900 
901 	/* initialize the object before it is accessible via sysfs */
902 	spin_lock_init(&stm->mc_lock);
903 	mutex_init(&stm->policy_mutex);
904 	stm->sw_nmasters = nmasters;
905 	stm->owner = owner;
906 	stm->data = stm_data;
907 	stm_data->stm = stm;
908 
909 	err = kobject_set_name(&stm->dev.kobj, "%s", stm_data->name);
910 	if (err)
911 		goto err_device;
912 
913 	err = device_add(&stm->dev);
914 	if (err)
915 		goto err_device;
916 
917 	/*
918 	 * Use delayed autosuspend to avoid bouncing back and forth
919 	 * on recurring character device writes, with the initial
920 	 * delay time of 2 seconds.
921 	 */
922 	pm_runtime_no_callbacks(&stm->dev);
923 	pm_runtime_use_autosuspend(&stm->dev);
924 	pm_runtime_set_autosuspend_delay(&stm->dev, 2000);
925 	pm_runtime_set_suspended(&stm->dev);
926 	pm_runtime_enable(&stm->dev);
927 
928 	return 0;
929 
930 err_device:
931 	unregister_chrdev(stm->major, stm_data->name);
932 
933 	/* matches device_initialize() above */
934 	put_device(&stm->dev);
935 err_free:
936 	vfree(stm);
937 
938 	return err;
939 }
940 EXPORT_SYMBOL_GPL(stm_register_device);
941 
942 static int __stm_source_link_drop(struct stm_source_device *src,
943 				  struct stm_device *stm);
944 
945 void stm_unregister_device(struct stm_data *stm_data)
946 {
947 	struct stm_device *stm = stm_data->stm;
948 	struct stm_source_device *src, *iter;
949 	int i, ret;
950 
951 	pm_runtime_dont_use_autosuspend(&stm->dev);
952 	pm_runtime_disable(&stm->dev);
953 
954 	mutex_lock(&stm->link_mutex);
955 	list_for_each_entry_safe(src, iter, &stm->link_list, link_entry) {
956 		ret = __stm_source_link_drop(src, stm);
957 		/*
958 		 * src <-> stm link must not change under the same
959 		 * stm::link_mutex, so complain loudly if it has;
960 		 * also in this situation ret!=0 means this src is
961 		 * not connected to this stm and it should be otherwise
962 		 * safe to proceed with the tear-down of stm.
963 		 */
964 		WARN_ON_ONCE(ret);
965 	}
966 	mutex_unlock(&stm->link_mutex);
967 
968 	synchronize_srcu(&stm_source_srcu);
969 
970 	unregister_chrdev(stm->major, stm_data->name);
971 
972 	mutex_lock(&stm->policy_mutex);
973 	if (stm->policy)
974 		stp_policy_unbind(stm->policy);
975 	mutex_unlock(&stm->policy_mutex);
976 
977 	for (i = stm->data->sw_start; i <= stm->data->sw_end; i++)
978 		stp_master_free(stm, i);
979 
980 	device_unregister(&stm->dev);
981 	stm_data->stm = NULL;
982 }
983 EXPORT_SYMBOL_GPL(stm_unregister_device);
984 
985 /*
986  * stm::link_list access serialization uses a spinlock and a mutex; holding
987  * either of them guarantees that the list is stable; modification requires
988  * holding both of them.
989  *
990  * Lock ordering is as follows:
991  *   stm::link_mutex
992  *     stm::link_lock
993  *       src::link_lock
994  */
995 
996 /**
997  * stm_source_link_add() - connect an stm_source device to an stm device
998  * @src:	stm_source device
999  * @stm:	stm device
1000  *
1001  * This function establishes a link from stm_source to an stm device so that
1002  * the former can send out trace data to the latter.
1003  *
1004  * Return:	0 on success, -errno otherwise.
1005  */
1006 static int stm_source_link_add(struct stm_source_device *src,
1007 			       struct stm_device *stm)
1008 {
1009 	char *ids[] = { NULL, "default", NULL };
1010 	int err = -ENOMEM;
1011 
1012 	mutex_lock(&stm->link_mutex);
1013 	spin_lock(&stm->link_lock);
1014 	spin_lock(&src->link_lock);
1015 
1016 	/* src->link is dereferenced under stm_source_srcu but not the list */
1017 	rcu_assign_pointer(src->link, stm);
1018 	list_add_tail(&src->link_entry, &stm->link_list);
1019 
1020 	spin_unlock(&src->link_lock);
1021 	spin_unlock(&stm->link_lock);
1022 	mutex_unlock(&stm->link_mutex);
1023 
1024 	ids[0] = kstrdup(src->data->name, GFP_KERNEL);
1025 	if (!ids[0])
1026 		goto fail_detach;
1027 
1028 	err = stm_assign_first_policy(stm, &src->output, ids,
1029 				      src->data->nr_chans);
1030 	kfree(ids[0]);
1031 
1032 	if (err)
1033 		goto fail_detach;
1034 
1035 	/* this is to notify the STM device that a new link has been made */
1036 	if (stm->data->link)
1037 		err = stm->data->link(stm->data, src->output.master,
1038 				      src->output.channel);
1039 
1040 	if (err)
1041 		goto fail_free_output;
1042 
1043 	/* this is to let the source carry out all necessary preparations */
1044 	if (src->data->link)
1045 		src->data->link(src->data);
1046 
1047 	return 0;
1048 
1049 fail_free_output:
1050 	stm_output_free(stm, &src->output);
1051 
1052 fail_detach:
1053 	mutex_lock(&stm->link_mutex);
1054 	spin_lock(&stm->link_lock);
1055 	spin_lock(&src->link_lock);
1056 
1057 	rcu_assign_pointer(src->link, NULL);
1058 	list_del_init(&src->link_entry);
1059 
1060 	spin_unlock(&src->link_lock);
1061 	spin_unlock(&stm->link_lock);
1062 	mutex_unlock(&stm->link_mutex);
1063 
1064 	return err;
1065 }
1066 
1067 /**
1068  * __stm_source_link_drop() - detach stm_source from an stm device
1069  * @src:	stm_source device
1070  * @stm:	stm device
1071  *
1072  * If @stm is @src::link, disconnect them from one another and put the
1073  * reference on the @stm device.
1074  *
1075  * Caller must hold stm::link_mutex.
1076  */
1077 static int __stm_source_link_drop(struct stm_source_device *src,
1078 				  struct stm_device *stm)
1079 {
1080 	struct stm_device *link;
1081 	int ret = 0;
1082 
1083 	lockdep_assert_held(&stm->link_mutex);
1084 
1085 	/* for stm::link_list modification, we hold both mutex and spinlock */
1086 	spin_lock(&stm->link_lock);
1087 	spin_lock(&src->link_lock);
1088 	link = srcu_dereference_check(src->link, &stm_source_srcu, 1);
1089 
1090 	/*
1091 	 * The linked device may have changed since we last looked, because
1092 	 * we weren't holding the src::link_lock back then; if this is the
1093 	 * case, tell the caller to retry.
1094 	 */
1095 	if (link != stm) {
1096 		ret = -EAGAIN;
1097 		goto unlock;
1098 	}
1099 
1100 	stm_output_free(link, &src->output);
1101 	list_del_init(&src->link_entry);
1102 	pm_runtime_mark_last_busy(&link->dev);
1103 	pm_runtime_put_autosuspend(&link->dev);
1104 	/* matches stm_find_device() from stm_source_link_store() */
1105 	stm_put_device(link);
1106 	rcu_assign_pointer(src->link, NULL);
1107 
1108 unlock:
1109 	spin_unlock(&src->link_lock);
1110 	spin_unlock(&stm->link_lock);
1111 
1112 	/*
1113 	 * Call the unlink callbacks for both source and stm, when we know
1114 	 * that we have actually performed the unlinking.
1115 	 */
1116 	if (!ret) {
1117 		if (src->data->unlink)
1118 			src->data->unlink(src->data);
1119 
1120 		if (stm->data->unlink)
1121 			stm->data->unlink(stm->data, src->output.master,
1122 					  src->output.channel);
1123 	}
1124 
1125 	return ret;
1126 }
1127 
1128 /**
1129  * stm_source_link_drop() - detach stm_source from its stm device
1130  * @src:	stm_source device
1131  *
1132  * Unlinking means disconnecting from source's STM device; after this
1133  * writes will be unsuccessful until it is linked to a new STM device.
1134  *
1135  * This will happen on "stm_source_link" sysfs attribute write to undo
1136  * the existing link (if any), or on linked STM device's de-registration.
1137  */
1138 static void stm_source_link_drop(struct stm_source_device *src)
1139 {
1140 	struct stm_device *stm;
1141 	int idx, ret;
1142 
1143 retry:
1144 	idx = srcu_read_lock(&stm_source_srcu);
1145 	/*
1146 	 * The stm device will be valid for the duration of this
1147 	 * read section, but the link may change before we grab
1148 	 * the src::link_lock in __stm_source_link_drop().
1149 	 */
1150 	stm = srcu_dereference(src->link, &stm_source_srcu);
1151 
1152 	ret = 0;
1153 	if (stm) {
1154 		mutex_lock(&stm->link_mutex);
1155 		ret = __stm_source_link_drop(src, stm);
1156 		mutex_unlock(&stm->link_mutex);
1157 	}
1158 
1159 	srcu_read_unlock(&stm_source_srcu, idx);
1160 
1161 	/* if it did change, retry */
1162 	if (ret == -EAGAIN)
1163 		goto retry;
1164 }
1165 
1166 static ssize_t stm_source_link_show(struct device *dev,
1167 				    struct device_attribute *attr,
1168 				    char *buf)
1169 {
1170 	struct stm_source_device *src = to_stm_source_device(dev);
1171 	struct stm_device *stm;
1172 	int idx, ret;
1173 
1174 	idx = srcu_read_lock(&stm_source_srcu);
1175 	stm = srcu_dereference(src->link, &stm_source_srcu);
1176 	ret = sprintf(buf, "%s\n",
1177 		      stm ? dev_name(&stm->dev) : "<none>");
1178 	srcu_read_unlock(&stm_source_srcu, idx);
1179 
1180 	return ret;
1181 }
1182 
1183 static ssize_t stm_source_link_store(struct device *dev,
1184 				     struct device_attribute *attr,
1185 				     const char *buf, size_t count)
1186 {
1187 	struct stm_source_device *src = to_stm_source_device(dev);
1188 	struct stm_device *link;
1189 	int err;
1190 
1191 	stm_source_link_drop(src);
1192 
1193 	link = stm_find_device(buf);
1194 	if (!link)
1195 		return -EINVAL;
1196 
1197 	pm_runtime_get(&link->dev);
1198 
1199 	err = stm_source_link_add(src, link);
1200 	if (err) {
1201 		pm_runtime_put_autosuspend(&link->dev);
1202 		/* matches the stm_find_device() above */
1203 		stm_put_device(link);
1204 	}
1205 
1206 	return err ? : count;
1207 }
1208 
1209 static DEVICE_ATTR_RW(stm_source_link);
1210 
1211 static struct attribute *stm_source_attrs[] = {
1212 	&dev_attr_stm_source_link.attr,
1213 	NULL,
1214 };
1215 
1216 ATTRIBUTE_GROUPS(stm_source);
1217 
1218 static struct class stm_source_class = {
1219 	.name		= "stm_source",
1220 	.dev_groups	= stm_source_groups,
1221 };
1222 
1223 static void stm_source_device_release(struct device *dev)
1224 {
1225 	struct stm_source_device *src = to_stm_source_device(dev);
1226 
1227 	kfree(src);
1228 }
1229 
1230 /**
1231  * stm_source_register_device() - register an stm_source device
1232  * @parent:	parent device
1233  * @data:	device description structure
1234  *
1235  * This will create a device of stm_source class that can write
1236  * data to an stm device once linked.
1237  *
1238  * Return:	0 on success, -errno otherwise.
1239  */
1240 int stm_source_register_device(struct device *parent,
1241 			       struct stm_source_data *data)
1242 {
1243 	struct stm_source_device *src;
1244 	int err;
1245 
1246 	if (!stm_core_up)
1247 		return -EPROBE_DEFER;
1248 
1249 	src = kzalloc(sizeof(*src), GFP_KERNEL);
1250 	if (!src)
1251 		return -ENOMEM;
1252 
1253 	device_initialize(&src->dev);
1254 	src->dev.class = &stm_source_class;
1255 	src->dev.parent = parent;
1256 	src->dev.release = stm_source_device_release;
1257 
1258 	err = kobject_set_name(&src->dev.kobj, "%s", data->name);
1259 	if (err)
1260 		goto err;
1261 
1262 	pm_runtime_no_callbacks(&src->dev);
1263 	pm_runtime_forbid(&src->dev);
1264 
1265 	err = device_add(&src->dev);
1266 	if (err)
1267 		goto err;
1268 
1269 	stm_output_init(&src->output);
1270 	spin_lock_init(&src->link_lock);
1271 	INIT_LIST_HEAD(&src->link_entry);
1272 	src->data = data;
1273 	data->src = src;
1274 
1275 	return 0;
1276 
1277 err:
1278 	put_device(&src->dev);
1279 	kfree(src);
1280 
1281 	return err;
1282 }
1283 EXPORT_SYMBOL_GPL(stm_source_register_device);
1284 
1285 /**
1286  * stm_source_unregister_device() - unregister an stm_source device
1287  * @data:	device description that was used to register the device
1288  *
1289  * This will remove a previously created stm_source device from the system.
1290  */
1291 void stm_source_unregister_device(struct stm_source_data *data)
1292 {
1293 	struct stm_source_device *src = data->src;
1294 
1295 	stm_source_link_drop(src);
1296 
1297 	device_unregister(&src->dev);
1298 }
1299 EXPORT_SYMBOL_GPL(stm_source_unregister_device);
1300 
1301 int notrace stm_source_write(struct stm_source_data *data,
1302 			     unsigned int chan,
1303 			     const char *buf, size_t count)
1304 {
1305 	struct stm_source_device *src = data->src;
1306 	struct stm_device *stm;
1307 	int idx;
1308 
1309 	if (!src->output.nr_chans)
1310 		return -ENODEV;
1311 
1312 	if (chan >= src->output.nr_chans)
1313 		return -EINVAL;
1314 
1315 	idx = srcu_read_lock(&stm_source_srcu);
1316 
1317 	stm = srcu_dereference(src->link, &stm_source_srcu);
1318 	if (stm)
1319 		count = stm_write(stm, &src->output, chan, buf, count);
1320 	else
1321 		count = -ENODEV;
1322 
1323 	srcu_read_unlock(&stm_source_srcu, idx);
1324 
1325 	return count;
1326 }
1327 EXPORT_SYMBOL_GPL(stm_source_write);
1328 
1329 static int __init stm_core_init(void)
1330 {
1331 	int err;
1332 
1333 	err = class_register(&stm_class);
1334 	if (err)
1335 		return err;
1336 
1337 	err = class_register(&stm_source_class);
1338 	if (err)
1339 		goto err_stm;
1340 
1341 	err = stp_configfs_init();
1342 	if (err)
1343 		goto err_src;
1344 
1345 	init_srcu_struct(&stm_source_srcu);
1346 	INIT_LIST_HEAD(&stm_pdrv_head);
1347 	mutex_init(&stm_pdrv_mutex);
1348 
1349 	/*
1350 	 * So as to not confuse existing users with a requirement
1351 	 * to load yet another module, do it here.
1352 	 */
1353 	if (IS_ENABLED(CONFIG_STM_PROTO_BASIC))
1354 		(void)request_module_nowait("stm_p_basic");
1355 	stm_core_up++;
1356 
1357 	return 0;
1358 
1359 err_src:
1360 	class_unregister(&stm_source_class);
1361 err_stm:
1362 	class_unregister(&stm_class);
1363 
1364 	return err;
1365 }
1366 
1367 module_init(stm_core_init);
1368 
1369 static void __exit stm_core_exit(void)
1370 {
1371 	cleanup_srcu_struct(&stm_source_srcu);
1372 	class_unregister(&stm_source_class);
1373 	class_unregister(&stm_class);
1374 	stp_configfs_exit();
1375 }
1376 
1377 module_exit(stm_core_exit);
1378 
1379 MODULE_LICENSE("GPL v2");
1380 MODULE_DESCRIPTION("System Trace Module device class");
1381 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");
1382