cc_sram_mgr.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2012-2019 ARM Limited (or its affiliates). */
  3. #include "cc_driver.h"
  4. #include "cc_sram_mgr.h"
  5. /**
  6. * struct cc_sram_ctx -Internal RAM context manager
  7. * @sram_free_offset: the offset to the non-allocated area
  8. */
  9. struct cc_sram_ctx {
  10. cc_sram_addr_t sram_free_offset;
  11. };
  12. /**
  13. * cc_sram_mgr_fini() - Cleanup SRAM pool.
  14. *
  15. * @drvdata: Associated device driver context
  16. */
  17. void cc_sram_mgr_fini(struct cc_drvdata *drvdata)
  18. {
  19. /* Nothing needed */
  20. }
  21. /**
  22. * cc_sram_mgr_init() - Initializes SRAM pool.
  23. * The pool starts right at the beginning of SRAM.
  24. * Returns zero for success, negative value otherwise.
  25. *
  26. * @drvdata: Associated device driver context
  27. */
  28. int cc_sram_mgr_init(struct cc_drvdata *drvdata)
  29. {
  30. struct cc_sram_ctx *ctx;
  31. dma_addr_t start = 0;
  32. struct device *dev = drvdata_to_dev(drvdata);
  33. if (drvdata->hw_rev < CC_HW_REV_712) {
  34. /* Pool starts after ROM bytes */
  35. start = (dma_addr_t)cc_ioread(drvdata,
  36. CC_REG(HOST_SEP_SRAM_THRESHOLD));
  37. if ((start & 0x3) != 0) {
  38. dev_err(dev, "Invalid SRAM offset %pad\n", &start);
  39. return -EINVAL;
  40. }
  41. }
  42. /* Allocate "this" context */
  43. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  44. if (!ctx)
  45. return -ENOMEM;
  46. ctx->sram_free_offset = start;
  47. drvdata->sram_mgr_handle = ctx;
  48. return 0;
  49. }
  50. /*!
  51. * Allocated buffer from SRAM pool.
  52. * Note: Caller is responsible to free the LAST allocated buffer.
  53. * This function does not taking care of any fragmentation may occur
  54. * by the order of calls to alloc/free.
  55. *
  56. * \param drvdata
  57. * \param size The requested bytes to allocate
  58. */
  59. cc_sram_addr_t cc_sram_alloc(struct cc_drvdata *drvdata, u32 size)
  60. {
  61. struct cc_sram_ctx *smgr_ctx = drvdata->sram_mgr_handle;
  62. struct device *dev = drvdata_to_dev(drvdata);
  63. cc_sram_addr_t p;
  64. if ((size & 0x3)) {
  65. dev_err(dev, "Requested buffer size (%u) is not multiple of 4",
  66. size);
  67. return NULL_SRAM_ADDR;
  68. }
  69. if (size > (CC_CC_SRAM_SIZE - smgr_ctx->sram_free_offset)) {
  70. dev_err(dev, "Not enough space to allocate %u B (at offset %llu)\n",
  71. size, smgr_ctx->sram_free_offset);
  72. return NULL_SRAM_ADDR;
  73. }
  74. p = smgr_ctx->sram_free_offset;
  75. smgr_ctx->sram_free_offset += size;
  76. dev_dbg(dev, "Allocated %u B @ %u\n", size, (unsigned int)p);
  77. return p;
  78. }
  79. /**
  80. * cc_set_sram_desc() - Create const descriptors sequence to
  81. * set values in given array into SRAM.
  82. * Note: each const value can't exceed word size.
  83. *
  84. * @src: A pointer to array of words to set as consts.
  85. * @dst: The target SRAM buffer to set into
  86. * @nelements: The number of words in "src" array
  87. * @seq: A pointer to the given IN/OUT descriptor sequence
  88. * @seq_len: A pointer to the given IN/OUT sequence length
  89. */
  90. void cc_set_sram_desc(const u32 *src, cc_sram_addr_t dst,
  91. unsigned int nelement, struct cc_hw_desc *seq,
  92. unsigned int *seq_len)
  93. {
  94. u32 i;
  95. unsigned int idx = *seq_len;
  96. for (i = 0; i < nelement; i++, idx++) {
  97. hw_desc_init(&seq[idx]);
  98. set_din_const(&seq[idx], src[i], sizeof(u32));
  99. set_dout_sram(&seq[idx], dst + (i * sizeof(u32)), sizeof(u32));
  100. set_flow_mode(&seq[idx], BYPASS);
  101. }
  102. *seq_len = idx;
  103. }