iovmm.c 19 KB

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