msu.c 34 KB

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