pci.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848
  1. // SPDX-License-Identifier: BSD-3-Clause-Clear
  2. /*
  3. * Copyright (c) 2019-2021 The Linux Foundation. All rights reserved.
  4. * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/msi.h>
  8. #include <linux/pci.h>
  9. #include <linux/time.h>
  10. #include <linux/vmalloc.h>
  11. #include "pci.h"
  12. #include "core.h"
  13. #include "hif.h"
  14. #include "mhi.h"
  15. #include "debug.h"
  16. #include "hal.h"
  17. #define ATH12K_PCI_BAR_NUM 0
  18. #define ATH12K_PCI_DMA_MASK 36
  19. #define ATH12K_PCI_IRQ_CE0_OFFSET 3
  20. #define WINDOW_ENABLE_BIT 0x40000000
  21. #define WINDOW_VALUE_MASK GENMASK(24, 19)
  22. #define WINDOW_START 0x80000
  23. #define WINDOW_RANGE_MASK GENMASK(18, 0)
  24. #define WINDOW_STATIC_MASK GENMASK(31, 6)
  25. /* BAR0 + 4k is always accessible, and no
  26. * need to force wakeup.
  27. * 4K - 32 = 0xFE0
  28. */
  29. #define ACCESS_ALWAYS_OFF 0xFE0
  30. static struct ath12k_pci_driver *ath12k_pci_family_drivers[ATH12K_DEVICE_FAMILY_MAX];
  31. static const struct ath12k_msi_config msi_config_one_msi = {
  32. .total_vectors = 1,
  33. .total_users = 4,
  34. .users = (struct ath12k_msi_user[]) {
  35. { .name = "MHI", .num_vectors = 3, .base_vector = 0 },
  36. { .name = "CE", .num_vectors = 1, .base_vector = 0 },
  37. { .name = "WAKE", .num_vectors = 1, .base_vector = 0 },
  38. { .name = "DP", .num_vectors = 1, .base_vector = 0 },
  39. },
  40. };
  41. static const char *irq_name[ATH12K_IRQ_NUM_MAX] = {
  42. "bhi",
  43. "mhi-er0",
  44. "mhi-er1",
  45. "ce0",
  46. "ce1",
  47. "ce2",
  48. "ce3",
  49. "ce4",
  50. "ce5",
  51. "ce6",
  52. "ce7",
  53. "ce8",
  54. "ce9",
  55. "ce10",
  56. "ce11",
  57. "ce12",
  58. "ce13",
  59. "ce14",
  60. "ce15",
  61. "host2wbm-desc-feed",
  62. "host2reo-re-injection",
  63. "host2reo-command",
  64. "host2rxdma-monitor-ring3",
  65. "host2rxdma-monitor-ring2",
  66. "host2rxdma-monitor-ring1",
  67. "reo2ost-exception",
  68. "wbm2host-rx-release",
  69. "reo2host-status",
  70. "reo2host-destination-ring4",
  71. "reo2host-destination-ring3",
  72. "reo2host-destination-ring2",
  73. "reo2host-destination-ring1",
  74. "rxdma2host-monitor-destination-mac3",
  75. "rxdma2host-monitor-destination-mac2",
  76. "rxdma2host-monitor-destination-mac1",
  77. "ppdu-end-interrupts-mac3",
  78. "ppdu-end-interrupts-mac2",
  79. "ppdu-end-interrupts-mac1",
  80. "rxdma2host-monitor-status-ring-mac3",
  81. "rxdma2host-monitor-status-ring-mac2",
  82. "rxdma2host-monitor-status-ring-mac1",
  83. "host2rxdma-host-buf-ring-mac3",
  84. "host2rxdma-host-buf-ring-mac2",
  85. "host2rxdma-host-buf-ring-mac1",
  86. "rxdma2host-destination-ring-mac3",
  87. "rxdma2host-destination-ring-mac2",
  88. "rxdma2host-destination-ring-mac1",
  89. "host2tcl-input-ring4",
  90. "host2tcl-input-ring3",
  91. "host2tcl-input-ring2",
  92. "host2tcl-input-ring1",
  93. "wbm2host-tx-completions-ring4",
  94. "wbm2host-tx-completions-ring3",
  95. "wbm2host-tx-completions-ring2",
  96. "wbm2host-tx-completions-ring1",
  97. "tcl2host-status-ring",
  98. };
  99. static void ath12k_pci_select_window(struct ath12k_pci *ab_pci, u32 offset)
  100. {
  101. struct ath12k_base *ab = ab_pci->ab;
  102. u32 window = u32_get_bits(offset, WINDOW_VALUE_MASK);
  103. u32 static_window;
  104. lockdep_assert_held(&ab_pci->window_lock);
  105. /* Preserve the static window configuration and reset only dynamic window */
  106. static_window = ab_pci->register_window & WINDOW_STATIC_MASK;
  107. window |= static_window;
  108. if (window != ab_pci->register_window) {
  109. iowrite32(WINDOW_ENABLE_BIT | window,
  110. ab->mem + ab_pci->window_reg_addr);
  111. ioread32(ab->mem + ab_pci->window_reg_addr);
  112. ab_pci->register_window = window;
  113. }
  114. }
  115. static void ath12k_pci_select_static_window(struct ath12k_pci *ab_pci)
  116. {
  117. u32 umac_window;
  118. u32 ce_window;
  119. u32 window;
  120. umac_window = u32_get_bits(ab_pci->reg_base->umac_base, WINDOW_VALUE_MASK);
  121. ce_window = u32_get_bits(ab_pci->reg_base->ce_reg_base, WINDOW_VALUE_MASK);
  122. window = (umac_window << 12) | (ce_window << 6);
  123. spin_lock_bh(&ab_pci->window_lock);
  124. ab_pci->register_window = window;
  125. spin_unlock_bh(&ab_pci->window_lock);
  126. iowrite32(WINDOW_ENABLE_BIT | window, ab_pci->ab->mem + ab_pci->window_reg_addr);
  127. }
  128. static u32 ath12k_pci_get_window_start(struct ath12k_base *ab,
  129. u32 offset)
  130. {
  131. struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
  132. u32 window_start;
  133. /* If offset lies within DP register range, use 3rd window */
  134. if ((offset ^ ab_pci->reg_base->umac_base) < WINDOW_RANGE_MASK)
  135. window_start = 3 * WINDOW_START;
  136. /* If offset lies within CE register range, use 2nd window */
  137. else if ((offset ^ ab_pci->reg_base->ce_reg_base) < WINDOW_RANGE_MASK)
  138. window_start = 2 * WINDOW_START;
  139. else
  140. window_start = WINDOW_START;
  141. return window_start;
  142. }
  143. static inline bool ath12k_pci_is_offset_within_mhi_region(u32 offset)
  144. {
  145. return (offset >= PCI_MHIREGLEN_REG && offset <= PCI_MHI_REGION_END);
  146. }
  147. static void ath12k_pci_restore_window(struct ath12k_base *ab)
  148. {
  149. struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
  150. spin_lock_bh(&ab_pci->window_lock);
  151. iowrite32(WINDOW_ENABLE_BIT | ab_pci->register_window,
  152. ab->mem + ab_pci->window_reg_addr);
  153. ioread32(ab->mem + ab_pci->window_reg_addr);
  154. spin_unlock_bh(&ab_pci->window_lock);
  155. }
  156. static void ath12k_pci_soc_global_reset(struct ath12k_base *ab)
  157. {
  158. u32 val, delay;
  159. val = ath12k_pci_read32(ab, PCIE_SOC_GLOBAL_RESET);
  160. val |= PCIE_SOC_GLOBAL_RESET_V;
  161. ath12k_pci_write32(ab, PCIE_SOC_GLOBAL_RESET, val);
  162. /* TODO: exact time to sleep is uncertain */
  163. delay = 10;
  164. mdelay(delay);
  165. /* Need to toggle V bit back otherwise stuck in reset status */
  166. val &= ~PCIE_SOC_GLOBAL_RESET_V;
  167. ath12k_pci_write32(ab, PCIE_SOC_GLOBAL_RESET, val);
  168. mdelay(delay);
  169. val = ath12k_pci_read32(ab, PCIE_SOC_GLOBAL_RESET);
  170. if (val == 0xffffffff)
  171. ath12k_warn(ab, "link down error during global reset\n");
  172. /* Restore window register as its content is cleared during
  173. * hardware global reset, such that it aligns with host cache.
  174. */
  175. ath12k_pci_restore_window(ab);
  176. }
  177. static void ath12k_pci_clear_dbg_registers(struct ath12k_base *ab)
  178. {
  179. u32 val;
  180. /* read cookie */
  181. val = ath12k_pci_read32(ab, PCIE_Q6_COOKIE_ADDR);
  182. ath12k_dbg(ab, ATH12K_DBG_PCI, "cookie:0x%x\n", val);
  183. val = ath12k_pci_read32(ab, WLAON_WARM_SW_ENTRY);
  184. ath12k_dbg(ab, ATH12K_DBG_PCI, "WLAON_WARM_SW_ENTRY 0x%x\n", val);
  185. /* TODO: exact time to sleep is uncertain */
  186. mdelay(10);
  187. /* write 0 to WLAON_WARM_SW_ENTRY to prevent Q6 from
  188. * continuing warm path and entering dead loop.
  189. */
  190. ath12k_pci_write32(ab, WLAON_WARM_SW_ENTRY, 0);
  191. mdelay(10);
  192. val = ath12k_pci_read32(ab, WLAON_WARM_SW_ENTRY);
  193. ath12k_dbg(ab, ATH12K_DBG_PCI, "WLAON_WARM_SW_ENTRY 0x%x\n", val);
  194. /* A read clear register. clear the register to prevent
  195. * Q6 from entering wrong code path.
  196. */
  197. val = ath12k_pci_read32(ab, WLAON_SOC_RESET_CAUSE_REG);
  198. ath12k_dbg(ab, ATH12K_DBG_PCI, "soc reset cause:%d\n", val);
  199. }
  200. static void ath12k_pci_enable_ltssm(struct ath12k_base *ab)
  201. {
  202. u32 val;
  203. int i;
  204. val = ath12k_pci_read32(ab, PCIE_PCIE_PARF_LTSSM);
  205. /* PCIE link seems very unstable after the Hot Reset*/
  206. for (i = 0; val != PARM_LTSSM_VALUE && i < 5; i++) {
  207. if (val == 0xffffffff)
  208. mdelay(5);
  209. ath12k_pci_write32(ab, PCIE_PCIE_PARF_LTSSM, PARM_LTSSM_VALUE);
  210. val = ath12k_pci_read32(ab, PCIE_PCIE_PARF_LTSSM);
  211. }
  212. ath12k_dbg(ab, ATH12K_DBG_PCI, "pci ltssm 0x%x\n", val);
  213. val = ath12k_pci_read32(ab, GCC_GCC_PCIE_HOT_RST(ab));
  214. val |= GCC_GCC_PCIE_HOT_RST_VAL;
  215. ath12k_pci_write32(ab, GCC_GCC_PCIE_HOT_RST(ab), val);
  216. val = ath12k_pci_read32(ab, GCC_GCC_PCIE_HOT_RST(ab));
  217. ath12k_dbg(ab, ATH12K_DBG_PCI, "pci pcie_hot_rst 0x%x\n", val);
  218. mdelay(5);
  219. }
  220. static void ath12k_pci_clear_all_intrs(struct ath12k_base *ab)
  221. {
  222. /* This is a WAR for PCIE Hotreset.
  223. * When target receive Hotreset, but will set the interrupt.
  224. * So when download SBL again, SBL will open Interrupt and
  225. * receive it, and crash immediately.
  226. */
  227. ath12k_pci_write32(ab, PCIE_PCIE_INT_ALL_CLEAR, PCIE_INT_CLEAR_ALL);
  228. }
  229. static void ath12k_pci_set_wlaon_pwr_ctrl(struct ath12k_base *ab)
  230. {
  231. u32 val;
  232. val = ath12k_pci_read32(ab, WLAON_QFPROM_PWR_CTRL_REG);
  233. val &= ~QFPROM_PWR_CTRL_VDD4BLOW_MASK;
  234. ath12k_pci_write32(ab, WLAON_QFPROM_PWR_CTRL_REG, val);
  235. }
  236. static void ath12k_pci_force_wake(struct ath12k_base *ab)
  237. {
  238. ath12k_pci_write32(ab, PCIE_SOC_WAKE_PCIE_LOCAL_REG, 1);
  239. mdelay(5);
  240. }
  241. static void ath12k_pci_sw_reset(struct ath12k_base *ab, bool power_on)
  242. {
  243. if (power_on) {
  244. ath12k_pci_enable_ltssm(ab);
  245. ath12k_pci_clear_all_intrs(ab);
  246. ath12k_pci_set_wlaon_pwr_ctrl(ab);
  247. }
  248. ath12k_mhi_clear_vector(ab);
  249. ath12k_pci_clear_dbg_registers(ab);
  250. ath12k_pci_soc_global_reset(ab);
  251. ath12k_mhi_set_mhictrl_reset(ab);
  252. }
  253. static void ath12k_pci_free_ext_irq(struct ath12k_base *ab)
  254. {
  255. int i, j;
  256. for (i = 0; i < ATH12K_EXT_IRQ_GRP_NUM_MAX; i++) {
  257. struct ath12k_ext_irq_grp *irq_grp = &ab->ext_irq_grp[i];
  258. for (j = 0; j < irq_grp->num_irq; j++)
  259. free_irq(ab->irq_num[irq_grp->irqs[j]], irq_grp);
  260. netif_napi_del(&irq_grp->napi);
  261. free_netdev(irq_grp->napi_ndev);
  262. }
  263. }
  264. static void ath12k_pci_free_irq(struct ath12k_base *ab)
  265. {
  266. int i, irq_idx;
  267. for (i = 0; i < ab->hw_params->ce_count; i++) {
  268. if (ath12k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
  269. continue;
  270. irq_idx = ATH12K_PCI_IRQ_CE0_OFFSET + i;
  271. free_irq(ab->irq_num[irq_idx], &ab->ce.ce_pipe[i]);
  272. }
  273. ath12k_pci_free_ext_irq(ab);
  274. }
  275. static void ath12k_pci_ce_irq_enable(struct ath12k_base *ab, u16 ce_id)
  276. {
  277. struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
  278. u32 irq_idx;
  279. /* In case of one MSI vector, we handle irq enable/disable in a
  280. * uniform way since we only have one irq
  281. */
  282. if (!test_bit(ATH12K_PCI_FLAG_MULTI_MSI_VECTORS, &ab_pci->flags))
  283. return;
  284. irq_idx = ATH12K_PCI_IRQ_CE0_OFFSET + ce_id;
  285. enable_irq(ab->irq_num[irq_idx]);
  286. }
  287. static void ath12k_pci_ce_irq_disable(struct ath12k_base *ab, u16 ce_id)
  288. {
  289. struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
  290. u32 irq_idx;
  291. /* In case of one MSI vector, we handle irq enable/disable in a
  292. * uniform way since we only have one irq
  293. */
  294. if (!test_bit(ATH12K_PCI_FLAG_MULTI_MSI_VECTORS, &ab_pci->flags))
  295. return;
  296. irq_idx = ATH12K_PCI_IRQ_CE0_OFFSET + ce_id;
  297. disable_irq_nosync(ab->irq_num[irq_idx]);
  298. }
  299. static void ath12k_pci_ce_irqs_disable(struct ath12k_base *ab)
  300. {
  301. int i;
  302. clear_bit(ATH12K_FLAG_CE_IRQ_ENABLED, &ab->dev_flags);
  303. for (i = 0; i < ab->hw_params->ce_count; i++) {
  304. if (ath12k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
  305. continue;
  306. ath12k_pci_ce_irq_disable(ab, i);
  307. }
  308. }
  309. static void ath12k_pci_sync_ce_irqs(struct ath12k_base *ab)
  310. {
  311. int i;
  312. int irq_idx;
  313. for (i = 0; i < ab->hw_params->ce_count; i++) {
  314. if (ath12k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
  315. continue;
  316. irq_idx = ATH12K_PCI_IRQ_CE0_OFFSET + i;
  317. synchronize_irq(ab->irq_num[irq_idx]);
  318. }
  319. }
  320. static void ath12k_pci_ce_workqueue(struct work_struct *work)
  321. {
  322. struct ath12k_ce_pipe *ce_pipe = from_work(ce_pipe, work, intr_wq);
  323. int irq_idx = ATH12K_PCI_IRQ_CE0_OFFSET + ce_pipe->pipe_num;
  324. ath12k_ce_per_engine_service(ce_pipe->ab, ce_pipe->pipe_num);
  325. enable_irq(ce_pipe->ab->irq_num[irq_idx]);
  326. }
  327. static irqreturn_t ath12k_pci_ce_interrupt_handler(int irq, void *arg)
  328. {
  329. struct ath12k_ce_pipe *ce_pipe = arg;
  330. struct ath12k_base *ab = ce_pipe->ab;
  331. int irq_idx = ATH12K_PCI_IRQ_CE0_OFFSET + ce_pipe->pipe_num;
  332. if (!test_bit(ATH12K_FLAG_CE_IRQ_ENABLED, &ab->dev_flags))
  333. return IRQ_HANDLED;
  334. /* last interrupt received for this CE */
  335. ce_pipe->timestamp = jiffies;
  336. disable_irq_nosync(ab->irq_num[irq_idx]);
  337. queue_work(system_bh_wq, &ce_pipe->intr_wq);
  338. return IRQ_HANDLED;
  339. }
  340. static void ath12k_pci_ext_grp_disable(struct ath12k_ext_irq_grp *irq_grp)
  341. {
  342. struct ath12k_pci *ab_pci = ath12k_pci_priv(irq_grp->ab);
  343. int i;
  344. /* In case of one MSI vector, we handle irq enable/disable
  345. * in a uniform way since we only have one irq
  346. */
  347. if (!test_bit(ATH12K_PCI_FLAG_MULTI_MSI_VECTORS, &ab_pci->flags))
  348. return;
  349. for (i = 0; i < irq_grp->num_irq; i++)
  350. disable_irq_nosync(irq_grp->ab->irq_num[irq_grp->irqs[i]]);
  351. }
  352. static void __ath12k_pci_ext_irq_disable(struct ath12k_base *ab)
  353. {
  354. int i;
  355. if (!test_and_clear_bit(ATH12K_FLAG_EXT_IRQ_ENABLED, &ab->dev_flags))
  356. return;
  357. for (i = 0; i < ATH12K_EXT_IRQ_GRP_NUM_MAX; i++) {
  358. struct ath12k_ext_irq_grp *irq_grp = &ab->ext_irq_grp[i];
  359. ath12k_pci_ext_grp_disable(irq_grp);
  360. if (irq_grp->napi_enabled) {
  361. napi_synchronize(&irq_grp->napi);
  362. napi_disable(&irq_grp->napi);
  363. irq_grp->napi_enabled = false;
  364. }
  365. }
  366. }
  367. static void ath12k_pci_ext_grp_enable(struct ath12k_ext_irq_grp *irq_grp)
  368. {
  369. struct ath12k_pci *ab_pci = ath12k_pci_priv(irq_grp->ab);
  370. int i;
  371. /* In case of one MSI vector, we handle irq enable/disable in a
  372. * uniform way since we only have one irq
  373. */
  374. if (!test_bit(ATH12K_PCI_FLAG_MULTI_MSI_VECTORS, &ab_pci->flags))
  375. return;
  376. for (i = 0; i < irq_grp->num_irq; i++)
  377. enable_irq(irq_grp->ab->irq_num[irq_grp->irqs[i]]);
  378. }
  379. static void ath12k_pci_sync_ext_irqs(struct ath12k_base *ab)
  380. {
  381. int i, j, irq_idx;
  382. for (i = 0; i < ATH12K_EXT_IRQ_GRP_NUM_MAX; i++) {
  383. struct ath12k_ext_irq_grp *irq_grp = &ab->ext_irq_grp[i];
  384. for (j = 0; j < irq_grp->num_irq; j++) {
  385. irq_idx = irq_grp->irqs[j];
  386. synchronize_irq(ab->irq_num[irq_idx]);
  387. }
  388. }
  389. }
  390. static int ath12k_pci_ext_grp_napi_poll(struct napi_struct *napi, int budget)
  391. {
  392. struct ath12k_ext_irq_grp *irq_grp = container_of(napi,
  393. struct ath12k_ext_irq_grp,
  394. napi);
  395. struct ath12k_base *ab = irq_grp->ab;
  396. struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
  397. int work_done;
  398. int i;
  399. work_done = ath12k_dp_service_srng(dp, irq_grp, budget);
  400. if (work_done < budget) {
  401. napi_complete_done(napi, work_done);
  402. for (i = 0; i < irq_grp->num_irq; i++)
  403. enable_irq(irq_grp->ab->irq_num[irq_grp->irqs[i]]);
  404. }
  405. if (work_done > budget)
  406. work_done = budget;
  407. return work_done;
  408. }
  409. static irqreturn_t ath12k_pci_ext_interrupt_handler(int irq, void *arg)
  410. {
  411. struct ath12k_ext_irq_grp *irq_grp = arg;
  412. struct ath12k_base *ab = irq_grp->ab;
  413. int i;
  414. if (!test_bit(ATH12K_FLAG_EXT_IRQ_ENABLED, &ab->dev_flags))
  415. return IRQ_HANDLED;
  416. ath12k_dbg(irq_grp->ab, ATH12K_DBG_PCI, "ext irq:%d\n", irq);
  417. /* last interrupt received for this group */
  418. irq_grp->timestamp = jiffies;
  419. for (i = 0; i < irq_grp->num_irq; i++)
  420. disable_irq_nosync(irq_grp->ab->irq_num[irq_grp->irqs[i]]);
  421. napi_schedule(&irq_grp->napi);
  422. return IRQ_HANDLED;
  423. }
  424. static int ath12k_pci_ext_irq_config(struct ath12k_base *ab)
  425. {
  426. struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
  427. int i, j, n, ret, num_vectors = 0;
  428. u32 user_base_data = 0, base_vector = 0, base_idx;
  429. struct ath12k_ext_irq_grp *irq_grp;
  430. base_idx = ATH12K_PCI_IRQ_CE0_OFFSET + CE_COUNT_MAX;
  431. ret = ath12k_pci_get_user_msi_assignment(ab, "DP",
  432. &num_vectors,
  433. &user_base_data,
  434. &base_vector);
  435. if (ret < 0)
  436. return ret;
  437. for (i = 0; i < ATH12K_EXT_IRQ_GRP_NUM_MAX; i++) {
  438. irq_grp = &ab->ext_irq_grp[i];
  439. u32 num_irq = 0;
  440. irq_grp->ab = ab;
  441. irq_grp->grp_id = i;
  442. irq_grp->napi_ndev = alloc_netdev_dummy(0);
  443. if (!irq_grp->napi_ndev) {
  444. ret = -ENOMEM;
  445. goto fail_allocate;
  446. }
  447. netif_napi_add(irq_grp->napi_ndev, &irq_grp->napi,
  448. ath12k_pci_ext_grp_napi_poll);
  449. if (ab->hw_params->ring_mask->tx[i] ||
  450. ab->hw_params->ring_mask->rx[i] ||
  451. ab->hw_params->ring_mask->rx_err[i] ||
  452. ab->hw_params->ring_mask->rx_wbm_rel[i] ||
  453. ab->hw_params->ring_mask->reo_status[i] ||
  454. ab->hw_params->ring_mask->host2rxdma[i] ||
  455. ab->hw_params->ring_mask->rx_mon_dest[i] ||
  456. ab->hw_params->ring_mask->rx_mon_status[i]) {
  457. num_irq = 1;
  458. }
  459. irq_grp->num_irq = num_irq;
  460. irq_grp->irqs[0] = base_idx + i;
  461. for (j = 0; j < irq_grp->num_irq; j++) {
  462. int irq_idx = irq_grp->irqs[j];
  463. int vector = (i % num_vectors) + base_vector;
  464. int irq = ath12k_pci_get_msi_irq(ab->dev, vector);
  465. ab->irq_num[irq_idx] = irq;
  466. ath12k_dbg(ab, ATH12K_DBG_PCI,
  467. "irq:%d group:%d\n", irq, i);
  468. irq_set_status_flags(irq, IRQ_DISABLE_UNLAZY);
  469. ret = request_irq(irq, ath12k_pci_ext_interrupt_handler,
  470. ab_pci->irq_flags,
  471. "DP_EXT_IRQ", irq_grp);
  472. if (ret) {
  473. ath12k_err(ab, "failed request irq %d: %d\n",
  474. vector, ret);
  475. goto fail_request;
  476. }
  477. }
  478. ath12k_pci_ext_grp_disable(irq_grp);
  479. }
  480. return 0;
  481. fail_request:
  482. /* i ->napi_ndev was properly allocated. Free it also */
  483. i += 1;
  484. fail_allocate:
  485. for (n = 0; n < i; n++) {
  486. irq_grp = &ab->ext_irq_grp[n];
  487. free_netdev(irq_grp->napi_ndev);
  488. }
  489. return ret;
  490. }
  491. static int ath12k_pci_set_irq_affinity_hint(struct ath12k_pci *ab_pci,
  492. const struct cpumask *m)
  493. {
  494. if (test_bit(ATH12K_PCI_FLAG_MULTI_MSI_VECTORS, &ab_pci->flags))
  495. return 0;
  496. return irq_set_affinity_and_hint(ab_pci->pdev->irq, m);
  497. }
  498. static int ath12k_pci_config_irq(struct ath12k_base *ab)
  499. {
  500. struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
  501. struct ath12k_ce_pipe *ce_pipe;
  502. u32 msi_data_start;
  503. u32 msi_data_count, msi_data_idx;
  504. u32 msi_irq_start;
  505. unsigned int msi_data;
  506. int irq, i, ret, irq_idx;
  507. ret = ath12k_pci_get_user_msi_assignment(ab,
  508. "CE", &msi_data_count,
  509. &msi_data_start, &msi_irq_start);
  510. if (ret)
  511. return ret;
  512. /* Configure CE irqs */
  513. for (i = 0, msi_data_idx = 0; i < ab->hw_params->ce_count; i++) {
  514. if (ath12k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
  515. continue;
  516. msi_data = (msi_data_idx % msi_data_count) + msi_irq_start;
  517. irq = ath12k_pci_get_msi_irq(ab->dev, msi_data);
  518. ce_pipe = &ab->ce.ce_pipe[i];
  519. irq_idx = ATH12K_PCI_IRQ_CE0_OFFSET + i;
  520. INIT_WORK(&ce_pipe->intr_wq, ath12k_pci_ce_workqueue);
  521. ret = request_irq(irq, ath12k_pci_ce_interrupt_handler,
  522. ab_pci->irq_flags, irq_name[irq_idx],
  523. ce_pipe);
  524. if (ret) {
  525. ath12k_err(ab, "failed to request irq %d: %d\n",
  526. irq_idx, ret);
  527. return ret;
  528. }
  529. ab->irq_num[irq_idx] = irq;
  530. msi_data_idx++;
  531. ath12k_pci_ce_irq_disable(ab, i);
  532. }
  533. ret = ath12k_pci_ext_irq_config(ab);
  534. if (ret)
  535. return ret;
  536. return 0;
  537. }
  538. static void ath12k_pci_init_qmi_ce_config(struct ath12k_base *ab)
  539. {
  540. struct ath12k_qmi_ce_cfg *cfg = &ab->qmi.ce_cfg;
  541. struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
  542. struct pci_bus *bus = ab_pci->pdev->bus;
  543. cfg->tgt_ce = ab->hw_params->target_ce_config;
  544. cfg->tgt_ce_len = ab->hw_params->target_ce_count;
  545. cfg->svc_to_ce_map = ab->hw_params->svc_to_ce_map;
  546. cfg->svc_to_ce_map_len = ab->hw_params->svc_to_ce_map_len;
  547. ab->qmi.service_ins_id = ab->hw_params->qmi_service_ins_id;
  548. if (ath12k_fw_feature_supported(ab, ATH12K_FW_FEATURE_MULTI_QRTR_ID)) {
  549. ab_pci->qmi_instance =
  550. u32_encode_bits(pci_domain_nr(bus), DOMAIN_NUMBER_MASK) |
  551. u32_encode_bits(bus->number, BUS_NUMBER_MASK);
  552. ab->qmi.service_ins_id += ab_pci->qmi_instance;
  553. }
  554. }
  555. static void ath12k_pci_ce_irqs_enable(struct ath12k_base *ab)
  556. {
  557. int i;
  558. set_bit(ATH12K_FLAG_CE_IRQ_ENABLED, &ab->dev_flags);
  559. for (i = 0; i < ab->hw_params->ce_count; i++) {
  560. if (ath12k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
  561. continue;
  562. ath12k_pci_ce_irq_enable(ab, i);
  563. }
  564. }
  565. static void ath12k_pci_msi_config(struct ath12k_pci *ab_pci, bool enable)
  566. {
  567. struct pci_dev *dev = ab_pci->pdev;
  568. u16 control;
  569. pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
  570. if (enable)
  571. control |= PCI_MSI_FLAGS_ENABLE;
  572. else
  573. control &= ~PCI_MSI_FLAGS_ENABLE;
  574. pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
  575. }
  576. static void ath12k_pci_msi_enable(struct ath12k_pci *ab_pci)
  577. {
  578. ath12k_pci_msi_config(ab_pci, true);
  579. }
  580. static void ath12k_pci_msi_disable(struct ath12k_pci *ab_pci)
  581. {
  582. ath12k_pci_msi_config(ab_pci, false);
  583. }
  584. static int ath12k_pci_msi_alloc(struct ath12k_pci *ab_pci)
  585. {
  586. struct ath12k_base *ab = ab_pci->ab;
  587. const struct ath12k_msi_config *msi_config = ab_pci->msi_config;
  588. struct msi_desc *msi_desc;
  589. int num_vectors;
  590. int ret;
  591. num_vectors = pci_alloc_irq_vectors(ab_pci->pdev,
  592. msi_config->total_vectors,
  593. msi_config->total_vectors,
  594. PCI_IRQ_MSI);
  595. if (num_vectors == msi_config->total_vectors) {
  596. set_bit(ATH12K_PCI_FLAG_MULTI_MSI_VECTORS, &ab_pci->flags);
  597. ab_pci->irq_flags = IRQF_SHARED;
  598. } else {
  599. num_vectors = pci_alloc_irq_vectors(ab_pci->pdev,
  600. 1,
  601. 1,
  602. PCI_IRQ_MSI);
  603. if (num_vectors < 0) {
  604. ret = -EINVAL;
  605. goto reset_msi_config;
  606. }
  607. clear_bit(ATH12K_PCI_FLAG_MULTI_MSI_VECTORS, &ab_pci->flags);
  608. ab_pci->msi_config = &msi_config_one_msi;
  609. ab_pci->irq_flags = IRQF_SHARED | IRQF_NOBALANCING;
  610. ath12k_dbg(ab, ATH12K_DBG_PCI, "request MSI one vector\n");
  611. }
  612. ath12k_info(ab, "MSI vectors: %d\n", num_vectors);
  613. ath12k_pci_msi_disable(ab_pci);
  614. msi_desc = irq_get_msi_desc(ab_pci->pdev->irq);
  615. if (!msi_desc) {
  616. ath12k_err(ab, "msi_desc is NULL!\n");
  617. ret = -EINVAL;
  618. goto free_msi_vector;
  619. }
  620. ab_pci->msi_ep_base_data = msi_desc->msg.data;
  621. if (msi_desc->pci.msi_attrib.is_64)
  622. set_bit(ATH12K_PCI_FLAG_IS_MSI_64, &ab_pci->flags);
  623. ath12k_dbg(ab, ATH12K_DBG_PCI, "msi base data is %d\n", ab_pci->msi_ep_base_data);
  624. return 0;
  625. free_msi_vector:
  626. pci_free_irq_vectors(ab_pci->pdev);
  627. reset_msi_config:
  628. return ret;
  629. }
  630. static void ath12k_pci_msi_free(struct ath12k_pci *ab_pci)
  631. {
  632. pci_free_irq_vectors(ab_pci->pdev);
  633. }
  634. static int ath12k_pci_config_msi_data(struct ath12k_pci *ab_pci)
  635. {
  636. struct msi_desc *msi_desc;
  637. msi_desc = irq_get_msi_desc(ab_pci->pdev->irq);
  638. if (!msi_desc) {
  639. ath12k_err(ab_pci->ab, "msi_desc is NULL!\n");
  640. pci_free_irq_vectors(ab_pci->pdev);
  641. return -EINVAL;
  642. }
  643. ab_pci->msi_ep_base_data = msi_desc->msg.data;
  644. ath12k_dbg(ab_pci->ab, ATH12K_DBG_PCI, "pci after request_irq msi_ep_base_data %d\n",
  645. ab_pci->msi_ep_base_data);
  646. return 0;
  647. }
  648. static int ath12k_pci_claim(struct ath12k_pci *ab_pci, struct pci_dev *pdev)
  649. {
  650. struct ath12k_base *ab = ab_pci->ab;
  651. u16 device_id;
  652. int ret = 0;
  653. pci_read_config_word(pdev, PCI_DEVICE_ID, &device_id);
  654. if (device_id != ab_pci->dev_id) {
  655. ath12k_err(ab, "pci device id mismatch: 0x%x 0x%x\n",
  656. device_id, ab_pci->dev_id);
  657. ret = -EIO;
  658. goto out;
  659. }
  660. ret = pci_assign_resource(pdev, ATH12K_PCI_BAR_NUM);
  661. if (ret) {
  662. ath12k_err(ab, "failed to assign pci resource: %d\n", ret);
  663. goto out;
  664. }
  665. ret = pci_enable_device(pdev);
  666. if (ret) {
  667. ath12k_err(ab, "failed to enable pci device: %d\n", ret);
  668. goto out;
  669. }
  670. ret = pci_request_region(pdev, ATH12K_PCI_BAR_NUM, "ath12k_pci");
  671. if (ret) {
  672. ath12k_err(ab, "failed to request pci region: %d\n", ret);
  673. goto disable_device;
  674. }
  675. ab_pci->dma_mask = DMA_BIT_MASK(ATH12K_PCI_DMA_MASK);
  676. dma_set_mask(&pdev->dev, ab_pci->dma_mask);
  677. dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  678. pci_set_master(pdev);
  679. ab->mem_len = pci_resource_len(pdev, ATH12K_PCI_BAR_NUM);
  680. ab->mem = pci_iomap(pdev, ATH12K_PCI_BAR_NUM, 0);
  681. if (!ab->mem) {
  682. ath12k_err(ab, "failed to map pci bar %d\n", ATH12K_PCI_BAR_NUM);
  683. ret = -EIO;
  684. goto release_region;
  685. }
  686. ath12k_dbg(ab, ATH12K_DBG_BOOT, "boot pci_mem 0x%p\n", ab->mem);
  687. return 0;
  688. release_region:
  689. pci_release_region(pdev, ATH12K_PCI_BAR_NUM);
  690. disable_device:
  691. pci_disable_device(pdev);
  692. out:
  693. return ret;
  694. }
  695. static void ath12k_pci_free_region(struct ath12k_pci *ab_pci)
  696. {
  697. struct ath12k_base *ab = ab_pci->ab;
  698. struct pci_dev *pci_dev = ab_pci->pdev;
  699. pci_iounmap(pci_dev, ab->mem);
  700. ab->mem = NULL;
  701. pci_release_region(pci_dev, ATH12K_PCI_BAR_NUM);
  702. if (pci_is_enabled(pci_dev))
  703. pci_disable_device(pci_dev);
  704. }
  705. static void ath12k_pci_aspm_disable(struct ath12k_pci *ab_pci)
  706. {
  707. struct ath12k_base *ab = ab_pci->ab;
  708. pcie_capability_read_word(ab_pci->pdev, PCI_EXP_LNKCTL,
  709. &ab_pci->link_ctl);
  710. ath12k_dbg(ab, ATH12K_DBG_PCI, "pci link_ctl 0x%04x L0s %d L1 %d\n",
  711. ab_pci->link_ctl,
  712. u16_get_bits(ab_pci->link_ctl, PCI_EXP_LNKCTL_ASPM_L0S),
  713. u16_get_bits(ab_pci->link_ctl, PCI_EXP_LNKCTL_ASPM_L1));
  714. /* disable L0s and L1 */
  715. pcie_capability_clear_word(ab_pci->pdev, PCI_EXP_LNKCTL,
  716. PCI_EXP_LNKCTL_ASPMC);
  717. set_bit(ATH12K_PCI_ASPM_RESTORE, &ab_pci->flags);
  718. }
  719. static void ath12k_pci_update_qrtr_node_id(struct ath12k_base *ab)
  720. {
  721. struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
  722. u32 reg;
  723. /* On platforms with two or more identical mhi devices, qmi service run
  724. * with identical qrtr-node-id. Because of this identical ID qrtr-lookup
  725. * cannot register more than one qmi service with identical node ID.
  726. *
  727. * This generates a unique instance ID from PCIe domain number and bus number,
  728. * writes to the given register, it is available for firmware when the QMI service
  729. * is spawned.
  730. */
  731. reg = PCIE_LOCAL_REG_QRTR_NODE_ID(ab) & WINDOW_RANGE_MASK;
  732. ath12k_pci_write32(ab, reg, ab_pci->qmi_instance);
  733. ath12k_dbg(ab, ATH12K_DBG_PCI, "pci reg 0x%x instance 0x%x read val 0x%x\n",
  734. reg, ab_pci->qmi_instance, ath12k_pci_read32(ab, reg));
  735. }
  736. static void ath12k_pci_aspm_restore(struct ath12k_pci *ab_pci)
  737. {
  738. if (ab_pci->ab->hw_params->supports_aspm &&
  739. test_and_clear_bit(ATH12K_PCI_ASPM_RESTORE, &ab_pci->flags))
  740. pcie_capability_clear_and_set_word(ab_pci->pdev, PCI_EXP_LNKCTL,
  741. PCI_EXP_LNKCTL_ASPMC,
  742. ab_pci->link_ctl &
  743. PCI_EXP_LNKCTL_ASPMC);
  744. }
  745. static void ath12k_pci_cancel_workqueue(struct ath12k_base *ab)
  746. {
  747. int i;
  748. for (i = 0; i < ab->hw_params->ce_count; i++) {
  749. struct ath12k_ce_pipe *ce_pipe = &ab->ce.ce_pipe[i];
  750. if (ath12k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
  751. continue;
  752. cancel_work_sync(&ce_pipe->intr_wq);
  753. }
  754. }
  755. static void ath12k_pci_ce_irq_disable_sync(struct ath12k_base *ab)
  756. {
  757. ath12k_pci_ce_irqs_disable(ab);
  758. ath12k_pci_sync_ce_irqs(ab);
  759. ath12k_pci_cancel_workqueue(ab);
  760. }
  761. int ath12k_pci_map_service_to_pipe(struct ath12k_base *ab, u16 service_id,
  762. u8 *ul_pipe, u8 *dl_pipe)
  763. {
  764. const struct service_to_pipe *entry;
  765. bool ul_set = false, dl_set = false;
  766. int i;
  767. for (i = 0; i < ab->hw_params->svc_to_ce_map_len; i++) {
  768. entry = &ab->hw_params->svc_to_ce_map[i];
  769. if (__le32_to_cpu(entry->service_id) != service_id)
  770. continue;
  771. switch (__le32_to_cpu(entry->pipedir)) {
  772. case PIPEDIR_NONE:
  773. break;
  774. case PIPEDIR_IN:
  775. WARN_ON(dl_set);
  776. *dl_pipe = __le32_to_cpu(entry->pipenum);
  777. dl_set = true;
  778. break;
  779. case PIPEDIR_OUT:
  780. WARN_ON(ul_set);
  781. *ul_pipe = __le32_to_cpu(entry->pipenum);
  782. ul_set = true;
  783. break;
  784. case PIPEDIR_INOUT:
  785. WARN_ON(dl_set);
  786. WARN_ON(ul_set);
  787. *dl_pipe = __le32_to_cpu(entry->pipenum);
  788. *ul_pipe = __le32_to_cpu(entry->pipenum);
  789. dl_set = true;
  790. ul_set = true;
  791. break;
  792. }
  793. }
  794. if (WARN_ON(!ul_set || !dl_set))
  795. return -ENOENT;
  796. return 0;
  797. }
  798. int ath12k_pci_get_msi_irq(struct device *dev, unsigned int vector)
  799. {
  800. struct pci_dev *pci_dev = to_pci_dev(dev);
  801. return pci_irq_vector(pci_dev, vector);
  802. }
  803. int ath12k_pci_get_user_msi_assignment(struct ath12k_base *ab, char *user_name,
  804. int *num_vectors, u32 *user_base_data,
  805. u32 *base_vector)
  806. {
  807. struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
  808. const struct ath12k_msi_config *msi_config = ab_pci->msi_config;
  809. int idx;
  810. for (idx = 0; idx < msi_config->total_users; idx++) {
  811. if (strcmp(user_name, msi_config->users[idx].name) == 0) {
  812. *num_vectors = msi_config->users[idx].num_vectors;
  813. *base_vector = msi_config->users[idx].base_vector;
  814. *user_base_data = *base_vector + ab_pci->msi_ep_base_data;
  815. ath12k_dbg(ab, ATH12K_DBG_PCI,
  816. "Assign MSI to user: %s, num_vectors: %d, user_base_data: %u, base_vector: %u\n",
  817. user_name, *num_vectors, *user_base_data,
  818. *base_vector);
  819. return 0;
  820. }
  821. }
  822. ath12k_err(ab, "Failed to find MSI assignment for %s!\n", user_name);
  823. return -EINVAL;
  824. }
  825. void ath12k_pci_get_msi_address(struct ath12k_base *ab, u32 *msi_addr_lo,
  826. u32 *msi_addr_hi)
  827. {
  828. struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
  829. struct pci_dev *pci_dev = to_pci_dev(ab->dev);
  830. pci_read_config_dword(pci_dev, pci_dev->msi_cap + PCI_MSI_ADDRESS_LO,
  831. msi_addr_lo);
  832. if (test_bit(ATH12K_PCI_FLAG_IS_MSI_64, &ab_pci->flags)) {
  833. pci_read_config_dword(pci_dev, pci_dev->msi_cap + PCI_MSI_ADDRESS_HI,
  834. msi_addr_hi);
  835. } else {
  836. *msi_addr_hi = 0;
  837. }
  838. }
  839. void ath12k_pci_get_ce_msi_idx(struct ath12k_base *ab, u32 ce_id,
  840. u32 *msi_idx)
  841. {
  842. u32 i, msi_data_idx;
  843. for (i = 0, msi_data_idx = 0; i < ab->hw_params->ce_count; i++) {
  844. if (ath12k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
  845. continue;
  846. if (ce_id == i)
  847. break;
  848. msi_data_idx++;
  849. }
  850. *msi_idx = msi_data_idx;
  851. }
  852. void ath12k_pci_hif_ce_irq_enable(struct ath12k_base *ab)
  853. {
  854. ath12k_pci_ce_irqs_enable(ab);
  855. }
  856. void ath12k_pci_hif_ce_irq_disable(struct ath12k_base *ab)
  857. {
  858. ath12k_pci_ce_irq_disable_sync(ab);
  859. }
  860. void ath12k_pci_ext_irq_enable(struct ath12k_base *ab)
  861. {
  862. int i;
  863. for (i = 0; i < ATH12K_EXT_IRQ_GRP_NUM_MAX; i++) {
  864. struct ath12k_ext_irq_grp *irq_grp = &ab->ext_irq_grp[i];
  865. if (!irq_grp->napi_enabled) {
  866. napi_enable(&irq_grp->napi);
  867. irq_grp->napi_enabled = true;
  868. }
  869. ath12k_pci_ext_grp_enable(irq_grp);
  870. }
  871. set_bit(ATH12K_FLAG_EXT_IRQ_ENABLED, &ab->dev_flags);
  872. }
  873. void ath12k_pci_ext_irq_disable(struct ath12k_base *ab)
  874. {
  875. if (!test_bit(ATH12K_FLAG_EXT_IRQ_ENABLED, &ab->dev_flags))
  876. return;
  877. __ath12k_pci_ext_irq_disable(ab);
  878. ath12k_pci_sync_ext_irqs(ab);
  879. }
  880. int ath12k_pci_hif_suspend(struct ath12k_base *ab)
  881. {
  882. struct ath12k_pci *ar_pci = ath12k_pci_priv(ab);
  883. ath12k_mhi_suspend(ar_pci);
  884. return 0;
  885. }
  886. int ath12k_pci_hif_resume(struct ath12k_base *ab)
  887. {
  888. struct ath12k_pci *ar_pci = ath12k_pci_priv(ab);
  889. ath12k_mhi_resume(ar_pci);
  890. return 0;
  891. }
  892. void ath12k_pci_stop(struct ath12k_base *ab)
  893. {
  894. struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
  895. if (!test_bit(ATH12K_PCI_FLAG_INIT_DONE, &ab_pci->flags))
  896. return;
  897. ath12k_pci_ce_irq_disable_sync(ab);
  898. ath12k_ce_cleanup_pipes(ab);
  899. }
  900. int ath12k_pci_start(struct ath12k_base *ab)
  901. {
  902. struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
  903. set_bit(ATH12K_PCI_FLAG_INIT_DONE, &ab_pci->flags);
  904. if (test_bit(ATH12K_PCI_FLAG_MULTI_MSI_VECTORS, &ab_pci->flags))
  905. ath12k_pci_aspm_restore(ab_pci);
  906. else
  907. ath12k_info(ab, "leaving PCI ASPM disabled to avoid MHI M2 problems\n");
  908. ath12k_pci_ce_irqs_enable(ab);
  909. ath12k_ce_rx_post_buf(ab);
  910. return 0;
  911. }
  912. u32 ath12k_pci_read32(struct ath12k_base *ab, u32 offset)
  913. {
  914. struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
  915. u32 val, window_start;
  916. int ret = 0;
  917. /* for offset beyond BAR + 4K - 32, may
  918. * need to wakeup MHI to access.
  919. */
  920. if (test_bit(ATH12K_PCI_FLAG_INIT_DONE, &ab_pci->flags) &&
  921. offset >= ACCESS_ALWAYS_OFF && ab_pci->pci_ops->wakeup)
  922. ret = ab_pci->pci_ops->wakeup(ab);
  923. if (offset < WINDOW_START) {
  924. val = ioread32(ab->mem + offset);
  925. } else {
  926. if (ab->static_window_map)
  927. window_start = ath12k_pci_get_window_start(ab, offset);
  928. else
  929. window_start = WINDOW_START;
  930. if (window_start == WINDOW_START) {
  931. spin_lock_bh(&ab_pci->window_lock);
  932. ath12k_pci_select_window(ab_pci, offset);
  933. if (ath12k_pci_is_offset_within_mhi_region(offset)) {
  934. offset = offset - PCI_MHIREGLEN_REG;
  935. val = ioread32(ab->mem +
  936. (offset & WINDOW_RANGE_MASK));
  937. } else {
  938. val = ioread32(ab->mem + window_start +
  939. (offset & WINDOW_RANGE_MASK));
  940. }
  941. spin_unlock_bh(&ab_pci->window_lock);
  942. } else {
  943. val = ioread32(ab->mem + window_start +
  944. (offset & WINDOW_RANGE_MASK));
  945. }
  946. }
  947. if (test_bit(ATH12K_PCI_FLAG_INIT_DONE, &ab_pci->flags) &&
  948. offset >= ACCESS_ALWAYS_OFF && ab_pci->pci_ops->release &&
  949. !ret)
  950. ab_pci->pci_ops->release(ab);
  951. return val;
  952. }
  953. EXPORT_SYMBOL(ath12k_pci_read32);
  954. void ath12k_pci_write32(struct ath12k_base *ab, u32 offset, u32 value)
  955. {
  956. struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
  957. u32 window_start;
  958. int ret = 0;
  959. /* for offset beyond BAR + 4K - 32, may
  960. * need to wakeup MHI to access.
  961. */
  962. if (test_bit(ATH12K_PCI_FLAG_INIT_DONE, &ab_pci->flags) &&
  963. offset >= ACCESS_ALWAYS_OFF && ab_pci->pci_ops->wakeup)
  964. ret = ab_pci->pci_ops->wakeup(ab);
  965. if (offset < WINDOW_START) {
  966. iowrite32(value, ab->mem + offset);
  967. } else {
  968. if (ab->static_window_map)
  969. window_start = ath12k_pci_get_window_start(ab, offset);
  970. else
  971. window_start = WINDOW_START;
  972. if (window_start == WINDOW_START) {
  973. spin_lock_bh(&ab_pci->window_lock);
  974. ath12k_pci_select_window(ab_pci, offset);
  975. if (ath12k_pci_is_offset_within_mhi_region(offset)) {
  976. offset = offset - PCI_MHIREGLEN_REG;
  977. iowrite32(value, ab->mem +
  978. (offset & WINDOW_RANGE_MASK));
  979. } else {
  980. iowrite32(value, ab->mem + window_start +
  981. (offset & WINDOW_RANGE_MASK));
  982. }
  983. spin_unlock_bh(&ab_pci->window_lock);
  984. } else {
  985. iowrite32(value, ab->mem + window_start +
  986. (offset & WINDOW_RANGE_MASK));
  987. }
  988. }
  989. if (test_bit(ATH12K_PCI_FLAG_INIT_DONE, &ab_pci->flags) &&
  990. offset >= ACCESS_ALWAYS_OFF && ab_pci->pci_ops->release &&
  991. !ret)
  992. ab_pci->pci_ops->release(ab);
  993. }
  994. #ifdef CONFIG_ATH12K_COREDUMP
  995. static int ath12k_pci_coredump_calculate_size(struct ath12k_base *ab, u32 *dump_seg_sz)
  996. {
  997. struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
  998. struct mhi_controller *mhi_ctrl = ab_pci->mhi_ctrl;
  999. struct image_info *rddm_img, *fw_img;
  1000. struct ath12k_tlv_dump_data *dump_tlv;
  1001. enum ath12k_fw_crash_dump_type mem_type;
  1002. u32 len = 0, rddm_tlv_sz = 0, paging_tlv_sz = 0;
  1003. struct ath12k_dump_file_data *file_data;
  1004. int i;
  1005. rddm_img = mhi_ctrl->rddm_image;
  1006. if (!rddm_img) {
  1007. ath12k_err(ab, "No RDDM dump found\n");
  1008. return 0;
  1009. }
  1010. fw_img = mhi_ctrl->fbc_image;
  1011. for (i = 0; i < fw_img->entries ; i++) {
  1012. if (!fw_img->mhi_buf[i].buf)
  1013. continue;
  1014. paging_tlv_sz += fw_img->mhi_buf[i].len;
  1015. }
  1016. dump_seg_sz[FW_CRASH_DUMP_PAGING_DATA] = paging_tlv_sz;
  1017. for (i = 0; i < rddm_img->entries; i++) {
  1018. if (!rddm_img->mhi_buf[i].buf)
  1019. continue;
  1020. rddm_tlv_sz += rddm_img->mhi_buf[i].len;
  1021. }
  1022. dump_seg_sz[FW_CRASH_DUMP_RDDM_DATA] = rddm_tlv_sz;
  1023. for (i = 0; i < ab->qmi.mem_seg_count; i++) {
  1024. mem_type = ath12k_coredump_get_dump_type(ab->qmi.target_mem[i].type);
  1025. if (mem_type == FW_CRASH_DUMP_NONE)
  1026. continue;
  1027. if (mem_type == FW_CRASH_DUMP_TYPE_MAX) {
  1028. ath12k_dbg(ab, ATH12K_DBG_PCI,
  1029. "target mem region type %d not supported",
  1030. ab->qmi.target_mem[i].type);
  1031. continue;
  1032. }
  1033. if (!ab->qmi.target_mem[i].paddr)
  1034. continue;
  1035. dump_seg_sz[mem_type] += ab->qmi.target_mem[i].size;
  1036. }
  1037. for (i = 0; i < FW_CRASH_DUMP_TYPE_MAX; i++) {
  1038. if (!dump_seg_sz[i])
  1039. continue;
  1040. len += sizeof(*dump_tlv) + dump_seg_sz[i];
  1041. }
  1042. if (len)
  1043. len += sizeof(*file_data);
  1044. return len;
  1045. }
  1046. static void ath12k_pci_coredump_download(struct ath12k_base *ab)
  1047. {
  1048. struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
  1049. struct mhi_controller *mhi_ctrl = ab_pci->mhi_ctrl;
  1050. struct image_info *rddm_img, *fw_img;
  1051. struct timespec64 timestamp;
  1052. int i, len, mem_idx;
  1053. enum ath12k_fw_crash_dump_type mem_type;
  1054. struct ath12k_dump_file_data *file_data;
  1055. struct ath12k_tlv_dump_data *dump_tlv;
  1056. size_t hdr_len = sizeof(*file_data);
  1057. void *buf;
  1058. u32 dump_seg_sz[FW_CRASH_DUMP_TYPE_MAX] = {};
  1059. ath12k_mhi_coredump(mhi_ctrl, false);
  1060. len = ath12k_pci_coredump_calculate_size(ab, dump_seg_sz);
  1061. if (!len) {
  1062. ath12k_warn(ab, "No crash dump data found for devcoredump");
  1063. return;
  1064. }
  1065. rddm_img = mhi_ctrl->rddm_image;
  1066. fw_img = mhi_ctrl->fbc_image;
  1067. /* dev_coredumpv() requires vmalloc data */
  1068. buf = vzalloc(len);
  1069. if (!buf)
  1070. return;
  1071. ab->dump_data = buf;
  1072. ab->ath12k_coredump_len = len;
  1073. file_data = ab->dump_data;
  1074. strscpy(file_data->df_magic, "ATH12K-FW-DUMP", sizeof(file_data->df_magic));
  1075. file_data->len = cpu_to_le32(len);
  1076. file_data->version = cpu_to_le32(ATH12K_FW_CRASH_DUMP_V2);
  1077. file_data->chip_id = cpu_to_le32(ab_pci->dev_id);
  1078. file_data->qrtr_id = cpu_to_le32(ab_pci->ab->qmi.service_ins_id);
  1079. file_data->bus_id = cpu_to_le32(pci_domain_nr(ab_pci->pdev->bus));
  1080. guid_gen(&file_data->guid);
  1081. ktime_get_real_ts64(&timestamp);
  1082. file_data->tv_sec = cpu_to_le64(timestamp.tv_sec);
  1083. file_data->tv_nsec = cpu_to_le64(timestamp.tv_nsec);
  1084. buf += hdr_len;
  1085. dump_tlv = buf;
  1086. dump_tlv->type = cpu_to_le32(FW_CRASH_DUMP_PAGING_DATA);
  1087. dump_tlv->tlv_len = cpu_to_le32(dump_seg_sz[FW_CRASH_DUMP_PAGING_DATA]);
  1088. buf += COREDUMP_TLV_HDR_SIZE;
  1089. /* append all segments together as they are all part of a single contiguous
  1090. * block of memory
  1091. */
  1092. for (i = 0; i < fw_img->entries ; i++) {
  1093. if (!fw_img->mhi_buf[i].buf)
  1094. continue;
  1095. memcpy_fromio(buf, (void const __iomem *)fw_img->mhi_buf[i].buf,
  1096. fw_img->mhi_buf[i].len);
  1097. buf += fw_img->mhi_buf[i].len;
  1098. }
  1099. dump_tlv = buf;
  1100. dump_tlv->type = cpu_to_le32(FW_CRASH_DUMP_RDDM_DATA);
  1101. dump_tlv->tlv_len = cpu_to_le32(dump_seg_sz[FW_CRASH_DUMP_RDDM_DATA]);
  1102. buf += COREDUMP_TLV_HDR_SIZE;
  1103. /* append all segments together as they are all part of a single contiguous
  1104. * block of memory
  1105. */
  1106. for (i = 0; i < rddm_img->entries; i++) {
  1107. if (!rddm_img->mhi_buf[i].buf)
  1108. continue;
  1109. memcpy_fromio(buf, (void const __iomem *)rddm_img->mhi_buf[i].buf,
  1110. rddm_img->mhi_buf[i].len);
  1111. buf += rddm_img->mhi_buf[i].len;
  1112. }
  1113. mem_idx = FW_CRASH_DUMP_REMOTE_MEM_DATA;
  1114. for (; mem_idx < FW_CRASH_DUMP_TYPE_MAX; mem_idx++) {
  1115. if (!dump_seg_sz[mem_idx] || mem_idx == FW_CRASH_DUMP_NONE)
  1116. continue;
  1117. dump_tlv = buf;
  1118. dump_tlv->type = cpu_to_le32(mem_idx);
  1119. dump_tlv->tlv_len = cpu_to_le32(dump_seg_sz[mem_idx]);
  1120. buf += COREDUMP_TLV_HDR_SIZE;
  1121. for (i = 0; i < ab->qmi.mem_seg_count; i++) {
  1122. mem_type = ath12k_coredump_get_dump_type
  1123. (ab->qmi.target_mem[i].type);
  1124. if (mem_type != mem_idx)
  1125. continue;
  1126. if (!ab->qmi.target_mem[i].paddr) {
  1127. ath12k_dbg(ab, ATH12K_DBG_PCI,
  1128. "Skipping mem region type %d",
  1129. ab->qmi.target_mem[i].type);
  1130. continue;
  1131. }
  1132. memcpy_fromio(buf, ab->qmi.target_mem[i].v.ioaddr,
  1133. ab->qmi.target_mem[i].size);
  1134. buf += ab->qmi.target_mem[i].size;
  1135. }
  1136. }
  1137. queue_work(ab->workqueue, &ab->dump_work);
  1138. }
  1139. #endif
  1140. int ath12k_pci_power_up(struct ath12k_base *ab)
  1141. {
  1142. struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
  1143. int ret;
  1144. ab_pci->register_window = 0;
  1145. clear_bit(ATH12K_PCI_FLAG_INIT_DONE, &ab_pci->flags);
  1146. ath12k_pci_sw_reset(ab_pci->ab, true);
  1147. /* Disable ASPM during firmware download due to problems switching
  1148. * to AMSS state.
  1149. */
  1150. ath12k_pci_aspm_disable(ab_pci);
  1151. ath12k_pci_msi_enable(ab_pci);
  1152. if (ath12k_fw_feature_supported(ab, ATH12K_FW_FEATURE_MULTI_QRTR_ID))
  1153. ath12k_pci_update_qrtr_node_id(ab);
  1154. ret = ath12k_mhi_start(ab_pci);
  1155. if (ret) {
  1156. ath12k_err(ab, "failed to start mhi: %d\n", ret);
  1157. return ret;
  1158. }
  1159. if (ab->static_window_map)
  1160. ath12k_pci_select_static_window(ab_pci);
  1161. return 0;
  1162. }
  1163. void ath12k_pci_power_down(struct ath12k_base *ab, bool is_suspend)
  1164. {
  1165. struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
  1166. if (!test_bit(ATH12K_PCI_FLAG_INIT_DONE, &ab_pci->flags))
  1167. return;
  1168. /* restore aspm in case firmware bootup fails */
  1169. ath12k_pci_aspm_restore(ab_pci);
  1170. ath12k_pci_force_wake(ab_pci->ab);
  1171. ath12k_pci_msi_disable(ab_pci);
  1172. ath12k_mhi_stop(ab_pci, is_suspend);
  1173. clear_bit(ATH12K_PCI_FLAG_INIT_DONE, &ab_pci->flags);
  1174. ath12k_pci_sw_reset(ab_pci->ab, false);
  1175. }
  1176. static int ath12k_pci_panic_handler(struct ath12k_base *ab)
  1177. {
  1178. ath12k_pci_sw_reset(ab, false);
  1179. return NOTIFY_OK;
  1180. }
  1181. static const struct ath12k_hif_ops ath12k_pci_hif_ops = {
  1182. .start = ath12k_pci_start,
  1183. .stop = ath12k_pci_stop,
  1184. .read32 = ath12k_pci_read32,
  1185. .write32 = ath12k_pci_write32,
  1186. .power_down = ath12k_pci_power_down,
  1187. .power_up = ath12k_pci_power_up,
  1188. .suspend = ath12k_pci_hif_suspend,
  1189. .resume = ath12k_pci_hif_resume,
  1190. .irq_enable = ath12k_pci_ext_irq_enable,
  1191. .irq_disable = ath12k_pci_ext_irq_disable,
  1192. .get_msi_address = ath12k_pci_get_msi_address,
  1193. .get_user_msi_vector = ath12k_pci_get_user_msi_assignment,
  1194. .map_service_to_pipe = ath12k_pci_map_service_to_pipe,
  1195. .ce_irq_enable = ath12k_pci_hif_ce_irq_enable,
  1196. .ce_irq_disable = ath12k_pci_hif_ce_irq_disable,
  1197. .get_ce_msi_idx = ath12k_pci_get_ce_msi_idx,
  1198. .panic_handler = ath12k_pci_panic_handler,
  1199. #ifdef CONFIG_ATH12K_COREDUMP
  1200. .coredump_download = ath12k_pci_coredump_download,
  1201. #endif
  1202. };
  1203. static enum ath12k_device_family
  1204. ath12k_get_device_family(const struct pci_device_id *pci_dev)
  1205. {
  1206. enum ath12k_device_family device_family_id;
  1207. const struct pci_device_id *id;
  1208. for (device_family_id = ATH12K_DEVICE_FAMILY_START;
  1209. device_family_id < ATH12K_DEVICE_FAMILY_MAX; device_family_id++) {
  1210. if (!ath12k_pci_family_drivers[device_family_id])
  1211. continue;
  1212. id = ath12k_pci_family_drivers[device_family_id]->id_table;
  1213. while (id->device) {
  1214. if (id->device == pci_dev->device)
  1215. return device_family_id;
  1216. id += 1;
  1217. }
  1218. }
  1219. return ATH12K_DEVICE_FAMILY_MAX;
  1220. }
  1221. static int ath12k_pci_probe(struct pci_dev *pdev,
  1222. const struct pci_device_id *pci_dev)
  1223. {
  1224. enum ath12k_device_family device_id;
  1225. struct ath12k_pci *ab_pci;
  1226. struct ath12k_base *ab;
  1227. int ret;
  1228. ab = ath12k_core_alloc(&pdev->dev, sizeof(*ab_pci), ATH12K_BUS_PCI);
  1229. if (!ab) {
  1230. dev_err(&pdev->dev, "failed to allocate ath12k base\n");
  1231. return -ENOMEM;
  1232. }
  1233. ab->dev = &pdev->dev;
  1234. ab_pci = ath12k_pci_priv(ab);
  1235. ab_pci->dev_id = pci_dev->device;
  1236. ab_pci->ab = ab;
  1237. ab_pci->pdev = pdev;
  1238. ab->hif.ops = &ath12k_pci_hif_ops;
  1239. ab->fw_mode = ATH12K_FIRMWARE_MODE_NORMAL;
  1240. pci_set_drvdata(pdev, ab);
  1241. spin_lock_init(&ab_pci->window_lock);
  1242. ret = ath12k_pci_claim(ab_pci, pdev);
  1243. if (ret) {
  1244. ath12k_err(ab, "failed to claim device: %d\n", ret);
  1245. goto err_free_core;
  1246. }
  1247. ath12k_dbg(ab, ATH12K_DBG_BOOT, "pci probe %04x:%04x %04x:%04x\n",
  1248. pdev->vendor, pdev->device,
  1249. pdev->subsystem_vendor, pdev->subsystem_device);
  1250. ab->id.vendor = pdev->vendor;
  1251. ab->id.device = pdev->device;
  1252. ab->id.subsystem_vendor = pdev->subsystem_vendor;
  1253. ab->id.subsystem_device = pdev->subsystem_device;
  1254. device_id = ath12k_get_device_family(pci_dev);
  1255. if (device_id >= ATH12K_DEVICE_FAMILY_MAX) {
  1256. ath12k_err(ab, "failed to get device family id\n");
  1257. ret = -EINVAL;
  1258. goto err_pci_free_region;
  1259. }
  1260. ath12k_dbg(ab, ATH12K_DBG_PCI, "PCI device family id: %d\n", device_id);
  1261. ab_pci->device_family_ops = &ath12k_pci_family_drivers[device_id]->ops;
  1262. ab_pci->reg_base = ath12k_pci_family_drivers[device_id]->reg_base;
  1263. /* Call device specific probe. This is the callback that can
  1264. * be used to override any ops in future
  1265. * probe is validated for NULL during registration.
  1266. */
  1267. ret = ab_pci->device_family_ops->probe(pdev, pci_dev);
  1268. if (ret) {
  1269. ath12k_err(ab, "failed to probe device: %d\n", ret);
  1270. goto err_pci_free_region;
  1271. }
  1272. ret = ath12k_pci_msi_alloc(ab_pci);
  1273. if (ret) {
  1274. ath12k_err(ab, "failed to alloc msi: %d\n", ret);
  1275. goto err_pci_free_region;
  1276. }
  1277. ret = ath12k_core_pre_init(ab);
  1278. if (ret)
  1279. goto err_pci_msi_free;
  1280. ret = ath12k_pci_set_irq_affinity_hint(ab_pci, cpumask_of(0));
  1281. if (ret) {
  1282. ath12k_err(ab, "failed to set irq affinity %d\n", ret);
  1283. goto err_pci_msi_free;
  1284. }
  1285. ret = ath12k_mhi_register(ab_pci);
  1286. if (ret) {
  1287. ath12k_err(ab, "failed to register mhi: %d\n", ret);
  1288. goto err_irq_affinity_cleanup;
  1289. }
  1290. ret = ath12k_hal_srng_init(ab);
  1291. if (ret)
  1292. goto err_mhi_unregister;
  1293. ret = ath12k_ce_alloc_pipes(ab);
  1294. if (ret) {
  1295. ath12k_err(ab, "failed to allocate ce pipes: %d\n", ret);
  1296. goto err_hal_srng_deinit;
  1297. }
  1298. ath12k_pci_init_qmi_ce_config(ab);
  1299. ret = ath12k_pci_config_irq(ab);
  1300. if (ret) {
  1301. ath12k_err(ab, "failed to config irq: %d\n", ret);
  1302. goto err_ce_free;
  1303. }
  1304. /* kernel may allocate a dummy vector before request_irq and
  1305. * then allocate a real vector when request_irq is called.
  1306. * So get msi_data here again to avoid spurious interrupt
  1307. * as msi_data will configured to srngs.
  1308. */
  1309. ret = ath12k_pci_config_msi_data(ab_pci);
  1310. if (ret) {
  1311. ath12k_err(ab, "failed to config msi_data: %d\n", ret);
  1312. goto err_free_irq;
  1313. }
  1314. /* Invoke arch_init here so that arch-specific init operations
  1315. * can utilize already initialized ab fields, such as HAL SRNGs.
  1316. */
  1317. ret = ab_pci->device_family_ops->arch_init(ab);
  1318. if (ret) {
  1319. ath12k_err(ab, "PCI arch_init failed %d\n", ret);
  1320. goto err_pci_msi_free;
  1321. }
  1322. ret = ath12k_core_init(ab);
  1323. if (ret) {
  1324. ath12k_err(ab, "failed to init core: %d\n", ret);
  1325. goto err_deinit_arch;
  1326. }
  1327. return 0;
  1328. err_deinit_arch:
  1329. ab_pci->device_family_ops->arch_deinit(ab);
  1330. err_free_irq:
  1331. /* __free_irq() expects the caller to have cleared the affinity hint */
  1332. ath12k_pci_set_irq_affinity_hint(ab_pci, NULL);
  1333. ath12k_pci_free_irq(ab);
  1334. err_ce_free:
  1335. ath12k_ce_free_pipes(ab);
  1336. err_hal_srng_deinit:
  1337. ath12k_hal_srng_deinit(ab);
  1338. err_mhi_unregister:
  1339. ath12k_mhi_unregister(ab_pci);
  1340. err_irq_affinity_cleanup:
  1341. ath12k_pci_set_irq_affinity_hint(ab_pci, NULL);
  1342. err_pci_msi_free:
  1343. ath12k_pci_msi_free(ab_pci);
  1344. err_pci_free_region:
  1345. ath12k_pci_free_region(ab_pci);
  1346. err_free_core:
  1347. ath12k_core_free(ab);
  1348. return ret;
  1349. }
  1350. static void ath12k_pci_remove(struct pci_dev *pdev)
  1351. {
  1352. struct ath12k_base *ab = pci_get_drvdata(pdev);
  1353. struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
  1354. ath12k_pci_set_irq_affinity_hint(ab_pci, NULL);
  1355. if (test_bit(ATH12K_FLAG_QMI_FAIL, &ab->dev_flags)) {
  1356. ath12k_pci_power_down(ab, false);
  1357. goto qmi_fail;
  1358. }
  1359. set_bit(ATH12K_FLAG_UNREGISTERING, &ab->dev_flags);
  1360. cancel_work_sync(&ab->reset_work);
  1361. cancel_work_sync(&ab->dump_work);
  1362. ath12k_core_hw_group_cleanup(ab->ag);
  1363. qmi_fail:
  1364. ath12k_core_deinit(ab);
  1365. ath12k_fw_unmap(ab);
  1366. ath12k_mhi_unregister(ab_pci);
  1367. ath12k_pci_free_irq(ab);
  1368. ath12k_pci_msi_free(ab_pci);
  1369. ath12k_pci_free_region(ab_pci);
  1370. ath12k_hal_srng_deinit(ab);
  1371. ath12k_ce_free_pipes(ab);
  1372. ab_pci->device_family_ops->arch_deinit(ab);
  1373. ath12k_core_free(ab);
  1374. }
  1375. static void ath12k_pci_hw_group_power_down(struct ath12k_hw_group *ag)
  1376. {
  1377. struct ath12k_base *ab;
  1378. int i;
  1379. if (!ag)
  1380. return;
  1381. mutex_lock(&ag->mutex);
  1382. for (i = 0; i < ag->num_devices; i++) {
  1383. ab = ag->ab[i];
  1384. if (!ab)
  1385. continue;
  1386. ath12k_pci_power_down(ab, false);
  1387. }
  1388. mutex_unlock(&ag->mutex);
  1389. }
  1390. static void ath12k_pci_shutdown(struct pci_dev *pdev)
  1391. {
  1392. struct ath12k_base *ab = pci_get_drvdata(pdev);
  1393. struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
  1394. ath12k_pci_set_irq_affinity_hint(ab_pci, NULL);
  1395. ath12k_pci_hw_group_power_down(ab->ag);
  1396. }
  1397. static __maybe_unused int ath12k_pci_pm_suspend(struct device *dev)
  1398. {
  1399. struct ath12k_base *ab = dev_get_drvdata(dev);
  1400. int ret;
  1401. ret = ath12k_core_suspend(ab);
  1402. if (ret)
  1403. ath12k_warn(ab, "failed to suspend core: %d\n", ret);
  1404. return ret;
  1405. }
  1406. static __maybe_unused int ath12k_pci_pm_resume(struct device *dev)
  1407. {
  1408. struct ath12k_base *ab = dev_get_drvdata(dev);
  1409. int ret;
  1410. ret = ath12k_core_resume(ab);
  1411. if (ret)
  1412. ath12k_warn(ab, "failed to resume core: %d\n", ret);
  1413. return ret;
  1414. }
  1415. static __maybe_unused int ath12k_pci_pm_suspend_late(struct device *dev)
  1416. {
  1417. struct ath12k_base *ab = dev_get_drvdata(dev);
  1418. int ret;
  1419. ret = ath12k_core_suspend_late(ab);
  1420. if (ret)
  1421. ath12k_warn(ab, "failed to late suspend core: %d\n", ret);
  1422. return ret;
  1423. }
  1424. static __maybe_unused int ath12k_pci_pm_resume_early(struct device *dev)
  1425. {
  1426. struct ath12k_base *ab = dev_get_drvdata(dev);
  1427. int ret;
  1428. ret = ath12k_core_resume_early(ab);
  1429. if (ret)
  1430. ath12k_warn(ab, "failed to early resume core: %d\n", ret);
  1431. return ret;
  1432. }
  1433. static const struct dev_pm_ops __maybe_unused ath12k_pci_pm_ops = {
  1434. SET_SYSTEM_SLEEP_PM_OPS(ath12k_pci_pm_suspend,
  1435. ath12k_pci_pm_resume)
  1436. SET_LATE_SYSTEM_SLEEP_PM_OPS(ath12k_pci_pm_suspend_late,
  1437. ath12k_pci_pm_resume_early)
  1438. };
  1439. int ath12k_pci_register_driver(const enum ath12k_device_family device_id,
  1440. struct ath12k_pci_driver *driver)
  1441. {
  1442. struct pci_driver *pci_driver;
  1443. if (device_id >= ATH12K_DEVICE_FAMILY_MAX)
  1444. return -EINVAL;
  1445. if (!driver || !driver->ops.probe ||
  1446. !driver->ops.arch_init || !driver->ops.arch_deinit)
  1447. return -EINVAL;
  1448. if (ath12k_pci_family_drivers[device_id]) {
  1449. pr_err("Driver already registered for %d\n", device_id);
  1450. return -EALREADY;
  1451. }
  1452. ath12k_pci_family_drivers[device_id] = driver;
  1453. pci_driver = &ath12k_pci_family_drivers[device_id]->driver;
  1454. pci_driver->name = driver->name;
  1455. pci_driver->id_table = driver->id_table;
  1456. pci_driver->probe = ath12k_pci_probe;
  1457. pci_driver->remove = ath12k_pci_remove;
  1458. pci_driver->shutdown = ath12k_pci_shutdown;
  1459. pci_driver->driver.pm = &ath12k_pci_pm_ops;
  1460. return pci_register_driver(pci_driver);
  1461. }
  1462. EXPORT_SYMBOL(ath12k_pci_register_driver);
  1463. void ath12k_pci_unregister_driver(const enum ath12k_device_family device_id)
  1464. {
  1465. if (device_id >= ATH12K_DEVICE_FAMILY_MAX ||
  1466. !ath12k_pci_family_drivers[device_id])
  1467. return;
  1468. pci_unregister_driver(&ath12k_pci_family_drivers[device_id]->driver);
  1469. ath12k_pci_family_drivers[device_id] = NULL;
  1470. }
  1471. EXPORT_SYMBOL(ath12k_pci_unregister_driver);
  1472. /* firmware files */
  1473. MODULE_FIRMWARE(ATH12K_FW_DIR "/QCN9274/hw2.0/*");
  1474. MODULE_FIRMWARE(ATH12K_FW_DIR "/WCN7850/hw2.0/*");