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 list_head iter_list
;
128 unsigned int enabled
: 1,
131 unsigned int burst_len
;
135 static inline bool msc_block_is_empty(struct msc_block_desc
*bdesc
)
137 /* header hasn't been written */
138 if (!bdesc
->valid_dw
)
141 /* valid_dw includes the header */
142 if (!msc_data_sz(bdesc
))
149 * msc_oldest_window() - locate the window with oldest data
152 * This should only be used in multiblock mode. Caller should hold the
153 * msc::user_count reference.
155 * Return: the oldest window with valid data
157 static struct msc_window
*msc_oldest_window(struct msc
*msc
)
159 struct msc_window
*win
;
160 u32 reg
= ioread32(msc
->reg_base
+ REG_MSU_MSC0NWSA
);
161 unsigned long win_addr
= (unsigned long)reg
<< PAGE_SHIFT
;
162 unsigned int found
= 0;
164 if (list_empty(&msc
->win_list
))
168 * we might need a radix tree for this, depending on how
169 * many windows a typical user would allocate; ideally it's
170 * something like 2, in which case we're good
172 list_for_each_entry(win
, &msc
->win_list
, entry
) {
173 if (win
->block
[0].addr
== win_addr
)
176 /* skip the empty ones */
177 if (msc_block_is_empty(win
->block
[0].bdesc
))
184 return list_entry(msc
->win_list
.next
, struct msc_window
, entry
);
188 * msc_win_oldest_block() - locate the oldest block in a given window
189 * @win: window to look at
191 * Return: index of the block with the oldest data
193 static unsigned int msc_win_oldest_block(struct msc_window
*win
)
196 struct msc_block_desc
*bdesc
= win
->block
[0].bdesc
;
198 /* without wrapping, first block is the oldest */
199 if (!msc_block_wrapped(bdesc
))
203 * with wrapping, last written block contains both the newest and the
204 * oldest data for this window.
206 for (blk
= 0; blk
< win
->nr_blocks
; blk
++) {
207 bdesc
= win
->block
[blk
].bdesc
;
209 if (msc_block_last_written(bdesc
))
217 * msc_is_last_win() - check if a window is the last one for a given MSC
219 * Return: true if @win is the last window in MSC's multiblock buffer
221 static inline bool msc_is_last_win(struct msc_window
*win
)
223 return win
->entry
.next
== &win
->msc
->win_list
;
227 * msc_next_window() - return next window in the multiblock buffer
228 * @win: current window
230 * Return: window following the current one
232 static struct msc_window
*msc_next_window(struct msc_window
*win
)
234 if (msc_is_last_win(win
))
235 return list_entry(win
->msc
->win_list
.next
, struct msc_window
,
238 return list_entry(win
->entry
.next
, struct msc_window
, entry
);
241 static struct msc_block_desc
*msc_iter_bdesc(struct msc_iter
*iter
)
243 return iter
->win
->block
[iter
->block
].bdesc
;
246 static void msc_iter_init(struct msc_iter
*iter
)
248 memset(iter
, 0, sizeof(*iter
));
249 iter
->start_block
= -1;
253 static struct msc_iter
*msc_iter_install(struct msc
*msc
)
255 struct msc_iter
*iter
;
257 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
259 return ERR_PTR(-ENOMEM
);
261 mutex_lock(&msc
->buf_mutex
);
264 * Reading and tracing are mutually exclusive; if msc is
265 * enabled, open() will fail; otherwise existing readers
266 * will prevent enabling the msc and the rest of fops don't
267 * need to worry about it.
271 iter
= ERR_PTR(-EBUSY
);
278 list_add_tail(&iter
->entry
, &msc
->iter_list
);
280 mutex_unlock(&msc
->buf_mutex
);
285 static void msc_iter_remove(struct msc_iter
*iter
, struct msc
*msc
)
287 mutex_lock(&msc
->buf_mutex
);
288 list_del(&iter
->entry
);
289 mutex_unlock(&msc
->buf_mutex
);
294 static void msc_iter_block_start(struct msc_iter
*iter
)
296 if (iter
->start_block
!= -1)
299 iter
->start_block
= msc_win_oldest_block(iter
->win
);
300 iter
->block
= iter
->start_block
;
301 iter
->wrap_count
= 0;
304 * start with the block with oldest data; if data has wrapped
305 * in this window, it should be in this block
307 if (msc_block_wrapped(msc_iter_bdesc(iter
)))
308 iter
->wrap_count
= 2;
312 static int msc_iter_win_start(struct msc_iter
*iter
, struct msc
*msc
)
314 /* already started, nothing to do */
318 iter
->start_win
= msc_oldest_window(msc
);
319 if (!iter
->start_win
)
322 iter
->win
= iter
->start_win
;
323 iter
->start_block
= -1;
325 msc_iter_block_start(iter
);
330 static int msc_iter_win_advance(struct msc_iter
*iter
)
332 iter
->win
= msc_next_window(iter
->win
);
333 iter
->start_block
= -1;
335 if (iter
->win
== iter
->start_win
) {
340 msc_iter_block_start(iter
);
345 static int msc_iter_block_advance(struct msc_iter
*iter
)
350 if (iter
->wrap_count
&& iter
->block
== iter
->start_block
) {
352 if (!iter
->wrap_count
)
353 /* copied newest data from the wrapped block */
354 return msc_iter_win_advance(iter
);
357 /* no wrapping, check for last written block */
358 if (!iter
->wrap_count
&& msc_block_last_written(msc_iter_bdesc(iter
)))
359 /* copied newest data for the window */
360 return msc_iter_win_advance(iter
);
363 if (++iter
->block
== iter
->win
->nr_blocks
)
366 /* no wrapping, sanity check in case there is no last written block */
367 if (!iter
->wrap_count
&& iter
->block
== iter
->start_block
)
368 return msc_iter_win_advance(iter
);
374 * msc_buffer_iterate() - go through multiblock buffer's data
375 * @iter: iterator structure
376 * @size: amount of data to scan
377 * @data: callback's private data
378 * @fn: iterator callback
380 * This will start at the window which will be written to next (containing
381 * the oldest data) and work its way to the current window, calling @fn
382 * for each chunk of data as it goes.
384 * Caller should have msc::user_count reference to make sure the buffer
385 * doesn't disappear from under us.
387 * Return: amount of data actually scanned.
390 msc_buffer_iterate(struct msc_iter
*iter
, size_t size
, void *data
,
391 unsigned long (*fn
)(void *, void *, size_t))
393 struct msc
*msc
= iter
->msc
;
395 unsigned int advance
;
400 /* start with the oldest window */
401 if (msc_iter_win_start(iter
, msc
))
405 unsigned long data_bytes
= msc_data_sz(msc_iter_bdesc(iter
));
406 void *src
= (void *)msc_iter_bdesc(iter
) + MSC_BDESC
;
407 size_t tocopy
= data_bytes
, copied
= 0;
408 size_t remaining
= 0;
413 * If block wrapping happened, we need to visit the last block
414 * twice, because it contains both the oldest and the newest
415 * data in this window.
417 * First time (wrap_count==2), in the very beginning, to collect
418 * the oldest data, which is in the range
419 * (data_bytes..DATA_IN_PAGE).
421 * Second time (wrap_count==1), it's just like any other block,
422 * containing data in the range of [MSC_BDESC..data_bytes].
424 if (iter
->block
== iter
->start_block
&& iter
->wrap_count
== 2) {
425 tocopy
= DATA_IN_PAGE
- data_bytes
;
432 tocopy
-= iter
->block_off
;
433 src
+= iter
->block_off
;
440 remaining
= fn(data
, src
, tocopy
);
445 copied
= tocopy
- remaining
;
447 iter
->block_off
+= copied
;
448 iter
->offset
+= copied
;
454 if (msc_iter_block_advance(iter
))
463 * msc_buffer_clear_hw_header() - clear hw header for multiblock
466 static void msc_buffer_clear_hw_header(struct msc
*msc
)
468 struct msc_window
*win
;
470 list_for_each_entry(win
, &msc
->win_list
, entry
) {
472 size_t hw_sz
= sizeof(struct msc_block_desc
) -
473 offsetof(struct msc_block_desc
, hw_tag
);
475 for (blk
= 0; blk
< win
->nr_blocks
; blk
++) {
476 struct msc_block_desc
*bdesc
= win
->block
[blk
].bdesc
;
478 memset(&bdesc
->hw_tag
, 0, hw_sz
);
484 * msc_configure() - set up MSC hardware
485 * @msc: the MSC device to configure
487 * Program storage mode, wrapping, burst length and trace buffer address
488 * into a given MSC. Then, enable tracing and set msc::enabled.
489 * The latter is serialized on msc::buf_mutex, so make sure to hold it.
491 static int msc_configure(struct msc
*msc
)
495 lockdep_assert_held(&msc
->buf_mutex
);
497 if (msc
->mode
> MSC_MODE_MULTI
)
500 if (msc
->mode
== MSC_MODE_MULTI
)
501 msc_buffer_clear_hw_header(msc
);
503 reg
= msc
->base_addr
>> PAGE_SHIFT
;
504 iowrite32(reg
, msc
->reg_base
+ REG_MSU_MSC0BAR
);
506 if (msc
->mode
== MSC_MODE_SINGLE
) {
508 iowrite32(reg
, msc
->reg_base
+ REG_MSU_MSC0SIZE
);
511 reg
= ioread32(msc
->reg_base
+ REG_MSU_MSC0CTL
);
512 reg
&= ~(MSC_MODE
| MSC_WRAPEN
| MSC_EN
| MSC_RD_HDR_OVRD
);
515 reg
|= msc
->mode
<< __ffs(MSC_MODE
);
516 reg
|= msc
->burst_len
<< __ffs(MSC_LEN
);
521 iowrite32(reg
, msc
->reg_base
+ REG_MSU_MSC0CTL
);
523 msc
->thdev
->output
.multiblock
= msc
->mode
== MSC_MODE_MULTI
;
524 intel_th_trace_enable(msc
->thdev
);
532 * msc_disable() - disable MSC hardware
533 * @msc: MSC device to disable
535 * If @msc is enabled, disable tracing on the switch and then disable MSC
536 * storage. Caller must hold msc::buf_mutex.
538 static void msc_disable(struct msc
*msc
)
543 lockdep_assert_held(&msc
->buf_mutex
);
545 intel_th_trace_disable(msc
->thdev
);
547 for (reg
= 0, count
= MSC_PLE_WAITLOOP_DEPTH
;
548 count
&& !(reg
& MSCSTS_PLE
); count
--) {
549 reg
= ioread32(msc
->reg_base
+ REG_MSU_MSC0STS
);
554 dev_dbg(msc_dev(msc
), "timeout waiting for MSC0 PLE\n");
556 if (msc
->mode
== MSC_MODE_SINGLE
) {
557 msc
->single_wrap
= !!(reg
& MSCSTS_WRAPSTAT
);
559 reg
= ioread32(msc
->reg_base
+ REG_MSU_MSC0MWP
);
560 msc
->single_sz
= reg
& ((msc
->nr_pages
<< PAGE_SHIFT
) - 1);
561 dev_dbg(msc_dev(msc
), "MSCnMWP: %08x/%08lx, wrap: %d\n",
562 reg
, msc
->single_sz
, msc
->single_wrap
);
565 reg
= ioread32(msc
->reg_base
+ REG_MSU_MSC0CTL
);
567 iowrite32(reg
, msc
->reg_base
+ REG_MSU_MSC0CTL
);
570 iowrite32(0, msc
->reg_base
+ REG_MSU_MSC0BAR
);
571 iowrite32(0, msc
->reg_base
+ REG_MSU_MSC0SIZE
);
573 dev_dbg(msc_dev(msc
), "MSCnNWSA: %08x\n",
574 ioread32(msc
->reg_base
+ REG_MSU_MSC0NWSA
));
576 reg
= ioread32(msc
->reg_base
+ REG_MSU_MSC0STS
);
577 dev_dbg(msc_dev(msc
), "MSCnSTS: %08x\n", reg
);
580 static int intel_th_msc_activate(struct intel_th_device
*thdev
)
582 struct msc
*msc
= dev_get_drvdata(&thdev
->dev
);
585 if (!atomic_inc_unless_negative(&msc
->user_count
))
588 mutex_lock(&msc
->buf_mutex
);
590 /* if there are readers, refuse */
591 if (list_empty(&msc
->iter_list
))
592 ret
= msc_configure(msc
);
594 mutex_unlock(&msc
->buf_mutex
);
597 atomic_dec(&msc
->user_count
);
602 static void intel_th_msc_deactivate(struct intel_th_device
*thdev
)
604 struct msc
*msc
= dev_get_drvdata(&thdev
->dev
);
606 mutex_lock(&msc
->buf_mutex
);
609 atomic_dec(&msc
->user_count
);
611 mutex_unlock(&msc
->buf_mutex
);
615 * msc_buffer_contig_alloc() - allocate a contiguous buffer for SINGLE mode
617 * @size: allocation size in bytes
619 * This modifies msc::base, which requires msc::buf_mutex to serialize, so the
620 * caller is expected to hold it.
622 * Return: 0 on success, -errno otherwise.
624 static int msc_buffer_contig_alloc(struct msc
*msc
, unsigned long size
)
626 unsigned int order
= get_order(size
);
632 page
= alloc_pages(GFP_KERNEL
| __GFP_ZERO
, order
);
636 split_page(page
, order
);
637 msc
->nr_pages
= size
>> PAGE_SHIFT
;
638 msc
->base
= page_address(page
);
639 msc
->base_addr
= page_to_phys(page
);
645 * msc_buffer_contig_free() - free a contiguous buffer
646 * @msc: MSC configured in SINGLE mode
648 static void msc_buffer_contig_free(struct msc
*msc
)
652 for (off
= 0; off
< msc
->nr_pages
<< PAGE_SHIFT
; off
+= PAGE_SIZE
) {
653 struct page
*page
= virt_to_page(msc
->base
+ off
);
655 page
->mapping
= NULL
;
663 * msc_buffer_contig_get_page() - find a page at a given offset
664 * @msc: MSC configured in SINGLE mode
665 * @pgoff: page offset
667 * Return: page, if @pgoff is within the range, NULL otherwise.
669 static struct page
*msc_buffer_contig_get_page(struct msc
*msc
,
672 if (pgoff
>= msc
->nr_pages
)
675 return virt_to_page(msc
->base
+ (pgoff
<< PAGE_SHIFT
));
679 * msc_buffer_win_alloc() - alloc a window for a multiblock mode
681 * @nr_blocks: number of pages in this window
683 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
684 * to serialize, so the caller is expected to hold it.
686 * Return: 0 on success, -errno otherwise.
688 static int msc_buffer_win_alloc(struct msc
*msc
, unsigned int nr_blocks
)
690 struct msc_window
*win
;
691 unsigned long size
= PAGE_SIZE
;
692 int i
, ret
= -ENOMEM
;
697 win
= kzalloc(offsetof(struct msc_window
, block
[nr_blocks
]),
702 if (!list_empty(&msc
->win_list
)) {
703 struct msc_window
*prev
= list_entry(msc
->win_list
.prev
,
704 struct msc_window
, entry
);
706 win
->pgoff
= prev
->pgoff
+ prev
->nr_blocks
;
709 for (i
= 0; i
< nr_blocks
; i
++) {
710 win
->block
[i
].bdesc
= dma_alloc_coherent(msc_dev(msc
), size
,
715 /* Set the page as uncached */
716 set_memory_uc((unsigned long)win
->block
[i
].bdesc
, 1);
719 if (!win
->block
[i
].bdesc
)
724 win
->nr_blocks
= nr_blocks
;
726 if (list_empty(&msc
->win_list
)) {
727 msc
->base
= win
->block
[0].bdesc
;
728 msc
->base_addr
= win
->block
[0].addr
;
731 list_add_tail(&win
->entry
, &msc
->win_list
);
732 msc
->nr_pages
+= nr_blocks
;
737 for (i
--; i
>= 0; i
--) {
739 /* Reset the page to write-back before releasing */
740 set_memory_wb((unsigned long)win
->block
[i
].bdesc
, 1);
742 dma_free_coherent(msc_dev(msc
), size
, win
->block
[i
].bdesc
,
751 * msc_buffer_win_free() - free a window from MSC's window list
753 * @win: window to free
755 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
756 * to serialize, so the caller is expected to hold it.
758 static void msc_buffer_win_free(struct msc
*msc
, struct msc_window
*win
)
762 msc
->nr_pages
-= win
->nr_blocks
;
764 list_del(&win
->entry
);
765 if (list_empty(&msc
->win_list
)) {
770 for (i
= 0; i
< win
->nr_blocks
; i
++) {
771 struct page
*page
= virt_to_page(win
->block
[i
].bdesc
);
773 page
->mapping
= NULL
;
775 /* Reset the page to write-back before releasing */
776 set_memory_wb((unsigned long)win
->block
[i
].bdesc
, 1);
778 dma_free_coherent(msc_dev(win
->msc
), PAGE_SIZE
,
779 win
->block
[i
].bdesc
, win
->block
[i
].addr
);
786 * msc_buffer_relink() - set up block descriptors for multiblock mode
789 * This traverses msc::win_list, which requires msc::buf_mutex to serialize,
790 * so the caller is expected to hold it.
792 static void msc_buffer_relink(struct msc
*msc
)
794 struct msc_window
*win
, *next_win
;
796 /* call with msc::mutex locked */
797 list_for_each_entry(win
, &msc
->win_list
, entry
) {
802 * Last window's next_win should point to the first window
803 * and MSC_SW_TAG_LASTWIN should be set.
805 if (msc_is_last_win(win
)) {
806 sw_tag
|= MSC_SW_TAG_LASTWIN
;
807 next_win
= list_entry(msc
->win_list
.next
,
808 struct msc_window
, entry
);
810 next_win
= list_entry(win
->entry
.next
,
811 struct msc_window
, entry
);
814 for (blk
= 0; blk
< win
->nr_blocks
; blk
++) {
815 struct msc_block_desc
*bdesc
= win
->block
[blk
].bdesc
;
817 memset(bdesc
, 0, sizeof(*bdesc
));
819 bdesc
->next_win
= next_win
->block
[0].addr
>> PAGE_SHIFT
;
822 * Similarly to last window, last block should point
825 if (blk
== win
->nr_blocks
- 1) {
826 sw_tag
|= MSC_SW_TAG_LASTBLK
;
828 win
->block
[0].addr
>> PAGE_SHIFT
;
831 win
->block
[blk
+ 1].addr
>> PAGE_SHIFT
;
834 bdesc
->sw_tag
= sw_tag
;
835 bdesc
->block_sz
= PAGE_SIZE
/ 64;
840 * Make the above writes globally visible before tracing is
841 * enabled to make sure hardware sees them coherently.
846 static void msc_buffer_multi_free(struct msc
*msc
)
848 struct msc_window
*win
, *iter
;
850 list_for_each_entry_safe(win
, iter
, &msc
->win_list
, entry
)
851 msc_buffer_win_free(msc
, win
);
854 static int msc_buffer_multi_alloc(struct msc
*msc
, unsigned long *nr_pages
,
855 unsigned int nr_wins
)
859 for (i
= 0; i
< nr_wins
; i
++) {
860 ret
= msc_buffer_win_alloc(msc
, nr_pages
[i
]);
862 msc_buffer_multi_free(msc
);
867 msc_buffer_relink(msc
);
873 * msc_buffer_free() - free buffers for MSC
876 * Free MSC's storage buffers.
878 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex to
879 * serialize, so the caller is expected to hold it.
881 static void msc_buffer_free(struct msc
*msc
)
883 if (msc
->mode
== MSC_MODE_SINGLE
)
884 msc_buffer_contig_free(msc
);
885 else if (msc
->mode
== MSC_MODE_MULTI
)
886 msc_buffer_multi_free(msc
);
890 * msc_buffer_alloc() - allocate a buffer for MSC
892 * @size: allocation size in bytes
894 * Allocate a storage buffer for MSC, depending on the msc::mode, it will be
895 * either done via msc_buffer_contig_alloc() for SINGLE operation mode or
896 * msc_buffer_win_alloc() for multiblock operation. The latter allocates one
897 * window per invocation, so in multiblock mode this can be called multiple
898 * times for the same MSC to allocate multiple windows.
900 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
901 * to serialize, so the caller is expected to hold it.
903 * Return: 0 on success, -errno otherwise.
905 static int msc_buffer_alloc(struct msc
*msc
, unsigned long *nr_pages
,
906 unsigned int nr_wins
)
910 /* -1: buffer not allocated */
911 if (atomic_read(&msc
->user_count
) != -1)
914 if (msc
->mode
== MSC_MODE_SINGLE
) {
918 ret
= msc_buffer_contig_alloc(msc
, nr_pages
[0] << PAGE_SHIFT
);
919 } else if (msc
->mode
== MSC_MODE_MULTI
) {
920 ret
= msc_buffer_multi_alloc(msc
, nr_pages
, nr_wins
);
926 /* allocation should be visible before the counter goes to 0 */
927 smp_mb__before_atomic();
929 if (WARN_ON_ONCE(atomic_cmpxchg(&msc
->user_count
, -1, 0) != -1))
937 * msc_buffer_unlocked_free_unless_used() - free a buffer unless it's in use
940 * This will free MSC buffer unless it is in use or there is no allocated
942 * Caller needs to hold msc::buf_mutex.
944 * Return: 0 on successful deallocation or if there was no buffer to
945 * deallocate, -EBUSY if there are active users.
947 static int msc_buffer_unlocked_free_unless_used(struct msc
*msc
)
951 count
= atomic_cmpxchg(&msc
->user_count
, 0, -1);
953 /* > 0: buffer is allocated and has users */
956 /* 0: buffer is allocated, no users */
958 msc_buffer_free(msc
);
959 /* < 0: no buffer, nothing to do */
965 * msc_buffer_free_unless_used() - free a buffer unless it's in use
968 * This is a locked version of msc_buffer_unlocked_free_unless_used().
970 static int msc_buffer_free_unless_used(struct msc
*msc
)
974 mutex_lock(&msc
->buf_mutex
);
975 ret
= msc_buffer_unlocked_free_unless_used(msc
);
976 mutex_unlock(&msc
->buf_mutex
);
982 * msc_buffer_get_page() - get MSC buffer page at a given offset
984 * @pgoff: page offset into the storage buffer
986 * This traverses msc::win_list, so holding msc::buf_mutex is expected from
989 * Return: page if @pgoff corresponds to a valid buffer page or NULL.
991 static struct page
*msc_buffer_get_page(struct msc
*msc
, unsigned long pgoff
)
993 struct msc_window
*win
;
995 if (msc
->mode
== MSC_MODE_SINGLE
)
996 return msc_buffer_contig_get_page(msc
, pgoff
);
998 list_for_each_entry(win
, &msc
->win_list
, entry
)
999 if (pgoff
>= win
->pgoff
&& pgoff
< win
->pgoff
+ win
->nr_blocks
)
1005 pgoff
-= win
->pgoff
;
1006 return virt_to_page(win
->block
[pgoff
].bdesc
);
1010 * struct msc_win_to_user_struct - data for copy_to_user() callback
1011 * @buf: userspace buffer to copy data to
1012 * @offset: running offset
1014 struct msc_win_to_user_struct
{
1016 unsigned long offset
;
1020 * msc_win_to_user() - iterator for msc_buffer_iterate() to copy data to user
1021 * @data: callback's private data
1022 * @src: source buffer
1023 * @len: amount of data to copy from the source buffer
1025 static unsigned long msc_win_to_user(void *data
, void *src
, size_t len
)
1027 struct msc_win_to_user_struct
*u
= data
;
1030 ret
= copy_to_user(u
->buf
+ u
->offset
, src
, len
);
1031 u
->offset
+= len
- ret
;
1038 * file operations' callbacks
1041 static int intel_th_msc_open(struct inode
*inode
, struct file
*file
)
1043 struct intel_th_device
*thdev
= file
->private_data
;
1044 struct msc
*msc
= dev_get_drvdata(&thdev
->dev
);
1045 struct msc_iter
*iter
;
1047 if (!capable(CAP_SYS_RAWIO
))
1050 iter
= msc_iter_install(msc
);
1052 return PTR_ERR(iter
);
1054 file
->private_data
= iter
;
1056 return nonseekable_open(inode
, file
);
1059 static int intel_th_msc_release(struct inode
*inode
, struct file
*file
)
1061 struct msc_iter
*iter
= file
->private_data
;
1062 struct msc
*msc
= iter
->msc
;
1064 msc_iter_remove(iter
, msc
);
1070 msc_single_to_user(struct msc
*msc
, char __user
*buf
, loff_t off
, size_t len
)
1072 unsigned long size
= msc
->nr_pages
<< PAGE_SHIFT
, rem
= len
;
1073 unsigned long start
= off
, tocopy
= 0;
1075 if (msc
->single_wrap
) {
1076 start
+= msc
->single_sz
;
1078 tocopy
= min(rem
, size
- start
);
1079 if (copy_to_user(buf
, msc
->base
+ start
, tocopy
))
1089 tocopy
= min(rem
, msc
->single_sz
- start
);
1090 if (copy_to_user(buf
, msc
->base
+ start
, tocopy
))
1099 if (copy_to_user(buf
, msc
->base
+ start
, rem
))
1105 static ssize_t
intel_th_msc_read(struct file
*file
, char __user
*buf
,
1106 size_t len
, loff_t
*ppos
)
1108 struct msc_iter
*iter
= file
->private_data
;
1109 struct msc
*msc
= iter
->msc
;
1114 if (!atomic_inc_unless_negative(&msc
->user_count
))
1117 if (msc
->mode
== MSC_MODE_SINGLE
&& !msc
->single_wrap
)
1118 size
= msc
->single_sz
;
1120 size
= msc
->nr_pages
<< PAGE_SHIFT
;
1128 if (off
+ len
>= size
)
1131 if (msc
->mode
== MSC_MODE_SINGLE
) {
1132 ret
= msc_single_to_user(msc
, buf
, off
, len
);
1135 } else if (msc
->mode
== MSC_MODE_MULTI
) {
1136 struct msc_win_to_user_struct u
= {
1141 ret
= msc_buffer_iterate(iter
, len
, &u
, msc_win_to_user
);
1143 *ppos
= iter
->offset
;
1149 atomic_dec(&msc
->user_count
);
1155 * vm operations callbacks (vm_ops)
1158 static void msc_mmap_open(struct vm_area_struct
*vma
)
1160 struct msc_iter
*iter
= vma
->vm_file
->private_data
;
1161 struct msc
*msc
= iter
->msc
;
1163 atomic_inc(&msc
->mmap_count
);
1166 static void msc_mmap_close(struct vm_area_struct
*vma
)
1168 struct msc_iter
*iter
= vma
->vm_file
->private_data
;
1169 struct msc
*msc
= iter
->msc
;
1172 if (!atomic_dec_and_mutex_lock(&msc
->mmap_count
, &msc
->buf_mutex
))
1175 /* drop page _refcounts */
1176 for (pg
= 0; pg
< msc
->nr_pages
; pg
++) {
1177 struct page
*page
= msc_buffer_get_page(msc
, pg
);
1179 if (WARN_ON_ONCE(!page
))
1183 page
->mapping
= NULL
;
1186 /* last mapping -- drop user_count */
1187 atomic_dec(&msc
->user_count
);
1188 mutex_unlock(&msc
->buf_mutex
);
1191 static int msc_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1193 struct msc_iter
*iter
= vma
->vm_file
->private_data
;
1194 struct msc
*msc
= iter
->msc
;
1196 vmf
->page
= msc_buffer_get_page(msc
, vmf
->pgoff
);
1198 return VM_FAULT_SIGBUS
;
1200 get_page(vmf
->page
);
1201 vmf
->page
->mapping
= vma
->vm_file
->f_mapping
;
1202 vmf
->page
->index
= vmf
->pgoff
;
1207 static const struct vm_operations_struct msc_mmap_ops
= {
1208 .open
= msc_mmap_open
,
1209 .close
= msc_mmap_close
,
1210 .fault
= msc_mmap_fault
,
1213 static int intel_th_msc_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1215 unsigned long size
= vma
->vm_end
- vma
->vm_start
;
1216 struct msc_iter
*iter
= vma
->vm_file
->private_data
;
1217 struct msc
*msc
= iter
->msc
;
1220 if (!size
|| offset_in_page(size
))
1226 /* grab user_count once per mmap; drop in msc_mmap_close() */
1227 if (!atomic_inc_unless_negative(&msc
->user_count
))
1230 if (msc
->mode
!= MSC_MODE_SINGLE
&&
1231 msc
->mode
!= MSC_MODE_MULTI
)
1234 if (size
>> PAGE_SHIFT
!= msc
->nr_pages
)
1237 atomic_set(&msc
->mmap_count
, 1);
1242 atomic_dec(&msc
->user_count
);
1244 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
1245 vma
->vm_flags
|= VM_DONTEXPAND
| VM_DONTCOPY
;
1246 vma
->vm_ops
= &msc_mmap_ops
;
1250 static const struct file_operations intel_th_msc_fops
= {
1251 .open
= intel_th_msc_open
,
1252 .release
= intel_th_msc_release
,
1253 .read
= intel_th_msc_read
,
1254 .mmap
= intel_th_msc_mmap
,
1255 .llseek
= no_llseek
,
1256 .owner
= THIS_MODULE
,
1259 static int intel_th_msc_init(struct msc
*msc
)
1261 atomic_set(&msc
->user_count
, -1);
1263 msc
->mode
= MSC_MODE_MULTI
;
1264 mutex_init(&msc
->buf_mutex
);
1265 INIT_LIST_HEAD(&msc
->win_list
);
1266 INIT_LIST_HEAD(&msc
->iter_list
);
1269 (ioread32(msc
->reg_base
+ REG_MSU_MSC0CTL
) & MSC_LEN
) >>
1275 static const char * const msc_mode
[] = {
1276 [MSC_MODE_SINGLE
] = "single",
1277 [MSC_MODE_MULTI
] = "multi",
1278 [MSC_MODE_EXI
] = "ExI",
1279 [MSC_MODE_DEBUG
] = "debug",
1283 wrap_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1285 struct msc
*msc
= dev_get_drvdata(dev
);
1287 return scnprintf(buf
, PAGE_SIZE
, "%d\n", msc
->wrap
);
1291 wrap_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
,
1294 struct msc
*msc
= dev_get_drvdata(dev
);
1298 ret
= kstrtoul(buf
, 10, &val
);
1307 static DEVICE_ATTR_RW(wrap
);
1310 mode_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1312 struct msc
*msc
= dev_get_drvdata(dev
);
1314 return scnprintf(buf
, PAGE_SIZE
, "%s\n", msc_mode
[msc
->mode
]);
1318 mode_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
,
1321 struct msc
*msc
= dev_get_drvdata(dev
);
1326 if (!capable(CAP_SYS_RAWIO
))
1329 cp
= memchr(buf
, '\n', len
);
1333 for (i
= 0; i
< ARRAY_SIZE(msc_mode
); i
++)
1334 if (!strncmp(msc_mode
[i
], buf
, len
))
1340 mutex_lock(&msc
->buf_mutex
);
1341 ret
= msc_buffer_unlocked_free_unless_used(msc
);
1344 mutex_unlock(&msc
->buf_mutex
);
1346 return ret
? ret
: size
;
1349 static DEVICE_ATTR_RW(mode
);
1352 nr_pages_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1354 struct msc
*msc
= dev_get_drvdata(dev
);
1355 struct msc_window
*win
;
1358 mutex_lock(&msc
->buf_mutex
);
1360 if (msc
->mode
== MSC_MODE_SINGLE
)
1361 count
= scnprintf(buf
, PAGE_SIZE
, "%ld\n", msc
->nr_pages
);
1362 else if (msc
->mode
== MSC_MODE_MULTI
) {
1363 list_for_each_entry(win
, &msc
->win_list
, entry
) {
1364 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1365 "%d%c", win
->nr_blocks
,
1366 msc_is_last_win(win
) ? '\n' : ',');
1369 count
= scnprintf(buf
, PAGE_SIZE
, "unsupported\n");
1372 mutex_unlock(&msc
->buf_mutex
);
1378 nr_pages_store(struct device
*dev
, struct device_attribute
*attr
,
1379 const char *buf
, size_t size
)
1381 struct msc
*msc
= dev_get_drvdata(dev
);
1382 unsigned long val
, *win
= NULL
, *rewin
;
1384 const char *p
= buf
;
1386 int ret
, nr_wins
= 0;
1388 if (!capable(CAP_SYS_RAWIO
))
1391 ret
= msc_buffer_free_unless_used(msc
);
1395 /* scan the comma-separated list of allocation sizes */
1396 end
= memchr(buf
, '\n', len
);
1401 end
= memchr(p
, ',', len
);
1402 s
= kstrndup(p
, end
? end
- p
: len
, GFP_KERNEL
);
1408 ret
= kstrtoul(s
, 10, &val
);
1414 if (nr_wins
&& msc
->mode
== MSC_MODE_SINGLE
) {
1420 rewin
= krealloc(win
, sizeof(*win
) * nr_wins
, GFP_KERNEL
);
1427 win
[nr_wins
- 1] = val
;
1436 mutex_lock(&msc
->buf_mutex
);
1437 ret
= msc_buffer_alloc(msc
, win
, nr_wins
);
1438 mutex_unlock(&msc
->buf_mutex
);
1443 return ret
? ret
: size
;
1446 static DEVICE_ATTR_RW(nr_pages
);
1448 static struct attribute
*msc_output_attrs
[] = {
1449 &dev_attr_wrap
.attr
,
1450 &dev_attr_mode
.attr
,
1451 &dev_attr_nr_pages
.attr
,
1455 static struct attribute_group msc_output_group
= {
1456 .attrs
= msc_output_attrs
,
1459 static int intel_th_msc_probe(struct intel_th_device
*thdev
)
1461 struct device
*dev
= &thdev
->dev
;
1462 struct resource
*res
;
1467 res
= intel_th_device_get_resource(thdev
, IORESOURCE_MEM
, 0);
1471 base
= devm_ioremap(dev
, res
->start
, resource_size(res
));
1475 msc
= devm_kzalloc(dev
, sizeof(*msc
), GFP_KERNEL
);
1479 msc
->index
= thdev
->id
;
1482 msc
->reg_base
= base
+ msc
->index
* 0x100;
1484 err
= intel_th_msc_init(msc
);
1488 dev_set_drvdata(dev
, msc
);
1493 static void intel_th_msc_remove(struct intel_th_device
*thdev
)
1495 struct msc
*msc
= dev_get_drvdata(&thdev
->dev
);
1498 intel_th_msc_deactivate(thdev
);
1501 * Buffers should not be used at this point except if the
1502 * output character device is still open and the parent
1503 * device gets detached from its bus, which is a FIXME.
1505 ret
= msc_buffer_free_unless_used(msc
);
1509 static struct intel_th_driver intel_th_msc_driver
= {
1510 .probe
= intel_th_msc_probe
,
1511 .remove
= intel_th_msc_remove
,
1512 .activate
= intel_th_msc_activate
,
1513 .deactivate
= intel_th_msc_deactivate
,
1514 .fops
= &intel_th_msc_fops
,
1515 .attr_group
= &msc_output_group
,
1518 .owner
= THIS_MODULE
,
1522 module_driver(intel_th_msc_driver
,
1523 intel_th_driver_register
,
1524 intel_th_driver_unregister
);
1526 MODULE_LICENSE("GPL v2");
1527 MODULE_DESCRIPTION("Intel(R) Trace Hub Memory Storage Unit driver");
1528 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");