xtensa_perfmon_access.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "xtensa_perfmon_access.h"
  15. #include "xtensa-debug-module.h"
  16. #include "eri.h"
  17. esp_err_t xtensa_perfmon_init(int id, uint16_t select, uint16_t mask, int kernelcnt, int tracelevel)
  18. {
  19. if (id >= ERI_PERFMON_MAX) {
  20. return ESP_ERR_INVALID_ARG;
  21. }
  22. uint32_t pmc =
  23. ((tracelevel & PMCTRL_TRACELEVEL_MASK) << PMCTRL_TRACELEVEL_SHIFT) |
  24. ((select & PMCTRL_SELECT_MASK) << PMCTRL_SELECT_SHIFT) |
  25. ((mask & PMCTRL_MASK_MASK) << PMCTRL_MASK_SHIFT) |
  26. ((kernelcnt & 1) << PMCTRL_KRNLCNT_SHIFT);
  27. eri_write(ERI_PERFMON_PM0 + id * sizeof(int32_t), 0);
  28. eri_write(ERI_PERFMON_PMCTRL0 + id * sizeof(int32_t), pmc);
  29. return ESP_OK;
  30. }
  31. void xtensa_perfmon_dump(void)
  32. {
  33. uint32_t pgm = eri_read(ERI_PERFMON_PGM);
  34. uint32_t intpc = eri_read(ERI_PERFMON_INTPC);
  35. printf("perfmon: PGM=%08x, INTPC=%08x\n", pgm, intpc);
  36. for (int i = 0 ; i < ERI_PERFMON_MAX ; i++) {
  37. uint32_t pm = eri_read(ERI_PERFMON_PM0 + i * sizeof(int32_t));
  38. uint32_t pmctrl = eri_read(ERI_PERFMON_PMCTRL0 + i * sizeof(int32_t));
  39. uint32_t pmstat = eri_read(ERI_PERFMON_PMSTAT0 + i * sizeof(int32_t));
  40. printf("perfmon: pm%i=%08x, pmctrl%i=%08x, pmstat%i=%08x\n", i, pm, i, pmctrl, i, pmstat);
  41. }
  42. }
  43. esp_err_t xtensa_perfmon_reset(int id)
  44. {
  45. if (id >= ERI_PERFMON_MAX) {
  46. return ESP_ERR_INVALID_ARG;
  47. }
  48. eri_write(ERI_PERFMON_PM0 + id * sizeof(int32_t), 0);
  49. return ESP_OK;
  50. }
  51. void xtensa_perfmon_start(void)
  52. {
  53. eri_write(ERI_PERFMON_PGM, PGM_PMEN);
  54. }
  55. void xtensa_perfmon_stop(void)
  56. {
  57. eri_write(ERI_PERFMON_PGM, 0);
  58. }
  59. uint32_t xtensa_perfmon_value(int id)
  60. {
  61. if (id >= ERI_PERFMON_MAX) {
  62. return 0;
  63. }
  64. uint32_t result = eri_read(ERI_PERFMON_PM0 + id * sizeof(int32_t));
  65. return result;
  66. }
  67. esp_err_t xtensa_perfmon_overflow(int id)
  68. {
  69. if (id >= ERI_PERFMON_MAX) {
  70. return ESP_ERR_INVALID_ARG;
  71. }
  72. uint32_t result = eri_read(ERI_PERFMON_PMSTAT0 + id * sizeof(int32_t));
  73. if (result & 1) {
  74. return ESP_FAIL;
  75. }
  76. return ESP_OK;
  77. }