]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/media/platform/s5p-mfc/s5p_mfc.c
320124352e823e8389e49b5297a57a0d1ae23483
[mirror_ubuntu-bionic-kernel.git] / drivers / media / platform / s5p-mfc / s5p_mfc.c
1 /*
2 * Samsung S5P Multi Format Codec v 5.1
3 *
4 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
5 * Kamil Debski, <k.debski@samsung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/interrupt.h>
16 #include <linux/io.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/videodev2.h>
22 #include <media/v4l2-event.h>
23 #include <linux/workqueue.h>
24 #include <linux/of.h>
25 #include <linux/of_reserved_mem.h>
26 #include <media/videobuf2-v4l2.h>
27 #include "s5p_mfc_common.h"
28 #include "s5p_mfc_ctrl.h"
29 #include "s5p_mfc_debug.h"
30 #include "s5p_mfc_dec.h"
31 #include "s5p_mfc_enc.h"
32 #include "s5p_mfc_intr.h"
33 #include "s5p_mfc_iommu.h"
34 #include "s5p_mfc_opr.h"
35 #include "s5p_mfc_cmd.h"
36 #include "s5p_mfc_pm.h"
37
38 #define S5P_MFC_DEC_NAME "s5p-mfc-dec"
39 #define S5P_MFC_ENC_NAME "s5p-mfc-enc"
40
41 int mfc_debug_level;
42 module_param_named(debug, mfc_debug_level, int, S_IRUGO | S_IWUSR);
43 MODULE_PARM_DESC(debug, "Debug level - higher value produces more verbose messages");
44
45 /* Helper functions for interrupt processing */
46
47 /* Remove from hw execution round robin */
48 void clear_work_bit(struct s5p_mfc_ctx *ctx)
49 {
50 struct s5p_mfc_dev *dev = ctx->dev;
51
52 spin_lock(&dev->condlock);
53 __clear_bit(ctx->num, &dev->ctx_work_bits);
54 spin_unlock(&dev->condlock);
55 }
56
57 /* Add to hw execution round robin */
58 void set_work_bit(struct s5p_mfc_ctx *ctx)
59 {
60 struct s5p_mfc_dev *dev = ctx->dev;
61
62 spin_lock(&dev->condlock);
63 __set_bit(ctx->num, &dev->ctx_work_bits);
64 spin_unlock(&dev->condlock);
65 }
66
67 /* Remove from hw execution round robin */
68 void clear_work_bit_irqsave(struct s5p_mfc_ctx *ctx)
69 {
70 struct s5p_mfc_dev *dev = ctx->dev;
71 unsigned long flags;
72
73 spin_lock_irqsave(&dev->condlock, flags);
74 __clear_bit(ctx->num, &dev->ctx_work_bits);
75 spin_unlock_irqrestore(&dev->condlock, flags);
76 }
77
78 /* Add to hw execution round robin */
79 void set_work_bit_irqsave(struct s5p_mfc_ctx *ctx)
80 {
81 struct s5p_mfc_dev *dev = ctx->dev;
82 unsigned long flags;
83
84 spin_lock_irqsave(&dev->condlock, flags);
85 __set_bit(ctx->num, &dev->ctx_work_bits);
86 spin_unlock_irqrestore(&dev->condlock, flags);
87 }
88
89 int s5p_mfc_get_new_ctx(struct s5p_mfc_dev *dev)
90 {
91 unsigned long flags;
92 int ctx;
93
94 spin_lock_irqsave(&dev->condlock, flags);
95 ctx = dev->curr_ctx;
96 do {
97 ctx = (ctx + 1) % MFC_NUM_CONTEXTS;
98 if (ctx == dev->curr_ctx) {
99 if (!test_bit(ctx, &dev->ctx_work_bits))
100 ctx = -EAGAIN;
101 break;
102 }
103 } while (!test_bit(ctx, &dev->ctx_work_bits));
104 spin_unlock_irqrestore(&dev->condlock, flags);
105
106 return ctx;
107 }
108
109 /* Wake up context wait_queue */
110 static void wake_up_ctx(struct s5p_mfc_ctx *ctx, unsigned int reason,
111 unsigned int err)
112 {
113 ctx->int_cond = 1;
114 ctx->int_type = reason;
115 ctx->int_err = err;
116 wake_up(&ctx->queue);
117 }
118
119 /* Wake up device wait_queue */
120 static void wake_up_dev(struct s5p_mfc_dev *dev, unsigned int reason,
121 unsigned int err)
122 {
123 dev->int_cond = 1;
124 dev->int_type = reason;
125 dev->int_err = err;
126 wake_up(&dev->queue);
127 }
128
129 void s5p_mfc_cleanup_queue(struct list_head *lh, struct vb2_queue *vq)
130 {
131 struct s5p_mfc_buf *b;
132 int i;
133
134 while (!list_empty(lh)) {
135 b = list_entry(lh->next, struct s5p_mfc_buf, list);
136 for (i = 0; i < b->b->vb2_buf.num_planes; i++)
137 vb2_set_plane_payload(&b->b->vb2_buf, i, 0);
138 vb2_buffer_done(&b->b->vb2_buf, VB2_BUF_STATE_ERROR);
139 list_del(&b->list);
140 }
141 }
142
143 static void s5p_mfc_watchdog(unsigned long arg)
144 {
145 struct s5p_mfc_dev *dev = (struct s5p_mfc_dev *)arg;
146
147 if (test_bit(0, &dev->hw_lock))
148 atomic_inc(&dev->watchdog_cnt);
149 if (atomic_read(&dev->watchdog_cnt) >= MFC_WATCHDOG_CNT) {
150 /* This means that hw is busy and no interrupts were
151 * generated by hw for the Nth time of running this
152 * watchdog timer. This usually means a serious hw
153 * error. Now it is time to kill all instances and
154 * reset the MFC. */
155 mfc_err("Time out during waiting for HW\n");
156 schedule_work(&dev->watchdog_work);
157 }
158 dev->watchdog_timer.expires = jiffies +
159 msecs_to_jiffies(MFC_WATCHDOG_INTERVAL);
160 add_timer(&dev->watchdog_timer);
161 }
162
163 static void s5p_mfc_watchdog_worker(struct work_struct *work)
164 {
165 struct s5p_mfc_dev *dev;
166 struct s5p_mfc_ctx *ctx;
167 unsigned long flags;
168 int mutex_locked;
169 int i, ret;
170
171 dev = container_of(work, struct s5p_mfc_dev, watchdog_work);
172
173 mfc_err("Driver timeout error handling\n");
174 /* Lock the mutex that protects open and release.
175 * This is necessary as they may load and unload firmware. */
176 mutex_locked = mutex_trylock(&dev->mfc_mutex);
177 if (!mutex_locked)
178 mfc_err("Error: some instance may be closing/opening\n");
179 spin_lock_irqsave(&dev->irqlock, flags);
180
181 s5p_mfc_clock_off();
182
183 for (i = 0; i < MFC_NUM_CONTEXTS; i++) {
184 ctx = dev->ctx[i];
185 if (!ctx)
186 continue;
187 ctx->state = MFCINST_ERROR;
188 s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
189 s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
190 clear_work_bit(ctx);
191 wake_up_ctx(ctx, S5P_MFC_R2H_CMD_ERR_RET, 0);
192 }
193 clear_bit(0, &dev->hw_lock);
194 spin_unlock_irqrestore(&dev->irqlock, flags);
195
196 /* De-init MFC */
197 s5p_mfc_deinit_hw(dev);
198
199 /* Double check if there is at least one instance running.
200 * If no instance is in memory than no firmware should be present */
201 if (dev->num_inst > 0) {
202 ret = s5p_mfc_load_firmware(dev);
203 if (ret) {
204 mfc_err("Failed to reload FW\n");
205 goto unlock;
206 }
207 s5p_mfc_clock_on();
208 ret = s5p_mfc_init_hw(dev);
209 if (ret)
210 mfc_err("Failed to reinit FW\n");
211 }
212 unlock:
213 if (mutex_locked)
214 mutex_unlock(&dev->mfc_mutex);
215 }
216
217 static void s5p_mfc_handle_frame_all_extracted(struct s5p_mfc_ctx *ctx)
218 {
219 struct s5p_mfc_buf *dst_buf;
220 struct s5p_mfc_dev *dev = ctx->dev;
221
222 ctx->state = MFCINST_FINISHED;
223 ctx->sequence++;
224 while (!list_empty(&ctx->dst_queue)) {
225 dst_buf = list_entry(ctx->dst_queue.next,
226 struct s5p_mfc_buf, list);
227 mfc_debug(2, "Cleaning up buffer: %d\n",
228 dst_buf->b->vb2_buf.index);
229 vb2_set_plane_payload(&dst_buf->b->vb2_buf, 0, 0);
230 vb2_set_plane_payload(&dst_buf->b->vb2_buf, 1, 0);
231 list_del(&dst_buf->list);
232 dst_buf->flags |= MFC_BUF_FLAG_EOS;
233 ctx->dst_queue_cnt--;
234 dst_buf->b->sequence = (ctx->sequence++);
235
236 if (s5p_mfc_hw_call(dev->mfc_ops, get_pic_type_top, ctx) ==
237 s5p_mfc_hw_call(dev->mfc_ops, get_pic_type_bot, ctx))
238 dst_buf->b->field = V4L2_FIELD_NONE;
239 else
240 dst_buf->b->field = V4L2_FIELD_INTERLACED;
241 dst_buf->b->flags |= V4L2_BUF_FLAG_LAST;
242
243 ctx->dec_dst_flag &= ~(1 << dst_buf->b->vb2_buf.index);
244 vb2_buffer_done(&dst_buf->b->vb2_buf, VB2_BUF_STATE_DONE);
245 }
246 }
247
248 static void s5p_mfc_handle_frame_copy_time(struct s5p_mfc_ctx *ctx)
249 {
250 struct s5p_mfc_dev *dev = ctx->dev;
251 struct s5p_mfc_buf *dst_buf, *src_buf;
252 size_t dec_y_addr;
253 unsigned int frame_type;
254
255 /* Make sure we actually have a new frame before continuing. */
256 frame_type = s5p_mfc_hw_call(dev->mfc_ops, get_dec_frame_type, dev);
257 if (frame_type == S5P_FIMV_DECODE_FRAME_SKIPPED)
258 return;
259 dec_y_addr = s5p_mfc_hw_call(dev->mfc_ops, get_dec_y_adr, dev);
260
261 /* Copy timestamp / timecode from decoded src to dst and set
262 appropriate flags. */
263 src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
264 list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
265 if (vb2_dma_contig_plane_dma_addr(&dst_buf->b->vb2_buf, 0)
266 == dec_y_addr) {
267 dst_buf->b->timecode =
268 src_buf->b->timecode;
269 dst_buf->b->vb2_buf.timestamp =
270 src_buf->b->vb2_buf.timestamp;
271 dst_buf->b->flags &=
272 ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
273 dst_buf->b->flags |=
274 src_buf->b->flags
275 & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
276 switch (frame_type) {
277 case S5P_FIMV_DECODE_FRAME_I_FRAME:
278 dst_buf->b->flags |=
279 V4L2_BUF_FLAG_KEYFRAME;
280 break;
281 case S5P_FIMV_DECODE_FRAME_P_FRAME:
282 dst_buf->b->flags |=
283 V4L2_BUF_FLAG_PFRAME;
284 break;
285 case S5P_FIMV_DECODE_FRAME_B_FRAME:
286 dst_buf->b->flags |=
287 V4L2_BUF_FLAG_BFRAME;
288 break;
289 default:
290 /* Don't know how to handle
291 S5P_FIMV_DECODE_FRAME_OTHER_FRAME. */
292 mfc_debug(2, "Unexpected frame type: %d\n",
293 frame_type);
294 }
295 break;
296 }
297 }
298 }
299
300 static void s5p_mfc_handle_frame_new(struct s5p_mfc_ctx *ctx, unsigned int err)
301 {
302 struct s5p_mfc_dev *dev = ctx->dev;
303 struct s5p_mfc_buf *dst_buf;
304 size_t dspl_y_addr;
305 unsigned int frame_type;
306
307 dspl_y_addr = s5p_mfc_hw_call(dev->mfc_ops, get_dspl_y_adr, dev);
308 if (IS_MFCV6_PLUS(dev))
309 frame_type = s5p_mfc_hw_call(dev->mfc_ops,
310 get_disp_frame_type, ctx);
311 else
312 frame_type = s5p_mfc_hw_call(dev->mfc_ops,
313 get_dec_frame_type, dev);
314
315 /* If frame is same as previous then skip and do not dequeue */
316 if (frame_type == S5P_FIMV_DECODE_FRAME_SKIPPED) {
317 if (!ctx->after_packed_pb)
318 ctx->sequence++;
319 ctx->after_packed_pb = 0;
320 return;
321 }
322 ctx->sequence++;
323 /* The MFC returns address of the buffer, now we have to
324 * check which videobuf does it correspond to */
325 list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
326 /* Check if this is the buffer we're looking for */
327 if (vb2_dma_contig_plane_dma_addr(&dst_buf->b->vb2_buf, 0)
328 == dspl_y_addr) {
329 list_del(&dst_buf->list);
330 ctx->dst_queue_cnt--;
331 dst_buf->b->sequence = ctx->sequence;
332 if (s5p_mfc_hw_call(dev->mfc_ops,
333 get_pic_type_top, ctx) ==
334 s5p_mfc_hw_call(dev->mfc_ops,
335 get_pic_type_bot, ctx))
336 dst_buf->b->field = V4L2_FIELD_NONE;
337 else
338 dst_buf->b->field =
339 V4L2_FIELD_INTERLACED;
340 vb2_set_plane_payload(&dst_buf->b->vb2_buf, 0,
341 ctx->luma_size);
342 vb2_set_plane_payload(&dst_buf->b->vb2_buf, 1,
343 ctx->chroma_size);
344 clear_bit(dst_buf->b->vb2_buf.index,
345 &ctx->dec_dst_flag);
346
347 vb2_buffer_done(&dst_buf->b->vb2_buf, err ?
348 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
349
350 break;
351 }
352 }
353 }
354
355 /* Handle frame decoding interrupt */
356 static void s5p_mfc_handle_frame(struct s5p_mfc_ctx *ctx,
357 unsigned int reason, unsigned int err)
358 {
359 struct s5p_mfc_dev *dev = ctx->dev;
360 unsigned int dst_frame_status;
361 unsigned int dec_frame_status;
362 struct s5p_mfc_buf *src_buf;
363 unsigned int res_change;
364
365 dst_frame_status = s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev)
366 & S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK;
367 dec_frame_status = s5p_mfc_hw_call(dev->mfc_ops, get_dec_status, dev)
368 & S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK;
369 res_change = (s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev)
370 & S5P_FIMV_DEC_STATUS_RESOLUTION_MASK)
371 >> S5P_FIMV_DEC_STATUS_RESOLUTION_SHIFT;
372 mfc_debug(2, "Frame Status: %x\n", dst_frame_status);
373 if (ctx->state == MFCINST_RES_CHANGE_INIT)
374 ctx->state = MFCINST_RES_CHANGE_FLUSH;
375 if (res_change == S5P_FIMV_RES_INCREASE ||
376 res_change == S5P_FIMV_RES_DECREASE) {
377 ctx->state = MFCINST_RES_CHANGE_INIT;
378 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
379 wake_up_ctx(ctx, reason, err);
380 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
381 s5p_mfc_clock_off();
382 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
383 return;
384 }
385 if (ctx->dpb_flush_flag)
386 ctx->dpb_flush_flag = 0;
387
388 /* All frames remaining in the buffer have been extracted */
389 if (dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_EMPTY) {
390 if (ctx->state == MFCINST_RES_CHANGE_FLUSH) {
391 static const struct v4l2_event ev_src_ch = {
392 .type = V4L2_EVENT_SOURCE_CHANGE,
393 .u.src_change.changes =
394 V4L2_EVENT_SRC_CH_RESOLUTION,
395 };
396
397 s5p_mfc_handle_frame_all_extracted(ctx);
398 ctx->state = MFCINST_RES_CHANGE_END;
399 v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
400
401 goto leave_handle_frame;
402 } else {
403 s5p_mfc_handle_frame_all_extracted(ctx);
404 }
405 }
406
407 if (dec_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY)
408 s5p_mfc_handle_frame_copy_time(ctx);
409
410 /* A frame has been decoded and is in the buffer */
411 if (dst_frame_status == S5P_FIMV_DEC_STATUS_DISPLAY_ONLY ||
412 dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY) {
413 s5p_mfc_handle_frame_new(ctx, err);
414 } else {
415 mfc_debug(2, "No frame decode\n");
416 }
417 /* Mark source buffer as complete */
418 if (dst_frame_status != S5P_FIMV_DEC_STATUS_DISPLAY_ONLY
419 && !list_empty(&ctx->src_queue)) {
420 src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf,
421 list);
422 ctx->consumed_stream += s5p_mfc_hw_call(dev->mfc_ops,
423 get_consumed_stream, dev);
424 if (ctx->codec_mode != S5P_MFC_CODEC_H264_DEC &&
425 ctx->codec_mode != S5P_MFC_CODEC_VP8_DEC &&
426 ctx->consumed_stream + STUFF_BYTE <
427 src_buf->b->vb2_buf.planes[0].bytesused) {
428 /* Run MFC again on the same buffer */
429 mfc_debug(2, "Running again the same buffer\n");
430 ctx->after_packed_pb = 1;
431 } else {
432 mfc_debug(2, "MFC needs next buffer\n");
433 ctx->consumed_stream = 0;
434 if (src_buf->flags & MFC_BUF_FLAG_EOS)
435 ctx->state = MFCINST_FINISHING;
436 list_del(&src_buf->list);
437 ctx->src_queue_cnt--;
438 if (s5p_mfc_hw_call(dev->mfc_ops, err_dec, err) > 0)
439 vb2_buffer_done(&src_buf->b->vb2_buf,
440 VB2_BUF_STATE_ERROR);
441 else
442 vb2_buffer_done(&src_buf->b->vb2_buf,
443 VB2_BUF_STATE_DONE);
444 }
445 }
446 leave_handle_frame:
447 if ((ctx->src_queue_cnt == 0 && ctx->state != MFCINST_FINISHING)
448 || ctx->dst_queue_cnt < ctx->pb_count)
449 clear_work_bit(ctx);
450 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
451 wake_up_ctx(ctx, reason, err);
452 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
453 s5p_mfc_clock_off();
454 /* if suspending, wake up device and do not try_run again*/
455 if (test_bit(0, &dev->enter_suspend))
456 wake_up_dev(dev, reason, err);
457 else
458 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
459 }
460
461 /* Error handling for interrupt */
462 static void s5p_mfc_handle_error(struct s5p_mfc_dev *dev,
463 struct s5p_mfc_ctx *ctx, unsigned int reason, unsigned int err)
464 {
465 mfc_err("Interrupt Error: %08x\n", err);
466
467 if (ctx != NULL) {
468 /* Error recovery is dependent on the state of context */
469 switch (ctx->state) {
470 case MFCINST_RES_CHANGE_INIT:
471 case MFCINST_RES_CHANGE_FLUSH:
472 case MFCINST_RES_CHANGE_END:
473 case MFCINST_FINISHING:
474 case MFCINST_FINISHED:
475 case MFCINST_RUNNING:
476 /* It is highly probable that an error occurred
477 * while decoding a frame */
478 clear_work_bit(ctx);
479 ctx->state = MFCINST_ERROR;
480 /* Mark all dst buffers as having an error */
481 s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
482 /* Mark all src buffers as having an error */
483 s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
484 wake_up_ctx(ctx, reason, err);
485 break;
486 default:
487 clear_work_bit(ctx);
488 ctx->state = MFCINST_ERROR;
489 wake_up_ctx(ctx, reason, err);
490 break;
491 }
492 }
493 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
494 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
495 s5p_mfc_clock_off();
496 wake_up_dev(dev, reason, err);
497 }
498
499 /* Header parsing interrupt handling */
500 static void s5p_mfc_handle_seq_done(struct s5p_mfc_ctx *ctx,
501 unsigned int reason, unsigned int err)
502 {
503 struct s5p_mfc_dev *dev;
504
505 if (ctx == NULL)
506 return;
507 dev = ctx->dev;
508 if (ctx->c_ops->post_seq_start) {
509 if (ctx->c_ops->post_seq_start(ctx))
510 mfc_err("post_seq_start() failed\n");
511 } else {
512 ctx->img_width = s5p_mfc_hw_call(dev->mfc_ops, get_img_width,
513 dev);
514 ctx->img_height = s5p_mfc_hw_call(dev->mfc_ops, get_img_height,
515 dev);
516
517 s5p_mfc_hw_call(dev->mfc_ops, dec_calc_dpb_size, ctx);
518
519 ctx->pb_count = s5p_mfc_hw_call(dev->mfc_ops, get_dpb_count,
520 dev);
521 ctx->mv_count = s5p_mfc_hw_call(dev->mfc_ops, get_mv_count,
522 dev);
523 if (ctx->img_width == 0 || ctx->img_height == 0)
524 ctx->state = MFCINST_ERROR;
525 else
526 ctx->state = MFCINST_HEAD_PARSED;
527
528 if ((ctx->codec_mode == S5P_MFC_CODEC_H264_DEC ||
529 ctx->codec_mode == S5P_MFC_CODEC_H264_MVC_DEC) &&
530 !list_empty(&ctx->src_queue)) {
531 struct s5p_mfc_buf *src_buf;
532 src_buf = list_entry(ctx->src_queue.next,
533 struct s5p_mfc_buf, list);
534 if (s5p_mfc_hw_call(dev->mfc_ops, get_consumed_stream,
535 dev) <
536 src_buf->b->vb2_buf.planes[0].bytesused)
537 ctx->head_processed = 0;
538 else
539 ctx->head_processed = 1;
540 } else {
541 ctx->head_processed = 1;
542 }
543 }
544 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
545 clear_work_bit(ctx);
546 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
547 s5p_mfc_clock_off();
548 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
549 wake_up_ctx(ctx, reason, err);
550 }
551
552 /* Header parsing interrupt handling */
553 static void s5p_mfc_handle_init_buffers(struct s5p_mfc_ctx *ctx,
554 unsigned int reason, unsigned int err)
555 {
556 struct s5p_mfc_buf *src_buf;
557 struct s5p_mfc_dev *dev;
558
559 if (ctx == NULL)
560 return;
561 dev = ctx->dev;
562 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
563 ctx->int_type = reason;
564 ctx->int_err = err;
565 ctx->int_cond = 1;
566 clear_work_bit(ctx);
567 if (err == 0) {
568 ctx->state = MFCINST_RUNNING;
569 if (!ctx->dpb_flush_flag && ctx->head_processed) {
570 if (!list_empty(&ctx->src_queue)) {
571 src_buf = list_entry(ctx->src_queue.next,
572 struct s5p_mfc_buf, list);
573 list_del(&src_buf->list);
574 ctx->src_queue_cnt--;
575 vb2_buffer_done(&src_buf->b->vb2_buf,
576 VB2_BUF_STATE_DONE);
577 }
578 } else {
579 ctx->dpb_flush_flag = 0;
580 }
581 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
582
583 s5p_mfc_clock_off();
584
585 wake_up(&ctx->queue);
586 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
587 } else {
588 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
589
590 s5p_mfc_clock_off();
591
592 wake_up(&ctx->queue);
593 }
594 }
595
596 static void s5p_mfc_handle_stream_complete(struct s5p_mfc_ctx *ctx)
597 {
598 struct s5p_mfc_dev *dev = ctx->dev;
599 struct s5p_mfc_buf *mb_entry;
600
601 mfc_debug(2, "Stream completed\n");
602
603 ctx->state = MFCINST_FINISHED;
604
605 if (!list_empty(&ctx->dst_queue)) {
606 mb_entry = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf,
607 list);
608 list_del(&mb_entry->list);
609 ctx->dst_queue_cnt--;
610 vb2_set_plane_payload(&mb_entry->b->vb2_buf, 0, 0);
611 vb2_buffer_done(&mb_entry->b->vb2_buf, VB2_BUF_STATE_DONE);
612 }
613
614 clear_work_bit(ctx);
615
616 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
617
618 s5p_mfc_clock_off();
619 wake_up(&ctx->queue);
620 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
621 }
622
623 /* Interrupt processing */
624 static irqreturn_t s5p_mfc_irq(int irq, void *priv)
625 {
626 struct s5p_mfc_dev *dev = priv;
627 struct s5p_mfc_ctx *ctx;
628 unsigned int reason;
629 unsigned int err;
630
631 mfc_debug_enter();
632 /* Reset the timeout watchdog */
633 atomic_set(&dev->watchdog_cnt, 0);
634 spin_lock(&dev->irqlock);
635 ctx = dev->ctx[dev->curr_ctx];
636 /* Get the reason of interrupt and the error code */
637 reason = s5p_mfc_hw_call(dev->mfc_ops, get_int_reason, dev);
638 err = s5p_mfc_hw_call(dev->mfc_ops, get_int_err, dev);
639 mfc_debug(1, "Int reason: %d (err: %08x)\n", reason, err);
640 switch (reason) {
641 case S5P_MFC_R2H_CMD_ERR_RET:
642 /* An error has occurred */
643 if (ctx->state == MFCINST_RUNNING &&
644 (s5p_mfc_hw_call(dev->mfc_ops, err_dec, err) >=
645 dev->warn_start ||
646 err == S5P_FIMV_ERR_NO_VALID_SEQ_HDR ||
647 err == S5P_FIMV_ERR_INCOMPLETE_FRAME ||
648 err == S5P_FIMV_ERR_TIMEOUT))
649 s5p_mfc_handle_frame(ctx, reason, err);
650 else
651 s5p_mfc_handle_error(dev, ctx, reason, err);
652 clear_bit(0, &dev->enter_suspend);
653 break;
654
655 case S5P_MFC_R2H_CMD_SLICE_DONE_RET:
656 case S5P_MFC_R2H_CMD_FIELD_DONE_RET:
657 case S5P_MFC_R2H_CMD_FRAME_DONE_RET:
658 if (ctx->c_ops->post_frame_start) {
659 if (ctx->c_ops->post_frame_start(ctx))
660 mfc_err("post_frame_start() failed\n");
661
662 if (ctx->state == MFCINST_FINISHING &&
663 list_empty(&ctx->ref_queue)) {
664 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
665 s5p_mfc_handle_stream_complete(ctx);
666 break;
667 }
668 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
669 wake_up_ctx(ctx, reason, err);
670 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
671 s5p_mfc_clock_off();
672 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
673 } else {
674 s5p_mfc_handle_frame(ctx, reason, err);
675 }
676 break;
677
678 case S5P_MFC_R2H_CMD_SEQ_DONE_RET:
679 s5p_mfc_handle_seq_done(ctx, reason, err);
680 break;
681
682 case S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET:
683 ctx->inst_no = s5p_mfc_hw_call(dev->mfc_ops, get_inst_no, dev);
684 ctx->state = MFCINST_GOT_INST;
685 clear_work_bit(ctx);
686 wake_up(&ctx->queue);
687 goto irq_cleanup_hw;
688
689 case S5P_MFC_R2H_CMD_CLOSE_INSTANCE_RET:
690 clear_work_bit(ctx);
691 ctx->inst_no = MFC_NO_INSTANCE_SET;
692 ctx->state = MFCINST_FREE;
693 wake_up(&ctx->queue);
694 goto irq_cleanup_hw;
695
696 case S5P_MFC_R2H_CMD_SYS_INIT_RET:
697 case S5P_MFC_R2H_CMD_FW_STATUS_RET:
698 case S5P_MFC_R2H_CMD_SLEEP_RET:
699 case S5P_MFC_R2H_CMD_WAKEUP_RET:
700 if (ctx)
701 clear_work_bit(ctx);
702 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
703 wake_up_dev(dev, reason, err);
704 clear_bit(0, &dev->hw_lock);
705 clear_bit(0, &dev->enter_suspend);
706 break;
707
708 case S5P_MFC_R2H_CMD_INIT_BUFFERS_RET:
709 s5p_mfc_handle_init_buffers(ctx, reason, err);
710 break;
711
712 case S5P_MFC_R2H_CMD_COMPLETE_SEQ_RET:
713 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
714 ctx->int_type = reason;
715 ctx->int_err = err;
716 s5p_mfc_handle_stream_complete(ctx);
717 break;
718
719 case S5P_MFC_R2H_CMD_DPB_FLUSH_RET:
720 clear_work_bit(ctx);
721 ctx->state = MFCINST_RUNNING;
722 wake_up(&ctx->queue);
723 goto irq_cleanup_hw;
724
725 default:
726 mfc_debug(2, "Unknown int reason\n");
727 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
728 }
729 spin_unlock(&dev->irqlock);
730 mfc_debug_leave();
731 return IRQ_HANDLED;
732 irq_cleanup_hw:
733 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
734 ctx->int_type = reason;
735 ctx->int_err = err;
736 ctx->int_cond = 1;
737 if (test_and_clear_bit(0, &dev->hw_lock) == 0)
738 mfc_err("Failed to unlock hw\n");
739
740 s5p_mfc_clock_off();
741
742 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
743 spin_unlock(&dev->irqlock);
744 mfc_debug(2, "Exit via irq_cleanup_hw\n");
745 return IRQ_HANDLED;
746 }
747
748 /* Open an MFC node */
749 static int s5p_mfc_open(struct file *file)
750 {
751 struct video_device *vdev = video_devdata(file);
752 struct s5p_mfc_dev *dev = video_drvdata(file);
753 struct s5p_mfc_ctx *ctx = NULL;
754 struct vb2_queue *q;
755 int ret = 0;
756
757 mfc_debug_enter();
758 if (mutex_lock_interruptible(&dev->mfc_mutex))
759 return -ERESTARTSYS;
760 dev->num_inst++; /* It is guarded by mfc_mutex in vfd */
761 /* Allocate memory for context */
762 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
763 if (!ctx) {
764 ret = -ENOMEM;
765 goto err_alloc;
766 }
767 v4l2_fh_init(&ctx->fh, vdev);
768 file->private_data = &ctx->fh;
769 v4l2_fh_add(&ctx->fh);
770 ctx->dev = dev;
771 INIT_LIST_HEAD(&ctx->src_queue);
772 INIT_LIST_HEAD(&ctx->dst_queue);
773 ctx->src_queue_cnt = 0;
774 ctx->dst_queue_cnt = 0;
775 /* Get context number */
776 ctx->num = 0;
777 while (dev->ctx[ctx->num]) {
778 ctx->num++;
779 if (ctx->num >= MFC_NUM_CONTEXTS) {
780 mfc_debug(2, "Too many open contexts\n");
781 ret = -EBUSY;
782 goto err_no_ctx;
783 }
784 }
785 /* Mark context as idle */
786 clear_work_bit_irqsave(ctx);
787 dev->ctx[ctx->num] = ctx;
788 if (vdev == dev->vfd_dec) {
789 ctx->type = MFCINST_DECODER;
790 ctx->c_ops = get_dec_codec_ops();
791 s5p_mfc_dec_init(ctx);
792 /* Setup ctrl handler */
793 ret = s5p_mfc_dec_ctrls_setup(ctx);
794 if (ret) {
795 mfc_err("Failed to setup mfc controls\n");
796 goto err_ctrls_setup;
797 }
798 } else if (vdev == dev->vfd_enc) {
799 ctx->type = MFCINST_ENCODER;
800 ctx->c_ops = get_enc_codec_ops();
801 /* only for encoder */
802 INIT_LIST_HEAD(&ctx->ref_queue);
803 ctx->ref_queue_cnt = 0;
804 s5p_mfc_enc_init(ctx);
805 /* Setup ctrl handler */
806 ret = s5p_mfc_enc_ctrls_setup(ctx);
807 if (ret) {
808 mfc_err("Failed to setup mfc controls\n");
809 goto err_ctrls_setup;
810 }
811 } else {
812 ret = -ENOENT;
813 goto err_bad_node;
814 }
815 ctx->fh.ctrl_handler = &ctx->ctrl_handler;
816 ctx->inst_no = MFC_NO_INSTANCE_SET;
817 /* Load firmware if this is the first instance */
818 if (dev->num_inst == 1) {
819 dev->watchdog_timer.expires = jiffies +
820 msecs_to_jiffies(MFC_WATCHDOG_INTERVAL);
821 add_timer(&dev->watchdog_timer);
822 ret = s5p_mfc_power_on();
823 if (ret < 0) {
824 mfc_err("power on failed\n");
825 goto err_pwr_enable;
826 }
827 s5p_mfc_clock_on();
828 ret = s5p_mfc_load_firmware(dev);
829 if (ret) {
830 s5p_mfc_clock_off();
831 goto err_load_fw;
832 }
833 /* Init the FW */
834 ret = s5p_mfc_init_hw(dev);
835 s5p_mfc_clock_off();
836 if (ret)
837 goto err_init_hw;
838 }
839 /* Init videobuf2 queue for CAPTURE */
840 q = &ctx->vq_dst;
841 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
842 q->drv_priv = &ctx->fh;
843 q->lock = &dev->mfc_mutex;
844 if (vdev == dev->vfd_dec) {
845 q->io_modes = VB2_MMAP;
846 q->ops = get_dec_queue_ops();
847 } else if (vdev == dev->vfd_enc) {
848 q->io_modes = VB2_MMAP | VB2_USERPTR;
849 q->ops = get_enc_queue_ops();
850 } else {
851 ret = -ENOENT;
852 goto err_queue_init;
853 }
854 q->mem_ops = &vb2_dma_contig_memops;
855 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
856 ret = vb2_queue_init(q);
857 if (ret) {
858 mfc_err("Failed to initialize videobuf2 queue(capture)\n");
859 goto err_queue_init;
860 }
861 /* Init videobuf2 queue for OUTPUT */
862 q = &ctx->vq_src;
863 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
864 q->io_modes = VB2_MMAP;
865 q->drv_priv = &ctx->fh;
866 q->lock = &dev->mfc_mutex;
867 if (vdev == dev->vfd_dec) {
868 q->io_modes = VB2_MMAP;
869 q->ops = get_dec_queue_ops();
870 } else if (vdev == dev->vfd_enc) {
871 q->io_modes = VB2_MMAP | VB2_USERPTR;
872 q->ops = get_enc_queue_ops();
873 } else {
874 ret = -ENOENT;
875 goto err_queue_init;
876 }
877 /* One way to indicate end-of-stream for MFC is to set the
878 * bytesused == 0. However by default videobuf2 handles bytesused
879 * equal to 0 as a special case and changes its value to the size
880 * of the buffer. Set the allow_zero_bytesused flag so that videobuf2
881 * will keep the value of bytesused intact.
882 */
883 q->allow_zero_bytesused = 1;
884 q->mem_ops = &vb2_dma_contig_memops;
885 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
886 ret = vb2_queue_init(q);
887 if (ret) {
888 mfc_err("Failed to initialize videobuf2 queue(output)\n");
889 goto err_queue_init;
890 }
891 init_waitqueue_head(&ctx->queue);
892 mutex_unlock(&dev->mfc_mutex);
893 mfc_debug_leave();
894 return ret;
895 /* Deinit when failure occurred */
896 err_queue_init:
897 if (dev->num_inst == 1)
898 s5p_mfc_deinit_hw(dev);
899 err_init_hw:
900 err_load_fw:
901 err_pwr_enable:
902 if (dev->num_inst == 1) {
903 if (s5p_mfc_power_off() < 0)
904 mfc_err("power off failed\n");
905 del_timer_sync(&dev->watchdog_timer);
906 }
907 err_ctrls_setup:
908 s5p_mfc_dec_ctrls_delete(ctx);
909 err_bad_node:
910 dev->ctx[ctx->num] = NULL;
911 err_no_ctx:
912 v4l2_fh_del(&ctx->fh);
913 v4l2_fh_exit(&ctx->fh);
914 kfree(ctx);
915 err_alloc:
916 dev->num_inst--;
917 mutex_unlock(&dev->mfc_mutex);
918 mfc_debug_leave();
919 return ret;
920 }
921
922 /* Release MFC context */
923 static int s5p_mfc_release(struct file *file)
924 {
925 struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
926 struct s5p_mfc_dev *dev = ctx->dev;
927
928 /* if dev is null, do cleanup that doesn't need dev */
929 mfc_debug_enter();
930 if (dev)
931 mutex_lock(&dev->mfc_mutex);
932 vb2_queue_release(&ctx->vq_src);
933 vb2_queue_release(&ctx->vq_dst);
934 if (dev) {
935 s5p_mfc_clock_on();
936
937 /* Mark context as idle */
938 clear_work_bit_irqsave(ctx);
939 /*
940 * If instance was initialised and not yet freed,
941 * return instance and free resources
942 */
943 if (ctx->state != MFCINST_FREE && ctx->state != MFCINST_INIT) {
944 mfc_debug(2, "Has to free instance\n");
945 s5p_mfc_close_mfc_inst(dev, ctx);
946 }
947 /* hardware locking scheme */
948 if (dev->curr_ctx == ctx->num)
949 clear_bit(0, &dev->hw_lock);
950 dev->num_inst--;
951 if (dev->num_inst == 0) {
952 mfc_debug(2, "Last instance\n");
953 s5p_mfc_deinit_hw(dev);
954 del_timer_sync(&dev->watchdog_timer);
955 if (s5p_mfc_power_off() < 0)
956 mfc_err("Power off failed\n");
957 }
958 mfc_debug(2, "Shutting down clock\n");
959 s5p_mfc_clock_off();
960 }
961 if (dev)
962 dev->ctx[ctx->num] = NULL;
963 s5p_mfc_dec_ctrls_delete(ctx);
964 v4l2_fh_del(&ctx->fh);
965 /* vdev is gone if dev is null */
966 if (dev)
967 v4l2_fh_exit(&ctx->fh);
968 kfree(ctx);
969 mfc_debug_leave();
970 if (dev)
971 mutex_unlock(&dev->mfc_mutex);
972
973 return 0;
974 }
975
976 /* Poll */
977 static unsigned int s5p_mfc_poll(struct file *file,
978 struct poll_table_struct *wait)
979 {
980 struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
981 struct s5p_mfc_dev *dev = ctx->dev;
982 struct vb2_queue *src_q, *dst_q;
983 struct vb2_buffer *src_vb = NULL, *dst_vb = NULL;
984 unsigned int rc = 0;
985 unsigned long flags;
986
987 mutex_lock(&dev->mfc_mutex);
988 src_q = &ctx->vq_src;
989 dst_q = &ctx->vq_dst;
990 /*
991 * There has to be at least one buffer queued on each queued_list, which
992 * means either in driver already or waiting for driver to claim it
993 * and start processing.
994 */
995 if ((!src_q->streaming || list_empty(&src_q->queued_list))
996 && (!dst_q->streaming || list_empty(&dst_q->queued_list))) {
997 rc = POLLERR;
998 goto end;
999 }
1000 mutex_unlock(&dev->mfc_mutex);
1001 poll_wait(file, &ctx->fh.wait, wait);
1002 poll_wait(file, &src_q->done_wq, wait);
1003 poll_wait(file, &dst_q->done_wq, wait);
1004 mutex_lock(&dev->mfc_mutex);
1005 if (v4l2_event_pending(&ctx->fh))
1006 rc |= POLLPRI;
1007 spin_lock_irqsave(&src_q->done_lock, flags);
1008 if (!list_empty(&src_q->done_list))
1009 src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer,
1010 done_entry);
1011 if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE
1012 || src_vb->state == VB2_BUF_STATE_ERROR))
1013 rc |= POLLOUT | POLLWRNORM;
1014 spin_unlock_irqrestore(&src_q->done_lock, flags);
1015 spin_lock_irqsave(&dst_q->done_lock, flags);
1016 if (!list_empty(&dst_q->done_list))
1017 dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer,
1018 done_entry);
1019 if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE
1020 || dst_vb->state == VB2_BUF_STATE_ERROR))
1021 rc |= POLLIN | POLLRDNORM;
1022 spin_unlock_irqrestore(&dst_q->done_lock, flags);
1023 end:
1024 mutex_unlock(&dev->mfc_mutex);
1025 return rc;
1026 }
1027
1028 /* Mmap */
1029 static int s5p_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1030 {
1031 struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
1032 struct s5p_mfc_dev *dev = ctx->dev;
1033 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1034 int ret;
1035
1036 if (mutex_lock_interruptible(&dev->mfc_mutex))
1037 return -ERESTARTSYS;
1038 if (offset < DST_QUEUE_OFF_BASE) {
1039 mfc_debug(2, "mmaping source\n");
1040 ret = vb2_mmap(&ctx->vq_src, vma);
1041 } else { /* capture */
1042 mfc_debug(2, "mmaping destination\n");
1043 vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
1044 ret = vb2_mmap(&ctx->vq_dst, vma);
1045 }
1046 mutex_unlock(&dev->mfc_mutex);
1047 return ret;
1048 }
1049
1050 /* v4l2 ops */
1051 static const struct v4l2_file_operations s5p_mfc_fops = {
1052 .owner = THIS_MODULE,
1053 .open = s5p_mfc_open,
1054 .release = s5p_mfc_release,
1055 .poll = s5p_mfc_poll,
1056 .unlocked_ioctl = video_ioctl2,
1057 .mmap = s5p_mfc_mmap,
1058 };
1059
1060 /* DMA memory related helper functions */
1061 static void s5p_mfc_memdev_release(struct device *dev)
1062 {
1063 of_reserved_mem_device_release(dev);
1064 }
1065
1066 static struct device *s5p_mfc_alloc_memdev(struct device *dev,
1067 const char *name, unsigned int idx)
1068 {
1069 struct device *child;
1070 int ret;
1071
1072 child = devm_kzalloc(dev, sizeof(struct device), GFP_KERNEL);
1073 if (!child)
1074 return NULL;
1075
1076 device_initialize(child);
1077 dev_set_name(child, "%s:%s", dev_name(dev), name);
1078 child->parent = dev;
1079 child->bus = dev->bus;
1080 child->coherent_dma_mask = dev->coherent_dma_mask;
1081 child->dma_mask = dev->dma_mask;
1082 child->release = s5p_mfc_memdev_release;
1083
1084 if (device_add(child) == 0) {
1085 ret = of_reserved_mem_device_init_by_idx(child, dev->of_node,
1086 idx);
1087 if (ret == 0)
1088 return child;
1089 device_del(child);
1090 }
1091
1092 put_device(child);
1093 return NULL;
1094 }
1095
1096 static int s5p_mfc_configure_dma_memory(struct s5p_mfc_dev *mfc_dev)
1097 {
1098 struct device *dev = &mfc_dev->plat_dev->dev;
1099
1100 /*
1101 * When IOMMU is available, we cannot use the default configuration,
1102 * because of MFC firmware requirements: address space limited to
1103 * 256M and non-zero default start address.
1104 * This is still simplified, not optimal configuration, but for now
1105 * IOMMU core doesn't allow to configure device's IOMMUs channel
1106 * separately.
1107 */
1108 if (exynos_is_iommu_available(dev)) {
1109 int ret = exynos_configure_iommu(dev, S5P_MFC_IOMMU_DMA_BASE,
1110 S5P_MFC_IOMMU_DMA_SIZE);
1111 if (ret == 0)
1112 mfc_dev->mem_dev_l = mfc_dev->mem_dev_r = dev;
1113 return ret;
1114 }
1115
1116 /*
1117 * Create and initialize virtual devices for accessing
1118 * reserved memory regions.
1119 */
1120 mfc_dev->mem_dev_l = s5p_mfc_alloc_memdev(dev, "left",
1121 MFC_BANK1_ALLOC_CTX);
1122 if (!mfc_dev->mem_dev_l)
1123 return -ENODEV;
1124 mfc_dev->mem_dev_r = s5p_mfc_alloc_memdev(dev, "right",
1125 MFC_BANK2_ALLOC_CTX);
1126 if (!mfc_dev->mem_dev_r) {
1127 device_unregister(mfc_dev->mem_dev_l);
1128 return -ENODEV;
1129 }
1130
1131 return 0;
1132 }
1133
1134 static void s5p_mfc_unconfigure_dma_memory(struct s5p_mfc_dev *mfc_dev)
1135 {
1136 struct device *dev = &mfc_dev->plat_dev->dev;
1137
1138 if (exynos_is_iommu_available(dev)) {
1139 exynos_unconfigure_iommu(dev);
1140 return;
1141 }
1142
1143 device_unregister(mfc_dev->mem_dev_l);
1144 device_unregister(mfc_dev->mem_dev_r);
1145 }
1146
1147 static void *mfc_get_drv_data(struct platform_device *pdev);
1148
1149 /* MFC probe function */
1150 static int s5p_mfc_probe(struct platform_device *pdev)
1151 {
1152 struct s5p_mfc_dev *dev;
1153 struct video_device *vfd;
1154 struct resource *res;
1155 int ret;
1156
1157 pr_debug("%s++\n", __func__);
1158 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
1159 if (!dev) {
1160 dev_err(&pdev->dev, "Not enough memory for MFC device\n");
1161 return -ENOMEM;
1162 }
1163
1164 spin_lock_init(&dev->irqlock);
1165 spin_lock_init(&dev->condlock);
1166 dev->plat_dev = pdev;
1167 if (!dev->plat_dev) {
1168 dev_err(&pdev->dev, "No platform data specified\n");
1169 return -ENODEV;
1170 }
1171
1172 dev->variant = mfc_get_drv_data(pdev);
1173
1174 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1175 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
1176 if (IS_ERR(dev->regs_base))
1177 return PTR_ERR(dev->regs_base);
1178
1179 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1180 if (res == NULL) {
1181 dev_err(&pdev->dev, "failed to get irq resource\n");
1182 return -ENOENT;
1183 }
1184 dev->irq = res->start;
1185 ret = devm_request_irq(&pdev->dev, dev->irq, s5p_mfc_irq,
1186 0, pdev->name, dev);
1187 if (ret) {
1188 dev_err(&pdev->dev, "Failed to install irq (%d)\n", ret);
1189 return ret;
1190 }
1191
1192 ret = s5p_mfc_configure_dma_memory(dev);
1193 if (ret < 0) {
1194 dev_err(&pdev->dev, "failed to configure DMA memory\n");
1195 return ret;
1196 }
1197
1198 ret = s5p_mfc_init_pm(dev);
1199 if (ret < 0) {
1200 dev_err(&pdev->dev, "failed to get mfc clock source\n");
1201 goto err_dma;
1202 }
1203
1204 vb2_dma_contig_set_max_seg_size(dev->mem_dev_l, DMA_BIT_MASK(32));
1205 vb2_dma_contig_set_max_seg_size(dev->mem_dev_r, DMA_BIT_MASK(32));
1206
1207 mutex_init(&dev->mfc_mutex);
1208
1209 ret = s5p_mfc_alloc_firmware(dev);
1210 if (ret)
1211 goto err_res;
1212
1213 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1214 if (ret)
1215 goto err_v4l2_dev_reg;
1216 init_waitqueue_head(&dev->queue);
1217
1218 /* decoder */
1219 vfd = video_device_alloc();
1220 if (!vfd) {
1221 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
1222 ret = -ENOMEM;
1223 goto err_dec_alloc;
1224 }
1225 vfd->fops = &s5p_mfc_fops;
1226 vfd->ioctl_ops = get_dec_v4l2_ioctl_ops();
1227 vfd->release = video_device_release;
1228 vfd->lock = &dev->mfc_mutex;
1229 vfd->v4l2_dev = &dev->v4l2_dev;
1230 vfd->vfl_dir = VFL_DIR_M2M;
1231 snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_DEC_NAME);
1232 dev->vfd_dec = vfd;
1233 video_set_drvdata(vfd, dev);
1234
1235 /* encoder */
1236 vfd = video_device_alloc();
1237 if (!vfd) {
1238 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
1239 ret = -ENOMEM;
1240 goto err_enc_alloc;
1241 }
1242 vfd->fops = &s5p_mfc_fops;
1243 vfd->ioctl_ops = get_enc_v4l2_ioctl_ops();
1244 vfd->release = video_device_release;
1245 vfd->lock = &dev->mfc_mutex;
1246 vfd->v4l2_dev = &dev->v4l2_dev;
1247 vfd->vfl_dir = VFL_DIR_M2M;
1248 snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_ENC_NAME);
1249 dev->vfd_enc = vfd;
1250 video_set_drvdata(vfd, dev);
1251 platform_set_drvdata(pdev, dev);
1252
1253 dev->hw_lock = 0;
1254 INIT_WORK(&dev->watchdog_work, s5p_mfc_watchdog_worker);
1255 atomic_set(&dev->watchdog_cnt, 0);
1256 init_timer(&dev->watchdog_timer);
1257 dev->watchdog_timer.data = (unsigned long)dev;
1258 dev->watchdog_timer.function = s5p_mfc_watchdog;
1259
1260 /* Initialize HW ops and commands based on MFC version */
1261 s5p_mfc_init_hw_ops(dev);
1262 s5p_mfc_init_hw_cmds(dev);
1263 s5p_mfc_init_regs(dev);
1264
1265 /* Register decoder and encoder */
1266 ret = video_register_device(dev->vfd_dec, VFL_TYPE_GRABBER, 0);
1267 if (ret) {
1268 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1269 goto err_dec_reg;
1270 }
1271 v4l2_info(&dev->v4l2_dev,
1272 "decoder registered as /dev/video%d\n", dev->vfd_dec->num);
1273
1274 ret = video_register_device(dev->vfd_enc, VFL_TYPE_GRABBER, 0);
1275 if (ret) {
1276 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1277 goto err_enc_reg;
1278 }
1279 v4l2_info(&dev->v4l2_dev,
1280 "encoder registered as /dev/video%d\n", dev->vfd_enc->num);
1281
1282 pr_debug("%s--\n", __func__);
1283 return 0;
1284
1285 /* Deinit MFC if probe had failed */
1286 err_enc_reg:
1287 video_unregister_device(dev->vfd_dec);
1288 err_dec_reg:
1289 video_device_release(dev->vfd_enc);
1290 err_enc_alloc:
1291 video_device_release(dev->vfd_dec);
1292 err_dec_alloc:
1293 v4l2_device_unregister(&dev->v4l2_dev);
1294 err_v4l2_dev_reg:
1295 s5p_mfc_release_firmware(dev);
1296 err_res:
1297 s5p_mfc_final_pm(dev);
1298 err_dma:
1299 s5p_mfc_unconfigure_dma_memory(dev);
1300
1301 pr_debug("%s-- with error\n", __func__);
1302 return ret;
1303
1304 }
1305
1306 /* Remove the driver */
1307 static int s5p_mfc_remove(struct platform_device *pdev)
1308 {
1309 struct s5p_mfc_dev *dev = platform_get_drvdata(pdev);
1310 struct s5p_mfc_ctx *ctx;
1311 int i;
1312
1313 v4l2_info(&dev->v4l2_dev, "Removing %s\n", pdev->name);
1314
1315 /*
1316 * Clear ctx dev pointer to avoid races between s5p_mfc_remove()
1317 * and s5p_mfc_release() and s5p_mfc_release() accessing ctx->dev
1318 * after s5p_mfc_remove() is run during unbind.
1319 */
1320 mutex_lock(&dev->mfc_mutex);
1321 for (i = 0; i < MFC_NUM_CONTEXTS; i++) {
1322 ctx = dev->ctx[i];
1323 if (!ctx)
1324 continue;
1325 /* clear ctx->dev */
1326 ctx->dev = NULL;
1327 }
1328 mutex_unlock(&dev->mfc_mutex);
1329
1330 del_timer_sync(&dev->watchdog_timer);
1331 flush_work(&dev->watchdog_work);
1332
1333 video_unregister_device(dev->vfd_enc);
1334 video_unregister_device(dev->vfd_dec);
1335 video_device_release(dev->vfd_enc);
1336 video_device_release(dev->vfd_dec);
1337 v4l2_device_unregister(&dev->v4l2_dev);
1338 s5p_mfc_release_firmware(dev);
1339 s5p_mfc_unconfigure_dma_memory(dev);
1340 vb2_dma_contig_clear_max_seg_size(dev->mem_dev_l);
1341 vb2_dma_contig_clear_max_seg_size(dev->mem_dev_r);
1342
1343 s5p_mfc_final_pm(dev);
1344 return 0;
1345 }
1346
1347 #ifdef CONFIG_PM_SLEEP
1348
1349 static int s5p_mfc_suspend(struct device *dev)
1350 {
1351 struct platform_device *pdev = to_platform_device(dev);
1352 struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
1353 int ret;
1354
1355 if (m_dev->num_inst == 0)
1356 return 0;
1357
1358 if (test_and_set_bit(0, &m_dev->enter_suspend) != 0) {
1359 mfc_err("Error: going to suspend for a second time\n");
1360 return -EIO;
1361 }
1362
1363 /* Check if we're processing then wait if it necessary. */
1364 while (test_and_set_bit(0, &m_dev->hw_lock) != 0) {
1365 /* Try and lock the HW */
1366 /* Wait on the interrupt waitqueue */
1367 ret = wait_event_interruptible_timeout(m_dev->queue,
1368 m_dev->int_cond, msecs_to_jiffies(MFC_INT_TIMEOUT));
1369 if (ret == 0) {
1370 mfc_err("Waiting for hardware to finish timed out\n");
1371 clear_bit(0, &m_dev->enter_suspend);
1372 return -EIO;
1373 }
1374 }
1375
1376 ret = s5p_mfc_sleep(m_dev);
1377 if (ret) {
1378 clear_bit(0, &m_dev->enter_suspend);
1379 clear_bit(0, &m_dev->hw_lock);
1380 }
1381 return ret;
1382 }
1383
1384 static int s5p_mfc_resume(struct device *dev)
1385 {
1386 struct platform_device *pdev = to_platform_device(dev);
1387 struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
1388
1389 if (m_dev->num_inst == 0)
1390 return 0;
1391 return s5p_mfc_wakeup(m_dev);
1392 }
1393 #endif
1394
1395 #ifdef CONFIG_PM
1396 static int s5p_mfc_runtime_suspend(struct device *dev)
1397 {
1398 struct platform_device *pdev = to_platform_device(dev);
1399 struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
1400
1401 atomic_set(&m_dev->pm.power, 0);
1402 return 0;
1403 }
1404
1405 static int s5p_mfc_runtime_resume(struct device *dev)
1406 {
1407 struct platform_device *pdev = to_platform_device(dev);
1408 struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
1409
1410 atomic_set(&m_dev->pm.power, 1);
1411 return 0;
1412 }
1413 #endif
1414
1415 /* Power management */
1416 static const struct dev_pm_ops s5p_mfc_pm_ops = {
1417 SET_SYSTEM_SLEEP_PM_OPS(s5p_mfc_suspend, s5p_mfc_resume)
1418 SET_RUNTIME_PM_OPS(s5p_mfc_runtime_suspend, s5p_mfc_runtime_resume,
1419 NULL)
1420 };
1421
1422 static struct s5p_mfc_buf_size_v5 mfc_buf_size_v5 = {
1423 .h264_ctx = MFC_H264_CTX_BUF_SIZE,
1424 .non_h264_ctx = MFC_CTX_BUF_SIZE,
1425 .dsc = DESC_BUF_SIZE,
1426 .shm = SHARED_BUF_SIZE,
1427 };
1428
1429 static struct s5p_mfc_buf_size buf_size_v5 = {
1430 .fw = MAX_FW_SIZE,
1431 .cpb = MAX_CPB_SIZE,
1432 .priv = &mfc_buf_size_v5,
1433 };
1434
1435 static struct s5p_mfc_buf_align mfc_buf_align_v5 = {
1436 .base = MFC_BASE_ALIGN_ORDER,
1437 };
1438
1439 static struct s5p_mfc_variant mfc_drvdata_v5 = {
1440 .version = MFC_VERSION,
1441 .version_bit = MFC_V5_BIT,
1442 .port_num = MFC_NUM_PORTS,
1443 .buf_size = &buf_size_v5,
1444 .buf_align = &mfc_buf_align_v5,
1445 .fw_name[0] = "s5p-mfc.fw",
1446 .use_clock_gating = true,
1447 };
1448
1449 static struct s5p_mfc_buf_size_v6 mfc_buf_size_v6 = {
1450 .dev_ctx = MFC_CTX_BUF_SIZE_V6,
1451 .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V6,
1452 .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V6,
1453 .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V6,
1454 .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V6,
1455 };
1456
1457 static struct s5p_mfc_buf_size buf_size_v6 = {
1458 .fw = MAX_FW_SIZE_V6,
1459 .cpb = MAX_CPB_SIZE_V6,
1460 .priv = &mfc_buf_size_v6,
1461 };
1462
1463 static struct s5p_mfc_buf_align mfc_buf_align_v6 = {
1464 .base = 0,
1465 };
1466
1467 static struct s5p_mfc_variant mfc_drvdata_v6 = {
1468 .version = MFC_VERSION_V6,
1469 .version_bit = MFC_V6_BIT,
1470 .port_num = MFC_NUM_PORTS_V6,
1471 .buf_size = &buf_size_v6,
1472 .buf_align = &mfc_buf_align_v6,
1473 .fw_name[0] = "s5p-mfc-v6.fw",
1474 /*
1475 * v6-v2 firmware contains bug fixes and interface change
1476 * for init buffer command
1477 */
1478 .fw_name[1] = "s5p-mfc-v6-v2.fw",
1479 };
1480
1481 static struct s5p_mfc_buf_size_v6 mfc_buf_size_v7 = {
1482 .dev_ctx = MFC_CTX_BUF_SIZE_V7,
1483 .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V7,
1484 .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V7,
1485 .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V7,
1486 .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V7,
1487 };
1488
1489 static struct s5p_mfc_buf_size buf_size_v7 = {
1490 .fw = MAX_FW_SIZE_V7,
1491 .cpb = MAX_CPB_SIZE_V7,
1492 .priv = &mfc_buf_size_v7,
1493 };
1494
1495 static struct s5p_mfc_buf_align mfc_buf_align_v7 = {
1496 .base = 0,
1497 };
1498
1499 static struct s5p_mfc_variant mfc_drvdata_v7 = {
1500 .version = MFC_VERSION_V7,
1501 .version_bit = MFC_V7_BIT,
1502 .port_num = MFC_NUM_PORTS_V7,
1503 .buf_size = &buf_size_v7,
1504 .buf_align = &mfc_buf_align_v7,
1505 .fw_name[0] = "s5p-mfc-v7.fw",
1506 };
1507
1508 static struct s5p_mfc_buf_size_v6 mfc_buf_size_v8 = {
1509 .dev_ctx = MFC_CTX_BUF_SIZE_V8,
1510 .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V8,
1511 .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V8,
1512 .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V8,
1513 .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V8,
1514 };
1515
1516 static struct s5p_mfc_buf_size buf_size_v8 = {
1517 .fw = MAX_FW_SIZE_V8,
1518 .cpb = MAX_CPB_SIZE_V8,
1519 .priv = &mfc_buf_size_v8,
1520 };
1521
1522 static struct s5p_mfc_buf_align mfc_buf_align_v8 = {
1523 .base = 0,
1524 };
1525
1526 static struct s5p_mfc_variant mfc_drvdata_v8 = {
1527 .version = MFC_VERSION_V8,
1528 .version_bit = MFC_V8_BIT,
1529 .port_num = MFC_NUM_PORTS_V8,
1530 .buf_size = &buf_size_v8,
1531 .buf_align = &mfc_buf_align_v8,
1532 .fw_name[0] = "s5p-mfc-v8.fw",
1533 };
1534
1535 static const struct of_device_id exynos_mfc_match[] = {
1536 {
1537 .compatible = "samsung,mfc-v5",
1538 .data = &mfc_drvdata_v5,
1539 }, {
1540 .compatible = "samsung,mfc-v6",
1541 .data = &mfc_drvdata_v6,
1542 }, {
1543 .compatible = "samsung,mfc-v7",
1544 .data = &mfc_drvdata_v7,
1545 }, {
1546 .compatible = "samsung,mfc-v8",
1547 .data = &mfc_drvdata_v8,
1548 },
1549 {},
1550 };
1551 MODULE_DEVICE_TABLE(of, exynos_mfc_match);
1552
1553 static void *mfc_get_drv_data(struct platform_device *pdev)
1554 {
1555 struct s5p_mfc_variant *driver_data = NULL;
1556 const struct of_device_id *match;
1557
1558 match = of_match_node(exynos_mfc_match, pdev->dev.of_node);
1559 if (match)
1560 driver_data = (struct s5p_mfc_variant *)match->data;
1561
1562 return driver_data;
1563 }
1564
1565 static struct platform_driver s5p_mfc_driver = {
1566 .probe = s5p_mfc_probe,
1567 .remove = s5p_mfc_remove,
1568 .driver = {
1569 .name = S5P_MFC_NAME,
1570 .pm = &s5p_mfc_pm_ops,
1571 .of_match_table = exynos_mfc_match,
1572 },
1573 };
1574
1575 module_platform_driver(s5p_mfc_driver);
1576
1577 MODULE_LICENSE("GPL");
1578 MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
1579 MODULE_DESCRIPTION("Samsung S5P Multi Format Codec V4L2 driver");
1580