net: airoha: npu: use coherent DMA for mailbox messages
Subsystems (4)
PATCH v4 1/1 net: airoha: npu: use coherent DMA for mailbox messages
Commit 6f884eb87a79 ("net: airoha: Fix DMA direction for NPU mailbox buffer") switched airoha_npu_send_msg() to DMA_BIDIRECTIONAL so non-coherent CPUs invalidate caches before reading NPU GET responses.
On EN7581 + MT7996 that change regresses probe: the mailbox completes successfully, but WLAN_FUNC_GET_WAIT_NPU_VERSION still reads as 0.0 and mt76 never binds NPU offload. Healthy boards report 0.1111.
airoha_npu_send_msg() is also used from PPE foe_commit under spin_lock_bh(), so per-message dma_alloc_coherent(GFP_ATOMIC) is a poor fit. Allocate one device-managed coherent bounce buffer per NPU core at probe and reuse it under the existing per-core mailbox lock. That also keeps the buffer valid if the NPU completes a write after a mailbox timeout.
Introduce __airoha_npu_send_msg() with an optional reply pointer so GET callers can copy the trailing response payload once. Keep airoha_npu_send_msg() as a thin wrapper for SET paths.
Verified on Quantum Fiber / Gemtek W1700K (EN7581 + MT7996).
---
v4:
- drop rsp_off; copy trailing reply_len bytes into reply pointer
- split __airoha_npu_send_msg() + thin airoha_npu_send_msg() wrapper so SET callers stay unchanged
- fix reverse-christmas-tree local variable ordering
- constify data argument; clarify reply copy comment
v3:
- move coherent mailbox buffer into struct airoha_npu_core (buf/addr)
- allocate one buffer per core at probe
- add optional reply buffer to avoid the extra copy in airoha_npu_wlan_msg_get()
- document AIROHA_NPU_MBOX_SIZE vs ppe_mbox_data / WLAN payloads
v2:
- allocate one coherent mailbox buffer at probe and reuse it under the per-core lock instead of per-message dma_alloc_coherent(GFP_ATOMIC)
drivers/net/ethernet/airoha/airoha_npu.c | 70 +++++++++++++++++------ include/linux/soc/airoha/airoha_offload.h | 3 + 2 files changed, 54 insertions(+), 19 deletions(-)
drivers/net/ethernet/airoha/airoha_npu.c +51 -19
@@ -23,6 +23,12 @@#define NPU_EN7581_FIRMWARE_RV32_MAX_SIZE 0x200000#define NPU_EN7581_FIRMWARE_DATA_MAX_SIZE 0x10000#define NPU_DUMP_SIZE 512 +/* +* Mailbox DMA payload size. Covers sizeof(struct ppe_mbox_data) (28) and +* WLAN TLV messages (header + payload); largest in-tree WLAN payload today +* is 16 bytes (INODE_TXRX_REG_ADDR). Keep headroom for future commands. +*/ +#define AIROHA_NPU_MBOX_SIZE 256#define REG_NPU_LOCAL_SRAM 0x0
@@ -160,23 +166,32 @@ struct wlan_mbox_data {DECLARE_FLEX_ARRAY (u8, d); }; -static int airoha_npu_send_msg (struct airoha_npu *npu,int func_id, -void *p,int size) +static int __airoha_npu_send_msg (struct airoha_npu *npu,int func_id, +const void *data,int len,void *reply, +int reply_len) { -u16 core =0 ;/* FIXME */ -u32 val, offset = core <<4 ; -dma_addr_t dma_addr; +struct airoha_npu_core *c = &npu->cores[0 ];/* FIXME: core */ +u32 val, offset =0 ;int ret; - dma_addr =dma_map_single (npu->dev, p, size,DMA_BIDIRECTIONAL ); - ret =dma_mapping_error (npu->dev, dma_addr); -if (ret) -return ret; +if (len >AIROHA_NPU_MBOX_SIZE || + (reply && (reply_len <0 || reply_len > len))) +return -EINVAL ; -spin_lock_bh (&npu->cores[core].lock); +/* +* Mailbox payloads are bidirectional (CPU request, NPU response). +* On EN7581+MT7996, streaming DMA_BIDIRECTIONAL against the +* caller kzalloc() buffer can leave WLAN_FUNC_GET_WAIT_NPU_VERSION +* reading as 0.0 despite MBOX success. Reuse a probe-time coherent +* bounce buffer under the per-core lock (also used from PPE +* foe_commit under atomic context). +*/ +spin_lock_bh (&c->lock); + +memcpy (c->buf, data, len); -regmap_write (npu->regmap,REG_CR_MBQ0_CTRL (0 ) + offset, dma_addr); -regmap_write (npu->regmap,REG_CR_MBQ0_CTRL (1 ) + offset, size); +regmap_write (npu->regmap,REG_CR_MBQ0_CTRL (0 ) + offset, c->addr); +regmap_write (npu->regmap,REG_CR_MBQ0_CTRL (1 ) + offset, len);regmap_read (npu->regmap,REG_CR_MBQ0_CTRL (2 ) + offset, &val);regmap_write (npu->regmap,REG_CR_MBQ0_CTRL (2 ) + offset, val +1 ); val =FIELD_PREP (MBOX_MSG_FUNC_ID , func_id) |MBOX_MSG_WAIT_RSP ;
@@ -189,13 +204,21 @@ static int airoha_npu_send_msg(struct airoha_npu *npu, int func_id,if (!ret &&FIELD_GET (MBOX_MSG_STATUS , val) !=NPU_MBOX_SUCCESS ) ret = -EINVAL ; -spin_unlock_bh (&npu->cores[core].lock); +/* Copy the trailing reply_len bytes of the response. */ +if (!ret && reply) +memcpy (reply, c->buf + len - reply_len, reply_len); -dma_unmap_single (npu->dev, dma_addr, size,DMA_BIDIRECTIONAL ); +spin_unlock_bh (&c->lock);return ret; } +static inline int airoha_npu_send_msg (struct airoha_npu *npu,int func_id, +const void *data,int len) +{ +return __airoha_npu_send_msg (npu, func_id, data, len,NULL ,0 ); +} +static int airoha_npu_load_firmware (struct device *dev,void __iomem *addr,const char *fw_name,int fw_max_size) {
@@ -442,8 +465,9 @@ static int airoha_npu_ppe_stats_setup(struct airoha_npu *npu, ppe_data->func_id =PPE_FUNC_SET_WAIT_FLOW_STATS_SETUP ; ppe_data->stats_info.foe_stats_addr = foe_stats_addr; - err =airoha_npu_send_msg (npu,NPU_FUNC_PPE , ppe_data, -sizeof (*ppe_data)); + err =__airoha_npu_send_msg (npu,NPU_FUNC_PPE , ppe_data, +sizeof (*ppe_data), ppe_data, +sizeof (*ppe_data));if (err) goto out;
@@ -497,9 +521,8 @@ static int airoha_npu_wlan_msg_get(struct airoha_npu *npu, int ifindex, wlan_data->func_type =NPU_OP_GET ; wlan_data->func_id = func_id; - err =airoha_npu_send_msg (npu,NPU_FUNC_WIFI , wlan_data, len); -if (!err) -memcpy (data, wlan_data->d, data_len); + err =__airoha_npu_send_msg (npu,NPU_FUNC_WIFI , wlan_data, len, + data, data_len);kfree (wlan_data);return err;
@@ -770,6 +793,15 @@ static int airoha_npu_probe(struct platform_device *pdev)if (err)return err; +for (i =0 ; i <ARRAY_SIZE (npu->cores); i++) { +struct airoha_npu_core *core = &npu->cores[i]; + + core->buf =dmam_alloc_coherent (dev,AIROHA_NPU_MBOX_SIZE , + &core->addr,GFP_KERNEL ); +if (!core->buf) +return -ENOMEM ; + } + err =airoha_npu_run_firmware (dev, base, &res);if (err)return dev_err_probe (dev, err,"failed to run npu firmware\n" );
include/linux/soc/airoha/airoha_offload.h +3 -0
@@ -173,6 +173,9 @@ struct airoha_npu {/* protect concurrent npu memory accesses */ spinlock_t lock;struct work_struct wdt_work; +/* Coherent bounce buffer for mailbox cmd/rsp */ +void *buf; +dma_addr_t addr; } cores[NPU_NUM_CORES ];int irqs[NPU_NUM_IRQ ];
Signature · 1 line
2.55.0
Lorenzo Bianconi
maintainerQuoted 67 lines
Commit
6f884eb87a79("net: airoha: Fix DMA direction for NPU mailbox buffer") switchedairoha_npu_send_msg()toDMA_BIDIRECTIONALso non-coherent CPUs invalidate caches before reading NPU GET responses.On EN7581 + MT7996 that change regresses probe: the mailbox completes successfully, but
WLAN_FUNC_GET_WAIT_NPU_VERSIONstill reads as 0.0 and mt76 never binds NPU offload. Healthy boards report 0.1111.
airoha_npu_send_msg()is also used from PPEfoe_commitunderspin_lock_bh(), so per-messagedma_alloc_coherent(GFP_ATOMIC)is a poor fit. Allocate one device-managed coherent bounce buffer per NPU core at probe and reuse it under the existing per-core mailbox lock. That also keeps the buffer valid if the NPU completes a write after a mailbox timeout.Introduce
__airoha_npu_send_msg()with an optional reply pointer so GET callers can copy the trailing response payload once. Keepairoha_npu_send_msg()as a thin wrapper for SET paths.Verified on Quantum Fiber / Gemtek W1700K (EN7581 + MT7996).
Fixes 6f884eb87a79 ("net: airoha: Fix DMA direction for NPU mailbox buffer")Assisted-by Cursor:composer-2Signed-off-by Daniel Pawlik <[email protected]>---
v4:
- droprsp_off; copy trailingreply_lenbytes into reply pointer
- split__airoha_npu_send_msg()+ thinairoha_npu_send_msg()wrapper so SET callers stay unchanged
- fix reverse-christmas-tree local variable ordering
- constify data argument; clarify reply copy commentv3:
- move coherent mailbox buffer into structairoha_npu_core(buf/addr)
- allocate one buffer per core at probe
- add optional reply buffer to avoid the extra copy inairoha_npu_wlan_msg_get()
- documentAIROHA_NPU_MBOX_SIZEvsppe_mbox_data/ WLAN payloadsv2:
- allocate one coherent mailbox buffer at probe and reuse it under the per-core lock instead of per-messagedma_alloc_coherent(GFP_ATOMIC)drivers/net/ethernet/airoha/airoha_npu.c | 70 +++++++++++++++++------ include/linux/soc/airoha/airoha_offload.h | 3 + 2 files changed, 54 insertions(+), 19 deletions(-)drivers/net/ethernet/airoha/airoha_npu.c +6
-0@@ -23,6 +23,12 @@#define NPU_EN7581_FIRMWARE_RV32_MAX_SIZE 0x200000#define NPU_EN7581_FIRMWARE_DATA_MAX_SIZE 0x10000#define NPU_DUMP_SIZE 512 +/* +* Mailbox DMA payload size. Covers sizeof(struct ppe_mbox_data) (28) and +* WLAN TLV messages (header + payload); largest in-tree WLAN payload today +* is 16 bytes (INODE_TXRX_REG_ADDR). Keep headroom for future commands. +*/ +#define AIROHA_NPU_MBOX_SIZE 256#define REG_NPU_LOCAL_SRAM 0x0
drivers/net/ethernet/airoha/airoha_npu.c struct wlan_mbox_data { · line 171
Quoted 8 lines
drivers/net/ethernet/airoha/airoha_npu.c +3
-2@@ -160,23 +166,32 @@ struct wlan_mbox_data {DECLARE_FLEX_ARRAY (u8, d); }; -static int airoha_npu_send_msg (struct airoha_npu *npu,int func_id, -void *p,int size) +static int __airoha_npu_send_msg (struct airoha_npu *npu,int func_id, +const void *data,int len,void *reply, +int reply_len)
if you use an u16 for reply_len you can avoid the reply_len < 0 check below.
Quoted 4 lines
drivers/net/ethernet/airoha/airoha_npu.c +0
-3{ -u16 core =0 ;/* FIXME */ -u32 val, offset = core <<4 ; -dma_addr_t dma_addr;
drivers/net/ethernet/airoha/airoha_npu.c struct wlan_mbox_data { · line 178
Quoted 10 lines
drivers/net/ethernet/airoha/airoha_npu.c +4
-4+struct airoha_npu_core *c = &npu->cores[0 ];/* FIXME: core */ +u32 val, offset =0 ;int ret; - dma_addr =dma_map_single (npu->dev, p, size,DMA_BIDIRECTIONAL ); - ret =dma_mapping_error (npu->dev, dma_addr); -if (ret) -return ret; +if (len >AIROHA_NPU_MBOX_SIZE || + (reply && (reply_len <0 || reply_len > len)))
maybe you can have a dedicated if for this check
if (reply && reply_len > len) reutrn -EINVAL;
Quoted 30 lines
drivers/net/ethernet/airoha/airoha_npu.c +17
-4+return -EINVAL ; -spin_lock_bh (&npu->cores[core].lock); +/* +* Mailbox payloads are bidirectional (CPU request, NPU response). +* On EN7581+MT7996, streaming DMA_BIDIRECTIONAL against the +* caller kzalloc() buffer can leave WLAN_FUNC_GET_WAIT_NPU_VERSION +* reading as 0.0 despite MBOX success. Reuse a probe-time coherent +* bounce buffer under the per-core lock (also used from PPE +* foe_commit under atomic context). +*/ +spin_lock_bh (&c->lock); + +memcpy (c->buf, data, len); -regmap_write (npu->regmap,REG_CR_MBQ0_CTRL (0 ) + offset, dma_addr); -regmap_write (npu->regmap,REG_CR_MBQ0_CTRL (1 ) + offset, size); +regmap_write (npu->regmap,REG_CR_MBQ0_CTRL (0 ) + offset, c->addr); +regmap_write (npu->regmap,REG_CR_MBQ0_CTRL (1 ) + offset, len);regmap_read (npu->regmap,REG_CR_MBQ0_CTRL (2 ) + offset, &val);regmap_write (npu->regmap,REG_CR_MBQ0_CTRL (2 ) + offset, val +1 ); val =FIELD_PREP (MBOX_MSG_FUNC_ID , func_id) |MBOX_MSG_WAIT_RSP ;@@ -189,13 +204,21 @@ static int airoha_npu_send_msg(struct airoha_npu *npu, int func_id,if (!ret &&FIELD_GET (MBOX_MSG_STATUS , val) !=NPU_MBOX_SUCCESS ) ret = -EINVAL ; -spin_unlock_bh (&npu->cores[core].lock); +/* Copy the trailing reply_len bytes of the response. */ +if (!ret && reply) +memcpy (reply, c->buf + len - reply_len, reply_len);
drivers/net/ethernet/airoha/airoha_npu.c static int airoha_npu_send_msg(struct airoha_npu *npu, int func_id, · line 218
Quoted 10 lines
drivers/net/ethernet/airoha/airoha_npu.c +4
-1-dma_unmap_single (npu->dev, dma_addr, size,DMA_BIDIRECTIONAL ); +spin_unlock_bh (&c->lock);return ret; } +static inline int airoha_npu_send_msg (struct airoha_npu *npu,int func_id, +const void *data,int len) +{
if you put in .c file please drop the inline.
Quoted 7 lines
drivers/net/ethernet/airoha/airoha_npu.c +3
-0+return __airoha_npu_send_msg (npu, func_id, data, len,NULL ,0 ); +} +static int airoha_npu_load_firmware (struct device *dev,void __iomem *addr,const char *fw_name,int fw_max_size) {
drivers/net/ethernet/airoha/airoha_npu.c static int airoha_npu_ppe_stats_setup(struct airoha_npu *npu, · line 470
Quoted 8 lines
drivers/net/ethernet/airoha/airoha_npu.c +3
-2@@ -442,8 +465,9 @@ static int airoha_npu_ppe_stats_setup(struct airoha_npu *npu, ppe_data->func_id =PPE_FUNC_SET_WAIT_FLOW_STATS_SETUP ; ppe_data->stats_info.foe_stats_addr = foe_stats_addr; - err =airoha_npu_send_msg (npu,NPU_FUNC_PPE , ppe_data, -sizeof (*ppe_data)); + err =__airoha_npu_send_msg (npu,NPU_FUNC_PPE , ppe_data, +sizeof (*ppe_data), ppe_data, +sizeof (*ppe_data));
why not using airoha_npu_send_msg() here?
Regards,
Lorenzo
Quoted 48 lines
drivers/net/ethernet/airoha/airoha_npu.c +11
-3if (err) goto out;@@ -497,9 +521,8 @@ static int airoha_npu_wlan_msg_get(struct airoha_npu *npu, int ifindex, wlan_data->func_type =NPU_OP_GET ; wlan_data->func_id = func_id; - err =airoha_npu_send_msg (npu,NPU_FUNC_WIFI , wlan_data, len); -if (!err) -memcpy (data, wlan_data->d, data_len); + err =__airoha_npu_send_msg (npu,NPU_FUNC_WIFI , wlan_data, len, + data, data_len);kfree (wlan_data);return err;@@ -770,6 +793,15 @@ static int airoha_npu_probe(struct platform_device *pdev)if (err)return err; +for (i =0 ; i <ARRAY_SIZE (npu->cores); i++) { +struct airoha_npu_core *core = &npu->cores[i]; + + core->buf =dmam_alloc_coherent (dev,AIROHA_NPU_MBOX_SIZE , + &core->addr,GFP_KERNEL ); +if (!core->buf) +return -ENOMEM ; + } + err =airoha_npu_run_firmware (dev, base, &res);if (err)return dev_err_probe (dev, err,"failed to run npu firmware\n" );include/linux/soc/airoha/airoha_offload.h +3
-0@@ -173,6 +173,9 @@ struct airoha_npu {/* protect concurrent npu memory accesses */ spinlock_t lock;struct work_struct wdt_work; +/* Coherent bounce buffer for mailbox cmd/rsp */ +void *buf; +dma_addr_t addr; } cores[NPU_NUM_CORES ];int irqs[NPU_NUM_IRQ ];Signature · 1 line
2.55.0