dma-alloc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <linux/hardirq.h>
  39. #include <linux/gfp.h>
  40. #include <asm/pgalloc.h>
  41. #include <asm/io.h>
  42. #include <asm/mmu_context.h>
  43. #include <asm/pgtable.h>
  44. #include <asm/mmu.h>
  45. #include <asm/uaccess.h>
  46. #include <asm/smp.h>
  47. static int map_page(unsigned long va, unsigned long pa, pgprot_t prot)
  48. {
  49. pgd_t *pge;
  50. pud_t *pue;
  51. pmd_t *pme;
  52. pte_t *pte;
  53. int err = -ENOMEM;
  54. /* Use upper 10 bits of VA to index the first level map */
  55. pge = pgd_offset_k(va);
  56. pue = pud_offset(pge, va);
  57. pme = pmd_offset(pue, va);
  58. /* Use middle 10 bits of VA to index the second-level map */
  59. pte = pte_alloc_kernel(pme, va);
  60. if (pte != 0) {
  61. err = 0;
  62. set_pte(pte, mk_pte_phys(pa & PAGE_MASK, prot));
  63. }
  64. return err;
  65. }
  66. /*
  67. * This function will allocate the requested contiguous pages and
  68. * map them into the kernel's vmalloc() space. This is done so we
  69. * get unique mapping for these pages, outside of the kernel's 1:1
  70. * virtual:physical mapping. This is necessary so we can cover large
  71. * portions of the kernel with single large page TLB entries, and
  72. * still get unique uncached pages for consistent DMA.
  73. */
  74. void *consistent_alloc(gfp_t gfp, size_t size, dma_addr_t *dma_handle)
  75. {
  76. struct vm_struct *area;
  77. unsigned long page, va, pa;
  78. void *ret;
  79. int order, err, i;
  80. if (in_interrupt())
  81. BUG();
  82. /* only allocate page size areas */
  83. size = PAGE_ALIGN(size);
  84. order = get_order(size);
  85. page = __get_free_pages(gfp, order);
  86. if (!page) {
  87. BUG();
  88. return NULL;
  89. }
  90. /* allocate some common virtual space to map the new pages */
  91. area = get_vm_area(size, VM_ALLOC);
  92. if (area == 0) {
  93. free_pages(page, order);
  94. return NULL;
  95. }
  96. va = VMALLOC_VMADDR(area->addr);
  97. ret = (void *) va;
  98. /* this gives us the real physical address of the first page */
  99. *dma_handle = pa = virt_to_bus((void *) page);
  100. /* set refcount=1 on all pages in an order>0 allocation so that vfree() will actually free
  101. * all pages that were allocated.
  102. */
  103. if (order > 0) {
  104. struct page *rpage = virt_to_page(page);
  105. split_page(rpage, order);
  106. }
  107. err = 0;
  108. for (i = 0; i < size && err == 0; i += PAGE_SIZE)
  109. err = map_page(va + i, pa + i, PAGE_KERNEL_NOCACHE);
  110. if (err) {
  111. vfree((void *) va);
  112. return NULL;
  113. }
  114. /* we need to ensure that there are no cachelines in use, or worse dirty in this area
  115. * - can't do until after virtual address mappings are created
  116. */
  117. frv_cache_invalidate(va, va + size);
  118. return ret;
  119. }
  120. /*
  121. * free page(s) as defined by the above mapping.
  122. */
  123. void consistent_free(void *vaddr)
  124. {
  125. if (in_interrupt())
  126. BUG();
  127. vfree(vaddr);
  128. }
  129. /*
  130. * make an area consistent.
  131. */
  132. void consistent_sync(void *vaddr, size_t size, int direction)
  133. {
  134. unsigned long start = (unsigned long) vaddr;
  135. unsigned long end = start + size;
  136. switch (direction) {
  137. case PCI_DMA_NONE:
  138. BUG();
  139. case PCI_DMA_FROMDEVICE: /* invalidate only */
  140. frv_cache_invalidate(start, end);
  141. break;
  142. case PCI_DMA_TODEVICE: /* writeback only */
  143. frv_dcache_writeback(start, end);
  144. break;
  145. case PCI_DMA_BIDIRECTIONAL: /* writeback and invalidate */
  146. frv_dcache_writeback(start, end);
  147. break;
  148. }
  149. }
  150. /*
  151. * consistent_sync_page make a page are consistent. identical
  152. * to consistent_sync, but takes a struct page instead of a virtual address
  153. */
  154. void consistent_sync_page(struct page *page, unsigned long offset,
  155. size_t size, int direction)
  156. {
  157. void *start;
  158. start = page_address(page) + offset;
  159. consistent_sync(start, size, direction);
  160. }