GoogleGit

blob: 4060b68a58ef875ca0aa749cde1b596b0245fc3c [file] [log] [blame]
  1. /* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
  2. * Use of this source code is governed by a BSD-style license that can be
  3. * found in the LICENSE file.
  4. */
  5. /* Shared memory module for Chrome EC */
  6. #include "common.h"
  7. #include "console.h"
  8. #include "link_defs.h"
  9. #include "shared_mem.h"
  10. #include "system.h"
  11. #include "util.h"
  12. static int buf_in_use;
  13. static int max_used;
  14. int shared_mem_size(void)
  15. {
  16. /*
  17. * Use all the RAM we can. The shared memory buffer is the last thing
  18. * allocated from the start of RAM, so we can use everything up to the
  19. * jump data at the end of RAM.
  20. */
  21. return system_usable_ram_end() - (uintptr_t)__shared_mem_buf;
  22. }
  23. int shared_mem_acquire(int size, char **dest_ptr)
  24. {
  25. if (size > shared_mem_size() || size <= 0)
  26. return EC_ERROR_INVAL;
  27. if (buf_in_use)
  28. return EC_ERROR_BUSY;
  29. /*
  30. * We could guard buf_in_use with a mutex, but since shared memory is
  31. * currently only used by debug commands, that's overkill.
  32. */
  33. buf_in_use = size;
  34. *dest_ptr = __shared_mem_buf;
  35. if (max_used < size)
  36. max_used = size;
  37. return EC_SUCCESS;
  38. }
  39. void shared_mem_release(void *ptr)
  40. {
  41. buf_in_use = 0;
  42. }
  43. #ifdef CONFIG_CMD_SHMEM
  44. static int command_shmem(int argc, char **argv)
  45. {
  46. ccprintf("Size:%6d\n", shared_mem_size());
  47. ccprintf("Used:%6d\n", buf_in_use);
  48. ccprintf("Max: %6d\n", max_used);
  49. return EC_SUCCESS;
  50. }
  51. DECLARE_CONSOLE_COMMAND(shmem, command_shmem,
  52. NULL,
  53. "Print shared memory stats",
  54. NULL);
  55. #endif