dma.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/cache.h>
  21. #include <asm/cacheflush.h>
  22. /*
  23. * Helpers for Coherent DMA API.
  24. */
  25. void *dma_alloc_noncoherent(struct device *dev, size_t size,
  26. dma_addr_t *dma_handle, gfp_t gfp)
  27. {
  28. void *paddr;
  29. /* This is linear addr (0x8000_0000 based) */
  30. paddr = alloc_pages_exact(size, gfp);
  31. if (!paddr)
  32. return NULL;
  33. /* This is bus address, platform dependent */
  34. *dma_handle = (dma_addr_t)paddr;
  35. return paddr;
  36. }
  37. EXPORT_SYMBOL(dma_alloc_noncoherent);
  38. void dma_free_noncoherent(struct device *dev, size_t size, void *vaddr,
  39. dma_addr_t dma_handle)
  40. {
  41. free_pages_exact((void *)dma_handle, size);
  42. }
  43. EXPORT_SYMBOL(dma_free_noncoherent);
  44. void *dma_alloc_coherent(struct device *dev, size_t size,
  45. dma_addr_t *dma_handle, gfp_t gfp)
  46. {
  47. void *paddr, *kvaddr;
  48. /*
  49. * IOC relies on all data (even coherent DMA data) being in cache
  50. * Thus allocate normal cached memory
  51. *
  52. * The gains with IOC are two pronged:
  53. * -For streaming data, elides needs for cache maintenance, saving
  54. * cycles in flush code, and bus bandwidth as all the lines of a
  55. * buffer need to be flushed out to memory
  56. * -For coherent data, Read/Write to buffers terminate early in cache
  57. * (vs. always going to memory - thus are faster)
  58. */
  59. if (is_isa_arcv2() && ioc_exists)
  60. return dma_alloc_noncoherent(dev, size, dma_handle, gfp);
  61. /* This is linear addr (0x8000_0000 based) */
  62. paddr = alloc_pages_exact(size, gfp);
  63. if (!paddr)
  64. return NULL;
  65. /* This is kernel Virtual address (0x7000_0000 based) */
  66. kvaddr = ioremap_nocache((unsigned long)paddr, size);
  67. if (kvaddr == NULL)
  68. return NULL;
  69. /* This is bus address, platform dependent */
  70. *dma_handle = (dma_addr_t)paddr;
  71. /*
  72. * Evict any existing L1 and/or L2 lines for the backing page
  73. * in case it was used earlier as a normal "cached" page.
  74. * Yeah this bit us - STAR 9000898266
  75. *
  76. * Although core does call flush_cache_vmap(), it gets kvaddr hence
  77. * can't be used to efficiently flush L1 and/or L2 which need paddr
  78. * Currently flush_cache_vmap nukes the L1 cache completely which
  79. * will be optimized as a separate commit
  80. */
  81. dma_cache_wback_inv((unsigned long)paddr, size);
  82. return kvaddr;
  83. }
  84. EXPORT_SYMBOL(dma_alloc_coherent);
  85. void dma_free_coherent(struct device *dev, size_t size, void *kvaddr,
  86. dma_addr_t dma_handle)
  87. {
  88. if (is_isa_arcv2() && ioc_exists)
  89. return dma_free_noncoherent(dev, size, kvaddr, dma_handle);
  90. iounmap((void __force __iomem *)kvaddr);
  91. free_pages_exact((void *)dma_handle, size);
  92. }
  93. EXPORT_SYMBOL(dma_free_coherent);
  94. /*
  95. * Helper for streaming DMA...
  96. */
  97. void __arc_dma_cache_sync(unsigned long paddr, size_t size,
  98. enum dma_data_direction dir)
  99. {
  100. __inline_dma_cache_sync(paddr, size, dir);
  101. }
  102. EXPORT_SYMBOL(__arc_dma_cache_sync);