iovmm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. /*
  2. * omap iommu: simple virtual address space management
  3. *
  4. * Copyright (C) 2008-2009 Nokia Corporation
  5. *
  6. * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/err.h>
  13. #include <linux/slab.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/device.h>
  16. #include <linux/scatterlist.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/mach/map.h>
  19. #include <plat/iommu.h>
  20. #include <plat/iovmm.h>
  21. #include "iopgtable.h"
  22. /*
  23. * A device driver needs to create address mappings between:
  24. *
  25. * - iommu/device address
  26. * - physical address
  27. * - mpu virtual address
  28. *
  29. * There are 4 possible patterns for them:
  30. *
  31. * |iova/ mapping iommu_ page
  32. * | da pa va (d)-(p)-(v) function type
  33. * ---------------------------------------------------------------------------
  34. * 1 | c c c 1 - 1 - 1 _kmap() / _kunmap() s
  35. * 2 | c c,a c 1 - 1 - 1 _kmalloc()/ _kfree() s
  36. * 3 | c d c 1 - n - 1 _vmap() / _vunmap() s
  37. * 4 | c d,a c 1 - n - 1 _vmalloc()/ _vfree() n*
  38. *
  39. *
  40. * 'iova': device iommu virtual address
  41. * 'da': alias of 'iova'
  42. * 'pa': physical address
  43. * 'va': mpu virtual address
  44. *
  45. * 'c': contiguous memory area
  46. * 'd': discontiguous memory area
  47. * 'a': anonymous memory allocation
  48. * '()': optional feature
  49. *
  50. * 'n': a normal page(4KB) size is used.
  51. * 's': multiple iommu superpage(16MB, 1MB, 64KB, 4KB) size is used.
  52. *
  53. * '*': not yet, but feasible.
  54. */
  55. static struct kmem_cache *iovm_area_cachep;
  56. /* return total bytes of sg buffers */
  57. static size_t sgtable_len(const struct sg_table *sgt)
  58. {
  59. unsigned int i, total = 0;
  60. struct scatterlist *sg;
  61. if (!sgt)
  62. return 0;
  63. for_each_sg(sgt->sgl, sg, sgt->nents, i) {
  64. size_t bytes;
  65. bytes = sg_dma_len(sg);
  66. if (!iopgsz_ok(bytes)) {
  67. pr_err("%s: sg[%d] not iommu pagesize(%x)\n",
  68. __func__, i, bytes);
  69. return 0;
  70. }
  71. total += bytes;
  72. }
  73. return total;
  74. }
  75. #define sgtable_ok(x) (!!sgtable_len(x))
  76. /*
  77. * calculate the optimal number sg elements from total bytes based on
  78. * iommu superpages
  79. */
  80. static unsigned int sgtable_nents(size_t bytes)
  81. {
  82. int i;
  83. unsigned int nr_entries;
  84. const unsigned long pagesize[] = { SZ_16M, SZ_1M, SZ_64K, SZ_4K, };
  85. if (!IS_ALIGNED(bytes, PAGE_SIZE)) {
  86. pr_err("%s: wrong size %08x\n", __func__, bytes);
  87. return 0;
  88. }
  89. nr_entries = 0;
  90. for (i = 0; i < ARRAY_SIZE(pagesize); i++) {
  91. if (bytes >= pagesize[i]) {
  92. nr_entries += (bytes / pagesize[i]);
  93. bytes %= pagesize[i];
  94. }
  95. }
  96. BUG_ON(bytes);
  97. return nr_entries;
  98. }
  99. /* allocate and initialize sg_table header(a kind of 'superblock') */
  100. static struct sg_table *sgtable_alloc(const size_t bytes, u32 flags)
  101. {
  102. unsigned int nr_entries;
  103. int err;
  104. struct sg_table *sgt;
  105. if (!bytes)
  106. return ERR_PTR(-EINVAL);
  107. if (!IS_ALIGNED(bytes, PAGE_SIZE))
  108. return ERR_PTR(-EINVAL);
  109. /* FIXME: IOVMF_DA_FIXED should support 'superpages' */
  110. if ((flags & IOVMF_LINEAR) && (flags & IOVMF_DA_ANON)) {
  111. nr_entries = sgtable_nents(bytes);
  112. if (!nr_entries)
  113. return ERR_PTR(-EINVAL);
  114. } else
  115. nr_entries = bytes / PAGE_SIZE;
  116. sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
  117. if (!sgt)
  118. return ERR_PTR(-ENOMEM);
  119. err = sg_alloc_table(sgt, nr_entries, GFP_KERNEL);
  120. if (err)
  121. return ERR_PTR(err);
  122. pr_debug("%s: sgt:%p(%d entries)\n", __func__, sgt, nr_entries);
  123. return sgt;
  124. }
  125. /* free sg_table header(a kind of superblock) */
  126. static void sgtable_free(struct sg_table *sgt)
  127. {
  128. if (!sgt)
  129. return;
  130. sg_free_table(sgt);
  131. kfree(sgt);
  132. pr_debug("%s: sgt:%p\n", __func__, sgt);
  133. }
  134. /* map 'sglist' to a contiguous mpu virtual area and return 'va' */
  135. static void *vmap_sg(const struct sg_table *sgt)
  136. {
  137. u32 va;
  138. size_t total;
  139. unsigned int i;
  140. struct scatterlist *sg;
  141. struct vm_struct *new;
  142. const struct mem_type *mtype;
  143. mtype = get_mem_type(MT_DEVICE);
  144. if (!mtype)
  145. return ERR_PTR(-EINVAL);
  146. total = sgtable_len(sgt);
  147. if (!total)
  148. return ERR_PTR(-EINVAL);
  149. new = __get_vm_area(total, VM_IOREMAP, VMALLOC_START, VMALLOC_END);
  150. if (!new)
  151. return ERR_PTR(-ENOMEM);
  152. va = (u32)new->addr;
  153. for_each_sg(sgt->sgl, sg, sgt->nents, i) {
  154. size_t bytes;
  155. u32 pa;
  156. int err;
  157. pa = sg_phys(sg);
  158. bytes = sg_dma_len(sg);
  159. BUG_ON(bytes != PAGE_SIZE);
  160. err = ioremap_page(va, pa, mtype);
  161. if (err)
  162. goto err_out;
  163. va += bytes;
  164. }
  165. flush_cache_vmap((unsigned long)new->addr,
  166. (unsigned long)(new->addr + total));
  167. return new->addr;
  168. err_out:
  169. WARN_ON(1); /* FIXME: cleanup some mpu mappings */
  170. vunmap(new->addr);
  171. return ERR_PTR(-EAGAIN);
  172. }
  173. static inline void vunmap_sg(const void *va)
  174. {
  175. vunmap(va);
  176. }
  177. static struct iovm_struct *__find_iovm_area(struct iommu *obj, const u32 da)
  178. {
  179. struct iovm_struct *tmp;
  180. list_for_each_entry(tmp, &obj->mmap, list) {
  181. if ((da >= tmp->da_start) && (da < tmp->da_end)) {
  182. size_t len;
  183. len = tmp->da_end - tmp->da_start;
  184. dev_dbg(obj->dev, "%s: %08x-%08x-%08x(%x) %08x\n",
  185. __func__, tmp->da_start, da, tmp->da_end, len,
  186. tmp->flags);
  187. return tmp;
  188. }
  189. }
  190. return NULL;
  191. }
  192. /**
  193. * find_iovm_area - find iovma which includes @da
  194. * @da: iommu device virtual address
  195. *
  196. * Find the existing iovma starting at @da
  197. */
  198. struct iovm_struct *find_iovm_area(struct iommu *obj, u32 da)
  199. {
  200. struct iovm_struct *area;
  201. mutex_lock(&obj->mmap_lock);
  202. area = __find_iovm_area(obj, da);
  203. mutex_unlock(&obj->mmap_lock);
  204. return area;
  205. }
  206. EXPORT_SYMBOL_GPL(find_iovm_area);
  207. /*
  208. * This finds the hole(area) which fits the requested address and len
  209. * in iovmas mmap, and returns the new allocated iovma.
  210. */
  211. static struct iovm_struct *alloc_iovm_area(struct iommu *obj, u32 da,
  212. size_t bytes, u32 flags)
  213. {
  214. struct iovm_struct *new, *tmp;
  215. u32 start, prev_end, alignement;
  216. if (!obj || !bytes)
  217. return ERR_PTR(-EINVAL);
  218. start = da;
  219. alignement = PAGE_SIZE;
  220. if (flags & IOVMF_DA_ANON) {
  221. /*
  222. * Reserve the first page for NULL
  223. */
  224. start = PAGE_SIZE;
  225. if (flags & IOVMF_LINEAR)
  226. alignement = iopgsz_max(bytes);
  227. start = roundup(start, alignement);
  228. }
  229. tmp = NULL;
  230. if (list_empty(&obj->mmap))
  231. goto found;
  232. prev_end = 0;
  233. list_for_each_entry(tmp, &obj->mmap, list) {
  234. if (prev_end >= start)
  235. break;
  236. if (start + bytes < tmp->da_start)
  237. goto found;
  238. if (flags & IOVMF_DA_ANON)
  239. start = roundup(tmp->da_end + 1, alignement);
  240. prev_end = tmp->da_end;
  241. }
  242. if ((start > prev_end) && (ULONG_MAX - start >= bytes))
  243. goto found;
  244. dev_dbg(obj->dev, "%s: no space to fit %08x(%x) flags: %08x\n",
  245. __func__, da, bytes, flags);
  246. return ERR_PTR(-EINVAL);
  247. found:
  248. new = kmem_cache_zalloc(iovm_area_cachep, GFP_KERNEL);
  249. if (!new)
  250. return ERR_PTR(-ENOMEM);
  251. new->iommu = obj;
  252. new->da_start = start;
  253. new->da_end = start + bytes;
  254. new->flags = flags;
  255. /*
  256. * keep ascending order of iovmas
  257. */
  258. if (tmp)
  259. list_add_tail(&new->list, &tmp->list);
  260. else
  261. list_add(&new->list, &obj->mmap);
  262. dev_dbg(obj->dev, "%s: found %08x-%08x-%08x(%x) %08x\n",
  263. __func__, new->da_start, start, new->da_end, bytes, flags);
  264. return new;
  265. }
  266. static void free_iovm_area(struct iommu *obj, struct iovm_struct *area)
  267. {
  268. size_t bytes;
  269. BUG_ON(!obj || !area);
  270. bytes = area->da_end - area->da_start;
  271. dev_dbg(obj->dev, "%s: %08x-%08x(%x) %08x\n",
  272. __func__, area->da_start, area->da_end, bytes, area->flags);
  273. list_del(&area->list);
  274. kmem_cache_free(iovm_area_cachep, area);
  275. }
  276. /**
  277. * da_to_va - convert (d) to (v)
  278. * @obj: objective iommu
  279. * @da: iommu device virtual address
  280. * @va: mpu virtual address
  281. *
  282. * Returns mpu virtual addr which corresponds to a given device virtual addr
  283. */
  284. void *da_to_va(struct iommu *obj, u32 da)
  285. {
  286. void *va = NULL;
  287. struct iovm_struct *area;
  288. mutex_lock(&obj->mmap_lock);
  289. area = __find_iovm_area(obj, da);
  290. if (!area) {
  291. dev_dbg(obj->dev, "%s: no da area(%08x)\n", __func__, da);
  292. goto out;
  293. }
  294. va = area->va;
  295. out:
  296. mutex_unlock(&obj->mmap_lock);
  297. return va;
  298. }
  299. EXPORT_SYMBOL_GPL(da_to_va);
  300. static void sgtable_fill_vmalloc(struct sg_table *sgt, void *_va)
  301. {
  302. unsigned int i;
  303. struct scatterlist *sg;
  304. void *va = _va;
  305. void *va_end;
  306. for_each_sg(sgt->sgl, sg, sgt->nents, i) {
  307. struct page *pg;
  308. const size_t bytes = PAGE_SIZE;
  309. /*
  310. * iommu 'superpage' isn't supported with 'iommu_vmalloc()'
  311. */
  312. pg = vmalloc_to_page(va);
  313. BUG_ON(!pg);
  314. sg_set_page(sg, pg, bytes, 0);
  315. va += bytes;
  316. }
  317. va_end = _va + PAGE_SIZE * i;
  318. }
  319. static inline void sgtable_drain_vmalloc(struct sg_table *sgt)
  320. {
  321. /*
  322. * Actually this is not necessary at all, just exists for
  323. * consistency of the code readability.
  324. */
  325. BUG_ON(!sgt);
  326. }
  327. static void sgtable_fill_kmalloc(struct sg_table *sgt, u32 pa, size_t len)
  328. {
  329. unsigned int i;
  330. struct scatterlist *sg;
  331. void *va;
  332. va = phys_to_virt(pa);
  333. for_each_sg(sgt->sgl, sg, sgt->nents, i) {
  334. size_t bytes;
  335. bytes = iopgsz_max(len);
  336. BUG_ON(!iopgsz_ok(bytes));
  337. sg_set_buf(sg, phys_to_virt(pa), bytes);
  338. /*
  339. * 'pa' is cotinuous(linear).
  340. */
  341. pa += bytes;
  342. len -= bytes;
  343. }
  344. BUG_ON(len);
  345. }
  346. static inline void sgtable_drain_kmalloc(struct sg_table *sgt)
  347. {
  348. /*
  349. * Actually this is not necessary at all, just exists for
  350. * consistency of the code readability
  351. */
  352. BUG_ON(!sgt);
  353. }
  354. /* create 'da' <-> 'pa' mapping from 'sgt' */
  355. static int map_iovm_area(struct iommu *obj, struct iovm_struct *new,
  356. const struct sg_table *sgt, u32 flags)
  357. {
  358. int err;
  359. unsigned int i, j;
  360. struct scatterlist *sg;
  361. u32 da = new->da_start;
  362. if (!obj || !sgt)
  363. return -EINVAL;
  364. BUG_ON(!sgtable_ok(sgt));
  365. for_each_sg(sgt->sgl, sg, sgt->nents, i) {
  366. u32 pa;
  367. int pgsz;
  368. size_t bytes;
  369. struct iotlb_entry e;
  370. pa = sg_phys(sg);
  371. bytes = sg_dma_len(sg);
  372. flags &= ~IOVMF_PGSZ_MASK;
  373. pgsz = bytes_to_iopgsz(bytes);
  374. if (pgsz < 0)
  375. goto err_out;
  376. flags |= pgsz;
  377. pr_debug("%s: [%d] %08x %08x(%x)\n", __func__,
  378. i, da, pa, bytes);
  379. iotlb_init_entry(&e, da, pa, flags);
  380. err = iopgtable_store_entry(obj, &e);
  381. if (err)
  382. goto err_out;
  383. da += bytes;
  384. }
  385. return 0;
  386. err_out:
  387. da = new->da_start;
  388. for_each_sg(sgt->sgl, sg, i, j) {
  389. size_t bytes;
  390. bytes = iopgtable_clear_entry(obj, da);
  391. BUG_ON(!iopgsz_ok(bytes));
  392. da += bytes;
  393. }
  394. return err;
  395. }
  396. /* release 'da' <-> 'pa' mapping */
  397. static void unmap_iovm_area(struct iommu *obj, struct iovm_struct *area)
  398. {
  399. u32 start;
  400. size_t total = area->da_end - area->da_start;
  401. BUG_ON((!total) || !IS_ALIGNED(total, PAGE_SIZE));
  402. start = area->da_start;
  403. while (total > 0) {
  404. size_t bytes;
  405. bytes = iopgtable_clear_entry(obj, start);
  406. if (bytes == 0)
  407. bytes = PAGE_SIZE;
  408. else
  409. dev_dbg(obj->dev, "%s: unmap %08x(%x) %08x\n",
  410. __func__, start, bytes, area->flags);
  411. BUG_ON(!IS_ALIGNED(bytes, PAGE_SIZE));
  412. total -= bytes;
  413. start += bytes;
  414. }
  415. BUG_ON(total);
  416. }
  417. /* template function for all unmapping */
  418. static struct sg_table *unmap_vm_area(struct iommu *obj, const u32 da,
  419. void (*fn)(const void *), u32 flags)
  420. {
  421. struct sg_table *sgt = NULL;
  422. struct iovm_struct *area;
  423. if (!IS_ALIGNED(da, PAGE_SIZE)) {
  424. dev_err(obj->dev, "%s: alignment err(%08x)\n", __func__, da);
  425. return NULL;
  426. }
  427. mutex_lock(&obj->mmap_lock);
  428. area = __find_iovm_area(obj, da);
  429. if (!area) {
  430. dev_dbg(obj->dev, "%s: no da area(%08x)\n", __func__, da);
  431. goto out;
  432. }
  433. if ((area->flags & flags) != flags) {
  434. dev_err(obj->dev, "%s: wrong flags(%08x)\n", __func__,
  435. area->flags);
  436. goto out;
  437. }
  438. sgt = (struct sg_table *)area->sgt;
  439. unmap_iovm_area(obj, area);
  440. fn(area->va);
  441. dev_dbg(obj->dev, "%s: %08x-%08x-%08x(%x) %08x\n", __func__,
  442. area->da_start, da, area->da_end,
  443. area->da_end - area->da_start, area->flags);
  444. free_iovm_area(obj, area);
  445. out:
  446. mutex_unlock(&obj->mmap_lock);
  447. return sgt;
  448. }
  449. static u32 map_iommu_region(struct iommu *obj, u32 da,
  450. const struct sg_table *sgt, void *va, size_t bytes, u32 flags)
  451. {
  452. int err = -ENOMEM;
  453. struct iovm_struct *new;
  454. mutex_lock(&obj->mmap_lock);
  455. new = alloc_iovm_area(obj, da, bytes, flags);
  456. if (IS_ERR(new)) {
  457. err = PTR_ERR(new);
  458. goto err_alloc_iovma;
  459. }
  460. new->va = va;
  461. new->sgt = sgt;
  462. if (map_iovm_area(obj, new, sgt, new->flags))
  463. goto err_map;
  464. mutex_unlock(&obj->mmap_lock);
  465. dev_dbg(obj->dev, "%s: da:%08x(%x) flags:%08x va:%p\n",
  466. __func__, new->da_start, bytes, new->flags, va);
  467. return new->da_start;
  468. err_map:
  469. free_iovm_area(obj, new);
  470. err_alloc_iovma:
  471. mutex_unlock(&obj->mmap_lock);
  472. return err;
  473. }
  474. static inline u32 __iommu_vmap(struct iommu *obj, u32 da,
  475. const struct sg_table *sgt, void *va, size_t bytes, u32 flags)
  476. {
  477. return map_iommu_region(obj, da, sgt, va, bytes, flags);
  478. }
  479. /**
  480. * iommu_vmap - (d)-(p)-(v) address mapper
  481. * @obj: objective iommu
  482. * @sgt: address of scatter gather table
  483. * @flags: iovma and page property
  484. *
  485. * Creates 1-n-1 mapping with given @sgt and returns @da.
  486. * All @sgt element must be io page size aligned.
  487. */
  488. u32 iommu_vmap(struct iommu *obj, u32 da, const struct sg_table *sgt,
  489. u32 flags)
  490. {
  491. size_t bytes;
  492. void *va = NULL;
  493. if (!obj || !obj->dev || !sgt)
  494. return -EINVAL;
  495. bytes = sgtable_len(sgt);
  496. if (!bytes)
  497. return -EINVAL;
  498. bytes = PAGE_ALIGN(bytes);
  499. if (flags & IOVMF_MMIO) {
  500. va = vmap_sg(sgt);
  501. if (IS_ERR(va))
  502. return PTR_ERR(va);
  503. }
  504. flags &= IOVMF_HW_MASK;
  505. flags |= IOVMF_DISCONT;
  506. flags |= IOVMF_MMIO;
  507. flags |= (da ? IOVMF_DA_FIXED : IOVMF_DA_ANON);
  508. da = __iommu_vmap(obj, da, sgt, va, bytes, flags);
  509. if (IS_ERR_VALUE(da))
  510. vunmap_sg(va);
  511. return da;
  512. }
  513. EXPORT_SYMBOL_GPL(iommu_vmap);
  514. /**
  515. * iommu_vunmap - release virtual mapping obtained by 'iommu_vmap()'
  516. * @obj: objective iommu
  517. * @da: iommu device virtual address
  518. *
  519. * Free the iommu virtually contiguous memory area starting at
  520. * @da, which was returned by 'iommu_vmap()'.
  521. */
  522. struct sg_table *iommu_vunmap(struct iommu *obj, u32 da)
  523. {
  524. struct sg_table *sgt;
  525. /*
  526. * 'sgt' is allocated before 'iommu_vmalloc()' is called.
  527. * Just returns 'sgt' to the caller to free
  528. */
  529. sgt = unmap_vm_area(obj, da, vunmap_sg, IOVMF_DISCONT | IOVMF_MMIO);
  530. if (!sgt)
  531. dev_dbg(obj->dev, "%s: No sgt\n", __func__);
  532. return sgt;
  533. }
  534. EXPORT_SYMBOL_GPL(iommu_vunmap);
  535. /**
  536. * iommu_vmalloc - (d)-(p)-(v) address allocator and mapper
  537. * @obj: objective iommu
  538. * @da: contiguous iommu virtual memory
  539. * @bytes: allocation size
  540. * @flags: iovma and page property
  541. *
  542. * Allocate @bytes linearly and creates 1-n-1 mapping and returns
  543. * @da again, which might be adjusted if 'IOVMF_DA_ANON' is set.
  544. */
  545. u32 iommu_vmalloc(struct iommu *obj, u32 da, size_t bytes, u32 flags)
  546. {
  547. void *va;
  548. struct sg_table *sgt;
  549. if (!obj || !obj->dev || !bytes)
  550. return -EINVAL;
  551. bytes = PAGE_ALIGN(bytes);
  552. va = vmalloc(bytes);
  553. if (!va)
  554. return -ENOMEM;
  555. sgt = sgtable_alloc(bytes, flags);
  556. if (IS_ERR(sgt)) {
  557. da = PTR_ERR(sgt);
  558. goto err_sgt_alloc;
  559. }
  560. sgtable_fill_vmalloc(sgt, va);
  561. flags &= IOVMF_HW_MASK;
  562. flags |= IOVMF_DISCONT;
  563. flags |= IOVMF_ALLOC;
  564. flags |= (da ? IOVMF_DA_FIXED : IOVMF_DA_ANON);
  565. da = __iommu_vmap(obj, da, sgt, va, bytes, flags);
  566. if (IS_ERR_VALUE(da))
  567. goto err_iommu_vmap;
  568. return da;
  569. err_iommu_vmap:
  570. sgtable_drain_vmalloc(sgt);
  571. sgtable_free(sgt);
  572. err_sgt_alloc:
  573. vfree(va);
  574. return da;
  575. }
  576. EXPORT_SYMBOL_GPL(iommu_vmalloc);
  577. /**
  578. * iommu_vfree - release memory allocated by 'iommu_vmalloc()'
  579. * @obj: objective iommu
  580. * @da: iommu device virtual address
  581. *
  582. * Frees the iommu virtually continuous memory area starting at
  583. * @da, as obtained from 'iommu_vmalloc()'.
  584. */
  585. void iommu_vfree(struct iommu *obj, const u32 da)
  586. {
  587. struct sg_table *sgt;
  588. sgt = unmap_vm_area(obj, da, vfree, IOVMF_DISCONT | IOVMF_ALLOC);
  589. if (!sgt)
  590. dev_dbg(obj->dev, "%s: No sgt\n", __func__);
  591. sgtable_free(sgt);
  592. }
  593. EXPORT_SYMBOL_GPL(iommu_vfree);
  594. static u32 __iommu_kmap(struct iommu *obj, u32 da, u32 pa, void *va,
  595. size_t bytes, u32 flags)
  596. {
  597. struct sg_table *sgt;
  598. sgt = sgtable_alloc(bytes, flags);
  599. if (IS_ERR(sgt))
  600. return PTR_ERR(sgt);
  601. sgtable_fill_kmalloc(sgt, pa, bytes);
  602. da = map_iommu_region(obj, da, sgt, va, bytes, flags);
  603. if (IS_ERR_VALUE(da)) {
  604. sgtable_drain_kmalloc(sgt);
  605. sgtable_free(sgt);
  606. }
  607. return da;
  608. }
  609. /**
  610. * iommu_kmap - (d)-(p)-(v) address mapper
  611. * @obj: objective iommu
  612. * @da: contiguous iommu virtual memory
  613. * @pa: contiguous physical memory
  614. * @flags: iovma and page property
  615. *
  616. * Creates 1-1-1 mapping and returns @da again, which can be
  617. * adjusted if 'IOVMF_DA_ANON' is set.
  618. */
  619. u32 iommu_kmap(struct iommu *obj, u32 da, u32 pa, size_t bytes,
  620. u32 flags)
  621. {
  622. void *va;
  623. if (!obj || !obj->dev || !bytes)
  624. return -EINVAL;
  625. bytes = PAGE_ALIGN(bytes);
  626. va = ioremap(pa, bytes);
  627. if (!va)
  628. return -ENOMEM;
  629. flags &= IOVMF_HW_MASK;
  630. flags |= IOVMF_LINEAR;
  631. flags |= IOVMF_MMIO;
  632. flags |= (da ? IOVMF_DA_FIXED : IOVMF_DA_ANON);
  633. da = __iommu_kmap(obj, da, pa, va, bytes, flags);
  634. if (IS_ERR_VALUE(da))
  635. iounmap(va);
  636. return da;
  637. }
  638. EXPORT_SYMBOL_GPL(iommu_kmap);
  639. /**
  640. * iommu_kunmap - release virtual mapping obtained by 'iommu_kmap()'
  641. * @obj: objective iommu
  642. * @da: iommu device virtual address
  643. *
  644. * Frees the iommu virtually contiguous memory area starting at
  645. * @da, which was passed to and was returned by'iommu_kmap()'.
  646. */
  647. void iommu_kunmap(struct iommu *obj, u32 da)
  648. {
  649. struct sg_table *sgt;
  650. typedef void (*func_t)(const void *);
  651. sgt = unmap_vm_area(obj, da, (func_t)__iounmap,
  652. IOVMF_LINEAR | IOVMF_MMIO);
  653. if (!sgt)
  654. dev_dbg(obj->dev, "%s: No sgt\n", __func__);
  655. sgtable_free(sgt);
  656. }
  657. EXPORT_SYMBOL_GPL(iommu_kunmap);
  658. /**
  659. * iommu_kmalloc - (d)-(p)-(v) address allocator and mapper
  660. * @obj: objective iommu
  661. * @da: contiguous iommu virtual memory
  662. * @bytes: bytes for allocation
  663. * @flags: iovma and page property
  664. *
  665. * Allocate @bytes linearly and creates 1-1-1 mapping and returns
  666. * @da again, which might be adjusted if 'IOVMF_DA_ANON' is set.
  667. */
  668. u32 iommu_kmalloc(struct iommu *obj, u32 da, size_t bytes, u32 flags)
  669. {
  670. void *va;
  671. u32 pa;
  672. if (!obj || !obj->dev || !bytes)
  673. return -EINVAL;
  674. bytes = PAGE_ALIGN(bytes);
  675. va = kmalloc(bytes, GFP_KERNEL | GFP_DMA);
  676. if (!va)
  677. return -ENOMEM;
  678. pa = virt_to_phys(va);
  679. flags &= IOVMF_HW_MASK;
  680. flags |= IOVMF_LINEAR;
  681. flags |= IOVMF_ALLOC;
  682. flags |= (da ? IOVMF_DA_FIXED : IOVMF_DA_ANON);
  683. da = __iommu_kmap(obj, da, pa, va, bytes, flags);
  684. if (IS_ERR_VALUE(da))
  685. kfree(va);
  686. return da;
  687. }
  688. EXPORT_SYMBOL_GPL(iommu_kmalloc);
  689. /**
  690. * iommu_kfree - release virtual mapping obtained by 'iommu_kmalloc()'
  691. * @obj: objective iommu
  692. * @da: iommu device virtual address
  693. *
  694. * Frees the iommu virtually contiguous memory area starting at
  695. * @da, which was passed to and was returned by'iommu_kmalloc()'.
  696. */
  697. void iommu_kfree(struct iommu *obj, u32 da)
  698. {
  699. struct sg_table *sgt;
  700. sgt = unmap_vm_area(obj, da, kfree, IOVMF_LINEAR | IOVMF_ALLOC);
  701. if (!sgt)
  702. dev_dbg(obj->dev, "%s: No sgt\n", __func__);
  703. sgtable_free(sgt);
  704. }
  705. EXPORT_SYMBOL_GPL(iommu_kfree);
  706. static int __init iovmm_init(void)
  707. {
  708. const unsigned long flags = SLAB_HWCACHE_ALIGN;
  709. struct kmem_cache *p;
  710. p = kmem_cache_create("iovm_area_cache", sizeof(struct iovm_struct), 0,
  711. flags, NULL);
  712. if (!p)
  713. return -ENOMEM;
  714. iovm_area_cachep = p;
  715. return 0;
  716. }
  717. module_init(iovmm_init);
  718. static void __exit iovmm_exit(void)
  719. {
  720. kmem_cache_destroy(iovm_area_cachep);
  721. }
  722. module_exit(iovmm_exit);
  723. MODULE_DESCRIPTION("omap iommu: simple virtual address space management");
  724. MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>");
  725. MODULE_LICENSE("GPL v2");