spapr_iommu.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 "kvm.h"
  21. #include "qdev.h"
  22. #include "kvm_ppc.h"
  23. #include "dma.h"
  24. #include "hw/spapr.h"
  25. #include <libfdt.h>
  26. /* #define DEBUG_TCE */
  27. enum sPAPRTCEAccess {
  28. SPAPR_TCE_FAULT = 0,
  29. SPAPR_TCE_RO = 1,
  30. SPAPR_TCE_WO = 2,
  31. SPAPR_TCE_RW = 3,
  32. };
  33. typedef struct sPAPRTCETable sPAPRTCETable;
  34. struct sPAPRTCETable {
  35. DMAContext dma;
  36. uint32_t liobn;
  37. uint32_t window_size;
  38. sPAPRTCE *table;
  39. int fd;
  40. QLIST_ENTRY(sPAPRTCETable) list;
  41. };
  42. QLIST_HEAD(spapr_tce_tables, sPAPRTCETable) spapr_tce_tables;
  43. static sPAPRTCETable *spapr_tce_find_by_liobn(uint32_t liobn)
  44. {
  45. sPAPRTCETable *tcet;
  46. QLIST_FOREACH(tcet, &spapr_tce_tables, list) {
  47. if (tcet->liobn == liobn) {
  48. return tcet;
  49. }
  50. }
  51. return NULL;
  52. }
  53. static int spapr_tce_translate(DMAContext *dma,
  54. dma_addr_t addr,
  55. target_phys_addr_t *paddr,
  56. target_phys_addr_t *len,
  57. DMADirection dir)
  58. {
  59. sPAPRTCETable *tcet = DO_UPCAST(sPAPRTCETable, dma, dma);
  60. enum sPAPRTCEAccess access = (dir == DMA_DIRECTION_FROM_DEVICE)
  61. ? SPAPR_TCE_WO : SPAPR_TCE_RO;
  62. uint64_t tce;
  63. #ifdef DEBUG_TCE
  64. fprintf(stderr, "spapr_tce_translate liobn=0x%" PRIx32 " addr=0x"
  65. DMA_ADDR_FMT "\n", tcet->liobn, addr);
  66. #endif
  67. /* Check if we are in bound */
  68. if (addr >= tcet->window_size) {
  69. #ifdef DEBUG_TCE
  70. fprintf(stderr, "spapr_tce_translate out of bounds\n");
  71. #endif
  72. return -EFAULT;
  73. }
  74. tce = tcet->table[addr >> SPAPR_TCE_PAGE_SHIFT].tce;
  75. /* Check TCE */
  76. if (!(tce & access)) {
  77. return -EPERM;
  78. }
  79. /* How much til end of page ? */
  80. *len = ((~addr) & SPAPR_TCE_PAGE_MASK) + 1;
  81. /* Translate */
  82. *paddr = (tce & ~SPAPR_TCE_PAGE_MASK) |
  83. (addr & SPAPR_TCE_PAGE_MASK);
  84. #ifdef DEBUG_TCE
  85. fprintf(stderr, " -> *paddr=0x" TARGET_FMT_plx ", *len=0x"
  86. TARGET_FMT_plx "\n", *paddr, *len);
  87. #endif
  88. return 0;
  89. }
  90. DMAContext *spapr_tce_new_dma_context(uint32_t liobn, size_t window_size)
  91. {
  92. sPAPRTCETable *tcet;
  93. if (!window_size) {
  94. return NULL;
  95. }
  96. tcet = g_malloc0(sizeof(*tcet));
  97. dma_context_init(&tcet->dma, spapr_tce_translate, NULL, NULL);
  98. tcet->liobn = liobn;
  99. tcet->window_size = window_size;
  100. if (kvm_enabled()) {
  101. tcet->table = kvmppc_create_spapr_tce(liobn,
  102. window_size,
  103. &tcet->fd);
  104. }
  105. if (!tcet->table) {
  106. size_t table_size = (window_size >> SPAPR_TCE_PAGE_SHIFT)
  107. * sizeof(sPAPRTCE);
  108. tcet->table = g_malloc0(table_size);
  109. }
  110. #ifdef DEBUG_TCE
  111. fprintf(stderr, "spapr_iommu: New TCE table, liobn=0x%x, context @ %p, "
  112. "table @ %p, fd=%d\n", liobn, &tcet->dma, tcet->table, tcet->fd);
  113. #endif
  114. QLIST_INSERT_HEAD(&spapr_tce_tables, tcet, list);
  115. return &tcet->dma;
  116. }
  117. void spapr_tce_free(DMAContext *dma)
  118. {
  119. if (dma) {
  120. sPAPRTCETable *tcet = DO_UPCAST(sPAPRTCETable, dma, dma);
  121. QLIST_REMOVE(tcet, list);
  122. if (!kvm_enabled() ||
  123. (kvmppc_remove_spapr_tce(tcet->table, tcet->fd,
  124. tcet->window_size) != 0)) {
  125. g_free(tcet->table);
  126. }
  127. g_free(tcet);
  128. }
  129. }
  130. static target_ulong put_tce_emu(sPAPRTCETable *tcet, target_ulong ioba,
  131. target_ulong tce)
  132. {
  133. sPAPRTCE *tcep;
  134. if (ioba >= tcet->window_size) {
  135. hcall_dprintf("spapr_vio_put_tce on out-of-boards IOBA 0x"
  136. TARGET_FMT_lx "\n", ioba);
  137. return H_PARAMETER;
  138. }
  139. tcep = tcet->table + (ioba >> SPAPR_TCE_PAGE_SHIFT);
  140. tcep->tce = tce;
  141. return H_SUCCESS;
  142. }
  143. static target_ulong h_put_tce(CPUPPCState *env, sPAPREnvironment *spapr,
  144. target_ulong opcode, target_ulong *args)
  145. {
  146. target_ulong liobn = args[0];
  147. target_ulong ioba = args[1];
  148. target_ulong tce = args[2];
  149. sPAPRTCETable *tcet = spapr_tce_find_by_liobn(liobn);
  150. if (liobn & 0xFFFFFFFF00000000ULL) {
  151. hcall_dprintf("spapr_vio_put_tce on out-of-boundsw LIOBN "
  152. TARGET_FMT_lx "\n", liobn);
  153. return H_PARAMETER;
  154. }
  155. ioba &= ~(SPAPR_TCE_PAGE_SIZE - 1);
  156. if (tcet) {
  157. return put_tce_emu(tcet, ioba, tce);
  158. }
  159. #ifdef DEBUG_TCE
  160. fprintf(stderr, "%s on liobn=" TARGET_FMT_lx /*%s*/
  161. " ioba 0x" TARGET_FMT_lx " TCE 0x" TARGET_FMT_lx "\n",
  162. __func__, liobn, /*dev->qdev.id, */ioba, tce);
  163. #endif
  164. return H_PARAMETER;
  165. }
  166. void spapr_iommu_init(void)
  167. {
  168. QLIST_INIT(&spapr_tce_tables);
  169. /* hcall-tce */
  170. spapr_register_hypercall(H_PUT_TCE, h_put_tce);
  171. }
  172. int spapr_dma_dt(void *fdt, int node_off, const char *propname,
  173. uint32_t liobn, uint64_t window, uint32_t size)
  174. {
  175. uint32_t dma_prop[5];
  176. int ret;
  177. dma_prop[0] = cpu_to_be32(liobn);
  178. dma_prop[1] = cpu_to_be32(window >> 32);
  179. dma_prop[2] = cpu_to_be32(window & 0xFFFFFFFF);
  180. dma_prop[3] = 0; /* window size is 32 bits */
  181. dma_prop[4] = cpu_to_be32(size);
  182. ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-address-cells", 2);
  183. if (ret < 0) {
  184. return ret;
  185. }
  186. ret = fdt_setprop_cell(fdt, node_off, "ibm,#dma-size-cells", 2);
  187. if (ret < 0) {
  188. return ret;
  189. }
  190. ret = fdt_setprop(fdt, node_off, propname, dma_prop, sizeof(dma_prop));
  191. if (ret < 0) {
  192. return ret;
  193. }
  194. return 0;
  195. }
  196. int spapr_tcet_dma_dt(void *fdt, int node_off, const char *propname,
  197. DMAContext *iommu)
  198. {
  199. if (!iommu) {
  200. return 0;
  201. }
  202. if (iommu->translate == spapr_tce_translate) {
  203. sPAPRTCETable *tcet = DO_UPCAST(sPAPRTCETable, dma, iommu);
  204. return spapr_dma_dt(fdt, node_off, propname,
  205. tcet->liobn, 0, tcet->window_size);
  206. }
  207. return -1;
  208. }