1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved.
4 */
5
6 #include <media/v4l2-mem2mem.h>
7
8 #include "iris_hfi_gen2.h"
9 #include "iris_hfi_gen2_defines.h"
10 #include "iris_hfi_gen2_packet.h"
11 #include "iris_vdec.h"
12 #include "iris_vpu_buffer.h"
13 #include "iris_vpu_common.h"
14
15 struct iris_hfi_gen2_core_hfi_range {
16 u32 begin;
17 u32 end;
18 int (*handle)(struct iris_core *core, struct iris_hfi_packet *pkt);
19 };
20
21 struct iris_hfi_gen2_inst_hfi_range {
22 u32 begin;
23 u32 end;
24 int (*handle)(struct iris_inst *inst, struct iris_hfi_packet *pkt);
25 };
26
27 struct iris_hfi_gen2_packet_handle {
28 enum hfi_buffer_type type;
29 int (*handle)(struct iris_inst *inst, struct iris_hfi_packet *pkt);
30 };
31
iris_hfi_gen2_buf_type_to_driver(enum hfi_buffer_type buf_type)32 static u32 iris_hfi_gen2_buf_type_to_driver(enum hfi_buffer_type buf_type)
33 {
34 switch (buf_type) {
35 case HFI_BUFFER_BITSTREAM:
36 return BUF_INPUT;
37 case HFI_BUFFER_RAW:
38 return BUF_OUTPUT;
39 case HFI_BUFFER_BIN:
40 return BUF_BIN;
41 case HFI_BUFFER_ARP:
42 return BUF_ARP;
43 case HFI_BUFFER_COMV:
44 return BUF_COMV;
45 case HFI_BUFFER_NON_COMV:
46 return BUF_NON_COMV;
47 case HFI_BUFFER_LINE:
48 return BUF_LINE;
49 case HFI_BUFFER_DPB:
50 return BUF_DPB;
51 case HFI_BUFFER_PERSIST:
52 return BUF_PERSIST;
53 default:
54 return 0;
55 }
56 }
57
iris_hfi_gen2_is_valid_hfi_buffer_type(u32 buffer_type)58 static bool iris_hfi_gen2_is_valid_hfi_buffer_type(u32 buffer_type)
59 {
60 switch (buffer_type) {
61 case HFI_BUFFER_BITSTREAM:
62 case HFI_BUFFER_RAW:
63 case HFI_BUFFER_BIN:
64 case HFI_BUFFER_ARP:
65 case HFI_BUFFER_COMV:
66 case HFI_BUFFER_NON_COMV:
67 case HFI_BUFFER_LINE:
68 case HFI_BUFFER_DPB:
69 case HFI_BUFFER_PERSIST:
70 case HFI_BUFFER_VPSS:
71 return true;
72 default:
73 return false;
74 }
75 }
76
iris_hfi_gen2_is_valid_hfi_port(u32 port,u32 buffer_type)77 static bool iris_hfi_gen2_is_valid_hfi_port(u32 port, u32 buffer_type)
78 {
79 if (port == HFI_PORT_NONE && buffer_type != HFI_BUFFER_PERSIST)
80 return false;
81
82 if (port != HFI_PORT_BITSTREAM && port != HFI_PORT_RAW)
83 return false;
84
85 return true;
86 }
87
iris_hfi_gen2_get_driver_buffer_flags(struct iris_inst * inst,u32 hfi_flags)88 static int iris_hfi_gen2_get_driver_buffer_flags(struct iris_inst *inst, u32 hfi_flags)
89 {
90 u32 keyframe = HFI_PICTURE_IDR | HFI_PICTURE_I | HFI_PICTURE_CRA | HFI_PICTURE_BLA;
91 struct iris_inst_hfi_gen2 *inst_hfi_gen2 = to_iris_inst_hfi_gen2(inst);
92 u32 driver_flags = 0;
93
94 if (inst_hfi_gen2->hfi_frame_info.picture_type & keyframe)
95 driver_flags |= V4L2_BUF_FLAG_KEYFRAME;
96 else if (inst_hfi_gen2->hfi_frame_info.picture_type & HFI_PICTURE_P)
97 driver_flags |= V4L2_BUF_FLAG_PFRAME;
98 else if (inst_hfi_gen2->hfi_frame_info.picture_type & HFI_PICTURE_B)
99 driver_flags |= V4L2_BUF_FLAG_BFRAME;
100
101 if (inst_hfi_gen2->hfi_frame_info.data_corrupt || inst_hfi_gen2->hfi_frame_info.overflow)
102 driver_flags |= V4L2_BUF_FLAG_ERROR;
103
104 if (hfi_flags & HFI_BUF_FW_FLAG_LAST ||
105 hfi_flags & HFI_BUF_FW_FLAG_PSC_LAST)
106 driver_flags |= V4L2_BUF_FLAG_LAST;
107
108 return driver_flags;
109 }
110
iris_hfi_gen2_validate_packet_payload(struct iris_hfi_packet * pkt)111 static bool iris_hfi_gen2_validate_packet_payload(struct iris_hfi_packet *pkt)
112 {
113 u32 payload_size = 0;
114
115 switch (pkt->payload_info) {
116 case HFI_PAYLOAD_U32:
117 case HFI_PAYLOAD_S32:
118 case HFI_PAYLOAD_Q16:
119 case HFI_PAYLOAD_U32_ENUM:
120 case HFI_PAYLOAD_32_PACKED:
121 payload_size = 4;
122 break;
123 case HFI_PAYLOAD_U64:
124 case HFI_PAYLOAD_S64:
125 case HFI_PAYLOAD_64_PACKED:
126 payload_size = 8;
127 break;
128 case HFI_PAYLOAD_STRUCTURE:
129 if (pkt->type == HFI_CMD_BUFFER)
130 payload_size = sizeof(struct iris_hfi_buffer);
131 break;
132 default:
133 payload_size = 0;
134 break;
135 }
136
137 if (pkt->size < sizeof(struct iris_hfi_packet) + payload_size)
138 return false;
139
140 return true;
141 }
142
iris_hfi_gen2_validate_packet(u8 * response_pkt,u8 * core_resp_pkt)143 static int iris_hfi_gen2_validate_packet(u8 *response_pkt, u8 *core_resp_pkt)
144 {
145 u8 *response_limit = core_resp_pkt + IFACEQ_CORE_PKT_SIZE;
146 u32 response_pkt_size = *(u32 *)response_pkt;
147
148 if (!response_pkt_size)
149 return -EINVAL;
150
151 if (response_pkt_size < sizeof(struct iris_hfi_packet))
152 return -EINVAL;
153
154 if (response_pkt + response_pkt_size > response_limit)
155 return -EINVAL;
156
157 return 0;
158 }
159
iris_hfi_gen2_validate_hdr_packet(struct iris_core * core,struct iris_hfi_header * hdr)160 static int iris_hfi_gen2_validate_hdr_packet(struct iris_core *core, struct iris_hfi_header *hdr)
161 {
162 struct iris_hfi_packet *packet;
163 int ret;
164 u8 *pkt;
165 u32 i;
166
167 if (hdr->size < sizeof(*hdr) + sizeof(*packet))
168 return -EINVAL;
169
170 pkt = (u8 *)((u8 *)hdr + sizeof(*hdr));
171
172 for (i = 0; i < hdr->num_packets; i++) {
173 packet = (struct iris_hfi_packet *)pkt;
174 ret = iris_hfi_gen2_validate_packet(pkt, core->response_packet);
175 if (ret)
176 return ret;
177
178 pkt += packet->size;
179 }
180
181 return 0;
182 }
183
iris_hfi_gen2_handle_session_info(struct iris_inst * inst,struct iris_hfi_packet * pkt)184 static int iris_hfi_gen2_handle_session_info(struct iris_inst *inst,
185 struct iris_hfi_packet *pkt)
186 {
187 struct iris_inst_hfi_gen2 *inst_hfi_gen2 = to_iris_inst_hfi_gen2(inst);
188 struct iris_core *core = inst->core;
189 int ret = 0;
190 char *info;
191
192 switch (pkt->type) {
193 case HFI_INFO_UNSUPPORTED:
194 info = "unsupported";
195 break;
196 case HFI_INFO_DATA_CORRUPT:
197 info = "data corrupt";
198 inst_hfi_gen2->hfi_frame_info.data_corrupt = 1;
199 break;
200 case HFI_INFO_BUFFER_OVERFLOW:
201 info = "buffer overflow";
202 inst_hfi_gen2->hfi_frame_info.overflow = 1;
203 break;
204 case HFI_INFO_HFI_FLAG_DRAIN_LAST:
205 info = "drain last flag";
206 ret = iris_inst_sub_state_change_drain_last(inst);
207 break;
208 case HFI_INFO_HFI_FLAG_PSC_LAST:
209 info = "drc last flag";
210 ret = iris_inst_sub_state_change_drc_last(inst);
211 break;
212 default:
213 info = "unknown";
214 break;
215 }
216
217 dev_dbg(core->dev, "session info received %#x: %s\n",
218 pkt->type, info);
219
220 return ret;
221 }
222
iris_hfi_gen2_handle_session_error(struct iris_inst * inst,struct iris_hfi_packet * pkt)223 static int iris_hfi_gen2_handle_session_error(struct iris_inst *inst,
224 struct iris_hfi_packet *pkt)
225 {
226 struct iris_core *core = inst->core;
227 char *error;
228
229 switch (pkt->type) {
230 case HFI_ERROR_MAX_SESSIONS:
231 error = "exceeded max sessions";
232 break;
233 case HFI_ERROR_UNKNOWN_SESSION:
234 error = "unknown session id";
235 break;
236 case HFI_ERROR_INVALID_STATE:
237 error = "invalid operation for current state";
238 break;
239 case HFI_ERROR_INSUFFICIENT_RESOURCES:
240 error = "insufficient resources";
241 break;
242 case HFI_ERROR_BUFFER_NOT_SET:
243 error = "internal buffers not set";
244 break;
245 case HFI_ERROR_FATAL:
246 error = "fatal error";
247 break;
248 case HFI_ERROR_STREAM_UNSUPPORTED:
249 error = "unsupported stream";
250 break;
251 default:
252 error = "unknown";
253 break;
254 }
255
256 dev_err(core->dev, "session error received %#x: %s\n", pkt->type, error);
257 iris_vb2_queue_error(inst);
258 iris_inst_change_state(inst, IRIS_INST_ERROR);
259
260 return 0;
261 }
262
iris_hfi_gen2_handle_system_error(struct iris_core * core,struct iris_hfi_packet * pkt)263 static int iris_hfi_gen2_handle_system_error(struct iris_core *core,
264 struct iris_hfi_packet *pkt)
265 {
266 struct iris_inst *instance;
267
268 dev_err(core->dev, "received system error of type %#x\n", pkt->type);
269
270 core->state = IRIS_CORE_ERROR;
271
272 mutex_lock(&core->lock);
273 list_for_each_entry(instance, &core->instances, list)
274 iris_inst_change_state(instance, IRIS_INST_ERROR);
275 mutex_unlock(&core->lock);
276
277 schedule_delayed_work(&core->sys_error_handler, msecs_to_jiffies(10));
278
279 return 0;
280 }
281
iris_hfi_gen2_handle_system_init(struct iris_core * core,struct iris_hfi_packet * pkt)282 static int iris_hfi_gen2_handle_system_init(struct iris_core *core,
283 struct iris_hfi_packet *pkt)
284 {
285 if (!(pkt->flags & HFI_FW_FLAGS_SUCCESS)) {
286 core->state = IRIS_CORE_ERROR;
287 return 0;
288 }
289
290 complete(&core->core_init_done);
291
292 return 0;
293 }
294
iris_hfi_gen2_handle_session_close(struct iris_inst * inst,struct iris_hfi_packet * pkt)295 static void iris_hfi_gen2_handle_session_close(struct iris_inst *inst,
296 struct iris_hfi_packet *pkt)
297 {
298 if (!(pkt->flags & HFI_FW_FLAGS_SUCCESS)) {
299 iris_inst_change_state(inst, IRIS_INST_ERROR);
300 return;
301 }
302
303 complete(&inst->completion);
304 }
305
iris_hfi_gen2_handle_input_buffer(struct iris_inst * inst,struct iris_hfi_buffer * buffer)306 static int iris_hfi_gen2_handle_input_buffer(struct iris_inst *inst,
307 struct iris_hfi_buffer *buffer)
308 {
309 struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
310 struct v4l2_m2m_buffer *m2m_buffer, *n;
311 struct iris_buffer *buf;
312 bool found = false;
313
314 v4l2_m2m_for_each_src_buf_safe(m2m_ctx, m2m_buffer, n) {
315 buf = to_iris_buffer(&m2m_buffer->vb);
316 if (buf->index == buffer->index) {
317 found = true;
318 break;
319 }
320 }
321 if (!found)
322 return -EINVAL;
323
324 if (!(buf->attr & BUF_ATTR_QUEUED))
325 return -EINVAL;
326
327 buf->attr &= ~BUF_ATTR_QUEUED;
328 buf->attr |= BUF_ATTR_DEQUEUED;
329
330 buf->flags = iris_hfi_gen2_get_driver_buffer_flags(inst, buffer->flags);
331
332 return 0;
333 }
334
iris_hfi_gen2_handle_output_buffer(struct iris_inst * inst,struct iris_hfi_buffer * hfi_buffer)335 static int iris_hfi_gen2_handle_output_buffer(struct iris_inst *inst,
336 struct iris_hfi_buffer *hfi_buffer)
337 {
338 struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
339 struct v4l2_m2m_buffer *m2m_buffer, *n;
340 struct iris_buffer *buf;
341 bool found = false;
342 int ret;
343
344 if (hfi_buffer->flags & HFI_BUF_FW_FLAG_LAST) {
345 ret = iris_inst_sub_state_change_drain_last(inst);
346 if (ret)
347 return ret;
348 }
349
350 if (hfi_buffer->flags & HFI_BUF_FW_FLAG_PSC_LAST) {
351 ret = iris_inst_sub_state_change_drc_last(inst);
352 if (ret)
353 return ret;
354 }
355
356 v4l2_m2m_for_each_dst_buf_safe(m2m_ctx, m2m_buffer, n) {
357 buf = to_iris_buffer(&m2m_buffer->vb);
358 if (buf->index == hfi_buffer->index &&
359 buf->device_addr == hfi_buffer->base_address &&
360 buf->data_offset == hfi_buffer->data_offset) {
361 found = true;
362 break;
363 }
364 }
365 if (!found)
366 return -EINVAL;
367
368 if (!(buf->attr & BUF_ATTR_QUEUED))
369 return -EINVAL;
370
371 buf->data_offset = hfi_buffer->data_offset;
372 buf->data_size = hfi_buffer->data_size;
373 buf->timestamp = hfi_buffer->timestamp;
374
375 buf->attr &= ~BUF_ATTR_QUEUED;
376 buf->attr |= BUF_ATTR_DEQUEUED;
377
378 buf->flags = iris_hfi_gen2_get_driver_buffer_flags(inst, hfi_buffer->flags);
379
380 return 0;
381 }
382
iris_hfi_gen2_handle_dequeue_buffers(struct iris_inst * inst)383 static void iris_hfi_gen2_handle_dequeue_buffers(struct iris_inst *inst)
384 {
385 struct v4l2_m2m_ctx *m2m_ctx = inst->m2m_ctx;
386 struct v4l2_m2m_buffer *buffer, *n;
387 struct iris_buffer *buf = NULL;
388
389 v4l2_m2m_for_each_src_buf_safe(m2m_ctx, buffer, n) {
390 buf = to_iris_buffer(&buffer->vb);
391 if (buf->attr & BUF_ATTR_DEQUEUED) {
392 buf->attr &= ~BUF_ATTR_DEQUEUED;
393 if (!(buf->attr & BUF_ATTR_BUFFER_DONE)) {
394 buf->attr |= BUF_ATTR_BUFFER_DONE;
395 iris_vb2_buffer_done(inst, buf);
396 }
397 }
398 }
399
400 v4l2_m2m_for_each_dst_buf_safe(m2m_ctx, buffer, n) {
401 buf = to_iris_buffer(&buffer->vb);
402 if (buf->attr & BUF_ATTR_DEQUEUED) {
403 buf->attr &= ~BUF_ATTR_DEQUEUED;
404 if (!(buf->attr & BUF_ATTR_BUFFER_DONE)) {
405 buf->attr |= BUF_ATTR_BUFFER_DONE;
406 iris_vb2_buffer_done(inst, buf);
407 }
408 }
409 }
410 }
411
iris_hfi_gen2_handle_release_internal_buffer(struct iris_inst * inst,struct iris_hfi_buffer * buffer)412 static int iris_hfi_gen2_handle_release_internal_buffer(struct iris_inst *inst,
413 struct iris_hfi_buffer *buffer)
414 {
415 u32 buf_type = iris_hfi_gen2_buf_type_to_driver(buffer->type);
416 struct iris_buffers *buffers = &inst->buffers[buf_type];
417 struct iris_buffer *buf, *iter;
418 bool found = false;
419 int ret = 0;
420
421 list_for_each_entry(iter, &buffers->list, list) {
422 if (iter->device_addr == buffer->base_address) {
423 found = true;
424 buf = iter;
425 break;
426 }
427 }
428 if (!found)
429 return -EINVAL;
430
431 buf->attr &= ~BUF_ATTR_QUEUED;
432 if (buf->attr & BUF_ATTR_PENDING_RELEASE)
433 ret = iris_destroy_internal_buffer(inst, buf);
434
435 return ret;
436 }
437
iris_hfi_gen2_handle_session_stop(struct iris_inst * inst,struct iris_hfi_packet * pkt)438 static int iris_hfi_gen2_handle_session_stop(struct iris_inst *inst,
439 struct iris_hfi_packet *pkt)
440 {
441 int ret = 0;
442
443 if (pkt->port == HFI_PORT_RAW)
444 ret = iris_inst_sub_state_change_pause(inst, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
445 else if (pkt->port == HFI_PORT_BITSTREAM)
446 ret = iris_inst_sub_state_change_pause(inst, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
447
448 complete(&inst->completion);
449
450 return ret;
451 }
452
iris_hfi_gen2_handle_session_buffer(struct iris_inst * inst,struct iris_hfi_packet * pkt)453 static int iris_hfi_gen2_handle_session_buffer(struct iris_inst *inst,
454 struct iris_hfi_packet *pkt)
455 {
456 struct iris_hfi_buffer *buffer;
457
458 if (pkt->payload_info == HFI_PAYLOAD_NONE)
459 return 0;
460
461 if (!iris_hfi_gen2_validate_packet_payload(pkt)) {
462 iris_inst_change_state(inst, IRIS_INST_ERROR);
463 return 0;
464 }
465
466 buffer = (struct iris_hfi_buffer *)((u8 *)pkt + sizeof(*pkt));
467 if (!iris_hfi_gen2_is_valid_hfi_buffer_type(buffer->type))
468 return 0;
469
470 if (!iris_hfi_gen2_is_valid_hfi_port(pkt->port, buffer->type))
471 return 0;
472
473 if (buffer->type == HFI_BUFFER_BITSTREAM)
474 return iris_hfi_gen2_handle_input_buffer(inst, buffer);
475 else if (buffer->type == HFI_BUFFER_RAW)
476 return iris_hfi_gen2_handle_output_buffer(inst, buffer);
477 else
478 return iris_hfi_gen2_handle_release_internal_buffer(inst, buffer);
479 }
480
iris_hfi_gen2_handle_session_drain(struct iris_inst * inst,struct iris_hfi_packet * pkt)481 static int iris_hfi_gen2_handle_session_drain(struct iris_inst *inst,
482 struct iris_hfi_packet *pkt)
483 {
484 int ret = 0;
485
486 if (!(pkt->flags & HFI_FW_FLAGS_SUCCESS)) {
487 iris_inst_change_state(inst, IRIS_INST_ERROR);
488 return 0;
489 }
490
491 if (inst->sub_state & IRIS_INST_SUB_DRAIN)
492 ret = iris_inst_change_sub_state(inst, 0, IRIS_INST_SUB_INPUT_PAUSE);
493
494 return ret;
495 }
496
iris_hfi_gen2_read_input_subcr_params(struct iris_inst * inst)497 static void iris_hfi_gen2_read_input_subcr_params(struct iris_inst *inst)
498 {
499 struct iris_inst_hfi_gen2 *inst_hfi_gen2 = to_iris_inst_hfi_gen2(inst);
500 struct v4l2_pix_format_mplane *pixmp_ip = &inst->fmt_src->fmt.pix_mp;
501 struct v4l2_pix_format_mplane *pixmp_op = &inst->fmt_dst->fmt.pix_mp;
502 u32 primaries, matrix_coeff, transfer_char;
503 struct hfi_subscription_params subsc_params;
504 u32 colour_description_present_flag;
505 u32 video_signal_type_present_flag;
506 struct iris_core *core = inst->core;
507 u32 full_range, width, height;
508 struct vb2_queue *dst_q;
509 struct v4l2_ctrl *ctrl;
510
511 subsc_params = inst_hfi_gen2->src_subcr_params;
512 width = (subsc_params.bitstream_resolution &
513 HFI_BITMASK_BITSTREAM_WIDTH) >> 16;
514 height = subsc_params.bitstream_resolution &
515 HFI_BITMASK_BITSTREAM_HEIGHT;
516
517 pixmp_ip->width = width;
518 pixmp_ip->height = height;
519
520 pixmp_op->width = ALIGN(width, 128);
521 pixmp_op->height = ALIGN(height, 32);
522 pixmp_op->plane_fmt[0].bytesperline = ALIGN(width, 128);
523 pixmp_op->plane_fmt[0].sizeimage = iris_get_buffer_size(inst, BUF_OUTPUT);
524
525 matrix_coeff = subsc_params.color_info & 0xFF;
526 transfer_char = (subsc_params.color_info & 0xFF00) >> 8;
527 primaries = (subsc_params.color_info & 0xFF0000) >> 16;
528 colour_description_present_flag =
529 (subsc_params.color_info & 0x1000000) >> 24;
530 full_range = (subsc_params.color_info & 0x2000000) >> 25;
531 video_signal_type_present_flag =
532 (subsc_params.color_info & 0x20000000) >> 29;
533
534 pixmp_op->colorspace = V4L2_COLORSPACE_DEFAULT;
535 pixmp_op->xfer_func = V4L2_XFER_FUNC_DEFAULT;
536 pixmp_op->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
537 pixmp_op->quantization = V4L2_QUANTIZATION_DEFAULT;
538
539 if (video_signal_type_present_flag) {
540 pixmp_op->quantization =
541 full_range ?
542 V4L2_QUANTIZATION_FULL_RANGE :
543 V4L2_QUANTIZATION_LIM_RANGE;
544 if (colour_description_present_flag) {
545 pixmp_op->colorspace =
546 iris_hfi_get_v4l2_color_primaries(primaries);
547 pixmp_op->xfer_func =
548 iris_hfi_get_v4l2_transfer_char(transfer_char);
549 pixmp_op->ycbcr_enc =
550 iris_hfi_get_v4l2_matrix_coefficients(matrix_coeff);
551 }
552 }
553
554 pixmp_ip->colorspace = pixmp_op->colorspace;
555 pixmp_ip->xfer_func = pixmp_op->xfer_func;
556 pixmp_ip->ycbcr_enc = pixmp_op->ycbcr_enc;
557 pixmp_ip->quantization = pixmp_op->quantization;
558
559 inst->crop.top = subsc_params.crop_offsets[0] & 0xFFFF;
560 inst->crop.left = (subsc_params.crop_offsets[0] >> 16) & 0xFFFF;
561 inst->crop.height = pixmp_ip->height -
562 (subsc_params.crop_offsets[1] & 0xFFFF) - inst->crop.top;
563 inst->crop.width = pixmp_ip->width -
564 ((subsc_params.crop_offsets[1] >> 16) & 0xFFFF) - inst->crop.left;
565
566 inst->fw_caps[PROFILE].value = subsc_params.profile;
567 inst->fw_caps[LEVEL].value = subsc_params.level;
568 inst->fw_caps[POC].value = subsc_params.pic_order_cnt;
569
570 if (subsc_params.bit_depth != BIT_DEPTH_8 ||
571 !(subsc_params.coded_frames & HFI_BITMASK_FRAME_MBS_ONLY_FLAG)) {
572 dev_err(core->dev, "unsupported content, bit depth: %x, pic_struct = %x\n",
573 subsc_params.bit_depth, subsc_params.coded_frames);
574 iris_inst_change_state(inst, IRIS_INST_ERROR);
575 }
576
577 inst->fw_min_count = subsc_params.fw_min_count;
578 inst->buffers[BUF_OUTPUT].min_count = iris_vpu_buf_count(inst, BUF_OUTPUT);
579 inst->buffers[BUF_OUTPUT].size = pixmp_op->plane_fmt[0].sizeimage;
580 ctrl = v4l2_ctrl_find(&inst->ctrl_handler, V4L2_CID_MIN_BUFFERS_FOR_CAPTURE);
581 if (ctrl)
582 v4l2_ctrl_s_ctrl(ctrl, inst->buffers[BUF_OUTPUT].min_count);
583
584 dst_q = v4l2_m2m_get_dst_vq(inst->m2m_ctx);
585 dst_q->min_reqbufs_allocation = inst->buffers[BUF_OUTPUT].min_count;
586 }
587
iris_hfi_gen2_handle_src_change(struct iris_inst * inst,struct iris_hfi_packet * pkt)588 static int iris_hfi_gen2_handle_src_change(struct iris_inst *inst,
589 struct iris_hfi_packet *pkt)
590 {
591 int ret;
592
593 if (pkt->port != HFI_PORT_BITSTREAM)
594 return 0;
595
596 ret = iris_inst_sub_state_change_drc(inst);
597 if (ret)
598 return ret;
599
600 iris_hfi_gen2_read_input_subcr_params(inst);
601 iris_vdec_src_change(inst);
602
603 return 0;
604 }
605
iris_hfi_gen2_handle_session_command(struct iris_inst * inst,struct iris_hfi_packet * pkt)606 static int iris_hfi_gen2_handle_session_command(struct iris_inst *inst,
607 struct iris_hfi_packet *pkt)
608 {
609 int ret = 0;
610
611 switch (pkt->type) {
612 case HFI_CMD_CLOSE:
613 iris_hfi_gen2_handle_session_close(inst, pkt);
614 break;
615 case HFI_CMD_STOP:
616 iris_hfi_gen2_handle_session_stop(inst, pkt);
617 break;
618 case HFI_CMD_BUFFER:
619 ret = iris_hfi_gen2_handle_session_buffer(inst, pkt);
620 break;
621 case HFI_CMD_SETTINGS_CHANGE:
622 ret = iris_hfi_gen2_handle_src_change(inst, pkt);
623 break;
624 case HFI_CMD_DRAIN:
625 ret = iris_hfi_gen2_handle_session_drain(inst, pkt);
626 break;
627 default:
628 break;
629 }
630
631 return ret;
632 }
633
iris_hfi_gen2_handle_session_property(struct iris_inst * inst,struct iris_hfi_packet * pkt)634 static int iris_hfi_gen2_handle_session_property(struct iris_inst *inst,
635 struct iris_hfi_packet *pkt)
636 {
637 struct iris_inst_hfi_gen2 *inst_hfi_gen2 = to_iris_inst_hfi_gen2(inst);
638
639 if (pkt->port != HFI_PORT_BITSTREAM)
640 return 0;
641
642 if (pkt->flags & HFI_FW_FLAGS_INFORMATION)
643 return 0;
644
645 switch (pkt->type) {
646 case HFI_PROP_BITSTREAM_RESOLUTION:
647 inst_hfi_gen2->src_subcr_params.bitstream_resolution = pkt->payload[0];
648 break;
649 case HFI_PROP_CROP_OFFSETS:
650 inst_hfi_gen2->src_subcr_params.crop_offsets[0] = pkt->payload[0];
651 inst_hfi_gen2->src_subcr_params.crop_offsets[1] = pkt->payload[1];
652 break;
653 case HFI_PROP_CODED_FRAMES:
654 inst_hfi_gen2->src_subcr_params.coded_frames = pkt->payload[0];
655 break;
656 case HFI_PROP_BUFFER_FW_MIN_OUTPUT_COUNT:
657 inst_hfi_gen2->src_subcr_params.fw_min_count = pkt->payload[0];
658 break;
659 case HFI_PROP_PIC_ORDER_CNT_TYPE:
660 inst_hfi_gen2->src_subcr_params.pic_order_cnt = pkt->payload[0];
661 break;
662 case HFI_PROP_SIGNAL_COLOR_INFO:
663 inst_hfi_gen2->src_subcr_params.color_info = pkt->payload[0];
664 break;
665 case HFI_PROP_PROFILE:
666 inst_hfi_gen2->src_subcr_params.profile = pkt->payload[0];
667 break;
668 case HFI_PROP_LEVEL:
669 inst_hfi_gen2->src_subcr_params.level = pkt->payload[0];
670 break;
671 case HFI_PROP_PICTURE_TYPE:
672 inst_hfi_gen2->hfi_frame_info.picture_type = pkt->payload[0];
673 break;
674 case HFI_PROP_NO_OUTPUT:
675 inst_hfi_gen2->hfi_frame_info.no_output = 1;
676 break;
677 case HFI_PROP_QUALITY_MODE:
678 case HFI_PROP_STAGE:
679 case HFI_PROP_PIPE:
680 default:
681 break;
682 }
683
684 return 0;
685 }
686
iris_hfi_gen2_handle_image_version_property(struct iris_core * core,struct iris_hfi_packet * pkt)687 static int iris_hfi_gen2_handle_image_version_property(struct iris_core *core,
688 struct iris_hfi_packet *pkt)
689 {
690 u8 *str_image_version = (u8 *)pkt + sizeof(*pkt);
691 u32 req_bytes = pkt->size - sizeof(*pkt);
692 char fw_version[IRIS_FW_VERSION_LENGTH];
693 u32 i;
694
695 if (req_bytes < IRIS_FW_VERSION_LENGTH - 1)
696 return -EINVAL;
697
698 for (i = 0; i < IRIS_FW_VERSION_LENGTH - 1; i++) {
699 if (str_image_version[i] != '\0')
700 fw_version[i] = str_image_version[i];
701 else
702 fw_version[i] = ' ';
703 }
704 fw_version[i] = '\0';
705 dev_dbg(core->dev, "firmware version: %s\n", fw_version);
706
707 return 0;
708 }
709
iris_hfi_gen2_handle_system_property(struct iris_core * core,struct iris_hfi_packet * pkt)710 static int iris_hfi_gen2_handle_system_property(struct iris_core *core,
711 struct iris_hfi_packet *pkt)
712 {
713 switch (pkt->type) {
714 case HFI_PROP_IMAGE_VERSION:
715 return iris_hfi_gen2_handle_image_version_property(core, pkt);
716 default:
717 return 0;
718 }
719 }
720
iris_hfi_gen2_handle_system_response(struct iris_core * core,struct iris_hfi_header * hdr)721 static int iris_hfi_gen2_handle_system_response(struct iris_core *core,
722 struct iris_hfi_header *hdr)
723 {
724 u8 *start_pkt = (u8 *)((u8 *)hdr + sizeof(*hdr));
725 struct iris_hfi_packet *packet;
726 u32 i, j;
727 u8 *pkt;
728 int ret;
729 static const struct iris_hfi_gen2_core_hfi_range range[] = {
730 {HFI_SYSTEM_ERROR_BEGIN, HFI_SYSTEM_ERROR_END, iris_hfi_gen2_handle_system_error },
731 {HFI_PROP_BEGIN, HFI_PROP_END, iris_hfi_gen2_handle_system_property },
732 {HFI_CMD_BEGIN, HFI_CMD_END, iris_hfi_gen2_handle_system_init },
733 };
734
735 for (i = 0; i < ARRAY_SIZE(range); i++) {
736 pkt = start_pkt;
737 for (j = 0; j < hdr->num_packets; j++) {
738 packet = (struct iris_hfi_packet *)pkt;
739 if (packet->flags & HFI_FW_FLAGS_SYSTEM_ERROR) {
740 ret = iris_hfi_gen2_handle_system_error(core, packet);
741 return ret;
742 }
743
744 if (packet->type > range[i].begin && packet->type < range[i].end) {
745 ret = range[i].handle(core, packet);
746 if (ret)
747 return ret;
748
749 if (packet->type > HFI_SYSTEM_ERROR_BEGIN &&
750 packet->type < HFI_SYSTEM_ERROR_END)
751 return 0;
752 }
753 pkt += packet->size;
754 }
755 }
756
757 return 0;
758 }
759
iris_hfi_gen2_init_src_change_param(struct iris_inst * inst)760 static void iris_hfi_gen2_init_src_change_param(struct iris_inst *inst)
761 {
762 struct iris_inst_hfi_gen2 *inst_hfi_gen2 = to_iris_inst_hfi_gen2(inst);
763 struct v4l2_pix_format_mplane *pixmp_ip = &inst->fmt_src->fmt.pix_mp;
764 struct v4l2_pix_format_mplane *pixmp_op = &inst->fmt_dst->fmt.pix_mp;
765 u32 bottom_offset = (pixmp_ip->height - inst->crop.height);
766 u32 right_offset = (pixmp_ip->width - inst->crop.width);
767 struct hfi_subscription_params *subsc_params;
768 u32 primaries, matrix_coeff, transfer_char;
769 u32 colour_description_present_flag = 0;
770 u32 video_signal_type_present_flag = 0;
771 u32 full_range, video_format = 0;
772 u32 left_offset = inst->crop.left;
773 u32 top_offset = inst->crop.top;
774
775 subsc_params = &inst_hfi_gen2->src_subcr_params;
776 subsc_params->bitstream_resolution =
777 pixmp_ip->width << 16 | pixmp_ip->height;
778 subsc_params->crop_offsets[0] =
779 left_offset << 16 | top_offset;
780 subsc_params->crop_offsets[1] =
781 right_offset << 16 | bottom_offset;
782 subsc_params->fw_min_count = inst->buffers[BUF_OUTPUT].min_count;
783
784 primaries = iris_hfi_gen2_get_color_primaries(pixmp_op->colorspace);
785 matrix_coeff = iris_hfi_gen2_get_matrix_coefficients(pixmp_op->ycbcr_enc);
786 transfer_char = iris_hfi_gen2_get_transfer_char(pixmp_op->xfer_func);
787 full_range = pixmp_op->quantization == V4L2_QUANTIZATION_FULL_RANGE ? 1 : 0;
788 subsc_params->color_info =
789 iris_hfi_gen2_get_color_info(matrix_coeff, transfer_char, primaries,
790 colour_description_present_flag,
791 full_range, video_format,
792 video_signal_type_present_flag);
793
794 subsc_params->profile = inst->fw_caps[PROFILE].value;
795 subsc_params->level = inst->fw_caps[LEVEL].value;
796 subsc_params->pic_order_cnt = inst->fw_caps[POC].value;
797 subsc_params->bit_depth = inst->fw_caps[BIT_DEPTH].value;
798 if (inst->fw_caps[CODED_FRAMES].value ==
799 CODED_FRAMES_PROGRESSIVE)
800 subsc_params->coded_frames = HFI_BITMASK_FRAME_MBS_ONLY_FLAG;
801 else
802 subsc_params->coded_frames = 0;
803 }
804
iris_hfi_gen2_handle_session_response(struct iris_core * core,struct iris_hfi_header * hdr)805 static int iris_hfi_gen2_handle_session_response(struct iris_core *core,
806 struct iris_hfi_header *hdr)
807 {
808 u8 *pkt = (u8 *)((u8 *)hdr + sizeof(*hdr));
809 struct iris_inst_hfi_gen2 *inst_hfi_gen2;
810 struct iris_hfi_packet *packet;
811 struct iris_inst *inst;
812 bool dequeue = false;
813 int ret = 0;
814 u32 i, j;
815 static const struct iris_hfi_gen2_inst_hfi_range range[] = {
816 {HFI_SESSION_ERROR_BEGIN, HFI_SESSION_ERROR_END,
817 iris_hfi_gen2_handle_session_error},
818 {HFI_INFORMATION_BEGIN, HFI_INFORMATION_END,
819 iris_hfi_gen2_handle_session_info},
820 {HFI_PROP_BEGIN, HFI_PROP_END,
821 iris_hfi_gen2_handle_session_property},
822 {HFI_CMD_BEGIN, HFI_CMD_END,
823 iris_hfi_gen2_handle_session_command },
824 };
825
826 inst = iris_get_instance(core, hdr->session_id);
827 if (!inst)
828 return -EINVAL;
829
830 mutex_lock(&inst->lock);
831 inst_hfi_gen2 = to_iris_inst_hfi_gen2(inst);
832 memset(&inst_hfi_gen2->hfi_frame_info, 0, sizeof(struct iris_hfi_frame_info));
833
834 for (i = 0; i < hdr->num_packets; i++) {
835 packet = (struct iris_hfi_packet *)pkt;
836 if (packet->type == HFI_CMD_SETTINGS_CHANGE) {
837 if (packet->port == HFI_PORT_BITSTREAM) {
838 iris_hfi_gen2_init_src_change_param(inst);
839 break;
840 }
841 }
842 pkt += packet->size;
843 }
844
845 pkt = (u8 *)((u8 *)hdr + sizeof(*hdr));
846 for (i = 0; i < ARRAY_SIZE(range); i++) {
847 pkt = (u8 *)((u8 *)hdr + sizeof(*hdr));
848 for (j = 0; j < hdr->num_packets; j++) {
849 packet = (struct iris_hfi_packet *)pkt;
850 if (packet->flags & HFI_FW_FLAGS_SESSION_ERROR)
851 iris_hfi_gen2_handle_session_error(inst, packet);
852
853 if (packet->type > range[i].begin && packet->type < range[i].end) {
854 dequeue |= (packet->type == HFI_CMD_BUFFER);
855 ret = range[i].handle(inst, packet);
856 if (ret)
857 iris_inst_change_state(inst, IRIS_INST_ERROR);
858 }
859 pkt += packet->size;
860 }
861 }
862
863 if (dequeue)
864 iris_hfi_gen2_handle_dequeue_buffers(inst);
865
866 mutex_unlock(&inst->lock);
867
868 return ret;
869 }
870
iris_hfi_gen2_handle_response(struct iris_core * core,void * response)871 static int iris_hfi_gen2_handle_response(struct iris_core *core, void *response)
872 {
873 struct iris_hfi_header *hdr = (struct iris_hfi_header *)response;
874 int ret;
875
876 ret = iris_hfi_gen2_validate_hdr_packet(core, hdr);
877 if (ret)
878 return iris_hfi_gen2_handle_system_error(core, NULL);
879
880 if (!hdr->session_id)
881 return iris_hfi_gen2_handle_system_response(core, hdr);
882 else
883 return iris_hfi_gen2_handle_session_response(core, hdr);
884 }
885
iris_hfi_gen2_flush_debug_queue(struct iris_core * core,u8 * packet)886 static void iris_hfi_gen2_flush_debug_queue(struct iris_core *core, u8 *packet)
887 {
888 struct hfi_debug_header *pkt;
889 u8 *log;
890
891 while (!iris_hfi_queue_dbg_read(core, packet)) {
892 pkt = (struct hfi_debug_header *)packet;
893
894 if (pkt->size < sizeof(*pkt))
895 continue;
896
897 if (pkt->size >= IFACEQ_CORE_PKT_SIZE)
898 continue;
899
900 packet[pkt->size] = '\0';
901 log = (u8 *)packet + sizeof(*pkt) + 1;
902 dev_dbg(core->dev, "%s", log);
903 }
904 }
905
iris_hfi_gen2_response_handler(struct iris_core * core)906 static void iris_hfi_gen2_response_handler(struct iris_core *core)
907 {
908 if (iris_vpu_watchdog(core, core->intr_status)) {
909 struct iris_hfi_packet pkt = {.type = HFI_SYS_ERROR_WD_TIMEOUT};
910
911 dev_err(core->dev, "cpu watchdog error received\n");
912 core->state = IRIS_CORE_ERROR;
913 iris_hfi_gen2_handle_system_error(core, &pkt);
914
915 return;
916 }
917
918 memset(core->response_packet, 0, sizeof(struct iris_hfi_header));
919 while (!iris_hfi_queue_msg_read(core, core->response_packet)) {
920 iris_hfi_gen2_handle_response(core, core->response_packet);
921 memset(core->response_packet, 0, sizeof(struct iris_hfi_header));
922 }
923
924 iris_hfi_gen2_flush_debug_queue(core, core->response_packet);
925 }
926
927 static const struct iris_hfi_response_ops iris_hfi_gen2_response_ops = {
928 .hfi_response_handler = iris_hfi_gen2_response_handler,
929 };
930
iris_hfi_gen2_response_ops_init(struct iris_core * core)931 void iris_hfi_gen2_response_ops_init(struct iris_core *core)
932 {
933 core->hfi_response_ops = &iris_hfi_gen2_response_ops;
934 }
935