pci_iommu.c 21 KB

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