pci-dma-nommu.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. void *dma_alloc_coherent(struct device *hwdev, size_t size, dma_addr_t *dma_handle, gfp_t gfp)
  33. {
  34. struct dma_alloc_record *new;
  35. struct list_head *this = &dma_alloc_list;
  36. unsigned long flags;
  37. unsigned long start = DMA_SRAM_START;
  38. unsigned long end;
  39. if (!DMA_SRAM_START) {
  40. printk("%s called without any DMA area reserved!\n", __func__);
  41. return NULL;
  42. }
  43. new = kmalloc(sizeof (*new), GFP_ATOMIC);
  44. if (!new)
  45. return NULL;
  46. /* Round up to a reasonable alignment */
  47. new->len = (size + 31) & ~31;
  48. spin_lock_irqsave(&dma_alloc_lock, flags);
  49. list_for_each (this, &dma_alloc_list) {
  50. struct dma_alloc_record *this_r = list_entry(this, struct dma_alloc_record, list);
  51. end = this_r->ofs;
  52. if (end - start >= size)
  53. goto gotone;
  54. start = this_r->ofs + this_r->len;
  55. }
  56. /* Reached end of list. */
  57. end = DMA_SRAM_END;
  58. this = &dma_alloc_list;
  59. if (end - start >= size) {
  60. gotone:
  61. new->ofs = start;
  62. list_add_tail(&new->list, this);
  63. spin_unlock_irqrestore(&dma_alloc_lock, flags);
  64. *dma_handle = start;
  65. return (void *)start;
  66. }
  67. kfree(new);
  68. spin_unlock_irqrestore(&dma_alloc_lock, flags);
  69. return NULL;
  70. }
  71. EXPORT_SYMBOL(dma_alloc_coherent);
  72. void dma_free_coherent(struct device *hwdev, size_t size, void *vaddr, dma_addr_t dma_handle)
  73. {
  74. struct dma_alloc_record *rec;
  75. unsigned long flags;
  76. spin_lock_irqsave(&dma_alloc_lock, flags);
  77. list_for_each_entry(rec, &dma_alloc_list, list) {
  78. if (rec->ofs == dma_handle) {
  79. list_del(&rec->list);
  80. kfree(rec);
  81. spin_unlock_irqrestore(&dma_alloc_lock, flags);
  82. return;
  83. }
  84. }
  85. spin_unlock_irqrestore(&dma_alloc_lock, flags);
  86. BUG();
  87. }
  88. EXPORT_SYMBOL(dma_free_coherent);
  89. dma_addr_t dma_map_single(struct device *dev, void *ptr, size_t size,
  90. enum dma_data_direction direction)
  91. {
  92. BUG_ON(direction == DMA_NONE);
  93. frv_cache_wback_inv((unsigned long) ptr, (unsigned long) ptr + size);
  94. return virt_to_bus(ptr);
  95. }
  96. EXPORT_SYMBOL(dma_map_single);
  97. int dma_map_sg(struct device *dev, struct scatterlist *sglist, int nents,
  98. enum dma_data_direction direction)
  99. {
  100. int i;
  101. struct scatterlist *sg;
  102. for_each_sg(sglist, sg, nents, i) {
  103. frv_cache_wback_inv(sg_dma_address(sg),
  104. sg_dma_address(sg) + sg_dma_len(sg));
  105. }
  106. BUG_ON(direction == DMA_NONE);
  107. return nents;
  108. }
  109. EXPORT_SYMBOL(dma_map_sg);
  110. dma_addr_t dma_map_page(struct device *dev, struct page *page, unsigned long offset,
  111. size_t size, enum dma_data_direction direction)
  112. {
  113. BUG_ON(direction == DMA_NONE);
  114. flush_dcache_page(page);
  115. return (dma_addr_t) page_to_phys(page) + offset;
  116. }
  117. EXPORT_SYMBOL(dma_map_page);