2 * Intel(R) Trace Hub Memory Storage Unit
4 * Copyright (C) 2014-2015 Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/types.h>
19 #include <linux/module.h>
20 #include <linux/device.h>
21 #include <linux/uaccess.h>
22 #include <linux/sizes.h>
23 #include <linux/printk.h>
24 #include <linux/slab.h>
28 #include <linux/dma-mapping.h>
30 #include <asm/cacheflush.h>
35 #define msc_dev(x) (&(x)->thdev->dev)
38 * struct msc_block - multiblock mode block descriptor
39 * @bdesc: pointer to hardware descriptor (beginning of the block)
40 * @addr: physical address of the block
43 struct msc_block_desc
*bdesc
;
48 * struct msc_window - multiblock mode window descriptor
49 * @entry: window list linkage (msc::win_list)
50 * @pgoff: page offset into the buffer that this window starts at
51 * @nr_blocks: number of blocks (pages) in this window
52 * @block: array of block descriptors
55 struct list_head entry
;
57 unsigned int nr_blocks
;
59 struct msc_block block
[0];
63 * struct msc_iter - iterator for msc buffer
64 * @entry: msc::iter_list linkage
65 * @msc: pointer to the MSC device
66 * @start_win: oldest window
67 * @win: current window
68 * @offset: current logical offset into the buffer
69 * @start_block: oldest block in the window
70 * @block: block number in the window
71 * @block_off: offset into current block
72 * @wrap_count: block wrapping handling
73 * @eof: end of buffer reached
76 struct list_head entry
;
78 struct msc_window
*start_win
;
79 struct msc_window
*win
;
83 unsigned int block_off
;
84 unsigned int wrap_count
;
89 * struct msc - MSC device representation
90 * @reg_base: register window base address
91 * @thdev: intel_th_device pointer
92 * @win_list: list of windows in multiblock mode
93 * @nr_pages: total number of pages allocated for this buffer
94 * @single_sz: amount of data in single mode
95 * @single_wrap: single mode wrap occurred
96 * @base: buffer's base pointer
97 * @base_addr: buffer's base address
98 * @user_count: number of users of the buffer
99 * @mmap_count: number of mappings
100 * @buf_mutex: mutex to serialize access to buffer-related bits
102 * @enabled: MSC is enabled
103 * @wrap: wrapping is enabled
104 * @mode: MSC operating mode
105 * @burst_len: write burst length
106 * @index: number of this MSC in the MSU
109 void __iomem
*reg_base
;
110 struct intel_th_device
*thdev
;
112 struct list_head win_list
;
113 unsigned long nr_pages
;
114 unsigned long single_sz
;
115 unsigned int single_wrap
: 1;
117 dma_addr_t base_addr
;
119 /* <0: no buffer, 0: no users, >0: active users */
123 struct mutex buf_mutex
;
125 struct mutex iter_mutex
;
126 struct list_head iter_list
;
129 unsigned int enabled
: 1,
132 unsigned int burst_len
;
136 static inline bool msc_block_is_empty(struct msc_block_desc
*bdesc
)
138 /* header hasn't been written */
139 if (!bdesc
->valid_dw
)
142 /* valid_dw includes the header */
143 if (!msc_data_sz(bdesc
))
150 * msc_oldest_window() - locate the window with oldest data
153 * This should only be used in multiblock mode. Caller should hold the
154 * msc::user_count reference.
156 * Return: the oldest window with valid data
158 static struct msc_window
*msc_oldest_window(struct msc
*msc
)
160 struct msc_window
*win
;
161 u32 reg
= ioread32(msc
->reg_base
+ REG_MSU_MSC0NWSA
);
162 unsigned long win_addr
= (unsigned long)reg
<< PAGE_SHIFT
;
163 unsigned int found
= 0;
165 if (list_empty(&msc
->win_list
))
169 * we might need a radix tree for this, depending on how
170 * many windows a typical user would allocate; ideally it's
171 * something like 2, in which case we're good
173 list_for_each_entry(win
, &msc
->win_list
, entry
) {
174 if (win
->block
[0].addr
== win_addr
)
177 /* skip the empty ones */
178 if (msc_block_is_empty(win
->block
[0].bdesc
))
185 return list_entry(msc
->win_list
.next
, struct msc_window
, entry
);
189 * msc_win_oldest_block() - locate the oldest block in a given window
190 * @win: window to look at
192 * Return: index of the block with the oldest data
194 static unsigned int msc_win_oldest_block(struct msc_window
*win
)
197 struct msc_block_desc
*bdesc
= win
->block
[0].bdesc
;
199 /* without wrapping, first block is the oldest */
200 if (!msc_block_wrapped(bdesc
))
204 * with wrapping, last written block contains both the newest and the
205 * oldest data for this window.
207 for (blk
= 0; blk
< win
->nr_blocks
; blk
++) {
208 bdesc
= win
->block
[blk
].bdesc
;
210 if (msc_block_last_written(bdesc
))
218 * msc_is_last_win() - check if a window is the last one for a given MSC
220 * Return: true if @win is the last window in MSC's multiblock buffer
222 static inline bool msc_is_last_win(struct msc_window
*win
)
224 return win
->entry
.next
== &win
->msc
->win_list
;
228 * msc_next_window() - return next window in the multiblock buffer
229 * @win: current window
231 * Return: window following the current one
233 static struct msc_window
*msc_next_window(struct msc_window
*win
)
235 if (msc_is_last_win(win
))
236 return list_entry(win
->msc
->win_list
.next
, struct msc_window
,
239 return list_entry(win
->entry
.next
, struct msc_window
, entry
);
242 static struct msc_block_desc
*msc_iter_bdesc(struct msc_iter
*iter
)
244 return iter
->win
->block
[iter
->block
].bdesc
;
247 static void msc_iter_init(struct msc_iter
*iter
)
249 memset(iter
, 0, sizeof(*iter
));
250 iter
->start_block
= -1;
254 static struct msc_iter
*msc_iter_install(struct msc
*msc
)
256 struct msc_iter
*iter
;
258 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
265 mutex_lock(&msc
->iter_mutex
);
266 list_add_tail(&iter
->entry
, &msc
->iter_list
);
267 mutex_unlock(&msc
->iter_mutex
);
272 static void msc_iter_remove(struct msc_iter
*iter
, struct msc
*msc
)
274 mutex_lock(&msc
->iter_mutex
);
275 list_del(&iter
->entry
);
276 mutex_unlock(&msc
->iter_mutex
);
281 static void msc_iter_block_start(struct msc_iter
*iter
)
283 if (iter
->start_block
!= -1)
286 iter
->start_block
= msc_win_oldest_block(iter
->win
);
287 iter
->block
= iter
->start_block
;
288 iter
->wrap_count
= 0;
291 * start with the block with oldest data; if data has wrapped
292 * in this window, it should be in this block
294 if (msc_block_wrapped(msc_iter_bdesc(iter
)))
295 iter
->wrap_count
= 2;
299 static int msc_iter_win_start(struct msc_iter
*iter
, struct msc
*msc
)
301 /* already started, nothing to do */
305 iter
->start_win
= msc_oldest_window(msc
);
306 if (!iter
->start_win
)
309 iter
->win
= iter
->start_win
;
310 iter
->start_block
= -1;
312 msc_iter_block_start(iter
);
317 static int msc_iter_win_advance(struct msc_iter
*iter
)
319 iter
->win
= msc_next_window(iter
->win
);
320 iter
->start_block
= -1;
322 if (iter
->win
== iter
->start_win
) {
327 msc_iter_block_start(iter
);
332 static int msc_iter_block_advance(struct msc_iter
*iter
)
337 if (iter
->wrap_count
&& iter
->block
== iter
->start_block
) {
339 if (!iter
->wrap_count
)
340 /* copied newest data from the wrapped block */
341 return msc_iter_win_advance(iter
);
344 /* no wrapping, check for last written block */
345 if (!iter
->wrap_count
&& msc_block_last_written(msc_iter_bdesc(iter
)))
346 /* copied newest data for the window */
347 return msc_iter_win_advance(iter
);
350 if (++iter
->block
== iter
->win
->nr_blocks
)
353 /* no wrapping, sanity check in case there is no last written block */
354 if (!iter
->wrap_count
&& iter
->block
== iter
->start_block
)
355 return msc_iter_win_advance(iter
);
361 * msc_buffer_iterate() - go through multiblock buffer's data
362 * @iter: iterator structure
363 * @size: amount of data to scan
364 * @data: callback's private data
365 * @fn: iterator callback
367 * This will start at the window which will be written to next (containing
368 * the oldest data) and work its way to the current window, calling @fn
369 * for each chunk of data as it goes.
371 * Caller should have msc::user_count reference to make sure the buffer
372 * doesn't disappear from under us.
374 * Return: amount of data actually scanned.
377 msc_buffer_iterate(struct msc_iter
*iter
, size_t size
, void *data
,
378 unsigned long (*fn
)(void *, void *, size_t))
380 struct msc
*msc
= iter
->msc
;
382 unsigned int advance
;
387 /* start with the oldest window */
388 if (msc_iter_win_start(iter
, msc
))
392 unsigned long data_bytes
= msc_data_sz(msc_iter_bdesc(iter
));
393 void *src
= (void *)msc_iter_bdesc(iter
) + MSC_BDESC
;
394 size_t tocopy
= data_bytes
, copied
= 0;
395 size_t remaining
= 0;
400 * If block wrapping happened, we need to visit the last block
401 * twice, because it contains both the oldest and the newest
402 * data in this window.
404 * First time (wrap_count==2), in the very beginning, to collect
405 * the oldest data, which is in the range
406 * (data_bytes..DATA_IN_PAGE).
408 * Second time (wrap_count==1), it's just like any other block,
409 * containing data in the range of [MSC_BDESC..data_bytes].
411 if (iter
->block
== iter
->start_block
&& iter
->wrap_count
) {
412 tocopy
= DATA_IN_PAGE
- data_bytes
;
419 tocopy
-= iter
->block_off
;
420 src
+= iter
->block_off
;
427 remaining
= fn(data
, src
, tocopy
);
432 copied
= tocopy
- remaining
;
434 iter
->block_off
+= copied
;
435 iter
->offset
+= copied
;
441 if (msc_iter_block_advance(iter
))
450 * msc_buffer_clear_hw_header() - clear hw header for multiblock
453 static void msc_buffer_clear_hw_header(struct msc
*msc
)
455 struct msc_window
*win
;
457 mutex_lock(&msc
->buf_mutex
);
458 list_for_each_entry(win
, &msc
->win_list
, entry
) {
460 size_t hw_sz
= sizeof(struct msc_block_desc
) -
461 offsetof(struct msc_block_desc
, hw_tag
);
463 for (blk
= 0; blk
< win
->nr_blocks
; blk
++) {
464 struct msc_block_desc
*bdesc
= win
->block
[blk
].bdesc
;
466 memset(&bdesc
->hw_tag
, 0, hw_sz
);
469 mutex_unlock(&msc
->buf_mutex
);
473 * msc_configure() - set up MSC hardware
474 * @msc: the MSC device to configure
476 * Program storage mode, wrapping, burst length and trace buffer address
477 * into a given MSC. If msc::enabled is set, enable the trace, too.
479 static int msc_configure(struct msc
*msc
)
483 if (msc
->mode
> MSC_MODE_MULTI
)
486 if (msc
->mode
== MSC_MODE_MULTI
)
487 msc_buffer_clear_hw_header(msc
);
489 reg
= msc
->base_addr
>> PAGE_SHIFT
;
490 iowrite32(reg
, msc
->reg_base
+ REG_MSU_MSC0BAR
);
492 if (msc
->mode
== MSC_MODE_SINGLE
) {
494 iowrite32(reg
, msc
->reg_base
+ REG_MSU_MSC0SIZE
);
497 reg
= ioread32(msc
->reg_base
+ REG_MSU_MSC0CTL
);
498 reg
&= ~(MSC_MODE
| MSC_WRAPEN
| MSC_EN
| MSC_RD_HDR_OVRD
);
500 reg
|= msc
->mode
<< __ffs(MSC_MODE
);
501 reg
|= msc
->burst_len
<< __ffs(MSC_LEN
);
502 /*if (msc->mode == MSC_MODE_MULTI)
503 reg |= MSC_RD_HDR_OVRD; */
509 iowrite32(reg
, msc
->reg_base
+ REG_MSU_MSC0CTL
);
512 msc
->thdev
->output
.multiblock
= msc
->mode
== MSC_MODE_MULTI
;
513 intel_th_trace_enable(msc
->thdev
);
520 * msc_disable() - disable MSC hardware
521 * @msc: MSC device to disable
523 * If @msc is enabled, disable tracing on the switch and then disable MSC
526 static void msc_disable(struct msc
*msc
)
534 intel_th_trace_disable(msc
->thdev
);
536 for (reg
= 0, count
= MSC_PLE_WAITLOOP_DEPTH
;
537 count
&& !(reg
& MSCSTS_PLE
); count
--) {
538 reg
= ioread32(msc
->reg_base
+ REG_MSU_MSC0STS
);
543 dev_dbg(msc_dev(msc
), "timeout waiting for MSC0 PLE\n");
545 if (msc
->mode
== MSC_MODE_SINGLE
) {
546 msc
->single_wrap
= !!(reg
& MSCSTS_WRAPSTAT
);
548 reg
= ioread32(msc
->reg_base
+ REG_MSU_MSC0MWP
);
549 msc
->single_sz
= reg
& ((msc
->nr_pages
<< PAGE_SHIFT
) - 1);
550 dev_dbg(msc_dev(msc
), "MSCnMWP: %08x/%08lx, wrap: %d\n",
551 reg
, msc
->single_sz
, msc
->single_wrap
);
554 reg
= ioread32(msc
->reg_base
+ REG_MSU_MSC0CTL
);
556 iowrite32(reg
, msc
->reg_base
+ REG_MSU_MSC0CTL
);
559 iowrite32(0, msc
->reg_base
+ REG_MSU_MSC0BAR
);
560 iowrite32(0, msc
->reg_base
+ REG_MSU_MSC0SIZE
);
562 dev_dbg(msc_dev(msc
), "MSCnNWSA: %08x\n",
563 ioread32(msc
->reg_base
+ REG_MSU_MSC0NWSA
));
565 reg
= ioread32(msc
->reg_base
+ REG_MSU_MSC0STS
);
566 dev_dbg(msc_dev(msc
), "MSCnSTS: %08x\n", reg
);
569 static int intel_th_msc_activate(struct intel_th_device
*thdev
)
571 struct msc
*msc
= dev_get_drvdata(&thdev
->dev
);
574 if (!atomic_inc_unless_negative(&msc
->user_count
))
577 mutex_lock(&msc
->iter_mutex
);
578 if (!list_empty(&msc
->iter_list
))
580 mutex_unlock(&msc
->iter_mutex
);
583 atomic_dec(&msc
->user_count
);
589 return msc_configure(msc
);
592 static void intel_th_msc_deactivate(struct intel_th_device
*thdev
)
594 struct msc
*msc
= dev_get_drvdata(&thdev
->dev
);
598 atomic_dec(&msc
->user_count
);
602 * msc_buffer_contig_alloc() - allocate a contiguous buffer for SINGLE mode
604 * @size: allocation size in bytes
606 * This modifies msc::base, which requires msc::buf_mutex to serialize, so the
607 * caller is expected to hold it.
609 * Return: 0 on success, -errno otherwise.
611 static int msc_buffer_contig_alloc(struct msc
*msc
, unsigned long size
)
613 unsigned int order
= get_order(size
);
619 page
= alloc_pages(GFP_KERNEL
| __GFP_ZERO
, order
);
623 split_page(page
, order
);
624 msc
->nr_pages
= size
>> PAGE_SHIFT
;
625 msc
->base
= page_address(page
);
626 msc
->base_addr
= page_to_phys(page
);
632 * msc_buffer_contig_free() - free a contiguous buffer
633 * @msc: MSC configured in SINGLE mode
635 static void msc_buffer_contig_free(struct msc
*msc
)
639 for (off
= 0; off
< msc
->nr_pages
<< PAGE_SHIFT
; off
+= PAGE_SIZE
) {
640 struct page
*page
= virt_to_page(msc
->base
+ off
);
642 page
->mapping
= NULL
;
650 * msc_buffer_contig_get_page() - find a page at a given offset
651 * @msc: MSC configured in SINGLE mode
652 * @pgoff: page offset
654 * Return: page, if @pgoff is within the range, NULL otherwise.
656 static struct page
*msc_buffer_contig_get_page(struct msc
*msc
,
659 if (pgoff
>= msc
->nr_pages
)
662 return virt_to_page(msc
->base
+ (pgoff
<< PAGE_SHIFT
));
666 * msc_buffer_win_alloc() - alloc a window for a multiblock mode
668 * @nr_blocks: number of pages in this window
670 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
671 * to serialize, so the caller is expected to hold it.
673 * Return: 0 on success, -errno otherwise.
675 static int msc_buffer_win_alloc(struct msc
*msc
, unsigned int nr_blocks
)
677 struct msc_window
*win
;
678 unsigned long size
= PAGE_SIZE
;
679 int i
, ret
= -ENOMEM
;
684 win
= kzalloc(offsetof(struct msc_window
, block
[nr_blocks
]),
689 if (!list_empty(&msc
->win_list
)) {
690 struct msc_window
*prev
= list_entry(msc
->win_list
.prev
,
691 struct msc_window
, entry
);
693 win
->pgoff
= prev
->pgoff
+ prev
->nr_blocks
;
696 for (i
= 0; i
< nr_blocks
; i
++) {
697 win
->block
[i
].bdesc
= dma_alloc_coherent(msc_dev(msc
), size
,
702 /* Set the page as uncached */
703 set_memory_uc((unsigned long)win
->block
[i
].bdesc
, 1);
706 if (!win
->block
[i
].bdesc
)
711 win
->nr_blocks
= nr_blocks
;
713 if (list_empty(&msc
->win_list
)) {
714 msc
->base
= win
->block
[0].bdesc
;
715 msc
->base_addr
= win
->block
[0].addr
;
718 list_add_tail(&win
->entry
, &msc
->win_list
);
719 msc
->nr_pages
+= nr_blocks
;
724 for (i
--; i
>= 0; i
--) {
726 /* Reset the page to write-back before releasing */
727 set_memory_wb((unsigned long)win
->block
[i
].bdesc
, 1);
729 dma_free_coherent(msc_dev(msc
), size
, win
->block
[i
].bdesc
,
738 * msc_buffer_win_free() - free a window from MSC's window list
740 * @win: window to free
742 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
743 * to serialize, so the caller is expected to hold it.
745 static void msc_buffer_win_free(struct msc
*msc
, struct msc_window
*win
)
749 msc
->nr_pages
-= win
->nr_blocks
;
751 list_del(&win
->entry
);
752 if (list_empty(&msc
->win_list
)) {
757 for (i
= 0; i
< win
->nr_blocks
; i
++) {
758 struct page
*page
= virt_to_page(win
->block
[i
].bdesc
);
760 page
->mapping
= NULL
;
762 /* Reset the page to write-back before releasing */
763 set_memory_wb((unsigned long)win
->block
[i
].bdesc
, 1);
765 dma_free_coherent(msc_dev(win
->msc
), PAGE_SIZE
,
766 win
->block
[i
].bdesc
, win
->block
[i
].addr
);
773 * msc_buffer_relink() - set up block descriptors for multiblock mode
776 * This traverses msc::win_list, which requires msc::buf_mutex to serialize,
777 * so the caller is expected to hold it.
779 static void msc_buffer_relink(struct msc
*msc
)
781 struct msc_window
*win
, *next_win
;
783 /* call with msc::mutex locked */
784 list_for_each_entry(win
, &msc
->win_list
, entry
) {
789 * Last window's next_win should point to the first window
790 * and MSC_SW_TAG_LASTWIN should be set.
792 if (msc_is_last_win(win
)) {
793 sw_tag
|= MSC_SW_TAG_LASTWIN
;
794 next_win
= list_entry(msc
->win_list
.next
,
795 struct msc_window
, entry
);
797 next_win
= list_entry(win
->entry
.next
,
798 struct msc_window
, entry
);
801 for (blk
= 0; blk
< win
->nr_blocks
; blk
++) {
802 struct msc_block_desc
*bdesc
= win
->block
[blk
].bdesc
;
804 memset(bdesc
, 0, sizeof(*bdesc
));
806 bdesc
->next_win
= next_win
->block
[0].addr
>> PAGE_SHIFT
;
809 * Similarly to last window, last block should point
812 if (blk
== win
->nr_blocks
- 1) {
813 sw_tag
|= MSC_SW_TAG_LASTBLK
;
815 win
->block
[0].addr
>> PAGE_SHIFT
;
818 win
->block
[blk
+ 1].addr
>> PAGE_SHIFT
;
821 bdesc
->sw_tag
= sw_tag
;
822 bdesc
->block_sz
= PAGE_SIZE
/ 64;
827 * Make the above writes globally visible before tracing is
828 * enabled to make sure hardware sees them coherently.
833 static void msc_buffer_multi_free(struct msc
*msc
)
835 struct msc_window
*win
, *iter
;
837 list_for_each_entry_safe(win
, iter
, &msc
->win_list
, entry
)
838 msc_buffer_win_free(msc
, win
);
841 static int msc_buffer_multi_alloc(struct msc
*msc
, unsigned long *nr_pages
,
842 unsigned int nr_wins
)
846 for (i
= 0; i
< nr_wins
; i
++) {
847 ret
= msc_buffer_win_alloc(msc
, nr_pages
[i
]);
849 msc_buffer_multi_free(msc
);
854 msc_buffer_relink(msc
);
860 * msc_buffer_free() - free buffers for MSC
863 * Free MSC's storage buffers.
865 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex to
866 * serialize, so the caller is expected to hold it.
868 static void msc_buffer_free(struct msc
*msc
)
870 if (msc
->mode
== MSC_MODE_SINGLE
)
871 msc_buffer_contig_free(msc
);
872 else if (msc
->mode
== MSC_MODE_MULTI
)
873 msc_buffer_multi_free(msc
);
877 * msc_buffer_alloc() - allocate a buffer for MSC
879 * @size: allocation size in bytes
881 * Allocate a storage buffer for MSC, depending on the msc::mode, it will be
882 * either done via msc_buffer_contig_alloc() for SINGLE operation mode or
883 * msc_buffer_win_alloc() for multiblock operation. The latter allocates one
884 * window per invocation, so in multiblock mode this can be called multiple
885 * times for the same MSC to allocate multiple windows.
887 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
888 * to serialize, so the caller is expected to hold it.
890 * Return: 0 on success, -errno otherwise.
892 static int msc_buffer_alloc(struct msc
*msc
, unsigned long *nr_pages
,
893 unsigned int nr_wins
)
897 /* -1: buffer not allocated */
898 if (atomic_read(&msc
->user_count
) != -1)
901 if (msc
->mode
== MSC_MODE_SINGLE
) {
905 ret
= msc_buffer_contig_alloc(msc
, nr_pages
[0] << PAGE_SHIFT
);
906 } else if (msc
->mode
== MSC_MODE_MULTI
) {
907 ret
= msc_buffer_multi_alloc(msc
, nr_pages
, nr_wins
);
913 /* allocation should be visible before the counter goes to 0 */
914 smp_mb__before_atomic();
916 if (WARN_ON_ONCE(atomic_cmpxchg(&msc
->user_count
, -1, 0) != -1))
924 * msc_buffer_unlocked_free_unless_used() - free a buffer unless it's in use
927 * This will free MSC buffer unless it is in use or there is no allocated
929 * Caller needs to hold msc::buf_mutex.
931 * Return: 0 on successful deallocation or if there was no buffer to
932 * deallocate, -EBUSY if there are active users.
934 static int msc_buffer_unlocked_free_unless_used(struct msc
*msc
)
938 count
= atomic_cmpxchg(&msc
->user_count
, 0, -1);
940 /* > 0: buffer is allocated and has users */
943 /* 0: buffer is allocated, no users */
945 msc_buffer_free(msc
);
946 /* < 0: no buffer, nothing to do */
952 * msc_buffer_free_unless_used() - free a buffer unless it's in use
955 * This is a locked version of msc_buffer_unlocked_free_unless_used().
957 static int msc_buffer_free_unless_used(struct msc
*msc
)
961 mutex_lock(&msc
->buf_mutex
);
962 ret
= msc_buffer_unlocked_free_unless_used(msc
);
963 mutex_unlock(&msc
->buf_mutex
);
969 * msc_buffer_get_page() - get MSC buffer page at a given offset
971 * @pgoff: page offset into the storage buffer
973 * This traverses msc::win_list, so holding msc::buf_mutex is expected from
976 * Return: page if @pgoff corresponds to a valid buffer page or NULL.
978 static struct page
*msc_buffer_get_page(struct msc
*msc
, unsigned long pgoff
)
980 struct msc_window
*win
;
982 if (msc
->mode
== MSC_MODE_SINGLE
)
983 return msc_buffer_contig_get_page(msc
, pgoff
);
985 list_for_each_entry(win
, &msc
->win_list
, entry
)
986 if (pgoff
>= win
->pgoff
&& pgoff
< win
->pgoff
+ win
->nr_blocks
)
993 return virt_to_page(win
->block
[pgoff
].bdesc
);
997 * struct msc_win_to_user_struct - data for copy_to_user() callback
998 * @buf: userspace buffer to copy data to
999 * @offset: running offset
1001 struct msc_win_to_user_struct
{
1003 unsigned long offset
;
1007 * msc_win_to_user() - iterator for msc_buffer_iterate() to copy data to user
1008 * @data: callback's private data
1009 * @src: source buffer
1010 * @len: amount of data to copy from the source buffer
1012 static unsigned long msc_win_to_user(void *data
, void *src
, size_t len
)
1014 struct msc_win_to_user_struct
*u
= data
;
1017 ret
= copy_to_user(u
->buf
+ u
->offset
, src
, len
);
1018 u
->offset
+= len
- ret
;
1025 * file operations' callbacks
1028 static int intel_th_msc_open(struct inode
*inode
, struct file
*file
)
1030 struct intel_th_device
*thdev
= file
->private_data
;
1031 struct msc
*msc
= dev_get_drvdata(&thdev
->dev
);
1032 struct msc_iter
*iter
;
1034 if (!capable(CAP_SYS_RAWIO
))
1037 iter
= msc_iter_install(msc
);
1041 file
->private_data
= iter
;
1043 return nonseekable_open(inode
, file
);
1046 static int intel_th_msc_release(struct inode
*inode
, struct file
*file
)
1048 struct msc_iter
*iter
= file
->private_data
;
1049 struct msc
*msc
= iter
->msc
;
1051 msc_iter_remove(iter
, msc
);
1057 msc_single_to_user(struct msc
*msc
, char __user
*buf
, loff_t off
, size_t len
)
1059 unsigned long size
= msc
->nr_pages
<< PAGE_SHIFT
, rem
= len
;
1060 unsigned long start
= off
, tocopy
= 0;
1062 if (msc
->single_wrap
) {
1063 start
+= msc
->single_sz
;
1065 tocopy
= min(rem
, size
- start
);
1066 if (copy_to_user(buf
, msc
->base
+ start
, tocopy
))
1076 tocopy
= min(rem
, msc
->single_sz
- start
);
1077 if (copy_to_user(buf
, msc
->base
+ start
, tocopy
))
1086 if (copy_to_user(buf
, msc
->base
+ start
, rem
))
1092 static ssize_t
intel_th_msc_read(struct file
*file
, char __user
*buf
,
1093 size_t len
, loff_t
*ppos
)
1095 struct msc_iter
*iter
= file
->private_data
;
1096 struct msc
*msc
= iter
->msc
;
1101 if (!atomic_inc_unless_negative(&msc
->user_count
))
1109 if (msc
->mode
== MSC_MODE_SINGLE
&& !msc
->single_wrap
)
1110 size
= msc
->single_sz
;
1112 size
= msc
->nr_pages
<< PAGE_SHIFT
;
1121 if (off
+ len
>= size
)
1124 if (msc
->mode
== MSC_MODE_SINGLE
) {
1125 ret
= msc_single_to_user(msc
, buf
, off
, len
);
1128 } else if (msc
->mode
== MSC_MODE_MULTI
) {
1129 struct msc_win_to_user_struct u
= {
1134 ret
= msc_buffer_iterate(iter
, len
, &u
, msc_win_to_user
);
1136 *ppos
= iter
->offset
;
1142 atomic_dec(&msc
->user_count
);
1148 * vm operations callbacks (vm_ops)
1151 static void msc_mmap_open(struct vm_area_struct
*vma
)
1153 struct msc_iter
*iter
= vma
->vm_file
->private_data
;
1154 struct msc
*msc
= iter
->msc
;
1156 atomic_inc(&msc
->mmap_count
);
1159 static void msc_mmap_close(struct vm_area_struct
*vma
)
1161 struct msc_iter
*iter
= vma
->vm_file
->private_data
;
1162 struct msc
*msc
= iter
->msc
;
1165 if (!atomic_dec_and_mutex_lock(&msc
->mmap_count
, &msc
->buf_mutex
))
1168 /* drop page _counts */
1169 for (pg
= 0; pg
< msc
->nr_pages
; pg
++) {
1170 struct page
*page
= msc_buffer_get_page(msc
, pg
);
1172 if (WARN_ON_ONCE(!page
))
1176 page
->mapping
= NULL
;
1179 /* last mapping -- drop user_count */
1180 atomic_dec(&msc
->user_count
);
1181 mutex_unlock(&msc
->buf_mutex
);
1184 static int msc_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1186 struct msc_iter
*iter
= vma
->vm_file
->private_data
;
1187 struct msc
*msc
= iter
->msc
;
1189 vmf
->page
= msc_buffer_get_page(msc
, vmf
->pgoff
);
1191 return VM_FAULT_SIGBUS
;
1193 get_page(vmf
->page
);
1194 vmf
->page
->mapping
= vma
->vm_file
->f_mapping
;
1195 vmf
->page
->index
= vmf
->pgoff
;
1200 static const struct vm_operations_struct msc_mmap_ops
= {
1201 .open
= msc_mmap_open
,
1202 .close
= msc_mmap_close
,
1203 .fault
= msc_mmap_fault
,
1206 static int intel_th_msc_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1208 unsigned long size
= vma
->vm_end
- vma
->vm_start
;
1209 struct msc_iter
*iter
= vma
->vm_file
->private_data
;
1210 struct msc
*msc
= iter
->msc
;
1213 if (!size
|| offset_in_page(size
))
1219 /* grab user_count once per mmap; drop in msc_mmap_close() */
1220 if (!atomic_inc_unless_negative(&msc
->user_count
))
1223 if (msc
->mode
!= MSC_MODE_SINGLE
&&
1224 msc
->mode
!= MSC_MODE_MULTI
)
1227 if (size
>> PAGE_SHIFT
!= msc
->nr_pages
)
1230 atomic_set(&msc
->mmap_count
, 1);
1235 atomic_dec(&msc
->user_count
);
1237 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
1238 vma
->vm_flags
|= VM_DONTEXPAND
| VM_DONTCOPY
;
1239 vma
->vm_ops
= &msc_mmap_ops
;
1243 static const struct file_operations intel_th_msc_fops
= {
1244 .open
= intel_th_msc_open
,
1245 .release
= intel_th_msc_release
,
1246 .read
= intel_th_msc_read
,
1247 .mmap
= intel_th_msc_mmap
,
1248 .llseek
= no_llseek
,
1251 static int intel_th_msc_init(struct msc
*msc
)
1253 atomic_set(&msc
->user_count
, -1);
1255 msc
->mode
= MSC_MODE_MULTI
;
1256 mutex_init(&msc
->buf_mutex
);
1257 INIT_LIST_HEAD(&msc
->win_list
);
1259 mutex_init(&msc
->iter_mutex
);
1260 INIT_LIST_HEAD(&msc
->iter_list
);
1263 (ioread32(msc
->reg_base
+ REG_MSU_MSC0CTL
) & MSC_LEN
) >>
1269 static const char * const msc_mode
[] = {
1270 [MSC_MODE_SINGLE
] = "single",
1271 [MSC_MODE_MULTI
] = "multi",
1272 [MSC_MODE_EXI
] = "ExI",
1273 [MSC_MODE_DEBUG
] = "debug",
1277 wrap_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1279 struct msc
*msc
= dev_get_drvdata(dev
);
1281 return scnprintf(buf
, PAGE_SIZE
, "%d\n", msc
->wrap
);
1285 wrap_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
,
1288 struct msc
*msc
= dev_get_drvdata(dev
);
1292 ret
= kstrtoul(buf
, 10, &val
);
1301 static DEVICE_ATTR_RW(wrap
);
1304 mode_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1306 struct msc
*msc
= dev_get_drvdata(dev
);
1308 return scnprintf(buf
, PAGE_SIZE
, "%s\n", msc_mode
[msc
->mode
]);
1312 mode_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
,
1315 struct msc
*msc
= dev_get_drvdata(dev
);
1320 if (!capable(CAP_SYS_RAWIO
))
1323 cp
= memchr(buf
, '\n', len
);
1327 for (i
= 0; i
< ARRAY_SIZE(msc_mode
); i
++)
1328 if (!strncmp(msc_mode
[i
], buf
, len
))
1334 mutex_lock(&msc
->buf_mutex
);
1335 ret
= msc_buffer_unlocked_free_unless_used(msc
);
1338 mutex_unlock(&msc
->buf_mutex
);
1340 return ret
? ret
: size
;
1343 static DEVICE_ATTR_RW(mode
);
1346 nr_pages_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1348 struct msc
*msc
= dev_get_drvdata(dev
);
1349 struct msc_window
*win
;
1352 mutex_lock(&msc
->buf_mutex
);
1354 if (msc
->mode
== MSC_MODE_SINGLE
)
1355 count
= scnprintf(buf
, PAGE_SIZE
, "%ld\n", msc
->nr_pages
);
1356 else if (msc
->mode
== MSC_MODE_MULTI
) {
1357 list_for_each_entry(win
, &msc
->win_list
, entry
) {
1358 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1359 "%d%c", win
->nr_blocks
,
1360 msc_is_last_win(win
) ? '\n' : ',');
1363 count
= scnprintf(buf
, PAGE_SIZE
, "unsupported\n");
1366 mutex_unlock(&msc
->buf_mutex
);
1372 nr_pages_store(struct device
*dev
, struct device_attribute
*attr
,
1373 const char *buf
, size_t size
)
1375 struct msc
*msc
= dev_get_drvdata(dev
);
1376 unsigned long val
, *win
= NULL
, *rewin
;
1378 const char *p
= buf
;
1380 int ret
, nr_wins
= 0;
1382 if (!capable(CAP_SYS_RAWIO
))
1385 ret
= msc_buffer_free_unless_used(msc
);
1389 /* scan the comma-separated list of allocation sizes */
1390 end
= memchr(buf
, '\n', len
);
1395 end
= memchr(p
, ',', len
);
1396 s
= kstrndup(p
, end
? end
- p
: len
, GFP_KERNEL
);
1397 ret
= kstrtoul(s
, 10, &val
);
1403 if (nr_wins
&& msc
->mode
== MSC_MODE_SINGLE
) {
1409 rewin
= krealloc(win
, sizeof(*win
) * nr_wins
, GFP_KERNEL
);
1416 win
[nr_wins
- 1] = val
;
1425 mutex_lock(&msc
->buf_mutex
);
1426 ret
= msc_buffer_alloc(msc
, win
, nr_wins
);
1427 mutex_unlock(&msc
->buf_mutex
);
1432 return ret
? ret
: size
;
1435 static DEVICE_ATTR_RW(nr_pages
);
1437 static struct attribute
*msc_output_attrs
[] = {
1438 &dev_attr_wrap
.attr
,
1439 &dev_attr_mode
.attr
,
1440 &dev_attr_nr_pages
.attr
,
1444 static struct attribute_group msc_output_group
= {
1445 .attrs
= msc_output_attrs
,
1448 static int intel_th_msc_probe(struct intel_th_device
*thdev
)
1450 struct device
*dev
= &thdev
->dev
;
1451 struct resource
*res
;
1456 res
= intel_th_device_get_resource(thdev
, IORESOURCE_MEM
, 0);
1460 base
= devm_ioremap(dev
, res
->start
, resource_size(res
));
1464 msc
= devm_kzalloc(dev
, sizeof(*msc
), GFP_KERNEL
);
1468 msc
->index
= thdev
->id
;
1471 msc
->reg_base
= base
+ msc
->index
* 0x100;
1473 err
= intel_th_msc_init(msc
);
1477 err
= sysfs_create_group(&dev
->kobj
, &msc_output_group
);
1481 dev_set_drvdata(dev
, msc
);
1486 static void intel_th_msc_remove(struct intel_th_device
*thdev
)
1488 sysfs_remove_group(&thdev
->dev
.kobj
, &msc_output_group
);
1491 static struct intel_th_driver intel_th_msc_driver
= {
1492 .probe
= intel_th_msc_probe
,
1493 .remove
= intel_th_msc_remove
,
1494 .activate
= intel_th_msc_activate
,
1495 .deactivate
= intel_th_msc_deactivate
,
1496 .fops
= &intel_th_msc_fops
,
1499 .owner
= THIS_MODULE
,
1503 module_driver(intel_th_msc_driver
,
1504 intel_th_driver_register
,
1505 intel_th_driver_unregister
);
1507 MODULE_LICENSE("GPL v2");
1508 MODULE_DESCRIPTION("Intel(R) Trace Hub Memory Storage Unit driver");
1509 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");