pci_iommu.c 21 KB

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