Path: blob/main/sys/contrib/dev/athk/ath11k/ahb.c
107846 views
// SPDX-License-Identifier: BSD-3-Clause-Clear1/*2* Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.3* Copyright (c) 2022-2025 Qualcomm Innovation Center, Inc. All rights reserved.4*/56#include <linux/module.h>7#include <linux/platform_device.h>8#include <linux/property.h>9#include <linux/of_device.h>10#include <linux/of.h>11#include <linux/of_reserved_mem.h>12#include <linux/dma-mapping.h>13#include <linux/iommu.h>14#include "ahb.h"15#include "debug.h"16#include "hif.h"17#include "qmi.h"18#include <linux/remoteproc.h>19#include "pcic.h"20#include <linux/soc/qcom/smem.h>21#include <linux/soc/qcom/smem_state.h>2223static const struct of_device_id ath11k_ahb_of_match[] = {24/* TODO: Should we change the compatible string to something similar25* to one that ath10k uses?26*/27{ .compatible = "qcom,ipq8074-wifi",28.data = (void *)ATH11K_HW_IPQ8074,29},30{ .compatible = "qcom,ipq6018-wifi",31.data = (void *)ATH11K_HW_IPQ6018_HW10,32},33{ .compatible = "qcom,wcn6750-wifi",34.data = (void *)ATH11K_HW_WCN6750_HW10,35},36{ .compatible = "qcom,ipq5018-wifi",37.data = (void *)ATH11K_HW_IPQ5018_HW10,38},39{ }40};4142MODULE_DEVICE_TABLE(of, ath11k_ahb_of_match);4344#define ATH11K_IRQ_CE0_OFFSET 44546static const char *irq_name[ATH11K_IRQ_NUM_MAX] = {47"misc-pulse1",48"misc-latch",49"sw-exception",50"watchdog",51"ce0",52"ce1",53"ce2",54"ce3",55"ce4",56"ce5",57"ce6",58"ce7",59"ce8",60"ce9",61"ce10",62"ce11",63"host2wbm-desc-feed",64"host2reo-re-injection",65"host2reo-command",66"host2rxdma-monitor-ring3",67"host2rxdma-monitor-ring2",68"host2rxdma-monitor-ring1",69"reo2ost-exception",70"wbm2host-rx-release",71"reo2host-status",72"reo2host-destination-ring4",73"reo2host-destination-ring3",74"reo2host-destination-ring2",75"reo2host-destination-ring1",76"rxdma2host-monitor-destination-mac3",77"rxdma2host-monitor-destination-mac2",78"rxdma2host-monitor-destination-mac1",79"ppdu-end-interrupts-mac3",80"ppdu-end-interrupts-mac2",81"ppdu-end-interrupts-mac1",82"rxdma2host-monitor-status-ring-mac3",83"rxdma2host-monitor-status-ring-mac2",84"rxdma2host-monitor-status-ring-mac1",85"host2rxdma-host-buf-ring-mac3",86"host2rxdma-host-buf-ring-mac2",87"host2rxdma-host-buf-ring-mac1",88"rxdma2host-destination-ring-mac3",89"rxdma2host-destination-ring-mac2",90"rxdma2host-destination-ring-mac1",91"host2tcl-input-ring4",92"host2tcl-input-ring3",93"host2tcl-input-ring2",94"host2tcl-input-ring1",95"wbm2host-tx-completions-ring3",96"wbm2host-tx-completions-ring2",97"wbm2host-tx-completions-ring1",98"tcl2host-status-ring",99};100101/* enum ext_irq_num - irq numbers that can be used by external modules102* like datapath103*/104enum ext_irq_num {105host2wbm_desc_feed = 16,106host2reo_re_injection,107host2reo_command,108host2rxdma_monitor_ring3,109host2rxdma_monitor_ring2,110host2rxdma_monitor_ring1,111reo2host_exception,112wbm2host_rx_release,113reo2host_status,114reo2host_destination_ring4,115reo2host_destination_ring3,116reo2host_destination_ring2,117reo2host_destination_ring1,118rxdma2host_monitor_destination_mac3,119rxdma2host_monitor_destination_mac2,120rxdma2host_monitor_destination_mac1,121ppdu_end_interrupts_mac3,122ppdu_end_interrupts_mac2,123ppdu_end_interrupts_mac1,124rxdma2host_monitor_status_ring_mac3,125rxdma2host_monitor_status_ring_mac2,126rxdma2host_monitor_status_ring_mac1,127host2rxdma_host_buf_ring_mac3,128host2rxdma_host_buf_ring_mac2,129host2rxdma_host_buf_ring_mac1,130rxdma2host_destination_ring_mac3,131rxdma2host_destination_ring_mac2,132rxdma2host_destination_ring_mac1,133host2tcl_input_ring4,134host2tcl_input_ring3,135host2tcl_input_ring2,136host2tcl_input_ring1,137wbm2host_tx_completions_ring3,138wbm2host_tx_completions_ring2,139wbm2host_tx_completions_ring1,140tcl2host_status_ring,141};142143static int144ath11k_ahb_get_msi_irq_wcn6750(struct ath11k_base *ab, unsigned int vector)145{146return ab->pci.msi.irqs[vector];147}148149static inline u32150ath11k_ahb_get_window_start_wcn6750(struct ath11k_base *ab, u32 offset)151{152u32 window_start = 0;153154/* If offset lies within DP register range, use 1st window */155if ((offset ^ HAL_SEQ_WCSS_UMAC_OFFSET) < ATH11K_PCI_WINDOW_RANGE_MASK)156window_start = ATH11K_PCI_WINDOW_START;157/* If offset lies within CE register range, use 2nd window */158else if ((offset ^ HAL_SEQ_WCSS_UMAC_CE0_SRC_REG(ab)) <159ATH11K_PCI_WINDOW_RANGE_MASK)160window_start = 2 * ATH11K_PCI_WINDOW_START;161162return window_start;163}164165static void166ath11k_ahb_window_write32_wcn6750(struct ath11k_base *ab, u32 offset, u32 value)167{168u32 window_start;169170/* WCN6750 uses static window based register access*/171window_start = ath11k_ahb_get_window_start_wcn6750(ab, offset);172173iowrite32(value, ab->mem + window_start +174(offset & ATH11K_PCI_WINDOW_RANGE_MASK));175}176177static u32 ath11k_ahb_window_read32_wcn6750(struct ath11k_base *ab, u32 offset)178{179u32 window_start;180u32 val;181182/* WCN6750 uses static window based register access */183window_start = ath11k_ahb_get_window_start_wcn6750(ab, offset);184185val = ioread32(ab->mem + window_start +186(offset & ATH11K_PCI_WINDOW_RANGE_MASK));187return val;188}189190static const struct ath11k_pci_ops ath11k_ahb_pci_ops_wcn6750 = {191.wakeup = NULL,192.release = NULL,193.get_msi_irq = ath11k_ahb_get_msi_irq_wcn6750,194.window_write32 = ath11k_ahb_window_write32_wcn6750,195.window_read32 = ath11k_ahb_window_read32_wcn6750,196};197198static inline u32 ath11k_ahb_read32(struct ath11k_base *ab, u32 offset)199{200return ioread32(ab->mem + offset);201}202203static inline void ath11k_ahb_write32(struct ath11k_base *ab, u32 offset, u32 value)204{205iowrite32(value, ab->mem + offset);206}207208static void ath11k_ahb_kill_tasklets(struct ath11k_base *ab)209{210int i;211212for (i = 0; i < ab->hw_params.ce_count; i++) {213struct ath11k_ce_pipe *ce_pipe = &ab->ce.ce_pipe[i];214215if (ath11k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)216continue;217218tasklet_kill(&ce_pipe->intr_tq);219}220}221222static void ath11k_ahb_ext_grp_disable(struct ath11k_ext_irq_grp *irq_grp)223{224int i;225226for (i = 0; i < irq_grp->num_irq; i++)227disable_irq_nosync(irq_grp->ab->irq_num[irq_grp->irqs[i]]);228}229230static void __ath11k_ahb_ext_irq_disable(struct ath11k_base *ab)231{232int i;233234for (i = 0; i < ATH11K_EXT_IRQ_GRP_NUM_MAX; i++) {235struct ath11k_ext_irq_grp *irq_grp = &ab->ext_irq_grp[i];236237ath11k_ahb_ext_grp_disable(irq_grp);238239if (irq_grp->napi_enabled) {240napi_synchronize(&irq_grp->napi);241napi_disable(&irq_grp->napi);242irq_grp->napi_enabled = false;243}244}245}246247static void ath11k_ahb_ext_grp_enable(struct ath11k_ext_irq_grp *irq_grp)248{249int i;250251for (i = 0; i < irq_grp->num_irq; i++)252enable_irq(irq_grp->ab->irq_num[irq_grp->irqs[i]]);253}254255static void ath11k_ahb_setbit32(struct ath11k_base *ab, u8 bit, u32 offset)256{257u32 val;258259val = ath11k_ahb_read32(ab, offset);260ath11k_ahb_write32(ab, offset, val | BIT(bit));261}262263static void ath11k_ahb_clearbit32(struct ath11k_base *ab, u8 bit, u32 offset)264{265u32 val;266267val = ath11k_ahb_read32(ab, offset);268ath11k_ahb_write32(ab, offset, val & ~BIT(bit));269}270271static void ath11k_ahb_ce_irq_enable(struct ath11k_base *ab, u16 ce_id)272{273const struct ce_attr *ce_attr;274const struct ce_ie_addr *ce_ie_addr = ab->hw_params.ce_ie_addr;275u32 ie1_reg_addr, ie2_reg_addr, ie3_reg_addr;276277ie1_reg_addr = ce_ie_addr->ie1_reg_addr + ATH11K_CE_OFFSET(ab);278ie2_reg_addr = ce_ie_addr->ie2_reg_addr + ATH11K_CE_OFFSET(ab);279ie3_reg_addr = ce_ie_addr->ie3_reg_addr + ATH11K_CE_OFFSET(ab);280281ce_attr = &ab->hw_params.host_ce_config[ce_id];282if (ce_attr->src_nentries)283ath11k_ahb_setbit32(ab, ce_id, ie1_reg_addr);284285if (ce_attr->dest_nentries) {286ath11k_ahb_setbit32(ab, ce_id, ie2_reg_addr);287ath11k_ahb_setbit32(ab, ce_id + CE_HOST_IE_3_SHIFT,288ie3_reg_addr);289}290}291292static void ath11k_ahb_ce_irq_disable(struct ath11k_base *ab, u16 ce_id)293{294const struct ce_attr *ce_attr;295const struct ce_ie_addr *ce_ie_addr = ab->hw_params.ce_ie_addr;296u32 ie1_reg_addr, ie2_reg_addr, ie3_reg_addr;297298ie1_reg_addr = ce_ie_addr->ie1_reg_addr + ATH11K_CE_OFFSET(ab);299ie2_reg_addr = ce_ie_addr->ie2_reg_addr + ATH11K_CE_OFFSET(ab);300ie3_reg_addr = ce_ie_addr->ie3_reg_addr + ATH11K_CE_OFFSET(ab);301302ce_attr = &ab->hw_params.host_ce_config[ce_id];303if (ce_attr->src_nentries)304ath11k_ahb_clearbit32(ab, ce_id, ie1_reg_addr);305306if (ce_attr->dest_nentries) {307ath11k_ahb_clearbit32(ab, ce_id, ie2_reg_addr);308ath11k_ahb_clearbit32(ab, ce_id + CE_HOST_IE_3_SHIFT,309ie3_reg_addr);310}311}312313static void ath11k_ahb_sync_ce_irqs(struct ath11k_base *ab)314{315int i;316int irq_idx;317318for (i = 0; i < ab->hw_params.ce_count; i++) {319if (ath11k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)320continue;321322irq_idx = ATH11K_IRQ_CE0_OFFSET + i;323synchronize_irq(ab->irq_num[irq_idx]);324}325}326327static void ath11k_ahb_sync_ext_irqs(struct ath11k_base *ab)328{329int i, j;330int irq_idx;331332for (i = 0; i < ATH11K_EXT_IRQ_GRP_NUM_MAX; i++) {333struct ath11k_ext_irq_grp *irq_grp = &ab->ext_irq_grp[i];334335for (j = 0; j < irq_grp->num_irq; j++) {336irq_idx = irq_grp->irqs[j];337synchronize_irq(ab->irq_num[irq_idx]);338}339}340}341342static void ath11k_ahb_ce_irqs_enable(struct ath11k_base *ab)343{344int i;345346for (i = 0; i < ab->hw_params.ce_count; i++) {347if (ath11k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)348continue;349ath11k_ahb_ce_irq_enable(ab, i);350}351}352353static void ath11k_ahb_ce_irqs_disable(struct ath11k_base *ab)354{355int i;356357for (i = 0; i < ab->hw_params.ce_count; i++) {358if (ath11k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)359continue;360ath11k_ahb_ce_irq_disable(ab, i);361}362}363364static int ath11k_ahb_start(struct ath11k_base *ab)365{366ath11k_ahb_ce_irqs_enable(ab);367ath11k_ce_rx_post_buf(ab);368369return 0;370}371372static void ath11k_ahb_ext_irq_enable(struct ath11k_base *ab)373{374int i;375376for (i = 0; i < ATH11K_EXT_IRQ_GRP_NUM_MAX; i++) {377struct ath11k_ext_irq_grp *irq_grp = &ab->ext_irq_grp[i];378379if (!irq_grp->napi_enabled) {380napi_enable(&irq_grp->napi);381irq_grp->napi_enabled = true;382}383ath11k_ahb_ext_grp_enable(irq_grp);384}385}386387static void ath11k_ahb_ext_irq_disable(struct ath11k_base *ab)388{389__ath11k_ahb_ext_irq_disable(ab);390ath11k_ahb_sync_ext_irqs(ab);391}392393static void ath11k_ahb_stop(struct ath11k_base *ab)394{395if (!test_bit(ATH11K_FLAG_CRASH_FLUSH, &ab->dev_flags))396ath11k_ahb_ce_irqs_disable(ab);397ath11k_ahb_sync_ce_irqs(ab);398ath11k_ahb_kill_tasklets(ab);399timer_delete_sync(&ab->rx_replenish_retry);400ath11k_ce_cleanup_pipes(ab);401}402403static int ath11k_ahb_power_up(struct ath11k_base *ab)404{405struct ath11k_ahb *ab_ahb = ath11k_ahb_priv(ab);406int ret;407408ret = rproc_boot(ab_ahb->tgt_rproc);409if (ret)410ath11k_err(ab, "failed to boot the remote processor Q6\n");411412return ret;413}414415static void ath11k_ahb_power_down(struct ath11k_base *ab, bool is_suspend)416{417struct ath11k_ahb *ab_ahb = ath11k_ahb_priv(ab);418419rproc_shutdown(ab_ahb->tgt_rproc);420}421422static void ath11k_ahb_init_qmi_ce_config(struct ath11k_base *ab)423{424struct ath11k_qmi_ce_cfg *cfg = &ab->qmi.ce_cfg;425426cfg->tgt_ce_len = ab->hw_params.target_ce_count;427cfg->tgt_ce = ab->hw_params.target_ce_config;428cfg->svc_to_ce_map_len = ab->hw_params.svc_to_ce_map_len;429cfg->svc_to_ce_map = ab->hw_params.svc_to_ce_map;430ab->qmi.service_ins_id = ab->hw_params.qmi_service_ins_id;431}432433static void ath11k_ahb_free_ext_irq(struct ath11k_base *ab)434{435int i, j;436437for (i = 0; i < ATH11K_EXT_IRQ_GRP_NUM_MAX; i++) {438struct ath11k_ext_irq_grp *irq_grp = &ab->ext_irq_grp[i];439440for (j = 0; j < irq_grp->num_irq; j++)441free_irq(ab->irq_num[irq_grp->irqs[j]], irq_grp);442443netif_napi_del(&irq_grp->napi);444free_netdev(irq_grp->napi_ndev);445}446}447448static void ath11k_ahb_free_irq(struct ath11k_base *ab)449{450int irq_idx;451int i;452453if (ab->hw_params.hybrid_bus_type)454return ath11k_pcic_free_irq(ab);455456for (i = 0; i < ab->hw_params.ce_count; i++) {457if (ath11k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)458continue;459irq_idx = ATH11K_IRQ_CE0_OFFSET + i;460free_irq(ab->irq_num[irq_idx], &ab->ce.ce_pipe[i]);461}462463ath11k_ahb_free_ext_irq(ab);464}465466static void ath11k_ahb_ce_tasklet(struct tasklet_struct *t)467{468struct ath11k_ce_pipe *ce_pipe = from_tasklet(ce_pipe, t, intr_tq);469470ath11k_ce_per_engine_service(ce_pipe->ab, ce_pipe->pipe_num);471472ath11k_ahb_ce_irq_enable(ce_pipe->ab, ce_pipe->pipe_num);473}474475static irqreturn_t ath11k_ahb_ce_interrupt_handler(int irq, void *arg)476{477struct ath11k_ce_pipe *ce_pipe = arg;478479/* last interrupt received for this CE */480ce_pipe->timestamp = jiffies;481482ath11k_ahb_ce_irq_disable(ce_pipe->ab, ce_pipe->pipe_num);483484tasklet_schedule(&ce_pipe->intr_tq);485486return IRQ_HANDLED;487}488489static int ath11k_ahb_ext_grp_napi_poll(struct napi_struct *napi, int budget)490{491struct ath11k_ext_irq_grp *irq_grp = container_of(napi,492struct ath11k_ext_irq_grp,493napi);494struct ath11k_base *ab = irq_grp->ab;495int work_done;496497work_done = ath11k_dp_service_srng(ab, irq_grp, budget);498if (work_done < budget) {499napi_complete_done(napi, work_done);500ath11k_ahb_ext_grp_enable(irq_grp);501}502503if (work_done > budget)504work_done = budget;505506return work_done;507}508509static irqreturn_t ath11k_ahb_ext_interrupt_handler(int irq, void *arg)510{511struct ath11k_ext_irq_grp *irq_grp = arg;512513/* last interrupt received for this group */514irq_grp->timestamp = jiffies;515516ath11k_ahb_ext_grp_disable(irq_grp);517518napi_schedule(&irq_grp->napi);519520return IRQ_HANDLED;521}522523static int ath11k_ahb_config_ext_irq(struct ath11k_base *ab)524{525struct ath11k_hw_params *hw = &ab->hw_params;526int i, j;527int irq;528int ret;529530for (i = 0; i < ATH11K_EXT_IRQ_GRP_NUM_MAX; i++) {531struct ath11k_ext_irq_grp *irq_grp = &ab->ext_irq_grp[i];532u32 num_irq = 0;533534irq_grp->ab = ab;535irq_grp->grp_id = i;536537irq_grp->napi_ndev = alloc_netdev_dummy(0);538if (!irq_grp->napi_ndev)539return -ENOMEM;540541netif_napi_add(irq_grp->napi_ndev, &irq_grp->napi,542ath11k_ahb_ext_grp_napi_poll);543544for (j = 0; j < ATH11K_EXT_IRQ_NUM_MAX; j++) {545if (ab->hw_params.ring_mask->tx[i] & BIT(j)) {546irq_grp->irqs[num_irq++] =547wbm2host_tx_completions_ring1 - j;548}549550if (ab->hw_params.ring_mask->rx[i] & BIT(j)) {551irq_grp->irqs[num_irq++] =552reo2host_destination_ring1 - j;553}554555if (ab->hw_params.ring_mask->rx_err[i] & BIT(j))556irq_grp->irqs[num_irq++] = reo2host_exception;557558if (ab->hw_params.ring_mask->rx_wbm_rel[i] & BIT(j))559irq_grp->irqs[num_irq++] = wbm2host_rx_release;560561if (ab->hw_params.ring_mask->reo_status[i] & BIT(j))562irq_grp->irqs[num_irq++] = reo2host_status;563564if (j < ab->hw_params.max_radios) {565if (ab->hw_params.ring_mask->rxdma2host[i] & BIT(j)) {566irq_grp->irqs[num_irq++] =567rxdma2host_destination_ring_mac1 -568ath11k_hw_get_mac_from_pdev_id(hw, j);569}570571if (ab->hw_params.ring_mask->host2rxdma[i] & BIT(j)) {572irq_grp->irqs[num_irq++] =573host2rxdma_host_buf_ring_mac1 -574ath11k_hw_get_mac_from_pdev_id(hw, j);575}576577if (ab->hw_params.ring_mask->rx_mon_status[i] & BIT(j)) {578irq_grp->irqs[num_irq++] =579ppdu_end_interrupts_mac1 -580ath11k_hw_get_mac_from_pdev_id(hw, j);581irq_grp->irqs[num_irq++] =582rxdma2host_monitor_status_ring_mac1 -583ath11k_hw_get_mac_from_pdev_id(hw, j);584}585}586}587irq_grp->num_irq = num_irq;588589for (j = 0; j < irq_grp->num_irq; j++) {590int irq_idx = irq_grp->irqs[j];591592irq = platform_get_irq_byname(ab->pdev,593irq_name[irq_idx]);594ab->irq_num[irq_idx] = irq;595irq_set_status_flags(irq, IRQ_NOAUTOEN | IRQ_DISABLE_UNLAZY);596ret = request_irq(irq, ath11k_ahb_ext_interrupt_handler,597IRQF_TRIGGER_RISING,598irq_name[irq_idx], irq_grp);599if (ret) {600ath11k_err(ab, "failed request_irq for %d\n",601irq);602}603}604}605606return 0;607}608609static int ath11k_ahb_config_irq(struct ath11k_base *ab)610{611int irq, irq_idx, i;612int ret;613614if (ab->hw_params.hybrid_bus_type)615return ath11k_pcic_config_irq(ab);616617/* Configure CE irqs */618for (i = 0; i < ab->hw_params.ce_count; i++) {619struct ath11k_ce_pipe *ce_pipe = &ab->ce.ce_pipe[i];620621if (ath11k_ce_get_attr_flags(ab, i) & CE_ATTR_DIS_INTR)622continue;623624irq_idx = ATH11K_IRQ_CE0_OFFSET + i;625626tasklet_setup(&ce_pipe->intr_tq, ath11k_ahb_ce_tasklet);627irq = platform_get_irq_byname(ab->pdev, irq_name[irq_idx]);628ret = request_irq(irq, ath11k_ahb_ce_interrupt_handler,629IRQF_TRIGGER_RISING, irq_name[irq_idx],630ce_pipe);631if (ret)632return ret;633634ab->irq_num[irq_idx] = irq;635}636637/* Configure external interrupts */638ret = ath11k_ahb_config_ext_irq(ab);639640return ret;641}642643static int ath11k_ahb_map_service_to_pipe(struct ath11k_base *ab, u16 service_id,644u8 *ul_pipe, u8 *dl_pipe)645{646const struct service_to_pipe *entry;647bool ul_set = false, dl_set = false;648int i;649650for (i = 0; i < ab->hw_params.svc_to_ce_map_len; i++) {651entry = &ab->hw_params.svc_to_ce_map[i];652653if (__le32_to_cpu(entry->service_id) != service_id)654continue;655656switch (__le32_to_cpu(entry->pipedir)) {657case PIPEDIR_NONE:658break;659case PIPEDIR_IN:660WARN_ON(dl_set);661*dl_pipe = __le32_to_cpu(entry->pipenum);662dl_set = true;663break;664case PIPEDIR_OUT:665WARN_ON(ul_set);666*ul_pipe = __le32_to_cpu(entry->pipenum);667ul_set = true;668break;669case PIPEDIR_INOUT:670WARN_ON(dl_set);671WARN_ON(ul_set);672*dl_pipe = __le32_to_cpu(entry->pipenum);673*ul_pipe = __le32_to_cpu(entry->pipenum);674dl_set = true;675ul_set = true;676break;677}678}679680if (WARN_ON(!ul_set || !dl_set))681return -ENOENT;682683return 0;684}685686static int ath11k_ahb_hif_suspend(struct ath11k_base *ab)687{688struct ath11k_ahb *ab_ahb = ath11k_ahb_priv(ab);689u32 wake_irq;690u32 value = 0;691int ret;692693if (!device_may_wakeup(ab->dev))694return -EPERM;695696wake_irq = ab->irq_num[ATH11K_PCI_IRQ_CE0_OFFSET + ATH11K_PCI_CE_WAKE_IRQ];697698ret = enable_irq_wake(wake_irq);699if (ret) {700ath11k_err(ab, "failed to enable wakeup irq :%d\n", ret);701return ret;702}703704value = u32_encode_bits(ab_ahb->smp2p_info.seq_no++,705ATH11K_AHB_SMP2P_SMEM_SEQ_NO);706value |= u32_encode_bits(ATH11K_AHB_POWER_SAVE_ENTER,707ATH11K_AHB_SMP2P_SMEM_MSG);708709ret = qcom_smem_state_update_bits(ab_ahb->smp2p_info.smem_state,710ATH11K_AHB_SMP2P_SMEM_VALUE_MASK, value);711if (ret) {712ath11k_err(ab, "failed to send smp2p power save enter cmd :%d\n", ret);713return ret;714}715716ath11k_dbg(ab, ATH11K_DBG_AHB, "device suspended\n");717718return ret;719}720721static int ath11k_ahb_hif_resume(struct ath11k_base *ab)722{723struct ath11k_ahb *ab_ahb = ath11k_ahb_priv(ab);724u32 wake_irq;725u32 value = 0;726int ret;727728if (!device_may_wakeup(ab->dev))729return -EPERM;730731wake_irq = ab->irq_num[ATH11K_PCI_IRQ_CE0_OFFSET + ATH11K_PCI_CE_WAKE_IRQ];732733ret = disable_irq_wake(wake_irq);734if (ret) {735ath11k_err(ab, "failed to disable wakeup irq: %d\n", ret);736return ret;737}738739reinit_completion(&ab->wow.wakeup_completed);740741value = u32_encode_bits(ab_ahb->smp2p_info.seq_no++,742ATH11K_AHB_SMP2P_SMEM_SEQ_NO);743value |= u32_encode_bits(ATH11K_AHB_POWER_SAVE_EXIT,744ATH11K_AHB_SMP2P_SMEM_MSG);745746ret = qcom_smem_state_update_bits(ab_ahb->smp2p_info.smem_state,747ATH11K_AHB_SMP2P_SMEM_VALUE_MASK, value);748if (ret) {749ath11k_err(ab, "failed to send smp2p power save enter cmd :%d\n", ret);750return ret;751}752753ret = wait_for_completion_timeout(&ab->wow.wakeup_completed, 3 * HZ);754if (ret == 0) {755ath11k_warn(ab, "timed out while waiting for wow wakeup completion\n");756return -ETIMEDOUT;757}758759ath11k_dbg(ab, ATH11K_DBG_AHB, "device resumed\n");760761return 0;762}763764static const struct ath11k_hif_ops ath11k_ahb_hif_ops_ipq8074 = {765.start = ath11k_ahb_start,766.stop = ath11k_ahb_stop,767.read32 = ath11k_ahb_read32,768.write32 = ath11k_ahb_write32,769.read = NULL,770.irq_enable = ath11k_ahb_ext_irq_enable,771.irq_disable = ath11k_ahb_ext_irq_disable,772.map_service_to_pipe = ath11k_ahb_map_service_to_pipe,773.power_down = ath11k_ahb_power_down,774.power_up = ath11k_ahb_power_up,775};776777static const struct ath11k_hif_ops ath11k_ahb_hif_ops_wcn6750 = {778.start = ath11k_pcic_start,779.stop = ath11k_pcic_stop,780.read32 = ath11k_pcic_read32,781.write32 = ath11k_pcic_write32,782.read = NULL,783.irq_enable = ath11k_pcic_ext_irq_enable,784.irq_disable = ath11k_pcic_ext_irq_disable,785.get_msi_address = ath11k_pcic_get_msi_address,786.get_user_msi_vector = ath11k_pcic_get_user_msi_assignment,787.map_service_to_pipe = ath11k_pcic_map_service_to_pipe,788.power_down = ath11k_ahb_power_down,789.power_up = ath11k_ahb_power_up,790.suspend = ath11k_ahb_hif_suspend,791.resume = ath11k_ahb_hif_resume,792.ce_irq_enable = ath11k_pci_enable_ce_irqs_except_wake_irq,793.ce_irq_disable = ath11k_pci_disable_ce_irqs_except_wake_irq,794};795796static int ath11k_core_get_rproc(struct ath11k_base *ab)797{798struct ath11k_ahb *ab_ahb = ath11k_ahb_priv(ab);799struct device *dev = ab->dev;800struct rproc *prproc;801phandle rproc_phandle;802803if (of_property_read_u32(dev->of_node, "qcom,rproc", &rproc_phandle)) {804ath11k_err(ab, "failed to get q6_rproc handle\n");805return -ENOENT;806}807808prproc = rproc_get_by_phandle(rproc_phandle);809if (!prproc) {810ath11k_dbg(ab, ATH11K_DBG_AHB, "failed to get rproc, deferring\n");811return -EPROBE_DEFER;812}813ab_ahb->tgt_rproc = prproc;814815return 0;816}817818static int ath11k_ahb_setup_msi_resources(struct ath11k_base *ab)819{820struct platform_device *pdev = ab->pdev;821phys_addr_t msi_addr_pa;822dma_addr_t msi_addr_iova;823struct resource *res;824int int_prop;825int ret;826int i;827828ret = ath11k_pcic_init_msi_config(ab);829if (ret) {830ath11k_err(ab, "failed to init msi config: %d\n", ret);831return ret;832}833834res = platform_get_resource(pdev, IORESOURCE_MEM, 0);835if (!res) {836ath11k_err(ab, "failed to fetch msi_addr\n");837return -ENOENT;838}839840msi_addr_pa = res->start;841msi_addr_iova = dma_map_resource(ab->dev, msi_addr_pa, PAGE_SIZE,842DMA_FROM_DEVICE, 0);843if (dma_mapping_error(ab->dev, msi_addr_iova))844return -ENOMEM;845846ab->pci.msi.addr_lo = lower_32_bits(msi_addr_iova);847ab->pci.msi.addr_hi = upper_32_bits(msi_addr_iova);848849ret = of_property_read_u32_index(ab->dev->of_node, "interrupts", 1, &int_prop);850if (ret)851return ret;852853ab->pci.msi.ep_base_data = int_prop + 32;854855for (i = 0; i < ab->pci.msi.config->total_vectors; i++) {856ret = platform_get_irq(pdev, i);857if (ret < 0)858return ret;859860ab->pci.msi.irqs[i] = ret;861}862863set_bit(ATH11K_FLAG_MULTI_MSI_VECTORS, &ab->dev_flags);864865return 0;866}867868static int ath11k_ahb_setup_smp2p_handle(struct ath11k_base *ab)869{870struct ath11k_ahb *ab_ahb = ath11k_ahb_priv(ab);871872if (!ab->hw_params.smp2p_wow_exit)873return 0;874875ab_ahb->smp2p_info.smem_state = qcom_smem_state_get(ab->dev, "wlan-smp2p-out",876&ab_ahb->smp2p_info.smem_bit);877if (IS_ERR(ab_ahb->smp2p_info.smem_state)) {878ath11k_err(ab, "failed to fetch smem state: %ld\n",879PTR_ERR(ab_ahb->smp2p_info.smem_state));880return PTR_ERR(ab_ahb->smp2p_info.smem_state);881}882883return 0;884}885886static void ath11k_ahb_release_smp2p_handle(struct ath11k_base *ab)887{888struct ath11k_ahb *ab_ahb = ath11k_ahb_priv(ab);889890if (!ab->hw_params.smp2p_wow_exit)891return;892893qcom_smem_state_put(ab_ahb->smp2p_info.smem_state);894}895896static int ath11k_ahb_setup_resources(struct ath11k_base *ab)897{898struct platform_device *pdev = ab->pdev;899struct resource *mem_res;900void __iomem *mem;901902if (ab->hw_params.hybrid_bus_type)903return ath11k_ahb_setup_msi_resources(ab);904905mem = devm_platform_get_and_ioremap_resource(pdev, 0, &mem_res);906if (IS_ERR(mem)) {907dev_err(&pdev->dev, "ioremap error\n");908return PTR_ERR(mem);909}910911ab->mem = mem;912ab->mem_len = resource_size(mem_res);913914return 0;915}916917static int ath11k_ahb_setup_msa_resources(struct ath11k_base *ab)918{919struct ath11k_ahb *ab_ahb = ath11k_ahb_priv(ab);920struct device *dev = ab->dev;921struct resource r;922int ret;923924ret = of_reserved_mem_region_to_resource(dev->of_node, 0, &r);925if (ret) {926dev_err(dev, "failed to resolve msa fixed region\n");927return ret;928}929930ab_ahb->fw.msa_paddr = r.start;931ab_ahb->fw.msa_size = resource_size(&r);932933ret = of_reserved_mem_region_to_resource(dev->of_node, 1, &r);934if (ret) {935dev_err(dev, "failed to resolve ce fixed region\n");936return ret;937}938939ab_ahb->fw.ce_paddr = r.start;940ab_ahb->fw.ce_size = resource_size(&r);941942return 0;943}944945static int ath11k_ahb_ce_remap(struct ath11k_base *ab)946{947const struct ce_remap *ce_remap = ab->hw_params.ce_remap;948struct platform_device *pdev = ab->pdev;949950if (!ce_remap) {951/* no separate CE register space */952ab->mem_ce = ab->mem;953return 0;954}955956/* ce register space is moved out of wcss unlike ipq8074 or ipq6018957* and the space is not contiguous, hence remapping the CE registers958* to a new space for accessing them.959*/960ab->mem_ce = ioremap(ce_remap->base, ce_remap->size);961if (!ab->mem_ce) {962dev_err(&pdev->dev, "ce ioremap error\n");963return -ENOMEM;964}965966return 0;967}968969static void ath11k_ahb_ce_unmap(struct ath11k_base *ab)970{971if (ab->hw_params.ce_remap)972iounmap(ab->mem_ce);973}974975static int ath11k_ahb_fw_resources_init(struct ath11k_base *ab)976{977struct ath11k_ahb *ab_ahb = ath11k_ahb_priv(ab);978struct device *host_dev = ab->dev;979struct platform_device_info info = {};980struct iommu_domain *iommu_dom;981struct platform_device *pdev;982struct device_node *node;983int ret;984985/* Chipsets not requiring MSA need not initialize986* MSA resources, return success in such cases.987*/988if (!ab->hw_params.fixed_fw_mem)989return 0;990991node = of_get_child_by_name(host_dev->of_node, "wifi-firmware");992if (!node) {993ab_ahb->fw.use_tz = true;994return 0;995}996997ret = ath11k_ahb_setup_msa_resources(ab);998if (ret) {999ath11k_err(ab, "failed to setup msa resources\n");1000return ret;1001}10021003info.fwnode = &node->fwnode;1004info.parent = host_dev;1005info.name = node->name;1006info.dma_mask = DMA_BIT_MASK(32);10071008pdev = platform_device_register_full(&info);1009if (IS_ERR(pdev)) {1010of_node_put(node);1011return PTR_ERR(pdev);1012}10131014ret = of_dma_configure(&pdev->dev, node, true);1015if (ret) {1016ath11k_err(ab, "dma configure fail: %d\n", ret);1017goto err_unregister;1018}10191020ab_ahb->fw.dev = &pdev->dev;10211022iommu_dom = iommu_paging_domain_alloc(ab_ahb->fw.dev);1023if (IS_ERR(iommu_dom)) {1024ath11k_err(ab, "failed to allocate iommu domain\n");1025ret = PTR_ERR(iommu_dom);1026goto err_unregister;1027}10281029ret = iommu_attach_device(iommu_dom, ab_ahb->fw.dev);1030if (ret) {1031ath11k_err(ab, "could not attach device: %d\n", ret);1032goto err_iommu_free;1033}10341035ret = iommu_map(iommu_dom, ab_ahb->fw.msa_paddr,1036ab_ahb->fw.msa_paddr, ab_ahb->fw.msa_size,1037IOMMU_READ | IOMMU_WRITE, GFP_KERNEL);1038if (ret) {1039ath11k_err(ab, "failed to map firmware region: %d\n", ret);1040goto err_iommu_detach;1041}10421043ret = iommu_map(iommu_dom, ab_ahb->fw.ce_paddr,1044ab_ahb->fw.ce_paddr, ab_ahb->fw.ce_size,1045IOMMU_READ | IOMMU_WRITE, GFP_KERNEL);1046if (ret) {1047ath11k_err(ab, "failed to map firmware CE region: %d\n", ret);1048goto err_iommu_unmap;1049}10501051ab_ahb->fw.use_tz = false;1052ab_ahb->fw.iommu_domain = iommu_dom;1053of_node_put(node);10541055return 0;10561057err_iommu_unmap:1058iommu_unmap(iommu_dom, ab_ahb->fw.msa_paddr, ab_ahb->fw.msa_size);10591060err_iommu_detach:1061iommu_detach_device(iommu_dom, ab_ahb->fw.dev);10621063err_iommu_free:1064iommu_domain_free(iommu_dom);10651066err_unregister:1067platform_device_unregister(pdev);1068of_node_put(node);10691070return ret;1071}10721073static int ath11k_ahb_fw_resource_deinit(struct ath11k_base *ab)1074{1075struct ath11k_ahb *ab_ahb = ath11k_ahb_priv(ab);1076struct iommu_domain *iommu;1077size_t unmapped_size;10781079/* Chipsets not requiring MSA would have not initialized1080* MSA resources, return success in such cases.1081*/1082if (!ab->hw_params.fixed_fw_mem)1083return 0;10841085if (ab_ahb->fw.use_tz)1086return 0;10871088iommu = ab_ahb->fw.iommu_domain;10891090unmapped_size = iommu_unmap(iommu, ab_ahb->fw.msa_paddr, ab_ahb->fw.msa_size);1091if (unmapped_size != ab_ahb->fw.msa_size)1092ath11k_err(ab, "failed to unmap firmware: %zu\n",1093unmapped_size);10941095unmapped_size = iommu_unmap(iommu, ab_ahb->fw.ce_paddr, ab_ahb->fw.ce_size);1096if (unmapped_size != ab_ahb->fw.ce_size)1097ath11k_err(ab, "failed to unmap firmware CE memory: %zu\n",1098unmapped_size);10991100iommu_detach_device(iommu, ab_ahb->fw.dev);1101iommu_domain_free(iommu);11021103platform_device_unregister(to_platform_device(ab_ahb->fw.dev));11041105return 0;1106}11071108static int ath11k_ahb_probe(struct platform_device *pdev)1109{1110struct ath11k_base *ab;1111const struct ath11k_hif_ops *hif_ops;1112const struct ath11k_pci_ops *pci_ops;1113enum ath11k_hw_rev hw_rev;1114int ret;11151116hw_rev = (uintptr_t)device_get_match_data(&pdev->dev);11171118switch (hw_rev) {1119case ATH11K_HW_IPQ8074:1120case ATH11K_HW_IPQ6018_HW10:1121case ATH11K_HW_IPQ5018_HW10:1122hif_ops = &ath11k_ahb_hif_ops_ipq8074;1123pci_ops = NULL;1124break;1125case ATH11K_HW_WCN6750_HW10:1126hif_ops = &ath11k_ahb_hif_ops_wcn6750;1127pci_ops = &ath11k_ahb_pci_ops_wcn6750;1128break;1129default:1130dev_err(&pdev->dev, "unsupported device type %d\n", hw_rev);1131return -EOPNOTSUPP;1132}11331134ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));1135if (ret) {1136dev_err(&pdev->dev, "failed to set 32-bit consistent dma\n");1137return ret;1138}11391140ab = ath11k_core_alloc(&pdev->dev, sizeof(struct ath11k_ahb),1141ATH11K_BUS_AHB);1142if (!ab) {1143dev_err(&pdev->dev, "failed to allocate ath11k base\n");1144return -ENOMEM;1145}11461147ab->hif.ops = hif_ops;1148ab->pdev = pdev;1149ab->hw_rev = hw_rev;1150ab->fw_mode = ATH11K_FIRMWARE_MODE_NORMAL;1151platform_set_drvdata(pdev, ab);11521153ret = ath11k_pcic_register_pci_ops(ab, pci_ops);1154if (ret) {1155ath11k_err(ab, "failed to register PCI ops: %d\n", ret);1156goto err_core_free;1157}11581159ret = ath11k_core_pre_init(ab);1160if (ret)1161goto err_core_free;11621163ret = ath11k_ahb_setup_resources(ab);1164if (ret)1165goto err_core_free;11661167ret = ath11k_ahb_ce_remap(ab);1168if (ret)1169goto err_core_free;11701171ret = ath11k_ahb_fw_resources_init(ab);1172if (ret)1173goto err_ce_unmap;11741175ret = ath11k_ahb_setup_smp2p_handle(ab);1176if (ret)1177goto err_fw_deinit;11781179ret = ath11k_hal_srng_init(ab);1180if (ret)1181goto err_release_smp2p_handle;11821183ret = ath11k_ce_alloc_pipes(ab);1184if (ret) {1185ath11k_err(ab, "failed to allocate ce pipes: %d\n", ret);1186goto err_hal_srng_deinit;1187}11881189ath11k_ahb_init_qmi_ce_config(ab);11901191ret = ath11k_core_get_rproc(ab);1192if (ret) {1193ath11k_err(ab, "failed to get rproc: %d\n", ret);1194goto err_ce_free;1195}11961197ret = ath11k_core_init(ab);1198if (ret) {1199ath11k_err(ab, "failed to init core: %d\n", ret);1200goto err_ce_free;1201}12021203ret = ath11k_ahb_config_irq(ab);1204if (ret) {1205ath11k_err(ab, "failed to configure irq: %d\n", ret);1206goto err_ce_free;1207}12081209ath11k_qmi_fwreset_from_cold_boot(ab);12101211return 0;12121213err_ce_free:1214ath11k_ce_free_pipes(ab);12151216err_hal_srng_deinit:1217ath11k_hal_srng_deinit(ab);12181219err_release_smp2p_handle:1220ath11k_ahb_release_smp2p_handle(ab);12211222err_fw_deinit:1223ath11k_ahb_fw_resource_deinit(ab);12241225err_ce_unmap:1226ath11k_ahb_ce_unmap(ab);12271228err_core_free:1229ath11k_core_free(ab);1230platform_set_drvdata(pdev, NULL);12311232return ret;1233}12341235static void ath11k_ahb_remove_prepare(struct ath11k_base *ab)1236{1237unsigned long left;12381239if (test_bit(ATH11K_FLAG_RECOVERY, &ab->dev_flags)) {1240left = wait_for_completion_timeout(&ab->driver_recovery,1241ATH11K_AHB_RECOVERY_TIMEOUT);1242if (!left)1243ath11k_warn(ab, "failed to receive recovery response completion\n");1244}12451246set_bit(ATH11K_FLAG_UNREGISTERING, &ab->dev_flags);1247cancel_work_sync(&ab->restart_work);1248cancel_work_sync(&ab->qmi.event_work);1249}12501251static void ath11k_ahb_free_resources(struct ath11k_base *ab)1252{1253struct platform_device *pdev = ab->pdev;12541255ath11k_ahb_free_irq(ab);1256ath11k_hal_srng_deinit(ab);1257ath11k_ahb_release_smp2p_handle(ab);1258ath11k_ahb_fw_resource_deinit(ab);1259ath11k_ce_free_pipes(ab);1260ath11k_ahb_ce_unmap(ab);12611262ath11k_core_free(ab);1263platform_set_drvdata(pdev, NULL);1264}12651266static void ath11k_ahb_remove(struct platform_device *pdev)1267{1268struct ath11k_base *ab = platform_get_drvdata(pdev);12691270if (test_bit(ATH11K_FLAG_QMI_FAIL, &ab->dev_flags)) {1271ath11k_ahb_power_down(ab, false);1272ath11k_debugfs_soc_destroy(ab);1273ath11k_qmi_deinit_service(ab);1274goto qmi_fail;1275}12761277ath11k_ahb_remove_prepare(ab);1278ath11k_core_deinit(ab);12791280qmi_fail:1281ath11k_fw_destroy(ab);1282ath11k_ahb_free_resources(ab);1283}12841285static void ath11k_ahb_shutdown(struct platform_device *pdev)1286{1287struct ath11k_base *ab = platform_get_drvdata(pdev);12881289/* platform shutdown() & remove() are mutually exclusive.1290* remove() is invoked during rmmod & shutdown() during1291* system reboot/shutdown.1292*/1293ath11k_ahb_remove_prepare(ab);12941295if (!(test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags)))1296goto free_resources;12971298ath11k_core_deinit(ab);12991300free_resources:1301ath11k_fw_destroy(ab);1302ath11k_ahb_free_resources(ab);1303}13041305static struct platform_driver ath11k_ahb_driver = {1306.driver = {1307.name = "ath11k",1308.of_match_table = ath11k_ahb_of_match,1309},1310.probe = ath11k_ahb_probe,1311.remove = ath11k_ahb_remove,1312.shutdown = ath11k_ahb_shutdown,1313};13141315module_platform_driver(ath11k_ahb_driver);13161317MODULE_DESCRIPTION("Driver support for Qualcomm Technologies 802.11ax WLAN AHB devices");1318MODULE_LICENSE("Dual BSD/GPL");131913201321