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 void __iomem *msu_base;
106 struct intel_th_device *thdev;
108 struct list_head win_list;
109 struct sg_table single_sgt;
110 unsigned long nr_pages;
111 unsigned long single_sz;
112 unsigned int single_wrap : 1;
114 dma_addr_t base_addr;
116 /* <0: no buffer, 0: no users, >0: active users */
120 struct mutex buf_mutex;
122 struct list_head iter_list;
125 unsigned int enabled : 1,
129 unsigned int burst_len;
133 static inline bool msc_block_is_empty(struct msc_block_desc *bdesc)
135 /* header hasn't been written */
136 if (!bdesc->valid_dw)
139 /* valid_dw includes the header */
140 if (!msc_data_sz(bdesc))
147 * msc_oldest_window() - locate the window with oldest data
150 * This should only be used in multiblock mode. Caller should hold the
151 * msc::user_count reference.
153 * Return: the oldest window with valid data
155 static struct msc_window *msc_oldest_window(struct msc *msc)
157 struct msc_window *win;
158 u32 reg = ioread32(msc->reg_base + REG_MSU_MSC0NWSA);
159 unsigned long win_addr = (unsigned long)reg << PAGE_SHIFT;
160 unsigned int found = 0;
162 if (list_empty(&msc->win_list))
166 * we might need a radix tree for this, depending on how
167 * many windows a typical user would allocate; ideally it's
168 * something like 2, in which case we're good
170 list_for_each_entry(win, &msc->win_list, entry) {
171 if (win->block[0].addr == win_addr)
174 /* skip the empty ones */
175 if (msc_block_is_empty(win->block[0].bdesc))
182 return list_first_entry(&msc->win_list, struct msc_window, entry);
186 * msc_win_oldest_block() - locate the oldest block in a given window
187 * @win: window to look at
189 * Return: index of the block with the oldest data
191 static unsigned int msc_win_oldest_block(struct msc_window *win)
194 struct msc_block_desc *bdesc = win->block[0].bdesc;
196 /* without wrapping, first block is the oldest */
197 if (!msc_block_wrapped(bdesc))
201 * with wrapping, last written block contains both the newest and the
202 * oldest data for this window.
204 for (blk = 0; blk < win->nr_blocks; blk++) {
205 bdesc = win->block[blk].bdesc;
207 if (msc_block_last_written(bdesc))
215 * msc_is_last_win() - check if a window is the last one for a given MSC
217 * Return: true if @win is the last window in MSC's multiblock buffer
219 static inline bool msc_is_last_win(struct msc_window *win)
221 return win->entry.next == &win->msc->win_list;
225 * msc_next_window() - return next window in the multiblock buffer
226 * @win: current window
228 * Return: window following the current one
230 static struct msc_window *msc_next_window(struct msc_window *win)
232 if (msc_is_last_win(win))
233 return list_first_entry(&win->msc->win_list, struct msc_window,
236 return list_next_entry(win, entry);
239 static struct msc_block_desc *msc_iter_bdesc(struct msc_iter *iter)
241 return iter->win->block[iter->block].bdesc;
244 static void msc_iter_init(struct msc_iter *iter)
246 memset(iter, 0, sizeof(*iter));
247 iter->start_block = -1;
251 static struct msc_iter *msc_iter_install(struct msc *msc)
253 struct msc_iter *iter;
255 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
257 return ERR_PTR(-ENOMEM);
259 mutex_lock(&msc->buf_mutex);
262 * Reading and tracing are mutually exclusive; if msc is
263 * enabled, open() will fail; otherwise existing readers
264 * will prevent enabling the msc and the rest of fops don't
265 * need to worry about it.
269 iter = ERR_PTR(-EBUSY);
276 list_add_tail(&iter->entry, &msc->iter_list);
278 mutex_unlock(&msc->buf_mutex);
283 static void msc_iter_remove(struct msc_iter *iter, struct msc *msc)
285 mutex_lock(&msc->buf_mutex);
286 list_del(&iter->entry);
287 mutex_unlock(&msc->buf_mutex);
292 static void msc_iter_block_start(struct msc_iter *iter)
294 if (iter->start_block != -1)
297 iter->start_block = msc_win_oldest_block(iter->win);
298 iter->block = iter->start_block;
299 iter->wrap_count = 0;
302 * start with the block with oldest data; if data has wrapped
303 * in this window, it should be in this block
305 if (msc_block_wrapped(msc_iter_bdesc(iter)))
306 iter->wrap_count = 2;
310 static int msc_iter_win_start(struct msc_iter *iter, struct msc *msc)
312 /* already started, nothing to do */
316 iter->start_win = msc_oldest_window(msc);
317 if (!iter->start_win)
320 iter->win = iter->start_win;
321 iter->start_block = -1;
323 msc_iter_block_start(iter);
328 static int msc_iter_win_advance(struct msc_iter *iter)
330 iter->win = msc_next_window(iter->win);
331 iter->start_block = -1;
333 if (iter->win == iter->start_win) {
338 msc_iter_block_start(iter);
343 static int msc_iter_block_advance(struct msc_iter *iter)
348 if (iter->wrap_count && iter->block == iter->start_block) {
350 if (!iter->wrap_count)
351 /* copied newest data from the wrapped block */
352 return msc_iter_win_advance(iter);
355 /* no wrapping, check for last written block */
356 if (!iter->wrap_count && msc_block_last_written(msc_iter_bdesc(iter)))
357 /* copied newest data for the window */
358 return msc_iter_win_advance(iter);
361 if (++iter->block == iter->win->nr_blocks)
364 /* no wrapping, sanity check in case there is no last written block */
365 if (!iter->wrap_count && iter->block == iter->start_block)
366 return msc_iter_win_advance(iter);
372 * msc_buffer_iterate() - go through multiblock buffer's data
373 * @iter: iterator structure
374 * @size: amount of data to scan
375 * @data: callback's private data
376 * @fn: iterator callback
378 * This will start at the window which will be written to next (containing
379 * the oldest data) and work its way to the current window, calling @fn
380 * for each chunk of data as it goes.
382 * Caller should have msc::user_count reference to make sure the buffer
383 * doesn't disappear from under us.
385 * Return: amount of data actually scanned.
388 msc_buffer_iterate(struct msc_iter *iter, size_t size, void *data,
389 unsigned long (*fn)(void *, void *, size_t))
391 struct msc *msc = iter->msc;
393 unsigned int advance;
398 /* start with the oldest window */
399 if (msc_iter_win_start(iter, msc))
403 unsigned long data_bytes = msc_data_sz(msc_iter_bdesc(iter));
404 void *src = (void *)msc_iter_bdesc(iter) + MSC_BDESC;
405 size_t tocopy = data_bytes, copied = 0;
406 size_t remaining = 0;
411 * If block wrapping happened, we need to visit the last block
412 * twice, because it contains both the oldest and the newest
413 * data in this window.
415 * First time (wrap_count==2), in the very beginning, to collect
416 * the oldest data, which is in the range
417 * (data_bytes..DATA_IN_PAGE).
419 * Second time (wrap_count==1), it's just like any other block,
420 * containing data in the range of [MSC_BDESC..data_bytes].
422 if (iter->block == iter->start_block && iter->wrap_count == 2) {
423 tocopy = DATA_IN_PAGE - data_bytes;
430 tocopy -= iter->block_off;
431 src += iter->block_off;
438 remaining = fn(data, src, tocopy);
443 copied = tocopy - remaining;
445 iter->block_off += copied;
446 iter->offset += copied;
452 if (msc_iter_block_advance(iter))
461 * msc_buffer_clear_hw_header() - clear hw header for multiblock
464 static void msc_buffer_clear_hw_header(struct msc *msc)
466 struct msc_window *win;
468 list_for_each_entry(win, &msc->win_list, entry) {
470 size_t hw_sz = sizeof(struct msc_block_desc) -
471 offsetof(struct msc_block_desc, hw_tag);
473 for (blk = 0; blk < win->nr_blocks; blk++) {
474 struct msc_block_desc *bdesc = win->block[blk].bdesc;
476 memset(&bdesc->hw_tag, 0, hw_sz);
481 static int intel_th_msu_init(struct msc *msc)
488 mintctl = ioread32(msc->msu_base + REG_MSU_MINTCTL);
489 mintctl |= msc->index ? M1BLIE : M0BLIE;
490 iowrite32(mintctl, msc->msu_base + REG_MSU_MINTCTL);
491 if (mintctl != ioread32(msc->msu_base + REG_MSU_MINTCTL)) {
492 dev_info(msc_dev(msc), "MINTCTL ignores writes: no usable interrupts\n");
497 msusts = ioread32(msc->msu_base + REG_MSU_MSUSTS);
498 iowrite32(msusts, msc->msu_base + REG_MSU_MSUSTS);
503 static void intel_th_msu_deinit(struct msc *msc)
510 mintctl = ioread32(msc->msu_base + REG_MSU_MINTCTL);
511 mintctl &= msc->index ? ~M1BLIE : ~M0BLIE;
512 iowrite32(mintctl, msc->msu_base + REG_MSU_MINTCTL);
516 * msc_configure() - set up MSC hardware
517 * @msc: the MSC device to configure
519 * Program storage mode, wrapping, burst length and trace buffer address
520 * into a given MSC. Then, enable tracing and set msc::enabled.
521 * The latter is serialized on msc::buf_mutex, so make sure to hold it.
523 static int msc_configure(struct msc *msc)
527 lockdep_assert_held(&msc->buf_mutex);
529 if (msc->mode > MSC_MODE_MULTI)
532 if (msc->mode == MSC_MODE_MULTI)
533 msc_buffer_clear_hw_header(msc);
535 reg = msc->base_addr >> PAGE_SHIFT;
536 iowrite32(reg, msc->reg_base + REG_MSU_MSC0BAR);
538 if (msc->mode == MSC_MODE_SINGLE) {
540 iowrite32(reg, msc->reg_base + REG_MSU_MSC0SIZE);
543 reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
544 reg &= ~(MSC_MODE | MSC_WRAPEN | MSC_EN | MSC_RD_HDR_OVRD);
547 reg |= msc->mode << __ffs(MSC_MODE);
548 reg |= msc->burst_len << __ffs(MSC_LEN);
553 iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
555 msc->thdev->output.multiblock = msc->mode == MSC_MODE_MULTI;
556 intel_th_trace_enable(msc->thdev);
564 * msc_disable() - disable MSC hardware
565 * @msc: MSC device to disable
567 * If @msc is enabled, disable tracing on the switch and then disable MSC
568 * storage. Caller must hold msc::buf_mutex.
570 static void msc_disable(struct msc *msc)
575 lockdep_assert_held(&msc->buf_mutex);
577 intel_th_trace_disable(msc->thdev);
579 for (reg = 0, count = MSC_PLE_WAITLOOP_DEPTH;
580 count && !(reg & MSCSTS_PLE); count--) {
581 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
586 dev_dbg(msc_dev(msc), "timeout waiting for MSC0 PLE\n");
588 if (msc->mode == MSC_MODE_SINGLE) {
589 msc->single_wrap = !!(reg & MSCSTS_WRAPSTAT);
591 reg = ioread32(msc->reg_base + REG_MSU_MSC0MWP);
592 msc->single_sz = reg & ((msc->nr_pages << PAGE_SHIFT) - 1);
593 dev_dbg(msc_dev(msc), "MSCnMWP: %08x/%08lx, wrap: %d\n",
594 reg, msc->single_sz, msc->single_wrap);
597 reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
599 iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
602 iowrite32(0, msc->reg_base + REG_MSU_MSC0BAR);
603 iowrite32(0, msc->reg_base + REG_MSU_MSC0SIZE);
605 dev_dbg(msc_dev(msc), "MSCnNWSA: %08x\n",
606 ioread32(msc->reg_base + REG_MSU_MSC0NWSA));
608 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
609 dev_dbg(msc_dev(msc), "MSCnSTS: %08x\n", reg);
612 static int intel_th_msc_activate(struct intel_th_device *thdev)
614 struct msc *msc = dev_get_drvdata(&thdev->dev);
617 if (!atomic_inc_unless_negative(&msc->user_count))
620 mutex_lock(&msc->buf_mutex);
622 /* if there are readers, refuse */
623 if (list_empty(&msc->iter_list))
624 ret = msc_configure(msc);
626 mutex_unlock(&msc->buf_mutex);
629 atomic_dec(&msc->user_count);
634 static void intel_th_msc_deactivate(struct intel_th_device *thdev)
636 struct msc *msc = dev_get_drvdata(&thdev->dev);
638 mutex_lock(&msc->buf_mutex);
641 atomic_dec(&msc->user_count);
643 mutex_unlock(&msc->buf_mutex);
647 * msc_buffer_contig_alloc() - allocate a contiguous buffer for SINGLE mode
649 * @size: allocation size in bytes
651 * This modifies msc::base, which requires msc::buf_mutex to serialize, so the
652 * caller is expected to hold it.
654 * Return: 0 on success, -errno otherwise.
656 static int msc_buffer_contig_alloc(struct msc *msc, unsigned long size)
658 unsigned long nr_pages = size >> PAGE_SHIFT;
659 unsigned int order = get_order(size);
666 ret = sg_alloc_table(&msc->single_sgt, 1, GFP_KERNEL);
671 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
675 split_page(page, order);
676 sg_set_buf(msc->single_sgt.sgl, page_address(page), size);
678 ret = dma_map_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl, 1,
683 msc->nr_pages = nr_pages;
684 msc->base = page_address(page);
685 msc->base_addr = sg_dma_address(msc->single_sgt.sgl);
690 __free_pages(page, order);
693 sg_free_table(&msc->single_sgt);
700 * msc_buffer_contig_free() - free a contiguous buffer
701 * @msc: MSC configured in SINGLE mode
703 static void msc_buffer_contig_free(struct msc *msc)
707 dma_unmap_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl,
709 sg_free_table(&msc->single_sgt);
711 for (off = 0; off < msc->nr_pages << PAGE_SHIFT; off += PAGE_SIZE) {
712 struct page *page = virt_to_page(msc->base + off);
714 page->mapping = NULL;
722 * msc_buffer_contig_get_page() - find a page at a given offset
723 * @msc: MSC configured in SINGLE mode
724 * @pgoff: page offset
726 * Return: page, if @pgoff is within the range, NULL otherwise.
728 static struct page *msc_buffer_contig_get_page(struct msc *msc,
731 if (pgoff >= msc->nr_pages)
734 return virt_to_page(msc->base + (pgoff << PAGE_SHIFT));
738 * msc_buffer_win_alloc() - alloc a window for a multiblock mode
740 * @nr_blocks: number of pages in this window
742 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
743 * to serialize, so the caller is expected to hold it.
745 * Return: 0 on success, -errno otherwise.
747 static int msc_buffer_win_alloc(struct msc *msc, unsigned int nr_blocks)
749 struct msc_window *win;
750 unsigned long size = PAGE_SIZE;
751 int i, ret = -ENOMEM;
756 win = kzalloc(offsetof(struct msc_window, block[nr_blocks]),
761 if (!list_empty(&msc->win_list)) {
762 struct msc_window *prev = list_last_entry(&msc->win_list,
766 win->pgoff = prev->pgoff + prev->nr_blocks;
769 for (i = 0; i < nr_blocks; i++) {
770 win->block[i].bdesc =
771 dma_alloc_coherent(msc_dev(msc)->parent->parent, size,
772 &win->block[i].addr, GFP_KERNEL);
774 if (!win->block[i].bdesc)
778 /* Set the page as uncached */
779 set_memory_uc((unsigned long)win->block[i].bdesc, 1);
784 win->nr_blocks = nr_blocks;
786 if (list_empty(&msc->win_list)) {
787 msc->base = win->block[0].bdesc;
788 msc->base_addr = win->block[0].addr;
791 list_add_tail(&win->entry, &msc->win_list);
792 msc->nr_pages += nr_blocks;
797 for (i--; i >= 0; i--) {
799 /* Reset the page to write-back before releasing */
800 set_memory_wb((unsigned long)win->block[i].bdesc, 1);
802 dma_free_coherent(msc_dev(msc)->parent->parent, size,
803 win->block[i].bdesc, win->block[i].addr);
811 * msc_buffer_win_free() - free a window from MSC's window list
813 * @win: window to free
815 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
816 * to serialize, so the caller is expected to hold it.
818 static void msc_buffer_win_free(struct msc *msc, struct msc_window *win)
822 msc->nr_pages -= win->nr_blocks;
824 list_del(&win->entry);
825 if (list_empty(&msc->win_list)) {
830 for (i = 0; i < win->nr_blocks; i++) {
831 struct page *page = virt_to_page(win->block[i].bdesc);
833 page->mapping = NULL;
835 /* Reset the page to write-back before releasing */
836 set_memory_wb((unsigned long)win->block[i].bdesc, 1);
838 dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
839 win->block[i].bdesc, win->block[i].addr);
846 * msc_buffer_relink() - set up block descriptors for multiblock mode
849 * This traverses msc::win_list, which requires msc::buf_mutex to serialize,
850 * so the caller is expected to hold it.
852 static void msc_buffer_relink(struct msc *msc)
854 struct msc_window *win, *next_win;
856 /* call with msc::mutex locked */
857 list_for_each_entry(win, &msc->win_list, entry) {
862 * Last window's next_win should point to the first window
863 * and MSC_SW_TAG_LASTWIN should be set.
865 if (msc_is_last_win(win)) {
866 sw_tag |= MSC_SW_TAG_LASTWIN;
867 next_win = list_first_entry(&msc->win_list,
868 struct msc_window, entry);
870 next_win = list_next_entry(win, entry);
873 for (blk = 0; blk < win->nr_blocks; blk++) {
874 struct msc_block_desc *bdesc = win->block[blk].bdesc;
876 memset(bdesc, 0, sizeof(*bdesc));
878 bdesc->next_win = next_win->block[0].addr >> PAGE_SHIFT;
881 * Similarly to last window, last block should point
884 if (blk == win->nr_blocks - 1) {
885 sw_tag |= MSC_SW_TAG_LASTBLK;
887 win->block[0].addr >> PAGE_SHIFT;
890 win->block[blk + 1].addr >> PAGE_SHIFT;
893 bdesc->sw_tag = sw_tag;
894 bdesc->block_sz = PAGE_SIZE / 64;
899 * Make the above writes globally visible before tracing is
900 * enabled to make sure hardware sees them coherently.
905 static void msc_buffer_multi_free(struct msc *msc)
907 struct msc_window *win, *iter;
909 list_for_each_entry_safe(win, iter, &msc->win_list, entry)
910 msc_buffer_win_free(msc, win);
913 static int msc_buffer_multi_alloc(struct msc *msc, unsigned long *nr_pages,
914 unsigned int nr_wins)
918 for (i = 0; i < nr_wins; i++) {
919 ret = msc_buffer_win_alloc(msc, nr_pages[i]);
921 msc_buffer_multi_free(msc);
926 msc_buffer_relink(msc);
932 * msc_buffer_free() - free buffers for MSC
935 * Free MSC's storage buffers.
937 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex to
938 * serialize, so the caller is expected to hold it.
940 static void msc_buffer_free(struct msc *msc)
942 if (msc->mode == MSC_MODE_SINGLE)
943 msc_buffer_contig_free(msc);
944 else if (msc->mode == MSC_MODE_MULTI)
945 msc_buffer_multi_free(msc);
949 * msc_buffer_alloc() - allocate a buffer for MSC
951 * @size: allocation size in bytes
953 * Allocate a storage buffer for MSC, depending on the msc::mode, it will be
954 * either done via msc_buffer_contig_alloc() for SINGLE operation mode or
955 * msc_buffer_win_alloc() for multiblock operation. The latter allocates one
956 * window per invocation, so in multiblock mode this can be called multiple
957 * times for the same MSC to allocate multiple windows.
959 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
960 * to serialize, so the caller is expected to hold it.
962 * Return: 0 on success, -errno otherwise.
964 static int msc_buffer_alloc(struct msc *msc, unsigned long *nr_pages,
965 unsigned int nr_wins)
969 /* -1: buffer not allocated */
970 if (atomic_read(&msc->user_count) != -1)
973 if (msc->mode == MSC_MODE_SINGLE) {
977 ret = msc_buffer_contig_alloc(msc, nr_pages[0] << PAGE_SHIFT);
978 } else if (msc->mode == MSC_MODE_MULTI) {
979 ret = msc_buffer_multi_alloc(msc, nr_pages, nr_wins);
985 /* allocation should be visible before the counter goes to 0 */
986 smp_mb__before_atomic();
988 if (WARN_ON_ONCE(atomic_cmpxchg(&msc->user_count, -1, 0) != -1))
996 * msc_buffer_unlocked_free_unless_used() - free a buffer unless it's in use
999 * This will free MSC buffer unless it is in use or there is no allocated
1001 * Caller needs to hold msc::buf_mutex.
1003 * Return: 0 on successful deallocation or if there was no buffer to
1004 * deallocate, -EBUSY if there are active users.
1006 static int msc_buffer_unlocked_free_unless_used(struct msc *msc)
1010 count = atomic_cmpxchg(&msc->user_count, 0, -1);
1012 /* > 0: buffer is allocated and has users */
1015 /* 0: buffer is allocated, no users */
1017 msc_buffer_free(msc);
1018 /* < 0: no buffer, nothing to do */
1024 * msc_buffer_free_unless_used() - free a buffer unless it's in use
1027 * This is a locked version of msc_buffer_unlocked_free_unless_used().
1029 static int msc_buffer_free_unless_used(struct msc *msc)
1033 mutex_lock(&msc->buf_mutex);
1034 ret = msc_buffer_unlocked_free_unless_used(msc);
1035 mutex_unlock(&msc->buf_mutex);
1041 * msc_buffer_get_page() - get MSC buffer page at a given offset
1043 * @pgoff: page offset into the storage buffer
1045 * This traverses msc::win_list, so holding msc::buf_mutex is expected from
1048 * Return: page if @pgoff corresponds to a valid buffer page or NULL.
1050 static struct page *msc_buffer_get_page(struct msc *msc, unsigned long pgoff)
1052 struct msc_window *win;
1054 if (msc->mode == MSC_MODE_SINGLE)
1055 return msc_buffer_contig_get_page(msc, pgoff);
1057 list_for_each_entry(win, &msc->win_list, entry)
1058 if (pgoff >= win->pgoff && pgoff < win->pgoff + win->nr_blocks)
1064 pgoff -= win->pgoff;
1065 return virt_to_page(win->block[pgoff].bdesc);
1069 * struct msc_win_to_user_struct - data for copy_to_user() callback
1070 * @buf: userspace buffer to copy data to
1071 * @offset: running offset
1073 struct msc_win_to_user_struct {
1075 unsigned long offset;
1079 * msc_win_to_user() - iterator for msc_buffer_iterate() to copy data to user
1080 * @data: callback's private data
1081 * @src: source buffer
1082 * @len: amount of data to copy from the source buffer
1084 static unsigned long msc_win_to_user(void *data, void *src, size_t len)
1086 struct msc_win_to_user_struct *u = data;
1089 ret = copy_to_user(u->buf + u->offset, src, len);
1090 u->offset += len - ret;
1097 * file operations' callbacks
1100 static int intel_th_msc_open(struct inode *inode, struct file *file)
1102 struct intel_th_device *thdev = file->private_data;
1103 struct msc *msc = dev_get_drvdata(&thdev->dev);
1104 struct msc_iter *iter;
1106 if (!capable(CAP_SYS_RAWIO))
1109 iter = msc_iter_install(msc);
1111 return PTR_ERR(iter);
1113 file->private_data = iter;
1115 return nonseekable_open(inode, file);
1118 static int intel_th_msc_release(struct inode *inode, struct file *file)
1120 struct msc_iter *iter = file->private_data;
1121 struct msc *msc = iter->msc;
1123 msc_iter_remove(iter, msc);
1129 msc_single_to_user(struct msc *msc, char __user *buf, loff_t off, size_t len)
1131 unsigned long size = msc->nr_pages << PAGE_SHIFT, rem = len;
1132 unsigned long start = off, tocopy = 0;
1134 if (msc->single_wrap) {
1135 start += msc->single_sz;
1137 tocopy = min(rem, size - start);
1138 if (copy_to_user(buf, msc->base + start, tocopy))
1148 tocopy = min(rem, msc->single_sz - start);
1149 if (copy_to_user(buf, msc->base + start, tocopy))
1158 if (copy_to_user(buf, msc->base + start, rem))
1164 static ssize_t intel_th_msc_read(struct file *file, char __user *buf,
1165 size_t len, loff_t *ppos)
1167 struct msc_iter *iter = file->private_data;
1168 struct msc *msc = iter->msc;
1173 if (!atomic_inc_unless_negative(&msc->user_count))
1176 if (msc->mode == MSC_MODE_SINGLE && !msc->single_wrap)
1177 size = msc->single_sz;
1179 size = msc->nr_pages << PAGE_SHIFT;
1187 if (off + len >= size)
1190 if (msc->mode == MSC_MODE_SINGLE) {
1191 ret = msc_single_to_user(msc, buf, off, len);
1194 } else if (msc->mode == MSC_MODE_MULTI) {
1195 struct msc_win_to_user_struct u = {
1200 ret = msc_buffer_iterate(iter, len, &u, msc_win_to_user);
1202 *ppos = iter->offset;
1208 atomic_dec(&msc->user_count);
1214 * vm operations callbacks (vm_ops)
1217 static void msc_mmap_open(struct vm_area_struct *vma)
1219 struct msc_iter *iter = vma->vm_file->private_data;
1220 struct msc *msc = iter->msc;
1222 atomic_inc(&msc->mmap_count);
1225 static void msc_mmap_close(struct vm_area_struct *vma)
1227 struct msc_iter *iter = vma->vm_file->private_data;
1228 struct msc *msc = iter->msc;
1231 if (!atomic_dec_and_mutex_lock(&msc->mmap_count, &msc->buf_mutex))
1234 /* drop page _refcounts */
1235 for (pg = 0; pg < msc->nr_pages; pg++) {
1236 struct page *page = msc_buffer_get_page(msc, pg);
1238 if (WARN_ON_ONCE(!page))
1242 page->mapping = NULL;
1245 /* last mapping -- drop user_count */
1246 atomic_dec(&msc->user_count);
1247 mutex_unlock(&msc->buf_mutex);
1250 static vm_fault_t msc_mmap_fault(struct vm_fault *vmf)
1252 struct msc_iter *iter = vmf->vma->vm_file->private_data;
1253 struct msc *msc = iter->msc;
1255 vmf->page = msc_buffer_get_page(msc, vmf->pgoff);
1257 return VM_FAULT_SIGBUS;
1259 get_page(vmf->page);
1260 vmf->page->mapping = vmf->vma->vm_file->f_mapping;
1261 vmf->page->index = vmf->pgoff;
1266 static const struct vm_operations_struct msc_mmap_ops = {
1267 .open = msc_mmap_open,
1268 .close = msc_mmap_close,
1269 .fault = msc_mmap_fault,
1272 static int intel_th_msc_mmap(struct file *file, struct vm_area_struct *vma)
1274 unsigned long size = vma->vm_end - vma->vm_start;
1275 struct msc_iter *iter = vma->vm_file->private_data;
1276 struct msc *msc = iter->msc;
1279 if (!size || offset_in_page(size))
1285 /* grab user_count once per mmap; drop in msc_mmap_close() */
1286 if (!atomic_inc_unless_negative(&msc->user_count))
1289 if (msc->mode != MSC_MODE_SINGLE &&
1290 msc->mode != MSC_MODE_MULTI)
1293 if (size >> PAGE_SHIFT != msc->nr_pages)
1296 atomic_set(&msc->mmap_count, 1);
1301 atomic_dec(&msc->user_count);
1303 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1304 vma->vm_flags |= VM_DONTEXPAND | VM_DONTCOPY;
1305 vma->vm_ops = &msc_mmap_ops;
1309 static const struct file_operations intel_th_msc_fops = {
1310 .open = intel_th_msc_open,
1311 .release = intel_th_msc_release,
1312 .read = intel_th_msc_read,
1313 .mmap = intel_th_msc_mmap,
1314 .llseek = no_llseek,
1315 .owner = THIS_MODULE,
1318 static int intel_th_msc_init(struct msc *msc)
1320 atomic_set(&msc->user_count, -1);
1322 msc->mode = MSC_MODE_MULTI;
1323 mutex_init(&msc->buf_mutex);
1324 INIT_LIST_HEAD(&msc->win_list);
1325 INIT_LIST_HEAD(&msc->iter_list);
1328 (ioread32(msc->reg_base + REG_MSU_MSC0CTL) & MSC_LEN) >>
1334 static irqreturn_t intel_th_msc_interrupt(struct intel_th_device *thdev)
1336 struct msc *msc = dev_get_drvdata(&thdev->dev);
1337 u32 msusts = ioread32(msc->msu_base + REG_MSU_MSUSTS);
1338 u32 mask = msc->index ? MSUSTS_MSC1BLAST : MSUSTS_MSC0BLAST;
1340 if (!(msusts & mask)) {
1349 static const char * const msc_mode[] = {
1350 [MSC_MODE_SINGLE] = "single",
1351 [MSC_MODE_MULTI] = "multi",
1352 [MSC_MODE_EXI] = "ExI",
1353 [MSC_MODE_DEBUG] = "debug",
1357 wrap_show(struct device *dev, struct device_attribute *attr, char *buf)
1359 struct msc *msc = dev_get_drvdata(dev);
1361 return scnprintf(buf, PAGE_SIZE, "%d\n", msc->wrap);
1365 wrap_store(struct device *dev, struct device_attribute *attr, const char *buf,
1368 struct msc *msc = dev_get_drvdata(dev);
1372 ret = kstrtoul(buf, 10, &val);
1381 static DEVICE_ATTR_RW(wrap);
1384 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1386 struct msc *msc = dev_get_drvdata(dev);
1388 return scnprintf(buf, PAGE_SIZE, "%s\n", msc_mode[msc->mode]);
1392 mode_store(struct device *dev, struct device_attribute *attr, const char *buf,
1395 struct msc *msc = dev_get_drvdata(dev);
1400 if (!capable(CAP_SYS_RAWIO))
1403 cp = memchr(buf, '\n', len);
1407 for (i = 0; i < ARRAY_SIZE(msc_mode); i++)
1408 if (!strncmp(msc_mode[i], buf, len))
1414 mutex_lock(&msc->buf_mutex);
1415 ret = msc_buffer_unlocked_free_unless_used(msc);
1418 mutex_unlock(&msc->buf_mutex);
1420 return ret ? ret : size;
1423 static DEVICE_ATTR_RW(mode);
1426 nr_pages_show(struct device *dev, struct device_attribute *attr, char *buf)
1428 struct msc *msc = dev_get_drvdata(dev);
1429 struct msc_window *win;
1432 mutex_lock(&msc->buf_mutex);
1434 if (msc->mode == MSC_MODE_SINGLE)
1435 count = scnprintf(buf, PAGE_SIZE, "%ld\n", msc->nr_pages);
1436 else if (msc->mode == MSC_MODE_MULTI) {
1437 list_for_each_entry(win, &msc->win_list, entry) {
1438 count += scnprintf(buf + count, PAGE_SIZE - count,
1439 "%d%c", win->nr_blocks,
1440 msc_is_last_win(win) ? '\n' : ',');
1443 count = scnprintf(buf, PAGE_SIZE, "unsupported\n");
1446 mutex_unlock(&msc->buf_mutex);
1452 nr_pages_store(struct device *dev, struct device_attribute *attr,
1453 const char *buf, size_t size)
1455 struct msc *msc = dev_get_drvdata(dev);
1456 unsigned long val, *win = NULL, *rewin;
1458 const char *p = buf;
1460 int ret, nr_wins = 0;
1462 if (!capable(CAP_SYS_RAWIO))
1465 ret = msc_buffer_free_unless_used(msc);
1469 /* scan the comma-separated list of allocation sizes */
1470 end = memchr(buf, '\n', len);
1475 end = memchr(p, ',', len);
1476 s = kstrndup(p, end ? end - p : len, GFP_KERNEL);
1482 ret = kstrtoul(s, 10, &val);
1488 if (nr_wins && msc->mode == MSC_MODE_SINGLE) {
1494 rewin = krealloc(win, sizeof(*win) * nr_wins, GFP_KERNEL);
1501 win[nr_wins - 1] = val;
1506 /* consume the number and the following comma, hence +1 */
1511 mutex_lock(&msc->buf_mutex);
1512 ret = msc_buffer_alloc(msc, win, nr_wins);
1513 mutex_unlock(&msc->buf_mutex);
1518 return ret ? ret : size;
1521 static DEVICE_ATTR_RW(nr_pages);
1523 static struct attribute *msc_output_attrs[] = {
1524 &dev_attr_wrap.attr,
1525 &dev_attr_mode.attr,
1526 &dev_attr_nr_pages.attr,
1530 static struct attribute_group msc_output_group = {
1531 .attrs = msc_output_attrs,
1534 static int intel_th_msc_probe(struct intel_th_device *thdev)
1536 struct device *dev = &thdev->dev;
1537 struct resource *res;
1542 res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
1546 base = devm_ioremap(dev, res->start, resource_size(res));
1550 msc = devm_kzalloc(dev, sizeof(*msc), GFP_KERNEL);
1554 res = intel_th_device_get_resource(thdev, IORESOURCE_IRQ, 1);
1558 msc->index = thdev->id;
1561 msc->reg_base = base + msc->index * 0x100;
1562 msc->msu_base = base;
1564 err = intel_th_msu_init(msc);
1568 err = intel_th_msc_init(msc);
1572 dev_set_drvdata(dev, msc);
1577 static void intel_th_msc_remove(struct intel_th_device *thdev)
1579 struct msc *msc = dev_get_drvdata(&thdev->dev);
1582 intel_th_msc_deactivate(thdev);
1583 intel_th_msu_deinit(msc);
1586 * Buffers should not be used at this point except if the
1587 * output character device is still open and the parent
1588 * device gets detached from its bus, which is a FIXME.
1590 ret = msc_buffer_free_unless_used(msc);
1594 static struct intel_th_driver intel_th_msc_driver = {
1595 .probe = intel_th_msc_probe,
1596 .remove = intel_th_msc_remove,
1597 .irq = intel_th_msc_interrupt,
1598 .activate = intel_th_msc_activate,
1599 .deactivate = intel_th_msc_deactivate,
1600 .fops = &intel_th_msc_fops,
1601 .attr_group = &msc_output_group,
1604 .owner = THIS_MODULE,
1608 module_driver(intel_th_msc_driver,
1609 intel_th_driver_register,
1610 intel_th_driver_unregister);
1612 MODULE_LICENSE("GPL v2");
1613 MODULE_DESCRIPTION("Intel(R) Trace Hub Memory Storage Unit driver");
1614 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");