coi_device.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. Copyright (c) 2014 Intel Corporation. All Rights Reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of Intel Corporation nor the names of its
  12. contributors may be used to endorse or promote products derived
  13. from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "coi_device.h"
  27. #include "coi_version_asm.h"
  28. #define CYCLE_FREQUENCY 1000000000
  29. static uint32_t engine_index;
  30. extern "C"
  31. {
  32. COIRESULT
  33. SYMBOL_VERSION (COIBufferAddRef, 1) (void *ptr)
  34. {
  35. COITRACE ("COIBufferAddRef");
  36. /* Looks like we have nothing to do here. */
  37. return COI_SUCCESS;
  38. }
  39. COIRESULT
  40. SYMBOL_VERSION (COIBufferReleaseRef, 1) (void *ptr)
  41. {
  42. COITRACE ("COIBufferReleaseRef");
  43. /* Looks like we have nothing to do here. */
  44. return COI_SUCCESS;
  45. }
  46. COIRESULT
  47. SYMBOL_VERSION (COIEngineGetIndex, 1) (COI_ISA_TYPE *type,
  48. uint32_t *index)
  49. {
  50. COITRACE ("COIEngineGetIndex");
  51. /* type is not used in liboffload. */
  52. *index = engine_index;
  53. return COI_SUCCESS;
  54. }
  55. COIRESULT
  56. SYMBOL_VERSION (COIPipelineStartExecutingRunFunctions, 1) ()
  57. {
  58. COITRACE ("COIPipelineStartExecutingRunFunctions");
  59. /* Looks like we have nothing to do here. */
  60. return COI_SUCCESS;
  61. }
  62. COIRESULT
  63. SYMBOL_VERSION (COIProcessWaitForShutdown, 1) ()
  64. {
  65. COITRACE ("COIProcessWaitForShutdown");
  66. char *mic_dir = getenv (MIC_DIR_ENV);
  67. char *mic_index = getenv (MIC_INDEX_ENV);
  68. char *pipe_host_path, *pipe_target_path;
  69. int pipe_host, pipe_target;
  70. int cmd_len;
  71. pid_t ppid = getppid ();
  72. cmd_t cmd;
  73. assert (mic_dir != NULL && mic_index != NULL);
  74. /* Get engine index. */
  75. engine_index = atoi (mic_index);
  76. /* Open pipes. */
  77. MALLOC (char *, pipe_host_path,
  78. strlen (PIPE_HOST_PATH) + strlen (mic_dir) + 1);
  79. MALLOC (char *, pipe_target_path,
  80. strlen (PIPE_TARGET_PATH) + strlen (mic_dir) + 1);
  81. sprintf (pipe_host_path, "%s"PIPE_HOST_PATH, mic_dir);
  82. sprintf (pipe_target_path, "%s"PIPE_TARGET_PATH, mic_dir);
  83. pipe_host = open (pipe_host_path, O_CLOEXEC | O_WRONLY);
  84. if (pipe_host < 0)
  85. COIERROR ("Cannot open target-to-host pipe.");
  86. pipe_target = open (pipe_target_path, O_CLOEXEC | O_RDONLY);
  87. if (pipe_target < 0)
  88. COIERROR ("Cannot open host-to-target pipe.");
  89. /* Clean up. */
  90. free (pipe_host_path);
  91. free (pipe_target_path);
  92. /* Handler. */
  93. while (1)
  94. {
  95. /* Read and execute command. */
  96. cmd = CMD_SHUTDOWN;
  97. cmd_len = read (pipe_target, &cmd, sizeof (cmd_t));
  98. if (cmd_len != sizeof (cmd_t) && cmd_len != 0)
  99. COIERROR ("Cannot read from pipe.");
  100. switch (cmd)
  101. {
  102. case CMD_BUFFER_COPY:
  103. {
  104. uint64_t len;
  105. void *dest, *source;
  106. /* Receive data from host. */
  107. READ (pipe_target, &dest, sizeof (void *));
  108. READ (pipe_target, &source, sizeof (void *));
  109. READ (pipe_target, &len, sizeof (uint64_t));
  110. /* Copy. */
  111. memcpy (dest, source, len);
  112. /* Notify host about completion. */
  113. WRITE (pipe_host, &cmd, sizeof (cmd_t));
  114. break;
  115. }
  116. case CMD_BUFFER_MAP:
  117. {
  118. char *name;
  119. int fd;
  120. size_t len;
  121. uint64_t buffer_len;
  122. void *buffer;
  123. /* Receive data from host. */
  124. READ (pipe_target, &len, sizeof (size_t));
  125. MALLOC (char *, name, len);
  126. READ (pipe_target, name, len);
  127. READ (pipe_target, &buffer_len, sizeof (uint64_t));
  128. /* Open shared memory. */
  129. fd = shm_open (name, O_CLOEXEC | O_RDWR, S_IRUSR | S_IWUSR);
  130. if (fd < 0)
  131. COIERROR ("Cannot open shared memory.");
  132. /* Map shared memory. */
  133. buffer = mmap (NULL, buffer_len, PROT_READ | PROT_WRITE,
  134. MAP_SHARED, fd, 0);
  135. if (buffer == NULL)
  136. COIERROR ("Cannot map shared memory.");
  137. /* Send data to host. */
  138. WRITE (pipe_host, &fd, sizeof (int));
  139. WRITE (pipe_host, &buffer, sizeof (void *));
  140. /* Clean up. */
  141. free (name);
  142. break;
  143. }
  144. case CMD_BUFFER_UNMAP:
  145. {
  146. int fd;
  147. uint64_t buffer_len;
  148. void *buffer;
  149. /* Receive data from host. */
  150. READ (pipe_target, &fd, sizeof (int));
  151. READ (pipe_target, &buffer, sizeof (void *));
  152. READ (pipe_target, &buffer_len, sizeof (uint64_t));
  153. /* Unmap buffer. */
  154. if (munmap (buffer, buffer_len) < 0)
  155. COIERROR ("Cannot unmap shared memory.");
  156. /* Close shared memory. */
  157. if (close (fd) < 0)
  158. COIERROR ("Cannot close shared memory file.");
  159. /* Notify host about completion. */
  160. WRITE (pipe_host, &cmd, sizeof (cmd_t));
  161. break;
  162. }
  163. case CMD_GET_FUNCTION_HANDLE:
  164. {
  165. char *name;
  166. size_t len;
  167. void *ptr;
  168. /* Receive data from host. */
  169. READ (pipe_target, &len, sizeof (size_t));
  170. MALLOC (char *, name, len);
  171. READ (pipe_target, name, len);
  172. /* Find function. */
  173. ptr = dlsym (RTLD_DEFAULT, name);
  174. if (ptr == NULL)
  175. COIERROR ("Cannot find symbol %s.", name);
  176. /* Send data to host. */
  177. WRITE (pipe_host, &ptr, sizeof (void *));
  178. /* Clean up. */
  179. free (name);
  180. break;
  181. }
  182. case CMD_OPEN_LIBRARY:
  183. {
  184. char *lib_path;
  185. size_t len;
  186. /* Receive data from host. */
  187. READ (pipe_target, &len, sizeof (size_t));
  188. MALLOC (char *, lib_path, len);
  189. READ (pipe_target, lib_path, len);
  190. /* Open library. */
  191. if (dlopen (lib_path, RTLD_LAZY | RTLD_GLOBAL) == 0)
  192. COIERROR ("Cannot load %s: %s", lib_path, dlerror ());
  193. /* Clean up. */
  194. free (lib_path);
  195. break;
  196. }
  197. case CMD_RUN_FUNCTION:
  198. {
  199. uint16_t misc_data_len, return_data_len;
  200. uint32_t buffer_count, i;
  201. uint64_t *buffers_len, size;
  202. void *ptr;
  203. void **buffers, *misc_data, *return_data;
  204. void (*func) (uint32_t, void **, uint64_t *,
  205. void *, uint16_t, void*, uint16_t);
  206. /* Receive data from host. */
  207. READ (pipe_target, &func, sizeof (void *));
  208. READ (pipe_target, &buffer_count, sizeof (uint32_t));
  209. MALLOC (void **, buffers, buffer_count * sizeof (void *));
  210. MALLOC (uint64_t *, buffers_len, buffer_count * sizeof (uint64_t));
  211. for (i = 0; i < buffer_count; i++)
  212. {
  213. READ (pipe_target, &(buffers_len[i]), sizeof (uint64_t));
  214. READ (pipe_target, &(buffers[i]), sizeof (void *));
  215. }
  216. READ (pipe_target, &misc_data_len, sizeof (uint16_t));
  217. if (misc_data_len > 0)
  218. {
  219. MALLOC (void *, misc_data, misc_data_len);
  220. READ (pipe_target, misc_data, misc_data_len);
  221. }
  222. READ (pipe_target, &return_data_len, sizeof (uint16_t));
  223. if (return_data_len > 0)
  224. MALLOC (void *, return_data, return_data_len);
  225. /* Run function. */
  226. func (buffer_count, buffers, buffers_len, misc_data,
  227. misc_data_len, return_data, return_data_len);
  228. /* Send data to host if any or just send notification. */
  229. WRITE (pipe_host, return_data_len > 0 ? return_data : &cmd,
  230. return_data_len > 0 ? return_data_len : sizeof (cmd_t));
  231. /* Clean up. */
  232. free (buffers);
  233. free (buffers_len);
  234. if (misc_data_len > 0)
  235. free (misc_data);
  236. if (return_data_len > 0)
  237. free (return_data);
  238. break;
  239. }
  240. case CMD_SHUTDOWN:
  241. if (close (pipe_host) < 0)
  242. COIERROR ("Cannot close target-to-host pipe.");
  243. if (close (pipe_target) < 0)
  244. COIERROR ("Cannot close host-to-target pipe.");
  245. return COI_SUCCESS;
  246. default:
  247. COIERROR ("Unrecognizable command from host.");
  248. }
  249. }
  250. return COI_ERROR;
  251. }
  252. uint64_t
  253. SYMBOL_VERSION (COIPerfGetCycleFrequency, 1) ()
  254. {
  255. COITRACE ("COIPerfGetCycleFrequency");
  256. return (uint64_t) CYCLE_FREQUENCY;
  257. }
  258. } // extern "C"