treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / hwtracing / intel_th / msu.c
blob8e48c7458aa35cbbf3963462d969d2b22614ce49
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Intel(R) Trace Hub Memory Storage Unit
5 * Copyright (C) 2014-2015 Intel Corporation.
6 */
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
23 #ifdef CONFIG_X86
24 #include <asm/set_memory.h>
25 #endif
27 #include <linux/intel_th.h>
28 #include "intel_th.h"
29 #include "msu.h"
31 #define msc_dev(x) (&(x)->thdev->dev)
34 * Lockout state transitions:
35 * READY -> INUSE -+-> LOCKED -+-> READY -> etc.
36 * \-----------/
37 * WIN_READY: window can be used by HW
38 * WIN_INUSE: window is in use
39 * WIN_LOCKED: window is filled up and is being processed by the buffer
40 * handling code
42 * All state transitions happen automatically, except for the LOCKED->READY,
43 * which needs to be signalled by the buffer code by calling
44 * intel_th_msc_window_unlock().
46 * When the interrupt handler has to switch to the next window, it checks
47 * whether it's READY, and if it is, it performs the switch and tracing
48 * continues. If it's LOCKED, it stops the trace.
50 enum lockout_state {
51 WIN_READY = 0,
52 WIN_INUSE,
53 WIN_LOCKED
56 /**
57 * struct msc_window - multiblock mode window descriptor
58 * @entry: window list linkage (msc::win_list)
59 * @pgoff: page offset into the buffer that this window starts at
60 * @lockout: lockout state, see comment below
61 * @lo_lock: lockout state serialization
62 * @nr_blocks: number of blocks (pages) in this window
63 * @nr_segs: number of segments in this window (<= @nr_blocks)
64 * @_sgt: array of block descriptors
65 * @sgt: array of block descriptors
67 struct msc_window {
68 struct list_head entry;
69 unsigned long pgoff;
70 enum lockout_state lockout;
71 spinlock_t lo_lock;
72 unsigned int nr_blocks;
73 unsigned int nr_segs;
74 struct msc *msc;
75 struct sg_table _sgt;
76 struct sg_table *sgt;
79 /**
80 * struct msc_iter - iterator for msc buffer
81 * @entry: msc::iter_list linkage
82 * @msc: pointer to the MSC device
83 * @start_win: oldest window
84 * @win: current window
85 * @offset: current logical offset into the buffer
86 * @start_block: oldest block in the window
87 * @block: block number in the window
88 * @block_off: offset into current block
89 * @wrap_count: block wrapping handling
90 * @eof: end of buffer reached
92 struct msc_iter {
93 struct list_head entry;
94 struct msc *msc;
95 struct msc_window *start_win;
96 struct msc_window *win;
97 unsigned long offset;
98 struct scatterlist *start_block;
99 struct scatterlist *block;
100 unsigned int block_off;
101 unsigned int wrap_count;
102 unsigned int eof;
106 * struct msc - MSC device representation
107 * @reg_base: register window base address
108 * @thdev: intel_th_device pointer
109 * @mbuf: MSU buffer, if assigned
110 * @mbuf_priv MSU buffer's private data, if @mbuf
111 * @win_list: list of windows in multiblock mode
112 * @single_sgt: single mode buffer
113 * @cur_win: current window
114 * @nr_pages: total number of pages allocated for this buffer
115 * @single_sz: amount of data in single mode
116 * @single_wrap: single mode wrap occurred
117 * @base: buffer's base pointer
118 * @base_addr: buffer's base address
119 * @user_count: number of users of the buffer
120 * @mmap_count: number of mappings
121 * @buf_mutex: mutex to serialize access to buffer-related bits
123 * @enabled: MSC is enabled
124 * @wrap: wrapping is enabled
125 * @mode: MSC operating mode
126 * @burst_len: write burst length
127 * @index: number of this MSC in the MSU
129 struct msc {
130 void __iomem *reg_base;
131 void __iomem *msu_base;
132 struct intel_th_device *thdev;
134 const struct msu_buffer *mbuf;
135 void *mbuf_priv;
137 struct work_struct work;
138 struct list_head win_list;
139 struct sg_table single_sgt;
140 struct msc_window *cur_win;
141 unsigned long nr_pages;
142 unsigned long single_sz;
143 unsigned int single_wrap : 1;
144 void *base;
145 dma_addr_t base_addr;
146 u32 orig_addr;
147 u32 orig_sz;
149 /* <0: no buffer, 0: no users, >0: active users */
150 atomic_t user_count;
152 atomic_t mmap_count;
153 struct mutex buf_mutex;
155 struct list_head iter_list;
157 /* config */
158 unsigned int enabled : 1,
159 wrap : 1,
160 do_irq : 1;
161 unsigned int mode;
162 unsigned int burst_len;
163 unsigned int index;
166 static LIST_HEAD(msu_buffer_list);
167 static DEFINE_MUTEX(msu_buffer_mutex);
170 * struct msu_buffer_entry - internal MSU buffer bookkeeping
171 * @entry: link to msu_buffer_list
172 * @mbuf: MSU buffer object
173 * @owner: module that provides this MSU buffer
175 struct msu_buffer_entry {
176 struct list_head entry;
177 const struct msu_buffer *mbuf;
178 struct module *owner;
181 static struct msu_buffer_entry *__msu_buffer_entry_find(const char *name)
183 struct msu_buffer_entry *mbe;
185 lockdep_assert_held(&msu_buffer_mutex);
187 list_for_each_entry(mbe, &msu_buffer_list, entry) {
188 if (!strcmp(mbe->mbuf->name, name))
189 return mbe;
192 return NULL;
195 static const struct msu_buffer *
196 msu_buffer_get(const char *name)
198 struct msu_buffer_entry *mbe;
200 mutex_lock(&msu_buffer_mutex);
201 mbe = __msu_buffer_entry_find(name);
202 if (mbe && !try_module_get(mbe->owner))
203 mbe = NULL;
204 mutex_unlock(&msu_buffer_mutex);
206 return mbe ? mbe->mbuf : NULL;
209 static void msu_buffer_put(const struct msu_buffer *mbuf)
211 struct msu_buffer_entry *mbe;
213 mutex_lock(&msu_buffer_mutex);
214 mbe = __msu_buffer_entry_find(mbuf->name);
215 if (mbe)
216 module_put(mbe->owner);
217 mutex_unlock(&msu_buffer_mutex);
220 int intel_th_msu_buffer_register(const struct msu_buffer *mbuf,
221 struct module *owner)
223 struct msu_buffer_entry *mbe;
224 int ret = 0;
226 mbe = kzalloc(sizeof(*mbe), GFP_KERNEL);
227 if (!mbe)
228 return -ENOMEM;
230 mutex_lock(&msu_buffer_mutex);
231 if (__msu_buffer_entry_find(mbuf->name)) {
232 ret = -EEXIST;
233 kfree(mbe);
234 goto unlock;
237 mbe->mbuf = mbuf;
238 mbe->owner = owner;
239 list_add_tail(&mbe->entry, &msu_buffer_list);
240 unlock:
241 mutex_unlock(&msu_buffer_mutex);
243 return ret;
245 EXPORT_SYMBOL_GPL(intel_th_msu_buffer_register);
247 void intel_th_msu_buffer_unregister(const struct msu_buffer *mbuf)
249 struct msu_buffer_entry *mbe;
251 mutex_lock(&msu_buffer_mutex);
252 mbe = __msu_buffer_entry_find(mbuf->name);
253 if (mbe) {
254 list_del(&mbe->entry);
255 kfree(mbe);
257 mutex_unlock(&msu_buffer_mutex);
259 EXPORT_SYMBOL_GPL(intel_th_msu_buffer_unregister);
261 static inline bool msc_block_is_empty(struct msc_block_desc *bdesc)
263 /* header hasn't been written */
264 if (!bdesc->valid_dw)
265 return true;
267 /* valid_dw includes the header */
268 if (!msc_data_sz(bdesc))
269 return true;
271 return false;
274 static inline struct scatterlist *msc_win_base_sg(struct msc_window *win)
276 return win->sgt->sgl;
279 static inline struct msc_block_desc *msc_win_base(struct msc_window *win)
281 return sg_virt(msc_win_base_sg(win));
284 static inline dma_addr_t msc_win_base_dma(struct msc_window *win)
286 return sg_dma_address(msc_win_base_sg(win));
289 static inline unsigned long
290 msc_win_base_pfn(struct msc_window *win)
292 return PFN_DOWN(msc_win_base_dma(win));
296 * msc_is_last_win() - check if a window is the last one for a given MSC
297 * @win: window
298 * Return: true if @win is the last window in MSC's multiblock buffer
300 static inline bool msc_is_last_win(struct msc_window *win)
302 return win->entry.next == &win->msc->win_list;
306 * msc_next_window() - return next window in the multiblock buffer
307 * @win: current window
309 * Return: window following the current one
311 static struct msc_window *msc_next_window(struct msc_window *win)
313 if (msc_is_last_win(win))
314 return list_first_entry(&win->msc->win_list, struct msc_window,
315 entry);
317 return list_next_entry(win, entry);
320 static size_t msc_win_total_sz(struct msc_window *win)
322 struct scatterlist *sg;
323 unsigned int blk;
324 size_t size = 0;
326 for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
327 struct msc_block_desc *bdesc = sg_virt(sg);
329 if (msc_block_wrapped(bdesc))
330 return (size_t)win->nr_blocks << PAGE_SHIFT;
332 size += msc_total_sz(bdesc);
333 if (msc_block_last_written(bdesc))
334 break;
337 return size;
341 * msc_find_window() - find a window matching a given sg_table
342 * @msc: MSC device
343 * @sgt: SG table of the window
344 * @nonempty: skip over empty windows
346 * Return: MSC window structure pointer or NULL if the window
347 * could not be found.
349 static struct msc_window *
350 msc_find_window(struct msc *msc, struct sg_table *sgt, bool nonempty)
352 struct msc_window *win;
353 unsigned int found = 0;
355 if (list_empty(&msc->win_list))
356 return NULL;
359 * we might need a radix tree for this, depending on how
360 * many windows a typical user would allocate; ideally it's
361 * something like 2, in which case we're good
363 list_for_each_entry(win, &msc->win_list, entry) {
364 if (win->sgt == sgt)
365 found++;
367 /* skip the empty ones */
368 if (nonempty && msc_block_is_empty(msc_win_base(win)))
369 continue;
371 if (found)
372 return win;
375 return NULL;
379 * msc_oldest_window() - locate the window with oldest data
380 * @msc: MSC device
382 * This should only be used in multiblock mode. Caller should hold the
383 * msc::user_count reference.
385 * Return: the oldest window with valid data
387 static struct msc_window *msc_oldest_window(struct msc *msc)
389 struct msc_window *win;
391 if (list_empty(&msc->win_list))
392 return NULL;
394 win = msc_find_window(msc, msc_next_window(msc->cur_win)->sgt, true);
395 if (win)
396 return win;
398 return list_first_entry(&msc->win_list, struct msc_window, entry);
402 * msc_win_oldest_sg() - locate the oldest block in a given window
403 * @win: window to look at
405 * Return: index of the block with the oldest data
407 static struct scatterlist *msc_win_oldest_sg(struct msc_window *win)
409 unsigned int blk;
410 struct scatterlist *sg;
411 struct msc_block_desc *bdesc = msc_win_base(win);
413 /* without wrapping, first block is the oldest */
414 if (!msc_block_wrapped(bdesc))
415 return msc_win_base_sg(win);
418 * with wrapping, last written block contains both the newest and the
419 * oldest data for this window.
421 for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
422 struct msc_block_desc *bdesc = sg_virt(sg);
424 if (msc_block_last_written(bdesc))
425 return sg;
428 return msc_win_base_sg(win);
431 static struct msc_block_desc *msc_iter_bdesc(struct msc_iter *iter)
433 return sg_virt(iter->block);
436 static struct msc_iter *msc_iter_install(struct msc *msc)
438 struct msc_iter *iter;
440 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
441 if (!iter)
442 return ERR_PTR(-ENOMEM);
444 mutex_lock(&msc->buf_mutex);
447 * Reading and tracing are mutually exclusive; if msc is
448 * enabled, open() will fail; otherwise existing readers
449 * will prevent enabling the msc and the rest of fops don't
450 * need to worry about it.
452 if (msc->enabled) {
453 kfree(iter);
454 iter = ERR_PTR(-EBUSY);
455 goto unlock;
458 iter->msc = msc;
460 list_add_tail(&iter->entry, &msc->iter_list);
461 unlock:
462 mutex_unlock(&msc->buf_mutex);
464 return iter;
467 static void msc_iter_remove(struct msc_iter *iter, struct msc *msc)
469 mutex_lock(&msc->buf_mutex);
470 list_del(&iter->entry);
471 mutex_unlock(&msc->buf_mutex);
473 kfree(iter);
476 static void msc_iter_block_start(struct msc_iter *iter)
478 if (iter->start_block)
479 return;
481 iter->start_block = msc_win_oldest_sg(iter->win);
482 iter->block = iter->start_block;
483 iter->wrap_count = 0;
486 * start with the block with oldest data; if data has wrapped
487 * in this window, it should be in this block
489 if (msc_block_wrapped(msc_iter_bdesc(iter)))
490 iter->wrap_count = 2;
494 static int msc_iter_win_start(struct msc_iter *iter, struct msc *msc)
496 /* already started, nothing to do */
497 if (iter->start_win)
498 return 0;
500 iter->start_win = msc_oldest_window(msc);
501 if (!iter->start_win)
502 return -EINVAL;
504 iter->win = iter->start_win;
505 iter->start_block = NULL;
507 msc_iter_block_start(iter);
509 return 0;
512 static int msc_iter_win_advance(struct msc_iter *iter)
514 iter->win = msc_next_window(iter->win);
515 iter->start_block = NULL;
517 if (iter->win == iter->start_win) {
518 iter->eof++;
519 return 1;
522 msc_iter_block_start(iter);
524 return 0;
527 static int msc_iter_block_advance(struct msc_iter *iter)
529 iter->block_off = 0;
531 /* wrapping */
532 if (iter->wrap_count && iter->block == iter->start_block) {
533 iter->wrap_count--;
534 if (!iter->wrap_count)
535 /* copied newest data from the wrapped block */
536 return msc_iter_win_advance(iter);
539 /* no wrapping, check for last written block */
540 if (!iter->wrap_count && msc_block_last_written(msc_iter_bdesc(iter)))
541 /* copied newest data for the window */
542 return msc_iter_win_advance(iter);
544 /* block advance */
545 if (sg_is_last(iter->block))
546 iter->block = msc_win_base_sg(iter->win);
547 else
548 iter->block = sg_next(iter->block);
550 /* no wrapping, sanity check in case there is no last written block */
551 if (!iter->wrap_count && iter->block == iter->start_block)
552 return msc_iter_win_advance(iter);
554 return 0;
558 * msc_buffer_iterate() - go through multiblock buffer's data
559 * @iter: iterator structure
560 * @size: amount of data to scan
561 * @data: callback's private data
562 * @fn: iterator callback
564 * This will start at the window which will be written to next (containing
565 * the oldest data) and work its way to the current window, calling @fn
566 * for each chunk of data as it goes.
568 * Caller should have msc::user_count reference to make sure the buffer
569 * doesn't disappear from under us.
571 * Return: amount of data actually scanned.
573 static ssize_t
574 msc_buffer_iterate(struct msc_iter *iter, size_t size, void *data,
575 unsigned long (*fn)(void *, void *, size_t))
577 struct msc *msc = iter->msc;
578 size_t len = size;
579 unsigned int advance;
581 if (iter->eof)
582 return 0;
584 /* start with the oldest window */
585 if (msc_iter_win_start(iter, msc))
586 return 0;
588 do {
589 unsigned long data_bytes = msc_data_sz(msc_iter_bdesc(iter));
590 void *src = (void *)msc_iter_bdesc(iter) + MSC_BDESC;
591 size_t tocopy = data_bytes, copied = 0;
592 size_t remaining = 0;
594 advance = 1;
597 * If block wrapping happened, we need to visit the last block
598 * twice, because it contains both the oldest and the newest
599 * data in this window.
601 * First time (wrap_count==2), in the very beginning, to collect
602 * the oldest data, which is in the range
603 * (data_bytes..DATA_IN_PAGE).
605 * Second time (wrap_count==1), it's just like any other block,
606 * containing data in the range of [MSC_BDESC..data_bytes].
608 if (iter->block == iter->start_block && iter->wrap_count == 2) {
609 tocopy = DATA_IN_PAGE - data_bytes;
610 src += data_bytes;
613 if (!tocopy)
614 goto next_block;
616 tocopy -= iter->block_off;
617 src += iter->block_off;
619 if (len < tocopy) {
620 tocopy = len;
621 advance = 0;
624 remaining = fn(data, src, tocopy);
626 if (remaining)
627 advance = 0;
629 copied = tocopy - remaining;
630 len -= copied;
631 iter->block_off += copied;
632 iter->offset += copied;
634 if (!advance)
635 break;
637 next_block:
638 if (msc_iter_block_advance(iter))
639 break;
641 } while (len);
643 return size - len;
647 * msc_buffer_clear_hw_header() - clear hw header for multiblock
648 * @msc: MSC device
650 static void msc_buffer_clear_hw_header(struct msc *msc)
652 struct msc_window *win;
653 struct scatterlist *sg;
655 list_for_each_entry(win, &msc->win_list, entry) {
656 unsigned int blk;
657 size_t hw_sz = sizeof(struct msc_block_desc) -
658 offsetof(struct msc_block_desc, hw_tag);
660 for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
661 struct msc_block_desc *bdesc = sg_virt(sg);
663 memset(&bdesc->hw_tag, 0, hw_sz);
668 static int intel_th_msu_init(struct msc *msc)
670 u32 mintctl, msusts;
672 if (!msc->do_irq)
673 return 0;
675 if (!msc->mbuf)
676 return 0;
678 mintctl = ioread32(msc->msu_base + REG_MSU_MINTCTL);
679 mintctl |= msc->index ? M1BLIE : M0BLIE;
680 iowrite32(mintctl, msc->msu_base + REG_MSU_MINTCTL);
681 if (mintctl != ioread32(msc->msu_base + REG_MSU_MINTCTL)) {
682 dev_info(msc_dev(msc), "MINTCTL ignores writes: no usable interrupts\n");
683 msc->do_irq = 0;
684 return 0;
687 msusts = ioread32(msc->msu_base + REG_MSU_MSUSTS);
688 iowrite32(msusts, msc->msu_base + REG_MSU_MSUSTS);
690 return 0;
693 static void intel_th_msu_deinit(struct msc *msc)
695 u32 mintctl;
697 if (!msc->do_irq)
698 return;
700 mintctl = ioread32(msc->msu_base + REG_MSU_MINTCTL);
701 mintctl &= msc->index ? ~M1BLIE : ~M0BLIE;
702 iowrite32(mintctl, msc->msu_base + REG_MSU_MINTCTL);
705 static int msc_win_set_lockout(struct msc_window *win,
706 enum lockout_state expect,
707 enum lockout_state new)
709 enum lockout_state old;
710 unsigned long flags;
711 int ret = 0;
713 if (!win->msc->mbuf)
714 return 0;
716 spin_lock_irqsave(&win->lo_lock, flags);
717 old = win->lockout;
719 if (old != expect) {
720 ret = -EINVAL;
721 dev_warn_ratelimited(msc_dev(win->msc),
722 "expected lockout state %d, got %d\n",
723 expect, old);
724 goto unlock;
727 win->lockout = new;
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);
734 unlock:
735 spin_unlock_irqrestore(&win->lo_lock, flags);
737 if (ret) {
738 if (expect == WIN_READY && old == WIN_LOCKED)
739 return -EBUSY;
741 /* from intel_th_msc_window_unlock(), don't warn if not locked */
742 if (expect == WIN_LOCKED && old == new)
743 return 0;
746 return ret;
749 * msc_configure() - set up MSC hardware
750 * @msc: the MSC device to configure
752 * Program storage mode, wrapping, burst length and trace buffer address
753 * into a given MSC. Then, enable tracing and set msc::enabled.
754 * The latter is serialized on msc::buf_mutex, so make sure to hold it.
756 static int msc_configure(struct msc *msc)
758 u32 reg;
760 lockdep_assert_held(&msc->buf_mutex);
762 if (msc->mode > MSC_MODE_MULTI)
763 return -ENOTSUPP;
765 if (msc->mode == MSC_MODE_MULTI) {
766 if (msc_win_set_lockout(msc->cur_win, WIN_READY, WIN_INUSE))
767 return -EBUSY;
769 msc_buffer_clear_hw_header(msc);
772 msc->orig_addr = ioread32(msc->reg_base + REG_MSU_MSC0BAR);
773 msc->orig_sz = ioread32(msc->reg_base + REG_MSU_MSC0SIZE);
775 reg = msc->base_addr >> PAGE_SHIFT;
776 iowrite32(reg, msc->reg_base + REG_MSU_MSC0BAR);
778 if (msc->mode == MSC_MODE_SINGLE) {
779 reg = msc->nr_pages;
780 iowrite32(reg, msc->reg_base + REG_MSU_MSC0SIZE);
783 reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
784 reg &= ~(MSC_MODE | MSC_WRAPEN | MSC_EN | MSC_RD_HDR_OVRD);
786 reg |= MSC_EN;
787 reg |= msc->mode << __ffs(MSC_MODE);
788 reg |= msc->burst_len << __ffs(MSC_LEN);
790 if (msc->wrap)
791 reg |= MSC_WRAPEN;
793 iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
795 intel_th_msu_init(msc);
797 msc->thdev->output.multiblock = msc->mode == MSC_MODE_MULTI;
798 intel_th_trace_enable(msc->thdev);
799 msc->enabled = 1;
801 if (msc->mbuf && msc->mbuf->activate)
802 msc->mbuf->activate(msc->mbuf_priv);
804 return 0;
808 * msc_disable() - disable MSC hardware
809 * @msc: MSC device to disable
811 * If @msc is enabled, disable tracing on the switch and then disable MSC
812 * storage. Caller must hold msc::buf_mutex.
814 static void msc_disable(struct msc *msc)
816 struct msc_window *win = msc->cur_win;
817 u32 reg;
819 lockdep_assert_held(&msc->buf_mutex);
821 if (msc->mode == MSC_MODE_MULTI)
822 msc_win_set_lockout(win, WIN_INUSE, WIN_LOCKED);
824 if (msc->mbuf && msc->mbuf->deactivate)
825 msc->mbuf->deactivate(msc->mbuf_priv);
826 intel_th_msu_deinit(msc);
827 intel_th_trace_disable(msc->thdev);
829 if (msc->mode == MSC_MODE_SINGLE) {
830 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
831 msc->single_wrap = !!(reg & MSCSTS_WRAPSTAT);
833 reg = ioread32(msc->reg_base + REG_MSU_MSC0MWP);
834 msc->single_sz = reg & ((msc->nr_pages << PAGE_SHIFT) - 1);
835 dev_dbg(msc_dev(msc), "MSCnMWP: %08x/%08lx, wrap: %d\n",
836 reg, msc->single_sz, msc->single_wrap);
839 reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
840 reg &= ~MSC_EN;
841 iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
843 if (msc->mbuf && msc->mbuf->ready)
844 msc->mbuf->ready(msc->mbuf_priv, win->sgt,
845 msc_win_total_sz(win));
847 msc->enabled = 0;
849 iowrite32(msc->orig_addr, msc->reg_base + REG_MSU_MSC0BAR);
850 iowrite32(msc->orig_sz, msc->reg_base + REG_MSU_MSC0SIZE);
852 dev_dbg(msc_dev(msc), "MSCnNWSA: %08x\n",
853 ioread32(msc->reg_base + REG_MSU_MSC0NWSA));
855 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
856 dev_dbg(msc_dev(msc), "MSCnSTS: %08x\n", reg);
858 reg = ioread32(msc->reg_base + REG_MSU_MSUSTS);
859 reg &= msc->index ? MSUSTS_MSC1BLAST : MSUSTS_MSC0BLAST;
860 iowrite32(reg, msc->reg_base + REG_MSU_MSUSTS);
863 static int intel_th_msc_activate(struct intel_th_device *thdev)
865 struct msc *msc = dev_get_drvdata(&thdev->dev);
866 int ret = -EBUSY;
868 if (!atomic_inc_unless_negative(&msc->user_count))
869 return -ENODEV;
871 mutex_lock(&msc->buf_mutex);
873 /* if there are readers, refuse */
874 if (list_empty(&msc->iter_list))
875 ret = msc_configure(msc);
877 mutex_unlock(&msc->buf_mutex);
879 if (ret)
880 atomic_dec(&msc->user_count);
882 return ret;
885 static void intel_th_msc_deactivate(struct intel_th_device *thdev)
887 struct msc *msc = dev_get_drvdata(&thdev->dev);
889 mutex_lock(&msc->buf_mutex);
890 if (msc->enabled) {
891 msc_disable(msc);
892 atomic_dec(&msc->user_count);
894 mutex_unlock(&msc->buf_mutex);
898 * msc_buffer_contig_alloc() - allocate a contiguous buffer for SINGLE mode
899 * @msc: MSC device
900 * @size: allocation size in bytes
902 * This modifies msc::base, which requires msc::buf_mutex to serialize, so the
903 * caller is expected to hold it.
905 * Return: 0 on success, -errno otherwise.
907 static int msc_buffer_contig_alloc(struct msc *msc, unsigned long size)
909 unsigned long nr_pages = size >> PAGE_SHIFT;
910 unsigned int order = get_order(size);
911 struct page *page;
912 int ret;
914 if (!size)
915 return 0;
917 ret = sg_alloc_table(&msc->single_sgt, 1, GFP_KERNEL);
918 if (ret)
919 goto err_out;
921 ret = -ENOMEM;
922 page = alloc_pages(GFP_KERNEL | __GFP_ZERO | GFP_DMA32, order);
923 if (!page)
924 goto err_free_sgt;
926 split_page(page, order);
927 sg_set_buf(msc->single_sgt.sgl, page_address(page), size);
929 ret = dma_map_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl, 1,
930 DMA_FROM_DEVICE);
931 if (ret < 0)
932 goto err_free_pages;
934 msc->nr_pages = nr_pages;
935 msc->base = page_address(page);
936 msc->base_addr = sg_dma_address(msc->single_sgt.sgl);
938 return 0;
940 err_free_pages:
941 __free_pages(page, order);
943 err_free_sgt:
944 sg_free_table(&msc->single_sgt);
946 err_out:
947 return ret;
951 * msc_buffer_contig_free() - free a contiguous buffer
952 * @msc: MSC configured in SINGLE mode
954 static void msc_buffer_contig_free(struct msc *msc)
956 unsigned long off;
958 dma_unmap_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl,
959 1, DMA_FROM_DEVICE);
960 sg_free_table(&msc->single_sgt);
962 for (off = 0; off < msc->nr_pages << PAGE_SHIFT; off += PAGE_SIZE) {
963 struct page *page = virt_to_page(msc->base + off);
965 page->mapping = NULL;
966 __free_page(page);
969 msc->nr_pages = 0;
973 * msc_buffer_contig_get_page() - find a page at a given offset
974 * @msc: MSC configured in SINGLE mode
975 * @pgoff: page offset
977 * Return: page, if @pgoff is within the range, NULL otherwise.
979 static struct page *msc_buffer_contig_get_page(struct msc *msc,
980 unsigned long pgoff)
982 if (pgoff >= msc->nr_pages)
983 return NULL;
985 return virt_to_page(msc->base + (pgoff << PAGE_SHIFT));
988 static int __msc_buffer_win_alloc(struct msc_window *win,
989 unsigned int nr_segs)
991 struct scatterlist *sg_ptr;
992 void *block;
993 int i, ret;
995 ret = sg_alloc_table(win->sgt, nr_segs, GFP_KERNEL);
996 if (ret)
997 return -ENOMEM;
999 for_each_sg(win->sgt->sgl, sg_ptr, nr_segs, i) {
1000 block = dma_alloc_coherent(msc_dev(win->msc)->parent->parent,
1001 PAGE_SIZE, &sg_dma_address(sg_ptr),
1002 GFP_KERNEL);
1003 if (!block)
1004 goto err_nomem;
1006 sg_set_buf(sg_ptr, block, PAGE_SIZE);
1009 return nr_segs;
1011 err_nomem:
1012 for_each_sg(win->sgt->sgl, sg_ptr, i, ret)
1013 dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
1014 sg_virt(sg_ptr), sg_dma_address(sg_ptr));
1016 sg_free_table(win->sgt);
1018 return -ENOMEM;
1021 #ifdef CONFIG_X86
1022 static void msc_buffer_set_uc(struct msc_window *win, unsigned int nr_segs)
1024 struct scatterlist *sg_ptr;
1025 int i;
1027 for_each_sg(win->sgt->sgl, sg_ptr, nr_segs, i) {
1028 /* Set the page as uncached */
1029 set_memory_uc((unsigned long)sg_virt(sg_ptr),
1030 PFN_DOWN(sg_ptr->length));
1034 static void msc_buffer_set_wb(struct msc_window *win)
1036 struct scatterlist *sg_ptr;
1037 int i;
1039 for_each_sg(win->sgt->sgl, sg_ptr, win->nr_segs, i) {
1040 /* Reset the page to write-back */
1041 set_memory_wb((unsigned long)sg_virt(sg_ptr),
1042 PFN_DOWN(sg_ptr->length));
1045 #else /* !X86 */
1046 static inline void
1047 msc_buffer_set_uc(struct msc_window *win, unsigned int nr_segs) {}
1048 static inline void msc_buffer_set_wb(struct msc_window *win) {}
1049 #endif /* CONFIG_X86 */
1052 * msc_buffer_win_alloc() - alloc a window for a multiblock mode
1053 * @msc: MSC device
1054 * @nr_blocks: number of pages in this window
1056 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
1057 * to serialize, so the caller is expected to hold it.
1059 * Return: 0 on success, -errno otherwise.
1061 static int msc_buffer_win_alloc(struct msc *msc, unsigned int nr_blocks)
1063 struct msc_window *win;
1064 int ret = -ENOMEM;
1066 if (!nr_blocks)
1067 return 0;
1069 win = kzalloc(sizeof(*win), GFP_KERNEL);
1070 if (!win)
1071 return -ENOMEM;
1073 win->msc = msc;
1074 win->sgt = &win->_sgt;
1075 win->lockout = WIN_READY;
1076 spin_lock_init(&win->lo_lock);
1078 if (!list_empty(&msc->win_list)) {
1079 struct msc_window *prev = list_last_entry(&msc->win_list,
1080 struct msc_window,
1081 entry);
1083 win->pgoff = prev->pgoff + prev->nr_blocks;
1086 if (msc->mbuf && msc->mbuf->alloc_window)
1087 ret = msc->mbuf->alloc_window(msc->mbuf_priv, &win->sgt,
1088 nr_blocks << PAGE_SHIFT);
1089 else
1090 ret = __msc_buffer_win_alloc(win, nr_blocks);
1092 if (ret <= 0)
1093 goto err_nomem;
1095 msc_buffer_set_uc(win, ret);
1097 win->nr_segs = ret;
1098 win->nr_blocks = nr_blocks;
1100 if (list_empty(&msc->win_list)) {
1101 msc->base = msc_win_base(win);
1102 msc->base_addr = msc_win_base_dma(win);
1103 msc->cur_win = win;
1106 list_add_tail(&win->entry, &msc->win_list);
1107 msc->nr_pages += nr_blocks;
1109 return 0;
1111 err_nomem:
1112 kfree(win);
1114 return ret;
1117 static void __msc_buffer_win_free(struct msc *msc, struct msc_window *win)
1119 struct scatterlist *sg;
1120 int i;
1122 for_each_sg(win->sgt->sgl, sg, win->nr_segs, i) {
1123 struct page *page = sg_page(sg);
1125 page->mapping = NULL;
1126 dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
1127 sg_virt(sg), sg_dma_address(sg));
1129 sg_free_table(win->sgt);
1133 * msc_buffer_win_free() - free a window from MSC's window list
1134 * @msc: MSC device
1135 * @win: window to free
1137 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
1138 * to serialize, so the caller is expected to hold it.
1140 static void msc_buffer_win_free(struct msc *msc, struct msc_window *win)
1142 msc->nr_pages -= win->nr_blocks;
1144 list_del(&win->entry);
1145 if (list_empty(&msc->win_list)) {
1146 msc->base = NULL;
1147 msc->base_addr = 0;
1150 msc_buffer_set_wb(win);
1152 if (msc->mbuf && msc->mbuf->free_window)
1153 msc->mbuf->free_window(msc->mbuf_priv, win->sgt);
1154 else
1155 __msc_buffer_win_free(msc, win);
1157 kfree(win);
1161 * msc_buffer_relink() - set up block descriptors for multiblock mode
1162 * @msc: MSC device
1164 * This traverses msc::win_list, which requires msc::buf_mutex to serialize,
1165 * so the caller is expected to hold it.
1167 static void msc_buffer_relink(struct msc *msc)
1169 struct msc_window *win, *next_win;
1171 /* call with msc::mutex locked */
1172 list_for_each_entry(win, &msc->win_list, entry) {
1173 struct scatterlist *sg;
1174 unsigned int blk;
1175 u32 sw_tag = 0;
1178 * Last window's next_win should point to the first window
1179 * and MSC_SW_TAG_LASTWIN should be set.
1181 if (msc_is_last_win(win)) {
1182 sw_tag |= MSC_SW_TAG_LASTWIN;
1183 next_win = list_first_entry(&msc->win_list,
1184 struct msc_window, entry);
1185 } else {
1186 next_win = list_next_entry(win, entry);
1189 for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
1190 struct msc_block_desc *bdesc = sg_virt(sg);
1192 memset(bdesc, 0, sizeof(*bdesc));
1194 bdesc->next_win = msc_win_base_pfn(next_win);
1197 * Similarly to last window, last block should point
1198 * to the first one.
1200 if (blk == win->nr_segs - 1) {
1201 sw_tag |= MSC_SW_TAG_LASTBLK;
1202 bdesc->next_blk = msc_win_base_pfn(win);
1203 } else {
1204 dma_addr_t addr = sg_dma_address(sg_next(sg));
1206 bdesc->next_blk = PFN_DOWN(addr);
1209 bdesc->sw_tag = sw_tag;
1210 bdesc->block_sz = sg->length / 64;
1215 * Make the above writes globally visible before tracing is
1216 * enabled to make sure hardware sees them coherently.
1218 wmb();
1221 static void msc_buffer_multi_free(struct msc *msc)
1223 struct msc_window *win, *iter;
1225 list_for_each_entry_safe(win, iter, &msc->win_list, entry)
1226 msc_buffer_win_free(msc, win);
1229 static int msc_buffer_multi_alloc(struct msc *msc, unsigned long *nr_pages,
1230 unsigned int nr_wins)
1232 int ret, i;
1234 for (i = 0; i < nr_wins; i++) {
1235 ret = msc_buffer_win_alloc(msc, nr_pages[i]);
1236 if (ret) {
1237 msc_buffer_multi_free(msc);
1238 return ret;
1242 msc_buffer_relink(msc);
1244 return 0;
1248 * msc_buffer_free() - free buffers for MSC
1249 * @msc: MSC device
1251 * Free MSC's storage buffers.
1253 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex to
1254 * serialize, so the caller is expected to hold it.
1256 static void msc_buffer_free(struct msc *msc)
1258 if (msc->mode == MSC_MODE_SINGLE)
1259 msc_buffer_contig_free(msc);
1260 else if (msc->mode == MSC_MODE_MULTI)
1261 msc_buffer_multi_free(msc);
1265 * msc_buffer_alloc() - allocate a buffer for MSC
1266 * @msc: MSC device
1267 * @size: allocation size in bytes
1269 * Allocate a storage buffer for MSC, depending on the msc::mode, it will be
1270 * either done via msc_buffer_contig_alloc() for SINGLE operation mode or
1271 * msc_buffer_win_alloc() for multiblock operation. The latter allocates one
1272 * window per invocation, so in multiblock mode this can be called multiple
1273 * times for the same MSC to allocate multiple windows.
1275 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
1276 * to serialize, so the caller is expected to hold it.
1278 * Return: 0 on success, -errno otherwise.
1280 static int msc_buffer_alloc(struct msc *msc, unsigned long *nr_pages,
1281 unsigned int nr_wins)
1283 int ret;
1285 /* -1: buffer not allocated */
1286 if (atomic_read(&msc->user_count) != -1)
1287 return -EBUSY;
1289 if (msc->mode == MSC_MODE_SINGLE) {
1290 if (nr_wins != 1)
1291 return -EINVAL;
1293 ret = msc_buffer_contig_alloc(msc, nr_pages[0] << PAGE_SHIFT);
1294 } else if (msc->mode == MSC_MODE_MULTI) {
1295 ret = msc_buffer_multi_alloc(msc, nr_pages, nr_wins);
1296 } else {
1297 ret = -ENOTSUPP;
1300 if (!ret) {
1301 /* allocation should be visible before the counter goes to 0 */
1302 smp_mb__before_atomic();
1304 if (WARN_ON_ONCE(atomic_cmpxchg(&msc->user_count, -1, 0) != -1))
1305 return -EINVAL;
1308 return ret;
1312 * msc_buffer_unlocked_free_unless_used() - free a buffer unless it's in use
1313 * @msc: MSC device
1315 * This will free MSC buffer unless it is in use or there is no allocated
1316 * buffer.
1317 * Caller needs to hold msc::buf_mutex.
1319 * Return: 0 on successful deallocation or if there was no buffer to
1320 * deallocate, -EBUSY if there are active users.
1322 static int msc_buffer_unlocked_free_unless_used(struct msc *msc)
1324 int count, ret = 0;
1326 count = atomic_cmpxchg(&msc->user_count, 0, -1);
1328 /* > 0: buffer is allocated and has users */
1329 if (count > 0)
1330 ret = -EBUSY;
1331 /* 0: buffer is allocated, no users */
1332 else if (!count)
1333 msc_buffer_free(msc);
1334 /* < 0: no buffer, nothing to do */
1336 return ret;
1340 * msc_buffer_free_unless_used() - free a buffer unless it's in use
1341 * @msc: MSC device
1343 * This is a locked version of msc_buffer_unlocked_free_unless_used().
1345 static int msc_buffer_free_unless_used(struct msc *msc)
1347 int ret;
1349 mutex_lock(&msc->buf_mutex);
1350 ret = msc_buffer_unlocked_free_unless_used(msc);
1351 mutex_unlock(&msc->buf_mutex);
1353 return ret;
1357 * msc_buffer_get_page() - get MSC buffer page at a given offset
1358 * @msc: MSC device
1359 * @pgoff: page offset into the storage buffer
1361 * This traverses msc::win_list, so holding msc::buf_mutex is expected from
1362 * the caller.
1364 * Return: page if @pgoff corresponds to a valid buffer page or NULL.
1366 static struct page *msc_buffer_get_page(struct msc *msc, unsigned long pgoff)
1368 struct msc_window *win;
1369 struct scatterlist *sg;
1370 unsigned int blk;
1372 if (msc->mode == MSC_MODE_SINGLE)
1373 return msc_buffer_contig_get_page(msc, pgoff);
1375 list_for_each_entry(win, &msc->win_list, entry)
1376 if (pgoff >= win->pgoff && pgoff < win->pgoff + win->nr_blocks)
1377 goto found;
1379 return NULL;
1381 found:
1382 pgoff -= win->pgoff;
1384 for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
1385 struct page *page = sg_page(sg);
1386 size_t pgsz = PFN_DOWN(sg->length);
1388 if (pgoff < pgsz)
1389 return page + pgoff;
1391 pgoff -= pgsz;
1394 return NULL;
1398 * struct msc_win_to_user_struct - data for copy_to_user() callback
1399 * @buf: userspace buffer to copy data to
1400 * @offset: running offset
1402 struct msc_win_to_user_struct {
1403 char __user *buf;
1404 unsigned long offset;
1408 * msc_win_to_user() - iterator for msc_buffer_iterate() to copy data to user
1409 * @data: callback's private data
1410 * @src: source buffer
1411 * @len: amount of data to copy from the source buffer
1413 static unsigned long msc_win_to_user(void *data, void *src, size_t len)
1415 struct msc_win_to_user_struct *u = data;
1416 unsigned long ret;
1418 ret = copy_to_user(u->buf + u->offset, src, len);
1419 u->offset += len - ret;
1421 return ret;
1426 * file operations' callbacks
1429 static int intel_th_msc_open(struct inode *inode, struct file *file)
1431 struct intel_th_device *thdev = file->private_data;
1432 struct msc *msc = dev_get_drvdata(&thdev->dev);
1433 struct msc_iter *iter;
1435 if (!capable(CAP_SYS_RAWIO))
1436 return -EPERM;
1438 iter = msc_iter_install(msc);
1439 if (IS_ERR(iter))
1440 return PTR_ERR(iter);
1442 file->private_data = iter;
1444 return nonseekable_open(inode, file);
1447 static int intel_th_msc_release(struct inode *inode, struct file *file)
1449 struct msc_iter *iter = file->private_data;
1450 struct msc *msc = iter->msc;
1452 msc_iter_remove(iter, msc);
1454 return 0;
1457 static ssize_t
1458 msc_single_to_user(struct msc *msc, char __user *buf, loff_t off, size_t len)
1460 unsigned long size = msc->nr_pages << PAGE_SHIFT, rem = len;
1461 unsigned long start = off, tocopy = 0;
1463 if (msc->single_wrap) {
1464 start += msc->single_sz;
1465 if (start < size) {
1466 tocopy = min(rem, size - start);
1467 if (copy_to_user(buf, msc->base + start, tocopy))
1468 return -EFAULT;
1470 buf += tocopy;
1471 rem -= tocopy;
1472 start += tocopy;
1475 start &= size - 1;
1476 if (rem) {
1477 tocopy = min(rem, msc->single_sz - start);
1478 if (copy_to_user(buf, msc->base + start, tocopy))
1479 return -EFAULT;
1481 rem -= tocopy;
1484 return len - rem;
1487 if (copy_to_user(buf, msc->base + start, rem))
1488 return -EFAULT;
1490 return len;
1493 static ssize_t intel_th_msc_read(struct file *file, char __user *buf,
1494 size_t len, loff_t *ppos)
1496 struct msc_iter *iter = file->private_data;
1497 struct msc *msc = iter->msc;
1498 size_t size;
1499 loff_t off = *ppos;
1500 ssize_t ret = 0;
1502 if (!atomic_inc_unless_negative(&msc->user_count))
1503 return 0;
1505 if (msc->mode == MSC_MODE_SINGLE && !msc->single_wrap)
1506 size = msc->single_sz;
1507 else
1508 size = msc->nr_pages << PAGE_SHIFT;
1510 if (!size)
1511 goto put_count;
1513 if (off >= size)
1514 goto put_count;
1516 if (off + len >= size)
1517 len = size - off;
1519 if (msc->mode == MSC_MODE_SINGLE) {
1520 ret = msc_single_to_user(msc, buf, off, len);
1521 if (ret >= 0)
1522 *ppos += ret;
1523 } else if (msc->mode == MSC_MODE_MULTI) {
1524 struct msc_win_to_user_struct u = {
1525 .buf = buf,
1526 .offset = 0,
1529 ret = msc_buffer_iterate(iter, len, &u, msc_win_to_user);
1530 if (ret >= 0)
1531 *ppos = iter->offset;
1532 } else {
1533 ret = -ENOTSUPP;
1536 put_count:
1537 atomic_dec(&msc->user_count);
1539 return ret;
1543 * vm operations callbacks (vm_ops)
1546 static void msc_mmap_open(struct vm_area_struct *vma)
1548 struct msc_iter *iter = vma->vm_file->private_data;
1549 struct msc *msc = iter->msc;
1551 atomic_inc(&msc->mmap_count);
1554 static void msc_mmap_close(struct vm_area_struct *vma)
1556 struct msc_iter *iter = vma->vm_file->private_data;
1557 struct msc *msc = iter->msc;
1558 unsigned long pg;
1560 if (!atomic_dec_and_mutex_lock(&msc->mmap_count, &msc->buf_mutex))
1561 return;
1563 /* drop page _refcounts */
1564 for (pg = 0; pg < msc->nr_pages; pg++) {
1565 struct page *page = msc_buffer_get_page(msc, pg);
1567 if (WARN_ON_ONCE(!page))
1568 continue;
1570 if (page->mapping)
1571 page->mapping = NULL;
1574 /* last mapping -- drop user_count */
1575 atomic_dec(&msc->user_count);
1576 mutex_unlock(&msc->buf_mutex);
1579 static vm_fault_t msc_mmap_fault(struct vm_fault *vmf)
1581 struct msc_iter *iter = vmf->vma->vm_file->private_data;
1582 struct msc *msc = iter->msc;
1584 vmf->page = msc_buffer_get_page(msc, vmf->pgoff);
1585 if (!vmf->page)
1586 return VM_FAULT_SIGBUS;
1588 get_page(vmf->page);
1589 vmf->page->mapping = vmf->vma->vm_file->f_mapping;
1590 vmf->page->index = vmf->pgoff;
1592 return 0;
1595 static const struct vm_operations_struct msc_mmap_ops = {
1596 .open = msc_mmap_open,
1597 .close = msc_mmap_close,
1598 .fault = msc_mmap_fault,
1601 static int intel_th_msc_mmap(struct file *file, struct vm_area_struct *vma)
1603 unsigned long size = vma->vm_end - vma->vm_start;
1604 struct msc_iter *iter = vma->vm_file->private_data;
1605 struct msc *msc = iter->msc;
1606 int ret = -EINVAL;
1608 if (!size || offset_in_page(size))
1609 return -EINVAL;
1611 if (vma->vm_pgoff)
1612 return -EINVAL;
1614 /* grab user_count once per mmap; drop in msc_mmap_close() */
1615 if (!atomic_inc_unless_negative(&msc->user_count))
1616 return -EINVAL;
1618 if (msc->mode != MSC_MODE_SINGLE &&
1619 msc->mode != MSC_MODE_MULTI)
1620 goto out;
1622 if (size >> PAGE_SHIFT != msc->nr_pages)
1623 goto out;
1625 atomic_set(&msc->mmap_count, 1);
1626 ret = 0;
1628 out:
1629 if (ret)
1630 atomic_dec(&msc->user_count);
1632 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1633 vma->vm_flags |= VM_DONTEXPAND | VM_DONTCOPY;
1634 vma->vm_ops = &msc_mmap_ops;
1635 return ret;
1638 static const struct file_operations intel_th_msc_fops = {
1639 .open = intel_th_msc_open,
1640 .release = intel_th_msc_release,
1641 .read = intel_th_msc_read,
1642 .mmap = intel_th_msc_mmap,
1643 .llseek = no_llseek,
1644 .owner = THIS_MODULE,
1647 static void intel_th_msc_wait_empty(struct intel_th_device *thdev)
1649 struct msc *msc = dev_get_drvdata(&thdev->dev);
1650 unsigned long count;
1651 u32 reg;
1653 for (reg = 0, count = MSC_PLE_WAITLOOP_DEPTH;
1654 count && !(reg & MSCSTS_PLE); count--) {
1655 reg = __raw_readl(msc->reg_base + REG_MSU_MSC0STS);
1656 cpu_relax();
1659 if (!count)
1660 dev_dbg(msc_dev(msc), "timeout waiting for MSC0 PLE\n");
1663 static int intel_th_msc_init(struct msc *msc)
1665 atomic_set(&msc->user_count, -1);
1667 msc->mode = MSC_MODE_MULTI;
1668 mutex_init(&msc->buf_mutex);
1669 INIT_LIST_HEAD(&msc->win_list);
1670 INIT_LIST_HEAD(&msc->iter_list);
1672 msc->burst_len =
1673 (ioread32(msc->reg_base + REG_MSU_MSC0CTL) & MSC_LEN) >>
1674 __ffs(MSC_LEN);
1676 return 0;
1679 static int msc_win_switch(struct msc *msc)
1681 struct msc_window *first;
1683 if (list_empty(&msc->win_list))
1684 return -EINVAL;
1686 first = list_first_entry(&msc->win_list, struct msc_window, entry);
1688 if (msc_is_last_win(msc->cur_win))
1689 msc->cur_win = first;
1690 else
1691 msc->cur_win = list_next_entry(msc->cur_win, entry);
1693 msc->base = msc_win_base(msc->cur_win);
1694 msc->base_addr = msc_win_base_dma(msc->cur_win);
1696 intel_th_trace_switch(msc->thdev);
1698 return 0;
1702 * intel_th_msc_window_unlock - put the window back in rotation
1703 * @dev: MSC device to which this relates
1704 * @sgt: buffer's sg_table for the window, does nothing if NULL
1706 void intel_th_msc_window_unlock(struct device *dev, struct sg_table *sgt)
1708 struct msc *msc = dev_get_drvdata(dev);
1709 struct msc_window *win;
1711 if (!sgt)
1712 return;
1714 win = msc_find_window(msc, sgt, false);
1715 if (!win)
1716 return;
1718 msc_win_set_lockout(win, WIN_LOCKED, WIN_READY);
1720 EXPORT_SYMBOL_GPL(intel_th_msc_window_unlock);
1722 static void msc_work(struct work_struct *work)
1724 struct msc *msc = container_of(work, struct msc, work);
1726 intel_th_msc_deactivate(msc->thdev);
1729 static irqreturn_t intel_th_msc_interrupt(struct intel_th_device *thdev)
1731 struct msc *msc = dev_get_drvdata(&thdev->dev);
1732 u32 msusts = ioread32(msc->msu_base + REG_MSU_MSUSTS);
1733 u32 mask = msc->index ? MSUSTS_MSC1BLAST : MSUSTS_MSC0BLAST;
1734 struct msc_window *win, *next_win;
1736 if (!msc->do_irq || !msc->mbuf)
1737 return IRQ_NONE;
1739 msusts &= mask;
1741 if (!msusts)
1742 return msc->enabled ? IRQ_HANDLED : IRQ_NONE;
1744 iowrite32(msusts, msc->msu_base + REG_MSU_MSUSTS);
1746 if (!msc->enabled)
1747 return IRQ_NONE;
1749 /* grab the window before we do the switch */
1750 win = msc->cur_win;
1751 if (!win)
1752 return IRQ_HANDLED;
1753 next_win = msc_next_window(win);
1754 if (!next_win)
1755 return IRQ_HANDLED;
1757 /* next window: if READY, proceed, if LOCKED, stop the trace */
1758 if (msc_win_set_lockout(next_win, WIN_READY, WIN_INUSE)) {
1759 schedule_work(&msc->work);
1760 return IRQ_HANDLED;
1763 /* current window: INUSE -> LOCKED */
1764 msc_win_set_lockout(win, WIN_INUSE, WIN_LOCKED);
1766 msc_win_switch(msc);
1768 if (msc->mbuf && msc->mbuf->ready)
1769 msc->mbuf->ready(msc->mbuf_priv, win->sgt,
1770 msc_win_total_sz(win));
1772 return IRQ_HANDLED;
1775 static const char * const msc_mode[] = {
1776 [MSC_MODE_SINGLE] = "single",
1777 [MSC_MODE_MULTI] = "multi",
1778 [MSC_MODE_EXI] = "ExI",
1779 [MSC_MODE_DEBUG] = "debug",
1782 static ssize_t
1783 wrap_show(struct device *dev, struct device_attribute *attr, char *buf)
1785 struct msc *msc = dev_get_drvdata(dev);
1787 return scnprintf(buf, PAGE_SIZE, "%d\n", msc->wrap);
1790 static ssize_t
1791 wrap_store(struct device *dev, struct device_attribute *attr, const char *buf,
1792 size_t size)
1794 struct msc *msc = dev_get_drvdata(dev);
1795 unsigned long val;
1796 int ret;
1798 ret = kstrtoul(buf, 10, &val);
1799 if (ret)
1800 return ret;
1802 msc->wrap = !!val;
1804 return size;
1807 static DEVICE_ATTR_RW(wrap);
1809 static void msc_buffer_unassign(struct msc *msc)
1811 lockdep_assert_held(&msc->buf_mutex);
1813 if (!msc->mbuf)
1814 return;
1816 msc->mbuf->unassign(msc->mbuf_priv);
1817 msu_buffer_put(msc->mbuf);
1818 msc->mbuf_priv = NULL;
1819 msc->mbuf = NULL;
1822 static ssize_t
1823 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1825 struct msc *msc = dev_get_drvdata(dev);
1826 const char *mode = msc_mode[msc->mode];
1827 ssize_t ret;
1829 mutex_lock(&msc->buf_mutex);
1830 if (msc->mbuf)
1831 mode = msc->mbuf->name;
1832 ret = scnprintf(buf, PAGE_SIZE, "%s\n", mode);
1833 mutex_unlock(&msc->buf_mutex);
1835 return ret;
1838 static ssize_t
1839 mode_store(struct device *dev, struct device_attribute *attr, const char *buf,
1840 size_t size)
1842 const struct msu_buffer *mbuf = NULL;
1843 struct msc *msc = dev_get_drvdata(dev);
1844 size_t len = size;
1845 char *cp, *mode;
1846 int i, ret;
1848 if (!capable(CAP_SYS_RAWIO))
1849 return -EPERM;
1851 cp = memchr(buf, '\n', len);
1852 if (cp)
1853 len = cp - buf;
1855 mode = kstrndup(buf, len, GFP_KERNEL);
1856 if (!mode)
1857 return -ENOMEM;
1859 i = match_string(msc_mode, ARRAY_SIZE(msc_mode), mode);
1860 if (i >= 0) {
1861 kfree(mode);
1862 goto found;
1865 /* Buffer sinks only work with a usable IRQ */
1866 if (!msc->do_irq) {
1867 kfree(mode);
1868 return -EINVAL;
1871 mbuf = msu_buffer_get(mode);
1872 kfree(mode);
1873 if (mbuf)
1874 goto found;
1876 return -EINVAL;
1878 found:
1879 mutex_lock(&msc->buf_mutex);
1880 ret = 0;
1882 /* Same buffer: do nothing */
1883 if (mbuf && mbuf == msc->mbuf) {
1884 /* put the extra reference we just got */
1885 msu_buffer_put(mbuf);
1886 goto unlock;
1889 ret = msc_buffer_unlocked_free_unless_used(msc);
1890 if (ret)
1891 goto unlock;
1893 if (mbuf) {
1894 void *mbuf_priv = mbuf->assign(dev, &i);
1896 if (!mbuf_priv) {
1897 ret = -ENOMEM;
1898 goto unlock;
1901 msc_buffer_unassign(msc);
1902 msc->mbuf_priv = mbuf_priv;
1903 msc->mbuf = mbuf;
1904 } else {
1905 msc_buffer_unassign(msc);
1908 msc->mode = i;
1910 unlock:
1911 if (ret && mbuf)
1912 msu_buffer_put(mbuf);
1913 mutex_unlock(&msc->buf_mutex);
1915 return ret ? ret : size;
1918 static DEVICE_ATTR_RW(mode);
1920 static ssize_t
1921 nr_pages_show(struct device *dev, struct device_attribute *attr, char *buf)
1923 struct msc *msc = dev_get_drvdata(dev);
1924 struct msc_window *win;
1925 size_t count = 0;
1927 mutex_lock(&msc->buf_mutex);
1929 if (msc->mode == MSC_MODE_SINGLE)
1930 count = scnprintf(buf, PAGE_SIZE, "%ld\n", msc->nr_pages);
1931 else if (msc->mode == MSC_MODE_MULTI) {
1932 list_for_each_entry(win, &msc->win_list, entry) {
1933 count += scnprintf(buf + count, PAGE_SIZE - count,
1934 "%d%c", win->nr_blocks,
1935 msc_is_last_win(win) ? '\n' : ',');
1937 } else {
1938 count = scnprintf(buf, PAGE_SIZE, "unsupported\n");
1941 mutex_unlock(&msc->buf_mutex);
1943 return count;
1946 static ssize_t
1947 nr_pages_store(struct device *dev, struct device_attribute *attr,
1948 const char *buf, size_t size)
1950 struct msc *msc = dev_get_drvdata(dev);
1951 unsigned long val, *win = NULL, *rewin;
1952 size_t len = size;
1953 const char *p = buf;
1954 char *end, *s;
1955 int ret, nr_wins = 0;
1957 if (!capable(CAP_SYS_RAWIO))
1958 return -EPERM;
1960 ret = msc_buffer_free_unless_used(msc);
1961 if (ret)
1962 return ret;
1964 /* scan the comma-separated list of allocation sizes */
1965 end = memchr(buf, '\n', len);
1966 if (end)
1967 len = end - buf;
1969 do {
1970 end = memchr(p, ',', len);
1971 s = kstrndup(p, end ? end - p : len, GFP_KERNEL);
1972 if (!s) {
1973 ret = -ENOMEM;
1974 goto free_win;
1977 ret = kstrtoul(s, 10, &val);
1978 kfree(s);
1980 if (ret || !val)
1981 goto free_win;
1983 if (nr_wins && msc->mode == MSC_MODE_SINGLE) {
1984 ret = -EINVAL;
1985 goto free_win;
1988 nr_wins++;
1989 rewin = krealloc(win, sizeof(*win) * nr_wins, GFP_KERNEL);
1990 if (!rewin) {
1991 kfree(win);
1992 return -ENOMEM;
1995 win = rewin;
1996 win[nr_wins - 1] = val;
1998 if (!end)
1999 break;
2001 /* consume the number and the following comma, hence +1 */
2002 len -= end - p + 1;
2003 p = end + 1;
2004 } while (len);
2006 mutex_lock(&msc->buf_mutex);
2007 ret = msc_buffer_alloc(msc, win, nr_wins);
2008 mutex_unlock(&msc->buf_mutex);
2010 free_win:
2011 kfree(win);
2013 return ret ? ret : size;
2016 static DEVICE_ATTR_RW(nr_pages);
2018 static ssize_t
2019 win_switch_store(struct device *dev, struct device_attribute *attr,
2020 const char *buf, size_t size)
2022 struct msc *msc = dev_get_drvdata(dev);
2023 unsigned long val;
2024 int ret;
2026 ret = kstrtoul(buf, 10, &val);
2027 if (ret)
2028 return ret;
2030 if (val != 1)
2031 return -EINVAL;
2033 ret = -EINVAL;
2034 mutex_lock(&msc->buf_mutex);
2036 * Window switch can only happen in the "multi" mode.
2037 * If a external buffer is engaged, they have the full
2038 * control over window switching.
2040 if (msc->mode == MSC_MODE_MULTI && !msc->mbuf)
2041 ret = msc_win_switch(msc);
2042 mutex_unlock(&msc->buf_mutex);
2044 return ret ? ret : size;
2047 static DEVICE_ATTR_WO(win_switch);
2049 static struct attribute *msc_output_attrs[] = {
2050 &dev_attr_wrap.attr,
2051 &dev_attr_mode.attr,
2052 &dev_attr_nr_pages.attr,
2053 &dev_attr_win_switch.attr,
2054 NULL,
2057 static struct attribute_group msc_output_group = {
2058 .attrs = msc_output_attrs,
2061 static int intel_th_msc_probe(struct intel_th_device *thdev)
2063 struct device *dev = &thdev->dev;
2064 struct resource *res;
2065 struct msc *msc;
2066 void __iomem *base;
2067 int err;
2069 res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
2070 if (!res)
2071 return -ENODEV;
2073 base = devm_ioremap(dev, res->start, resource_size(res));
2074 if (!base)
2075 return -ENOMEM;
2077 msc = devm_kzalloc(dev, sizeof(*msc), GFP_KERNEL);
2078 if (!msc)
2079 return -ENOMEM;
2081 res = intel_th_device_get_resource(thdev, IORESOURCE_IRQ, 1);
2082 if (!res)
2083 msc->do_irq = 1;
2085 msc->index = thdev->id;
2087 msc->thdev = thdev;
2088 msc->reg_base = base + msc->index * 0x100;
2089 msc->msu_base = base;
2091 INIT_WORK(&msc->work, msc_work);
2092 err = intel_th_msc_init(msc);
2093 if (err)
2094 return err;
2096 dev_set_drvdata(dev, msc);
2098 return 0;
2101 static void intel_th_msc_remove(struct intel_th_device *thdev)
2103 struct msc *msc = dev_get_drvdata(&thdev->dev);
2104 int ret;
2106 intel_th_msc_deactivate(thdev);
2109 * Buffers should not be used at this point except if the
2110 * output character device is still open and the parent
2111 * device gets detached from its bus, which is a FIXME.
2113 ret = msc_buffer_free_unless_used(msc);
2114 WARN_ON_ONCE(ret);
2117 static struct intel_th_driver intel_th_msc_driver = {
2118 .probe = intel_th_msc_probe,
2119 .remove = intel_th_msc_remove,
2120 .irq = intel_th_msc_interrupt,
2121 .wait_empty = intel_th_msc_wait_empty,
2122 .activate = intel_th_msc_activate,
2123 .deactivate = intel_th_msc_deactivate,
2124 .fops = &intel_th_msc_fops,
2125 .attr_group = &msc_output_group,
2126 .driver = {
2127 .name = "msc",
2128 .owner = THIS_MODULE,
2132 module_driver(intel_th_msc_driver,
2133 intel_th_driver_register,
2134 intel_th_driver_unregister);
2136 MODULE_LICENSE("GPL v2");
2137 MODULE_DESCRIPTION("Intel(R) Trace Hub Memory Storage Unit driver");
2138 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");