proc.c 9.6 KB

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