pci-dma.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* pci-dma.c: Dynamic DMA mapping support for the FRV CPUs that have MMUs
  2. *
  3. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/types.h>
  12. #include <linux/slab.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/list.h>
  15. #include <linux/pci.h>
  16. #include <linux/highmem.h>
  17. #include <linux/scatterlist.h>
  18. #include <asm/io.h>
  19. void *dma_alloc_coherent(struct device *hwdev, size_t size, dma_addr_t *dma_handle, gfp_t gfp)
  20. {
  21. void *ret;
  22. ret = consistent_alloc(gfp, size, dma_handle);
  23. if (ret)
  24. memset(ret, 0, size);
  25. return ret;
  26. }
  27. EXPORT_SYMBOL(dma_alloc_coherent);
  28. void dma_free_coherent(struct device *hwdev, size_t size, void *vaddr, dma_addr_t dma_handle)
  29. {
  30. consistent_free(vaddr);
  31. }
  32. EXPORT_SYMBOL(dma_free_coherent);
  33. /*
  34. * Map a single buffer of the indicated size for DMA in streaming mode.
  35. * The 32-bit bus address to use is returned.
  36. *
  37. * Once the device is given the dma address, the device owns this memory
  38. * until either pci_unmap_single or pci_dma_sync_single is performed.
  39. */
  40. dma_addr_t dma_map_single(struct device *dev, void *ptr, size_t size,
  41. enum dma_data_direction direction)
  42. {
  43. if (direction == DMA_NONE)
  44. BUG();
  45. frv_cache_wback_inv((unsigned long) ptr, (unsigned long) ptr + size);
  46. return virt_to_bus(ptr);
  47. }
  48. EXPORT_SYMBOL(dma_map_single);
  49. /*
  50. * Map a set of buffers described by scatterlist in streaming
  51. * mode for DMA. This is the scather-gather version of the
  52. * above pci_map_single interface. Here the scatter gather list
  53. * elements are each tagged with the appropriate dma address
  54. * and length. They are obtained via sg_dma_{address,length}(SG).
  55. *
  56. * NOTE: An implementation may be able to use a smaller number of
  57. * DMA address/length pairs than there are SG table elements.
  58. * (for example via virtual mapping capabilities)
  59. * The routine returns the number of addr/length pairs actually
  60. * used, at most nents.
  61. *
  62. * Device ownership issues as mentioned above for pci_map_single are
  63. * the same here.
  64. */
  65. int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  66. enum dma_data_direction direction)
  67. {
  68. unsigned long dampr2;
  69. void *vaddr;
  70. int i;
  71. if (direction == DMA_NONE)
  72. BUG();
  73. dampr2 = __get_DAMPR(2);
  74. for (i = 0; i < nents; i++) {
  75. vaddr = kmap_atomic(sg_page(&sg[i]), __KM_CACHE);
  76. frv_dcache_writeback((unsigned long) vaddr,
  77. (unsigned long) vaddr + PAGE_SIZE);
  78. }
  79. kunmap_atomic(vaddr, __KM_CACHE);
  80. if (dampr2) {
  81. __set_DAMPR(2, dampr2);
  82. __set_IAMPR(2, dampr2);
  83. }
  84. return nents;
  85. }
  86. EXPORT_SYMBOL(dma_map_sg);
  87. dma_addr_t dma_map_page(struct device *dev, struct page *page, unsigned long offset,
  88. size_t size, enum dma_data_direction direction)
  89. {
  90. BUG_ON(direction == DMA_NONE);
  91. flush_dcache_page(page);
  92. return (dma_addr_t) page_to_phys(page) + offset;
  93. }
  94. EXPORT_SYMBOL(dma_map_page);