ce.c 56 KB

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