dma.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * DMA implementation for Hexagon
  3. *
  4. * Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 and
  8. * only version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA.
  19. */
  20. #include <linux/dma-noncoherent.h>
  21. #include <linux/memblock.h>
  22. #include <linux/genalloc.h>
  23. #include <linux/module.h>
  24. #include <asm/page.h>
  25. static struct gen_pool *coherent_pool;
  26. /* Allocates from a pool of uncached memory that was reserved at boot time */
  27. void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_addr,
  28. gfp_t flag, unsigned long attrs)
  29. {
  30. void *ret;
  31. /*
  32. * Our max_low_pfn should have been backed off by 16MB in
  33. * mm/init.c to create DMA coherent space. Use that as the VA
  34. * for the pool.
  35. */
  36. if (coherent_pool == NULL) {
  37. coherent_pool = gen_pool_create(PAGE_SHIFT, -1);
  38. if (coherent_pool == NULL)
  39. panic("Can't create %s() memory pool!", __func__);
  40. else
  41. gen_pool_add(coherent_pool,
  42. (unsigned long)pfn_to_virt(max_low_pfn),
  43. hexagon_coherent_pool_size, -1);
  44. }
  45. ret = (void *) gen_pool_alloc(coherent_pool, size);
  46. if (ret) {
  47. memset(ret, 0, size);
  48. *dma_addr = (dma_addr_t) virt_to_phys(ret);
  49. } else
  50. *dma_addr = ~0;
  51. return ret;
  52. }
  53. void arch_dma_free(struct device *dev, size_t size, void *vaddr,
  54. dma_addr_t dma_addr, unsigned long attrs)
  55. {
  56. gen_pool_free(coherent_pool, (unsigned long) vaddr, size);
  57. }
  58. void arch_sync_dma_for_device(struct device *dev, phys_addr_t paddr,
  59. size_t size, enum dma_data_direction dir)
  60. {
  61. void *addr = phys_to_virt(paddr);
  62. switch (dir) {
  63. case DMA_TO_DEVICE:
  64. hexagon_clean_dcache_range((unsigned long) addr,
  65. (unsigned long) addr + size);
  66. break;
  67. case DMA_FROM_DEVICE:
  68. hexagon_inv_dcache_range((unsigned long) addr,
  69. (unsigned long) addr + size);
  70. break;
  71. case DMA_BIDIRECTIONAL:
  72. flush_dcache_range((unsigned long) addr,
  73. (unsigned long) addr + size);
  74. break;
  75. default:
  76. BUG();
  77. }
  78. }