iommu.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. /* iommu.c: Generic sparc64 IOMMU support.
  2. *
  3. * Copyright (C) 1999, 2007 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/scatterlist.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 largely upon the ppc64 iommu allocator. */
  72. static long arena_alloc(struct iommu *iommu, unsigned long npages)
  73. {
  74. struct iommu_arena *arena = &iommu->arena;
  75. unsigned long n, i, start, end, limit;
  76. int pass;
  77. limit = arena->limit;
  78. start = arena->hint;
  79. pass = 0;
  80. again:
  81. n = find_next_zero_bit(arena->map, limit, start);
  82. end = n + npages;
  83. if (unlikely(end >= limit)) {
  84. if (likely(pass < 1)) {
  85. limit = start;
  86. start = 0;
  87. __iommu_flushall(iommu);
  88. pass++;
  89. goto again;
  90. } else {
  91. /* Scanned the whole thing, give up. */
  92. return -1;
  93. }
  94. }
  95. for (i = n; i < end; i++) {
  96. if (test_bit(i, arena->map)) {
  97. start = i + 1;
  98. goto again;
  99. }
  100. }
  101. for (i = n; i < end; i++)
  102. __set_bit(i, arena->map);
  103. arena->hint = end;
  104. return n;
  105. }
  106. static void arena_free(struct iommu_arena *arena, unsigned long base, unsigned long npages)
  107. {
  108. unsigned long i;
  109. for (i = base; i < (base + npages); i++)
  110. __clear_bit(i, arena->map);
  111. }
  112. int iommu_table_init(struct iommu *iommu, int tsbsize,
  113. u32 dma_offset, u32 dma_addr_mask)
  114. {
  115. unsigned long i, tsbbase, order, sz, num_tsb_entries;
  116. num_tsb_entries = tsbsize / sizeof(iopte_t);
  117. /* Setup initial software IOMMU state. */
  118. spin_lock_init(&iommu->lock);
  119. iommu->ctx_lowest_free = 1;
  120. iommu->page_table_map_base = dma_offset;
  121. iommu->dma_addr_mask = dma_addr_mask;
  122. /* Allocate and initialize the free area map. */
  123. sz = num_tsb_entries / 8;
  124. sz = (sz + 7UL) & ~7UL;
  125. iommu->arena.map = kzalloc(sz, GFP_KERNEL);
  126. if (!iommu->arena.map) {
  127. printk(KERN_ERR "IOMMU: Error, kmalloc(arena.map) failed.\n");
  128. return -ENOMEM;
  129. }
  130. iommu->arena.limit = num_tsb_entries;
  131. /* Allocate and initialize the dummy page which we
  132. * set inactive IO PTEs to point to.
  133. */
  134. iommu->dummy_page = __get_free_pages(GFP_KERNEL, 0);
  135. if (!iommu->dummy_page) {
  136. printk(KERN_ERR "IOMMU: Error, gfp(dummy_page) failed.\n");
  137. goto out_free_map;
  138. }
  139. memset((void *)iommu->dummy_page, 0, PAGE_SIZE);
  140. iommu->dummy_page_pa = (unsigned long) __pa(iommu->dummy_page);
  141. /* Now allocate and setup the IOMMU page table itself. */
  142. order = get_order(tsbsize);
  143. tsbbase = __get_free_pages(GFP_KERNEL, order);
  144. if (!tsbbase) {
  145. printk(KERN_ERR "IOMMU: Error, gfp(tsb) failed.\n");
  146. goto out_free_dummy_page;
  147. }
  148. iommu->page_table = (iopte_t *)tsbbase;
  149. for (i = 0; i < num_tsb_entries; i++)
  150. iopte_make_dummy(iommu, &iommu->page_table[i]);
  151. return 0;
  152. out_free_dummy_page:
  153. free_page(iommu->dummy_page);
  154. iommu->dummy_page = 0UL;
  155. out_free_map:
  156. kfree(iommu->arena.map);
  157. iommu->arena.map = NULL;
  158. return -ENOMEM;
  159. }
  160. static inline iopte_t *alloc_npages(struct iommu *iommu, unsigned long npages)
  161. {
  162. long entry;
  163. entry = arena_alloc(iommu, npages);
  164. if (unlikely(entry < 0))
  165. return NULL;
  166. return iommu->page_table + entry;
  167. }
  168. static inline void free_npages(struct iommu *iommu, dma_addr_t base, unsigned long npages)
  169. {
  170. arena_free(&iommu->arena, base >> IO_PAGE_SHIFT, npages);
  171. }
  172. static int iommu_alloc_ctx(struct iommu *iommu)
  173. {
  174. int lowest = iommu->ctx_lowest_free;
  175. int sz = IOMMU_NUM_CTXS - lowest;
  176. int n = find_next_zero_bit(iommu->ctx_bitmap, sz, lowest);
  177. if (unlikely(n == sz)) {
  178. n = find_next_zero_bit(iommu->ctx_bitmap, lowest, 1);
  179. if (unlikely(n == lowest)) {
  180. printk(KERN_WARNING "IOMMU: Ran out of contexts.\n");
  181. n = 0;
  182. }
  183. }
  184. if (n)
  185. __set_bit(n, iommu->ctx_bitmap);
  186. return n;
  187. }
  188. static inline void iommu_free_ctx(struct iommu *iommu, int ctx)
  189. {
  190. if (likely(ctx)) {
  191. __clear_bit(ctx, iommu->ctx_bitmap);
  192. if (ctx < iommu->ctx_lowest_free)
  193. iommu->ctx_lowest_free = ctx;
  194. }
  195. }
  196. static void *dma_4u_alloc_coherent(struct device *dev, size_t size,
  197. dma_addr_t *dma_addrp, gfp_t gfp)
  198. {
  199. struct iommu *iommu;
  200. iopte_t *iopte;
  201. unsigned long flags, order, first_page;
  202. void *ret;
  203. int npages;
  204. size = IO_PAGE_ALIGN(size);
  205. order = get_order(size);
  206. if (order >= 10)
  207. return NULL;
  208. first_page = __get_free_pages(gfp, order);
  209. if (first_page == 0UL)
  210. return NULL;
  211. memset((char *)first_page, 0, PAGE_SIZE << order);
  212. iommu = dev->archdata.iommu;
  213. spin_lock_irqsave(&iommu->lock, flags);
  214. iopte = alloc_npages(iommu, size >> IO_PAGE_SHIFT);
  215. spin_unlock_irqrestore(&iommu->lock, flags);
  216. if (unlikely(iopte == NULL)) {
  217. free_pages(first_page, order);
  218. return NULL;
  219. }
  220. *dma_addrp = (iommu->page_table_map_base +
  221. ((iopte - iommu->page_table) << IO_PAGE_SHIFT));
  222. ret = (void *) first_page;
  223. npages = size >> IO_PAGE_SHIFT;
  224. first_page = __pa(first_page);
  225. while (npages--) {
  226. iopte_val(*iopte) = (IOPTE_CONSISTENT(0UL) |
  227. IOPTE_WRITE |
  228. (first_page & IOPTE_PAGE));
  229. iopte++;
  230. first_page += IO_PAGE_SIZE;
  231. }
  232. return ret;
  233. }
  234. static void dma_4u_free_coherent(struct device *dev, size_t size,
  235. void *cpu, dma_addr_t dvma)
  236. {
  237. struct iommu *iommu;
  238. iopte_t *iopte;
  239. unsigned long flags, order, npages;
  240. npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
  241. iommu = dev->archdata.iommu;
  242. iopte = iommu->page_table +
  243. ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  244. spin_lock_irqsave(&iommu->lock, flags);
  245. free_npages(iommu, dvma - iommu->page_table_map_base, npages);
  246. spin_unlock_irqrestore(&iommu->lock, flags);
  247. order = get_order(size);
  248. if (order < 10)
  249. free_pages((unsigned long)cpu, order);
  250. }
  251. static dma_addr_t dma_4u_map_single(struct device *dev, void *ptr, size_t sz,
  252. enum dma_data_direction direction)
  253. {
  254. struct iommu *iommu;
  255. struct strbuf *strbuf;
  256. iopte_t *base;
  257. unsigned long flags, npages, oaddr;
  258. unsigned long i, base_paddr, ctx;
  259. u32 bus_addr, ret;
  260. unsigned long iopte_protection;
  261. iommu = dev->archdata.iommu;
  262. strbuf = dev->archdata.stc;
  263. if (unlikely(direction == DMA_NONE))
  264. goto bad_no_ctx;
  265. oaddr = (unsigned long)ptr;
  266. npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
  267. npages >>= IO_PAGE_SHIFT;
  268. spin_lock_irqsave(&iommu->lock, flags);
  269. base = alloc_npages(iommu, npages);
  270. ctx = 0;
  271. if (iommu->iommu_ctxflush)
  272. ctx = iommu_alloc_ctx(iommu);
  273. spin_unlock_irqrestore(&iommu->lock, flags);
  274. if (unlikely(!base))
  275. goto bad;
  276. bus_addr = (iommu->page_table_map_base +
  277. ((base - iommu->page_table) << IO_PAGE_SHIFT));
  278. ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
  279. base_paddr = __pa(oaddr & IO_PAGE_MASK);
  280. if (strbuf->strbuf_enabled)
  281. iopte_protection = IOPTE_STREAMING(ctx);
  282. else
  283. iopte_protection = IOPTE_CONSISTENT(ctx);
  284. if (direction != DMA_TO_DEVICE)
  285. iopte_protection |= IOPTE_WRITE;
  286. for (i = 0; i < npages; i++, base++, base_paddr += IO_PAGE_SIZE)
  287. iopte_val(*base) = iopte_protection | base_paddr;
  288. return ret;
  289. bad:
  290. iommu_free_ctx(iommu, ctx);
  291. bad_no_ctx:
  292. if (printk_ratelimit())
  293. WARN_ON(1);
  294. return DMA_ERROR_CODE;
  295. }
  296. static void strbuf_flush(struct strbuf *strbuf, struct iommu *iommu,
  297. u32 vaddr, unsigned long ctx, unsigned long npages,
  298. enum dma_data_direction direction)
  299. {
  300. int limit;
  301. if (strbuf->strbuf_ctxflush &&
  302. iommu->iommu_ctxflush) {
  303. unsigned long matchreg, flushreg;
  304. u64 val;
  305. flushreg = strbuf->strbuf_ctxflush;
  306. matchreg = STC_CTXMATCH_ADDR(strbuf, ctx);
  307. iommu_write(flushreg, ctx);
  308. val = iommu_read(matchreg);
  309. val &= 0xffff;
  310. if (!val)
  311. goto do_flush_sync;
  312. while (val) {
  313. if (val & 0x1)
  314. iommu_write(flushreg, ctx);
  315. val >>= 1;
  316. }
  317. val = iommu_read(matchreg);
  318. if (unlikely(val)) {
  319. printk(KERN_WARNING "strbuf_flush: ctx flush "
  320. "timeout matchreg[%lx] ctx[%lx]\n",
  321. val, ctx);
  322. goto do_page_flush;
  323. }
  324. } else {
  325. unsigned long i;
  326. do_page_flush:
  327. for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE)
  328. iommu_write(strbuf->strbuf_pflush, vaddr);
  329. }
  330. do_flush_sync:
  331. /* If the device could not have possibly put dirty data into
  332. * the streaming cache, no flush-flag synchronization needs
  333. * to be performed.
  334. */
  335. if (direction == DMA_TO_DEVICE)
  336. return;
  337. STC_FLUSHFLAG_INIT(strbuf);
  338. iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
  339. (void) iommu_read(iommu->write_complete_reg);
  340. limit = 100000;
  341. while (!STC_FLUSHFLAG_SET(strbuf)) {
  342. limit--;
  343. if (!limit)
  344. break;
  345. udelay(1);
  346. rmb();
  347. }
  348. if (!limit)
  349. printk(KERN_WARNING "strbuf_flush: flushflag timeout "
  350. "vaddr[%08x] ctx[%lx] npages[%ld]\n",
  351. vaddr, ctx, npages);
  352. }
  353. static void dma_4u_unmap_single(struct device *dev, dma_addr_t bus_addr,
  354. size_t sz, enum dma_data_direction direction)
  355. {
  356. struct iommu *iommu;
  357. struct strbuf *strbuf;
  358. iopte_t *base;
  359. unsigned long flags, npages, ctx, i;
  360. if (unlikely(direction == DMA_NONE)) {
  361. if (printk_ratelimit())
  362. WARN_ON(1);
  363. return;
  364. }
  365. iommu = dev->archdata.iommu;
  366. strbuf = dev->archdata.stc;
  367. npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
  368. npages >>= IO_PAGE_SHIFT;
  369. base = iommu->page_table +
  370. ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  371. bus_addr &= IO_PAGE_MASK;
  372. spin_lock_irqsave(&iommu->lock, flags);
  373. /* Record the context, if any. */
  374. ctx = 0;
  375. if (iommu->iommu_ctxflush)
  376. ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
  377. /* Step 1: Kick data out of streaming buffers if necessary. */
  378. if (strbuf->strbuf_enabled)
  379. strbuf_flush(strbuf, iommu, bus_addr, ctx,
  380. npages, direction);
  381. /* Step 2: Clear out TSB entries. */
  382. for (i = 0; i < npages; i++)
  383. iopte_make_dummy(iommu, base + i);
  384. free_npages(iommu, bus_addr - iommu->page_table_map_base, npages);
  385. iommu_free_ctx(iommu, ctx);
  386. spin_unlock_irqrestore(&iommu->lock, flags);
  387. }
  388. #define SG_ENT_PHYS_ADDRESS(SG) \
  389. (__pa(page_address((SG)->page)) + (SG)->offset)
  390. static inline void fill_sg(iopte_t *iopte, struct scatterlist *sg,
  391. int nused, int nelems,
  392. unsigned long iopte_protection)
  393. {
  394. struct scatterlist *dma_sg = sg;
  395. struct scatterlist *sg_end = sg_last(sg, nelems);
  396. int i;
  397. for (i = 0; i < nused; i++) {
  398. unsigned long pteval = ~0UL;
  399. u32 dma_npages;
  400. dma_npages = ((dma_sg->dma_address & (IO_PAGE_SIZE - 1UL)) +
  401. dma_sg->dma_length +
  402. ((IO_PAGE_SIZE - 1UL))) >> IO_PAGE_SHIFT;
  403. do {
  404. unsigned long offset;
  405. signed int len;
  406. /* If we are here, we know we have at least one
  407. * more page to map. So walk forward until we
  408. * hit a page crossing, and begin creating new
  409. * mappings from that spot.
  410. */
  411. for (;;) {
  412. unsigned long tmp;
  413. tmp = SG_ENT_PHYS_ADDRESS(sg);
  414. len = sg->length;
  415. if (((tmp ^ pteval) >> IO_PAGE_SHIFT) != 0UL) {
  416. pteval = tmp & IO_PAGE_MASK;
  417. offset = tmp & (IO_PAGE_SIZE - 1UL);
  418. break;
  419. }
  420. if (((tmp ^ (tmp + len - 1UL)) >> IO_PAGE_SHIFT) != 0UL) {
  421. pteval = (tmp + IO_PAGE_SIZE) & IO_PAGE_MASK;
  422. offset = 0UL;
  423. len -= (IO_PAGE_SIZE - (tmp & (IO_PAGE_SIZE - 1UL)));
  424. break;
  425. }
  426. sg = sg_next(sg);
  427. }
  428. pteval = iopte_protection | (pteval & IOPTE_PAGE);
  429. while (len > 0) {
  430. *iopte++ = __iopte(pteval);
  431. pteval += IO_PAGE_SIZE;
  432. len -= (IO_PAGE_SIZE - offset);
  433. offset = 0;
  434. dma_npages--;
  435. }
  436. pteval = (pteval & IOPTE_PAGE) + len;
  437. sg = sg_next(sg);
  438. /* Skip over any tail mappings we've fully mapped,
  439. * adjusting pteval along the way. Stop when we
  440. * detect a page crossing event.
  441. */
  442. while (sg != sg_end &&
  443. (pteval << (64 - IO_PAGE_SHIFT)) != 0UL &&
  444. (pteval == SG_ENT_PHYS_ADDRESS(sg)) &&
  445. ((pteval ^
  446. (SG_ENT_PHYS_ADDRESS(sg) + sg->length - 1UL)) >> IO_PAGE_SHIFT) == 0UL) {
  447. pteval += sg->length;
  448. sg = sg_next(sg);
  449. }
  450. if ((pteval << (64 - IO_PAGE_SHIFT)) == 0UL)
  451. pteval = ~0UL;
  452. } while (dma_npages != 0);
  453. dma_sg = sg_next(dma_sg);
  454. }
  455. }
  456. static int dma_4u_map_sg(struct device *dev, struct scatterlist *sglist,
  457. int nelems, enum dma_data_direction direction)
  458. {
  459. struct iommu *iommu;
  460. struct strbuf *strbuf;
  461. unsigned long flags, ctx, npages, iopte_protection;
  462. iopte_t *base;
  463. u32 dma_base;
  464. struct scatterlist *sgtmp;
  465. int used;
  466. /* Fast path single entry scatterlists. */
  467. if (nelems == 1) {
  468. sglist->dma_address =
  469. dma_4u_map_single(dev,
  470. (page_address(sglist->page) +
  471. sglist->offset),
  472. sglist->length, direction);
  473. if (unlikely(sglist->dma_address == DMA_ERROR_CODE))
  474. return 0;
  475. sglist->dma_length = sglist->length;
  476. return 1;
  477. }
  478. iommu = dev->archdata.iommu;
  479. strbuf = dev->archdata.stc;
  480. if (unlikely(direction == DMA_NONE))
  481. goto bad_no_ctx;
  482. /* Step 1: Prepare scatter list. */
  483. npages = prepare_sg(sglist, nelems);
  484. /* Step 2: Allocate a cluster and context, if necessary. */
  485. spin_lock_irqsave(&iommu->lock, flags);
  486. base = alloc_npages(iommu, npages);
  487. ctx = 0;
  488. if (iommu->iommu_ctxflush)
  489. ctx = iommu_alloc_ctx(iommu);
  490. spin_unlock_irqrestore(&iommu->lock, flags);
  491. if (base == NULL)
  492. goto bad;
  493. dma_base = iommu->page_table_map_base +
  494. ((base - iommu->page_table) << IO_PAGE_SHIFT);
  495. /* Step 3: Normalize DMA addresses. */
  496. used = nelems;
  497. sgtmp = sglist;
  498. while (used && sgtmp->dma_length) {
  499. sgtmp->dma_address += dma_base;
  500. sgtmp = sg_next(sgtmp);
  501. used--;
  502. }
  503. used = nelems - used;
  504. /* Step 4: Create the mappings. */
  505. if (strbuf->strbuf_enabled)
  506. iopte_protection = IOPTE_STREAMING(ctx);
  507. else
  508. iopte_protection = IOPTE_CONSISTENT(ctx);
  509. if (direction != DMA_TO_DEVICE)
  510. iopte_protection |= IOPTE_WRITE;
  511. fill_sg(base, sglist, used, nelems, iopte_protection);
  512. #ifdef VERIFY_SG
  513. verify_sglist(sglist, nelems, base, npages);
  514. #endif
  515. return used;
  516. bad:
  517. iommu_free_ctx(iommu, ctx);
  518. bad_no_ctx:
  519. if (printk_ratelimit())
  520. WARN_ON(1);
  521. return 0;
  522. }
  523. static void dma_4u_unmap_sg(struct device *dev, struct scatterlist *sglist,
  524. int nelems, enum dma_data_direction direction)
  525. {
  526. struct iommu *iommu;
  527. struct strbuf *strbuf;
  528. iopte_t *base;
  529. unsigned long flags, ctx, i, npages;
  530. struct scatterlist *sg, *sgprv;
  531. u32 bus_addr;
  532. if (unlikely(direction == DMA_NONE)) {
  533. if (printk_ratelimit())
  534. WARN_ON(1);
  535. }
  536. iommu = dev->archdata.iommu;
  537. strbuf = dev->archdata.stc;
  538. bus_addr = sglist->dma_address & IO_PAGE_MASK;
  539. sgprv = NULL;
  540. for_each_sg(sglist, sg, nelems, i) {
  541. if (sg->dma_length == 0)
  542. break;
  543. sgprv = sg;
  544. }
  545. npages = (IO_PAGE_ALIGN(sgprv->dma_address + sgprv->dma_length) -
  546. bus_addr) >> IO_PAGE_SHIFT;
  547. base = iommu->page_table +
  548. ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  549. spin_lock_irqsave(&iommu->lock, flags);
  550. /* Record the context, if any. */
  551. ctx = 0;
  552. if (iommu->iommu_ctxflush)
  553. ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
  554. /* Step 1: Kick data out of streaming buffers if necessary. */
  555. if (strbuf->strbuf_enabled)
  556. strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
  557. /* Step 2: Clear out the TSB entries. */
  558. for (i = 0; i < npages; i++)
  559. iopte_make_dummy(iommu, base + i);
  560. free_npages(iommu, bus_addr - iommu->page_table_map_base, npages);
  561. iommu_free_ctx(iommu, ctx);
  562. spin_unlock_irqrestore(&iommu->lock, flags);
  563. }
  564. static void dma_4u_sync_single_for_cpu(struct device *dev,
  565. dma_addr_t bus_addr, size_t sz,
  566. enum dma_data_direction direction)
  567. {
  568. struct iommu *iommu;
  569. struct strbuf *strbuf;
  570. unsigned long flags, ctx, npages;
  571. iommu = dev->archdata.iommu;
  572. strbuf = dev->archdata.stc;
  573. if (!strbuf->strbuf_enabled)
  574. return;
  575. spin_lock_irqsave(&iommu->lock, flags);
  576. npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
  577. npages >>= IO_PAGE_SHIFT;
  578. bus_addr &= IO_PAGE_MASK;
  579. /* Step 1: Record the context, if any. */
  580. ctx = 0;
  581. if (iommu->iommu_ctxflush &&
  582. strbuf->strbuf_ctxflush) {
  583. iopte_t *iopte;
  584. iopte = iommu->page_table +
  585. ((bus_addr - iommu->page_table_map_base)>>IO_PAGE_SHIFT);
  586. ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
  587. }
  588. /* Step 2: Kick data out of streaming buffers. */
  589. strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
  590. spin_unlock_irqrestore(&iommu->lock, flags);
  591. }
  592. static void dma_4u_sync_sg_for_cpu(struct device *dev,
  593. struct scatterlist *sglist, int nelems,
  594. enum dma_data_direction direction)
  595. {
  596. struct iommu *iommu;
  597. struct strbuf *strbuf;
  598. unsigned long flags, ctx, npages, i;
  599. struct scatterlist *sg, *sgprv;
  600. u32 bus_addr;
  601. iommu = dev->archdata.iommu;
  602. strbuf = dev->archdata.stc;
  603. if (!strbuf->strbuf_enabled)
  604. return;
  605. spin_lock_irqsave(&iommu->lock, flags);
  606. /* Step 1: Record the context, if any. */
  607. ctx = 0;
  608. if (iommu->iommu_ctxflush &&
  609. strbuf->strbuf_ctxflush) {
  610. iopte_t *iopte;
  611. iopte = iommu->page_table +
  612. ((sglist[0].dma_address - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  613. ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
  614. }
  615. /* Step 2: Kick data out of streaming buffers. */
  616. bus_addr = sglist[0].dma_address & IO_PAGE_MASK;
  617. sgprv = NULL;
  618. for_each_sg(sglist, sg, nelems, i) {
  619. if (sg->dma_length == 0)
  620. break;
  621. sgprv = sg;
  622. }
  623. npages = (IO_PAGE_ALIGN(sgprv->dma_address + sgprv->dma_length)
  624. - bus_addr) >> IO_PAGE_SHIFT;
  625. strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
  626. spin_unlock_irqrestore(&iommu->lock, flags);
  627. }
  628. const struct dma_ops sun4u_dma_ops = {
  629. .alloc_coherent = dma_4u_alloc_coherent,
  630. .free_coherent = dma_4u_free_coherent,
  631. .map_single = dma_4u_map_single,
  632. .unmap_single = dma_4u_unmap_single,
  633. .map_sg = dma_4u_map_sg,
  634. .unmap_sg = dma_4u_unmap_sg,
  635. .sync_single_for_cpu = dma_4u_sync_single_for_cpu,
  636. .sync_sg_for_cpu = dma_4u_sync_sg_for_cpu,
  637. };
  638. const struct dma_ops *dma_ops = &sun4u_dma_ops;
  639. EXPORT_SYMBOL(dma_ops);
  640. int dma_supported(struct device *dev, u64 device_mask)
  641. {
  642. struct iommu *iommu = dev->archdata.iommu;
  643. u64 dma_addr_mask = iommu->dma_addr_mask;
  644. if (device_mask >= (1UL << 32UL))
  645. return 0;
  646. if ((device_mask & dma_addr_mask) == dma_addr_mask)
  647. return 1;
  648. #ifdef CONFIG_PCI
  649. if (dev->bus == &pci_bus_type)
  650. return pci_dma_supported(to_pci_dev(dev), device_mask);
  651. #endif
  652. return 0;
  653. }
  654. EXPORT_SYMBOL(dma_supported);
  655. int dma_set_mask(struct device *dev, u64 dma_mask)
  656. {
  657. #ifdef CONFIG_PCI
  658. if (dev->bus == &pci_bus_type)
  659. return pci_set_dma_mask(to_pci_dev(dev), dma_mask);
  660. #endif
  661. return -EINVAL;
  662. }
  663. EXPORT_SYMBOL(dma_set_mask);