rtas.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright (c) 2011 Nathan Whitehorn
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. #include <sys/cdefs.h>
  29. __FBSDID("$FreeBSD$");
  30. #include <sys/endian.h>
  31. #include <sys/param.h>
  32. #include <sys/kernel.h>
  33. #include <sys/lock.h>
  34. #include <sys/mutex.h>
  35. #include <sys/systm.h>
  36. #include <sys/proc.h>
  37. #include <vm/vm.h>
  38. #include <vm/vm_page.h>
  39. #include <vm/pmap.h>
  40. #include <machine/bus.h>
  41. #include <machine/md_var.h>
  42. #include <machine/pcb.h>
  43. #include <machine/rtas.h>
  44. #include <machine/stdarg.h>
  45. #include <dev/ofw/openfirm.h>
  46. static MALLOC_DEFINE(M_RTAS, "rtas", "Run Time Abstraction Service");
  47. static vm_offset_t rtas_bounce_phys;
  48. static caddr_t rtas_bounce_virt;
  49. static off_t rtas_bounce_offset;
  50. static size_t rtas_bounce_size;
  51. static uintptr_t rtas_private_data;
  52. static struct mtx rtas_mtx;
  53. static phandle_t rtas;
  54. /* From ofwcall.S */
  55. int rtascall(vm_offset_t callbuffer, uintptr_t rtas_privdat);
  56. extern uintptr_t rtas_entry;
  57. extern register_t rtasmsr;
  58. /*
  59. * After the VM is up, allocate RTAS memory and instantiate it
  60. */
  61. static void rtas_setup(void *);
  62. SYSINIT(rtas_setup, SI_SUB_KMEM, SI_ORDER_ANY, rtas_setup, NULL);
  63. static void
  64. rtas_setup(void *junk)
  65. {
  66. ihandle_t rtasi;
  67. cell_t rtas_size = 0, rtas_ptr;
  68. char path[31];
  69. int result;
  70. rtas = OF_finddevice("/rtas");
  71. if (rtas == -1) {
  72. rtas = 0;
  73. return;
  74. }
  75. OF_package_to_path(rtas, path, sizeof(path));
  76. mtx_init(&rtas_mtx, "RTAS", NULL, MTX_SPIN);
  77. /* RTAS must be called with everything turned off in MSR */
  78. rtasmsr = mfmsr();
  79. rtasmsr &= ~(PSL_IR | PSL_DR | PSL_EE | PSL_SE | PSL_LE);
  80. #ifdef __powerpc64__
  81. rtasmsr &= ~PSL_SF;
  82. #endif
  83. /*
  84. * Allocate rtas_size + one page of contiguous, wired physical memory
  85. * that can fit into a 32-bit address space and accessed from real mode.
  86. * This is used both to bounce arguments and for RTAS private data.
  87. *
  88. * It must be 4KB-aligned and not cross a 256 MB boundary.
  89. */
  90. OF_getencprop(rtas, "rtas-size", &rtas_size, sizeof(rtas_size));
  91. rtas_size = round_page(rtas_size);
  92. rtas_bounce_virt = contigmalloc(rtas_size + PAGE_SIZE, M_RTAS, 0, 0,
  93. ulmin(platform_real_maxaddr(), BUS_SPACE_MAXADDR_32BIT),
  94. 4096, 256*1024*1024);
  95. rtas_private_data = vtophys(rtas_bounce_virt);
  96. rtas_bounce_virt += rtas_size; /* Actual bounce area */
  97. rtas_bounce_phys = vtophys(rtas_bounce_virt);
  98. rtas_bounce_size = PAGE_SIZE;
  99. /*
  100. * Instantiate RTAS. We always use the 32-bit version.
  101. */
  102. if (OF_hasprop(rtas, "linux,rtas-entry") &&
  103. OF_hasprop(rtas, "linux,rtas-base")) {
  104. OF_getencprop(rtas, "linux,rtas-base", &rtas_ptr,
  105. sizeof(rtas_ptr));
  106. rtas_private_data = rtas_ptr;
  107. OF_getencprop(rtas, "linux,rtas-entry", &rtas_ptr,
  108. sizeof(rtas_ptr));
  109. } else {
  110. rtasi = OF_open(path);
  111. if (rtasi == 0) {
  112. rtas = 0;
  113. printf("Error initializing RTAS: could not open "
  114. "node\n");
  115. return;
  116. }
  117. result = OF_call_method("instantiate-rtas", rtasi, 1, 1,
  118. (cell_t)rtas_private_data, &rtas_ptr);
  119. OF_close(rtasi);
  120. if (result != 0) {
  121. rtas = 0;
  122. rtas_ptr = 0;
  123. printf("Error initializing RTAS (%d)\n", result);
  124. return;
  125. }
  126. }
  127. rtas_entry = (uintptr_t)(rtas_ptr);
  128. }
  129. static cell_t
  130. rtas_real_map(const void *buf, size_t len)
  131. {
  132. cell_t phys;
  133. mtx_assert(&rtas_mtx, MA_OWNED);
  134. /*
  135. * Make sure the bounce page offset satisfies any reasonable
  136. * alignment constraint.
  137. */
  138. rtas_bounce_offset += sizeof(register_t) -
  139. (rtas_bounce_offset % sizeof(register_t));
  140. if (rtas_bounce_offset + len > rtas_bounce_size) {
  141. panic("Oversize RTAS call!");
  142. return 0;
  143. }
  144. if (buf != NULL)
  145. memcpy(rtas_bounce_virt + rtas_bounce_offset, buf, len);
  146. else
  147. return (0);
  148. phys = rtas_bounce_phys + rtas_bounce_offset;
  149. rtas_bounce_offset += len;
  150. return (phys);
  151. }
  152. static void
  153. rtas_real_unmap(cell_t physaddr, void *buf, size_t len)
  154. {
  155. mtx_assert(&rtas_mtx, MA_OWNED);
  156. if (physaddr == 0)
  157. return;
  158. memcpy(buf, rtas_bounce_virt + (physaddr - rtas_bounce_phys), len);
  159. }
  160. /* Check if we have RTAS */
  161. int
  162. rtas_exists(void)
  163. {
  164. return (rtas != 0);
  165. }
  166. /* Call an RTAS method by token */
  167. int
  168. rtas_call_method(cell_t token, int nargs, int nreturns, ...)
  169. {
  170. vm_offset_t argsptr;
  171. jmp_buf env, *oldfaultbuf;
  172. va_list ap;
  173. struct {
  174. cell_t token;
  175. cell_t nargs;
  176. cell_t nreturns;
  177. cell_t args_n_results[12];
  178. } args;
  179. int n, result;
  180. if (!rtas_exists() || nargs + nreturns > 12)
  181. return (-1);
  182. args.token = htobe32(token);
  183. va_start(ap, nreturns);
  184. mtx_lock_spin(&rtas_mtx);
  185. rtas_bounce_offset = 0;
  186. args.nargs = htobe32(nargs);
  187. args.nreturns = htobe32(nreturns);
  188. for (n = 0; n < nargs; n++)
  189. args.args_n_results[n] = htobe32(va_arg(ap, cell_t));
  190. argsptr = rtas_real_map(&args, sizeof(args));
  191. /* Get rid of any stale machine checks that have been waiting. */
  192. __asm __volatile ("sync; isync");
  193. oldfaultbuf = curthread->td_pcb->pcb_onfault;
  194. curthread->td_pcb->pcb_onfault = &env;
  195. if (!setjmp(env)) {
  196. __asm __volatile ("sync");
  197. result = rtascall(argsptr, rtas_private_data);
  198. __asm __volatile ("sync; isync");
  199. } else {
  200. result = RTAS_HW_ERROR;
  201. }
  202. curthread->td_pcb->pcb_onfault = oldfaultbuf;
  203. __asm __volatile ("sync");
  204. rtas_real_unmap(argsptr, &args, sizeof(args));
  205. mtx_unlock_spin(&rtas_mtx);
  206. if (result < 0)
  207. return (result);
  208. for (n = nargs; n < nargs + nreturns; n++)
  209. *va_arg(ap, cell_t *) = be32toh(args.args_n_results[n]);
  210. return (result);
  211. }
  212. /* Look up an RTAS token */
  213. cell_t
  214. rtas_token_lookup(const char *method)
  215. {
  216. cell_t token;
  217. if (!rtas_exists())
  218. return (-1);
  219. if (OF_getencprop(rtas, method, &token, sizeof(token)) == -1)
  220. return (-1);
  221. return (token);
  222. }