udbg_memcons.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * A udbg backend which logs messages and reads input from in memory
  3. * buffers.
  4. *
  5. * The console output can be read from memcons_output which is a
  6. * circular buffer whose next write position is stored in memcons.output_pos.
  7. *
  8. * Input may be passed by writing into the memcons_input buffer when it is
  9. * empty. The input buffer is empty when both input_pos == input_start and
  10. * *input_start == '\0'.
  11. *
  12. * Copyright (C) 2003-2005 Anton Blanchard and Milton Miller, IBM Corp
  13. * Copyright (C) 2013 Alistair Popple, IBM Corp
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. */
  20. #include <linux/kernel.h>
  21. #include <asm/barrier.h>
  22. #include <asm/page.h>
  23. #include <asm/processor.h>
  24. #include <asm/udbg.h>
  25. struct memcons {
  26. char *output_start;
  27. char *output_pos;
  28. char *output_end;
  29. char *input_start;
  30. char *input_pos;
  31. char *input_end;
  32. };
  33. static char memcons_output[CONFIG_PPC_MEMCONS_OUTPUT_SIZE];
  34. static char memcons_input[CONFIG_PPC_MEMCONS_INPUT_SIZE];
  35. struct memcons memcons = {
  36. .output_start = memcons_output,
  37. .output_pos = memcons_output,
  38. .output_end = &memcons_output[CONFIG_PPC_MEMCONS_OUTPUT_SIZE],
  39. .input_start = memcons_input,
  40. .input_pos = memcons_input,
  41. .input_end = &memcons_input[CONFIG_PPC_MEMCONS_INPUT_SIZE],
  42. };
  43. void memcons_putc(char c)
  44. {
  45. char *new_output_pos;
  46. *memcons.output_pos = c;
  47. wmb();
  48. new_output_pos = memcons.output_pos + 1;
  49. if (new_output_pos >= memcons.output_end)
  50. new_output_pos = memcons.output_start;
  51. memcons.output_pos = new_output_pos;
  52. }
  53. int memcons_getc_poll(void)
  54. {
  55. char c;
  56. char *new_input_pos;
  57. if (*memcons.input_pos) {
  58. c = *memcons.input_pos;
  59. new_input_pos = memcons.input_pos + 1;
  60. if (new_input_pos >= memcons.input_end)
  61. new_input_pos = memcons.input_start;
  62. else if (*new_input_pos == '\0')
  63. new_input_pos = memcons.input_start;
  64. *memcons.input_pos = '\0';
  65. wmb();
  66. memcons.input_pos = new_input_pos;
  67. return c;
  68. }
  69. return -1;
  70. }
  71. int memcons_getc(void)
  72. {
  73. int c;
  74. while (1) {
  75. c = memcons_getc_poll();
  76. if (c == -1)
  77. cpu_relax();
  78. else
  79. break;
  80. }
  81. return c;
  82. }
  83. void udbg_init_memcons(void)
  84. {
  85. udbg_putc = memcons_putc;
  86. udbg_getc = memcons_getc;
  87. udbg_getc_poll = memcons_getc_poll;
  88. }