1 /*
2  * ePAPR hcall interface
3  *
4  * Copyright 2008-2011 Freescale Semiconductor, Inc.
5  *
6  * Author: Timur Tabi <timur@freescale.com>
7  *
8  * This file is provided under a dual BSD/GPL license.  When using or
9  * redistributing this file, you may do so under either license.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are met:
13  *     * Redistributions of source code must retain the above copyright
14  *       notice, this list of conditions and the following disclaimer.
15  *     * Redistributions in binary form must reproduce the above copyright
16  *       notice, this list of conditions and the following disclaimer in the
17  *       documentation and/or other materials provided with the distribution.
18  *     * Neither the name of Freescale Semiconductor nor the
19  *       names of its contributors may be used to endorse or promote products
20  *       derived from this software without specific prior written permission.
21  *
22  *
23  * ALTERNATIVELY, this software may be distributed under the terms of the
24  * GNU General Public License ("GPL") as published by the Free Software
25  * Foundation, either version 2 of that License or (at your option) any
26  * later version.
27  *
28  * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
29  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31  * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
32  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /* A "hypercall" is an "sc 1" instruction.  This header file file provides C
41  * wrapper functions for the ePAPR hypervisor interface.  It is inteded
42  * for use by Linux device drivers and other operating systems.
43  *
44  * The hypercalls are implemented as inline assembly, rather than assembly
45  * language functions in a .S file, for optimization.  It allows
46  * the caller to issue the hypercall instruction directly, improving both
47  * performance and memory footprint.
48  */
49 
50 #ifndef _EPAPR_HCALLS_H
51 #define _EPAPR_HCALLS_H
52 
53 #include <linux/types.h>
54 #include <linux/errno.h>
55 #include <asm/byteorder.h>
56 
57 #define EV_BYTE_CHANNEL_SEND		1
58 #define EV_BYTE_CHANNEL_RECEIVE		2
59 #define EV_BYTE_CHANNEL_POLL		3
60 #define EV_INT_SET_CONFIG		4
61 #define EV_INT_GET_CONFIG		5
62 #define EV_INT_SET_MASK			6
63 #define EV_INT_GET_MASK			7
64 #define EV_INT_IACK			9
65 #define EV_INT_EOI			10
66 #define EV_INT_SEND_IPI			11
67 #define EV_INT_SET_TASK_PRIORITY	12
68 #define EV_INT_GET_TASK_PRIORITY	13
69 #define EV_DOORBELL_SEND		14
70 #define EV_MSGSND			15
71 #define EV_IDLE				16
72 
73 /* vendor ID: epapr */
74 #define EV_LOCAL_VENDOR_ID		0	/* for private use */
75 #define EV_EPAPR_VENDOR_ID		1
76 #define EV_FSL_VENDOR_ID		2	/* Freescale Semiconductor */
77 #define EV_IBM_VENDOR_ID		3	/* IBM */
78 #define EV_GHS_VENDOR_ID		4	/* Green Hills Software */
79 #define EV_ENEA_VENDOR_ID		5	/* Enea */
80 #define EV_WR_VENDOR_ID			6	/* Wind River Systems */
81 #define EV_AMCC_VENDOR_ID		7	/* Applied Micro Circuits */
82 #define EV_KVM_VENDOR_ID		42	/* KVM */
83 
84 /* The max number of bytes that a byte channel can send or receive per call */
85 #define EV_BYTE_CHANNEL_MAX_BYTES	16
86 
87 
88 #define _EV_HCALL_TOKEN(id, num) (((id) << 16) | (num))
89 #define EV_HCALL_TOKEN(hcall_num) _EV_HCALL_TOKEN(EV_EPAPR_VENDOR_ID, hcall_num)
90 
91 /* epapr error codes */
92 #define EV_EPERM		1	/* Operation not permitted */
93 #define EV_ENOENT		2	/*  Entry Not Found */
94 #define EV_EIO			3	/* I/O error occured */
95 #define EV_EAGAIN		4	/* The operation had insufficient
96 					 * resources to complete and should be
97 					 * retried
98 					 */
99 #define EV_ENOMEM		5	/* There was insufficient memory to
100 					 * complete the operation */
101 #define EV_EFAULT		6	/* Bad guest address */
102 #define EV_ENODEV		7	/* No such device */
103 #define EV_EINVAL		8	/* An argument supplied to the hcall
104 					   was out of range or invalid */
105 #define EV_INTERNAL		9	/* An internal error occured */
106 #define EV_CONFIG		10	/* A configuration error was detected */
107 #define EV_INVALID_STATE	11	/* The object is in an invalid state */
108 #define EV_UNIMPLEMENTED	12	/* Unimplemented hypercall */
109 #define EV_BUFFER_OVERFLOW	13	/* Caller-supplied buffer too small */
110 
111 /*
112  * Hypercall register clobber list
113  *
114  * These macros are used to define the list of clobbered registers during a
115  * hypercall.  Technically, registers r0 and r3-r12 are always clobbered,
116  * but the gcc inline assembly syntax does not allow us to specify registers
117  * on the clobber list that are also on the input/output list.  Therefore,
118  * the lists of clobbered registers depends on the number of register
119  * parmeters ("+r" and "=r") passed to the hypercall.
120  *
121  * Each assembly block should use one of the HCALL_CLOBBERSx macros.  As a
122  * general rule, 'x' is the number of parameters passed to the assembly
123  * block *except* for r11.
124  *
125  * If you're not sure, just use the smallest value of 'x' that does not
126  * generate a compilation error.  Because these are static inline functions,
127  * the compiler will only check the clobber list for a function if you
128  * compile code that calls that function.
129  *
130  * r3 and r11 are not included in any clobbers list because they are always
131  * listed as output registers.
132  *
133  * XER, CTR, and LR are currently listed as clobbers because it's uncertain
134  * whether they will be clobbered.
135  *
136  * Note that r11 can be used as an output parameter.
137 */
138 
139 /* List of common clobbered registers.  Do not use this macro. */
140 #define EV_HCALL_CLOBBERS "r0", "r12", "xer", "ctr", "lr", "cc"
141 
142 #define EV_HCALL_CLOBBERS8 EV_HCALL_CLOBBERS
143 #define EV_HCALL_CLOBBERS7 EV_HCALL_CLOBBERS8, "r10"
144 #define EV_HCALL_CLOBBERS6 EV_HCALL_CLOBBERS7, "r9"
145 #define EV_HCALL_CLOBBERS5 EV_HCALL_CLOBBERS6, "r8"
146 #define EV_HCALL_CLOBBERS4 EV_HCALL_CLOBBERS5, "r7"
147 #define EV_HCALL_CLOBBERS3 EV_HCALL_CLOBBERS4, "r6"
148 #define EV_HCALL_CLOBBERS2 EV_HCALL_CLOBBERS3, "r5"
149 #define EV_HCALL_CLOBBERS1 EV_HCALL_CLOBBERS2, "r4"
150 
151 
152 /*
153  * We use "uintptr_t" to define a register because it's guaranteed to be a
154  * 32-bit integer on a 32-bit platform, and a 64-bit integer on a 64-bit
155  * platform.
156  *
157  * All registers are either input/output or output only.  Registers that are
158  * initialized before making the hypercall are input/output.  All
159  * input/output registers are represented with "+r".  Output-only registers
160  * are represented with "=r".  Do not specify any unused registers.  The
161  * clobber list will tell the compiler that the hypercall modifies those
162  * registers, which is good enough.
163  */
164 
165 /**
166  * ev_int_set_config - configure the specified interrupt
167  * @interrupt: the interrupt number
168  * @config: configuration for this interrupt
169  * @priority: interrupt priority
170  * @destination: destination CPU number
171  *
172  * Returns 0 for success, or an error code.
173  */
ev_int_set_config(unsigned int interrupt,uint32_t config,unsigned int priority,uint32_t destination)174 static inline unsigned int ev_int_set_config(unsigned int interrupt,
175 	uint32_t config, unsigned int priority, uint32_t destination)
176 {
177 	register uintptr_t r11 __asm__("r11");
178 	register uintptr_t r3 __asm__("r3");
179 	register uintptr_t r4 __asm__("r4");
180 	register uintptr_t r5 __asm__("r5");
181 	register uintptr_t r6 __asm__("r6");
182 
183 	r11 = EV_HCALL_TOKEN(EV_INT_SET_CONFIG);
184 	r3  = interrupt;
185 	r4  = config;
186 	r5  = priority;
187 	r6  = destination;
188 
189 	__asm__ __volatile__ ("sc 1"
190 		: "+r" (r11), "+r" (r3), "+r" (r4), "+r" (r5), "+r" (r6)
191 		: : EV_HCALL_CLOBBERS4
192 	);
193 
194 	return r3;
195 }
196 
197 /**
198  * ev_int_get_config - return the config of the specified interrupt
199  * @interrupt: the interrupt number
200  * @config: returned configuration for this interrupt
201  * @priority: returned interrupt priority
202  * @destination: returned destination CPU number
203  *
204  * Returns 0 for success, or an error code.
205  */
ev_int_get_config(unsigned int interrupt,uint32_t * config,unsigned int * priority,uint32_t * destination)206 static inline unsigned int ev_int_get_config(unsigned int interrupt,
207 	uint32_t *config, unsigned int *priority, uint32_t *destination)
208 {
209 	register uintptr_t r11 __asm__("r11");
210 	register uintptr_t r3 __asm__("r3");
211 	register uintptr_t r4 __asm__("r4");
212 	register uintptr_t r5 __asm__("r5");
213 	register uintptr_t r6 __asm__("r6");
214 
215 	r11 = EV_HCALL_TOKEN(EV_INT_GET_CONFIG);
216 	r3 = interrupt;
217 
218 	__asm__ __volatile__ ("sc 1"
219 		: "+r" (r11), "+r" (r3), "=r" (r4), "=r" (r5), "=r" (r6)
220 		: : EV_HCALL_CLOBBERS4
221 	);
222 
223 	*config = r4;
224 	*priority = r5;
225 	*destination = r6;
226 
227 	return r3;
228 }
229 
230 /**
231  * ev_int_set_mask - sets the mask for the specified interrupt source
232  * @interrupt: the interrupt number
233  * @mask: 0=enable interrupts, 1=disable interrupts
234  *
235  * Returns 0 for success, or an error code.
236  */
ev_int_set_mask(unsigned int interrupt,unsigned int mask)237 static inline unsigned int ev_int_set_mask(unsigned int interrupt,
238 	unsigned int mask)
239 {
240 	register uintptr_t r11 __asm__("r11");
241 	register uintptr_t r3 __asm__("r3");
242 	register uintptr_t r4 __asm__("r4");
243 
244 	r11 = EV_HCALL_TOKEN(EV_INT_SET_MASK);
245 	r3 = interrupt;
246 	r4 = mask;
247 
248 	__asm__ __volatile__ ("sc 1"
249 		: "+r" (r11), "+r" (r3), "+r" (r4)
250 		: : EV_HCALL_CLOBBERS2
251 	);
252 
253 	return r3;
254 }
255 
256 /**
257  * ev_int_get_mask - returns the mask for the specified interrupt source
258  * @interrupt: the interrupt number
259  * @mask: returned mask for this interrupt (0=enabled, 1=disabled)
260  *
261  * Returns 0 for success, or an error code.
262  */
ev_int_get_mask(unsigned int interrupt,unsigned int * mask)263 static inline unsigned int ev_int_get_mask(unsigned int interrupt,
264 	unsigned int *mask)
265 {
266 	register uintptr_t r11 __asm__("r11");
267 	register uintptr_t r3 __asm__("r3");
268 	register uintptr_t r4 __asm__("r4");
269 
270 	r11 = EV_HCALL_TOKEN(EV_INT_GET_MASK);
271 	r3 = interrupt;
272 
273 	__asm__ __volatile__ ("sc 1"
274 		: "+r" (r11), "+r" (r3), "=r" (r4)
275 		: : EV_HCALL_CLOBBERS2
276 	);
277 
278 	*mask = r4;
279 
280 	return r3;
281 }
282 
283 /**
284  * ev_int_eoi - signal the end of interrupt processing
285  * @interrupt: the interrupt number
286  *
287  * This function signals the end of processing for the the specified
288  * interrupt, which must be the interrupt currently in service. By
289  * definition, this is also the highest-priority interrupt.
290  *
291  * Returns 0 for success, or an error code.
292  */
ev_int_eoi(unsigned int interrupt)293 static inline unsigned int ev_int_eoi(unsigned int interrupt)
294 {
295 	register uintptr_t r11 __asm__("r11");
296 	register uintptr_t r3 __asm__("r3");
297 
298 	r11 = EV_HCALL_TOKEN(EV_INT_EOI);
299 	r3 = interrupt;
300 
301 	__asm__ __volatile__ ("sc 1"
302 		: "+r" (r11), "+r" (r3)
303 		: : EV_HCALL_CLOBBERS1
304 	);
305 
306 	return r3;
307 }
308 
309 /**
310  * ev_byte_channel_send - send characters to a byte stream
311  * @handle: byte stream handle
312  * @count: (input) num of chars to send, (output) num chars sent
313  * @buffer: pointer to a 16-byte buffer
314  *
315  * @buffer must be at least 16 bytes long, because all 16 bytes will be
316  * read from memory into registers, even if count < 16.
317  *
318  * Returns 0 for success, or an error code.
319  */
ev_byte_channel_send(unsigned int handle,unsigned int * count,const char buffer[EV_BYTE_CHANNEL_MAX_BYTES])320 static inline unsigned int ev_byte_channel_send(unsigned int handle,
321 	unsigned int *count, const char buffer[EV_BYTE_CHANNEL_MAX_BYTES])
322 {
323 	register uintptr_t r11 __asm__("r11");
324 	register uintptr_t r3 __asm__("r3");
325 	register uintptr_t r4 __asm__("r4");
326 	register uintptr_t r5 __asm__("r5");
327 	register uintptr_t r6 __asm__("r6");
328 	register uintptr_t r7 __asm__("r7");
329 	register uintptr_t r8 __asm__("r8");
330 	const uint32_t *p = (const uint32_t *) buffer;
331 
332 	r11 = EV_HCALL_TOKEN(EV_BYTE_CHANNEL_SEND);
333 	r3 = handle;
334 	r4 = *count;
335 	r5 = be32_to_cpu(p[0]);
336 	r6 = be32_to_cpu(p[1]);
337 	r7 = be32_to_cpu(p[2]);
338 	r8 = be32_to_cpu(p[3]);
339 
340 	__asm__ __volatile__ ("sc 1"
341 		: "+r" (r11), "+r" (r3),
342 		  "+r" (r4), "+r" (r5), "+r" (r6), "+r" (r7), "+r" (r8)
343 		: : EV_HCALL_CLOBBERS6
344 	);
345 
346 	*count = r4;
347 
348 	return r3;
349 }
350 
351 /**
352  * ev_byte_channel_receive - fetch characters from a byte channel
353  * @handle: byte channel handle
354  * @count: (input) max num of chars to receive, (output) num chars received
355  * @buffer: pointer to a 16-byte buffer
356  *
357  * The size of @buffer must be at least 16 bytes, even if you request fewer
358  * than 16 characters, because we always write 16 bytes to @buffer.  This is
359  * for performance reasons.
360  *
361  * Returns 0 for success, or an error code.
362  */
ev_byte_channel_receive(unsigned int handle,unsigned int * count,char buffer[EV_BYTE_CHANNEL_MAX_BYTES])363 static inline unsigned int ev_byte_channel_receive(unsigned int handle,
364 	unsigned int *count, char buffer[EV_BYTE_CHANNEL_MAX_BYTES])
365 {
366 	register uintptr_t r11 __asm__("r11");
367 	register uintptr_t r3 __asm__("r3");
368 	register uintptr_t r4 __asm__("r4");
369 	register uintptr_t r5 __asm__("r5");
370 	register uintptr_t r6 __asm__("r6");
371 	register uintptr_t r7 __asm__("r7");
372 	register uintptr_t r8 __asm__("r8");
373 	uint32_t *p = (uint32_t *) buffer;
374 
375 	r11 = EV_HCALL_TOKEN(EV_BYTE_CHANNEL_RECEIVE);
376 	r3 = handle;
377 	r4 = *count;
378 
379 	__asm__ __volatile__ ("sc 1"
380 		: "+r" (r11), "+r" (r3), "+r" (r4),
381 		  "=r" (r5), "=r" (r6), "=r" (r7), "=r" (r8)
382 		: : EV_HCALL_CLOBBERS6
383 	);
384 
385 	*count = r4;
386 	p[0] = cpu_to_be32(r5);
387 	p[1] = cpu_to_be32(r6);
388 	p[2] = cpu_to_be32(r7);
389 	p[3] = cpu_to_be32(r8);
390 
391 	return r3;
392 }
393 
394 /**
395  * ev_byte_channel_poll - returns the status of the byte channel buffers
396  * @handle: byte channel handle
397  * @rx_count: returned count of bytes in receive queue
398  * @tx_count: returned count of free space in transmit queue
399  *
400  * This function reports the amount of data in the receive queue (i.e. the
401  * number of bytes you can read), and the amount of free space in the transmit
402  * queue (i.e. the number of bytes you can write).
403  *
404  * Returns 0 for success, or an error code.
405  */
ev_byte_channel_poll(unsigned int handle,unsigned int * rx_count,unsigned int * tx_count)406 static inline unsigned int ev_byte_channel_poll(unsigned int handle,
407 	unsigned int *rx_count,	unsigned int *tx_count)
408 {
409 	register uintptr_t r11 __asm__("r11");
410 	register uintptr_t r3 __asm__("r3");
411 	register uintptr_t r4 __asm__("r4");
412 	register uintptr_t r5 __asm__("r5");
413 
414 	r11 = EV_HCALL_TOKEN(EV_BYTE_CHANNEL_POLL);
415 	r3 = handle;
416 
417 	__asm__ __volatile__ ("sc 1"
418 		: "+r" (r11), "+r" (r3), "=r" (r4), "=r" (r5)
419 		: : EV_HCALL_CLOBBERS3
420 	);
421 
422 	*rx_count = r4;
423 	*tx_count = r5;
424 
425 	return r3;
426 }
427 
428 /**
429  * ev_int_iack - acknowledge an interrupt
430  * @handle: handle to the target interrupt controller
431  * @vector: returned interrupt vector
432  *
433  * If handle is zero, the function returns the next interrupt source
434  * number to be handled irrespective of the hierarchy or cascading
435  * of interrupt controllers. If non-zero, specifies a handle to the
436  * interrupt controller that is the target of the acknowledge.
437  *
438  * Returns 0 for success, or an error code.
439  */
ev_int_iack(unsigned int handle,unsigned int * vector)440 static inline unsigned int ev_int_iack(unsigned int handle,
441 	unsigned int *vector)
442 {
443 	register uintptr_t r11 __asm__("r11");
444 	register uintptr_t r3 __asm__("r3");
445 	register uintptr_t r4 __asm__("r4");
446 
447 	r11 = EV_HCALL_TOKEN(EV_INT_IACK);
448 	r3 = handle;
449 
450 	__asm__ __volatile__ ("sc 1"
451 		: "+r" (r11), "+r" (r3), "=r" (r4)
452 		: : EV_HCALL_CLOBBERS2
453 	);
454 
455 	*vector = r4;
456 
457 	return r3;
458 }
459 
460 /**
461  * ev_doorbell_send - send a doorbell to another partition
462  * @handle: doorbell send handle
463  *
464  * Returns 0 for success, or an error code.
465  */
ev_doorbell_send(unsigned int handle)466 static inline unsigned int ev_doorbell_send(unsigned int handle)
467 {
468 	register uintptr_t r11 __asm__("r11");
469 	register uintptr_t r3 __asm__("r3");
470 
471 	r11 = EV_HCALL_TOKEN(EV_DOORBELL_SEND);
472 	r3 = handle;
473 
474 	__asm__ __volatile__ ("sc 1"
475 		: "+r" (r11), "+r" (r3)
476 		: : EV_HCALL_CLOBBERS1
477 	);
478 
479 	return r3;
480 }
481 
482 /**
483  * ev_idle -- wait for next interrupt on this core
484  *
485  * Returns 0 for success, or an error code.
486  */
ev_idle(void)487 static inline unsigned int ev_idle(void)
488 {
489 	register uintptr_t r11 __asm__("r11");
490 	register uintptr_t r3 __asm__("r3");
491 
492 	r11 = EV_HCALL_TOKEN(EV_IDLE);
493 
494 	__asm__ __volatile__ ("sc 1"
495 		: "+r" (r11), "=r" (r3)
496 		: : EV_HCALL_CLOBBERS1
497 	);
498 
499 	return r3;
500 }
501 
502 #endif
503