GNU Linux-libre 5.19-rc6-gnu
[releases.git] / drivers / staging / media / hantro / hantro_hevc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Hantro VPU HEVC codec driver
4  *
5  * Copyright (C) 2020 Safran Passenger Innovations LLC
6  */
7
8 #include <linux/types.h>
9 #include <media/v4l2-mem2mem.h>
10
11 #include "hantro.h"
12 #include "hantro_hw.h"
13
14 #define VERT_FILTER_RAM_SIZE 8 /* bytes per pixel row */
15 /*
16  * BSD control data of current picture at tile border
17  * 128 bits per 4x4 tile = 128/(8*4) bytes per row
18  */
19 #define BSD_CTRL_RAM_SIZE 4 /* bytes per pixel row */
20 /* tile border coefficients of filter */
21 #define VERT_SAO_RAM_SIZE 48 /* bytes per pixel */
22
23 #define SCALING_LIST_SIZE (16 * 64)
24
25 #define MAX_TILE_COLS 20
26 #define MAX_TILE_ROWS 22
27
28 void hantro_hevc_ref_init(struct hantro_ctx *ctx)
29 {
30         struct hantro_hevc_dec_hw_ctx *hevc_dec = &ctx->hevc_dec;
31
32         hevc_dec->ref_bufs_used = 0;
33 }
34
35 dma_addr_t hantro_hevc_get_ref_buf(struct hantro_ctx *ctx,
36                                    int poc)
37 {
38         struct hantro_hevc_dec_hw_ctx *hevc_dec = &ctx->hevc_dec;
39         int i;
40
41         /* Find the reference buffer in already known ones */
42         for (i = 0;  i < NUM_REF_PICTURES; i++) {
43                 if (hevc_dec->ref_bufs_poc[i] == poc) {
44                         hevc_dec->ref_bufs_used |= 1 << i;
45                         return hevc_dec->ref_bufs[i].dma;
46                 }
47         }
48
49         return 0;
50 }
51
52 int hantro_hevc_add_ref_buf(struct hantro_ctx *ctx, int poc, dma_addr_t addr)
53 {
54         struct hantro_hevc_dec_hw_ctx *hevc_dec = &ctx->hevc_dec;
55         int i;
56
57         /* Add a new reference buffer */
58         for (i = 0; i < NUM_REF_PICTURES; i++) {
59                 if (!(hevc_dec->ref_bufs_used & 1 << i)) {
60                         hevc_dec->ref_bufs_used |= 1 << i;
61                         hevc_dec->ref_bufs_poc[i] = poc;
62                         hevc_dec->ref_bufs[i].dma = addr;
63                         return 0;
64                 }
65         }
66
67         return -EINVAL;
68 }
69
70 static int tile_buffer_reallocate(struct hantro_ctx *ctx)
71 {
72         struct hantro_dev *vpu = ctx->dev;
73         struct hantro_hevc_dec_hw_ctx *hevc_dec = &ctx->hevc_dec;
74         const struct hantro_hevc_dec_ctrls *ctrls = &ctx->hevc_dec.ctrls;
75         const struct v4l2_ctrl_hevc_pps *pps = ctrls->pps;
76         const struct v4l2_ctrl_hevc_sps *sps = ctrls->sps;
77         unsigned int num_tile_cols = pps->num_tile_columns_minus1 + 1;
78         unsigned int height64 = (sps->pic_height_in_luma_samples + 63) & ~63;
79         unsigned int size;
80
81         if (num_tile_cols <= 1 ||
82             num_tile_cols <= hevc_dec->num_tile_cols_allocated)
83                 return 0;
84
85         /* Need to reallocate due to tiles passed via PPS */
86         if (hevc_dec->tile_filter.cpu) {
87                 dma_free_coherent(vpu->dev, hevc_dec->tile_filter.size,
88                                   hevc_dec->tile_filter.cpu,
89                                   hevc_dec->tile_filter.dma);
90                 hevc_dec->tile_filter.cpu = NULL;
91         }
92
93         if (hevc_dec->tile_sao.cpu) {
94                 dma_free_coherent(vpu->dev, hevc_dec->tile_sao.size,
95                                   hevc_dec->tile_sao.cpu,
96                                   hevc_dec->tile_sao.dma);
97                 hevc_dec->tile_sao.cpu = NULL;
98         }
99
100         if (hevc_dec->tile_bsd.cpu) {
101                 dma_free_coherent(vpu->dev, hevc_dec->tile_bsd.size,
102                                   hevc_dec->tile_bsd.cpu,
103                                   hevc_dec->tile_bsd.dma);
104                 hevc_dec->tile_bsd.cpu = NULL;
105         }
106
107         size = VERT_FILTER_RAM_SIZE * height64 * (num_tile_cols - 1);
108         hevc_dec->tile_filter.cpu = dma_alloc_coherent(vpu->dev, size,
109                                                        &hevc_dec->tile_filter.dma,
110                                                        GFP_KERNEL);
111         if (!hevc_dec->tile_filter.cpu)
112                 goto err_free_tile_buffers;
113         hevc_dec->tile_filter.size = size;
114
115         size = VERT_SAO_RAM_SIZE * height64 * (num_tile_cols - 1);
116         hevc_dec->tile_sao.cpu = dma_alloc_coherent(vpu->dev, size,
117                                                     &hevc_dec->tile_sao.dma,
118                                                     GFP_KERNEL);
119         if (!hevc_dec->tile_sao.cpu)
120                 goto err_free_tile_buffers;
121         hevc_dec->tile_sao.size = size;
122
123         size = BSD_CTRL_RAM_SIZE * height64 * (num_tile_cols - 1);
124         hevc_dec->tile_bsd.cpu = dma_alloc_coherent(vpu->dev, size,
125                                                     &hevc_dec->tile_bsd.dma,
126                                                     GFP_KERNEL);
127         if (!hevc_dec->tile_bsd.cpu)
128                 goto err_free_tile_buffers;
129         hevc_dec->tile_bsd.size = size;
130
131         hevc_dec->num_tile_cols_allocated = num_tile_cols;
132
133         return 0;
134
135 err_free_tile_buffers:
136         if (hevc_dec->tile_filter.cpu)
137                 dma_free_coherent(vpu->dev, hevc_dec->tile_filter.size,
138                                   hevc_dec->tile_filter.cpu,
139                                   hevc_dec->tile_filter.dma);
140         hevc_dec->tile_filter.cpu = NULL;
141
142         if (hevc_dec->tile_sao.cpu)
143                 dma_free_coherent(vpu->dev, hevc_dec->tile_sao.size,
144                                   hevc_dec->tile_sao.cpu,
145                                   hevc_dec->tile_sao.dma);
146         hevc_dec->tile_sao.cpu = NULL;
147
148         if (hevc_dec->tile_bsd.cpu)
149                 dma_free_coherent(vpu->dev, hevc_dec->tile_bsd.size,
150                                   hevc_dec->tile_bsd.cpu,
151                                   hevc_dec->tile_bsd.dma);
152         hevc_dec->tile_bsd.cpu = NULL;
153
154         return -ENOMEM;
155 }
156
157 int hantro_hevc_dec_prepare_run(struct hantro_ctx *ctx)
158 {
159         struct hantro_hevc_dec_hw_ctx *hevc_ctx = &ctx->hevc_dec;
160         struct hantro_hevc_dec_ctrls *ctrls = &hevc_ctx->ctrls;
161         int ret;
162
163         hantro_start_prepare_run(ctx);
164
165         ctrls->decode_params =
166                 hantro_get_ctrl(ctx, V4L2_CID_MPEG_VIDEO_HEVC_DECODE_PARAMS);
167         if (WARN_ON(!ctrls->decode_params))
168                 return -EINVAL;
169
170         ctrls->scaling =
171                 hantro_get_ctrl(ctx, V4L2_CID_MPEG_VIDEO_HEVC_SCALING_MATRIX);
172         if (WARN_ON(!ctrls->scaling))
173                 return -EINVAL;
174
175         ctrls->sps =
176                 hantro_get_ctrl(ctx, V4L2_CID_MPEG_VIDEO_HEVC_SPS);
177         if (WARN_ON(!ctrls->sps))
178                 return -EINVAL;
179
180         ctrls->pps =
181                 hantro_get_ctrl(ctx, V4L2_CID_MPEG_VIDEO_HEVC_PPS);
182         if (WARN_ON(!ctrls->pps))
183                 return -EINVAL;
184
185         ret = tile_buffer_reallocate(ctx);
186         if (ret)
187                 return ret;
188
189         return 0;
190 }
191
192 void hantro_hevc_dec_exit(struct hantro_ctx *ctx)
193 {
194         struct hantro_dev *vpu = ctx->dev;
195         struct hantro_hevc_dec_hw_ctx *hevc_dec = &ctx->hevc_dec;
196
197         if (hevc_dec->tile_sizes.cpu)
198                 dma_free_coherent(vpu->dev, hevc_dec->tile_sizes.size,
199                                   hevc_dec->tile_sizes.cpu,
200                                   hevc_dec->tile_sizes.dma);
201         hevc_dec->tile_sizes.cpu = NULL;
202
203         if (hevc_dec->scaling_lists.cpu)
204                 dma_free_coherent(vpu->dev, hevc_dec->scaling_lists.size,
205                                   hevc_dec->scaling_lists.cpu,
206                                   hevc_dec->scaling_lists.dma);
207         hevc_dec->scaling_lists.cpu = NULL;
208
209         if (hevc_dec->tile_filter.cpu)
210                 dma_free_coherent(vpu->dev, hevc_dec->tile_filter.size,
211                                   hevc_dec->tile_filter.cpu,
212                                   hevc_dec->tile_filter.dma);
213         hevc_dec->tile_filter.cpu = NULL;
214
215         if (hevc_dec->tile_sao.cpu)
216                 dma_free_coherent(vpu->dev, hevc_dec->tile_sao.size,
217                                   hevc_dec->tile_sao.cpu,
218                                   hevc_dec->tile_sao.dma);
219         hevc_dec->tile_sao.cpu = NULL;
220
221         if (hevc_dec->tile_bsd.cpu)
222                 dma_free_coherent(vpu->dev, hevc_dec->tile_bsd.size,
223                                   hevc_dec->tile_bsd.cpu,
224                                   hevc_dec->tile_bsd.dma);
225         hevc_dec->tile_bsd.cpu = NULL;
226 }
227
228 int hantro_hevc_dec_init(struct hantro_ctx *ctx)
229 {
230         struct hantro_dev *vpu = ctx->dev;
231         struct hantro_hevc_dec_hw_ctx *hevc_dec = &ctx->hevc_dec;
232         unsigned int size;
233
234         memset(hevc_dec, 0, sizeof(*hevc_dec));
235
236         /*
237          * Maximum number of tiles times width and height (2 bytes each),
238          * rounding up to next 16 bytes boundary + one extra 16 byte
239          * chunk (HW guys wanted to have this).
240          */
241         size = round_up(MAX_TILE_COLS * MAX_TILE_ROWS * 4 * sizeof(u16) + 16, 16);
242         hevc_dec->tile_sizes.cpu = dma_alloc_coherent(vpu->dev, size,
243                                                       &hevc_dec->tile_sizes.dma,
244                                                       GFP_KERNEL);
245         if (!hevc_dec->tile_sizes.cpu)
246                 return -ENOMEM;
247
248         hevc_dec->tile_sizes.size = size;
249
250         hevc_dec->scaling_lists.cpu = dma_alloc_coherent(vpu->dev, SCALING_LIST_SIZE,
251                                                          &hevc_dec->scaling_lists.dma,
252                                                          GFP_KERNEL);
253         if (!hevc_dec->scaling_lists.cpu)
254                 return -ENOMEM;
255
256         hevc_dec->scaling_lists.size = SCALING_LIST_SIZE;
257
258         hantro_hevc_ref_init(ctx);
259
260         return 0;
261 }