2 * System Trace Module (STM) infrastructure
3 * Copyright (c) 2014, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * STM class implements generic infrastructure for System Trace Module devices
15 * as defined in MIPI STPv2 specification.
18 #include <linux/uaccess.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/device.h>
22 #include <linux/compat.h>
23 #include <linux/kdev_t.h>
24 #include <linux/srcu.h>
25 #include <linux/slab.h>
26 #include <linux/stm.h>
31 #include <uapi/linux/stm.h>
33 static unsigned int stm_core_up;
36 * The SRCU here makes sure that STM device doesn't disappear from under a
37 * stm_source_write() caller, which may want to have as little overhead as
40 static struct srcu_struct stm_source_srcu;
42 static ssize_t masters_show(struct device *dev,
43 struct device_attribute *attr,
46 struct stm_device *stm = to_stm_device(dev);
49 ret = sprintf(buf, "%u %u\n", stm->data->sw_start, stm->data->sw_end);
54 static DEVICE_ATTR_RO(masters);
56 static ssize_t channels_show(struct device *dev,
57 struct device_attribute *attr,
60 struct stm_device *stm = to_stm_device(dev);
63 ret = sprintf(buf, "%u\n", stm->data->sw_nchannels);
68 static DEVICE_ATTR_RO(channels);
70 static struct attribute *stm_attrs[] = {
71 &dev_attr_masters.attr,
72 &dev_attr_channels.attr,
76 ATTRIBUTE_GROUPS(stm);
78 static struct class stm_class = {
80 .dev_groups = stm_groups,
83 static int stm_dev_match(struct device *dev, const void *data)
85 const char *name = data;
87 return sysfs_streq(name, dev_name(dev));
91 * stm_find_device() - find stm device by name
92 * @buf: character buffer containing the name
94 * This is called when either policy gets assigned to an stm device or an
95 * stm_source device gets linked to an stm device.
97 * This grabs device's reference (get_device()) and module reference, both
98 * of which the calling path needs to make sure to drop with stm_put_device().
100 * Return: stm device pointer or null if lookup failed.
102 struct stm_device *stm_find_device(const char *buf)
104 struct stm_device *stm;
110 dev = class_find_device(&stm_class, NULL, buf, stm_dev_match);
114 stm = to_stm_device(dev);
115 if (!try_module_get(stm->owner)) {
124 * stm_put_device() - drop references on the stm device
125 * @stm: stm device, previously acquired by stm_find_device()
127 * This drops the module reference and device reference taken by
130 void stm_put_device(struct stm_device *stm)
132 module_put(stm->owner);
133 put_device(&stm->dev);
137 * Internally we only care about software-writable masters here, that is the
138 * ones in the range [stm_data->sw_start..stm_data..sw_end], however we need
139 * original master numbers to be visible externally, since they are the ones
140 * that will appear in the STP stream. Thus, the internal bookkeeping uses
141 * $master - stm_data->sw_start to reference master descriptors and such.
144 #define __stm_master(_s, _m) \
145 ((_s)->masters[(_m) - (_s)->data->sw_start])
147 static inline struct stp_master *
148 stm_master(struct stm_device *stm, unsigned int idx)
150 if (idx < stm->data->sw_start || idx > stm->data->sw_end)
153 return __stm_master(stm, idx);
156 static int stp_master_alloc(struct stm_device *stm, unsigned int idx)
158 struct stp_master *master;
161 size = ALIGN(stm->data->sw_nchannels, 8) / 8;
162 size += sizeof(struct stp_master);
163 master = kzalloc(size, GFP_ATOMIC);
167 master->nr_free = stm->data->sw_nchannels;
168 __stm_master(stm, idx) = master;
173 static void stp_master_free(struct stm_device *stm, unsigned int idx)
175 struct stp_master *master = stm_master(stm, idx);
180 __stm_master(stm, idx) = NULL;
184 static void stm_output_claim(struct stm_device *stm, struct stm_output *output)
186 struct stp_master *master = stm_master(stm, output->master);
188 lockdep_assert_held(&stm->mc_lock);
189 lockdep_assert_held(&output->lock);
191 if (WARN_ON_ONCE(master->nr_free < output->nr_chans))
194 bitmap_allocate_region(&master->chan_map[0], output->channel,
195 ilog2(output->nr_chans));
197 master->nr_free -= output->nr_chans;
201 stm_output_disclaim(struct stm_device *stm, struct stm_output *output)
203 struct stp_master *master = stm_master(stm, output->master);
205 lockdep_assert_held(&stm->mc_lock);
206 lockdep_assert_held(&output->lock);
208 bitmap_release_region(&master->chan_map[0], output->channel,
209 ilog2(output->nr_chans));
211 output->nr_chans = 0;
212 master->nr_free += output->nr_chans;
216 * This is like bitmap_find_free_region(), except it can ignore @start bits
219 static int find_free_channels(unsigned long *bitmap, unsigned int start,
220 unsigned int end, unsigned int width)
225 for (pos = start; pos < end + 1; pos = ALIGN(pos, width)) {
226 pos = find_next_zero_bit(bitmap, end + 1, pos);
227 if (pos + width > end + 1)
230 if (pos & (width - 1))
233 for (i = 1; i < width && !test_bit(pos + i, bitmap); i++)
243 stm_find_master_chan(struct stm_device *stm, unsigned int width,
244 unsigned int *mstart, unsigned int mend,
245 unsigned int *cstart, unsigned int cend)
247 struct stp_master *master;
251 for (midx = *mstart; midx <= mend; midx++) {
252 if (!stm_master(stm, midx)) {
253 err = stp_master_alloc(stm, midx);
258 master = stm_master(stm, midx);
260 if (!master->nr_free)
263 pos = find_free_channels(master->chan_map, *cstart, cend,
276 static int stm_output_assign(struct stm_device *stm, unsigned int width,
277 struct stp_policy_node *policy_node,
278 struct stm_output *output)
280 unsigned int midx, cidx, mend, cend;
283 if (width > stm->data->sw_nchannels)
287 stp_policy_node_get_ranges(policy_node,
288 &midx, &mend, &cidx, &cend);
290 midx = stm->data->sw_start;
292 mend = stm->data->sw_end;
293 cend = stm->data->sw_nchannels - 1;
296 spin_lock(&stm->mc_lock);
297 spin_lock(&output->lock);
298 /* output is already assigned -- shouldn't happen */
299 if (WARN_ON_ONCE(output->nr_chans))
302 ret = stm_find_master_chan(stm, width, &midx, mend, &cidx, cend);
306 output->master = midx;
307 output->channel = cidx;
308 output->nr_chans = width;
309 stm_output_claim(stm, output);
310 dev_dbg(&stm->dev, "assigned %u:%u (+%u)\n", midx, cidx, width);
314 spin_unlock(&output->lock);
315 spin_unlock(&stm->mc_lock);
320 static void stm_output_free(struct stm_device *stm, struct stm_output *output)
322 spin_lock(&stm->mc_lock);
323 spin_lock(&output->lock);
324 if (output->nr_chans)
325 stm_output_disclaim(stm, output);
326 spin_unlock(&output->lock);
327 spin_unlock(&stm->mc_lock);
330 static void stm_output_init(struct stm_output *output)
332 spin_lock_init(&output->lock);
335 static int major_match(struct device *dev, const void *data)
337 unsigned int major = *(unsigned int *)data;
339 return MAJOR(dev->devt) == major;
342 static int stm_char_open(struct inode *inode, struct file *file)
344 struct stm_file *stmf;
346 unsigned int major = imajor(inode);
349 dev = class_find_device(&stm_class, NULL, &major, major_match);
353 stmf = kzalloc(sizeof(*stmf), GFP_KERNEL);
357 stm_output_init(&stmf->output);
358 stmf->stm = to_stm_device(dev);
360 if (!try_module_get(stmf->stm->owner))
363 file->private_data = stmf;
365 return nonseekable_open(inode, file);
373 static int stm_char_release(struct inode *inode, struct file *file)
375 struct stm_file *stmf = file->private_data;
377 stm_output_free(stmf->stm, &stmf->output);
378 stm_put_device(stmf->stm);
384 static int stm_file_assign(struct stm_file *stmf, char *id, unsigned int width)
386 struct stm_device *stm = stmf->stm;
389 stmf->policy_node = stp_policy_node_lookup(stm, id);
391 ret = stm_output_assign(stm, width, stmf->policy_node, &stmf->output);
393 if (stmf->policy_node)
394 stp_policy_node_put(stmf->policy_node);
399 static ssize_t stm_write(struct stm_data *data, unsigned int master,
400 unsigned int channel, const char *buf, size_t count)
402 unsigned int flags = STP_PACKET_TIMESTAMPED;
403 const unsigned char *p = buf, nil = 0;
407 for (pos = 0, p = buf; count > pos; pos += sz, p += sz) {
408 sz = min_t(unsigned int, count - pos, 8);
409 sz = data->packet(data, master, channel, STP_PACKET_DATA, flags,
417 data->packet(data, master, channel, STP_PACKET_FLAG, 0, 0, &nil);
422 static ssize_t stm_char_write(struct file *file, const char __user *buf,
423 size_t count, loff_t *ppos)
425 struct stm_file *stmf = file->private_data;
426 struct stm_device *stm = stmf->stm;
430 if (count + 1 > PAGE_SIZE)
431 count = PAGE_SIZE - 1;
434 * if no m/c have been assigned to this writer up to this
435 * point, use "default" policy entry
437 if (!stmf->output.nr_chans) {
438 err = stm_file_assign(stmf, "default", 1);
440 * EBUSY means that somebody else just assigned this
441 * output, which is just fine for write()
443 if (err && err != -EBUSY)
447 kbuf = kmalloc(count + 1, GFP_KERNEL);
451 err = copy_from_user(kbuf, buf, count);
457 count = stm_write(stm->data, stmf->output.master, stmf->output.channel,
465 static int stm_char_mmap(struct file *file, struct vm_area_struct *vma)
467 struct stm_file *stmf = file->private_data;
468 struct stm_device *stm = stmf->stm;
469 unsigned long size, phys;
471 if (!stm->data->mmio_addr)
477 size = vma->vm_end - vma->vm_start;
479 if (stmf->output.nr_chans * stm->data->sw_mmiosz != size)
482 phys = stm->data->mmio_addr(stm->data, stmf->output.master,
483 stmf->output.channel,
484 stmf->output.nr_chans);
489 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
490 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
491 vm_iomap_memory(vma, phys, size);
496 static int stm_char_policy_set_ioctl(struct stm_file *stmf, void __user *arg)
498 struct stm_device *stm = stmf->stm;
499 struct stp_policy_id *id;
503 if (stmf->output.nr_chans)
506 if (copy_from_user(&size, arg, sizeof(size)))
509 if (size >= PATH_MAX + sizeof(*id))
513 * size + 1 to make sure the .id string at the bottom is terminated,
514 * which is also why memdup_user() is not useful here
516 id = kzalloc(size + 1, GFP_KERNEL);
520 if (copy_from_user(id, arg, size)) {
525 if (id->__reserved_0 || id->__reserved_1)
529 id->width > PAGE_SIZE / stm->data->sw_mmiosz)
532 ret = stm_file_assign(stmf, id->id, id->width);
539 ret = stm->data->link(stm->data, stmf->output.master,
540 stmf->output.channel);
543 stm_output_free(stmf->stm, &stmf->output);
544 stm_put_device(stmf->stm);
553 static int stm_char_policy_get_ioctl(struct stm_file *stmf, void __user *arg)
555 struct stp_policy_id id = {
557 .master = stmf->output.master,
558 .channel = stmf->output.channel,
559 .width = stmf->output.nr_chans,
564 return copy_to_user(arg, &id, id.size) ? -EFAULT : 0;
568 stm_char_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
570 struct stm_file *stmf = file->private_data;
571 struct stm_data *stm_data = stmf->stm->data;
576 case STP_POLICY_ID_SET:
577 err = stm_char_policy_set_ioctl(stmf, (void __user *)arg);
581 return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
583 case STP_POLICY_ID_GET:
584 return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
586 case STP_SET_OPTIONS:
587 if (copy_from_user(&options, (u64 __user *)arg, sizeof(u64)))
590 if (stm_data->set_options)
591 err = stm_data->set_options(stm_data,
593 stmf->output.channel,
594 stmf->output.nr_chans,
607 stm_char_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
609 return stm_char_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
612 #define stm_char_compat_ioctl NULL
615 static const struct file_operations stm_fops = {
616 .open = stm_char_open,
617 .release = stm_char_release,
618 .write = stm_char_write,
619 .mmap = stm_char_mmap,
620 .unlocked_ioctl = stm_char_ioctl,
621 .compat_ioctl = stm_char_compat_ioctl,
625 static void stm_device_release(struct device *dev)
627 struct stm_device *stm = to_stm_device(dev);
632 int stm_register_device(struct device *parent, struct stm_data *stm_data,
633 struct module *owner)
635 struct stm_device *stm;
636 unsigned int nmasters;
640 return -EPROBE_DEFER;
642 if (!stm_data->packet || !stm_data->sw_nchannels)
645 nmasters = stm_data->sw_end - stm_data->sw_start;
646 stm = kzalloc(sizeof(*stm) + nmasters * sizeof(void *), GFP_KERNEL);
650 stm->major = register_chrdev(0, stm_data->name, &stm_fops);
654 device_initialize(&stm->dev);
655 stm->dev.devt = MKDEV(stm->major, 0);
656 stm->dev.class = &stm_class;
657 stm->dev.parent = parent;
658 stm->dev.release = stm_device_release;
660 err = kobject_set_name(&stm->dev.kobj, "%s", stm_data->name);
664 err = device_add(&stm->dev);
668 mutex_init(&stm->link_mutex);
669 spin_lock_init(&stm->link_lock);
670 INIT_LIST_HEAD(&stm->link_list);
672 spin_lock_init(&stm->mc_lock);
673 mutex_init(&stm->policy_mutex);
674 stm->sw_nmasters = nmasters;
676 stm->data = stm_data;
682 put_device(&stm->dev);
688 EXPORT_SYMBOL_GPL(stm_register_device);
690 static void __stm_source_link_drop(struct stm_source_device *src,
691 struct stm_device *stm);
693 void stm_unregister_device(struct stm_data *stm_data)
695 struct stm_device *stm = stm_data->stm;
696 struct stm_source_device *src, *iter;
699 mutex_lock(&stm->link_mutex);
700 list_for_each_entry_safe(src, iter, &stm->link_list, link_entry) {
701 __stm_source_link_drop(src, stm);
703 mutex_unlock(&stm->link_mutex);
705 synchronize_srcu(&stm_source_srcu);
707 unregister_chrdev(stm->major, stm_data->name);
709 mutex_lock(&stm->policy_mutex);
711 stp_policy_unbind(stm->policy);
712 mutex_unlock(&stm->policy_mutex);
714 for (i = 0; i < stm->sw_nmasters; i++)
715 stp_master_free(stm, i);
717 device_unregister(&stm->dev);
718 stm_data->stm = NULL;
720 EXPORT_SYMBOL_GPL(stm_unregister_device);
723 * stm::link_list access serialization uses a spinlock and a mutex; holding
724 * either of them guarantees that the list is stable; modification requires
725 * holding both of them.
727 * Lock ordering is as follows:
734 * stm_source_link_add() - connect an stm_source device to an stm device
735 * @src: stm_source device
738 * This function establishes a link from stm_source to an stm device so that
739 * the former can send out trace data to the latter.
741 * Return: 0 on success, -errno otherwise.
743 static int stm_source_link_add(struct stm_source_device *src,
744 struct stm_device *stm)
749 mutex_lock(&stm->link_mutex);
750 spin_lock(&stm->link_lock);
751 spin_lock(&src->link_lock);
753 /* src->link is dereferenced under stm_source_srcu but not the list */
754 rcu_assign_pointer(src->link, stm);
755 list_add_tail(&src->link_entry, &stm->link_list);
757 spin_unlock(&src->link_lock);
758 spin_unlock(&stm->link_lock);
759 mutex_unlock(&stm->link_mutex);
761 id = kstrdup(src->data->name, GFP_KERNEL);
764 stp_policy_node_lookup(stm, id);
769 err = stm_output_assign(stm, src->data->nr_chans,
770 src->policy_node, &src->output);
772 if (src->policy_node)
773 stp_policy_node_put(src->policy_node);
778 /* this is to notify the STM device that a new link has been made */
780 err = stm->data->link(stm->data, src->output.master,
781 src->output.channel);
784 goto fail_free_output;
786 /* this is to let the source carry out all necessary preparations */
788 src->data->link(src->data);
793 stm_output_free(stm, &src->output);
797 mutex_lock(&stm->link_mutex);
798 spin_lock(&stm->link_lock);
799 spin_lock(&src->link_lock);
801 rcu_assign_pointer(src->link, NULL);
802 list_del_init(&src->link_entry);
804 spin_unlock(&src->link_lock);
805 spin_unlock(&stm->link_lock);
806 mutex_unlock(&stm->link_mutex);
812 * __stm_source_link_drop() - detach stm_source from an stm device
813 * @src: stm_source device
816 * If @stm is @src::link, disconnect them from one another and put the
817 * reference on the @stm device.
819 * Caller must hold stm::link_mutex.
821 static void __stm_source_link_drop(struct stm_source_device *src,
822 struct stm_device *stm)
824 struct stm_device *link;
826 lockdep_assert_held(&stm->link_mutex);
828 if (src->data->unlink)
829 src->data->unlink(src->data);
831 /* for stm::link_list modification, we hold both mutex and spinlock */
832 spin_lock(&stm->link_lock);
833 spin_lock(&src->link_lock);
834 link = srcu_dereference_check(src->link, &stm_source_srcu, 1);
835 if (WARN_ON_ONCE(link != stm))
838 stm_output_free(link, &src->output);
839 list_del_init(&src->link_entry);
840 /* matches stm_find_device() from stm_source_link_store() */
841 stm_put_device(link);
842 rcu_assign_pointer(src->link, NULL);
845 spin_unlock(&src->link_lock);
846 spin_unlock(&stm->link_lock);
850 * stm_source_link_drop() - detach stm_source from its stm device
851 * @src: stm_source device
853 * Unlinking means disconnecting from source's STM device; after this
854 * writes will be unsuccessful until it is linked to a new STM device.
856 * This will happen on "stm_source_link" sysfs attribute write to undo
857 * the existing link (if any), or on linked STM device's de-registration.
859 static void stm_source_link_drop(struct stm_source_device *src)
861 struct stm_device *stm;
864 idx = srcu_read_lock(&stm_source_srcu);
865 stm = srcu_dereference(src->link, &stm_source_srcu);
868 mutex_lock(&stm->link_mutex);
869 __stm_source_link_drop(src, stm);
870 mutex_unlock(&stm->link_mutex);
873 srcu_read_unlock(&stm_source_srcu, idx);
876 static ssize_t stm_source_link_show(struct device *dev,
877 struct device_attribute *attr,
880 struct stm_source_device *src = to_stm_source_device(dev);
881 struct stm_device *stm;
884 idx = srcu_read_lock(&stm_source_srcu);
885 stm = srcu_dereference(src->link, &stm_source_srcu);
886 ret = sprintf(buf, "%s\n",
887 stm ? dev_name(&stm->dev) : "<none>");
888 srcu_read_unlock(&stm_source_srcu, idx);
893 static ssize_t stm_source_link_store(struct device *dev,
894 struct device_attribute *attr,
895 const char *buf, size_t count)
897 struct stm_source_device *src = to_stm_source_device(dev);
898 struct stm_device *link;
901 stm_source_link_drop(src);
903 link = stm_find_device(buf);
907 err = stm_source_link_add(src, link);
909 stm_put_device(link);
911 return err ? : count;
914 static DEVICE_ATTR_RW(stm_source_link);
916 static struct attribute *stm_source_attrs[] = {
917 &dev_attr_stm_source_link.attr,
921 ATTRIBUTE_GROUPS(stm_source);
923 static struct class stm_source_class = {
924 .name = "stm_source",
925 .dev_groups = stm_source_groups,
928 static void stm_source_device_release(struct device *dev)
930 struct stm_source_device *src = to_stm_source_device(dev);
936 * stm_source_register_device() - register an stm_source device
937 * @parent: parent device
938 * @data: device description structure
940 * This will create a device of stm_source class that can write
941 * data to an stm device once linked.
943 * Return: 0 on success, -errno otherwise.
945 int stm_source_register_device(struct device *parent,
946 struct stm_source_data *data)
948 struct stm_source_device *src;
952 return -EPROBE_DEFER;
954 src = kzalloc(sizeof(*src), GFP_KERNEL);
958 device_initialize(&src->dev);
959 src->dev.class = &stm_source_class;
960 src->dev.parent = parent;
961 src->dev.release = stm_source_device_release;
963 err = kobject_set_name(&src->dev.kobj, "%s", data->name);
967 err = device_add(&src->dev);
971 stm_output_init(&src->output);
972 spin_lock_init(&src->link_lock);
973 INIT_LIST_HEAD(&src->link_entry);
980 put_device(&src->dev);
985 EXPORT_SYMBOL_GPL(stm_source_register_device);
988 * stm_source_unregister_device() - unregister an stm_source device
989 * @data: device description that was used to register the device
991 * This will remove a previously created stm_source device from the system.
993 void stm_source_unregister_device(struct stm_source_data *data)
995 struct stm_source_device *src = data->src;
997 stm_source_link_drop(src);
999 device_destroy(&stm_source_class, src->dev.devt);
1001 EXPORT_SYMBOL_GPL(stm_source_unregister_device);
1003 int stm_source_write(struct stm_source_data *data, unsigned int chan,
1004 const char *buf, size_t count)
1006 struct stm_source_device *src = data->src;
1007 struct stm_device *stm;
1010 if (!src->output.nr_chans)
1013 if (chan >= src->output.nr_chans)
1016 idx = srcu_read_lock(&stm_source_srcu);
1018 stm = srcu_dereference(src->link, &stm_source_srcu);
1020 count = stm_write(stm->data, src->output.master,
1021 src->output.channel + chan,
1026 srcu_read_unlock(&stm_source_srcu, idx);
1030 EXPORT_SYMBOL_GPL(stm_source_write);
1032 static int __init stm_core_init(void)
1036 err = class_register(&stm_class);
1040 err = class_register(&stm_source_class);
1044 err = stp_configfs_init();
1048 init_srcu_struct(&stm_source_srcu);
1055 class_unregister(&stm_source_class);
1057 class_unregister(&stm_class);
1062 module_init(stm_core_init);
1064 static void __exit stm_core_exit(void)
1066 cleanup_srcu_struct(&stm_source_srcu);
1067 class_unregister(&stm_source_class);
1068 class_unregister(&stm_class);
1069 stp_configfs_exit();
1072 module_exit(stm_core_exit);
1074 MODULE_LICENSE("GPL v2");
1075 MODULE_DESCRIPTION("System Trace Module device class");
1076 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");