epapr_hcalls.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. * The "memory" clobber is only necessary for hcalls where the Hypervisor
  129. * will read or write guest memory. However, we add it to all hcalls because
  130. * the impact is minimal, and we want to ensure that it's present for the
  131. * hcalls that need it.
  132. */
  133. /* List of common clobbered registers. Do not use this macro. */
  134. #define EV_HCALL_CLOBBERS "r0", "r12", "xer", "ctr", "lr", "cc", "memory"
  135. #define EV_HCALL_CLOBBERS8 EV_HCALL_CLOBBERS
  136. #define EV_HCALL_CLOBBERS7 EV_HCALL_CLOBBERS8, "r10"
  137. #define EV_HCALL_CLOBBERS6 EV_HCALL_CLOBBERS7, "r9"
  138. #define EV_HCALL_CLOBBERS5 EV_HCALL_CLOBBERS6, "r8"
  139. #define EV_HCALL_CLOBBERS4 EV_HCALL_CLOBBERS5, "r7"
  140. #define EV_HCALL_CLOBBERS3 EV_HCALL_CLOBBERS4, "r6"
  141. #define EV_HCALL_CLOBBERS2 EV_HCALL_CLOBBERS3, "r5"
  142. #define EV_HCALL_CLOBBERS1 EV_HCALL_CLOBBERS2, "r4"
  143. /*
  144. * We use "uintptr_t" to define a register because it's guaranteed to be a
  145. * 32-bit integer on a 32-bit platform, and a 64-bit integer on a 64-bit
  146. * platform.
  147. *
  148. * All registers are either input/output or output only. Registers that are
  149. * initialized before making the hypercall are input/output. All
  150. * input/output registers are represented with "+r". Output-only registers
  151. * are represented with "=r". Do not specify any unused registers. The
  152. * clobber list will tell the compiler that the hypercall modifies those
  153. * registers, which is good enough.
  154. */
  155. /**
  156. * ev_int_set_config - configure the specified interrupt
  157. * @interrupt: the interrupt number
  158. * @config: configuration for this interrupt
  159. * @priority: interrupt priority
  160. * @destination: destination CPU number
  161. *
  162. * Returns 0 for success, or an error code.
  163. */
  164. static inline unsigned int ev_int_set_config(unsigned int interrupt,
  165. uint32_t config, unsigned int priority, uint32_t destination)
  166. {
  167. register uintptr_t r11 __asm__("r11");
  168. register uintptr_t r3 __asm__("r3");
  169. register uintptr_t r4 __asm__("r4");
  170. register uintptr_t r5 __asm__("r5");
  171. register uintptr_t r6 __asm__("r6");
  172. r11 = EV_HCALL_TOKEN(EV_INT_SET_CONFIG);
  173. r3 = interrupt;
  174. r4 = config;
  175. r5 = priority;
  176. r6 = destination;
  177. __asm__ __volatile__ ("sc 1"
  178. : "+r" (r11), "+r" (r3), "+r" (r4), "+r" (r5), "+r" (r6)
  179. : : EV_HCALL_CLOBBERS4
  180. );
  181. return r3;
  182. }
  183. /**
  184. * ev_int_get_config - return the config of the specified interrupt
  185. * @interrupt: the interrupt number
  186. * @config: returned configuration for this interrupt
  187. * @priority: returned interrupt priority
  188. * @destination: returned destination CPU number
  189. *
  190. * Returns 0 for success, or an error code.
  191. */
  192. static inline unsigned int ev_int_get_config(unsigned int interrupt,
  193. uint32_t *config, unsigned int *priority, uint32_t *destination)
  194. {
  195. register uintptr_t r11 __asm__("r11");
  196. register uintptr_t r3 __asm__("r3");
  197. register uintptr_t r4 __asm__("r4");
  198. register uintptr_t r5 __asm__("r5");
  199. register uintptr_t r6 __asm__("r6");
  200. r11 = EV_HCALL_TOKEN(EV_INT_GET_CONFIG);
  201. r3 = interrupt;
  202. __asm__ __volatile__ ("sc 1"
  203. : "+r" (r11), "+r" (r3), "=r" (r4), "=r" (r5), "=r" (r6)
  204. : : EV_HCALL_CLOBBERS4
  205. );
  206. *config = r4;
  207. *priority = r5;
  208. *destination = r6;
  209. return r3;
  210. }
  211. /**
  212. * ev_int_set_mask - sets the mask for the specified interrupt source
  213. * @interrupt: the interrupt number
  214. * @mask: 0=enable interrupts, 1=disable interrupts
  215. *
  216. * Returns 0 for success, or an error code.
  217. */
  218. static inline unsigned int ev_int_set_mask(unsigned int interrupt,
  219. unsigned int mask)
  220. {
  221. register uintptr_t r11 __asm__("r11");
  222. register uintptr_t r3 __asm__("r3");
  223. register uintptr_t r4 __asm__("r4");
  224. r11 = EV_HCALL_TOKEN(EV_INT_SET_MASK);
  225. r3 = interrupt;
  226. r4 = mask;
  227. __asm__ __volatile__ ("sc 1"
  228. : "+r" (r11), "+r" (r3), "+r" (r4)
  229. : : EV_HCALL_CLOBBERS2
  230. );
  231. return r3;
  232. }
  233. /**
  234. * ev_int_get_mask - returns the mask for the specified interrupt source
  235. * @interrupt: the interrupt number
  236. * @mask: returned mask for this interrupt (0=enabled, 1=disabled)
  237. *
  238. * Returns 0 for success, or an error code.
  239. */
  240. static inline unsigned int ev_int_get_mask(unsigned int interrupt,
  241. unsigned int *mask)
  242. {
  243. register uintptr_t r11 __asm__("r11");
  244. register uintptr_t r3 __asm__("r3");
  245. register uintptr_t r4 __asm__("r4");
  246. r11 = EV_HCALL_TOKEN(EV_INT_GET_MASK);
  247. r3 = interrupt;
  248. __asm__ __volatile__ ("sc 1"
  249. : "+r" (r11), "+r" (r3), "=r" (r4)
  250. : : EV_HCALL_CLOBBERS2
  251. );
  252. *mask = r4;
  253. return r3;
  254. }
  255. /**
  256. * ev_int_eoi - signal the end of interrupt processing
  257. * @interrupt: the interrupt number
  258. *
  259. * This function signals the end of processing for the the specified
  260. * interrupt, which must be the interrupt currently in service. By
  261. * definition, this is also the highest-priority interrupt.
  262. *
  263. * Returns 0 for success, or an error code.
  264. */
  265. static inline unsigned int ev_int_eoi(unsigned int interrupt)
  266. {
  267. register uintptr_t r11 __asm__("r11");
  268. register uintptr_t r3 __asm__("r3");
  269. r11 = EV_HCALL_TOKEN(EV_INT_EOI);
  270. r3 = interrupt;
  271. __asm__ __volatile__ ("sc 1"
  272. : "+r" (r11), "+r" (r3)
  273. : : EV_HCALL_CLOBBERS1
  274. );
  275. return r3;
  276. }
  277. /**
  278. * ev_byte_channel_send - send characters to a byte stream
  279. * @handle: byte stream handle
  280. * @count: (input) num of chars to send, (output) num chars sent
  281. * @buffer: pointer to a 16-byte buffer
  282. *
  283. * @buffer must be at least 16 bytes long, because all 16 bytes will be
  284. * read from memory into registers, even if count < 16.
  285. *
  286. * Returns 0 for success, or an error code.
  287. */
  288. static inline unsigned int ev_byte_channel_send(unsigned int handle,
  289. unsigned int *count, const char buffer[EV_BYTE_CHANNEL_MAX_BYTES])
  290. {
  291. register uintptr_t r11 __asm__("r11");
  292. register uintptr_t r3 __asm__("r3");
  293. register uintptr_t r4 __asm__("r4");
  294. register uintptr_t r5 __asm__("r5");
  295. register uintptr_t r6 __asm__("r6");
  296. register uintptr_t r7 __asm__("r7");
  297. register uintptr_t r8 __asm__("r8");
  298. const uint32_t *p = (const uint32_t *) buffer;
  299. r11 = EV_HCALL_TOKEN(EV_BYTE_CHANNEL_SEND);
  300. r3 = handle;
  301. r4 = *count;
  302. r5 = be32_to_cpu(p[0]);
  303. r6 = be32_to_cpu(p[1]);
  304. r7 = be32_to_cpu(p[2]);
  305. r8 = be32_to_cpu(p[3]);
  306. __asm__ __volatile__ ("sc 1"
  307. : "+r" (r11), "+r" (r3),
  308. "+r" (r4), "+r" (r5), "+r" (r6), "+r" (r7), "+r" (r8)
  309. : : EV_HCALL_CLOBBERS6
  310. );
  311. *count = r4;
  312. return r3;
  313. }
  314. /**
  315. * ev_byte_channel_receive - fetch characters from a byte channel
  316. * @handle: byte channel handle
  317. * @count: (input) max num of chars to receive, (output) num chars received
  318. * @buffer: pointer to a 16-byte buffer
  319. *
  320. * The size of @buffer must be at least 16 bytes, even if you request fewer
  321. * than 16 characters, because we always write 16 bytes to @buffer. This is
  322. * for performance reasons.
  323. *
  324. * Returns 0 for success, or an error code.
  325. */
  326. static inline unsigned int ev_byte_channel_receive(unsigned int handle,
  327. unsigned int *count, char buffer[EV_BYTE_CHANNEL_MAX_BYTES])
  328. {
  329. register uintptr_t r11 __asm__("r11");
  330. register uintptr_t r3 __asm__("r3");
  331. register uintptr_t r4 __asm__("r4");
  332. register uintptr_t r5 __asm__("r5");
  333. register uintptr_t r6 __asm__("r6");
  334. register uintptr_t r7 __asm__("r7");
  335. register uintptr_t r8 __asm__("r8");
  336. uint32_t *p = (uint32_t *) buffer;
  337. r11 = EV_HCALL_TOKEN(EV_BYTE_CHANNEL_RECEIVE);
  338. r3 = handle;
  339. r4 = *count;
  340. __asm__ __volatile__ ("sc 1"
  341. : "+r" (r11), "+r" (r3), "+r" (r4),
  342. "=r" (r5), "=r" (r6), "=r" (r7), "=r" (r8)
  343. : : EV_HCALL_CLOBBERS6
  344. );
  345. *count = r4;
  346. p[0] = cpu_to_be32(r5);
  347. p[1] = cpu_to_be32(r6);
  348. p[2] = cpu_to_be32(r7);
  349. p[3] = cpu_to_be32(r8);
  350. return r3;
  351. }
  352. /**
  353. * ev_byte_channel_poll - returns the status of the byte channel buffers
  354. * @handle: byte channel handle
  355. * @rx_count: returned count of bytes in receive queue
  356. * @tx_count: returned count of free space in transmit queue
  357. *
  358. * This function reports the amount of data in the receive queue (i.e. the
  359. * number of bytes you can read), and the amount of free space in the transmit
  360. * queue (i.e. the number of bytes you can write).
  361. *
  362. * Returns 0 for success, or an error code.
  363. */
  364. static inline unsigned int ev_byte_channel_poll(unsigned int handle,
  365. unsigned int *rx_count, unsigned int *tx_count)
  366. {
  367. register uintptr_t r11 __asm__("r11");
  368. register uintptr_t r3 __asm__("r3");
  369. register uintptr_t r4 __asm__("r4");
  370. register uintptr_t r5 __asm__("r5");
  371. r11 = EV_HCALL_TOKEN(EV_BYTE_CHANNEL_POLL);
  372. r3 = handle;
  373. __asm__ __volatile__ ("sc 1"
  374. : "+r" (r11), "+r" (r3), "=r" (r4), "=r" (r5)
  375. : : EV_HCALL_CLOBBERS3
  376. );
  377. *rx_count = r4;
  378. *tx_count = r5;
  379. return r3;
  380. }
  381. /**
  382. * ev_int_iack - acknowledge an interrupt
  383. * @handle: handle to the target interrupt controller
  384. * @vector: returned interrupt vector
  385. *
  386. * If handle is zero, the function returns the next interrupt source
  387. * number to be handled irrespective of the hierarchy or cascading
  388. * of interrupt controllers. If non-zero, specifies a handle to the
  389. * interrupt controller that is the target of the acknowledge.
  390. *
  391. * Returns 0 for success, or an error code.
  392. */
  393. static inline unsigned int ev_int_iack(unsigned int handle,
  394. unsigned int *vector)
  395. {
  396. register uintptr_t r11 __asm__("r11");
  397. register uintptr_t r3 __asm__("r3");
  398. register uintptr_t r4 __asm__("r4");
  399. r11 = EV_HCALL_TOKEN(EV_INT_IACK);
  400. r3 = handle;
  401. __asm__ __volatile__ ("sc 1"
  402. : "+r" (r11), "+r" (r3), "=r" (r4)
  403. : : EV_HCALL_CLOBBERS2
  404. );
  405. *vector = r4;
  406. return r3;
  407. }
  408. /**
  409. * ev_doorbell_send - send a doorbell to another partition
  410. * @handle: doorbell send handle
  411. *
  412. * Returns 0 for success, or an error code.
  413. */
  414. static inline unsigned int ev_doorbell_send(unsigned int handle)
  415. {
  416. register uintptr_t r11 __asm__("r11");
  417. register uintptr_t r3 __asm__("r3");
  418. r11 = EV_HCALL_TOKEN(EV_DOORBELL_SEND);
  419. r3 = handle;
  420. __asm__ __volatile__ ("sc 1"
  421. : "+r" (r11), "+r" (r3)
  422. : : EV_HCALL_CLOBBERS1
  423. );
  424. return r3;
  425. }
  426. /**
  427. * ev_idle -- wait for next interrupt on this core
  428. *
  429. * Returns 0 for success, or an error code.
  430. */
  431. static inline unsigned int ev_idle(void)
  432. {
  433. register uintptr_t r11 __asm__("r11");
  434. register uintptr_t r3 __asm__("r3");
  435. r11 = EV_HCALL_TOKEN(EV_IDLE);
  436. __asm__ __volatile__ ("sc 1"
  437. : "+r" (r11), "=r" (r3)
  438. : : EV_HCALL_CLOBBERS1
  439. );
  440. return r3;
  441. }
  442. #endif