pci-dma-nommu.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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/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. dma_addr_t dma_map_single(struct device *dev, void *ptr, size_t size,
  89. enum dma_data_direction direction)
  90. {
  91. BUG_ON(direction == DMA_NONE);
  92. frv_cache_wback_inv((unsigned long) ptr, (unsigned long) ptr + size);
  93. return virt_to_bus(ptr);
  94. }
  95. EXPORT_SYMBOL(dma_map_single);
  96. int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  97. enum dma_data_direction direction)
  98. {
  99. int i;
  100. for (i=0; i<nents; i++)
  101. frv_cache_wback_inv(sg_dma_address(&sg[i]),
  102. sg_dma_address(&sg[i]) + sg_dma_len(&sg[i]));
  103. BUG_ON(direction == DMA_NONE);
  104. return nents;
  105. }
  106. EXPORT_SYMBOL(dma_map_sg);
  107. dma_addr_t dma_map_page(struct device *dev, struct page *page, unsigned long offset,
  108. size_t size, enum dma_data_direction direction)
  109. {
  110. BUG_ON(direction == DMA_NONE);
  111. flush_dcache_page(page);
  112. return (dma_addr_t) page_to_phys(page) + offset;
  113. }
  114. EXPORT_SYMBOL(dma_map_page);