msu.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509
  1. /*
  2. * Intel(R) Trace Hub Memory Storage Unit
  3. *
  4. * Copyright (C) 2014-2015 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/types.h>
  17. #include <linux/module.h>
  18. #include <linux/device.h>
  19. #include <linux/uaccess.h>
  20. #include <linux/sizes.h>
  21. #include <linux/printk.h>
  22. #include <linux/slab.h>
  23. #include <linux/mm.h>
  24. #include <linux/fs.h>
  25. #include <linux/io.h>
  26. #include <linux/dma-mapping.h>
  27. #include <asm/cacheflush.h>
  28. #include "intel_th.h"
  29. #include "msu.h"
  30. #define msc_dev(x) (&(x)->thdev->dev)
  31. /**
  32. * struct msc_block - multiblock mode block descriptor
  33. * @bdesc: pointer to hardware descriptor (beginning of the block)
  34. * @addr: physical address of the block
  35. */
  36. struct msc_block {
  37. struct msc_block_desc *bdesc;
  38. dma_addr_t addr;
  39. };
  40. /**
  41. * struct msc_window - multiblock mode window descriptor
  42. * @entry: window list linkage (msc::win_list)
  43. * @pgoff: page offset into the buffer that this window starts at
  44. * @nr_blocks: number of blocks (pages) in this window
  45. * @block: array of block descriptors
  46. */
  47. struct msc_window {
  48. struct list_head entry;
  49. unsigned long pgoff;
  50. unsigned int nr_blocks;
  51. struct msc *msc;
  52. struct msc_block block[0];
  53. };
  54. /**
  55. * struct msc_iter - iterator for msc buffer
  56. * @entry: msc::iter_list linkage
  57. * @msc: pointer to the MSC device
  58. * @start_win: oldest window
  59. * @win: current window
  60. * @offset: current logical offset into the buffer
  61. * @start_block: oldest block in the window
  62. * @block: block number in the window
  63. * @block_off: offset into current block
  64. * @wrap_count: block wrapping handling
  65. * @eof: end of buffer reached
  66. */
  67. struct msc_iter {
  68. struct list_head entry;
  69. struct msc *msc;
  70. struct msc_window *start_win;
  71. struct msc_window *win;
  72. unsigned long offset;
  73. int start_block;
  74. int block;
  75. unsigned int block_off;
  76. unsigned int wrap_count;
  77. unsigned int eof;
  78. };
  79. /**
  80. * struct msc - MSC device representation
  81. * @reg_base: register window base address
  82. * @thdev: intel_th_device pointer
  83. * @win_list: list of windows in multiblock mode
  84. * @nr_pages: total number of pages allocated for this buffer
  85. * @single_sz: amount of data in single mode
  86. * @single_wrap: single mode wrap occurred
  87. * @base: buffer's base pointer
  88. * @base_addr: buffer's base address
  89. * @user_count: number of users of the buffer
  90. * @mmap_count: number of mappings
  91. * @buf_mutex: mutex to serialize access to buffer-related bits
  92. * @enabled: MSC is enabled
  93. * @wrap: wrapping is enabled
  94. * @mode: MSC operating mode
  95. * @burst_len: write burst length
  96. * @index: number of this MSC in the MSU
  97. */
  98. struct msc {
  99. void __iomem *reg_base;
  100. struct intel_th_device *thdev;
  101. struct list_head win_list;
  102. 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. /* <0: no buffer, 0: no users, >0: active users */
  108. atomic_t user_count;
  109. atomic_t mmap_count;
  110. struct mutex buf_mutex;
  111. struct mutex iter_mutex;
  112. struct list_head iter_list;
  113. /* config */
  114. unsigned int enabled : 1,
  115. wrap : 1;
  116. unsigned int mode;
  117. unsigned int burst_len;
  118. unsigned int index;
  119. };
  120. static inline bool msc_block_is_empty(struct msc_block_desc *bdesc)
  121. {
  122. /* header hasn't been written */
  123. if (!bdesc->valid_dw)
  124. return true;
  125. /* valid_dw includes the header */
  126. if (!msc_data_sz(bdesc))
  127. return true;
  128. return false;
  129. }
  130. /**
  131. * msc_oldest_window() - locate the window with oldest data
  132. * @msc: MSC device
  133. *
  134. * This should only be used in multiblock mode. Caller should hold the
  135. * msc::user_count reference.
  136. *
  137. * Return: the oldest window with valid data
  138. */
  139. static struct msc_window *msc_oldest_window(struct msc *msc)
  140. {
  141. struct msc_window *win;
  142. u32 reg = ioread32(msc->reg_base + REG_MSU_MSC0NWSA);
  143. unsigned long win_addr = (unsigned long)reg << PAGE_SHIFT;
  144. unsigned int found = 0;
  145. if (list_empty(&msc->win_list))
  146. return NULL;
  147. /*
  148. * we might need a radix tree for this, depending on how
  149. * many windows a typical user would allocate; ideally it's
  150. * something like 2, in which case we're good
  151. */
  152. list_for_each_entry(win, &msc->win_list, entry) {
  153. if (win->block[0].addr == win_addr)
  154. found++;
  155. /* skip the empty ones */
  156. if (msc_block_is_empty(win->block[0].bdesc))
  157. continue;
  158. if (found)
  159. return win;
  160. }
  161. return list_entry(msc->win_list.next, struct msc_window, entry);
  162. }
  163. /**
  164. * msc_win_oldest_block() - locate the oldest block in a given window
  165. * @win: window to look at
  166. *
  167. * Return: index of the block with the oldest data
  168. */
  169. static unsigned int msc_win_oldest_block(struct msc_window *win)
  170. {
  171. unsigned int blk;
  172. struct msc_block_desc *bdesc = win->block[0].bdesc;
  173. /* without wrapping, first block is the oldest */
  174. if (!msc_block_wrapped(bdesc))
  175. return 0;
  176. /*
  177. * with wrapping, last written block contains both the newest and the
  178. * oldest data for this window.
  179. */
  180. for (blk = 0; blk < win->nr_blocks; blk++) {
  181. bdesc = win->block[blk].bdesc;
  182. if (msc_block_last_written(bdesc))
  183. return blk;
  184. }
  185. return 0;
  186. }
  187. /**
  188. * msc_is_last_win() - check if a window is the last one for a given MSC
  189. * @win: window
  190. * Return: true if @win is the last window in MSC's multiblock buffer
  191. */
  192. static inline bool msc_is_last_win(struct msc_window *win)
  193. {
  194. return win->entry.next == &win->msc->win_list;
  195. }
  196. /**
  197. * msc_next_window() - return next window in the multiblock buffer
  198. * @win: current window
  199. *
  200. * Return: window following the current one
  201. */
  202. static struct msc_window *msc_next_window(struct msc_window *win)
  203. {
  204. if (msc_is_last_win(win))
  205. return list_entry(win->msc->win_list.next, struct msc_window,
  206. entry);
  207. return list_entry(win->entry.next, struct msc_window, entry);
  208. }
  209. static struct msc_block_desc *msc_iter_bdesc(struct msc_iter *iter)
  210. {
  211. return iter->win->block[iter->block].bdesc;
  212. }
  213. static void msc_iter_init(struct msc_iter *iter)
  214. {
  215. memset(iter, 0, sizeof(*iter));
  216. iter->start_block = -1;
  217. iter->block = -1;
  218. }
  219. static struct msc_iter *msc_iter_install(struct msc *msc)
  220. {
  221. struct msc_iter *iter;
  222. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  223. if (!iter)
  224. return NULL;
  225. msc_iter_init(iter);
  226. iter->msc = msc;
  227. mutex_lock(&msc->iter_mutex);
  228. list_add_tail(&iter->entry, &msc->iter_list);
  229. mutex_unlock(&msc->iter_mutex);
  230. return iter;
  231. }
  232. static void msc_iter_remove(struct msc_iter *iter, struct msc *msc)
  233. {
  234. mutex_lock(&msc->iter_mutex);
  235. list_del(&iter->entry);
  236. mutex_unlock(&msc->iter_mutex);
  237. kfree(iter);
  238. }
  239. static void msc_iter_block_start(struct msc_iter *iter)
  240. {
  241. if (iter->start_block != -1)
  242. return;
  243. iter->start_block = msc_win_oldest_block(iter->win);
  244. iter->block = iter->start_block;
  245. iter->wrap_count = 0;
  246. /*
  247. * start with the block with oldest data; if data has wrapped
  248. * in this window, it should be in this block
  249. */
  250. if (msc_block_wrapped(msc_iter_bdesc(iter)))
  251. iter->wrap_count = 2;
  252. }
  253. static int msc_iter_win_start(struct msc_iter *iter, struct msc *msc)
  254. {
  255. /* already started, nothing to do */
  256. if (iter->start_win)
  257. return 0;
  258. iter->start_win = msc_oldest_window(msc);
  259. if (!iter->start_win)
  260. return -EINVAL;
  261. iter->win = iter->start_win;
  262. iter->start_block = -1;
  263. msc_iter_block_start(iter);
  264. return 0;
  265. }
  266. static int msc_iter_win_advance(struct msc_iter *iter)
  267. {
  268. iter->win = msc_next_window(iter->win);
  269. iter->start_block = -1;
  270. if (iter->win == iter->start_win) {
  271. iter->eof++;
  272. return 1;
  273. }
  274. msc_iter_block_start(iter);
  275. return 0;
  276. }
  277. static int msc_iter_block_advance(struct msc_iter *iter)
  278. {
  279. iter->block_off = 0;
  280. /* wrapping */
  281. if (iter->wrap_count && iter->block == iter->start_block) {
  282. iter->wrap_count--;
  283. if (!iter->wrap_count)
  284. /* copied newest data from the wrapped block */
  285. return msc_iter_win_advance(iter);
  286. }
  287. /* no wrapping, check for last written block */
  288. if (!iter->wrap_count && msc_block_last_written(msc_iter_bdesc(iter)))
  289. /* copied newest data for the window */
  290. return msc_iter_win_advance(iter);
  291. /* block advance */
  292. if (++iter->block == iter->win->nr_blocks)
  293. iter->block = 0;
  294. /* no wrapping, sanity check in case there is no last written block */
  295. if (!iter->wrap_count && iter->block == iter->start_block)
  296. return msc_iter_win_advance(iter);
  297. return 0;
  298. }
  299. /**
  300. * msc_buffer_iterate() - go through multiblock buffer's data
  301. * @iter: iterator structure
  302. * @size: amount of data to scan
  303. * @data: callback's private data
  304. * @fn: iterator callback
  305. *
  306. * This will start at the window which will be written to next (containing
  307. * the oldest data) and work its way to the current window, calling @fn
  308. * for each chunk of data as it goes.
  309. *
  310. * Caller should have msc::user_count reference to make sure the buffer
  311. * doesn't disappear from under us.
  312. *
  313. * Return: amount of data actually scanned.
  314. */
  315. static ssize_t
  316. msc_buffer_iterate(struct msc_iter *iter, size_t size, void *data,
  317. unsigned long (*fn)(void *, void *, size_t))
  318. {
  319. struct msc *msc = iter->msc;
  320. size_t len = size;
  321. unsigned int advance;
  322. if (iter->eof)
  323. return 0;
  324. /* start with the oldest window */
  325. if (msc_iter_win_start(iter, msc))
  326. return 0;
  327. do {
  328. unsigned long data_bytes = msc_data_sz(msc_iter_bdesc(iter));
  329. void *src = (void *)msc_iter_bdesc(iter) + MSC_BDESC;
  330. size_t tocopy = data_bytes, copied = 0;
  331. size_t remaining = 0;
  332. advance = 1;
  333. /*
  334. * If block wrapping happened, we need to visit the last block
  335. * twice, because it contains both the oldest and the newest
  336. * data in this window.
  337. *
  338. * First time (wrap_count==2), in the very beginning, to collect
  339. * the oldest data, which is in the range
  340. * (data_bytes..DATA_IN_PAGE).
  341. *
  342. * Second time (wrap_count==1), it's just like any other block,
  343. * containing data in the range of [MSC_BDESC..data_bytes].
  344. */
  345. if (iter->block == iter->start_block && iter->wrap_count) {
  346. tocopy = DATA_IN_PAGE - data_bytes;
  347. src += data_bytes;
  348. }
  349. if (!tocopy)
  350. goto next_block;
  351. tocopy -= iter->block_off;
  352. src += iter->block_off;
  353. if (len < tocopy) {
  354. tocopy = len;
  355. advance = 0;
  356. }
  357. remaining = fn(data, src, tocopy);
  358. if (remaining)
  359. advance = 0;
  360. copied = tocopy - remaining;
  361. len -= copied;
  362. iter->block_off += copied;
  363. iter->offset += copied;
  364. if (!advance)
  365. break;
  366. next_block:
  367. if (msc_iter_block_advance(iter))
  368. break;
  369. } while (len);
  370. return size - len;
  371. }
  372. /**
  373. * msc_buffer_clear_hw_header() - clear hw header for multiblock
  374. * @msc: MSC device
  375. */
  376. static void msc_buffer_clear_hw_header(struct msc *msc)
  377. {
  378. struct msc_window *win;
  379. mutex_lock(&msc->buf_mutex);
  380. list_for_each_entry(win, &msc->win_list, entry) {
  381. unsigned int blk;
  382. size_t hw_sz = sizeof(struct msc_block_desc) -
  383. offsetof(struct msc_block_desc, hw_tag);
  384. for (blk = 0; blk < win->nr_blocks; blk++) {
  385. struct msc_block_desc *bdesc = win->block[blk].bdesc;
  386. memset(&bdesc->hw_tag, 0, hw_sz);
  387. }
  388. }
  389. mutex_unlock(&msc->buf_mutex);
  390. }
  391. /**
  392. * msc_configure() - set up MSC hardware
  393. * @msc: the MSC device to configure
  394. *
  395. * Program storage mode, wrapping, burst length and trace buffer address
  396. * into a given MSC. If msc::enabled is set, enable the trace, too.
  397. */
  398. static int msc_configure(struct msc *msc)
  399. {
  400. u32 reg;
  401. if (msc->mode > MSC_MODE_MULTI)
  402. return -ENOTSUPP;
  403. if (msc->mode == MSC_MODE_MULTI)
  404. msc_buffer_clear_hw_header(msc);
  405. reg = msc->base_addr >> PAGE_SHIFT;
  406. iowrite32(reg, msc->reg_base + REG_MSU_MSC0BAR);
  407. if (msc->mode == MSC_MODE_SINGLE) {
  408. reg = msc->nr_pages;
  409. iowrite32(reg, msc->reg_base + REG_MSU_MSC0SIZE);
  410. }
  411. reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
  412. reg &= ~(MSC_MODE | MSC_WRAPEN | MSC_EN | MSC_RD_HDR_OVRD);
  413. reg |= msc->mode << __ffs(MSC_MODE);
  414. reg |= msc->burst_len << __ffs(MSC_LEN);
  415. /*if (msc->mode == MSC_MODE_MULTI)
  416. reg |= MSC_RD_HDR_OVRD; */
  417. if (msc->wrap)
  418. reg |= MSC_WRAPEN;
  419. if (msc->enabled)
  420. reg |= MSC_EN;
  421. iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
  422. if (msc->enabled) {
  423. msc->thdev->output.multiblock = msc->mode == MSC_MODE_MULTI;
  424. intel_th_trace_enable(msc->thdev);
  425. }
  426. return 0;
  427. }
  428. /**
  429. * msc_disable() - disable MSC hardware
  430. * @msc: MSC device to disable
  431. *
  432. * If @msc is enabled, disable tracing on the switch and then disable MSC
  433. * storage.
  434. */
  435. static void msc_disable(struct msc *msc)
  436. {
  437. unsigned long count;
  438. u32 reg;
  439. if (!msc->enabled)
  440. return;
  441. intel_th_trace_disable(msc->thdev);
  442. for (reg = 0, count = MSC_PLE_WAITLOOP_DEPTH;
  443. count && !(reg & MSCSTS_PLE); count--) {
  444. reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
  445. cpu_relax();
  446. }
  447. if (!count)
  448. dev_dbg(msc_dev(msc), "timeout waiting for MSC0 PLE\n");
  449. if (msc->mode == MSC_MODE_SINGLE) {
  450. msc->single_wrap = !!(reg & MSCSTS_WRAPSTAT);
  451. reg = ioread32(msc->reg_base + REG_MSU_MSC0MWP);
  452. msc->single_sz = reg & ((msc->nr_pages << PAGE_SHIFT) - 1);
  453. dev_dbg(msc_dev(msc), "MSCnMWP: %08x/%08lx, wrap: %d\n",
  454. reg, msc->single_sz, msc->single_wrap);
  455. }
  456. reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
  457. reg &= ~MSC_EN;
  458. iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
  459. msc->enabled = 0;
  460. iowrite32(0, msc->reg_base + REG_MSU_MSC0BAR);
  461. iowrite32(0, msc->reg_base + REG_MSU_MSC0SIZE);
  462. dev_dbg(msc_dev(msc), "MSCnNWSA: %08x\n",
  463. ioread32(msc->reg_base + REG_MSU_MSC0NWSA));
  464. reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
  465. dev_dbg(msc_dev(msc), "MSCnSTS: %08x\n", reg);
  466. }
  467. static int intel_th_msc_activate(struct intel_th_device *thdev)
  468. {
  469. struct msc *msc = dev_get_drvdata(&thdev->dev);
  470. int ret = 0;
  471. if (!atomic_inc_unless_negative(&msc->user_count))
  472. return -ENODEV;
  473. mutex_lock(&msc->iter_mutex);
  474. if (!list_empty(&msc->iter_list))
  475. ret = -EBUSY;
  476. mutex_unlock(&msc->iter_mutex);
  477. if (ret) {
  478. atomic_dec(&msc->user_count);
  479. return ret;
  480. }
  481. msc->enabled = 1;
  482. return msc_configure(msc);
  483. }
  484. static void intel_th_msc_deactivate(struct intel_th_device *thdev)
  485. {
  486. struct msc *msc = dev_get_drvdata(&thdev->dev);
  487. msc_disable(msc);
  488. atomic_dec(&msc->user_count);
  489. }
  490. /**
  491. * msc_buffer_contig_alloc() - allocate a contiguous buffer for SINGLE mode
  492. * @msc: MSC device
  493. * @size: allocation size in bytes
  494. *
  495. * This modifies msc::base, which requires msc::buf_mutex to serialize, so the
  496. * caller is expected to hold it.
  497. *
  498. * Return: 0 on success, -errno otherwise.
  499. */
  500. static int msc_buffer_contig_alloc(struct msc *msc, unsigned long size)
  501. {
  502. unsigned int order = get_order(size);
  503. struct page *page;
  504. if (!size)
  505. return 0;
  506. page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
  507. if (!page)
  508. return -ENOMEM;
  509. split_page(page, order);
  510. msc->nr_pages = size >> PAGE_SHIFT;
  511. msc->base = page_address(page);
  512. msc->base_addr = page_to_phys(page);
  513. return 0;
  514. }
  515. /**
  516. * msc_buffer_contig_free() - free a contiguous buffer
  517. * @msc: MSC configured in SINGLE mode
  518. */
  519. static void msc_buffer_contig_free(struct msc *msc)
  520. {
  521. unsigned long off;
  522. for (off = 0; off < msc->nr_pages << PAGE_SHIFT; off += PAGE_SIZE) {
  523. struct page *page = virt_to_page(msc->base + off);
  524. page->mapping = NULL;
  525. __free_page(page);
  526. }
  527. msc->nr_pages = 0;
  528. }
  529. /**
  530. * msc_buffer_contig_get_page() - find a page at a given offset
  531. * @msc: MSC configured in SINGLE mode
  532. * @pgoff: page offset
  533. *
  534. * Return: page, if @pgoff is within the range, NULL otherwise.
  535. */
  536. static struct page *msc_buffer_contig_get_page(struct msc *msc,
  537. unsigned long pgoff)
  538. {
  539. if (pgoff >= msc->nr_pages)
  540. return NULL;
  541. return virt_to_page(msc->base + (pgoff << PAGE_SHIFT));
  542. }
  543. /**
  544. * msc_buffer_win_alloc() - alloc a window for a multiblock mode
  545. * @msc: MSC device
  546. * @nr_blocks: number of pages in this window
  547. *
  548. * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
  549. * to serialize, so the caller is expected to hold it.
  550. *
  551. * Return: 0 on success, -errno otherwise.
  552. */
  553. static int msc_buffer_win_alloc(struct msc *msc, unsigned int nr_blocks)
  554. {
  555. struct msc_window *win;
  556. unsigned long size = PAGE_SIZE;
  557. int i, ret = -ENOMEM;
  558. if (!nr_blocks)
  559. return 0;
  560. win = kzalloc(offsetof(struct msc_window, block[nr_blocks]),
  561. GFP_KERNEL);
  562. if (!win)
  563. return -ENOMEM;
  564. if (!list_empty(&msc->win_list)) {
  565. struct msc_window *prev = list_entry(msc->win_list.prev,
  566. struct msc_window, entry);
  567. win->pgoff = prev->pgoff + prev->nr_blocks;
  568. }
  569. for (i = 0; i < nr_blocks; i++) {
  570. win->block[i].bdesc = dma_alloc_coherent(msc_dev(msc), size,
  571. &win->block[i].addr,
  572. GFP_KERNEL);
  573. #ifdef CONFIG_X86
  574. /* Set the page as uncached */
  575. set_memory_uc((unsigned long)win->block[i].bdesc, 1);
  576. #endif
  577. if (!win->block[i].bdesc)
  578. goto err_nomem;
  579. }
  580. win->msc = msc;
  581. win->nr_blocks = nr_blocks;
  582. if (list_empty(&msc->win_list)) {
  583. msc->base = win->block[0].bdesc;
  584. msc->base_addr = win->block[0].addr;
  585. }
  586. list_add_tail(&win->entry, &msc->win_list);
  587. msc->nr_pages += nr_blocks;
  588. return 0;
  589. err_nomem:
  590. for (i--; i >= 0; i--) {
  591. #ifdef CONFIG_X86
  592. /* Reset the page to write-back before releasing */
  593. set_memory_wb((unsigned long)win->block[i].bdesc, 1);
  594. #endif
  595. dma_free_coherent(msc_dev(msc), size, win->block[i].bdesc,
  596. win->block[i].addr);
  597. }
  598. kfree(win);
  599. return ret;
  600. }
  601. /**
  602. * msc_buffer_win_free() - free a window from MSC's window list
  603. * @msc: MSC device
  604. * @win: window to free
  605. *
  606. * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
  607. * to serialize, so the caller is expected to hold it.
  608. */
  609. static void msc_buffer_win_free(struct msc *msc, struct msc_window *win)
  610. {
  611. int i;
  612. msc->nr_pages -= win->nr_blocks;
  613. list_del(&win->entry);
  614. if (list_empty(&msc->win_list)) {
  615. msc->base = NULL;
  616. msc->base_addr = 0;
  617. }
  618. for (i = 0; i < win->nr_blocks; i++) {
  619. struct page *page = virt_to_page(win->block[i].bdesc);
  620. page->mapping = NULL;
  621. #ifdef CONFIG_X86
  622. /* Reset the page to write-back before releasing */
  623. set_memory_wb((unsigned long)win->block[i].bdesc, 1);
  624. #endif
  625. dma_free_coherent(msc_dev(win->msc), PAGE_SIZE,
  626. win->block[i].bdesc, win->block[i].addr);
  627. }
  628. kfree(win);
  629. }
  630. /**
  631. * msc_buffer_relink() - set up block descriptors for multiblock mode
  632. * @msc: MSC device
  633. *
  634. * This traverses msc::win_list, which requires msc::buf_mutex to serialize,
  635. * so the caller is expected to hold it.
  636. */
  637. static void msc_buffer_relink(struct msc *msc)
  638. {
  639. struct msc_window *win, *next_win;
  640. /* call with msc::mutex locked */
  641. list_for_each_entry(win, &msc->win_list, entry) {
  642. unsigned int blk;
  643. u32 sw_tag = 0;
  644. /*
  645. * Last window's next_win should point to the first window
  646. * and MSC_SW_TAG_LASTWIN should be set.
  647. */
  648. if (msc_is_last_win(win)) {
  649. sw_tag |= MSC_SW_TAG_LASTWIN;
  650. next_win = list_entry(msc->win_list.next,
  651. struct msc_window, entry);
  652. } else {
  653. next_win = list_entry(win->entry.next,
  654. struct msc_window, entry);
  655. }
  656. for (blk = 0; blk < win->nr_blocks; blk++) {
  657. struct msc_block_desc *bdesc = win->block[blk].bdesc;
  658. memset(bdesc, 0, sizeof(*bdesc));
  659. bdesc->next_win = next_win->block[0].addr >> PAGE_SHIFT;
  660. /*
  661. * Similarly to last window, last block should point
  662. * to the first one.
  663. */
  664. if (blk == win->nr_blocks - 1) {
  665. sw_tag |= MSC_SW_TAG_LASTBLK;
  666. bdesc->next_blk =
  667. win->block[0].addr >> PAGE_SHIFT;
  668. } else {
  669. bdesc->next_blk =
  670. win->block[blk + 1].addr >> PAGE_SHIFT;
  671. }
  672. bdesc->sw_tag = sw_tag;
  673. bdesc->block_sz = PAGE_SIZE / 64;
  674. }
  675. }
  676. /*
  677. * Make the above writes globally visible before tracing is
  678. * enabled to make sure hardware sees them coherently.
  679. */
  680. wmb();
  681. }
  682. static void msc_buffer_multi_free(struct msc *msc)
  683. {
  684. struct msc_window *win, *iter;
  685. list_for_each_entry_safe(win, iter, &msc->win_list, entry)
  686. msc_buffer_win_free(msc, win);
  687. }
  688. static int msc_buffer_multi_alloc(struct msc *msc, unsigned long *nr_pages,
  689. unsigned int nr_wins)
  690. {
  691. int ret, i;
  692. for (i = 0; i < nr_wins; i++) {
  693. ret = msc_buffer_win_alloc(msc, nr_pages[i]);
  694. if (ret) {
  695. msc_buffer_multi_free(msc);
  696. return ret;
  697. }
  698. }
  699. msc_buffer_relink(msc);
  700. return 0;
  701. }
  702. /**
  703. * msc_buffer_free() - free buffers for MSC
  704. * @msc: MSC device
  705. *
  706. * Free MSC's storage buffers.
  707. *
  708. * This modifies msc::win_list and msc::base, which requires msc::buf_mutex to
  709. * serialize, so the caller is expected to hold it.
  710. */
  711. static void msc_buffer_free(struct msc *msc)
  712. {
  713. if (msc->mode == MSC_MODE_SINGLE)
  714. msc_buffer_contig_free(msc);
  715. else if (msc->mode == MSC_MODE_MULTI)
  716. msc_buffer_multi_free(msc);
  717. }
  718. /**
  719. * msc_buffer_alloc() - allocate a buffer for MSC
  720. * @msc: MSC device
  721. * @size: allocation size in bytes
  722. *
  723. * Allocate a storage buffer for MSC, depending on the msc::mode, it will be
  724. * either done via msc_buffer_contig_alloc() for SINGLE operation mode or
  725. * msc_buffer_win_alloc() for multiblock operation. The latter allocates one
  726. * window per invocation, so in multiblock mode this can be called multiple
  727. * times for the same MSC to allocate multiple windows.
  728. *
  729. * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
  730. * to serialize, so the caller is expected to hold it.
  731. *
  732. * Return: 0 on success, -errno otherwise.
  733. */
  734. static int msc_buffer_alloc(struct msc *msc, unsigned long *nr_pages,
  735. unsigned int nr_wins)
  736. {
  737. int ret;
  738. /* -1: buffer not allocated */
  739. if (atomic_read(&msc->user_count) != -1)
  740. return -EBUSY;
  741. if (msc->mode == MSC_MODE_SINGLE) {
  742. if (nr_wins != 1)
  743. return -EINVAL;
  744. ret = msc_buffer_contig_alloc(msc, nr_pages[0] << PAGE_SHIFT);
  745. } else if (msc->mode == MSC_MODE_MULTI) {
  746. ret = msc_buffer_multi_alloc(msc, nr_pages, nr_wins);
  747. } else {
  748. ret = -ENOTSUPP;
  749. }
  750. if (!ret) {
  751. /* allocation should be visible before the counter goes to 0 */
  752. smp_mb__before_atomic();
  753. if (WARN_ON_ONCE(atomic_cmpxchg(&msc->user_count, -1, 0) != -1))
  754. return -EINVAL;
  755. }
  756. return ret;
  757. }
  758. /**
  759. * msc_buffer_unlocked_free_unless_used() - free a buffer unless it's in use
  760. * @msc: MSC device
  761. *
  762. * This will free MSC buffer unless it is in use or there is no allocated
  763. * buffer.
  764. * Caller needs to hold msc::buf_mutex.
  765. *
  766. * Return: 0 on successful deallocation or if there was no buffer to
  767. * deallocate, -EBUSY if there are active users.
  768. */
  769. static int msc_buffer_unlocked_free_unless_used(struct msc *msc)
  770. {
  771. int count, ret = 0;
  772. count = atomic_cmpxchg(&msc->user_count, 0, -1);
  773. /* > 0: buffer is allocated and has users */
  774. if (count > 0)
  775. ret = -EBUSY;
  776. /* 0: buffer is allocated, no users */
  777. else if (!count)
  778. msc_buffer_free(msc);
  779. /* < 0: no buffer, nothing to do */
  780. return ret;
  781. }
  782. /**
  783. * msc_buffer_free_unless_used() - free a buffer unless it's in use
  784. * @msc: MSC device
  785. *
  786. * This is a locked version of msc_buffer_unlocked_free_unless_used().
  787. */
  788. static int msc_buffer_free_unless_used(struct msc *msc)
  789. {
  790. int ret;
  791. mutex_lock(&msc->buf_mutex);
  792. ret = msc_buffer_unlocked_free_unless_used(msc);
  793. mutex_unlock(&msc->buf_mutex);
  794. return ret;
  795. }
  796. /**
  797. * msc_buffer_get_page() - get MSC buffer page at a given offset
  798. * @msc: MSC device
  799. * @pgoff: page offset into the storage buffer
  800. *
  801. * This traverses msc::win_list, so holding msc::buf_mutex is expected from
  802. * the caller.
  803. *
  804. * Return: page if @pgoff corresponds to a valid buffer page or NULL.
  805. */
  806. static struct page *msc_buffer_get_page(struct msc *msc, unsigned long pgoff)
  807. {
  808. struct msc_window *win;
  809. if (msc->mode == MSC_MODE_SINGLE)
  810. return msc_buffer_contig_get_page(msc, pgoff);
  811. list_for_each_entry(win, &msc->win_list, entry)
  812. if (pgoff >= win->pgoff && pgoff < win->pgoff + win->nr_blocks)
  813. goto found;
  814. return NULL;
  815. found:
  816. pgoff -= win->pgoff;
  817. return virt_to_page(win->block[pgoff].bdesc);
  818. }
  819. /**
  820. * struct msc_win_to_user_struct - data for copy_to_user() callback
  821. * @buf: userspace buffer to copy data to
  822. * @offset: running offset
  823. */
  824. struct msc_win_to_user_struct {
  825. char __user *buf;
  826. unsigned long offset;
  827. };
  828. /**
  829. * msc_win_to_user() - iterator for msc_buffer_iterate() to copy data to user
  830. * @data: callback's private data
  831. * @src: source buffer
  832. * @len: amount of data to copy from the source buffer
  833. */
  834. static unsigned long msc_win_to_user(void *data, void *src, size_t len)
  835. {
  836. struct msc_win_to_user_struct *u = data;
  837. unsigned long ret;
  838. ret = copy_to_user(u->buf + u->offset, src, len);
  839. u->offset += len - ret;
  840. return ret;
  841. }
  842. /*
  843. * file operations' callbacks
  844. */
  845. static int intel_th_msc_open(struct inode *inode, struct file *file)
  846. {
  847. struct intel_th_device *thdev = file->private_data;
  848. struct msc *msc = dev_get_drvdata(&thdev->dev);
  849. struct msc_iter *iter;
  850. if (!capable(CAP_SYS_RAWIO))
  851. return -EPERM;
  852. iter = msc_iter_install(msc);
  853. if (!iter)
  854. return -ENOMEM;
  855. file->private_data = iter;
  856. return nonseekable_open(inode, file);
  857. }
  858. static int intel_th_msc_release(struct inode *inode, struct file *file)
  859. {
  860. struct msc_iter *iter = file->private_data;
  861. struct msc *msc = iter->msc;
  862. msc_iter_remove(iter, msc);
  863. return 0;
  864. }
  865. static ssize_t
  866. msc_single_to_user(struct msc *msc, char __user *buf, loff_t off, size_t len)
  867. {
  868. unsigned long size = msc->nr_pages << PAGE_SHIFT, rem = len;
  869. unsigned long start = off, tocopy = 0;
  870. if (msc->single_wrap) {
  871. start += msc->single_sz;
  872. if (start < size) {
  873. tocopy = min(rem, size - start);
  874. if (copy_to_user(buf, msc->base + start, tocopy))
  875. return -EFAULT;
  876. buf += tocopy;
  877. rem -= tocopy;
  878. start += tocopy;
  879. }
  880. start &= size - 1;
  881. if (rem) {
  882. tocopy = min(rem, msc->single_sz - start);
  883. if (copy_to_user(buf, msc->base + start, tocopy))
  884. return -EFAULT;
  885. rem -= tocopy;
  886. }
  887. return len - rem;
  888. }
  889. if (copy_to_user(buf, msc->base + start, rem))
  890. return -EFAULT;
  891. return len;
  892. }
  893. static ssize_t intel_th_msc_read(struct file *file, char __user *buf,
  894. size_t len, loff_t *ppos)
  895. {
  896. struct msc_iter *iter = file->private_data;
  897. struct msc *msc = iter->msc;
  898. size_t size;
  899. loff_t off = *ppos;
  900. ssize_t ret = 0;
  901. if (!atomic_inc_unless_negative(&msc->user_count))
  902. return 0;
  903. if (msc->enabled) {
  904. ret = -EBUSY;
  905. goto put_count;
  906. }
  907. if (msc->mode == MSC_MODE_SINGLE && !msc->single_wrap)
  908. size = msc->single_sz;
  909. else
  910. size = msc->nr_pages << PAGE_SHIFT;
  911. if (!size)
  912. return 0;
  913. if (off >= size) {
  914. len = 0;
  915. goto put_count;
  916. }
  917. if (off + len >= size)
  918. len = size - off;
  919. if (msc->mode == MSC_MODE_SINGLE) {
  920. ret = msc_single_to_user(msc, buf, off, len);
  921. if (ret >= 0)
  922. *ppos += ret;
  923. } else if (msc->mode == MSC_MODE_MULTI) {
  924. struct msc_win_to_user_struct u = {
  925. .buf = buf,
  926. .offset = 0,
  927. };
  928. ret = msc_buffer_iterate(iter, len, &u, msc_win_to_user);
  929. if (ret >= 0)
  930. *ppos = iter->offset;
  931. } else {
  932. ret = -ENOTSUPP;
  933. }
  934. put_count:
  935. atomic_dec(&msc->user_count);
  936. return ret;
  937. }
  938. /*
  939. * vm operations callbacks (vm_ops)
  940. */
  941. static void msc_mmap_open(struct vm_area_struct *vma)
  942. {
  943. struct msc_iter *iter = vma->vm_file->private_data;
  944. struct msc *msc = iter->msc;
  945. atomic_inc(&msc->mmap_count);
  946. }
  947. static void msc_mmap_close(struct vm_area_struct *vma)
  948. {
  949. struct msc_iter *iter = vma->vm_file->private_data;
  950. struct msc *msc = iter->msc;
  951. unsigned long pg;
  952. if (!atomic_dec_and_mutex_lock(&msc->mmap_count, &msc->buf_mutex))
  953. return;
  954. /* drop page _counts */
  955. for (pg = 0; pg < msc->nr_pages; pg++) {
  956. struct page *page = msc_buffer_get_page(msc, pg);
  957. if (WARN_ON_ONCE(!page))
  958. continue;
  959. if (page->mapping)
  960. page->mapping = NULL;
  961. }
  962. /* last mapping -- drop user_count */
  963. atomic_dec(&msc->user_count);
  964. mutex_unlock(&msc->buf_mutex);
  965. }
  966. static int msc_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  967. {
  968. struct msc_iter *iter = vma->vm_file->private_data;
  969. struct msc *msc = iter->msc;
  970. vmf->page = msc_buffer_get_page(msc, vmf->pgoff);
  971. if (!vmf->page)
  972. return VM_FAULT_SIGBUS;
  973. get_page(vmf->page);
  974. vmf->page->mapping = vma->vm_file->f_mapping;
  975. vmf->page->index = vmf->pgoff;
  976. return 0;
  977. }
  978. static const struct vm_operations_struct msc_mmap_ops = {
  979. .open = msc_mmap_open,
  980. .close = msc_mmap_close,
  981. .fault = msc_mmap_fault,
  982. };
  983. static int intel_th_msc_mmap(struct file *file, struct vm_area_struct *vma)
  984. {
  985. unsigned long size = vma->vm_end - vma->vm_start;
  986. struct msc_iter *iter = vma->vm_file->private_data;
  987. struct msc *msc = iter->msc;
  988. int ret = -EINVAL;
  989. if (!size || offset_in_page(size))
  990. return -EINVAL;
  991. if (vma->vm_pgoff)
  992. return -EINVAL;
  993. /* grab user_count once per mmap; drop in msc_mmap_close() */
  994. if (!atomic_inc_unless_negative(&msc->user_count))
  995. return -EINVAL;
  996. if (msc->mode != MSC_MODE_SINGLE &&
  997. msc->mode != MSC_MODE_MULTI)
  998. goto out;
  999. if (size >> PAGE_SHIFT != msc->nr_pages)
  1000. goto out;
  1001. atomic_set(&msc->mmap_count, 1);
  1002. ret = 0;
  1003. out:
  1004. if (ret)
  1005. atomic_dec(&msc->user_count);
  1006. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  1007. vma->vm_flags |= VM_DONTEXPAND | VM_DONTCOPY;
  1008. vma->vm_ops = &msc_mmap_ops;
  1009. return ret;
  1010. }
  1011. static const struct file_operations intel_th_msc_fops = {
  1012. .open = intel_th_msc_open,
  1013. .release = intel_th_msc_release,
  1014. .read = intel_th_msc_read,
  1015. .mmap = intel_th_msc_mmap,
  1016. .llseek = no_llseek,
  1017. };
  1018. static int intel_th_msc_init(struct msc *msc)
  1019. {
  1020. atomic_set(&msc->user_count, -1);
  1021. msc->mode = MSC_MODE_MULTI;
  1022. mutex_init(&msc->buf_mutex);
  1023. INIT_LIST_HEAD(&msc->win_list);
  1024. mutex_init(&msc->iter_mutex);
  1025. INIT_LIST_HEAD(&msc->iter_list);
  1026. msc->burst_len =
  1027. (ioread32(msc->reg_base + REG_MSU_MSC0CTL) & MSC_LEN) >>
  1028. __ffs(MSC_LEN);
  1029. return 0;
  1030. }
  1031. static const char * const msc_mode[] = {
  1032. [MSC_MODE_SINGLE] = "single",
  1033. [MSC_MODE_MULTI] = "multi",
  1034. [MSC_MODE_EXI] = "ExI",
  1035. [MSC_MODE_DEBUG] = "debug",
  1036. };
  1037. static ssize_t
  1038. wrap_show(struct device *dev, struct device_attribute *attr, char *buf)
  1039. {
  1040. struct msc *msc = dev_get_drvdata(dev);
  1041. return scnprintf(buf, PAGE_SIZE, "%d\n", msc->wrap);
  1042. }
  1043. static ssize_t
  1044. wrap_store(struct device *dev, struct device_attribute *attr, const char *buf,
  1045. size_t size)
  1046. {
  1047. struct msc *msc = dev_get_drvdata(dev);
  1048. unsigned long val;
  1049. int ret;
  1050. ret = kstrtoul(buf, 10, &val);
  1051. if (ret)
  1052. return ret;
  1053. msc->wrap = !!val;
  1054. return size;
  1055. }
  1056. static DEVICE_ATTR_RW(wrap);
  1057. static ssize_t
  1058. mode_show(struct device *dev, struct device_attribute *attr, char *buf)
  1059. {
  1060. struct msc *msc = dev_get_drvdata(dev);
  1061. return scnprintf(buf, PAGE_SIZE, "%s\n", msc_mode[msc->mode]);
  1062. }
  1063. static ssize_t
  1064. mode_store(struct device *dev, struct device_attribute *attr, const char *buf,
  1065. size_t size)
  1066. {
  1067. struct msc *msc = dev_get_drvdata(dev);
  1068. size_t len = size;
  1069. char *cp;
  1070. int i, ret;
  1071. if (!capable(CAP_SYS_RAWIO))
  1072. return -EPERM;
  1073. cp = memchr(buf, '\n', len);
  1074. if (cp)
  1075. len = cp - buf;
  1076. for (i = 0; i < ARRAY_SIZE(msc_mode); i++)
  1077. if (!strncmp(msc_mode[i], buf, len))
  1078. goto found;
  1079. return -EINVAL;
  1080. found:
  1081. mutex_lock(&msc->buf_mutex);
  1082. ret = msc_buffer_unlocked_free_unless_used(msc);
  1083. if (!ret)
  1084. msc->mode = i;
  1085. mutex_unlock(&msc->buf_mutex);
  1086. return ret ? ret : size;
  1087. }
  1088. static DEVICE_ATTR_RW(mode);
  1089. static ssize_t
  1090. nr_pages_show(struct device *dev, struct device_attribute *attr, char *buf)
  1091. {
  1092. struct msc *msc = dev_get_drvdata(dev);
  1093. struct msc_window *win;
  1094. size_t count = 0;
  1095. mutex_lock(&msc->buf_mutex);
  1096. if (msc->mode == MSC_MODE_SINGLE)
  1097. count = scnprintf(buf, PAGE_SIZE, "%ld\n", msc->nr_pages);
  1098. else if (msc->mode == MSC_MODE_MULTI) {
  1099. list_for_each_entry(win, &msc->win_list, entry) {
  1100. count += scnprintf(buf + count, PAGE_SIZE - count,
  1101. "%d%c", win->nr_blocks,
  1102. msc_is_last_win(win) ? '\n' : ',');
  1103. }
  1104. } else {
  1105. count = scnprintf(buf, PAGE_SIZE, "unsupported\n");
  1106. }
  1107. mutex_unlock(&msc->buf_mutex);
  1108. return count;
  1109. }
  1110. static ssize_t
  1111. nr_pages_store(struct device *dev, struct device_attribute *attr,
  1112. const char *buf, size_t size)
  1113. {
  1114. struct msc *msc = dev_get_drvdata(dev);
  1115. unsigned long val, *win = NULL, *rewin;
  1116. size_t len = size;
  1117. const char *p = buf;
  1118. char *end, *s;
  1119. int ret, nr_wins = 0;
  1120. if (!capable(CAP_SYS_RAWIO))
  1121. return -EPERM;
  1122. ret = msc_buffer_free_unless_used(msc);
  1123. if (ret)
  1124. return ret;
  1125. /* scan the comma-separated list of allocation sizes */
  1126. end = memchr(buf, '\n', len);
  1127. if (end)
  1128. len = end - buf;
  1129. do {
  1130. end = memchr(p, ',', len);
  1131. s = kstrndup(p, end ? end - p : len, GFP_KERNEL);
  1132. ret = kstrtoul(s, 10, &val);
  1133. kfree(s);
  1134. if (ret || !val)
  1135. goto free_win;
  1136. if (nr_wins && msc->mode == MSC_MODE_SINGLE) {
  1137. ret = -EINVAL;
  1138. goto free_win;
  1139. }
  1140. nr_wins++;
  1141. rewin = krealloc(win, sizeof(*win) * nr_wins, GFP_KERNEL);
  1142. if (!rewin) {
  1143. kfree(win);
  1144. return -ENOMEM;
  1145. }
  1146. win = rewin;
  1147. win[nr_wins - 1] = val;
  1148. if (!end)
  1149. break;
  1150. len -= end - p;
  1151. p = end + 1;
  1152. } while (len);
  1153. mutex_lock(&msc->buf_mutex);
  1154. ret = msc_buffer_alloc(msc, win, nr_wins);
  1155. mutex_unlock(&msc->buf_mutex);
  1156. free_win:
  1157. kfree(win);
  1158. return ret ? ret : size;
  1159. }
  1160. static DEVICE_ATTR_RW(nr_pages);
  1161. static struct attribute *msc_output_attrs[] = {
  1162. &dev_attr_wrap.attr,
  1163. &dev_attr_mode.attr,
  1164. &dev_attr_nr_pages.attr,
  1165. NULL,
  1166. };
  1167. static struct attribute_group msc_output_group = {
  1168. .attrs = msc_output_attrs,
  1169. };
  1170. static int intel_th_msc_probe(struct intel_th_device *thdev)
  1171. {
  1172. struct device *dev = &thdev->dev;
  1173. struct resource *res;
  1174. struct msc *msc;
  1175. void __iomem *base;
  1176. int err;
  1177. res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
  1178. if (!res)
  1179. return -ENODEV;
  1180. base = devm_ioremap(dev, res->start, resource_size(res));
  1181. if (!base)
  1182. return -ENOMEM;
  1183. msc = devm_kzalloc(dev, sizeof(*msc), GFP_KERNEL);
  1184. if (!msc)
  1185. return -ENOMEM;
  1186. msc->index = thdev->id;
  1187. msc->thdev = thdev;
  1188. msc->reg_base = base + msc->index * 0x100;
  1189. err = intel_th_msc_init(msc);
  1190. if (err)
  1191. return err;
  1192. err = sysfs_create_group(&dev->kobj, &msc_output_group);
  1193. if (err)
  1194. return err;
  1195. dev_set_drvdata(dev, msc);
  1196. return 0;
  1197. }
  1198. static void intel_th_msc_remove(struct intel_th_device *thdev)
  1199. {
  1200. sysfs_remove_group(&thdev->dev.kobj, &msc_output_group);
  1201. }
  1202. static struct intel_th_driver intel_th_msc_driver = {
  1203. .probe = intel_th_msc_probe,
  1204. .remove = intel_th_msc_remove,
  1205. .activate = intel_th_msc_activate,
  1206. .deactivate = intel_th_msc_deactivate,
  1207. .fops = &intel_th_msc_fops,
  1208. .driver = {
  1209. .name = "msc",
  1210. .owner = THIS_MODULE,
  1211. },
  1212. };
  1213. module_driver(intel_th_msc_driver,
  1214. intel_th_driver_register,
  1215. intel_th_driver_unregister);
  1216. MODULE_LICENSE("GPL v2");
  1217. MODULE_DESCRIPTION("Intel(R) Trace Hub Memory Storage Unit driver");
  1218. MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");