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;
171 size = ALIGN(stm->data->sw_nchannels, 8) / 8;
172 size += sizeof(struct stp_master);
173 master = kzalloc(size, GFP_ATOMIC);
177 master->nr_free = stm->data->sw_nchannels;
178 __stm_master(stm, idx) = master;
183 static void stp_master_free(struct stm_device *stm, unsigned int idx)
185 struct stp_master *master = stm_master(stm, idx);
190 __stm_master(stm, idx) = NULL;
194 static void stm_output_claim(struct stm_device *stm, struct stm_output *output)
196 struct stp_master *master = stm_master(stm, output->master);
198 lockdep_assert_held(&stm->mc_lock);
199 lockdep_assert_held(&output->lock);
201 if (WARN_ON_ONCE(master->nr_free < output->nr_chans))
204 bitmap_allocate_region(&master->chan_map[0], output->channel,
205 ilog2(output->nr_chans));
207 master->nr_free -= output->nr_chans;
211 stm_output_disclaim(struct stm_device *stm, struct stm_output *output)
213 struct stp_master *master = stm_master(stm, output->master);
215 lockdep_assert_held(&stm->mc_lock);
216 lockdep_assert_held(&output->lock);
218 bitmap_release_region(&master->chan_map[0], output->channel,
219 ilog2(output->nr_chans));
221 output->nr_chans = 0;
222 master->nr_free += output->nr_chans;
226 * This is like bitmap_find_free_region(), except it can ignore @start bits
229 static int find_free_channels(unsigned long *bitmap, unsigned int start,
230 unsigned int end, unsigned int width)
235 for (pos = start; pos < end + 1; pos = ALIGN(pos, width)) {
236 pos = find_next_zero_bit(bitmap, end + 1, pos);
237 if (pos + width > end + 1)
240 if (pos & (width - 1))
243 for (i = 1; i < width && !test_bit(pos + i, bitmap); i++)
248 /* step over [pos..pos+i) to continue search */
256 stm_find_master_chan(struct stm_device *stm, unsigned int width,
257 unsigned int *mstart, unsigned int mend,
258 unsigned int *cstart, unsigned int cend)
260 struct stp_master *master;
264 for (midx = *mstart; midx <= mend; midx++) {
265 if (!stm_master(stm, midx)) {
266 err = stp_master_alloc(stm, midx);
271 master = stm_master(stm, midx);
273 if (!master->nr_free)
276 pos = find_free_channels(master->chan_map, *cstart, cend,
289 static int stm_output_assign(struct stm_device *stm, unsigned int width,
290 struct stp_policy_node *policy_node,
291 struct stm_output *output)
293 unsigned int midx, cidx, mend, cend;
296 if (width > stm->data->sw_nchannels)
299 /* We no longer accept policy_node==NULL here */
300 if (WARN_ON_ONCE(!policy_node))
304 * Also, the caller holds reference to policy_node, so it won't
307 stp_policy_node_get_ranges(policy_node, &midx, &mend, &cidx, &cend);
309 spin_lock(&stm->mc_lock);
310 spin_lock(&output->lock);
311 /* output is already assigned -- shouldn't happen */
312 if (WARN_ON_ONCE(output->nr_chans))
315 ret = stm_find_master_chan(stm, width, &midx, mend, &cidx, cend);
319 output->master = midx;
320 output->channel = cidx;
321 output->nr_chans = width;
322 if (stm->pdrv->output_open) {
323 void *priv = stp_policy_node_priv(policy_node);
325 if (WARN_ON_ONCE(!priv))
328 /* configfs subsys mutex is held by the caller */
329 ret = stm->pdrv->output_open(priv, output);
334 stm_output_claim(stm, output);
335 dev_dbg(&stm->dev, "assigned %u:%u (+%u)\n", midx, cidx, width);
340 output->nr_chans = 0;
342 spin_unlock(&output->lock);
343 spin_unlock(&stm->mc_lock);
348 static void stm_output_free(struct stm_device *stm, struct stm_output *output)
350 spin_lock(&stm->mc_lock);
351 spin_lock(&output->lock);
352 if (output->nr_chans)
353 stm_output_disclaim(stm, output);
354 if (stm->pdrv && stm->pdrv->output_close)
355 stm->pdrv->output_close(output);
356 spin_unlock(&output->lock);
357 spin_unlock(&stm->mc_lock);
360 static void stm_output_init(struct stm_output *output)
362 spin_lock_init(&output->lock);
365 static int major_match(struct device *dev, const void *data)
367 unsigned int major = *(unsigned int *)data;
369 return MAJOR(dev->devt) == major;
373 * Framing protocol management
374 * Modules can implement STM protocol drivers and (un-)register them
375 * with the STM class framework.
377 static struct list_head stm_pdrv_head;
378 static struct mutex stm_pdrv_mutex;
380 struct stm_pdrv_entry {
381 struct list_head entry;
382 const struct stm_protocol_driver *pdrv;
383 const struct config_item_type *node_type;
386 static const struct stm_pdrv_entry *
387 __stm_lookup_protocol(const char *name)
389 struct stm_pdrv_entry *pe;
392 * If no name is given (NULL or ""), fall back to "p_basic".
397 list_for_each_entry(pe, &stm_pdrv_head, entry) {
398 if (!strcmp(name, pe->pdrv->name))
405 int stm_register_protocol(const struct stm_protocol_driver *pdrv)
407 struct stm_pdrv_entry *pe = NULL;
410 mutex_lock(&stm_pdrv_mutex);
412 if (__stm_lookup_protocol(pdrv->name)) {
417 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
421 if (pdrv->policy_attr) {
422 pe->node_type = get_policy_node_type(pdrv->policy_attr);
427 list_add_tail(&pe->entry, &stm_pdrv_head);
432 mutex_unlock(&stm_pdrv_mutex);
439 EXPORT_SYMBOL_GPL(stm_register_protocol);
441 void stm_unregister_protocol(const struct stm_protocol_driver *pdrv)
443 struct stm_pdrv_entry *pe, *iter;
445 mutex_lock(&stm_pdrv_mutex);
447 list_for_each_entry_safe(pe, iter, &stm_pdrv_head, entry) {
448 if (pe->pdrv == pdrv) {
449 list_del(&pe->entry);
452 kfree(pe->node_type->ct_attrs);
453 kfree(pe->node_type);
460 mutex_unlock(&stm_pdrv_mutex);
462 EXPORT_SYMBOL_GPL(stm_unregister_protocol);
464 static bool stm_get_protocol(const struct stm_protocol_driver *pdrv)
466 return try_module_get(pdrv->owner);
469 void stm_put_protocol(const struct stm_protocol_driver *pdrv)
471 module_put(pdrv->owner);
474 int stm_lookup_protocol(const char *name,
475 const struct stm_protocol_driver **pdrv,
476 const struct config_item_type **node_type)
478 const struct stm_pdrv_entry *pe;
480 mutex_lock(&stm_pdrv_mutex);
482 pe = __stm_lookup_protocol(name);
483 if (pe && pe->pdrv && stm_get_protocol(pe->pdrv)) {
485 *node_type = pe->node_type;
488 mutex_unlock(&stm_pdrv_mutex);
490 return pe ? 0 : -ENOENT;
493 static int stm_char_open(struct inode *inode, struct file *file)
495 struct stm_file *stmf;
497 unsigned int major = imajor(inode);
500 dev = class_find_device(&stm_class, NULL, &major, major_match);
504 stmf = kzalloc(sizeof(*stmf), GFP_KERNEL);
509 stm_output_init(&stmf->output);
510 stmf->stm = to_stm_device(dev);
512 if (!try_module_get(stmf->stm->owner))
515 file->private_data = stmf;
517 return nonseekable_open(inode, file);
522 /* matches class_find_device() above */
528 static int stm_char_release(struct inode *inode, struct file *file)
530 struct stm_file *stmf = file->private_data;
531 struct stm_device *stm = stmf->stm;
533 if (stm->data->unlink)
534 stm->data->unlink(stm->data, stmf->output.master,
535 stmf->output.channel);
537 stm_output_free(stm, &stmf->output);
540 * matches the stm_char_open()'s
541 * class_find_device() + try_module_get()
550 stm_assign_first_policy(struct stm_device *stm, struct stm_output *output,
551 char **ids, unsigned int width)
553 struct stp_policy_node *pn;
557 * On success, stp_policy_node_lookup() will return holding the
558 * configfs subsystem mutex, which is then released in
559 * stp_policy_node_put(). This allows the pdrv->output_open() in
560 * stm_output_assign() to serialize against the attribute accessors.
562 for (n = 0, pn = NULL; ids[n] && !pn; n++)
563 pn = stp_policy_node_lookup(stm, ids[n]);
568 err = stm_output_assign(stm, width, pn, output);
570 stp_policy_node_put(pn);
576 * stm_data_write() - send the given payload as data packets
577 * @data: stm driver's data
580 * @ts_first: timestamp the first packet
581 * @buf: data payload buffer
582 * @count: data payload size
584 ssize_t notrace stm_data_write(struct stm_data *data, unsigned int m,
585 unsigned int c, bool ts_first, const void *buf,
588 unsigned int flags = ts_first ? STP_PACKET_TIMESTAMPED : 0;
592 for (pos = 0, sz = 0; pos < count; pos += sz) {
593 sz = min_t(unsigned int, count - pos, 8);
594 sz = data->packet(data, m, c, STP_PACKET_DATA, flags, sz,
605 return sz < 0 ? sz : pos;
607 EXPORT_SYMBOL_GPL(stm_data_write);
609 static ssize_t notrace
610 stm_write(struct stm_device *stm, struct stm_output *output,
611 unsigned int chan, const char *buf, size_t count)
615 /* stm->pdrv is serialized against policy_mutex */
619 err = stm->pdrv->write(stm->data, output, chan, buf, count);
626 static ssize_t stm_char_write(struct file *file, const char __user *buf,
627 size_t count, loff_t *ppos)
629 struct stm_file *stmf = file->private_data;
630 struct stm_device *stm = stmf->stm;
634 if (count + 1 > PAGE_SIZE)
635 count = PAGE_SIZE - 1;
638 * If no m/c have been assigned to this writer up to this
639 * point, try to use the task name and "default" policy entries.
641 if (!stmf->output.nr_chans) {
642 char comm[sizeof(current->comm)];
643 char *ids[] = { comm, "default", NULL };
645 get_task_comm(comm, current);
647 err = stm_assign_first_policy(stmf->stm, &stmf->output, ids, 1);
649 * EBUSY means that somebody else just assigned this
650 * output, which is just fine for write()
656 kbuf = kmalloc(count + 1, GFP_KERNEL);
660 err = copy_from_user(kbuf, buf, count);
666 pm_runtime_get_sync(&stm->dev);
668 count = stm_write(stm, &stmf->output, 0, kbuf, count);
670 pm_runtime_mark_last_busy(&stm->dev);
671 pm_runtime_put_autosuspend(&stm->dev);
677 static void stm_mmap_open(struct vm_area_struct *vma)
679 struct stm_file *stmf = vma->vm_file->private_data;
680 struct stm_device *stm = stmf->stm;
682 pm_runtime_get(&stm->dev);
685 static void stm_mmap_close(struct vm_area_struct *vma)
687 struct stm_file *stmf = vma->vm_file->private_data;
688 struct stm_device *stm = stmf->stm;
690 pm_runtime_mark_last_busy(&stm->dev);
691 pm_runtime_put_autosuspend(&stm->dev);
694 static const struct vm_operations_struct stm_mmap_vmops = {
695 .open = stm_mmap_open,
696 .close = stm_mmap_close,
699 static int stm_char_mmap(struct file *file, struct vm_area_struct *vma)
701 struct stm_file *stmf = file->private_data;
702 struct stm_device *stm = stmf->stm;
703 unsigned long size, phys;
705 if (!stm->data->mmio_addr)
711 size = vma->vm_end - vma->vm_start;
713 if (stmf->output.nr_chans * stm->data->sw_mmiosz != size)
716 phys = stm->data->mmio_addr(stm->data, stmf->output.master,
717 stmf->output.channel,
718 stmf->output.nr_chans);
723 pm_runtime_get_sync(&stm->dev);
725 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
726 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
727 vma->vm_ops = &stm_mmap_vmops;
728 vm_iomap_memory(vma, phys, size);
733 static int stm_char_policy_set_ioctl(struct stm_file *stmf, void __user *arg)
735 struct stm_device *stm = stmf->stm;
736 struct stp_policy_id *id;
737 char *ids[] = { NULL, NULL };
741 if (stmf->output.nr_chans)
744 if (copy_from_user(&size, arg, sizeof(size)))
747 if (size < sizeof(*id) || size >= PATH_MAX + sizeof(*id))
751 * size + 1 to make sure the .id string at the bottom is terminated,
752 * which is also why memdup_user() is not useful here
754 id = kzalloc(size + 1, GFP_KERNEL);
758 if (copy_from_user(id, arg, size)) {
763 if (id->__reserved_0 || id->__reserved_1)
767 id->width > PAGE_SIZE / stm->data->sw_mmiosz)
771 ret = stm_assign_first_policy(stmf->stm, &stmf->output, ids,
777 ret = stm->data->link(stm->data, stmf->output.master,
778 stmf->output.channel);
781 stm_output_free(stmf->stm, &stmf->output);
789 static int stm_char_policy_get_ioctl(struct stm_file *stmf, void __user *arg)
791 struct stp_policy_id id = {
793 .master = stmf->output.master,
794 .channel = stmf->output.channel,
795 .width = stmf->output.nr_chans,
800 return copy_to_user(arg, &id, id.size) ? -EFAULT : 0;
804 stm_char_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
806 struct stm_file *stmf = file->private_data;
807 struct stm_data *stm_data = stmf->stm->data;
812 case STP_POLICY_ID_SET:
813 err = stm_char_policy_set_ioctl(stmf, (void __user *)arg);
817 return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
819 case STP_POLICY_ID_GET:
820 return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
822 case STP_SET_OPTIONS:
823 if (copy_from_user(&options, (u64 __user *)arg, sizeof(u64)))
826 if (stm_data->set_options)
827 err = stm_data->set_options(stm_data,
829 stmf->output.channel,
830 stmf->output.nr_chans,
843 stm_char_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
845 return stm_char_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
848 #define stm_char_compat_ioctl NULL
851 static const struct file_operations stm_fops = {
852 .open = stm_char_open,
853 .release = stm_char_release,
854 .write = stm_char_write,
855 .mmap = stm_char_mmap,
856 .unlocked_ioctl = stm_char_ioctl,
857 .compat_ioctl = stm_char_compat_ioctl,
861 static void stm_device_release(struct device *dev)
863 struct stm_device *stm = to_stm_device(dev);
868 int stm_register_device(struct device *parent, struct stm_data *stm_data,
869 struct module *owner)
871 struct stm_device *stm;
872 unsigned int nmasters;
876 return -EPROBE_DEFER;
878 if (!stm_data->packet || !stm_data->sw_nchannels)
881 nmasters = stm_data->sw_end - stm_data->sw_start + 1;
882 stm = vzalloc(sizeof(*stm) + nmasters * sizeof(void *));
886 stm->major = register_chrdev(0, stm_data->name, &stm_fops);
890 device_initialize(&stm->dev);
891 stm->dev.devt = MKDEV(stm->major, 0);
892 stm->dev.class = &stm_class;
893 stm->dev.parent = parent;
894 stm->dev.release = stm_device_release;
896 mutex_init(&stm->link_mutex);
897 spin_lock_init(&stm->link_lock);
898 INIT_LIST_HEAD(&stm->link_list);
900 /* initialize the object before it is accessible via sysfs */
901 spin_lock_init(&stm->mc_lock);
902 mutex_init(&stm->policy_mutex);
903 stm->sw_nmasters = nmasters;
905 stm->data = stm_data;
908 err = kobject_set_name(&stm->dev.kobj, "%s", stm_data->name);
912 err = device_add(&stm->dev);
917 * Use delayed autosuspend to avoid bouncing back and forth
918 * on recurring character device writes, with the initial
919 * delay time of 2 seconds.
921 pm_runtime_no_callbacks(&stm->dev);
922 pm_runtime_use_autosuspend(&stm->dev);
923 pm_runtime_set_autosuspend_delay(&stm->dev, 2000);
924 pm_runtime_set_suspended(&stm->dev);
925 pm_runtime_enable(&stm->dev);
930 unregister_chrdev(stm->major, stm_data->name);
932 /* matches device_initialize() above */
933 put_device(&stm->dev);
939 EXPORT_SYMBOL_GPL(stm_register_device);
941 static int __stm_source_link_drop(struct stm_source_device *src,
942 struct stm_device *stm);
944 void stm_unregister_device(struct stm_data *stm_data)
946 struct stm_device *stm = stm_data->stm;
947 struct stm_source_device *src, *iter;
950 pm_runtime_dont_use_autosuspend(&stm->dev);
951 pm_runtime_disable(&stm->dev);
953 mutex_lock(&stm->link_mutex);
954 list_for_each_entry_safe(src, iter, &stm->link_list, link_entry) {
955 ret = __stm_source_link_drop(src, stm);
957 * src <-> stm link must not change under the same
958 * stm::link_mutex, so complain loudly if it has;
959 * also in this situation ret!=0 means this src is
960 * not connected to this stm and it should be otherwise
961 * safe to proceed with the tear-down of stm.
965 mutex_unlock(&stm->link_mutex);
967 synchronize_srcu(&stm_source_srcu);
969 unregister_chrdev(stm->major, stm_data->name);
971 mutex_lock(&stm->policy_mutex);
973 stp_policy_unbind(stm->policy);
974 mutex_unlock(&stm->policy_mutex);
976 for (i = stm->data->sw_start; i <= stm->data->sw_end; i++)
977 stp_master_free(stm, i);
979 device_unregister(&stm->dev);
980 stm_data->stm = NULL;
982 EXPORT_SYMBOL_GPL(stm_unregister_device);
985 * stm::link_list access serialization uses a spinlock and a mutex; holding
986 * either of them guarantees that the list is stable; modification requires
987 * holding both of them.
989 * Lock ordering is as follows:
996 * stm_source_link_add() - connect an stm_source device to an stm device
997 * @src: stm_source device
1000 * This function establishes a link from stm_source to an stm device so that
1001 * the former can send out trace data to the latter.
1003 * Return: 0 on success, -errno otherwise.
1005 static int stm_source_link_add(struct stm_source_device *src,
1006 struct stm_device *stm)
1008 char *ids[] = { NULL, "default", NULL };
1011 mutex_lock(&stm->link_mutex);
1012 spin_lock(&stm->link_lock);
1013 spin_lock(&src->link_lock);
1015 /* src->link is dereferenced under stm_source_srcu but not the list */
1016 rcu_assign_pointer(src->link, stm);
1017 list_add_tail(&src->link_entry, &stm->link_list);
1019 spin_unlock(&src->link_lock);
1020 spin_unlock(&stm->link_lock);
1021 mutex_unlock(&stm->link_mutex);
1023 ids[0] = kstrdup(src->data->name, GFP_KERNEL);
1027 err = stm_assign_first_policy(stm, &src->output, ids,
1028 src->data->nr_chans);
1034 /* this is to notify the STM device that a new link has been made */
1035 if (stm->data->link)
1036 err = stm->data->link(stm->data, src->output.master,
1037 src->output.channel);
1040 goto fail_free_output;
1042 /* this is to let the source carry out all necessary preparations */
1043 if (src->data->link)
1044 src->data->link(src->data);
1049 stm_output_free(stm, &src->output);
1052 mutex_lock(&stm->link_mutex);
1053 spin_lock(&stm->link_lock);
1054 spin_lock(&src->link_lock);
1056 rcu_assign_pointer(src->link, NULL);
1057 list_del_init(&src->link_entry);
1059 spin_unlock(&src->link_lock);
1060 spin_unlock(&stm->link_lock);
1061 mutex_unlock(&stm->link_mutex);
1067 * __stm_source_link_drop() - detach stm_source from an stm device
1068 * @src: stm_source device
1071 * If @stm is @src::link, disconnect them from one another and put the
1072 * reference on the @stm device.
1074 * Caller must hold stm::link_mutex.
1076 static int __stm_source_link_drop(struct stm_source_device *src,
1077 struct stm_device *stm)
1079 struct stm_device *link;
1082 lockdep_assert_held(&stm->link_mutex);
1084 /* for stm::link_list modification, we hold both mutex and spinlock */
1085 spin_lock(&stm->link_lock);
1086 spin_lock(&src->link_lock);
1087 link = srcu_dereference_check(src->link, &stm_source_srcu, 1);
1090 * The linked device may have changed since we last looked, because
1091 * we weren't holding the src::link_lock back then; if this is the
1092 * case, tell the caller to retry.
1099 stm_output_free(link, &src->output);
1100 list_del_init(&src->link_entry);
1101 pm_runtime_mark_last_busy(&link->dev);
1102 pm_runtime_put_autosuspend(&link->dev);
1103 /* matches stm_find_device() from stm_source_link_store() */
1104 stm_put_device(link);
1105 rcu_assign_pointer(src->link, NULL);
1108 spin_unlock(&src->link_lock);
1109 spin_unlock(&stm->link_lock);
1112 * Call the unlink callbacks for both source and stm, when we know
1113 * that we have actually performed the unlinking.
1116 if (src->data->unlink)
1117 src->data->unlink(src->data);
1119 if (stm->data->unlink)
1120 stm->data->unlink(stm->data, src->output.master,
1121 src->output.channel);
1128 * stm_source_link_drop() - detach stm_source from its stm device
1129 * @src: stm_source device
1131 * Unlinking means disconnecting from source's STM device; after this
1132 * writes will be unsuccessful until it is linked to a new STM device.
1134 * This will happen on "stm_source_link" sysfs attribute write to undo
1135 * the existing link (if any), or on linked STM device's de-registration.
1137 static void stm_source_link_drop(struct stm_source_device *src)
1139 struct stm_device *stm;
1143 idx = srcu_read_lock(&stm_source_srcu);
1145 * The stm device will be valid for the duration of this
1146 * read section, but the link may change before we grab
1147 * the src::link_lock in __stm_source_link_drop().
1149 stm = srcu_dereference(src->link, &stm_source_srcu);
1153 mutex_lock(&stm->link_mutex);
1154 ret = __stm_source_link_drop(src, stm);
1155 mutex_unlock(&stm->link_mutex);
1158 srcu_read_unlock(&stm_source_srcu, idx);
1160 /* if it did change, retry */
1165 static ssize_t stm_source_link_show(struct device *dev,
1166 struct device_attribute *attr,
1169 struct stm_source_device *src = to_stm_source_device(dev);
1170 struct stm_device *stm;
1173 idx = srcu_read_lock(&stm_source_srcu);
1174 stm = srcu_dereference(src->link, &stm_source_srcu);
1175 ret = sprintf(buf, "%s\n",
1176 stm ? dev_name(&stm->dev) : "<none>");
1177 srcu_read_unlock(&stm_source_srcu, idx);
1182 static ssize_t stm_source_link_store(struct device *dev,
1183 struct device_attribute *attr,
1184 const char *buf, size_t count)
1186 struct stm_source_device *src = to_stm_source_device(dev);
1187 struct stm_device *link;
1190 stm_source_link_drop(src);
1192 link = stm_find_device(buf);
1196 pm_runtime_get(&link->dev);
1198 err = stm_source_link_add(src, link);
1200 pm_runtime_put_autosuspend(&link->dev);
1201 /* matches the stm_find_device() above */
1202 stm_put_device(link);
1205 return err ? : count;
1208 static DEVICE_ATTR_RW(stm_source_link);
1210 static struct attribute *stm_source_attrs[] = {
1211 &dev_attr_stm_source_link.attr,
1215 ATTRIBUTE_GROUPS(stm_source);
1217 static struct class stm_source_class = {
1218 .name = "stm_source",
1219 .dev_groups = stm_source_groups,
1222 static void stm_source_device_release(struct device *dev)
1224 struct stm_source_device *src = to_stm_source_device(dev);
1230 * stm_source_register_device() - register an stm_source device
1231 * @parent: parent device
1232 * @data: device description structure
1234 * This will create a device of stm_source class that can write
1235 * data to an stm device once linked.
1237 * Return: 0 on success, -errno otherwise.
1239 int stm_source_register_device(struct device *parent,
1240 struct stm_source_data *data)
1242 struct stm_source_device *src;
1246 return -EPROBE_DEFER;
1248 src = kzalloc(sizeof(*src), GFP_KERNEL);
1252 device_initialize(&src->dev);
1253 src->dev.class = &stm_source_class;
1254 src->dev.parent = parent;
1255 src->dev.release = stm_source_device_release;
1257 err = kobject_set_name(&src->dev.kobj, "%s", data->name);
1261 pm_runtime_no_callbacks(&src->dev);
1262 pm_runtime_forbid(&src->dev);
1264 err = device_add(&src->dev);
1268 stm_output_init(&src->output);
1269 spin_lock_init(&src->link_lock);
1270 INIT_LIST_HEAD(&src->link_entry);
1277 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");
1380 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");