1 // SPDX-License-Identifier: ISC
3 * Copyright (c) 2018 The Linux Foundation. All rights reserved.
6 #include <linux/bits.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
11 #include <linux/of_device.h>
12 #include <linux/platform_device.h>
13 #include <linux/property.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/remoteproc/qcom_rproc.h>
16 #include <linux/of_address.h>
17 #include <linux/iommu.h>
26 #define ATH10K_SNOC_RX_POST_RETRY_MS 50
27 #define CE_POLL_PIPE 4
28 #define ATH10K_SNOC_WAKE_IRQ 2
30 static char *const ce_name[] = {
45 static const char * const ath10k_regulators[] = {
53 static const char * const ath10k_clocks[] = {
54 "cxo_ref_clk_pin", "qdss",
57 static void ath10k_snoc_htc_tx_cb(struct ath10k_ce_pipe *ce_state);
58 static void ath10k_snoc_htt_tx_cb(struct ath10k_ce_pipe *ce_state);
59 static void ath10k_snoc_htc_rx_cb(struct ath10k_ce_pipe *ce_state);
60 static void ath10k_snoc_htt_rx_cb(struct ath10k_ce_pipe *ce_state);
61 static void ath10k_snoc_htt_htc_rx_cb(struct ath10k_ce_pipe *ce_state);
62 static void ath10k_snoc_pktlog_rx_cb(struct ath10k_ce_pipe *ce_state);
64 static const struct ath10k_snoc_drv_priv drv_priv = {
65 .hw_rev = ATH10K_HW_WCN3990,
66 .dma_mask = DMA_BIT_MASK(35),
70 #define WCN3990_SRC_WR_IDX_OFFSET 0x3C
71 #define WCN3990_DST_WR_IDX_OFFSET 0x40
73 static struct ath10k_shadow_reg_cfg target_shadow_reg_cfg_map[] = {
75 .ce_id = __cpu_to_le16(0),
76 .reg_offset = __cpu_to_le16(WCN3990_SRC_WR_IDX_OFFSET),
80 .ce_id = __cpu_to_le16(3),
81 .reg_offset = __cpu_to_le16(WCN3990_SRC_WR_IDX_OFFSET),
85 .ce_id = __cpu_to_le16(4),
86 .reg_offset = __cpu_to_le16(WCN3990_SRC_WR_IDX_OFFSET),
90 .ce_id = __cpu_to_le16(5),
91 .reg_offset = __cpu_to_le16(WCN3990_SRC_WR_IDX_OFFSET),
95 .ce_id = __cpu_to_le16(7),
96 .reg_offset = __cpu_to_le16(WCN3990_SRC_WR_IDX_OFFSET),
100 .ce_id = __cpu_to_le16(1),
101 .reg_offset = __cpu_to_le16(WCN3990_DST_WR_IDX_OFFSET),
105 .ce_id = __cpu_to_le16(2),
106 .reg_offset = __cpu_to_le16(WCN3990_DST_WR_IDX_OFFSET),
110 .ce_id = __cpu_to_le16(7),
111 .reg_offset = __cpu_to_le16(WCN3990_DST_WR_IDX_OFFSET),
115 .ce_id = __cpu_to_le16(8),
116 .reg_offset = __cpu_to_le16(WCN3990_DST_WR_IDX_OFFSET),
120 .ce_id = __cpu_to_le16(9),
121 .reg_offset = __cpu_to_le16(WCN3990_DST_WR_IDX_OFFSET),
125 .ce_id = __cpu_to_le16(10),
126 .reg_offset = __cpu_to_le16(WCN3990_DST_WR_IDX_OFFSET),
130 .ce_id = __cpu_to_le16(11),
131 .reg_offset = __cpu_to_le16(WCN3990_DST_WR_IDX_OFFSET),
135 static struct ce_attr host_ce_config_wlan[] = {
136 /* CE0: host->target HTC control streams */
138 .flags = CE_ATTR_FLAGS,
142 .send_cb = ath10k_snoc_htc_tx_cb,
145 /* CE1: target->host HTT + HTC control */
147 .flags = CE_ATTR_FLAGS,
150 .dest_nentries = 512,
151 .recv_cb = ath10k_snoc_htt_htc_rx_cb,
154 /* CE2: target->host WMI */
156 .flags = CE_ATTR_FLAGS,
160 .recv_cb = ath10k_snoc_htc_rx_cb,
163 /* CE3: host->target WMI */
165 .flags = CE_ATTR_FLAGS,
169 .send_cb = ath10k_snoc_htc_tx_cb,
172 /* CE4: host->target HTT */
174 .flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
175 .src_nentries = 2048,
178 .send_cb = ath10k_snoc_htt_tx_cb,
181 /* CE5: target->host HTT (ipa_uc->target ) */
183 .flags = CE_ATTR_FLAGS,
186 .dest_nentries = 512,
187 .recv_cb = ath10k_snoc_htt_rx_cb,
190 /* CE6: target autonomous hif_memcpy */
192 .flags = CE_ATTR_FLAGS,
198 /* CE7: ce_diag, the Diagnostic Window */
200 .flags = CE_ATTR_FLAGS,
206 /* CE8: Target to uMC */
208 .flags = CE_ATTR_FLAGS,
211 .dest_nentries = 128,
214 /* CE9 target->host HTT */
216 .flags = CE_ATTR_FLAGS,
219 .dest_nentries = 512,
220 .recv_cb = ath10k_snoc_htt_htc_rx_cb,
223 /* CE10: target->host HTT */
225 .flags = CE_ATTR_FLAGS,
228 .dest_nentries = 512,
229 .recv_cb = ath10k_snoc_htt_htc_rx_cb,
232 /* CE11: target -> host PKTLOG */
234 .flags = CE_ATTR_FLAGS,
237 .dest_nentries = 512,
238 .recv_cb = ath10k_snoc_pktlog_rx_cb,
242 static struct ce_pipe_config target_ce_config_wlan[] = {
243 /* CE0: host->target HTC control and raw streams */
245 .pipenum = __cpu_to_le32(0),
246 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
247 .nentries = __cpu_to_le32(32),
248 .nbytes_max = __cpu_to_le32(2048),
249 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
250 .reserved = __cpu_to_le32(0),
253 /* CE1: target->host HTT + HTC control */
255 .pipenum = __cpu_to_le32(1),
256 .pipedir = __cpu_to_le32(PIPEDIR_IN),
257 .nentries = __cpu_to_le32(32),
258 .nbytes_max = __cpu_to_le32(2048),
259 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
260 .reserved = __cpu_to_le32(0),
263 /* CE2: target->host WMI */
265 .pipenum = __cpu_to_le32(2),
266 .pipedir = __cpu_to_le32(PIPEDIR_IN),
267 .nentries = __cpu_to_le32(64),
268 .nbytes_max = __cpu_to_le32(2048),
269 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
270 .reserved = __cpu_to_le32(0),
273 /* CE3: host->target WMI */
275 .pipenum = __cpu_to_le32(3),
276 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
277 .nentries = __cpu_to_le32(32),
278 .nbytes_max = __cpu_to_le32(2048),
279 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
280 .reserved = __cpu_to_le32(0),
283 /* CE4: host->target HTT */
285 .pipenum = __cpu_to_le32(4),
286 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
287 .nentries = __cpu_to_le32(256),
288 .nbytes_max = __cpu_to_le32(256),
289 .flags = __cpu_to_le32(CE_ATTR_FLAGS | CE_ATTR_DIS_INTR),
290 .reserved = __cpu_to_le32(0),
293 /* CE5: target->host HTT (HIF->HTT) */
295 .pipenum = __cpu_to_le32(5),
296 .pipedir = __cpu_to_le32(PIPEDIR_OUT),
297 .nentries = __cpu_to_le32(1024),
298 .nbytes_max = __cpu_to_le32(64),
299 .flags = __cpu_to_le32(CE_ATTR_FLAGS | CE_ATTR_DIS_INTR),
300 .reserved = __cpu_to_le32(0),
303 /* CE6: Reserved for target autonomous hif_memcpy */
305 .pipenum = __cpu_to_le32(6),
306 .pipedir = __cpu_to_le32(PIPEDIR_INOUT),
307 .nentries = __cpu_to_le32(32),
308 .nbytes_max = __cpu_to_le32(16384),
309 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
310 .reserved = __cpu_to_le32(0),
313 /* CE7 used only by Host */
315 .pipenum = __cpu_to_le32(7),
316 .pipedir = __cpu_to_le32(4),
317 .nentries = __cpu_to_le32(0),
318 .nbytes_max = __cpu_to_le32(0),
319 .flags = __cpu_to_le32(CE_ATTR_FLAGS | CE_ATTR_DIS_INTR),
320 .reserved = __cpu_to_le32(0),
323 /* CE8 Target to uMC */
325 .pipenum = __cpu_to_le32(8),
326 .pipedir = __cpu_to_le32(PIPEDIR_IN),
327 .nentries = __cpu_to_le32(32),
328 .nbytes_max = __cpu_to_le32(2048),
329 .flags = __cpu_to_le32(0),
330 .reserved = __cpu_to_le32(0),
333 /* CE9 target->host HTT */
335 .pipenum = __cpu_to_le32(9),
336 .pipedir = __cpu_to_le32(PIPEDIR_IN),
337 .nentries = __cpu_to_le32(32),
338 .nbytes_max = __cpu_to_le32(2048),
339 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
340 .reserved = __cpu_to_le32(0),
343 /* CE10 target->host HTT */
345 .pipenum = __cpu_to_le32(10),
346 .pipedir = __cpu_to_le32(PIPEDIR_IN),
347 .nentries = __cpu_to_le32(32),
348 .nbytes_max = __cpu_to_le32(2048),
349 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
350 .reserved = __cpu_to_le32(0),
353 /* CE11 target autonomous qcache memcpy */
355 .pipenum = __cpu_to_le32(11),
356 .pipedir = __cpu_to_le32(PIPEDIR_IN),
357 .nentries = __cpu_to_le32(32),
358 .nbytes_max = __cpu_to_le32(2048),
359 .flags = __cpu_to_le32(CE_ATTR_FLAGS),
360 .reserved = __cpu_to_le32(0),
364 static struct ce_service_to_pipe target_service_to_ce_map_wlan[] = {
366 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
367 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
371 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VO),
372 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
376 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
377 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
381 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BK),
382 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
386 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
387 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
391 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_BE),
392 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
396 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
397 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
401 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_DATA_VI),
402 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
406 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
407 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
411 __cpu_to_le32(ATH10K_HTC_SVC_ID_WMI_CONTROL),
412 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
416 __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
417 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
421 __cpu_to_le32(ATH10K_HTC_SVC_ID_RSVD_CTRL),
422 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
426 __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
427 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
431 __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
432 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
436 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
437 __cpu_to_le32(PIPEDIR_OUT), /* out = UL = host -> target */
441 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA_MSG),
442 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
446 __cpu_to_le32(ATH10K_HTC_SVC_ID_TEST_RAW_STREAMS),
447 __cpu_to_le32(PIPEDIR_OUT),
450 { /* in = DL = target -> host */
451 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA2_MSG),
452 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
455 { /* in = DL = target -> host */
456 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_DATA3_MSG),
457 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
460 { /* in = DL = target -> host pktlog */
461 __cpu_to_le32(ATH10K_HTC_SVC_ID_HTT_LOG_MSG),
462 __cpu_to_le32(PIPEDIR_IN), /* in = DL = target -> host */
465 /* (Additions here) */
474 static void ath10k_snoc_write32(struct ath10k *ar, u32 offset, u32 value)
476 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
478 iowrite32(value, ar_snoc->mem + offset);
481 static u32 ath10k_snoc_read32(struct ath10k *ar, u32 offset)
483 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
486 val = ioread32(ar_snoc->mem + offset);
491 static int __ath10k_snoc_rx_post_buf(struct ath10k_snoc_pipe *pipe)
493 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
494 struct ath10k *ar = pipe->hif_ce_state;
495 struct ath10k_ce *ce = ath10k_ce_priv(ar);
500 skb = dev_alloc_skb(pipe->buf_sz);
504 WARN_ONCE((unsigned long)skb->data & 3, "unaligned skb");
506 paddr = dma_map_single(ar->dev, skb->data,
507 skb->len + skb_tailroom(skb),
509 if (unlikely(dma_mapping_error(ar->dev, paddr))) {
510 ath10k_warn(ar, "failed to dma map snoc rx buf\n");
511 dev_kfree_skb_any(skb);
515 ATH10K_SKB_RXCB(skb)->paddr = paddr;
517 spin_lock_bh(&ce->ce_lock);
518 ret = ce_pipe->ops->ce_rx_post_buf(ce_pipe, skb, paddr);
519 spin_unlock_bh(&ce->ce_lock);
521 dma_unmap_single(ar->dev, paddr, skb->len + skb_tailroom(skb),
523 dev_kfree_skb_any(skb);
530 static void ath10k_snoc_rx_post_pipe(struct ath10k_snoc_pipe *pipe)
532 struct ath10k *ar = pipe->hif_ce_state;
533 struct ath10k_ce *ce = ath10k_ce_priv(ar);
534 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
535 struct ath10k_ce_pipe *ce_pipe = pipe->ce_hdl;
538 if (pipe->buf_sz == 0)
541 if (!ce_pipe->dest_ring)
544 spin_lock_bh(&ce->ce_lock);
545 num = __ath10k_ce_rx_num_free_bufs(ce_pipe);
546 spin_unlock_bh(&ce->ce_lock);
548 ret = __ath10k_snoc_rx_post_buf(pipe);
552 ath10k_warn(ar, "failed to post rx buf: %d\n", ret);
553 mod_timer(&ar_snoc->rx_post_retry, jiffies +
554 ATH10K_SNOC_RX_POST_RETRY_MS);
560 static void ath10k_snoc_rx_post(struct ath10k *ar)
562 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
565 for (i = 0; i < CE_COUNT; i++)
566 ath10k_snoc_rx_post_pipe(&ar_snoc->pipe_info[i]);
569 static void ath10k_snoc_process_rx_cb(struct ath10k_ce_pipe *ce_state,
570 void (*callback)(struct ath10k *ar,
571 struct sk_buff *skb))
573 struct ath10k *ar = ce_state->ar;
574 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
575 struct ath10k_snoc_pipe *pipe_info = &ar_snoc->pipe_info[ce_state->id];
577 struct sk_buff_head list;
578 void *transfer_context;
579 unsigned int nbytes, max_nbytes;
581 __skb_queue_head_init(&list);
582 while (ath10k_ce_completed_recv_next(ce_state, &transfer_context,
584 skb = transfer_context;
585 max_nbytes = skb->len + skb_tailroom(skb);
586 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
587 max_nbytes, DMA_FROM_DEVICE);
589 if (unlikely(max_nbytes < nbytes)) {
590 ath10k_warn(ar, "rxed more than expected (nbytes %d, max %d)\n",
592 dev_kfree_skb_any(skb);
596 skb_put(skb, nbytes);
597 __skb_queue_tail(&list, skb);
600 while ((skb = __skb_dequeue(&list))) {
601 ath10k_dbg(ar, ATH10K_DBG_SNOC, "snoc rx ce pipe %d len %d\n",
602 ce_state->id, skb->len);
607 ath10k_snoc_rx_post_pipe(pipe_info);
610 static void ath10k_snoc_htc_rx_cb(struct ath10k_ce_pipe *ce_state)
612 ath10k_snoc_process_rx_cb(ce_state, ath10k_htc_rx_completion_handler);
615 static void ath10k_snoc_htt_htc_rx_cb(struct ath10k_ce_pipe *ce_state)
617 /* CE4 polling needs to be done whenever CE pipe which transports
618 * HTT Rx (target->host) is processed.
620 ath10k_ce_per_engine_service(ce_state->ar, CE_POLL_PIPE);
622 ath10k_snoc_process_rx_cb(ce_state, ath10k_htc_rx_completion_handler);
625 /* Called by lower (CE) layer when data is received from the Target.
626 * WCN3990 firmware uses separate CE(CE11) to transfer pktlog data.
628 static void ath10k_snoc_pktlog_rx_cb(struct ath10k_ce_pipe *ce_state)
630 ath10k_snoc_process_rx_cb(ce_state, ath10k_htc_rx_completion_handler);
633 static void ath10k_snoc_htt_rx_deliver(struct ath10k *ar, struct sk_buff *skb)
635 skb_pull(skb, sizeof(struct ath10k_htc_hdr));
636 ath10k_htt_t2h_msg_handler(ar, skb);
639 static void ath10k_snoc_htt_rx_cb(struct ath10k_ce_pipe *ce_state)
641 ath10k_ce_per_engine_service(ce_state->ar, CE_POLL_PIPE);
642 ath10k_snoc_process_rx_cb(ce_state, ath10k_snoc_htt_rx_deliver);
645 static void ath10k_snoc_rx_replenish_retry(struct timer_list *t)
647 struct ath10k_snoc *ar_snoc = from_timer(ar_snoc, t, rx_post_retry);
648 struct ath10k *ar = ar_snoc->ar;
650 ath10k_snoc_rx_post(ar);
653 static void ath10k_snoc_htc_tx_cb(struct ath10k_ce_pipe *ce_state)
655 struct ath10k *ar = ce_state->ar;
656 struct sk_buff_head list;
659 __skb_queue_head_init(&list);
660 while (ath10k_ce_completed_send_next(ce_state, (void **)&skb) == 0) {
664 __skb_queue_tail(&list, skb);
667 while ((skb = __skb_dequeue(&list)))
668 ath10k_htc_tx_completion_handler(ar, skb);
671 static void ath10k_snoc_htt_tx_cb(struct ath10k_ce_pipe *ce_state)
673 struct ath10k *ar = ce_state->ar;
676 while (ath10k_ce_completed_send_next(ce_state, (void **)&skb) == 0) {
680 dma_unmap_single(ar->dev, ATH10K_SKB_CB(skb)->paddr,
681 skb->len, DMA_TO_DEVICE);
682 ath10k_htt_hif_tx_complete(ar, skb);
686 static int ath10k_snoc_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
687 struct ath10k_hif_sg_item *items, int n_items)
689 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
690 struct ath10k_ce *ce = ath10k_ce_priv(ar);
691 struct ath10k_snoc_pipe *snoc_pipe;
692 struct ath10k_ce_pipe *ce_pipe;
695 snoc_pipe = &ar_snoc->pipe_info[pipe_id];
696 ce_pipe = snoc_pipe->ce_hdl;
697 spin_lock_bh(&ce->ce_lock);
699 for (i = 0; i < n_items - 1; i++) {
700 ath10k_dbg(ar, ATH10K_DBG_SNOC,
701 "snoc tx item %d paddr %pad len %d n_items %d\n",
702 i, &items[i].paddr, items[i].len, n_items);
704 err = ath10k_ce_send_nolock(ce_pipe,
705 items[i].transfer_context,
708 items[i].transfer_id,
709 CE_SEND_FLAG_GATHER);
714 ath10k_dbg(ar, ATH10K_DBG_SNOC,
715 "snoc tx item %d paddr %pad len %d n_items %d\n",
716 i, &items[i].paddr, items[i].len, n_items);
718 err = ath10k_ce_send_nolock(ce_pipe,
719 items[i].transfer_context,
722 items[i].transfer_id,
727 spin_unlock_bh(&ce->ce_lock);
733 __ath10k_ce_send_revert(ce_pipe);
735 spin_unlock_bh(&ce->ce_lock);
739 static int ath10k_snoc_hif_get_target_info(struct ath10k *ar,
740 struct bmi_target_info *target_info)
742 target_info->version = ATH10K_HW_WCN3990;
743 target_info->type = ATH10K_HW_WCN3990;
748 static u16 ath10k_snoc_hif_get_free_queue_number(struct ath10k *ar, u8 pipe)
750 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
752 ath10k_dbg(ar, ATH10K_DBG_SNOC, "hif get free queue number\n");
754 return ath10k_ce_num_free_src_entries(ar_snoc->pipe_info[pipe].ce_hdl);
757 static void ath10k_snoc_hif_send_complete_check(struct ath10k *ar, u8 pipe,
762 ath10k_dbg(ar, ATH10K_DBG_SNOC, "snoc hif send complete check\n");
765 resources = ath10k_snoc_hif_get_free_queue_number(ar, pipe);
767 if (resources > (host_ce_config_wlan[pipe].src_nentries >> 1))
770 ath10k_ce_per_engine_service(ar, pipe);
773 static int ath10k_snoc_hif_map_service_to_pipe(struct ath10k *ar,
775 u8 *ul_pipe, u8 *dl_pipe)
777 const struct ce_service_to_pipe *entry;
778 bool ul_set = false, dl_set = false;
781 ath10k_dbg(ar, ATH10K_DBG_SNOC, "snoc hif map service\n");
783 for (i = 0; i < ARRAY_SIZE(target_service_to_ce_map_wlan); i++) {
784 entry = &target_service_to_ce_map_wlan[i];
786 if (__le32_to_cpu(entry->service_id) != service_id)
789 switch (__le32_to_cpu(entry->pipedir)) {
794 *dl_pipe = __le32_to_cpu(entry->pipenum);
799 *ul_pipe = __le32_to_cpu(entry->pipenum);
805 *dl_pipe = __le32_to_cpu(entry->pipenum);
806 *ul_pipe = __le32_to_cpu(entry->pipenum);
813 if (!ul_set || !dl_set)
819 static void ath10k_snoc_hif_get_default_pipe(struct ath10k *ar,
820 u8 *ul_pipe, u8 *dl_pipe)
822 ath10k_dbg(ar, ATH10K_DBG_SNOC, "snoc hif get default pipe\n");
824 (void)ath10k_snoc_hif_map_service_to_pipe(ar,
825 ATH10K_HTC_SVC_ID_RSVD_CTRL,
829 static inline void ath10k_snoc_irq_disable(struct ath10k *ar)
831 ath10k_ce_disable_interrupts(ar);
834 static inline void ath10k_snoc_irq_enable(struct ath10k *ar)
836 ath10k_ce_enable_interrupts(ar);
839 static void ath10k_snoc_rx_pipe_cleanup(struct ath10k_snoc_pipe *snoc_pipe)
841 struct ath10k_ce_pipe *ce_pipe;
842 struct ath10k_ce_ring *ce_ring;
847 ar = snoc_pipe->hif_ce_state;
848 ce_pipe = snoc_pipe->ce_hdl;
849 ce_ring = ce_pipe->dest_ring;
854 if (!snoc_pipe->buf_sz)
857 for (i = 0; i < ce_ring->nentries; i++) {
858 skb = ce_ring->per_transfer_context[i];
862 ce_ring->per_transfer_context[i] = NULL;
864 dma_unmap_single(ar->dev, ATH10K_SKB_RXCB(skb)->paddr,
865 skb->len + skb_tailroom(skb),
867 dev_kfree_skb_any(skb);
871 static void ath10k_snoc_tx_pipe_cleanup(struct ath10k_snoc_pipe *snoc_pipe)
873 struct ath10k_ce_pipe *ce_pipe;
874 struct ath10k_ce_ring *ce_ring;
879 ar = snoc_pipe->hif_ce_state;
880 ce_pipe = snoc_pipe->ce_hdl;
881 ce_ring = ce_pipe->src_ring;
886 if (!snoc_pipe->buf_sz)
889 for (i = 0; i < ce_ring->nentries; i++) {
890 skb = ce_ring->per_transfer_context[i];
894 ce_ring->per_transfer_context[i] = NULL;
896 ath10k_htc_tx_completion_handler(ar, skb);
900 static void ath10k_snoc_buffer_cleanup(struct ath10k *ar)
902 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
903 struct ath10k_snoc_pipe *pipe_info;
906 del_timer_sync(&ar_snoc->rx_post_retry);
907 for (pipe_num = 0; pipe_num < CE_COUNT; pipe_num++) {
908 pipe_info = &ar_snoc->pipe_info[pipe_num];
909 ath10k_snoc_rx_pipe_cleanup(pipe_info);
910 ath10k_snoc_tx_pipe_cleanup(pipe_info);
914 static void ath10k_snoc_hif_stop(struct ath10k *ar)
916 if (!test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
917 ath10k_snoc_irq_disable(ar);
919 ath10k_core_napi_sync_disable(ar);
920 ath10k_snoc_buffer_cleanup(ar);
921 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif stop\n");
924 static int ath10k_snoc_hif_start(struct ath10k *ar)
926 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
928 bitmap_clear(ar_snoc->pending_ce_irqs, 0, CE_COUNT_MAX);
930 ath10k_core_napi_enable(ar);
931 ath10k_snoc_irq_enable(ar);
932 ath10k_snoc_rx_post(ar);
934 clear_bit(ATH10K_SNOC_FLAG_RECOVERY, &ar_snoc->flags);
936 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif start\n");
941 static int ath10k_snoc_init_pipes(struct ath10k *ar)
945 for (i = 0; i < CE_COUNT; i++) {
946 ret = ath10k_ce_init_pipe(ar, i, &host_ce_config_wlan[i]);
948 ath10k_err(ar, "failed to initialize copy engine pipe %d: %d\n",
957 static int ath10k_snoc_wlan_enable(struct ath10k *ar,
958 enum ath10k_firmware_mode fw_mode)
960 struct ath10k_tgt_pipe_cfg tgt_cfg[CE_COUNT_MAX];
961 struct ath10k_qmi_wlan_enable_cfg cfg;
962 enum wlfw_driver_mode_enum_v01 mode;
965 for (pipe_num = 0; pipe_num < CE_COUNT_MAX; pipe_num++) {
966 tgt_cfg[pipe_num].pipe_num =
967 target_ce_config_wlan[pipe_num].pipenum;
968 tgt_cfg[pipe_num].pipe_dir =
969 target_ce_config_wlan[pipe_num].pipedir;
970 tgt_cfg[pipe_num].nentries =
971 target_ce_config_wlan[pipe_num].nentries;
972 tgt_cfg[pipe_num].nbytes_max =
973 target_ce_config_wlan[pipe_num].nbytes_max;
974 tgt_cfg[pipe_num].flags =
975 target_ce_config_wlan[pipe_num].flags;
976 tgt_cfg[pipe_num].reserved = 0;
979 cfg.num_ce_tgt_cfg = sizeof(target_ce_config_wlan) /
980 sizeof(struct ath10k_tgt_pipe_cfg);
981 cfg.ce_tgt_cfg = (struct ath10k_tgt_pipe_cfg *)
983 cfg.num_ce_svc_pipe_cfg = sizeof(target_service_to_ce_map_wlan) /
984 sizeof(struct ath10k_svc_pipe_cfg);
985 cfg.ce_svc_cfg = (struct ath10k_svc_pipe_cfg *)
986 &target_service_to_ce_map_wlan;
987 cfg.num_shadow_reg_cfg = ARRAY_SIZE(target_shadow_reg_cfg_map);
988 cfg.shadow_reg_cfg = (struct ath10k_shadow_reg_cfg *)
989 &target_shadow_reg_cfg_map;
992 case ATH10K_FIRMWARE_MODE_NORMAL:
993 mode = QMI_WLFW_MISSION_V01;
995 case ATH10K_FIRMWARE_MODE_UTF:
996 mode = QMI_WLFW_FTM_V01;
999 ath10k_err(ar, "invalid firmware mode %d\n", fw_mode);
1003 return ath10k_qmi_wlan_enable(ar, &cfg, mode,
1007 static int ath10k_hw_power_on(struct ath10k *ar)
1009 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
1012 ath10k_dbg(ar, ATH10K_DBG_SNOC, "soc power on\n");
1014 ret = regulator_bulk_enable(ar_snoc->num_vregs, ar_snoc->vregs);
1018 ret = clk_bulk_prepare_enable(ar_snoc->num_clks, ar_snoc->clks);
1025 regulator_bulk_disable(ar_snoc->num_vregs, ar_snoc->vregs);
1029 static int ath10k_hw_power_off(struct ath10k *ar)
1031 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
1033 ath10k_dbg(ar, ATH10K_DBG_SNOC, "soc power off\n");
1035 clk_bulk_disable_unprepare(ar_snoc->num_clks, ar_snoc->clks);
1037 return regulator_bulk_disable(ar_snoc->num_vregs, ar_snoc->vregs);
1040 static void ath10k_snoc_wlan_disable(struct ath10k *ar)
1042 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
1044 /* If both ATH10K_FLAG_CRASH_FLUSH and ATH10K_SNOC_FLAG_RECOVERY
1045 * flags are not set, it means that the driver has restarted
1046 * due to a crash inject via debugfs. In this case, the driver
1047 * needs to restart the firmware and hence send qmi wlan disable,
1048 * during the driver restart sequence.
1050 if (!test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags) ||
1051 !test_bit(ATH10K_SNOC_FLAG_RECOVERY, &ar_snoc->flags))
1052 ath10k_qmi_wlan_disable(ar);
1055 static void ath10k_snoc_hif_power_down(struct ath10k *ar)
1057 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot hif power down\n");
1059 ath10k_snoc_wlan_disable(ar);
1060 ath10k_ce_free_rri(ar);
1061 ath10k_hw_power_off(ar);
1064 static int ath10k_snoc_hif_power_up(struct ath10k *ar,
1065 enum ath10k_firmware_mode fw_mode)
1069 ath10k_dbg(ar, ATH10K_DBG_SNOC, "%s:WCN3990 driver state = %d\n",
1070 __func__, ar->state);
1072 ret = ath10k_hw_power_on(ar);
1074 ath10k_err(ar, "failed to power on device: %d\n", ret);
1078 ret = ath10k_snoc_wlan_enable(ar, fw_mode);
1080 ath10k_err(ar, "failed to enable wcn3990: %d\n", ret);
1081 goto err_hw_power_off;
1084 ath10k_ce_alloc_rri(ar);
1086 ret = ath10k_snoc_init_pipes(ar);
1088 ath10k_err(ar, "failed to initialize CE: %d\n", ret);
1095 ath10k_ce_free_rri(ar);
1096 ath10k_snoc_wlan_disable(ar);
1099 ath10k_hw_power_off(ar);
1104 static int ath10k_snoc_hif_set_target_log_mode(struct ath10k *ar,
1110 fw_dbg_mode = ATH10K_ENABLE_FW_LOG_CE;
1112 fw_dbg_mode = ATH10K_ENABLE_FW_LOG_DIAG;
1114 return ath10k_qmi_set_fw_log_mode(ar, fw_dbg_mode);
1118 static int ath10k_snoc_hif_suspend(struct ath10k *ar)
1120 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
1123 if (!device_may_wakeup(ar->dev))
1126 ret = enable_irq_wake(ar_snoc->ce_irqs[ATH10K_SNOC_WAKE_IRQ].irq_line);
1128 ath10k_err(ar, "failed to enable wakeup irq :%d\n", ret);
1132 ath10k_dbg(ar, ATH10K_DBG_SNOC, "snoc device suspended\n");
1137 static int ath10k_snoc_hif_resume(struct ath10k *ar)
1139 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
1142 if (!device_may_wakeup(ar->dev))
1145 ret = disable_irq_wake(ar_snoc->ce_irqs[ATH10K_SNOC_WAKE_IRQ].irq_line);
1147 ath10k_err(ar, "failed to disable wakeup irq: %d\n", ret);
1151 ath10k_dbg(ar, ATH10K_DBG_SNOC, "snoc device resumed\n");
1157 static const struct ath10k_hif_ops ath10k_snoc_hif_ops = {
1158 .read32 = ath10k_snoc_read32,
1159 .write32 = ath10k_snoc_write32,
1160 .start = ath10k_snoc_hif_start,
1161 .stop = ath10k_snoc_hif_stop,
1162 .map_service_to_pipe = ath10k_snoc_hif_map_service_to_pipe,
1163 .get_default_pipe = ath10k_snoc_hif_get_default_pipe,
1164 .power_up = ath10k_snoc_hif_power_up,
1165 .power_down = ath10k_snoc_hif_power_down,
1166 .tx_sg = ath10k_snoc_hif_tx_sg,
1167 .send_complete_check = ath10k_snoc_hif_send_complete_check,
1168 .get_free_queue_number = ath10k_snoc_hif_get_free_queue_number,
1169 .get_target_info = ath10k_snoc_hif_get_target_info,
1170 .set_target_log_mode = ath10k_snoc_hif_set_target_log_mode,
1173 .suspend = ath10k_snoc_hif_suspend,
1174 .resume = ath10k_snoc_hif_resume,
1178 static const struct ath10k_bus_ops ath10k_snoc_bus_ops = {
1179 .read32 = ath10k_snoc_read32,
1180 .write32 = ath10k_snoc_write32,
1183 static int ath10k_snoc_get_ce_id_from_irq(struct ath10k *ar, int irq)
1185 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
1188 for (i = 0; i < CE_COUNT_MAX; i++) {
1189 if (ar_snoc->ce_irqs[i].irq_line == irq)
1192 ath10k_err(ar, "No matching CE id for irq %d\n", irq);
1197 static irqreturn_t ath10k_snoc_per_engine_handler(int irq, void *arg)
1199 struct ath10k *ar = arg;
1200 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
1201 int ce_id = ath10k_snoc_get_ce_id_from_irq(ar, irq);
1203 if (ce_id < 0 || ce_id >= ARRAY_SIZE(ar_snoc->pipe_info)) {
1204 ath10k_warn(ar, "unexpected/invalid irq %d ce_id %d\n", irq,
1209 ath10k_ce_disable_interrupt(ar, ce_id);
1210 set_bit(ce_id, ar_snoc->pending_ce_irqs);
1212 napi_schedule(&ar->napi);
1217 static int ath10k_snoc_napi_poll(struct napi_struct *ctx, int budget)
1219 struct ath10k *ar = container_of(ctx, struct ath10k, napi);
1220 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
1224 if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags)) {
1229 for (ce_id = 0; ce_id < CE_COUNT; ce_id++)
1230 if (test_and_clear_bit(ce_id, ar_snoc->pending_ce_irqs)) {
1231 ath10k_ce_per_engine_service(ar, ce_id);
1232 ath10k_ce_enable_interrupt(ar, ce_id);
1235 done = ath10k_htt_txrx_compl_task(ar, budget);
1243 static void ath10k_snoc_init_napi(struct ath10k *ar)
1245 netif_napi_add(&ar->napi_dev, &ar->napi, ath10k_snoc_napi_poll,
1249 static int ath10k_snoc_request_irq(struct ath10k *ar)
1251 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
1252 int irqflags = IRQF_TRIGGER_RISING;
1255 for (id = 0; id < CE_COUNT_MAX; id++) {
1256 ret = request_irq(ar_snoc->ce_irqs[id].irq_line,
1257 ath10k_snoc_per_engine_handler,
1258 irqflags, ce_name[id], ar);
1261 "failed to register IRQ handler for CE %d: %d\n",
1270 for (id -= 1; id >= 0; id--)
1271 free_irq(ar_snoc->ce_irqs[id].irq_line, ar);
1276 static void ath10k_snoc_free_irq(struct ath10k *ar)
1278 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
1281 for (id = 0; id < CE_COUNT_MAX; id++)
1282 free_irq(ar_snoc->ce_irqs[id].irq_line, ar);
1285 static int ath10k_snoc_resource_init(struct ath10k *ar)
1287 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
1288 struct platform_device *pdev;
1289 struct resource *res;
1292 pdev = ar_snoc->dev;
1293 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "membase");
1295 ath10k_err(ar, "Memory base not found in DT\n");
1299 ar_snoc->mem_pa = res->start;
1300 ar_snoc->mem = devm_ioremap(&pdev->dev, ar_snoc->mem_pa,
1301 resource_size(res));
1302 if (!ar_snoc->mem) {
1303 ath10k_err(ar, "Memory base ioremap failed with physical address %pa\n",
1308 for (i = 0; i < CE_COUNT; i++) {
1309 ret = platform_get_irq(ar_snoc->dev, i);
1312 ar_snoc->ce_irqs[i].irq_line = ret;
1315 ret = device_property_read_u32(&pdev->dev, "qcom,xo-cal-data",
1316 &ar_snoc->xo_cal_data);
1317 ath10k_dbg(ar, ATH10K_DBG_SNOC, "snoc xo-cal-data return %d\n", ret);
1319 ar_snoc->xo_cal_supported = true;
1320 ath10k_dbg(ar, ATH10K_DBG_SNOC, "xo cal data %x\n",
1321 ar_snoc->xo_cal_data);
1327 static void ath10k_snoc_quirks_init(struct ath10k *ar)
1329 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
1330 struct device *dev = &ar_snoc->dev->dev;
1332 if (of_property_read_bool(dev->of_node, "qcom,snoc-host-cap-8bit-quirk"))
1333 set_bit(ATH10K_SNOC_FLAG_8BIT_HOST_CAP_QUIRK, &ar_snoc->flags);
1336 int ath10k_snoc_fw_indication(struct ath10k *ar, u64 type)
1338 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
1339 struct ath10k_bus_params bus_params = {};
1342 if (test_bit(ATH10K_SNOC_FLAG_UNREGISTERING, &ar_snoc->flags))
1346 case ATH10K_QMI_EVENT_FW_READY_IND:
1347 if (test_bit(ATH10K_SNOC_FLAG_REGISTERED, &ar_snoc->flags)) {
1348 ath10k_core_start_recovery(ar);
1352 bus_params.dev_type = ATH10K_DEV_TYPE_LL;
1353 bus_params.chip_id = ar_snoc->target_info.soc_version;
1354 ret = ath10k_core_register(ar, &bus_params);
1356 ath10k_err(ar, "Failed to register driver core: %d\n",
1360 set_bit(ATH10K_SNOC_FLAG_REGISTERED, &ar_snoc->flags);
1362 case ATH10K_QMI_EVENT_FW_DOWN_IND:
1363 set_bit(ATH10K_SNOC_FLAG_RECOVERY, &ar_snoc->flags);
1364 set_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags);
1367 ath10k_err(ar, "invalid fw indication: %llx\n", type);
1374 static int ath10k_snoc_setup_resource(struct ath10k *ar)
1376 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
1377 struct ath10k_ce *ce = ath10k_ce_priv(ar);
1378 struct ath10k_snoc_pipe *pipe;
1381 timer_setup(&ar_snoc->rx_post_retry, ath10k_snoc_rx_replenish_retry, 0);
1382 spin_lock_init(&ce->ce_lock);
1383 for (i = 0; i < CE_COUNT; i++) {
1384 pipe = &ar_snoc->pipe_info[i];
1385 pipe->ce_hdl = &ce->ce_states[i];
1387 pipe->hif_ce_state = ar;
1389 ret = ath10k_ce_alloc_pipe(ar, i, &host_ce_config_wlan[i]);
1391 ath10k_err(ar, "failed to allocate copy engine pipe %d: %d\n",
1396 pipe->buf_sz = host_ce_config_wlan[i].src_sz_max;
1398 ath10k_snoc_init_napi(ar);
1403 static void ath10k_snoc_release_resource(struct ath10k *ar)
1407 netif_napi_del(&ar->napi);
1408 for (i = 0; i < CE_COUNT; i++)
1409 ath10k_ce_free_pipe(ar, i);
1412 static void ath10k_msa_dump_memory(struct ath10k *ar,
1413 struct ath10k_fw_crash_data *crash_data)
1415 const struct ath10k_hw_mem_layout *mem_layout;
1416 const struct ath10k_mem_region *current_region;
1417 struct ath10k_dump_ram_data_hdr *hdr;
1421 if (!crash_data || !crash_data->ramdump_buf)
1424 mem_layout = ath10k_coredump_get_mem_layout(ar);
1428 current_region = &mem_layout->region_table.regions[0];
1430 buf = crash_data->ramdump_buf;
1431 buf_len = crash_data->ramdump_buf_len;
1432 memset(buf, 0, buf_len);
1434 /* Reserve space for the header. */
1436 buf += sizeof(*hdr);
1437 buf_len -= sizeof(*hdr);
1439 hdr->region_type = cpu_to_le32(current_region->type);
1440 hdr->start = cpu_to_le32((unsigned long)ar->msa.vaddr);
1441 hdr->length = cpu_to_le32(ar->msa.mem_size);
1443 if (current_region->len < ar->msa.mem_size) {
1444 memcpy(buf, ar->msa.vaddr, current_region->len);
1445 ath10k_warn(ar, "msa dump length is less than msa size %x, %x\n",
1446 current_region->len, ar->msa.mem_size);
1448 memcpy(buf, ar->msa.vaddr, ar->msa.mem_size);
1452 void ath10k_snoc_fw_crashed_dump(struct ath10k *ar)
1454 struct ath10k_fw_crash_data *crash_data;
1455 char guid[UUID_STRING_LEN + 1];
1457 mutex_lock(&ar->dump_mutex);
1459 spin_lock_bh(&ar->data_lock);
1460 ar->stats.fw_crash_counter++;
1461 spin_unlock_bh(&ar->data_lock);
1463 crash_data = ath10k_coredump_new(ar);
1466 scnprintf(guid, sizeof(guid), "%pUl", &crash_data->guid);
1468 scnprintf(guid, sizeof(guid), "n/a");
1470 ath10k_err(ar, "firmware crashed! (guid %s)\n", guid);
1471 ath10k_print_driver_info(ar);
1472 ath10k_msa_dump_memory(ar, crash_data);
1473 mutex_unlock(&ar->dump_mutex);
1476 static int ath10k_snoc_modem_notify(struct notifier_block *nb, unsigned long action,
1479 struct ath10k_snoc *ar_snoc = container_of(nb, struct ath10k_snoc, nb);
1480 struct ath10k *ar = ar_snoc->ar;
1481 struct qcom_ssr_notify_data *notify_data = data;
1484 case QCOM_SSR_BEFORE_POWERUP:
1485 ath10k_dbg(ar, ATH10K_DBG_SNOC, "received modem starting event\n");
1486 clear_bit(ATH10K_SNOC_FLAG_MODEM_STOPPED, &ar_snoc->flags);
1489 case QCOM_SSR_AFTER_POWERUP:
1490 ath10k_dbg(ar, ATH10K_DBG_SNOC, "received modem running event\n");
1493 case QCOM_SSR_BEFORE_SHUTDOWN:
1494 ath10k_dbg(ar, ATH10K_DBG_SNOC, "received modem %s event\n",
1495 notify_data->crashed ? "crashed" : "stopping");
1496 if (!notify_data->crashed)
1497 set_bit(ATH10K_SNOC_FLAG_MODEM_STOPPED, &ar_snoc->flags);
1499 clear_bit(ATH10K_SNOC_FLAG_MODEM_STOPPED, &ar_snoc->flags);
1502 case QCOM_SSR_AFTER_SHUTDOWN:
1503 ath10k_dbg(ar, ATH10K_DBG_SNOC, "received modem offline event\n");
1507 ath10k_err(ar, "received unrecognized event %lu\n", action);
1514 static int ath10k_modem_init(struct ath10k *ar)
1516 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
1520 ar_snoc->nb.notifier_call = ath10k_snoc_modem_notify;
1522 notifier = qcom_register_ssr_notifier("mpss", &ar_snoc->nb);
1523 if (IS_ERR(notifier)) {
1524 ret = PTR_ERR(notifier);
1525 ath10k_err(ar, "failed to initialize modem notifier: %d\n", ret);
1529 ar_snoc->notifier = notifier;
1534 static void ath10k_modem_deinit(struct ath10k *ar)
1537 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
1539 ret = qcom_unregister_ssr_notifier(ar_snoc->notifier, &ar_snoc->nb);
1541 ath10k_err(ar, "error %d unregistering notifier\n", ret);
1544 static int ath10k_setup_msa_resources(struct ath10k *ar, u32 msa_size)
1546 struct device *dev = ar->dev;
1547 struct device_node *node;
1551 node = of_parse_phandle(dev->of_node, "memory-region", 0);
1553 ret = of_address_to_resource(node, 0, &r);
1556 dev_err(dev, "failed to resolve msa fixed region\n");
1560 ar->msa.paddr = r.start;
1561 ar->msa.mem_size = resource_size(&r);
1562 ar->msa.vaddr = devm_memremap(dev, ar->msa.paddr,
1565 if (IS_ERR(ar->msa.vaddr)) {
1566 dev_err(dev, "failed to map memory region: %pa\n",
1568 return PTR_ERR(ar->msa.vaddr);
1571 ar->msa.vaddr = dmam_alloc_coherent(dev, msa_size,
1574 if (!ar->msa.vaddr) {
1575 ath10k_err(ar, "failed to allocate dma memory for msa region\n");
1578 ar->msa.mem_size = msa_size;
1581 ath10k_dbg(ar, ATH10K_DBG_QMI, "qmi msa.paddr: %pad , msa.vaddr: 0x%p\n",
1588 static int ath10k_fw_init(struct ath10k *ar)
1590 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
1591 struct device *host_dev = &ar_snoc->dev->dev;
1592 struct platform_device_info info;
1593 struct iommu_domain *iommu_dom;
1594 struct platform_device *pdev;
1595 struct device_node *node;
1598 node = of_get_child_by_name(host_dev->of_node, "wifi-firmware");
1600 ar_snoc->use_tz = true;
1604 memset(&info, 0, sizeof(info));
1605 info.fwnode = &node->fwnode;
1606 info.parent = host_dev;
1607 info.name = node->name;
1608 info.dma_mask = DMA_BIT_MASK(32);
1610 pdev = platform_device_register_full(&info);
1613 return PTR_ERR(pdev);
1616 pdev->dev.of_node = node;
1618 ret = of_dma_configure(&pdev->dev, node, true);
1620 ath10k_err(ar, "dma configure fail: %d\n", ret);
1621 goto err_unregister;
1624 ar_snoc->fw.dev = &pdev->dev;
1626 iommu_dom = iommu_domain_alloc(&platform_bus_type);
1628 ath10k_err(ar, "failed to allocate iommu domain\n");
1630 goto err_unregister;
1633 ret = iommu_attach_device(iommu_dom, ar_snoc->fw.dev);
1635 ath10k_err(ar, "could not attach device: %d\n", ret);
1636 goto err_iommu_free;
1639 ar_snoc->fw.iommu_domain = iommu_dom;
1640 ar_snoc->fw.fw_start_addr = ar->msa.paddr;
1642 ret = iommu_map(iommu_dom, ar_snoc->fw.fw_start_addr,
1643 ar->msa.paddr, ar->msa.mem_size,
1644 IOMMU_READ | IOMMU_WRITE);
1646 ath10k_err(ar, "failed to map firmware region: %d\n", ret);
1647 goto err_iommu_detach;
1655 iommu_detach_device(iommu_dom, ar_snoc->fw.dev);
1658 iommu_domain_free(iommu_dom);
1661 platform_device_unregister(pdev);
1667 static int ath10k_fw_deinit(struct ath10k *ar)
1669 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
1670 const size_t mapped_size = ar_snoc->fw.mapped_mem_size;
1671 struct iommu_domain *iommu;
1672 size_t unmapped_size;
1674 if (ar_snoc->use_tz)
1677 iommu = ar_snoc->fw.iommu_domain;
1679 unmapped_size = iommu_unmap(iommu, ar_snoc->fw.fw_start_addr,
1681 if (unmapped_size != mapped_size)
1682 ath10k_err(ar, "failed to unmap firmware: %zu\n",
1685 iommu_detach_device(iommu, ar_snoc->fw.dev);
1686 iommu_domain_free(iommu);
1688 platform_device_unregister(to_platform_device(ar_snoc->fw.dev));
1693 static const struct of_device_id ath10k_snoc_dt_match[] = {
1694 { .compatible = "qcom,wcn3990-wifi",
1699 MODULE_DEVICE_TABLE(of, ath10k_snoc_dt_match);
1701 static int ath10k_snoc_probe(struct platform_device *pdev)
1703 const struct ath10k_snoc_drv_priv *drv_data;
1704 struct ath10k_snoc *ar_snoc;
1712 drv_data = device_get_match_data(dev);
1714 dev_err(dev, "failed to find matching device tree id\n");
1718 ret = dma_set_mask_and_coherent(dev, drv_data->dma_mask);
1720 dev_err(dev, "failed to set dma mask: %d\n", ret);
1724 ar = ath10k_core_create(sizeof(*ar_snoc), dev, ATH10K_BUS_SNOC,
1725 drv_data->hw_rev, &ath10k_snoc_hif_ops);
1727 dev_err(dev, "failed to allocate core\n");
1731 ar_snoc = ath10k_snoc_priv(ar);
1732 ar_snoc->dev = pdev;
1733 platform_set_drvdata(pdev, ar);
1735 ar_snoc->ce.bus_ops = &ath10k_snoc_bus_ops;
1736 ar->ce_priv = &ar_snoc->ce;
1737 msa_size = drv_data->msa_size;
1739 ath10k_snoc_quirks_init(ar);
1741 ret = ath10k_snoc_resource_init(ar);
1743 ath10k_warn(ar, "failed to initialize resource: %d\n", ret);
1744 goto err_core_destroy;
1747 ret = ath10k_snoc_setup_resource(ar);
1749 ath10k_warn(ar, "failed to setup resource: %d\n", ret);
1750 goto err_core_destroy;
1752 ret = ath10k_snoc_request_irq(ar);
1754 ath10k_warn(ar, "failed to request irqs: %d\n", ret);
1755 goto err_release_resource;
1758 ar_snoc->num_vregs = ARRAY_SIZE(ath10k_regulators);
1759 ar_snoc->vregs = devm_kcalloc(&pdev->dev, ar_snoc->num_vregs,
1760 sizeof(*ar_snoc->vregs), GFP_KERNEL);
1761 if (!ar_snoc->vregs) {
1765 for (i = 0; i < ar_snoc->num_vregs; i++)
1766 ar_snoc->vregs[i].supply = ath10k_regulators[i];
1768 ret = devm_regulator_bulk_get(&pdev->dev, ar_snoc->num_vregs,
1773 ar_snoc->num_clks = ARRAY_SIZE(ath10k_clocks);
1774 ar_snoc->clks = devm_kcalloc(&pdev->dev, ar_snoc->num_clks,
1775 sizeof(*ar_snoc->clks), GFP_KERNEL);
1776 if (!ar_snoc->clks) {
1781 for (i = 0; i < ar_snoc->num_clks; i++)
1782 ar_snoc->clks[i].id = ath10k_clocks[i];
1784 ret = devm_clk_bulk_get_optional(&pdev->dev, ar_snoc->num_clks,
1789 ret = ath10k_setup_msa_resources(ar, msa_size);
1791 ath10k_warn(ar, "failed to setup msa resources: %d\n", ret);
1795 ret = ath10k_fw_init(ar);
1797 ath10k_err(ar, "failed to initialize firmware: %d\n", ret);
1801 ret = ath10k_qmi_init(ar, msa_size);
1803 ath10k_warn(ar, "failed to register wlfw qmi client: %d\n", ret);
1807 ret = ath10k_modem_init(ar);
1809 goto err_qmi_deinit;
1811 ath10k_dbg(ar, ATH10K_DBG_SNOC, "snoc probe\n");
1816 ath10k_qmi_deinit(ar);
1819 ath10k_fw_deinit(ar);
1822 ath10k_snoc_free_irq(ar);
1824 err_release_resource:
1825 ath10k_snoc_release_resource(ar);
1828 ath10k_core_destroy(ar);
1833 static int ath10k_snoc_free_resources(struct ath10k *ar)
1835 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
1837 ath10k_dbg(ar, ATH10K_DBG_SNOC, "snoc free resources\n");
1839 set_bit(ATH10K_SNOC_FLAG_UNREGISTERING, &ar_snoc->flags);
1841 ath10k_core_unregister(ar);
1842 ath10k_fw_deinit(ar);
1843 ath10k_snoc_free_irq(ar);
1844 ath10k_snoc_release_resource(ar);
1845 ath10k_modem_deinit(ar);
1846 ath10k_qmi_deinit(ar);
1847 ath10k_core_destroy(ar);
1852 static int ath10k_snoc_remove(struct platform_device *pdev)
1854 struct ath10k *ar = platform_get_drvdata(pdev);
1855 struct ath10k_snoc *ar_snoc = ath10k_snoc_priv(ar);
1857 ath10k_dbg(ar, ATH10K_DBG_SNOC, "snoc remove\n");
1859 reinit_completion(&ar->driver_recovery);
1861 if (test_bit(ATH10K_SNOC_FLAG_RECOVERY, &ar_snoc->flags))
1862 wait_for_completion_timeout(&ar->driver_recovery, 3 * HZ);
1864 ath10k_snoc_free_resources(ar);
1869 static void ath10k_snoc_shutdown(struct platform_device *pdev)
1871 struct ath10k *ar = platform_get_drvdata(pdev);
1873 ath10k_dbg(ar, ATH10K_DBG_SNOC, "snoc shutdown\n");
1874 ath10k_snoc_free_resources(ar);
1877 static struct platform_driver ath10k_snoc_driver = {
1878 .probe = ath10k_snoc_probe,
1879 .remove = ath10k_snoc_remove,
1880 .shutdown = ath10k_snoc_shutdown,
1882 .name = "ath10k_snoc",
1883 .of_match_table = ath10k_snoc_dt_match,
1886 module_platform_driver(ath10k_snoc_driver);
1888 MODULE_AUTHOR("Qualcomm");
1889 MODULE_LICENSE("Dual BSD/GPL");
1890 MODULE_DESCRIPTION("Driver support for Atheros WCN3990 SNOC devices");