ewl_x280_common.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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. -- Abstract : Encoder Wrapper Layer for 6280/7280/8270/8290, common parts
  17. --
  18. ------------------------------------------------------------------------------*/
  19. /*------------------------------------------------------------------------------
  20. 1. Include headers
  21. ------------------------------------------------------------------------------*/
  22. #include "basetype.h"
  23. #include "ewl.h"
  24. #include "ewl_linux_lock.h"
  25. #include "ewl_x280_common.h"
  26. #include "hx280enc.h"
  27. #include "memalloc.h"
  28. #include <sys/syscall.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <sys/mman.h>
  32. #include <sys/ioctl.h>
  33. #include <fcntl.h>
  34. #include <unistd.h>
  35. #include <errno.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <assert.h>
  40. #ifdef USE_EFENCE
  41. #include "efence.h"
  42. #endif
  43. #ifndef MEMALLOC_MODULE_PATH
  44. #define MEMALLOC_MODULE_PATH "/tmp/dev/memalloc"
  45. #endif
  46. #ifndef ENC_MODULE_PATH
  47. #define ENC_MODULE_PATH "/tmp/dev/hx280"
  48. #endif
  49. #ifndef SDRAM_LM_BASE
  50. #define SDRAM_LM_BASE 0x00000000
  51. #endif
  52. /* macro to convert CPU bus address to ASIC bus address */
  53. #ifdef PC_PCI_FPGA_DEMO
  54. #define BUS_CPU_TO_ASIC(address) (((address) & (~0xff000000)) | SDRAM_LM_BASE)
  55. #else
  56. #define BUS_CPU_TO_ASIC(address) ((address) | SDRAM_LM_BASE)
  57. #endif
  58. static const char *busTypeName[7] = { "UNKNOWN", "AHB", "OCP", "AXI", "PCI", "AXIAHB", "AXIAPB" };
  59. static const char *synthLangName[3] = { "UNKNOWN", "VHDL", "VERILOG" };
  60. volatile u32 asic_status = 0;
  61. FILE *fEwl = NULL;
  62. int MapAsicRegisters(hx280ewl_t * ewl)
  63. {
  64. unsigned long base, size;
  65. u32 *pRegs, *pSysRegs;
  66. ioctl(ewl->fd_enc, HX280ENC_IOCGHWOFFSET, &base);
  67. ioctl(ewl->fd_enc, HX280ENC_IOCGHWIOSIZE, &size);
  68. /* map hw registers to user space */
  69. pRegs =
  70. (u32 *) mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED,
  71. ewl->fd_mem, base);
  72. if(pRegs == MAP_FAILED)
  73. {
  74. PTRACE("EWLInit: Failed to mmap regs\n");
  75. return -1;
  76. }
  77. /* map sys registers to user space */
  78. pSysRegs =
  79. (u32 *) mmap(0, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED,
  80. ewl->fd_mem, 0x40408000);
  81. if(pSysRegs == MAP_FAILED)
  82. {
  83. PTRACE("EWLInit: Failed to mmap sys regs\n");
  84. return -1;
  85. }
  86. ewl->regSize = size;
  87. ewl->regBase = base;
  88. ewl->pRegBase = pRegs;
  89. ewl->pSysReg = pSysRegs;
  90. return 0;
  91. }
  92. /*******************************************************************************
  93. Function name : EWLReadAsicID
  94. Description : Read ASIC ID register, static implementation
  95. Return type : u32 ID
  96. Argument : void
  97. *******************************************************************************/
  98. u32 EWLReadAsicID()
  99. {
  100. u32 id = ~0;
  101. int fd_mem = -1, fd_enc = -1;
  102. unsigned long base = ~0, size;
  103. u32 *pRegs = MAP_FAILED;
  104. EWLHwConfig_t cfg_info;
  105. memset(&cfg_info, 0, sizeof(cfg_info));
  106. if(fEwl == NULL)
  107. fEwl = fopen("ewl.trc", "w");
  108. fd_mem = open("/dev/mem", O_RDONLY);
  109. if(fd_mem == -1)
  110. {
  111. PTRACE("EWLReadAsicID: failed to open: %s\n", "/dev/mem");
  112. goto end;
  113. }
  114. fd_enc = open(ENC_MODULE_PATH, O_RDONLY);
  115. if(fd_enc == -1)
  116. {
  117. PTRACE("EWLReadAsicID: failed to open: %s\n", ENC_MODULE_PATH);
  118. goto end;
  119. }
  120. ioctl(fd_enc, HX280ENC_IOCGHWOFFSET, &base);
  121. ioctl(fd_enc, HX280ENC_IOCGHWIOSIZE, &size);
  122. /* map hw registers to user space */
  123. pRegs = (u32 *) mmap(0, size, PROT_READ, MAP_SHARED, fd_mem, base);
  124. if(pRegs == MAP_FAILED)
  125. {
  126. PTRACE("EWLReadAsicID: Failed to mmap regs\n");
  127. goto end;
  128. }
  129. id = pRegs[0];
  130. end:
  131. if(pRegs != MAP_FAILED)
  132. munmap(pRegs, size);
  133. if(fd_mem != -1)
  134. close(fd_mem);
  135. if(fd_enc != -1)
  136. close(fd_enc);
  137. PTRACE("EWLReadAsicID: 0x%08x at 0x%08lx\n", id, base);
  138. return id;
  139. }
  140. /*******************************************************************************
  141. Function name : EWLReadAsicConfig
  142. Description : Reads ASIC capability register, static implementation
  143. Return type : EWLHwConfig_t
  144. Argument : void
  145. *******************************************************************************/
  146. EWLHwConfig_t EWLReadAsicConfig(void)
  147. {
  148. int fd_mem = -1, fd_enc = -1;
  149. unsigned long base, size;
  150. u32 *pRegs = MAP_FAILED, cfgval;
  151. EWLHwConfig_t cfg_info;
  152. memset(&cfg_info, 0, sizeof(cfg_info));
  153. fd_mem = open("/dev/mem", O_RDONLY);
  154. if(fd_mem == -1)
  155. {
  156. PTRACE("EWLReadAsicConfig: failed to open: %s\n", "/dev/mem");
  157. goto end;
  158. }
  159. fd_enc = open(ENC_MODULE_PATH, O_RDONLY);
  160. if(fd_enc == -1)
  161. {
  162. PTRACE("EWLReadAsicConfig: failed to open: %s\n", ENC_MODULE_PATH);
  163. goto end;
  164. }
  165. ioctl(fd_enc, HX280ENC_IOCGHWOFFSET, &base);
  166. ioctl(fd_enc, HX280ENC_IOCGHWIOSIZE, &size);
  167. /* map hw registers to user space */
  168. pRegs = (u32 *) mmap(0, size, PROT_READ, MAP_SHARED, fd_mem, base);
  169. if(pRegs == MAP_FAILED)
  170. {
  171. PTRACE("EWLReadAsicConfig: Failed to mmap regs\n");
  172. goto end;
  173. }
  174. cfgval = pRegs[63];
  175. cfg_info.maxEncodedWidth = cfgval & ((1 << 12) - 1);
  176. cfg_info.h264Enabled = (cfgval >> 27) & 1;
  177. cfg_info.mpeg4Enabled = (cfgval >> 26) & 1;
  178. cfg_info.jpegEnabled = (cfgval >> 25) & 1;
  179. cfg_info.vsEnabled = (cfgval >> 24) & 1;
  180. cfg_info.rgbEnabled = (cfgval >> 28) & 1;
  181. cfg_info.searchAreaSmall = (cfgval >> 29) & 1;
  182. cfg_info.inputYuvTiledEnabled = (cfgval >> 30) & 1;
  183. cfg_info.busType = (cfgval >> 20) & 15;
  184. cfg_info.synthesisLanguage = (cfgval >> 16) & 15;
  185. cfg_info.busWidth = (cfgval >> 12) & 15;
  186. PTRACE("EWLReadAsicConfig:\n"
  187. " maxEncodedWidth = %d\n"
  188. " h264Enabled = %s\n"
  189. " jpegEnabled = %s\n"
  190. " mpeg4Enabled = %s\n"
  191. " vsEnabled = %s\n"
  192. " rgbEnabled = %s\n"
  193. " searchAreaSmall = %s\n"
  194. " inputYuvTiled = %s\n"
  195. " busType = %s\n"
  196. " synthesisLanguage = %s\n"
  197. " busWidth = %d\n",
  198. cfg_info.maxEncodedWidth,
  199. cfg_info.h264Enabled == 1 ? "YES" : "NO",
  200. cfg_info.jpegEnabled == 1 ? "YES" : "NO",
  201. cfg_info.mpeg4Enabled == 1 ? "YES" : "NO",
  202. cfg_info.vsEnabled == 1 ? "YES" : "NO",
  203. cfg_info.rgbEnabled == 1 ? "YES" : "NO",
  204. cfg_info.searchAreaSmall == 1 ? "YES" : "NO",
  205. cfg_info.inputYuvTiledEnabled == 1 ? "YES" : "NO",
  206. cfg_info.busType < 7 ? busTypeName[cfg_info.busType] : "UNKNOWN",
  207. cfg_info.synthesisLanguage <
  208. 3 ? synthLangName[cfg_info.synthesisLanguage] : "ERROR",
  209. cfg_info.busWidth * 32);
  210. end:
  211. if(pRegs != MAP_FAILED)
  212. munmap(pRegs, size);
  213. if(fd_mem != -1)
  214. close(fd_mem);
  215. if(fd_enc != -1)
  216. close(fd_enc);
  217. return cfg_info;
  218. }
  219. /*******************************************************************************
  220. Function name : EWLInit
  221. Description : Allocate resources and setup the wrapper module
  222. Return type : ewl_ret
  223. Argument : void
  224. *******************************************************************************/
  225. const void *EWLInit(EWLInitParam_t * param)
  226. {
  227. hx280ewl_t *enc = NULL;
  228. PTRACE("EWLInit: Start\n");
  229. /* Check for NULL pointer */
  230. if(param == NULL || param->clientType > 4)
  231. {
  232. PTRACE(("EWLInit: Bad calling parameters!\n"));
  233. return NULL;
  234. }
  235. /* Allocate instance */
  236. if((enc = (hx280ewl_t *) EWLmalloc(sizeof(hx280ewl_t))) == NULL)
  237. {
  238. PTRACE("EWLInit: failed to alloc hx280ewl_t struct\n");
  239. return NULL;
  240. }
  241. enc->clientType = param->clientType;
  242. enc->fd_mem = enc->fd_enc = enc->fd_memalloc = -1;
  243. /* New instance allocated */
  244. enc->fd_mem = open("/dev/mem", O_RDWR | O_SYNC);
  245. if(enc->fd_mem == -1)
  246. {
  247. PTRACE("EWLInit: failed to open: %s\n", "/dev/mem");
  248. goto err;
  249. }
  250. enc->fd_enc = open(ENC_MODULE_PATH, O_RDWR);
  251. if(enc->fd_enc == -1)
  252. {
  253. PTRACE("EWLInit: failed to open: %s\n", ENC_MODULE_PATH);
  254. goto err;
  255. }
  256. enc->fd_memalloc = open(MEMALLOC_MODULE_PATH, O_RDWR);
  257. if(enc->fd_memalloc == -1)
  258. {
  259. PTRACE("EWLInit: failed to open: %s\n", MEMALLOC_MODULE_PATH);
  260. goto err;
  261. }
  262. /* map hw registers to user space */
  263. if(MapAsicRegisters(enc) != 0)
  264. {
  265. goto err;
  266. }
  267. PTRACE("EWLInit: mmap regs %d bytes --> %p\n", enc->regSize, enc->pRegBase);
  268. {
  269. /* common semaphore with the decoder */
  270. key_t key = 0x8070;
  271. int semid;
  272. if((semid = binary_semaphore_allocation(key, 2, 0666)) != -1)
  273. {
  274. PTRACE("EWLInit: HW lock sem aquired (key=%x, id=%d)\n", key,
  275. semid);
  276. }
  277. else if(errno == ENOENT)
  278. {
  279. semid = binary_semaphore_allocation(key, 2, IPC_CREAT | 0666);
  280. if(binary_semaphore_initialize(semid, 0) != 0)
  281. {
  282. PTRACE("binary_semaphore_initialize error: %s\n",
  283. strerror(errno));
  284. }
  285. /* init PP semaphore also */
  286. if(binary_semaphore_initialize(semid, 1) != 0)
  287. {
  288. PTRACE("binary_semaphore_initialize error: %s\n",
  289. strerror(errno));
  290. }
  291. PTRACE("EWLInit: HW lock sem created (key=%x, id=%d)\n", key,
  292. semid);
  293. }
  294. else
  295. {
  296. PTRACE("binary_semaphore_allocation error: %s\n", strerror(errno));
  297. goto err;
  298. }
  299. enc->semid = semid;
  300. }
  301. /* asynchronus notification handler (when needed) */
  302. HandleSIGIO(enc);
  303. PTRACE("EWLInit: Return %0xd\n", (u32) enc);
  304. return enc;
  305. err:
  306. EWLRelease(enc);
  307. PTRACE("EWLInit: Return NULL\n");
  308. return NULL;
  309. }
  310. /*******************************************************************************
  311. Function name : EWLRelease
  312. Description : Release the wrapper module by freeing all the resources
  313. Return type : ewl_ret
  314. Argument : void
  315. *******************************************************************************/
  316. i32 EWLRelease(const void *inst)
  317. {
  318. hx280ewl_t *enc = (hx280ewl_t *) inst;
  319. assert(enc != NULL);
  320. if(enc == NULL)
  321. return EWL_OK;
  322. /* Release the instance */
  323. if(enc->pRegBase != MAP_FAILED)
  324. munmap((void *) enc->pRegBase, enc->regSize);
  325. if(enc->pSysReg != MAP_FAILED)
  326. munmap((void *) enc->pSysReg, 0x1000);
  327. if(enc->fd_mem != -1)
  328. close(enc->fd_mem);
  329. if(enc->fd_enc != -1)
  330. close(enc->fd_enc);
  331. if(enc->fd_memalloc != -1)
  332. close(enc->fd_memalloc);
  333. EWLfree(enc);
  334. PTRACE("EWLRelease: instance freed\n");
  335. if(fEwl != NULL) {
  336. fclose(fEwl);
  337. fEwl = NULL;
  338. }
  339. return EWL_OK;
  340. }
  341. /*******************************************************************************
  342. Function name : EWLWriteReg
  343. Description : Set the content of a hadware register
  344. Return type : void
  345. Argument : u32 offset
  346. Argument : u32 val
  347. *******************************************************************************/
  348. void EWLWriteReg(const void *inst, u32 offset, u32 val)
  349. {
  350. hx280ewl_t *enc = (hx280ewl_t *) inst;
  351. assert(enc != NULL && offset < enc->regSize);
  352. if(offset == 0x04)
  353. {
  354. asic_status = val;
  355. }
  356. offset = offset / 4;
  357. *(enc->pRegBase + offset) = val;
  358. PTRACE("EWLWriteReg 0x%02x with value %08x\n", offset * 4, val);
  359. }
  360. /*------------------------------------------------------------------------------
  361. Function name : EWLEnableHW
  362. Description :
  363. Return type : void
  364. Argument : const void *inst
  365. Argument : u32 offset
  366. Argument : u32 val
  367. ------------------------------------------------------------------------------*/
  368. void EWLEnableHW(const void *inst, u32 offset, u32 val)
  369. {
  370. hx280ewl_t *enc = (hx280ewl_t *) inst;
  371. assert(enc != NULL && offset < enc->regSize);
  372. if(enc->sigio_needed)
  373. {
  374. int oflags;
  375. /* asynchronus notification enable */
  376. fcntl(enc->fd_enc, F_SETOWN, syscall(SYS_gettid)); /* this thread will receive SIGIO */
  377. oflags = fcntl(enc->fd_enc, F_GETFL);
  378. fcntl(enc->fd_enc, F_SETFL, oflags | FASYNC); /* set ASYNC notification flag */
  379. enc->sigio_needed = 0; /* do it only once */
  380. }
  381. if(offset == 0x04)
  382. {
  383. asic_status = val;
  384. }
  385. offset = offset / 4;
  386. *(enc->pRegBase + offset) = val;
  387. PTRACE("EWLEnableHW 0x%02x with value %08x\n", offset * 4, val);
  388. }
  389. /*------------------------------------------------------------------------------
  390. Function name : EWLDisableHW
  391. Description :
  392. Return type : void
  393. Argument : const void *inst
  394. Argument : u32 offset
  395. Argument : u32 val
  396. ------------------------------------------------------------------------------*/
  397. void EWLDisableHW(const void *inst, u32 offset, u32 val)
  398. {
  399. hx280ewl_t *enc = (hx280ewl_t *) inst;
  400. assert(enc != NULL && offset < enc->regSize);
  401. offset = offset / 4;
  402. *(enc->pRegBase + offset) = val;
  403. asic_status = val;
  404. PTRACE("EWLDisableHW 0x%02x with value %08x\n", offset * 4, val);
  405. }
  406. /*******************************************************************************
  407. Function name : EWLReadReg
  408. Description : Retrive the content of a hadware register
  409. Note: The status register will be read after every MB
  410. so it may be needed to buffer it's content if reading
  411. the HW register is slow.
  412. Return type : u32
  413. Argument : u32 offset
  414. *******************************************************************************/
  415. u32 EWLReadReg(const void *inst, u32 offset)
  416. {
  417. u32 val;
  418. hx280ewl_t *enc = (hx280ewl_t *) inst;
  419. assert(offset < enc->regSize);
  420. if(offset == 0x04)
  421. {
  422. return asic_status;
  423. }
  424. offset = offset / 4;
  425. val = *(enc->pRegBase + offset);
  426. PTRACE("EWLReadReg 0x%02x --> %08x\n", offset * 4, val);
  427. return val;
  428. }
  429. /*------------------------------------------------------------------------------
  430. Function name : EWLMallocRefFrm
  431. Description : Allocate a frame buffer (contiguous linear RAM memory)
  432. Return type : i32 - 0 for success or a negative error code
  433. Argument : const void * instance - EWL instance
  434. Argument : u32 size - size in bytes of the requested memory
  435. Argument : EWLLinearMem_t *info - place where the allocated memory
  436. buffer parameters are returned
  437. ------------------------------------------------------------------------------*/
  438. i32 EWLMallocRefFrm(const void *instance, u32 size, EWLLinearMem_t * info)
  439. {
  440. hx280ewl_t *enc_ewl = (hx280ewl_t *) instance;
  441. EWLLinearMem_t *buff = (EWLLinearMem_t *) info;
  442. i32 ret;
  443. assert(enc_ewl != NULL);
  444. assert(buff != NULL);
  445. PTRACE("EWLMallocRefFrm\t%8d bytes\n", size);
  446. ret = EWLMallocLinear(enc_ewl, size, buff);
  447. PTRACE("EWLMallocRefFrm %08x --> %p\n", buff->busAddress,
  448. buff->virtualAddress);
  449. return ret;
  450. }
  451. /*------------------------------------------------------------------------------
  452. Function name : EWLFreeRefFrm
  453. Description : Release a frame buffer previously allocated with
  454. EWLMallocRefFrm.
  455. Return type : void
  456. Argument : const void * instance - EWL instance
  457. Argument : EWLLinearMem_t *info - frame buffer memory information
  458. ------------------------------------------------------------------------------*/
  459. void EWLFreeRefFrm(const void *instance, EWLLinearMem_t * info)
  460. {
  461. hx280ewl_t *enc_ewl = (hx280ewl_t *) instance;
  462. EWLLinearMem_t *buff = (EWLLinearMem_t *) info;
  463. assert(enc_ewl != NULL);
  464. assert(buff != NULL);
  465. EWLFreeLinear(enc_ewl, buff);
  466. PTRACE("EWLFreeRefFrm\t%p\n", buff->virtualAddress);
  467. }
  468. /*------------------------------------------------------------------------------
  469. Function name : EWLMallocLinear
  470. Description : Allocate a contiguous, linear RAM memory buffer
  471. Return type : i32 - 0 for success or a negative error code
  472. Argument : const void * instance - EWL instance
  473. Argument : u32 size - size in bytes of the requested memory
  474. Argument : EWLLinearMem_t *info - place where the allocated memory
  475. buffer parameters are returned
  476. ------------------------------------------------------------------------------*/
  477. i32 EWLMallocLinear(const void *instance, u32 size, EWLLinearMem_t * info)
  478. {
  479. hx280ewl_t *enc_ewl = (hx280ewl_t *) instance;
  480. EWLLinearMem_t *buff = (EWLLinearMem_t *) info;
  481. MemallocParams params;
  482. u32 pgsize = getpagesize();
  483. assert(enc_ewl != NULL);
  484. assert(buff != NULL);
  485. PTRACE("EWLMallocLinear\t%8d bytes\n", size);
  486. buff->size = (size + (pgsize - 1)) & (~(pgsize - 1));
  487. params.size = size;
  488. buff->virtualAddress = 0;
  489. /* get memory linear memory buffers */
  490. ioctl(enc_ewl->fd_memalloc, MEMALLOC_IOCXGETBUFFER, &params);
  491. if(params.busAddress == 0)
  492. {
  493. PTRACE("EWLMallocLinear: Linear buffer not allocated\n");
  494. return EWL_ERROR;
  495. }
  496. /* Map the bus address to virtual address */
  497. buff->virtualAddress = MAP_FAILED;
  498. buff->virtualAddress = (u32 *) mmap(0, buff->size, PROT_READ | PROT_WRITE,
  499. MAP_SHARED, enc_ewl->fd_mem,
  500. params.busAddress);
  501. if(buff->virtualAddress == MAP_FAILED)
  502. {
  503. PTRACE("EWLInit: Failed to mmap busAddress: 0x%08x\n",
  504. params.busAddress);
  505. return EWL_ERROR;
  506. }
  507. /* ASIC might be in different address space */
  508. buff->busAddress = BUS_CPU_TO_ASIC(params.busAddress);
  509. PTRACE("EWLMallocLinear 0x%08x (CPU) 0x%08x (ASIC) --> %p\n",
  510. params.busAddress, buff->busAddress, buff->virtualAddress);
  511. return EWL_OK;
  512. }
  513. /*------------------------------------------------------------------------------
  514. Function name : EWLFreeLinear
  515. Description : Release a linera memory buffer, previously allocated with
  516. EWLMallocLinear.
  517. Return type : void
  518. Argument : const void * instance - EWL instance
  519. Argument : EWLLinearMem_t *info - linear buffer memory information
  520. ------------------------------------------------------------------------------*/
  521. void EWLFreeLinear(const void *instance, EWLLinearMem_t * info)
  522. {
  523. hx280ewl_t *enc_ewl = (hx280ewl_t *) instance;
  524. EWLLinearMem_t *buff = (EWLLinearMem_t *) info;
  525. assert(enc_ewl != NULL);
  526. assert(buff != NULL);
  527. if(buff->busAddress != 0)
  528. ioctl(enc_ewl->fd_memalloc, MEMALLOC_IOCSFREEBUFFER, &buff->busAddress);
  529. if(buff->virtualAddress != MAP_FAILED)
  530. munmap(buff->virtualAddress, buff->size);
  531. PTRACE("EWLFreeLinear\t%p\n", buff->virtualAddress);
  532. }
  533. /*******************************************************************************
  534. Function name : EWLReleaseHw
  535. Description : Release HW resource when frame is ready
  536. *******************************************************************************/
  537. void EWLReleaseHw(const void *inst)
  538. {
  539. u32 val;
  540. hx280ewl_t *enc = (hx280ewl_t *) inst;
  541. assert(enc != NULL);
  542. val = EWLReadReg(inst, 0x38);
  543. EWLWriteReg(inst, 0x38, val & (~0x01)); /* reset ASIC */
  544. PTRACE("EWLReleaseHw: PID %d trying to release ...\n", getpid());
  545. if(binary_semaphore_post(enc->semid, 0) != 0)
  546. {
  547. PTRACE("binary_semaphore_post error: %s\n", strerror(errno));
  548. assert(0);
  549. }
  550. /* we have to release postprocessor also */
  551. if(binary_semaphore_post(enc->semid, 1) != 0)
  552. {
  553. PTRACE("binary_semaphore_post error: %s\n", strerror(errno));
  554. assert(0);
  555. }
  556. PTRACE("EWLReleaseHw: HW released by PID %d\n", getpid());
  557. }
  558. /* SW/SW shared memory */
  559. /*------------------------------------------------------------------------------
  560. Function name : EWLmalloc
  561. Description : Allocate a memory block. Same functionality as
  562. the ANSI C malloc()
  563. Return type : void pointer to the allocated space, or NULL if there
  564. is insufficient memory available
  565. Argument : u32 n - Bytes to allocate
  566. ------------------------------------------------------------------------------*/
  567. void *EWLmalloc(u32 n)
  568. {
  569. void *p = malloc((size_t) n);
  570. PTRACE("EWLmalloc\t%8d bytes --> %p\n", n, p);
  571. return p;
  572. }
  573. /*------------------------------------------------------------------------------
  574. Function name : EWLfree
  575. Description : Deallocates or frees a memory block. Same functionality as
  576. the ANSI C free()
  577. Return type : void
  578. Argument : void *p - Previously allocated memory block to be freed
  579. ------------------------------------------------------------------------------*/
  580. void EWLfree(void *p)
  581. {
  582. PTRACE("EWLfree\t%p\n", p);
  583. if(p != NULL)
  584. free(p);
  585. }
  586. /*------------------------------------------------------------------------------
  587. Function name : EWLcalloc
  588. Description : Allocates an array in memory with elements initialized
  589. to 0. Same functionality as the ANSI C calloc()
  590. Return type : void pointer to the allocated space, or NULL if there
  591. is insufficient memory available
  592. Argument : u32 n - Number of elements
  593. Argument : u32 s - Length in bytes of each element.
  594. ------------------------------------------------------------------------------*/
  595. void *EWLcalloc(u32 n, u32 s)
  596. {
  597. void *p = calloc((size_t) n, (size_t) s);
  598. PTRACE("EWLcalloc\t%8d bytes --> %p\n", n * s, p);
  599. return p;
  600. }
  601. /*------------------------------------------------------------------------------
  602. Function name : EWLmemcpy
  603. Description : Copies characters between buffers. Same functionality as
  604. the ANSI C memcpy()
  605. Return type : The value of destination d
  606. Argument : void *d - Destination buffer
  607. Argument : const void *s - Buffer to copy from
  608. Argument : u32 n - Number of bytes to copy
  609. ------------------------------------------------------------------------------*/
  610. void *EWLmemcpy(void *d, const void *s, u32 n)
  611. {
  612. return memcpy(d, s, (size_t) n);
  613. }
  614. /*------------------------------------------------------------------------------
  615. Function name : EWLmemset
  616. Description : Sets buffers to a specified character. Same functionality
  617. as the ANSI C memset()
  618. Return type : The value of destination d
  619. Argument : void *d - Pointer to destination
  620. Argument : i32 c - Character to set
  621. Argument : u32 n - Number of characters
  622. ------------------------------------------------------------------------------*/
  623. void *EWLmemset(void *d, i32 c, u32 n)
  624. {
  625. return memset(d, (int) c, (size_t) n);
  626. }