dma.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. */
  17. #include <linux/dma-mapping.h>
  18. #include <linux/dma-debug.h>
  19. #include <linux/export.h>
  20. #include <asm/cacheflush.h>
  21. /*
  22. * Helpers for Coherent DMA API.
  23. */
  24. void *dma_alloc_noncoherent(struct device *dev, size_t size,
  25. dma_addr_t *dma_handle, gfp_t gfp)
  26. {
  27. void *paddr;
  28. /* This is linear addr (0x8000_0000 based) */
  29. paddr = alloc_pages_exact(size, gfp);
  30. if (!paddr)
  31. return NULL;
  32. /* This is bus address, platform dependent */
  33. *dma_handle = (dma_addr_t)paddr;
  34. return paddr;
  35. }
  36. EXPORT_SYMBOL(dma_alloc_noncoherent);
  37. void dma_free_noncoherent(struct device *dev, size_t size, void *vaddr,
  38. dma_addr_t dma_handle)
  39. {
  40. free_pages_exact((void *)dma_handle, size);
  41. }
  42. EXPORT_SYMBOL(dma_free_noncoherent);
  43. void *dma_alloc_coherent(struct device *dev, size_t size,
  44. dma_addr_t *dma_handle, gfp_t gfp)
  45. {
  46. void *paddr, *kvaddr;
  47. /* This is linear addr (0x8000_0000 based) */
  48. paddr = alloc_pages_exact(size, gfp);
  49. if (!paddr)
  50. return NULL;
  51. /* This is kernel Virtual address (0x7000_0000 based) */
  52. kvaddr = ioremap_nocache((unsigned long)paddr, size);
  53. if (kvaddr != NULL)
  54. memset(kvaddr, 0, size);
  55. /* This is bus address, platform dependent */
  56. *dma_handle = (dma_addr_t)paddr;
  57. /*
  58. * Evict any existing L1 and/or L2 lines for the backing page
  59. * in case it was used earlier as a normal "cached" page.
  60. * Yeah this bit us - STAR 9000898266
  61. *
  62. * Although core does call flush_cache_vmap(), it gets kvaddr hence
  63. * can't be used to efficiently flush L1 and/or L2 which need paddr
  64. * Currently flush_cache_vmap nukes the L1 cache completely which
  65. * will be optimized as a separate commit
  66. */
  67. dma_cache_wback_inv((unsigned long)paddr, size);
  68. return kvaddr;
  69. }
  70. EXPORT_SYMBOL(dma_alloc_coherent);
  71. void dma_free_coherent(struct device *dev, size_t size, void *kvaddr,
  72. dma_addr_t dma_handle)
  73. {
  74. iounmap((void __force __iomem *)kvaddr);
  75. free_pages_exact((void *)dma_handle, size);
  76. }
  77. EXPORT_SYMBOL(dma_free_coherent);
  78. /*
  79. * Helper for streaming DMA...
  80. */
  81. void __arc_dma_cache_sync(unsigned long paddr, size_t size,
  82. enum dma_data_direction dir)
  83. {
  84. __inline_dma_cache_sync(paddr, size, dir);
  85. }
  86. EXPORT_SYMBOL(__arc_dma_cache_sync);