dma-alloc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* dma-alloc.c: consistent DMA memory allocation
  2. *
  3. * Derived from arch/ppc/mm/cachemap.c
  4. *
  5. * PowerPC version derived from arch/arm/mm/consistent.c
  6. * Copyright (C) 2001 Dan Malek (dmalek@jlc.net)
  7. *
  8. * linux/arch/arm/mm/consistent.c
  9. *
  10. * Copyright (C) 2000 Russell King
  11. *
  12. * Consistent memory allocators. Used for DMA devices that want to
  13. * share uncached memory with the processor core. The function return
  14. * is the virtual address and 'dma_handle' is the physical address.
  15. * Mostly stolen from the ARM port, with some changes for PowerPC.
  16. * -- Dan
  17. * Modified for 36-bit support. -Matt
  18. *
  19. * This program is free software; you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License version 2 as
  21. * published by the Free Software Foundation.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/signal.h>
  25. #include <linux/sched.h>
  26. #include <linux/kernel.h>
  27. #include <linux/errno.h>
  28. #include <linux/string.h>
  29. #include <linux/types.h>
  30. #include <linux/ptrace.h>
  31. #include <linux/mman.h>
  32. #include <linux/mm.h>
  33. #include <linux/swap.h>
  34. #include <linux/stddef.h>
  35. #include <linux/vmalloc.h>
  36. #include <linux/init.h>
  37. #include <linux/pci.h>
  38. #include <asm/pgalloc.h>
  39. #include <asm/io.h>
  40. #include <asm/hardirq.h>
  41. #include <asm/mmu_context.h>
  42. #include <asm/pgtable.h>
  43. #include <asm/mmu.h>
  44. #include <asm/uaccess.h>
  45. #include <asm/smp.h>
  46. static int map_page(unsigned long va, unsigned long pa, pgprot_t prot)
  47. {
  48. pgd_t *pge;
  49. pud_t *pue;
  50. pmd_t *pme;
  51. pte_t *pte;
  52. int err = -ENOMEM;
  53. /* Use upper 10 bits of VA to index the first level map */
  54. pge = pgd_offset_k(va);
  55. pue = pud_offset(pge, va);
  56. pme = pmd_offset(pue, va);
  57. /* Use middle 10 bits of VA to index the second-level map */
  58. pte = pte_alloc_kernel(pme, va);
  59. if (pte != 0) {
  60. err = 0;
  61. set_pte(pte, mk_pte_phys(pa & PAGE_MASK, prot));
  62. }
  63. return err;
  64. }
  65. /*
  66. * This function will allocate the requested contiguous pages and
  67. * map them into the kernel's vmalloc() space. This is done so we
  68. * get unique mapping for these pages, outside of the kernel's 1:1
  69. * virtual:physical mapping. This is necessary so we can cover large
  70. * portions of the kernel with single large page TLB entries, and
  71. * still get unique uncached pages for consistent DMA.
  72. */
  73. void *consistent_alloc(gfp_t gfp, size_t size, dma_addr_t *dma_handle)
  74. {
  75. struct vm_struct *area;
  76. unsigned long page, va, pa;
  77. void *ret;
  78. int order, err, i;
  79. if (in_interrupt())
  80. BUG();
  81. /* only allocate page size areas */
  82. size = PAGE_ALIGN(size);
  83. order = get_order(size);
  84. page = __get_free_pages(gfp, order);
  85. if (!page) {
  86. BUG();
  87. return NULL;
  88. }
  89. /* allocate some common virtual space to map the new pages */
  90. area = get_vm_area(size, VM_ALLOC);
  91. if (area == 0) {
  92. free_pages(page, order);
  93. return NULL;
  94. }
  95. va = VMALLOC_VMADDR(area->addr);
  96. ret = (void *) va;
  97. /* this gives us the real physical address of the first page */
  98. *dma_handle = pa = virt_to_bus((void *) page);
  99. /* set refcount=1 on all pages in an order>0 allocation so that vfree() will actually free
  100. * all pages that were allocated.
  101. */
  102. if (order > 0) {
  103. struct page *rpage = virt_to_page(page);
  104. split_page(rpage, order);
  105. }
  106. err = 0;
  107. for (i = 0; i < size && err == 0; i += PAGE_SIZE)
  108. err = map_page(va + i, pa + i, PAGE_KERNEL_NOCACHE);
  109. if (err) {
  110. vfree((void *) va);
  111. return NULL;
  112. }
  113. /* we need to ensure that there are no cachelines in use, or worse dirty in this area
  114. * - can't do until after virtual address mappings are created
  115. */
  116. frv_cache_invalidate(va, va + size);
  117. return ret;
  118. }
  119. /*
  120. * free page(s) as defined by the above mapping.
  121. */
  122. void consistent_free(void *vaddr)
  123. {
  124. if (in_interrupt())
  125. BUG();
  126. vfree(vaddr);
  127. }
  128. /*
  129. * make an area consistent.
  130. */
  131. void consistent_sync(void *vaddr, size_t size, int direction)
  132. {
  133. unsigned long start = (unsigned long) vaddr;
  134. unsigned long end = start + size;
  135. switch (direction) {
  136. case PCI_DMA_NONE:
  137. BUG();
  138. case PCI_DMA_FROMDEVICE: /* invalidate only */
  139. frv_cache_invalidate(start, end);
  140. break;
  141. case PCI_DMA_TODEVICE: /* writeback only */
  142. frv_dcache_writeback(start, end);
  143. break;
  144. case PCI_DMA_BIDIRECTIONAL: /* writeback and invalidate */
  145. frv_dcache_writeback(start, end);
  146. break;
  147. }
  148. }
  149. /*
  150. * consistent_sync_page make a page are consistent. identical
  151. * to consistent_sync, but takes a struct page instead of a virtual address
  152. */
  153. void consistent_sync_page(struct page *page, unsigned long offset,
  154. size_t size, int direction)
  155. {
  156. void *start;
  157. start = page_address(page) + offset;
  158. consistent_sync(start, size, direction);
  159. }