pci_iommu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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 void inline 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 pcidev_cookie *pcp;
  185. struct pci_iommu *iommu;
  186. iopte_t *iopte;
  187. unsigned long flags, order, first_page;
  188. void *ret;
  189. int npages;
  190. size = IO_PAGE_ALIGN(size);
  191. order = get_order(size);
  192. if (order >= 10)
  193. return NULL;
  194. first_page = __get_free_pages(gfp, order);
  195. if (first_page == 0UL)
  196. return NULL;
  197. memset((char *)first_page, 0, PAGE_SIZE << order);
  198. pcp = pdev->sysdata;
  199. iommu = pcp->pbm->iommu;
  200. spin_lock_irqsave(&iommu->lock, flags);
  201. iopte = alloc_npages(iommu, size >> IO_PAGE_SHIFT);
  202. spin_unlock_irqrestore(&iommu->lock, flags);
  203. if (unlikely(iopte == NULL)) {
  204. free_pages(first_page, order);
  205. return NULL;
  206. }
  207. *dma_addrp = (iommu->page_table_map_base +
  208. ((iopte - iommu->page_table) << IO_PAGE_SHIFT));
  209. ret = (void *) first_page;
  210. npages = size >> IO_PAGE_SHIFT;
  211. first_page = __pa(first_page);
  212. while (npages--) {
  213. iopte_val(*iopte) = (IOPTE_CONSISTENT(0UL) |
  214. IOPTE_WRITE |
  215. (first_page & IOPTE_PAGE));
  216. iopte++;
  217. first_page += IO_PAGE_SIZE;
  218. }
  219. return ret;
  220. }
  221. /* Free and unmap a consistent DMA translation. */
  222. static void pci_4u_free_consistent(struct pci_dev *pdev, size_t size, void *cpu, dma_addr_t dvma)
  223. {
  224. struct pcidev_cookie *pcp;
  225. struct pci_iommu *iommu;
  226. iopte_t *iopte;
  227. unsigned long flags, order, npages;
  228. npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
  229. pcp = pdev->sysdata;
  230. iommu = pcp->pbm->iommu;
  231. iopte = iommu->page_table +
  232. ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  233. spin_lock_irqsave(&iommu->lock, flags);
  234. free_npages(iommu, dvma, npages);
  235. spin_unlock_irqrestore(&iommu->lock, flags);
  236. order = get_order(size);
  237. if (order < 10)
  238. free_pages((unsigned long)cpu, order);
  239. }
  240. /* Map a single buffer at PTR of SZ bytes for PCI DMA
  241. * in streaming mode.
  242. */
  243. static dma_addr_t pci_4u_map_single(struct pci_dev *pdev, void *ptr, size_t sz, int direction)
  244. {
  245. struct pcidev_cookie *pcp;
  246. struct pci_iommu *iommu;
  247. struct pci_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. pcp = pdev->sysdata;
  254. iommu = pcp->pbm->iommu;
  255. strbuf = &pcp->pbm->stc;
  256. if (unlikely(direction == PCI_DMA_NONE))
  257. goto bad_no_ctx;
  258. oaddr = (unsigned long)ptr;
  259. npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
  260. npages >>= IO_PAGE_SHIFT;
  261. spin_lock_irqsave(&iommu->lock, flags);
  262. base = alloc_npages(iommu, npages);
  263. ctx = 0;
  264. if (iommu->iommu_ctxflush)
  265. ctx = iommu_alloc_ctx(iommu);
  266. spin_unlock_irqrestore(&iommu->lock, flags);
  267. if (unlikely(!base))
  268. goto bad;
  269. bus_addr = (iommu->page_table_map_base +
  270. ((base - iommu->page_table) << IO_PAGE_SHIFT));
  271. ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
  272. base_paddr = __pa(oaddr & IO_PAGE_MASK);
  273. if (strbuf->strbuf_enabled)
  274. iopte_protection = IOPTE_STREAMING(ctx);
  275. else
  276. iopte_protection = IOPTE_CONSISTENT(ctx);
  277. if (direction != PCI_DMA_TODEVICE)
  278. iopte_protection |= IOPTE_WRITE;
  279. for (i = 0; i < npages; i++, base++, base_paddr += IO_PAGE_SIZE)
  280. iopte_val(*base) = iopte_protection | base_paddr;
  281. return ret;
  282. bad:
  283. iommu_free_ctx(iommu, ctx);
  284. bad_no_ctx:
  285. if (printk_ratelimit())
  286. WARN_ON(1);
  287. return PCI_DMA_ERROR_CODE;
  288. }
  289. static void pci_strbuf_flush(struct pci_strbuf *strbuf, struct pci_iommu *iommu, u32 vaddr, unsigned long ctx, unsigned long npages, int direction)
  290. {
  291. int limit;
  292. if (strbuf->strbuf_ctxflush &&
  293. iommu->iommu_ctxflush) {
  294. unsigned long matchreg, flushreg;
  295. u64 val;
  296. flushreg = strbuf->strbuf_ctxflush;
  297. matchreg = PCI_STC_CTXMATCH_ADDR(strbuf, ctx);
  298. pci_iommu_write(flushreg, ctx);
  299. val = pci_iommu_read(matchreg);
  300. val &= 0xffff;
  301. if (!val)
  302. goto do_flush_sync;
  303. while (val) {
  304. if (val & 0x1)
  305. pci_iommu_write(flushreg, ctx);
  306. val >>= 1;
  307. }
  308. val = pci_iommu_read(matchreg);
  309. if (unlikely(val)) {
  310. printk(KERN_WARNING "pci_strbuf_flush: ctx flush "
  311. "timeout matchreg[%lx] ctx[%lx]\n",
  312. val, ctx);
  313. goto do_page_flush;
  314. }
  315. } else {
  316. unsigned long i;
  317. do_page_flush:
  318. for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE)
  319. pci_iommu_write(strbuf->strbuf_pflush, vaddr);
  320. }
  321. do_flush_sync:
  322. /* If the device could not have possibly put dirty data into
  323. * the streaming cache, no flush-flag synchronization needs
  324. * to be performed.
  325. */
  326. if (direction == PCI_DMA_TODEVICE)
  327. return;
  328. PCI_STC_FLUSHFLAG_INIT(strbuf);
  329. pci_iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
  330. (void) pci_iommu_read(iommu->write_complete_reg);
  331. limit = 100000;
  332. while (!PCI_STC_FLUSHFLAG_SET(strbuf)) {
  333. limit--;
  334. if (!limit)
  335. break;
  336. udelay(1);
  337. rmb();
  338. }
  339. if (!limit)
  340. printk(KERN_WARNING "pci_strbuf_flush: flushflag timeout "
  341. "vaddr[%08x] ctx[%lx] npages[%ld]\n",
  342. vaddr, ctx, npages);
  343. }
  344. /* Unmap a single streaming mode DMA translation. */
  345. static void pci_4u_unmap_single(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
  346. {
  347. struct pcidev_cookie *pcp;
  348. struct pci_iommu *iommu;
  349. struct pci_strbuf *strbuf;
  350. iopte_t *base;
  351. unsigned long flags, npages, ctx, i;
  352. if (unlikely(direction == PCI_DMA_NONE)) {
  353. if (printk_ratelimit())
  354. WARN_ON(1);
  355. return;
  356. }
  357. pcp = pdev->sysdata;
  358. iommu = pcp->pbm->iommu;
  359. strbuf = &pcp->pbm->stc;
  360. npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
  361. npages >>= IO_PAGE_SHIFT;
  362. base = iommu->page_table +
  363. ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  364. #ifdef DEBUG_PCI_IOMMU
  365. if (IOPTE_IS_DUMMY(iommu, base))
  366. printk("pci_unmap_single called on non-mapped region %08x,%08x from %016lx\n",
  367. bus_addr, sz, __builtin_return_address(0));
  368. #endif
  369. bus_addr &= IO_PAGE_MASK;
  370. spin_lock_irqsave(&iommu->lock, flags);
  371. /* Record the context, if any. */
  372. ctx = 0;
  373. if (iommu->iommu_ctxflush)
  374. ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
  375. /* Step 1: Kick data out of streaming buffers if necessary. */
  376. if (strbuf->strbuf_enabled)
  377. pci_strbuf_flush(strbuf, iommu, bus_addr, ctx,
  378. npages, direction);
  379. /* Step 2: Clear out TSB entries. */
  380. for (i = 0; i < npages; i++)
  381. iopte_make_dummy(iommu, base + i);
  382. free_npages(iommu, bus_addr - iommu->page_table_map_base, npages);
  383. iommu_free_ctx(iommu, ctx);
  384. spin_unlock_irqrestore(&iommu->lock, flags);
  385. }
  386. #define SG_ENT_PHYS_ADDRESS(SG) \
  387. (__pa(page_address((SG)->page)) + (SG)->offset)
  388. static inline void fill_sg(iopte_t *iopte, struct scatterlist *sg,
  389. int nused, int nelems, unsigned long iopte_protection)
  390. {
  391. struct scatterlist *dma_sg = sg;
  392. struct scatterlist *sg_end = sg + nelems;
  393. int i;
  394. for (i = 0; i < nused; i++) {
  395. unsigned long pteval = ~0UL;
  396. u32 dma_npages;
  397. dma_npages = ((dma_sg->dma_address & (IO_PAGE_SIZE - 1UL)) +
  398. dma_sg->dma_length +
  399. ((IO_PAGE_SIZE - 1UL))) >> IO_PAGE_SHIFT;
  400. do {
  401. unsigned long offset;
  402. signed int len;
  403. /* If we are here, we know we have at least one
  404. * more page to map. So walk forward until we
  405. * hit a page crossing, and begin creating new
  406. * mappings from that spot.
  407. */
  408. for (;;) {
  409. unsigned long tmp;
  410. tmp = SG_ENT_PHYS_ADDRESS(sg);
  411. len = sg->length;
  412. if (((tmp ^ pteval) >> IO_PAGE_SHIFT) != 0UL) {
  413. pteval = tmp & IO_PAGE_MASK;
  414. offset = tmp & (IO_PAGE_SIZE - 1UL);
  415. break;
  416. }
  417. if (((tmp ^ (tmp + len - 1UL)) >> IO_PAGE_SHIFT) != 0UL) {
  418. pteval = (tmp + IO_PAGE_SIZE) & IO_PAGE_MASK;
  419. offset = 0UL;
  420. len -= (IO_PAGE_SIZE - (tmp & (IO_PAGE_SIZE - 1UL)));
  421. break;
  422. }
  423. sg++;
  424. }
  425. pteval = iopte_protection | (pteval & IOPTE_PAGE);
  426. while (len > 0) {
  427. *iopte++ = __iopte(pteval);
  428. pteval += IO_PAGE_SIZE;
  429. len -= (IO_PAGE_SIZE - offset);
  430. offset = 0;
  431. dma_npages--;
  432. }
  433. pteval = (pteval & IOPTE_PAGE) + len;
  434. sg++;
  435. /* Skip over any tail mappings we've fully mapped,
  436. * adjusting pteval along the way. Stop when we
  437. * detect a page crossing event.
  438. */
  439. while (sg < sg_end &&
  440. (pteval << (64 - IO_PAGE_SHIFT)) != 0UL &&
  441. (pteval == SG_ENT_PHYS_ADDRESS(sg)) &&
  442. ((pteval ^
  443. (SG_ENT_PHYS_ADDRESS(sg) + sg->length - 1UL)) >> IO_PAGE_SHIFT) == 0UL) {
  444. pteval += sg->length;
  445. sg++;
  446. }
  447. if ((pteval << (64 - IO_PAGE_SHIFT)) == 0UL)
  448. pteval = ~0UL;
  449. } while (dma_npages != 0);
  450. dma_sg++;
  451. }
  452. }
  453. /* Map a set of buffers described by SGLIST with NELEMS array
  454. * elements in streaming mode for PCI DMA.
  455. * When making changes here, inspect the assembly output. I was having
  456. * hard time to kepp this routine out of using stack slots for holding variables.
  457. */
  458. static int pci_4u_map_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
  459. {
  460. struct pcidev_cookie *pcp;
  461. struct pci_iommu *iommu;
  462. struct pci_strbuf *strbuf;
  463. unsigned long flags, ctx, npages, iopte_protection;
  464. iopte_t *base;
  465. u32 dma_base;
  466. struct scatterlist *sgtmp;
  467. int used;
  468. /* Fast path single entry scatterlists. */
  469. if (nelems == 1) {
  470. sglist->dma_address =
  471. pci_4u_map_single(pdev,
  472. (page_address(sglist->page) + sglist->offset),
  473. sglist->length, direction);
  474. if (unlikely(sglist->dma_address == PCI_DMA_ERROR_CODE))
  475. return 0;
  476. sglist->dma_length = sglist->length;
  477. return 1;
  478. }
  479. pcp = pdev->sysdata;
  480. iommu = pcp->pbm->iommu;
  481. strbuf = &pcp->pbm->stc;
  482. if (unlikely(direction == PCI_DMA_NONE))
  483. goto bad_no_ctx;
  484. /* Step 1: Prepare scatter list. */
  485. npages = prepare_sg(sglist, nelems);
  486. /* Step 2: Allocate a cluster and context, if necessary. */
  487. spin_lock_irqsave(&iommu->lock, flags);
  488. base = alloc_npages(iommu, npages);
  489. ctx = 0;
  490. if (iommu->iommu_ctxflush)
  491. ctx = iommu_alloc_ctx(iommu);
  492. spin_unlock_irqrestore(&iommu->lock, flags);
  493. if (base == NULL)
  494. goto bad;
  495. dma_base = iommu->page_table_map_base +
  496. ((base - iommu->page_table) << IO_PAGE_SHIFT);
  497. /* Step 3: Normalize DMA addresses. */
  498. used = nelems;
  499. sgtmp = sglist;
  500. while (used && sgtmp->dma_length) {
  501. sgtmp->dma_address += dma_base;
  502. sgtmp++;
  503. used--;
  504. }
  505. used = nelems - used;
  506. /* Step 4: Create the mappings. */
  507. if (strbuf->strbuf_enabled)
  508. iopte_protection = IOPTE_STREAMING(ctx);
  509. else
  510. iopte_protection = IOPTE_CONSISTENT(ctx);
  511. if (direction != PCI_DMA_TODEVICE)
  512. iopte_protection |= IOPTE_WRITE;
  513. fill_sg(base, sglist, used, nelems, iopte_protection);
  514. #ifdef VERIFY_SG
  515. verify_sglist(sglist, nelems, base, npages);
  516. #endif
  517. return used;
  518. bad:
  519. iommu_free_ctx(iommu, ctx);
  520. bad_no_ctx:
  521. if (printk_ratelimit())
  522. WARN_ON(1);
  523. return 0;
  524. }
  525. /* Unmap a set of streaming mode DMA translations. */
  526. static void pci_4u_unmap_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
  527. {
  528. struct pcidev_cookie *pcp;
  529. struct pci_iommu *iommu;
  530. struct pci_strbuf *strbuf;
  531. iopte_t *base;
  532. unsigned long flags, ctx, i, npages;
  533. u32 bus_addr;
  534. if (unlikely(direction == PCI_DMA_NONE)) {
  535. if (printk_ratelimit())
  536. WARN_ON(1);
  537. }
  538. pcp = pdev->sysdata;
  539. iommu = pcp->pbm->iommu;
  540. strbuf = &pcp->pbm->stc;
  541. bus_addr = sglist->dma_address & IO_PAGE_MASK;
  542. for (i = 1; i < nelems; i++)
  543. if (sglist[i].dma_length == 0)
  544. break;
  545. i--;
  546. npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length) -
  547. bus_addr) >> IO_PAGE_SHIFT;
  548. base = iommu->page_table +
  549. ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  550. #ifdef DEBUG_PCI_IOMMU
  551. if (IOPTE_IS_DUMMY(iommu, base))
  552. printk("pci_unmap_sg called on non-mapped region %016lx,%d from %016lx\n", sglist->dma_address, nelems, __builtin_return_address(0));
  553. #endif
  554. spin_lock_irqsave(&iommu->lock, flags);
  555. /* Record the context, if any. */
  556. ctx = 0;
  557. if (iommu->iommu_ctxflush)
  558. ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
  559. /* Step 1: Kick data out of streaming buffers if necessary. */
  560. if (strbuf->strbuf_enabled)
  561. pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
  562. /* Step 2: Clear out the TSB entries. */
  563. for (i = 0; i < npages; i++)
  564. iopte_make_dummy(iommu, base + i);
  565. free_npages(iommu, bus_addr - iommu->page_table_map_base, npages);
  566. iommu_free_ctx(iommu, ctx);
  567. spin_unlock_irqrestore(&iommu->lock, flags);
  568. }
  569. /* Make physical memory consistent for a single
  570. * streaming mode DMA translation after a transfer.
  571. */
  572. static void pci_4u_dma_sync_single_for_cpu(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
  573. {
  574. struct pcidev_cookie *pcp;
  575. struct pci_iommu *iommu;
  576. struct pci_strbuf *strbuf;
  577. unsigned long flags, ctx, npages;
  578. pcp = pdev->sysdata;
  579. iommu = pcp->pbm->iommu;
  580. strbuf = &pcp->pbm->stc;
  581. if (!strbuf->strbuf_enabled)
  582. return;
  583. spin_lock_irqsave(&iommu->lock, flags);
  584. npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
  585. npages >>= IO_PAGE_SHIFT;
  586. bus_addr &= IO_PAGE_MASK;
  587. /* Step 1: Record the context, if any. */
  588. ctx = 0;
  589. if (iommu->iommu_ctxflush &&
  590. strbuf->strbuf_ctxflush) {
  591. iopte_t *iopte;
  592. iopte = iommu->page_table +
  593. ((bus_addr - iommu->page_table_map_base)>>IO_PAGE_SHIFT);
  594. ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
  595. }
  596. /* Step 2: Kick data out of streaming buffers. */
  597. pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
  598. spin_unlock_irqrestore(&iommu->lock, flags);
  599. }
  600. /* Make physical memory consistent for a set of streaming
  601. * mode DMA translations after a transfer.
  602. */
  603. static void pci_4u_dma_sync_sg_for_cpu(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
  604. {
  605. struct pcidev_cookie *pcp;
  606. struct pci_iommu *iommu;
  607. struct pci_strbuf *strbuf;
  608. unsigned long flags, ctx, npages, i;
  609. u32 bus_addr;
  610. pcp = pdev->sysdata;
  611. iommu = pcp->pbm->iommu;
  612. strbuf = &pcp->pbm->stc;
  613. if (!strbuf->strbuf_enabled)
  614. return;
  615. spin_lock_irqsave(&iommu->lock, flags);
  616. /* Step 1: Record the context, if any. */
  617. ctx = 0;
  618. if (iommu->iommu_ctxflush &&
  619. strbuf->strbuf_ctxflush) {
  620. iopte_t *iopte;
  621. iopte = iommu->page_table +
  622. ((sglist[0].dma_address - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  623. ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
  624. }
  625. /* Step 2: Kick data out of streaming buffers. */
  626. bus_addr = sglist[0].dma_address & IO_PAGE_MASK;
  627. for(i = 1; i < nelems; i++)
  628. if (!sglist[i].dma_length)
  629. break;
  630. i--;
  631. npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length)
  632. - bus_addr) >> IO_PAGE_SHIFT;
  633. pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages, direction);
  634. spin_unlock_irqrestore(&iommu->lock, flags);
  635. }
  636. struct pci_iommu_ops pci_sun4u_iommu_ops = {
  637. .alloc_consistent = pci_4u_alloc_consistent,
  638. .free_consistent = pci_4u_free_consistent,
  639. .map_single = pci_4u_map_single,
  640. .unmap_single = pci_4u_unmap_single,
  641. .map_sg = pci_4u_map_sg,
  642. .unmap_sg = pci_4u_unmap_sg,
  643. .dma_sync_single_for_cpu = pci_4u_dma_sync_single_for_cpu,
  644. .dma_sync_sg_for_cpu = pci_4u_dma_sync_sg_for_cpu,
  645. };
  646. static void ali_sound_dma_hack(struct pci_dev *pdev, int set_bit)
  647. {
  648. struct pci_dev *ali_isa_bridge;
  649. u8 val;
  650. /* ALI sound chips generate 31-bits of DMA, a special register
  651. * determines what bit 31 is emitted as.
  652. */
  653. ali_isa_bridge = pci_get_device(PCI_VENDOR_ID_AL,
  654. PCI_DEVICE_ID_AL_M1533,
  655. NULL);
  656. pci_read_config_byte(ali_isa_bridge, 0x7e, &val);
  657. if (set_bit)
  658. val |= 0x01;
  659. else
  660. val &= ~0x01;
  661. pci_write_config_byte(ali_isa_bridge, 0x7e, val);
  662. pci_dev_put(ali_isa_bridge);
  663. }
  664. int pci_dma_supported(struct pci_dev *pdev, u64 device_mask)
  665. {
  666. struct pcidev_cookie *pcp = pdev->sysdata;
  667. u64 dma_addr_mask;
  668. if (pdev == NULL) {
  669. dma_addr_mask = 0xffffffff;
  670. } else {
  671. struct pci_iommu *iommu = pcp->pbm->iommu;
  672. dma_addr_mask = iommu->dma_addr_mask;
  673. if (pdev->vendor == PCI_VENDOR_ID_AL &&
  674. pdev->device == PCI_DEVICE_ID_AL_M5451 &&
  675. device_mask == 0x7fffffff) {
  676. ali_sound_dma_hack(pdev,
  677. (dma_addr_mask & 0x80000000) != 0);
  678. return 1;
  679. }
  680. }
  681. if (device_mask >= (1UL << 32UL))
  682. return 0;
  683. return (device_mask & dma_addr_mask) == dma_addr_mask;
  684. }