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
,
93 * stm_find_device() - find stm device by name
94 * @buf: character buffer containing the name
96 * This is called when either policy gets assigned to an stm device or an
97 * stm_source device gets linked to an stm device.
99 * This grabs device's reference (get_device()) and module reference, both
100 * of which the calling path needs to make sure to drop with stm_put_device().
102 * Return: stm device pointer or null if lookup failed.
104 struct stm_device
*stm_find_device(const char *buf
)
106 struct stm_device
*stm
;
112 dev
= class_find_device_by_name(&stm_class
, buf
);
116 stm
= to_stm_device(dev
);
117 if (!try_module_get(stm
->owner
)) {
118 /* matches class_find_device() above */
127 * stm_put_device() - drop references on the stm device
128 * @stm: stm device, previously acquired by stm_find_device()
130 * This drops the module reference and device reference taken by
131 * stm_find_device() or stm_char_open().
133 void stm_put_device(struct stm_device
*stm
)
135 module_put(stm
->owner
);
136 put_device(&stm
->dev
);
140 * Internally we only care about software-writable masters here, that is the
141 * ones in the range [stm_data->sw_start..stm_data..sw_end], however we need
142 * original master numbers to be visible externally, since they are the ones
143 * that will appear in the STP stream. Thus, the internal bookkeeping uses
144 * $master - stm_data->sw_start to reference master descriptors and such.
147 #define __stm_master(_s, _m) \
148 ((_s)->masters[(_m) - (_s)->data->sw_start])
150 static inline struct stp_master
*
151 stm_master(struct stm_device
*stm
, unsigned int idx
)
153 if (idx
< stm
->data
->sw_start
|| idx
> stm
->data
->sw_end
)
156 return __stm_master(stm
, idx
);
159 static int stp_master_alloc(struct stm_device
*stm
, unsigned int idx
)
161 struct stp_master
*master
;
163 master
= kzalloc(struct_size(master
, chan_map
,
164 BITS_TO_LONGS(stm
->data
->sw_nchannels
)),
169 master
->nr_free
= stm
->data
->sw_nchannels
;
170 __stm_master(stm
, idx
) = master
;
175 static void stp_master_free(struct stm_device
*stm
, unsigned int idx
)
177 struct stp_master
*master
= stm_master(stm
, idx
);
182 __stm_master(stm
, idx
) = NULL
;
186 static void stm_output_claim(struct stm_device
*stm
, struct stm_output
*output
)
188 struct stp_master
*master
= stm_master(stm
, output
->master
);
190 lockdep_assert_held(&stm
->mc_lock
);
191 lockdep_assert_held(&output
->lock
);
193 if (WARN_ON_ONCE(master
->nr_free
< output
->nr_chans
))
196 bitmap_allocate_region(&master
->chan_map
[0], output
->channel
,
197 ilog2(output
->nr_chans
));
199 master
->nr_free
-= output
->nr_chans
;
203 stm_output_disclaim(struct stm_device
*stm
, struct stm_output
*output
)
205 struct stp_master
*master
= stm_master(stm
, output
->master
);
207 lockdep_assert_held(&stm
->mc_lock
);
208 lockdep_assert_held(&output
->lock
);
210 bitmap_release_region(&master
->chan_map
[0], output
->channel
,
211 ilog2(output
->nr_chans
));
213 master
->nr_free
+= output
->nr_chans
;
214 output
->nr_chans
= 0;
218 * This is like bitmap_find_free_region(), except it can ignore @start bits
221 static int find_free_channels(unsigned long *bitmap
, unsigned int start
,
222 unsigned int end
, unsigned int width
)
227 for (pos
= start
; pos
< end
+ 1; pos
= ALIGN(pos
, width
)) {
228 pos
= find_next_zero_bit(bitmap
, end
+ 1, pos
);
229 if (pos
+ width
> end
+ 1)
232 if (pos
& (width
- 1))
235 for (i
= 1; i
< width
&& !test_bit(pos
+ i
, bitmap
); i
++)
240 /* step over [pos..pos+i) to continue search */
248 stm_find_master_chan(struct stm_device
*stm
, unsigned int width
,
249 unsigned int *mstart
, unsigned int mend
,
250 unsigned int *cstart
, unsigned int cend
)
252 struct stp_master
*master
;
256 for (midx
= *mstart
; midx
<= mend
; midx
++) {
257 if (!stm_master(stm
, midx
)) {
258 err
= stp_master_alloc(stm
, midx
);
263 master
= stm_master(stm
, midx
);
265 if (!master
->nr_free
)
268 pos
= find_free_channels(master
->chan_map
, *cstart
, cend
,
281 static int stm_output_assign(struct stm_device
*stm
, unsigned int width
,
282 struct stp_policy_node
*policy_node
,
283 struct stm_output
*output
)
285 unsigned int midx
, cidx
, mend
, cend
;
288 if (width
> stm
->data
->sw_nchannels
)
291 /* We no longer accept policy_node==NULL here */
292 if (WARN_ON_ONCE(!policy_node
))
296 * Also, the caller holds reference to policy_node, so it won't
299 stp_policy_node_get_ranges(policy_node
, &midx
, &mend
, &cidx
, &cend
);
301 spin_lock(&stm
->mc_lock
);
302 spin_lock(&output
->lock
);
303 /* output is already assigned -- shouldn't happen */
304 if (WARN_ON_ONCE(output
->nr_chans
))
307 ret
= stm_find_master_chan(stm
, width
, &midx
, mend
, &cidx
, cend
);
311 output
->master
= midx
;
312 output
->channel
= cidx
;
313 output
->nr_chans
= width
;
314 if (stm
->pdrv
->output_open
) {
315 void *priv
= stp_policy_node_priv(policy_node
);
317 if (WARN_ON_ONCE(!priv
))
320 /* configfs subsys mutex is held by the caller */
321 ret
= stm
->pdrv
->output_open(priv
, output
);
326 stm_output_claim(stm
, output
);
327 dev_dbg(&stm
->dev
, "assigned %u:%u (+%u)\n", midx
, cidx
, width
);
332 output
->nr_chans
= 0;
334 spin_unlock(&output
->lock
);
335 spin_unlock(&stm
->mc_lock
);
340 static void stm_output_free(struct stm_device
*stm
, struct stm_output
*output
)
342 spin_lock(&stm
->mc_lock
);
343 spin_lock(&output
->lock
);
344 if (output
->nr_chans
)
345 stm_output_disclaim(stm
, output
);
346 if (stm
->pdrv
&& stm
->pdrv
->output_close
)
347 stm
->pdrv
->output_close(output
);
348 spin_unlock(&output
->lock
);
349 spin_unlock(&stm
->mc_lock
);
352 static void stm_output_init(struct stm_output
*output
)
354 spin_lock_init(&output
->lock
);
357 static int major_match(struct device
*dev
, const void *data
)
359 unsigned int major
= *(unsigned int *)data
;
361 return MAJOR(dev
->devt
) == major
;
365 * Framing protocol management
366 * Modules can implement STM protocol drivers and (un-)register them
367 * with the STM class framework.
369 static struct list_head stm_pdrv_head
;
370 static struct mutex stm_pdrv_mutex
;
372 struct stm_pdrv_entry
{
373 struct list_head entry
;
374 const struct stm_protocol_driver
*pdrv
;
375 const struct config_item_type
*node_type
;
378 static const struct stm_pdrv_entry
*
379 __stm_lookup_protocol(const char *name
)
381 struct stm_pdrv_entry
*pe
;
384 * If no name is given (NULL or ""), fall back to "p_basic".
389 list_for_each_entry(pe
, &stm_pdrv_head
, entry
) {
390 if (!strcmp(name
, pe
->pdrv
->name
))
397 int stm_register_protocol(const struct stm_protocol_driver
*pdrv
)
399 struct stm_pdrv_entry
*pe
= NULL
;
402 mutex_lock(&stm_pdrv_mutex
);
404 if (__stm_lookup_protocol(pdrv
->name
)) {
409 pe
= kzalloc(sizeof(*pe
), GFP_KERNEL
);
413 if (pdrv
->policy_attr
) {
414 pe
->node_type
= get_policy_node_type(pdrv
->policy_attr
);
419 list_add_tail(&pe
->entry
, &stm_pdrv_head
);
424 mutex_unlock(&stm_pdrv_mutex
);
431 EXPORT_SYMBOL_GPL(stm_register_protocol
);
433 void stm_unregister_protocol(const struct stm_protocol_driver
*pdrv
)
435 struct stm_pdrv_entry
*pe
, *iter
;
437 mutex_lock(&stm_pdrv_mutex
);
439 list_for_each_entry_safe(pe
, iter
, &stm_pdrv_head
, entry
) {
440 if (pe
->pdrv
== pdrv
) {
441 list_del(&pe
->entry
);
444 kfree(pe
->node_type
->ct_attrs
);
445 kfree(pe
->node_type
);
452 mutex_unlock(&stm_pdrv_mutex
);
454 EXPORT_SYMBOL_GPL(stm_unregister_protocol
);
456 static bool stm_get_protocol(const struct stm_protocol_driver
*pdrv
)
458 return try_module_get(pdrv
->owner
);
461 void stm_put_protocol(const struct stm_protocol_driver
*pdrv
)
463 module_put(pdrv
->owner
);
466 int stm_lookup_protocol(const char *name
,
467 const struct stm_protocol_driver
**pdrv
,
468 const struct config_item_type
**node_type
)
470 const struct stm_pdrv_entry
*pe
;
472 mutex_lock(&stm_pdrv_mutex
);
474 pe
= __stm_lookup_protocol(name
);
475 if (pe
&& pe
->pdrv
&& stm_get_protocol(pe
->pdrv
)) {
477 *node_type
= pe
->node_type
;
480 mutex_unlock(&stm_pdrv_mutex
);
482 return pe
? 0 : -ENOENT
;
485 static int stm_char_open(struct inode
*inode
, struct file
*file
)
487 struct stm_file
*stmf
;
489 unsigned int major
= imajor(inode
);
492 dev
= class_find_device(&stm_class
, NULL
, &major
, major_match
);
496 stmf
= kzalloc(sizeof(*stmf
), GFP_KERNEL
);
501 stm_output_init(&stmf
->output
);
502 stmf
->stm
= to_stm_device(dev
);
504 if (!try_module_get(stmf
->stm
->owner
))
507 file
->private_data
= stmf
;
509 return nonseekable_open(inode
, file
);
514 /* matches class_find_device() above */
520 static int stm_char_release(struct inode
*inode
, struct file
*file
)
522 struct stm_file
*stmf
= file
->private_data
;
523 struct stm_device
*stm
= stmf
->stm
;
525 if (stm
->data
->unlink
)
526 stm
->data
->unlink(stm
->data
, stmf
->output
.master
,
527 stmf
->output
.channel
);
529 stm_output_free(stm
, &stmf
->output
);
532 * matches the stm_char_open()'s
533 * class_find_device() + try_module_get()
542 stm_assign_first_policy(struct stm_device
*stm
, struct stm_output
*output
,
543 char **ids
, unsigned int width
)
545 struct stp_policy_node
*pn
;
549 * On success, stp_policy_node_lookup() will return holding the
550 * configfs subsystem mutex, which is then released in
551 * stp_policy_node_put(). This allows the pdrv->output_open() in
552 * stm_output_assign() to serialize against the attribute accessors.
554 for (n
= 0, pn
= NULL
; ids
[n
] && !pn
; n
++)
555 pn
= stp_policy_node_lookup(stm
, ids
[n
]);
560 err
= stm_output_assign(stm
, width
, pn
, output
);
562 stp_policy_node_put(pn
);
568 * stm_data_write() - send the given payload as data packets
569 * @data: stm driver's data
572 * @ts_first: timestamp the first packet
573 * @buf: data payload buffer
574 * @count: data payload size
576 ssize_t notrace
stm_data_write(struct stm_data
*data
, unsigned int m
,
577 unsigned int c
, bool ts_first
, const void *buf
,
580 unsigned int flags
= ts_first
? STP_PACKET_TIMESTAMPED
: 0;
584 for (pos
= 0, sz
= 0; pos
< count
; pos
+= sz
) {
585 sz
= min_t(unsigned int, count
- pos
, 8);
586 sz
= data
->packet(data
, m
, c
, STP_PACKET_DATA
, flags
, sz
,
597 return sz
< 0 ? sz
: pos
;
599 EXPORT_SYMBOL_GPL(stm_data_write
);
601 static ssize_t notrace
602 stm_write(struct stm_device
*stm
, struct stm_output
*output
,
603 unsigned int chan
, const char *buf
, size_t count
)
607 /* stm->pdrv is serialized against policy_mutex */
611 err
= stm
->pdrv
->write(stm
->data
, output
, chan
, buf
, count
);
618 static ssize_t
stm_char_write(struct file
*file
, const char __user
*buf
,
619 size_t count
, loff_t
*ppos
)
621 struct stm_file
*stmf
= file
->private_data
;
622 struct stm_device
*stm
= stmf
->stm
;
626 if (count
+ 1 > PAGE_SIZE
)
627 count
= PAGE_SIZE
- 1;
630 * If no m/c have been assigned to this writer up to this
631 * point, try to use the task name and "default" policy entries.
633 if (!stmf
->output
.nr_chans
) {
634 char comm
[sizeof(current
->comm
)];
635 char *ids
[] = { comm
, "default", NULL
};
637 get_task_comm(comm
, current
);
639 err
= stm_assign_first_policy(stmf
->stm
, &stmf
->output
, ids
, 1);
641 * EBUSY means that somebody else just assigned this
642 * output, which is just fine for write()
648 kbuf
= kmalloc(count
+ 1, GFP_KERNEL
);
652 err
= copy_from_user(kbuf
, buf
, count
);
658 pm_runtime_get_sync(&stm
->dev
);
660 count
= stm_write(stm
, &stmf
->output
, 0, kbuf
, count
);
662 pm_runtime_mark_last_busy(&stm
->dev
);
663 pm_runtime_put_autosuspend(&stm
->dev
);
669 static void stm_mmap_open(struct vm_area_struct
*vma
)
671 struct stm_file
*stmf
= vma
->vm_file
->private_data
;
672 struct stm_device
*stm
= stmf
->stm
;
674 pm_runtime_get(&stm
->dev
);
677 static void stm_mmap_close(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_mark_last_busy(&stm
->dev
);
683 pm_runtime_put_autosuspend(&stm
->dev
);
686 static const struct vm_operations_struct stm_mmap_vmops
= {
687 .open
= stm_mmap_open
,
688 .close
= stm_mmap_close
,
691 static int stm_char_mmap(struct file
*file
, struct vm_area_struct
*vma
)
693 struct stm_file
*stmf
= file
->private_data
;
694 struct stm_device
*stm
= stmf
->stm
;
695 unsigned long size
, phys
;
697 if (!stm
->data
->mmio_addr
)
703 size
= vma
->vm_end
- vma
->vm_start
;
705 if (stmf
->output
.nr_chans
* stm
->data
->sw_mmiosz
!= size
)
708 phys
= stm
->data
->mmio_addr(stm
->data
, stmf
->output
.master
,
709 stmf
->output
.channel
,
710 stmf
->output
.nr_chans
);
715 pm_runtime_get_sync(&stm
->dev
);
717 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
718 vma
->vm_flags
|= VM_IO
| VM_DONTEXPAND
| VM_DONTDUMP
;
719 vma
->vm_ops
= &stm_mmap_vmops
;
720 vm_iomap_memory(vma
, phys
, size
);
725 static int stm_char_policy_set_ioctl(struct stm_file
*stmf
, void __user
*arg
)
727 struct stm_device
*stm
= stmf
->stm
;
728 struct stp_policy_id
*id
;
729 char *ids
[] = { NULL
, NULL
};
730 int ret
= -EINVAL
, wlimit
= 1;
733 if (stmf
->output
.nr_chans
)
736 if (copy_from_user(&size
, arg
, sizeof(size
)))
739 if (size
< sizeof(*id
) || size
>= PATH_MAX
+ sizeof(*id
))
743 * size + 1 to make sure the .id string at the bottom is terminated,
744 * which is also why memdup_user() is not useful here
746 id
= kzalloc(size
+ 1, GFP_KERNEL
);
750 if (copy_from_user(id
, arg
, size
)) {
755 if (id
->__reserved_0
|| id
->__reserved_1
)
758 if (stm
->data
->sw_mmiosz
)
759 wlimit
= PAGE_SIZE
/ stm
->data
->sw_mmiosz
;
761 if (id
->width
< 1 || id
->width
> wlimit
)
765 ret
= stm_assign_first_policy(stmf
->stm
, &stmf
->output
, ids
,
771 ret
= stm
->data
->link(stm
->data
, stmf
->output
.master
,
772 stmf
->output
.channel
);
775 stm_output_free(stmf
->stm
, &stmf
->output
);
783 static int stm_char_policy_get_ioctl(struct stm_file
*stmf
, void __user
*arg
)
785 struct stp_policy_id id
= {
787 .master
= stmf
->output
.master
,
788 .channel
= stmf
->output
.channel
,
789 .width
= stmf
->output
.nr_chans
,
794 return copy_to_user(arg
, &id
, id
.size
) ? -EFAULT
: 0;
798 stm_char_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
800 struct stm_file
*stmf
= file
->private_data
;
801 struct stm_data
*stm_data
= stmf
->stm
->data
;
806 case STP_POLICY_ID_SET
:
807 err
= stm_char_policy_set_ioctl(stmf
, (void __user
*)arg
);
811 return stm_char_policy_get_ioctl(stmf
, (void __user
*)arg
);
813 case STP_POLICY_ID_GET
:
814 return stm_char_policy_get_ioctl(stmf
, (void __user
*)arg
);
816 case STP_SET_OPTIONS
:
817 if (copy_from_user(&options
, (u64 __user
*)arg
, sizeof(u64
)))
820 if (stm_data
->set_options
)
821 err
= stm_data
->set_options(stm_data
,
823 stmf
->output
.channel
,
824 stmf
->output
.nr_chans
,
835 static const struct file_operations stm_fops
= {
836 .open
= stm_char_open
,
837 .release
= stm_char_release
,
838 .write
= stm_char_write
,
839 .mmap
= stm_char_mmap
,
840 .unlocked_ioctl
= stm_char_ioctl
,
841 .compat_ioctl
= compat_ptr_ioctl
,
845 static void stm_device_release(struct device
*dev
)
847 struct stm_device
*stm
= to_stm_device(dev
);
852 int stm_register_device(struct device
*parent
, struct stm_data
*stm_data
,
853 struct module
*owner
)
855 struct stm_device
*stm
;
856 unsigned int nmasters
;
860 return -EPROBE_DEFER
;
862 if (!stm_data
->packet
|| !stm_data
->sw_nchannels
)
865 nmasters
= stm_data
->sw_end
- stm_data
->sw_start
+ 1;
866 stm
= vzalloc(sizeof(*stm
) + nmasters
* sizeof(void *));
870 stm
->major
= register_chrdev(0, stm_data
->name
, &stm_fops
);
874 device_initialize(&stm
->dev
);
875 stm
->dev
.devt
= MKDEV(stm
->major
, 0);
876 stm
->dev
.class = &stm_class
;
877 stm
->dev
.parent
= parent
;
878 stm
->dev
.release
= stm_device_release
;
880 mutex_init(&stm
->link_mutex
);
881 spin_lock_init(&stm
->link_lock
);
882 INIT_LIST_HEAD(&stm
->link_list
);
884 /* initialize the object before it is accessible via sysfs */
885 spin_lock_init(&stm
->mc_lock
);
886 mutex_init(&stm
->policy_mutex
);
887 stm
->sw_nmasters
= nmasters
;
889 stm
->data
= stm_data
;
892 err
= kobject_set_name(&stm
->dev
.kobj
, "%s", stm_data
->name
);
896 err
= device_add(&stm
->dev
);
901 * Use delayed autosuspend to avoid bouncing back and forth
902 * on recurring character device writes, with the initial
903 * delay time of 2 seconds.
905 pm_runtime_no_callbacks(&stm
->dev
);
906 pm_runtime_use_autosuspend(&stm
->dev
);
907 pm_runtime_set_autosuspend_delay(&stm
->dev
, 2000);
908 pm_runtime_set_suspended(&stm
->dev
);
909 pm_runtime_enable(&stm
->dev
);
914 unregister_chrdev(stm
->major
, stm_data
->name
);
916 /* matches device_initialize() above */
917 put_device(&stm
->dev
);
923 EXPORT_SYMBOL_GPL(stm_register_device
);
925 static int __stm_source_link_drop(struct stm_source_device
*src
,
926 struct stm_device
*stm
);
928 void stm_unregister_device(struct stm_data
*stm_data
)
930 struct stm_device
*stm
= stm_data
->stm
;
931 struct stm_source_device
*src
, *iter
;
934 pm_runtime_dont_use_autosuspend(&stm
->dev
);
935 pm_runtime_disable(&stm
->dev
);
937 mutex_lock(&stm
->link_mutex
);
938 list_for_each_entry_safe(src
, iter
, &stm
->link_list
, link_entry
) {
939 ret
= __stm_source_link_drop(src
, stm
);
941 * src <-> stm link must not change under the same
942 * stm::link_mutex, so complain loudly if it has;
943 * also in this situation ret!=0 means this src is
944 * not connected to this stm and it should be otherwise
945 * safe to proceed with the tear-down of stm.
949 mutex_unlock(&stm
->link_mutex
);
951 synchronize_srcu(&stm_source_srcu
);
953 unregister_chrdev(stm
->major
, stm_data
->name
);
955 mutex_lock(&stm
->policy_mutex
);
957 stp_policy_unbind(stm
->policy
);
958 mutex_unlock(&stm
->policy_mutex
);
960 for (i
= stm
->data
->sw_start
; i
<= stm
->data
->sw_end
; i
++)
961 stp_master_free(stm
, i
);
963 device_unregister(&stm
->dev
);
964 stm_data
->stm
= NULL
;
966 EXPORT_SYMBOL_GPL(stm_unregister_device
);
969 * stm::link_list access serialization uses a spinlock and a mutex; holding
970 * either of them guarantees that the list is stable; modification requires
971 * holding both of them.
973 * Lock ordering is as follows:
980 * stm_source_link_add() - connect an stm_source device to an stm device
981 * @src: stm_source device
984 * This function establishes a link from stm_source to an stm device so that
985 * the former can send out trace data to the latter.
987 * Return: 0 on success, -errno otherwise.
989 static int stm_source_link_add(struct stm_source_device
*src
,
990 struct stm_device
*stm
)
992 char *ids
[] = { NULL
, "default", NULL
};
995 mutex_lock(&stm
->link_mutex
);
996 spin_lock(&stm
->link_lock
);
997 spin_lock(&src
->link_lock
);
999 /* src->link is dereferenced under stm_source_srcu but not the list */
1000 rcu_assign_pointer(src
->link
, stm
);
1001 list_add_tail(&src
->link_entry
, &stm
->link_list
);
1003 spin_unlock(&src
->link_lock
);
1004 spin_unlock(&stm
->link_lock
);
1005 mutex_unlock(&stm
->link_mutex
);
1007 ids
[0] = kstrdup(src
->data
->name
, GFP_KERNEL
);
1011 err
= stm_assign_first_policy(stm
, &src
->output
, ids
,
1012 src
->data
->nr_chans
);
1018 /* this is to notify the STM device that a new link has been made */
1019 if (stm
->data
->link
)
1020 err
= stm
->data
->link(stm
->data
, src
->output
.master
,
1021 src
->output
.channel
);
1024 goto fail_free_output
;
1026 /* this is to let the source carry out all necessary preparations */
1027 if (src
->data
->link
)
1028 src
->data
->link(src
->data
);
1033 stm_output_free(stm
, &src
->output
);
1036 mutex_lock(&stm
->link_mutex
);
1037 spin_lock(&stm
->link_lock
);
1038 spin_lock(&src
->link_lock
);
1040 rcu_assign_pointer(src
->link
, NULL
);
1041 list_del_init(&src
->link_entry
);
1043 spin_unlock(&src
->link_lock
);
1044 spin_unlock(&stm
->link_lock
);
1045 mutex_unlock(&stm
->link_mutex
);
1051 * __stm_source_link_drop() - detach stm_source from an stm device
1052 * @src: stm_source device
1055 * If @stm is @src::link, disconnect them from one another and put the
1056 * reference on the @stm device.
1058 * Caller must hold stm::link_mutex.
1060 static int __stm_source_link_drop(struct stm_source_device
*src
,
1061 struct stm_device
*stm
)
1063 struct stm_device
*link
;
1066 lockdep_assert_held(&stm
->link_mutex
);
1068 /* for stm::link_list modification, we hold both mutex and spinlock */
1069 spin_lock(&stm
->link_lock
);
1070 spin_lock(&src
->link_lock
);
1071 link
= srcu_dereference_check(src
->link
, &stm_source_srcu
, 1);
1074 * The linked device may have changed since we last looked, because
1075 * we weren't holding the src::link_lock back then; if this is the
1076 * case, tell the caller to retry.
1083 stm_output_free(link
, &src
->output
);
1084 list_del_init(&src
->link_entry
);
1085 pm_runtime_mark_last_busy(&link
->dev
);
1086 pm_runtime_put_autosuspend(&link
->dev
);
1087 /* matches stm_find_device() from stm_source_link_store() */
1088 stm_put_device(link
);
1089 rcu_assign_pointer(src
->link
, NULL
);
1092 spin_unlock(&src
->link_lock
);
1093 spin_unlock(&stm
->link_lock
);
1096 * Call the unlink callbacks for both source and stm, when we know
1097 * that we have actually performed the unlinking.
1100 if (src
->data
->unlink
)
1101 src
->data
->unlink(src
->data
);
1103 if (stm
->data
->unlink
)
1104 stm
->data
->unlink(stm
->data
, src
->output
.master
,
1105 src
->output
.channel
);
1112 * stm_source_link_drop() - detach stm_source from its stm device
1113 * @src: stm_source device
1115 * Unlinking means disconnecting from source's STM device; after this
1116 * writes will be unsuccessful until it is linked to a new STM device.
1118 * This will happen on "stm_source_link" sysfs attribute write to undo
1119 * the existing link (if any), or on linked STM device's de-registration.
1121 static void stm_source_link_drop(struct stm_source_device
*src
)
1123 struct stm_device
*stm
;
1127 idx
= srcu_read_lock(&stm_source_srcu
);
1129 * The stm device will be valid for the duration of this
1130 * read section, but the link may change before we grab
1131 * the src::link_lock in __stm_source_link_drop().
1133 stm
= srcu_dereference(src
->link
, &stm_source_srcu
);
1137 mutex_lock(&stm
->link_mutex
);
1138 ret
= __stm_source_link_drop(src
, stm
);
1139 mutex_unlock(&stm
->link_mutex
);
1142 srcu_read_unlock(&stm_source_srcu
, idx
);
1144 /* if it did change, retry */
1149 static ssize_t
stm_source_link_show(struct device
*dev
,
1150 struct device_attribute
*attr
,
1153 struct stm_source_device
*src
= to_stm_source_device(dev
);
1154 struct stm_device
*stm
;
1157 idx
= srcu_read_lock(&stm_source_srcu
);
1158 stm
= srcu_dereference(src
->link
, &stm_source_srcu
);
1159 ret
= sprintf(buf
, "%s\n",
1160 stm
? dev_name(&stm
->dev
) : "<none>");
1161 srcu_read_unlock(&stm_source_srcu
, idx
);
1166 static ssize_t
stm_source_link_store(struct device
*dev
,
1167 struct device_attribute
*attr
,
1168 const char *buf
, size_t count
)
1170 struct stm_source_device
*src
= to_stm_source_device(dev
);
1171 struct stm_device
*link
;
1174 stm_source_link_drop(src
);
1176 link
= stm_find_device(buf
);
1180 pm_runtime_get(&link
->dev
);
1182 err
= stm_source_link_add(src
, link
);
1184 pm_runtime_put_autosuspend(&link
->dev
);
1185 /* matches the stm_find_device() above */
1186 stm_put_device(link
);
1189 return err
? : count
;
1192 static DEVICE_ATTR_RW(stm_source_link
);
1194 static struct attribute
*stm_source_attrs
[] = {
1195 &dev_attr_stm_source_link
.attr
,
1199 ATTRIBUTE_GROUPS(stm_source
);
1201 static struct class stm_source_class
= {
1202 .name
= "stm_source",
1203 .dev_groups
= stm_source_groups
,
1206 static void stm_source_device_release(struct device
*dev
)
1208 struct stm_source_device
*src
= to_stm_source_device(dev
);
1214 * stm_source_register_device() - register an stm_source device
1215 * @parent: parent device
1216 * @data: device description structure
1218 * This will create a device of stm_source class that can write
1219 * data to an stm device once linked.
1221 * Return: 0 on success, -errno otherwise.
1223 int stm_source_register_device(struct device
*parent
,
1224 struct stm_source_data
*data
)
1226 struct stm_source_device
*src
;
1230 return -EPROBE_DEFER
;
1232 src
= kzalloc(sizeof(*src
), GFP_KERNEL
);
1236 device_initialize(&src
->dev
);
1237 src
->dev
.class = &stm_source_class
;
1238 src
->dev
.parent
= parent
;
1239 src
->dev
.release
= stm_source_device_release
;
1241 err
= kobject_set_name(&src
->dev
.kobj
, "%s", data
->name
);
1245 pm_runtime_no_callbacks(&src
->dev
);
1246 pm_runtime_forbid(&src
->dev
);
1248 err
= device_add(&src
->dev
);
1252 stm_output_init(&src
->output
);
1253 spin_lock_init(&src
->link_lock
);
1254 INIT_LIST_HEAD(&src
->link_entry
);
1261 put_device(&src
->dev
);
1265 EXPORT_SYMBOL_GPL(stm_source_register_device
);
1268 * stm_source_unregister_device() - unregister an stm_source device
1269 * @data: device description that was used to register the device
1271 * This will remove a previously created stm_source device from the system.
1273 void stm_source_unregister_device(struct stm_source_data
*data
)
1275 struct stm_source_device
*src
= data
->src
;
1277 stm_source_link_drop(src
);
1279 device_unregister(&src
->dev
);
1281 EXPORT_SYMBOL_GPL(stm_source_unregister_device
);
1283 int notrace
stm_source_write(struct stm_source_data
*data
,
1285 const char *buf
, size_t count
)
1287 struct stm_source_device
*src
= data
->src
;
1288 struct stm_device
*stm
;
1291 if (!src
->output
.nr_chans
)
1294 if (chan
>= src
->output
.nr_chans
)
1297 idx
= srcu_read_lock(&stm_source_srcu
);
1299 stm
= srcu_dereference(src
->link
, &stm_source_srcu
);
1301 count
= stm_write(stm
, &src
->output
, chan
, buf
, count
);
1305 srcu_read_unlock(&stm_source_srcu
, idx
);
1309 EXPORT_SYMBOL_GPL(stm_source_write
);
1311 static int __init
stm_core_init(void)
1315 err
= class_register(&stm_class
);
1319 err
= class_register(&stm_source_class
);
1323 err
= stp_configfs_init();
1327 init_srcu_struct(&stm_source_srcu
);
1328 INIT_LIST_HEAD(&stm_pdrv_head
);
1329 mutex_init(&stm_pdrv_mutex
);
1332 * So as to not confuse existing users with a requirement
1333 * to load yet another module, do it here.
1335 if (IS_ENABLED(CONFIG_STM_PROTO_BASIC
))
1336 (void)request_module_nowait("stm_p_basic");
1342 class_unregister(&stm_source_class
);
1344 class_unregister(&stm_class
);
1349 module_init(stm_core_init
);
1351 static void __exit
stm_core_exit(void)
1353 cleanup_srcu_struct(&stm_source_srcu
);
1354 class_unregister(&stm_source_class
);
1355 class_unregister(&stm_class
);
1356 stp_configfs_exit();
1359 module_exit(stm_core_exit
);
1361 MODULE_LICENSE("GPL v2");
1362 MODULE_DESCRIPTION("System Trace Module device class");
1363 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");