]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/hwtracing/intel_th/msu.c
intel_th: msu: Add a sysfs attribute to trigger window switch
[linux.git] / drivers / hwtracing / intel_th / msu.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Intel(R) Trace Hub Memory Storage Unit
4  *
5  * Copyright (C) 2014-2015 Intel Corporation.
6  */
7
8 #define pr_fmt(fmt)     KBUILD_MODNAME ": " fmt
9
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>
17 #include <linux/mm.h>
18 #include <linux/fs.h>
19 #include <linux/io.h>
20 #include <linux/dma-mapping.h>
21
22 #ifdef CONFIG_X86
23 #include <asm/set_memory.h>
24 #endif
25
26 #include "intel_th.h"
27 #include "msu.h"
28
29 #define msc_dev(x) (&(x)->thdev->dev)
30
31 /**
32  * struct msc_window - multiblock mode window descriptor
33  * @entry:      window list linkage (msc::win_list)
34  * @pgoff:      page offset into the buffer that this window starts at
35  * @nr_blocks:  number of blocks (pages) in this window
36  * @sgt:        array of block descriptors
37  */
38 struct msc_window {
39         struct list_head        entry;
40         unsigned long           pgoff;
41         unsigned int            nr_blocks;
42         struct msc              *msc;
43         struct sg_table         sgt;
44 };
45
46 /**
47  * struct msc_iter - iterator for msc buffer
48  * @entry:              msc::iter_list linkage
49  * @msc:                pointer to the MSC device
50  * @start_win:          oldest window
51  * @win:                current window
52  * @offset:             current logical offset into the buffer
53  * @start_block:        oldest block in the window
54  * @block:              block number in the window
55  * @block_off:          offset into current block
56  * @wrap_count:         block wrapping handling
57  * @eof:                end of buffer reached
58  */
59 struct msc_iter {
60         struct list_head        entry;
61         struct msc              *msc;
62         struct msc_window       *start_win;
63         struct msc_window       *win;
64         unsigned long           offset;
65         int                     start_block;
66         int                     block;
67         unsigned int            block_off;
68         unsigned int            wrap_count;
69         unsigned int            eof;
70 };
71
72 /**
73  * struct msc - MSC device representation
74  * @reg_base:           register window base address
75  * @thdev:              intel_th_device pointer
76  * @win_list:           list of windows in multiblock mode
77  * @single_sgt:         single mode buffer
78  * @nr_pages:           total number of pages allocated for this buffer
79  * @single_sz:          amount of data in single mode
80  * @single_wrap:        single mode wrap occurred
81  * @base:               buffer's base pointer
82  * @base_addr:          buffer's base address
83  * @user_count:         number of users of the buffer
84  * @mmap_count:         number of mappings
85  * @buf_mutex:          mutex to serialize access to buffer-related bits
86
87  * @enabled:            MSC is enabled
88  * @wrap:               wrapping is enabled
89  * @mode:               MSC operating mode
90  * @burst_len:          write burst length
91  * @index:              number of this MSC in the MSU
92  */
93 struct msc {
94         void __iomem            *reg_base;
95         void __iomem            *msu_base;
96         struct intel_th_device  *thdev;
97
98         struct list_head        win_list;
99         struct sg_table         single_sgt;
100         unsigned long           nr_pages;
101         unsigned long           single_sz;
102         unsigned int            single_wrap : 1;
103         void                    *base;
104         dma_addr_t              base_addr;
105
106         /* <0: no buffer, 0: no users, >0: active users */
107         atomic_t                user_count;
108
109         atomic_t                mmap_count;
110         struct mutex            buf_mutex;
111
112         struct list_head        iter_list;
113
114         /* config */
115         unsigned int            enabled : 1,
116                                 wrap    : 1,
117                                 do_irq  : 1;
118         unsigned int            mode;
119         unsigned int            burst_len;
120         unsigned int            index;
121 };
122
123 static inline bool msc_block_is_empty(struct msc_block_desc *bdesc)
124 {
125         /* header hasn't been written */
126         if (!bdesc->valid_dw)
127                 return true;
128
129         /* valid_dw includes the header */
130         if (!msc_data_sz(bdesc))
131                 return true;
132
133         return false;
134 }
135
136 static inline struct msc_block_desc *
137 msc_win_block(struct msc_window *win, unsigned int block)
138 {
139         return sg_virt(&win->sgt.sgl[block]);
140 }
141
142 static inline dma_addr_t
143 msc_win_baddr(struct msc_window *win, unsigned int block)
144 {
145         return sg_dma_address(&win->sgt.sgl[block]);
146 }
147
148 static inline unsigned long
149 msc_win_bpfn(struct msc_window *win, unsigned int block)
150 {
151         return msc_win_baddr(win, block) >> PAGE_SHIFT;
152 }
153
154 /**
155  * msc_oldest_window() - locate the window with oldest data
156  * @msc:        MSC device
157  *
158  * This should only be used in multiblock mode. Caller should hold the
159  * msc::user_count reference.
160  *
161  * Return:      the oldest window with valid data
162  */
163 static struct msc_window *msc_oldest_window(struct msc *msc)
164 {
165         struct msc_window *win;
166         u32 reg = ioread32(msc->reg_base + REG_MSU_MSC0NWSA);
167         unsigned long win_addr = (unsigned long)reg << PAGE_SHIFT;
168         unsigned int found = 0;
169
170         if (list_empty(&msc->win_list))
171                 return NULL;
172
173         /*
174          * we might need a radix tree for this, depending on how
175          * many windows a typical user would allocate; ideally it's
176          * something like 2, in which case we're good
177          */
178         list_for_each_entry(win, &msc->win_list, entry) {
179                 if (sg_dma_address(win->sgt.sgl) == win_addr)
180                         found++;
181
182                 /* skip the empty ones */
183                 if (msc_block_is_empty(msc_win_block(win, 0)))
184                         continue;
185
186                 if (found)
187                         return win;
188         }
189
190         return list_first_entry(&msc->win_list, struct msc_window, entry);
191 }
192
193 /**
194  * msc_win_oldest_block() - locate the oldest block in a given window
195  * @win:        window to look at
196  *
197  * Return:      index of the block with the oldest data
198  */
199 static unsigned int msc_win_oldest_block(struct msc_window *win)
200 {
201         unsigned int blk;
202         struct msc_block_desc *bdesc = msc_win_block(win, 0);
203
204         /* without wrapping, first block is the oldest */
205         if (!msc_block_wrapped(bdesc))
206                 return 0;
207
208         /*
209          * with wrapping, last written block contains both the newest and the
210          * oldest data for this window.
211          */
212         for (blk = 0; blk < win->nr_blocks; blk++) {
213                 bdesc = msc_win_block(win, blk);
214
215                 if (msc_block_last_written(bdesc))
216                         return blk;
217         }
218
219         return 0;
220 }
221
222 /**
223  * msc_is_last_win() - check if a window is the last one for a given MSC
224  * @win:        window
225  * Return:      true if @win is the last window in MSC's multiblock buffer
226  */
227 static inline bool msc_is_last_win(struct msc_window *win)
228 {
229         return win->entry.next == &win->msc->win_list;
230 }
231
232 /**
233  * msc_next_window() - return next window in the multiblock buffer
234  * @win:        current window
235  *
236  * Return:      window following the current one
237  */
238 static struct msc_window *msc_next_window(struct msc_window *win)
239 {
240         if (msc_is_last_win(win))
241                 return list_first_entry(&win->msc->win_list, struct msc_window,
242                                         entry);
243
244         return list_next_entry(win, entry);
245 }
246
247 static struct msc_block_desc *msc_iter_bdesc(struct msc_iter *iter)
248 {
249         return msc_win_block(iter->win, iter->block);
250 }
251
252 static void msc_iter_init(struct msc_iter *iter)
253 {
254         memset(iter, 0, sizeof(*iter));
255         iter->start_block = -1;
256         iter->block = -1;
257 }
258
259 static struct msc_iter *msc_iter_install(struct msc *msc)
260 {
261         struct msc_iter *iter;
262
263         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
264         if (!iter)
265                 return ERR_PTR(-ENOMEM);
266
267         mutex_lock(&msc->buf_mutex);
268
269         /*
270          * Reading and tracing are mutually exclusive; if msc is
271          * enabled, open() will fail; otherwise existing readers
272          * will prevent enabling the msc and the rest of fops don't
273          * need to worry about it.
274          */
275         if (msc->enabled) {
276                 kfree(iter);
277                 iter = ERR_PTR(-EBUSY);
278                 goto unlock;
279         }
280
281         msc_iter_init(iter);
282         iter->msc = msc;
283
284         list_add_tail(&iter->entry, &msc->iter_list);
285 unlock:
286         mutex_unlock(&msc->buf_mutex);
287
288         return iter;
289 }
290
291 static void msc_iter_remove(struct msc_iter *iter, struct msc *msc)
292 {
293         mutex_lock(&msc->buf_mutex);
294         list_del(&iter->entry);
295         mutex_unlock(&msc->buf_mutex);
296
297         kfree(iter);
298 }
299
300 static void msc_iter_block_start(struct msc_iter *iter)
301 {
302         if (iter->start_block != -1)
303                 return;
304
305         iter->start_block = msc_win_oldest_block(iter->win);
306         iter->block = iter->start_block;
307         iter->wrap_count = 0;
308
309         /*
310          * start with the block with oldest data; if data has wrapped
311          * in this window, it should be in this block
312          */
313         if (msc_block_wrapped(msc_iter_bdesc(iter)))
314                 iter->wrap_count = 2;
315
316 }
317
318 static int msc_iter_win_start(struct msc_iter *iter, struct msc *msc)
319 {
320         /* already started, nothing to do */
321         if (iter->start_win)
322                 return 0;
323
324         iter->start_win = msc_oldest_window(msc);
325         if (!iter->start_win)
326                 return -EINVAL;
327
328         iter->win = iter->start_win;
329         iter->start_block = -1;
330
331         msc_iter_block_start(iter);
332
333         return 0;
334 }
335
336 static int msc_iter_win_advance(struct msc_iter *iter)
337 {
338         iter->win = msc_next_window(iter->win);
339         iter->start_block = -1;
340
341         if (iter->win == iter->start_win) {
342                 iter->eof++;
343                 return 1;
344         }
345
346         msc_iter_block_start(iter);
347
348         return 0;
349 }
350
351 static int msc_iter_block_advance(struct msc_iter *iter)
352 {
353         iter->block_off = 0;
354
355         /* wrapping */
356         if (iter->wrap_count && iter->block == iter->start_block) {
357                 iter->wrap_count--;
358                 if (!iter->wrap_count)
359                         /* copied newest data from the wrapped block */
360                         return msc_iter_win_advance(iter);
361         }
362
363         /* no wrapping, check for last written block */
364         if (!iter->wrap_count && msc_block_last_written(msc_iter_bdesc(iter)))
365                 /* copied newest data for the window */
366                 return msc_iter_win_advance(iter);
367
368         /* block advance */
369         if (++iter->block == iter->win->nr_blocks)
370                 iter->block = 0;
371
372         /* no wrapping, sanity check in case there is no last written block */
373         if (!iter->wrap_count && iter->block == iter->start_block)
374                 return msc_iter_win_advance(iter);
375
376         return 0;
377 }
378
379 /**
380  * msc_buffer_iterate() - go through multiblock buffer's data
381  * @iter:       iterator structure
382  * @size:       amount of data to scan
383  * @data:       callback's private data
384  * @fn:         iterator callback
385  *
386  * This will start at the window which will be written to next (containing
387  * the oldest data) and work its way to the current window, calling @fn
388  * for each chunk of data as it goes.
389  *
390  * Caller should have msc::user_count reference to make sure the buffer
391  * doesn't disappear from under us.
392  *
393  * Return:      amount of data actually scanned.
394  */
395 static ssize_t
396 msc_buffer_iterate(struct msc_iter *iter, size_t size, void *data,
397                    unsigned long (*fn)(void *, void *, size_t))
398 {
399         struct msc *msc = iter->msc;
400         size_t len = size;
401         unsigned int advance;
402
403         if (iter->eof)
404                 return 0;
405
406         /* start with the oldest window */
407         if (msc_iter_win_start(iter, msc))
408                 return 0;
409
410         do {
411                 unsigned long data_bytes = msc_data_sz(msc_iter_bdesc(iter));
412                 void *src = (void *)msc_iter_bdesc(iter) + MSC_BDESC;
413                 size_t tocopy = data_bytes, copied = 0;
414                 size_t remaining = 0;
415
416                 advance = 1;
417
418                 /*
419                  * If block wrapping happened, we need to visit the last block
420                  * twice, because it contains both the oldest and the newest
421                  * data in this window.
422                  *
423                  * First time (wrap_count==2), in the very beginning, to collect
424                  * the oldest data, which is in the range
425                  * (data_bytes..DATA_IN_PAGE).
426                  *
427                  * Second time (wrap_count==1), it's just like any other block,
428                  * containing data in the range of [MSC_BDESC..data_bytes].
429                  */
430                 if (iter->block == iter->start_block && iter->wrap_count == 2) {
431                         tocopy = DATA_IN_PAGE - data_bytes;
432                         src += data_bytes;
433                 }
434
435                 if (!tocopy)
436                         goto next_block;
437
438                 tocopy -= iter->block_off;
439                 src += iter->block_off;
440
441                 if (len < tocopy) {
442                         tocopy = len;
443                         advance = 0;
444                 }
445
446                 remaining = fn(data, src, tocopy);
447
448                 if (remaining)
449                         advance = 0;
450
451                 copied = tocopy - remaining;
452                 len -= copied;
453                 iter->block_off += copied;
454                 iter->offset += copied;
455
456                 if (!advance)
457                         break;
458
459 next_block:
460                 if (msc_iter_block_advance(iter))
461                         break;
462
463         } while (len);
464
465         return size - len;
466 }
467
468 /**
469  * msc_buffer_clear_hw_header() - clear hw header for multiblock
470  * @msc:        MSC device
471  */
472 static void msc_buffer_clear_hw_header(struct msc *msc)
473 {
474         struct msc_window *win;
475
476         list_for_each_entry(win, &msc->win_list, entry) {
477                 unsigned int blk;
478                 size_t hw_sz = sizeof(struct msc_block_desc) -
479                         offsetof(struct msc_block_desc, hw_tag);
480
481                 for (blk = 0; blk < win->nr_blocks; blk++) {
482                         struct msc_block_desc *bdesc = msc_win_block(win, blk);
483
484                         memset(&bdesc->hw_tag, 0, hw_sz);
485                 }
486         }
487 }
488
489 static int intel_th_msu_init(struct msc *msc)
490 {
491         u32 mintctl, msusts;
492
493         if (!msc->do_irq)
494                 return 0;
495
496         mintctl = ioread32(msc->msu_base + REG_MSU_MINTCTL);
497         mintctl |= msc->index ? M1BLIE : M0BLIE;
498         iowrite32(mintctl, msc->msu_base + REG_MSU_MINTCTL);
499         if (mintctl != ioread32(msc->msu_base + REG_MSU_MINTCTL)) {
500                 dev_info(msc_dev(msc), "MINTCTL ignores writes: no usable interrupts\n");
501                 msc->do_irq = 0;
502                 return 0;
503         }
504
505         msusts = ioread32(msc->msu_base + REG_MSU_MSUSTS);
506         iowrite32(msusts, msc->msu_base + REG_MSU_MSUSTS);
507
508         return 0;
509 }
510
511 static void intel_th_msu_deinit(struct msc *msc)
512 {
513         u32 mintctl;
514
515         if (!msc->do_irq)
516                 return;
517
518         mintctl = ioread32(msc->msu_base + REG_MSU_MINTCTL);
519         mintctl &= msc->index ? ~M1BLIE : ~M0BLIE;
520         iowrite32(mintctl, msc->msu_base + REG_MSU_MINTCTL);
521 }
522
523 /**
524  * msc_configure() - set up MSC hardware
525  * @msc:        the MSC device to configure
526  *
527  * Program storage mode, wrapping, burst length and trace buffer address
528  * into a given MSC. Then, enable tracing and set msc::enabled.
529  * The latter is serialized on msc::buf_mutex, so make sure to hold it.
530  */
531 static int msc_configure(struct msc *msc)
532 {
533         u32 reg;
534
535         lockdep_assert_held(&msc->buf_mutex);
536
537         if (msc->mode > MSC_MODE_MULTI)
538                 return -ENOTSUPP;
539
540         if (msc->mode == MSC_MODE_MULTI)
541                 msc_buffer_clear_hw_header(msc);
542
543         reg = msc->base_addr >> PAGE_SHIFT;
544         iowrite32(reg, msc->reg_base + REG_MSU_MSC0BAR);
545
546         if (msc->mode == MSC_MODE_SINGLE) {
547                 reg = msc->nr_pages;
548                 iowrite32(reg, msc->reg_base + REG_MSU_MSC0SIZE);
549         }
550
551         reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
552         reg &= ~(MSC_MODE | MSC_WRAPEN | MSC_EN | MSC_RD_HDR_OVRD);
553
554         reg |= MSC_EN;
555         reg |= msc->mode << __ffs(MSC_MODE);
556         reg |= msc->burst_len << __ffs(MSC_LEN);
557
558         if (msc->wrap)
559                 reg |= MSC_WRAPEN;
560
561         iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
562
563         msc->thdev->output.multiblock = msc->mode == MSC_MODE_MULTI;
564         intel_th_trace_enable(msc->thdev);
565         msc->enabled = 1;
566
567
568         return 0;
569 }
570
571 /**
572  * msc_disable() - disable MSC hardware
573  * @msc:        MSC device to disable
574  *
575  * If @msc is enabled, disable tracing on the switch and then disable MSC
576  * storage. Caller must hold msc::buf_mutex.
577  */
578 static void msc_disable(struct msc *msc)
579 {
580         u32 reg;
581
582         lockdep_assert_held(&msc->buf_mutex);
583
584         intel_th_trace_disable(msc->thdev);
585
586         if (msc->mode == MSC_MODE_SINGLE) {
587                 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
588                 msc->single_wrap = !!(reg & MSCSTS_WRAPSTAT);
589
590                 reg = ioread32(msc->reg_base + REG_MSU_MSC0MWP);
591                 msc->single_sz = reg & ((msc->nr_pages << PAGE_SHIFT) - 1);
592                 dev_dbg(msc_dev(msc), "MSCnMWP: %08x/%08lx, wrap: %d\n",
593                         reg, msc->single_sz, msc->single_wrap);
594         }
595
596         reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
597         reg &= ~MSC_EN;
598         iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
599         msc->enabled = 0;
600
601         iowrite32(0, msc->reg_base + REG_MSU_MSC0BAR);
602         iowrite32(0, msc->reg_base + REG_MSU_MSC0SIZE);
603
604         dev_dbg(msc_dev(msc), "MSCnNWSA: %08x\n",
605                 ioread32(msc->reg_base + REG_MSU_MSC0NWSA));
606
607         reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
608         dev_dbg(msc_dev(msc), "MSCnSTS: %08x\n", reg);
609 }
610
611 static int intel_th_msc_activate(struct intel_th_device *thdev)
612 {
613         struct msc *msc = dev_get_drvdata(&thdev->dev);
614         int ret = -EBUSY;
615
616         if (!atomic_inc_unless_negative(&msc->user_count))
617                 return -ENODEV;
618
619         mutex_lock(&msc->buf_mutex);
620
621         /* if there are readers, refuse */
622         if (list_empty(&msc->iter_list))
623                 ret = msc_configure(msc);
624
625         mutex_unlock(&msc->buf_mutex);
626
627         if (ret)
628                 atomic_dec(&msc->user_count);
629
630         return ret;
631 }
632
633 static void intel_th_msc_deactivate(struct intel_th_device *thdev)
634 {
635         struct msc *msc = dev_get_drvdata(&thdev->dev);
636
637         mutex_lock(&msc->buf_mutex);
638         if (msc->enabled) {
639                 msc_disable(msc);
640                 atomic_dec(&msc->user_count);
641         }
642         mutex_unlock(&msc->buf_mutex);
643 }
644
645 /**
646  * msc_buffer_contig_alloc() - allocate a contiguous buffer for SINGLE mode
647  * @msc:        MSC device
648  * @size:       allocation size in bytes
649  *
650  * This modifies msc::base, which requires msc::buf_mutex to serialize, so the
651  * caller is expected to hold it.
652  *
653  * Return:      0 on success, -errno otherwise.
654  */
655 static int msc_buffer_contig_alloc(struct msc *msc, unsigned long size)
656 {
657         unsigned long nr_pages = size >> PAGE_SHIFT;
658         unsigned int order = get_order(size);
659         struct page *page;
660         int ret;
661
662         if (!size)
663                 return 0;
664
665         ret = sg_alloc_table(&msc->single_sgt, 1, GFP_KERNEL);
666         if (ret)
667                 goto err_out;
668
669         ret = -ENOMEM;
670         page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
671         if (!page)
672                 goto err_free_sgt;
673
674         split_page(page, order);
675         sg_set_buf(msc->single_sgt.sgl, page_address(page), size);
676
677         ret = dma_map_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl, 1,
678                          DMA_FROM_DEVICE);
679         if (ret < 0)
680                 goto err_free_pages;
681
682         msc->nr_pages = nr_pages;
683         msc->base = page_address(page);
684         msc->base_addr = sg_dma_address(msc->single_sgt.sgl);
685
686         return 0;
687
688 err_free_pages:
689         __free_pages(page, order);
690
691 err_free_sgt:
692         sg_free_table(&msc->single_sgt);
693
694 err_out:
695         return ret;
696 }
697
698 /**
699  * msc_buffer_contig_free() - free a contiguous buffer
700  * @msc:        MSC configured in SINGLE mode
701  */
702 static void msc_buffer_contig_free(struct msc *msc)
703 {
704         unsigned long off;
705
706         dma_unmap_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl,
707                      1, DMA_FROM_DEVICE);
708         sg_free_table(&msc->single_sgt);
709
710         for (off = 0; off < msc->nr_pages << PAGE_SHIFT; off += PAGE_SIZE) {
711                 struct page *page = virt_to_page(msc->base + off);
712
713                 page->mapping = NULL;
714                 __free_page(page);
715         }
716
717         msc->nr_pages = 0;
718 }
719
720 /**
721  * msc_buffer_contig_get_page() - find a page at a given offset
722  * @msc:        MSC configured in SINGLE mode
723  * @pgoff:      page offset
724  *
725  * Return:      page, if @pgoff is within the range, NULL otherwise.
726  */
727 static struct page *msc_buffer_contig_get_page(struct msc *msc,
728                                                unsigned long pgoff)
729 {
730         if (pgoff >= msc->nr_pages)
731                 return NULL;
732
733         return virt_to_page(msc->base + (pgoff << PAGE_SHIFT));
734 }
735
736 static int __msc_buffer_win_alloc(struct msc_window *win,
737                                   unsigned int nr_blocks)
738 {
739         struct scatterlist *sg_ptr;
740         void *block;
741         int i, ret;
742
743         ret = sg_alloc_table(&win->sgt, nr_blocks, GFP_KERNEL);
744         if (ret)
745                 return -ENOMEM;
746
747         for_each_sg(win->sgt.sgl, sg_ptr, nr_blocks, i) {
748                 block = dma_alloc_coherent(msc_dev(win->msc)->parent->parent,
749                                           PAGE_SIZE, &sg_dma_address(sg_ptr),
750                                           GFP_KERNEL);
751                 if (!block)
752                         goto err_nomem;
753
754                 sg_set_buf(sg_ptr, block, PAGE_SIZE);
755         }
756
757         return nr_blocks;
758
759 err_nomem:
760         for (i--; i >= 0; i--)
761                 dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
762                                   msc_win_block(win, i),
763                                   msc_win_baddr(win, i));
764
765         sg_free_table(&win->sgt);
766
767         return -ENOMEM;
768 }
769
770 /**
771  * msc_buffer_win_alloc() - alloc a window for a multiblock mode
772  * @msc:        MSC device
773  * @nr_blocks:  number of pages in this window
774  *
775  * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
776  * to serialize, so the caller is expected to hold it.
777  *
778  * Return:      0 on success, -errno otherwise.
779  */
780 static int msc_buffer_win_alloc(struct msc *msc, unsigned int nr_blocks)
781 {
782         struct msc_window *win;
783         int ret = -ENOMEM, i;
784
785         if (!nr_blocks)
786                 return 0;
787
788         /*
789          * This limitation hold as long as we need random access to the
790          * block. When that changes, this can go away.
791          */
792         if (nr_blocks > SG_MAX_SINGLE_ALLOC)
793                 return -EINVAL;
794
795         win = kzalloc(sizeof(*win), GFP_KERNEL);
796         if (!win)
797                 return -ENOMEM;
798
799         win->msc = msc;
800
801         if (!list_empty(&msc->win_list)) {
802                 struct msc_window *prev = list_last_entry(&msc->win_list,
803                                                           struct msc_window,
804                                                           entry);
805
806                 /* This works as long as blocks are page-sized */
807                 win->pgoff = prev->pgoff + prev->nr_blocks;
808         }
809
810         ret = __msc_buffer_win_alloc(win, nr_blocks);
811         if (ret < 0)
812                 goto err_nomem;
813
814 #ifdef CONFIG_X86
815         for (i = 0; i < ret; i++)
816                 /* Set the page as uncached */
817                 set_memory_uc((unsigned long)msc_win_block(win, i), 1);
818 #endif
819
820         win->nr_blocks = ret;
821
822         if (list_empty(&msc->win_list)) {
823                 msc->base = msc_win_block(win, 0);
824                 msc->base_addr = msc_win_baddr(win, 0);
825         }
826
827         list_add_tail(&win->entry, &msc->win_list);
828         msc->nr_pages += nr_blocks;
829
830         return 0;
831
832 err_nomem:
833         kfree(win);
834
835         return ret;
836 }
837
838 static void __msc_buffer_win_free(struct msc *msc, struct msc_window *win)
839 {
840         int i;
841
842         for (i = 0; i < win->nr_blocks; i++) {
843                 struct page *page = sg_page(&win->sgt.sgl[i]);
844
845                 page->mapping = NULL;
846                 dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
847                                   msc_win_block(win, i), msc_win_baddr(win, i));
848         }
849         sg_free_table(&win->sgt);
850 }
851
852 /**
853  * msc_buffer_win_free() - free a window from MSC's window list
854  * @msc:        MSC device
855  * @win:        window to free
856  *
857  * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
858  * to serialize, so the caller is expected to hold it.
859  */
860 static void msc_buffer_win_free(struct msc *msc, struct msc_window *win)
861 {
862         int i;
863
864         msc->nr_pages -= win->nr_blocks;
865
866         list_del(&win->entry);
867         if (list_empty(&msc->win_list)) {
868                 msc->base = NULL;
869                 msc->base_addr = 0;
870         }
871
872 #ifdef CONFIG_X86
873         for (i = 0; i < win->nr_blocks; i++)
874                 /* Reset the page to write-back */
875                 set_memory_wb((unsigned long)msc_win_block(win, i), 1);
876 #endif
877
878         __msc_buffer_win_free(msc, win);
879
880         kfree(win);
881 }
882
883 /**
884  * msc_buffer_relink() - set up block descriptors for multiblock mode
885  * @msc:        MSC device
886  *
887  * This traverses msc::win_list, which requires msc::buf_mutex to serialize,
888  * so the caller is expected to hold it.
889  */
890 static void msc_buffer_relink(struct msc *msc)
891 {
892         struct msc_window *win, *next_win;
893
894         /* call with msc::mutex locked */
895         list_for_each_entry(win, &msc->win_list, entry) {
896                 unsigned int blk;
897                 u32 sw_tag = 0;
898
899                 /*
900                  * Last window's next_win should point to the first window
901                  * and MSC_SW_TAG_LASTWIN should be set.
902                  */
903                 if (msc_is_last_win(win)) {
904                         sw_tag |= MSC_SW_TAG_LASTWIN;
905                         next_win = list_first_entry(&msc->win_list,
906                                                     struct msc_window, entry);
907                 } else {
908                         next_win = list_next_entry(win, entry);
909                 }
910
911                 for (blk = 0; blk < win->nr_blocks; blk++) {
912                         struct msc_block_desc *bdesc = msc_win_block(win, blk);
913
914                         memset(bdesc, 0, sizeof(*bdesc));
915
916                         bdesc->next_win = msc_win_bpfn(next_win, 0);
917
918                         /*
919                          * Similarly to last window, last block should point
920                          * to the first one.
921                          */
922                         if (blk == win->nr_blocks - 1) {
923                                 sw_tag |= MSC_SW_TAG_LASTBLK;
924                                 bdesc->next_blk = msc_win_bpfn(win, 0);
925                         } else {
926                                 bdesc->next_blk = msc_win_bpfn(win, blk + 1);
927                         }
928
929                         bdesc->sw_tag = sw_tag;
930                         bdesc->block_sz = PAGE_SIZE / 64;
931                 }
932         }
933
934         /*
935          * Make the above writes globally visible before tracing is
936          * enabled to make sure hardware sees them coherently.
937          */
938         wmb();
939 }
940
941 static void msc_buffer_multi_free(struct msc *msc)
942 {
943         struct msc_window *win, *iter;
944
945         list_for_each_entry_safe(win, iter, &msc->win_list, entry)
946                 msc_buffer_win_free(msc, win);
947 }
948
949 static int msc_buffer_multi_alloc(struct msc *msc, unsigned long *nr_pages,
950                                   unsigned int nr_wins)
951 {
952         int ret, i;
953
954         for (i = 0; i < nr_wins; i++) {
955                 ret = msc_buffer_win_alloc(msc, nr_pages[i]);
956                 if (ret) {
957                         msc_buffer_multi_free(msc);
958                         return ret;
959                 }
960         }
961
962         msc_buffer_relink(msc);
963
964         return 0;
965 }
966
967 /**
968  * msc_buffer_free() - free buffers for MSC
969  * @msc:        MSC device
970  *
971  * Free MSC's storage buffers.
972  *
973  * This modifies msc::win_list and msc::base, which requires msc::buf_mutex to
974  * serialize, so the caller is expected to hold it.
975  */
976 static void msc_buffer_free(struct msc *msc)
977 {
978         if (msc->mode == MSC_MODE_SINGLE)
979                 msc_buffer_contig_free(msc);
980         else if (msc->mode == MSC_MODE_MULTI)
981                 msc_buffer_multi_free(msc);
982 }
983
984 /**
985  * msc_buffer_alloc() - allocate a buffer for MSC
986  * @msc:        MSC device
987  * @size:       allocation size in bytes
988  *
989  * Allocate a storage buffer for MSC, depending on the msc::mode, it will be
990  * either done via msc_buffer_contig_alloc() for SINGLE operation mode or
991  * msc_buffer_win_alloc() for multiblock operation. The latter allocates one
992  * window per invocation, so in multiblock mode this can be called multiple
993  * times for the same MSC to allocate multiple windows.
994  *
995  * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
996  * to serialize, so the caller is expected to hold it.
997  *
998  * Return:      0 on success, -errno otherwise.
999  */
1000 static int msc_buffer_alloc(struct msc *msc, unsigned long *nr_pages,
1001                             unsigned int nr_wins)
1002 {
1003         int ret;
1004
1005         /* -1: buffer not allocated */
1006         if (atomic_read(&msc->user_count) != -1)
1007                 return -EBUSY;
1008
1009         if (msc->mode == MSC_MODE_SINGLE) {
1010                 if (nr_wins != 1)
1011                         return -EINVAL;
1012
1013                 ret = msc_buffer_contig_alloc(msc, nr_pages[0] << PAGE_SHIFT);
1014         } else if (msc->mode == MSC_MODE_MULTI) {
1015                 ret = msc_buffer_multi_alloc(msc, nr_pages, nr_wins);
1016         } else {
1017                 ret = -ENOTSUPP;
1018         }
1019
1020         if (!ret) {
1021                 /* allocation should be visible before the counter goes to 0 */
1022                 smp_mb__before_atomic();
1023
1024                 if (WARN_ON_ONCE(atomic_cmpxchg(&msc->user_count, -1, 0) != -1))
1025                         return -EINVAL;
1026         }
1027
1028         return ret;
1029 }
1030
1031 /**
1032  * msc_buffer_unlocked_free_unless_used() - free a buffer unless it's in use
1033  * @msc:        MSC device
1034  *
1035  * This will free MSC buffer unless it is in use or there is no allocated
1036  * buffer.
1037  * Caller needs to hold msc::buf_mutex.
1038  *
1039  * Return:      0 on successful deallocation or if there was no buffer to
1040  *              deallocate, -EBUSY if there are active users.
1041  */
1042 static int msc_buffer_unlocked_free_unless_used(struct msc *msc)
1043 {
1044         int count, ret = 0;
1045
1046         count = atomic_cmpxchg(&msc->user_count, 0, -1);
1047
1048         /* > 0: buffer is allocated and has users */
1049         if (count > 0)
1050                 ret = -EBUSY;
1051         /* 0: buffer is allocated, no users */
1052         else if (!count)
1053                 msc_buffer_free(msc);
1054         /* < 0: no buffer, nothing to do */
1055
1056         return ret;
1057 }
1058
1059 /**
1060  * msc_buffer_free_unless_used() - free a buffer unless it's in use
1061  * @msc:        MSC device
1062  *
1063  * This is a locked version of msc_buffer_unlocked_free_unless_used().
1064  */
1065 static int msc_buffer_free_unless_used(struct msc *msc)
1066 {
1067         int ret;
1068
1069         mutex_lock(&msc->buf_mutex);
1070         ret = msc_buffer_unlocked_free_unless_used(msc);
1071         mutex_unlock(&msc->buf_mutex);
1072
1073         return ret;
1074 }
1075
1076 /**
1077  * msc_buffer_get_page() - get MSC buffer page at a given offset
1078  * @msc:        MSC device
1079  * @pgoff:      page offset into the storage buffer
1080  *
1081  * This traverses msc::win_list, so holding msc::buf_mutex is expected from
1082  * the caller.
1083  *
1084  * Return:      page if @pgoff corresponds to a valid buffer page or NULL.
1085  */
1086 static struct page *msc_buffer_get_page(struct msc *msc, unsigned long pgoff)
1087 {
1088         struct msc_window *win;
1089
1090         if (msc->mode == MSC_MODE_SINGLE)
1091                 return msc_buffer_contig_get_page(msc, pgoff);
1092
1093         list_for_each_entry(win, &msc->win_list, entry)
1094                 if (pgoff >= win->pgoff && pgoff < win->pgoff + win->nr_blocks)
1095                         goto found;
1096
1097         return NULL;
1098
1099 found:
1100         pgoff -= win->pgoff;
1101         return sg_page(&win->sgt.sgl[pgoff]);
1102 }
1103
1104 /**
1105  * struct msc_win_to_user_struct - data for copy_to_user() callback
1106  * @buf:        userspace buffer to copy data to
1107  * @offset:     running offset
1108  */
1109 struct msc_win_to_user_struct {
1110         char __user     *buf;
1111         unsigned long   offset;
1112 };
1113
1114 /**
1115  * msc_win_to_user() - iterator for msc_buffer_iterate() to copy data to user
1116  * @data:       callback's private data
1117  * @src:        source buffer
1118  * @len:        amount of data to copy from the source buffer
1119  */
1120 static unsigned long msc_win_to_user(void *data, void *src, size_t len)
1121 {
1122         struct msc_win_to_user_struct *u = data;
1123         unsigned long ret;
1124
1125         ret = copy_to_user(u->buf + u->offset, src, len);
1126         u->offset += len - ret;
1127
1128         return ret;
1129 }
1130
1131
1132 /*
1133  * file operations' callbacks
1134  */
1135
1136 static int intel_th_msc_open(struct inode *inode, struct file *file)
1137 {
1138         struct intel_th_device *thdev = file->private_data;
1139         struct msc *msc = dev_get_drvdata(&thdev->dev);
1140         struct msc_iter *iter;
1141
1142         if (!capable(CAP_SYS_RAWIO))
1143                 return -EPERM;
1144
1145         iter = msc_iter_install(msc);
1146         if (IS_ERR(iter))
1147                 return PTR_ERR(iter);
1148
1149         file->private_data = iter;
1150
1151         return nonseekable_open(inode, file);
1152 }
1153
1154 static int intel_th_msc_release(struct inode *inode, struct file *file)
1155 {
1156         struct msc_iter *iter = file->private_data;
1157         struct msc *msc = iter->msc;
1158
1159         msc_iter_remove(iter, msc);
1160
1161         return 0;
1162 }
1163
1164 static ssize_t
1165 msc_single_to_user(struct msc *msc, char __user *buf, loff_t off, size_t len)
1166 {
1167         unsigned long size = msc->nr_pages << PAGE_SHIFT, rem = len;
1168         unsigned long start = off, tocopy = 0;
1169
1170         if (msc->single_wrap) {
1171                 start += msc->single_sz;
1172                 if (start < size) {
1173                         tocopy = min(rem, size - start);
1174                         if (copy_to_user(buf, msc->base + start, tocopy))
1175                                 return -EFAULT;
1176
1177                         buf += tocopy;
1178                         rem -= tocopy;
1179                         start += tocopy;
1180                 }
1181
1182                 start &= size - 1;
1183                 if (rem) {
1184                         tocopy = min(rem, msc->single_sz - start);
1185                         if (copy_to_user(buf, msc->base + start, tocopy))
1186                                 return -EFAULT;
1187
1188                         rem -= tocopy;
1189                 }
1190
1191                 return len - rem;
1192         }
1193
1194         if (copy_to_user(buf, msc->base + start, rem))
1195                 return -EFAULT;
1196
1197         return len;
1198 }
1199
1200 static ssize_t intel_th_msc_read(struct file *file, char __user *buf,
1201                                  size_t len, loff_t *ppos)
1202 {
1203         struct msc_iter *iter = file->private_data;
1204         struct msc *msc = iter->msc;
1205         size_t size;
1206         loff_t off = *ppos;
1207         ssize_t ret = 0;
1208
1209         if (!atomic_inc_unless_negative(&msc->user_count))
1210                 return 0;
1211
1212         if (msc->mode == MSC_MODE_SINGLE && !msc->single_wrap)
1213                 size = msc->single_sz;
1214         else
1215                 size = msc->nr_pages << PAGE_SHIFT;
1216
1217         if (!size)
1218                 goto put_count;
1219
1220         if (off >= size)
1221                 goto put_count;
1222
1223         if (off + len >= size)
1224                 len = size - off;
1225
1226         if (msc->mode == MSC_MODE_SINGLE) {
1227                 ret = msc_single_to_user(msc, buf, off, len);
1228                 if (ret >= 0)
1229                         *ppos += ret;
1230         } else if (msc->mode == MSC_MODE_MULTI) {
1231                 struct msc_win_to_user_struct u = {
1232                         .buf    = buf,
1233                         .offset = 0,
1234                 };
1235
1236                 ret = msc_buffer_iterate(iter, len, &u, msc_win_to_user);
1237                 if (ret >= 0)
1238                         *ppos = iter->offset;
1239         } else {
1240                 ret = -ENOTSUPP;
1241         }
1242
1243 put_count:
1244         atomic_dec(&msc->user_count);
1245
1246         return ret;
1247 }
1248
1249 /*
1250  * vm operations callbacks (vm_ops)
1251  */
1252
1253 static void msc_mmap_open(struct vm_area_struct *vma)
1254 {
1255         struct msc_iter *iter = vma->vm_file->private_data;
1256         struct msc *msc = iter->msc;
1257
1258         atomic_inc(&msc->mmap_count);
1259 }
1260
1261 static void msc_mmap_close(struct vm_area_struct *vma)
1262 {
1263         struct msc_iter *iter = vma->vm_file->private_data;
1264         struct msc *msc = iter->msc;
1265         unsigned long pg;
1266
1267         if (!atomic_dec_and_mutex_lock(&msc->mmap_count, &msc->buf_mutex))
1268                 return;
1269
1270         /* drop page _refcounts */
1271         for (pg = 0; pg < msc->nr_pages; pg++) {
1272                 struct page *page = msc_buffer_get_page(msc, pg);
1273
1274                 if (WARN_ON_ONCE(!page))
1275                         continue;
1276
1277                 if (page->mapping)
1278                         page->mapping = NULL;
1279         }
1280
1281         /* last mapping -- drop user_count */
1282         atomic_dec(&msc->user_count);
1283         mutex_unlock(&msc->buf_mutex);
1284 }
1285
1286 static vm_fault_t msc_mmap_fault(struct vm_fault *vmf)
1287 {
1288         struct msc_iter *iter = vmf->vma->vm_file->private_data;
1289         struct msc *msc = iter->msc;
1290
1291         vmf->page = msc_buffer_get_page(msc, vmf->pgoff);
1292         if (!vmf->page)
1293                 return VM_FAULT_SIGBUS;
1294
1295         get_page(vmf->page);
1296         vmf->page->mapping = vmf->vma->vm_file->f_mapping;
1297         vmf->page->index = vmf->pgoff;
1298
1299         return 0;
1300 }
1301
1302 static const struct vm_operations_struct msc_mmap_ops = {
1303         .open   = msc_mmap_open,
1304         .close  = msc_mmap_close,
1305         .fault  = msc_mmap_fault,
1306 };
1307
1308 static int intel_th_msc_mmap(struct file *file, struct vm_area_struct *vma)
1309 {
1310         unsigned long size = vma->vm_end - vma->vm_start;
1311         struct msc_iter *iter = vma->vm_file->private_data;
1312         struct msc *msc = iter->msc;
1313         int ret = -EINVAL;
1314
1315         if (!size || offset_in_page(size))
1316                 return -EINVAL;
1317
1318         if (vma->vm_pgoff)
1319                 return -EINVAL;
1320
1321         /* grab user_count once per mmap; drop in msc_mmap_close() */
1322         if (!atomic_inc_unless_negative(&msc->user_count))
1323                 return -EINVAL;
1324
1325         if (msc->mode != MSC_MODE_SINGLE &&
1326             msc->mode != MSC_MODE_MULTI)
1327                 goto out;
1328
1329         if (size >> PAGE_SHIFT != msc->nr_pages)
1330                 goto out;
1331
1332         atomic_set(&msc->mmap_count, 1);
1333         ret = 0;
1334
1335 out:
1336         if (ret)
1337                 atomic_dec(&msc->user_count);
1338
1339         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1340         vma->vm_flags |= VM_DONTEXPAND | VM_DONTCOPY;
1341         vma->vm_ops = &msc_mmap_ops;
1342         return ret;
1343 }
1344
1345 static const struct file_operations intel_th_msc_fops = {
1346         .open           = intel_th_msc_open,
1347         .release        = intel_th_msc_release,
1348         .read           = intel_th_msc_read,
1349         .mmap           = intel_th_msc_mmap,
1350         .llseek         = no_llseek,
1351         .owner          = THIS_MODULE,
1352 };
1353
1354 static void intel_th_msc_wait_empty(struct intel_th_device *thdev)
1355 {
1356         struct msc *msc = dev_get_drvdata(&thdev->dev);
1357         unsigned long count;
1358         u32 reg;
1359
1360         for (reg = 0, count = MSC_PLE_WAITLOOP_DEPTH;
1361              count && !(reg & MSCSTS_PLE); count--) {
1362                 reg = __raw_readl(msc->reg_base + REG_MSU_MSC0STS);
1363                 cpu_relax();
1364         }
1365
1366         if (!count)
1367                 dev_dbg(msc_dev(msc), "timeout waiting for MSC0 PLE\n");
1368 }
1369
1370 static int intel_th_msc_init(struct msc *msc)
1371 {
1372         atomic_set(&msc->user_count, -1);
1373
1374         msc->mode = MSC_MODE_MULTI;
1375         mutex_init(&msc->buf_mutex);
1376         INIT_LIST_HEAD(&msc->win_list);
1377         INIT_LIST_HEAD(&msc->iter_list);
1378
1379         msc->burst_len =
1380                 (ioread32(msc->reg_base + REG_MSU_MSC0CTL) & MSC_LEN) >>
1381                 __ffs(MSC_LEN);
1382
1383         return 0;
1384 }
1385
1386 static irqreturn_t intel_th_msc_interrupt(struct intel_th_device *thdev)
1387 {
1388         struct msc *msc = dev_get_drvdata(&thdev->dev);
1389         u32 msusts = ioread32(msc->msu_base + REG_MSU_MSUSTS);
1390         u32 mask = msc->index ? MSUSTS_MSC1BLAST : MSUSTS_MSC0BLAST;
1391
1392         if (!(msusts & mask)) {
1393                 if (msc->enabled)
1394                         return IRQ_HANDLED;
1395                 return IRQ_NONE;
1396         }
1397
1398         return IRQ_HANDLED;
1399 }
1400
1401 static const char * const msc_mode[] = {
1402         [MSC_MODE_SINGLE]       = "single",
1403         [MSC_MODE_MULTI]        = "multi",
1404         [MSC_MODE_EXI]          = "ExI",
1405         [MSC_MODE_DEBUG]        = "debug",
1406 };
1407
1408 static ssize_t
1409 wrap_show(struct device *dev, struct device_attribute *attr, char *buf)
1410 {
1411         struct msc *msc = dev_get_drvdata(dev);
1412
1413         return scnprintf(buf, PAGE_SIZE, "%d\n", msc->wrap);
1414 }
1415
1416 static ssize_t
1417 wrap_store(struct device *dev, struct device_attribute *attr, const char *buf,
1418            size_t size)
1419 {
1420         struct msc *msc = dev_get_drvdata(dev);
1421         unsigned long val;
1422         int ret;
1423
1424         ret = kstrtoul(buf, 10, &val);
1425         if (ret)
1426                 return ret;
1427
1428         msc->wrap = !!val;
1429
1430         return size;
1431 }
1432
1433 static DEVICE_ATTR_RW(wrap);
1434
1435 static ssize_t
1436 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1437 {
1438         struct msc *msc = dev_get_drvdata(dev);
1439
1440         return scnprintf(buf, PAGE_SIZE, "%s\n", msc_mode[msc->mode]);
1441 }
1442
1443 static ssize_t
1444 mode_store(struct device *dev, struct device_attribute *attr, const char *buf,
1445            size_t size)
1446 {
1447         struct msc *msc = dev_get_drvdata(dev);
1448         size_t len = size;
1449         char *cp;
1450         int i, ret;
1451
1452         if (!capable(CAP_SYS_RAWIO))
1453                 return -EPERM;
1454
1455         cp = memchr(buf, '\n', len);
1456         if (cp)
1457                 len = cp - buf;
1458
1459         for (i = 0; i < ARRAY_SIZE(msc_mode); i++)
1460                 if (!strncmp(msc_mode[i], buf, len))
1461                         goto found;
1462
1463         return -EINVAL;
1464
1465 found:
1466         mutex_lock(&msc->buf_mutex);
1467         ret = msc_buffer_unlocked_free_unless_used(msc);
1468         if (!ret)
1469                 msc->mode = i;
1470         mutex_unlock(&msc->buf_mutex);
1471
1472         return ret ? ret : size;
1473 }
1474
1475 static DEVICE_ATTR_RW(mode);
1476
1477 static ssize_t
1478 nr_pages_show(struct device *dev, struct device_attribute *attr, char *buf)
1479 {
1480         struct msc *msc = dev_get_drvdata(dev);
1481         struct msc_window *win;
1482         size_t count = 0;
1483
1484         mutex_lock(&msc->buf_mutex);
1485
1486         if (msc->mode == MSC_MODE_SINGLE)
1487                 count = scnprintf(buf, PAGE_SIZE, "%ld\n", msc->nr_pages);
1488         else if (msc->mode == MSC_MODE_MULTI) {
1489                 list_for_each_entry(win, &msc->win_list, entry) {
1490                         count += scnprintf(buf + count, PAGE_SIZE - count,
1491                                            "%d%c", win->nr_blocks,
1492                                            msc_is_last_win(win) ? '\n' : ',');
1493                 }
1494         } else {
1495                 count = scnprintf(buf, PAGE_SIZE, "unsupported\n");
1496         }
1497
1498         mutex_unlock(&msc->buf_mutex);
1499
1500         return count;
1501 }
1502
1503 static ssize_t
1504 nr_pages_store(struct device *dev, struct device_attribute *attr,
1505                const char *buf, size_t size)
1506 {
1507         struct msc *msc = dev_get_drvdata(dev);
1508         unsigned long val, *win = NULL, *rewin;
1509         size_t len = size;
1510         const char *p = buf;
1511         char *end, *s;
1512         int ret, nr_wins = 0;
1513
1514         if (!capable(CAP_SYS_RAWIO))
1515                 return -EPERM;
1516
1517         ret = msc_buffer_free_unless_used(msc);
1518         if (ret)
1519                 return ret;
1520
1521         /* scan the comma-separated list of allocation sizes */
1522         end = memchr(buf, '\n', len);
1523         if (end)
1524                 len = end - buf;
1525
1526         do {
1527                 end = memchr(p, ',', len);
1528                 s = kstrndup(p, end ? end - p : len, GFP_KERNEL);
1529                 if (!s) {
1530                         ret = -ENOMEM;
1531                         goto free_win;
1532                 }
1533
1534                 ret = kstrtoul(s, 10, &val);
1535                 kfree(s);
1536
1537                 if (ret || !val)
1538                         goto free_win;
1539
1540                 if (nr_wins && msc->mode == MSC_MODE_SINGLE) {
1541                         ret = -EINVAL;
1542                         goto free_win;
1543                 }
1544
1545                 nr_wins++;
1546                 rewin = krealloc(win, sizeof(*win) * nr_wins, GFP_KERNEL);
1547                 if (!rewin) {
1548                         kfree(win);
1549                         return -ENOMEM;
1550                 }
1551
1552                 win = rewin;
1553                 win[nr_wins - 1] = val;
1554
1555                 if (!end)
1556                         break;
1557
1558                 /* consume the number and the following comma, hence +1 */
1559                 len -= end - p + 1;
1560                 p = end + 1;
1561         } while (len);
1562
1563         mutex_lock(&msc->buf_mutex);
1564         ret = msc_buffer_alloc(msc, win, nr_wins);
1565         mutex_unlock(&msc->buf_mutex);
1566
1567 free_win:
1568         kfree(win);
1569
1570         return ret ? ret : size;
1571 }
1572
1573 static DEVICE_ATTR_RW(nr_pages);
1574
1575 static ssize_t
1576 win_switch_store(struct device *dev, struct device_attribute *attr,
1577                  const char *buf, size_t size)
1578 {
1579         struct msc *msc = dev_get_drvdata(dev);
1580         unsigned long val;
1581         int ret;
1582
1583         ret = kstrtoul(buf, 10, &val);
1584         if (ret)
1585                 return ret;
1586
1587         if (val != 1)
1588                 return -EINVAL;
1589
1590         mutex_lock(&msc->buf_mutex);
1591         if (msc->mode != MSC_MODE_MULTI)
1592                 ret = -ENOTSUPP;
1593         else
1594                 ret = intel_th_trace_switch(msc->thdev);
1595         mutex_unlock(&msc->buf_mutex);
1596
1597         return ret ? ret : size;
1598 }
1599
1600 static DEVICE_ATTR_WO(win_switch);
1601
1602 static struct attribute *msc_output_attrs[] = {
1603         &dev_attr_wrap.attr,
1604         &dev_attr_mode.attr,
1605         &dev_attr_nr_pages.attr,
1606         &dev_attr_win_switch.attr,
1607         NULL,
1608 };
1609
1610 static struct attribute_group msc_output_group = {
1611         .attrs  = msc_output_attrs,
1612 };
1613
1614 static int intel_th_msc_probe(struct intel_th_device *thdev)
1615 {
1616         struct device *dev = &thdev->dev;
1617         struct resource *res;
1618         struct msc *msc;
1619         void __iomem *base;
1620         int err;
1621
1622         res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
1623         if (!res)
1624                 return -ENODEV;
1625
1626         base = devm_ioremap(dev, res->start, resource_size(res));
1627         if (!base)
1628                 return -ENOMEM;
1629
1630         msc = devm_kzalloc(dev, sizeof(*msc), GFP_KERNEL);
1631         if (!msc)
1632                 return -ENOMEM;
1633
1634         res = intel_th_device_get_resource(thdev, IORESOURCE_IRQ, 1);
1635         if (!res)
1636                 msc->do_irq = 1;
1637
1638         msc->index = thdev->id;
1639
1640         msc->thdev = thdev;
1641         msc->reg_base = base + msc->index * 0x100;
1642         msc->msu_base = base;
1643
1644         err = intel_th_msu_init(msc);
1645         if (err)
1646                 return err;
1647
1648         err = intel_th_msc_init(msc);
1649         if (err)
1650                 return err;
1651
1652         dev_set_drvdata(dev, msc);
1653
1654         return 0;
1655 }
1656
1657 static void intel_th_msc_remove(struct intel_th_device *thdev)
1658 {
1659         struct msc *msc = dev_get_drvdata(&thdev->dev);
1660         int ret;
1661
1662         intel_th_msc_deactivate(thdev);
1663         intel_th_msu_deinit(msc);
1664
1665         /*
1666          * Buffers should not be used at this point except if the
1667          * output character device is still open and the parent
1668          * device gets detached from its bus, which is a FIXME.
1669          */
1670         ret = msc_buffer_free_unless_used(msc);
1671         WARN_ON_ONCE(ret);
1672 }
1673
1674 static struct intel_th_driver intel_th_msc_driver = {
1675         .probe  = intel_th_msc_probe,
1676         .remove = intel_th_msc_remove,
1677         .irq            = intel_th_msc_interrupt,
1678         .wait_empty     = intel_th_msc_wait_empty,
1679         .activate       = intel_th_msc_activate,
1680         .deactivate     = intel_th_msc_deactivate,
1681         .fops   = &intel_th_msc_fops,
1682         .attr_group     = &msc_output_group,
1683         .driver = {
1684                 .name   = "msc",
1685                 .owner  = THIS_MODULE,
1686         },
1687 };
1688
1689 module_driver(intel_th_msc_driver,
1690               intel_th_driver_register,
1691               intel_th_driver_unregister);
1692
1693 MODULE_LICENSE("GPL v2");
1694 MODULE_DESCRIPTION("Intel(R) Trace Hub Memory Storage Unit driver");
1695 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");