dscr_sysfs_test.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. if (!(dp->d_type & DT_DIR))
  50. continue;
  51. if (!strcmp(dp->d_name, "cpuidle"))
  52. continue;
  53. if (!strstr(dp->d_name, "cpu"))
  54. continue;
  55. sprintf(file, "%s%s/dscr", CPU_PATH, dp->d_name);
  56. if (access(file, F_OK))
  57. continue;
  58. if (check_cpu_dscr_default(file, val))
  59. return 1;
  60. }
  61. closedir(sysfs);
  62. return 0;
  63. }
  64. int dscr_sysfs(void)
  65. {
  66. unsigned long orig_dscr_default;
  67. int i, j;
  68. orig_dscr_default = get_default_dscr();
  69. for (i = 0; i < COUNT; i++) {
  70. for (j = 0; j < DSCR_MAX; j++) {
  71. set_default_dscr(j);
  72. if (check_all_cpu_dscr_defaults(j))
  73. goto fail;
  74. }
  75. }
  76. set_default_dscr(orig_dscr_default);
  77. return 0;
  78. fail:
  79. set_default_dscr(orig_dscr_default);
  80. return 1;
  81. }
  82. int main(int argc, char *argv[])
  83. {
  84. return test_harness(dscr_sysfs, "dscr_sysfs_test");
  85. }