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 if (WARN_ON_ONCE(master->nr_free < output->nr_chans))
191 bitmap_allocate_region(&master->chan_map[0], output->channel,
192 ilog2(output->nr_chans));
194 master->nr_free -= output->nr_chans;
198 stm_output_disclaim(struct stm_device *stm, struct stm_output *output)
200 struct stp_master *master = stm_master(stm, output->master);
202 bitmap_release_region(&master->chan_map[0], output->channel,
203 ilog2(output->nr_chans));
205 output->nr_chans = 0;
206 master->nr_free += output->nr_chans;
210 * This is like bitmap_find_free_region(), except it can ignore @start bits
213 static int find_free_channels(unsigned long *bitmap, unsigned int start,
214 unsigned int end, unsigned int width)
219 for (pos = start; pos < end + 1; pos = ALIGN(pos, width)) {
220 pos = find_next_zero_bit(bitmap, end + 1, pos);
221 if (pos + width > end + 1)
224 if (pos & (width - 1))
227 for (i = 1; i < width && !test_bit(pos + i, bitmap); i++)
237 stm_find_master_chan(struct stm_device *stm, unsigned int width,
238 unsigned int *mstart, unsigned int mend,
239 unsigned int *cstart, unsigned int cend)
241 struct stp_master *master;
245 for (midx = *mstart; midx <= mend; midx++) {
246 if (!stm_master(stm, midx)) {
247 err = stp_master_alloc(stm, midx);
252 master = stm_master(stm, midx);
254 if (!master->nr_free)
257 pos = find_free_channels(master->chan_map, *cstart, cend,
270 static int stm_output_assign(struct stm_device *stm, unsigned int width,
271 struct stp_policy_node *policy_node,
272 struct stm_output *output)
274 unsigned int midx, cidx, mend, cend;
277 if (width > stm->data->sw_nchannels)
281 stp_policy_node_get_ranges(policy_node,
282 &midx, &mend, &cidx, &cend);
284 midx = stm->data->sw_start;
286 mend = stm->data->sw_end;
287 cend = stm->data->sw_nchannels - 1;
290 spin_lock(&stm->mc_lock);
291 /* output is already assigned -- shouldn't happen */
292 if (WARN_ON_ONCE(output->nr_chans))
295 ret = stm_find_master_chan(stm, width, &midx, mend, &cidx, cend);
299 output->master = midx;
300 output->channel = cidx;
301 output->nr_chans = width;
302 stm_output_claim(stm, output);
303 dev_dbg(&stm->dev, "assigned %u:%u (+%u)\n", midx, cidx, width);
307 spin_unlock(&stm->mc_lock);
312 static void stm_output_free(struct stm_device *stm, struct stm_output *output)
314 spin_lock(&stm->mc_lock);
315 if (output->nr_chans)
316 stm_output_disclaim(stm, output);
317 spin_unlock(&stm->mc_lock);
320 static int major_match(struct device *dev, const void *data)
322 unsigned int major = *(unsigned int *)data;
324 return MAJOR(dev->devt) == major;
327 static int stm_char_open(struct inode *inode, struct file *file)
329 struct stm_file *stmf;
331 unsigned int major = imajor(inode);
334 dev = class_find_device(&stm_class, NULL, &major, major_match);
338 stmf = kzalloc(sizeof(*stmf), GFP_KERNEL);
342 stmf->stm = to_stm_device(dev);
344 if (!try_module_get(stmf->stm->owner))
347 file->private_data = stmf;
349 return nonseekable_open(inode, file);
357 static int stm_char_release(struct inode *inode, struct file *file)
359 struct stm_file *stmf = file->private_data;
361 stm_output_free(stmf->stm, &stmf->output);
362 stm_put_device(stmf->stm);
368 static int stm_file_assign(struct stm_file *stmf, char *id, unsigned int width)
370 struct stm_device *stm = stmf->stm;
373 stmf->policy_node = stp_policy_node_lookup(stm, id);
375 ret = stm_output_assign(stm, width, stmf->policy_node, &stmf->output);
377 if (stmf->policy_node)
378 stp_policy_node_put(stmf->policy_node);
383 static void stm_write(struct stm_data *data, unsigned int master,
384 unsigned int channel, const char *buf, size_t count)
386 unsigned int flags = STP_PACKET_TIMESTAMPED;
387 const unsigned char *p = buf, nil = 0;
391 for (pos = 0, p = buf; count > pos; pos += sz, p += sz) {
392 sz = min_t(unsigned int, count - pos, 8);
393 sz = data->packet(data, master, channel, STP_PACKET_DATA, flags,
398 data->packet(data, master, channel, STP_PACKET_FLAG, 0, 0, &nil);
401 static ssize_t stm_char_write(struct file *file, const char __user *buf,
402 size_t count, loff_t *ppos)
404 struct stm_file *stmf = file->private_data;
405 struct stm_device *stm = stmf->stm;
410 * if no m/c have been assigned to this writer up to this
411 * point, use "default" policy entry
413 if (!stmf->output.nr_chans) {
414 err = stm_file_assign(stmf, "default", 1);
416 * EBUSY means that somebody else just assigned this
417 * output, which is just fine for write()
419 if (err && err != -EBUSY)
423 kbuf = kmalloc(count + 1, GFP_KERNEL);
427 err = copy_from_user(kbuf, buf, count);
433 stm_write(stm->data, stmf->output.master, stmf->output.channel, kbuf,
441 static int stm_char_mmap(struct file *file, struct vm_area_struct *vma)
443 struct stm_file *stmf = file->private_data;
444 struct stm_device *stm = stmf->stm;
445 unsigned long size, phys;
447 if (!stm->data->mmio_addr)
453 size = vma->vm_end - vma->vm_start;
455 if (stmf->output.nr_chans * stm->data->sw_mmiosz != size)
458 phys = stm->data->mmio_addr(stm->data, stmf->output.master,
459 stmf->output.channel,
460 stmf->output.nr_chans);
465 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
466 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
467 vm_iomap_memory(vma, phys, size);
472 static int stm_char_policy_set_ioctl(struct stm_file *stmf, void __user *arg)
474 struct stm_device *stm = stmf->stm;
475 struct stp_policy_id *id;
479 if (stmf->output.nr_chans)
482 if (copy_from_user(&size, arg, sizeof(size)))
485 if (size >= PATH_MAX + sizeof(*id))
489 * size + 1 to make sure the .id string at the bottom is terminated,
490 * which is also why memdup_user() is not useful here
492 id = kzalloc(size + 1, GFP_KERNEL);
496 if (copy_from_user(id, arg, size)) {
501 if (id->__reserved_0 || id->__reserved_1)
505 id->width > PAGE_SIZE / stm->data->sw_mmiosz)
508 ret = stm_file_assign(stmf, id->id, id->width);
515 ret = stm->data->link(stm->data, stmf->output.master,
516 stmf->output.channel);
519 stm_output_free(stmf->stm, &stmf->output);
520 stm_put_device(stmf->stm);
529 static int stm_char_policy_get_ioctl(struct stm_file *stmf, void __user *arg)
531 struct stp_policy_id id = {
533 .master = stmf->output.master,
534 .channel = stmf->output.channel,
535 .width = stmf->output.nr_chans,
540 return copy_to_user(arg, &id, id.size) ? -EFAULT : 0;
544 stm_char_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
546 struct stm_file *stmf = file->private_data;
547 struct stm_data *stm_data = stmf->stm->data;
552 case STP_POLICY_ID_SET:
553 err = stm_char_policy_set_ioctl(stmf, (void __user *)arg);
557 return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
559 case STP_POLICY_ID_GET:
560 return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
562 case STP_SET_OPTIONS:
563 if (copy_from_user(&options, (u64 __user *)arg, sizeof(u64)))
566 if (stm_data->set_options)
567 err = stm_data->set_options(stm_data,
569 stmf->output.channel,
570 stmf->output.nr_chans,
583 stm_char_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
585 return stm_char_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
588 #define stm_char_compat_ioctl NULL
591 static const struct file_operations stm_fops = {
592 .open = stm_char_open,
593 .release = stm_char_release,
594 .write = stm_char_write,
595 .mmap = stm_char_mmap,
596 .unlocked_ioctl = stm_char_ioctl,
597 .compat_ioctl = stm_char_compat_ioctl,
601 static void stm_device_release(struct device *dev)
603 struct stm_device *stm = to_stm_device(dev);
608 int stm_register_device(struct device *parent, struct stm_data *stm_data,
609 struct module *owner)
611 struct stm_device *stm;
612 unsigned int nmasters;
616 return -EPROBE_DEFER;
618 if (!stm_data->packet || !stm_data->sw_nchannels)
621 nmasters = stm_data->sw_end - stm_data->sw_start;
622 stm = kzalloc(sizeof(*stm) + nmasters * sizeof(void *), GFP_KERNEL);
626 stm->major = register_chrdev(0, stm_data->name, &stm_fops);
630 device_initialize(&stm->dev);
631 stm->dev.devt = MKDEV(stm->major, 0);
632 stm->dev.class = &stm_class;
633 stm->dev.parent = parent;
634 stm->dev.release = stm_device_release;
636 err = kobject_set_name(&stm->dev.kobj, "%s", stm_data->name);
640 err = device_add(&stm->dev);
644 spin_lock_init(&stm->link_lock);
645 INIT_LIST_HEAD(&stm->link_list);
647 spin_lock_init(&stm->mc_lock);
648 mutex_init(&stm->policy_mutex);
649 stm->sw_nmasters = nmasters;
651 stm->data = stm_data;
657 put_device(&stm->dev);
663 EXPORT_SYMBOL_GPL(stm_register_device);
665 static void __stm_source_link_drop(struct stm_source_device *src,
666 struct stm_device *stm);
668 void stm_unregister_device(struct stm_data *stm_data)
670 struct stm_device *stm = stm_data->stm;
671 struct stm_source_device *src, *iter;
674 spin_lock(&stm->link_lock);
675 list_for_each_entry_safe(src, iter, &stm->link_list, link_entry) {
676 __stm_source_link_drop(src, stm);
678 spin_unlock(&stm->link_lock);
680 synchronize_srcu(&stm_source_srcu);
682 unregister_chrdev(stm->major, stm_data->name);
684 mutex_lock(&stm->policy_mutex);
686 stp_policy_unbind(stm->policy);
687 mutex_unlock(&stm->policy_mutex);
689 for (i = 0; i < stm->sw_nmasters; i++)
690 stp_master_free(stm, i);
692 device_unregister(&stm->dev);
693 stm_data->stm = NULL;
695 EXPORT_SYMBOL_GPL(stm_unregister_device);
698 * stm_source_link_add() - connect an stm_source device to an stm device
699 * @src: stm_source device
702 * This function establishes a link from stm_source to an stm device so that
703 * the former can send out trace data to the latter.
705 * Return: 0 on success, -errno otherwise.
707 static int stm_source_link_add(struct stm_source_device *src,
708 struct stm_device *stm)
713 spin_lock(&stm->link_lock);
714 spin_lock(&src->link_lock);
716 /* src->link is dereferenced under stm_source_srcu but not the list */
717 rcu_assign_pointer(src->link, stm);
718 list_add_tail(&src->link_entry, &stm->link_list);
720 spin_unlock(&src->link_lock);
721 spin_unlock(&stm->link_lock);
723 id = kstrdup(src->data->name, GFP_KERNEL);
726 stp_policy_node_lookup(stm, id);
731 err = stm_output_assign(stm, src->data->nr_chans,
732 src->policy_node, &src->output);
734 if (src->policy_node)
735 stp_policy_node_put(src->policy_node);
740 /* this is to notify the STM device that a new link has been made */
742 err = stm->data->link(stm->data, src->output.master,
743 src->output.channel);
746 goto fail_free_output;
748 /* this is to let the source carry out all necessary preparations */
750 src->data->link(src->data);
755 stm_output_free(stm, &src->output);
759 spin_lock(&stm->link_lock);
760 spin_lock(&src->link_lock);
762 rcu_assign_pointer(src->link, NULL);
763 list_del_init(&src->link_entry);
765 spin_unlock(&src->link_lock);
766 spin_unlock(&stm->link_lock);
772 * __stm_source_link_drop() - detach stm_source from an stm device
773 * @src: stm_source device
776 * If @stm is @src::link, disconnect them from one another and put the
777 * reference on the @stm device.
779 * Caller must hold stm::link_lock.
781 static void __stm_source_link_drop(struct stm_source_device *src,
782 struct stm_device *stm)
784 struct stm_device *link;
786 spin_lock(&src->link_lock);
787 link = srcu_dereference_check(src->link, &stm_source_srcu, 1);
788 if (WARN_ON_ONCE(link != stm)) {
789 spin_unlock(&src->link_lock);
793 stm_output_free(link, &src->output);
794 /* caller must hold stm::link_lock */
795 list_del_init(&src->link_entry);
796 /* matches stm_find_device() from stm_source_link_store() */
797 stm_put_device(link);
798 rcu_assign_pointer(src->link, NULL);
800 spin_unlock(&src->link_lock);
804 * stm_source_link_drop() - detach stm_source from its stm device
805 * @src: stm_source device
807 * Unlinking means disconnecting from source's STM device; after this
808 * writes will be unsuccessful until it is linked to a new STM device.
810 * This will happen on "stm_source_link" sysfs attribute write to undo
811 * the existing link (if any), or on linked STM device's de-registration.
813 static void stm_source_link_drop(struct stm_source_device *src)
815 struct stm_device *stm;
818 idx = srcu_read_lock(&stm_source_srcu);
819 stm = srcu_dereference(src->link, &stm_source_srcu);
822 if (src->data->unlink)
823 src->data->unlink(src->data);
825 spin_lock(&stm->link_lock);
826 __stm_source_link_drop(src, stm);
827 spin_unlock(&stm->link_lock);
830 srcu_read_unlock(&stm_source_srcu, idx);
833 static ssize_t stm_source_link_show(struct device *dev,
834 struct device_attribute *attr,
837 struct stm_source_device *src = to_stm_source_device(dev);
838 struct stm_device *stm;
841 idx = srcu_read_lock(&stm_source_srcu);
842 stm = srcu_dereference(src->link, &stm_source_srcu);
843 ret = sprintf(buf, "%s\n",
844 stm ? dev_name(&stm->dev) : "<none>");
845 srcu_read_unlock(&stm_source_srcu, idx);
850 static ssize_t stm_source_link_store(struct device *dev,
851 struct device_attribute *attr,
852 const char *buf, size_t count)
854 struct stm_source_device *src = to_stm_source_device(dev);
855 struct stm_device *link;
858 stm_source_link_drop(src);
860 link = stm_find_device(buf);
864 err = stm_source_link_add(src, link);
866 stm_put_device(link);
868 return err ? : count;
871 static DEVICE_ATTR_RW(stm_source_link);
873 static struct attribute *stm_source_attrs[] = {
874 &dev_attr_stm_source_link.attr,
878 ATTRIBUTE_GROUPS(stm_source);
880 static struct class stm_source_class = {
881 .name = "stm_source",
882 .dev_groups = stm_source_groups,
885 static void stm_source_device_release(struct device *dev)
887 struct stm_source_device *src = to_stm_source_device(dev);
893 * stm_source_register_device() - register an stm_source device
894 * @parent: parent device
895 * @data: device description structure
897 * This will create a device of stm_source class that can write
898 * data to an stm device once linked.
900 * Return: 0 on success, -errno otherwise.
902 int stm_source_register_device(struct device *parent,
903 struct stm_source_data *data)
905 struct stm_source_device *src;
909 return -EPROBE_DEFER;
911 src = kzalloc(sizeof(*src), GFP_KERNEL);
915 device_initialize(&src->dev);
916 src->dev.class = &stm_source_class;
917 src->dev.parent = parent;
918 src->dev.release = stm_source_device_release;
920 err = kobject_set_name(&src->dev.kobj, "%s", data->name);
924 err = device_add(&src->dev);
928 spin_lock_init(&src->link_lock);
929 INIT_LIST_HEAD(&src->link_entry);
936 put_device(&src->dev);
941 EXPORT_SYMBOL_GPL(stm_source_register_device);
944 * stm_source_unregister_device() - unregister an stm_source device
945 * @data: device description that was used to register the device
947 * This will remove a previously created stm_source device from the system.
949 void stm_source_unregister_device(struct stm_source_data *data)
951 struct stm_source_device *src = data->src;
953 stm_source_link_drop(src);
955 device_destroy(&stm_source_class, src->dev.devt);
957 EXPORT_SYMBOL_GPL(stm_source_unregister_device);
959 int stm_source_write(struct stm_source_data *data, unsigned int chan,
960 const char *buf, size_t count)
962 struct stm_source_device *src = data->src;
963 struct stm_device *stm;
966 if (!src->output.nr_chans)
969 if (chan >= src->output.nr_chans)
972 idx = srcu_read_lock(&stm_source_srcu);
974 stm = srcu_dereference(src->link, &stm_source_srcu);
976 stm_write(stm->data, src->output.master,
977 src->output.channel + chan,
982 srcu_read_unlock(&stm_source_srcu, idx);
986 EXPORT_SYMBOL_GPL(stm_source_write);
988 static int __init stm_core_init(void)
992 err = class_register(&stm_class);
996 err = class_register(&stm_source_class);
1000 err = stp_configfs_init();
1004 init_srcu_struct(&stm_source_srcu);
1011 class_unregister(&stm_source_class);
1013 class_unregister(&stm_class);
1018 module_init(stm_core_init);
1020 static void __exit stm_core_exit(void)
1022 cleanup_srcu_struct(&stm_source_srcu);
1023 class_unregister(&stm_source_class);
1024 class_unregister(&stm_class);
1025 stp_configfs_exit();
1028 module_exit(stm_core_exit);
1030 MODULE_LICENSE("GPL v2");
1031 MODULE_DESCRIPTION("System Trace Module device class");
1032 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");