epapr_hcalls.h 16 KB

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