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