iovmm.c 19 KB

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