1 // SPDX-License-Identifier: GPL-2.0
3 * Intel(R) Trace Hub Memory Storage Unit
5 * Copyright (C) 2014-2015 Intel Corporation.
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/types.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/uaccess.h>
14 #include <linux/sizes.h>
15 #include <linux/printk.h>
16 #include <linux/slab.h>
20 #include <linux/dma-mapping.h>
23 #include <asm/set_memory.h>
29 #define msc_dev(x) (&(x)->thdev->dev)
32 * struct msc_block - multiblock mode block descriptor
33 * @bdesc: pointer to hardware descriptor (beginning of the block)
34 * @addr: physical address of the block
37 struct msc_block_desc *bdesc;
42 * struct msc_window - multiblock mode window descriptor
43 * @entry: window list linkage (msc::win_list)
44 * @pgoff: page offset into the buffer that this window starts at
45 * @nr_blocks: number of blocks (pages) in this window
46 * @block: array of block descriptors
49 struct list_head entry;
51 unsigned int nr_blocks;
53 struct msc_block block[0];
57 * struct msc_iter - iterator for msc buffer
58 * @entry: msc::iter_list linkage
59 * @msc: pointer to the MSC device
60 * @start_win: oldest window
61 * @win: current window
62 * @offset: current logical offset into the buffer
63 * @start_block: oldest block in the window
64 * @block: block number in the window
65 * @block_off: offset into current block
66 * @wrap_count: block wrapping handling
67 * @eof: end of buffer reached
70 struct list_head entry;
72 struct msc_window *start_win;
73 struct msc_window *win;
77 unsigned int block_off;
78 unsigned int wrap_count;
83 * struct msc - MSC device representation
84 * @reg_base: register window base address
85 * @thdev: intel_th_device pointer
86 * @win_list: list of windows in multiblock mode
87 * @single_sgt: single mode buffer
88 * @nr_pages: total number of pages allocated for this buffer
89 * @single_sz: amount of data in single mode
90 * @single_wrap: single mode wrap occurred
91 * @base: buffer's base pointer
92 * @base_addr: buffer's base address
93 * @user_count: number of users of the buffer
94 * @mmap_count: number of mappings
95 * @buf_mutex: mutex to serialize access to buffer-related bits
97 * @enabled: MSC is enabled
98 * @wrap: wrapping is enabled
99 * @mode: MSC operating mode
100 * @burst_len: write burst length
101 * @index: number of this MSC in the MSU
104 void __iomem *reg_base;
105 struct intel_th_device *thdev;
107 struct list_head win_list;
108 struct sg_table single_sgt;
109 unsigned long nr_pages;
110 unsigned long single_sz;
111 unsigned int single_wrap : 1;
113 dma_addr_t base_addr;
115 /* <0: no buffer, 0: no users, >0: active users */
119 struct mutex buf_mutex;
121 struct list_head iter_list;
124 unsigned int enabled : 1,
127 unsigned int burst_len;
131 static inline bool msc_block_is_empty(struct msc_block_desc *bdesc)
133 /* header hasn't been written */
134 if (!bdesc->valid_dw)
137 /* valid_dw includes the header */
138 if (!msc_data_sz(bdesc))
145 * msc_oldest_window() - locate the window with oldest data
148 * This should only be used in multiblock mode. Caller should hold the
149 * msc::user_count reference.
151 * Return: the oldest window with valid data
153 static struct msc_window *msc_oldest_window(struct msc *msc)
155 struct msc_window *win;
156 u32 reg = ioread32(msc->reg_base + REG_MSU_MSC0NWSA);
157 unsigned long win_addr = (unsigned long)reg << PAGE_SHIFT;
158 unsigned int found = 0;
160 if (list_empty(&msc->win_list))
164 * we might need a radix tree for this, depending on how
165 * many windows a typical user would allocate; ideally it's
166 * something like 2, in which case we're good
168 list_for_each_entry(win, &msc->win_list, entry) {
169 if (win->block[0].addr == win_addr)
172 /* skip the empty ones */
173 if (msc_block_is_empty(win->block[0].bdesc))
180 return list_entry(msc->win_list.next, struct msc_window, entry);
184 * msc_win_oldest_block() - locate the oldest block in a given window
185 * @win: window to look at
187 * Return: index of the block with the oldest data
189 static unsigned int msc_win_oldest_block(struct msc_window *win)
192 struct msc_block_desc *bdesc = win->block[0].bdesc;
194 /* without wrapping, first block is the oldest */
195 if (!msc_block_wrapped(bdesc))
199 * with wrapping, last written block contains both the newest and the
200 * oldest data for this window.
202 for (blk = 0; blk < win->nr_blocks; blk++) {
203 bdesc = win->block[blk].bdesc;
205 if (msc_block_last_written(bdesc))
213 * msc_is_last_win() - check if a window is the last one for a given MSC
215 * Return: true if @win is the last window in MSC's multiblock buffer
217 static inline bool msc_is_last_win(struct msc_window *win)
219 return win->entry.next == &win->msc->win_list;
223 * msc_next_window() - return next window in the multiblock buffer
224 * @win: current window
226 * Return: window following the current one
228 static struct msc_window *msc_next_window(struct msc_window *win)
230 if (msc_is_last_win(win))
231 return list_entry(win->msc->win_list.next, struct msc_window,
234 return list_entry(win->entry.next, struct msc_window, entry);
237 static struct msc_block_desc *msc_iter_bdesc(struct msc_iter *iter)
239 return iter->win->block[iter->block].bdesc;
242 static void msc_iter_init(struct msc_iter *iter)
244 memset(iter, 0, sizeof(*iter));
245 iter->start_block = -1;
249 static struct msc_iter *msc_iter_install(struct msc *msc)
251 struct msc_iter *iter;
253 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
255 return ERR_PTR(-ENOMEM);
257 mutex_lock(&msc->buf_mutex);
260 * Reading and tracing are mutually exclusive; if msc is
261 * enabled, open() will fail; otherwise existing readers
262 * will prevent enabling the msc and the rest of fops don't
263 * need to worry about it.
267 iter = ERR_PTR(-EBUSY);
274 list_add_tail(&iter->entry, &msc->iter_list);
276 mutex_unlock(&msc->buf_mutex);
281 static void msc_iter_remove(struct msc_iter *iter, struct msc *msc)
283 mutex_lock(&msc->buf_mutex);
284 list_del(&iter->entry);
285 mutex_unlock(&msc->buf_mutex);
290 static void msc_iter_block_start(struct msc_iter *iter)
292 if (iter->start_block != -1)
295 iter->start_block = msc_win_oldest_block(iter->win);
296 iter->block = iter->start_block;
297 iter->wrap_count = 0;
300 * start with the block with oldest data; if data has wrapped
301 * in this window, it should be in this block
303 if (msc_block_wrapped(msc_iter_bdesc(iter)))
304 iter->wrap_count = 2;
308 static int msc_iter_win_start(struct msc_iter *iter, struct msc *msc)
310 /* already started, nothing to do */
314 iter->start_win = msc_oldest_window(msc);
315 if (!iter->start_win)
318 iter->win = iter->start_win;
319 iter->start_block = -1;
321 msc_iter_block_start(iter);
326 static int msc_iter_win_advance(struct msc_iter *iter)
328 iter->win = msc_next_window(iter->win);
329 iter->start_block = -1;
331 if (iter->win == iter->start_win) {
336 msc_iter_block_start(iter);
341 static int msc_iter_block_advance(struct msc_iter *iter)
346 if (iter->wrap_count && iter->block == iter->start_block) {
348 if (!iter->wrap_count)
349 /* copied newest data from the wrapped block */
350 return msc_iter_win_advance(iter);
353 /* no wrapping, check for last written block */
354 if (!iter->wrap_count && msc_block_last_written(msc_iter_bdesc(iter)))
355 /* copied newest data for the window */
356 return msc_iter_win_advance(iter);
359 if (++iter->block == iter->win->nr_blocks)
362 /* no wrapping, sanity check in case there is no last written block */
363 if (!iter->wrap_count && iter->block == iter->start_block)
364 return msc_iter_win_advance(iter);
370 * msc_buffer_iterate() - go through multiblock buffer's data
371 * @iter: iterator structure
372 * @size: amount of data to scan
373 * @data: callback's private data
374 * @fn: iterator callback
376 * This will start at the window which will be written to next (containing
377 * the oldest data) and work its way to the current window, calling @fn
378 * for each chunk of data as it goes.
380 * Caller should have msc::user_count reference to make sure the buffer
381 * doesn't disappear from under us.
383 * Return: amount of data actually scanned.
386 msc_buffer_iterate(struct msc_iter *iter, size_t size, void *data,
387 unsigned long (*fn)(void *, void *, size_t))
389 struct msc *msc = iter->msc;
391 unsigned int advance;
396 /* start with the oldest window */
397 if (msc_iter_win_start(iter, msc))
401 unsigned long data_bytes = msc_data_sz(msc_iter_bdesc(iter));
402 void *src = (void *)msc_iter_bdesc(iter) + MSC_BDESC;
403 size_t tocopy = data_bytes, copied = 0;
404 size_t remaining = 0;
409 * If block wrapping happened, we need to visit the last block
410 * twice, because it contains both the oldest and the newest
411 * data in this window.
413 * First time (wrap_count==2), in the very beginning, to collect
414 * the oldest data, which is in the range
415 * (data_bytes..DATA_IN_PAGE).
417 * Second time (wrap_count==1), it's just like any other block,
418 * containing data in the range of [MSC_BDESC..data_bytes].
420 if (iter->block == iter->start_block && iter->wrap_count == 2) {
421 tocopy = DATA_IN_PAGE - data_bytes;
428 tocopy -= iter->block_off;
429 src += iter->block_off;
436 remaining = fn(data, src, tocopy);
441 copied = tocopy - remaining;
443 iter->block_off += copied;
444 iter->offset += copied;
450 if (msc_iter_block_advance(iter))
459 * msc_buffer_clear_hw_header() - clear hw header for multiblock
462 static void msc_buffer_clear_hw_header(struct msc *msc)
464 struct msc_window *win;
466 list_for_each_entry(win, &msc->win_list, entry) {
468 size_t hw_sz = sizeof(struct msc_block_desc) -
469 offsetof(struct msc_block_desc, hw_tag);
471 for (blk = 0; blk < win->nr_blocks; blk++) {
472 struct msc_block_desc *bdesc = win->block[blk].bdesc;
474 memset(&bdesc->hw_tag, 0, hw_sz);
480 * msc_configure() - set up MSC hardware
481 * @msc: the MSC device to configure
483 * Program storage mode, wrapping, burst length and trace buffer address
484 * into a given MSC. Then, enable tracing and set msc::enabled.
485 * The latter is serialized on msc::buf_mutex, so make sure to hold it.
487 static int msc_configure(struct msc *msc)
491 lockdep_assert_held(&msc->buf_mutex);
493 if (msc->mode > MSC_MODE_MULTI)
496 if (msc->mode == MSC_MODE_MULTI)
497 msc_buffer_clear_hw_header(msc);
499 reg = msc->base_addr >> PAGE_SHIFT;
500 iowrite32(reg, msc->reg_base + REG_MSU_MSC0BAR);
502 if (msc->mode == MSC_MODE_SINGLE) {
504 iowrite32(reg, msc->reg_base + REG_MSU_MSC0SIZE);
507 reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
508 reg &= ~(MSC_MODE | MSC_WRAPEN | MSC_EN | MSC_RD_HDR_OVRD);
511 reg |= msc->mode << __ffs(MSC_MODE);
512 reg |= msc->burst_len << __ffs(MSC_LEN);
517 iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
519 msc->thdev->output.multiblock = msc->mode == MSC_MODE_MULTI;
520 intel_th_trace_enable(msc->thdev);
528 * msc_disable() - disable MSC hardware
529 * @msc: MSC device to disable
531 * If @msc is enabled, disable tracing on the switch and then disable MSC
532 * storage. Caller must hold msc::buf_mutex.
534 static void msc_disable(struct msc *msc)
539 lockdep_assert_held(&msc->buf_mutex);
541 intel_th_trace_disable(msc->thdev);
543 for (reg = 0, count = MSC_PLE_WAITLOOP_DEPTH;
544 count && !(reg & MSCSTS_PLE); count--) {
545 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
550 dev_dbg(msc_dev(msc), "timeout waiting for MSC0 PLE\n");
552 if (msc->mode == MSC_MODE_SINGLE) {
553 msc->single_wrap = !!(reg & MSCSTS_WRAPSTAT);
555 reg = ioread32(msc->reg_base + REG_MSU_MSC0MWP);
556 msc->single_sz = reg & ((msc->nr_pages << PAGE_SHIFT) - 1);
557 dev_dbg(msc_dev(msc), "MSCnMWP: %08x/%08lx, wrap: %d\n",
558 reg, msc->single_sz, msc->single_wrap);
561 reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
563 iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
566 iowrite32(0, msc->reg_base + REG_MSU_MSC0BAR);
567 iowrite32(0, msc->reg_base + REG_MSU_MSC0SIZE);
569 dev_dbg(msc_dev(msc), "MSCnNWSA: %08x\n",
570 ioread32(msc->reg_base + REG_MSU_MSC0NWSA));
572 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
573 dev_dbg(msc_dev(msc), "MSCnSTS: %08x\n", reg);
576 static int intel_th_msc_activate(struct intel_th_device *thdev)
578 struct msc *msc = dev_get_drvdata(&thdev->dev);
581 if (!atomic_inc_unless_negative(&msc->user_count))
584 mutex_lock(&msc->buf_mutex);
586 /* if there are readers, refuse */
587 if (list_empty(&msc->iter_list))
588 ret = msc_configure(msc);
590 mutex_unlock(&msc->buf_mutex);
593 atomic_dec(&msc->user_count);
598 static void intel_th_msc_deactivate(struct intel_th_device *thdev)
600 struct msc *msc = dev_get_drvdata(&thdev->dev);
602 mutex_lock(&msc->buf_mutex);
605 atomic_dec(&msc->user_count);
607 mutex_unlock(&msc->buf_mutex);
611 * msc_buffer_contig_alloc() - allocate a contiguous buffer for SINGLE mode
613 * @size: allocation size in bytes
615 * This modifies msc::base, which requires msc::buf_mutex to serialize, so the
616 * caller is expected to hold it.
618 * Return: 0 on success, -errno otherwise.
620 static int msc_buffer_contig_alloc(struct msc *msc, unsigned long size)
622 unsigned long nr_pages = size >> PAGE_SHIFT;
623 unsigned int order = get_order(size);
630 ret = sg_alloc_table(&msc->single_sgt, 1, GFP_KERNEL);
635 page = alloc_pages(GFP_KERNEL | __GFP_ZERO | GFP_DMA32, order);
639 split_page(page, order);
640 sg_set_buf(msc->single_sgt.sgl, page_address(page), size);
642 ret = dma_map_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl, 1,
647 msc->nr_pages = nr_pages;
648 msc->base = page_address(page);
649 msc->base_addr = sg_dma_address(msc->single_sgt.sgl);
654 __free_pages(page, order);
657 sg_free_table(&msc->single_sgt);
664 * msc_buffer_contig_free() - free a contiguous buffer
665 * @msc: MSC configured in SINGLE mode
667 static void msc_buffer_contig_free(struct msc *msc)
671 dma_unmap_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl,
673 sg_free_table(&msc->single_sgt);
675 for (off = 0; off < msc->nr_pages << PAGE_SHIFT; off += PAGE_SIZE) {
676 struct page *page = virt_to_page(msc->base + off);
678 page->mapping = NULL;
686 * msc_buffer_contig_get_page() - find a page at a given offset
687 * @msc: MSC configured in SINGLE mode
688 * @pgoff: page offset
690 * Return: page, if @pgoff is within the range, NULL otherwise.
692 static struct page *msc_buffer_contig_get_page(struct msc *msc,
695 if (pgoff >= msc->nr_pages)
698 return virt_to_page(msc->base + (pgoff << PAGE_SHIFT));
702 * msc_buffer_win_alloc() - alloc a window for a multiblock mode
704 * @nr_blocks: number of pages in this window
706 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
707 * to serialize, so the caller is expected to hold it.
709 * Return: 0 on success, -errno otherwise.
711 static int msc_buffer_win_alloc(struct msc *msc, unsigned int nr_blocks)
713 struct msc_window *win;
714 unsigned long size = PAGE_SIZE;
715 int i, ret = -ENOMEM;
720 win = kzalloc(offsetof(struct msc_window, block[nr_blocks]),
725 if (!list_empty(&msc->win_list)) {
726 struct msc_window *prev = list_entry(msc->win_list.prev,
727 struct msc_window, entry);
729 win->pgoff = prev->pgoff + prev->nr_blocks;
732 for (i = 0; i < nr_blocks; i++) {
733 win->block[i].bdesc =
734 dma_alloc_coherent(msc_dev(msc)->parent->parent, size,
735 &win->block[i].addr, GFP_KERNEL);
737 if (!win->block[i].bdesc)
741 /* Set the page as uncached */
742 set_memory_uc((unsigned long)win->block[i].bdesc, 1);
747 win->nr_blocks = nr_blocks;
749 if (list_empty(&msc->win_list)) {
750 msc->base = win->block[0].bdesc;
751 msc->base_addr = win->block[0].addr;
754 list_add_tail(&win->entry, &msc->win_list);
755 msc->nr_pages += nr_blocks;
760 for (i--; i >= 0; i--) {
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(msc)->parent->parent, size,
766 win->block[i].bdesc, win->block[i].addr);
774 * msc_buffer_win_free() - free a window from MSC's window list
776 * @win: window to free
778 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
779 * to serialize, so the caller is expected to hold it.
781 static void msc_buffer_win_free(struct msc *msc, struct msc_window *win)
785 msc->nr_pages -= win->nr_blocks;
787 list_del(&win->entry);
788 if (list_empty(&msc->win_list)) {
793 for (i = 0; i < win->nr_blocks; i++) {
794 struct page *page = virt_to_page(win->block[i].bdesc);
796 page->mapping = NULL;
798 /* Reset the page to write-back before releasing */
799 set_memory_wb((unsigned long)win->block[i].bdesc, 1);
801 dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
802 win->block[i].bdesc, win->block[i].addr);
809 * msc_buffer_relink() - set up block descriptors for multiblock mode
812 * This traverses msc::win_list, which requires msc::buf_mutex to serialize,
813 * so the caller is expected to hold it.
815 static void msc_buffer_relink(struct msc *msc)
817 struct msc_window *win, *next_win;
819 /* call with msc::mutex locked */
820 list_for_each_entry(win, &msc->win_list, entry) {
825 * Last window's next_win should point to the first window
826 * and MSC_SW_TAG_LASTWIN should be set.
828 if (msc_is_last_win(win)) {
829 sw_tag |= MSC_SW_TAG_LASTWIN;
830 next_win = list_entry(msc->win_list.next,
831 struct msc_window, entry);
833 next_win = list_entry(win->entry.next,
834 struct msc_window, entry);
837 for (blk = 0; blk < win->nr_blocks; blk++) {
838 struct msc_block_desc *bdesc = win->block[blk].bdesc;
840 memset(bdesc, 0, sizeof(*bdesc));
842 bdesc->next_win = next_win->block[0].addr >> PAGE_SHIFT;
845 * Similarly to last window, last block should point
848 if (blk == win->nr_blocks - 1) {
849 sw_tag |= MSC_SW_TAG_LASTBLK;
851 win->block[0].addr >> PAGE_SHIFT;
854 win->block[blk + 1].addr >> PAGE_SHIFT;
857 bdesc->sw_tag = sw_tag;
858 bdesc->block_sz = PAGE_SIZE / 64;
863 * Make the above writes globally visible before tracing is
864 * enabled to make sure hardware sees them coherently.
869 static void msc_buffer_multi_free(struct msc *msc)
871 struct msc_window *win, *iter;
873 list_for_each_entry_safe(win, iter, &msc->win_list, entry)
874 msc_buffer_win_free(msc, win);
877 static int msc_buffer_multi_alloc(struct msc *msc, unsigned long *nr_pages,
878 unsigned int nr_wins)
882 for (i = 0; i < nr_wins; i++) {
883 ret = msc_buffer_win_alloc(msc, nr_pages[i]);
885 msc_buffer_multi_free(msc);
890 msc_buffer_relink(msc);
896 * msc_buffer_free() - free buffers for MSC
899 * Free MSC's storage buffers.
901 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex to
902 * serialize, so the caller is expected to hold it.
904 static void msc_buffer_free(struct msc *msc)
906 if (msc->mode == MSC_MODE_SINGLE)
907 msc_buffer_contig_free(msc);
908 else if (msc->mode == MSC_MODE_MULTI)
909 msc_buffer_multi_free(msc);
913 * msc_buffer_alloc() - allocate a buffer for MSC
915 * @size: allocation size in bytes
917 * Allocate a storage buffer for MSC, depending on the msc::mode, it will be
918 * either done via msc_buffer_contig_alloc() for SINGLE operation mode or
919 * msc_buffer_win_alloc() for multiblock operation. The latter allocates one
920 * window per invocation, so in multiblock mode this can be called multiple
921 * times for the same MSC to allocate multiple windows.
923 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
924 * to serialize, so the caller is expected to hold it.
926 * Return: 0 on success, -errno otherwise.
928 static int msc_buffer_alloc(struct msc *msc, unsigned long *nr_pages,
929 unsigned int nr_wins)
933 /* -1: buffer not allocated */
934 if (atomic_read(&msc->user_count) != -1)
937 if (msc->mode == MSC_MODE_SINGLE) {
941 ret = msc_buffer_contig_alloc(msc, nr_pages[0] << PAGE_SHIFT);
942 } else if (msc->mode == MSC_MODE_MULTI) {
943 ret = msc_buffer_multi_alloc(msc, nr_pages, nr_wins);
949 /* allocation should be visible before the counter goes to 0 */
950 smp_mb__before_atomic();
952 if (WARN_ON_ONCE(atomic_cmpxchg(&msc->user_count, -1, 0) != -1))
960 * msc_buffer_unlocked_free_unless_used() - free a buffer unless it's in use
963 * This will free MSC buffer unless it is in use or there is no allocated
965 * Caller needs to hold msc::buf_mutex.
967 * Return: 0 on successful deallocation or if there was no buffer to
968 * deallocate, -EBUSY if there are active users.
970 static int msc_buffer_unlocked_free_unless_used(struct msc *msc)
974 count = atomic_cmpxchg(&msc->user_count, 0, -1);
976 /* > 0: buffer is allocated and has users */
979 /* 0: buffer is allocated, no users */
981 msc_buffer_free(msc);
982 /* < 0: no buffer, nothing to do */
988 * msc_buffer_free_unless_used() - free a buffer unless it's in use
991 * This is a locked version of msc_buffer_unlocked_free_unless_used().
993 static int msc_buffer_free_unless_used(struct msc *msc)
997 mutex_lock(&msc->buf_mutex);
998 ret = msc_buffer_unlocked_free_unless_used(msc);
999 mutex_unlock(&msc->buf_mutex);
1005 * msc_buffer_get_page() - get MSC buffer page at a given offset
1007 * @pgoff: page offset into the storage buffer
1009 * This traverses msc::win_list, so holding msc::buf_mutex is expected from
1012 * Return: page if @pgoff corresponds to a valid buffer page or NULL.
1014 static struct page *msc_buffer_get_page(struct msc *msc, unsigned long pgoff)
1016 struct msc_window *win;
1018 if (msc->mode == MSC_MODE_SINGLE)
1019 return msc_buffer_contig_get_page(msc, pgoff);
1021 list_for_each_entry(win, &msc->win_list, entry)
1022 if (pgoff >= win->pgoff && pgoff < win->pgoff + win->nr_blocks)
1028 pgoff -= win->pgoff;
1029 return virt_to_page(win->block[pgoff].bdesc);
1033 * struct msc_win_to_user_struct - data for copy_to_user() callback
1034 * @buf: userspace buffer to copy data to
1035 * @offset: running offset
1037 struct msc_win_to_user_struct {
1039 unsigned long offset;
1043 * msc_win_to_user() - iterator for msc_buffer_iterate() to copy data to user
1044 * @data: callback's private data
1045 * @src: source buffer
1046 * @len: amount of data to copy from the source buffer
1048 static unsigned long msc_win_to_user(void *data, void *src, size_t len)
1050 struct msc_win_to_user_struct *u = data;
1053 ret = copy_to_user(u->buf + u->offset, src, len);
1054 u->offset += len - ret;
1061 * file operations' callbacks
1064 static int intel_th_msc_open(struct inode *inode, struct file *file)
1066 struct intel_th_device *thdev = file->private_data;
1067 struct msc *msc = dev_get_drvdata(&thdev->dev);
1068 struct msc_iter *iter;
1070 if (!capable(CAP_SYS_RAWIO))
1073 iter = msc_iter_install(msc);
1075 return PTR_ERR(iter);
1077 file->private_data = iter;
1079 return nonseekable_open(inode, file);
1082 static int intel_th_msc_release(struct inode *inode, struct file *file)
1084 struct msc_iter *iter = file->private_data;
1085 struct msc *msc = iter->msc;
1087 msc_iter_remove(iter, msc);
1093 msc_single_to_user(struct msc *msc, char __user *buf, loff_t off, size_t len)
1095 unsigned long size = msc->nr_pages << PAGE_SHIFT, rem = len;
1096 unsigned long start = off, tocopy = 0;
1098 if (msc->single_wrap) {
1099 start += msc->single_sz;
1101 tocopy = min(rem, size - start);
1102 if (copy_to_user(buf, msc->base + start, tocopy))
1112 tocopy = min(rem, msc->single_sz - start);
1113 if (copy_to_user(buf, msc->base + start, tocopy))
1122 if (copy_to_user(buf, msc->base + start, rem))
1128 static ssize_t intel_th_msc_read(struct file *file, char __user *buf,
1129 size_t len, loff_t *ppos)
1131 struct msc_iter *iter = file->private_data;
1132 struct msc *msc = iter->msc;
1137 if (!atomic_inc_unless_negative(&msc->user_count))
1140 if (msc->mode == MSC_MODE_SINGLE && !msc->single_wrap)
1141 size = msc->single_sz;
1143 size = msc->nr_pages << PAGE_SHIFT;
1151 if (off + len >= size)
1154 if (msc->mode == MSC_MODE_SINGLE) {
1155 ret = msc_single_to_user(msc, buf, off, len);
1158 } else if (msc->mode == MSC_MODE_MULTI) {
1159 struct msc_win_to_user_struct u = {
1164 ret = msc_buffer_iterate(iter, len, &u, msc_win_to_user);
1166 *ppos = iter->offset;
1172 atomic_dec(&msc->user_count);
1178 * vm operations callbacks (vm_ops)
1181 static void msc_mmap_open(struct vm_area_struct *vma)
1183 struct msc_iter *iter = vma->vm_file->private_data;
1184 struct msc *msc = iter->msc;
1186 atomic_inc(&msc->mmap_count);
1189 static void msc_mmap_close(struct vm_area_struct *vma)
1191 struct msc_iter *iter = vma->vm_file->private_data;
1192 struct msc *msc = iter->msc;
1195 if (!atomic_dec_and_mutex_lock(&msc->mmap_count, &msc->buf_mutex))
1198 /* drop page _refcounts */
1199 for (pg = 0; pg < msc->nr_pages; pg++) {
1200 struct page *page = msc_buffer_get_page(msc, pg);
1202 if (WARN_ON_ONCE(!page))
1206 page->mapping = NULL;
1209 /* last mapping -- drop user_count */
1210 atomic_dec(&msc->user_count);
1211 mutex_unlock(&msc->buf_mutex);
1214 static vm_fault_t msc_mmap_fault(struct vm_fault *vmf)
1216 struct msc_iter *iter = vmf->vma->vm_file->private_data;
1217 struct msc *msc = iter->msc;
1219 vmf->page = msc_buffer_get_page(msc, vmf->pgoff);
1221 return VM_FAULT_SIGBUS;
1223 get_page(vmf->page);
1224 vmf->page->mapping = vmf->vma->vm_file->f_mapping;
1225 vmf->page->index = vmf->pgoff;
1230 static const struct vm_operations_struct msc_mmap_ops = {
1231 .open = msc_mmap_open,
1232 .close = msc_mmap_close,
1233 .fault = msc_mmap_fault,
1236 static int intel_th_msc_mmap(struct file *file, struct vm_area_struct *vma)
1238 unsigned long size = vma->vm_end - vma->vm_start;
1239 struct msc_iter *iter = vma->vm_file->private_data;
1240 struct msc *msc = iter->msc;
1243 if (!size || offset_in_page(size))
1249 /* grab user_count once per mmap; drop in msc_mmap_close() */
1250 if (!atomic_inc_unless_negative(&msc->user_count))
1253 if (msc->mode != MSC_MODE_SINGLE &&
1254 msc->mode != MSC_MODE_MULTI)
1257 if (size >> PAGE_SHIFT != msc->nr_pages)
1260 atomic_set(&msc->mmap_count, 1);
1265 atomic_dec(&msc->user_count);
1267 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1268 vma->vm_flags |= VM_DONTEXPAND | VM_DONTCOPY;
1269 vma->vm_ops = &msc_mmap_ops;
1273 static const struct file_operations intel_th_msc_fops = {
1274 .open = intel_th_msc_open,
1275 .release = intel_th_msc_release,
1276 .read = intel_th_msc_read,
1277 .mmap = intel_th_msc_mmap,
1278 .llseek = no_llseek,
1279 .owner = THIS_MODULE,
1282 static int intel_th_msc_init(struct msc *msc)
1284 atomic_set(&msc->user_count, -1);
1286 msc->mode = MSC_MODE_MULTI;
1287 mutex_init(&msc->buf_mutex);
1288 INIT_LIST_HEAD(&msc->win_list);
1289 INIT_LIST_HEAD(&msc->iter_list);
1292 (ioread32(msc->reg_base + REG_MSU_MSC0CTL) & MSC_LEN) >>
1298 static const char * const msc_mode[] = {
1299 [MSC_MODE_SINGLE] = "single",
1300 [MSC_MODE_MULTI] = "multi",
1301 [MSC_MODE_EXI] = "ExI",
1302 [MSC_MODE_DEBUG] = "debug",
1306 wrap_show(struct device *dev, struct device_attribute *attr, char *buf)
1308 struct msc *msc = dev_get_drvdata(dev);
1310 return scnprintf(buf, PAGE_SIZE, "%d\n", msc->wrap);
1314 wrap_store(struct device *dev, struct device_attribute *attr, const char *buf,
1317 struct msc *msc = dev_get_drvdata(dev);
1321 ret = kstrtoul(buf, 10, &val);
1330 static DEVICE_ATTR_RW(wrap);
1333 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1335 struct msc *msc = dev_get_drvdata(dev);
1337 return scnprintf(buf, PAGE_SIZE, "%s\n", msc_mode[msc->mode]);
1341 mode_store(struct device *dev, struct device_attribute *attr, const char *buf,
1344 struct msc *msc = dev_get_drvdata(dev);
1349 if (!capable(CAP_SYS_RAWIO))
1352 cp = memchr(buf, '\n', len);
1356 for (i = 0; i < ARRAY_SIZE(msc_mode); i++)
1357 if (!strncmp(msc_mode[i], buf, len))
1363 mutex_lock(&msc->buf_mutex);
1364 ret = msc_buffer_unlocked_free_unless_used(msc);
1367 mutex_unlock(&msc->buf_mutex);
1369 return ret ? ret : size;
1372 static DEVICE_ATTR_RW(mode);
1375 nr_pages_show(struct device *dev, struct device_attribute *attr, char *buf)
1377 struct msc *msc = dev_get_drvdata(dev);
1378 struct msc_window *win;
1381 mutex_lock(&msc->buf_mutex);
1383 if (msc->mode == MSC_MODE_SINGLE)
1384 count = scnprintf(buf, PAGE_SIZE, "%ld\n", msc->nr_pages);
1385 else if (msc->mode == MSC_MODE_MULTI) {
1386 list_for_each_entry(win, &msc->win_list, entry) {
1387 count += scnprintf(buf + count, PAGE_SIZE - count,
1388 "%d%c", win->nr_blocks,
1389 msc_is_last_win(win) ? '\n' : ',');
1392 count = scnprintf(buf, PAGE_SIZE, "unsupported\n");
1395 mutex_unlock(&msc->buf_mutex);
1401 nr_pages_store(struct device *dev, struct device_attribute *attr,
1402 const char *buf, size_t size)
1404 struct msc *msc = dev_get_drvdata(dev);
1405 unsigned long val, *win = NULL, *rewin;
1407 const char *p = buf;
1409 int ret, nr_wins = 0;
1411 if (!capable(CAP_SYS_RAWIO))
1414 ret = msc_buffer_free_unless_used(msc);
1418 /* scan the comma-separated list of allocation sizes */
1419 end = memchr(buf, '\n', len);
1424 end = memchr(p, ',', len);
1425 s = kstrndup(p, end ? end - p : len, GFP_KERNEL);
1431 ret = kstrtoul(s, 10, &val);
1437 if (nr_wins && msc->mode == MSC_MODE_SINGLE) {
1443 rewin = krealloc(win, sizeof(*win) * nr_wins, GFP_KERNEL);
1450 win[nr_wins - 1] = val;
1455 /* consume the number and the following comma, hence +1 */
1460 mutex_lock(&msc->buf_mutex);
1461 ret = msc_buffer_alloc(msc, win, nr_wins);
1462 mutex_unlock(&msc->buf_mutex);
1467 return ret ? ret : size;
1470 static DEVICE_ATTR_RW(nr_pages);
1472 static struct attribute *msc_output_attrs[] = {
1473 &dev_attr_wrap.attr,
1474 &dev_attr_mode.attr,
1475 &dev_attr_nr_pages.attr,
1479 static struct attribute_group msc_output_group = {
1480 .attrs = msc_output_attrs,
1483 static int intel_th_msc_probe(struct intel_th_device *thdev)
1485 struct device *dev = &thdev->dev;
1486 struct resource *res;
1491 res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
1495 base = devm_ioremap(dev, res->start, resource_size(res));
1499 msc = devm_kzalloc(dev, sizeof(*msc), GFP_KERNEL);
1503 msc->index = thdev->id;
1506 msc->reg_base = base + msc->index * 0x100;
1508 err = intel_th_msc_init(msc);
1512 dev_set_drvdata(dev, msc);
1517 static void intel_th_msc_remove(struct intel_th_device *thdev)
1519 struct msc *msc = dev_get_drvdata(&thdev->dev);
1522 intel_th_msc_deactivate(thdev);
1525 * Buffers should not be used at this point except if the
1526 * output character device is still open and the parent
1527 * device gets detached from its bus, which is a FIXME.
1529 ret = msc_buffer_free_unless_used(msc);
1533 static struct intel_th_driver intel_th_msc_driver = {
1534 .probe = intel_th_msc_probe,
1535 .remove = intel_th_msc_remove,
1536 .activate = intel_th_msc_activate,
1537 .deactivate = intel_th_msc_deactivate,
1538 .fops = &intel_th_msc_fops,
1539 .attr_group = &msc_output_group,
1542 .owner = THIS_MODULE,
1546 module_driver(intel_th_msc_driver,
1547 intel_th_driver_register,
1548 intel_th_driver_unregister);
1550 MODULE_LICENSE("GPL v2");
1551 MODULE_DESCRIPTION("Intel(R) Trace Hub Memory Storage Unit driver");
1552 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");