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 struct msc_window *switch_on_unlock;
142 unsigned long nr_pages;
143 unsigned long single_sz;
144 unsigned int single_wrap : 1;
146 dma_addr_t base_addr;
150 /* <0: no buffer, 0: no users, >0: active users */
154 struct mutex buf_mutex;
156 struct list_head iter_list;
161 unsigned int enabled : 1,
166 unsigned int burst_len;
170 static LIST_HEAD(msu_buffer_list);
171 static DEFINE_MUTEX(msu_buffer_mutex);
174 * struct msu_buffer_entry - internal MSU buffer bookkeeping
175 * @entry: link to msu_buffer_list
176 * @mbuf: MSU buffer object
177 * @owner: module that provides this MSU buffer
179 struct msu_buffer_entry {
180 struct list_head entry;
181 const struct msu_buffer *mbuf;
182 struct module *owner;
185 static struct msu_buffer_entry *__msu_buffer_entry_find(const char *name)
187 struct msu_buffer_entry *mbe;
189 lockdep_assert_held(&msu_buffer_mutex);
191 list_for_each_entry(mbe, &msu_buffer_list, entry) {
192 if (!strcmp(mbe->mbuf->name, name))
199 static const struct msu_buffer *
200 msu_buffer_get(const char *name)
202 struct msu_buffer_entry *mbe;
204 mutex_lock(&msu_buffer_mutex);
205 mbe = __msu_buffer_entry_find(name);
206 if (mbe && !try_module_get(mbe->owner))
208 mutex_unlock(&msu_buffer_mutex);
210 return mbe ? mbe->mbuf : NULL;
213 static void msu_buffer_put(const struct msu_buffer *mbuf)
215 struct msu_buffer_entry *mbe;
217 mutex_lock(&msu_buffer_mutex);
218 mbe = __msu_buffer_entry_find(mbuf->name);
220 module_put(mbe->owner);
221 mutex_unlock(&msu_buffer_mutex);
224 int intel_th_msu_buffer_register(const struct msu_buffer *mbuf,
225 struct module *owner)
227 struct msu_buffer_entry *mbe;
230 mbe = kzalloc(sizeof(*mbe), GFP_KERNEL);
234 mutex_lock(&msu_buffer_mutex);
235 if (__msu_buffer_entry_find(mbuf->name)) {
243 list_add_tail(&mbe->entry, &msu_buffer_list);
245 mutex_unlock(&msu_buffer_mutex);
249 EXPORT_SYMBOL_GPL(intel_th_msu_buffer_register);
251 void intel_th_msu_buffer_unregister(const struct msu_buffer *mbuf)
253 struct msu_buffer_entry *mbe;
255 mutex_lock(&msu_buffer_mutex);
256 mbe = __msu_buffer_entry_find(mbuf->name);
258 list_del(&mbe->entry);
261 mutex_unlock(&msu_buffer_mutex);
263 EXPORT_SYMBOL_GPL(intel_th_msu_buffer_unregister);
265 static inline bool msc_block_is_empty(struct msc_block_desc *bdesc)
267 /* header hasn't been written */
268 if (!bdesc->valid_dw)
271 /* valid_dw includes the header */
272 if (!msc_data_sz(bdesc))
278 static inline struct scatterlist *msc_win_base_sg(struct msc_window *win)
280 return win->sgt->sgl;
283 static inline struct msc_block_desc *msc_win_base(struct msc_window *win)
285 return sg_virt(msc_win_base_sg(win));
288 static inline dma_addr_t msc_win_base_dma(struct msc_window *win)
290 return sg_dma_address(msc_win_base_sg(win));
293 static inline unsigned long
294 msc_win_base_pfn(struct msc_window *win)
296 return PFN_DOWN(msc_win_base_dma(win));
300 * msc_is_last_win() - check if a window is the last one for a given MSC
302 * Return: true if @win is the last window in MSC's multiblock buffer
304 static inline bool msc_is_last_win(struct msc_window *win)
306 return win->entry.next == &win->msc->win_list;
310 * msc_next_window() - return next window in the multiblock buffer
311 * @win: current window
313 * Return: window following the current one
315 static struct msc_window *msc_next_window(struct msc_window *win)
317 if (msc_is_last_win(win))
318 return list_first_entry(&win->msc->win_list, struct msc_window,
321 return list_next_entry(win, entry);
324 static size_t msc_win_total_sz(struct msc_window *win)
326 struct scatterlist *sg;
330 for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
331 struct msc_block_desc *bdesc = sg_virt(sg);
333 if (msc_block_wrapped(bdesc))
334 return (size_t)win->nr_blocks << PAGE_SHIFT;
336 size += msc_total_sz(bdesc);
337 if (msc_block_last_written(bdesc))
345 * msc_find_window() - find a window matching a given sg_table
347 * @sgt: SG table of the window
348 * @nonempty: skip over empty windows
350 * Return: MSC window structure pointer or NULL if the window
351 * could not be found.
353 static struct msc_window *
354 msc_find_window(struct msc *msc, struct sg_table *sgt, bool nonempty)
356 struct msc_window *win;
357 unsigned int found = 0;
359 if (list_empty(&msc->win_list))
363 * we might need a radix tree for this, depending on how
364 * many windows a typical user would allocate; ideally it's
365 * something like 2, in which case we're good
367 list_for_each_entry(win, &msc->win_list, entry) {
371 /* skip the empty ones */
372 if (nonempty && msc_block_is_empty(msc_win_base(win)))
383 * msc_oldest_window() - locate the window with oldest data
386 * This should only be used in multiblock mode. Caller should hold the
387 * msc::user_count reference.
389 * Return: the oldest window with valid data
391 static struct msc_window *msc_oldest_window(struct msc *msc)
393 struct msc_window *win;
395 if (list_empty(&msc->win_list))
398 win = msc_find_window(msc, msc_next_window(msc->cur_win)->sgt, true);
402 return list_first_entry(&msc->win_list, struct msc_window, entry);
406 * msc_win_oldest_sg() - locate the oldest block in a given window
407 * @win: window to look at
409 * Return: index of the block with the oldest data
411 static struct scatterlist *msc_win_oldest_sg(struct msc_window *win)
414 struct scatterlist *sg;
415 struct msc_block_desc *bdesc = msc_win_base(win);
417 /* without wrapping, first block is the oldest */
418 if (!msc_block_wrapped(bdesc))
419 return msc_win_base_sg(win);
422 * with wrapping, last written block contains both the newest and the
423 * oldest data for this window.
425 for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
426 struct msc_block_desc *bdesc = sg_virt(sg);
428 if (msc_block_last_written(bdesc))
432 return msc_win_base_sg(win);
435 static struct msc_block_desc *msc_iter_bdesc(struct msc_iter *iter)
437 return sg_virt(iter->block);
440 static struct msc_iter *msc_iter_install(struct msc *msc)
442 struct msc_iter *iter;
444 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
446 return ERR_PTR(-ENOMEM);
448 mutex_lock(&msc->buf_mutex);
451 * Reading and tracing are mutually exclusive; if msc is
452 * enabled, open() will fail; otherwise existing readers
453 * will prevent enabling the msc and the rest of fops don't
454 * need to worry about it.
458 iter = ERR_PTR(-EBUSY);
464 list_add_tail(&iter->entry, &msc->iter_list);
466 mutex_unlock(&msc->buf_mutex);
471 static void msc_iter_remove(struct msc_iter *iter, struct msc *msc)
473 mutex_lock(&msc->buf_mutex);
474 list_del(&iter->entry);
475 mutex_unlock(&msc->buf_mutex);
480 static void msc_iter_block_start(struct msc_iter *iter)
482 if (iter->start_block)
485 iter->start_block = msc_win_oldest_sg(iter->win);
486 iter->block = iter->start_block;
487 iter->wrap_count = 0;
490 * start with the block with oldest data; if data has wrapped
491 * in this window, it should be in this block
493 if (msc_block_wrapped(msc_iter_bdesc(iter)))
494 iter->wrap_count = 2;
498 static int msc_iter_win_start(struct msc_iter *iter, struct msc *msc)
500 /* already started, nothing to do */
504 iter->start_win = msc_oldest_window(msc);
505 if (!iter->start_win)
508 iter->win = iter->start_win;
509 iter->start_block = NULL;
511 msc_iter_block_start(iter);
516 static int msc_iter_win_advance(struct msc_iter *iter)
518 iter->win = msc_next_window(iter->win);
519 iter->start_block = NULL;
521 if (iter->win == iter->start_win) {
526 msc_iter_block_start(iter);
531 static int msc_iter_block_advance(struct msc_iter *iter)
536 if (iter->wrap_count && iter->block == iter->start_block) {
538 if (!iter->wrap_count)
539 /* copied newest data from the wrapped block */
540 return msc_iter_win_advance(iter);
543 /* no wrapping, check for last written block */
544 if (!iter->wrap_count && msc_block_last_written(msc_iter_bdesc(iter)))
545 /* copied newest data for the window */
546 return msc_iter_win_advance(iter);
549 if (sg_is_last(iter->block))
550 iter->block = msc_win_base_sg(iter->win);
552 iter->block = sg_next(iter->block);
554 /* no wrapping, sanity check in case there is no last written block */
555 if (!iter->wrap_count && iter->block == iter->start_block)
556 return msc_iter_win_advance(iter);
562 * msc_buffer_iterate() - go through multiblock buffer's data
563 * @iter: iterator structure
564 * @size: amount of data to scan
565 * @data: callback's private data
566 * @fn: iterator callback
568 * This will start at the window which will be written to next (containing
569 * the oldest data) and work its way to the current window, calling @fn
570 * for each chunk of data as it goes.
572 * Caller should have msc::user_count reference to make sure the buffer
573 * doesn't disappear from under us.
575 * Return: amount of data actually scanned.
578 msc_buffer_iterate(struct msc_iter *iter, size_t size, void *data,
579 unsigned long (*fn)(void *, void *, size_t))
581 struct msc *msc = iter->msc;
583 unsigned int advance;
588 /* start with the oldest window */
589 if (msc_iter_win_start(iter, msc))
593 unsigned long data_bytes = msc_data_sz(msc_iter_bdesc(iter));
594 void *src = (void *)msc_iter_bdesc(iter) + MSC_BDESC;
595 size_t tocopy = data_bytes, copied = 0;
596 size_t remaining = 0;
601 * If block wrapping happened, we need to visit the last block
602 * twice, because it contains both the oldest and the newest
603 * data in this window.
605 * First time (wrap_count==2), in the very beginning, to collect
606 * the oldest data, which is in the range
607 * (data_bytes..DATA_IN_PAGE).
609 * Second time (wrap_count==1), it's just like any other block,
610 * containing data in the range of [MSC_BDESC..data_bytes].
612 if (iter->block == iter->start_block && iter->wrap_count == 2) {
613 tocopy = DATA_IN_PAGE - data_bytes;
620 tocopy -= iter->block_off;
621 src += iter->block_off;
628 remaining = fn(data, src, tocopy);
633 copied = tocopy - remaining;
635 iter->block_off += copied;
636 iter->offset += copied;
642 if (msc_iter_block_advance(iter))
651 * msc_buffer_clear_hw_header() - clear hw header for multiblock
654 static void msc_buffer_clear_hw_header(struct msc *msc)
656 struct msc_window *win;
657 struct scatterlist *sg;
659 list_for_each_entry(win, &msc->win_list, entry) {
662 for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
663 struct msc_block_desc *bdesc = sg_virt(sg);
665 memset_startat(bdesc, 0, hw_tag);
670 static int intel_th_msu_init(struct msc *msc)
680 mintctl = ioread32(msc->msu_base + REG_MSU_MINTCTL);
681 mintctl |= msc->index ? M1BLIE : M0BLIE;
682 iowrite32(mintctl, msc->msu_base + REG_MSU_MINTCTL);
683 if (mintctl != ioread32(msc->msu_base + REG_MSU_MINTCTL)) {
684 dev_info(msc_dev(msc), "MINTCTL ignores writes: no usable interrupts\n");
689 msusts = ioread32(msc->msu_base + REG_MSU_MSUSTS);
690 iowrite32(msusts, msc->msu_base + REG_MSU_MSUSTS);
695 static void intel_th_msu_deinit(struct msc *msc)
702 mintctl = ioread32(msc->msu_base + REG_MSU_MINTCTL);
703 mintctl &= msc->index ? ~M1BLIE : ~M0BLIE;
704 iowrite32(mintctl, msc->msu_base + REG_MSU_MINTCTL);
707 static int msc_win_set_lockout(struct msc_window *win,
708 enum lockout_state expect,
709 enum lockout_state new)
711 enum lockout_state old;
718 spin_lock_irqsave(&win->lo_lock, flags);
728 if (old == expect && new == WIN_LOCKED)
729 atomic_inc(&win->msc->user_count);
730 else if (old == expect && old == WIN_LOCKED)
731 atomic_dec(&win->msc->user_count);
734 spin_unlock_irqrestore(&win->lo_lock, flags);
737 if (expect == WIN_READY && old == WIN_LOCKED)
740 /* from intel_th_msc_window_unlock(), don't warn if not locked */
741 if (expect == WIN_LOCKED && old == new)
744 dev_warn_ratelimited(msc_dev(win->msc),
745 "expected lockout state %d, got %d\n",
752 * msc_configure() - set up MSC hardware
753 * @msc: the MSC device to configure
755 * Program storage mode, wrapping, burst length and trace buffer address
756 * into a given MSC. Then, enable tracing and set msc::enabled.
757 * The latter is serialized on msc::buf_mutex, so make sure to hold it.
759 static int msc_configure(struct msc *msc)
763 lockdep_assert_held(&msc->buf_mutex);
765 if (msc->mode > MSC_MODE_MULTI)
768 if (msc->mode == MSC_MODE_MULTI) {
769 if (msc_win_set_lockout(msc->cur_win, WIN_READY, WIN_INUSE))
772 msc_buffer_clear_hw_header(msc);
775 msc->orig_addr = ioread32(msc->reg_base + REG_MSU_MSC0BAR);
776 msc->orig_sz = ioread32(msc->reg_base + REG_MSU_MSC0SIZE);
778 reg = msc->base_addr >> PAGE_SHIFT;
779 iowrite32(reg, msc->reg_base + REG_MSU_MSC0BAR);
781 if (msc->mode == MSC_MODE_SINGLE) {
783 iowrite32(reg, msc->reg_base + REG_MSU_MSC0SIZE);
786 reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
787 reg &= ~(MSC_MODE | MSC_WRAPEN | MSC_EN | MSC_RD_HDR_OVRD);
790 reg |= msc->mode << __ffs(MSC_MODE);
791 reg |= msc->burst_len << __ffs(MSC_LEN);
796 iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
798 intel_th_msu_init(msc);
800 msc->thdev->output.multiblock = msc->mode == MSC_MODE_MULTI;
801 intel_th_trace_enable(msc->thdev);
804 if (msc->mbuf && msc->mbuf->activate)
805 msc->mbuf->activate(msc->mbuf_priv);
811 * msc_disable() - disable MSC hardware
812 * @msc: MSC device to disable
814 * If @msc is enabled, disable tracing on the switch and then disable MSC
815 * storage. Caller must hold msc::buf_mutex.
817 static void msc_disable(struct msc *msc)
819 struct msc_window *win = msc->cur_win;
822 lockdep_assert_held(&msc->buf_mutex);
824 if (msc->mode == MSC_MODE_MULTI)
825 msc_win_set_lockout(win, WIN_INUSE, WIN_LOCKED);
827 if (msc->mbuf && msc->mbuf->deactivate)
828 msc->mbuf->deactivate(msc->mbuf_priv);
829 intel_th_msu_deinit(msc);
830 intel_th_trace_disable(msc->thdev);
832 if (msc->mode == MSC_MODE_SINGLE) {
833 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
834 msc->single_wrap = !!(reg & MSCSTS_WRAPSTAT);
836 reg = ioread32(msc->reg_base + REG_MSU_MSC0MWP);
837 msc->single_sz = reg & ((msc->nr_pages << PAGE_SHIFT) - 1);
838 dev_dbg(msc_dev(msc), "MSCnMWP: %08x/%08lx, wrap: %d\n",
839 reg, msc->single_sz, msc->single_wrap);
842 reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
844 iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
846 if (msc->mbuf && msc->mbuf->ready)
847 msc->mbuf->ready(msc->mbuf_priv, win->sgt,
848 msc_win_total_sz(win));
852 iowrite32(msc->orig_addr, msc->reg_base + REG_MSU_MSC0BAR);
853 iowrite32(msc->orig_sz, msc->reg_base + REG_MSU_MSC0SIZE);
855 dev_dbg(msc_dev(msc), "MSCnNWSA: %08x\n",
856 ioread32(msc->reg_base + REG_MSU_MSC0NWSA));
858 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
859 dev_dbg(msc_dev(msc), "MSCnSTS: %08x\n", reg);
861 reg = ioread32(msc->reg_base + REG_MSU_MSUSTS);
862 reg &= msc->index ? MSUSTS_MSC1BLAST : MSUSTS_MSC0BLAST;
863 iowrite32(reg, msc->reg_base + REG_MSU_MSUSTS);
866 static int intel_th_msc_activate(struct intel_th_device *thdev)
868 struct msc *msc = dev_get_drvdata(&thdev->dev);
871 if (!atomic_inc_unless_negative(&msc->user_count))
874 mutex_lock(&msc->buf_mutex);
876 /* if there are readers, refuse */
877 if (list_empty(&msc->iter_list))
878 ret = msc_configure(msc);
880 mutex_unlock(&msc->buf_mutex);
883 atomic_dec(&msc->user_count);
888 static void intel_th_msc_deactivate(struct intel_th_device *thdev)
890 struct msc *msc = dev_get_drvdata(&thdev->dev);
892 mutex_lock(&msc->buf_mutex);
895 atomic_dec(&msc->user_count);
897 mutex_unlock(&msc->buf_mutex);
901 * msc_buffer_contig_alloc() - allocate a contiguous buffer for SINGLE mode
903 * @size: allocation size in bytes
905 * This modifies msc::base, which requires msc::buf_mutex to serialize, so the
906 * caller is expected to hold it.
908 * Return: 0 on success, -errno otherwise.
910 static int msc_buffer_contig_alloc(struct msc *msc, unsigned long size)
912 unsigned long nr_pages = size >> PAGE_SHIFT;
913 unsigned int order = get_order(size);
920 ret = sg_alloc_table(&msc->single_sgt, 1, GFP_KERNEL);
925 page = alloc_pages(GFP_KERNEL | __GFP_ZERO | GFP_DMA32, order);
929 split_page(page, order);
930 sg_set_buf(msc->single_sgt.sgl, page_address(page), size);
932 ret = dma_map_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl, 1,
937 msc->nr_pages = nr_pages;
938 msc->base = page_address(page);
939 msc->base_addr = sg_dma_address(msc->single_sgt.sgl);
944 __free_pages(page, order);
947 sg_free_table(&msc->single_sgt);
954 * msc_buffer_contig_free() - free a contiguous buffer
955 * @msc: MSC configured in SINGLE mode
957 static void msc_buffer_contig_free(struct msc *msc)
961 dma_unmap_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl,
963 sg_free_table(&msc->single_sgt);
965 for (off = 0; off < msc->nr_pages << PAGE_SHIFT; off += PAGE_SIZE) {
966 struct page *page = virt_to_page(msc->base + off);
968 page->mapping = NULL;
976 * msc_buffer_contig_get_page() - find a page at a given offset
977 * @msc: MSC configured in SINGLE mode
978 * @pgoff: page offset
980 * Return: page, if @pgoff is within the range, NULL otherwise.
982 static struct page *msc_buffer_contig_get_page(struct msc *msc,
985 if (pgoff >= msc->nr_pages)
988 return virt_to_page(msc->base + (pgoff << PAGE_SHIFT));
991 static int __msc_buffer_win_alloc(struct msc_window *win,
992 unsigned int nr_segs)
994 struct scatterlist *sg_ptr;
998 ret = sg_alloc_table(win->sgt, nr_segs, GFP_KERNEL);
1002 for_each_sg(win->sgt->sgl, sg_ptr, nr_segs, i) {
1003 block = dma_alloc_coherent(msc_dev(win->msc)->parent->parent,
1004 PAGE_SIZE, &sg_dma_address(sg_ptr),
1009 sg_set_buf(sg_ptr, block, PAGE_SIZE);
1015 for_each_sg(win->sgt->sgl, sg_ptr, i, ret)
1016 dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
1017 sg_virt(sg_ptr), sg_dma_address(sg_ptr));
1019 sg_free_table(win->sgt);
1025 static void msc_buffer_set_uc(struct msc *msc)
1027 struct scatterlist *sg_ptr;
1028 struct msc_window *win;
1031 if (msc->mode == MSC_MODE_SINGLE) {
1032 set_memory_uc((unsigned long)msc->base, msc->nr_pages);
1036 list_for_each_entry(win, &msc->win_list, entry) {
1037 for_each_sg(win->sgt->sgl, sg_ptr, win->nr_segs, i) {
1038 /* Set the page as uncached */
1039 set_memory_uc((unsigned long)sg_virt(sg_ptr),
1040 PFN_DOWN(sg_ptr->length));
1045 static void msc_buffer_set_wb(struct msc *msc)
1047 struct scatterlist *sg_ptr;
1048 struct msc_window *win;
1051 if (msc->mode == MSC_MODE_SINGLE) {
1052 set_memory_wb((unsigned long)msc->base, msc->nr_pages);
1056 list_for_each_entry(win, &msc->win_list, entry) {
1057 for_each_sg(win->sgt->sgl, sg_ptr, win->nr_segs, i) {
1058 /* Reset the page to write-back */
1059 set_memory_wb((unsigned long)sg_virt(sg_ptr),
1060 PFN_DOWN(sg_ptr->length));
1066 msc_buffer_set_uc(struct msc *msc) {}
1067 static inline void msc_buffer_set_wb(struct msc *msc) {}
1068 #endif /* CONFIG_X86 */
1071 * msc_buffer_win_alloc() - alloc a window for a multiblock mode
1073 * @nr_blocks: number of pages in this window
1075 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
1076 * to serialize, so the caller is expected to hold it.
1078 * Return: 0 on success, -errno otherwise.
1080 static int msc_buffer_win_alloc(struct msc *msc, unsigned int nr_blocks)
1082 struct msc_window *win;
1088 win = kzalloc(sizeof(*win), GFP_KERNEL);
1093 win->sgt = &win->_sgt;
1094 win->lockout = WIN_READY;
1095 spin_lock_init(&win->lo_lock);
1097 if (!list_empty(&msc->win_list)) {
1098 struct msc_window *prev = list_last_entry(&msc->win_list,
1102 win->pgoff = prev->pgoff + prev->nr_blocks;
1105 if (msc->mbuf && msc->mbuf->alloc_window)
1106 ret = msc->mbuf->alloc_window(msc->mbuf_priv, &win->sgt,
1107 nr_blocks << PAGE_SHIFT);
1109 ret = __msc_buffer_win_alloc(win, nr_blocks);
1115 win->nr_blocks = nr_blocks;
1117 if (list_empty(&msc->win_list)) {
1118 msc->base = msc_win_base(win);
1119 msc->base_addr = msc_win_base_dma(win);
1123 list_add_tail(&win->entry, &msc->win_list);
1124 msc->nr_pages += nr_blocks;
1134 static void __msc_buffer_win_free(struct msc *msc, struct msc_window *win)
1136 struct scatterlist *sg;
1139 for_each_sg(win->sgt->sgl, sg, win->nr_segs, i) {
1140 struct page *page = sg_page(sg);
1142 page->mapping = NULL;
1143 dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
1144 sg_virt(sg), sg_dma_address(sg));
1146 sg_free_table(win->sgt);
1150 * msc_buffer_win_free() - free a window from MSC's window list
1152 * @win: window to free
1154 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
1155 * to serialize, so the caller is expected to hold it.
1157 static void msc_buffer_win_free(struct msc *msc, struct msc_window *win)
1159 msc->nr_pages -= win->nr_blocks;
1161 list_del(&win->entry);
1162 if (list_empty(&msc->win_list)) {
1167 if (msc->mbuf && msc->mbuf->free_window)
1168 msc->mbuf->free_window(msc->mbuf_priv, win->sgt);
1170 __msc_buffer_win_free(msc, win);
1176 * msc_buffer_relink() - set up block descriptors for multiblock mode
1179 * This traverses msc::win_list, which requires msc::buf_mutex to serialize,
1180 * so the caller is expected to hold it.
1182 static void msc_buffer_relink(struct msc *msc)
1184 struct msc_window *win, *next_win;
1186 /* call with msc::mutex locked */
1187 list_for_each_entry(win, &msc->win_list, entry) {
1188 struct scatterlist *sg;
1193 * Last window's next_win should point to the first window
1194 * and MSC_SW_TAG_LASTWIN should be set.
1196 if (msc_is_last_win(win)) {
1197 sw_tag |= MSC_SW_TAG_LASTWIN;
1198 next_win = list_first_entry(&msc->win_list,
1199 struct msc_window, entry);
1201 next_win = list_next_entry(win, entry);
1204 for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
1205 struct msc_block_desc *bdesc = sg_virt(sg);
1207 memset(bdesc, 0, sizeof(*bdesc));
1209 bdesc->next_win = msc_win_base_pfn(next_win);
1212 * Similarly to last window, last block should point
1215 if (blk == win->nr_segs - 1) {
1216 sw_tag |= MSC_SW_TAG_LASTBLK;
1217 bdesc->next_blk = msc_win_base_pfn(win);
1219 dma_addr_t addr = sg_dma_address(sg_next(sg));
1221 bdesc->next_blk = PFN_DOWN(addr);
1224 bdesc->sw_tag = sw_tag;
1225 bdesc->block_sz = sg->length / 64;
1230 * Make the above writes globally visible before tracing is
1231 * enabled to make sure hardware sees them coherently.
1236 static void msc_buffer_multi_free(struct msc *msc)
1238 struct msc_window *win, *iter;
1240 list_for_each_entry_safe(win, iter, &msc->win_list, entry)
1241 msc_buffer_win_free(msc, win);
1244 static int msc_buffer_multi_alloc(struct msc *msc, unsigned long *nr_pages,
1245 unsigned int nr_wins)
1249 for (i = 0; i < nr_wins; i++) {
1250 ret = msc_buffer_win_alloc(msc, nr_pages[i]);
1252 msc_buffer_multi_free(msc);
1257 msc_buffer_relink(msc);
1263 * msc_buffer_free() - free buffers for MSC
1266 * Free MSC's storage buffers.
1268 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex to
1269 * serialize, so the caller is expected to hold it.
1271 static void msc_buffer_free(struct msc *msc)
1273 msc_buffer_set_wb(msc);
1275 if (msc->mode == MSC_MODE_SINGLE)
1276 msc_buffer_contig_free(msc);
1277 else if (msc->mode == MSC_MODE_MULTI)
1278 msc_buffer_multi_free(msc);
1282 * msc_buffer_alloc() - allocate a buffer for MSC
1284 * @size: allocation size in bytes
1286 * Allocate a storage buffer for MSC, depending on the msc::mode, it will be
1287 * either done via msc_buffer_contig_alloc() for SINGLE operation mode or
1288 * msc_buffer_win_alloc() for multiblock operation. The latter allocates one
1289 * window per invocation, so in multiblock mode this can be called multiple
1290 * times for the same MSC to allocate multiple windows.
1292 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
1293 * to serialize, so the caller is expected to hold it.
1295 * Return: 0 on success, -errno otherwise.
1297 static int msc_buffer_alloc(struct msc *msc, unsigned long *nr_pages,
1298 unsigned int nr_wins)
1302 /* -1: buffer not allocated */
1303 if (atomic_read(&msc->user_count) != -1)
1306 if (msc->mode == MSC_MODE_SINGLE) {
1310 ret = msc_buffer_contig_alloc(msc, nr_pages[0] << PAGE_SHIFT);
1311 } else if (msc->mode == MSC_MODE_MULTI) {
1312 ret = msc_buffer_multi_alloc(msc, nr_pages, nr_wins);
1318 msc_buffer_set_uc(msc);
1320 /* allocation should be visible before the counter goes to 0 */
1321 smp_mb__before_atomic();
1323 if (WARN_ON_ONCE(atomic_cmpxchg(&msc->user_count, -1, 0) != -1))
1331 * msc_buffer_unlocked_free_unless_used() - free a buffer unless it's in use
1334 * This will free MSC buffer unless it is in use or there is no allocated
1336 * Caller needs to hold msc::buf_mutex.
1338 * Return: 0 on successful deallocation or if there was no buffer to
1339 * deallocate, -EBUSY if there are active users.
1341 static int msc_buffer_unlocked_free_unless_used(struct msc *msc)
1345 count = atomic_cmpxchg(&msc->user_count, 0, -1);
1347 /* > 0: buffer is allocated and has users */
1350 /* 0: buffer is allocated, no users */
1352 msc_buffer_free(msc);
1353 /* < 0: no buffer, nothing to do */
1359 * msc_buffer_free_unless_used() - free a buffer unless it's in use
1362 * This is a locked version of msc_buffer_unlocked_free_unless_used().
1364 static int msc_buffer_free_unless_used(struct msc *msc)
1368 mutex_lock(&msc->buf_mutex);
1369 ret = msc_buffer_unlocked_free_unless_used(msc);
1370 mutex_unlock(&msc->buf_mutex);
1376 * msc_buffer_get_page() - get MSC buffer page at a given offset
1378 * @pgoff: page offset into the storage buffer
1380 * This traverses msc::win_list, so holding msc::buf_mutex is expected from
1383 * Return: page if @pgoff corresponds to a valid buffer page or NULL.
1385 static struct page *msc_buffer_get_page(struct msc *msc, unsigned long pgoff)
1387 struct msc_window *win;
1388 struct scatterlist *sg;
1391 if (msc->mode == MSC_MODE_SINGLE)
1392 return msc_buffer_contig_get_page(msc, pgoff);
1394 list_for_each_entry(win, &msc->win_list, entry)
1395 if (pgoff >= win->pgoff && pgoff < win->pgoff + win->nr_blocks)
1401 pgoff -= win->pgoff;
1403 for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
1404 struct page *page = sg_page(sg);
1405 size_t pgsz = PFN_DOWN(sg->length);
1408 return page + pgoff;
1417 * struct msc_win_to_user_struct - data for copy_to_user() callback
1418 * @buf: userspace buffer to copy data to
1419 * @offset: running offset
1421 struct msc_win_to_user_struct {
1423 unsigned long offset;
1427 * msc_win_to_user() - iterator for msc_buffer_iterate() to copy data to user
1428 * @data: callback's private data
1429 * @src: source buffer
1430 * @len: amount of data to copy from the source buffer
1432 static unsigned long msc_win_to_user(void *data, void *src, size_t len)
1434 struct msc_win_to_user_struct *u = data;
1437 ret = copy_to_user(u->buf + u->offset, src, len);
1438 u->offset += len - ret;
1445 * file operations' callbacks
1448 static int intel_th_msc_open(struct inode *inode, struct file *file)
1450 struct intel_th_device *thdev = file->private_data;
1451 struct msc *msc = dev_get_drvdata(&thdev->dev);
1452 struct msc_iter *iter;
1454 if (!capable(CAP_SYS_RAWIO))
1457 iter = msc_iter_install(msc);
1459 return PTR_ERR(iter);
1461 file->private_data = iter;
1463 return nonseekable_open(inode, file);
1466 static int intel_th_msc_release(struct inode *inode, struct file *file)
1468 struct msc_iter *iter = file->private_data;
1469 struct msc *msc = iter->msc;
1471 msc_iter_remove(iter, msc);
1477 msc_single_to_user(struct msc *msc, char __user *buf, loff_t off, size_t len)
1479 unsigned long size = msc->nr_pages << PAGE_SHIFT, rem = len;
1480 unsigned long start = off, tocopy = 0;
1482 if (msc->single_wrap) {
1483 start += msc->single_sz;
1485 tocopy = min(rem, size - start);
1486 if (copy_to_user(buf, msc->base + start, tocopy))
1496 tocopy = min(rem, msc->single_sz - start);
1497 if (copy_to_user(buf, msc->base + start, tocopy))
1506 if (copy_to_user(buf, msc->base + start, rem))
1512 static ssize_t intel_th_msc_read(struct file *file, char __user *buf,
1513 size_t len, loff_t *ppos)
1515 struct msc_iter *iter = file->private_data;
1516 struct msc *msc = iter->msc;
1521 if (!atomic_inc_unless_negative(&msc->user_count))
1524 if (msc->mode == MSC_MODE_SINGLE && !msc->single_wrap)
1525 size = msc->single_sz;
1527 size = msc->nr_pages << PAGE_SHIFT;
1535 if (off + len >= size)
1538 if (msc->mode == MSC_MODE_SINGLE) {
1539 ret = msc_single_to_user(msc, buf, off, len);
1542 } else if (msc->mode == MSC_MODE_MULTI) {
1543 struct msc_win_to_user_struct u = {
1548 ret = msc_buffer_iterate(iter, len, &u, msc_win_to_user);
1550 *ppos = iter->offset;
1556 atomic_dec(&msc->user_count);
1562 * vm operations callbacks (vm_ops)
1565 static void msc_mmap_open(struct vm_area_struct *vma)
1567 struct msc_iter *iter = vma->vm_file->private_data;
1568 struct msc *msc = iter->msc;
1570 atomic_inc(&msc->mmap_count);
1573 static void msc_mmap_close(struct vm_area_struct *vma)
1575 struct msc_iter *iter = vma->vm_file->private_data;
1576 struct msc *msc = iter->msc;
1579 if (!atomic_dec_and_mutex_lock(&msc->mmap_count, &msc->buf_mutex))
1582 /* drop page _refcounts */
1583 for (pg = 0; pg < msc->nr_pages; pg++) {
1584 struct page *page = msc_buffer_get_page(msc, pg);
1586 if (WARN_ON_ONCE(!page))
1590 page->mapping = NULL;
1593 /* last mapping -- drop user_count */
1594 atomic_dec(&msc->user_count);
1595 mutex_unlock(&msc->buf_mutex);
1598 static vm_fault_t msc_mmap_fault(struct vm_fault *vmf)
1600 struct msc_iter *iter = vmf->vma->vm_file->private_data;
1601 struct msc *msc = iter->msc;
1603 vmf->page = msc_buffer_get_page(msc, vmf->pgoff);
1605 return VM_FAULT_SIGBUS;
1607 get_page(vmf->page);
1608 vmf->page->mapping = vmf->vma->vm_file->f_mapping;
1609 vmf->page->index = vmf->pgoff;
1614 static const struct vm_operations_struct msc_mmap_ops = {
1615 .open = msc_mmap_open,
1616 .close = msc_mmap_close,
1617 .fault = msc_mmap_fault,
1620 static int intel_th_msc_mmap(struct file *file, struct vm_area_struct *vma)
1622 unsigned long size = vma->vm_end - vma->vm_start;
1623 struct msc_iter *iter = vma->vm_file->private_data;
1624 struct msc *msc = iter->msc;
1627 if (!size || offset_in_page(size))
1633 /* grab user_count once per mmap; drop in msc_mmap_close() */
1634 if (!atomic_inc_unless_negative(&msc->user_count))
1637 if (msc->mode != MSC_MODE_SINGLE &&
1638 msc->mode != MSC_MODE_MULTI)
1641 if (size >> PAGE_SHIFT != msc->nr_pages)
1644 atomic_set(&msc->mmap_count, 1);
1649 atomic_dec(&msc->user_count);
1651 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1652 vma->vm_flags |= VM_DONTEXPAND | VM_DONTCOPY;
1653 vma->vm_ops = &msc_mmap_ops;
1657 static const struct file_operations intel_th_msc_fops = {
1658 .open = intel_th_msc_open,
1659 .release = intel_th_msc_release,
1660 .read = intel_th_msc_read,
1661 .mmap = intel_th_msc_mmap,
1662 .llseek = no_llseek,
1663 .owner = THIS_MODULE,
1666 static void intel_th_msc_wait_empty(struct intel_th_device *thdev)
1668 struct msc *msc = dev_get_drvdata(&thdev->dev);
1669 unsigned long count;
1672 for (reg = 0, count = MSC_PLE_WAITLOOP_DEPTH;
1673 count && !(reg & MSCSTS_PLE); count--) {
1674 reg = __raw_readl(msc->reg_base + REG_MSU_MSC0STS);
1679 dev_dbg(msc_dev(msc), "timeout waiting for MSC0 PLE\n");
1682 static int intel_th_msc_init(struct msc *msc)
1684 atomic_set(&msc->user_count, -1);
1686 msc->mode = msc->multi_is_broken ? MSC_MODE_SINGLE : MSC_MODE_MULTI;
1687 mutex_init(&msc->buf_mutex);
1688 INIT_LIST_HEAD(&msc->win_list);
1689 INIT_LIST_HEAD(&msc->iter_list);
1692 (ioread32(msc->reg_base + REG_MSU_MSC0CTL) & MSC_LEN) >>
1698 static int msc_win_switch(struct msc *msc)
1700 struct msc_window *first;
1702 if (list_empty(&msc->win_list))
1705 first = list_first_entry(&msc->win_list, struct msc_window, entry);
1707 if (msc_is_last_win(msc->cur_win))
1708 msc->cur_win = first;
1710 msc->cur_win = list_next_entry(msc->cur_win, entry);
1712 msc->base = msc_win_base(msc->cur_win);
1713 msc->base_addr = msc_win_base_dma(msc->cur_win);
1715 intel_th_trace_switch(msc->thdev);
1721 * intel_th_msc_window_unlock - put the window back in rotation
1722 * @dev: MSC device to which this relates
1723 * @sgt: buffer's sg_table for the window, does nothing if NULL
1725 void intel_th_msc_window_unlock(struct device *dev, struct sg_table *sgt)
1727 struct msc *msc = dev_get_drvdata(dev);
1728 struct msc_window *win;
1733 win = msc_find_window(msc, sgt, false);
1737 msc_win_set_lockout(win, WIN_LOCKED, WIN_READY);
1738 if (msc->switch_on_unlock == win) {
1739 msc->switch_on_unlock = NULL;
1740 msc_win_switch(msc);
1743 EXPORT_SYMBOL_GPL(intel_th_msc_window_unlock);
1745 static void msc_work(struct work_struct *work)
1747 struct msc *msc = container_of(work, struct msc, work);
1749 intel_th_msc_deactivate(msc->thdev);
1752 static irqreturn_t intel_th_msc_interrupt(struct intel_th_device *thdev)
1754 struct msc *msc = dev_get_drvdata(&thdev->dev);
1755 u32 msusts = ioread32(msc->msu_base + REG_MSU_MSUSTS);
1756 u32 mask = msc->index ? MSUSTS_MSC1BLAST : MSUSTS_MSC0BLAST;
1757 struct msc_window *win, *next_win;
1759 if (!msc->do_irq || !msc->mbuf)
1765 return msc->enabled ? IRQ_HANDLED : IRQ_NONE;
1767 iowrite32(msusts, msc->msu_base + REG_MSU_MSUSTS);
1772 /* grab the window before we do the switch */
1776 next_win = msc_next_window(win);
1780 /* next window: if READY, proceed, if LOCKED, stop the trace */
1781 if (msc_win_set_lockout(next_win, WIN_READY, WIN_INUSE)) {
1782 if (msc->stop_on_full)
1783 schedule_work(&msc->work);
1785 msc->switch_on_unlock = next_win;
1790 /* current window: INUSE -> LOCKED */
1791 msc_win_set_lockout(win, WIN_INUSE, WIN_LOCKED);
1793 msc_win_switch(msc);
1795 if (msc->mbuf && msc->mbuf->ready)
1796 msc->mbuf->ready(msc->mbuf_priv, win->sgt,
1797 msc_win_total_sz(win));
1802 static const char * const msc_mode[] = {
1803 [MSC_MODE_SINGLE] = "single",
1804 [MSC_MODE_MULTI] = "multi",
1805 [MSC_MODE_EXI] = "ExI",
1806 [MSC_MODE_DEBUG] = "debug",
1810 wrap_show(struct device *dev, struct device_attribute *attr, char *buf)
1812 struct msc *msc = dev_get_drvdata(dev);
1814 return scnprintf(buf, PAGE_SIZE, "%d\n", msc->wrap);
1818 wrap_store(struct device *dev, struct device_attribute *attr, const char *buf,
1821 struct msc *msc = dev_get_drvdata(dev);
1825 ret = kstrtoul(buf, 10, &val);
1834 static DEVICE_ATTR_RW(wrap);
1836 static void msc_buffer_unassign(struct msc *msc)
1838 lockdep_assert_held(&msc->buf_mutex);
1843 msc->mbuf->unassign(msc->mbuf_priv);
1844 msu_buffer_put(msc->mbuf);
1845 msc->mbuf_priv = NULL;
1850 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1852 struct msc *msc = dev_get_drvdata(dev);
1853 const char *mode = msc_mode[msc->mode];
1856 mutex_lock(&msc->buf_mutex);
1858 mode = msc->mbuf->name;
1859 ret = scnprintf(buf, PAGE_SIZE, "%s\n", mode);
1860 mutex_unlock(&msc->buf_mutex);
1866 mode_store(struct device *dev, struct device_attribute *attr, const char *buf,
1869 const struct msu_buffer *mbuf = NULL;
1870 struct msc *msc = dev_get_drvdata(dev);
1875 if (!capable(CAP_SYS_RAWIO))
1878 cp = memchr(buf, '\n', len);
1882 mode = kstrndup(buf, len, GFP_KERNEL);
1886 i = match_string(msc_mode, ARRAY_SIZE(msc_mode), mode);
1892 /* Buffer sinks only work with a usable IRQ */
1898 mbuf = msu_buffer_get(mode);
1906 if (i == MSC_MODE_MULTI && msc->multi_is_broken)
1909 mutex_lock(&msc->buf_mutex);
1912 /* Same buffer: do nothing */
1913 if (mbuf && mbuf == msc->mbuf) {
1914 /* put the extra reference we just got */
1915 msu_buffer_put(mbuf);
1919 ret = msc_buffer_unlocked_free_unless_used(msc);
1924 void *mbuf_priv = mbuf->assign(dev, &i);
1931 msc_buffer_unassign(msc);
1932 msc->mbuf_priv = mbuf_priv;
1935 msc_buffer_unassign(msc);
1942 msu_buffer_put(mbuf);
1943 mutex_unlock(&msc->buf_mutex);
1945 return ret ? ret : size;
1948 static DEVICE_ATTR_RW(mode);
1951 nr_pages_show(struct device *dev, struct device_attribute *attr, char *buf)
1953 struct msc *msc = dev_get_drvdata(dev);
1954 struct msc_window *win;
1957 mutex_lock(&msc->buf_mutex);
1959 if (msc->mode == MSC_MODE_SINGLE)
1960 count = scnprintf(buf, PAGE_SIZE, "%ld\n", msc->nr_pages);
1961 else if (msc->mode == MSC_MODE_MULTI) {
1962 list_for_each_entry(win, &msc->win_list, entry) {
1963 count += scnprintf(buf + count, PAGE_SIZE - count,
1964 "%d%c", win->nr_blocks,
1965 msc_is_last_win(win) ? '\n' : ',');
1968 count = scnprintf(buf, PAGE_SIZE, "unsupported\n");
1971 mutex_unlock(&msc->buf_mutex);
1977 nr_pages_store(struct device *dev, struct device_attribute *attr,
1978 const char *buf, size_t size)
1980 struct msc *msc = dev_get_drvdata(dev);
1981 unsigned long val, *win = NULL, *rewin;
1983 const char *p = buf;
1985 int ret, nr_wins = 0;
1987 if (!capable(CAP_SYS_RAWIO))
1990 ret = msc_buffer_free_unless_used(msc);
1994 /* scan the comma-separated list of allocation sizes */
1995 end = memchr(buf, '\n', len);
2000 end = memchr(p, ',', len);
2001 s = kstrndup(p, end ? end - p : len, GFP_KERNEL);
2007 ret = kstrtoul(s, 10, &val);
2013 if (nr_wins && msc->mode == MSC_MODE_SINGLE) {
2019 rewin = krealloc_array(win, nr_wins, sizeof(*win), GFP_KERNEL);
2026 win[nr_wins - 1] = val;
2031 /* consume the number and the following comma, hence +1 */
2036 mutex_lock(&msc->buf_mutex);
2037 ret = msc_buffer_alloc(msc, win, nr_wins);
2038 mutex_unlock(&msc->buf_mutex);
2043 return ret ? ret : size;
2046 static DEVICE_ATTR_RW(nr_pages);
2049 win_switch_store(struct device *dev, struct device_attribute *attr,
2050 const char *buf, size_t size)
2052 struct msc *msc = dev_get_drvdata(dev);
2056 ret = kstrtoul(buf, 10, &val);
2064 mutex_lock(&msc->buf_mutex);
2066 * Window switch can only happen in the "multi" mode.
2067 * If a external buffer is engaged, they have the full
2068 * control over window switching.
2070 if (msc->mode == MSC_MODE_MULTI && !msc->mbuf)
2071 ret = msc_win_switch(msc);
2072 mutex_unlock(&msc->buf_mutex);
2074 return ret ? ret : size;
2077 static DEVICE_ATTR_WO(win_switch);
2079 static ssize_t stop_on_full_show(struct device *dev,
2080 struct device_attribute *attr, char *buf)
2082 struct msc *msc = dev_get_drvdata(dev);
2084 return sprintf(buf, "%d\n", msc->stop_on_full);
2087 static ssize_t stop_on_full_store(struct device *dev,
2088 struct device_attribute *attr,
2089 const char *buf, size_t size)
2091 struct msc *msc = dev_get_drvdata(dev);
2094 ret = kstrtobool(buf, &msc->stop_on_full);
2101 static DEVICE_ATTR_RW(stop_on_full);
2103 static struct attribute *msc_output_attrs[] = {
2104 &dev_attr_wrap.attr,
2105 &dev_attr_mode.attr,
2106 &dev_attr_nr_pages.attr,
2107 &dev_attr_win_switch.attr,
2108 &dev_attr_stop_on_full.attr,
2112 static const struct attribute_group msc_output_group = {
2113 .attrs = msc_output_attrs,
2116 static int intel_th_msc_probe(struct intel_th_device *thdev)
2118 struct device *dev = &thdev->dev;
2119 struct resource *res;
2124 res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
2128 base = devm_ioremap(dev, res->start, resource_size(res));
2132 msc = devm_kzalloc(dev, sizeof(*msc), GFP_KERNEL);
2136 res = intel_th_device_get_resource(thdev, IORESOURCE_IRQ, 1);
2140 if (INTEL_TH_CAP(to_intel_th(thdev), multi_is_broken))
2141 msc->multi_is_broken = 1;
2143 msc->index = thdev->id;
2146 msc->reg_base = base + msc->index * 0x100;
2147 msc->msu_base = base;
2149 INIT_WORK(&msc->work, msc_work);
2150 err = intel_th_msc_init(msc);
2154 dev_set_drvdata(dev, msc);
2159 static void intel_th_msc_remove(struct intel_th_device *thdev)
2161 struct msc *msc = dev_get_drvdata(&thdev->dev);
2164 intel_th_msc_deactivate(thdev);
2167 * Buffers should not be used at this point except if the
2168 * output character device is still open and the parent
2169 * device gets detached from its bus, which is a FIXME.
2171 ret = msc_buffer_free_unless_used(msc);
2175 static struct intel_th_driver intel_th_msc_driver = {
2176 .probe = intel_th_msc_probe,
2177 .remove = intel_th_msc_remove,
2178 .irq = intel_th_msc_interrupt,
2179 .wait_empty = intel_th_msc_wait_empty,
2180 .activate = intel_th_msc_activate,
2181 .deactivate = intel_th_msc_deactivate,
2182 .fops = &intel_th_msc_fops,
2183 .attr_group = &msc_output_group,
2186 .owner = THIS_MODULE,
2190 module_driver(intel_th_msc_driver,
2191 intel_th_driver_register,
2192 intel_th_driver_unregister);
2194 MODULE_LICENSE("GPL v2");
2195 MODULE_DESCRIPTION("Intel(R) Trace Hub Memory Storage Unit driver");
2196 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");