qman.c 74 KB

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