Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2
  3/* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
  4 * Copyright (C) 2018-2023 Linaro Ltd.
  5 */
  6
  7#include <linux/types.h>
  8#include <linux/kernel.h>
  9#include <linux/bits.h>
 10#include <linux/bitops.h>
 11#include <linux/bitfield.h>
 12#include <linux/io.h>
 13#include <linux/build_bug.h>
 14#include <linux/device.h>
 15#include <linux/dma-mapping.h>
 
 
 16
 
 
 17#include "ipa.h"
 18#include "ipa_version.h"
 19#include "ipa_endpoint.h"
 20#include "ipa_table.h"
 21#include "ipa_reg.h"
 22#include "ipa_mem.h"
 23#include "ipa_cmd.h"
 24#include "gsi.h"
 25#include "gsi_trans.h"
 26
 27/**
 28 * DOC: IPA Filter and Route Tables
 29 *
 30 * The IPA has tables defined in its local (IPA-resident) memory that define
 31 * filter and routing rules.  An entry in either of these tables is a little
 32 * endian 64-bit "slot" that holds the address of a rule definition.  (The
 33 * size of these slots is 64 bits regardless of the host DMA address size.)
 34 *
 35 * Separate tables (both filter and route) are used for IPv4 and IPv6.  There
 36 * is normally another set of "hashed" filter and route tables, which are
 37 * used with a hash of message metadata.  Hashed operation is not supported
 38 * by all IPA hardware (IPA v4.2 doesn't support hashed tables).
 39 *
 40 * Rules can be in local memory or in DRAM (system memory).  The offset of
 41 * an object (such as a route or filter table) in IPA-resident memory must
 42 * 128-byte aligned.  An object in system memory (such as a route or filter
 43 * rule) must be at an 8-byte aligned address.  We currently only place
 44 * route or filter rules in system memory.
 45 *
 46 * A rule consists of a contiguous block of 32-bit values terminated with
 47 * 32 zero bits.  A special "zero entry" rule consisting of 64 zero bits
 48 * represents "no filtering" or "no routing," and is the reset value for
 49 * filter or route table rules.
 50 *
 51 * Each filter rule is associated with an AP or modem TX endpoint, though
 52 * not all TX endpoints support filtering.  The first 64-bit slot in a
 53 * filter table is a bitmap indicating which endpoints have entries in
 54 * the table.  Each set bit in this bitmap indicates the presence of the
 55 * address of a filter rule in the memory following the bitmap.  Until IPA
 56 * v5.0,  the low-order bit (bit 0) in this bitmap represents a special
 57 * global filter, which applies to all traffic.  Otherwise the position of
 58 * each set bit represents an endpoint for which a filter rule is defined.
 59 *
 60 * The global rule is not used in current code, and support for it is
 61 * removed starting at IPA v5.0.  For IPA v5.0+, the endpoint bitmap
 62 * position defines the endpoint ID--i.e. if bit 1 is set in the endpoint
 63 * bitmap, endpoint 1 has a filter rule.  Older versions of IPA represent
 64 * the presence of a filter rule for endpoint X by bit (X + 1) being set.
 65 * I.e., bit 1 set indicates the presence of a filter rule for endpoint 0,
 66 * and bit 3 set means there is a filter rule present for endpoint 2.
 67 *
 68 * Each filter table entry has the address of a set of equations that
 69 * implement a filter rule.  So following the endpoint bitmap there
 70 * will be such an address/entry for each endpoint with a set bit in
 71 * the bitmap.
 72 *
 73 * The AP initializes all entries in a filter table to refer to a "zero"
 74 * rule.  Once initialized, the modem and AP update the entries for
 75 * endpoints they "own" directly.  Currently the AP does not use the IPA
 76 * filtering functionality.
 77 *
 78 * This diagram shows an example of a filter table with an endpoint
 79 * bitmap as defined prior to IPA v5.0.
 80 *
 81 *                    IPA Filter Table
 82 *                 ----------------------
 83 * endpoint bitmap | 0x0000000000000048 | Bits 3 and 6 set (endpoints 2 and 5)
 84 *                 |--------------------|
 85 * 1st endpoint    | 0x000123456789abc0 | DMA address for modem endpoint 2 rule
 86 *                 |--------------------|
 87 * 2nd endpoint    | 0x000123456789abf0 | DMA address for AP endpoint 5 rule
 88 *                 |--------------------|
 89 * (unused)        |                    | (Unused space in filter table)
 90 *                 |--------------------|
 91 *                          . . .
 92 *                 |--------------------|
 93 * (unused)        |                    | (Unused space in filter table)
 94 *                 ----------------------
 95 *
 96 * The set of available route rules is divided about equally between the AP
 97 * and modem.  The AP initializes all entries in a route table to refer to
 98 * a "zero entry".  Once initialized, the modem and AP are responsible for
 99 * updating their own entries.  All entries in a route table are usable,
100 * though the AP currently does not use the IPA routing functionality.
101 *
102 *                    IPA Route Table
103 *                 ----------------------
104 * 1st modem route | 0x0001234500001100 | DMA address for first route rule
105 *                 |--------------------|
106 * 2nd modem route | 0x0001234500001140 | DMA address for second route rule
107 *                 |--------------------|
108 *                          . . .
109 *                 |--------------------|
110 * Last modem route| 0x0001234500002280 | DMA address for Nth route rule
111 *                 |--------------------|
112 * 1st AP route    | 0x0001234500001100 | DMA address for route rule (N+1)
113 *                 |--------------------|
114 * 2nd AP route    | 0x0001234500001140 | DMA address for next route rule
115 *                 |--------------------|
116 *                          . . .
117 *                 |--------------------|
118 * Last AP route   | 0x0001234500002280 | DMA address for last route rule
119 *                 ----------------------
120 */
121
122/* Filter or route rules consist of a set of 32-bit values followed by a
123 * 32-bit all-zero rule list terminator.  The "zero rule" is simply an
124 * all-zero rule followed by the list terminator.
125 */
126#define IPA_ZERO_RULE_SIZE		(2 * sizeof(__le32))
127
128/* Check things that can be validated at build time. */
129static void ipa_table_validate_build(void)
130{
131	/* Filter and route tables contain DMA addresses that refer
132	 * to filter or route rules.  But the size of a table entry
133	 * is 64 bits regardless of what the size of an AP DMA address
134	 * is.  A fixed constant defines the size of an entry, and
135	 * code in ipa_table_init() uses a pointer to __le64 to
136	 * initialize tables.
137	 */
138	BUILD_BUG_ON(sizeof(dma_addr_t) > sizeof(__le64));
139
140	/* A "zero rule" is used to represent no filtering or no routing.
141	 * It is a 64-bit block of zeroed memory.  Code in ipa_table_init()
142	 * assumes that it can be written using a pointer to __le64.
143	 */
144	BUILD_BUG_ON(IPA_ZERO_RULE_SIZE != sizeof(__le64));
145}
146
147static const struct ipa_mem *
148ipa_table_mem(struct ipa *ipa, bool filter, bool hashed, bool ipv6)
149{
150	enum ipa_mem_id mem_id;
151
152	mem_id = filter ? hashed ? ipv6 ? IPA_MEM_V6_FILTER_HASHED
153					: IPA_MEM_V4_FILTER_HASHED
154				 : ipv6 ? IPA_MEM_V6_FILTER
155					: IPA_MEM_V4_FILTER
156			: hashed ? ipv6 ? IPA_MEM_V6_ROUTE_HASHED
157					: IPA_MEM_V4_ROUTE_HASHED
158				 : ipv6 ? IPA_MEM_V6_ROUTE
159					: IPA_MEM_V4_ROUTE;
160
161	return ipa_mem_find(ipa, mem_id);
162}
163
 
 
 
 
 
 
164bool ipa_filtered_valid(struct ipa *ipa, u64 filtered)
165{
166	struct device *dev = &ipa->pdev->dev;
167	u32 count;
168
169	if (!filtered) {
170		dev_err(dev, "at least one filtering endpoint is required\n");
171
172		return false;
173	}
174
175	count = hweight64(filtered);
176	if (count > ipa->filter_count) {
177		dev_err(dev, "too many filtering endpoints (%u > %u)\n",
178			count, ipa->filter_count);
179
180		return false;
181	}
182
183	return true;
184}
185
186/* Zero entry count means no table, so just return a 0 address */
187static dma_addr_t ipa_table_addr(struct ipa *ipa, bool filter_mask, u16 count)
188{
189	u32 skip;
190
191	if (!count)
192		return 0;
193
194	WARN_ON(count > max_t(u32, ipa->filter_count, ipa->route_count));
195
196	/* Skip over the zero rule and possibly the filter mask */
197	skip = filter_mask ? 1 : 2;
198
199	return ipa->table_addr + skip * sizeof(*ipa->table_virt);
200}
201
202static void ipa_table_reset_add(struct gsi_trans *trans, bool filter,
203				bool hashed, bool ipv6, u16 first, u16 count)
204{
205	struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
206	const struct ipa_mem *mem;
207	dma_addr_t addr;
208	u32 offset;
209	u16 size;
210
211	/* Nothing to do if the memory region is doesn't exist or is empty */
212	mem = ipa_table_mem(ipa, filter, hashed, ipv6);
213	if (!mem || !mem->size)
214		return;
215
216	if (filter)
217		first++;	/* skip over bitmap */
218
219	offset = mem->offset + first * sizeof(__le64);
220	size = count * sizeof(__le64);
221	addr = ipa_table_addr(ipa, false, count);
222
223	ipa_cmd_dma_shared_mem_add(trans, offset, size, addr, true);
224}
225
226/* Reset entries in a single filter table belonging to either the AP or
227 * modem to refer to the zero entry.  The memory region supplied will be
228 * for the IPv4 and IPv6 non-hashed and hashed filter tables.
229 */
230static int
231ipa_filter_reset_table(struct ipa *ipa, bool hashed, bool ipv6, bool modem)
232{
233	u64 ep_mask = ipa->filtered;
234	struct gsi_trans *trans;
235	enum gsi_ee_id ee_id;
236
237	trans = ipa_cmd_trans_alloc(ipa, hweight64(ep_mask));
238	if (!trans) {
239		dev_err(&ipa->pdev->dev,
240			"no transaction for %s filter reset\n",
241			modem ? "modem" : "AP");
242		return -EBUSY;
243	}
244
245	ee_id = modem ? GSI_EE_MODEM : GSI_EE_AP;
246	while (ep_mask) {
247		u32 endpoint_id = __ffs(ep_mask);
248		struct ipa_endpoint *endpoint;
249
250		ep_mask ^= BIT(endpoint_id);
251
252		endpoint = &ipa->endpoint[endpoint_id];
253		if (endpoint->ee_id != ee_id)
254			continue;
255
256		ipa_table_reset_add(trans, true, hashed, ipv6, endpoint_id, 1);
257	}
258
259	gsi_trans_commit_wait(trans);
260
261	return 0;
262}
263
264/* Theoretically, each filter table could have more filter slots to
265 * update than the maximum number of commands in a transaction.  So
266 * we do each table separately.
267 */
268static int ipa_filter_reset(struct ipa *ipa, bool modem)
269{
270	int ret;
271
272	ret = ipa_filter_reset_table(ipa, false, false, modem);
273	if (ret)
274		return ret;
275
276	ret = ipa_filter_reset_table(ipa, false, true, modem);
277	if (ret || !ipa_table_hash_support(ipa))
278		return ret;
279
280	ret = ipa_filter_reset_table(ipa, true, false, modem);
281	if (ret)
282		return ret;
283
284	return ipa_filter_reset_table(ipa, true, true, modem);
285}
286
287/* The AP routes and modem routes are each contiguous within the
288 * table.  We can update each table with a single command, and we
289 * won't exceed the per-transaction command limit.
290 * */
291static int ipa_route_reset(struct ipa *ipa, bool modem)
292{
293	bool hash_support = ipa_table_hash_support(ipa);
294	u32 modem_route_count = ipa->modem_route_count;
295	struct gsi_trans *trans;
296	u16 first;
297	u16 count;
298
299	trans = ipa_cmd_trans_alloc(ipa, hash_support ? 4 : 2);
300	if (!trans) {
301		dev_err(&ipa->pdev->dev,
302			"no transaction for %s route reset\n",
303			modem ? "modem" : "AP");
304		return -EBUSY;
305	}
306
307	if (modem) {
308		first = 0;
309		count = modem_route_count;
310	} else {
311		first = modem_route_count;
312		count = ipa->route_count - modem_route_count;
313	}
314
315	ipa_table_reset_add(trans, false, false, false, first, count);
316	ipa_table_reset_add(trans, false, false, true, first, count);
317
318	if (hash_support) {
319		ipa_table_reset_add(trans, false, true, false, first, count);
320		ipa_table_reset_add(trans, false, true, true, first, count);
321	}
322
323	gsi_trans_commit_wait(trans);
324
325	return 0;
326}
327
328void ipa_table_reset(struct ipa *ipa, bool modem)
329{
330	struct device *dev = &ipa->pdev->dev;
331	const char *ee_name;
332	int ret;
333
334	ee_name = modem ? "modem" : "AP";
335
336	/* Report errors, but reset filter and route tables */
337	ret = ipa_filter_reset(ipa, modem);
338	if (ret)
339		dev_err(dev, "error %d resetting filter table for %s\n",
340				ret, ee_name);
341
342	ret = ipa_route_reset(ipa, modem);
343	if (ret)
344		dev_err(dev, "error %d resetting route table for %s\n",
345				ret, ee_name);
346}
347
348int ipa_table_hash_flush(struct ipa *ipa)
349{
350	struct gsi_trans *trans;
351	const struct reg *reg;
352	u32 val;
353
354	if (!ipa_table_hash_support(ipa))
355		return 0;
356
357	trans = ipa_cmd_trans_alloc(ipa, 1);
358	if (!trans) {
359		dev_err(&ipa->pdev->dev, "no transaction for hash flush\n");
360		return -EBUSY;
361	}
362
363	if (ipa->version < IPA_VERSION_5_0) {
364		reg = ipa_reg(ipa, FILT_ROUT_HASH_FLUSH);
365
366		val = reg_bit(reg, IPV6_ROUTER_HASH);
367		val |= reg_bit(reg, IPV6_FILTER_HASH);
368		val |= reg_bit(reg, IPV4_ROUTER_HASH);
369		val |= reg_bit(reg, IPV4_FILTER_HASH);
370	} else {
371		reg = ipa_reg(ipa, FILT_ROUT_CACHE_FLUSH);
372
373		/* IPA v5.0+ uses a unified cache (both IPv4 and IPv6) */
374		val = reg_bit(reg, ROUTER_CACHE);
375		val |= reg_bit(reg, FILTER_CACHE);
376	}
377
378	ipa_cmd_register_write_add(trans, reg_offset(reg), val, val, false);
379
380	gsi_trans_commit_wait(trans);
381
382	return 0;
383}
384
385static void ipa_table_init_add(struct gsi_trans *trans, bool filter, bool ipv6)
386{
387	struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
388	const struct ipa_mem *hash_mem;
389	enum ipa_cmd_opcode opcode;
390	const struct ipa_mem *mem;
391	dma_addr_t hash_addr;
392	dma_addr_t addr;
393	u32 hash_offset;
394	u32 zero_offset;
395	u16 hash_count;
396	u32 zero_size;
397	u16 hash_size;
398	u16 count;
399	u16 size;
400
401	opcode = filter ? ipv6 ? IPA_CMD_IP_V6_FILTER_INIT
402			       : IPA_CMD_IP_V4_FILTER_INIT
403			: ipv6 ? IPA_CMD_IP_V6_ROUTING_INIT
404			       : IPA_CMD_IP_V4_ROUTING_INIT;
405
406	/* The non-hashed region will exist (see ipa_table_mem_valid()) */
407	mem = ipa_table_mem(ipa, filter, false, ipv6);
408	hash_mem = ipa_table_mem(ipa, filter, true, ipv6);
409	hash_offset = hash_mem ? hash_mem->offset : 0;
410
411	/* Compute the number of table entries to initialize */
412	if (filter) {
413		/* The number of filtering endpoints determines number of
414		 * entries in the filter table; we also add one more "slot"
415		 * to hold the bitmap itself.  The size of the hashed filter
416		 * table is either the same as the non-hashed one, or zero.
417		 */
418		count = 1 + hweight64(ipa->filtered);
419		hash_count = hash_mem && hash_mem->size ? count : 0;
420	} else {
421		/* The size of a route table region determines the number
422		 * of entries it has.
423		 */
424		count = mem->size / sizeof(__le64);
425		hash_count = hash_mem ? hash_mem->size / sizeof(__le64) : 0;
426	}
427	size = count * sizeof(__le64);
428	hash_size = hash_count * sizeof(__le64);
429
430	addr = ipa_table_addr(ipa, filter, count);
431	hash_addr = ipa_table_addr(ipa, filter, hash_count);
432
433	ipa_cmd_table_init_add(trans, opcode, size, mem->offset, addr,
434			       hash_size, hash_offset, hash_addr);
435	if (!filter)
436		return;
437
438	/* Zero the unused space in the filter table */
439	zero_offset = mem->offset + size;
440	zero_size = mem->size - size;
441	ipa_cmd_dma_shared_mem_add(trans, zero_offset, zero_size,
442				   ipa->zero_addr, true);
443	if (!hash_size)
444		return;
445
446	/* Zero the unused space in the hashed filter table */
447	zero_offset = hash_offset + hash_size;
448	zero_size = hash_mem->size - hash_size;
449	ipa_cmd_dma_shared_mem_add(trans, zero_offset, zero_size,
450				   ipa->zero_addr, true);
451}
452
453int ipa_table_setup(struct ipa *ipa)
454{
455	struct gsi_trans *trans;
456
457	/* We will need at most 8 TREs:
458	 * - IPv4:
459	 *     - One for route table initialization (non-hashed and hashed)
460	 *     - One for filter table initialization (non-hashed and hashed)
461	 *     - One to zero unused entries in the non-hashed filter table
462	 *     - One to zero unused entries in the hashed filter table
463	 * - IPv6:
464	 *     - One for route table initialization (non-hashed and hashed)
465	 *     - One for filter table initialization (non-hashed and hashed)
466	 *     - One to zero unused entries in the non-hashed filter table
467	 *     - One to zero unused entries in the hashed filter table
468	 * All platforms support at least 8 TREs in a transaction.
469	 */
470	trans = ipa_cmd_trans_alloc(ipa, 8);
471	if (!trans) {
472		dev_err(&ipa->pdev->dev, "no transaction for table setup\n");
473		return -EBUSY;
474	}
475
476	ipa_table_init_add(trans, false, false);
477	ipa_table_init_add(trans, false, true);
478	ipa_table_init_add(trans, true, false);
479	ipa_table_init_add(trans, true, true);
480
481	gsi_trans_commit_wait(trans);
482
483	return 0;
484}
485
486/**
487 * ipa_filter_tuple_zero() - Zero an endpoint's hashed filter tuple
488 * @endpoint:	Endpoint whose filter hash tuple should be zeroed
489 *
490 * Endpoint must be for the AP (not modem) and support filtering. Updates
491 * the filter hash values without changing route ones.
492 */
493static void ipa_filter_tuple_zero(struct ipa_endpoint *endpoint)
494{
495	u32 endpoint_id = endpoint->endpoint_id;
496	struct ipa *ipa = endpoint->ipa;
497	const struct reg *reg;
498	u32 offset;
499	u32 val;
500
501	if (ipa->version < IPA_VERSION_5_0) {
502		reg = ipa_reg(ipa, ENDP_FILTER_ROUTER_HSH_CFG);
503
504		offset = reg_n_offset(reg, endpoint_id);
505		val = ioread32(endpoint->ipa->reg_virt + offset);
506
507		/* Zero all filter-related fields, preserving the rest */
508		val &= ~reg_fmask(reg, FILTER_HASH_MSK_ALL);
509	} else {
510		/* IPA v5.0 separates filter and router cache configuration */
511		reg = ipa_reg(ipa, ENDP_FILTER_CACHE_CFG);
512		offset = reg_n_offset(reg, endpoint_id);
513
514		/* Zero all filter-related fields */
515		val = 0;
516	}
517
518	iowrite32(val, endpoint->ipa->reg_virt + offset);
519}
520
521/* Configure a hashed filter table; there is no ipa_filter_deconfig() */
522static void ipa_filter_config(struct ipa *ipa, bool modem)
523{
524	enum gsi_ee_id ee_id = modem ? GSI_EE_MODEM : GSI_EE_AP;
525	u64 ep_mask = ipa->filtered;
526
527	if (!ipa_table_hash_support(ipa))
528		return;
529
530	while (ep_mask) {
531		u32 endpoint_id = __ffs(ep_mask);
532		struct ipa_endpoint *endpoint;
533
534		ep_mask ^= BIT(endpoint_id);
535
536		endpoint = &ipa->endpoint[endpoint_id];
537		if (endpoint->ee_id == ee_id)
538			ipa_filter_tuple_zero(endpoint);
539	}
540}
541
542static bool ipa_route_id_modem(struct ipa *ipa, u32 route_id)
543{
544	return route_id < ipa->modem_route_count;
545}
546
547/**
548 * ipa_route_tuple_zero() - Zero a hashed route table entry tuple
549 * @ipa:	IPA pointer
550 * @route_id:	Route table entry whose hash tuple should be zeroed
551 *
552 * Updates the route hash values without changing filter ones.
553 */
554static void ipa_route_tuple_zero(struct ipa *ipa, u32 route_id)
555{
556	const struct reg *reg;
557	u32 offset;
558	u32 val;
559
560	if (ipa->version < IPA_VERSION_5_0) {
561		reg = ipa_reg(ipa, ENDP_FILTER_ROUTER_HSH_CFG);
562		offset = reg_n_offset(reg, route_id);
563
564		val = ioread32(ipa->reg_virt + offset);
565
566		/* Zero all route-related fields, preserving the rest */
567		val &= ~reg_fmask(reg, ROUTER_HASH_MSK_ALL);
568	} else {
569		/* IPA v5.0 separates filter and router cache configuration */
570		reg = ipa_reg(ipa, ENDP_ROUTER_CACHE_CFG);
571		offset = reg_n_offset(reg, route_id);
572
573		/* Zero all route-related fields */
574		val = 0;
575	}
576
577	iowrite32(val, ipa->reg_virt + offset);
578}
579
580/* Configure a hashed route table; there is no ipa_route_deconfig() */
581static void ipa_route_config(struct ipa *ipa, bool modem)
582{
583	u32 route_id;
584
585	if (!ipa_table_hash_support(ipa))
586		return;
587
588	for (route_id = 0; route_id < ipa->route_count; route_id++)
589		if (ipa_route_id_modem(ipa, route_id) == modem)
590			ipa_route_tuple_zero(ipa, route_id);
591}
592
593/* Configure a filter and route tables; there is no ipa_table_deconfig() */
594void ipa_table_config(struct ipa *ipa)
595{
596	ipa_filter_config(ipa, false);
597	ipa_filter_config(ipa, true);
598	ipa_route_config(ipa, false);
599	ipa_route_config(ipa, true);
600}
601
602/* Verify the sizes of all IPA table filter or routing table memory regions
603 * are valid.  If valid, this records the size of the routing table.
604 */
605bool ipa_table_mem_valid(struct ipa *ipa, bool filter)
606{
607	bool hash_support = ipa_table_hash_support(ipa);
608	const struct ipa_mem *mem_hashed;
609	const struct ipa_mem *mem_ipv4;
610	const struct ipa_mem *mem_ipv6;
611	u32 count;
612
613	/* IPv4 and IPv6 non-hashed tables are expected to be defined and
614	 * have the same size.  Both must have at least two entries (and
615	 * would normally have more than that).
616	 */
617	mem_ipv4 = ipa_table_mem(ipa, filter, false, false);
618	if (!mem_ipv4)
619		return false;
620
621	mem_ipv6 = ipa_table_mem(ipa, filter, false, true);
622	if (!mem_ipv6)
623		return false;
624
625	if (mem_ipv4->size != mem_ipv6->size)
626		return false;
627
628	/* Compute and record the number of entries for each table type */
629	count = mem_ipv4->size / sizeof(__le64);
630	if (count < 2)
631		return false;
632	if (filter)
633		ipa->filter_count = count - 1;	/* Filter map in first entry */
634	else
635		ipa->route_count = count;
636
637	/* Table offset and size must fit in TABLE_INIT command fields */
638	if (!ipa_cmd_table_init_valid(ipa, mem_ipv4, !filter))
639		return false;
640
641	/* Make sure the regions are big enough */
642	if (filter) {
643		/* Filter tables must able to hold the endpoint bitmap plus
644		 * an entry for each endpoint that supports filtering
645		 */
646		if (count < 1 + hweight64(ipa->filtered))
647			return false;
648	} else {
649		/* Routing tables must be able to hold all modem entries,
650		 * plus at least one entry for the AP.
651		 */
652		if (count < ipa->modem_route_count + 1)
653			return false;
654	}
655
656	/* If hashing is supported, hashed tables are expected to be defined,
657	 * and have the same size as non-hashed tables.  If hashing is not
658	 * supported, hashed tables are expected to have zero size (or not
659	 * be defined).
660	 */
661	mem_hashed = ipa_table_mem(ipa, filter, true, false);
662	if (hash_support) {
663		if (!mem_hashed || mem_hashed->size != mem_ipv4->size)
664			return false;
665	} else {
666		if (mem_hashed && mem_hashed->size)
667			return false;
668	}
669
670	/* Same check for IPv6 tables */
671	mem_hashed = ipa_table_mem(ipa, filter, true, true);
672	if (hash_support) {
673		if (!mem_hashed || mem_hashed->size != mem_ipv6->size)
674			return false;
675	} else {
676		if (mem_hashed && mem_hashed->size)
677			return false;
678	}
679
680	return true;
681}
682
683/* Initialize a coherent DMA allocation containing initialized filter and
684 * route table data.  This is used when initializing or resetting the IPA
685 * filter or route table.
686 *
687 * The first entry in a filter table contains a bitmap indicating which
688 * endpoints contain entries in the table.  In addition to that first entry,
689 * there is a fixed maximum number of entries that follow.  Filter table
690 * entries are 64 bits wide, and (other than the bitmap) contain the DMA
691 * address of a filter rule.  A "zero rule" indicates no filtering, and
692 * consists of 64 bits of zeroes.  When a filter table is initialized (or
693 * reset) its entries are made to refer to the zero rule.
694 *
695 * Each entry in a route table is the DMA address of a routing rule.  For
696 * routing there is also a 64-bit "zero rule" that means no routing, and
697 * when a route table is initialized or reset, its entries are made to refer
698 * to the zero rule.  The zero rule is shared for route and filter tables.
699 *
700 *	     +-------------------+
701 *	 --> |     zero rule     |
702 *	/    |-------------------|
703 *	|    |     filter mask   |
704 *	|\   |-------------------|
705 *	| ---- zero rule address | \
706 *	|\   |-------------------|  |
707 *	| ---- zero rule address |  |	Max IPA filter count
708 *	|    |-------------------|   >	or IPA route count,
709 *	|	      ...	    |	whichever is greater
710 *	 \   |-------------------|  |
711 *	  ---- zero rule address | /
712 *	     +-------------------+
713 */
714int ipa_table_init(struct ipa *ipa)
715{
716	struct device *dev = &ipa->pdev->dev;
717	dma_addr_t addr;
718	__le64 le_addr;
719	__le64 *virt;
720	size_t size;
721	u32 count;
722
723	ipa_table_validate_build();
724
725	count = max_t(u32, ipa->filter_count, ipa->route_count);
726
727	/* The IPA hardware requires route and filter table rules to be
728	 * aligned on a 128-byte boundary.  We put the "zero rule" at the
729	 * base of the table area allocated here.  The DMA address returned
730	 * by dma_alloc_coherent() is guaranteed to be a power-of-2 number
731	 * of pages, which satisfies the rule alignment requirement.
732	 */
733	size = IPA_ZERO_RULE_SIZE + (1 + count) * sizeof(__le64);
734	virt = dma_alloc_coherent(dev, size, &addr, GFP_KERNEL);
735	if (!virt)
736		return -ENOMEM;
737
738	ipa->table_virt = virt;
739	ipa->table_addr = addr;
740
741	/* First slot is the zero rule */
742	*virt++ = 0;
743
744	/* Next is the filter table bitmap.  The "soft" bitmap value might
745	 * need to be converted to the hardware representation by shifting
746	 * it left one position.  Prior to IPA v5.0, bit 0 repesents global
747	 * filtering, which is possible but not used.  IPA v5.0+ eliminated
748	 * that option, so there's no shifting required.
749	 */
750	if (ipa->version < IPA_VERSION_5_0)
751		*virt++ = cpu_to_le64(ipa->filtered << 1);
752	else
753		*virt++ = cpu_to_le64(ipa->filtered);
754
755	/* All the rest contain the DMA address of the zero rule */
756	le_addr = cpu_to_le64(addr);
757	while (count--)
758		*virt++ = le_addr;
759
760	return 0;
761}
762
763void ipa_table_exit(struct ipa *ipa)
764{
765	u32 count = max_t(u32, 1 + ipa->filter_count, ipa->route_count);
766	struct device *dev = &ipa->pdev->dev;
767	size_t size;
768
769	size = IPA_ZERO_RULE_SIZE + (1 + count) * sizeof(__le64);
770
771	dma_free_coherent(dev, size, ipa->table_virt, ipa->table_addr);
772	ipa->table_addr = 0;
773	ipa->table_virt = NULL;
774}
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0
  2
  3/* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
  4 * Copyright (C) 2018-2024 Linaro Ltd.
  5 */
  6
 
 
 
  7#include <linux/bitops.h>
 
 
  8#include <linux/build_bug.h>
  9#include <linux/device.h>
 10#include <linux/dma-mapping.h>
 11#include <linux/io.h>
 12#include <linux/types.h>
 13
 14#include "gsi.h"
 15#include "gsi_trans.h"
 16#include "ipa.h"
 17#include "ipa_cmd.h"
 18#include "ipa_endpoint.h"
 
 
 19#include "ipa_mem.h"
 20#include "ipa_reg.h"
 21#include "ipa_table.h"
 22#include "ipa_version.h"
 23
 24/**
 25 * DOC: IPA Filter and Route Tables
 26 *
 27 * The IPA has tables defined in its local (IPA-resident) memory that define
 28 * filter and routing rules.  An entry in either of these tables is a little
 29 * endian 64-bit "slot" that holds the address of a rule definition.  (The
 30 * size of these slots is 64 bits regardless of the host DMA address size.)
 31 *
 32 * Separate tables (both filter and route) are used for IPv4 and IPv6.  There
 33 * is normally another set of "hashed" filter and route tables, which are
 34 * used with a hash of message metadata.  Hashed operation is not supported
 35 * by all IPA hardware (IPA v4.2 doesn't support hashed tables).
 36 *
 37 * Rules can be in local memory or in DRAM (system memory).  The offset of
 38 * an object (such as a route or filter table) in IPA-resident memory must
 39 * 128-byte aligned.  An object in system memory (such as a route or filter
 40 * rule) must be at an 8-byte aligned address.  We currently only place
 41 * route or filter rules in system memory.
 42 *
 43 * A rule consists of a contiguous block of 32-bit values terminated with
 44 * 32 zero bits.  A special "zero entry" rule consisting of 64 zero bits
 45 * represents "no filtering" or "no routing," and is the reset value for
 46 * filter or route table rules.
 47 *
 48 * Each filter rule is associated with an AP or modem TX endpoint, though
 49 * not all TX endpoints support filtering.  The first 64-bit slot in a
 50 * filter table is a bitmap indicating which endpoints have entries in
 51 * the table.  Each set bit in this bitmap indicates the presence of the
 52 * address of a filter rule in the memory following the bitmap.  Until IPA
 53 * v5.0,  the low-order bit (bit 0) in this bitmap represents a special
 54 * global filter, which applies to all traffic.  Otherwise the position of
 55 * each set bit represents an endpoint for which a filter rule is defined.
 56 *
 57 * The global rule is not used in current code, and support for it is
 58 * removed starting at IPA v5.0.  For IPA v5.0+, the endpoint bitmap
 59 * position defines the endpoint ID--i.e. if bit 1 is set in the endpoint
 60 * bitmap, endpoint 1 has a filter rule.  Older versions of IPA represent
 61 * the presence of a filter rule for endpoint X by bit (X + 1) being set.
 62 * I.e., bit 1 set indicates the presence of a filter rule for endpoint 0,
 63 * and bit 3 set means there is a filter rule present for endpoint 2.
 64 *
 65 * Each filter table entry has the address of a set of equations that
 66 * implement a filter rule.  So following the endpoint bitmap there
 67 * will be such an address/entry for each endpoint with a set bit in
 68 * the bitmap.
 69 *
 70 * The AP initializes all entries in a filter table to refer to a "zero"
 71 * rule.  Once initialized, the modem and AP update the entries for
 72 * endpoints they "own" directly.  Currently the AP does not use the IPA
 73 * filtering functionality.
 74 *
 75 * This diagram shows an example of a filter table with an endpoint
 76 * bitmap as defined prior to IPA v5.0.
 77 *
 78 *                    IPA Filter Table
 79 *                 ----------------------
 80 * endpoint bitmap | 0x0000000000000048 | Bits 3 and 6 set (endpoints 2 and 5)
 81 *                 |--------------------|
 82 * 1st endpoint    | 0x000123456789abc0 | DMA address for modem endpoint 2 rule
 83 *                 |--------------------|
 84 * 2nd endpoint    | 0x000123456789abf0 | DMA address for AP endpoint 5 rule
 85 *                 |--------------------|
 86 * (unused)        |                    | (Unused space in filter table)
 87 *                 |--------------------|
 88 *                          . . .
 89 *                 |--------------------|
 90 * (unused)        |                    | (Unused space in filter table)
 91 *                 ----------------------
 92 *
 93 * The set of available route rules is divided about equally between the AP
 94 * and modem.  The AP initializes all entries in a route table to refer to
 95 * a "zero entry".  Once initialized, the modem and AP are responsible for
 96 * updating their own entries.  All entries in a route table are usable,
 97 * though the AP currently does not use the IPA routing functionality.
 98 *
 99 *                    IPA Route Table
100 *                 ----------------------
101 * 1st modem route | 0x0001234500001100 | DMA address for first route rule
102 *                 |--------------------|
103 * 2nd modem route | 0x0001234500001140 | DMA address for second route rule
104 *                 |--------------------|
105 *                          . . .
106 *                 |--------------------|
107 * Last modem route| 0x0001234500002280 | DMA address for Nth route rule
108 *                 |--------------------|
109 * 1st AP route    | 0x0001234500001100 | DMA address for route rule (N+1)
110 *                 |--------------------|
111 * 2nd AP route    | 0x0001234500001140 | DMA address for next route rule
112 *                 |--------------------|
113 *                          . . .
114 *                 |--------------------|
115 * Last AP route   | 0x0001234500002280 | DMA address for last route rule
116 *                 ----------------------
117 */
118
119/* Filter or route rules consist of a set of 32-bit values followed by a
120 * 32-bit all-zero rule list terminator.  The "zero rule" is simply an
121 * all-zero rule followed by the list terminator.
122 */
123#define IPA_ZERO_RULE_SIZE		(2 * sizeof(__le32))
124
125/* Check things that can be validated at build time. */
126static void ipa_table_validate_build(void)
127{
128	/* Filter and route tables contain DMA addresses that refer
129	 * to filter or route rules.  But the size of a table entry
130	 * is 64 bits regardless of what the size of an AP DMA address
131	 * is.  A fixed constant defines the size of an entry, and
132	 * code in ipa_table_init() uses a pointer to __le64 to
133	 * initialize tables.
134	 */
135	BUILD_BUG_ON(sizeof(dma_addr_t) > sizeof(__le64));
136
137	/* A "zero rule" is used to represent no filtering or no routing.
138	 * It is a 64-bit block of zeroed memory.  Code in ipa_table_init()
139	 * assumes that it can be written using a pointer to __le64.
140	 */
141	BUILD_BUG_ON(IPA_ZERO_RULE_SIZE != sizeof(__le64));
142}
143
144static const struct ipa_mem *
145ipa_table_mem(struct ipa *ipa, bool filter, bool hashed, bool ipv6)
146{
147	enum ipa_mem_id mem_id;
148
149	mem_id = filter ? hashed ? ipv6 ? IPA_MEM_V6_FILTER_HASHED
150					: IPA_MEM_V4_FILTER_HASHED
151				 : ipv6 ? IPA_MEM_V6_FILTER
152					: IPA_MEM_V4_FILTER
153			: hashed ? ipv6 ? IPA_MEM_V6_ROUTE_HASHED
154					: IPA_MEM_V4_ROUTE_HASHED
155				 : ipv6 ? IPA_MEM_V6_ROUTE
156					: IPA_MEM_V4_ROUTE;
157
158	return ipa_mem_find(ipa, mem_id);
159}
160
161/* Return true if hashed tables are supported */
162bool ipa_table_hash_support(struct ipa *ipa)
163{
164	return ipa->version != IPA_VERSION_4_2;
165}
166
167bool ipa_filtered_valid(struct ipa *ipa, u64 filtered)
168{
169	struct device *dev = ipa->dev;
170	u32 count;
171
172	if (!filtered) {
173		dev_err(dev, "at least one filtering endpoint is required\n");
174
175		return false;
176	}
177
178	count = hweight64(filtered);
179	if (count > ipa->filter_count) {
180		dev_err(dev, "too many filtering endpoints (%u > %u)\n",
181			count, ipa->filter_count);
182
183		return false;
184	}
185
186	return true;
187}
188
189/* Zero entry count means no table, so just return a 0 address */
190static dma_addr_t ipa_table_addr(struct ipa *ipa, bool filter_mask, u16 count)
191{
192	u32 skip;
193
194	if (!count)
195		return 0;
196
197	WARN_ON(count > max_t(u32, ipa->filter_count, ipa->route_count));
198
199	/* Skip over the zero rule and possibly the filter mask */
200	skip = filter_mask ? 1 : 2;
201
202	return ipa->table_addr + skip * sizeof(*ipa->table_virt);
203}
204
205static void ipa_table_reset_add(struct gsi_trans *trans, bool filter,
206				bool hashed, bool ipv6, u16 first, u16 count)
207{
208	struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
209	const struct ipa_mem *mem;
210	dma_addr_t addr;
211	u32 offset;
212	u16 size;
213
214	/* Nothing to do if the memory region is doesn't exist or is empty */
215	mem = ipa_table_mem(ipa, filter, hashed, ipv6);
216	if (!mem || !mem->size)
217		return;
218
219	if (filter)
220		first++;	/* skip over bitmap */
221
222	offset = mem->offset + first * sizeof(__le64);
223	size = count * sizeof(__le64);
224	addr = ipa_table_addr(ipa, false, count);
225
226	ipa_cmd_dma_shared_mem_add(trans, offset, size, addr, true);
227}
228
229/* Reset entries in a single filter table belonging to either the AP or
230 * modem to refer to the zero entry.  The memory region supplied will be
231 * for the IPv4 and IPv6 non-hashed and hashed filter tables.
232 */
233static int
234ipa_filter_reset_table(struct ipa *ipa, bool hashed, bool ipv6, bool modem)
235{
236	u64 ep_mask = ipa->filtered;
237	struct gsi_trans *trans;
238	enum gsi_ee_id ee_id;
239
240	trans = ipa_cmd_trans_alloc(ipa, hweight64(ep_mask));
241	if (!trans) {
242		dev_err(ipa->dev, "no transaction for %s filter reset\n",
 
243			modem ? "modem" : "AP");
244		return -EBUSY;
245	}
246
247	ee_id = modem ? GSI_EE_MODEM : GSI_EE_AP;
248	while (ep_mask) {
249		u32 endpoint_id = __ffs(ep_mask);
250		struct ipa_endpoint *endpoint;
251
252		ep_mask ^= BIT(endpoint_id);
253
254		endpoint = &ipa->endpoint[endpoint_id];
255		if (endpoint->ee_id != ee_id)
256			continue;
257
258		ipa_table_reset_add(trans, true, hashed, ipv6, endpoint_id, 1);
259	}
260
261	gsi_trans_commit_wait(trans);
262
263	return 0;
264}
265
266/* Theoretically, each filter table could have more filter slots to
267 * update than the maximum number of commands in a transaction.  So
268 * we do each table separately.
269 */
270static int ipa_filter_reset(struct ipa *ipa, bool modem)
271{
272	int ret;
273
274	ret = ipa_filter_reset_table(ipa, false, false, modem);
275	if (ret)
276		return ret;
277
278	ret = ipa_filter_reset_table(ipa, false, true, modem);
279	if (ret || !ipa_table_hash_support(ipa))
280		return ret;
281
282	ret = ipa_filter_reset_table(ipa, true, false, modem);
283	if (ret)
284		return ret;
285
286	return ipa_filter_reset_table(ipa, true, true, modem);
287}
288
289/* The AP routes and modem routes are each contiguous within the
290 * table.  We can update each table with a single command, and we
291 * won't exceed the per-transaction command limit.
292 * */
293static int ipa_route_reset(struct ipa *ipa, bool modem)
294{
295	bool hash_support = ipa_table_hash_support(ipa);
296	u32 modem_route_count = ipa->modem_route_count;
297	struct gsi_trans *trans;
298	u16 first;
299	u16 count;
300
301	trans = ipa_cmd_trans_alloc(ipa, hash_support ? 4 : 2);
302	if (!trans) {
303		dev_err(ipa->dev, "no transaction for %s route reset\n",
 
304			modem ? "modem" : "AP");
305		return -EBUSY;
306	}
307
308	if (modem) {
309		first = 0;
310		count = modem_route_count;
311	} else {
312		first = modem_route_count;
313		count = ipa->route_count - modem_route_count;
314	}
315
316	ipa_table_reset_add(trans, false, false, false, first, count);
317	ipa_table_reset_add(trans, false, false, true, first, count);
318
319	if (hash_support) {
320		ipa_table_reset_add(trans, false, true, false, first, count);
321		ipa_table_reset_add(trans, false, true, true, first, count);
322	}
323
324	gsi_trans_commit_wait(trans);
325
326	return 0;
327}
328
329void ipa_table_reset(struct ipa *ipa, bool modem)
330{
331	struct device *dev = ipa->dev;
332	const char *ee_name;
333	int ret;
334
335	ee_name = modem ? "modem" : "AP";
336
337	/* Report errors, but reset filter and route tables */
338	ret = ipa_filter_reset(ipa, modem);
339	if (ret)
340		dev_err(dev, "error %d resetting filter table for %s\n",
341				ret, ee_name);
342
343	ret = ipa_route_reset(ipa, modem);
344	if (ret)
345		dev_err(dev, "error %d resetting route table for %s\n",
346				ret, ee_name);
347}
348
349int ipa_table_hash_flush(struct ipa *ipa)
350{
351	struct gsi_trans *trans;
352	const struct reg *reg;
353	u32 val;
354
355	if (!ipa_table_hash_support(ipa))
356		return 0;
357
358	trans = ipa_cmd_trans_alloc(ipa, 1);
359	if (!trans) {
360		dev_err(ipa->dev, "no transaction for hash flush\n");
361		return -EBUSY;
362	}
363
364	if (ipa->version < IPA_VERSION_5_0) {
365		reg = ipa_reg(ipa, FILT_ROUT_HASH_FLUSH);
366
367		val = reg_bit(reg, IPV6_ROUTER_HASH);
368		val |= reg_bit(reg, IPV6_FILTER_HASH);
369		val |= reg_bit(reg, IPV4_ROUTER_HASH);
370		val |= reg_bit(reg, IPV4_FILTER_HASH);
371	} else {
372		reg = ipa_reg(ipa, FILT_ROUT_CACHE_FLUSH);
373
374		/* IPA v5.0+ uses a unified cache (both IPv4 and IPv6) */
375		val = reg_bit(reg, ROUTER_CACHE);
376		val |= reg_bit(reg, FILTER_CACHE);
377	}
378
379	ipa_cmd_register_write_add(trans, reg_offset(reg), val, val, false);
380
381	gsi_trans_commit_wait(trans);
382
383	return 0;
384}
385
386static void ipa_table_init_add(struct gsi_trans *trans, bool filter, bool ipv6)
387{
388	struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
389	const struct ipa_mem *hash_mem;
390	enum ipa_cmd_opcode opcode;
391	const struct ipa_mem *mem;
392	dma_addr_t hash_addr;
393	dma_addr_t addr;
394	u32 hash_offset;
395	u32 zero_offset;
396	u16 hash_count;
397	u32 zero_size;
398	u16 hash_size;
399	u16 count;
400	u16 size;
401
402	opcode = filter ? ipv6 ? IPA_CMD_IP_V6_FILTER_INIT
403			       : IPA_CMD_IP_V4_FILTER_INIT
404			: ipv6 ? IPA_CMD_IP_V6_ROUTING_INIT
405			       : IPA_CMD_IP_V4_ROUTING_INIT;
406
407	/* The non-hashed region will exist (see ipa_table_mem_valid()) */
408	mem = ipa_table_mem(ipa, filter, false, ipv6);
409	hash_mem = ipa_table_mem(ipa, filter, true, ipv6);
410	hash_offset = hash_mem ? hash_mem->offset : 0;
411
412	/* Compute the number of table entries to initialize */
413	if (filter) {
414		/* The number of filtering endpoints determines number of
415		 * entries in the filter table; we also add one more "slot"
416		 * to hold the bitmap itself.  The size of the hashed filter
417		 * table is either the same as the non-hashed one, or zero.
418		 */
419		count = 1 + hweight64(ipa->filtered);
420		hash_count = hash_mem && hash_mem->size ? count : 0;
421	} else {
422		/* The size of a route table region determines the number
423		 * of entries it has.
424		 */
425		count = mem->size / sizeof(__le64);
426		hash_count = hash_mem ? hash_mem->size / sizeof(__le64) : 0;
427	}
428	size = count * sizeof(__le64);
429	hash_size = hash_count * sizeof(__le64);
430
431	addr = ipa_table_addr(ipa, filter, count);
432	hash_addr = ipa_table_addr(ipa, filter, hash_count);
433
434	ipa_cmd_table_init_add(trans, opcode, size, mem->offset, addr,
435			       hash_size, hash_offset, hash_addr);
436	if (!filter)
437		return;
438
439	/* Zero the unused space in the filter table */
440	zero_offset = mem->offset + size;
441	zero_size = mem->size - size;
442	ipa_cmd_dma_shared_mem_add(trans, zero_offset, zero_size,
443				   ipa->zero_addr, true);
444	if (!hash_size)
445		return;
446
447	/* Zero the unused space in the hashed filter table */
448	zero_offset = hash_offset + hash_size;
449	zero_size = hash_mem->size - hash_size;
450	ipa_cmd_dma_shared_mem_add(trans, zero_offset, zero_size,
451				   ipa->zero_addr, true);
452}
453
454int ipa_table_setup(struct ipa *ipa)
455{
456	struct gsi_trans *trans;
457
458	/* We will need at most 8 TREs:
459	 * - IPv4:
460	 *     - One for route table initialization (non-hashed and hashed)
461	 *     - One for filter table initialization (non-hashed and hashed)
462	 *     - One to zero unused entries in the non-hashed filter table
463	 *     - One to zero unused entries in the hashed filter table
464	 * - IPv6:
465	 *     - One for route table initialization (non-hashed and hashed)
466	 *     - One for filter table initialization (non-hashed and hashed)
467	 *     - One to zero unused entries in the non-hashed filter table
468	 *     - One to zero unused entries in the hashed filter table
469	 * All platforms support at least 8 TREs in a transaction.
470	 */
471	trans = ipa_cmd_trans_alloc(ipa, 8);
472	if (!trans) {
473		dev_err(ipa->dev, "no transaction for table setup\n");
474		return -EBUSY;
475	}
476
477	ipa_table_init_add(trans, false, false);
478	ipa_table_init_add(trans, false, true);
479	ipa_table_init_add(trans, true, false);
480	ipa_table_init_add(trans, true, true);
481
482	gsi_trans_commit_wait(trans);
483
484	return 0;
485}
486
487/**
488 * ipa_filter_tuple_zero() - Zero an endpoint's hashed filter tuple
489 * @endpoint:	Endpoint whose filter hash tuple should be zeroed
490 *
491 * Endpoint must be for the AP (not modem) and support filtering. Updates
492 * the filter hash values without changing route ones.
493 */
494static void ipa_filter_tuple_zero(struct ipa_endpoint *endpoint)
495{
496	u32 endpoint_id = endpoint->endpoint_id;
497	struct ipa *ipa = endpoint->ipa;
498	const struct reg *reg;
499	u32 offset;
500	u32 val;
501
502	if (ipa->version < IPA_VERSION_5_0) {
503		reg = ipa_reg(ipa, ENDP_FILTER_ROUTER_HSH_CFG);
504
505		offset = reg_n_offset(reg, endpoint_id);
506		val = ioread32(endpoint->ipa->reg_virt + offset);
507
508		/* Zero all filter-related fields, preserving the rest */
509		val &= ~reg_fmask(reg, FILTER_HASH_MSK_ALL);
510	} else {
511		/* IPA v5.0 separates filter and router cache configuration */
512		reg = ipa_reg(ipa, ENDP_FILTER_CACHE_CFG);
513		offset = reg_n_offset(reg, endpoint_id);
514
515		/* Zero all filter-related fields */
516		val = 0;
517	}
518
519	iowrite32(val, endpoint->ipa->reg_virt + offset);
520}
521
522/* Configure a hashed filter table; there is no ipa_filter_deconfig() */
523static void ipa_filter_config(struct ipa *ipa, bool modem)
524{
525	enum gsi_ee_id ee_id = modem ? GSI_EE_MODEM : GSI_EE_AP;
526	u64 ep_mask = ipa->filtered;
527
528	if (!ipa_table_hash_support(ipa))
529		return;
530
531	while (ep_mask) {
532		u32 endpoint_id = __ffs(ep_mask);
533		struct ipa_endpoint *endpoint;
534
535		ep_mask ^= BIT(endpoint_id);
536
537		endpoint = &ipa->endpoint[endpoint_id];
538		if (endpoint->ee_id == ee_id)
539			ipa_filter_tuple_zero(endpoint);
540	}
541}
542
543static bool ipa_route_id_modem(struct ipa *ipa, u32 route_id)
544{
545	return route_id < ipa->modem_route_count;
546}
547
548/**
549 * ipa_route_tuple_zero() - Zero a hashed route table entry tuple
550 * @ipa:	IPA pointer
551 * @route_id:	Route table entry whose hash tuple should be zeroed
552 *
553 * Updates the route hash values without changing filter ones.
554 */
555static void ipa_route_tuple_zero(struct ipa *ipa, u32 route_id)
556{
557	const struct reg *reg;
558	u32 offset;
559	u32 val;
560
561	if (ipa->version < IPA_VERSION_5_0) {
562		reg = ipa_reg(ipa, ENDP_FILTER_ROUTER_HSH_CFG);
563		offset = reg_n_offset(reg, route_id);
564
565		val = ioread32(ipa->reg_virt + offset);
566
567		/* Zero all route-related fields, preserving the rest */
568		val &= ~reg_fmask(reg, ROUTER_HASH_MSK_ALL);
569	} else {
570		/* IPA v5.0 separates filter and router cache configuration */
571		reg = ipa_reg(ipa, ENDP_ROUTER_CACHE_CFG);
572		offset = reg_n_offset(reg, route_id);
573
574		/* Zero all route-related fields */
575		val = 0;
576	}
577
578	iowrite32(val, ipa->reg_virt + offset);
579}
580
581/* Configure a hashed route table; there is no ipa_route_deconfig() */
582static void ipa_route_config(struct ipa *ipa, bool modem)
583{
584	u32 route_id;
585
586	if (!ipa_table_hash_support(ipa))
587		return;
588
589	for (route_id = 0; route_id < ipa->route_count; route_id++)
590		if (ipa_route_id_modem(ipa, route_id) == modem)
591			ipa_route_tuple_zero(ipa, route_id);
592}
593
594/* Configure a filter and route tables; there is no ipa_table_deconfig() */
595void ipa_table_config(struct ipa *ipa)
596{
597	ipa_filter_config(ipa, false);
598	ipa_filter_config(ipa, true);
599	ipa_route_config(ipa, false);
600	ipa_route_config(ipa, true);
601}
602
603/* Verify the sizes of all IPA table filter or routing table memory regions
604 * are valid.  If valid, this records the size of the routing table.
605 */
606bool ipa_table_mem_valid(struct ipa *ipa, bool filter)
607{
608	bool hash_support = ipa_table_hash_support(ipa);
609	const struct ipa_mem *mem_hashed;
610	const struct ipa_mem *mem_ipv4;
611	const struct ipa_mem *mem_ipv6;
612	u32 count;
613
614	/* IPv4 and IPv6 non-hashed tables are expected to be defined and
615	 * have the same size.  Both must have at least two entries (and
616	 * would normally have more than that).
617	 */
618	mem_ipv4 = ipa_table_mem(ipa, filter, false, false);
619	if (!mem_ipv4)
620		return false;
621
622	mem_ipv6 = ipa_table_mem(ipa, filter, false, true);
623	if (!mem_ipv6)
624		return false;
625
626	if (mem_ipv4->size != mem_ipv6->size)
627		return false;
628
629	/* Compute and record the number of entries for each table type */
630	count = mem_ipv4->size / sizeof(__le64);
631	if (count < 2)
632		return false;
633	if (filter)
634		ipa->filter_count = count - 1;	/* Filter map in first entry */
635	else
636		ipa->route_count = count;
637
638	/* Table offset and size must fit in TABLE_INIT command fields */
639	if (!ipa_cmd_table_init_valid(ipa, mem_ipv4, !filter))
640		return false;
641
642	/* Make sure the regions are big enough */
643	if (filter) {
644		/* Filter tables must able to hold the endpoint bitmap plus
645		 * an entry for each endpoint that supports filtering
646		 */
647		if (count < 1 + hweight64(ipa->filtered))
648			return false;
649	} else {
650		/* Routing tables must be able to hold all modem entries,
651		 * plus at least one entry for the AP.
652		 */
653		if (count < ipa->modem_route_count + 1)
654			return false;
655	}
656
657	/* If hashing is supported, hashed tables are expected to be defined,
658	 * and have the same size as non-hashed tables.  If hashing is not
659	 * supported, hashed tables are expected to have zero size (or not
660	 * be defined).
661	 */
662	mem_hashed = ipa_table_mem(ipa, filter, true, false);
663	if (hash_support) {
664		if (!mem_hashed || mem_hashed->size != mem_ipv4->size)
665			return false;
666	} else {
667		if (mem_hashed && mem_hashed->size)
668			return false;
669	}
670
671	/* Same check for IPv6 tables */
672	mem_hashed = ipa_table_mem(ipa, filter, true, true);
673	if (hash_support) {
674		if (!mem_hashed || mem_hashed->size != mem_ipv6->size)
675			return false;
676	} else {
677		if (mem_hashed && mem_hashed->size)
678			return false;
679	}
680
681	return true;
682}
683
684/* Initialize a coherent DMA allocation containing initialized filter and
685 * route table data.  This is used when initializing or resetting the IPA
686 * filter or route table.
687 *
688 * The first entry in a filter table contains a bitmap indicating which
689 * endpoints contain entries in the table.  In addition to that first entry,
690 * there is a fixed maximum number of entries that follow.  Filter table
691 * entries are 64 bits wide, and (other than the bitmap) contain the DMA
692 * address of a filter rule.  A "zero rule" indicates no filtering, and
693 * consists of 64 bits of zeroes.  When a filter table is initialized (or
694 * reset) its entries are made to refer to the zero rule.
695 *
696 * Each entry in a route table is the DMA address of a routing rule.  For
697 * routing there is also a 64-bit "zero rule" that means no routing, and
698 * when a route table is initialized or reset, its entries are made to refer
699 * to the zero rule.  The zero rule is shared for route and filter tables.
700 *
701 *	     +-------------------+
702 *	 --> |     zero rule     |
703 *	/    |-------------------|
704 *	|    |     filter mask   |
705 *	|\   |-------------------|
706 *	| ---- zero rule address | \
707 *	|\   |-------------------|  |
708 *	| ---- zero rule address |  |	Max IPA filter count
709 *	|    |-------------------|   >	or IPA route count,
710 *	|	      ...	    |	whichever is greater
711 *	 \   |-------------------|  |
712 *	  ---- zero rule address | /
713 *	     +-------------------+
714 */
715int ipa_table_init(struct ipa *ipa)
716{
717	struct device *dev = ipa->dev;
718	dma_addr_t addr;
719	__le64 le_addr;
720	__le64 *virt;
721	size_t size;
722	u32 count;
723
724	ipa_table_validate_build();
725
726	count = max_t(u32, ipa->filter_count, ipa->route_count);
727
728	/* The IPA hardware requires route and filter table rules to be
729	 * aligned on a 128-byte boundary.  We put the "zero rule" at the
730	 * base of the table area allocated here.  The DMA address returned
731	 * by dma_alloc_coherent() is guaranteed to be a power-of-2 number
732	 * of pages, which satisfies the rule alignment requirement.
733	 */
734	size = IPA_ZERO_RULE_SIZE + (1 + count) * sizeof(__le64);
735	virt = dma_alloc_coherent(dev, size, &addr, GFP_KERNEL);
736	if (!virt)
737		return -ENOMEM;
738
739	ipa->table_virt = virt;
740	ipa->table_addr = addr;
741
742	/* First slot is the zero rule */
743	*virt++ = 0;
744
745	/* Next is the filter table bitmap.  The "soft" bitmap value might
746	 * need to be converted to the hardware representation by shifting
747	 * it left one position.  Prior to IPA v5.0, bit 0 repesents global
748	 * filtering, which is possible but not used.  IPA v5.0+ eliminated
749	 * that option, so there's no shifting required.
750	 */
751	if (ipa->version < IPA_VERSION_5_0)
752		*virt++ = cpu_to_le64(ipa->filtered << 1);
753	else
754		*virt++ = cpu_to_le64(ipa->filtered);
755
756	/* All the rest contain the DMA address of the zero rule */
757	le_addr = cpu_to_le64(addr);
758	while (count--)
759		*virt++ = le_addr;
760
761	return 0;
762}
763
764void ipa_table_exit(struct ipa *ipa)
765{
766	u32 count = max_t(u32, 1 + ipa->filter_count, ipa->route_count);
767	struct device *dev = ipa->dev;
768	size_t size;
769
770	size = IPA_ZERO_RULE_SIZE + (1 + count) * sizeof(__le64);
771
772	dma_free_coherent(dev, size, ipa->table_virt, ipa->table_addr);
773	ipa->table_addr = 0;
774	ipa->table_virt = NULL;
775}