blob: c38645106783ed739193476fe0cbe3babbb91ccd [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
Alexander Shishkin8e0469a2016-06-28 11:35:02 +030018#include <linux/pm_runtime.h>
Alexander Shishkin7bd1d402015-09-22 15:47:10 +030019#include <linux/uaccess.h>
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/device.h>
23#include <linux/compat.h>
24#include <linux/kdev_t.h>
25#include <linux/srcu.h>
26#include <linux/slab.h>
27#include <linux/stm.h>
28#include <linux/fs.h>
29#include <linux/mm.h>
Greg Kroah-Hartman6ba7b042018-05-26 08:49:24 +020030#include <linux/vmalloc.h>
Alexander Shishkin7bd1d402015-09-22 15:47:10 +030031#include "stm.h"
32
33#include <uapi/linux/stm.h>
34
35static unsigned int stm_core_up;
36
37/*
38 * The SRCU here makes sure that STM device doesn't disappear from under a
39 * stm_source_write() caller, which may want to have as little overhead as
40 * possible.
41 */
42static struct srcu_struct stm_source_srcu;
43
44static ssize_t masters_show(struct device *dev,
45 struct device_attribute *attr,
46 char *buf)
47{
48 struct stm_device *stm = to_stm_device(dev);
49 int ret;
50
51 ret = sprintf(buf, "%u %u\n", stm->data->sw_start, stm->data->sw_end);
52
53 return ret;
54}
55
56static DEVICE_ATTR_RO(masters);
57
58static ssize_t channels_show(struct device *dev,
59 struct device_attribute *attr,
60 char *buf)
61{
62 struct stm_device *stm = to_stm_device(dev);
63 int ret;
64
65 ret = sprintf(buf, "%u\n", stm->data->sw_nchannels);
66
67 return ret;
68}
69
70static DEVICE_ATTR_RO(channels);
71
Alexander Shishkin8e996a22016-05-03 11:33:37 -060072static ssize_t hw_override_show(struct device *dev,
73 struct device_attribute *attr,
74 char *buf)
75{
76 struct stm_device *stm = to_stm_device(dev);
77 int ret;
78
79 ret = sprintf(buf, "%u\n", stm->data->hw_override);
80
81 return ret;
82}
83
84static DEVICE_ATTR_RO(hw_override);
85
Alexander Shishkin7bd1d402015-09-22 15:47:10 +030086static struct attribute *stm_attrs[] = {
87 &dev_attr_masters.attr,
88 &dev_attr_channels.attr,
Alexander Shishkin8e996a22016-05-03 11:33:37 -060089 &dev_attr_hw_override.attr,
Alexander Shishkin7bd1d402015-09-22 15:47:10 +030090 NULL,
91};
92
93ATTRIBUTE_GROUPS(stm);
94
95static struct class stm_class = {
96 .name = "stm",
97 .dev_groups = stm_groups,
98};
99
100static int stm_dev_match(struct device *dev, const void *data)
101{
102 const char *name = data;
103
104 return sysfs_streq(name, dev_name(dev));
105}
106
107/**
108 * stm_find_device() - find stm device by name
109 * @buf: character buffer containing the name
110 *
111 * This is called when either policy gets assigned to an stm device or an
112 * stm_source device gets linked to an stm device.
113 *
114 * This grabs device's reference (get_device()) and module reference, both
115 * of which the calling path needs to make sure to drop with stm_put_device().
116 *
117 * Return: stm device pointer or null if lookup failed.
118 */
119struct stm_device *stm_find_device(const char *buf)
120{
121 struct stm_device *stm;
122 struct device *dev;
123
124 if (!stm_core_up)
125 return NULL;
126
127 dev = class_find_device(&stm_class, NULL, buf, stm_dev_match);
128 if (!dev)
129 return NULL;
130
131 stm = to_stm_device(dev);
132 if (!try_module_get(stm->owner)) {
Alexander Shishkinf7c81c72016-02-15 19:12:07 +0200133 /* matches class_find_device() above */
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300134 put_device(dev);
135 return NULL;
136 }
137
138 return stm;
139}
140
141/**
142 * stm_put_device() - drop references on the stm device
143 * @stm: stm device, previously acquired by stm_find_device()
144 *
145 * This drops the module reference and device reference taken by
Alexander Shishkinf7c81c72016-02-15 19:12:07 +0200146 * stm_find_device() or stm_char_open().
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300147 */
148void stm_put_device(struct stm_device *stm)
149{
150 module_put(stm->owner);
151 put_device(&stm->dev);
152}
153
154/*
155 * Internally we only care about software-writable masters here, that is the
156 * ones in the range [stm_data->sw_start..stm_data..sw_end], however we need
157 * original master numbers to be visible externally, since they are the ones
158 * that will appear in the STP stream. Thus, the internal bookkeeping uses
159 * $master - stm_data->sw_start to reference master descriptors and such.
160 */
161
162#define __stm_master(_s, _m) \
163 ((_s)->masters[(_m) - (_s)->data->sw_start])
164
165static inline struct stp_master *
166stm_master(struct stm_device *stm, unsigned int idx)
167{
168 if (idx < stm->data->sw_start || idx > stm->data->sw_end)
169 return NULL;
170
171 return __stm_master(stm, idx);
172}
173
174static int stp_master_alloc(struct stm_device *stm, unsigned int idx)
175{
176 struct stp_master *master;
177 size_t size;
178
179 size = ALIGN(stm->data->sw_nchannels, 8) / 8;
180 size += sizeof(struct stp_master);
181 master = kzalloc(size, GFP_ATOMIC);
182 if (!master)
183 return -ENOMEM;
184
185 master->nr_free = stm->data->sw_nchannels;
186 __stm_master(stm, idx) = master;
187
188 return 0;
189}
190
191static void stp_master_free(struct stm_device *stm, unsigned int idx)
192{
193 struct stp_master *master = stm_master(stm, idx);
194
195 if (!master)
196 return;
197
198 __stm_master(stm, idx) = NULL;
199 kfree(master);
200}
201
202static void stm_output_claim(struct stm_device *stm, struct stm_output *output)
203{
204 struct stp_master *master = stm_master(stm, output->master);
205
Alexander Shishkincde4ad82016-02-15 19:12:06 +0200206 lockdep_assert_held(&stm->mc_lock);
207 lockdep_assert_held(&output->lock);
208
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300209 if (WARN_ON_ONCE(master->nr_free < output->nr_chans))
210 return;
211
212 bitmap_allocate_region(&master->chan_map[0], output->channel,
213 ilog2(output->nr_chans));
214
215 master->nr_free -= output->nr_chans;
216}
217
218static void
219stm_output_disclaim(struct stm_device *stm, struct stm_output *output)
220{
221 struct stp_master *master = stm_master(stm, output->master);
222
Alexander Shishkincde4ad82016-02-15 19:12:06 +0200223 lockdep_assert_held(&stm->mc_lock);
224 lockdep_assert_held(&output->lock);
225
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300226 bitmap_release_region(&master->chan_map[0], output->channel,
227 ilog2(output->nr_chans));
228
229 output->nr_chans = 0;
230 master->nr_free += output->nr_chans;
231}
232
233/*
234 * This is like bitmap_find_free_region(), except it can ignore @start bits
235 * at the beginning.
236 */
237static int find_free_channels(unsigned long *bitmap, unsigned int start,
238 unsigned int end, unsigned int width)
239{
240 unsigned int pos;
241 int i;
242
243 for (pos = start; pos < end + 1; pos = ALIGN(pos, width)) {
244 pos = find_next_zero_bit(bitmap, end + 1, pos);
245 if (pos + width > end + 1)
246 break;
247
248 if (pos & (width - 1))
249 continue;
250
251 for (i = 1; i < width && !test_bit(pos + i, bitmap); i++)
252 ;
253 if (i == width)
254 return pos;
255 }
256
257 return -1;
258}
259
Lucas Tanuref45f40a2016-02-15 19:11:51 +0200260static int
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300261stm_find_master_chan(struct stm_device *stm, unsigned int width,
262 unsigned int *mstart, unsigned int mend,
263 unsigned int *cstart, unsigned int cend)
264{
265 struct stp_master *master;
266 unsigned int midx;
267 int pos, err;
268
269 for (midx = *mstart; midx <= mend; midx++) {
270 if (!stm_master(stm, midx)) {
271 err = stp_master_alloc(stm, midx);
272 if (err)
273 return err;
274 }
275
276 master = stm_master(stm, midx);
277
278 if (!master->nr_free)
279 continue;
280
281 pos = find_free_channels(master->chan_map, *cstart, cend,
282 width);
283 if (pos < 0)
284 continue;
285
286 *mstart = midx;
287 *cstart = pos;
288 return 0;
289 }
290
291 return -ENOSPC;
292}
293
294static int stm_output_assign(struct stm_device *stm, unsigned int width,
295 struct stp_policy_node *policy_node,
296 struct stm_output *output)
297{
298 unsigned int midx, cidx, mend, cend;
299 int ret = -EINVAL;
300
301 if (width > stm->data->sw_nchannels)
302 return -EINVAL;
303
304 if (policy_node) {
305 stp_policy_node_get_ranges(policy_node,
306 &midx, &mend, &cidx, &cend);
307 } else {
308 midx = stm->data->sw_start;
309 cidx = 0;
310 mend = stm->data->sw_end;
311 cend = stm->data->sw_nchannels - 1;
312 }
313
314 spin_lock(&stm->mc_lock);
Alexander Shishkincde4ad82016-02-15 19:12:06 +0200315 spin_lock(&output->lock);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300316 /* output is already assigned -- shouldn't happen */
317 if (WARN_ON_ONCE(output->nr_chans))
318 goto unlock;
319
320 ret = stm_find_master_chan(stm, width, &midx, mend, &cidx, cend);
Lucas Tanuref45f40a2016-02-15 19:11:51 +0200321 if (ret < 0)
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300322 goto unlock;
323
324 output->master = midx;
325 output->channel = cidx;
326 output->nr_chans = width;
327 stm_output_claim(stm, output);
328 dev_dbg(&stm->dev, "assigned %u:%u (+%u)\n", midx, cidx, width);
329
330 ret = 0;
331unlock:
Alexander Shishkincde4ad82016-02-15 19:12:06 +0200332 spin_unlock(&output->lock);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300333 spin_unlock(&stm->mc_lock);
334
335 return ret;
336}
337
338static void stm_output_free(struct stm_device *stm, struct stm_output *output)
339{
340 spin_lock(&stm->mc_lock);
Alexander Shishkincde4ad82016-02-15 19:12:06 +0200341 spin_lock(&output->lock);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300342 if (output->nr_chans)
343 stm_output_disclaim(stm, output);
Alexander Shishkincde4ad82016-02-15 19:12:06 +0200344 spin_unlock(&output->lock);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300345 spin_unlock(&stm->mc_lock);
346}
347
Alexander Shishkincde4ad82016-02-15 19:12:06 +0200348static void stm_output_init(struct stm_output *output)
349{
350 spin_lock_init(&output->lock);
351}
352
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300353static int major_match(struct device *dev, const void *data)
354{
355 unsigned int major = *(unsigned int *)data;
356
357 return MAJOR(dev->devt) == major;
358}
359
360static int stm_char_open(struct inode *inode, struct file *file)
361{
362 struct stm_file *stmf;
363 struct device *dev;
364 unsigned int major = imajor(inode);
Johan Hovold2d291432016-11-18 14:17:31 +0200365 int err = -ENOMEM;
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300366
367 dev = class_find_device(&stm_class, NULL, &major, major_match);
368 if (!dev)
369 return -ENODEV;
370
371 stmf = kzalloc(sizeof(*stmf), GFP_KERNEL);
372 if (!stmf)
Johan Hovold2d291432016-11-18 14:17:31 +0200373 goto err_put_device;
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300374
Johan Hovold2d291432016-11-18 14:17:31 +0200375 err = -ENODEV;
Alexander Shishkincde4ad82016-02-15 19:12:06 +0200376 stm_output_init(&stmf->output);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300377 stmf->stm = to_stm_device(dev);
378
379 if (!try_module_get(stmf->stm->owner))
380 goto err_free;
381
382 file->private_data = stmf;
383
384 return nonseekable_open(inode, file);
385
386err_free:
Johan Hovold2d291432016-11-18 14:17:31 +0200387 kfree(stmf);
388err_put_device:
Alexander Shishkinf7c81c72016-02-15 19:12:07 +0200389 /* matches class_find_device() above */
390 put_device(dev);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300391
392 return err;
393}
394
395static int stm_char_release(struct inode *inode, struct file *file)
396{
397 struct stm_file *stmf = file->private_data;
Alexander Shishkincc842402016-02-15 19:12:09 +0200398 struct stm_device *stm = stmf->stm;
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300399
Alexander Shishkincc842402016-02-15 19:12:09 +0200400 if (stm->data->unlink)
401 stm->data->unlink(stm->data, stmf->output.master,
402 stmf->output.channel);
403
404 stm_output_free(stm, &stmf->output);
Alexander Shishkinf7c81c72016-02-15 19:12:07 +0200405
406 /*
407 * matches the stm_char_open()'s
408 * class_find_device() + try_module_get()
409 */
Alexander Shishkincc842402016-02-15 19:12:09 +0200410 stm_put_device(stm);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300411 kfree(stmf);
412
413 return 0;
414}
415
416static int stm_file_assign(struct stm_file *stmf, char *id, unsigned int width)
417{
418 struct stm_device *stm = stmf->stm;
419 int ret;
420
421 stmf->policy_node = stp_policy_node_lookup(stm, id);
422
423 ret = stm_output_assign(stm, width, stmf->policy_node, &stmf->output);
424
425 if (stmf->policy_node)
426 stp_policy_node_put(stmf->policy_node);
427
428 return ret;
429}
430
Alexander Shishkinf8560a92016-02-15 19:12:01 +0200431static ssize_t stm_write(struct stm_data *data, unsigned int master,
432 unsigned int channel, const char *buf, size_t count)
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300433{
434 unsigned int flags = STP_PACKET_TIMESTAMPED;
435 const unsigned char *p = buf, nil = 0;
436 size_t pos;
437 ssize_t sz;
438
439 for (pos = 0, p = buf; count > pos; pos += sz, p += sz) {
440 sz = min_t(unsigned int, count - pos, 8);
441 sz = data->packet(data, master, channel, STP_PACKET_DATA, flags,
442 sz, p);
443 flags = 0;
Alexander Shishkinf8560a92016-02-15 19:12:01 +0200444
445 if (sz < 0)
446 break;
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300447 }
448
449 data->packet(data, master, channel, STP_PACKET_FLAG, 0, 0, &nil);
Alexander Shishkinf8560a92016-02-15 19:12:01 +0200450
451 return pos;
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300452}
453
454static ssize_t stm_char_write(struct file *file, const char __user *buf,
455 size_t count, loff_t *ppos)
456{
457 struct stm_file *stmf = file->private_data;
458 struct stm_device *stm = stmf->stm;
459 char *kbuf;
460 int err;
461
Alexander Shishkinf08b1822015-12-22 17:25:21 +0200462 if (count + 1 > PAGE_SIZE)
463 count = PAGE_SIZE - 1;
464
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300465 /*
466 * if no m/c have been assigned to this writer up to this
467 * point, use "default" policy entry
468 */
469 if (!stmf->output.nr_chans) {
470 err = stm_file_assign(stmf, "default", 1);
471 /*
472 * EBUSY means that somebody else just assigned this
473 * output, which is just fine for write()
474 */
475 if (err && err != -EBUSY)
476 return err;
477 }
478
479 kbuf = kmalloc(count + 1, GFP_KERNEL);
480 if (!kbuf)
481 return -ENOMEM;
482
483 err = copy_from_user(kbuf, buf, count);
484 if (err) {
485 kfree(kbuf);
486 return -EFAULT;
487 }
488
Alexander Shishkin8e0469a2016-06-28 11:35:02 +0300489 pm_runtime_get_sync(&stm->dev);
490
Alexander Shishkinf8560a92016-02-15 19:12:01 +0200491 count = stm_write(stm->data, stmf->output.master, stmf->output.channel,
492 kbuf, count);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300493
Alexander Shishkin8e0469a2016-06-28 11:35:02 +0300494 pm_runtime_mark_last_busy(&stm->dev);
495 pm_runtime_put_autosuspend(&stm->dev);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300496 kfree(kbuf);
497
498 return count;
499}
500
Alexander Shishkin8e0469a2016-06-28 11:35:02 +0300501static void stm_mmap_open(struct vm_area_struct *vma)
502{
503 struct stm_file *stmf = vma->vm_file->private_data;
504 struct stm_device *stm = stmf->stm;
505
506 pm_runtime_get(&stm->dev);
507}
508
509static void stm_mmap_close(struct vm_area_struct *vma)
510{
511 struct stm_file *stmf = vma->vm_file->private_data;
512 struct stm_device *stm = stmf->stm;
513
514 pm_runtime_mark_last_busy(&stm->dev);
515 pm_runtime_put_autosuspend(&stm->dev);
516}
517
518static const struct vm_operations_struct stm_mmap_vmops = {
519 .open = stm_mmap_open,
520 .close = stm_mmap_close,
521};
522
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300523static int stm_char_mmap(struct file *file, struct vm_area_struct *vma)
524{
525 struct stm_file *stmf = file->private_data;
526 struct stm_device *stm = stmf->stm;
527 unsigned long size, phys;
528
529 if (!stm->data->mmio_addr)
530 return -EOPNOTSUPP;
531
532 if (vma->vm_pgoff)
533 return -EINVAL;
534
535 size = vma->vm_end - vma->vm_start;
536
537 if (stmf->output.nr_chans * stm->data->sw_mmiosz != size)
538 return -EINVAL;
539
540 phys = stm->data->mmio_addr(stm->data, stmf->output.master,
541 stmf->output.channel,
542 stmf->output.nr_chans);
543
544 if (!phys)
545 return -EINVAL;
546
Alexander Shishkin8e0469a2016-06-28 11:35:02 +0300547 pm_runtime_get_sync(&stm->dev);
548
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300549 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
550 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
Alexander Shishkin8e0469a2016-06-28 11:35:02 +0300551 vma->vm_ops = &stm_mmap_vmops;
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300552 vm_iomap_memory(vma, phys, size);
553
554 return 0;
555}
556
557static int stm_char_policy_set_ioctl(struct stm_file *stmf, void __user *arg)
558{
559 struct stm_device *stm = stmf->stm;
560 struct stp_policy_id *id;
561 int ret = -EINVAL;
562 u32 size;
563
564 if (stmf->output.nr_chans)
565 return -EBUSY;
566
567 if (copy_from_user(&size, arg, sizeof(size)))
568 return -EFAULT;
569
570 if (size >= PATH_MAX + sizeof(*id))
571 return -EINVAL;
572
573 /*
574 * size + 1 to make sure the .id string at the bottom is terminated,
575 * which is also why memdup_user() is not useful here
576 */
577 id = kzalloc(size + 1, GFP_KERNEL);
578 if (!id)
579 return -ENOMEM;
580
581 if (copy_from_user(id, arg, size)) {
582 ret = -EFAULT;
583 goto err_free;
584 }
585
586 if (id->__reserved_0 || id->__reserved_1)
587 goto err_free;
588
589 if (id->width < 1 ||
590 id->width > PAGE_SIZE / stm->data->sw_mmiosz)
591 goto err_free;
592
593 ret = stm_file_assign(stmf, id->id, id->width);
594 if (ret)
595 goto err_free;
596
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300597 if (stm->data->link)
598 ret = stm->data->link(stm->data, stmf->output.master,
599 stmf->output.channel);
600
Alexander Shishkinf7c81c72016-02-15 19:12:07 +0200601 if (ret)
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300602 stm_output_free(stmf->stm, &stmf->output);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300603
604err_free:
605 kfree(id);
606
607 return ret;
608}
609
610static int stm_char_policy_get_ioctl(struct stm_file *stmf, void __user *arg)
611{
612 struct stp_policy_id id = {
613 .size = sizeof(id),
614 .master = stmf->output.master,
615 .channel = stmf->output.channel,
616 .width = stmf->output.nr_chans,
617 .__reserved_0 = 0,
618 .__reserved_1 = 0,
619 };
620
621 return copy_to_user(arg, &id, id.size) ? -EFAULT : 0;
622}
623
624static long
625stm_char_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
626{
627 struct stm_file *stmf = file->private_data;
628 struct stm_data *stm_data = stmf->stm->data;
629 int err = -ENOTTY;
630 u64 options;
631
632 switch (cmd) {
633 case STP_POLICY_ID_SET:
634 err = stm_char_policy_set_ioctl(stmf, (void __user *)arg);
635 if (err)
636 return err;
637
638 return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
639
640 case STP_POLICY_ID_GET:
641 return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
642
643 case STP_SET_OPTIONS:
644 if (copy_from_user(&options, (u64 __user *)arg, sizeof(u64)))
645 return -EFAULT;
646
647 if (stm_data->set_options)
648 err = stm_data->set_options(stm_data,
649 stmf->output.master,
650 stmf->output.channel,
651 stmf->output.nr_chans,
652 options);
653
654 break;
655 default:
656 break;
657 }
658
659 return err;
660}
661
662#ifdef CONFIG_COMPAT
663static long
664stm_char_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
665{
666 return stm_char_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
667}
668#else
669#define stm_char_compat_ioctl NULL
670#endif
671
672static const struct file_operations stm_fops = {
673 .open = stm_char_open,
674 .release = stm_char_release,
675 .write = stm_char_write,
676 .mmap = stm_char_mmap,
677 .unlocked_ioctl = stm_char_ioctl,
678 .compat_ioctl = stm_char_compat_ioctl,
679 .llseek = no_llseek,
680};
681
682static void stm_device_release(struct device *dev)
683{
684 struct stm_device *stm = to_stm_device(dev);
685
Alexander Shishkin99434702018-05-24 11:27:26 +0300686 vfree(stm);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300687}
688
689int stm_register_device(struct device *parent, struct stm_data *stm_data,
690 struct module *owner)
691{
692 struct stm_device *stm;
693 unsigned int nmasters;
694 int err = -ENOMEM;
695
696 if (!stm_core_up)
697 return -EPROBE_DEFER;
698
699 if (!stm_data->packet || !stm_data->sw_nchannels)
700 return -EINVAL;
701
Chunyan Zhang7b3bb0e2015-12-22 17:25:20 +0200702 nmasters = stm_data->sw_end - stm_data->sw_start + 1;
Alexander Shishkin99434702018-05-24 11:27:26 +0300703 stm = vzalloc(sizeof(*stm) + nmasters * sizeof(void *));
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300704 if (!stm)
705 return -ENOMEM;
706
707 stm->major = register_chrdev(0, stm_data->name, &stm_fops);
708 if (stm->major < 0)
709 goto err_free;
710
711 device_initialize(&stm->dev);
712 stm->dev.devt = MKDEV(stm->major, 0);
713 stm->dev.class = &stm_class;
714 stm->dev.parent = parent;
715 stm->dev.release = stm_device_release;
716
Alexander Shishkin389b6692016-03-04 16:48:14 +0200717 mutex_init(&stm->link_mutex);
718 spin_lock_init(&stm->link_lock);
719 INIT_LIST_HEAD(&stm->link_list);
720
721 /* initialize the object before it is accessible via sysfs */
722 spin_lock_init(&stm->mc_lock);
723 mutex_init(&stm->policy_mutex);
724 stm->sw_nmasters = nmasters;
725 stm->owner = owner;
726 stm->data = stm_data;
727 stm_data->stm = stm;
728
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300729 err = kobject_set_name(&stm->dev.kobj, "%s", stm_data->name);
730 if (err)
731 goto err_device;
732
733 err = device_add(&stm->dev);
734 if (err)
735 goto err_device;
736
Alexander Shishkin8e0469a2016-06-28 11:35:02 +0300737 /*
738 * Use delayed autosuspend to avoid bouncing back and forth
739 * on recurring character device writes, with the initial
740 * delay time of 2 seconds.
741 */
742 pm_runtime_no_callbacks(&stm->dev);
743 pm_runtime_use_autosuspend(&stm->dev);
744 pm_runtime_set_autosuspend_delay(&stm->dev, 2000);
745 pm_runtime_set_suspended(&stm->dev);
746 pm_runtime_enable(&stm->dev);
747
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300748 return 0;
749
750err_device:
Alexander Shishkincbe4a612016-03-04 16:36:10 +0200751 unregister_chrdev(stm->major, stm_data->name);
752
Alexander Shishkinf7c81c72016-02-15 19:12:07 +0200753 /* matches device_initialize() above */
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300754 put_device(&stm->dev);
755err_free:
Alexander Shishkin99434702018-05-24 11:27:26 +0300756 vfree(stm);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300757
758 return err;
759}
760EXPORT_SYMBOL_GPL(stm_register_device);
761
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200762static int __stm_source_link_drop(struct stm_source_device *src,
763 struct stm_device *stm);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300764
765void stm_unregister_device(struct stm_data *stm_data)
766{
767 struct stm_device *stm = stm_data->stm;
768 struct stm_source_device *src, *iter;
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200769 int i, ret;
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300770
Alexander Shishkin8e0469a2016-06-28 11:35:02 +0300771 pm_runtime_dont_use_autosuspend(&stm->dev);
772 pm_runtime_disable(&stm->dev);
773
Alexander Shishkinc74f7e82015-12-22 17:25:19 +0200774 mutex_lock(&stm->link_mutex);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300775 list_for_each_entry_safe(src, iter, &stm->link_list, link_entry) {
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200776 ret = __stm_source_link_drop(src, stm);
777 /*
778 * src <-> stm link must not change under the same
779 * stm::link_mutex, so complain loudly if it has;
780 * also in this situation ret!=0 means this src is
781 * not connected to this stm and it should be otherwise
782 * safe to proceed with the tear-down of stm.
783 */
784 WARN_ON_ONCE(ret);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300785 }
Alexander Shishkinc74f7e82015-12-22 17:25:19 +0200786 mutex_unlock(&stm->link_mutex);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300787
788 synchronize_srcu(&stm_source_srcu);
789
790 unregister_chrdev(stm->major, stm_data->name);
791
792 mutex_lock(&stm->policy_mutex);
793 if (stm->policy)
794 stp_policy_unbind(stm->policy);
795 mutex_unlock(&stm->policy_mutex);
796
Chunyan Zhang73a3ed12016-02-15 19:11:52 +0200797 for (i = stm->data->sw_start; i <= stm->data->sw_end; i++)
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300798 stp_master_free(stm, i);
799
800 device_unregister(&stm->dev);
801 stm_data->stm = NULL;
802}
803EXPORT_SYMBOL_GPL(stm_unregister_device);
804
Alexander Shishkinc74f7e82015-12-22 17:25:19 +0200805/*
806 * stm::link_list access serialization uses a spinlock and a mutex; holding
807 * either of them guarantees that the list is stable; modification requires
808 * holding both of them.
809 *
810 * Lock ordering is as follows:
811 * stm::link_mutex
812 * stm::link_lock
813 * src::link_lock
814 */
815
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300816/**
817 * stm_source_link_add() - connect an stm_source device to an stm device
818 * @src: stm_source device
819 * @stm: stm device
820 *
821 * This function establishes a link from stm_source to an stm device so that
822 * the former can send out trace data to the latter.
823 *
824 * Return: 0 on success, -errno otherwise.
825 */
826static int stm_source_link_add(struct stm_source_device *src,
827 struct stm_device *stm)
828{
829 char *id;
830 int err;
831
Alexander Shishkinc74f7e82015-12-22 17:25:19 +0200832 mutex_lock(&stm->link_mutex);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300833 spin_lock(&stm->link_lock);
834 spin_lock(&src->link_lock);
835
836 /* src->link is dereferenced under stm_source_srcu but not the list */
837 rcu_assign_pointer(src->link, stm);
838 list_add_tail(&src->link_entry, &stm->link_list);
839
840 spin_unlock(&src->link_lock);
841 spin_unlock(&stm->link_lock);
Alexander Shishkinc74f7e82015-12-22 17:25:19 +0200842 mutex_unlock(&stm->link_mutex);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300843
844 id = kstrdup(src->data->name, GFP_KERNEL);
845 if (id) {
846 src->policy_node =
847 stp_policy_node_lookup(stm, id);
848
849 kfree(id);
850 }
851
852 err = stm_output_assign(stm, src->data->nr_chans,
853 src->policy_node, &src->output);
854
855 if (src->policy_node)
856 stp_policy_node_put(src->policy_node);
857
858 if (err)
859 goto fail_detach;
860
861 /* this is to notify the STM device that a new link has been made */
862 if (stm->data->link)
863 err = stm->data->link(stm->data, src->output.master,
864 src->output.channel);
865
866 if (err)
867 goto fail_free_output;
868
869 /* this is to let the source carry out all necessary preparations */
870 if (src->data->link)
871 src->data->link(src->data);
872
873 return 0;
874
875fail_free_output:
876 stm_output_free(stm, &src->output);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300877
878fail_detach:
Alexander Shishkinc74f7e82015-12-22 17:25:19 +0200879 mutex_lock(&stm->link_mutex);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300880 spin_lock(&stm->link_lock);
881 spin_lock(&src->link_lock);
882
883 rcu_assign_pointer(src->link, NULL);
884 list_del_init(&src->link_entry);
885
886 spin_unlock(&src->link_lock);
887 spin_unlock(&stm->link_lock);
Alexander Shishkinc74f7e82015-12-22 17:25:19 +0200888 mutex_unlock(&stm->link_mutex);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300889
890 return err;
891}
892
893/**
894 * __stm_source_link_drop() - detach stm_source from an stm device
895 * @src: stm_source device
896 * @stm: stm device
897 *
898 * If @stm is @src::link, disconnect them from one another and put the
899 * reference on the @stm device.
900 *
Alexander Shishkinc74f7e82015-12-22 17:25:19 +0200901 * Caller must hold stm::link_mutex.
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300902 */
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200903static int __stm_source_link_drop(struct stm_source_device *src,
904 struct stm_device *stm)
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300905{
Alexander Shishkin0df771d2015-10-06 12:47:17 +0300906 struct stm_device *link;
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200907 int ret = 0;
Alexander Shishkin0df771d2015-10-06 12:47:17 +0300908
Alexander Shishkinc74f7e82015-12-22 17:25:19 +0200909 lockdep_assert_held(&stm->link_mutex);
910
Alexander Shishkinc74f7e82015-12-22 17:25:19 +0200911 /* for stm::link_list modification, we hold both mutex and spinlock */
912 spin_lock(&stm->link_lock);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300913 spin_lock(&src->link_lock);
Alexander Shishkin0df771d2015-10-06 12:47:17 +0300914 link = srcu_dereference_check(src->link, &stm_source_srcu, 1);
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200915
916 /*
917 * The linked device may have changed since we last looked, because
918 * we weren't holding the src::link_lock back then; if this is the
919 * case, tell the caller to retry.
920 */
921 if (link != stm) {
922 ret = -EAGAIN;
Alexander Shishkin1810f2c2016-02-15 19:12:05 +0200923 goto unlock;
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200924 }
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300925
Alexander Shishkin0df771d2015-10-06 12:47:17 +0300926 stm_output_free(link, &src->output);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300927 list_del_init(&src->link_entry);
Alexander Shishkin8e0469a2016-06-28 11:35:02 +0300928 pm_runtime_mark_last_busy(&link->dev);
929 pm_runtime_put_autosuspend(&link->dev);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300930 /* matches stm_find_device() from stm_source_link_store() */
Alexander Shishkin0df771d2015-10-06 12:47:17 +0300931 stm_put_device(link);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300932 rcu_assign_pointer(src->link, NULL);
933
Alexander Shishkin1810f2c2016-02-15 19:12:05 +0200934unlock:
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300935 spin_unlock(&src->link_lock);
Alexander Shishkinc74f7e82015-12-22 17:25:19 +0200936 spin_unlock(&stm->link_lock);
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200937
Alexander Shishkincc842402016-02-15 19:12:09 +0200938 /*
939 * Call the unlink callbacks for both source and stm, when we know
940 * that we have actually performed the unlinking.
941 */
942 if (!ret) {
943 if (src->data->unlink)
944 src->data->unlink(src->data);
945
946 if (stm->data->unlink)
947 stm->data->unlink(stm->data, src->output.master,
948 src->output.channel);
949 }
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200950
951 return ret;
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300952}
953
954/**
955 * stm_source_link_drop() - detach stm_source from its stm device
956 * @src: stm_source device
957 *
958 * Unlinking means disconnecting from source's STM device; after this
959 * writes will be unsuccessful until it is linked to a new STM device.
960 *
961 * This will happen on "stm_source_link" sysfs attribute write to undo
962 * the existing link (if any), or on linked STM device's de-registration.
963 */
964static void stm_source_link_drop(struct stm_source_device *src)
965{
966 struct stm_device *stm;
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200967 int idx, ret;
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300968
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200969retry:
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300970 idx = srcu_read_lock(&stm_source_srcu);
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200971 /*
972 * The stm device will be valid for the duration of this
973 * read section, but the link may change before we grab
974 * the src::link_lock in __stm_source_link_drop().
975 */
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300976 stm = srcu_dereference(src->link, &stm_source_srcu);
977
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200978 ret = 0;
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300979 if (stm) {
Alexander Shishkinc74f7e82015-12-22 17:25:19 +0200980 mutex_lock(&stm->link_mutex);
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200981 ret = __stm_source_link_drop(src, stm);
Alexander Shishkinc74f7e82015-12-22 17:25:19 +0200982 mutex_unlock(&stm->link_mutex);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300983 }
984
985 srcu_read_unlock(&stm_source_srcu, idx);
Alexander Shishkinb4ca34a2016-02-15 19:12:08 +0200986
987 /* if it did change, retry */
988 if (ret == -EAGAIN)
989 goto retry;
Alexander Shishkin7bd1d402015-09-22 15:47:10 +0300990}
991
992static ssize_t stm_source_link_show(struct device *dev,
993 struct device_attribute *attr,
994 char *buf)
995{
996 struct stm_source_device *src = to_stm_source_device(dev);
997 struct stm_device *stm;
998 int idx, ret;
999
1000 idx = srcu_read_lock(&stm_source_srcu);
1001 stm = srcu_dereference(src->link, &stm_source_srcu);
1002 ret = sprintf(buf, "%s\n",
1003 stm ? dev_name(&stm->dev) : "<none>");
1004 srcu_read_unlock(&stm_source_srcu, idx);
1005
1006 return ret;
1007}
1008
1009static ssize_t stm_source_link_store(struct device *dev,
1010 struct device_attribute *attr,
1011 const char *buf, size_t count)
1012{
1013 struct stm_source_device *src = to_stm_source_device(dev);
1014 struct stm_device *link;
1015 int err;
1016
1017 stm_source_link_drop(src);
1018
1019 link = stm_find_device(buf);
1020 if (!link)
1021 return -EINVAL;
1022
Alexander Shishkin8e0469a2016-06-28 11:35:02 +03001023 pm_runtime_get(&link->dev);
1024
Alexander Shishkin7bd1d402015-09-22 15:47:10 +03001025 err = stm_source_link_add(src, link);
Alexander Shishkinf7c81c72016-02-15 19:12:07 +02001026 if (err) {
Alexander Shishkin8e0469a2016-06-28 11:35:02 +03001027 pm_runtime_put_autosuspend(&link->dev);
Alexander Shishkinf7c81c72016-02-15 19:12:07 +02001028 /* matches the stm_find_device() above */
Alexander Shishkin7bd1d402015-09-22 15:47:10 +03001029 stm_put_device(link);
Alexander Shishkinf7c81c72016-02-15 19:12:07 +02001030 }
Alexander Shishkin7bd1d402015-09-22 15:47:10 +03001031
1032 return err ? : count;
1033}
1034
1035static DEVICE_ATTR_RW(stm_source_link);
1036
1037static struct attribute *stm_source_attrs[] = {
1038 &dev_attr_stm_source_link.attr,
1039 NULL,
1040};
1041
1042ATTRIBUTE_GROUPS(stm_source);
1043
1044static struct class stm_source_class = {
1045 .name = "stm_source",
1046 .dev_groups = stm_source_groups,
1047};
1048
1049static void stm_source_device_release(struct device *dev)
1050{
1051 struct stm_source_device *src = to_stm_source_device(dev);
1052
1053 kfree(src);
1054}
1055
1056/**
1057 * stm_source_register_device() - register an stm_source device
1058 * @parent: parent device
1059 * @data: device description structure
1060 *
1061 * This will create a device of stm_source class that can write
1062 * data to an stm device once linked.
1063 *
1064 * Return: 0 on success, -errno otherwise.
1065 */
1066int stm_source_register_device(struct device *parent,
1067 struct stm_source_data *data)
1068{
1069 struct stm_source_device *src;
1070 int err;
1071
1072 if (!stm_core_up)
1073 return -EPROBE_DEFER;
1074
1075 src = kzalloc(sizeof(*src), GFP_KERNEL);
1076 if (!src)
1077 return -ENOMEM;
1078
1079 device_initialize(&src->dev);
1080 src->dev.class = &stm_source_class;
1081 src->dev.parent = parent;
1082 src->dev.release = stm_source_device_release;
1083
1084 err = kobject_set_name(&src->dev.kobj, "%s", data->name);
1085 if (err)
1086 goto err;
1087
Alexander Shishkin8e0469a2016-06-28 11:35:02 +03001088 pm_runtime_no_callbacks(&src->dev);
1089 pm_runtime_forbid(&src->dev);
1090
Alexander Shishkin7bd1d402015-09-22 15:47:10 +03001091 err = device_add(&src->dev);
1092 if (err)
1093 goto err;
1094
Alexander Shishkincde4ad82016-02-15 19:12:06 +02001095 stm_output_init(&src->output);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +03001096 spin_lock_init(&src->link_lock);
1097 INIT_LIST_HEAD(&src->link_entry);
1098 src->data = data;
1099 data->src = src;
1100
1101 return 0;
1102
1103err:
1104 put_device(&src->dev);
1105 kfree(src);
1106
1107 return err;
1108}
1109EXPORT_SYMBOL_GPL(stm_source_register_device);
1110
1111/**
1112 * stm_source_unregister_device() - unregister an stm_source device
1113 * @data: device description that was used to register the device
1114 *
1115 * This will remove a previously created stm_source device from the system.
1116 */
1117void stm_source_unregister_device(struct stm_source_data *data)
1118{
1119 struct stm_source_device *src = data->src;
1120
1121 stm_source_link_drop(src);
1122
Alexander Shishkin3ff8bc82017-09-19 18:47:40 +03001123 device_unregister(&src->dev);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +03001124}
1125EXPORT_SYMBOL_GPL(stm_source_unregister_device);
1126
1127int stm_source_write(struct stm_source_data *data, unsigned int chan,
1128 const char *buf, size_t count)
1129{
1130 struct stm_source_device *src = data->src;
1131 struct stm_device *stm;
1132 int idx;
1133
1134 if (!src->output.nr_chans)
1135 return -ENODEV;
1136
1137 if (chan >= src->output.nr_chans)
1138 return -EINVAL;
1139
1140 idx = srcu_read_lock(&stm_source_srcu);
1141
1142 stm = srcu_dereference(src->link, &stm_source_srcu);
1143 if (stm)
Alexander Shishkinf8560a92016-02-15 19:12:01 +02001144 count = stm_write(stm->data, src->output.master,
1145 src->output.channel + chan,
1146 buf, count);
Alexander Shishkin7bd1d402015-09-22 15:47:10 +03001147 else
1148 count = -ENODEV;
1149
1150 srcu_read_unlock(&stm_source_srcu, idx);
1151
1152 return count;
1153}
1154EXPORT_SYMBOL_GPL(stm_source_write);
1155
1156static int __init stm_core_init(void)
1157{
1158 int err;
1159
1160 err = class_register(&stm_class);
1161 if (err)
1162 return err;
1163
1164 err = class_register(&stm_source_class);
1165 if (err)
1166 goto err_stm;
1167
1168 err = stp_configfs_init();
1169 if (err)
1170 goto err_src;
1171
1172 init_srcu_struct(&stm_source_srcu);
1173
1174 stm_core_up++;
1175
1176 return 0;
1177
1178err_src:
1179 class_unregister(&stm_source_class);
1180err_stm:
1181 class_unregister(&stm_class);
1182
1183 return err;
1184}
1185
1186module_init(stm_core_init);
1187
1188static void __exit stm_core_exit(void)
1189{
1190 cleanup_srcu_struct(&stm_source_srcu);
1191 class_unregister(&stm_source_class);
1192 class_unregister(&stm_class);
1193 stp_configfs_exit();
1194}
1195
1196module_exit(stm_core_exit);
1197
1198MODULE_LICENSE("GPL v2");
1199MODULE_DESCRIPTION("System Trace Module device class");
1200MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");