2
0

spapr_iommu.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * QEMU sPAPR IOMMU (TCE) code
  3. *
  4. * Copyright (c) 2010 David Gibson, IBM Corporation <dwg@au1.ibm.com>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "hw.h"
  20. #include "sysemu/kvm.h"
  21. #include "qdev.h"
  22. #include "kvm_ppc.h"
  23. #include "sysemu/dma.h"
  24. #include "exec/address-spaces.h"
  25. #include "hw/spapr.h"
  26. #include <libfdt.h>
  27. /* #define DEBUG_TCE */
  28. enum sPAPRTCEAccess {
  29. SPAPR_TCE_FAULT = 0,
  30. SPAPR_TCE_RO = 1,
  31. SPAPR_TCE_WO = 2,
  32. SPAPR_TCE_RW = 3,
  33. };
  34. typedef struct sPAPRTCETable sPAPRTCETable;
  35. struct sPAPRTCETable {
  36. DMAContext dma;
  37. uint32_t liobn;
  38. uint32_t window_size;
  39. sPAPRTCE *table;
  40. bool bypass;
  41. int fd;
  42. QLIST_ENTRY(sPAPRTCETable) list;
  43. };
  44. QLIST_HEAD(spapr_tce_tables, sPAPRTCETable) spapr_tce_tables;
  45. static sPAPRTCETable *spapr_tce_find_by_liobn(uint32_t liobn)
  46. {
  47. sPAPRTCETable *tcet;
  48. QLIST_FOREACH(tcet, &spapr_tce_tables, list) {
  49. if (tcet->liobn == liobn) {
  50. return tcet;
  51. }
  52. }
  53. return NULL;
  54. }
  55. static int spapr_tce_translate(DMAContext *dma,
  56. dma_addr_t addr,
  57. hwaddr *paddr,
  58. hwaddr *len,
  59. DMADirection dir)
  60. {
  61. sPAPRTCETable *tcet = DO_UPCAST(sPAPRTCETable, dma, dma);
  62. enum sPAPRTCEAccess access = (dir == DMA_DIRECTION_FROM_DEVICE)
  63. ? SPAPR_TCE_WO : SPAPR_TCE_RO;
  64. uint64_t tce;
  65. #ifdef DEBUG_TCE
  66. fprintf(stderr, "spapr_tce_translate liobn=0x%" PRIx32 " addr=0x"
  67. DMA_ADDR_FMT "\n", tcet->liobn, addr);
  68. #endif
  69. if (tcet->bypass) {
  70. *paddr = addr;
  71. *len = (hwaddr)-1;
  72. return 0;
  73. }
  74. /* Check if we are in bound */
  75. if (addr >= tcet->window_size) {
  76. #ifdef DEBUG_TCE
  77. fprintf(stderr, "spapr_tce_translate out of bounds\n");
  78. #endif
  79. return -EFAULT;
  80. }
  81. tce = tcet->table[addr >> SPAPR_TCE_PAGE_SHIFT].tce;
  82. /* Check TCE */
  83. if (!(tce & access)) {
  84. return -EPERM;
  85. }
  86. /* How much til end of page ? */
  87. *len = ((~addr) & SPAPR_TCE_PAGE_MASK) + 1;
  88. /* Translate */
  89. *paddr = (tce & ~SPAPR_TCE_PAGE_MASK) |
  90. (addr & SPAPR_TCE_PAGE_MASK);
  91. #ifdef DEBUG_TCE
  92. fprintf(stderr, " -> *paddr=0x" TARGET_FMT_plx ", *len=0x"
  93. TARGET_FMT_plx "\n", *paddr, *len);
  94. #endif
  95. return 0;
  96. }
  97. DMAContext *spapr_tce_new_dma_context(uint32_t liobn, size_t window_size)
  98. {
  99. sPAPRTCETable *tcet;
  100. if (spapr_tce_find_by_liobn(liobn)) {
  101. fprintf(stderr, "Attempted to create TCE table with duplicate"
  102. " LIOBN 0x%x\n", liobn);
  103. return NULL;
  104. }
  105. if (!window_size) {
  106. return NULL;
  107. }
  108. tcet = g_malloc0(sizeof(*tcet));
  109. dma_context_init(&tcet->dma, &address_space_memory, spapr_tce_translate, NULL, NULL);
  110. tcet->liobn = liobn;
  111. tcet->window_size = window_size;
  112. if (kvm_enabled()) {
  113. tcet->table = kvmppc_create_spapr_tce(liobn,
  114. window_size,
  115. &tcet->fd);
  116. }
  117. if (!tcet->table) {
  118. size_t table_size = (window_size >> SPAPR_TCE_PAGE_SHIFT)
  119. * sizeof(sPAPRTCE);
  120. tcet->table = g_malloc0(table_size);
  121. }
  122. #ifdef DEBUG_TCE
  123. fprintf(stderr, "spapr_iommu: New TCE table, liobn=0x%x, context @ %p, "
  124. "table @ %p, fd=%d\n", liobn, &tcet->dma, tcet->table, tcet->fd);
  125. #endif
  126. QLIST_INSERT_HEAD(&spapr_tce_tables, tcet, list);
  127. return &tcet->dma;
  128. }
  129. void spapr_tce_free(DMAContext *dma)
  130. {
  131. if (dma) {
  132. sPAPRTCETable *tcet = DO_UPCAST(sPAPRTCETable, dma, dma);
  133. QLIST_REMOVE(tcet, list);
  134. if (!kvm_enabled() ||
  135. (kvmppc_remove_spapr_tce(tcet->table, tcet->fd,
  136. tcet->window_size) != 0)) {
  137. g_free(tcet->table);
  138. }
  139. g_free(tcet);
  140. }
  141. }
  142. void spapr_tce_set_bypass(DMAContext *dma, bool bypass)
  143. {
  144. sPAPRTCETable *tcet = DO_UPCAST(sPAPRTCETable, dma, dma);
  145. tcet->bypass = bypass;
  146. }
  147. void spapr_tce_reset(DMAContext *dma)
  148. {
  149. sPAPRTCETable *tcet = DO_UPCAST(sPAPRTCETable, dma, dma);
  150. size_t table_size = (tcet->window_size >> SPAPR_TCE_PAGE_SHIFT)
  151. * sizeof(sPAPRTCE);
  152. tcet->bypass = false;
  153. memset(tcet->table, 0, table_size);
  154. }
  155. static target_ulong put_tce_emu(sPAPRTCETable *tcet, target_ulong ioba,
  156. target_ulong tce)
  157. {
  158. sPAPRTCE *tcep;
  159. if (ioba >= tcet->window_size) {
  160. hcall_dprintf("spapr_vio_put_tce on out-of-boards IOBA 0x"
  161. TARGET_FMT_lx "\n", ioba);
  162. return H_PARAMETER;
  163. }
  164. tcep = tcet->table + (ioba >> SPAPR_TCE_PAGE_SHIFT);
  165. tcep->tce = tce;
  166. return H_SUCCESS;
  167. }
  168. static target_ulong h_put_tce(PowerPCCPU *cpu, sPAPREnvironment *spapr,
  169. target_ulong opcode, target_ulong *args)
  170. {
  171. target_ulong liobn = args[0];
  172. target_ulong ioba = args[1];
  173. target_ulong tce = args[2];
  174. sPAPRTCETable *tcet = spapr_tce_find_by_liobn(liobn);
  175. if (liobn & 0xFFFFFFFF00000000ULL) {
  176. hcall_dprintf("spapr_vio_put_tce on out-of-boundsw LIOBN "
  177. TARGET_FMT_lx "\n", liobn);
  178. return H_PARAMETER;
  179. }
  180. ioba &= ~(SPAPR_TCE_PAGE_SIZE - 1);
  181. if (tcet) {
  182. return put_tce_emu(tcet, ioba, tce);
  183. }
  184. #ifdef DEBUG_TCE
  185. fprintf(stderr, "%s on liobn=" TARGET_FMT_lx /*%s*/
  186. " ioba 0x" TARGET_FMT_lx " TCE 0x" TARGET_FMT_lx "\n",
  187. __func__, liobn, /*dev->qdev.id, */ioba, tce);
  188. #endif
  189. return H_PARAMETER;
  190. }
  191. void spapr_iommu_init(void)
  192. {
  193. QLIST_INIT(&spapr_tce_tables);
  194. /* hcall-tce */
  195. spapr_register_hypercall(H_PUT_TCE, h_put_tce);
  196. }
  197. int spapr_dma_dt(void *fdt, int node_off, const char *propname,
  198. uint32_t liobn, uint64_t window, uint32_t size)
  199. {
  200. uint32_t dma_prop[5];
  201. int ret;
  202. dma_prop[0] = cpu_to_be32(liobn);
  203. dma_prop[1] = cpu_to_be32(window >> 32);
  204. dma_prop[2] = cpu_to_be32(window & 0xFFFFFFFF);
  205. dma_prop[3] = 0; /* window size is 32 bits */
  206. dma_prop[4] = cpu_to_be32(size);
  207. ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-address-cells", 2);
  208. if (ret < 0) {
  209. return ret;
  210. }
  211. ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-size-cells", 2);
  212. if (ret < 0) {
  213. return ret;
  214. }
  215. ret = fdt_setprop(fdt, node_off, propname, dma_prop, sizeof(dma_prop));
  216. if (ret < 0) {
  217. return ret;
  218. }
  219. return 0;
  220. }
  221. int spapr_tcet_dma_dt(void *fdt, int node_off, const char *propname,
  222. DMAContext *iommu)
  223. {
  224. if (!iommu) {
  225. return 0;
  226. }
  227. if (iommu->translate == spapr_tce_translate) {
  228. sPAPRTCETable *tcet = DO_UPCAST(sPAPRTCETable, dma, iommu);
  229. return spapr_dma_dt(fdt, node_off, propname,
  230. tcet->liobn, 0, tcet->window_size);
  231. }
  232. return -1;
  233. }