blob: 0e7fbef8dee00813d363bf9cf07536c69ac89423 [file] [log] [blame]
Alexander Shishkin50352fa2018-03-28 18:46:15 +03001// SPDX-License-Identifier: GPL-2.0
Alexander Shishkinba826642015-09-22 15:47:18 +03002/*
3 * Intel(R) Trace Hub Memory Storage Unit
4 *
5 * Copyright (C) 2014-2015 Intel Corporation.
Alexander Shishkinba826642015-09-22 15:47:18 +03006 */
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
Laura Abbott0c14dac2017-05-08 15:58:20 -070022#ifdef CONFIG_X86
23#include <asm/set_memory.h>
24#endif
Alexander Shishkinba826642015-09-22 15:47:18 +030025
26#include "intel_th.h"
27#include "msu.h"
28
29#define msc_dev(x) (&(x)->thdev->dev)
30
31/**
Alexander Shishkinba826642015-09-22 15:47:18 +030032 * 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
Alexander Shishkinba39bd82019-05-03 11:44:44 +030036 * @sgt: array of block descriptors
Alexander Shishkinba826642015-09-22 15:47:18 +030037 */
38struct msc_window {
39 struct list_head entry;
40 unsigned long pgoff;
41 unsigned int nr_blocks;
42 struct msc *msc;
Alexander Shishkinba39bd82019-05-03 11:44:44 +030043 struct sg_table sgt;
Alexander Shishkinba826642015-09-22 15:47:18 +030044};
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 */
59struct 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
Alexander Shishkin4e0eaf22019-05-03 11:44:34 +030077 * @single_sgt: single mode buffer
Alexander Shishkinba826642015-09-22 15:47:18 +030078 * @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 */
93struct msc {
94 void __iomem *reg_base;
Alexander Shishkinaac8da62019-05-03 11:44:41 +030095 void __iomem *msu_base;
Alexander Shishkinba826642015-09-22 15:47:18 +030096 struct intel_th_device *thdev;
97
98 struct list_head win_list;
Alexander Shishkin4e0eaf22019-05-03 11:44:34 +030099 struct sg_table single_sgt;
Alexander Shishkinba826642015-09-22 15:47:18 +0300100 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
Alexander Shishkinba826642015-09-22 15:47:18 +0300112 struct list_head iter_list;
113
114 /* config */
115 unsigned int enabled : 1,
Alexander Shishkinaac8da62019-05-03 11:44:41 +0300116 wrap : 1,
117 do_irq : 1;
Alexander Shishkinba826642015-09-22 15:47:18 +0300118 unsigned int mode;
119 unsigned int burst_len;
120 unsigned int index;
121};
122
123static 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
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300136static inline struct msc_block_desc *
137msc_win_block(struct msc_window *win, unsigned int block)
138{
139 return sg_virt(&win->sgt.sgl[block]);
140}
141
142static inline dma_addr_t
143msc_win_baddr(struct msc_window *win, unsigned int block)
144{
145 return sg_dma_address(&win->sgt.sgl[block]);
146}
147
148static inline unsigned long
149msc_win_bpfn(struct msc_window *win, unsigned int block)
150{
151 return msc_win_baddr(win, block) >> PAGE_SHIFT;
152}
153
Alexander Shishkinba826642015-09-22 15:47:18 +0300154/**
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 */
163static 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) {
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300179 if (sg_dma_address(win->sgt.sgl) == win_addr)
Alexander Shishkinba826642015-09-22 15:47:18 +0300180 found++;
181
182 /* skip the empty ones */
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300183 if (msc_block_is_empty(msc_win_block(win, 0)))
Alexander Shishkinba826642015-09-22 15:47:18 +0300184 continue;
185
186 if (found)
187 return win;
188 }
189
Alexander Shishkin0de9e032019-05-03 11:44:43 +0300190 return list_first_entry(&msc->win_list, struct msc_window, entry);
Alexander Shishkinba826642015-09-22 15:47:18 +0300191}
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 */
199static unsigned int msc_win_oldest_block(struct msc_window *win)
200{
201 unsigned int blk;
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300202 struct msc_block_desc *bdesc = msc_win_block(win, 0);
Alexander Shishkinba826642015-09-22 15:47:18 +0300203
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++) {
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300213 bdesc = msc_win_block(win, blk);
Alexander Shishkinba826642015-09-22 15:47:18 +0300214
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 */
227static 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 */
238static struct msc_window *msc_next_window(struct msc_window *win)
239{
240 if (msc_is_last_win(win))
Alexander Shishkin0de9e032019-05-03 11:44:43 +0300241 return list_first_entry(&win->msc->win_list, struct msc_window,
242 entry);
Alexander Shishkinba826642015-09-22 15:47:18 +0300243
Alexander Shishkin0de9e032019-05-03 11:44:43 +0300244 return list_next_entry(win, entry);
Alexander Shishkinba826642015-09-22 15:47:18 +0300245}
246
247static struct msc_block_desc *msc_iter_bdesc(struct msc_iter *iter)
248{
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300249 return msc_win_block(iter->win, iter->block);
Alexander Shishkinba826642015-09-22 15:47:18 +0300250}
251
252static 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
259static 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)
Alexander Shishkina45ff6e2016-03-10 18:21:14 +0200265 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 }
Alexander Shishkinba826642015-09-22 15:47:18 +0300280
281 msc_iter_init(iter);
282 iter->msc = msc;
283
Alexander Shishkinba826642015-09-22 15:47:18 +0300284 list_add_tail(&iter->entry, &msc->iter_list);
Alexander Shishkina45ff6e2016-03-10 18:21:14 +0200285unlock:
286 mutex_unlock(&msc->buf_mutex);
Alexander Shishkinba826642015-09-22 15:47:18 +0300287
288 return iter;
289}
290
291static void msc_iter_remove(struct msc_iter *iter, struct msc *msc)
292{
Alexander Shishkina45ff6e2016-03-10 18:21:14 +0200293 mutex_lock(&msc->buf_mutex);
Alexander Shishkinba826642015-09-22 15:47:18 +0300294 list_del(&iter->entry);
Alexander Shishkina45ff6e2016-03-10 18:21:14 +0200295 mutex_unlock(&msc->buf_mutex);
Alexander Shishkinba826642015-09-22 15:47:18 +0300296
297 kfree(iter);
298}
299
300static 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
318static 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
336static 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
351static 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 */
395static ssize_t
396msc_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 */
Laurent FERTe4eca2a2016-02-15 19:11:56 +0200430 if (iter->block == iter->start_block && iter->wrap_count == 2) {
Alexander Shishkinba826642015-09-22 15:47:18 +0300431 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
459next_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 */
472static void msc_buffer_clear_hw_header(struct msc *msc)
473{
474 struct msc_window *win;
475
Alexander Shishkinba826642015-09-22 15:47:18 +0300476 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++) {
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300482 struct msc_block_desc *bdesc = msc_win_block(win, blk);
Alexander Shishkinba826642015-09-22 15:47:18 +0300483
484 memset(&bdesc->hw_tag, 0, hw_sz);
485 }
486 }
Alexander Shishkinba826642015-09-22 15:47:18 +0300487}
488
Alexander Shishkinaac8da62019-05-03 11:44:41 +0300489static 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
511static 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
Alexander Shishkinba826642015-09-22 15:47:18 +0300523/**
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
Alexander Shishkina45ff6e2016-03-10 18:21:14 +0200528 * 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.
Alexander Shishkinba826642015-09-22 15:47:18 +0300530 */
531static int msc_configure(struct msc *msc)
532{
533 u32 reg;
534
Alexander Shishkina45ff6e2016-03-10 18:21:14 +0200535 lockdep_assert_held(&msc->buf_mutex);
536
Alexander Shishkinba826642015-09-22 15:47:18 +0300537 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
Alexander Shishkina45ff6e2016-03-10 18:21:14 +0200554 reg |= MSC_EN;
Alexander Shishkinba826642015-09-22 15:47:18 +0300555 reg |= msc->mode << __ffs(MSC_MODE);
556 reg |= msc->burst_len << __ffs(MSC_LEN);
Alexander Shishkina45ff6e2016-03-10 18:21:14 +0200557
Alexander Shishkinba826642015-09-22 15:47:18 +0300558 if (msc->wrap)
559 reg |= MSC_WRAPEN;
Alexander Shishkinba826642015-09-22 15:47:18 +0300560
561 iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
562
Alexander Shishkina45ff6e2016-03-10 18:21:14 +0200563 msc->thdev->output.multiblock = msc->mode == MSC_MODE_MULTI;
564 intel_th_trace_enable(msc->thdev);
565 msc->enabled = 1;
566
Alexander Shishkinba826642015-09-22 15:47:18 +0300567
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
Alexander Shishkina45ff6e2016-03-10 18:21:14 +0200576 * storage. Caller must hold msc::buf_mutex.
Alexander Shishkinba826642015-09-22 15:47:18 +0300577 */
578static void msc_disable(struct msc *msc)
579{
580 unsigned long count;
581 u32 reg;
582
Alexander Shishkina45ff6e2016-03-10 18:21:14 +0200583 lockdep_assert_held(&msc->buf_mutex);
Alexander Shishkinba826642015-09-22 15:47:18 +0300584
585 intel_th_trace_disable(msc->thdev);
586
587 for (reg = 0, count = MSC_PLE_WAITLOOP_DEPTH;
588 count && !(reg & MSCSTS_PLE); count--) {
589 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
590 cpu_relax();
591 }
592
593 if (!count)
594 dev_dbg(msc_dev(msc), "timeout waiting for MSC0 PLE\n");
595
596 if (msc->mode == MSC_MODE_SINGLE) {
597 msc->single_wrap = !!(reg & MSCSTS_WRAPSTAT);
598
599 reg = ioread32(msc->reg_base + REG_MSU_MSC0MWP);
600 msc->single_sz = reg & ((msc->nr_pages << PAGE_SHIFT) - 1);
601 dev_dbg(msc_dev(msc), "MSCnMWP: %08x/%08lx, wrap: %d\n",
602 reg, msc->single_sz, msc->single_wrap);
603 }
604
605 reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
606 reg &= ~MSC_EN;
607 iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
608 msc->enabled = 0;
609
610 iowrite32(0, msc->reg_base + REG_MSU_MSC0BAR);
611 iowrite32(0, msc->reg_base + REG_MSU_MSC0SIZE);
612
613 dev_dbg(msc_dev(msc), "MSCnNWSA: %08x\n",
614 ioread32(msc->reg_base + REG_MSU_MSC0NWSA));
615
616 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
617 dev_dbg(msc_dev(msc), "MSCnSTS: %08x\n", reg);
618}
619
620static int intel_th_msc_activate(struct intel_th_device *thdev)
621{
622 struct msc *msc = dev_get_drvdata(&thdev->dev);
Alexander Shishkina45ff6e2016-03-10 18:21:14 +0200623 int ret = -EBUSY;
Alexander Shishkinba826642015-09-22 15:47:18 +0300624
625 if (!atomic_inc_unless_negative(&msc->user_count))
626 return -ENODEV;
627
Alexander Shishkina45ff6e2016-03-10 18:21:14 +0200628 mutex_lock(&msc->buf_mutex);
Alexander Shishkinba826642015-09-22 15:47:18 +0300629
Alexander Shishkina45ff6e2016-03-10 18:21:14 +0200630 /* if there are readers, refuse */
631 if (list_empty(&msc->iter_list))
632 ret = msc_configure(msc);
633
634 mutex_unlock(&msc->buf_mutex);
635
636 if (ret)
Alexander Shishkinba826642015-09-22 15:47:18 +0300637 atomic_dec(&msc->user_count);
Alexander Shishkinba826642015-09-22 15:47:18 +0300638
Alexander Shishkina45ff6e2016-03-10 18:21:14 +0200639 return ret;
Alexander Shishkinba826642015-09-22 15:47:18 +0300640}
641
642static void intel_th_msc_deactivate(struct intel_th_device *thdev)
643{
644 struct msc *msc = dev_get_drvdata(&thdev->dev);
645
Alexander Shishkina45ff6e2016-03-10 18:21:14 +0200646 mutex_lock(&msc->buf_mutex);
647 if (msc->enabled) {
648 msc_disable(msc);
649 atomic_dec(&msc->user_count);
650 }
651 mutex_unlock(&msc->buf_mutex);
Alexander Shishkinba826642015-09-22 15:47:18 +0300652}
653
654/**
655 * msc_buffer_contig_alloc() - allocate a contiguous buffer for SINGLE mode
656 * @msc: MSC device
657 * @size: allocation size in bytes
658 *
659 * This modifies msc::base, which requires msc::buf_mutex to serialize, so the
660 * caller is expected to hold it.
661 *
662 * Return: 0 on success, -errno otherwise.
663 */
664static int msc_buffer_contig_alloc(struct msc *msc, unsigned long size)
665{
Alexander Shishkin4e0eaf22019-05-03 11:44:34 +0300666 unsigned long nr_pages = size >> PAGE_SHIFT;
Alexander Shishkinba826642015-09-22 15:47:18 +0300667 unsigned int order = get_order(size);
668 struct page *page;
Alexander Shishkin4e0eaf22019-05-03 11:44:34 +0300669 int ret;
Alexander Shishkinba826642015-09-22 15:47:18 +0300670
671 if (!size)
672 return 0;
673
Alexander Shishkin4e0eaf22019-05-03 11:44:34 +0300674 ret = sg_alloc_table(&msc->single_sgt, 1, GFP_KERNEL);
675 if (ret)
676 goto err_out;
677
678 ret = -ENOMEM;
Alexander Shishkinba826642015-09-22 15:47:18 +0300679 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
680 if (!page)
Alexander Shishkin4e0eaf22019-05-03 11:44:34 +0300681 goto err_free_sgt;
Alexander Shishkinba826642015-09-22 15:47:18 +0300682
683 split_page(page, order);
Alexander Shishkin4e0eaf22019-05-03 11:44:34 +0300684 sg_set_buf(msc->single_sgt.sgl, page_address(page), size);
685
686 ret = dma_map_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl, 1,
687 DMA_FROM_DEVICE);
688 if (ret < 0)
689 goto err_free_pages;
690
691 msc->nr_pages = nr_pages;
Alexander Shishkinba826642015-09-22 15:47:18 +0300692 msc->base = page_address(page);
Alexander Shishkin4e0eaf22019-05-03 11:44:34 +0300693 msc->base_addr = sg_dma_address(msc->single_sgt.sgl);
Alexander Shishkinba826642015-09-22 15:47:18 +0300694
695 return 0;
Alexander Shishkin4e0eaf22019-05-03 11:44:34 +0300696
697err_free_pages:
698 __free_pages(page, order);
699
700err_free_sgt:
701 sg_free_table(&msc->single_sgt);
702
703err_out:
704 return ret;
Alexander Shishkinba826642015-09-22 15:47:18 +0300705}
706
707/**
708 * msc_buffer_contig_free() - free a contiguous buffer
709 * @msc: MSC configured in SINGLE mode
710 */
711static void msc_buffer_contig_free(struct msc *msc)
712{
713 unsigned long off;
714
Alexander Shishkin4e0eaf22019-05-03 11:44:34 +0300715 dma_unmap_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl,
716 1, DMA_FROM_DEVICE);
717 sg_free_table(&msc->single_sgt);
718
Alexander Shishkinba826642015-09-22 15:47:18 +0300719 for (off = 0; off < msc->nr_pages << PAGE_SHIFT; off += PAGE_SIZE) {
720 struct page *page = virt_to_page(msc->base + off);
721
722 page->mapping = NULL;
723 __free_page(page);
724 }
725
726 msc->nr_pages = 0;
727}
728
729/**
730 * msc_buffer_contig_get_page() - find a page at a given offset
731 * @msc: MSC configured in SINGLE mode
732 * @pgoff: page offset
733 *
734 * Return: page, if @pgoff is within the range, NULL otherwise.
735 */
736static struct page *msc_buffer_contig_get_page(struct msc *msc,
737 unsigned long pgoff)
738{
739 if (pgoff >= msc->nr_pages)
740 return NULL;
741
742 return virt_to_page(msc->base + (pgoff << PAGE_SHIFT));
743}
744
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300745static int __msc_buffer_win_alloc(struct msc_window *win,
746 unsigned int nr_blocks)
747{
748 struct scatterlist *sg_ptr;
749 void *block;
750 int i, ret;
751
752 ret = sg_alloc_table(&win->sgt, nr_blocks, GFP_KERNEL);
753 if (ret)
754 return -ENOMEM;
755
756 for_each_sg(win->sgt.sgl, sg_ptr, nr_blocks, i) {
757 block = dma_alloc_coherent(msc_dev(win->msc)->parent->parent,
758 PAGE_SIZE, &sg_dma_address(sg_ptr),
759 GFP_KERNEL);
760 if (!block)
761 goto err_nomem;
762
763 sg_set_buf(sg_ptr, block, PAGE_SIZE);
764 }
765
766 return nr_blocks;
767
768err_nomem:
769 for (i--; i >= 0; i--)
770 dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
771 msc_win_block(win, i),
772 msc_win_baddr(win, i));
773
774 sg_free_table(&win->sgt);
775
776 return -ENOMEM;
777}
778
Alexander Shishkinba826642015-09-22 15:47:18 +0300779/**
780 * msc_buffer_win_alloc() - alloc a window for a multiblock mode
781 * @msc: MSC device
782 * @nr_blocks: number of pages in this window
783 *
784 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
785 * to serialize, so the caller is expected to hold it.
786 *
787 * Return: 0 on success, -errno otherwise.
788 */
789static int msc_buffer_win_alloc(struct msc *msc, unsigned int nr_blocks)
790{
791 struct msc_window *win;
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300792 int ret = -ENOMEM, i;
Alexander Shishkinba826642015-09-22 15:47:18 +0300793
794 if (!nr_blocks)
795 return 0;
796
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300797 /*
798 * This limitation hold as long as we need random access to the
799 * block. When that changes, this can go away.
800 */
801 if (nr_blocks > SG_MAX_SINGLE_ALLOC)
802 return -EINVAL;
803
804 win = kzalloc(sizeof(*win), GFP_KERNEL);
Alexander Shishkinba826642015-09-22 15:47:18 +0300805 if (!win)
806 return -ENOMEM;
807
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300808 win->msc = msc;
809
Alexander Shishkinba826642015-09-22 15:47:18 +0300810 if (!list_empty(&msc->win_list)) {
Alexander Shishkin0de9e032019-05-03 11:44:43 +0300811 struct msc_window *prev = list_last_entry(&msc->win_list,
812 struct msc_window,
813 entry);
Alexander Shishkinba826642015-09-22 15:47:18 +0300814
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300815 /* This works as long as blocks are page-sized */
Alexander Shishkinba826642015-09-22 15:47:18 +0300816 win->pgoff = prev->pgoff + prev->nr_blocks;
817 }
818
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300819 ret = __msc_buffer_win_alloc(win, nr_blocks);
820 if (ret < 0)
821 goto err_nomem;
Alexander Shishkinba826642015-09-22 15:47:18 +0300822
823#ifdef CONFIG_X86
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300824 for (i = 0; i < ret; i++)
Alexander Shishkinba826642015-09-22 15:47:18 +0300825 /* Set the page as uncached */
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300826 set_memory_uc((unsigned long)msc_win_block(win, i), 1);
Alexander Shishkinba826642015-09-22 15:47:18 +0300827#endif
Alexander Shishkinba826642015-09-22 15:47:18 +0300828
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300829 win->nr_blocks = ret;
Alexander Shishkinba826642015-09-22 15:47:18 +0300830
831 if (list_empty(&msc->win_list)) {
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300832 msc->base = msc_win_block(win, 0);
833 msc->base_addr = msc_win_baddr(win, 0);
Alexander Shishkinba826642015-09-22 15:47:18 +0300834 }
835
836 list_add_tail(&win->entry, &msc->win_list);
837 msc->nr_pages += nr_blocks;
838
839 return 0;
840
841err_nomem:
Alexander Shishkinba826642015-09-22 15:47:18 +0300842 kfree(win);
843
844 return ret;
845}
846
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300847static void __msc_buffer_win_free(struct msc *msc, struct msc_window *win)
848{
849 int i;
850
851 for (i = 0; i < win->nr_blocks; i++) {
852 struct page *page = sg_page(&win->sgt.sgl[i]);
853
854 page->mapping = NULL;
855 dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
856 msc_win_block(win, i), msc_win_baddr(win, i));
857 }
858 sg_free_table(&win->sgt);
859}
860
Alexander Shishkinba826642015-09-22 15:47:18 +0300861/**
862 * msc_buffer_win_free() - free a window from MSC's window list
863 * @msc: MSC device
864 * @win: window to free
865 *
866 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
867 * to serialize, so the caller is expected to hold it.
868 */
869static void msc_buffer_win_free(struct msc *msc, struct msc_window *win)
870{
871 int i;
872
873 msc->nr_pages -= win->nr_blocks;
874
875 list_del(&win->entry);
876 if (list_empty(&msc->win_list)) {
877 msc->base = NULL;
878 msc->base_addr = 0;
879 }
880
Alexander Shishkinba826642015-09-22 15:47:18 +0300881#ifdef CONFIG_X86
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300882 for (i = 0; i < win->nr_blocks; i++)
883 /* Reset the page to write-back */
884 set_memory_wb((unsigned long)msc_win_block(win, i), 1);
Alexander Shishkinba826642015-09-22 15:47:18 +0300885#endif
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300886
887 __msc_buffer_win_free(msc, win);
Alexander Shishkinba826642015-09-22 15:47:18 +0300888
889 kfree(win);
890}
891
892/**
893 * msc_buffer_relink() - set up block descriptors for multiblock mode
894 * @msc: MSC device
895 *
896 * This traverses msc::win_list, which requires msc::buf_mutex to serialize,
897 * so the caller is expected to hold it.
898 */
899static void msc_buffer_relink(struct msc *msc)
900{
901 struct msc_window *win, *next_win;
902
903 /* call with msc::mutex locked */
904 list_for_each_entry(win, &msc->win_list, entry) {
905 unsigned int blk;
906 u32 sw_tag = 0;
907
908 /*
909 * Last window's next_win should point to the first window
910 * and MSC_SW_TAG_LASTWIN should be set.
911 */
912 if (msc_is_last_win(win)) {
913 sw_tag |= MSC_SW_TAG_LASTWIN;
Alexander Shishkin0de9e032019-05-03 11:44:43 +0300914 next_win = list_first_entry(&msc->win_list,
915 struct msc_window, entry);
Alexander Shishkinba826642015-09-22 15:47:18 +0300916 } else {
Alexander Shishkin0de9e032019-05-03 11:44:43 +0300917 next_win = list_next_entry(win, entry);
Alexander Shishkinba826642015-09-22 15:47:18 +0300918 }
919
920 for (blk = 0; blk < win->nr_blocks; blk++) {
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300921 struct msc_block_desc *bdesc = msc_win_block(win, blk);
Alexander Shishkinba826642015-09-22 15:47:18 +0300922
923 memset(bdesc, 0, sizeof(*bdesc));
924
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300925 bdesc->next_win = msc_win_bpfn(next_win, 0);
Alexander Shishkinba826642015-09-22 15:47:18 +0300926
927 /*
928 * Similarly to last window, last block should point
929 * to the first one.
930 */
931 if (blk == win->nr_blocks - 1) {
932 sw_tag |= MSC_SW_TAG_LASTBLK;
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300933 bdesc->next_blk = msc_win_bpfn(win, 0);
Alexander Shishkinba826642015-09-22 15:47:18 +0300934 } else {
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300935 bdesc->next_blk = msc_win_bpfn(win, blk + 1);
Alexander Shishkinba826642015-09-22 15:47:18 +0300936 }
937
938 bdesc->sw_tag = sw_tag;
939 bdesc->block_sz = PAGE_SIZE / 64;
940 }
941 }
942
943 /*
944 * Make the above writes globally visible before tracing is
945 * enabled to make sure hardware sees them coherently.
946 */
947 wmb();
948}
949
950static void msc_buffer_multi_free(struct msc *msc)
951{
952 struct msc_window *win, *iter;
953
954 list_for_each_entry_safe(win, iter, &msc->win_list, entry)
955 msc_buffer_win_free(msc, win);
956}
957
958static int msc_buffer_multi_alloc(struct msc *msc, unsigned long *nr_pages,
959 unsigned int nr_wins)
960{
961 int ret, i;
962
963 for (i = 0; i < nr_wins; i++) {
964 ret = msc_buffer_win_alloc(msc, nr_pages[i]);
965 if (ret) {
966 msc_buffer_multi_free(msc);
967 return ret;
968 }
969 }
970
971 msc_buffer_relink(msc);
972
973 return 0;
974}
975
976/**
977 * msc_buffer_free() - free buffers for MSC
978 * @msc: MSC device
979 *
980 * Free MSC's storage buffers.
981 *
982 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex to
983 * serialize, so the caller is expected to hold it.
984 */
985static void msc_buffer_free(struct msc *msc)
986{
987 if (msc->mode == MSC_MODE_SINGLE)
988 msc_buffer_contig_free(msc);
989 else if (msc->mode == MSC_MODE_MULTI)
990 msc_buffer_multi_free(msc);
991}
992
993/**
994 * msc_buffer_alloc() - allocate a buffer for MSC
995 * @msc: MSC device
996 * @size: allocation size in bytes
997 *
998 * Allocate a storage buffer for MSC, depending on the msc::mode, it will be
999 * either done via msc_buffer_contig_alloc() for SINGLE operation mode or
1000 * msc_buffer_win_alloc() for multiblock operation. The latter allocates one
1001 * window per invocation, so in multiblock mode this can be called multiple
1002 * times for the same MSC to allocate multiple windows.
1003 *
1004 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
1005 * to serialize, so the caller is expected to hold it.
1006 *
1007 * Return: 0 on success, -errno otherwise.
1008 */
1009static int msc_buffer_alloc(struct msc *msc, unsigned long *nr_pages,
1010 unsigned int nr_wins)
1011{
1012 int ret;
1013
1014 /* -1: buffer not allocated */
1015 if (atomic_read(&msc->user_count) != -1)
1016 return -EBUSY;
1017
1018 if (msc->mode == MSC_MODE_SINGLE) {
1019 if (nr_wins != 1)
1020 return -EINVAL;
1021
1022 ret = msc_buffer_contig_alloc(msc, nr_pages[0] << PAGE_SHIFT);
1023 } else if (msc->mode == MSC_MODE_MULTI) {
1024 ret = msc_buffer_multi_alloc(msc, nr_pages, nr_wins);
1025 } else {
1026 ret = -ENOTSUPP;
1027 }
1028
1029 if (!ret) {
1030 /* allocation should be visible before the counter goes to 0 */
1031 smp_mb__before_atomic();
1032
1033 if (WARN_ON_ONCE(atomic_cmpxchg(&msc->user_count, -1, 0) != -1))
1034 return -EINVAL;
1035 }
1036
1037 return ret;
1038}
1039
1040/**
1041 * msc_buffer_unlocked_free_unless_used() - free a buffer unless it's in use
1042 * @msc: MSC device
1043 *
1044 * This will free MSC buffer unless it is in use or there is no allocated
1045 * buffer.
1046 * Caller needs to hold msc::buf_mutex.
1047 *
1048 * Return: 0 on successful deallocation or if there was no buffer to
1049 * deallocate, -EBUSY if there are active users.
1050 */
1051static int msc_buffer_unlocked_free_unless_used(struct msc *msc)
1052{
1053 int count, ret = 0;
1054
1055 count = atomic_cmpxchg(&msc->user_count, 0, -1);
1056
1057 /* > 0: buffer is allocated and has users */
1058 if (count > 0)
1059 ret = -EBUSY;
1060 /* 0: buffer is allocated, no users */
1061 else if (!count)
1062 msc_buffer_free(msc);
1063 /* < 0: no buffer, nothing to do */
1064
1065 return ret;
1066}
1067
1068/**
1069 * msc_buffer_free_unless_used() - free a buffer unless it's in use
1070 * @msc: MSC device
1071 *
1072 * This is a locked version of msc_buffer_unlocked_free_unless_used().
1073 */
1074static int msc_buffer_free_unless_used(struct msc *msc)
1075{
1076 int ret;
1077
1078 mutex_lock(&msc->buf_mutex);
1079 ret = msc_buffer_unlocked_free_unless_used(msc);
1080 mutex_unlock(&msc->buf_mutex);
1081
1082 return ret;
1083}
1084
1085/**
1086 * msc_buffer_get_page() - get MSC buffer page at a given offset
1087 * @msc: MSC device
1088 * @pgoff: page offset into the storage buffer
1089 *
1090 * This traverses msc::win_list, so holding msc::buf_mutex is expected from
1091 * the caller.
1092 *
1093 * Return: page if @pgoff corresponds to a valid buffer page or NULL.
1094 */
1095static struct page *msc_buffer_get_page(struct msc *msc, unsigned long pgoff)
1096{
1097 struct msc_window *win;
1098
1099 if (msc->mode == MSC_MODE_SINGLE)
1100 return msc_buffer_contig_get_page(msc, pgoff);
1101
1102 list_for_each_entry(win, &msc->win_list, entry)
1103 if (pgoff >= win->pgoff && pgoff < win->pgoff + win->nr_blocks)
1104 goto found;
1105
1106 return NULL;
1107
1108found:
1109 pgoff -= win->pgoff;
Alexander Shishkinba39bd82019-05-03 11:44:44 +03001110 return sg_page(&win->sgt.sgl[pgoff]);
Alexander Shishkinba826642015-09-22 15:47:18 +03001111}
1112
1113/**
1114 * struct msc_win_to_user_struct - data for copy_to_user() callback
1115 * @buf: userspace buffer to copy data to
1116 * @offset: running offset
1117 */
1118struct msc_win_to_user_struct {
1119 char __user *buf;
1120 unsigned long offset;
1121};
1122
1123/**
1124 * msc_win_to_user() - iterator for msc_buffer_iterate() to copy data to user
1125 * @data: callback's private data
1126 * @src: source buffer
1127 * @len: amount of data to copy from the source buffer
1128 */
1129static unsigned long msc_win_to_user(void *data, void *src, size_t len)
1130{
1131 struct msc_win_to_user_struct *u = data;
1132 unsigned long ret;
1133
1134 ret = copy_to_user(u->buf + u->offset, src, len);
1135 u->offset += len - ret;
1136
1137 return ret;
1138}
1139
1140
1141/*
1142 * file operations' callbacks
1143 */
1144
1145static int intel_th_msc_open(struct inode *inode, struct file *file)
1146{
1147 struct intel_th_device *thdev = file->private_data;
1148 struct msc *msc = dev_get_drvdata(&thdev->dev);
1149 struct msc_iter *iter;
1150
1151 if (!capable(CAP_SYS_RAWIO))
1152 return -EPERM;
1153
1154 iter = msc_iter_install(msc);
Alexander Shishkina45ff6e2016-03-10 18:21:14 +02001155 if (IS_ERR(iter))
1156 return PTR_ERR(iter);
Alexander Shishkinba826642015-09-22 15:47:18 +03001157
1158 file->private_data = iter;
1159
1160 return nonseekable_open(inode, file);
1161}
1162
1163static int intel_th_msc_release(struct inode *inode, struct file *file)
1164{
1165 struct msc_iter *iter = file->private_data;
1166 struct msc *msc = iter->msc;
1167
1168 msc_iter_remove(iter, msc);
1169
1170 return 0;
1171}
1172
1173static ssize_t
1174msc_single_to_user(struct msc *msc, char __user *buf, loff_t off, size_t len)
1175{
Alexander Shishkined392682015-10-06 12:47:18 +03001176 unsigned long size = msc->nr_pages << PAGE_SHIFT, rem = len;
Alexander Shishkinba826642015-09-22 15:47:18 +03001177 unsigned long start = off, tocopy = 0;
1178
1179 if (msc->single_wrap) {
1180 start += msc->single_sz;
1181 if (start < size) {
1182 tocopy = min(rem, size - start);
1183 if (copy_to_user(buf, msc->base + start, tocopy))
1184 return -EFAULT;
1185
1186 buf += tocopy;
1187 rem -= tocopy;
1188 start += tocopy;
1189 }
1190
1191 start &= size - 1;
1192 if (rem) {
1193 tocopy = min(rem, msc->single_sz - start);
1194 if (copy_to_user(buf, msc->base + start, tocopy))
1195 return -EFAULT;
1196
1197 rem -= tocopy;
1198 }
1199
1200 return len - rem;
1201 }
1202
1203 if (copy_to_user(buf, msc->base + start, rem))
1204 return -EFAULT;
1205
1206 return len;
1207}
1208
1209static ssize_t intel_th_msc_read(struct file *file, char __user *buf,
1210 size_t len, loff_t *ppos)
1211{
1212 struct msc_iter *iter = file->private_data;
1213 struct msc *msc = iter->msc;
1214 size_t size;
1215 loff_t off = *ppos;
1216 ssize_t ret = 0;
1217
1218 if (!atomic_inc_unless_negative(&msc->user_count))
1219 return 0;
1220
Alexander Shishkinba826642015-09-22 15:47:18 +03001221 if (msc->mode == MSC_MODE_SINGLE && !msc->single_wrap)
1222 size = msc->single_sz;
1223 else
1224 size = msc->nr_pages << PAGE_SHIFT;
1225
1226 if (!size)
Alexander Shishkinba826642015-09-22 15:47:18 +03001227 goto put_count;
Laurent FERT2bed0742016-02-15 19:11:57 +02001228
1229 if (off >= size)
1230 goto put_count;
1231
Alexander Shishkinba826642015-09-22 15:47:18 +03001232 if (off + len >= size)
1233 len = size - off;
1234
1235 if (msc->mode == MSC_MODE_SINGLE) {
1236 ret = msc_single_to_user(msc, buf, off, len);
1237 if (ret >= 0)
1238 *ppos += ret;
1239 } else if (msc->mode == MSC_MODE_MULTI) {
1240 struct msc_win_to_user_struct u = {
1241 .buf = buf,
1242 .offset = 0,
1243 };
1244
1245 ret = msc_buffer_iterate(iter, len, &u, msc_win_to_user);
1246 if (ret >= 0)
1247 *ppos = iter->offset;
1248 } else {
1249 ret = -ENOTSUPP;
1250 }
1251
1252put_count:
1253 atomic_dec(&msc->user_count);
1254
1255 return ret;
1256}
1257
1258/*
1259 * vm operations callbacks (vm_ops)
1260 */
1261
1262static void msc_mmap_open(struct vm_area_struct *vma)
1263{
1264 struct msc_iter *iter = vma->vm_file->private_data;
1265 struct msc *msc = iter->msc;
1266
1267 atomic_inc(&msc->mmap_count);
1268}
1269
1270static void msc_mmap_close(struct vm_area_struct *vma)
1271{
1272 struct msc_iter *iter = vma->vm_file->private_data;
1273 struct msc *msc = iter->msc;
1274 unsigned long pg;
1275
1276 if (!atomic_dec_and_mutex_lock(&msc->mmap_count, &msc->buf_mutex))
1277 return;
1278
Joonsoo Kim0139aa72016-05-19 17:10:49 -07001279 /* drop page _refcounts */
Alexander Shishkinba826642015-09-22 15:47:18 +03001280 for (pg = 0; pg < msc->nr_pages; pg++) {
1281 struct page *page = msc_buffer_get_page(msc, pg);
1282
1283 if (WARN_ON_ONCE(!page))
1284 continue;
1285
1286 if (page->mapping)
1287 page->mapping = NULL;
1288 }
1289
1290 /* last mapping -- drop user_count */
1291 atomic_dec(&msc->user_count);
1292 mutex_unlock(&msc->buf_mutex);
1293}
1294
Souptick Joarder42df0502018-08-23 17:00:45 -07001295static vm_fault_t msc_mmap_fault(struct vm_fault *vmf)
Alexander Shishkinba826642015-09-22 15:47:18 +03001296{
Dave Jiang11bac802017-02-24 14:56:41 -08001297 struct msc_iter *iter = vmf->vma->vm_file->private_data;
Alexander Shishkinba826642015-09-22 15:47:18 +03001298 struct msc *msc = iter->msc;
1299
1300 vmf->page = msc_buffer_get_page(msc, vmf->pgoff);
1301 if (!vmf->page)
1302 return VM_FAULT_SIGBUS;
1303
1304 get_page(vmf->page);
Dave Jiang11bac802017-02-24 14:56:41 -08001305 vmf->page->mapping = vmf->vma->vm_file->f_mapping;
Alexander Shishkinba826642015-09-22 15:47:18 +03001306 vmf->page->index = vmf->pgoff;
1307
1308 return 0;
1309}
1310
1311static const struct vm_operations_struct msc_mmap_ops = {
1312 .open = msc_mmap_open,
1313 .close = msc_mmap_close,
1314 .fault = msc_mmap_fault,
1315};
1316
1317static int intel_th_msc_mmap(struct file *file, struct vm_area_struct *vma)
1318{
1319 unsigned long size = vma->vm_end - vma->vm_start;
1320 struct msc_iter *iter = vma->vm_file->private_data;
1321 struct msc *msc = iter->msc;
1322 int ret = -EINVAL;
1323
1324 if (!size || offset_in_page(size))
1325 return -EINVAL;
1326
1327 if (vma->vm_pgoff)
1328 return -EINVAL;
1329
1330 /* grab user_count once per mmap; drop in msc_mmap_close() */
1331 if (!atomic_inc_unless_negative(&msc->user_count))
1332 return -EINVAL;
1333
1334 if (msc->mode != MSC_MODE_SINGLE &&
1335 msc->mode != MSC_MODE_MULTI)
1336 goto out;
1337
1338 if (size >> PAGE_SHIFT != msc->nr_pages)
1339 goto out;
1340
1341 atomic_set(&msc->mmap_count, 1);
1342 ret = 0;
1343
1344out:
1345 if (ret)
1346 atomic_dec(&msc->user_count);
1347
1348 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1349 vma->vm_flags |= VM_DONTEXPAND | VM_DONTCOPY;
1350 vma->vm_ops = &msc_mmap_ops;
1351 return ret;
1352}
1353
1354static const struct file_operations intel_th_msc_fops = {
1355 .open = intel_th_msc_open,
1356 .release = intel_th_msc_release,
1357 .read = intel_th_msc_read,
1358 .mmap = intel_th_msc_mmap,
1359 .llseek = no_llseek,
Alexander Shishkin8e9a2be2016-04-08 17:36:04 +03001360 .owner = THIS_MODULE,
Alexander Shishkinba826642015-09-22 15:47:18 +03001361};
1362
1363static int intel_th_msc_init(struct msc *msc)
1364{
1365 atomic_set(&msc->user_count, -1);
1366
1367 msc->mode = MSC_MODE_MULTI;
1368 mutex_init(&msc->buf_mutex);
1369 INIT_LIST_HEAD(&msc->win_list);
Alexander Shishkinba826642015-09-22 15:47:18 +03001370 INIT_LIST_HEAD(&msc->iter_list);
1371
1372 msc->burst_len =
1373 (ioread32(msc->reg_base + REG_MSU_MSC0CTL) & MSC_LEN) >>
1374 __ffs(MSC_LEN);
1375
1376 return 0;
1377}
1378
Alexander Shishkinaac8da62019-05-03 11:44:41 +03001379static irqreturn_t intel_th_msc_interrupt(struct intel_th_device *thdev)
1380{
1381 struct msc *msc = dev_get_drvdata(&thdev->dev);
1382 u32 msusts = ioread32(msc->msu_base + REG_MSU_MSUSTS);
1383 u32 mask = msc->index ? MSUSTS_MSC1BLAST : MSUSTS_MSC0BLAST;
1384
1385 if (!(msusts & mask)) {
1386 if (msc->enabled)
1387 return IRQ_HANDLED;
1388 return IRQ_NONE;
1389 }
1390
1391 return IRQ_HANDLED;
1392}
1393
Alexander Shishkinba826642015-09-22 15:47:18 +03001394static const char * const msc_mode[] = {
1395 [MSC_MODE_SINGLE] = "single",
1396 [MSC_MODE_MULTI] = "multi",
1397 [MSC_MODE_EXI] = "ExI",
1398 [MSC_MODE_DEBUG] = "debug",
1399};
1400
1401static ssize_t
1402wrap_show(struct device *dev, struct device_attribute *attr, char *buf)
1403{
1404 struct msc *msc = dev_get_drvdata(dev);
1405
1406 return scnprintf(buf, PAGE_SIZE, "%d\n", msc->wrap);
1407}
1408
1409static ssize_t
1410wrap_store(struct device *dev, struct device_attribute *attr, const char *buf,
1411 size_t size)
1412{
1413 struct msc *msc = dev_get_drvdata(dev);
1414 unsigned long val;
1415 int ret;
1416
1417 ret = kstrtoul(buf, 10, &val);
1418 if (ret)
1419 return ret;
1420
1421 msc->wrap = !!val;
1422
1423 return size;
1424}
1425
1426static DEVICE_ATTR_RW(wrap);
1427
1428static ssize_t
1429mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1430{
1431 struct msc *msc = dev_get_drvdata(dev);
1432
1433 return scnprintf(buf, PAGE_SIZE, "%s\n", msc_mode[msc->mode]);
1434}
1435
1436static ssize_t
1437mode_store(struct device *dev, struct device_attribute *attr, const char *buf,
1438 size_t size)
1439{
1440 struct msc *msc = dev_get_drvdata(dev);
1441 size_t len = size;
1442 char *cp;
1443 int i, ret;
1444
1445 if (!capable(CAP_SYS_RAWIO))
1446 return -EPERM;
1447
1448 cp = memchr(buf, '\n', len);
1449 if (cp)
1450 len = cp - buf;
1451
1452 for (i = 0; i < ARRAY_SIZE(msc_mode); i++)
1453 if (!strncmp(msc_mode[i], buf, len))
1454 goto found;
1455
1456 return -EINVAL;
1457
1458found:
1459 mutex_lock(&msc->buf_mutex);
1460 ret = msc_buffer_unlocked_free_unless_used(msc);
1461 if (!ret)
1462 msc->mode = i;
1463 mutex_unlock(&msc->buf_mutex);
1464
1465 return ret ? ret : size;
1466}
1467
1468static DEVICE_ATTR_RW(mode);
1469
1470static ssize_t
1471nr_pages_show(struct device *dev, struct device_attribute *attr, char *buf)
1472{
1473 struct msc *msc = dev_get_drvdata(dev);
1474 struct msc_window *win;
1475 size_t count = 0;
1476
1477 mutex_lock(&msc->buf_mutex);
1478
1479 if (msc->mode == MSC_MODE_SINGLE)
1480 count = scnprintf(buf, PAGE_SIZE, "%ld\n", msc->nr_pages);
1481 else if (msc->mode == MSC_MODE_MULTI) {
1482 list_for_each_entry(win, &msc->win_list, entry) {
1483 count += scnprintf(buf + count, PAGE_SIZE - count,
1484 "%d%c", win->nr_blocks,
1485 msc_is_last_win(win) ? '\n' : ',');
1486 }
1487 } else {
1488 count = scnprintf(buf, PAGE_SIZE, "unsupported\n");
1489 }
1490
1491 mutex_unlock(&msc->buf_mutex);
1492
1493 return count;
1494}
1495
1496static ssize_t
1497nr_pages_store(struct device *dev, struct device_attribute *attr,
1498 const char *buf, size_t size)
1499{
1500 struct msc *msc = dev_get_drvdata(dev);
1501 unsigned long val, *win = NULL, *rewin;
1502 size_t len = size;
1503 const char *p = buf;
1504 char *end, *s;
1505 int ret, nr_wins = 0;
1506
1507 if (!capable(CAP_SYS_RAWIO))
1508 return -EPERM;
1509
1510 ret = msc_buffer_free_unless_used(msc);
1511 if (ret)
1512 return ret;
1513
1514 /* scan the comma-separated list of allocation sizes */
1515 end = memchr(buf, '\n', len);
1516 if (end)
1517 len = end - buf;
1518
1519 do {
1520 end = memchr(p, ',', len);
1521 s = kstrndup(p, end ? end - p : len, GFP_KERNEL);
Alexander Shishkin6575cbd2016-03-04 16:16:31 +02001522 if (!s) {
1523 ret = -ENOMEM;
1524 goto free_win;
1525 }
1526
Alexander Shishkinba826642015-09-22 15:47:18 +03001527 ret = kstrtoul(s, 10, &val);
1528 kfree(s);
1529
1530 if (ret || !val)
1531 goto free_win;
1532
1533 if (nr_wins && msc->mode == MSC_MODE_SINGLE) {
1534 ret = -EINVAL;
1535 goto free_win;
1536 }
1537
1538 nr_wins++;
1539 rewin = krealloc(win, sizeof(*win) * nr_wins, GFP_KERNEL);
1540 if (!rewin) {
1541 kfree(win);
1542 return -ENOMEM;
1543 }
1544
1545 win = rewin;
1546 win[nr_wins - 1] = val;
1547
1548 if (!end)
1549 break;
1550
Alexander Shishkinec5b5ad2018-12-19 17:19:22 +02001551 /* consume the number and the following comma, hence +1 */
1552 len -= end - p + 1;
Alexander Shishkinba826642015-09-22 15:47:18 +03001553 p = end + 1;
1554 } while (len);
1555
1556 mutex_lock(&msc->buf_mutex);
1557 ret = msc_buffer_alloc(msc, win, nr_wins);
1558 mutex_unlock(&msc->buf_mutex);
1559
1560free_win:
1561 kfree(win);
1562
1563 return ret ? ret : size;
1564}
1565
1566static DEVICE_ATTR_RW(nr_pages);
1567
1568static struct attribute *msc_output_attrs[] = {
1569 &dev_attr_wrap.attr,
1570 &dev_attr_mode.attr,
1571 &dev_attr_nr_pages.attr,
1572 NULL,
1573};
1574
1575static struct attribute_group msc_output_group = {
1576 .attrs = msc_output_attrs,
1577};
1578
1579static int intel_th_msc_probe(struct intel_th_device *thdev)
1580{
1581 struct device *dev = &thdev->dev;
1582 struct resource *res;
1583 struct msc *msc;
1584 void __iomem *base;
1585 int err;
1586
1587 res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
1588 if (!res)
1589 return -ENODEV;
1590
1591 base = devm_ioremap(dev, res->start, resource_size(res));
Dan Carpenter73061da2015-10-16 17:09:13 +03001592 if (!base)
1593 return -ENOMEM;
Alexander Shishkinba826642015-09-22 15:47:18 +03001594
1595 msc = devm_kzalloc(dev, sizeof(*msc), GFP_KERNEL);
1596 if (!msc)
1597 return -ENOMEM;
1598
Alexander Shishkinaac8da62019-05-03 11:44:41 +03001599 res = intel_th_device_get_resource(thdev, IORESOURCE_IRQ, 1);
1600 if (!res)
1601 msc->do_irq = 1;
1602
Alexander Shishkinba826642015-09-22 15:47:18 +03001603 msc->index = thdev->id;
1604
1605 msc->thdev = thdev;
1606 msc->reg_base = base + msc->index * 0x100;
Alexander Shishkinaac8da62019-05-03 11:44:41 +03001607 msc->msu_base = base;
1608
1609 err = intel_th_msu_init(msc);
1610 if (err)
1611 return err;
Alexander Shishkinba826642015-09-22 15:47:18 +03001612
1613 err = intel_th_msc_init(msc);
1614 if (err)
1615 return err;
1616
Alexander Shishkinba826642015-09-22 15:47:18 +03001617 dev_set_drvdata(dev, msc);
1618
1619 return 0;
1620}
1621
1622static void intel_th_msc_remove(struct intel_th_device *thdev)
1623{
Alexander Shishkinf152dfe2016-04-08 17:37:40 +03001624 struct msc *msc = dev_get_drvdata(&thdev->dev);
1625 int ret;
1626
1627 intel_th_msc_deactivate(thdev);
Alexander Shishkinaac8da62019-05-03 11:44:41 +03001628 intel_th_msu_deinit(msc);
Alexander Shishkinf152dfe2016-04-08 17:37:40 +03001629
1630 /*
1631 * Buffers should not be used at this point except if the
1632 * output character device is still open and the parent
1633 * device gets detached from its bus, which is a FIXME.
1634 */
1635 ret = msc_buffer_free_unless_used(msc);
1636 WARN_ON_ONCE(ret);
Alexander Shishkinba826642015-09-22 15:47:18 +03001637}
1638
1639static struct intel_th_driver intel_th_msc_driver = {
1640 .probe = intel_th_msc_probe,
1641 .remove = intel_th_msc_remove,
Alexander Shishkinaac8da62019-05-03 11:44:41 +03001642 .irq = intel_th_msc_interrupt,
Alexander Shishkinba826642015-09-22 15:47:18 +03001643 .activate = intel_th_msc_activate,
1644 .deactivate = intel_th_msc_deactivate,
1645 .fops = &intel_th_msc_fops,
Alexander Shishkin9d482ae2016-03-04 19:55:10 +02001646 .attr_group = &msc_output_group,
Alexander Shishkinba826642015-09-22 15:47:18 +03001647 .driver = {
1648 .name = "msc",
1649 .owner = THIS_MODULE,
1650 },
1651};
1652
1653module_driver(intel_th_msc_driver,
1654 intel_th_driver_register,
1655 intel_th_driver_unregister);
1656
1657MODULE_LICENSE("GPL v2");
1658MODULE_DESCRIPTION("Intel(R) Trace Hub Memory Storage Unit driver");
1659MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");