msu.c 34 KB

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