cx18-mailbox.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. /*
  2. * cx18 mailbox functions
  3. *
  4. * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
  5. * Copyright (C) 2008 Andy Walls <awalls@radix.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. static const char *rpu_str[] = { "APU", "CPU", "EPU", "HPU" };
  31. #define API_FAST (1 << 2) /* Short timeout */
  32. #define API_SLOW (1 << 3) /* Additional 300ms timeout */
  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. #define API_ENTRY(rpu, x, f) { (x), (f), (rpu), #x }
  40. static const struct cx18_api_info api_info[] = {
  41. /* MPEG encoder API */
  42. API_ENTRY(CPU, CX18_CPU_SET_CHANNEL_TYPE, 0),
  43. API_ENTRY(CPU, CX18_EPU_DEBUG, 0),
  44. API_ENTRY(CPU, CX18_CREATE_TASK, 0),
  45. API_ENTRY(CPU, CX18_DESTROY_TASK, 0),
  46. API_ENTRY(CPU, CX18_CPU_CAPTURE_START, API_SLOW),
  47. API_ENTRY(CPU, CX18_CPU_CAPTURE_STOP, API_SLOW),
  48. API_ENTRY(CPU, CX18_CPU_CAPTURE_PAUSE, 0),
  49. API_ENTRY(CPU, CX18_CPU_CAPTURE_RESUME, 0),
  50. API_ENTRY(CPU, CX18_CPU_SET_CHANNEL_TYPE, 0),
  51. API_ENTRY(CPU, CX18_CPU_SET_STREAM_OUTPUT_TYPE, 0),
  52. API_ENTRY(CPU, CX18_CPU_SET_VIDEO_IN, 0),
  53. API_ENTRY(CPU, CX18_CPU_SET_VIDEO_RATE, 0),
  54. API_ENTRY(CPU, CX18_CPU_SET_VIDEO_RESOLUTION, 0),
  55. API_ENTRY(CPU, CX18_CPU_SET_FILTER_PARAM, 0),
  56. API_ENTRY(CPU, CX18_CPU_SET_SPATIAL_FILTER_TYPE, 0),
  57. API_ENTRY(CPU, CX18_CPU_SET_MEDIAN_CORING, 0),
  58. API_ENTRY(CPU, CX18_CPU_SET_INDEXTABLE, 0),
  59. API_ENTRY(CPU, CX18_CPU_SET_AUDIO_PARAMETERS, 0),
  60. API_ENTRY(CPU, CX18_CPU_SET_VIDEO_MUTE, 0),
  61. API_ENTRY(CPU, CX18_CPU_SET_AUDIO_MUTE, 0),
  62. API_ENTRY(CPU, CX18_CPU_SET_MISC_PARAMETERS, 0),
  63. API_ENTRY(CPU, CX18_CPU_SET_RAW_VBI_PARAM, API_SLOW),
  64. API_ENTRY(CPU, CX18_CPU_SET_CAPTURE_LINE_NO, 0),
  65. API_ENTRY(CPU, CX18_CPU_SET_COPYRIGHT, 0),
  66. API_ENTRY(CPU, CX18_CPU_SET_AUDIO_PID, 0),
  67. API_ENTRY(CPU, CX18_CPU_SET_VIDEO_PID, 0),
  68. API_ENTRY(CPU, CX18_CPU_SET_VER_CROP_LINE, 0),
  69. API_ENTRY(CPU, CX18_CPU_SET_GOP_STRUCTURE, 0),
  70. API_ENTRY(CPU, CX18_CPU_SET_SCENE_CHANGE_DETECTION, 0),
  71. API_ENTRY(CPU, CX18_CPU_SET_ASPECT_RATIO, 0),
  72. API_ENTRY(CPU, CX18_CPU_SET_SKIP_INPUT_FRAME, 0),
  73. API_ENTRY(CPU, CX18_CPU_SET_SLICED_VBI_PARAM, 0),
  74. API_ENTRY(CPU, CX18_CPU_SET_USERDATA_PLACE_HOLDER, 0),
  75. API_ENTRY(CPU, CX18_CPU_GET_ENC_PTS, 0),
  76. API_ENTRY(CPU, CX18_CPU_DE_SET_MDL_ACK, 0),
  77. API_ENTRY(CPU, CX18_CPU_DE_SET_MDL, API_FAST),
  78. API_ENTRY(CPU, CX18_CPU_DE_RELEASE_MDL, API_SLOW),
  79. API_ENTRY(APU, CX18_APU_RESETAI, 0),
  80. API_ENTRY(CPU, CX18_CPU_DEBUG_PEEK32, 0),
  81. API_ENTRY(0, 0, 0),
  82. };
  83. static const struct cx18_api_info *find_api_info(u32 cmd)
  84. {
  85. int i;
  86. for (i = 0; api_info[i].cmd; i++)
  87. if (api_info[i].cmd == cmd)
  88. return &api_info[i];
  89. return NULL;
  90. }
  91. static void dump_mb(struct cx18 *cx, struct cx18_mailbox *mb, char *name)
  92. {
  93. char argstr[MAX_MB_ARGUMENTS*11+1];
  94. char *p;
  95. int i;
  96. if (!(cx18_debug & CX18_DBGFLG_API))
  97. return;
  98. for (i = 0, p = argstr; i < MAX_MB_ARGUMENTS; i++, p += 11) {
  99. /* kernel snprintf() appends '\0' always */
  100. snprintf(p, 12, " %#010x", mb->args[i]);
  101. }
  102. CX18_DEBUG_API("%s: req %#010x ack %#010x cmd %#010x err %#010x args%s"
  103. "\n", name, mb->request, mb->ack, mb->cmd, mb->error, argstr);
  104. }
  105. /*
  106. * Functions that run in a work_queue work handling context
  107. */
  108. static void epu_dma_done(struct cx18 *cx, struct cx18_epu_work_order *order)
  109. {
  110. u32 handle, mdl_ack_count, id;
  111. struct cx18_mailbox *mb;
  112. struct cx18_mdl_ack *mdl_ack;
  113. struct cx18_stream *s;
  114. struct cx18_buffer *buf;
  115. int i;
  116. mb = &order->mb;
  117. handle = mb->args[0];
  118. s = cx18_handle_to_stream(cx, handle);
  119. if (s == NULL) {
  120. CX18_WARN("Got DMA done notification for unknown/inactive"
  121. " handle %d, %s mailbox seq no %d\n", handle,
  122. (order->flags & CX18_F_EWO_MB_STALE_UPON_RECEIPT) ?
  123. "stale" : "good", mb->request);
  124. return;
  125. }
  126. mdl_ack_count = mb->args[2];
  127. mdl_ack = order->mdl_ack;
  128. for (i = 0; i < mdl_ack_count; i++, mdl_ack++) {
  129. id = mdl_ack->id;
  130. /*
  131. * Simple integrity check for processing a stale (and possibly
  132. * inconsistent mailbox): make sure the buffer id is in the
  133. * valid range for the stream.
  134. *
  135. * We go through the trouble of dealing with stale mailboxes
  136. * because most of the time, the mailbox data is still valid and
  137. * unchanged (and in practice the firmware ping-pongs the
  138. * two mdl_ack buffers so mdl_acks are not stale).
  139. *
  140. * There are occasions when we get a half changed mailbox,
  141. * which this check catches for a handle & id mismatch. If the
  142. * handle and id do correspond, the worst case is that we
  143. * completely lost the old buffer, but pick up the new buffer
  144. * early (but the new mdl_ack is guaranteed to be good in this
  145. * case as the firmware wouldn't point us to a new mdl_ack until
  146. * it's filled in).
  147. *
  148. * cx18_queue_get buf() will detect the lost buffers
  149. * and send them back to q_free for fw rotation eventually.
  150. */
  151. if ((order->flags & CX18_F_EWO_MB_STALE_UPON_RECEIPT) &&
  152. !(id >= s->mdl_offset &&
  153. id < (s->mdl_offset + s->buffers))) {
  154. CX18_WARN("Fell behind! Ignoring stale mailbox with "
  155. " inconsistent data. Lost buffer for mailbox "
  156. "seq no %d\n", mb->request);
  157. break;
  158. }
  159. buf = cx18_queue_get_buf(s, id, mdl_ack->data_used);
  160. CX18_DEBUG_HI_DMA("DMA DONE for %s (buffer %d)\n", s->name, id);
  161. if (buf == NULL) {
  162. CX18_WARN("Could not find buf %d for stream %s\n",
  163. id, s->name);
  164. /* Put as many buffers as possible back into fw use */
  165. cx18_stream_load_fw_queue(s);
  166. continue;
  167. }
  168. if (s->type == CX18_ENC_STREAM_TYPE_TS && s->dvb.enabled) {
  169. CX18_DEBUG_HI_DMA("TS recv bytesused = %d\n",
  170. buf->bytesused);
  171. dvb_dmx_swfilter(&s->dvb.demux, buf->buf,
  172. buf->bytesused);
  173. }
  174. /* Put as many buffers as possible back into fw use */
  175. cx18_stream_load_fw_queue(s);
  176. /* Put back TS buffer, since it was removed from all queues */
  177. if (s->type == CX18_ENC_STREAM_TYPE_TS)
  178. cx18_stream_put_buf_fw(s, buf);
  179. }
  180. wake_up(&cx->dma_waitq);
  181. if (s->id != -1)
  182. wake_up(&s->waitq);
  183. }
  184. static void epu_debug(struct cx18 *cx, struct cx18_epu_work_order *order)
  185. {
  186. char *p;
  187. char *str = order->str;
  188. CX18_DEBUG_INFO("%x %s\n", order->mb.args[0], str);
  189. p = strchr(str, '.');
  190. if (!test_bit(CX18_F_I_LOADED_FW, &cx->i_flags) && p && p > str)
  191. CX18_INFO("FW version: %s\n", p - 1);
  192. }
  193. static void epu_cmd(struct cx18 *cx, struct cx18_epu_work_order *order)
  194. {
  195. switch (order->rpu) {
  196. case CPU:
  197. {
  198. switch (order->mb.cmd) {
  199. case CX18_EPU_DMA_DONE:
  200. epu_dma_done(cx, order);
  201. break;
  202. case CX18_EPU_DEBUG:
  203. epu_debug(cx, order);
  204. break;
  205. default:
  206. CX18_WARN("Unknown CPU to EPU mailbox command %#0x\n",
  207. order->mb.cmd);
  208. break;
  209. }
  210. break;
  211. }
  212. case APU:
  213. CX18_WARN("Unknown APU to EPU mailbox command %#0x\n",
  214. order->mb.cmd);
  215. break;
  216. default:
  217. break;
  218. }
  219. }
  220. static
  221. void free_epu_work_order(struct cx18 *cx, struct cx18_epu_work_order *order)
  222. {
  223. atomic_set(&order->pending, 0);
  224. }
  225. void cx18_epu_work_handler(struct work_struct *work)
  226. {
  227. struct cx18_epu_work_order *order =
  228. container_of(work, struct cx18_epu_work_order, work);
  229. struct cx18 *cx = order->cx;
  230. epu_cmd(cx, order);
  231. free_epu_work_order(cx, order);
  232. }
  233. /*
  234. * Functions that run in an interrupt handling context
  235. */
  236. static void mb_ack_irq(struct cx18 *cx, struct cx18_epu_work_order *order)
  237. {
  238. struct cx18_mailbox __iomem *ack_mb;
  239. u32 ack_irq, req;
  240. switch (order->rpu) {
  241. case APU:
  242. ack_irq = IRQ_EPU_TO_APU_ACK;
  243. ack_mb = &cx->scb->apu2epu_mb;
  244. break;
  245. case CPU:
  246. ack_irq = IRQ_EPU_TO_CPU_ACK;
  247. ack_mb = &cx->scb->cpu2epu_mb;
  248. break;
  249. default:
  250. CX18_WARN("Unhandled RPU (%d) for command %x ack\n",
  251. order->rpu, order->mb.cmd);
  252. return;
  253. }
  254. req = order->mb.request;
  255. /* Don't ack if the RPU has gotten impatient and timed us out */
  256. if (req != cx18_readl(cx, &ack_mb->request) ||
  257. req == cx18_readl(cx, &ack_mb->ack)) {
  258. CX18_DEBUG_WARN("Possibly falling behind: %s self-ack'ed our "
  259. "incoming %s to EPU mailbox (sequence no. %u) "
  260. "while processing\n",
  261. rpu_str[order->rpu], rpu_str[order->rpu], req);
  262. order->flags |= CX18_F_EWO_MB_STALE_WHILE_PROC;
  263. return;
  264. }
  265. cx18_writel(cx, req, &ack_mb->ack);
  266. cx18_write_reg_expect(cx, ack_irq, SW2_INT_SET, ack_irq, ack_irq);
  267. return;
  268. }
  269. static int epu_dma_done_irq(struct cx18 *cx, struct cx18_epu_work_order *order)
  270. {
  271. u32 handle, mdl_ack_offset, mdl_ack_count;
  272. struct cx18_mailbox *mb;
  273. mb = &order->mb;
  274. handle = mb->args[0];
  275. mdl_ack_offset = mb->args[1];
  276. mdl_ack_count = mb->args[2];
  277. if (handle == CX18_INVALID_TASK_HANDLE ||
  278. mdl_ack_count == 0 || mdl_ack_count > CX18_MAX_MDL_ACKS) {
  279. if ((order->flags & CX18_F_EWO_MB_STALE) == 0)
  280. mb_ack_irq(cx, order);
  281. return -1;
  282. }
  283. cx18_memcpy_fromio(cx, order->mdl_ack, cx->enc_mem + mdl_ack_offset,
  284. sizeof(struct cx18_mdl_ack) * mdl_ack_count);
  285. if ((order->flags & CX18_F_EWO_MB_STALE) == 0)
  286. mb_ack_irq(cx, order);
  287. return 1;
  288. }
  289. static
  290. int epu_debug_irq(struct cx18 *cx, struct cx18_epu_work_order *order)
  291. {
  292. u32 str_offset;
  293. char *str = order->str;
  294. str[0] = '\0';
  295. str_offset = order->mb.args[1];
  296. if (str_offset) {
  297. cx18_setup_page(cx, str_offset);
  298. cx18_memcpy_fromio(cx, str, cx->enc_mem + str_offset, 252);
  299. str[252] = '\0';
  300. cx18_setup_page(cx, SCB_OFFSET);
  301. }
  302. if ((order->flags & CX18_F_EWO_MB_STALE) == 0)
  303. mb_ack_irq(cx, order);
  304. return str_offset ? 1 : 0;
  305. }
  306. static inline
  307. int epu_cmd_irq(struct cx18 *cx, struct cx18_epu_work_order *order)
  308. {
  309. int ret = -1;
  310. switch (order->rpu) {
  311. case CPU:
  312. {
  313. switch (order->mb.cmd) {
  314. case CX18_EPU_DMA_DONE:
  315. ret = epu_dma_done_irq(cx, order);
  316. break;
  317. case CX18_EPU_DEBUG:
  318. ret = epu_debug_irq(cx, order);
  319. break;
  320. default:
  321. CX18_WARN("Unknown CPU to EPU mailbox command %#0x\n",
  322. order->mb.cmd);
  323. break;
  324. }
  325. break;
  326. }
  327. case APU:
  328. CX18_WARN("Unknown APU to EPU mailbox command %#0x\n",
  329. order->mb.cmd);
  330. break;
  331. default:
  332. break;
  333. }
  334. return ret;
  335. }
  336. static inline
  337. struct cx18_epu_work_order *alloc_epu_work_order_irq(struct cx18 *cx)
  338. {
  339. int i;
  340. struct cx18_epu_work_order *order = NULL;
  341. for (i = 0; i < CX18_MAX_EPU_WORK_ORDERS; i++) {
  342. /*
  343. * We only need "pending" atomic to inspect its contents,
  344. * and need not do a check and set because:
  345. * 1. Any work handler thread only clears "pending" and only
  346. * on one, particular work order at a time, per handler thread.
  347. * 2. "pending" is only set here, and we're serialized because
  348. * we're called in an IRQ handler context.
  349. */
  350. if (atomic_read(&cx->epu_work_order[i].pending) == 0) {
  351. order = &cx->epu_work_order[i];
  352. atomic_set(&order->pending, 1);
  353. break;
  354. }
  355. }
  356. return order;
  357. }
  358. void cx18_api_epu_cmd_irq(struct cx18 *cx, int rpu)
  359. {
  360. struct cx18_mailbox __iomem *mb;
  361. struct cx18_mailbox *order_mb;
  362. struct cx18_epu_work_order *order;
  363. int submit;
  364. switch (rpu) {
  365. case CPU:
  366. mb = &cx->scb->cpu2epu_mb;
  367. break;
  368. case APU:
  369. mb = &cx->scb->apu2epu_mb;
  370. break;
  371. default:
  372. return;
  373. }
  374. order = alloc_epu_work_order_irq(cx);
  375. if (order == NULL) {
  376. CX18_WARN("Unable to find blank work order form to schedule "
  377. "incoming mailbox command processing\n");
  378. return;
  379. }
  380. order->flags = 0;
  381. order->rpu = rpu;
  382. order_mb = &order->mb;
  383. /* mb->cmd and mb->args[0] through mb->args[2] */
  384. cx18_memcpy_fromio(cx, &order_mb->cmd, &mb->cmd, 4 * sizeof(u32));
  385. /* mb->request and mb->ack. N.B. we want to read mb->ack last */
  386. cx18_memcpy_fromio(cx, &order_mb->request, &mb->request,
  387. 2 * sizeof(u32));
  388. if (order_mb->request == order_mb->ack) {
  389. CX18_DEBUG_WARN("Possibly falling behind: %s self-ack'ed our "
  390. "incoming %s to EPU mailbox (sequence no. %u)"
  391. "\n",
  392. rpu_str[rpu], rpu_str[rpu], order_mb->request);
  393. dump_mb(cx, order_mb, "incoming");
  394. order->flags = CX18_F_EWO_MB_STALE_UPON_RECEIPT;
  395. }
  396. /*
  397. * Individual EPU command processing is responsible for ack-ing
  398. * a non-stale mailbox as soon as possible
  399. */
  400. submit = epu_cmd_irq(cx, order);
  401. if (submit > 0) {
  402. queue_work(cx->work_queue, &order->work);
  403. }
  404. }
  405. /*
  406. * Functions called from a non-interrupt, non work_queue context
  407. */
  408. static int cx18_api_call(struct cx18 *cx, u32 cmd, int args, u32 data[])
  409. {
  410. const struct cx18_api_info *info = find_api_info(cmd);
  411. u32 state, irq, req, ack, err;
  412. struct cx18_mailbox __iomem *mb;
  413. u32 __iomem *xpu_state;
  414. wait_queue_head_t *waitq;
  415. struct mutex *mb_lock;
  416. long int timeout, ret;
  417. int i;
  418. if (info == NULL) {
  419. CX18_WARN("unknown cmd %x\n", cmd);
  420. return -EINVAL;
  421. }
  422. if (cmd == CX18_CPU_DE_SET_MDL)
  423. CX18_DEBUG_HI_API("%s\n", info->name);
  424. else
  425. CX18_DEBUG_API("%s\n", info->name);
  426. switch (info->rpu) {
  427. case APU:
  428. waitq = &cx->mb_apu_waitq;
  429. mb_lock = &cx->epu2apu_mb_lock;
  430. irq = IRQ_EPU_TO_APU;
  431. mb = &cx->scb->epu2apu_mb;
  432. xpu_state = &cx->scb->apu_state;
  433. break;
  434. case CPU:
  435. waitq = &cx->mb_cpu_waitq;
  436. mb_lock = &cx->epu2cpu_mb_lock;
  437. irq = IRQ_EPU_TO_CPU;
  438. mb = &cx->scb->epu2cpu_mb;
  439. xpu_state = &cx->scb->cpu_state;
  440. break;
  441. default:
  442. CX18_WARN("Unknown RPU (%d) for API call\n", info->rpu);
  443. return -EINVAL;
  444. }
  445. mutex_lock(mb_lock);
  446. /*
  447. * Wait for an in-use mailbox to complete
  448. *
  449. * If the XPU is responding with Ack's, the mailbox shouldn't be in
  450. * a busy state, since we serialize access to it on our end.
  451. *
  452. * If the wait for ack after sending a previous command was interrupted
  453. * by a signal, we may get here and find a busy mailbox. After waiting,
  454. * mark it "not busy" from our end, if the XPU hasn't ack'ed it still.
  455. */
  456. state = cx18_readl(cx, xpu_state);
  457. req = cx18_readl(cx, &mb->request);
  458. timeout = msecs_to_jiffies(10);
  459. ret = wait_event_timeout(*waitq,
  460. (ack = cx18_readl(cx, &mb->ack)) == req,
  461. timeout);
  462. if (req != ack) {
  463. /* waited long enough, make the mbox "not busy" from our end */
  464. cx18_writel(cx, req, &mb->ack);
  465. CX18_ERR("mbox was found stuck busy when setting up for %s; "
  466. "clearing busy and trying to proceed\n", info->name);
  467. } else if (ret != timeout)
  468. CX18_DEBUG_API("waited %u msecs for busy mbox to be acked\n",
  469. jiffies_to_msecs(timeout-ret));
  470. /* Build the outgoing mailbox */
  471. req = ((req & 0xfffffffe) == 0xfffffffe) ? 1 : req + 1;
  472. cx18_writel(cx, cmd, &mb->cmd);
  473. for (i = 0; i < args; i++)
  474. cx18_writel(cx, data[i], &mb->args[i]);
  475. cx18_writel(cx, 0, &mb->error);
  476. cx18_writel(cx, req, &mb->request);
  477. cx18_writel(cx, req - 1, &mb->ack); /* ensure ack & req are distinct */
  478. /*
  479. * Notify the XPU and wait for it to send an Ack back
  480. */
  481. timeout = msecs_to_jiffies((info->flags & API_FAST) ? 10 : 20);
  482. CX18_DEBUG_HI_IRQ("sending interrupt SW1: %x to send %s\n",
  483. irq, info->name);
  484. cx18_write_reg_expect(cx, irq, SW1_INT_SET, irq, irq);
  485. ret = wait_event_timeout(
  486. *waitq,
  487. cx18_readl(cx, &mb->ack) == cx18_readl(cx, &mb->request),
  488. timeout);
  489. if (ret == 0) {
  490. /* Timed out */
  491. mutex_unlock(mb_lock);
  492. CX18_DEBUG_WARN("sending %s timed out waiting %d msecs for RPU "
  493. "acknowledgement\n",
  494. info->name, jiffies_to_msecs(timeout));
  495. return -EINVAL;
  496. }
  497. if (ret != timeout)
  498. CX18_DEBUG_HI_API("waited %u msecs for %s to be acked\n",
  499. jiffies_to_msecs(timeout-ret), info->name);
  500. /* Collect data returned by the XPU */
  501. for (i = 0; i < MAX_MB_ARGUMENTS; i++)
  502. data[i] = cx18_readl(cx, &mb->args[i]);
  503. err = cx18_readl(cx, &mb->error);
  504. mutex_unlock(mb_lock);
  505. /*
  506. * Wait for XPU to perform extra actions for the caller in some cases.
  507. * e.g. CX18_CPU_DE_RELEASE_MDL will cause the CPU to send all buffers
  508. * back in a burst shortly thereafter
  509. */
  510. if (info->flags & API_SLOW)
  511. cx18_msleep_timeout(300, 0);
  512. if (err)
  513. CX18_DEBUG_API("mailbox error %08x for command %s\n", err,
  514. info->name);
  515. return err ? -EIO : 0;
  516. }
  517. int cx18_api(struct cx18 *cx, u32 cmd, int args, u32 data[])
  518. {
  519. return cx18_api_call(cx, cmd, args, data);
  520. }
  521. static int cx18_set_filter_param(struct cx18_stream *s)
  522. {
  523. struct cx18 *cx = s->cx;
  524. u32 mode;
  525. int ret;
  526. mode = (cx->filter_mode & 1) ? 2 : (cx->spatial_strength ? 1 : 0);
  527. ret = cx18_vapi(cx, CX18_CPU_SET_FILTER_PARAM, 4,
  528. s->handle, 1, mode, cx->spatial_strength);
  529. mode = (cx->filter_mode & 2) ? 2 : (cx->temporal_strength ? 1 : 0);
  530. ret = ret ? ret : cx18_vapi(cx, CX18_CPU_SET_FILTER_PARAM, 4,
  531. s->handle, 0, mode, cx->temporal_strength);
  532. ret = ret ? ret : cx18_vapi(cx, CX18_CPU_SET_FILTER_PARAM, 4,
  533. s->handle, 2, cx->filter_mode >> 2, 0);
  534. return ret;
  535. }
  536. int cx18_api_func(void *priv, u32 cmd, int in, int out,
  537. u32 data[CX2341X_MBOX_MAX_DATA])
  538. {
  539. struct cx18_api_func_private *api_priv = priv;
  540. struct cx18 *cx = api_priv->cx;
  541. struct cx18_stream *s = api_priv->s;
  542. switch (cmd) {
  543. case CX2341X_ENC_SET_OUTPUT_PORT:
  544. return 0;
  545. case CX2341X_ENC_SET_FRAME_RATE:
  546. return cx18_vapi(cx, CX18_CPU_SET_VIDEO_IN, 6,
  547. s->handle, 0, 0, 0, 0, data[0]);
  548. case CX2341X_ENC_SET_FRAME_SIZE:
  549. return cx18_vapi(cx, CX18_CPU_SET_VIDEO_RESOLUTION, 3,
  550. s->handle, data[1], data[0]);
  551. case CX2341X_ENC_SET_STREAM_TYPE:
  552. return cx18_vapi(cx, CX18_CPU_SET_STREAM_OUTPUT_TYPE, 2,
  553. s->handle, data[0]);
  554. case CX2341X_ENC_SET_ASPECT_RATIO:
  555. return cx18_vapi(cx, CX18_CPU_SET_ASPECT_RATIO, 2,
  556. s->handle, data[0]);
  557. case CX2341X_ENC_SET_GOP_PROPERTIES:
  558. return cx18_vapi(cx, CX18_CPU_SET_GOP_STRUCTURE, 3,
  559. s->handle, data[0], data[1]);
  560. case CX2341X_ENC_SET_GOP_CLOSURE:
  561. return 0;
  562. case CX2341X_ENC_SET_AUDIO_PROPERTIES:
  563. return cx18_vapi(cx, CX18_CPU_SET_AUDIO_PARAMETERS, 2,
  564. s->handle, data[0]);
  565. case CX2341X_ENC_MUTE_AUDIO:
  566. return cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2,
  567. s->handle, data[0]);
  568. case CX2341X_ENC_SET_BIT_RATE:
  569. return cx18_vapi(cx, CX18_CPU_SET_VIDEO_RATE, 5,
  570. s->handle, data[0], data[1], data[2], data[3]);
  571. case CX2341X_ENC_MUTE_VIDEO:
  572. return cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2,
  573. s->handle, data[0]);
  574. case CX2341X_ENC_SET_FRAME_DROP_RATE:
  575. return cx18_vapi(cx, CX18_CPU_SET_SKIP_INPUT_FRAME, 2,
  576. s->handle, data[0]);
  577. case CX2341X_ENC_MISC:
  578. return cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 4,
  579. s->handle, data[0], data[1], data[2]);
  580. case CX2341X_ENC_SET_DNR_FILTER_MODE:
  581. cx->filter_mode = (data[0] & 3) | (data[1] << 2);
  582. return cx18_set_filter_param(s);
  583. case CX2341X_ENC_SET_DNR_FILTER_PROPS:
  584. cx->spatial_strength = data[0];
  585. cx->temporal_strength = data[1];
  586. return cx18_set_filter_param(s);
  587. case CX2341X_ENC_SET_SPATIAL_FILTER_TYPE:
  588. return cx18_vapi(cx, CX18_CPU_SET_SPATIAL_FILTER_TYPE, 3,
  589. s->handle, data[0], data[1]);
  590. case CX2341X_ENC_SET_CORING_LEVELS:
  591. return cx18_vapi(cx, CX18_CPU_SET_MEDIAN_CORING, 5,
  592. s->handle, data[0], data[1], data[2], data[3]);
  593. }
  594. CX18_WARN("Unknown cmd %x\n", cmd);
  595. return 0;
  596. }
  597. int cx18_vapi_result(struct cx18 *cx, u32 data[MAX_MB_ARGUMENTS],
  598. u32 cmd, int args, ...)
  599. {
  600. va_list ap;
  601. int i;
  602. va_start(ap, args);
  603. for (i = 0; i < args; i++)
  604. data[i] = va_arg(ap, u32);
  605. va_end(ap);
  606. return cx18_api(cx, cmd, args, data);
  607. }
  608. int cx18_vapi(struct cx18 *cx, u32 cmd, int args, ...)
  609. {
  610. u32 data[MAX_MB_ARGUMENTS];
  611. va_list ap;
  612. int i;
  613. if (cx == NULL) {
  614. CX18_ERR("cx == NULL (cmd=%x)\n", cmd);
  615. return 0;
  616. }
  617. if (args > MAX_MB_ARGUMENTS) {
  618. CX18_ERR("args too big (cmd=%x)\n", cmd);
  619. args = MAX_MB_ARGUMENTS;
  620. }
  621. va_start(ap, args);
  622. for (i = 0; i < args; i++)
  623. data[i] = va_arg(ap, u32);
  624. va_end(ap);
  625. return cx18_api(cx, cmd, args, data);
  626. }