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