msu.c 34 KB

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