msu.c 35 KB

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