rvu_common.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2018 Marvell International Ltd.
  4. */
  5. #include <dm.h>
  6. #include <errno.h>
  7. #include <malloc.h>
  8. #include <misc.h>
  9. #include <net.h>
  10. #include <asm/io.h>
  11. #include "rvu.h"
  12. int qmem_alloc(struct qmem *q, u32 qsize, size_t entry_sz)
  13. {
  14. q->base = memalign(CONFIG_SYS_CACHELINE_SIZE, qsize * entry_sz);
  15. if (!q->base)
  16. return -ENOMEM;
  17. q->entry_sz = entry_sz;
  18. q->qsize = qsize;
  19. q->alloc_sz = (size_t)qsize * entry_sz;
  20. q->iova = (dma_addr_t)(q->base);
  21. debug("NIX: qmem alloc for (%d * %d = %ld bytes) at %p\n",
  22. q->qsize, q->entry_sz, q->alloc_sz, q->base);
  23. return 0;
  24. }
  25. void qmem_free(struct qmem *q)
  26. {
  27. if (q->base)
  28. free(q->base);
  29. memset(q, 0, sizeof(*q));
  30. }
  31. /**
  32. * Allocates an admin queue for instructions and results
  33. *
  34. * @param aq admin queue to allocate for
  35. * @param qsize Number of entries in the queue
  36. * @param inst_size Size of each instruction
  37. * @param res_size Size of each result
  38. *
  39. * @return -ENOMEM on error, 0 on success
  40. */
  41. int rvu_aq_alloc(struct admin_queue *aq, unsigned int qsize,
  42. size_t inst_size, size_t res_size)
  43. {
  44. int err;
  45. err = qmem_alloc(&aq->inst, qsize, inst_size);
  46. if (err)
  47. return err;
  48. err = qmem_alloc(&aq->res, qsize, res_size);
  49. if (err)
  50. qmem_free(&aq->inst);
  51. return err;
  52. }
  53. /**
  54. * Frees an admin queue
  55. *
  56. * @param aq Admin queue to free
  57. */
  58. void rvu_aq_free(struct admin_queue *aq)
  59. {
  60. qmem_free(&aq->inst);
  61. qmem_free(&aq->res);
  62. memset(aq, 0, sizeof(*aq));
  63. }