1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Intel(R) Trace Hub Memory Storage Unit
4 *
5 * Copyright (C) 2014-2015 Intel Corporation.
6 */
7
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10 #include <linux/types.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/uaccess.h>
14 #include <linux/sizes.h>
15 #include <linux/printk.h>
16 #include <linux/slab.h>
17 #include <linux/mm.h>
18 #include <linux/fs.h>
19 #include <linux/io.h>
20 #include <linux/workqueue.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/pfn_t.h>
23
24 #ifdef CONFIG_X86
25 #include <asm/set_memory.h>
26 #endif
27
28 #include <linux/intel_th.h>
29 #include "intel_th.h"
30 #include "msu.h"
31
32 #define msc_dev(x) (&(x)->thdev->dev)
33
34 /*
35 * Lockout state transitions:
36 * READY -> INUSE -+-> LOCKED -+-> READY -> etc.
37 * \-----------/
38 * WIN_READY: window can be used by HW
39 * WIN_INUSE: window is in use
40 * WIN_LOCKED: window is filled up and is being processed by the buffer
41 * handling code
42 *
43 * All state transitions happen automatically, except for the LOCKED->READY,
44 * which needs to be signalled by the buffer code by calling
45 * intel_th_msc_window_unlock().
46 *
47 * When the interrupt handler has to switch to the next window, it checks
48 * whether it's READY, and if it is, it performs the switch and tracing
49 * continues. If it's LOCKED, it stops the trace.
50 */
51 enum lockout_state {
52 WIN_READY = 0,
53 WIN_INUSE,
54 WIN_LOCKED
55 };
56
57 /**
58 * struct msc_window - multiblock mode window descriptor
59 * @entry: window list linkage (msc::win_list)
60 * @pgoff: page offset into the buffer that this window starts at
61 * @lockout: lockout state, see comment below
62 * @lo_lock: lockout state serialization
63 * @nr_blocks: number of blocks (pages) in this window
64 * @nr_segs: number of segments in this window (<= @nr_blocks)
65 * @_sgt: array of block descriptors
66 * @sgt: array of block descriptors
67 */
68 struct msc_window {
69 struct list_head entry;
70 unsigned long pgoff;
71 enum lockout_state lockout;
72 spinlock_t lo_lock;
73 unsigned int nr_blocks;
74 unsigned int nr_segs;
75 struct msc *msc;
76 struct sg_table _sgt;
77 struct sg_table *sgt;
78 };
79
80 /**
81 * struct msc_iter - iterator for msc buffer
82 * @entry: msc::iter_list linkage
83 * @msc: pointer to the MSC device
84 * @start_win: oldest window
85 * @win: current window
86 * @offset: current logical offset into the buffer
87 * @start_block: oldest block in the window
88 * @block: block number in the window
89 * @block_off: offset into current block
90 * @wrap_count: block wrapping handling
91 * @eof: end of buffer reached
92 */
93 struct msc_iter {
94 struct list_head entry;
95 struct msc *msc;
96 struct msc_window *start_win;
97 struct msc_window *win;
98 unsigned long offset;
99 struct scatterlist *start_block;
100 struct scatterlist *block;
101 unsigned int block_off;
102 unsigned int wrap_count;
103 unsigned int eof;
104 };
105
106 /**
107 * struct msc - MSC device representation
108 * @reg_base: register window base address
109 * @thdev: intel_th_device pointer
110 * @mbuf: MSU buffer, if assigned
111 * @mbuf_priv MSU buffer's private data, if @mbuf
112 * @win_list: list of windows in multiblock mode
113 * @single_sgt: single mode buffer
114 * @cur_win: current window
115 * @nr_pages: total number of pages allocated for this buffer
116 * @single_sz: amount of data in single mode
117 * @single_wrap: single mode wrap occurred
118 * @base: buffer's base pointer
119 * @base_addr: buffer's base address
120 * @user_count: number of users of the buffer
121 * @mmap_count: number of mappings
122 * @buf_mutex: mutex to serialize access to buffer-related bits
123
124 * @enabled: MSC is enabled
125 * @wrap: wrapping is enabled
126 * @mode: MSC operating mode
127 * @burst_len: write burst length
128 * @index: number of this MSC in the MSU
129 */
130 struct msc {
131 void __iomem *reg_base;
132 void __iomem *msu_base;
133 struct intel_th_device *thdev;
134
135 const struct msu_buffer *mbuf;
136 void *mbuf_priv;
137
138 struct work_struct work;
139 struct list_head win_list;
140 struct sg_table single_sgt;
141 struct msc_window *cur_win;
142 struct msc_window *switch_on_unlock;
143 unsigned long nr_pages;
144 unsigned long single_sz;
145 unsigned int single_wrap : 1;
146 void *base;
147 dma_addr_t base_addr;
148 u32 orig_addr;
149 u32 orig_sz;
150
151 /* <0: no buffer, 0: no users, >0: active users */
152 atomic_t user_count;
153
154 atomic_t mmap_count;
155 struct mutex buf_mutex;
156
157 struct list_head iter_list;
158
159 bool stop_on_full;
160
161 /* config */
162 unsigned int enabled : 1,
163 wrap : 1,
164 do_irq : 1,
165 multi_is_broken : 1;
166 unsigned int mode;
167 unsigned int burst_len;
168 unsigned int index;
169 };
170
171 static LIST_HEAD(msu_buffer_list);
172 static DEFINE_MUTEX(msu_buffer_mutex);
173
174 /**
175 * struct msu_buffer_entry - internal MSU buffer bookkeeping
176 * @entry: link to msu_buffer_list
177 * @mbuf: MSU buffer object
178 * @owner: module that provides this MSU buffer
179 */
180 struct msu_buffer_entry {
181 struct list_head entry;
182 const struct msu_buffer *mbuf;
183 struct module *owner;
184 };
185
__msu_buffer_entry_find(const char * name)186 static struct msu_buffer_entry *__msu_buffer_entry_find(const char *name)
187 {
188 struct msu_buffer_entry *mbe;
189
190 lockdep_assert_held(&msu_buffer_mutex);
191
192 list_for_each_entry(mbe, &msu_buffer_list, entry) {
193 if (!strcmp(mbe->mbuf->name, name))
194 return mbe;
195 }
196
197 return NULL;
198 }
199
200 static const struct msu_buffer *
msu_buffer_get(const char * name)201 msu_buffer_get(const char *name)
202 {
203 struct msu_buffer_entry *mbe;
204
205 mutex_lock(&msu_buffer_mutex);
206 mbe = __msu_buffer_entry_find(name);
207 if (mbe && !try_module_get(mbe->owner))
208 mbe = NULL;
209 mutex_unlock(&msu_buffer_mutex);
210
211 return mbe ? mbe->mbuf : NULL;
212 }
213
msu_buffer_put(const struct msu_buffer * mbuf)214 static void msu_buffer_put(const struct msu_buffer *mbuf)
215 {
216 struct msu_buffer_entry *mbe;
217
218 mutex_lock(&msu_buffer_mutex);
219 mbe = __msu_buffer_entry_find(mbuf->name);
220 if (mbe)
221 module_put(mbe->owner);
222 mutex_unlock(&msu_buffer_mutex);
223 }
224
intel_th_msu_buffer_register(const struct msu_buffer * mbuf,struct module * owner)225 int intel_th_msu_buffer_register(const struct msu_buffer *mbuf,
226 struct module *owner)
227 {
228 struct msu_buffer_entry *mbe;
229 int ret = 0;
230
231 mbe = kzalloc(sizeof(*mbe), GFP_KERNEL);
232 if (!mbe)
233 return -ENOMEM;
234
235 mutex_lock(&msu_buffer_mutex);
236 if (__msu_buffer_entry_find(mbuf->name)) {
237 ret = -EEXIST;
238 kfree(mbe);
239 goto unlock;
240 }
241
242 mbe->mbuf = mbuf;
243 mbe->owner = owner;
244 list_add_tail(&mbe->entry, &msu_buffer_list);
245 unlock:
246 mutex_unlock(&msu_buffer_mutex);
247
248 return ret;
249 }
250 EXPORT_SYMBOL_GPL(intel_th_msu_buffer_register);
251
intel_th_msu_buffer_unregister(const struct msu_buffer * mbuf)252 void intel_th_msu_buffer_unregister(const struct msu_buffer *mbuf)
253 {
254 struct msu_buffer_entry *mbe;
255
256 mutex_lock(&msu_buffer_mutex);
257 mbe = __msu_buffer_entry_find(mbuf->name);
258 if (mbe) {
259 list_del(&mbe->entry);
260 kfree(mbe);
261 }
262 mutex_unlock(&msu_buffer_mutex);
263 }
264 EXPORT_SYMBOL_GPL(intel_th_msu_buffer_unregister);
265
msc_block_is_empty(struct msc_block_desc * bdesc)266 static inline bool msc_block_is_empty(struct msc_block_desc *bdesc)
267 {
268 /* header hasn't been written */
269 if (!bdesc->valid_dw)
270 return true;
271
272 /* valid_dw includes the header */
273 if (!msc_data_sz(bdesc))
274 return true;
275
276 return false;
277 }
278
msc_win_base_sg(struct msc_window * win)279 static inline struct scatterlist *msc_win_base_sg(struct msc_window *win)
280 {
281 return win->sgt->sgl;
282 }
283
msc_win_base(struct msc_window * win)284 static inline struct msc_block_desc *msc_win_base(struct msc_window *win)
285 {
286 return sg_virt(msc_win_base_sg(win));
287 }
288
msc_win_base_dma(struct msc_window * win)289 static inline dma_addr_t msc_win_base_dma(struct msc_window *win)
290 {
291 return sg_dma_address(msc_win_base_sg(win));
292 }
293
294 static inline unsigned long
msc_win_base_pfn(struct msc_window * win)295 msc_win_base_pfn(struct msc_window *win)
296 {
297 return PFN_DOWN(msc_win_base_dma(win));
298 }
299
300 /**
301 * msc_is_last_win() - check if a window is the last one for a given MSC
302 * @win: window
303 * Return: true if @win is the last window in MSC's multiblock buffer
304 */
msc_is_last_win(struct msc_window * win)305 static inline bool msc_is_last_win(struct msc_window *win)
306 {
307 return win->entry.next == &win->msc->win_list;
308 }
309
310 /**
311 * msc_next_window() - return next window in the multiblock buffer
312 * @win: current window
313 *
314 * Return: window following the current one
315 */
msc_next_window(struct msc_window * win)316 static struct msc_window *msc_next_window(struct msc_window *win)
317 {
318 if (msc_is_last_win(win))
319 return list_first_entry(&win->msc->win_list, struct msc_window,
320 entry);
321
322 return list_next_entry(win, entry);
323 }
324
msc_win_total_sz(struct msc_window * win)325 static size_t msc_win_total_sz(struct msc_window *win)
326 {
327 struct scatterlist *sg;
328 unsigned int blk;
329 size_t size = 0;
330
331 for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
332 struct msc_block_desc *bdesc = sg_virt(sg);
333
334 if (msc_block_wrapped(bdesc))
335 return (size_t)win->nr_blocks << PAGE_SHIFT;
336
337 size += msc_total_sz(bdesc);
338 if (msc_block_last_written(bdesc))
339 break;
340 }
341
342 return size;
343 }
344
345 /**
346 * msc_find_window() - find a window matching a given sg_table
347 * @msc: MSC device
348 * @sgt: SG table of the window
349 * @nonempty: skip over empty windows
350 *
351 * Return: MSC window structure pointer or NULL if the window
352 * could not be found.
353 */
354 static struct msc_window *
msc_find_window(struct msc * msc,struct sg_table * sgt,bool nonempty)355 msc_find_window(struct msc *msc, struct sg_table *sgt, bool nonempty)
356 {
357 struct msc_window *win;
358 unsigned int found = 0;
359
360 if (list_empty(&msc->win_list))
361 return NULL;
362
363 /*
364 * we might need a radix tree for this, depending on how
365 * many windows a typical user would allocate; ideally it's
366 * something like 2, in which case we're good
367 */
368 list_for_each_entry(win, &msc->win_list, entry) {
369 if (win->sgt == sgt)
370 found++;
371
372 /* skip the empty ones */
373 if (nonempty && msc_block_is_empty(msc_win_base(win)))
374 continue;
375
376 if (found)
377 return win;
378 }
379
380 return NULL;
381 }
382
383 /**
384 * msc_oldest_window() - locate the window with oldest data
385 * @msc: MSC device
386 *
387 * This should only be used in multiblock mode. Caller should hold the
388 * msc::user_count reference.
389 *
390 * Return: the oldest window with valid data
391 */
msc_oldest_window(struct msc * msc)392 static struct msc_window *msc_oldest_window(struct msc *msc)
393 {
394 struct msc_window *win;
395
396 if (list_empty(&msc->win_list))
397 return NULL;
398
399 win = msc_find_window(msc, msc_next_window(msc->cur_win)->sgt, true);
400 if (win)
401 return win;
402
403 return list_first_entry(&msc->win_list, struct msc_window, entry);
404 }
405
406 /**
407 * msc_win_oldest_sg() - locate the oldest block in a given window
408 * @win: window to look at
409 *
410 * Return: index of the block with the oldest data
411 */
msc_win_oldest_sg(struct msc_window * win)412 static struct scatterlist *msc_win_oldest_sg(struct msc_window *win)
413 {
414 unsigned int blk;
415 struct scatterlist *sg;
416 struct msc_block_desc *bdesc = msc_win_base(win);
417
418 /* without wrapping, first block is the oldest */
419 if (!msc_block_wrapped(bdesc))
420 return msc_win_base_sg(win);
421
422 /*
423 * with wrapping, last written block contains both the newest and the
424 * oldest data for this window.
425 */
426 for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
427 struct msc_block_desc *bdesc = sg_virt(sg);
428
429 if (msc_block_last_written(bdesc))
430 return sg;
431 }
432
433 return msc_win_base_sg(win);
434 }
435
msc_iter_bdesc(struct msc_iter * iter)436 static struct msc_block_desc *msc_iter_bdesc(struct msc_iter *iter)
437 {
438 return sg_virt(iter->block);
439 }
440
msc_iter_install(struct msc * msc)441 static struct msc_iter *msc_iter_install(struct msc *msc)
442 {
443 struct msc_iter *iter;
444
445 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
446 if (!iter)
447 return ERR_PTR(-ENOMEM);
448
449 mutex_lock(&msc->buf_mutex);
450
451 /*
452 * Reading and tracing are mutually exclusive; if msc is
453 * enabled, open() will fail; otherwise existing readers
454 * will prevent enabling the msc and the rest of fops don't
455 * need to worry about it.
456 */
457 if (msc->enabled) {
458 kfree(iter);
459 iter = ERR_PTR(-EBUSY);
460 goto unlock;
461 }
462
463 iter->msc = msc;
464
465 list_add_tail(&iter->entry, &msc->iter_list);
466 unlock:
467 mutex_unlock(&msc->buf_mutex);
468
469 return iter;
470 }
471
msc_iter_remove(struct msc_iter * iter,struct msc * msc)472 static void msc_iter_remove(struct msc_iter *iter, struct msc *msc)
473 {
474 mutex_lock(&msc->buf_mutex);
475 list_del(&iter->entry);
476 mutex_unlock(&msc->buf_mutex);
477
478 kfree(iter);
479 }
480
msc_iter_block_start(struct msc_iter * iter)481 static void msc_iter_block_start(struct msc_iter *iter)
482 {
483 if (iter->start_block)
484 return;
485
486 iter->start_block = msc_win_oldest_sg(iter->win);
487 iter->block = iter->start_block;
488 iter->wrap_count = 0;
489
490 /*
491 * start with the block with oldest data; if data has wrapped
492 * in this window, it should be in this block
493 */
494 if (msc_block_wrapped(msc_iter_bdesc(iter)))
495 iter->wrap_count = 2;
496
497 }
498
msc_iter_win_start(struct msc_iter * iter,struct msc * msc)499 static int msc_iter_win_start(struct msc_iter *iter, struct msc *msc)
500 {
501 /* already started, nothing to do */
502 if (iter->start_win)
503 return 0;
504
505 iter->start_win = msc_oldest_window(msc);
506 if (!iter->start_win)
507 return -EINVAL;
508
509 iter->win = iter->start_win;
510 iter->start_block = NULL;
511
512 msc_iter_block_start(iter);
513
514 return 0;
515 }
516
msc_iter_win_advance(struct msc_iter * iter)517 static int msc_iter_win_advance(struct msc_iter *iter)
518 {
519 iter->win = msc_next_window(iter->win);
520 iter->start_block = NULL;
521
522 if (iter->win == iter->start_win) {
523 iter->eof++;
524 return 1;
525 }
526
527 msc_iter_block_start(iter);
528
529 return 0;
530 }
531
msc_iter_block_advance(struct msc_iter * iter)532 static int msc_iter_block_advance(struct msc_iter *iter)
533 {
534 iter->block_off = 0;
535
536 /* wrapping */
537 if (iter->wrap_count && iter->block == iter->start_block) {
538 iter->wrap_count--;
539 if (!iter->wrap_count)
540 /* copied newest data from the wrapped block */
541 return msc_iter_win_advance(iter);
542 }
543
544 /* no wrapping, check for last written block */
545 if (!iter->wrap_count && msc_block_last_written(msc_iter_bdesc(iter)))
546 /* copied newest data for the window */
547 return msc_iter_win_advance(iter);
548
549 /* block advance */
550 if (sg_is_last(iter->block))
551 iter->block = msc_win_base_sg(iter->win);
552 else
553 iter->block = sg_next(iter->block);
554
555 /* no wrapping, sanity check in case there is no last written block */
556 if (!iter->wrap_count && iter->block == iter->start_block)
557 return msc_iter_win_advance(iter);
558
559 return 0;
560 }
561
562 /**
563 * msc_buffer_iterate() - go through multiblock buffer's data
564 * @iter: iterator structure
565 * @size: amount of data to scan
566 * @data: callback's private data
567 * @fn: iterator callback
568 *
569 * This will start at the window which will be written to next (containing
570 * the oldest data) and work its way to the current window, calling @fn
571 * for each chunk of data as it goes.
572 *
573 * Caller should have msc::user_count reference to make sure the buffer
574 * doesn't disappear from under us.
575 *
576 * Return: amount of data actually scanned.
577 */
578 static ssize_t
msc_buffer_iterate(struct msc_iter * iter,size_t size,void * data,unsigned long (* fn)(void *,void *,size_t))579 msc_buffer_iterate(struct msc_iter *iter, size_t size, void *data,
580 unsigned long (*fn)(void *, void *, size_t))
581 {
582 struct msc *msc = iter->msc;
583 size_t len = size;
584 unsigned int advance;
585
586 if (iter->eof)
587 return 0;
588
589 /* start with the oldest window */
590 if (msc_iter_win_start(iter, msc))
591 return 0;
592
593 do {
594 unsigned long data_bytes = msc_data_sz(msc_iter_bdesc(iter));
595 void *src = (void *)msc_iter_bdesc(iter) + MSC_BDESC;
596 size_t tocopy = data_bytes, copied = 0;
597 size_t remaining = 0;
598
599 advance = 1;
600
601 /*
602 * If block wrapping happened, we need to visit the last block
603 * twice, because it contains both the oldest and the newest
604 * data in this window.
605 *
606 * First time (wrap_count==2), in the very beginning, to collect
607 * the oldest data, which is in the range
608 * (data_bytes..DATA_IN_PAGE).
609 *
610 * Second time (wrap_count==1), it's just like any other block,
611 * containing data in the range of [MSC_BDESC..data_bytes].
612 */
613 if (iter->block == iter->start_block && iter->wrap_count == 2) {
614 tocopy = DATA_IN_PAGE - data_bytes;
615 src += data_bytes;
616 }
617
618 if (!tocopy)
619 goto next_block;
620
621 tocopy -= iter->block_off;
622 src += iter->block_off;
623
624 if (len < tocopy) {
625 tocopy = len;
626 advance = 0;
627 }
628
629 remaining = fn(data, src, tocopy);
630
631 if (remaining)
632 advance = 0;
633
634 copied = tocopy - remaining;
635 len -= copied;
636 iter->block_off += copied;
637 iter->offset += copied;
638
639 if (!advance)
640 break;
641
642 next_block:
643 if (msc_iter_block_advance(iter))
644 break;
645
646 } while (len);
647
648 return size - len;
649 }
650
651 /**
652 * msc_buffer_clear_hw_header() - clear hw header for multiblock
653 * @msc: MSC device
654 */
msc_buffer_clear_hw_header(struct msc * msc)655 static void msc_buffer_clear_hw_header(struct msc *msc)
656 {
657 struct msc_window *win;
658 struct scatterlist *sg;
659
660 list_for_each_entry(win, &msc->win_list, entry) {
661 unsigned int blk;
662
663 for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
664 struct msc_block_desc *bdesc = sg_virt(sg);
665
666 memset_startat(bdesc, 0, hw_tag);
667 }
668 }
669 }
670
intel_th_msu_init(struct msc * msc)671 static int intel_th_msu_init(struct msc *msc)
672 {
673 u32 mintctl, msusts;
674
675 if (!msc->do_irq)
676 return 0;
677
678 if (!msc->mbuf)
679 return 0;
680
681 mintctl = ioread32(msc->msu_base + REG_MSU_MINTCTL);
682 mintctl |= msc->index ? M1BLIE : M0BLIE;
683 iowrite32(mintctl, msc->msu_base + REG_MSU_MINTCTL);
684 if (mintctl != ioread32(msc->msu_base + REG_MSU_MINTCTL)) {
685 dev_info(msc_dev(msc), "MINTCTL ignores writes: no usable interrupts\n");
686 msc->do_irq = 0;
687 return 0;
688 }
689
690 msusts = ioread32(msc->msu_base + REG_MSU_MSUSTS);
691 iowrite32(msusts, msc->msu_base + REG_MSU_MSUSTS);
692
693 return 0;
694 }
695
intel_th_msu_deinit(struct msc * msc)696 static void intel_th_msu_deinit(struct msc *msc)
697 {
698 u32 mintctl;
699
700 if (!msc->do_irq)
701 return;
702
703 mintctl = ioread32(msc->msu_base + REG_MSU_MINTCTL);
704 mintctl &= msc->index ? ~M1BLIE : ~M0BLIE;
705 iowrite32(mintctl, msc->msu_base + REG_MSU_MINTCTL);
706 }
707
msc_win_set_lockout(struct msc_window * win,enum lockout_state expect,enum lockout_state new)708 static int msc_win_set_lockout(struct msc_window *win,
709 enum lockout_state expect,
710 enum lockout_state new)
711 {
712 enum lockout_state old;
713 unsigned long flags;
714 int ret = 0;
715
716 if (!win->msc->mbuf)
717 return 0;
718
719 spin_lock_irqsave(&win->lo_lock, flags);
720 old = win->lockout;
721
722 if (old != expect) {
723 ret = -EINVAL;
724 goto unlock;
725 }
726
727 win->lockout = new;
728
729 if (old == expect && new == WIN_LOCKED)
730 atomic_inc(&win->msc->user_count);
731 else if (old == expect && old == WIN_LOCKED)
732 atomic_dec(&win->msc->user_count);
733
734 unlock:
735 spin_unlock_irqrestore(&win->lo_lock, flags);
736
737 if (ret) {
738 if (expect == WIN_READY && old == WIN_LOCKED)
739 return -EBUSY;
740
741 /* from intel_th_msc_window_unlock(), don't warn if not locked */
742 if (expect == WIN_LOCKED && old == new)
743 return 0;
744
745 dev_warn_ratelimited(msc_dev(win->msc),
746 "expected lockout state %d, got %d\n",
747 expect, old);
748 }
749
750 return ret;
751 }
752 /**
753 * msc_configure() - set up MSC hardware
754 * @msc: the MSC device to configure
755 *
756 * Program storage mode, wrapping, burst length and trace buffer address
757 * into a given MSC. Then, enable tracing and set msc::enabled.
758 * The latter is serialized on msc::buf_mutex, so make sure to hold it.
759 */
msc_configure(struct msc * msc)760 static int msc_configure(struct msc *msc)
761 {
762 u32 reg;
763
764 lockdep_assert_held(&msc->buf_mutex);
765
766 if (msc->mode > MSC_MODE_MULTI)
767 return -EINVAL;
768
769 if (msc->mode == MSC_MODE_MULTI) {
770 if (msc_win_set_lockout(msc->cur_win, WIN_READY, WIN_INUSE))
771 return -EBUSY;
772
773 msc_buffer_clear_hw_header(msc);
774 }
775
776 msc->orig_addr = ioread32(msc->reg_base + REG_MSU_MSC0BAR);
777 msc->orig_sz = ioread32(msc->reg_base + REG_MSU_MSC0SIZE);
778
779 reg = msc->base_addr >> PAGE_SHIFT;
780 iowrite32(reg, msc->reg_base + REG_MSU_MSC0BAR);
781
782 if (msc->mode == MSC_MODE_SINGLE) {
783 reg = msc->nr_pages;
784 iowrite32(reg, msc->reg_base + REG_MSU_MSC0SIZE);
785 }
786
787 reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
788 reg &= ~(MSC_MODE | MSC_WRAPEN | MSC_EN | MSC_RD_HDR_OVRD);
789
790 reg |= MSC_EN;
791 reg |= msc->mode << __ffs(MSC_MODE);
792 reg |= msc->burst_len << __ffs(MSC_LEN);
793
794 if (msc->wrap)
795 reg |= MSC_WRAPEN;
796
797 iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
798
799 intel_th_msu_init(msc);
800
801 msc->thdev->output.multiblock = msc->mode == MSC_MODE_MULTI;
802 intel_th_trace_enable(msc->thdev);
803 msc->enabled = 1;
804
805 if (msc->mbuf && msc->mbuf->activate)
806 msc->mbuf->activate(msc->mbuf_priv);
807
808 return 0;
809 }
810
811 /**
812 * msc_disable() - disable MSC hardware
813 * @msc: MSC device to disable
814 *
815 * If @msc is enabled, disable tracing on the switch and then disable MSC
816 * storage. Caller must hold msc::buf_mutex.
817 */
msc_disable(struct msc * msc)818 static void msc_disable(struct msc *msc)
819 {
820 struct msc_window *win = msc->cur_win;
821 u32 reg;
822
823 lockdep_assert_held(&msc->buf_mutex);
824
825 if (msc->mode == MSC_MODE_MULTI)
826 msc_win_set_lockout(win, WIN_INUSE, WIN_LOCKED);
827
828 if (msc->mbuf && msc->mbuf->deactivate)
829 msc->mbuf->deactivate(msc->mbuf_priv);
830 intel_th_msu_deinit(msc);
831 intel_th_trace_disable(msc->thdev);
832
833 if (msc->mode == MSC_MODE_SINGLE) {
834 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
835 msc->single_wrap = !!(reg & MSCSTS_WRAPSTAT);
836
837 reg = ioread32(msc->reg_base + REG_MSU_MSC0MWP);
838 msc->single_sz = reg & ((msc->nr_pages << PAGE_SHIFT) - 1);
839 dev_dbg(msc_dev(msc), "MSCnMWP: %08x/%08lx, wrap: %d\n",
840 reg, msc->single_sz, msc->single_wrap);
841 }
842
843 reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
844 reg &= ~MSC_EN;
845 iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
846
847 if (msc->mbuf && msc->mbuf->ready)
848 msc->mbuf->ready(msc->mbuf_priv, win->sgt,
849 msc_win_total_sz(win));
850
851 msc->enabled = 0;
852
853 iowrite32(msc->orig_addr, msc->reg_base + REG_MSU_MSC0BAR);
854 iowrite32(msc->orig_sz, msc->reg_base + REG_MSU_MSC0SIZE);
855
856 dev_dbg(msc_dev(msc), "MSCnNWSA: %08x\n",
857 ioread32(msc->reg_base + REG_MSU_MSC0NWSA));
858
859 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
860 dev_dbg(msc_dev(msc), "MSCnSTS: %08x\n", reg);
861
862 reg = ioread32(msc->reg_base + REG_MSU_MSUSTS);
863 reg &= msc->index ? MSUSTS_MSC1BLAST : MSUSTS_MSC0BLAST;
864 iowrite32(reg, msc->reg_base + REG_MSU_MSUSTS);
865 }
866
intel_th_msc_activate(struct intel_th_device * thdev)867 static int intel_th_msc_activate(struct intel_th_device *thdev)
868 {
869 struct msc *msc = dev_get_drvdata(&thdev->dev);
870 int ret = -EBUSY;
871
872 if (!atomic_inc_unless_negative(&msc->user_count))
873 return -ENODEV;
874
875 mutex_lock(&msc->buf_mutex);
876
877 /* if there are readers, refuse */
878 if (list_empty(&msc->iter_list))
879 ret = msc_configure(msc);
880
881 mutex_unlock(&msc->buf_mutex);
882
883 if (ret)
884 atomic_dec(&msc->user_count);
885
886 return ret;
887 }
888
intel_th_msc_deactivate(struct intel_th_device * thdev)889 static void intel_th_msc_deactivate(struct intel_th_device *thdev)
890 {
891 struct msc *msc = dev_get_drvdata(&thdev->dev);
892
893 mutex_lock(&msc->buf_mutex);
894 if (msc->enabled) {
895 msc_disable(msc);
896 atomic_dec(&msc->user_count);
897 }
898 mutex_unlock(&msc->buf_mutex);
899 }
900
901 /**
902 * msc_buffer_contig_alloc() - allocate a contiguous buffer for SINGLE mode
903 * @msc: MSC device
904 * @size: allocation size in bytes
905 *
906 * This modifies msc::base, which requires msc::buf_mutex to serialize, so the
907 * caller is expected to hold it.
908 *
909 * Return: 0 on success, -errno otherwise.
910 */
msc_buffer_contig_alloc(struct msc * msc,unsigned long size)911 static int msc_buffer_contig_alloc(struct msc *msc, unsigned long size)
912 {
913 unsigned long nr_pages = size >> PAGE_SHIFT;
914 unsigned int order = get_order(size);
915 struct page *page;
916 int ret;
917
918 if (!size)
919 return 0;
920
921 ret = sg_alloc_table(&msc->single_sgt, 1, GFP_KERNEL);
922 if (ret)
923 goto err_out;
924
925 ret = -ENOMEM;
926 page = alloc_pages(GFP_KERNEL | __GFP_ZERO | GFP_DMA32, order);
927 if (!page)
928 goto err_free_sgt;
929
930 split_page(page, order);
931 sg_set_buf(msc->single_sgt.sgl, page_address(page), size);
932
933 ret = dma_map_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl, 1,
934 DMA_FROM_DEVICE);
935 if (ret < 0)
936 goto err_free_pages;
937
938 msc->nr_pages = nr_pages;
939 msc->base = page_address(page);
940 msc->base_addr = sg_dma_address(msc->single_sgt.sgl);
941
942 return 0;
943
944 err_free_pages:
945 __free_pages(page, order);
946
947 err_free_sgt:
948 sg_free_table(&msc->single_sgt);
949
950 err_out:
951 return ret;
952 }
953
954 /**
955 * msc_buffer_contig_free() - free a contiguous buffer
956 * @msc: MSC configured in SINGLE mode
957 */
msc_buffer_contig_free(struct msc * msc)958 static void msc_buffer_contig_free(struct msc *msc)
959 {
960 unsigned long off;
961
962 dma_unmap_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl,
963 1, DMA_FROM_DEVICE);
964 sg_free_table(&msc->single_sgt);
965
966 for (off = 0; off < msc->nr_pages << PAGE_SHIFT; off += PAGE_SIZE) {
967 struct page *page = virt_to_page(msc->base + off);
968
969 __free_page(page);
970 }
971
972 msc->nr_pages = 0;
973 }
974
975 /**
976 * msc_buffer_contig_get_page() - find a page at a given offset
977 * @msc: MSC configured in SINGLE mode
978 * @pgoff: page offset
979 *
980 * Return: page, if @pgoff is within the range, NULL otherwise.
981 */
msc_buffer_contig_get_page(struct msc * msc,unsigned long pgoff)982 static struct page *msc_buffer_contig_get_page(struct msc *msc,
983 unsigned long pgoff)
984 {
985 if (pgoff >= msc->nr_pages)
986 return NULL;
987
988 return virt_to_page(msc->base + (pgoff << PAGE_SHIFT));
989 }
990
__msc_buffer_win_alloc(struct msc_window * win,unsigned int nr_segs)991 static int __msc_buffer_win_alloc(struct msc_window *win,
992 unsigned int nr_segs)
993 {
994 struct scatterlist *sg_ptr;
995 void *block;
996 int i, ret;
997
998 ret = sg_alloc_table(win->sgt, nr_segs, GFP_KERNEL);
999 if (ret)
1000 return -ENOMEM;
1001
1002 for_each_sg(win->sgt->sgl, sg_ptr, nr_segs, i) {
1003 block = dma_alloc_coherent(msc_dev(win->msc)->parent->parent,
1004 PAGE_SIZE, &sg_dma_address(sg_ptr),
1005 GFP_KERNEL);
1006 if (!block)
1007 goto err_nomem;
1008
1009 sg_set_buf(sg_ptr, block, PAGE_SIZE);
1010 }
1011
1012 return nr_segs;
1013
1014 err_nomem:
1015 for_each_sg(win->sgt->sgl, sg_ptr, i, ret)
1016 dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
1017 sg_virt(sg_ptr), sg_dma_address(sg_ptr));
1018
1019 sg_free_table(win->sgt);
1020
1021 return -ENOMEM;
1022 }
1023
1024 #ifdef CONFIG_X86
msc_buffer_set_uc(struct msc * msc)1025 static void msc_buffer_set_uc(struct msc *msc)
1026 {
1027 struct scatterlist *sg_ptr;
1028 struct msc_window *win;
1029 int i;
1030
1031 if (msc->mode == MSC_MODE_SINGLE) {
1032 set_memory_uc((unsigned long)msc->base, msc->nr_pages);
1033 return;
1034 }
1035
1036 list_for_each_entry(win, &msc->win_list, entry) {
1037 for_each_sg(win->sgt->sgl, sg_ptr, win->nr_segs, i) {
1038 /* Set the page as uncached */
1039 set_memory_uc((unsigned long)sg_virt(sg_ptr),
1040 PFN_DOWN(sg_ptr->length));
1041 }
1042 }
1043 }
1044
msc_buffer_set_wb(struct msc * msc)1045 static void msc_buffer_set_wb(struct msc *msc)
1046 {
1047 struct scatterlist *sg_ptr;
1048 struct msc_window *win;
1049 int i;
1050
1051 if (msc->mode == MSC_MODE_SINGLE) {
1052 set_memory_wb((unsigned long)msc->base, msc->nr_pages);
1053 return;
1054 }
1055
1056 list_for_each_entry(win, &msc->win_list, entry) {
1057 for_each_sg(win->sgt->sgl, sg_ptr, win->nr_segs, i) {
1058 /* Reset the page to write-back */
1059 set_memory_wb((unsigned long)sg_virt(sg_ptr),
1060 PFN_DOWN(sg_ptr->length));
1061 }
1062 }
1063 }
1064 #else /* !X86 */
1065 static inline void
msc_buffer_set_uc(struct msc * msc)1066 msc_buffer_set_uc(struct msc *msc) {}
msc_buffer_set_wb(struct msc * msc)1067 static inline void msc_buffer_set_wb(struct msc *msc) {}
1068 #endif /* CONFIG_X86 */
1069
msc_sg_page(struct scatterlist * sg)1070 static struct page *msc_sg_page(struct scatterlist *sg)
1071 {
1072 void *addr = sg_virt(sg);
1073
1074 if (is_vmalloc_addr(addr))
1075 return vmalloc_to_page(addr);
1076
1077 return sg_page(sg);
1078 }
1079
1080 /**
1081 * msc_buffer_win_alloc() - alloc a window for a multiblock mode
1082 * @msc: MSC device
1083 * @nr_blocks: number of pages in this window
1084 *
1085 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
1086 * to serialize, so the caller is expected to hold it.
1087 *
1088 * Return: 0 on success, -errno otherwise.
1089 */
msc_buffer_win_alloc(struct msc * msc,unsigned int nr_blocks)1090 static int msc_buffer_win_alloc(struct msc *msc, unsigned int nr_blocks)
1091 {
1092 struct msc_window *win;
1093 int ret = -ENOMEM;
1094
1095 if (!nr_blocks)
1096 return 0;
1097
1098 win = kzalloc(sizeof(*win), GFP_KERNEL);
1099 if (!win)
1100 return -ENOMEM;
1101
1102 win->msc = msc;
1103 win->sgt = &win->_sgt;
1104 win->lockout = WIN_READY;
1105 spin_lock_init(&win->lo_lock);
1106
1107 if (!list_empty(&msc->win_list)) {
1108 struct msc_window *prev = list_last_entry(&msc->win_list,
1109 struct msc_window,
1110 entry);
1111
1112 win->pgoff = prev->pgoff + prev->nr_blocks;
1113 }
1114
1115 if (msc->mbuf && msc->mbuf->alloc_window)
1116 ret = msc->mbuf->alloc_window(msc->mbuf_priv, &win->sgt,
1117 nr_blocks << PAGE_SHIFT);
1118 else
1119 ret = __msc_buffer_win_alloc(win, nr_blocks);
1120
1121 if (ret <= 0)
1122 goto err_nomem;
1123
1124 win->nr_segs = ret;
1125 win->nr_blocks = nr_blocks;
1126
1127 if (list_empty(&msc->win_list)) {
1128 msc->base = msc_win_base(win);
1129 msc->base_addr = msc_win_base_dma(win);
1130 msc->cur_win = win;
1131 }
1132
1133 list_add_tail(&win->entry, &msc->win_list);
1134 msc->nr_pages += nr_blocks;
1135
1136 return 0;
1137
1138 err_nomem:
1139 kfree(win);
1140
1141 return ret;
1142 }
1143
__msc_buffer_win_free(struct msc * msc,struct msc_window * win)1144 static void __msc_buffer_win_free(struct msc *msc, struct msc_window *win)
1145 {
1146 struct scatterlist *sg;
1147 int i;
1148
1149 for_each_sg(win->sgt->sgl, sg, win->nr_segs, i) {
1150 dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
1151 sg_virt(sg), sg_dma_address(sg));
1152 }
1153 sg_free_table(win->sgt);
1154 }
1155
1156 /**
1157 * msc_buffer_win_free() - free a window from MSC's window list
1158 * @msc: MSC device
1159 * @win: window to free
1160 *
1161 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
1162 * to serialize, so the caller is expected to hold it.
1163 */
msc_buffer_win_free(struct msc * msc,struct msc_window * win)1164 static void msc_buffer_win_free(struct msc *msc, struct msc_window *win)
1165 {
1166 msc->nr_pages -= win->nr_blocks;
1167
1168 list_del(&win->entry);
1169 if (list_empty(&msc->win_list)) {
1170 msc->base = NULL;
1171 msc->base_addr = 0;
1172 }
1173
1174 if (msc->mbuf && msc->mbuf->free_window)
1175 msc->mbuf->free_window(msc->mbuf_priv, win->sgt);
1176 else
1177 __msc_buffer_win_free(msc, win);
1178
1179 kfree(win);
1180 }
1181
1182 /**
1183 * msc_buffer_relink() - set up block descriptors for multiblock mode
1184 * @msc: MSC device
1185 *
1186 * This traverses msc::win_list, which requires msc::buf_mutex to serialize,
1187 * so the caller is expected to hold it.
1188 */
msc_buffer_relink(struct msc * msc)1189 static void msc_buffer_relink(struct msc *msc)
1190 {
1191 struct msc_window *win, *next_win;
1192
1193 /* call with msc::mutex locked */
1194 list_for_each_entry(win, &msc->win_list, entry) {
1195 struct scatterlist *sg;
1196 unsigned int blk;
1197 u32 sw_tag = 0;
1198
1199 /*
1200 * Last window's next_win should point to the first window
1201 * and MSC_SW_TAG_LASTWIN should be set.
1202 */
1203 if (msc_is_last_win(win)) {
1204 sw_tag |= MSC_SW_TAG_LASTWIN;
1205 next_win = list_first_entry(&msc->win_list,
1206 struct msc_window, entry);
1207 } else {
1208 next_win = list_next_entry(win, entry);
1209 }
1210
1211 for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
1212 struct msc_block_desc *bdesc = sg_virt(sg);
1213
1214 memset(bdesc, 0, sizeof(*bdesc));
1215
1216 bdesc->next_win = msc_win_base_pfn(next_win);
1217
1218 /*
1219 * Similarly to last window, last block should point
1220 * to the first one.
1221 */
1222 if (blk == win->nr_segs - 1) {
1223 sw_tag |= MSC_SW_TAG_LASTBLK;
1224 bdesc->next_blk = msc_win_base_pfn(win);
1225 } else {
1226 dma_addr_t addr = sg_dma_address(sg_next(sg));
1227
1228 bdesc->next_blk = PFN_DOWN(addr);
1229 }
1230
1231 bdesc->sw_tag = sw_tag;
1232 bdesc->block_sz = sg->length / 64;
1233 }
1234 }
1235
1236 /*
1237 * Make the above writes globally visible before tracing is
1238 * enabled to make sure hardware sees them coherently.
1239 */
1240 wmb();
1241 }
1242
msc_buffer_multi_free(struct msc * msc)1243 static void msc_buffer_multi_free(struct msc *msc)
1244 {
1245 struct msc_window *win, *iter;
1246
1247 list_for_each_entry_safe(win, iter, &msc->win_list, entry)
1248 msc_buffer_win_free(msc, win);
1249 }
1250
msc_buffer_multi_alloc(struct msc * msc,unsigned long * nr_pages,unsigned int nr_wins)1251 static int msc_buffer_multi_alloc(struct msc *msc, unsigned long *nr_pages,
1252 unsigned int nr_wins)
1253 {
1254 int ret, i;
1255
1256 for (i = 0; i < nr_wins; i++) {
1257 ret = msc_buffer_win_alloc(msc, nr_pages[i]);
1258 if (ret) {
1259 msc_buffer_multi_free(msc);
1260 return ret;
1261 }
1262 }
1263
1264 msc_buffer_relink(msc);
1265
1266 return 0;
1267 }
1268
1269 /**
1270 * msc_buffer_free() - free buffers for MSC
1271 * @msc: MSC device
1272 *
1273 * Free MSC's storage buffers.
1274 *
1275 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex to
1276 * serialize, so the caller is expected to hold it.
1277 */
msc_buffer_free(struct msc * msc)1278 static void msc_buffer_free(struct msc *msc)
1279 {
1280 msc_buffer_set_wb(msc);
1281
1282 if (msc->mode == MSC_MODE_SINGLE)
1283 msc_buffer_contig_free(msc);
1284 else if (msc->mode == MSC_MODE_MULTI)
1285 msc_buffer_multi_free(msc);
1286 }
1287
1288 /**
1289 * msc_buffer_alloc() - allocate a buffer for MSC
1290 * @msc: MSC device
1291 * @size: allocation size in bytes
1292 *
1293 * Allocate a storage buffer for MSC, depending on the msc::mode, it will be
1294 * either done via msc_buffer_contig_alloc() for SINGLE operation mode or
1295 * msc_buffer_win_alloc() for multiblock operation. The latter allocates one
1296 * window per invocation, so in multiblock mode this can be called multiple
1297 * times for the same MSC to allocate multiple windows.
1298 *
1299 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
1300 * to serialize, so the caller is expected to hold it.
1301 *
1302 * Return: 0 on success, -errno otherwise.
1303 */
msc_buffer_alloc(struct msc * msc,unsigned long * nr_pages,unsigned int nr_wins)1304 static int msc_buffer_alloc(struct msc *msc, unsigned long *nr_pages,
1305 unsigned int nr_wins)
1306 {
1307 int ret;
1308
1309 /* -1: buffer not allocated */
1310 if (atomic_read(&msc->user_count) != -1)
1311 return -EBUSY;
1312
1313 if (msc->mode == MSC_MODE_SINGLE) {
1314 if (nr_wins != 1)
1315 return -EINVAL;
1316
1317 ret = msc_buffer_contig_alloc(msc, nr_pages[0] << PAGE_SHIFT);
1318 } else if (msc->mode == MSC_MODE_MULTI) {
1319 ret = msc_buffer_multi_alloc(msc, nr_pages, nr_wins);
1320 } else {
1321 ret = -EINVAL;
1322 }
1323
1324 if (!ret) {
1325 msc_buffer_set_uc(msc);
1326
1327 /* allocation should be visible before the counter goes to 0 */
1328 smp_mb__before_atomic();
1329
1330 if (WARN_ON_ONCE(atomic_cmpxchg(&msc->user_count, -1, 0) != -1))
1331 return -EINVAL;
1332 }
1333
1334 return ret;
1335 }
1336
1337 /**
1338 * msc_buffer_unlocked_free_unless_used() - free a buffer unless it's in use
1339 * @msc: MSC device
1340 *
1341 * This will free MSC buffer unless it is in use or there is no allocated
1342 * buffer.
1343 * Caller needs to hold msc::buf_mutex.
1344 *
1345 * Return: 0 on successful deallocation or if there was no buffer to
1346 * deallocate, -EBUSY if there are active users.
1347 */
msc_buffer_unlocked_free_unless_used(struct msc * msc)1348 static int msc_buffer_unlocked_free_unless_used(struct msc *msc)
1349 {
1350 int count, ret = 0;
1351
1352 count = atomic_cmpxchg(&msc->user_count, 0, -1);
1353
1354 /* > 0: buffer is allocated and has users */
1355 if (count > 0)
1356 ret = -EBUSY;
1357 /* 0: buffer is allocated, no users */
1358 else if (!count)
1359 msc_buffer_free(msc);
1360 /* < 0: no buffer, nothing to do */
1361
1362 return ret;
1363 }
1364
1365 /**
1366 * msc_buffer_free_unless_used() - free a buffer unless it's in use
1367 * @msc: MSC device
1368 *
1369 * This is a locked version of msc_buffer_unlocked_free_unless_used().
1370 */
msc_buffer_free_unless_used(struct msc * msc)1371 static int msc_buffer_free_unless_used(struct msc *msc)
1372 {
1373 int ret;
1374
1375 mutex_lock(&msc->buf_mutex);
1376 ret = msc_buffer_unlocked_free_unless_used(msc);
1377 mutex_unlock(&msc->buf_mutex);
1378
1379 return ret;
1380 }
1381
1382 /**
1383 * msc_buffer_get_page() - get MSC buffer page at a given offset
1384 * @msc: MSC device
1385 * @pgoff: page offset into the storage buffer
1386 *
1387 * This traverses msc::win_list, so holding msc::buf_mutex is expected from
1388 * the caller.
1389 *
1390 * Return: page if @pgoff corresponds to a valid buffer page or NULL.
1391 */
msc_buffer_get_page(struct msc * msc,unsigned long pgoff)1392 static struct page *msc_buffer_get_page(struct msc *msc, unsigned long pgoff)
1393 {
1394 struct msc_window *win;
1395 struct scatterlist *sg;
1396 unsigned int blk;
1397
1398 if (msc->mode == MSC_MODE_SINGLE)
1399 return msc_buffer_contig_get_page(msc, pgoff);
1400
1401 list_for_each_entry(win, &msc->win_list, entry)
1402 if (pgoff >= win->pgoff && pgoff < win->pgoff + win->nr_blocks)
1403 goto found;
1404
1405 return NULL;
1406
1407 found:
1408 pgoff -= win->pgoff;
1409
1410 for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
1411 struct page *page = msc_sg_page(sg);
1412 size_t pgsz = PFN_DOWN(sg->length);
1413
1414 if (pgoff < pgsz)
1415 return page + pgoff;
1416
1417 pgoff -= pgsz;
1418 }
1419
1420 return NULL;
1421 }
1422
1423 /**
1424 * struct msc_win_to_user_struct - data for copy_to_user() callback
1425 * @buf: userspace buffer to copy data to
1426 * @offset: running offset
1427 */
1428 struct msc_win_to_user_struct {
1429 char __user *buf;
1430 unsigned long offset;
1431 };
1432
1433 /**
1434 * msc_win_to_user() - iterator for msc_buffer_iterate() to copy data to user
1435 * @data: callback's private data
1436 * @src: source buffer
1437 * @len: amount of data to copy from the source buffer
1438 */
msc_win_to_user(void * data,void * src,size_t len)1439 static unsigned long msc_win_to_user(void *data, void *src, size_t len)
1440 {
1441 struct msc_win_to_user_struct *u = data;
1442 unsigned long ret;
1443
1444 ret = copy_to_user(u->buf + u->offset, src, len);
1445 u->offset += len - ret;
1446
1447 return ret;
1448 }
1449
1450
1451 /*
1452 * file operations' callbacks
1453 */
1454
intel_th_msc_open(struct inode * inode,struct file * file)1455 static int intel_th_msc_open(struct inode *inode, struct file *file)
1456 {
1457 struct intel_th_device *thdev = file->private_data;
1458 struct msc *msc = dev_get_drvdata(&thdev->dev);
1459 struct msc_iter *iter;
1460
1461 if (!capable(CAP_SYS_RAWIO))
1462 return -EPERM;
1463
1464 iter = msc_iter_install(msc);
1465 if (IS_ERR(iter))
1466 return PTR_ERR(iter);
1467
1468 file->private_data = iter;
1469
1470 return nonseekable_open(inode, file);
1471 }
1472
intel_th_msc_release(struct inode * inode,struct file * file)1473 static int intel_th_msc_release(struct inode *inode, struct file *file)
1474 {
1475 struct msc_iter *iter = file->private_data;
1476 struct msc *msc = iter->msc;
1477
1478 msc_iter_remove(iter, msc);
1479
1480 return 0;
1481 }
1482
1483 static ssize_t
msc_single_to_user(struct msc * msc,char __user * buf,loff_t off,size_t len)1484 msc_single_to_user(struct msc *msc, char __user *buf, loff_t off, size_t len)
1485 {
1486 unsigned long size = msc->nr_pages << PAGE_SHIFT, rem = len;
1487 unsigned long start = off, tocopy = 0;
1488
1489 if (msc->single_wrap) {
1490 start += msc->single_sz;
1491 if (start < size) {
1492 tocopy = min(rem, size - start);
1493 if (copy_to_user(buf, msc->base + start, tocopy))
1494 return -EFAULT;
1495
1496 buf += tocopy;
1497 rem -= tocopy;
1498 start += tocopy;
1499 }
1500
1501 start &= size - 1;
1502 if (rem) {
1503 tocopy = min(rem, msc->single_sz - start);
1504 if (copy_to_user(buf, msc->base + start, tocopy))
1505 return -EFAULT;
1506
1507 rem -= tocopy;
1508 }
1509
1510 return len - rem;
1511 }
1512
1513 if (copy_to_user(buf, msc->base + start, rem))
1514 return -EFAULT;
1515
1516 return len;
1517 }
1518
intel_th_msc_read(struct file * file,char __user * buf,size_t len,loff_t * ppos)1519 static ssize_t intel_th_msc_read(struct file *file, char __user *buf,
1520 size_t len, loff_t *ppos)
1521 {
1522 struct msc_iter *iter = file->private_data;
1523 struct msc *msc = iter->msc;
1524 size_t size;
1525 loff_t off = *ppos;
1526 ssize_t ret = 0;
1527
1528 if (!atomic_inc_unless_negative(&msc->user_count))
1529 return 0;
1530
1531 if (msc->mode == MSC_MODE_SINGLE && !msc->single_wrap)
1532 size = msc->single_sz;
1533 else
1534 size = msc->nr_pages << PAGE_SHIFT;
1535
1536 if (!size)
1537 goto put_count;
1538
1539 if (off >= size)
1540 goto put_count;
1541
1542 if (off + len >= size)
1543 len = size - off;
1544
1545 if (msc->mode == MSC_MODE_SINGLE) {
1546 ret = msc_single_to_user(msc, buf, off, len);
1547 if (ret >= 0)
1548 *ppos += ret;
1549 } else if (msc->mode == MSC_MODE_MULTI) {
1550 struct msc_win_to_user_struct u = {
1551 .buf = buf,
1552 .offset = 0,
1553 };
1554
1555 ret = msc_buffer_iterate(iter, len, &u, msc_win_to_user);
1556 if (ret >= 0)
1557 *ppos = iter->offset;
1558 } else {
1559 ret = -EINVAL;
1560 }
1561
1562 put_count:
1563 atomic_dec(&msc->user_count);
1564
1565 return ret;
1566 }
1567
1568 /*
1569 * vm operations callbacks (vm_ops)
1570 */
1571
msc_mmap_open(struct vm_area_struct * vma)1572 static void msc_mmap_open(struct vm_area_struct *vma)
1573 {
1574 struct msc_iter *iter = vma->vm_file->private_data;
1575 struct msc *msc = iter->msc;
1576
1577 atomic_inc(&msc->mmap_count);
1578 }
1579
msc_mmap_close(struct vm_area_struct * vma)1580 static void msc_mmap_close(struct vm_area_struct *vma)
1581 {
1582 struct msc_iter *iter = vma->vm_file->private_data;
1583 struct msc *msc = iter->msc;
1584
1585 if (!atomic_dec_and_mutex_lock(&msc->mmap_count, &msc->buf_mutex))
1586 return;
1587
1588 /* last mapping -- drop user_count */
1589 atomic_dec(&msc->user_count);
1590 mutex_unlock(&msc->buf_mutex);
1591 }
1592
msc_mmap_fault(struct vm_fault * vmf)1593 static vm_fault_t msc_mmap_fault(struct vm_fault *vmf)
1594 {
1595 struct msc_iter *iter = vmf->vma->vm_file->private_data;
1596 struct msc *msc = iter->msc;
1597 struct page *page;
1598
1599 page = msc_buffer_get_page(msc, vmf->pgoff);
1600 if (!page)
1601 return VM_FAULT_SIGBUS;
1602
1603 get_page(page);
1604 return vmf_insert_mixed(vmf->vma, vmf->address, page_to_pfn_t(page));
1605 }
1606
1607 static const struct vm_operations_struct msc_mmap_ops = {
1608 .open = msc_mmap_open,
1609 .close = msc_mmap_close,
1610 .fault = msc_mmap_fault,
1611 };
1612
intel_th_msc_mmap(struct file * file,struct vm_area_struct * vma)1613 static int intel_th_msc_mmap(struct file *file, struct vm_area_struct *vma)
1614 {
1615 unsigned long size = vma->vm_end - vma->vm_start;
1616 struct msc_iter *iter = vma->vm_file->private_data;
1617 struct msc *msc = iter->msc;
1618 int ret = -EINVAL;
1619
1620 if (!size || offset_in_page(size))
1621 return -EINVAL;
1622
1623 if (vma->vm_pgoff)
1624 return -EINVAL;
1625
1626 /* grab user_count once per mmap; drop in msc_mmap_close() */
1627 if (!atomic_inc_unless_negative(&msc->user_count))
1628 return -EINVAL;
1629
1630 if (msc->mode != MSC_MODE_SINGLE &&
1631 msc->mode != MSC_MODE_MULTI)
1632 goto out;
1633
1634 if (size >> PAGE_SHIFT != msc->nr_pages)
1635 goto out;
1636
1637 atomic_set(&msc->mmap_count, 1);
1638 ret = 0;
1639
1640 out:
1641 if (ret)
1642 atomic_dec(&msc->user_count);
1643
1644 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1645 vm_flags_set(vma, VM_DONTEXPAND | VM_DONTCOPY | VM_MIXEDMAP);
1646 vma->vm_ops = &msc_mmap_ops;
1647 return ret;
1648 }
1649
1650 static const struct file_operations intel_th_msc_fops = {
1651 .open = intel_th_msc_open,
1652 .release = intel_th_msc_release,
1653 .read = intel_th_msc_read,
1654 .mmap = intel_th_msc_mmap,
1655 .llseek = no_llseek,
1656 .owner = THIS_MODULE,
1657 };
1658
intel_th_msc_wait_empty(struct intel_th_device * thdev)1659 static void intel_th_msc_wait_empty(struct intel_th_device *thdev)
1660 {
1661 struct msc *msc = dev_get_drvdata(&thdev->dev);
1662 unsigned long count;
1663 u32 reg;
1664
1665 for (reg = 0, count = MSC_PLE_WAITLOOP_DEPTH;
1666 count && !(reg & MSCSTS_PLE); count--) {
1667 reg = __raw_readl(msc->reg_base + REG_MSU_MSC0STS);
1668 cpu_relax();
1669 }
1670
1671 if (!count)
1672 dev_dbg(msc_dev(msc), "timeout waiting for MSC0 PLE\n");
1673 }
1674
intel_th_msc_init(struct msc * msc)1675 static int intel_th_msc_init(struct msc *msc)
1676 {
1677 atomic_set(&msc->user_count, -1);
1678
1679 msc->mode = msc->multi_is_broken ? MSC_MODE_SINGLE : MSC_MODE_MULTI;
1680 mutex_init(&msc->buf_mutex);
1681 INIT_LIST_HEAD(&msc->win_list);
1682 INIT_LIST_HEAD(&msc->iter_list);
1683
1684 msc->burst_len =
1685 (ioread32(msc->reg_base + REG_MSU_MSC0CTL) & MSC_LEN) >>
1686 __ffs(MSC_LEN);
1687
1688 return 0;
1689 }
1690
msc_win_switch(struct msc * msc)1691 static int msc_win_switch(struct msc *msc)
1692 {
1693 struct msc_window *first;
1694
1695 if (list_empty(&msc->win_list))
1696 return -EINVAL;
1697
1698 first = list_first_entry(&msc->win_list, struct msc_window, entry);
1699
1700 if (msc_is_last_win(msc->cur_win))
1701 msc->cur_win = first;
1702 else
1703 msc->cur_win = list_next_entry(msc->cur_win, entry);
1704
1705 msc->base = msc_win_base(msc->cur_win);
1706 msc->base_addr = msc_win_base_dma(msc->cur_win);
1707
1708 intel_th_trace_switch(msc->thdev);
1709
1710 return 0;
1711 }
1712
1713 /**
1714 * intel_th_msc_window_unlock - put the window back in rotation
1715 * @dev: MSC device to which this relates
1716 * @sgt: buffer's sg_table for the window, does nothing if NULL
1717 */
intel_th_msc_window_unlock(struct device * dev,struct sg_table * sgt)1718 void intel_th_msc_window_unlock(struct device *dev, struct sg_table *sgt)
1719 {
1720 struct msc *msc = dev_get_drvdata(dev);
1721 struct msc_window *win;
1722
1723 if (!sgt)
1724 return;
1725
1726 win = msc_find_window(msc, sgt, false);
1727 if (!win)
1728 return;
1729
1730 msc_win_set_lockout(win, WIN_LOCKED, WIN_READY);
1731 if (msc->switch_on_unlock == win) {
1732 msc->switch_on_unlock = NULL;
1733 msc_win_switch(msc);
1734 }
1735 }
1736 EXPORT_SYMBOL_GPL(intel_th_msc_window_unlock);
1737
msc_work(struct work_struct * work)1738 static void msc_work(struct work_struct *work)
1739 {
1740 struct msc *msc = container_of(work, struct msc, work);
1741
1742 intel_th_msc_deactivate(msc->thdev);
1743 }
1744
intel_th_msc_interrupt(struct intel_th_device * thdev)1745 static irqreturn_t intel_th_msc_interrupt(struct intel_th_device *thdev)
1746 {
1747 struct msc *msc = dev_get_drvdata(&thdev->dev);
1748 u32 msusts = ioread32(msc->msu_base + REG_MSU_MSUSTS);
1749 u32 mask = msc->index ? MSUSTS_MSC1BLAST : MSUSTS_MSC0BLAST;
1750 struct msc_window *win, *next_win;
1751
1752 if (!msc->do_irq || !msc->mbuf)
1753 return IRQ_NONE;
1754
1755 msusts &= mask;
1756
1757 if (!msusts)
1758 return msc->enabled ? IRQ_HANDLED : IRQ_NONE;
1759
1760 iowrite32(msusts, msc->msu_base + REG_MSU_MSUSTS);
1761
1762 if (!msc->enabled)
1763 return IRQ_NONE;
1764
1765 /* grab the window before we do the switch */
1766 win = msc->cur_win;
1767 if (!win)
1768 return IRQ_HANDLED;
1769 next_win = msc_next_window(win);
1770 if (!next_win)
1771 return IRQ_HANDLED;
1772
1773 /* next window: if READY, proceed, if LOCKED, stop the trace */
1774 if (msc_win_set_lockout(next_win, WIN_READY, WIN_INUSE)) {
1775 if (msc->stop_on_full)
1776 schedule_work(&msc->work);
1777 else
1778 msc->switch_on_unlock = next_win;
1779
1780 return IRQ_HANDLED;
1781 }
1782
1783 /* current window: INUSE -> LOCKED */
1784 msc_win_set_lockout(win, WIN_INUSE, WIN_LOCKED);
1785
1786 msc_win_switch(msc);
1787
1788 if (msc->mbuf && msc->mbuf->ready)
1789 msc->mbuf->ready(msc->mbuf_priv, win->sgt,
1790 msc_win_total_sz(win));
1791
1792 return IRQ_HANDLED;
1793 }
1794
1795 static const char * const msc_mode[] = {
1796 [MSC_MODE_SINGLE] = "single",
1797 [MSC_MODE_MULTI] = "multi",
1798 [MSC_MODE_EXI] = "ExI",
1799 [MSC_MODE_DEBUG] = "debug",
1800 };
1801
1802 static ssize_t
wrap_show(struct device * dev,struct device_attribute * attr,char * buf)1803 wrap_show(struct device *dev, struct device_attribute *attr, char *buf)
1804 {
1805 struct msc *msc = dev_get_drvdata(dev);
1806
1807 return scnprintf(buf, PAGE_SIZE, "%d\n", msc->wrap);
1808 }
1809
1810 static ssize_t
wrap_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1811 wrap_store(struct device *dev, struct device_attribute *attr, const char *buf,
1812 size_t size)
1813 {
1814 struct msc *msc = dev_get_drvdata(dev);
1815 unsigned long val;
1816 int ret;
1817
1818 ret = kstrtoul(buf, 10, &val);
1819 if (ret)
1820 return ret;
1821
1822 msc->wrap = !!val;
1823
1824 return size;
1825 }
1826
1827 static DEVICE_ATTR_RW(wrap);
1828
msc_buffer_unassign(struct msc * msc)1829 static void msc_buffer_unassign(struct msc *msc)
1830 {
1831 lockdep_assert_held(&msc->buf_mutex);
1832
1833 if (!msc->mbuf)
1834 return;
1835
1836 msc->mbuf->unassign(msc->mbuf_priv);
1837 msu_buffer_put(msc->mbuf);
1838 msc->mbuf_priv = NULL;
1839 msc->mbuf = NULL;
1840 }
1841
1842 static ssize_t
mode_show(struct device * dev,struct device_attribute * attr,char * buf)1843 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1844 {
1845 struct msc *msc = dev_get_drvdata(dev);
1846 const char *mode = msc_mode[msc->mode];
1847 ssize_t ret;
1848
1849 mutex_lock(&msc->buf_mutex);
1850 if (msc->mbuf)
1851 mode = msc->mbuf->name;
1852 ret = scnprintf(buf, PAGE_SIZE, "%s\n", mode);
1853 mutex_unlock(&msc->buf_mutex);
1854
1855 return ret;
1856 }
1857
1858 static ssize_t
mode_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1859 mode_store(struct device *dev, struct device_attribute *attr, const char *buf,
1860 size_t size)
1861 {
1862 const struct msu_buffer *mbuf = NULL;
1863 struct msc *msc = dev_get_drvdata(dev);
1864 size_t len = size;
1865 char *cp, *mode;
1866 int i, ret;
1867
1868 if (!capable(CAP_SYS_RAWIO))
1869 return -EPERM;
1870
1871 cp = memchr(buf, '\n', len);
1872 if (cp)
1873 len = cp - buf;
1874
1875 mode = kstrndup(buf, len, GFP_KERNEL);
1876 if (!mode)
1877 return -ENOMEM;
1878
1879 i = match_string(msc_mode, ARRAY_SIZE(msc_mode), mode);
1880 if (i >= 0) {
1881 kfree(mode);
1882 goto found;
1883 }
1884
1885 /* Buffer sinks only work with a usable IRQ */
1886 if (!msc->do_irq) {
1887 kfree(mode);
1888 return -EINVAL;
1889 }
1890
1891 mbuf = msu_buffer_get(mode);
1892 kfree(mode);
1893 if (mbuf)
1894 goto found;
1895
1896 return -EINVAL;
1897
1898 found:
1899 if (i == MSC_MODE_MULTI && msc->multi_is_broken)
1900 return -EOPNOTSUPP;
1901
1902 mutex_lock(&msc->buf_mutex);
1903 ret = 0;
1904
1905 /* Same buffer: do nothing */
1906 if (mbuf && mbuf == msc->mbuf) {
1907 /* put the extra reference we just got */
1908 msu_buffer_put(mbuf);
1909 goto unlock;
1910 }
1911
1912 ret = msc_buffer_unlocked_free_unless_used(msc);
1913 if (ret)
1914 goto unlock;
1915
1916 if (mbuf) {
1917 void *mbuf_priv = mbuf->assign(dev, &i);
1918
1919 if (!mbuf_priv) {
1920 ret = -ENOMEM;
1921 goto unlock;
1922 }
1923
1924 msc_buffer_unassign(msc);
1925 msc->mbuf_priv = mbuf_priv;
1926 msc->mbuf = mbuf;
1927 } else {
1928 msc_buffer_unassign(msc);
1929 }
1930
1931 msc->mode = i;
1932
1933 unlock:
1934 if (ret && mbuf)
1935 msu_buffer_put(mbuf);
1936 mutex_unlock(&msc->buf_mutex);
1937
1938 return ret ? ret : size;
1939 }
1940
1941 static DEVICE_ATTR_RW(mode);
1942
1943 static ssize_t
nr_pages_show(struct device * dev,struct device_attribute * attr,char * buf)1944 nr_pages_show(struct device *dev, struct device_attribute *attr, char *buf)
1945 {
1946 struct msc *msc = dev_get_drvdata(dev);
1947 struct msc_window *win;
1948 size_t count = 0;
1949
1950 mutex_lock(&msc->buf_mutex);
1951
1952 if (msc->mode == MSC_MODE_SINGLE)
1953 count = scnprintf(buf, PAGE_SIZE, "%ld\n", msc->nr_pages);
1954 else if (msc->mode == MSC_MODE_MULTI) {
1955 list_for_each_entry(win, &msc->win_list, entry) {
1956 count += scnprintf(buf + count, PAGE_SIZE - count,
1957 "%d%c", win->nr_blocks,
1958 msc_is_last_win(win) ? '\n' : ',');
1959 }
1960 } else {
1961 count = scnprintf(buf, PAGE_SIZE, "unsupported\n");
1962 }
1963
1964 mutex_unlock(&msc->buf_mutex);
1965
1966 return count;
1967 }
1968
1969 static ssize_t
nr_pages_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1970 nr_pages_store(struct device *dev, struct device_attribute *attr,
1971 const char *buf, size_t size)
1972 {
1973 struct msc *msc = dev_get_drvdata(dev);
1974 unsigned long val, *win = NULL, *rewin;
1975 size_t len = size;
1976 const char *p = buf;
1977 char *end, *s;
1978 int ret, nr_wins = 0;
1979
1980 if (!capable(CAP_SYS_RAWIO))
1981 return -EPERM;
1982
1983 ret = msc_buffer_free_unless_used(msc);
1984 if (ret)
1985 return ret;
1986
1987 /* scan the comma-separated list of allocation sizes */
1988 end = memchr(buf, '\n', len);
1989 if (end)
1990 len = end - buf;
1991
1992 do {
1993 end = memchr(p, ',', len);
1994 s = kstrndup(p, end ? end - p : len, GFP_KERNEL);
1995 if (!s) {
1996 ret = -ENOMEM;
1997 goto free_win;
1998 }
1999
2000 ret = kstrtoul(s, 10, &val);
2001 kfree(s);
2002
2003 if (ret || !val)
2004 goto free_win;
2005
2006 if (nr_wins && msc->mode == MSC_MODE_SINGLE) {
2007 ret = -EINVAL;
2008 goto free_win;
2009 }
2010
2011 nr_wins++;
2012 rewin = krealloc_array(win, nr_wins, sizeof(*win), GFP_KERNEL);
2013 if (!rewin) {
2014 kfree(win);
2015 return -ENOMEM;
2016 }
2017
2018 win = rewin;
2019 win[nr_wins - 1] = val;
2020
2021 if (!end)
2022 break;
2023
2024 /* consume the number and the following comma, hence +1 */
2025 len -= end - p + 1;
2026 p = end + 1;
2027 } while (len);
2028
2029 mutex_lock(&msc->buf_mutex);
2030 ret = msc_buffer_alloc(msc, win, nr_wins);
2031 mutex_unlock(&msc->buf_mutex);
2032
2033 free_win:
2034 kfree(win);
2035
2036 return ret ? ret : size;
2037 }
2038
2039 static DEVICE_ATTR_RW(nr_pages);
2040
2041 static ssize_t
win_switch_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)2042 win_switch_store(struct device *dev, struct device_attribute *attr,
2043 const char *buf, size_t size)
2044 {
2045 struct msc *msc = dev_get_drvdata(dev);
2046 unsigned long val;
2047 int ret;
2048
2049 ret = kstrtoul(buf, 10, &val);
2050 if (ret)
2051 return ret;
2052
2053 if (val != 1)
2054 return -EINVAL;
2055
2056 ret = -EINVAL;
2057 mutex_lock(&msc->buf_mutex);
2058 /*
2059 * Window switch can only happen in the "multi" mode.
2060 * If a external buffer is engaged, they have the full
2061 * control over window switching.
2062 */
2063 if (msc->mode == MSC_MODE_MULTI && !msc->mbuf)
2064 ret = msc_win_switch(msc);
2065 mutex_unlock(&msc->buf_mutex);
2066
2067 return ret ? ret : size;
2068 }
2069
2070 static DEVICE_ATTR_WO(win_switch);
2071
stop_on_full_show(struct device * dev,struct device_attribute * attr,char * buf)2072 static ssize_t stop_on_full_show(struct device *dev,
2073 struct device_attribute *attr, char *buf)
2074 {
2075 struct msc *msc = dev_get_drvdata(dev);
2076
2077 return sprintf(buf, "%d\n", msc->stop_on_full);
2078 }
2079
stop_on_full_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)2080 static ssize_t stop_on_full_store(struct device *dev,
2081 struct device_attribute *attr,
2082 const char *buf, size_t size)
2083 {
2084 struct msc *msc = dev_get_drvdata(dev);
2085 int ret;
2086
2087 ret = kstrtobool(buf, &msc->stop_on_full);
2088 if (ret)
2089 return ret;
2090
2091 return size;
2092 }
2093
2094 static DEVICE_ATTR_RW(stop_on_full);
2095
2096 static struct attribute *msc_output_attrs[] = {
2097 &dev_attr_wrap.attr,
2098 &dev_attr_mode.attr,
2099 &dev_attr_nr_pages.attr,
2100 &dev_attr_win_switch.attr,
2101 &dev_attr_stop_on_full.attr,
2102 NULL,
2103 };
2104
2105 static const struct attribute_group msc_output_group = {
2106 .attrs = msc_output_attrs,
2107 };
2108
intel_th_msc_probe(struct intel_th_device * thdev)2109 static int intel_th_msc_probe(struct intel_th_device *thdev)
2110 {
2111 struct device *dev = &thdev->dev;
2112 struct resource *res;
2113 struct msc *msc;
2114 void __iomem *base;
2115 int err;
2116
2117 res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
2118 if (!res)
2119 return -ENODEV;
2120
2121 base = devm_ioremap(dev, res->start, resource_size(res));
2122 if (!base)
2123 return -ENOMEM;
2124
2125 msc = devm_kzalloc(dev, sizeof(*msc), GFP_KERNEL);
2126 if (!msc)
2127 return -ENOMEM;
2128
2129 res = intel_th_device_get_resource(thdev, IORESOURCE_IRQ, 1);
2130 if (!res)
2131 msc->do_irq = 1;
2132
2133 if (INTEL_TH_CAP(to_intel_th(thdev), multi_is_broken))
2134 msc->multi_is_broken = 1;
2135
2136 msc->index = thdev->id;
2137
2138 msc->thdev = thdev;
2139 msc->reg_base = base + msc->index * 0x100;
2140 msc->msu_base = base;
2141
2142 INIT_WORK(&msc->work, msc_work);
2143 err = intel_th_msc_init(msc);
2144 if (err)
2145 return err;
2146
2147 dev_set_drvdata(dev, msc);
2148
2149 return 0;
2150 }
2151
intel_th_msc_remove(struct intel_th_device * thdev)2152 static void intel_th_msc_remove(struct intel_th_device *thdev)
2153 {
2154 struct msc *msc = dev_get_drvdata(&thdev->dev);
2155 int ret;
2156
2157 intel_th_msc_deactivate(thdev);
2158
2159 /*
2160 * Buffers should not be used at this point except if the
2161 * output character device is still open and the parent
2162 * device gets detached from its bus, which is a FIXME.
2163 */
2164 ret = msc_buffer_free_unless_used(msc);
2165 WARN_ON_ONCE(ret);
2166 }
2167
2168 static struct intel_th_driver intel_th_msc_driver = {
2169 .probe = intel_th_msc_probe,
2170 .remove = intel_th_msc_remove,
2171 .irq = intel_th_msc_interrupt,
2172 .wait_empty = intel_th_msc_wait_empty,
2173 .activate = intel_th_msc_activate,
2174 .deactivate = intel_th_msc_deactivate,
2175 .fops = &intel_th_msc_fops,
2176 .attr_group = &msc_output_group,
2177 .driver = {
2178 .name = "msc",
2179 .owner = THIS_MODULE,
2180 },
2181 };
2182
2183 module_driver(intel_th_msc_driver,
2184 intel_th_driver_register,
2185 intel_th_driver_unregister);
2186
2187 MODULE_LICENSE("GPL v2");
2188 MODULE_DESCRIPTION("Intel(R) Trace Hub Memory Storage Unit driver");
2189 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");
2190