cx18-mailbox.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. /*
  2. * cx18 mailbox functions
  3. *
  4. * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
  5. * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
  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. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  20. * 02111-1307 USA
  21. */
  22. #include <stdarg.h>
  23. #include "cx18-driver.h"
  24. #include "cx18-io.h"
  25. #include "cx18-scb.h"
  26. #include "cx18-irq.h"
  27. #include "cx18-mailbox.h"
  28. #include "cx18-queue.h"
  29. #include "cx18-streams.h"
  30. #include "cx18-alsa-pcm.h" /* FIXME make configurable */
  31. static const char *rpu_str[] = { "APU", "CPU", "EPU", "HPU" };
  32. #define API_FAST (1 << 2) /* Short timeout */
  33. #define API_SLOW (1 << 3) /* Additional 300ms timeout */
  34. struct cx18_api_info {
  35. u32 cmd;
  36. u8 flags; /* Flags, see above */
  37. u8 rpu; /* Processing unit */
  38. const char *name; /* The name of the command */
  39. };
  40. #define API_ENTRY(rpu, x, f) { (x), (f), (rpu), #x }
  41. static const struct cx18_api_info api_info[] = {
  42. /* MPEG encoder API */
  43. API_ENTRY(CPU, CX18_CPU_SET_CHANNEL_TYPE, 0),
  44. API_ENTRY(CPU, CX18_EPU_DEBUG, 0),
  45. API_ENTRY(CPU, CX18_CREATE_TASK, 0),
  46. API_ENTRY(CPU, CX18_DESTROY_TASK, 0),
  47. API_ENTRY(CPU, CX18_CPU_CAPTURE_START, API_SLOW),
  48. API_ENTRY(CPU, CX18_CPU_CAPTURE_STOP, API_SLOW),
  49. API_ENTRY(CPU, CX18_CPU_CAPTURE_PAUSE, 0),
  50. API_ENTRY(CPU, CX18_CPU_CAPTURE_RESUME, 0),
  51. API_ENTRY(CPU, CX18_CPU_SET_CHANNEL_TYPE, 0),
  52. API_ENTRY(CPU, CX18_CPU_SET_STREAM_OUTPUT_TYPE, 0),
  53. API_ENTRY(CPU, CX18_CPU_SET_VIDEO_IN, 0),
  54. API_ENTRY(CPU, CX18_CPU_SET_VIDEO_RATE, 0),
  55. API_ENTRY(CPU, CX18_CPU_SET_VIDEO_RESOLUTION, 0),
  56. API_ENTRY(CPU, CX18_CPU_SET_FILTER_PARAM, 0),
  57. API_ENTRY(CPU, CX18_CPU_SET_SPATIAL_FILTER_TYPE, 0),
  58. API_ENTRY(CPU, CX18_CPU_SET_MEDIAN_CORING, 0),
  59. API_ENTRY(CPU, CX18_CPU_SET_INDEXTABLE, 0),
  60. API_ENTRY(CPU, CX18_CPU_SET_AUDIO_PARAMETERS, 0),
  61. API_ENTRY(CPU, CX18_CPU_SET_VIDEO_MUTE, 0),
  62. API_ENTRY(CPU, CX18_CPU_SET_AUDIO_MUTE, 0),
  63. API_ENTRY(CPU, CX18_CPU_SET_MISC_PARAMETERS, 0),
  64. API_ENTRY(CPU, CX18_CPU_SET_RAW_VBI_PARAM, API_SLOW),
  65. API_ENTRY(CPU, CX18_CPU_SET_CAPTURE_LINE_NO, 0),
  66. API_ENTRY(CPU, CX18_CPU_SET_COPYRIGHT, 0),
  67. API_ENTRY(CPU, CX18_CPU_SET_AUDIO_PID, 0),
  68. API_ENTRY(CPU, CX18_CPU_SET_VIDEO_PID, 0),
  69. API_ENTRY(CPU, CX18_CPU_SET_VER_CROP_LINE, 0),
  70. API_ENTRY(CPU, CX18_CPU_SET_GOP_STRUCTURE, 0),
  71. API_ENTRY(CPU, CX18_CPU_SET_SCENE_CHANGE_DETECTION, 0),
  72. API_ENTRY(CPU, CX18_CPU_SET_ASPECT_RATIO, 0),
  73. API_ENTRY(CPU, CX18_CPU_SET_SKIP_INPUT_FRAME, 0),
  74. API_ENTRY(CPU, CX18_CPU_SET_SLICED_VBI_PARAM, 0),
  75. API_ENTRY(CPU, CX18_CPU_SET_USERDATA_PLACE_HOLDER, 0),
  76. API_ENTRY(CPU, CX18_CPU_GET_ENC_PTS, 0),
  77. API_ENTRY(CPU, CX18_CPU_SET_VFC_PARAM, 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_CPU_DE_RELEASE_MDL, API_SLOW),
  81. API_ENTRY(APU, CX18_APU_START, 0),
  82. API_ENTRY(APU, CX18_APU_STOP, 0),
  83. API_ENTRY(APU, CX18_APU_RESETAI, 0),
  84. API_ENTRY(CPU, CX18_CPU_DEBUG_PEEK32, 0),
  85. API_ENTRY(0, 0, 0),
  86. };
  87. static const struct cx18_api_info *find_api_info(u32 cmd)
  88. {
  89. int i;
  90. for (i = 0; api_info[i].cmd; i++)
  91. if (api_info[i].cmd == cmd)
  92. return &api_info[i];
  93. return NULL;
  94. }
  95. /* Call with buf of n*11+1 bytes */
  96. static char *u32arr2hex(u32 data[], int n, char *buf)
  97. {
  98. char *p;
  99. int i;
  100. for (i = 0, p = buf; i < n; i++, p += 11) {
  101. /* kernel snprintf() appends '\0' always */
  102. snprintf(p, 12, " %#010x", data[i]);
  103. }
  104. *p = '\0';
  105. return buf;
  106. }
  107. static void dump_mb(struct cx18 *cx, struct cx18_mailbox *mb, char *name)
  108. {
  109. char argstr[MAX_MB_ARGUMENTS*11+1];
  110. if (!(cx18_debug & CX18_DBGFLG_API))
  111. return;
  112. CX18_DEBUG_API("%s: req %#010x ack %#010x cmd %#010x err %#010x args%s"
  113. "\n", name, mb->request, mb->ack, mb->cmd, mb->error,
  114. u32arr2hex(mb->args, MAX_MB_ARGUMENTS, argstr));
  115. }
  116. /*
  117. * Functions that run in a work_queue work handling context
  118. */
  119. static void cx18_mdl_send_to_dvb(struct cx18_stream *s, struct cx18_mdl *mdl)
  120. {
  121. struct cx18_buffer *buf;
  122. if (s->dvb == NULL || !s->dvb->enabled || mdl->bytesused == 0)
  123. return;
  124. /* We ignore mdl and buf readpos accounting here - it doesn't matter */
  125. /* The likely case */
  126. if (list_is_singular(&mdl->buf_list)) {
  127. buf = list_first_entry(&mdl->buf_list, struct cx18_buffer,
  128. list);
  129. if (buf->bytesused)
  130. dvb_dmx_swfilter(&s->dvb->demux,
  131. buf->buf, buf->bytesused);
  132. return;
  133. }
  134. list_for_each_entry(buf, &mdl->buf_list, list) {
  135. if (buf->bytesused == 0)
  136. break;
  137. dvb_dmx_swfilter(&s->dvb->demux, buf->buf, buf->bytesused);
  138. }
  139. }
  140. static void cx18_mdl_send_to_videobuf(struct cx18_stream *s,
  141. struct cx18_mdl *mdl)
  142. {
  143. struct cx18_videobuf_buffer *vb_buf;
  144. struct cx18_buffer *buf;
  145. u8 *p, u;
  146. u32 offset = 0;
  147. int dispatch = 0;
  148. int i;
  149. if (mdl->bytesused == 0)
  150. return;
  151. /* Acquire a videobuf buffer, clone to and and release it */
  152. spin_lock(&s->vb_lock);
  153. if (list_empty(&s->vb_capture))
  154. goto out;
  155. vb_buf = list_entry(s->vb_capture.next, struct cx18_videobuf_buffer,
  156. vb.queue);
  157. p = videobuf_to_vmalloc(&vb_buf->vb);
  158. if (!p)
  159. goto out;
  160. offset = vb_buf->bytes_used;
  161. list_for_each_entry(buf, &mdl->buf_list, list) {
  162. if (buf->bytesused == 0)
  163. break;
  164. if ((offset + buf->bytesused) <= vb_buf->vb.bsize) {
  165. memcpy(p + offset, buf->buf, buf->bytesused);
  166. offset += buf->bytesused;
  167. vb_buf->bytes_used += buf->bytesused;
  168. }
  169. }
  170. /* If we've filled the buffer as per the callers res then dispatch it */
  171. if (vb_buf->bytes_used >= (vb_buf->vb.width * vb_buf->vb.height * 2)) {
  172. dispatch = 1;
  173. vb_buf->bytes_used = 0;
  174. }
  175. /* */
  176. if (dispatch) {
  177. if (s->pixelformat == V4L2_PIX_FMT_YUYV) {
  178. /* UYVY to YUYV */
  179. for (i = 0; i < (720 * 480 * 2); i += 2) {
  180. u = *(p + i);
  181. *(p + i) = *(p + i + 1);
  182. *(p + i + 1) = u;
  183. }
  184. }
  185. do_gettimeofday(&vb_buf->vb.ts);
  186. list_del(&vb_buf->vb.queue);
  187. vb_buf->vb.state = VIDEOBUF_DONE;
  188. wake_up(&vb_buf->vb.done);
  189. }
  190. mod_timer(&s->vb_timeout, jiffies + (HZ / 10));
  191. out:
  192. spin_unlock(&s->vb_lock);
  193. }
  194. static void cx18_mdl_send_to_alsa(struct cx18 *cx, struct cx18_stream *s,
  195. struct cx18_mdl *mdl)
  196. {
  197. struct cx18_buffer *buf;
  198. if (mdl->bytesused == 0)
  199. return;
  200. /* We ignore mdl and buf readpos accounting here - it doesn't matter */
  201. /* The likely case */
  202. if (list_is_singular(&mdl->buf_list)) {
  203. buf = list_first_entry(&mdl->buf_list, struct cx18_buffer,
  204. list);
  205. if (buf->bytesused)
  206. cx->pcm_announce_callback(cx->alsa, buf->buf,
  207. buf->bytesused);
  208. return;
  209. }
  210. list_for_each_entry(buf, &mdl->buf_list, list) {
  211. if (buf->bytesused == 0)
  212. break;
  213. cx->pcm_announce_callback(cx->alsa, buf->buf, buf->bytesused);
  214. }
  215. }
  216. static void epu_dma_done(struct cx18 *cx, struct cx18_in_work_order *order)
  217. {
  218. u32 handle, mdl_ack_count, id;
  219. struct cx18_mailbox *mb;
  220. struct cx18_mdl_ack *mdl_ack;
  221. struct cx18_stream *s;
  222. struct cx18_mdl *mdl;
  223. int i;
  224. mb = &order->mb;
  225. handle = mb->args[0];
  226. s = cx18_handle_to_stream(cx, handle);
  227. if (s == NULL) {
  228. CX18_WARN("Got DMA done notification for unknown/inactive"
  229. " handle %d, %s mailbox seq no %d\n", handle,
  230. (order->flags & CX18_F_EWO_MB_STALE_UPON_RECEIPT) ?
  231. "stale" : "good", mb->request);
  232. return;
  233. }
  234. mdl_ack_count = mb->args[2];
  235. mdl_ack = order->mdl_ack;
  236. for (i = 0; i < mdl_ack_count; i++, mdl_ack++) {
  237. id = mdl_ack->id;
  238. /*
  239. * Simple integrity check for processing a stale (and possibly
  240. * inconsistent mailbox): make sure the MDL id is in the
  241. * valid range for the stream.
  242. *
  243. * We go through the trouble of dealing with stale mailboxes
  244. * because most of the time, the mailbox data is still valid and
  245. * unchanged (and in practice the firmware ping-pongs the
  246. * two mdl_ack buffers so mdl_acks are not stale).
  247. *
  248. * There are occasions when we get a half changed mailbox,
  249. * which this check catches for a handle & id mismatch. If the
  250. * handle and id do correspond, the worst case is that we
  251. * completely lost the old MDL, but pick up the new MDL
  252. * early (but the new mdl_ack is guaranteed to be good in this
  253. * case as the firmware wouldn't point us to a new mdl_ack until
  254. * it's filled in).
  255. *
  256. * cx18_queue_get_mdl() will detect the lost MDLs
  257. * and send them back to q_free for fw rotation eventually.
  258. */
  259. if ((order->flags & CX18_F_EWO_MB_STALE_UPON_RECEIPT) &&
  260. !(id >= s->mdl_base_idx &&
  261. id < (s->mdl_base_idx + s->buffers))) {
  262. CX18_WARN("Fell behind! Ignoring stale mailbox with "
  263. " inconsistent data. Lost MDL for mailbox "
  264. "seq no %d\n", mb->request);
  265. break;
  266. }
  267. mdl = cx18_queue_get_mdl(s, id, mdl_ack->data_used);
  268. CX18_DEBUG_HI_DMA("DMA DONE for %s (MDL %d)\n", s->name, id);
  269. if (mdl == NULL) {
  270. CX18_WARN("Could not find MDL %d for stream %s\n",
  271. id, s->name);
  272. continue;
  273. }
  274. CX18_DEBUG_HI_DMA("%s recv bytesused = %d\n",
  275. s->name, mdl->bytesused);
  276. if (s->type == CX18_ENC_STREAM_TYPE_TS) {
  277. cx18_mdl_send_to_dvb(s, mdl);
  278. cx18_enqueue(s, mdl, &s->q_free);
  279. } else if (s->type == CX18_ENC_STREAM_TYPE_PCM) {
  280. /* Pass the data to cx18-alsa */
  281. if (cx->pcm_announce_callback != NULL) {
  282. cx18_mdl_send_to_alsa(cx, s, mdl);
  283. cx18_enqueue(s, mdl, &s->q_free);
  284. } else {
  285. cx18_enqueue(s, mdl, &s->q_full);
  286. }
  287. } else if (s->type == CX18_ENC_STREAM_TYPE_YUV) {
  288. cx18_mdl_send_to_videobuf(s, mdl);
  289. cx18_enqueue(s, mdl, &s->q_free);
  290. } else {
  291. cx18_enqueue(s, mdl, &s->q_full);
  292. if (s->type == CX18_ENC_STREAM_TYPE_IDX)
  293. cx18_stream_rotate_idx_mdls(cx);
  294. }
  295. }
  296. /* Put as many MDLs as possible back into fw use */
  297. cx18_stream_load_fw_queue(s);
  298. wake_up(&cx->dma_waitq);
  299. if (s->id != -1)
  300. wake_up(&s->waitq);
  301. }
  302. static void epu_debug(struct cx18 *cx, struct cx18_in_work_order *order)
  303. {
  304. char *p;
  305. char *str = order->str;
  306. CX18_DEBUG_INFO("%x %s\n", order->mb.args[0], str);
  307. p = strchr(str, '.');
  308. if (!test_bit(CX18_F_I_LOADED_FW, &cx->i_flags) && p && p > str)
  309. CX18_INFO("FW version: %s\n", p - 1);
  310. }
  311. static void epu_cmd(struct cx18 *cx, struct cx18_in_work_order *order)
  312. {
  313. switch (order->rpu) {
  314. case CPU:
  315. {
  316. switch (order->mb.cmd) {
  317. case CX18_EPU_DMA_DONE:
  318. epu_dma_done(cx, order);
  319. break;
  320. case CX18_EPU_DEBUG:
  321. epu_debug(cx, order);
  322. break;
  323. default:
  324. CX18_WARN("Unknown CPU to EPU mailbox command %#0x\n",
  325. order->mb.cmd);
  326. break;
  327. }
  328. break;
  329. }
  330. case APU:
  331. CX18_WARN("Unknown APU to EPU mailbox command %#0x\n",
  332. order->mb.cmd);
  333. break;
  334. default:
  335. break;
  336. }
  337. }
  338. static
  339. void free_in_work_order(struct cx18 *cx, struct cx18_in_work_order *order)
  340. {
  341. atomic_set(&order->pending, 0);
  342. }
  343. void cx18_in_work_handler(struct work_struct *work)
  344. {
  345. struct cx18_in_work_order *order =
  346. container_of(work, struct cx18_in_work_order, work);
  347. struct cx18 *cx = order->cx;
  348. epu_cmd(cx, order);
  349. free_in_work_order(cx, order);
  350. }
  351. /*
  352. * Functions that run in an interrupt handling context
  353. */
  354. static void mb_ack_irq(struct cx18 *cx, struct cx18_in_work_order *order)
  355. {
  356. struct cx18_mailbox __iomem *ack_mb;
  357. u32 ack_irq, req;
  358. switch (order->rpu) {
  359. case APU:
  360. ack_irq = IRQ_EPU_TO_APU_ACK;
  361. ack_mb = &cx->scb->apu2epu_mb;
  362. break;
  363. case CPU:
  364. ack_irq = IRQ_EPU_TO_CPU_ACK;
  365. ack_mb = &cx->scb->cpu2epu_mb;
  366. break;
  367. default:
  368. CX18_WARN("Unhandled RPU (%d) for command %x ack\n",
  369. order->rpu, order->mb.cmd);
  370. return;
  371. }
  372. req = order->mb.request;
  373. /* Don't ack if the RPU has gotten impatient and timed us out */
  374. if (req != cx18_readl(cx, &ack_mb->request) ||
  375. req == cx18_readl(cx, &ack_mb->ack)) {
  376. CX18_DEBUG_WARN("Possibly falling behind: %s self-ack'ed our "
  377. "incoming %s to EPU mailbox (sequence no. %u) "
  378. "while processing\n",
  379. rpu_str[order->rpu], rpu_str[order->rpu], req);
  380. order->flags |= CX18_F_EWO_MB_STALE_WHILE_PROC;
  381. return;
  382. }
  383. cx18_writel(cx, req, &ack_mb->ack);
  384. cx18_write_reg_expect(cx, ack_irq, SW2_INT_SET, ack_irq, ack_irq);
  385. return;
  386. }
  387. static int epu_dma_done_irq(struct cx18 *cx, struct cx18_in_work_order *order)
  388. {
  389. u32 handle, mdl_ack_offset, mdl_ack_count;
  390. struct cx18_mailbox *mb;
  391. mb = &order->mb;
  392. handle = mb->args[0];
  393. mdl_ack_offset = mb->args[1];
  394. mdl_ack_count = mb->args[2];
  395. if (handle == CX18_INVALID_TASK_HANDLE ||
  396. mdl_ack_count == 0 || mdl_ack_count > CX18_MAX_MDL_ACKS) {
  397. if ((order->flags & CX18_F_EWO_MB_STALE) == 0)
  398. mb_ack_irq(cx, order);
  399. return -1;
  400. }
  401. cx18_memcpy_fromio(cx, order->mdl_ack, cx->enc_mem + mdl_ack_offset,
  402. sizeof(struct cx18_mdl_ack) * mdl_ack_count);
  403. if ((order->flags & CX18_F_EWO_MB_STALE) == 0)
  404. mb_ack_irq(cx, order);
  405. return 1;
  406. }
  407. static
  408. int epu_debug_irq(struct cx18 *cx, struct cx18_in_work_order *order)
  409. {
  410. u32 str_offset;
  411. char *str = order->str;
  412. str[0] = '\0';
  413. str_offset = order->mb.args[1];
  414. if (str_offset) {
  415. cx18_setup_page(cx, str_offset);
  416. cx18_memcpy_fromio(cx, str, cx->enc_mem + str_offset, 252);
  417. str[252] = '\0';
  418. cx18_setup_page(cx, SCB_OFFSET);
  419. }
  420. if ((order->flags & CX18_F_EWO_MB_STALE) == 0)
  421. mb_ack_irq(cx, order);
  422. return str_offset ? 1 : 0;
  423. }
  424. static inline
  425. int epu_cmd_irq(struct cx18 *cx, struct cx18_in_work_order *order)
  426. {
  427. int ret = -1;
  428. switch (order->rpu) {
  429. case CPU:
  430. {
  431. switch (order->mb.cmd) {
  432. case CX18_EPU_DMA_DONE:
  433. ret = epu_dma_done_irq(cx, order);
  434. break;
  435. case CX18_EPU_DEBUG:
  436. ret = epu_debug_irq(cx, order);
  437. break;
  438. default:
  439. CX18_WARN("Unknown CPU to EPU mailbox command %#0x\n",
  440. order->mb.cmd);
  441. break;
  442. }
  443. break;
  444. }
  445. case APU:
  446. CX18_WARN("Unknown APU to EPU mailbox command %#0x\n",
  447. order->mb.cmd);
  448. break;
  449. default:
  450. break;
  451. }
  452. return ret;
  453. }
  454. static inline
  455. struct cx18_in_work_order *alloc_in_work_order_irq(struct cx18 *cx)
  456. {
  457. int i;
  458. struct cx18_in_work_order *order = NULL;
  459. for (i = 0; i < CX18_MAX_IN_WORK_ORDERS; i++) {
  460. /*
  461. * We only need "pending" atomic to inspect its contents,
  462. * and need not do a check and set because:
  463. * 1. Any work handler thread only clears "pending" and only
  464. * on one, particular work order at a time, per handler thread.
  465. * 2. "pending" is only set here, and we're serialized because
  466. * we're called in an IRQ handler context.
  467. */
  468. if (atomic_read(&cx->in_work_order[i].pending) == 0) {
  469. order = &cx->in_work_order[i];
  470. atomic_set(&order->pending, 1);
  471. break;
  472. }
  473. }
  474. return order;
  475. }
  476. void cx18_api_epu_cmd_irq(struct cx18 *cx, int rpu)
  477. {
  478. struct cx18_mailbox __iomem *mb;
  479. struct cx18_mailbox *order_mb;
  480. struct cx18_in_work_order *order;
  481. int submit;
  482. switch (rpu) {
  483. case CPU:
  484. mb = &cx->scb->cpu2epu_mb;
  485. break;
  486. case APU:
  487. mb = &cx->scb->apu2epu_mb;
  488. break;
  489. default:
  490. return;
  491. }
  492. order = alloc_in_work_order_irq(cx);
  493. if (order == NULL) {
  494. CX18_WARN("Unable to find blank work order form to schedule "
  495. "incoming mailbox command processing\n");
  496. return;
  497. }
  498. order->flags = 0;
  499. order->rpu = rpu;
  500. order_mb = &order->mb;
  501. /* mb->cmd and mb->args[0] through mb->args[2] */
  502. cx18_memcpy_fromio(cx, &order_mb->cmd, &mb->cmd, 4 * sizeof(u32));
  503. /* mb->request and mb->ack. N.B. we want to read mb->ack last */
  504. cx18_memcpy_fromio(cx, &order_mb->request, &mb->request,
  505. 2 * sizeof(u32));
  506. if (order_mb->request == order_mb->ack) {
  507. CX18_DEBUG_WARN("Possibly falling behind: %s self-ack'ed our "
  508. "incoming %s to EPU mailbox (sequence no. %u)"
  509. "\n",
  510. rpu_str[rpu], rpu_str[rpu], order_mb->request);
  511. if (cx18_debug & CX18_DBGFLG_WARN)
  512. dump_mb(cx, order_mb, "incoming");
  513. order->flags = CX18_F_EWO_MB_STALE_UPON_RECEIPT;
  514. }
  515. /*
  516. * Individual EPU command processing is responsible for ack-ing
  517. * a non-stale mailbox as soon as possible
  518. */
  519. submit = epu_cmd_irq(cx, order);
  520. if (submit > 0) {
  521. queue_work(cx->in_work_queue, &order->work);
  522. }
  523. }
  524. /*
  525. * Functions called from a non-interrupt, non work_queue context
  526. */
  527. static int cx18_api_call(struct cx18 *cx, u32 cmd, int args, u32 data[])
  528. {
  529. const struct cx18_api_info *info = find_api_info(cmd);
  530. u32 state, irq, req, ack, err;
  531. struct cx18_mailbox __iomem *mb;
  532. u32 __iomem *xpu_state;
  533. wait_queue_head_t *waitq;
  534. struct mutex *mb_lock;
  535. unsigned long int t0, timeout, ret;
  536. int i;
  537. char argstr[MAX_MB_ARGUMENTS*11+1];
  538. DEFINE_WAIT(w);
  539. if (info == NULL) {
  540. CX18_WARN("unknown cmd %x\n", cmd);
  541. return -EINVAL;
  542. }
  543. if (cx18_debug & CX18_DBGFLG_API) { /* only call u32arr2hex if needed */
  544. if (cmd == CX18_CPU_DE_SET_MDL) {
  545. if (cx18_debug & CX18_DBGFLG_HIGHVOL)
  546. CX18_DEBUG_HI_API("%s\tcmd %#010x args%s\n",
  547. info->name, cmd,
  548. u32arr2hex(data, args, argstr));
  549. } else
  550. CX18_DEBUG_API("%s\tcmd %#010x args%s\n",
  551. info->name, cmd,
  552. u32arr2hex(data, args, argstr));
  553. }
  554. switch (info->rpu) {
  555. case APU:
  556. waitq = &cx->mb_apu_waitq;
  557. mb_lock = &cx->epu2apu_mb_lock;
  558. irq = IRQ_EPU_TO_APU;
  559. mb = &cx->scb->epu2apu_mb;
  560. xpu_state = &cx->scb->apu_state;
  561. break;
  562. case CPU:
  563. waitq = &cx->mb_cpu_waitq;
  564. mb_lock = &cx->epu2cpu_mb_lock;
  565. irq = IRQ_EPU_TO_CPU;
  566. mb = &cx->scb->epu2cpu_mb;
  567. xpu_state = &cx->scb->cpu_state;
  568. break;
  569. default:
  570. CX18_WARN("Unknown RPU (%d) for API call\n", info->rpu);
  571. return -EINVAL;
  572. }
  573. mutex_lock(mb_lock);
  574. /*
  575. * Wait for an in-use mailbox to complete
  576. *
  577. * If the XPU is responding with Ack's, the mailbox shouldn't be in
  578. * a busy state, since we serialize access to it on our end.
  579. *
  580. * If the wait for ack after sending a previous command was interrupted
  581. * by a signal, we may get here and find a busy mailbox. After waiting,
  582. * mark it "not busy" from our end, if the XPU hasn't ack'ed it still.
  583. */
  584. state = cx18_readl(cx, xpu_state);
  585. req = cx18_readl(cx, &mb->request);
  586. timeout = msecs_to_jiffies(10);
  587. ret = wait_event_timeout(*waitq,
  588. (ack = cx18_readl(cx, &mb->ack)) == req,
  589. timeout);
  590. if (req != ack) {
  591. /* waited long enough, make the mbox "not busy" from our end */
  592. cx18_writel(cx, req, &mb->ack);
  593. CX18_ERR("mbox was found stuck busy when setting up for %s; "
  594. "clearing busy and trying to proceed\n", info->name);
  595. } else if (ret != timeout)
  596. CX18_DEBUG_API("waited %u msecs for busy mbox to be acked\n",
  597. jiffies_to_msecs(timeout-ret));
  598. /* Build the outgoing mailbox */
  599. req = ((req & 0xfffffffe) == 0xfffffffe) ? 1 : req + 1;
  600. cx18_writel(cx, cmd, &mb->cmd);
  601. for (i = 0; i < args; i++)
  602. cx18_writel(cx, data[i], &mb->args[i]);
  603. cx18_writel(cx, 0, &mb->error);
  604. cx18_writel(cx, req, &mb->request);
  605. cx18_writel(cx, req - 1, &mb->ack); /* ensure ack & req are distinct */
  606. /*
  607. * Notify the XPU and wait for it to send an Ack back
  608. */
  609. timeout = msecs_to_jiffies((info->flags & API_FAST) ? 10 : 20);
  610. CX18_DEBUG_HI_IRQ("sending interrupt SW1: %x to send %s\n",
  611. irq, info->name);
  612. /* So we don't miss the wakeup, prepare to wait before notifying fw */
  613. prepare_to_wait(waitq, &w, TASK_UNINTERRUPTIBLE);
  614. cx18_write_reg_expect(cx, irq, SW1_INT_SET, irq, irq);
  615. t0 = jiffies;
  616. ack = cx18_readl(cx, &mb->ack);
  617. if (ack != req) {
  618. schedule_timeout(timeout);
  619. ret = jiffies - t0;
  620. ack = cx18_readl(cx, &mb->ack);
  621. } else {
  622. ret = jiffies - t0;
  623. }
  624. finish_wait(waitq, &w);
  625. if (req != ack) {
  626. mutex_unlock(mb_lock);
  627. if (ret >= timeout) {
  628. /* Timed out */
  629. CX18_DEBUG_WARN("sending %s timed out waiting %d msecs "
  630. "for RPU acknowledgement\n",
  631. info->name, jiffies_to_msecs(ret));
  632. } else {
  633. CX18_DEBUG_WARN("woken up before mailbox ack was ready "
  634. "after submitting %s to RPU. only "
  635. "waited %d msecs on req %u but awakened"
  636. " with unmatched ack %u\n",
  637. info->name,
  638. jiffies_to_msecs(ret),
  639. req, ack);
  640. }
  641. return -EINVAL;
  642. }
  643. if (ret >= timeout)
  644. CX18_DEBUG_WARN("failed to be awakened upon RPU acknowledgment "
  645. "sending %s; timed out waiting %d msecs\n",
  646. info->name, jiffies_to_msecs(ret));
  647. else
  648. CX18_DEBUG_HI_API("waited %u msecs for %s to be acked\n",
  649. jiffies_to_msecs(ret), info->name);
  650. /* Collect data returned by the XPU */
  651. for (i = 0; i < MAX_MB_ARGUMENTS; i++)
  652. data[i] = cx18_readl(cx, &mb->args[i]);
  653. err = cx18_readl(cx, &mb->error);
  654. mutex_unlock(mb_lock);
  655. /*
  656. * Wait for XPU to perform extra actions for the caller in some cases.
  657. * e.g. CX18_CPU_DE_RELEASE_MDL will cause the CPU to send all MDLs
  658. * back in a burst shortly thereafter
  659. */
  660. if (info->flags & API_SLOW)
  661. cx18_msleep_timeout(300, 0);
  662. if (err)
  663. CX18_DEBUG_API("mailbox error %08x for command %s\n", err,
  664. info->name);
  665. return err ? -EIO : 0;
  666. }
  667. int cx18_api(struct cx18 *cx, u32 cmd, int args, u32 data[])
  668. {
  669. return cx18_api_call(cx, cmd, args, data);
  670. }
  671. static int cx18_set_filter_param(struct cx18_stream *s)
  672. {
  673. struct cx18 *cx = s->cx;
  674. u32 mode;
  675. int ret;
  676. mode = (cx->filter_mode & 1) ? 2 : (cx->spatial_strength ? 1 : 0);
  677. ret = cx18_vapi(cx, CX18_CPU_SET_FILTER_PARAM, 4,
  678. s->handle, 1, mode, cx->spatial_strength);
  679. mode = (cx->filter_mode & 2) ? 2 : (cx->temporal_strength ? 1 : 0);
  680. ret = ret ? ret : cx18_vapi(cx, CX18_CPU_SET_FILTER_PARAM, 4,
  681. s->handle, 0, mode, cx->temporal_strength);
  682. ret = ret ? ret : cx18_vapi(cx, CX18_CPU_SET_FILTER_PARAM, 4,
  683. s->handle, 2, cx->filter_mode >> 2, 0);
  684. return ret;
  685. }
  686. int cx18_api_func(void *priv, u32 cmd, int in, int out,
  687. u32 data[CX2341X_MBOX_MAX_DATA])
  688. {
  689. struct cx18_stream *s = priv;
  690. struct cx18 *cx = s->cx;
  691. switch (cmd) {
  692. case CX2341X_ENC_SET_OUTPUT_PORT:
  693. return 0;
  694. case CX2341X_ENC_SET_FRAME_RATE:
  695. return cx18_vapi(cx, CX18_CPU_SET_VIDEO_IN, 6,
  696. s->handle, 0, 0, 0, 0, data[0]);
  697. case CX2341X_ENC_SET_FRAME_SIZE:
  698. return cx18_vapi(cx, CX18_CPU_SET_VIDEO_RESOLUTION, 3,
  699. s->handle, data[1], data[0]);
  700. case CX2341X_ENC_SET_STREAM_TYPE:
  701. return cx18_vapi(cx, CX18_CPU_SET_STREAM_OUTPUT_TYPE, 2,
  702. s->handle, data[0]);
  703. case CX2341X_ENC_SET_ASPECT_RATIO:
  704. return cx18_vapi(cx, CX18_CPU_SET_ASPECT_RATIO, 2,
  705. s->handle, data[0]);
  706. case CX2341X_ENC_SET_GOP_PROPERTIES:
  707. return cx18_vapi(cx, CX18_CPU_SET_GOP_STRUCTURE, 3,
  708. s->handle, data[0], data[1]);
  709. case CX2341X_ENC_SET_GOP_CLOSURE:
  710. return 0;
  711. case CX2341X_ENC_SET_AUDIO_PROPERTIES:
  712. return cx18_vapi(cx, CX18_CPU_SET_AUDIO_PARAMETERS, 2,
  713. s->handle, data[0]);
  714. case CX2341X_ENC_MUTE_AUDIO:
  715. return cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2,
  716. s->handle, data[0]);
  717. case CX2341X_ENC_SET_BIT_RATE:
  718. return cx18_vapi(cx, CX18_CPU_SET_VIDEO_RATE, 5,
  719. s->handle, data[0], data[1], data[2], data[3]);
  720. case CX2341X_ENC_MUTE_VIDEO:
  721. return cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2,
  722. s->handle, data[0]);
  723. case CX2341X_ENC_SET_FRAME_DROP_RATE:
  724. return cx18_vapi(cx, CX18_CPU_SET_SKIP_INPUT_FRAME, 2,
  725. s->handle, data[0]);
  726. case CX2341X_ENC_MISC:
  727. return cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 4,
  728. s->handle, data[0], data[1], data[2]);
  729. case CX2341X_ENC_SET_DNR_FILTER_MODE:
  730. cx->filter_mode = (data[0] & 3) | (data[1] << 2);
  731. return cx18_set_filter_param(s);
  732. case CX2341X_ENC_SET_DNR_FILTER_PROPS:
  733. cx->spatial_strength = data[0];
  734. cx->temporal_strength = data[1];
  735. return cx18_set_filter_param(s);
  736. case CX2341X_ENC_SET_SPATIAL_FILTER_TYPE:
  737. return cx18_vapi(cx, CX18_CPU_SET_SPATIAL_FILTER_TYPE, 3,
  738. s->handle, data[0], data[1]);
  739. case CX2341X_ENC_SET_CORING_LEVELS:
  740. return cx18_vapi(cx, CX18_CPU_SET_MEDIAN_CORING, 5,
  741. s->handle, data[0], data[1], data[2], data[3]);
  742. }
  743. CX18_WARN("Unknown cmd %x\n", cmd);
  744. return 0;
  745. }
  746. int cx18_vapi_result(struct cx18 *cx, u32 data[MAX_MB_ARGUMENTS],
  747. u32 cmd, int args, ...)
  748. {
  749. va_list ap;
  750. int i;
  751. va_start(ap, args);
  752. for (i = 0; i < args; i++)
  753. data[i] = va_arg(ap, u32);
  754. va_end(ap);
  755. return cx18_api(cx, cmd, args, data);
  756. }
  757. int cx18_vapi(struct cx18 *cx, u32 cmd, int args, ...)
  758. {
  759. u32 data[MAX_MB_ARGUMENTS];
  760. va_list ap;
  761. int i;
  762. if (cx == NULL) {
  763. CX18_ERR("cx == NULL (cmd=%x)\n", cmd);
  764. return 0;
  765. }
  766. if (args > MAX_MB_ARGUMENTS) {
  767. CX18_ERR("args too big (cmd=%x)\n", cmd);
  768. args = MAX_MB_ARGUMENTS;
  769. }
  770. va_start(ap, args);
  771. for (i = 0; i < args; i++)
  772. data[i] = va_arg(ap, u32);
  773. va_end(ap);
  774. return cx18_api(cx, cmd, args, data);
  775. }