ipa_table.c 22 KB

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