spike_util.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright (c) 2013, The Regents of the University of California (Regents).
  3. * All Rights Reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * 3. Neither the name of the Regents nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
  17. * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
  18. * OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
  19. * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20. *
  21. * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
  24. * HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
  25. * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  26. */
  27. #include "u.h"
  28. #include "../port/lib.h"
  29. #include "mem.h"
  30. #include "dat.h"
  31. #include "fns.h"
  32. #include "encoding.h"
  33. #include "atomic.h"
  34. #include "spike_util.h"
  35. uintptr_t translate_address(uintptr_t vAddr) {
  36. // TODO: implement the page table translation algorithm
  37. //uintptr_t pageTableRoot = read_csr(sptbr);
  38. uintptr_t physAddrMask = 0xfffffff;
  39. uintptr_t translationResult = vAddr & physAddrMask;
  40. print("Translated virtual address 0x%llx to physical address 0x%llx\n", vAddr, translationResult);
  41. return translationResult;
  42. }
  43. uintptr_t mcall_query_memory(uintptr_t id, memory_block_info *p)
  44. {
  45. uintptr_t physicalAddr = translate_address((uintptr_t) p);
  46. memory_block_info *info = (memory_block_info*) physicalAddr;
  47. if (id == 0) {
  48. info->base = 0x1000000; // hard coded for now, but we can put these values somewhere later
  49. info->size = 0x7F000000 - info->base;
  50. return 0;
  51. }
  52. return -1;
  53. }
  54. #if 0
  55. uintptr_t mcall_send_ipi(uintptr_t recipient)
  56. {
  57. //if (recipient >= num_harts)
  58. //return -1;
  59. if (atomic_swap(&OTHER_HLS(recipient)->ipi_pending, 1) == 0) {
  60. mb();
  61. write_csr(send_ipi, recipient);
  62. }
  63. return 0;
  64. }
  65. uintptr_t mcall_clear_ipi(void)
  66. {
  67. // only clear SSIP if no other events are pending
  68. if (HLS()->device_response_queue_head == NULL) {
  69. clear_csr(mip, MIP_SSIP);
  70. mb();
  71. }
  72. return atomic_swap(&HLS()->ipi_pending, 0);
  73. }
  74. #endif
  75. uintptr_t mcall_shutdown(void)
  76. {
  77. while (1) write_csr(mtohost, 1);
  78. return 0;
  79. }
  80. uintptr_t mcall_set_timer(unsigned long long when)
  81. {
  82. write_csr(mtimecmp, when);
  83. clear_csr(mip, MIP_STIP);
  84. set_csr(mie, MIP_MTIP);
  85. return 0;
  86. }
  87. #if 0
  88. uintptr_t mcall_dev_req(sbi_device_message *m)
  89. {
  90. if ((m->dev > 0xFFU) | (m->cmd > 0xFFU) | (m->data > 0x0000FFFFFFFFFFFFU)) return -EINVAL;
  91. while (swap_csr(mtohost, TOHOST_CMD(m->dev, m->cmd, m->data)) != 0);
  92. m->sbi_private_data = (uintptr_t)HLS()->device_request_queue_head;
  93. HLS()->device_request_queue_head = m;
  94. HLS()->device_request_queue_size++;
  95. return 0;
  96. }
  97. #endif
  98. uintptr_t mcall_dev_resp(void)
  99. {
  100. htif_interrupt(0, 0);
  101. sbi_device_message* m = HLS()->device_response_queue_head;
  102. if (m) {
  103. //printm("resp %p\n", m);
  104. sbi_device_message* next = (void*)atomic_read(&m->sbi_private_data);
  105. HLS()->device_response_queue_head = next;
  106. if (!next) {
  107. HLS()->device_response_queue_tail = 0;
  108. // only clear SSIP if no other events are pending
  109. clear_csr(mip, MIP_SSIP);
  110. mb();
  111. if (HLS()->ipi_pending) set_csr(mip, MIP_SSIP);
  112. }
  113. }
  114. return (uintptr_t)m;
  115. }
  116. uintptr_t mcall_hart_id(void)
  117. {
  118. return HLS()->hart_id;
  119. }
  120. void hls_init(uint32_t hart_id)
  121. {
  122. memset(HLS(), 0, sizeof(*HLS()));
  123. HLS()->hart_id = hart_id;
  124. }
  125. uintptr_t htif_interrupt(uintptr_t mcause, uintptr_t* regs) {
  126. uintptr_t fromhost = swap_csr(mfromhost, 0);
  127. if (!fromhost)
  128. return 0;
  129. uintptr_t dev = FROMHOST_DEV(fromhost);
  130. uintptr_t cmd = FROMHOST_CMD(fromhost);
  131. uintptr_t data = FROMHOST_DATA(fromhost);
  132. sbi_device_message* m = HLS()->device_request_queue_head;
  133. sbi_device_message* prev = 0x0;
  134. unsigned long i, n;
  135. for (i = 0, n = HLS()->device_request_queue_size; i < n; i++) {
  136. /*
  137. if (!supervisor_paddr_valid(m, sizeof(*m))
  138. && EXTRACT_FIELD(read_csr(mstatus), MSTATUS_PRV1) != PRV_M)
  139. panic("htif: page fault");
  140. */
  141. sbi_device_message* next = (void*)m->sbi_private_data;
  142. if (m->dev == dev && m->cmd == cmd) {
  143. m->data = data;
  144. // dequeue from request queue
  145. if (prev)
  146. prev->sbi_private_data = (uintptr_t)next;
  147. else
  148. HLS()->device_request_queue_head = next;
  149. HLS()->device_request_queue_size = n-1;
  150. m->sbi_private_data = 0;
  151. // enqueue to response queue
  152. if (HLS()->device_response_queue_tail)
  153. {
  154. HLS()->device_response_queue_tail->sbi_private_data = (uintptr_t)m;
  155. }
  156. else
  157. {
  158. HLS()->device_response_queue_head = m;
  159. }
  160. HLS()->device_response_queue_tail = m;
  161. // signal software interrupt
  162. set_csr(mip, MIP_SSIP);
  163. return 0;
  164. }
  165. prev = m;
  166. m = (void*)atomic_read(&m->sbi_private_data);
  167. }
  168. //HLT();
  169. return 0;
  170. //panic("htif: no record");
  171. }
  172. uintptr_t mcall_console_putchar(uint8_t ch)
  173. {
  174. while (swap_csr(mtohost, TOHOST_CMD(1, 1, ch)) != 0);
  175. while (1) {
  176. uintptr_t fromhost = read_csr(mfromhost);
  177. if (FROMHOST_DEV(fromhost) != 1 || FROMHOST_CMD(fromhost) != 1) {
  178. if (fromhost)
  179. htif_interrupt(0, 0);
  180. continue;
  181. }
  182. write_csr(mfromhost, 0);
  183. break;
  184. }
  185. return 0;
  186. }
  187. void testPrint(void) {
  188. /* Print a test command to check Spike console output */
  189. mcall_console_putchar('h');
  190. mcall_console_putchar('e');
  191. mcall_console_putchar('l');
  192. mcall_console_putchar('l');
  193. mcall_console_putchar('o');
  194. mcall_console_putchar('\n');
  195. }