blob: 05386b76465ed56105436b4d3b15da98d8051bf1 [file] [log] [blame]
Alexander Shishkin9ea393d2018-03-28 18:43:57 +03001// SPDX-License-Identifier: GPL-2.0
Alexander Shishkin7bd1d402015-09-22 15:47:10 +03002/*
3 * System Trace Module (STM) infrastructure
4 * Copyright (c) 2014, Intel Corporation.
5 *
Alexander Shishkin7bd1d402015-09-22 15:47:10 +03006 * STM class implements generic infrastructure for System Trace Module devices
7 * as defined in MIPI STPv2 specification.
8 */
9
Alexander Shishkin8e0469a2016-06-28 11:35:02 +030010#include <linux/pm_runtime.h>
Alexander Shishkin7bd1d402015-09-22 15:47:10 +030011#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 "stm.h"
23
24#include <uapi/linux/stm.h>
25
26static unsigned int stm_core_up;
27
28/*
29 * The SRCU here makes sure that STM device doesn't disappear from under a
30 * stm_source_write() caller, which may want to have as little overhead as
31 * possible.
32 */
33static struct srcu_struct stm_source_srcu;
34
35static ssize_t masters_show(struct device *dev,
36 struct device_attribute *attr,
37 char *buf)
38{
39 struct stm_device *stm = to_stm_device(dev);
40 int ret;
41
42 ret = sprintf(buf, "%u %u\n", stm->data->sw_start, stm->data->sw_end);
43
44 return ret;
45}
46
47static DEVICE_ATTR_RO(masters);
48
49static ssize_t channels_show(struct device *dev,
50 struct device_attribute *attr,
51 char *buf)
52{
53 struct stm_device *stm = to_stm_device(dev);
54 int ret;
55
56 ret = sprintf(buf, "%u\n", stm->data->sw_nchannels);
57
58 return ret;
59}
60
61static DEVICE_ATTR_RO(channels);
62
Alexander Shishkin8e996a22016-05-03 11:33:37 -060063static ssize_t hw_override_show(struct device *dev,
64 struct device_attribute *attr,
65 char *buf)
66{
67 struct stm_device *stm = to_stm_device(dev);
68 int ret;
69
70 ret = sprintf(buf, "%u\n", stm->data->hw_override);
71
72 return ret;
73}
74
75static DEVICE_ATTR_RO(hw_override);
76
Alexander Shishkin7bd1d402015-09-22 15:47:10 +030077static struct attribute *stm_attrs[] = {
78 &dev_attr_masters.attr,
79 &dev_attr_channels.attr,
Alexander Shishkin8e996a22016-05-03 11:33:37 -060080 &dev_attr_hw_override.attr,
Alexander Shishkin7bd1d402015-09-22 15:47:10 +030081 NULL,
82};
83
84ATTRIBUTE_GROUPS(stm);
85
86static struct class stm_class = {
87 .name = "stm",
88 .dev_groups = stm_groups,
89};
90
91static int stm_dev_match(struct device *dev, const void *data)
92{
93 const char *name = data;
94
95 return sysfs_streq(name, dev_name(dev));
96}
97
98/**
99 * stm_find_device() - find stm device by name
100 * @buf: character buffer containing the name
101 *
102 * This is called when either policy gets assigned to an stm device or an
103 * stm_source device gets linked to an stm device.
104 *
105 * This grabs device's reference (get_device()) and module reference, both
106 * of which the calling path needs to make sure to drop with stm_put_device().
107 *
108 * Return: stm device pointer or null if lookup failed.
109 */
110struct stm_device *stm_find_device(const char *buf)
111{
112 struct stm_device *stm;
113 struct device *dev;
114
115 if (!stm_core_up)
116 return NULL;
117
118 dev = class_find_device(&stm_class, NULL, buf, stm_dev_match);
119 if (!dev)
120 return NULL;
121
122 stm = to_stm_device(dev);
123 if (!try_module_get(stm->owner)) {
Alexander Shishkinf7c81c72016-02-15 19:12:07 +0200124 /* matches class_find_device() above */
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300125 put_device(dev);
126 return NULL;
127 }
128
129 return stm;
130}
131
132/**
133 * stm_put_device() - drop references on the stm device
134 * @stm: stm device, previously acquired by stm_find_device()
135 *
136 * This drops the module reference and device reference taken by
Alexander Shishkinf7c81c72016-02-15 19:12:07 +0200137 * stm_find_device() or stm_char_open().
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300138 */
139void stm_put_device(struct stm_device *stm)
140{
141 module_put(stm->owner);
142 put_device(&stm->dev);
143}
144
145/*
146 * Internally we only care about software-writable masters here, that is the
147 * ones in the range [stm_data->sw_start..stm_data..sw_end], however we need
148 * original master numbers to be visible externally, since they are the ones
149 * that will appear in the STP stream. Thus, the internal bookkeeping uses
150 * $master - stm_data->sw_start to reference master descriptors and such.
151 */
152
153#define __stm_master(_s, _m) \
154 ((_s)->masters[(_m) - (_s)->data->sw_start])
155
156static inline struct stp_master *
157stm_master(struct stm_device *stm, unsigned int idx)
158{
159 if (idx < stm->data->sw_start || idx > stm->data->sw_end)
160 return NULL;
161
162 return __stm_master(stm, idx);
163}
164
165static int stp_master_alloc(struct stm_device *stm, unsigned int idx)
166{
167 struct stp_master *master;
168 size_t size;
169
170 size = ALIGN(stm->data->sw_nchannels, 8) / 8;
171 size += sizeof(struct stp_master);
172 master = kzalloc(size, GFP_ATOMIC);
173 if (!master)
174 return -ENOMEM;
175
176 master->nr_free = stm->data->sw_nchannels;
177 __stm_master(stm, idx) = master;
178
179 return 0;
180}
181
182static void stp_master_free(struct stm_device *stm, unsigned int idx)
183{
184 struct stp_master *master = stm_master(stm, idx);
185
186 if (!master)
187 return;
188
189 __stm_master(stm, idx) = NULL;
190 kfree(master);
191}
192
193static void stm_output_claim(struct stm_device *stm, struct stm_output *output)
194{
195 struct stp_master *master = stm_master(stm, output->master);
196
Alexander Shishkincde4ad82016-02-15 19:12:06 +0200197 lockdep_assert_held(&stm->mc_lock);
198 lockdep_assert_held(&output->lock);
199
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300200 if (WARN_ON_ONCE(master->nr_free < output->nr_chans))
201 return;
202
203 bitmap_allocate_region(&master->chan_map[0], output->channel,
204 ilog2(output->nr_chans));
205
206 master->nr_free -= output->nr_chans;
207}
208
209static void
210stm_output_disclaim(struct stm_device *stm, struct stm_output *output)
211{
212 struct stp_master *master = stm_master(stm, output->master);
213
Alexander Shishkincde4ad82016-02-15 19:12:06 +0200214 lockdep_assert_held(&stm->mc_lock);
215 lockdep_assert_held(&output->lock);
216
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300217 bitmap_release_region(&master->chan_map[0], output->channel,
218 ilog2(output->nr_chans));
219
220 output->nr_chans = 0;
221 master->nr_free += output->nr_chans;
222}
223
224/*
225 * This is like bitmap_find_free_region(), except it can ignore @start bits
226 * at the beginning.
227 */
228static int find_free_channels(unsigned long *bitmap, unsigned int start,
229 unsigned int end, unsigned int width)
230{
231 unsigned int pos;
232 int i;
233
234 for (pos = start; pos < end + 1; pos = ALIGN(pos, width)) {
235 pos = find_next_zero_bit(bitmap, end + 1, pos);
236 if (pos + width > end + 1)
237 break;
238
239 if (pos & (width - 1))
240 continue;
241
242 for (i = 1; i < width && !test_bit(pos + i, bitmap); i++)
243 ;
244 if (i == width)
245 return pos;
246 }
247
248 return -1;
249}
250
Lucas Tanuref45f40a2016-02-15 19:11:51 +0200251static int
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300252stm_find_master_chan(struct stm_device *stm, unsigned int width,
253 unsigned int *mstart, unsigned int mend,
254 unsigned int *cstart, unsigned int cend)
255{
256 struct stp_master *master;
257 unsigned int midx;
258 int pos, err;
259
260 for (midx = *mstart; midx <= mend; midx++) {
261 if (!stm_master(stm, midx)) {
262 err = stp_master_alloc(stm, midx);
263 if (err)
264 return err;
265 }
266
267 master = stm_master(stm, midx);
268
269 if (!master->nr_free)
270 continue;
271
272 pos = find_free_channels(master->chan_map, *cstart, cend,
273 width);
274 if (pos < 0)
275 continue;
276
277 *mstart = midx;
278 *cstart = pos;
279 return 0;
280 }
281
282 return -ENOSPC;
283}
284
285static int stm_output_assign(struct stm_device *stm, unsigned int width,
286 struct stp_policy_node *policy_node,
287 struct stm_output *output)
288{
289 unsigned int midx, cidx, mend, cend;
290 int ret = -EINVAL;
291
292 if (width > stm->data->sw_nchannels)
293 return -EINVAL;
294
295 if (policy_node) {
296 stp_policy_node_get_ranges(policy_node,
297 &midx, &mend, &cidx, &cend);
298 } else {
299 midx = stm->data->sw_start;
300 cidx = 0;
301 mend = stm->data->sw_end;
302 cend = stm->data->sw_nchannels - 1;
303 }
304
305 spin_lock(&stm->mc_lock);
Alexander Shishkincde4ad82016-02-15 19:12:06 +0200306 spin_lock(&output->lock);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300307 /* output is already assigned -- shouldn't happen */
308 if (WARN_ON_ONCE(output->nr_chans))
309 goto unlock;
310
311 ret = stm_find_master_chan(stm, width, &midx, mend, &cidx, cend);
Lucas Tanuref45f40a2016-02-15 19:11:51 +0200312 if (ret < 0)
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300313 goto unlock;
314
315 output->master = midx;
316 output->channel = cidx;
317 output->nr_chans = width;
318 stm_output_claim(stm, output);
319 dev_dbg(&stm->dev, "assigned %u:%u (+%u)\n", midx, cidx, width);
320
321 ret = 0;
322unlock:
Alexander Shishkincde4ad82016-02-15 19:12:06 +0200323 spin_unlock(&output->lock);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300324 spin_unlock(&stm->mc_lock);
325
326 return ret;
327}
328
329static void stm_output_free(struct stm_device *stm, struct stm_output *output)
330{
331 spin_lock(&stm->mc_lock);
Alexander Shishkincde4ad82016-02-15 19:12:06 +0200332 spin_lock(&output->lock);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300333 if (output->nr_chans)
334 stm_output_disclaim(stm, output);
Alexander Shishkincde4ad82016-02-15 19:12:06 +0200335 spin_unlock(&output->lock);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300336 spin_unlock(&stm->mc_lock);
337}
338
Alexander Shishkincde4ad82016-02-15 19:12:06 +0200339static void stm_output_init(struct stm_output *output)
340{
341 spin_lock_init(&output->lock);
342}
343
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300344static int major_match(struct device *dev, const void *data)
345{
346 unsigned int major = *(unsigned int *)data;
347
348 return MAJOR(dev->devt) == major;
349}
350
351static int stm_char_open(struct inode *inode, struct file *file)
352{
353 struct stm_file *stmf;
354 struct device *dev;
355 unsigned int major = imajor(inode);
Johan Hovolda0ebf512016-11-18 14:17:31 +0200356 int err = -ENOMEM;
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300357
358 dev = class_find_device(&stm_class, NULL, &major, major_match);
359 if (!dev)
360 return -ENODEV;
361
362 stmf = kzalloc(sizeof(*stmf), GFP_KERNEL);
363 if (!stmf)
Johan Hovolda0ebf512016-11-18 14:17:31 +0200364 goto err_put_device;
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300365
Johan Hovolda0ebf512016-11-18 14:17:31 +0200366 err = -ENODEV;
Alexander Shishkincde4ad82016-02-15 19:12:06 +0200367 stm_output_init(&stmf->output);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300368 stmf->stm = to_stm_device(dev);
369
370 if (!try_module_get(stmf->stm->owner))
371 goto err_free;
372
373 file->private_data = stmf;
374
375 return nonseekable_open(inode, file);
376
377err_free:
Johan Hovolda0ebf512016-11-18 14:17:31 +0200378 kfree(stmf);
379err_put_device:
Alexander Shishkinf7c81c72016-02-15 19:12:07 +0200380 /* matches class_find_device() above */
381 put_device(dev);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300382
383 return err;
384}
385
386static int stm_char_release(struct inode *inode, struct file *file)
387{
388 struct stm_file *stmf = file->private_data;
Alexander Shishkincc842402016-02-15 19:12:09 +0200389 struct stm_device *stm = stmf->stm;
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300390
Alexander Shishkincc842402016-02-15 19:12:09 +0200391 if (stm->data->unlink)
392 stm->data->unlink(stm->data, stmf->output.master,
393 stmf->output.channel);
394
395 stm_output_free(stm, &stmf->output);
Alexander Shishkinf7c81c72016-02-15 19:12:07 +0200396
397 /*
398 * matches the stm_char_open()'s
399 * class_find_device() + try_module_get()
400 */
Alexander Shishkincc842402016-02-15 19:12:09 +0200401 stm_put_device(stm);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300402 kfree(stmf);
403
404 return 0;
405}
406
407static int stm_file_assign(struct stm_file *stmf, char *id, unsigned int width)
408{
409 struct stm_device *stm = stmf->stm;
410 int ret;
411
412 stmf->policy_node = stp_policy_node_lookup(stm, id);
413
414 ret = stm_output_assign(stm, width, stmf->policy_node, &stmf->output);
415
416 if (stmf->policy_node)
417 stp_policy_node_put(stmf->policy_node);
418
419 return ret;
420}
421
Chunyan Zhang9dfed802016-11-21 15:57:23 +0800422static ssize_t notrace stm_write(struct stm_data *data, unsigned int master,
Alexander Shishkinf8560a92016-02-15 19:12:01 +0200423 unsigned int channel, const char *buf, size_t count)
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300424{
425 unsigned int flags = STP_PACKET_TIMESTAMPED;
426 const unsigned char *p = buf, nil = 0;
427 size_t pos;
428 ssize_t sz;
429
430 for (pos = 0, p = buf; count > pos; pos += sz, p += sz) {
431 sz = min_t(unsigned int, count - pos, 8);
432 sz = data->packet(data, master, channel, STP_PACKET_DATA, flags,
433 sz, p);
434 flags = 0;
Alexander Shishkinf8560a92016-02-15 19:12:01 +0200435
436 if (sz < 0)
437 break;
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300438 }
439
440 data->packet(data, master, channel, STP_PACKET_FLAG, 0, 0, &nil);
Alexander Shishkinf8560a92016-02-15 19:12:01 +0200441
442 return pos;
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300443}
444
445static ssize_t stm_char_write(struct file *file, const char __user *buf,
446 size_t count, loff_t *ppos)
447{
448 struct stm_file *stmf = file->private_data;
449 struct stm_device *stm = stmf->stm;
450 char *kbuf;
451 int err;
452
Alexander Shishkinf08b1822015-12-22 17:25:21 +0200453 if (count + 1 > PAGE_SIZE)
454 count = PAGE_SIZE - 1;
455
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300456 /*
457 * if no m/c have been assigned to this writer up to this
458 * point, use "default" policy entry
459 */
460 if (!stmf->output.nr_chans) {
461 err = stm_file_assign(stmf, "default", 1);
462 /*
463 * EBUSY means that somebody else just assigned this
464 * output, which is just fine for write()
465 */
466 if (err && err != -EBUSY)
467 return err;
468 }
469
470 kbuf = kmalloc(count + 1, GFP_KERNEL);
471 if (!kbuf)
472 return -ENOMEM;
473
474 err = copy_from_user(kbuf, buf, count);
475 if (err) {
476 kfree(kbuf);
477 return -EFAULT;
478 }
479
Alexander Shishkin8e0469a2016-06-28 11:35:02 +0300480 pm_runtime_get_sync(&stm->dev);
481
Alexander Shishkinf8560a92016-02-15 19:12:01 +0200482 count = stm_write(stm->data, stmf->output.master, stmf->output.channel,
483 kbuf, count);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300484
Alexander Shishkin8e0469a2016-06-28 11:35:02 +0300485 pm_runtime_mark_last_busy(&stm->dev);
486 pm_runtime_put_autosuspend(&stm->dev);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300487 kfree(kbuf);
488
489 return count;
490}
491
Alexander Shishkin8e0469a2016-06-28 11:35:02 +0300492static void stm_mmap_open(struct vm_area_struct *vma)
493{
494 struct stm_file *stmf = vma->vm_file->private_data;
495 struct stm_device *stm = stmf->stm;
496
497 pm_runtime_get(&stm->dev);
498}
499
500static void stm_mmap_close(struct vm_area_struct *vma)
501{
502 struct stm_file *stmf = vma->vm_file->private_data;
503 struct stm_device *stm = stmf->stm;
504
505 pm_runtime_mark_last_busy(&stm->dev);
506 pm_runtime_put_autosuspend(&stm->dev);
507}
508
509static const struct vm_operations_struct stm_mmap_vmops = {
510 .open = stm_mmap_open,
511 .close = stm_mmap_close,
512};
513
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300514static int stm_char_mmap(struct file *file, struct vm_area_struct *vma)
515{
516 struct stm_file *stmf = file->private_data;
517 struct stm_device *stm = stmf->stm;
518 unsigned long size, phys;
519
520 if (!stm->data->mmio_addr)
521 return -EOPNOTSUPP;
522
523 if (vma->vm_pgoff)
524 return -EINVAL;
525
526 size = vma->vm_end - vma->vm_start;
527
528 if (stmf->output.nr_chans * stm->data->sw_mmiosz != size)
529 return -EINVAL;
530
531 phys = stm->data->mmio_addr(stm->data, stmf->output.master,
532 stmf->output.channel,
533 stmf->output.nr_chans);
534
535 if (!phys)
536 return -EINVAL;
537
Alexander Shishkin8e0469a2016-06-28 11:35:02 +0300538 pm_runtime_get_sync(&stm->dev);
539
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300540 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
541 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
Alexander Shishkin8e0469a2016-06-28 11:35:02 +0300542 vma->vm_ops = &stm_mmap_vmops;
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300543 vm_iomap_memory(vma, phys, size);
544
545 return 0;
546}
547
548static int stm_char_policy_set_ioctl(struct stm_file *stmf, void __user *arg)
549{
550 struct stm_device *stm = stmf->stm;
551 struct stp_policy_id *id;
552 int ret = -EINVAL;
553 u32 size;
554
555 if (stmf->output.nr_chans)
556 return -EBUSY;
557
558 if (copy_from_user(&size, arg, sizeof(size)))
559 return -EFAULT;
560
Dan Carpenter71c488f2017-08-10 15:45:10 +0300561 if (size < sizeof(*id) || size >= PATH_MAX + sizeof(*id))
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300562 return -EINVAL;
563
564 /*
565 * size + 1 to make sure the .id string at the bottom is terminated,
566 * which is also why memdup_user() is not useful here
567 */
568 id = kzalloc(size + 1, GFP_KERNEL);
569 if (!id)
570 return -ENOMEM;
571
572 if (copy_from_user(id, arg, size)) {
573 ret = -EFAULT;
574 goto err_free;
575 }
576
577 if (id->__reserved_0 || id->__reserved_1)
578 goto err_free;
579
580 if (id->width < 1 ||
581 id->width > PAGE_SIZE / stm->data->sw_mmiosz)
582 goto err_free;
583
584 ret = stm_file_assign(stmf, id->id, id->width);
585 if (ret)
586 goto err_free;
587
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300588 if (stm->data->link)
589 ret = stm->data->link(stm->data, stmf->output.master,
590 stmf->output.channel);
591
Alexander Shishkinf7c81c72016-02-15 19:12:07 +0200592 if (ret)
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300593 stm_output_free(stmf->stm, &stmf->output);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300594
595err_free:
596 kfree(id);
597
598 return ret;
599}
600
601static int stm_char_policy_get_ioctl(struct stm_file *stmf, void __user *arg)
602{
603 struct stp_policy_id id = {
604 .size = sizeof(id),
605 .master = stmf->output.master,
606 .channel = stmf->output.channel,
607 .width = stmf->output.nr_chans,
608 .__reserved_0 = 0,
609 .__reserved_1 = 0,
610 };
611
612 return copy_to_user(arg, &id, id.size) ? -EFAULT : 0;
613}
614
615static long
616stm_char_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
617{
618 struct stm_file *stmf = file->private_data;
619 struct stm_data *stm_data = stmf->stm->data;
620 int err = -ENOTTY;
621 u64 options;
622
623 switch (cmd) {
624 case STP_POLICY_ID_SET:
625 err = stm_char_policy_set_ioctl(stmf, (void __user *)arg);
626 if (err)
627 return err;
628
629 return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
630
631 case STP_POLICY_ID_GET:
632 return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
633
634 case STP_SET_OPTIONS:
635 if (copy_from_user(&options, (u64 __user *)arg, sizeof(u64)))
636 return -EFAULT;
637
638 if (stm_data->set_options)
639 err = stm_data->set_options(stm_data,
640 stmf->output.master,
641 stmf->output.channel,
642 stmf->output.nr_chans,
643 options);
644
645 break;
646 default:
647 break;
648 }
649
650 return err;
651}
652
653#ifdef CONFIG_COMPAT
654static long
655stm_char_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
656{
657 return stm_char_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
658}
659#else
660#define stm_char_compat_ioctl NULL
661#endif
662
663static const struct file_operations stm_fops = {
664 .open = stm_char_open,
665 .release = stm_char_release,
666 .write = stm_char_write,
667 .mmap = stm_char_mmap,
668 .unlocked_ioctl = stm_char_ioctl,
669 .compat_ioctl = stm_char_compat_ioctl,
670 .llseek = no_llseek,
671};
672
673static void stm_device_release(struct device *dev)
674{
675 struct stm_device *stm = to_stm_device(dev);
676
677 kfree(stm);
678}
679
680int stm_register_device(struct device *parent, struct stm_data *stm_data,
681 struct module *owner)
682{
683 struct stm_device *stm;
684 unsigned int nmasters;
685 int err = -ENOMEM;
686
687 if (!stm_core_up)
688 return -EPROBE_DEFER;
689
690 if (!stm_data->packet || !stm_data->sw_nchannels)
691 return -EINVAL;
692
Chunyan Zhang7b3bb0e2015-12-22 17:25:20 +0200693 nmasters = stm_data->sw_end - stm_data->sw_start + 1;
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300694 stm = kzalloc(sizeof(*stm) + nmasters * sizeof(void *), GFP_KERNEL);
695 if (!stm)
696 return -ENOMEM;
697
698 stm->major = register_chrdev(0, stm_data->name, &stm_fops);
699 if (stm->major < 0)
700 goto err_free;
701
702 device_initialize(&stm->dev);
703 stm->dev.devt = MKDEV(stm->major, 0);
704 stm->dev.class = &stm_class;
705 stm->dev.parent = parent;
706 stm->dev.release = stm_device_release;
707
Alexander Shishkin389b6692016-03-04 16:48:14 +0200708 mutex_init(&stm->link_mutex);
709 spin_lock_init(&stm->link_lock);
710 INIT_LIST_HEAD(&stm->link_list);
711
712 /* initialize the object before it is accessible via sysfs */
713 spin_lock_init(&stm->mc_lock);
714 mutex_init(&stm->policy_mutex);
715 stm->sw_nmasters = nmasters;
716 stm->owner = owner;
717 stm->data = stm_data;
718 stm_data->stm = stm;
719
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300720 err = kobject_set_name(&stm->dev.kobj, "%s", stm_data->name);
721 if (err)
722 goto err_device;
723
724 err = device_add(&stm->dev);
725 if (err)
726 goto err_device;
727
Alexander Shishkin8e0469a2016-06-28 11:35:02 +0300728 /*
729 * Use delayed autosuspend to avoid bouncing back and forth
730 * on recurring character device writes, with the initial
731 * delay time of 2 seconds.
732 */
733 pm_runtime_no_callbacks(&stm->dev);
734 pm_runtime_use_autosuspend(&stm->dev);
735 pm_runtime_set_autosuspend_delay(&stm->dev, 2000);
736 pm_runtime_set_suspended(&stm->dev);
737 pm_runtime_enable(&stm->dev);
738
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300739 return 0;
740
741err_device:
Alexander Shishkincbe4a612016-03-04 16:36:10 +0200742 unregister_chrdev(stm->major, stm_data->name);
743
Alexander Shishkinf7c81c72016-02-15 19:12:07 +0200744 /* matches device_initialize() above */
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300745 put_device(&stm->dev);
746err_free:
747 kfree(stm);
748
749 return err;
750}
751EXPORT_SYMBOL_GPL(stm_register_device);
752
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200753static int __stm_source_link_drop(struct stm_source_device *src,
754 struct stm_device *stm);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300755
756void stm_unregister_device(struct stm_data *stm_data)
757{
758 struct stm_device *stm = stm_data->stm;
759 struct stm_source_device *src, *iter;
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200760 int i, ret;
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300761
Alexander Shishkin8e0469a2016-06-28 11:35:02 +0300762 pm_runtime_dont_use_autosuspend(&stm->dev);
763 pm_runtime_disable(&stm->dev);
764
Alexander Shishkinc74f7e82015-12-22 17:25:19 +0200765 mutex_lock(&stm->link_mutex);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300766 list_for_each_entry_safe(src, iter, &stm->link_list, link_entry) {
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200767 ret = __stm_source_link_drop(src, stm);
768 /*
769 * src <-> stm link must not change under the same
770 * stm::link_mutex, so complain loudly if it has;
771 * also in this situation ret!=0 means this src is
772 * not connected to this stm and it should be otherwise
773 * safe to proceed with the tear-down of stm.
774 */
775 WARN_ON_ONCE(ret);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300776 }
Alexander Shishkinc74f7e82015-12-22 17:25:19 +0200777 mutex_unlock(&stm->link_mutex);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300778
779 synchronize_srcu(&stm_source_srcu);
780
781 unregister_chrdev(stm->major, stm_data->name);
782
783 mutex_lock(&stm->policy_mutex);
784 if (stm->policy)
785 stp_policy_unbind(stm->policy);
786 mutex_unlock(&stm->policy_mutex);
787
Chunyan Zhang73a3ed12016-02-15 19:11:52 +0200788 for (i = stm->data->sw_start; i <= stm->data->sw_end; i++)
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300789 stp_master_free(stm, i);
790
791 device_unregister(&stm->dev);
792 stm_data->stm = NULL;
793}
794EXPORT_SYMBOL_GPL(stm_unregister_device);
795
Alexander Shishkinc74f7e82015-12-22 17:25:19 +0200796/*
797 * stm::link_list access serialization uses a spinlock and a mutex; holding
798 * either of them guarantees that the list is stable; modification requires
799 * holding both of them.
800 *
801 * Lock ordering is as follows:
802 * stm::link_mutex
803 * stm::link_lock
804 * src::link_lock
805 */
806
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300807/**
808 * stm_source_link_add() - connect an stm_source device to an stm device
809 * @src: stm_source device
810 * @stm: stm device
811 *
812 * This function establishes a link from stm_source to an stm device so that
813 * the former can send out trace data to the latter.
814 *
815 * Return: 0 on success, -errno otherwise.
816 */
817static int stm_source_link_add(struct stm_source_device *src,
818 struct stm_device *stm)
819{
820 char *id;
821 int err;
822
Alexander Shishkinc74f7e82015-12-22 17:25:19 +0200823 mutex_lock(&stm->link_mutex);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300824 spin_lock(&stm->link_lock);
825 spin_lock(&src->link_lock);
826
827 /* src->link is dereferenced under stm_source_srcu but not the list */
828 rcu_assign_pointer(src->link, stm);
829 list_add_tail(&src->link_entry, &stm->link_list);
830
831 spin_unlock(&src->link_lock);
832 spin_unlock(&stm->link_lock);
Alexander Shishkinc74f7e82015-12-22 17:25:19 +0200833 mutex_unlock(&stm->link_mutex);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300834
835 id = kstrdup(src->data->name, GFP_KERNEL);
836 if (id) {
837 src->policy_node =
838 stp_policy_node_lookup(stm, id);
839
840 kfree(id);
841 }
842
843 err = stm_output_assign(stm, src->data->nr_chans,
844 src->policy_node, &src->output);
845
846 if (src->policy_node)
847 stp_policy_node_put(src->policy_node);
848
849 if (err)
850 goto fail_detach;
851
852 /* this is to notify the STM device that a new link has been made */
853 if (stm->data->link)
854 err = stm->data->link(stm->data, src->output.master,
855 src->output.channel);
856
857 if (err)
858 goto fail_free_output;
859
860 /* this is to let the source carry out all necessary preparations */
861 if (src->data->link)
862 src->data->link(src->data);
863
864 return 0;
865
866fail_free_output:
867 stm_output_free(stm, &src->output);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300868
869fail_detach:
Alexander Shishkinc74f7e82015-12-22 17:25:19 +0200870 mutex_lock(&stm->link_mutex);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300871 spin_lock(&stm->link_lock);
872 spin_lock(&src->link_lock);
873
874 rcu_assign_pointer(src->link, NULL);
875 list_del_init(&src->link_entry);
876
877 spin_unlock(&src->link_lock);
878 spin_unlock(&stm->link_lock);
Alexander Shishkinc74f7e82015-12-22 17:25:19 +0200879 mutex_unlock(&stm->link_mutex);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300880
881 return err;
882}
883
884/**
885 * __stm_source_link_drop() - detach stm_source from an stm device
886 * @src: stm_source device
887 * @stm: stm device
888 *
889 * If @stm is @src::link, disconnect them from one another and put the
890 * reference on the @stm device.
891 *
Alexander Shishkinc74f7e82015-12-22 17:25:19 +0200892 * Caller must hold stm::link_mutex.
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300893 */
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200894static int __stm_source_link_drop(struct stm_source_device *src,
895 struct stm_device *stm)
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300896{
Alexander Shishkin0df771d2015-10-06 12:47:17 +0300897 struct stm_device *link;
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200898 int ret = 0;
Alexander Shishkin0df771d2015-10-06 12:47:17 +0300899
Alexander Shishkinc74f7e82015-12-22 17:25:19 +0200900 lockdep_assert_held(&stm->link_mutex);
901
Alexander Shishkinc74f7e82015-12-22 17:25:19 +0200902 /* for stm::link_list modification, we hold both mutex and spinlock */
903 spin_lock(&stm->link_lock);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300904 spin_lock(&src->link_lock);
Alexander Shishkin0df771d2015-10-06 12:47:17 +0300905 link = srcu_dereference_check(src->link, &stm_source_srcu, 1);
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200906
907 /*
908 * The linked device may have changed since we last looked, because
909 * we weren't holding the src::link_lock back then; if this is the
910 * case, tell the caller to retry.
911 */
912 if (link != stm) {
913 ret = -EAGAIN;
Alexander Shishkin1810f2c2016-02-15 19:12:05 +0200914 goto unlock;
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200915 }
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300916
Alexander Shishkin0df771d2015-10-06 12:47:17 +0300917 stm_output_free(link, &src->output);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300918 list_del_init(&src->link_entry);
Alexander Shishkin8e0469a2016-06-28 11:35:02 +0300919 pm_runtime_mark_last_busy(&link->dev);
920 pm_runtime_put_autosuspend(&link->dev);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300921 /* matches stm_find_device() from stm_source_link_store() */
Alexander Shishkin0df771d2015-10-06 12:47:17 +0300922 stm_put_device(link);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300923 rcu_assign_pointer(src->link, NULL);
924
Alexander Shishkin1810f2c2016-02-15 19:12:05 +0200925unlock:
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300926 spin_unlock(&src->link_lock);
Alexander Shishkinc74f7e82015-12-22 17:25:19 +0200927 spin_unlock(&stm->link_lock);
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200928
Alexander Shishkincc842402016-02-15 19:12:09 +0200929 /*
930 * Call the unlink callbacks for both source and stm, when we know
931 * that we have actually performed the unlinking.
932 */
933 if (!ret) {
934 if (src->data->unlink)
935 src->data->unlink(src->data);
936
937 if (stm->data->unlink)
938 stm->data->unlink(stm->data, src->output.master,
939 src->output.channel);
940 }
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200941
942 return ret;
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300943}
944
945/**
946 * stm_source_link_drop() - detach stm_source from its stm device
947 * @src: stm_source device
948 *
949 * Unlinking means disconnecting from source's STM device; after this
950 * writes will be unsuccessful until it is linked to a new STM device.
951 *
952 * This will happen on "stm_source_link" sysfs attribute write to undo
953 * the existing link (if any), or on linked STM device's de-registration.
954 */
955static void stm_source_link_drop(struct stm_source_device *src)
956{
957 struct stm_device *stm;
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200958 int idx, ret;
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300959
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200960retry:
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300961 idx = srcu_read_lock(&stm_source_srcu);
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200962 /*
963 * The stm device will be valid for the duration of this
964 * read section, but the link may change before we grab
965 * the src::link_lock in __stm_source_link_drop().
966 */
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300967 stm = srcu_dereference(src->link, &stm_source_srcu);
968
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200969 ret = 0;
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300970 if (stm) {
Alexander Shishkinc74f7e82015-12-22 17:25:19 +0200971 mutex_lock(&stm->link_mutex);
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200972 ret = __stm_source_link_drop(src, stm);
Alexander Shishkinc74f7e82015-12-22 17:25:19 +0200973 mutex_unlock(&stm->link_mutex);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300974 }
975
976 srcu_read_unlock(&stm_source_srcu, idx);
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200977
978 /* if it did change, retry */
979 if (ret == -EAGAIN)
980 goto retry;
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300981}
982
983static ssize_t stm_source_link_show(struct device *dev,
984 struct device_attribute *attr,
985 char *buf)
986{
987 struct stm_source_device *src = to_stm_source_device(dev);
988 struct stm_device *stm;
989 int idx, ret;
990
991 idx = srcu_read_lock(&stm_source_srcu);
992 stm = srcu_dereference(src->link, &stm_source_srcu);
993 ret = sprintf(buf, "%s\n",
994 stm ? dev_name(&stm->dev) : "<none>");
995 srcu_read_unlock(&stm_source_srcu, idx);
996
997 return ret;
998}
999
1000static ssize_t stm_source_link_store(struct device *dev,
1001 struct device_attribute *attr,
1002 const char *buf, size_t count)
1003{
1004 struct stm_source_device *src = to_stm_source_device(dev);
1005 struct stm_device *link;
1006 int err;
1007
1008 stm_source_link_drop(src);
1009
1010 link = stm_find_device(buf);
1011 if (!link)
1012 return -EINVAL;
1013
Alexander Shishkin8e0469a2016-06-28 11:35:02 +03001014 pm_runtime_get(&link->dev);
1015
Alexander Shishkin7bd1d402015-09-22 15:47:10 +03001016 err = stm_source_link_add(src, link);
Alexander Shishkinf7c81c72016-02-15 19:12:07 +02001017 if (err) {
Alexander Shishkin8e0469a2016-06-28 11:35:02 +03001018 pm_runtime_put_autosuspend(&link->dev);
Alexander Shishkinf7c81c72016-02-15 19:12:07 +02001019 /* matches the stm_find_device() above */
Alexander Shishkin7bd1d402015-09-22 15:47:10 +03001020 stm_put_device(link);
Alexander Shishkinf7c81c72016-02-15 19:12:07 +02001021 }
Alexander Shishkin7bd1d402015-09-22 15:47:10 +03001022
1023 return err ? : count;
1024}
1025
1026static DEVICE_ATTR_RW(stm_source_link);
1027
1028static struct attribute *stm_source_attrs[] = {
1029 &dev_attr_stm_source_link.attr,
1030 NULL,
1031};
1032
1033ATTRIBUTE_GROUPS(stm_source);
1034
1035static struct class stm_source_class = {
1036 .name = "stm_source",
1037 .dev_groups = stm_source_groups,
1038};
1039
1040static void stm_source_device_release(struct device *dev)
1041{
1042 struct stm_source_device *src = to_stm_source_device(dev);
1043
1044 kfree(src);
1045}
1046
1047/**
1048 * stm_source_register_device() - register an stm_source device
1049 * @parent: parent device
1050 * @data: device description structure
1051 *
1052 * This will create a device of stm_source class that can write
1053 * data to an stm device once linked.
1054 *
1055 * Return: 0 on success, -errno otherwise.
1056 */
1057int stm_source_register_device(struct device *parent,
1058 struct stm_source_data *data)
1059{
1060 struct stm_source_device *src;
1061 int err;
1062
1063 if (!stm_core_up)
1064 return -EPROBE_DEFER;
1065
1066 src = kzalloc(sizeof(*src), GFP_KERNEL);
1067 if (!src)
1068 return -ENOMEM;
1069
1070 device_initialize(&src->dev);
1071 src->dev.class = &stm_source_class;
1072 src->dev.parent = parent;
1073 src->dev.release = stm_source_device_release;
1074
1075 err = kobject_set_name(&src->dev.kobj, "%s", data->name);
1076 if (err)
1077 goto err;
1078
Alexander Shishkin8e0469a2016-06-28 11:35:02 +03001079 pm_runtime_no_callbacks(&src->dev);
1080 pm_runtime_forbid(&src->dev);
1081
Alexander Shishkin7bd1d402015-09-22 15:47:10 +03001082 err = device_add(&src->dev);
1083 if (err)
1084 goto err;
1085
Alexander Shishkincde4ad82016-02-15 19:12:06 +02001086 stm_output_init(&src->output);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +03001087 spin_lock_init(&src->link_lock);
1088 INIT_LIST_HEAD(&src->link_entry);
1089 src->data = data;
1090 data->src = src;
1091
1092 return 0;
1093
1094err:
1095 put_device(&src->dev);
1096 kfree(src);
1097
1098 return err;
1099}
1100EXPORT_SYMBOL_GPL(stm_source_register_device);
1101
1102/**
1103 * stm_source_unregister_device() - unregister an stm_source device
1104 * @data: device description that was used to register the device
1105 *
1106 * This will remove a previously created stm_source device from the system.
1107 */
1108void stm_source_unregister_device(struct stm_source_data *data)
1109{
1110 struct stm_source_device *src = data->src;
1111
1112 stm_source_link_drop(src);
1113
Alexander Shishkinfd085bb2017-09-19 18:47:40 +03001114 device_unregister(&src->dev);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +03001115}
1116EXPORT_SYMBOL_GPL(stm_source_unregister_device);
1117
Chunyan Zhang9dfed802016-11-21 15:57:23 +08001118int notrace stm_source_write(struct stm_source_data *data,
1119 unsigned int chan,
1120 const char *buf, size_t count)
Alexander Shishkin7bd1d402015-09-22 15:47:10 +03001121{
1122 struct stm_source_device *src = data->src;
1123 struct stm_device *stm;
1124 int idx;
1125
1126 if (!src->output.nr_chans)
1127 return -ENODEV;
1128
1129 if (chan >= src->output.nr_chans)
1130 return -EINVAL;
1131
1132 idx = srcu_read_lock(&stm_source_srcu);
1133
1134 stm = srcu_dereference(src->link, &stm_source_srcu);
1135 if (stm)
Alexander Shishkinf8560a92016-02-15 19:12:01 +02001136 count = stm_write(stm->data, src->output.master,
1137 src->output.channel + chan,
1138 buf, count);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +03001139 else
1140 count = -ENODEV;
1141
1142 srcu_read_unlock(&stm_source_srcu, idx);
1143
1144 return count;
1145}
1146EXPORT_SYMBOL_GPL(stm_source_write);
1147
1148static int __init stm_core_init(void)
1149{
1150 int err;
1151
1152 err = class_register(&stm_class);
1153 if (err)
1154 return err;
1155
1156 err = class_register(&stm_source_class);
1157 if (err)
1158 goto err_stm;
1159
1160 err = stp_configfs_init();
1161 if (err)
1162 goto err_src;
1163
1164 init_srcu_struct(&stm_source_srcu);
1165
1166 stm_core_up++;
1167
1168 return 0;
1169
1170err_src:
1171 class_unregister(&stm_source_class);
1172err_stm:
1173 class_unregister(&stm_class);
1174
1175 return err;
1176}
1177
1178module_init(stm_core_init);
1179
1180static void __exit stm_core_exit(void)
1181{
1182 cleanup_srcu_struct(&stm_source_srcu);
1183 class_unregister(&stm_source_class);
1184 class_unregister(&stm_class);
1185 stp_configfs_exit();
1186}
1187
1188module_exit(stm_core_exit);
1189
1190MODULE_LICENSE("GPL v2");
1191MODULE_DESCRIPTION("System Trace Module device class");
1192MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");