iommu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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/export.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. struct dma_attrs *attrs)
  234. {
  235. unsigned long flags, order, first_page;
  236. struct iommu *iommu;
  237. struct page *page;
  238. int npages, nid;
  239. iopte_t *iopte;
  240. void *ret;
  241. size = IO_PAGE_ALIGN(size);
  242. order = get_order(size);
  243. if (order >= 10)
  244. return NULL;
  245. nid = dev->archdata.numa_node;
  246. page = alloc_pages_node(nid, gfp, order);
  247. if (unlikely(!page))
  248. return NULL;
  249. first_page = (unsigned long) page_address(page);
  250. memset((char *)first_page, 0, PAGE_SIZE << order);
  251. iommu = dev->archdata.iommu;
  252. spin_lock_irqsave(&iommu->lock, flags);
  253. iopte = alloc_npages(dev, iommu, size >> IO_PAGE_SHIFT);
  254. spin_unlock_irqrestore(&iommu->lock, flags);
  255. if (unlikely(iopte == NULL)) {
  256. free_pages(first_page, order);
  257. return NULL;
  258. }
  259. *dma_addrp = (iommu->page_table_map_base +
  260. ((iopte - iommu->page_table) << IO_PAGE_SHIFT));
  261. ret = (void *) first_page;
  262. npages = size >> IO_PAGE_SHIFT;
  263. first_page = __pa(first_page);
  264. while (npages--) {
  265. iopte_val(*iopte) = (IOPTE_CONSISTENT(0UL) |
  266. IOPTE_WRITE |
  267. (first_page & IOPTE_PAGE));
  268. iopte++;
  269. first_page += IO_PAGE_SIZE;
  270. }
  271. return ret;
  272. }
  273. static void dma_4u_free_coherent(struct device *dev, size_t size,
  274. void *cpu, dma_addr_t dvma,
  275. struct dma_attrs *attrs)
  276. {
  277. struct iommu *iommu;
  278. unsigned long flags, order, npages;
  279. npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
  280. iommu = dev->archdata.iommu;
  281. spin_lock_irqsave(&iommu->lock, flags);
  282. iommu_range_free(iommu, dvma, npages);
  283. spin_unlock_irqrestore(&iommu->lock, flags);
  284. order = get_order(size);
  285. if (order < 10)
  286. free_pages((unsigned long)cpu, order);
  287. }
  288. static dma_addr_t dma_4u_map_page(struct device *dev, struct page *page,
  289. unsigned long offset, size_t sz,
  290. enum dma_data_direction direction,
  291. struct dma_attrs *attrs)
  292. {
  293. struct iommu *iommu;
  294. struct strbuf *strbuf;
  295. iopte_t *base;
  296. unsigned long flags, npages, oaddr;
  297. unsigned long i, base_paddr, ctx;
  298. u32 bus_addr, ret;
  299. unsigned long iopte_protection;
  300. iommu = dev->archdata.iommu;
  301. strbuf = dev->archdata.stc;
  302. if (unlikely(direction == DMA_NONE))
  303. goto bad_no_ctx;
  304. oaddr = (unsigned long)(page_address(page) + offset);
  305. npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
  306. npages >>= IO_PAGE_SHIFT;
  307. spin_lock_irqsave(&iommu->lock, flags);
  308. base = alloc_npages(dev, iommu, npages);
  309. ctx = 0;
  310. if (iommu->iommu_ctxflush)
  311. ctx = iommu_alloc_ctx(iommu);
  312. spin_unlock_irqrestore(&iommu->lock, flags);
  313. if (unlikely(!base))
  314. goto bad;
  315. bus_addr = (iommu->page_table_map_base +
  316. ((base - iommu->page_table) << IO_PAGE_SHIFT));
  317. ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
  318. base_paddr = __pa(oaddr & IO_PAGE_MASK);
  319. if (strbuf->strbuf_enabled)
  320. iopte_protection = IOPTE_STREAMING(ctx);
  321. else
  322. iopte_protection = IOPTE_CONSISTENT(ctx);
  323. if (direction != DMA_TO_DEVICE)
  324. iopte_protection |= IOPTE_WRITE;
  325. for (i = 0; i < npages; i++, base++, base_paddr += IO_PAGE_SIZE)
  326. iopte_val(*base) = iopte_protection | base_paddr;
  327. return ret;
  328. bad:
  329. iommu_free_ctx(iommu, ctx);
  330. bad_no_ctx:
  331. if (printk_ratelimit())
  332. WARN_ON(1);
  333. return DMA_ERROR_CODE;
  334. }
  335. static void strbuf_flush(struct strbuf *strbuf, struct iommu *iommu,
  336. u32 vaddr, unsigned long ctx, unsigned long npages,
  337. enum dma_data_direction direction)
  338. {
  339. int limit;
  340. if (strbuf->strbuf_ctxflush &&
  341. iommu->iommu_ctxflush) {
  342. unsigned long matchreg, flushreg;
  343. u64 val;
  344. flushreg = strbuf->strbuf_ctxflush;
  345. matchreg = STC_CTXMATCH_ADDR(strbuf, ctx);
  346. iommu_write(flushreg, ctx);
  347. val = iommu_read(matchreg);
  348. val &= 0xffff;
  349. if (!val)
  350. goto do_flush_sync;
  351. while (val) {
  352. if (val & 0x1)
  353. iommu_write(flushreg, ctx);
  354. val >>= 1;
  355. }
  356. val = iommu_read(matchreg);
  357. if (unlikely(val)) {
  358. printk(KERN_WARNING "strbuf_flush: ctx flush "
  359. "timeout matchreg[%llx] ctx[%lx]\n",
  360. val, ctx);
  361. goto do_page_flush;
  362. }
  363. } else {
  364. unsigned long i;
  365. do_page_flush:
  366. for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE)
  367. iommu_write(strbuf->strbuf_pflush, vaddr);
  368. }
  369. do_flush_sync:
  370. /* If the device could not have possibly put dirty data into
  371. * the streaming cache, no flush-flag synchronization needs
  372. * to be performed.
  373. */
  374. if (direction == DMA_TO_DEVICE)
  375. return;
  376. STC_FLUSHFLAG_INIT(strbuf);
  377. iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
  378. (void) iommu_read(iommu->write_complete_reg);
  379. limit = 100000;
  380. while (!STC_FLUSHFLAG_SET(strbuf)) {
  381. limit--;
  382. if (!limit)
  383. break;
  384. udelay(1);
  385. rmb();
  386. }
  387. if (!limit)
  388. printk(KERN_WARNING "strbuf_flush: flushflag timeout "
  389. "vaddr[%08x] ctx[%lx] npages[%ld]\n",
  390. vaddr, ctx, npages);
  391. }
  392. static void dma_4u_unmap_page(struct device *dev, dma_addr_t bus_addr,
  393. size_t sz, enum dma_data_direction direction,
  394. struct dma_attrs *attrs)
  395. {
  396. struct iommu *iommu;
  397. struct strbuf *strbuf;
  398. iopte_t *base;
  399. unsigned long flags, npages, ctx, i;
  400. if (unlikely(direction == DMA_NONE)) {
  401. if (printk_ratelimit())
  402. WARN_ON(1);
  403. return;
  404. }
  405. iommu = dev->archdata.iommu;
  406. strbuf = dev->archdata.stc;
  407. npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
  408. npages >>= IO_PAGE_SHIFT;
  409. base = iommu->page_table +
  410. ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  411. bus_addr &= IO_PAGE_MASK;
  412. spin_lock_irqsave(&iommu->lock, flags);
  413. /* Record the context, if any. */
  414. ctx = 0;
  415. if (iommu->iommu_ctxflush)
  416. ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
  417. /* Step 1: Kick data out of streaming buffers if necessary. */
  418. if (strbuf->strbuf_enabled)
  419. strbuf_flush(strbuf, iommu, bus_addr, ctx,
  420. npages, direction);
  421. /* Step 2: Clear out TSB entries. */
  422. for (i = 0; i < npages; i++)
  423. iopte_make_dummy(iommu, base + i);
  424. iommu_range_free(iommu, bus_addr, npages);
  425. iommu_free_ctx(iommu, ctx);
  426. spin_unlock_irqrestore(&iommu->lock, flags);
  427. }
  428. static int dma_4u_map_sg(struct device *dev, struct scatterlist *sglist,
  429. int nelems, enum dma_data_direction direction,
  430. struct dma_attrs *attrs)
  431. {
  432. struct scatterlist *s, *outs, *segstart;
  433. unsigned long flags, handle, prot, ctx;
  434. dma_addr_t dma_next = 0, dma_addr;
  435. unsigned int max_seg_size;
  436. unsigned long seg_boundary_size;
  437. int outcount, incount, i;
  438. struct strbuf *strbuf;
  439. struct iommu *iommu;
  440. unsigned long base_shift;
  441. BUG_ON(direction == DMA_NONE);
  442. iommu = dev->archdata.iommu;
  443. strbuf = dev->archdata.stc;
  444. if (nelems == 0 || !iommu)
  445. return 0;
  446. spin_lock_irqsave(&iommu->lock, flags);
  447. ctx = 0;
  448. if (iommu->iommu_ctxflush)
  449. ctx = iommu_alloc_ctx(iommu);
  450. if (strbuf->strbuf_enabled)
  451. prot = IOPTE_STREAMING(ctx);
  452. else
  453. prot = IOPTE_CONSISTENT(ctx);
  454. if (direction != DMA_TO_DEVICE)
  455. prot |= IOPTE_WRITE;
  456. outs = s = segstart = &sglist[0];
  457. outcount = 1;
  458. incount = nelems;
  459. handle = 0;
  460. /* Init first segment length for backout at failure */
  461. outs->dma_length = 0;
  462. max_seg_size = dma_get_max_seg_size(dev);
  463. seg_boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
  464. IO_PAGE_SIZE) >> IO_PAGE_SHIFT;
  465. base_shift = iommu->page_table_map_base >> IO_PAGE_SHIFT;
  466. for_each_sg(sglist, s, nelems, i) {
  467. unsigned long paddr, npages, entry, out_entry = 0, slen;
  468. iopte_t *base;
  469. slen = s->length;
  470. /* Sanity check */
  471. if (slen == 0) {
  472. dma_next = 0;
  473. continue;
  474. }
  475. /* Allocate iommu entries for that segment */
  476. paddr = (unsigned long) SG_ENT_PHYS_ADDRESS(s);
  477. npages = iommu_num_pages(paddr, slen, IO_PAGE_SIZE);
  478. entry = iommu_range_alloc(dev, iommu, npages, &handle);
  479. /* Handle failure */
  480. if (unlikely(entry == DMA_ERROR_CODE)) {
  481. if (printk_ratelimit())
  482. printk(KERN_INFO "iommu_alloc failed, iommu %p paddr %lx"
  483. " npages %lx\n", iommu, paddr, npages);
  484. goto iommu_map_failed;
  485. }
  486. base = iommu->page_table + entry;
  487. /* Convert entry to a dma_addr_t */
  488. dma_addr = iommu->page_table_map_base +
  489. (entry << IO_PAGE_SHIFT);
  490. dma_addr |= (s->offset & ~IO_PAGE_MASK);
  491. /* Insert into HW table */
  492. paddr &= IO_PAGE_MASK;
  493. while (npages--) {
  494. iopte_val(*base) = prot | paddr;
  495. base++;
  496. paddr += IO_PAGE_SIZE;
  497. }
  498. /* If we are in an open segment, try merging */
  499. if (segstart != s) {
  500. /* We cannot merge if:
  501. * - allocated dma_addr isn't contiguous to previous allocation
  502. */
  503. if ((dma_addr != dma_next) ||
  504. (outs->dma_length + s->length > max_seg_size) ||
  505. (is_span_boundary(out_entry, base_shift,
  506. seg_boundary_size, outs, s))) {
  507. /* Can't merge: create a new segment */
  508. segstart = s;
  509. outcount++;
  510. outs = sg_next(outs);
  511. } else {
  512. outs->dma_length += s->length;
  513. }
  514. }
  515. if (segstart == s) {
  516. /* This is a new segment, fill entries */
  517. outs->dma_address = dma_addr;
  518. outs->dma_length = slen;
  519. out_entry = entry;
  520. }
  521. /* Calculate next page pointer for contiguous check */
  522. dma_next = dma_addr + slen;
  523. }
  524. spin_unlock_irqrestore(&iommu->lock, flags);
  525. if (outcount < incount) {
  526. outs = sg_next(outs);
  527. outs->dma_address = DMA_ERROR_CODE;
  528. outs->dma_length = 0;
  529. }
  530. return outcount;
  531. iommu_map_failed:
  532. for_each_sg(sglist, s, nelems, i) {
  533. if (s->dma_length != 0) {
  534. unsigned long vaddr, npages, entry, j;
  535. iopte_t *base;
  536. vaddr = s->dma_address & IO_PAGE_MASK;
  537. npages = iommu_num_pages(s->dma_address, s->dma_length,
  538. IO_PAGE_SIZE);
  539. iommu_range_free(iommu, vaddr, npages);
  540. entry = (vaddr - iommu->page_table_map_base)
  541. >> IO_PAGE_SHIFT;
  542. base = iommu->page_table + entry;
  543. for (j = 0; j < npages; j++)
  544. iopte_make_dummy(iommu, base + j);
  545. s->dma_address = DMA_ERROR_CODE;
  546. s->dma_length = 0;
  547. }
  548. if (s == outs)
  549. break;
  550. }
  551. spin_unlock_irqrestore(&iommu->lock, flags);
  552. return 0;
  553. }
  554. /* If contexts are being used, they are the same in all of the mappings
  555. * we make for a particular SG.
  556. */
  557. static unsigned long fetch_sg_ctx(struct iommu *iommu, struct scatterlist *sg)
  558. {
  559. unsigned long ctx = 0;
  560. if (iommu->iommu_ctxflush) {
  561. iopte_t *base;
  562. u32 bus_addr;
  563. bus_addr = sg->dma_address & IO_PAGE_MASK;
  564. base = iommu->page_table +
  565. ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  566. ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
  567. }
  568. return ctx;
  569. }
  570. static void dma_4u_unmap_sg(struct device *dev, struct scatterlist *sglist,
  571. int nelems, enum dma_data_direction direction,
  572. struct dma_attrs *attrs)
  573. {
  574. unsigned long flags, ctx;
  575. struct scatterlist *sg;
  576. struct strbuf *strbuf;
  577. struct iommu *iommu;
  578. BUG_ON(direction == DMA_NONE);
  579. iommu = dev->archdata.iommu;
  580. strbuf = dev->archdata.stc;
  581. ctx = fetch_sg_ctx(iommu, sglist);
  582. spin_lock_irqsave(&iommu->lock, flags);
  583. sg = sglist;
  584. while (nelems--) {
  585. dma_addr_t dma_handle = sg->dma_address;
  586. unsigned int len = sg->dma_length;
  587. unsigned long npages, entry;
  588. iopte_t *base;
  589. int i;
  590. if (!len)
  591. break;
  592. npages = iommu_num_pages(dma_handle, len, IO_PAGE_SIZE);
  593. iommu_range_free(iommu, dma_handle, npages);
  594. entry = ((dma_handle - iommu->page_table_map_base)
  595. >> IO_PAGE_SHIFT);
  596. base = iommu->page_table + entry;
  597. dma_handle &= IO_PAGE_MASK;
  598. if (strbuf->strbuf_enabled)
  599. strbuf_flush(strbuf, iommu, dma_handle, ctx,
  600. npages, direction);
  601. for (i = 0; i < npages; i++)
  602. iopte_make_dummy(iommu, base + i);
  603. sg = sg_next(sg);
  604. }
  605. iommu_free_ctx(iommu, ctx);
  606. spin_unlock_irqrestore(&iommu->lock, flags);
  607. }
  608. static void dma_4u_sync_single_for_cpu(struct device *dev,
  609. dma_addr_t bus_addr, size_t sz,
  610. enum dma_data_direction direction)
  611. {
  612. struct iommu *iommu;
  613. struct strbuf *strbuf;
  614. unsigned long flags, ctx, npages;
  615. iommu = dev->archdata.iommu;
  616. strbuf = dev->archdata.stc;
  617. if (!strbuf->strbuf_enabled)
  618. return;
  619. spin_lock_irqsave(&iommu->lock, flags);
  620. npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
  621. npages >>= IO_PAGE_SHIFT;
  622. bus_addr &= IO_PAGE_MASK;
  623. /* Step 1: Record the context, if any. */
  624. ctx = 0;
  625. if (iommu->iommu_ctxflush &&
  626. strbuf->strbuf_ctxflush) {
  627. iopte_t *iopte;
  628. iopte = iommu->page_table +
  629. ((bus_addr - iommu->page_table_map_base)>>IO_PAGE_SHIFT);
  630. ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
  631. }
  632. /* Step 2: Kick data out of streaming buffers. */
  633. strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
  634. spin_unlock_irqrestore(&iommu->lock, flags);
  635. }
  636. static void dma_4u_sync_sg_for_cpu(struct device *dev,
  637. struct scatterlist *sglist, int nelems,
  638. enum dma_data_direction direction)
  639. {
  640. struct iommu *iommu;
  641. struct strbuf *strbuf;
  642. unsigned long flags, ctx, npages, i;
  643. struct scatterlist *sg, *sgprv;
  644. u32 bus_addr;
  645. iommu = dev->archdata.iommu;
  646. strbuf = dev->archdata.stc;
  647. if (!strbuf->strbuf_enabled)
  648. return;
  649. spin_lock_irqsave(&iommu->lock, flags);
  650. /* Step 1: Record the context, if any. */
  651. ctx = 0;
  652. if (iommu->iommu_ctxflush &&
  653. strbuf->strbuf_ctxflush) {
  654. iopte_t *iopte;
  655. iopte = iommu->page_table +
  656. ((sglist[0].dma_address - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  657. ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
  658. }
  659. /* Step 2: Kick data out of streaming buffers. */
  660. bus_addr = sglist[0].dma_address & IO_PAGE_MASK;
  661. sgprv = NULL;
  662. for_each_sg(sglist, sg, nelems, i) {
  663. if (sg->dma_length == 0)
  664. break;
  665. sgprv = sg;
  666. }
  667. npages = (IO_PAGE_ALIGN(sgprv->dma_address + sgprv->dma_length)
  668. - bus_addr) >> IO_PAGE_SHIFT;
  669. strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
  670. spin_unlock_irqrestore(&iommu->lock, flags);
  671. }
  672. static struct dma_map_ops sun4u_dma_ops = {
  673. .alloc = dma_4u_alloc_coherent,
  674. .free = dma_4u_free_coherent,
  675. .map_page = dma_4u_map_page,
  676. .unmap_page = dma_4u_unmap_page,
  677. .map_sg = dma_4u_map_sg,
  678. .unmap_sg = dma_4u_unmap_sg,
  679. .sync_single_for_cpu = dma_4u_sync_single_for_cpu,
  680. .sync_sg_for_cpu = dma_4u_sync_sg_for_cpu,
  681. };
  682. struct dma_map_ops *dma_ops = &sun4u_dma_ops;
  683. EXPORT_SYMBOL(dma_ops);
  684. extern int pci64_dma_supported(struct pci_dev *pdev, u64 device_mask);
  685. int dma_supported(struct device *dev, u64 device_mask)
  686. {
  687. struct iommu *iommu = dev->archdata.iommu;
  688. u64 dma_addr_mask = iommu->dma_addr_mask;
  689. if (device_mask >= (1UL << 32UL))
  690. return 0;
  691. if ((device_mask & dma_addr_mask) == dma_addr_mask)
  692. return 1;
  693. #ifdef CONFIG_PCI
  694. if (dev->bus == &pci_bus_type)
  695. return pci64_dma_supported(to_pci_dev(dev), device_mask);
  696. #endif
  697. return 0;
  698. }
  699. EXPORT_SYMBOL(dma_supported);