ipa_mem.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2019-2022 Linaro Ltd.
  4. */
  5. #include <linux/types.h>
  6. #include <linux/bitfield.h>
  7. #include <linux/bug.h>
  8. #include <linux/dma-mapping.h>
  9. #include <linux/iommu.h>
  10. #include <linux/io.h>
  11. #include <linux/soc/qcom/smem.h>
  12. #include "ipa.h"
  13. #include "ipa_reg.h"
  14. #include "ipa_data.h"
  15. #include "ipa_cmd.h"
  16. #include "ipa_mem.h"
  17. #include "ipa_table.h"
  18. #include "gsi_trans.h"
  19. /* "Canary" value placed between memory regions to detect overflow */
  20. #define IPA_MEM_CANARY_VAL cpu_to_le32(0xdeadbeef)
  21. /* SMEM host id representing the modem. */
  22. #define QCOM_SMEM_HOST_MODEM 1
  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_reg *reg;
  66. const struct ipa_mem *mem;
  67. struct gsi_trans *trans;
  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->pdev->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 = ipa_reg_encode(reg, IPA_BASE_ADDR, offset);
  99. iowrite32(val, ipa->reg_virt + ipa_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_NAT_TABLE:
  141. case IPA_MEM_STATS_FILTER_ROUTE:
  142. if (version < IPA_VERSION_4_5)
  143. return false;
  144. break;
  145. default:
  146. return false;
  147. }
  148. return true;
  149. }
  150. /* Must the given memory region be present in the configuration? */
  151. static bool ipa_mem_id_required(struct ipa *ipa, enum ipa_mem_id mem_id)
  152. {
  153. switch (mem_id) {
  154. case IPA_MEM_UC_SHARED:
  155. case IPA_MEM_UC_INFO:
  156. case IPA_MEM_V4_FILTER_HASHED:
  157. case IPA_MEM_V4_FILTER:
  158. case IPA_MEM_V6_FILTER_HASHED:
  159. case IPA_MEM_V6_FILTER:
  160. case IPA_MEM_V4_ROUTE_HASHED:
  161. case IPA_MEM_V4_ROUTE:
  162. case IPA_MEM_V6_ROUTE_HASHED:
  163. case IPA_MEM_V6_ROUTE:
  164. case IPA_MEM_MODEM_HEADER:
  165. case IPA_MEM_MODEM_PROC_CTX:
  166. case IPA_MEM_AP_PROC_CTX:
  167. case IPA_MEM_MODEM:
  168. return true;
  169. case IPA_MEM_PDN_CONFIG:
  170. case IPA_MEM_STATS_QUOTA_MODEM:
  171. case IPA_MEM_STATS_TETHERING:
  172. return ipa->version >= IPA_VERSION_4_0;
  173. default:
  174. return false; /* Anything else is optional */
  175. }
  176. }
  177. static bool ipa_mem_valid_one(struct ipa *ipa, const struct ipa_mem *mem)
  178. {
  179. struct device *dev = &ipa->pdev->dev;
  180. enum ipa_mem_id mem_id = mem->id;
  181. u16 size_multiple;
  182. /* Make sure the memory region is valid for this version of IPA */
  183. if (!ipa_mem_id_valid(ipa, mem_id)) {
  184. dev_err(dev, "region id %u not valid\n", mem_id);
  185. return false;
  186. }
  187. if (!mem->size && !mem->canary_count) {
  188. dev_err(dev, "empty memory region %u\n", mem_id);
  189. return false;
  190. }
  191. /* Other than modem memory, sizes must be a multiple of 8 */
  192. size_multiple = mem_id == IPA_MEM_MODEM ? 4 : 8;
  193. if (mem->size % size_multiple)
  194. dev_err(dev, "region %u size not a multiple of %u bytes\n",
  195. mem_id, size_multiple);
  196. else if (mem->offset % 8)
  197. dev_err(dev, "region %u offset not 8-byte aligned\n", mem_id);
  198. else if (mem->offset < mem->canary_count * sizeof(__le32))
  199. dev_err(dev, "region %u offset too small for %hu canaries\n",
  200. mem_id, mem->canary_count);
  201. else if (mem_id == IPA_MEM_END_MARKER && mem->size)
  202. dev_err(dev, "non-zero end marker region size\n");
  203. else
  204. return true;
  205. return false;
  206. }
  207. /* Verify each defined memory region is valid. */
  208. static bool ipa_mem_valid(struct ipa *ipa, const struct ipa_mem_data *mem_data)
  209. {
  210. DECLARE_BITMAP(regions, IPA_MEM_COUNT) = { };
  211. struct device *dev = &ipa->pdev->dev;
  212. enum ipa_mem_id mem_id;
  213. u32 i;
  214. if (mem_data->local_count > IPA_MEM_COUNT) {
  215. dev_err(dev, "too many memory regions (%u > %u)\n",
  216. mem_data->local_count, IPA_MEM_COUNT);
  217. return false;
  218. }
  219. for (i = 0; i < mem_data->local_count; i++) {
  220. const struct ipa_mem *mem = &mem_data->local[i];
  221. if (__test_and_set_bit(mem->id, regions)) {
  222. dev_err(dev, "duplicate memory region %u\n", mem->id);
  223. return false;
  224. }
  225. /* Defined regions have non-zero size and/or canary count */
  226. if (!ipa_mem_valid_one(ipa, mem))
  227. return false;
  228. }
  229. /* Now see if any required regions are not defined */
  230. for_each_clear_bit(mem_id, regions, IPA_MEM_COUNT) {
  231. if (ipa_mem_id_required(ipa, mem_id))
  232. dev_err(dev, "required memory region %u missing\n",
  233. mem_id);
  234. }
  235. return true;
  236. }
  237. /* Do all memory regions fit within the IPA local memory? */
  238. static bool ipa_mem_size_valid(struct ipa *ipa)
  239. {
  240. struct device *dev = &ipa->pdev->dev;
  241. u32 limit = ipa->mem_size;
  242. u32 i;
  243. for (i = 0; i < ipa->mem_count; i++) {
  244. const struct ipa_mem *mem = &ipa->mem[i];
  245. if (mem->offset + mem->size <= limit)
  246. continue;
  247. dev_err(dev, "region %u ends beyond memory limit (0x%08x)\n",
  248. mem->id, limit);
  249. return false;
  250. }
  251. return true;
  252. }
  253. /**
  254. * ipa_mem_config() - Configure IPA shared memory
  255. * @ipa: IPA pointer
  256. *
  257. * Return: 0 if successful, or a negative error code
  258. */
  259. int ipa_mem_config(struct ipa *ipa)
  260. {
  261. struct device *dev = &ipa->pdev->dev;
  262. const struct ipa_reg *reg;
  263. const struct ipa_mem *mem;
  264. dma_addr_t addr;
  265. u32 mem_size;
  266. void *virt;
  267. u32 val;
  268. u32 i;
  269. /* Check the advertised location and size of the shared memory area */
  270. reg = ipa_reg(ipa, SHARED_MEM_SIZE);
  271. val = ioread32(ipa->reg_virt + ipa_reg_offset(reg));
  272. /* The fields in the register are in 8 byte units */
  273. ipa->mem_offset = 8 * ipa_reg_decode(reg, MEM_BADDR, val);
  274. /* Make sure the end is within the region's mapped space */
  275. mem_size = 8 * ipa_reg_decode(reg, MEM_SIZE, val);
  276. /* If the sizes don't match, issue a warning */
  277. if (ipa->mem_offset + mem_size < ipa->mem_size) {
  278. dev_warn(dev, "limiting IPA memory size to 0x%08x\n",
  279. mem_size);
  280. ipa->mem_size = mem_size;
  281. } else if (ipa->mem_offset + mem_size > ipa->mem_size) {
  282. dev_dbg(dev, "ignoring larger reported memory size: 0x%08x\n",
  283. mem_size);
  284. }
  285. /* We know our memory size; make sure regions are all in range */
  286. if (!ipa_mem_size_valid(ipa))
  287. return -EINVAL;
  288. /* Prealloc DMA memory for zeroing regions */
  289. virt = dma_alloc_coherent(dev, IPA_MEM_MAX, &addr, GFP_KERNEL);
  290. if (!virt)
  291. return -ENOMEM;
  292. ipa->zero_addr = addr;
  293. ipa->zero_virt = virt;
  294. ipa->zero_size = IPA_MEM_MAX;
  295. /* For each defined region, write "canary" values in the
  296. * space prior to the region's base address if indicated.
  297. */
  298. for (i = 0; i < ipa->mem_count; i++) {
  299. u16 canary_count = ipa->mem[i].canary_count;
  300. __le32 *canary;
  301. if (!canary_count)
  302. continue;
  303. /* Write canary values in the space before the region */
  304. canary = ipa->mem_virt + ipa->mem_offset + ipa->mem[i].offset;
  305. do
  306. *--canary = IPA_MEM_CANARY_VAL;
  307. while (--canary_count);
  308. }
  309. /* Make sure filter and route table memory regions are valid */
  310. if (!ipa_table_valid(ipa))
  311. goto err_dma_free;
  312. /* Validate memory-related properties relevant to immediate commands */
  313. if (!ipa_cmd_data_valid(ipa))
  314. goto err_dma_free;
  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->pdev->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->pdev->dev,
  354. "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->pdev->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. 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 iommu_domain *domain;
  407. struct device *dev;
  408. if (!ipa->imem_size)
  409. return;
  410. dev = &ipa->pdev->dev;
  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. * @item: Item ID of SMEM memory
  428. * @size: Size (bytes) of SMEM memory region
  429. *
  430. * SMEM is a managed block of shared DRAM, from which numbered "items"
  431. * can be allocated. One item is designated for use by the IPA.
  432. *
  433. * The modem accesses SMEM memory directly, but the IPA accesses it
  434. * via the IOMMU, using the AP's credentials.
  435. *
  436. * If size provided is non-zero, we allocate it and map it for
  437. * access through the IOMMU.
  438. *
  439. * Note: @size and the item address are is not guaranteed to be page-aligned.
  440. */
  441. static int ipa_smem_init(struct ipa *ipa, u32 item, size_t size)
  442. {
  443. struct device *dev = &ipa->pdev->dev;
  444. struct iommu_domain *domain;
  445. unsigned long iova;
  446. phys_addr_t phys;
  447. phys_addr_t addr;
  448. size_t actual;
  449. void *virt;
  450. int ret;
  451. if (!size)
  452. return 0; /* SMEM memory not used */
  453. /* SMEM is memory shared between the AP and another system entity
  454. * (in this case, the modem). An allocation from SMEM is persistent
  455. * until the AP reboots; there is no way to free an allocated SMEM
  456. * region. Allocation only reserves the space; to use it you need
  457. * to "get" a pointer it (this does not imply reference counting).
  458. * The item might have already been allocated, in which case we
  459. * use it unless the size isn't what we expect.
  460. */
  461. ret = qcom_smem_alloc(QCOM_SMEM_HOST_MODEM, item, size);
  462. if (ret && ret != -EEXIST) {
  463. dev_err(dev, "error %d allocating size %zu SMEM item %u\n",
  464. ret, size, item);
  465. return ret;
  466. }
  467. /* Now get the address of the SMEM memory region */
  468. virt = qcom_smem_get(QCOM_SMEM_HOST_MODEM, item, &actual);
  469. if (IS_ERR(virt)) {
  470. ret = PTR_ERR(virt);
  471. dev_err(dev, "error %d getting SMEM item %u\n", ret, item);
  472. return ret;
  473. }
  474. /* In case the region was already allocated, verify the size */
  475. if (ret && actual != size) {
  476. dev_err(dev, "SMEM item %u has size %zu, expected %zu\n",
  477. item, actual, size);
  478. return -EINVAL;
  479. }
  480. domain = iommu_get_domain_for_dev(dev);
  481. if (!domain) {
  482. dev_err(dev, "no IOMMU domain found for SMEM\n");
  483. return -EINVAL;
  484. }
  485. /* Align the address down and the size up to a page boundary */
  486. addr = qcom_smem_virt_to_phys(virt);
  487. phys = addr & PAGE_MASK;
  488. size = PAGE_ALIGN(size + addr - phys);
  489. iova = phys; /* We just want a direct mapping */
  490. ret = iommu_map(domain, iova, phys, size, IOMMU_READ | IOMMU_WRITE);
  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->pdev->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, const struct ipa_mem_data *mem_data)
  516. {
  517. struct device *dev = &ipa->pdev->dev;
  518. struct resource *res;
  519. int ret;
  520. /* Make sure the set of defined memory regions is valid */
  521. if (!ipa_mem_valid(ipa, mem_data))
  522. return -EINVAL;
  523. ipa->mem_count = mem_data->local_count;
  524. ipa->mem = mem_data->local;
  525. ret = dma_set_mask_and_coherent(&ipa->pdev->dev, DMA_BIT_MASK(64));
  526. if (ret) {
  527. dev_err(dev, "error %d setting DMA mask\n", ret);
  528. return ret;
  529. }
  530. res = platform_get_resource_byname(ipa->pdev, IORESOURCE_MEM,
  531. "ipa-shared");
  532. if (!res) {
  533. dev_err(dev,
  534. "DT error getting \"ipa-shared\" memory property\n");
  535. return -ENODEV;
  536. }
  537. ipa->mem_virt = memremap(res->start, resource_size(res), MEMREMAP_WC);
  538. if (!ipa->mem_virt) {
  539. dev_err(dev, "unable to remap \"ipa-shared\" memory\n");
  540. return -ENOMEM;
  541. }
  542. ipa->mem_addr = res->start;
  543. ipa->mem_size = resource_size(res);
  544. ret = ipa_imem_init(ipa, mem_data->imem_addr, mem_data->imem_size);
  545. if (ret)
  546. goto err_unmap;
  547. ret = ipa_smem_init(ipa, mem_data->smem_id, mem_data->smem_size);
  548. if (ret)
  549. goto err_imem_exit;
  550. return 0;
  551. err_imem_exit:
  552. ipa_imem_exit(ipa);
  553. err_unmap:
  554. memunmap(ipa->mem_virt);
  555. return ret;
  556. }
  557. /* Inverse of ipa_mem_init() */
  558. void ipa_mem_exit(struct ipa *ipa)
  559. {
  560. ipa_smem_exit(ipa);
  561. ipa_imem_exit(ipa);
  562. memunmap(ipa->mem_virt);
  563. }