pci_iommu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. /* $Id: pci_iommu.c,v 1.17 2001/12/17 07:05:09 davem Exp $
  2. * pci_iommu.c: UltraSparc PCI controller IOM/STC support.
  3. *
  4. * Copyright (C) 1999 David S. Miller (davem@redhat.com)
  5. * Copyright (C) 1999, 2000 Jakub Jelinek (jakub@redhat.com)
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/sched.h>
  9. #include <linux/mm.h>
  10. #include <linux/delay.h>
  11. #include <asm/pbm.h>
  12. #include "iommu_common.h"
  13. #define PCI_STC_CTXMATCH_ADDR(STC, CTX) \
  14. ((STC)->strbuf_ctxmatch_base + ((CTX) << 3))
  15. /* Accessing IOMMU and Streaming Buffer registers.
  16. * REG parameter is a physical address. All registers
  17. * are 64-bits in size.
  18. */
  19. #define pci_iommu_read(__reg) \
  20. ({ u64 __ret; \
  21. __asm__ __volatile__("ldxa [%1] %2, %0" \
  22. : "=r" (__ret) \
  23. : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
  24. : "memory"); \
  25. __ret; \
  26. })
  27. #define pci_iommu_write(__reg, __val) \
  28. __asm__ __volatile__("stxa %0, [%1] %2" \
  29. : /* no outputs */ \
  30. : "r" (__val), "r" (__reg), \
  31. "i" (ASI_PHYS_BYPASS_EC_E))
  32. /* Must be invoked under the IOMMU lock. */
  33. static void __iommu_flushall(struct pci_iommu *iommu)
  34. {
  35. unsigned long tag;
  36. int entry;
  37. tag = iommu->iommu_flush + (0xa580UL - 0x0210UL);
  38. for (entry = 0; entry < 16; entry++) {
  39. pci_iommu_write(tag, 0);
  40. tag += 8;
  41. }
  42. /* Ensure completion of previous PIO writes. */
  43. (void) pci_iommu_read(iommu->write_complete_reg);
  44. }
  45. #define IOPTE_CONSISTENT(CTX) \
  46. (IOPTE_VALID | IOPTE_CACHE | \
  47. (((CTX) << 47) & IOPTE_CONTEXT))
  48. #define IOPTE_STREAMING(CTX) \
  49. (IOPTE_CONSISTENT(CTX) | IOPTE_STBUF)
  50. /* Existing mappings are never marked invalid, instead they
  51. * are pointed to a dummy page.
  52. */
  53. #define IOPTE_IS_DUMMY(iommu, iopte) \
  54. ((iopte_val(*iopte) & IOPTE_PAGE) == (iommu)->dummy_page_pa)
  55. static inline void iopte_make_dummy(struct pci_iommu *iommu, iopte_t *iopte)
  56. {
  57. unsigned long val = iopte_val(*iopte);
  58. val &= ~IOPTE_PAGE;
  59. val |= iommu->dummy_page_pa;
  60. iopte_val(*iopte) = val;
  61. }
  62. /* Based largely upon the ppc64 iommu allocator. */
  63. static long pci_arena_alloc(struct pci_iommu *iommu, unsigned long npages)
  64. {
  65. struct pci_iommu_arena *arena = &iommu->arena;
  66. unsigned long n, i, start, end, limit;
  67. int pass;
  68. limit = arena->limit;
  69. start = arena->hint;
  70. pass = 0;
  71. again:
  72. n = find_next_zero_bit(arena->map, limit, start);
  73. end = n + npages;
  74. if (unlikely(end >= limit)) {
  75. if (likely(pass < 1)) {
  76. limit = start;
  77. start = 0;
  78. __iommu_flushall(iommu);
  79. pass++;
  80. goto again;
  81. } else {
  82. /* Scanned the whole thing, give up. */
  83. return -1;
  84. }
  85. }
  86. for (i = n; i < end; i++) {
  87. if (test_bit(i, arena->map)) {
  88. start = i + 1;
  89. goto again;
  90. }
  91. }
  92. for (i = n; i < end; i++)
  93. __set_bit(i, arena->map);
  94. arena->hint = end;
  95. return n;
  96. }
  97. static void pci_arena_free(struct pci_iommu_arena *arena, unsigned long base, unsigned long npages)
  98. {
  99. unsigned long i;
  100. for (i = base; i < (base + npages); i++)
  101. __clear_bit(i, arena->map);
  102. }
  103. void pci_iommu_table_init(struct pci_iommu *iommu, int tsbsize, u32 dma_offset, u32 dma_addr_mask)
  104. {
  105. unsigned long i, tsbbase, order, sz, num_tsb_entries;
  106. num_tsb_entries = tsbsize / sizeof(iopte_t);
  107. /* Setup initial software IOMMU state. */
  108. spin_lock_init(&iommu->lock);
  109. iommu->ctx_lowest_free = 1;
  110. iommu->page_table_map_base = dma_offset;
  111. iommu->dma_addr_mask = dma_addr_mask;
  112. /* Allocate and initialize the free area map. */
  113. sz = num_tsb_entries / 8;
  114. sz = (sz + 7UL) & ~7UL;
  115. iommu->arena.map = kzalloc(sz, GFP_KERNEL);
  116. if (!iommu->arena.map) {
  117. prom_printf("PCI_IOMMU: Error, kmalloc(arena.map) failed.\n");
  118. prom_halt();
  119. }
  120. iommu->arena.limit = num_tsb_entries;
  121. /* Allocate and initialize the dummy page which we
  122. * set inactive IO PTEs to point to.
  123. */
  124. iommu->dummy_page = __get_free_pages(GFP_KERNEL, 0);
  125. if (!iommu->dummy_page) {
  126. prom_printf("PCI_IOMMU: Error, gfp(dummy_page) failed.\n");
  127. prom_halt();
  128. }
  129. memset((void *)iommu->dummy_page, 0, PAGE_SIZE);
  130. iommu->dummy_page_pa = (unsigned long) __pa(iommu->dummy_page);
  131. /* Now allocate and setup the IOMMU page table itself. */
  132. order = get_order(tsbsize);
  133. tsbbase = __get_free_pages(GFP_KERNEL, order);
  134. if (!tsbbase) {
  135. prom_printf("PCI_IOMMU: Error, gfp(tsb) failed.\n");
  136. prom_halt();
  137. }
  138. iommu->page_table = (iopte_t *)tsbbase;
  139. for (i = 0; i < num_tsb_entries; i++)
  140. iopte_make_dummy(iommu, &iommu->page_table[i]);
  141. }
  142. static inline iopte_t *alloc_npages(struct pci_iommu *iommu, unsigned long npages)
  143. {
  144. long entry;
  145. entry = pci_arena_alloc(iommu, npages);
  146. if (unlikely(entry < 0))
  147. return NULL;
  148. return iommu->page_table + entry;
  149. }
  150. static inline void free_npages(struct pci_iommu *iommu, dma_addr_t base, unsigned long npages)
  151. {
  152. pci_arena_free(&iommu->arena, base >> IO_PAGE_SHIFT, npages);
  153. }
  154. static int iommu_alloc_ctx(struct pci_iommu *iommu)
  155. {
  156. int lowest = iommu->ctx_lowest_free;
  157. int sz = IOMMU_NUM_CTXS - lowest;
  158. int n = find_next_zero_bit(iommu->ctx_bitmap, sz, lowest);
  159. if (unlikely(n == sz)) {
  160. n = find_next_zero_bit(iommu->ctx_bitmap, lowest, 1);
  161. if (unlikely(n == lowest)) {
  162. printk(KERN_WARNING "IOMMU: Ran out of contexts.\n");
  163. n = 0;
  164. }
  165. }
  166. if (n)
  167. __set_bit(n, iommu->ctx_bitmap);
  168. return n;
  169. }
  170. static inline void iommu_free_ctx(struct pci_iommu *iommu, int ctx)
  171. {
  172. if (likely(ctx)) {
  173. __clear_bit(ctx, iommu->ctx_bitmap);
  174. if (ctx < iommu->ctx_lowest_free)
  175. iommu->ctx_lowest_free = ctx;
  176. }
  177. }
  178. /* Allocate and map kernel buffer of size SIZE using consistent mode
  179. * DMA for PCI device PDEV. Return non-NULL cpu-side address if
  180. * successful and set *DMA_ADDRP to the PCI side dma address.
  181. */
  182. static void *pci_4u_alloc_consistent(struct pci_dev *pdev, size_t size, dma_addr_t *dma_addrp, gfp_t gfp)
  183. {
  184. struct pci_iommu *iommu;
  185. iopte_t *iopte;
  186. unsigned long flags, order, first_page;
  187. void *ret;
  188. int npages;
  189. size = IO_PAGE_ALIGN(size);
  190. order = get_order(size);
  191. if (order >= 10)
  192. return NULL;
  193. first_page = __get_free_pages(gfp, order);
  194. if (first_page == 0UL)
  195. return NULL;
  196. memset((char *)first_page, 0, PAGE_SIZE << order);
  197. iommu = pdev->dev.archdata.iommu;
  198. spin_lock_irqsave(&iommu->lock, flags);
  199. iopte = alloc_npages(iommu, size >> IO_PAGE_SHIFT);
  200. spin_unlock_irqrestore(&iommu->lock, flags);
  201. if (unlikely(iopte == NULL)) {
  202. free_pages(first_page, order);
  203. return NULL;
  204. }
  205. *dma_addrp = (iommu->page_table_map_base +
  206. ((iopte - iommu->page_table) << IO_PAGE_SHIFT));
  207. ret = (void *) first_page;
  208. npages = size >> IO_PAGE_SHIFT;
  209. first_page = __pa(first_page);
  210. while (npages--) {
  211. iopte_val(*iopte) = (IOPTE_CONSISTENT(0UL) |
  212. IOPTE_WRITE |
  213. (first_page & IOPTE_PAGE));
  214. iopte++;
  215. first_page += IO_PAGE_SIZE;
  216. }
  217. return ret;
  218. }
  219. /* Free and unmap a consistent DMA translation. */
  220. static void pci_4u_free_consistent(struct pci_dev *pdev, size_t size, void *cpu, dma_addr_t dvma)
  221. {
  222. struct pci_iommu *iommu;
  223. iopte_t *iopte;
  224. unsigned long flags, order, npages;
  225. npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
  226. iommu = pdev->dev.archdata.iommu;
  227. iopte = iommu->page_table +
  228. ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  229. spin_lock_irqsave(&iommu->lock, flags);
  230. free_npages(iommu, dvma - iommu->page_table_map_base, npages);
  231. spin_unlock_irqrestore(&iommu->lock, flags);
  232. order = get_order(size);
  233. if (order < 10)
  234. free_pages((unsigned long)cpu, order);
  235. }
  236. /* Map a single buffer at PTR of SZ bytes for PCI DMA
  237. * in streaming mode.
  238. */
  239. static dma_addr_t pci_4u_map_single(struct pci_dev *pdev, void *ptr, size_t sz, int direction)
  240. {
  241. struct pci_iommu *iommu;
  242. struct pci_strbuf *strbuf;
  243. iopte_t *base;
  244. unsigned long flags, npages, oaddr;
  245. unsigned long i, base_paddr, ctx;
  246. u32 bus_addr, ret;
  247. unsigned long iopte_protection;
  248. iommu = pdev->dev.archdata.iommu;
  249. strbuf = pdev->dev.archdata.stc;
  250. if (unlikely(direction == PCI_DMA_NONE))
  251. goto bad_no_ctx;
  252. oaddr = (unsigned long)ptr;
  253. npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
  254. npages >>= IO_PAGE_SHIFT;
  255. spin_lock_irqsave(&iommu->lock, flags);
  256. base = alloc_npages(iommu, npages);
  257. ctx = 0;
  258. if (iommu->iommu_ctxflush)
  259. ctx = iommu_alloc_ctx(iommu);
  260. spin_unlock_irqrestore(&iommu->lock, flags);
  261. if (unlikely(!base))
  262. goto bad;
  263. bus_addr = (iommu->page_table_map_base +
  264. ((base - iommu->page_table) << IO_PAGE_SHIFT));
  265. ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
  266. base_paddr = __pa(oaddr & IO_PAGE_MASK);
  267. if (strbuf->strbuf_enabled)
  268. iopte_protection = IOPTE_STREAMING(ctx);
  269. else
  270. iopte_protection = IOPTE_CONSISTENT(ctx);
  271. if (direction != PCI_DMA_TODEVICE)
  272. iopte_protection |= IOPTE_WRITE;
  273. for (i = 0; i < npages; i++, base++, base_paddr += IO_PAGE_SIZE)
  274. iopte_val(*base) = iopte_protection | base_paddr;
  275. return ret;
  276. bad:
  277. iommu_free_ctx(iommu, ctx);
  278. bad_no_ctx:
  279. if (printk_ratelimit())
  280. WARN_ON(1);
  281. return PCI_DMA_ERROR_CODE;
  282. }
  283. static void pci_strbuf_flush(struct pci_strbuf *strbuf, struct pci_iommu *iommu, u32 vaddr, unsigned long ctx, unsigned long npages, int direction)
  284. {
  285. int limit;
  286. if (strbuf->strbuf_ctxflush &&
  287. iommu->iommu_ctxflush) {
  288. unsigned long matchreg, flushreg;
  289. u64 val;
  290. flushreg = strbuf->strbuf_ctxflush;
  291. matchreg = PCI_STC_CTXMATCH_ADDR(strbuf, ctx);
  292. pci_iommu_write(flushreg, ctx);
  293. val = pci_iommu_read(matchreg);
  294. val &= 0xffff;
  295. if (!val)
  296. goto do_flush_sync;
  297. while (val) {
  298. if (val & 0x1)
  299. pci_iommu_write(flushreg, ctx);
  300. val >>= 1;
  301. }
  302. val = pci_iommu_read(matchreg);
  303. if (unlikely(val)) {
  304. printk(KERN_WARNING "pci_strbuf_flush: ctx flush "
  305. "timeout matchreg[%lx] ctx[%lx]\n",
  306. val, ctx);
  307. goto do_page_flush;
  308. }
  309. } else {
  310. unsigned long i;
  311. do_page_flush:
  312. for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE)
  313. pci_iommu_write(strbuf->strbuf_pflush, vaddr);
  314. }
  315. do_flush_sync:
  316. /* If the device could not have possibly put dirty data into
  317. * the streaming cache, no flush-flag synchronization needs
  318. * to be performed.
  319. */
  320. if (direction == PCI_DMA_TODEVICE)
  321. return;
  322. PCI_STC_FLUSHFLAG_INIT(strbuf);
  323. pci_iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
  324. (void) pci_iommu_read(iommu->write_complete_reg);
  325. limit = 100000;
  326. while (!PCI_STC_FLUSHFLAG_SET(strbuf)) {
  327. limit--;
  328. if (!limit)
  329. break;
  330. udelay(1);
  331. rmb();
  332. }
  333. if (!limit)
  334. printk(KERN_WARNING "pci_strbuf_flush: flushflag timeout "
  335. "vaddr[%08x] ctx[%lx] npages[%ld]\n",
  336. vaddr, ctx, npages);
  337. }
  338. /* Unmap a single streaming mode DMA translation. */
  339. static void pci_4u_unmap_single(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
  340. {
  341. struct pci_iommu *iommu;
  342. struct pci_strbuf *strbuf;
  343. iopte_t *base;
  344. unsigned long flags, npages, ctx, i;
  345. if (unlikely(direction == PCI_DMA_NONE)) {
  346. if (printk_ratelimit())
  347. WARN_ON(1);
  348. return;
  349. }
  350. iommu = pdev->dev.archdata.iommu;
  351. strbuf = pdev->dev.archdata.stc;
  352. npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
  353. npages >>= IO_PAGE_SHIFT;
  354. base = iommu->page_table +
  355. ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  356. #ifdef DEBUG_PCI_IOMMU
  357. if (IOPTE_IS_DUMMY(iommu, base))
  358. printk("pci_unmap_single called on non-mapped region %08x,%08x from %016lx\n",
  359. bus_addr, sz, __builtin_return_address(0));
  360. #endif
  361. bus_addr &= IO_PAGE_MASK;
  362. spin_lock_irqsave(&iommu->lock, flags);
  363. /* Record the context, if any. */
  364. ctx = 0;
  365. if (iommu->iommu_ctxflush)
  366. ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
  367. /* Step 1: Kick data out of streaming buffers if necessary. */
  368. if (strbuf->strbuf_enabled)
  369. pci_strbuf_flush(strbuf, iommu, bus_addr, ctx,
  370. npages, direction);
  371. /* Step 2: Clear out TSB entries. */
  372. for (i = 0; i < npages; i++)
  373. iopte_make_dummy(iommu, base + i);
  374. free_npages(iommu, bus_addr - iommu->page_table_map_base, npages);
  375. iommu_free_ctx(iommu, ctx);
  376. spin_unlock_irqrestore(&iommu->lock, flags);
  377. }
  378. #define SG_ENT_PHYS_ADDRESS(SG) \
  379. (__pa(page_address((SG)->page)) + (SG)->offset)
  380. static inline void fill_sg(iopte_t *iopte, struct scatterlist *sg,
  381. int nused, int nelems, unsigned long iopte_protection)
  382. {
  383. struct scatterlist *dma_sg = sg;
  384. struct scatterlist *sg_end = sg + nelems;
  385. int i;
  386. for (i = 0; i < nused; i++) {
  387. unsigned long pteval = ~0UL;
  388. u32 dma_npages;
  389. dma_npages = ((dma_sg->dma_address & (IO_PAGE_SIZE - 1UL)) +
  390. dma_sg->dma_length +
  391. ((IO_PAGE_SIZE - 1UL))) >> IO_PAGE_SHIFT;
  392. do {
  393. unsigned long offset;
  394. signed int len;
  395. /* If we are here, we know we have at least one
  396. * more page to map. So walk forward until we
  397. * hit a page crossing, and begin creating new
  398. * mappings from that spot.
  399. */
  400. for (;;) {
  401. unsigned long tmp;
  402. tmp = SG_ENT_PHYS_ADDRESS(sg);
  403. len = sg->length;
  404. if (((tmp ^ pteval) >> IO_PAGE_SHIFT) != 0UL) {
  405. pteval = tmp & IO_PAGE_MASK;
  406. offset = tmp & (IO_PAGE_SIZE - 1UL);
  407. break;
  408. }
  409. if (((tmp ^ (tmp + len - 1UL)) >> IO_PAGE_SHIFT) != 0UL) {
  410. pteval = (tmp + IO_PAGE_SIZE) & IO_PAGE_MASK;
  411. offset = 0UL;
  412. len -= (IO_PAGE_SIZE - (tmp & (IO_PAGE_SIZE - 1UL)));
  413. break;
  414. }
  415. sg++;
  416. }
  417. pteval = iopte_protection | (pteval & IOPTE_PAGE);
  418. while (len > 0) {
  419. *iopte++ = __iopte(pteval);
  420. pteval += IO_PAGE_SIZE;
  421. len -= (IO_PAGE_SIZE - offset);
  422. offset = 0;
  423. dma_npages--;
  424. }
  425. pteval = (pteval & IOPTE_PAGE) + len;
  426. sg++;
  427. /* Skip over any tail mappings we've fully mapped,
  428. * adjusting pteval along the way. Stop when we
  429. * detect a page crossing event.
  430. */
  431. while (sg < sg_end &&
  432. (pteval << (64 - IO_PAGE_SHIFT)) != 0UL &&
  433. (pteval == SG_ENT_PHYS_ADDRESS(sg)) &&
  434. ((pteval ^
  435. (SG_ENT_PHYS_ADDRESS(sg) + sg->length - 1UL)) >> IO_PAGE_SHIFT) == 0UL) {
  436. pteval += sg->length;
  437. sg++;
  438. }
  439. if ((pteval << (64 - IO_PAGE_SHIFT)) == 0UL)
  440. pteval = ~0UL;
  441. } while (dma_npages != 0);
  442. dma_sg++;
  443. }
  444. }
  445. /* Map a set of buffers described by SGLIST with NELEMS array
  446. * elements in streaming mode for PCI DMA.
  447. * When making changes here, inspect the assembly output. I was having
  448. * hard time to kepp this routine out of using stack slots for holding variables.
  449. */
  450. static int pci_4u_map_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
  451. {
  452. struct pci_iommu *iommu;
  453. struct pci_strbuf *strbuf;
  454. unsigned long flags, ctx, npages, iopte_protection;
  455. iopte_t *base;
  456. u32 dma_base;
  457. struct scatterlist *sgtmp;
  458. int used;
  459. /* Fast path single entry scatterlists. */
  460. if (nelems == 1) {
  461. sglist->dma_address =
  462. pci_4u_map_single(pdev,
  463. (page_address(sglist->page) + sglist->offset),
  464. sglist->length, direction);
  465. if (unlikely(sglist->dma_address == PCI_DMA_ERROR_CODE))
  466. return 0;
  467. sglist->dma_length = sglist->length;
  468. return 1;
  469. }
  470. iommu = pdev->dev.archdata.iommu;
  471. strbuf = pdev->dev.archdata.stc;
  472. if (unlikely(direction == PCI_DMA_NONE))
  473. goto bad_no_ctx;
  474. /* Step 1: Prepare scatter list. */
  475. npages = prepare_sg(sglist, nelems);
  476. /* Step 2: Allocate a cluster and context, if necessary. */
  477. spin_lock_irqsave(&iommu->lock, flags);
  478. base = alloc_npages(iommu, npages);
  479. ctx = 0;
  480. if (iommu->iommu_ctxflush)
  481. ctx = iommu_alloc_ctx(iommu);
  482. spin_unlock_irqrestore(&iommu->lock, flags);
  483. if (base == NULL)
  484. goto bad;
  485. dma_base = iommu->page_table_map_base +
  486. ((base - iommu->page_table) << IO_PAGE_SHIFT);
  487. /* Step 3: Normalize DMA addresses. */
  488. used = nelems;
  489. sgtmp = sglist;
  490. while (used && sgtmp->dma_length) {
  491. sgtmp->dma_address += dma_base;
  492. sgtmp++;
  493. used--;
  494. }
  495. used = nelems - used;
  496. /* Step 4: Create the mappings. */
  497. if (strbuf->strbuf_enabled)
  498. iopte_protection = IOPTE_STREAMING(ctx);
  499. else
  500. iopte_protection = IOPTE_CONSISTENT(ctx);
  501. if (direction != PCI_DMA_TODEVICE)
  502. iopte_protection |= IOPTE_WRITE;
  503. fill_sg(base, sglist, used, nelems, iopte_protection);
  504. #ifdef VERIFY_SG
  505. verify_sglist(sglist, nelems, base, npages);
  506. #endif
  507. return used;
  508. bad:
  509. iommu_free_ctx(iommu, ctx);
  510. bad_no_ctx:
  511. if (printk_ratelimit())
  512. WARN_ON(1);
  513. return 0;
  514. }
  515. /* Unmap a set of streaming mode DMA translations. */
  516. static void pci_4u_unmap_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
  517. {
  518. struct pci_iommu *iommu;
  519. struct pci_strbuf *strbuf;
  520. iopte_t *base;
  521. unsigned long flags, ctx, i, npages;
  522. u32 bus_addr;
  523. if (unlikely(direction == PCI_DMA_NONE)) {
  524. if (printk_ratelimit())
  525. WARN_ON(1);
  526. }
  527. iommu = pdev->dev.archdata.iommu;
  528. strbuf = pdev->dev.archdata.stc;
  529. bus_addr = sglist->dma_address & IO_PAGE_MASK;
  530. for (i = 1; i < nelems; i++)
  531. if (sglist[i].dma_length == 0)
  532. break;
  533. i--;
  534. npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length) -
  535. bus_addr) >> IO_PAGE_SHIFT;
  536. base = iommu->page_table +
  537. ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  538. #ifdef DEBUG_PCI_IOMMU
  539. if (IOPTE_IS_DUMMY(iommu, base))
  540. printk("pci_unmap_sg called on non-mapped region %016lx,%d from %016lx\n", sglist->dma_address, nelems, __builtin_return_address(0));
  541. #endif
  542. spin_lock_irqsave(&iommu->lock, flags);
  543. /* Record the context, if any. */
  544. ctx = 0;
  545. if (iommu->iommu_ctxflush)
  546. ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
  547. /* Step 1: Kick data out of streaming buffers if necessary. */
  548. if (strbuf->strbuf_enabled)
  549. pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
  550. /* Step 2: Clear out the TSB entries. */
  551. for (i = 0; i < npages; i++)
  552. iopte_make_dummy(iommu, base + i);
  553. free_npages(iommu, bus_addr - iommu->page_table_map_base, npages);
  554. iommu_free_ctx(iommu, ctx);
  555. spin_unlock_irqrestore(&iommu->lock, flags);
  556. }
  557. /* Make physical memory consistent for a single
  558. * streaming mode DMA translation after a transfer.
  559. */
  560. static void pci_4u_dma_sync_single_for_cpu(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
  561. {
  562. struct pci_iommu *iommu;
  563. struct pci_strbuf *strbuf;
  564. unsigned long flags, ctx, npages;
  565. iommu = pdev->dev.archdata.iommu;
  566. strbuf = pdev->dev.archdata.stc;
  567. if (!strbuf->strbuf_enabled)
  568. return;
  569. spin_lock_irqsave(&iommu->lock, flags);
  570. npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
  571. npages >>= IO_PAGE_SHIFT;
  572. bus_addr &= IO_PAGE_MASK;
  573. /* Step 1: Record the context, if any. */
  574. ctx = 0;
  575. if (iommu->iommu_ctxflush &&
  576. strbuf->strbuf_ctxflush) {
  577. iopte_t *iopte;
  578. iopte = iommu->page_table +
  579. ((bus_addr - iommu->page_table_map_base)>>IO_PAGE_SHIFT);
  580. ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
  581. }
  582. /* Step 2: Kick data out of streaming buffers. */
  583. pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
  584. spin_unlock_irqrestore(&iommu->lock, flags);
  585. }
  586. /* Make physical memory consistent for a set of streaming
  587. * mode DMA translations after a transfer.
  588. */
  589. static void pci_4u_dma_sync_sg_for_cpu(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
  590. {
  591. struct pci_iommu *iommu;
  592. struct pci_strbuf *strbuf;
  593. unsigned long flags, ctx, npages, i;
  594. u32 bus_addr;
  595. iommu = pdev->dev.archdata.iommu;
  596. strbuf = pdev->dev.archdata.stc;
  597. if (!strbuf->strbuf_enabled)
  598. return;
  599. spin_lock_irqsave(&iommu->lock, flags);
  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. ((sglist[0].dma_address - 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. bus_addr = sglist[0].dma_address & IO_PAGE_MASK;
  611. for(i = 1; i < nelems; i++)
  612. if (!sglist[i].dma_length)
  613. break;
  614. i--;
  615. npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length)
  616. - bus_addr) >> IO_PAGE_SHIFT;
  617. pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
  618. spin_unlock_irqrestore(&iommu->lock, flags);
  619. }
  620. const struct pci_iommu_ops pci_sun4u_iommu_ops = {
  621. .alloc_consistent = pci_4u_alloc_consistent,
  622. .free_consistent = pci_4u_free_consistent,
  623. .map_single = pci_4u_map_single,
  624. .unmap_single = pci_4u_unmap_single,
  625. .map_sg = pci_4u_map_sg,
  626. .unmap_sg = pci_4u_unmap_sg,
  627. .dma_sync_single_for_cpu = pci_4u_dma_sync_single_for_cpu,
  628. .dma_sync_sg_for_cpu = pci_4u_dma_sync_sg_for_cpu,
  629. };
  630. static void ali_sound_dma_hack(struct pci_dev *pdev, int set_bit)
  631. {
  632. struct pci_dev *ali_isa_bridge;
  633. u8 val;
  634. /* ALI sound chips generate 31-bits of DMA, a special register
  635. * determines what bit 31 is emitted as.
  636. */
  637. ali_isa_bridge = pci_get_device(PCI_VENDOR_ID_AL,
  638. PCI_DEVICE_ID_AL_M1533,
  639. NULL);
  640. pci_read_config_byte(ali_isa_bridge, 0x7e, &val);
  641. if (set_bit)
  642. val |= 0x01;
  643. else
  644. val &= ~0x01;
  645. pci_write_config_byte(ali_isa_bridge, 0x7e, val);
  646. pci_dev_put(ali_isa_bridge);
  647. }
  648. int pci_dma_supported(struct pci_dev *pdev, u64 device_mask)
  649. {
  650. u64 dma_addr_mask;
  651. if (pdev == NULL) {
  652. dma_addr_mask = 0xffffffff;
  653. } else {
  654. struct pci_iommu *iommu = pdev->dev.archdata.iommu;
  655. dma_addr_mask = iommu->dma_addr_mask;
  656. if (pdev->vendor == PCI_VENDOR_ID_AL &&
  657. pdev->device == PCI_DEVICE_ID_AL_M5451 &&
  658. device_mask == 0x7fffffff) {
  659. ali_sound_dma_hack(pdev,
  660. (dma_addr_mask & 0x80000000) != 0);
  661. return 1;
  662. }
  663. }
  664. if (device_mask >= (1UL << 32UL))
  665. return 0;
  666. return (device_mask & dma_addr_mask) == dma_addr_mask;
  667. }