1
2
3
4
5
6
7#include <linux/types.h>
8#include <linux/device.h>
9#include <linux/slab.h>
10#include <linux/bitfield.h>
11#include <linux/dma-direction.h>
12
13#include "gsi.h"
14#include "gsi_trans.h"
15#include "ipa.h"
16#include "ipa_endpoint.h"
17#include "ipa_table.h"
18#include "ipa_cmd.h"
19#include "ipa_mem.h"
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40enum pipeline_clear_options {
41 pipeline_clear_hps = 0,
42 pipeline_clear_src_grp = 1,
43 pipeline_clear_full = 2,
44};
45
46
47
48struct ipa_cmd_hw_ip_fltrt_init {
49 __le64 hash_rules_addr;
50 __le64 flags;
51 __le64 nhash_rules_addr;
52};
53
54
55#define IP_FLTRT_FLAGS_HASH_SIZE_FMASK GENMASK_ULL(11, 0)
56#define IP_FLTRT_FLAGS_HASH_ADDR_FMASK GENMASK_ULL(27, 12)
57#define IP_FLTRT_FLAGS_NHASH_SIZE_FMASK GENMASK_ULL(39, 28)
58#define IP_FLTRT_FLAGS_NHASH_ADDR_FMASK GENMASK_ULL(55, 40)
59
60
61
62struct ipa_cmd_hw_hdr_init_local {
63 __le64 hdr_table_addr;
64 __le32 flags;
65 __le32 reserved;
66};
67
68
69#define HDR_INIT_LOCAL_FLAGS_TABLE_SIZE_FMASK GENMASK(11, 0)
70#define HDR_INIT_LOCAL_FLAGS_HDR_ADDR_FMASK GENMASK(27, 12)
71
72
73
74
75
76#define REGISTER_WRITE_OPCODE_SKIP_CLEAR_FMASK GENMASK(8, 8)
77#define REGISTER_WRITE_OPCODE_CLEAR_OPTION_FMASK GENMASK(10, 9)
78
79struct ipa_cmd_register_write {
80 __le16 flags;
81 __le16 offset;
82 __le32 value;
83 __le32 value_mask;
84 __le32 clear_options;
85};
86
87
88
89#define REGISTER_WRITE_FLAGS_OFFSET_HIGH_FMASK GENMASK(14, 11)
90
91#define REGISTER_WRITE_FLAGS_SKIP_CLEAR_FMASK GENMASK(15, 15)
92
93
94#define REGISTER_WRITE_CLEAR_OPTIONS_FMASK GENMASK(1, 0)
95
96
97
98struct ipa_cmd_ip_packet_init {
99 u8 dest_endpoint;
100 u8 reserved[7];
101};
102
103
104#define IPA_PACKET_INIT_DEST_ENDPOINT_FMASK GENMASK(4, 0)
105
106
107
108
109
110#define DMA_SHARED_MEM_OPCODE_SKIP_CLEAR_FMASK GENMASK(8, 8)
111#define DMA_SHARED_MEM_OPCODE_CLEAR_OPTION_FMASK GENMASK(10, 9)
112
113struct ipa_cmd_hw_dma_mem_mem {
114 __le16 clear_after_read;
115 __le16 size;
116 __le16 local_addr;
117 __le16 flags;
118 __le64 system_addr;
119};
120
121
122#define DMA_SHARED_MEM_CLEAR_AFTER_READ GENMASK(15, 15)
123
124
125#define DMA_SHARED_MEM_FLAGS_DIRECTION_FMASK GENMASK(0, 0)
126
127#define DMA_SHARED_MEM_FLAGS_SKIP_CLEAR_FMASK GENMASK(1, 1)
128#define DMA_SHARED_MEM_FLAGS_CLEAR_OPTIONS_FMASK GENMASK(3, 2)
129
130
131
132struct ipa_cmd_ip_packet_tag_status {
133 __le64 tag;
134};
135
136#define IP_PACKET_TAG_STATUS_TAG_FMASK GENMASK_ULL(63, 16)
137
138
139union ipa_cmd_payload {
140 struct ipa_cmd_hw_ip_fltrt_init table_init;
141 struct ipa_cmd_hw_hdr_init_local hdr_init_local;
142 struct ipa_cmd_register_write register_write;
143 struct ipa_cmd_ip_packet_init ip_packet_init;
144 struct ipa_cmd_hw_dma_mem_mem dma_shared_mem;
145 struct ipa_cmd_ip_packet_tag_status ip_packet_tag_status;
146};
147
148static void ipa_cmd_validate_build(void)
149{
150
151
152
153
154
155
156
157#define TABLE_SIZE (TABLE_COUNT_MAX * IPA_TABLE_ENTRY_SIZE)
158#define TABLE_COUNT_MAX max_t(u32, IPA_ROUTE_COUNT_MAX, IPA_FILTER_COUNT_MAX)
159 BUILD_BUG_ON(TABLE_SIZE > field_max(IP_FLTRT_FLAGS_HASH_SIZE_FMASK));
160 BUILD_BUG_ON(TABLE_SIZE > field_max(IP_FLTRT_FLAGS_NHASH_SIZE_FMASK));
161#undef TABLE_COUNT_MAX
162#undef TABLE_SIZE
163}
164
165#ifdef IPA_VALIDATE
166
167
168bool ipa_cmd_table_valid(struct ipa *ipa, const struct ipa_mem *mem,
169 bool route, bool ipv6, bool hashed)
170{
171 struct device *dev = &ipa->pdev->dev;
172 u32 offset_max;
173
174 offset_max = hashed ? field_max(IP_FLTRT_FLAGS_HASH_ADDR_FMASK)
175 : field_max(IP_FLTRT_FLAGS_NHASH_ADDR_FMASK);
176 if (mem->offset > offset_max ||
177 ipa->mem_offset > offset_max - mem->offset) {
178 dev_err(dev, "IPv%c %s%s table region offset too large "
179 "(0x%04x + 0x%04x > 0x%04x)\n",
180 ipv6 ? '6' : '4', hashed ? "hashed " : "",
181 route ? "route" : "filter",
182 ipa->mem_offset, mem->offset, offset_max);
183 return false;
184 }
185
186 if (mem->offset > ipa->mem_size ||
187 mem->size > ipa->mem_size - mem->offset) {
188 dev_err(dev, "IPv%c %s%s table region out of range "
189 "(0x%04x + 0x%04x > 0x%04x)\n",
190 ipv6 ? '6' : '4', hashed ? "hashed " : "",
191 route ? "route" : "filter",
192 mem->offset, mem->size, ipa->mem_size);
193 return false;
194 }
195
196 return true;
197}
198
199
200static bool ipa_cmd_header_valid(struct ipa *ipa)
201{
202 const struct ipa_mem *mem = &ipa->mem[IPA_MEM_MODEM_HEADER];
203 struct device *dev = &ipa->pdev->dev;
204 u32 offset_max;
205 u32 size_max;
206 u32 size;
207
208 offset_max = field_max(HDR_INIT_LOCAL_FLAGS_HDR_ADDR_FMASK);
209 if (mem->offset > offset_max ||
210 ipa->mem_offset > offset_max - mem->offset) {
211 dev_err(dev, "header table region offset too large "
212 "(0x%04x + 0x%04x > 0x%04x)\n",
213 ipa->mem_offset + mem->offset, offset_max);
214 return false;
215 }
216
217 size_max = field_max(HDR_INIT_LOCAL_FLAGS_TABLE_SIZE_FMASK);
218 size = ipa->mem[IPA_MEM_MODEM_HEADER].size;
219 size += ipa->mem[IPA_MEM_AP_HEADER].size;
220 if (mem->offset > ipa->mem_size || size > ipa->mem_size - mem->offset) {
221 dev_err(dev, "header table region out of range "
222 "(0x%04x + 0x%04x > 0x%04x)\n",
223 mem->offset, size, ipa->mem_size);
224 return false;
225 }
226
227 return true;
228}
229
230
231static bool ipa_cmd_register_write_offset_valid(struct ipa *ipa,
232 const char *name, u32 offset)
233{
234 struct ipa_cmd_register_write *payload;
235 struct device *dev = &ipa->pdev->dev;
236 u32 offset_max;
237 u32 bit_count;
238
239
240
241
242
243 bit_count = BITS_PER_BYTE * sizeof(payload->offset);
244 if (ipa->version != IPA_VERSION_3_5_1)
245 bit_count += hweight32(REGISTER_WRITE_FLAGS_OFFSET_HIGH_FMASK);
246 BUILD_BUG_ON(bit_count > 32);
247 offset_max = ~0 >> (32 - bit_count);
248
249 if (offset > offset_max || ipa->mem_offset > offset_max - offset) {
250 dev_err(dev, "%s offset too large 0x%04x + 0x%04x > 0x%04x)\n",
251 ipa->mem_offset + offset, offset_max);
252 return false;
253 }
254
255 return true;
256}
257
258
259static bool ipa_cmd_register_write_valid(struct ipa *ipa)
260{
261 const char *name;
262 u32 offset;
263
264 offset = ipa_reg_filt_rout_hash_flush_offset(ipa->version);
265 name = "filter/route hash flush";
266 if (!ipa_cmd_register_write_offset_valid(ipa, name, offset))
267 return false;
268
269 offset = IPA_REG_ENDP_STATUS_N_OFFSET(IPA_ENDPOINT_COUNT);
270 name = "maximal endpoint status";
271 if (!ipa_cmd_register_write_offset_valid(ipa, name, offset))
272 return false;
273
274 return true;
275}
276
277bool ipa_cmd_data_valid(struct ipa *ipa)
278{
279 if (!ipa_cmd_header_valid(ipa))
280 return false;
281
282 if (!ipa_cmd_register_write_valid(ipa))
283 return false;
284
285 return true;
286}
287
288#endif
289
290int ipa_cmd_pool_init(struct gsi_channel *channel, u32 tre_max)
291{
292 struct gsi_trans_info *trans_info = &channel->trans_info;
293 struct device *dev = channel->gsi->dev;
294 int ret;
295
296
297 ipa_cmd_validate_build();
298
299
300
301
302
303 ret = gsi_trans_pool_init_dma(dev, &trans_info->cmd_pool,
304 sizeof(union ipa_cmd_payload),
305 tre_max, channel->tlv_count);
306 if (ret)
307 return ret;
308
309
310 ret = gsi_trans_pool_init(&trans_info->info_pool,
311 sizeof(struct ipa_cmd_info),
312 tre_max, channel->tlv_count);
313 if (ret)
314 gsi_trans_pool_exit_dma(dev, &trans_info->cmd_pool);
315
316 return ret;
317}
318
319void ipa_cmd_pool_exit(struct gsi_channel *channel)
320{
321 struct gsi_trans_info *trans_info = &channel->trans_info;
322 struct device *dev = channel->gsi->dev;
323
324 gsi_trans_pool_exit(&trans_info->info_pool);
325 gsi_trans_pool_exit_dma(dev, &trans_info->cmd_pool);
326}
327
328static union ipa_cmd_payload *
329ipa_cmd_payload_alloc(struct ipa *ipa, dma_addr_t *addr)
330{
331 struct gsi_trans_info *trans_info;
332 struct ipa_endpoint *endpoint;
333
334 endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX];
335 trans_info = &ipa->gsi.channel[endpoint->channel_id].trans_info;
336
337 return gsi_trans_pool_alloc_dma(&trans_info->cmd_pool, addr);
338}
339
340
341void ipa_cmd_table_init_add(struct gsi_trans *trans,
342 enum ipa_cmd_opcode opcode, u16 size, u32 offset,
343 dma_addr_t addr, u16 hash_size, u32 hash_offset,
344 dma_addr_t hash_addr)
345{
346 struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
347 enum dma_data_direction direction = DMA_TO_DEVICE;
348 struct ipa_cmd_hw_ip_fltrt_init *payload;
349 union ipa_cmd_payload *cmd_payload;
350 dma_addr_t payload_addr;
351 u64 val;
352
353
354 offset += ipa->mem_offset;
355 val = u64_encode_bits(offset, IP_FLTRT_FLAGS_NHASH_ADDR_FMASK);
356 val |= u64_encode_bits(size, IP_FLTRT_FLAGS_NHASH_SIZE_FMASK);
357
358
359 if (hash_size) {
360
361 hash_offset += ipa->mem_offset;
362 val |= u64_encode_bits(hash_offset,
363 IP_FLTRT_FLAGS_HASH_ADDR_FMASK);
364 val |= u64_encode_bits(hash_size,
365 IP_FLTRT_FLAGS_HASH_SIZE_FMASK);
366 }
367
368 cmd_payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
369 payload = &cmd_payload->table_init;
370
371
372 if (hash_size)
373 payload->hash_rules_addr = cpu_to_le64(hash_addr);
374 payload->flags = cpu_to_le64(val);
375 payload->nhash_rules_addr = cpu_to_le64(addr);
376
377 gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
378 direction, opcode);
379}
380
381
382void ipa_cmd_hdr_init_local_add(struct gsi_trans *trans, u32 offset, u16 size,
383 dma_addr_t addr)
384{
385 struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
386 enum ipa_cmd_opcode opcode = IPA_CMD_HDR_INIT_LOCAL;
387 enum dma_data_direction direction = DMA_TO_DEVICE;
388 struct ipa_cmd_hw_hdr_init_local *payload;
389 union ipa_cmd_payload *cmd_payload;
390 dma_addr_t payload_addr;
391 u32 flags;
392
393 offset += ipa->mem_offset;
394
395
396
397
398
399
400 cmd_payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
401 payload = &cmd_payload->hdr_init_local;
402
403 payload->hdr_table_addr = cpu_to_le64(addr);
404 flags = u32_encode_bits(size, HDR_INIT_LOCAL_FLAGS_TABLE_SIZE_FMASK);
405 flags |= u32_encode_bits(offset, HDR_INIT_LOCAL_FLAGS_HDR_ADDR_FMASK);
406 payload->flags = cpu_to_le32(flags);
407
408 gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
409 direction, opcode);
410}
411
412void ipa_cmd_register_write_add(struct gsi_trans *trans, u32 offset, u32 value,
413 u32 mask, bool clear_full)
414{
415 struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
416 struct ipa_cmd_register_write *payload;
417 union ipa_cmd_payload *cmd_payload;
418 u32 opcode = IPA_CMD_REGISTER_WRITE;
419 dma_addr_t payload_addr;
420 u32 clear_option;
421 u32 options;
422 u16 flags;
423
424
425 clear_option = clear_full ? pipeline_clear_full : pipeline_clear_hps;
426
427 if (ipa->version != IPA_VERSION_3_5_1) {
428 u16 offset_high;
429 u32 val;
430
431
432
433 val = u16_encode_bits(clear_option,
434 REGISTER_WRITE_OPCODE_CLEAR_OPTION_FMASK);
435 opcode |= val;
436
437
438 offset_high = (u16)u32_get_bits(offset, GENMASK(19, 16));
439 offset &= (1 << 16) - 1;
440
441
442 flags = u16_encode_bits(offset_high,
443 REGISTER_WRITE_FLAGS_OFFSET_HIGH_FMASK);
444 options = 0;
445
446 } else {
447 flags = 0;
448 options = u16_encode_bits(clear_option,
449 REGISTER_WRITE_CLEAR_OPTIONS_FMASK);
450 }
451
452 cmd_payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
453 payload = &cmd_payload->register_write;
454
455 payload->flags = cpu_to_le16(flags);
456 payload->offset = cpu_to_le16((u16)offset);
457 payload->value = cpu_to_le32(value);
458 payload->value_mask = cpu_to_le32(mask);
459 payload->clear_options = cpu_to_le32(options);
460
461 gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
462 DMA_NONE, opcode);
463}
464
465
466static void ipa_cmd_ip_packet_init_add(struct gsi_trans *trans, u8 endpoint_id)
467{
468 struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
469 enum ipa_cmd_opcode opcode = IPA_CMD_IP_PACKET_INIT;
470 enum dma_data_direction direction = DMA_TO_DEVICE;
471 struct ipa_cmd_ip_packet_init *payload;
472 union ipa_cmd_payload *cmd_payload;
473 dma_addr_t payload_addr;
474
475
476
477
478 cmd_payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
479 payload = &cmd_payload->ip_packet_init;
480
481 payload->dest_endpoint = u8_encode_bits(endpoint_id,
482 IPA_PACKET_INIT_DEST_ENDPOINT_FMASK);
483
484 gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
485 direction, opcode);
486}
487
488
489void ipa_cmd_dma_shared_mem_add(struct gsi_trans *trans, u32 offset, u16 size,
490 dma_addr_t addr, bool toward_ipa)
491{
492 struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
493 enum ipa_cmd_opcode opcode = IPA_CMD_DMA_SHARED_MEM;
494 struct ipa_cmd_hw_dma_mem_mem *payload;
495 union ipa_cmd_payload *cmd_payload;
496 enum dma_data_direction direction;
497 dma_addr_t payload_addr;
498 u16 flags;
499
500
501
502
503
504 offset += ipa->mem_offset;
505
506 cmd_payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
507 payload = &cmd_payload->dma_shared_mem;
508
509
510
511
512 payload->size = cpu_to_le16(size);
513 payload->local_addr = cpu_to_le16(offset);
514
515
516
517
518
519
520
521
522 flags = toward_ipa ? 0 : DMA_SHARED_MEM_FLAGS_DIRECTION_FMASK;
523 payload->flags = cpu_to_le16(flags);
524 payload->system_addr = cpu_to_le64(addr);
525
526 direction = toward_ipa ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
527
528 gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
529 direction, opcode);
530}
531
532static void ipa_cmd_ip_tag_status_add(struct gsi_trans *trans, u64 tag)
533{
534 struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
535 enum ipa_cmd_opcode opcode = IPA_CMD_IP_PACKET_TAG_STATUS;
536 enum dma_data_direction direction = DMA_TO_DEVICE;
537 struct ipa_cmd_ip_packet_tag_status *payload;
538 union ipa_cmd_payload *cmd_payload;
539 dma_addr_t payload_addr;
540
541
542
543 cmd_payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
544 payload = &cmd_payload->ip_packet_tag_status;
545
546 payload->tag = u64_encode_bits(tag, IP_PACKET_TAG_STATUS_TAG_FMASK);
547
548 gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
549 direction, opcode);
550}
551
552
553static void ipa_cmd_transfer_add(struct gsi_trans *trans, u16 size)
554{
555 struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
556 enum dma_data_direction direction = DMA_TO_DEVICE;
557 enum ipa_cmd_opcode opcode = IPA_CMD_NONE;
558 union ipa_cmd_payload *payload;
559 dma_addr_t payload_addr;
560
561
562
563
564 payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
565
566 gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
567 direction, opcode);
568}
569
570void ipa_cmd_tag_process_add(struct gsi_trans *trans)
571{
572 struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
573 struct ipa_endpoint *endpoint;
574
575 endpoint = ipa->name_map[IPA_ENDPOINT_AP_LAN_RX];
576
577 ipa_cmd_register_write_add(trans, 0, 0, 0, true);
578 ipa_cmd_ip_packet_init_add(trans, endpoint->endpoint_id);
579 ipa_cmd_ip_tag_status_add(trans, 0xcba987654321);
580 ipa_cmd_transfer_add(trans, 4);
581}
582
583
584u32 ipa_cmd_tag_process_count(void)
585{
586 return 4;
587}
588
589void ipa_cmd_tag_process(struct ipa *ipa)
590{
591 u32 count = ipa_cmd_tag_process_count();
592 struct gsi_trans *trans;
593
594 trans = ipa_cmd_trans_alloc(ipa, count);
595 if (trans) {
596 ipa_cmd_tag_process_add(trans);
597 gsi_trans_commit_wait(trans);
598 } else {
599 dev_err(&ipa->pdev->dev,
600 "error allocating %u entry tag transaction\n", count);
601 }
602}
603
604static struct ipa_cmd_info *
605ipa_cmd_info_alloc(struct ipa_endpoint *endpoint, u32 tre_count)
606{
607 struct gsi_channel *channel;
608
609 channel = &endpoint->ipa->gsi.channel[endpoint->channel_id];
610
611 return gsi_trans_pool_alloc(&channel->trans_info.info_pool, tre_count);
612}
613
614
615struct gsi_trans *ipa_cmd_trans_alloc(struct ipa *ipa, u32 tre_count)
616{
617 struct ipa_endpoint *endpoint;
618 struct gsi_trans *trans;
619
620 endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX];
621
622 trans = gsi_channel_trans_alloc(&ipa->gsi, endpoint->channel_id,
623 tre_count, DMA_NONE);
624 if (trans)
625 trans->info = ipa_cmd_info_alloc(endpoint, tre_count);
626
627 return trans;
628}
629