1 // SPDX-License-Identifier: BSD-3-Clause-Clear
3 * Copyright (c) 2019-2021 The Linux Foundation. All rights reserved.
4 * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
7 #include <linux/module.h>
17 #define ATH12K_PCI_BAR_NUM 0
18 #define ATH12K_PCI_DMA_MASK 32
20 #define ATH12K_PCI_IRQ_CE0_OFFSET 3
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)
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)
33 /* BAR0 + 4k is always accessible, and no
34 * need to force wakeup.
37 #define ACCESS_ALWAYS_OFF 0xFE0
39 #define QCN9274_DEVICE_ID 0x1109
40 #define WCN7850_DEVICE_ID 0x1107
42 static const struct pci_device_id ath12k_pci_id_table[] = {
43 { PCI_VDEVICE(QCOM, QCN9274_DEVICE_ID) },
44 { PCI_VDEVICE(QCOM, WCN7850_DEVICE_ID) },
48 MODULE_DEVICE_TABLE(pci, ath12k_pci_id_table);
50 /* TODO: revisit IRQ mapping for new SRNG's */
51 static const struct ath12k_msi_config ath12k_msi_config[] = {
55 .users = (struct ath12k_msi_user[]) {
56 { .name = "MHI", .num_vectors = 3, .base_vector = 0 },
57 { .name = "CE", .num_vectors = 5, .base_vector = 3 },
58 { .name = "DP", .num_vectors = 8, .base_vector = 8 },
63 static const char *irq_name[ATH12K_IRQ_NUM_MAX] = {
84 "host2reo-re-injection",
86 "host2rxdma-monitor-ring3",
87 "host2rxdma-monitor-ring2",
88 "host2rxdma-monitor-ring1",
90 "wbm2host-rx-release",
92 "reo2host-destination-ring4",
93 "reo2host-destination-ring3",
94 "reo2host-destination-ring2",
95 "reo2host-destination-ring1",
96 "rxdma2host-monitor-destination-mac3",
97 "rxdma2host-monitor-destination-mac2",
98 "rxdma2host-monitor-destination-mac1",
99 "ppdu-end-interrupts-mac3",
100 "ppdu-end-interrupts-mac2",
101 "ppdu-end-interrupts-mac1",
102 "rxdma2host-monitor-status-ring-mac3",
103 "rxdma2host-monitor-status-ring-mac2",
104 "rxdma2host-monitor-status-ring-mac1",
105 "host2rxdma-host-buf-ring-mac3",
106 "host2rxdma-host-buf-ring-mac2",
107 "host2rxdma-host-buf-ring-mac1",
108 "rxdma2host-destination-ring-mac3",
109 "rxdma2host-destination-ring-mac2",
110 "rxdma2host-destination-ring-mac1",
111 "host2tcl-input-ring4",
112 "host2tcl-input-ring3",
113 "host2tcl-input-ring2",
114 "host2tcl-input-ring1",
115 "wbm2host-tx-completions-ring4",
116 "wbm2host-tx-completions-ring3",
117 "wbm2host-tx-completions-ring2",
118 "wbm2host-tx-completions-ring1",
119 "tcl2host-status-ring",
122 static int ath12k_pci_bus_wake_up(struct ath12k_base *ab)
124 struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
126 return mhi_device_get_sync(ab_pci->mhi_ctrl->mhi_dev);
129 static void ath12k_pci_bus_release(struct ath12k_base *ab)
131 struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
133 mhi_device_put(ab_pci->mhi_ctrl->mhi_dev);
136 static const struct ath12k_pci_ops ath12k_pci_ops_qcn9274 = {
141 static const struct ath12k_pci_ops ath12k_pci_ops_wcn7850 = {
142 .wakeup = ath12k_pci_bus_wake_up,
143 .release = ath12k_pci_bus_release,
146 static void ath12k_pci_select_window(struct ath12k_pci *ab_pci, u32 offset)
148 struct ath12k_base *ab = ab_pci->ab;
150 u32 window = u32_get_bits(offset, WINDOW_VALUE_MASK);
153 lockdep_assert_held(&ab_pci->window_lock);
155 /* Preserve the static window configuration and reset only dynamic window */
156 static_window = ab_pci->register_window & WINDOW_STATIC_MASK;
157 window |= static_window;
159 if (window != ab_pci->register_window) {
160 iowrite32(WINDOW_ENABLE_BIT | window,
161 ab->mem + WINDOW_REG_ADDRESS);
162 ioread32(ab->mem + WINDOW_REG_ADDRESS);
163 ab_pci->register_window = window;
167 static void ath12k_pci_select_static_window(struct ath12k_pci *ab_pci)
169 u32 umac_window = u32_get_bits(HAL_SEQ_WCSS_UMAC_OFFSET, WINDOW_VALUE_MASK);
170 u32 ce_window = u32_get_bits(HAL_CE_WFSS_CE_REG_BASE, WINDOW_VALUE_MASK);
173 window = (umac_window << 12) | (ce_window << 6);
175 spin_lock_bh(&ab_pci->window_lock);
176 ab_pci->register_window = window;
177 spin_unlock_bh(&ab_pci->window_lock);
179 iowrite32(WINDOW_ENABLE_BIT | window, ab_pci->ab->mem + WINDOW_REG_ADDRESS);
182 static u32 ath12k_pci_get_window_start(struct ath12k_base *ab,
187 /* If offset lies within DP register range, use 3rd window */
188 if ((offset ^ HAL_SEQ_WCSS_UMAC_OFFSET) < WINDOW_RANGE_MASK)
189 window_start = 3 * WINDOW_START;
190 /* If offset lies within CE register range, use 2nd window */
191 else if ((offset ^ HAL_CE_WFSS_CE_REG_BASE) < WINDOW_RANGE_MASK)
192 window_start = 2 * WINDOW_START;
193 /* If offset lies within PCI_BAR_WINDOW0_BASE and within PCI_SOC_PCI_REG_BASE
196 else if (((offset ^ PCI_BAR_WINDOW0_BASE) < WINDOW_RANGE_MASK) &&
197 !((offset ^ PCI_SOC_PCI_REG_BASE) < PCI_SOC_RANGE_MASK))
200 window_start = WINDOW_START;
205 static void ath12k_pci_soc_global_reset(struct ath12k_base *ab)
209 val = ath12k_pci_read32(ab, PCIE_SOC_GLOBAL_RESET);
211 val |= PCIE_SOC_GLOBAL_RESET_V;
213 ath12k_pci_write32(ab, PCIE_SOC_GLOBAL_RESET, val);
215 /* TODO: exact time to sleep is uncertain */
219 /* Need to toggle V bit back otherwise stuck in reset status */
220 val &= ~PCIE_SOC_GLOBAL_RESET_V;
222 ath12k_pci_write32(ab, PCIE_SOC_GLOBAL_RESET, val);
226 val = ath12k_pci_read32(ab, PCIE_SOC_GLOBAL_RESET);
227 if (val == 0xffffffff)
228 ath12k_warn(ab, "link down error during global reset\n");
231 static void ath12k_pci_clear_dbg_registers(struct ath12k_base *ab)
236 val = ath12k_pci_read32(ab, PCIE_Q6_COOKIE_ADDR);
237 ath12k_dbg(ab, ATH12K_DBG_PCI, "cookie:0x%x\n", val);
239 val = ath12k_pci_read32(ab, WLAON_WARM_SW_ENTRY);
240 ath12k_dbg(ab, ATH12K_DBG_PCI, "WLAON_WARM_SW_ENTRY 0x%x\n", val);
242 /* TODO: exact time to sleep is uncertain */
245 /* write 0 to WLAON_WARM_SW_ENTRY to prevent Q6 from
246 * continuing warm path and entering dead loop.
248 ath12k_pci_write32(ab, WLAON_WARM_SW_ENTRY, 0);
251 val = ath12k_pci_read32(ab, WLAON_WARM_SW_ENTRY);
252 ath12k_dbg(ab, ATH12K_DBG_PCI, "WLAON_WARM_SW_ENTRY 0x%x\n", val);
254 /* A read clear register. clear the register to prevent
255 * Q6 from entering wrong code path.
257 val = ath12k_pci_read32(ab, WLAON_SOC_RESET_CAUSE_REG);
258 ath12k_dbg(ab, ATH12K_DBG_PCI, "soc reset cause:%d\n", val);
261 static void ath12k_pci_enable_ltssm(struct ath12k_base *ab)
266 val = ath12k_pci_read32(ab, PCIE_PCIE_PARF_LTSSM);
268 /* PCIE link seems very unstable after the Hot Reset*/
269 for (i = 0; val != PARM_LTSSM_VALUE && i < 5; i++) {
270 if (val == 0xffffffff)
273 ath12k_pci_write32(ab, PCIE_PCIE_PARF_LTSSM, PARM_LTSSM_VALUE);
274 val = ath12k_pci_read32(ab, PCIE_PCIE_PARF_LTSSM);
277 ath12k_dbg(ab, ATH12K_DBG_PCI, "pci ltssm 0x%x\n", val);
279 val = ath12k_pci_read32(ab, GCC_GCC_PCIE_HOT_RST);
280 val |= GCC_GCC_PCIE_HOT_RST_VAL;
281 ath12k_pci_write32(ab, GCC_GCC_PCIE_HOT_RST, val);
282 val = ath12k_pci_read32(ab, GCC_GCC_PCIE_HOT_RST);
284 ath12k_dbg(ab, ATH12K_DBG_PCI, "pci pcie_hot_rst 0x%x\n", val);
289 static void ath12k_pci_clear_all_intrs(struct ath12k_base *ab)
291 /* This is a WAR for PCIE Hotreset.
292 * When target receive Hotreset, but will set the interrupt.
293 * So when download SBL again, SBL will open Interrupt and
294 * receive it, and crash immediately.
296 ath12k_pci_write32(ab, PCIE_PCIE_INT_ALL_CLEAR, PCIE_INT_CLEAR_ALL);
299 static void ath12k_pci_set_wlaon_pwr_ctrl(struct ath12k_base *ab)
303 val = ath12k_pci_read32(ab, WLAON_QFPROM_PWR_CTRL_REG);
304 val &= ~QFPROM_PWR_CTRL_VDD4BLOW_MASK;
305 ath12k_pci_write32(ab, WLAON_QFPROM_PWR_CTRL_REG, val);
308 static void ath12k_pci_force_wake(struct ath12k_base *ab)
310 ath12k_pci_write32(ab, PCIE_SOC_WAKE_PCIE_LOCAL_REG, 1);
314 static void ath12k_pci_sw_reset(struct ath12k_base *ab, bool power_on)
317 ath12k_pci_enable_ltssm(ab);
318 ath12k_pci_clear_all_intrs(ab);
319 ath12k_pci_set_wlaon_pwr_ctrl(ab);
322 ath12k_mhi_clear_vector(ab);
323 ath12k_pci_clear_dbg_registers(ab);
324 ath12k_pci_soc_global_reset(ab);
325 ath12k_mhi_set_mhictrl_reset(ab);
328 static void ath12k_pci_free_ext_irq(struct ath12k_base *ab)
332 for (i = 0; i < ATH12K_EXT_IRQ_GRP_NUM_MAX; i++) {
333 struct ath12k_ext_irq_grp *irq_grp = &ab->ext_irq_grp[i];
335 for (j = 0; j < irq_grp->num_irq; j++)
336 free_irq(ab->irq_num[irq_grp->irqs[j]], irq_grp);
338 netif_napi_del(&irq_grp->napi);
342 static void ath12k_pci_free_irq(struct ath12k_base *ab)
346 for (i = 0; i < ab->hw_params->ce_count; i++) {
347 if (ath12k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
349 irq_idx = ATH12K_PCI_IRQ_CE0_OFFSET + i;
350 free_irq(ab->irq_num[irq_idx], &ab->ce.ce_pipe[i]);
353 ath12k_pci_free_ext_irq(ab);
356 static void ath12k_pci_ce_irq_enable(struct ath12k_base *ab, u16 ce_id)
360 irq_idx = ATH12K_PCI_IRQ_CE0_OFFSET + ce_id;
361 enable_irq(ab->irq_num[irq_idx]);
364 static void ath12k_pci_ce_irq_disable(struct ath12k_base *ab, u16 ce_id)
368 irq_idx = ATH12K_PCI_IRQ_CE0_OFFSET + ce_id;
369 disable_irq_nosync(ab->irq_num[irq_idx]);
372 static void ath12k_pci_ce_irqs_disable(struct ath12k_base *ab)
376 for (i = 0; i < ab->hw_params->ce_count; i++) {
377 if (ath12k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
379 ath12k_pci_ce_irq_disable(ab, i);
383 static void ath12k_pci_sync_ce_irqs(struct ath12k_base *ab)
388 for (i = 0; i < ab->hw_params->ce_count; i++) {
389 if (ath12k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
392 irq_idx = ATH12K_PCI_IRQ_CE0_OFFSET + i;
393 synchronize_irq(ab->irq_num[irq_idx]);
397 static void ath12k_pci_ce_tasklet(struct tasklet_struct *t)
399 struct ath12k_ce_pipe *ce_pipe = from_tasklet(ce_pipe, t, intr_tq);
401 ath12k_ce_per_engine_service(ce_pipe->ab, ce_pipe->pipe_num);
403 ath12k_pci_ce_irq_enable(ce_pipe->ab, ce_pipe->pipe_num);
406 static irqreturn_t ath12k_pci_ce_interrupt_handler(int irq, void *arg)
408 struct ath12k_ce_pipe *ce_pipe = arg;
410 /* last interrupt received for this CE */
411 ce_pipe->timestamp = jiffies;
413 ath12k_pci_ce_irq_disable(ce_pipe->ab, ce_pipe->pipe_num);
414 tasklet_schedule(&ce_pipe->intr_tq);
419 static void ath12k_pci_ext_grp_disable(struct ath12k_ext_irq_grp *irq_grp)
423 for (i = 0; i < irq_grp->num_irq; i++)
424 disable_irq_nosync(irq_grp->ab->irq_num[irq_grp->irqs[i]]);
427 static void __ath12k_pci_ext_irq_disable(struct ath12k_base *sc)
431 for (i = 0; i < ATH12K_EXT_IRQ_GRP_NUM_MAX; i++) {
432 struct ath12k_ext_irq_grp *irq_grp = &sc->ext_irq_grp[i];
434 ath12k_pci_ext_grp_disable(irq_grp);
436 napi_synchronize(&irq_grp->napi);
437 napi_disable(&irq_grp->napi);
441 static void ath12k_pci_ext_grp_enable(struct ath12k_ext_irq_grp *irq_grp)
445 for (i = 0; i < irq_grp->num_irq; i++)
446 enable_irq(irq_grp->ab->irq_num[irq_grp->irqs[i]]);
449 static void ath12k_pci_sync_ext_irqs(struct ath12k_base *ab)
453 for (i = 0; i < ATH12K_EXT_IRQ_GRP_NUM_MAX; i++) {
454 struct ath12k_ext_irq_grp *irq_grp = &ab->ext_irq_grp[i];
456 for (j = 0; j < irq_grp->num_irq; j++) {
457 irq_idx = irq_grp->irqs[j];
458 synchronize_irq(ab->irq_num[irq_idx]);
463 static int ath12k_pci_ext_grp_napi_poll(struct napi_struct *napi, int budget)
465 struct ath12k_ext_irq_grp *irq_grp = container_of(napi,
466 struct ath12k_ext_irq_grp,
468 struct ath12k_base *ab = irq_grp->ab;
471 work_done = ath12k_dp_service_srng(ab, irq_grp, budget);
472 if (work_done < budget) {
473 napi_complete_done(napi, work_done);
474 ath12k_pci_ext_grp_enable(irq_grp);
477 if (work_done > budget)
483 static irqreturn_t ath12k_pci_ext_interrupt_handler(int irq, void *arg)
485 struct ath12k_ext_irq_grp *irq_grp = arg;
487 ath12k_dbg(irq_grp->ab, ATH12K_DBG_PCI, "ext irq:%d\n", irq);
489 /* last interrupt received for this group */
490 irq_grp->timestamp = jiffies;
492 ath12k_pci_ext_grp_disable(irq_grp);
494 napi_schedule(&irq_grp->napi);
499 static int ath12k_pci_ext_irq_config(struct ath12k_base *ab)
501 int i, j, ret, num_vectors = 0;
502 u32 user_base_data = 0, base_vector = 0, base_idx;
504 base_idx = ATH12K_PCI_IRQ_CE0_OFFSET + CE_COUNT_MAX;
505 ret = ath12k_pci_get_user_msi_assignment(ab, "DP",
512 for (i = 0; i < ATH12K_EXT_IRQ_GRP_NUM_MAX; i++) {
513 struct ath12k_ext_irq_grp *irq_grp = &ab->ext_irq_grp[i];
518 init_dummy_netdev(&irq_grp->napi_ndev);
519 netif_napi_add(&irq_grp->napi_ndev, &irq_grp->napi,
520 ath12k_pci_ext_grp_napi_poll);
522 if (ab->hw_params->ring_mask->tx[i] ||
523 ab->hw_params->ring_mask->rx[i] ||
524 ab->hw_params->ring_mask->rx_err[i] ||
525 ab->hw_params->ring_mask->rx_wbm_rel[i] ||
526 ab->hw_params->ring_mask->reo_status[i] ||
527 ab->hw_params->ring_mask->host2rxdma[i] ||
528 ab->hw_params->ring_mask->rx_mon_dest[i]) {
532 irq_grp->num_irq = num_irq;
533 irq_grp->irqs[0] = base_idx + i;
535 for (j = 0; j < irq_grp->num_irq; j++) {
536 int irq_idx = irq_grp->irqs[j];
537 int vector = (i % num_vectors) + base_vector;
538 int irq = ath12k_pci_get_msi_irq(ab->dev, vector);
540 ab->irq_num[irq_idx] = irq;
542 ath12k_dbg(ab, ATH12K_DBG_PCI,
543 "irq:%d group:%d\n", irq, i);
545 irq_set_status_flags(irq, IRQ_DISABLE_UNLAZY);
546 ret = request_irq(irq, ath12k_pci_ext_interrupt_handler,
548 "DP_EXT_IRQ", irq_grp);
550 ath12k_err(ab, "failed request irq %d: %d\n",
555 disable_irq_nosync(ab->irq_num[irq_idx]);
562 static int ath12k_pci_config_irq(struct ath12k_base *ab)
564 struct ath12k_ce_pipe *ce_pipe;
566 u32 msi_data_count, msi_data_idx;
568 unsigned int msi_data;
569 int irq, i, ret, irq_idx;
571 ret = ath12k_pci_get_user_msi_assignment(ab,
572 "CE", &msi_data_count,
573 &msi_data_start, &msi_irq_start);
577 /* Configure CE irqs */
579 for (i = 0, msi_data_idx = 0; i < ab->hw_params->ce_count; i++) {
580 if (ath12k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
583 msi_data = (msi_data_idx % msi_data_count) + msi_irq_start;
584 irq = ath12k_pci_get_msi_irq(ab->dev, msi_data);
585 ce_pipe = &ab->ce.ce_pipe[i];
587 irq_idx = ATH12K_PCI_IRQ_CE0_OFFSET + i;
589 tasklet_setup(&ce_pipe->intr_tq, ath12k_pci_ce_tasklet);
591 ret = request_irq(irq, ath12k_pci_ce_interrupt_handler,
592 IRQF_SHARED, irq_name[irq_idx],
595 ath12k_err(ab, "failed to request irq %d: %d\n",
600 ab->irq_num[irq_idx] = irq;
603 ath12k_pci_ce_irq_disable(ab, i);
606 ret = ath12k_pci_ext_irq_config(ab);
613 static void ath12k_pci_init_qmi_ce_config(struct ath12k_base *ab)
615 struct ath12k_qmi_ce_cfg *cfg = &ab->qmi.ce_cfg;
617 cfg->tgt_ce = ab->hw_params->target_ce_config;
618 cfg->tgt_ce_len = ab->hw_params->target_ce_count;
620 cfg->svc_to_ce_map = ab->hw_params->svc_to_ce_map;
621 cfg->svc_to_ce_map_len = ab->hw_params->svc_to_ce_map_len;
622 ab->qmi.service_ins_id = ab->hw_params->qmi_service_ins_id;
625 static void ath12k_pci_ce_irqs_enable(struct ath12k_base *ab)
629 for (i = 0; i < ab->hw_params->ce_count; i++) {
630 if (ath12k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
632 ath12k_pci_ce_irq_enable(ab, i);
636 static void ath12k_pci_msi_config(struct ath12k_pci *ab_pci, bool enable)
638 struct pci_dev *dev = ab_pci->pdev;
641 pci_read_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, &control);
644 control |= PCI_MSI_FLAGS_ENABLE;
646 control &= ~PCI_MSI_FLAGS_ENABLE;
648 pci_write_config_word(dev, dev->msi_cap + PCI_MSI_FLAGS, control);
651 static void ath12k_pci_msi_enable(struct ath12k_pci *ab_pci)
653 ath12k_pci_msi_config(ab_pci, true);
656 static void ath12k_pci_msi_disable(struct ath12k_pci *ab_pci)
658 ath12k_pci_msi_config(ab_pci, false);
661 static int ath12k_pci_msi_alloc(struct ath12k_pci *ab_pci)
663 struct ath12k_base *ab = ab_pci->ab;
664 const struct ath12k_msi_config *msi_config = ab_pci->msi_config;
665 struct msi_desc *msi_desc;
669 num_vectors = pci_alloc_irq_vectors(ab_pci->pdev,
670 msi_config->total_vectors,
671 msi_config->total_vectors,
673 if (num_vectors != msi_config->total_vectors) {
674 ath12k_err(ab, "failed to get %d MSI vectors, only %d available",
675 msi_config->total_vectors, num_vectors);
677 if (num_vectors >= 0)
683 ath12k_pci_msi_disable(ab_pci);
685 msi_desc = irq_get_msi_desc(ab_pci->pdev->irq);
687 ath12k_err(ab, "msi_desc is NULL!\n");
689 goto free_msi_vector;
692 ab_pci->msi_ep_base_data = msi_desc->msg.data;
693 if (msi_desc->pci.msi_attrib.is_64)
694 set_bit(ATH12K_PCI_FLAG_IS_MSI_64, &ab_pci->flags);
696 ath12k_dbg(ab, ATH12K_DBG_PCI, "msi base data is %d\n", ab_pci->msi_ep_base_data);
701 pci_free_irq_vectors(ab_pci->pdev);
706 static void ath12k_pci_msi_free(struct ath12k_pci *ab_pci)
708 pci_free_irq_vectors(ab_pci->pdev);
711 static int ath12k_pci_claim(struct ath12k_pci *ab_pci, struct pci_dev *pdev)
713 struct ath12k_base *ab = ab_pci->ab;
717 pci_read_config_word(pdev, PCI_DEVICE_ID, &device_id);
718 if (device_id != ab_pci->dev_id) {
719 ath12k_err(ab, "pci device id mismatch: 0x%x 0x%x\n",
720 device_id, ab_pci->dev_id);
725 ret = pci_assign_resource(pdev, ATH12K_PCI_BAR_NUM);
727 ath12k_err(ab, "failed to assign pci resource: %d\n", ret);
731 ret = pci_enable_device(pdev);
733 ath12k_err(ab, "failed to enable pci device: %d\n", ret);
737 ret = pci_request_region(pdev, ATH12K_PCI_BAR_NUM, "ath12k_pci");
739 ath12k_err(ab, "failed to request pci region: %d\n", ret);
743 ret = dma_set_mask_and_coherent(&pdev->dev,
744 DMA_BIT_MASK(ATH12K_PCI_DMA_MASK));
746 ath12k_err(ab, "failed to set pci dma mask to %d: %d\n",
747 ATH12K_PCI_DMA_MASK, ret);
751 pci_set_master(pdev);
753 ab->mem_len = pci_resource_len(pdev, ATH12K_PCI_BAR_NUM);
754 ab->mem = pci_iomap(pdev, ATH12K_PCI_BAR_NUM, 0);
756 ath12k_err(ab, "failed to map pci bar %d\n", ATH12K_PCI_BAR_NUM);
761 ath12k_dbg(ab, ATH12K_DBG_BOOT, "boot pci_mem 0x%pK\n", ab->mem);
765 pci_release_region(pdev, ATH12K_PCI_BAR_NUM);
767 pci_disable_device(pdev);
772 static void ath12k_pci_free_region(struct ath12k_pci *ab_pci)
774 struct ath12k_base *ab = ab_pci->ab;
775 struct pci_dev *pci_dev = ab_pci->pdev;
777 pci_iounmap(pci_dev, ab->mem);
779 pci_release_region(pci_dev, ATH12K_PCI_BAR_NUM);
780 if (pci_is_enabled(pci_dev))
781 pci_disable_device(pci_dev);
784 static void ath12k_pci_aspm_disable(struct ath12k_pci *ab_pci)
786 struct ath12k_base *ab = ab_pci->ab;
788 pcie_capability_read_word(ab_pci->pdev, PCI_EXP_LNKCTL,
791 ath12k_dbg(ab, ATH12K_DBG_PCI, "pci link_ctl 0x%04x L0s %d L1 %d\n",
793 u16_get_bits(ab_pci->link_ctl, PCI_EXP_LNKCTL_ASPM_L0S),
794 u16_get_bits(ab_pci->link_ctl, PCI_EXP_LNKCTL_ASPM_L1));
796 /* disable L0s and L1 */
797 pcie_capability_clear_word(ab_pci->pdev, PCI_EXP_LNKCTL,
798 PCI_EXP_LNKCTL_ASPMC);
800 set_bit(ATH12K_PCI_ASPM_RESTORE, &ab_pci->flags);
803 static void ath12k_pci_aspm_restore(struct ath12k_pci *ab_pci)
805 if (test_and_clear_bit(ATH12K_PCI_ASPM_RESTORE, &ab_pci->flags))
806 pcie_capability_clear_and_set_word(ab_pci->pdev, PCI_EXP_LNKCTL,
807 PCI_EXP_LNKCTL_ASPMC,
809 PCI_EXP_LNKCTL_ASPMC);
812 static void ath12k_pci_kill_tasklets(struct ath12k_base *ab)
816 for (i = 0; i < ab->hw_params->ce_count; i++) {
817 struct ath12k_ce_pipe *ce_pipe = &ab->ce.ce_pipe[i];
819 if (ath12k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
822 tasklet_kill(&ce_pipe->intr_tq);
826 static void ath12k_pci_ce_irq_disable_sync(struct ath12k_base *ab)
828 ath12k_pci_ce_irqs_disable(ab);
829 ath12k_pci_sync_ce_irqs(ab);
830 ath12k_pci_kill_tasklets(ab);
833 int ath12k_pci_map_service_to_pipe(struct ath12k_base *ab, u16 service_id,
834 u8 *ul_pipe, u8 *dl_pipe)
836 const struct service_to_pipe *entry;
837 bool ul_set = false, dl_set = false;
840 for (i = 0; i < ab->hw_params->svc_to_ce_map_len; i++) {
841 entry = &ab->hw_params->svc_to_ce_map[i];
843 if (__le32_to_cpu(entry->service_id) != service_id)
846 switch (__le32_to_cpu(entry->pipedir)) {
851 *dl_pipe = __le32_to_cpu(entry->pipenum);
856 *ul_pipe = __le32_to_cpu(entry->pipenum);
862 *dl_pipe = __le32_to_cpu(entry->pipenum);
863 *ul_pipe = __le32_to_cpu(entry->pipenum);
870 if (WARN_ON(!ul_set || !dl_set))
876 int ath12k_pci_get_msi_irq(struct device *dev, unsigned int vector)
878 struct pci_dev *pci_dev = to_pci_dev(dev);
880 return pci_irq_vector(pci_dev, vector);
883 int ath12k_pci_get_user_msi_assignment(struct ath12k_base *ab, char *user_name,
884 int *num_vectors, u32 *user_base_data,
887 struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
888 const struct ath12k_msi_config *msi_config = ab_pci->msi_config;
891 for (idx = 0; idx < msi_config->total_users; idx++) {
892 if (strcmp(user_name, msi_config->users[idx].name) == 0) {
893 *num_vectors = msi_config->users[idx].num_vectors;
894 *user_base_data = msi_config->users[idx].base_vector
895 + ab_pci->msi_ep_base_data;
896 *base_vector = msi_config->users[idx].base_vector;
898 ath12k_dbg(ab, ATH12K_DBG_PCI, "Assign MSI to user: %s, num_vectors: %d, user_base_data: %u, base_vector: %u\n",
899 user_name, *num_vectors, *user_base_data,
906 ath12k_err(ab, "Failed to find MSI assignment for %s!\n", user_name);
911 void ath12k_pci_get_msi_address(struct ath12k_base *ab, u32 *msi_addr_lo,
914 struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
915 struct pci_dev *pci_dev = to_pci_dev(ab->dev);
917 pci_read_config_dword(pci_dev, pci_dev->msi_cap + PCI_MSI_ADDRESS_LO,
920 if (test_bit(ATH12K_PCI_FLAG_IS_MSI_64, &ab_pci->flags)) {
921 pci_read_config_dword(pci_dev, pci_dev->msi_cap + PCI_MSI_ADDRESS_HI,
928 void ath12k_pci_get_ce_msi_idx(struct ath12k_base *ab, u32 ce_id,
933 for (i = 0, msi_data_idx = 0; i < ab->hw_params->ce_count; i++) {
934 if (ath12k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)
942 *msi_idx = msi_data_idx;
945 void ath12k_pci_hif_ce_irq_enable(struct ath12k_base *ab)
947 ath12k_pci_ce_irqs_enable(ab);
950 void ath12k_pci_hif_ce_irq_disable(struct ath12k_base *ab)
952 ath12k_pci_ce_irq_disable_sync(ab);
955 void ath12k_pci_ext_irq_enable(struct ath12k_base *ab)
959 for (i = 0; i < ATH12K_EXT_IRQ_GRP_NUM_MAX; i++) {
960 struct ath12k_ext_irq_grp *irq_grp = &ab->ext_irq_grp[i];
962 napi_enable(&irq_grp->napi);
963 ath12k_pci_ext_grp_enable(irq_grp);
967 void ath12k_pci_ext_irq_disable(struct ath12k_base *ab)
969 __ath12k_pci_ext_irq_disable(ab);
970 ath12k_pci_sync_ext_irqs(ab);
973 int ath12k_pci_hif_suspend(struct ath12k_base *ab)
975 struct ath12k_pci *ar_pci = ath12k_pci_priv(ab);
977 ath12k_mhi_suspend(ar_pci);
982 int ath12k_pci_hif_resume(struct ath12k_base *ab)
984 struct ath12k_pci *ar_pci = ath12k_pci_priv(ab);
986 ath12k_mhi_resume(ar_pci);
991 void ath12k_pci_stop(struct ath12k_base *ab)
993 ath12k_pci_ce_irq_disable_sync(ab);
994 ath12k_ce_cleanup_pipes(ab);
997 int ath12k_pci_start(struct ath12k_base *ab)
999 struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
1001 set_bit(ATH12K_PCI_FLAG_INIT_DONE, &ab_pci->flags);
1003 ath12k_pci_aspm_restore(ab_pci);
1005 ath12k_pci_ce_irqs_enable(ab);
1006 ath12k_ce_rx_post_buf(ab);
1011 u32 ath12k_pci_read32(struct ath12k_base *ab, u32 offset)
1013 struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
1014 u32 val, window_start;
1017 /* for offset beyond BAR + 4K - 32, may
1018 * need to wakeup MHI to access.
1020 if (test_bit(ATH12K_PCI_FLAG_INIT_DONE, &ab_pci->flags) &&
1021 offset >= ACCESS_ALWAYS_OFF && ab_pci->pci_ops->wakeup)
1022 ret = ab_pci->pci_ops->wakeup(ab);
1024 if (offset < WINDOW_START) {
1025 val = ioread32(ab->mem + offset);
1027 if (ab->static_window_map)
1028 window_start = ath12k_pci_get_window_start(ab, offset);
1030 window_start = WINDOW_START;
1032 if (window_start == WINDOW_START) {
1033 spin_lock_bh(&ab_pci->window_lock);
1034 ath12k_pci_select_window(ab_pci, offset);
1035 val = ioread32(ab->mem + window_start +
1036 (offset & WINDOW_RANGE_MASK));
1037 spin_unlock_bh(&ab_pci->window_lock);
1039 if ((!window_start) &&
1040 (offset >= PCI_MHIREGLEN_REG &&
1041 offset <= PCI_MHI_REGION_END))
1042 offset = offset - PCI_MHIREGLEN_REG;
1044 val = ioread32(ab->mem + window_start +
1045 (offset & WINDOW_RANGE_MASK));
1049 if (test_bit(ATH12K_PCI_FLAG_INIT_DONE, &ab_pci->flags) &&
1050 offset >= ACCESS_ALWAYS_OFF && ab_pci->pci_ops->release &&
1052 ab_pci->pci_ops->release(ab);
1056 void ath12k_pci_write32(struct ath12k_base *ab, u32 offset, u32 value)
1058 struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
1062 /* for offset beyond BAR + 4K - 32, may
1063 * need to wakeup MHI to access.
1065 if (test_bit(ATH12K_PCI_FLAG_INIT_DONE, &ab_pci->flags) &&
1066 offset >= ACCESS_ALWAYS_OFF && ab_pci->pci_ops->wakeup)
1067 ret = ab_pci->pci_ops->wakeup(ab);
1069 if (offset < WINDOW_START) {
1070 iowrite32(value, ab->mem + offset);
1072 if (ab->static_window_map)
1073 window_start = ath12k_pci_get_window_start(ab, offset);
1075 window_start = WINDOW_START;
1077 if (window_start == WINDOW_START) {
1078 spin_lock_bh(&ab_pci->window_lock);
1079 ath12k_pci_select_window(ab_pci, offset);
1080 iowrite32(value, ab->mem + window_start +
1081 (offset & WINDOW_RANGE_MASK));
1082 spin_unlock_bh(&ab_pci->window_lock);
1084 if ((!window_start) &&
1085 (offset >= PCI_MHIREGLEN_REG &&
1086 offset <= PCI_MHI_REGION_END))
1087 offset = offset - PCI_MHIREGLEN_REG;
1089 iowrite32(value, ab->mem + window_start +
1090 (offset & WINDOW_RANGE_MASK));
1094 if (test_bit(ATH12K_PCI_FLAG_INIT_DONE, &ab_pci->flags) &&
1095 offset >= ACCESS_ALWAYS_OFF && ab_pci->pci_ops->release &&
1097 ab_pci->pci_ops->release(ab);
1100 int ath12k_pci_power_up(struct ath12k_base *ab)
1102 struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
1105 ab_pci->register_window = 0;
1106 clear_bit(ATH12K_PCI_FLAG_INIT_DONE, &ab_pci->flags);
1107 ath12k_pci_sw_reset(ab_pci->ab, true);
1109 /* Disable ASPM during firmware download due to problems switching
1112 ath12k_pci_aspm_disable(ab_pci);
1114 ath12k_pci_msi_enable(ab_pci);
1116 ret = ath12k_mhi_start(ab_pci);
1118 ath12k_err(ab, "failed to start mhi: %d\n", ret);
1122 if (ab->static_window_map)
1123 ath12k_pci_select_static_window(ab_pci);
1128 void ath12k_pci_power_down(struct ath12k_base *ab)
1130 struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
1132 /* restore aspm in case firmware bootup fails */
1133 ath12k_pci_aspm_restore(ab_pci);
1135 ath12k_pci_force_wake(ab_pci->ab);
1136 ath12k_pci_msi_disable(ab_pci);
1137 ath12k_mhi_stop(ab_pci);
1138 clear_bit(ATH12K_PCI_FLAG_INIT_DONE, &ab_pci->flags);
1139 ath12k_pci_sw_reset(ab_pci->ab, false);
1142 static const struct ath12k_hif_ops ath12k_pci_hif_ops = {
1143 .start = ath12k_pci_start,
1144 .stop = ath12k_pci_stop,
1145 .read32 = ath12k_pci_read32,
1146 .write32 = ath12k_pci_write32,
1147 .power_down = ath12k_pci_power_down,
1148 .power_up = ath12k_pci_power_up,
1149 .suspend = ath12k_pci_hif_suspend,
1150 .resume = ath12k_pci_hif_resume,
1151 .irq_enable = ath12k_pci_ext_irq_enable,
1152 .irq_disable = ath12k_pci_ext_irq_disable,
1153 .get_msi_address = ath12k_pci_get_msi_address,
1154 .get_user_msi_vector = ath12k_pci_get_user_msi_assignment,
1155 .map_service_to_pipe = ath12k_pci_map_service_to_pipe,
1156 .ce_irq_enable = ath12k_pci_hif_ce_irq_enable,
1157 .ce_irq_disable = ath12k_pci_hif_ce_irq_disable,
1158 .get_ce_msi_idx = ath12k_pci_get_ce_msi_idx,
1162 void ath12k_pci_read_hw_version(struct ath12k_base *ab, u32 *major, u32 *minor)
1166 soc_hw_version = ath12k_pci_read32(ab, TCSR_SOC_HW_VERSION);
1167 *major = FIELD_GET(TCSR_SOC_HW_VERSION_MAJOR_MASK,
1169 *minor = FIELD_GET(TCSR_SOC_HW_VERSION_MINOR_MASK,
1172 ath12k_dbg(ab, ATH12K_DBG_PCI,
1173 "pci tcsr_soc_hw_version major %d minor %d\n",
1177 static int ath12k_pci_probe(struct pci_dev *pdev,
1178 const struct pci_device_id *pci_dev)
1180 struct ath12k_base *ab;
1181 struct ath12k_pci *ab_pci;
1182 u32 soc_hw_version_major, soc_hw_version_minor;
1185 ab = ath12k_core_alloc(&pdev->dev, sizeof(*ab_pci), ATH12K_BUS_PCI);
1187 dev_err(&pdev->dev, "failed to allocate ath12k base\n");
1191 ab->dev = &pdev->dev;
1192 pci_set_drvdata(pdev, ab);
1193 ab_pci = ath12k_pci_priv(ab);
1194 ab_pci->dev_id = pci_dev->device;
1196 ab_pci->pdev = pdev;
1197 ab->hif.ops = &ath12k_pci_hif_ops;
1198 pci_set_drvdata(pdev, ab);
1199 spin_lock_init(&ab_pci->window_lock);
1201 ret = ath12k_pci_claim(ab_pci, pdev);
1203 ath12k_err(ab, "failed to claim device: %d\n", ret);
1207 switch (pci_dev->device) {
1208 case QCN9274_DEVICE_ID:
1209 ab_pci->msi_config = &ath12k_msi_config[0];
1210 ab->static_window_map = true;
1211 ab_pci->pci_ops = &ath12k_pci_ops_qcn9274;
1212 ath12k_pci_read_hw_version(ab, &soc_hw_version_major,
1213 &soc_hw_version_minor);
1214 switch (soc_hw_version_major) {
1215 case ATH12K_PCI_SOC_HW_VERSION_2:
1216 ab->hw_rev = ATH12K_HW_QCN9274_HW20;
1218 case ATH12K_PCI_SOC_HW_VERSION_1:
1219 ab->hw_rev = ATH12K_HW_QCN9274_HW10;
1223 "Unknown hardware version found for QCN9274: 0x%x\n",
1224 soc_hw_version_major);
1226 goto err_pci_free_region;
1229 case WCN7850_DEVICE_ID:
1230 ab_pci->msi_config = &ath12k_msi_config[0];
1231 ab->static_window_map = false;
1232 ab_pci->pci_ops = &ath12k_pci_ops_wcn7850;
1233 ath12k_pci_read_hw_version(ab, &soc_hw_version_major,
1234 &soc_hw_version_minor);
1235 switch (soc_hw_version_major) {
1236 case ATH12K_PCI_SOC_HW_VERSION_2:
1237 ab->hw_rev = ATH12K_HW_WCN7850_HW20;
1241 "Unknown hardware version found for WCN7850: 0x%x\n",
1242 soc_hw_version_major);
1244 goto err_pci_free_region;
1249 dev_err(&pdev->dev, "Unknown PCI device found: 0x%x\n",
1252 goto err_pci_free_region;
1255 ret = ath12k_pci_msi_alloc(ab_pci);
1257 ath12k_err(ab, "failed to alloc msi: %d\n", ret);
1258 goto err_pci_free_region;
1261 ret = ath12k_core_pre_init(ab);
1263 goto err_pci_msi_free;
1265 ret = ath12k_mhi_register(ab_pci);
1267 ath12k_err(ab, "failed to register mhi: %d\n", ret);
1268 goto err_pci_msi_free;
1271 ret = ath12k_hal_srng_init(ab);
1273 goto err_mhi_unregister;
1275 ret = ath12k_ce_alloc_pipes(ab);
1277 ath12k_err(ab, "failed to allocate ce pipes: %d\n", ret);
1278 goto err_hal_srng_deinit;
1281 ath12k_pci_init_qmi_ce_config(ab);
1283 ret = ath12k_pci_config_irq(ab);
1285 ath12k_err(ab, "failed to config irq: %d\n", ret);
1289 ret = ath12k_core_init(ab);
1291 ath12k_err(ab, "failed to init core: %d\n", ret);
1297 ath12k_pci_free_irq(ab);
1300 ath12k_ce_free_pipes(ab);
1302 err_hal_srng_deinit:
1303 ath12k_hal_srng_deinit(ab);
1306 ath12k_mhi_unregister(ab_pci);
1309 ath12k_pci_msi_free(ab_pci);
1311 err_pci_free_region:
1312 ath12k_pci_free_region(ab_pci);
1315 ath12k_core_free(ab);
1320 static void ath12k_pci_remove(struct pci_dev *pdev)
1322 struct ath12k_base *ab = pci_get_drvdata(pdev);
1323 struct ath12k_pci *ab_pci = ath12k_pci_priv(ab);
1325 if (test_bit(ATH12K_FLAG_QMI_FAIL, &ab->dev_flags)) {
1326 ath12k_pci_power_down(ab);
1327 ath12k_qmi_deinit_service(ab);
1331 set_bit(ATH12K_FLAG_UNREGISTERING, &ab->dev_flags);
1333 cancel_work_sync(&ab->reset_work);
1334 ath12k_core_deinit(ab);
1337 ath12k_mhi_unregister(ab_pci);
1339 ath12k_pci_free_irq(ab);
1340 ath12k_pci_msi_free(ab_pci);
1341 ath12k_pci_free_region(ab_pci);
1343 ath12k_hal_srng_deinit(ab);
1344 ath12k_ce_free_pipes(ab);
1345 ath12k_core_free(ab);
1348 static void ath12k_pci_shutdown(struct pci_dev *pdev)
1350 struct ath12k_base *ab = pci_get_drvdata(pdev);
1352 ath12k_pci_power_down(ab);
1355 static __maybe_unused int ath12k_pci_pm_suspend(struct device *dev)
1357 struct ath12k_base *ab = dev_get_drvdata(dev);
1360 ret = ath12k_core_suspend(ab);
1362 ath12k_warn(ab, "failed to suspend core: %d\n", ret);
1367 static __maybe_unused int ath12k_pci_pm_resume(struct device *dev)
1369 struct ath12k_base *ab = dev_get_drvdata(dev);
1372 ret = ath12k_core_resume(ab);
1374 ath12k_warn(ab, "failed to resume core: %d\n", ret);
1379 static SIMPLE_DEV_PM_OPS(ath12k_pci_pm_ops,
1380 ath12k_pci_pm_suspend,
1381 ath12k_pci_pm_resume);
1383 static struct pci_driver ath12k_pci_driver = {
1384 .name = "ath12k_pci",
1385 .id_table = ath12k_pci_id_table,
1386 .probe = ath12k_pci_probe,
1387 .remove = ath12k_pci_remove,
1388 .shutdown = ath12k_pci_shutdown,
1389 .driver.pm = &ath12k_pci_pm_ops,
1392 static int ath12k_pci_init(void)
1396 ret = pci_register_driver(&ath12k_pci_driver);
1398 pr_err("failed to register ath12k pci driver: %d\n",
1405 module_init(ath12k_pci_init);
1407 static void ath12k_pci_exit(void)
1409 pci_unregister_driver(&ath12k_pci_driver);
1412 module_exit(ath12k_pci_exit);
1414 MODULE_DESCRIPTION("Driver support for Qualcomm Technologies PCIe 802.11be WLAN devices");
1415 MODULE_LICENSE("Dual BSD/GPL");