thneed_qcom2.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include "selfdrive/modeld/thneed/thneed.h"
  2. #include <dlfcn.h>
  3. #include <sys/mman.h>
  4. #include <cassert>
  5. #include <cerrno>
  6. #include <cstring>
  7. #include <map>
  8. #include <string>
  9. #include "common/clutil.h"
  10. #include "common/timing.h"
  11. Thneed *g_thneed = NULL;
  12. int g_fd = -1;
  13. void hexdump(uint8_t *d, int len) {
  14. assert((len%4) == 0);
  15. printf(" dumping %p len 0x%x\n", d, len);
  16. for (int i = 0; i < len/4; i++) {
  17. if (i != 0 && (i%0x10) == 0) printf("\n");
  18. printf("%8x ", d[i]);
  19. }
  20. printf("\n");
  21. }
  22. // *********** ioctl interceptor ***********
  23. extern "C" {
  24. int (*my_ioctl)(int filedes, unsigned long request, void *argp) = NULL;
  25. #undef ioctl
  26. int ioctl(int filedes, unsigned long request, void *argp) {
  27. request &= 0xFFFFFFFF; // needed on QCOM2
  28. if (my_ioctl == NULL) my_ioctl = reinterpret_cast<decltype(my_ioctl)>(dlsym(RTLD_NEXT, "ioctl"));
  29. Thneed *thneed = g_thneed;
  30. // save the fd
  31. if (request == IOCTL_KGSL_GPUOBJ_ALLOC) g_fd = filedes;
  32. // note that this runs always, even without a thneed object
  33. if (request == IOCTL_KGSL_DRAWCTXT_CREATE) {
  34. struct kgsl_drawctxt_create *create = (struct kgsl_drawctxt_create *)argp;
  35. create->flags &= ~KGSL_CONTEXT_PRIORITY_MASK;
  36. create->flags |= 6 << KGSL_CONTEXT_PRIORITY_SHIFT; // priority from 1-15, 1 is max priority
  37. printf("IOCTL_KGSL_DRAWCTXT_CREATE: creating context with flags 0x%x\n", create->flags);
  38. }
  39. if (thneed != NULL) {
  40. if (request == IOCTL_KGSL_GPU_COMMAND) {
  41. struct kgsl_gpu_command *cmd = (struct kgsl_gpu_command *)argp;
  42. if (thneed->record) {
  43. thneed->timestamp = cmd->timestamp;
  44. thneed->context_id = cmd->context_id;
  45. thneed->cmds.push_back(unique_ptr<CachedCommand>(new CachedCommand(thneed, cmd)));
  46. }
  47. if (thneed->debug >= 1) {
  48. printf("IOCTL_KGSL_GPU_COMMAND(%2zu): flags: 0x%lx context_id: %u timestamp: %u numcmds: %d numobjs: %d\n",
  49. thneed->cmds.size(),
  50. cmd->flags,
  51. cmd->context_id, cmd->timestamp, cmd->numcmds, cmd->numobjs);
  52. }
  53. } else if (request == IOCTL_KGSL_GPUOBJ_SYNC) {
  54. struct kgsl_gpuobj_sync *cmd = (struct kgsl_gpuobj_sync *)argp;
  55. struct kgsl_gpuobj_sync_obj *objs = (struct kgsl_gpuobj_sync_obj *)(cmd->objs);
  56. if (thneed->debug >= 2) {
  57. printf("IOCTL_KGSL_GPUOBJ_SYNC count:%d ", cmd->count);
  58. for (int i = 0; i < cmd->count; i++) {
  59. printf(" -- offset:0x%lx len:0x%lx id:%d op:%d ", objs[i].offset, objs[i].length, objs[i].id, objs[i].op);
  60. }
  61. printf("\n");
  62. }
  63. if (thneed->record) {
  64. thneed->cmds.push_back(unique_ptr<CachedSync>(new
  65. CachedSync(thneed, string((char *)objs, sizeof(struct kgsl_gpuobj_sync_obj)*cmd->count))));
  66. }
  67. } else if (request == IOCTL_KGSL_DEVICE_WAITTIMESTAMP_CTXTID) {
  68. struct kgsl_device_waittimestamp_ctxtid *cmd = (struct kgsl_device_waittimestamp_ctxtid *)argp;
  69. if (thneed->debug >= 1) {
  70. printf("IOCTL_KGSL_DEVICE_WAITTIMESTAMP_CTXTID: context_id: %d timestamp: %d timeout: %d\n",
  71. cmd->context_id, cmd->timestamp, cmd->timeout);
  72. }
  73. } else if (request == IOCTL_KGSL_SETPROPERTY) {
  74. if (thneed->debug >= 1) {
  75. struct kgsl_device_getproperty *prop = (struct kgsl_device_getproperty *)argp;
  76. printf("IOCTL_KGSL_SETPROPERTY: 0x%x sizebytes:%zu\n", prop->type, prop->sizebytes);
  77. if (thneed->debug >= 2) {
  78. hexdump((uint8_t *)prop->value, prop->sizebytes);
  79. if (prop->type == KGSL_PROP_PWR_CONSTRAINT) {
  80. struct kgsl_device_constraint *constraint = (struct kgsl_device_constraint *)prop->value;
  81. hexdump((uint8_t *)constraint->data, constraint->size);
  82. }
  83. }
  84. }
  85. } else if (request == IOCTL_KGSL_DRAWCTXT_CREATE || request == IOCTL_KGSL_DRAWCTXT_DESTROY) {
  86. // this happens
  87. } else if (request == IOCTL_KGSL_GPUOBJ_ALLOC || request == IOCTL_KGSL_GPUOBJ_FREE) {
  88. // this happens
  89. } else {
  90. if (thneed->debug >= 1) {
  91. printf("other ioctl %lx\n", request);
  92. }
  93. }
  94. }
  95. int ret = my_ioctl(filedes, request, argp);
  96. // NOTE: This error message goes into stdout and messes up pyenv
  97. // if (ret != 0) printf("ioctl returned %d with errno %d\n", ret, errno);
  98. return ret;
  99. }
  100. }
  101. // *********** GPUMalloc ***********
  102. GPUMalloc::GPUMalloc(int size, int fd) {
  103. struct kgsl_gpuobj_alloc alloc;
  104. memset(&alloc, 0, sizeof(alloc));
  105. alloc.size = size;
  106. alloc.flags = 0x10000a00;
  107. ioctl(fd, IOCTL_KGSL_GPUOBJ_ALLOC, &alloc);
  108. void *addr = mmap64(NULL, alloc.mmapsize, 0x3, 0x1, fd, alloc.id*0x1000);
  109. assert(addr != MAP_FAILED);
  110. base = (uint64_t)addr;
  111. remaining = size;
  112. }
  113. GPUMalloc::~GPUMalloc() {
  114. // TODO: free the GPU malloced area
  115. }
  116. void *GPUMalloc::alloc(int size) {
  117. void *ret = (void*)base;
  118. size = (size+0xff) & (~0xFF);
  119. assert(size <= remaining);
  120. remaining -= size;
  121. base += size;
  122. return ret;
  123. }
  124. // *********** CachedSync, at the ioctl layer ***********
  125. void CachedSync::exec() {
  126. struct kgsl_gpuobj_sync cmd;
  127. cmd.objs = (uint64_t)data.data();
  128. cmd.obj_len = data.length();
  129. cmd.count = data.length() / sizeof(struct kgsl_gpuobj_sync_obj);
  130. int ret = ioctl(thneed->fd, IOCTL_KGSL_GPUOBJ_SYNC, &cmd);
  131. assert(ret == 0);
  132. }
  133. // *********** CachedCommand, at the ioctl layer ***********
  134. CachedCommand::CachedCommand(Thneed *lthneed, struct kgsl_gpu_command *cmd) {
  135. thneed = lthneed;
  136. assert(cmd->numsyncs == 0);
  137. memcpy(&cache, cmd, sizeof(cache));
  138. if (cmd->numcmds > 0) {
  139. cmds = make_unique<struct kgsl_command_object[]>(cmd->numcmds);
  140. memcpy(cmds.get(), (void *)cmd->cmdlist, sizeof(struct kgsl_command_object)*cmd->numcmds);
  141. cache.cmdlist = (uint64_t)cmds.get();
  142. for (int i = 0; i < cmd->numcmds; i++) {
  143. void *nn = thneed->ram->alloc(cmds[i].size);
  144. memcpy(nn, (void*)cmds[i].gpuaddr, cmds[i].size);
  145. cmds[i].gpuaddr = (uint64_t)nn;
  146. }
  147. }
  148. if (cmd->numobjs > 0) {
  149. objs = make_unique<struct kgsl_command_object[]>(cmd->numobjs);
  150. memcpy(objs.get(), (void *)cmd->objlist, sizeof(struct kgsl_command_object)*cmd->numobjs);
  151. cache.objlist = (uint64_t)objs.get();
  152. for (int i = 0; i < cmd->numobjs; i++) {
  153. void *nn = thneed->ram->alloc(objs[i].size);
  154. memset(nn, 0, objs[i].size);
  155. objs[i].gpuaddr = (uint64_t)nn;
  156. }
  157. }
  158. kq = thneed->ckq;
  159. thneed->ckq.clear();
  160. }
  161. void CachedCommand::exec() {
  162. cache.timestamp = ++thneed->timestamp;
  163. int ret = ioctl(thneed->fd, IOCTL_KGSL_GPU_COMMAND, &cache);
  164. if (thneed->debug >= 1) printf("CachedCommand::exec got %d\n", ret);
  165. if (thneed->debug >= 2) {
  166. for (auto &it : kq) {
  167. it->debug_print(false);
  168. }
  169. }
  170. assert(ret == 0);
  171. }
  172. // *********** Thneed ***********
  173. Thneed::Thneed(bool do_clinit, cl_context _context) {
  174. // TODO: QCOM2 actually requires a different context
  175. //context = _context;
  176. if (do_clinit) clinit();
  177. assert(g_fd != -1);
  178. fd = g_fd;
  179. ram = make_unique<GPUMalloc>(0x80000, fd);
  180. timestamp = -1;
  181. g_thneed = this;
  182. char *thneed_debug_env = getenv("THNEED_DEBUG");
  183. debug = (thneed_debug_env != NULL) ? atoi(thneed_debug_env) : 0;
  184. }
  185. void Thneed::wait() {
  186. struct kgsl_device_waittimestamp_ctxtid wait;
  187. wait.context_id = context_id;
  188. wait.timestamp = timestamp;
  189. wait.timeout = -1;
  190. uint64_t tb = nanos_since_boot();
  191. int wret = ioctl(fd, IOCTL_KGSL_DEVICE_WAITTIMESTAMP_CTXTID, &wait);
  192. uint64_t te = nanos_since_boot();
  193. if (debug >= 1) printf("wait %d after %lu us\n", wret, (te-tb)/1000);
  194. }
  195. void Thneed::execute(float **finputs, float *foutput, bool slow) {
  196. uint64_t tb, te;
  197. if (debug >= 1) tb = nanos_since_boot();
  198. // ****** copy inputs
  199. copy_inputs(finputs, true);
  200. // ****** run commands
  201. int i = 0;
  202. for (auto &it : cmds) {
  203. ++i;
  204. if (debug >= 1) printf("run %2d @ %7lu us: ", i, (nanos_since_boot()-tb)/1000);
  205. it->exec();
  206. if ((i == cmds.size()) || slow) wait();
  207. }
  208. // ****** copy outputs
  209. copy_output(foutput);
  210. if (debug >= 1) {
  211. te = nanos_since_boot();
  212. printf("model exec in %lu us\n", (te-tb)/1000);
  213. }
  214. }