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