iovmm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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->length;
  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->length;
  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. for_each_sg(sgt->sgl, sg, sgt->nents, i) {
  342. unsigned bytes;
  343. bytes = max_alignment(da | pa);
  344. bytes = min_t(unsigned, bytes, iopgsz_max(len));
  345. BUG_ON(!iopgsz_ok(bytes));
  346. sg_set_buf(sg, phys_to_virt(pa), bytes);
  347. /*
  348. * 'pa' is cotinuous(linear).
  349. */
  350. pa += bytes;
  351. da += bytes;
  352. len -= bytes;
  353. }
  354. BUG_ON(len);
  355. }
  356. static inline void sgtable_drain_kmalloc(struct sg_table *sgt)
  357. {
  358. /*
  359. * Actually this is not necessary at all, just exists for
  360. * consistency of the code readability
  361. */
  362. BUG_ON(!sgt);
  363. }
  364. /* create 'da' <-> 'pa' mapping from 'sgt' */
  365. static int map_iovm_area(struct iommu *obj, struct iovm_struct *new,
  366. const struct sg_table *sgt, u32 flags)
  367. {
  368. int err;
  369. unsigned int i, j;
  370. struct scatterlist *sg;
  371. u32 da = new->da_start;
  372. if (!obj || !sgt)
  373. return -EINVAL;
  374. BUG_ON(!sgtable_ok(sgt));
  375. for_each_sg(sgt->sgl, sg, sgt->nents, i) {
  376. u32 pa;
  377. int pgsz;
  378. size_t bytes;
  379. struct iotlb_entry e;
  380. pa = sg_phys(sg);
  381. bytes = sg->length;
  382. flags &= ~IOVMF_PGSZ_MASK;
  383. pgsz = bytes_to_iopgsz(bytes);
  384. if (pgsz < 0)
  385. goto err_out;
  386. flags |= pgsz;
  387. pr_debug("%s: [%d] %08x %08x(%x)\n", __func__,
  388. i, da, pa, bytes);
  389. iotlb_init_entry(&e, da, pa, flags);
  390. err = iopgtable_store_entry(obj, &e);
  391. if (err)
  392. goto err_out;
  393. da += bytes;
  394. }
  395. return 0;
  396. err_out:
  397. da = new->da_start;
  398. for_each_sg(sgt->sgl, sg, i, j) {
  399. size_t bytes;
  400. bytes = iopgtable_clear_entry(obj, da);
  401. BUG_ON(!iopgsz_ok(bytes));
  402. da += bytes;
  403. }
  404. return err;
  405. }
  406. /* release 'da' <-> 'pa' mapping */
  407. static void unmap_iovm_area(struct iommu *obj, struct iovm_struct *area)
  408. {
  409. u32 start;
  410. size_t total = area->da_end - area->da_start;
  411. BUG_ON((!total) || !IS_ALIGNED(total, PAGE_SIZE));
  412. start = area->da_start;
  413. while (total > 0) {
  414. size_t bytes;
  415. bytes = iopgtable_clear_entry(obj, start);
  416. if (bytes == 0)
  417. bytes = PAGE_SIZE;
  418. else
  419. dev_dbg(obj->dev, "%s: unmap %08x(%x) %08x\n",
  420. __func__, start, bytes, area->flags);
  421. BUG_ON(!IS_ALIGNED(bytes, PAGE_SIZE));
  422. total -= bytes;
  423. start += bytes;
  424. }
  425. BUG_ON(total);
  426. }
  427. /* template function for all unmapping */
  428. static struct sg_table *unmap_vm_area(struct iommu *obj, const u32 da,
  429. void (*fn)(const void *), u32 flags)
  430. {
  431. struct sg_table *sgt = NULL;
  432. struct iovm_struct *area;
  433. if (!IS_ALIGNED(da, PAGE_SIZE)) {
  434. dev_err(obj->dev, "%s: alignment err(%08x)\n", __func__, da);
  435. return NULL;
  436. }
  437. mutex_lock(&obj->mmap_lock);
  438. area = __find_iovm_area(obj, da);
  439. if (!area) {
  440. dev_dbg(obj->dev, "%s: no da area(%08x)\n", __func__, da);
  441. goto out;
  442. }
  443. if ((area->flags & flags) != flags) {
  444. dev_err(obj->dev, "%s: wrong flags(%08x)\n", __func__,
  445. area->flags);
  446. goto out;
  447. }
  448. sgt = (struct sg_table *)area->sgt;
  449. unmap_iovm_area(obj, area);
  450. fn(area->va);
  451. dev_dbg(obj->dev, "%s: %08x-%08x-%08x(%x) %08x\n", __func__,
  452. area->da_start, da, area->da_end,
  453. area->da_end - area->da_start, area->flags);
  454. free_iovm_area(obj, area);
  455. out:
  456. mutex_unlock(&obj->mmap_lock);
  457. return sgt;
  458. }
  459. static u32 map_iommu_region(struct iommu *obj, u32 da,
  460. const struct sg_table *sgt, void *va, size_t bytes, u32 flags)
  461. {
  462. int err = -ENOMEM;
  463. struct iovm_struct *new;
  464. mutex_lock(&obj->mmap_lock);
  465. new = alloc_iovm_area(obj, da, bytes, flags);
  466. if (IS_ERR(new)) {
  467. err = PTR_ERR(new);
  468. goto err_alloc_iovma;
  469. }
  470. new->va = va;
  471. new->sgt = sgt;
  472. if (map_iovm_area(obj, new, sgt, new->flags))
  473. goto err_map;
  474. mutex_unlock(&obj->mmap_lock);
  475. dev_dbg(obj->dev, "%s: da:%08x(%x) flags:%08x va:%p\n",
  476. __func__, new->da_start, bytes, new->flags, va);
  477. return new->da_start;
  478. err_map:
  479. free_iovm_area(obj, new);
  480. err_alloc_iovma:
  481. mutex_unlock(&obj->mmap_lock);
  482. return err;
  483. }
  484. static inline u32 __iommu_vmap(struct iommu *obj, u32 da,
  485. const struct sg_table *sgt, void *va, size_t bytes, u32 flags)
  486. {
  487. return map_iommu_region(obj, da, sgt, va, bytes, flags);
  488. }
  489. /**
  490. * iommu_vmap - (d)-(p)-(v) address mapper
  491. * @obj: objective iommu
  492. * @sgt: address of scatter gather table
  493. * @flags: iovma and page property
  494. *
  495. * Creates 1-n-1 mapping with given @sgt and returns @da.
  496. * All @sgt element must be io page size aligned.
  497. */
  498. u32 iommu_vmap(struct iommu *obj, u32 da, const struct sg_table *sgt,
  499. u32 flags)
  500. {
  501. size_t bytes;
  502. void *va = NULL;
  503. if (!obj || !obj->dev || !sgt)
  504. return -EINVAL;
  505. bytes = sgtable_len(sgt);
  506. if (!bytes)
  507. return -EINVAL;
  508. bytes = PAGE_ALIGN(bytes);
  509. if (flags & IOVMF_MMIO) {
  510. va = vmap_sg(sgt);
  511. if (IS_ERR(va))
  512. return PTR_ERR(va);
  513. }
  514. flags |= IOVMF_DISCONT;
  515. flags |= IOVMF_MMIO;
  516. da = __iommu_vmap(obj, da, sgt, va, bytes, flags);
  517. if (IS_ERR_VALUE(da))
  518. vunmap_sg(va);
  519. return da;
  520. }
  521. EXPORT_SYMBOL_GPL(iommu_vmap);
  522. /**
  523. * iommu_vunmap - release virtual mapping obtained by 'iommu_vmap()'
  524. * @obj: objective iommu
  525. * @da: iommu device virtual address
  526. *
  527. * Free the iommu virtually contiguous memory area starting at
  528. * @da, which was returned by 'iommu_vmap()'.
  529. */
  530. struct sg_table *iommu_vunmap(struct iommu *obj, u32 da)
  531. {
  532. struct sg_table *sgt;
  533. /*
  534. * 'sgt' is allocated before 'iommu_vmalloc()' is called.
  535. * Just returns 'sgt' to the caller to free
  536. */
  537. sgt = unmap_vm_area(obj, da, vunmap_sg, IOVMF_DISCONT | IOVMF_MMIO);
  538. if (!sgt)
  539. dev_dbg(obj->dev, "%s: No sgt\n", __func__);
  540. return sgt;
  541. }
  542. EXPORT_SYMBOL_GPL(iommu_vunmap);
  543. /**
  544. * iommu_vmalloc - (d)-(p)-(v) address allocator and mapper
  545. * @obj: objective iommu
  546. * @da: contiguous iommu virtual memory
  547. * @bytes: allocation size
  548. * @flags: iovma and page property
  549. *
  550. * Allocate @bytes linearly and creates 1-n-1 mapping and returns
  551. * @da again, which might be adjusted if 'IOVMF_DA_FIXED' is not set.
  552. */
  553. u32 iommu_vmalloc(struct iommu *obj, u32 da, size_t bytes, u32 flags)
  554. {
  555. void *va;
  556. struct sg_table *sgt;
  557. if (!obj || !obj->dev || !bytes)
  558. return -EINVAL;
  559. bytes = PAGE_ALIGN(bytes);
  560. va = vmalloc(bytes);
  561. if (!va)
  562. return -ENOMEM;
  563. flags |= IOVMF_DISCONT;
  564. flags |= IOVMF_ALLOC;
  565. sgt = sgtable_alloc(bytes, flags, da, 0);
  566. if (IS_ERR(sgt)) {
  567. da = PTR_ERR(sgt);
  568. goto err_sgt_alloc;
  569. }
  570. sgtable_fill_vmalloc(sgt, va);
  571. da = __iommu_vmap(obj, da, sgt, va, bytes, flags);
  572. if (IS_ERR_VALUE(da))
  573. goto err_iommu_vmap;
  574. return da;
  575. err_iommu_vmap:
  576. sgtable_drain_vmalloc(sgt);
  577. sgtable_free(sgt);
  578. err_sgt_alloc:
  579. vfree(va);
  580. return da;
  581. }
  582. EXPORT_SYMBOL_GPL(iommu_vmalloc);
  583. /**
  584. * iommu_vfree - release memory allocated by 'iommu_vmalloc()'
  585. * @obj: objective iommu
  586. * @da: iommu device virtual address
  587. *
  588. * Frees the iommu virtually continuous memory area starting at
  589. * @da, as obtained from 'iommu_vmalloc()'.
  590. */
  591. void iommu_vfree(struct iommu *obj, const u32 da)
  592. {
  593. struct sg_table *sgt;
  594. sgt = unmap_vm_area(obj, da, vfree, IOVMF_DISCONT | IOVMF_ALLOC);
  595. if (!sgt)
  596. dev_dbg(obj->dev, "%s: No sgt\n", __func__);
  597. sgtable_free(sgt);
  598. }
  599. EXPORT_SYMBOL_GPL(iommu_vfree);
  600. static u32 __iommu_kmap(struct iommu *obj, u32 da, u32 pa, void *va,
  601. size_t bytes, u32 flags)
  602. {
  603. struct sg_table *sgt;
  604. sgt = sgtable_alloc(bytes, flags, da, pa);
  605. if (IS_ERR(sgt))
  606. return PTR_ERR(sgt);
  607. sgtable_fill_kmalloc(sgt, pa, da, bytes);
  608. da = map_iommu_region(obj, da, sgt, va, bytes, flags);
  609. if (IS_ERR_VALUE(da)) {
  610. sgtable_drain_kmalloc(sgt);
  611. sgtable_free(sgt);
  612. }
  613. return da;
  614. }
  615. /**
  616. * iommu_kmap - (d)-(p)-(v) address mapper
  617. * @obj: objective iommu
  618. * @da: contiguous iommu virtual memory
  619. * @pa: contiguous physical memory
  620. * @flags: iovma and page property
  621. *
  622. * Creates 1-1-1 mapping and returns @da again, which can be
  623. * adjusted if 'IOVMF_DA_FIXED' is not set.
  624. */
  625. u32 iommu_kmap(struct iommu *obj, u32 da, u32 pa, size_t bytes,
  626. u32 flags)
  627. {
  628. void *va;
  629. if (!obj || !obj->dev || !bytes)
  630. return -EINVAL;
  631. bytes = PAGE_ALIGN(bytes);
  632. va = ioremap(pa, bytes);
  633. if (!va)
  634. return -ENOMEM;
  635. flags |= IOVMF_LINEAR;
  636. flags |= IOVMF_MMIO;
  637. da = __iommu_kmap(obj, da, pa, va, bytes, flags);
  638. if (IS_ERR_VALUE(da))
  639. iounmap(va);
  640. return da;
  641. }
  642. EXPORT_SYMBOL_GPL(iommu_kmap);
  643. /**
  644. * iommu_kunmap - release virtual mapping obtained by 'iommu_kmap()'
  645. * @obj: objective iommu
  646. * @da: iommu device virtual address
  647. *
  648. * Frees the iommu virtually contiguous memory area starting at
  649. * @da, which was passed to and was returned by'iommu_kmap()'.
  650. */
  651. void iommu_kunmap(struct iommu *obj, u32 da)
  652. {
  653. struct sg_table *sgt;
  654. typedef void (*func_t)(const void *);
  655. sgt = unmap_vm_area(obj, da, (func_t)iounmap,
  656. IOVMF_LINEAR | IOVMF_MMIO);
  657. if (!sgt)
  658. dev_dbg(obj->dev, "%s: No sgt\n", __func__);
  659. sgtable_free(sgt);
  660. }
  661. EXPORT_SYMBOL_GPL(iommu_kunmap);
  662. /**
  663. * iommu_kmalloc - (d)-(p)-(v) address allocator and mapper
  664. * @obj: objective iommu
  665. * @da: contiguous iommu virtual memory
  666. * @bytes: bytes for allocation
  667. * @flags: iovma and page property
  668. *
  669. * Allocate @bytes linearly and creates 1-1-1 mapping and returns
  670. * @da again, which might be adjusted if 'IOVMF_DA_FIXED' is not set.
  671. */
  672. u32 iommu_kmalloc(struct iommu *obj, u32 da, size_t bytes, u32 flags)
  673. {
  674. void *va;
  675. u32 pa;
  676. if (!obj || !obj->dev || !bytes)
  677. return -EINVAL;
  678. bytes = PAGE_ALIGN(bytes);
  679. va = kmalloc(bytes, GFP_KERNEL | GFP_DMA);
  680. if (!va)
  681. return -ENOMEM;
  682. pa = virt_to_phys(va);
  683. flags |= IOVMF_LINEAR;
  684. flags |= IOVMF_ALLOC;
  685. da = __iommu_kmap(obj, da, pa, va, bytes, flags);
  686. if (IS_ERR_VALUE(da))
  687. kfree(va);
  688. return da;
  689. }
  690. EXPORT_SYMBOL_GPL(iommu_kmalloc);
  691. /**
  692. * iommu_kfree - release virtual mapping obtained by 'iommu_kmalloc()'
  693. * @obj: objective iommu
  694. * @da: iommu device virtual address
  695. *
  696. * Frees the iommu virtually contiguous memory area starting at
  697. * @da, which was passed to and was returned by'iommu_kmalloc()'.
  698. */
  699. void iommu_kfree(struct iommu *obj, u32 da)
  700. {
  701. struct sg_table *sgt;
  702. sgt = unmap_vm_area(obj, da, kfree, IOVMF_LINEAR | IOVMF_ALLOC);
  703. if (!sgt)
  704. dev_dbg(obj->dev, "%s: No sgt\n", __func__);
  705. sgtable_free(sgt);
  706. }
  707. EXPORT_SYMBOL_GPL(iommu_kfree);
  708. static int __init iovmm_init(void)
  709. {
  710. const unsigned long flags = SLAB_HWCACHE_ALIGN;
  711. struct kmem_cache *p;
  712. p = kmem_cache_create("iovm_area_cache", sizeof(struct iovm_struct), 0,
  713. flags, NULL);
  714. if (!p)
  715. return -ENOMEM;
  716. iovm_area_cachep = p;
  717. return 0;
  718. }
  719. module_init(iovmm_init);
  720. static void __exit iovmm_exit(void)
  721. {
  722. kmem_cache_destroy(iovm_area_cachep);
  723. }
  724. module_exit(iovmm_exit);
  725. MODULE_DESCRIPTION("omap iommu: simple virtual address space management");
  726. MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>");
  727. MODULE_LICENSE("GPL v2");