irq_parse.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* irq_parse.c
  2. * Parse IRQ-related files.
  3. */
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <dirent.h>
  9. #include <limits.h>
  10. #include <ctype.h>
  11. #include "lub/list.h"
  12. #include "irq.h"
  13. #define STR(str) ( str ? str : "" )
  14. int irq_list_compare(const void *first, const void *second)
  15. {
  16. const irq_t *f = (const irq_t *)first;
  17. const irq_t *s = (const irq_t *)second;
  18. return (f->irq - s->irq);
  19. }
  20. static irq_t * irq_new(int num)
  21. {
  22. irq_t *new;
  23. if (!(new = malloc(sizeof(*new))))
  24. return NULL;
  25. new->irq = num;
  26. new->type = NULL;
  27. new->desc = NULL;
  28. new->refresh = 1;
  29. new->old_intr = 0;
  30. new->intr = 0;
  31. return new;
  32. }
  33. static void irq_free(irq_t *irq)
  34. {
  35. free(irq->type);
  36. free(irq->desc);
  37. free(irq);
  38. }
  39. irq_t * irq_list_search(lub_list_t *irqs, unsigned int num)
  40. {
  41. lub_list_node_t *node;
  42. irq_t search;
  43. search.irq = num;
  44. node = lub_list_search(irqs, &search);
  45. if (!node)
  46. return NULL;
  47. return (irq_t *)lub_list_node__get_data(node);
  48. }
  49. static irq_t * irq_list_add(lub_list_t *irqs, unsigned int num)
  50. {
  51. lub_list_node_t *node;
  52. irq_t *new;
  53. irq_t search;
  54. search.irq = num;
  55. node = lub_list_search(irqs, &search);
  56. if (node) /* IRQ already exists. May be renew some fields later */
  57. return (irq_t *)lub_list_node__get_data(node);
  58. if (!(new = irq_new(num)))
  59. return NULL;
  60. lub_list_add(irqs, new);
  61. return new;
  62. }
  63. int irq_list_free(lub_list_t *irqs)
  64. {
  65. lub_list_node_t *iter;
  66. while ((iter = lub_list__get_head(irqs))) {
  67. irq_t *irq;
  68. irq = (irq_t *)lub_list_node__get_data(iter);
  69. irq_free(irq);
  70. lub_list_del(irqs, iter);
  71. lub_list_node_free(iter);
  72. }
  73. lub_list_free(irqs);
  74. return 0;
  75. }
  76. /* Show IRQ information */
  77. static void irq_show(irq_t *irq)
  78. {
  79. char buf[NR_CPUS + 1];
  80. if (cpus_full(irq->local_cpus))
  81. snprintf(buf, sizeof(buf), "*");
  82. else
  83. cpumask_scnprintf(buf, sizeof(buf), irq->local_cpus);
  84. printf("IRQ %3d %s [%s] %s\n", irq->irq, buf, STR(irq->type), STR(irq->desc));
  85. }
  86. /* Show IRQ list */
  87. int irq_list_show(lub_list_t *irqs)
  88. {
  89. lub_list_node_t *iter;
  90. for (iter = lub_list_iterator_init(irqs); iter;
  91. iter = lub_list_iterator_next(iter)) {
  92. irq_t *irq;
  93. irq = (irq_t *)lub_list_node__get_data(iter);
  94. irq_show(irq);
  95. }
  96. return 0;
  97. }
  98. static int parse_local_cpus(lub_list_t *irqs, const char *sysfs_path,
  99. unsigned int num)
  100. {
  101. char path[PATH_MAX];
  102. FILE *fd;
  103. char *str = NULL;
  104. size_t sz;
  105. irq_t *irq;
  106. irq = irq_list_search(irqs, num);
  107. if (!irq)
  108. return -1;
  109. sprintf(path, "%s/%s/local_cpus", SYSFS_PCI_PATH, sysfs_path);
  110. if (!(fd = fopen(path, "r")))
  111. return -1;
  112. if (getline(&str, &sz, fd) < 0) {
  113. fclose(fd);
  114. return -1;
  115. }
  116. fclose(fd);
  117. cpumask_parse_user(str, strlen(str), irq->local_cpus);
  118. // printf("%d %s %s\n", num, str, sysfs_path);
  119. free(str);
  120. return 0;
  121. }
  122. static int scan_sysfs(lub_list_t *irqs)
  123. {
  124. DIR *dir;
  125. DIR *msi;
  126. struct dirent *dent;
  127. struct dirent *ment;
  128. FILE *fd;
  129. char path[PATH_MAX];
  130. int num;
  131. /* Now we can parse PCI devices only */
  132. /* Get info from /sys/bus/pci/devices */
  133. dir = opendir(SYSFS_PCI_PATH);
  134. if (!dir)
  135. return -1;
  136. while((dent = readdir(dir))) {
  137. if (!strcmp(dent->d_name, ".") ||
  138. !strcmp(dent->d_name, ".."))
  139. continue;
  140. /* Search for MSI IRQs. Since linux-3.2 */
  141. sprintf(path, "%s/%s/msi_irqs", SYSFS_PCI_PATH, dent->d_name);
  142. if ((msi = opendir(path))) {
  143. while((ment = readdir(msi))) {
  144. if (!strcmp(ment->d_name, ".") ||
  145. !strcmp(ment->d_name, ".."))
  146. continue;
  147. num = strtol(ment->d_name, NULL, 10);
  148. if (!num)
  149. continue;
  150. parse_local_cpus(irqs, dent->d_name, num);
  151. }
  152. closedir(msi);
  153. continue;
  154. }
  155. /* Try to get IRQ number from irq file */
  156. sprintf(path, "%s/%s/irq", SYSFS_PCI_PATH, dent->d_name);
  157. if (!(fd = fopen(path, "r")))
  158. continue;
  159. if (fscanf(fd, "%d", &num) < 0) {
  160. fclose(fd);
  161. continue;
  162. }
  163. fclose(fd);
  164. if (!num)
  165. continue;
  166. parse_local_cpus(irqs, dent->d_name, num);
  167. }
  168. closedir(dir);
  169. return 0;
  170. }
  171. /* Parse /proc/interrupts to get actual IRQ list */
  172. int irq_list_populate(lub_list_t *irqs)
  173. {
  174. FILE *fd;
  175. unsigned int num;
  176. char *str = NULL;
  177. size_t sz;
  178. irq_t *irq;
  179. lub_list_node_t *iter;
  180. if (!(fd = fopen(PROC_INTERRUPTS, "r")))
  181. return -1;
  182. while(getline(&str, &sz, fd) >= 0) {
  183. char *endptr, *tok;
  184. int new = 0;
  185. num = strtoul(str, &endptr, 10);
  186. if (endptr == str)
  187. continue;
  188. if (!(irq = irq_list_search(irqs, num))) {
  189. new = 1;
  190. irq = irq_list_add(irqs, num);
  191. }
  192. /* Find IRQ type - first non-digital and non-space */
  193. while (*endptr && !isalpha(*endptr))
  194. endptr++;
  195. tok = endptr; /* It will be IRQ type */
  196. while (*endptr && !isblank(*endptr))
  197. endptr++;
  198. free(irq->type);
  199. irq->type = strndup(tok, endptr - tok);
  200. /* Find IRQ devices list */
  201. while (*endptr && !isalpha(*endptr))
  202. endptr++;
  203. tok = endptr; /* It will be device list */
  204. while (*endptr && !iscntrl(*endptr))
  205. endptr++;
  206. free(irq->desc);
  207. irq->desc = strndup(tok, endptr - tok);
  208. /* Set refresh flag because IRQ was found */
  209. irq->refresh = 1;
  210. /* By default all CPUs are local for IRQ */
  211. cpus_setall(irq->local_cpus);
  212. if (new)
  213. printf("Add IRQ %3d %s\n", irq->irq, STR(irq->desc));
  214. }
  215. free(str);
  216. fclose(fd);
  217. /* Remove disapeared IRQs */
  218. iter = lub_list_iterator_init(irqs);
  219. while(iter) {
  220. irq_t *irq;
  221. lub_list_node_t *old_iter;
  222. irq = (irq_t *)lub_list_node__get_data(iter);
  223. old_iter = iter;
  224. iter = lub_list_iterator_next(iter);
  225. if (!irq->refresh) {
  226. lub_list_del(irqs, old_iter);
  227. irq_free(irq);
  228. printf("Remove IRQ %3d %s\n", irq->irq, STR(irq->desc));
  229. } else {
  230. /* Drop refresh flag for next iteration */
  231. irq->refresh = 0;
  232. }
  233. }
  234. /* Add IRQ info from sysfs */
  235. scan_sysfs(irqs);
  236. return 0;
  237. }