xrp_linux.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. * Copyright (c) 2016 - 2018 Cadence Design Systems Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #define _GNU_SOURCE
  24. #include <fcntl.h>
  25. #include <stdint.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <sys/ioctl.h>
  30. #include <sys/stat.h>
  31. #include <sys/types.h>
  32. #include <signal.h>
  33. #include <unistd.h>
  34. #include "xrp_types.h"
  35. #include "xrp_host_common.h"
  36. #include "xrp_host_impl.h"
  37. #include "xrp_kernel_defs.h"
  38. #include "xrp_report.h"
  39. #include "dsp_common.h"
  40. // #include "csi_dsp_task_defs.h"
  41. struct xrp_request {
  42. struct xrp_queue_item q;
  43. void *in_data;
  44. void *out_data;
  45. size_t in_data_size;
  46. size_t out_data_size;
  47. struct xrp_buffer_group *buffer_group;
  48. struct xrp_event *event;
  49. };
  50. /* Device API. */
  51. struct xrp_device *xrp_open_device(int idx, enum xrp_status *status)
  52. {
  53. struct xrp_device *device;
  54. char name[sizeof("/dev/xvp") + sizeof(int) * 4];
  55. int fd;
  56. sprintf(name, "/dev/xvp%u", idx);
  57. fd = open(name, O_RDWR);
  58. if (fd == -1) {
  59. set_status(status, XRP_STATUS_FAILURE);
  60. return NULL;
  61. }
  62. device = alloc_refcounted(sizeof(*device));
  63. if (!device) {
  64. set_status(status, XRP_STATUS_FAILURE);
  65. return NULL;
  66. }
  67. device->impl.fd = fd;
  68. set_status(status, XRP_STATUS_SUCCESS);
  69. return device;
  70. }
  71. void xrp_impl_release_device(struct xrp_device *device)
  72. {
  73. close(device->impl.fd);
  74. }
  75. /* Buffer API. */
  76. void xrp_impl_create_device_buffer(struct xrp_device *device,
  77. struct xrp_buffer *buffer,
  78. size_t size,
  79. enum xrp_status *status)
  80. {
  81. struct xrp_ioctl_alloc ioctl_alloc = {
  82. .size = size,
  83. };
  84. int ret;
  85. xrp_retain_device(device);
  86. buffer->device = device;
  87. ret = ioctl(buffer->device->impl.fd, XRP_IOCTL_ALLOC, &ioctl_alloc);
  88. if (ret < 0) {
  89. xrp_release_device(buffer->device);
  90. set_status(status, XRP_STATUS_FAILURE);
  91. return;
  92. }
  93. buffer->ptr = (void *)(uintptr_t)ioctl_alloc.addr;
  94. buffer->size = size;
  95. buffer->phy_addr = ioctl_alloc.paddr;
  96. set_status(status, XRP_STATUS_SUCCESS);
  97. }
  98. void xrp_impl_release_device_buffer(struct xrp_buffer *buffer)
  99. {
  100. struct xrp_ioctl_alloc ioctl_alloc = {
  101. .addr = (uintptr_t)buffer->ptr,
  102. };
  103. ioctl(buffer->device->impl.fd,
  104. XRP_IOCTL_FREE, &ioctl_alloc);
  105. xrp_release_device(buffer->device);
  106. }
  107. /* Queue API. */
  108. static void _xrp_run_command(struct xrp_queue *queue,
  109. const void *in_data, size_t in_data_size,
  110. void *out_data, size_t out_data_size,
  111. struct xrp_buffer_group *buffer_group,
  112. enum xrp_status *status)
  113. {
  114. int ret;
  115. if (buffer_group)
  116. xrp_mutex_lock(&buffer_group->mutex);
  117. {
  118. size_t n_buffers = buffer_group ? buffer_group->n_buffers : 0;
  119. struct xrp_ioctl_buffer ioctl_buffer[n_buffers];/* TODO */
  120. struct xrp_ioctl_queue ioctl_queue = {
  121. .flags = (queue->use_nsid ? XRP_QUEUE_FLAG_NSID : 0) |
  122. ((queue->priority << XRP_QUEUE_FLAG_PRIO_SHIFT) &
  123. XRP_QUEUE_FLAG_PRIO),
  124. .in_data_size = in_data_size,
  125. .out_data_size = out_data_size,
  126. .buffer_size = n_buffers *
  127. sizeof(struct xrp_ioctl_buffer),
  128. .in_data_addr = (uintptr_t)in_data,
  129. .out_data_addr = (uintptr_t)out_data,
  130. .buffer_addr = (uintptr_t)ioctl_buffer,
  131. .nsid_addr = (uintptr_t)queue->nsid,
  132. };
  133. size_t i;
  134. for (i = 0; i < n_buffers; ++i) {
  135. ioctl_buffer[i] = (struct xrp_ioctl_buffer){
  136. .flags = buffer_group->buffer[i].access_flags,
  137. .size = buffer_group->buffer[i].buffer->size,
  138. .addr = (uintptr_t)buffer_group->buffer[i].buffer->ptr,
  139. };
  140. }
  141. if (buffer_group)
  142. xrp_mutex_unlock(&buffer_group->mutex);
  143. ret = ioctl(queue->device->impl.fd,
  144. XRP_IOCTL_QUEUE, &ioctl_queue);
  145. }
  146. // printf("%s, user out data\n",__func__);
  147. if (ret < 0)
  148. set_status(status, XRP_STATUS_FAILURE);
  149. else
  150. set_status(status, XRP_STATUS_SUCCESS);
  151. }
  152. static void xrp_request_process(struct xrp_queue_item *q,
  153. void *context)
  154. {
  155. enum xrp_status status;
  156. struct xrp_request *rq = (struct xrp_request *)q;
  157. _xrp_run_command(context,
  158. rq->in_data, rq->in_data_size,
  159. rq->out_data, rq->out_data_size,
  160. rq->buffer_group,
  161. &status);
  162. if (rq->buffer_group)
  163. xrp_release_buffer_group(rq->buffer_group);
  164. if (rq->event) {
  165. xrp_impl_broadcast_event(rq->event, status);
  166. xrp_release_event(rq->event);
  167. }
  168. // printf("%s,get resp with event %p!\n",__func__,rq->event);
  169. free(rq->in_data);
  170. free(rq);
  171. }
  172. void xrp_impl_create_queue(struct xrp_queue *queue,
  173. enum xrp_status *status)
  174. {
  175. xrp_queue_init(&queue->impl.queue, queue->priority,
  176. queue, xrp_request_process);
  177. set_status(status, XRP_STATUS_SUCCESS);
  178. }
  179. void xrp_impl_release_queue(struct xrp_queue *queue)
  180. {
  181. xrp_queue_destroy(&queue->impl.queue);
  182. }
  183. /* Communication API */
  184. void xrp_enqueue_command(struct xrp_queue *queue,
  185. const void *in_data, size_t in_data_size,
  186. void *out_data, size_t out_data_size,
  187. struct xrp_buffer_group *buffer_group,
  188. struct xrp_event **evt,
  189. enum xrp_status *status)
  190. {
  191. struct xrp_request *rq;
  192. void *in_data_copy;
  193. rq = malloc(sizeof(*rq));
  194. in_data_copy = malloc(in_data_size);
  195. if (!rq || (in_data_size && !in_data_copy)) {
  196. free(in_data_copy);
  197. free(rq);
  198. set_status(status, XRP_STATUS_FAILURE);
  199. return;
  200. }
  201. memcpy(in_data_copy, in_data, in_data_size);
  202. rq->in_data = in_data_copy;
  203. rq->in_data_size = in_data_size;
  204. rq->out_data = out_data;
  205. rq->out_data_size = out_data_size;
  206. if (evt) {
  207. struct xrp_event *event = xrp_event_create();
  208. if (!event) {
  209. free(rq->in_data);
  210. free(rq);
  211. set_status(status, XRP_STATUS_FAILURE);
  212. return;
  213. }
  214. xrp_retain_queue(queue);
  215. event->queue = queue;
  216. *evt = event;
  217. xrp_retain_event(event);
  218. rq->event = event;
  219. } else {
  220. rq->event = NULL;
  221. }
  222. if (buffer_group)
  223. xrp_retain_buffer_group(buffer_group);
  224. rq->buffer_group = buffer_group;
  225. set_status(status, XRP_STATUS_SUCCESS);
  226. xrp_queue_push(&queue->impl.queue, &rq->q);
  227. }
  228. static struct xrp_report *reporter;
  229. static int report_cnt =0;
  230. static void xrp_prcoess_report(struct xrp_report *reporter)
  231. {
  232. struct xrp_report_buffer *report_buffer;
  233. // struct timeval start_time,mid_time,current_time;
  234. if(!reporter || !reporter->report_buf)
  235. {
  236. return;
  237. }
  238. report_buffer = (struct xrp_report_buffer *)reporter->report_buf;
  239. // gettimeofday(&start_time, 0);
  240. while(ioctl(reporter->device->impl.fd, XRP_IOCTL_POP_NEW_REPORT,report_buffer)==0)
  241. {
  242. // csi_dsp_report_item_t * item = (csi_dsp_report_item_t *)report_buffer->data;
  243. // gettimeofday(&mid_time, 0);
  244. xrp_process_report(&reporter->list,report_buffer->data,report_buffer->report_id);
  245. // gettimeofday(&current_time, 0);
  246. report_cnt++;
  247. // printf("report_cnt:%d,report %d,process time:(%ld s,%ld us,delta:%ldus),callback:%ldus\n",report_cnt,report_buffer->report_id,
  248. // start_time.tv_sec,start_time.tv_usec,
  249. // (current_time.tv_sec-start_time.tv_sec)*1000000+(current_time.tv_usec-start_time.tv_usec),
  250. // (current_time.tv_sec-mid_time.tv_sec)*1000000+(current_time.tv_usec-mid_time.tv_usec));
  251. DSP_PRINT(DEBUG,"report_cnt:%d,report %d\n",report_cnt,report_buffer->report_id);
  252. }
  253. }
  254. static void xrp_reporter_sig_handler()
  255. {
  256. if(!reporter)
  257. {
  258. return;
  259. }
  260. reporter->process_sig =1;
  261. }
  262. static void *xrp_report_thread(void *p)
  263. {
  264. sigset_t waitset,oldset;
  265. struct xrp_report * report_handler =(struct xrp_report *) p;
  266. if(report_handler == NULL)
  267. {
  268. DSP_PRINT(WARNING,"report is not created\n");
  269. return NULL;
  270. }
  271. report_cnt =0;
  272. sigemptyset(&waitset);
  273. sigaddset(&waitset, SIG_REPORT);
  274. signal(SIG_REPORT, xrp_reporter_sig_handler); /* sigaction() is better */
  275. struct f_owner_ex owner_ex;
  276. owner_ex.pid = gettid();//syscall(SYS_gettid);
  277. owner_ex.type = F_OWNER_TID;
  278. fcntl(report_handler->device->impl.fd,F_SETOWN_EX, &owner_ex);
  279. int oflags = fcntl(report_handler->device->impl.fd, F_GETFL);
  280. fcntl(report_handler->device->impl.fd, F_SETFL, oflags | FASYNC);
  281. fcntl(report_handler->device->impl.fd, F_SETSIG, SIG_REPORT);
  282. DSP_PRINT(INFO,"report thread runing....\n");
  283. while(1)
  284. {
  285. sigprocmask(SIG_BLOCK, &waitset,&oldset);
  286. if(report_handler->process_sig)
  287. {
  288. xrp_prcoess_report(report_handler);
  289. report_handler->process_sig = 0;
  290. }
  291. sigsuspend(&oldset);
  292. sigprocmask(SIG_UNBLOCK, &waitset,NULL);
  293. }
  294. DSP_PRINT(INFO,"report thread exit\n");
  295. return NULL;
  296. }
  297. int xrp_add_report_item_with_id(struct xrp_report *report,
  298. int (*cb)(void*context,void*data),
  299. int report_id,
  300. void* context,
  301. size_t data_size)
  302. {
  303. if(report_id<0 || !report)
  304. {
  305. return -1;
  306. }
  307. if(data_size>report->buf_size)
  308. {
  309. DSP_PRINT(WARNING,"report instance size %d is exceed limit %d\n",data_size,report->buf_size);
  310. // set_status(status, XRP_STATUS_FAILURE);
  311. return -1;
  312. }
  313. char *report_buf = malloc(data_size);
  314. if(!report_buf)
  315. {
  316. // set_status(status, XRP_STATUS_FAILURE);
  317. return -1;
  318. }
  319. struct xrp_report_item new_item={
  320. .report_id = report_id,
  321. .buf = report_buf,
  322. .size = data_size,
  323. .context = context,
  324. .fn = cb,
  325. };
  326. DSP_PRINT(DEBUG,"add report id:%d\n", report_id);
  327. if(xrp_add_report(&report->list,&new_item))
  328. {
  329. free(report_buf);
  330. return -1;
  331. }
  332. DSP_PRINT(INFO,"the report item: %d is added sucessfully\n",report_id);
  333. return report_id;
  334. }
  335. int xrp_add_report_item(struct xrp_report *report,
  336. int (*cb)(void*context,void*data),
  337. void* context,
  338. size_t data_size)
  339. {
  340. int id;
  341. id =xrp_alloc_report_id(&report->list);
  342. if(id<0)
  343. {
  344. return -1;
  345. }
  346. return xrp_add_report_item_with_id(report,cb,id,context,data_size);
  347. }
  348. void xrp_remove_report_item(struct xrp_report *report,int report_id)
  349. {
  350. int id;
  351. struct xrp_report_item* item=xrp_get_report_entry(&report->list,report_id);
  352. free(item->buf);
  353. xrp_remove_report(&report->list,report_id);
  354. }
  355. void xrp_impl_create_report(struct xrp_device *device,
  356. struct xrp_report *report,
  357. size_t size,
  358. enum xrp_status *status)
  359. {
  360. sigset_t bset,oset;
  361. struct xrp_ioctl_alloc ioctl_alloc = {
  362. .addr = (uintptr_t)NULL,
  363. .size = size,
  364. };
  365. int ret;
  366. xrp_retain_device(device);
  367. report->device = device;
  368. ret = ioctl(report->device->impl.fd, XRP_IOCTL_REPORT_CREATE, &ioctl_alloc);
  369. if (ret < 0) {
  370. // free(report_buf);
  371. set_status(status, XRP_STATUS_FAILURE);
  372. return;
  373. }
  374. report->report_buf = malloc(size);
  375. if(report->report_buf ==NULL)
  376. {
  377. set_status(status, XRP_STATUS_FAILURE);
  378. return;
  379. }
  380. // printf("buf:%lx,report:x\n",ioctl_alloc.addr,report);
  381. report->buf_size = size;
  382. report->list.queue.head=NULL;
  383. report->process_sig =0;
  384. reporter=report;
  385. xrp_thread_create(&report->report_thread, NULL, xrp_report_thread, report);
  386. set_status(status, XRP_STATUS_SUCCESS);
  387. DSP_PRINT(INFO,"buf:%lx,user space report create\n",ioctl_alloc.addr);
  388. }
  389. void xrp_impl_release_report(struct xrp_device *device,
  390. struct xrp_report *report,enum xrp_status *status)
  391. {
  392. struct xrp_ioctl_alloc ioctl_alloc = {
  393. .addr = (uintptr_t)report->report_buf,
  394. .size = report->buf_size,
  395. };
  396. int ret = ioctl(report->device->impl.fd, XRP_IOCTL_REPORT_RELEASE, &ioctl_alloc);
  397. if (ret < 0) {
  398. // free(report_buf);
  399. set_status(status, XRP_STATUS_FAILURE);
  400. return;
  401. }
  402. xrp_thread_cancel(&report->report_thread);
  403. if(!xrp_thread_join(&report->report_thread))
  404. {
  405. DSP_PRINT(INFO,"report_thread release done\n");
  406. }
  407. free(report->report_buf);
  408. report->report_buf=NULL;
  409. xrp_release_device(device);
  410. set_status(status, XRP_STATUS_SUCCESS);
  411. return;
  412. }
  413. void xrp_import_dma_buf(struct xrp_device *device, int fd,enum xrp_access_flags flag,uint64_t *phy_addr,
  414. uint64_t *user_addr,size_t* size,enum xrp_status *status)
  415. {
  416. struct xrp_dma_buf dma_buf;
  417. if(fd < 0 || !(flag&XRP_FLAG_READ_WRITE))
  418. {
  419. set_status(status, XRP_STATUS_FAILURE);
  420. DSP_PRINT(DEBUG,"param check fail\n");
  421. return;
  422. }
  423. dma_buf.fd = fd;
  424. dma_buf.flags = flag;
  425. int ret = ioctl(device->impl.fd, XRP_IOCTL_DMABUF_IMPORT,&dma_buf);
  426. if (ret < 0) {
  427. DSP_PRINT(DEBUG,"_DMABUF_IMPORT fail\n");
  428. set_status(status, XRP_STATUS_FAILURE);
  429. }
  430. else
  431. {
  432. *phy_addr = dma_buf.paddr;
  433. *user_addr = dma_buf.addr;
  434. *size = dma_buf.size;
  435. set_status(status, XRP_STATUS_SUCCESS);
  436. }
  437. return;
  438. }
  439. void xrp_release_dma_buf(struct xrp_device *device, int fd,uint64_t user_addr,size_t size,enum xrp_status *status)
  440. {
  441. struct xrp_dma_buf dma_buf;
  442. if(fd < 0)
  443. {
  444. set_status(status, XRP_STATUS_FAILURE);
  445. return;
  446. }
  447. dma_buf.fd = fd;
  448. dma_buf.addr = user_addr;
  449. dma_buf.size = size;
  450. int ret = ioctl(device->impl.fd, XRP_IOCTL_DMABUF_RELEASE,&dma_buf);
  451. if (ret < 0) {
  452. set_status(status, XRP_STATUS_FAILURE);
  453. }
  454. else
  455. {
  456. set_status(status, XRP_STATUS_SUCCESS);
  457. }
  458. return ;
  459. }
  460. void xrp_flush_dma_buf(struct xrp_device *device, int fd,enum xrp_access_flags flag,enum xrp_status *status)
  461. {
  462. struct xrp_dma_buf dma_buf;
  463. dma_buf.fd = fd;
  464. dma_buf.flags = flag;
  465. if(fd < 0)
  466. {
  467. set_status(status, XRP_STATUS_FAILURE);
  468. return;
  469. }
  470. int ret = ioctl(device->impl.fd, XRP_IOCTL_DMABUF_SYNC,&dma_buf);
  471. if (ret < 0) {
  472. set_status(status, XRP_STATUS_FAILURE);
  473. }
  474. else
  475. {
  476. set_status(status, XRP_STATUS_SUCCESS);
  477. }
  478. }