ItLpQueue.c 7.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * ItLpQueue.c
 * Copyright (C) 2001 Mike Corrigan  IBM Corporation
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <linux/stddef.h>
#include <linux/kernel.h>
#include <linux/sched.h>
14
#include <linux/bootmem.h>
15 16
#include <linux/seq_file.h>
#include <linux/proc_fs.h>
L
Linus Torvalds 已提交
17 18 19 20 21 22
#include <asm/system.h>
#include <asm/paca.h>
#include <asm/iSeries/ItLpQueue.h>
#include <asm/iSeries/HvLpEvent.h>
#include <asm/iSeries/HvCallEvent.h>

23 24 25 26 27 28
/*
 * The LpQueue is used to pass event data from the hypervisor to
 * the partition.  This is where I/O interrupt events are communicated.
 *
 * It is written to by the hypervisor so cannot end up in the BSS.
 */
29
struct hvlpevent_queue hvlpevent_queue __attribute__((__section__(".data")));
30

31 32 33 34 35 36 37 38 39 40 41 42
static char *event_types[9] = {
	"Hypervisor\t\t",
	"Machine Facilities\t",
	"Session Manager\t",
	"SPD I/O\t\t",
	"Virtual Bus\t\t",
	"PCI I/O\t\t",
	"RIO I/O\t\t",
	"Virtual Lan\t\t",
	"Virtual I/O\t\t"
};

43
static __inline__ int set_inUse(void)
L
Linus Torvalds 已提交
44 45
{
	int t;
46
	u32 * inUseP = &hvlpevent_queue.xInUseWord;
L
Linus Torvalds 已提交
47 48 49 50 51 52 53 54 55 56

	__asm__ __volatile__("\n\
1:	lwarx	%0,0,%2		\n\
	cmpwi	0,%0,0		\n\
	li	%0,0		\n\
	bne-	2f		\n\
	addi	%0,%0,1		\n\
	stwcx.	%0,0,%2		\n\
	bne-	1b		\n\
2:	eieio"
57 58
	: "=&r" (t), "=m" (hvlpevent_queue.xInUseWord)
	: "r" (inUseP), "m" (hvlpevent_queue.xInUseWord)
L
Linus Torvalds 已提交
59 60 61 62 63
	: "cc");

	return t;
}

64
static __inline__ void clear_inUse(void)
L
Linus Torvalds 已提交
65
{
66
	hvlpevent_queue.xInUseWord = 0;
L
Linus Torvalds 已提交
67 68 69 70 71 72
}

/* Array of LpEvent handler functions */
extern LpEventHandler lpEventHandler[HvLpEvent_Type_NumTypes];
unsigned long ItLpQueueInProcess = 0;

73
static struct HvLpEvent * ItLpQueue_getNextLpEvent(void)
L
Linus Torvalds 已提交
74 75
{
	struct HvLpEvent * nextLpEvent = 
76
		(struct HvLpEvent *)hvlpevent_queue.xSlicCurEventPtr;
L
Linus Torvalds 已提交
77 78 79 80
	if ( nextLpEvent->xFlags.xValid ) {
		/* rmb() needed only for weakly consistent machines (regatta) */
		rmb();
		/* Set pointer to next potential event */
81
		hvlpevent_queue.xSlicCurEventPtr += ((nextLpEvent->xSizeMinus1 +
L
Linus Torvalds 已提交
82 83 84 85
				      LpEventAlign ) /
				      LpEventAlign ) *
				      LpEventAlign;
		/* Wrap to beginning if no room at end */
86 87
		if (hvlpevent_queue.xSlicCurEventPtr > hvlpevent_queue.xSlicLastValidEventPtr)
			hvlpevent_queue.xSlicCurEventPtr = hvlpevent_queue.xSlicEventStackPtr;
L
Linus Torvalds 已提交
88 89 90 91 92 93 94
	}
	else 
		nextLpEvent = NULL;

	return nextLpEvent;
}

95
static unsigned long spread_lpevents = NR_CPUS;
96

97
int ItLpQueue_isLpIntPending(void)
L
Linus Torvalds 已提交
98
{
99 100 101 102 103
	struct HvLpEvent *next_event;

	if (smp_processor_id() >= spread_lpevents)
		return 0;

104 105
	next_event = (struct HvLpEvent *)hvlpevent_queue.xSlicCurEventPtr;
	return next_event->xFlags.xValid | hvlpevent_queue.xPlicOverflowIntPending;
L
Linus Torvalds 已提交
106 107
}

108
static void ItLpQueue_clearValid( struct HvLpEvent * event )
L
Linus Torvalds 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
{
	/* Clear the valid bit of the event
	 * Also clear bits within this event that might
	 * look like valid bits (on 64-byte boundaries)
   	 */
	unsigned extra = (( event->xSizeMinus1 + LpEventAlign ) /
						 LpEventAlign ) - 1;
	switch ( extra ) {
	  case 3:
	   ((struct HvLpEvent*)((char*)event+3*LpEventAlign))->xFlags.xValid=0;
	  case 2:
	   ((struct HvLpEvent*)((char*)event+2*LpEventAlign))->xFlags.xValid=0;
	  case 1:
	   ((struct HvLpEvent*)((char*)event+1*LpEventAlign))->xFlags.xValid=0;
	  case 0:
	   ;	
	}
	mb();
	event->xFlags.xValid = 0;
}

130
unsigned ItLpQueue_process(struct pt_regs *regs)
L
Linus Torvalds 已提交
131 132 133 134 135
{
	unsigned numIntsProcessed = 0;
	struct HvLpEvent * nextLpEvent;

	/* If we have recursed, just return */
136
	if ( !set_inUse() )
L
Linus Torvalds 已提交
137 138 139 140 141 142 143 144
		return 0;
	
	if (ItLpQueueInProcess == 0)
		ItLpQueueInProcess = 1;
	else
		BUG();

	for (;;) {
145
		nextLpEvent = ItLpQueue_getNextLpEvent();
L
Linus Torvalds 已提交
146 147
		if ( nextLpEvent ) {
			/* Count events to return to caller
148
			 * and count processed events in hvlpevent_queue
L
Linus Torvalds 已提交
149 150
 			 */
			++numIntsProcessed;
151
			hvlpevent_queue.xLpIntCount++;
L
Linus Torvalds 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165
			/* Call appropriate handler here, passing 
			 * a pointer to the LpEvent.  The handler
			 * must make a copy of the LpEvent if it
			 * needs it in a bottom half. (perhaps for
			 * an ACK)
			 *	
			 *  Handlers are responsible for ACK processing 
			 *
			 * The Hypervisor guarantees that LpEvents will
			 * only be delivered with types that we have
			 * registered for, so no type check is necessary
			 * here!
  			 */
			if ( nextLpEvent->xType < HvLpEvent_Type_NumTypes )
166
				hvlpevent_queue.xLpIntCountByType[nextLpEvent->xType]++;
L
Linus Torvalds 已提交
167 168 169 170 171 172 173
			if ( nextLpEvent->xType < HvLpEvent_Type_NumTypes &&
			     lpEventHandler[nextLpEvent->xType] ) 
				lpEventHandler[nextLpEvent->xType](nextLpEvent, regs);
			else
				printk(KERN_INFO "Unexpected Lp Event type=%d\n", nextLpEvent->xType );
			
			ItLpQueue_clearValid( nextLpEvent );
174
		} else if ( hvlpevent_queue.xPlicOverflowIntPending )
L
Linus Torvalds 已提交
175 176 177 178
			/*
			 * No more valid events. If overflow events are
			 * pending process them
			 */
179
			HvCallEvent_getOverflowLpEvents( hvlpevent_queue.xIndex);
L
Linus Torvalds 已提交
180 181 182 183 184 185
		else
			break;
	}

	ItLpQueueInProcess = 0;
	mb();
186
	clear_inUse();
L
Linus Torvalds 已提交
187 188 189 190 191

	get_paca()->lpevent_count += numIntsProcessed;

	return numIntsProcessed;
}
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

static int set_spread_lpevents(char *str)
{
	unsigned long val = simple_strtoul(str, NULL, 0);

	/*
	 * The parameter is the number of processors to share in processing
	 * lp events.
	 */
	if (( val > 0) && (val <= NR_CPUS)) {
		spread_lpevents = val;
		printk("lpevent processing spread over %ld processors\n", val);
	} else {
		printk("invalid spread_lpevents %ld\n", val);
	}

	return 1;
}
__setup("spread_lpevents=", set_spread_lpevents);

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
void setup_hvlpevent_queue(void)
{
	void *eventStack;

	/*
	 * Allocate a page for the Event Stack. The Hypervisor needs the
	 * absolute real address, so we subtract out the KERNELBASE and add
	 * in the absolute real address of the kernel load area.
	 */
	eventStack = alloc_bootmem_pages(LpEventStackSize);
	memset(eventStack, 0, LpEventStackSize);

	/* Invoke the hypervisor to initialize the event stack */
	HvCallEvent_setLpEventStack(0, eventStack, LpEventStackSize);

227 228 229
	hvlpevent_queue.xSlicEventStackPtr = (char *)eventStack;
	hvlpevent_queue.xSlicCurEventPtr = (char *)eventStack;
	hvlpevent_queue.xSlicLastValidEventPtr = (char *)eventStack +
230
					(LpEventStackSize - LpEventMaxSize);
231
	hvlpevent_queue.xIndex = 0;
232
}
233 234 235 236 237 238 239

static int proc_lpevents_show(struct seq_file *m, void *v)
{
	unsigned int i;

	seq_printf(m, "LpEventQueue 0\n");
	seq_printf(m, "  events processed:\t%lu\n",
240
		   (unsigned long)hvlpevent_queue.xLpIntCount);
241 242 243

	for (i = 0; i < 9; ++i)
		seq_printf(m, "    %s %10lu\n", event_types[i],
244
			   (unsigned long)hvlpevent_queue.xLpIntCountByType[i]);
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

	seq_printf(m, "\n  events processed by processor:\n");

	for_each_online_cpu(i)
		seq_printf(m, "    CPU%02d  %10u\n", i, paca[i].lpevent_count);

	return 0;
}

static int proc_lpevents_open(struct inode *inode, struct file *file)
{
	return single_open(file, proc_lpevents_show, NULL);
}

static struct file_operations proc_lpevents_operations = {
	.open		= proc_lpevents_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

static int __init proc_lpevents_init(void)
{
	struct proc_dir_entry *e;

	e = create_proc_entry("iSeries/lpevents", S_IFREG|S_IRUGO, NULL);
	if (e)
		e->proc_fops = &proc_lpevents_operations;

	return 0;
}
__initcall(proc_lpevents_init);