pci_iommu.c 22 KB

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