iommu.c 20 KB

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