iommu.c 16 KB

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