pci-dma-nommu.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* pci-dma-nommu.c: Dynamic DMA mapping support for the FRV
  2. *
  3. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Woodhouse (dwmw2@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 <asm/io.h>
  17. #if 1
  18. #define DMA_SRAM_START dma_coherent_mem_start
  19. #define DMA_SRAM_END dma_coherent_mem_end
  20. #else // Use video RAM on Matrox
  21. #define DMA_SRAM_START 0xe8900000
  22. #define DMA_SRAM_END 0xe8a00000
  23. #endif
  24. struct dma_alloc_record {
  25. struct list_head list;
  26. unsigned long ofs;
  27. unsigned long len;
  28. };
  29. static DEFINE_SPINLOCK(dma_alloc_lock);
  30. static LIST_HEAD(dma_alloc_list);
  31. void *dma_alloc_coherent(struct device *hwdev, size_t size, dma_addr_t *dma_handle, gfp_t gfp)
  32. {
  33. struct dma_alloc_record *new;
  34. struct list_head *this = &dma_alloc_list;
  35. unsigned long flags;
  36. unsigned long start = DMA_SRAM_START;
  37. unsigned long end;
  38. if (!DMA_SRAM_START) {
  39. printk("%s called without any DMA area reserved!\n", __func__);
  40. return NULL;
  41. }
  42. new = kmalloc(sizeof (*new), GFP_ATOMIC);
  43. if (!new)
  44. return NULL;
  45. /* Round up to a reasonable alignment */
  46. new->len = (size + 31) & ~31;
  47. spin_lock_irqsave(&dma_alloc_lock, flags);
  48. list_for_each (this, &dma_alloc_list) {
  49. struct dma_alloc_record *this_r = list_entry(this, struct dma_alloc_record, list);
  50. end = this_r->ofs;
  51. if (end - start >= size)
  52. goto gotone;
  53. start = this_r->ofs + this_r->len;
  54. }
  55. /* Reached end of list. */
  56. end = DMA_SRAM_END;
  57. this = &dma_alloc_list;
  58. if (end - start >= size) {
  59. gotone:
  60. new->ofs = start;
  61. list_add_tail(&new->list, this);
  62. spin_unlock_irqrestore(&dma_alloc_lock, flags);
  63. *dma_handle = start;
  64. return (void *)start;
  65. }
  66. kfree(new);
  67. spin_unlock_irqrestore(&dma_alloc_lock, flags);
  68. return NULL;
  69. }
  70. EXPORT_SYMBOL(dma_alloc_coherent);
  71. void dma_free_coherent(struct device *hwdev, size_t size, void *vaddr, dma_addr_t dma_handle)
  72. {
  73. struct dma_alloc_record *rec;
  74. unsigned long flags;
  75. spin_lock_irqsave(&dma_alloc_lock, flags);
  76. list_for_each_entry(rec, &dma_alloc_list, list) {
  77. if (rec->ofs == dma_handle) {
  78. list_del(&rec->list);
  79. kfree(rec);
  80. spin_unlock_irqrestore(&dma_alloc_lock, flags);
  81. return;
  82. }
  83. }
  84. spin_unlock_irqrestore(&dma_alloc_lock, flags);
  85. BUG();
  86. }
  87. EXPORT_SYMBOL(dma_free_coherent);
  88. /*
  89. * Map a single buffer of the indicated size for DMA in streaming mode.
  90. * The 32-bit bus address to use is returned.
  91. *
  92. * Once the device is given the dma address, the device owns this memory
  93. * until either pci_unmap_single or pci_dma_sync_single is performed.
  94. */
  95. dma_addr_t dma_map_single(struct device *dev, void *ptr, size_t size,
  96. enum dma_data_direction direction)
  97. {
  98. if (direction == DMA_NONE)
  99. BUG();
  100. frv_cache_wback_inv((unsigned long) ptr, (unsigned long) ptr + size);
  101. return virt_to_bus(ptr);
  102. }
  103. EXPORT_SYMBOL(dma_map_single);
  104. /*
  105. * Map a set of buffers described by scatterlist in streaming
  106. * mode for DMA. This is the scather-gather version of the
  107. * above pci_map_single interface. Here the scatter gather list
  108. * elements are each tagged with the appropriate dma address
  109. * and length. They are obtained via sg_dma_{address,length}(SG).
  110. *
  111. * NOTE: An implementation may be able to use a smaller number of
  112. * DMA address/length pairs than there are SG table elements.
  113. * (for example via virtual mapping capabilities)
  114. * The routine returns the number of addr/length pairs actually
  115. * used, at most nents.
  116. *
  117. * Device ownership issues as mentioned above for pci_map_single are
  118. * the same here.
  119. */
  120. int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  121. enum dma_data_direction direction)
  122. {
  123. int i;
  124. for (i=0; i<nents; i++)
  125. frv_cache_wback_inv(sg_dma_address(&sg[i]),
  126. sg_dma_address(&sg[i]) + sg_dma_len(&sg[i]));
  127. if (direction == DMA_NONE)
  128. BUG();
  129. return nents;
  130. }
  131. EXPORT_SYMBOL(dma_map_sg);