Linux-libre 4.19.20-gnu
[librecmc/linux-libre.git] / drivers / media / platform / s5p-mfc / s5p_mfc_opr.c
1 /*
2  * drivers/media/platform/s5p-mfc/s5p_mfc_opr.c
3  *
4  * Samsung MFC (Multi Function Codec - FIMV) driver
5  * This file contains hw related functions.
6  *
7  * Kamil Debski, Copyright (c) 2012 Samsung Electronics Co., Ltd.
8  * http://www.samsung.com/
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  */
14
15 #include "s5p_mfc_debug.h"
16 #include "s5p_mfc_opr.h"
17 #include "s5p_mfc_opr_v5.h"
18 #include "s5p_mfc_opr_v6.h"
19
20 static struct s5p_mfc_hw_ops *s5p_mfc_ops;
21
22 void s5p_mfc_init_hw_ops(struct s5p_mfc_dev *dev)
23 {
24         if (IS_MFCV6_PLUS(dev)) {
25                 s5p_mfc_ops = s5p_mfc_init_hw_ops_v6();
26                 dev->warn_start = S5P_FIMV_ERR_WARNINGS_START_V6;
27         } else {
28                 s5p_mfc_ops = s5p_mfc_init_hw_ops_v5();
29                 dev->warn_start = S5P_FIMV_ERR_WARNINGS_START;
30         }
31         dev->mfc_ops = s5p_mfc_ops;
32 }
33
34 void s5p_mfc_init_regs(struct s5p_mfc_dev *dev)
35 {
36         if (IS_MFCV6_PLUS(dev))
37                 dev->mfc_regs = s5p_mfc_init_regs_v6_plus(dev);
38 }
39
40 int s5p_mfc_alloc_priv_buf(struct s5p_mfc_dev *dev, unsigned int mem_ctx,
41                            struct s5p_mfc_priv_buf *b)
42 {
43         unsigned int bits = dev->mem_size >> PAGE_SHIFT;
44         unsigned int count = b->size >> PAGE_SHIFT;
45         unsigned int align = (SZ_64K >> PAGE_SHIFT) - 1;
46         unsigned int start, offset;
47
48         mfc_debug(3, "Allocating priv: %zu\n", b->size);
49
50         if (dev->mem_virt) {
51                 start = bitmap_find_next_zero_area(dev->mem_bitmap, bits, 0, count, align);
52                 if (start > bits)
53                         goto no_mem;
54
55                 bitmap_set(dev->mem_bitmap, start, count);
56                 offset = start << PAGE_SHIFT;
57                 b->virt = dev->mem_virt + offset;
58                 b->dma = dev->mem_base + offset;
59         } else {
60                 struct device *mem_dev = dev->mem_dev[mem_ctx];
61                 dma_addr_t base = dev->dma_base[mem_ctx];
62
63                 b->ctx = mem_ctx;
64                 b->virt = dma_alloc_coherent(mem_dev, b->size, &b->dma, GFP_KERNEL);
65                 if (!b->virt)
66                         goto no_mem;
67                 if (b->dma < base) {
68                         mfc_err("Invalid memory configuration - buffer (%pad) is below base memory address(%pad)\n",
69                                 &b->dma, &base);
70                         dma_free_coherent(mem_dev, b->size, b->virt, b->dma);
71                         return -ENOMEM;
72                 }
73         }
74
75         mfc_debug(3, "Allocated addr %p %pad\n", b->virt, &b->dma);
76         return 0;
77 no_mem:
78         mfc_err("Allocating private buffer of size %zu failed\n", b->size);
79         return -ENOMEM;
80 }
81
82 int s5p_mfc_alloc_generic_buf(struct s5p_mfc_dev *dev, unsigned int mem_ctx,
83                            struct s5p_mfc_priv_buf *b)
84 {
85         struct device *mem_dev = dev->mem_dev[mem_ctx];
86
87         mfc_debug(3, "Allocating generic buf: %zu\n", b->size);
88
89         b->ctx = mem_ctx;
90         b->virt = dma_alloc_coherent(mem_dev, b->size, &b->dma, GFP_KERNEL);
91         if (!b->virt)
92                 goto no_mem;
93
94         mfc_debug(3, "Allocated addr %p %pad\n", b->virt, &b->dma);
95         return 0;
96 no_mem:
97         mfc_err("Allocating generic buffer of size %zu failed\n", b->size);
98         return -ENOMEM;
99 }
100
101 void s5p_mfc_release_priv_buf(struct s5p_mfc_dev *dev,
102                               struct s5p_mfc_priv_buf *b)
103 {
104         if (dev->mem_virt) {
105                 unsigned int start = (b->dma - dev->mem_base) >> PAGE_SHIFT;
106                 unsigned int count = b->size >> PAGE_SHIFT;
107
108                 bitmap_clear(dev->mem_bitmap, start, count);
109         } else {
110                 struct device *mem_dev = dev->mem_dev[b->ctx];
111
112                 dma_free_coherent(mem_dev, b->size, b->virt, b->dma);
113         }
114         b->virt = NULL;
115         b->dma = 0;
116         b->size = 0;
117 }
118
119 void s5p_mfc_release_generic_buf(struct s5p_mfc_dev *dev,
120                               struct s5p_mfc_priv_buf *b)
121 {
122         struct device *mem_dev = dev->mem_dev[b->ctx];
123         dma_free_coherent(mem_dev, b->size, b->virt, b->dma);
124         b->virt = NULL;
125         b->dma = 0;
126         b->size = 0;
127 }