epapr_hcalls.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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 <uapi/asm/epapr_hcalls.h>
  51. #ifndef __ASSEMBLY__
  52. #include <linux/types.h>
  53. #include <linux/errno.h>
  54. #include <asm/byteorder.h>
  55. /*
  56. * Hypercall register clobber list
  57. *
  58. * These macros are used to define the list of clobbered registers during a
  59. * hypercall. Technically, registers r0 and r3-r12 are always clobbered,
  60. * but the gcc inline assembly syntax does not allow us to specify registers
  61. * on the clobber list that are also on the input/output list. Therefore,
  62. * the lists of clobbered registers depends on the number of register
  63. * parmeters ("+r" and "=r") passed to the hypercall.
  64. *
  65. * Each assembly block should use one of the HCALL_CLOBBERSx macros. As a
  66. * general rule, 'x' is the number of parameters passed to the assembly
  67. * block *except* for r11.
  68. *
  69. * If you're not sure, just use the smallest value of 'x' that does not
  70. * generate a compilation error. Because these are static inline functions,
  71. * the compiler will only check the clobber list for a function if you
  72. * compile code that calls that function.
  73. *
  74. * r3 and r11 are not included in any clobbers list because they are always
  75. * listed as output registers.
  76. *
  77. * XER, CTR, and LR are currently listed as clobbers because it's uncertain
  78. * whether they will be clobbered.
  79. *
  80. * Note that r11 can be used as an output parameter.
  81. *
  82. * The "memory" clobber is only necessary for hcalls where the Hypervisor
  83. * will read or write guest memory. However, we add it to all hcalls because
  84. * the impact is minimal, and we want to ensure that it's present for the
  85. * hcalls that need it.
  86. */
  87. /* List of common clobbered registers. Do not use this macro. */
  88. #define EV_HCALL_CLOBBERS "r0", "r12", "xer", "ctr", "lr", "cc", "memory"
  89. #define EV_HCALL_CLOBBERS8 EV_HCALL_CLOBBERS
  90. #define EV_HCALL_CLOBBERS7 EV_HCALL_CLOBBERS8, "r10"
  91. #define EV_HCALL_CLOBBERS6 EV_HCALL_CLOBBERS7, "r9"
  92. #define EV_HCALL_CLOBBERS5 EV_HCALL_CLOBBERS6, "r8"
  93. #define EV_HCALL_CLOBBERS4 EV_HCALL_CLOBBERS5, "r7"
  94. #define EV_HCALL_CLOBBERS3 EV_HCALL_CLOBBERS4, "r6"
  95. #define EV_HCALL_CLOBBERS2 EV_HCALL_CLOBBERS3, "r5"
  96. #define EV_HCALL_CLOBBERS1 EV_HCALL_CLOBBERS2, "r4"
  97. extern bool epapr_paravirt_enabled;
  98. extern u32 epapr_hypercall_start[];
  99. /*
  100. * We use "uintptr_t" to define a register because it's guaranteed to be a
  101. * 32-bit integer on a 32-bit platform, and a 64-bit integer on a 64-bit
  102. * platform.
  103. *
  104. * All registers are either input/output or output only. Registers that are
  105. * initialized before making the hypercall are input/output. All
  106. * input/output registers are represented with "+r". Output-only registers
  107. * are represented with "=r". Do not specify any unused registers. The
  108. * clobber list will tell the compiler that the hypercall modifies those
  109. * registers, which is good enough.
  110. */
  111. /**
  112. * ev_int_set_config - configure the specified interrupt
  113. * @interrupt: the interrupt number
  114. * @config: configuration for this interrupt
  115. * @priority: interrupt priority
  116. * @destination: destination CPU number
  117. *
  118. * Returns 0 for success, or an error code.
  119. */
  120. static inline unsigned int ev_int_set_config(unsigned int interrupt,
  121. uint32_t config, unsigned int priority, uint32_t destination)
  122. {
  123. register uintptr_t r11 __asm__("r11");
  124. register uintptr_t r3 __asm__("r3");
  125. register uintptr_t r4 __asm__("r4");
  126. register uintptr_t r5 __asm__("r5");
  127. register uintptr_t r6 __asm__("r6");
  128. r11 = EV_HCALL_TOKEN(EV_INT_SET_CONFIG);
  129. r3 = interrupt;
  130. r4 = config;
  131. r5 = priority;
  132. r6 = destination;
  133. asm volatile("bl epapr_hypercall_start"
  134. : "+r" (r11), "+r" (r3), "+r" (r4), "+r" (r5), "+r" (r6)
  135. : : EV_HCALL_CLOBBERS4
  136. );
  137. return r3;
  138. }
  139. /**
  140. * ev_int_get_config - return the config of the specified interrupt
  141. * @interrupt: the interrupt number
  142. * @config: returned configuration for this interrupt
  143. * @priority: returned interrupt priority
  144. * @destination: returned destination CPU number
  145. *
  146. * Returns 0 for success, or an error code.
  147. */
  148. static inline unsigned int ev_int_get_config(unsigned int interrupt,
  149. uint32_t *config, unsigned int *priority, uint32_t *destination)
  150. {
  151. register uintptr_t r11 __asm__("r11");
  152. register uintptr_t r3 __asm__("r3");
  153. register uintptr_t r4 __asm__("r4");
  154. register uintptr_t r5 __asm__("r5");
  155. register uintptr_t r6 __asm__("r6");
  156. r11 = EV_HCALL_TOKEN(EV_INT_GET_CONFIG);
  157. r3 = interrupt;
  158. asm volatile("bl epapr_hypercall_start"
  159. : "+r" (r11), "+r" (r3), "=r" (r4), "=r" (r5), "=r" (r6)
  160. : : EV_HCALL_CLOBBERS4
  161. );
  162. *config = r4;
  163. *priority = r5;
  164. *destination = r6;
  165. return r3;
  166. }
  167. /**
  168. * ev_int_set_mask - sets the mask for the specified interrupt source
  169. * @interrupt: the interrupt number
  170. * @mask: 0=enable interrupts, 1=disable interrupts
  171. *
  172. * Returns 0 for success, or an error code.
  173. */
  174. static inline unsigned int ev_int_set_mask(unsigned int interrupt,
  175. unsigned int mask)
  176. {
  177. register uintptr_t r11 __asm__("r11");
  178. register uintptr_t r3 __asm__("r3");
  179. register uintptr_t r4 __asm__("r4");
  180. r11 = EV_HCALL_TOKEN(EV_INT_SET_MASK);
  181. r3 = interrupt;
  182. r4 = mask;
  183. asm volatile("bl epapr_hypercall_start"
  184. : "+r" (r11), "+r" (r3), "+r" (r4)
  185. : : EV_HCALL_CLOBBERS2
  186. );
  187. return r3;
  188. }
  189. /**
  190. * ev_int_get_mask - returns the mask for the specified interrupt source
  191. * @interrupt: the interrupt number
  192. * @mask: returned mask for this interrupt (0=enabled, 1=disabled)
  193. *
  194. * Returns 0 for success, or an error code.
  195. */
  196. static inline unsigned int ev_int_get_mask(unsigned int interrupt,
  197. unsigned int *mask)
  198. {
  199. register uintptr_t r11 __asm__("r11");
  200. register uintptr_t r3 __asm__("r3");
  201. register uintptr_t r4 __asm__("r4");
  202. r11 = EV_HCALL_TOKEN(EV_INT_GET_MASK);
  203. r3 = interrupt;
  204. asm volatile("bl epapr_hypercall_start"
  205. : "+r" (r11), "+r" (r3), "=r" (r4)
  206. : : EV_HCALL_CLOBBERS2
  207. );
  208. *mask = r4;
  209. return r3;
  210. }
  211. /**
  212. * ev_int_eoi - signal the end of interrupt processing
  213. * @interrupt: the interrupt number
  214. *
  215. * This function signals the end of processing for the the specified
  216. * interrupt, which must be the interrupt currently in service. By
  217. * definition, this is also the highest-priority interrupt.
  218. *
  219. * Returns 0 for success, or an error code.
  220. */
  221. static inline unsigned int ev_int_eoi(unsigned int interrupt)
  222. {
  223. register uintptr_t r11 __asm__("r11");
  224. register uintptr_t r3 __asm__("r3");
  225. r11 = EV_HCALL_TOKEN(EV_INT_EOI);
  226. r3 = interrupt;
  227. asm volatile("bl epapr_hypercall_start"
  228. : "+r" (r11), "+r" (r3)
  229. : : EV_HCALL_CLOBBERS1
  230. );
  231. return r3;
  232. }
  233. /**
  234. * ev_byte_channel_send - send characters to a byte stream
  235. * @handle: byte stream handle
  236. * @count: (input) num of chars to send, (output) num chars sent
  237. * @buffer: pointer to a 16-byte buffer
  238. *
  239. * @buffer must be at least 16 bytes long, because all 16 bytes will be
  240. * read from memory into registers, even if count < 16.
  241. *
  242. * Returns 0 for success, or an error code.
  243. */
  244. static inline unsigned int ev_byte_channel_send(unsigned int handle,
  245. unsigned int *count, const char buffer[EV_BYTE_CHANNEL_MAX_BYTES])
  246. {
  247. register uintptr_t r11 __asm__("r11");
  248. register uintptr_t r3 __asm__("r3");
  249. register uintptr_t r4 __asm__("r4");
  250. register uintptr_t r5 __asm__("r5");
  251. register uintptr_t r6 __asm__("r6");
  252. register uintptr_t r7 __asm__("r7");
  253. register uintptr_t r8 __asm__("r8");
  254. const uint32_t *p = (const uint32_t *) buffer;
  255. r11 = EV_HCALL_TOKEN(EV_BYTE_CHANNEL_SEND);
  256. r3 = handle;
  257. r4 = *count;
  258. r5 = be32_to_cpu(p[0]);
  259. r6 = be32_to_cpu(p[1]);
  260. r7 = be32_to_cpu(p[2]);
  261. r8 = be32_to_cpu(p[3]);
  262. asm volatile("bl epapr_hypercall_start"
  263. : "+r" (r11), "+r" (r3),
  264. "+r" (r4), "+r" (r5), "+r" (r6), "+r" (r7), "+r" (r8)
  265. : : EV_HCALL_CLOBBERS6
  266. );
  267. *count = r4;
  268. return r3;
  269. }
  270. /**
  271. * ev_byte_channel_receive - fetch characters from a byte channel
  272. * @handle: byte channel handle
  273. * @count: (input) max num of chars to receive, (output) num chars received
  274. * @buffer: pointer to a 16-byte buffer
  275. *
  276. * The size of @buffer must be at least 16 bytes, even if you request fewer
  277. * than 16 characters, because we always write 16 bytes to @buffer. This is
  278. * for performance reasons.
  279. *
  280. * Returns 0 for success, or an error code.
  281. */
  282. static inline unsigned int ev_byte_channel_receive(unsigned int handle,
  283. unsigned int *count, char buffer[EV_BYTE_CHANNEL_MAX_BYTES])
  284. {
  285. register uintptr_t r11 __asm__("r11");
  286. register uintptr_t r3 __asm__("r3");
  287. register uintptr_t r4 __asm__("r4");
  288. register uintptr_t r5 __asm__("r5");
  289. register uintptr_t r6 __asm__("r6");
  290. register uintptr_t r7 __asm__("r7");
  291. register uintptr_t r8 __asm__("r8");
  292. uint32_t *p = (uint32_t *) buffer;
  293. r11 = EV_HCALL_TOKEN(EV_BYTE_CHANNEL_RECEIVE);
  294. r3 = handle;
  295. r4 = *count;
  296. asm volatile("bl epapr_hypercall_start"
  297. : "+r" (r11), "+r" (r3), "+r" (r4),
  298. "=r" (r5), "=r" (r6), "=r" (r7), "=r" (r8)
  299. : : EV_HCALL_CLOBBERS6
  300. );
  301. *count = r4;
  302. p[0] = cpu_to_be32(r5);
  303. p[1] = cpu_to_be32(r6);
  304. p[2] = cpu_to_be32(r7);
  305. p[3] = cpu_to_be32(r8);
  306. return r3;
  307. }
  308. /**
  309. * ev_byte_channel_poll - returns the status of the byte channel buffers
  310. * @handle: byte channel handle
  311. * @rx_count: returned count of bytes in receive queue
  312. * @tx_count: returned count of free space in transmit queue
  313. *
  314. * This function reports the amount of data in the receive queue (i.e. the
  315. * number of bytes you can read), and the amount of free space in the transmit
  316. * queue (i.e. the number of bytes you can write).
  317. *
  318. * Returns 0 for success, or an error code.
  319. */
  320. static inline unsigned int ev_byte_channel_poll(unsigned int handle,
  321. unsigned int *rx_count, unsigned int *tx_count)
  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. r11 = EV_HCALL_TOKEN(EV_BYTE_CHANNEL_POLL);
  328. r3 = handle;
  329. asm volatile("bl epapr_hypercall_start"
  330. : "+r" (r11), "+r" (r3), "=r" (r4), "=r" (r5)
  331. : : EV_HCALL_CLOBBERS3
  332. );
  333. *rx_count = r4;
  334. *tx_count = r5;
  335. return r3;
  336. }
  337. /**
  338. * ev_int_iack - acknowledge an interrupt
  339. * @handle: handle to the target interrupt controller
  340. * @vector: returned interrupt vector
  341. *
  342. * If handle is zero, the function returns the next interrupt source
  343. * number to be handled irrespective of the hierarchy or cascading
  344. * of interrupt controllers. If non-zero, specifies a handle to the
  345. * interrupt controller that is the target of the acknowledge.
  346. *
  347. * Returns 0 for success, or an error code.
  348. */
  349. static inline unsigned int ev_int_iack(unsigned int handle,
  350. unsigned int *vector)
  351. {
  352. register uintptr_t r11 __asm__("r11");
  353. register uintptr_t r3 __asm__("r3");
  354. register uintptr_t r4 __asm__("r4");
  355. r11 = EV_HCALL_TOKEN(EV_INT_IACK);
  356. r3 = handle;
  357. asm volatile("bl epapr_hypercall_start"
  358. : "+r" (r11), "+r" (r3), "=r" (r4)
  359. : : EV_HCALL_CLOBBERS2
  360. );
  361. *vector = r4;
  362. return r3;
  363. }
  364. /**
  365. * ev_doorbell_send - send a doorbell to another partition
  366. * @handle: doorbell send handle
  367. *
  368. * Returns 0 for success, or an error code.
  369. */
  370. static inline unsigned int ev_doorbell_send(unsigned int handle)
  371. {
  372. register uintptr_t r11 __asm__("r11");
  373. register uintptr_t r3 __asm__("r3");
  374. r11 = EV_HCALL_TOKEN(EV_DOORBELL_SEND);
  375. r3 = handle;
  376. asm volatile("bl epapr_hypercall_start"
  377. : "+r" (r11), "+r" (r3)
  378. : : EV_HCALL_CLOBBERS1
  379. );
  380. return r3;
  381. }
  382. /**
  383. * ev_idle -- wait for next interrupt on this core
  384. *
  385. * Returns 0 for success, or an error code.
  386. */
  387. static inline unsigned int ev_idle(void)
  388. {
  389. register uintptr_t r11 __asm__("r11");
  390. register uintptr_t r3 __asm__("r3");
  391. r11 = EV_HCALL_TOKEN(EV_IDLE);
  392. asm volatile("bl epapr_hypercall_start"
  393. : "+r" (r11), "=r" (r3)
  394. : : EV_HCALL_CLOBBERS1
  395. );
  396. return r3;
  397. }
  398. #endif /* !__ASSEMBLY__ */
  399. #endif /* _EPAPR_HCALLS_H */