proc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * linux/kernel/irq/proc.c
  3. *
  4. * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
  5. *
  6. * This file contains the /proc/irq/ handling code.
  7. */
  8. #include <linux/irq.h>
  9. #include <linux/gfp.h>
  10. #include <linux/proc_fs.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/kernel_stat.h>
  14. #include "internals.h"
  15. static struct proc_dir_entry *root_irq_dir;
  16. #ifdef CONFIG_SMP
  17. static int show_irq_affinity(int type, struct seq_file *m, void *v)
  18. {
  19. struct irq_desc *desc = irq_to_desc((long)m->private);
  20. const struct cpumask *mask = desc->irq_data.affinity;
  21. #ifdef CONFIG_GENERIC_PENDING_IRQ
  22. if (irqd_is_setaffinity_pending(&desc->irq_data))
  23. mask = desc->pending_mask;
  24. #endif
  25. if (type)
  26. seq_cpumask_list(m, mask);
  27. else
  28. seq_cpumask(m, mask);
  29. seq_putc(m, '\n');
  30. return 0;
  31. }
  32. static int irq_affinity_hint_proc_show(struct seq_file *m, void *v)
  33. {
  34. struct irq_desc *desc = irq_to_desc((long)m->private);
  35. unsigned long flags;
  36. cpumask_var_t mask;
  37. if (!zalloc_cpumask_var(&mask, GFP_KERNEL))
  38. return -ENOMEM;
  39. raw_spin_lock_irqsave(&desc->lock, flags);
  40. if (desc->affinity_hint)
  41. cpumask_copy(mask, desc->affinity_hint);
  42. raw_spin_unlock_irqrestore(&desc->lock, flags);
  43. seq_cpumask(m, mask);
  44. seq_putc(m, '\n');
  45. free_cpumask_var(mask);
  46. return 0;
  47. }
  48. #ifndef is_affinity_mask_valid
  49. #define is_affinity_mask_valid(val) 1
  50. #endif
  51. int no_irq_affinity;
  52. static int irq_affinity_proc_show(struct seq_file *m, void *v)
  53. {
  54. return show_irq_affinity(0, m, v);
  55. }
  56. static int irq_affinity_list_proc_show(struct seq_file *m, void *v)
  57. {
  58. return show_irq_affinity(1, m, v);
  59. }
  60. static ssize_t write_irq_affinity(int type, struct file *file,
  61. const char __user *buffer, size_t count, loff_t *pos)
  62. {
  63. unsigned int irq = (int)(long)PDE(file->f_path.dentry->d_inode)->data;
  64. cpumask_var_t new_value;
  65. int err;
  66. if (!irq_can_set_affinity(irq) || no_irq_affinity)
  67. return -EIO;
  68. if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
  69. return -ENOMEM;
  70. if (type)
  71. err = cpumask_parselist_user(buffer, count, new_value);
  72. else
  73. err = cpumask_parse_user(buffer, count, new_value);
  74. if (err)
  75. goto free_cpumask;
  76. if (!is_affinity_mask_valid(new_value)) {
  77. err = -EINVAL;
  78. goto free_cpumask;
  79. }
  80. /*
  81. * Do not allow disabling IRQs completely - it's a too easy
  82. * way to make the system unusable accidentally :-) At least
  83. * one online CPU still has to be targeted.
  84. */
  85. if (!cpumask_intersects(new_value, cpu_online_mask)) {
  86. /* Special case for empty set - allow the architecture
  87. code to set default SMP affinity. */
  88. err = irq_select_affinity_usr(irq, new_value) ? -EINVAL : count;
  89. } else {
  90. irq_set_affinity(irq, new_value);
  91. err = count;
  92. }
  93. free_cpumask:
  94. free_cpumask_var(new_value);
  95. return err;
  96. }
  97. static ssize_t irq_affinity_proc_write(struct file *file,
  98. const char __user *buffer, size_t count, loff_t *pos)
  99. {
  100. return write_irq_affinity(0, file, buffer, count, pos);
  101. }
  102. static ssize_t irq_affinity_list_proc_write(struct file *file,
  103. const char __user *buffer, size_t count, loff_t *pos)
  104. {
  105. return write_irq_affinity(1, file, buffer, count, pos);
  106. }
  107. static int irq_affinity_proc_open(struct inode *inode, struct file *file)
  108. {
  109. return single_open(file, irq_affinity_proc_show, PDE(inode)->data);
  110. }
  111. static int irq_affinity_list_proc_open(struct inode *inode, struct file *file)
  112. {
  113. return single_open(file, irq_affinity_list_proc_show, PDE(inode)->data);
  114. }
  115. static int irq_affinity_hint_proc_open(struct inode *inode, struct file *file)
  116. {
  117. return single_open(file, irq_affinity_hint_proc_show, PDE(inode)->data);
  118. }
  119. static const struct file_operations irq_affinity_proc_fops = {
  120. .open = irq_affinity_proc_open,
  121. .read = seq_read,
  122. .llseek = seq_lseek,
  123. .release = single_release,
  124. .write = irq_affinity_proc_write,
  125. };
  126. static const struct file_operations irq_affinity_hint_proc_fops = {
  127. .open = irq_affinity_hint_proc_open,
  128. .read = seq_read,
  129. .llseek = seq_lseek,
  130. .release = single_release,
  131. };
  132. static const struct file_operations irq_affinity_list_proc_fops = {
  133. .open = irq_affinity_list_proc_open,
  134. .read = seq_read,
  135. .llseek = seq_lseek,
  136. .release = single_release,
  137. .write = irq_affinity_list_proc_write,
  138. };
  139. static int default_affinity_show(struct seq_file *m, void *v)
  140. {
  141. seq_cpumask(m, irq_default_affinity);
  142. seq_putc(m, '\n');
  143. return 0;
  144. }
  145. static ssize_t default_affinity_write(struct file *file,
  146. const char __user *buffer, size_t count, loff_t *ppos)
  147. {
  148. cpumask_var_t new_value;
  149. int err;
  150. if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
  151. return -ENOMEM;
  152. err = cpumask_parse_user(buffer, count, new_value);
  153. if (err)
  154. goto out;
  155. if (!is_affinity_mask_valid(new_value)) {
  156. err = -EINVAL;
  157. goto out;
  158. }
  159. /*
  160. * Do not allow disabling IRQs completely - it's a too easy
  161. * way to make the system unusable accidentally :-) At least
  162. * one online CPU still has to be targeted.
  163. */
  164. if (!cpumask_intersects(new_value, cpu_online_mask)) {
  165. err = -EINVAL;
  166. goto out;
  167. }
  168. cpumask_copy(irq_default_affinity, new_value);
  169. err = count;
  170. out:
  171. free_cpumask_var(new_value);
  172. return err;
  173. }
  174. static int default_affinity_open(struct inode *inode, struct file *file)
  175. {
  176. return single_open(file, default_affinity_show, PDE(inode)->data);
  177. }
  178. static const struct file_operations default_affinity_proc_fops = {
  179. .open = default_affinity_open,
  180. .read = seq_read,
  181. .llseek = seq_lseek,
  182. .release = single_release,
  183. .write = default_affinity_write,
  184. };
  185. static int irq_node_proc_show(struct seq_file *m, void *v)
  186. {
  187. struct irq_desc *desc = irq_to_desc((long) m->private);
  188. seq_printf(m, "%d\n", desc->irq_data.node);
  189. return 0;
  190. }
  191. static int irq_node_proc_open(struct inode *inode, struct file *file)
  192. {
  193. return single_open(file, irq_node_proc_show, PDE(inode)->data);
  194. }
  195. static const struct file_operations irq_node_proc_fops = {
  196. .open = irq_node_proc_open,
  197. .read = seq_read,
  198. .llseek = seq_lseek,
  199. .release = single_release,
  200. };
  201. #endif
  202. static int irq_spurious_proc_show(struct seq_file *m, void *v)
  203. {
  204. struct irq_desc *desc = irq_to_desc((long) m->private);
  205. seq_printf(m, "count %u\n" "unhandled %u\n" "last_unhandled %u ms\n",
  206. desc->irq_count, desc->irqs_unhandled,
  207. jiffies_to_msecs(desc->last_unhandled));
  208. return 0;
  209. }
  210. static int irq_spurious_proc_open(struct inode *inode, struct file *file)
  211. {
  212. return single_open(file, irq_spurious_proc_show, PDE(inode)->data);
  213. }
  214. static const struct file_operations irq_spurious_proc_fops = {
  215. .open = irq_spurious_proc_open,
  216. .read = seq_read,
  217. .llseek = seq_lseek,
  218. .release = single_release,
  219. };
  220. #define MAX_NAMELEN 128
  221. static int name_unique(unsigned int irq, struct irqaction *new_action)
  222. {
  223. struct irq_desc *desc = irq_to_desc(irq);
  224. struct irqaction *action;
  225. unsigned long flags;
  226. int ret = 1;
  227. raw_spin_lock_irqsave(&desc->lock, flags);
  228. for (action = desc->action ; action; action = action->next) {
  229. if ((action != new_action) && action->name &&
  230. !strcmp(new_action->name, action->name)) {
  231. ret = 0;
  232. break;
  233. }
  234. }
  235. raw_spin_unlock_irqrestore(&desc->lock, flags);
  236. return ret;
  237. }
  238. void register_handler_proc(unsigned int irq, struct irqaction *action)
  239. {
  240. char name [MAX_NAMELEN];
  241. struct irq_desc *desc = irq_to_desc(irq);
  242. if (!desc->dir || action->dir || !action->name ||
  243. !name_unique(irq, action))
  244. return;
  245. memset(name, 0, MAX_NAMELEN);
  246. snprintf(name, MAX_NAMELEN, "%s", action->name);
  247. /* create /proc/irq/1234/handler/ */
  248. action->dir = proc_mkdir(name, desc->dir);
  249. }
  250. #undef MAX_NAMELEN
  251. #define MAX_NAMELEN 10
  252. void register_irq_proc(unsigned int irq, struct irq_desc *desc)
  253. {
  254. char name [MAX_NAMELEN];
  255. if (!root_irq_dir || (desc->irq_data.chip == &no_irq_chip) || desc->dir)
  256. return;
  257. memset(name, 0, MAX_NAMELEN);
  258. sprintf(name, "%d", irq);
  259. /* create /proc/irq/1234 */
  260. desc->dir = proc_mkdir(name, root_irq_dir);
  261. if (!desc->dir)
  262. return;
  263. #ifdef CONFIG_SMP
  264. /* create /proc/irq/<irq>/smp_affinity */
  265. proc_create_data("smp_affinity", 0600, desc->dir,
  266. &irq_affinity_proc_fops, (void *)(long)irq);
  267. /* create /proc/irq/<irq>/affinity_hint */
  268. proc_create_data("affinity_hint", 0400, desc->dir,
  269. &irq_affinity_hint_proc_fops, (void *)(long)irq);
  270. /* create /proc/irq/<irq>/smp_affinity_list */
  271. proc_create_data("smp_affinity_list", 0600, desc->dir,
  272. &irq_affinity_list_proc_fops, (void *)(long)irq);
  273. proc_create_data("node", 0444, desc->dir,
  274. &irq_node_proc_fops, (void *)(long)irq);
  275. #endif
  276. proc_create_data("spurious", 0444, desc->dir,
  277. &irq_spurious_proc_fops, (void *)(long)irq);
  278. }
  279. void unregister_irq_proc(unsigned int irq, struct irq_desc *desc)
  280. {
  281. char name [MAX_NAMELEN];
  282. if (!root_irq_dir || !desc->dir)
  283. return;
  284. #ifdef CONFIG_SMP
  285. remove_proc_entry("smp_affinity", desc->dir);
  286. remove_proc_entry("affinity_hint", desc->dir);
  287. remove_proc_entry("smp_affinity_list", desc->dir);
  288. remove_proc_entry("node", desc->dir);
  289. #endif
  290. remove_proc_entry("spurious", desc->dir);
  291. memset(name, 0, MAX_NAMELEN);
  292. sprintf(name, "%u", irq);
  293. remove_proc_entry(name, root_irq_dir);
  294. }
  295. #undef MAX_NAMELEN
  296. void unregister_handler_proc(unsigned int irq, struct irqaction *action)
  297. {
  298. if (action->dir) {
  299. struct irq_desc *desc = irq_to_desc(irq);
  300. remove_proc_entry(action->dir->name, desc->dir);
  301. }
  302. }
  303. static void register_default_affinity_proc(void)
  304. {
  305. #ifdef CONFIG_SMP
  306. proc_create("irq/default_smp_affinity", 0600, NULL,
  307. &default_affinity_proc_fops);
  308. #endif
  309. }
  310. void init_irq_proc(void)
  311. {
  312. unsigned int irq;
  313. struct irq_desc *desc;
  314. /* create /proc/irq */
  315. root_irq_dir = proc_mkdir("irq", NULL);
  316. if (!root_irq_dir)
  317. return;
  318. register_default_affinity_proc();
  319. /*
  320. * Create entries for all existing IRQs.
  321. */
  322. for_each_irq_desc(irq, desc) {
  323. if (!desc)
  324. continue;
  325. register_irq_proc(irq, desc);
  326. }
  327. }
  328. #ifdef CONFIG_GENERIC_IRQ_SHOW
  329. int __weak arch_show_interrupts(struct seq_file *p, int prec)
  330. {
  331. return 0;
  332. }
  333. #ifndef ACTUAL_NR_IRQS
  334. # define ACTUAL_NR_IRQS nr_irqs
  335. #endif
  336. int show_interrupts(struct seq_file *p, void *v)
  337. {
  338. static int prec;
  339. unsigned long flags, any_count = 0;
  340. int i = *(loff_t *) v, j;
  341. struct irqaction *action;
  342. struct irq_desc *desc;
  343. if (i > ACTUAL_NR_IRQS)
  344. return 0;
  345. if (i == ACTUAL_NR_IRQS)
  346. return arch_show_interrupts(p, prec);
  347. /* print header and calculate the width of the first column */
  348. if (i == 0) {
  349. for (prec = 3, j = 1000; prec < 10 && j <= nr_irqs; ++prec)
  350. j *= 10;
  351. seq_printf(p, "%*s", prec + 8, "");
  352. for_each_online_cpu(j)
  353. seq_printf(p, "CPU%-8d", j);
  354. seq_putc(p, '\n');
  355. }
  356. desc = irq_to_desc(i);
  357. if (!desc)
  358. return 0;
  359. raw_spin_lock_irqsave(&desc->lock, flags);
  360. for_each_online_cpu(j)
  361. any_count |= kstat_irqs_cpu(i, j);
  362. action = desc->action;
  363. if (!action && !any_count)
  364. goto out;
  365. seq_printf(p, "%*d: ", prec, i);
  366. for_each_online_cpu(j)
  367. seq_printf(p, "%10u ", kstat_irqs_cpu(i, j));
  368. if (desc->irq_data.chip) {
  369. if (desc->irq_data.chip->irq_print_chip)
  370. desc->irq_data.chip->irq_print_chip(&desc->irq_data, p);
  371. else if (desc->irq_data.chip->name)
  372. seq_printf(p, " %8s", desc->irq_data.chip->name);
  373. else
  374. seq_printf(p, " %8s", "-");
  375. } else {
  376. seq_printf(p, " %8s", "None");
  377. }
  378. #ifdef CONFIG_GENERIC_IRQ_SHOW_LEVEL
  379. seq_printf(p, " %-8s", irqd_is_level_type(&desc->irq_data) ? "Level" : "Edge");
  380. #endif
  381. if (desc->name)
  382. seq_printf(p, "-%-8s", desc->name);
  383. if (action) {
  384. seq_printf(p, " %s", action->name);
  385. while ((action = action->next) != NULL)
  386. seq_printf(p, ", %s", action->name);
  387. }
  388. seq_putc(p, '\n');
  389. out:
  390. raw_spin_unlock_irqrestore(&desc->lock, flags);
  391. return 0;
  392. }
  393. #endif