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