gsi.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2018-2024 Linaro Ltd.
  4. */
  5. #include <linux/bits.h>
  6. #include <linux/bug.h>
  7. #include <linux/completion.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/mutex.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/types.h>
  13. #include "gsi.h"
  14. #include "gsi_private.h"
  15. #include "gsi_reg.h"
  16. #include "gsi_trans.h"
  17. #include "ipa_data.h"
  18. #include "ipa_gsi.h"
  19. #include "ipa_version.h"
  20. #include "reg.h"
  21. /**
  22. * DOC: The IPA Generic Software Interface
  23. *
  24. * The generic software interface (GSI) is an integral component of the IPA,
  25. * providing a well-defined communication layer between the AP subsystem
  26. * and the IPA core. The modem uses the GSI layer as well.
  27. *
  28. * -------- ---------
  29. * | | | |
  30. * | AP +<---. .----+ Modem |
  31. * | +--. | | .->+ |
  32. * | | | | | | | |
  33. * -------- | | | | ---------
  34. * v | v |
  35. * --+-+---+-+--
  36. * | GSI |
  37. * |-----------|
  38. * | |
  39. * | IPA |
  40. * | |
  41. * -------------
  42. *
  43. * In the above diagram, the AP and Modem represent "execution environments"
  44. * (EEs), which are independent operating environments that use the IPA for
  45. * data transfer.
  46. *
  47. * Each EE uses a set of unidirectional GSI "channels," which allow transfer
  48. * of data to or from the IPA. A channel is implemented as a ring buffer,
  49. * with a DRAM-resident array of "transfer elements" (TREs) available to
  50. * describe transfers to or from other EEs through the IPA. A transfer
  51. * element can also contain an immediate command, requesting the IPA perform
  52. * actions other than data transfer.
  53. *
  54. * Each TRE refers to a block of data--also located in DRAM. After writing
  55. * one or more TREs to a channel, the writer (either the IPA or an EE) writes
  56. * a doorbell register to inform the receiving side how many elements have
  57. * been written.
  58. *
  59. * Each channel has a GSI "event ring" associated with it. An event ring
  60. * is implemented very much like a channel ring, but is always directed from
  61. * the IPA to an EE. The IPA notifies an EE (such as the AP) about channel
  62. * events by adding an entry to the event ring associated with the channel.
  63. * The GSI then writes its doorbell for the event ring, causing the target
  64. * EE to be interrupted. Each entry in an event ring contains a pointer
  65. * to the channel TRE whose completion the event represents.
  66. *
  67. * Each TRE in a channel ring has a set of flags. One flag indicates whether
  68. * the completion of the transfer operation generates an entry (and possibly
  69. * an interrupt) in the channel's event ring. Other flags allow transfer
  70. * elements to be chained together, forming a single logical transaction.
  71. * TRE flags are used to control whether and when interrupts are generated
  72. * to signal completion of channel transfers.
  73. *
  74. * Elements in channel and event rings are completed (or consumed) strictly
  75. * in order. Completion of one entry implies the completion of all preceding
  76. * entries. A single completion interrupt can therefore communicate the
  77. * completion of many transfers.
  78. *
  79. * Note that all GSI registers are little-endian, which is the assumed
  80. * endianness of I/O space accesses. The accessor functions perform byte
  81. * swapping if needed (i.e., for a big endian CPU).
  82. */
  83. /* Delay period for interrupt moderation (in 32KHz IPA internal timer ticks) */
  84. #define GSI_EVT_RING_INT_MODT (32 * 1) /* 1ms under 32KHz clock */
  85. #define GSI_CMD_TIMEOUT 50 /* milliseconds */
  86. #define GSI_CHANNEL_STOP_RETRIES 10
  87. #define GSI_CHANNEL_MODEM_HALT_RETRIES 10
  88. #define GSI_CHANNEL_MODEM_FLOW_RETRIES 5 /* disable flow control only */
  89. #define GSI_MHI_EVENT_ID_START 10 /* 1st reserved event id */
  90. #define GSI_MHI_EVENT_ID_END 16 /* Last reserved event id */
  91. #define GSI_ISR_MAX_ITER 50 /* Detect interrupt storms */
  92. /* An entry in an event ring */
  93. struct gsi_event {
  94. __le64 xfer_ptr;
  95. __le16 len;
  96. u8 reserved1;
  97. u8 code;
  98. __le16 reserved2;
  99. u8 type;
  100. u8 chid;
  101. };
  102. /** gsi_channel_scratch_gpi - GPI protocol scratch register
  103. * @max_outstanding_tre:
  104. * Defines the maximum number of TREs allowed in a single transaction
  105. * on a channel (in bytes). This determines the amount of prefetch
  106. * performed by the hardware. We configure this to equal the size of
  107. * the TLV FIFO for the channel.
  108. * @outstanding_threshold:
  109. * Defines the threshold (in bytes) determining when the sequencer
  110. * should update the channel doorbell. We configure this to equal
  111. * the size of two TREs.
  112. */
  113. struct gsi_channel_scratch_gpi {
  114. u64 reserved1;
  115. u16 reserved2;
  116. u16 max_outstanding_tre;
  117. u16 reserved3;
  118. u16 outstanding_threshold;
  119. };
  120. /** gsi_channel_scratch - channel scratch configuration area
  121. *
  122. * The exact interpretation of this register is protocol-specific.
  123. * We only use GPI channels; see struct gsi_channel_scratch_gpi, above.
  124. */
  125. union gsi_channel_scratch {
  126. struct gsi_channel_scratch_gpi gpi;
  127. struct {
  128. u32 word1;
  129. u32 word2;
  130. u32 word3;
  131. u32 word4;
  132. } data;
  133. };
  134. /* Check things that can be validated at build time. */
  135. static void gsi_validate_build(void)
  136. {
  137. /* This is used as a divisor */
  138. BUILD_BUG_ON(!GSI_RING_ELEMENT_SIZE);
  139. /* Code assumes the size of channel and event ring element are
  140. * the same (and fixed). Make sure the size of an event ring
  141. * element is what's expected.
  142. */
  143. BUILD_BUG_ON(sizeof(struct gsi_event) != GSI_RING_ELEMENT_SIZE);
  144. /* Hardware requires a 2^n ring size. We ensure the number of
  145. * elements in an event ring is a power of 2 elsewhere; this
  146. * ensure the elements themselves meet the requirement.
  147. */
  148. BUILD_BUG_ON(!is_power_of_2(GSI_RING_ELEMENT_SIZE));
  149. }
  150. /* Return the channel id associated with a given channel */
  151. static u32 gsi_channel_id(struct gsi_channel *channel)
  152. {
  153. return channel - &channel->gsi->channel[0];
  154. }
  155. /* An initialized channel has a non-null GSI pointer */
  156. static bool gsi_channel_initialized(struct gsi_channel *channel)
  157. {
  158. return !!channel->gsi;
  159. }
  160. /* Encode the channel protocol for the CH_C_CNTXT_0 register */
  161. static u32 ch_c_cntxt_0_type_encode(enum ipa_version version,
  162. const struct reg *reg,
  163. enum gsi_channel_type type)
  164. {
  165. u32 val;
  166. val = reg_encode(reg, CHTYPE_PROTOCOL, type);
  167. if (version < IPA_VERSION_4_5 || version >= IPA_VERSION_5_0)
  168. return val;
  169. type >>= hweight32(reg_fmask(reg, CHTYPE_PROTOCOL));
  170. return val | reg_encode(reg, CHTYPE_PROTOCOL_MSB, type);
  171. }
  172. /* Update the GSI IRQ type register with the cached value */
  173. static void gsi_irq_type_update(struct gsi *gsi, u32 val)
  174. {
  175. const struct reg *reg = gsi_reg(gsi, CNTXT_TYPE_IRQ_MSK);
  176. gsi->type_enabled_bitmap = val;
  177. iowrite32(val, gsi->virt + reg_offset(reg));
  178. }
  179. static void gsi_irq_type_enable(struct gsi *gsi, enum gsi_irq_type_id type_id)
  180. {
  181. gsi_irq_type_update(gsi, gsi->type_enabled_bitmap | type_id);
  182. }
  183. static void gsi_irq_type_disable(struct gsi *gsi, enum gsi_irq_type_id type_id)
  184. {
  185. gsi_irq_type_update(gsi, gsi->type_enabled_bitmap & ~type_id);
  186. }
  187. /* Event ring commands are performed one at a time. Their completion
  188. * is signaled by the event ring control GSI interrupt type, which is
  189. * only enabled when we issue an event ring command. Only the event
  190. * ring being operated on has this interrupt enabled.
  191. */
  192. static void gsi_irq_ev_ctrl_enable(struct gsi *gsi, u32 evt_ring_id)
  193. {
  194. u32 val = BIT(evt_ring_id);
  195. const struct reg *reg;
  196. /* There's a small chance that a previous command completed
  197. * after the interrupt was disabled, so make sure we have no
  198. * pending interrupts before we enable them.
  199. */
  200. reg = gsi_reg(gsi, CNTXT_SRC_EV_CH_IRQ_CLR);
  201. iowrite32(~0, gsi->virt + reg_offset(reg));
  202. reg = gsi_reg(gsi, CNTXT_SRC_EV_CH_IRQ_MSK);
  203. iowrite32(val, gsi->virt + reg_offset(reg));
  204. gsi_irq_type_enable(gsi, GSI_EV_CTRL);
  205. }
  206. /* Disable event ring control interrupts */
  207. static void gsi_irq_ev_ctrl_disable(struct gsi *gsi)
  208. {
  209. const struct reg *reg;
  210. gsi_irq_type_disable(gsi, GSI_EV_CTRL);
  211. reg = gsi_reg(gsi, CNTXT_SRC_EV_CH_IRQ_MSK);
  212. iowrite32(0, gsi->virt + reg_offset(reg));
  213. }
  214. /* Channel commands are performed one at a time. Their completion is
  215. * signaled by the channel control GSI interrupt type, which is only
  216. * enabled when we issue a channel command. Only the channel being
  217. * operated on has this interrupt enabled.
  218. */
  219. static void gsi_irq_ch_ctrl_enable(struct gsi *gsi, u32 channel_id)
  220. {
  221. u32 val = BIT(channel_id);
  222. const struct reg *reg;
  223. /* There's a small chance that a previous command completed
  224. * after the interrupt was disabled, so make sure we have no
  225. * pending interrupts before we enable them.
  226. */
  227. reg = gsi_reg(gsi, CNTXT_SRC_CH_IRQ_CLR);
  228. iowrite32(~0, gsi->virt + reg_offset(reg));
  229. reg = gsi_reg(gsi, CNTXT_SRC_CH_IRQ_MSK);
  230. iowrite32(val, gsi->virt + reg_offset(reg));
  231. gsi_irq_type_enable(gsi, GSI_CH_CTRL);
  232. }
  233. /* Disable channel control interrupts */
  234. static void gsi_irq_ch_ctrl_disable(struct gsi *gsi)
  235. {
  236. const struct reg *reg;
  237. gsi_irq_type_disable(gsi, GSI_CH_CTRL);
  238. reg = gsi_reg(gsi, CNTXT_SRC_CH_IRQ_MSK);
  239. iowrite32(0, gsi->virt + reg_offset(reg));
  240. }
  241. static void gsi_irq_ieob_enable_one(struct gsi *gsi, u32 evt_ring_id)
  242. {
  243. bool enable_ieob = !gsi->ieob_enabled_bitmap;
  244. const struct reg *reg;
  245. u32 val;
  246. gsi->ieob_enabled_bitmap |= BIT(evt_ring_id);
  247. reg = gsi_reg(gsi, CNTXT_SRC_IEOB_IRQ_MSK);
  248. val = gsi->ieob_enabled_bitmap;
  249. iowrite32(val, gsi->virt + reg_offset(reg));
  250. /* Enable the interrupt type if this is the first channel enabled */
  251. if (enable_ieob)
  252. gsi_irq_type_enable(gsi, GSI_IEOB);
  253. }
  254. static void gsi_irq_ieob_disable(struct gsi *gsi, u32 event_mask)
  255. {
  256. const struct reg *reg;
  257. u32 val;
  258. gsi->ieob_enabled_bitmap &= ~event_mask;
  259. /* Disable the interrupt type if this was the last enabled channel */
  260. if (!gsi->ieob_enabled_bitmap)
  261. gsi_irq_type_disable(gsi, GSI_IEOB);
  262. reg = gsi_reg(gsi, CNTXT_SRC_IEOB_IRQ_MSK);
  263. val = gsi->ieob_enabled_bitmap;
  264. iowrite32(val, gsi->virt + reg_offset(reg));
  265. }
  266. static void gsi_irq_ieob_disable_one(struct gsi *gsi, u32 evt_ring_id)
  267. {
  268. gsi_irq_ieob_disable(gsi, BIT(evt_ring_id));
  269. }
  270. /* Enable all GSI_interrupt types */
  271. static void gsi_irq_enable(struct gsi *gsi)
  272. {
  273. const struct reg *reg;
  274. u32 val;
  275. /* Global interrupts include hardware error reports. Enable
  276. * that so we can at least report the error should it occur.
  277. */
  278. reg = gsi_reg(gsi, CNTXT_GLOB_IRQ_EN);
  279. iowrite32(ERROR_INT, gsi->virt + reg_offset(reg));
  280. gsi_irq_type_update(gsi, gsi->type_enabled_bitmap | GSI_GLOB_EE);
  281. /* General GSI interrupts are reported to all EEs; if they occur
  282. * they are unrecoverable (without reset). A breakpoint interrupt
  283. * also exists, but we don't support that. We want to be notified
  284. * of errors so we can report them, even if they can't be handled.
  285. */
  286. reg = gsi_reg(gsi, CNTXT_GSI_IRQ_EN);
  287. val = BUS_ERROR;
  288. val |= CMD_FIFO_OVRFLOW;
  289. val |= MCS_STACK_OVRFLOW;
  290. iowrite32(val, gsi->virt + reg_offset(reg));
  291. gsi_irq_type_update(gsi, gsi->type_enabled_bitmap | GSI_GENERAL);
  292. }
  293. /* Disable all GSI interrupt types */
  294. static void gsi_irq_disable(struct gsi *gsi)
  295. {
  296. const struct reg *reg;
  297. gsi_irq_type_update(gsi, 0);
  298. /* Clear the type-specific interrupt masks set by gsi_irq_enable() */
  299. reg = gsi_reg(gsi, CNTXT_GSI_IRQ_EN);
  300. iowrite32(0, gsi->virt + reg_offset(reg));
  301. reg = gsi_reg(gsi, CNTXT_GLOB_IRQ_EN);
  302. iowrite32(0, gsi->virt + reg_offset(reg));
  303. }
  304. /* Return the virtual address associated with a ring index */
  305. void *gsi_ring_virt(struct gsi_ring *ring, u32 index)
  306. {
  307. /* Note: index *must* be used modulo the ring count here */
  308. return ring->virt + (index % ring->count) * GSI_RING_ELEMENT_SIZE;
  309. }
  310. /* Return the 32-bit DMA address associated with a ring index */
  311. static u32 gsi_ring_addr(struct gsi_ring *ring, u32 index)
  312. {
  313. return lower_32_bits(ring->addr) + index * GSI_RING_ELEMENT_SIZE;
  314. }
  315. /* Return the ring index of a 32-bit ring offset */
  316. static u32 gsi_ring_index(struct gsi_ring *ring, u32 offset)
  317. {
  318. return (offset - gsi_ring_addr(ring, 0)) / GSI_RING_ELEMENT_SIZE;
  319. }
  320. /* Issue a GSI command by writing a value to a register, then wait for
  321. * completion to be signaled. Returns true if the command completes
  322. * or false if it times out.
  323. */
  324. static bool gsi_command(struct gsi *gsi, u32 reg, u32 val)
  325. {
  326. unsigned long timeout = msecs_to_jiffies(GSI_CMD_TIMEOUT);
  327. struct completion *completion = &gsi->completion;
  328. reinit_completion(completion);
  329. iowrite32(val, gsi->virt + reg);
  330. return !!wait_for_completion_timeout(completion, timeout);
  331. }
  332. /* Return the hardware's notion of the current state of an event ring */
  333. static enum gsi_evt_ring_state
  334. gsi_evt_ring_state(struct gsi *gsi, u32 evt_ring_id)
  335. {
  336. const struct reg *reg = gsi_reg(gsi, EV_CH_E_CNTXT_0);
  337. u32 val;
  338. val = ioread32(gsi->virt + reg_n_offset(reg, evt_ring_id));
  339. return reg_decode(reg, EV_CHSTATE, val);
  340. }
  341. /* Issue an event ring command and wait for it to complete */
  342. static void gsi_evt_ring_command(struct gsi *gsi, u32 evt_ring_id,
  343. enum gsi_evt_cmd_opcode opcode)
  344. {
  345. struct device *dev = gsi->dev;
  346. const struct reg *reg;
  347. bool timeout;
  348. u32 val;
  349. /* Enable the completion interrupt for the command */
  350. gsi_irq_ev_ctrl_enable(gsi, evt_ring_id);
  351. reg = gsi_reg(gsi, EV_CH_CMD);
  352. val = reg_encode(reg, EV_CHID, evt_ring_id);
  353. val |= reg_encode(reg, EV_OPCODE, opcode);
  354. timeout = !gsi_command(gsi, reg_offset(reg), val);
  355. gsi_irq_ev_ctrl_disable(gsi);
  356. if (!timeout)
  357. return;
  358. dev_err(dev, "GSI command %u for event ring %u timed out, state %u\n",
  359. opcode, evt_ring_id, gsi_evt_ring_state(gsi, evt_ring_id));
  360. }
  361. /* Allocate an event ring in NOT_ALLOCATED state */
  362. static int gsi_evt_ring_alloc_command(struct gsi *gsi, u32 evt_ring_id)
  363. {
  364. enum gsi_evt_ring_state state;
  365. /* Get initial event ring state */
  366. state = gsi_evt_ring_state(gsi, evt_ring_id);
  367. if (state != GSI_EVT_RING_STATE_NOT_ALLOCATED) {
  368. dev_err(gsi->dev, "event ring %u bad state %u before alloc\n",
  369. evt_ring_id, state);
  370. return -EINVAL;
  371. }
  372. gsi_evt_ring_command(gsi, evt_ring_id, GSI_EVT_ALLOCATE);
  373. /* If successful the event ring state will have changed */
  374. state = gsi_evt_ring_state(gsi, evt_ring_id);
  375. if (state == GSI_EVT_RING_STATE_ALLOCATED)
  376. return 0;
  377. dev_err(gsi->dev, "event ring %u bad state %u after alloc\n",
  378. evt_ring_id, state);
  379. return -EIO;
  380. }
  381. /* Reset a GSI event ring in ALLOCATED or ERROR state. */
  382. static void gsi_evt_ring_reset_command(struct gsi *gsi, u32 evt_ring_id)
  383. {
  384. enum gsi_evt_ring_state state;
  385. state = gsi_evt_ring_state(gsi, evt_ring_id);
  386. if (state != GSI_EVT_RING_STATE_ALLOCATED &&
  387. state != GSI_EVT_RING_STATE_ERROR) {
  388. dev_err(gsi->dev, "event ring %u bad state %u before reset\n",
  389. evt_ring_id, state);
  390. return;
  391. }
  392. gsi_evt_ring_command(gsi, evt_ring_id, GSI_EVT_RESET);
  393. /* If successful the event ring state will have changed */
  394. state = gsi_evt_ring_state(gsi, evt_ring_id);
  395. if (state == GSI_EVT_RING_STATE_ALLOCATED)
  396. return;
  397. dev_err(gsi->dev, "event ring %u bad state %u after reset\n",
  398. evt_ring_id, state);
  399. }
  400. /* Issue a hardware de-allocation request for an allocated event ring */
  401. static void gsi_evt_ring_de_alloc_command(struct gsi *gsi, u32 evt_ring_id)
  402. {
  403. enum gsi_evt_ring_state state;
  404. state = gsi_evt_ring_state(gsi, evt_ring_id);
  405. if (state != GSI_EVT_RING_STATE_ALLOCATED) {
  406. dev_err(gsi->dev, "event ring %u state %u before dealloc\n",
  407. evt_ring_id, state);
  408. return;
  409. }
  410. gsi_evt_ring_command(gsi, evt_ring_id, GSI_EVT_DE_ALLOC);
  411. /* If successful the event ring state will have changed */
  412. state = gsi_evt_ring_state(gsi, evt_ring_id);
  413. if (state == GSI_EVT_RING_STATE_NOT_ALLOCATED)
  414. return;
  415. dev_err(gsi->dev, "event ring %u bad state %u after dealloc\n",
  416. evt_ring_id, state);
  417. }
  418. /* Fetch the current state of a channel from hardware */
  419. static enum gsi_channel_state gsi_channel_state(struct gsi_channel *channel)
  420. {
  421. const struct reg *reg = gsi_reg(channel->gsi, CH_C_CNTXT_0);
  422. u32 channel_id = gsi_channel_id(channel);
  423. struct gsi *gsi = channel->gsi;
  424. void __iomem *virt = gsi->virt;
  425. u32 val;
  426. reg = gsi_reg(gsi, CH_C_CNTXT_0);
  427. val = ioread32(virt + reg_n_offset(reg, channel_id));
  428. return reg_decode(reg, CHSTATE, val);
  429. }
  430. /* Issue a channel command and wait for it to complete */
  431. static void
  432. gsi_channel_command(struct gsi_channel *channel, enum gsi_ch_cmd_opcode opcode)
  433. {
  434. u32 channel_id = gsi_channel_id(channel);
  435. struct gsi *gsi = channel->gsi;
  436. struct device *dev = gsi->dev;
  437. const struct reg *reg;
  438. bool timeout;
  439. u32 val;
  440. /* Enable the completion interrupt for the command */
  441. gsi_irq_ch_ctrl_enable(gsi, channel_id);
  442. reg = gsi_reg(gsi, CH_CMD);
  443. val = reg_encode(reg, CH_CHID, channel_id);
  444. val |= reg_encode(reg, CH_OPCODE, opcode);
  445. timeout = !gsi_command(gsi, reg_offset(reg), val);
  446. gsi_irq_ch_ctrl_disable(gsi);
  447. if (!timeout)
  448. return;
  449. dev_err(dev, "GSI command %u for channel %u timed out, state %u\n",
  450. opcode, channel_id, gsi_channel_state(channel));
  451. }
  452. /* Allocate GSI channel in NOT_ALLOCATED state */
  453. static int gsi_channel_alloc_command(struct gsi *gsi, u32 channel_id)
  454. {
  455. struct gsi_channel *channel = &gsi->channel[channel_id];
  456. struct device *dev = gsi->dev;
  457. enum gsi_channel_state state;
  458. /* Get initial channel state */
  459. state = gsi_channel_state(channel);
  460. if (state != GSI_CHANNEL_STATE_NOT_ALLOCATED) {
  461. dev_err(dev, "channel %u bad state %u before alloc\n",
  462. channel_id, state);
  463. return -EINVAL;
  464. }
  465. gsi_channel_command(channel, GSI_CH_ALLOCATE);
  466. /* If successful the channel state will have changed */
  467. state = gsi_channel_state(channel);
  468. if (state == GSI_CHANNEL_STATE_ALLOCATED)
  469. return 0;
  470. dev_err(dev, "channel %u bad state %u after alloc\n",
  471. channel_id, state);
  472. return -EIO;
  473. }
  474. /* Start an ALLOCATED channel */
  475. static int gsi_channel_start_command(struct gsi_channel *channel)
  476. {
  477. struct device *dev = channel->gsi->dev;
  478. enum gsi_channel_state state;
  479. state = gsi_channel_state(channel);
  480. if (state != GSI_CHANNEL_STATE_ALLOCATED &&
  481. state != GSI_CHANNEL_STATE_STOPPED) {
  482. dev_err(dev, "channel %u bad state %u before start\n",
  483. gsi_channel_id(channel), state);
  484. return -EINVAL;
  485. }
  486. gsi_channel_command(channel, GSI_CH_START);
  487. /* If successful the channel state will have changed */
  488. state = gsi_channel_state(channel);
  489. if (state == GSI_CHANNEL_STATE_STARTED)
  490. return 0;
  491. dev_err(dev, "channel %u bad state %u after start\n",
  492. gsi_channel_id(channel), state);
  493. return -EIO;
  494. }
  495. /* Stop a GSI channel in STARTED state */
  496. static int gsi_channel_stop_command(struct gsi_channel *channel)
  497. {
  498. struct device *dev = channel->gsi->dev;
  499. enum gsi_channel_state state;
  500. state = gsi_channel_state(channel);
  501. /* Channel could have entered STOPPED state since last call
  502. * if it timed out. If so, we're done.
  503. */
  504. if (state == GSI_CHANNEL_STATE_STOPPED)
  505. return 0;
  506. if (state != GSI_CHANNEL_STATE_STARTED &&
  507. state != GSI_CHANNEL_STATE_STOP_IN_PROC) {
  508. dev_err(dev, "channel %u bad state %u before stop\n",
  509. gsi_channel_id(channel), state);
  510. return -EINVAL;
  511. }
  512. gsi_channel_command(channel, GSI_CH_STOP);
  513. /* If successful the channel state will have changed */
  514. state = gsi_channel_state(channel);
  515. if (state == GSI_CHANNEL_STATE_STOPPED)
  516. return 0;
  517. /* We may have to try again if stop is in progress */
  518. if (state == GSI_CHANNEL_STATE_STOP_IN_PROC)
  519. return -EAGAIN;
  520. dev_err(dev, "channel %u bad state %u after stop\n",
  521. gsi_channel_id(channel), state);
  522. return -EIO;
  523. }
  524. /* Reset a GSI channel in ALLOCATED or ERROR state. */
  525. static void gsi_channel_reset_command(struct gsi_channel *channel)
  526. {
  527. struct device *dev = channel->gsi->dev;
  528. enum gsi_channel_state state;
  529. /* A short delay is required before a RESET command */
  530. usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC);
  531. state = gsi_channel_state(channel);
  532. if (state != GSI_CHANNEL_STATE_STOPPED &&
  533. state != GSI_CHANNEL_STATE_ERROR) {
  534. /* No need to reset a channel already in ALLOCATED state */
  535. if (state != GSI_CHANNEL_STATE_ALLOCATED)
  536. dev_err(dev, "channel %u bad state %u before reset\n",
  537. gsi_channel_id(channel), state);
  538. return;
  539. }
  540. gsi_channel_command(channel, GSI_CH_RESET);
  541. /* If successful the channel state will have changed */
  542. state = gsi_channel_state(channel);
  543. if (state != GSI_CHANNEL_STATE_ALLOCATED)
  544. dev_err(dev, "channel %u bad state %u after reset\n",
  545. gsi_channel_id(channel), state);
  546. }
  547. /* Deallocate an ALLOCATED GSI channel */
  548. static void gsi_channel_de_alloc_command(struct gsi *gsi, u32 channel_id)
  549. {
  550. struct gsi_channel *channel = &gsi->channel[channel_id];
  551. struct device *dev = gsi->dev;
  552. enum gsi_channel_state state;
  553. state = gsi_channel_state(channel);
  554. if (state != GSI_CHANNEL_STATE_ALLOCATED) {
  555. dev_err(dev, "channel %u bad state %u before dealloc\n",
  556. channel_id, state);
  557. return;
  558. }
  559. gsi_channel_command(channel, GSI_CH_DE_ALLOC);
  560. /* If successful the channel state will have changed */
  561. state = gsi_channel_state(channel);
  562. if (state != GSI_CHANNEL_STATE_NOT_ALLOCATED)
  563. dev_err(dev, "channel %u bad state %u after dealloc\n",
  564. channel_id, state);
  565. }
  566. /* Ring an event ring doorbell, reporting the last entry processed by the AP.
  567. * The index argument (modulo the ring count) is the first unfilled entry, so
  568. * we supply one less than that with the doorbell. Update the event ring
  569. * index field with the value provided.
  570. */
  571. static void gsi_evt_ring_doorbell(struct gsi *gsi, u32 evt_ring_id, u32 index)
  572. {
  573. const struct reg *reg = gsi_reg(gsi, EV_CH_E_DOORBELL_0);
  574. struct gsi_ring *ring = &gsi->evt_ring[evt_ring_id].ring;
  575. u32 val;
  576. ring->index = index; /* Next unused entry */
  577. /* Note: index *must* be used modulo the ring count here */
  578. val = gsi_ring_addr(ring, (index - 1) % ring->count);
  579. iowrite32(val, gsi->virt + reg_n_offset(reg, evt_ring_id));
  580. }
  581. /* Program an event ring for use */
  582. static void gsi_evt_ring_program(struct gsi *gsi, u32 evt_ring_id)
  583. {
  584. struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
  585. struct gsi_ring *ring = &evt_ring->ring;
  586. const struct reg *reg;
  587. u32 val;
  588. reg = gsi_reg(gsi, EV_CH_E_CNTXT_0);
  589. /* We program all event rings as GPI type/protocol */
  590. val = reg_encode(reg, EV_CHTYPE, GSI_CHANNEL_TYPE_GPI);
  591. /* EV_EE field is 0 (GSI_EE_AP) */
  592. val |= reg_bit(reg, EV_INTYPE);
  593. val |= reg_encode(reg, EV_ELEMENT_SIZE, GSI_RING_ELEMENT_SIZE);
  594. iowrite32(val, gsi->virt + reg_n_offset(reg, evt_ring_id));
  595. reg = gsi_reg(gsi, EV_CH_E_CNTXT_1);
  596. val = reg_encode(reg, R_LENGTH, ring->count * GSI_RING_ELEMENT_SIZE);
  597. iowrite32(val, gsi->virt + reg_n_offset(reg, evt_ring_id));
  598. /* The context 2 and 3 registers store the low-order and
  599. * high-order 32 bits of the address of the event ring,
  600. * respectively.
  601. */
  602. reg = gsi_reg(gsi, EV_CH_E_CNTXT_2);
  603. val = lower_32_bits(ring->addr);
  604. iowrite32(val, gsi->virt + reg_n_offset(reg, evt_ring_id));
  605. reg = gsi_reg(gsi, EV_CH_E_CNTXT_3);
  606. val = upper_32_bits(ring->addr);
  607. iowrite32(val, gsi->virt + reg_n_offset(reg, evt_ring_id));
  608. /* Enable interrupt moderation by setting the moderation delay */
  609. reg = gsi_reg(gsi, EV_CH_E_CNTXT_8);
  610. val = reg_encode(reg, EV_MODT, GSI_EVT_RING_INT_MODT);
  611. val |= reg_encode(reg, EV_MODC, 1); /* comes from channel */
  612. /* EV_MOD_CNT is 0 (no counter-based interrupt coalescing) */
  613. iowrite32(val, gsi->virt + reg_n_offset(reg, evt_ring_id));
  614. /* No MSI write data, and MSI high and low address is 0 */
  615. reg = gsi_reg(gsi, EV_CH_E_CNTXT_9);
  616. iowrite32(0, gsi->virt + reg_n_offset(reg, evt_ring_id));
  617. reg = gsi_reg(gsi, EV_CH_E_CNTXT_10);
  618. iowrite32(0, gsi->virt + reg_n_offset(reg, evt_ring_id));
  619. reg = gsi_reg(gsi, EV_CH_E_CNTXT_11);
  620. iowrite32(0, gsi->virt + reg_n_offset(reg, evt_ring_id));
  621. /* We don't need to get event read pointer updates */
  622. reg = gsi_reg(gsi, EV_CH_E_CNTXT_12);
  623. iowrite32(0, gsi->virt + reg_n_offset(reg, evt_ring_id));
  624. reg = gsi_reg(gsi, EV_CH_E_CNTXT_13);
  625. iowrite32(0, gsi->virt + reg_n_offset(reg, evt_ring_id));
  626. /* Finally, tell the hardware our "last processed" event (arbitrary) */
  627. gsi_evt_ring_doorbell(gsi, evt_ring_id, ring->index);
  628. }
  629. /* Find the transaction whose completion indicates a channel is quiesced */
  630. static struct gsi_trans *gsi_channel_trans_last(struct gsi_channel *channel)
  631. {
  632. struct gsi_trans_info *trans_info = &channel->trans_info;
  633. u32 pending_id = trans_info->pending_id;
  634. struct gsi_trans *trans;
  635. u16 trans_id;
  636. if (channel->toward_ipa && pending_id != trans_info->free_id) {
  637. /* There is a small chance a TX transaction got allocated
  638. * just before we disabled transmits, so check for that.
  639. * The last allocated, committed, or pending transaction
  640. * precedes the first free transaction.
  641. */
  642. trans_id = trans_info->free_id - 1;
  643. } else if (trans_info->polled_id != pending_id) {
  644. /* Otherwise (TX or RX) we want to wait for anything that
  645. * has completed, or has been polled but not released yet.
  646. *
  647. * The last completed or polled transaction precedes the
  648. * first pending transaction.
  649. */
  650. trans_id = pending_id - 1;
  651. } else {
  652. return NULL;
  653. }
  654. /* Caller will wait for this, so take a reference */
  655. trans = &trans_info->trans[trans_id % channel->tre_count];
  656. refcount_inc(&trans->refcount);
  657. return trans;
  658. }
  659. /* Wait for transaction activity on a channel to complete */
  660. static void gsi_channel_trans_quiesce(struct gsi_channel *channel)
  661. {
  662. struct gsi_trans *trans;
  663. /* Get the last transaction, and wait for it to complete */
  664. trans = gsi_channel_trans_last(channel);
  665. if (trans) {
  666. wait_for_completion(&trans->completion);
  667. gsi_trans_free(trans);
  668. }
  669. }
  670. /* Program a channel for use; there is no gsi_channel_deprogram() */
  671. static void gsi_channel_program(struct gsi_channel *channel, bool doorbell)
  672. {
  673. size_t size = channel->tre_ring.count * GSI_RING_ELEMENT_SIZE;
  674. u32 channel_id = gsi_channel_id(channel);
  675. union gsi_channel_scratch scr = { };
  676. struct gsi_channel_scratch_gpi *gpi;
  677. struct gsi *gsi = channel->gsi;
  678. const struct reg *reg;
  679. u32 wrr_weight = 0;
  680. u32 offset;
  681. u32 val;
  682. reg = gsi_reg(gsi, CH_C_CNTXT_0);
  683. /* We program all channels as GPI type/protocol */
  684. val = ch_c_cntxt_0_type_encode(gsi->version, reg, GSI_CHANNEL_TYPE_GPI);
  685. if (channel->toward_ipa)
  686. val |= reg_bit(reg, CHTYPE_DIR);
  687. if (gsi->version < IPA_VERSION_5_0)
  688. val |= reg_encode(reg, ERINDEX, channel->evt_ring_id);
  689. val |= reg_encode(reg, ELEMENT_SIZE, GSI_RING_ELEMENT_SIZE);
  690. iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
  691. reg = gsi_reg(gsi, CH_C_CNTXT_1);
  692. val = reg_encode(reg, CH_R_LENGTH, size);
  693. if (gsi->version >= IPA_VERSION_5_0)
  694. val |= reg_encode(reg, CH_ERINDEX, channel->evt_ring_id);
  695. iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
  696. /* The context 2 and 3 registers store the low-order and
  697. * high-order 32 bits of the address of the channel ring,
  698. * respectively.
  699. */
  700. reg = gsi_reg(gsi, CH_C_CNTXT_2);
  701. val = lower_32_bits(channel->tre_ring.addr);
  702. iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
  703. reg = gsi_reg(gsi, CH_C_CNTXT_3);
  704. val = upper_32_bits(channel->tre_ring.addr);
  705. iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
  706. reg = gsi_reg(gsi, CH_C_QOS);
  707. /* Command channel gets low weighted round-robin priority */
  708. if (channel->command)
  709. wrr_weight = reg_field_max(reg, WRR_WEIGHT);
  710. val = reg_encode(reg, WRR_WEIGHT, wrr_weight);
  711. /* Max prefetch is 1 segment (do not set MAX_PREFETCH_FMASK) */
  712. /* No need to use the doorbell engine starting at IPA v4.0 */
  713. if (gsi->version < IPA_VERSION_4_0 && doorbell)
  714. val |= reg_bit(reg, USE_DB_ENG);
  715. /* v4.0 introduces an escape buffer for prefetch. We use it
  716. * on all but the AP command channel.
  717. */
  718. if (gsi->version >= IPA_VERSION_4_0 && !channel->command) {
  719. /* If not otherwise set, prefetch buffers are used */
  720. if (gsi->version < IPA_VERSION_4_5)
  721. val |= reg_bit(reg, USE_ESCAPE_BUF_ONLY);
  722. else
  723. val |= reg_encode(reg, PREFETCH_MODE, ESCAPE_BUF_ONLY);
  724. }
  725. /* All channels set DB_IN_BYTES */
  726. if (gsi->version >= IPA_VERSION_4_9)
  727. val |= reg_bit(reg, DB_IN_BYTES);
  728. iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
  729. /* Now update the scratch registers for GPI protocol */
  730. gpi = &scr.gpi;
  731. gpi->max_outstanding_tre = channel->trans_tre_max *
  732. GSI_RING_ELEMENT_SIZE;
  733. gpi->outstanding_threshold = 2 * GSI_RING_ELEMENT_SIZE;
  734. reg = gsi_reg(gsi, CH_C_SCRATCH_0);
  735. val = scr.data.word1;
  736. iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
  737. reg = gsi_reg(gsi, CH_C_SCRATCH_1);
  738. val = scr.data.word2;
  739. iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
  740. reg = gsi_reg(gsi, CH_C_SCRATCH_2);
  741. val = scr.data.word3;
  742. iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
  743. /* We must preserve the upper 16 bits of the last scratch register.
  744. * The next sequence assumes those bits remain unchanged between the
  745. * read and the write.
  746. */
  747. reg = gsi_reg(gsi, CH_C_SCRATCH_3);
  748. offset = reg_n_offset(reg, channel_id);
  749. val = ioread32(gsi->virt + offset);
  750. val = (scr.data.word4 & GENMASK(31, 16)) | (val & GENMASK(15, 0));
  751. iowrite32(val, gsi->virt + offset);
  752. /* All done! */
  753. }
  754. static int __gsi_channel_start(struct gsi_channel *channel, bool resume)
  755. {
  756. struct gsi *gsi = channel->gsi;
  757. int ret;
  758. /* Prior to IPA v4.0 suspend/resume is not implemented by GSI */
  759. if (resume && gsi->version < IPA_VERSION_4_0)
  760. return 0;
  761. mutex_lock(&gsi->mutex);
  762. ret = gsi_channel_start_command(channel);
  763. mutex_unlock(&gsi->mutex);
  764. return ret;
  765. }
  766. /* Start an allocated GSI channel */
  767. int gsi_channel_start(struct gsi *gsi, u32 channel_id)
  768. {
  769. struct gsi_channel *channel = &gsi->channel[channel_id];
  770. int ret;
  771. /* Enable NAPI and the completion interrupt */
  772. napi_enable(&channel->napi);
  773. gsi_irq_ieob_enable_one(gsi, channel->evt_ring_id);
  774. ret = __gsi_channel_start(channel, false);
  775. if (ret) {
  776. gsi_irq_ieob_disable_one(gsi, channel->evt_ring_id);
  777. napi_disable(&channel->napi);
  778. }
  779. return ret;
  780. }
  781. static int gsi_channel_stop_retry(struct gsi_channel *channel)
  782. {
  783. u32 retries = GSI_CHANNEL_STOP_RETRIES;
  784. int ret;
  785. do {
  786. ret = gsi_channel_stop_command(channel);
  787. if (ret != -EAGAIN)
  788. break;
  789. usleep_range(3 * USEC_PER_MSEC, 5 * USEC_PER_MSEC);
  790. } while (retries--);
  791. return ret;
  792. }
  793. static int __gsi_channel_stop(struct gsi_channel *channel, bool suspend)
  794. {
  795. struct gsi *gsi = channel->gsi;
  796. int ret;
  797. /* Wait for any underway transactions to complete before stopping. */
  798. gsi_channel_trans_quiesce(channel);
  799. /* Prior to IPA v4.0 suspend/resume is not implemented by GSI */
  800. if (suspend && gsi->version < IPA_VERSION_4_0)
  801. return 0;
  802. mutex_lock(&gsi->mutex);
  803. ret = gsi_channel_stop_retry(channel);
  804. mutex_unlock(&gsi->mutex);
  805. return ret;
  806. }
  807. /* Stop a started channel */
  808. int gsi_channel_stop(struct gsi *gsi, u32 channel_id)
  809. {
  810. struct gsi_channel *channel = &gsi->channel[channel_id];
  811. int ret;
  812. ret = __gsi_channel_stop(channel, false);
  813. if (ret)
  814. return ret;
  815. /* Disable the completion interrupt and NAPI if successful */
  816. gsi_irq_ieob_disable_one(gsi, channel->evt_ring_id);
  817. napi_disable(&channel->napi);
  818. return 0;
  819. }
  820. /* Reset and reconfigure a channel, (possibly) enabling the doorbell engine */
  821. void gsi_channel_reset(struct gsi *gsi, u32 channel_id, bool doorbell)
  822. {
  823. struct gsi_channel *channel = &gsi->channel[channel_id];
  824. mutex_lock(&gsi->mutex);
  825. gsi_channel_reset_command(channel);
  826. /* Due to a hardware quirk we may need to reset RX channels twice. */
  827. if (gsi->version < IPA_VERSION_4_0 && !channel->toward_ipa)
  828. gsi_channel_reset_command(channel);
  829. /* Hardware assumes this is 0 following reset */
  830. channel->tre_ring.index = 0;
  831. gsi_channel_program(channel, doorbell);
  832. gsi_channel_trans_cancel_pending(channel);
  833. mutex_unlock(&gsi->mutex);
  834. }
  835. /* Stop a started channel for suspend */
  836. int gsi_channel_suspend(struct gsi *gsi, u32 channel_id)
  837. {
  838. struct gsi_channel *channel = &gsi->channel[channel_id];
  839. int ret;
  840. ret = __gsi_channel_stop(channel, true);
  841. if (ret)
  842. return ret;
  843. /* Ensure NAPI polling has finished. */
  844. napi_synchronize(&channel->napi);
  845. return 0;
  846. }
  847. /* Resume a suspended channel (starting if stopped) */
  848. int gsi_channel_resume(struct gsi *gsi, u32 channel_id)
  849. {
  850. struct gsi_channel *channel = &gsi->channel[channel_id];
  851. return __gsi_channel_start(channel, true);
  852. }
  853. /* Prevent all GSI interrupts while suspended */
  854. void gsi_suspend(struct gsi *gsi)
  855. {
  856. disable_irq(gsi->irq);
  857. }
  858. /* Allow all GSI interrupts again when resuming */
  859. void gsi_resume(struct gsi *gsi)
  860. {
  861. enable_irq(gsi->irq);
  862. }
  863. void gsi_trans_tx_committed(struct gsi_trans *trans)
  864. {
  865. struct gsi_channel *channel = &trans->gsi->channel[trans->channel_id];
  866. channel->trans_count++;
  867. channel->byte_count += trans->len;
  868. trans->trans_count = channel->trans_count;
  869. trans->byte_count = channel->byte_count;
  870. }
  871. void gsi_trans_tx_queued(struct gsi_trans *trans)
  872. {
  873. u32 channel_id = trans->channel_id;
  874. struct gsi *gsi = trans->gsi;
  875. struct gsi_channel *channel;
  876. u32 trans_count;
  877. u32 byte_count;
  878. channel = &gsi->channel[channel_id];
  879. byte_count = channel->byte_count - channel->queued_byte_count;
  880. trans_count = channel->trans_count - channel->queued_trans_count;
  881. channel->queued_byte_count = channel->byte_count;
  882. channel->queued_trans_count = channel->trans_count;
  883. ipa_gsi_channel_tx_queued(gsi, channel_id, trans_count, byte_count);
  884. }
  885. /**
  886. * gsi_trans_tx_completed() - Report completed TX transactions
  887. * @trans: TX channel transaction that has completed
  888. *
  889. * Report that a transaction on a TX channel has completed. At the time a
  890. * transaction is committed, we record *in the transaction* its channel's
  891. * committed transaction and byte counts. Transactions are completed in
  892. * order, and the difference between the channel's byte/transaction count
  893. * when the transaction was committed and when it completes tells us
  894. * exactly how much data has been transferred while the transaction was
  895. * pending.
  896. *
  897. * We report this information to the network stack, which uses it to manage
  898. * the rate at which data is sent to hardware.
  899. */
  900. static void gsi_trans_tx_completed(struct gsi_trans *trans)
  901. {
  902. u32 channel_id = trans->channel_id;
  903. struct gsi *gsi = trans->gsi;
  904. struct gsi_channel *channel;
  905. u32 trans_count;
  906. u32 byte_count;
  907. channel = &gsi->channel[channel_id];
  908. trans_count = trans->trans_count - channel->compl_trans_count;
  909. byte_count = trans->byte_count - channel->compl_byte_count;
  910. channel->compl_trans_count += trans_count;
  911. channel->compl_byte_count += byte_count;
  912. ipa_gsi_channel_tx_completed(gsi, channel_id, trans_count, byte_count);
  913. }
  914. /* Channel control interrupt handler */
  915. static void gsi_isr_chan_ctrl(struct gsi *gsi)
  916. {
  917. const struct reg *reg;
  918. u32 channel_mask;
  919. reg = gsi_reg(gsi, CNTXT_SRC_CH_IRQ);
  920. channel_mask = ioread32(gsi->virt + reg_offset(reg));
  921. reg = gsi_reg(gsi, CNTXT_SRC_CH_IRQ_CLR);
  922. iowrite32(channel_mask, gsi->virt + reg_offset(reg));
  923. while (channel_mask) {
  924. u32 channel_id = __ffs(channel_mask);
  925. channel_mask ^= BIT(channel_id);
  926. complete(&gsi->completion);
  927. }
  928. }
  929. /* Event ring control interrupt handler */
  930. static void gsi_isr_evt_ctrl(struct gsi *gsi)
  931. {
  932. const struct reg *reg;
  933. u32 event_mask;
  934. reg = gsi_reg(gsi, CNTXT_SRC_EV_CH_IRQ);
  935. event_mask = ioread32(gsi->virt + reg_offset(reg));
  936. reg = gsi_reg(gsi, CNTXT_SRC_EV_CH_IRQ_CLR);
  937. iowrite32(event_mask, gsi->virt + reg_offset(reg));
  938. while (event_mask) {
  939. u32 evt_ring_id = __ffs(event_mask);
  940. event_mask ^= BIT(evt_ring_id);
  941. complete(&gsi->completion);
  942. }
  943. }
  944. /* Global channel error interrupt handler */
  945. static void
  946. gsi_isr_glob_chan_err(struct gsi *gsi, u32 err_ee, u32 channel_id, u32 code)
  947. {
  948. if (code == GSI_OUT_OF_RESOURCES) {
  949. dev_err(gsi->dev, "channel %u out of resources\n", channel_id);
  950. complete(&gsi->completion);
  951. return;
  952. }
  953. /* Report, but otherwise ignore all other error codes */
  954. dev_err(gsi->dev, "channel %u global error ee 0x%08x code 0x%08x\n",
  955. channel_id, err_ee, code);
  956. }
  957. /* Global event error interrupt handler */
  958. static void
  959. gsi_isr_glob_evt_err(struct gsi *gsi, u32 err_ee, u32 evt_ring_id, u32 code)
  960. {
  961. if (code == GSI_OUT_OF_RESOURCES) {
  962. struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
  963. u32 channel_id = gsi_channel_id(evt_ring->channel);
  964. complete(&gsi->completion);
  965. dev_err(gsi->dev, "evt_ring for channel %u out of resources\n",
  966. channel_id);
  967. return;
  968. }
  969. /* Report, but otherwise ignore all other error codes */
  970. dev_err(gsi->dev, "event ring %u global error ee %u code 0x%08x\n",
  971. evt_ring_id, err_ee, code);
  972. }
  973. /* Global error interrupt handler */
  974. static void gsi_isr_glob_err(struct gsi *gsi)
  975. {
  976. const struct reg *log_reg;
  977. const struct reg *clr_reg;
  978. enum gsi_err_type type;
  979. enum gsi_err_code code;
  980. u32 offset;
  981. u32 which;
  982. u32 val;
  983. u32 ee;
  984. /* Get the logged error, then reinitialize the log */
  985. log_reg = gsi_reg(gsi, ERROR_LOG);
  986. offset = reg_offset(log_reg);
  987. val = ioread32(gsi->virt + offset);
  988. iowrite32(0, gsi->virt + offset);
  989. clr_reg = gsi_reg(gsi, ERROR_LOG_CLR);
  990. iowrite32(~0, gsi->virt + reg_offset(clr_reg));
  991. /* Parse the error value */
  992. ee = reg_decode(log_reg, ERR_EE, val);
  993. type = reg_decode(log_reg, ERR_TYPE, val);
  994. which = reg_decode(log_reg, ERR_VIRT_IDX, val);
  995. code = reg_decode(log_reg, ERR_CODE, val);
  996. if (type == GSI_ERR_TYPE_CHAN)
  997. gsi_isr_glob_chan_err(gsi, ee, which, code);
  998. else if (type == GSI_ERR_TYPE_EVT)
  999. gsi_isr_glob_evt_err(gsi, ee, which, code);
  1000. else /* type GSI_ERR_TYPE_GLOB should be fatal */
  1001. dev_err(gsi->dev, "unexpected global error 0x%08x\n", type);
  1002. }
  1003. /* Generic EE interrupt handler */
  1004. static void gsi_isr_gp_int1(struct gsi *gsi)
  1005. {
  1006. const struct reg *reg;
  1007. u32 result;
  1008. u32 val;
  1009. /* This interrupt is used to handle completions of GENERIC GSI
  1010. * commands. We use these to allocate and halt channels on the
  1011. * modem's behalf due to a hardware quirk on IPA v4.2. The modem
  1012. * "owns" channels even when the AP allocates them, and have no
  1013. * way of knowing whether a modem channel's state has been changed.
  1014. *
  1015. * We also use GENERIC commands to enable/disable channel flow
  1016. * control for IPA v4.2+.
  1017. *
  1018. * It is recommended that we halt the modem channels we allocated
  1019. * when shutting down, but it's possible the channel isn't running
  1020. * at the time we issue the HALT command. We'll get an error in
  1021. * that case, but it's harmless (the channel is already halted).
  1022. * Similarly, we could get an error back when updating flow control
  1023. * on a channel because it's not in the proper state.
  1024. *
  1025. * In either case, we silently ignore a INCORRECT_CHANNEL_STATE
  1026. * error if we receive it.
  1027. */
  1028. reg = gsi_reg(gsi, CNTXT_SCRATCH_0);
  1029. val = ioread32(gsi->virt + reg_offset(reg));
  1030. result = reg_decode(reg, GENERIC_EE_RESULT, val);
  1031. switch (result) {
  1032. case GENERIC_EE_SUCCESS:
  1033. case GENERIC_EE_INCORRECT_CHANNEL_STATE:
  1034. gsi->result = 0;
  1035. break;
  1036. case GENERIC_EE_RETRY:
  1037. gsi->result = -EAGAIN;
  1038. break;
  1039. default:
  1040. dev_err(gsi->dev, "global INT1 generic result %u\n", result);
  1041. gsi->result = -EIO;
  1042. break;
  1043. }
  1044. complete(&gsi->completion);
  1045. }
  1046. /* Inter-EE interrupt handler */
  1047. static void gsi_isr_glob_ee(struct gsi *gsi)
  1048. {
  1049. const struct reg *reg;
  1050. u32 val;
  1051. reg = gsi_reg(gsi, CNTXT_GLOB_IRQ_STTS);
  1052. val = ioread32(gsi->virt + reg_offset(reg));
  1053. if (val & ERROR_INT)
  1054. gsi_isr_glob_err(gsi);
  1055. reg = gsi_reg(gsi, CNTXT_GLOB_IRQ_CLR);
  1056. iowrite32(val, gsi->virt + reg_offset(reg));
  1057. val &= ~ERROR_INT;
  1058. if (val & GP_INT1) {
  1059. val ^= GP_INT1;
  1060. gsi_isr_gp_int1(gsi);
  1061. }
  1062. if (val)
  1063. dev_err(gsi->dev, "unexpected global interrupt 0x%08x\n", val);
  1064. }
  1065. /* I/O completion interrupt event */
  1066. static void gsi_isr_ieob(struct gsi *gsi)
  1067. {
  1068. const struct reg *reg;
  1069. u32 event_mask;
  1070. reg = gsi_reg(gsi, CNTXT_SRC_IEOB_IRQ);
  1071. event_mask = ioread32(gsi->virt + reg_offset(reg));
  1072. gsi_irq_ieob_disable(gsi, event_mask);
  1073. reg = gsi_reg(gsi, CNTXT_SRC_IEOB_IRQ_CLR);
  1074. iowrite32(event_mask, gsi->virt + reg_offset(reg));
  1075. while (event_mask) {
  1076. u32 evt_ring_id = __ffs(event_mask);
  1077. event_mask ^= BIT(evt_ring_id);
  1078. napi_schedule(&gsi->evt_ring[evt_ring_id].channel->napi);
  1079. }
  1080. }
  1081. /* General event interrupts represent serious problems, so report them */
  1082. static void gsi_isr_general(struct gsi *gsi)
  1083. {
  1084. struct device *dev = gsi->dev;
  1085. const struct reg *reg;
  1086. u32 val;
  1087. reg = gsi_reg(gsi, CNTXT_GSI_IRQ_STTS);
  1088. val = ioread32(gsi->virt + reg_offset(reg));
  1089. reg = gsi_reg(gsi, CNTXT_GSI_IRQ_CLR);
  1090. iowrite32(val, gsi->virt + reg_offset(reg));
  1091. dev_err(dev, "unexpected general interrupt 0x%08x\n", val);
  1092. }
  1093. /**
  1094. * gsi_isr() - Top level GSI interrupt service routine
  1095. * @irq: Interrupt number (ignored)
  1096. * @dev_id: GSI pointer supplied to request_irq()
  1097. *
  1098. * This is the main handler function registered for the GSI IRQ. Each type
  1099. * of interrupt has a separate handler function that is called from here.
  1100. */
  1101. static irqreturn_t gsi_isr(int irq, void *dev_id)
  1102. {
  1103. struct gsi *gsi = dev_id;
  1104. const struct reg *reg;
  1105. u32 intr_mask;
  1106. u32 cnt = 0;
  1107. u32 offset;
  1108. reg = gsi_reg(gsi, CNTXT_TYPE_IRQ);
  1109. offset = reg_offset(reg);
  1110. /* enum gsi_irq_type_id defines GSI interrupt types */
  1111. while ((intr_mask = ioread32(gsi->virt + offset))) {
  1112. /* intr_mask contains bitmask of pending GSI interrupts */
  1113. do {
  1114. u32 gsi_intr = BIT(__ffs(intr_mask));
  1115. intr_mask ^= gsi_intr;
  1116. /* Note: the IRQ condition for each type is cleared
  1117. * when the type-specific register is updated.
  1118. */
  1119. switch (gsi_intr) {
  1120. case GSI_CH_CTRL:
  1121. gsi_isr_chan_ctrl(gsi);
  1122. break;
  1123. case GSI_EV_CTRL:
  1124. gsi_isr_evt_ctrl(gsi);
  1125. break;
  1126. case GSI_GLOB_EE:
  1127. gsi_isr_glob_ee(gsi);
  1128. break;
  1129. case GSI_IEOB:
  1130. gsi_isr_ieob(gsi);
  1131. break;
  1132. case GSI_GENERAL:
  1133. gsi_isr_general(gsi);
  1134. break;
  1135. default:
  1136. dev_err(gsi->dev,
  1137. "unrecognized interrupt type 0x%08x\n",
  1138. gsi_intr);
  1139. break;
  1140. }
  1141. } while (intr_mask);
  1142. if (++cnt > GSI_ISR_MAX_ITER) {
  1143. dev_err(gsi->dev, "interrupt flood\n");
  1144. break;
  1145. }
  1146. }
  1147. return IRQ_HANDLED;
  1148. }
  1149. /* Init function for GSI IRQ lookup; there is no gsi_irq_exit() */
  1150. static int gsi_irq_init(struct gsi *gsi, struct platform_device *pdev)
  1151. {
  1152. int ret;
  1153. ret = platform_get_irq_byname(pdev, "gsi");
  1154. if (ret <= 0)
  1155. return ret ? : -EINVAL;
  1156. gsi->irq = ret;
  1157. return 0;
  1158. }
  1159. /* Return the transaction associated with a transfer completion event */
  1160. static struct gsi_trans *
  1161. gsi_event_trans(struct gsi *gsi, struct gsi_event *event)
  1162. {
  1163. u32 channel_id = event->chid;
  1164. struct gsi_channel *channel;
  1165. struct gsi_trans *trans;
  1166. u32 tre_offset;
  1167. u32 tre_index;
  1168. channel = &gsi->channel[channel_id];
  1169. if (WARN(!channel->gsi, "event has bad channel %u\n", channel_id))
  1170. return NULL;
  1171. /* Event xfer_ptr records the TRE it's associated with */
  1172. tre_offset = lower_32_bits(le64_to_cpu(event->xfer_ptr));
  1173. tre_index = gsi_ring_index(&channel->tre_ring, tre_offset);
  1174. trans = gsi_channel_trans_mapped(channel, tre_index);
  1175. if (WARN(!trans, "channel %u event with no transaction\n", channel_id))
  1176. return NULL;
  1177. return trans;
  1178. }
  1179. /**
  1180. * gsi_evt_ring_update() - Update transaction state from hardware
  1181. * @gsi: GSI pointer
  1182. * @evt_ring_id: Event ring ID
  1183. * @index: Event index in ring reported by hardware
  1184. *
  1185. * Events for RX channels contain the actual number of bytes received into
  1186. * the buffer. Every event has a transaction associated with it, and here
  1187. * we update transactions to record their actual received lengths.
  1188. *
  1189. * When an event for a TX channel arrives we use information in the
  1190. * transaction to report the number of requests and bytes that have
  1191. * been transferred.
  1192. *
  1193. * This function is called whenever we learn that the GSI hardware has filled
  1194. * new events since the last time we checked. The ring's index field tells
  1195. * the first entry in need of processing. The index provided is the
  1196. * first *unfilled* event in the ring (following the last filled one).
  1197. *
  1198. * Events are sequential within the event ring, and transactions are
  1199. * sequential within the transaction array.
  1200. *
  1201. * Note that @index always refers to an element *within* the event ring.
  1202. */
  1203. static void gsi_evt_ring_update(struct gsi *gsi, u32 evt_ring_id, u32 index)
  1204. {
  1205. struct gsi_evt_ring *evt_ring = &gsi->evt_ring[evt_ring_id];
  1206. struct gsi_ring *ring = &evt_ring->ring;
  1207. struct gsi_event *event_done;
  1208. struct gsi_event *event;
  1209. u32 event_avail;
  1210. u32 old_index;
  1211. /* Starting with the oldest un-processed event, determine which
  1212. * transaction (and which channel) is associated with the event.
  1213. * For RX channels, update each completed transaction with the
  1214. * number of bytes that were actually received. For TX channels
  1215. * associated with a network device, report to the network stack
  1216. * the number of transfers and bytes this completion represents.
  1217. */
  1218. old_index = ring->index;
  1219. event = gsi_ring_virt(ring, old_index);
  1220. /* Compute the number of events to process before we wrap,
  1221. * and determine when we'll be done processing events.
  1222. */
  1223. event_avail = ring->count - old_index % ring->count;
  1224. event_done = gsi_ring_virt(ring, index);
  1225. do {
  1226. struct gsi_trans *trans;
  1227. trans = gsi_event_trans(gsi, event);
  1228. if (!trans)
  1229. return;
  1230. if (trans->direction == DMA_FROM_DEVICE)
  1231. trans->len = __le16_to_cpu(event->len);
  1232. else
  1233. gsi_trans_tx_completed(trans);
  1234. gsi_trans_move_complete(trans);
  1235. /* Move on to the next event and transaction */
  1236. if (--event_avail)
  1237. event++;
  1238. else
  1239. event = gsi_ring_virt(ring, 0);
  1240. } while (event != event_done);
  1241. /* Tell the hardware we've handled these events */
  1242. gsi_evt_ring_doorbell(gsi, evt_ring_id, index);
  1243. }
  1244. /* Initialize a ring, including allocating DMA memory for its entries */
  1245. static int gsi_ring_alloc(struct gsi *gsi, struct gsi_ring *ring, u32 count)
  1246. {
  1247. u32 size = count * GSI_RING_ELEMENT_SIZE;
  1248. struct device *dev = gsi->dev;
  1249. dma_addr_t addr;
  1250. /* Hardware requires a 2^n ring size, with alignment equal to size.
  1251. * The DMA address returned by dma_alloc_coherent() is guaranteed to
  1252. * be a power-of-2 number of pages, which satisfies the requirement.
  1253. */
  1254. ring->virt = dma_alloc_coherent(dev, size, &addr, GFP_KERNEL);
  1255. if (!ring->virt)
  1256. return -ENOMEM;
  1257. ring->addr = addr;
  1258. ring->count = count;
  1259. ring->index = 0;
  1260. return 0;
  1261. }
  1262. /* Free a previously-allocated ring */
  1263. static void gsi_ring_free(struct gsi *gsi, struct gsi_ring *ring)
  1264. {
  1265. size_t size = ring->count * GSI_RING_ELEMENT_SIZE;
  1266. dma_free_coherent(gsi->dev, size, ring->virt, ring->addr);
  1267. }
  1268. /* Allocate an available event ring id */
  1269. static int gsi_evt_ring_id_alloc(struct gsi *gsi)
  1270. {
  1271. u32 evt_ring_id;
  1272. if (gsi->event_bitmap == ~0U) {
  1273. dev_err(gsi->dev, "event rings exhausted\n");
  1274. return -ENOSPC;
  1275. }
  1276. evt_ring_id = ffz(gsi->event_bitmap);
  1277. gsi->event_bitmap |= BIT(evt_ring_id);
  1278. return (int)evt_ring_id;
  1279. }
  1280. /* Free a previously-allocated event ring id */
  1281. static void gsi_evt_ring_id_free(struct gsi *gsi, u32 evt_ring_id)
  1282. {
  1283. gsi->event_bitmap &= ~BIT(evt_ring_id);
  1284. }
  1285. /* Ring a channel doorbell, reporting the first un-filled entry */
  1286. void gsi_channel_doorbell(struct gsi_channel *channel)
  1287. {
  1288. struct gsi_ring *tre_ring = &channel->tre_ring;
  1289. u32 channel_id = gsi_channel_id(channel);
  1290. struct gsi *gsi = channel->gsi;
  1291. const struct reg *reg;
  1292. u32 val;
  1293. reg = gsi_reg(gsi, CH_C_DOORBELL_0);
  1294. /* Note: index *must* be used modulo the ring count here */
  1295. val = gsi_ring_addr(tre_ring, tre_ring->index % tre_ring->count);
  1296. iowrite32(val, gsi->virt + reg_n_offset(reg, channel_id));
  1297. }
  1298. /* Consult hardware, move newly completed transactions to completed state */
  1299. void gsi_channel_update(struct gsi_channel *channel)
  1300. {
  1301. u32 evt_ring_id = channel->evt_ring_id;
  1302. struct gsi *gsi = channel->gsi;
  1303. struct gsi_evt_ring *evt_ring;
  1304. struct gsi_trans *trans;
  1305. struct gsi_ring *ring;
  1306. const struct reg *reg;
  1307. u32 offset;
  1308. u32 index;
  1309. evt_ring = &gsi->evt_ring[evt_ring_id];
  1310. ring = &evt_ring->ring;
  1311. /* See if there's anything new to process; if not, we're done. Note
  1312. * that index always refers to an entry *within* the event ring.
  1313. */
  1314. reg = gsi_reg(gsi, EV_CH_E_CNTXT_4);
  1315. offset = reg_n_offset(reg, evt_ring_id);
  1316. index = gsi_ring_index(ring, ioread32(gsi->virt + offset));
  1317. if (index == ring->index % ring->count)
  1318. return;
  1319. /* Get the transaction for the latest completed event. */
  1320. trans = gsi_event_trans(gsi, gsi_ring_virt(ring, index - 1));
  1321. if (!trans)
  1322. return;
  1323. /* For RX channels, update each completed transaction with the number
  1324. * of bytes that were actually received. For TX channels, report
  1325. * the number of transactions and bytes this completion represents
  1326. * up the network stack.
  1327. */
  1328. gsi_evt_ring_update(gsi, evt_ring_id, index);
  1329. }
  1330. /**
  1331. * gsi_channel_poll_one() - Return a single completed transaction on a channel
  1332. * @channel: Channel to be polled
  1333. *
  1334. * Return: Transaction pointer, or null if none are available
  1335. *
  1336. * This function returns the first of a channel's completed transactions.
  1337. * If no transactions are in completed state, the hardware is consulted to
  1338. * determine whether any new transactions have completed. If so, they're
  1339. * moved to completed state and the first such transaction is returned.
  1340. * If there are no more completed transactions, a null pointer is returned.
  1341. */
  1342. static struct gsi_trans *gsi_channel_poll_one(struct gsi_channel *channel)
  1343. {
  1344. struct gsi_trans *trans;
  1345. /* Get the first completed transaction */
  1346. trans = gsi_channel_trans_complete(channel);
  1347. if (trans)
  1348. gsi_trans_move_polled(trans);
  1349. return trans;
  1350. }
  1351. /**
  1352. * gsi_channel_poll() - NAPI poll function for a channel
  1353. * @napi: NAPI structure for the channel
  1354. * @budget: Budget supplied by NAPI core
  1355. *
  1356. * Return: Number of items polled (<= budget)
  1357. *
  1358. * Single transactions completed by hardware are polled until either
  1359. * the budget is exhausted, or there are no more. Each transaction
  1360. * polled is passed to gsi_trans_complete(), to perform remaining
  1361. * completion processing and retire/free the transaction.
  1362. */
  1363. static int gsi_channel_poll(struct napi_struct *napi, int budget)
  1364. {
  1365. struct gsi_channel *channel;
  1366. int count;
  1367. channel = container_of(napi, struct gsi_channel, napi);
  1368. for (count = 0; count < budget; count++) {
  1369. struct gsi_trans *trans;
  1370. trans = gsi_channel_poll_one(channel);
  1371. if (!trans)
  1372. break;
  1373. gsi_trans_complete(trans);
  1374. }
  1375. if (count < budget && napi_complete(napi))
  1376. gsi_irq_ieob_enable_one(channel->gsi, channel->evt_ring_id);
  1377. return count;
  1378. }
  1379. /* The event bitmap represents which event ids are available for allocation.
  1380. * Set bits are not available, clear bits can be used. This function
  1381. * initializes the map so all events supported by the hardware are available,
  1382. * then precludes any reserved events from being allocated.
  1383. */
  1384. static u32 gsi_event_bitmap_init(u32 evt_ring_max)
  1385. {
  1386. u32 event_bitmap = GENMASK(BITS_PER_LONG - 1, evt_ring_max);
  1387. event_bitmap |= GENMASK(GSI_MHI_EVENT_ID_END, GSI_MHI_EVENT_ID_START);
  1388. return event_bitmap;
  1389. }
  1390. /* Setup function for a single channel */
  1391. static int gsi_channel_setup_one(struct gsi *gsi, u32 channel_id)
  1392. {
  1393. struct gsi_channel *channel = &gsi->channel[channel_id];
  1394. u32 evt_ring_id = channel->evt_ring_id;
  1395. int ret;
  1396. if (!gsi_channel_initialized(channel))
  1397. return 0;
  1398. ret = gsi_evt_ring_alloc_command(gsi, evt_ring_id);
  1399. if (ret)
  1400. return ret;
  1401. gsi_evt_ring_program(gsi, evt_ring_id);
  1402. ret = gsi_channel_alloc_command(gsi, channel_id);
  1403. if (ret)
  1404. goto err_evt_ring_de_alloc;
  1405. gsi_channel_program(channel, true);
  1406. if (channel->toward_ipa)
  1407. netif_napi_add_tx(gsi->dummy_dev, &channel->napi,
  1408. gsi_channel_poll);
  1409. else
  1410. netif_napi_add(gsi->dummy_dev, &channel->napi,
  1411. gsi_channel_poll);
  1412. return 0;
  1413. err_evt_ring_de_alloc:
  1414. /* We've done nothing with the event ring yet so don't reset */
  1415. gsi_evt_ring_de_alloc_command(gsi, evt_ring_id);
  1416. return ret;
  1417. }
  1418. /* Inverse of gsi_channel_setup_one() */
  1419. static void gsi_channel_teardown_one(struct gsi *gsi, u32 channel_id)
  1420. {
  1421. struct gsi_channel *channel = &gsi->channel[channel_id];
  1422. u32 evt_ring_id = channel->evt_ring_id;
  1423. if (!gsi_channel_initialized(channel))
  1424. return;
  1425. netif_napi_del(&channel->napi);
  1426. gsi_channel_de_alloc_command(gsi, channel_id);
  1427. gsi_evt_ring_reset_command(gsi, evt_ring_id);
  1428. gsi_evt_ring_de_alloc_command(gsi, evt_ring_id);
  1429. }
  1430. /* We use generic commands only to operate on modem channels. We don't have
  1431. * the ability to determine channel state for a modem channel, so we simply
  1432. * issue the command and wait for it to complete.
  1433. */
  1434. static int gsi_generic_command(struct gsi *gsi, u32 channel_id,
  1435. enum gsi_generic_cmd_opcode opcode,
  1436. u8 params)
  1437. {
  1438. const struct reg *reg;
  1439. bool timeout;
  1440. u32 offset;
  1441. u32 val;
  1442. /* The error global interrupt type is always enabled (until we tear
  1443. * down), so we will keep it enabled.
  1444. *
  1445. * A generic EE command completes with a GSI global interrupt of
  1446. * type GP_INT1. We only perform one generic command at a time
  1447. * (to allocate, halt, or enable/disable flow control on a modem
  1448. * channel), and only from this function. So we enable the GP_INT1
  1449. * IRQ type here, and disable it again after the command completes.
  1450. */
  1451. reg = gsi_reg(gsi, CNTXT_GLOB_IRQ_EN);
  1452. val = ERROR_INT | GP_INT1;
  1453. iowrite32(val, gsi->virt + reg_offset(reg));
  1454. /* First zero the result code field */
  1455. reg = gsi_reg(gsi, CNTXT_SCRATCH_0);
  1456. offset = reg_offset(reg);
  1457. val = ioread32(gsi->virt + offset);
  1458. val &= ~reg_fmask(reg, GENERIC_EE_RESULT);
  1459. iowrite32(val, gsi->virt + offset);
  1460. /* Now issue the command */
  1461. reg = gsi_reg(gsi, GENERIC_CMD);
  1462. val = reg_encode(reg, GENERIC_OPCODE, opcode);
  1463. val |= reg_encode(reg, GENERIC_CHID, channel_id);
  1464. val |= reg_encode(reg, GENERIC_EE, GSI_EE_MODEM);
  1465. if (gsi->version >= IPA_VERSION_4_11)
  1466. val |= reg_encode(reg, GENERIC_PARAMS, params);
  1467. timeout = !gsi_command(gsi, reg_offset(reg), val);
  1468. /* Disable the GP_INT1 IRQ type again */
  1469. reg = gsi_reg(gsi, CNTXT_GLOB_IRQ_EN);
  1470. iowrite32(ERROR_INT, gsi->virt + reg_offset(reg));
  1471. if (!timeout)
  1472. return gsi->result;
  1473. dev_err(gsi->dev, "GSI generic command %u to channel %u timed out\n",
  1474. opcode, channel_id);
  1475. return -ETIMEDOUT;
  1476. }
  1477. static int gsi_modem_channel_alloc(struct gsi *gsi, u32 channel_id)
  1478. {
  1479. return gsi_generic_command(gsi, channel_id,
  1480. GSI_GENERIC_ALLOCATE_CHANNEL, 0);
  1481. }
  1482. static void gsi_modem_channel_halt(struct gsi *gsi, u32 channel_id)
  1483. {
  1484. u32 retries = GSI_CHANNEL_MODEM_HALT_RETRIES;
  1485. int ret;
  1486. do
  1487. ret = gsi_generic_command(gsi, channel_id,
  1488. GSI_GENERIC_HALT_CHANNEL, 0);
  1489. while (ret == -EAGAIN && retries--);
  1490. if (ret)
  1491. dev_err(gsi->dev, "error %d halting modem channel %u\n",
  1492. ret, channel_id);
  1493. }
  1494. /* Enable or disable flow control for a modem GSI TX channel (IPA v4.2+) */
  1495. void
  1496. gsi_modem_channel_flow_control(struct gsi *gsi, u32 channel_id, bool enable)
  1497. {
  1498. u32 retries = 0;
  1499. u32 command;
  1500. int ret;
  1501. command = enable ? GSI_GENERIC_ENABLE_FLOW_CONTROL
  1502. : GSI_GENERIC_DISABLE_FLOW_CONTROL;
  1503. /* Disabling flow control on IPA v4.11+ can return -EAGAIN if enable
  1504. * is underway. In this case we need to retry the command.
  1505. */
  1506. if (!enable && gsi->version >= IPA_VERSION_4_11)
  1507. retries = GSI_CHANNEL_MODEM_FLOW_RETRIES;
  1508. do
  1509. ret = gsi_generic_command(gsi, channel_id, command, 0);
  1510. while (ret == -EAGAIN && retries--);
  1511. if (ret)
  1512. dev_err(gsi->dev,
  1513. "error %d %sabling mode channel %u flow control\n",
  1514. ret, enable ? "en" : "dis", channel_id);
  1515. }
  1516. /* Setup function for channels */
  1517. static int gsi_channel_setup(struct gsi *gsi)
  1518. {
  1519. u32 channel_id = 0;
  1520. u32 mask;
  1521. int ret;
  1522. gsi_irq_enable(gsi);
  1523. mutex_lock(&gsi->mutex);
  1524. do {
  1525. ret = gsi_channel_setup_one(gsi, channel_id);
  1526. if (ret)
  1527. goto err_unwind;
  1528. } while (++channel_id < gsi->channel_count);
  1529. /* Make sure no channels were defined that hardware does not support */
  1530. while (channel_id < GSI_CHANNEL_COUNT_MAX) {
  1531. struct gsi_channel *channel = &gsi->channel[channel_id++];
  1532. if (!gsi_channel_initialized(channel))
  1533. continue;
  1534. ret = -EINVAL;
  1535. dev_err(gsi->dev, "channel %u not supported by hardware\n",
  1536. channel_id - 1);
  1537. channel_id = gsi->channel_count;
  1538. goto err_unwind;
  1539. }
  1540. /* Allocate modem channels if necessary */
  1541. mask = gsi->modem_channel_bitmap;
  1542. while (mask) {
  1543. u32 modem_channel_id = __ffs(mask);
  1544. ret = gsi_modem_channel_alloc(gsi, modem_channel_id);
  1545. if (ret)
  1546. goto err_unwind_modem;
  1547. /* Clear bit from mask only after success (for unwind) */
  1548. mask ^= BIT(modem_channel_id);
  1549. }
  1550. mutex_unlock(&gsi->mutex);
  1551. return 0;
  1552. err_unwind_modem:
  1553. /* Compute which modem channels need to be deallocated */
  1554. mask ^= gsi->modem_channel_bitmap;
  1555. while (mask) {
  1556. channel_id = __fls(mask);
  1557. mask ^= BIT(channel_id);
  1558. gsi_modem_channel_halt(gsi, channel_id);
  1559. }
  1560. err_unwind:
  1561. while (channel_id--)
  1562. gsi_channel_teardown_one(gsi, channel_id);
  1563. mutex_unlock(&gsi->mutex);
  1564. gsi_irq_disable(gsi);
  1565. return ret;
  1566. }
  1567. /* Inverse of gsi_channel_setup() */
  1568. static void gsi_channel_teardown(struct gsi *gsi)
  1569. {
  1570. u32 mask = gsi->modem_channel_bitmap;
  1571. u32 channel_id;
  1572. mutex_lock(&gsi->mutex);
  1573. while (mask) {
  1574. channel_id = __fls(mask);
  1575. mask ^= BIT(channel_id);
  1576. gsi_modem_channel_halt(gsi, channel_id);
  1577. }
  1578. channel_id = gsi->channel_count - 1;
  1579. do
  1580. gsi_channel_teardown_one(gsi, channel_id);
  1581. while (channel_id--);
  1582. mutex_unlock(&gsi->mutex);
  1583. gsi_irq_disable(gsi);
  1584. }
  1585. /* Turn off all GSI interrupts initially */
  1586. static int gsi_irq_setup(struct gsi *gsi)
  1587. {
  1588. const struct reg *reg;
  1589. int ret;
  1590. /* Writing 1 indicates IRQ interrupts; 0 would be MSI */
  1591. reg = gsi_reg(gsi, CNTXT_INTSET);
  1592. iowrite32(reg_bit(reg, INTYPE), gsi->virt + reg_offset(reg));
  1593. /* Disable all interrupt types */
  1594. gsi_irq_type_update(gsi, 0);
  1595. /* Clear all type-specific interrupt masks */
  1596. reg = gsi_reg(gsi, CNTXT_SRC_CH_IRQ_MSK);
  1597. iowrite32(0, gsi->virt + reg_offset(reg));
  1598. reg = gsi_reg(gsi, CNTXT_SRC_EV_CH_IRQ_MSK);
  1599. iowrite32(0, gsi->virt + reg_offset(reg));
  1600. reg = gsi_reg(gsi, CNTXT_GLOB_IRQ_EN);
  1601. iowrite32(0, gsi->virt + reg_offset(reg));
  1602. reg = gsi_reg(gsi, CNTXT_SRC_IEOB_IRQ_MSK);
  1603. iowrite32(0, gsi->virt + reg_offset(reg));
  1604. /* The inter-EE interrupts are not supported for IPA v3.0-v3.1 */
  1605. if (gsi->version > IPA_VERSION_3_1) {
  1606. reg = gsi_reg(gsi, INTER_EE_SRC_CH_IRQ_MSK);
  1607. iowrite32(0, gsi->virt + reg_offset(reg));
  1608. reg = gsi_reg(gsi, INTER_EE_SRC_EV_CH_IRQ_MSK);
  1609. iowrite32(0, gsi->virt + reg_offset(reg));
  1610. }
  1611. reg = gsi_reg(gsi, CNTXT_GSI_IRQ_EN);
  1612. iowrite32(0, gsi->virt + reg_offset(reg));
  1613. ret = request_irq(gsi->irq, gsi_isr, 0, "gsi", gsi);
  1614. if (ret)
  1615. dev_err(gsi->dev, "error %d requesting \"gsi\" IRQ\n", ret);
  1616. return ret;
  1617. }
  1618. static void gsi_irq_teardown(struct gsi *gsi)
  1619. {
  1620. free_irq(gsi->irq, gsi);
  1621. }
  1622. /* Get # supported channel and event rings; there is no gsi_ring_teardown() */
  1623. static int gsi_ring_setup(struct gsi *gsi)
  1624. {
  1625. struct device *dev = gsi->dev;
  1626. const struct reg *reg;
  1627. u32 count;
  1628. u32 val;
  1629. if (gsi->version < IPA_VERSION_3_5_1) {
  1630. /* No HW_PARAM_2 register prior to IPA v3.5.1, assume the max */
  1631. gsi->channel_count = GSI_CHANNEL_COUNT_MAX;
  1632. gsi->evt_ring_count = GSI_EVT_RING_COUNT_MAX;
  1633. return 0;
  1634. }
  1635. reg = gsi_reg(gsi, HW_PARAM_2);
  1636. val = ioread32(gsi->virt + reg_offset(reg));
  1637. count = reg_decode(reg, NUM_CH_PER_EE, val);
  1638. if (!count) {
  1639. dev_err(dev, "GSI reports zero channels supported\n");
  1640. return -EINVAL;
  1641. }
  1642. if (count > GSI_CHANNEL_COUNT_MAX) {
  1643. dev_warn(dev, "limiting to %u channels; hardware supports %u\n",
  1644. GSI_CHANNEL_COUNT_MAX, count);
  1645. count = GSI_CHANNEL_COUNT_MAX;
  1646. }
  1647. gsi->channel_count = count;
  1648. if (gsi->version < IPA_VERSION_5_0) {
  1649. count = reg_decode(reg, NUM_EV_PER_EE, val);
  1650. } else {
  1651. reg = gsi_reg(gsi, HW_PARAM_4);
  1652. count = reg_decode(reg, EV_PER_EE, val);
  1653. }
  1654. if (!count) {
  1655. dev_err(dev, "GSI reports zero event rings supported\n");
  1656. return -EINVAL;
  1657. }
  1658. if (count > GSI_EVT_RING_COUNT_MAX) {
  1659. dev_warn(dev,
  1660. "limiting to %u event rings; hardware supports %u\n",
  1661. GSI_EVT_RING_COUNT_MAX, count);
  1662. count = GSI_EVT_RING_COUNT_MAX;
  1663. }
  1664. gsi->evt_ring_count = count;
  1665. return 0;
  1666. }
  1667. /* Setup function for GSI. GSI firmware must be loaded and initialized */
  1668. int gsi_setup(struct gsi *gsi)
  1669. {
  1670. const struct reg *reg;
  1671. u32 val;
  1672. int ret;
  1673. /* Here is where we first touch the GSI hardware */
  1674. reg = gsi_reg(gsi, GSI_STATUS);
  1675. val = ioread32(gsi->virt + reg_offset(reg));
  1676. if (!(val & reg_bit(reg, ENABLED))) {
  1677. dev_err(gsi->dev, "GSI has not been enabled\n");
  1678. return -EIO;
  1679. }
  1680. ret = gsi_irq_setup(gsi);
  1681. if (ret)
  1682. return ret;
  1683. ret = gsi_ring_setup(gsi); /* No matching teardown required */
  1684. if (ret)
  1685. goto err_irq_teardown;
  1686. /* Initialize the error log */
  1687. reg = gsi_reg(gsi, ERROR_LOG);
  1688. iowrite32(0, gsi->virt + reg_offset(reg));
  1689. ret = gsi_channel_setup(gsi);
  1690. if (ret)
  1691. goto err_irq_teardown;
  1692. return 0;
  1693. err_irq_teardown:
  1694. gsi_irq_teardown(gsi);
  1695. return ret;
  1696. }
  1697. /* Inverse of gsi_setup() */
  1698. void gsi_teardown(struct gsi *gsi)
  1699. {
  1700. gsi_channel_teardown(gsi);
  1701. gsi_irq_teardown(gsi);
  1702. }
  1703. /* Initialize a channel's event ring */
  1704. static int gsi_channel_evt_ring_init(struct gsi_channel *channel)
  1705. {
  1706. struct gsi *gsi = channel->gsi;
  1707. struct gsi_evt_ring *evt_ring;
  1708. int ret;
  1709. ret = gsi_evt_ring_id_alloc(gsi);
  1710. if (ret < 0)
  1711. return ret;
  1712. channel->evt_ring_id = ret;
  1713. evt_ring = &gsi->evt_ring[channel->evt_ring_id];
  1714. evt_ring->channel = channel;
  1715. ret = gsi_ring_alloc(gsi, &evt_ring->ring, channel->event_count);
  1716. if (!ret)
  1717. return 0; /* Success! */
  1718. dev_err(gsi->dev, "error %d allocating channel %u event ring\n",
  1719. ret, gsi_channel_id(channel));
  1720. gsi_evt_ring_id_free(gsi, channel->evt_ring_id);
  1721. return ret;
  1722. }
  1723. /* Inverse of gsi_channel_evt_ring_init() */
  1724. static void gsi_channel_evt_ring_exit(struct gsi_channel *channel)
  1725. {
  1726. u32 evt_ring_id = channel->evt_ring_id;
  1727. struct gsi *gsi = channel->gsi;
  1728. struct gsi_evt_ring *evt_ring;
  1729. evt_ring = &gsi->evt_ring[evt_ring_id];
  1730. gsi_ring_free(gsi, &evt_ring->ring);
  1731. gsi_evt_ring_id_free(gsi, evt_ring_id);
  1732. }
  1733. static bool gsi_channel_data_valid(struct gsi *gsi, bool command,
  1734. const struct ipa_gsi_endpoint_data *data)
  1735. {
  1736. const struct gsi_channel_data *channel_data;
  1737. u32 channel_id = data->channel_id;
  1738. struct device *dev = gsi->dev;
  1739. /* Make sure channel ids are in the range driver supports */
  1740. if (channel_id >= GSI_CHANNEL_COUNT_MAX) {
  1741. dev_err(dev, "bad channel id %u; must be less than %u\n",
  1742. channel_id, GSI_CHANNEL_COUNT_MAX);
  1743. return false;
  1744. }
  1745. if (data->ee_id != GSI_EE_AP && data->ee_id != GSI_EE_MODEM) {
  1746. dev_err(dev, "bad EE id %u; not AP or modem\n", data->ee_id);
  1747. return false;
  1748. }
  1749. if (command && !data->toward_ipa) {
  1750. dev_err(dev, "command channel %u is not TX\n", channel_id);
  1751. return false;
  1752. }
  1753. channel_data = &data->channel;
  1754. if (!channel_data->tlv_count ||
  1755. channel_data->tlv_count > GSI_TLV_MAX) {
  1756. dev_err(dev, "channel %u bad tlv_count %u; must be 1..%u\n",
  1757. channel_id, channel_data->tlv_count, GSI_TLV_MAX);
  1758. return false;
  1759. }
  1760. if (command && IPA_COMMAND_TRANS_TRE_MAX > channel_data->tlv_count) {
  1761. dev_err(dev, "command TRE max too big for channel %u (%u > %u)\n",
  1762. channel_id, IPA_COMMAND_TRANS_TRE_MAX,
  1763. channel_data->tlv_count);
  1764. return false;
  1765. }
  1766. /* We have to allow at least one maximally-sized transaction to
  1767. * be outstanding (which would use tlv_count TREs). Given how
  1768. * gsi_channel_tre_max() is computed, tre_count has to be almost
  1769. * twice the TLV FIFO size to satisfy this requirement.
  1770. */
  1771. if (channel_data->tre_count < 2 * channel_data->tlv_count - 1) {
  1772. dev_err(dev, "channel %u TLV count %u exceeds TRE count %u\n",
  1773. channel_id, channel_data->tlv_count,
  1774. channel_data->tre_count);
  1775. return false;
  1776. }
  1777. if (!is_power_of_2(channel_data->tre_count)) {
  1778. dev_err(dev, "channel %u bad tre_count %u; not power of 2\n",
  1779. channel_id, channel_data->tre_count);
  1780. return false;
  1781. }
  1782. if (!is_power_of_2(channel_data->event_count)) {
  1783. dev_err(dev, "channel %u bad event_count %u; not power of 2\n",
  1784. channel_id, channel_data->event_count);
  1785. return false;
  1786. }
  1787. return true;
  1788. }
  1789. /* Init function for a single channel */
  1790. static int gsi_channel_init_one(struct gsi *gsi,
  1791. const struct ipa_gsi_endpoint_data *data,
  1792. bool command)
  1793. {
  1794. struct gsi_channel *channel;
  1795. u32 tre_count;
  1796. int ret;
  1797. if (!gsi_channel_data_valid(gsi, command, data))
  1798. return -EINVAL;
  1799. /* Worst case we need an event for every outstanding TRE */
  1800. if (data->channel.tre_count > data->channel.event_count) {
  1801. tre_count = data->channel.event_count;
  1802. dev_warn(gsi->dev, "channel %u limited to %u TREs\n",
  1803. data->channel_id, tre_count);
  1804. } else {
  1805. tre_count = data->channel.tre_count;
  1806. }
  1807. channel = &gsi->channel[data->channel_id];
  1808. memset(channel, 0, sizeof(*channel));
  1809. channel->gsi = gsi;
  1810. channel->toward_ipa = data->toward_ipa;
  1811. channel->command = command;
  1812. channel->trans_tre_max = data->channel.tlv_count;
  1813. channel->tre_count = tre_count;
  1814. channel->event_count = data->channel.event_count;
  1815. ret = gsi_channel_evt_ring_init(channel);
  1816. if (ret)
  1817. goto err_clear_gsi;
  1818. ret = gsi_ring_alloc(gsi, &channel->tre_ring, data->channel.tre_count);
  1819. if (ret) {
  1820. dev_err(gsi->dev, "error %d allocating channel %u ring\n",
  1821. ret, data->channel_id);
  1822. goto err_channel_evt_ring_exit;
  1823. }
  1824. ret = gsi_channel_trans_init(gsi, data->channel_id);
  1825. if (ret)
  1826. goto err_ring_free;
  1827. if (command) {
  1828. u32 tre_max = gsi_channel_tre_max(gsi, data->channel_id);
  1829. ret = ipa_cmd_pool_init(channel, tre_max);
  1830. }
  1831. if (!ret)
  1832. return 0; /* Success! */
  1833. gsi_channel_trans_exit(channel);
  1834. err_ring_free:
  1835. gsi_ring_free(gsi, &channel->tre_ring);
  1836. err_channel_evt_ring_exit:
  1837. gsi_channel_evt_ring_exit(channel);
  1838. err_clear_gsi:
  1839. channel->gsi = NULL; /* Mark it not (fully) initialized */
  1840. return ret;
  1841. }
  1842. /* Inverse of gsi_channel_init_one() */
  1843. static void gsi_channel_exit_one(struct gsi_channel *channel)
  1844. {
  1845. if (!gsi_channel_initialized(channel))
  1846. return;
  1847. if (channel->command)
  1848. ipa_cmd_pool_exit(channel);
  1849. gsi_channel_trans_exit(channel);
  1850. gsi_ring_free(channel->gsi, &channel->tre_ring);
  1851. gsi_channel_evt_ring_exit(channel);
  1852. }
  1853. /* Init function for channels */
  1854. static int gsi_channel_init(struct gsi *gsi, u32 count,
  1855. const struct ipa_gsi_endpoint_data *data)
  1856. {
  1857. bool modem_alloc;
  1858. int ret = 0;
  1859. u32 i;
  1860. /* IPA v4.2 requires the AP to allocate channels for the modem */
  1861. modem_alloc = gsi->version == IPA_VERSION_4_2;
  1862. gsi->event_bitmap = gsi_event_bitmap_init(GSI_EVT_RING_COUNT_MAX);
  1863. gsi->ieob_enabled_bitmap = 0;
  1864. /* The endpoint data array is indexed by endpoint name */
  1865. for (i = 0; i < count; i++) {
  1866. bool command = i == IPA_ENDPOINT_AP_COMMAND_TX;
  1867. if (ipa_gsi_endpoint_data_empty(&data[i]))
  1868. continue; /* Skip over empty slots */
  1869. /* Mark modem channels to be allocated (hardware workaround) */
  1870. if (data[i].ee_id == GSI_EE_MODEM) {
  1871. if (modem_alloc)
  1872. gsi->modem_channel_bitmap |=
  1873. BIT(data[i].channel_id);
  1874. continue;
  1875. }
  1876. ret = gsi_channel_init_one(gsi, &data[i], command);
  1877. if (ret)
  1878. goto err_unwind;
  1879. }
  1880. return ret;
  1881. err_unwind:
  1882. while (i--) {
  1883. if (ipa_gsi_endpoint_data_empty(&data[i]))
  1884. continue;
  1885. if (modem_alloc && data[i].ee_id == GSI_EE_MODEM) {
  1886. gsi->modem_channel_bitmap &= ~BIT(data[i].channel_id);
  1887. continue;
  1888. }
  1889. gsi_channel_exit_one(&gsi->channel[data->channel_id]);
  1890. }
  1891. return ret;
  1892. }
  1893. /* Inverse of gsi_channel_init() */
  1894. static void gsi_channel_exit(struct gsi *gsi)
  1895. {
  1896. u32 channel_id = GSI_CHANNEL_COUNT_MAX - 1;
  1897. do
  1898. gsi_channel_exit_one(&gsi->channel[channel_id]);
  1899. while (channel_id--);
  1900. gsi->modem_channel_bitmap = 0;
  1901. }
  1902. /* Init function for GSI. GSI hardware does not need to be "ready" */
  1903. int gsi_init(struct gsi *gsi, struct platform_device *pdev,
  1904. enum ipa_version version, u32 count,
  1905. const struct ipa_gsi_endpoint_data *data)
  1906. {
  1907. int ret;
  1908. gsi_validate_build();
  1909. gsi->dev = &pdev->dev;
  1910. gsi->version = version;
  1911. /* GSI uses NAPI on all channels. Create a dummy network device
  1912. * for the channel NAPI contexts to be associated with.
  1913. */
  1914. gsi->dummy_dev = alloc_netdev_dummy(0);
  1915. if (!gsi->dummy_dev)
  1916. return -ENOMEM;
  1917. init_completion(&gsi->completion);
  1918. ret = gsi_reg_init(gsi, pdev);
  1919. if (ret)
  1920. goto err_reg_exit;
  1921. ret = gsi_irq_init(gsi, pdev); /* No matching exit required */
  1922. if (ret)
  1923. goto err_reg_exit;
  1924. ret = gsi_channel_init(gsi, count, data);
  1925. if (ret)
  1926. goto err_reg_exit;
  1927. mutex_init(&gsi->mutex);
  1928. return 0;
  1929. err_reg_exit:
  1930. free_netdev(gsi->dummy_dev);
  1931. gsi_reg_exit(gsi);
  1932. return ret;
  1933. }
  1934. /* Inverse of gsi_init() */
  1935. void gsi_exit(struct gsi *gsi)
  1936. {
  1937. mutex_destroy(&gsi->mutex);
  1938. gsi_channel_exit(gsi);
  1939. free_netdev(gsi->dummy_dev);
  1940. gsi_reg_exit(gsi);
  1941. }
  1942. /* The maximum number of outstanding TREs on a channel. This limits
  1943. * a channel's maximum number of transactions outstanding (worst case
  1944. * is one TRE per transaction).
  1945. *
  1946. * The absolute limit is the number of TREs in the channel's TRE ring,
  1947. * and in theory we should be able use all of them. But in practice,
  1948. * doing that led to the hardware reporting exhaustion of event ring
  1949. * slots for writing completion information. So the hardware limit
  1950. * would be (tre_count - 1).
  1951. *
  1952. * We reduce it a bit further though. Transaction resource pools are
  1953. * sized to be a little larger than this maximum, to allow resource
  1954. * allocations to always be contiguous. The number of entries in a
  1955. * TRE ring buffer is a power of 2, and the extra resources in a pool
  1956. * tends to nearly double the memory allocated for it. Reducing the
  1957. * maximum number of outstanding TREs allows the number of entries in
  1958. * a pool to avoid crossing that power-of-2 boundary, and this can
  1959. * substantially reduce pool memory requirements. The number we
  1960. * reduce it by matches the number added in gsi_trans_pool_init().
  1961. */
  1962. u32 gsi_channel_tre_max(struct gsi *gsi, u32 channel_id)
  1963. {
  1964. struct gsi_channel *channel = &gsi->channel[channel_id];
  1965. /* Hardware limit is channel->tre_count - 1 */
  1966. return channel->tre_count - (channel->trans_tre_max - 1);
  1967. }