]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - arch/x86/kernel/pci-dma_32.c
x86: move dma_coherent functions to pci-dma.c
[mirror_ubuntu-jammy-kernel.git] / arch / x86 / kernel / pci-dma_32.c
1 /*
2 * Dynamic DMA mapping support.
3 *
4 * On i386 there is no hardware dynamic DMA address translation,
5 * so consistent alloc/free are merely page allocation/freeing.
6 * The rest of the dynamic DMA mapping interface is implemented
7 * in asm/pci.h.
8 */
9
10 #include <linux/types.h>
11 #include <linux/mm.h>
12 #include <linux/string.h>
13 #include <linux/pci.h>
14 #include <linux/module.h>
15 #include <asm/io.h>
16
17 /* For i386, we make it point to the NULL address */
18 dma_addr_t bad_dma_address __read_mostly = 0x0;
19 EXPORT_SYMBOL(bad_dma_address);
20
21 void *dma_alloc_coherent(struct device *dev, size_t size,
22 dma_addr_t *dma_handle, gfp_t gfp)
23 {
24 void *ret;
25 struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
26 int order = get_order(size);
27 /* ignore region specifiers */
28 gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
29
30 if (mem) {
31 int page = bitmap_find_free_region(mem->bitmap, mem->size,
32 order);
33 if (page >= 0) {
34 *dma_handle = mem->device_base + (page << PAGE_SHIFT);
35 ret = mem->virt_base + (page << PAGE_SHIFT);
36 memset(ret, 0, size);
37 return ret;
38 }
39 if (mem->flags & DMA_MEMORY_EXCLUSIVE)
40 return NULL;
41 }
42
43 if (dev == NULL || (dev->coherent_dma_mask < 0xffffffff))
44 gfp |= GFP_DMA;
45
46 ret = (void *)__get_free_pages(gfp, order);
47
48 if (ret != NULL) {
49 memset(ret, 0, size);
50 *dma_handle = virt_to_phys(ret);
51 }
52 return ret;
53 }
54 EXPORT_SYMBOL(dma_alloc_coherent);
55
56 void dma_free_coherent(struct device *dev, size_t size,
57 void *vaddr, dma_addr_t dma_handle)
58 {
59 struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
60 int order = get_order(size);
61
62 WARN_ON(irqs_disabled()); /* for portability */
63 if (mem && vaddr >= mem->virt_base && vaddr < (mem->virt_base + (mem->size << PAGE_SHIFT))) {
64 int page = (vaddr - mem->virt_base) >> PAGE_SHIFT;
65
66 bitmap_release_region(mem->bitmap, page, order);
67 } else
68 free_pages((unsigned long)vaddr, order);
69 }
70 EXPORT_SYMBOL(dma_free_coherent);