dma.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 accessintg 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. * A platform/device can make it zero based, by over-riding
  17. * plat_{dma,kernel}_addr_to_{kernel,dma}
  18. */
  19. #include <linux/dma-mapping.h>
  20. #include <linux/dma-debug.h>
  21. #include <linux/export.h>
  22. #include <asm/cacheflush.h>
  23. /*
  24. * Helpers for Coherent DMA API.
  25. */
  26. void *dma_alloc_noncoherent(struct device *dev, size_t size,
  27. dma_addr_t *dma_handle, gfp_t gfp)
  28. {
  29. void *paddr;
  30. /* This is linear addr (0x8000_0000 based) */
  31. paddr = alloc_pages_exact(size, gfp);
  32. if (!paddr)
  33. return NULL;
  34. /* This is bus address, platform dependent */
  35. *dma_handle = plat_kernel_addr_to_dma(dev, paddr);
  36. return paddr;
  37. }
  38. EXPORT_SYMBOL(dma_alloc_noncoherent);
  39. void dma_free_noncoherent(struct device *dev, size_t size, void *vaddr,
  40. dma_addr_t dma_handle)
  41. {
  42. free_pages_exact((void *)plat_dma_addr_to_kernel(dev, dma_handle),
  43. size);
  44. }
  45. EXPORT_SYMBOL(dma_free_noncoherent);
  46. void *dma_alloc_coherent(struct device *dev, size_t size,
  47. dma_addr_t *dma_handle, gfp_t gfp)
  48. {
  49. void *paddr, *kvaddr;
  50. /* This is linear addr (0x8000_0000 based) */
  51. paddr = alloc_pages_exact(size, gfp);
  52. if (!paddr)
  53. return NULL;
  54. /* This is kernel Virtual address (0x7000_0000 based) */
  55. kvaddr = ioremap_nocache((unsigned long)paddr, size);
  56. if (kvaddr != NULL)
  57. memset(kvaddr, 0, size);
  58. /* This is bus address, platform dependent */
  59. *dma_handle = plat_kernel_addr_to_dma(dev, paddr);
  60. return kvaddr;
  61. }
  62. EXPORT_SYMBOL(dma_alloc_coherent);
  63. void dma_free_coherent(struct device *dev, size_t size, void *kvaddr,
  64. dma_addr_t dma_handle)
  65. {
  66. iounmap((void __force __iomem *)kvaddr);
  67. free_pages_exact((void *)plat_dma_addr_to_kernel(dev, dma_handle),
  68. size);
  69. }
  70. EXPORT_SYMBOL(dma_free_coherent);
  71. /*
  72. * Helper for streaming DMA...
  73. */
  74. void __arc_dma_cache_sync(unsigned long paddr, size_t size,
  75. enum dma_data_direction dir)
  76. {
  77. __inline_dma_cache_sync(paddr, size, dir);
  78. }
  79. EXPORT_SYMBOL(__arc_dma_cache_sync);