msu.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196
  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/workqueue.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/pfn_t.h>
  21. #ifdef CONFIG_X86
  22. #include <asm/set_memory.h>
  23. #endif
  24. #include <linux/intel_th.h>
  25. #include "intel_th.h"
  26. #include "msu.h"
  27. #define msc_dev(x) (&(x)->thdev->dev)
  28. /*
  29. * Lockout state transitions:
  30. * READY -> INUSE -+-> LOCKED -+-> READY -> etc.
  31. * \-----------/
  32. * WIN_READY: window can be used by HW
  33. * WIN_INUSE: window is in use
  34. * WIN_LOCKED: window is filled up and is being processed by the buffer
  35. * handling code
  36. *
  37. * All state transitions happen automatically, except for the LOCKED->READY,
  38. * which needs to be signalled by the buffer code by calling
  39. * intel_th_msc_window_unlock().
  40. *
  41. * When the interrupt handler has to switch to the next window, it checks
  42. * whether it's READY, and if it is, it performs the switch and tracing
  43. * continues. If it's LOCKED, it stops the trace.
  44. */
  45. enum lockout_state {
  46. WIN_READY = 0,
  47. WIN_INUSE,
  48. WIN_LOCKED
  49. };
  50. /**
  51. * struct msc_window - multiblock mode window descriptor
  52. * @entry: window list linkage (msc::win_list)
  53. * @pgoff: page offset into the buffer that this window starts at
  54. * @lockout: lockout state, see comment below
  55. * @lo_lock: lockout state serialization
  56. * @nr_blocks: number of blocks (pages) in this window
  57. * @nr_segs: number of segments in this window (<= @nr_blocks)
  58. * @msc: pointer to the MSC device
  59. * @_sgt: array of block descriptors
  60. * @sgt: array of block descriptors
  61. */
  62. struct msc_window {
  63. struct list_head entry;
  64. unsigned long pgoff;
  65. enum lockout_state lockout;
  66. spinlock_t lo_lock;
  67. unsigned int nr_blocks;
  68. unsigned int nr_segs;
  69. struct msc *msc;
  70. struct sg_table _sgt;
  71. struct sg_table *sgt;
  72. };
  73. /**
  74. * struct msc_iter - iterator for msc buffer
  75. * @entry: msc::iter_list linkage
  76. * @msc: pointer to the MSC device
  77. * @start_win: oldest window
  78. * @win: current window
  79. * @offset: current logical offset into the buffer
  80. * @start_block: oldest block in the window
  81. * @block: block number in the window
  82. * @block_off: offset into current block
  83. * @wrap_count: block wrapping handling
  84. * @eof: end of buffer reached
  85. */
  86. struct msc_iter {
  87. struct list_head entry;
  88. struct msc *msc;
  89. struct msc_window *start_win;
  90. struct msc_window *win;
  91. unsigned long offset;
  92. struct scatterlist *start_block;
  93. struct scatterlist *block;
  94. unsigned int block_off;
  95. unsigned int wrap_count;
  96. unsigned int eof;
  97. };
  98. /**
  99. * struct msc - MSC device representation
  100. * @reg_base: register window base address
  101. * @thdev: intel_th_device pointer
  102. * @mbuf: MSU buffer, if assigned
  103. * @mbuf_priv MSU buffer's private data, if @mbuf
  104. * @win_list: list of windows in multiblock mode
  105. * @single_sgt: single mode buffer
  106. * @cur_win: current window
  107. * @nr_pages: total number of pages allocated for this buffer
  108. * @single_sz: amount of data in single mode
  109. * @single_wrap: single mode wrap occurred
  110. * @base: buffer's base pointer
  111. * @base_addr: buffer's base address
  112. * @user_count: number of users of the buffer
  113. * @mmap_count: number of mappings
  114. * @buf_mutex: mutex to serialize access to buffer-related bits
  115. * @enabled: MSC is enabled
  116. * @wrap: wrapping is enabled
  117. * @mode: MSC operating mode
  118. * @burst_len: write burst length
  119. * @index: number of this MSC in the MSU
  120. */
  121. struct msc {
  122. void __iomem *reg_base;
  123. void __iomem *msu_base;
  124. struct intel_th_device *thdev;
  125. const struct msu_buffer *mbuf;
  126. void *mbuf_priv;
  127. struct work_struct work;
  128. struct list_head win_list;
  129. struct sg_table single_sgt;
  130. struct msc_window *cur_win;
  131. struct msc_window *switch_on_unlock;
  132. unsigned long nr_pages;
  133. unsigned long single_sz;
  134. unsigned int single_wrap : 1;
  135. void *base;
  136. dma_addr_t base_addr;
  137. u32 orig_addr;
  138. u32 orig_sz;
  139. /* <0: no buffer, 0: no users, >0: active users */
  140. atomic_t user_count;
  141. atomic_t mmap_count;
  142. struct mutex buf_mutex;
  143. struct list_head iter_list;
  144. bool stop_on_full;
  145. /* config */
  146. unsigned int enabled : 1,
  147. wrap : 1,
  148. do_irq : 1,
  149. multi_is_broken : 1;
  150. unsigned int mode;
  151. unsigned int burst_len;
  152. unsigned int index;
  153. };
  154. static LIST_HEAD(msu_buffer_list);
  155. static DEFINE_MUTEX(msu_buffer_mutex);
  156. /**
  157. * struct msu_buffer_entry - internal MSU buffer bookkeeping
  158. * @entry: link to msu_buffer_list
  159. * @mbuf: MSU buffer object
  160. * @owner: module that provides this MSU buffer
  161. */
  162. struct msu_buffer_entry {
  163. struct list_head entry;
  164. const struct msu_buffer *mbuf;
  165. struct module *owner;
  166. };
  167. static struct msu_buffer_entry *__msu_buffer_entry_find(const char *name)
  168. {
  169. struct msu_buffer_entry *mbe;
  170. lockdep_assert_held(&msu_buffer_mutex);
  171. list_for_each_entry(mbe, &msu_buffer_list, entry) {
  172. if (!strcmp(mbe->mbuf->name, name))
  173. return mbe;
  174. }
  175. return NULL;
  176. }
  177. static const struct msu_buffer *
  178. msu_buffer_get(const char *name)
  179. {
  180. struct msu_buffer_entry *mbe;
  181. mutex_lock(&msu_buffer_mutex);
  182. mbe = __msu_buffer_entry_find(name);
  183. if (mbe && !try_module_get(mbe->owner))
  184. mbe = NULL;
  185. mutex_unlock(&msu_buffer_mutex);
  186. return mbe ? mbe->mbuf : NULL;
  187. }
  188. static void msu_buffer_put(const struct msu_buffer *mbuf)
  189. {
  190. struct msu_buffer_entry *mbe;
  191. mutex_lock(&msu_buffer_mutex);
  192. mbe = __msu_buffer_entry_find(mbuf->name);
  193. if (mbe)
  194. module_put(mbe->owner);
  195. mutex_unlock(&msu_buffer_mutex);
  196. }
  197. int intel_th_msu_buffer_register(const struct msu_buffer *mbuf,
  198. struct module *owner)
  199. {
  200. struct msu_buffer_entry *mbe;
  201. int ret = 0;
  202. mbe = kzalloc(sizeof(*mbe), GFP_KERNEL);
  203. if (!mbe)
  204. return -ENOMEM;
  205. mutex_lock(&msu_buffer_mutex);
  206. if (__msu_buffer_entry_find(mbuf->name)) {
  207. ret = -EEXIST;
  208. kfree(mbe);
  209. goto unlock;
  210. }
  211. mbe->mbuf = mbuf;
  212. mbe->owner = owner;
  213. list_add_tail(&mbe->entry, &msu_buffer_list);
  214. unlock:
  215. mutex_unlock(&msu_buffer_mutex);
  216. return ret;
  217. }
  218. EXPORT_SYMBOL_GPL(intel_th_msu_buffer_register);
  219. void intel_th_msu_buffer_unregister(const struct msu_buffer *mbuf)
  220. {
  221. struct msu_buffer_entry *mbe;
  222. mutex_lock(&msu_buffer_mutex);
  223. mbe = __msu_buffer_entry_find(mbuf->name);
  224. if (mbe) {
  225. list_del(&mbe->entry);
  226. kfree(mbe);
  227. }
  228. mutex_unlock(&msu_buffer_mutex);
  229. }
  230. EXPORT_SYMBOL_GPL(intel_th_msu_buffer_unregister);
  231. static inline bool msc_block_is_empty(struct msc_block_desc *bdesc)
  232. {
  233. /* header hasn't been written */
  234. if (!bdesc->valid_dw)
  235. return true;
  236. /* valid_dw includes the header */
  237. if (!msc_data_sz(bdesc))
  238. return true;
  239. return false;
  240. }
  241. static inline struct scatterlist *msc_win_base_sg(struct msc_window *win)
  242. {
  243. return win->sgt->sgl;
  244. }
  245. static inline struct msc_block_desc *msc_win_base(struct msc_window *win)
  246. {
  247. return sg_virt(msc_win_base_sg(win));
  248. }
  249. static inline dma_addr_t msc_win_base_dma(struct msc_window *win)
  250. {
  251. return sg_dma_address(msc_win_base_sg(win));
  252. }
  253. static inline unsigned long
  254. msc_win_base_pfn(struct msc_window *win)
  255. {
  256. return PFN_DOWN(msc_win_base_dma(win));
  257. }
  258. /**
  259. * msc_is_last_win() - check if a window is the last one for a given MSC
  260. * @win: window
  261. * Return: true if @win is the last window in MSC's multiblock buffer
  262. */
  263. static inline bool msc_is_last_win(struct msc_window *win)
  264. {
  265. return win->entry.next == &win->msc->win_list;
  266. }
  267. /**
  268. * msc_next_window() - return next window in the multiblock buffer
  269. * @win: current window
  270. *
  271. * Return: window following the current one
  272. */
  273. static struct msc_window *msc_next_window(struct msc_window *win)
  274. {
  275. if (msc_is_last_win(win))
  276. return list_first_entry(&win->msc->win_list, struct msc_window,
  277. entry);
  278. return list_next_entry(win, entry);
  279. }
  280. static size_t msc_win_total_sz(struct msc_window *win)
  281. {
  282. struct scatterlist *sg;
  283. unsigned int blk;
  284. size_t size = 0;
  285. for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
  286. struct msc_block_desc *bdesc = sg_virt(sg);
  287. if (msc_block_wrapped(bdesc))
  288. return (size_t)win->nr_blocks << PAGE_SHIFT;
  289. size += msc_total_sz(bdesc);
  290. if (msc_block_last_written(bdesc))
  291. break;
  292. }
  293. return size;
  294. }
  295. /**
  296. * msc_find_window() - find a window matching a given sg_table
  297. * @msc: MSC device
  298. * @sgt: SG table of the window
  299. * @nonempty: skip over empty windows
  300. *
  301. * Return: MSC window structure pointer or NULL if the window
  302. * could not be found.
  303. */
  304. static struct msc_window *
  305. msc_find_window(struct msc *msc, struct sg_table *sgt, bool nonempty)
  306. {
  307. struct msc_window *win;
  308. unsigned int found = 0;
  309. if (list_empty(&msc->win_list))
  310. return NULL;
  311. /*
  312. * we might need a radix tree for this, depending on how
  313. * many windows a typical user would allocate; ideally it's
  314. * something like 2, in which case we're good
  315. */
  316. list_for_each_entry(win, &msc->win_list, entry) {
  317. if (win->sgt == sgt)
  318. found++;
  319. /* skip the empty ones */
  320. if (nonempty && msc_block_is_empty(msc_win_base(win)))
  321. continue;
  322. if (found)
  323. return win;
  324. }
  325. return NULL;
  326. }
  327. /**
  328. * msc_oldest_window() - locate the window with oldest data
  329. * @msc: MSC device
  330. *
  331. * This should only be used in multiblock mode. Caller should hold the
  332. * msc::user_count reference.
  333. *
  334. * Return: the oldest window with valid data
  335. */
  336. static struct msc_window *msc_oldest_window(struct msc *msc)
  337. {
  338. struct msc_window *win;
  339. if (list_empty(&msc->win_list))
  340. return NULL;
  341. win = msc_find_window(msc, msc_next_window(msc->cur_win)->sgt, true);
  342. if (win)
  343. return win;
  344. return list_first_entry(&msc->win_list, struct msc_window, entry);
  345. }
  346. /**
  347. * msc_win_oldest_sg() - locate the oldest block in a given window
  348. * @win: window to look at
  349. *
  350. * Return: index of the block with the oldest data
  351. */
  352. static struct scatterlist *msc_win_oldest_sg(struct msc_window *win)
  353. {
  354. unsigned int blk;
  355. struct scatterlist *sg;
  356. struct msc_block_desc *bdesc = msc_win_base(win);
  357. /* without wrapping, first block is the oldest */
  358. if (!msc_block_wrapped(bdesc))
  359. return msc_win_base_sg(win);
  360. /*
  361. * with wrapping, last written block contains both the newest and the
  362. * oldest data for this window.
  363. */
  364. for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
  365. struct msc_block_desc *bdesc = sg_virt(sg);
  366. if (msc_block_last_written(bdesc))
  367. return sg;
  368. }
  369. return msc_win_base_sg(win);
  370. }
  371. static struct msc_block_desc *msc_iter_bdesc(struct msc_iter *iter)
  372. {
  373. return sg_virt(iter->block);
  374. }
  375. static struct msc_iter *msc_iter_install(struct msc *msc)
  376. {
  377. struct msc_iter *iter;
  378. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  379. if (!iter)
  380. return ERR_PTR(-ENOMEM);
  381. mutex_lock(&msc->buf_mutex);
  382. /*
  383. * Reading and tracing are mutually exclusive; if msc is
  384. * enabled, open() will fail; otherwise existing readers
  385. * will prevent enabling the msc and the rest of fops don't
  386. * need to worry about it.
  387. */
  388. if (msc->enabled) {
  389. kfree(iter);
  390. iter = ERR_PTR(-EBUSY);
  391. goto unlock;
  392. }
  393. iter->msc = msc;
  394. list_add_tail(&iter->entry, &msc->iter_list);
  395. unlock:
  396. mutex_unlock(&msc->buf_mutex);
  397. return iter;
  398. }
  399. static void msc_iter_remove(struct msc_iter *iter, struct msc *msc)
  400. {
  401. mutex_lock(&msc->buf_mutex);
  402. list_del(&iter->entry);
  403. mutex_unlock(&msc->buf_mutex);
  404. kfree(iter);
  405. }
  406. static void msc_iter_block_start(struct msc_iter *iter)
  407. {
  408. if (iter->start_block)
  409. return;
  410. iter->start_block = msc_win_oldest_sg(iter->win);
  411. iter->block = iter->start_block;
  412. iter->wrap_count = 0;
  413. /*
  414. * start with the block with oldest data; if data has wrapped
  415. * in this window, it should be in this block
  416. */
  417. if (msc_block_wrapped(msc_iter_bdesc(iter)))
  418. iter->wrap_count = 2;
  419. }
  420. static int msc_iter_win_start(struct msc_iter *iter, struct msc *msc)
  421. {
  422. /* already started, nothing to do */
  423. if (iter->start_win)
  424. return 0;
  425. iter->start_win = msc_oldest_window(msc);
  426. if (!iter->start_win)
  427. return -EINVAL;
  428. iter->win = iter->start_win;
  429. iter->start_block = NULL;
  430. msc_iter_block_start(iter);
  431. return 0;
  432. }
  433. static int msc_iter_win_advance(struct msc_iter *iter)
  434. {
  435. iter->win = msc_next_window(iter->win);
  436. iter->start_block = NULL;
  437. if (iter->win == iter->start_win) {
  438. iter->eof++;
  439. return 1;
  440. }
  441. msc_iter_block_start(iter);
  442. return 0;
  443. }
  444. static int msc_iter_block_advance(struct msc_iter *iter)
  445. {
  446. iter->block_off = 0;
  447. /* wrapping */
  448. if (iter->wrap_count && iter->block == iter->start_block) {
  449. iter->wrap_count--;
  450. if (!iter->wrap_count)
  451. /* copied newest data from the wrapped block */
  452. return msc_iter_win_advance(iter);
  453. }
  454. /* no wrapping, check for last written block */
  455. if (!iter->wrap_count && msc_block_last_written(msc_iter_bdesc(iter)))
  456. /* copied newest data for the window */
  457. return msc_iter_win_advance(iter);
  458. /* block advance */
  459. if (sg_is_last(iter->block))
  460. iter->block = msc_win_base_sg(iter->win);
  461. else
  462. iter->block = sg_next(iter->block);
  463. /* no wrapping, sanity check in case there is no last written block */
  464. if (!iter->wrap_count && iter->block == iter->start_block)
  465. return msc_iter_win_advance(iter);
  466. return 0;
  467. }
  468. /**
  469. * msc_buffer_iterate() - go through multiblock buffer's data
  470. * @iter: iterator structure
  471. * @size: amount of data to scan
  472. * @data: callback's private data
  473. * @fn: iterator callback
  474. *
  475. * This will start at the window which will be written to next (containing
  476. * the oldest data) and work its way to the current window, calling @fn
  477. * for each chunk of data as it goes.
  478. *
  479. * Caller should have msc::user_count reference to make sure the buffer
  480. * doesn't disappear from under us.
  481. *
  482. * Return: amount of data actually scanned.
  483. */
  484. static ssize_t
  485. msc_buffer_iterate(struct msc_iter *iter, size_t size, void *data,
  486. unsigned long (*fn)(void *, void *, size_t))
  487. {
  488. struct msc *msc = iter->msc;
  489. size_t len = size;
  490. unsigned int advance;
  491. if (iter->eof)
  492. return 0;
  493. /* start with the oldest window */
  494. if (msc_iter_win_start(iter, msc))
  495. return 0;
  496. do {
  497. unsigned long data_bytes = msc_data_sz(msc_iter_bdesc(iter));
  498. void *src = (void *)msc_iter_bdesc(iter) + MSC_BDESC;
  499. size_t tocopy = data_bytes, copied = 0;
  500. size_t remaining = 0;
  501. advance = 1;
  502. /*
  503. * If block wrapping happened, we need to visit the last block
  504. * twice, because it contains both the oldest and the newest
  505. * data in this window.
  506. *
  507. * First time (wrap_count==2), in the very beginning, to collect
  508. * the oldest data, which is in the range
  509. * (data_bytes..DATA_IN_PAGE).
  510. *
  511. * Second time (wrap_count==1), it's just like any other block,
  512. * containing data in the range of [MSC_BDESC..data_bytes].
  513. */
  514. if (iter->block == iter->start_block && iter->wrap_count == 2) {
  515. tocopy = DATA_IN_PAGE - data_bytes;
  516. src += data_bytes;
  517. }
  518. if (!tocopy)
  519. goto next_block;
  520. tocopy -= iter->block_off;
  521. src += iter->block_off;
  522. if (len < tocopy) {
  523. tocopy = len;
  524. advance = 0;
  525. }
  526. remaining = fn(data, src, tocopy);
  527. if (remaining)
  528. advance = 0;
  529. copied = tocopy - remaining;
  530. len -= copied;
  531. iter->block_off += copied;
  532. iter->offset += copied;
  533. if (!advance)
  534. break;
  535. next_block:
  536. if (msc_iter_block_advance(iter))
  537. break;
  538. } while (len);
  539. return size - len;
  540. }
  541. /**
  542. * msc_buffer_clear_hw_header() - clear hw header for multiblock
  543. * @msc: MSC device
  544. */
  545. static void msc_buffer_clear_hw_header(struct msc *msc)
  546. {
  547. struct msc_window *win;
  548. struct scatterlist *sg;
  549. list_for_each_entry(win, &msc->win_list, entry) {
  550. unsigned int blk;
  551. for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
  552. struct msc_block_desc *bdesc = sg_virt(sg);
  553. memset_startat(bdesc, 0, hw_tag);
  554. }
  555. }
  556. }
  557. static int intel_th_msu_init(struct msc *msc)
  558. {
  559. u32 mintctl, msusts;
  560. if (!msc->do_irq)
  561. return 0;
  562. if (!msc->mbuf)
  563. return 0;
  564. mintctl = ioread32(msc->msu_base + REG_MSU_MINTCTL);
  565. mintctl |= msc->index ? M1BLIE : M0BLIE;
  566. iowrite32(mintctl, msc->msu_base + REG_MSU_MINTCTL);
  567. if (mintctl != ioread32(msc->msu_base + REG_MSU_MINTCTL)) {
  568. dev_info(msc_dev(msc), "MINTCTL ignores writes: no usable interrupts\n");
  569. msc->do_irq = 0;
  570. return 0;
  571. }
  572. msusts = ioread32(msc->msu_base + REG_MSU_MSUSTS);
  573. iowrite32(msusts, msc->msu_base + REG_MSU_MSUSTS);
  574. return 0;
  575. }
  576. static void intel_th_msu_deinit(struct msc *msc)
  577. {
  578. u32 mintctl;
  579. if (!msc->do_irq)
  580. return;
  581. mintctl = ioread32(msc->msu_base + REG_MSU_MINTCTL);
  582. mintctl &= msc->index ? ~M1BLIE : ~M0BLIE;
  583. iowrite32(mintctl, msc->msu_base + REG_MSU_MINTCTL);
  584. }
  585. static int msc_win_set_lockout(struct msc_window *win,
  586. enum lockout_state expect,
  587. enum lockout_state new)
  588. {
  589. enum lockout_state old;
  590. unsigned long flags;
  591. int ret = 0;
  592. if (!win->msc->mbuf)
  593. return 0;
  594. spin_lock_irqsave(&win->lo_lock, flags);
  595. old = win->lockout;
  596. if (old != expect) {
  597. ret = -EINVAL;
  598. goto unlock;
  599. }
  600. win->lockout = new;
  601. if (old == expect && new == WIN_LOCKED)
  602. atomic_inc(&win->msc->user_count);
  603. else if (old == expect && old == WIN_LOCKED)
  604. atomic_dec(&win->msc->user_count);
  605. unlock:
  606. spin_unlock_irqrestore(&win->lo_lock, flags);
  607. if (ret) {
  608. if (expect == WIN_READY && old == WIN_LOCKED)
  609. return -EBUSY;
  610. /* from intel_th_msc_window_unlock(), don't warn if not locked */
  611. if (expect == WIN_LOCKED && old == new)
  612. return 0;
  613. dev_warn_ratelimited(msc_dev(win->msc),
  614. "expected lockout state %d, got %d\n",
  615. expect, old);
  616. }
  617. return ret;
  618. }
  619. /**
  620. * msc_configure() - set up MSC hardware
  621. * @msc: the MSC device to configure
  622. *
  623. * Program storage mode, wrapping, burst length and trace buffer address
  624. * into a given MSC. Then, enable tracing and set msc::enabled.
  625. * The latter is serialized on msc::buf_mutex, so make sure to hold it.
  626. *
  627. * Return: %0 for success or a negative error code otherwise.
  628. */
  629. static int msc_configure(struct msc *msc)
  630. {
  631. u32 reg;
  632. lockdep_assert_held(&msc->buf_mutex);
  633. if (msc->mode > MSC_MODE_MULTI)
  634. return -EINVAL;
  635. if (msc->mode == MSC_MODE_MULTI) {
  636. if (msc_win_set_lockout(msc->cur_win, WIN_READY, WIN_INUSE))
  637. return -EBUSY;
  638. msc_buffer_clear_hw_header(msc);
  639. }
  640. msc->orig_addr = ioread32(msc->reg_base + REG_MSU_MSC0BAR);
  641. msc->orig_sz = ioread32(msc->reg_base + REG_MSU_MSC0SIZE);
  642. reg = msc->base_addr >> PAGE_SHIFT;
  643. iowrite32(reg, msc->reg_base + REG_MSU_MSC0BAR);
  644. if (msc->mode == MSC_MODE_SINGLE) {
  645. reg = msc->nr_pages;
  646. iowrite32(reg, msc->reg_base + REG_MSU_MSC0SIZE);
  647. }
  648. reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
  649. reg &= ~(MSC_MODE | MSC_WRAPEN | MSC_EN | MSC_RD_HDR_OVRD);
  650. reg |= MSC_EN;
  651. reg |= msc->mode << __ffs(MSC_MODE);
  652. reg |= msc->burst_len << __ffs(MSC_LEN);
  653. if (msc->wrap)
  654. reg |= MSC_WRAPEN;
  655. iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
  656. intel_th_msu_init(msc);
  657. msc->thdev->output.multiblock = msc->mode == MSC_MODE_MULTI;
  658. intel_th_trace_enable(msc->thdev);
  659. msc->enabled = 1;
  660. if (msc->mbuf && msc->mbuf->activate)
  661. msc->mbuf->activate(msc->mbuf_priv);
  662. return 0;
  663. }
  664. /**
  665. * msc_disable() - disable MSC hardware
  666. * @msc: MSC device to disable
  667. *
  668. * If @msc is enabled, disable tracing on the switch and then disable MSC
  669. * storage. Caller must hold msc::buf_mutex.
  670. */
  671. static void msc_disable(struct msc *msc)
  672. {
  673. struct msc_window *win = msc->cur_win;
  674. u32 reg;
  675. lockdep_assert_held(&msc->buf_mutex);
  676. if (msc->mode == MSC_MODE_MULTI)
  677. msc_win_set_lockout(win, WIN_INUSE, WIN_LOCKED);
  678. if (msc->mbuf && msc->mbuf->deactivate)
  679. msc->mbuf->deactivate(msc->mbuf_priv);
  680. intel_th_msu_deinit(msc);
  681. intel_th_trace_disable(msc->thdev);
  682. if (msc->mode == MSC_MODE_SINGLE) {
  683. reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
  684. msc->single_wrap = !!(reg & MSCSTS_WRAPSTAT);
  685. reg = ioread32(msc->reg_base + REG_MSU_MSC0MWP);
  686. msc->single_sz = reg & ((msc->nr_pages << PAGE_SHIFT) - 1);
  687. dev_dbg(msc_dev(msc), "MSCnMWP: %08x/%08lx, wrap: %d\n",
  688. reg, msc->single_sz, msc->single_wrap);
  689. }
  690. reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
  691. reg &= ~MSC_EN;
  692. iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
  693. if (msc->mbuf && msc->mbuf->ready)
  694. msc->mbuf->ready(msc->mbuf_priv, win->sgt,
  695. msc_win_total_sz(win));
  696. msc->enabled = 0;
  697. iowrite32(msc->orig_addr, msc->reg_base + REG_MSU_MSC0BAR);
  698. iowrite32(msc->orig_sz, msc->reg_base + REG_MSU_MSC0SIZE);
  699. dev_dbg(msc_dev(msc), "MSCnNWSA: %08x\n",
  700. ioread32(msc->reg_base + REG_MSU_MSC0NWSA));
  701. reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
  702. dev_dbg(msc_dev(msc), "MSCnSTS: %08x\n", reg);
  703. reg = ioread32(msc->reg_base + REG_MSU_MSUSTS);
  704. reg &= msc->index ? MSUSTS_MSC1BLAST : MSUSTS_MSC0BLAST;
  705. iowrite32(reg, msc->reg_base + REG_MSU_MSUSTS);
  706. }
  707. static int intel_th_msc_activate(struct intel_th_device *thdev)
  708. {
  709. struct msc *msc = dev_get_drvdata(&thdev->dev);
  710. int ret = -EBUSY;
  711. if (!atomic_inc_unless_negative(&msc->user_count))
  712. return -ENODEV;
  713. mutex_lock(&msc->buf_mutex);
  714. /* if there are readers, refuse */
  715. if (list_empty(&msc->iter_list))
  716. ret = msc_configure(msc);
  717. mutex_unlock(&msc->buf_mutex);
  718. if (ret)
  719. atomic_dec(&msc->user_count);
  720. return ret;
  721. }
  722. static void intel_th_msc_deactivate(struct intel_th_device *thdev)
  723. {
  724. struct msc *msc = dev_get_drvdata(&thdev->dev);
  725. mutex_lock(&msc->buf_mutex);
  726. if (msc->enabled) {
  727. msc_disable(msc);
  728. atomic_dec(&msc->user_count);
  729. }
  730. mutex_unlock(&msc->buf_mutex);
  731. }
  732. /**
  733. * msc_buffer_contig_alloc() - allocate a contiguous buffer for SINGLE mode
  734. * @msc: MSC device
  735. * @size: allocation size in bytes
  736. *
  737. * This modifies msc::base, which requires msc::buf_mutex to serialize, so the
  738. * caller is expected to hold it.
  739. *
  740. * Return: 0 on success, -errno otherwise.
  741. */
  742. static int msc_buffer_contig_alloc(struct msc *msc, unsigned long size)
  743. {
  744. unsigned long nr_pages = size >> PAGE_SHIFT;
  745. unsigned int order = get_order(size);
  746. struct page *page;
  747. int ret;
  748. if (!size)
  749. return 0;
  750. ret = sg_alloc_table(&msc->single_sgt, 1, GFP_KERNEL);
  751. if (ret)
  752. goto err_out;
  753. ret = -ENOMEM;
  754. page = alloc_pages(GFP_KERNEL | __GFP_ZERO | GFP_DMA32, order);
  755. if (!page)
  756. goto err_free_sgt;
  757. split_page(page, order);
  758. sg_set_buf(msc->single_sgt.sgl, page_address(page), size);
  759. ret = dma_map_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl, 1,
  760. DMA_FROM_DEVICE);
  761. if (ret < 0)
  762. goto err_free_pages;
  763. msc->nr_pages = nr_pages;
  764. msc->base = page_address(page);
  765. msc->base_addr = sg_dma_address(msc->single_sgt.sgl);
  766. return 0;
  767. err_free_pages:
  768. __free_pages(page, order);
  769. err_free_sgt:
  770. sg_free_table(&msc->single_sgt);
  771. err_out:
  772. return ret;
  773. }
  774. /**
  775. * msc_buffer_contig_free() - free a contiguous buffer
  776. * @msc: MSC configured in SINGLE mode
  777. */
  778. static void msc_buffer_contig_free(struct msc *msc)
  779. {
  780. unsigned long off;
  781. dma_unmap_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl,
  782. 1, DMA_FROM_DEVICE);
  783. sg_free_table(&msc->single_sgt);
  784. for (off = 0; off < msc->nr_pages << PAGE_SHIFT; off += PAGE_SIZE) {
  785. struct page *page = virt_to_page(msc->base + off);
  786. __free_page(page);
  787. }
  788. msc->nr_pages = 0;
  789. }
  790. /**
  791. * msc_buffer_contig_get_page() - find a page at a given offset
  792. * @msc: MSC configured in SINGLE mode
  793. * @pgoff: page offset
  794. *
  795. * Return: page, if @pgoff is within the range, NULL otherwise.
  796. */
  797. static struct page *msc_buffer_contig_get_page(struct msc *msc,
  798. unsigned long pgoff)
  799. {
  800. if (pgoff >= msc->nr_pages)
  801. return NULL;
  802. return virt_to_page(msc->base + (pgoff << PAGE_SHIFT));
  803. }
  804. static int __msc_buffer_win_alloc(struct msc_window *win,
  805. unsigned int nr_segs)
  806. {
  807. struct scatterlist *sg_ptr;
  808. void *block;
  809. int i, ret;
  810. ret = sg_alloc_table(win->sgt, nr_segs, GFP_KERNEL);
  811. if (ret)
  812. return -ENOMEM;
  813. for_each_sg(win->sgt->sgl, sg_ptr, nr_segs, i) {
  814. block = dma_alloc_coherent(msc_dev(win->msc)->parent->parent,
  815. PAGE_SIZE, &sg_dma_address(sg_ptr),
  816. GFP_KERNEL);
  817. if (!block)
  818. goto err_nomem;
  819. sg_set_buf(sg_ptr, block, PAGE_SIZE);
  820. }
  821. return nr_segs;
  822. err_nomem:
  823. for_each_sg(win->sgt->sgl, sg_ptr, i, ret)
  824. dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
  825. sg_virt(sg_ptr), sg_dma_address(sg_ptr));
  826. sg_free_table(win->sgt);
  827. return -ENOMEM;
  828. }
  829. #ifdef CONFIG_X86
  830. static void msc_buffer_set_uc(struct msc *msc)
  831. {
  832. struct scatterlist *sg_ptr;
  833. struct msc_window *win;
  834. int i;
  835. if (msc->mode == MSC_MODE_SINGLE) {
  836. set_memory_uc((unsigned long)msc->base, msc->nr_pages);
  837. return;
  838. }
  839. list_for_each_entry(win, &msc->win_list, entry) {
  840. for_each_sg(win->sgt->sgl, sg_ptr, win->nr_segs, i) {
  841. /* Set the page as uncached */
  842. set_memory_uc((unsigned long)sg_virt(sg_ptr),
  843. PFN_DOWN(sg_ptr->length));
  844. }
  845. }
  846. }
  847. static void msc_buffer_set_wb(struct msc *msc)
  848. {
  849. struct scatterlist *sg_ptr;
  850. struct msc_window *win;
  851. int i;
  852. if (msc->mode == MSC_MODE_SINGLE) {
  853. set_memory_wb((unsigned long)msc->base, msc->nr_pages);
  854. return;
  855. }
  856. list_for_each_entry(win, &msc->win_list, entry) {
  857. for_each_sg(win->sgt->sgl, sg_ptr, win->nr_segs, i) {
  858. /* Reset the page to write-back */
  859. set_memory_wb((unsigned long)sg_virt(sg_ptr),
  860. PFN_DOWN(sg_ptr->length));
  861. }
  862. }
  863. }
  864. #else /* !X86 */
  865. static inline void
  866. msc_buffer_set_uc(struct msc *msc) {}
  867. static inline void msc_buffer_set_wb(struct msc *msc) {}
  868. #endif /* CONFIG_X86 */
  869. static struct page *msc_sg_page(struct scatterlist *sg)
  870. {
  871. void *addr = sg_virt(sg);
  872. if (is_vmalloc_addr(addr))
  873. return vmalloc_to_page(addr);
  874. return sg_page(sg);
  875. }
  876. /**
  877. * msc_buffer_win_alloc() - alloc a window for a multiblock mode
  878. * @msc: MSC device
  879. * @nr_blocks: number of pages in this window
  880. *
  881. * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
  882. * to serialize, so the caller is expected to hold it.
  883. *
  884. * Return: 0 on success, -errno otherwise.
  885. */
  886. static int msc_buffer_win_alloc(struct msc *msc, unsigned int nr_blocks)
  887. {
  888. struct msc_window *win;
  889. int ret = -ENOMEM;
  890. if (!nr_blocks)
  891. return 0;
  892. win = kzalloc(sizeof(*win), GFP_KERNEL);
  893. if (!win)
  894. return -ENOMEM;
  895. win->msc = msc;
  896. win->sgt = &win->_sgt;
  897. win->lockout = WIN_READY;
  898. spin_lock_init(&win->lo_lock);
  899. if (!list_empty(&msc->win_list)) {
  900. struct msc_window *prev = list_last_entry(&msc->win_list,
  901. struct msc_window,
  902. entry);
  903. win->pgoff = prev->pgoff + prev->nr_blocks;
  904. }
  905. if (msc->mbuf && msc->mbuf->alloc_window)
  906. ret = msc->mbuf->alloc_window(msc->mbuf_priv, &win->sgt,
  907. nr_blocks << PAGE_SHIFT);
  908. else
  909. ret = __msc_buffer_win_alloc(win, nr_blocks);
  910. if (ret <= 0)
  911. goto err_nomem;
  912. win->nr_segs = ret;
  913. win->nr_blocks = nr_blocks;
  914. if (list_empty(&msc->win_list)) {
  915. msc->base = msc_win_base(win);
  916. msc->base_addr = msc_win_base_dma(win);
  917. msc->cur_win = win;
  918. }
  919. list_add_tail(&win->entry, &msc->win_list);
  920. msc->nr_pages += nr_blocks;
  921. return 0;
  922. err_nomem:
  923. kfree(win);
  924. return ret;
  925. }
  926. static void __msc_buffer_win_free(struct msc *msc, struct msc_window *win)
  927. {
  928. struct scatterlist *sg;
  929. int i;
  930. for_each_sg(win->sgt->sgl, sg, win->nr_segs, i) {
  931. dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
  932. sg_virt(sg), sg_dma_address(sg));
  933. }
  934. sg_free_table(win->sgt);
  935. }
  936. /**
  937. * msc_buffer_win_free() - free a window from MSC's window list
  938. * @msc: MSC device
  939. * @win: window to free
  940. *
  941. * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
  942. * to serialize, so the caller is expected to hold it.
  943. */
  944. static void msc_buffer_win_free(struct msc *msc, struct msc_window *win)
  945. {
  946. msc->nr_pages -= win->nr_blocks;
  947. list_del(&win->entry);
  948. if (list_empty(&msc->win_list)) {
  949. msc->base = NULL;
  950. msc->base_addr = 0;
  951. }
  952. if (msc->mbuf && msc->mbuf->free_window)
  953. msc->mbuf->free_window(msc->mbuf_priv, win->sgt);
  954. else
  955. __msc_buffer_win_free(msc, win);
  956. kfree(win);
  957. }
  958. /**
  959. * msc_buffer_relink() - set up block descriptors for multiblock mode
  960. * @msc: MSC device
  961. *
  962. * This traverses msc::win_list, which requires msc::buf_mutex to serialize,
  963. * so the caller is expected to hold it.
  964. */
  965. static void msc_buffer_relink(struct msc *msc)
  966. {
  967. struct msc_window *win, *next_win;
  968. /* call with msc::mutex locked */
  969. list_for_each_entry(win, &msc->win_list, entry) {
  970. struct scatterlist *sg;
  971. unsigned int blk;
  972. u32 sw_tag = 0;
  973. /*
  974. * Last window's next_win should point to the first window
  975. * and MSC_SW_TAG_LASTWIN should be set.
  976. */
  977. if (msc_is_last_win(win)) {
  978. sw_tag |= MSC_SW_TAG_LASTWIN;
  979. next_win = list_first_entry(&msc->win_list,
  980. struct msc_window, entry);
  981. } else {
  982. next_win = list_next_entry(win, entry);
  983. }
  984. for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
  985. struct msc_block_desc *bdesc = sg_virt(sg);
  986. memset(bdesc, 0, sizeof(*bdesc));
  987. bdesc->next_win = msc_win_base_pfn(next_win);
  988. /*
  989. * Similarly to last window, last block should point
  990. * to the first one.
  991. */
  992. if (blk == win->nr_segs - 1) {
  993. sw_tag |= MSC_SW_TAG_LASTBLK;
  994. bdesc->next_blk = msc_win_base_pfn(win);
  995. } else {
  996. dma_addr_t addr = sg_dma_address(sg_next(sg));
  997. bdesc->next_blk = PFN_DOWN(addr);
  998. }
  999. bdesc->sw_tag = sw_tag;
  1000. bdesc->block_sz = sg->length / 64;
  1001. }
  1002. }
  1003. /*
  1004. * Make the above writes globally visible before tracing is
  1005. * enabled to make sure hardware sees them coherently.
  1006. */
  1007. wmb();
  1008. }
  1009. static void msc_buffer_multi_free(struct msc *msc)
  1010. {
  1011. struct msc_window *win, *iter;
  1012. list_for_each_entry_safe(win, iter, &msc->win_list, entry)
  1013. msc_buffer_win_free(msc, win);
  1014. }
  1015. static int msc_buffer_multi_alloc(struct msc *msc, unsigned long *nr_pages,
  1016. unsigned int nr_wins)
  1017. {
  1018. int ret, i;
  1019. for (i = 0; i < nr_wins; i++) {
  1020. ret = msc_buffer_win_alloc(msc, nr_pages[i]);
  1021. if (ret) {
  1022. msc_buffer_multi_free(msc);
  1023. return ret;
  1024. }
  1025. }
  1026. msc_buffer_relink(msc);
  1027. return 0;
  1028. }
  1029. /**
  1030. * msc_buffer_free() - free buffers for MSC
  1031. * @msc: MSC device
  1032. *
  1033. * Free MSC's storage buffers.
  1034. *
  1035. * This modifies msc::win_list and msc::base, which requires msc::buf_mutex to
  1036. * serialize, so the caller is expected to hold it.
  1037. */
  1038. static void msc_buffer_free(struct msc *msc)
  1039. {
  1040. msc_buffer_set_wb(msc);
  1041. if (msc->mode == MSC_MODE_SINGLE)
  1042. msc_buffer_contig_free(msc);
  1043. else if (msc->mode == MSC_MODE_MULTI)
  1044. msc_buffer_multi_free(msc);
  1045. }
  1046. /**
  1047. * msc_buffer_alloc() - allocate a buffer for MSC
  1048. * @msc: MSC device
  1049. * @nr_pages: number of pages for each window
  1050. * @nr_wins: number of windows
  1051. *
  1052. * Allocate a storage buffer for MSC, depending on the msc::mode, it will be
  1053. * either done via msc_buffer_contig_alloc() for SINGLE operation mode or
  1054. * msc_buffer_win_alloc() for multiblock operation. The latter allocates one
  1055. * window per invocation, so in multiblock mode this can be called multiple
  1056. * times for the same MSC to allocate multiple windows.
  1057. *
  1058. * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
  1059. * to serialize, so the caller is expected to hold it.
  1060. *
  1061. * Return: 0 on success, -errno otherwise.
  1062. */
  1063. static int msc_buffer_alloc(struct msc *msc, unsigned long *nr_pages,
  1064. unsigned int nr_wins)
  1065. {
  1066. int ret;
  1067. /* -1: buffer not allocated */
  1068. if (atomic_read(&msc->user_count) != -1)
  1069. return -EBUSY;
  1070. if (msc->mode == MSC_MODE_SINGLE) {
  1071. if (nr_wins != 1)
  1072. return -EINVAL;
  1073. ret = msc_buffer_contig_alloc(msc, nr_pages[0] << PAGE_SHIFT);
  1074. } else if (msc->mode == MSC_MODE_MULTI) {
  1075. ret = msc_buffer_multi_alloc(msc, nr_pages, nr_wins);
  1076. } else {
  1077. ret = -EINVAL;
  1078. }
  1079. if (!ret) {
  1080. msc_buffer_set_uc(msc);
  1081. /* allocation should be visible before the counter goes to 0 */
  1082. smp_mb__before_atomic();
  1083. if (WARN_ON_ONCE(atomic_cmpxchg(&msc->user_count, -1, 0) != -1))
  1084. return -EINVAL;
  1085. }
  1086. return ret;
  1087. }
  1088. /**
  1089. * msc_buffer_unlocked_free_unless_used() - free a buffer unless it's in use
  1090. * @msc: MSC device
  1091. *
  1092. * This will free MSC buffer unless it is in use or there is no allocated
  1093. * buffer.
  1094. * Caller needs to hold msc::buf_mutex.
  1095. *
  1096. * Return: 0 on successful deallocation or if there was no buffer to
  1097. * deallocate, -EBUSY if there are active users.
  1098. */
  1099. static int msc_buffer_unlocked_free_unless_used(struct msc *msc)
  1100. {
  1101. int count, ret = 0;
  1102. count = atomic_cmpxchg(&msc->user_count, 0, -1);
  1103. /* > 0: buffer is allocated and has users */
  1104. if (count > 0)
  1105. ret = -EBUSY;
  1106. /* 0: buffer is allocated, no users */
  1107. else if (!count)
  1108. msc_buffer_free(msc);
  1109. /* < 0: no buffer, nothing to do */
  1110. return ret;
  1111. }
  1112. /**
  1113. * msc_buffer_free_unless_used() - free a buffer unless it's in use
  1114. * @msc: MSC device
  1115. *
  1116. * This is a locked version of msc_buffer_unlocked_free_unless_used().
  1117. *
  1118. * Return: 0 on successful deallocation or if there was no buffer to
  1119. * deallocate, -EBUSY if there are active users.
  1120. */
  1121. static int msc_buffer_free_unless_used(struct msc *msc)
  1122. {
  1123. int ret;
  1124. mutex_lock(&msc->buf_mutex);
  1125. ret = msc_buffer_unlocked_free_unless_used(msc);
  1126. mutex_unlock(&msc->buf_mutex);
  1127. return ret;
  1128. }
  1129. /**
  1130. * msc_buffer_get_page() - get MSC buffer page at a given offset
  1131. * @msc: MSC device
  1132. * @pgoff: page offset into the storage buffer
  1133. *
  1134. * This traverses msc::win_list, so holding msc::buf_mutex is expected from
  1135. * the caller.
  1136. *
  1137. * Return: page if @pgoff corresponds to a valid buffer page or NULL.
  1138. */
  1139. static struct page *msc_buffer_get_page(struct msc *msc, unsigned long pgoff)
  1140. {
  1141. struct msc_window *win;
  1142. struct scatterlist *sg;
  1143. unsigned int blk;
  1144. if (msc->mode == MSC_MODE_SINGLE)
  1145. return msc_buffer_contig_get_page(msc, pgoff);
  1146. list_for_each_entry(win, &msc->win_list, entry)
  1147. if (pgoff >= win->pgoff && pgoff < win->pgoff + win->nr_blocks)
  1148. goto found;
  1149. return NULL;
  1150. found:
  1151. pgoff -= win->pgoff;
  1152. for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
  1153. struct page *page = msc_sg_page(sg);
  1154. size_t pgsz = PFN_DOWN(sg->length);
  1155. if (pgoff < pgsz)
  1156. return page + pgoff;
  1157. pgoff -= pgsz;
  1158. }
  1159. return NULL;
  1160. }
  1161. /**
  1162. * struct msc_win_to_user_struct - data for copy_to_user() callback
  1163. * @buf: userspace buffer to copy data to
  1164. * @offset: running offset
  1165. */
  1166. struct msc_win_to_user_struct {
  1167. char __user *buf;
  1168. unsigned long offset;
  1169. };
  1170. /**
  1171. * msc_win_to_user() - iterator for msc_buffer_iterate() to copy data to user
  1172. * @data: callback's private data
  1173. * @src: source buffer
  1174. * @len: amount of data to copy from the source buffer
  1175. *
  1176. * Return: >= %0 for success or -errno for error.
  1177. */
  1178. static unsigned long msc_win_to_user(void *data, void *src, size_t len)
  1179. {
  1180. struct msc_win_to_user_struct *u = data;
  1181. unsigned long ret;
  1182. ret = copy_to_user(u->buf + u->offset, src, len);
  1183. u->offset += len - ret;
  1184. return ret;
  1185. }
  1186. /*
  1187. * file operations' callbacks
  1188. */
  1189. static int intel_th_msc_open(struct inode *inode, struct file *file)
  1190. {
  1191. struct intel_th_device *thdev = file->private_data;
  1192. struct msc *msc = dev_get_drvdata(&thdev->dev);
  1193. struct msc_iter *iter;
  1194. if (!capable(CAP_SYS_RAWIO))
  1195. return -EPERM;
  1196. iter = msc_iter_install(msc);
  1197. if (IS_ERR(iter))
  1198. return PTR_ERR(iter);
  1199. file->private_data = iter;
  1200. return nonseekable_open(inode, file);
  1201. }
  1202. static int intel_th_msc_release(struct inode *inode, struct file *file)
  1203. {
  1204. struct msc_iter *iter = file->private_data;
  1205. struct msc *msc = iter->msc;
  1206. msc_iter_remove(iter, msc);
  1207. return 0;
  1208. }
  1209. static ssize_t
  1210. msc_single_to_user(struct msc *msc, char __user *buf, loff_t off, size_t len)
  1211. {
  1212. unsigned long size = msc->nr_pages << PAGE_SHIFT, rem = len;
  1213. unsigned long start = off, tocopy = 0;
  1214. if (msc->single_wrap) {
  1215. start += msc->single_sz;
  1216. if (start < size) {
  1217. tocopy = min(rem, size - start);
  1218. if (copy_to_user(buf, msc->base + start, tocopy))
  1219. return -EFAULT;
  1220. buf += tocopy;
  1221. rem -= tocopy;
  1222. start += tocopy;
  1223. }
  1224. start &= size - 1;
  1225. if (rem) {
  1226. tocopy = min(rem, msc->single_sz - start);
  1227. if (copy_to_user(buf, msc->base + start, tocopy))
  1228. return -EFAULT;
  1229. rem -= tocopy;
  1230. }
  1231. return len - rem;
  1232. }
  1233. if (copy_to_user(buf, msc->base + start, rem))
  1234. return -EFAULT;
  1235. return len;
  1236. }
  1237. static ssize_t intel_th_msc_read(struct file *file, char __user *buf,
  1238. size_t len, loff_t *ppos)
  1239. {
  1240. struct msc_iter *iter = file->private_data;
  1241. struct msc *msc = iter->msc;
  1242. size_t size;
  1243. loff_t off = *ppos;
  1244. ssize_t ret = 0;
  1245. if (!atomic_inc_unless_negative(&msc->user_count))
  1246. return 0;
  1247. if (msc->mode == MSC_MODE_SINGLE && !msc->single_wrap)
  1248. size = msc->single_sz;
  1249. else
  1250. size = msc->nr_pages << PAGE_SHIFT;
  1251. if (!size)
  1252. goto put_count;
  1253. if (off >= size)
  1254. goto put_count;
  1255. if (off + len >= size)
  1256. len = size - off;
  1257. if (msc->mode == MSC_MODE_SINGLE) {
  1258. ret = msc_single_to_user(msc, buf, off, len);
  1259. if (ret >= 0)
  1260. *ppos += ret;
  1261. } else if (msc->mode == MSC_MODE_MULTI) {
  1262. struct msc_win_to_user_struct u = {
  1263. .buf = buf,
  1264. .offset = 0,
  1265. };
  1266. ret = msc_buffer_iterate(iter, len, &u, msc_win_to_user);
  1267. if (ret >= 0)
  1268. *ppos = iter->offset;
  1269. } else {
  1270. ret = -EINVAL;
  1271. }
  1272. put_count:
  1273. atomic_dec(&msc->user_count);
  1274. return ret;
  1275. }
  1276. /*
  1277. * vm operations callbacks (vm_ops)
  1278. */
  1279. static void msc_mmap_open(struct vm_area_struct *vma)
  1280. {
  1281. struct msc_iter *iter = vma->vm_file->private_data;
  1282. struct msc *msc = iter->msc;
  1283. atomic_inc(&msc->mmap_count);
  1284. }
  1285. static void msc_mmap_close(struct vm_area_struct *vma)
  1286. {
  1287. struct msc_iter *iter = vma->vm_file->private_data;
  1288. struct msc *msc = iter->msc;
  1289. if (!atomic_dec_and_mutex_lock(&msc->mmap_count, &msc->buf_mutex))
  1290. return;
  1291. /* last mapping -- drop user_count */
  1292. atomic_dec(&msc->user_count);
  1293. mutex_unlock(&msc->buf_mutex);
  1294. }
  1295. static vm_fault_t msc_mmap_fault(struct vm_fault *vmf)
  1296. {
  1297. struct msc_iter *iter = vmf->vma->vm_file->private_data;
  1298. struct msc *msc = iter->msc;
  1299. struct page *page;
  1300. page = msc_buffer_get_page(msc, vmf->pgoff);
  1301. if (!page)
  1302. return VM_FAULT_SIGBUS;
  1303. get_page(page);
  1304. return vmf_insert_mixed(vmf->vma, vmf->address, page_to_pfn_t(page));
  1305. }
  1306. static const struct vm_operations_struct msc_mmap_ops = {
  1307. .open = msc_mmap_open,
  1308. .close = msc_mmap_close,
  1309. .fault = msc_mmap_fault,
  1310. };
  1311. static int intel_th_msc_mmap(struct file *file, struct vm_area_struct *vma)
  1312. {
  1313. unsigned long size = vma->vm_end - vma->vm_start;
  1314. struct msc_iter *iter = vma->vm_file->private_data;
  1315. struct msc *msc = iter->msc;
  1316. int ret = -EINVAL;
  1317. if (!size || offset_in_page(size))
  1318. return -EINVAL;
  1319. if (vma->vm_pgoff)
  1320. return -EINVAL;
  1321. /* grab user_count once per mmap; drop in msc_mmap_close() */
  1322. if (!atomic_inc_unless_negative(&msc->user_count))
  1323. return -EINVAL;
  1324. if (msc->mode != MSC_MODE_SINGLE &&
  1325. msc->mode != MSC_MODE_MULTI)
  1326. goto out;
  1327. if (size >> PAGE_SHIFT != msc->nr_pages)
  1328. goto out;
  1329. atomic_set(&msc->mmap_count, 1);
  1330. ret = 0;
  1331. out:
  1332. if (ret)
  1333. atomic_dec(&msc->user_count);
  1334. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  1335. vm_flags_set(vma, VM_DONTEXPAND | VM_DONTCOPY | VM_MIXEDMAP);
  1336. vma->vm_ops = &msc_mmap_ops;
  1337. return ret;
  1338. }
  1339. static const struct file_operations intel_th_msc_fops = {
  1340. .open = intel_th_msc_open,
  1341. .release = intel_th_msc_release,
  1342. .read = intel_th_msc_read,
  1343. .mmap = intel_th_msc_mmap,
  1344. .owner = THIS_MODULE,
  1345. };
  1346. static void intel_th_msc_wait_empty(struct intel_th_device *thdev)
  1347. {
  1348. struct msc *msc = dev_get_drvdata(&thdev->dev);
  1349. unsigned long count;
  1350. u32 reg;
  1351. for (reg = 0, count = MSC_PLE_WAITLOOP_DEPTH;
  1352. count && !(reg & MSCSTS_PLE); count--) {
  1353. reg = __raw_readl(msc->reg_base + REG_MSU_MSC0STS);
  1354. cpu_relax();
  1355. }
  1356. if (!count)
  1357. dev_dbg(msc_dev(msc), "timeout waiting for MSC0 PLE\n");
  1358. }
  1359. static int intel_th_msc_init(struct msc *msc)
  1360. {
  1361. atomic_set(&msc->user_count, -1);
  1362. msc->mode = msc->multi_is_broken ? MSC_MODE_SINGLE : MSC_MODE_MULTI;
  1363. mutex_init(&msc->buf_mutex);
  1364. INIT_LIST_HEAD(&msc->win_list);
  1365. INIT_LIST_HEAD(&msc->iter_list);
  1366. msc->burst_len =
  1367. (ioread32(msc->reg_base + REG_MSU_MSC0CTL) & MSC_LEN) >>
  1368. __ffs(MSC_LEN);
  1369. return 0;
  1370. }
  1371. static int msc_win_switch(struct msc *msc)
  1372. {
  1373. struct msc_window *first;
  1374. if (list_empty(&msc->win_list))
  1375. return -EINVAL;
  1376. first = list_first_entry(&msc->win_list, struct msc_window, entry);
  1377. if (msc_is_last_win(msc->cur_win))
  1378. msc->cur_win = first;
  1379. else
  1380. msc->cur_win = list_next_entry(msc->cur_win, entry);
  1381. msc->base = msc_win_base(msc->cur_win);
  1382. msc->base_addr = msc_win_base_dma(msc->cur_win);
  1383. intel_th_trace_switch(msc->thdev);
  1384. return 0;
  1385. }
  1386. /**
  1387. * intel_th_msc_window_unlock - put the window back in rotation
  1388. * @dev: MSC device to which this relates
  1389. * @sgt: buffer's sg_table for the window, does nothing if NULL
  1390. */
  1391. void intel_th_msc_window_unlock(struct device *dev, struct sg_table *sgt)
  1392. {
  1393. struct msc *msc = dev_get_drvdata(dev);
  1394. struct msc_window *win;
  1395. if (!sgt)
  1396. return;
  1397. win = msc_find_window(msc, sgt, false);
  1398. if (!win)
  1399. return;
  1400. msc_win_set_lockout(win, WIN_LOCKED, WIN_READY);
  1401. if (msc->switch_on_unlock == win) {
  1402. msc->switch_on_unlock = NULL;
  1403. msc_win_switch(msc);
  1404. }
  1405. }
  1406. EXPORT_SYMBOL_GPL(intel_th_msc_window_unlock);
  1407. static void msc_work(struct work_struct *work)
  1408. {
  1409. struct msc *msc = container_of(work, struct msc, work);
  1410. intel_th_msc_deactivate(msc->thdev);
  1411. }
  1412. static irqreturn_t intel_th_msc_interrupt(struct intel_th_device *thdev)
  1413. {
  1414. struct msc *msc = dev_get_drvdata(&thdev->dev);
  1415. u32 msusts = ioread32(msc->msu_base + REG_MSU_MSUSTS);
  1416. u32 mask = msc->index ? MSUSTS_MSC1BLAST : MSUSTS_MSC0BLAST;
  1417. struct msc_window *win, *next_win;
  1418. if (!msc->do_irq || !msc->mbuf)
  1419. return IRQ_NONE;
  1420. msusts &= mask;
  1421. if (!msusts)
  1422. return msc->enabled ? IRQ_HANDLED : IRQ_NONE;
  1423. iowrite32(msusts, msc->msu_base + REG_MSU_MSUSTS);
  1424. if (!msc->enabled)
  1425. return IRQ_NONE;
  1426. /* grab the window before we do the switch */
  1427. win = msc->cur_win;
  1428. if (!win)
  1429. return IRQ_HANDLED;
  1430. next_win = msc_next_window(win);
  1431. if (!next_win)
  1432. return IRQ_HANDLED;
  1433. /* next window: if READY, proceed, if LOCKED, stop the trace */
  1434. if (msc_win_set_lockout(next_win, WIN_READY, WIN_INUSE)) {
  1435. if (msc->stop_on_full)
  1436. schedule_work(&msc->work);
  1437. else
  1438. msc->switch_on_unlock = next_win;
  1439. return IRQ_HANDLED;
  1440. }
  1441. /* current window: INUSE -> LOCKED */
  1442. msc_win_set_lockout(win, WIN_INUSE, WIN_LOCKED);
  1443. msc_win_switch(msc);
  1444. if (msc->mbuf && msc->mbuf->ready)
  1445. msc->mbuf->ready(msc->mbuf_priv, win->sgt,
  1446. msc_win_total_sz(win));
  1447. return IRQ_HANDLED;
  1448. }
  1449. static const char * const msc_mode[] = {
  1450. [MSC_MODE_SINGLE] = "single",
  1451. [MSC_MODE_MULTI] = "multi",
  1452. [MSC_MODE_EXI] = "ExI",
  1453. [MSC_MODE_DEBUG] = "debug",
  1454. };
  1455. static ssize_t
  1456. wrap_show(struct device *dev, struct device_attribute *attr, char *buf)
  1457. {
  1458. struct msc *msc = dev_get_drvdata(dev);
  1459. return scnprintf(buf, PAGE_SIZE, "%d\n", msc->wrap);
  1460. }
  1461. static ssize_t
  1462. wrap_store(struct device *dev, struct device_attribute *attr, const char *buf,
  1463. size_t size)
  1464. {
  1465. struct msc *msc = dev_get_drvdata(dev);
  1466. unsigned long val;
  1467. int ret;
  1468. ret = kstrtoul(buf, 10, &val);
  1469. if (ret)
  1470. return ret;
  1471. msc->wrap = !!val;
  1472. return size;
  1473. }
  1474. static DEVICE_ATTR_RW(wrap);
  1475. static void msc_buffer_unassign(struct msc *msc)
  1476. {
  1477. lockdep_assert_held(&msc->buf_mutex);
  1478. if (!msc->mbuf)
  1479. return;
  1480. msc->mbuf->unassign(msc->mbuf_priv);
  1481. msu_buffer_put(msc->mbuf);
  1482. msc->mbuf_priv = NULL;
  1483. msc->mbuf = NULL;
  1484. }
  1485. static ssize_t
  1486. mode_show(struct device *dev, struct device_attribute *attr, char *buf)
  1487. {
  1488. struct msc *msc = dev_get_drvdata(dev);
  1489. const char *mode = msc_mode[msc->mode];
  1490. ssize_t ret;
  1491. mutex_lock(&msc->buf_mutex);
  1492. if (msc->mbuf)
  1493. mode = msc->mbuf->name;
  1494. ret = scnprintf(buf, PAGE_SIZE, "%s\n", mode);
  1495. mutex_unlock(&msc->buf_mutex);
  1496. return ret;
  1497. }
  1498. static ssize_t
  1499. mode_store(struct device *dev, struct device_attribute *attr, const char *buf,
  1500. size_t size)
  1501. {
  1502. const struct msu_buffer *mbuf = NULL;
  1503. struct msc *msc = dev_get_drvdata(dev);
  1504. size_t len = size;
  1505. char *cp, *mode;
  1506. int i, ret;
  1507. if (!capable(CAP_SYS_RAWIO))
  1508. return -EPERM;
  1509. cp = memchr(buf, '\n', len);
  1510. if (cp)
  1511. len = cp - buf;
  1512. mode = kstrndup(buf, len, GFP_KERNEL);
  1513. if (!mode)
  1514. return -ENOMEM;
  1515. i = match_string(msc_mode, ARRAY_SIZE(msc_mode), mode);
  1516. if (i >= 0) {
  1517. kfree(mode);
  1518. goto found;
  1519. }
  1520. /* Buffer sinks only work with a usable IRQ */
  1521. if (!msc->do_irq) {
  1522. kfree(mode);
  1523. return -EINVAL;
  1524. }
  1525. mbuf = msu_buffer_get(mode);
  1526. kfree(mode);
  1527. if (mbuf)
  1528. goto found;
  1529. return -EINVAL;
  1530. found:
  1531. if (i == MSC_MODE_MULTI && msc->multi_is_broken)
  1532. return -EOPNOTSUPP;
  1533. mutex_lock(&msc->buf_mutex);
  1534. ret = 0;
  1535. /* Same buffer: do nothing */
  1536. if (mbuf && mbuf == msc->mbuf) {
  1537. /* put the extra reference we just got */
  1538. msu_buffer_put(mbuf);
  1539. goto unlock;
  1540. }
  1541. ret = msc_buffer_unlocked_free_unless_used(msc);
  1542. if (ret)
  1543. goto unlock;
  1544. if (mbuf) {
  1545. void *mbuf_priv = mbuf->assign(dev, &i);
  1546. if (!mbuf_priv) {
  1547. ret = -ENOMEM;
  1548. goto unlock;
  1549. }
  1550. msc_buffer_unassign(msc);
  1551. msc->mbuf_priv = mbuf_priv;
  1552. msc->mbuf = mbuf;
  1553. } else {
  1554. msc_buffer_unassign(msc);
  1555. }
  1556. msc->mode = i;
  1557. unlock:
  1558. if (ret && mbuf)
  1559. msu_buffer_put(mbuf);
  1560. mutex_unlock(&msc->buf_mutex);
  1561. return ret ? ret : size;
  1562. }
  1563. static DEVICE_ATTR_RW(mode);
  1564. static ssize_t
  1565. nr_pages_show(struct device *dev, struct device_attribute *attr, char *buf)
  1566. {
  1567. struct msc *msc = dev_get_drvdata(dev);
  1568. struct msc_window *win;
  1569. size_t count = 0;
  1570. mutex_lock(&msc->buf_mutex);
  1571. if (msc->mode == MSC_MODE_SINGLE)
  1572. count = scnprintf(buf, PAGE_SIZE, "%ld\n", msc->nr_pages);
  1573. else if (msc->mode == MSC_MODE_MULTI) {
  1574. list_for_each_entry(win, &msc->win_list, entry) {
  1575. count += scnprintf(buf + count, PAGE_SIZE - count,
  1576. "%d%c", win->nr_blocks,
  1577. msc_is_last_win(win) ? '\n' : ',');
  1578. }
  1579. } else {
  1580. count = scnprintf(buf, PAGE_SIZE, "unsupported\n");
  1581. }
  1582. mutex_unlock(&msc->buf_mutex);
  1583. return count;
  1584. }
  1585. static ssize_t
  1586. nr_pages_store(struct device *dev, struct device_attribute *attr,
  1587. const char *buf, size_t size)
  1588. {
  1589. struct msc *msc = dev_get_drvdata(dev);
  1590. unsigned long val, *win = NULL, *rewin;
  1591. size_t len = size;
  1592. const char *p = buf;
  1593. char *end, *s;
  1594. int ret, nr_wins = 0;
  1595. if (!capable(CAP_SYS_RAWIO))
  1596. return -EPERM;
  1597. ret = msc_buffer_free_unless_used(msc);
  1598. if (ret)
  1599. return ret;
  1600. /* scan the comma-separated list of allocation sizes */
  1601. end = memchr(buf, '\n', len);
  1602. if (end)
  1603. len = end - buf;
  1604. do {
  1605. end = memchr(p, ',', len);
  1606. s = kstrndup(p, end ? end - p : len, GFP_KERNEL);
  1607. if (!s) {
  1608. ret = -ENOMEM;
  1609. goto free_win;
  1610. }
  1611. ret = kstrtoul(s, 10, &val);
  1612. kfree(s);
  1613. if (ret || !val)
  1614. goto free_win;
  1615. if (nr_wins && msc->mode == MSC_MODE_SINGLE) {
  1616. ret = -EINVAL;
  1617. goto free_win;
  1618. }
  1619. nr_wins++;
  1620. rewin = krealloc_array(win, nr_wins, sizeof(*win), GFP_KERNEL);
  1621. if (!rewin) {
  1622. kfree(win);
  1623. return -ENOMEM;
  1624. }
  1625. win = rewin;
  1626. win[nr_wins - 1] = val;
  1627. if (!end)
  1628. break;
  1629. /* consume the number and the following comma, hence +1 */
  1630. len -= end - p + 1;
  1631. p = end + 1;
  1632. } while (len);
  1633. mutex_lock(&msc->buf_mutex);
  1634. ret = msc_buffer_alloc(msc, win, nr_wins);
  1635. mutex_unlock(&msc->buf_mutex);
  1636. free_win:
  1637. kfree(win);
  1638. return ret ? ret : size;
  1639. }
  1640. static DEVICE_ATTR_RW(nr_pages);
  1641. static ssize_t
  1642. win_switch_store(struct device *dev, struct device_attribute *attr,
  1643. const char *buf, size_t size)
  1644. {
  1645. struct msc *msc = dev_get_drvdata(dev);
  1646. unsigned long val;
  1647. int ret;
  1648. ret = kstrtoul(buf, 10, &val);
  1649. if (ret)
  1650. return ret;
  1651. if (val != 1)
  1652. return -EINVAL;
  1653. ret = -EINVAL;
  1654. mutex_lock(&msc->buf_mutex);
  1655. /*
  1656. * Window switch can only happen in the "multi" mode.
  1657. * If a external buffer is engaged, they have the full
  1658. * control over window switching.
  1659. */
  1660. if (msc->mode == MSC_MODE_MULTI && !msc->mbuf)
  1661. ret = msc_win_switch(msc);
  1662. mutex_unlock(&msc->buf_mutex);
  1663. return ret ? ret : size;
  1664. }
  1665. static DEVICE_ATTR_WO(win_switch);
  1666. static ssize_t stop_on_full_show(struct device *dev,
  1667. struct device_attribute *attr, char *buf)
  1668. {
  1669. struct msc *msc = dev_get_drvdata(dev);
  1670. return sprintf(buf, "%d\n", msc->stop_on_full);
  1671. }
  1672. static ssize_t stop_on_full_store(struct device *dev,
  1673. struct device_attribute *attr,
  1674. const char *buf, size_t size)
  1675. {
  1676. struct msc *msc = dev_get_drvdata(dev);
  1677. int ret;
  1678. ret = kstrtobool(buf, &msc->stop_on_full);
  1679. if (ret)
  1680. return ret;
  1681. return size;
  1682. }
  1683. static DEVICE_ATTR_RW(stop_on_full);
  1684. static struct attribute *msc_output_attrs[] = {
  1685. &dev_attr_wrap.attr,
  1686. &dev_attr_mode.attr,
  1687. &dev_attr_nr_pages.attr,
  1688. &dev_attr_win_switch.attr,
  1689. &dev_attr_stop_on_full.attr,
  1690. NULL,
  1691. };
  1692. static const struct attribute_group msc_output_group = {
  1693. .attrs = msc_output_attrs,
  1694. };
  1695. static int intel_th_msc_probe(struct intel_th_device *thdev)
  1696. {
  1697. struct device *dev = &thdev->dev;
  1698. struct resource *res;
  1699. struct msc *msc;
  1700. void __iomem *base;
  1701. int err;
  1702. res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
  1703. if (!res)
  1704. return -ENODEV;
  1705. base = devm_ioremap(dev, res->start, resource_size(res));
  1706. if (!base)
  1707. return -ENOMEM;
  1708. msc = devm_kzalloc(dev, sizeof(*msc), GFP_KERNEL);
  1709. if (!msc)
  1710. return -ENOMEM;
  1711. res = intel_th_device_get_resource(thdev, IORESOURCE_IRQ, 1);
  1712. if (!res)
  1713. msc->do_irq = 1;
  1714. if (INTEL_TH_CAP(to_intel_th(thdev), multi_is_broken))
  1715. msc->multi_is_broken = 1;
  1716. msc->index = thdev->id;
  1717. msc->thdev = thdev;
  1718. msc->reg_base = base + msc->index * 0x100;
  1719. msc->msu_base = base;
  1720. INIT_WORK(&msc->work, msc_work);
  1721. err = intel_th_msc_init(msc);
  1722. if (err)
  1723. return err;
  1724. dev_set_drvdata(dev, msc);
  1725. return 0;
  1726. }
  1727. static void intel_th_msc_remove(struct intel_th_device *thdev)
  1728. {
  1729. struct msc *msc = dev_get_drvdata(&thdev->dev);
  1730. int ret;
  1731. intel_th_msc_deactivate(thdev);
  1732. /*
  1733. * Buffers should not be used at this point except if the
  1734. * output character device is still open and the parent
  1735. * device gets detached from its bus, which is a FIXME.
  1736. */
  1737. ret = msc_buffer_free_unless_used(msc);
  1738. WARN_ON_ONCE(ret);
  1739. }
  1740. static struct intel_th_driver intel_th_msc_driver = {
  1741. .probe = intel_th_msc_probe,
  1742. .remove = intel_th_msc_remove,
  1743. .irq = intel_th_msc_interrupt,
  1744. .wait_empty = intel_th_msc_wait_empty,
  1745. .activate = intel_th_msc_activate,
  1746. .deactivate = intel_th_msc_deactivate,
  1747. .fops = &intel_th_msc_fops,
  1748. .attr_group = &msc_output_group,
  1749. .driver = {
  1750. .name = "msc",
  1751. .owner = THIS_MODULE,
  1752. },
  1753. };
  1754. module_driver(intel_th_msc_driver,
  1755. intel_th_driver_register,
  1756. intel_th_driver_unregister);
  1757. MODULE_LICENSE("GPL v2");
  1758. MODULE_DESCRIPTION("Intel(R) Trace Hub Memory Storage Unit driver");
  1759. MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");