1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3     mailbox functions
4     Copyright (C) 2003-2004  Kevin Thayer <nufan_wfk at yahoo.com>
5     Copyright (C) 2004  Chris Kennedy <c@groovy.org>
6     Copyright (C) 2005-2007  Hans Verkuil <hverkuil@xs4all.nl>
7 
8  */
9 
10 #include "ivtv-driver.h"
11 #include "ivtv-mailbox.h"
12 
13 #include <stdarg.h>
14 
15 /* Firmware mailbox flags*/
16 #define IVTV_MBOX_FIRMWARE_DONE 0x00000004
17 #define IVTV_MBOX_DRIVER_DONE   0x00000002
18 #define IVTV_MBOX_DRIVER_BUSY   0x00000001
19 #define IVTV_MBOX_FREE		0x00000000
20 
21 /* Firmware mailbox standard timeout */
22 #define IVTV_API_STD_TIMEOUT	0x02000000
23 
24 #define API_CACHE	 (1 << 0)	/* Allow the command to be stored in the cache */
25 #define API_RESULT	 (1 << 1)	/* Allow 1 second for this cmd to end */
26 #define API_FAST_RESULT	 (3 << 1)	/* Allow 0.1 second for this cmd to end */
27 #define API_DMA		 (1 << 3)	/* DMA mailbox, has special handling */
28 #define API_HIGH_VOL	 (1 << 5)	/* High volume command (i.e. called during encoding or decoding) */
29 #define API_NO_WAIT_MB	 (1 << 4)	/* Command may not wait for a free mailbox */
30 #define API_NO_WAIT_RES	 (1 << 5)	/* Command may not wait for the result */
31 #define API_NO_POLL	 (1 << 6)	/* Avoid pointless polling */
32 
33 struct ivtv_api_info {
34 	int flags;		/* Flags, see above */
35 	const char *name;	/* The name of the command */
36 };
37 
38 #define API_ENTRY(x, f) [x] = { (f), #x }
39 
40 static const struct ivtv_api_info api_info[256] = {
41 	/* MPEG encoder API */
42 	API_ENTRY(CX2341X_ENC_PING_FW,			API_FAST_RESULT),
43 	API_ENTRY(CX2341X_ENC_START_CAPTURE,		API_RESULT | API_NO_POLL),
44 	API_ENTRY(CX2341X_ENC_STOP_CAPTURE,		API_RESULT),
45 	API_ENTRY(CX2341X_ENC_SET_AUDIO_ID,		API_CACHE),
46 	API_ENTRY(CX2341X_ENC_SET_VIDEO_ID,		API_CACHE),
47 	API_ENTRY(CX2341X_ENC_SET_PCR_ID,		API_CACHE),
48 	API_ENTRY(CX2341X_ENC_SET_FRAME_RATE,		API_CACHE),
49 	API_ENTRY(CX2341X_ENC_SET_FRAME_SIZE,		API_CACHE),
50 	API_ENTRY(CX2341X_ENC_SET_BIT_RATE,		API_CACHE),
51 	API_ENTRY(CX2341X_ENC_SET_GOP_PROPERTIES,	API_CACHE),
52 	API_ENTRY(CX2341X_ENC_SET_ASPECT_RATIO,		API_CACHE),
53 	API_ENTRY(CX2341X_ENC_SET_DNR_FILTER_MODE,	API_CACHE),
54 	API_ENTRY(CX2341X_ENC_SET_DNR_FILTER_PROPS,	API_CACHE),
55 	API_ENTRY(CX2341X_ENC_SET_CORING_LEVELS,	API_CACHE),
56 	API_ENTRY(CX2341X_ENC_SET_SPATIAL_FILTER_TYPE,	API_CACHE),
57 	API_ENTRY(CX2341X_ENC_SET_VBI_LINE,		API_RESULT),
58 	API_ENTRY(CX2341X_ENC_SET_STREAM_TYPE,		API_CACHE),
59 	API_ENTRY(CX2341X_ENC_SET_OUTPUT_PORT,		API_CACHE),
60 	API_ENTRY(CX2341X_ENC_SET_AUDIO_PROPERTIES,	API_CACHE),
61 	API_ENTRY(CX2341X_ENC_HALT_FW,			API_FAST_RESULT),
62 	API_ENTRY(CX2341X_ENC_GET_VERSION,		API_FAST_RESULT),
63 	API_ENTRY(CX2341X_ENC_SET_GOP_CLOSURE,		API_CACHE),
64 	API_ENTRY(CX2341X_ENC_GET_SEQ_END,		API_RESULT),
65 	API_ENTRY(CX2341X_ENC_SET_PGM_INDEX_INFO,	API_FAST_RESULT),
66 	API_ENTRY(CX2341X_ENC_SET_VBI_CONFIG,		API_RESULT),
67 	API_ENTRY(CX2341X_ENC_SET_DMA_BLOCK_SIZE,	API_CACHE),
68 	API_ENTRY(CX2341X_ENC_GET_PREV_DMA_INFO_MB_10,	API_FAST_RESULT),
69 	API_ENTRY(CX2341X_ENC_GET_PREV_DMA_INFO_MB_9,	API_FAST_RESULT),
70 	API_ENTRY(CX2341X_ENC_SCHED_DMA_TO_HOST,	API_DMA | API_HIGH_VOL),
71 	API_ENTRY(CX2341X_ENC_INITIALIZE_INPUT,		API_RESULT),
72 	API_ENTRY(CX2341X_ENC_SET_FRAME_DROP_RATE,	API_CACHE),
73 	API_ENTRY(CX2341X_ENC_PAUSE_ENCODER,		API_RESULT),
74 	API_ENTRY(CX2341X_ENC_REFRESH_INPUT,		API_NO_WAIT_MB | API_HIGH_VOL),
75 	API_ENTRY(CX2341X_ENC_SET_COPYRIGHT,		API_CACHE),
76 	API_ENTRY(CX2341X_ENC_SET_EVENT_NOTIFICATION,	API_RESULT),
77 	API_ENTRY(CX2341X_ENC_SET_NUM_VSYNC_LINES,	API_CACHE),
78 	API_ENTRY(CX2341X_ENC_SET_PLACEHOLDER,		API_CACHE),
79 	API_ENTRY(CX2341X_ENC_MUTE_VIDEO,		API_RESULT),
80 	API_ENTRY(CX2341X_ENC_MUTE_AUDIO,		API_RESULT),
81 	API_ENTRY(CX2341X_ENC_SET_VERT_CROP_LINE,	API_FAST_RESULT),
82 	API_ENTRY(CX2341X_ENC_MISC,			API_FAST_RESULT),
83 	/* Obsolete PULLDOWN API command */
84 	API_ENTRY(0xb1,					API_CACHE),
85 
86 	/* MPEG decoder API */
87 	API_ENTRY(CX2341X_DEC_PING_FW,			API_FAST_RESULT),
88 	API_ENTRY(CX2341X_DEC_START_PLAYBACK,		API_RESULT | API_NO_POLL),
89 	API_ENTRY(CX2341X_DEC_STOP_PLAYBACK,		API_RESULT),
90 	API_ENTRY(CX2341X_DEC_SET_PLAYBACK_SPEED,	API_RESULT),
91 	API_ENTRY(CX2341X_DEC_STEP_VIDEO,		API_RESULT),
92 	API_ENTRY(CX2341X_DEC_SET_DMA_BLOCK_SIZE,	API_CACHE),
93 	API_ENTRY(CX2341X_DEC_GET_XFER_INFO,		API_FAST_RESULT),
94 	API_ENTRY(CX2341X_DEC_GET_DMA_STATUS,		API_FAST_RESULT),
95 	API_ENTRY(CX2341X_DEC_SCHED_DMA_FROM_HOST,	API_DMA | API_HIGH_VOL),
96 	API_ENTRY(CX2341X_DEC_PAUSE_PLAYBACK,		API_RESULT),
97 	API_ENTRY(CX2341X_DEC_HALT_FW,			API_FAST_RESULT),
98 	API_ENTRY(CX2341X_DEC_SET_STANDARD,		API_CACHE),
99 	API_ENTRY(CX2341X_DEC_GET_VERSION,		API_FAST_RESULT),
100 	API_ENTRY(CX2341X_DEC_SET_STREAM_INPUT,		API_CACHE),
101 	API_ENTRY(CX2341X_DEC_GET_TIMING_INFO,		API_RESULT /*| API_NO_WAIT_RES*/),
102 	API_ENTRY(CX2341X_DEC_SET_AUDIO_MODE,		API_CACHE),
103 	API_ENTRY(CX2341X_DEC_SET_EVENT_NOTIFICATION,	API_RESULT),
104 	API_ENTRY(CX2341X_DEC_SET_DISPLAY_BUFFERS,	API_CACHE),
105 	API_ENTRY(CX2341X_DEC_EXTRACT_VBI,		API_RESULT),
106 	API_ENTRY(CX2341X_DEC_SET_DECODER_SOURCE,	API_FAST_RESULT),
107 	API_ENTRY(CX2341X_DEC_SET_PREBUFFERING,		API_CACHE),
108 
109 	/* OSD API */
110 	API_ENTRY(CX2341X_OSD_GET_FRAMEBUFFER,		API_FAST_RESULT),
111 	API_ENTRY(CX2341X_OSD_GET_PIXEL_FORMAT,		API_FAST_RESULT),
112 	API_ENTRY(CX2341X_OSD_SET_PIXEL_FORMAT,		API_CACHE),
113 	API_ENTRY(CX2341X_OSD_GET_STATE,		API_FAST_RESULT),
114 	API_ENTRY(CX2341X_OSD_SET_STATE,		API_CACHE),
115 	API_ENTRY(CX2341X_OSD_GET_OSD_COORDS,		API_FAST_RESULT),
116 	API_ENTRY(CX2341X_OSD_SET_OSD_COORDS,		API_CACHE),
117 	API_ENTRY(CX2341X_OSD_GET_SCREEN_COORDS,	API_FAST_RESULT),
118 	API_ENTRY(CX2341X_OSD_SET_SCREEN_COORDS,	API_CACHE),
119 	API_ENTRY(CX2341X_OSD_GET_GLOBAL_ALPHA,		API_FAST_RESULT),
120 	API_ENTRY(CX2341X_OSD_SET_GLOBAL_ALPHA,		API_CACHE),
121 	API_ENTRY(CX2341X_OSD_SET_BLEND_COORDS,		API_CACHE),
122 	API_ENTRY(CX2341X_OSD_GET_FLICKER_STATE,	API_FAST_RESULT),
123 	API_ENTRY(CX2341X_OSD_SET_FLICKER_STATE,	API_CACHE),
124 	API_ENTRY(CX2341X_OSD_BLT_COPY,			API_RESULT),
125 	API_ENTRY(CX2341X_OSD_BLT_FILL,			API_RESULT),
126 	API_ENTRY(CX2341X_OSD_BLT_TEXT,			API_RESULT),
127 	API_ENTRY(CX2341X_OSD_SET_FRAMEBUFFER_WINDOW,	API_CACHE),
128 	API_ENTRY(CX2341X_OSD_SET_CHROMA_KEY,		API_CACHE),
129 	API_ENTRY(CX2341X_OSD_GET_ALPHA_CONTENT_INDEX,	API_FAST_RESULT),
130 	API_ENTRY(CX2341X_OSD_SET_ALPHA_CONTENT_INDEX,	API_CACHE)
131 };
132 
133 static int try_mailbox(struct ivtv *itv, struct ivtv_mailbox_data *mbdata, int mb)
134 {
135 	u32 flags = readl(&mbdata->mbox[mb].flags);
136 	int is_free = flags == IVTV_MBOX_FREE || (flags & IVTV_MBOX_FIRMWARE_DONE);
137 
138 	/* if the mailbox is free, then try to claim it */
139 	if (is_free && !test_and_set_bit(mb, &mbdata->busy)) {
140 		write_sync(IVTV_MBOX_DRIVER_BUSY, &mbdata->mbox[mb].flags);
141 		return 1;
142 	}
143 	return 0;
144 }
145 
146 /* Try to find a free mailbox. Note mailbox 0 is reserved for DMA and so is not
147    attempted here. */
148 static int get_mailbox(struct ivtv *itv, struct ivtv_mailbox_data *mbdata, int flags)
149 {
150 	unsigned long then = jiffies;
151 	int i, mb;
152 	int max_mbox = mbdata->max_mbox;
153 	int retries = 100;
154 
155 	/* All slow commands use the same mailbox, serializing them and also
156 	   leaving the other mailbox free for simple fast commands. */
157 	if ((flags & API_FAST_RESULT) == API_RESULT)
158 		max_mbox = 1;
159 
160 	/* find free non-DMA mailbox */
161 	for (i = 0; i < retries; i++) {
162 		for (mb = 1; mb <= max_mbox; mb++)
163 			if (try_mailbox(itv, mbdata, mb))
164 				return mb;
165 
166 		/* Sleep before a retry, if not atomic */
167 		if (!(flags & API_NO_WAIT_MB)) {
168 			if (time_after(jiffies,
169 				       then + msecs_to_jiffies(10*retries)))
170 			       break;
171 			ivtv_msleep_timeout(10, 0);
172 		}
173 	}
174 	return -ENODEV;
175 }
176 
177 static void write_mailbox(volatile struct ivtv_mailbox __iomem *mbox, int cmd, int args, u32 data[])
178 {
179 	int i;
180 
181 	write_sync(cmd, &mbox->cmd);
182 	write_sync(IVTV_API_STD_TIMEOUT, &mbox->timeout);
183 
184 	for (i = 0; i < CX2341X_MBOX_MAX_DATA; i++)
185 		write_sync(data[i], &mbox->data[i]);
186 
187 	write_sync(IVTV_MBOX_DRIVER_DONE | IVTV_MBOX_DRIVER_BUSY, &mbox->flags);
188 }
189 
190 static void clear_all_mailboxes(struct ivtv *itv, struct ivtv_mailbox_data *mbdata)
191 {
192 	int i;
193 
194 	for (i = 0; i <= mbdata->max_mbox; i++) {
195 		IVTV_DEBUG_WARN("Clearing mailbox %d: cmd 0x%08x flags 0x%08x\n",
196 			i, readl(&mbdata->mbox[i].cmd), readl(&mbdata->mbox[i].flags));
197 		write_sync(0, &mbdata->mbox[i].flags);
198 		clear_bit(i, &mbdata->busy);
199 	}
200 }
201 
202 static int ivtv_api_call(struct ivtv *itv, int cmd, int args, u32 data[])
203 {
204 	struct ivtv_mailbox_data *mbdata = (cmd >= 128) ? &itv->enc_mbox : &itv->dec_mbox;
205 	volatile struct ivtv_mailbox __iomem *mbox;
206 	int api_timeout = msecs_to_jiffies(1000);
207 	int flags, mb, i;
208 	unsigned long then;
209 
210 	/* sanity checks */
211 	if (NULL == mbdata) {
212 		IVTV_ERR("No mailbox allocated\n");
213 		return -ENODEV;
214 	}
215 	if (args < 0 || args > CX2341X_MBOX_MAX_DATA ||
216 	    cmd < 0 || cmd > 255 || api_info[cmd].name == NULL) {
217 		IVTV_ERR("Invalid MB call: cmd = 0x%02x, args = %d\n", cmd, args);
218 		return -EINVAL;
219 	}
220 
221 	if (api_info[cmd].flags & API_HIGH_VOL) {
222 	    IVTV_DEBUG_HI_MB("MB Call: %s\n", api_info[cmd].name);
223 	}
224 	else {
225 	    IVTV_DEBUG_MB("MB Call: %s\n", api_info[cmd].name);
226 	}
227 
228 	/* clear possibly uninitialized part of data array */
229 	for (i = args; i < CX2341X_MBOX_MAX_DATA; i++)
230 		data[i] = 0;
231 
232 	/* If this command was issued within the last 30 minutes and with identical
233 	   data, then just return 0 as there is no need to issue this command again.
234 	   Just an optimization to prevent unnecessary use of mailboxes. */
235 	if (itv->api_cache[cmd].last_jiffies &&
236 	    time_before(jiffies,
237 			itv->api_cache[cmd].last_jiffies +
238 			msecs_to_jiffies(1800000)) &&
239 	    !memcmp(data, itv->api_cache[cmd].data, sizeof(itv->api_cache[cmd].data))) {
240 		itv->api_cache[cmd].last_jiffies = jiffies;
241 		return 0;
242 	}
243 
244 	flags = api_info[cmd].flags;
245 
246 	if (flags & API_DMA) {
247 		for (i = 0; i < 100; i++) {
248 			mb = i % (mbdata->max_mbox + 1);
249 			if (try_mailbox(itv, mbdata, mb)) {
250 				write_mailbox(&mbdata->mbox[mb], cmd, args, data);
251 				clear_bit(mb, &mbdata->busy);
252 				return 0;
253 			}
254 			IVTV_DEBUG_WARN("%s: mailbox %d not free %08x\n",
255 					api_info[cmd].name, mb, readl(&mbdata->mbox[mb].flags));
256 		}
257 		IVTV_WARN("Could not find free DMA mailbox for %s\n", api_info[cmd].name);
258 		clear_all_mailboxes(itv, mbdata);
259 		return -EBUSY;
260 	}
261 
262 	if ((flags & API_FAST_RESULT) == API_FAST_RESULT)
263 		api_timeout = msecs_to_jiffies(100);
264 
265 	mb = get_mailbox(itv, mbdata, flags);
266 	if (mb < 0) {
267 		IVTV_DEBUG_WARN("No free mailbox found (%s)\n", api_info[cmd].name);
268 		clear_all_mailboxes(itv, mbdata);
269 		return -EBUSY;
270 	}
271 	mbox = &mbdata->mbox[mb];
272 	write_mailbox(mbox, cmd, args, data);
273 	if (flags & API_CACHE) {
274 		memcpy(itv->api_cache[cmd].data, data, sizeof(itv->api_cache[cmd].data));
275 		itv->api_cache[cmd].last_jiffies = jiffies;
276 	}
277 	if ((flags & API_RESULT) == 0) {
278 		clear_bit(mb, &mbdata->busy);
279 		return 0;
280 	}
281 
282 	/* Get results */
283 	then = jiffies;
284 
285 	if (!(flags & API_NO_POLL)) {
286 		/* First try to poll, then switch to delays */
287 		for (i = 0; i < 100; i++) {
288 			if (readl(&mbox->flags) & IVTV_MBOX_FIRMWARE_DONE)
289 				break;
290 		}
291 	}
292 	while (!(readl(&mbox->flags) & IVTV_MBOX_FIRMWARE_DONE)) {
293 		if (time_after(jiffies, then + api_timeout)) {
294 			IVTV_DEBUG_WARN("Could not get result (%s)\n", api_info[cmd].name);
295 			/* reset the mailbox, but it is likely too late already */
296 			write_sync(0, &mbox->flags);
297 			clear_bit(mb, &mbdata->busy);
298 			return -EIO;
299 		}
300 		if (flags & API_NO_WAIT_RES)
301 			mdelay(1);
302 		else
303 			ivtv_msleep_timeout(1, 0);
304 	}
305 	if (time_after(jiffies, then + msecs_to_jiffies(100)))
306 		IVTV_DEBUG_WARN("%s took %u jiffies\n",
307 				api_info[cmd].name,
308 				jiffies_to_msecs(jiffies - then));
309 
310 	for (i = 0; i < CX2341X_MBOX_MAX_DATA; i++)
311 		data[i] = readl(&mbox->data[i]);
312 	write_sync(0, &mbox->flags);
313 	clear_bit(mb, &mbdata->busy);
314 	return 0;
315 }
316 
317 int ivtv_api(struct ivtv *itv, int cmd, int args, u32 data[])
318 {
319 	int res = ivtv_api_call(itv, cmd, args, data);
320 
321 	/* Allow a single retry, probably already too late though.
322 	   If there is no free mailbox then that is usually an indication
323 	   of a more serious problem. */
324 	return (res == -EBUSY) ? ivtv_api_call(itv, cmd, args, data) : res;
325 }
326 
327 int ivtv_api_func(void *priv, u32 cmd, int in, int out, u32 data[CX2341X_MBOX_MAX_DATA])
328 {
329 	return ivtv_api(priv, cmd, in, data);
330 }
331 
332 int ivtv_vapi_result(struct ivtv *itv, u32 data[CX2341X_MBOX_MAX_DATA], int cmd, int args, ...)
333 {
334 	va_list ap;
335 	int i;
336 
337 	va_start(ap, args);
338 	for (i = 0; i < args; i++) {
339 		data[i] = va_arg(ap, u32);
340 	}
341 	va_end(ap);
342 	return ivtv_api(itv, cmd, args, data);
343 }
344 
345 int ivtv_vapi(struct ivtv *itv, int cmd, int args, ...)
346 {
347 	u32 data[CX2341X_MBOX_MAX_DATA];
348 	va_list ap;
349 	int i;
350 
351 	va_start(ap, args);
352 	for (i = 0; i < args; i++) {
353 		data[i] = va_arg(ap, u32);
354 	}
355 	va_end(ap);
356 	return ivtv_api(itv, cmd, args, data);
357 }
358 
359 /* This one is for stuff that can't sleep.. irq handlers, etc.. */
360 void ivtv_api_get_data(struct ivtv_mailbox_data *mbdata, int mb,
361 		       int argc, u32 data[])
362 {
363 	volatile u32 __iomem *p = mbdata->mbox[mb].data;
364 	int i;
365 	for (i = 0; i < argc; i++, p++)
366 		data[i] = readl(p);
367 }
368 
369 /* Wipe api cache */
370 void ivtv_mailbox_cache_invalidate(struct ivtv *itv)
371 {
372 	int i;
373 	for (i = 0; i < 256; i++)
374 		itv->api_cache[i].last_jiffies = 0;
375 }
376