Merge tag 'mmc-v4.9-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/ulfh/mmc
[cascardo/linux.git] / drivers / media / platform / sti / hva / hva-mem.c
1 /*
2  * Copyright (C) STMicroelectronics SA 2015
3  * Authors: Yannick Fertre <yannick.fertre@st.com>
4  *          Hugues Fruchet <hugues.fruchet@st.com>
5  * License terms:  GNU General Public License (GPL), version 2
6  */
7
8 #include "hva.h"
9 #include "hva-mem.h"
10
11 int hva_mem_alloc(struct hva_ctx *ctx, u32 size, const char *name,
12                   struct hva_buffer **buf)
13 {
14         struct device *dev = ctx_to_dev(ctx);
15         struct hva_buffer *b;
16         dma_addr_t paddr;
17         void *base;
18
19         b = devm_kzalloc(dev, sizeof(*b), GFP_KERNEL);
20         if (!b)
21                 return -ENOMEM;
22
23         base = dma_alloc_attrs(dev, size, &paddr, GFP_KERNEL | GFP_DMA,
24                                DMA_ATTR_WRITE_COMBINE);
25         if (!base) {
26                 dev_err(dev, "%s %s : dma_alloc_attrs failed for %s (size=%d)\n",
27                         ctx->name, __func__, name, size);
28                 devm_kfree(dev, b);
29                 return -ENOMEM;
30         }
31
32         b->size = size;
33         b->paddr = paddr;
34         b->vaddr = base;
35         b->name = name;
36
37         dev_dbg(dev,
38                 "%s allocate %d bytes of HW memory @(virt=%p, phy=%pad): %s\n",
39                 ctx->name, size, b->vaddr, &b->paddr, b->name);
40
41         /* return  hva buffer to user */
42         *buf = b;
43
44         return 0;
45 }
46
47 void hva_mem_free(struct hva_ctx *ctx, struct hva_buffer *buf)
48 {
49         struct device *dev = ctx_to_dev(ctx);
50
51         dev_dbg(dev,
52                 "%s free %d bytes of HW memory @(virt=%p, phy=%pad): %s\n",
53                 ctx->name, buf->size, buf->vaddr, &buf->paddr, buf->name);
54
55         dma_free_attrs(dev, buf->size, buf->vaddr, buf->paddr,
56                        DMA_ATTR_WRITE_COMBINE);
57
58         devm_kfree(dev, buf);
59 }