dma.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2009-2010 PetaLogix
  4. * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corporation
  5. *
  6. * Provide default implementations of the DMA mapping callbacks for
  7. * directly mapped busses.
  8. */
  9. #include <linux/device.h>
  10. #include <linux/dma-noncoherent.h>
  11. #include <linux/gfp.h>
  12. #include <linux/dma-debug.h>
  13. #include <linux/export.h>
  14. #include <linux/bug.h>
  15. #include <asm/cacheflush.h>
  16. static void __dma_sync(struct device *dev, phys_addr_t paddr, size_t size,
  17. enum dma_data_direction direction)
  18. {
  19. switch (direction) {
  20. case DMA_TO_DEVICE:
  21. case DMA_BIDIRECTIONAL:
  22. flush_dcache_range(paddr, paddr + size);
  23. break;
  24. case DMA_FROM_DEVICE:
  25. invalidate_dcache_range(paddr, paddr + size);
  26. break;
  27. default:
  28. BUG();
  29. }
  30. }
  31. void arch_sync_dma_for_device(struct device *dev, phys_addr_t paddr,
  32. size_t size, enum dma_data_direction dir)
  33. {
  34. __dma_sync(dev, paddr, size, dir);
  35. }
  36. void arch_sync_dma_for_cpu(struct device *dev, phys_addr_t paddr,
  37. size_t size, enum dma_data_direction dir)
  38. {
  39. __dma_sync(dev, paddr, size, dir);
  40. }
  41. int arch_dma_mmap(struct device *dev, struct vm_area_struct *vma,
  42. void *cpu_addr, dma_addr_t handle, size_t size,
  43. unsigned long attrs)
  44. {
  45. #ifdef CONFIG_MMU
  46. unsigned long user_count = vma_pages(vma);
  47. unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  48. unsigned long off = vma->vm_pgoff;
  49. unsigned long pfn;
  50. if (off >= count || user_count > (count - off))
  51. return -ENXIO;
  52. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  53. pfn = consistent_virt_to_pfn(cpu_addr);
  54. return remap_pfn_range(vma, vma->vm_start, pfn + off,
  55. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  56. #else
  57. return -ENXIO;
  58. #endif
  59. }