pci_iommu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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. /* Allocate and map kernel buffer of size SIZE using consistent mode
  166. * DMA for PCI device PDEV. Return non-NULL cpu-side address if
  167. * successful and set *DMA_ADDRP to the PCI side dma address.
  168. */
  169. void *pci_alloc_consistent(struct pci_dev *pdev, size_t size, dma_addr_t *dma_addrp)
  170. {
  171. struct pcidev_cookie *pcp;
  172. struct pci_iommu *iommu;
  173. iopte_t *iopte;
  174. unsigned long flags, order, first_page, ctx;
  175. void *ret;
  176. int npages;
  177. size = IO_PAGE_ALIGN(size);
  178. order = get_order(size);
  179. if (order >= 10)
  180. return NULL;
  181. first_page = __get_free_pages(GFP_ATOMIC, order);
  182. if (first_page == 0UL)
  183. return NULL;
  184. memset((char *)first_page, 0, PAGE_SIZE << order);
  185. pcp = pdev->sysdata;
  186. iommu = pcp->pbm->iommu;
  187. spin_lock_irqsave(&iommu->lock, flags);
  188. iopte = alloc_consistent_cluster(iommu, size >> IO_PAGE_SHIFT);
  189. if (iopte == NULL) {
  190. spin_unlock_irqrestore(&iommu->lock, flags);
  191. free_pages(first_page, order);
  192. return NULL;
  193. }
  194. *dma_addrp = (iommu->page_table_map_base +
  195. ((iopte - iommu->page_table) << IO_PAGE_SHIFT));
  196. ret = (void *) first_page;
  197. npages = size >> IO_PAGE_SHIFT;
  198. ctx = 0;
  199. if (iommu->iommu_ctxflush)
  200. ctx = iommu->iommu_cur_ctx++;
  201. first_page = __pa(first_page);
  202. while (npages--) {
  203. iopte_val(*iopte) = (IOPTE_CONSISTENT(ctx) |
  204. IOPTE_WRITE |
  205. (first_page & IOPTE_PAGE));
  206. iopte++;
  207. first_page += IO_PAGE_SIZE;
  208. }
  209. {
  210. int i;
  211. u32 daddr = *dma_addrp;
  212. npages = size >> IO_PAGE_SHIFT;
  213. for (i = 0; i < npages; i++) {
  214. pci_iommu_write(iommu->iommu_flush, daddr);
  215. daddr += IO_PAGE_SIZE;
  216. }
  217. }
  218. spin_unlock_irqrestore(&iommu->lock, flags);
  219. return ret;
  220. }
  221. /* Free and unmap a consistent DMA translation. */
  222. void pci_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, i, ctx;
  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. if ((iopte - iommu->page_table) ==
  235. iommu->lowest_consistent_map) {
  236. iopte_t *walk = iopte + npages;
  237. iopte_t *limit;
  238. limit = (iommu->page_table +
  239. (1 << (iommu->page_table_sz_bits - PBM_LOGCLUSTERS)));
  240. while (walk < limit) {
  241. if (!IOPTE_IS_DUMMY(iommu, walk))
  242. break;
  243. walk++;
  244. }
  245. iommu->lowest_consistent_map =
  246. (walk - iommu->page_table);
  247. }
  248. /* Data for consistent mappings cannot enter the streaming
  249. * buffers, so we only need to update the TSB. We flush
  250. * the IOMMU here as well to prevent conflicts with the
  251. * streaming mapping deferred tlb flush scheme.
  252. */
  253. ctx = 0;
  254. if (iommu->iommu_ctxflush)
  255. ctx = (iopte_val(*iopte) & IOPTE_CONTEXT) >> 47UL;
  256. for (i = 0; i < npages; i++, iopte++)
  257. iopte_make_dummy(iommu, iopte);
  258. if (iommu->iommu_ctxflush) {
  259. pci_iommu_write(iommu->iommu_ctxflush, ctx);
  260. } else {
  261. for (i = 0; i < npages; i++) {
  262. u32 daddr = dvma + (i << IO_PAGE_SHIFT);
  263. pci_iommu_write(iommu->iommu_flush, daddr);
  264. }
  265. }
  266. spin_unlock_irqrestore(&iommu->lock, flags);
  267. order = get_order(size);
  268. if (order < 10)
  269. free_pages((unsigned long)cpu, order);
  270. }
  271. /* Map a single buffer at PTR of SZ bytes for PCI DMA
  272. * in streaming mode.
  273. */
  274. dma_addr_t pci_map_single(struct pci_dev *pdev, void *ptr, size_t sz, int direction)
  275. {
  276. struct pcidev_cookie *pcp;
  277. struct pci_iommu *iommu;
  278. struct pci_strbuf *strbuf;
  279. iopte_t *base;
  280. unsigned long flags, npages, oaddr;
  281. unsigned long i, base_paddr, ctx;
  282. u32 bus_addr, ret;
  283. unsigned long iopte_protection;
  284. pcp = pdev->sysdata;
  285. iommu = pcp->pbm->iommu;
  286. strbuf = &pcp->pbm->stc;
  287. if (direction == PCI_DMA_NONE)
  288. BUG();
  289. oaddr = (unsigned long)ptr;
  290. npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
  291. npages >>= IO_PAGE_SHIFT;
  292. spin_lock_irqsave(&iommu->lock, flags);
  293. base = alloc_streaming_cluster(iommu, npages);
  294. if (base == NULL)
  295. goto bad;
  296. bus_addr = (iommu->page_table_map_base +
  297. ((base - iommu->page_table) << IO_PAGE_SHIFT));
  298. ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
  299. base_paddr = __pa(oaddr & IO_PAGE_MASK);
  300. ctx = 0;
  301. if (iommu->iommu_ctxflush)
  302. ctx = iommu->iommu_cur_ctx++;
  303. if (strbuf->strbuf_enabled)
  304. iopte_protection = IOPTE_STREAMING(ctx);
  305. else
  306. iopte_protection = IOPTE_CONSISTENT(ctx);
  307. if (direction != PCI_DMA_TODEVICE)
  308. iopte_protection |= IOPTE_WRITE;
  309. for (i = 0; i < npages; i++, base++, base_paddr += IO_PAGE_SIZE)
  310. iopte_val(*base) = iopte_protection | base_paddr;
  311. spin_unlock_irqrestore(&iommu->lock, flags);
  312. return ret;
  313. bad:
  314. spin_unlock_irqrestore(&iommu->lock, flags);
  315. return PCI_DMA_ERROR_CODE;
  316. }
  317. static void pci_strbuf_flush(struct pci_strbuf *strbuf, struct pci_iommu *iommu, u32 vaddr, unsigned long ctx, unsigned long npages)
  318. {
  319. int limit;
  320. PCI_STC_FLUSHFLAG_INIT(strbuf);
  321. if (strbuf->strbuf_ctxflush &&
  322. iommu->iommu_ctxflush) {
  323. unsigned long matchreg, flushreg;
  324. flushreg = strbuf->strbuf_ctxflush;
  325. matchreg = PCI_STC_CTXMATCH_ADDR(strbuf, ctx);
  326. limit = 10000;
  327. do {
  328. pci_iommu_write(flushreg, ctx);
  329. udelay(10);
  330. limit--;
  331. if (!limit)
  332. break;
  333. } while(((long)pci_iommu_read(matchreg)) < 0L);
  334. if (!limit)
  335. printk(KERN_WARNING "pci_strbuf_flush: ctx flush "
  336. "timeout vaddr[%08x] ctx[%lx]\n",
  337. vaddr, ctx);
  338. } else {
  339. unsigned long i;
  340. for (i = 0; i < npages; i++, vaddr += IO_PAGE_SIZE)
  341. pci_iommu_write(strbuf->strbuf_pflush, vaddr);
  342. }
  343. pci_iommu_write(strbuf->strbuf_fsync, strbuf->strbuf_flushflag_pa);
  344. (void) pci_iommu_read(iommu->write_complete_reg);
  345. limit = 10000;
  346. while (!PCI_STC_FLUSHFLAG_SET(strbuf)) {
  347. limit--;
  348. if (!limit)
  349. break;
  350. udelay(10);
  351. membar("#LoadLoad");
  352. }
  353. if (!limit)
  354. printk(KERN_WARNING "pci_strbuf_flush: flushflag timeout "
  355. "vaddr[%08x] ctx[%lx] npages[%ld]\n",
  356. vaddr, ctx, npages);
  357. }
  358. /* Unmap a single streaming mode DMA translation. */
  359. void pci_unmap_single(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
  360. {
  361. struct pcidev_cookie *pcp;
  362. struct pci_iommu *iommu;
  363. struct pci_strbuf *strbuf;
  364. iopte_t *base;
  365. unsigned long flags, npages, ctx;
  366. if (direction == PCI_DMA_NONE)
  367. BUG();
  368. pcp = pdev->sysdata;
  369. iommu = pcp->pbm->iommu;
  370. strbuf = &pcp->pbm->stc;
  371. npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
  372. npages >>= IO_PAGE_SHIFT;
  373. base = iommu->page_table +
  374. ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  375. #ifdef DEBUG_PCI_IOMMU
  376. if (IOPTE_IS_DUMMY(iommu, base))
  377. printk("pci_unmap_single called on non-mapped region %08x,%08x from %016lx\n",
  378. bus_addr, sz, __builtin_return_address(0));
  379. #endif
  380. bus_addr &= IO_PAGE_MASK;
  381. spin_lock_irqsave(&iommu->lock, flags);
  382. /* Record the context, if any. */
  383. ctx = 0;
  384. if (iommu->iommu_ctxflush)
  385. ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
  386. /* Step 1: Kick data out of streaming buffers if necessary. */
  387. if (strbuf->strbuf_enabled)
  388. pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages);
  389. /* Step 2: Clear out first TSB entry. */
  390. iopte_make_dummy(iommu, base);
  391. free_streaming_cluster(iommu, bus_addr - iommu->page_table_map_base,
  392. npages, ctx);
  393. spin_unlock_irqrestore(&iommu->lock, flags);
  394. }
  395. #define SG_ENT_PHYS_ADDRESS(SG) \
  396. (__pa(page_address((SG)->page)) + (SG)->offset)
  397. static inline void fill_sg(iopte_t *iopte, struct scatterlist *sg,
  398. int nused, int nelems, unsigned long iopte_protection)
  399. {
  400. struct scatterlist *dma_sg = sg;
  401. struct scatterlist *sg_end = sg + nelems;
  402. int i;
  403. for (i = 0; i < nused; i++) {
  404. unsigned long pteval = ~0UL;
  405. u32 dma_npages;
  406. dma_npages = ((dma_sg->dma_address & (IO_PAGE_SIZE - 1UL)) +
  407. dma_sg->dma_length +
  408. ((IO_PAGE_SIZE - 1UL))) >> IO_PAGE_SHIFT;
  409. do {
  410. unsigned long offset;
  411. signed int len;
  412. /* If we are here, we know we have at least one
  413. * more page to map. So walk forward until we
  414. * hit a page crossing, and begin creating new
  415. * mappings from that spot.
  416. */
  417. for (;;) {
  418. unsigned long tmp;
  419. tmp = SG_ENT_PHYS_ADDRESS(sg);
  420. len = sg->length;
  421. if (((tmp ^ pteval) >> IO_PAGE_SHIFT) != 0UL) {
  422. pteval = tmp & IO_PAGE_MASK;
  423. offset = tmp & (IO_PAGE_SIZE - 1UL);
  424. break;
  425. }
  426. if (((tmp ^ (tmp + len - 1UL)) >> IO_PAGE_SHIFT) != 0UL) {
  427. pteval = (tmp + IO_PAGE_SIZE) & IO_PAGE_MASK;
  428. offset = 0UL;
  429. len -= (IO_PAGE_SIZE - (tmp & (IO_PAGE_SIZE - 1UL)));
  430. break;
  431. }
  432. sg++;
  433. }
  434. pteval = iopte_protection | (pteval & IOPTE_PAGE);
  435. while (len > 0) {
  436. *iopte++ = __iopte(pteval);
  437. pteval += IO_PAGE_SIZE;
  438. len -= (IO_PAGE_SIZE - offset);
  439. offset = 0;
  440. dma_npages--;
  441. }
  442. pteval = (pteval & IOPTE_PAGE) + len;
  443. sg++;
  444. /* Skip over any tail mappings we've fully mapped,
  445. * adjusting pteval along the way. Stop when we
  446. * detect a page crossing event.
  447. */
  448. while (sg < sg_end &&
  449. (pteval << (64 - IO_PAGE_SHIFT)) != 0UL &&
  450. (pteval == SG_ENT_PHYS_ADDRESS(sg)) &&
  451. ((pteval ^
  452. (SG_ENT_PHYS_ADDRESS(sg) + sg->length - 1UL)) >> IO_PAGE_SHIFT) == 0UL) {
  453. pteval += sg->length;
  454. sg++;
  455. }
  456. if ((pteval << (64 - IO_PAGE_SHIFT)) == 0UL)
  457. pteval = ~0UL;
  458. } while (dma_npages != 0);
  459. dma_sg++;
  460. }
  461. }
  462. /* Map a set of buffers described by SGLIST with NELEMS array
  463. * elements in streaming mode for PCI DMA.
  464. * When making changes here, inspect the assembly output. I was having
  465. * hard time to kepp this routine out of using stack slots for holding variables.
  466. */
  467. int pci_map_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
  468. {
  469. struct pcidev_cookie *pcp;
  470. struct pci_iommu *iommu;
  471. struct pci_strbuf *strbuf;
  472. unsigned long flags, ctx, npages, iopte_protection;
  473. iopte_t *base;
  474. u32 dma_base;
  475. struct scatterlist *sgtmp;
  476. int used;
  477. /* Fast path single entry scatterlists. */
  478. if (nelems == 1) {
  479. sglist->dma_address =
  480. pci_map_single(pdev,
  481. (page_address(sglist->page) + sglist->offset),
  482. sglist->length, direction);
  483. sglist->dma_length = sglist->length;
  484. return 1;
  485. }
  486. pcp = pdev->sysdata;
  487. iommu = pcp->pbm->iommu;
  488. strbuf = &pcp->pbm->stc;
  489. if (direction == PCI_DMA_NONE)
  490. BUG();
  491. /* Step 1: Prepare scatter list. */
  492. npages = prepare_sg(sglist, nelems);
  493. /* Step 2: Allocate a cluster. */
  494. spin_lock_irqsave(&iommu->lock, flags);
  495. base = alloc_streaming_cluster(iommu, npages);
  496. if (base == NULL)
  497. goto bad;
  498. dma_base = iommu->page_table_map_base + ((base - iommu->page_table) << IO_PAGE_SHIFT);
  499. /* Step 3: Normalize DMA addresses. */
  500. used = nelems;
  501. sgtmp = sglist;
  502. while (used && sgtmp->dma_length) {
  503. sgtmp->dma_address += dma_base;
  504. sgtmp++;
  505. used--;
  506. }
  507. used = nelems - used;
  508. /* Step 4: Choose a context if necessary. */
  509. ctx = 0;
  510. if (iommu->iommu_ctxflush)
  511. ctx = iommu->iommu_cur_ctx++;
  512. /* Step 5: Create the mappings. */
  513. if (strbuf->strbuf_enabled)
  514. iopte_protection = IOPTE_STREAMING(ctx);
  515. else
  516. iopte_protection = IOPTE_CONSISTENT(ctx);
  517. if (direction != PCI_DMA_TODEVICE)
  518. iopte_protection |= IOPTE_WRITE;
  519. fill_sg (base, sglist, used, nelems, iopte_protection);
  520. #ifdef VERIFY_SG
  521. verify_sglist(sglist, nelems, base, npages);
  522. #endif
  523. spin_unlock_irqrestore(&iommu->lock, flags);
  524. return used;
  525. bad:
  526. spin_unlock_irqrestore(&iommu->lock, flags);
  527. return PCI_DMA_ERROR_CODE;
  528. }
  529. /* Unmap a set of streaming mode DMA translations. */
  530. void pci_unmap_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
  531. {
  532. struct pcidev_cookie *pcp;
  533. struct pci_iommu *iommu;
  534. struct pci_strbuf *strbuf;
  535. iopte_t *base;
  536. unsigned long flags, ctx, i, npages;
  537. u32 bus_addr;
  538. if (direction == PCI_DMA_NONE)
  539. BUG();
  540. pcp = pdev->sysdata;
  541. iommu = pcp->pbm->iommu;
  542. strbuf = &pcp->pbm->stc;
  543. bus_addr = sglist->dma_address & IO_PAGE_MASK;
  544. for (i = 1; i < nelems; i++)
  545. if (sglist[i].dma_length == 0)
  546. break;
  547. i--;
  548. npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length) - bus_addr) >> IO_PAGE_SHIFT;
  549. base = iommu->page_table +
  550. ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
  551. #ifdef DEBUG_PCI_IOMMU
  552. if (IOPTE_IS_DUMMY(iommu, base))
  553. printk("pci_unmap_sg called on non-mapped region %016lx,%d from %016lx\n", sglist->dma_address, nelems, __builtin_return_address(0));
  554. #endif
  555. spin_lock_irqsave(&iommu->lock, flags);
  556. /* Record the context, if any. */
  557. ctx = 0;
  558. if (iommu->iommu_ctxflush)
  559. ctx = (iopte_val(*base) & IOPTE_CONTEXT) >> 47UL;
  560. /* Step 1: Kick data out of streaming buffers if necessary. */
  561. if (strbuf->strbuf_enabled)
  562. pci_strbuf_flush(strbuf, iommu, bus_addr, ctx, npages);
  563. /* Step 2: Clear out first TSB entry. */
  564. iopte_make_dummy(iommu, base);
  565. free_streaming_cluster(iommu, bus_addr - iommu->page_table_map_base,
  566. npages, 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. void pci_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);
  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. void pci_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);
  634. spin_unlock_irqrestore(&iommu->lock, flags);
  635. }
  636. static void ali_sound_dma_hack(struct pci_dev *pdev, int set_bit)
  637. {
  638. struct pci_dev *ali_isa_bridge;
  639. u8 val;
  640. /* ALI sound chips generate 31-bits of DMA, a special register
  641. * determines what bit 31 is emitted as.
  642. */
  643. ali_isa_bridge = pci_get_device(PCI_VENDOR_ID_AL,
  644. PCI_DEVICE_ID_AL_M1533,
  645. NULL);
  646. pci_read_config_byte(ali_isa_bridge, 0x7e, &val);
  647. if (set_bit)
  648. val |= 0x01;
  649. else
  650. val &= ~0x01;
  651. pci_write_config_byte(ali_isa_bridge, 0x7e, val);
  652. pci_dev_put(ali_isa_bridge);
  653. }
  654. int pci_dma_supported(struct pci_dev *pdev, u64 device_mask)
  655. {
  656. struct pcidev_cookie *pcp = pdev->sysdata;
  657. u64 dma_addr_mask;
  658. if (pdev == NULL) {
  659. dma_addr_mask = 0xffffffff;
  660. } else {
  661. struct pci_iommu *iommu = pcp->pbm->iommu;
  662. dma_addr_mask = iommu->dma_addr_mask;
  663. if (pdev->vendor == PCI_VENDOR_ID_AL &&
  664. pdev->device == PCI_DEVICE_ID_AL_M5451 &&
  665. device_mask == 0x7fffffff) {
  666. ali_sound_dma_hack(pdev,
  667. (dma_addr_mask & 0x80000000) != 0);
  668. return 1;
  669. }
  670. }
  671. if (device_mask >= (1UL << 32UL))
  672. return 0;
  673. return (device_mask & dma_addr_mask) == dma_addr_mask;
  674. }