cx18-mailbox.c 20 KB

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