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 * @single_sgt: single mode buffer
96 * @nr_pages: total number of pages allocated for this buffer
97 * @single_sz: amount of data in single mode
98 * @single_wrap: single mode wrap occurred
99 * @base: buffer's base pointer
100 * @base_addr: buffer's base address
101 * @user_count: number of users of the buffer
102 * @mmap_count: number of mappings
103 * @buf_mutex: mutex to serialize access to buffer-related bits
105 * @enabled: MSC is enabled
106 * @wrap: wrapping is enabled
107 * @mode: MSC operating mode
108 * @burst_len: write burst length
109 * @index: number of this MSC in the MSU
112 void __iomem *reg_base;
113 struct intel_th_device *thdev;
115 struct list_head win_list;
116 struct sg_table single_sgt;
117 unsigned long nr_pages;
118 unsigned long single_sz;
119 unsigned int single_wrap : 1;
121 dma_addr_t base_addr;
123 /* <0: no buffer, 0: no users, >0: active users */
127 struct mutex buf_mutex;
129 struct list_head iter_list;
132 unsigned int enabled : 1,
135 unsigned int burst_len;
139 static inline bool msc_block_is_empty(struct msc_block_desc *bdesc)
141 /* header hasn't been written */
142 if (!bdesc->valid_dw)
145 /* valid_dw includes the header */
146 if (!msc_data_sz(bdesc))
153 * msc_oldest_window() - locate the window with oldest data
156 * This should only be used in multiblock mode. Caller should hold the
157 * msc::user_count reference.
159 * Return: the oldest window with valid data
161 static struct msc_window *msc_oldest_window(struct msc *msc)
163 struct msc_window *win;
164 u32 reg = ioread32(msc->reg_base + REG_MSU_MSC0NWSA);
165 unsigned long win_addr = (unsigned long)reg << PAGE_SHIFT;
166 unsigned int found = 0;
168 if (list_empty(&msc->win_list))
172 * we might need a radix tree for this, depending on how
173 * many windows a typical user would allocate; ideally it's
174 * something like 2, in which case we're good
176 list_for_each_entry(win, &msc->win_list, entry) {
177 if (win->block[0].addr == win_addr)
180 /* skip the empty ones */
181 if (msc_block_is_empty(win->block[0].bdesc))
188 return list_entry(msc->win_list.next, struct msc_window, entry);
192 * msc_win_oldest_block() - locate the oldest block in a given window
193 * @win: window to look at
195 * Return: index of the block with the oldest data
197 static unsigned int msc_win_oldest_block(struct msc_window *win)
200 struct msc_block_desc *bdesc = win->block[0].bdesc;
202 /* without wrapping, first block is the oldest */
203 if (!msc_block_wrapped(bdesc))
207 * with wrapping, last written block contains both the newest and the
208 * oldest data for this window.
210 for (blk = 0; blk < win->nr_blocks; blk++) {
211 bdesc = win->block[blk].bdesc;
213 if (msc_block_last_written(bdesc))
221 * msc_is_last_win() - check if a window is the last one for a given MSC
223 * Return: true if @win is the last window in MSC's multiblock buffer
225 static inline bool msc_is_last_win(struct msc_window *win)
227 return win->entry.next == &win->msc->win_list;
231 * msc_next_window() - return next window in the multiblock buffer
232 * @win: current window
234 * Return: window following the current one
236 static struct msc_window *msc_next_window(struct msc_window *win)
238 if (msc_is_last_win(win))
239 return list_entry(win->msc->win_list.next, struct msc_window,
242 return list_entry(win->entry.next, struct msc_window, entry);
245 static struct msc_block_desc *msc_iter_bdesc(struct msc_iter *iter)
247 return iter->win->block[iter->block].bdesc;
250 static void msc_iter_init(struct msc_iter *iter)
252 memset(iter, 0, sizeof(*iter));
253 iter->start_block = -1;
257 static struct msc_iter *msc_iter_install(struct msc *msc)
259 struct msc_iter *iter;
261 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
263 return ERR_PTR(-ENOMEM);
265 mutex_lock(&msc->buf_mutex);
268 * Reading and tracing are mutually exclusive; if msc is
269 * enabled, open() will fail; otherwise existing readers
270 * will prevent enabling the msc and the rest of fops don't
271 * need to worry about it.
275 iter = ERR_PTR(-EBUSY);
282 list_add_tail(&iter->entry, &msc->iter_list);
284 mutex_unlock(&msc->buf_mutex);
289 static void msc_iter_remove(struct msc_iter *iter, struct msc *msc)
291 mutex_lock(&msc->buf_mutex);
292 list_del(&iter->entry);
293 mutex_unlock(&msc->buf_mutex);
298 static void msc_iter_block_start(struct msc_iter *iter)
300 if (iter->start_block != -1)
303 iter->start_block = msc_win_oldest_block(iter->win);
304 iter->block = iter->start_block;
305 iter->wrap_count = 0;
308 * start with the block with oldest data; if data has wrapped
309 * in this window, it should be in this block
311 if (msc_block_wrapped(msc_iter_bdesc(iter)))
312 iter->wrap_count = 2;
316 static int msc_iter_win_start(struct msc_iter *iter, struct msc *msc)
318 /* already started, nothing to do */
322 iter->start_win = msc_oldest_window(msc);
323 if (!iter->start_win)
326 iter->win = iter->start_win;
327 iter->start_block = -1;
329 msc_iter_block_start(iter);
334 static int msc_iter_win_advance(struct msc_iter *iter)
336 iter->win = msc_next_window(iter->win);
337 iter->start_block = -1;
339 if (iter->win == iter->start_win) {
344 msc_iter_block_start(iter);
349 static int msc_iter_block_advance(struct msc_iter *iter)
354 if (iter->wrap_count && iter->block == iter->start_block) {
356 if (!iter->wrap_count)
357 /* copied newest data from the wrapped block */
358 return msc_iter_win_advance(iter);
361 /* no wrapping, check for last written block */
362 if (!iter->wrap_count && msc_block_last_written(msc_iter_bdesc(iter)))
363 /* copied newest data for the window */
364 return msc_iter_win_advance(iter);
367 if (++iter->block == iter->win->nr_blocks)
370 /* no wrapping, sanity check in case there is no last written block */
371 if (!iter->wrap_count && iter->block == iter->start_block)
372 return msc_iter_win_advance(iter);
378 * msc_buffer_iterate() - go through multiblock buffer's data
379 * @iter: iterator structure
380 * @size: amount of data to scan
381 * @data: callback's private data
382 * @fn: iterator callback
384 * This will start at the window which will be written to next (containing
385 * the oldest data) and work its way to the current window, calling @fn
386 * for each chunk of data as it goes.
388 * Caller should have msc::user_count reference to make sure the buffer
389 * doesn't disappear from under us.
391 * Return: amount of data actually scanned.
394 msc_buffer_iterate(struct msc_iter *iter, size_t size, void *data,
395 unsigned long (*fn)(void *, void *, size_t))
397 struct msc *msc = iter->msc;
399 unsigned int advance;
404 /* start with the oldest window */
405 if (msc_iter_win_start(iter, msc))
409 unsigned long data_bytes = msc_data_sz(msc_iter_bdesc(iter));
410 void *src = (void *)msc_iter_bdesc(iter) + MSC_BDESC;
411 size_t tocopy = data_bytes, copied = 0;
412 size_t remaining = 0;
417 * If block wrapping happened, we need to visit the last block
418 * twice, because it contains both the oldest and the newest
419 * data in this window.
421 * First time (wrap_count==2), in the very beginning, to collect
422 * the oldest data, which is in the range
423 * (data_bytes..DATA_IN_PAGE).
425 * Second time (wrap_count==1), it's just like any other block,
426 * containing data in the range of [MSC_BDESC..data_bytes].
428 if (iter->block == iter->start_block && iter->wrap_count == 2) {
429 tocopy = DATA_IN_PAGE - data_bytes;
436 tocopy -= iter->block_off;
437 src += iter->block_off;
444 remaining = fn(data, src, tocopy);
449 copied = tocopy - remaining;
451 iter->block_off += copied;
452 iter->offset += copied;
458 if (msc_iter_block_advance(iter))
467 * msc_buffer_clear_hw_header() - clear hw header for multiblock
470 static void msc_buffer_clear_hw_header(struct msc *msc)
472 struct msc_window *win;
474 list_for_each_entry(win, &msc->win_list, entry) {
476 size_t hw_sz = sizeof(struct msc_block_desc) -
477 offsetof(struct msc_block_desc, hw_tag);
479 for (blk = 0; blk < win->nr_blocks; blk++) {
480 struct msc_block_desc *bdesc = win->block[blk].bdesc;
482 memset(&bdesc->hw_tag, 0, hw_sz);
488 * msc_configure() - set up MSC hardware
489 * @msc: the MSC device to configure
491 * Program storage mode, wrapping, burst length and trace buffer address
492 * into a given MSC. Then, enable tracing and set msc::enabled.
493 * The latter is serialized on msc::buf_mutex, so make sure to hold it.
495 static int msc_configure(struct msc *msc)
499 lockdep_assert_held(&msc->buf_mutex);
501 if (msc->mode > MSC_MODE_MULTI)
504 if (msc->mode == MSC_MODE_MULTI)
505 msc_buffer_clear_hw_header(msc);
507 reg = msc->base_addr >> PAGE_SHIFT;
508 iowrite32(reg, msc->reg_base + REG_MSU_MSC0BAR);
510 if (msc->mode == MSC_MODE_SINGLE) {
512 iowrite32(reg, msc->reg_base + REG_MSU_MSC0SIZE);
515 reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
516 reg &= ~(MSC_MODE | MSC_WRAPEN | MSC_EN | MSC_RD_HDR_OVRD);
519 reg |= msc->mode << __ffs(MSC_MODE);
520 reg |= msc->burst_len << __ffs(MSC_LEN);
525 iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
527 msc->thdev->output.multiblock = msc->mode == MSC_MODE_MULTI;
528 intel_th_trace_enable(msc->thdev);
536 * msc_disable() - disable MSC hardware
537 * @msc: MSC device to disable
539 * If @msc is enabled, disable tracing on the switch and then disable MSC
540 * storage. Caller must hold msc::buf_mutex.
542 static void msc_disable(struct msc *msc)
547 lockdep_assert_held(&msc->buf_mutex);
549 intel_th_trace_disable(msc->thdev);
551 for (reg = 0, count = MSC_PLE_WAITLOOP_DEPTH;
552 count && !(reg & MSCSTS_PLE); count--) {
553 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
558 dev_dbg(msc_dev(msc), "timeout waiting for MSC0 PLE\n");
560 if (msc->mode == MSC_MODE_SINGLE) {
561 msc->single_wrap = !!(reg & MSCSTS_WRAPSTAT);
563 reg = ioread32(msc->reg_base + REG_MSU_MSC0MWP);
564 msc->single_sz = reg & ((msc->nr_pages << PAGE_SHIFT) - 1);
565 dev_dbg(msc_dev(msc), "MSCnMWP: %08x/%08lx, wrap: %d\n",
566 reg, msc->single_sz, msc->single_wrap);
569 reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
571 iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
574 iowrite32(0, msc->reg_base + REG_MSU_MSC0BAR);
575 iowrite32(0, msc->reg_base + REG_MSU_MSC0SIZE);
577 dev_dbg(msc_dev(msc), "MSCnNWSA: %08x\n",
578 ioread32(msc->reg_base + REG_MSU_MSC0NWSA));
580 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
581 dev_dbg(msc_dev(msc), "MSCnSTS: %08x\n", reg);
584 static int intel_th_msc_activate(struct intel_th_device *thdev)
586 struct msc *msc = dev_get_drvdata(&thdev->dev);
589 if (!atomic_inc_unless_negative(&msc->user_count))
592 mutex_lock(&msc->buf_mutex);
594 /* if there are readers, refuse */
595 if (list_empty(&msc->iter_list))
596 ret = msc_configure(msc);
598 mutex_unlock(&msc->buf_mutex);
601 atomic_dec(&msc->user_count);
606 static void intel_th_msc_deactivate(struct intel_th_device *thdev)
608 struct msc *msc = dev_get_drvdata(&thdev->dev);
610 mutex_lock(&msc->buf_mutex);
613 atomic_dec(&msc->user_count);
615 mutex_unlock(&msc->buf_mutex);
619 * msc_buffer_contig_alloc() - allocate a contiguous buffer for SINGLE mode
621 * @size: allocation size in bytes
623 * This modifies msc::base, which requires msc::buf_mutex to serialize, so the
624 * caller is expected to hold it.
626 * Return: 0 on success, -errno otherwise.
628 static int msc_buffer_contig_alloc(struct msc *msc, unsigned long size)
630 unsigned long nr_pages = size >> PAGE_SHIFT;
631 unsigned int order = get_order(size);
638 ret = sg_alloc_table(&msc->single_sgt, 1, GFP_KERNEL);
643 page = alloc_pages(GFP_KERNEL | __GFP_ZERO | GFP_DMA32, order);
647 split_page(page, order);
648 sg_set_buf(msc->single_sgt.sgl, page_address(page), size);
650 ret = dma_map_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl, 1,
655 msc->nr_pages = nr_pages;
656 msc->base = page_address(page);
657 msc->base_addr = sg_dma_address(msc->single_sgt.sgl);
662 __free_pages(page, order);
665 sg_free_table(&msc->single_sgt);
672 * msc_buffer_contig_free() - free a contiguous buffer
673 * @msc: MSC configured in SINGLE mode
675 static void msc_buffer_contig_free(struct msc *msc)
679 dma_unmap_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl,
681 sg_free_table(&msc->single_sgt);
683 for (off = 0; off < msc->nr_pages << PAGE_SHIFT; off += PAGE_SIZE) {
684 struct page *page = virt_to_page(msc->base + off);
686 page->mapping = NULL;
694 * msc_buffer_contig_get_page() - find a page at a given offset
695 * @msc: MSC configured in SINGLE mode
696 * @pgoff: page offset
698 * Return: page, if @pgoff is within the range, NULL otherwise.
700 static struct page *msc_buffer_contig_get_page(struct msc *msc,
703 if (pgoff >= msc->nr_pages)
706 return virt_to_page(msc->base + (pgoff << PAGE_SHIFT));
710 * msc_buffer_win_alloc() - alloc a window for a multiblock mode
712 * @nr_blocks: number of pages in this window
714 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
715 * to serialize, so the caller is expected to hold it.
717 * Return: 0 on success, -errno otherwise.
719 static int msc_buffer_win_alloc(struct msc *msc, unsigned int nr_blocks)
721 struct msc_window *win;
722 unsigned long size = PAGE_SIZE;
723 int i, ret = -ENOMEM;
728 win = kzalloc(offsetof(struct msc_window, block[nr_blocks]),
733 if (!list_empty(&msc->win_list)) {
734 struct msc_window *prev = list_entry(msc->win_list.prev,
735 struct msc_window, entry);
737 win->pgoff = prev->pgoff + prev->nr_blocks;
740 for (i = 0; i < nr_blocks; i++) {
741 win->block[i].bdesc =
742 dma_alloc_coherent(msc_dev(msc)->parent->parent, size,
743 &win->block[i].addr, GFP_KERNEL);
745 if (!win->block[i].bdesc)
749 /* Set the page as uncached */
750 set_memory_uc((unsigned long)win->block[i].bdesc, 1);
755 win->nr_blocks = nr_blocks;
757 if (list_empty(&msc->win_list)) {
758 msc->base = win->block[0].bdesc;
759 msc->base_addr = win->block[0].addr;
762 list_add_tail(&win->entry, &msc->win_list);
763 msc->nr_pages += nr_blocks;
768 for (i--; i >= 0; i--) {
770 /* Reset the page to write-back before releasing */
771 set_memory_wb((unsigned long)win->block[i].bdesc, 1);
773 dma_free_coherent(msc_dev(msc)->parent->parent, size,
774 win->block[i].bdesc, win->block[i].addr);
782 * msc_buffer_win_free() - free a window from MSC's window list
784 * @win: window to free
786 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
787 * to serialize, so the caller is expected to hold it.
789 static void msc_buffer_win_free(struct msc *msc, struct msc_window *win)
793 msc->nr_pages -= win->nr_blocks;
795 list_del(&win->entry);
796 if (list_empty(&msc->win_list)) {
801 for (i = 0; i < win->nr_blocks; i++) {
802 struct page *page = virt_to_page(win->block[i].bdesc);
804 page->mapping = NULL;
806 /* Reset the page to write-back before releasing */
807 set_memory_wb((unsigned long)win->block[i].bdesc, 1);
809 dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
810 win->block[i].bdesc, win->block[i].addr);
817 * msc_buffer_relink() - set up block descriptors for multiblock mode
820 * This traverses msc::win_list, which requires msc::buf_mutex to serialize,
821 * so the caller is expected to hold it.
823 static void msc_buffer_relink(struct msc *msc)
825 struct msc_window *win, *next_win;
827 /* call with msc::mutex locked */
828 list_for_each_entry(win, &msc->win_list, entry) {
833 * Last window's next_win should point to the first window
834 * and MSC_SW_TAG_LASTWIN should be set.
836 if (msc_is_last_win(win)) {
837 sw_tag |= MSC_SW_TAG_LASTWIN;
838 next_win = list_entry(msc->win_list.next,
839 struct msc_window, entry);
841 next_win = list_entry(win->entry.next,
842 struct msc_window, entry);
845 for (blk = 0; blk < win->nr_blocks; blk++) {
846 struct msc_block_desc *bdesc = win->block[blk].bdesc;
848 memset(bdesc, 0, sizeof(*bdesc));
850 bdesc->next_win = next_win->block[0].addr >> PAGE_SHIFT;
853 * Similarly to last window, last block should point
856 if (blk == win->nr_blocks - 1) {
857 sw_tag |= MSC_SW_TAG_LASTBLK;
859 win->block[0].addr >> PAGE_SHIFT;
862 win->block[blk + 1].addr >> PAGE_SHIFT;
865 bdesc->sw_tag = sw_tag;
866 bdesc->block_sz = PAGE_SIZE / 64;
871 * Make the above writes globally visible before tracing is
872 * enabled to make sure hardware sees them coherently.
877 static void msc_buffer_multi_free(struct msc *msc)
879 struct msc_window *win, *iter;
881 list_for_each_entry_safe(win, iter, &msc->win_list, entry)
882 msc_buffer_win_free(msc, win);
885 static int msc_buffer_multi_alloc(struct msc *msc, unsigned long *nr_pages,
886 unsigned int nr_wins)
890 for (i = 0; i < nr_wins; i++) {
891 ret = msc_buffer_win_alloc(msc, nr_pages[i]);
893 msc_buffer_multi_free(msc);
898 msc_buffer_relink(msc);
904 * msc_buffer_free() - free buffers for MSC
907 * Free MSC's storage buffers.
909 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex to
910 * serialize, so the caller is expected to hold it.
912 static void msc_buffer_free(struct msc *msc)
914 if (msc->mode == MSC_MODE_SINGLE)
915 msc_buffer_contig_free(msc);
916 else if (msc->mode == MSC_MODE_MULTI)
917 msc_buffer_multi_free(msc);
921 * msc_buffer_alloc() - allocate a buffer for MSC
923 * @size: allocation size in bytes
925 * Allocate a storage buffer for MSC, depending on the msc::mode, it will be
926 * either done via msc_buffer_contig_alloc() for SINGLE operation mode or
927 * msc_buffer_win_alloc() for multiblock operation. The latter allocates one
928 * window per invocation, so in multiblock mode this can be called multiple
929 * times for the same MSC to allocate multiple windows.
931 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
932 * to serialize, so the caller is expected to hold it.
934 * Return: 0 on success, -errno otherwise.
936 static int msc_buffer_alloc(struct msc *msc, unsigned long *nr_pages,
937 unsigned int nr_wins)
941 /* -1: buffer not allocated */
942 if (atomic_read(&msc->user_count) != -1)
945 if (msc->mode == MSC_MODE_SINGLE) {
949 ret = msc_buffer_contig_alloc(msc, nr_pages[0] << PAGE_SHIFT);
950 } else if (msc->mode == MSC_MODE_MULTI) {
951 ret = msc_buffer_multi_alloc(msc, nr_pages, nr_wins);
957 /* allocation should be visible before the counter goes to 0 */
958 smp_mb__before_atomic();
960 if (WARN_ON_ONCE(atomic_cmpxchg(&msc->user_count, -1, 0) != -1))
968 * msc_buffer_unlocked_free_unless_used() - free a buffer unless it's in use
971 * This will free MSC buffer unless it is in use or there is no allocated
973 * Caller needs to hold msc::buf_mutex.
975 * Return: 0 on successful deallocation or if there was no buffer to
976 * deallocate, -EBUSY if there are active users.
978 static int msc_buffer_unlocked_free_unless_used(struct msc *msc)
982 count = atomic_cmpxchg(&msc->user_count, 0, -1);
984 /* > 0: buffer is allocated and has users */
987 /* 0: buffer is allocated, no users */
989 msc_buffer_free(msc);
990 /* < 0: no buffer, nothing to do */
996 * msc_buffer_free_unless_used() - free a buffer unless it's in use
999 * This is a locked version of msc_buffer_unlocked_free_unless_used().
1001 static int msc_buffer_free_unless_used(struct msc *msc)
1005 mutex_lock(&msc->buf_mutex);
1006 ret = msc_buffer_unlocked_free_unless_used(msc);
1007 mutex_unlock(&msc->buf_mutex);
1013 * msc_buffer_get_page() - get MSC buffer page at a given offset
1015 * @pgoff: page offset into the storage buffer
1017 * This traverses msc::win_list, so holding msc::buf_mutex is expected from
1020 * Return: page if @pgoff corresponds to a valid buffer page or NULL.
1022 static struct page *msc_buffer_get_page(struct msc *msc, unsigned long pgoff)
1024 struct msc_window *win;
1026 if (msc->mode == MSC_MODE_SINGLE)
1027 return msc_buffer_contig_get_page(msc, pgoff);
1029 list_for_each_entry(win, &msc->win_list, entry)
1030 if (pgoff >= win->pgoff && pgoff < win->pgoff + win->nr_blocks)
1036 pgoff -= win->pgoff;
1037 return virt_to_page(win->block[pgoff].bdesc);
1041 * struct msc_win_to_user_struct - data for copy_to_user() callback
1042 * @buf: userspace buffer to copy data to
1043 * @offset: running offset
1045 struct msc_win_to_user_struct {
1047 unsigned long offset;
1051 * msc_win_to_user() - iterator for msc_buffer_iterate() to copy data to user
1052 * @data: callback's private data
1053 * @src: source buffer
1054 * @len: amount of data to copy from the source buffer
1056 static unsigned long msc_win_to_user(void *data, void *src, size_t len)
1058 struct msc_win_to_user_struct *u = data;
1061 ret = copy_to_user(u->buf + u->offset, src, len);
1062 u->offset += len - ret;
1069 * file operations' callbacks
1072 static int intel_th_msc_open(struct inode *inode, struct file *file)
1074 struct intel_th_device *thdev = file->private_data;
1075 struct msc *msc = dev_get_drvdata(&thdev->dev);
1076 struct msc_iter *iter;
1078 if (!capable(CAP_SYS_RAWIO))
1081 iter = msc_iter_install(msc);
1083 return PTR_ERR(iter);
1085 file->private_data = iter;
1087 return nonseekable_open(inode, file);
1090 static int intel_th_msc_release(struct inode *inode, struct file *file)
1092 struct msc_iter *iter = file->private_data;
1093 struct msc *msc = iter->msc;
1095 msc_iter_remove(iter, msc);
1101 msc_single_to_user(struct msc *msc, char __user *buf, loff_t off, size_t len)
1103 unsigned long size = msc->nr_pages << PAGE_SHIFT, rem = len;
1104 unsigned long start = off, tocopy = 0;
1106 if (msc->single_wrap) {
1107 start += msc->single_sz;
1109 tocopy = min(rem, size - start);
1110 if (copy_to_user(buf, msc->base + start, tocopy))
1120 tocopy = min(rem, msc->single_sz - start);
1121 if (copy_to_user(buf, msc->base + start, tocopy))
1130 if (copy_to_user(buf, msc->base + start, rem))
1136 static ssize_t intel_th_msc_read(struct file *file, char __user *buf,
1137 size_t len, loff_t *ppos)
1139 struct msc_iter *iter = file->private_data;
1140 struct msc *msc = iter->msc;
1145 if (!atomic_inc_unless_negative(&msc->user_count))
1148 if (msc->mode == MSC_MODE_SINGLE && !msc->single_wrap)
1149 size = msc->single_sz;
1151 size = msc->nr_pages << PAGE_SHIFT;
1159 if (off + len >= size)
1162 if (msc->mode == MSC_MODE_SINGLE) {
1163 ret = msc_single_to_user(msc, buf, off, len);
1166 } else if (msc->mode == MSC_MODE_MULTI) {
1167 struct msc_win_to_user_struct u = {
1172 ret = msc_buffer_iterate(iter, len, &u, msc_win_to_user);
1174 *ppos = iter->offset;
1180 atomic_dec(&msc->user_count);
1186 * vm operations callbacks (vm_ops)
1189 static void msc_mmap_open(struct vm_area_struct *vma)
1191 struct msc_iter *iter = vma->vm_file->private_data;
1192 struct msc *msc = iter->msc;
1194 atomic_inc(&msc->mmap_count);
1197 static void msc_mmap_close(struct vm_area_struct *vma)
1199 struct msc_iter *iter = vma->vm_file->private_data;
1200 struct msc *msc = iter->msc;
1203 if (!atomic_dec_and_mutex_lock(&msc->mmap_count, &msc->buf_mutex))
1206 /* drop page _refcounts */
1207 for (pg = 0; pg < msc->nr_pages; pg++) {
1208 struct page *page = msc_buffer_get_page(msc, pg);
1210 if (WARN_ON_ONCE(!page))
1214 page->mapping = NULL;
1217 /* last mapping -- drop user_count */
1218 atomic_dec(&msc->user_count);
1219 mutex_unlock(&msc->buf_mutex);
1222 static int msc_mmap_fault(struct vm_fault *vmf)
1224 struct msc_iter *iter = vmf->vma->vm_file->private_data;
1225 struct msc *msc = iter->msc;
1227 vmf->page = msc_buffer_get_page(msc, vmf->pgoff);
1229 return VM_FAULT_SIGBUS;
1231 get_page(vmf->page);
1232 vmf->page->mapping = vmf->vma->vm_file->f_mapping;
1233 vmf->page->index = vmf->pgoff;
1238 static const struct vm_operations_struct msc_mmap_ops = {
1239 .open = msc_mmap_open,
1240 .close = msc_mmap_close,
1241 .fault = msc_mmap_fault,
1244 static int intel_th_msc_mmap(struct file *file, struct vm_area_struct *vma)
1246 unsigned long size = vma->vm_end - vma->vm_start;
1247 struct msc_iter *iter = vma->vm_file->private_data;
1248 struct msc *msc = iter->msc;
1251 if (!size || offset_in_page(size))
1257 /* grab user_count once per mmap; drop in msc_mmap_close() */
1258 if (!atomic_inc_unless_negative(&msc->user_count))
1261 if (msc->mode != MSC_MODE_SINGLE &&
1262 msc->mode != MSC_MODE_MULTI)
1265 if (size >> PAGE_SHIFT != msc->nr_pages)
1268 atomic_set(&msc->mmap_count, 1);
1273 atomic_dec(&msc->user_count);
1275 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1276 vma->vm_flags |= VM_DONTEXPAND | VM_DONTCOPY;
1277 vma->vm_ops = &msc_mmap_ops;
1281 static const struct file_operations intel_th_msc_fops = {
1282 .open = intel_th_msc_open,
1283 .release = intel_th_msc_release,
1284 .read = intel_th_msc_read,
1285 .mmap = intel_th_msc_mmap,
1286 .llseek = no_llseek,
1287 .owner = THIS_MODULE,
1290 static int intel_th_msc_init(struct msc *msc)
1292 atomic_set(&msc->user_count, -1);
1294 msc->mode = MSC_MODE_MULTI;
1295 mutex_init(&msc->buf_mutex);
1296 INIT_LIST_HEAD(&msc->win_list);
1297 INIT_LIST_HEAD(&msc->iter_list);
1300 (ioread32(msc->reg_base + REG_MSU_MSC0CTL) & MSC_LEN) >>
1306 static const char * const msc_mode[] = {
1307 [MSC_MODE_SINGLE] = "single",
1308 [MSC_MODE_MULTI] = "multi",
1309 [MSC_MODE_EXI] = "ExI",
1310 [MSC_MODE_DEBUG] = "debug",
1314 wrap_show(struct device *dev, struct device_attribute *attr, char *buf)
1316 struct msc *msc = dev_get_drvdata(dev);
1318 return scnprintf(buf, PAGE_SIZE, "%d\n", msc->wrap);
1322 wrap_store(struct device *dev, struct device_attribute *attr, const char *buf,
1325 struct msc *msc = dev_get_drvdata(dev);
1329 ret = kstrtoul(buf, 10, &val);
1338 static DEVICE_ATTR_RW(wrap);
1341 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1343 struct msc *msc = dev_get_drvdata(dev);
1345 return scnprintf(buf, PAGE_SIZE, "%s\n", msc_mode[msc->mode]);
1349 mode_store(struct device *dev, struct device_attribute *attr, const char *buf,
1352 struct msc *msc = dev_get_drvdata(dev);
1357 if (!capable(CAP_SYS_RAWIO))
1360 cp = memchr(buf, '\n', len);
1364 for (i = 0; i < ARRAY_SIZE(msc_mode); i++)
1365 if (!strncmp(msc_mode[i], buf, len))
1371 mutex_lock(&msc->buf_mutex);
1372 ret = msc_buffer_unlocked_free_unless_used(msc);
1375 mutex_unlock(&msc->buf_mutex);
1377 return ret ? ret : size;
1380 static DEVICE_ATTR_RW(mode);
1383 nr_pages_show(struct device *dev, struct device_attribute *attr, char *buf)
1385 struct msc *msc = dev_get_drvdata(dev);
1386 struct msc_window *win;
1389 mutex_lock(&msc->buf_mutex);
1391 if (msc->mode == MSC_MODE_SINGLE)
1392 count = scnprintf(buf, PAGE_SIZE, "%ld\n", msc->nr_pages);
1393 else if (msc->mode == MSC_MODE_MULTI) {
1394 list_for_each_entry(win, &msc->win_list, entry) {
1395 count += scnprintf(buf + count, PAGE_SIZE - count,
1396 "%d%c", win->nr_blocks,
1397 msc_is_last_win(win) ? '\n' : ',');
1400 count = scnprintf(buf, PAGE_SIZE, "unsupported\n");
1403 mutex_unlock(&msc->buf_mutex);
1409 nr_pages_store(struct device *dev, struct device_attribute *attr,
1410 const char *buf, size_t size)
1412 struct msc *msc = dev_get_drvdata(dev);
1413 unsigned long val, *win = NULL, *rewin;
1415 const char *p = buf;
1417 int ret, nr_wins = 0;
1419 if (!capable(CAP_SYS_RAWIO))
1422 ret = msc_buffer_free_unless_used(msc);
1426 /* scan the comma-separated list of allocation sizes */
1427 end = memchr(buf, '\n', len);
1432 end = memchr(p, ',', len);
1433 s = kstrndup(p, end ? end - p : len, GFP_KERNEL);
1439 ret = kstrtoul(s, 10, &val);
1445 if (nr_wins && msc->mode == MSC_MODE_SINGLE) {
1451 rewin = krealloc(win, sizeof(*win) * nr_wins, GFP_KERNEL);
1458 win[nr_wins - 1] = val;
1463 /* consume the number and the following comma, hence +1 */
1468 mutex_lock(&msc->buf_mutex);
1469 ret = msc_buffer_alloc(msc, win, nr_wins);
1470 mutex_unlock(&msc->buf_mutex);
1475 return ret ? ret : size;
1478 static DEVICE_ATTR_RW(nr_pages);
1480 static struct attribute *msc_output_attrs[] = {
1481 &dev_attr_wrap.attr,
1482 &dev_attr_mode.attr,
1483 &dev_attr_nr_pages.attr,
1487 static struct attribute_group msc_output_group = {
1488 .attrs = msc_output_attrs,
1491 static int intel_th_msc_probe(struct intel_th_device *thdev)
1493 struct device *dev = &thdev->dev;
1494 struct resource *res;
1499 res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
1503 base = devm_ioremap(dev, res->start, resource_size(res));
1507 msc = devm_kzalloc(dev, sizeof(*msc), GFP_KERNEL);
1511 msc->index = thdev->id;
1514 msc->reg_base = base + msc->index * 0x100;
1516 err = intel_th_msc_init(msc);
1520 dev_set_drvdata(dev, msc);
1525 static void intel_th_msc_remove(struct intel_th_device *thdev)
1527 struct msc *msc = dev_get_drvdata(&thdev->dev);
1530 intel_th_msc_deactivate(thdev);
1533 * Buffers should not be used at this point except if the
1534 * output character device is still open and the parent
1535 * device gets detached from its bus, which is a FIXME.
1537 ret = msc_buffer_free_unless_used(msc);
1541 static struct intel_th_driver intel_th_msc_driver = {
1542 .probe = intel_th_msc_probe,
1543 .remove = intel_th_msc_remove,
1544 .activate = intel_th_msc_activate,
1545 .deactivate = intel_th_msc_deactivate,
1546 .fops = &intel_th_msc_fops,
1547 .attr_group = &msc_output_group,
1550 .owner = THIS_MODULE,
1554 module_driver(intel_th_msc_driver,
1555 intel_th_driver_register,
1556 intel_th_driver_unregister);
1558 MODULE_LICENSE("GPL v2");
1559 MODULE_DESCRIPTION("Intel(R) Trace Hub Memory Storage Unit driver");
1560 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");