1 // SPDX-License-Identifier: GPL-2.0
3 * System Trace Module (STM) infrastructure
4 * Copyright (c) 2014, Intel Corporation.
6 * STM class implements generic infrastructure for System Trace Module devices
7 * as defined in MIPI STPv2 specification.
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>
22 #include <linux/vmalloc.h>
25 #include <uapi/linux/stm.h>
27 static unsigned int stm_core_up;
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
34 static struct srcu_struct stm_source_srcu;
36 static ssize_t masters_show(struct device *dev,
37 struct device_attribute *attr,
40 struct stm_device *stm = to_stm_device(dev);
43 ret = sprintf(buf, "%u %u\n", stm->data->sw_start, stm->data->sw_end);
48 static DEVICE_ATTR_RO(masters);
50 static ssize_t channels_show(struct device *dev,
51 struct device_attribute *attr,
54 struct stm_device *stm = to_stm_device(dev);
57 ret = sprintf(buf, "%u\n", stm->data->sw_nchannels);
62 static DEVICE_ATTR_RO(channels);
64 static ssize_t hw_override_show(struct device *dev,
65 struct device_attribute *attr,
68 struct stm_device *stm = to_stm_device(dev);
71 ret = sprintf(buf, "%u\n", stm->data->hw_override);
76 static DEVICE_ATTR_RO(hw_override);
78 static struct attribute *stm_attrs[] = {
79 &dev_attr_masters.attr,
80 &dev_attr_channels.attr,
81 &dev_attr_hw_override.attr,
85 ATTRIBUTE_GROUPS(stm);
87 static struct class stm_class = {
89 .dev_groups = stm_groups,
92 static int stm_dev_match(struct device *dev, const void *data)
94 const char *name = data;
96 return sysfs_streq(name, dev_name(dev));
100 * stm_find_device() - find stm device by name
101 * @buf: character buffer containing the name
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.
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().
109 * Return: stm device pointer or null if lookup failed.
111 struct stm_device *stm_find_device(const char *buf)
113 struct stm_device *stm;
119 dev = class_find_device(&stm_class, NULL, buf, stm_dev_match);
123 stm = to_stm_device(dev);
124 if (!try_module_get(stm->owner)) {
125 /* matches class_find_device() above */
134 * stm_put_device() - drop references on the stm device
135 * @stm: stm device, previously acquired by stm_find_device()
137 * This drops the module reference and device reference taken by
138 * stm_find_device() or stm_char_open().
140 void stm_put_device(struct stm_device *stm)
142 module_put(stm->owner);
143 put_device(&stm->dev);
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.
154 #define __stm_master(_s, _m) \
155 ((_s)->masters[(_m) - (_s)->data->sw_start])
157 static inline struct stp_master *
158 stm_master(struct stm_device *stm, unsigned int idx)
160 if (idx < stm->data->sw_start || idx > stm->data->sw_end)
163 return __stm_master(stm, idx);
166 static int stp_master_alloc(struct stm_device *stm, unsigned int idx)
168 struct stp_master *master;
170 master = kzalloc(struct_size(master, chan_map,
171 BITS_TO_LONGS(stm->data->sw_nchannels)),
176 master->nr_free = stm->data->sw_nchannels;
177 __stm_master(stm, idx) = master;
182 static void stp_master_free(struct stm_device *stm, unsigned int idx)
184 struct stp_master *master = stm_master(stm, idx);
189 __stm_master(stm, idx) = NULL;
193 static void stm_output_claim(struct stm_device *stm, struct stm_output *output)
195 struct stp_master *master = stm_master(stm, output->master);
197 lockdep_assert_held(&stm->mc_lock);
198 lockdep_assert_held(&output->lock);
200 if (WARN_ON_ONCE(master->nr_free < output->nr_chans))
203 bitmap_allocate_region(&master->chan_map[0], output->channel,
204 ilog2(output->nr_chans));
206 master->nr_free -= output->nr_chans;
210 stm_output_disclaim(struct stm_device *stm, struct stm_output *output)
212 struct stp_master *master = stm_master(stm, output->master);
214 lockdep_assert_held(&stm->mc_lock);
215 lockdep_assert_held(&output->lock);
217 bitmap_release_region(&master->chan_map[0], output->channel,
218 ilog2(output->nr_chans));
220 master->nr_free += output->nr_chans;
221 output->nr_chans = 0;
225 * This is like bitmap_find_free_region(), except it can ignore @start bits
228 static int find_free_channels(unsigned long *bitmap, unsigned int start,
229 unsigned int end, unsigned int width)
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)
239 if (pos & (width - 1))
242 for (i = 1; i < width && !test_bit(pos + i, bitmap); i++)
247 /* step over [pos..pos+i) to continue search */
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)
259 struct stp_master *master;
263 for (midx = *mstart; midx <= mend; midx++) {
264 if (!stm_master(stm, midx)) {
265 err = stp_master_alloc(stm, midx);
270 master = stm_master(stm, midx);
272 if (!master->nr_free)
275 pos = find_free_channels(master->chan_map, *cstart, cend,
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)
292 unsigned int midx, cidx, mend, cend;
295 if (width > stm->data->sw_nchannels)
298 /* We no longer accept policy_node==NULL here */
299 if (WARN_ON_ONCE(!policy_node))
303 * Also, the caller holds reference to policy_node, so it won't
306 stp_policy_node_get_ranges(policy_node, &midx, &mend, &cidx, &cend);
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))
314 ret = stm_find_master_chan(stm, width, &midx, mend, &cidx, cend);
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);
324 if (WARN_ON_ONCE(!priv))
327 /* configfs subsys mutex is held by the caller */
328 ret = stm->pdrv->output_open(priv, output);
333 stm_output_claim(stm, output);
334 dev_dbg(&stm->dev, "assigned %u:%u (+%u)\n", midx, cidx, width);
339 output->nr_chans = 0;
341 spin_unlock(&output->lock);
342 spin_unlock(&stm->mc_lock);
347 static void stm_output_free(struct stm_device *stm, struct stm_output *output)
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);
359 static void stm_output_init(struct stm_output *output)
361 spin_lock_init(&output->lock);
364 static int major_match(struct device *dev, const void *data)
366 unsigned int major = *(unsigned int *)data;
368 return MAJOR(dev->devt) == major;
372 * Framing protocol management
373 * Modules can implement STM protocol drivers and (un-)register them
374 * with the STM class framework.
376 static struct list_head stm_pdrv_head;
377 static struct mutex stm_pdrv_mutex;
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;
385 static const struct stm_pdrv_entry *
386 __stm_lookup_protocol(const char *name)
388 struct stm_pdrv_entry *pe;
391 * If no name is given (NULL or ""), fall back to "p_basic".
396 list_for_each_entry(pe, &stm_pdrv_head, entry) {
397 if (!strcmp(name, pe->pdrv->name))
404 int stm_register_protocol(const struct stm_protocol_driver *pdrv)
406 struct stm_pdrv_entry *pe = NULL;
409 mutex_lock(&stm_pdrv_mutex);
411 if (__stm_lookup_protocol(pdrv->name)) {
416 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
420 if (pdrv->policy_attr) {
421 pe->node_type = get_policy_node_type(pdrv->policy_attr);
426 list_add_tail(&pe->entry, &stm_pdrv_head);
431 mutex_unlock(&stm_pdrv_mutex);
438 EXPORT_SYMBOL_GPL(stm_register_protocol);
440 void stm_unregister_protocol(const struct stm_protocol_driver *pdrv)
442 struct stm_pdrv_entry *pe, *iter;
444 mutex_lock(&stm_pdrv_mutex);
446 list_for_each_entry_safe(pe, iter, &stm_pdrv_head, entry) {
447 if (pe->pdrv == pdrv) {
448 list_del(&pe->entry);
451 kfree(pe->node_type->ct_attrs);
452 kfree(pe->node_type);
459 mutex_unlock(&stm_pdrv_mutex);
461 EXPORT_SYMBOL_GPL(stm_unregister_protocol);
463 static bool stm_get_protocol(const struct stm_protocol_driver *pdrv)
465 return try_module_get(pdrv->owner);
468 void stm_put_protocol(const struct stm_protocol_driver *pdrv)
470 module_put(pdrv->owner);
473 int stm_lookup_protocol(const char *name,
474 const struct stm_protocol_driver **pdrv,
475 const struct config_item_type **node_type)
477 const struct stm_pdrv_entry *pe;
479 mutex_lock(&stm_pdrv_mutex);
481 pe = __stm_lookup_protocol(name);
482 if (pe && pe->pdrv && stm_get_protocol(pe->pdrv)) {
484 *node_type = pe->node_type;
487 mutex_unlock(&stm_pdrv_mutex);
489 return pe ? 0 : -ENOENT;
492 static int stm_char_open(struct inode *inode, struct file *file)
494 struct stm_file *stmf;
496 unsigned int major = imajor(inode);
499 dev = class_find_device(&stm_class, NULL, &major, major_match);
503 stmf = kzalloc(sizeof(*stmf), GFP_KERNEL);
508 stm_output_init(&stmf->output);
509 stmf->stm = to_stm_device(dev);
511 if (!try_module_get(stmf->stm->owner))
514 file->private_data = stmf;
516 return nonseekable_open(inode, file);
521 /* matches class_find_device() above */
527 static int stm_char_release(struct inode *inode, struct file *file)
529 struct stm_file *stmf = file->private_data;
530 struct stm_device *stm = stmf->stm;
532 if (stm->data->unlink)
533 stm->data->unlink(stm->data, stmf->output.master,
534 stmf->output.channel);
536 stm_output_free(stm, &stmf->output);
539 * matches the stm_char_open()'s
540 * class_find_device() + try_module_get()
549 stm_assign_first_policy(struct stm_device *stm, struct stm_output *output,
550 char **ids, unsigned int width)
552 struct stp_policy_node *pn;
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.
561 for (n = 0, pn = NULL; ids[n] && !pn; n++)
562 pn = stp_policy_node_lookup(stm, ids[n]);
567 err = stm_output_assign(stm, width, pn, output);
569 stp_policy_node_put(pn);
575 * stm_data_write() - send the given payload as data packets
576 * @data: stm driver's data
579 * @ts_first: timestamp the first packet
580 * @buf: data payload buffer
581 * @count: data payload size
583 ssize_t notrace stm_data_write(struct stm_data *data, unsigned int m,
584 unsigned int c, bool ts_first, const void *buf,
587 unsigned int flags = ts_first ? STP_PACKET_TIMESTAMPED : 0;
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,
604 return sz < 0 ? sz : pos;
606 EXPORT_SYMBOL_GPL(stm_data_write);
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)
614 /* stm->pdrv is serialized against policy_mutex */
618 err = stm->pdrv->write(stm->data, output, chan, buf, count);
625 static ssize_t stm_char_write(struct file *file, const char __user *buf,
626 size_t count, loff_t *ppos)
628 struct stm_file *stmf = file->private_data;
629 struct stm_device *stm = stmf->stm;
633 if (count + 1 > PAGE_SIZE)
634 count = PAGE_SIZE - 1;
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.
640 if (!stmf->output.nr_chans) {
641 char comm[sizeof(current->comm)];
642 char *ids[] = { comm, "default", NULL };
644 get_task_comm(comm, current);
646 err = stm_assign_first_policy(stmf->stm, &stmf->output, ids, 1);
648 * EBUSY means that somebody else just assigned this
649 * output, which is just fine for write()
655 kbuf = kmalloc(count + 1, GFP_KERNEL);
659 err = copy_from_user(kbuf, buf, count);
665 pm_runtime_get_sync(&stm->dev);
667 count = stm_write(stm, &stmf->output, 0, kbuf, count);
669 pm_runtime_mark_last_busy(&stm->dev);
670 pm_runtime_put_autosuspend(&stm->dev);
676 static void stm_mmap_open(struct vm_area_struct *vma)
678 struct stm_file *stmf = vma->vm_file->private_data;
679 struct stm_device *stm = stmf->stm;
681 pm_runtime_get(&stm->dev);
684 static void stm_mmap_close(struct vm_area_struct *vma)
686 struct stm_file *stmf = vma->vm_file->private_data;
687 struct stm_device *stm = stmf->stm;
689 pm_runtime_mark_last_busy(&stm->dev);
690 pm_runtime_put_autosuspend(&stm->dev);
693 static const struct vm_operations_struct stm_mmap_vmops = {
694 .open = stm_mmap_open,
695 .close = stm_mmap_close,
698 static int stm_char_mmap(struct file *file, struct vm_area_struct *vma)
700 struct stm_file *stmf = file->private_data;
701 struct stm_device *stm = stmf->stm;
702 unsigned long size, phys;
704 if (!stm->data->mmio_addr)
710 size = vma->vm_end - vma->vm_start;
712 if (stmf->output.nr_chans * stm->data->sw_mmiosz != size)
715 phys = stm->data->mmio_addr(stm->data, stmf->output.master,
716 stmf->output.channel,
717 stmf->output.nr_chans);
722 pm_runtime_get_sync(&stm->dev);
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);
732 static int stm_char_policy_set_ioctl(struct stm_file *stmf, void __user *arg)
734 struct stm_device *stm = stmf->stm;
735 struct stp_policy_id *id;
736 char *ids[] = { NULL, NULL };
737 int ret = -EINVAL, wlimit = 1;
740 if (stmf->output.nr_chans)
743 if (copy_from_user(&size, arg, sizeof(size)))
746 if (size < sizeof(*id) || size >= PATH_MAX + sizeof(*id))
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
753 id = kzalloc(size + 1, GFP_KERNEL);
757 if (copy_from_user(id, arg, size)) {
762 if (id->__reserved_0 || id->__reserved_1)
765 if (stm->data->sw_mmiosz)
766 wlimit = PAGE_SIZE / stm->data->sw_mmiosz;
768 if (id->width < 1 || id->width > wlimit)
772 ret = stm_assign_first_policy(stmf->stm, &stmf->output, ids,
778 ret = stm->data->link(stm->data, stmf->output.master,
779 stmf->output.channel);
782 stm_output_free(stmf->stm, &stmf->output);
790 static int stm_char_policy_get_ioctl(struct stm_file *stmf, void __user *arg)
792 struct stp_policy_id id = {
794 .master = stmf->output.master,
795 .channel = stmf->output.channel,
796 .width = stmf->output.nr_chans,
801 return copy_to_user(arg, &id, id.size) ? -EFAULT : 0;
805 stm_char_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
807 struct stm_file *stmf = file->private_data;
808 struct stm_data *stm_data = stmf->stm->data;
813 case STP_POLICY_ID_SET:
814 err = stm_char_policy_set_ioctl(stmf, (void __user *)arg);
818 return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
820 case STP_POLICY_ID_GET:
821 return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
823 case STP_SET_OPTIONS:
824 if (copy_from_user(&options, (u64 __user *)arg, sizeof(u64)))
827 if (stm_data->set_options)
828 err = stm_data->set_options(stm_data,
830 stmf->output.channel,
831 stmf->output.nr_chans,
844 stm_char_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
846 return stm_char_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
849 #define stm_char_compat_ioctl NULL
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,
862 static void stm_device_release(struct device *dev)
864 struct stm_device *stm = to_stm_device(dev);
869 int stm_register_device(struct device *parent, struct stm_data *stm_data,
870 struct module *owner)
872 struct stm_device *stm;
873 unsigned int nmasters;
877 return -EPROBE_DEFER;
879 if (!stm_data->packet || !stm_data->sw_nchannels)
882 nmasters = stm_data->sw_end - stm_data->sw_start + 1;
883 stm = vzalloc(sizeof(*stm) + nmasters * sizeof(void *));
887 stm->major = register_chrdev(0, stm_data->name, &stm_fops);
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;
897 mutex_init(&stm->link_mutex);
898 spin_lock_init(&stm->link_lock);
899 INIT_LIST_HEAD(&stm->link_list);
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;
906 stm->data = stm_data;
909 err = kobject_set_name(&stm->dev.kobj, "%s", stm_data->name);
913 err = device_add(&stm->dev);
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.
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);
931 unregister_chrdev(stm->major, stm_data->name);
933 /* matches device_initialize() above */
934 put_device(&stm->dev);
940 EXPORT_SYMBOL_GPL(stm_register_device);
942 static int __stm_source_link_drop(struct stm_source_device *src,
943 struct stm_device *stm);
945 void stm_unregister_device(struct stm_data *stm_data)
947 struct stm_device *stm = stm_data->stm;
948 struct stm_source_device *src, *iter;
951 pm_runtime_dont_use_autosuspend(&stm->dev);
952 pm_runtime_disable(&stm->dev);
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);
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.
966 mutex_unlock(&stm->link_mutex);
968 synchronize_srcu(&stm_source_srcu);
970 unregister_chrdev(stm->major, stm_data->name);
972 mutex_lock(&stm->policy_mutex);
974 stp_policy_unbind(stm->policy);
975 mutex_unlock(&stm->policy_mutex);
977 for (i = stm->data->sw_start; i <= stm->data->sw_end; i++)
978 stp_master_free(stm, i);
980 device_unregister(&stm->dev);
981 stm_data->stm = NULL;
983 EXPORT_SYMBOL_GPL(stm_unregister_device);
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.
990 * Lock ordering is as follows:
997 * stm_source_link_add() - connect an stm_source device to an stm device
998 * @src: stm_source device
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.
1004 * Return: 0 on success, -errno otherwise.
1006 static int stm_source_link_add(struct stm_source_device *src,
1007 struct stm_device *stm)
1009 char *ids[] = { NULL, "default", NULL };
1012 mutex_lock(&stm->link_mutex);
1013 spin_lock(&stm->link_lock);
1014 spin_lock(&src->link_lock);
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);
1020 spin_unlock(&src->link_lock);
1021 spin_unlock(&stm->link_lock);
1022 mutex_unlock(&stm->link_mutex);
1024 ids[0] = kstrdup(src->data->name, GFP_KERNEL);
1028 err = stm_assign_first_policy(stm, &src->output, ids,
1029 src->data->nr_chans);
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);
1041 goto fail_free_output;
1043 /* this is to let the source carry out all necessary preparations */
1044 if (src->data->link)
1045 src->data->link(src->data);
1050 stm_output_free(stm, &src->output);
1053 mutex_lock(&stm->link_mutex);
1054 spin_lock(&stm->link_lock);
1055 spin_lock(&src->link_lock);
1057 rcu_assign_pointer(src->link, NULL);
1058 list_del_init(&src->link_entry);
1060 spin_unlock(&src->link_lock);
1061 spin_unlock(&stm->link_lock);
1062 mutex_unlock(&stm->link_mutex);
1068 * __stm_source_link_drop() - detach stm_source from an stm device
1069 * @src: stm_source device
1072 * If @stm is @src::link, disconnect them from one another and put the
1073 * reference on the @stm device.
1075 * Caller must hold stm::link_mutex.
1077 static int __stm_source_link_drop(struct stm_source_device *src,
1078 struct stm_device *stm)
1080 struct stm_device *link;
1083 lockdep_assert_held(&stm->link_mutex);
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);
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.
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);
1109 spin_unlock(&src->link_lock);
1110 spin_unlock(&stm->link_lock);
1113 * Call the unlink callbacks for both source and stm, when we know
1114 * that we have actually performed the unlinking.
1117 if (src->data->unlink)
1118 src->data->unlink(src->data);
1120 if (stm->data->unlink)
1121 stm->data->unlink(stm->data, src->output.master,
1122 src->output.channel);
1129 * stm_source_link_drop() - detach stm_source from its stm device
1130 * @src: stm_source device
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.
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.
1138 static void stm_source_link_drop(struct stm_source_device *src)
1140 struct stm_device *stm;
1144 idx = srcu_read_lock(&stm_source_srcu);
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().
1150 stm = srcu_dereference(src->link, &stm_source_srcu);
1154 mutex_lock(&stm->link_mutex);
1155 ret = __stm_source_link_drop(src, stm);
1156 mutex_unlock(&stm->link_mutex);
1159 srcu_read_unlock(&stm_source_srcu, idx);
1161 /* if it did change, retry */
1166 static ssize_t stm_source_link_show(struct device *dev,
1167 struct device_attribute *attr,
1170 struct stm_source_device *src = to_stm_source_device(dev);
1171 struct stm_device *stm;
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);
1183 static ssize_t stm_source_link_store(struct device *dev,
1184 struct device_attribute *attr,
1185 const char *buf, size_t count)
1187 struct stm_source_device *src = to_stm_source_device(dev);
1188 struct stm_device *link;
1191 stm_source_link_drop(src);
1193 link = stm_find_device(buf);
1197 pm_runtime_get(&link->dev);
1199 err = stm_source_link_add(src, link);
1201 pm_runtime_put_autosuspend(&link->dev);
1202 /* matches the stm_find_device() above */
1203 stm_put_device(link);
1206 return err ? : count;
1209 static DEVICE_ATTR_RW(stm_source_link);
1211 static struct attribute *stm_source_attrs[] = {
1212 &dev_attr_stm_source_link.attr,
1216 ATTRIBUTE_GROUPS(stm_source);
1218 static struct class stm_source_class = {
1219 .name = "stm_source",
1220 .dev_groups = stm_source_groups,
1223 static void stm_source_device_release(struct device *dev)
1225 struct stm_source_device *src = to_stm_source_device(dev);
1231 * stm_source_register_device() - register an stm_source device
1232 * @parent: parent device
1233 * @data: device description structure
1235 * This will create a device of stm_source class that can write
1236 * data to an stm device once linked.
1238 * Return: 0 on success, -errno otherwise.
1240 int stm_source_register_device(struct device *parent,
1241 struct stm_source_data *data)
1243 struct stm_source_device *src;
1247 return -EPROBE_DEFER;
1249 src = kzalloc(sizeof(*src), GFP_KERNEL);
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;
1258 err = kobject_set_name(&src->dev.kobj, "%s", data->name);
1262 pm_runtime_no_callbacks(&src->dev);
1263 pm_runtime_forbid(&src->dev);
1265 err = device_add(&src->dev);
1269 stm_output_init(&src->output);
1270 spin_lock_init(&src->link_lock);
1271 INIT_LIST_HEAD(&src->link_entry);
1278 put_device(&src->dev);
1282 EXPORT_SYMBOL_GPL(stm_source_register_device);
1285 * stm_source_unregister_device() - unregister an stm_source device
1286 * @data: device description that was used to register the device
1288 * This will remove a previously created stm_source device from the system.
1290 void stm_source_unregister_device(struct stm_source_data *data)
1292 struct stm_source_device *src = data->src;
1294 stm_source_link_drop(src);
1296 device_unregister(&src->dev);
1298 EXPORT_SYMBOL_GPL(stm_source_unregister_device);
1300 int notrace stm_source_write(struct stm_source_data *data,
1302 const char *buf, size_t count)
1304 struct stm_source_device *src = data->src;
1305 struct stm_device *stm;
1308 if (!src->output.nr_chans)
1311 if (chan >= src->output.nr_chans)
1314 idx = srcu_read_lock(&stm_source_srcu);
1316 stm = srcu_dereference(src->link, &stm_source_srcu);
1318 count = stm_write(stm, &src->output, chan, buf, count);
1322 srcu_read_unlock(&stm_source_srcu, idx);
1326 EXPORT_SYMBOL_GPL(stm_source_write);
1328 static int __init stm_core_init(void)
1332 err = class_register(&stm_class);
1336 err = class_register(&stm_source_class);
1340 err = stp_configfs_init();
1344 init_srcu_struct(&stm_source_srcu);
1345 INIT_LIST_HEAD(&stm_pdrv_head);
1346 mutex_init(&stm_pdrv_mutex);
1349 * So as to not confuse existing users with a requirement
1350 * to load yet another module, do it here.
1352 if (IS_ENABLED(CONFIG_STM_PROTO_BASIC))
1353 (void)request_module_nowait("stm_p_basic");
1359 class_unregister(&stm_source_class);
1361 class_unregister(&stm_class);
1366 module_init(stm_core_init);
1368 static void __exit stm_core_exit(void)
1370 cleanup_srcu_struct(&stm_source_srcu);
1371 class_unregister(&stm_source_class);
1372 class_unregister(&stm_class);
1373 stp_configfs_exit();
1376 module_exit(stm_core_exit);
1378 MODULE_LICENSE("GPL v2");
1379 MODULE_DESCRIPTION("System Trace Module device class");