iommu.c 19 KB

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