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>
31 #include <asm/set_memory.h>
37 #define msc_dev(x) (&(x)->thdev->dev)
40 * struct msc_block - multiblock mode block descriptor
41 * @bdesc: pointer to hardware descriptor (beginning of the block)
42 * @addr: physical address of the block
45 struct msc_block_desc
*bdesc
;
50 * struct msc_window - multiblock mode window descriptor
51 * @entry: window list linkage (msc::win_list)
52 * @pgoff: page offset into the buffer that this window starts at
53 * @nr_blocks: number of blocks (pages) in this window
54 * @block: array of block descriptors
57 struct list_head entry
;
59 unsigned int nr_blocks
;
61 struct msc_block block
[0];
65 * struct msc_iter - iterator for msc buffer
66 * @entry: msc::iter_list linkage
67 * @msc: pointer to the MSC device
68 * @start_win: oldest window
69 * @win: current window
70 * @offset: current logical offset into the buffer
71 * @start_block: oldest block in the window
72 * @block: block number in the window
73 * @block_off: offset into current block
74 * @wrap_count: block wrapping handling
75 * @eof: end of buffer reached
78 struct list_head entry
;
80 struct msc_window
*start_win
;
81 struct msc_window
*win
;
85 unsigned int block_off
;
86 unsigned int wrap_count
;
91 * struct msc - MSC device representation
92 * @reg_base: register window base address
93 * @thdev: intel_th_device pointer
94 * @win_list: list of windows in multiblock mode
95 * @nr_pages: total number of pages allocated for this buffer
96 * @single_sz: amount of data in single mode
97 * @single_wrap: single mode wrap occurred
98 * @base: buffer's base pointer
99 * @base_addr: buffer's base address
100 * @user_count: number of users of the buffer
101 * @mmap_count: number of mappings
102 * @buf_mutex: mutex to serialize access to buffer-related bits
104 * @enabled: MSC is enabled
105 * @wrap: wrapping is enabled
106 * @mode: MSC operating mode
107 * @burst_len: write burst length
108 * @index: number of this MSC in the MSU
111 void __iomem
*reg_base
;
112 struct intel_th_device
*thdev
;
114 struct list_head win_list
;
115 unsigned long nr_pages
;
116 unsigned long single_sz
;
117 unsigned int single_wrap
: 1;
119 dma_addr_t base_addr
;
121 /* <0: no buffer, 0: no users, >0: active users */
125 struct mutex buf_mutex
;
127 struct list_head iter_list
;
130 unsigned int enabled
: 1,
133 unsigned int burst_len
;
137 static inline bool msc_block_is_empty(struct msc_block_desc
*bdesc
)
139 /* header hasn't been written */
140 if (!bdesc
->valid_dw
)
143 /* valid_dw includes the header */
144 if (!msc_data_sz(bdesc
))
151 * msc_oldest_window() - locate the window with oldest data
154 * This should only be used in multiblock mode. Caller should hold the
155 * msc::user_count reference.
157 * Return: the oldest window with valid data
159 static struct msc_window
*msc_oldest_window(struct msc
*msc
)
161 struct msc_window
*win
;
162 u32 reg
= ioread32(msc
->reg_base
+ REG_MSU_MSC0NWSA
);
163 unsigned long win_addr
= (unsigned long)reg
<< PAGE_SHIFT
;
164 unsigned int found
= 0;
166 if (list_empty(&msc
->win_list
))
170 * we might need a radix tree for this, depending on how
171 * many windows a typical user would allocate; ideally it's
172 * something like 2, in which case we're good
174 list_for_each_entry(win
, &msc
->win_list
, entry
) {
175 if (win
->block
[0].addr
== win_addr
)
178 /* skip the empty ones */
179 if (msc_block_is_empty(win
->block
[0].bdesc
))
186 return list_entry(msc
->win_list
.next
, struct msc_window
, entry
);
190 * msc_win_oldest_block() - locate the oldest block in a given window
191 * @win: window to look at
193 * Return: index of the block with the oldest data
195 static unsigned int msc_win_oldest_block(struct msc_window
*win
)
198 struct msc_block_desc
*bdesc
= win
->block
[0].bdesc
;
200 /* without wrapping, first block is the oldest */
201 if (!msc_block_wrapped(bdesc
))
205 * with wrapping, last written block contains both the newest and the
206 * oldest data for this window.
208 for (blk
= 0; blk
< win
->nr_blocks
; blk
++) {
209 bdesc
= win
->block
[blk
].bdesc
;
211 if (msc_block_last_written(bdesc
))
219 * msc_is_last_win() - check if a window is the last one for a given MSC
221 * Return: true if @win is the last window in MSC's multiblock buffer
223 static inline bool msc_is_last_win(struct msc_window
*win
)
225 return win
->entry
.next
== &win
->msc
->win_list
;
229 * msc_next_window() - return next window in the multiblock buffer
230 * @win: current window
232 * Return: window following the current one
234 static struct msc_window
*msc_next_window(struct msc_window
*win
)
236 if (msc_is_last_win(win
))
237 return list_entry(win
->msc
->win_list
.next
, struct msc_window
,
240 return list_entry(win
->entry
.next
, struct msc_window
, entry
);
243 static struct msc_block_desc
*msc_iter_bdesc(struct msc_iter
*iter
)
245 return iter
->win
->block
[iter
->block
].bdesc
;
248 static void msc_iter_init(struct msc_iter
*iter
)
250 memset(iter
, 0, sizeof(*iter
));
251 iter
->start_block
= -1;
255 static struct msc_iter
*msc_iter_install(struct msc
*msc
)
257 struct msc_iter
*iter
;
259 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
261 return ERR_PTR(-ENOMEM
);
263 mutex_lock(&msc
->buf_mutex
);
266 * Reading and tracing are mutually exclusive; if msc is
267 * enabled, open() will fail; otherwise existing readers
268 * will prevent enabling the msc and the rest of fops don't
269 * need to worry about it.
273 iter
= ERR_PTR(-EBUSY
);
280 list_add_tail(&iter
->entry
, &msc
->iter_list
);
282 mutex_unlock(&msc
->buf_mutex
);
287 static void msc_iter_remove(struct msc_iter
*iter
, struct msc
*msc
)
289 mutex_lock(&msc
->buf_mutex
);
290 list_del(&iter
->entry
);
291 mutex_unlock(&msc
->buf_mutex
);
296 static void msc_iter_block_start(struct msc_iter
*iter
)
298 if (iter
->start_block
!= -1)
301 iter
->start_block
= msc_win_oldest_block(iter
->win
);
302 iter
->block
= iter
->start_block
;
303 iter
->wrap_count
= 0;
306 * start with the block with oldest data; if data has wrapped
307 * in this window, it should be in this block
309 if (msc_block_wrapped(msc_iter_bdesc(iter
)))
310 iter
->wrap_count
= 2;
314 static int msc_iter_win_start(struct msc_iter
*iter
, struct msc
*msc
)
316 /* already started, nothing to do */
320 iter
->start_win
= msc_oldest_window(msc
);
321 if (!iter
->start_win
)
324 iter
->win
= iter
->start_win
;
325 iter
->start_block
= -1;
327 msc_iter_block_start(iter
);
332 static int msc_iter_win_advance(struct msc_iter
*iter
)
334 iter
->win
= msc_next_window(iter
->win
);
335 iter
->start_block
= -1;
337 if (iter
->win
== iter
->start_win
) {
342 msc_iter_block_start(iter
);
347 static int msc_iter_block_advance(struct msc_iter
*iter
)
352 if (iter
->wrap_count
&& iter
->block
== iter
->start_block
) {
354 if (!iter
->wrap_count
)
355 /* copied newest data from the wrapped block */
356 return msc_iter_win_advance(iter
);
359 /* no wrapping, check for last written block */
360 if (!iter
->wrap_count
&& msc_block_last_written(msc_iter_bdesc(iter
)))
361 /* copied newest data for the window */
362 return msc_iter_win_advance(iter
);
365 if (++iter
->block
== iter
->win
->nr_blocks
)
368 /* no wrapping, sanity check in case there is no last written block */
369 if (!iter
->wrap_count
&& iter
->block
== iter
->start_block
)
370 return msc_iter_win_advance(iter
);
376 * msc_buffer_iterate() - go through multiblock buffer's data
377 * @iter: iterator structure
378 * @size: amount of data to scan
379 * @data: callback's private data
380 * @fn: iterator callback
382 * This will start at the window which will be written to next (containing
383 * the oldest data) and work its way to the current window, calling @fn
384 * for each chunk of data as it goes.
386 * Caller should have msc::user_count reference to make sure the buffer
387 * doesn't disappear from under us.
389 * Return: amount of data actually scanned.
392 msc_buffer_iterate(struct msc_iter
*iter
, size_t size
, void *data
,
393 unsigned long (*fn
)(void *, void *, size_t))
395 struct msc
*msc
= iter
->msc
;
397 unsigned int advance
;
402 /* start with the oldest window */
403 if (msc_iter_win_start(iter
, msc
))
407 unsigned long data_bytes
= msc_data_sz(msc_iter_bdesc(iter
));
408 void *src
= (void *)msc_iter_bdesc(iter
) + MSC_BDESC
;
409 size_t tocopy
= data_bytes
, copied
= 0;
410 size_t remaining
= 0;
415 * If block wrapping happened, we need to visit the last block
416 * twice, because it contains both the oldest and the newest
417 * data in this window.
419 * First time (wrap_count==2), in the very beginning, to collect
420 * the oldest data, which is in the range
421 * (data_bytes..DATA_IN_PAGE).
423 * Second time (wrap_count==1), it's just like any other block,
424 * containing data in the range of [MSC_BDESC..data_bytes].
426 if (iter
->block
== iter
->start_block
&& iter
->wrap_count
== 2) {
427 tocopy
= DATA_IN_PAGE
- data_bytes
;
434 tocopy
-= iter
->block_off
;
435 src
+= iter
->block_off
;
442 remaining
= fn(data
, src
, tocopy
);
447 copied
= tocopy
- remaining
;
449 iter
->block_off
+= copied
;
450 iter
->offset
+= copied
;
456 if (msc_iter_block_advance(iter
))
465 * msc_buffer_clear_hw_header() - clear hw header for multiblock
468 static void msc_buffer_clear_hw_header(struct msc
*msc
)
470 struct msc_window
*win
;
472 list_for_each_entry(win
, &msc
->win_list
, entry
) {
474 size_t hw_sz
= sizeof(struct msc_block_desc
) -
475 offsetof(struct msc_block_desc
, hw_tag
);
477 for (blk
= 0; blk
< win
->nr_blocks
; blk
++) {
478 struct msc_block_desc
*bdesc
= win
->block
[blk
].bdesc
;
480 memset(&bdesc
->hw_tag
, 0, hw_sz
);
486 * msc_configure() - set up MSC hardware
487 * @msc: the MSC device to configure
489 * Program storage mode, wrapping, burst length and trace buffer address
490 * into a given MSC. Then, enable tracing and set msc::enabled.
491 * The latter is serialized on msc::buf_mutex, so make sure to hold it.
493 static int msc_configure(struct msc
*msc
)
497 lockdep_assert_held(&msc
->buf_mutex
);
499 if (msc
->mode
> MSC_MODE_MULTI
)
502 if (msc
->mode
== MSC_MODE_MULTI
)
503 msc_buffer_clear_hw_header(msc
);
505 reg
= msc
->base_addr
>> PAGE_SHIFT
;
506 iowrite32(reg
, msc
->reg_base
+ REG_MSU_MSC0BAR
);
508 if (msc
->mode
== MSC_MODE_SINGLE
) {
510 iowrite32(reg
, msc
->reg_base
+ REG_MSU_MSC0SIZE
);
513 reg
= ioread32(msc
->reg_base
+ REG_MSU_MSC0CTL
);
514 reg
&= ~(MSC_MODE
| MSC_WRAPEN
| MSC_EN
| MSC_RD_HDR_OVRD
);
517 reg
|= msc
->mode
<< __ffs(MSC_MODE
);
518 reg
|= msc
->burst_len
<< __ffs(MSC_LEN
);
523 iowrite32(reg
, msc
->reg_base
+ REG_MSU_MSC0CTL
);
525 msc
->thdev
->output
.multiblock
= msc
->mode
== MSC_MODE_MULTI
;
526 intel_th_trace_enable(msc
->thdev
);
534 * msc_disable() - disable MSC hardware
535 * @msc: MSC device to disable
537 * If @msc is enabled, disable tracing on the switch and then disable MSC
538 * storage. Caller must hold msc::buf_mutex.
540 static void msc_disable(struct msc
*msc
)
545 lockdep_assert_held(&msc
->buf_mutex
);
547 intel_th_trace_disable(msc
->thdev
);
549 for (reg
= 0, count
= MSC_PLE_WAITLOOP_DEPTH
;
550 count
&& !(reg
& MSCSTS_PLE
); count
--) {
551 reg
= ioread32(msc
->reg_base
+ REG_MSU_MSC0STS
);
556 dev_dbg(msc_dev(msc
), "timeout waiting for MSC0 PLE\n");
558 if (msc
->mode
== MSC_MODE_SINGLE
) {
559 msc
->single_wrap
= !!(reg
& MSCSTS_WRAPSTAT
);
561 reg
= ioread32(msc
->reg_base
+ REG_MSU_MSC0MWP
);
562 msc
->single_sz
= reg
& ((msc
->nr_pages
<< PAGE_SHIFT
) - 1);
563 dev_dbg(msc_dev(msc
), "MSCnMWP: %08x/%08lx, wrap: %d\n",
564 reg
, msc
->single_sz
, msc
->single_wrap
);
567 reg
= ioread32(msc
->reg_base
+ REG_MSU_MSC0CTL
);
569 iowrite32(reg
, msc
->reg_base
+ REG_MSU_MSC0CTL
);
572 iowrite32(0, msc
->reg_base
+ REG_MSU_MSC0BAR
);
573 iowrite32(0, msc
->reg_base
+ REG_MSU_MSC0SIZE
);
575 dev_dbg(msc_dev(msc
), "MSCnNWSA: %08x\n",
576 ioread32(msc
->reg_base
+ REG_MSU_MSC0NWSA
));
578 reg
= ioread32(msc
->reg_base
+ REG_MSU_MSC0STS
);
579 dev_dbg(msc_dev(msc
), "MSCnSTS: %08x\n", reg
);
582 static int intel_th_msc_activate(struct intel_th_device
*thdev
)
584 struct msc
*msc
= dev_get_drvdata(&thdev
->dev
);
587 if (!atomic_inc_unless_negative(&msc
->user_count
))
590 mutex_lock(&msc
->buf_mutex
);
592 /* if there are readers, refuse */
593 if (list_empty(&msc
->iter_list
))
594 ret
= msc_configure(msc
);
596 mutex_unlock(&msc
->buf_mutex
);
599 atomic_dec(&msc
->user_count
);
604 static void intel_th_msc_deactivate(struct intel_th_device
*thdev
)
606 struct msc
*msc
= dev_get_drvdata(&thdev
->dev
);
608 mutex_lock(&msc
->buf_mutex
);
611 atomic_dec(&msc
->user_count
);
613 mutex_unlock(&msc
->buf_mutex
);
617 * msc_buffer_contig_alloc() - allocate a contiguous buffer for SINGLE mode
619 * @size: allocation size in bytes
621 * This modifies msc::base, which requires msc::buf_mutex to serialize, so the
622 * caller is expected to hold it.
624 * Return: 0 on success, -errno otherwise.
626 static int msc_buffer_contig_alloc(struct msc
*msc
, unsigned long size
)
628 unsigned int order
= get_order(size
);
634 page
= alloc_pages(GFP_KERNEL
| __GFP_ZERO
, order
);
638 split_page(page
, order
);
639 msc
->nr_pages
= size
>> PAGE_SHIFT
;
640 msc
->base
= page_address(page
);
641 msc
->base_addr
= page_to_phys(page
);
647 * msc_buffer_contig_free() - free a contiguous buffer
648 * @msc: MSC configured in SINGLE mode
650 static void msc_buffer_contig_free(struct msc
*msc
)
654 for (off
= 0; off
< msc
->nr_pages
<< PAGE_SHIFT
; off
+= PAGE_SIZE
) {
655 struct page
*page
= virt_to_page(msc
->base
+ off
);
657 page
->mapping
= NULL
;
665 * msc_buffer_contig_get_page() - find a page at a given offset
666 * @msc: MSC configured in SINGLE mode
667 * @pgoff: page offset
669 * Return: page, if @pgoff is within the range, NULL otherwise.
671 static struct page
*msc_buffer_contig_get_page(struct msc
*msc
,
674 if (pgoff
>= msc
->nr_pages
)
677 return virt_to_page(msc
->base
+ (pgoff
<< PAGE_SHIFT
));
681 * msc_buffer_win_alloc() - alloc a window for a multiblock mode
683 * @nr_blocks: number of pages in this window
685 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
686 * to serialize, so the caller is expected to hold it.
688 * Return: 0 on success, -errno otherwise.
690 static int msc_buffer_win_alloc(struct msc
*msc
, unsigned int nr_blocks
)
692 struct msc_window
*win
;
693 unsigned long size
= PAGE_SIZE
;
694 int i
, ret
= -ENOMEM
;
699 win
= kzalloc(offsetof(struct msc_window
, block
[nr_blocks
]),
704 if (!list_empty(&msc
->win_list
)) {
705 struct msc_window
*prev
= list_entry(msc
->win_list
.prev
,
706 struct msc_window
, entry
);
708 win
->pgoff
= prev
->pgoff
+ prev
->nr_blocks
;
711 for (i
= 0; i
< nr_blocks
; i
++) {
712 win
->block
[i
].bdesc
=
713 dma_alloc_coherent(msc_dev(msc
)->parent
->parent
, size
,
714 &win
->block
[i
].addr
, GFP_KERNEL
);
716 if (!win
->block
[i
].bdesc
)
720 /* Set the page as uncached */
721 set_memory_uc((unsigned long)win
->block
[i
].bdesc
, 1);
726 win
->nr_blocks
= nr_blocks
;
728 if (list_empty(&msc
->win_list
)) {
729 msc
->base
= win
->block
[0].bdesc
;
730 msc
->base_addr
= win
->block
[0].addr
;
733 list_add_tail(&win
->entry
, &msc
->win_list
);
734 msc
->nr_pages
+= nr_blocks
;
739 for (i
--; i
>= 0; i
--) {
741 /* Reset the page to write-back before releasing */
742 set_memory_wb((unsigned long)win
->block
[i
].bdesc
, 1);
744 dma_free_coherent(msc_dev(msc
), size
, win
->block
[i
].bdesc
,
753 * msc_buffer_win_free() - free a window from MSC's window list
755 * @win: window to free
757 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
758 * to serialize, so the caller is expected to hold it.
760 static void msc_buffer_win_free(struct msc
*msc
, struct msc_window
*win
)
764 msc
->nr_pages
-= win
->nr_blocks
;
766 list_del(&win
->entry
);
767 if (list_empty(&msc
->win_list
)) {
772 for (i
= 0; i
< win
->nr_blocks
; i
++) {
773 struct page
*page
= virt_to_page(win
->block
[i
].bdesc
);
775 page
->mapping
= NULL
;
777 /* Reset the page to write-back before releasing */
778 set_memory_wb((unsigned long)win
->block
[i
].bdesc
, 1);
780 dma_free_coherent(msc_dev(win
->msc
), PAGE_SIZE
,
781 win
->block
[i
].bdesc
, win
->block
[i
].addr
);
788 * msc_buffer_relink() - set up block descriptors for multiblock mode
791 * This traverses msc::win_list, which requires msc::buf_mutex to serialize,
792 * so the caller is expected to hold it.
794 static void msc_buffer_relink(struct msc
*msc
)
796 struct msc_window
*win
, *next_win
;
798 /* call with msc::mutex locked */
799 list_for_each_entry(win
, &msc
->win_list
, entry
) {
804 * Last window's next_win should point to the first window
805 * and MSC_SW_TAG_LASTWIN should be set.
807 if (msc_is_last_win(win
)) {
808 sw_tag
|= MSC_SW_TAG_LASTWIN
;
809 next_win
= list_entry(msc
->win_list
.next
,
810 struct msc_window
, entry
);
812 next_win
= list_entry(win
->entry
.next
,
813 struct msc_window
, entry
);
816 for (blk
= 0; blk
< win
->nr_blocks
; blk
++) {
817 struct msc_block_desc
*bdesc
= win
->block
[blk
].bdesc
;
819 memset(bdesc
, 0, sizeof(*bdesc
));
821 bdesc
->next_win
= next_win
->block
[0].addr
>> PAGE_SHIFT
;
824 * Similarly to last window, last block should point
827 if (blk
== win
->nr_blocks
- 1) {
828 sw_tag
|= MSC_SW_TAG_LASTBLK
;
830 win
->block
[0].addr
>> PAGE_SHIFT
;
833 win
->block
[blk
+ 1].addr
>> PAGE_SHIFT
;
836 bdesc
->sw_tag
= sw_tag
;
837 bdesc
->block_sz
= PAGE_SIZE
/ 64;
842 * Make the above writes globally visible before tracing is
843 * enabled to make sure hardware sees them coherently.
848 static void msc_buffer_multi_free(struct msc
*msc
)
850 struct msc_window
*win
, *iter
;
852 list_for_each_entry_safe(win
, iter
, &msc
->win_list
, entry
)
853 msc_buffer_win_free(msc
, win
);
856 static int msc_buffer_multi_alloc(struct msc
*msc
, unsigned long *nr_pages
,
857 unsigned int nr_wins
)
861 for (i
= 0; i
< nr_wins
; i
++) {
862 ret
= msc_buffer_win_alloc(msc
, nr_pages
[i
]);
864 msc_buffer_multi_free(msc
);
869 msc_buffer_relink(msc
);
875 * msc_buffer_free() - free buffers for MSC
878 * Free MSC's storage buffers.
880 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex to
881 * serialize, so the caller is expected to hold it.
883 static void msc_buffer_free(struct msc
*msc
)
885 if (msc
->mode
== MSC_MODE_SINGLE
)
886 msc_buffer_contig_free(msc
);
887 else if (msc
->mode
== MSC_MODE_MULTI
)
888 msc_buffer_multi_free(msc
);
892 * msc_buffer_alloc() - allocate a buffer for MSC
894 * @size: allocation size in bytes
896 * Allocate a storage buffer for MSC, depending on the msc::mode, it will be
897 * either done via msc_buffer_contig_alloc() for SINGLE operation mode or
898 * msc_buffer_win_alloc() for multiblock operation. The latter allocates one
899 * window per invocation, so in multiblock mode this can be called multiple
900 * times for the same MSC to allocate multiple windows.
902 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
903 * to serialize, so the caller is expected to hold it.
905 * Return: 0 on success, -errno otherwise.
907 static int msc_buffer_alloc(struct msc
*msc
, unsigned long *nr_pages
,
908 unsigned int nr_wins
)
912 /* -1: buffer not allocated */
913 if (atomic_read(&msc
->user_count
) != -1)
916 if (msc
->mode
== MSC_MODE_SINGLE
) {
920 ret
= msc_buffer_contig_alloc(msc
, nr_pages
[0] << PAGE_SHIFT
);
921 } else if (msc
->mode
== MSC_MODE_MULTI
) {
922 ret
= msc_buffer_multi_alloc(msc
, nr_pages
, nr_wins
);
928 /* allocation should be visible before the counter goes to 0 */
929 smp_mb__before_atomic();
931 if (WARN_ON_ONCE(atomic_cmpxchg(&msc
->user_count
, -1, 0) != -1))
939 * msc_buffer_unlocked_free_unless_used() - free a buffer unless it's in use
942 * This will free MSC buffer unless it is in use or there is no allocated
944 * Caller needs to hold msc::buf_mutex.
946 * Return: 0 on successful deallocation or if there was no buffer to
947 * deallocate, -EBUSY if there are active users.
949 static int msc_buffer_unlocked_free_unless_used(struct msc
*msc
)
953 count
= atomic_cmpxchg(&msc
->user_count
, 0, -1);
955 /* > 0: buffer is allocated and has users */
958 /* 0: buffer is allocated, no users */
960 msc_buffer_free(msc
);
961 /* < 0: no buffer, nothing to do */
967 * msc_buffer_free_unless_used() - free a buffer unless it's in use
970 * This is a locked version of msc_buffer_unlocked_free_unless_used().
972 static int msc_buffer_free_unless_used(struct msc
*msc
)
976 mutex_lock(&msc
->buf_mutex
);
977 ret
= msc_buffer_unlocked_free_unless_used(msc
);
978 mutex_unlock(&msc
->buf_mutex
);
984 * msc_buffer_get_page() - get MSC buffer page at a given offset
986 * @pgoff: page offset into the storage buffer
988 * This traverses msc::win_list, so holding msc::buf_mutex is expected from
991 * Return: page if @pgoff corresponds to a valid buffer page or NULL.
993 static struct page
*msc_buffer_get_page(struct msc
*msc
, unsigned long pgoff
)
995 struct msc_window
*win
;
997 if (msc
->mode
== MSC_MODE_SINGLE
)
998 return msc_buffer_contig_get_page(msc
, pgoff
);
1000 list_for_each_entry(win
, &msc
->win_list
, entry
)
1001 if (pgoff
>= win
->pgoff
&& pgoff
< win
->pgoff
+ win
->nr_blocks
)
1007 pgoff
-= win
->pgoff
;
1008 return virt_to_page(win
->block
[pgoff
].bdesc
);
1012 * struct msc_win_to_user_struct - data for copy_to_user() callback
1013 * @buf: userspace buffer to copy data to
1014 * @offset: running offset
1016 struct msc_win_to_user_struct
{
1018 unsigned long offset
;
1022 * msc_win_to_user() - iterator for msc_buffer_iterate() to copy data to user
1023 * @data: callback's private data
1024 * @src: source buffer
1025 * @len: amount of data to copy from the source buffer
1027 static unsigned long msc_win_to_user(void *data
, void *src
, size_t len
)
1029 struct msc_win_to_user_struct
*u
= data
;
1032 ret
= copy_to_user(u
->buf
+ u
->offset
, src
, len
);
1033 u
->offset
+= len
- ret
;
1040 * file operations' callbacks
1043 static int intel_th_msc_open(struct inode
*inode
, struct file
*file
)
1045 struct intel_th_device
*thdev
= file
->private_data
;
1046 struct msc
*msc
= dev_get_drvdata(&thdev
->dev
);
1047 struct msc_iter
*iter
;
1049 if (!capable(CAP_SYS_RAWIO
))
1052 iter
= msc_iter_install(msc
);
1054 return PTR_ERR(iter
);
1056 file
->private_data
= iter
;
1058 return nonseekable_open(inode
, file
);
1061 static int intel_th_msc_release(struct inode
*inode
, struct file
*file
)
1063 struct msc_iter
*iter
= file
->private_data
;
1064 struct msc
*msc
= iter
->msc
;
1066 msc_iter_remove(iter
, msc
);
1072 msc_single_to_user(struct msc
*msc
, char __user
*buf
, loff_t off
, size_t len
)
1074 unsigned long size
= msc
->nr_pages
<< PAGE_SHIFT
, rem
= len
;
1075 unsigned long start
= off
, tocopy
= 0;
1077 if (msc
->single_wrap
) {
1078 start
+= msc
->single_sz
;
1080 tocopy
= min(rem
, size
- start
);
1081 if (copy_to_user(buf
, msc
->base
+ start
, tocopy
))
1091 tocopy
= min(rem
, msc
->single_sz
- start
);
1092 if (copy_to_user(buf
, msc
->base
+ start
, tocopy
))
1101 if (copy_to_user(buf
, msc
->base
+ start
, rem
))
1107 static ssize_t
intel_th_msc_read(struct file
*file
, char __user
*buf
,
1108 size_t len
, loff_t
*ppos
)
1110 struct msc_iter
*iter
= file
->private_data
;
1111 struct msc
*msc
= iter
->msc
;
1116 if (!atomic_inc_unless_negative(&msc
->user_count
))
1119 if (msc
->mode
== MSC_MODE_SINGLE
&& !msc
->single_wrap
)
1120 size
= msc
->single_sz
;
1122 size
= msc
->nr_pages
<< PAGE_SHIFT
;
1130 if (off
+ len
>= size
)
1133 if (msc
->mode
== MSC_MODE_SINGLE
) {
1134 ret
= msc_single_to_user(msc
, buf
, off
, len
);
1137 } else if (msc
->mode
== MSC_MODE_MULTI
) {
1138 struct msc_win_to_user_struct u
= {
1143 ret
= msc_buffer_iterate(iter
, len
, &u
, msc_win_to_user
);
1145 *ppos
= iter
->offset
;
1151 atomic_dec(&msc
->user_count
);
1157 * vm operations callbacks (vm_ops)
1160 static void msc_mmap_open(struct vm_area_struct
*vma
)
1162 struct msc_iter
*iter
= vma
->vm_file
->private_data
;
1163 struct msc
*msc
= iter
->msc
;
1165 atomic_inc(&msc
->mmap_count
);
1168 static void msc_mmap_close(struct vm_area_struct
*vma
)
1170 struct msc_iter
*iter
= vma
->vm_file
->private_data
;
1171 struct msc
*msc
= iter
->msc
;
1174 if (!atomic_dec_and_mutex_lock(&msc
->mmap_count
, &msc
->buf_mutex
))
1177 /* drop page _refcounts */
1178 for (pg
= 0; pg
< msc
->nr_pages
; pg
++) {
1179 struct page
*page
= msc_buffer_get_page(msc
, pg
);
1181 if (WARN_ON_ONCE(!page
))
1185 page
->mapping
= NULL
;
1188 /* last mapping -- drop user_count */
1189 atomic_dec(&msc
->user_count
);
1190 mutex_unlock(&msc
->buf_mutex
);
1193 static int msc_mmap_fault(struct vm_fault
*vmf
)
1195 struct msc_iter
*iter
= vmf
->vma
->vm_file
->private_data
;
1196 struct msc
*msc
= iter
->msc
;
1198 vmf
->page
= msc_buffer_get_page(msc
, vmf
->pgoff
);
1200 return VM_FAULT_SIGBUS
;
1202 get_page(vmf
->page
);
1203 vmf
->page
->mapping
= vmf
->vma
->vm_file
->f_mapping
;
1204 vmf
->page
->index
= vmf
->pgoff
;
1209 static const struct vm_operations_struct msc_mmap_ops
= {
1210 .open
= msc_mmap_open
,
1211 .close
= msc_mmap_close
,
1212 .fault
= msc_mmap_fault
,
1215 static int intel_th_msc_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1217 unsigned long size
= vma
->vm_end
- vma
->vm_start
;
1218 struct msc_iter
*iter
= vma
->vm_file
->private_data
;
1219 struct msc
*msc
= iter
->msc
;
1222 if (!size
|| offset_in_page(size
))
1228 /* grab user_count once per mmap; drop in msc_mmap_close() */
1229 if (!atomic_inc_unless_negative(&msc
->user_count
))
1232 if (msc
->mode
!= MSC_MODE_SINGLE
&&
1233 msc
->mode
!= MSC_MODE_MULTI
)
1236 if (size
>> PAGE_SHIFT
!= msc
->nr_pages
)
1239 atomic_set(&msc
->mmap_count
, 1);
1244 atomic_dec(&msc
->user_count
);
1246 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
1247 vma
->vm_flags
|= VM_DONTEXPAND
| VM_DONTCOPY
;
1248 vma
->vm_ops
= &msc_mmap_ops
;
1252 static const struct file_operations intel_th_msc_fops
= {
1253 .open
= intel_th_msc_open
,
1254 .release
= intel_th_msc_release
,
1255 .read
= intel_th_msc_read
,
1256 .mmap
= intel_th_msc_mmap
,
1257 .llseek
= no_llseek
,
1258 .owner
= THIS_MODULE
,
1261 static int intel_th_msc_init(struct msc
*msc
)
1263 atomic_set(&msc
->user_count
, -1);
1265 msc
->mode
= MSC_MODE_MULTI
;
1266 mutex_init(&msc
->buf_mutex
);
1267 INIT_LIST_HEAD(&msc
->win_list
);
1268 INIT_LIST_HEAD(&msc
->iter_list
);
1271 (ioread32(msc
->reg_base
+ REG_MSU_MSC0CTL
) & MSC_LEN
) >>
1277 static const char * const msc_mode
[] = {
1278 [MSC_MODE_SINGLE
] = "single",
1279 [MSC_MODE_MULTI
] = "multi",
1280 [MSC_MODE_EXI
] = "ExI",
1281 [MSC_MODE_DEBUG
] = "debug",
1285 wrap_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1287 struct msc
*msc
= dev_get_drvdata(dev
);
1289 return scnprintf(buf
, PAGE_SIZE
, "%d\n", msc
->wrap
);
1293 wrap_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
,
1296 struct msc
*msc
= dev_get_drvdata(dev
);
1300 ret
= kstrtoul(buf
, 10, &val
);
1309 static DEVICE_ATTR_RW(wrap
);
1312 mode_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1314 struct msc
*msc
= dev_get_drvdata(dev
);
1316 return scnprintf(buf
, PAGE_SIZE
, "%s\n", msc_mode
[msc
->mode
]);
1320 mode_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
,
1323 struct msc
*msc
= dev_get_drvdata(dev
);
1328 if (!capable(CAP_SYS_RAWIO
))
1331 cp
= memchr(buf
, '\n', len
);
1335 for (i
= 0; i
< ARRAY_SIZE(msc_mode
); i
++)
1336 if (!strncmp(msc_mode
[i
], buf
, len
))
1342 mutex_lock(&msc
->buf_mutex
);
1343 ret
= msc_buffer_unlocked_free_unless_used(msc
);
1346 mutex_unlock(&msc
->buf_mutex
);
1348 return ret
? ret
: size
;
1351 static DEVICE_ATTR_RW(mode
);
1354 nr_pages_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
1356 struct msc
*msc
= dev_get_drvdata(dev
);
1357 struct msc_window
*win
;
1360 mutex_lock(&msc
->buf_mutex
);
1362 if (msc
->mode
== MSC_MODE_SINGLE
)
1363 count
= scnprintf(buf
, PAGE_SIZE
, "%ld\n", msc
->nr_pages
);
1364 else if (msc
->mode
== MSC_MODE_MULTI
) {
1365 list_for_each_entry(win
, &msc
->win_list
, entry
) {
1366 count
+= scnprintf(buf
+ count
, PAGE_SIZE
- count
,
1367 "%d%c", win
->nr_blocks
,
1368 msc_is_last_win(win
) ? '\n' : ',');
1371 count
= scnprintf(buf
, PAGE_SIZE
, "unsupported\n");
1374 mutex_unlock(&msc
->buf_mutex
);
1380 nr_pages_store(struct device
*dev
, struct device_attribute
*attr
,
1381 const char *buf
, size_t size
)
1383 struct msc
*msc
= dev_get_drvdata(dev
);
1384 unsigned long val
, *win
= NULL
, *rewin
;
1386 const char *p
= buf
;
1388 int ret
, nr_wins
= 0;
1390 if (!capable(CAP_SYS_RAWIO
))
1393 ret
= msc_buffer_free_unless_used(msc
);
1397 /* scan the comma-separated list of allocation sizes */
1398 end
= memchr(buf
, '\n', len
);
1403 end
= memchr(p
, ',', len
);
1404 s
= kstrndup(p
, end
? end
- p
: len
, GFP_KERNEL
);
1410 ret
= kstrtoul(s
, 10, &val
);
1416 if (nr_wins
&& msc
->mode
== MSC_MODE_SINGLE
) {
1422 rewin
= krealloc(win
, sizeof(*win
) * nr_wins
, GFP_KERNEL
);
1429 win
[nr_wins
- 1] = val
;
1438 mutex_lock(&msc
->buf_mutex
);
1439 ret
= msc_buffer_alloc(msc
, win
, nr_wins
);
1440 mutex_unlock(&msc
->buf_mutex
);
1445 return ret
? ret
: size
;
1448 static DEVICE_ATTR_RW(nr_pages
);
1450 static struct attribute
*msc_output_attrs
[] = {
1451 &dev_attr_wrap
.attr
,
1452 &dev_attr_mode
.attr
,
1453 &dev_attr_nr_pages
.attr
,
1457 static struct attribute_group msc_output_group
= {
1458 .attrs
= msc_output_attrs
,
1461 static int intel_th_msc_probe(struct intel_th_device
*thdev
)
1463 struct device
*dev
= &thdev
->dev
;
1464 struct resource
*res
;
1469 res
= intel_th_device_get_resource(thdev
, IORESOURCE_MEM
, 0);
1473 base
= devm_ioremap(dev
, res
->start
, resource_size(res
));
1477 msc
= devm_kzalloc(dev
, sizeof(*msc
), GFP_KERNEL
);
1481 msc
->index
= thdev
->id
;
1484 msc
->reg_base
= base
+ msc
->index
* 0x100;
1486 err
= intel_th_msc_init(msc
);
1490 dev_set_drvdata(dev
, msc
);
1495 static void intel_th_msc_remove(struct intel_th_device
*thdev
)
1497 struct msc
*msc
= dev_get_drvdata(&thdev
->dev
);
1500 intel_th_msc_deactivate(thdev
);
1503 * Buffers should not be used at this point except if the
1504 * output character device is still open and the parent
1505 * device gets detached from its bus, which is a FIXME.
1507 ret
= msc_buffer_free_unless_used(msc
);
1511 static struct intel_th_driver intel_th_msc_driver
= {
1512 .probe
= intel_th_msc_probe
,
1513 .remove
= intel_th_msc_remove
,
1514 .activate
= intel_th_msc_activate
,
1515 .deactivate
= intel_th_msc_deactivate
,
1516 .fops
= &intel_th_msc_fops
,
1517 .attr_group
= &msc_output_group
,
1520 .owner
= THIS_MODULE
,
1524 module_driver(intel_th_msc_driver
,
1525 intel_th_driver_register
,
1526 intel_th_driver_unregister
);
1528 MODULE_LICENSE("GPL v2");
1529 MODULE_DESCRIPTION("Intel(R) Trace Hub Memory Storage Unit driver");
1530 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");