iommu.c 19 KB

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