qman.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868
  1. /* Copyright 2008 - 2016 Freescale Semiconductor, Inc.
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions are met:
  5. * * Redistributions of source code must retain the above copyright
  6. * notice, this list of conditions and the following disclaimer.
  7. * * Redistributions in binary form must reproduce the above copyright
  8. * notice, this list of conditions and the following disclaimer in the
  9. * documentation and/or other materials provided with the distribution.
  10. * * Neither the name of Freescale Semiconductor nor the
  11. * names of its contributors may be used to endorse or promote products
  12. * derived from this software without specific prior written permission.
  13. *
  14. * ALTERNATIVELY, this software may be distributed under the terms of the
  15. * GNU General Public License ("GPL") as published by the Free Software
  16. * Foundation, either version 2 of that License or (at your option) any
  17. * later version.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
  20. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "qman_priv.h"
  31. #define DQRR_MAXFILL 15
  32. #define EQCR_ITHRESH 4 /* if EQCR congests, interrupt threshold */
  33. #define IRQNAME "QMan portal %d"
  34. #define MAX_IRQNAME 16 /* big enough for "QMan portal %d" */
  35. #define QMAN_POLL_LIMIT 32
  36. #define QMAN_PIRQ_DQRR_ITHRESH 12
  37. #define QMAN_PIRQ_MR_ITHRESH 4
  38. #define QMAN_PIRQ_IPERIOD 100
  39. /* Portal register assists */
  40. /* Cache-inhibited register offsets */
  41. #define QM_REG_EQCR_PI_CINH 0x0000
  42. #define QM_REG_EQCR_CI_CINH 0x0004
  43. #define QM_REG_EQCR_ITR 0x0008
  44. #define QM_REG_DQRR_PI_CINH 0x0040
  45. #define QM_REG_DQRR_CI_CINH 0x0044
  46. #define QM_REG_DQRR_ITR 0x0048
  47. #define QM_REG_DQRR_DCAP 0x0050
  48. #define QM_REG_DQRR_SDQCR 0x0054
  49. #define QM_REG_DQRR_VDQCR 0x0058
  50. #define QM_REG_DQRR_PDQCR 0x005c
  51. #define QM_REG_MR_PI_CINH 0x0080
  52. #define QM_REG_MR_CI_CINH 0x0084
  53. #define QM_REG_MR_ITR 0x0088
  54. #define QM_REG_CFG 0x0100
  55. #define QM_REG_ISR 0x0e00
  56. #define QM_REG_IER 0x0e04
  57. #define QM_REG_ISDR 0x0e08
  58. #define QM_REG_IIR 0x0e0c
  59. #define QM_REG_ITPR 0x0e14
  60. /* Cache-enabled register offsets */
  61. #define QM_CL_EQCR 0x0000
  62. #define QM_CL_DQRR 0x1000
  63. #define QM_CL_MR 0x2000
  64. #define QM_CL_EQCR_PI_CENA 0x3000
  65. #define QM_CL_EQCR_CI_CENA 0x3100
  66. #define QM_CL_DQRR_PI_CENA 0x3200
  67. #define QM_CL_DQRR_CI_CENA 0x3300
  68. #define QM_CL_MR_PI_CENA 0x3400
  69. #define QM_CL_MR_CI_CENA 0x3500
  70. #define QM_CL_CR 0x3800
  71. #define QM_CL_RR0 0x3900
  72. #define QM_CL_RR1 0x3940
  73. /*
  74. * BTW, the drivers (and h/w programming model) already obtain the required
  75. * synchronisation for portal accesses and data-dependencies. Use of barrier()s
  76. * or other order-preserving primitives simply degrade performance. Hence the
  77. * use of the __raw_*() interfaces, which simply ensure that the compiler treats
  78. * the portal registers as volatile
  79. */
  80. /* Cache-enabled ring access */
  81. #define qm_cl(base, idx) ((void *)base + ((idx) << 6))
  82. /*
  83. * Portal modes.
  84. * Enum types;
  85. * pmode == production mode
  86. * cmode == consumption mode,
  87. * dmode == h/w dequeue mode.
  88. * Enum values use 3 letter codes. First letter matches the portal mode,
  89. * remaining two letters indicate;
  90. * ci == cache-inhibited portal register
  91. * ce == cache-enabled portal register
  92. * vb == in-band valid-bit (cache-enabled)
  93. * dc == DCA (Discrete Consumption Acknowledgment), DQRR-only
  94. * As for "enum qm_dqrr_dmode", it should be self-explanatory.
  95. */
  96. enum qm_eqcr_pmode { /* matches QCSP_CFG::EPM */
  97. qm_eqcr_pci = 0, /* PI index, cache-inhibited */
  98. qm_eqcr_pce = 1, /* PI index, cache-enabled */
  99. qm_eqcr_pvb = 2 /* valid-bit */
  100. };
  101. enum qm_dqrr_dmode { /* matches QCSP_CFG::DP */
  102. qm_dqrr_dpush = 0, /* SDQCR + VDQCR */
  103. qm_dqrr_dpull = 1 /* PDQCR */
  104. };
  105. enum qm_dqrr_pmode { /* s/w-only */
  106. qm_dqrr_pci, /* reads DQRR_PI_CINH */
  107. qm_dqrr_pce, /* reads DQRR_PI_CENA */
  108. qm_dqrr_pvb /* reads valid-bit */
  109. };
  110. enum qm_dqrr_cmode { /* matches QCSP_CFG::DCM */
  111. qm_dqrr_cci = 0, /* CI index, cache-inhibited */
  112. qm_dqrr_cce = 1, /* CI index, cache-enabled */
  113. qm_dqrr_cdc = 2 /* Discrete Consumption Acknowledgment */
  114. };
  115. enum qm_mr_pmode { /* s/w-only */
  116. qm_mr_pci, /* reads MR_PI_CINH */
  117. qm_mr_pce, /* reads MR_PI_CENA */
  118. qm_mr_pvb /* reads valid-bit */
  119. };
  120. enum qm_mr_cmode { /* matches QCSP_CFG::MM */
  121. qm_mr_cci = 0, /* CI index, cache-inhibited */
  122. qm_mr_cce = 1 /* CI index, cache-enabled */
  123. };
  124. /* --- Portal structures --- */
  125. #define QM_EQCR_SIZE 8
  126. #define QM_DQRR_SIZE 16
  127. #define QM_MR_SIZE 8
  128. /* "Enqueue Command" */
  129. struct qm_eqcr_entry {
  130. u8 _ncw_verb; /* writes to this are non-coherent */
  131. u8 dca;
  132. __be16 seqnum;
  133. u8 __reserved[4];
  134. __be32 fqid; /* 24-bit */
  135. __be32 tag;
  136. struct qm_fd fd;
  137. u8 __reserved3[32];
  138. } __packed;
  139. #define QM_EQCR_VERB_VBIT 0x80
  140. #define QM_EQCR_VERB_CMD_MASK 0x61 /* but only one value; */
  141. #define QM_EQCR_VERB_CMD_ENQUEUE 0x01
  142. #define QM_EQCR_SEQNUM_NESN 0x8000 /* Advance NESN */
  143. #define QM_EQCR_SEQNUM_NLIS 0x4000 /* More fragments to come */
  144. #define QM_EQCR_SEQNUM_SEQMASK 0x3fff /* sequence number goes here */
  145. struct qm_eqcr {
  146. struct qm_eqcr_entry *ring, *cursor;
  147. u8 ci, available, ithresh, vbit;
  148. #ifdef CONFIG_FSL_DPAA_CHECKING
  149. u32 busy;
  150. enum qm_eqcr_pmode pmode;
  151. #endif
  152. };
  153. struct qm_dqrr {
  154. const struct qm_dqrr_entry *ring, *cursor;
  155. u8 pi, ci, fill, ithresh, vbit;
  156. #ifdef CONFIG_FSL_DPAA_CHECKING
  157. enum qm_dqrr_dmode dmode;
  158. enum qm_dqrr_pmode pmode;
  159. enum qm_dqrr_cmode cmode;
  160. #endif
  161. };
  162. struct qm_mr {
  163. union qm_mr_entry *ring, *cursor;
  164. u8 pi, ci, fill, ithresh, vbit;
  165. #ifdef CONFIG_FSL_DPAA_CHECKING
  166. enum qm_mr_pmode pmode;
  167. enum qm_mr_cmode cmode;
  168. #endif
  169. };
  170. /* MC (Management Command) command */
  171. /* "FQ" command layout */
  172. struct qm_mcc_fq {
  173. u8 _ncw_verb;
  174. u8 __reserved1[3];
  175. __be32 fqid; /* 24-bit */
  176. u8 __reserved2[56];
  177. } __packed;
  178. /* "CGR" command layout */
  179. struct qm_mcc_cgr {
  180. u8 _ncw_verb;
  181. u8 __reserved1[30];
  182. u8 cgid;
  183. u8 __reserved2[32];
  184. };
  185. #define QM_MCC_VERB_VBIT 0x80
  186. #define QM_MCC_VERB_MASK 0x7f /* where the verb contains; */
  187. #define QM_MCC_VERB_INITFQ_PARKED 0x40
  188. #define QM_MCC_VERB_INITFQ_SCHED 0x41
  189. #define QM_MCC_VERB_QUERYFQ 0x44
  190. #define QM_MCC_VERB_QUERYFQ_NP 0x45 /* "non-programmable" fields */
  191. #define QM_MCC_VERB_QUERYWQ 0x46
  192. #define QM_MCC_VERB_QUERYWQ_DEDICATED 0x47
  193. #define QM_MCC_VERB_ALTER_SCHED 0x48 /* Schedule FQ */
  194. #define QM_MCC_VERB_ALTER_FE 0x49 /* Force Eligible FQ */
  195. #define QM_MCC_VERB_ALTER_RETIRE 0x4a /* Retire FQ */
  196. #define QM_MCC_VERB_ALTER_OOS 0x4b /* Take FQ out of service */
  197. #define QM_MCC_VERB_ALTER_FQXON 0x4d /* FQ XON */
  198. #define QM_MCC_VERB_ALTER_FQXOFF 0x4e /* FQ XOFF */
  199. #define QM_MCC_VERB_INITCGR 0x50
  200. #define QM_MCC_VERB_MODIFYCGR 0x51
  201. #define QM_MCC_VERB_CGRTESTWRITE 0x52
  202. #define QM_MCC_VERB_QUERYCGR 0x58
  203. #define QM_MCC_VERB_QUERYCONGESTION 0x59
  204. union qm_mc_command {
  205. struct {
  206. u8 _ncw_verb; /* writes to this are non-coherent */
  207. u8 __reserved[63];
  208. };
  209. struct qm_mcc_initfq initfq;
  210. struct qm_mcc_initcgr initcgr;
  211. struct qm_mcc_fq fq;
  212. struct qm_mcc_cgr cgr;
  213. };
  214. /* MC (Management Command) result */
  215. /* "Query FQ" */
  216. struct qm_mcr_queryfq {
  217. u8 verb;
  218. u8 result;
  219. u8 __reserved1[8];
  220. struct qm_fqd fqd; /* the FQD fields are here */
  221. u8 __reserved2[30];
  222. } __packed;
  223. /* "Alter FQ State Commands" */
  224. struct qm_mcr_alterfq {
  225. u8 verb;
  226. u8 result;
  227. u8 fqs; /* Frame Queue Status */
  228. u8 __reserved1[61];
  229. };
  230. #define QM_MCR_VERB_RRID 0x80
  231. #define QM_MCR_VERB_MASK QM_MCC_VERB_MASK
  232. #define QM_MCR_VERB_INITFQ_PARKED QM_MCC_VERB_INITFQ_PARKED
  233. #define QM_MCR_VERB_INITFQ_SCHED QM_MCC_VERB_INITFQ_SCHED
  234. #define QM_MCR_VERB_QUERYFQ QM_MCC_VERB_QUERYFQ
  235. #define QM_MCR_VERB_QUERYFQ_NP QM_MCC_VERB_QUERYFQ_NP
  236. #define QM_MCR_VERB_QUERYWQ QM_MCC_VERB_QUERYWQ
  237. #define QM_MCR_VERB_QUERYWQ_DEDICATED QM_MCC_VERB_QUERYWQ_DEDICATED
  238. #define QM_MCR_VERB_ALTER_SCHED QM_MCC_VERB_ALTER_SCHED
  239. #define QM_MCR_VERB_ALTER_FE QM_MCC_VERB_ALTER_FE
  240. #define QM_MCR_VERB_ALTER_RETIRE QM_MCC_VERB_ALTER_RETIRE
  241. #define QM_MCR_VERB_ALTER_OOS QM_MCC_VERB_ALTER_OOS
  242. #define QM_MCR_RESULT_NULL 0x00
  243. #define QM_MCR_RESULT_OK 0xf0
  244. #define QM_MCR_RESULT_ERR_FQID 0xf1
  245. #define QM_MCR_RESULT_ERR_FQSTATE 0xf2
  246. #define QM_MCR_RESULT_ERR_NOTEMPTY 0xf3 /* OOS fails if FQ is !empty */
  247. #define QM_MCR_RESULT_ERR_BADCHANNEL 0xf4
  248. #define QM_MCR_RESULT_PENDING 0xf8
  249. #define QM_MCR_RESULT_ERR_BADCOMMAND 0xff
  250. #define QM_MCR_FQS_ORLPRESENT 0x02 /* ORL fragments to come */
  251. #define QM_MCR_FQS_NOTEMPTY 0x01 /* FQ has enqueued frames */
  252. #define QM_MCR_TIMEOUT 10000 /* us */
  253. union qm_mc_result {
  254. struct {
  255. u8 verb;
  256. u8 result;
  257. u8 __reserved1[62];
  258. };
  259. struct qm_mcr_queryfq queryfq;
  260. struct qm_mcr_alterfq alterfq;
  261. struct qm_mcr_querycgr querycgr;
  262. struct qm_mcr_querycongestion querycongestion;
  263. struct qm_mcr_querywq querywq;
  264. struct qm_mcr_queryfq_np queryfq_np;
  265. };
  266. struct qm_mc {
  267. union qm_mc_command *cr;
  268. union qm_mc_result *rr;
  269. u8 rridx, vbit;
  270. #ifdef CONFIG_FSL_DPAA_CHECKING
  271. enum {
  272. /* Can be _mc_start()ed */
  273. qman_mc_idle,
  274. /* Can be _mc_commit()ed or _mc_abort()ed */
  275. qman_mc_user,
  276. /* Can only be _mc_retry()ed */
  277. qman_mc_hw
  278. } state;
  279. #endif
  280. };
  281. struct qm_addr {
  282. void __iomem *ce; /* cache-enabled */
  283. void __iomem *ci; /* cache-inhibited */
  284. };
  285. struct qm_portal {
  286. /*
  287. * In the non-CONFIG_FSL_DPAA_CHECKING case, the following stuff up to
  288. * and including 'mc' fits within a cacheline (yay!). The 'config' part
  289. * is setup-only, so isn't a cause for a concern. In other words, don't
  290. * rearrange this structure on a whim, there be dragons ...
  291. */
  292. struct qm_addr addr;
  293. struct qm_eqcr eqcr;
  294. struct qm_dqrr dqrr;
  295. struct qm_mr mr;
  296. struct qm_mc mc;
  297. } ____cacheline_aligned;
  298. /* Cache-inhibited register access. */
  299. static inline u32 qm_in(struct qm_portal *p, u32 offset)
  300. {
  301. return be32_to_cpu(__raw_readl(p->addr.ci + offset));
  302. }
  303. static inline void qm_out(struct qm_portal *p, u32 offset, u32 val)
  304. {
  305. __raw_writel(cpu_to_be32(val), p->addr.ci + offset);
  306. }
  307. /* Cache Enabled Portal Access */
  308. static inline void qm_cl_invalidate(struct qm_portal *p, u32 offset)
  309. {
  310. dpaa_invalidate(p->addr.ce + offset);
  311. }
  312. static inline void qm_cl_touch_ro(struct qm_portal *p, u32 offset)
  313. {
  314. dpaa_touch_ro(p->addr.ce + offset);
  315. }
  316. static inline u32 qm_ce_in(struct qm_portal *p, u32 offset)
  317. {
  318. return be32_to_cpu(__raw_readl(p->addr.ce + offset));
  319. }
  320. /* --- EQCR API --- */
  321. #define EQCR_SHIFT ilog2(sizeof(struct qm_eqcr_entry))
  322. #define EQCR_CARRY (uintptr_t)(QM_EQCR_SIZE << EQCR_SHIFT)
  323. /* Bit-wise logic to wrap a ring pointer by clearing the "carry bit" */
  324. static struct qm_eqcr_entry *eqcr_carryclear(struct qm_eqcr_entry *p)
  325. {
  326. uintptr_t addr = (uintptr_t)p;
  327. addr &= ~EQCR_CARRY;
  328. return (struct qm_eqcr_entry *)addr;
  329. }
  330. /* Bit-wise logic to convert a ring pointer to a ring index */
  331. static int eqcr_ptr2idx(struct qm_eqcr_entry *e)
  332. {
  333. return ((uintptr_t)e >> EQCR_SHIFT) & (QM_EQCR_SIZE - 1);
  334. }
  335. /* Increment the 'cursor' ring pointer, taking 'vbit' into account */
  336. static inline void eqcr_inc(struct qm_eqcr *eqcr)
  337. {
  338. /* increment to the next EQCR pointer and handle overflow and 'vbit' */
  339. struct qm_eqcr_entry *partial = eqcr->cursor + 1;
  340. eqcr->cursor = eqcr_carryclear(partial);
  341. if (partial != eqcr->cursor)
  342. eqcr->vbit ^= QM_EQCR_VERB_VBIT;
  343. }
  344. static inline int qm_eqcr_init(struct qm_portal *portal,
  345. enum qm_eqcr_pmode pmode,
  346. unsigned int eq_stash_thresh,
  347. int eq_stash_prio)
  348. {
  349. struct qm_eqcr *eqcr = &portal->eqcr;
  350. u32 cfg;
  351. u8 pi;
  352. eqcr->ring = portal->addr.ce + QM_CL_EQCR;
  353. eqcr->ci = qm_in(portal, QM_REG_EQCR_CI_CINH) & (QM_EQCR_SIZE - 1);
  354. qm_cl_invalidate(portal, QM_CL_EQCR_CI_CENA);
  355. pi = qm_in(portal, QM_REG_EQCR_PI_CINH) & (QM_EQCR_SIZE - 1);
  356. eqcr->cursor = eqcr->ring + pi;
  357. eqcr->vbit = (qm_in(portal, QM_REG_EQCR_PI_CINH) & QM_EQCR_SIZE) ?
  358. QM_EQCR_VERB_VBIT : 0;
  359. eqcr->available = QM_EQCR_SIZE - 1 -
  360. dpaa_cyc_diff(QM_EQCR_SIZE, eqcr->ci, pi);
  361. eqcr->ithresh = qm_in(portal, QM_REG_EQCR_ITR);
  362. #ifdef CONFIG_FSL_DPAA_CHECKING
  363. eqcr->busy = 0;
  364. eqcr->pmode = pmode;
  365. #endif
  366. cfg = (qm_in(portal, QM_REG_CFG) & 0x00ffffff) |
  367. (eq_stash_thresh << 28) | /* QCSP_CFG: EST */
  368. (eq_stash_prio << 26) | /* QCSP_CFG: EP */
  369. ((pmode & 0x3) << 24); /* QCSP_CFG::EPM */
  370. qm_out(portal, QM_REG_CFG, cfg);
  371. return 0;
  372. }
  373. static inline unsigned int qm_eqcr_get_ci_stashing(struct qm_portal *portal)
  374. {
  375. return (qm_in(portal, QM_REG_CFG) >> 28) & 0x7;
  376. }
  377. static inline void qm_eqcr_finish(struct qm_portal *portal)
  378. {
  379. struct qm_eqcr *eqcr = &portal->eqcr;
  380. u8 pi = qm_in(portal, QM_REG_EQCR_PI_CINH) & (QM_EQCR_SIZE - 1);
  381. u8 ci = qm_in(portal, QM_REG_EQCR_CI_CINH) & (QM_EQCR_SIZE - 1);
  382. DPAA_ASSERT(!eqcr->busy);
  383. if (pi != eqcr_ptr2idx(eqcr->cursor))
  384. pr_crit("losing uncommitted EQCR entries\n");
  385. if (ci != eqcr->ci)
  386. pr_crit("missing existing EQCR completions\n");
  387. if (eqcr->ci != eqcr_ptr2idx(eqcr->cursor))
  388. pr_crit("EQCR destroyed unquiesced\n");
  389. }
  390. static inline struct qm_eqcr_entry *qm_eqcr_start_no_stash(struct qm_portal
  391. *portal)
  392. {
  393. struct qm_eqcr *eqcr = &portal->eqcr;
  394. DPAA_ASSERT(!eqcr->busy);
  395. if (!eqcr->available)
  396. return NULL;
  397. #ifdef CONFIG_FSL_DPAA_CHECKING
  398. eqcr->busy = 1;
  399. #endif
  400. dpaa_zero(eqcr->cursor);
  401. return eqcr->cursor;
  402. }
  403. static inline struct qm_eqcr_entry *qm_eqcr_start_stash(struct qm_portal
  404. *portal)
  405. {
  406. struct qm_eqcr *eqcr = &portal->eqcr;
  407. u8 diff, old_ci;
  408. DPAA_ASSERT(!eqcr->busy);
  409. if (!eqcr->available) {
  410. old_ci = eqcr->ci;
  411. eqcr->ci = qm_ce_in(portal, QM_CL_EQCR_CI_CENA) &
  412. (QM_EQCR_SIZE - 1);
  413. diff = dpaa_cyc_diff(QM_EQCR_SIZE, old_ci, eqcr->ci);
  414. eqcr->available += diff;
  415. if (!diff)
  416. return NULL;
  417. }
  418. #ifdef CONFIG_FSL_DPAA_CHECKING
  419. eqcr->busy = 1;
  420. #endif
  421. dpaa_zero(eqcr->cursor);
  422. return eqcr->cursor;
  423. }
  424. static inline void eqcr_commit_checks(struct qm_eqcr *eqcr)
  425. {
  426. DPAA_ASSERT(eqcr->busy);
  427. DPAA_ASSERT(!(be32_to_cpu(eqcr->cursor->fqid) & ~QM_FQID_MASK));
  428. DPAA_ASSERT(eqcr->available >= 1);
  429. }
  430. static inline void qm_eqcr_pvb_commit(struct qm_portal *portal, u8 myverb)
  431. {
  432. struct qm_eqcr *eqcr = &portal->eqcr;
  433. struct qm_eqcr_entry *eqcursor;
  434. eqcr_commit_checks(eqcr);
  435. DPAA_ASSERT(eqcr->pmode == qm_eqcr_pvb);
  436. dma_wmb();
  437. eqcursor = eqcr->cursor;
  438. eqcursor->_ncw_verb = myverb | eqcr->vbit;
  439. dpaa_flush(eqcursor);
  440. eqcr_inc(eqcr);
  441. eqcr->available--;
  442. #ifdef CONFIG_FSL_DPAA_CHECKING
  443. eqcr->busy = 0;
  444. #endif
  445. }
  446. static inline void qm_eqcr_cce_prefetch(struct qm_portal *portal)
  447. {
  448. qm_cl_touch_ro(portal, QM_CL_EQCR_CI_CENA);
  449. }
  450. static inline u8 qm_eqcr_cce_update(struct qm_portal *portal)
  451. {
  452. struct qm_eqcr *eqcr = &portal->eqcr;
  453. u8 diff, old_ci = eqcr->ci;
  454. eqcr->ci = qm_ce_in(portal, QM_CL_EQCR_CI_CENA) & (QM_EQCR_SIZE - 1);
  455. qm_cl_invalidate(portal, QM_CL_EQCR_CI_CENA);
  456. diff = dpaa_cyc_diff(QM_EQCR_SIZE, old_ci, eqcr->ci);
  457. eqcr->available += diff;
  458. return diff;
  459. }
  460. static inline void qm_eqcr_set_ithresh(struct qm_portal *portal, u8 ithresh)
  461. {
  462. struct qm_eqcr *eqcr = &portal->eqcr;
  463. eqcr->ithresh = ithresh;
  464. qm_out(portal, QM_REG_EQCR_ITR, ithresh);
  465. }
  466. static inline u8 qm_eqcr_get_avail(struct qm_portal *portal)
  467. {
  468. struct qm_eqcr *eqcr = &portal->eqcr;
  469. return eqcr->available;
  470. }
  471. static inline u8 qm_eqcr_get_fill(struct qm_portal *portal)
  472. {
  473. struct qm_eqcr *eqcr = &portal->eqcr;
  474. return QM_EQCR_SIZE - 1 - eqcr->available;
  475. }
  476. /* --- DQRR API --- */
  477. #define DQRR_SHIFT ilog2(sizeof(struct qm_dqrr_entry))
  478. #define DQRR_CARRY (uintptr_t)(QM_DQRR_SIZE << DQRR_SHIFT)
  479. static const struct qm_dqrr_entry *dqrr_carryclear(
  480. const struct qm_dqrr_entry *p)
  481. {
  482. uintptr_t addr = (uintptr_t)p;
  483. addr &= ~DQRR_CARRY;
  484. return (const struct qm_dqrr_entry *)addr;
  485. }
  486. static inline int dqrr_ptr2idx(const struct qm_dqrr_entry *e)
  487. {
  488. return ((uintptr_t)e >> DQRR_SHIFT) & (QM_DQRR_SIZE - 1);
  489. }
  490. static const struct qm_dqrr_entry *dqrr_inc(const struct qm_dqrr_entry *e)
  491. {
  492. return dqrr_carryclear(e + 1);
  493. }
  494. static inline void qm_dqrr_set_maxfill(struct qm_portal *portal, u8 mf)
  495. {
  496. qm_out(portal, QM_REG_CFG, (qm_in(portal, QM_REG_CFG) & 0xff0fffff) |
  497. ((mf & (QM_DQRR_SIZE - 1)) << 20));
  498. }
  499. static inline int qm_dqrr_init(struct qm_portal *portal,
  500. const struct qm_portal_config *config,
  501. enum qm_dqrr_dmode dmode,
  502. enum qm_dqrr_pmode pmode,
  503. enum qm_dqrr_cmode cmode, u8 max_fill)
  504. {
  505. struct qm_dqrr *dqrr = &portal->dqrr;
  506. u32 cfg;
  507. /* Make sure the DQRR will be idle when we enable */
  508. qm_out(portal, QM_REG_DQRR_SDQCR, 0);
  509. qm_out(portal, QM_REG_DQRR_VDQCR, 0);
  510. qm_out(portal, QM_REG_DQRR_PDQCR, 0);
  511. dqrr->ring = portal->addr.ce + QM_CL_DQRR;
  512. dqrr->pi = qm_in(portal, QM_REG_DQRR_PI_CINH) & (QM_DQRR_SIZE - 1);
  513. dqrr->ci = qm_in(portal, QM_REG_DQRR_CI_CINH) & (QM_DQRR_SIZE - 1);
  514. dqrr->cursor = dqrr->ring + dqrr->ci;
  515. dqrr->fill = dpaa_cyc_diff(QM_DQRR_SIZE, dqrr->ci, dqrr->pi);
  516. dqrr->vbit = (qm_in(portal, QM_REG_DQRR_PI_CINH) & QM_DQRR_SIZE) ?
  517. QM_DQRR_VERB_VBIT : 0;
  518. dqrr->ithresh = qm_in(portal, QM_REG_DQRR_ITR);
  519. #ifdef CONFIG_FSL_DPAA_CHECKING
  520. dqrr->dmode = dmode;
  521. dqrr->pmode = pmode;
  522. dqrr->cmode = cmode;
  523. #endif
  524. /* Invalidate every ring entry before beginning */
  525. for (cfg = 0; cfg < QM_DQRR_SIZE; cfg++)
  526. dpaa_invalidate(qm_cl(dqrr->ring, cfg));
  527. cfg = (qm_in(portal, QM_REG_CFG) & 0xff000f00) |
  528. ((max_fill & (QM_DQRR_SIZE - 1)) << 20) | /* DQRR_MF */
  529. ((dmode & 1) << 18) | /* DP */
  530. ((cmode & 3) << 16) | /* DCM */
  531. 0xa0 | /* RE+SE */
  532. (0 ? 0x40 : 0) | /* Ignore RP */
  533. (0 ? 0x10 : 0); /* Ignore SP */
  534. qm_out(portal, QM_REG_CFG, cfg);
  535. qm_dqrr_set_maxfill(portal, max_fill);
  536. return 0;
  537. }
  538. static inline void qm_dqrr_finish(struct qm_portal *portal)
  539. {
  540. #ifdef CONFIG_FSL_DPAA_CHECKING
  541. struct qm_dqrr *dqrr = &portal->dqrr;
  542. if (dqrr->cmode != qm_dqrr_cdc &&
  543. dqrr->ci != dqrr_ptr2idx(dqrr->cursor))
  544. pr_crit("Ignoring completed DQRR entries\n");
  545. #endif
  546. }
  547. static inline const struct qm_dqrr_entry *qm_dqrr_current(
  548. struct qm_portal *portal)
  549. {
  550. struct qm_dqrr *dqrr = &portal->dqrr;
  551. if (!dqrr->fill)
  552. return NULL;
  553. return dqrr->cursor;
  554. }
  555. static inline u8 qm_dqrr_next(struct qm_portal *portal)
  556. {
  557. struct qm_dqrr *dqrr = &portal->dqrr;
  558. DPAA_ASSERT(dqrr->fill);
  559. dqrr->cursor = dqrr_inc(dqrr->cursor);
  560. return --dqrr->fill;
  561. }
  562. static inline void qm_dqrr_pvb_update(struct qm_portal *portal)
  563. {
  564. struct qm_dqrr *dqrr = &portal->dqrr;
  565. struct qm_dqrr_entry *res = qm_cl(dqrr->ring, dqrr->pi);
  566. DPAA_ASSERT(dqrr->pmode == qm_dqrr_pvb);
  567. #ifndef CONFIG_FSL_PAMU
  568. /*
  569. * If PAMU is not available we need to invalidate the cache.
  570. * When PAMU is available the cache is updated by stash
  571. */
  572. dpaa_invalidate_touch_ro(res);
  573. #endif
  574. /*
  575. * when accessing 'verb', use __raw_readb() to ensure that compiler
  576. * inlining doesn't try to optimise out "excess reads".
  577. */
  578. if ((__raw_readb(&res->verb) & QM_DQRR_VERB_VBIT) == dqrr->vbit) {
  579. dqrr->pi = (dqrr->pi + 1) & (QM_DQRR_SIZE - 1);
  580. if (!dqrr->pi)
  581. dqrr->vbit ^= QM_DQRR_VERB_VBIT;
  582. dqrr->fill++;
  583. }
  584. }
  585. static inline void qm_dqrr_cdc_consume_1ptr(struct qm_portal *portal,
  586. const struct qm_dqrr_entry *dq,
  587. int park)
  588. {
  589. __maybe_unused struct qm_dqrr *dqrr = &portal->dqrr;
  590. int idx = dqrr_ptr2idx(dq);
  591. DPAA_ASSERT(dqrr->cmode == qm_dqrr_cdc);
  592. DPAA_ASSERT((dqrr->ring + idx) == dq);
  593. DPAA_ASSERT(idx < QM_DQRR_SIZE);
  594. qm_out(portal, QM_REG_DQRR_DCAP, (0 << 8) | /* DQRR_DCAP::S */
  595. ((park ? 1 : 0) << 6) | /* DQRR_DCAP::PK */
  596. idx); /* DQRR_DCAP::DCAP_CI */
  597. }
  598. static inline void qm_dqrr_cdc_consume_n(struct qm_portal *portal, u32 bitmask)
  599. {
  600. __maybe_unused struct qm_dqrr *dqrr = &portal->dqrr;
  601. DPAA_ASSERT(dqrr->cmode == qm_dqrr_cdc);
  602. qm_out(portal, QM_REG_DQRR_DCAP, (1 << 8) | /* DQRR_DCAP::S */
  603. (bitmask << 16)); /* DQRR_DCAP::DCAP_CI */
  604. }
  605. static inline void qm_dqrr_sdqcr_set(struct qm_portal *portal, u32 sdqcr)
  606. {
  607. qm_out(portal, QM_REG_DQRR_SDQCR, sdqcr);
  608. }
  609. static inline void qm_dqrr_vdqcr_set(struct qm_portal *portal, u32 vdqcr)
  610. {
  611. qm_out(portal, QM_REG_DQRR_VDQCR, vdqcr);
  612. }
  613. static inline void qm_dqrr_set_ithresh(struct qm_portal *portal, u8 ithresh)
  614. {
  615. qm_out(portal, QM_REG_DQRR_ITR, ithresh);
  616. }
  617. /* --- MR API --- */
  618. #define MR_SHIFT ilog2(sizeof(union qm_mr_entry))
  619. #define MR_CARRY (uintptr_t)(QM_MR_SIZE << MR_SHIFT)
  620. static union qm_mr_entry *mr_carryclear(union qm_mr_entry *p)
  621. {
  622. uintptr_t addr = (uintptr_t)p;
  623. addr &= ~MR_CARRY;
  624. return (union qm_mr_entry *)addr;
  625. }
  626. static inline int mr_ptr2idx(const union qm_mr_entry *e)
  627. {
  628. return ((uintptr_t)e >> MR_SHIFT) & (QM_MR_SIZE - 1);
  629. }
  630. static inline union qm_mr_entry *mr_inc(union qm_mr_entry *e)
  631. {
  632. return mr_carryclear(e + 1);
  633. }
  634. static inline int qm_mr_init(struct qm_portal *portal, enum qm_mr_pmode pmode,
  635. enum qm_mr_cmode cmode)
  636. {
  637. struct qm_mr *mr = &portal->mr;
  638. u32 cfg;
  639. mr->ring = portal->addr.ce + QM_CL_MR;
  640. mr->pi = qm_in(portal, QM_REG_MR_PI_CINH) & (QM_MR_SIZE - 1);
  641. mr->ci = qm_in(portal, QM_REG_MR_CI_CINH) & (QM_MR_SIZE - 1);
  642. mr->cursor = mr->ring + mr->ci;
  643. mr->fill = dpaa_cyc_diff(QM_MR_SIZE, mr->ci, mr->pi);
  644. mr->vbit = (qm_in(portal, QM_REG_MR_PI_CINH) & QM_MR_SIZE)
  645. ? QM_MR_VERB_VBIT : 0;
  646. mr->ithresh = qm_in(portal, QM_REG_MR_ITR);
  647. #ifdef CONFIG_FSL_DPAA_CHECKING
  648. mr->pmode = pmode;
  649. mr->cmode = cmode;
  650. #endif
  651. cfg = (qm_in(portal, QM_REG_CFG) & 0xfffff0ff) |
  652. ((cmode & 1) << 8); /* QCSP_CFG:MM */
  653. qm_out(portal, QM_REG_CFG, cfg);
  654. return 0;
  655. }
  656. static inline void qm_mr_finish(struct qm_portal *portal)
  657. {
  658. struct qm_mr *mr = &portal->mr;
  659. if (mr->ci != mr_ptr2idx(mr->cursor))
  660. pr_crit("Ignoring completed MR entries\n");
  661. }
  662. static inline const union qm_mr_entry *qm_mr_current(struct qm_portal *portal)
  663. {
  664. struct qm_mr *mr = &portal->mr;
  665. if (!mr->fill)
  666. return NULL;
  667. return mr->cursor;
  668. }
  669. static inline int qm_mr_next(struct qm_portal *portal)
  670. {
  671. struct qm_mr *mr = &portal->mr;
  672. DPAA_ASSERT(mr->fill);
  673. mr->cursor = mr_inc(mr->cursor);
  674. return --mr->fill;
  675. }
  676. static inline void qm_mr_pvb_update(struct qm_portal *portal)
  677. {
  678. struct qm_mr *mr = &portal->mr;
  679. union qm_mr_entry *res = qm_cl(mr->ring, mr->pi);
  680. DPAA_ASSERT(mr->pmode == qm_mr_pvb);
  681. /*
  682. * when accessing 'verb', use __raw_readb() to ensure that compiler
  683. * inlining doesn't try to optimise out "excess reads".
  684. */
  685. if ((__raw_readb(&res->verb) & QM_MR_VERB_VBIT) == mr->vbit) {
  686. mr->pi = (mr->pi + 1) & (QM_MR_SIZE - 1);
  687. if (!mr->pi)
  688. mr->vbit ^= QM_MR_VERB_VBIT;
  689. mr->fill++;
  690. res = mr_inc(res);
  691. }
  692. dpaa_invalidate_touch_ro(res);
  693. }
  694. static inline void qm_mr_cci_consume(struct qm_portal *portal, u8 num)
  695. {
  696. struct qm_mr *mr = &portal->mr;
  697. DPAA_ASSERT(mr->cmode == qm_mr_cci);
  698. mr->ci = (mr->ci + num) & (QM_MR_SIZE - 1);
  699. qm_out(portal, QM_REG_MR_CI_CINH, mr->ci);
  700. }
  701. static inline void qm_mr_cci_consume_to_current(struct qm_portal *portal)
  702. {
  703. struct qm_mr *mr = &portal->mr;
  704. DPAA_ASSERT(mr->cmode == qm_mr_cci);
  705. mr->ci = mr_ptr2idx(mr->cursor);
  706. qm_out(portal, QM_REG_MR_CI_CINH, mr->ci);
  707. }
  708. static inline void qm_mr_set_ithresh(struct qm_portal *portal, u8 ithresh)
  709. {
  710. qm_out(portal, QM_REG_MR_ITR, ithresh);
  711. }
  712. /* --- Management command API --- */
  713. static inline int qm_mc_init(struct qm_portal *portal)
  714. {
  715. struct qm_mc *mc = &portal->mc;
  716. mc->cr = portal->addr.ce + QM_CL_CR;
  717. mc->rr = portal->addr.ce + QM_CL_RR0;
  718. mc->rridx = (__raw_readb(&mc->cr->_ncw_verb) & QM_MCC_VERB_VBIT)
  719. ? 0 : 1;
  720. mc->vbit = mc->rridx ? QM_MCC_VERB_VBIT : 0;
  721. #ifdef CONFIG_FSL_DPAA_CHECKING
  722. mc->state = qman_mc_idle;
  723. #endif
  724. return 0;
  725. }
  726. static inline void qm_mc_finish(struct qm_portal *portal)
  727. {
  728. #ifdef CONFIG_FSL_DPAA_CHECKING
  729. struct qm_mc *mc = &portal->mc;
  730. DPAA_ASSERT(mc->state == qman_mc_idle);
  731. if (mc->state != qman_mc_idle)
  732. pr_crit("Losing incomplete MC command\n");
  733. #endif
  734. }
  735. static inline union qm_mc_command *qm_mc_start(struct qm_portal *portal)
  736. {
  737. struct qm_mc *mc = &portal->mc;
  738. DPAA_ASSERT(mc->state == qman_mc_idle);
  739. #ifdef CONFIG_FSL_DPAA_CHECKING
  740. mc->state = qman_mc_user;
  741. #endif
  742. dpaa_zero(mc->cr);
  743. return mc->cr;
  744. }
  745. static inline void qm_mc_commit(struct qm_portal *portal, u8 myverb)
  746. {
  747. struct qm_mc *mc = &portal->mc;
  748. union qm_mc_result *rr = mc->rr + mc->rridx;
  749. DPAA_ASSERT(mc->state == qman_mc_user);
  750. dma_wmb();
  751. mc->cr->_ncw_verb = myverb | mc->vbit;
  752. dpaa_flush(mc->cr);
  753. dpaa_invalidate_touch_ro(rr);
  754. #ifdef CONFIG_FSL_DPAA_CHECKING
  755. mc->state = qman_mc_hw;
  756. #endif
  757. }
  758. static inline union qm_mc_result *qm_mc_result(struct qm_portal *portal)
  759. {
  760. struct qm_mc *mc = &portal->mc;
  761. union qm_mc_result *rr = mc->rr + mc->rridx;
  762. DPAA_ASSERT(mc->state == qman_mc_hw);
  763. /*
  764. * The inactive response register's verb byte always returns zero until
  765. * its command is submitted and completed. This includes the valid-bit,
  766. * in case you were wondering...
  767. */
  768. if (!__raw_readb(&rr->verb)) {
  769. dpaa_invalidate_touch_ro(rr);
  770. return NULL;
  771. }
  772. mc->rridx ^= 1;
  773. mc->vbit ^= QM_MCC_VERB_VBIT;
  774. #ifdef CONFIG_FSL_DPAA_CHECKING
  775. mc->state = qman_mc_idle;
  776. #endif
  777. return rr;
  778. }
  779. static inline int qm_mc_result_timeout(struct qm_portal *portal,
  780. union qm_mc_result **mcr)
  781. {
  782. int timeout = QM_MCR_TIMEOUT;
  783. do {
  784. *mcr = qm_mc_result(portal);
  785. if (*mcr)
  786. break;
  787. udelay(1);
  788. } while (--timeout);
  789. return timeout;
  790. }
  791. static inline void fq_set(struct qman_fq *fq, u32 mask)
  792. {
  793. set_bits(mask, &fq->flags);
  794. }
  795. static inline void fq_clear(struct qman_fq *fq, u32 mask)
  796. {
  797. clear_bits(mask, &fq->flags);
  798. }
  799. static inline int fq_isset(struct qman_fq *fq, u32 mask)
  800. {
  801. return fq->flags & mask;
  802. }
  803. static inline int fq_isclear(struct qman_fq *fq, u32 mask)
  804. {
  805. return !(fq->flags & mask);
  806. }
  807. struct qman_portal {
  808. struct qm_portal p;
  809. /* PORTAL_BITS_*** - dynamic, strictly internal */
  810. unsigned long bits;
  811. /* interrupt sources processed by portal_isr(), configurable */
  812. unsigned long irq_sources;
  813. u32 use_eqcr_ci_stashing;
  814. /* only 1 volatile dequeue at a time */
  815. struct qman_fq *vdqcr_owned;
  816. u32 sdqcr;
  817. /* probing time config params for cpu-affine portals */
  818. const struct qm_portal_config *config;
  819. /* 2-element array. cgrs[0] is mask, cgrs[1] is snapshot. */
  820. struct qman_cgrs *cgrs;
  821. /* linked-list of CSCN handlers. */
  822. struct list_head cgr_cbs;
  823. /* list lock */
  824. spinlock_t cgr_lock;
  825. struct work_struct congestion_work;
  826. struct work_struct mr_work;
  827. char irqname[MAX_IRQNAME];
  828. };
  829. static cpumask_t affine_mask;
  830. static DEFINE_SPINLOCK(affine_mask_lock);
  831. static u16 affine_channels[NR_CPUS];
  832. static DEFINE_PER_CPU(struct qman_portal, qman_affine_portal);
  833. struct qman_portal *affine_portals[NR_CPUS];
  834. static inline struct qman_portal *get_affine_portal(void)
  835. {
  836. return &get_cpu_var(qman_affine_portal);
  837. }
  838. static inline void put_affine_portal(void)
  839. {
  840. put_cpu_var(qman_affine_portal);
  841. }
  842. static struct workqueue_struct *qm_portal_wq;
  843. int qman_wq_alloc(void)
  844. {
  845. qm_portal_wq = alloc_workqueue("qman_portal_wq", 0, 1);
  846. if (!qm_portal_wq)
  847. return -ENOMEM;
  848. return 0;
  849. }
  850. /*
  851. * This is what everything can wait on, even if it migrates to a different cpu
  852. * to the one whose affine portal it is waiting on.
  853. */
  854. static DECLARE_WAIT_QUEUE_HEAD(affine_queue);
  855. static struct qman_fq **fq_table;
  856. static u32 num_fqids;
  857. int qman_alloc_fq_table(u32 _num_fqids)
  858. {
  859. num_fqids = _num_fqids;
  860. fq_table = vzalloc(num_fqids * 2 * sizeof(struct qman_fq *));
  861. if (!fq_table)
  862. return -ENOMEM;
  863. pr_debug("Allocated fq lookup table at %p, entry count %u\n",
  864. fq_table, num_fqids * 2);
  865. return 0;
  866. }
  867. static struct qman_fq *idx_to_fq(u32 idx)
  868. {
  869. struct qman_fq *fq;
  870. #ifdef CONFIG_FSL_DPAA_CHECKING
  871. if (WARN_ON(idx >= num_fqids * 2))
  872. return NULL;
  873. #endif
  874. fq = fq_table[idx];
  875. DPAA_ASSERT(!fq || idx == fq->idx);
  876. return fq;
  877. }
  878. /*
  879. * Only returns full-service fq objects, not enqueue-only
  880. * references (QMAN_FQ_FLAG_NO_MODIFY).
  881. */
  882. static struct qman_fq *fqid_to_fq(u32 fqid)
  883. {
  884. return idx_to_fq(fqid * 2);
  885. }
  886. static struct qman_fq *tag_to_fq(u32 tag)
  887. {
  888. #if BITS_PER_LONG == 64
  889. return idx_to_fq(tag);
  890. #else
  891. return (struct qman_fq *)tag;
  892. #endif
  893. }
  894. static u32 fq_to_tag(struct qman_fq *fq)
  895. {
  896. #if BITS_PER_LONG == 64
  897. return fq->idx;
  898. #else
  899. return (u32)fq;
  900. #endif
  901. }
  902. static u32 __poll_portal_slow(struct qman_portal *p, u32 is);
  903. static inline unsigned int __poll_portal_fast(struct qman_portal *p,
  904. unsigned int poll_limit);
  905. static void qm_congestion_task(struct work_struct *work);
  906. static void qm_mr_process_task(struct work_struct *work);
  907. static irqreturn_t portal_isr(int irq, void *ptr)
  908. {
  909. struct qman_portal *p = ptr;
  910. u32 clear = QM_DQAVAIL_MASK | p->irq_sources;
  911. u32 is = qm_in(&p->p, QM_REG_ISR) & p->irq_sources;
  912. if (unlikely(!is))
  913. return IRQ_NONE;
  914. /* DQRR-handling if it's interrupt-driven */
  915. if (is & QM_PIRQ_DQRI)
  916. __poll_portal_fast(p, QMAN_POLL_LIMIT);
  917. /* Handling of anything else that's interrupt-driven */
  918. clear |= __poll_portal_slow(p, is);
  919. qm_out(&p->p, QM_REG_ISR, clear);
  920. return IRQ_HANDLED;
  921. }
  922. static int drain_mr_fqrni(struct qm_portal *p)
  923. {
  924. const union qm_mr_entry *msg;
  925. loop:
  926. msg = qm_mr_current(p);
  927. if (!msg) {
  928. /*
  929. * if MR was full and h/w had other FQRNI entries to produce, we
  930. * need to allow it time to produce those entries once the
  931. * existing entries are consumed. A worst-case situation
  932. * (fully-loaded system) means h/w sequencers may have to do 3-4
  933. * other things before servicing the portal's MR pump, each of
  934. * which (if slow) may take ~50 qman cycles (which is ~200
  935. * processor cycles). So rounding up and then multiplying this
  936. * worst-case estimate by a factor of 10, just to be
  937. * ultra-paranoid, goes as high as 10,000 cycles. NB, we consume
  938. * one entry at a time, so h/w has an opportunity to produce new
  939. * entries well before the ring has been fully consumed, so
  940. * we're being *really* paranoid here.
  941. */
  942. u64 now, then = jiffies;
  943. do {
  944. now = jiffies;
  945. } while ((then + 10000) > now);
  946. msg = qm_mr_current(p);
  947. if (!msg)
  948. return 0;
  949. }
  950. if ((msg->verb & QM_MR_VERB_TYPE_MASK) != QM_MR_VERB_FQRNI) {
  951. /* We aren't draining anything but FQRNIs */
  952. pr_err("Found verb 0x%x in MR\n", msg->verb);
  953. return -1;
  954. }
  955. qm_mr_next(p);
  956. qm_mr_cci_consume(p, 1);
  957. goto loop;
  958. }
  959. static int qman_create_portal(struct qman_portal *portal,
  960. const struct qm_portal_config *c,
  961. const struct qman_cgrs *cgrs)
  962. {
  963. struct qm_portal *p;
  964. int ret;
  965. u32 isdr;
  966. p = &portal->p;
  967. #ifdef CONFIG_FSL_PAMU
  968. /* PAMU is required for stashing */
  969. portal->use_eqcr_ci_stashing = ((qman_ip_rev >= QMAN_REV30) ? 1 : 0);
  970. #else
  971. portal->use_eqcr_ci_stashing = 0;
  972. #endif
  973. /*
  974. * prep the low-level portal struct with the mapped addresses from the
  975. * config, everything that follows depends on it and "config" is more
  976. * for (de)reference
  977. */
  978. p->addr.ce = c->addr_virt[DPAA_PORTAL_CE];
  979. p->addr.ci = c->addr_virt[DPAA_PORTAL_CI];
  980. /*
  981. * If CI-stashing is used, the current defaults use a threshold of 3,
  982. * and stash with high-than-DQRR priority.
  983. */
  984. if (qm_eqcr_init(p, qm_eqcr_pvb,
  985. portal->use_eqcr_ci_stashing ? 3 : 0, 1)) {
  986. dev_err(c->dev, "EQCR initialisation failed\n");
  987. goto fail_eqcr;
  988. }
  989. if (qm_dqrr_init(p, c, qm_dqrr_dpush, qm_dqrr_pvb,
  990. qm_dqrr_cdc, DQRR_MAXFILL)) {
  991. dev_err(c->dev, "DQRR initialisation failed\n");
  992. goto fail_dqrr;
  993. }
  994. if (qm_mr_init(p, qm_mr_pvb, qm_mr_cci)) {
  995. dev_err(c->dev, "MR initialisation failed\n");
  996. goto fail_mr;
  997. }
  998. if (qm_mc_init(p)) {
  999. dev_err(c->dev, "MC initialisation failed\n");
  1000. goto fail_mc;
  1001. }
  1002. /* static interrupt-gating controls */
  1003. qm_dqrr_set_ithresh(p, QMAN_PIRQ_DQRR_ITHRESH);
  1004. qm_mr_set_ithresh(p, QMAN_PIRQ_MR_ITHRESH);
  1005. qm_out(p, QM_REG_ITPR, QMAN_PIRQ_IPERIOD);
  1006. portal->cgrs = kmalloc(2 * sizeof(*cgrs), GFP_KERNEL);
  1007. if (!portal->cgrs)
  1008. goto fail_cgrs;
  1009. /* initial snapshot is no-depletion */
  1010. qman_cgrs_init(&portal->cgrs[1]);
  1011. if (cgrs)
  1012. portal->cgrs[0] = *cgrs;
  1013. else
  1014. /* if the given mask is NULL, assume all CGRs can be seen */
  1015. qman_cgrs_fill(&portal->cgrs[0]);
  1016. INIT_LIST_HEAD(&portal->cgr_cbs);
  1017. spin_lock_init(&portal->cgr_lock);
  1018. INIT_WORK(&portal->congestion_work, qm_congestion_task);
  1019. INIT_WORK(&portal->mr_work, qm_mr_process_task);
  1020. portal->bits = 0;
  1021. portal->sdqcr = QM_SDQCR_SOURCE_CHANNELS | QM_SDQCR_COUNT_UPTO3 |
  1022. QM_SDQCR_DEDICATED_PRECEDENCE | QM_SDQCR_TYPE_PRIO_QOS |
  1023. QM_SDQCR_TOKEN_SET(0xab) | QM_SDQCR_CHANNELS_DEDICATED;
  1024. isdr = 0xffffffff;
  1025. qm_out(p, QM_REG_ISDR, isdr);
  1026. portal->irq_sources = 0;
  1027. qm_out(p, QM_REG_IER, 0);
  1028. qm_out(p, QM_REG_ISR, 0xffffffff);
  1029. snprintf(portal->irqname, MAX_IRQNAME, IRQNAME, c->cpu);
  1030. if (request_irq(c->irq, portal_isr, 0, portal->irqname, portal)) {
  1031. dev_err(c->dev, "request_irq() failed\n");
  1032. goto fail_irq;
  1033. }
  1034. if (c->cpu != -1 && irq_can_set_affinity(c->irq) &&
  1035. irq_set_affinity(c->irq, cpumask_of(c->cpu))) {
  1036. dev_err(c->dev, "irq_set_affinity() failed\n");
  1037. goto fail_affinity;
  1038. }
  1039. /* Need EQCR to be empty before continuing */
  1040. isdr &= ~QM_PIRQ_EQCI;
  1041. qm_out(p, QM_REG_ISDR, isdr);
  1042. ret = qm_eqcr_get_fill(p);
  1043. if (ret) {
  1044. dev_err(c->dev, "EQCR unclean\n");
  1045. goto fail_eqcr_empty;
  1046. }
  1047. isdr &= ~(QM_PIRQ_DQRI | QM_PIRQ_MRI);
  1048. qm_out(p, QM_REG_ISDR, isdr);
  1049. if (qm_dqrr_current(p)) {
  1050. dev_err(c->dev, "DQRR unclean\n");
  1051. qm_dqrr_cdc_consume_n(p, 0xffff);
  1052. }
  1053. if (qm_mr_current(p) && drain_mr_fqrni(p)) {
  1054. /* special handling, drain just in case it's a few FQRNIs */
  1055. const union qm_mr_entry *e = qm_mr_current(p);
  1056. dev_err(c->dev, "MR dirty, VB 0x%x, rc 0x%x, addr 0x%llx\n",
  1057. e->verb, e->ern.rc, qm_fd_addr_get64(&e->ern.fd));
  1058. goto fail_dqrr_mr_empty;
  1059. }
  1060. /* Success */
  1061. portal->config = c;
  1062. qm_out(p, QM_REG_ISDR, 0);
  1063. qm_out(p, QM_REG_IIR, 0);
  1064. /* Write a sane SDQCR */
  1065. qm_dqrr_sdqcr_set(p, portal->sdqcr);
  1066. return 0;
  1067. fail_dqrr_mr_empty:
  1068. fail_eqcr_empty:
  1069. fail_affinity:
  1070. free_irq(c->irq, portal);
  1071. fail_irq:
  1072. kfree(portal->cgrs);
  1073. fail_cgrs:
  1074. qm_mc_finish(p);
  1075. fail_mc:
  1076. qm_mr_finish(p);
  1077. fail_mr:
  1078. qm_dqrr_finish(p);
  1079. fail_dqrr:
  1080. qm_eqcr_finish(p);
  1081. fail_eqcr:
  1082. return -EIO;
  1083. }
  1084. struct qman_portal *qman_create_affine_portal(const struct qm_portal_config *c,
  1085. const struct qman_cgrs *cgrs)
  1086. {
  1087. struct qman_portal *portal;
  1088. int err;
  1089. portal = &per_cpu(qman_affine_portal, c->cpu);
  1090. err = qman_create_portal(portal, c, cgrs);
  1091. if (err)
  1092. return NULL;
  1093. spin_lock(&affine_mask_lock);
  1094. cpumask_set_cpu(c->cpu, &affine_mask);
  1095. affine_channels[c->cpu] = c->channel;
  1096. affine_portals[c->cpu] = portal;
  1097. spin_unlock(&affine_mask_lock);
  1098. return portal;
  1099. }
  1100. static void qman_destroy_portal(struct qman_portal *qm)
  1101. {
  1102. const struct qm_portal_config *pcfg;
  1103. /* Stop dequeues on the portal */
  1104. qm_dqrr_sdqcr_set(&qm->p, 0);
  1105. /*
  1106. * NB we do this to "quiesce" EQCR. If we add enqueue-completions or
  1107. * something related to QM_PIRQ_EQCI, this may need fixing.
  1108. * Also, due to the prefetching model used for CI updates in the enqueue
  1109. * path, this update will only invalidate the CI cacheline *after*
  1110. * working on it, so we need to call this twice to ensure a full update
  1111. * irrespective of where the enqueue processing was at when the teardown
  1112. * began.
  1113. */
  1114. qm_eqcr_cce_update(&qm->p);
  1115. qm_eqcr_cce_update(&qm->p);
  1116. pcfg = qm->config;
  1117. free_irq(pcfg->irq, qm);
  1118. kfree(qm->cgrs);
  1119. qm_mc_finish(&qm->p);
  1120. qm_mr_finish(&qm->p);
  1121. qm_dqrr_finish(&qm->p);
  1122. qm_eqcr_finish(&qm->p);
  1123. qm->config = NULL;
  1124. }
  1125. const struct qm_portal_config *qman_destroy_affine_portal(void)
  1126. {
  1127. struct qman_portal *qm = get_affine_portal();
  1128. const struct qm_portal_config *pcfg;
  1129. int cpu;
  1130. pcfg = qm->config;
  1131. cpu = pcfg->cpu;
  1132. qman_destroy_portal(qm);
  1133. spin_lock(&affine_mask_lock);
  1134. cpumask_clear_cpu(cpu, &affine_mask);
  1135. spin_unlock(&affine_mask_lock);
  1136. put_affine_portal();
  1137. return pcfg;
  1138. }
  1139. /* Inline helper to reduce nesting in __poll_portal_slow() */
  1140. static inline void fq_state_change(struct qman_portal *p, struct qman_fq *fq,
  1141. const union qm_mr_entry *msg, u8 verb)
  1142. {
  1143. switch (verb) {
  1144. case QM_MR_VERB_FQRL:
  1145. DPAA_ASSERT(fq_isset(fq, QMAN_FQ_STATE_ORL));
  1146. fq_clear(fq, QMAN_FQ_STATE_ORL);
  1147. break;
  1148. case QM_MR_VERB_FQRN:
  1149. DPAA_ASSERT(fq->state == qman_fq_state_parked ||
  1150. fq->state == qman_fq_state_sched);
  1151. DPAA_ASSERT(fq_isset(fq, QMAN_FQ_STATE_CHANGING));
  1152. fq_clear(fq, QMAN_FQ_STATE_CHANGING);
  1153. if (msg->fq.fqs & QM_MR_FQS_NOTEMPTY)
  1154. fq_set(fq, QMAN_FQ_STATE_NE);
  1155. if (msg->fq.fqs & QM_MR_FQS_ORLPRESENT)
  1156. fq_set(fq, QMAN_FQ_STATE_ORL);
  1157. fq->state = qman_fq_state_retired;
  1158. break;
  1159. case QM_MR_VERB_FQPN:
  1160. DPAA_ASSERT(fq->state == qman_fq_state_sched);
  1161. DPAA_ASSERT(fq_isclear(fq, QMAN_FQ_STATE_CHANGING));
  1162. fq->state = qman_fq_state_parked;
  1163. }
  1164. }
  1165. static void qm_congestion_task(struct work_struct *work)
  1166. {
  1167. struct qman_portal *p = container_of(work, struct qman_portal,
  1168. congestion_work);
  1169. struct qman_cgrs rr, c;
  1170. union qm_mc_result *mcr;
  1171. struct qman_cgr *cgr;
  1172. spin_lock(&p->cgr_lock);
  1173. qm_mc_start(&p->p);
  1174. qm_mc_commit(&p->p, QM_MCC_VERB_QUERYCONGESTION);
  1175. if (!qm_mc_result_timeout(&p->p, &mcr)) {
  1176. spin_unlock(&p->cgr_lock);
  1177. dev_crit(p->config->dev, "QUERYCONGESTION timeout\n");
  1178. return;
  1179. }
  1180. /* mask out the ones I'm not interested in */
  1181. qman_cgrs_and(&rr, (struct qman_cgrs *)&mcr->querycongestion.state,
  1182. &p->cgrs[0]);
  1183. /* check previous snapshot for delta, enter/exit congestion */
  1184. qman_cgrs_xor(&c, &rr, &p->cgrs[1]);
  1185. /* update snapshot */
  1186. qman_cgrs_cp(&p->cgrs[1], &rr);
  1187. /* Invoke callback */
  1188. list_for_each_entry(cgr, &p->cgr_cbs, node)
  1189. if (cgr->cb && qman_cgrs_get(&c, cgr->cgrid))
  1190. cgr->cb(p, cgr, qman_cgrs_get(&rr, cgr->cgrid));
  1191. spin_unlock(&p->cgr_lock);
  1192. }
  1193. static void qm_mr_process_task(struct work_struct *work)
  1194. {
  1195. struct qman_portal *p = container_of(work, struct qman_portal,
  1196. mr_work);
  1197. const union qm_mr_entry *msg;
  1198. struct qman_fq *fq;
  1199. u8 verb, num = 0;
  1200. preempt_disable();
  1201. while (1) {
  1202. qm_mr_pvb_update(&p->p);
  1203. msg = qm_mr_current(&p->p);
  1204. if (!msg)
  1205. break;
  1206. verb = msg->verb & QM_MR_VERB_TYPE_MASK;
  1207. /* The message is a software ERN iff the 0x20 bit is clear */
  1208. if (verb & 0x20) {
  1209. switch (verb) {
  1210. case QM_MR_VERB_FQRNI:
  1211. /* nada, we drop FQRNIs on the floor */
  1212. break;
  1213. case QM_MR_VERB_FQRN:
  1214. case QM_MR_VERB_FQRL:
  1215. /* Lookup in the retirement table */
  1216. fq = fqid_to_fq(qm_fqid_get(&msg->fq));
  1217. if (WARN_ON(!fq))
  1218. break;
  1219. fq_state_change(p, fq, msg, verb);
  1220. if (fq->cb.fqs)
  1221. fq->cb.fqs(p, fq, msg);
  1222. break;
  1223. case QM_MR_VERB_FQPN:
  1224. /* Parked */
  1225. fq = tag_to_fq(be32_to_cpu(msg->fq.context_b));
  1226. fq_state_change(p, fq, msg, verb);
  1227. if (fq->cb.fqs)
  1228. fq->cb.fqs(p, fq, msg);
  1229. break;
  1230. case QM_MR_VERB_DC_ERN:
  1231. /* DCP ERN */
  1232. pr_crit_once("Leaking DCP ERNs!\n");
  1233. break;
  1234. default:
  1235. pr_crit("Invalid MR verb 0x%02x\n", verb);
  1236. }
  1237. } else {
  1238. /* Its a software ERN */
  1239. fq = tag_to_fq(be32_to_cpu(msg->ern.tag));
  1240. fq->cb.ern(p, fq, msg);
  1241. }
  1242. num++;
  1243. qm_mr_next(&p->p);
  1244. }
  1245. qm_mr_cci_consume(&p->p, num);
  1246. preempt_enable();
  1247. }
  1248. static u32 __poll_portal_slow(struct qman_portal *p, u32 is)
  1249. {
  1250. if (is & QM_PIRQ_CSCI) {
  1251. queue_work_on(smp_processor_id(), qm_portal_wq,
  1252. &p->congestion_work);
  1253. }
  1254. if (is & QM_PIRQ_EQRI) {
  1255. qm_eqcr_cce_update(&p->p);
  1256. qm_eqcr_set_ithresh(&p->p, 0);
  1257. wake_up(&affine_queue);
  1258. }
  1259. if (is & QM_PIRQ_MRI) {
  1260. queue_work_on(smp_processor_id(), qm_portal_wq,
  1261. &p->mr_work);
  1262. }
  1263. return is;
  1264. }
  1265. /*
  1266. * remove some slowish-path stuff from the "fast path" and make sure it isn't
  1267. * inlined.
  1268. */
  1269. static noinline void clear_vdqcr(struct qman_portal *p, struct qman_fq *fq)
  1270. {
  1271. p->vdqcr_owned = NULL;
  1272. fq_clear(fq, QMAN_FQ_STATE_VDQCR);
  1273. wake_up(&affine_queue);
  1274. }
  1275. /*
  1276. * The only states that would conflict with other things if they ran at the
  1277. * same time on the same cpu are:
  1278. *
  1279. * (i) setting/clearing vdqcr_owned, and
  1280. * (ii) clearing the NE (Not Empty) flag.
  1281. *
  1282. * Both are safe. Because;
  1283. *
  1284. * (i) this clearing can only occur after qman_volatile_dequeue() has set the
  1285. * vdqcr_owned field (which it does before setting VDQCR), and
  1286. * qman_volatile_dequeue() blocks interrupts and preemption while this is
  1287. * done so that we can't interfere.
  1288. * (ii) the NE flag is only cleared after qman_retire_fq() has set it, and as
  1289. * with (i) that API prevents us from interfering until it's safe.
  1290. *
  1291. * The good thing is that qman_volatile_dequeue() and qman_retire_fq() run far
  1292. * less frequently (ie. per-FQ) than __poll_portal_fast() does, so the nett
  1293. * advantage comes from this function not having to "lock" anything at all.
  1294. *
  1295. * Note also that the callbacks are invoked at points which are safe against the
  1296. * above potential conflicts, but that this function itself is not re-entrant
  1297. * (this is because the function tracks one end of each FIFO in the portal and
  1298. * we do *not* want to lock that). So the consequence is that it is safe for
  1299. * user callbacks to call into any QMan API.
  1300. */
  1301. static inline unsigned int __poll_portal_fast(struct qman_portal *p,
  1302. unsigned int poll_limit)
  1303. {
  1304. const struct qm_dqrr_entry *dq;
  1305. struct qman_fq *fq;
  1306. enum qman_cb_dqrr_result res;
  1307. unsigned int limit = 0;
  1308. do {
  1309. qm_dqrr_pvb_update(&p->p);
  1310. dq = qm_dqrr_current(&p->p);
  1311. if (!dq)
  1312. break;
  1313. if (dq->stat & QM_DQRR_STAT_UNSCHEDULED) {
  1314. /*
  1315. * VDQCR: don't trust context_b as the FQ may have
  1316. * been configured for h/w consumption and we're
  1317. * draining it post-retirement.
  1318. */
  1319. fq = p->vdqcr_owned;
  1320. /*
  1321. * We only set QMAN_FQ_STATE_NE when retiring, so we
  1322. * only need to check for clearing it when doing
  1323. * volatile dequeues. It's one less thing to check
  1324. * in the critical path (SDQCR).
  1325. */
  1326. if (dq->stat & QM_DQRR_STAT_FQ_EMPTY)
  1327. fq_clear(fq, QMAN_FQ_STATE_NE);
  1328. /*
  1329. * This is duplicated from the SDQCR code, but we
  1330. * have stuff to do before *and* after this callback,
  1331. * and we don't want multiple if()s in the critical
  1332. * path (SDQCR).
  1333. */
  1334. res = fq->cb.dqrr(p, fq, dq);
  1335. if (res == qman_cb_dqrr_stop)
  1336. break;
  1337. /* Check for VDQCR completion */
  1338. if (dq->stat & QM_DQRR_STAT_DQCR_EXPIRED)
  1339. clear_vdqcr(p, fq);
  1340. } else {
  1341. /* SDQCR: context_b points to the FQ */
  1342. fq = tag_to_fq(be32_to_cpu(dq->context_b));
  1343. /* Now let the callback do its stuff */
  1344. res = fq->cb.dqrr(p, fq, dq);
  1345. /*
  1346. * The callback can request that we exit without
  1347. * consuming this entry nor advancing;
  1348. */
  1349. if (res == qman_cb_dqrr_stop)
  1350. break;
  1351. }
  1352. /* Interpret 'dq' from a driver perspective. */
  1353. /*
  1354. * Parking isn't possible unless HELDACTIVE was set. NB,
  1355. * FORCEELIGIBLE implies HELDACTIVE, so we only need to
  1356. * check for HELDACTIVE to cover both.
  1357. */
  1358. DPAA_ASSERT((dq->stat & QM_DQRR_STAT_FQ_HELDACTIVE) ||
  1359. (res != qman_cb_dqrr_park));
  1360. /* just means "skip it, I'll consume it myself later on" */
  1361. if (res != qman_cb_dqrr_defer)
  1362. qm_dqrr_cdc_consume_1ptr(&p->p, dq,
  1363. res == qman_cb_dqrr_park);
  1364. /* Move forward */
  1365. qm_dqrr_next(&p->p);
  1366. /*
  1367. * Entry processed and consumed, increment our counter. The
  1368. * callback can request that we exit after consuming the
  1369. * entry, and we also exit if we reach our processing limit,
  1370. * so loop back only if neither of these conditions is met.
  1371. */
  1372. } while (++limit < poll_limit && res != qman_cb_dqrr_consume_stop);
  1373. return limit;
  1374. }
  1375. void qman_p_irqsource_add(struct qman_portal *p, u32 bits)
  1376. {
  1377. unsigned long irqflags;
  1378. local_irq_save(irqflags);
  1379. set_bits(bits & QM_PIRQ_VISIBLE, &p->irq_sources);
  1380. qm_out(&p->p, QM_REG_IER, p->irq_sources);
  1381. local_irq_restore(irqflags);
  1382. }
  1383. EXPORT_SYMBOL(qman_p_irqsource_add);
  1384. void qman_p_irqsource_remove(struct qman_portal *p, u32 bits)
  1385. {
  1386. unsigned long irqflags;
  1387. u32 ier;
  1388. /*
  1389. * Our interrupt handler only processes+clears status register bits that
  1390. * are in p->irq_sources. As we're trimming that mask, if one of them
  1391. * were to assert in the status register just before we remove it from
  1392. * the enable register, there would be an interrupt-storm when we
  1393. * release the IRQ lock. So we wait for the enable register update to
  1394. * take effect in h/w (by reading it back) and then clear all other bits
  1395. * in the status register. Ie. we clear them from ISR once it's certain
  1396. * IER won't allow them to reassert.
  1397. */
  1398. local_irq_save(irqflags);
  1399. bits &= QM_PIRQ_VISIBLE;
  1400. clear_bits(bits, &p->irq_sources);
  1401. qm_out(&p->p, QM_REG_IER, p->irq_sources);
  1402. ier = qm_in(&p->p, QM_REG_IER);
  1403. /*
  1404. * Using "~ier" (rather than "bits" or "~p->irq_sources") creates a
  1405. * data-dependency, ie. to protect against re-ordering.
  1406. */
  1407. qm_out(&p->p, QM_REG_ISR, ~ier);
  1408. local_irq_restore(irqflags);
  1409. }
  1410. EXPORT_SYMBOL(qman_p_irqsource_remove);
  1411. const cpumask_t *qman_affine_cpus(void)
  1412. {
  1413. return &affine_mask;
  1414. }
  1415. EXPORT_SYMBOL(qman_affine_cpus);
  1416. u16 qman_affine_channel(int cpu)
  1417. {
  1418. if (cpu < 0) {
  1419. struct qman_portal *portal = get_affine_portal();
  1420. cpu = portal->config->cpu;
  1421. put_affine_portal();
  1422. }
  1423. WARN_ON(!cpumask_test_cpu(cpu, &affine_mask));
  1424. return affine_channels[cpu];
  1425. }
  1426. EXPORT_SYMBOL(qman_affine_channel);
  1427. struct qman_portal *qman_get_affine_portal(int cpu)
  1428. {
  1429. return affine_portals[cpu];
  1430. }
  1431. EXPORT_SYMBOL(qman_get_affine_portal);
  1432. int qman_p_poll_dqrr(struct qman_portal *p, unsigned int limit)
  1433. {
  1434. return __poll_portal_fast(p, limit);
  1435. }
  1436. EXPORT_SYMBOL(qman_p_poll_dqrr);
  1437. void qman_p_static_dequeue_add(struct qman_portal *p, u32 pools)
  1438. {
  1439. unsigned long irqflags;
  1440. local_irq_save(irqflags);
  1441. pools &= p->config->pools;
  1442. p->sdqcr |= pools;
  1443. qm_dqrr_sdqcr_set(&p->p, p->sdqcr);
  1444. local_irq_restore(irqflags);
  1445. }
  1446. EXPORT_SYMBOL(qman_p_static_dequeue_add);
  1447. /* Frame queue API */
  1448. static const char *mcr_result_str(u8 result)
  1449. {
  1450. switch (result) {
  1451. case QM_MCR_RESULT_NULL:
  1452. return "QM_MCR_RESULT_NULL";
  1453. case QM_MCR_RESULT_OK:
  1454. return "QM_MCR_RESULT_OK";
  1455. case QM_MCR_RESULT_ERR_FQID:
  1456. return "QM_MCR_RESULT_ERR_FQID";
  1457. case QM_MCR_RESULT_ERR_FQSTATE:
  1458. return "QM_MCR_RESULT_ERR_FQSTATE";
  1459. case QM_MCR_RESULT_ERR_NOTEMPTY:
  1460. return "QM_MCR_RESULT_ERR_NOTEMPTY";
  1461. case QM_MCR_RESULT_PENDING:
  1462. return "QM_MCR_RESULT_PENDING";
  1463. case QM_MCR_RESULT_ERR_BADCOMMAND:
  1464. return "QM_MCR_RESULT_ERR_BADCOMMAND";
  1465. }
  1466. return "<unknown MCR result>";
  1467. }
  1468. int qman_create_fq(u32 fqid, u32 flags, struct qman_fq *fq)
  1469. {
  1470. if (flags & QMAN_FQ_FLAG_DYNAMIC_FQID) {
  1471. int ret = qman_alloc_fqid(&fqid);
  1472. if (ret)
  1473. return ret;
  1474. }
  1475. fq->fqid = fqid;
  1476. fq->flags = flags;
  1477. fq->state = qman_fq_state_oos;
  1478. fq->cgr_groupid = 0;
  1479. /* A context_b of 0 is allegedly special, so don't use that fqid */
  1480. if (fqid == 0 || fqid >= num_fqids) {
  1481. WARN(1, "bad fqid %d\n", fqid);
  1482. return -EINVAL;
  1483. }
  1484. fq->idx = fqid * 2;
  1485. if (flags & QMAN_FQ_FLAG_NO_MODIFY)
  1486. fq->idx++;
  1487. WARN_ON(fq_table[fq->idx]);
  1488. fq_table[fq->idx] = fq;
  1489. return 0;
  1490. }
  1491. EXPORT_SYMBOL(qman_create_fq);
  1492. void qman_destroy_fq(struct qman_fq *fq)
  1493. {
  1494. /*
  1495. * We don't need to lock the FQ as it is a pre-condition that the FQ be
  1496. * quiesced. Instead, run some checks.
  1497. */
  1498. switch (fq->state) {
  1499. case qman_fq_state_parked:
  1500. case qman_fq_state_oos:
  1501. if (fq_isset(fq, QMAN_FQ_FLAG_DYNAMIC_FQID))
  1502. qman_release_fqid(fq->fqid);
  1503. DPAA_ASSERT(fq_table[fq->idx]);
  1504. fq_table[fq->idx] = NULL;
  1505. return;
  1506. default:
  1507. break;
  1508. }
  1509. DPAA_ASSERT(NULL == "qman_free_fq() on unquiesced FQ!");
  1510. }
  1511. EXPORT_SYMBOL(qman_destroy_fq);
  1512. u32 qman_fq_fqid(struct qman_fq *fq)
  1513. {
  1514. return fq->fqid;
  1515. }
  1516. EXPORT_SYMBOL(qman_fq_fqid);
  1517. int qman_init_fq(struct qman_fq *fq, u32 flags, struct qm_mcc_initfq *opts)
  1518. {
  1519. union qm_mc_command *mcc;
  1520. union qm_mc_result *mcr;
  1521. struct qman_portal *p;
  1522. u8 res, myverb;
  1523. int ret = 0;
  1524. myverb = (flags & QMAN_INITFQ_FLAG_SCHED)
  1525. ? QM_MCC_VERB_INITFQ_SCHED : QM_MCC_VERB_INITFQ_PARKED;
  1526. if (fq->state != qman_fq_state_oos &&
  1527. fq->state != qman_fq_state_parked)
  1528. return -EINVAL;
  1529. #ifdef CONFIG_FSL_DPAA_CHECKING
  1530. if (fq_isset(fq, QMAN_FQ_FLAG_NO_MODIFY))
  1531. return -EINVAL;
  1532. #endif
  1533. if (opts && (be16_to_cpu(opts->we_mask) & QM_INITFQ_WE_OAC)) {
  1534. /* And can't be set at the same time as TDTHRESH */
  1535. if (be16_to_cpu(opts->we_mask) & QM_INITFQ_WE_TDTHRESH)
  1536. return -EINVAL;
  1537. }
  1538. /* Issue an INITFQ_[PARKED|SCHED] management command */
  1539. p = get_affine_portal();
  1540. if (fq_isset(fq, QMAN_FQ_STATE_CHANGING) ||
  1541. (fq->state != qman_fq_state_oos &&
  1542. fq->state != qman_fq_state_parked)) {
  1543. ret = -EBUSY;
  1544. goto out;
  1545. }
  1546. mcc = qm_mc_start(&p->p);
  1547. if (opts)
  1548. mcc->initfq = *opts;
  1549. qm_fqid_set(&mcc->fq, fq->fqid);
  1550. mcc->initfq.count = 0;
  1551. /*
  1552. * If the FQ does *not* have the TO_DCPORTAL flag, context_b is set as a
  1553. * demux pointer. Otherwise, the caller-provided value is allowed to
  1554. * stand, don't overwrite it.
  1555. */
  1556. if (fq_isclear(fq, QMAN_FQ_FLAG_TO_DCPORTAL)) {
  1557. dma_addr_t phys_fq;
  1558. mcc->initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_CONTEXTB);
  1559. mcc->initfq.fqd.context_b = cpu_to_be32(fq_to_tag(fq));
  1560. /*
  1561. * and the physical address - NB, if the user wasn't trying to
  1562. * set CONTEXTA, clear the stashing settings.
  1563. */
  1564. if (!(be16_to_cpu(mcc->initfq.we_mask) &
  1565. QM_INITFQ_WE_CONTEXTA)) {
  1566. mcc->initfq.we_mask |=
  1567. cpu_to_be16(QM_INITFQ_WE_CONTEXTA);
  1568. memset(&mcc->initfq.fqd.context_a, 0,
  1569. sizeof(mcc->initfq.fqd.context_a));
  1570. } else {
  1571. struct qman_portal *p = qman_dma_portal;
  1572. phys_fq = dma_map_single(p->config->dev, fq,
  1573. sizeof(*fq), DMA_TO_DEVICE);
  1574. if (dma_mapping_error(p->config->dev, phys_fq)) {
  1575. dev_err(p->config->dev, "dma_mapping failed\n");
  1576. ret = -EIO;
  1577. goto out;
  1578. }
  1579. qm_fqd_stashing_set64(&mcc->initfq.fqd, phys_fq);
  1580. }
  1581. }
  1582. if (flags & QMAN_INITFQ_FLAG_LOCAL) {
  1583. int wq = 0;
  1584. if (!(be16_to_cpu(mcc->initfq.we_mask) &
  1585. QM_INITFQ_WE_DESTWQ)) {
  1586. mcc->initfq.we_mask |=
  1587. cpu_to_be16(QM_INITFQ_WE_DESTWQ);
  1588. wq = 4;
  1589. }
  1590. qm_fqd_set_destwq(&mcc->initfq.fqd, p->config->channel, wq);
  1591. }
  1592. qm_mc_commit(&p->p, myverb);
  1593. if (!qm_mc_result_timeout(&p->p, &mcr)) {
  1594. dev_err(p->config->dev, "MCR timeout\n");
  1595. ret = -ETIMEDOUT;
  1596. goto out;
  1597. }
  1598. DPAA_ASSERT((mcr->verb & QM_MCR_VERB_MASK) == myverb);
  1599. res = mcr->result;
  1600. if (res != QM_MCR_RESULT_OK) {
  1601. ret = -EIO;
  1602. goto out;
  1603. }
  1604. if (opts) {
  1605. if (be16_to_cpu(opts->we_mask) & QM_INITFQ_WE_FQCTRL) {
  1606. if (be16_to_cpu(opts->fqd.fq_ctrl) & QM_FQCTRL_CGE)
  1607. fq_set(fq, QMAN_FQ_STATE_CGR_EN);
  1608. else
  1609. fq_clear(fq, QMAN_FQ_STATE_CGR_EN);
  1610. }
  1611. if (be16_to_cpu(opts->we_mask) & QM_INITFQ_WE_CGID)
  1612. fq->cgr_groupid = opts->fqd.cgid;
  1613. }
  1614. fq->state = (flags & QMAN_INITFQ_FLAG_SCHED) ?
  1615. qman_fq_state_sched : qman_fq_state_parked;
  1616. out:
  1617. put_affine_portal();
  1618. return ret;
  1619. }
  1620. EXPORT_SYMBOL(qman_init_fq);
  1621. int qman_schedule_fq(struct qman_fq *fq)
  1622. {
  1623. union qm_mc_command *mcc;
  1624. union qm_mc_result *mcr;
  1625. struct qman_portal *p;
  1626. int ret = 0;
  1627. if (fq->state != qman_fq_state_parked)
  1628. return -EINVAL;
  1629. #ifdef CONFIG_FSL_DPAA_CHECKING
  1630. if (fq_isset(fq, QMAN_FQ_FLAG_NO_MODIFY))
  1631. return -EINVAL;
  1632. #endif
  1633. /* Issue a ALTERFQ_SCHED management command */
  1634. p = get_affine_portal();
  1635. if (fq_isset(fq, QMAN_FQ_STATE_CHANGING) ||
  1636. fq->state != qman_fq_state_parked) {
  1637. ret = -EBUSY;
  1638. goto out;
  1639. }
  1640. mcc = qm_mc_start(&p->p);
  1641. qm_fqid_set(&mcc->fq, fq->fqid);
  1642. qm_mc_commit(&p->p, QM_MCC_VERB_ALTER_SCHED);
  1643. if (!qm_mc_result_timeout(&p->p, &mcr)) {
  1644. dev_err(p->config->dev, "ALTER_SCHED timeout\n");
  1645. ret = -ETIMEDOUT;
  1646. goto out;
  1647. }
  1648. DPAA_ASSERT((mcr->verb & QM_MCR_VERB_MASK) == QM_MCR_VERB_ALTER_SCHED);
  1649. if (mcr->result != QM_MCR_RESULT_OK) {
  1650. ret = -EIO;
  1651. goto out;
  1652. }
  1653. fq->state = qman_fq_state_sched;
  1654. out:
  1655. put_affine_portal();
  1656. return ret;
  1657. }
  1658. EXPORT_SYMBOL(qman_schedule_fq);
  1659. int qman_retire_fq(struct qman_fq *fq, u32 *flags)
  1660. {
  1661. union qm_mc_command *mcc;
  1662. union qm_mc_result *mcr;
  1663. struct qman_portal *p;
  1664. int ret;
  1665. u8 res;
  1666. if (fq->state != qman_fq_state_parked &&
  1667. fq->state != qman_fq_state_sched)
  1668. return -EINVAL;
  1669. #ifdef CONFIG_FSL_DPAA_CHECKING
  1670. if (fq_isset(fq, QMAN_FQ_FLAG_NO_MODIFY))
  1671. return -EINVAL;
  1672. #endif
  1673. p = get_affine_portal();
  1674. if (fq_isset(fq, QMAN_FQ_STATE_CHANGING) ||
  1675. fq->state == qman_fq_state_retired ||
  1676. fq->state == qman_fq_state_oos) {
  1677. ret = -EBUSY;
  1678. goto out;
  1679. }
  1680. mcc = qm_mc_start(&p->p);
  1681. qm_fqid_set(&mcc->fq, fq->fqid);
  1682. qm_mc_commit(&p->p, QM_MCC_VERB_ALTER_RETIRE);
  1683. if (!qm_mc_result_timeout(&p->p, &mcr)) {
  1684. dev_crit(p->config->dev, "ALTER_RETIRE timeout\n");
  1685. ret = -ETIMEDOUT;
  1686. goto out;
  1687. }
  1688. DPAA_ASSERT((mcr->verb & QM_MCR_VERB_MASK) == QM_MCR_VERB_ALTER_RETIRE);
  1689. res = mcr->result;
  1690. /*
  1691. * "Elegant" would be to treat OK/PENDING the same way; set CHANGING,
  1692. * and defer the flags until FQRNI or FQRN (respectively) show up. But
  1693. * "Friendly" is to process OK immediately, and not set CHANGING. We do
  1694. * friendly, otherwise the caller doesn't necessarily have a fully
  1695. * "retired" FQ on return even if the retirement was immediate. However
  1696. * this does mean some code duplication between here and
  1697. * fq_state_change().
  1698. */
  1699. if (res == QM_MCR_RESULT_OK) {
  1700. ret = 0;
  1701. /* Process 'fq' right away, we'll ignore FQRNI */
  1702. if (mcr->alterfq.fqs & QM_MCR_FQS_NOTEMPTY)
  1703. fq_set(fq, QMAN_FQ_STATE_NE);
  1704. if (mcr->alterfq.fqs & QM_MCR_FQS_ORLPRESENT)
  1705. fq_set(fq, QMAN_FQ_STATE_ORL);
  1706. if (flags)
  1707. *flags = fq->flags;
  1708. fq->state = qman_fq_state_retired;
  1709. if (fq->cb.fqs) {
  1710. /*
  1711. * Another issue with supporting "immediate" retirement
  1712. * is that we're forced to drop FQRNIs, because by the
  1713. * time they're seen it may already be "too late" (the
  1714. * fq may have been OOS'd and free()'d already). But if
  1715. * the upper layer wants a callback whether it's
  1716. * immediate or not, we have to fake a "MR" entry to
  1717. * look like an FQRNI...
  1718. */
  1719. union qm_mr_entry msg;
  1720. msg.verb = QM_MR_VERB_FQRNI;
  1721. msg.fq.fqs = mcr->alterfq.fqs;
  1722. qm_fqid_set(&msg.fq, fq->fqid);
  1723. msg.fq.context_b = cpu_to_be32(fq_to_tag(fq));
  1724. fq->cb.fqs(p, fq, &msg);
  1725. }
  1726. } else if (res == QM_MCR_RESULT_PENDING) {
  1727. ret = 1;
  1728. fq_set(fq, QMAN_FQ_STATE_CHANGING);
  1729. } else {
  1730. ret = -EIO;
  1731. }
  1732. out:
  1733. put_affine_portal();
  1734. return ret;
  1735. }
  1736. EXPORT_SYMBOL(qman_retire_fq);
  1737. int qman_oos_fq(struct qman_fq *fq)
  1738. {
  1739. union qm_mc_command *mcc;
  1740. union qm_mc_result *mcr;
  1741. struct qman_portal *p;
  1742. int ret = 0;
  1743. if (fq->state != qman_fq_state_retired)
  1744. return -EINVAL;
  1745. #ifdef CONFIG_FSL_DPAA_CHECKING
  1746. if (fq_isset(fq, QMAN_FQ_FLAG_NO_MODIFY))
  1747. return -EINVAL;
  1748. #endif
  1749. p = get_affine_portal();
  1750. if (fq_isset(fq, QMAN_FQ_STATE_BLOCKOOS) ||
  1751. fq->state != qman_fq_state_retired) {
  1752. ret = -EBUSY;
  1753. goto out;
  1754. }
  1755. mcc = qm_mc_start(&p->p);
  1756. qm_fqid_set(&mcc->fq, fq->fqid);
  1757. qm_mc_commit(&p->p, QM_MCC_VERB_ALTER_OOS);
  1758. if (!qm_mc_result_timeout(&p->p, &mcr)) {
  1759. ret = -ETIMEDOUT;
  1760. goto out;
  1761. }
  1762. DPAA_ASSERT((mcr->verb & QM_MCR_VERB_MASK) == QM_MCR_VERB_ALTER_OOS);
  1763. if (mcr->result != QM_MCR_RESULT_OK) {
  1764. ret = -EIO;
  1765. goto out;
  1766. }
  1767. fq->state = qman_fq_state_oos;
  1768. out:
  1769. put_affine_portal();
  1770. return ret;
  1771. }
  1772. EXPORT_SYMBOL(qman_oos_fq);
  1773. int qman_query_fq(struct qman_fq *fq, struct qm_fqd *fqd)
  1774. {
  1775. union qm_mc_command *mcc;
  1776. union qm_mc_result *mcr;
  1777. struct qman_portal *p = get_affine_portal();
  1778. int ret = 0;
  1779. mcc = qm_mc_start(&p->p);
  1780. qm_fqid_set(&mcc->fq, fq->fqid);
  1781. qm_mc_commit(&p->p, QM_MCC_VERB_QUERYFQ);
  1782. if (!qm_mc_result_timeout(&p->p, &mcr)) {
  1783. ret = -ETIMEDOUT;
  1784. goto out;
  1785. }
  1786. DPAA_ASSERT((mcr->verb & QM_MCR_VERB_MASK) == QM_MCR_VERB_QUERYFQ);
  1787. if (mcr->result == QM_MCR_RESULT_OK)
  1788. *fqd = mcr->queryfq.fqd;
  1789. else
  1790. ret = -EIO;
  1791. out:
  1792. put_affine_portal();
  1793. return ret;
  1794. }
  1795. static int qman_query_fq_np(struct qman_fq *fq,
  1796. struct qm_mcr_queryfq_np *np)
  1797. {
  1798. union qm_mc_command *mcc;
  1799. union qm_mc_result *mcr;
  1800. struct qman_portal *p = get_affine_portal();
  1801. int ret = 0;
  1802. mcc = qm_mc_start(&p->p);
  1803. qm_fqid_set(&mcc->fq, fq->fqid);
  1804. qm_mc_commit(&p->p, QM_MCC_VERB_QUERYFQ_NP);
  1805. if (!qm_mc_result_timeout(&p->p, &mcr)) {
  1806. ret = -ETIMEDOUT;
  1807. goto out;
  1808. }
  1809. DPAA_ASSERT((mcr->verb & QM_MCR_VERB_MASK) == QM_MCR_VERB_QUERYFQ_NP);
  1810. if (mcr->result == QM_MCR_RESULT_OK)
  1811. *np = mcr->queryfq_np;
  1812. else if (mcr->result == QM_MCR_RESULT_ERR_FQID)
  1813. ret = -ERANGE;
  1814. else
  1815. ret = -EIO;
  1816. out:
  1817. put_affine_portal();
  1818. return ret;
  1819. }
  1820. static int qman_query_cgr(struct qman_cgr *cgr,
  1821. struct qm_mcr_querycgr *cgrd)
  1822. {
  1823. union qm_mc_command *mcc;
  1824. union qm_mc_result *mcr;
  1825. struct qman_portal *p = get_affine_portal();
  1826. int ret = 0;
  1827. mcc = qm_mc_start(&p->p);
  1828. mcc->cgr.cgid = cgr->cgrid;
  1829. qm_mc_commit(&p->p, QM_MCC_VERB_QUERYCGR);
  1830. if (!qm_mc_result_timeout(&p->p, &mcr)) {
  1831. ret = -ETIMEDOUT;
  1832. goto out;
  1833. }
  1834. DPAA_ASSERT((mcr->verb & QM_MCR_VERB_MASK) == QM_MCC_VERB_QUERYCGR);
  1835. if (mcr->result == QM_MCR_RESULT_OK)
  1836. *cgrd = mcr->querycgr;
  1837. else {
  1838. dev_err(p->config->dev, "QUERY_CGR failed: %s\n",
  1839. mcr_result_str(mcr->result));
  1840. ret = -EIO;
  1841. }
  1842. out:
  1843. put_affine_portal();
  1844. return ret;
  1845. }
  1846. int qman_query_cgr_congested(struct qman_cgr *cgr, bool *result)
  1847. {
  1848. struct qm_mcr_querycgr query_cgr;
  1849. int err;
  1850. err = qman_query_cgr(cgr, &query_cgr);
  1851. if (err)
  1852. return err;
  1853. *result = !!query_cgr.cgr.cs;
  1854. return 0;
  1855. }
  1856. EXPORT_SYMBOL(qman_query_cgr_congested);
  1857. /* internal function used as a wait_event() expression */
  1858. static int set_p_vdqcr(struct qman_portal *p, struct qman_fq *fq, u32 vdqcr)
  1859. {
  1860. unsigned long irqflags;
  1861. int ret = -EBUSY;
  1862. local_irq_save(irqflags);
  1863. if (p->vdqcr_owned)
  1864. goto out;
  1865. if (fq_isset(fq, QMAN_FQ_STATE_VDQCR))
  1866. goto out;
  1867. fq_set(fq, QMAN_FQ_STATE_VDQCR);
  1868. p->vdqcr_owned = fq;
  1869. qm_dqrr_vdqcr_set(&p->p, vdqcr);
  1870. ret = 0;
  1871. out:
  1872. local_irq_restore(irqflags);
  1873. return ret;
  1874. }
  1875. static int set_vdqcr(struct qman_portal **p, struct qman_fq *fq, u32 vdqcr)
  1876. {
  1877. int ret;
  1878. *p = get_affine_portal();
  1879. ret = set_p_vdqcr(*p, fq, vdqcr);
  1880. put_affine_portal();
  1881. return ret;
  1882. }
  1883. static int wait_vdqcr_start(struct qman_portal **p, struct qman_fq *fq,
  1884. u32 vdqcr, u32 flags)
  1885. {
  1886. int ret = 0;
  1887. if (flags & QMAN_VOLATILE_FLAG_WAIT_INT)
  1888. ret = wait_event_interruptible(affine_queue,
  1889. !set_vdqcr(p, fq, vdqcr));
  1890. else
  1891. wait_event(affine_queue, !set_vdqcr(p, fq, vdqcr));
  1892. return ret;
  1893. }
  1894. int qman_volatile_dequeue(struct qman_fq *fq, u32 flags, u32 vdqcr)
  1895. {
  1896. struct qman_portal *p;
  1897. int ret;
  1898. if (fq->state != qman_fq_state_parked &&
  1899. fq->state != qman_fq_state_retired)
  1900. return -EINVAL;
  1901. if (vdqcr & QM_VDQCR_FQID_MASK)
  1902. return -EINVAL;
  1903. if (fq_isset(fq, QMAN_FQ_STATE_VDQCR))
  1904. return -EBUSY;
  1905. vdqcr = (vdqcr & ~QM_VDQCR_FQID_MASK) | fq->fqid;
  1906. if (flags & QMAN_VOLATILE_FLAG_WAIT)
  1907. ret = wait_vdqcr_start(&p, fq, vdqcr, flags);
  1908. else
  1909. ret = set_vdqcr(&p, fq, vdqcr);
  1910. if (ret)
  1911. return ret;
  1912. /* VDQCR is set */
  1913. if (flags & QMAN_VOLATILE_FLAG_FINISH) {
  1914. if (flags & QMAN_VOLATILE_FLAG_WAIT_INT)
  1915. /*
  1916. * NB: don't propagate any error - the caller wouldn't
  1917. * know whether the VDQCR was issued or not. A signal
  1918. * could arrive after returning anyway, so the caller
  1919. * can check signal_pending() if that's an issue.
  1920. */
  1921. wait_event_interruptible(affine_queue,
  1922. !fq_isset(fq, QMAN_FQ_STATE_VDQCR));
  1923. else
  1924. wait_event(affine_queue,
  1925. !fq_isset(fq, QMAN_FQ_STATE_VDQCR));
  1926. }
  1927. return 0;
  1928. }
  1929. EXPORT_SYMBOL(qman_volatile_dequeue);
  1930. static void update_eqcr_ci(struct qman_portal *p, u8 avail)
  1931. {
  1932. if (avail)
  1933. qm_eqcr_cce_prefetch(&p->p);
  1934. else
  1935. qm_eqcr_cce_update(&p->p);
  1936. }
  1937. int qman_enqueue(struct qman_fq *fq, const struct qm_fd *fd)
  1938. {
  1939. struct qman_portal *p;
  1940. struct qm_eqcr_entry *eq;
  1941. unsigned long irqflags;
  1942. u8 avail;
  1943. p = get_affine_portal();
  1944. local_irq_save(irqflags);
  1945. if (p->use_eqcr_ci_stashing) {
  1946. /*
  1947. * The stashing case is easy, only update if we need to in
  1948. * order to try and liberate ring entries.
  1949. */
  1950. eq = qm_eqcr_start_stash(&p->p);
  1951. } else {
  1952. /*
  1953. * The non-stashing case is harder, need to prefetch ahead of
  1954. * time.
  1955. */
  1956. avail = qm_eqcr_get_avail(&p->p);
  1957. if (avail < 2)
  1958. update_eqcr_ci(p, avail);
  1959. eq = qm_eqcr_start_no_stash(&p->p);
  1960. }
  1961. if (unlikely(!eq))
  1962. goto out;
  1963. qm_fqid_set(eq, fq->fqid);
  1964. eq->tag = cpu_to_be32(fq_to_tag(fq));
  1965. eq->fd = *fd;
  1966. qm_eqcr_pvb_commit(&p->p, QM_EQCR_VERB_CMD_ENQUEUE);
  1967. out:
  1968. local_irq_restore(irqflags);
  1969. put_affine_portal();
  1970. return 0;
  1971. }
  1972. EXPORT_SYMBOL(qman_enqueue);
  1973. static int qm_modify_cgr(struct qman_cgr *cgr, u32 flags,
  1974. struct qm_mcc_initcgr *opts)
  1975. {
  1976. union qm_mc_command *mcc;
  1977. union qm_mc_result *mcr;
  1978. struct qman_portal *p = get_affine_portal();
  1979. u8 verb = QM_MCC_VERB_MODIFYCGR;
  1980. int ret = 0;
  1981. mcc = qm_mc_start(&p->p);
  1982. if (opts)
  1983. mcc->initcgr = *opts;
  1984. mcc->initcgr.cgid = cgr->cgrid;
  1985. if (flags & QMAN_CGR_FLAG_USE_INIT)
  1986. verb = QM_MCC_VERB_INITCGR;
  1987. qm_mc_commit(&p->p, verb);
  1988. if (!qm_mc_result_timeout(&p->p, &mcr)) {
  1989. ret = -ETIMEDOUT;
  1990. goto out;
  1991. }
  1992. DPAA_ASSERT((mcr->verb & QM_MCR_VERB_MASK) == verb);
  1993. if (mcr->result != QM_MCR_RESULT_OK)
  1994. ret = -EIO;
  1995. out:
  1996. put_affine_portal();
  1997. return ret;
  1998. }
  1999. #define PORTAL_IDX(n) (n->config->channel - QM_CHANNEL_SWPORTAL0)
  2000. /* congestion state change notification target update control */
  2001. static void qm_cgr_cscn_targ_set(struct __qm_mc_cgr *cgr, int pi, u32 val)
  2002. {
  2003. if (qman_ip_rev >= QMAN_REV30)
  2004. cgr->cscn_targ_upd_ctrl = cpu_to_be16(pi |
  2005. QM_CGR_TARG_UDP_CTRL_WRITE_BIT);
  2006. else
  2007. cgr->cscn_targ = cpu_to_be32(val | QM_CGR_TARG_PORTAL(pi));
  2008. }
  2009. static void qm_cgr_cscn_targ_clear(struct __qm_mc_cgr *cgr, int pi, u32 val)
  2010. {
  2011. if (qman_ip_rev >= QMAN_REV30)
  2012. cgr->cscn_targ_upd_ctrl = cpu_to_be16(pi);
  2013. else
  2014. cgr->cscn_targ = cpu_to_be32(val & ~QM_CGR_TARG_PORTAL(pi));
  2015. }
  2016. static u8 qman_cgr_cpus[CGR_NUM];
  2017. void qman_init_cgr_all(void)
  2018. {
  2019. struct qman_cgr cgr;
  2020. int err_cnt = 0;
  2021. for (cgr.cgrid = 0; cgr.cgrid < CGR_NUM; cgr.cgrid++) {
  2022. if (qm_modify_cgr(&cgr, QMAN_CGR_FLAG_USE_INIT, NULL))
  2023. err_cnt++;
  2024. }
  2025. if (err_cnt)
  2026. pr_err("Warning: %d error%s while initialising CGR h/w\n",
  2027. err_cnt, (err_cnt > 1) ? "s" : "");
  2028. }
  2029. int qman_create_cgr(struct qman_cgr *cgr, u32 flags,
  2030. struct qm_mcc_initcgr *opts)
  2031. {
  2032. struct qm_mcr_querycgr cgr_state;
  2033. int ret;
  2034. struct qman_portal *p;
  2035. /*
  2036. * We have to check that the provided CGRID is within the limits of the
  2037. * data-structures, for obvious reasons. However we'll let h/w take
  2038. * care of determining whether it's within the limits of what exists on
  2039. * the SoC.
  2040. */
  2041. if (cgr->cgrid >= CGR_NUM)
  2042. return -EINVAL;
  2043. preempt_disable();
  2044. p = get_affine_portal();
  2045. qman_cgr_cpus[cgr->cgrid] = smp_processor_id();
  2046. preempt_enable();
  2047. cgr->chan = p->config->channel;
  2048. spin_lock(&p->cgr_lock);
  2049. if (opts) {
  2050. struct qm_mcc_initcgr local_opts = *opts;
  2051. ret = qman_query_cgr(cgr, &cgr_state);
  2052. if (ret)
  2053. goto out;
  2054. qm_cgr_cscn_targ_set(&local_opts.cgr, PORTAL_IDX(p),
  2055. be32_to_cpu(cgr_state.cgr.cscn_targ));
  2056. local_opts.we_mask |= cpu_to_be16(QM_CGR_WE_CSCN_TARG);
  2057. /* send init if flags indicate so */
  2058. if (flags & QMAN_CGR_FLAG_USE_INIT)
  2059. ret = qm_modify_cgr(cgr, QMAN_CGR_FLAG_USE_INIT,
  2060. &local_opts);
  2061. else
  2062. ret = qm_modify_cgr(cgr, 0, &local_opts);
  2063. if (ret)
  2064. goto out;
  2065. }
  2066. list_add(&cgr->node, &p->cgr_cbs);
  2067. /* Determine if newly added object requires its callback to be called */
  2068. ret = qman_query_cgr(cgr, &cgr_state);
  2069. if (ret) {
  2070. /* we can't go back, so proceed and return success */
  2071. dev_err(p->config->dev, "CGR HW state partially modified\n");
  2072. ret = 0;
  2073. goto out;
  2074. }
  2075. if (cgr->cb && cgr_state.cgr.cscn_en &&
  2076. qman_cgrs_get(&p->cgrs[1], cgr->cgrid))
  2077. cgr->cb(p, cgr, 1);
  2078. out:
  2079. spin_unlock(&p->cgr_lock);
  2080. put_affine_portal();
  2081. return ret;
  2082. }
  2083. EXPORT_SYMBOL(qman_create_cgr);
  2084. int qman_delete_cgr(struct qman_cgr *cgr)
  2085. {
  2086. unsigned long irqflags;
  2087. struct qm_mcr_querycgr cgr_state;
  2088. struct qm_mcc_initcgr local_opts;
  2089. int ret = 0;
  2090. struct qman_cgr *i;
  2091. struct qman_portal *p = get_affine_portal();
  2092. if (cgr->chan != p->config->channel) {
  2093. /* attempt to delete from other portal than creator */
  2094. dev_err(p->config->dev, "CGR not owned by current portal");
  2095. dev_dbg(p->config->dev, " create 0x%x, delete 0x%x\n",
  2096. cgr->chan, p->config->channel);
  2097. ret = -EINVAL;
  2098. goto put_portal;
  2099. }
  2100. memset(&local_opts, 0, sizeof(struct qm_mcc_initcgr));
  2101. spin_lock_irqsave(&p->cgr_lock, irqflags);
  2102. list_del(&cgr->node);
  2103. /*
  2104. * If there are no other CGR objects for this CGRID in the list,
  2105. * update CSCN_TARG accordingly
  2106. */
  2107. list_for_each_entry(i, &p->cgr_cbs, node)
  2108. if (i->cgrid == cgr->cgrid && i->cb)
  2109. goto release_lock;
  2110. ret = qman_query_cgr(cgr, &cgr_state);
  2111. if (ret) {
  2112. /* add back to the list */
  2113. list_add(&cgr->node, &p->cgr_cbs);
  2114. goto release_lock;
  2115. }
  2116. local_opts.we_mask = cpu_to_be16(QM_CGR_WE_CSCN_TARG);
  2117. qm_cgr_cscn_targ_clear(&local_opts.cgr, PORTAL_IDX(p),
  2118. be32_to_cpu(cgr_state.cgr.cscn_targ));
  2119. ret = qm_modify_cgr(cgr, 0, &local_opts);
  2120. if (ret)
  2121. /* add back to the list */
  2122. list_add(&cgr->node, &p->cgr_cbs);
  2123. release_lock:
  2124. spin_unlock_irqrestore(&p->cgr_lock, irqflags);
  2125. put_portal:
  2126. put_affine_portal();
  2127. return ret;
  2128. }
  2129. EXPORT_SYMBOL(qman_delete_cgr);
  2130. struct cgr_comp {
  2131. struct qman_cgr *cgr;
  2132. struct completion completion;
  2133. };
  2134. static int qman_delete_cgr_thread(void *p)
  2135. {
  2136. struct cgr_comp *cgr_comp = (struct cgr_comp *)p;
  2137. int ret;
  2138. ret = qman_delete_cgr(cgr_comp->cgr);
  2139. complete(&cgr_comp->completion);
  2140. return ret;
  2141. }
  2142. void qman_delete_cgr_safe(struct qman_cgr *cgr)
  2143. {
  2144. struct task_struct *thread;
  2145. struct cgr_comp cgr_comp;
  2146. preempt_disable();
  2147. if (qman_cgr_cpus[cgr->cgrid] != smp_processor_id()) {
  2148. init_completion(&cgr_comp.completion);
  2149. cgr_comp.cgr = cgr;
  2150. thread = kthread_create(qman_delete_cgr_thread, &cgr_comp,
  2151. "cgr_del");
  2152. if (IS_ERR(thread))
  2153. goto out;
  2154. kthread_bind(thread, qman_cgr_cpus[cgr->cgrid]);
  2155. wake_up_process(thread);
  2156. wait_for_completion(&cgr_comp.completion);
  2157. preempt_enable();
  2158. return;
  2159. }
  2160. out:
  2161. qman_delete_cgr(cgr);
  2162. preempt_enable();
  2163. }
  2164. EXPORT_SYMBOL(qman_delete_cgr_safe);
  2165. /* Cleanup FQs */
  2166. static int _qm_mr_consume_and_match_verb(struct qm_portal *p, int v)
  2167. {
  2168. const union qm_mr_entry *msg;
  2169. int found = 0;
  2170. qm_mr_pvb_update(p);
  2171. msg = qm_mr_current(p);
  2172. while (msg) {
  2173. if ((msg->verb & QM_MR_VERB_TYPE_MASK) == v)
  2174. found = 1;
  2175. qm_mr_next(p);
  2176. qm_mr_cci_consume_to_current(p);
  2177. qm_mr_pvb_update(p);
  2178. msg = qm_mr_current(p);
  2179. }
  2180. return found;
  2181. }
  2182. static int _qm_dqrr_consume_and_match(struct qm_portal *p, u32 fqid, int s,
  2183. bool wait)
  2184. {
  2185. const struct qm_dqrr_entry *dqrr;
  2186. int found = 0;
  2187. do {
  2188. qm_dqrr_pvb_update(p);
  2189. dqrr = qm_dqrr_current(p);
  2190. if (!dqrr)
  2191. cpu_relax();
  2192. } while (wait && !dqrr);
  2193. while (dqrr) {
  2194. if (qm_fqid_get(dqrr) == fqid && (dqrr->stat & s))
  2195. found = 1;
  2196. qm_dqrr_cdc_consume_1ptr(p, dqrr, 0);
  2197. qm_dqrr_pvb_update(p);
  2198. qm_dqrr_next(p);
  2199. dqrr = qm_dqrr_current(p);
  2200. }
  2201. return found;
  2202. }
  2203. #define qm_mr_drain(p, V) \
  2204. _qm_mr_consume_and_match_verb(p, QM_MR_VERB_##V)
  2205. #define qm_dqrr_drain(p, f, S) \
  2206. _qm_dqrr_consume_and_match(p, f, QM_DQRR_STAT_##S, false)
  2207. #define qm_dqrr_drain_wait(p, f, S) \
  2208. _qm_dqrr_consume_and_match(p, f, QM_DQRR_STAT_##S, true)
  2209. #define qm_dqrr_drain_nomatch(p) \
  2210. _qm_dqrr_consume_and_match(p, 0, 0, false)
  2211. static int qman_shutdown_fq(u32 fqid)
  2212. {
  2213. struct qman_portal *p;
  2214. struct device *dev;
  2215. union qm_mc_command *mcc;
  2216. union qm_mc_result *mcr;
  2217. int orl_empty, drain = 0, ret = 0;
  2218. u32 channel, wq, res;
  2219. u8 state;
  2220. p = get_affine_portal();
  2221. dev = p->config->dev;
  2222. /* Determine the state of the FQID */
  2223. mcc = qm_mc_start(&p->p);
  2224. qm_fqid_set(&mcc->fq, fqid);
  2225. qm_mc_commit(&p->p, QM_MCC_VERB_QUERYFQ_NP);
  2226. if (!qm_mc_result_timeout(&p->p, &mcr)) {
  2227. dev_err(dev, "QUERYFQ_NP timeout\n");
  2228. ret = -ETIMEDOUT;
  2229. goto out;
  2230. }
  2231. DPAA_ASSERT((mcr->verb & QM_MCR_VERB_MASK) == QM_MCR_VERB_QUERYFQ_NP);
  2232. state = mcr->queryfq_np.state & QM_MCR_NP_STATE_MASK;
  2233. if (state == QM_MCR_NP_STATE_OOS)
  2234. goto out; /* Already OOS, no need to do anymore checks */
  2235. /* Query which channel the FQ is using */
  2236. mcc = qm_mc_start(&p->p);
  2237. qm_fqid_set(&mcc->fq, fqid);
  2238. qm_mc_commit(&p->p, QM_MCC_VERB_QUERYFQ);
  2239. if (!qm_mc_result_timeout(&p->p, &mcr)) {
  2240. dev_err(dev, "QUERYFQ timeout\n");
  2241. ret = -ETIMEDOUT;
  2242. goto out;
  2243. }
  2244. DPAA_ASSERT((mcr->verb & QM_MCR_VERB_MASK) == QM_MCR_VERB_QUERYFQ);
  2245. /* Need to store these since the MCR gets reused */
  2246. channel = qm_fqd_get_chan(&mcr->queryfq.fqd);
  2247. wq = qm_fqd_get_wq(&mcr->queryfq.fqd);
  2248. switch (state) {
  2249. case QM_MCR_NP_STATE_TEN_SCHED:
  2250. case QM_MCR_NP_STATE_TRU_SCHED:
  2251. case QM_MCR_NP_STATE_ACTIVE:
  2252. case QM_MCR_NP_STATE_PARKED:
  2253. orl_empty = 0;
  2254. mcc = qm_mc_start(&p->p);
  2255. qm_fqid_set(&mcc->fq, fqid);
  2256. qm_mc_commit(&p->p, QM_MCC_VERB_ALTER_RETIRE);
  2257. if (!qm_mc_result_timeout(&p->p, &mcr)) {
  2258. dev_err(dev, "QUERYFQ_NP timeout\n");
  2259. ret = -ETIMEDOUT;
  2260. goto out;
  2261. }
  2262. DPAA_ASSERT((mcr->verb & QM_MCR_VERB_MASK) ==
  2263. QM_MCR_VERB_ALTER_RETIRE);
  2264. res = mcr->result; /* Make a copy as we reuse MCR below */
  2265. if (res == QM_MCR_RESULT_PENDING) {
  2266. /*
  2267. * Need to wait for the FQRN in the message ring, which
  2268. * will only occur once the FQ has been drained. In
  2269. * order for the FQ to drain the portal needs to be set
  2270. * to dequeue from the channel the FQ is scheduled on
  2271. */
  2272. int found_fqrn = 0;
  2273. u16 dequeue_wq = 0;
  2274. /* Flag that we need to drain FQ */
  2275. drain = 1;
  2276. if (channel >= qm_channel_pool1 &&
  2277. channel < qm_channel_pool1 + 15) {
  2278. /* Pool channel, enable the bit in the portal */
  2279. dequeue_wq = (channel -
  2280. qm_channel_pool1 + 1)<<4 | wq;
  2281. } else if (channel < qm_channel_pool1) {
  2282. /* Dedicated channel */
  2283. dequeue_wq = wq;
  2284. } else {
  2285. dev_err(dev, "Can't recover FQ 0x%x, ch: 0x%x",
  2286. fqid, channel);
  2287. ret = -EBUSY;
  2288. goto out;
  2289. }
  2290. /* Set the sdqcr to drain this channel */
  2291. if (channel < qm_channel_pool1)
  2292. qm_dqrr_sdqcr_set(&p->p,
  2293. QM_SDQCR_TYPE_ACTIVE |
  2294. QM_SDQCR_CHANNELS_DEDICATED);
  2295. else
  2296. qm_dqrr_sdqcr_set(&p->p,
  2297. QM_SDQCR_TYPE_ACTIVE |
  2298. QM_SDQCR_CHANNELS_POOL_CONV
  2299. (channel));
  2300. do {
  2301. /* Keep draining DQRR while checking the MR*/
  2302. qm_dqrr_drain_nomatch(&p->p);
  2303. /* Process message ring too */
  2304. found_fqrn = qm_mr_drain(&p->p, FQRN);
  2305. cpu_relax();
  2306. } while (!found_fqrn);
  2307. }
  2308. if (res != QM_MCR_RESULT_OK &&
  2309. res != QM_MCR_RESULT_PENDING) {
  2310. dev_err(dev, "retire_fq failed: FQ 0x%x, res=0x%x\n",
  2311. fqid, res);
  2312. ret = -EIO;
  2313. goto out;
  2314. }
  2315. if (!(mcr->alterfq.fqs & QM_MCR_FQS_ORLPRESENT)) {
  2316. /*
  2317. * ORL had no entries, no need to wait until the
  2318. * ERNs come in
  2319. */
  2320. orl_empty = 1;
  2321. }
  2322. /*
  2323. * Retirement succeeded, check to see if FQ needs
  2324. * to be drained
  2325. */
  2326. if (drain || mcr->alterfq.fqs & QM_MCR_FQS_NOTEMPTY) {
  2327. /* FQ is Not Empty, drain using volatile DQ commands */
  2328. do {
  2329. u32 vdqcr = fqid | QM_VDQCR_NUMFRAMES_SET(3);
  2330. qm_dqrr_vdqcr_set(&p->p, vdqcr);
  2331. /*
  2332. * Wait for a dequeue and process the dequeues,
  2333. * making sure to empty the ring completely
  2334. */
  2335. } while (qm_dqrr_drain_wait(&p->p, fqid, FQ_EMPTY));
  2336. }
  2337. qm_dqrr_sdqcr_set(&p->p, 0);
  2338. while (!orl_empty) {
  2339. /* Wait for the ORL to have been completely drained */
  2340. orl_empty = qm_mr_drain(&p->p, FQRL);
  2341. cpu_relax();
  2342. }
  2343. mcc = qm_mc_start(&p->p);
  2344. qm_fqid_set(&mcc->fq, fqid);
  2345. qm_mc_commit(&p->p, QM_MCC_VERB_ALTER_OOS);
  2346. if (!qm_mc_result_timeout(&p->p, &mcr)) {
  2347. ret = -ETIMEDOUT;
  2348. goto out;
  2349. }
  2350. DPAA_ASSERT((mcr->verb & QM_MCR_VERB_MASK) ==
  2351. QM_MCR_VERB_ALTER_OOS);
  2352. if (mcr->result != QM_MCR_RESULT_OK) {
  2353. dev_err(dev, "OOS after drain fail: FQ 0x%x (0x%x)\n",
  2354. fqid, mcr->result);
  2355. ret = -EIO;
  2356. goto out;
  2357. }
  2358. break;
  2359. case QM_MCR_NP_STATE_RETIRED:
  2360. /* Send OOS Command */
  2361. mcc = qm_mc_start(&p->p);
  2362. qm_fqid_set(&mcc->fq, fqid);
  2363. qm_mc_commit(&p->p, QM_MCC_VERB_ALTER_OOS);
  2364. if (!qm_mc_result_timeout(&p->p, &mcr)) {
  2365. ret = -ETIMEDOUT;
  2366. goto out;
  2367. }
  2368. DPAA_ASSERT((mcr->verb & QM_MCR_VERB_MASK) ==
  2369. QM_MCR_VERB_ALTER_OOS);
  2370. if (mcr->result) {
  2371. dev_err(dev, "OOS fail: FQ 0x%x (0x%x)\n",
  2372. fqid, mcr->result);
  2373. ret = -EIO;
  2374. goto out;
  2375. }
  2376. break;
  2377. case QM_MCR_NP_STATE_OOS:
  2378. /* Done */
  2379. break;
  2380. default:
  2381. ret = -EIO;
  2382. }
  2383. out:
  2384. put_affine_portal();
  2385. return ret;
  2386. }
  2387. const struct qm_portal_config *qman_get_qm_portal_config(
  2388. struct qman_portal *portal)
  2389. {
  2390. return portal->config;
  2391. }
  2392. EXPORT_SYMBOL(qman_get_qm_portal_config);
  2393. struct gen_pool *qm_fqalloc; /* FQID allocator */
  2394. struct gen_pool *qm_qpalloc; /* pool-channel allocator */
  2395. struct gen_pool *qm_cgralloc; /* CGR ID allocator */
  2396. static int qman_alloc_range(struct gen_pool *p, u32 *result, u32 cnt)
  2397. {
  2398. unsigned long addr;
  2399. addr = gen_pool_alloc(p, cnt);
  2400. if (!addr)
  2401. return -ENOMEM;
  2402. *result = addr & ~DPAA_GENALLOC_OFF;
  2403. return 0;
  2404. }
  2405. int qman_alloc_fqid_range(u32 *result, u32 count)
  2406. {
  2407. return qman_alloc_range(qm_fqalloc, result, count);
  2408. }
  2409. EXPORT_SYMBOL(qman_alloc_fqid_range);
  2410. int qman_alloc_pool_range(u32 *result, u32 count)
  2411. {
  2412. return qman_alloc_range(qm_qpalloc, result, count);
  2413. }
  2414. EXPORT_SYMBOL(qman_alloc_pool_range);
  2415. int qman_alloc_cgrid_range(u32 *result, u32 count)
  2416. {
  2417. return qman_alloc_range(qm_cgralloc, result, count);
  2418. }
  2419. EXPORT_SYMBOL(qman_alloc_cgrid_range);
  2420. int qman_release_fqid(u32 fqid)
  2421. {
  2422. int ret = qman_shutdown_fq(fqid);
  2423. if (ret) {
  2424. pr_debug("FQID %d leaked\n", fqid);
  2425. return ret;
  2426. }
  2427. gen_pool_free(qm_fqalloc, fqid | DPAA_GENALLOC_OFF, 1);
  2428. return 0;
  2429. }
  2430. EXPORT_SYMBOL(qman_release_fqid);
  2431. static int qpool_cleanup(u32 qp)
  2432. {
  2433. /*
  2434. * We query all FQDs starting from
  2435. * FQID 1 until we get an "invalid FQID" error, looking for non-OOS FQDs
  2436. * whose destination channel is the pool-channel being released.
  2437. * When a non-OOS FQD is found we attempt to clean it up
  2438. */
  2439. struct qman_fq fq = {
  2440. .fqid = QM_FQID_RANGE_START
  2441. };
  2442. int err;
  2443. do {
  2444. struct qm_mcr_queryfq_np np;
  2445. err = qman_query_fq_np(&fq, &np);
  2446. if (err == -ERANGE)
  2447. /* FQID range exceeded, found no problems */
  2448. return 0;
  2449. else if (WARN_ON(err))
  2450. return err;
  2451. if ((np.state & QM_MCR_NP_STATE_MASK) != QM_MCR_NP_STATE_OOS) {
  2452. struct qm_fqd fqd;
  2453. err = qman_query_fq(&fq, &fqd);
  2454. if (WARN_ON(err))
  2455. return err;
  2456. if (qm_fqd_get_chan(&fqd) == qp) {
  2457. /* The channel is the FQ's target, clean it */
  2458. err = qman_shutdown_fq(fq.fqid);
  2459. if (err)
  2460. /*
  2461. * Couldn't shut down the FQ
  2462. * so the pool must be leaked
  2463. */
  2464. return err;
  2465. }
  2466. }
  2467. /* Move to the next FQID */
  2468. fq.fqid++;
  2469. } while (1);
  2470. }
  2471. int qman_release_pool(u32 qp)
  2472. {
  2473. int ret;
  2474. ret = qpool_cleanup(qp);
  2475. if (ret) {
  2476. pr_debug("CHID %d leaked\n", qp);
  2477. return ret;
  2478. }
  2479. gen_pool_free(qm_qpalloc, qp | DPAA_GENALLOC_OFF, 1);
  2480. return 0;
  2481. }
  2482. EXPORT_SYMBOL(qman_release_pool);
  2483. static int cgr_cleanup(u32 cgrid)
  2484. {
  2485. /*
  2486. * query all FQDs starting from FQID 1 until we get an "invalid FQID"
  2487. * error, looking for non-OOS FQDs whose CGR is the CGR being released
  2488. */
  2489. struct qman_fq fq = {
  2490. .fqid = QM_FQID_RANGE_START
  2491. };
  2492. int err;
  2493. do {
  2494. struct qm_mcr_queryfq_np np;
  2495. err = qman_query_fq_np(&fq, &np);
  2496. if (err == -ERANGE)
  2497. /* FQID range exceeded, found no problems */
  2498. return 0;
  2499. else if (WARN_ON(err))
  2500. return err;
  2501. if ((np.state & QM_MCR_NP_STATE_MASK) != QM_MCR_NP_STATE_OOS) {
  2502. struct qm_fqd fqd;
  2503. err = qman_query_fq(&fq, &fqd);
  2504. if (WARN_ON(err))
  2505. return err;
  2506. if (be16_to_cpu(fqd.fq_ctrl) & QM_FQCTRL_CGE &&
  2507. fqd.cgid == cgrid) {
  2508. pr_err("CRGID 0x%x is being used by FQID 0x%x, CGR will be leaked\n",
  2509. cgrid, fq.fqid);
  2510. return -EIO;
  2511. }
  2512. }
  2513. /* Move to the next FQID */
  2514. fq.fqid++;
  2515. } while (1);
  2516. }
  2517. int qman_release_cgrid(u32 cgrid)
  2518. {
  2519. int ret;
  2520. ret = cgr_cleanup(cgrid);
  2521. if (ret) {
  2522. pr_debug("CGRID %d leaked\n", cgrid);
  2523. return ret;
  2524. }
  2525. gen_pool_free(qm_cgralloc, cgrid | DPAA_GENALLOC_OFF, 1);
  2526. return 0;
  2527. }
  2528. EXPORT_SYMBOL(qman_release_cgrid);