irq_parse.c 5.5 KB

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