pci-dma.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Dynamic DMA mapping support.
  3. */
  4. #include <linux/types.h>
  5. #include <linux/mm.h>
  6. #include <linux/string.h>
  7. #include <linux/pci.h>
  8. #include <linux/module.h>
  9. #include <asm/io.h>
  10. /* Map a set of buffers described by scatterlist in streaming
  11. * mode for DMA. This is the scatter-gather version of the
  12. * above pci_map_single interface. Here the scatter gather list
  13. * elements are each tagged with the appropriate dma address
  14. * and length. They are obtained via sg_dma_{address,length}(SG).
  15. *
  16. * NOTE: An implementation may be able to use a smaller number of
  17. * DMA address/length pairs than there are SG table elements.
  18. * (for example via virtual mapping capabilities)
  19. * The routine returns the number of addr/length pairs actually
  20. * used, at most nents.
  21. *
  22. * Device ownership issues as mentioned above for pci_map_single are
  23. * the same here.
  24. */
  25. int dma_map_sg(struct device *hwdev, struct scatterlist *sg,
  26. int nents, int direction)
  27. {
  28. int i;
  29. BUG_ON(direction == DMA_NONE);
  30. for (i = 0; i < nents; i++ ) {
  31. struct scatterlist *s = &sg[i];
  32. BUG_ON(!s->page);
  33. s->dma_address = virt_to_bus(page_address(s->page) +s->offset);
  34. s->dma_length = s->length;
  35. }
  36. return nents;
  37. }
  38. EXPORT_SYMBOL(dma_map_sg);
  39. /* Unmap a set of streaming mode DMA translations.
  40. * Again, cpu read rules concerning calls here are the same as for
  41. * pci_unmap_single() above.
  42. */
  43. void dma_unmap_sg(struct device *dev, struct scatterlist *sg,
  44. int nents, int dir)
  45. {
  46. int i;
  47. for (i = 0; i < nents; i++) {
  48. struct scatterlist *s = &sg[i];
  49. BUG_ON(s->page == NULL);
  50. BUG_ON(s->dma_address == 0);
  51. dma_unmap_single(dev, s->dma_address, s->dma_length, dir);
  52. }
  53. }
  54. EXPORT_SYMBOL(dma_unmap_sg);