ipa_cmd.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2019-2024 Linaro Ltd.
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/bits.h>
  7. #include <linux/device.h>
  8. #include <linux/dma-direction.h>
  9. #include <linux/types.h>
  10. #include "gsi.h"
  11. #include "gsi_trans.h"
  12. #include "ipa.h"
  13. #include "ipa_cmd.h"
  14. #include "ipa_endpoint.h"
  15. #include "ipa_mem.h"
  16. #include "ipa_reg.h"
  17. #include "ipa_table.h"
  18. /**
  19. * DOC: IPA Immediate Commands
  20. *
  21. * The AP command TX endpoint is used to issue immediate commands to the IPA.
  22. * An immediate command is generally used to request the IPA do something
  23. * other than data transfer to another endpoint.
  24. *
  25. * Immediate commands are represented by GSI transactions just like other
  26. * transfer requests, and use a single GSI TRE. Each immediate command
  27. * has a well-defined format, having a payload of a known length. This
  28. * allows the transfer element's length field to be used to hold an
  29. * immediate command's opcode. The payload for a command resides in AP
  30. * memory and is described by a single scatterlist entry in its transaction.
  31. * Commands do not require a transaction completion callback, and are
  32. * always issued using gsi_trans_commit_wait().
  33. */
  34. /* Some commands can wait until indicated pipeline stages are clear */
  35. enum pipeline_clear_options {
  36. pipeline_clear_hps = 0x0,
  37. pipeline_clear_src_grp = 0x1,
  38. pipeline_clear_full = 0x2,
  39. };
  40. /* IPA_CMD_IP_V{4,6}_{FILTER,ROUTING}_INIT */
  41. struct ipa_cmd_hw_ip_fltrt_init {
  42. __le64 hash_rules_addr;
  43. __le64 flags;
  44. __le64 nhash_rules_addr;
  45. };
  46. /* Field masks for ipa_cmd_hw_ip_fltrt_init structure fields */
  47. #define IP_FLTRT_FLAGS_HASH_SIZE_FMASK GENMASK_ULL(11, 0)
  48. #define IP_FLTRT_FLAGS_HASH_ADDR_FMASK GENMASK_ULL(27, 12)
  49. #define IP_FLTRT_FLAGS_NHASH_SIZE_FMASK GENMASK_ULL(39, 28)
  50. #define IP_FLTRT_FLAGS_NHASH_ADDR_FMASK GENMASK_ULL(55, 40)
  51. /* IPA_CMD_HDR_INIT_LOCAL */
  52. struct ipa_cmd_hw_hdr_init_local {
  53. __le64 hdr_table_addr;
  54. __le32 flags;
  55. __le32 reserved;
  56. };
  57. /* Field masks for ipa_cmd_hw_hdr_init_local structure fields */
  58. #define HDR_INIT_LOCAL_FLAGS_TABLE_SIZE_FMASK GENMASK(11, 0)
  59. #define HDR_INIT_LOCAL_FLAGS_HDR_ADDR_FMASK GENMASK(27, 12)
  60. /* IPA_CMD_REGISTER_WRITE */
  61. /* For IPA v4.0+, the pipeline clear options are encoded in the opcode */
  62. #define REGISTER_WRITE_OPCODE_SKIP_CLEAR_FMASK GENMASK(8, 8)
  63. #define REGISTER_WRITE_OPCODE_CLEAR_OPTION_FMASK GENMASK(10, 9)
  64. struct ipa_cmd_register_write {
  65. __le16 flags; /* Unused/reserved prior to IPA v4.0 */
  66. __le16 offset;
  67. __le32 value;
  68. __le32 value_mask;
  69. __le32 clear_options; /* Unused/reserved for IPA v4.0+ */
  70. };
  71. /* Field masks for ipa_cmd_register_write structure fields */
  72. /* The next field is present for IPA v4.0+ */
  73. #define REGISTER_WRITE_FLAGS_OFFSET_HIGH_FMASK GENMASK(14, 11)
  74. /* The next field is not present for IPA v4.0+ */
  75. #define REGISTER_WRITE_FLAGS_SKIP_CLEAR_FMASK GENMASK(15, 15)
  76. /* The next field and its values are not present for IPA v4.0+ */
  77. #define REGISTER_WRITE_CLEAR_OPTIONS_FMASK GENMASK(1, 0)
  78. /* IPA_CMD_IP_PACKET_INIT */
  79. struct ipa_cmd_ip_packet_init {
  80. u8 dest_endpoint; /* Full 8 bits used for IPA v5.0+ */
  81. u8 reserved[7];
  82. };
  83. /* Field mask for ipa_cmd_ip_packet_init dest_endpoint field (unused v5.0+) */
  84. #define IPA_PACKET_INIT_DEST_ENDPOINT_FMASK GENMASK(4, 0)
  85. /* IPA_CMD_DMA_SHARED_MEM */
  86. /* For IPA v4.0+, this opcode gets modified with pipeline clear options */
  87. #define DMA_SHARED_MEM_OPCODE_SKIP_CLEAR_FMASK GENMASK(8, 8)
  88. #define DMA_SHARED_MEM_OPCODE_CLEAR_OPTION_FMASK GENMASK(10, 9)
  89. struct ipa_cmd_hw_dma_mem_mem {
  90. __le16 clear_after_read; /* 0 or DMA_SHARED_MEM_CLEAR_AFTER_READ */
  91. __le16 size;
  92. __le16 local_addr;
  93. __le16 flags;
  94. __le64 system_addr;
  95. };
  96. /* Flag allowing atomic clear of target region after reading data (v4.0+)*/
  97. #define DMA_SHARED_MEM_CLEAR_AFTER_READ GENMASK(15, 15)
  98. /* Field masks for ipa_cmd_hw_dma_mem_mem structure fields */
  99. #define DMA_SHARED_MEM_FLAGS_DIRECTION_FMASK GENMASK(0, 0)
  100. /* The next two fields are not present for IPA v4.0+ */
  101. #define DMA_SHARED_MEM_FLAGS_SKIP_CLEAR_FMASK GENMASK(1, 1)
  102. #define DMA_SHARED_MEM_FLAGS_CLEAR_OPTIONS_FMASK GENMASK(3, 2)
  103. /* IPA_CMD_IP_PACKET_TAG_STATUS */
  104. struct ipa_cmd_ip_packet_tag_status {
  105. __le64 tag;
  106. };
  107. #define IP_PACKET_TAG_STATUS_TAG_FMASK GENMASK_ULL(63, 16)
  108. /* Immediate command payload */
  109. union ipa_cmd_payload {
  110. struct ipa_cmd_hw_ip_fltrt_init table_init;
  111. struct ipa_cmd_hw_hdr_init_local hdr_init_local;
  112. struct ipa_cmd_register_write register_write;
  113. struct ipa_cmd_ip_packet_init ip_packet_init;
  114. struct ipa_cmd_hw_dma_mem_mem dma_shared_mem;
  115. struct ipa_cmd_ip_packet_tag_status ip_packet_tag_status;
  116. };
  117. static void ipa_cmd_validate_build(void)
  118. {
  119. /* The size of a filter table needs to fit into fields in the
  120. * ipa_cmd_hw_ip_fltrt_init structure. Although hashed tables
  121. * might not be used, non-hashed and hashed tables have the same
  122. * maximum size. IPv4 and IPv6 filter tables have the same number
  123. * of entries.
  124. */
  125. /* Hashed and non-hashed fields are assumed to be the same size */
  126. BUILD_BUG_ON(field_max(IP_FLTRT_FLAGS_HASH_SIZE_FMASK) !=
  127. field_max(IP_FLTRT_FLAGS_NHASH_SIZE_FMASK));
  128. BUILD_BUG_ON(field_max(IP_FLTRT_FLAGS_HASH_ADDR_FMASK) !=
  129. field_max(IP_FLTRT_FLAGS_NHASH_ADDR_FMASK));
  130. /* Prior to IPA v5.0, we supported no more than 32 endpoints,
  131. * and this was reflected in some 5-bit fields that held
  132. * endpoint numbers. Starting with IPA v5.0, the widths of
  133. * these fields were extended to 8 bits, meaning up to 256
  134. * endpoints. If the driver claims to support more than
  135. * that it's an error.
  136. */
  137. BUILD_BUG_ON(IPA_ENDPOINT_MAX - 1 > U8_MAX);
  138. }
  139. /* Validate a memory region holding a table */
  140. bool ipa_cmd_table_init_valid(struct ipa *ipa, const struct ipa_mem *mem,
  141. bool route)
  142. {
  143. u32 offset_max = field_max(IP_FLTRT_FLAGS_NHASH_ADDR_FMASK);
  144. u32 size_max = field_max(IP_FLTRT_FLAGS_NHASH_SIZE_FMASK);
  145. const char *table = route ? "route" : "filter";
  146. struct device *dev = ipa->dev;
  147. u32 size;
  148. size = route ? ipa->route_count : ipa->filter_count + 1;
  149. size *= sizeof(__le64);
  150. /* Size must fit in the immediate command field that holds it */
  151. if (size > size_max) {
  152. dev_err(dev, "%s table region size too large\n", table);
  153. dev_err(dev, " (0x%04x > 0x%04x)\n", size, size_max);
  154. return false;
  155. }
  156. /* Offset must fit in the immediate command field that holds it */
  157. if (mem->offset > offset_max ||
  158. ipa->mem_offset > offset_max - mem->offset) {
  159. dev_err(dev, "%s table region offset too large\n", table);
  160. dev_err(dev, " (0x%04x + 0x%04x > 0x%04x)\n",
  161. ipa->mem_offset, mem->offset, offset_max);
  162. return false;
  163. }
  164. return true;
  165. }
  166. /* Validate the memory region that holds headers */
  167. static bool ipa_cmd_header_init_local_valid(struct ipa *ipa)
  168. {
  169. struct device *dev = ipa->dev;
  170. const struct ipa_mem *mem;
  171. u32 offset_max;
  172. u32 size_max;
  173. u32 offset;
  174. u32 size;
  175. /* In ipa_cmd_hdr_init_local_add() we record the offset and size of
  176. * the header table memory area in an immediate command. Make sure
  177. * the offset and size fit in the fields that need to hold them, and
  178. * that the entire range is within the overall IPA memory range.
  179. */
  180. offset_max = field_max(HDR_INIT_LOCAL_FLAGS_HDR_ADDR_FMASK);
  181. size_max = field_max(HDR_INIT_LOCAL_FLAGS_TABLE_SIZE_FMASK);
  182. /* The header memory area contains both the modem and AP header
  183. * regions. The modem portion defines the address of the region.
  184. */
  185. mem = ipa_mem_find(ipa, IPA_MEM_MODEM_HEADER);
  186. offset = mem->offset;
  187. size = mem->size;
  188. /* Make sure the offset fits in the IPA command */
  189. if (offset > offset_max || ipa->mem_offset > offset_max - offset) {
  190. dev_err(dev, "header table region offset too large\n");
  191. dev_err(dev, " (0x%04x + 0x%04x > 0x%04x)\n",
  192. ipa->mem_offset, offset, offset_max);
  193. return false;
  194. }
  195. /* Add the size of the AP portion (if defined) to the combined size */
  196. mem = ipa_mem_find(ipa, IPA_MEM_AP_HEADER);
  197. if (mem)
  198. size += mem->size;
  199. /* Make sure the combined size fits in the IPA command */
  200. if (size > size_max) {
  201. dev_err(dev, "header table region size too large\n");
  202. dev_err(dev, " (0x%04x > 0x%08x)\n", size, size_max);
  203. return false;
  204. }
  205. return true;
  206. }
  207. /* Indicate whether an offset can be used with a register_write command */
  208. static bool ipa_cmd_register_write_offset_valid(struct ipa *ipa,
  209. const char *name, u32 offset)
  210. {
  211. struct ipa_cmd_register_write *payload;
  212. struct device *dev = ipa->dev;
  213. u32 offset_max;
  214. u32 bit_count;
  215. /* The maximum offset in a register_write immediate command depends
  216. * on the version of IPA. A 16 bit offset is always supported,
  217. * but starting with IPA v4.0 some additional high-order bits are
  218. * allowed.
  219. */
  220. bit_count = BITS_PER_BYTE * sizeof(payload->offset);
  221. if (ipa->version >= IPA_VERSION_4_0)
  222. bit_count += hweight32(REGISTER_WRITE_FLAGS_OFFSET_HIGH_FMASK);
  223. BUILD_BUG_ON(bit_count > 32);
  224. offset_max = ~0U >> (32 - bit_count);
  225. /* Make sure the offset can be represented by the field(s)
  226. * that holds it. Also make sure the offset is not outside
  227. * the overall IPA memory range.
  228. */
  229. if (offset > offset_max || ipa->mem_offset > offset_max - offset) {
  230. dev_err(dev, "%s offset too large 0x%04x + 0x%04x > 0x%04x)\n",
  231. name, ipa->mem_offset, offset, offset_max);
  232. return false;
  233. }
  234. return true;
  235. }
  236. /* Check whether offsets passed to register_write are valid */
  237. static bool ipa_cmd_register_write_valid(struct ipa *ipa)
  238. {
  239. const struct reg *reg;
  240. const char *name;
  241. u32 offset;
  242. /* If hashed tables are supported, ensure the hash flush register
  243. * offset will fit in a register write IPA immediate command.
  244. */
  245. if (ipa_table_hash_support(ipa)) {
  246. if (ipa->version < IPA_VERSION_5_0)
  247. reg = ipa_reg(ipa, FILT_ROUT_HASH_FLUSH);
  248. else
  249. reg = ipa_reg(ipa, FILT_ROUT_CACHE_FLUSH);
  250. offset = reg_offset(reg);
  251. name = "filter/route hash flush";
  252. if (!ipa_cmd_register_write_offset_valid(ipa, name, offset))
  253. return false;
  254. }
  255. /* Each endpoint can have a status endpoint associated with it,
  256. * and this is recorded in an endpoint register. If the modem
  257. * crashes, we reset the status endpoint for all modem endpoints
  258. * using a register write IPA immediate command. Make sure the
  259. * worst case (highest endpoint number) offset of that endpoint
  260. * fits in the register write command field(s) that must hold it.
  261. */
  262. reg = ipa_reg(ipa, ENDP_STATUS);
  263. offset = reg_n_offset(reg, IPA_ENDPOINT_COUNT - 1);
  264. name = "maximal endpoint status";
  265. if (!ipa_cmd_register_write_offset_valid(ipa, name, offset))
  266. return false;
  267. return true;
  268. }
  269. int ipa_cmd_pool_init(struct gsi_channel *channel, u32 tre_max)
  270. {
  271. struct gsi_trans_info *trans_info = &channel->trans_info;
  272. struct device *dev = channel->gsi->dev;
  273. /* Command payloads are allocated one at a time, but a single
  274. * transaction can require up to the maximum supported by the
  275. * channel; treat them as if they were allocated all at once.
  276. */
  277. return gsi_trans_pool_init_dma(dev, &trans_info->cmd_pool,
  278. sizeof(union ipa_cmd_payload),
  279. tre_max, channel->trans_tre_max);
  280. }
  281. void ipa_cmd_pool_exit(struct gsi_channel *channel)
  282. {
  283. struct gsi_trans_info *trans_info = &channel->trans_info;
  284. struct device *dev = channel->gsi->dev;
  285. gsi_trans_pool_exit_dma(dev, &trans_info->cmd_pool);
  286. }
  287. static union ipa_cmd_payload *
  288. ipa_cmd_payload_alloc(struct ipa *ipa, dma_addr_t *addr)
  289. {
  290. struct gsi_trans_info *trans_info;
  291. struct ipa_endpoint *endpoint;
  292. endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX];
  293. trans_info = &ipa->gsi.channel[endpoint->channel_id].trans_info;
  294. return gsi_trans_pool_alloc_dma(&trans_info->cmd_pool, addr);
  295. }
  296. /* If hash_size is 0, hash_offset and hash_addr ignored. */
  297. void ipa_cmd_table_init_add(struct gsi_trans *trans,
  298. enum ipa_cmd_opcode opcode, u16 size, u32 offset,
  299. dma_addr_t addr, u16 hash_size, u32 hash_offset,
  300. dma_addr_t hash_addr)
  301. {
  302. struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
  303. struct ipa_cmd_hw_ip_fltrt_init *payload;
  304. union ipa_cmd_payload *cmd_payload;
  305. dma_addr_t payload_addr;
  306. u64 val;
  307. /* Record the non-hash table offset and size */
  308. offset += ipa->mem_offset;
  309. val = u64_encode_bits(offset, IP_FLTRT_FLAGS_NHASH_ADDR_FMASK);
  310. val |= u64_encode_bits(size, IP_FLTRT_FLAGS_NHASH_SIZE_FMASK);
  311. /* The hash table offset and address are zero if its size is 0 */
  312. if (hash_size) {
  313. /* Record the hash table offset and size */
  314. hash_offset += ipa->mem_offset;
  315. val |= u64_encode_bits(hash_offset,
  316. IP_FLTRT_FLAGS_HASH_ADDR_FMASK);
  317. val |= u64_encode_bits(hash_size,
  318. IP_FLTRT_FLAGS_HASH_SIZE_FMASK);
  319. }
  320. cmd_payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
  321. payload = &cmd_payload->table_init;
  322. /* Fill in all offsets and sizes and the non-hash table address */
  323. if (hash_size)
  324. payload->hash_rules_addr = cpu_to_le64(hash_addr);
  325. payload->flags = cpu_to_le64(val);
  326. payload->nhash_rules_addr = cpu_to_le64(addr);
  327. gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
  328. opcode);
  329. }
  330. /* Initialize header space in IPA-local memory */
  331. void ipa_cmd_hdr_init_local_add(struct gsi_trans *trans, u32 offset, u16 size,
  332. dma_addr_t addr)
  333. {
  334. struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
  335. enum ipa_cmd_opcode opcode = IPA_CMD_HDR_INIT_LOCAL;
  336. struct ipa_cmd_hw_hdr_init_local *payload;
  337. union ipa_cmd_payload *cmd_payload;
  338. dma_addr_t payload_addr;
  339. u32 flags;
  340. offset += ipa->mem_offset;
  341. /* With this command we tell the IPA where in its local memory the
  342. * header tables reside. The content of the buffer provided is
  343. * also written via DMA into that space. The IPA hardware owns
  344. * the table, but the AP must initialize it.
  345. */
  346. cmd_payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
  347. payload = &cmd_payload->hdr_init_local;
  348. payload->hdr_table_addr = cpu_to_le64(addr);
  349. flags = u32_encode_bits(size, HDR_INIT_LOCAL_FLAGS_TABLE_SIZE_FMASK);
  350. flags |= u32_encode_bits(offset, HDR_INIT_LOCAL_FLAGS_HDR_ADDR_FMASK);
  351. payload->flags = cpu_to_le32(flags);
  352. gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
  353. opcode);
  354. }
  355. void ipa_cmd_register_write_add(struct gsi_trans *trans, u32 offset, u32 value,
  356. u32 mask, bool clear_full)
  357. {
  358. struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
  359. struct ipa_cmd_register_write *payload;
  360. union ipa_cmd_payload *cmd_payload;
  361. u32 opcode = IPA_CMD_REGISTER_WRITE;
  362. dma_addr_t payload_addr;
  363. u32 clear_option;
  364. u32 options;
  365. u16 flags;
  366. /* pipeline_clear_src_grp is not used */
  367. clear_option = clear_full ? pipeline_clear_full : pipeline_clear_hps;
  368. /* IPA v4.0+ represents the pipeline clear options in the opcode. It
  369. * also supports a larger offset by encoding additional high-order
  370. * bits in the payload flags field.
  371. */
  372. if (ipa->version >= IPA_VERSION_4_0) {
  373. u16 offset_high;
  374. u32 val;
  375. /* Opcode encodes pipeline clear options */
  376. /* SKIP_CLEAR is always 0 (don't skip pipeline clear) */
  377. val = u16_encode_bits(clear_option,
  378. REGISTER_WRITE_OPCODE_CLEAR_OPTION_FMASK);
  379. opcode |= val;
  380. /* Extract the high 4 bits from the offset */
  381. offset_high = (u16)u32_get_bits(offset, GENMASK(19, 16));
  382. offset &= (1 << 16) - 1;
  383. /* Extract the top 4 bits and encode it into the flags field */
  384. flags = u16_encode_bits(offset_high,
  385. REGISTER_WRITE_FLAGS_OFFSET_HIGH_FMASK);
  386. options = 0; /* reserved */
  387. } else {
  388. flags = 0; /* SKIP_CLEAR flag is always 0 */
  389. options = u16_encode_bits(clear_option,
  390. REGISTER_WRITE_CLEAR_OPTIONS_FMASK);
  391. }
  392. cmd_payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
  393. payload = &cmd_payload->register_write;
  394. payload->flags = cpu_to_le16(flags);
  395. payload->offset = cpu_to_le16((u16)offset);
  396. payload->value = cpu_to_le32(value);
  397. payload->value_mask = cpu_to_le32(mask);
  398. payload->clear_options = cpu_to_le32(options);
  399. gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
  400. opcode);
  401. }
  402. /* Skip IP packet processing on the next data transfer on a TX channel */
  403. static void ipa_cmd_ip_packet_init_add(struct gsi_trans *trans, u8 endpoint_id)
  404. {
  405. struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
  406. enum ipa_cmd_opcode opcode = IPA_CMD_IP_PACKET_INIT;
  407. struct ipa_cmd_ip_packet_init *payload;
  408. union ipa_cmd_payload *cmd_payload;
  409. dma_addr_t payload_addr;
  410. cmd_payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
  411. payload = &cmd_payload->ip_packet_init;
  412. if (ipa->version < IPA_VERSION_5_0) {
  413. payload->dest_endpoint =
  414. u8_encode_bits(endpoint_id,
  415. IPA_PACKET_INIT_DEST_ENDPOINT_FMASK);
  416. } else {
  417. payload->dest_endpoint = endpoint_id;
  418. }
  419. gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
  420. opcode);
  421. }
  422. /* Use a DMA command to read or write a block of IPA-resident memory */
  423. void ipa_cmd_dma_shared_mem_add(struct gsi_trans *trans, u32 offset, u16 size,
  424. dma_addr_t addr, bool toward_ipa)
  425. {
  426. struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
  427. enum ipa_cmd_opcode opcode = IPA_CMD_DMA_SHARED_MEM;
  428. struct ipa_cmd_hw_dma_mem_mem *payload;
  429. union ipa_cmd_payload *cmd_payload;
  430. dma_addr_t payload_addr;
  431. u16 flags;
  432. /* size and offset must fit in 16 bit fields */
  433. WARN_ON(!size);
  434. WARN_ON(size > U16_MAX);
  435. WARN_ON(offset > U16_MAX || ipa->mem_offset > U16_MAX - offset);
  436. offset += ipa->mem_offset;
  437. cmd_payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
  438. payload = &cmd_payload->dma_shared_mem;
  439. /* payload->clear_after_read was reserved prior to IPA v4.0. It's
  440. * never needed for current code, so it's 0 regardless of version.
  441. */
  442. payload->size = cpu_to_le16(size);
  443. payload->local_addr = cpu_to_le16(offset);
  444. /* payload->flags:
  445. * direction: 0 = write to IPA, 1 read from IPA
  446. * Starting at v4.0 these are reserved; either way, all zero:
  447. * pipeline clear: 0 = wait for pipeline clear (don't skip)
  448. * clear_options: 0 = pipeline_clear_hps
  449. * Instead, for v4.0+ these are encoded in the opcode. But again
  450. * since both values are 0 we won't bother OR'ing them in.
  451. */
  452. flags = toward_ipa ? 0 : DMA_SHARED_MEM_FLAGS_DIRECTION_FMASK;
  453. payload->flags = cpu_to_le16(flags);
  454. payload->system_addr = cpu_to_le64(addr);
  455. gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
  456. opcode);
  457. }
  458. static void ipa_cmd_ip_tag_status_add(struct gsi_trans *trans)
  459. {
  460. struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
  461. enum ipa_cmd_opcode opcode = IPA_CMD_IP_PACKET_TAG_STATUS;
  462. struct ipa_cmd_ip_packet_tag_status *payload;
  463. union ipa_cmd_payload *cmd_payload;
  464. dma_addr_t payload_addr;
  465. cmd_payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
  466. payload = &cmd_payload->ip_packet_tag_status;
  467. payload->tag = le64_encode_bits(0, IP_PACKET_TAG_STATUS_TAG_FMASK);
  468. gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
  469. opcode);
  470. }
  471. /* Issue a small command TX data transfer */
  472. static void ipa_cmd_transfer_add(struct gsi_trans *trans)
  473. {
  474. struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
  475. enum ipa_cmd_opcode opcode = IPA_CMD_NONE;
  476. union ipa_cmd_payload *payload;
  477. dma_addr_t payload_addr;
  478. /* Just transfer a zero-filled payload structure */
  479. payload = ipa_cmd_payload_alloc(ipa, &payload_addr);
  480. gsi_trans_cmd_add(trans, payload, sizeof(*payload), payload_addr,
  481. opcode);
  482. }
  483. /* Add immediate commands to a transaction to clear the hardware pipeline */
  484. void ipa_cmd_pipeline_clear_add(struct gsi_trans *trans)
  485. {
  486. struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
  487. struct ipa_endpoint *endpoint;
  488. /* This will complete when the transfer is received */
  489. reinit_completion(&ipa->completion);
  490. /* Issue a no-op register write command (mask 0 means no write) */
  491. ipa_cmd_register_write_add(trans, 0, 0, 0, true);
  492. /* Send a data packet through the IPA pipeline. The packet_init
  493. * command says to send the next packet directly to the exception
  494. * endpoint without any other IPA processing. The tag_status
  495. * command requests that status be generated on completion of
  496. * that transfer, and that it will be tagged with a value.
  497. * Finally, the transfer command sends a small packet of data
  498. * (instead of a command) using the command endpoint.
  499. */
  500. endpoint = ipa->name_map[IPA_ENDPOINT_AP_LAN_RX];
  501. ipa_cmd_ip_packet_init_add(trans, endpoint->endpoint_id);
  502. ipa_cmd_ip_tag_status_add(trans);
  503. ipa_cmd_transfer_add(trans);
  504. }
  505. /* Returns the number of commands required to clear the pipeline */
  506. u32 ipa_cmd_pipeline_clear_count(void)
  507. {
  508. return 4;
  509. }
  510. void ipa_cmd_pipeline_clear_wait(struct ipa *ipa)
  511. {
  512. wait_for_completion(&ipa->completion);
  513. }
  514. /* Allocate a transaction for the command TX endpoint */
  515. struct gsi_trans *ipa_cmd_trans_alloc(struct ipa *ipa, u32 tre_count)
  516. {
  517. struct ipa_endpoint *endpoint;
  518. if (WARN_ON(tre_count > IPA_COMMAND_TRANS_TRE_MAX))
  519. return NULL;
  520. endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX];
  521. return gsi_channel_trans_alloc(&ipa->gsi, endpoint->channel_id,
  522. tre_count, DMA_NONE);
  523. }
  524. /* Init function for immediate commands; there is no ipa_cmd_exit() */
  525. int ipa_cmd_init(struct ipa *ipa)
  526. {
  527. ipa_cmd_validate_build();
  528. if (!ipa_cmd_header_init_local_valid(ipa))
  529. return -EINVAL;
  530. if (!ipa_cmd_register_write_valid(ipa))
  531. return -EINVAL;
  532. return 0;
  533. }