ipa_table.c 24 KB

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