ce.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969
  1. // SPDX-License-Identifier: ISC
  2. /*
  3. * Copyright (c) 2005-2011 Atheros Communications Inc.
  4. * Copyright (c) 2011-2017 Qualcomm Atheros, Inc.
  5. * Copyright (c) 2018 The Linux Foundation. All rights reserved.
  6. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  7. */
  8. #include "hif.h"
  9. #include "ce.h"
  10. #include "debug.h"
  11. /*
  12. * Support for Copy Engine hardware, which is mainly used for
  13. * communication between Host and Target over a PCIe interconnect.
  14. */
  15. /*
  16. * A single CopyEngine (CE) comprises two "rings":
  17. * a source ring
  18. * a destination ring
  19. *
  20. * Each ring consists of a number of descriptors which specify
  21. * an address, length, and meta-data.
  22. *
  23. * Typically, one side of the PCIe/AHB/SNOC interconnect (Host or Target)
  24. * controls one ring and the other side controls the other ring.
  25. * The source side chooses when to initiate a transfer and it
  26. * chooses what to send (buffer address, length). The destination
  27. * side keeps a supply of "anonymous receive buffers" available and
  28. * it handles incoming data as it arrives (when the destination
  29. * receives an interrupt).
  30. *
  31. * The sender may send a simple buffer (address/length) or it may
  32. * send a small list of buffers. When a small list is sent, hardware
  33. * "gathers" these and they end up in a single destination buffer
  34. * with a single interrupt.
  35. *
  36. * There are several "contexts" managed by this layer -- more, it
  37. * may seem -- than should be needed. These are provided mainly for
  38. * maximum flexibility and especially to facilitate a simpler HIF
  39. * implementation. There are per-CopyEngine recv, send, and watermark
  40. * contexts. These are supplied by the caller when a recv, send,
  41. * or watermark handler is established and they are echoed back to
  42. * the caller when the respective callbacks are invoked. There is
  43. * also a per-transfer context supplied by the caller when a buffer
  44. * (or sendlist) is sent and when a buffer is enqueued for recv.
  45. * These per-transfer contexts are echoed back to the caller when
  46. * the buffer is sent/received.
  47. */
  48. static inline u32 shadow_sr_wr_ind_addr(struct ath10k *ar,
  49. struct ath10k_ce_pipe *ce_state)
  50. {
  51. u32 ce_id = ce_state->id;
  52. u32 addr = 0;
  53. switch (ce_id) {
  54. case 0:
  55. addr = 0x00032000;
  56. break;
  57. case 3:
  58. addr = 0x0003200C;
  59. break;
  60. case 4:
  61. addr = 0x00032010;
  62. break;
  63. case 5:
  64. addr = 0x00032014;
  65. break;
  66. case 7:
  67. addr = 0x0003201C;
  68. break;
  69. default:
  70. ath10k_warn(ar, "invalid CE id: %d", ce_id);
  71. break;
  72. }
  73. return addr;
  74. }
  75. static inline unsigned int
  76. ath10k_set_ring_byte(unsigned int offset,
  77. struct ath10k_hw_ce_regs_addr_map *addr_map)
  78. {
  79. return ((offset << addr_map->lsb) & addr_map->mask);
  80. }
  81. static inline u32 ath10k_ce_read32(struct ath10k *ar, u32 offset)
  82. {
  83. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  84. return ce->bus_ops->read32(ar, offset);
  85. }
  86. static inline void ath10k_ce_write32(struct ath10k *ar, u32 offset, u32 value)
  87. {
  88. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  89. ce->bus_ops->write32(ar, offset, value);
  90. }
  91. static inline void ath10k_ce_dest_ring_write_index_set(struct ath10k *ar,
  92. u32 ce_ctrl_addr,
  93. unsigned int n)
  94. {
  95. ath10k_ce_write32(ar, ce_ctrl_addr +
  96. ar->hw_ce_regs->dst_wr_index_addr, n);
  97. }
  98. static inline u32 ath10k_ce_dest_ring_write_index_get(struct ath10k *ar,
  99. u32 ce_ctrl_addr)
  100. {
  101. return ath10k_ce_read32(ar, ce_ctrl_addr +
  102. ar->hw_ce_regs->dst_wr_index_addr);
  103. }
  104. static inline void ath10k_ce_src_ring_write_index_set(struct ath10k *ar,
  105. u32 ce_ctrl_addr,
  106. unsigned int n)
  107. {
  108. ath10k_ce_write32(ar, ce_ctrl_addr +
  109. ar->hw_ce_regs->sr_wr_index_addr, n);
  110. }
  111. static inline u32 ath10k_ce_src_ring_write_index_get(struct ath10k *ar,
  112. u32 ce_ctrl_addr)
  113. {
  114. return ath10k_ce_read32(ar, ce_ctrl_addr +
  115. ar->hw_ce_regs->sr_wr_index_addr);
  116. }
  117. static inline u32 ath10k_ce_src_ring_read_index_from_ddr(struct ath10k *ar,
  118. u32 ce_id)
  119. {
  120. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  121. return ce->vaddr_rri[ce_id] & CE_DDR_RRI_MASK;
  122. }
  123. static inline u32 ath10k_ce_src_ring_read_index_get(struct ath10k *ar,
  124. u32 ce_ctrl_addr)
  125. {
  126. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  127. u32 ce_id = COPY_ENGINE_ID(ce_ctrl_addr);
  128. struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
  129. u32 index;
  130. if (ar->hw_params.rri_on_ddr &&
  131. (ce_state->attr_flags & CE_ATTR_DIS_INTR))
  132. index = ath10k_ce_src_ring_read_index_from_ddr(ar, ce_id);
  133. else
  134. index = ath10k_ce_read32(ar, ce_ctrl_addr +
  135. ar->hw_ce_regs->current_srri_addr);
  136. return index;
  137. }
  138. static inline void
  139. ath10k_ce_shadow_src_ring_write_index_set(struct ath10k *ar,
  140. struct ath10k_ce_pipe *ce_state,
  141. unsigned int value)
  142. {
  143. ath10k_ce_write32(ar, shadow_sr_wr_ind_addr(ar, ce_state), value);
  144. }
  145. static inline void ath10k_ce_src_ring_base_addr_set(struct ath10k *ar,
  146. u32 ce_id,
  147. u64 addr)
  148. {
  149. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  150. struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
  151. u32 ce_ctrl_addr = ath10k_ce_base_address(ar, ce_id);
  152. u32 addr_lo = lower_32_bits(addr);
  153. ath10k_ce_write32(ar, ce_ctrl_addr +
  154. ar->hw_ce_regs->sr_base_addr_lo, addr_lo);
  155. if (ce_state->ops->ce_set_src_ring_base_addr_hi) {
  156. ce_state->ops->ce_set_src_ring_base_addr_hi(ar, ce_ctrl_addr,
  157. addr);
  158. }
  159. }
  160. static void ath10k_ce_set_src_ring_base_addr_hi(struct ath10k *ar,
  161. u32 ce_ctrl_addr,
  162. u64 addr)
  163. {
  164. u32 addr_hi = upper_32_bits(addr) & CE_DESC_ADDR_HI_MASK;
  165. ath10k_ce_write32(ar, ce_ctrl_addr +
  166. ar->hw_ce_regs->sr_base_addr_hi, addr_hi);
  167. }
  168. static inline void ath10k_ce_src_ring_size_set(struct ath10k *ar,
  169. u32 ce_ctrl_addr,
  170. unsigned int n)
  171. {
  172. ath10k_ce_write32(ar, ce_ctrl_addr +
  173. ar->hw_ce_regs->sr_size_addr, n);
  174. }
  175. static inline void ath10k_ce_src_ring_dmax_set(struct ath10k *ar,
  176. u32 ce_ctrl_addr,
  177. unsigned int n)
  178. {
  179. struct ath10k_hw_ce_ctrl1 *ctrl_regs = ar->hw_ce_regs->ctrl1_regs;
  180. u32 ctrl1_addr = ath10k_ce_read32(ar, ce_ctrl_addr +
  181. ctrl_regs->addr);
  182. ath10k_ce_write32(ar, ce_ctrl_addr + ctrl_regs->addr,
  183. (ctrl1_addr & ~(ctrl_regs->dmax->mask)) |
  184. ath10k_set_ring_byte(n, ctrl_regs->dmax));
  185. }
  186. static inline void ath10k_ce_src_ring_byte_swap_set(struct ath10k *ar,
  187. u32 ce_ctrl_addr,
  188. unsigned int n)
  189. {
  190. struct ath10k_hw_ce_ctrl1 *ctrl_regs = ar->hw_ce_regs->ctrl1_regs;
  191. u32 ctrl1_addr = ath10k_ce_read32(ar, ce_ctrl_addr +
  192. ctrl_regs->addr);
  193. ath10k_ce_write32(ar, ce_ctrl_addr + ctrl_regs->addr,
  194. (ctrl1_addr & ~(ctrl_regs->src_ring->mask)) |
  195. ath10k_set_ring_byte(n, ctrl_regs->src_ring));
  196. }
  197. static inline void ath10k_ce_dest_ring_byte_swap_set(struct ath10k *ar,
  198. u32 ce_ctrl_addr,
  199. unsigned int n)
  200. {
  201. struct ath10k_hw_ce_ctrl1 *ctrl_regs = ar->hw_ce_regs->ctrl1_regs;
  202. u32 ctrl1_addr = ath10k_ce_read32(ar, ce_ctrl_addr +
  203. ctrl_regs->addr);
  204. ath10k_ce_write32(ar, ce_ctrl_addr + ctrl_regs->addr,
  205. (ctrl1_addr & ~(ctrl_regs->dst_ring->mask)) |
  206. ath10k_set_ring_byte(n, ctrl_regs->dst_ring));
  207. }
  208. static inline
  209. u32 ath10k_ce_dest_ring_read_index_from_ddr(struct ath10k *ar, u32 ce_id)
  210. {
  211. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  212. return (ce->vaddr_rri[ce_id] >> CE_DDR_DRRI_SHIFT) &
  213. CE_DDR_RRI_MASK;
  214. }
  215. static inline u32 ath10k_ce_dest_ring_read_index_get(struct ath10k *ar,
  216. u32 ce_ctrl_addr)
  217. {
  218. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  219. u32 ce_id = COPY_ENGINE_ID(ce_ctrl_addr);
  220. struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
  221. u32 index;
  222. if (ar->hw_params.rri_on_ddr &&
  223. (ce_state->attr_flags & CE_ATTR_DIS_INTR))
  224. index = ath10k_ce_dest_ring_read_index_from_ddr(ar, ce_id);
  225. else
  226. index = ath10k_ce_read32(ar, ce_ctrl_addr +
  227. ar->hw_ce_regs->current_drri_addr);
  228. return index;
  229. }
  230. static inline void ath10k_ce_dest_ring_base_addr_set(struct ath10k *ar,
  231. u32 ce_id,
  232. u64 addr)
  233. {
  234. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  235. struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
  236. u32 ce_ctrl_addr = ath10k_ce_base_address(ar, ce_id);
  237. u32 addr_lo = lower_32_bits(addr);
  238. ath10k_ce_write32(ar, ce_ctrl_addr +
  239. ar->hw_ce_regs->dr_base_addr_lo, addr_lo);
  240. if (ce_state->ops->ce_set_dest_ring_base_addr_hi) {
  241. ce_state->ops->ce_set_dest_ring_base_addr_hi(ar, ce_ctrl_addr,
  242. addr);
  243. }
  244. }
  245. static void ath10k_ce_set_dest_ring_base_addr_hi(struct ath10k *ar,
  246. u32 ce_ctrl_addr,
  247. u64 addr)
  248. {
  249. u32 addr_hi = upper_32_bits(addr) & CE_DESC_ADDR_HI_MASK;
  250. u32 reg_value;
  251. reg_value = ath10k_ce_read32(ar, ce_ctrl_addr +
  252. ar->hw_ce_regs->dr_base_addr_hi);
  253. reg_value &= ~CE_DESC_ADDR_HI_MASK;
  254. reg_value |= addr_hi;
  255. ath10k_ce_write32(ar, ce_ctrl_addr +
  256. ar->hw_ce_regs->dr_base_addr_hi, reg_value);
  257. }
  258. static inline void ath10k_ce_dest_ring_size_set(struct ath10k *ar,
  259. u32 ce_ctrl_addr,
  260. unsigned int n)
  261. {
  262. ath10k_ce_write32(ar, ce_ctrl_addr +
  263. ar->hw_ce_regs->dr_size_addr, n);
  264. }
  265. static inline void ath10k_ce_src_ring_highmark_set(struct ath10k *ar,
  266. u32 ce_ctrl_addr,
  267. unsigned int n)
  268. {
  269. struct ath10k_hw_ce_dst_src_wm_regs *srcr_wm = ar->hw_ce_regs->wm_srcr;
  270. u32 addr = ath10k_ce_read32(ar, ce_ctrl_addr + srcr_wm->addr);
  271. ath10k_ce_write32(ar, ce_ctrl_addr + srcr_wm->addr,
  272. (addr & ~(srcr_wm->wm_high->mask)) |
  273. (ath10k_set_ring_byte(n, srcr_wm->wm_high)));
  274. }
  275. static inline void ath10k_ce_src_ring_lowmark_set(struct ath10k *ar,
  276. u32 ce_ctrl_addr,
  277. unsigned int n)
  278. {
  279. struct ath10k_hw_ce_dst_src_wm_regs *srcr_wm = ar->hw_ce_regs->wm_srcr;
  280. u32 addr = ath10k_ce_read32(ar, ce_ctrl_addr + srcr_wm->addr);
  281. ath10k_ce_write32(ar, ce_ctrl_addr + srcr_wm->addr,
  282. (addr & ~(srcr_wm->wm_low->mask)) |
  283. (ath10k_set_ring_byte(n, srcr_wm->wm_low)));
  284. }
  285. static inline void ath10k_ce_dest_ring_highmark_set(struct ath10k *ar,
  286. u32 ce_ctrl_addr,
  287. unsigned int n)
  288. {
  289. struct ath10k_hw_ce_dst_src_wm_regs *dstr_wm = ar->hw_ce_regs->wm_dstr;
  290. u32 addr = ath10k_ce_read32(ar, ce_ctrl_addr + dstr_wm->addr);
  291. ath10k_ce_write32(ar, ce_ctrl_addr + dstr_wm->addr,
  292. (addr & ~(dstr_wm->wm_high->mask)) |
  293. (ath10k_set_ring_byte(n, dstr_wm->wm_high)));
  294. }
  295. static inline void ath10k_ce_dest_ring_lowmark_set(struct ath10k *ar,
  296. u32 ce_ctrl_addr,
  297. unsigned int n)
  298. {
  299. struct ath10k_hw_ce_dst_src_wm_regs *dstr_wm = ar->hw_ce_regs->wm_dstr;
  300. u32 addr = ath10k_ce_read32(ar, ce_ctrl_addr + dstr_wm->addr);
  301. ath10k_ce_write32(ar, ce_ctrl_addr + dstr_wm->addr,
  302. (addr & ~(dstr_wm->wm_low->mask)) |
  303. (ath10k_set_ring_byte(n, dstr_wm->wm_low)));
  304. }
  305. static inline void ath10k_ce_copy_complete_inter_enable(struct ath10k *ar,
  306. u32 ce_ctrl_addr)
  307. {
  308. struct ath10k_hw_ce_host_ie *host_ie = ar->hw_ce_regs->host_ie;
  309. u32 host_ie_addr = ath10k_ce_read32(ar, ce_ctrl_addr +
  310. ar->hw_ce_regs->host_ie_addr);
  311. ath10k_ce_write32(ar, ce_ctrl_addr + ar->hw_ce_regs->host_ie_addr,
  312. host_ie_addr | host_ie->copy_complete->mask);
  313. }
  314. static inline void ath10k_ce_copy_complete_intr_disable(struct ath10k *ar,
  315. u32 ce_ctrl_addr)
  316. {
  317. struct ath10k_hw_ce_host_ie *host_ie = ar->hw_ce_regs->host_ie;
  318. u32 host_ie_addr = ath10k_ce_read32(ar, ce_ctrl_addr +
  319. ar->hw_ce_regs->host_ie_addr);
  320. ath10k_ce_write32(ar, ce_ctrl_addr + ar->hw_ce_regs->host_ie_addr,
  321. host_ie_addr & ~(host_ie->copy_complete->mask));
  322. }
  323. static inline void ath10k_ce_watermark_intr_disable(struct ath10k *ar,
  324. u32 ce_ctrl_addr)
  325. {
  326. struct ath10k_hw_ce_host_wm_regs *wm_regs = ar->hw_ce_regs->wm_regs;
  327. u32 host_ie_addr = ath10k_ce_read32(ar, ce_ctrl_addr +
  328. ar->hw_ce_regs->host_ie_addr);
  329. ath10k_ce_write32(ar, ce_ctrl_addr + ar->hw_ce_regs->host_ie_addr,
  330. host_ie_addr & ~(wm_regs->wm_mask));
  331. }
  332. static inline void ath10k_ce_error_intr_disable(struct ath10k *ar,
  333. u32 ce_ctrl_addr)
  334. {
  335. struct ath10k_hw_ce_misc_regs *misc_regs = ar->hw_ce_regs->misc_regs;
  336. u32 misc_ie_addr = ath10k_ce_read32(ar,
  337. ce_ctrl_addr + ar->hw_ce_regs->misc_ie_addr);
  338. ath10k_ce_write32(ar,
  339. ce_ctrl_addr + ar->hw_ce_regs->misc_ie_addr,
  340. misc_ie_addr & ~(misc_regs->err_mask));
  341. }
  342. static inline void ath10k_ce_engine_int_status_clear(struct ath10k *ar,
  343. u32 ce_ctrl_addr,
  344. unsigned int mask)
  345. {
  346. struct ath10k_hw_ce_host_wm_regs *wm_regs = ar->hw_ce_regs->wm_regs;
  347. ath10k_ce_write32(ar, ce_ctrl_addr + wm_regs->addr, mask);
  348. }
  349. /*
  350. * Guts of ath10k_ce_send.
  351. * The caller takes responsibility for any needed locking.
  352. */
  353. static int _ath10k_ce_send_nolock(struct ath10k_ce_pipe *ce_state,
  354. void *per_transfer_context,
  355. dma_addr_t buffer,
  356. unsigned int nbytes,
  357. unsigned int transfer_id,
  358. unsigned int flags)
  359. {
  360. struct ath10k *ar = ce_state->ar;
  361. struct ath10k_ce_ring *src_ring = ce_state->src_ring;
  362. struct ce_desc *desc, sdesc;
  363. unsigned int nentries_mask = src_ring->nentries_mask;
  364. unsigned int sw_index = src_ring->sw_index;
  365. unsigned int write_index = src_ring->write_index;
  366. u32 ctrl_addr = ce_state->ctrl_addr;
  367. u32 desc_flags = 0;
  368. int ret = 0;
  369. if (nbytes > ce_state->src_sz_max)
  370. ath10k_warn(ar, "%s: send more we can (nbytes: %d, max: %d)\n",
  371. __func__, nbytes, ce_state->src_sz_max);
  372. if (unlikely(CE_RING_DELTA(nentries_mask,
  373. write_index, sw_index - 1) <= 0)) {
  374. ret = -ENOSR;
  375. goto exit;
  376. }
  377. desc = CE_SRC_RING_TO_DESC(src_ring->base_addr_owner_space,
  378. write_index);
  379. desc_flags |= SM(transfer_id, CE_DESC_FLAGS_META_DATA);
  380. if (flags & CE_SEND_FLAG_GATHER)
  381. desc_flags |= CE_DESC_FLAGS_GATHER;
  382. if (flags & CE_SEND_FLAG_BYTE_SWAP)
  383. desc_flags |= CE_DESC_FLAGS_BYTE_SWAP;
  384. sdesc.addr = __cpu_to_le32(buffer);
  385. sdesc.nbytes = __cpu_to_le16(nbytes);
  386. sdesc.flags = __cpu_to_le16(desc_flags);
  387. *desc = sdesc;
  388. src_ring->per_transfer_context[write_index] = per_transfer_context;
  389. /* Update Source Ring Write Index */
  390. write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
  391. /* WORKAROUND */
  392. if (!(flags & CE_SEND_FLAG_GATHER))
  393. ath10k_ce_src_ring_write_index_set(ar, ctrl_addr, write_index);
  394. src_ring->write_index = write_index;
  395. exit:
  396. return ret;
  397. }
  398. static int _ath10k_ce_send_nolock_64(struct ath10k_ce_pipe *ce_state,
  399. void *per_transfer_context,
  400. dma_addr_t buffer,
  401. unsigned int nbytes,
  402. unsigned int transfer_id,
  403. unsigned int flags)
  404. {
  405. struct ath10k *ar = ce_state->ar;
  406. struct ath10k_ce_ring *src_ring = ce_state->src_ring;
  407. struct ce_desc_64 *desc, sdesc;
  408. unsigned int nentries_mask = src_ring->nentries_mask;
  409. unsigned int sw_index;
  410. unsigned int write_index = src_ring->write_index;
  411. u32 ctrl_addr = ce_state->ctrl_addr;
  412. __le32 *addr;
  413. u32 desc_flags = 0;
  414. int ret = 0;
  415. if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
  416. return -ESHUTDOWN;
  417. if (nbytes > ce_state->src_sz_max)
  418. ath10k_warn(ar, "%s: send more we can (nbytes: %d, max: %d)\n",
  419. __func__, nbytes, ce_state->src_sz_max);
  420. if (ar->hw_params.rri_on_ddr)
  421. sw_index = ath10k_ce_src_ring_read_index_from_ddr(ar, ce_state->id);
  422. else
  423. sw_index = src_ring->sw_index;
  424. if (unlikely(CE_RING_DELTA(nentries_mask,
  425. write_index, sw_index - 1) <= 0)) {
  426. ret = -ENOSR;
  427. goto exit;
  428. }
  429. desc = CE_SRC_RING_TO_DESC_64(src_ring->base_addr_owner_space,
  430. write_index);
  431. desc_flags |= SM(transfer_id, CE_DESC_FLAGS_META_DATA);
  432. if (flags & CE_SEND_FLAG_GATHER)
  433. desc_flags |= CE_DESC_FLAGS_GATHER;
  434. if (flags & CE_SEND_FLAG_BYTE_SWAP)
  435. desc_flags |= CE_DESC_FLAGS_BYTE_SWAP;
  436. addr = (__le32 *)&sdesc.addr;
  437. flags |= upper_32_bits(buffer) & CE_DESC_ADDR_HI_MASK;
  438. addr[0] = __cpu_to_le32(buffer);
  439. addr[1] = __cpu_to_le32(flags);
  440. if (flags & CE_SEND_FLAG_GATHER)
  441. addr[1] |= __cpu_to_le32(CE_WCN3990_DESC_FLAGS_GATHER);
  442. else
  443. addr[1] &= ~(__cpu_to_le32(CE_WCN3990_DESC_FLAGS_GATHER));
  444. sdesc.nbytes = __cpu_to_le16(nbytes);
  445. sdesc.flags = __cpu_to_le16(desc_flags);
  446. *desc = sdesc;
  447. src_ring->per_transfer_context[write_index] = per_transfer_context;
  448. /* Update Source Ring Write Index */
  449. write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
  450. if (!(flags & CE_SEND_FLAG_GATHER)) {
  451. if (ar->hw_params.shadow_reg_support)
  452. ath10k_ce_shadow_src_ring_write_index_set(ar, ce_state,
  453. write_index);
  454. else
  455. ath10k_ce_src_ring_write_index_set(ar, ctrl_addr,
  456. write_index);
  457. }
  458. src_ring->write_index = write_index;
  459. exit:
  460. return ret;
  461. }
  462. int ath10k_ce_send_nolock(struct ath10k_ce_pipe *ce_state,
  463. void *per_transfer_context,
  464. dma_addr_t buffer,
  465. unsigned int nbytes,
  466. unsigned int transfer_id,
  467. unsigned int flags)
  468. {
  469. return ce_state->ops->ce_send_nolock(ce_state, per_transfer_context,
  470. buffer, nbytes, transfer_id, flags);
  471. }
  472. EXPORT_SYMBOL(ath10k_ce_send_nolock);
  473. void __ath10k_ce_send_revert(struct ath10k_ce_pipe *pipe)
  474. {
  475. struct ath10k *ar = pipe->ar;
  476. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  477. struct ath10k_ce_ring *src_ring = pipe->src_ring;
  478. u32 ctrl_addr = pipe->ctrl_addr;
  479. lockdep_assert_held(&ce->ce_lock);
  480. /*
  481. * This function must be called only if there is an incomplete
  482. * scatter-gather transfer (before index register is updated)
  483. * that needs to be cleaned up.
  484. */
  485. if (WARN_ON_ONCE(src_ring->write_index == src_ring->sw_index))
  486. return;
  487. if (WARN_ON_ONCE(src_ring->write_index ==
  488. ath10k_ce_src_ring_write_index_get(ar, ctrl_addr)))
  489. return;
  490. src_ring->write_index--;
  491. src_ring->write_index &= src_ring->nentries_mask;
  492. src_ring->per_transfer_context[src_ring->write_index] = NULL;
  493. }
  494. EXPORT_SYMBOL(__ath10k_ce_send_revert);
  495. int ath10k_ce_send(struct ath10k_ce_pipe *ce_state,
  496. void *per_transfer_context,
  497. dma_addr_t buffer,
  498. unsigned int nbytes,
  499. unsigned int transfer_id,
  500. unsigned int flags)
  501. {
  502. struct ath10k *ar = ce_state->ar;
  503. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  504. int ret;
  505. spin_lock_bh(&ce->ce_lock);
  506. ret = ath10k_ce_send_nolock(ce_state, per_transfer_context,
  507. buffer, nbytes, transfer_id, flags);
  508. spin_unlock_bh(&ce->ce_lock);
  509. return ret;
  510. }
  511. EXPORT_SYMBOL(ath10k_ce_send);
  512. int ath10k_ce_num_free_src_entries(struct ath10k_ce_pipe *pipe)
  513. {
  514. struct ath10k *ar = pipe->ar;
  515. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  516. int delta;
  517. spin_lock_bh(&ce->ce_lock);
  518. delta = CE_RING_DELTA(pipe->src_ring->nentries_mask,
  519. pipe->src_ring->write_index,
  520. pipe->src_ring->sw_index - 1);
  521. spin_unlock_bh(&ce->ce_lock);
  522. return delta;
  523. }
  524. EXPORT_SYMBOL(ath10k_ce_num_free_src_entries);
  525. int __ath10k_ce_rx_num_free_bufs(struct ath10k_ce_pipe *pipe)
  526. {
  527. struct ath10k *ar = pipe->ar;
  528. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  529. struct ath10k_ce_ring *dest_ring = pipe->dest_ring;
  530. unsigned int nentries_mask = dest_ring->nentries_mask;
  531. unsigned int write_index = dest_ring->write_index;
  532. unsigned int sw_index = dest_ring->sw_index;
  533. lockdep_assert_held(&ce->ce_lock);
  534. return CE_RING_DELTA(nentries_mask, write_index, sw_index - 1);
  535. }
  536. EXPORT_SYMBOL(__ath10k_ce_rx_num_free_bufs);
  537. static int __ath10k_ce_rx_post_buf(struct ath10k_ce_pipe *pipe, void *ctx,
  538. dma_addr_t paddr)
  539. {
  540. struct ath10k *ar = pipe->ar;
  541. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  542. struct ath10k_ce_ring *dest_ring = pipe->dest_ring;
  543. unsigned int nentries_mask = dest_ring->nentries_mask;
  544. unsigned int write_index = dest_ring->write_index;
  545. unsigned int sw_index = dest_ring->sw_index;
  546. struct ce_desc *base = dest_ring->base_addr_owner_space;
  547. struct ce_desc *desc = CE_DEST_RING_TO_DESC(base, write_index);
  548. u32 ctrl_addr = pipe->ctrl_addr;
  549. lockdep_assert_held(&ce->ce_lock);
  550. if ((pipe->id != 5) &&
  551. CE_RING_DELTA(nentries_mask, write_index, sw_index - 1) == 0)
  552. return -ENOSPC;
  553. desc->addr = __cpu_to_le32(paddr);
  554. desc->nbytes = 0;
  555. dest_ring->per_transfer_context[write_index] = ctx;
  556. write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
  557. ath10k_ce_dest_ring_write_index_set(ar, ctrl_addr, write_index);
  558. dest_ring->write_index = write_index;
  559. return 0;
  560. }
  561. static int __ath10k_ce_rx_post_buf_64(struct ath10k_ce_pipe *pipe,
  562. void *ctx,
  563. dma_addr_t paddr)
  564. {
  565. struct ath10k *ar = pipe->ar;
  566. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  567. struct ath10k_ce_ring *dest_ring = pipe->dest_ring;
  568. unsigned int nentries_mask = dest_ring->nentries_mask;
  569. unsigned int write_index = dest_ring->write_index;
  570. unsigned int sw_index = dest_ring->sw_index;
  571. struct ce_desc_64 *base = dest_ring->base_addr_owner_space;
  572. struct ce_desc_64 *desc =
  573. CE_DEST_RING_TO_DESC_64(base, write_index);
  574. u32 ctrl_addr = pipe->ctrl_addr;
  575. lockdep_assert_held(&ce->ce_lock);
  576. if (CE_RING_DELTA(nentries_mask, write_index, sw_index - 1) == 0)
  577. return -ENOSPC;
  578. desc->addr = __cpu_to_le64(paddr);
  579. desc->addr &= __cpu_to_le64(CE_DESC_ADDR_MASK);
  580. desc->nbytes = 0;
  581. dest_ring->per_transfer_context[write_index] = ctx;
  582. write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
  583. ath10k_ce_dest_ring_write_index_set(ar, ctrl_addr, write_index);
  584. dest_ring->write_index = write_index;
  585. return 0;
  586. }
  587. void ath10k_ce_rx_update_write_idx(struct ath10k_ce_pipe *pipe, u32 nentries)
  588. {
  589. struct ath10k *ar = pipe->ar;
  590. struct ath10k_ce_ring *dest_ring = pipe->dest_ring;
  591. unsigned int nentries_mask = dest_ring->nentries_mask;
  592. unsigned int write_index = dest_ring->write_index;
  593. u32 ctrl_addr = pipe->ctrl_addr;
  594. u32 cur_write_idx = ath10k_ce_dest_ring_write_index_get(ar, ctrl_addr);
  595. /* Prevent CE ring stuck issue that will occur when ring is full.
  596. * Make sure that write index is 1 less than read index.
  597. */
  598. if (((cur_write_idx + nentries) & nentries_mask) == dest_ring->sw_index)
  599. nentries -= 1;
  600. write_index = CE_RING_IDX_ADD(nentries_mask, write_index, nentries);
  601. ath10k_ce_dest_ring_write_index_set(ar, ctrl_addr, write_index);
  602. dest_ring->write_index = write_index;
  603. }
  604. EXPORT_SYMBOL(ath10k_ce_rx_update_write_idx);
  605. int ath10k_ce_rx_post_buf(struct ath10k_ce_pipe *pipe, void *ctx,
  606. dma_addr_t paddr)
  607. {
  608. struct ath10k *ar = pipe->ar;
  609. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  610. int ret;
  611. spin_lock_bh(&ce->ce_lock);
  612. ret = pipe->ops->ce_rx_post_buf(pipe, ctx, paddr);
  613. spin_unlock_bh(&ce->ce_lock);
  614. return ret;
  615. }
  616. EXPORT_SYMBOL(ath10k_ce_rx_post_buf);
  617. /*
  618. * Guts of ath10k_ce_completed_recv_next.
  619. * The caller takes responsibility for any necessary locking.
  620. */
  621. static int
  622. _ath10k_ce_completed_recv_next_nolock(struct ath10k_ce_pipe *ce_state,
  623. void **per_transfer_contextp,
  624. unsigned int *nbytesp)
  625. {
  626. struct ath10k_ce_ring *dest_ring = ce_state->dest_ring;
  627. unsigned int nentries_mask = dest_ring->nentries_mask;
  628. unsigned int sw_index = dest_ring->sw_index;
  629. struct ce_desc *base = dest_ring->base_addr_owner_space;
  630. struct ce_desc *desc = CE_DEST_RING_TO_DESC(base, sw_index);
  631. struct ce_desc sdesc;
  632. u16 nbytes;
  633. /* Copy in one go for performance reasons */
  634. sdesc = *desc;
  635. nbytes = __le16_to_cpu(sdesc.nbytes);
  636. if (nbytes == 0) {
  637. /*
  638. * This closes a relatively unusual race where the Host
  639. * sees the updated DRRI before the update to the
  640. * corresponding descriptor has completed. We treat this
  641. * as a descriptor that is not yet done.
  642. */
  643. return -EIO;
  644. }
  645. desc->nbytes = 0;
  646. /* Return data from completed destination descriptor */
  647. *nbytesp = nbytes;
  648. if (per_transfer_contextp)
  649. *per_transfer_contextp =
  650. dest_ring->per_transfer_context[sw_index];
  651. /* Copy engine 5 (HTT Rx) will reuse the same transfer context.
  652. * So update transfer context all CEs except CE5.
  653. */
  654. if (ce_state->id != 5)
  655. dest_ring->per_transfer_context[sw_index] = NULL;
  656. /* Update sw_index */
  657. sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
  658. dest_ring->sw_index = sw_index;
  659. return 0;
  660. }
  661. static int
  662. _ath10k_ce_completed_recv_next_nolock_64(struct ath10k_ce_pipe *ce_state,
  663. void **per_transfer_contextp,
  664. unsigned int *nbytesp)
  665. {
  666. struct ath10k_ce_ring *dest_ring = ce_state->dest_ring;
  667. unsigned int nentries_mask = dest_ring->nentries_mask;
  668. unsigned int sw_index = dest_ring->sw_index;
  669. struct ce_desc_64 *base = dest_ring->base_addr_owner_space;
  670. struct ce_desc_64 *desc =
  671. CE_DEST_RING_TO_DESC_64(base, sw_index);
  672. struct ce_desc_64 sdesc;
  673. u16 nbytes;
  674. /* Copy in one go for performance reasons */
  675. sdesc = *desc;
  676. nbytes = __le16_to_cpu(sdesc.nbytes);
  677. if (nbytes == 0) {
  678. /* This closes a relatively unusual race where the Host
  679. * sees the updated DRRI before the update to the
  680. * corresponding descriptor has completed. We treat this
  681. * as a descriptor that is not yet done.
  682. */
  683. return -EIO;
  684. }
  685. desc->nbytes = 0;
  686. /* Return data from completed destination descriptor */
  687. *nbytesp = nbytes;
  688. if (per_transfer_contextp)
  689. *per_transfer_contextp =
  690. dest_ring->per_transfer_context[sw_index];
  691. /* Copy engine 5 (HTT Rx) will reuse the same transfer context.
  692. * So update transfer context all CEs except CE5.
  693. */
  694. if (ce_state->id != 5)
  695. dest_ring->per_transfer_context[sw_index] = NULL;
  696. /* Update sw_index */
  697. sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
  698. dest_ring->sw_index = sw_index;
  699. return 0;
  700. }
  701. int ath10k_ce_completed_recv_next_nolock(struct ath10k_ce_pipe *ce_state,
  702. void **per_transfer_ctx,
  703. unsigned int *nbytesp)
  704. {
  705. return ce_state->ops->ce_completed_recv_next_nolock(ce_state,
  706. per_transfer_ctx,
  707. nbytesp);
  708. }
  709. EXPORT_SYMBOL(ath10k_ce_completed_recv_next_nolock);
  710. int ath10k_ce_completed_recv_next(struct ath10k_ce_pipe *ce_state,
  711. void **per_transfer_contextp,
  712. unsigned int *nbytesp)
  713. {
  714. struct ath10k *ar = ce_state->ar;
  715. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  716. int ret;
  717. spin_lock_bh(&ce->ce_lock);
  718. ret = ce_state->ops->ce_completed_recv_next_nolock(ce_state,
  719. per_transfer_contextp,
  720. nbytesp);
  721. spin_unlock_bh(&ce->ce_lock);
  722. return ret;
  723. }
  724. EXPORT_SYMBOL(ath10k_ce_completed_recv_next);
  725. static int _ath10k_ce_revoke_recv_next(struct ath10k_ce_pipe *ce_state,
  726. void **per_transfer_contextp,
  727. dma_addr_t *bufferp)
  728. {
  729. struct ath10k_ce_ring *dest_ring;
  730. unsigned int nentries_mask;
  731. unsigned int sw_index;
  732. unsigned int write_index;
  733. int ret;
  734. struct ath10k *ar;
  735. struct ath10k_ce *ce;
  736. dest_ring = ce_state->dest_ring;
  737. if (!dest_ring)
  738. return -EIO;
  739. ar = ce_state->ar;
  740. ce = ath10k_ce_priv(ar);
  741. spin_lock_bh(&ce->ce_lock);
  742. nentries_mask = dest_ring->nentries_mask;
  743. sw_index = dest_ring->sw_index;
  744. write_index = dest_ring->write_index;
  745. if (write_index != sw_index) {
  746. struct ce_desc *base = dest_ring->base_addr_owner_space;
  747. struct ce_desc *desc = CE_DEST_RING_TO_DESC(base, sw_index);
  748. /* Return data from completed destination descriptor */
  749. *bufferp = __le32_to_cpu(desc->addr);
  750. if (per_transfer_contextp)
  751. *per_transfer_contextp =
  752. dest_ring->per_transfer_context[sw_index];
  753. /* sanity */
  754. dest_ring->per_transfer_context[sw_index] = NULL;
  755. desc->nbytes = 0;
  756. /* Update sw_index */
  757. sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
  758. dest_ring->sw_index = sw_index;
  759. ret = 0;
  760. } else {
  761. ret = -EIO;
  762. }
  763. spin_unlock_bh(&ce->ce_lock);
  764. return ret;
  765. }
  766. static int _ath10k_ce_revoke_recv_next_64(struct ath10k_ce_pipe *ce_state,
  767. void **per_transfer_contextp,
  768. dma_addr_t *bufferp)
  769. {
  770. struct ath10k_ce_ring *dest_ring;
  771. unsigned int nentries_mask;
  772. unsigned int sw_index;
  773. unsigned int write_index;
  774. int ret;
  775. struct ath10k *ar;
  776. struct ath10k_ce *ce;
  777. dest_ring = ce_state->dest_ring;
  778. if (!dest_ring)
  779. return -EIO;
  780. ar = ce_state->ar;
  781. ce = ath10k_ce_priv(ar);
  782. spin_lock_bh(&ce->ce_lock);
  783. nentries_mask = dest_ring->nentries_mask;
  784. sw_index = dest_ring->sw_index;
  785. write_index = dest_ring->write_index;
  786. if (write_index != sw_index) {
  787. struct ce_desc_64 *base = dest_ring->base_addr_owner_space;
  788. struct ce_desc_64 *desc =
  789. CE_DEST_RING_TO_DESC_64(base, sw_index);
  790. /* Return data from completed destination descriptor */
  791. *bufferp = __le64_to_cpu(desc->addr);
  792. if (per_transfer_contextp)
  793. *per_transfer_contextp =
  794. dest_ring->per_transfer_context[sw_index];
  795. /* sanity */
  796. dest_ring->per_transfer_context[sw_index] = NULL;
  797. desc->nbytes = 0;
  798. /* Update sw_index */
  799. sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
  800. dest_ring->sw_index = sw_index;
  801. ret = 0;
  802. } else {
  803. ret = -EIO;
  804. }
  805. spin_unlock_bh(&ce->ce_lock);
  806. return ret;
  807. }
  808. int ath10k_ce_revoke_recv_next(struct ath10k_ce_pipe *ce_state,
  809. void **per_transfer_contextp,
  810. dma_addr_t *bufferp)
  811. {
  812. return ce_state->ops->ce_revoke_recv_next(ce_state,
  813. per_transfer_contextp,
  814. bufferp);
  815. }
  816. EXPORT_SYMBOL(ath10k_ce_revoke_recv_next);
  817. /*
  818. * Guts of ath10k_ce_completed_send_next.
  819. * The caller takes responsibility for any necessary locking.
  820. */
  821. static int _ath10k_ce_completed_send_next_nolock(struct ath10k_ce_pipe *ce_state,
  822. void **per_transfer_contextp)
  823. {
  824. struct ath10k_ce_ring *src_ring = ce_state->src_ring;
  825. u32 ctrl_addr = ce_state->ctrl_addr;
  826. struct ath10k *ar = ce_state->ar;
  827. unsigned int nentries_mask = src_ring->nentries_mask;
  828. unsigned int sw_index = src_ring->sw_index;
  829. unsigned int read_index;
  830. struct ce_desc *desc;
  831. if (src_ring->hw_index == sw_index) {
  832. /*
  833. * The SW completion index has caught up with the cached
  834. * version of the HW completion index.
  835. * Update the cached HW completion index to see whether
  836. * the SW has really caught up to the HW, or if the cached
  837. * value of the HW index has become stale.
  838. */
  839. read_index = ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
  840. if (read_index == 0xffffffff)
  841. return -ENODEV;
  842. read_index &= nentries_mask;
  843. src_ring->hw_index = read_index;
  844. }
  845. if (ar->hw_params.rri_on_ddr)
  846. read_index = ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
  847. else
  848. read_index = src_ring->hw_index;
  849. if (read_index == sw_index)
  850. return -EIO;
  851. if (per_transfer_contextp)
  852. *per_transfer_contextp =
  853. src_ring->per_transfer_context[sw_index];
  854. /* sanity */
  855. src_ring->per_transfer_context[sw_index] = NULL;
  856. desc = CE_SRC_RING_TO_DESC(src_ring->base_addr_owner_space,
  857. sw_index);
  858. desc->nbytes = 0;
  859. /* Update sw_index */
  860. sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
  861. src_ring->sw_index = sw_index;
  862. return 0;
  863. }
  864. static int _ath10k_ce_completed_send_next_nolock_64(struct ath10k_ce_pipe *ce_state,
  865. void **per_transfer_contextp)
  866. {
  867. struct ath10k_ce_ring *src_ring = ce_state->src_ring;
  868. u32 ctrl_addr = ce_state->ctrl_addr;
  869. struct ath10k *ar = ce_state->ar;
  870. unsigned int nentries_mask = src_ring->nentries_mask;
  871. unsigned int sw_index = src_ring->sw_index;
  872. unsigned int read_index;
  873. struct ce_desc_64 *desc;
  874. if (src_ring->hw_index == sw_index) {
  875. /*
  876. * The SW completion index has caught up with the cached
  877. * version of the HW completion index.
  878. * Update the cached HW completion index to see whether
  879. * the SW has really caught up to the HW, or if the cached
  880. * value of the HW index has become stale.
  881. */
  882. read_index = ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
  883. if (read_index == 0xffffffff)
  884. return -ENODEV;
  885. read_index &= nentries_mask;
  886. src_ring->hw_index = read_index;
  887. }
  888. if (ar->hw_params.rri_on_ddr)
  889. read_index = ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
  890. else
  891. read_index = src_ring->hw_index;
  892. if (read_index == sw_index)
  893. return -EIO;
  894. if (per_transfer_contextp)
  895. *per_transfer_contextp =
  896. src_ring->per_transfer_context[sw_index];
  897. /* sanity */
  898. src_ring->per_transfer_context[sw_index] = NULL;
  899. desc = CE_SRC_RING_TO_DESC_64(src_ring->base_addr_owner_space,
  900. sw_index);
  901. desc->nbytes = 0;
  902. /* Update sw_index */
  903. sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
  904. src_ring->sw_index = sw_index;
  905. return 0;
  906. }
  907. int ath10k_ce_completed_send_next_nolock(struct ath10k_ce_pipe *ce_state,
  908. void **per_transfer_contextp)
  909. {
  910. return ce_state->ops->ce_completed_send_next_nolock(ce_state,
  911. per_transfer_contextp);
  912. }
  913. EXPORT_SYMBOL(ath10k_ce_completed_send_next_nolock);
  914. static void ath10k_ce_extract_desc_data(struct ath10k *ar,
  915. struct ath10k_ce_ring *src_ring,
  916. u32 sw_index,
  917. dma_addr_t *bufferp,
  918. u32 *nbytesp,
  919. u32 *transfer_idp)
  920. {
  921. struct ce_desc *base = src_ring->base_addr_owner_space;
  922. struct ce_desc *desc = CE_SRC_RING_TO_DESC(base, sw_index);
  923. /* Return data from completed source descriptor */
  924. *bufferp = __le32_to_cpu(desc->addr);
  925. *nbytesp = __le16_to_cpu(desc->nbytes);
  926. *transfer_idp = MS(__le16_to_cpu(desc->flags),
  927. CE_DESC_FLAGS_META_DATA);
  928. }
  929. static void ath10k_ce_extract_desc_data_64(struct ath10k *ar,
  930. struct ath10k_ce_ring *src_ring,
  931. u32 sw_index,
  932. dma_addr_t *bufferp,
  933. u32 *nbytesp,
  934. u32 *transfer_idp)
  935. {
  936. struct ce_desc_64 *base = src_ring->base_addr_owner_space;
  937. struct ce_desc_64 *desc =
  938. CE_SRC_RING_TO_DESC_64(base, sw_index);
  939. /* Return data from completed source descriptor */
  940. *bufferp = __le64_to_cpu(desc->addr);
  941. *nbytesp = __le16_to_cpu(desc->nbytes);
  942. *transfer_idp = MS(__le16_to_cpu(desc->flags),
  943. CE_DESC_FLAGS_META_DATA);
  944. }
  945. /* NB: Modeled after ath10k_ce_completed_send_next */
  946. int ath10k_ce_cancel_send_next(struct ath10k_ce_pipe *ce_state,
  947. void **per_transfer_contextp,
  948. dma_addr_t *bufferp,
  949. unsigned int *nbytesp,
  950. unsigned int *transfer_idp)
  951. {
  952. struct ath10k_ce_ring *src_ring;
  953. unsigned int nentries_mask;
  954. unsigned int sw_index;
  955. unsigned int write_index;
  956. int ret;
  957. struct ath10k *ar;
  958. struct ath10k_ce *ce;
  959. src_ring = ce_state->src_ring;
  960. if (!src_ring)
  961. return -EIO;
  962. ar = ce_state->ar;
  963. ce = ath10k_ce_priv(ar);
  964. spin_lock_bh(&ce->ce_lock);
  965. nentries_mask = src_ring->nentries_mask;
  966. sw_index = src_ring->sw_index;
  967. write_index = src_ring->write_index;
  968. if (write_index != sw_index) {
  969. ce_state->ops->ce_extract_desc_data(ar, src_ring, sw_index,
  970. bufferp, nbytesp,
  971. transfer_idp);
  972. if (per_transfer_contextp)
  973. *per_transfer_contextp =
  974. src_ring->per_transfer_context[sw_index];
  975. /* sanity */
  976. src_ring->per_transfer_context[sw_index] = NULL;
  977. /* Update sw_index */
  978. sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
  979. src_ring->sw_index = sw_index;
  980. ret = 0;
  981. } else {
  982. ret = -EIO;
  983. }
  984. spin_unlock_bh(&ce->ce_lock);
  985. return ret;
  986. }
  987. EXPORT_SYMBOL(ath10k_ce_cancel_send_next);
  988. int ath10k_ce_completed_send_next(struct ath10k_ce_pipe *ce_state,
  989. void **per_transfer_contextp)
  990. {
  991. struct ath10k *ar = ce_state->ar;
  992. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  993. int ret;
  994. spin_lock_bh(&ce->ce_lock);
  995. ret = ath10k_ce_completed_send_next_nolock(ce_state,
  996. per_transfer_contextp);
  997. spin_unlock_bh(&ce->ce_lock);
  998. return ret;
  999. }
  1000. EXPORT_SYMBOL(ath10k_ce_completed_send_next);
  1001. /*
  1002. * Guts of interrupt handler for per-engine interrupts on a particular CE.
  1003. *
  1004. * Invokes registered callbacks for recv_complete,
  1005. * send_complete, and watermarks.
  1006. */
  1007. void ath10k_ce_per_engine_service(struct ath10k *ar, unsigned int ce_id)
  1008. {
  1009. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  1010. struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
  1011. struct ath10k_hw_ce_host_wm_regs *wm_regs = ar->hw_ce_regs->wm_regs;
  1012. u32 ctrl_addr = ce_state->ctrl_addr;
  1013. /*
  1014. * Clear before handling
  1015. *
  1016. * Misc CE interrupts are not being handled, but still need
  1017. * to be cleared.
  1018. *
  1019. * NOTE: When the last copy engine interrupt is cleared the
  1020. * hardware will go to sleep. Once this happens any access to
  1021. * the CE registers can cause a hardware fault.
  1022. */
  1023. ath10k_ce_engine_int_status_clear(ar, ctrl_addr,
  1024. wm_regs->cc_mask | wm_regs->wm_mask);
  1025. if (ce_state->recv_cb)
  1026. ce_state->recv_cb(ce_state);
  1027. if (ce_state->send_cb)
  1028. ce_state->send_cb(ce_state);
  1029. }
  1030. EXPORT_SYMBOL(ath10k_ce_per_engine_service);
  1031. /*
  1032. * Handler for per-engine interrupts on ALL active CEs.
  1033. * This is used in cases where the system is sharing a
  1034. * single interrupt for all CEs
  1035. */
  1036. void ath10k_ce_per_engine_service_any(struct ath10k *ar)
  1037. {
  1038. int ce_id;
  1039. u32 intr_summary;
  1040. intr_summary = ath10k_ce_interrupt_summary(ar);
  1041. for (ce_id = 0; intr_summary && (ce_id < CE_COUNT); ce_id++) {
  1042. if (intr_summary & (1 << ce_id))
  1043. intr_summary &= ~(1 << ce_id);
  1044. else
  1045. /* no intr pending on this CE */
  1046. continue;
  1047. ath10k_ce_per_engine_service(ar, ce_id);
  1048. }
  1049. }
  1050. EXPORT_SYMBOL(ath10k_ce_per_engine_service_any);
  1051. /*
  1052. * Adjust interrupts for the copy complete handler.
  1053. * If it's needed for either send or recv, then unmask
  1054. * this interrupt; otherwise, mask it.
  1055. *
  1056. * Called with ce_lock held.
  1057. */
  1058. static void ath10k_ce_per_engine_handler_adjust(struct ath10k_ce_pipe *ce_state)
  1059. {
  1060. u32 ctrl_addr = ce_state->ctrl_addr;
  1061. struct ath10k *ar = ce_state->ar;
  1062. bool disable_copy_compl_intr = ce_state->attr_flags & CE_ATTR_DIS_INTR;
  1063. if ((!disable_copy_compl_intr) &&
  1064. (ce_state->send_cb || ce_state->recv_cb))
  1065. ath10k_ce_copy_complete_inter_enable(ar, ctrl_addr);
  1066. else
  1067. ath10k_ce_copy_complete_intr_disable(ar, ctrl_addr);
  1068. ath10k_ce_watermark_intr_disable(ar, ctrl_addr);
  1069. }
  1070. void ath10k_ce_disable_interrupt(struct ath10k *ar, int ce_id)
  1071. {
  1072. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  1073. struct ath10k_ce_pipe *ce_state;
  1074. u32 ctrl_addr;
  1075. ce_state = &ce->ce_states[ce_id];
  1076. if (ce_state->attr_flags & CE_ATTR_POLL)
  1077. return;
  1078. ctrl_addr = ath10k_ce_base_address(ar, ce_id);
  1079. ath10k_ce_copy_complete_intr_disable(ar, ctrl_addr);
  1080. ath10k_ce_error_intr_disable(ar, ctrl_addr);
  1081. ath10k_ce_watermark_intr_disable(ar, ctrl_addr);
  1082. }
  1083. EXPORT_SYMBOL(ath10k_ce_disable_interrupt);
  1084. void ath10k_ce_disable_interrupts(struct ath10k *ar)
  1085. {
  1086. int ce_id;
  1087. for (ce_id = 0; ce_id < CE_COUNT; ce_id++)
  1088. ath10k_ce_disable_interrupt(ar, ce_id);
  1089. }
  1090. EXPORT_SYMBOL(ath10k_ce_disable_interrupts);
  1091. void ath10k_ce_enable_interrupt(struct ath10k *ar, int ce_id)
  1092. {
  1093. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  1094. struct ath10k_ce_pipe *ce_state;
  1095. ce_state = &ce->ce_states[ce_id];
  1096. if (ce_state->attr_flags & CE_ATTR_POLL)
  1097. return;
  1098. ath10k_ce_per_engine_handler_adjust(ce_state);
  1099. }
  1100. EXPORT_SYMBOL(ath10k_ce_enable_interrupt);
  1101. void ath10k_ce_enable_interrupts(struct ath10k *ar)
  1102. {
  1103. int ce_id;
  1104. /* Enable interrupts for copy engine that
  1105. * are not using polling mode.
  1106. */
  1107. for (ce_id = 0; ce_id < CE_COUNT; ce_id++)
  1108. ath10k_ce_enable_interrupt(ar, ce_id);
  1109. }
  1110. EXPORT_SYMBOL(ath10k_ce_enable_interrupts);
  1111. static int ath10k_ce_init_src_ring(struct ath10k *ar,
  1112. unsigned int ce_id,
  1113. const struct ce_attr *attr)
  1114. {
  1115. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  1116. struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
  1117. struct ath10k_ce_ring *src_ring = ce_state->src_ring;
  1118. u32 nentries, ctrl_addr = ath10k_ce_base_address(ar, ce_id);
  1119. nentries = roundup_pow_of_two(attr->src_nentries);
  1120. if (ar->hw_params.target_64bit)
  1121. memset(src_ring->base_addr_owner_space, 0,
  1122. nentries * sizeof(struct ce_desc_64));
  1123. else
  1124. memset(src_ring->base_addr_owner_space, 0,
  1125. nentries * sizeof(struct ce_desc));
  1126. src_ring->sw_index = ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
  1127. src_ring->sw_index &= src_ring->nentries_mask;
  1128. src_ring->hw_index = src_ring->sw_index;
  1129. src_ring->write_index =
  1130. ath10k_ce_src_ring_write_index_get(ar, ctrl_addr);
  1131. src_ring->write_index &= src_ring->nentries_mask;
  1132. ath10k_ce_src_ring_base_addr_set(ar, ce_id,
  1133. src_ring->base_addr_ce_space);
  1134. ath10k_ce_src_ring_size_set(ar, ctrl_addr, nentries);
  1135. ath10k_ce_src_ring_dmax_set(ar, ctrl_addr, attr->src_sz_max);
  1136. ath10k_ce_src_ring_byte_swap_set(ar, ctrl_addr, 0);
  1137. ath10k_ce_src_ring_lowmark_set(ar, ctrl_addr, 0);
  1138. ath10k_ce_src_ring_highmark_set(ar, ctrl_addr, nentries);
  1139. ath10k_dbg(ar, ATH10K_DBG_BOOT,
  1140. "boot init ce src ring id %d entries %d base_addr %pK\n",
  1141. ce_id, nentries, src_ring->base_addr_owner_space);
  1142. return 0;
  1143. }
  1144. static int ath10k_ce_init_dest_ring(struct ath10k *ar,
  1145. unsigned int ce_id,
  1146. const struct ce_attr *attr)
  1147. {
  1148. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  1149. struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
  1150. struct ath10k_ce_ring *dest_ring = ce_state->dest_ring;
  1151. u32 nentries, ctrl_addr = ath10k_ce_base_address(ar, ce_id);
  1152. nentries = roundup_pow_of_two(attr->dest_nentries);
  1153. if (ar->hw_params.target_64bit)
  1154. memset(dest_ring->base_addr_owner_space, 0,
  1155. nentries * sizeof(struct ce_desc_64));
  1156. else
  1157. memset(dest_ring->base_addr_owner_space, 0,
  1158. nentries * sizeof(struct ce_desc));
  1159. dest_ring->sw_index = ath10k_ce_dest_ring_read_index_get(ar, ctrl_addr);
  1160. dest_ring->sw_index &= dest_ring->nentries_mask;
  1161. dest_ring->write_index =
  1162. ath10k_ce_dest_ring_write_index_get(ar, ctrl_addr);
  1163. dest_ring->write_index &= dest_ring->nentries_mask;
  1164. ath10k_ce_dest_ring_base_addr_set(ar, ce_id,
  1165. dest_ring->base_addr_ce_space);
  1166. ath10k_ce_dest_ring_size_set(ar, ctrl_addr, nentries);
  1167. ath10k_ce_dest_ring_byte_swap_set(ar, ctrl_addr, 0);
  1168. ath10k_ce_dest_ring_lowmark_set(ar, ctrl_addr, 0);
  1169. ath10k_ce_dest_ring_highmark_set(ar, ctrl_addr, nentries);
  1170. ath10k_dbg(ar, ATH10K_DBG_BOOT,
  1171. "boot ce dest ring id %d entries %d base_addr %pK\n",
  1172. ce_id, nentries, dest_ring->base_addr_owner_space);
  1173. return 0;
  1174. }
  1175. static int ath10k_ce_alloc_shadow_base(struct ath10k *ar,
  1176. struct ath10k_ce_ring *src_ring,
  1177. u32 nentries)
  1178. {
  1179. src_ring->shadow_base_unaligned = kcalloc(nentries,
  1180. sizeof(struct ce_desc_64),
  1181. GFP_KERNEL);
  1182. if (!src_ring->shadow_base_unaligned)
  1183. return -ENOMEM;
  1184. src_ring->shadow_base = (struct ce_desc_64 *)
  1185. PTR_ALIGN(src_ring->shadow_base_unaligned,
  1186. CE_DESC_RING_ALIGN);
  1187. return 0;
  1188. }
  1189. static struct ath10k_ce_ring *
  1190. ath10k_ce_alloc_src_ring(struct ath10k *ar, unsigned int ce_id,
  1191. const struct ce_attr *attr)
  1192. {
  1193. struct ath10k_ce_ring *src_ring;
  1194. u32 nentries = attr->src_nentries;
  1195. dma_addr_t base_addr;
  1196. int ret;
  1197. nentries = roundup_pow_of_two(nentries);
  1198. src_ring = kzalloc(struct_size(src_ring, per_transfer_context,
  1199. nentries), GFP_KERNEL);
  1200. if (src_ring == NULL)
  1201. return ERR_PTR(-ENOMEM);
  1202. src_ring->nentries = nentries;
  1203. src_ring->nentries_mask = nentries - 1;
  1204. /*
  1205. * Legacy platforms that do not support cache
  1206. * coherent DMA are unsupported
  1207. */
  1208. src_ring->base_addr_owner_space_unaligned =
  1209. dma_alloc_coherent(ar->dev,
  1210. (nentries * sizeof(struct ce_desc) +
  1211. CE_DESC_RING_ALIGN),
  1212. &base_addr, GFP_KERNEL);
  1213. if (!src_ring->base_addr_owner_space_unaligned) {
  1214. kfree(src_ring);
  1215. return ERR_PTR(-ENOMEM);
  1216. }
  1217. src_ring->base_addr_ce_space_unaligned = base_addr;
  1218. src_ring->base_addr_owner_space =
  1219. PTR_ALIGN(src_ring->base_addr_owner_space_unaligned,
  1220. CE_DESC_RING_ALIGN);
  1221. src_ring->base_addr_ce_space =
  1222. ALIGN(src_ring->base_addr_ce_space_unaligned,
  1223. CE_DESC_RING_ALIGN);
  1224. if (ar->hw_params.shadow_reg_support) {
  1225. ret = ath10k_ce_alloc_shadow_base(ar, src_ring, nentries);
  1226. if (ret) {
  1227. dma_free_coherent(ar->dev,
  1228. (nentries * sizeof(struct ce_desc) +
  1229. CE_DESC_RING_ALIGN),
  1230. src_ring->base_addr_owner_space_unaligned,
  1231. base_addr);
  1232. kfree(src_ring);
  1233. return ERR_PTR(ret);
  1234. }
  1235. }
  1236. return src_ring;
  1237. }
  1238. static struct ath10k_ce_ring *
  1239. ath10k_ce_alloc_src_ring_64(struct ath10k *ar, unsigned int ce_id,
  1240. const struct ce_attr *attr)
  1241. {
  1242. struct ath10k_ce_ring *src_ring;
  1243. u32 nentries = attr->src_nentries;
  1244. dma_addr_t base_addr;
  1245. int ret;
  1246. nentries = roundup_pow_of_two(nentries);
  1247. src_ring = kzalloc(struct_size(src_ring, per_transfer_context,
  1248. nentries), GFP_KERNEL);
  1249. if (!src_ring)
  1250. return ERR_PTR(-ENOMEM);
  1251. src_ring->nentries = nentries;
  1252. src_ring->nentries_mask = nentries - 1;
  1253. /* Legacy platforms that do not support cache
  1254. * coherent DMA are unsupported
  1255. */
  1256. src_ring->base_addr_owner_space_unaligned =
  1257. dma_alloc_coherent(ar->dev,
  1258. (nentries * sizeof(struct ce_desc_64) +
  1259. CE_DESC_RING_ALIGN),
  1260. &base_addr, GFP_KERNEL);
  1261. if (!src_ring->base_addr_owner_space_unaligned) {
  1262. kfree(src_ring);
  1263. return ERR_PTR(-ENOMEM);
  1264. }
  1265. src_ring->base_addr_ce_space_unaligned = base_addr;
  1266. src_ring->base_addr_owner_space =
  1267. PTR_ALIGN(src_ring->base_addr_owner_space_unaligned,
  1268. CE_DESC_RING_ALIGN);
  1269. src_ring->base_addr_ce_space =
  1270. ALIGN(src_ring->base_addr_ce_space_unaligned,
  1271. CE_DESC_RING_ALIGN);
  1272. if (ar->hw_params.shadow_reg_support) {
  1273. ret = ath10k_ce_alloc_shadow_base(ar, src_ring, nentries);
  1274. if (ret) {
  1275. dma_free_coherent(ar->dev,
  1276. (nentries * sizeof(struct ce_desc_64) +
  1277. CE_DESC_RING_ALIGN),
  1278. src_ring->base_addr_owner_space_unaligned,
  1279. base_addr);
  1280. kfree(src_ring);
  1281. return ERR_PTR(ret);
  1282. }
  1283. }
  1284. return src_ring;
  1285. }
  1286. static struct ath10k_ce_ring *
  1287. ath10k_ce_alloc_dest_ring(struct ath10k *ar, unsigned int ce_id,
  1288. const struct ce_attr *attr)
  1289. {
  1290. struct ath10k_ce_ring *dest_ring;
  1291. u32 nentries;
  1292. dma_addr_t base_addr;
  1293. nentries = roundup_pow_of_two(attr->dest_nentries);
  1294. dest_ring = kzalloc(struct_size(dest_ring, per_transfer_context,
  1295. nentries), GFP_KERNEL);
  1296. if (dest_ring == NULL)
  1297. return ERR_PTR(-ENOMEM);
  1298. dest_ring->nentries = nentries;
  1299. dest_ring->nentries_mask = nentries - 1;
  1300. /*
  1301. * Legacy platforms that do not support cache
  1302. * coherent DMA are unsupported
  1303. */
  1304. dest_ring->base_addr_owner_space_unaligned =
  1305. dma_alloc_coherent(ar->dev,
  1306. (nentries * sizeof(struct ce_desc) +
  1307. CE_DESC_RING_ALIGN),
  1308. &base_addr, GFP_KERNEL);
  1309. if (!dest_ring->base_addr_owner_space_unaligned) {
  1310. kfree(dest_ring);
  1311. return ERR_PTR(-ENOMEM);
  1312. }
  1313. dest_ring->base_addr_ce_space_unaligned = base_addr;
  1314. dest_ring->base_addr_owner_space =
  1315. PTR_ALIGN(dest_ring->base_addr_owner_space_unaligned,
  1316. CE_DESC_RING_ALIGN);
  1317. dest_ring->base_addr_ce_space =
  1318. ALIGN(dest_ring->base_addr_ce_space_unaligned,
  1319. CE_DESC_RING_ALIGN);
  1320. return dest_ring;
  1321. }
  1322. static struct ath10k_ce_ring *
  1323. ath10k_ce_alloc_dest_ring_64(struct ath10k *ar, unsigned int ce_id,
  1324. const struct ce_attr *attr)
  1325. {
  1326. struct ath10k_ce_ring *dest_ring;
  1327. u32 nentries;
  1328. dma_addr_t base_addr;
  1329. nentries = roundup_pow_of_two(attr->dest_nentries);
  1330. dest_ring = kzalloc(struct_size(dest_ring, per_transfer_context,
  1331. nentries), GFP_KERNEL);
  1332. if (!dest_ring)
  1333. return ERR_PTR(-ENOMEM);
  1334. dest_ring->nentries = nentries;
  1335. dest_ring->nentries_mask = nentries - 1;
  1336. /* Legacy platforms that do not support cache
  1337. * coherent DMA are unsupported
  1338. */
  1339. dest_ring->base_addr_owner_space_unaligned =
  1340. dma_alloc_coherent(ar->dev,
  1341. (nentries * sizeof(struct ce_desc_64) +
  1342. CE_DESC_RING_ALIGN),
  1343. &base_addr, GFP_KERNEL);
  1344. if (!dest_ring->base_addr_owner_space_unaligned) {
  1345. kfree(dest_ring);
  1346. return ERR_PTR(-ENOMEM);
  1347. }
  1348. dest_ring->base_addr_ce_space_unaligned = base_addr;
  1349. /* Correctly initialize memory to 0 to prevent garbage
  1350. * data crashing system when download firmware
  1351. */
  1352. dest_ring->base_addr_owner_space =
  1353. PTR_ALIGN(dest_ring->base_addr_owner_space_unaligned,
  1354. CE_DESC_RING_ALIGN);
  1355. dest_ring->base_addr_ce_space =
  1356. ALIGN(dest_ring->base_addr_ce_space_unaligned,
  1357. CE_DESC_RING_ALIGN);
  1358. return dest_ring;
  1359. }
  1360. /*
  1361. * Initialize a Copy Engine based on caller-supplied attributes.
  1362. * This may be called once to initialize both source and destination
  1363. * rings or it may be called twice for separate source and destination
  1364. * initialization. It may be that only one side or the other is
  1365. * initialized by software/firmware.
  1366. */
  1367. int ath10k_ce_init_pipe(struct ath10k *ar, unsigned int ce_id,
  1368. const struct ce_attr *attr)
  1369. {
  1370. int ret;
  1371. if (attr->src_nentries) {
  1372. ret = ath10k_ce_init_src_ring(ar, ce_id, attr);
  1373. if (ret) {
  1374. ath10k_err(ar, "Failed to initialize CE src ring for ID: %d (%d)\n",
  1375. ce_id, ret);
  1376. return ret;
  1377. }
  1378. }
  1379. if (attr->dest_nentries) {
  1380. ret = ath10k_ce_init_dest_ring(ar, ce_id, attr);
  1381. if (ret) {
  1382. ath10k_err(ar, "Failed to initialize CE dest ring for ID: %d (%d)\n",
  1383. ce_id, ret);
  1384. return ret;
  1385. }
  1386. }
  1387. return 0;
  1388. }
  1389. EXPORT_SYMBOL(ath10k_ce_init_pipe);
  1390. static void ath10k_ce_deinit_src_ring(struct ath10k *ar, unsigned int ce_id)
  1391. {
  1392. u32 ctrl_addr = ath10k_ce_base_address(ar, ce_id);
  1393. ath10k_ce_src_ring_base_addr_set(ar, ce_id, 0);
  1394. ath10k_ce_src_ring_size_set(ar, ctrl_addr, 0);
  1395. ath10k_ce_src_ring_dmax_set(ar, ctrl_addr, 0);
  1396. ath10k_ce_src_ring_highmark_set(ar, ctrl_addr, 0);
  1397. }
  1398. static void ath10k_ce_deinit_dest_ring(struct ath10k *ar, unsigned int ce_id)
  1399. {
  1400. u32 ctrl_addr = ath10k_ce_base_address(ar, ce_id);
  1401. ath10k_ce_dest_ring_base_addr_set(ar, ce_id, 0);
  1402. ath10k_ce_dest_ring_size_set(ar, ctrl_addr, 0);
  1403. ath10k_ce_dest_ring_highmark_set(ar, ctrl_addr, 0);
  1404. }
  1405. void ath10k_ce_deinit_pipe(struct ath10k *ar, unsigned int ce_id)
  1406. {
  1407. ath10k_ce_deinit_src_ring(ar, ce_id);
  1408. ath10k_ce_deinit_dest_ring(ar, ce_id);
  1409. }
  1410. EXPORT_SYMBOL(ath10k_ce_deinit_pipe);
  1411. static void _ath10k_ce_free_pipe(struct ath10k *ar, int ce_id)
  1412. {
  1413. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  1414. struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
  1415. if (ce_state->src_ring) {
  1416. if (ar->hw_params.shadow_reg_support)
  1417. kfree(ce_state->src_ring->shadow_base_unaligned);
  1418. dma_free_coherent(ar->dev,
  1419. (ce_state->src_ring->nentries *
  1420. sizeof(struct ce_desc) +
  1421. CE_DESC_RING_ALIGN),
  1422. ce_state->src_ring->base_addr_owner_space,
  1423. ce_state->src_ring->base_addr_ce_space);
  1424. kfree(ce_state->src_ring);
  1425. }
  1426. if (ce_state->dest_ring) {
  1427. dma_free_coherent(ar->dev,
  1428. (ce_state->dest_ring->nentries *
  1429. sizeof(struct ce_desc) +
  1430. CE_DESC_RING_ALIGN),
  1431. ce_state->dest_ring->base_addr_owner_space,
  1432. ce_state->dest_ring->base_addr_ce_space);
  1433. kfree(ce_state->dest_ring);
  1434. }
  1435. ce_state->src_ring = NULL;
  1436. ce_state->dest_ring = NULL;
  1437. }
  1438. static void _ath10k_ce_free_pipe_64(struct ath10k *ar, int ce_id)
  1439. {
  1440. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  1441. struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
  1442. if (ce_state->src_ring) {
  1443. if (ar->hw_params.shadow_reg_support)
  1444. kfree(ce_state->src_ring->shadow_base_unaligned);
  1445. dma_free_coherent(ar->dev,
  1446. (ce_state->src_ring->nentries *
  1447. sizeof(struct ce_desc_64) +
  1448. CE_DESC_RING_ALIGN),
  1449. ce_state->src_ring->base_addr_owner_space,
  1450. ce_state->src_ring->base_addr_ce_space);
  1451. kfree(ce_state->src_ring);
  1452. }
  1453. if (ce_state->dest_ring) {
  1454. dma_free_coherent(ar->dev,
  1455. (ce_state->dest_ring->nentries *
  1456. sizeof(struct ce_desc_64) +
  1457. CE_DESC_RING_ALIGN),
  1458. ce_state->dest_ring->base_addr_owner_space,
  1459. ce_state->dest_ring->base_addr_ce_space);
  1460. kfree(ce_state->dest_ring);
  1461. }
  1462. ce_state->src_ring = NULL;
  1463. ce_state->dest_ring = NULL;
  1464. }
  1465. void ath10k_ce_free_pipe(struct ath10k *ar, int ce_id)
  1466. {
  1467. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  1468. struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
  1469. ce_state->ops->ce_free_pipe(ar, ce_id);
  1470. }
  1471. EXPORT_SYMBOL(ath10k_ce_free_pipe);
  1472. void ath10k_ce_dump_registers(struct ath10k *ar,
  1473. struct ath10k_fw_crash_data *crash_data)
  1474. {
  1475. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  1476. struct ath10k_ce_crash_data ce_data;
  1477. u32 addr, id;
  1478. lockdep_assert_held(&ar->dump_mutex);
  1479. ath10k_err(ar, "Copy Engine register dump:\n");
  1480. spin_lock_bh(&ce->ce_lock);
  1481. for (id = 0; id < CE_COUNT; id++) {
  1482. addr = ath10k_ce_base_address(ar, id);
  1483. ce_data.base_addr = cpu_to_le32(addr);
  1484. ce_data.src_wr_idx =
  1485. cpu_to_le32(ath10k_ce_src_ring_write_index_get(ar, addr));
  1486. ce_data.src_r_idx =
  1487. cpu_to_le32(ath10k_ce_src_ring_read_index_get(ar, addr));
  1488. ce_data.dst_wr_idx =
  1489. cpu_to_le32(ath10k_ce_dest_ring_write_index_get(ar, addr));
  1490. ce_data.dst_r_idx =
  1491. cpu_to_le32(ath10k_ce_dest_ring_read_index_get(ar, addr));
  1492. if (crash_data)
  1493. crash_data->ce_crash_data[id] = ce_data;
  1494. ath10k_err(ar, "[%02d]: 0x%08x %3u %3u %3u %3u", id,
  1495. le32_to_cpu(ce_data.base_addr),
  1496. le32_to_cpu(ce_data.src_wr_idx),
  1497. le32_to_cpu(ce_data.src_r_idx),
  1498. le32_to_cpu(ce_data.dst_wr_idx),
  1499. le32_to_cpu(ce_data.dst_r_idx));
  1500. }
  1501. spin_unlock_bh(&ce->ce_lock);
  1502. }
  1503. EXPORT_SYMBOL(ath10k_ce_dump_registers);
  1504. static const struct ath10k_ce_ops ce_ops = {
  1505. .ce_alloc_src_ring = ath10k_ce_alloc_src_ring,
  1506. .ce_alloc_dst_ring = ath10k_ce_alloc_dest_ring,
  1507. .ce_rx_post_buf = __ath10k_ce_rx_post_buf,
  1508. .ce_completed_recv_next_nolock = _ath10k_ce_completed_recv_next_nolock,
  1509. .ce_revoke_recv_next = _ath10k_ce_revoke_recv_next,
  1510. .ce_extract_desc_data = ath10k_ce_extract_desc_data,
  1511. .ce_free_pipe = _ath10k_ce_free_pipe,
  1512. .ce_send_nolock = _ath10k_ce_send_nolock,
  1513. .ce_set_src_ring_base_addr_hi = NULL,
  1514. .ce_set_dest_ring_base_addr_hi = NULL,
  1515. .ce_completed_send_next_nolock = _ath10k_ce_completed_send_next_nolock,
  1516. };
  1517. static const struct ath10k_ce_ops ce_64_ops = {
  1518. .ce_alloc_src_ring = ath10k_ce_alloc_src_ring_64,
  1519. .ce_alloc_dst_ring = ath10k_ce_alloc_dest_ring_64,
  1520. .ce_rx_post_buf = __ath10k_ce_rx_post_buf_64,
  1521. .ce_completed_recv_next_nolock =
  1522. _ath10k_ce_completed_recv_next_nolock_64,
  1523. .ce_revoke_recv_next = _ath10k_ce_revoke_recv_next_64,
  1524. .ce_extract_desc_data = ath10k_ce_extract_desc_data_64,
  1525. .ce_free_pipe = _ath10k_ce_free_pipe_64,
  1526. .ce_send_nolock = _ath10k_ce_send_nolock_64,
  1527. .ce_set_src_ring_base_addr_hi = ath10k_ce_set_src_ring_base_addr_hi,
  1528. .ce_set_dest_ring_base_addr_hi = ath10k_ce_set_dest_ring_base_addr_hi,
  1529. .ce_completed_send_next_nolock = _ath10k_ce_completed_send_next_nolock_64,
  1530. };
  1531. static void ath10k_ce_set_ops(struct ath10k *ar,
  1532. struct ath10k_ce_pipe *ce_state)
  1533. {
  1534. switch (ar->hw_rev) {
  1535. case ATH10K_HW_WCN3990:
  1536. ce_state->ops = &ce_64_ops;
  1537. break;
  1538. default:
  1539. ce_state->ops = &ce_ops;
  1540. break;
  1541. }
  1542. }
  1543. int ath10k_ce_alloc_pipe(struct ath10k *ar, int ce_id,
  1544. const struct ce_attr *attr)
  1545. {
  1546. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  1547. struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
  1548. int ret;
  1549. ath10k_ce_set_ops(ar, ce_state);
  1550. /* Make sure there's enough CE ringbuffer entries for HTT TX to avoid
  1551. * additional TX locking checks.
  1552. *
  1553. * For the lack of a better place do the check here.
  1554. */
  1555. BUILD_BUG_ON(2 * TARGET_NUM_MSDU_DESC >
  1556. (CE_HTT_H2T_MSG_SRC_NENTRIES - 1));
  1557. BUILD_BUG_ON(2 * TARGET_10_4_NUM_MSDU_DESC_PFC >
  1558. (CE_HTT_H2T_MSG_SRC_NENTRIES - 1));
  1559. BUILD_BUG_ON(2 * TARGET_TLV_NUM_MSDU_DESC >
  1560. (CE_HTT_H2T_MSG_SRC_NENTRIES - 1));
  1561. ce_state->ar = ar;
  1562. ce_state->id = ce_id;
  1563. ce_state->ctrl_addr = ath10k_ce_base_address(ar, ce_id);
  1564. ce_state->attr_flags = attr->flags;
  1565. ce_state->src_sz_max = attr->src_sz_max;
  1566. if (attr->src_nentries)
  1567. ce_state->send_cb = attr->send_cb;
  1568. if (attr->dest_nentries)
  1569. ce_state->recv_cb = attr->recv_cb;
  1570. if (attr->src_nentries) {
  1571. ce_state->src_ring =
  1572. ce_state->ops->ce_alloc_src_ring(ar, ce_id, attr);
  1573. if (IS_ERR(ce_state->src_ring)) {
  1574. ret = PTR_ERR(ce_state->src_ring);
  1575. ath10k_err(ar, "failed to alloc CE src ring %d: %d\n",
  1576. ce_id, ret);
  1577. ce_state->src_ring = NULL;
  1578. return ret;
  1579. }
  1580. }
  1581. if (attr->dest_nentries) {
  1582. ce_state->dest_ring = ce_state->ops->ce_alloc_dst_ring(ar,
  1583. ce_id,
  1584. attr);
  1585. if (IS_ERR(ce_state->dest_ring)) {
  1586. ret = PTR_ERR(ce_state->dest_ring);
  1587. ath10k_err(ar, "failed to alloc CE dest ring %d: %d\n",
  1588. ce_id, ret);
  1589. ce_state->dest_ring = NULL;
  1590. return ret;
  1591. }
  1592. }
  1593. return 0;
  1594. }
  1595. EXPORT_SYMBOL(ath10k_ce_alloc_pipe);
  1596. void ath10k_ce_alloc_rri(struct ath10k *ar)
  1597. {
  1598. int i;
  1599. u32 value;
  1600. u32 ctrl1_regs;
  1601. u32 ce_base_addr;
  1602. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  1603. ce->vaddr_rri = dma_alloc_coherent(ar->dev,
  1604. (CE_COUNT * sizeof(u32)),
  1605. &ce->paddr_rri, GFP_KERNEL);
  1606. if (!ce->vaddr_rri)
  1607. return;
  1608. ath10k_ce_write32(ar, ar->hw_ce_regs->ce_rri_low,
  1609. lower_32_bits(ce->paddr_rri));
  1610. ath10k_ce_write32(ar, ar->hw_ce_regs->ce_rri_high,
  1611. (upper_32_bits(ce->paddr_rri) &
  1612. CE_DESC_ADDR_HI_MASK));
  1613. for (i = 0; i < CE_COUNT; i++) {
  1614. ctrl1_regs = ar->hw_ce_regs->ctrl1_regs->addr;
  1615. ce_base_addr = ath10k_ce_base_address(ar, i);
  1616. value = ath10k_ce_read32(ar, ce_base_addr + ctrl1_regs);
  1617. value |= ar->hw_ce_regs->upd->mask;
  1618. ath10k_ce_write32(ar, ce_base_addr + ctrl1_regs, value);
  1619. }
  1620. }
  1621. EXPORT_SYMBOL(ath10k_ce_alloc_rri);
  1622. void ath10k_ce_free_rri(struct ath10k *ar)
  1623. {
  1624. struct ath10k_ce *ce = ath10k_ce_priv(ar);
  1625. dma_free_coherent(ar->dev, (CE_COUNT * sizeof(u32)),
  1626. ce->vaddr_rri,
  1627. ce->paddr_rri);
  1628. }
  1629. EXPORT_SYMBOL(ath10k_ce_free_rri);