sbi_scratch.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2019 Western Digital Corporation or its affiliates.
  5. *
  6. * Authors:
  7. * Anup Patel <anup.patel@wdc.com>
  8. */
  9. #include <sbi/riscv_locks.h>
  10. #include <sbi/sbi_scratch.h>
  11. static spinlock_t extra_lock = SPIN_LOCK_INITIALIZER;
  12. static unsigned long extra_offset = SBI_SCRATCH_EXTRA_SPACE_OFFSET;
  13. unsigned long sbi_scratch_alloc_offset(unsigned long size, const char *owner)
  14. {
  15. unsigned long ret = 0;
  16. /*
  17. * We have a simple brain-dead allocator which never expects
  18. * anything to be free-ed hence it keeps incrementing the
  19. * next allocation offset until it runs-out of space.
  20. *
  21. * In future, we will have more sophisticated allocator which
  22. * will allow us to re-claim free-ed space.
  23. */
  24. if (!size)
  25. return 0;
  26. while (size & (__SIZEOF_POINTER__ - 1))
  27. size++;
  28. spin_lock(&extra_lock);
  29. if (SBI_SCRATCH_SIZE < (extra_offset + size))
  30. goto done;
  31. ret = extra_offset;
  32. extra_offset += size;
  33. done:
  34. spin_unlock(&extra_lock);
  35. return ret;
  36. }
  37. void sbi_scratch_free_offset(unsigned long offset)
  38. {
  39. if ((offset < SBI_SCRATCH_EXTRA_SPACE_OFFSET) ||
  40. (SBI_SCRATCH_SIZE <= offset))
  41. return;
  42. /*
  43. * We don't actually free-up because it's a simple
  44. * brain-dead allocator.
  45. */
  46. }