dma-nommu.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * DMA mapping support for platforms lacking IOMMUs.
  3. *
  4. * Copyright (C) 2009 Paul Mundt
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. */
  10. #include <linux/dma-mapping.h>
  11. #include <linux/io.h>
  12. static dma_addr_t nommu_map_page(struct device *dev, struct page *page,
  13. unsigned long offset, size_t size,
  14. enum dma_data_direction dir,
  15. struct dma_attrs *attrs)
  16. {
  17. dma_addr_t addr = page_to_phys(page) + offset;
  18. WARN_ON(size == 0);
  19. dma_cache_sync(dev, page_address(page) + offset, size, dir);
  20. return addr;
  21. }
  22. static int nommu_map_sg(struct device *dev, struct scatterlist *sg,
  23. int nents, enum dma_data_direction dir,
  24. struct dma_attrs *attrs)
  25. {
  26. struct scatterlist *s;
  27. int i;
  28. WARN_ON(nents == 0 || sg[0].length == 0);
  29. for_each_sg(sg, s, nents, i) {
  30. BUG_ON(!sg_page(s));
  31. dma_cache_sync(dev, sg_virt(s), s->length, dir);
  32. s->dma_address = sg_phys(s);
  33. s->dma_length = s->length;
  34. }
  35. return nents;
  36. }
  37. #ifdef CONFIG_DMA_NONCOHERENT
  38. static void nommu_sync_single(struct device *dev, dma_addr_t addr,
  39. size_t size, enum dma_data_direction dir)
  40. {
  41. dma_cache_sync(dev, phys_to_virt(addr), size, dir);
  42. }
  43. static void nommu_sync_sg(struct device *dev, struct scatterlist *sg,
  44. int nelems, enum dma_data_direction dir)
  45. {
  46. struct scatterlist *s;
  47. int i;
  48. for_each_sg(sg, s, nelems, i)
  49. dma_cache_sync(dev, sg_virt(s), s->length, dir);
  50. }
  51. #endif
  52. struct dma_map_ops nommu_dma_ops = {
  53. .alloc = dma_generic_alloc_coherent,
  54. .free = dma_generic_free_coherent,
  55. .map_page = nommu_map_page,
  56. .map_sg = nommu_map_sg,
  57. #ifdef CONFIG_DMA_NONCOHERENT
  58. .sync_single_for_device = nommu_sync_single,
  59. .sync_sg_for_device = nommu_sync_sg,
  60. #endif
  61. .is_phys = 1,
  62. };
  63. void __init no_iommu_init(void)
  64. {
  65. if (dma_ops)
  66. return;
  67. dma_ops = &nommu_dma_ops;
  68. }