1/* Modified by Broadcom Corp. Portions Copyright (c) Broadcom Corp, 2012. */ 2/* 3 * linux/arch/arm/mm/dma-mapping.c 4 * 5 * Copyright (C) 2000-2004 Russell King 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License version 2 as 9 * published by the Free Software Foundation. 10 * 11 * DMA uncached mapping support. 12 */ 13#include <linux/module.h> 14#include <linux/mm.h> 15#include <linux/gfp.h> 16#include <linux/errno.h> 17#include <linux/list.h> 18#include <linux/init.h> 19#include <linux/device.h> 20#include <linux/dma-mapping.h> 21 22#include <asm/memory.h> 23#include <asm/highmem.h> 24#include <asm/cacheflush.h> 25#include <asm/tlbflush.h> 26#include <asm/sizes.h> 27 28#include <typedefs.h> 29#include <bcmdefs.h> 30 31static u64 get_coherent_dma_mask(struct device *dev) 32{ 33 u64 mask = ISA_DMA_THRESHOLD; 34 35 if (dev) { 36 mask = dev->coherent_dma_mask; 37 38 /* 39 * Sanity check the DMA mask - it must be non-zero, and 40 * must be able to be satisfied by a DMA allocation. 41 */ 42 if (mask == 0) { 43 dev_warn(dev, "coherent DMA mask is unset\n"); 44 return 0; 45 } 46 47 if ((~mask) & ISA_DMA_THRESHOLD) { 48 dev_warn(dev, "coherent DMA mask %#llx is smaller " 49 "than system GFP_DMA mask %#llx\n", 50 mask, (unsigned long long)ISA_DMA_THRESHOLD); 51 return 0; 52 } 53 } 54 55 return mask; 56} 57 58/* 59 * Allocate a DMA buffer for 'dev' of size 'size' using the 60 * specified gfp mask. Note that 'size' must be page aligned. 61 */ 62static struct page *__dma_alloc_buffer(struct device *dev, size_t size, gfp_t gfp) 63{ 64 unsigned long order = get_order(size); 65 struct page *page, *p, *e; 66 void *ptr; 67 u64 mask = get_coherent_dma_mask(dev); 68 69#ifdef CONFIG_DMA_API_DEBUG 70 u64 limit = (mask + 1) & ~mask; 71 if (limit && size >= limit) { 72 dev_warn(dev, "coherent allocation too big (requested %#x mask %#llx)\n", 73 size, mask); 74 return NULL; 75 } 76#endif 77 78 if (!mask) 79 return NULL; 80 81 if (mask < 0xffffffffULL) 82 gfp |= GFP_DMA; 83 84 page = alloc_pages(gfp, order); 85 if (!page) 86 return NULL; 87 88 /* 89 * Now split the huge page and free the excess pages 90 */ 91 split_page(page, order); 92 for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++) 93 __free_page(p); 94 95 /* 96 * Ensure that the allocated pages are zeroed, and that any data 97 * lurking in the kernel direct-mapped region is invalidated. 98 */ 99 ptr = page_address(page); 100 memset(ptr, 0, size); 101 dmac_flush_range(ptr, ptr + size); 102 outer_flush_range(__pa(ptr), __pa(ptr) + size); 103 104 return page; 105} 106 107/* 108 * Free a DMA buffer. 'size' must be page aligned. 109 */ 110static void __dma_free_buffer(struct page *page, size_t size) 111{ 112 struct page *e = page + (size >> PAGE_SHIFT); 113 114 while (page < e) { 115 __free_page(page); 116 page++; 117 } 118} 119 120#ifdef CONFIG_MMU 121/* Sanity check size */ 122#if (CONSISTENT_DMA_SIZE % SZ_2M) 123#error "CONSISTENT_DMA_SIZE must be multiple of 2MiB" 124#endif 125 126#define CONSISTENT_OFFSET(x) (((unsigned long)(x) - CONSISTENT_BASE) >> PAGE_SHIFT) 127#define CONSISTENT_PTE_INDEX(x) (((unsigned long)(x) - CONSISTENT_BASE) >> PGDIR_SHIFT) 128#define NUM_CONSISTENT_PTES (CONSISTENT_DMA_SIZE >> PGDIR_SHIFT) 129 130/* 131 * These are the page tables (2MB each) covering uncached, DMA consistent allocations 132 */ 133static pte_t *consistent_pte[NUM_CONSISTENT_PTES]; 134 135#include "vmregion.h" 136 137static struct arm_vmregion_head consistent_head = { 138 .vm_lock = __SPIN_LOCK_UNLOCKED(&consistent_head.vm_lock), 139 .vm_list = LIST_HEAD_INIT(consistent_head.vm_list), 140 .vm_start = CONSISTENT_BASE, 141 .vm_end = CONSISTENT_END, 142}; 143 144#ifdef CONFIG_HUGETLB_PAGE 145#error ARM Coherent DMA allocator does not (yet) support huge TLB 146#endif 147 148/* 149 * Initialise the consistent memory allocation. 150 */ 151static int __init consistent_init(void) 152{ 153 int ret = 0; 154 pgd_t *pgd; 155 pmd_t *pmd; 156 pte_t *pte; 157 int i = 0; 158 u32 base = CONSISTENT_BASE; 159 160 do { 161 pgd = pgd_offset(&init_mm, base); 162 pmd = pmd_alloc(&init_mm, pgd, base); 163 if (!pmd) { 164 printk(KERN_ERR "%s: no pmd tables\n", __func__); 165 ret = -ENOMEM; 166 break; 167 } 168 WARN_ON(!pmd_none(*pmd)); 169 170 pte = pte_alloc_kernel(pmd, base); 171 if (!pte) { 172 printk(KERN_ERR "%s: no pte tables\n", __func__); 173 ret = -ENOMEM; 174 break; 175 } 176 177 consistent_pte[i++] = pte; 178 base += (1 << PGDIR_SHIFT); 179 } while (base < CONSISTENT_END); 180 181 return ret; 182} 183 184core_initcall(consistent_init); 185 186static void * 187__dma_alloc_remap(struct page *page, size_t size, gfp_t gfp, pgprot_t prot) 188{ 189 struct arm_vmregion *c; 190 size_t align; 191 int bit; 192 193 if (!consistent_pte[0]) { 194 printk(KERN_ERR "%s: not initialised\n", __func__); 195 dump_stack(); 196 return NULL; 197 } 198 199 /* 200 * Align the virtual region allocation - maximum alignment is 201 * a section size, minimum is a page size. This helps reduce 202 * fragmentation of the DMA space, and also prevents allocations 203 * smaller than a section from crossing a section boundary. 204 */ 205 bit = fls(size - 1) + 1; 206 if (bit > SECTION_SHIFT) 207 bit = SECTION_SHIFT; 208 align = 1 << bit; 209 210 /* 211 * Allocate a virtual address in the consistent mapping region. 212 */ 213 c = arm_vmregion_alloc(&consistent_head, align, size, 214 gfp & ~(__GFP_DMA | __GFP_HIGHMEM)); 215 if (c) { 216 pte_t *pte; 217 int idx = CONSISTENT_PTE_INDEX(c->vm_start); 218 u32 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1); 219 220 pte = consistent_pte[idx] + off; 221 c->vm_pages = page; 222 223 do { 224 BUG_ON(!pte_none(*pte)); 225 226 set_pte_ext(pte, mk_pte(page, prot), 0); 227 page++; 228 pte++; 229 off++; 230 if (off >= PTRS_PER_PTE) { 231 BUG_ON(idx >= (NUM_CONSISTENT_PTES-1)); 232 off = 0; 233 pte = consistent_pte[++idx]; 234 } 235 } while (size -= PAGE_SIZE); 236 237 dsb(); 238 239 return (void *)c->vm_start; 240 } 241 return NULL; 242} 243 244static void __dma_free_remap(void *cpu_addr, size_t size) 245{ 246 struct arm_vmregion *c; 247 unsigned long addr; 248 pte_t *ptep; 249 int idx; 250 u32 off; 251 252 c = arm_vmregion_find_remove(&consistent_head, (unsigned long)cpu_addr); 253 if (!c) { 254 printk(KERN_ERR "%s: trying to free invalid coherent area: %p\n", 255 __func__, cpu_addr); 256 dump_stack(); 257 return; 258 } 259 260 if ((c->vm_end - c->vm_start) != size) { 261 printk(KERN_ERR "%s: freeing wrong coherent size (%ld != %d)\n", 262 __func__, c->vm_end - c->vm_start, size); 263 dump_stack(); 264 size = c->vm_end - c->vm_start; 265 } 266 267 idx = CONSISTENT_PTE_INDEX(c->vm_start); 268 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1); 269 ptep = consistent_pte[idx] + off; 270 addr = c->vm_start; 271 do { 272 pte_t pte = ptep_get_and_clear(&init_mm, addr, ptep); 273 274 ptep++; 275 addr += PAGE_SIZE; 276 off++; 277 if (off >= PTRS_PER_PTE) { 278 BUG_ON(idx >= (NUM_CONSISTENT_PTES-1)); 279 off = 0; 280 ptep = consistent_pte[++idx]; 281 } 282 283 if (pte_none(pte) || !pte_present(pte)) 284 printk(KERN_CRIT "%s: bad page in kernel page table\n", 285 __func__); 286 } while (size -= PAGE_SIZE); 287 288 flush_tlb_kernel_range(c->vm_start, c->vm_end); 289 290 arm_vmregion_free(&consistent_head, c); 291} 292 293#else /* !CONFIG_MMU */ 294 295#define __dma_alloc_remap(page, size, gfp, prot) page_address(page) 296#define __dma_free_remap(addr, size) do { } while (0) 297 298#endif /* CONFIG_MMU */ 299 300static void * 301__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp, 302 pgprot_t prot) 303{ 304 struct page *page; 305 void *addr; 306 307 *handle = ~0; 308 size = PAGE_ALIGN(size); 309 310 page = __dma_alloc_buffer(dev, size, gfp); 311 if (!page) 312 return NULL; 313 314 if (!arch_is_coherent()) 315 addr = __dma_alloc_remap(page, size, gfp, prot); 316 else 317 addr = page_address(page); 318 319 if (addr) 320 *handle = page_to_dma(dev, page); 321 322 return addr; 323} 324 325/* 326 * Allocate DMA-coherent memory space and return both the kernel remapped 327 * virtual and bus address for that space. 328 */ 329void * 330dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp) 331{ 332 void *memory; 333 334 if (dma_alloc_from_coherent(dev, size, handle, &memory)) 335 return memory; 336 337 return __dma_alloc(dev, size, handle, gfp, 338 pgprot_dmacoherent(pgprot_kernel)); 339} 340EXPORT_SYMBOL(dma_alloc_coherent); 341 342/* 343 * Allocate a writecombining region, in much the same way as 344 * dma_alloc_coherent above. 345 */ 346void * 347dma_alloc_writecombine(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp) 348{ 349 return __dma_alloc(dev, size, handle, gfp, 350 pgprot_writecombine(pgprot_kernel)); 351} 352EXPORT_SYMBOL(dma_alloc_writecombine); 353 354static int dma_mmap(struct device *dev, struct vm_area_struct *vma, 355 void *cpu_addr, dma_addr_t dma_addr, size_t size) 356{ 357 int ret = -ENXIO; 358#ifdef CONFIG_MMU 359 unsigned long user_size, kern_size; 360 struct arm_vmregion *c; 361 362 user_size = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; 363 364 c = arm_vmregion_find(&consistent_head, (unsigned long)cpu_addr); 365 if (c) { 366 unsigned long off = vma->vm_pgoff; 367 368 kern_size = (c->vm_end - c->vm_start) >> PAGE_SHIFT; 369 370 if (off < kern_size && 371 user_size <= (kern_size - off)) { 372 ret = remap_pfn_range(vma, vma->vm_start, 373 page_to_pfn(c->vm_pages) + off, 374 user_size << PAGE_SHIFT, 375 vma->vm_page_prot); 376 } 377 } 378#endif /* CONFIG_MMU */ 379 380 return ret; 381} 382 383int dma_mmap_coherent(struct device *dev, struct vm_area_struct *vma, 384 void *cpu_addr, dma_addr_t dma_addr, size_t size) 385{ 386 vma->vm_page_prot = pgprot_dmacoherent(vma->vm_page_prot); 387 return dma_mmap(dev, vma, cpu_addr, dma_addr, size); 388} 389EXPORT_SYMBOL(dma_mmap_coherent); 390 391int dma_mmap_writecombine(struct device *dev, struct vm_area_struct *vma, 392 void *cpu_addr, dma_addr_t dma_addr, size_t size) 393{ 394 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot); 395 return dma_mmap(dev, vma, cpu_addr, dma_addr, size); 396} 397EXPORT_SYMBOL(dma_mmap_writecombine); 398 399/* 400 * free a page as defined by the above mapping. 401 * Must not be called with IRQs disabled. 402 */ 403void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle) 404{ 405 WARN_ON(irqs_disabled()); 406 407 if (dma_release_from_coherent(dev, get_order(size), cpu_addr)) 408 return; 409 410 size = PAGE_ALIGN(size); 411 412 if (!arch_is_coherent()) 413 __dma_free_remap(cpu_addr, size); 414 415 __dma_free_buffer(dma_to_page(dev, handle), size); 416} 417EXPORT_SYMBOL(dma_free_coherent); 418 419/* 420 * Make an area consistent for devices. 421 * Note: Drivers should NOT use this function directly, as it will break 422 * platforms with CONFIG_DMABOUNCE. 423 * Use the driver DMA support - see dma-mapping.h (dma_sync_*) 424 */ 425void ___dma_single_cpu_to_dev(const void *kaddr, size_t size, 426 enum dma_data_direction dir) 427{ 428 unsigned long paddr; 429 430 BUG_ON(!virt_addr_valid(kaddr) || !virt_addr_valid(kaddr + size - 1)); 431 432 dmac_map_area(kaddr, size, dir); 433 434 paddr = __pa(kaddr); 435 if (dir == DMA_FROM_DEVICE) { 436 outer_inv_range(paddr, paddr + size); 437 } else { 438 outer_clean_range(paddr, paddr + size); 439 } 440} 441EXPORT_SYMBOL(___dma_single_cpu_to_dev); 442 443void ___dma_single_dev_to_cpu(const void *kaddr, size_t size, 444 enum dma_data_direction dir) 445{ 446 BUG_ON(!virt_addr_valid(kaddr) || !virt_addr_valid(kaddr + size - 1)); 447 448 /* don't bother invalidating if DMA to device */ 449 if (dir != DMA_TO_DEVICE) { 450 unsigned long paddr = __pa(kaddr); 451 outer_inv_range(paddr, paddr + size); 452 } 453 454 dmac_unmap_area(kaddr, size, dir); 455} 456EXPORT_SYMBOL(___dma_single_dev_to_cpu); 457 458static void dma_cache_maint_page(struct page *page, unsigned long offset, 459 size_t size, enum dma_data_direction dir, 460 void (*op)(const void *, size_t, int)) 461{ 462 /* 463 * A single sg entry may refer to multiple physically contiguous 464 * pages. But we still need to process highmem pages individually. 465 * If highmem is not configured then the bulk of this loop gets 466 * optimized out. 467 */ 468 size_t left = size; 469 do { 470 size_t len = left; 471 void *vaddr; 472 473 if (PageHighMem(page)) { 474 if (len + offset > PAGE_SIZE) { 475 if (offset >= PAGE_SIZE) { 476 page += offset / PAGE_SIZE; 477 offset %= PAGE_SIZE; 478 } 479 len = PAGE_SIZE - offset; 480 } 481 vaddr = kmap_high_get(page); 482 if (vaddr) { 483 vaddr += offset; 484 op(vaddr, len, dir); 485 kunmap_high(page); 486 } else if (cache_is_vipt()) { 487 pte_t saved_pte; 488 vaddr = kmap_high_l1_vipt(page, &saved_pte); 489 op(vaddr + offset, len, dir); 490 kunmap_high_l1_vipt(page, saved_pte); 491 } 492 } else { 493 vaddr = page_address(page) + offset; 494 op(vaddr, len, dir); 495 } 496 offset = 0; 497 page++; 498 left -= len; 499 } while (left); 500} 501 502void BCMFASTPATH_HOST ___dma_page_cpu_to_dev(struct page *page, unsigned long off, 503 size_t size, enum dma_data_direction dir) 504{ 505 unsigned long paddr; 506 507 dma_cache_maint_page(page, off, size, dir, dmac_map_area); 508 509 paddr = page_to_phys(page) + off; 510 if (dir == DMA_FROM_DEVICE) { 511 outer_inv_range(paddr, paddr + size); 512 } else { 513 outer_clean_range(paddr, paddr + size); 514 } 515} 516EXPORT_SYMBOL(___dma_page_cpu_to_dev); 517 518void ___dma_page_dev_to_cpu(struct page *page, unsigned long off, 519 size_t size, enum dma_data_direction dir) 520{ 521 unsigned long paddr = page_to_phys(page) + off; 522 523 /* don't bother invalidating if DMA to device */ 524 if (dir != DMA_TO_DEVICE) 525 outer_inv_range(paddr, paddr + size); 526 527 dma_cache_maint_page(page, off, size, dir, dmac_unmap_area); 528#ifdef CONFIG_BCM47XX 529 /* 530 * Merged from Linux-2.6.37 531 * Mark the D-cache clean for this page to avoid extra flushing. 532 */ 533 if (dir != DMA_TO_DEVICE && off == 0 && size >= PAGE_SIZE) 534 set_bit(PG_dcache_clean, &page->flags); 535#endif /* CONFIG_BCM47XX */ 536} 537EXPORT_SYMBOL(___dma_page_dev_to_cpu); 538 539/** 540 * dma_map_sg - map a set of SG buffers for streaming mode DMA 541 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices 542 * @sg: list of buffers 543 * @nents: number of buffers to map 544 * @dir: DMA transfer direction 545 * 546 * Map a set of buffers described by scatterlist in streaming mode for DMA. 547 * This is the scatter-gather version of the dma_map_single interface. 548 * Here the scatter gather list elements are each tagged with the 549 * appropriate dma address and length. They are obtained via 550 * sg_dma_{address,length}. 551 * 552 * Device ownership issues as mentioned for dma_map_single are the same 553 * here. 554 */ 555int BCMFASTPATH_HOST dma_map_sg(struct device *dev, struct scatterlist *sg, int nents, 556 enum dma_data_direction dir) 557{ 558 struct scatterlist *s; 559 int i, j; 560 561 for_each_sg(sg, s, nents, i) { 562 s->dma_address = dma_map_page(dev, sg_page(s), s->offset, s->length, dir); 563 if (dma_mapping_error(dev, s->dma_address)) 564 goto bad_mapping; 565 } 566 return nents; 567 568 bad_mapping: 569 for_each_sg(sg, s, i, j) 570 dma_unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir); 571 return 0; 572} 573EXPORT_SYMBOL(dma_map_sg); 574 575/** 576 * dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg 577 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices 578 * @sg: list of buffers 579 * @nents: number of buffers to unmap (returned from dma_map_sg) 580 * @dir: DMA transfer direction (same as was passed to dma_map_sg) 581 * 582 * Unmap a set of streaming mode DMA translations. Again, CPU access 583 * rules concerning calls here are the same as for dma_unmap_single(). 584 */ 585void dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, 586 enum dma_data_direction dir) 587{ 588 struct scatterlist *s; 589 int i; 590 591 for_each_sg(sg, s, nents, i) 592 dma_unmap_page(dev, sg_dma_address(s), sg_dma_len(s), dir); 593} 594EXPORT_SYMBOL(dma_unmap_sg); 595 596/** 597 * dma_sync_sg_for_cpu 598 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices 599 * @sg: list of buffers 600 * @nents: number of buffers to map (returned from dma_map_sg) 601 * @dir: DMA transfer direction (same as was passed to dma_map_sg) 602 */ 603void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, 604 int nents, enum dma_data_direction dir) 605{ 606 struct scatterlist *s; 607 int i; 608 609 for_each_sg(sg, s, nents, i) { 610 if (!dmabounce_sync_for_cpu(dev, sg_dma_address(s), 0, 611 sg_dma_len(s), dir)) 612 continue; 613 614 __dma_page_dev_to_cpu(sg_page(s), s->offset, 615 s->length, dir); 616 } 617} 618EXPORT_SYMBOL(dma_sync_sg_for_cpu); 619 620/** 621 * dma_sync_sg_for_device 622 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices 623 * @sg: list of buffers 624 * @nents: number of buffers to map (returned from dma_map_sg) 625 * @dir: DMA transfer direction (same as was passed to dma_map_sg) 626 */ 627void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, 628 int nents, enum dma_data_direction dir) 629{ 630 struct scatterlist *s; 631 int i; 632 633 for_each_sg(sg, s, nents, i) { 634 if (!dmabounce_sync_for_device(dev, sg_dma_address(s), 0, 635 sg_dma_len(s), dir)) 636 continue; 637 638 __dma_page_cpu_to_dev(sg_page(s), s->offset, 639 s->length, dir); 640 } 641} 642EXPORT_SYMBOL(dma_sync_sg_for_device); 643