pci-dma.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <asm/io.h>
  23. #include <asm/cacheflush.h>
  24. /*
  25. * Note: We assume that the full memory space is always mapped to 'kseg'
  26. * Otherwise we have to use page attributes (not implemented).
  27. */
  28. void *
  29. dma_alloc_coherent(struct device *dev,size_t size,dma_addr_t *handle,gfp_t flag)
  30. {
  31. unsigned long ret;
  32. unsigned long uncached = 0;
  33. /* ignore region speicifiers */
  34. flag &= ~(__GFP_DMA | __GFP_HIGHMEM);
  35. if (dev == NULL || (dev->coherent_dma_mask < 0xffffffff))
  36. flag |= GFP_DMA;
  37. ret = (unsigned long)__get_free_pages(flag, get_order(size));
  38. if (ret == 0)
  39. return NULL;
  40. /* We currently don't support coherent memory outside KSEG */
  41. if (ret < XCHAL_KSEG_CACHED_VADDR
  42. || ret >= XCHAL_KSEG_CACHED_VADDR + XCHAL_KSEG_SIZE)
  43. BUG();
  44. if (ret != 0) {
  45. memset((void*) ret, 0, size);
  46. uncached = ret+XCHAL_KSEG_BYPASS_VADDR-XCHAL_KSEG_CACHED_VADDR;
  47. *handle = virt_to_bus((void*)ret);
  48. __flush_invalidate_dcache_range(ret, size);
  49. }
  50. return (void*)uncached;
  51. }
  52. void dma_free_coherent(struct device *hwdev, size_t size,
  53. void *vaddr, dma_addr_t dma_handle)
  54. {
  55. long addr=(long)vaddr+XCHAL_KSEG_CACHED_VADDR-XCHAL_KSEG_BYPASS_VADDR;
  56. if (addr < 0 || addr >= XCHAL_KSEG_SIZE)
  57. BUG();
  58. free_pages(addr, get_order(size));
  59. }
  60. void consistent_sync(void *vaddr, size_t size, int direction)
  61. {
  62. switch (direction) {
  63. case PCI_DMA_NONE:
  64. BUG();
  65. case PCI_DMA_FROMDEVICE: /* invalidate only */
  66. __invalidate_dcache_range((unsigned long)vaddr,
  67. (unsigned long)size);
  68. break;
  69. case PCI_DMA_TODEVICE: /* writeback only */
  70. case PCI_DMA_BIDIRECTIONAL: /* writeback and invalidate */
  71. __flush_invalidate_dcache_range((unsigned long)vaddr,
  72. (unsigned long)size);
  73. break;
  74. }
  75. }