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