lpevents.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Copyright (C) 2001 Mike Corrigan IBM Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. */
  9. #include <linux/stddef.h>
  10. #include <linux/kernel.h>
  11. #include <linux/sched.h>
  12. #include <linux/bootmem.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/proc_fs.h>
  15. #include <linux/module.h>
  16. #include <asm/system.h>
  17. #include <asm/paca.h>
  18. #include <asm/firmware.h>
  19. #include <asm/iseries/it_lp_queue.h>
  20. #include <asm/iseries/hv_lp_event.h>
  21. #include <asm/iseries/hv_call_event.h>
  22. #include "it_lp_naca.h"
  23. /*
  24. * The LpQueue is used to pass event data from the hypervisor to
  25. * the partition. This is where I/O interrupt events are communicated.
  26. *
  27. * It is written to by the hypervisor so cannot end up in the BSS.
  28. */
  29. struct hvlpevent_queue hvlpevent_queue __attribute__((__section__(".data")));
  30. DEFINE_PER_CPU(unsigned long[HvLpEvent_Type_NumTypes], hvlpevent_counts);
  31. static char *event_types[HvLpEvent_Type_NumTypes] = {
  32. "Hypervisor",
  33. "Machine Facilities",
  34. "Session Manager",
  35. "SPD I/O",
  36. "Virtual Bus",
  37. "PCI I/O",
  38. "RIO I/O",
  39. "Virtual Lan",
  40. "Virtual I/O"
  41. };
  42. /* Array of LpEvent handler functions */
  43. static LpEventHandler lpEventHandler[HvLpEvent_Type_NumTypes];
  44. static unsigned lpEventHandlerPaths[HvLpEvent_Type_NumTypes];
  45. static struct HvLpEvent * get_next_hvlpevent(void)
  46. {
  47. struct HvLpEvent * event;
  48. event = (struct HvLpEvent *)hvlpevent_queue.hq_current_event;
  49. if (hvlpevent_is_valid(event)) {
  50. /* rmb() needed only for weakly consistent machines (regatta) */
  51. rmb();
  52. /* Set pointer to next potential event */
  53. hvlpevent_queue.hq_current_event += ((event->xSizeMinus1 +
  54. IT_LP_EVENT_ALIGN) / IT_LP_EVENT_ALIGN) *
  55. IT_LP_EVENT_ALIGN;
  56. /* Wrap to beginning if no room at end */
  57. if (hvlpevent_queue.hq_current_event >
  58. hvlpevent_queue.hq_last_event) {
  59. hvlpevent_queue.hq_current_event =
  60. hvlpevent_queue.hq_event_stack;
  61. }
  62. } else {
  63. event = NULL;
  64. }
  65. return event;
  66. }
  67. static unsigned long spread_lpevents = NR_CPUS;
  68. int hvlpevent_is_pending(void)
  69. {
  70. struct HvLpEvent *next_event;
  71. if (smp_processor_id() >= spread_lpevents)
  72. return 0;
  73. next_event = (struct HvLpEvent *)hvlpevent_queue.hq_current_event;
  74. return hvlpevent_is_valid(next_event) ||
  75. hvlpevent_queue.hq_overflow_pending;
  76. }
  77. static void hvlpevent_clear_valid(struct HvLpEvent * event)
  78. {
  79. /* Tell the Hypervisor that we're done with this event.
  80. * Also clear bits within this event that might look like valid bits.
  81. * ie. on 64-byte boundaries.
  82. */
  83. struct HvLpEvent *tmp;
  84. unsigned extra = ((event->xSizeMinus1 + IT_LP_EVENT_ALIGN) /
  85. IT_LP_EVENT_ALIGN) - 1;
  86. switch (extra) {
  87. case 3:
  88. tmp = (struct HvLpEvent*)((char*)event + 3 * IT_LP_EVENT_ALIGN);
  89. hvlpevent_invalidate(tmp);
  90. case 2:
  91. tmp = (struct HvLpEvent*)((char*)event + 2 * IT_LP_EVENT_ALIGN);
  92. hvlpevent_invalidate(tmp);
  93. case 1:
  94. tmp = (struct HvLpEvent*)((char*)event + 1 * IT_LP_EVENT_ALIGN);
  95. hvlpevent_invalidate(tmp);
  96. }
  97. mb();
  98. hvlpevent_invalidate(event);
  99. }
  100. void process_hvlpevents(void)
  101. {
  102. struct HvLpEvent * event;
  103. restart:
  104. /* If we have recursed, just return */
  105. if (!spin_trylock(&hvlpevent_queue.hq_lock))
  106. return;
  107. for (;;) {
  108. event = get_next_hvlpevent();
  109. if (event) {
  110. /* Call appropriate handler here, passing
  111. * a pointer to the LpEvent. The handler
  112. * must make a copy of the LpEvent if it
  113. * needs it in a bottom half. (perhaps for
  114. * an ACK)
  115. *
  116. * Handlers are responsible for ACK processing
  117. *
  118. * The Hypervisor guarantees that LpEvents will
  119. * only be delivered with types that we have
  120. * registered for, so no type check is necessary
  121. * here!
  122. */
  123. if (event->xType < HvLpEvent_Type_NumTypes)
  124. __get_cpu_var(hvlpevent_counts)[event->xType]++;
  125. if (event->xType < HvLpEvent_Type_NumTypes &&
  126. lpEventHandler[event->xType])
  127. lpEventHandler[event->xType](event);
  128. else {
  129. u8 type = event->xType;
  130. /*
  131. * Don't printk in the spinlock as printk
  132. * may require ack events form the HV to send
  133. * any characters there.
  134. */
  135. hvlpevent_clear_valid(event);
  136. spin_unlock(&hvlpevent_queue.hq_lock);
  137. printk(KERN_INFO
  138. "Unexpected Lp Event type=%d\n", type);
  139. goto restart;
  140. }
  141. hvlpevent_clear_valid(event);
  142. } else if (hvlpevent_queue.hq_overflow_pending)
  143. /*
  144. * No more valid events. If overflow events are
  145. * pending process them
  146. */
  147. HvCallEvent_getOverflowLpEvents(hvlpevent_queue.hq_index);
  148. else
  149. break;
  150. }
  151. spin_unlock(&hvlpevent_queue.hq_lock);
  152. }
  153. static int set_spread_lpevents(char *str)
  154. {
  155. unsigned long val = simple_strtoul(str, NULL, 0);
  156. /*
  157. * The parameter is the number of processors to share in processing
  158. * lp events.
  159. */
  160. if (( val > 0) && (val <= NR_CPUS)) {
  161. spread_lpevents = val;
  162. printk("lpevent processing spread over %ld processors\n", val);
  163. } else {
  164. printk("invalid spread_lpevents %ld\n", val);
  165. }
  166. return 1;
  167. }
  168. __setup("spread_lpevents=", set_spread_lpevents);
  169. void __init setup_hvlpevent_queue(void)
  170. {
  171. void *eventStack;
  172. spin_lock_init(&hvlpevent_queue.hq_lock);
  173. /* Allocate a page for the Event Stack. */
  174. eventStack = alloc_bootmem_pages(IT_LP_EVENT_STACK_SIZE);
  175. memset(eventStack, 0, IT_LP_EVENT_STACK_SIZE);
  176. /* Invoke the hypervisor to initialize the event stack */
  177. HvCallEvent_setLpEventStack(0, eventStack, IT_LP_EVENT_STACK_SIZE);
  178. hvlpevent_queue.hq_event_stack = eventStack;
  179. hvlpevent_queue.hq_current_event = eventStack;
  180. hvlpevent_queue.hq_last_event = (char *)eventStack +
  181. (IT_LP_EVENT_STACK_SIZE - IT_LP_EVENT_MAX_SIZE);
  182. hvlpevent_queue.hq_index = 0;
  183. }
  184. /* Register a handler for an LpEvent type */
  185. int HvLpEvent_registerHandler(HvLpEvent_Type eventType, LpEventHandler handler)
  186. {
  187. if (eventType < HvLpEvent_Type_NumTypes) {
  188. lpEventHandler[eventType] = handler;
  189. return 0;
  190. }
  191. return 1;
  192. }
  193. EXPORT_SYMBOL(HvLpEvent_registerHandler);
  194. int HvLpEvent_unregisterHandler(HvLpEvent_Type eventType)
  195. {
  196. might_sleep();
  197. if (eventType < HvLpEvent_Type_NumTypes) {
  198. if (!lpEventHandlerPaths[eventType]) {
  199. lpEventHandler[eventType] = NULL;
  200. /*
  201. * We now sleep until all other CPUs have scheduled.
  202. * This ensures that the deletion is seen by all
  203. * other CPUs, and that the deleted handler isn't
  204. * still running on another CPU when we return.
  205. */
  206. synchronize_sched();
  207. return 0;
  208. }
  209. }
  210. return 1;
  211. }
  212. EXPORT_SYMBOL(HvLpEvent_unregisterHandler);
  213. /*
  214. * lpIndex is the partition index of the target partition.
  215. * needed only for VirtualIo, VirtualLan and SessionMgr. Zero
  216. * indicates to use our partition index - for the other types.
  217. */
  218. int HvLpEvent_openPath(HvLpEvent_Type eventType, HvLpIndex lpIndex)
  219. {
  220. if ((eventType < HvLpEvent_Type_NumTypes) &&
  221. lpEventHandler[eventType]) {
  222. if (lpIndex == 0)
  223. lpIndex = itLpNaca.xLpIndex;
  224. HvCallEvent_openLpEventPath(lpIndex, eventType);
  225. ++lpEventHandlerPaths[eventType];
  226. return 0;
  227. }
  228. return 1;
  229. }
  230. int HvLpEvent_closePath(HvLpEvent_Type eventType, HvLpIndex lpIndex)
  231. {
  232. if ((eventType < HvLpEvent_Type_NumTypes) &&
  233. lpEventHandler[eventType] &&
  234. lpEventHandlerPaths[eventType]) {
  235. if (lpIndex == 0)
  236. lpIndex = itLpNaca.xLpIndex;
  237. HvCallEvent_closeLpEventPath(lpIndex, eventType);
  238. --lpEventHandlerPaths[eventType];
  239. return 0;
  240. }
  241. return 1;
  242. }
  243. static int proc_lpevents_show(struct seq_file *m, void *v)
  244. {
  245. int cpu, i;
  246. unsigned long sum;
  247. static unsigned long cpu_totals[NR_CPUS];
  248. /* FIXME: do we care that there's no locking here? */
  249. sum = 0;
  250. for_each_online_cpu(cpu) {
  251. cpu_totals[cpu] = 0;
  252. for (i = 0; i < HvLpEvent_Type_NumTypes; i++) {
  253. cpu_totals[cpu] += per_cpu(hvlpevent_counts, cpu)[i];
  254. }
  255. sum += cpu_totals[cpu];
  256. }
  257. seq_printf(m, "LpEventQueue 0\n");
  258. seq_printf(m, " events processed:\t%lu\n", sum);
  259. for (i = 0; i < HvLpEvent_Type_NumTypes; ++i) {
  260. sum = 0;
  261. for_each_online_cpu(cpu) {
  262. sum += per_cpu(hvlpevent_counts, cpu)[i];
  263. }
  264. seq_printf(m, " %-20s %10lu\n", event_types[i], sum);
  265. }
  266. seq_printf(m, "\n events processed by processor:\n");
  267. for_each_online_cpu(cpu) {
  268. seq_printf(m, " CPU%02d %10lu\n", cpu, cpu_totals[cpu]);
  269. }
  270. return 0;
  271. }
  272. static int proc_lpevents_open(struct inode *inode, struct file *file)
  273. {
  274. return single_open(file, proc_lpevents_show, NULL);
  275. }
  276. static const struct file_operations proc_lpevents_operations = {
  277. .open = proc_lpevents_open,
  278. .read = seq_read,
  279. .llseek = seq_lseek,
  280. .release = single_release,
  281. };
  282. static int __init proc_lpevents_init(void)
  283. {
  284. if (!firmware_has_feature(FW_FEATURE_ISERIES))
  285. return 0;
  286. proc_create("iSeries/lpevents", S_IFREG|S_IRUGO, NULL,
  287. &proc_lpevents_operations);
  288. return 0;
  289. }
  290. __initcall(proc_lpevents_init);