iommu.c 20 KB

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