iommu.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation
  3. *
  4. * Rewrite, cleanup, new allocation schemes, virtual merging:
  5. * Copyright (C) 2004 Olof Johansson, IBM Corporation
  6. * and Ben. Herrenschmidt, IBM Corporation
  7. *
  8. * Dynamic DMA mapping support, bus-independent parts.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/config.h>
  25. #include <linux/init.h>
  26. #include <linux/types.h>
  27. #include <linux/slab.h>
  28. #include <linux/mm.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/string.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/init.h>
  33. #include <linux/bitops.h>
  34. #include <asm/io.h>
  35. #include <asm/prom.h>
  36. #include <asm/iommu.h>
  37. #include <asm/pci-bridge.h>
  38. #include <asm/machdep.h>
  39. #define DBG(...)
  40. #ifdef CONFIG_IOMMU_VMERGE
  41. static int novmerge = 0;
  42. #else
  43. static int novmerge = 1;
  44. #endif
  45. static int __init setup_iommu(char *str)
  46. {
  47. if (!strcmp(str, "novmerge"))
  48. novmerge = 1;
  49. else if (!strcmp(str, "vmerge"))
  50. novmerge = 0;
  51. return 1;
  52. }
  53. __setup("iommu=", setup_iommu);
  54. static unsigned long iommu_range_alloc(struct iommu_table *tbl,
  55. unsigned long npages,
  56. unsigned long *handle,
  57. unsigned int align_order)
  58. {
  59. unsigned long n, end, i, start;
  60. unsigned long limit;
  61. int largealloc = npages > 15;
  62. int pass = 0;
  63. unsigned long align_mask;
  64. align_mask = 0xffffffffffffffffl >> (64 - align_order);
  65. /* This allocator was derived from x86_64's bit string search */
  66. /* Sanity check */
  67. if (unlikely(npages) == 0) {
  68. if (printk_ratelimit())
  69. WARN_ON(1);
  70. return DMA_ERROR_CODE;
  71. }
  72. if (handle && *handle)
  73. start = *handle;
  74. else
  75. start = largealloc ? tbl->it_largehint : tbl->it_hint;
  76. /* Use only half of the table for small allocs (15 pages or less) */
  77. limit = largealloc ? tbl->it_size : tbl->it_halfpoint;
  78. if (largealloc && start < tbl->it_halfpoint)
  79. start = tbl->it_halfpoint;
  80. /* The case below can happen if we have a small segment appended
  81. * to a large, or when the previous alloc was at the very end of
  82. * the available space. If so, go back to the initial start.
  83. */
  84. if (start >= limit)
  85. start = largealloc ? tbl->it_largehint : tbl->it_hint;
  86. again:
  87. n = find_next_zero_bit(tbl->it_map, limit, start);
  88. /* Align allocation */
  89. n = (n + align_mask) & ~align_mask;
  90. end = n + npages;
  91. if (unlikely(end >= limit)) {
  92. if (likely(pass < 2)) {
  93. /* First failure, just rescan the half of the table.
  94. * Second failure, rescan the other half of the table.
  95. */
  96. start = (largealloc ^ pass) ? tbl->it_halfpoint : 0;
  97. limit = pass ? tbl->it_size : limit;
  98. pass++;
  99. goto again;
  100. } else {
  101. /* Third failure, give up */
  102. return DMA_ERROR_CODE;
  103. }
  104. }
  105. for (i = n; i < end; i++)
  106. if (test_bit(i, tbl->it_map)) {
  107. start = i+1;
  108. goto again;
  109. }
  110. for (i = n; i < end; i++)
  111. __set_bit(i, tbl->it_map);
  112. /* Bump the hint to a new block for small allocs. */
  113. if (largealloc) {
  114. /* Don't bump to new block to avoid fragmentation */
  115. tbl->it_largehint = end;
  116. } else {
  117. /* Overflow will be taken care of at the next allocation */
  118. tbl->it_hint = (end + tbl->it_blocksize - 1) &
  119. ~(tbl->it_blocksize - 1);
  120. }
  121. /* Update handle for SG allocations */
  122. if (handle)
  123. *handle = end;
  124. return n;
  125. }
  126. static dma_addr_t iommu_alloc(struct iommu_table *tbl, void *page,
  127. unsigned int npages, enum dma_data_direction direction,
  128. unsigned int align_order)
  129. {
  130. unsigned long entry, flags;
  131. dma_addr_t ret = DMA_ERROR_CODE;
  132. spin_lock_irqsave(&(tbl->it_lock), flags);
  133. entry = iommu_range_alloc(tbl, npages, NULL, align_order);
  134. if (unlikely(entry == DMA_ERROR_CODE)) {
  135. spin_unlock_irqrestore(&(tbl->it_lock), flags);
  136. return DMA_ERROR_CODE;
  137. }
  138. entry += tbl->it_offset; /* Offset into real TCE table */
  139. ret = entry << PAGE_SHIFT; /* Set the return dma address */
  140. /* Put the TCEs in the HW table */
  141. ppc_md.tce_build(tbl, entry, npages, (unsigned long)page & PAGE_MASK,
  142. direction);
  143. /* Flush/invalidate TLB caches if necessary */
  144. if (ppc_md.tce_flush)
  145. ppc_md.tce_flush(tbl);
  146. spin_unlock_irqrestore(&(tbl->it_lock), flags);
  147. /* Make sure updates are seen by hardware */
  148. mb();
  149. return ret;
  150. }
  151. static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
  152. unsigned int npages)
  153. {
  154. unsigned long entry, free_entry;
  155. unsigned long i;
  156. entry = dma_addr >> PAGE_SHIFT;
  157. free_entry = entry - tbl->it_offset;
  158. if (((free_entry + npages) > tbl->it_size) ||
  159. (entry < tbl->it_offset)) {
  160. if (printk_ratelimit()) {
  161. printk(KERN_INFO "iommu_free: invalid entry\n");
  162. printk(KERN_INFO "\tentry = 0x%lx\n", entry);
  163. printk(KERN_INFO "\tdma_addr = 0x%lx\n", (u64)dma_addr);
  164. printk(KERN_INFO "\tTable = 0x%lx\n", (u64)tbl);
  165. printk(KERN_INFO "\tbus# = 0x%lx\n", (u64)tbl->it_busno);
  166. printk(KERN_INFO "\tsize = 0x%lx\n", (u64)tbl->it_size);
  167. printk(KERN_INFO "\tstartOff = 0x%lx\n", (u64)tbl->it_offset);
  168. printk(KERN_INFO "\tindex = 0x%lx\n", (u64)tbl->it_index);
  169. WARN_ON(1);
  170. }
  171. return;
  172. }
  173. ppc_md.tce_free(tbl, entry, npages);
  174. for (i = 0; i < npages; i++)
  175. __clear_bit(free_entry+i, tbl->it_map);
  176. }
  177. static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
  178. unsigned int npages)
  179. {
  180. unsigned long flags;
  181. spin_lock_irqsave(&(tbl->it_lock), flags);
  182. __iommu_free(tbl, dma_addr, npages);
  183. /* Make sure TLB cache is flushed if the HW needs it. We do
  184. * not do an mb() here on purpose, it is not needed on any of
  185. * the current platforms.
  186. */
  187. if (ppc_md.tce_flush)
  188. ppc_md.tce_flush(tbl);
  189. spin_unlock_irqrestore(&(tbl->it_lock), flags);
  190. }
  191. int iommu_map_sg(struct device *dev, struct iommu_table *tbl,
  192. struct scatterlist *sglist, int nelems,
  193. enum dma_data_direction direction)
  194. {
  195. dma_addr_t dma_next = 0, dma_addr;
  196. unsigned long flags;
  197. struct scatterlist *s, *outs, *segstart;
  198. int outcount, incount;
  199. unsigned long handle;
  200. BUG_ON(direction == DMA_NONE);
  201. if ((nelems == 0) || !tbl)
  202. return 0;
  203. outs = s = segstart = &sglist[0];
  204. outcount = 1;
  205. incount = nelems;
  206. handle = 0;
  207. /* Init first segment length for backout at failure */
  208. outs->dma_length = 0;
  209. DBG("mapping %d elements:\n", nelems);
  210. spin_lock_irqsave(&(tbl->it_lock), flags);
  211. for (s = outs; nelems; nelems--, s++) {
  212. unsigned long vaddr, npages, entry, slen;
  213. slen = s->length;
  214. /* Sanity check */
  215. if (slen == 0) {
  216. dma_next = 0;
  217. continue;
  218. }
  219. /* Allocate iommu entries for that segment */
  220. vaddr = (unsigned long)page_address(s->page) + s->offset;
  221. npages = PAGE_ALIGN(vaddr + slen) - (vaddr & PAGE_MASK);
  222. npages >>= PAGE_SHIFT;
  223. entry = iommu_range_alloc(tbl, npages, &handle, 0);
  224. DBG(" - vaddr: %lx, size: %lx\n", vaddr, slen);
  225. /* Handle failure */
  226. if (unlikely(entry == DMA_ERROR_CODE)) {
  227. if (printk_ratelimit())
  228. printk(KERN_INFO "iommu_alloc failed, tbl %p vaddr %lx"
  229. " npages %lx\n", tbl, vaddr, npages);
  230. goto failure;
  231. }
  232. /* Convert entry to a dma_addr_t */
  233. entry += tbl->it_offset;
  234. dma_addr = entry << PAGE_SHIFT;
  235. dma_addr |= s->offset;
  236. DBG(" - %lx pages, entry: %lx, dma_addr: %lx\n",
  237. npages, entry, dma_addr);
  238. /* Insert into HW table */
  239. ppc_md.tce_build(tbl, entry, npages, vaddr & PAGE_MASK, direction);
  240. /* If we are in an open segment, try merging */
  241. if (segstart != s) {
  242. DBG(" - trying merge...\n");
  243. /* We cannot merge if:
  244. * - allocated dma_addr isn't contiguous to previous allocation
  245. */
  246. if (novmerge || (dma_addr != dma_next)) {
  247. /* Can't merge: create a new segment */
  248. segstart = s;
  249. outcount++; outs++;
  250. DBG(" can't merge, new segment.\n");
  251. } else {
  252. outs->dma_length += s->length;
  253. DBG(" merged, new len: %lx\n", outs->dma_length);
  254. }
  255. }
  256. if (segstart == s) {
  257. /* This is a new segment, fill entries */
  258. DBG(" - filling new segment.\n");
  259. outs->dma_address = dma_addr;
  260. outs->dma_length = slen;
  261. }
  262. /* Calculate next page pointer for contiguous check */
  263. dma_next = dma_addr + slen;
  264. DBG(" - dma next is: %lx\n", dma_next);
  265. }
  266. /* Flush/invalidate TLB caches if necessary */
  267. if (ppc_md.tce_flush)
  268. ppc_md.tce_flush(tbl);
  269. spin_unlock_irqrestore(&(tbl->it_lock), flags);
  270. DBG("mapped %d elements:\n", outcount);
  271. /* For the sake of iommu_unmap_sg, we clear out the length in the
  272. * next entry of the sglist if we didn't fill the list completely
  273. */
  274. if (outcount < incount) {
  275. outs++;
  276. outs->dma_address = DMA_ERROR_CODE;
  277. outs->dma_length = 0;
  278. }
  279. /* Make sure updates are seen by hardware */
  280. mb();
  281. return outcount;
  282. failure:
  283. for (s = &sglist[0]; s <= outs; s++) {
  284. if (s->dma_length != 0) {
  285. unsigned long vaddr, npages;
  286. vaddr = s->dma_address & PAGE_MASK;
  287. npages = (PAGE_ALIGN(s->dma_address + s->dma_length) - vaddr)
  288. >> PAGE_SHIFT;
  289. __iommu_free(tbl, vaddr, npages);
  290. s->dma_address = DMA_ERROR_CODE;
  291. s->dma_length = 0;
  292. }
  293. }
  294. spin_unlock_irqrestore(&(tbl->it_lock), flags);
  295. return 0;
  296. }
  297. void iommu_unmap_sg(struct iommu_table *tbl, struct scatterlist *sglist,
  298. int nelems, enum dma_data_direction direction)
  299. {
  300. unsigned long flags;
  301. BUG_ON(direction == DMA_NONE);
  302. if (!tbl)
  303. return;
  304. spin_lock_irqsave(&(tbl->it_lock), flags);
  305. while (nelems--) {
  306. unsigned int npages;
  307. dma_addr_t dma_handle = sglist->dma_address;
  308. if (sglist->dma_length == 0)
  309. break;
  310. npages = (PAGE_ALIGN(dma_handle + sglist->dma_length)
  311. - (dma_handle & PAGE_MASK)) >> PAGE_SHIFT;
  312. __iommu_free(tbl, dma_handle, npages);
  313. sglist++;
  314. }
  315. /* Flush/invalidate TLBs if necessary. As for iommu_free(), we
  316. * do not do an mb() here, the affected platforms do not need it
  317. * when freeing.
  318. */
  319. if (ppc_md.tce_flush)
  320. ppc_md.tce_flush(tbl);
  321. spin_unlock_irqrestore(&(tbl->it_lock), flags);
  322. }
  323. /*
  324. * Build a iommu_table structure. This contains a bit map which
  325. * is used to manage allocation of the tce space.
  326. */
  327. struct iommu_table *iommu_init_table(struct iommu_table *tbl)
  328. {
  329. unsigned long sz;
  330. static int welcomed = 0;
  331. /* Set aside 1/4 of the table for large allocations. */
  332. tbl->it_halfpoint = tbl->it_size * 3 / 4;
  333. /* number of bytes needed for the bitmap */
  334. sz = (tbl->it_size + 7) >> 3;
  335. tbl->it_map = (unsigned long *)__get_free_pages(GFP_ATOMIC, get_order(sz));
  336. if (!tbl->it_map)
  337. panic("iommu_init_table: Can't allocate %ld bytes\n", sz);
  338. memset(tbl->it_map, 0, sz);
  339. tbl->it_hint = 0;
  340. tbl->it_largehint = tbl->it_halfpoint;
  341. spin_lock_init(&tbl->it_lock);
  342. /* Clear the hardware table in case firmware left allocations in it */
  343. ppc_md.tce_free(tbl, tbl->it_offset, tbl->it_size);
  344. if (!welcomed) {
  345. printk(KERN_INFO "IOMMU table initialized, virtual merging %s\n",
  346. novmerge ? "disabled" : "enabled");
  347. welcomed = 1;
  348. }
  349. return tbl;
  350. }
  351. void iommu_free_table(struct device_node *dn)
  352. {
  353. struct pci_dn *pdn = dn->data;
  354. struct iommu_table *tbl = pdn->iommu_table;
  355. unsigned long bitmap_sz, i;
  356. unsigned int order;
  357. if (!tbl || !tbl->it_map) {
  358. printk(KERN_ERR "%s: expected TCE map for %s\n", __FUNCTION__,
  359. dn->full_name);
  360. return;
  361. }
  362. /* verify that table contains no entries */
  363. /* it_size is in entries, and we're examining 64 at a time */
  364. for (i = 0; i < (tbl->it_size/64); i++) {
  365. if (tbl->it_map[i] != 0) {
  366. printk(KERN_WARNING "%s: Unexpected TCEs for %s\n",
  367. __FUNCTION__, dn->full_name);
  368. break;
  369. }
  370. }
  371. /* calculate bitmap size in bytes */
  372. bitmap_sz = (tbl->it_size + 7) / 8;
  373. /* free bitmap */
  374. order = get_order(bitmap_sz);
  375. free_pages((unsigned long) tbl->it_map, order);
  376. /* free table */
  377. kfree(tbl);
  378. }
  379. /* Creates TCEs for a user provided buffer. The user buffer must be
  380. * contiguous real kernel storage (not vmalloc). The address of the buffer
  381. * passed here is the kernel (virtual) address of the buffer. The buffer
  382. * need not be page aligned, the dma_addr_t returned will point to the same
  383. * byte within the page as vaddr.
  384. */
  385. dma_addr_t iommu_map_single(struct iommu_table *tbl, void *vaddr,
  386. size_t size, enum dma_data_direction direction)
  387. {
  388. dma_addr_t dma_handle = DMA_ERROR_CODE;
  389. unsigned long uaddr;
  390. unsigned int npages;
  391. BUG_ON(direction == DMA_NONE);
  392. uaddr = (unsigned long)vaddr;
  393. npages = PAGE_ALIGN(uaddr + size) - (uaddr & PAGE_MASK);
  394. npages >>= PAGE_SHIFT;
  395. if (tbl) {
  396. dma_handle = iommu_alloc(tbl, vaddr, npages, direction, 0);
  397. if (dma_handle == DMA_ERROR_CODE) {
  398. if (printk_ratelimit()) {
  399. printk(KERN_INFO "iommu_alloc failed, "
  400. "tbl %p vaddr %p npages %d\n",
  401. tbl, vaddr, npages);
  402. }
  403. } else
  404. dma_handle |= (uaddr & ~PAGE_MASK);
  405. }
  406. return dma_handle;
  407. }
  408. void iommu_unmap_single(struct iommu_table *tbl, dma_addr_t dma_handle,
  409. size_t size, enum dma_data_direction direction)
  410. {
  411. BUG_ON(direction == DMA_NONE);
  412. if (tbl)
  413. iommu_free(tbl, dma_handle, (PAGE_ALIGN(dma_handle + size) -
  414. (dma_handle & PAGE_MASK)) >> PAGE_SHIFT);
  415. }
  416. /* Allocates a contiguous real buffer and creates mappings over it.
  417. * Returns the virtual address of the buffer and sets dma_handle
  418. * to the dma address (mapping) of the first page.
  419. */
  420. void *iommu_alloc_coherent(struct iommu_table *tbl, size_t size,
  421. dma_addr_t *dma_handle, gfp_t flag)
  422. {
  423. void *ret = NULL;
  424. dma_addr_t mapping;
  425. unsigned int npages, order;
  426. size = PAGE_ALIGN(size);
  427. npages = size >> PAGE_SHIFT;
  428. order = get_order(size);
  429. /*
  430. * Client asked for way too much space. This is checked later
  431. * anyway. It is easier to debug here for the drivers than in
  432. * the tce tables.
  433. */
  434. if (order >= IOMAP_MAX_ORDER) {
  435. printk("iommu_alloc_consistent size too large: 0x%lx\n", size);
  436. return NULL;
  437. }
  438. if (!tbl)
  439. return NULL;
  440. /* Alloc enough pages (and possibly more) */
  441. ret = (void *)__get_free_pages(flag, order);
  442. if (!ret)
  443. return NULL;
  444. memset(ret, 0, size);
  445. /* Set up tces to cover the allocated range */
  446. mapping = iommu_alloc(tbl, ret, npages, DMA_BIDIRECTIONAL, order);
  447. if (mapping == DMA_ERROR_CODE) {
  448. free_pages((unsigned long)ret, order);
  449. ret = NULL;
  450. } else
  451. *dma_handle = mapping;
  452. return ret;
  453. }
  454. void iommu_free_coherent(struct iommu_table *tbl, size_t size,
  455. void *vaddr, dma_addr_t dma_handle)
  456. {
  457. unsigned int npages;
  458. if (tbl) {
  459. size = PAGE_ALIGN(size);
  460. npages = size >> PAGE_SHIFT;
  461. iommu_free(tbl, dma_handle, npages);
  462. free_pages((unsigned long)vaddr, get_order(size));
  463. }
  464. }