xref: /openbmc/linux/drivers/net/ipa/ipa_table.c (revision aa0dc6a7)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4  * Copyright (C) 2018-2021 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) used for IPv4 and IPv6.  There
36  * are 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.  The low-order bit (bit 0) in this bitmap represents a
55  * special global filter, which applies to all traffic.  This is not
56  * used in the current code.  Bit 1, if set, indicates that there is an
57  * entry (i.e. slot containing a system address referring to a rule) for
58  * endpoint 0 in the table.  Bit 3, if set, indicates there is an entry
59  * for endpoint 2, and so on.  Space is set aside in IPA local memory to
60  * hold as many filter table entries as might be required, but typically
61  * they are not all used.
62  *
63  * The AP initializes all entries in a filter table to refer to a "zero"
64  * entry.  Once initialized the modem and AP update the entries for
65  * endpoints they "own" directly.  Currently the AP does not use the
66  * IPA filtering functionality.
67  *
68  *                    IPA Filter Table
69  *                 ----------------------
70  * endpoint bitmap | 0x0000000000000048 | Bits 3 and 6 set (endpoints 2 and 5)
71  *                 |--------------------|
72  * 1st endpoint    | 0x000123456789abc0 | DMA address for modem endpoint 2 rule
73  *                 |--------------------|
74  * 2nd endpoint    | 0x000123456789abf0 | DMA address for AP endpoint 5 rule
75  *                 |--------------------|
76  * (unused)        |                    | (Unused space in filter table)
77  *                 |--------------------|
78  *                          . . .
79  *                 |--------------------|
80  * (unused)        |                    | (Unused space in filter table)
81  *                 ----------------------
82  *
83  * The set of available route rules is divided about equally between the AP
84  * and modem.  The AP initializes all entries in a route table to refer to
85  * a "zero entry".  Once initialized, the modem and AP are responsible for
86  * updating their own entries.  All entries in a route table are usable,
87  * though the AP currently does not use the IPA routing functionality.
88  *
89  *                    IPA Route Table
90  *                 ----------------------
91  * 1st modem route | 0x0001234500001100 | DMA address for first route rule
92  *                 |--------------------|
93  * 2nd modem route | 0x0001234500001140 | DMA address for second route rule
94  *                 |--------------------|
95  *                          . . .
96  *                 |--------------------|
97  * Last modem route| 0x0001234500002280 | DMA address for Nth route rule
98  *                 |--------------------|
99  * 1st AP route    | 0x0001234500001100 | DMA address for route rule (N+1)
100  *                 |--------------------|
101  * 2nd AP route    | 0x0001234500001140 | DMA address for next route rule
102  *                 |--------------------|
103  *                          . . .
104  *                 |--------------------|
105  * Last AP route   | 0x0001234500002280 | DMA address for last route rule
106  *                 ----------------------
107  */
108 
109 /* Assignment of route table entries to the modem and AP */
110 #define IPA_ROUTE_MODEM_MIN		0
111 #define IPA_ROUTE_MODEM_COUNT		8
112 
113 #define IPA_ROUTE_AP_MIN		IPA_ROUTE_MODEM_COUNT
114 #define IPA_ROUTE_AP_COUNT \
115 		(IPA_ROUTE_COUNT_MAX - IPA_ROUTE_MODEM_COUNT)
116 
117 /* Filter or route rules consist of a set of 32-bit values followed by a
118  * 32-bit all-zero rule list terminator.  The "zero rule" is simply an
119  * all-zero rule followed by the list terminator.
120  */
121 #define IPA_ZERO_RULE_SIZE		(2 * sizeof(__le32))
122 
123 #ifdef IPA_VALIDATE
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 	/* Impose a practical limit on the number of routes */
144 	BUILD_BUG_ON(IPA_ROUTE_COUNT_MAX > 32);
145 	/* The modem must be allotted at least one route table entry */
146 	BUILD_BUG_ON(!IPA_ROUTE_MODEM_COUNT);
147 	/* But it can't have more than what is available */
148 	BUILD_BUG_ON(IPA_ROUTE_MODEM_COUNT > IPA_ROUTE_COUNT_MAX);
149 
150 }
151 
152 static bool
153 ipa_table_valid_one(struct ipa *ipa, enum ipa_mem_id mem_id, bool route)
154 {
155 	const struct ipa_mem *mem = ipa_mem_find(ipa, mem_id);
156 	struct device *dev = &ipa->pdev->dev;
157 	u32 size;
158 
159 	if (route)
160 		size = IPA_ROUTE_COUNT_MAX * sizeof(__le64);
161 	else
162 		size = (1 + IPA_FILTER_COUNT_MAX) * sizeof(__le64);
163 
164 	if (!ipa_cmd_table_valid(ipa, mem, route, ipv6, hashed))
165 		return false;
166 
167 	/* mem->size >= size is sufficient, but we'll demand more */
168 	if (mem->size == size)
169 		return true;
170 
171 	/* Hashed table regions can be zero size if hashing is not supported */
172 	if (hashed && !mem->size)
173 		return true;
174 
175 	dev_err(dev, "%s table region %u size 0x%02x, expected 0x%02x\n",
176 		route ? "route" : "filter", mem_id, mem->size, size);
177 
178 	return false;
179 }
180 
181 /* Verify the filter and route table memory regions are the expected size */
182 bool ipa_table_valid(struct ipa *ipa)
183 {
184 	bool valid;
185 
186 	valid = ipa_table_valid_one(IPA_MEM_V4_FILTER, false);
187 	valid = valid && ipa_table_valid_one(IPA_MEM_V4_FILTER_HASHED, false);
188 	valid = valid && ipa_table_valid_one(IPA_MEM_V6_FILTER, false);
189 	valid = valid && ipa_table_valid_one(IPA_MEM_V6_FILTER_HASHED, false);
190 	valid = valid && ipa_table_valid_one(IPA_MEM_V4_ROUTE, true);
191 	valid = valid && ipa_table_valid_one(IPA_MEM_V4_ROUTE_HASHED, true);
192 	valid = valid && ipa_table_valid_one(IPA_MEM_V6_ROUTE, true);
193 	valid = valid && ipa_table_valid_one(IPA_MEM_V6_ROUTE_HASHED, true);
194 
195 	return valid;
196 }
197 
198 bool ipa_filter_map_valid(struct ipa *ipa, u32 filter_map)
199 {
200 	struct device *dev = &ipa->pdev->dev;
201 	u32 count;
202 
203 	if (!filter_map) {
204 		dev_err(dev, "at least one filtering endpoint is required\n");
205 
206 		return false;
207 	}
208 
209 	count = hweight32(filter_map);
210 	if (count > IPA_FILTER_COUNT_MAX) {
211 		dev_err(dev, "too many filtering endpoints (%u, max %u)\n",
212 			count, IPA_FILTER_COUNT_MAX);
213 
214 		return false;
215 	}
216 
217 	return true;
218 }
219 
220 #else /* !IPA_VALIDATE */
221 static void ipa_table_validate_build(void)
222 
223 {
224 }
225 
226 #endif /* !IPA_VALIDATE */
227 
228 /* Zero entry count means no table, so just return a 0 address */
229 static dma_addr_t ipa_table_addr(struct ipa *ipa, bool filter_mask, u16 count)
230 {
231 	u32 skip;
232 
233 	if (!count)
234 		return 0;
235 
236 /* assert(count <= max_t(u32, IPA_FILTER_COUNT_MAX, IPA_ROUTE_COUNT_MAX)); */
237 
238 	/* Skip over the zero rule and possibly the filter mask */
239 	skip = filter_mask ? 1 : 2;
240 
241 	return ipa->table_addr + skip * sizeof(*ipa->table_virt);
242 }
243 
244 static void ipa_table_reset_add(struct gsi_trans *trans, bool filter,
245 				u16 first, u16 count, enum ipa_mem_id mem_id)
246 {
247 	struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
248 	const struct ipa_mem *mem = ipa_mem_find(ipa, mem_id);
249 	dma_addr_t addr;
250 	u32 offset;
251 	u16 size;
252 
253 	/* Nothing to do if the table memory region is empty */
254 	if (!mem->size)
255 		return;
256 
257 	if (filter)
258 		first++;	/* skip over bitmap */
259 
260 	offset = mem->offset + first * sizeof(__le64);
261 	size = count * sizeof(__le64);
262 	addr = ipa_table_addr(ipa, false, count);
263 
264 	ipa_cmd_dma_shared_mem_add(trans, offset, size, addr, true);
265 }
266 
267 /* Reset entries in a single filter table belonging to either the AP or
268  * modem to refer to the zero entry.  The memory region supplied will be
269  * for the IPv4 and IPv6 non-hashed and hashed filter tables.
270  */
271 static int
272 ipa_filter_reset_table(struct ipa *ipa, enum ipa_mem_id mem_id, bool modem)
273 {
274 	u32 ep_mask = ipa->filter_map;
275 	u32 count = hweight32(ep_mask);
276 	struct gsi_trans *trans;
277 	enum gsi_ee_id ee_id;
278 
279 	trans = ipa_cmd_trans_alloc(ipa, count);
280 	if (!trans) {
281 		dev_err(&ipa->pdev->dev,
282 			"no transaction for %s filter reset\n",
283 			modem ? "modem" : "AP");
284 		return -EBUSY;
285 	}
286 
287 	ee_id = modem ? GSI_EE_MODEM : GSI_EE_AP;
288 	while (ep_mask) {
289 		u32 endpoint_id = __ffs(ep_mask);
290 		struct ipa_endpoint *endpoint;
291 
292 		ep_mask ^= BIT(endpoint_id);
293 
294 		endpoint = &ipa->endpoint[endpoint_id];
295 		if (endpoint->ee_id != ee_id)
296 			continue;
297 
298 		ipa_table_reset_add(trans, true, endpoint_id, 1, mem_id);
299 	}
300 
301 	gsi_trans_commit_wait(trans);
302 
303 	return 0;
304 }
305 
306 /* Theoretically, each filter table could have more filter slots to
307  * update than the maximum number of commands in a transaction.  So
308  * we do each table separately.
309  */
310 static int ipa_filter_reset(struct ipa *ipa, bool modem)
311 {
312 	int ret;
313 
314 	ret = ipa_filter_reset_table(ipa, IPA_MEM_V4_FILTER, modem);
315 	if (ret)
316 		return ret;
317 
318 	ret = ipa_filter_reset_table(ipa, IPA_MEM_V4_FILTER_HASHED, modem);
319 	if (ret)
320 		return ret;
321 
322 	ret = ipa_filter_reset_table(ipa, IPA_MEM_V6_FILTER, modem);
323 	if (ret)
324 		return ret;
325 	ret = ipa_filter_reset_table(ipa, IPA_MEM_V6_FILTER_HASHED, modem);
326 
327 	return ret;
328 }
329 
330 /* The AP routes and modem routes are each contiguous within the
331  * table.  We can update each table with a single command, and we
332  * won't exceed the per-transaction command limit.
333  * */
334 static int ipa_route_reset(struct ipa *ipa, bool modem)
335 {
336 	struct gsi_trans *trans;
337 	u16 first;
338 	u16 count;
339 
340 	trans = ipa_cmd_trans_alloc(ipa, 4);
341 	if (!trans) {
342 		dev_err(&ipa->pdev->dev,
343 			"no transaction for %s route reset\n",
344 			modem ? "modem" : "AP");
345 		return -EBUSY;
346 	}
347 
348 	if (modem) {
349 		first = IPA_ROUTE_MODEM_MIN;
350 		count = IPA_ROUTE_MODEM_COUNT;
351 	} else {
352 		first = IPA_ROUTE_AP_MIN;
353 		count = IPA_ROUTE_AP_COUNT;
354 	}
355 
356 	ipa_table_reset_add(trans, false, first, count, IPA_MEM_V4_ROUTE);
357 	ipa_table_reset_add(trans, false, first, count,
358 			    IPA_MEM_V4_ROUTE_HASHED);
359 
360 	ipa_table_reset_add(trans, false, first, count, IPA_MEM_V6_ROUTE);
361 	ipa_table_reset_add(trans, false, first, count,
362 			    IPA_MEM_V6_ROUTE_HASHED);
363 
364 	gsi_trans_commit_wait(trans);
365 
366 	return 0;
367 }
368 
369 void ipa_table_reset(struct ipa *ipa, bool modem)
370 {
371 	struct device *dev = &ipa->pdev->dev;
372 	const char *ee_name;
373 	int ret;
374 
375 	ee_name = modem ? "modem" : "AP";
376 
377 	/* Report errors, but reset filter and route tables */
378 	ret = ipa_filter_reset(ipa, modem);
379 	if (ret)
380 		dev_err(dev, "error %d resetting filter table for %s\n",
381 				ret, ee_name);
382 
383 	ret = ipa_route_reset(ipa, modem);
384 	if (ret)
385 		dev_err(dev, "error %d resetting route table for %s\n",
386 				ret, ee_name);
387 }
388 
389 int ipa_table_hash_flush(struct ipa *ipa)
390 {
391 	u32 offset = ipa_reg_filt_rout_hash_flush_offset(ipa->version);
392 	struct gsi_trans *trans;
393 	u32 val;
394 
395 	if (!ipa_table_hash_support(ipa))
396 		return 0;
397 
398 	trans = ipa_cmd_trans_alloc(ipa, 1);
399 	if (!trans) {
400 		dev_err(&ipa->pdev->dev, "no transaction for hash flush\n");
401 		return -EBUSY;
402 	}
403 
404 	val = IPV4_FILTER_HASH_FMASK | IPV6_FILTER_HASH_FMASK;
405 	val |= IPV6_ROUTER_HASH_FMASK | IPV4_ROUTER_HASH_FMASK;
406 
407 	ipa_cmd_register_write_add(trans, offset, val, val, false);
408 
409 	gsi_trans_commit_wait(trans);
410 
411 	return 0;
412 }
413 
414 static void ipa_table_init_add(struct gsi_trans *trans, bool filter,
415 			       enum ipa_cmd_opcode opcode,
416 			       enum ipa_mem_id mem_id,
417 			       enum ipa_mem_id hash_mem_id)
418 {
419 	struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
420 	const struct ipa_mem *hash_mem = ipa_mem_find(ipa, hash_mem_id);
421 	const struct ipa_mem *mem = ipa_mem_find(ipa, mem_id);
422 	dma_addr_t hash_addr;
423 	dma_addr_t addr;
424 	u16 hash_count;
425 	u16 hash_size;
426 	u16 count;
427 	u16 size;
428 
429 	/* The number of filtering endpoints determines number of entries
430 	 * in the filter table.  The hashed and non-hashed filter table
431 	 * will have the same number of entries.  The size of the route
432 	 * table region determines the number of entries it has.
433 	 */
434 	if (filter) {
435 		count = hweight32(ipa->filter_map);
436 		hash_count = hash_mem->size ? count : 0;
437 	} else {
438 		count = mem->size / sizeof(__le64);
439 		hash_count = hash_mem->size / sizeof(__le64);
440 	}
441 	size = count * sizeof(__le64);
442 	hash_size = hash_count * sizeof(__le64);
443 
444 	addr = ipa_table_addr(ipa, filter, count);
445 	hash_addr = ipa_table_addr(ipa, filter, hash_count);
446 
447 	ipa_cmd_table_init_add(trans, opcode, size, mem->offset, addr,
448 			       hash_size, hash_mem->offset, hash_addr);
449 }
450 
451 int ipa_table_setup(struct ipa *ipa)
452 {
453 	struct gsi_trans *trans;
454 
455 	trans = ipa_cmd_trans_alloc(ipa, 4);
456 	if (!trans) {
457 		dev_err(&ipa->pdev->dev, "no transaction for table setup\n");
458 		return -EBUSY;
459 	}
460 
461 	ipa_table_init_add(trans, false, IPA_CMD_IP_V4_ROUTING_INIT,
462 			   IPA_MEM_V4_ROUTE, IPA_MEM_V4_ROUTE_HASHED);
463 
464 	ipa_table_init_add(trans, false, IPA_CMD_IP_V6_ROUTING_INIT,
465 			   IPA_MEM_V6_ROUTE, IPA_MEM_V6_ROUTE_HASHED);
466 
467 	ipa_table_init_add(trans, true, IPA_CMD_IP_V4_FILTER_INIT,
468 			   IPA_MEM_V4_FILTER, IPA_MEM_V4_FILTER_HASHED);
469 
470 	ipa_table_init_add(trans, true, IPA_CMD_IP_V6_FILTER_INIT,
471 			   IPA_MEM_V6_FILTER, IPA_MEM_V6_FILTER_HASHED);
472 
473 	gsi_trans_commit_wait(trans);
474 
475 	return 0;
476 }
477 
478 /**
479  * ipa_filter_tuple_zero() - Zero an endpoint's hashed filter tuple
480  * @endpoint:	Endpoint whose filter hash tuple should be zeroed
481  *
482  * Endpoint must be for the AP (not modem) and support filtering. Updates
483  * the filter hash values without changing route ones.
484  */
485 static void ipa_filter_tuple_zero(struct ipa_endpoint *endpoint)
486 {
487 	u32 endpoint_id = endpoint->endpoint_id;
488 	u32 offset;
489 	u32 val;
490 
491 	offset = IPA_REG_ENDP_FILTER_ROUTER_HSH_CFG_N_OFFSET(endpoint_id);
492 
493 	val = ioread32(endpoint->ipa->reg_virt + offset);
494 
495 	/* Zero all filter-related fields, preserving the rest */
496 	u32p_replace_bits(&val, 0, IPA_REG_ENDP_FILTER_HASH_MSK_ALL);
497 
498 	iowrite32(val, endpoint->ipa->reg_virt + offset);
499 }
500 
501 /* Configure a hashed filter table; there is no ipa_filter_deconfig() */
502 static void ipa_filter_config(struct ipa *ipa, bool modem)
503 {
504 	enum gsi_ee_id ee_id = modem ? GSI_EE_MODEM : GSI_EE_AP;
505 	u32 ep_mask = ipa->filter_map;
506 
507 	if (!ipa_table_hash_support(ipa))
508 		return;
509 
510 	while (ep_mask) {
511 		u32 endpoint_id = __ffs(ep_mask);
512 		struct ipa_endpoint *endpoint;
513 
514 		ep_mask ^= BIT(endpoint_id);
515 
516 		endpoint = &ipa->endpoint[endpoint_id];
517 		if (endpoint->ee_id == ee_id)
518 			ipa_filter_tuple_zero(endpoint);
519 	}
520 }
521 
522 static bool ipa_route_id_modem(u32 route_id)
523 {
524 	return route_id >= IPA_ROUTE_MODEM_MIN &&
525 		route_id <= IPA_ROUTE_MODEM_MIN + IPA_ROUTE_MODEM_COUNT - 1;
526 }
527 
528 /**
529  * ipa_route_tuple_zero() - Zero a hashed route table entry tuple
530  * @ipa:	IPA pointer
531  * @route_id:	Route table entry whose hash tuple should be zeroed
532  *
533  * Updates the route hash values without changing filter ones.
534  */
535 static void ipa_route_tuple_zero(struct ipa *ipa, u32 route_id)
536 {
537 	u32 offset = IPA_REG_ENDP_FILTER_ROUTER_HSH_CFG_N_OFFSET(route_id);
538 	u32 val;
539 
540 	val = ioread32(ipa->reg_virt + offset);
541 
542 	/* Zero all route-related fields, preserving the rest */
543 	u32p_replace_bits(&val, 0, IPA_REG_ENDP_ROUTER_HASH_MSK_ALL);
544 
545 	iowrite32(val, ipa->reg_virt + offset);
546 }
547 
548 /* Configure a hashed route table; there is no ipa_route_deconfig() */
549 static void ipa_route_config(struct ipa *ipa, bool modem)
550 {
551 	u32 route_id;
552 
553 	if (!ipa_table_hash_support(ipa))
554 		return;
555 
556 	for (route_id = 0; route_id < IPA_ROUTE_COUNT_MAX; route_id++)
557 		if (ipa_route_id_modem(route_id) == modem)
558 			ipa_route_tuple_zero(ipa, route_id);
559 }
560 
561 /* Configure a filter and route tables; there is no ipa_table_deconfig() */
562 void ipa_table_config(struct ipa *ipa)
563 {
564 	ipa_filter_config(ipa, false);
565 	ipa_filter_config(ipa, true);
566 	ipa_route_config(ipa, false);
567 	ipa_route_config(ipa, true);
568 }
569 
570 /*
571  * Initialize a coherent DMA allocation containing initialized filter and
572  * route table data.  This is used when initializing or resetting the IPA
573  * filter or route table.
574  *
575  * The first entry in a filter table contains a bitmap indicating which
576  * endpoints contain entries in the table.  In addition to that first entry,
577  * there are at most IPA_FILTER_COUNT_MAX entries that follow.  Filter table
578  * entries are 64 bits wide, and (other than the bitmap) contain the DMA
579  * address of a filter rule.  A "zero rule" indicates no filtering, and
580  * consists of 64 bits of zeroes.  When a filter table is initialized (or
581  * reset) its entries are made to refer to the zero rule.
582  *
583  * Each entry in a route table is the DMA address of a routing rule.  For
584  * routing there is also a 64-bit "zero rule" that means no routing, and
585  * when a route table is initialized or reset, its entries are made to refer
586  * to the zero rule.  The zero rule is shared for route and filter tables.
587  *
588  * Note that the IPA hardware requires a filter or route rule address to be
589  * aligned on a 128 byte boundary.  The coherent DMA buffer we allocate here
590  * has a minimum alignment, and we place the zero rule at the base of that
591  * allocated space.  In ipa_table_init() we verify the minimum DMA allocation
592  * meets our requirement.
593  *
594  *	     +-------------------+
595  *	 --> |     zero rule     |
596  *	/    |-------------------|
597  *	|    |     filter mask   |
598  *	|\   |-------------------|
599  *	| ---- zero rule address | \
600  *	|\   |-------------------|  |
601  *	| ---- zero rule address |  |	IPA_FILTER_COUNT_MAX
602  *	|    |-------------------|   >	or IPA_ROUTE_COUNT_MAX,
603  *	|	      ...	    |	whichever is greater
604  *	 \   |-------------------|  |
605  *	  ---- zero rule address | /
606  *	     +-------------------+
607  */
608 int ipa_table_init(struct ipa *ipa)
609 {
610 	u32 count = max_t(u32, IPA_FILTER_COUNT_MAX, IPA_ROUTE_COUNT_MAX);
611 	struct device *dev = &ipa->pdev->dev;
612 	dma_addr_t addr;
613 	__le64 le_addr;
614 	__le64 *virt;
615 	size_t size;
616 
617 	ipa_table_validate_build();
618 
619 	/* The IPA hardware requires route and filter table rules to be
620 	 * aligned on a 128-byte boundary.  We put the "zero rule" at the
621 	 * base of the table area allocated here.  The DMA address returned
622 	 * by dma_alloc_coherent() is guaranteed to be a power-of-2 number
623 	 * of pages, which satisfies the rule alignment requirement.
624 	 */
625 	size = IPA_ZERO_RULE_SIZE + (1 + count) * sizeof(__le64);
626 	virt = dma_alloc_coherent(dev, size, &addr, GFP_KERNEL);
627 	if (!virt)
628 		return -ENOMEM;
629 
630 	ipa->table_virt = virt;
631 	ipa->table_addr = addr;
632 
633 	/* First slot is the zero rule */
634 	*virt++ = 0;
635 
636 	/* Next is the filter table bitmap.  The "soft" bitmap value
637 	 * must be converted to the hardware representation by shifting
638 	 * it left one position.  (Bit 0 repesents global filtering,
639 	 * which is possible but not used.)
640 	 */
641 	*virt++ = cpu_to_le64((u64)ipa->filter_map << 1);
642 
643 	/* All the rest contain the DMA address of the zero rule */
644 	le_addr = cpu_to_le64(addr);
645 	while (count--)
646 		*virt++ = le_addr;
647 
648 	return 0;
649 }
650 
651 void ipa_table_exit(struct ipa *ipa)
652 {
653 	u32 count = max_t(u32, 1 + IPA_FILTER_COUNT_MAX, IPA_ROUTE_COUNT_MAX);
654 	struct device *dev = &ipa->pdev->dev;
655 	size_t size;
656 
657 	size = IPA_ZERO_RULE_SIZE + (1 + count) * sizeof(__le64);
658 
659 	dma_free_coherent(dev, size, ipa->table_virt, ipa->table_addr);
660 	ipa->table_addr = 0;
661 	ipa->table_virt = NULL;
662 }
663