1 // SPDX-License-Identifier: GPL-2.0
2 
3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4  * Copyright (C) 2018-2022 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_AP_MIN		IPA_ROUTE_MODEM_COUNT
112 #define IPA_ROUTE_AP_COUNT \
113 		(IPA_ROUTE_COUNT_MAX - IPA_ROUTE_MODEM_COUNT)
114 
115 /* Filter or route rules consist of a set of 32-bit values followed by a
116  * 32-bit all-zero rule list terminator.  The "zero rule" is simply an
117  * all-zero rule followed by the list terminator.
118  */
119 #define IPA_ZERO_RULE_SIZE		(2 * sizeof(__le32))
120 
121 /* Check things that can be validated at build time. */
ipa_table_validate_build(void)122 static void ipa_table_validate_build(void)
123 {
124 	/* Filter and route tables contain DMA addresses that refer
125 	 * to filter or route rules.  But the size of a table entry
126 	 * is 64 bits regardless of what the size of an AP DMA address
127 	 * is.  A fixed constant defines the size of an entry, and
128 	 * code in ipa_table_init() uses a pointer to __le64 to
129 	 * initialize tables.
130 	 */
131 	BUILD_BUG_ON(sizeof(dma_addr_t) > sizeof(__le64));
132 
133 	/* A "zero rule" is used to represent no filtering or no routing.
134 	 * It is a 64-bit block of zeroed memory.  Code in ipa_table_init()
135 	 * assumes that it can be written using a pointer to __le64.
136 	 */
137 	BUILD_BUG_ON(IPA_ZERO_RULE_SIZE != sizeof(__le64));
138 
139 	/* Impose a practical limit on the number of routes */
140 	BUILD_BUG_ON(IPA_ROUTE_COUNT_MAX > 32);
141 	/* The modem must be allotted at least one route table entry */
142 	BUILD_BUG_ON(!IPA_ROUTE_MODEM_COUNT);
143 	/* But it can't have more than what is available */
144 	BUILD_BUG_ON(IPA_ROUTE_MODEM_COUNT > IPA_ROUTE_COUNT_MAX);
145 
146 }
147 
148 static bool
ipa_table_valid_one(struct ipa * ipa,enum ipa_mem_id mem_id,bool route)149 ipa_table_valid_one(struct ipa *ipa, enum ipa_mem_id mem_id, bool route)
150 {
151 	const struct ipa_mem *mem = ipa_mem_find(ipa, mem_id);
152 	struct device *dev = &ipa->pdev->dev;
153 	u32 size;
154 
155 	if (route)
156 		size = IPA_ROUTE_COUNT_MAX * sizeof(__le64);
157 	else
158 		size = (1 + IPA_FILTER_COUNT_MAX) * sizeof(__le64);
159 
160 	if (!ipa_cmd_table_valid(ipa, mem, route))
161 		return false;
162 
163 	/* mem->size >= size is sufficient, but we'll demand more */
164 	if (mem->size == size)
165 		return true;
166 
167 	/* Hashed table regions can be zero size if hashing is not supported */
168 	if (ipa_table_hash_support(ipa) && !mem->size)
169 		return true;
170 
171 	dev_err(dev, "%s table region %u size 0x%02x, expected 0x%02x\n",
172 		route ? "route" : "filter", mem_id, mem->size, size);
173 
174 	return false;
175 }
176 
177 /* Verify the filter and route table memory regions are the expected size */
ipa_table_valid(struct ipa * ipa)178 bool ipa_table_valid(struct ipa *ipa)
179 {
180 	bool valid;
181 
182 	valid = ipa_table_valid_one(ipa, IPA_MEM_V4_FILTER, false);
183 	valid = valid && ipa_table_valid_one(ipa, IPA_MEM_V6_FILTER, false);
184 	valid = valid && ipa_table_valid_one(ipa, IPA_MEM_V4_ROUTE, true);
185 	valid = valid && ipa_table_valid_one(ipa, IPA_MEM_V6_ROUTE, true);
186 
187 	if (!ipa_table_hash_support(ipa))
188 		return valid;
189 
190 	valid = valid && ipa_table_valid_one(ipa, IPA_MEM_V4_FILTER_HASHED,
191 					     false);
192 	valid = valid && ipa_table_valid_one(ipa, IPA_MEM_V6_FILTER_HASHED,
193 					     false);
194 	valid = valid && ipa_table_valid_one(ipa, IPA_MEM_V4_ROUTE_HASHED,
195 					     true);
196 	valid = valid && ipa_table_valid_one(ipa, IPA_MEM_V6_ROUTE_HASHED,
197 					     true);
198 
199 	return valid;
200 }
201 
ipa_filter_map_valid(struct ipa * ipa,u32 filter_map)202 bool ipa_filter_map_valid(struct ipa *ipa, u32 filter_map)
203 {
204 	struct device *dev = &ipa->pdev->dev;
205 	u32 count;
206 
207 	if (!filter_map) {
208 		dev_err(dev, "at least one filtering endpoint is required\n");
209 
210 		return false;
211 	}
212 
213 	count = hweight32(filter_map);
214 	if (count > IPA_FILTER_COUNT_MAX) {
215 		dev_err(dev, "too many filtering endpoints (%u, max %u)\n",
216 			count, IPA_FILTER_COUNT_MAX);
217 
218 		return false;
219 	}
220 
221 	return true;
222 }
223 
224 /* Zero entry count means no table, so just return a 0 address */
ipa_table_addr(struct ipa * ipa,bool filter_mask,u16 count)225 static dma_addr_t ipa_table_addr(struct ipa *ipa, bool filter_mask, u16 count)
226 {
227 	u32 skip;
228 
229 	if (!count)
230 		return 0;
231 
232 	WARN_ON(count > max_t(u32, IPA_FILTER_COUNT_MAX, IPA_ROUTE_COUNT_MAX));
233 
234 	/* Skip over the zero rule and possibly the filter mask */
235 	skip = filter_mask ? 1 : 2;
236 
237 	return ipa->table_addr + skip * sizeof(*ipa->table_virt);
238 }
239 
ipa_table_reset_add(struct gsi_trans * trans,bool filter,u16 first,u16 count,enum ipa_mem_id mem_id)240 static void ipa_table_reset_add(struct gsi_trans *trans, bool filter,
241 				u16 first, u16 count, enum ipa_mem_id mem_id)
242 {
243 	struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
244 	const struct ipa_mem *mem = ipa_mem_find(ipa, mem_id);
245 	dma_addr_t addr;
246 	u32 offset;
247 	u16 size;
248 
249 	/* Nothing to do if the table memory region is empty */
250 	if (!mem->size)
251 		return;
252 
253 	if (filter)
254 		first++;	/* skip over bitmap */
255 
256 	offset = mem->offset + first * sizeof(__le64);
257 	size = count * sizeof(__le64);
258 	addr = ipa_table_addr(ipa, false, count);
259 
260 	ipa_cmd_dma_shared_mem_add(trans, offset, size, addr, true);
261 }
262 
263 /* Reset entries in a single filter table belonging to either the AP or
264  * modem to refer to the zero entry.  The memory region supplied will be
265  * for the IPv4 and IPv6 non-hashed and hashed filter tables.
266  */
267 static int
ipa_filter_reset_table(struct ipa * ipa,enum ipa_mem_id mem_id,bool modem)268 ipa_filter_reset_table(struct ipa *ipa, enum ipa_mem_id mem_id, bool modem)
269 {
270 	u32 ep_mask = ipa->filter_map;
271 	u32 count = hweight32(ep_mask);
272 	struct gsi_trans *trans;
273 	enum gsi_ee_id ee_id;
274 
275 	trans = ipa_cmd_trans_alloc(ipa, count);
276 	if (!trans) {
277 		dev_err(&ipa->pdev->dev,
278 			"no transaction for %s filter reset\n",
279 			modem ? "modem" : "AP");
280 		return -EBUSY;
281 	}
282 
283 	ee_id = modem ? GSI_EE_MODEM : GSI_EE_AP;
284 	while (ep_mask) {
285 		u32 endpoint_id = __ffs(ep_mask);
286 		struct ipa_endpoint *endpoint;
287 
288 		ep_mask ^= BIT(endpoint_id);
289 
290 		endpoint = &ipa->endpoint[endpoint_id];
291 		if (endpoint->ee_id != ee_id)
292 			continue;
293 
294 		ipa_table_reset_add(trans, true, endpoint_id, 1, mem_id);
295 	}
296 
297 	gsi_trans_commit_wait(trans);
298 
299 	return 0;
300 }
301 
302 /* Theoretically, each filter table could have more filter slots to
303  * update than the maximum number of commands in a transaction.  So
304  * we do each table separately.
305  */
ipa_filter_reset(struct ipa * ipa,bool modem)306 static int ipa_filter_reset(struct ipa *ipa, bool modem)
307 {
308 	int ret;
309 
310 	ret = ipa_filter_reset_table(ipa, IPA_MEM_V4_FILTER, modem);
311 	if (ret)
312 		return ret;
313 
314 	ret = ipa_filter_reset_table(ipa, IPA_MEM_V4_FILTER_HASHED, modem);
315 	if (ret)
316 		return ret;
317 
318 	ret = ipa_filter_reset_table(ipa, IPA_MEM_V6_FILTER, modem);
319 	if (ret)
320 		return ret;
321 	ret = ipa_filter_reset_table(ipa, IPA_MEM_V6_FILTER_HASHED, modem);
322 
323 	return ret;
324 }
325 
326 /* The AP routes and modem routes are each contiguous within the
327  * table.  We can update each table with a single command, and we
328  * won't exceed the per-transaction command limit.
329  * */
ipa_route_reset(struct ipa * ipa,bool modem)330 static int ipa_route_reset(struct ipa *ipa, bool modem)
331 {
332 	struct gsi_trans *trans;
333 	u16 first;
334 	u16 count;
335 
336 	trans = ipa_cmd_trans_alloc(ipa, 4);
337 	if (!trans) {
338 		dev_err(&ipa->pdev->dev,
339 			"no transaction for %s route reset\n",
340 			modem ? "modem" : "AP");
341 		return -EBUSY;
342 	}
343 
344 	if (modem) {
345 		first = IPA_ROUTE_MODEM_MIN;
346 		count = IPA_ROUTE_MODEM_COUNT;
347 	} else {
348 		first = IPA_ROUTE_AP_MIN;
349 		count = IPA_ROUTE_AP_COUNT;
350 	}
351 
352 	ipa_table_reset_add(trans, false, first, count, IPA_MEM_V4_ROUTE);
353 	ipa_table_reset_add(trans, false, first, count,
354 			    IPA_MEM_V4_ROUTE_HASHED);
355 
356 	ipa_table_reset_add(trans, false, first, count, IPA_MEM_V6_ROUTE);
357 	ipa_table_reset_add(trans, false, first, count,
358 			    IPA_MEM_V6_ROUTE_HASHED);
359 
360 	gsi_trans_commit_wait(trans);
361 
362 	return 0;
363 }
364 
ipa_table_reset(struct ipa * ipa,bool modem)365 void ipa_table_reset(struct ipa *ipa, bool modem)
366 {
367 	struct device *dev = &ipa->pdev->dev;
368 	const char *ee_name;
369 	int ret;
370 
371 	ee_name = modem ? "modem" : "AP";
372 
373 	/* Report errors, but reset filter and route tables */
374 	ret = ipa_filter_reset(ipa, modem);
375 	if (ret)
376 		dev_err(dev, "error %d resetting filter table for %s\n",
377 				ret, ee_name);
378 
379 	ret = ipa_route_reset(ipa, modem);
380 	if (ret)
381 		dev_err(dev, "error %d resetting route table for %s\n",
382 				ret, ee_name);
383 }
384 
ipa_table_hash_flush(struct ipa * ipa)385 int ipa_table_hash_flush(struct ipa *ipa)
386 {
387 	const struct ipa_reg *reg;
388 	struct gsi_trans *trans;
389 	u32 offset;
390 	u32 val;
391 
392 	if (!ipa_table_hash_support(ipa))
393 		return 0;
394 
395 	trans = ipa_cmd_trans_alloc(ipa, 1);
396 	if (!trans) {
397 		dev_err(&ipa->pdev->dev, "no transaction for hash flush\n");
398 		return -EBUSY;
399 	}
400 
401 	reg = ipa_reg(ipa, FILT_ROUT_HASH_FLUSH);
402 	offset = ipa_reg_offset(reg);
403 
404 	val = ipa_reg_bit(reg, IPV6_ROUTER_HASH);
405 	val |= ipa_reg_bit(reg, IPV6_FILTER_HASH);
406 	val |= ipa_reg_bit(reg, IPV4_ROUTER_HASH);
407 	val |= ipa_reg_bit(reg, IPV4_FILTER_HASH);
408 
409 	ipa_cmd_register_write_add(trans, offset, val, val, false);
410 
411 	gsi_trans_commit_wait(trans);
412 
413 	return 0;
414 }
415 
ipa_table_init_add(struct gsi_trans * trans,bool filter,enum ipa_cmd_opcode opcode,enum ipa_mem_id mem_id,enum ipa_mem_id hash_mem_id)416 static void ipa_table_init_add(struct gsi_trans *trans, bool filter,
417 			       enum ipa_cmd_opcode opcode,
418 			       enum ipa_mem_id mem_id,
419 			       enum ipa_mem_id hash_mem_id)
420 {
421 	struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
422 	const struct ipa_mem *hash_mem = ipa_mem_find(ipa, hash_mem_id);
423 	const struct ipa_mem *mem = ipa_mem_find(ipa, mem_id);
424 	dma_addr_t hash_addr;
425 	dma_addr_t addr;
426 	u32 zero_offset;
427 	u16 hash_count;
428 	u32 zero_size;
429 	u16 hash_size;
430 	u16 count;
431 	u16 size;
432 
433 	/* Compute the number of table entries to initialize */
434 	if (filter) {
435 		/* The number of filtering endpoints determines number of
436 		 * entries in the filter table; we also add one more "slot"
437 		 * to hold the bitmap itself.  The size of the hashed filter
438 		 * table is either the same as the non-hashed one, or zero.
439 		 */
440 		count = 1 + hweight32(ipa->filter_map);
441 		hash_count = hash_mem->size ? count : 0;
442 	} else {
443 		/* The size of a route table region determines the number
444 		 * of entries it has.
445 		 */
446 		count = mem->size / sizeof(__le64);
447 		hash_count = hash_mem->size / sizeof(__le64);
448 	}
449 	size = count * sizeof(__le64);
450 	hash_size = hash_count * sizeof(__le64);
451 
452 	addr = ipa_table_addr(ipa, filter, count);
453 	hash_addr = ipa_table_addr(ipa, filter, hash_count);
454 
455 	ipa_cmd_table_init_add(trans, opcode, size, mem->offset, addr,
456 			       hash_size, hash_mem->offset, hash_addr);
457 	if (!filter)
458 		return;
459 
460 	/* Zero the unused space in the filter table */
461 	zero_offset = mem->offset + size;
462 	zero_size = mem->size - size;
463 	ipa_cmd_dma_shared_mem_add(trans, zero_offset, zero_size,
464 				   ipa->zero_addr, true);
465 	if (!hash_size)
466 		return;
467 
468 	/* Zero the unused space in the hashed filter table */
469 	zero_offset = hash_mem->offset + hash_size;
470 	zero_size = hash_mem->size - hash_size;
471 	ipa_cmd_dma_shared_mem_add(trans, zero_offset, zero_size,
472 				   ipa->zero_addr, true);
473 }
474 
ipa_table_setup(struct ipa * ipa)475 int ipa_table_setup(struct ipa *ipa)
476 {
477 	struct gsi_trans *trans;
478 
479 	/* We will need at most 8 TREs:
480 	 * - IPv4:
481 	 *     - One for route table initialization (non-hashed and hashed)
482 	 *     - One for filter table initialization (non-hashed and hashed)
483 	 *     - One to zero unused entries in the non-hashed filter table
484 	 *     - One to zero unused entries in the hashed filter table
485 	 * - IPv6:
486 	 *     - One for route table initialization (non-hashed and hashed)
487 	 *     - One for filter table initialization (non-hashed and hashed)
488 	 *     - One to zero unused entries in the non-hashed filter table
489 	 *     - One to zero unused entries in the hashed filter table
490 	 * All platforms support at least 8 TREs in a transaction.
491 	 */
492 	trans = ipa_cmd_trans_alloc(ipa, 8);
493 	if (!trans) {
494 		dev_err(&ipa->pdev->dev, "no transaction for table setup\n");
495 		return -EBUSY;
496 	}
497 
498 	ipa_table_init_add(trans, false, IPA_CMD_IP_V4_ROUTING_INIT,
499 			   IPA_MEM_V4_ROUTE, IPA_MEM_V4_ROUTE_HASHED);
500 
501 	ipa_table_init_add(trans, false, IPA_CMD_IP_V6_ROUTING_INIT,
502 			   IPA_MEM_V6_ROUTE, IPA_MEM_V6_ROUTE_HASHED);
503 
504 	ipa_table_init_add(trans, true, IPA_CMD_IP_V4_FILTER_INIT,
505 			   IPA_MEM_V4_FILTER, IPA_MEM_V4_FILTER_HASHED);
506 
507 	ipa_table_init_add(trans, true, IPA_CMD_IP_V6_FILTER_INIT,
508 			   IPA_MEM_V6_FILTER, IPA_MEM_V6_FILTER_HASHED);
509 
510 	gsi_trans_commit_wait(trans);
511 
512 	return 0;
513 }
514 
515 /**
516  * ipa_filter_tuple_zero() - Zero an endpoint's hashed filter tuple
517  * @endpoint:	Endpoint whose filter hash tuple should be zeroed
518  *
519  * Endpoint must be for the AP (not modem) and support filtering. Updates
520  * the filter hash values without changing route ones.
521  */
ipa_filter_tuple_zero(struct ipa_endpoint * endpoint)522 static void ipa_filter_tuple_zero(struct ipa_endpoint *endpoint)
523 {
524 	u32 endpoint_id = endpoint->endpoint_id;
525 	struct ipa *ipa = endpoint->ipa;
526 	const struct ipa_reg *reg;
527 	u32 offset;
528 	u32 val;
529 
530 	reg = ipa_reg(ipa, ENDP_FILTER_ROUTER_HSH_CFG);
531 
532 	offset = ipa_reg_n_offset(reg, endpoint_id);
533 	val = ioread32(endpoint->ipa->reg_virt + offset);
534 
535 	/* Zero all filter-related fields, preserving the rest */
536 	val &= ~ipa_reg_fmask(reg, FILTER_HASH_MSK_ALL);
537 
538 	iowrite32(val, endpoint->ipa->reg_virt + offset);
539 }
540 
541 /* Configure a hashed filter table; there is no ipa_filter_deconfig() */
ipa_filter_config(struct ipa * ipa,bool modem)542 static void ipa_filter_config(struct ipa *ipa, bool modem)
543 {
544 	enum gsi_ee_id ee_id = modem ? GSI_EE_MODEM : GSI_EE_AP;
545 	u32 ep_mask = ipa->filter_map;
546 
547 	if (!ipa_table_hash_support(ipa))
548 		return;
549 
550 	while (ep_mask) {
551 		u32 endpoint_id = __ffs(ep_mask);
552 		struct ipa_endpoint *endpoint;
553 
554 		ep_mask ^= BIT(endpoint_id);
555 
556 		endpoint = &ipa->endpoint[endpoint_id];
557 		if (endpoint->ee_id == ee_id)
558 			ipa_filter_tuple_zero(endpoint);
559 	}
560 }
561 
ipa_route_id_modem(u32 route_id)562 static bool ipa_route_id_modem(u32 route_id)
563 {
564 	return route_id >= IPA_ROUTE_MODEM_MIN &&
565 		route_id <= IPA_ROUTE_MODEM_MIN + IPA_ROUTE_MODEM_COUNT - 1;
566 }
567 
568 /**
569  * ipa_route_tuple_zero() - Zero a hashed route table entry tuple
570  * @ipa:	IPA pointer
571  * @route_id:	Route table entry whose hash tuple should be zeroed
572  *
573  * Updates the route hash values without changing filter ones.
574  */
ipa_route_tuple_zero(struct ipa * ipa,u32 route_id)575 static void ipa_route_tuple_zero(struct ipa *ipa, u32 route_id)
576 {
577 	const struct ipa_reg *reg;
578 	u32 offset;
579 	u32 val;
580 
581 	reg = ipa_reg(ipa, ENDP_FILTER_ROUTER_HSH_CFG);
582 	offset = ipa_reg_n_offset(reg, route_id);
583 
584 	val = ioread32(ipa->reg_virt + offset);
585 
586 	/* Zero all route-related fields, preserving the rest */
587 	val &= ~ipa_reg_fmask(reg, ROUTER_HASH_MSK_ALL);
588 
589 	iowrite32(val, ipa->reg_virt + offset);
590 }
591 
592 /* Configure a hashed route table; there is no ipa_route_deconfig() */
ipa_route_config(struct ipa * ipa,bool modem)593 static void ipa_route_config(struct ipa *ipa, bool modem)
594 {
595 	u32 route_id;
596 
597 	if (!ipa_table_hash_support(ipa))
598 		return;
599 
600 	for (route_id = 0; route_id < IPA_ROUTE_COUNT_MAX; route_id++)
601 		if (ipa_route_id_modem(route_id) == modem)
602 			ipa_route_tuple_zero(ipa, route_id);
603 }
604 
605 /* Configure a filter and route tables; there is no ipa_table_deconfig() */
ipa_table_config(struct ipa * ipa)606 void ipa_table_config(struct ipa *ipa)
607 {
608 	ipa_filter_config(ipa, false);
609 	ipa_filter_config(ipa, true);
610 	ipa_route_config(ipa, false);
611 	ipa_route_config(ipa, true);
612 }
613 
614 /*
615  * Initialize a coherent DMA allocation containing initialized filter and
616  * route table data.  This is used when initializing or resetting the IPA
617  * filter or route table.
618  *
619  * The first entry in a filter table contains a bitmap indicating which
620  * endpoints contain entries in the table.  In addition to that first entry,
621  * there are at most IPA_FILTER_COUNT_MAX entries that follow.  Filter table
622  * entries are 64 bits wide, and (other than the bitmap) contain the DMA
623  * address of a filter rule.  A "zero rule" indicates no filtering, and
624  * consists of 64 bits of zeroes.  When a filter table is initialized (or
625  * reset) its entries are made to refer to the zero rule.
626  *
627  * Each entry in a route table is the DMA address of a routing rule.  For
628  * routing there is also a 64-bit "zero rule" that means no routing, and
629  * when a route table is initialized or reset, its entries are made to refer
630  * to the zero rule.  The zero rule is shared for route and filter tables.
631  *
632  * Note that the IPA hardware requires a filter or route rule address to be
633  * aligned on a 128 byte boundary.  The coherent DMA buffer we allocate here
634  * has a minimum alignment, and we place the zero rule at the base of that
635  * allocated space.  In ipa_table_init() we verify the minimum DMA allocation
636  * meets our requirement.
637  *
638  *	     +-------------------+
639  *	 --> |     zero rule     |
640  *	/    |-------------------|
641  *	|    |     filter mask   |
642  *	|\   |-------------------|
643  *	| ---- zero rule address | \
644  *	|\   |-------------------|  |
645  *	| ---- zero rule address |  |	IPA_FILTER_COUNT_MAX
646  *	|    |-------------------|   >	or IPA_ROUTE_COUNT_MAX,
647  *	|	      ...	    |	whichever is greater
648  *	 \   |-------------------|  |
649  *	  ---- zero rule address | /
650  *	     +-------------------+
651  */
ipa_table_init(struct ipa * ipa)652 int ipa_table_init(struct ipa *ipa)
653 {
654 	u32 count = max_t(u32, IPA_FILTER_COUNT_MAX, IPA_ROUTE_COUNT_MAX);
655 	struct device *dev = &ipa->pdev->dev;
656 	dma_addr_t addr;
657 	__le64 le_addr;
658 	__le64 *virt;
659 	size_t size;
660 
661 	ipa_table_validate_build();
662 
663 	/* The IPA hardware requires route and filter table rules to be
664 	 * aligned on a 128-byte boundary.  We put the "zero rule" at the
665 	 * base of the table area allocated here.  The DMA address returned
666 	 * by dma_alloc_coherent() is guaranteed to be a power-of-2 number
667 	 * of pages, which satisfies the rule alignment requirement.
668 	 */
669 	size = IPA_ZERO_RULE_SIZE + (1 + count) * sizeof(__le64);
670 	virt = dma_alloc_coherent(dev, size, &addr, GFP_KERNEL);
671 	if (!virt)
672 		return -ENOMEM;
673 
674 	ipa->table_virt = virt;
675 	ipa->table_addr = addr;
676 
677 	/* First slot is the zero rule */
678 	*virt++ = 0;
679 
680 	/* Next is the filter table bitmap.  The "soft" bitmap value
681 	 * must be converted to the hardware representation by shifting
682 	 * it left one position.  (Bit 0 repesents global filtering,
683 	 * which is possible but not used.)
684 	 */
685 	*virt++ = cpu_to_le64((u64)ipa->filter_map << 1);
686 
687 	/* All the rest contain the DMA address of the zero rule */
688 	le_addr = cpu_to_le64(addr);
689 	while (count--)
690 		*virt++ = le_addr;
691 
692 	return 0;
693 }
694 
ipa_table_exit(struct ipa * ipa)695 void ipa_table_exit(struct ipa *ipa)
696 {
697 	u32 count = max_t(u32, 1 + IPA_FILTER_COUNT_MAX, IPA_ROUTE_COUNT_MAX);
698 	struct device *dev = &ipa->pdev->dev;
699 	size_t size;
700 
701 	size = IPA_ZERO_RULE_SIZE + (1 + count) * sizeof(__le64);
702 
703 	dma_free_coherent(dev, size, ipa->table_virt, ipa->table_addr);
704 	ipa->table_addr = 0;
705 	ipa->table_virt = NULL;
706 }
707