qman.c 77 KB

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