dma.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. /*
  9. * DMA Coherent API Notes
  10. *
  11. * I/O is inherently non-coherent on ARC. So a coherent DMA buffer is
  12. * implemented by accessing it using a kernel virtual address, with
  13. * Cache bit off in the TLB entry.
  14. *
  15. * The default DMA address == Phy address which is 0x8000_0000 based.
  16. */
  17. #include <linux/dma-noncoherent.h>
  18. #include <asm/cache.h>
  19. #include <asm/cacheflush.h>
  20. void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
  21. gfp_t gfp, unsigned long attrs)
  22. {
  23. unsigned long order = get_order(size);
  24. struct page *page;
  25. phys_addr_t paddr;
  26. void *kvaddr;
  27. int need_coh = 1, need_kvaddr = 0;
  28. page = alloc_pages(gfp, order);
  29. if (!page)
  30. return NULL;
  31. /*
  32. * IOC relies on all data (even coherent DMA data) being in cache
  33. * Thus allocate normal cached memory
  34. *
  35. * The gains with IOC are two pronged:
  36. * -For streaming data, elides need for cache maintenance, saving
  37. * cycles in flush code, and bus bandwidth as all the lines of a
  38. * buffer need to be flushed out to memory
  39. * -For coherent data, Read/Write to buffers terminate early in cache
  40. * (vs. always going to memory - thus are faster)
  41. */
  42. if ((is_isa_arcv2() && ioc_enable) ||
  43. (attrs & DMA_ATTR_NON_CONSISTENT))
  44. need_coh = 0;
  45. /*
  46. * - A coherent buffer needs MMU mapping to enforce non-cachability
  47. * - A highmem page needs a virtual handle (hence MMU mapping)
  48. * independent of cachability
  49. */
  50. if (PageHighMem(page) || need_coh)
  51. need_kvaddr = 1;
  52. /* This is linear addr (0x8000_0000 based) */
  53. paddr = page_to_phys(page);
  54. *dma_handle = paddr;
  55. /* This is kernel Virtual address (0x7000_0000 based) */
  56. if (need_kvaddr) {
  57. kvaddr = ioremap_nocache(paddr, size);
  58. if (kvaddr == NULL) {
  59. __free_pages(page, order);
  60. return NULL;
  61. }
  62. } else {
  63. kvaddr = (void *)(u32)paddr;
  64. }
  65. /*
  66. * Evict any existing L1 and/or L2 lines for the backing page
  67. * in case it was used earlier as a normal "cached" page.
  68. * Yeah this bit us - STAR 9000898266
  69. *
  70. * Although core does call flush_cache_vmap(), it gets kvaddr hence
  71. * can't be used to efficiently flush L1 and/or L2 which need paddr
  72. * Currently flush_cache_vmap nukes the L1 cache completely which
  73. * will be optimized as a separate commit
  74. */
  75. if (need_coh)
  76. dma_cache_wback_inv(paddr, size);
  77. return kvaddr;
  78. }
  79. void arch_dma_free(struct device *dev, size_t size, void *vaddr,
  80. dma_addr_t dma_handle, unsigned long attrs)
  81. {
  82. phys_addr_t paddr = dma_handle;
  83. struct page *page = virt_to_page(paddr);
  84. int is_non_coh = 1;
  85. is_non_coh = (attrs & DMA_ATTR_NON_CONSISTENT) ||
  86. (is_isa_arcv2() && ioc_enable);
  87. if (PageHighMem(page) || !is_non_coh)
  88. iounmap((void __force __iomem *)vaddr);
  89. __free_pages(page, get_order(size));
  90. }
  91. int arch_dma_mmap(struct device *dev, struct vm_area_struct *vma,
  92. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  93. unsigned long attrs)
  94. {
  95. unsigned long user_count = vma_pages(vma);
  96. unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  97. unsigned long pfn = __phys_to_pfn(dma_addr);
  98. unsigned long off = vma->vm_pgoff;
  99. int ret = -ENXIO;
  100. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  101. if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
  102. return ret;
  103. if (off < count && user_count <= (count - off)) {
  104. ret = remap_pfn_range(vma, vma->vm_start,
  105. pfn + off,
  106. user_count << PAGE_SHIFT,
  107. vma->vm_page_prot);
  108. }
  109. return ret;
  110. }
  111. void arch_sync_dma_for_device(struct device *dev, phys_addr_t paddr,
  112. size_t size, enum dma_data_direction dir)
  113. {
  114. dma_cache_wback(paddr, size);
  115. }
  116. void arch_sync_dma_for_cpu(struct device *dev, phys_addr_t paddr,
  117. size_t size, enum dma_data_direction dir)
  118. {
  119. dma_cache_inv(paddr, size);
  120. }