iovmm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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) && (start + bytes < tmp->da_start))
  235. goto found;
  236. if (flags & IOVMF_DA_ANON)
  237. start = roundup(tmp->da_end, alignement);
  238. prev_end = tmp->da_end;
  239. }
  240. if ((start >= prev_end) && (ULONG_MAX - start >= bytes))
  241. goto found;
  242. dev_dbg(obj->dev, "%s: no space to fit %08x(%x) flags: %08x\n",
  243. __func__, da, bytes, flags);
  244. return ERR_PTR(-EINVAL);
  245. found:
  246. new = kmem_cache_zalloc(iovm_area_cachep, GFP_KERNEL);
  247. if (!new)
  248. return ERR_PTR(-ENOMEM);
  249. new->iommu = obj;
  250. new->da_start = start;
  251. new->da_end = start + bytes;
  252. new->flags = flags;
  253. /*
  254. * keep ascending order of iovmas
  255. */
  256. if (tmp)
  257. list_add_tail(&new->list, &tmp->list);
  258. else
  259. list_add(&new->list, &obj->mmap);
  260. dev_dbg(obj->dev, "%s: found %08x-%08x-%08x(%x) %08x\n",
  261. __func__, new->da_start, start, new->da_end, bytes, flags);
  262. return new;
  263. }
  264. static void free_iovm_area(struct iommu *obj, struct iovm_struct *area)
  265. {
  266. size_t bytes;
  267. BUG_ON(!obj || !area);
  268. bytes = area->da_end - area->da_start;
  269. dev_dbg(obj->dev, "%s: %08x-%08x(%x) %08x\n",
  270. __func__, area->da_start, area->da_end, bytes, area->flags);
  271. list_del(&area->list);
  272. kmem_cache_free(iovm_area_cachep, area);
  273. }
  274. /**
  275. * da_to_va - convert (d) to (v)
  276. * @obj: objective iommu
  277. * @da: iommu device virtual address
  278. * @va: mpu virtual address
  279. *
  280. * Returns mpu virtual addr which corresponds to a given device virtual addr
  281. */
  282. void *da_to_va(struct iommu *obj, u32 da)
  283. {
  284. void *va = NULL;
  285. struct iovm_struct *area;
  286. mutex_lock(&obj->mmap_lock);
  287. area = __find_iovm_area(obj, da);
  288. if (!area) {
  289. dev_dbg(obj->dev, "%s: no da area(%08x)\n", __func__, da);
  290. goto out;
  291. }
  292. va = area->va;
  293. out:
  294. mutex_unlock(&obj->mmap_lock);
  295. return va;
  296. }
  297. EXPORT_SYMBOL_GPL(da_to_va);
  298. static void sgtable_fill_vmalloc(struct sg_table *sgt, void *_va)
  299. {
  300. unsigned int i;
  301. struct scatterlist *sg;
  302. void *va = _va;
  303. void *va_end;
  304. for_each_sg(sgt->sgl, sg, sgt->nents, i) {
  305. struct page *pg;
  306. const size_t bytes = PAGE_SIZE;
  307. /*
  308. * iommu 'superpage' isn't supported with 'iommu_vmalloc()'
  309. */
  310. pg = vmalloc_to_page(va);
  311. BUG_ON(!pg);
  312. sg_set_page(sg, pg, bytes, 0);
  313. va += bytes;
  314. }
  315. va_end = _va + PAGE_SIZE * i;
  316. }
  317. static inline void sgtable_drain_vmalloc(struct sg_table *sgt)
  318. {
  319. /*
  320. * Actually this is not necessary at all, just exists for
  321. * consistency of the code readability.
  322. */
  323. BUG_ON(!sgt);
  324. }
  325. static void sgtable_fill_kmalloc(struct sg_table *sgt, u32 pa, size_t len)
  326. {
  327. unsigned int i;
  328. struct scatterlist *sg;
  329. void *va;
  330. va = phys_to_virt(pa);
  331. for_each_sg(sgt->sgl, sg, sgt->nents, i) {
  332. size_t bytes;
  333. bytes = iopgsz_max(len);
  334. BUG_ON(!iopgsz_ok(bytes));
  335. sg_set_buf(sg, phys_to_virt(pa), bytes);
  336. /*
  337. * 'pa' is cotinuous(linear).
  338. */
  339. pa += bytes;
  340. len -= bytes;
  341. }
  342. BUG_ON(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 readability
  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 || !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 = NULL;
  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. if (flags & IOVMF_MMIO) {
  498. va = vmap_sg(sgt);
  499. if (IS_ERR(va))
  500. return PTR_ERR(va);
  501. }
  502. flags &= IOVMF_HW_MASK;
  503. flags |= IOVMF_DISCONT;
  504. flags |= IOVMF_MMIO;
  505. flags |= (da ? IOVMF_DA_FIXED : IOVMF_DA_ANON);
  506. da = __iommu_vmap(obj, da, sgt, va, bytes, flags);
  507. if (IS_ERR_VALUE(da))
  508. vunmap_sg(va);
  509. return da;
  510. }
  511. EXPORT_SYMBOL_GPL(iommu_vmap);
  512. /**
  513. * iommu_vunmap - release virtual mapping obtained by 'iommu_vmap()'
  514. * @obj: objective iommu
  515. * @da: iommu device virtual address
  516. *
  517. * Free the iommu virtually contiguous memory area starting at
  518. * @da, which was returned by 'iommu_vmap()'.
  519. */
  520. struct sg_table *iommu_vunmap(struct iommu *obj, u32 da)
  521. {
  522. struct sg_table *sgt;
  523. /*
  524. * 'sgt' is allocated before 'iommu_vmalloc()' is called.
  525. * Just returns 'sgt' to the caller to free
  526. */
  527. sgt = unmap_vm_area(obj, da, vunmap_sg, IOVMF_DISCONT | IOVMF_MMIO);
  528. if (!sgt)
  529. dev_dbg(obj->dev, "%s: No sgt\n", __func__);
  530. return sgt;
  531. }
  532. EXPORT_SYMBOL_GPL(iommu_vunmap);
  533. /**
  534. * iommu_vmalloc - (d)-(p)-(v) address allocator and mapper
  535. * @obj: objective iommu
  536. * @da: contiguous iommu virtual memory
  537. * @bytes: allocation size
  538. * @flags: iovma and page property
  539. *
  540. * Allocate @bytes linearly and creates 1-n-1 mapping and returns
  541. * @da again, which might be adjusted if 'IOVMF_DA_ANON' is set.
  542. */
  543. u32 iommu_vmalloc(struct iommu *obj, u32 da, size_t bytes, u32 flags)
  544. {
  545. void *va;
  546. struct sg_table *sgt;
  547. if (!obj || !obj->dev || !bytes)
  548. return -EINVAL;
  549. bytes = PAGE_ALIGN(bytes);
  550. va = vmalloc(bytes);
  551. if (!va)
  552. return -ENOMEM;
  553. sgt = sgtable_alloc(bytes, flags);
  554. if (IS_ERR(sgt)) {
  555. da = PTR_ERR(sgt);
  556. goto err_sgt_alloc;
  557. }
  558. sgtable_fill_vmalloc(sgt, va);
  559. flags &= IOVMF_HW_MASK;
  560. flags |= IOVMF_DISCONT;
  561. flags |= IOVMF_ALLOC;
  562. flags |= (da ? IOVMF_DA_FIXED : IOVMF_DA_ANON);
  563. da = __iommu_vmap(obj, da, sgt, va, bytes, flags);
  564. if (IS_ERR_VALUE(da))
  565. goto err_iommu_vmap;
  566. return da;
  567. err_iommu_vmap:
  568. sgtable_drain_vmalloc(sgt);
  569. sgtable_free(sgt);
  570. err_sgt_alloc:
  571. vfree(va);
  572. return da;
  573. }
  574. EXPORT_SYMBOL_GPL(iommu_vmalloc);
  575. /**
  576. * iommu_vfree - release memory allocated by 'iommu_vmalloc()'
  577. * @obj: objective iommu
  578. * @da: iommu device virtual address
  579. *
  580. * Frees the iommu virtually continuous memory area starting at
  581. * @da, as obtained from 'iommu_vmalloc()'.
  582. */
  583. void iommu_vfree(struct iommu *obj, const u32 da)
  584. {
  585. struct sg_table *sgt;
  586. sgt = unmap_vm_area(obj, da, vfree, IOVMF_DISCONT | IOVMF_ALLOC);
  587. if (!sgt)
  588. dev_dbg(obj->dev, "%s: No sgt\n", __func__);
  589. sgtable_free(sgt);
  590. }
  591. EXPORT_SYMBOL_GPL(iommu_vfree);
  592. static u32 __iommu_kmap(struct iommu *obj, u32 da, u32 pa, void *va,
  593. size_t bytes, u32 flags)
  594. {
  595. struct sg_table *sgt;
  596. sgt = sgtable_alloc(bytes, flags);
  597. if (IS_ERR(sgt))
  598. return PTR_ERR(sgt);
  599. sgtable_fill_kmalloc(sgt, pa, bytes);
  600. da = map_iommu_region(obj, da, sgt, va, bytes, flags);
  601. if (IS_ERR_VALUE(da)) {
  602. sgtable_drain_kmalloc(sgt);
  603. sgtable_free(sgt);
  604. }
  605. return da;
  606. }
  607. /**
  608. * iommu_kmap - (d)-(p)-(v) address mapper
  609. * @obj: objective iommu
  610. * @da: contiguous iommu virtual memory
  611. * @pa: contiguous physical memory
  612. * @flags: iovma and page property
  613. *
  614. * Creates 1-1-1 mapping and returns @da again, which can be
  615. * adjusted if 'IOVMF_DA_ANON' is set.
  616. */
  617. u32 iommu_kmap(struct iommu *obj, u32 da, u32 pa, size_t bytes,
  618. u32 flags)
  619. {
  620. void *va;
  621. if (!obj || !obj->dev || !bytes)
  622. return -EINVAL;
  623. bytes = PAGE_ALIGN(bytes);
  624. va = ioremap(pa, bytes);
  625. if (!va)
  626. return -ENOMEM;
  627. flags &= IOVMF_HW_MASK;
  628. flags |= IOVMF_LINEAR;
  629. flags |= IOVMF_MMIO;
  630. flags |= (da ? IOVMF_DA_FIXED : IOVMF_DA_ANON);
  631. da = __iommu_kmap(obj, da, pa, va, bytes, flags);
  632. if (IS_ERR_VALUE(da))
  633. iounmap(va);
  634. return da;
  635. }
  636. EXPORT_SYMBOL_GPL(iommu_kmap);
  637. /**
  638. * iommu_kunmap - release virtual mapping obtained by 'iommu_kmap()'
  639. * @obj: objective iommu
  640. * @da: iommu device virtual address
  641. *
  642. * Frees the iommu virtually contiguous memory area starting at
  643. * @da, which was passed to and was returned by'iommu_kmap()'.
  644. */
  645. void iommu_kunmap(struct iommu *obj, u32 da)
  646. {
  647. struct sg_table *sgt;
  648. typedef void (*func_t)(const void *);
  649. sgt = unmap_vm_area(obj, da, (func_t)__iounmap,
  650. IOVMF_LINEAR | IOVMF_MMIO);
  651. if (!sgt)
  652. dev_dbg(obj->dev, "%s: No sgt\n", __func__);
  653. sgtable_free(sgt);
  654. }
  655. EXPORT_SYMBOL_GPL(iommu_kunmap);
  656. /**
  657. * iommu_kmalloc - (d)-(p)-(v) address allocator and mapper
  658. * @obj: objective iommu
  659. * @da: contiguous iommu virtual memory
  660. * @bytes: bytes for allocation
  661. * @flags: iovma and page property
  662. *
  663. * Allocate @bytes linearly and creates 1-1-1 mapping and returns
  664. * @da again, which might be adjusted if 'IOVMF_DA_ANON' is set.
  665. */
  666. u32 iommu_kmalloc(struct iommu *obj, u32 da, size_t bytes, u32 flags)
  667. {
  668. void *va;
  669. u32 pa;
  670. if (!obj || !obj->dev || !bytes)
  671. return -EINVAL;
  672. bytes = PAGE_ALIGN(bytes);
  673. va = kmalloc(bytes, GFP_KERNEL | GFP_DMA);
  674. if (!va)
  675. return -ENOMEM;
  676. pa = virt_to_phys(va);
  677. flags &= IOVMF_HW_MASK;
  678. flags |= IOVMF_LINEAR;
  679. flags |= IOVMF_ALLOC;
  680. flags |= (da ? IOVMF_DA_FIXED : IOVMF_DA_ANON);
  681. da = __iommu_kmap(obj, da, pa, va, bytes, flags);
  682. if (IS_ERR_VALUE(da))
  683. kfree(va);
  684. return da;
  685. }
  686. EXPORT_SYMBOL_GPL(iommu_kmalloc);
  687. /**
  688. * iommu_kfree - release virtual mapping obtained by 'iommu_kmalloc()'
  689. * @obj: objective iommu
  690. * @da: iommu device virtual address
  691. *
  692. * Frees the iommu virtually contiguous memory area starting at
  693. * @da, which was passed to and was returned by'iommu_kmalloc()'.
  694. */
  695. void iommu_kfree(struct iommu *obj, u32 da)
  696. {
  697. struct sg_table *sgt;
  698. sgt = unmap_vm_area(obj, da, kfree, IOVMF_LINEAR | IOVMF_ALLOC);
  699. if (!sgt)
  700. dev_dbg(obj->dev, "%s: No sgt\n", __func__);
  701. sgtable_free(sgt);
  702. }
  703. EXPORT_SYMBOL_GPL(iommu_kfree);
  704. static int __init iovmm_init(void)
  705. {
  706. const unsigned long flags = SLAB_HWCACHE_ALIGN;
  707. struct kmem_cache *p;
  708. p = kmem_cache_create("iovm_area_cache", sizeof(struct iovm_struct), 0,
  709. flags, NULL);
  710. if (!p)
  711. return -ENOMEM;
  712. iovm_area_cachep = p;
  713. return 0;
  714. }
  715. module_init(iovmm_init);
  716. static void __exit iovmm_exit(void)
  717. {
  718. kmem_cache_destroy(iovm_area_cachep);
  719. }
  720. module_exit(iovmm_exit);
  721. MODULE_DESCRIPTION("omap iommu: simple virtual address space management");
  722. MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>");
  723. MODULE_LICENSE("GPL v2");