xref: /linux/drivers/net/ipa/ipa_table.c (revision 57b30d2a5475eb837fe1124f15f93a2a230c7bf3)
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/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. */
126 static 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 
144 static const struct ipa_mem *
145 ipa_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 bool ipa_filtered_valid(struct ipa *ipa, u64 filtered)
162 {
163 	struct device *dev = ipa->dev;
164 	u32 count;
165 
166 	if (!filtered) {
167 		dev_err(dev, "at least one filtering endpoint is required\n");
168 
169 		return false;
170 	}
171 
172 	count = hweight64(filtered);
173 	if (count > ipa->filter_count) {
174 		dev_err(dev, "too many filtering endpoints (%u > %u)\n",
175 			count, ipa->filter_count);
176 
177 		return false;
178 	}
179 
180 	return true;
181 }
182 
183 /* Zero entry count means no table, so just return a 0 address */
184 static dma_addr_t ipa_table_addr(struct ipa *ipa, bool filter_mask, u16 count)
185 {
186 	u32 skip;
187 
188 	if (!count)
189 		return 0;
190 
191 	WARN_ON(count > max_t(u32, ipa->filter_count, ipa->route_count));
192 
193 	/* Skip over the zero rule and possibly the filter mask */
194 	skip = filter_mask ? 1 : 2;
195 
196 	return ipa->table_addr + skip * sizeof(*ipa->table_virt);
197 }
198 
199 static void ipa_table_reset_add(struct gsi_trans *trans, bool filter,
200 				bool hashed, bool ipv6, u16 first, u16 count)
201 {
202 	struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
203 	const struct ipa_mem *mem;
204 	dma_addr_t addr;
205 	u32 offset;
206 	u16 size;
207 
208 	/* Nothing to do if the memory region is doesn't exist or is empty */
209 	mem = ipa_table_mem(ipa, filter, hashed, ipv6);
210 	if (!mem || !mem->size)
211 		return;
212 
213 	if (filter)
214 		first++;	/* skip over bitmap */
215 
216 	offset = mem->offset + first * sizeof(__le64);
217 	size = count * sizeof(__le64);
218 	addr = ipa_table_addr(ipa, false, count);
219 
220 	ipa_cmd_dma_shared_mem_add(trans, offset, size, addr, true);
221 }
222 
223 /* Reset entries in a single filter table belonging to either the AP or
224  * modem to refer to the zero entry.  The memory region supplied will be
225  * for the IPv4 and IPv6 non-hashed and hashed filter tables.
226  */
227 static int
228 ipa_filter_reset_table(struct ipa *ipa, bool hashed, bool ipv6, bool modem)
229 {
230 	u64 ep_mask = ipa->filtered;
231 	struct gsi_trans *trans;
232 	enum gsi_ee_id ee_id;
233 
234 	trans = ipa_cmd_trans_alloc(ipa, hweight64(ep_mask));
235 	if (!trans) {
236 		dev_err(ipa->dev, "no transaction for %s filter reset\n",
237 			modem ? "modem" : "AP");
238 		return -EBUSY;
239 	}
240 
241 	ee_id = modem ? GSI_EE_MODEM : GSI_EE_AP;
242 	while (ep_mask) {
243 		u32 endpoint_id = __ffs(ep_mask);
244 		struct ipa_endpoint *endpoint;
245 
246 		ep_mask ^= BIT(endpoint_id);
247 
248 		endpoint = &ipa->endpoint[endpoint_id];
249 		if (endpoint->ee_id != ee_id)
250 			continue;
251 
252 		ipa_table_reset_add(trans, true, hashed, ipv6, endpoint_id, 1);
253 	}
254 
255 	gsi_trans_commit_wait(trans);
256 
257 	return 0;
258 }
259 
260 /* Theoretically, each filter table could have more filter slots to
261  * update than the maximum number of commands in a transaction.  So
262  * we do each table separately.
263  */
264 static int ipa_filter_reset(struct ipa *ipa, bool modem)
265 {
266 	int ret;
267 
268 	ret = ipa_filter_reset_table(ipa, false, false, modem);
269 	if (ret)
270 		return ret;
271 
272 	ret = ipa_filter_reset_table(ipa, false, true, modem);
273 	if (ret || !ipa_table_hash_support(ipa))
274 		return ret;
275 
276 	ret = ipa_filter_reset_table(ipa, true, false, modem);
277 	if (ret)
278 		return ret;
279 
280 	return ipa_filter_reset_table(ipa, true, true, modem);
281 }
282 
283 /* The AP routes and modem routes are each contiguous within the
284  * table.  We can update each table with a single command, and we
285  * won't exceed the per-transaction command limit.
286  * */
287 static int ipa_route_reset(struct ipa *ipa, bool modem)
288 {
289 	bool hash_support = ipa_table_hash_support(ipa);
290 	u32 modem_route_count = ipa->modem_route_count;
291 	struct gsi_trans *trans;
292 	u16 first;
293 	u16 count;
294 
295 	trans = ipa_cmd_trans_alloc(ipa, hash_support ? 4 : 2);
296 	if (!trans) {
297 		dev_err(ipa->dev, "no transaction for %s route reset\n",
298 			modem ? "modem" : "AP");
299 		return -EBUSY;
300 	}
301 
302 	if (modem) {
303 		first = 0;
304 		count = modem_route_count;
305 	} else {
306 		first = modem_route_count;
307 		count = ipa->route_count - modem_route_count;
308 	}
309 
310 	ipa_table_reset_add(trans, false, false, false, first, count);
311 	ipa_table_reset_add(trans, false, false, true, first, count);
312 
313 	if (hash_support) {
314 		ipa_table_reset_add(trans, false, true, false, first, count);
315 		ipa_table_reset_add(trans, false, true, true, first, count);
316 	}
317 
318 	gsi_trans_commit_wait(trans);
319 
320 	return 0;
321 }
322 
323 void ipa_table_reset(struct ipa *ipa, bool modem)
324 {
325 	struct device *dev = ipa->dev;
326 	const char *ee_name;
327 	int ret;
328 
329 	ee_name = modem ? "modem" : "AP";
330 
331 	/* Report errors, but reset filter and route tables */
332 	ret = ipa_filter_reset(ipa, modem);
333 	if (ret)
334 		dev_err(dev, "error %d resetting filter table for %s\n",
335 				ret, ee_name);
336 
337 	ret = ipa_route_reset(ipa, modem);
338 	if (ret)
339 		dev_err(dev, "error %d resetting route table for %s\n",
340 				ret, ee_name);
341 }
342 
343 int ipa_table_hash_flush(struct ipa *ipa)
344 {
345 	struct gsi_trans *trans;
346 	const struct reg *reg;
347 	u32 val;
348 
349 	if (!ipa_table_hash_support(ipa))
350 		return 0;
351 
352 	trans = ipa_cmd_trans_alloc(ipa, 1);
353 	if (!trans) {
354 		dev_err(ipa->dev, "no transaction for hash flush\n");
355 		return -EBUSY;
356 	}
357 
358 	if (ipa->version < IPA_VERSION_5_0) {
359 		reg = ipa_reg(ipa, FILT_ROUT_HASH_FLUSH);
360 
361 		val = reg_bit(reg, IPV6_ROUTER_HASH);
362 		val |= reg_bit(reg, IPV6_FILTER_HASH);
363 		val |= reg_bit(reg, IPV4_ROUTER_HASH);
364 		val |= reg_bit(reg, IPV4_FILTER_HASH);
365 	} else {
366 		reg = ipa_reg(ipa, FILT_ROUT_CACHE_FLUSH);
367 
368 		/* IPA v5.0+ uses a unified cache (both IPv4 and IPv6) */
369 		val = reg_bit(reg, ROUTER_CACHE);
370 		val |= reg_bit(reg, FILTER_CACHE);
371 	}
372 
373 	ipa_cmd_register_write_add(trans, reg_offset(reg), val, val, false);
374 
375 	gsi_trans_commit_wait(trans);
376 
377 	return 0;
378 }
379 
380 static void ipa_table_init_add(struct gsi_trans *trans, bool filter, bool ipv6)
381 {
382 	struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
383 	const struct ipa_mem *hash_mem;
384 	enum ipa_cmd_opcode opcode;
385 	const struct ipa_mem *mem;
386 	dma_addr_t hash_addr;
387 	dma_addr_t addr;
388 	u32 hash_offset;
389 	u32 zero_offset;
390 	u16 hash_count;
391 	u32 zero_size;
392 	u16 hash_size;
393 	u16 count;
394 	u16 size;
395 
396 	opcode = filter ? ipv6 ? IPA_CMD_IP_V6_FILTER_INIT
397 			       : IPA_CMD_IP_V4_FILTER_INIT
398 			: ipv6 ? IPA_CMD_IP_V6_ROUTING_INIT
399 			       : IPA_CMD_IP_V4_ROUTING_INIT;
400 
401 	/* The non-hashed region will exist (see ipa_table_mem_valid()) */
402 	mem = ipa_table_mem(ipa, filter, false, ipv6);
403 	hash_mem = ipa_table_mem(ipa, filter, true, ipv6);
404 	hash_offset = hash_mem ? hash_mem->offset : 0;
405 
406 	/* Compute the number of table entries to initialize */
407 	if (filter) {
408 		/* The number of filtering endpoints determines number of
409 		 * entries in the filter table; we also add one more "slot"
410 		 * to hold the bitmap itself.  The size of the hashed filter
411 		 * table is either the same as the non-hashed one, or zero.
412 		 */
413 		count = 1 + hweight64(ipa->filtered);
414 		hash_count = hash_mem && hash_mem->size ? count : 0;
415 	} else {
416 		/* The size of a route table region determines the number
417 		 * of entries it has.
418 		 */
419 		count = mem->size / sizeof(__le64);
420 		hash_count = hash_mem ? hash_mem->size / sizeof(__le64) : 0;
421 	}
422 	size = count * sizeof(__le64);
423 	hash_size = hash_count * sizeof(__le64);
424 
425 	addr = ipa_table_addr(ipa, filter, count);
426 	hash_addr = ipa_table_addr(ipa, filter, hash_count);
427 
428 	ipa_cmd_table_init_add(trans, opcode, size, mem->offset, addr,
429 			       hash_size, hash_offset, hash_addr);
430 	if (!filter)
431 		return;
432 
433 	/* Zero the unused space in the filter table */
434 	zero_offset = mem->offset + size;
435 	zero_size = mem->size - size;
436 	ipa_cmd_dma_shared_mem_add(trans, zero_offset, zero_size,
437 				   ipa->zero_addr, true);
438 	if (!hash_size)
439 		return;
440 
441 	/* Zero the unused space in the hashed filter table */
442 	zero_offset = hash_offset + hash_size;
443 	zero_size = hash_mem->size - hash_size;
444 	ipa_cmd_dma_shared_mem_add(trans, zero_offset, zero_size,
445 				   ipa->zero_addr, true);
446 }
447 
448 int ipa_table_setup(struct ipa *ipa)
449 {
450 	struct gsi_trans *trans;
451 
452 	/* We will need at most 8 TREs:
453 	 * - IPv4:
454 	 *     - One for route table initialization (non-hashed and hashed)
455 	 *     - One for filter table initialization (non-hashed and hashed)
456 	 *     - One to zero unused entries in the non-hashed filter table
457 	 *     - One to zero unused entries in the hashed filter table
458 	 * - IPv6:
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 	 * All platforms support at least 8 TREs in a transaction.
464 	 */
465 	trans = ipa_cmd_trans_alloc(ipa, 8);
466 	if (!trans) {
467 		dev_err(ipa->dev, "no transaction for table setup\n");
468 		return -EBUSY;
469 	}
470 
471 	ipa_table_init_add(trans, false, false);
472 	ipa_table_init_add(trans, false, true);
473 	ipa_table_init_add(trans, true, false);
474 	ipa_table_init_add(trans, true, true);
475 
476 	gsi_trans_commit_wait(trans);
477 
478 	return 0;
479 }
480 
481 /**
482  * ipa_filter_tuple_zero() - Zero an endpoint's hashed filter tuple
483  * @endpoint:	Endpoint whose filter hash tuple should be zeroed
484  *
485  * Endpoint must be for the AP (not modem) and support filtering. Updates
486  * the filter hash values without changing route ones.
487  */
488 static void ipa_filter_tuple_zero(struct ipa_endpoint *endpoint)
489 {
490 	u32 endpoint_id = endpoint->endpoint_id;
491 	struct ipa *ipa = endpoint->ipa;
492 	const struct reg *reg;
493 	u32 offset;
494 	u32 val;
495 
496 	if (ipa->version < IPA_VERSION_5_0) {
497 		reg = ipa_reg(ipa, ENDP_FILTER_ROUTER_HSH_CFG);
498 
499 		offset = reg_n_offset(reg, endpoint_id);
500 		val = ioread32(endpoint->ipa->reg_virt + offset);
501 
502 		/* Zero all filter-related fields, preserving the rest */
503 		val &= ~reg_fmask(reg, FILTER_HASH_MSK_ALL);
504 	} else {
505 		/* IPA v5.0 separates filter and router cache configuration */
506 		reg = ipa_reg(ipa, ENDP_FILTER_CACHE_CFG);
507 		offset = reg_n_offset(reg, endpoint_id);
508 
509 		/* Zero all filter-related fields */
510 		val = 0;
511 	}
512 
513 	iowrite32(val, endpoint->ipa->reg_virt + offset);
514 }
515 
516 /* Configure a hashed filter table; there is no ipa_filter_deconfig() */
517 static void ipa_filter_config(struct ipa *ipa, bool modem)
518 {
519 	enum gsi_ee_id ee_id = modem ? GSI_EE_MODEM : GSI_EE_AP;
520 	u64 ep_mask = ipa->filtered;
521 
522 	if (!ipa_table_hash_support(ipa))
523 		return;
524 
525 	while (ep_mask) {
526 		u32 endpoint_id = __ffs(ep_mask);
527 		struct ipa_endpoint *endpoint;
528 
529 		ep_mask ^= BIT(endpoint_id);
530 
531 		endpoint = &ipa->endpoint[endpoint_id];
532 		if (endpoint->ee_id == ee_id)
533 			ipa_filter_tuple_zero(endpoint);
534 	}
535 }
536 
537 static bool ipa_route_id_modem(struct ipa *ipa, u32 route_id)
538 {
539 	return route_id < ipa->modem_route_count;
540 }
541 
542 /**
543  * ipa_route_tuple_zero() - Zero a hashed route table entry tuple
544  * @ipa:	IPA pointer
545  * @route_id:	Route table entry whose hash tuple should be zeroed
546  *
547  * Updates the route hash values without changing filter ones.
548  */
549 static void ipa_route_tuple_zero(struct ipa *ipa, u32 route_id)
550 {
551 	const struct reg *reg;
552 	u32 offset;
553 	u32 val;
554 
555 	if (ipa->version < IPA_VERSION_5_0) {
556 		reg = ipa_reg(ipa, ENDP_FILTER_ROUTER_HSH_CFG);
557 		offset = reg_n_offset(reg, route_id);
558 
559 		val = ioread32(ipa->reg_virt + offset);
560 
561 		/* Zero all route-related fields, preserving the rest */
562 		val &= ~reg_fmask(reg, ROUTER_HASH_MSK_ALL);
563 	} else {
564 		/* IPA v5.0 separates filter and router cache configuration */
565 		reg = ipa_reg(ipa, ENDP_ROUTER_CACHE_CFG);
566 		offset = reg_n_offset(reg, route_id);
567 
568 		/* Zero all route-related fields */
569 		val = 0;
570 	}
571 
572 	iowrite32(val, ipa->reg_virt + offset);
573 }
574 
575 /* Configure a hashed route table; there is no ipa_route_deconfig() */
576 static void ipa_route_config(struct ipa *ipa, bool modem)
577 {
578 	u32 route_id;
579 
580 	if (!ipa_table_hash_support(ipa))
581 		return;
582 
583 	for (route_id = 0; route_id < ipa->route_count; route_id++)
584 		if (ipa_route_id_modem(ipa, route_id) == modem)
585 			ipa_route_tuple_zero(ipa, route_id);
586 }
587 
588 /* Configure a filter and route tables; there is no ipa_table_deconfig() */
589 void ipa_table_config(struct ipa *ipa)
590 {
591 	ipa_filter_config(ipa, false);
592 	ipa_filter_config(ipa, true);
593 	ipa_route_config(ipa, false);
594 	ipa_route_config(ipa, true);
595 }
596 
597 /* Verify the sizes of all IPA table filter or routing table memory regions
598  * are valid.  If valid, this records the size of the routing table.
599  */
600 bool ipa_table_mem_valid(struct ipa *ipa, bool filter)
601 {
602 	bool hash_support = ipa_table_hash_support(ipa);
603 	const struct ipa_mem *mem_hashed;
604 	const struct ipa_mem *mem_ipv4;
605 	const struct ipa_mem *mem_ipv6;
606 	u32 count;
607 
608 	/* IPv4 and IPv6 non-hashed tables are expected to be defined and
609 	 * have the same size.  Both must have at least two entries (and
610 	 * would normally have more than that).
611 	 */
612 	mem_ipv4 = ipa_table_mem(ipa, filter, false, false);
613 	if (!mem_ipv4)
614 		return false;
615 
616 	mem_ipv6 = ipa_table_mem(ipa, filter, false, true);
617 	if (!mem_ipv6)
618 		return false;
619 
620 	if (mem_ipv4->size != mem_ipv6->size)
621 		return false;
622 
623 	/* Compute and record the number of entries for each table type */
624 	count = mem_ipv4->size / sizeof(__le64);
625 	if (count < 2)
626 		return false;
627 	if (filter)
628 		ipa->filter_count = count - 1;	/* Filter map in first entry */
629 	else
630 		ipa->route_count = count;
631 
632 	/* Table offset and size must fit in TABLE_INIT command fields */
633 	if (!ipa_cmd_table_init_valid(ipa, mem_ipv4, !filter))
634 		return false;
635 
636 	/* Make sure the regions are big enough */
637 	if (filter) {
638 		/* Filter tables must able to hold the endpoint bitmap plus
639 		 * an entry for each endpoint that supports filtering
640 		 */
641 		if (count < 1 + hweight64(ipa->filtered))
642 			return false;
643 	} else {
644 		/* Routing tables must be able to hold all modem entries,
645 		 * plus at least one entry for the AP.
646 		 */
647 		if (count < ipa->modem_route_count + 1)
648 			return false;
649 	}
650 
651 	/* If hashing is supported, hashed tables are expected to be defined,
652 	 * and have the same size as non-hashed tables.  If hashing is not
653 	 * supported, hashed tables are expected to have zero size (or not
654 	 * be defined).
655 	 */
656 	mem_hashed = ipa_table_mem(ipa, filter, true, false);
657 	if (hash_support) {
658 		if (!mem_hashed || mem_hashed->size != mem_ipv4->size)
659 			return false;
660 	} else {
661 		if (mem_hashed && mem_hashed->size)
662 			return false;
663 	}
664 
665 	/* Same check for IPv6 tables */
666 	mem_hashed = ipa_table_mem(ipa, filter, true, true);
667 	if (hash_support) {
668 		if (!mem_hashed || mem_hashed->size != mem_ipv6->size)
669 			return false;
670 	} else {
671 		if (mem_hashed && mem_hashed->size)
672 			return false;
673 	}
674 
675 	return true;
676 }
677 
678 /* Initialize a coherent DMA allocation containing initialized filter and
679  * route table data.  This is used when initializing or resetting the IPA
680  * filter or route table.
681  *
682  * The first entry in a filter table contains a bitmap indicating which
683  * endpoints contain entries in the table.  In addition to that first entry,
684  * there is a fixed maximum number of entries that follow.  Filter table
685  * entries are 64 bits wide, and (other than the bitmap) contain the DMA
686  * address of a filter rule.  A "zero rule" indicates no filtering, and
687  * consists of 64 bits of zeroes.  When a filter table is initialized (or
688  * reset) its entries are made to refer to the zero rule.
689  *
690  * Each entry in a route table is the DMA address of a routing rule.  For
691  * routing there is also a 64-bit "zero rule" that means no routing, and
692  * when a route table is initialized or reset, its entries are made to refer
693  * to the zero rule.  The zero rule is shared for route and filter tables.
694  *
695  *	     +-------------------+
696  *	 --> |     zero rule     |
697  *	/    |-------------------|
698  *	|    |     filter mask   |
699  *	|\   |-------------------|
700  *	| ---- zero rule address | \
701  *	|\   |-------------------|  |
702  *	| ---- zero rule address |  |	Max IPA filter count
703  *	|    |-------------------|   >	or IPA route count,
704  *	|	      ...	    |	whichever is greater
705  *	 \   |-------------------|  |
706  *	  ---- zero rule address | /
707  *	     +-------------------+
708  */
709 int ipa_table_init(struct ipa *ipa)
710 {
711 	struct device *dev = ipa->dev;
712 	dma_addr_t addr;
713 	__le64 le_addr;
714 	__le64 *virt;
715 	size_t size;
716 	u32 count;
717 
718 	ipa_table_validate_build();
719 
720 	count = max_t(u32, ipa->filter_count, ipa->route_count);
721 
722 	/* The IPA hardware requires route and filter table rules to be
723 	 * aligned on a 128-byte boundary.  We put the "zero rule" at the
724 	 * base of the table area allocated here.  The DMA address returned
725 	 * by dma_alloc_coherent() is guaranteed to be a power-of-2 number
726 	 * of pages, which satisfies the rule alignment requirement.
727 	 */
728 	size = IPA_ZERO_RULE_SIZE + (1 + count) * sizeof(__le64);
729 	virt = dma_alloc_coherent(dev, size, &addr, GFP_KERNEL);
730 	if (!virt)
731 		return -ENOMEM;
732 
733 	ipa->table_virt = virt;
734 	ipa->table_addr = addr;
735 
736 	/* First slot is the zero rule */
737 	*virt++ = 0;
738 
739 	/* Next is the filter table bitmap.  The "soft" bitmap value might
740 	 * need to be converted to the hardware representation by shifting
741 	 * it left one position.  Prior to IPA v5.0, bit 0 repesents global
742 	 * filtering, which is possible but not used.  IPA v5.0+ eliminated
743 	 * that option, so there's no shifting required.
744 	 */
745 	if (ipa->version < IPA_VERSION_5_0)
746 		*virt++ = cpu_to_le64(ipa->filtered << 1);
747 	else
748 		*virt++ = cpu_to_le64(ipa->filtered);
749 
750 	/* All the rest contain the DMA address of the zero rule */
751 	le_addr = cpu_to_le64(addr);
752 	while (count--)
753 		*virt++ = le_addr;
754 
755 	return 0;
756 }
757 
758 void ipa_table_exit(struct ipa *ipa)
759 {
760 	u32 count = max_t(u32, 1 + ipa->filter_count, ipa->route_count);
761 	struct device *dev = ipa->dev;
762 	size_t size;
763 
764 	size = IPA_ZERO_RULE_SIZE + (1 + count) * sizeof(__le64);
765 
766 	dma_free_coherent(dev, size, ipa->table_virt, ipa->table_addr);
767 	ipa->table_addr = 0;
768 	ipa->table_virt = NULL;
769 }
770