dscr_sysfs_test.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * POWER Data Stream Control Register (DSCR) sysfs interface test
  3. *
  4. * This test updates to system wide DSCR default through the sysfs interface
  5. * and then verifies that all the CPU specific DSCR defaults are updated as
  6. * well verified from their sysfs interfaces.
  7. *
  8. * Copyright 2015, Anshuman Khandual, IBM Corporation.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published
  12. * by the Free Software Foundation.
  13. */
  14. #include "dscr.h"
  15. static int check_cpu_dscr_default(char *file, unsigned long val)
  16. {
  17. char buf[10];
  18. int fd, rc;
  19. fd = open(file, O_RDWR);
  20. if (fd == -1) {
  21. perror("open() failed");
  22. return 1;
  23. }
  24. rc = read(fd, buf, sizeof(buf));
  25. if (rc == -1) {
  26. perror("read() failed");
  27. return 1;
  28. }
  29. close(fd);
  30. buf[rc] = '\0';
  31. if (strtol(buf, NULL, 16) != val) {
  32. printf("DSCR match failed: %ld (system) %ld (cpu)\n",
  33. val, strtol(buf, NULL, 16));
  34. return 1;
  35. }
  36. return 0;
  37. }
  38. static int check_all_cpu_dscr_defaults(unsigned long val)
  39. {
  40. DIR *sysfs;
  41. struct dirent *dp;
  42. char file[LEN_MAX];
  43. sysfs = opendir(CPU_PATH);
  44. if (!sysfs) {
  45. perror("opendir() failed");
  46. return 1;
  47. }
  48. while ((dp = readdir(sysfs))) {
  49. int len;
  50. if (!(dp->d_type & DT_DIR))
  51. continue;
  52. if (!strcmp(dp->d_name, "cpuidle"))
  53. continue;
  54. if (!strstr(dp->d_name, "cpu"))
  55. continue;
  56. len = snprintf(file, LEN_MAX, "%s%s/dscr", CPU_PATH, dp->d_name);
  57. if (len >= LEN_MAX)
  58. continue;
  59. if (access(file, F_OK))
  60. continue;
  61. if (check_cpu_dscr_default(file, val))
  62. return 1;
  63. }
  64. closedir(sysfs);
  65. return 0;
  66. }
  67. int dscr_sysfs(void)
  68. {
  69. unsigned long orig_dscr_default;
  70. int i, j;
  71. orig_dscr_default = get_default_dscr();
  72. for (i = 0; i < COUNT; i++) {
  73. for (j = 0; j < DSCR_MAX; j++) {
  74. set_default_dscr(j);
  75. if (check_all_cpu_dscr_defaults(j))
  76. goto fail;
  77. }
  78. }
  79. set_default_dscr(orig_dscr_default);
  80. return 0;
  81. fail:
  82. set_default_dscr(orig_dscr_default);
  83. return 1;
  84. }
  85. int main(int argc, char *argv[])
  86. {
  87. return test_harness(dscr_sysfs, "dscr_sysfs_test");
  88. }