pci_iommu.c 22 KB

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