sps_bam.c 65 KB

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