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