pci_iommu.c 21 KB

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