1
0

ewl_x280_irq (copy).c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*------------------------------------------------------------------------------
  2. -- --
  3. -- This software is confidential and proprietary and may be used --
  4. -- only as expressly authorized by a licensing agreement from --
  5. -- --
  6. -- Hantro Products Oy. --
  7. -- --
  8. -- (C) COPYRIGHT 2006 HANTRO PRODUCTS OY --
  9. -- ALL RIGHTS RESERVED --
  10. -- --
  11. -- The entire notice above must be reproduced --
  12. -- on all copies and should not be removed. --
  13. -- --
  14. --------------------------------------------------------------------------------
  15. --
  16. -- Description : Encoder Wrapper Layer (user space module)
  17. --
  18. ------------------------------------------------------------------------------*/
  19. /*------------------------------------------------------------------------------
  20. 1. Include headers
  21. ------------------------------------------------------------------------------*/
  22. #include "basetype.h"
  23. #include "ewl.h"
  24. #include "hx280enc.h" /* This EWL uses the kernel module */
  25. #include "ewl_x280_common.h"
  26. #include "ewl_linux_lock.h"
  27. #include <signal.h>
  28. #include <unistd.h>
  29. #include <fcntl.h>
  30. #include <errno.h>
  31. #include <sys/types.h>
  32. #include <unistd.h>
  33. #include <pthread.h>
  34. #include <semaphore.h>
  35. #include <assert.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. /*------------------------------------------------------------------------------
  40. 2. External compiler flags
  41. --------------------------------------------------------------------------------
  42. ENC_MODULE_PATH defines the path for encoder device driver nod.
  43. e.g. "/tmp/dev/hx280"
  44. MEMALLOC_MODULE_PATH defines the path for memalloc device driver nod.
  45. e.g. "/tmp/dev/memalloc"
  46. ENC_IO_BASE defines the IO base address for encoder HW registers
  47. e.g. 0xC0000000
  48. SDRAM_LM_BASE defines the base address for SDRAM as seen by HW
  49. e.g. 0x80000000
  50. --------------------------------------------------------------------------------
  51. 3. Module defines
  52. ------------------------------------------------------------------------------*/
  53. extern volatile u32 asic_status;
  54. #ifndef ENC_DEMO
  55. static volatile sig_atomic_t sig_delivered = 0;
  56. /* SIGIO handler */
  57. static void sig_handler(int signal_number)
  58. {
  59. sig_delivered++;
  60. }
  61. #endif
  62. void HandleSIGIO(hx280ewl_t * enc)
  63. {
  64. #ifndef ENC_DEMO
  65. struct sigaction sa;
  66. /* asynchronus notification handler */
  67. memset(&sa, 0, sizeof(sa));
  68. sa.sa_handler = sig_handler;
  69. sa.sa_flags |= SA_RESTART; /* restart of system calls */
  70. sigaction(SIGIO, &sa, NULL);
  71. #endif
  72. /* EWLInit might be called in a separate thread */
  73. /* we want to register the encoding thread for SIGIO */
  74. enc->sigio_needed = 1; /* register for SIGIO in EWLEnableHW */
  75. }
  76. /*******************************************************************************
  77. Function name : EWLReserveHw
  78. Description : Reserve HW resource for currently running codec
  79. *******************************************************************************/
  80. i32 EWLReserveHw(const void *inst)
  81. {
  82. hx280ewl_t *enc = (hx280ewl_t *) inst;
  83. assert(enc != NULL);
  84. PTRACE("EWLReserveHw: PID %d trying to reserve ...\n", getpid());
  85. /* Check invalid parameters */
  86. if(inst == NULL)
  87. return EWL_ERROR;
  88. if(binary_semaphore_wait(enc->semid, 0) != 0)
  89. {
  90. PTRACE("binary_semaphore_wait error: %s\n", strerror(errno));
  91. return EWL_ERROR;
  92. }
  93. PTRACE("Codec semaphore locked\n");
  94. /* we have to lock postprocessor also */
  95. if(binary_semaphore_wait(enc->semid, 1) != 0)
  96. {
  97. PTRACE("binary_semaphore_wait error: %s\n", strerror(errno));
  98. if(binary_semaphore_post(enc->semid, 0) != 0)
  99. {
  100. PTRACE("binary_semaphore_post error: %s\n", strerror(errno));
  101. assert(0);
  102. }
  103. return EWL_ERROR;
  104. }
  105. PTRACE("Post-processor semaphore locked\n");
  106. EWLWriteReg(inst, 0x38, 0);
  107. #ifndef ENC_DEMO
  108. {
  109. sigset_t set;
  110. sigemptyset(&set);
  111. sigaddset(&set, SIGIO);
  112. pthread_sigmask(SIG_BLOCK, &set, NULL);
  113. sig_delivered = 0;
  114. pthread_sigmask(SIG_UNBLOCK, &set, NULL);
  115. }
  116. #endif
  117. PTRACE("EWLReserveHw: ENC HW locked by PID %d\n", getpid());
  118. return EWL_OK;
  119. }
  120. /*******************************************************************************
  121. Function name : EWLWaitHwRdy
  122. Description : Wait for the encoder semaphore
  123. Return type : i32
  124. Argument : void
  125. *******************************************************************************/
  126. i32 EWLWaitHwRdy(const void *inst, u32 *slicesReady)
  127. {
  128. hx280ewl_t *enc = (hx280ewl_t *) inst;
  129. u32 prevSlicesReady = 0;
  130. PTRACE("EWLWaitHw: Start\n");
  131. /* Check invalid parameters */
  132. if(enc == NULL)
  133. {
  134. assert(0);
  135. return EWL_HW_WAIT_ERROR;
  136. }
  137. #ifdef EWL_NO_HW_TIMEOUT
  138. {
  139. sigset_t set, oldset;
  140. sigemptyset(&set);
  141. sigaddset(&set, SIGIO);
  142. #ifndef ENC_DEMO
  143. if (slicesReady)
  144. {
  145. /* The function should return when a slice is ready */
  146. prevSlicesReady = *slicesReady;
  147. /* Get the number of completed slices from ASIC registers. */
  148. *slicesReady = (enc->pRegBase[21] >> 16) & 0xFF;
  149. /* The reference implementation uses polling for testing purpose.
  150. * More efficient implementation should signal the slice ready IRQ. */
  151. while (!sig_delivered && *slicesReady <= prevSlicesReady)
  152. {
  153. /* Get the number of completed slices from ASIC regs */
  154. *slicesReady = (enc->pRegBase[21] >> 16) & 0xFF;
  155. }
  156. sig_delivered = 0;
  157. }
  158. else
  159. {
  160. /* Wait for frame ready signal (SIGIO) */
  161. pthread_sigmask(SIG_BLOCK, &set, &oldset);
  162. while(!sig_delivered)
  163. {
  164. printf("before sigsuspend.\n");
  165. sigsuspend(&oldset);
  166. printf("after sigsuspend.\n");
  167. }
  168. sig_delivered = 0;
  169. pthread_sigmask(SIG_UNBLOCK, &set, NULL);
  170. }
  171. #else
  172. {
  173. int signo;
  174. sigwait(&set, &signo);
  175. }
  176. #endif
  177. }
  178. #else
  179. #error Timeout not implemented!
  180. #endif
  181. asic_status = enc->pRegBase[1]; /* update the buffered asic status */
  182. if (slicesReady)
  183. PTRACE("EWLWaitHw: slicesReady = %d\n", *slicesReady);
  184. PTRACE("EWLWaitHw: asic_status = %x\n", asic_status);
  185. PTRACE("EWLWaitHw: OK!\n");
  186. return EWL_OK;
  187. }