video: adf: add memblock helper
[firefly-linux-kernel-4.4.55.git] / drivers / video / adf / adf_memblock.c
1 /*
2  * Copyright (C) 2013 Google, Inc.
3  *
4  * This software is licensed under the terms of the GNU General Public
5  * License version 2, as published by the Free Software Foundation, and
6  * may be copied, distributed, and modified under those terms.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  */
14
15 #include <linux/dma-buf.h>
16 #include <linux/highmem.h>
17 #include <linux/memblock.h>
18 #include <linux/slab.h>
19
20 struct adf_memblock_pdata {
21         phys_addr_t base;
22 };
23
24 static struct sg_table *adf_memblock_map(struct dma_buf_attachment *attach,
25                 enum dma_data_direction direction)
26 {
27         struct adf_memblock_pdata *pdata = attach->dmabuf->priv;
28         unsigned long pfn = PFN_DOWN(pdata->base);
29         struct page *page = pfn_to_page(pfn);
30         struct sg_table *table;
31         int ret;
32
33         table = kzalloc(sizeof(*table), GFP_KERNEL);
34         if (!table)
35                 return ERR_PTR(-ENOMEM);
36
37         ret = sg_alloc_table(table, 1, GFP_KERNEL);
38         if (ret < 0)
39                 goto err;
40
41         sg_set_page(table->sgl, page, attach->dmabuf->size, 0);
42         return table;
43
44 err:
45         kfree(table);
46         return ERR_PTR(ret);
47 }
48
49 static void adf_memblock_unmap(struct dma_buf_attachment *attach,
50                 struct sg_table *table, enum dma_data_direction direction)
51 {
52         sg_free_table(table);
53 }
54
55 static void __init_memblock adf_memblock_release(struct dma_buf *buf)
56 {
57         struct adf_memblock_pdata *pdata = buf->priv;
58         int err = memblock_free(pdata->base, buf->size);
59
60         if (err < 0)
61                 pr_warn("%s: freeing memblock failed: %d\n", __func__, err);
62         kfree(pdata);
63 }
64
65 static void *adf_memblock_do_kmap(struct dma_buf *buf, unsigned long pgoffset,
66                 bool atomic)
67 {
68         struct adf_memblock_pdata *pdata = buf->priv;
69         unsigned long pfn = PFN_DOWN(pdata->base) + pgoffset;
70         struct page *page = pfn_to_page(pfn);
71
72         if (atomic)
73                 return kmap_atomic(page);
74         else
75                 return kmap(page);
76 }
77
78 static void *adf_memblock_kmap_atomic(struct dma_buf *buf,
79                 unsigned long pgoffset)
80 {
81         return adf_memblock_do_kmap(buf, pgoffset, true);
82 }
83
84 static void adf_memblock_kunmap_atomic(struct dma_buf *buf,
85                 unsigned long pgoffset, void *vaddr)
86 {
87         kunmap_atomic(vaddr);
88 }
89
90 static void *adf_memblock_kmap(struct dma_buf *buf, unsigned long pgoffset)
91 {
92         return adf_memblock_do_kmap(buf, pgoffset, false);
93 }
94
95 static void adf_memblock_kunmap(struct dma_buf *buf, unsigned long pgoffset,
96                 void *vaddr)
97 {
98         kunmap(vaddr);
99 }
100
101 static int adf_memblock_mmap(struct dma_buf *buf, struct vm_area_struct *vma)
102 {
103         struct adf_memblock_pdata *pdata = buf->priv;
104
105         return remap_pfn_range(vma, vma->vm_start, PFN_DOWN(pdata->base),
106                         vma->vm_end - vma->vm_start, vma->vm_page_prot);
107 }
108
109 struct dma_buf_ops adf_memblock_ops = {
110         .map_dma_buf = adf_memblock_map,
111         .unmap_dma_buf = adf_memblock_unmap,
112         .release = adf_memblock_release,
113         .kmap_atomic = adf_memblock_kmap_atomic,
114         .kunmap_atomic = adf_memblock_kunmap_atomic,
115         .kmap = adf_memblock_kmap,
116         .kunmap = adf_memblock_kunmap,
117         .mmap = adf_memblock_mmap,
118 };
119
120 /**
121  * adf_memblock_export - export a memblock reserved area as a dma-buf
122  *
123  * @base: base physical address
124  * @size: memblock size
125  * @flags: mode flags for the dma-buf's file
126  *
127  * @base and @size must be page-aligned.
128  *
129  * Returns a dma-buf on success or ERR_PTR(-errno) on failure.
130  */
131 struct dma_buf *adf_memblock_export(phys_addr_t base, size_t size, int flags)
132 {
133         struct adf_memblock_pdata *pdata;
134         struct dma_buf *buf;
135
136         if (PAGE_ALIGN(base) != base || PAGE_ALIGN(size) != size)
137                 return ERR_PTR(-EINVAL);
138
139         pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
140         if (!pdata)
141                 return ERR_PTR(-ENOMEM);
142
143         pdata->base = base;
144         buf = dma_buf_export(pdata, &adf_memblock_ops, size, flags);
145         if (IS_ERR(buf))
146                 kfree(pdata);
147
148         return buf;
149 }