dma-noop.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * lib/dma-noop.c
  3. *
  4. * Simple DMA noop-ops that map 1:1 with memory
  5. */
  6. #include <linux/export.h>
  7. #include <linux/mm.h>
  8. #include <linux/dma-mapping.h>
  9. #include <linux/scatterlist.h>
  10. static void *dma_noop_alloc(struct device *dev, size_t size,
  11. dma_addr_t *dma_handle, gfp_t gfp,
  12. unsigned long attrs)
  13. {
  14. void *ret;
  15. ret = (void *)__get_free_pages(gfp, get_order(size));
  16. if (ret)
  17. *dma_handle = virt_to_phys(ret);
  18. return ret;
  19. }
  20. static void dma_noop_free(struct device *dev, size_t size,
  21. void *cpu_addr, dma_addr_t dma_addr,
  22. unsigned long attrs)
  23. {
  24. free_pages((unsigned long)cpu_addr, get_order(size));
  25. }
  26. static dma_addr_t dma_noop_map_page(struct device *dev, struct page *page,
  27. unsigned long offset, size_t size,
  28. enum dma_data_direction dir,
  29. unsigned long attrs)
  30. {
  31. return page_to_phys(page) + offset;
  32. }
  33. static int dma_noop_map_sg(struct device *dev, struct scatterlist *sgl, int nents,
  34. enum dma_data_direction dir,
  35. unsigned long attrs)
  36. {
  37. int i;
  38. struct scatterlist *sg;
  39. for_each_sg(sgl, sg, nents, i) {
  40. void *va;
  41. BUG_ON(!sg_page(sg));
  42. va = sg_virt(sg);
  43. sg_dma_address(sg) = (dma_addr_t)virt_to_phys(va);
  44. sg_dma_len(sg) = sg->length;
  45. }
  46. return nents;
  47. }
  48. static int dma_noop_mapping_error(struct device *dev, dma_addr_t dma_addr)
  49. {
  50. return 0;
  51. }
  52. static int dma_noop_supported(struct device *dev, u64 mask)
  53. {
  54. return 1;
  55. }
  56. struct dma_map_ops dma_noop_ops = {
  57. .alloc = dma_noop_alloc,
  58. .free = dma_noop_free,
  59. .map_page = dma_noop_map_page,
  60. .map_sg = dma_noop_map_sg,
  61. .mapping_error = dma_noop_mapping_error,
  62. .dma_supported = dma_noop_supported,
  63. };
  64. EXPORT_SYMBOL(dma_noop_ops);