]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/hwtracing/stm/core.c
Merge branch 'hash' of git://ftp.sciencehorizons.net/linux
[karo-tx-linux.git] / drivers / hwtracing / stm / core.c
1 /*
2  * System Trace Module (STM) infrastructure
3  * Copyright (c) 2014, Intel Corporation.
4  *
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.
8  *
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
12  * more details.
13  *
14  * STM class implements generic infrastructure for  System Trace Module devices
15  * as defined in MIPI STPv2 specification.
16  */
17
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>
27 #include <linux/fs.h>
28 #include <linux/mm.h>
29 #include "stm.h"
30
31 #include <uapi/linux/stm.h>
32
33 static unsigned int stm_core_up;
34
35 /*
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
38  * possible.
39  */
40 static struct srcu_struct stm_source_srcu;
41
42 static ssize_t masters_show(struct device *dev,
43                             struct device_attribute *attr,
44                             char *buf)
45 {
46         struct stm_device *stm = to_stm_device(dev);
47         int ret;
48
49         ret = sprintf(buf, "%u %u\n", stm->data->sw_start, stm->data->sw_end);
50
51         return ret;
52 }
53
54 static DEVICE_ATTR_RO(masters);
55
56 static ssize_t channels_show(struct device *dev,
57                              struct device_attribute *attr,
58                              char *buf)
59 {
60         struct stm_device *stm = to_stm_device(dev);
61         int ret;
62
63         ret = sprintf(buf, "%u\n", stm->data->sw_nchannels);
64
65         return ret;
66 }
67
68 static DEVICE_ATTR_RO(channels);
69
70 static ssize_t hw_override_show(struct device *dev,
71                                 struct device_attribute *attr,
72                                 char *buf)
73 {
74         struct stm_device *stm = to_stm_device(dev);
75         int ret;
76
77         ret = sprintf(buf, "%u\n", stm->data->hw_override);
78
79         return ret;
80 }
81
82 static DEVICE_ATTR_RO(hw_override);
83
84 static struct attribute *stm_attrs[] = {
85         &dev_attr_masters.attr,
86         &dev_attr_channels.attr,
87         &dev_attr_hw_override.attr,
88         NULL,
89 };
90
91 ATTRIBUTE_GROUPS(stm);
92
93 static struct class stm_class = {
94         .name           = "stm",
95         .dev_groups     = stm_groups,
96 };
97
98 static int stm_dev_match(struct device *dev, const void *data)
99 {
100         const char *name = data;
101
102         return sysfs_streq(name, dev_name(dev));
103 }
104
105 /**
106  * stm_find_device() - find stm device by name
107  * @buf:        character buffer containing the name
108  *
109  * This is called when either policy gets assigned to an stm device or an
110  * stm_source device gets linked to an stm device.
111  *
112  * This grabs device's reference (get_device()) and module reference, both
113  * of which the calling path needs to make sure to drop with stm_put_device().
114  *
115  * Return:      stm device pointer or null if lookup failed.
116  */
117 struct stm_device *stm_find_device(const char *buf)
118 {
119         struct stm_device *stm;
120         struct device *dev;
121
122         if (!stm_core_up)
123                 return NULL;
124
125         dev = class_find_device(&stm_class, NULL, buf, stm_dev_match);
126         if (!dev)
127                 return NULL;
128
129         stm = to_stm_device(dev);
130         if (!try_module_get(stm->owner)) {
131                 /* matches class_find_device() above */
132                 put_device(dev);
133                 return NULL;
134         }
135
136         return stm;
137 }
138
139 /**
140  * stm_put_device() - drop references on the stm device
141  * @stm:        stm device, previously acquired by stm_find_device()
142  *
143  * This drops the module reference and device reference taken by
144  * stm_find_device() or stm_char_open().
145  */
146 void stm_put_device(struct stm_device *stm)
147 {
148         module_put(stm->owner);
149         put_device(&stm->dev);
150 }
151
152 /*
153  * Internally we only care about software-writable masters here, that is the
154  * ones in the range [stm_data->sw_start..stm_data..sw_end], however we need
155  * original master numbers to be visible externally, since they are the ones
156  * that will appear in the STP stream. Thus, the internal bookkeeping uses
157  * $master - stm_data->sw_start to reference master descriptors and such.
158  */
159
160 #define __stm_master(_s, _m)                            \
161         ((_s)->masters[(_m) - (_s)->data->sw_start])
162
163 static inline struct stp_master *
164 stm_master(struct stm_device *stm, unsigned int idx)
165 {
166         if (idx < stm->data->sw_start || idx > stm->data->sw_end)
167                 return NULL;
168
169         return __stm_master(stm, idx);
170 }
171
172 static int stp_master_alloc(struct stm_device *stm, unsigned int idx)
173 {
174         struct stp_master *master;
175         size_t size;
176
177         size = ALIGN(stm->data->sw_nchannels, 8) / 8;
178         size += sizeof(struct stp_master);
179         master = kzalloc(size, GFP_ATOMIC);
180         if (!master)
181                 return -ENOMEM;
182
183         master->nr_free = stm->data->sw_nchannels;
184         __stm_master(stm, idx) = master;
185
186         return 0;
187 }
188
189 static void stp_master_free(struct stm_device *stm, unsigned int idx)
190 {
191         struct stp_master *master = stm_master(stm, idx);
192
193         if (!master)
194                 return;
195
196         __stm_master(stm, idx) = NULL;
197         kfree(master);
198 }
199
200 static void stm_output_claim(struct stm_device *stm, struct stm_output *output)
201 {
202         struct stp_master *master = stm_master(stm, output->master);
203
204         lockdep_assert_held(&stm->mc_lock);
205         lockdep_assert_held(&output->lock);
206
207         if (WARN_ON_ONCE(master->nr_free < output->nr_chans))
208                 return;
209
210         bitmap_allocate_region(&master->chan_map[0], output->channel,
211                                ilog2(output->nr_chans));
212
213         master->nr_free -= output->nr_chans;
214 }
215
216 static void
217 stm_output_disclaim(struct stm_device *stm, struct stm_output *output)
218 {
219         struct stp_master *master = stm_master(stm, output->master);
220
221         lockdep_assert_held(&stm->mc_lock);
222         lockdep_assert_held(&output->lock);
223
224         bitmap_release_region(&master->chan_map[0], output->channel,
225                               ilog2(output->nr_chans));
226
227         output->nr_chans = 0;
228         master->nr_free += output->nr_chans;
229 }
230
231 /*
232  * This is like bitmap_find_free_region(), except it can ignore @start bits
233  * at the beginning.
234  */
235 static int find_free_channels(unsigned long *bitmap, unsigned int start,
236                               unsigned int end, unsigned int width)
237 {
238         unsigned int pos;
239         int i;
240
241         for (pos = start; pos < end + 1; pos = ALIGN(pos, width)) {
242                 pos = find_next_zero_bit(bitmap, end + 1, pos);
243                 if (pos + width > end + 1)
244                         break;
245
246                 if (pos & (width - 1))
247                         continue;
248
249                 for (i = 1; i < width && !test_bit(pos + i, bitmap); i++)
250                         ;
251                 if (i == width)
252                         return pos;
253         }
254
255         return -1;
256 }
257
258 static int
259 stm_find_master_chan(struct stm_device *stm, unsigned int width,
260                      unsigned int *mstart, unsigned int mend,
261                      unsigned int *cstart, unsigned int cend)
262 {
263         struct stp_master *master;
264         unsigned int midx;
265         int pos, err;
266
267         for (midx = *mstart; midx <= mend; midx++) {
268                 if (!stm_master(stm, midx)) {
269                         err = stp_master_alloc(stm, midx);
270                         if (err)
271                                 return err;
272                 }
273
274                 master = stm_master(stm, midx);
275
276                 if (!master->nr_free)
277                         continue;
278
279                 pos = find_free_channels(master->chan_map, *cstart, cend,
280                                          width);
281                 if (pos < 0)
282                         continue;
283
284                 *mstart = midx;
285                 *cstart = pos;
286                 return 0;
287         }
288
289         return -ENOSPC;
290 }
291
292 static int stm_output_assign(struct stm_device *stm, unsigned int width,
293                              struct stp_policy_node *policy_node,
294                              struct stm_output *output)
295 {
296         unsigned int midx, cidx, mend, cend;
297         int ret = -EINVAL;
298
299         if (width > stm->data->sw_nchannels)
300                 return -EINVAL;
301
302         if (policy_node) {
303                 stp_policy_node_get_ranges(policy_node,
304                                            &midx, &mend, &cidx, &cend);
305         } else {
306                 midx = stm->data->sw_start;
307                 cidx = 0;
308                 mend = stm->data->sw_end;
309                 cend = stm->data->sw_nchannels - 1;
310         }
311
312         spin_lock(&stm->mc_lock);
313         spin_lock(&output->lock);
314         /* output is already assigned -- shouldn't happen */
315         if (WARN_ON_ONCE(output->nr_chans))
316                 goto unlock;
317
318         ret = stm_find_master_chan(stm, width, &midx, mend, &cidx, cend);
319         if (ret < 0)
320                 goto unlock;
321
322         output->master = midx;
323         output->channel = cidx;
324         output->nr_chans = width;
325         stm_output_claim(stm, output);
326         dev_dbg(&stm->dev, "assigned %u:%u (+%u)\n", midx, cidx, width);
327
328         ret = 0;
329 unlock:
330         spin_unlock(&output->lock);
331         spin_unlock(&stm->mc_lock);
332
333         return ret;
334 }
335
336 static void stm_output_free(struct stm_device *stm, struct stm_output *output)
337 {
338         spin_lock(&stm->mc_lock);
339         spin_lock(&output->lock);
340         if (output->nr_chans)
341                 stm_output_disclaim(stm, output);
342         spin_unlock(&output->lock);
343         spin_unlock(&stm->mc_lock);
344 }
345
346 static void stm_output_init(struct stm_output *output)
347 {
348         spin_lock_init(&output->lock);
349 }
350
351 static int major_match(struct device *dev, const void *data)
352 {
353         unsigned int major = *(unsigned int *)data;
354
355         return MAJOR(dev->devt) == major;
356 }
357
358 static int stm_char_open(struct inode *inode, struct file *file)
359 {
360         struct stm_file *stmf;
361         struct device *dev;
362         unsigned int major = imajor(inode);
363         int err = -ENODEV;
364
365         dev = class_find_device(&stm_class, NULL, &major, major_match);
366         if (!dev)
367                 return -ENODEV;
368
369         stmf = kzalloc(sizeof(*stmf), GFP_KERNEL);
370         if (!stmf)
371                 return -ENOMEM;
372
373         stm_output_init(&stmf->output);
374         stmf->stm = to_stm_device(dev);
375
376         if (!try_module_get(stmf->stm->owner))
377                 goto err_free;
378
379         file->private_data = stmf;
380
381         return nonseekable_open(inode, file);
382
383 err_free:
384         /* matches class_find_device() above */
385         put_device(dev);
386         kfree(stmf);
387
388         return err;
389 }
390
391 static int stm_char_release(struct inode *inode, struct file *file)
392 {
393         struct stm_file *stmf = file->private_data;
394         struct stm_device *stm = stmf->stm;
395
396         if (stm->data->unlink)
397                 stm->data->unlink(stm->data, stmf->output.master,
398                                   stmf->output.channel);
399
400         stm_output_free(stm, &stmf->output);
401
402         /*
403          * matches the stm_char_open()'s
404          * class_find_device() + try_module_get()
405          */
406         stm_put_device(stm);
407         kfree(stmf);
408
409         return 0;
410 }
411
412 static int stm_file_assign(struct stm_file *stmf, char *id, unsigned int width)
413 {
414         struct stm_device *stm = stmf->stm;
415         int ret;
416
417         stmf->policy_node = stp_policy_node_lookup(stm, id);
418
419         ret = stm_output_assign(stm, width, stmf->policy_node, &stmf->output);
420
421         if (stmf->policy_node)
422                 stp_policy_node_put(stmf->policy_node);
423
424         return ret;
425 }
426
427 static ssize_t stm_write(struct stm_data *data, unsigned int master,
428                           unsigned int channel, const char *buf, size_t count)
429 {
430         unsigned int flags = STP_PACKET_TIMESTAMPED;
431         const unsigned char *p = buf, nil = 0;
432         size_t pos;
433         ssize_t sz;
434
435         for (pos = 0, p = buf; count > pos; pos += sz, p += sz) {
436                 sz = min_t(unsigned int, count - pos, 8);
437                 sz = data->packet(data, master, channel, STP_PACKET_DATA, flags,
438                                   sz, p);
439                 flags = 0;
440
441                 if (sz < 0)
442                         break;
443         }
444
445         data->packet(data, master, channel, STP_PACKET_FLAG, 0, 0, &nil);
446
447         return pos;
448 }
449
450 static ssize_t stm_char_write(struct file *file, const char __user *buf,
451                               size_t count, loff_t *ppos)
452 {
453         struct stm_file *stmf = file->private_data;
454         struct stm_device *stm = stmf->stm;
455         char *kbuf;
456         int err;
457
458         if (count + 1 > PAGE_SIZE)
459                 count = PAGE_SIZE - 1;
460
461         /*
462          * if no m/c have been assigned to this writer up to this
463          * point, use "default" policy entry
464          */
465         if (!stmf->output.nr_chans) {
466                 err = stm_file_assign(stmf, "default", 1);
467                 /*
468                  * EBUSY means that somebody else just assigned this
469                  * output, which is just fine for write()
470                  */
471                 if (err && err != -EBUSY)
472                         return err;
473         }
474
475         kbuf = kmalloc(count + 1, GFP_KERNEL);
476         if (!kbuf)
477                 return -ENOMEM;
478
479         err = copy_from_user(kbuf, buf, count);
480         if (err) {
481                 kfree(kbuf);
482                 return -EFAULT;
483         }
484
485         count = stm_write(stm->data, stmf->output.master, stmf->output.channel,
486                           kbuf, count);
487
488         kfree(kbuf);
489
490         return count;
491 }
492
493 static int stm_char_mmap(struct file *file, struct vm_area_struct *vma)
494 {
495         struct stm_file *stmf = file->private_data;
496         struct stm_device *stm = stmf->stm;
497         unsigned long size, phys;
498
499         if (!stm->data->mmio_addr)
500                 return -EOPNOTSUPP;
501
502         if (vma->vm_pgoff)
503                 return -EINVAL;
504
505         size = vma->vm_end - vma->vm_start;
506
507         if (stmf->output.nr_chans * stm->data->sw_mmiosz != size)
508                 return -EINVAL;
509
510         phys = stm->data->mmio_addr(stm->data, stmf->output.master,
511                                     stmf->output.channel,
512                                     stmf->output.nr_chans);
513
514         if (!phys)
515                 return -EINVAL;
516
517         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
518         vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
519         vm_iomap_memory(vma, phys, size);
520
521         return 0;
522 }
523
524 static int stm_char_policy_set_ioctl(struct stm_file *stmf, void __user *arg)
525 {
526         struct stm_device *stm = stmf->stm;
527         struct stp_policy_id *id;
528         int ret = -EINVAL;
529         u32 size;
530
531         if (stmf->output.nr_chans)
532                 return -EBUSY;
533
534         if (copy_from_user(&size, arg, sizeof(size)))
535                 return -EFAULT;
536
537         if (size >= PATH_MAX + sizeof(*id))
538                 return -EINVAL;
539
540         /*
541          * size + 1 to make sure the .id string at the bottom is terminated,
542          * which is also why memdup_user() is not useful here
543          */
544         id = kzalloc(size + 1, GFP_KERNEL);
545         if (!id)
546                 return -ENOMEM;
547
548         if (copy_from_user(id, arg, size)) {
549                 ret = -EFAULT;
550                 goto err_free;
551         }
552
553         if (id->__reserved_0 || id->__reserved_1)
554                 goto err_free;
555
556         if (id->width < 1 ||
557             id->width > PAGE_SIZE / stm->data->sw_mmiosz)
558                 goto err_free;
559
560         ret = stm_file_assign(stmf, id->id, id->width);
561         if (ret)
562                 goto err_free;
563
564         if (stm->data->link)
565                 ret = stm->data->link(stm->data, stmf->output.master,
566                                       stmf->output.channel);
567
568         if (ret)
569                 stm_output_free(stmf->stm, &stmf->output);
570
571 err_free:
572         kfree(id);
573
574         return ret;
575 }
576
577 static int stm_char_policy_get_ioctl(struct stm_file *stmf, void __user *arg)
578 {
579         struct stp_policy_id id = {
580                 .size           = sizeof(id),
581                 .master         = stmf->output.master,
582                 .channel        = stmf->output.channel,
583                 .width          = stmf->output.nr_chans,
584                 .__reserved_0   = 0,
585                 .__reserved_1   = 0,
586         };
587
588         return copy_to_user(arg, &id, id.size) ? -EFAULT : 0;
589 }
590
591 static long
592 stm_char_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
593 {
594         struct stm_file *stmf = file->private_data;
595         struct stm_data *stm_data = stmf->stm->data;
596         int err = -ENOTTY;
597         u64 options;
598
599         switch (cmd) {
600         case STP_POLICY_ID_SET:
601                 err = stm_char_policy_set_ioctl(stmf, (void __user *)arg);
602                 if (err)
603                         return err;
604
605                 return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
606
607         case STP_POLICY_ID_GET:
608                 return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
609
610         case STP_SET_OPTIONS:
611                 if (copy_from_user(&options, (u64 __user *)arg, sizeof(u64)))
612                         return -EFAULT;
613
614                 if (stm_data->set_options)
615                         err = stm_data->set_options(stm_data,
616                                                     stmf->output.master,
617                                                     stmf->output.channel,
618                                                     stmf->output.nr_chans,
619                                                     options);
620
621                 break;
622         default:
623                 break;
624         }
625
626         return err;
627 }
628
629 #ifdef CONFIG_COMPAT
630 static long
631 stm_char_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
632 {
633         return stm_char_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
634 }
635 #else
636 #define stm_char_compat_ioctl   NULL
637 #endif
638
639 static const struct file_operations stm_fops = {
640         .open           = stm_char_open,
641         .release        = stm_char_release,
642         .write          = stm_char_write,
643         .mmap           = stm_char_mmap,
644         .unlocked_ioctl = stm_char_ioctl,
645         .compat_ioctl   = stm_char_compat_ioctl,
646         .llseek         = no_llseek,
647 };
648
649 static void stm_device_release(struct device *dev)
650 {
651         struct stm_device *stm = to_stm_device(dev);
652
653         kfree(stm);
654 }
655
656 int stm_register_device(struct device *parent, struct stm_data *stm_data,
657                         struct module *owner)
658 {
659         struct stm_device *stm;
660         unsigned int nmasters;
661         int err = -ENOMEM;
662
663         if (!stm_core_up)
664                 return -EPROBE_DEFER;
665
666         if (!stm_data->packet || !stm_data->sw_nchannels)
667                 return -EINVAL;
668
669         nmasters = stm_data->sw_end - stm_data->sw_start + 1;
670         stm = kzalloc(sizeof(*stm) + nmasters * sizeof(void *), GFP_KERNEL);
671         if (!stm)
672                 return -ENOMEM;
673
674         stm->major = register_chrdev(0, stm_data->name, &stm_fops);
675         if (stm->major < 0)
676                 goto err_free;
677
678         device_initialize(&stm->dev);
679         stm->dev.devt = MKDEV(stm->major, 0);
680         stm->dev.class = &stm_class;
681         stm->dev.parent = parent;
682         stm->dev.release = stm_device_release;
683
684         mutex_init(&stm->link_mutex);
685         spin_lock_init(&stm->link_lock);
686         INIT_LIST_HEAD(&stm->link_list);
687
688         /* initialize the object before it is accessible via sysfs */
689         spin_lock_init(&stm->mc_lock);
690         mutex_init(&stm->policy_mutex);
691         stm->sw_nmasters = nmasters;
692         stm->owner = owner;
693         stm->data = stm_data;
694         stm_data->stm = stm;
695
696         err = kobject_set_name(&stm->dev.kobj, "%s", stm_data->name);
697         if (err)
698                 goto err_device;
699
700         err = device_add(&stm->dev);
701         if (err)
702                 goto err_device;
703
704         return 0;
705
706 err_device:
707         unregister_chrdev(stm->major, stm_data->name);
708
709         /* matches device_initialize() above */
710         put_device(&stm->dev);
711 err_free:
712         kfree(stm);
713
714         return err;
715 }
716 EXPORT_SYMBOL_GPL(stm_register_device);
717
718 static int __stm_source_link_drop(struct stm_source_device *src,
719                                   struct stm_device *stm);
720
721 void stm_unregister_device(struct stm_data *stm_data)
722 {
723         struct stm_device *stm = stm_data->stm;
724         struct stm_source_device *src, *iter;
725         int i, ret;
726
727         mutex_lock(&stm->link_mutex);
728         list_for_each_entry_safe(src, iter, &stm->link_list, link_entry) {
729                 ret = __stm_source_link_drop(src, stm);
730                 /*
731                  * src <-> stm link must not change under the same
732                  * stm::link_mutex, so complain loudly if it has;
733                  * also in this situation ret!=0 means this src is
734                  * not connected to this stm and it should be otherwise
735                  * safe to proceed with the tear-down of stm.
736                  */
737                 WARN_ON_ONCE(ret);
738         }
739         mutex_unlock(&stm->link_mutex);
740
741         synchronize_srcu(&stm_source_srcu);
742
743         unregister_chrdev(stm->major, stm_data->name);
744
745         mutex_lock(&stm->policy_mutex);
746         if (stm->policy)
747                 stp_policy_unbind(stm->policy);
748         mutex_unlock(&stm->policy_mutex);
749
750         for (i = stm->data->sw_start; i <= stm->data->sw_end; i++)
751                 stp_master_free(stm, i);
752
753         device_unregister(&stm->dev);
754         stm_data->stm = NULL;
755 }
756 EXPORT_SYMBOL_GPL(stm_unregister_device);
757
758 /*
759  * stm::link_list access serialization uses a spinlock and a mutex; holding
760  * either of them guarantees that the list is stable; modification requires
761  * holding both of them.
762  *
763  * Lock ordering is as follows:
764  *   stm::link_mutex
765  *     stm::link_lock
766  *       src::link_lock
767  */
768
769 /**
770  * stm_source_link_add() - connect an stm_source device to an stm device
771  * @src:        stm_source device
772  * @stm:        stm device
773  *
774  * This function establishes a link from stm_source to an stm device so that
775  * the former can send out trace data to the latter.
776  *
777  * Return:      0 on success, -errno otherwise.
778  */
779 static int stm_source_link_add(struct stm_source_device *src,
780                                struct stm_device *stm)
781 {
782         char *id;
783         int err;
784
785         mutex_lock(&stm->link_mutex);
786         spin_lock(&stm->link_lock);
787         spin_lock(&src->link_lock);
788
789         /* src->link is dereferenced under stm_source_srcu but not the list */
790         rcu_assign_pointer(src->link, stm);
791         list_add_tail(&src->link_entry, &stm->link_list);
792
793         spin_unlock(&src->link_lock);
794         spin_unlock(&stm->link_lock);
795         mutex_unlock(&stm->link_mutex);
796
797         id = kstrdup(src->data->name, GFP_KERNEL);
798         if (id) {
799                 src->policy_node =
800                         stp_policy_node_lookup(stm, id);
801
802                 kfree(id);
803         }
804
805         err = stm_output_assign(stm, src->data->nr_chans,
806                                 src->policy_node, &src->output);
807
808         if (src->policy_node)
809                 stp_policy_node_put(src->policy_node);
810
811         if (err)
812                 goto fail_detach;
813
814         /* this is to notify the STM device that a new link has been made */
815         if (stm->data->link)
816                 err = stm->data->link(stm->data, src->output.master,
817                                       src->output.channel);
818
819         if (err)
820                 goto fail_free_output;
821
822         /* this is to let the source carry out all necessary preparations */
823         if (src->data->link)
824                 src->data->link(src->data);
825
826         return 0;
827
828 fail_free_output:
829         stm_output_free(stm, &src->output);
830
831 fail_detach:
832         mutex_lock(&stm->link_mutex);
833         spin_lock(&stm->link_lock);
834         spin_lock(&src->link_lock);
835
836         rcu_assign_pointer(src->link, NULL);
837         list_del_init(&src->link_entry);
838
839         spin_unlock(&src->link_lock);
840         spin_unlock(&stm->link_lock);
841         mutex_unlock(&stm->link_mutex);
842
843         return err;
844 }
845
846 /**
847  * __stm_source_link_drop() - detach stm_source from an stm device
848  * @src:        stm_source device
849  * @stm:        stm device
850  *
851  * If @stm is @src::link, disconnect them from one another and put the
852  * reference on the @stm device.
853  *
854  * Caller must hold stm::link_mutex.
855  */
856 static int __stm_source_link_drop(struct stm_source_device *src,
857                                   struct stm_device *stm)
858 {
859         struct stm_device *link;
860         int ret = 0;
861
862         lockdep_assert_held(&stm->link_mutex);
863
864         /* for stm::link_list modification, we hold both mutex and spinlock */
865         spin_lock(&stm->link_lock);
866         spin_lock(&src->link_lock);
867         link = srcu_dereference_check(src->link, &stm_source_srcu, 1);
868
869         /*
870          * The linked device may have changed since we last looked, because
871          * we weren't holding the src::link_lock back then; if this is the
872          * case, tell the caller to retry.
873          */
874         if (link != stm) {
875                 ret = -EAGAIN;
876                 goto unlock;
877         }
878
879         stm_output_free(link, &src->output);
880         list_del_init(&src->link_entry);
881         /* matches stm_find_device() from stm_source_link_store() */
882         stm_put_device(link);
883         rcu_assign_pointer(src->link, NULL);
884
885 unlock:
886         spin_unlock(&src->link_lock);
887         spin_unlock(&stm->link_lock);
888
889         /*
890          * Call the unlink callbacks for both source and stm, when we know
891          * that we have actually performed the unlinking.
892          */
893         if (!ret) {
894                 if (src->data->unlink)
895                         src->data->unlink(src->data);
896
897                 if (stm->data->unlink)
898                         stm->data->unlink(stm->data, src->output.master,
899                                           src->output.channel);
900         }
901
902         return ret;
903 }
904
905 /**
906  * stm_source_link_drop() - detach stm_source from its stm device
907  * @src:        stm_source device
908  *
909  * Unlinking means disconnecting from source's STM device; after this
910  * writes will be unsuccessful until it is linked to a new STM device.
911  *
912  * This will happen on "stm_source_link" sysfs attribute write to undo
913  * the existing link (if any), or on linked STM device's de-registration.
914  */
915 static void stm_source_link_drop(struct stm_source_device *src)
916 {
917         struct stm_device *stm;
918         int idx, ret;
919
920 retry:
921         idx = srcu_read_lock(&stm_source_srcu);
922         /*
923          * The stm device will be valid for the duration of this
924          * read section, but the link may change before we grab
925          * the src::link_lock in __stm_source_link_drop().
926          */
927         stm = srcu_dereference(src->link, &stm_source_srcu);
928
929         ret = 0;
930         if (stm) {
931                 mutex_lock(&stm->link_mutex);
932                 ret = __stm_source_link_drop(src, stm);
933                 mutex_unlock(&stm->link_mutex);
934         }
935
936         srcu_read_unlock(&stm_source_srcu, idx);
937
938         /* if it did change, retry */
939         if (ret == -EAGAIN)
940                 goto retry;
941 }
942
943 static ssize_t stm_source_link_show(struct device *dev,
944                                     struct device_attribute *attr,
945                                     char *buf)
946 {
947         struct stm_source_device *src = to_stm_source_device(dev);
948         struct stm_device *stm;
949         int idx, ret;
950
951         idx = srcu_read_lock(&stm_source_srcu);
952         stm = srcu_dereference(src->link, &stm_source_srcu);
953         ret = sprintf(buf, "%s\n",
954                       stm ? dev_name(&stm->dev) : "<none>");
955         srcu_read_unlock(&stm_source_srcu, idx);
956
957         return ret;
958 }
959
960 static ssize_t stm_source_link_store(struct device *dev,
961                                      struct device_attribute *attr,
962                                      const char *buf, size_t count)
963 {
964         struct stm_source_device *src = to_stm_source_device(dev);
965         struct stm_device *link;
966         int err;
967
968         stm_source_link_drop(src);
969
970         link = stm_find_device(buf);
971         if (!link)
972                 return -EINVAL;
973
974         err = stm_source_link_add(src, link);
975         if (err) {
976                 /* matches the stm_find_device() above */
977                 stm_put_device(link);
978         }
979
980         return err ? : count;
981 }
982
983 static DEVICE_ATTR_RW(stm_source_link);
984
985 static struct attribute *stm_source_attrs[] = {
986         &dev_attr_stm_source_link.attr,
987         NULL,
988 };
989
990 ATTRIBUTE_GROUPS(stm_source);
991
992 static struct class stm_source_class = {
993         .name           = "stm_source",
994         .dev_groups     = stm_source_groups,
995 };
996
997 static void stm_source_device_release(struct device *dev)
998 {
999         struct stm_source_device *src = to_stm_source_device(dev);
1000
1001         kfree(src);
1002 }
1003
1004 /**
1005  * stm_source_register_device() - register an stm_source device
1006  * @parent:     parent device
1007  * @data:       device description structure
1008  *
1009  * This will create a device of stm_source class that can write
1010  * data to an stm device once linked.
1011  *
1012  * Return:      0 on success, -errno otherwise.
1013  */
1014 int stm_source_register_device(struct device *parent,
1015                                struct stm_source_data *data)
1016 {
1017         struct stm_source_device *src;
1018         int err;
1019
1020         if (!stm_core_up)
1021                 return -EPROBE_DEFER;
1022
1023         src = kzalloc(sizeof(*src), GFP_KERNEL);
1024         if (!src)
1025                 return -ENOMEM;
1026
1027         device_initialize(&src->dev);
1028         src->dev.class = &stm_source_class;
1029         src->dev.parent = parent;
1030         src->dev.release = stm_source_device_release;
1031
1032         err = kobject_set_name(&src->dev.kobj, "%s", data->name);
1033         if (err)
1034                 goto err;
1035
1036         err = device_add(&src->dev);
1037         if (err)
1038                 goto err;
1039
1040         stm_output_init(&src->output);
1041         spin_lock_init(&src->link_lock);
1042         INIT_LIST_HEAD(&src->link_entry);
1043         src->data = data;
1044         data->src = src;
1045
1046         return 0;
1047
1048 err:
1049         put_device(&src->dev);
1050         kfree(src);
1051
1052         return err;
1053 }
1054 EXPORT_SYMBOL_GPL(stm_source_register_device);
1055
1056 /**
1057  * stm_source_unregister_device() - unregister an stm_source device
1058  * @data:       device description that was used to register the device
1059  *
1060  * This will remove a previously created stm_source device from the system.
1061  */
1062 void stm_source_unregister_device(struct stm_source_data *data)
1063 {
1064         struct stm_source_device *src = data->src;
1065
1066         stm_source_link_drop(src);
1067
1068         device_destroy(&stm_source_class, src->dev.devt);
1069 }
1070 EXPORT_SYMBOL_GPL(stm_source_unregister_device);
1071
1072 int stm_source_write(struct stm_source_data *data, unsigned int chan,
1073                      const char *buf, size_t count)
1074 {
1075         struct stm_source_device *src = data->src;
1076         struct stm_device *stm;
1077         int idx;
1078
1079         if (!src->output.nr_chans)
1080                 return -ENODEV;
1081
1082         if (chan >= src->output.nr_chans)
1083                 return -EINVAL;
1084
1085         idx = srcu_read_lock(&stm_source_srcu);
1086
1087         stm = srcu_dereference(src->link, &stm_source_srcu);
1088         if (stm)
1089                 count = stm_write(stm->data, src->output.master,
1090                                   src->output.channel + chan,
1091                                   buf, count);
1092         else
1093                 count = -ENODEV;
1094
1095         srcu_read_unlock(&stm_source_srcu, idx);
1096
1097         return count;
1098 }
1099 EXPORT_SYMBOL_GPL(stm_source_write);
1100
1101 static int __init stm_core_init(void)
1102 {
1103         int err;
1104
1105         err = class_register(&stm_class);
1106         if (err)
1107                 return err;
1108
1109         err = class_register(&stm_source_class);
1110         if (err)
1111                 goto err_stm;
1112
1113         err = stp_configfs_init();
1114         if (err)
1115                 goto err_src;
1116
1117         init_srcu_struct(&stm_source_srcu);
1118
1119         stm_core_up++;
1120
1121         return 0;
1122
1123 err_src:
1124         class_unregister(&stm_source_class);
1125 err_stm:
1126         class_unregister(&stm_class);
1127
1128         return err;
1129 }
1130
1131 module_init(stm_core_init);
1132
1133 static void __exit stm_core_exit(void)
1134 {
1135         cleanup_srcu_struct(&stm_source_srcu);
1136         class_unregister(&stm_source_class);
1137         class_unregister(&stm_class);
1138         stp_configfs_exit();
1139 }
1140
1141 module_exit(stm_core_exit);
1142
1143 MODULE_LICENSE("GPL v2");
1144 MODULE_DESCRIPTION("System Trace Module device class");
1145 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");