pci-dma.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * arch/xtensa/kernel/pci-dma.c
  3. *
  4. * DMA coherent memory allocation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * Copyright (C) 2002 - 2005 Tensilica Inc.
  12. *
  13. * Based on version for i386.
  14. *
  15. * Chris Zankel <chris@zankel.net>
  16. * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
  17. */
  18. #include <linux/types.h>
  19. #include <linux/mm.h>
  20. #include <linux/string.h>
  21. #include <linux/pci.h>
  22. #include <linux/gfp.h>
  23. #include <linux/module.h>
  24. #include <asm/io.h>
  25. #include <asm/cacheflush.h>
  26. /*
  27. * Note: We assume that the full memory space is always mapped to 'kseg'
  28. * Otherwise we have to use page attributes (not implemented).
  29. */
  30. void *
  31. dma_alloc_coherent(struct device *dev,size_t size,dma_addr_t *handle,gfp_t flag)
  32. {
  33. unsigned long ret;
  34. unsigned long uncached = 0;
  35. /* ignore region speicifiers */
  36. flag &= ~(__GFP_DMA | __GFP_HIGHMEM);
  37. if (dev == NULL || (dev->coherent_dma_mask < 0xffffffff))
  38. flag |= GFP_DMA;
  39. ret = (unsigned long)__get_free_pages(flag, get_order(size));
  40. if (ret == 0)
  41. return NULL;
  42. /* We currently don't support coherent memory outside KSEG */
  43. BUG_ON(ret < XCHAL_KSEG_CACHED_VADDR ||
  44. ret > XCHAL_KSEG_CACHED_VADDR + XCHAL_KSEG_SIZE - 1);
  45. if (ret != 0) {
  46. memset((void*) ret, 0, size);
  47. uncached = ret+XCHAL_KSEG_BYPASS_VADDR-XCHAL_KSEG_CACHED_VADDR;
  48. *handle = virt_to_bus((void*)ret);
  49. __flush_invalidate_dcache_range(ret, size);
  50. }
  51. return (void*)uncached;
  52. }
  53. EXPORT_SYMBOL(dma_alloc_coherent);
  54. void dma_free_coherent(struct device *hwdev, size_t size,
  55. void *vaddr, dma_addr_t dma_handle)
  56. {
  57. unsigned long addr = (unsigned long)vaddr +
  58. XCHAL_KSEG_CACHED_VADDR - XCHAL_KSEG_BYPASS_VADDR;
  59. BUG_ON(addr < XCHAL_KSEG_CACHED_VADDR ||
  60. addr > XCHAL_KSEG_CACHED_VADDR + XCHAL_KSEG_SIZE - 1);
  61. free_pages(addr, get_order(size));
  62. }
  63. EXPORT_SYMBOL(dma_free_coherent);
  64. void consistent_sync(void *vaddr, size_t size, int direction)
  65. {
  66. switch (direction) {
  67. case PCI_DMA_NONE:
  68. BUG();
  69. case PCI_DMA_FROMDEVICE: /* invalidate only */
  70. __invalidate_dcache_range((unsigned long)vaddr,
  71. (unsigned long)size);
  72. break;
  73. case PCI_DMA_TODEVICE: /* writeback only */
  74. case PCI_DMA_BIDIRECTIONAL: /* writeback and invalidate */
  75. __flush_invalidate_dcache_range((unsigned long)vaddr,
  76. (unsigned long)size);
  77. break;
  78. }
  79. }
  80. EXPORT_SYMBOL(consistent_sync);