multifd-uadk.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Multifd UADK compression accelerator implementation
  3. *
  4. * Copyright (c) 2024 Huawei Technologies R & D (UK) Ltd
  5. *
  6. * Authors:
  7. * Shameer Kolothum <shameerali.kolothum.thodi@huawei.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. */
  12. #include "qemu/osdep.h"
  13. #include "qemu/module.h"
  14. #include "qapi/error.h"
  15. #include "exec/ramblock.h"
  16. #include "migration.h"
  17. #include "multifd.h"
  18. #include "options.h"
  19. #include "qemu/error-report.h"
  20. #include "uadk/wd_comp.h"
  21. #include "uadk/wd_sched.h"
  22. struct wd_data {
  23. handle_t handle;
  24. uint8_t *buf;
  25. uint32_t *buf_hdr;
  26. };
  27. static bool uadk_hw_init(void)
  28. {
  29. char alg[] = "zlib";
  30. int ret;
  31. ret = wd_comp_init2(alg, SCHED_POLICY_RR, TASK_HW);
  32. if (ret && ret != -WD_EEXIST) {
  33. return false;
  34. } else {
  35. return true;
  36. }
  37. }
  38. static struct wd_data *multifd_uadk_init_sess(uint32_t count,
  39. uint32_t page_size,
  40. bool compress, Error **errp)
  41. {
  42. struct wd_comp_sess_setup ss = {0};
  43. struct sched_params param = {0};
  44. uint32_t size = count * page_size;
  45. struct wd_data *wd;
  46. wd = g_new0(struct wd_data, 1);
  47. if (uadk_hw_init()) {
  48. ss.alg_type = WD_ZLIB;
  49. if (compress) {
  50. ss.op_type = WD_DIR_COMPRESS;
  51. /* Add an additional page for handling output > input */
  52. size += page_size;
  53. } else {
  54. ss.op_type = WD_DIR_DECOMPRESS;
  55. }
  56. /* We use default level 1 compression and 4K window size */
  57. param.type = ss.op_type;
  58. ss.sched_param = &param;
  59. wd->handle = wd_comp_alloc_sess(&ss);
  60. if (!wd->handle) {
  61. error_setg(errp, "multifd: failed wd_comp_alloc_sess");
  62. goto out;
  63. }
  64. } else {
  65. /* For CI test use */
  66. warn_report_once("UADK hardware not available. Switch to no compression mode");
  67. }
  68. wd->buf = g_try_malloc(size);
  69. if (!wd->buf) {
  70. error_setg(errp, "multifd: out of mem for uadk buf");
  71. goto out_free_sess;
  72. }
  73. wd->buf_hdr = g_new0(uint32_t, count);
  74. return wd;
  75. out_free_sess:
  76. if (wd->handle) {
  77. wd_comp_free_sess(wd->handle);
  78. }
  79. out:
  80. wd_comp_uninit2();
  81. g_free(wd);
  82. return NULL;
  83. }
  84. static void multifd_uadk_uninit_sess(struct wd_data *wd)
  85. {
  86. if (wd->handle) {
  87. wd_comp_free_sess(wd->handle);
  88. }
  89. wd_comp_uninit2();
  90. g_free(wd->buf);
  91. g_free(wd->buf_hdr);
  92. g_free(wd);
  93. }
  94. static int multifd_uadk_send_setup(MultiFDSendParams *p, Error **errp)
  95. {
  96. struct wd_data *wd;
  97. uint32_t page_size = multifd_ram_page_size();
  98. uint32_t page_count = multifd_ram_page_count();
  99. wd = multifd_uadk_init_sess(page_count, page_size, true, errp);
  100. if (!wd) {
  101. return -1;
  102. }
  103. p->compress_data = wd;
  104. assert(p->iov == NULL);
  105. /*
  106. * Each page will be compressed independently and sent using an IOV. The
  107. * additional two IOVs are used to store packet header and compressed data
  108. * length
  109. */
  110. p->iov = g_new0(struct iovec, page_count + 2);
  111. return 0;
  112. }
  113. static void multifd_uadk_send_cleanup(MultiFDSendParams *p, Error **errp)
  114. {
  115. struct wd_data *wd = p->compress_data;
  116. multifd_uadk_uninit_sess(wd);
  117. p->compress_data = NULL;
  118. g_free(p->iov);
  119. p->iov = NULL;
  120. }
  121. static inline void prepare_next_iov(MultiFDSendParams *p, void *base,
  122. uint32_t len)
  123. {
  124. p->iov[p->iovs_num].iov_base = (uint8_t *)base;
  125. p->iov[p->iovs_num].iov_len = len;
  126. p->next_packet_size += len;
  127. p->iovs_num++;
  128. }
  129. static int multifd_uadk_send_prepare(MultiFDSendParams *p, Error **errp)
  130. {
  131. struct wd_data *uadk_data = p->compress_data;
  132. uint32_t hdr_size;
  133. uint32_t page_size = multifd_ram_page_size();
  134. uint8_t *buf = uadk_data->buf;
  135. int ret = 0;
  136. MultiFDPages_t *pages = &p->data->u.ram;
  137. if (!multifd_send_prepare_common(p)) {
  138. goto out;
  139. }
  140. hdr_size = pages->normal_num * sizeof(uint32_t);
  141. /* prepare the header that stores the lengths of all compressed data */
  142. prepare_next_iov(p, uadk_data->buf_hdr, hdr_size);
  143. for (int i = 0; i < pages->normal_num; i++) {
  144. struct wd_comp_req creq = {
  145. .op_type = WD_DIR_COMPRESS,
  146. .src = pages->block->host + pages->offset[i],
  147. .src_len = page_size,
  148. .dst = buf,
  149. /* Set dst_len to double the src in case compressed out >= page_size */
  150. .dst_len = page_size * 2,
  151. };
  152. if (uadk_data->handle) {
  153. ret = wd_do_comp_sync(uadk_data->handle, &creq);
  154. if (ret || creq.status) {
  155. error_setg(errp, "multifd %u: failed compression, ret %d status %d",
  156. p->id, ret, creq.status);
  157. return -1;
  158. }
  159. if (creq.dst_len < page_size) {
  160. uadk_data->buf_hdr[i] = cpu_to_be32(creq.dst_len);
  161. prepare_next_iov(p, buf, creq.dst_len);
  162. buf += creq.dst_len;
  163. }
  164. }
  165. /*
  166. * Send raw data if no UADK hardware or if compressed out >= page_size.
  167. * We might be better off sending raw data if output is slightly less
  168. * than page_size as well because at the receive end we can skip the
  169. * decompression. But it is tricky to find the right number here.
  170. */
  171. if (!uadk_data->handle || creq.dst_len >= page_size) {
  172. uadk_data->buf_hdr[i] = cpu_to_be32(page_size);
  173. prepare_next_iov(p, pages->block->host + pages->offset[i],
  174. page_size);
  175. buf += page_size;
  176. }
  177. }
  178. out:
  179. p->flags |= MULTIFD_FLAG_UADK;
  180. multifd_send_fill_packet(p);
  181. return 0;
  182. }
  183. static int multifd_uadk_recv_setup(MultiFDRecvParams *p, Error **errp)
  184. {
  185. struct wd_data *wd;
  186. uint32_t page_size = multifd_ram_page_size();
  187. uint32_t page_count = multifd_ram_page_count();
  188. wd = multifd_uadk_init_sess(page_count, page_size, false, errp);
  189. if (!wd) {
  190. return -1;
  191. }
  192. p->compress_data = wd;
  193. return 0;
  194. }
  195. static void multifd_uadk_recv_cleanup(MultiFDRecvParams *p)
  196. {
  197. struct wd_data *wd = p->compress_data;
  198. multifd_uadk_uninit_sess(wd);
  199. p->compress_data = NULL;
  200. }
  201. static int multifd_uadk_recv(MultiFDRecvParams *p, Error **errp)
  202. {
  203. struct wd_data *uadk_data = p->compress_data;
  204. uint32_t in_size = p->next_packet_size;
  205. uint32_t flags = p->flags & MULTIFD_FLAG_COMPRESSION_MASK;
  206. uint32_t hdr_len = p->normal_num * sizeof(uint32_t);
  207. uint32_t data_len = 0;
  208. uint32_t page_size = multifd_ram_page_size();
  209. uint8_t *buf = uadk_data->buf;
  210. int ret = 0;
  211. if (flags != MULTIFD_FLAG_UADK) {
  212. error_setg(errp, "multifd %u: flags received %x flags expected %x",
  213. p->id, flags, MULTIFD_FLAG_ZLIB);
  214. return -1;
  215. }
  216. multifd_recv_zero_page_process(p);
  217. if (!p->normal_num) {
  218. assert(in_size == 0);
  219. return 0;
  220. }
  221. /* read compressed data lengths */
  222. assert(hdr_len < in_size);
  223. ret = qio_channel_read_all(p->c, (void *) uadk_data->buf_hdr,
  224. hdr_len, errp);
  225. if (ret != 0) {
  226. return ret;
  227. }
  228. for (int i = 0; i < p->normal_num; i++) {
  229. uadk_data->buf_hdr[i] = be32_to_cpu(uadk_data->buf_hdr[i]);
  230. data_len += uadk_data->buf_hdr[i];
  231. assert(uadk_data->buf_hdr[i] <= page_size);
  232. }
  233. /* read compressed data */
  234. assert(in_size == hdr_len + data_len);
  235. ret = qio_channel_read_all(p->c, (void *)buf, data_len, errp);
  236. if (ret != 0) {
  237. return ret;
  238. }
  239. for (int i = 0; i < p->normal_num; i++) {
  240. struct wd_comp_req creq = {
  241. .op_type = WD_DIR_DECOMPRESS,
  242. .src = buf,
  243. .src_len = uadk_data->buf_hdr[i],
  244. .dst = p->host + p->normal[i],
  245. .dst_len = page_size,
  246. };
  247. if (uadk_data->buf_hdr[i] == page_size) {
  248. memcpy(p->host + p->normal[i], buf, page_size);
  249. buf += page_size;
  250. continue;
  251. }
  252. if (unlikely(!uadk_data->handle)) {
  253. error_setg(errp, "multifd %u: UADK HW not available for decompression",
  254. p->id);
  255. return -1;
  256. }
  257. ret = wd_do_comp_sync(uadk_data->handle, &creq);
  258. if (ret || creq.status) {
  259. error_setg(errp, "multifd %u: failed decompression, ret %d status %d",
  260. p->id, ret, creq.status);
  261. return -1;
  262. }
  263. if (creq.dst_len != page_size) {
  264. error_setg(errp, "multifd %u: decompressed length error", p->id);
  265. return -1;
  266. }
  267. buf += uadk_data->buf_hdr[i];
  268. }
  269. return 0;
  270. }
  271. static const MultiFDMethods multifd_uadk_ops = {
  272. .send_setup = multifd_uadk_send_setup,
  273. .send_cleanup = multifd_uadk_send_cleanup,
  274. .send_prepare = multifd_uadk_send_prepare,
  275. .recv_setup = multifd_uadk_recv_setup,
  276. .recv_cleanup = multifd_uadk_recv_cleanup,
  277. .recv = multifd_uadk_recv,
  278. };
  279. static void multifd_uadk_register(void)
  280. {
  281. multifd_register_ops(MULTIFD_COMPRESSION_UADK, &multifd_uadk_ops);
  282. }
  283. migration_init(multifd_uadk_register);