1731c46edSAlex Elder // SPDX-License-Identifier: GPL-2.0
2731c46edSAlex Elder
3731c46edSAlex Elder /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
407abde54SAlex Elder * Copyright (C) 2019-2023 Linaro Ltd.
5731c46edSAlex Elder */
6731c46edSAlex Elder
7731c46edSAlex Elder #include <linux/types.h>
8731c46edSAlex Elder #include <linux/device.h>
9731c46edSAlex Elder #include <linux/slab.h>
10731c46edSAlex Elder #include <linux/bitfield.h>
11731c46edSAlex Elder #include <linux/dma-direction.h>
12731c46edSAlex Elder
13731c46edSAlex Elder #include "gsi.h"
14731c46edSAlex Elder #include "gsi_trans.h"
15731c46edSAlex Elder #include "ipa.h"
16731c46edSAlex Elder #include "ipa_endpoint.h"
17731c46edSAlex Elder #include "ipa_table.h"
18731c46edSAlex Elder #include "ipa_cmd.h"
19731c46edSAlex Elder #include "ipa_mem.h"
20731c46edSAlex Elder
21731c46edSAlex Elder /**
22731c46edSAlex Elder * DOC: IPA Immediate Commands
23731c46edSAlex Elder *
24731c46edSAlex Elder * The AP command TX endpoint is used to issue immediate commands to the IPA.
25731c46edSAlex Elder * An immediate command is generally used to request the IPA do something
26731c46edSAlex Elder * other than data transfer to another endpoint.
27731c46edSAlex Elder *
28731c46edSAlex Elder * Immediate commands are represented by GSI transactions just like other
29d15180b4SAlex Elder * transfer requests, and use a single GSI TRE. Each immediate command
30d15180b4SAlex Elder * has a well-defined format, having a payload of a known length. This
31d15180b4SAlex Elder * allows the transfer element's length field to be used to hold an
32d15180b4SAlex Elder * immediate command's opcode. The payload for a command resides in AP
33d15180b4SAlex Elder * memory and is described by a single scatterlist entry in its transaction.
34d15180b4SAlex Elder * Commands do not require a transaction completion callback, and are
35ace5dc61SAlex Elder * always issued using gsi_trans_commit_wait().
36731c46edSAlex Elder */
37731c46edSAlex Elder
38731c46edSAlex Elder /* Some commands can wait until indicated pipeline stages are clear */
39731c46edSAlex Elder enum pipeline_clear_options {
408701cb00SAlex Elder pipeline_clear_hps = 0x0,
418701cb00SAlex Elder pipeline_clear_src_grp = 0x1,
428701cb00SAlex Elder pipeline_clear_full = 0x2,
43731c46edSAlex Elder };
44731c46edSAlex Elder
45731c46edSAlex Elder /* IPA_CMD_IP_V{4,6}_{FILTER,ROUTING}_INIT */
46731c46edSAlex Elder
47731c46edSAlex Elder struct ipa_cmd_hw_ip_fltrt_init {
48731c46edSAlex Elder __le64 hash_rules_addr;
49731c46edSAlex Elder __le64 flags;
50731c46edSAlex Elder __le64 nhash_rules_addr;
51731c46edSAlex Elder };
52731c46edSAlex Elder
53731c46edSAlex Elder /* Field masks for ipa_cmd_hw_ip_fltrt_init structure fields */
54731c46edSAlex Elder #define IP_FLTRT_FLAGS_HASH_SIZE_FMASK GENMASK_ULL(11, 0)
55731c46edSAlex Elder #define IP_FLTRT_FLAGS_HASH_ADDR_FMASK GENMASK_ULL(27, 12)
56731c46edSAlex Elder #define IP_FLTRT_FLAGS_NHASH_SIZE_FMASK GENMASK_ULL(39, 28)
57731c46edSAlex Elder #define IP_FLTRT_FLAGS_NHASH_ADDR_FMASK GENMASK_ULL(55, 40)
58731c46edSAlex Elder
59731c46edSAlex Elder /* IPA_CMD_HDR_INIT_LOCAL */
60731c46edSAlex Elder
61731c46edSAlex Elder struct ipa_cmd_hw_hdr_init_local {
62731c46edSAlex Elder __le64 hdr_table_addr;
63731c46edSAlex Elder __le32 flags;
64731c46edSAlex Elder __le32 reserved;
65731c46edSAlex Elder };
66731c46edSAlex Elder
67731c46edSAlex Elder /* Field masks for ipa_cmd_hw_hdr_init_local structure fields */
68731c46edSAlex Elder #define HDR_INIT_LOCAL_FLAGS_TABLE_SIZE_FMASK GENMASK(11, 0)
69731c46edSAlex Elder #define HDR_INIT_LOCAL_FLAGS_HDR_ADDR_FMASK GENMASK(27, 12)
70731c46edSAlex Elder
71731c46edSAlex Elder /* IPA_CMD_REGISTER_WRITE */
72731c46edSAlex Elder
73d7f3087bSAlex Elder /* For IPA v4.0+, the pipeline clear options are encoded in the opcode */
74731c46edSAlex Elder #define REGISTER_WRITE_OPCODE_SKIP_CLEAR_FMASK GENMASK(8, 8)
75731c46edSAlex Elder #define REGISTER_WRITE_OPCODE_CLEAR_OPTION_FMASK GENMASK(10, 9)
76731c46edSAlex Elder
77731c46edSAlex Elder struct ipa_cmd_register_write {
78d7f3087bSAlex Elder __le16 flags; /* Unused/reserved prior to IPA v4.0 */
79731c46edSAlex Elder __le16 offset;
80731c46edSAlex Elder __le32 value;
81731c46edSAlex Elder __le32 value_mask;
82731c46edSAlex Elder __le32 clear_options; /* Unused/reserved for IPA v4.0+ */
83731c46edSAlex Elder };
84731c46edSAlex Elder
85731c46edSAlex Elder /* Field masks for ipa_cmd_register_write structure fields */
86d7f3087bSAlex Elder /* The next field is present for IPA v4.0+ */
87731c46edSAlex Elder #define REGISTER_WRITE_FLAGS_OFFSET_HIGH_FMASK GENMASK(14, 11)
88d7f3087bSAlex Elder /* The next field is not present for IPA v4.0+ */
89731c46edSAlex Elder #define REGISTER_WRITE_FLAGS_SKIP_CLEAR_FMASK GENMASK(15, 15)
90731c46edSAlex Elder
91d7f3087bSAlex Elder /* The next field and its values are not present for IPA v4.0+ */
92731c46edSAlex Elder #define REGISTER_WRITE_CLEAR_OPTIONS_FMASK GENMASK(1, 0)
93731c46edSAlex Elder
94731c46edSAlex Elder /* IPA_CMD_IP_PACKET_INIT */
95731c46edSAlex Elder
96731c46edSAlex Elder struct ipa_cmd_ip_packet_init {
97c84ddc11SAlex Elder u8 dest_endpoint; /* Full 8 bits used for IPA v5.0+ */
98731c46edSAlex Elder u8 reserved[7];
99731c46edSAlex Elder };
100731c46edSAlex Elder
101c84ddc11SAlex Elder /* Field mask for ipa_cmd_ip_packet_init dest_endpoint field (unused v5.0+) */
102731c46edSAlex Elder #define IPA_PACKET_INIT_DEST_ENDPOINT_FMASK GENMASK(4, 0)
103731c46edSAlex Elder
104731c46edSAlex Elder /* IPA_CMD_DMA_SHARED_MEM */
105731c46edSAlex Elder
106731c46edSAlex Elder /* For IPA v4.0+, this opcode gets modified with pipeline clear options */
107731c46edSAlex Elder
108731c46edSAlex Elder #define DMA_SHARED_MEM_OPCODE_SKIP_CLEAR_FMASK GENMASK(8, 8)
109731c46edSAlex Elder #define DMA_SHARED_MEM_OPCODE_CLEAR_OPTION_FMASK GENMASK(10, 9)
110731c46edSAlex Elder
111731c46edSAlex Elder struct ipa_cmd_hw_dma_mem_mem {
112731c46edSAlex Elder __le16 clear_after_read; /* 0 or DMA_SHARED_MEM_CLEAR_AFTER_READ */
113731c46edSAlex Elder __le16 size;
114731c46edSAlex Elder __le16 local_addr;
115731c46edSAlex Elder __le16 flags;
116731c46edSAlex Elder __le64 system_addr;
117731c46edSAlex Elder };
118731c46edSAlex Elder
119731c46edSAlex Elder /* Flag allowing atomic clear of target region after reading data (v4.0+)*/
120731c46edSAlex Elder #define DMA_SHARED_MEM_CLEAR_AFTER_READ GENMASK(15, 15)
121731c46edSAlex Elder
122731c46edSAlex Elder /* Field masks for ipa_cmd_hw_dma_mem_mem structure fields */
123731c46edSAlex Elder #define DMA_SHARED_MEM_FLAGS_DIRECTION_FMASK GENMASK(0, 0)
124d7f3087bSAlex Elder /* The next two fields are not present for IPA v4.0+ */
125731c46edSAlex Elder #define DMA_SHARED_MEM_FLAGS_SKIP_CLEAR_FMASK GENMASK(1, 1)
126731c46edSAlex Elder #define DMA_SHARED_MEM_FLAGS_CLEAR_OPTIONS_FMASK GENMASK(3, 2)
127731c46edSAlex Elder
128731c46edSAlex Elder /* IPA_CMD_IP_PACKET_TAG_STATUS */
129731c46edSAlex Elder
130731c46edSAlex Elder struct ipa_cmd_ip_packet_tag_status {
131731c46edSAlex Elder __le64 tag;
132731c46edSAlex Elder };
133731c46edSAlex Elder
134731c46edSAlex Elder #define IP_PACKET_TAG_STATUS_TAG_FMASK GENMASK_ULL(63, 16)
135731c46edSAlex Elder
136731c46edSAlex Elder /* Immediate command payload */
137731c46edSAlex Elder union ipa_cmd_payload {
138731c46edSAlex Elder struct ipa_cmd_hw_ip_fltrt_init table_init;
139731c46edSAlex Elder struct ipa_cmd_hw_hdr_init_local hdr_init_local;
140731c46edSAlex Elder struct ipa_cmd_register_write register_write;
141731c46edSAlex Elder struct ipa_cmd_ip_packet_init ip_packet_init;
142731c46edSAlex Elder struct ipa_cmd_hw_dma_mem_mem dma_shared_mem;
143731c46edSAlex Elder struct ipa_cmd_ip_packet_tag_status ip_packet_tag_status;
144731c46edSAlex Elder };
145731c46edSAlex Elder
ipa_cmd_validate_build(void)146731c46edSAlex Elder static void ipa_cmd_validate_build(void)
147731c46edSAlex Elder {
1480439e674SAlex Elder /* The size of a filter table needs to fit into fields in the
1490439e674SAlex Elder * ipa_cmd_hw_ip_fltrt_init structure. Although hashed tables
150731c46edSAlex Elder * might not be used, non-hashed and hashed tables have the same
151731c46edSAlex Elder * maximum size. IPv4 and IPv6 filter tables have the same number
152731c46edSAlex Elder * of entries.
153731c46edSAlex Elder */
154f2c1dac0SAlex Elder /* Hashed and non-hashed fields are assumed to be the same size */
155f2c1dac0SAlex Elder BUILD_BUG_ON(field_max(IP_FLTRT_FLAGS_HASH_SIZE_FMASK) !=
156f2c1dac0SAlex Elder field_max(IP_FLTRT_FLAGS_NHASH_SIZE_FMASK));
157f2c1dac0SAlex Elder BUILD_BUG_ON(field_max(IP_FLTRT_FLAGS_HASH_ADDR_FMASK) !=
158f2c1dac0SAlex Elder field_max(IP_FLTRT_FLAGS_NHASH_ADDR_FMASK));
1595bc55884SAlex Elder
16007abde54SAlex Elder /* Prior to IPA v5.0, we supported no more than 32 endpoints,
16107abde54SAlex Elder * and this was reflected in some 5-bit fields that held
16207abde54SAlex Elder * endpoint numbers. Starting with IPA v5.0, the widths of
16307abde54SAlex Elder * these fields were extended to 8 bits, meaning up to 256
16407abde54SAlex Elder * endpoints. If the driver claims to support more than
16507abde54SAlex Elder * that it's an error.
16607abde54SAlex Elder */
16707abde54SAlex Elder BUILD_BUG_ON(IPA_ENDPOINT_MAX - 1 > U8_MAX);
168731c46edSAlex Elder }
169731c46edSAlex Elder
170731c46edSAlex Elder /* Validate a memory region holding a table */
ipa_cmd_table_init_valid(struct ipa * ipa,const struct ipa_mem * mem,bool route)1715444b0eaSAlex Elder bool ipa_cmd_table_init_valid(struct ipa *ipa, const struct ipa_mem *mem,
1725444b0eaSAlex Elder bool route)
173731c46edSAlex Elder {
174f2c1dac0SAlex Elder u32 offset_max = field_max(IP_FLTRT_FLAGS_NHASH_ADDR_FMASK);
175f2c1dac0SAlex Elder u32 size_max = field_max(IP_FLTRT_FLAGS_NHASH_SIZE_FMASK);
176f2c1dac0SAlex Elder const char *table = route ? "route" : "filter";
177731c46edSAlex Elder struct device *dev = &ipa->pdev->dev;
1780439e674SAlex Elder u32 size;
1790439e674SAlex Elder
180f787d848SAlex Elder size = route ? ipa->route_count : ipa->filter_count + 1;
181f787d848SAlex Elder size *= sizeof(__le64);
182731c46edSAlex Elder
183f2c1dac0SAlex Elder /* Size must fit in the immediate command field that holds it */
1840439e674SAlex Elder if (size > size_max) {
185f2c1dac0SAlex Elder dev_err(dev, "%s table region size too large\n", table);
1860439e674SAlex Elder dev_err(dev, " (0x%04x > 0x%04x)\n", size, size_max);
187f2c1dac0SAlex Elder
188f2c1dac0SAlex Elder return false;
189f2c1dac0SAlex Elder }
190f2c1dac0SAlex Elder
191f2c1dac0SAlex Elder /* Offset must fit in the immediate command field that holds it */
192731c46edSAlex Elder if (mem->offset > offset_max ||
193731c46edSAlex Elder ipa->mem_offset > offset_max - mem->offset) {
194f2c1dac0SAlex Elder dev_err(dev, "%s table region offset too large\n", table);
195b4afd4b9SAlex Elder dev_err(dev, " (0x%04x + 0x%04x > 0x%04x)\n",
196731c46edSAlex Elder ipa->mem_offset, mem->offset, offset_max);
197b4afd4b9SAlex Elder
198731c46edSAlex Elder return false;
199731c46edSAlex Elder }
200731c46edSAlex Elder
201731c46edSAlex Elder return true;
202731c46edSAlex Elder }
203731c46edSAlex Elder
204731c46edSAlex Elder /* Validate the memory region that holds headers */
ipa_cmd_header_init_local_valid(struct ipa * ipa)2057fd10a2aSAlex Elder static bool ipa_cmd_header_init_local_valid(struct ipa *ipa)
206731c46edSAlex Elder {
207731c46edSAlex Elder struct device *dev = &ipa->pdev->dev;
208ce05a9f3SAlex Elder const struct ipa_mem *mem;
209731c46edSAlex Elder u32 offset_max;
210731c46edSAlex Elder u32 size_max;
211ce05a9f3SAlex Elder u32 offset;
212731c46edSAlex Elder u32 size;
213731c46edSAlex Elder
214ce05a9f3SAlex Elder /* In ipa_cmd_hdr_init_local_add() we record the offset and size of
215ce05a9f3SAlex Elder * the header table memory area in an immediate command. Make sure
216ce05a9f3SAlex Elder * the offset and size fit in the fields that need to hold them, and
217ce05a9f3SAlex Elder * that the entire range is within the overall IPA memory range.
218b4afd4b9SAlex Elder */
219731c46edSAlex Elder offset_max = field_max(HDR_INIT_LOCAL_FLAGS_HDR_ADDR_FMASK);
220ce05a9f3SAlex Elder size_max = field_max(HDR_INIT_LOCAL_FLAGS_TABLE_SIZE_FMASK);
221ce05a9f3SAlex Elder
222ce05a9f3SAlex Elder /* The header memory area contains both the modem and AP header
223ce05a9f3SAlex Elder * regions. The modem portion defines the address of the region.
224ce05a9f3SAlex Elder */
2255e3bc1e5SAlex Elder mem = ipa_mem_find(ipa, IPA_MEM_MODEM_HEADER);
226ce05a9f3SAlex Elder offset = mem->offset;
227ce05a9f3SAlex Elder size = mem->size;
228ce05a9f3SAlex Elder
229ce05a9f3SAlex Elder /* Make sure the offset fits in the IPA command */
230ce05a9f3SAlex Elder if (offset > offset_max || ipa->mem_offset > offset_max - offset) {
231b4afd4b9SAlex Elder dev_err(dev, "header table region offset too large\n");
232b4afd4b9SAlex Elder dev_err(dev, " (0x%04x + 0x%04x > 0x%04x)\n",
233ce05a9f3SAlex Elder ipa->mem_offset, offset, offset_max);
234b4afd4b9SAlex Elder
235731c46edSAlex Elder return false;
236731c46edSAlex Elder }
237731c46edSAlex Elder
2385e3bc1e5SAlex Elder /* Add the size of the AP portion (if defined) to the combined size */
2395e3bc1e5SAlex Elder mem = ipa_mem_find(ipa, IPA_MEM_AP_HEADER);
2405e3bc1e5SAlex Elder if (mem)
2415e3bc1e5SAlex Elder size += mem->size;
242b4afd4b9SAlex Elder
243ce05a9f3SAlex Elder /* Make sure the combined size fits in the IPA command */
244b4afd4b9SAlex Elder if (size > size_max) {
245b4afd4b9SAlex Elder dev_err(dev, "header table region size too large\n");
246b4afd4b9SAlex Elder dev_err(dev, " (0x%04x > 0x%08x)\n", size, size_max);
247b4afd4b9SAlex Elder
248b4afd4b9SAlex Elder return false;
249b4afd4b9SAlex Elder }
250ce05a9f3SAlex Elder
251731c46edSAlex Elder return true;
252731c46edSAlex Elder }
253731c46edSAlex Elder
254731c46edSAlex Elder /* Indicate whether an offset can be used with a register_write command */
ipa_cmd_register_write_offset_valid(struct ipa * ipa,const char * name,u32 offset)255731c46edSAlex Elder static bool ipa_cmd_register_write_offset_valid(struct ipa *ipa,
256731c46edSAlex Elder const char *name, u32 offset)
257731c46edSAlex Elder {
258731c46edSAlex Elder struct ipa_cmd_register_write *payload;
259731c46edSAlex Elder struct device *dev = &ipa->pdev->dev;
260731c46edSAlex Elder u32 offset_max;
261731c46edSAlex Elder u32 bit_count;
262731c46edSAlex Elder
263731c46edSAlex Elder /* The maximum offset in a register_write immediate command depends
264d7f3087bSAlex Elder * on the version of IPA. A 16 bit offset is always supported,
265d7f3087bSAlex Elder * but starting with IPA v4.0 some additional high-order bits are
266d7f3087bSAlex Elder * allowed.
267731c46edSAlex Elder */
268731c46edSAlex Elder bit_count = BITS_PER_BYTE * sizeof(payload->offset);
269d7f3087bSAlex Elder if (ipa->version >= IPA_VERSION_4_0)
270731c46edSAlex Elder bit_count += hweight32(REGISTER_WRITE_FLAGS_OFFSET_HIGH_FMASK);
271731c46edSAlex Elder BUILD_BUG_ON(bit_count > 32);
2722d65ed76SAlex Elder offset_max = ~0U >> (32 - bit_count);
273731c46edSAlex Elder
2742d65ed76SAlex Elder /* Make sure the offset can be represented by the field(s)
2752d65ed76SAlex Elder * that holds it. Also make sure the offset is not outside
2762d65ed76SAlex Elder * the overall IPA memory range.
2772d65ed76SAlex Elder */
278731c46edSAlex Elder if (offset > offset_max || ipa->mem_offset > offset_max - offset) {
279731c46edSAlex Elder dev_err(dev, "%s offset too large 0x%04x + 0x%04x > 0x%04x)\n",
2802d65ed76SAlex Elder name, ipa->mem_offset, offset, offset_max);
281731c46edSAlex Elder return false;
282731c46edSAlex Elder }
283731c46edSAlex Elder
284731c46edSAlex Elder return true;
285731c46edSAlex Elder }
286731c46edSAlex Elder
287731c46edSAlex Elder /* Check whether offsets passed to register_write are valid */
ipa_cmd_register_write_valid(struct ipa * ipa)288731c46edSAlex Elder static bool ipa_cmd_register_write_valid(struct ipa *ipa)
289731c46edSAlex Elder {
29081772e44SAlex Elder const struct reg *reg;
291731c46edSAlex Elder const char *name;
292731c46edSAlex Elder u32 offset;
293731c46edSAlex Elder
2942d65ed76SAlex Elder /* If hashed tables are supported, ensure the hash flush register
2952d65ed76SAlex Elder * offset will fit in a register write IPA immediate command.
2962d65ed76SAlex Elder */
297a266ad6bSAlex Elder if (ipa_table_hash_support(ipa)) {
2988e7c89d8SAlex Elder if (ipa->version < IPA_VERSION_5_0)
2996a244b75SAlex Elder reg = ipa_reg(ipa, FILT_ROUT_HASH_FLUSH);
3008e7c89d8SAlex Elder else
3018e7c89d8SAlex Elder reg = ipa_reg(ipa, FILT_ROUT_CACHE_FLUSH);
3028e7c89d8SAlex Elder
303*fc4cecf7SAlex Elder offset = reg_offset(reg);
304731c46edSAlex Elder name = "filter/route hash flush";
305731c46edSAlex Elder if (!ipa_cmd_register_write_offset_valid(ipa, name, offset))
306731c46edSAlex Elder return false;
3072d65ed76SAlex Elder }
308731c46edSAlex Elder
3092d65ed76SAlex Elder /* Each endpoint can have a status endpoint associated with it,
3102d65ed76SAlex Elder * and this is recorded in an endpoint register. If the modem
3112d65ed76SAlex Elder * crashes, we reset the status endpoint for all modem endpoints
3122d65ed76SAlex Elder * using a register write IPA immediate command. Make sure the
3132d65ed76SAlex Elder * worst case (highest endpoint number) offset of that endpoint
3142d65ed76SAlex Elder * fits in the register write command field(s) that must hold it.
3152d65ed76SAlex Elder */
3166a244b75SAlex Elder reg = ipa_reg(ipa, ENDP_STATUS);
317*fc4cecf7SAlex Elder offset = reg_n_offset(reg, IPA_ENDPOINT_COUNT - 1);
318731c46edSAlex Elder name = "maximal endpoint status";
319731c46edSAlex Elder if (!ipa_cmd_register_write_offset_valid(ipa, name, offset))
320731c46edSAlex Elder return false;
321731c46edSAlex Elder
322731c46edSAlex Elder return true;
323731c46edSAlex Elder }
324731c46edSAlex Elder
ipa_cmd_pool_init(struct gsi_channel * channel,u32 tre_max)325731c46edSAlex Elder int ipa_cmd_pool_init(struct gsi_channel *channel, u32 tre_max)
326731c46edSAlex Elder {
327731c46edSAlex Elder struct gsi_trans_info *trans_info = &channel->trans_info;
328731c46edSAlex Elder struct device *dev = channel->gsi->dev;
329731c46edSAlex Elder
33088e03057SAlex Elder /* Command payloads are allocated one at a time, but a single
33188e03057SAlex Elder * transaction can require up to the maximum supported by the
33288e03057SAlex Elder * channel; treat them as if they were allocated all at once.
333731c46edSAlex Elder */
3348797972aSAlex Elder return gsi_trans_pool_init_dma(dev, &trans_info->cmd_pool,
335731c46edSAlex Elder sizeof(union ipa_cmd_payload),
33688e03057SAlex Elder tre_max, channel->trans_tre_max);
337731c46edSAlex Elder }
338731c46edSAlex Elder
ipa_cmd_pool_exit(struct gsi_channel * channel)339731c46edSAlex Elder void ipa_cmd_pool_exit(struct gsi_channel *channel)
340731c46edSAlex Elder {
341731c46edSAlex Elder struct gsi_trans_info *trans_info = &channel->trans_info;
342731c46edSAlex Elder struct device *dev = channel->gsi->dev;
343731c46edSAlex Elder
344731c46edSAlex Elder gsi_trans_pool_exit_dma(dev, &trans_info->cmd_pool);
345731c46edSAlex Elder }
346731c46edSAlex Elder
347731c46edSAlex Elder static union ipa_cmd_payload *
ipa_cmd_payload_alloc(struct ipa * ipa,dma_addr_t * addr)348731c46edSAlex Elder ipa_cmd_payload_alloc(struct ipa *ipa, dma_addr_t *addr)
349731c46edSAlex Elder {
350731c46edSAlex Elder struct gsi_trans_info *trans_info;
351731c46edSAlex Elder struct ipa_endpoint *endpoint;
352731c46edSAlex Elder
353731c46edSAlex Elder endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX];
354731c46edSAlex Elder trans_info = &ipa->gsi.channel[endpoint->channel_id].trans_info;
355731c46edSAlex Elder
356731c46edSAlex Elder return gsi_trans_pool_alloc_dma(&trans_info->cmd_pool, addr);
357731c46edSAlex Elder }
358731c46edSAlex Elder
359731c46edSAlex Elder /* If hash_size is 0, hash_offset and hash_addr ignored. */
ipa_cmd_table_init_add(struct gsi_trans * trans,enum ipa_cmd_opcode opcode,u16 size,u32 offset,dma_addr_t addr,u16 hash_size,u32 hash_offset,dma_addr_t hash_addr)360731c46edSAlex Elder void ipa_cmd_table_init_add(struct gsi_trans *trans,
361731c46edSAlex Elder enum ipa_cmd_opcode opcode, u16 size, u32 offset,
362731c46edSAlex Elder dma_addr_t addr, u16 hash_size, u32 hash_offset,
363731c46edSAlex Elder dma_addr_t hash_addr)
364731c46edSAlex Elder {
365731c46edSAlex Elder struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
366731c46edSAlex Elder struct ipa_cmd_hw_ip_fltrt_init *payload;
367731c46edSAlex Elder union ipa_cmd_payload *cmd_payload;
368731c46edSAlex Elder dma_addr_t payload_addr;
369731c46edSAlex Elder u64 val;
370731c46edSAlex Elder
371731c46edSAlex Elder /* Record the non-hash table offset and size */
372731c46edSAlex Elder offset += ipa->mem_offset;
373731c46edSAlex Elder val = u64_encode_bits(offset, IP_FLTRT_FLAGS_NHASH_ADDR_FMASK);
374731c46edSAlex Elder val |= u64_encode_bits(size, IP_FLTRT_FLAGS_NHASH_SIZE_FMASK);
375731c46edSAlex Elder
376731c46edSAlex Elder /* The hash table offset and address are zero if its size is 0 */
377731c46edSAlex Elder if (hash_size) {
378731c46edSAlex Elder /* Record the hash table offset and size */
379731c46edSAlex Elder hash_offset += ipa->mem_offset;
380731c46edSAlex Elder val |= u64_encode_bits(hash_offset,
381731c46edSAlex Elder IP_FLTRT_FLAGS_HASH_ADDR_FMASK);
382731c46edSAlex Elder val |= u64_encode_bits(hash_size,
383731c46edSAlex Elder IP_FLTRT_FLAGS_HASH_SIZE_FMASK);
384731c46edSAlex Elder }
385731c46edSAlex Elder
386731c46edSAlex Elder cmd_payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
387731c46edSAlex Elder payload = &cmd_payload->table_init;
388731c46edSAlex Elder
389731c46edSAlex Elder /* Fill in all offsets and sizes and the non-hash table address */
390731c46edSAlex Elder if (hash_size)
391731c46edSAlex Elder payload->hash_rules_addr = cpu_to_le64(hash_addr);
392731c46edSAlex Elder payload->flags = cpu_to_le64(val);
393731c46edSAlex Elder payload->nhash_rules_addr = cpu_to_le64(addr);
394731c46edSAlex Elder
395731c46edSAlex Elder gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
3964de284b7SAlex Elder opcode);
397731c46edSAlex Elder }
398731c46edSAlex Elder
399731c46edSAlex Elder /* Initialize header space in IPA-local memory */
ipa_cmd_hdr_init_local_add(struct gsi_trans * trans,u32 offset,u16 size,dma_addr_t addr)400731c46edSAlex Elder void ipa_cmd_hdr_init_local_add(struct gsi_trans *trans, u32 offset, u16 size,
401731c46edSAlex Elder dma_addr_t addr)
402731c46edSAlex Elder {
403731c46edSAlex Elder struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
404731c46edSAlex Elder enum ipa_cmd_opcode opcode = IPA_CMD_HDR_INIT_LOCAL;
405731c46edSAlex Elder struct ipa_cmd_hw_hdr_init_local *payload;
406731c46edSAlex Elder union ipa_cmd_payload *cmd_payload;
407731c46edSAlex Elder dma_addr_t payload_addr;
408731c46edSAlex Elder u32 flags;
409731c46edSAlex Elder
410731c46edSAlex Elder offset += ipa->mem_offset;
411731c46edSAlex Elder
412731c46edSAlex Elder /* With this command we tell the IPA where in its local memory the
413731c46edSAlex Elder * header tables reside. The content of the buffer provided is
414731c46edSAlex Elder * also written via DMA into that space. The IPA hardware owns
415731c46edSAlex Elder * the table, but the AP must initialize it.
416731c46edSAlex Elder */
417731c46edSAlex Elder cmd_payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
418731c46edSAlex Elder payload = &cmd_payload->hdr_init_local;
419731c46edSAlex Elder
420731c46edSAlex Elder payload->hdr_table_addr = cpu_to_le64(addr);
421731c46edSAlex Elder flags = u32_encode_bits(size, HDR_INIT_LOCAL_FLAGS_TABLE_SIZE_FMASK);
422731c46edSAlex Elder flags |= u32_encode_bits(offset, HDR_INIT_LOCAL_FLAGS_HDR_ADDR_FMASK);
423731c46edSAlex Elder payload->flags = cpu_to_le32(flags);
424731c46edSAlex Elder
425731c46edSAlex Elder gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
4264de284b7SAlex Elder opcode);
427731c46edSAlex Elder }
428731c46edSAlex Elder
ipa_cmd_register_write_add(struct gsi_trans * trans,u32 offset,u32 value,u32 mask,bool clear_full)429731c46edSAlex Elder void ipa_cmd_register_write_add(struct gsi_trans *trans, u32 offset, u32 value,
430731c46edSAlex Elder u32 mask, bool clear_full)
431731c46edSAlex Elder {
432731c46edSAlex Elder struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
433731c46edSAlex Elder struct ipa_cmd_register_write *payload;
434731c46edSAlex Elder union ipa_cmd_payload *cmd_payload;
435731c46edSAlex Elder u32 opcode = IPA_CMD_REGISTER_WRITE;
436731c46edSAlex Elder dma_addr_t payload_addr;
437731c46edSAlex Elder u32 clear_option;
438731c46edSAlex Elder u32 options;
439731c46edSAlex Elder u16 flags;
440731c46edSAlex Elder
441731c46edSAlex Elder /* pipeline_clear_src_grp is not used */
442731c46edSAlex Elder clear_option = clear_full ? pipeline_clear_full : pipeline_clear_hps;
443731c46edSAlex Elder
444d7f3087bSAlex Elder /* IPA v4.0+ represents the pipeline clear options in the opcode. It
445d7f3087bSAlex Elder * also supports a larger offset by encoding additional high-order
446d7f3087bSAlex Elder * bits in the payload flags field.
447d7f3087bSAlex Elder */
448d7f3087bSAlex Elder if (ipa->version >= IPA_VERSION_4_0) {
449731c46edSAlex Elder u16 offset_high;
450731c46edSAlex Elder u32 val;
451731c46edSAlex Elder
452731c46edSAlex Elder /* Opcode encodes pipeline clear options */
453731c46edSAlex Elder /* SKIP_CLEAR is always 0 (don't skip pipeline clear) */
454731c46edSAlex Elder val = u16_encode_bits(clear_option,
455731c46edSAlex Elder REGISTER_WRITE_OPCODE_CLEAR_OPTION_FMASK);
456731c46edSAlex Elder opcode |= val;
457731c46edSAlex Elder
458731c46edSAlex Elder /* Extract the high 4 bits from the offset */
459731c46edSAlex Elder offset_high = (u16)u32_get_bits(offset, GENMASK(19, 16));
460731c46edSAlex Elder offset &= (1 << 16) - 1;
461731c46edSAlex Elder
462731c46edSAlex Elder /* Extract the top 4 bits and encode it into the flags field */
463731c46edSAlex Elder flags = u16_encode_bits(offset_high,
464731c46edSAlex Elder REGISTER_WRITE_FLAGS_OFFSET_HIGH_FMASK);
465731c46edSAlex Elder options = 0; /* reserved */
466731c46edSAlex Elder
467731c46edSAlex Elder } else {
468731c46edSAlex Elder flags = 0; /* SKIP_CLEAR flag is always 0 */
469731c46edSAlex Elder options = u16_encode_bits(clear_option,
470731c46edSAlex Elder REGISTER_WRITE_CLEAR_OPTIONS_FMASK);
471731c46edSAlex Elder }
472731c46edSAlex Elder
473731c46edSAlex Elder cmd_payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
474731c46edSAlex Elder payload = &cmd_payload->register_write;
475731c46edSAlex Elder
476731c46edSAlex Elder payload->flags = cpu_to_le16(flags);
477731c46edSAlex Elder payload->offset = cpu_to_le16((u16)offset);
478731c46edSAlex Elder payload->value = cpu_to_le32(value);
479731c46edSAlex Elder payload->value_mask = cpu_to_le32(mask);
480731c46edSAlex Elder payload->clear_options = cpu_to_le32(options);
481731c46edSAlex Elder
482731c46edSAlex Elder gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
4834de284b7SAlex Elder opcode);
484731c46edSAlex Elder }
485731c46edSAlex Elder
486731c46edSAlex Elder /* Skip IP packet processing on the next data transfer on a TX channel */
ipa_cmd_ip_packet_init_add(struct gsi_trans * trans,u8 endpoint_id)487731c46edSAlex Elder static void ipa_cmd_ip_packet_init_add(struct gsi_trans *trans, u8 endpoint_id)
488731c46edSAlex Elder {
489731c46edSAlex Elder struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
490731c46edSAlex Elder enum ipa_cmd_opcode opcode = IPA_CMD_IP_PACKET_INIT;
491731c46edSAlex Elder struct ipa_cmd_ip_packet_init *payload;
492731c46edSAlex Elder union ipa_cmd_payload *cmd_payload;
493731c46edSAlex Elder dma_addr_t payload_addr;
494731c46edSAlex Elder
495731c46edSAlex Elder cmd_payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
496731c46edSAlex Elder payload = &cmd_payload->ip_packet_init;
497731c46edSAlex Elder
498c84ddc11SAlex Elder if (ipa->version < IPA_VERSION_5_0) {
499c84ddc11SAlex Elder payload->dest_endpoint =
500c84ddc11SAlex Elder u8_encode_bits(endpoint_id,
501731c46edSAlex Elder IPA_PACKET_INIT_DEST_ENDPOINT_FMASK);
502c84ddc11SAlex Elder } else {
503c84ddc11SAlex Elder payload->dest_endpoint = endpoint_id;
504c84ddc11SAlex Elder }
505731c46edSAlex Elder
506731c46edSAlex Elder gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
5074de284b7SAlex Elder opcode);
508731c46edSAlex Elder }
509731c46edSAlex Elder
510731c46edSAlex Elder /* Use a DMA command to read or write a block of IPA-resident memory */
ipa_cmd_dma_shared_mem_add(struct gsi_trans * trans,u32 offset,u16 size,dma_addr_t addr,bool toward_ipa)511731c46edSAlex Elder void ipa_cmd_dma_shared_mem_add(struct gsi_trans *trans, u32 offset, u16 size,
512731c46edSAlex Elder dma_addr_t addr, bool toward_ipa)
513731c46edSAlex Elder {
514731c46edSAlex Elder struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
515731c46edSAlex Elder enum ipa_cmd_opcode opcode = IPA_CMD_DMA_SHARED_MEM;
516731c46edSAlex Elder struct ipa_cmd_hw_dma_mem_mem *payload;
517731c46edSAlex Elder union ipa_cmd_payload *cmd_payload;
518731c46edSAlex Elder dma_addr_t payload_addr;
519731c46edSAlex Elder u16 flags;
520731c46edSAlex Elder
521731c46edSAlex Elder /* size and offset must fit in 16 bit fields */
5225bc55884SAlex Elder WARN_ON(!size);
5235bc55884SAlex Elder WARN_ON(size > U16_MAX);
5245bc55884SAlex Elder WARN_ON(offset > U16_MAX || ipa->mem_offset > U16_MAX - offset);
525731c46edSAlex Elder
526731c46edSAlex Elder offset += ipa->mem_offset;
527731c46edSAlex Elder
528731c46edSAlex Elder cmd_payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
529731c46edSAlex Elder payload = &cmd_payload->dma_shared_mem;
530731c46edSAlex Elder
531731c46edSAlex Elder /* payload->clear_after_read was reserved prior to IPA v4.0. It's
532731c46edSAlex Elder * never needed for current code, so it's 0 regardless of version.
533731c46edSAlex Elder */
534731c46edSAlex Elder payload->size = cpu_to_le16(size);
535731c46edSAlex Elder payload->local_addr = cpu_to_le16(offset);
536731c46edSAlex Elder /* payload->flags:
537731c46edSAlex Elder * direction: 0 = write to IPA, 1 read from IPA
538731c46edSAlex Elder * Starting at v4.0 these are reserved; either way, all zero:
539731c46edSAlex Elder * pipeline clear: 0 = wait for pipeline clear (don't skip)
540731c46edSAlex Elder * clear_options: 0 = pipeline_clear_hps
541731c46edSAlex Elder * Instead, for v4.0+ these are encoded in the opcode. But again
542731c46edSAlex Elder * since both values are 0 we won't bother OR'ing them in.
543731c46edSAlex Elder */
544731c46edSAlex Elder flags = toward_ipa ? 0 : DMA_SHARED_MEM_FLAGS_DIRECTION_FMASK;
545731c46edSAlex Elder payload->flags = cpu_to_le16(flags);
546731c46edSAlex Elder payload->system_addr = cpu_to_le64(addr);
547731c46edSAlex Elder
548731c46edSAlex Elder gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
5494de284b7SAlex Elder opcode);
550731c46edSAlex Elder }
551731c46edSAlex Elder
ipa_cmd_ip_tag_status_add(struct gsi_trans * trans)552792b75b1SAlex Elder static void ipa_cmd_ip_tag_status_add(struct gsi_trans *trans)
553731c46edSAlex Elder {
554731c46edSAlex Elder struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
555731c46edSAlex Elder enum ipa_cmd_opcode opcode = IPA_CMD_IP_PACKET_TAG_STATUS;
556731c46edSAlex Elder struct ipa_cmd_ip_packet_tag_status *payload;
557731c46edSAlex Elder union ipa_cmd_payload *cmd_payload;
558731c46edSAlex Elder dma_addr_t payload_addr;
559731c46edSAlex Elder
560731c46edSAlex Elder cmd_payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
561731c46edSAlex Elder payload = &cmd_payload->ip_packet_tag_status;
562731c46edSAlex Elder
563792b75b1SAlex Elder payload->tag = le64_encode_bits(0, IP_PACKET_TAG_STATUS_TAG_FMASK);
564731c46edSAlex Elder
565731c46edSAlex Elder gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
5664de284b7SAlex Elder opcode);
567731c46edSAlex Elder }
568731c46edSAlex Elder
569731c46edSAlex Elder /* Issue a small command TX data transfer */
ipa_cmd_transfer_add(struct gsi_trans * trans)570070740d3SAlex Elder static void ipa_cmd_transfer_add(struct gsi_trans *trans)
571731c46edSAlex Elder {
572731c46edSAlex Elder struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
573731c46edSAlex Elder enum ipa_cmd_opcode opcode = IPA_CMD_NONE;
574731c46edSAlex Elder union ipa_cmd_payload *payload;
575731c46edSAlex Elder dma_addr_t payload_addr;
576731c46edSAlex Elder
577731c46edSAlex Elder /* Just transfer a zero-filled payload structure */
578731c46edSAlex Elder payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
579731c46edSAlex Elder
580731c46edSAlex Elder gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
5814de284b7SAlex Elder opcode);
582731c46edSAlex Elder }
583731c46edSAlex Elder
584aa56e3e5SAlex Elder /* Add immediate commands to a transaction to clear the hardware pipeline */
ipa_cmd_pipeline_clear_add(struct gsi_trans * trans)585aa56e3e5SAlex Elder void ipa_cmd_pipeline_clear_add(struct gsi_trans *trans)
586731c46edSAlex Elder {
587731c46edSAlex Elder struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
5882c4bb809SAlex Elder struct ipa_endpoint *endpoint;
589731c46edSAlex Elder
59051c48ce2SAlex Elder /* This will complete when the transfer is received */
59151c48ce2SAlex Elder reinit_completion(&ipa->completion);
59251c48ce2SAlex Elder
593aa56e3e5SAlex Elder /* Issue a no-op register write command (mask 0 means no write) */
5942c4bb809SAlex Elder ipa_cmd_register_write_add(trans, 0, 0, 0, true);
595aa56e3e5SAlex Elder
596aa56e3e5SAlex Elder /* Send a data packet through the IPA pipeline. The packet_init
597aa56e3e5SAlex Elder * command says to send the next packet directly to the exception
598aa56e3e5SAlex Elder * endpoint without any other IPA processing. The tag_status
599aa56e3e5SAlex Elder * command requests that status be generated on completion of
600792b75b1SAlex Elder * that transfer, and that it will be tagged with a value.
601aa56e3e5SAlex Elder * Finally, the transfer command sends a small packet of data
602aa56e3e5SAlex Elder * (instead of a command) using the command endpoint.
603aa56e3e5SAlex Elder */
604aa56e3e5SAlex Elder endpoint = ipa->name_map[IPA_ENDPOINT_AP_LAN_RX];
605731c46edSAlex Elder ipa_cmd_ip_packet_init_add(trans, endpoint->endpoint_id);
606792b75b1SAlex Elder ipa_cmd_ip_tag_status_add(trans);
607070740d3SAlex Elder ipa_cmd_transfer_add(trans);
608731c46edSAlex Elder }
609731c46edSAlex Elder
610aa56e3e5SAlex Elder /* Returns the number of commands required to clear the pipeline */
ipa_cmd_pipeline_clear_count(void)611aa56e3e5SAlex Elder u32 ipa_cmd_pipeline_clear_count(void)
612731c46edSAlex Elder {
613731c46edSAlex Elder return 4;
614731c46edSAlex Elder }
615731c46edSAlex Elder
ipa_cmd_pipeline_clear_wait(struct ipa * ipa)61651c48ce2SAlex Elder void ipa_cmd_pipeline_clear_wait(struct ipa *ipa)
61751c48ce2SAlex Elder {
61851c48ce2SAlex Elder wait_for_completion(&ipa->completion);
61951c48ce2SAlex Elder }
62051c48ce2SAlex Elder
621731c46edSAlex Elder /* Allocate a transaction for the command TX endpoint */
ipa_cmd_trans_alloc(struct ipa * ipa,u32 tre_count)622731c46edSAlex Elder struct gsi_trans *ipa_cmd_trans_alloc(struct ipa *ipa, u32 tre_count)
623731c46edSAlex Elder {
624731c46edSAlex Elder struct ipa_endpoint *endpoint;
6258797972aSAlex Elder
6268797972aSAlex Elder if (WARN_ON(tre_count > IPA_COMMAND_TRANS_TRE_MAX))
6278797972aSAlex Elder return NULL;
628731c46edSAlex Elder
629731c46edSAlex Elder endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX];
630731c46edSAlex Elder
6318797972aSAlex Elder return gsi_channel_trans_alloc(&ipa->gsi, endpoint->channel_id,
632731c46edSAlex Elder tre_count, DMA_NONE);
633731c46edSAlex Elder }
6347fd10a2aSAlex Elder
6357fd10a2aSAlex Elder /* Init function for immediate commands; there is no ipa_cmd_exit() */
ipa_cmd_init(struct ipa * ipa)6367fd10a2aSAlex Elder int ipa_cmd_init(struct ipa *ipa)
6377fd10a2aSAlex Elder {
6387fd10a2aSAlex Elder ipa_cmd_validate_build();
6397fd10a2aSAlex Elder
6407fd10a2aSAlex Elder if (!ipa_cmd_header_init_local_valid(ipa))
6417fd10a2aSAlex Elder return -EINVAL;
6427fd10a2aSAlex Elder
6437fd10a2aSAlex Elder if (!ipa_cmd_register_write_valid(ipa))
6447fd10a2aSAlex Elder return -EINVAL;
6457fd10a2aSAlex Elder
6467fd10a2aSAlex Elder return 0;
6477fd10a2aSAlex Elder }
648