linux/lib/dma-virt.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 *      lib/dma-virt.c
   4 *
   5 * DMA operations that map to virtual addresses without flushing memory.
   6 */
   7#include <linux/export.h>
   8#include <linux/mm.h>
   9#include <linux/dma-mapping.h>
  10#include <linux/scatterlist.h>
  11
  12static void *dma_virt_alloc(struct device *dev, size_t size,
  13                            dma_addr_t *dma_handle, gfp_t gfp,
  14                            unsigned long attrs)
  15{
  16        void *ret;
  17
  18        ret = (void *)__get_free_pages(gfp, get_order(size));
  19        if (ret)
  20                *dma_handle = (uintptr_t)ret;
  21        return ret;
  22}
  23
  24static void dma_virt_free(struct device *dev, size_t size,
  25                          void *cpu_addr, dma_addr_t dma_addr,
  26                          unsigned long attrs)
  27{
  28        free_pages((unsigned long)cpu_addr, get_order(size));
  29}
  30
  31static dma_addr_t dma_virt_map_page(struct device *dev, struct page *page,
  32                                    unsigned long offset, size_t size,
  33                                    enum dma_data_direction dir,
  34                                    unsigned long attrs)
  35{
  36        return (uintptr_t)(page_address(page) + offset);
  37}
  38
  39static int dma_virt_map_sg(struct device *dev, struct scatterlist *sgl,
  40                           int nents, enum dma_data_direction dir,
  41                           unsigned long attrs)
  42{
  43        int i;
  44        struct scatterlist *sg;
  45
  46        for_each_sg(sgl, sg, nents, i) {
  47                BUG_ON(!sg_page(sg));
  48                sg_dma_address(sg) = (uintptr_t)sg_virt(sg);
  49                sg_dma_len(sg) = sg->length;
  50        }
  51
  52        return nents;
  53}
  54
  55const struct dma_map_ops dma_virt_ops = {
  56        .alloc                  = dma_virt_alloc,
  57        .free                   = dma_virt_free,
  58        .map_page               = dma_virt_map_page,
  59        .map_sg                 = dma_virt_map_sg,
  60};
  61EXPORT_SYMBOL(dma_virt_ops);
  62