V4L/DVB (9595): cx18: Improve handling of outgoing mailboxes detected to be busy
[safe/jmp/linux-2.6] / drivers / media / video / cx18 / cx18-mailbox.c
1 /*
2  *  cx18 mailbox functions
3  *
4  *  Copyright (C) 2007  Hans Verkuil <hverkuil@xs4all.nl>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19  *  02111-1307  USA
20  */
21
22 #include <stdarg.h>
23
24 #include "cx18-driver.h"
25 #include "cx18-io.h"
26 #include "cx18-scb.h"
27 #include "cx18-irq.h"
28 #include "cx18-mailbox.h"
29
30 #define API_FAST (1 << 2) /* Short timeout */
31 #define API_SLOW (1 << 3) /* Additional 300ms timeout */
32
33 struct cx18_api_info {
34         u32 cmd;
35         u8 flags;               /* Flags, see above */
36         u8 rpu;                 /* Processing unit */
37         const char *name;       /* The name of the command */
38 };
39
40 #define API_ENTRY(rpu, x, f) { (x), (f), (rpu), #x }
41
42 static const struct cx18_api_info api_info[] = {
43         /* MPEG encoder API */
44         API_ENTRY(CPU, CX18_CPU_SET_CHANNEL_TYPE,               0),
45         API_ENTRY(CPU, CX18_EPU_DEBUG,                          0),
46         API_ENTRY(CPU, CX18_CREATE_TASK,                        0),
47         API_ENTRY(CPU, CX18_DESTROY_TASK,                       0),
48         API_ENTRY(CPU, CX18_CPU_CAPTURE_START,                  API_SLOW),
49         API_ENTRY(CPU, CX18_CPU_CAPTURE_STOP,                   API_SLOW),
50         API_ENTRY(CPU, CX18_CPU_CAPTURE_PAUSE,                  0),
51         API_ENTRY(CPU, CX18_CPU_CAPTURE_RESUME,                 0),
52         API_ENTRY(CPU, CX18_CPU_SET_CHANNEL_TYPE,               0),
53         API_ENTRY(CPU, CX18_CPU_SET_STREAM_OUTPUT_TYPE,         0),
54         API_ENTRY(CPU, CX18_CPU_SET_VIDEO_IN,                   0),
55         API_ENTRY(CPU, CX18_CPU_SET_VIDEO_RATE,                 0),
56         API_ENTRY(CPU, CX18_CPU_SET_VIDEO_RESOLUTION,           0),
57         API_ENTRY(CPU, CX18_CPU_SET_FILTER_PARAM,               0),
58         API_ENTRY(CPU, CX18_CPU_SET_SPATIAL_FILTER_TYPE,        0),
59         API_ENTRY(CPU, CX18_CPU_SET_MEDIAN_CORING,              0),
60         API_ENTRY(CPU, CX18_CPU_SET_INDEXTABLE,                 0),
61         API_ENTRY(CPU, CX18_CPU_SET_AUDIO_PARAMETERS,           0),
62         API_ENTRY(CPU, CX18_CPU_SET_VIDEO_MUTE,                 0),
63         API_ENTRY(CPU, CX18_CPU_SET_AUDIO_MUTE,                 0),
64         API_ENTRY(CPU, CX18_CPU_SET_MISC_PARAMETERS,            0),
65         API_ENTRY(CPU, CX18_CPU_SET_RAW_VBI_PARAM,              API_SLOW),
66         API_ENTRY(CPU, CX18_CPU_SET_CAPTURE_LINE_NO,            0),
67         API_ENTRY(CPU, CX18_CPU_SET_COPYRIGHT,                  0),
68         API_ENTRY(CPU, CX18_CPU_SET_AUDIO_PID,                  0),
69         API_ENTRY(CPU, CX18_CPU_SET_VIDEO_PID,                  0),
70         API_ENTRY(CPU, CX18_CPU_SET_VER_CROP_LINE,              0),
71         API_ENTRY(CPU, CX18_CPU_SET_GOP_STRUCTURE,              0),
72         API_ENTRY(CPU, CX18_CPU_SET_SCENE_CHANGE_DETECTION,     0),
73         API_ENTRY(CPU, CX18_CPU_SET_ASPECT_RATIO,               0),
74         API_ENTRY(CPU, CX18_CPU_SET_SKIP_INPUT_FRAME,           0),
75         API_ENTRY(CPU, CX18_CPU_SET_SLICED_VBI_PARAM,           0),
76         API_ENTRY(CPU, CX18_CPU_SET_USERDATA_PLACE_HOLDER,      0),
77         API_ENTRY(CPU, CX18_CPU_GET_ENC_PTS,                    0),
78         API_ENTRY(CPU, CX18_CPU_DE_SET_MDL_ACK,                 0),
79         API_ENTRY(CPU, CX18_CPU_DE_SET_MDL,                     API_FAST),
80         API_ENTRY(CPU, CX18_APU_RESETAI,                        API_FAST),
81         API_ENTRY(CPU, CX18_CPU_DE_RELEASE_MDL,                 API_SLOW),
82         API_ENTRY(0, 0,                                         0),
83 };
84
85 static const struct cx18_api_info *find_api_info(u32 cmd)
86 {
87         int i;
88
89         for (i = 0; api_info[i].cmd; i++)
90                 if (api_info[i].cmd == cmd)
91                         return &api_info[i];
92         return NULL;
93 }
94
95 long cx18_mb_ack(struct cx18 *cx, const struct cx18_mailbox *mb, int rpu)
96 {
97         struct cx18_mailbox __iomem *ack_mb;
98         u32 ack_irq;
99
100         switch (rpu) {
101         case APU:
102                 ack_irq = IRQ_EPU_TO_APU_ACK;
103                 ack_mb = &cx->scb->apu2epu_mb;
104                 break;
105         case CPU:
106                 ack_irq = IRQ_EPU_TO_CPU_ACK;
107                 ack_mb = &cx->scb->cpu2epu_mb;
108                 break;
109         default:
110                 CX18_WARN("Unhandled RPU (%d) for command %x ack\n",
111                           rpu, mb->cmd);
112                 return -EINVAL;
113         }
114
115         cx18_setup_page(cx, SCB_OFFSET);
116         cx18_write_sync(cx, mb->request, &ack_mb->ack);
117         cx18_write_reg_expect(cx, ack_irq, SW2_INT_SET, ack_irq, ack_irq);
118         return 0;
119 }
120
121
122 static int cx18_api_call(struct cx18 *cx, u32 cmd, int args, u32 data[])
123 {
124         const struct cx18_api_info *info = find_api_info(cmd);
125         u32 state, irq, req, ack, err;
126         struct cx18_mailbox __iomem *mb;
127         u32 __iomem *xpu_state;
128         wait_queue_head_t *waitq;
129         struct mutex *mb_lock;
130         int timeout = 100;
131         long unsigned int j, ret;
132         int i;
133
134         if (info == NULL) {
135                 CX18_WARN("unknown cmd %x\n", cmd);
136                 return -EINVAL;
137         }
138
139         if (cmd == CX18_CPU_DE_SET_MDL)
140                 CX18_DEBUG_HI_API("%s\n", info->name);
141         else
142                 CX18_DEBUG_API("%s\n", info->name);
143
144         switch (info->rpu) {
145         case APU:
146                 waitq = &cx->mb_apu_waitq;
147                 mb_lock = &cx->epu2apu_mb_lock;
148                 irq = IRQ_EPU_TO_APU;
149                 mb = &cx->scb->epu2apu_mb;
150                 xpu_state = &cx->scb->apu_state;
151                 break;
152         case CPU:
153                 waitq = &cx->mb_cpu_waitq;
154                 mb_lock = &cx->epu2cpu_mb_lock;
155                 irq = IRQ_EPU_TO_CPU;
156                 mb = &cx->scb->epu2cpu_mb;
157                 xpu_state = &cx->scb->cpu_state;
158                 break;
159         default:
160                 CX18_WARN("Unknown RPU (%d) for API call\n", info->rpu);
161                 return -EINVAL;
162         }
163
164         mutex_lock(mb_lock);
165         cx18_setup_page(cx, SCB_OFFSET);
166
167         /*
168          * Wait for an in-use mailbox to complete
169          *
170          * If the XPU is responding with Ack's, the mailbox shouldn't be in
171          * a busy state, since we serialize access to it on our end.
172          *
173          * If the wait for ack after sending a previous command was interrupted
174          * by a signal, we may get here and find a busy mailbox.  After waiting,
175          * mark it "not busy" from our end, if the XPU hasn't ack'ed it still.
176          */
177         state = cx18_readl(cx, xpu_state);
178         req = cx18_readl(cx, &mb->request);
179         j = msecs_to_jiffies(timeout);
180         ret = wait_event_timeout(*waitq,
181                                  (ack = cx18_readl(cx, &mb->ack)) == req,
182                                  j);
183         if (req != ack) {
184                 /* waited long enough, make the mbox "not busy" from our end */
185                 cx18_writel(cx, req, &mb->ack);
186                 CX18_ERR("mbox was found stuck busy when setting up for %s; "
187                          "clearing busy and trying to proceed\n", info->name);
188         } else if (ret != j)
189                 CX18_DEBUG_API("waited %u usecs for busy mbox to be acked\n",
190                                jiffies_to_usecs(j-ret));
191
192         /* Build the outgoing mailbox */
193         req = ((req & 0xfffffffe) == 0xfffffffe) ? 1 : req + 1;
194
195         cx18_writel(cx, cmd, &mb->cmd);
196         for (i = 0; i < args; i++)
197                 cx18_writel(cx, data[i], &mb->args[i]);
198         cx18_writel(cx, 0, &mb->error);
199         cx18_writel(cx, req, &mb->request);
200         cx18_writel(cx, req - 1, &mb->ack); /* ensure ack & req are distinct */
201
202         /* Notify the XPU and wait for it to send an Ack back */
203         if (info->flags & API_FAST)
204                 timeout /= 2;
205         j = msecs_to_jiffies(timeout);
206
207         CX18_DEBUG_HI_IRQ("sending interrupt SW1: %x to send %s\n",
208                           irq, info->name);
209         cx18_write_reg_expect(cx, irq, SW1_INT_SET, irq, irq);
210
211         ret = wait_event_interruptible_timeout(
212                        *waitq,
213                        cx18_readl(cx, &mb->ack) == cx18_readl(cx, &mb->request),
214                        j);
215         if (ret == 0) {
216                 /* Timed out */
217                 mutex_unlock(mb_lock);
218                 CX18_ERR("sending %s timed out waiting for RPU "
219                          "acknowledgement\n", info->name);
220                 return -EINVAL;
221         } else if (ret < 0) {
222                 /* Interrupted */
223                 mutex_unlock(mb_lock);
224                 CX18_WARN("sending %s was interrupted waiting for RPU"
225                           "acknowledgement\n", info->name);
226                 return -EINTR;
227         } else if (ret != j)
228                 CX18_DEBUG_HI_API("waited %u usecs for %s to be acked\n",
229                                   jiffies_to_usecs(j-ret), info->name);
230
231         /* Collect data returned by the XPU */
232         for (i = 0; i < MAX_MB_ARGUMENTS; i++)
233                 data[i] = cx18_readl(cx, &mb->args[i]);
234         err = cx18_readl(cx, &mb->error);
235         mutex_unlock(mb_lock);
236
237         /*
238          * Wait for XPU to perform extra actions for the caller in some cases.
239          * e.g. CX18_CPU_DE_RELEASE_MDL will cause the CPU to send all buffers
240          * back in a burst shortly thereafter
241          */
242         if (info->flags & API_SLOW)
243                 cx18_msleep_timeout(300, 0);
244
245         if (err)
246                 CX18_DEBUG_API("mailbox error %08x for command %s\n", err,
247                                 info->name);
248         return err ? -EIO : 0;
249 }
250
251 int cx18_api(struct cx18 *cx, u32 cmd, int args, u32 data[])
252 {
253         return cx18_api_call(cx, cmd, args, data);
254 }
255
256 static int cx18_set_filter_param(struct cx18_stream *s)
257 {
258         struct cx18 *cx = s->cx;
259         u32 mode;
260         int ret;
261
262         mode = (cx->filter_mode & 1) ? 2 : (cx->spatial_strength ? 1 : 0);
263         ret = cx18_vapi(cx, CX18_CPU_SET_FILTER_PARAM, 4,
264                         s->handle, 1, mode, cx->spatial_strength);
265         mode = (cx->filter_mode & 2) ? 2 : (cx->temporal_strength ? 1 : 0);
266         ret = ret ? ret : cx18_vapi(cx, CX18_CPU_SET_FILTER_PARAM, 4,
267                         s->handle, 0, mode, cx->temporal_strength);
268         ret = ret ? ret : cx18_vapi(cx, CX18_CPU_SET_FILTER_PARAM, 4,
269                         s->handle, 2, cx->filter_mode >> 2, 0);
270         return ret;
271 }
272
273 int cx18_api_func(void *priv, u32 cmd, int in, int out,
274                 u32 data[CX2341X_MBOX_MAX_DATA])
275 {
276         struct cx18 *cx = priv;
277         struct cx18_stream *s = &cx->streams[CX18_ENC_STREAM_TYPE_MPG];
278
279         switch (cmd) {
280         case CX2341X_ENC_SET_OUTPUT_PORT:
281                 return 0;
282         case CX2341X_ENC_SET_FRAME_RATE:
283                 return cx18_vapi(cx, CX18_CPU_SET_VIDEO_IN, 6,
284                                 s->handle, 0, 0, 0, 0, data[0]);
285         case CX2341X_ENC_SET_FRAME_SIZE:
286                 return cx18_vapi(cx, CX18_CPU_SET_VIDEO_RESOLUTION, 3,
287                                 s->handle, data[1], data[0]);
288         case CX2341X_ENC_SET_STREAM_TYPE:
289                 return cx18_vapi(cx, CX18_CPU_SET_STREAM_OUTPUT_TYPE, 2,
290                                 s->handle, data[0]);
291         case CX2341X_ENC_SET_ASPECT_RATIO:
292                 return cx18_vapi(cx, CX18_CPU_SET_ASPECT_RATIO, 2,
293                                 s->handle, data[0]);
294
295         case CX2341X_ENC_SET_GOP_PROPERTIES:
296                 return cx18_vapi(cx, CX18_CPU_SET_GOP_STRUCTURE, 3,
297                                 s->handle, data[0], data[1]);
298         case CX2341X_ENC_SET_GOP_CLOSURE:
299                 return 0;
300         case CX2341X_ENC_SET_AUDIO_PROPERTIES:
301                 return cx18_vapi(cx, CX18_CPU_SET_AUDIO_PARAMETERS, 2,
302                                 s->handle, data[0]);
303         case CX2341X_ENC_MUTE_AUDIO:
304                 return cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2,
305                                 s->handle, data[0]);
306         case CX2341X_ENC_SET_BIT_RATE:
307                 return cx18_vapi(cx, CX18_CPU_SET_VIDEO_RATE, 5,
308                                 s->handle, data[0], data[1], data[2], data[3]);
309         case CX2341X_ENC_MUTE_VIDEO:
310                 return cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2,
311                                 s->handle, data[0]);
312         case CX2341X_ENC_SET_FRAME_DROP_RATE:
313                 return cx18_vapi(cx, CX18_CPU_SET_SKIP_INPUT_FRAME, 2,
314                                 s->handle, data[0]);
315         case CX2341X_ENC_MISC:
316                 return cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 4,
317                                 s->handle, data[0], data[1], data[2]);
318         case CX2341X_ENC_SET_DNR_FILTER_MODE:
319                 cx->filter_mode = (data[0] & 3) | (data[1] << 2);
320                 return cx18_set_filter_param(s);
321         case CX2341X_ENC_SET_DNR_FILTER_PROPS:
322                 cx->spatial_strength = data[0];
323                 cx->temporal_strength = data[1];
324                 return cx18_set_filter_param(s);
325         case CX2341X_ENC_SET_SPATIAL_FILTER_TYPE:
326                 return cx18_vapi(cx, CX18_CPU_SET_SPATIAL_FILTER_TYPE, 3,
327                                 s->handle, data[0], data[1]);
328         case CX2341X_ENC_SET_CORING_LEVELS:
329                 return cx18_vapi(cx, CX18_CPU_SET_MEDIAN_CORING, 5,
330                                 s->handle, data[0], data[1], data[2], data[3]);
331         }
332         CX18_WARN("Unknown cmd %x\n", cmd);
333         return 0;
334 }
335
336 int cx18_vapi_result(struct cx18 *cx, u32 data[MAX_MB_ARGUMENTS],
337                 u32 cmd, int args, ...)
338 {
339         va_list ap;
340         int i;
341
342         va_start(ap, args);
343         for (i = 0; i < args; i++)
344                 data[i] = va_arg(ap, u32);
345         va_end(ap);
346         return cx18_api(cx, cmd, args, data);
347 }
348
349 int cx18_vapi(struct cx18 *cx, u32 cmd, int args, ...)
350 {
351         u32 data[MAX_MB_ARGUMENTS];
352         va_list ap;
353         int i;
354
355         if (cx == NULL) {
356                 CX18_ERR("cx == NULL (cmd=%x)\n", cmd);
357                 return 0;
358         }
359         if (args > MAX_MB_ARGUMENTS) {
360                 CX18_ERR("args too big (cmd=%x)\n", cmd);
361                 args = MAX_MB_ARGUMENTS;
362         }
363         va_start(ap, args);
364         for (i = 0; i < args; i++)
365                 data[i] = va_arg(ap, u32);
366         va_end(ap);
367         return cx18_api(cx, cmd, args, data);
368 }