hwrng: core - Don't use a stack buffer in add_early_randomness()
[linux/fpc-iii.git] / drivers / hwtracing / stm / core.c
blob51f81d64ca37f963b770e05227c74cb14c90498a
1 /*
2 * System Trace Module (STM) infrastructure
3 * Copyright (c) 2014, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
14 * STM class implements generic infrastructure for System Trace Module devices
15 * as defined in MIPI STPv2 specification.
18 #include <linux/pm_runtime.h>
19 #include <linux/uaccess.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/device.h>
23 #include <linux/compat.h>
24 #include <linux/kdev_t.h>
25 #include <linux/srcu.h>
26 #include <linux/slab.h>
27 #include <linux/stm.h>
28 #include <linux/fs.h>
29 #include <linux/mm.h>
30 #include "stm.h"
32 #include <uapi/linux/stm.h>
34 static unsigned int stm_core_up;
37 * The SRCU here makes sure that STM device doesn't disappear from under a
38 * stm_source_write() caller, which may want to have as little overhead as
39 * possible.
41 static struct srcu_struct stm_source_srcu;
43 static ssize_t masters_show(struct device *dev,
44 struct device_attribute *attr,
45 char *buf)
47 struct stm_device *stm = to_stm_device(dev);
48 int ret;
50 ret = sprintf(buf, "%u %u\n", stm->data->sw_start, stm->data->sw_end);
52 return ret;
55 static DEVICE_ATTR_RO(masters);
57 static ssize_t channels_show(struct device *dev,
58 struct device_attribute *attr,
59 char *buf)
61 struct stm_device *stm = to_stm_device(dev);
62 int ret;
64 ret = sprintf(buf, "%u\n", stm->data->sw_nchannels);
66 return ret;
69 static DEVICE_ATTR_RO(channels);
71 static ssize_t hw_override_show(struct device *dev,
72 struct device_attribute *attr,
73 char *buf)
75 struct stm_device *stm = to_stm_device(dev);
76 int ret;
78 ret = sprintf(buf, "%u\n", stm->data->hw_override);
80 return ret;
83 static DEVICE_ATTR_RO(hw_override);
85 static struct attribute *stm_attrs[] = {
86 &dev_attr_masters.attr,
87 &dev_attr_channels.attr,
88 &dev_attr_hw_override.attr,
89 NULL,
92 ATTRIBUTE_GROUPS(stm);
94 static struct class stm_class = {
95 .name = "stm",
96 .dev_groups = stm_groups,
99 static int stm_dev_match(struct device *dev, const void *data)
101 const char *name = data;
103 return sysfs_streq(name, dev_name(dev));
107 * stm_find_device() - find stm device by name
108 * @buf: character buffer containing the name
110 * This is called when either policy gets assigned to an stm device or an
111 * stm_source device gets linked to an stm device.
113 * This grabs device's reference (get_device()) and module reference, both
114 * of which the calling path needs to make sure to drop with stm_put_device().
116 * Return: stm device pointer or null if lookup failed.
118 struct stm_device *stm_find_device(const char *buf)
120 struct stm_device *stm;
121 struct device *dev;
123 if (!stm_core_up)
124 return NULL;
126 dev = class_find_device(&stm_class, NULL, buf, stm_dev_match);
127 if (!dev)
128 return NULL;
130 stm = to_stm_device(dev);
131 if (!try_module_get(stm->owner)) {
132 /* matches class_find_device() above */
133 put_device(dev);
134 return NULL;
137 return stm;
141 * stm_put_device() - drop references on the stm device
142 * @stm: stm device, previously acquired by stm_find_device()
144 * This drops the module reference and device reference taken by
145 * stm_find_device() or stm_char_open().
147 void stm_put_device(struct stm_device *stm)
149 module_put(stm->owner);
150 put_device(&stm->dev);
154 * Internally we only care about software-writable masters here, that is the
155 * ones in the range [stm_data->sw_start..stm_data..sw_end], however we need
156 * original master numbers to be visible externally, since they are the ones
157 * that will appear in the STP stream. Thus, the internal bookkeeping uses
158 * $master - stm_data->sw_start to reference master descriptors and such.
161 #define __stm_master(_s, _m) \
162 ((_s)->masters[(_m) - (_s)->data->sw_start])
164 static inline struct stp_master *
165 stm_master(struct stm_device *stm, unsigned int idx)
167 if (idx < stm->data->sw_start || idx > stm->data->sw_end)
168 return NULL;
170 return __stm_master(stm, idx);
173 static int stp_master_alloc(struct stm_device *stm, unsigned int idx)
175 struct stp_master *master;
176 size_t size;
178 size = ALIGN(stm->data->sw_nchannels, 8) / 8;
179 size += sizeof(struct stp_master);
180 master = kzalloc(size, GFP_ATOMIC);
181 if (!master)
182 return -ENOMEM;
184 master->nr_free = stm->data->sw_nchannels;
185 __stm_master(stm, idx) = master;
187 return 0;
190 static void stp_master_free(struct stm_device *stm, unsigned int idx)
192 struct stp_master *master = stm_master(stm, idx);
194 if (!master)
195 return;
197 __stm_master(stm, idx) = NULL;
198 kfree(master);
201 static void stm_output_claim(struct stm_device *stm, struct stm_output *output)
203 struct stp_master *master = stm_master(stm, output->master);
205 lockdep_assert_held(&stm->mc_lock);
206 lockdep_assert_held(&output->lock);
208 if (WARN_ON_ONCE(master->nr_free < output->nr_chans))
209 return;
211 bitmap_allocate_region(&master->chan_map[0], output->channel,
212 ilog2(output->nr_chans));
214 master->nr_free -= output->nr_chans;
217 static void
218 stm_output_disclaim(struct stm_device *stm, struct stm_output *output)
220 struct stp_master *master = stm_master(stm, output->master);
222 lockdep_assert_held(&stm->mc_lock);
223 lockdep_assert_held(&output->lock);
225 bitmap_release_region(&master->chan_map[0], output->channel,
226 ilog2(output->nr_chans));
228 output->nr_chans = 0;
229 master->nr_free += output->nr_chans;
233 * This is like bitmap_find_free_region(), except it can ignore @start bits
234 * at the beginning.
236 static int find_free_channels(unsigned long *bitmap, unsigned int start,
237 unsigned int end, unsigned int width)
239 unsigned int pos;
240 int i;
242 for (pos = start; pos < end + 1; pos = ALIGN(pos, width)) {
243 pos = find_next_zero_bit(bitmap, end + 1, pos);
244 if (pos + width > end + 1)
245 break;
247 if (pos & (width - 1))
248 continue;
250 for (i = 1; i < width && !test_bit(pos + i, bitmap); i++)
252 if (i == width)
253 return pos;
256 return -1;
259 static int
260 stm_find_master_chan(struct stm_device *stm, unsigned int width,
261 unsigned int *mstart, unsigned int mend,
262 unsigned int *cstart, unsigned int cend)
264 struct stp_master *master;
265 unsigned int midx;
266 int pos, err;
268 for (midx = *mstart; midx <= mend; midx++) {
269 if (!stm_master(stm, midx)) {
270 err = stp_master_alloc(stm, midx);
271 if (err)
272 return err;
275 master = stm_master(stm, midx);
277 if (!master->nr_free)
278 continue;
280 pos = find_free_channels(master->chan_map, *cstart, cend,
281 width);
282 if (pos < 0)
283 continue;
285 *mstart = midx;
286 *cstart = pos;
287 return 0;
290 return -ENOSPC;
293 static int stm_output_assign(struct stm_device *stm, unsigned int width,
294 struct stp_policy_node *policy_node,
295 struct stm_output *output)
297 unsigned int midx, cidx, mend, cend;
298 int ret = -EINVAL;
300 if (width > stm->data->sw_nchannels)
301 return -EINVAL;
303 if (policy_node) {
304 stp_policy_node_get_ranges(policy_node,
305 &midx, &mend, &cidx, &cend);
306 } else {
307 midx = stm->data->sw_start;
308 cidx = 0;
309 mend = stm->data->sw_end;
310 cend = stm->data->sw_nchannels - 1;
313 spin_lock(&stm->mc_lock);
314 spin_lock(&output->lock);
315 /* output is already assigned -- shouldn't happen */
316 if (WARN_ON_ONCE(output->nr_chans))
317 goto unlock;
319 ret = stm_find_master_chan(stm, width, &midx, mend, &cidx, cend);
320 if (ret < 0)
321 goto unlock;
323 output->master = midx;
324 output->channel = cidx;
325 output->nr_chans = width;
326 stm_output_claim(stm, output);
327 dev_dbg(&stm->dev, "assigned %u:%u (+%u)\n", midx, cidx, width);
329 ret = 0;
330 unlock:
331 spin_unlock(&output->lock);
332 spin_unlock(&stm->mc_lock);
334 return ret;
337 static void stm_output_free(struct stm_device *stm, struct stm_output *output)
339 spin_lock(&stm->mc_lock);
340 spin_lock(&output->lock);
341 if (output->nr_chans)
342 stm_output_disclaim(stm, output);
343 spin_unlock(&output->lock);
344 spin_unlock(&stm->mc_lock);
347 static void stm_output_init(struct stm_output *output)
349 spin_lock_init(&output->lock);
352 static int major_match(struct device *dev, const void *data)
354 unsigned int major = *(unsigned int *)data;
356 return MAJOR(dev->devt) == major;
359 static int stm_char_open(struct inode *inode, struct file *file)
361 struct stm_file *stmf;
362 struct device *dev;
363 unsigned int major = imajor(inode);
364 int err = -ENODEV;
366 dev = class_find_device(&stm_class, NULL, &major, major_match);
367 if (!dev)
368 return -ENODEV;
370 stmf = kzalloc(sizeof(*stmf), GFP_KERNEL);
371 if (!stmf)
372 return -ENOMEM;
374 stm_output_init(&stmf->output);
375 stmf->stm = to_stm_device(dev);
377 if (!try_module_get(stmf->stm->owner))
378 goto err_free;
380 file->private_data = stmf;
382 return nonseekable_open(inode, file);
384 err_free:
385 /* matches class_find_device() above */
386 put_device(dev);
387 kfree(stmf);
389 return err;
392 static int stm_char_release(struct inode *inode, struct file *file)
394 struct stm_file *stmf = file->private_data;
395 struct stm_device *stm = stmf->stm;
397 if (stm->data->unlink)
398 stm->data->unlink(stm->data, stmf->output.master,
399 stmf->output.channel);
401 stm_output_free(stm, &stmf->output);
404 * matches the stm_char_open()'s
405 * class_find_device() + try_module_get()
407 stm_put_device(stm);
408 kfree(stmf);
410 return 0;
413 static int stm_file_assign(struct stm_file *stmf, char *id, unsigned int width)
415 struct stm_device *stm = stmf->stm;
416 int ret;
418 stmf->policy_node = stp_policy_node_lookup(stm, id);
420 ret = stm_output_assign(stm, width, stmf->policy_node, &stmf->output);
422 if (stmf->policy_node)
423 stp_policy_node_put(stmf->policy_node);
425 return ret;
428 static ssize_t stm_write(struct stm_data *data, unsigned int master,
429 unsigned int channel, const char *buf, size_t count)
431 unsigned int flags = STP_PACKET_TIMESTAMPED;
432 const unsigned char *p = buf, nil = 0;
433 size_t pos;
434 ssize_t sz;
436 for (pos = 0, p = buf; count > pos; pos += sz, p += sz) {
437 sz = min_t(unsigned int, count - pos, 8);
438 sz = data->packet(data, master, channel, STP_PACKET_DATA, flags,
439 sz, p);
440 flags = 0;
442 if (sz < 0)
443 break;
446 data->packet(data, master, channel, STP_PACKET_FLAG, 0, 0, &nil);
448 return pos;
451 static ssize_t stm_char_write(struct file *file, const char __user *buf,
452 size_t count, loff_t *ppos)
454 struct stm_file *stmf = file->private_data;
455 struct stm_device *stm = stmf->stm;
456 char *kbuf;
457 int err;
459 if (count + 1 > PAGE_SIZE)
460 count = PAGE_SIZE - 1;
463 * if no m/c have been assigned to this writer up to this
464 * point, use "default" policy entry
466 if (!stmf->output.nr_chans) {
467 err = stm_file_assign(stmf, "default", 1);
469 * EBUSY means that somebody else just assigned this
470 * output, which is just fine for write()
472 if (err && err != -EBUSY)
473 return err;
476 kbuf = kmalloc(count + 1, GFP_KERNEL);
477 if (!kbuf)
478 return -ENOMEM;
480 err = copy_from_user(kbuf, buf, count);
481 if (err) {
482 kfree(kbuf);
483 return -EFAULT;
486 pm_runtime_get_sync(&stm->dev);
488 count = stm_write(stm->data, stmf->output.master, stmf->output.channel,
489 kbuf, count);
491 pm_runtime_mark_last_busy(&stm->dev);
492 pm_runtime_put_autosuspend(&stm->dev);
493 kfree(kbuf);
495 return count;
498 static void stm_mmap_open(struct vm_area_struct *vma)
500 struct stm_file *stmf = vma->vm_file->private_data;
501 struct stm_device *stm = stmf->stm;
503 pm_runtime_get(&stm->dev);
506 static void stm_mmap_close(struct vm_area_struct *vma)
508 struct stm_file *stmf = vma->vm_file->private_data;
509 struct stm_device *stm = stmf->stm;
511 pm_runtime_mark_last_busy(&stm->dev);
512 pm_runtime_put_autosuspend(&stm->dev);
515 static const struct vm_operations_struct stm_mmap_vmops = {
516 .open = stm_mmap_open,
517 .close = stm_mmap_close,
520 static int stm_char_mmap(struct file *file, struct vm_area_struct *vma)
522 struct stm_file *stmf = file->private_data;
523 struct stm_device *stm = stmf->stm;
524 unsigned long size, phys;
526 if (!stm->data->mmio_addr)
527 return -EOPNOTSUPP;
529 if (vma->vm_pgoff)
530 return -EINVAL;
532 size = vma->vm_end - vma->vm_start;
534 if (stmf->output.nr_chans * stm->data->sw_mmiosz != size)
535 return -EINVAL;
537 phys = stm->data->mmio_addr(stm->data, stmf->output.master,
538 stmf->output.channel,
539 stmf->output.nr_chans);
541 if (!phys)
542 return -EINVAL;
544 pm_runtime_get_sync(&stm->dev);
546 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
547 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
548 vma->vm_ops = &stm_mmap_vmops;
549 vm_iomap_memory(vma, phys, size);
551 return 0;
554 static int stm_char_policy_set_ioctl(struct stm_file *stmf, void __user *arg)
556 struct stm_device *stm = stmf->stm;
557 struct stp_policy_id *id;
558 int ret = -EINVAL;
559 u32 size;
561 if (stmf->output.nr_chans)
562 return -EBUSY;
564 if (copy_from_user(&size, arg, sizeof(size)))
565 return -EFAULT;
567 if (size >= PATH_MAX + sizeof(*id))
568 return -EINVAL;
571 * size + 1 to make sure the .id string at the bottom is terminated,
572 * which is also why memdup_user() is not useful here
574 id = kzalloc(size + 1, GFP_KERNEL);
575 if (!id)
576 return -ENOMEM;
578 if (copy_from_user(id, arg, size)) {
579 ret = -EFAULT;
580 goto err_free;
583 if (id->__reserved_0 || id->__reserved_1)
584 goto err_free;
586 if (id->width < 1 ||
587 id->width > PAGE_SIZE / stm->data->sw_mmiosz)
588 goto err_free;
590 ret = stm_file_assign(stmf, id->id, id->width);
591 if (ret)
592 goto err_free;
594 if (stm->data->link)
595 ret = stm->data->link(stm->data, stmf->output.master,
596 stmf->output.channel);
598 if (ret)
599 stm_output_free(stmf->stm, &stmf->output);
601 err_free:
602 kfree(id);
604 return ret;
607 static int stm_char_policy_get_ioctl(struct stm_file *stmf, void __user *arg)
609 struct stp_policy_id id = {
610 .size = sizeof(id),
611 .master = stmf->output.master,
612 .channel = stmf->output.channel,
613 .width = stmf->output.nr_chans,
614 .__reserved_0 = 0,
615 .__reserved_1 = 0,
618 return copy_to_user(arg, &id, id.size) ? -EFAULT : 0;
621 static long
622 stm_char_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
624 struct stm_file *stmf = file->private_data;
625 struct stm_data *stm_data = stmf->stm->data;
626 int err = -ENOTTY;
627 u64 options;
629 switch (cmd) {
630 case STP_POLICY_ID_SET:
631 err = stm_char_policy_set_ioctl(stmf, (void __user *)arg);
632 if (err)
633 return err;
635 return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
637 case STP_POLICY_ID_GET:
638 return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
640 case STP_SET_OPTIONS:
641 if (copy_from_user(&options, (u64 __user *)arg, sizeof(u64)))
642 return -EFAULT;
644 if (stm_data->set_options)
645 err = stm_data->set_options(stm_data,
646 stmf->output.master,
647 stmf->output.channel,
648 stmf->output.nr_chans,
649 options);
651 break;
652 default:
653 break;
656 return err;
659 #ifdef CONFIG_COMPAT
660 static long
661 stm_char_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
663 return stm_char_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
665 #else
666 #define stm_char_compat_ioctl NULL
667 #endif
669 static const struct file_operations stm_fops = {
670 .open = stm_char_open,
671 .release = stm_char_release,
672 .write = stm_char_write,
673 .mmap = stm_char_mmap,
674 .unlocked_ioctl = stm_char_ioctl,
675 .compat_ioctl = stm_char_compat_ioctl,
676 .llseek = no_llseek,
679 static void stm_device_release(struct device *dev)
681 struct stm_device *stm = to_stm_device(dev);
683 kfree(stm);
686 int stm_register_device(struct device *parent, struct stm_data *stm_data,
687 struct module *owner)
689 struct stm_device *stm;
690 unsigned int nmasters;
691 int err = -ENOMEM;
693 if (!stm_core_up)
694 return -EPROBE_DEFER;
696 if (!stm_data->packet || !stm_data->sw_nchannels)
697 return -EINVAL;
699 nmasters = stm_data->sw_end - stm_data->sw_start + 1;
700 stm = kzalloc(sizeof(*stm) + nmasters * sizeof(void *), GFP_KERNEL);
701 if (!stm)
702 return -ENOMEM;
704 stm->major = register_chrdev(0, stm_data->name, &stm_fops);
705 if (stm->major < 0)
706 goto err_free;
708 device_initialize(&stm->dev);
709 stm->dev.devt = MKDEV(stm->major, 0);
710 stm->dev.class = &stm_class;
711 stm->dev.parent = parent;
712 stm->dev.release = stm_device_release;
714 mutex_init(&stm->link_mutex);
715 spin_lock_init(&stm->link_lock);
716 INIT_LIST_HEAD(&stm->link_list);
718 /* initialize the object before it is accessible via sysfs */
719 spin_lock_init(&stm->mc_lock);
720 mutex_init(&stm->policy_mutex);
721 stm->sw_nmasters = nmasters;
722 stm->owner = owner;
723 stm->data = stm_data;
724 stm_data->stm = stm;
726 err = kobject_set_name(&stm->dev.kobj, "%s", stm_data->name);
727 if (err)
728 goto err_device;
730 err = device_add(&stm->dev);
731 if (err)
732 goto err_device;
735 * Use delayed autosuspend to avoid bouncing back and forth
736 * on recurring character device writes, with the initial
737 * delay time of 2 seconds.
739 pm_runtime_no_callbacks(&stm->dev);
740 pm_runtime_use_autosuspend(&stm->dev);
741 pm_runtime_set_autosuspend_delay(&stm->dev, 2000);
742 pm_runtime_set_suspended(&stm->dev);
743 pm_runtime_enable(&stm->dev);
745 return 0;
747 err_device:
748 unregister_chrdev(stm->major, stm_data->name);
750 /* matches device_initialize() above */
751 put_device(&stm->dev);
752 err_free:
753 kfree(stm);
755 return err;
757 EXPORT_SYMBOL_GPL(stm_register_device);
759 static int __stm_source_link_drop(struct stm_source_device *src,
760 struct stm_device *stm);
762 void stm_unregister_device(struct stm_data *stm_data)
764 struct stm_device *stm = stm_data->stm;
765 struct stm_source_device *src, *iter;
766 int i, ret;
768 pm_runtime_dont_use_autosuspend(&stm->dev);
769 pm_runtime_disable(&stm->dev);
771 mutex_lock(&stm->link_mutex);
772 list_for_each_entry_safe(src, iter, &stm->link_list, link_entry) {
773 ret = __stm_source_link_drop(src, stm);
775 * src <-> stm link must not change under the same
776 * stm::link_mutex, so complain loudly if it has;
777 * also in this situation ret!=0 means this src is
778 * not connected to this stm and it should be otherwise
779 * safe to proceed with the tear-down of stm.
781 WARN_ON_ONCE(ret);
783 mutex_unlock(&stm->link_mutex);
785 synchronize_srcu(&stm_source_srcu);
787 unregister_chrdev(stm->major, stm_data->name);
789 mutex_lock(&stm->policy_mutex);
790 if (stm->policy)
791 stp_policy_unbind(stm->policy);
792 mutex_unlock(&stm->policy_mutex);
794 for (i = stm->data->sw_start; i <= stm->data->sw_end; i++)
795 stp_master_free(stm, i);
797 device_unregister(&stm->dev);
798 stm_data->stm = NULL;
800 EXPORT_SYMBOL_GPL(stm_unregister_device);
803 * stm::link_list access serialization uses a spinlock and a mutex; holding
804 * either of them guarantees that the list is stable; modification requires
805 * holding both of them.
807 * Lock ordering is as follows:
808 * stm::link_mutex
809 * stm::link_lock
810 * src::link_lock
814 * stm_source_link_add() - connect an stm_source device to an stm device
815 * @src: stm_source device
816 * @stm: stm device
818 * This function establishes a link from stm_source to an stm device so that
819 * the former can send out trace data to the latter.
821 * Return: 0 on success, -errno otherwise.
823 static int stm_source_link_add(struct stm_source_device *src,
824 struct stm_device *stm)
826 char *id;
827 int err;
829 mutex_lock(&stm->link_mutex);
830 spin_lock(&stm->link_lock);
831 spin_lock(&src->link_lock);
833 /* src->link is dereferenced under stm_source_srcu but not the list */
834 rcu_assign_pointer(src->link, stm);
835 list_add_tail(&src->link_entry, &stm->link_list);
837 spin_unlock(&src->link_lock);
838 spin_unlock(&stm->link_lock);
839 mutex_unlock(&stm->link_mutex);
841 id = kstrdup(src->data->name, GFP_KERNEL);
842 if (id) {
843 src->policy_node =
844 stp_policy_node_lookup(stm, id);
846 kfree(id);
849 err = stm_output_assign(stm, src->data->nr_chans,
850 src->policy_node, &src->output);
852 if (src->policy_node)
853 stp_policy_node_put(src->policy_node);
855 if (err)
856 goto fail_detach;
858 /* this is to notify the STM device that a new link has been made */
859 if (stm->data->link)
860 err = stm->data->link(stm->data, src->output.master,
861 src->output.channel);
863 if (err)
864 goto fail_free_output;
866 /* this is to let the source carry out all necessary preparations */
867 if (src->data->link)
868 src->data->link(src->data);
870 return 0;
872 fail_free_output:
873 stm_output_free(stm, &src->output);
875 fail_detach:
876 mutex_lock(&stm->link_mutex);
877 spin_lock(&stm->link_lock);
878 spin_lock(&src->link_lock);
880 rcu_assign_pointer(src->link, NULL);
881 list_del_init(&src->link_entry);
883 spin_unlock(&src->link_lock);
884 spin_unlock(&stm->link_lock);
885 mutex_unlock(&stm->link_mutex);
887 return err;
891 * __stm_source_link_drop() - detach stm_source from an stm device
892 * @src: stm_source device
893 * @stm: stm device
895 * If @stm is @src::link, disconnect them from one another and put the
896 * reference on the @stm device.
898 * Caller must hold stm::link_mutex.
900 static int __stm_source_link_drop(struct stm_source_device *src,
901 struct stm_device *stm)
903 struct stm_device *link;
904 int ret = 0;
906 lockdep_assert_held(&stm->link_mutex);
908 /* for stm::link_list modification, we hold both mutex and spinlock */
909 spin_lock(&stm->link_lock);
910 spin_lock(&src->link_lock);
911 link = srcu_dereference_check(src->link, &stm_source_srcu, 1);
914 * The linked device may have changed since we last looked, because
915 * we weren't holding the src::link_lock back then; if this is the
916 * case, tell the caller to retry.
918 if (link != stm) {
919 ret = -EAGAIN;
920 goto unlock;
923 stm_output_free(link, &src->output);
924 list_del_init(&src->link_entry);
925 pm_runtime_mark_last_busy(&link->dev);
926 pm_runtime_put_autosuspend(&link->dev);
927 /* matches stm_find_device() from stm_source_link_store() */
928 stm_put_device(link);
929 rcu_assign_pointer(src->link, NULL);
931 unlock:
932 spin_unlock(&src->link_lock);
933 spin_unlock(&stm->link_lock);
936 * Call the unlink callbacks for both source and stm, when we know
937 * that we have actually performed the unlinking.
939 if (!ret) {
940 if (src->data->unlink)
941 src->data->unlink(src->data);
943 if (stm->data->unlink)
944 stm->data->unlink(stm->data, src->output.master,
945 src->output.channel);
948 return ret;
952 * stm_source_link_drop() - detach stm_source from its stm device
953 * @src: stm_source device
955 * Unlinking means disconnecting from source's STM device; after this
956 * writes will be unsuccessful until it is linked to a new STM device.
958 * This will happen on "stm_source_link" sysfs attribute write to undo
959 * the existing link (if any), or on linked STM device's de-registration.
961 static void stm_source_link_drop(struct stm_source_device *src)
963 struct stm_device *stm;
964 int idx, ret;
966 retry:
967 idx = srcu_read_lock(&stm_source_srcu);
969 * The stm device will be valid for the duration of this
970 * read section, but the link may change before we grab
971 * the src::link_lock in __stm_source_link_drop().
973 stm = srcu_dereference(src->link, &stm_source_srcu);
975 ret = 0;
976 if (stm) {
977 mutex_lock(&stm->link_mutex);
978 ret = __stm_source_link_drop(src, stm);
979 mutex_unlock(&stm->link_mutex);
982 srcu_read_unlock(&stm_source_srcu, idx);
984 /* if it did change, retry */
985 if (ret == -EAGAIN)
986 goto retry;
989 static ssize_t stm_source_link_show(struct device *dev,
990 struct device_attribute *attr,
991 char *buf)
993 struct stm_source_device *src = to_stm_source_device(dev);
994 struct stm_device *stm;
995 int idx, ret;
997 idx = srcu_read_lock(&stm_source_srcu);
998 stm = srcu_dereference(src->link, &stm_source_srcu);
999 ret = sprintf(buf, "%s\n",
1000 stm ? dev_name(&stm->dev) : "<none>");
1001 srcu_read_unlock(&stm_source_srcu, idx);
1003 return ret;
1006 static ssize_t stm_source_link_store(struct device *dev,
1007 struct device_attribute *attr,
1008 const char *buf, size_t count)
1010 struct stm_source_device *src = to_stm_source_device(dev);
1011 struct stm_device *link;
1012 int err;
1014 stm_source_link_drop(src);
1016 link = stm_find_device(buf);
1017 if (!link)
1018 return -EINVAL;
1020 pm_runtime_get(&link->dev);
1022 err = stm_source_link_add(src, link);
1023 if (err) {
1024 pm_runtime_put_autosuspend(&link->dev);
1025 /* matches the stm_find_device() above */
1026 stm_put_device(link);
1029 return err ? : count;
1032 static DEVICE_ATTR_RW(stm_source_link);
1034 static struct attribute *stm_source_attrs[] = {
1035 &dev_attr_stm_source_link.attr,
1036 NULL,
1039 ATTRIBUTE_GROUPS(stm_source);
1041 static struct class stm_source_class = {
1042 .name = "stm_source",
1043 .dev_groups = stm_source_groups,
1046 static void stm_source_device_release(struct device *dev)
1048 struct stm_source_device *src = to_stm_source_device(dev);
1050 kfree(src);
1054 * stm_source_register_device() - register an stm_source device
1055 * @parent: parent device
1056 * @data: device description structure
1058 * This will create a device of stm_source class that can write
1059 * data to an stm device once linked.
1061 * Return: 0 on success, -errno otherwise.
1063 int stm_source_register_device(struct device *parent,
1064 struct stm_source_data *data)
1066 struct stm_source_device *src;
1067 int err;
1069 if (!stm_core_up)
1070 return -EPROBE_DEFER;
1072 src = kzalloc(sizeof(*src), GFP_KERNEL);
1073 if (!src)
1074 return -ENOMEM;
1076 device_initialize(&src->dev);
1077 src->dev.class = &stm_source_class;
1078 src->dev.parent = parent;
1079 src->dev.release = stm_source_device_release;
1081 err = kobject_set_name(&src->dev.kobj, "%s", data->name);
1082 if (err)
1083 goto err;
1085 pm_runtime_no_callbacks(&src->dev);
1086 pm_runtime_forbid(&src->dev);
1088 err = device_add(&src->dev);
1089 if (err)
1090 goto err;
1092 stm_output_init(&src->output);
1093 spin_lock_init(&src->link_lock);
1094 INIT_LIST_HEAD(&src->link_entry);
1095 src->data = data;
1096 data->src = src;
1098 return 0;
1100 err:
1101 put_device(&src->dev);
1102 kfree(src);
1104 return err;
1106 EXPORT_SYMBOL_GPL(stm_source_register_device);
1109 * stm_source_unregister_device() - unregister an stm_source device
1110 * @data: device description that was used to register the device
1112 * This will remove a previously created stm_source device from the system.
1114 void stm_source_unregister_device(struct stm_source_data *data)
1116 struct stm_source_device *src = data->src;
1118 stm_source_link_drop(src);
1120 device_destroy(&stm_source_class, src->dev.devt);
1122 EXPORT_SYMBOL_GPL(stm_source_unregister_device);
1124 int stm_source_write(struct stm_source_data *data, unsigned int chan,
1125 const char *buf, size_t count)
1127 struct stm_source_device *src = data->src;
1128 struct stm_device *stm;
1129 int idx;
1131 if (!src->output.nr_chans)
1132 return -ENODEV;
1134 if (chan >= src->output.nr_chans)
1135 return -EINVAL;
1137 idx = srcu_read_lock(&stm_source_srcu);
1139 stm = srcu_dereference(src->link, &stm_source_srcu);
1140 if (stm)
1141 count = stm_write(stm->data, src->output.master,
1142 src->output.channel + chan,
1143 buf, count);
1144 else
1145 count = -ENODEV;
1147 srcu_read_unlock(&stm_source_srcu, idx);
1149 return count;
1151 EXPORT_SYMBOL_GPL(stm_source_write);
1153 static int __init stm_core_init(void)
1155 int err;
1157 err = class_register(&stm_class);
1158 if (err)
1159 return err;
1161 err = class_register(&stm_source_class);
1162 if (err)
1163 goto err_stm;
1165 err = stp_configfs_init();
1166 if (err)
1167 goto err_src;
1169 init_srcu_struct(&stm_source_srcu);
1171 stm_core_up++;
1173 return 0;
1175 err_src:
1176 class_unregister(&stm_source_class);
1177 err_stm:
1178 class_unregister(&stm_class);
1180 return err;
1183 module_init(stm_core_init);
1185 static void __exit stm_core_exit(void)
1187 cleanup_srcu_struct(&stm_source_srcu);
1188 class_unregister(&stm_source_class);
1189 class_unregister(&stm_class);
1190 stp_configfs_exit();
1193 module_exit(stm_core_exit);
1195 MODULE_LICENSE("GPL v2");
1196 MODULE_DESCRIPTION("System Trace Module device class");
1197 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");