blob: cfd48c81b9d9a683d5031d8126c8b7f66c01030d [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 Shishkinaad14ad2019-05-03 11:44:51 +030078 * @cur_win: current window
Alexander Shishkinba826642015-09-22 15:47:18 +030079 * @nr_pages: total number of pages allocated for this buffer
80 * @single_sz: amount of data in single mode
81 * @single_wrap: single mode wrap occurred
82 * @base: buffer's base pointer
83 * @base_addr: buffer's base address
84 * @user_count: number of users of the buffer
85 * @mmap_count: number of mappings
86 * @buf_mutex: mutex to serialize access to buffer-related bits
87
88 * @enabled: MSC is enabled
89 * @wrap: wrapping is enabled
90 * @mode: MSC operating mode
91 * @burst_len: write burst length
92 * @index: number of this MSC in the MSU
93 */
94struct msc {
95 void __iomem *reg_base;
Alexander Shishkinaac8da62019-05-03 11:44:41 +030096 void __iomem *msu_base;
Alexander Shishkinba826642015-09-22 15:47:18 +030097 struct intel_th_device *thdev;
98
99 struct list_head win_list;
Alexander Shishkin4e0eaf22019-05-03 11:44:34 +0300100 struct sg_table single_sgt;
Alexander Shishkinaad14ad2019-05-03 11:44:51 +0300101 struct msc_window *cur_win;
Alexander Shishkinba826642015-09-22 15:47:18 +0300102 unsigned long nr_pages;
103 unsigned long single_sz;
104 unsigned int single_wrap : 1;
105 void *base;
106 dma_addr_t base_addr;
107
108 /* <0: no buffer, 0: no users, >0: active users */
109 atomic_t user_count;
110
111 atomic_t mmap_count;
112 struct mutex buf_mutex;
113
Alexander Shishkinba826642015-09-22 15:47:18 +0300114 struct list_head iter_list;
115
116 /* config */
117 unsigned int enabled : 1,
Alexander Shishkinaac8da62019-05-03 11:44:41 +0300118 wrap : 1,
119 do_irq : 1;
Alexander Shishkinba826642015-09-22 15:47:18 +0300120 unsigned int mode;
121 unsigned int burst_len;
122 unsigned int index;
123};
124
125static inline bool msc_block_is_empty(struct msc_block_desc *bdesc)
126{
127 /* header hasn't been written */
128 if (!bdesc->valid_dw)
129 return true;
130
131 /* valid_dw includes the header */
132 if (!msc_data_sz(bdesc))
133 return true;
134
135 return false;
136}
137
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300138static inline struct msc_block_desc *
139msc_win_block(struct msc_window *win, unsigned int block)
140{
141 return sg_virt(&win->sgt.sgl[block]);
142}
143
144static inline dma_addr_t
145msc_win_baddr(struct msc_window *win, unsigned int block)
146{
147 return sg_dma_address(&win->sgt.sgl[block]);
148}
149
150static inline unsigned long
151msc_win_bpfn(struct msc_window *win, unsigned int block)
152{
153 return msc_win_baddr(win, block) >> PAGE_SHIFT;
154}
155
Alexander Shishkinba826642015-09-22 15:47:18 +0300156/**
Alexander Shishkinaad14ad2019-05-03 11:44:51 +0300157 * msc_is_last_win() - check if a window is the last one for a given MSC
158 * @win: window
159 * Return: true if @win is the last window in MSC's multiblock buffer
160 */
161static inline bool msc_is_last_win(struct msc_window *win)
162{
163 return win->entry.next == &win->msc->win_list;
164}
165
166/**
167 * msc_next_window() - return next window in the multiblock buffer
168 * @win: current window
169 *
170 * Return: window following the current one
171 */
172static struct msc_window *msc_next_window(struct msc_window *win)
173{
174 if (msc_is_last_win(win))
175 return list_first_entry(&win->msc->win_list, struct msc_window,
176 entry);
177
178 return list_next_entry(win, entry);
179}
180
181/**
Alexander Shishkinba826642015-09-22 15:47:18 +0300182 * msc_oldest_window() - locate the window with oldest data
183 * @msc: MSC device
184 *
185 * This should only be used in multiblock mode. Caller should hold the
186 * msc::user_count reference.
187 *
188 * Return: the oldest window with valid data
189 */
190static struct msc_window *msc_oldest_window(struct msc *msc)
191{
Alexander Shishkinaad14ad2019-05-03 11:44:51 +0300192 struct msc_window *win, *next = msc_next_window(msc->cur_win);
Alexander Shishkinba826642015-09-22 15:47:18 +0300193 unsigned int found = 0;
194
195 if (list_empty(&msc->win_list))
196 return NULL;
197
198 /*
199 * we might need a radix tree for this, depending on how
200 * many windows a typical user would allocate; ideally it's
201 * something like 2, in which case we're good
202 */
203 list_for_each_entry(win, &msc->win_list, entry) {
Alexander Shishkinaad14ad2019-05-03 11:44:51 +0300204 if (win == next)
Alexander Shishkinba826642015-09-22 15:47:18 +0300205 found++;
206
207 /* skip the empty ones */
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300208 if (msc_block_is_empty(msc_win_block(win, 0)))
Alexander Shishkinba826642015-09-22 15:47:18 +0300209 continue;
210
211 if (found)
212 return win;
213 }
214
Alexander Shishkin0de9e032019-05-03 11:44:43 +0300215 return list_first_entry(&msc->win_list, struct msc_window, entry);
Alexander Shishkinba826642015-09-22 15:47:18 +0300216}
217
218/**
219 * msc_win_oldest_block() - locate the oldest block in a given window
220 * @win: window to look at
221 *
222 * Return: index of the block with the oldest data
223 */
224static unsigned int msc_win_oldest_block(struct msc_window *win)
225{
226 unsigned int blk;
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300227 struct msc_block_desc *bdesc = msc_win_block(win, 0);
Alexander Shishkinba826642015-09-22 15:47:18 +0300228
229 /* without wrapping, first block is the oldest */
230 if (!msc_block_wrapped(bdesc))
231 return 0;
232
233 /*
234 * with wrapping, last written block contains both the newest and the
235 * oldest data for this window.
236 */
237 for (blk = 0; blk < win->nr_blocks; blk++) {
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300238 bdesc = msc_win_block(win, blk);
Alexander Shishkinba826642015-09-22 15:47:18 +0300239
240 if (msc_block_last_written(bdesc))
241 return blk;
242 }
243
244 return 0;
245}
246
Alexander Shishkinba826642015-09-22 15:47:18 +0300247static 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{
Alexander Shishkinba826642015-09-22 15:47:18 +0300580 u32 reg;
581
Alexander Shishkina45ff6e2016-03-10 18:21:14 +0200582 lockdep_assert_held(&msc->buf_mutex);
Alexander Shishkinba826642015-09-22 15:47:18 +0300583
584 intel_th_trace_disable(msc->thdev);
585
Alexander Shishkinba826642015-09-22 15:47:18 +0300586 if (msc->mode == MSC_MODE_SINGLE) {
Alexander Shishkin8d415512019-05-03 11:44:46 +0300587 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
Alexander Shishkinba826642015-09-22 15:47:18 +0300588 msc->single_wrap = !!(reg & MSCSTS_WRAPSTAT);
589
590 reg = ioread32(msc->reg_base + REG_MSU_MSC0MWP);
591 msc->single_sz = reg & ((msc->nr_pages << PAGE_SHIFT) - 1);
592 dev_dbg(msc_dev(msc), "MSCnMWP: %08x/%08lx, wrap: %d\n",
593 reg, msc->single_sz, msc->single_wrap);
594 }
595
596 reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
597 reg &= ~MSC_EN;
598 iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
599 msc->enabled = 0;
600
601 iowrite32(0, msc->reg_base + REG_MSU_MSC0BAR);
602 iowrite32(0, msc->reg_base + REG_MSU_MSC0SIZE);
603
604 dev_dbg(msc_dev(msc), "MSCnNWSA: %08x\n",
605 ioread32(msc->reg_base + REG_MSU_MSC0NWSA));
606
607 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
608 dev_dbg(msc_dev(msc), "MSCnSTS: %08x\n", reg);
609}
610
611static int intel_th_msc_activate(struct intel_th_device *thdev)
612{
613 struct msc *msc = dev_get_drvdata(&thdev->dev);
Alexander Shishkina45ff6e2016-03-10 18:21:14 +0200614 int ret = -EBUSY;
Alexander Shishkinba826642015-09-22 15:47:18 +0300615
616 if (!atomic_inc_unless_negative(&msc->user_count))
617 return -ENODEV;
618
Alexander Shishkina45ff6e2016-03-10 18:21:14 +0200619 mutex_lock(&msc->buf_mutex);
Alexander Shishkinba826642015-09-22 15:47:18 +0300620
Alexander Shishkina45ff6e2016-03-10 18:21:14 +0200621 /* if there are readers, refuse */
622 if (list_empty(&msc->iter_list))
623 ret = msc_configure(msc);
624
625 mutex_unlock(&msc->buf_mutex);
626
627 if (ret)
Alexander Shishkinba826642015-09-22 15:47:18 +0300628 atomic_dec(&msc->user_count);
Alexander Shishkinba826642015-09-22 15:47:18 +0300629
Alexander Shishkina45ff6e2016-03-10 18:21:14 +0200630 return ret;
Alexander Shishkinba826642015-09-22 15:47:18 +0300631}
632
633static void intel_th_msc_deactivate(struct intel_th_device *thdev)
634{
635 struct msc *msc = dev_get_drvdata(&thdev->dev);
636
Alexander Shishkina45ff6e2016-03-10 18:21:14 +0200637 mutex_lock(&msc->buf_mutex);
638 if (msc->enabled) {
639 msc_disable(msc);
640 atomic_dec(&msc->user_count);
641 }
642 mutex_unlock(&msc->buf_mutex);
Alexander Shishkinba826642015-09-22 15:47:18 +0300643}
644
645/**
646 * msc_buffer_contig_alloc() - allocate a contiguous buffer for SINGLE mode
647 * @msc: MSC device
648 * @size: allocation size in bytes
649 *
650 * This modifies msc::base, which requires msc::buf_mutex to serialize, so the
651 * caller is expected to hold it.
652 *
653 * Return: 0 on success, -errno otherwise.
654 */
655static int msc_buffer_contig_alloc(struct msc *msc, unsigned long size)
656{
Alexander Shishkin4e0eaf22019-05-03 11:44:34 +0300657 unsigned long nr_pages = size >> PAGE_SHIFT;
Alexander Shishkinba826642015-09-22 15:47:18 +0300658 unsigned int order = get_order(size);
659 struct page *page;
Alexander Shishkin4e0eaf22019-05-03 11:44:34 +0300660 int ret;
Alexander Shishkinba826642015-09-22 15:47:18 +0300661
662 if (!size)
663 return 0;
664
Alexander Shishkin4e0eaf22019-05-03 11:44:34 +0300665 ret = sg_alloc_table(&msc->single_sgt, 1, GFP_KERNEL);
666 if (ret)
667 goto err_out;
668
669 ret = -ENOMEM;
Alexander Shishkin918b8642019-06-21 19:19:29 +0300670 page = alloc_pages(GFP_KERNEL | __GFP_ZERO | GFP_DMA32, order);
Alexander Shishkinba826642015-09-22 15:47:18 +0300671 if (!page)
Alexander Shishkin4e0eaf22019-05-03 11:44:34 +0300672 goto err_free_sgt;
Alexander Shishkinba826642015-09-22 15:47:18 +0300673
674 split_page(page, order);
Alexander Shishkin4e0eaf22019-05-03 11:44:34 +0300675 sg_set_buf(msc->single_sgt.sgl, page_address(page), size);
676
677 ret = dma_map_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl, 1,
678 DMA_FROM_DEVICE);
679 if (ret < 0)
680 goto err_free_pages;
681
682 msc->nr_pages = nr_pages;
Alexander Shishkinba826642015-09-22 15:47:18 +0300683 msc->base = page_address(page);
Alexander Shishkin4e0eaf22019-05-03 11:44:34 +0300684 msc->base_addr = sg_dma_address(msc->single_sgt.sgl);
Alexander Shishkinba826642015-09-22 15:47:18 +0300685
686 return 0;
Alexander Shishkin4e0eaf22019-05-03 11:44:34 +0300687
688err_free_pages:
689 __free_pages(page, order);
690
691err_free_sgt:
692 sg_free_table(&msc->single_sgt);
693
694err_out:
695 return ret;
Alexander Shishkinba826642015-09-22 15:47:18 +0300696}
697
698/**
699 * msc_buffer_contig_free() - free a contiguous buffer
700 * @msc: MSC configured in SINGLE mode
701 */
702static void msc_buffer_contig_free(struct msc *msc)
703{
704 unsigned long off;
705
Alexander Shishkin4e0eaf22019-05-03 11:44:34 +0300706 dma_unmap_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl,
707 1, DMA_FROM_DEVICE);
708 sg_free_table(&msc->single_sgt);
709
Alexander Shishkinba826642015-09-22 15:47:18 +0300710 for (off = 0; off < msc->nr_pages << PAGE_SHIFT; off += PAGE_SIZE) {
711 struct page *page = virt_to_page(msc->base + off);
712
713 page->mapping = NULL;
714 __free_page(page);
715 }
716
717 msc->nr_pages = 0;
718}
719
720/**
721 * msc_buffer_contig_get_page() - find a page at a given offset
722 * @msc: MSC configured in SINGLE mode
723 * @pgoff: page offset
724 *
725 * Return: page, if @pgoff is within the range, NULL otherwise.
726 */
727static struct page *msc_buffer_contig_get_page(struct msc *msc,
728 unsigned long pgoff)
729{
730 if (pgoff >= msc->nr_pages)
731 return NULL;
732
733 return virt_to_page(msc->base + (pgoff << PAGE_SHIFT));
734}
735
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300736static int __msc_buffer_win_alloc(struct msc_window *win,
737 unsigned int nr_blocks)
738{
739 struct scatterlist *sg_ptr;
740 void *block;
741 int i, ret;
742
743 ret = sg_alloc_table(&win->sgt, nr_blocks, GFP_KERNEL);
744 if (ret)
745 return -ENOMEM;
746
747 for_each_sg(win->sgt.sgl, sg_ptr, nr_blocks, i) {
748 block = dma_alloc_coherent(msc_dev(win->msc)->parent->parent,
749 PAGE_SIZE, &sg_dma_address(sg_ptr),
750 GFP_KERNEL);
751 if (!block)
752 goto err_nomem;
753
754 sg_set_buf(sg_ptr, block, PAGE_SIZE);
755 }
756
757 return nr_blocks;
758
759err_nomem:
760 for (i--; i >= 0; i--)
761 dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
762 msc_win_block(win, i),
763 msc_win_baddr(win, i));
764
765 sg_free_table(&win->sgt);
766
767 return -ENOMEM;
768}
769
Shaokun Zhangb96fb362019-06-21 19:19:27 +0300770#ifdef CONFIG_X86
771static void msc_buffer_set_uc(struct msc_window *win, unsigned int nr_blocks)
772{
773 int i;
774
775 for (i = 0; i < nr_blocks; i++)
776 /* Set the page as uncached */
777 set_memory_uc((unsigned long)msc_win_block(win, i), 1);
778}
779
780static void msc_buffer_set_wb(struct msc_window *win)
781{
782 int i;
783
784 for (i = 0; i < win->nr_blocks; i++)
785 /* Reset the page to write-back */
786 set_memory_wb((unsigned long)msc_win_block(win, i), 1);
787}
788#else /* !X86 */
789static inline void
790msc_buffer_set_uc(struct msc_window *win, unsigned int nr_blocks) {}
791static inline void msc_buffer_set_wb(struct msc_window *win) {}
792#endif /* CONFIG_X86 */
793
Alexander Shishkinba826642015-09-22 15:47:18 +0300794/**
795 * msc_buffer_win_alloc() - alloc a window for a multiblock mode
796 * @msc: MSC device
797 * @nr_blocks: number of pages in this window
798 *
799 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
800 * to serialize, so the caller is expected to hold it.
801 *
802 * Return: 0 on success, -errno otherwise.
803 */
804static int msc_buffer_win_alloc(struct msc *msc, unsigned int nr_blocks)
805{
806 struct msc_window *win;
Shaokun Zhangb96fb362019-06-21 19:19:27 +0300807 int ret = -ENOMEM;
Alexander Shishkinba826642015-09-22 15:47:18 +0300808
809 if (!nr_blocks)
810 return 0;
811
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300812 /*
813 * This limitation hold as long as we need random access to the
814 * block. When that changes, this can go away.
815 */
816 if (nr_blocks > SG_MAX_SINGLE_ALLOC)
817 return -EINVAL;
818
819 win = kzalloc(sizeof(*win), GFP_KERNEL);
Alexander Shishkinba826642015-09-22 15:47:18 +0300820 if (!win)
821 return -ENOMEM;
822
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300823 win->msc = msc;
824
Alexander Shishkinba826642015-09-22 15:47:18 +0300825 if (!list_empty(&msc->win_list)) {
Alexander Shishkin0de9e032019-05-03 11:44:43 +0300826 struct msc_window *prev = list_last_entry(&msc->win_list,
827 struct msc_window,
828 entry);
Alexander Shishkinba826642015-09-22 15:47:18 +0300829
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300830 /* This works as long as blocks are page-sized */
Alexander Shishkinba826642015-09-22 15:47:18 +0300831 win->pgoff = prev->pgoff + prev->nr_blocks;
832 }
833
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300834 ret = __msc_buffer_win_alloc(win, nr_blocks);
835 if (ret < 0)
836 goto err_nomem;
Alexander Shishkinba826642015-09-22 15:47:18 +0300837
Shaokun Zhangb96fb362019-06-21 19:19:27 +0300838 msc_buffer_set_uc(win, ret);
Alexander Shishkinba826642015-09-22 15:47:18 +0300839
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300840 win->nr_blocks = ret;
Alexander Shishkinba826642015-09-22 15:47:18 +0300841
842 if (list_empty(&msc->win_list)) {
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300843 msc->base = msc_win_block(win, 0);
844 msc->base_addr = msc_win_baddr(win, 0);
Alexander Shishkinaad14ad2019-05-03 11:44:51 +0300845 msc->cur_win = win;
Alexander Shishkinba826642015-09-22 15:47:18 +0300846 }
847
848 list_add_tail(&win->entry, &msc->win_list);
849 msc->nr_pages += nr_blocks;
850
851 return 0;
852
853err_nomem:
Alexander Shishkinba826642015-09-22 15:47:18 +0300854 kfree(win);
855
856 return ret;
857}
858
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300859static void __msc_buffer_win_free(struct msc *msc, struct msc_window *win)
860{
861 int i;
862
863 for (i = 0; i < win->nr_blocks; i++) {
864 struct page *page = sg_page(&win->sgt.sgl[i]);
865
866 page->mapping = NULL;
867 dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
868 msc_win_block(win, i), msc_win_baddr(win, i));
869 }
870 sg_free_table(&win->sgt);
871}
872
Alexander Shishkinba826642015-09-22 15:47:18 +0300873/**
874 * msc_buffer_win_free() - free a window from MSC's window list
875 * @msc: MSC device
876 * @win: window to free
877 *
878 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
879 * to serialize, so the caller is expected to hold it.
880 */
881static void msc_buffer_win_free(struct msc *msc, struct msc_window *win)
882{
Alexander Shishkinba826642015-09-22 15:47:18 +0300883 msc->nr_pages -= win->nr_blocks;
884
885 list_del(&win->entry);
886 if (list_empty(&msc->win_list)) {
887 msc->base = NULL;
888 msc->base_addr = 0;
889 }
890
Shaokun Zhangb96fb362019-06-21 19:19:27 +0300891 msc_buffer_set_wb(win);
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300892
893 __msc_buffer_win_free(msc, win);
Alexander Shishkinba826642015-09-22 15:47:18 +0300894
895 kfree(win);
896}
897
898/**
899 * msc_buffer_relink() - set up block descriptors for multiblock mode
900 * @msc: MSC device
901 *
902 * This traverses msc::win_list, which requires msc::buf_mutex to serialize,
903 * so the caller is expected to hold it.
904 */
905static void msc_buffer_relink(struct msc *msc)
906{
907 struct msc_window *win, *next_win;
908
909 /* call with msc::mutex locked */
910 list_for_each_entry(win, &msc->win_list, entry) {
911 unsigned int blk;
912 u32 sw_tag = 0;
913
914 /*
915 * Last window's next_win should point to the first window
916 * and MSC_SW_TAG_LASTWIN should be set.
917 */
918 if (msc_is_last_win(win)) {
919 sw_tag |= MSC_SW_TAG_LASTWIN;
Alexander Shishkin0de9e032019-05-03 11:44:43 +0300920 next_win = list_first_entry(&msc->win_list,
921 struct msc_window, entry);
Alexander Shishkinba826642015-09-22 15:47:18 +0300922 } else {
Alexander Shishkin0de9e032019-05-03 11:44:43 +0300923 next_win = list_next_entry(win, entry);
Alexander Shishkinba826642015-09-22 15:47:18 +0300924 }
925
926 for (blk = 0; blk < win->nr_blocks; blk++) {
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300927 struct msc_block_desc *bdesc = msc_win_block(win, blk);
Alexander Shishkinba826642015-09-22 15:47:18 +0300928
929 memset(bdesc, 0, sizeof(*bdesc));
930
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300931 bdesc->next_win = msc_win_bpfn(next_win, 0);
Alexander Shishkinba826642015-09-22 15:47:18 +0300932
933 /*
934 * Similarly to last window, last block should point
935 * to the first one.
936 */
937 if (blk == win->nr_blocks - 1) {
938 sw_tag |= MSC_SW_TAG_LASTBLK;
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300939 bdesc->next_blk = msc_win_bpfn(win, 0);
Alexander Shishkinba826642015-09-22 15:47:18 +0300940 } else {
Alexander Shishkinba39bd82019-05-03 11:44:44 +0300941 bdesc->next_blk = msc_win_bpfn(win, blk + 1);
Alexander Shishkinba826642015-09-22 15:47:18 +0300942 }
943
944 bdesc->sw_tag = sw_tag;
945 bdesc->block_sz = PAGE_SIZE / 64;
946 }
947 }
948
949 /*
950 * Make the above writes globally visible before tracing is
951 * enabled to make sure hardware sees them coherently.
952 */
953 wmb();
954}
955
956static void msc_buffer_multi_free(struct msc *msc)
957{
958 struct msc_window *win, *iter;
959
960 list_for_each_entry_safe(win, iter, &msc->win_list, entry)
961 msc_buffer_win_free(msc, win);
962}
963
964static int msc_buffer_multi_alloc(struct msc *msc, unsigned long *nr_pages,
965 unsigned int nr_wins)
966{
967 int ret, i;
968
969 for (i = 0; i < nr_wins; i++) {
970 ret = msc_buffer_win_alloc(msc, nr_pages[i]);
971 if (ret) {
972 msc_buffer_multi_free(msc);
973 return ret;
974 }
975 }
976
977 msc_buffer_relink(msc);
978
979 return 0;
980}
981
982/**
983 * msc_buffer_free() - free buffers for MSC
984 * @msc: MSC device
985 *
986 * Free MSC's storage buffers.
987 *
988 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex to
989 * serialize, so the caller is expected to hold it.
990 */
991static void msc_buffer_free(struct msc *msc)
992{
993 if (msc->mode == MSC_MODE_SINGLE)
994 msc_buffer_contig_free(msc);
995 else if (msc->mode == MSC_MODE_MULTI)
996 msc_buffer_multi_free(msc);
997}
998
999/**
1000 * msc_buffer_alloc() - allocate a buffer for MSC
1001 * @msc: MSC device
1002 * @size: allocation size in bytes
1003 *
1004 * Allocate a storage buffer for MSC, depending on the msc::mode, it will be
1005 * either done via msc_buffer_contig_alloc() for SINGLE operation mode or
1006 * msc_buffer_win_alloc() for multiblock operation. The latter allocates one
1007 * window per invocation, so in multiblock mode this can be called multiple
1008 * times for the same MSC to allocate multiple windows.
1009 *
1010 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
1011 * to serialize, so the caller is expected to hold it.
1012 *
1013 * Return: 0 on success, -errno otherwise.
1014 */
1015static int msc_buffer_alloc(struct msc *msc, unsigned long *nr_pages,
1016 unsigned int nr_wins)
1017{
1018 int ret;
1019
1020 /* -1: buffer not allocated */
1021 if (atomic_read(&msc->user_count) != -1)
1022 return -EBUSY;
1023
1024 if (msc->mode == MSC_MODE_SINGLE) {
1025 if (nr_wins != 1)
1026 return -EINVAL;
1027
1028 ret = msc_buffer_contig_alloc(msc, nr_pages[0] << PAGE_SHIFT);
1029 } else if (msc->mode == MSC_MODE_MULTI) {
1030 ret = msc_buffer_multi_alloc(msc, nr_pages, nr_wins);
1031 } else {
1032 ret = -ENOTSUPP;
1033 }
1034
1035 if (!ret) {
1036 /* allocation should be visible before the counter goes to 0 */
1037 smp_mb__before_atomic();
1038
1039 if (WARN_ON_ONCE(atomic_cmpxchg(&msc->user_count, -1, 0) != -1))
1040 return -EINVAL;
1041 }
1042
1043 return ret;
1044}
1045
1046/**
1047 * msc_buffer_unlocked_free_unless_used() - free a buffer unless it's in use
1048 * @msc: MSC device
1049 *
1050 * This will free MSC buffer unless it is in use or there is no allocated
1051 * buffer.
1052 * Caller needs to hold msc::buf_mutex.
1053 *
1054 * Return: 0 on successful deallocation or if there was no buffer to
1055 * deallocate, -EBUSY if there are active users.
1056 */
1057static int msc_buffer_unlocked_free_unless_used(struct msc *msc)
1058{
1059 int count, ret = 0;
1060
1061 count = atomic_cmpxchg(&msc->user_count, 0, -1);
1062
1063 /* > 0: buffer is allocated and has users */
1064 if (count > 0)
1065 ret = -EBUSY;
1066 /* 0: buffer is allocated, no users */
1067 else if (!count)
1068 msc_buffer_free(msc);
1069 /* < 0: no buffer, nothing to do */
1070
1071 return ret;
1072}
1073
1074/**
1075 * msc_buffer_free_unless_used() - free a buffer unless it's in use
1076 * @msc: MSC device
1077 *
1078 * This is a locked version of msc_buffer_unlocked_free_unless_used().
1079 */
1080static int msc_buffer_free_unless_used(struct msc *msc)
1081{
1082 int ret;
1083
1084 mutex_lock(&msc->buf_mutex);
1085 ret = msc_buffer_unlocked_free_unless_used(msc);
1086 mutex_unlock(&msc->buf_mutex);
1087
1088 return ret;
1089}
1090
1091/**
1092 * msc_buffer_get_page() - get MSC buffer page at a given offset
1093 * @msc: MSC device
1094 * @pgoff: page offset into the storage buffer
1095 *
1096 * This traverses msc::win_list, so holding msc::buf_mutex is expected from
1097 * the caller.
1098 *
1099 * Return: page if @pgoff corresponds to a valid buffer page or NULL.
1100 */
1101static struct page *msc_buffer_get_page(struct msc *msc, unsigned long pgoff)
1102{
1103 struct msc_window *win;
1104
1105 if (msc->mode == MSC_MODE_SINGLE)
1106 return msc_buffer_contig_get_page(msc, pgoff);
1107
1108 list_for_each_entry(win, &msc->win_list, entry)
1109 if (pgoff >= win->pgoff && pgoff < win->pgoff + win->nr_blocks)
1110 goto found;
1111
1112 return NULL;
1113
1114found:
1115 pgoff -= win->pgoff;
Alexander Shishkinba39bd82019-05-03 11:44:44 +03001116 return sg_page(&win->sgt.sgl[pgoff]);
Alexander Shishkinba826642015-09-22 15:47:18 +03001117}
1118
1119/**
1120 * struct msc_win_to_user_struct - data for copy_to_user() callback
1121 * @buf: userspace buffer to copy data to
1122 * @offset: running offset
1123 */
1124struct msc_win_to_user_struct {
1125 char __user *buf;
1126 unsigned long offset;
1127};
1128
1129/**
1130 * msc_win_to_user() - iterator for msc_buffer_iterate() to copy data to user
1131 * @data: callback's private data
1132 * @src: source buffer
1133 * @len: amount of data to copy from the source buffer
1134 */
1135static unsigned long msc_win_to_user(void *data, void *src, size_t len)
1136{
1137 struct msc_win_to_user_struct *u = data;
1138 unsigned long ret;
1139
1140 ret = copy_to_user(u->buf + u->offset, src, len);
1141 u->offset += len - ret;
1142
1143 return ret;
1144}
1145
1146
1147/*
1148 * file operations' callbacks
1149 */
1150
1151static int intel_th_msc_open(struct inode *inode, struct file *file)
1152{
1153 struct intel_th_device *thdev = file->private_data;
1154 struct msc *msc = dev_get_drvdata(&thdev->dev);
1155 struct msc_iter *iter;
1156
1157 if (!capable(CAP_SYS_RAWIO))
1158 return -EPERM;
1159
1160 iter = msc_iter_install(msc);
Alexander Shishkina45ff6e2016-03-10 18:21:14 +02001161 if (IS_ERR(iter))
1162 return PTR_ERR(iter);
Alexander Shishkinba826642015-09-22 15:47:18 +03001163
1164 file->private_data = iter;
1165
1166 return nonseekable_open(inode, file);
1167}
1168
1169static int intel_th_msc_release(struct inode *inode, struct file *file)
1170{
1171 struct msc_iter *iter = file->private_data;
1172 struct msc *msc = iter->msc;
1173
1174 msc_iter_remove(iter, msc);
1175
1176 return 0;
1177}
1178
1179static ssize_t
1180msc_single_to_user(struct msc *msc, char __user *buf, loff_t off, size_t len)
1181{
Alexander Shishkined392682015-10-06 12:47:18 +03001182 unsigned long size = msc->nr_pages << PAGE_SHIFT, rem = len;
Alexander Shishkinba826642015-09-22 15:47:18 +03001183 unsigned long start = off, tocopy = 0;
1184
1185 if (msc->single_wrap) {
1186 start += msc->single_sz;
1187 if (start < size) {
1188 tocopy = min(rem, size - start);
1189 if (copy_to_user(buf, msc->base + start, tocopy))
1190 return -EFAULT;
1191
1192 buf += tocopy;
1193 rem -= tocopy;
1194 start += tocopy;
1195 }
1196
1197 start &= size - 1;
1198 if (rem) {
1199 tocopy = min(rem, msc->single_sz - start);
1200 if (copy_to_user(buf, msc->base + start, tocopy))
1201 return -EFAULT;
1202
1203 rem -= tocopy;
1204 }
1205
1206 return len - rem;
1207 }
1208
1209 if (copy_to_user(buf, msc->base + start, rem))
1210 return -EFAULT;
1211
1212 return len;
1213}
1214
1215static ssize_t intel_th_msc_read(struct file *file, char __user *buf,
1216 size_t len, loff_t *ppos)
1217{
1218 struct msc_iter *iter = file->private_data;
1219 struct msc *msc = iter->msc;
1220 size_t size;
1221 loff_t off = *ppos;
1222 ssize_t ret = 0;
1223
1224 if (!atomic_inc_unless_negative(&msc->user_count))
1225 return 0;
1226
Alexander Shishkinba826642015-09-22 15:47:18 +03001227 if (msc->mode == MSC_MODE_SINGLE && !msc->single_wrap)
1228 size = msc->single_sz;
1229 else
1230 size = msc->nr_pages << PAGE_SHIFT;
1231
1232 if (!size)
Alexander Shishkinba826642015-09-22 15:47:18 +03001233 goto put_count;
Laurent FERT2bed0742016-02-15 19:11:57 +02001234
1235 if (off >= size)
1236 goto put_count;
1237
Alexander Shishkinba826642015-09-22 15:47:18 +03001238 if (off + len >= size)
1239 len = size - off;
1240
1241 if (msc->mode == MSC_MODE_SINGLE) {
1242 ret = msc_single_to_user(msc, buf, off, len);
1243 if (ret >= 0)
1244 *ppos += ret;
1245 } else if (msc->mode == MSC_MODE_MULTI) {
1246 struct msc_win_to_user_struct u = {
1247 .buf = buf,
1248 .offset = 0,
1249 };
1250
1251 ret = msc_buffer_iterate(iter, len, &u, msc_win_to_user);
1252 if (ret >= 0)
1253 *ppos = iter->offset;
1254 } else {
1255 ret = -ENOTSUPP;
1256 }
1257
1258put_count:
1259 atomic_dec(&msc->user_count);
1260
1261 return ret;
1262}
1263
1264/*
1265 * vm operations callbacks (vm_ops)
1266 */
1267
1268static void msc_mmap_open(struct vm_area_struct *vma)
1269{
1270 struct msc_iter *iter = vma->vm_file->private_data;
1271 struct msc *msc = iter->msc;
1272
1273 atomic_inc(&msc->mmap_count);
1274}
1275
1276static void msc_mmap_close(struct vm_area_struct *vma)
1277{
1278 struct msc_iter *iter = vma->vm_file->private_data;
1279 struct msc *msc = iter->msc;
1280 unsigned long pg;
1281
1282 if (!atomic_dec_and_mutex_lock(&msc->mmap_count, &msc->buf_mutex))
1283 return;
1284
Joonsoo Kim0139aa72016-05-19 17:10:49 -07001285 /* drop page _refcounts */
Alexander Shishkinba826642015-09-22 15:47:18 +03001286 for (pg = 0; pg < msc->nr_pages; pg++) {
1287 struct page *page = msc_buffer_get_page(msc, pg);
1288
1289 if (WARN_ON_ONCE(!page))
1290 continue;
1291
1292 if (page->mapping)
1293 page->mapping = NULL;
1294 }
1295
1296 /* last mapping -- drop user_count */
1297 atomic_dec(&msc->user_count);
1298 mutex_unlock(&msc->buf_mutex);
1299}
1300
Souptick Joarder42df0502018-08-23 17:00:45 -07001301static vm_fault_t msc_mmap_fault(struct vm_fault *vmf)
Alexander Shishkinba826642015-09-22 15:47:18 +03001302{
Dave Jiang11bac802017-02-24 14:56:41 -08001303 struct msc_iter *iter = vmf->vma->vm_file->private_data;
Alexander Shishkinba826642015-09-22 15:47:18 +03001304 struct msc *msc = iter->msc;
1305
1306 vmf->page = msc_buffer_get_page(msc, vmf->pgoff);
1307 if (!vmf->page)
1308 return VM_FAULT_SIGBUS;
1309
1310 get_page(vmf->page);
Dave Jiang11bac802017-02-24 14:56:41 -08001311 vmf->page->mapping = vmf->vma->vm_file->f_mapping;
Alexander Shishkinba826642015-09-22 15:47:18 +03001312 vmf->page->index = vmf->pgoff;
1313
1314 return 0;
1315}
1316
1317static const struct vm_operations_struct msc_mmap_ops = {
1318 .open = msc_mmap_open,
1319 .close = msc_mmap_close,
1320 .fault = msc_mmap_fault,
1321};
1322
1323static int intel_th_msc_mmap(struct file *file, struct vm_area_struct *vma)
1324{
1325 unsigned long size = vma->vm_end - vma->vm_start;
1326 struct msc_iter *iter = vma->vm_file->private_data;
1327 struct msc *msc = iter->msc;
1328 int ret = -EINVAL;
1329
1330 if (!size || offset_in_page(size))
1331 return -EINVAL;
1332
1333 if (vma->vm_pgoff)
1334 return -EINVAL;
1335
1336 /* grab user_count once per mmap; drop in msc_mmap_close() */
1337 if (!atomic_inc_unless_negative(&msc->user_count))
1338 return -EINVAL;
1339
1340 if (msc->mode != MSC_MODE_SINGLE &&
1341 msc->mode != MSC_MODE_MULTI)
1342 goto out;
1343
1344 if (size >> PAGE_SHIFT != msc->nr_pages)
1345 goto out;
1346
1347 atomic_set(&msc->mmap_count, 1);
1348 ret = 0;
1349
1350out:
1351 if (ret)
1352 atomic_dec(&msc->user_count);
1353
1354 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1355 vma->vm_flags |= VM_DONTEXPAND | VM_DONTCOPY;
1356 vma->vm_ops = &msc_mmap_ops;
1357 return ret;
1358}
1359
1360static const struct file_operations intel_th_msc_fops = {
1361 .open = intel_th_msc_open,
1362 .release = intel_th_msc_release,
1363 .read = intel_th_msc_read,
1364 .mmap = intel_th_msc_mmap,
1365 .llseek = no_llseek,
Alexander Shishkin8e9a2be2016-04-08 17:36:04 +03001366 .owner = THIS_MODULE,
Alexander Shishkinba826642015-09-22 15:47:18 +03001367};
1368
Alexander Shishkin8d415512019-05-03 11:44:46 +03001369static void intel_th_msc_wait_empty(struct intel_th_device *thdev)
1370{
1371 struct msc *msc = dev_get_drvdata(&thdev->dev);
1372 unsigned long count;
1373 u32 reg;
1374
1375 for (reg = 0, count = MSC_PLE_WAITLOOP_DEPTH;
1376 count && !(reg & MSCSTS_PLE); count--) {
1377 reg = __raw_readl(msc->reg_base + REG_MSU_MSC0STS);
1378 cpu_relax();
1379 }
1380
1381 if (!count)
1382 dev_dbg(msc_dev(msc), "timeout waiting for MSC0 PLE\n");
1383}
1384
Alexander Shishkinba826642015-09-22 15:47:18 +03001385static int intel_th_msc_init(struct msc *msc)
1386{
1387 atomic_set(&msc->user_count, -1);
1388
1389 msc->mode = MSC_MODE_MULTI;
1390 mutex_init(&msc->buf_mutex);
1391 INIT_LIST_HEAD(&msc->win_list);
Alexander Shishkinba826642015-09-22 15:47:18 +03001392 INIT_LIST_HEAD(&msc->iter_list);
1393
1394 msc->burst_len =
1395 (ioread32(msc->reg_base + REG_MSU_MSC0CTL) & MSC_LEN) >>
1396 __ffs(MSC_LEN);
1397
1398 return 0;
1399}
1400
Alexander Shishkinaad14ad2019-05-03 11:44:51 +03001401static void msc_win_switch(struct msc *msc)
1402{
YueHaibing9800db22019-06-21 19:19:28 +03001403 struct msc_window *first;
Alexander Shishkinaad14ad2019-05-03 11:44:51 +03001404
1405 first = list_first_entry(&msc->win_list, struct msc_window, entry);
Alexander Shishkinaad14ad2019-05-03 11:44:51 +03001406
1407 if (msc_is_last_win(msc->cur_win))
1408 msc->cur_win = first;
1409 else
1410 msc->cur_win = list_next_entry(msc->cur_win, entry);
1411
1412 msc->base = msc_win_block(msc->cur_win, 0);
1413 msc->base_addr = msc_win_baddr(msc->cur_win, 0);
1414
1415 intel_th_trace_switch(msc->thdev);
1416}
1417
Alexander Shishkinaac8da62019-05-03 11:44:41 +03001418static irqreturn_t intel_th_msc_interrupt(struct intel_th_device *thdev)
1419{
1420 struct msc *msc = dev_get_drvdata(&thdev->dev);
1421 u32 msusts = ioread32(msc->msu_base + REG_MSU_MSUSTS);
1422 u32 mask = msc->index ? MSUSTS_MSC1BLAST : MSUSTS_MSC0BLAST;
1423
1424 if (!(msusts & mask)) {
1425 if (msc->enabled)
1426 return IRQ_HANDLED;
1427 return IRQ_NONE;
1428 }
1429
1430 return IRQ_HANDLED;
1431}
1432
Alexander Shishkinba826642015-09-22 15:47:18 +03001433static const char * const msc_mode[] = {
1434 [MSC_MODE_SINGLE] = "single",
1435 [MSC_MODE_MULTI] = "multi",
1436 [MSC_MODE_EXI] = "ExI",
1437 [MSC_MODE_DEBUG] = "debug",
1438};
1439
1440static ssize_t
1441wrap_show(struct device *dev, struct device_attribute *attr, char *buf)
1442{
1443 struct msc *msc = dev_get_drvdata(dev);
1444
1445 return scnprintf(buf, PAGE_SIZE, "%d\n", msc->wrap);
1446}
1447
1448static ssize_t
1449wrap_store(struct device *dev, struct device_attribute *attr, const char *buf,
1450 size_t size)
1451{
1452 struct msc *msc = dev_get_drvdata(dev);
1453 unsigned long val;
1454 int ret;
1455
1456 ret = kstrtoul(buf, 10, &val);
1457 if (ret)
1458 return ret;
1459
1460 msc->wrap = !!val;
1461
1462 return size;
1463}
1464
1465static DEVICE_ATTR_RW(wrap);
1466
1467static ssize_t
1468mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1469{
1470 struct msc *msc = dev_get_drvdata(dev);
1471
1472 return scnprintf(buf, PAGE_SIZE, "%s\n", msc_mode[msc->mode]);
1473}
1474
1475static ssize_t
1476mode_store(struct device *dev, struct device_attribute *attr, const char *buf,
1477 size_t size)
1478{
1479 struct msc *msc = dev_get_drvdata(dev);
1480 size_t len = size;
1481 char *cp;
1482 int i, ret;
1483
1484 if (!capable(CAP_SYS_RAWIO))
1485 return -EPERM;
1486
1487 cp = memchr(buf, '\n', len);
1488 if (cp)
1489 len = cp - buf;
1490
1491 for (i = 0; i < ARRAY_SIZE(msc_mode); i++)
1492 if (!strncmp(msc_mode[i], buf, len))
1493 goto found;
1494
1495 return -EINVAL;
1496
1497found:
1498 mutex_lock(&msc->buf_mutex);
1499 ret = msc_buffer_unlocked_free_unless_used(msc);
1500 if (!ret)
1501 msc->mode = i;
1502 mutex_unlock(&msc->buf_mutex);
1503
1504 return ret ? ret : size;
1505}
1506
1507static DEVICE_ATTR_RW(mode);
1508
1509static ssize_t
1510nr_pages_show(struct device *dev, struct device_attribute *attr, char *buf)
1511{
1512 struct msc *msc = dev_get_drvdata(dev);
1513 struct msc_window *win;
1514 size_t count = 0;
1515
1516 mutex_lock(&msc->buf_mutex);
1517
1518 if (msc->mode == MSC_MODE_SINGLE)
1519 count = scnprintf(buf, PAGE_SIZE, "%ld\n", msc->nr_pages);
1520 else if (msc->mode == MSC_MODE_MULTI) {
1521 list_for_each_entry(win, &msc->win_list, entry) {
1522 count += scnprintf(buf + count, PAGE_SIZE - count,
1523 "%d%c", win->nr_blocks,
1524 msc_is_last_win(win) ? '\n' : ',');
1525 }
1526 } else {
1527 count = scnprintf(buf, PAGE_SIZE, "unsupported\n");
1528 }
1529
1530 mutex_unlock(&msc->buf_mutex);
1531
1532 return count;
1533}
1534
1535static ssize_t
1536nr_pages_store(struct device *dev, struct device_attribute *attr,
1537 const char *buf, size_t size)
1538{
1539 struct msc *msc = dev_get_drvdata(dev);
1540 unsigned long val, *win = NULL, *rewin;
1541 size_t len = size;
1542 const char *p = buf;
1543 char *end, *s;
1544 int ret, nr_wins = 0;
1545
1546 if (!capable(CAP_SYS_RAWIO))
1547 return -EPERM;
1548
1549 ret = msc_buffer_free_unless_used(msc);
1550 if (ret)
1551 return ret;
1552
1553 /* scan the comma-separated list of allocation sizes */
1554 end = memchr(buf, '\n', len);
1555 if (end)
1556 len = end - buf;
1557
1558 do {
1559 end = memchr(p, ',', len);
1560 s = kstrndup(p, end ? end - p : len, GFP_KERNEL);
Alexander Shishkin6575cbd2016-03-04 16:16:31 +02001561 if (!s) {
1562 ret = -ENOMEM;
1563 goto free_win;
1564 }
1565
Alexander Shishkinba826642015-09-22 15:47:18 +03001566 ret = kstrtoul(s, 10, &val);
1567 kfree(s);
1568
1569 if (ret || !val)
1570 goto free_win;
1571
1572 if (nr_wins && msc->mode == MSC_MODE_SINGLE) {
1573 ret = -EINVAL;
1574 goto free_win;
1575 }
1576
1577 nr_wins++;
1578 rewin = krealloc(win, sizeof(*win) * nr_wins, GFP_KERNEL);
1579 if (!rewin) {
1580 kfree(win);
1581 return -ENOMEM;
1582 }
1583
1584 win = rewin;
1585 win[nr_wins - 1] = val;
1586
1587 if (!end)
1588 break;
1589
Alexander Shishkinec5b5ad2018-12-19 17:19:22 +02001590 /* consume the number and the following comma, hence +1 */
1591 len -= end - p + 1;
Alexander Shishkinba826642015-09-22 15:47:18 +03001592 p = end + 1;
1593 } while (len);
1594
1595 mutex_lock(&msc->buf_mutex);
1596 ret = msc_buffer_alloc(msc, win, nr_wins);
1597 mutex_unlock(&msc->buf_mutex);
1598
1599free_win:
1600 kfree(win);
1601
1602 return ret ? ret : size;
1603}
1604
1605static DEVICE_ATTR_RW(nr_pages);
1606
Alexander Shishkin6cac7862019-05-03 11:44:50 +03001607static ssize_t
1608win_switch_store(struct device *dev, struct device_attribute *attr,
1609 const char *buf, size_t size)
1610{
1611 struct msc *msc = dev_get_drvdata(dev);
1612 unsigned long val;
1613 int ret;
1614
1615 ret = kstrtoul(buf, 10, &val);
1616 if (ret)
1617 return ret;
1618
1619 if (val != 1)
1620 return -EINVAL;
1621
1622 mutex_lock(&msc->buf_mutex);
1623 if (msc->mode != MSC_MODE_MULTI)
1624 ret = -ENOTSUPP;
1625 else
Alexander Shishkinaad14ad2019-05-03 11:44:51 +03001626 msc_win_switch(msc);
Alexander Shishkin6cac7862019-05-03 11:44:50 +03001627 mutex_unlock(&msc->buf_mutex);
1628
1629 return ret ? ret : size;
1630}
1631
1632static DEVICE_ATTR_WO(win_switch);
1633
Alexander Shishkinba826642015-09-22 15:47:18 +03001634static struct attribute *msc_output_attrs[] = {
1635 &dev_attr_wrap.attr,
1636 &dev_attr_mode.attr,
1637 &dev_attr_nr_pages.attr,
Alexander Shishkin6cac7862019-05-03 11:44:50 +03001638 &dev_attr_win_switch.attr,
Alexander Shishkinba826642015-09-22 15:47:18 +03001639 NULL,
1640};
1641
1642static struct attribute_group msc_output_group = {
1643 .attrs = msc_output_attrs,
1644};
1645
1646static int intel_th_msc_probe(struct intel_th_device *thdev)
1647{
1648 struct device *dev = &thdev->dev;
1649 struct resource *res;
1650 struct msc *msc;
1651 void __iomem *base;
1652 int err;
1653
1654 res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
1655 if (!res)
1656 return -ENODEV;
1657
1658 base = devm_ioremap(dev, res->start, resource_size(res));
Dan Carpenter73061da2015-10-16 17:09:13 +03001659 if (!base)
1660 return -ENOMEM;
Alexander Shishkinba826642015-09-22 15:47:18 +03001661
1662 msc = devm_kzalloc(dev, sizeof(*msc), GFP_KERNEL);
1663 if (!msc)
1664 return -ENOMEM;
1665
Alexander Shishkinaac8da62019-05-03 11:44:41 +03001666 res = intel_th_device_get_resource(thdev, IORESOURCE_IRQ, 1);
1667 if (!res)
1668 msc->do_irq = 1;
1669
Alexander Shishkinba826642015-09-22 15:47:18 +03001670 msc->index = thdev->id;
1671
1672 msc->thdev = thdev;
1673 msc->reg_base = base + msc->index * 0x100;
Alexander Shishkinaac8da62019-05-03 11:44:41 +03001674 msc->msu_base = base;
1675
1676 err = intel_th_msu_init(msc);
1677 if (err)
1678 return err;
Alexander Shishkinba826642015-09-22 15:47:18 +03001679
1680 err = intel_th_msc_init(msc);
1681 if (err)
1682 return err;
1683
Alexander Shishkinba826642015-09-22 15:47:18 +03001684 dev_set_drvdata(dev, msc);
1685
1686 return 0;
1687}
1688
1689static void intel_th_msc_remove(struct intel_th_device *thdev)
1690{
Alexander Shishkinf152dfe2016-04-08 17:37:40 +03001691 struct msc *msc = dev_get_drvdata(&thdev->dev);
1692 int ret;
1693
1694 intel_th_msc_deactivate(thdev);
Alexander Shishkinaac8da62019-05-03 11:44:41 +03001695 intel_th_msu_deinit(msc);
Alexander Shishkinf152dfe2016-04-08 17:37:40 +03001696
1697 /*
1698 * Buffers should not be used at this point except if the
1699 * output character device is still open and the parent
1700 * device gets detached from its bus, which is a FIXME.
1701 */
1702 ret = msc_buffer_free_unless_used(msc);
1703 WARN_ON_ONCE(ret);
Alexander Shishkinba826642015-09-22 15:47:18 +03001704}
1705
1706static struct intel_th_driver intel_th_msc_driver = {
1707 .probe = intel_th_msc_probe,
1708 .remove = intel_th_msc_remove,
Alexander Shishkinaac8da62019-05-03 11:44:41 +03001709 .irq = intel_th_msc_interrupt,
Alexander Shishkin8d415512019-05-03 11:44:46 +03001710 .wait_empty = intel_th_msc_wait_empty,
Alexander Shishkinba826642015-09-22 15:47:18 +03001711 .activate = intel_th_msc_activate,
1712 .deactivate = intel_th_msc_deactivate,
1713 .fops = &intel_th_msc_fops,
Alexander Shishkin9d482ae2016-03-04 19:55:10 +02001714 .attr_group = &msc_output_group,
Alexander Shishkinba826642015-09-22 15:47:18 +03001715 .driver = {
1716 .name = "msc",
1717 .owner = THIS_MODULE,
1718 },
1719};
1720
1721module_driver(intel_th_msc_driver,
1722 intel_th_driver_register,
1723 intel_th_driver_unregister);
1724
1725MODULE_LICENSE("GPL v2");
1726MODULE_DESCRIPTION("Intel(R) Trace Hub Memory Storage Unit driver");
1727MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");