lpevents.c 8.4 KB

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