ipa_mem.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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. #define SMEM_IPA_FILTER_TABLE 497
  23. const struct ipa_mem *ipa_mem_find(struct ipa *ipa, enum ipa_mem_id mem_id)
  24. {
  25. u32 i;
  26. for (i = 0; i < ipa->mem_count; i++) {
  27. const struct ipa_mem *mem = &ipa->mem[i];
  28. if (mem->id == mem_id)
  29. return mem;
  30. }
  31. return NULL;
  32. }
  33. /* Add an immediate command to a transaction that zeroes a memory region */
  34. static void
  35. ipa_mem_zero_region_add(struct gsi_trans *trans, enum ipa_mem_id mem_id)
  36. {
  37. struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
  38. const struct ipa_mem *mem = ipa_mem_find(ipa, mem_id);
  39. dma_addr_t addr = ipa->zero_addr;
  40. if (!mem->size)
  41. return;
  42. ipa_cmd_dma_shared_mem_add(trans, mem->offset, mem->size, addr, true);
  43. }
  44. /**
  45. * ipa_mem_setup() - Set up IPA AP and modem shared memory areas
  46. * @ipa: IPA pointer
  47. *
  48. * Set up the shared memory regions in IPA local memory. This involves
  49. * zero-filling memory regions, and in the case of header memory, telling
  50. * the IPA where it's located.
  51. *
  52. * This function performs the initial setup of this memory. If the modem
  53. * crashes, its regions are re-zeroed in ipa_mem_zero_modem().
  54. *
  55. * The AP informs the modem where its portions of memory are located
  56. * in a QMI exchange that occurs at modem startup.
  57. *
  58. * There is no need for a matching ipa_mem_teardown() function.
  59. *
  60. * Return: 0 if successful, or a negative error code
  61. */
  62. int ipa_mem_setup(struct ipa *ipa)
  63. {
  64. dma_addr_t addr = ipa->zero_addr;
  65. const struct ipa_mem *mem;
  66. struct gsi_trans *trans;
  67. const struct reg *reg;
  68. u32 offset;
  69. u16 size;
  70. u32 val;
  71. /* Get a transaction to define the header memory region and to zero
  72. * the processing context and modem memory regions.
  73. */
  74. trans = ipa_cmd_trans_alloc(ipa, 4);
  75. if (!trans) {
  76. dev_err(ipa->dev, "no transaction for memory setup\n");
  77. return -EBUSY;
  78. }
  79. /* Initialize IPA-local header memory. The AP header region, if
  80. * present, is contiguous with and follows the modem header region,
  81. * and they are initialized together.
  82. */
  83. mem = ipa_mem_find(ipa, IPA_MEM_MODEM_HEADER);
  84. offset = mem->offset;
  85. size = mem->size;
  86. mem = ipa_mem_find(ipa, IPA_MEM_AP_HEADER);
  87. if (mem)
  88. size += mem->size;
  89. ipa_cmd_hdr_init_local_add(trans, offset, size, addr);
  90. ipa_mem_zero_region_add(trans, IPA_MEM_MODEM_PROC_CTX);
  91. ipa_mem_zero_region_add(trans, IPA_MEM_AP_PROC_CTX);
  92. ipa_mem_zero_region_add(trans, IPA_MEM_MODEM);
  93. gsi_trans_commit_wait(trans);
  94. /* Tell the hardware where the processing context area is located */
  95. mem = ipa_mem_find(ipa, IPA_MEM_MODEM_PROC_CTX);
  96. offset = ipa->mem_offset + mem->offset;
  97. reg = ipa_reg(ipa, LOCAL_PKT_PROC_CNTXT);
  98. val = reg_encode(reg, IPA_BASE_ADDR, offset);
  99. iowrite32(val, ipa->reg_virt + reg_offset(reg));
  100. return 0;
  101. }
  102. /* Is the given memory region ID is valid for the current IPA version? */
  103. static bool ipa_mem_id_valid(struct ipa *ipa, enum ipa_mem_id mem_id)
  104. {
  105. enum ipa_version version = ipa->version;
  106. switch (mem_id) {
  107. case IPA_MEM_UC_SHARED:
  108. case IPA_MEM_UC_INFO:
  109. case IPA_MEM_V4_FILTER_HASHED:
  110. case IPA_MEM_V4_FILTER:
  111. case IPA_MEM_V6_FILTER_HASHED:
  112. case IPA_MEM_V6_FILTER:
  113. case IPA_MEM_V4_ROUTE_HASHED:
  114. case IPA_MEM_V4_ROUTE:
  115. case IPA_MEM_V6_ROUTE_HASHED:
  116. case IPA_MEM_V6_ROUTE:
  117. case IPA_MEM_MODEM_HEADER:
  118. case IPA_MEM_AP_HEADER:
  119. case IPA_MEM_MODEM_PROC_CTX:
  120. case IPA_MEM_AP_PROC_CTX:
  121. case IPA_MEM_MODEM:
  122. case IPA_MEM_UC_EVENT_RING:
  123. case IPA_MEM_PDN_CONFIG:
  124. case IPA_MEM_STATS_QUOTA_MODEM:
  125. case IPA_MEM_STATS_QUOTA_AP:
  126. case IPA_MEM_END_MARKER: /* pseudo region */
  127. break;
  128. case IPA_MEM_STATS_TETHERING:
  129. case IPA_MEM_STATS_DROP:
  130. if (version < IPA_VERSION_4_0)
  131. return false;
  132. break;
  133. case IPA_MEM_STATS_V4_FILTER:
  134. case IPA_MEM_STATS_V6_FILTER:
  135. case IPA_MEM_STATS_V4_ROUTE:
  136. case IPA_MEM_STATS_V6_ROUTE:
  137. if (version < IPA_VERSION_4_0 || version > IPA_VERSION_4_2)
  138. return false;
  139. break;
  140. case IPA_MEM_AP_V4_FILTER:
  141. case IPA_MEM_AP_V6_FILTER:
  142. if (version < IPA_VERSION_5_0)
  143. return false;
  144. break;
  145. case IPA_MEM_NAT_TABLE:
  146. case IPA_MEM_STATS_FILTER_ROUTE:
  147. if (version < IPA_VERSION_4_5)
  148. return false;
  149. break;
  150. default:
  151. return false;
  152. }
  153. return true;
  154. }
  155. /* Must the given memory region be present in the configuration? */
  156. static bool ipa_mem_id_required(struct ipa *ipa, enum ipa_mem_id mem_id)
  157. {
  158. switch (mem_id) {
  159. case IPA_MEM_UC_SHARED:
  160. case IPA_MEM_UC_INFO:
  161. case IPA_MEM_V4_FILTER_HASHED:
  162. case IPA_MEM_V4_FILTER:
  163. case IPA_MEM_V6_FILTER_HASHED:
  164. case IPA_MEM_V6_FILTER:
  165. case IPA_MEM_V4_ROUTE_HASHED:
  166. case IPA_MEM_V4_ROUTE:
  167. case IPA_MEM_V6_ROUTE_HASHED:
  168. case IPA_MEM_V6_ROUTE:
  169. case IPA_MEM_MODEM_HEADER:
  170. case IPA_MEM_MODEM_PROC_CTX:
  171. case IPA_MEM_AP_PROC_CTX:
  172. case IPA_MEM_MODEM:
  173. return true;
  174. case IPA_MEM_PDN_CONFIG:
  175. case IPA_MEM_STATS_QUOTA_MODEM:
  176. return ipa->version >= IPA_VERSION_4_0;
  177. case IPA_MEM_STATS_TETHERING:
  178. return ipa->version >= IPA_VERSION_4_0 &&
  179. ipa->version != IPA_VERSION_5_0;
  180. default:
  181. return false; /* Anything else is optional */
  182. }
  183. }
  184. static bool ipa_mem_valid_one(struct ipa *ipa, const struct ipa_mem *mem)
  185. {
  186. enum ipa_mem_id mem_id = mem->id;
  187. struct device *dev = ipa->dev;
  188. u16 size_multiple;
  189. /* Make sure the memory region is valid for this version of IPA */
  190. if (!ipa_mem_id_valid(ipa, mem_id)) {
  191. dev_err(dev, "region id %u not valid\n", mem_id);
  192. return false;
  193. }
  194. if (!mem->size && !mem->canary_count) {
  195. dev_err(dev, "empty memory region %u\n", mem_id);
  196. return false;
  197. }
  198. /* Other than modem memory, sizes must be a multiple of 8 */
  199. size_multiple = mem_id == IPA_MEM_MODEM ? 4 : 8;
  200. if (mem->size % size_multiple)
  201. dev_err(dev, "region %u size not a multiple of %u bytes\n",
  202. mem_id, size_multiple);
  203. else if (mem->offset % 8)
  204. dev_err(dev, "region %u offset not 8-byte aligned\n", mem_id);
  205. else if (mem->offset < mem->canary_count * sizeof(__le32))
  206. dev_err(dev, "region %u offset too small for %hu canaries\n",
  207. mem_id, mem->canary_count);
  208. else if (mem_id == IPA_MEM_END_MARKER && mem->size)
  209. dev_err(dev, "non-zero end marker region size\n");
  210. else
  211. return true;
  212. return false;
  213. }
  214. /* Verify each defined memory region is valid. */
  215. static bool ipa_mem_valid(struct ipa *ipa, const struct ipa_mem_data *mem_data)
  216. {
  217. DECLARE_BITMAP(regions, IPA_MEM_COUNT) = { };
  218. struct device *dev = ipa->dev;
  219. enum ipa_mem_id mem_id;
  220. u32 i;
  221. if (mem_data->local_count > IPA_MEM_COUNT) {
  222. dev_err(dev, "too many memory regions (%u > %u)\n",
  223. mem_data->local_count, IPA_MEM_COUNT);
  224. return false;
  225. }
  226. for (i = 0; i < mem_data->local_count; i++) {
  227. const struct ipa_mem *mem = &mem_data->local[i];
  228. if (__test_and_set_bit(mem->id, regions)) {
  229. dev_err(dev, "duplicate memory region %u\n", mem->id);
  230. return false;
  231. }
  232. /* Defined regions have non-zero size and/or canary count */
  233. if (!ipa_mem_valid_one(ipa, mem))
  234. return false;
  235. }
  236. /* Now see if any required regions are not defined */
  237. for_each_clear_bit(mem_id, regions, IPA_MEM_COUNT) {
  238. if (ipa_mem_id_required(ipa, mem_id))
  239. dev_err(dev, "required memory region %u missing\n",
  240. mem_id);
  241. }
  242. return true;
  243. }
  244. /* Do all memory regions fit within the IPA local memory? */
  245. static bool ipa_mem_size_valid(struct ipa *ipa)
  246. {
  247. struct device *dev = ipa->dev;
  248. u32 limit = ipa->mem_size;
  249. u32 i;
  250. for (i = 0; i < ipa->mem_count; i++) {
  251. const struct ipa_mem *mem = &ipa->mem[i];
  252. if (mem->offset + mem->size <= limit)
  253. continue;
  254. dev_err(dev, "region %u ends beyond memory limit (0x%08x)\n",
  255. mem->id, limit);
  256. return false;
  257. }
  258. return true;
  259. }
  260. /**
  261. * ipa_mem_config() - Configure IPA shared memory
  262. * @ipa: IPA pointer
  263. *
  264. * Return: 0 if successful, or a negative error code
  265. */
  266. int ipa_mem_config(struct ipa *ipa)
  267. {
  268. struct device *dev = ipa->dev;
  269. const struct ipa_mem *mem;
  270. const struct reg *reg;
  271. dma_addr_t addr;
  272. u32 mem_size;
  273. void *virt;
  274. u32 val;
  275. u32 i;
  276. /* Check the advertised location and size of the shared memory area */
  277. reg = ipa_reg(ipa, SHARED_MEM_SIZE);
  278. val = ioread32(ipa->reg_virt + reg_offset(reg));
  279. /* The fields in the register are in 8 byte units */
  280. ipa->mem_offset = 8 * reg_decode(reg, MEM_BADDR, val);
  281. /* Make sure the end is within the region's mapped space */
  282. mem_size = 8 * reg_decode(reg, MEM_SIZE, val);
  283. /* If the sizes don't match, issue a warning */
  284. if (ipa->mem_offset + mem_size < ipa->mem_size) {
  285. dev_warn(dev, "limiting IPA memory size to 0x%08x\n",
  286. mem_size);
  287. ipa->mem_size = mem_size;
  288. } else if (ipa->mem_offset + mem_size > ipa->mem_size) {
  289. dev_dbg(dev, "ignoring larger reported memory size: 0x%08x\n",
  290. mem_size);
  291. }
  292. /* We know our memory size; make sure regions are all in range */
  293. if (!ipa_mem_size_valid(ipa))
  294. return -EINVAL;
  295. /* Prealloc DMA memory for zeroing regions */
  296. virt = dma_alloc_coherent(dev, IPA_MEM_MAX, &addr, GFP_KERNEL);
  297. if (!virt)
  298. return -ENOMEM;
  299. ipa->zero_addr = addr;
  300. ipa->zero_virt = virt;
  301. ipa->zero_size = IPA_MEM_MAX;
  302. /* For each defined region, write "canary" values in the
  303. * space prior to the region's base address if indicated.
  304. */
  305. for (i = 0; i < ipa->mem_count; i++) {
  306. u16 canary_count = ipa->mem[i].canary_count;
  307. __le32 *canary;
  308. if (!canary_count)
  309. continue;
  310. /* Write canary values in the space before the region */
  311. canary = ipa->mem_virt + ipa->mem_offset + ipa->mem[i].offset;
  312. do
  313. *--canary = IPA_MEM_CANARY_VAL;
  314. while (--canary_count);
  315. }
  316. /* Verify the microcontroller ring alignment (if defined) */
  317. mem = ipa_mem_find(ipa, IPA_MEM_UC_EVENT_RING);
  318. if (mem && mem->offset % 1024) {
  319. dev_err(dev, "microcontroller ring not 1024-byte aligned\n");
  320. goto err_dma_free;
  321. }
  322. return 0;
  323. err_dma_free:
  324. dma_free_coherent(dev, IPA_MEM_MAX, ipa->zero_virt, ipa->zero_addr);
  325. return -EINVAL;
  326. }
  327. /* Inverse of ipa_mem_config() */
  328. void ipa_mem_deconfig(struct ipa *ipa)
  329. {
  330. struct device *dev = ipa->dev;
  331. dma_free_coherent(dev, ipa->zero_size, ipa->zero_virt, ipa->zero_addr);
  332. ipa->zero_size = 0;
  333. ipa->zero_virt = NULL;
  334. ipa->zero_addr = 0;
  335. }
  336. /**
  337. * ipa_mem_zero_modem() - Zero IPA-local memory regions owned by the modem
  338. * @ipa: IPA pointer
  339. *
  340. * Zero regions of IPA-local memory used by the modem. These are configured
  341. * (and initially zeroed) by ipa_mem_setup(), but if the modem crashes and
  342. * restarts via SSR we need to re-initialize them. A QMI message tells the
  343. * modem where to find regions of IPA local memory it needs to know about
  344. * (these included).
  345. */
  346. int ipa_mem_zero_modem(struct ipa *ipa)
  347. {
  348. struct gsi_trans *trans;
  349. /* Get a transaction to zero the modem memory, modem header,
  350. * and modem processing context regions.
  351. */
  352. trans = ipa_cmd_trans_alloc(ipa, 3);
  353. if (!trans) {
  354. dev_err(ipa->dev, "no transaction to zero modem memory\n");
  355. return -EBUSY;
  356. }
  357. ipa_mem_zero_region_add(trans, IPA_MEM_MODEM_HEADER);
  358. ipa_mem_zero_region_add(trans, IPA_MEM_MODEM_PROC_CTX);
  359. ipa_mem_zero_region_add(trans, IPA_MEM_MODEM);
  360. gsi_trans_commit_wait(trans);
  361. return 0;
  362. }
  363. /**
  364. * ipa_imem_init() - Initialize IMEM memory used by the IPA
  365. * @ipa: IPA pointer
  366. * @addr: Physical address of the IPA region in IMEM
  367. * @size: Size (bytes) of the IPA region in IMEM
  368. *
  369. * IMEM is a block of shared memory separate from system DRAM, and
  370. * a portion of this memory is available for the IPA to use. The
  371. * modem accesses this memory directly, but the IPA accesses it
  372. * via the IOMMU, using the AP's credentials.
  373. *
  374. * If this region exists (size > 0) we map it for read/write access
  375. * through the IOMMU using the IPA device.
  376. *
  377. * Note: @addr and @size are not guaranteed to be page-aligned.
  378. */
  379. static int ipa_imem_init(struct ipa *ipa, unsigned long addr, size_t size)
  380. {
  381. struct device *dev = ipa->dev;
  382. struct iommu_domain *domain;
  383. unsigned long iova;
  384. phys_addr_t phys;
  385. int ret;
  386. if (!size)
  387. return 0; /* IMEM memory not used */
  388. domain = iommu_get_domain_for_dev(dev);
  389. if (!domain) {
  390. dev_err(dev, "no IOMMU domain found for IMEM\n");
  391. return -EINVAL;
  392. }
  393. /* Align the address down and the size up to page boundaries */
  394. phys = addr & PAGE_MASK;
  395. size = PAGE_ALIGN(size + addr - phys);
  396. iova = phys; /* We just want a direct mapping */
  397. ret = iommu_map(domain, iova, phys, size, IOMMU_READ | IOMMU_WRITE,
  398. GFP_KERNEL);
  399. if (ret)
  400. return ret;
  401. ipa->imem_iova = iova;
  402. ipa->imem_size = size;
  403. return 0;
  404. }
  405. static void ipa_imem_exit(struct ipa *ipa)
  406. {
  407. struct device *dev = ipa->dev;
  408. struct iommu_domain *domain;
  409. if (!ipa->imem_size)
  410. return;
  411. domain = iommu_get_domain_for_dev(dev);
  412. if (domain) {
  413. size_t size;
  414. size = iommu_unmap(domain, ipa->imem_iova, ipa->imem_size);
  415. if (size != ipa->imem_size)
  416. dev_warn(dev, "unmapped %zu IMEM bytes, expected %zu\n",
  417. size, ipa->imem_size);
  418. } else {
  419. dev_err(dev, "couldn't get IPA IOMMU domain for IMEM\n");
  420. }
  421. ipa->imem_size = 0;
  422. ipa->imem_iova = 0;
  423. }
  424. /**
  425. * ipa_smem_init() - Initialize SMEM memory used by the IPA
  426. * @ipa: IPA pointer
  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, 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, SMEM_IPA_FILTER_TABLE, size);
  461. if (ret && ret != -EEXIST) {
  462. dev_err(dev, "error %d allocating size %zu SMEM item\n",
  463. ret, size);
  464. return ret;
  465. }
  466. /* Now get the address of the SMEM memory region */
  467. virt = qcom_smem_get(QCOM_SMEM_HOST_MODEM, SMEM_IPA_FILTER_TABLE, &actual);
  468. if (IS_ERR(virt)) {
  469. ret = PTR_ERR(virt);
  470. dev_err(dev, "error %d getting SMEM item\n", ret);
  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 has size %zu, expected %zu\n",
  476. 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_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. }