proc.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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->irq_data.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 (!zalloc_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. raw_spin_unlock_irqrestore(&desc->lock, flags);
  39. seq_cpumask(m, mask);
  40. seq_putc(m, '\n');
  41. free_cpumask_var(mask);
  42. return 0;
  43. }
  44. #ifndef is_affinity_mask_valid
  45. #define is_affinity_mask_valid(val) 1
  46. #endif
  47. int no_irq_affinity;
  48. static ssize_t irq_affinity_proc_write(struct file *file,
  49. const char __user *buffer, size_t count, loff_t *pos)
  50. {
  51. unsigned int irq = (int)(long)PDE(file->f_path.dentry->d_inode)->data;
  52. cpumask_var_t new_value;
  53. int err;
  54. if (!irq_to_desc(irq)->irq_data.chip->irq_set_affinity || no_irq_affinity ||
  55. irq_balancing_disabled(irq))
  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) ? -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, NULL);
  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. #undef MAX_NAMELEN
  242. void unregister_handler_proc(unsigned int irq, struct irqaction *action)
  243. {
  244. if (action->dir) {
  245. struct irq_desc *desc = irq_to_desc(irq);
  246. remove_proc_entry(action->dir->name, desc->dir);
  247. }
  248. }
  249. static void register_default_affinity_proc(void)
  250. {
  251. #ifdef CONFIG_SMP
  252. proc_create("irq/default_smp_affinity", 0600, NULL,
  253. &default_affinity_proc_fops);
  254. #endif
  255. }
  256. void init_irq_proc(void)
  257. {
  258. unsigned int irq;
  259. struct irq_desc *desc;
  260. /* create /proc/irq */
  261. root_irq_dir = proc_mkdir("irq", NULL);
  262. if (!root_irq_dir)
  263. return;
  264. register_default_affinity_proc();
  265. /*
  266. * Create entries for all existing IRQs.
  267. */
  268. for_each_irq_desc(irq, desc) {
  269. if (!desc)
  270. continue;
  271. register_irq_proc(irq, desc);
  272. }
  273. }