sps_bam.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238
  1. /* Copyright (c) 2011-2014, 2016, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/types.h> /* u32 */
  13. #include <linux/kernel.h> /* pr_info() */
  14. #include <linux/mutex.h> /* mutex */
  15. #include <linux/list.h> /* list_head */
  16. #include <linux/slab.h> /* kzalloc() */
  17. #include <linux/interrupt.h> /* request_irq() */
  18. #include <linux/memory.h> /* memset */
  19. #include <linux/vmalloc.h>
  20. #include "sps_bam.h"
  21. #include "bam.h"
  22. #include "spsi.h"
  23. /* All BAM global IRQ sources */
  24. #define BAM_IRQ_ALL (BAM_DEV_IRQ_HRESP_ERROR | BAM_DEV_IRQ_ERROR | \
  25. BAM_DEV_IRQ_TIMER)
  26. /* BAM device state flags */
  27. #define BAM_STATE_INIT (1UL << 1)
  28. #define BAM_STATE_IRQ (1UL << 2)
  29. #define BAM_STATE_ENABLED (1UL << 3)
  30. #define BAM_STATE_BAM2BAM (1UL << 4)
  31. #define BAM_STATE_MTI (1UL << 5)
  32. #define BAM_STATE_REMOTE (1UL << 6)
  33. /* Mask for valid hardware descriptor flags */
  34. #define BAM_IOVEC_FLAG_MASK \
  35. (SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT | SPS_IOVEC_FLAG_EOB | \
  36. SPS_IOVEC_FLAG_NWD | SPS_IOVEC_FLAG_CMD | SPS_IOVEC_FLAG_LOCK | \
  37. SPS_IOVEC_FLAG_UNLOCK | SPS_IOVEC_FLAG_IMME)
  38. /* Mask for invalid BAM-to-BAM pipe options */
  39. #define BAM2BAM_O_INVALID \
  40. (SPS_O_DESC_DONE | \
  41. SPS_O_EOT | \
  42. SPS_O_POLL | \
  43. SPS_O_NO_Q | \
  44. SPS_O_ACK_TRANSFERS)
  45. /**
  46. * Pipe/client pointer value indicating pipe is allocated, but no client has
  47. * been assigned
  48. */
  49. #define BAM_PIPE_UNASSIGNED ((struct sps_pipe *)((~0x0ul) - 0x88888888))
  50. /* Check whether pipe has been assigned */
  51. #define BAM_PIPE_IS_ASSIGNED(p) \
  52. (((p) != NULL) && ((p) != BAM_PIPE_UNASSIGNED))
  53. /* Is MTI use supported for a specific BAM version? */
  54. #define BAM_VERSION_MTI_SUPPORT(ver) (ver <= 2)
  55. /* Event option<->event translation table entry */
  56. struct sps_bam_opt_event_table {
  57. enum sps_event event_id;
  58. enum sps_option option;
  59. enum bam_pipe_irq pipe_irq;
  60. };
  61. static const struct sps_bam_opt_event_table opt_event_table[] = {
  62. {SPS_EVENT_EOT, SPS_O_EOT, BAM_PIPE_IRQ_EOT},
  63. {SPS_EVENT_DESC_DONE, SPS_O_DESC_DONE, BAM_PIPE_IRQ_DESC_INT},
  64. {SPS_EVENT_WAKEUP, SPS_O_WAKEUP, BAM_PIPE_IRQ_WAKE},
  65. {SPS_EVENT_INACTIVE, SPS_O_INACTIVE, BAM_PIPE_IRQ_TIMER},
  66. {SPS_EVENT_OUT_OF_DESC, SPS_O_OUT_OF_DESC,
  67. BAM_PIPE_IRQ_OUT_OF_DESC},
  68. {SPS_EVENT_ERROR, SPS_O_ERROR, BAM_PIPE_IRQ_ERROR},
  69. {SPS_EVENT_RST_ERROR, SPS_O_RST_ERROR, BAM_PIPE_IRQ_RST_ERROR},
  70. {SPS_EVENT_HRESP_ERROR, SPS_O_HRESP_ERROR, BAM_PIPE_IRQ_HRESP_ERROR}
  71. };
  72. /* Pipe event source handler */
  73. static void pipe_handler(struct sps_bam *dev,
  74. struct sps_pipe *pipe);
  75. /**
  76. * Pipe transfer event (EOT, DESC_DONE) source handler.
  77. * This function is called by pipe_handler() and other functions to process the
  78. * descriptor FIFO.
  79. */
  80. static void pipe_handler_eot(struct sps_bam *dev,
  81. struct sps_pipe *pipe);
  82. /**
  83. * BAM driver initialization
  84. */
  85. int sps_bam_driver_init(u32 options)
  86. {
  87. int n;
  88. /*
  89. * Check that SPS_O_ and BAM_PIPE_IRQ_ values are identical.
  90. * This is required so that the raw pipe IRQ status can be passed
  91. * to the client in the SPS_EVENT_IRQ.
  92. */
  93. for (n = 0; n < ARRAY_SIZE(opt_event_table); n++) {
  94. if ((u32)opt_event_table[n].option !=
  95. (u32)opt_event_table[n].pipe_irq) {
  96. SPS_ERR("sps:SPS_O 0x%x != HAL IRQ 0x%x\n",
  97. opt_event_table[n].option,
  98. opt_event_table[n].pipe_irq);
  99. return SPS_ERROR;
  100. }
  101. }
  102. return 0;
  103. }
  104. /*
  105. * Check BAM interrupt
  106. */
  107. int sps_bam_check_irq(struct sps_bam *dev)
  108. {
  109. struct sps_pipe *pipe;
  110. u32 source;
  111. unsigned long flags = 0;
  112. int ret = 0;
  113. SPS_DBG1("sps:%s:bam=%pa.\n", __func__, BAM_ID(dev));
  114. spin_lock_irqsave(&dev->isr_lock, flags);
  115. polling:
  116. /* Get BAM interrupt source(s) */
  117. if ((dev->state & BAM_STATE_MTI) == 0) {
  118. u32 mask = dev->pipe_active_mask;
  119. enum sps_callback_case cb_case;
  120. source = bam_check_irq_source(dev->base, dev->props.ee,
  121. mask, &cb_case);
  122. SPS_DBG1("sps:bam=%pa;source=0x%x;mask=0x%x.\n",
  123. BAM_ID(dev), source, mask);
  124. if ((source & (1UL << 31)) && (dev->props.callback)) {
  125. SPS_DBG1("sps:bam=%pa;callback for case %d.\n",
  126. BAM_ID(dev), cb_case);
  127. dev->props.callback(cb_case, dev->props.user);
  128. }
  129. /* Mask any non-local source */
  130. source &= dev->pipe_active_mask;
  131. } else {
  132. /* If MTIs are used, must poll each active pipe */
  133. source = dev->pipe_active_mask;
  134. SPS_DBG1("sps:MTI:bam=%pa;source=0x%x.\n",
  135. BAM_ID(dev), source);
  136. }
  137. /* Process active pipe sources */
  138. pipe = list_first_entry(&dev->pipes_q, struct sps_pipe, list);
  139. list_for_each_entry(pipe, &dev->pipes_q, list) {
  140. /* Check this pipe's bit in the source mask */
  141. if (BAM_PIPE_IS_ASSIGNED(pipe)
  142. && (!pipe->disconnecting)
  143. && (source & pipe->pipe_index_mask)) {
  144. /* This pipe has an interrupt pending */
  145. pipe_handler(dev, pipe);
  146. source &= ~pipe->pipe_index_mask;
  147. }
  148. if (source == 0)
  149. break;
  150. }
  151. /* Process any inactive pipe sources */
  152. if (source) {
  153. SPS_ERR("sps:IRQ from BAM %pa inactive pipe(s) 0x%x\n",
  154. BAM_ID(dev), source);
  155. dev->irq_from_disabled_pipe++;
  156. }
  157. if (dev->props.options & SPS_BAM_RES_CONFIRM) {
  158. u32 mask = dev->pipe_active_mask;
  159. enum sps_callback_case cb_case;
  160. source = bam_check_irq_source(dev->base, dev->props.ee,
  161. mask, &cb_case);
  162. SPS_DBG1(
  163. "sps:check if there is any new IRQ coming:bam=%pa;source=0x%x;mask=0x%x.\n",
  164. BAM_ID(dev), source, mask);
  165. if ((source & (1UL << 31)) && (dev->props.callback)) {
  166. SPS_DBG1("sps:bam=%pa;callback for case %d.\n",
  167. BAM_ID(dev), cb_case);
  168. dev->props.callback(cb_case, dev->props.user);
  169. }
  170. if (source)
  171. goto polling;
  172. }
  173. spin_unlock_irqrestore(&dev->isr_lock, flags);
  174. return ret;
  175. }
  176. /**
  177. * BAM interrupt service routine
  178. *
  179. * This function is the BAM interrupt service routine.
  180. *
  181. * @ctxt - pointer to ISR's registered argument
  182. *
  183. * @return void
  184. */
  185. static irqreturn_t bam_isr(int irq, void *ctxt)
  186. {
  187. struct sps_bam *dev = ctxt;
  188. SPS_DBG1("sps:bam_isr: bam:%pa; IRQ #:%d.\n",
  189. BAM_ID(dev), irq);
  190. if (dev->props.options & SPS_BAM_RES_CONFIRM) {
  191. if (dev->props.callback) {
  192. bool ready = false;
  193. dev->props.callback(SPS_CALLBACK_BAM_RES_REQ, &ready);
  194. if (ready) {
  195. SPS_DBG1(
  196. "sps:bam_isr: handle IRQ for bam:%pa IRQ #:%d.\n",
  197. BAM_ID(dev), irq);
  198. sps_bam_check_irq(dev);
  199. dev->props.callback(SPS_CALLBACK_BAM_RES_REL,
  200. &ready);
  201. } else {
  202. SPS_DBG1(
  203. "sps:bam_isr: BAM is not ready and thus skip IRQ for bam:%pa IRQ #:%d.\n",
  204. BAM_ID(dev), irq);
  205. }
  206. } else {
  207. SPS_ERR(
  208. "sps:Client of BAM %pa requires confirmation but does not register callback\n",
  209. BAM_ID(dev));
  210. }
  211. } else {
  212. sps_bam_check_irq(dev);
  213. }
  214. return IRQ_HANDLED;
  215. }
  216. /**
  217. * BAM device enable
  218. */
  219. int sps_bam_enable(struct sps_bam *dev)
  220. {
  221. u32 num_pipes;
  222. u32 irq_mask;
  223. int result;
  224. int rc;
  225. int MTIenabled;
  226. /* Is this BAM enabled? */
  227. if ((dev->state & BAM_STATE_ENABLED))
  228. return 0; /* Yes, so no work to do */
  229. /* Is there any access to this BAM? */
  230. if ((dev->props.manage & SPS_BAM_MGR_ACCESS_MASK) == SPS_BAM_MGR_NONE) {
  231. SPS_ERR("sps:No local access to BAM %pa\n", BAM_ID(dev));
  232. return SPS_ERROR;
  233. }
  234. /* Set interrupt handling */
  235. if ((dev->props.options & SPS_BAM_OPT_IRQ_DISABLED) != 0 ||
  236. dev->props.irq == SPS_IRQ_INVALID) {
  237. /* Disable the BAM interrupt */
  238. irq_mask = 0;
  239. dev->state &= ~BAM_STATE_IRQ;
  240. } else {
  241. /* Register BAM ISR */
  242. if (dev->props.irq > 0) {
  243. if (dev->props.options & SPS_BAM_RES_CONFIRM) {
  244. result = request_irq(dev->props.irq,
  245. (irq_handler_t) bam_isr,
  246. IRQF_TRIGGER_RISING | IRQF_NO_SUSPEND,
  247. "sps", dev);
  248. SPS_DBG(
  249. "sps:BAM %pa uses edge for IRQ# %d\n",
  250. BAM_ID(dev), dev->props.irq);
  251. } else {
  252. result = request_irq(dev->props.irq,
  253. (irq_handler_t) bam_isr,
  254. IRQF_TRIGGER_HIGH | IRQF_NO_SUSPEND,
  255. "sps", dev);
  256. SPS_DBG(
  257. "sps:BAM %pa uses level for IRQ# %d\n",
  258. BAM_ID(dev), dev->props.irq);
  259. }
  260. } else {
  261. SPS_DBG1("sps:BAM %pa does not have an vaild IRQ# %d\n",
  262. BAM_ID(dev), dev->props.irq);
  263. }
  264. if (result) {
  265. SPS_ERR("sps:Failed to enable BAM %pa IRQ %d\n",
  266. BAM_ID(dev), dev->props.irq);
  267. return SPS_ERROR;
  268. }
  269. /* Enable the BAM interrupt */
  270. irq_mask = BAM_IRQ_ALL;
  271. dev->state |= BAM_STATE_IRQ;
  272. /* Register BAM IRQ for apps wakeup */
  273. if (dev->props.options & SPS_BAM_OPT_IRQ_WAKEUP) {
  274. result = enable_irq_wake(dev->props.irq);
  275. if (result) {
  276. SPS_ERR(
  277. "sps:Fail to enable wakeup irq for BAM %pa IRQ %d\n",
  278. BAM_ID(dev), dev->props.irq);
  279. return SPS_ERROR;
  280. } else
  281. SPS_DBG2(
  282. "sps:Enable wakeup irq for BAM %pa IRQ %d\n",
  283. BAM_ID(dev), dev->props.irq);
  284. }
  285. }
  286. /* Is global BAM control managed by the local processor? */
  287. num_pipes = 0;
  288. if ((dev->props.manage & SPS_BAM_MGR_DEVICE_REMOTE) == 0)
  289. /* Yes, so initialize the BAM device */
  290. rc = bam_init(dev->base,
  291. dev->props.ee,
  292. (u16) dev->props.summing_threshold,
  293. irq_mask,
  294. &dev->version, &num_pipes,
  295. dev->props.options);
  296. else
  297. /* No, so just verify that it is enabled */
  298. rc = bam_check(dev->base, &dev->version,
  299. dev->props.ee, &num_pipes);
  300. if (rc) {
  301. SPS_ERR("sps:Fail to init BAM %pa IRQ %d\n",
  302. BAM_ID(dev), dev->props.irq);
  303. return SPS_ERROR;
  304. }
  305. /* Check if this BAM supports MTIs (Message Triggered Interrupts) or
  306. * multiple EEs (Execution Environments).
  307. * MTI and EE support are mutually exclusive.
  308. */
  309. MTIenabled = BAM_VERSION_MTI_SUPPORT(dev->version);
  310. if ((dev->props.manage & SPS_BAM_MGR_DEVICE_REMOTE) != 0 &&
  311. (dev->props.manage & SPS_BAM_MGR_MULTI_EE) != 0 &&
  312. dev->props.ee == 0 && MTIenabled) {
  313. /*
  314. * BAM global is owned by remote processor and local processor
  315. * must use MTI. Thus, force EE index to a non-zero value to
  316. * insure that EE zero globals can't be modified.
  317. */
  318. SPS_ERR("sps:EE for satellite BAM must be set to non-zero.\n");
  319. return SPS_ERROR;
  320. }
  321. /*
  322. * Enable MTI use (message triggered interrupt)
  323. * if local processor does not control the global BAM config
  324. * and this BAM supports MTIs.
  325. */
  326. if ((dev->state & BAM_STATE_IRQ) != 0 &&
  327. (dev->props.manage & SPS_BAM_MGR_DEVICE_REMOTE) != 0 &&
  328. MTIenabled) {
  329. if (dev->props.irq_gen_addr == 0 ||
  330. dev->props.irq_gen_addr == SPS_ADDR_INVALID) {
  331. SPS_ERR(
  332. "sps:MTI destination address not specified for BAM %pa\n",
  333. BAM_ID(dev));
  334. return SPS_ERROR;
  335. }
  336. dev->state |= BAM_STATE_MTI;
  337. }
  338. if (num_pipes) {
  339. dev->props.num_pipes = num_pipes;
  340. SPS_DBG1("sps:BAM %pa number of pipes reported by hw: %d\n",
  341. BAM_ID(dev), dev->props.num_pipes);
  342. }
  343. /* Check EE index */
  344. if (!MTIenabled && dev->props.ee >= SPS_BAM_NUM_EES) {
  345. SPS_ERR("sps:Invalid EE BAM %pa: %d\n", BAM_ID(dev),
  346. dev->props.ee);
  347. return SPS_ERROR;
  348. }
  349. /*
  350. * Process EE configuration parameters,
  351. * if specified in the properties
  352. */
  353. if (!MTIenabled && dev->props.sec_config == SPS_BAM_SEC_DO_CONFIG) {
  354. struct sps_bam_sec_config_props *p_sec =
  355. dev->props.p_sec_config_props;
  356. if (p_sec == NULL) {
  357. SPS_ERR(
  358. "sps:EE config table is not specified for BAM %pa\n",
  359. BAM_ID(dev));
  360. return SPS_ERROR;
  361. }
  362. /*
  363. * Set restricted pipes based on the pipes assigned to local EE
  364. */
  365. dev->props.restricted_pipes =
  366. ~p_sec->ees[dev->props.ee].pipe_mask;
  367. /*
  368. * If local processor manages the BAM, perform the EE
  369. * configuration
  370. */
  371. if ((dev->props.manage & SPS_BAM_MGR_DEVICE_REMOTE) == 0) {
  372. u32 ee;
  373. u32 pipe_mask;
  374. int n, i;
  375. /*
  376. * Verify that there are no overlapping pipe
  377. * assignments
  378. */
  379. for (n = 0; n < SPS_BAM_NUM_EES - 1; n++) {
  380. for (i = n + 1; i < SPS_BAM_NUM_EES; i++) {
  381. if ((p_sec->ees[n].pipe_mask &
  382. p_sec->ees[i].pipe_mask) != 0) {
  383. SPS_ERR(
  384. "sps:Overlapping pipe assignments for BAM %pa: EEs %d and %d\n",
  385. BAM_ID(dev), n, i);
  386. return SPS_ERROR;
  387. }
  388. }
  389. }
  390. for (ee = 0; ee < SPS_BAM_NUM_EES; ee++) {
  391. /*
  392. * MSbit specifies EE for the global (top-level)
  393. * BAM interrupt
  394. */
  395. pipe_mask = p_sec->ees[ee].pipe_mask;
  396. if (ee == dev->props.ee)
  397. pipe_mask |= (1UL << 31);
  398. else
  399. pipe_mask &= ~(1UL << 31);
  400. bam_security_init(dev->base, ee,
  401. p_sec->ees[ee].vmid, pipe_mask);
  402. }
  403. }
  404. }
  405. /*
  406. * If local processor manages the BAM and the BAM supports MTIs
  407. * but does not support multiple EEs, set all restricted pipes
  408. * to MTI mode.
  409. */
  410. if ((dev->props.manage & SPS_BAM_MGR_DEVICE_REMOTE) == 0
  411. && MTIenabled) {
  412. u32 pipe_index;
  413. u32 pipe_mask;
  414. for (pipe_index = 0, pipe_mask = 1;
  415. pipe_index < dev->props.num_pipes;
  416. pipe_index++, pipe_mask <<= 1) {
  417. if ((pipe_mask & dev->props.restricted_pipes) == 0)
  418. continue; /* This is a local pipe */
  419. /*
  420. * Enable MTI with destination address of zero
  421. * (and source mask zero). Pipe is in reset,
  422. * so no interrupt will be generated.
  423. */
  424. bam_pipe_satellite_mti(dev->base, pipe_index, 0,
  425. dev->props.ee);
  426. }
  427. }
  428. dev->state |= BAM_STATE_ENABLED;
  429. if (!dev->props.constrained_logging ||
  430. (dev->props.constrained_logging && dev->props.logging_number)) {
  431. if (dev->props.logging_number > 0)
  432. dev->props.logging_number--;
  433. SPS_INFO(
  434. "sps:BAM %pa (va:0x%p) enabled: ver:0x%x, number of pipes:%d\n",
  435. BAM_ID(dev), dev->base, dev->version,
  436. dev->props.num_pipes);
  437. } else
  438. SPS_DBG2(
  439. "sps:BAM %pa (va:0x%p) enabled: ver:0x%x, number of pipes:%d\n",
  440. BAM_ID(dev), dev->base, dev->version,
  441. dev->props.num_pipes);
  442. return 0;
  443. }
  444. /**
  445. * BAM device disable
  446. *
  447. */
  448. int sps_bam_disable(struct sps_bam *dev)
  449. {
  450. if ((dev->state & BAM_STATE_ENABLED) == 0)
  451. return 0;
  452. /* Is there any access to this BAM? */
  453. if ((dev->props.manage & SPS_BAM_MGR_ACCESS_MASK) == SPS_BAM_MGR_NONE) {
  454. SPS_ERR("sps:No local access to BAM %pa\n", BAM_ID(dev));
  455. return SPS_ERROR;
  456. }
  457. /* Is this BAM controlled by the local processor? */
  458. if ((dev->props.manage & SPS_BAM_MGR_DEVICE_REMOTE)) {
  459. /* No, so just mark it disabled */
  460. dev->state &= ~BAM_STATE_ENABLED;
  461. if ((dev->state & BAM_STATE_IRQ) && (dev->props.irq > 0)) {
  462. free_irq(dev->props.irq, dev);
  463. dev->state &= ~BAM_STATE_IRQ;
  464. }
  465. return 0;
  466. }
  467. /* Disable BAM (interrupts) */
  468. if ((dev->state & BAM_STATE_IRQ)) {
  469. bam_exit(dev->base, dev->props.ee);
  470. /* Deregister BAM ISR */
  471. if ((dev->state & BAM_STATE_IRQ))
  472. if (dev->props.irq > 0)
  473. free_irq(dev->props.irq, dev);
  474. dev->state &= ~BAM_STATE_IRQ;
  475. }
  476. dev->state &= ~BAM_STATE_ENABLED;
  477. SPS_DBG2("sps:BAM %pa disabled\n", BAM_ID(dev));
  478. return 0;
  479. }
  480. /**
  481. * BAM device initialization
  482. */
  483. int sps_bam_device_init(struct sps_bam *dev)
  484. {
  485. if (dev->props.virt_addr == NULL) {
  486. SPS_ERR("sps:NULL BAM virtual address\n");
  487. return SPS_ERROR;
  488. }
  489. dev->base = (void *) dev->props.virt_addr;
  490. if (dev->props.num_pipes == 0) {
  491. /* Assume max number of pipes until BAM registers can be read */
  492. dev->props.num_pipes = BAM_MAX_PIPES;
  493. SPS_DBG2("sps:BAM %pa: assuming max number of pipes: %d\n",
  494. BAM_ID(dev), dev->props.num_pipes);
  495. }
  496. /* Init BAM state data */
  497. dev->state = 0;
  498. dev->pipe_active_mask = 0;
  499. dev->pipe_remote_mask = 0;
  500. INIT_LIST_HEAD(&dev->pipes_q);
  501. spin_lock_init(&dev->isr_lock);
  502. spin_lock_init(&dev->connection_lock);
  503. if ((dev->props.options & SPS_BAM_OPT_ENABLE_AT_BOOT))
  504. if (sps_bam_enable(dev)) {
  505. SPS_ERR("sps:Fail to enable bam device\n");
  506. return SPS_ERROR;
  507. }
  508. SPS_DBG2("sps:BAM device: phys %pa IRQ %d\n",
  509. BAM_ID(dev), dev->props.irq);
  510. return 0;
  511. }
  512. /**
  513. * BAM device de-initialization
  514. *
  515. */
  516. int sps_bam_device_de_init(struct sps_bam *dev)
  517. {
  518. int result;
  519. SPS_DBG2("sps:BAM device DEINIT: phys %pa IRQ %d\n",
  520. BAM_ID(dev), dev->props.irq);
  521. result = sps_bam_disable(dev);
  522. return result;
  523. }
  524. /**
  525. * BAM device reset
  526. *
  527. */
  528. int sps_bam_reset(struct sps_bam *dev)
  529. {
  530. struct sps_pipe *pipe;
  531. u32 pipe_index;
  532. int result;
  533. SPS_DBG2("sps:BAM device RESET: phys %pa IRQ %d\n",
  534. BAM_ID(dev), dev->props.irq);
  535. /* If BAM is enabled, then disable */
  536. result = 0;
  537. if ((dev->state & BAM_STATE_ENABLED)) {
  538. /* Verify that no pipes are currently allocated */
  539. for (pipe_index = 0; pipe_index < dev->props.num_pipes;
  540. pipe_index++) {
  541. pipe = dev->pipes[pipe_index];
  542. if (BAM_PIPE_IS_ASSIGNED(pipe)) {
  543. SPS_ERR(
  544. "sps:BAM device %pa RESET failed: pipe %d in use\n",
  545. BAM_ID(dev), pipe_index);
  546. result = SPS_ERROR;
  547. break;
  548. }
  549. }
  550. if (result == 0)
  551. result = sps_bam_disable(dev);
  552. }
  553. /* BAM will be reset as part of the enable process */
  554. if (result == 0)
  555. result = sps_bam_enable(dev);
  556. return result;
  557. }
  558. /**
  559. * Clear the BAM pipe state struct
  560. *
  561. * This function clears the BAM pipe state struct.
  562. *
  563. * @pipe - pointer to client pipe struct
  564. *
  565. */
  566. static void pipe_clear(struct sps_pipe *pipe)
  567. {
  568. INIT_LIST_HEAD(&pipe->list);
  569. pipe->state = 0;
  570. pipe->pipe_index = SPS_BAM_PIPE_INVALID;
  571. pipe->pipe_index_mask = 0;
  572. pipe->irq_mask = 0;
  573. pipe->mode = -1;
  574. pipe->num_descs = 0;
  575. pipe->desc_size = 0;
  576. pipe->disconnecting = false;
  577. pipe->late_eot = false;
  578. memset(&pipe->sys, 0, sizeof(pipe->sys));
  579. INIT_LIST_HEAD(&pipe->sys.events_q);
  580. }
  581. /**
  582. * Allocate a BAM pipe
  583. *
  584. */
  585. u32 sps_bam_pipe_alloc(struct sps_bam *dev, u32 pipe_index)
  586. {
  587. u32 pipe_mask;
  588. if (pipe_index == SPS_BAM_PIPE_INVALID) {
  589. /* Allocate a pipe from the BAM */
  590. if ((dev->props.manage & SPS_BAM_MGR_PIPE_NO_ALLOC)) {
  591. SPS_ERR(
  592. "sps:Restricted from allocating pipes on BAM %pa\n",
  593. BAM_ID(dev));
  594. return SPS_BAM_PIPE_INVALID;
  595. }
  596. for (pipe_index = 0, pipe_mask = 1;
  597. pipe_index < dev->props.num_pipes;
  598. pipe_index++, pipe_mask <<= 1) {
  599. if ((pipe_mask & dev->props.restricted_pipes))
  600. continue; /* This is a restricted pipe */
  601. if (dev->pipes[pipe_index] == NULL)
  602. break; /* Found an available pipe */
  603. }
  604. if (pipe_index >= dev->props.num_pipes) {
  605. SPS_ERR("sps:Fail to allocate pipe on BAM %pa\n",
  606. BAM_ID(dev));
  607. return SPS_BAM_PIPE_INVALID;
  608. }
  609. } else {
  610. /* Check that client-specified pipe is available */
  611. if (pipe_index >= dev->props.num_pipes) {
  612. SPS_ERR(
  613. "sps:Invalid pipe %d for allocate on BAM %pa\n",
  614. pipe_index, BAM_ID(dev));
  615. return SPS_BAM_PIPE_INVALID;
  616. }
  617. if ((dev->props.restricted_pipes & (1UL << pipe_index))) {
  618. SPS_ERR("sps:BAM %pa pipe %d is not local\n",
  619. BAM_ID(dev), pipe_index);
  620. return SPS_BAM_PIPE_INVALID;
  621. }
  622. if (dev->pipes[pipe_index] != NULL) {
  623. SPS_ERR("sps:Pipe %d already allocated on BAM %pa\n",
  624. pipe_index, BAM_ID(dev));
  625. return SPS_BAM_PIPE_INVALID;
  626. }
  627. }
  628. /* Mark pipe as allocated */
  629. dev->pipes[pipe_index] = BAM_PIPE_UNASSIGNED;
  630. return pipe_index;
  631. }
  632. /**
  633. * Free a BAM pipe
  634. *
  635. */
  636. void sps_bam_pipe_free(struct sps_bam *dev, u32 pipe_index)
  637. {
  638. struct sps_pipe *pipe;
  639. if (pipe_index >= dev->props.num_pipes) {
  640. SPS_ERR("sps:Invalid BAM %pa pipe: %d\n", BAM_ID(dev),
  641. pipe_index);
  642. return;
  643. }
  644. /* Get the client pipe struct and mark the pipe free */
  645. pipe = dev->pipes[pipe_index];
  646. dev->pipes[pipe_index] = NULL;
  647. /* Is the pipe currently allocated? */
  648. if (pipe == NULL) {
  649. SPS_ERR("sps:Attempt to free unallocated pipe %d on BAM %pa\n",
  650. pipe_index, BAM_ID(dev));
  651. return;
  652. }
  653. if (pipe == BAM_PIPE_UNASSIGNED)
  654. return; /* Never assigned, so no work to do */
  655. /* Return pending items to appropriate pools */
  656. if (!list_empty(&pipe->sys.events_q)) {
  657. struct sps_q_event *sps_event;
  658. SPS_ERR("sps:Disconnect BAM %pa pipe %d with events pending\n",
  659. BAM_ID(dev), pipe_index);
  660. sps_event = list_entry((&pipe->sys.events_q)->next,
  661. typeof(*sps_event), list);
  662. while (&sps_event->list != (&pipe->sys.events_q)) {
  663. struct sps_q_event *sps_event_delete = sps_event;
  664. list_del(&sps_event->list);
  665. sps_event = list_entry(sps_event->list.next,
  666. typeof(*sps_event), list);
  667. kfree(sps_event_delete);
  668. }
  669. }
  670. /* Clear the BAM pipe state struct */
  671. pipe_clear(pipe);
  672. }
  673. /**
  674. * Establish BAM pipe connection
  675. *
  676. */
  677. int sps_bam_pipe_connect(struct sps_pipe *bam_pipe,
  678. const struct sps_bam_connect_param *params)
  679. {
  680. struct bam_pipe_parameters hw_params;
  681. struct sps_bam *dev;
  682. const struct sps_connection *map = bam_pipe->map;
  683. const struct sps_conn_end_pt *map_pipe;
  684. const struct sps_conn_end_pt *other_pipe;
  685. void *desc_buf = NULL;
  686. u32 pipe_index;
  687. int result;
  688. /* Clear the client pipe state and hw init struct */
  689. pipe_clear(bam_pipe);
  690. memset(&hw_params, 0, sizeof(hw_params));
  691. /* Initialize the BAM state struct */
  692. bam_pipe->mode = params->mode;
  693. /* Set pipe streaming mode */
  694. if ((params->options & SPS_O_STREAMING) == 0)
  695. hw_params.stream_mode = BAM_STREAM_MODE_DISABLE;
  696. else
  697. hw_params.stream_mode = BAM_STREAM_MODE_ENABLE;
  698. /* Determine which end point to connect */
  699. if (bam_pipe->mode == SPS_MODE_SRC) {
  700. map_pipe = &map->src;
  701. other_pipe = &map->dest;
  702. hw_params.dir = BAM_PIPE_PRODUCER;
  703. } else {
  704. map_pipe = &map->dest;
  705. other_pipe = &map->src;
  706. hw_params.dir = BAM_PIPE_CONSUMER;
  707. }
  708. /* Process map parameters */
  709. dev = map_pipe->bam;
  710. pipe_index = map_pipe->pipe_index;
  711. if (pipe_index >= dev->props.num_pipes) {
  712. SPS_ERR("sps:Invalid BAM %pa pipe: %d\n", BAM_ID(dev),
  713. pipe_index);
  714. return SPS_ERROR;
  715. }
  716. hw_params.event_threshold = (u16) map_pipe->event_threshold;
  717. hw_params.ee = dev->props.ee;
  718. hw_params.lock_group = map_pipe->lock_group;
  719. /* Verify that control of this pipe is allowed */
  720. if ((dev->props.manage & SPS_BAM_MGR_PIPE_NO_CTRL) ||
  721. (dev->props.restricted_pipes & (1UL << pipe_index))) {
  722. SPS_ERR("sps:BAM %pa pipe %d is not local\n",
  723. BAM_ID(dev), pipe_index);
  724. return SPS_ERROR;
  725. }
  726. /* Control without configuration permission is not supported yet */
  727. if ((dev->props.manage & SPS_BAM_MGR_PIPE_NO_CONFIG)) {
  728. SPS_ERR("sps:BAM %pa pipe %d remote config is not supported\n",
  729. BAM_ID(dev), pipe_index);
  730. return SPS_ERROR;
  731. }
  732. /* Determine operational mode */
  733. if (other_pipe->bam != NULL) {
  734. /* BAM-to-BAM mode */
  735. bam_pipe->state |= BAM_STATE_BAM2BAM;
  736. hw_params.mode = BAM_PIPE_MODE_BAM2BAM;
  737. hw_params.peer_phys_addr =
  738. ((struct sps_bam *) (other_pipe->bam))->props.phys_addr;
  739. hw_params.peer_pipe = other_pipe->pipe_index;
  740. /* Verify FIFO buffers are allocated for BAM-to-BAM pipes */
  741. if (map->desc.phys_base == SPS_ADDR_INVALID ||
  742. map->data.phys_base == SPS_ADDR_INVALID ||
  743. map->desc.size == 0 || map->data.size == 0) {
  744. SPS_ERR(
  745. "sps:FIFO buffers are not allocated for BAM %pa pipe %d.\n",
  746. BAM_ID(dev), pipe_index);
  747. return SPS_ERROR;
  748. }
  749. hw_params.data_base = map->data.phys_base;
  750. hw_params.data_size = map->data.size;
  751. /* Clear the data FIFO for debug */
  752. if (map->data.base != NULL && bam_pipe->mode == SPS_MODE_SRC)
  753. memset_io(map->data.base, 0, hw_params.data_size);
  754. /* set NWD bit for BAM2BAM producer pipe */
  755. if (bam_pipe->mode == SPS_MODE_SRC) {
  756. if ((params->options & SPS_O_WRITE_NWD) == 0)
  757. hw_params.write_nwd = BAM_WRITE_NWD_DISABLE;
  758. else
  759. hw_params.write_nwd = BAM_WRITE_NWD_ENABLE;
  760. }
  761. } else {
  762. /* System mode */
  763. hw_params.mode = BAM_PIPE_MODE_SYSTEM;
  764. bam_pipe->sys.desc_buf = map->desc.base;
  765. bam_pipe->sys.desc_offset = 0;
  766. bam_pipe->sys.acked_offset = 0;
  767. }
  768. /* Initialize the client pipe state */
  769. bam_pipe->pipe_index = pipe_index;
  770. bam_pipe->pipe_index_mask = 1UL << pipe_index;
  771. /* Get virtual address for descriptor FIFO */
  772. if (map->desc.phys_base != SPS_ADDR_INVALID) {
  773. if (map->desc.size < (2 * sizeof(struct sps_iovec))) {
  774. SPS_ERR(
  775. "sps:Invalid descriptor FIFO size for BAM %pa pipe %d: %d\n",
  776. BAM_ID(dev), pipe_index, map->desc.size);
  777. return SPS_ERROR;
  778. }
  779. desc_buf = map->desc.base;
  780. /*
  781. * Note that descriptor base and size will be left zero from
  782. * the memset() above if the physical address was invalid.
  783. * This allows a satellite driver to set the FIFO as
  784. * local memory for system mode.
  785. */
  786. hw_params.desc_base = map->desc.phys_base;
  787. hw_params.desc_size = map->desc.size;
  788. }
  789. /* Configure the descriptor FIFO for both operational modes */
  790. if (desc_buf != NULL)
  791. if (bam_pipe->mode == SPS_MODE_SRC ||
  792. hw_params.mode == BAM_PIPE_MODE_SYSTEM)
  793. memset_io(desc_buf, 0, hw_params.desc_size);
  794. bam_pipe->desc_size = hw_params.desc_size;
  795. bam_pipe->num_descs = bam_pipe->desc_size / sizeof(struct sps_iovec);
  796. result = SPS_ERROR;
  797. /* Insure that the BAM is enabled */
  798. if ((dev->state & BAM_STATE_ENABLED) == 0)
  799. if (sps_bam_enable(dev))
  800. goto exit_init_err;
  801. /* Check pipe allocation */
  802. if (dev->pipes[pipe_index] != BAM_PIPE_UNASSIGNED) {
  803. SPS_ERR("sps:Invalid pipe %d on BAM %pa for connect\n",
  804. pipe_index, BAM_ID(dev));
  805. return SPS_ERROR;
  806. }
  807. if (bam_pipe_is_enabled(dev->base, pipe_index)) {
  808. if (params->options & SPS_O_NO_DISABLE)
  809. SPS_DBG("sps:BAM %pa pipe %d is already enabled\n",
  810. BAM_ID(dev), pipe_index);
  811. else {
  812. SPS_ERR("sps:BAM %pa pipe %d sharing violation\n",
  813. BAM_ID(dev), pipe_index);
  814. return SPS_ERROR;
  815. }
  816. }
  817. if (bam_pipe_init(dev->base, pipe_index, &hw_params, dev->props.ee)) {
  818. SPS_ERR("sps:BAM %pa pipe %d init error\n",
  819. BAM_ID(dev), pipe_index);
  820. goto exit_err;
  821. }
  822. /* Assign pipe to client */
  823. dev->pipes[pipe_index] = bam_pipe;
  824. /* Process configuration parameters */
  825. if (params->options != 0 ||
  826. (bam_pipe->state & BAM_STATE_BAM2BAM) == 0) {
  827. /* Process init-time only parameters */
  828. u32 irq_gen_addr;
  829. /* Set interrupt mode */
  830. irq_gen_addr = SPS_ADDR_INVALID;
  831. if ((params->options & SPS_O_IRQ_MTI))
  832. /* Client has directly specified the MTI address */
  833. irq_gen_addr = params->irq_gen_addr;
  834. else if ((dev->state & BAM_STATE_MTI))
  835. /* This BAM has MTI use enabled */
  836. irq_gen_addr = dev->props.irq_gen_addr;
  837. if (irq_gen_addr != SPS_ADDR_INVALID) {
  838. /*
  839. * No checks - assume BAM is already setup for
  840. * MTI generation,
  841. * or the pipe will be set to satellite control.
  842. */
  843. bam_pipe->state |= BAM_STATE_MTI;
  844. bam_pipe->irq_gen_addr = irq_gen_addr;
  845. }
  846. /* Process runtime parameters */
  847. if (sps_bam_pipe_set_params(dev, pipe_index,
  848. params->options)) {
  849. dev->pipes[pipe_index] = BAM_PIPE_UNASSIGNED;
  850. goto exit_err;
  851. }
  852. }
  853. /* Indicate initialization is complete */
  854. dev->pipes[pipe_index] = bam_pipe;
  855. dev->pipe_active_mask |= 1UL << pipe_index;
  856. list_add_tail(&bam_pipe->list, &dev->pipes_q);
  857. bam_pipe->state |= BAM_STATE_INIT;
  858. result = 0;
  859. exit_err:
  860. if (result) {
  861. if (params->options & SPS_O_NO_DISABLE)
  862. SPS_DBG("sps:BAM %pa pipe %d connection exits\n",
  863. BAM_ID(dev), pipe_index);
  864. else
  865. bam_pipe_exit(dev->base, pipe_index, dev->props.ee);
  866. }
  867. exit_init_err:
  868. if (result) {
  869. /* Clear the client pipe state */
  870. pipe_clear(bam_pipe);
  871. }
  872. return result;
  873. }
  874. /**
  875. * Disconnect a BAM pipe connection
  876. *
  877. */
  878. int sps_bam_pipe_disconnect(struct sps_bam *dev, u32 pipe_index)
  879. {
  880. struct sps_pipe *pipe;
  881. int result;
  882. if (pipe_index >= dev->props.num_pipes) {
  883. SPS_ERR("sps:Invalid BAM %pa pipe: %d\n", BAM_ID(dev),
  884. pipe_index);
  885. return SPS_ERROR;
  886. }
  887. /* Deallocate and reset the BAM pipe */
  888. pipe = dev->pipes[pipe_index];
  889. if (BAM_PIPE_IS_ASSIGNED(pipe)) {
  890. if ((dev->pipe_active_mask & (1UL << pipe_index))) {
  891. list_del(&pipe->list);
  892. dev->pipe_active_mask &= ~(1UL << pipe_index);
  893. }
  894. dev->pipe_remote_mask &= ~(1UL << pipe_index);
  895. if (pipe->connect.options & SPS_O_NO_DISABLE)
  896. SPS_DBG("sps:BAM %pa pipe %d exits\n", BAM_ID(dev),
  897. pipe_index);
  898. else
  899. bam_pipe_exit(dev->base, pipe_index, dev->props.ee);
  900. if (pipe->sys.desc_cache != NULL) {
  901. u32 size = pipe->num_descs * sizeof(void *);
  902. if (pipe->desc_size + size <= PAGE_SIZE) {
  903. if (dev->props.options & SPS_BAM_HOLD_MEM)
  904. memset(pipe->sys.desc_cache, 0,
  905. pipe->desc_size + size);
  906. else
  907. kfree(pipe->sys.desc_cache);
  908. } else {
  909. vfree(pipe->sys.desc_cache);
  910. }
  911. pipe->sys.desc_cache = NULL;
  912. }
  913. dev->pipes[pipe_index] = BAM_PIPE_UNASSIGNED;
  914. pipe_clear(pipe);
  915. result = 0;
  916. } else {
  917. result = SPS_ERROR;
  918. }
  919. if (result)
  920. SPS_ERR("sps:BAM %pa pipe %d already disconnected\n",
  921. BAM_ID(dev), pipe_index);
  922. return result;
  923. }
  924. /**
  925. * Set BAM pipe interrupt enable state
  926. *
  927. * This function sets the interrupt enable state for a BAM pipe.
  928. *
  929. * @dev - pointer to BAM device descriptor
  930. *
  931. * @pipe_index - pipe index
  932. *
  933. * @poll - true if SPS_O_POLL is set, false otherwise
  934. *
  935. */
  936. static void pipe_set_irq(struct sps_bam *dev, u32 pipe_index,
  937. u32 poll)
  938. {
  939. struct sps_pipe *pipe = dev->pipes[pipe_index];
  940. enum bam_enable irq_enable;
  941. if (poll == 0 && pipe->irq_mask != 0 &&
  942. (dev->state & BAM_STATE_IRQ)) {
  943. if ((pipe->state & BAM_STATE_BAM2BAM) != 0 &&
  944. (pipe->state & BAM_STATE_IRQ) == 0) {
  945. /*
  946. * If enabling the interrupt for a BAM-to-BAM pipe,
  947. * clear the existing interrupt status
  948. */
  949. (void)bam_pipe_get_and_clear_irq_status(dev->base,
  950. pipe_index);
  951. }
  952. pipe->state |= BAM_STATE_IRQ;
  953. irq_enable = BAM_ENABLE;
  954. pipe->polled = false;
  955. } else {
  956. pipe->state &= ~BAM_STATE_IRQ;
  957. irq_enable = BAM_DISABLE;
  958. pipe->polled = true;
  959. if (poll == 0 && pipe->irq_mask)
  960. SPS_DBG2("sps:BAM %pa pipe %d forced to use polling\n",
  961. BAM_ID(dev), pipe_index);
  962. }
  963. if ((pipe->state & BAM_STATE_MTI) == 0)
  964. bam_pipe_set_irq(dev->base, pipe_index, irq_enable,
  965. pipe->irq_mask, dev->props.ee);
  966. else
  967. bam_pipe_set_mti(dev->base, pipe_index, irq_enable,
  968. pipe->irq_mask, pipe->irq_gen_addr);
  969. }
  970. /**
  971. * Set BAM pipe parameters
  972. *
  973. */
  974. int sps_bam_pipe_set_params(struct sps_bam *dev, u32 pipe_index, u32 options)
  975. {
  976. struct sps_pipe *pipe = dev->pipes[pipe_index];
  977. u32 mask;
  978. int wake_up_is_one_shot;
  979. int no_queue;
  980. int ack_xfers;
  981. u32 size;
  982. int n;
  983. /* Capture some options */
  984. wake_up_is_one_shot = ((options & SPS_O_WAKEUP_IS_ONESHOT));
  985. no_queue = ((options & SPS_O_NO_Q));
  986. ack_xfers = ((options & SPS_O_ACK_TRANSFERS));
  987. pipe->hybrid = options & SPS_O_HYBRID;
  988. pipe->late_eot = options & SPS_O_LATE_EOT;
  989. /* Create interrupt source mask */
  990. mask = 0;
  991. for (n = 0; n < ARRAY_SIZE(opt_event_table); n++) {
  992. /* Is client registering for this event? */
  993. if ((options & opt_event_table[n].option) == 0)
  994. continue; /* No */
  995. mask |= opt_event_table[n].pipe_irq;
  996. }
  997. #ifdef SPS_BAM_STATISTICS
  998. /* Is an illegal mode change specified? */
  999. if (pipe->sys.desc_wr_count > 0 &&
  1000. (no_queue != pipe->sys.no_queue
  1001. || ack_xfers != pipe->sys.ack_xfers)) {
  1002. SPS_ERR(
  1003. "sps:Queue/ack mode change after transfer: BAM %pa pipe %d opt 0x%x\n",
  1004. BAM_ID(dev), pipe_index, options);
  1005. return SPS_ERROR;
  1006. }
  1007. #endif /* SPS_BAM_STATISTICS */
  1008. /* Is client setting invalid options for a BAM-to-BAM connection? */
  1009. if ((pipe->state & BAM_STATE_BAM2BAM) &&
  1010. (options & BAM2BAM_O_INVALID)) {
  1011. SPS_ERR(
  1012. "sps:Invalid option for BAM-to-BAM: BAM %pa pipe %d opt 0x%x\n",
  1013. BAM_ID(dev), pipe_index, options);
  1014. return SPS_ERROR;
  1015. }
  1016. /* Allocate descriptor FIFO cache if NO_Q option is disabled */
  1017. if (!no_queue && pipe->sys.desc_cache == NULL && pipe->num_descs > 0
  1018. && (pipe->state & BAM_STATE_BAM2BAM) == 0) {
  1019. /* Allocate both descriptor cache and user pointer array */
  1020. size = pipe->num_descs * sizeof(void *);
  1021. if (pipe->desc_size + size <= PAGE_SIZE) {
  1022. if ((dev->props.options &
  1023. SPS_BAM_HOLD_MEM)) {
  1024. if (dev->desc_cache_pointers[pipe_index]) {
  1025. pipe->sys.desc_cache =
  1026. dev->desc_cache_pointers
  1027. [pipe_index];
  1028. } else {
  1029. pipe->sys.desc_cache =
  1030. kzalloc(pipe->desc_size + size,
  1031. GFP_KERNEL);
  1032. dev->desc_cache_pointers[pipe_index] =
  1033. pipe->sys.desc_cache;
  1034. }
  1035. } else {
  1036. pipe->sys.desc_cache =
  1037. kzalloc(pipe->desc_size + size,
  1038. GFP_KERNEL);
  1039. }
  1040. if (pipe->sys.desc_cache == NULL) {
  1041. SPS_ERR("sps:No memory for pipe%d of BAM 0x%pa\n",
  1042. pipe_index, BAM_ID(dev));
  1043. return -ENOMEM;
  1044. }
  1045. } else {
  1046. pipe->sys.desc_cache =
  1047. vmalloc(pipe->desc_size + size);
  1048. if (pipe->sys.desc_cache == NULL) {
  1049. SPS_ERR("sps:No memory for pipe %d of BAM %pa\n",
  1050. pipe_index, BAM_ID(dev));
  1051. return -ENOMEM;
  1052. }
  1053. memset(pipe->sys.desc_cache, 0, pipe->desc_size + size);
  1054. }
  1055. pipe->sys.user_ptrs = (void **)(pipe->sys.desc_cache +
  1056. pipe->desc_size);
  1057. pipe->sys.cache_offset = pipe->sys.acked_offset;
  1058. }
  1059. /*
  1060. * No failures beyond this point. Note that malloc() is last point of
  1061. * failure, so no free() handling is needed.
  1062. */
  1063. /* Enable/disable the pipe's interrupt sources */
  1064. pipe->irq_mask = mask;
  1065. pipe_set_irq(dev, pipe_index, (options & SPS_O_POLL));
  1066. /* Store software feature enables */
  1067. pipe->wake_up_is_one_shot = wake_up_is_one_shot;
  1068. pipe->sys.no_queue = no_queue;
  1069. pipe->sys.ack_xfers = ack_xfers;
  1070. return 0;
  1071. }
  1072. /**
  1073. * Enable a BAM pipe
  1074. *
  1075. */
  1076. int sps_bam_pipe_enable(struct sps_bam *dev, u32 pipe_index)
  1077. {
  1078. struct sps_pipe *pipe = dev->pipes[pipe_index];
  1079. /* Enable the BAM pipe */
  1080. bam_pipe_enable(dev->base, pipe_index);
  1081. pipe->state |= BAM_STATE_ENABLED;
  1082. return 0;
  1083. }
  1084. /**
  1085. * Disable a BAM pipe
  1086. *
  1087. */
  1088. int sps_bam_pipe_disable(struct sps_bam *dev, u32 pipe_index)
  1089. {
  1090. struct sps_pipe *pipe = dev->pipes[pipe_index];
  1091. /* Disable the BAM pipe */
  1092. if (pipe->connect.options & SPS_O_NO_DISABLE)
  1093. SPS_DBG("sps:BAM %pa pipe %d enters disable state\n",
  1094. BAM_ID(dev), pipe_index);
  1095. else
  1096. bam_pipe_disable(dev->base, pipe_index);
  1097. pipe->state &= ~BAM_STATE_ENABLED;
  1098. return 0;
  1099. }
  1100. /**
  1101. * Register an event for a BAM pipe
  1102. *
  1103. */
  1104. int sps_bam_pipe_reg_event(struct sps_bam *dev,
  1105. u32 pipe_index,
  1106. struct sps_register_event *reg)
  1107. {
  1108. struct sps_pipe *pipe = dev->pipes[pipe_index];
  1109. struct sps_bam_event_reg *event_reg;
  1110. int n;
  1111. if (pipe->sys.no_queue && reg->xfer_done != NULL &&
  1112. reg->mode != SPS_TRIGGER_CALLBACK) {
  1113. SPS_ERR(
  1114. "sps:Only callback events support for NO_Q: BAM %pa pipe %d mode %d\n",
  1115. BAM_ID(dev), pipe_index, reg->mode);
  1116. return SPS_ERROR;
  1117. }
  1118. for (n = 0; n < ARRAY_SIZE(opt_event_table); n++) {
  1119. int index;
  1120. /* Is client registering for this event? */
  1121. if ((reg->options & opt_event_table[n].option) == 0)
  1122. continue; /* No */
  1123. index = SPS_EVENT_INDEX(opt_event_table[n].event_id);
  1124. if (index < 0)
  1125. SPS_ERR(
  1126. "sps:Negative event index: BAM %pa pipe %d mode %d\n",
  1127. BAM_ID(dev), pipe_index, reg->mode);
  1128. else {
  1129. event_reg = &pipe->sys.event_regs[index];
  1130. event_reg->xfer_done = reg->xfer_done;
  1131. event_reg->callback = reg->callback;
  1132. event_reg->mode = reg->mode;
  1133. event_reg->user = reg->user;
  1134. }
  1135. }
  1136. return 0;
  1137. }
  1138. /**
  1139. * Submit a transfer of a single buffer to a BAM pipe
  1140. *
  1141. */
  1142. int sps_bam_pipe_transfer_one(struct sps_bam *dev,
  1143. u32 pipe_index, u32 addr, u32 size,
  1144. void *user, u32 flags)
  1145. {
  1146. struct sps_pipe *pipe = dev->pipes[pipe_index];
  1147. struct sps_iovec *desc;
  1148. struct sps_iovec iovec;
  1149. u32 next_write;
  1150. static int show_recom;
  1151. /* Is this a BAM-to-BAM or satellite connection? */
  1152. if ((pipe->state & (BAM_STATE_BAM2BAM | BAM_STATE_REMOTE))) {
  1153. SPS_ERR("sps:Transfer on BAM-to-BAM: BAM %pa pipe %d\n",
  1154. BAM_ID(dev), pipe_index);
  1155. return SPS_ERROR;
  1156. }
  1157. /*
  1158. * Client identifier (user pointer) is not supported for
  1159. * SPS_O_NO_Q option.
  1160. */
  1161. if (pipe->sys.no_queue && user != NULL) {
  1162. SPS_ERR("sps:User pointer arg non-NULL: BAM %pa pipe %d\n",
  1163. BAM_ID(dev), pipe_index);
  1164. return SPS_ERROR;
  1165. }
  1166. /* Determine if descriptor can be queued */
  1167. next_write = pipe->sys.desc_offset + sizeof(struct sps_iovec);
  1168. if (next_write >= pipe->desc_size)
  1169. next_write = 0;
  1170. if (next_write == pipe->sys.acked_offset) {
  1171. /*
  1172. * If pipe is polled and client is not ACK'ing descriptors,
  1173. * perform polling operation so that any outstanding ACKs
  1174. * can occur.
  1175. */
  1176. if (!pipe->sys.ack_xfers && pipe->polled) {
  1177. pipe_handler_eot(dev, pipe);
  1178. if (next_write == pipe->sys.acked_offset) {
  1179. if (!show_recom) {
  1180. show_recom = true;
  1181. SPS_ERR(
  1182. "sps:Client of BAM %pa pipe %d is recommended to have flow control\n",
  1183. BAM_ID(dev), pipe_index);
  1184. }
  1185. SPS_DBG2(
  1186. "sps:Descriptor FIFO is full for BAM %pa pipe %d after pipe_handler_eot\n",
  1187. BAM_ID(dev), pipe_index);
  1188. return SPS_ERROR;
  1189. }
  1190. } else {
  1191. if (!show_recom) {
  1192. show_recom = true;
  1193. SPS_ERR(
  1194. "sps:Client of BAM %pa pipe %d is recommended to have flow control.\n",
  1195. BAM_ID(dev), pipe_index);
  1196. }
  1197. SPS_DBG2(
  1198. "sps:Descriptor FIFO is full for BAM %pa pipe %d\n",
  1199. BAM_ID(dev), pipe_index);
  1200. return SPS_ERROR;
  1201. }
  1202. }
  1203. /* Create descriptor */
  1204. if (!pipe->sys.no_queue)
  1205. desc = (struct sps_iovec *) (pipe->sys.desc_cache +
  1206. pipe->sys.desc_offset);
  1207. else
  1208. desc = &iovec;
  1209. desc->addr = addr;
  1210. desc->size = size;
  1211. if ((flags & SPS_IOVEC_FLAG_DEFAULT) == 0) {
  1212. desc->flags = (flags & BAM_IOVEC_FLAG_MASK)
  1213. | DESC_UPPER_ADDR(flags);
  1214. } else {
  1215. if (pipe->mode == SPS_MODE_SRC)
  1216. desc->flags = SPS_IOVEC_FLAG_INT
  1217. | DESC_UPPER_ADDR(flags);
  1218. else
  1219. desc->flags = (SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT)
  1220. | DESC_UPPER_ADDR(flags);
  1221. }
  1222. #ifdef SPS_BAM_STATISTICS
  1223. if ((flags & SPS_IOVEC_FLAG_INT))
  1224. pipe->sys.int_flags++;
  1225. if ((flags & SPS_IOVEC_FLAG_EOT))
  1226. pipe->sys.eot_flags++;
  1227. #endif /* SPS_BAM_STATISTICS */
  1228. /* Update hardware descriptor FIFO - should result in burst */
  1229. *((struct sps_iovec *) (pipe->sys.desc_buf + pipe->sys.desc_offset))
  1230. = *desc;
  1231. /* Record user pointer value */
  1232. if (!pipe->sys.no_queue) {
  1233. u32 index = pipe->sys.desc_offset / sizeof(struct sps_iovec);
  1234. pipe->sys.user_ptrs[index] = user;
  1235. #ifdef SPS_BAM_STATISTICS
  1236. if (user != NULL)
  1237. pipe->sys.user_ptrs_count++;
  1238. #endif /* SPS_BAM_STATISTICS */
  1239. }
  1240. /* Update descriptor ACK offset */
  1241. pipe->sys.desc_offset = next_write;
  1242. #ifdef SPS_BAM_STATISTICS
  1243. /* Update statistics */
  1244. pipe->sys.desc_wr_count++;
  1245. #endif /* SPS_BAM_STATISTICS */
  1246. /* Notify pipe */
  1247. if ((flags & SPS_IOVEC_FLAG_NO_SUBMIT) == 0) {
  1248. wmb(); /* Memory Barrier */
  1249. bam_pipe_set_desc_write_offset(dev->base, pipe_index,
  1250. next_write);
  1251. }
  1252. return 0;
  1253. }
  1254. /**
  1255. * Submit a transfer to a BAM pipe
  1256. *
  1257. */
  1258. int sps_bam_pipe_transfer(struct sps_bam *dev,
  1259. u32 pipe_index, struct sps_transfer *transfer)
  1260. {
  1261. struct sps_iovec *iovec;
  1262. u32 count;
  1263. u32 flags;
  1264. void *user;
  1265. int n;
  1266. int result;
  1267. struct sps_pipe *pipe = dev->pipes[pipe_index];
  1268. if (transfer->iovec_count == 0) {
  1269. SPS_ERR("sps:iovec count zero: BAM %pa pipe %d\n",
  1270. BAM_ID(dev), pipe_index);
  1271. return SPS_ERROR;
  1272. }
  1273. if (!pipe->sys.ack_xfers && pipe->polled) {
  1274. sps_bam_pipe_get_unused_desc_num(dev, pipe_index,
  1275. &count);
  1276. count = pipe->desc_size - count - 1;
  1277. } else
  1278. sps_bam_get_free_count(dev, pipe_index, &count);
  1279. if (count < transfer->iovec_count) {
  1280. SPS_ERR("sps:Insufficient free desc: BAM %pa pipe %d: %d\n",
  1281. BAM_ID(dev), pipe_index, count);
  1282. return SPS_ERROR;
  1283. }
  1284. user = NULL; /* NULL for all except last descriptor */
  1285. for (n = (int)transfer->iovec_count - 1, iovec = transfer->iovec;
  1286. n >= 0; n--, iovec++) {
  1287. if (n > 0) {
  1288. /* This is *not* the last descriptor */
  1289. flags = iovec->flags | SPS_IOVEC_FLAG_NO_SUBMIT;
  1290. } else {
  1291. /* This *is* the last descriptor */
  1292. flags = iovec->flags;
  1293. user = transfer->user;
  1294. }
  1295. result = sps_bam_pipe_transfer_one(dev, pipe_index,
  1296. iovec->addr,
  1297. iovec->size, user,
  1298. flags);
  1299. if (result)
  1300. return SPS_ERROR;
  1301. }
  1302. return 0;
  1303. }
  1304. /**
  1305. * Allocate an event tracking struct
  1306. *
  1307. * This function allocates an event tracking struct.
  1308. *
  1309. * @pipe - pointer to pipe state
  1310. *
  1311. * @event_reg - pointer to event registration
  1312. *
  1313. * @return - pointer to event notification struct, or NULL
  1314. *
  1315. */
  1316. static struct sps_q_event *alloc_event(struct sps_pipe *pipe,
  1317. struct sps_bam_event_reg *event_reg)
  1318. {
  1319. struct sps_q_event *event;
  1320. /* A callback event object is registered, so trigger with payload */
  1321. event = &pipe->sys.event;
  1322. memset(event, 0, sizeof(*event));
  1323. return event;
  1324. }
  1325. /**
  1326. * Trigger an event notification
  1327. *
  1328. * This function triggers an event notification.
  1329. *
  1330. * @dev - pointer to BAM device descriptor
  1331. *
  1332. * @pipe - pointer to pipe state
  1333. *
  1334. * @event_reg - pointer to event registration
  1335. *
  1336. * @sps_event - pointer to event struct
  1337. *
  1338. */
  1339. static void trigger_event(struct sps_bam *dev,
  1340. struct sps_pipe *pipe,
  1341. struct sps_bam_event_reg *event_reg,
  1342. struct sps_q_event *sps_event)
  1343. {
  1344. if (sps_event == NULL) {
  1345. SPS_DBG("sps:trigger_event.sps_event is NULL.\n");
  1346. return;
  1347. }
  1348. if (event_reg->xfer_done) {
  1349. complete(event_reg->xfer_done);
  1350. SPS_DBG("sps:trigger_event.done=%d.\n",
  1351. event_reg->xfer_done->done);
  1352. }
  1353. if (event_reg->callback) {
  1354. SPS_DBG("sps:trigger_event.using callback.\n");
  1355. event_reg->callback(&sps_event->notify);
  1356. }
  1357. }
  1358. /**
  1359. * Handle a BAM pipe's generic interrupt sources
  1360. *
  1361. * This function creates the event notification for a BAM pipe's
  1362. * generic interrupt sources. The caller of this function must lock the BAM
  1363. * device's mutex.
  1364. *
  1365. * @dev - pointer to BAM device descriptor
  1366. *
  1367. * @pipe - pointer to pipe state
  1368. *
  1369. * @event_id - event identifier enum
  1370. *
  1371. */
  1372. static void pipe_handler_generic(struct sps_bam *dev,
  1373. struct sps_pipe *pipe,
  1374. enum sps_event event_id)
  1375. {
  1376. struct sps_bam_event_reg *event_reg;
  1377. struct sps_q_event *sps_event;
  1378. int index;
  1379. index = SPS_EVENT_INDEX(event_id);
  1380. if (index < 0 || index >= SPS_EVENT_INDEX(SPS_EVENT_MAX))
  1381. return;
  1382. event_reg = &pipe->sys.event_regs[index];
  1383. sps_event = alloc_event(pipe, event_reg);
  1384. if (sps_event != NULL) {
  1385. sps_event->notify.event_id = event_id;
  1386. sps_event->notify.user = event_reg->user;
  1387. trigger_event(dev, pipe, event_reg, sps_event);
  1388. }
  1389. }
  1390. /**
  1391. * Handle a BAM pipe's WAKEUP interrupt sources
  1392. *
  1393. * This function creates the event notification for a BAM pipe's
  1394. * WAKEUP interrupt source. The caller of this function must lock the BAM
  1395. * device's mutex.
  1396. *
  1397. * @dev - pointer to BAM device descriptor
  1398. *
  1399. * @pipe - pointer to pipe state
  1400. *
  1401. */
  1402. static void pipe_handler_wakeup(struct sps_bam *dev, struct sps_pipe *pipe)
  1403. {
  1404. struct sps_bam_event_reg *event_reg;
  1405. struct sps_q_event *event;
  1406. u32 pipe_index = pipe->pipe_index;
  1407. if (pipe->wake_up_is_one_shot) {
  1408. /* Disable the pipe WAKEUP interrupt source */
  1409. pipe->irq_mask &= ~BAM_PIPE_IRQ_WAKE;
  1410. pipe_set_irq(dev, pipe_index, pipe->polled);
  1411. }
  1412. event_reg = &pipe->sys.event_regs[SPS_EVENT_INDEX(SPS_EVENT_WAKEUP)];
  1413. event = alloc_event(pipe, event_reg);
  1414. if (event != NULL) {
  1415. event->notify.event_id = SPS_EVENT_WAKEUP;
  1416. event->notify.user = event_reg->user;
  1417. trigger_event(dev, pipe, event_reg, event);
  1418. }
  1419. }
  1420. /**
  1421. * Handle a BAM pipe's EOT/INT interrupt sources
  1422. *
  1423. * This function creates the event notification for a BAM pipe's EOT interrupt
  1424. * source. The caller of this function must lock the BAM device's mutex.
  1425. *
  1426. * @dev - pointer to BAM device descriptor
  1427. *
  1428. * @pipe - pointer to pipe state
  1429. *
  1430. */
  1431. static void pipe_handler_eot(struct sps_bam *dev, struct sps_pipe *pipe)
  1432. {
  1433. struct sps_bam_event_reg *event_reg;
  1434. struct sps_q_event *event;
  1435. struct sps_iovec *desc;
  1436. struct sps_iovec *cache;
  1437. void **user;
  1438. u32 *update_offset;
  1439. u32 pipe_index = pipe->pipe_index;
  1440. u32 offset;
  1441. u32 end_offset;
  1442. enum sps_event event_id;
  1443. u32 flags;
  1444. u32 enabled;
  1445. int producer = (pipe->mode == SPS_MODE_SRC);
  1446. if (pipe->sys.handler_eot)
  1447. /*
  1448. * This can happen if the pipe is configured for polling
  1449. * (IRQ disabled) and callback event generation.
  1450. * The client may perform a get_iovec() inside the callback.
  1451. */
  1452. return;
  1453. pipe->sys.handler_eot = true;
  1454. /* Get offset of last descriptor completed by the pipe */
  1455. end_offset = bam_pipe_get_desc_read_offset(dev->base, pipe_index);
  1456. if (producer && pipe->late_eot) {
  1457. struct sps_iovec *desc_end;
  1458. if (end_offset == 0)
  1459. desc_end = (struct sps_iovec *)(pipe->sys.desc_buf
  1460. + pipe->desc_size - sizeof(struct sps_iovec));
  1461. else
  1462. desc_end = (struct sps_iovec *) (pipe->sys.desc_buf
  1463. + end_offset - sizeof(struct sps_iovec));
  1464. if (!(desc_end->flags & SPS_IOVEC_FLAG_EOT)) {
  1465. if (end_offset == 0)
  1466. end_offset = pipe->desc_size
  1467. - sizeof(struct sps_iovec);
  1468. else
  1469. end_offset -= sizeof(struct sps_iovec);
  1470. }
  1471. }
  1472. /* If no queue, then do not generate any events */
  1473. if (pipe->sys.no_queue) {
  1474. if (!pipe->sys.ack_xfers) {
  1475. /* Client is not ACK'ing transfers, so do it now */
  1476. pipe->sys.acked_offset = end_offset;
  1477. }
  1478. pipe->sys.handler_eot = false;
  1479. return;
  1480. }
  1481. /*
  1482. * Get offset of last descriptor processed by software,
  1483. * and update to the last descriptor completed by the pipe
  1484. */
  1485. if (!pipe->sys.ack_xfers) {
  1486. update_offset = &pipe->sys.acked_offset;
  1487. offset = *update_offset;
  1488. } else {
  1489. update_offset = &pipe->sys.cache_offset;
  1490. offset = *update_offset;
  1491. }
  1492. /* Are there any completed descriptors to process? */
  1493. if (offset == end_offset) {
  1494. pipe->sys.handler_eot = false;
  1495. return;
  1496. }
  1497. /* Determine enabled events */
  1498. enabled = 0;
  1499. if ((pipe->irq_mask & SPS_O_EOT))
  1500. enabled |= SPS_IOVEC_FLAG_EOT;
  1501. if ((pipe->irq_mask & SPS_O_DESC_DONE))
  1502. enabled |= SPS_IOVEC_FLAG_INT;
  1503. /*
  1504. * For producer pipe, update the cached descriptor byte count and flags.
  1505. * For consumer pipe, the BAM does not update the descriptors, so just
  1506. * use the cached copies.
  1507. */
  1508. if (producer) {
  1509. /*
  1510. * Do copies in a tight loop to increase chance of
  1511. * multi-descriptor burst accesses on the bus
  1512. */
  1513. struct sps_iovec *desc_end;
  1514. /* Set starting point for copy */
  1515. desc = (struct sps_iovec *) (pipe->sys.desc_buf + offset);
  1516. cache = (struct sps_iovec *) (pipe->sys.desc_cache + offset);
  1517. /* Fetch all completed descriptors to end of FIFO (wrap) */
  1518. if (end_offset < offset) {
  1519. desc_end = (struct sps_iovec *)
  1520. (pipe->sys.desc_buf + pipe->desc_size);
  1521. while (desc < desc_end)
  1522. *cache++ = *desc++;
  1523. desc = (void *)pipe->sys.desc_buf;
  1524. cache = (void *)pipe->sys.desc_cache;
  1525. }
  1526. /* Fetch all remaining completed descriptors (no wrap) */
  1527. desc_end = (struct sps_iovec *) (pipe->sys.desc_buf +
  1528. end_offset);
  1529. while (desc < desc_end)
  1530. *cache++ = *desc++;
  1531. }
  1532. /* Process all completed descriptors */
  1533. cache = (struct sps_iovec *) (pipe->sys.desc_cache + offset);
  1534. user = &pipe->sys.user_ptrs[offset / sizeof(struct sps_iovec)];
  1535. for (;;) {
  1536. /*
  1537. * Increment offset to next descriptor and update pipe offset
  1538. * so a client callback can fetch the I/O vector.
  1539. */
  1540. offset += sizeof(struct sps_iovec);
  1541. if (offset >= pipe->desc_size)
  1542. /* Roll to start of descriptor FIFO */
  1543. offset = 0;
  1544. *update_offset = offset;
  1545. #ifdef SPS_BAM_STATISTICS
  1546. pipe->sys.desc_rd_count++;
  1547. #endif /* SPS_BAM_STATISTICS */
  1548. /* Did client request notification for this descriptor? */
  1549. flags = cache->flags & enabled;
  1550. if (*user != NULL || flags) {
  1551. int index;
  1552. if ((flags & SPS_IOVEC_FLAG_EOT))
  1553. event_id = SPS_EVENT_EOT;
  1554. else
  1555. event_id = SPS_EVENT_DESC_DONE;
  1556. index = SPS_EVENT_INDEX(event_id);
  1557. event_reg = &pipe->sys.event_regs[index];
  1558. event = alloc_event(pipe, event_reg);
  1559. if (event != NULL) {
  1560. /*
  1561. * Store the descriptor and user pointer
  1562. * in the notification
  1563. */
  1564. event->notify.data.transfer.iovec = *cache;
  1565. event->notify.data.transfer.user = *user;
  1566. event->notify.event_id = event_id;
  1567. event->notify.user = event_reg->user;
  1568. trigger_event(dev, pipe, event_reg, event);
  1569. }
  1570. #ifdef SPS_BAM_STATISTICS
  1571. if (*user != NULL)
  1572. pipe->sys.user_found++;
  1573. #endif /* SPS_BAM_STATISTICS */
  1574. }
  1575. /* Increment to next descriptor */
  1576. if (offset == end_offset)
  1577. break; /* No more descriptors */
  1578. if (offset) {
  1579. cache++;
  1580. user++;
  1581. } else {
  1582. cache = (void *)pipe->sys.desc_cache;
  1583. user = pipe->sys.user_ptrs;
  1584. }
  1585. }
  1586. pipe->sys.handler_eot = false;
  1587. }
  1588. /**
  1589. * Handle a BAM pipe's interrupt sources
  1590. *
  1591. * This function handles a BAM pipe's interrupt sources.
  1592. * The caller of this function must lock the BAM device's mutex.
  1593. *
  1594. * @dev - pointer to BAM device descriptor
  1595. *
  1596. * @pipe_index - pipe index
  1597. *
  1598. * @return void
  1599. *
  1600. */
  1601. static void pipe_handler(struct sps_bam *dev, struct sps_pipe *pipe)
  1602. {
  1603. u32 pipe_index;
  1604. u32 status;
  1605. enum sps_event event_id;
  1606. /* Get interrupt sources and ack all */
  1607. pipe_index = pipe->pipe_index;
  1608. status = bam_pipe_get_and_clear_irq_status(dev->base, pipe_index);
  1609. SPS_DBG("sps:pipe_handler.bam %pa.pipe %d.status=0x%x.\n",
  1610. BAM_ID(dev), pipe_index, status);
  1611. /* Check for enabled interrupt sources */
  1612. status &= pipe->irq_mask;
  1613. if (status == 0)
  1614. /* No enabled interrupt sources are active */
  1615. return;
  1616. /*
  1617. * Process the interrupt sources in order of frequency of occurrance.
  1618. * Check for early exit opportunities.
  1619. */
  1620. if ((status & (SPS_O_EOT | SPS_O_DESC_DONE)) &&
  1621. (pipe->state & BAM_STATE_BAM2BAM) == 0) {
  1622. pipe_handler_eot(dev, pipe);
  1623. if (pipe->sys.no_queue) {
  1624. /*
  1625. * EOT handler will not generate any event if there
  1626. * is no queue,
  1627. * so generate "empty" (no descriptor) event
  1628. */
  1629. if ((status & SPS_O_EOT))
  1630. event_id = SPS_EVENT_EOT;
  1631. else
  1632. event_id = SPS_EVENT_DESC_DONE;
  1633. pipe_handler_generic(dev, pipe, event_id);
  1634. }
  1635. status &= ~(SPS_O_EOT | SPS_O_DESC_DONE);
  1636. if (status == 0)
  1637. return;
  1638. }
  1639. if ((status & SPS_O_WAKEUP)) {
  1640. pipe_handler_wakeup(dev, pipe);
  1641. status &= ~SPS_O_WAKEUP;
  1642. if (status == 0)
  1643. return;
  1644. }
  1645. if ((status & SPS_O_INACTIVE)) {
  1646. pipe_handler_generic(dev, pipe, SPS_EVENT_INACTIVE);
  1647. status &= ~SPS_O_INACTIVE;
  1648. if (status == 0)
  1649. return;
  1650. }
  1651. if ((status & SPS_O_OUT_OF_DESC)) {
  1652. pipe_handler_generic(dev, pipe,
  1653. SPS_EVENT_OUT_OF_DESC);
  1654. status &= ~SPS_O_OUT_OF_DESC;
  1655. if (status == 0)
  1656. return;
  1657. }
  1658. if ((status & SPS_O_RST_ERROR) && enhd_pipe) {
  1659. SPS_ERR("sps:bam %pa ;pipe 0x%x irq status=0x%x.\n"
  1660. "sps: BAM_PIPE_IRQ_RST_ERROR\n",
  1661. BAM_ID(dev), pipe_index, status);
  1662. bam_output_register_content(dev->base, dev->props.ee);
  1663. pipe_handler_generic(dev, pipe,
  1664. SPS_EVENT_RST_ERROR);
  1665. status &= ~SPS_O_RST_ERROR;
  1666. if (status == 0)
  1667. return;
  1668. }
  1669. if ((status & SPS_O_HRESP_ERROR) && enhd_pipe) {
  1670. SPS_ERR("sps:bam %pa ;pipe 0x%x irq status=0x%x.\n"
  1671. "sps: BAM_PIPE_IRQ_HRESP_ERROR\n",
  1672. BAM_ID(dev), pipe_index, status);
  1673. bam_output_register_content(dev->base, dev->props.ee);
  1674. pipe_handler_generic(dev, pipe,
  1675. SPS_EVENT_HRESP_ERROR);
  1676. status &= ~SPS_O_HRESP_ERROR;
  1677. if (status == 0)
  1678. return;
  1679. }
  1680. if ((status & SPS_EVENT_ERROR))
  1681. pipe_handler_generic(dev, pipe, SPS_EVENT_ERROR);
  1682. }
  1683. /**
  1684. * Get a BAM pipe event
  1685. *
  1686. */
  1687. int sps_bam_pipe_get_event(struct sps_bam *dev,
  1688. u32 pipe_index, struct sps_event_notify *notify)
  1689. {
  1690. struct sps_pipe *pipe = dev->pipes[pipe_index];
  1691. struct sps_q_event *event_queue;
  1692. if (pipe->sys.no_queue) {
  1693. SPS_ERR(
  1694. "sps:Invalid connection for event: BAM %pa pipe %d context 0x%p\n",
  1695. BAM_ID(dev), pipe_index, pipe);
  1696. notify->event_id = SPS_EVENT_INVALID;
  1697. return SPS_ERROR;
  1698. }
  1699. /* If pipe is polled, perform polling operation */
  1700. if (pipe->polled && (pipe->state & BAM_STATE_BAM2BAM) == 0)
  1701. pipe_handler_eot(dev, pipe);
  1702. /* Pull an event off the synchronous event queue */
  1703. if (list_empty(&pipe->sys.events_q)) {
  1704. event_queue = NULL;
  1705. SPS_DBG("sps:events_q of bam %pa is empty.\n", BAM_ID(dev));
  1706. } else {
  1707. SPS_DBG("sps:events_q of bam %pa is not empty.\n",
  1708. BAM_ID(dev));
  1709. event_queue =
  1710. list_first_entry(&pipe->sys.events_q, struct sps_q_event,
  1711. list);
  1712. list_del(&event_queue->list);
  1713. }
  1714. /* Update client's event buffer */
  1715. if (event_queue == NULL) {
  1716. /* No event queued, so set client's event to "invalid" */
  1717. notify->event_id = SPS_EVENT_INVALID;
  1718. } else {
  1719. /*
  1720. * Copy event into client's buffer and return the event
  1721. * to the pool
  1722. */
  1723. *notify = event_queue->notify;
  1724. kfree(event_queue);
  1725. #ifdef SPS_BAM_STATISTICS
  1726. pipe->sys.get_events++;
  1727. #endif /* SPS_BAM_STATISTICS */
  1728. }
  1729. return 0;
  1730. }
  1731. /**
  1732. * Get processed I/O vector
  1733. */
  1734. int sps_bam_pipe_get_iovec(struct sps_bam *dev, u32 pipe_index,
  1735. struct sps_iovec *iovec)
  1736. {
  1737. struct sps_pipe *pipe = dev->pipes[pipe_index];
  1738. struct sps_iovec *desc;
  1739. u32 read_offset;
  1740. /* Is this a valid pipe configured for get_iovec use? */
  1741. if (!pipe->sys.ack_xfers ||
  1742. (pipe->state & BAM_STATE_BAM2BAM) != 0 ||
  1743. (pipe->state & BAM_STATE_REMOTE)) {
  1744. return SPS_ERROR;
  1745. }
  1746. /* If pipe is polled and queue is enabled, perform polling operation */
  1747. if ((pipe->polled || pipe->hybrid) && !pipe->sys.no_queue)
  1748. pipe_handler_eot(dev, pipe);
  1749. /* Is there a completed descriptor? */
  1750. if (pipe->sys.no_queue)
  1751. read_offset =
  1752. bam_pipe_get_desc_read_offset(dev->base, pipe_index);
  1753. else
  1754. read_offset = pipe->sys.cache_offset;
  1755. if (read_offset == pipe->sys.acked_offset) {
  1756. /* No, so clear the iovec to indicate FIFO is empty */
  1757. memset(iovec, 0, sizeof(*iovec));
  1758. return 0;
  1759. }
  1760. /* Fetch next descriptor */
  1761. desc = (struct sps_iovec *) (pipe->sys.desc_buf +
  1762. pipe->sys.acked_offset);
  1763. *iovec = *desc;
  1764. #ifdef SPS_BAM_STATISTICS
  1765. pipe->sys.get_iovecs++;
  1766. #endif /* SPS_BAM_STATISTICS */
  1767. /* Update read/ACK offset */
  1768. pipe->sys.acked_offset += sizeof(struct sps_iovec);
  1769. if (pipe->sys.acked_offset >= pipe->desc_size)
  1770. pipe->sys.acked_offset = 0;
  1771. return 0;
  1772. }
  1773. /**
  1774. * Determine whether a BAM pipe descriptor FIFO is empty
  1775. *
  1776. */
  1777. int sps_bam_pipe_is_empty(struct sps_bam *dev, u32 pipe_index,
  1778. u32 *empty)
  1779. {
  1780. struct sps_pipe *pipe = dev->pipes[pipe_index];
  1781. u32 end_offset;
  1782. u32 acked_offset;
  1783. /* Is this a satellite connection? */
  1784. if ((pipe->state & BAM_STATE_REMOTE)) {
  1785. SPS_ERR("sps:Is empty on remote: BAM %pa pipe %d\n",
  1786. BAM_ID(dev), pipe_index);
  1787. return SPS_ERROR;
  1788. }
  1789. /* Get offset of last descriptor completed by the pipe */
  1790. end_offset = bam_pipe_get_desc_read_offset(dev->base, pipe_index);
  1791. if ((pipe->state & BAM_STATE_BAM2BAM) == 0)
  1792. /* System mode */
  1793. acked_offset = pipe->sys.acked_offset;
  1794. else
  1795. /* BAM-to-BAM */
  1796. acked_offset = bam_pipe_get_desc_write_offset(dev->base,
  1797. pipe_index);
  1798. /* Determine descriptor FIFO state */
  1799. if (end_offset == acked_offset)
  1800. *empty = true;
  1801. else
  1802. *empty = false;
  1803. return 0;
  1804. }
  1805. /**
  1806. * Get number of free slots in a BAM pipe descriptor FIFO
  1807. *
  1808. */
  1809. int sps_bam_get_free_count(struct sps_bam *dev, u32 pipe_index,
  1810. u32 *count)
  1811. {
  1812. struct sps_pipe *pipe = dev->pipes[pipe_index];
  1813. u32 next_write;
  1814. u32 free;
  1815. /* Is this a BAM-to-BAM or satellite connection? */
  1816. if ((pipe->state & (BAM_STATE_BAM2BAM | BAM_STATE_REMOTE))) {
  1817. SPS_ERR(
  1818. "sps:Free count on BAM-to-BAM or remote: BAM %pa pipe %d\n",
  1819. BAM_ID(dev), pipe_index);
  1820. *count = 0;
  1821. return SPS_ERROR;
  1822. }
  1823. /* Determine descriptor FIFO state */
  1824. next_write = pipe->sys.desc_offset + sizeof(struct sps_iovec);
  1825. if (next_write >= pipe->desc_size)
  1826. next_write = 0;
  1827. if (pipe->sys.acked_offset >= next_write)
  1828. free = pipe->sys.acked_offset - next_write;
  1829. else
  1830. free = pipe->desc_size - next_write + pipe->sys.acked_offset;
  1831. free /= sizeof(struct sps_iovec);
  1832. *count = free;
  1833. return 0;
  1834. }
  1835. /**
  1836. * Set BAM pipe to satellite ownership
  1837. *
  1838. */
  1839. int sps_bam_set_satellite(struct sps_bam *dev, u32 pipe_index)
  1840. {
  1841. struct sps_pipe *pipe = dev->pipes[pipe_index];
  1842. /*
  1843. * Switch to satellite control is only supported on processor
  1844. * that controls the BAM global config on multi-EE BAMs
  1845. */
  1846. if ((dev->props.manage & SPS_BAM_MGR_MULTI_EE) == 0 ||
  1847. (dev->props.manage & SPS_BAM_MGR_DEVICE_REMOTE)) {
  1848. SPS_ERR(
  1849. "sps:Cannot grant satellite control to BAM %pa pipe %d\n",
  1850. BAM_ID(dev), pipe_index);
  1851. return SPS_ERROR;
  1852. }
  1853. /* Is this pipe locally controlled? */
  1854. if ((dev->pipe_active_mask & (1UL << pipe_index)) == 0) {
  1855. SPS_ERR("sps:BAM %pa pipe %d not local and active\n",
  1856. BAM_ID(dev), pipe_index);
  1857. return SPS_ERROR;
  1858. }
  1859. /* Disable local interrupts for this pipe */
  1860. if (!pipe->polled)
  1861. bam_pipe_set_irq(dev->base, pipe_index, BAM_DISABLE,
  1862. pipe->irq_mask, dev->props.ee);
  1863. if (BAM_VERSION_MTI_SUPPORT(dev->version)) {
  1864. /*
  1865. * Set pipe to MTI interrupt mode.
  1866. * Must be performed after IRQ disable,
  1867. * because it is necessary to re-enable the IRQ to enable
  1868. * MTI generation.
  1869. * Set both pipe IRQ mask and MTI dest address to zero.
  1870. */
  1871. if ((pipe->state & BAM_STATE_MTI) == 0 || pipe->polled) {
  1872. bam_pipe_satellite_mti(dev->base, pipe_index, 0,
  1873. dev->props.ee);
  1874. pipe->state |= BAM_STATE_MTI;
  1875. }
  1876. }
  1877. /* Indicate satellite control */
  1878. list_del(&pipe->list);
  1879. dev->pipe_active_mask &= ~(1UL << pipe_index);
  1880. dev->pipe_remote_mask |= pipe->pipe_index_mask;
  1881. pipe->state |= BAM_STATE_REMOTE;
  1882. return 0;
  1883. }
  1884. /**
  1885. * Perform BAM pipe timer control
  1886. *
  1887. */
  1888. int sps_bam_pipe_timer_ctrl(struct sps_bam *dev,
  1889. u32 pipe_index,
  1890. struct sps_timer_ctrl *timer_ctrl,
  1891. struct sps_timer_result *timer_result)
  1892. {
  1893. enum bam_pipe_timer_mode mode;
  1894. int result = 0;
  1895. /* Is this pipe locally controlled? */
  1896. if ((dev->pipe_active_mask & (1UL << pipe_index)) == 0) {
  1897. SPS_ERR("sps:BAM %pa pipe %d not local and active\n",
  1898. BAM_ID(dev), pipe_index);
  1899. return SPS_ERROR;
  1900. }
  1901. /* Perform the timer operation */
  1902. switch (timer_ctrl->op) {
  1903. case SPS_TIMER_OP_CONFIG:
  1904. mode = (timer_ctrl->mode == SPS_TIMER_MODE_ONESHOT) ?
  1905. BAM_PIPE_TIMER_ONESHOT :
  1906. BAM_PIPE_TIMER_PERIODIC;
  1907. bam_pipe_timer_config(dev->base, pipe_index, mode,
  1908. timer_ctrl->timeout_msec * 8);
  1909. break;
  1910. case SPS_TIMER_OP_RESET:
  1911. bam_pipe_timer_reset(dev->base, pipe_index);
  1912. break;
  1913. case SPS_TIMER_OP_READ:
  1914. break;
  1915. default:
  1916. result = SPS_ERROR;
  1917. break;
  1918. }
  1919. /* Provide the current timer value */
  1920. if (timer_result != NULL)
  1921. timer_result->current_timer =
  1922. bam_pipe_timer_get_count(dev->base, pipe_index);
  1923. return result;
  1924. }
  1925. /**
  1926. * Get the number of unused descriptors in the descriptor FIFO
  1927. * of a pipe
  1928. */
  1929. int sps_bam_pipe_get_unused_desc_num(struct sps_bam *dev, u32 pipe_index,
  1930. u32 *desc_num)
  1931. {
  1932. u32 sw_offset, peer_offset, fifo_size;
  1933. u32 desc_size = sizeof(struct sps_iovec);
  1934. struct sps_pipe *pipe = dev->pipes[pipe_index];
  1935. if (pipe == NULL)
  1936. return SPS_ERROR;
  1937. fifo_size = pipe->desc_size;
  1938. sw_offset = bam_pipe_get_desc_read_offset(dev->base, pipe_index);
  1939. peer_offset = bam_pipe_get_desc_write_offset(dev->base, pipe_index);
  1940. if (sw_offset <= peer_offset)
  1941. *desc_num = (peer_offset - sw_offset) / desc_size;
  1942. else
  1943. *desc_num = (peer_offset + fifo_size - sw_offset) / desc_size;
  1944. return 0;
  1945. }