pci-dma-nommu.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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@infradead.org)
  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/export.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/list.h>
  16. #include <linux/pci.h>
  17. #include <asm/io.h>
  18. #if 1
  19. #define DMA_SRAM_START dma_coherent_mem_start
  20. #define DMA_SRAM_END dma_coherent_mem_end
  21. #else // Use video RAM on Matrox
  22. #define DMA_SRAM_START 0xe8900000
  23. #define DMA_SRAM_END 0xe8a00000
  24. #endif
  25. struct dma_alloc_record {
  26. struct list_head list;
  27. unsigned long ofs;
  28. unsigned long len;
  29. };
  30. static DEFINE_SPINLOCK(dma_alloc_lock);
  31. static LIST_HEAD(dma_alloc_list);
  32. static void *frv_dma_alloc(struct device *hwdev, size_t size, dma_addr_t *dma_handle,
  33. gfp_t gfp, unsigned long attrs)
  34. {
  35. struct dma_alloc_record *new;
  36. struct list_head *this = &dma_alloc_list;
  37. unsigned long flags;
  38. unsigned long start = DMA_SRAM_START;
  39. unsigned long end;
  40. if (!DMA_SRAM_START) {
  41. printk("%s called without any DMA area reserved!\n", __func__);
  42. return NULL;
  43. }
  44. new = kmalloc(sizeof (*new), GFP_ATOMIC);
  45. if (!new)
  46. return NULL;
  47. /* Round up to a reasonable alignment */
  48. new->len = (size + 31) & ~31;
  49. spin_lock_irqsave(&dma_alloc_lock, flags);
  50. list_for_each (this, &dma_alloc_list) {
  51. struct dma_alloc_record *this_r = list_entry(this, struct dma_alloc_record, list);
  52. end = this_r->ofs;
  53. if (end - start >= size)
  54. goto gotone;
  55. start = this_r->ofs + this_r->len;
  56. }
  57. /* Reached end of list. */
  58. end = DMA_SRAM_END;
  59. this = &dma_alloc_list;
  60. if (end - start >= size) {
  61. gotone:
  62. new->ofs = start;
  63. list_add_tail(&new->list, this);
  64. spin_unlock_irqrestore(&dma_alloc_lock, flags);
  65. *dma_handle = start;
  66. return (void *)start;
  67. }
  68. kfree(new);
  69. spin_unlock_irqrestore(&dma_alloc_lock, flags);
  70. return NULL;
  71. }
  72. static void frv_dma_free(struct device *hwdev, size_t size, void *vaddr,
  73. dma_addr_t dma_handle, unsigned long attrs)
  74. {
  75. struct dma_alloc_record *rec;
  76. unsigned long flags;
  77. spin_lock_irqsave(&dma_alloc_lock, flags);
  78. list_for_each_entry(rec, &dma_alloc_list, list) {
  79. if (rec->ofs == dma_handle) {
  80. list_del(&rec->list);
  81. kfree(rec);
  82. spin_unlock_irqrestore(&dma_alloc_lock, flags);
  83. return;
  84. }
  85. }
  86. spin_unlock_irqrestore(&dma_alloc_lock, flags);
  87. BUG();
  88. }
  89. static int frv_dma_map_sg(struct device *dev, struct scatterlist *sglist,
  90. int nents, enum dma_data_direction direction,
  91. unsigned long attrs)
  92. {
  93. int i;
  94. struct scatterlist *sg;
  95. for_each_sg(sglist, sg, nents, i) {
  96. frv_cache_wback_inv(sg_dma_address(sg),
  97. sg_dma_address(sg) + sg_dma_len(sg));
  98. }
  99. BUG_ON(direction == DMA_NONE);
  100. return nents;
  101. }
  102. static dma_addr_t frv_dma_map_page(struct device *dev, struct page *page,
  103. unsigned long offset, size_t size,
  104. enum dma_data_direction direction, unsigned long attrs)
  105. {
  106. BUG_ON(direction == DMA_NONE);
  107. flush_dcache_page(page);
  108. return (dma_addr_t) page_to_phys(page) + offset;
  109. }
  110. static void frv_dma_sync_single_for_device(struct device *dev,
  111. dma_addr_t dma_handle, size_t size,
  112. enum dma_data_direction direction)
  113. {
  114. flush_write_buffers();
  115. }
  116. static void frv_dma_sync_sg_for_device(struct device *dev,
  117. struct scatterlist *sg, int nelems,
  118. enum dma_data_direction direction)
  119. {
  120. flush_write_buffers();
  121. }
  122. static int frv_dma_supported(struct device *dev, u64 mask)
  123. {
  124. /*
  125. * we fall back to GFP_DMA when the mask isn't all 1s,
  126. * so we can't guarantee allocations that must be
  127. * within a tighter range than GFP_DMA..
  128. */
  129. if (mask < 0x00ffffff)
  130. return 0;
  131. return 1;
  132. }
  133. struct dma_map_ops frv_dma_ops = {
  134. .alloc = frv_dma_alloc,
  135. .free = frv_dma_free,
  136. .map_page = frv_dma_map_page,
  137. .map_sg = frv_dma_map_sg,
  138. .sync_single_for_device = frv_dma_sync_single_for_device,
  139. .sync_sg_for_device = frv_dma_sync_sg_for_device,
  140. .dma_supported = frv_dma_supported,
  141. };
  142. EXPORT_SYMBOL(frv_dma_ops);