ivtv-mailbox.c 13 KB

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