epapr_hcalls.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. /* A "hypercall" is an "sc 1" instruction. This header file file provides C
  40. * wrapper functions for the ePAPR hypervisor interface. It is inteded
  41. * for use by Linux device drivers and other operating systems.
  42. *
  43. * The hypercalls are implemented as inline assembly, rather than assembly
  44. * language functions in a .S file, for optimization. It allows
  45. * the caller to issue the hypercall instruction directly, improving both
  46. * performance and memory footprint.
  47. */
  48. #ifndef _EPAPR_HCALLS_H
  49. #define _EPAPR_HCALLS_H
  50. #include <linux/types.h>
  51. #include <linux/errno.h>
  52. #include <asm/byteorder.h>
  53. #define EV_BYTE_CHANNEL_SEND 1
  54. #define EV_BYTE_CHANNEL_RECEIVE 2
  55. #define EV_BYTE_CHANNEL_POLL 3
  56. #define EV_INT_SET_CONFIG 4
  57. #define EV_INT_GET_CONFIG 5
  58. #define EV_INT_SET_MASK 6
  59. #define EV_INT_GET_MASK 7
  60. #define EV_INT_IACK 9
  61. #define EV_INT_EOI 10
  62. #define EV_INT_SEND_IPI 11
  63. #define EV_INT_SET_TASK_PRIORITY 12
  64. #define EV_INT_GET_TASK_PRIORITY 13
  65. #define EV_DOORBELL_SEND 14
  66. #define EV_MSGSND 15
  67. #define EV_IDLE 16
  68. /* vendor ID: epapr */
  69. #define EV_LOCAL_VENDOR_ID 0 /* for private use */
  70. #define EV_EPAPR_VENDOR_ID 1
  71. #define EV_FSL_VENDOR_ID 2 /* Freescale Semiconductor */
  72. #define EV_IBM_VENDOR_ID 3 /* IBM */
  73. #define EV_GHS_VENDOR_ID 4 /* Green Hills Software */
  74. #define EV_ENEA_VENDOR_ID 5 /* Enea */
  75. #define EV_WR_VENDOR_ID 6 /* Wind River Systems */
  76. #define EV_AMCC_VENDOR_ID 7 /* Applied Micro Circuits */
  77. #define EV_KVM_VENDOR_ID 42 /* KVM */
  78. /* The max number of bytes that a byte channel can send or receive per call */
  79. #define EV_BYTE_CHANNEL_MAX_BYTES 16
  80. #define _EV_HCALL_TOKEN(id, num) (((id) << 16) | (num))
  81. #define EV_HCALL_TOKEN(hcall_num) _EV_HCALL_TOKEN(EV_EPAPR_VENDOR_ID, hcall_num)
  82. /* epapr error codes */
  83. #define EV_EPERM 1 /* Operation not permitted */
  84. #define EV_ENOENT 2 /* Entry Not Found */
  85. #define EV_EIO 3 /* I/O error occured */
  86. #define EV_EAGAIN 4 /* The operation had insufficient
  87. * resources to complete and should be
  88. * retried
  89. */
  90. #define EV_ENOMEM 5 /* There was insufficient memory to
  91. * complete the operation */
  92. #define EV_EFAULT 6 /* Bad guest address */
  93. #define EV_ENODEV 7 /* No such device */
  94. #define EV_EINVAL 8 /* An argument supplied to the hcall
  95. was out of range or invalid */
  96. #define EV_INTERNAL 9 /* An internal error occured */
  97. #define EV_CONFIG 10 /* A configuration error was detected */
  98. #define EV_INVALID_STATE 11 /* The object is in an invalid state */
  99. #define EV_UNIMPLEMENTED 12 /* Unimplemented hypercall */
  100. #define EV_BUFFER_OVERFLOW 13 /* Caller-supplied buffer too small */
  101. /*
  102. * Hypercall register clobber list
  103. *
  104. * These macros are used to define the list of clobbered registers during a
  105. * hypercall. Technically, registers r0 and r3-r12 are always clobbered,
  106. * but the gcc inline assembly syntax does not allow us to specify registers
  107. * on the clobber list that are also on the input/output list. Therefore,
  108. * the lists of clobbered registers depends on the number of register
  109. * parmeters ("+r" and "=r") passed to the hypercall.
  110. *
  111. * Each assembly block should use one of the HCALL_CLOBBERSx macros. As a
  112. * general rule, 'x' is the number of parameters passed to the assembly
  113. * block *except* for r11.
  114. *
  115. * If you're not sure, just use the smallest value of 'x' that does not
  116. * generate a compilation error. Because these are static inline functions,
  117. * the compiler will only check the clobber list for a function if you
  118. * compile code that calls that function.
  119. *
  120. * r3 and r11 are not included in any clobbers list because they are always
  121. * listed as output registers.
  122. *
  123. * XER, CTR, and LR are currently listed as clobbers because it's uncertain
  124. * whether they will be clobbered.
  125. *
  126. * Note that r11 can be used as an output parameter.
  127. */
  128. /* List of common clobbered registers. Do not use this macro. */
  129. #define EV_HCALL_CLOBBERS "r0", "r12", "xer", "ctr", "lr", "cc"
  130. #define EV_HCALL_CLOBBERS8 EV_HCALL_CLOBBERS
  131. #define EV_HCALL_CLOBBERS7 EV_HCALL_CLOBBERS8, "r10"
  132. #define EV_HCALL_CLOBBERS6 EV_HCALL_CLOBBERS7, "r9"
  133. #define EV_HCALL_CLOBBERS5 EV_HCALL_CLOBBERS6, "r8"
  134. #define EV_HCALL_CLOBBERS4 EV_HCALL_CLOBBERS5, "r7"
  135. #define EV_HCALL_CLOBBERS3 EV_HCALL_CLOBBERS4, "r6"
  136. #define EV_HCALL_CLOBBERS2 EV_HCALL_CLOBBERS3, "r5"
  137. #define EV_HCALL_CLOBBERS1 EV_HCALL_CLOBBERS2, "r4"
  138. /*
  139. * We use "uintptr_t" to define a register because it's guaranteed to be a
  140. * 32-bit integer on a 32-bit platform, and a 64-bit integer on a 64-bit
  141. * platform.
  142. *
  143. * All registers are either input/output or output only. Registers that are
  144. * initialized before making the hypercall are input/output. All
  145. * input/output registers are represented with "+r". Output-only registers
  146. * are represented with "=r". Do not specify any unused registers. The
  147. * clobber list will tell the compiler that the hypercall modifies those
  148. * registers, which is good enough.
  149. */
  150. /**
  151. * ev_int_set_config - configure the specified interrupt
  152. * @interrupt: the interrupt number
  153. * @config: configuration for this interrupt
  154. * @priority: interrupt priority
  155. * @destination: destination CPU number
  156. *
  157. * Returns 0 for success, or an error code.
  158. */
  159. static inline unsigned int ev_int_set_config(unsigned int interrupt,
  160. uint32_t config, unsigned int priority, uint32_t destination)
  161. {
  162. register uintptr_t r11 __asm__("r11");
  163. register uintptr_t r3 __asm__("r3");
  164. register uintptr_t r4 __asm__("r4");
  165. register uintptr_t r5 __asm__("r5");
  166. register uintptr_t r6 __asm__("r6");
  167. r11 = EV_HCALL_TOKEN(EV_INT_SET_CONFIG);
  168. r3 = interrupt;
  169. r4 = config;
  170. r5 = priority;
  171. r6 = destination;
  172. __asm__ __volatile__ ("sc 1"
  173. : "+r" (r11), "+r" (r3), "+r" (r4), "+r" (r5), "+r" (r6)
  174. : : EV_HCALL_CLOBBERS4
  175. );
  176. return r3;
  177. }
  178. /**
  179. * ev_int_get_config - return the config of the specified interrupt
  180. * @interrupt: the interrupt number
  181. * @config: returned configuration for this interrupt
  182. * @priority: returned interrupt priority
  183. * @destination: returned destination CPU number
  184. *
  185. * Returns 0 for success, or an error code.
  186. */
  187. static inline unsigned int ev_int_get_config(unsigned int interrupt,
  188. uint32_t *config, unsigned int *priority, uint32_t *destination)
  189. {
  190. register uintptr_t r11 __asm__("r11");
  191. register uintptr_t r3 __asm__("r3");
  192. register uintptr_t r4 __asm__("r4");
  193. register uintptr_t r5 __asm__("r5");
  194. register uintptr_t r6 __asm__("r6");
  195. r11 = EV_HCALL_TOKEN(EV_INT_GET_CONFIG);
  196. r3 = interrupt;
  197. __asm__ __volatile__ ("sc 1"
  198. : "+r" (r11), "+r" (r3), "=r" (r4), "=r" (r5), "=r" (r6)
  199. : : EV_HCALL_CLOBBERS4
  200. );
  201. *config = r4;
  202. *priority = r5;
  203. *destination = r6;
  204. return r3;
  205. }
  206. /**
  207. * ev_int_set_mask - sets the mask for the specified interrupt source
  208. * @interrupt: the interrupt number
  209. * @mask: 0=enable interrupts, 1=disable interrupts
  210. *
  211. * Returns 0 for success, or an error code.
  212. */
  213. static inline unsigned int ev_int_set_mask(unsigned int interrupt,
  214. unsigned int mask)
  215. {
  216. register uintptr_t r11 __asm__("r11");
  217. register uintptr_t r3 __asm__("r3");
  218. register uintptr_t r4 __asm__("r4");
  219. r11 = EV_HCALL_TOKEN(EV_INT_SET_MASK);
  220. r3 = interrupt;
  221. r4 = mask;
  222. __asm__ __volatile__ ("sc 1"
  223. : "+r" (r11), "+r" (r3), "+r" (r4)
  224. : : EV_HCALL_CLOBBERS2
  225. );
  226. return r3;
  227. }
  228. /**
  229. * ev_int_get_mask - returns the mask for the specified interrupt source
  230. * @interrupt: the interrupt number
  231. * @mask: returned mask for this interrupt (0=enabled, 1=disabled)
  232. *
  233. * Returns 0 for success, or an error code.
  234. */
  235. static inline unsigned int ev_int_get_mask(unsigned int interrupt,
  236. unsigned int *mask)
  237. {
  238. register uintptr_t r11 __asm__("r11");
  239. register uintptr_t r3 __asm__("r3");
  240. register uintptr_t r4 __asm__("r4");
  241. r11 = EV_HCALL_TOKEN(EV_INT_GET_MASK);
  242. r3 = interrupt;
  243. __asm__ __volatile__ ("sc 1"
  244. : "+r" (r11), "+r" (r3), "=r" (r4)
  245. : : EV_HCALL_CLOBBERS2
  246. );
  247. *mask = r4;
  248. return r3;
  249. }
  250. /**
  251. * ev_int_eoi - signal the end of interrupt processing
  252. * @interrupt: the interrupt number
  253. *
  254. * This function signals the end of processing for the the specified
  255. * interrupt, which must be the interrupt currently in service. By
  256. * definition, this is also the highest-priority interrupt.
  257. *
  258. * Returns 0 for success, or an error code.
  259. */
  260. static inline unsigned int ev_int_eoi(unsigned int interrupt)
  261. {
  262. register uintptr_t r11 __asm__("r11");
  263. register uintptr_t r3 __asm__("r3");
  264. r11 = EV_HCALL_TOKEN(EV_INT_EOI);
  265. r3 = interrupt;
  266. __asm__ __volatile__ ("sc 1"
  267. : "+r" (r11), "+r" (r3)
  268. : : EV_HCALL_CLOBBERS1
  269. );
  270. return r3;
  271. }
  272. /**
  273. * ev_byte_channel_send - send characters to a byte stream
  274. * @handle: byte stream handle
  275. * @count: (input) num of chars to send, (output) num chars sent
  276. * @buffer: pointer to a 16-byte buffer
  277. *
  278. * @buffer must be at least 16 bytes long, because all 16 bytes will be
  279. * read from memory into registers, even if count < 16.
  280. *
  281. * Returns 0 for success, or an error code.
  282. */
  283. static inline unsigned int ev_byte_channel_send(unsigned int handle,
  284. unsigned int *count, const char buffer[EV_BYTE_CHANNEL_MAX_BYTES])
  285. {
  286. register uintptr_t r11 __asm__("r11");
  287. register uintptr_t r3 __asm__("r3");
  288. register uintptr_t r4 __asm__("r4");
  289. register uintptr_t r5 __asm__("r5");
  290. register uintptr_t r6 __asm__("r6");
  291. register uintptr_t r7 __asm__("r7");
  292. register uintptr_t r8 __asm__("r8");
  293. const uint32_t *p = (const uint32_t *) buffer;
  294. r11 = EV_HCALL_TOKEN(EV_BYTE_CHANNEL_SEND);
  295. r3 = handle;
  296. r4 = *count;
  297. r5 = be32_to_cpu(p[0]);
  298. r6 = be32_to_cpu(p[1]);
  299. r7 = be32_to_cpu(p[2]);
  300. r8 = be32_to_cpu(p[3]);
  301. __asm__ __volatile__ ("sc 1"
  302. : "+r" (r11), "+r" (r3),
  303. "+r" (r4), "+r" (r5), "+r" (r6), "+r" (r7), "+r" (r8)
  304. : : EV_HCALL_CLOBBERS6
  305. );
  306. *count = r4;
  307. return r3;
  308. }
  309. /**
  310. * ev_byte_channel_receive - fetch characters from a byte channel
  311. * @handle: byte channel handle
  312. * @count: (input) max num of chars to receive, (output) num chars received
  313. * @buffer: pointer to a 16-byte buffer
  314. *
  315. * The size of @buffer must be at least 16 bytes, even if you request fewer
  316. * than 16 characters, because we always write 16 bytes to @buffer. This is
  317. * for performance reasons.
  318. *
  319. * Returns 0 for success, or an error code.
  320. */
  321. static inline unsigned int ev_byte_channel_receive(unsigned int handle,
  322. unsigned int *count, char buffer[EV_BYTE_CHANNEL_MAX_BYTES])
  323. {
  324. register uintptr_t r11 __asm__("r11");
  325. register uintptr_t r3 __asm__("r3");
  326. register uintptr_t r4 __asm__("r4");
  327. register uintptr_t r5 __asm__("r5");
  328. register uintptr_t r6 __asm__("r6");
  329. register uintptr_t r7 __asm__("r7");
  330. register uintptr_t r8 __asm__("r8");
  331. uint32_t *p = (uint32_t *) buffer;
  332. r11 = EV_HCALL_TOKEN(EV_BYTE_CHANNEL_RECEIVE);
  333. r3 = handle;
  334. r4 = *count;
  335. __asm__ __volatile__ ("sc 1"
  336. : "+r" (r11), "+r" (r3), "+r" (r4),
  337. "=r" (r5), "=r" (r6), "=r" (r7), "=r" (r8)
  338. : : EV_HCALL_CLOBBERS6
  339. );
  340. *count = r4;
  341. p[0] = cpu_to_be32(r5);
  342. p[1] = cpu_to_be32(r6);
  343. p[2] = cpu_to_be32(r7);
  344. p[3] = cpu_to_be32(r8);
  345. return r3;
  346. }
  347. /**
  348. * ev_byte_channel_poll - returns the status of the byte channel buffers
  349. * @handle: byte channel handle
  350. * @rx_count: returned count of bytes in receive queue
  351. * @tx_count: returned count of free space in transmit queue
  352. *
  353. * This function reports the amount of data in the receive queue (i.e. the
  354. * number of bytes you can read), and the amount of free space in the transmit
  355. * queue (i.e. the number of bytes you can write).
  356. *
  357. * Returns 0 for success, or an error code.
  358. */
  359. static inline unsigned int ev_byte_channel_poll(unsigned int handle,
  360. unsigned int *rx_count, unsigned int *tx_count)
  361. {
  362. register uintptr_t r11 __asm__("r11");
  363. register uintptr_t r3 __asm__("r3");
  364. register uintptr_t r4 __asm__("r4");
  365. register uintptr_t r5 __asm__("r5");
  366. r11 = EV_HCALL_TOKEN(EV_BYTE_CHANNEL_POLL);
  367. r3 = handle;
  368. __asm__ __volatile__ ("sc 1"
  369. : "+r" (r11), "+r" (r3), "=r" (r4), "=r" (r5)
  370. : : EV_HCALL_CLOBBERS3
  371. );
  372. *rx_count = r4;
  373. *tx_count = r5;
  374. return r3;
  375. }
  376. /**
  377. * ev_int_iack - acknowledge an interrupt
  378. * @handle: handle to the target interrupt controller
  379. * @vector: returned interrupt vector
  380. *
  381. * If handle is zero, the function returns the next interrupt source
  382. * number to be handled irrespective of the hierarchy or cascading
  383. * of interrupt controllers. If non-zero, specifies a handle to the
  384. * interrupt controller that is the target of the acknowledge.
  385. *
  386. * Returns 0 for success, or an error code.
  387. */
  388. static inline unsigned int ev_int_iack(unsigned int handle,
  389. unsigned int *vector)
  390. {
  391. register uintptr_t r11 __asm__("r11");
  392. register uintptr_t r3 __asm__("r3");
  393. register uintptr_t r4 __asm__("r4");
  394. r11 = EV_HCALL_TOKEN(EV_INT_IACK);
  395. r3 = handle;
  396. __asm__ __volatile__ ("sc 1"
  397. : "+r" (r11), "+r" (r3), "=r" (r4)
  398. : : EV_HCALL_CLOBBERS2
  399. );
  400. *vector = r4;
  401. return r3;
  402. }
  403. /**
  404. * ev_doorbell_send - send a doorbell to another partition
  405. * @handle: doorbell send handle
  406. *
  407. * Returns 0 for success, or an error code.
  408. */
  409. static inline unsigned int ev_doorbell_send(unsigned int handle)
  410. {
  411. register uintptr_t r11 __asm__("r11");
  412. register uintptr_t r3 __asm__("r3");
  413. r11 = EV_HCALL_TOKEN(EV_DOORBELL_SEND);
  414. r3 = handle;
  415. __asm__ __volatile__ ("sc 1"
  416. : "+r" (r11), "+r" (r3)
  417. : : EV_HCALL_CLOBBERS1
  418. );
  419. return r3;
  420. }
  421. /**
  422. * ev_idle -- wait for next interrupt on this core
  423. *
  424. * Returns 0 for success, or an error code.
  425. */
  426. static inline unsigned int ev_idle(void)
  427. {
  428. register uintptr_t r11 __asm__("r11");
  429. register uintptr_t r3 __asm__("r3");
  430. r11 = EV_HCALL_TOKEN(EV_IDLE);
  431. __asm__ __volatile__ ("sc 1"
  432. : "+r" (r11), "=r" (r3)
  433. : : EV_HCALL_CLOBBERS1
  434. );
  435. return r3;
  436. }
  437. #endif