1 // SPDX-License-Identifier: GPL-2.0
3 * Intel(R) Trace Hub Memory Storage Unit
5 * Copyright (C) 2014-2015 Intel Corporation.
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>
20 #include <linux/workqueue.h>
21 #include <linux/dma-mapping.h>
24 #include <asm/set_memory.h>
27 #include <linux/intel_th.h>
31 #define msc_dev(x) (&(x)->thdev->dev)
34 * Lockout state transitions:
35 * READY -> INUSE -+-> LOCKED -+-> READY -> etc.
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
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.
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
68 struct list_head entry;
70 enum lockout_state lockout;
72 unsigned int nr_blocks;
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
93 struct list_head entry;
95 struct msc_window *start_win;
96 struct msc_window *win;
98 struct scatterlist *start_block;
99 struct scatterlist *block;
100 unsigned int block_off;
101 unsigned int wrap_count;
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
130 void __iomem *reg_base;
131 void __iomem *msu_base;
132 struct intel_th_device *thdev;
134 const struct msu_buffer *mbuf;
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;
145 dma_addr_t base_addr;
149 /* <0: no buffer, 0: no users, >0: active users */
153 struct mutex buf_mutex;
155 struct list_head iter_list;
158 unsigned int enabled : 1,
162 unsigned int burst_len;
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))
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))
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);
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;
226 mbe = kzalloc(sizeof(*mbe), GFP_KERNEL);
230 mutex_lock(&msu_buffer_mutex);
231 if (__msu_buffer_entry_find(mbuf->name)) {
239 list_add_tail(&mbe->entry, &msu_buffer_list);
241 mutex_unlock(&msu_buffer_mutex);
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);
254 list_del(&mbe->entry);
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)
267 /* valid_dw includes the header */
268 if (!msc_data_sz(bdesc))
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
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,
317 return list_next_entry(win, entry);
320 static size_t msc_win_total_sz(struct msc_window *win)
322 struct scatterlist *sg;
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))
341 * msc_find_window() - find a window matching a given sg_table
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))
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) {
367 /* skip the empty ones */
368 if (nonempty && msc_block_is_empty(msc_win_base(win)))
379 * msc_oldest_window() - locate the window with oldest data
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))
394 win = msc_find_window(msc, msc_next_window(msc->cur_win)->sgt, true);
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)
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))
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);
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.
454 iter = ERR_PTR(-EBUSY);
460 list_add_tail(&iter->entry, &msc->iter_list);
462 mutex_unlock(&msc->buf_mutex);
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);
476 static void msc_iter_block_start(struct msc_iter *iter)
478 if (iter->start_block)
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 */
500 iter->start_win = msc_oldest_window(msc);
501 if (!iter->start_win)
504 iter->win = iter->start_win;
505 iter->start_block = NULL;
507 msc_iter_block_start(iter);
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) {
522 msc_iter_block_start(iter);
527 static int msc_iter_block_advance(struct msc_iter *iter)
532 if (iter->wrap_count && iter->block == iter->start_block) {
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);
545 if (sg_is_last(iter->block))
546 iter->block = msc_win_base_sg(iter->win);
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);
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.
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;
579 unsigned int advance;
584 /* start with the oldest window */
585 if (msc_iter_win_start(iter, msc))
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;
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;
616 tocopy -= iter->block_off;
617 src += iter->block_off;
624 remaining = fn(data, src, tocopy);
629 copied = tocopy - remaining;
631 iter->block_off += copied;
632 iter->offset += copied;
638 if (msc_iter_block_advance(iter))
647 * msc_buffer_clear_hw_header() - clear hw header for multiblock
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) {
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)
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");
687 msusts = ioread32(msc->msu_base + REG_MSU_MSUSTS);
688 iowrite32(msusts, msc->msu_base + REG_MSU_MSUSTS);
693 static void intel_th_msu_deinit(struct msc *msc)
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;
716 spin_lock_irqsave(&win->lo_lock, flags);
726 if (old == expect && new == WIN_LOCKED)
727 atomic_inc(&win->msc->user_count);
728 else if (old == expect && old == WIN_LOCKED)
729 atomic_dec(&win->msc->user_count);
732 spin_unlock_irqrestore(&win->lo_lock, flags);
735 if (expect == WIN_READY && old == WIN_LOCKED)
738 /* from intel_th_msc_window_unlock(), don't warn if not locked */
739 if (expect == WIN_LOCKED && old == new)
742 dev_warn_ratelimited(msc_dev(win->msc),
743 "expected lockout state %d, got %d\n",
750 * msc_configure() - set up MSC hardware
751 * @msc: the MSC device to configure
753 * Program storage mode, wrapping, burst length and trace buffer address
754 * into a given MSC. Then, enable tracing and set msc::enabled.
755 * The latter is serialized on msc::buf_mutex, so make sure to hold it.
757 static int msc_configure(struct msc *msc)
761 lockdep_assert_held(&msc->buf_mutex);
763 if (msc->mode > MSC_MODE_MULTI)
766 if (msc->mode == MSC_MODE_MULTI) {
767 if (msc_win_set_lockout(msc->cur_win, WIN_READY, WIN_INUSE))
770 msc_buffer_clear_hw_header(msc);
773 msc->orig_addr = ioread32(msc->reg_base + REG_MSU_MSC0BAR);
774 msc->orig_sz = ioread32(msc->reg_base + REG_MSU_MSC0SIZE);
776 reg = msc->base_addr >> PAGE_SHIFT;
777 iowrite32(reg, msc->reg_base + REG_MSU_MSC0BAR);
779 if (msc->mode == MSC_MODE_SINGLE) {
781 iowrite32(reg, msc->reg_base + REG_MSU_MSC0SIZE);
784 reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
785 reg &= ~(MSC_MODE | MSC_WRAPEN | MSC_EN | MSC_RD_HDR_OVRD);
788 reg |= msc->mode << __ffs(MSC_MODE);
789 reg |= msc->burst_len << __ffs(MSC_LEN);
794 iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
796 intel_th_msu_init(msc);
798 msc->thdev->output.multiblock = msc->mode == MSC_MODE_MULTI;
799 intel_th_trace_enable(msc->thdev);
802 if (msc->mbuf && msc->mbuf->activate)
803 msc->mbuf->activate(msc->mbuf_priv);
809 * msc_disable() - disable MSC hardware
810 * @msc: MSC device to disable
812 * If @msc is enabled, disable tracing on the switch and then disable MSC
813 * storage. Caller must hold msc::buf_mutex.
815 static void msc_disable(struct msc *msc)
817 struct msc_window *win = msc->cur_win;
820 lockdep_assert_held(&msc->buf_mutex);
822 if (msc->mode == MSC_MODE_MULTI)
823 msc_win_set_lockout(win, WIN_INUSE, WIN_LOCKED);
825 if (msc->mbuf && msc->mbuf->deactivate)
826 msc->mbuf->deactivate(msc->mbuf_priv);
827 intel_th_msu_deinit(msc);
828 intel_th_trace_disable(msc->thdev);
830 if (msc->mode == MSC_MODE_SINGLE) {
831 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
832 msc->single_wrap = !!(reg & MSCSTS_WRAPSTAT);
834 reg = ioread32(msc->reg_base + REG_MSU_MSC0MWP);
835 msc->single_sz = reg & ((msc->nr_pages << PAGE_SHIFT) - 1);
836 dev_dbg(msc_dev(msc), "MSCnMWP: %08x/%08lx, wrap: %d\n",
837 reg, msc->single_sz, msc->single_wrap);
840 reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
842 iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
844 if (msc->mbuf && msc->mbuf->ready)
845 msc->mbuf->ready(msc->mbuf_priv, win->sgt,
846 msc_win_total_sz(win));
850 iowrite32(msc->orig_addr, msc->reg_base + REG_MSU_MSC0BAR);
851 iowrite32(msc->orig_sz, msc->reg_base + REG_MSU_MSC0SIZE);
853 dev_dbg(msc_dev(msc), "MSCnNWSA: %08x\n",
854 ioread32(msc->reg_base + REG_MSU_MSC0NWSA));
856 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
857 dev_dbg(msc_dev(msc), "MSCnSTS: %08x\n", reg);
859 reg = ioread32(msc->reg_base + REG_MSU_MSUSTS);
860 reg &= msc->index ? MSUSTS_MSC1BLAST : MSUSTS_MSC0BLAST;
861 iowrite32(reg, msc->reg_base + REG_MSU_MSUSTS);
864 static int intel_th_msc_activate(struct intel_th_device *thdev)
866 struct msc *msc = dev_get_drvdata(&thdev->dev);
869 if (!atomic_inc_unless_negative(&msc->user_count))
872 mutex_lock(&msc->buf_mutex);
874 /* if there are readers, refuse */
875 if (list_empty(&msc->iter_list))
876 ret = msc_configure(msc);
878 mutex_unlock(&msc->buf_mutex);
881 atomic_dec(&msc->user_count);
886 static void intel_th_msc_deactivate(struct intel_th_device *thdev)
888 struct msc *msc = dev_get_drvdata(&thdev->dev);
890 mutex_lock(&msc->buf_mutex);
893 atomic_dec(&msc->user_count);
895 mutex_unlock(&msc->buf_mutex);
899 * msc_buffer_contig_alloc() - allocate a contiguous buffer for SINGLE mode
901 * @size: allocation size in bytes
903 * This modifies msc::base, which requires msc::buf_mutex to serialize, so the
904 * caller is expected to hold it.
906 * Return: 0 on success, -errno otherwise.
908 static int msc_buffer_contig_alloc(struct msc *msc, unsigned long size)
910 unsigned long nr_pages = size >> PAGE_SHIFT;
911 unsigned int order = get_order(size);
918 ret = sg_alloc_table(&msc->single_sgt, 1, GFP_KERNEL);
923 page = alloc_pages(GFP_KERNEL | __GFP_ZERO | GFP_DMA32, order);
927 split_page(page, order);
928 sg_set_buf(msc->single_sgt.sgl, page_address(page), size);
930 ret = dma_map_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl, 1,
935 msc->nr_pages = nr_pages;
936 msc->base = page_address(page);
937 msc->base_addr = sg_dma_address(msc->single_sgt.sgl);
942 __free_pages(page, order);
945 sg_free_table(&msc->single_sgt);
952 * msc_buffer_contig_free() - free a contiguous buffer
953 * @msc: MSC configured in SINGLE mode
955 static void msc_buffer_contig_free(struct msc *msc)
959 dma_unmap_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl,
961 sg_free_table(&msc->single_sgt);
963 for (off = 0; off < msc->nr_pages << PAGE_SHIFT; off += PAGE_SIZE) {
964 struct page *page = virt_to_page(msc->base + off);
966 page->mapping = NULL;
974 * msc_buffer_contig_get_page() - find a page at a given offset
975 * @msc: MSC configured in SINGLE mode
976 * @pgoff: page offset
978 * Return: page, if @pgoff is within the range, NULL otherwise.
980 static struct page *msc_buffer_contig_get_page(struct msc *msc,
983 if (pgoff >= msc->nr_pages)
986 return virt_to_page(msc->base + (pgoff << PAGE_SHIFT));
989 static int __msc_buffer_win_alloc(struct msc_window *win,
990 unsigned int nr_segs)
992 struct scatterlist *sg_ptr;
996 ret = sg_alloc_table(win->sgt, nr_segs, GFP_KERNEL);
1000 for_each_sg(win->sgt->sgl, sg_ptr, nr_segs, i) {
1001 block = dma_alloc_coherent(msc_dev(win->msc)->parent->parent,
1002 PAGE_SIZE, &sg_dma_address(sg_ptr),
1007 sg_set_buf(sg_ptr, block, PAGE_SIZE);
1013 for_each_sg(win->sgt->sgl, sg_ptr, i, ret)
1014 dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
1015 sg_virt(sg_ptr), sg_dma_address(sg_ptr));
1017 sg_free_table(win->sgt);
1023 static void msc_buffer_set_uc(struct msc_window *win, unsigned int nr_segs)
1025 struct scatterlist *sg_ptr;
1028 for_each_sg(win->sgt->sgl, sg_ptr, nr_segs, i) {
1029 /* Set the page as uncached */
1030 set_memory_uc((unsigned long)sg_virt(sg_ptr),
1031 PFN_DOWN(sg_ptr->length));
1035 static void msc_buffer_set_wb(struct msc_window *win)
1037 struct scatterlist *sg_ptr;
1040 for_each_sg(win->sgt->sgl, sg_ptr, win->nr_segs, i) {
1041 /* Reset the page to write-back */
1042 set_memory_wb((unsigned long)sg_virt(sg_ptr),
1043 PFN_DOWN(sg_ptr->length));
1048 msc_buffer_set_uc(struct msc_window *win, unsigned int nr_segs) {}
1049 static inline void msc_buffer_set_wb(struct msc_window *win) {}
1050 #endif /* CONFIG_X86 */
1053 * msc_buffer_win_alloc() - alloc a window for a multiblock mode
1055 * @nr_blocks: number of pages in this window
1057 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
1058 * to serialize, so the caller is expected to hold it.
1060 * Return: 0 on success, -errno otherwise.
1062 static int msc_buffer_win_alloc(struct msc *msc, unsigned int nr_blocks)
1064 struct msc_window *win;
1070 win = kzalloc(sizeof(*win), GFP_KERNEL);
1075 win->sgt = &win->_sgt;
1076 win->lockout = WIN_READY;
1077 spin_lock_init(&win->lo_lock);
1079 if (!list_empty(&msc->win_list)) {
1080 struct msc_window *prev = list_last_entry(&msc->win_list,
1084 win->pgoff = prev->pgoff + prev->nr_blocks;
1087 if (msc->mbuf && msc->mbuf->alloc_window)
1088 ret = msc->mbuf->alloc_window(msc->mbuf_priv, &win->sgt,
1089 nr_blocks << PAGE_SHIFT);
1091 ret = __msc_buffer_win_alloc(win, nr_blocks);
1096 msc_buffer_set_uc(win, ret);
1099 win->nr_blocks = nr_blocks;
1101 if (list_empty(&msc->win_list)) {
1102 msc->base = msc_win_base(win);
1103 msc->base_addr = msc_win_base_dma(win);
1107 list_add_tail(&win->entry, &msc->win_list);
1108 msc->nr_pages += nr_blocks;
1118 static void __msc_buffer_win_free(struct msc *msc, struct msc_window *win)
1120 struct scatterlist *sg;
1123 for_each_sg(win->sgt->sgl, sg, win->nr_segs, i) {
1124 struct page *page = sg_page(sg);
1126 page->mapping = NULL;
1127 dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
1128 sg_virt(sg), sg_dma_address(sg));
1130 sg_free_table(win->sgt);
1134 * msc_buffer_win_free() - free a window from MSC's window list
1136 * @win: window to free
1138 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
1139 * to serialize, so the caller is expected to hold it.
1141 static void msc_buffer_win_free(struct msc *msc, struct msc_window *win)
1143 msc->nr_pages -= win->nr_blocks;
1145 list_del(&win->entry);
1146 if (list_empty(&msc->win_list)) {
1151 msc_buffer_set_wb(win);
1153 if (msc->mbuf && msc->mbuf->free_window)
1154 msc->mbuf->free_window(msc->mbuf_priv, win->sgt);
1156 __msc_buffer_win_free(msc, win);
1162 * msc_buffer_relink() - set up block descriptors for multiblock mode
1165 * This traverses msc::win_list, which requires msc::buf_mutex to serialize,
1166 * so the caller is expected to hold it.
1168 static void msc_buffer_relink(struct msc *msc)
1170 struct msc_window *win, *next_win;
1172 /* call with msc::mutex locked */
1173 list_for_each_entry(win, &msc->win_list, entry) {
1174 struct scatterlist *sg;
1179 * Last window's next_win should point to the first window
1180 * and MSC_SW_TAG_LASTWIN should be set.
1182 if (msc_is_last_win(win)) {
1183 sw_tag |= MSC_SW_TAG_LASTWIN;
1184 next_win = list_first_entry(&msc->win_list,
1185 struct msc_window, entry);
1187 next_win = list_next_entry(win, entry);
1190 for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
1191 struct msc_block_desc *bdesc = sg_virt(sg);
1193 memset(bdesc, 0, sizeof(*bdesc));
1195 bdesc->next_win = msc_win_base_pfn(next_win);
1198 * Similarly to last window, last block should point
1201 if (blk == win->nr_segs - 1) {
1202 sw_tag |= MSC_SW_TAG_LASTBLK;
1203 bdesc->next_blk = msc_win_base_pfn(win);
1205 dma_addr_t addr = sg_dma_address(sg_next(sg));
1207 bdesc->next_blk = PFN_DOWN(addr);
1210 bdesc->sw_tag = sw_tag;
1211 bdesc->block_sz = sg->length / 64;
1216 * Make the above writes globally visible before tracing is
1217 * enabled to make sure hardware sees them coherently.
1222 static void msc_buffer_multi_free(struct msc *msc)
1224 struct msc_window *win, *iter;
1226 list_for_each_entry_safe(win, iter, &msc->win_list, entry)
1227 msc_buffer_win_free(msc, win);
1230 static int msc_buffer_multi_alloc(struct msc *msc, unsigned long *nr_pages,
1231 unsigned int nr_wins)
1235 for (i = 0; i < nr_wins; i++) {
1236 ret = msc_buffer_win_alloc(msc, nr_pages[i]);
1238 msc_buffer_multi_free(msc);
1243 msc_buffer_relink(msc);
1249 * msc_buffer_free() - free buffers for MSC
1252 * Free MSC's storage buffers.
1254 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex to
1255 * serialize, so the caller is expected to hold it.
1257 static void msc_buffer_free(struct msc *msc)
1259 if (msc->mode == MSC_MODE_SINGLE)
1260 msc_buffer_contig_free(msc);
1261 else if (msc->mode == MSC_MODE_MULTI)
1262 msc_buffer_multi_free(msc);
1266 * msc_buffer_alloc() - allocate a buffer for MSC
1268 * @size: allocation size in bytes
1270 * Allocate a storage buffer for MSC, depending on the msc::mode, it will be
1271 * either done via msc_buffer_contig_alloc() for SINGLE operation mode or
1272 * msc_buffer_win_alloc() for multiblock operation. The latter allocates one
1273 * window per invocation, so in multiblock mode this can be called multiple
1274 * times for the same MSC to allocate multiple windows.
1276 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
1277 * to serialize, so the caller is expected to hold it.
1279 * Return: 0 on success, -errno otherwise.
1281 static int msc_buffer_alloc(struct msc *msc, unsigned long *nr_pages,
1282 unsigned int nr_wins)
1286 /* -1: buffer not allocated */
1287 if (atomic_read(&msc->user_count) != -1)
1290 if (msc->mode == MSC_MODE_SINGLE) {
1294 ret = msc_buffer_contig_alloc(msc, nr_pages[0] << PAGE_SHIFT);
1295 } else if (msc->mode == MSC_MODE_MULTI) {
1296 ret = msc_buffer_multi_alloc(msc, nr_pages, nr_wins);
1302 /* allocation should be visible before the counter goes to 0 */
1303 smp_mb__before_atomic();
1305 if (WARN_ON_ONCE(atomic_cmpxchg(&msc->user_count, -1, 0) != -1))
1313 * msc_buffer_unlocked_free_unless_used() - free a buffer unless it's in use
1316 * This will free MSC buffer unless it is in use or there is no allocated
1318 * Caller needs to hold msc::buf_mutex.
1320 * Return: 0 on successful deallocation or if there was no buffer to
1321 * deallocate, -EBUSY if there are active users.
1323 static int msc_buffer_unlocked_free_unless_used(struct msc *msc)
1327 count = atomic_cmpxchg(&msc->user_count, 0, -1);
1329 /* > 0: buffer is allocated and has users */
1332 /* 0: buffer is allocated, no users */
1334 msc_buffer_free(msc);
1335 /* < 0: no buffer, nothing to do */
1341 * msc_buffer_free_unless_used() - free a buffer unless it's in use
1344 * This is a locked version of msc_buffer_unlocked_free_unless_used().
1346 static int msc_buffer_free_unless_used(struct msc *msc)
1350 mutex_lock(&msc->buf_mutex);
1351 ret = msc_buffer_unlocked_free_unless_used(msc);
1352 mutex_unlock(&msc->buf_mutex);
1358 * msc_buffer_get_page() - get MSC buffer page at a given offset
1360 * @pgoff: page offset into the storage buffer
1362 * This traverses msc::win_list, so holding msc::buf_mutex is expected from
1365 * Return: page if @pgoff corresponds to a valid buffer page or NULL.
1367 static struct page *msc_buffer_get_page(struct msc *msc, unsigned long pgoff)
1369 struct msc_window *win;
1370 struct scatterlist *sg;
1373 if (msc->mode == MSC_MODE_SINGLE)
1374 return msc_buffer_contig_get_page(msc, pgoff);
1376 list_for_each_entry(win, &msc->win_list, entry)
1377 if (pgoff >= win->pgoff && pgoff < win->pgoff + win->nr_blocks)
1383 pgoff -= win->pgoff;
1385 for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
1386 struct page *page = sg_page(sg);
1387 size_t pgsz = PFN_DOWN(sg->length);
1390 return page + pgoff;
1399 * struct msc_win_to_user_struct - data for copy_to_user() callback
1400 * @buf: userspace buffer to copy data to
1401 * @offset: running offset
1403 struct msc_win_to_user_struct {
1405 unsigned long offset;
1409 * msc_win_to_user() - iterator for msc_buffer_iterate() to copy data to user
1410 * @data: callback's private data
1411 * @src: source buffer
1412 * @len: amount of data to copy from the source buffer
1414 static unsigned long msc_win_to_user(void *data, void *src, size_t len)
1416 struct msc_win_to_user_struct *u = data;
1419 ret = copy_to_user(u->buf + u->offset, src, len);
1420 u->offset += len - ret;
1427 * file operations' callbacks
1430 static int intel_th_msc_open(struct inode *inode, struct file *file)
1432 struct intel_th_device *thdev = file->private_data;
1433 struct msc *msc = dev_get_drvdata(&thdev->dev);
1434 struct msc_iter *iter;
1436 if (!capable(CAP_SYS_RAWIO))
1439 iter = msc_iter_install(msc);
1441 return PTR_ERR(iter);
1443 file->private_data = iter;
1445 return nonseekable_open(inode, file);
1448 static int intel_th_msc_release(struct inode *inode, struct file *file)
1450 struct msc_iter *iter = file->private_data;
1451 struct msc *msc = iter->msc;
1453 msc_iter_remove(iter, msc);
1459 msc_single_to_user(struct msc *msc, char __user *buf, loff_t off, size_t len)
1461 unsigned long size = msc->nr_pages << PAGE_SHIFT, rem = len;
1462 unsigned long start = off, tocopy = 0;
1464 if (msc->single_wrap) {
1465 start += msc->single_sz;
1467 tocopy = min(rem, size - start);
1468 if (copy_to_user(buf, msc->base + start, tocopy))
1478 tocopy = min(rem, msc->single_sz - start);
1479 if (copy_to_user(buf, msc->base + start, tocopy))
1488 if (copy_to_user(buf, msc->base + start, rem))
1494 static ssize_t intel_th_msc_read(struct file *file, char __user *buf,
1495 size_t len, loff_t *ppos)
1497 struct msc_iter *iter = file->private_data;
1498 struct msc *msc = iter->msc;
1503 if (!atomic_inc_unless_negative(&msc->user_count))
1506 if (msc->mode == MSC_MODE_SINGLE && !msc->single_wrap)
1507 size = msc->single_sz;
1509 size = msc->nr_pages << PAGE_SHIFT;
1517 if (off + len >= size)
1520 if (msc->mode == MSC_MODE_SINGLE) {
1521 ret = msc_single_to_user(msc, buf, off, len);
1524 } else if (msc->mode == MSC_MODE_MULTI) {
1525 struct msc_win_to_user_struct u = {
1530 ret = msc_buffer_iterate(iter, len, &u, msc_win_to_user);
1532 *ppos = iter->offset;
1538 atomic_dec(&msc->user_count);
1544 * vm operations callbacks (vm_ops)
1547 static void msc_mmap_open(struct vm_area_struct *vma)
1549 struct msc_iter *iter = vma->vm_file->private_data;
1550 struct msc *msc = iter->msc;
1552 atomic_inc(&msc->mmap_count);
1555 static void msc_mmap_close(struct vm_area_struct *vma)
1557 struct msc_iter *iter = vma->vm_file->private_data;
1558 struct msc *msc = iter->msc;
1561 if (!atomic_dec_and_mutex_lock(&msc->mmap_count, &msc->buf_mutex))
1564 /* drop page _refcounts */
1565 for (pg = 0; pg < msc->nr_pages; pg++) {
1566 struct page *page = msc_buffer_get_page(msc, pg);
1568 if (WARN_ON_ONCE(!page))
1572 page->mapping = NULL;
1575 /* last mapping -- drop user_count */
1576 atomic_dec(&msc->user_count);
1577 mutex_unlock(&msc->buf_mutex);
1580 static vm_fault_t msc_mmap_fault(struct vm_fault *vmf)
1582 struct msc_iter *iter = vmf->vma->vm_file->private_data;
1583 struct msc *msc = iter->msc;
1585 vmf->page = msc_buffer_get_page(msc, vmf->pgoff);
1587 return VM_FAULT_SIGBUS;
1589 get_page(vmf->page);
1590 vmf->page->mapping = vmf->vma->vm_file->f_mapping;
1591 vmf->page->index = vmf->pgoff;
1596 static const struct vm_operations_struct msc_mmap_ops = {
1597 .open = msc_mmap_open,
1598 .close = msc_mmap_close,
1599 .fault = msc_mmap_fault,
1602 static int intel_th_msc_mmap(struct file *file, struct vm_area_struct *vma)
1604 unsigned long size = vma->vm_end - vma->vm_start;
1605 struct msc_iter *iter = vma->vm_file->private_data;
1606 struct msc *msc = iter->msc;
1609 if (!size || offset_in_page(size))
1615 /* grab user_count once per mmap; drop in msc_mmap_close() */
1616 if (!atomic_inc_unless_negative(&msc->user_count))
1619 if (msc->mode != MSC_MODE_SINGLE &&
1620 msc->mode != MSC_MODE_MULTI)
1623 if (size >> PAGE_SHIFT != msc->nr_pages)
1626 atomic_set(&msc->mmap_count, 1);
1631 atomic_dec(&msc->user_count);
1633 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1634 vma->vm_flags |= VM_DONTEXPAND | VM_DONTCOPY;
1635 vma->vm_ops = &msc_mmap_ops;
1639 static const struct file_operations intel_th_msc_fops = {
1640 .open = intel_th_msc_open,
1641 .release = intel_th_msc_release,
1642 .read = intel_th_msc_read,
1643 .mmap = intel_th_msc_mmap,
1644 .llseek = no_llseek,
1645 .owner = THIS_MODULE,
1648 static void intel_th_msc_wait_empty(struct intel_th_device *thdev)
1650 struct msc *msc = dev_get_drvdata(&thdev->dev);
1651 unsigned long count;
1654 for (reg = 0, count = MSC_PLE_WAITLOOP_DEPTH;
1655 count && !(reg & MSCSTS_PLE); count--) {
1656 reg = __raw_readl(msc->reg_base + REG_MSU_MSC0STS);
1661 dev_dbg(msc_dev(msc), "timeout waiting for MSC0 PLE\n");
1664 static int intel_th_msc_init(struct msc *msc)
1666 atomic_set(&msc->user_count, -1);
1668 msc->mode = MSC_MODE_MULTI;
1669 mutex_init(&msc->buf_mutex);
1670 INIT_LIST_HEAD(&msc->win_list);
1671 INIT_LIST_HEAD(&msc->iter_list);
1674 (ioread32(msc->reg_base + REG_MSU_MSC0CTL) & MSC_LEN) >>
1680 static int msc_win_switch(struct msc *msc)
1682 struct msc_window *first;
1684 if (list_empty(&msc->win_list))
1687 first = list_first_entry(&msc->win_list, struct msc_window, entry);
1689 if (msc_is_last_win(msc->cur_win))
1690 msc->cur_win = first;
1692 msc->cur_win = list_next_entry(msc->cur_win, entry);
1694 msc->base = msc_win_base(msc->cur_win);
1695 msc->base_addr = msc_win_base_dma(msc->cur_win);
1697 intel_th_trace_switch(msc->thdev);
1703 * intel_th_msc_window_unlock - put the window back in rotation
1704 * @dev: MSC device to which this relates
1705 * @sgt: buffer's sg_table for the window, does nothing if NULL
1707 void intel_th_msc_window_unlock(struct device *dev, struct sg_table *sgt)
1709 struct msc *msc = dev_get_drvdata(dev);
1710 struct msc_window *win;
1715 win = msc_find_window(msc, sgt, false);
1719 msc_win_set_lockout(win, WIN_LOCKED, WIN_READY);
1721 EXPORT_SYMBOL_GPL(intel_th_msc_window_unlock);
1723 static void msc_work(struct work_struct *work)
1725 struct msc *msc = container_of(work, struct msc, work);
1727 intel_th_msc_deactivate(msc->thdev);
1730 static irqreturn_t intel_th_msc_interrupt(struct intel_th_device *thdev)
1732 struct msc *msc = dev_get_drvdata(&thdev->dev);
1733 u32 msusts = ioread32(msc->msu_base + REG_MSU_MSUSTS);
1734 u32 mask = msc->index ? MSUSTS_MSC1BLAST : MSUSTS_MSC0BLAST;
1735 struct msc_window *win, *next_win;
1737 if (!msc->do_irq || !msc->mbuf)
1743 return msc->enabled ? IRQ_HANDLED : IRQ_NONE;
1745 iowrite32(msusts, msc->msu_base + REG_MSU_MSUSTS);
1750 /* grab the window before we do the switch */
1754 next_win = msc_next_window(win);
1758 /* next window: if READY, proceed, if LOCKED, stop the trace */
1759 if (msc_win_set_lockout(next_win, WIN_READY, WIN_INUSE)) {
1760 schedule_work(&msc->work);
1764 /* current window: INUSE -> LOCKED */
1765 msc_win_set_lockout(win, WIN_INUSE, WIN_LOCKED);
1767 msc_win_switch(msc);
1769 if (msc->mbuf && msc->mbuf->ready)
1770 msc->mbuf->ready(msc->mbuf_priv, win->sgt,
1771 msc_win_total_sz(win));
1776 static const char * const msc_mode[] = {
1777 [MSC_MODE_SINGLE] = "single",
1778 [MSC_MODE_MULTI] = "multi",
1779 [MSC_MODE_EXI] = "ExI",
1780 [MSC_MODE_DEBUG] = "debug",
1784 wrap_show(struct device *dev, struct device_attribute *attr, char *buf)
1786 struct msc *msc = dev_get_drvdata(dev);
1788 return scnprintf(buf, PAGE_SIZE, "%d\n", msc->wrap);
1792 wrap_store(struct device *dev, struct device_attribute *attr, const char *buf,
1795 struct msc *msc = dev_get_drvdata(dev);
1799 ret = kstrtoul(buf, 10, &val);
1808 static DEVICE_ATTR_RW(wrap);
1810 static void msc_buffer_unassign(struct msc *msc)
1812 lockdep_assert_held(&msc->buf_mutex);
1817 msc->mbuf->unassign(msc->mbuf_priv);
1818 msu_buffer_put(msc->mbuf);
1819 msc->mbuf_priv = NULL;
1824 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1826 struct msc *msc = dev_get_drvdata(dev);
1827 const char *mode = msc_mode[msc->mode];
1830 mutex_lock(&msc->buf_mutex);
1832 mode = msc->mbuf->name;
1833 ret = scnprintf(buf, PAGE_SIZE, "%s\n", mode);
1834 mutex_unlock(&msc->buf_mutex);
1840 mode_store(struct device *dev, struct device_attribute *attr, const char *buf,
1843 const struct msu_buffer *mbuf = NULL;
1844 struct msc *msc = dev_get_drvdata(dev);
1849 if (!capable(CAP_SYS_RAWIO))
1852 cp = memchr(buf, '\n', len);
1856 mode = kstrndup(buf, len, GFP_KERNEL);
1860 i = match_string(msc_mode, ARRAY_SIZE(msc_mode), mode);
1866 /* Buffer sinks only work with a usable IRQ */
1872 mbuf = msu_buffer_get(mode);
1880 mutex_lock(&msc->buf_mutex);
1883 /* Same buffer: do nothing */
1884 if (mbuf && mbuf == msc->mbuf) {
1885 /* put the extra reference we just got */
1886 msu_buffer_put(mbuf);
1890 ret = msc_buffer_unlocked_free_unless_used(msc);
1895 void *mbuf_priv = mbuf->assign(dev, &i);
1902 msc_buffer_unassign(msc);
1903 msc->mbuf_priv = mbuf_priv;
1906 msc_buffer_unassign(msc);
1913 msu_buffer_put(mbuf);
1914 mutex_unlock(&msc->buf_mutex);
1916 return ret ? ret : size;
1919 static DEVICE_ATTR_RW(mode);
1922 nr_pages_show(struct device *dev, struct device_attribute *attr, char *buf)
1924 struct msc *msc = dev_get_drvdata(dev);
1925 struct msc_window *win;
1928 mutex_lock(&msc->buf_mutex);
1930 if (msc->mode == MSC_MODE_SINGLE)
1931 count = scnprintf(buf, PAGE_SIZE, "%ld\n", msc->nr_pages);
1932 else if (msc->mode == MSC_MODE_MULTI) {
1933 list_for_each_entry(win, &msc->win_list, entry) {
1934 count += scnprintf(buf + count, PAGE_SIZE - count,
1935 "%d%c", win->nr_blocks,
1936 msc_is_last_win(win) ? '\n' : ',');
1939 count = scnprintf(buf, PAGE_SIZE, "unsupported\n");
1942 mutex_unlock(&msc->buf_mutex);
1948 nr_pages_store(struct device *dev, struct device_attribute *attr,
1949 const char *buf, size_t size)
1951 struct msc *msc = dev_get_drvdata(dev);
1952 unsigned long val, *win = NULL, *rewin;
1954 const char *p = buf;
1956 int ret, nr_wins = 0;
1958 if (!capable(CAP_SYS_RAWIO))
1961 ret = msc_buffer_free_unless_used(msc);
1965 /* scan the comma-separated list of allocation sizes */
1966 end = memchr(buf, '\n', len);
1971 end = memchr(p, ',', len);
1972 s = kstrndup(p, end ? end - p : len, GFP_KERNEL);
1978 ret = kstrtoul(s, 10, &val);
1984 if (nr_wins && msc->mode == MSC_MODE_SINGLE) {
1990 rewin = krealloc(win, sizeof(*win) * nr_wins, GFP_KERNEL);
1997 win[nr_wins - 1] = val;
2002 /* consume the number and the following comma, hence +1 */
2007 mutex_lock(&msc->buf_mutex);
2008 ret = msc_buffer_alloc(msc, win, nr_wins);
2009 mutex_unlock(&msc->buf_mutex);
2014 return ret ? ret : size;
2017 static DEVICE_ATTR_RW(nr_pages);
2020 win_switch_store(struct device *dev, struct device_attribute *attr,
2021 const char *buf, size_t size)
2023 struct msc *msc = dev_get_drvdata(dev);
2027 ret = kstrtoul(buf, 10, &val);
2035 mutex_lock(&msc->buf_mutex);
2037 * Window switch can only happen in the "multi" mode.
2038 * If a external buffer is engaged, they have the full
2039 * control over window switching.
2041 if (msc->mode == MSC_MODE_MULTI && !msc->mbuf)
2042 ret = msc_win_switch(msc);
2043 mutex_unlock(&msc->buf_mutex);
2045 return ret ? ret : size;
2048 static DEVICE_ATTR_WO(win_switch);
2050 static struct attribute *msc_output_attrs[] = {
2051 &dev_attr_wrap.attr,
2052 &dev_attr_mode.attr,
2053 &dev_attr_nr_pages.attr,
2054 &dev_attr_win_switch.attr,
2058 static struct attribute_group msc_output_group = {
2059 .attrs = msc_output_attrs,
2062 static int intel_th_msc_probe(struct intel_th_device *thdev)
2064 struct device *dev = &thdev->dev;
2065 struct resource *res;
2070 res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
2074 base = devm_ioremap(dev, res->start, resource_size(res));
2078 msc = devm_kzalloc(dev, sizeof(*msc), GFP_KERNEL);
2082 res = intel_th_device_get_resource(thdev, IORESOURCE_IRQ, 1);
2086 msc->index = thdev->id;
2089 msc->reg_base = base + msc->index * 0x100;
2090 msc->msu_base = base;
2092 INIT_WORK(&msc->work, msc_work);
2093 err = intel_th_msc_init(msc);
2097 dev_set_drvdata(dev, msc);
2102 static void intel_th_msc_remove(struct intel_th_device *thdev)
2104 struct msc *msc = dev_get_drvdata(&thdev->dev);
2107 intel_th_msc_deactivate(thdev);
2110 * Buffers should not be used at this point except if the
2111 * output character device is still open and the parent
2112 * device gets detached from its bus, which is a FIXME.
2114 ret = msc_buffer_free_unless_used(msc);
2118 static struct intel_th_driver intel_th_msc_driver = {
2119 .probe = intel_th_msc_probe,
2120 .remove = intel_th_msc_remove,
2121 .irq = intel_th_msc_interrupt,
2122 .wait_empty = intel_th_msc_wait_empty,
2123 .activate = intel_th_msc_activate,
2124 .deactivate = intel_th_msc_deactivate,
2125 .fops = &intel_th_msc_fops,
2126 .attr_group = &msc_output_group,
2129 .owner = THIS_MODULE,
2133 module_driver(intel_th_msc_driver,
2134 intel_th_driver_register,
2135 intel_th_driver_unregister);
2137 MODULE_LICENSE("GPL v2");
2138 MODULE_DESCRIPTION("Intel(R) Trace Hub Memory Storage Unit driver");
2139 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");