iommu.c 16 KB

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