ipa_mem.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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/dma-mapping.h>
  6. #include <linux/io.h>
  7. #include <linux/iommu.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/types.h>
  10. #include <linux/soc/qcom/smem.h>
  11. #include "gsi_trans.h"
  12. #include "ipa.h"
  13. #include "ipa_cmd.h"
  14. #include "ipa_data.h"
  15. #include "ipa_mem.h"
  16. #include "ipa_reg.h"
  17. #include "ipa_table.h"
  18. /* "Canary" value placed between memory regions to detect overflow */
  19. #define IPA_MEM_CANARY_VAL cpu_to_le32(0xdeadbeef)
  20. /* SMEM host id representing the modem. */
  21. #define QCOM_SMEM_HOST_MODEM 1
  22. const struct ipa_mem *ipa_mem_find(struct ipa *ipa, enum ipa_mem_id mem_id)
  23. {
  24. u32 i;
  25. for (i = 0; i < ipa->mem_count; i++) {
  26. const struct ipa_mem *mem = &ipa->mem[i];
  27. if (mem->id == mem_id)
  28. return mem;
  29. }
  30. return NULL;
  31. }
  32. /* Add an immediate command to a transaction that zeroes a memory region */
  33. static void
  34. ipa_mem_zero_region_add(struct gsi_trans *trans, enum ipa_mem_id mem_id)
  35. {
  36. struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
  37. const struct ipa_mem *mem = ipa_mem_find(ipa, mem_id);
  38. dma_addr_t addr = ipa->zero_addr;
  39. if (!mem->size)
  40. return;
  41. ipa_cmd_dma_shared_mem_add(trans, mem->offset, mem->size, addr, true);
  42. }
  43. /**
  44. * ipa_mem_setup() - Set up IPA AP and modem shared memory areas
  45. * @ipa: IPA pointer
  46. *
  47. * Set up the shared memory regions in IPA local memory. This involves
  48. * zero-filling memory regions, and in the case of header memory, telling
  49. * the IPA where it's located.
  50. *
  51. * This function performs the initial setup of this memory. If the modem
  52. * crashes, its regions are re-zeroed in ipa_mem_zero_modem().
  53. *
  54. * The AP informs the modem where its portions of memory are located
  55. * in a QMI exchange that occurs at modem startup.
  56. *
  57. * There is no need for a matching ipa_mem_teardown() function.
  58. *
  59. * Return: 0 if successful, or a negative error code
  60. */
  61. int ipa_mem_setup(struct ipa *ipa)
  62. {
  63. dma_addr_t addr = ipa->zero_addr;
  64. const struct ipa_mem *mem;
  65. struct gsi_trans *trans;
  66. const struct reg *reg;
  67. u32 offset;
  68. u16 size;
  69. u32 val;
  70. /* Get a transaction to define the header memory region and to zero
  71. * the processing context and modem memory regions.
  72. */
  73. trans = ipa_cmd_trans_alloc(ipa, 4);
  74. if (!trans) {
  75. dev_err(ipa->dev, "no transaction for memory setup\n");
  76. return -EBUSY;
  77. }
  78. /* Initialize IPA-local header memory. The AP header region, if
  79. * present, is contiguous with and follows the modem header region,
  80. * and they are initialized together.
  81. */
  82. mem = ipa_mem_find(ipa, IPA_MEM_MODEM_HEADER);
  83. offset = mem->offset;
  84. size = mem->size;
  85. mem = ipa_mem_find(ipa, IPA_MEM_AP_HEADER);
  86. if (mem)
  87. size += mem->size;
  88. ipa_cmd_hdr_init_local_add(trans, offset, size, addr);
  89. ipa_mem_zero_region_add(trans, IPA_MEM_MODEM_PROC_CTX);
  90. ipa_mem_zero_region_add(trans, IPA_MEM_AP_PROC_CTX);
  91. ipa_mem_zero_region_add(trans, IPA_MEM_MODEM);
  92. gsi_trans_commit_wait(trans);
  93. /* Tell the hardware where the processing context area is located */
  94. mem = ipa_mem_find(ipa, IPA_MEM_MODEM_PROC_CTX);
  95. offset = ipa->mem_offset + mem->offset;
  96. reg = ipa_reg(ipa, LOCAL_PKT_PROC_CNTXT);
  97. val = reg_encode(reg, IPA_BASE_ADDR, offset);
  98. iowrite32(val, ipa->reg_virt + reg_offset(reg));
  99. return 0;
  100. }
  101. /* Is the given memory region ID is valid for the current IPA version? */
  102. static bool ipa_mem_id_valid(struct ipa *ipa, enum ipa_mem_id mem_id)
  103. {
  104. enum ipa_version version = ipa->version;
  105. switch (mem_id) {
  106. case IPA_MEM_UC_SHARED:
  107. case IPA_MEM_UC_INFO:
  108. case IPA_MEM_V4_FILTER_HASHED:
  109. case IPA_MEM_V4_FILTER:
  110. case IPA_MEM_V6_FILTER_HASHED:
  111. case IPA_MEM_V6_FILTER:
  112. case IPA_MEM_V4_ROUTE_HASHED:
  113. case IPA_MEM_V4_ROUTE:
  114. case IPA_MEM_V6_ROUTE_HASHED:
  115. case IPA_MEM_V6_ROUTE:
  116. case IPA_MEM_MODEM_HEADER:
  117. case IPA_MEM_AP_HEADER:
  118. case IPA_MEM_MODEM_PROC_CTX:
  119. case IPA_MEM_AP_PROC_CTX:
  120. case IPA_MEM_MODEM:
  121. case IPA_MEM_UC_EVENT_RING:
  122. case IPA_MEM_PDN_CONFIG:
  123. case IPA_MEM_STATS_QUOTA_MODEM:
  124. case IPA_MEM_STATS_QUOTA_AP:
  125. case IPA_MEM_END_MARKER: /* pseudo region */
  126. break;
  127. case IPA_MEM_STATS_TETHERING:
  128. case IPA_MEM_STATS_DROP:
  129. if (version < IPA_VERSION_4_0)
  130. return false;
  131. break;
  132. case IPA_MEM_STATS_V4_FILTER:
  133. case IPA_MEM_STATS_V6_FILTER:
  134. case IPA_MEM_STATS_V4_ROUTE:
  135. case IPA_MEM_STATS_V6_ROUTE:
  136. if (version < IPA_VERSION_4_0 || version > IPA_VERSION_4_2)
  137. return false;
  138. break;
  139. case IPA_MEM_AP_V4_FILTER:
  140. case IPA_MEM_AP_V6_FILTER:
  141. if (version < IPA_VERSION_5_0)
  142. return false;
  143. break;
  144. case IPA_MEM_NAT_TABLE:
  145. case IPA_MEM_STATS_FILTER_ROUTE:
  146. if (version < IPA_VERSION_4_5)
  147. return false;
  148. break;
  149. default:
  150. return false;
  151. }
  152. return true;
  153. }
  154. /* Must the given memory region be present in the configuration? */
  155. static bool ipa_mem_id_required(struct ipa *ipa, enum ipa_mem_id mem_id)
  156. {
  157. switch (mem_id) {
  158. case IPA_MEM_UC_SHARED:
  159. case IPA_MEM_UC_INFO:
  160. case IPA_MEM_V4_FILTER_HASHED:
  161. case IPA_MEM_V4_FILTER:
  162. case IPA_MEM_V6_FILTER_HASHED:
  163. case IPA_MEM_V6_FILTER:
  164. case IPA_MEM_V4_ROUTE_HASHED:
  165. case IPA_MEM_V4_ROUTE:
  166. case IPA_MEM_V6_ROUTE_HASHED:
  167. case IPA_MEM_V6_ROUTE:
  168. case IPA_MEM_MODEM_HEADER:
  169. case IPA_MEM_MODEM_PROC_CTX:
  170. case IPA_MEM_AP_PROC_CTX:
  171. case IPA_MEM_MODEM:
  172. return true;
  173. case IPA_MEM_PDN_CONFIG:
  174. case IPA_MEM_STATS_QUOTA_MODEM:
  175. return ipa->version >= IPA_VERSION_4_0;
  176. case IPA_MEM_STATS_TETHERING:
  177. return ipa->version >= IPA_VERSION_4_0 &&
  178. ipa->version != IPA_VERSION_5_0;
  179. default:
  180. return false; /* Anything else is optional */
  181. }
  182. }
  183. static bool ipa_mem_valid_one(struct ipa *ipa, const struct ipa_mem *mem)
  184. {
  185. enum ipa_mem_id mem_id = mem->id;
  186. struct device *dev = ipa->dev;
  187. u16 size_multiple;
  188. /* Make sure the memory region is valid for this version of IPA */
  189. if (!ipa_mem_id_valid(ipa, mem_id)) {
  190. dev_err(dev, "region id %u not valid\n", mem_id);
  191. return false;
  192. }
  193. if (!mem->size && !mem->canary_count) {
  194. dev_err(dev, "empty memory region %u\n", mem_id);
  195. return false;
  196. }
  197. /* Other than modem memory, sizes must be a multiple of 8 */
  198. size_multiple = mem_id == IPA_MEM_MODEM ? 4 : 8;
  199. if (mem->size % size_multiple)
  200. dev_err(dev, "region %u size not a multiple of %u bytes\n",
  201. mem_id, size_multiple);
  202. else if (mem->offset % 8)
  203. dev_err(dev, "region %u offset not 8-byte aligned\n", mem_id);
  204. else if (mem->offset < mem->canary_count * sizeof(__le32))
  205. dev_err(dev, "region %u offset too small for %hu canaries\n",
  206. mem_id, mem->canary_count);
  207. else if (mem_id == IPA_MEM_END_MARKER && mem->size)
  208. dev_err(dev, "non-zero end marker region size\n");
  209. else
  210. return true;
  211. return false;
  212. }
  213. /* Verify each defined memory region is valid. */
  214. static bool ipa_mem_valid(struct ipa *ipa, const struct ipa_mem_data *mem_data)
  215. {
  216. DECLARE_BITMAP(regions, IPA_MEM_COUNT) = { };
  217. struct device *dev = ipa->dev;
  218. enum ipa_mem_id mem_id;
  219. u32 i;
  220. if (mem_data->local_count > IPA_MEM_COUNT) {
  221. dev_err(dev, "too many memory regions (%u > %u)\n",
  222. mem_data->local_count, IPA_MEM_COUNT);
  223. return false;
  224. }
  225. for (i = 0; i < mem_data->local_count; i++) {
  226. const struct ipa_mem *mem = &mem_data->local[i];
  227. if (__test_and_set_bit(mem->id, regions)) {
  228. dev_err(dev, "duplicate memory region %u\n", mem->id);
  229. return false;
  230. }
  231. /* Defined regions have non-zero size and/or canary count */
  232. if (!ipa_mem_valid_one(ipa, mem))
  233. return false;
  234. }
  235. /* Now see if any required regions are not defined */
  236. for_each_clear_bit(mem_id, regions, IPA_MEM_COUNT) {
  237. if (ipa_mem_id_required(ipa, mem_id))
  238. dev_err(dev, "required memory region %u missing\n",
  239. mem_id);
  240. }
  241. return true;
  242. }
  243. /* Do all memory regions fit within the IPA local memory? */
  244. static bool ipa_mem_size_valid(struct ipa *ipa)
  245. {
  246. struct device *dev = ipa->dev;
  247. u32 limit = ipa->mem_size;
  248. u32 i;
  249. for (i = 0; i < ipa->mem_count; i++) {
  250. const struct ipa_mem *mem = &ipa->mem[i];
  251. if (mem->offset + mem->size <= limit)
  252. continue;
  253. dev_err(dev, "region %u ends beyond memory limit (0x%08x)\n",
  254. mem->id, limit);
  255. return false;
  256. }
  257. return true;
  258. }
  259. /**
  260. * ipa_mem_config() - Configure IPA shared memory
  261. * @ipa: IPA pointer
  262. *
  263. * Return: 0 if successful, or a negative error code
  264. */
  265. int ipa_mem_config(struct ipa *ipa)
  266. {
  267. struct device *dev = ipa->dev;
  268. const struct ipa_mem *mem;
  269. const struct reg *reg;
  270. dma_addr_t addr;
  271. u32 mem_size;
  272. void *virt;
  273. u32 val;
  274. u32 i;
  275. /* Check the advertised location and size of the shared memory area */
  276. reg = ipa_reg(ipa, SHARED_MEM_SIZE);
  277. val = ioread32(ipa->reg_virt + reg_offset(reg));
  278. /* The fields in the register are in 8 byte units */
  279. ipa->mem_offset = 8 * reg_decode(reg, MEM_BADDR, val);
  280. /* Make sure the end is within the region's mapped space */
  281. mem_size = 8 * reg_decode(reg, MEM_SIZE, val);
  282. /* If the sizes don't match, issue a warning */
  283. if (ipa->mem_offset + mem_size < ipa->mem_size) {
  284. dev_warn(dev, "limiting IPA memory size to 0x%08x\n",
  285. mem_size);
  286. ipa->mem_size = mem_size;
  287. } else if (ipa->mem_offset + mem_size > ipa->mem_size) {
  288. dev_dbg(dev, "ignoring larger reported memory size: 0x%08x\n",
  289. mem_size);
  290. }
  291. /* We know our memory size; make sure regions are all in range */
  292. if (!ipa_mem_size_valid(ipa))
  293. return -EINVAL;
  294. /* Prealloc DMA memory for zeroing regions */
  295. virt = dma_alloc_coherent(dev, IPA_MEM_MAX, &addr, GFP_KERNEL);
  296. if (!virt)
  297. return -ENOMEM;
  298. ipa->zero_addr = addr;
  299. ipa->zero_virt = virt;
  300. ipa->zero_size = IPA_MEM_MAX;
  301. /* For each defined region, write "canary" values in the
  302. * space prior to the region's base address if indicated.
  303. */
  304. for (i = 0; i < ipa->mem_count; i++) {
  305. u16 canary_count = ipa->mem[i].canary_count;
  306. __le32 *canary;
  307. if (!canary_count)
  308. continue;
  309. /* Write canary values in the space before the region */
  310. canary = ipa->mem_virt + ipa->mem_offset + ipa->mem[i].offset;
  311. do
  312. *--canary = IPA_MEM_CANARY_VAL;
  313. while (--canary_count);
  314. }
  315. /* Verify the microcontroller ring alignment (if defined) */
  316. mem = ipa_mem_find(ipa, IPA_MEM_UC_EVENT_RING);
  317. if (mem && mem->offset % 1024) {
  318. dev_err(dev, "microcontroller ring not 1024-byte aligned\n");
  319. goto err_dma_free;
  320. }
  321. return 0;
  322. err_dma_free:
  323. dma_free_coherent(dev, IPA_MEM_MAX, ipa->zero_virt, ipa->zero_addr);
  324. return -EINVAL;
  325. }
  326. /* Inverse of ipa_mem_config() */
  327. void ipa_mem_deconfig(struct ipa *ipa)
  328. {
  329. struct device *dev = ipa->dev;
  330. dma_free_coherent(dev, ipa->zero_size, ipa->zero_virt, ipa->zero_addr);
  331. ipa->zero_size = 0;
  332. ipa->zero_virt = NULL;
  333. ipa->zero_addr = 0;
  334. }
  335. /**
  336. * ipa_mem_zero_modem() - Zero IPA-local memory regions owned by the modem
  337. * @ipa: IPA pointer
  338. *
  339. * Zero regions of IPA-local memory used by the modem. These are configured
  340. * (and initially zeroed) by ipa_mem_setup(), but if the modem crashes and
  341. * restarts via SSR we need to re-initialize them. A QMI message tells the
  342. * modem where to find regions of IPA local memory it needs to know about
  343. * (these included).
  344. */
  345. int ipa_mem_zero_modem(struct ipa *ipa)
  346. {
  347. struct gsi_trans *trans;
  348. /* Get a transaction to zero the modem memory, modem header,
  349. * and modem processing context regions.
  350. */
  351. trans = ipa_cmd_trans_alloc(ipa, 3);
  352. if (!trans) {
  353. dev_err(ipa->dev, "no transaction to zero modem memory\n");
  354. return -EBUSY;
  355. }
  356. ipa_mem_zero_region_add(trans, IPA_MEM_MODEM_HEADER);
  357. ipa_mem_zero_region_add(trans, IPA_MEM_MODEM_PROC_CTX);
  358. ipa_mem_zero_region_add(trans, IPA_MEM_MODEM);
  359. gsi_trans_commit_wait(trans);
  360. return 0;
  361. }
  362. /**
  363. * ipa_imem_init() - Initialize IMEM memory used by the IPA
  364. * @ipa: IPA pointer
  365. * @addr: Physical address of the IPA region in IMEM
  366. * @size: Size (bytes) of the IPA region in IMEM
  367. *
  368. * IMEM is a block of shared memory separate from system DRAM, and
  369. * a portion of this memory is available for the IPA to use. The
  370. * modem accesses this memory directly, but the IPA accesses it
  371. * via the IOMMU, using the AP's credentials.
  372. *
  373. * If this region exists (size > 0) we map it for read/write access
  374. * through the IOMMU using the IPA device.
  375. *
  376. * Note: @addr and @size are not guaranteed to be page-aligned.
  377. */
  378. static int ipa_imem_init(struct ipa *ipa, unsigned long addr, size_t size)
  379. {
  380. struct device *dev = ipa->dev;
  381. struct iommu_domain *domain;
  382. unsigned long iova;
  383. phys_addr_t phys;
  384. int ret;
  385. if (!size)
  386. return 0; /* IMEM memory not used */
  387. domain = iommu_get_domain_for_dev(dev);
  388. if (!domain) {
  389. dev_err(dev, "no IOMMU domain found for IMEM\n");
  390. return -EINVAL;
  391. }
  392. /* Align the address down and the size up to page boundaries */
  393. phys = addr & PAGE_MASK;
  394. size = PAGE_ALIGN(size + addr - phys);
  395. iova = phys; /* We just want a direct mapping */
  396. ret = iommu_map(domain, iova, phys, size, IOMMU_READ | IOMMU_WRITE,
  397. GFP_KERNEL);
  398. if (ret)
  399. return ret;
  400. ipa->imem_iova = iova;
  401. ipa->imem_size = size;
  402. return 0;
  403. }
  404. static void ipa_imem_exit(struct ipa *ipa)
  405. {
  406. struct device *dev = ipa->dev;
  407. struct iommu_domain *domain;
  408. if (!ipa->imem_size)
  409. return;
  410. domain = iommu_get_domain_for_dev(dev);
  411. if (domain) {
  412. size_t size;
  413. size = iommu_unmap(domain, ipa->imem_iova, ipa->imem_size);
  414. if (size != ipa->imem_size)
  415. dev_warn(dev, "unmapped %zu IMEM bytes, expected %zu\n",
  416. size, ipa->imem_size);
  417. } else {
  418. dev_err(dev, "couldn't get IPA IOMMU domain for IMEM\n");
  419. }
  420. ipa->imem_size = 0;
  421. ipa->imem_iova = 0;
  422. }
  423. /**
  424. * ipa_smem_init() - Initialize SMEM memory used by the IPA
  425. * @ipa: IPA pointer
  426. * @item: Item ID of SMEM memory
  427. * @size: Size (bytes) of SMEM memory region
  428. *
  429. * SMEM is a managed block of shared DRAM, from which numbered "items"
  430. * can be allocated. One item is designated for use by the IPA.
  431. *
  432. * The modem accesses SMEM memory directly, but the IPA accesses it
  433. * via the IOMMU, using the AP's credentials.
  434. *
  435. * If size provided is non-zero, we allocate it and map it for
  436. * access through the IOMMU.
  437. *
  438. * Note: @size and the item address are is not guaranteed to be page-aligned.
  439. */
  440. static int ipa_smem_init(struct ipa *ipa, u32 item, size_t size)
  441. {
  442. struct device *dev = ipa->dev;
  443. struct iommu_domain *domain;
  444. unsigned long iova;
  445. phys_addr_t phys;
  446. phys_addr_t addr;
  447. size_t actual;
  448. void *virt;
  449. int ret;
  450. if (!size)
  451. return 0; /* SMEM memory not used */
  452. /* SMEM is memory shared between the AP and another system entity
  453. * (in this case, the modem). An allocation from SMEM is persistent
  454. * until the AP reboots; there is no way to free an allocated SMEM
  455. * region. Allocation only reserves the space; to use it you need
  456. * to "get" a pointer it (this does not imply reference counting).
  457. * The item might have already been allocated, in which case we
  458. * use it unless the size isn't what we expect.
  459. */
  460. ret = qcom_smem_alloc(QCOM_SMEM_HOST_MODEM, item, size);
  461. if (ret && ret != -EEXIST) {
  462. dev_err(dev, "error %d allocating size %zu SMEM item %u\n",
  463. ret, size, item);
  464. return ret;
  465. }
  466. /* Now get the address of the SMEM memory region */
  467. virt = qcom_smem_get(QCOM_SMEM_HOST_MODEM, item, &actual);
  468. if (IS_ERR(virt)) {
  469. ret = PTR_ERR(virt);
  470. dev_err(dev, "error %d getting SMEM item %u\n", ret, item);
  471. return ret;
  472. }
  473. /* In case the region was already allocated, verify the size */
  474. if (ret && actual != size) {
  475. dev_err(dev, "SMEM item %u has size %zu, expected %zu\n",
  476. item, actual, size);
  477. return -EINVAL;
  478. }
  479. domain = iommu_get_domain_for_dev(dev);
  480. if (!domain) {
  481. dev_err(dev, "no IOMMU domain found for SMEM\n");
  482. return -EINVAL;
  483. }
  484. /* Align the address down and the size up to a page boundary */
  485. addr = qcom_smem_virt_to_phys(virt);
  486. phys = addr & PAGE_MASK;
  487. size = PAGE_ALIGN(size + addr - phys);
  488. iova = phys; /* We just want a direct mapping */
  489. ret = iommu_map(domain, iova, phys, size, IOMMU_READ | IOMMU_WRITE,
  490. GFP_KERNEL);
  491. if (ret)
  492. return ret;
  493. ipa->smem_iova = iova;
  494. ipa->smem_size = size;
  495. return 0;
  496. }
  497. static void ipa_smem_exit(struct ipa *ipa)
  498. {
  499. struct device *dev = ipa->dev;
  500. struct iommu_domain *domain;
  501. domain = iommu_get_domain_for_dev(dev);
  502. if (domain) {
  503. size_t size;
  504. size = iommu_unmap(domain, ipa->smem_iova, ipa->smem_size);
  505. if (size != ipa->smem_size)
  506. dev_warn(dev, "unmapped %zu SMEM bytes, expected %zu\n",
  507. size, ipa->smem_size);
  508. } else {
  509. dev_err(dev, "couldn't get IPA IOMMU domain for SMEM\n");
  510. }
  511. ipa->smem_size = 0;
  512. ipa->smem_iova = 0;
  513. }
  514. /* Perform memory region-related initialization */
  515. int ipa_mem_init(struct ipa *ipa, struct platform_device *pdev,
  516. const struct ipa_mem_data *mem_data)
  517. {
  518. struct device *dev = &pdev->dev;
  519. struct resource *res;
  520. int ret;
  521. /* Make sure the set of defined memory regions is valid */
  522. if (!ipa_mem_valid(ipa, mem_data))
  523. return -EINVAL;
  524. ipa->mem_count = mem_data->local_count;
  525. ipa->mem = mem_data->local;
  526. /* Check the route and filter table memory regions */
  527. if (!ipa_table_mem_valid(ipa, false))
  528. return -EINVAL;
  529. if (!ipa_table_mem_valid(ipa, true))
  530. return -EINVAL;
  531. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
  532. if (ret) {
  533. dev_err(dev, "error %d setting DMA mask\n", ret);
  534. return ret;
  535. }
  536. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ipa-shared");
  537. if (!res) {
  538. dev_err(dev,
  539. "DT error getting \"ipa-shared\" memory property\n");
  540. return -ENODEV;
  541. }
  542. ipa->mem_virt = memremap(res->start, resource_size(res), MEMREMAP_WC);
  543. if (!ipa->mem_virt) {
  544. dev_err(dev, "unable to remap \"ipa-shared\" memory\n");
  545. return -ENOMEM;
  546. }
  547. ipa->mem_addr = res->start;
  548. ipa->mem_size = resource_size(res);
  549. ret = ipa_imem_init(ipa, mem_data->imem_addr, mem_data->imem_size);
  550. if (ret)
  551. goto err_unmap;
  552. ret = ipa_smem_init(ipa, mem_data->smem_id, mem_data->smem_size);
  553. if (ret)
  554. goto err_imem_exit;
  555. return 0;
  556. err_imem_exit:
  557. ipa_imem_exit(ipa);
  558. err_unmap:
  559. memunmap(ipa->mem_virt);
  560. return ret;
  561. }
  562. /* Inverse of ipa_mem_init() */
  563. void ipa_mem_exit(struct ipa *ipa)
  564. {
  565. ipa_smem_exit(ipa);
  566. ipa_imem_exit(ipa);
  567. memunmap(ipa->mem_virt);
  568. }