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