cx18-mailbox.c 23 KB

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