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
)
299 stp_policy_node_get_ranges(policy_node
,
300 &midx
, &mend
, &cidx
, &cend
);
302 midx
= stm
->data
->sw_start
;
304 mend
= stm
->data
->sw_end
;
305 cend
= stm
->data
->sw_nchannels
- 1;
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 stm_output_claim(stm
, output
);
322 dev_dbg(&stm
->dev
, "assigned %u:%u (+%u)\n", midx
, cidx
, width
);
326 spin_unlock(&output
->lock
);
327 spin_unlock(&stm
->mc_lock
);
332 static void stm_output_free(struct stm_device
*stm
, struct stm_output
*output
)
334 spin_lock(&stm
->mc_lock
);
335 spin_lock(&output
->lock
);
336 if (output
->nr_chans
)
337 stm_output_disclaim(stm
, output
);
338 spin_unlock(&output
->lock
);
339 spin_unlock(&stm
->mc_lock
);
342 static void stm_output_init(struct stm_output
*output
)
344 spin_lock_init(&output
->lock
);
347 static int major_match(struct device
*dev
, const void *data
)
349 unsigned int major
= *(unsigned int *)data
;
351 return MAJOR(dev
->devt
) == major
;
354 static int stm_char_open(struct inode
*inode
, struct file
*file
)
356 struct stm_file
*stmf
;
358 unsigned int major
= imajor(inode
);
361 dev
= class_find_device(&stm_class
, NULL
, &major
, major_match
);
365 stmf
= kzalloc(sizeof(*stmf
), GFP_KERNEL
);
370 stm_output_init(&stmf
->output
);
371 stmf
->stm
= to_stm_device(dev
);
373 if (!try_module_get(stmf
->stm
->owner
))
376 file
->private_data
= stmf
;
378 return nonseekable_open(inode
, file
);
383 /* matches class_find_device() above */
389 static int stm_char_release(struct inode
*inode
, struct file
*file
)
391 struct stm_file
*stmf
= file
->private_data
;
392 struct stm_device
*stm
= stmf
->stm
;
394 if (stm
->data
->unlink
)
395 stm
->data
->unlink(stm
->data
, stmf
->output
.master
,
396 stmf
->output
.channel
);
398 stm_output_free(stm
, &stmf
->output
);
401 * matches the stm_char_open()'s
402 * class_find_device() + try_module_get()
410 static int stm_file_assign(struct stm_file
*stmf
, char *id
, unsigned int width
)
412 struct stm_device
*stm
= stmf
->stm
;
415 stmf
->policy_node
= stp_policy_node_lookup(stm
, id
);
417 ret
= stm_output_assign(stm
, width
, stmf
->policy_node
, &stmf
->output
);
419 if (stmf
->policy_node
)
420 stp_policy_node_put(stmf
->policy_node
);
425 static ssize_t notrace
stm_write(struct stm_data
*data
, unsigned int master
,
426 unsigned int channel
, const char *buf
, size_t count
)
428 unsigned int flags
= STP_PACKET_TIMESTAMPED
;
429 const unsigned char *p
= buf
, nil
= 0;
433 for (pos
= 0, p
= buf
; count
> pos
; pos
+= sz
, p
+= sz
) {
434 sz
= min_t(unsigned int, count
- pos
, 8);
435 sz
= data
->packet(data
, master
, channel
, STP_PACKET_DATA
, flags
,
443 data
->packet(data
, master
, channel
, STP_PACKET_FLAG
, 0, 0, &nil
);
448 static ssize_t
stm_char_write(struct file
*file
, const char __user
*buf
,
449 size_t count
, loff_t
*ppos
)
451 struct stm_file
*stmf
= file
->private_data
;
452 struct stm_device
*stm
= stmf
->stm
;
456 if (count
+ 1 > PAGE_SIZE
)
457 count
= PAGE_SIZE
- 1;
460 * if no m/c have been assigned to this writer up to this
461 * point, use "default" policy entry
463 if (!stmf
->output
.nr_chans
) {
464 err
= stm_file_assign(stmf
, "default", 1);
466 * EBUSY means that somebody else just assigned this
467 * output, which is just fine for write()
469 if (err
&& err
!= -EBUSY
)
473 kbuf
= kmalloc(count
+ 1, GFP_KERNEL
);
477 err
= copy_from_user(kbuf
, buf
, count
);
483 pm_runtime_get_sync(&stm
->dev
);
485 count
= stm_write(stm
->data
, stmf
->output
.master
, stmf
->output
.channel
,
488 pm_runtime_mark_last_busy(&stm
->dev
);
489 pm_runtime_put_autosuspend(&stm
->dev
);
495 static void stm_mmap_open(struct vm_area_struct
*vma
)
497 struct stm_file
*stmf
= vma
->vm_file
->private_data
;
498 struct stm_device
*stm
= stmf
->stm
;
500 pm_runtime_get(&stm
->dev
);
503 static void stm_mmap_close(struct vm_area_struct
*vma
)
505 struct stm_file
*stmf
= vma
->vm_file
->private_data
;
506 struct stm_device
*stm
= stmf
->stm
;
508 pm_runtime_mark_last_busy(&stm
->dev
);
509 pm_runtime_put_autosuspend(&stm
->dev
);
512 static const struct vm_operations_struct stm_mmap_vmops
= {
513 .open
= stm_mmap_open
,
514 .close
= stm_mmap_close
,
517 static int stm_char_mmap(struct file
*file
, struct vm_area_struct
*vma
)
519 struct stm_file
*stmf
= file
->private_data
;
520 struct stm_device
*stm
= stmf
->stm
;
521 unsigned long size
, phys
;
523 if (!stm
->data
->mmio_addr
)
529 size
= vma
->vm_end
- vma
->vm_start
;
531 if (stmf
->output
.nr_chans
* stm
->data
->sw_mmiosz
!= size
)
534 phys
= stm
->data
->mmio_addr(stm
->data
, stmf
->output
.master
,
535 stmf
->output
.channel
,
536 stmf
->output
.nr_chans
);
541 pm_runtime_get_sync(&stm
->dev
);
543 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
544 vma
->vm_flags
|= VM_IO
| VM_DONTEXPAND
| VM_DONTDUMP
;
545 vma
->vm_ops
= &stm_mmap_vmops
;
546 vm_iomap_memory(vma
, phys
, size
);
551 static int stm_char_policy_set_ioctl(struct stm_file
*stmf
, void __user
*arg
)
553 struct stm_device
*stm
= stmf
->stm
;
554 struct stp_policy_id
*id
;
555 int ret
= -EINVAL
, wlimit
= 1;
558 if (stmf
->output
.nr_chans
)
561 if (copy_from_user(&size
, arg
, sizeof(size
)))
564 if (size
< sizeof(*id
) || size
>= PATH_MAX
+ sizeof(*id
))
568 * size + 1 to make sure the .id string at the bottom is terminated,
569 * which is also why memdup_user() is not useful here
571 id
= kzalloc(size
+ 1, GFP_KERNEL
);
575 if (copy_from_user(id
, arg
, size
)) {
580 if (id
->__reserved_0
|| id
->__reserved_1
)
583 if (stm
->data
->sw_mmiosz
)
584 wlimit
= PAGE_SIZE
/ stm
->data
->sw_mmiosz
;
586 if (id
->width
< 1 || id
->width
> wlimit
)
589 ret
= stm_file_assign(stmf
, id
->id
, id
->width
);
594 ret
= stm
->data
->link(stm
->data
, stmf
->output
.master
,
595 stmf
->output
.channel
);
598 stm_output_free(stmf
->stm
, &stmf
->output
);
606 static int stm_char_policy_get_ioctl(struct stm_file
*stmf
, void __user
*arg
)
608 struct stp_policy_id id
= {
610 .master
= stmf
->output
.master
,
611 .channel
= stmf
->output
.channel
,
612 .width
= stmf
->output
.nr_chans
,
617 return copy_to_user(arg
, &id
, id
.size
) ? -EFAULT
: 0;
621 stm_char_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
623 struct stm_file
*stmf
= file
->private_data
;
624 struct stm_data
*stm_data
= stmf
->stm
->data
;
629 case STP_POLICY_ID_SET
:
630 err
= stm_char_policy_set_ioctl(stmf
, (void __user
*)arg
);
634 return stm_char_policy_get_ioctl(stmf
, (void __user
*)arg
);
636 case STP_POLICY_ID_GET
:
637 return stm_char_policy_get_ioctl(stmf
, (void __user
*)arg
);
639 case STP_SET_OPTIONS
:
640 if (copy_from_user(&options
, (u64 __user
*)arg
, sizeof(u64
)))
643 if (stm_data
->set_options
)
644 err
= stm_data
->set_options(stm_data
,
646 stmf
->output
.channel
,
647 stmf
->output
.nr_chans
,
660 stm_char_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
662 return stm_char_ioctl(file
, cmd
, (unsigned long)compat_ptr(arg
));
665 #define stm_char_compat_ioctl NULL
668 static const struct file_operations stm_fops
= {
669 .open
= stm_char_open
,
670 .release
= stm_char_release
,
671 .write
= stm_char_write
,
672 .mmap
= stm_char_mmap
,
673 .unlocked_ioctl
= stm_char_ioctl
,
674 .compat_ioctl
= stm_char_compat_ioctl
,
678 static void stm_device_release(struct device
*dev
)
680 struct stm_device
*stm
= to_stm_device(dev
);
685 int stm_register_device(struct device
*parent
, struct stm_data
*stm_data
,
686 struct module
*owner
)
688 struct stm_device
*stm
;
689 unsigned int nmasters
;
693 return -EPROBE_DEFER
;
695 if (!stm_data
->packet
|| !stm_data
->sw_nchannels
)
698 nmasters
= stm_data
->sw_end
- stm_data
->sw_start
+ 1;
699 stm
= vzalloc(sizeof(*stm
) + nmasters
* sizeof(void *));
703 stm
->major
= register_chrdev(0, stm_data
->name
, &stm_fops
);
707 device_initialize(&stm
->dev
);
708 stm
->dev
.devt
= MKDEV(stm
->major
, 0);
709 stm
->dev
.class = &stm_class
;
710 stm
->dev
.parent
= parent
;
711 stm
->dev
.release
= stm_device_release
;
713 mutex_init(&stm
->link_mutex
);
714 spin_lock_init(&stm
->link_lock
);
715 INIT_LIST_HEAD(&stm
->link_list
);
717 /* initialize the object before it is accessible via sysfs */
718 spin_lock_init(&stm
->mc_lock
);
719 mutex_init(&stm
->policy_mutex
);
720 stm
->sw_nmasters
= nmasters
;
722 stm
->data
= stm_data
;
725 err
= kobject_set_name(&stm
->dev
.kobj
, "%s", stm_data
->name
);
729 err
= device_add(&stm
->dev
);
734 * Use delayed autosuspend to avoid bouncing back and forth
735 * on recurring character device writes, with the initial
736 * delay time of 2 seconds.
738 pm_runtime_no_callbacks(&stm
->dev
);
739 pm_runtime_use_autosuspend(&stm
->dev
);
740 pm_runtime_set_autosuspend_delay(&stm
->dev
, 2000);
741 pm_runtime_set_suspended(&stm
->dev
);
742 pm_runtime_enable(&stm
->dev
);
747 unregister_chrdev(stm
->major
, stm_data
->name
);
749 /* matches device_initialize() above */
750 put_device(&stm
->dev
);
756 EXPORT_SYMBOL_GPL(stm_register_device
);
758 static int __stm_source_link_drop(struct stm_source_device
*src
,
759 struct stm_device
*stm
);
761 void stm_unregister_device(struct stm_data
*stm_data
)
763 struct stm_device
*stm
= stm_data
->stm
;
764 struct stm_source_device
*src
, *iter
;
767 pm_runtime_dont_use_autosuspend(&stm
->dev
);
768 pm_runtime_disable(&stm
->dev
);
770 mutex_lock(&stm
->link_mutex
);
771 list_for_each_entry_safe(src
, iter
, &stm
->link_list
, link_entry
) {
772 ret
= __stm_source_link_drop(src
, stm
);
774 * src <-> stm link must not change under the same
775 * stm::link_mutex, so complain loudly if it has;
776 * also in this situation ret!=0 means this src is
777 * not connected to this stm and it should be otherwise
778 * safe to proceed with the tear-down of stm.
782 mutex_unlock(&stm
->link_mutex
);
784 synchronize_srcu(&stm_source_srcu
);
786 unregister_chrdev(stm
->major
, stm_data
->name
);
788 mutex_lock(&stm
->policy_mutex
);
790 stp_policy_unbind(stm
->policy
);
791 mutex_unlock(&stm
->policy_mutex
);
793 for (i
= stm
->data
->sw_start
; i
<= stm
->data
->sw_end
; i
++)
794 stp_master_free(stm
, i
);
796 device_unregister(&stm
->dev
);
797 stm_data
->stm
= NULL
;
799 EXPORT_SYMBOL_GPL(stm_unregister_device
);
802 * stm::link_list access serialization uses a spinlock and a mutex; holding
803 * either of them guarantees that the list is stable; modification requires
804 * holding both of them.
806 * Lock ordering is as follows:
813 * stm_source_link_add() - connect an stm_source device to an stm device
814 * @src: stm_source device
817 * This function establishes a link from stm_source to an stm device so that
818 * the former can send out trace data to the latter.
820 * Return: 0 on success, -errno otherwise.
822 static int stm_source_link_add(struct stm_source_device
*src
,
823 struct stm_device
*stm
)
828 mutex_lock(&stm
->link_mutex
);
829 spin_lock(&stm
->link_lock
);
830 spin_lock(&src
->link_lock
);
832 /* src->link is dereferenced under stm_source_srcu but not the list */
833 rcu_assign_pointer(src
->link
, stm
);
834 list_add_tail(&src
->link_entry
, &stm
->link_list
);
836 spin_unlock(&src
->link_lock
);
837 spin_unlock(&stm
->link_lock
);
838 mutex_unlock(&stm
->link_mutex
);
840 id
= kstrdup(src
->data
->name
, GFP_KERNEL
);
843 stp_policy_node_lookup(stm
, id
);
848 err
= stm_output_assign(stm
, src
->data
->nr_chans
,
849 src
->policy_node
, &src
->output
);
851 if (src
->policy_node
)
852 stp_policy_node_put(src
->policy_node
);
857 /* this is to notify the STM device that a new link has been made */
859 err
= stm
->data
->link(stm
->data
, src
->output
.master
,
860 src
->output
.channel
);
863 goto fail_free_output
;
865 /* this is to let the source carry out all necessary preparations */
867 src
->data
->link(src
->data
);
872 stm_output_free(stm
, &src
->output
);
875 mutex_lock(&stm
->link_mutex
);
876 spin_lock(&stm
->link_lock
);
877 spin_lock(&src
->link_lock
);
879 rcu_assign_pointer(src
->link
, NULL
);
880 list_del_init(&src
->link_entry
);
882 spin_unlock(&src
->link_lock
);
883 spin_unlock(&stm
->link_lock
);
884 mutex_unlock(&stm
->link_mutex
);
890 * __stm_source_link_drop() - detach stm_source from an stm device
891 * @src: stm_source device
894 * If @stm is @src::link, disconnect them from one another and put the
895 * reference on the @stm device.
897 * Caller must hold stm::link_mutex.
899 static int __stm_source_link_drop(struct stm_source_device
*src
,
900 struct stm_device
*stm
)
902 struct stm_device
*link
;
905 lockdep_assert_held(&stm
->link_mutex
);
907 /* for stm::link_list modification, we hold both mutex and spinlock */
908 spin_lock(&stm
->link_lock
);
909 spin_lock(&src
->link_lock
);
910 link
= srcu_dereference_check(src
->link
, &stm_source_srcu
, 1);
913 * The linked device may have changed since we last looked, because
914 * we weren't holding the src::link_lock back then; if this is the
915 * case, tell the caller to retry.
922 stm_output_free(link
, &src
->output
);
923 list_del_init(&src
->link_entry
);
924 pm_runtime_mark_last_busy(&link
->dev
);
925 pm_runtime_put_autosuspend(&link
->dev
);
926 /* matches stm_find_device() from stm_source_link_store() */
927 stm_put_device(link
);
928 rcu_assign_pointer(src
->link
, NULL
);
931 spin_unlock(&src
->link_lock
);
932 spin_unlock(&stm
->link_lock
);
935 * Call the unlink callbacks for both source and stm, when we know
936 * that we have actually performed the unlinking.
939 if (src
->data
->unlink
)
940 src
->data
->unlink(src
->data
);
942 if (stm
->data
->unlink
)
943 stm
->data
->unlink(stm
->data
, src
->output
.master
,
944 src
->output
.channel
);
951 * stm_source_link_drop() - detach stm_source from its stm device
952 * @src: stm_source device
954 * Unlinking means disconnecting from source's STM device; after this
955 * writes will be unsuccessful until it is linked to a new STM device.
957 * This will happen on "stm_source_link" sysfs attribute write to undo
958 * the existing link (if any), or on linked STM device's de-registration.
960 static void stm_source_link_drop(struct stm_source_device
*src
)
962 struct stm_device
*stm
;
966 idx
= srcu_read_lock(&stm_source_srcu
);
968 * The stm device will be valid for the duration of this
969 * read section, but the link may change before we grab
970 * the src::link_lock in __stm_source_link_drop().
972 stm
= srcu_dereference(src
->link
, &stm_source_srcu
);
976 mutex_lock(&stm
->link_mutex
);
977 ret
= __stm_source_link_drop(src
, stm
);
978 mutex_unlock(&stm
->link_mutex
);
981 srcu_read_unlock(&stm_source_srcu
, idx
);
983 /* if it did change, retry */
988 static ssize_t
stm_source_link_show(struct device
*dev
,
989 struct device_attribute
*attr
,
992 struct stm_source_device
*src
= to_stm_source_device(dev
);
993 struct stm_device
*stm
;
996 idx
= srcu_read_lock(&stm_source_srcu
);
997 stm
= srcu_dereference(src
->link
, &stm_source_srcu
);
998 ret
= sprintf(buf
, "%s\n",
999 stm
? dev_name(&stm
->dev
) : "<none>");
1000 srcu_read_unlock(&stm_source_srcu
, idx
);
1005 static ssize_t
stm_source_link_store(struct device
*dev
,
1006 struct device_attribute
*attr
,
1007 const char *buf
, size_t count
)
1009 struct stm_source_device
*src
= to_stm_source_device(dev
);
1010 struct stm_device
*link
;
1013 stm_source_link_drop(src
);
1015 link
= stm_find_device(buf
);
1019 pm_runtime_get(&link
->dev
);
1021 err
= stm_source_link_add(src
, link
);
1023 pm_runtime_put_autosuspend(&link
->dev
);
1024 /* matches the stm_find_device() above */
1025 stm_put_device(link
);
1028 return err
? : count
;
1031 static DEVICE_ATTR_RW(stm_source_link
);
1033 static struct attribute
*stm_source_attrs
[] = {
1034 &dev_attr_stm_source_link
.attr
,
1038 ATTRIBUTE_GROUPS(stm_source
);
1040 static struct class stm_source_class
= {
1041 .name
= "stm_source",
1042 .dev_groups
= stm_source_groups
,
1045 static void stm_source_device_release(struct device
*dev
)
1047 struct stm_source_device
*src
= to_stm_source_device(dev
);
1053 * stm_source_register_device() - register an stm_source device
1054 * @parent: parent device
1055 * @data: device description structure
1057 * This will create a device of stm_source class that can write
1058 * data to an stm device once linked.
1060 * Return: 0 on success, -errno otherwise.
1062 int stm_source_register_device(struct device
*parent
,
1063 struct stm_source_data
*data
)
1065 struct stm_source_device
*src
;
1069 return -EPROBE_DEFER
;
1071 src
= kzalloc(sizeof(*src
), GFP_KERNEL
);
1075 device_initialize(&src
->dev
);
1076 src
->dev
.class = &stm_source_class
;
1077 src
->dev
.parent
= parent
;
1078 src
->dev
.release
= stm_source_device_release
;
1080 err
= kobject_set_name(&src
->dev
.kobj
, "%s", data
->name
);
1084 pm_runtime_no_callbacks(&src
->dev
);
1085 pm_runtime_forbid(&src
->dev
);
1087 err
= device_add(&src
->dev
);
1091 stm_output_init(&src
->output
);
1092 spin_lock_init(&src
->link_lock
);
1093 INIT_LIST_HEAD(&src
->link_entry
);
1100 put_device(&src
->dev
);
1104 EXPORT_SYMBOL_GPL(stm_source_register_device
);
1107 * stm_source_unregister_device() - unregister an stm_source device
1108 * @data: device description that was used to register the device
1110 * This will remove a previously created stm_source device from the system.
1112 void stm_source_unregister_device(struct stm_source_data
*data
)
1114 struct stm_source_device
*src
= data
->src
;
1116 stm_source_link_drop(src
);
1118 device_unregister(&src
->dev
);
1120 EXPORT_SYMBOL_GPL(stm_source_unregister_device
);
1122 int notrace
stm_source_write(struct stm_source_data
*data
,
1124 const char *buf
, size_t count
)
1126 struct stm_source_device
*src
= data
->src
;
1127 struct stm_device
*stm
;
1130 if (!src
->output
.nr_chans
)
1133 if (chan
>= src
->output
.nr_chans
)
1136 idx
= srcu_read_lock(&stm_source_srcu
);
1138 stm
= srcu_dereference(src
->link
, &stm_source_srcu
);
1140 count
= stm_write(stm
->data
, src
->output
.master
,
1141 src
->output
.channel
+ chan
,
1146 srcu_read_unlock(&stm_source_srcu
, idx
);
1150 EXPORT_SYMBOL_GPL(stm_source_write
);
1152 static int __init
stm_core_init(void)
1156 err
= class_register(&stm_class
);
1160 err
= class_register(&stm_source_class
);
1164 err
= stp_configfs_init();
1168 init_srcu_struct(&stm_source_srcu
);
1175 class_unregister(&stm_source_class
);
1177 class_unregister(&stm_class
);
1182 module_init(stm_core_init
);
1184 static void __exit
stm_core_exit(void)
1186 cleanup_srcu_struct(&stm_source_srcu
);
1187 class_unregister(&stm_source_class
);
1188 class_unregister(&stm_class
);
1189 stp_configfs_exit();
1192 module_exit(stm_core_exit
);
1194 MODULE_LICENSE("GPL v2");
1195 MODULE_DESCRIPTION("System Trace Module device class");
1196 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");