epapr_hcalls.h 16 KB

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