proc.c 7.8 KB

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