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 * @msc: pointer to the MSC device
66 * @_sgt: array of block descriptors
67 * @sgt: array of block descriptors
68 */
69 struct msc_window {
70 struct list_head entry;
71 unsigned long pgoff;
72 enum lockout_state lockout;
73 spinlock_t lo_lock;
74 unsigned int nr_blocks;
75 unsigned int nr_segs;
76 struct msc *msc;
77 struct sg_table _sgt;
78 struct sg_table *sgt;
79 };
80
81 /**
82 * struct msc_iter - iterator for msc buffer
83 * @entry: msc::iter_list linkage
84 * @msc: pointer to the MSC device
85 * @start_win: oldest window
86 * @win: current window
87 * @offset: current logical offset into the buffer
88 * @start_block: oldest block in the window
89 * @block: block number in the window
90 * @block_off: offset into current block
91 * @wrap_count: block wrapping handling
92 * @eof: end of buffer reached
93 */
94 struct msc_iter {
95 struct list_head entry;
96 struct msc *msc;
97 struct msc_window *start_win;
98 struct msc_window *win;
99 unsigned long offset;
100 struct scatterlist *start_block;
101 struct scatterlist *block;
102 unsigned int block_off;
103 unsigned int wrap_count;
104 unsigned int eof;
105 };
106
107 /**
108 * struct msc - MSC device representation
109 * @reg_base: register window base address
110 * @thdev: intel_th_device pointer
111 * @mbuf: MSU buffer, if assigned
112 * @mbuf_priv MSU buffer's private data, if @mbuf
113 * @win_list: list of windows in multiblock mode
114 * @single_sgt: single mode buffer
115 * @cur_win: current window
116 * @nr_pages: total number of pages allocated for this buffer
117 * @single_sz: amount of data in single mode
118 * @single_wrap: single mode wrap occurred
119 * @base: buffer's base pointer
120 * @base_addr: buffer's base address
121 * @user_count: number of users of the buffer
122 * @mmap_count: number of mappings
123 * @buf_mutex: mutex to serialize access to buffer-related bits
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 *
760 * Return: %0 for success or a negative error code otherwise.
761 */
msc_configure(struct msc * msc)762 static int msc_configure(struct msc *msc)
763 {
764 u32 reg;
765
766 lockdep_assert_held(&msc->buf_mutex);
767
768 if (msc->mode > MSC_MODE_MULTI)
769 return -EINVAL;
770
771 if (msc->mode == MSC_MODE_MULTI) {
772 if (msc_win_set_lockout(msc->cur_win, WIN_READY, WIN_INUSE))
773 return -EBUSY;
774
775 msc_buffer_clear_hw_header(msc);
776 }
777
778 msc->orig_addr = ioread32(msc->reg_base + REG_MSU_MSC0BAR);
779 msc->orig_sz = ioread32(msc->reg_base + REG_MSU_MSC0SIZE);
780
781 reg = msc->base_addr >> PAGE_SHIFT;
782 iowrite32(reg, msc->reg_base + REG_MSU_MSC0BAR);
783
784 if (msc->mode == MSC_MODE_SINGLE) {
785 reg = msc->nr_pages;
786 iowrite32(reg, msc->reg_base + REG_MSU_MSC0SIZE);
787 }
788
789 reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
790 reg &= ~(MSC_MODE | MSC_WRAPEN | MSC_EN | MSC_RD_HDR_OVRD);
791
792 reg |= MSC_EN;
793 reg |= msc->mode << __ffs(MSC_MODE);
794 reg |= msc->burst_len << __ffs(MSC_LEN);
795
796 if (msc->wrap)
797 reg |= MSC_WRAPEN;
798
799 iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
800
801 intel_th_msu_init(msc);
802
803 msc->thdev->output.multiblock = msc->mode == MSC_MODE_MULTI;
804 intel_th_trace_enable(msc->thdev);
805 msc->enabled = 1;
806
807 if (msc->mbuf && msc->mbuf->activate)
808 msc->mbuf->activate(msc->mbuf_priv);
809
810 return 0;
811 }
812
813 /**
814 * msc_disable() - disable MSC hardware
815 * @msc: MSC device to disable
816 *
817 * If @msc is enabled, disable tracing on the switch and then disable MSC
818 * storage. Caller must hold msc::buf_mutex.
819 */
msc_disable(struct msc * msc)820 static void msc_disable(struct msc *msc)
821 {
822 struct msc_window *win = msc->cur_win;
823 u32 reg;
824
825 lockdep_assert_held(&msc->buf_mutex);
826
827 if (msc->mode == MSC_MODE_MULTI)
828 msc_win_set_lockout(win, WIN_INUSE, WIN_LOCKED);
829
830 if (msc->mbuf && msc->mbuf->deactivate)
831 msc->mbuf->deactivate(msc->mbuf_priv);
832 intel_th_msu_deinit(msc);
833 intel_th_trace_disable(msc->thdev);
834
835 if (msc->mode == MSC_MODE_SINGLE) {
836 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
837 msc->single_wrap = !!(reg & MSCSTS_WRAPSTAT);
838
839 reg = ioread32(msc->reg_base + REG_MSU_MSC0MWP);
840 msc->single_sz = reg & ((msc->nr_pages << PAGE_SHIFT) - 1);
841 dev_dbg(msc_dev(msc), "MSCnMWP: %08x/%08lx, wrap: %d\n",
842 reg, msc->single_sz, msc->single_wrap);
843 }
844
845 reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
846 reg &= ~MSC_EN;
847 iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
848
849 if (msc->mbuf && msc->mbuf->ready)
850 msc->mbuf->ready(msc->mbuf_priv, win->sgt,
851 msc_win_total_sz(win));
852
853 msc->enabled = 0;
854
855 iowrite32(msc->orig_addr, msc->reg_base + REG_MSU_MSC0BAR);
856 iowrite32(msc->orig_sz, msc->reg_base + REG_MSU_MSC0SIZE);
857
858 dev_dbg(msc_dev(msc), "MSCnNWSA: %08x\n",
859 ioread32(msc->reg_base + REG_MSU_MSC0NWSA));
860
861 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
862 dev_dbg(msc_dev(msc), "MSCnSTS: %08x\n", reg);
863
864 reg = ioread32(msc->reg_base + REG_MSU_MSUSTS);
865 reg &= msc->index ? MSUSTS_MSC1BLAST : MSUSTS_MSC0BLAST;
866 iowrite32(reg, msc->reg_base + REG_MSU_MSUSTS);
867 }
868
intel_th_msc_activate(struct intel_th_device * thdev)869 static int intel_th_msc_activate(struct intel_th_device *thdev)
870 {
871 struct msc *msc = dev_get_drvdata(&thdev->dev);
872 int ret = -EBUSY;
873
874 if (!atomic_inc_unless_negative(&msc->user_count))
875 return -ENODEV;
876
877 mutex_lock(&msc->buf_mutex);
878
879 /* if there are readers, refuse */
880 if (list_empty(&msc->iter_list))
881 ret = msc_configure(msc);
882
883 mutex_unlock(&msc->buf_mutex);
884
885 if (ret)
886 atomic_dec(&msc->user_count);
887
888 return ret;
889 }
890
intel_th_msc_deactivate(struct intel_th_device * thdev)891 static void intel_th_msc_deactivate(struct intel_th_device *thdev)
892 {
893 struct msc *msc = dev_get_drvdata(&thdev->dev);
894
895 mutex_lock(&msc->buf_mutex);
896 if (msc->enabled) {
897 msc_disable(msc);
898 atomic_dec(&msc->user_count);
899 }
900 mutex_unlock(&msc->buf_mutex);
901 }
902
903 /**
904 * msc_buffer_contig_alloc() - allocate a contiguous buffer for SINGLE mode
905 * @msc: MSC device
906 * @size: allocation size in bytes
907 *
908 * This modifies msc::base, which requires msc::buf_mutex to serialize, so the
909 * caller is expected to hold it.
910 *
911 * Return: 0 on success, -errno otherwise.
912 */
msc_buffer_contig_alloc(struct msc * msc,unsigned long size)913 static int msc_buffer_contig_alloc(struct msc *msc, unsigned long size)
914 {
915 unsigned long nr_pages = size >> PAGE_SHIFT;
916 unsigned int order = get_order(size);
917 struct page *page;
918 int ret;
919
920 if (!size)
921 return 0;
922
923 ret = sg_alloc_table(&msc->single_sgt, 1, GFP_KERNEL);
924 if (ret)
925 goto err_out;
926
927 ret = -ENOMEM;
928 page = alloc_pages(GFP_KERNEL | __GFP_ZERO | GFP_DMA32, order);
929 if (!page)
930 goto err_free_sgt;
931
932 split_page(page, order);
933 sg_set_buf(msc->single_sgt.sgl, page_address(page), size);
934
935 ret = dma_map_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl, 1,
936 DMA_FROM_DEVICE);
937 if (ret < 0)
938 goto err_free_pages;
939
940 msc->nr_pages = nr_pages;
941 msc->base = page_address(page);
942 msc->base_addr = sg_dma_address(msc->single_sgt.sgl);
943
944 return 0;
945
946 err_free_pages:
947 __free_pages(page, order);
948
949 err_free_sgt:
950 sg_free_table(&msc->single_sgt);
951
952 err_out:
953 return ret;
954 }
955
956 /**
957 * msc_buffer_contig_free() - free a contiguous buffer
958 * @msc: MSC configured in SINGLE mode
959 */
msc_buffer_contig_free(struct msc * msc)960 static void msc_buffer_contig_free(struct msc *msc)
961 {
962 unsigned long off;
963
964 dma_unmap_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl,
965 1, DMA_FROM_DEVICE);
966 sg_free_table(&msc->single_sgt);
967
968 for (off = 0; off < msc->nr_pages << PAGE_SHIFT; off += PAGE_SIZE) {
969 struct page *page = virt_to_page(msc->base + off);
970
971 __free_page(page);
972 }
973
974 msc->nr_pages = 0;
975 }
976
977 /**
978 * msc_buffer_contig_get_page() - find a page at a given offset
979 * @msc: MSC configured in SINGLE mode
980 * @pgoff: page offset
981 *
982 * Return: page, if @pgoff is within the range, NULL otherwise.
983 */
msc_buffer_contig_get_page(struct msc * msc,unsigned long pgoff)984 static struct page *msc_buffer_contig_get_page(struct msc *msc,
985 unsigned long pgoff)
986 {
987 if (pgoff >= msc->nr_pages)
988 return NULL;
989
990 return virt_to_page(msc->base + (pgoff << PAGE_SHIFT));
991 }
992
__msc_buffer_win_alloc(struct msc_window * win,unsigned int nr_segs)993 static int __msc_buffer_win_alloc(struct msc_window *win,
994 unsigned int nr_segs)
995 {
996 struct scatterlist *sg_ptr;
997 void *block;
998 int i, ret;
999
1000 ret = sg_alloc_table(win->sgt, nr_segs, GFP_KERNEL);
1001 if (ret)
1002 return -ENOMEM;
1003
1004 for_each_sg(win->sgt->sgl, sg_ptr, nr_segs, i) {
1005 block = dma_alloc_coherent(msc_dev(win->msc)->parent->parent,
1006 PAGE_SIZE, &sg_dma_address(sg_ptr),
1007 GFP_KERNEL);
1008 if (!block)
1009 goto err_nomem;
1010
1011 sg_set_buf(sg_ptr, block, PAGE_SIZE);
1012 }
1013
1014 return nr_segs;
1015
1016 err_nomem:
1017 for_each_sg(win->sgt->sgl, sg_ptr, i, ret)
1018 dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
1019 sg_virt(sg_ptr), sg_dma_address(sg_ptr));
1020
1021 sg_free_table(win->sgt);
1022
1023 return -ENOMEM;
1024 }
1025
1026 #ifdef CONFIG_X86
msc_buffer_set_uc(struct msc * msc)1027 static void msc_buffer_set_uc(struct msc *msc)
1028 {
1029 struct scatterlist *sg_ptr;
1030 struct msc_window *win;
1031 int i;
1032
1033 if (msc->mode == MSC_MODE_SINGLE) {
1034 set_memory_uc((unsigned long)msc->base, msc->nr_pages);
1035 return;
1036 }
1037
1038 list_for_each_entry(win, &msc->win_list, entry) {
1039 for_each_sg(win->sgt->sgl, sg_ptr, win->nr_segs, i) {
1040 /* Set the page as uncached */
1041 set_memory_uc((unsigned long)sg_virt(sg_ptr),
1042 PFN_DOWN(sg_ptr->length));
1043 }
1044 }
1045 }
1046
msc_buffer_set_wb(struct msc * msc)1047 static void msc_buffer_set_wb(struct msc *msc)
1048 {
1049 struct scatterlist *sg_ptr;
1050 struct msc_window *win;
1051 int i;
1052
1053 if (msc->mode == MSC_MODE_SINGLE) {
1054 set_memory_wb((unsigned long)msc->base, msc->nr_pages);
1055 return;
1056 }
1057
1058 list_for_each_entry(win, &msc->win_list, entry) {
1059 for_each_sg(win->sgt->sgl, sg_ptr, win->nr_segs, i) {
1060 /* Reset the page to write-back */
1061 set_memory_wb((unsigned long)sg_virt(sg_ptr),
1062 PFN_DOWN(sg_ptr->length));
1063 }
1064 }
1065 }
1066 #else /* !X86 */
1067 static inline void
msc_buffer_set_uc(struct msc * msc)1068 msc_buffer_set_uc(struct msc *msc) {}
msc_buffer_set_wb(struct msc * msc)1069 static inline void msc_buffer_set_wb(struct msc *msc) {}
1070 #endif /* CONFIG_X86 */
1071
msc_sg_page(struct scatterlist * sg)1072 static struct page *msc_sg_page(struct scatterlist *sg)
1073 {
1074 void *addr = sg_virt(sg);
1075
1076 if (is_vmalloc_addr(addr))
1077 return vmalloc_to_page(addr);
1078
1079 return sg_page(sg);
1080 }
1081
1082 /**
1083 * msc_buffer_win_alloc() - alloc a window for a multiblock mode
1084 * @msc: MSC device
1085 * @nr_blocks: number of pages in this window
1086 *
1087 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
1088 * to serialize, so the caller is expected to hold it.
1089 *
1090 * Return: 0 on success, -errno otherwise.
1091 */
msc_buffer_win_alloc(struct msc * msc,unsigned int nr_blocks)1092 static int msc_buffer_win_alloc(struct msc *msc, unsigned int nr_blocks)
1093 {
1094 struct msc_window *win;
1095 int ret = -ENOMEM;
1096
1097 if (!nr_blocks)
1098 return 0;
1099
1100 win = kzalloc(sizeof(*win), GFP_KERNEL);
1101 if (!win)
1102 return -ENOMEM;
1103
1104 win->msc = msc;
1105 win->sgt = &win->_sgt;
1106 win->lockout = WIN_READY;
1107 spin_lock_init(&win->lo_lock);
1108
1109 if (!list_empty(&msc->win_list)) {
1110 struct msc_window *prev = list_last_entry(&msc->win_list,
1111 struct msc_window,
1112 entry);
1113
1114 win->pgoff = prev->pgoff + prev->nr_blocks;
1115 }
1116
1117 if (msc->mbuf && msc->mbuf->alloc_window)
1118 ret = msc->mbuf->alloc_window(msc->mbuf_priv, &win->sgt,
1119 nr_blocks << PAGE_SHIFT);
1120 else
1121 ret = __msc_buffer_win_alloc(win, nr_blocks);
1122
1123 if (ret <= 0)
1124 goto err_nomem;
1125
1126 win->nr_segs = ret;
1127 win->nr_blocks = nr_blocks;
1128
1129 if (list_empty(&msc->win_list)) {
1130 msc->base = msc_win_base(win);
1131 msc->base_addr = msc_win_base_dma(win);
1132 msc->cur_win = win;
1133 }
1134
1135 list_add_tail(&win->entry, &msc->win_list);
1136 msc->nr_pages += nr_blocks;
1137
1138 return 0;
1139
1140 err_nomem:
1141 kfree(win);
1142
1143 return ret;
1144 }
1145
__msc_buffer_win_free(struct msc * msc,struct msc_window * win)1146 static void __msc_buffer_win_free(struct msc *msc, struct msc_window *win)
1147 {
1148 struct scatterlist *sg;
1149 int i;
1150
1151 for_each_sg(win->sgt->sgl, sg, win->nr_segs, i) {
1152 dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
1153 sg_virt(sg), sg_dma_address(sg));
1154 }
1155 sg_free_table(win->sgt);
1156 }
1157
1158 /**
1159 * msc_buffer_win_free() - free a window from MSC's window list
1160 * @msc: MSC device
1161 * @win: window to free
1162 *
1163 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
1164 * to serialize, so the caller is expected to hold it.
1165 */
msc_buffer_win_free(struct msc * msc,struct msc_window * win)1166 static void msc_buffer_win_free(struct msc *msc, struct msc_window *win)
1167 {
1168 msc->nr_pages -= win->nr_blocks;
1169
1170 list_del(&win->entry);
1171 if (list_empty(&msc->win_list)) {
1172 msc->base = NULL;
1173 msc->base_addr = 0;
1174 }
1175
1176 if (msc->mbuf && msc->mbuf->free_window)
1177 msc->mbuf->free_window(msc->mbuf_priv, win->sgt);
1178 else
1179 __msc_buffer_win_free(msc, win);
1180
1181 kfree(win);
1182 }
1183
1184 /**
1185 * msc_buffer_relink() - set up block descriptors for multiblock mode
1186 * @msc: MSC device
1187 *
1188 * This traverses msc::win_list, which requires msc::buf_mutex to serialize,
1189 * so the caller is expected to hold it.
1190 */
msc_buffer_relink(struct msc * msc)1191 static void msc_buffer_relink(struct msc *msc)
1192 {
1193 struct msc_window *win, *next_win;
1194
1195 /* call with msc::mutex locked */
1196 list_for_each_entry(win, &msc->win_list, entry) {
1197 struct scatterlist *sg;
1198 unsigned int blk;
1199 u32 sw_tag = 0;
1200
1201 /*
1202 * Last window's next_win should point to the first window
1203 * and MSC_SW_TAG_LASTWIN should be set.
1204 */
1205 if (msc_is_last_win(win)) {
1206 sw_tag |= MSC_SW_TAG_LASTWIN;
1207 next_win = list_first_entry(&msc->win_list,
1208 struct msc_window, entry);
1209 } else {
1210 next_win = list_next_entry(win, entry);
1211 }
1212
1213 for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
1214 struct msc_block_desc *bdesc = sg_virt(sg);
1215
1216 memset(bdesc, 0, sizeof(*bdesc));
1217
1218 bdesc->next_win = msc_win_base_pfn(next_win);
1219
1220 /*
1221 * Similarly to last window, last block should point
1222 * to the first one.
1223 */
1224 if (blk == win->nr_segs - 1) {
1225 sw_tag |= MSC_SW_TAG_LASTBLK;
1226 bdesc->next_blk = msc_win_base_pfn(win);
1227 } else {
1228 dma_addr_t addr = sg_dma_address(sg_next(sg));
1229
1230 bdesc->next_blk = PFN_DOWN(addr);
1231 }
1232
1233 bdesc->sw_tag = sw_tag;
1234 bdesc->block_sz = sg->length / 64;
1235 }
1236 }
1237
1238 /*
1239 * Make the above writes globally visible before tracing is
1240 * enabled to make sure hardware sees them coherently.
1241 */
1242 wmb();
1243 }
1244
msc_buffer_multi_free(struct msc * msc)1245 static void msc_buffer_multi_free(struct msc *msc)
1246 {
1247 struct msc_window *win, *iter;
1248
1249 list_for_each_entry_safe(win, iter, &msc->win_list, entry)
1250 msc_buffer_win_free(msc, win);
1251 }
1252
msc_buffer_multi_alloc(struct msc * msc,unsigned long * nr_pages,unsigned int nr_wins)1253 static int msc_buffer_multi_alloc(struct msc *msc, unsigned long *nr_pages,
1254 unsigned int nr_wins)
1255 {
1256 int ret, i;
1257
1258 for (i = 0; i < nr_wins; i++) {
1259 ret = msc_buffer_win_alloc(msc, nr_pages[i]);
1260 if (ret) {
1261 msc_buffer_multi_free(msc);
1262 return ret;
1263 }
1264 }
1265
1266 msc_buffer_relink(msc);
1267
1268 return 0;
1269 }
1270
1271 /**
1272 * msc_buffer_free() - free buffers for MSC
1273 * @msc: MSC device
1274 *
1275 * Free MSC's storage buffers.
1276 *
1277 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex to
1278 * serialize, so the caller is expected to hold it.
1279 */
msc_buffer_free(struct msc * msc)1280 static void msc_buffer_free(struct msc *msc)
1281 {
1282 msc_buffer_set_wb(msc);
1283
1284 if (msc->mode == MSC_MODE_SINGLE)
1285 msc_buffer_contig_free(msc);
1286 else if (msc->mode == MSC_MODE_MULTI)
1287 msc_buffer_multi_free(msc);
1288 }
1289
1290 /**
1291 * msc_buffer_alloc() - allocate a buffer for MSC
1292 * @msc: MSC device
1293 * @nr_pages: number of pages for each window
1294 * @nr_wins: number of windows
1295 *
1296 * Allocate a storage buffer for MSC, depending on the msc::mode, it will be
1297 * either done via msc_buffer_contig_alloc() for SINGLE operation mode or
1298 * msc_buffer_win_alloc() for multiblock operation. The latter allocates one
1299 * window per invocation, so in multiblock mode this can be called multiple
1300 * times for the same MSC to allocate multiple windows.
1301 *
1302 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
1303 * to serialize, so the caller is expected to hold it.
1304 *
1305 * Return: 0 on success, -errno otherwise.
1306 */
msc_buffer_alloc(struct msc * msc,unsigned long * nr_pages,unsigned int nr_wins)1307 static int msc_buffer_alloc(struct msc *msc, unsigned long *nr_pages,
1308 unsigned int nr_wins)
1309 {
1310 int ret;
1311
1312 /* -1: buffer not allocated */
1313 if (atomic_read(&msc->user_count) != -1)
1314 return -EBUSY;
1315
1316 if (msc->mode == MSC_MODE_SINGLE) {
1317 if (nr_wins != 1)
1318 return -EINVAL;
1319
1320 ret = msc_buffer_contig_alloc(msc, nr_pages[0] << PAGE_SHIFT);
1321 } else if (msc->mode == MSC_MODE_MULTI) {
1322 ret = msc_buffer_multi_alloc(msc, nr_pages, nr_wins);
1323 } else {
1324 ret = -EINVAL;
1325 }
1326
1327 if (!ret) {
1328 msc_buffer_set_uc(msc);
1329
1330 /* allocation should be visible before the counter goes to 0 */
1331 smp_mb__before_atomic();
1332
1333 if (WARN_ON_ONCE(atomic_cmpxchg(&msc->user_count, -1, 0) != -1))
1334 return -EINVAL;
1335 }
1336
1337 return ret;
1338 }
1339
1340 /**
1341 * msc_buffer_unlocked_free_unless_used() - free a buffer unless it's in use
1342 * @msc: MSC device
1343 *
1344 * This will free MSC buffer unless it is in use or there is no allocated
1345 * buffer.
1346 * Caller needs to hold msc::buf_mutex.
1347 *
1348 * Return: 0 on successful deallocation or if there was no buffer to
1349 * deallocate, -EBUSY if there are active users.
1350 */
msc_buffer_unlocked_free_unless_used(struct msc * msc)1351 static int msc_buffer_unlocked_free_unless_used(struct msc *msc)
1352 {
1353 int count, ret = 0;
1354
1355 count = atomic_cmpxchg(&msc->user_count, 0, -1);
1356
1357 /* > 0: buffer is allocated and has users */
1358 if (count > 0)
1359 ret = -EBUSY;
1360 /* 0: buffer is allocated, no users */
1361 else if (!count)
1362 msc_buffer_free(msc);
1363 /* < 0: no buffer, nothing to do */
1364
1365 return ret;
1366 }
1367
1368 /**
1369 * msc_buffer_free_unless_used() - free a buffer unless it's in use
1370 * @msc: MSC device
1371 *
1372 * This is a locked version of msc_buffer_unlocked_free_unless_used().
1373 *
1374 * Return: 0 on successful deallocation or if there was no buffer to
1375 * deallocate, -EBUSY if there are active users.
1376 */
msc_buffer_free_unless_used(struct msc * msc)1377 static int msc_buffer_free_unless_used(struct msc *msc)
1378 {
1379 int ret;
1380
1381 mutex_lock(&msc->buf_mutex);
1382 ret = msc_buffer_unlocked_free_unless_used(msc);
1383 mutex_unlock(&msc->buf_mutex);
1384
1385 return ret;
1386 }
1387
1388 /**
1389 * msc_buffer_get_page() - get MSC buffer page at a given offset
1390 * @msc: MSC device
1391 * @pgoff: page offset into the storage buffer
1392 *
1393 * This traverses msc::win_list, so holding msc::buf_mutex is expected from
1394 * the caller.
1395 *
1396 * Return: page if @pgoff corresponds to a valid buffer page or NULL.
1397 */
msc_buffer_get_page(struct msc * msc,unsigned long pgoff)1398 static struct page *msc_buffer_get_page(struct msc *msc, unsigned long pgoff)
1399 {
1400 struct msc_window *win;
1401 struct scatterlist *sg;
1402 unsigned int blk;
1403
1404 if (msc->mode == MSC_MODE_SINGLE)
1405 return msc_buffer_contig_get_page(msc, pgoff);
1406
1407 list_for_each_entry(win, &msc->win_list, entry)
1408 if (pgoff >= win->pgoff && pgoff < win->pgoff + win->nr_blocks)
1409 goto found;
1410
1411 return NULL;
1412
1413 found:
1414 pgoff -= win->pgoff;
1415
1416 for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
1417 struct page *page = msc_sg_page(sg);
1418 size_t pgsz = PFN_DOWN(sg->length);
1419
1420 if (pgoff < pgsz)
1421 return page + pgoff;
1422
1423 pgoff -= pgsz;
1424 }
1425
1426 return NULL;
1427 }
1428
1429 /**
1430 * struct msc_win_to_user_struct - data for copy_to_user() callback
1431 * @buf: userspace buffer to copy data to
1432 * @offset: running offset
1433 */
1434 struct msc_win_to_user_struct {
1435 char __user *buf;
1436 unsigned long offset;
1437 };
1438
1439 /**
1440 * msc_win_to_user() - iterator for msc_buffer_iterate() to copy data to user
1441 * @data: callback's private data
1442 * @src: source buffer
1443 * @len: amount of data to copy from the source buffer
1444 *
1445 * Return: >= %0 for success or -errno for error.
1446 */
msc_win_to_user(void * data,void * src,size_t len)1447 static unsigned long msc_win_to_user(void *data, void *src, size_t len)
1448 {
1449 struct msc_win_to_user_struct *u = data;
1450 unsigned long ret;
1451
1452 ret = copy_to_user(u->buf + u->offset, src, len);
1453 u->offset += len - ret;
1454
1455 return ret;
1456 }
1457
1458
1459 /*
1460 * file operations' callbacks
1461 */
1462
intel_th_msc_open(struct inode * inode,struct file * file)1463 static int intel_th_msc_open(struct inode *inode, struct file *file)
1464 {
1465 struct intel_th_device *thdev = file->private_data;
1466 struct msc *msc = dev_get_drvdata(&thdev->dev);
1467 struct msc_iter *iter;
1468
1469 if (!capable(CAP_SYS_RAWIO))
1470 return -EPERM;
1471
1472 iter = msc_iter_install(msc);
1473 if (IS_ERR(iter))
1474 return PTR_ERR(iter);
1475
1476 file->private_data = iter;
1477
1478 return nonseekable_open(inode, file);
1479 }
1480
intel_th_msc_release(struct inode * inode,struct file * file)1481 static int intel_th_msc_release(struct inode *inode, struct file *file)
1482 {
1483 struct msc_iter *iter = file->private_data;
1484 struct msc *msc = iter->msc;
1485
1486 msc_iter_remove(iter, msc);
1487
1488 return 0;
1489 }
1490
1491 static ssize_t
msc_single_to_user(struct msc * msc,char __user * buf,loff_t off,size_t len)1492 msc_single_to_user(struct msc *msc, char __user *buf, loff_t off, size_t len)
1493 {
1494 unsigned long size = msc->nr_pages << PAGE_SHIFT, rem = len;
1495 unsigned long start = off, tocopy = 0;
1496
1497 if (msc->single_wrap) {
1498 start += msc->single_sz;
1499 if (start < size) {
1500 tocopy = min(rem, size - start);
1501 if (copy_to_user(buf, msc->base + start, tocopy))
1502 return -EFAULT;
1503
1504 buf += tocopy;
1505 rem -= tocopy;
1506 start += tocopy;
1507 }
1508
1509 start &= size - 1;
1510 if (rem) {
1511 tocopy = min(rem, msc->single_sz - start);
1512 if (copy_to_user(buf, msc->base + start, tocopy))
1513 return -EFAULT;
1514
1515 rem -= tocopy;
1516 }
1517
1518 return len - rem;
1519 }
1520
1521 if (copy_to_user(buf, msc->base + start, rem))
1522 return -EFAULT;
1523
1524 return len;
1525 }
1526
intel_th_msc_read(struct file * file,char __user * buf,size_t len,loff_t * ppos)1527 static ssize_t intel_th_msc_read(struct file *file, char __user *buf,
1528 size_t len, loff_t *ppos)
1529 {
1530 struct msc_iter *iter = file->private_data;
1531 struct msc *msc = iter->msc;
1532 size_t size;
1533 loff_t off = *ppos;
1534 ssize_t ret = 0;
1535
1536 if (!atomic_inc_unless_negative(&msc->user_count))
1537 return 0;
1538
1539 if (msc->mode == MSC_MODE_SINGLE && !msc->single_wrap)
1540 size = msc->single_sz;
1541 else
1542 size = msc->nr_pages << PAGE_SHIFT;
1543
1544 if (!size)
1545 goto put_count;
1546
1547 if (off >= size)
1548 goto put_count;
1549
1550 if (off + len >= size)
1551 len = size - off;
1552
1553 if (msc->mode == MSC_MODE_SINGLE) {
1554 ret = msc_single_to_user(msc, buf, off, len);
1555 if (ret >= 0)
1556 *ppos += ret;
1557 } else if (msc->mode == MSC_MODE_MULTI) {
1558 struct msc_win_to_user_struct u = {
1559 .buf = buf,
1560 .offset = 0,
1561 };
1562
1563 ret = msc_buffer_iterate(iter, len, &u, msc_win_to_user);
1564 if (ret >= 0)
1565 *ppos = iter->offset;
1566 } else {
1567 ret = -EINVAL;
1568 }
1569
1570 put_count:
1571 atomic_dec(&msc->user_count);
1572
1573 return ret;
1574 }
1575
1576 /*
1577 * vm operations callbacks (vm_ops)
1578 */
1579
msc_mmap_open(struct vm_area_struct * vma)1580 static void msc_mmap_open(struct vm_area_struct *vma)
1581 {
1582 struct msc_iter *iter = vma->vm_file->private_data;
1583 struct msc *msc = iter->msc;
1584
1585 atomic_inc(&msc->mmap_count);
1586 }
1587
msc_mmap_close(struct vm_area_struct * vma)1588 static void msc_mmap_close(struct vm_area_struct *vma)
1589 {
1590 struct msc_iter *iter = vma->vm_file->private_data;
1591 struct msc *msc = iter->msc;
1592
1593 if (!atomic_dec_and_mutex_lock(&msc->mmap_count, &msc->buf_mutex))
1594 return;
1595
1596 /* last mapping -- drop user_count */
1597 atomic_dec(&msc->user_count);
1598 mutex_unlock(&msc->buf_mutex);
1599 }
1600
msc_mmap_fault(struct vm_fault * vmf)1601 static vm_fault_t msc_mmap_fault(struct vm_fault *vmf)
1602 {
1603 struct msc_iter *iter = vmf->vma->vm_file->private_data;
1604 struct msc *msc = iter->msc;
1605 struct page *page;
1606
1607 page = msc_buffer_get_page(msc, vmf->pgoff);
1608 if (!page)
1609 return VM_FAULT_SIGBUS;
1610
1611 get_page(page);
1612 return vmf_insert_mixed(vmf->vma, vmf->address, page_to_pfn_t(page));
1613 }
1614
1615 static const struct vm_operations_struct msc_mmap_ops = {
1616 .open = msc_mmap_open,
1617 .close = msc_mmap_close,
1618 .fault = msc_mmap_fault,
1619 };
1620
intel_th_msc_mmap(struct file * file,struct vm_area_struct * vma)1621 static int intel_th_msc_mmap(struct file *file, struct vm_area_struct *vma)
1622 {
1623 unsigned long size = vma->vm_end - vma->vm_start;
1624 struct msc_iter *iter = vma->vm_file->private_data;
1625 struct msc *msc = iter->msc;
1626 int ret = -EINVAL;
1627
1628 if (!size || offset_in_page(size))
1629 return -EINVAL;
1630
1631 if (vma->vm_pgoff)
1632 return -EINVAL;
1633
1634 /* grab user_count once per mmap; drop in msc_mmap_close() */
1635 if (!atomic_inc_unless_negative(&msc->user_count))
1636 return -EINVAL;
1637
1638 if (msc->mode != MSC_MODE_SINGLE &&
1639 msc->mode != MSC_MODE_MULTI)
1640 goto out;
1641
1642 if (size >> PAGE_SHIFT != msc->nr_pages)
1643 goto out;
1644
1645 atomic_set(&msc->mmap_count, 1);
1646 ret = 0;
1647
1648 out:
1649 if (ret)
1650 atomic_dec(&msc->user_count);
1651
1652 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1653 vm_flags_set(vma, VM_DONTEXPAND | VM_DONTCOPY | VM_MIXEDMAP);
1654 vma->vm_ops = &msc_mmap_ops;
1655 return ret;
1656 }
1657
1658 static const struct file_operations intel_th_msc_fops = {
1659 .open = intel_th_msc_open,
1660 .release = intel_th_msc_release,
1661 .read = intel_th_msc_read,
1662 .mmap = intel_th_msc_mmap,
1663 .owner = THIS_MODULE,
1664 };
1665
intel_th_msc_wait_empty(struct intel_th_device * thdev)1666 static void intel_th_msc_wait_empty(struct intel_th_device *thdev)
1667 {
1668 struct msc *msc = dev_get_drvdata(&thdev->dev);
1669 unsigned long count;
1670 u32 reg;
1671
1672 for (reg = 0, count = MSC_PLE_WAITLOOP_DEPTH;
1673 count && !(reg & MSCSTS_PLE); count--) {
1674 reg = __raw_readl(msc->reg_base + REG_MSU_MSC0STS);
1675 cpu_relax();
1676 }
1677
1678 if (!count)
1679 dev_dbg(msc_dev(msc), "timeout waiting for MSC0 PLE\n");
1680 }
1681
intel_th_msc_init(struct msc * msc)1682 static int intel_th_msc_init(struct msc *msc)
1683 {
1684 atomic_set(&msc->user_count, -1);
1685
1686 msc->mode = msc->multi_is_broken ? MSC_MODE_SINGLE : MSC_MODE_MULTI;
1687 mutex_init(&msc->buf_mutex);
1688 INIT_LIST_HEAD(&msc->win_list);
1689 INIT_LIST_HEAD(&msc->iter_list);
1690
1691 msc->burst_len =
1692 (ioread32(msc->reg_base + REG_MSU_MSC0CTL) & MSC_LEN) >>
1693 __ffs(MSC_LEN);
1694
1695 return 0;
1696 }
1697
msc_win_switch(struct msc * msc)1698 static int msc_win_switch(struct msc *msc)
1699 {
1700 struct msc_window *first;
1701
1702 if (list_empty(&msc->win_list))
1703 return -EINVAL;
1704
1705 first = list_first_entry(&msc->win_list, struct msc_window, entry);
1706
1707 if (msc_is_last_win(msc->cur_win))
1708 msc->cur_win = first;
1709 else
1710 msc->cur_win = list_next_entry(msc->cur_win, entry);
1711
1712 msc->base = msc_win_base(msc->cur_win);
1713 msc->base_addr = msc_win_base_dma(msc->cur_win);
1714
1715 intel_th_trace_switch(msc->thdev);
1716
1717 return 0;
1718 }
1719
1720 /**
1721 * intel_th_msc_window_unlock - put the window back in rotation
1722 * @dev: MSC device to which this relates
1723 * @sgt: buffer's sg_table for the window, does nothing if NULL
1724 */
intel_th_msc_window_unlock(struct device * dev,struct sg_table * sgt)1725 void intel_th_msc_window_unlock(struct device *dev, struct sg_table *sgt)
1726 {
1727 struct msc *msc = dev_get_drvdata(dev);
1728 struct msc_window *win;
1729
1730 if (!sgt)
1731 return;
1732
1733 win = msc_find_window(msc, sgt, false);
1734 if (!win)
1735 return;
1736
1737 msc_win_set_lockout(win, WIN_LOCKED, WIN_READY);
1738 if (msc->switch_on_unlock == win) {
1739 msc->switch_on_unlock = NULL;
1740 msc_win_switch(msc);
1741 }
1742 }
1743 EXPORT_SYMBOL_GPL(intel_th_msc_window_unlock);
1744
msc_work(struct work_struct * work)1745 static void msc_work(struct work_struct *work)
1746 {
1747 struct msc *msc = container_of(work, struct msc, work);
1748
1749 intel_th_msc_deactivate(msc->thdev);
1750 }
1751
intel_th_msc_interrupt(struct intel_th_device * thdev)1752 static irqreturn_t intel_th_msc_interrupt(struct intel_th_device *thdev)
1753 {
1754 struct msc *msc = dev_get_drvdata(&thdev->dev);
1755 u32 msusts = ioread32(msc->msu_base + REG_MSU_MSUSTS);
1756 u32 mask = msc->index ? MSUSTS_MSC1BLAST : MSUSTS_MSC0BLAST;
1757 struct msc_window *win, *next_win;
1758
1759 if (!msc->do_irq || !msc->mbuf)
1760 return IRQ_NONE;
1761
1762 msusts &= mask;
1763
1764 if (!msusts)
1765 return msc->enabled ? IRQ_HANDLED : IRQ_NONE;
1766
1767 iowrite32(msusts, msc->msu_base + REG_MSU_MSUSTS);
1768
1769 if (!msc->enabled)
1770 return IRQ_NONE;
1771
1772 /* grab the window before we do the switch */
1773 win = msc->cur_win;
1774 if (!win)
1775 return IRQ_HANDLED;
1776 next_win = msc_next_window(win);
1777 if (!next_win)
1778 return IRQ_HANDLED;
1779
1780 /* next window: if READY, proceed, if LOCKED, stop the trace */
1781 if (msc_win_set_lockout(next_win, WIN_READY, WIN_INUSE)) {
1782 if (msc->stop_on_full)
1783 schedule_work(&msc->work);
1784 else
1785 msc->switch_on_unlock = next_win;
1786
1787 return IRQ_HANDLED;
1788 }
1789
1790 /* current window: INUSE -> LOCKED */
1791 msc_win_set_lockout(win, WIN_INUSE, WIN_LOCKED);
1792
1793 msc_win_switch(msc);
1794
1795 if (msc->mbuf && msc->mbuf->ready)
1796 msc->mbuf->ready(msc->mbuf_priv, win->sgt,
1797 msc_win_total_sz(win));
1798
1799 return IRQ_HANDLED;
1800 }
1801
1802 static const char * const msc_mode[] = {
1803 [MSC_MODE_SINGLE] = "single",
1804 [MSC_MODE_MULTI] = "multi",
1805 [MSC_MODE_EXI] = "ExI",
1806 [MSC_MODE_DEBUG] = "debug",
1807 };
1808
1809 static ssize_t
wrap_show(struct device * dev,struct device_attribute * attr,char * buf)1810 wrap_show(struct device *dev, struct device_attribute *attr, char *buf)
1811 {
1812 struct msc *msc = dev_get_drvdata(dev);
1813
1814 return scnprintf(buf, PAGE_SIZE, "%d\n", msc->wrap);
1815 }
1816
1817 static ssize_t
wrap_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1818 wrap_store(struct device *dev, struct device_attribute *attr, const char *buf,
1819 size_t size)
1820 {
1821 struct msc *msc = dev_get_drvdata(dev);
1822 unsigned long val;
1823 int ret;
1824
1825 ret = kstrtoul(buf, 10, &val);
1826 if (ret)
1827 return ret;
1828
1829 msc->wrap = !!val;
1830
1831 return size;
1832 }
1833
1834 static DEVICE_ATTR_RW(wrap);
1835
msc_buffer_unassign(struct msc * msc)1836 static void msc_buffer_unassign(struct msc *msc)
1837 {
1838 lockdep_assert_held(&msc->buf_mutex);
1839
1840 if (!msc->mbuf)
1841 return;
1842
1843 msc->mbuf->unassign(msc->mbuf_priv);
1844 msu_buffer_put(msc->mbuf);
1845 msc->mbuf_priv = NULL;
1846 msc->mbuf = NULL;
1847 }
1848
1849 static ssize_t
mode_show(struct device * dev,struct device_attribute * attr,char * buf)1850 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1851 {
1852 struct msc *msc = dev_get_drvdata(dev);
1853 const char *mode = msc_mode[msc->mode];
1854 ssize_t ret;
1855
1856 mutex_lock(&msc->buf_mutex);
1857 if (msc->mbuf)
1858 mode = msc->mbuf->name;
1859 ret = scnprintf(buf, PAGE_SIZE, "%s\n", mode);
1860 mutex_unlock(&msc->buf_mutex);
1861
1862 return ret;
1863 }
1864
1865 static ssize_t
mode_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1866 mode_store(struct device *dev, struct device_attribute *attr, const char *buf,
1867 size_t size)
1868 {
1869 const struct msu_buffer *mbuf = NULL;
1870 struct msc *msc = dev_get_drvdata(dev);
1871 size_t len = size;
1872 char *cp, *mode;
1873 int i, ret;
1874
1875 if (!capable(CAP_SYS_RAWIO))
1876 return -EPERM;
1877
1878 cp = memchr(buf, '\n', len);
1879 if (cp)
1880 len = cp - buf;
1881
1882 mode = kstrndup(buf, len, GFP_KERNEL);
1883 if (!mode)
1884 return -ENOMEM;
1885
1886 i = match_string(msc_mode, ARRAY_SIZE(msc_mode), mode);
1887 if (i >= 0) {
1888 kfree(mode);
1889 goto found;
1890 }
1891
1892 /* Buffer sinks only work with a usable IRQ */
1893 if (!msc->do_irq) {
1894 kfree(mode);
1895 return -EINVAL;
1896 }
1897
1898 mbuf = msu_buffer_get(mode);
1899 kfree(mode);
1900 if (mbuf)
1901 goto found;
1902
1903 return -EINVAL;
1904
1905 found:
1906 if (i == MSC_MODE_MULTI && msc->multi_is_broken)
1907 return -EOPNOTSUPP;
1908
1909 mutex_lock(&msc->buf_mutex);
1910 ret = 0;
1911
1912 /* Same buffer: do nothing */
1913 if (mbuf && mbuf == msc->mbuf) {
1914 /* put the extra reference we just got */
1915 msu_buffer_put(mbuf);
1916 goto unlock;
1917 }
1918
1919 ret = msc_buffer_unlocked_free_unless_used(msc);
1920 if (ret)
1921 goto unlock;
1922
1923 if (mbuf) {
1924 void *mbuf_priv = mbuf->assign(dev, &i);
1925
1926 if (!mbuf_priv) {
1927 ret = -ENOMEM;
1928 goto unlock;
1929 }
1930
1931 msc_buffer_unassign(msc);
1932 msc->mbuf_priv = mbuf_priv;
1933 msc->mbuf = mbuf;
1934 } else {
1935 msc_buffer_unassign(msc);
1936 }
1937
1938 msc->mode = i;
1939
1940 unlock:
1941 if (ret && mbuf)
1942 msu_buffer_put(mbuf);
1943 mutex_unlock(&msc->buf_mutex);
1944
1945 return ret ? ret : size;
1946 }
1947
1948 static DEVICE_ATTR_RW(mode);
1949
1950 static ssize_t
nr_pages_show(struct device * dev,struct device_attribute * attr,char * buf)1951 nr_pages_show(struct device *dev, struct device_attribute *attr, char *buf)
1952 {
1953 struct msc *msc = dev_get_drvdata(dev);
1954 struct msc_window *win;
1955 size_t count = 0;
1956
1957 mutex_lock(&msc->buf_mutex);
1958
1959 if (msc->mode == MSC_MODE_SINGLE)
1960 count = scnprintf(buf, PAGE_SIZE, "%ld\n", msc->nr_pages);
1961 else if (msc->mode == MSC_MODE_MULTI) {
1962 list_for_each_entry(win, &msc->win_list, entry) {
1963 count += scnprintf(buf + count, PAGE_SIZE - count,
1964 "%d%c", win->nr_blocks,
1965 msc_is_last_win(win) ? '\n' : ',');
1966 }
1967 } else {
1968 count = scnprintf(buf, PAGE_SIZE, "unsupported\n");
1969 }
1970
1971 mutex_unlock(&msc->buf_mutex);
1972
1973 return count;
1974 }
1975
1976 static ssize_t
nr_pages_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1977 nr_pages_store(struct device *dev, struct device_attribute *attr,
1978 const char *buf, size_t size)
1979 {
1980 struct msc *msc = dev_get_drvdata(dev);
1981 unsigned long val, *win = NULL, *rewin;
1982 size_t len = size;
1983 const char *p = buf;
1984 char *end, *s;
1985 int ret, nr_wins = 0;
1986
1987 if (!capable(CAP_SYS_RAWIO))
1988 return -EPERM;
1989
1990 ret = msc_buffer_free_unless_used(msc);
1991 if (ret)
1992 return ret;
1993
1994 /* scan the comma-separated list of allocation sizes */
1995 end = memchr(buf, '\n', len);
1996 if (end)
1997 len = end - buf;
1998
1999 do {
2000 end = memchr(p, ',', len);
2001 s = kstrndup(p, end ? end - p : len, GFP_KERNEL);
2002 if (!s) {
2003 ret = -ENOMEM;
2004 goto free_win;
2005 }
2006
2007 ret = kstrtoul(s, 10, &val);
2008 kfree(s);
2009
2010 if (ret || !val)
2011 goto free_win;
2012
2013 if (nr_wins && msc->mode == MSC_MODE_SINGLE) {
2014 ret = -EINVAL;
2015 goto free_win;
2016 }
2017
2018 nr_wins++;
2019 rewin = krealloc_array(win, nr_wins, sizeof(*win), GFP_KERNEL);
2020 if (!rewin) {
2021 kfree(win);
2022 return -ENOMEM;
2023 }
2024
2025 win = rewin;
2026 win[nr_wins - 1] = val;
2027
2028 if (!end)
2029 break;
2030
2031 /* consume the number and the following comma, hence +1 */
2032 len -= end - p + 1;
2033 p = end + 1;
2034 } while (len);
2035
2036 mutex_lock(&msc->buf_mutex);
2037 ret = msc_buffer_alloc(msc, win, nr_wins);
2038 mutex_unlock(&msc->buf_mutex);
2039
2040 free_win:
2041 kfree(win);
2042
2043 return ret ? ret : size;
2044 }
2045
2046 static DEVICE_ATTR_RW(nr_pages);
2047
2048 static ssize_t
win_switch_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)2049 win_switch_store(struct device *dev, struct device_attribute *attr,
2050 const char *buf, size_t size)
2051 {
2052 struct msc *msc = dev_get_drvdata(dev);
2053 unsigned long val;
2054 int ret;
2055
2056 ret = kstrtoul(buf, 10, &val);
2057 if (ret)
2058 return ret;
2059
2060 if (val != 1)
2061 return -EINVAL;
2062
2063 ret = -EINVAL;
2064 mutex_lock(&msc->buf_mutex);
2065 /*
2066 * Window switch can only happen in the "multi" mode.
2067 * If a external buffer is engaged, they have the full
2068 * control over window switching.
2069 */
2070 if (msc->mode == MSC_MODE_MULTI && !msc->mbuf)
2071 ret = msc_win_switch(msc);
2072 mutex_unlock(&msc->buf_mutex);
2073
2074 return ret ? ret : size;
2075 }
2076
2077 static DEVICE_ATTR_WO(win_switch);
2078
stop_on_full_show(struct device * dev,struct device_attribute * attr,char * buf)2079 static ssize_t stop_on_full_show(struct device *dev,
2080 struct device_attribute *attr, char *buf)
2081 {
2082 struct msc *msc = dev_get_drvdata(dev);
2083
2084 return sprintf(buf, "%d\n", msc->stop_on_full);
2085 }
2086
stop_on_full_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)2087 static ssize_t stop_on_full_store(struct device *dev,
2088 struct device_attribute *attr,
2089 const char *buf, size_t size)
2090 {
2091 struct msc *msc = dev_get_drvdata(dev);
2092 int ret;
2093
2094 ret = kstrtobool(buf, &msc->stop_on_full);
2095 if (ret)
2096 return ret;
2097
2098 return size;
2099 }
2100
2101 static DEVICE_ATTR_RW(stop_on_full);
2102
2103 static struct attribute *msc_output_attrs[] = {
2104 &dev_attr_wrap.attr,
2105 &dev_attr_mode.attr,
2106 &dev_attr_nr_pages.attr,
2107 &dev_attr_win_switch.attr,
2108 &dev_attr_stop_on_full.attr,
2109 NULL,
2110 };
2111
2112 static const struct attribute_group msc_output_group = {
2113 .attrs = msc_output_attrs,
2114 };
2115
intel_th_msc_probe(struct intel_th_device * thdev)2116 static int intel_th_msc_probe(struct intel_th_device *thdev)
2117 {
2118 struct device *dev = &thdev->dev;
2119 struct resource *res;
2120 struct msc *msc;
2121 void __iomem *base;
2122 int err;
2123
2124 res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
2125 if (!res)
2126 return -ENODEV;
2127
2128 base = devm_ioremap(dev, res->start, resource_size(res));
2129 if (!base)
2130 return -ENOMEM;
2131
2132 msc = devm_kzalloc(dev, sizeof(*msc), GFP_KERNEL);
2133 if (!msc)
2134 return -ENOMEM;
2135
2136 res = intel_th_device_get_resource(thdev, IORESOURCE_IRQ, 1);
2137 if (!res)
2138 msc->do_irq = 1;
2139
2140 if (INTEL_TH_CAP(to_intel_th(thdev), multi_is_broken))
2141 msc->multi_is_broken = 1;
2142
2143 msc->index = thdev->id;
2144
2145 msc->thdev = thdev;
2146 msc->reg_base = base + msc->index * 0x100;
2147 msc->msu_base = base;
2148
2149 INIT_WORK(&msc->work, msc_work);
2150 err = intel_th_msc_init(msc);
2151 if (err)
2152 return err;
2153
2154 dev_set_drvdata(dev, msc);
2155
2156 return 0;
2157 }
2158
intel_th_msc_remove(struct intel_th_device * thdev)2159 static void intel_th_msc_remove(struct intel_th_device *thdev)
2160 {
2161 struct msc *msc = dev_get_drvdata(&thdev->dev);
2162 int ret;
2163
2164 intel_th_msc_deactivate(thdev);
2165
2166 /*
2167 * Buffers should not be used at this point except if the
2168 * output character device is still open and the parent
2169 * device gets detached from its bus, which is a FIXME.
2170 */
2171 ret = msc_buffer_free_unless_used(msc);
2172 WARN_ON_ONCE(ret);
2173 }
2174
2175 static struct intel_th_driver intel_th_msc_driver = {
2176 .probe = intel_th_msc_probe,
2177 .remove = intel_th_msc_remove,
2178 .irq = intel_th_msc_interrupt,
2179 .wait_empty = intel_th_msc_wait_empty,
2180 .activate = intel_th_msc_activate,
2181 .deactivate = intel_th_msc_deactivate,
2182 .fops = &intel_th_msc_fops,
2183 .attr_group = &msc_output_group,
2184 .driver = {
2185 .name = "msc",
2186 .owner = THIS_MODULE,
2187 },
2188 };
2189
2190 module_driver(intel_th_msc_driver,
2191 intel_th_driver_register,
2192 intel_th_driver_unregister);
2193
2194 MODULE_LICENSE("GPL v2");
2195 MODULE_DESCRIPTION("Intel(R) Trace Hub Memory Storage Unit driver");
2196 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");
2197