hva-mem.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (C) STMicroelectronics SA 2015
  3. * Authors: Yannick Fertre <yannick.fertre@st.com>
  4. * Hugues Fruchet <hugues.fruchet@st.com>
  5. * License terms: GNU General Public License (GPL), version 2
  6. */
  7. #include "hva.h"
  8. #include "hva-mem.h"
  9. int hva_mem_alloc(struct hva_ctx *ctx, u32 size, const char *name,
  10. struct hva_buffer **buf)
  11. {
  12. struct device *dev = ctx_to_dev(ctx);
  13. struct hva_buffer *b;
  14. dma_addr_t paddr;
  15. void *base;
  16. b = devm_kzalloc(dev, sizeof(*b), GFP_KERNEL);
  17. if (!b) {
  18. ctx->sys_errors++;
  19. return -ENOMEM;
  20. }
  21. base = dma_alloc_attrs(dev, size, &paddr, GFP_KERNEL | GFP_DMA,
  22. DMA_ATTR_WRITE_COMBINE);
  23. if (!base) {
  24. dev_err(dev, "%s %s : dma_alloc_attrs failed for %s (size=%d)\n",
  25. ctx->name, __func__, name, size);
  26. ctx->sys_errors++;
  27. devm_kfree(dev, b);
  28. return -ENOMEM;
  29. }
  30. b->size = size;
  31. b->paddr = paddr;
  32. b->vaddr = base;
  33. b->name = name;
  34. dev_dbg(dev,
  35. "%s allocate %d bytes of HW memory @(virt=%p, phy=%pad): %s\n",
  36. ctx->name, size, b->vaddr, &b->paddr, b->name);
  37. /* return hva buffer to user */
  38. *buf = b;
  39. return 0;
  40. }
  41. void hva_mem_free(struct hva_ctx *ctx, struct hva_buffer *buf)
  42. {
  43. struct device *dev = ctx_to_dev(ctx);
  44. dev_dbg(dev,
  45. "%s free %d bytes of HW memory @(virt=%p, phy=%pad): %s\n",
  46. ctx->name, buf->size, buf->vaddr, &buf->paddr, buf->name);
  47. dma_free_attrs(dev, buf->size, buf->vaddr, buf->paddr,
  48. DMA_ATTR_WRITE_COMBINE);
  49. devm_kfree(dev, buf);
  50. }