iommu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /* iommu.c: Generic sparc64 IOMMU support.
  2. *
  3. * Copyright (C) 1999, 2007, 2008 David S. Miller (davem@davemloft.net)
  4. * Copyright (C) 1999, 2000 Jakub Jelinek (jakub@redhat.com)
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/slab.h>
  9. #include <linux/delay.h>
  10. #include <linux/device.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/errno.h>
  13. #include <linux/iommu-helper.h>
  14. #include <linux/bitmap.h>
  15. #ifdef CONFIG_PCI
  16. #include <linux/pci.h>
  17. #endif
  18. #include <asm/iommu.h>
  19. #include "iommu_common.h"
  20. #define STC_CTXMATCH_ADDR(STC, CTX) \
  21. ((STC)->strbuf_ctxmatch_base + ((CTX) << 3))
  22. #define STC_FLUSHFLAG_INIT(STC) \
  23. (*((STC)->strbuf_flushflag) = 0UL)
  24. #define STC_FLUSHFLAG_SET(STC) \
  25. (*((STC)->strbuf_flushflag) != 0UL)
  26. #define iommu_read(__reg) \
  27. ({ u64 __ret; \
  28. __asm__ __volatile__("ldxa [%1] %2, %0" \
  29. : "=r" (__ret) \
  30. : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
  31. : "memory"); \
  32. __ret; \
  33. })
  34. #define iommu_write(__reg, __val) \
  35. __asm__ __volatile__("stxa %0, [%1] %2" \
  36. : /* no outputs */ \
  37. : "r" (__val), "r" (__reg), \
  38. "i" (ASI_PHYS_BYPASS_EC_E))
  39. /* Must be invoked under the IOMMU lock. */
  40. static void iommu_flushall(struct iommu *iommu)
  41. {
  42. if (iommu->iommu_flushinv) {
  43. iommu_write(iommu->iommu_flushinv, ~(u64)0);
  44. } else {
  45. unsigned long tag;
  46. int entry;
  47. tag = iommu->iommu_tags;
  48. for (entry = 0; entry < 16; entry++) {
  49. iommu_write(tag, 0);
  50. tag += 8;
  51. }
  52. /* Ensure completion of previous PIO writes. */
  53. (void) iommu_read(iommu->write_complete_reg);
  54. }
  55. }
  56. #define IOPTE_CONSISTENT(CTX) \
  57. (IOPTE_VALID | IOPTE_CACHE | \
  58. (((CTX) << 47) & IOPTE_CONTEXT))
  59. #define IOPTE_STREAMING(CTX) \
  60. (IOPTE_CONSISTENT(CTX) | IOPTE_STBUF)
  61. /* Existing mappings are never marked invalid, instead they
  62. * are pointed to a dummy page.
  63. */
  64. #define IOPTE_IS_DUMMY(iommu, iopte) \
  65. ((iopte_val(*iopte) & IOPTE_PAGE) == (iommu)->dummy_page_pa)
  66. static inline void iopte_make_dummy(struct iommu *iommu, iopte_t *iopte)
  67. {
  68. unsigned long val = iopte_val(*iopte);
  69. val &= ~IOPTE_PAGE;
  70. val |= iommu->dummy_page_pa;
  71. iopte_val(*iopte) = val;
  72. }
  73. /* Based almost entirely upon the ppc64 iommu allocator. If you use the 'handle'
  74. * facility it must all be done in one pass while under the iommu lock.
  75. *
  76. * On sun4u platforms, we only flush the IOMMU once every time we've passed
  77. * over the entire page table doing allocations. Therefore we only ever advance
  78. * the hint and cannot backtrack it.
  79. */
  80. unsigned long iommu_range_alloc(struct device *dev,
  81. struct iommu *iommu,
  82. unsigned long npages,
  83. unsigned long *handle)
  84. {
  85. unsigned long n, end, start, limit, boundary_size;
  86. struct iommu_arena *arena = &iommu->arena;
  87. int pass = 0;
  88. /* This allocator was derived from x86_64's bit string search */
  89. /* Sanity check */
  90. if (unlikely(npages == 0)) {
  91. if (printk_ratelimit())
  92. WARN_ON(1);
  93. return DMA_ERROR_CODE;
  94. }
  95. if (handle && *handle)
  96. start = *handle;
  97. else
  98. start = arena->hint;
  99. limit = arena->limit;
  100. /* The case below can happen if we have a small segment appended
  101. * to a large, or when the previous alloc was at the very end of
  102. * the available space. If so, go back to the beginning and flush.
  103. */
  104. if (start >= limit) {
  105. start = 0;
  106. if (iommu->flush_all)
  107. iommu->flush_all(iommu);
  108. }
  109. again:
  110. if (dev)
  111. boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
  112. 1 << IO_PAGE_SHIFT);
  113. else
  114. boundary_size = ALIGN(1UL << 32, 1 << IO_PAGE_SHIFT);
  115. n = iommu_area_alloc(arena->map, limit, start, npages,
  116. iommu->page_table_map_base >> IO_PAGE_SHIFT,
  117. boundary_size >> IO_PAGE_SHIFT, 0);
  118. if (n == -1) {
  119. if (likely(pass < 1)) {
  120. /* First failure, rescan from the beginning. */
  121. start = 0;
  122. if (iommu->flush_all)
  123. iommu->flush_all(iommu);
  124. pass++;
  125. goto again;
  126. } else {
  127. /* Second failure, give up */
  128. return DMA_ERROR_CODE;
  129. }
  130. }
  131. end = n + npages;
  132. arena->hint = end;
  133. /* Update handle for SG allocations */
  134. if (handle)
  135. *handle = end;
  136. return n;
  137. }
  138. void iommu_range_free(struct iommu *iommu, dma_addr_t dma_addr, unsigned long npages)
  139. {
  140. struct iommu_arena *arena = &iommu->arena;
  141. unsigned long entry;
  142. entry = (dma_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT;
  143. bitmap_clear(arena->map, entry, npages);
  144. }
  145. int iommu_table_init(struct iommu *iommu, int tsbsize,
  146. u32 dma_offset, u32 dma_addr_mask,
  147. int numa_node)
  148. {
  149. unsigned long i, order, sz, num_tsb_entries;
  150. struct page *page;
  151. num_tsb_entries = tsbsize / sizeof(iopte_t);
  152. /* Setup initial software IOMMU state. */
  153. spin_lock_init(&iommu->lock);
  154. iommu->ctx_lowest_free = 1;
  155. iommu->page_table_map_base = dma_offset;
  156. iommu->dma_addr_mask = dma_addr_mask;
  157. /* Allocate and initialize the free area map. */
  158. sz = num_tsb_entries / 8;
  159. sz = (sz + 7UL) & ~7UL;
  160. iommu->arena.map = kmalloc_node(sz, GFP_KERNEL, numa_node);
  161. if (!iommu->arena.map) {
  162. printk(KERN_ERR "IOMMU: Error, kmalloc(arena.map) failed.\n");
  163. return -ENOMEM;
  164. }
  165. memset(iommu->arena.map, 0, sz);
  166. iommu->arena.limit = num_tsb_entries;
  167. if (tlb_type != hypervisor)
  168. iommu->flush_all = iommu_flushall;
  169. /* Allocate and initialize the dummy page which we
  170. * set inactive IO PTEs to point to.
  171. */
  172. page = alloc_pages_node(numa_node, GFP_KERNEL, 0);
  173. if (!page) {
  174. printk(KERN_ERR "IOMMU: Error, gfp(dummy_page) failed.\n");
  175. goto out_free_map;
  176. }
  177. iommu->dummy_page = (unsigned long) page_address(page);
  178. memset((void *)iommu->dummy_page, 0, PAGE_SIZE);
  179. iommu->dummy_page_pa = (unsigned long) __pa(iommu->dummy_page);
  180. /* Now allocate and setup the IOMMU page table itself. */
  181. order = get_order(tsbsize);
  182. page = alloc_pages_node(numa_node, GFP_KERNEL, order);
  183. if (!page) {
  184. printk(KERN_ERR "IOMMU: Error, gfp(tsb) failed.\n");
  185. goto out_free_dummy_page;
  186. }
  187. iommu->page_table = (iopte_t *)page_address(page);
  188. for (i = 0; i < num_tsb_entries; i++)
  189. iopte_make_dummy(iommu, &iommu->page_table[i]);
  190. return 0;
  191. out_free_dummy_page:
  192. free_page(iommu->dummy_page);
  193. iommu->dummy_page = 0UL;
  194. out_free_map:
  195. kfree(iommu->arena.map);
  196. iommu->arena.map = NULL;
  197. return -ENOMEM;
  198. }
  199. static inline iopte_t *alloc_npages(struct device *dev, struct iommu *iommu,
  200. unsigned long npages)
  201. {
  202. unsigned long entry;
  203. entry = iommu_range_alloc(dev, iommu, npages, NULL);
  204. if (unlikely(entry == DMA_ERROR_CODE))
  205. return NULL;
  206. return iommu->page_table + entry;
  207. }
  208. static int iommu_alloc_ctx(struct iommu *iommu)
  209. {
  210. int lowest = iommu->ctx_lowest_free;
  211. int n = find_next_zero_bit(iommu->ctx_bitmap, IOMMU_NUM_CTXS, lowest);
  212. if (unlikely(n == IOMMU_NUM_CTXS)) {
  213. n = find_next_zero_bit(iommu->ctx_bitmap, lowest, 1);
  214. if (unlikely(n == lowest)) {
  215. printk(KERN_WARNING "IOMMU: Ran out of contexts.\n");
  216. n = 0;
  217. }
  218. }
  219. if (n)
  220. __set_bit(n, iommu->ctx_bitmap);
  221. return n;
  222. }
  223. static inline void iommu_free_ctx(struct iommu *iommu, int ctx)
  224. {
  225. if (likely(ctx)) {
  226. __clear_bit(ctx, iommu->ctx_bitmap);
  227. if (ctx < iommu->ctx_lowest_free)
  228. iommu->ctx_lowest_free = ctx;
  229. }
  230. }
  231. static void *dma_4u_alloc_coherent(struct device *dev, size_t size,
  232. dma_addr_t *dma_addrp, gfp_t gfp)
  233. {
  234. unsigned long flags, order, first_page;
  235. struct iommu *iommu;
  236. struct page *page;
  237. int npages, nid;
  238. iopte_t *iopte;
  239. void *ret;
  240. size = IO_PAGE_ALIGN(size);
  241. order = get_order(size);
  242. if (order >= 10)
  243. return NULL;
  244. nid = dev->archdata.numa_node;
  245. page = alloc_pages_node(nid, gfp, order);
  246. if (unlikely(!page))
  247. return NULL;
  248. first_page = (unsigned long) page_address(page);
  249. memset((char *)first_page, 0, PAGE_SIZE << order);
  250. iommu = dev->archdata.iommu;
  251. spin_lock_irqsave(&iommu->lock, flags);
  252. iopte = alloc_npages(dev, iommu, size >> IO_PAGE_SHIFT);
  253. spin_unlock_irqrestore(&iommu->lock, flags);
  254. if (unlikely(iopte == NULL)) {
  255. free_pages(first_page, order);
  256. return NULL;
  257. }
  258. *dma_addrp = (iommu->page_table_map_base +
  259. ((iopte - iommu->page_table) << IO_PAGE_SHIFT));
  260. ret = (void *) first_page;
  261. npages = size >> IO_PAGE_SHIFT;
  262. first_page = __pa(first_page);
  263. while (npages--) {
  264. iopte_val(*iopte) = (IOPTE_CONSISTENT(0UL) |
  265. IOPTE_WRITE |
  266. (first_page & IOPTE_PAGE));
  267. iopte++;
  268. first_page += IO_PAGE_SIZE;
  269. }
  270. return ret;
  271. }
  272. static void dma_4u_free_coherent(struct device *dev, size_t size,
  273. void *cpu, dma_addr_t dvma)
  274. {
  275. struct iommu *iommu;
  276. unsigned long flags, order, npages;
  277. npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
  278. iommu = dev->archdata.iommu;
  279. spin_lock_irqsave(&iommu->lock, flags);
  280. iommu_range_free(iommu, dvma, npages);
  281. spin_unlock_irqrestore(&iommu->lock, flags);
  282. order = get_order(size);
  283. if (order < 10)
  284. free_pages((unsigned long)cpu, order);
  285. }
  286. static dma_addr_t dma_4u_map_page(struct device *dev, struct page *page,
  287. unsigned long offset, size_t sz,
  288. enum dma_data_direction direction,
  289. struct dma_attrs *attrs)
  290. {
  291. struct iommu *iommu;
  292. struct strbuf *strbuf;
  293. iopte_t *base;
  294. unsigned long flags, npages, oaddr;
  295. unsigned long i, base_paddr, ctx;
  296. u32 bus_addr, ret;
  297. unsigned long iopte_protection;
  298. iommu = dev->archdata.iommu;
  299. strbuf = dev->archdata.stc;
  300. if (unlikely(direction == DMA_NONE))
  301. goto bad_no_ctx;
  302. oaddr = (unsigned long)(page_address(page) + offset);
  303. npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
  304. npages >>= IO_PAGE_SHIFT;
  305. spin_lock_irqsave(&iommu->lock, flags);
  306. base = alloc_npages(dev, iommu, npages);
  307. ctx = 0;
  308. if (iommu->iommu_ctxflush)
  309. ctx = iommu_alloc_ctx(iommu);
  310. spin_unlock_irqrestore(&iommu->lock, flags);
  311. if (unlikely(!base))
  312. goto bad;
  313. bus_addr = (iommu->page_table_map_base +
  314. ((base - iommu->page_table) << IO_PAGE_SHIFT));
  315. ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
  316. base_paddr = __pa(oaddr & IO_PAGE_MASK);
  317. if (strbuf->strbuf_enabled)
  318. iopte_protection = IOPTE_STREAMING(ctx);
  319. else
  320. iopte_protection = IOPTE_CONSISTENT(ctx);
  321. if (direction != DMA_TO_DEVICE)
  322. iopte_protection |= IOPTE_WRITE;
  323. for (i = 0; i < npages; i++, base++, base_paddr += IO_PAGE_SIZE)
  324. iopte_val(*base) = iopte_protection | base_paddr;
  325. return ret;
  326. bad:
  327. iommu_free_ctx(iommu, ctx);
  328. bad_no_ctx:
  329. if (printk_ratelimit())
  330. WARN_ON(1);
  331. return DMA_ERROR_CODE;
  332. }
  333. static void strbuf_flush(struct strbuf *strbuf, struct iommu *iommu,
  334. u32 vaddr, unsigned long ctx, unsigned long npages,
  335. enum dma_data_direction direction)
  336. {
  337. int limit;
  338. if (strbuf->strbuf_ctxflush &&
  339. iommu->iommu_ctxflush) {
  340. unsigned long matchreg, flushreg;
  341. u64 val;
  342. flushreg = strbuf->strbuf_ctxflush;
  343. matchreg = STC_CTXMATCH_ADDR(strbuf, ctx);
  344. iommu_write(flushreg, ctx);
  345. val = iommu_read(matchreg);
  346. val &= 0xffff;
  347. if (!val)
  348. goto do_flush_sync;
  349. while (val) {
  350. if (val & 0x1)
  351. iommu_write(flushreg, ctx);
  352. val >>= 1;
  353. }
  354. val = iommu_read(matchreg);
  355. if (unlikely(val)) {
  356. printk(KERN_WARNING "strbuf_flush: ctx flush "
  357. "timeout matchreg[%llx] ctx[%lx]\n",
  358. val, ctx);
  359. goto do_page_flush;
  360. }
  361. } else {
  362. unsigned long i;
  363. do_page_flush:
  364. for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE)
  365. iommu_write(strbuf->strbuf_pflush, vaddr);
  366. }
  367. do_flush_sync:
  368. /* If the device could not have possibly put dirty data into
  369. * the streaming cache, no flush-flag synchronization needs
  370. * to be performed.
  371. */
  372. if (direction == DMA_TO_DEVICE)
  373. return;
  374. STC_FLUSHFLAG_INIT(strbuf);
  375. iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
  376. (void) iommu_read(iommu->write_complete_reg);
  377. limit = 100000;
  378. while (!STC_FLUSHFLAG_SET(strbuf)) {
  379. limit--;
  380. if (!limit)
  381. break;
  382. udelay(1);
  383. rmb();
  384. }
  385. if (!limit)
  386. printk(KERN_WARNING "strbuf_flush: flushflag timeout "
  387. "vaddr[%08x] ctx[%lx] npages[%ld]\n",
  388. vaddr, ctx, npages);
  389. }
  390. static void dma_4u_unmap_page(struct device *dev, dma_addr_t bus_addr,
  391. size_t sz, enum dma_data_direction direction,
  392. struct dma_attrs *attrs)
  393. {
  394. struct iommu *iommu;
  395. struct strbuf *strbuf;
  396. iopte_t *base;
  397. unsigned long flags, npages, ctx, i;
  398. if (unlikely(direction == DMA_NONE)) {
  399. if (printk_ratelimit())
  400. WARN_ON(1);
  401. return;
  402. }
  403. iommu = dev->archdata.iommu;
  404. strbuf = dev->archdata.stc;
  405. npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
  406. npages >>= IO_PAGE_SHIFT;
  407. base = iommu->page_table +
  408. ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  409. bus_addr &= IO_PAGE_MASK;
  410. spin_lock_irqsave(&iommu->lock, flags);
  411. /* Record the context, if any. */
  412. ctx = 0;
  413. if (iommu->iommu_ctxflush)
  414. ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
  415. /* Step 1: Kick data out of streaming buffers if necessary. */
  416. if (strbuf->strbuf_enabled)
  417. strbuf_flush(strbuf, iommu, bus_addr, ctx,
  418. npages, direction);
  419. /* Step 2: Clear out TSB entries. */
  420. for (i = 0; i < npages; i++)
  421. iopte_make_dummy(iommu, base + i);
  422. iommu_range_free(iommu, bus_addr, npages);
  423. iommu_free_ctx(iommu, ctx);
  424. spin_unlock_irqrestore(&iommu->lock, flags);
  425. }
  426. static int dma_4u_map_sg(struct device *dev, struct scatterlist *sglist,
  427. int nelems, enum dma_data_direction direction,
  428. struct dma_attrs *attrs)
  429. {
  430. struct scatterlist *s, *outs, *segstart;
  431. unsigned long flags, handle, prot, ctx;
  432. dma_addr_t dma_next = 0, dma_addr;
  433. unsigned int max_seg_size;
  434. unsigned long seg_boundary_size;
  435. int outcount, incount, i;
  436. struct strbuf *strbuf;
  437. struct iommu *iommu;
  438. unsigned long base_shift;
  439. BUG_ON(direction == DMA_NONE);
  440. iommu = dev->archdata.iommu;
  441. strbuf = dev->archdata.stc;
  442. if (nelems == 0 || !iommu)
  443. return 0;
  444. spin_lock_irqsave(&iommu->lock, flags);
  445. ctx = 0;
  446. if (iommu->iommu_ctxflush)
  447. ctx = iommu_alloc_ctx(iommu);
  448. if (strbuf->strbuf_enabled)
  449. prot = IOPTE_STREAMING(ctx);
  450. else
  451. prot = IOPTE_CONSISTENT(ctx);
  452. if (direction != DMA_TO_DEVICE)
  453. prot |= IOPTE_WRITE;
  454. outs = s = segstart = &sglist[0];
  455. outcount = 1;
  456. incount = nelems;
  457. handle = 0;
  458. /* Init first segment length for backout at failure */
  459. outs->dma_length = 0;
  460. max_seg_size = dma_get_max_seg_size(dev);
  461. seg_boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
  462. IO_PAGE_SIZE) >> IO_PAGE_SHIFT;
  463. base_shift = iommu->page_table_map_base >> IO_PAGE_SHIFT;
  464. for_each_sg(sglist, s, nelems, i) {
  465. unsigned long paddr, npages, entry, out_entry = 0, slen;
  466. iopte_t *base;
  467. slen = s->length;
  468. /* Sanity check */
  469. if (slen == 0) {
  470. dma_next = 0;
  471. continue;
  472. }
  473. /* Allocate iommu entries for that segment */
  474. paddr = (unsigned long) SG_ENT_PHYS_ADDRESS(s);
  475. npages = iommu_num_pages(paddr, slen, IO_PAGE_SIZE);
  476. entry = iommu_range_alloc(dev, iommu, npages, &handle);
  477. /* Handle failure */
  478. if (unlikely(entry == DMA_ERROR_CODE)) {
  479. if (printk_ratelimit())
  480. printk(KERN_INFO "iommu_alloc failed, iommu %p paddr %lx"
  481. " npages %lx\n", iommu, paddr, npages);
  482. goto iommu_map_failed;
  483. }
  484. base = iommu->page_table + entry;
  485. /* Convert entry to a dma_addr_t */
  486. dma_addr = iommu->page_table_map_base +
  487. (entry << IO_PAGE_SHIFT);
  488. dma_addr |= (s->offset & ~IO_PAGE_MASK);
  489. /* Insert into HW table */
  490. paddr &= IO_PAGE_MASK;
  491. while (npages--) {
  492. iopte_val(*base) = prot | paddr;
  493. base++;
  494. paddr += IO_PAGE_SIZE;
  495. }
  496. /* If we are in an open segment, try merging */
  497. if (segstart != s) {
  498. /* We cannot merge if:
  499. * - allocated dma_addr isn't contiguous to previous allocation
  500. */
  501. if ((dma_addr != dma_next) ||
  502. (outs->dma_length + s->length > max_seg_size) ||
  503. (is_span_boundary(out_entry, base_shift,
  504. seg_boundary_size, outs, s))) {
  505. /* Can't merge: create a new segment */
  506. segstart = s;
  507. outcount++;
  508. outs = sg_next(outs);
  509. } else {
  510. outs->dma_length += s->length;
  511. }
  512. }
  513. if (segstart == s) {
  514. /* This is a new segment, fill entries */
  515. outs->dma_address = dma_addr;
  516. outs->dma_length = slen;
  517. out_entry = entry;
  518. }
  519. /* Calculate next page pointer for contiguous check */
  520. dma_next = dma_addr + slen;
  521. }
  522. spin_unlock_irqrestore(&iommu->lock, flags);
  523. if (outcount < incount) {
  524. outs = sg_next(outs);
  525. outs->dma_address = DMA_ERROR_CODE;
  526. outs->dma_length = 0;
  527. }
  528. return outcount;
  529. iommu_map_failed:
  530. for_each_sg(sglist, s, nelems, i) {
  531. if (s->dma_length != 0) {
  532. unsigned long vaddr, npages, entry, j;
  533. iopte_t *base;
  534. vaddr = s->dma_address & IO_PAGE_MASK;
  535. npages = iommu_num_pages(s->dma_address, s->dma_length,
  536. IO_PAGE_SIZE);
  537. iommu_range_free(iommu, vaddr, npages);
  538. entry = (vaddr - iommu->page_table_map_base)
  539. >> IO_PAGE_SHIFT;
  540. base = iommu->page_table + entry;
  541. for (j = 0; j < npages; j++)
  542. iopte_make_dummy(iommu, base + j);
  543. s->dma_address = DMA_ERROR_CODE;
  544. s->dma_length = 0;
  545. }
  546. if (s == outs)
  547. break;
  548. }
  549. spin_unlock_irqrestore(&iommu->lock, flags);
  550. return 0;
  551. }
  552. /* If contexts are being used, they are the same in all of the mappings
  553. * we make for a particular SG.
  554. */
  555. static unsigned long fetch_sg_ctx(struct iommu *iommu, struct scatterlist *sg)
  556. {
  557. unsigned long ctx = 0;
  558. if (iommu->iommu_ctxflush) {
  559. iopte_t *base;
  560. u32 bus_addr;
  561. bus_addr = sg->dma_address & IO_PAGE_MASK;
  562. base = iommu->page_table +
  563. ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  564. ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
  565. }
  566. return ctx;
  567. }
  568. static void dma_4u_unmap_sg(struct device *dev, struct scatterlist *sglist,
  569. int nelems, enum dma_data_direction direction,
  570. struct dma_attrs *attrs)
  571. {
  572. unsigned long flags, ctx;
  573. struct scatterlist *sg;
  574. struct strbuf *strbuf;
  575. struct iommu *iommu;
  576. BUG_ON(direction == DMA_NONE);
  577. iommu = dev->archdata.iommu;
  578. strbuf = dev->archdata.stc;
  579. ctx = fetch_sg_ctx(iommu, sglist);
  580. spin_lock_irqsave(&iommu->lock, flags);
  581. sg = sglist;
  582. while (nelems--) {
  583. dma_addr_t dma_handle = sg->dma_address;
  584. unsigned int len = sg->dma_length;
  585. unsigned long npages, entry;
  586. iopte_t *base;
  587. int i;
  588. if (!len)
  589. break;
  590. npages = iommu_num_pages(dma_handle, len, IO_PAGE_SIZE);
  591. iommu_range_free(iommu, dma_handle, npages);
  592. entry = ((dma_handle - iommu->page_table_map_base)
  593. >> IO_PAGE_SHIFT);
  594. base = iommu->page_table + entry;
  595. dma_handle &= IO_PAGE_MASK;
  596. if (strbuf->strbuf_enabled)
  597. strbuf_flush(strbuf, iommu, dma_handle, ctx,
  598. npages, direction);
  599. for (i = 0; i < npages; i++)
  600. iopte_make_dummy(iommu, base + i);
  601. sg = sg_next(sg);
  602. }
  603. iommu_free_ctx(iommu, ctx);
  604. spin_unlock_irqrestore(&iommu->lock, flags);
  605. }
  606. static void dma_4u_sync_single_for_cpu(struct device *dev,
  607. dma_addr_t bus_addr, size_t sz,
  608. enum dma_data_direction direction)
  609. {
  610. struct iommu *iommu;
  611. struct strbuf *strbuf;
  612. unsigned long flags, ctx, npages;
  613. iommu = dev->archdata.iommu;
  614. strbuf = dev->archdata.stc;
  615. if (!strbuf->strbuf_enabled)
  616. return;
  617. spin_lock_irqsave(&iommu->lock, flags);
  618. npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
  619. npages >>= IO_PAGE_SHIFT;
  620. bus_addr &= IO_PAGE_MASK;
  621. /* Step 1: Record the context, if any. */
  622. ctx = 0;
  623. if (iommu->iommu_ctxflush &&
  624. strbuf->strbuf_ctxflush) {
  625. iopte_t *iopte;
  626. iopte = iommu->page_table +
  627. ((bus_addr - iommu->page_table_map_base)>>IO_PAGE_SHIFT);
  628. ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
  629. }
  630. /* Step 2: Kick data out of streaming buffers. */
  631. strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
  632. spin_unlock_irqrestore(&iommu->lock, flags);
  633. }
  634. static void dma_4u_sync_sg_for_cpu(struct device *dev,
  635. struct scatterlist *sglist, int nelems,
  636. enum dma_data_direction direction)
  637. {
  638. struct iommu *iommu;
  639. struct strbuf *strbuf;
  640. unsigned long flags, ctx, npages, i;
  641. struct scatterlist *sg, *sgprv;
  642. u32 bus_addr;
  643. iommu = dev->archdata.iommu;
  644. strbuf = dev->archdata.stc;
  645. if (!strbuf->strbuf_enabled)
  646. return;
  647. spin_lock_irqsave(&iommu->lock, flags);
  648. /* Step 1: Record the context, if any. */
  649. ctx = 0;
  650. if (iommu->iommu_ctxflush &&
  651. strbuf->strbuf_ctxflush) {
  652. iopte_t *iopte;
  653. iopte = iommu->page_table +
  654. ((sglist[0].dma_address - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  655. ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
  656. }
  657. /* Step 2: Kick data out of streaming buffers. */
  658. bus_addr = sglist[0].dma_address & IO_PAGE_MASK;
  659. sgprv = NULL;
  660. for_each_sg(sglist, sg, nelems, i) {
  661. if (sg->dma_length == 0)
  662. break;
  663. sgprv = sg;
  664. }
  665. npages = (IO_PAGE_ALIGN(sgprv->dma_address + sgprv->dma_length)
  666. - bus_addr) >> IO_PAGE_SHIFT;
  667. strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
  668. spin_unlock_irqrestore(&iommu->lock, flags);
  669. }
  670. static struct dma_map_ops sun4u_dma_ops = {
  671. .alloc_coherent = dma_4u_alloc_coherent,
  672. .free_coherent = dma_4u_free_coherent,
  673. .map_page = dma_4u_map_page,
  674. .unmap_page = dma_4u_unmap_page,
  675. .map_sg = dma_4u_map_sg,
  676. .unmap_sg = dma_4u_unmap_sg,
  677. .sync_single_for_cpu = dma_4u_sync_single_for_cpu,
  678. .sync_sg_for_cpu = dma_4u_sync_sg_for_cpu,
  679. };
  680. struct dma_map_ops *dma_ops = &sun4u_dma_ops;
  681. EXPORT_SYMBOL(dma_ops);
  682. extern int pci64_dma_supported(struct pci_dev *pdev, u64 device_mask);
  683. int dma_supported(struct device *dev, u64 device_mask)
  684. {
  685. struct iommu *iommu = dev->archdata.iommu;
  686. u64 dma_addr_mask = iommu->dma_addr_mask;
  687. if (device_mask >= (1UL << 32UL))
  688. return 0;
  689. if ((device_mask & dma_addr_mask) == dma_addr_mask)
  690. return 1;
  691. #ifdef CONFIG_PCI
  692. if (dev->bus == &pci_bus_type)
  693. return pci64_dma_supported(to_pci_dev(dev), device_mask);
  694. #endif
  695. return 0;
  696. }
  697. EXPORT_SYMBOL(dma_supported);