qman.c 74 KB

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