msu.c 34 KB

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