msu.c 34 KB

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