cx18-mailbox.c 22 KB

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