2
0

sun4u_iommu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * QEMU sun4u IOMMU emulation
  3. *
  4. * Copyright (c) 2006 Fabrice Bellard
  5. * Copyright (c) 2012,2013 Artyom Tarasenko
  6. * Copyright (c) 2017 Mark Cave-Ayland
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "qemu/osdep.h"
  27. #include "hw/sysbus.h"
  28. #include "hw/sparc/sun4u_iommu.h"
  29. #include "exec/address-spaces.h"
  30. #include "qemu/log.h"
  31. #include "qemu/module.h"
  32. #include "trace.h"
  33. #define IOMMU_PAGE_SIZE_8K (1ULL << 13)
  34. #define IOMMU_PAGE_MASK_8K (~(IOMMU_PAGE_SIZE_8K - 1))
  35. #define IOMMU_PAGE_SIZE_64K (1ULL << 16)
  36. #define IOMMU_PAGE_MASK_64K (~(IOMMU_PAGE_SIZE_64K - 1))
  37. #define IOMMU_CTRL 0x0
  38. #define IOMMU_CTRL_TBW_SIZE (1ULL << 2)
  39. #define IOMMU_CTRL_MMU_EN (1ULL)
  40. #define IOMMU_CTRL_TSB_SHIFT 16
  41. #define IOMMU_BASE 0x8
  42. #define IOMMU_FLUSH 0x10
  43. #define IOMMU_TTE_DATA_V (1ULL << 63)
  44. #define IOMMU_TTE_DATA_SIZE (1ULL << 61)
  45. #define IOMMU_TTE_DATA_W (1ULL << 1)
  46. #define IOMMU_TTE_PHYS_MASK_8K 0x1ffffffe000ULL
  47. #define IOMMU_TTE_PHYS_MASK_64K 0x1ffffff8000ULL
  48. #define IOMMU_TSB_8K_OFFSET_MASK_8M 0x00000000007fe000ULL
  49. #define IOMMU_TSB_8K_OFFSET_MASK_16M 0x0000000000ffe000ULL
  50. #define IOMMU_TSB_8K_OFFSET_MASK_32M 0x0000000001ffe000ULL
  51. #define IOMMU_TSB_8K_OFFSET_MASK_64M 0x0000000003ffe000ULL
  52. #define IOMMU_TSB_8K_OFFSET_MASK_128M 0x0000000007ffe000ULL
  53. #define IOMMU_TSB_8K_OFFSET_MASK_256M 0x000000000fffe000ULL
  54. #define IOMMU_TSB_8K_OFFSET_MASK_512M 0x000000001fffe000ULL
  55. #define IOMMU_TSB_8K_OFFSET_MASK_1G 0x000000003fffe000ULL
  56. #define IOMMU_TSB_64K_OFFSET_MASK_64M 0x0000000003ff0000ULL
  57. #define IOMMU_TSB_64K_OFFSET_MASK_128M 0x0000000007ff0000ULL
  58. #define IOMMU_TSB_64K_OFFSET_MASK_256M 0x000000000fff0000ULL
  59. #define IOMMU_TSB_64K_OFFSET_MASK_512M 0x000000001fff0000ULL
  60. #define IOMMU_TSB_64K_OFFSET_MASK_1G 0x000000003fff0000ULL
  61. #define IOMMU_TSB_64K_OFFSET_MASK_2G 0x000000007fff0000ULL
  62. /* Called from RCU critical section */
  63. static IOMMUTLBEntry sun4u_translate_iommu(IOMMUMemoryRegion *iommu,
  64. hwaddr addr,
  65. IOMMUAccessFlags flag, int iommu_idx)
  66. {
  67. IOMMUState *is = container_of(iommu, IOMMUState, iommu);
  68. hwaddr baseaddr, offset;
  69. uint64_t tte;
  70. uint32_t tsbsize;
  71. IOMMUTLBEntry ret = {
  72. .target_as = &address_space_memory,
  73. .iova = 0,
  74. .translated_addr = 0,
  75. .addr_mask = ~(hwaddr)0,
  76. .perm = IOMMU_NONE,
  77. };
  78. if (!(is->regs[IOMMU_CTRL >> 3] & IOMMU_CTRL_MMU_EN)) {
  79. /* IOMMU disabled, passthrough using standard 8K page */
  80. ret.iova = addr & IOMMU_PAGE_MASK_8K;
  81. ret.translated_addr = addr;
  82. ret.addr_mask = IOMMU_PAGE_MASK_8K;
  83. ret.perm = IOMMU_RW;
  84. return ret;
  85. }
  86. baseaddr = is->regs[IOMMU_BASE >> 3];
  87. tsbsize = (is->regs[IOMMU_CTRL >> 3] >> IOMMU_CTRL_TSB_SHIFT) & 0x7;
  88. if (is->regs[IOMMU_CTRL >> 3] & IOMMU_CTRL_TBW_SIZE) {
  89. /* 64K */
  90. switch (tsbsize) {
  91. case 0:
  92. offset = (addr & IOMMU_TSB_64K_OFFSET_MASK_64M) >> 13;
  93. break;
  94. case 1:
  95. offset = (addr & IOMMU_TSB_64K_OFFSET_MASK_128M) >> 13;
  96. break;
  97. case 2:
  98. offset = (addr & IOMMU_TSB_64K_OFFSET_MASK_256M) >> 13;
  99. break;
  100. case 3:
  101. offset = (addr & IOMMU_TSB_64K_OFFSET_MASK_512M) >> 13;
  102. break;
  103. case 4:
  104. offset = (addr & IOMMU_TSB_64K_OFFSET_MASK_1G) >> 13;
  105. break;
  106. case 5:
  107. offset = (addr & IOMMU_TSB_64K_OFFSET_MASK_2G) >> 13;
  108. break;
  109. default:
  110. /* Not implemented, error */
  111. return ret;
  112. }
  113. } else {
  114. /* 8K */
  115. switch (tsbsize) {
  116. case 0:
  117. offset = (addr & IOMMU_TSB_8K_OFFSET_MASK_8M) >> 10;
  118. break;
  119. case 1:
  120. offset = (addr & IOMMU_TSB_8K_OFFSET_MASK_16M) >> 10;
  121. break;
  122. case 2:
  123. offset = (addr & IOMMU_TSB_8K_OFFSET_MASK_32M) >> 10;
  124. break;
  125. case 3:
  126. offset = (addr & IOMMU_TSB_8K_OFFSET_MASK_64M) >> 10;
  127. break;
  128. case 4:
  129. offset = (addr & IOMMU_TSB_8K_OFFSET_MASK_128M) >> 10;
  130. break;
  131. case 5:
  132. offset = (addr & IOMMU_TSB_8K_OFFSET_MASK_256M) >> 10;
  133. break;
  134. case 6:
  135. offset = (addr & IOMMU_TSB_8K_OFFSET_MASK_512M) >> 10;
  136. break;
  137. case 7:
  138. offset = (addr & IOMMU_TSB_8K_OFFSET_MASK_1G) >> 10;
  139. break;
  140. }
  141. }
  142. tte = address_space_ldq_be(&address_space_memory, baseaddr + offset,
  143. MEMTXATTRS_UNSPECIFIED, NULL);
  144. if (!(tte & IOMMU_TTE_DATA_V)) {
  145. /* Invalid mapping */
  146. return ret;
  147. }
  148. if (tte & IOMMU_TTE_DATA_W) {
  149. /* Writable */
  150. ret.perm = IOMMU_RW;
  151. } else {
  152. ret.perm = IOMMU_RO;
  153. }
  154. /* Extract phys */
  155. if (tte & IOMMU_TTE_DATA_SIZE) {
  156. /* 64K */
  157. ret.iova = addr & IOMMU_PAGE_MASK_64K;
  158. ret.translated_addr = tte & IOMMU_TTE_PHYS_MASK_64K;
  159. ret.addr_mask = (IOMMU_PAGE_SIZE_64K - 1);
  160. } else {
  161. /* 8K */
  162. ret.iova = addr & IOMMU_PAGE_MASK_8K;
  163. ret.translated_addr = tte & IOMMU_TTE_PHYS_MASK_8K;
  164. ret.addr_mask = (IOMMU_PAGE_SIZE_8K - 1);
  165. }
  166. trace_sun4u_iommu_translate(ret.iova, ret.translated_addr, tte);
  167. return ret;
  168. }
  169. static void iommu_mem_write(void *opaque, hwaddr addr,
  170. uint64_t val, unsigned size)
  171. {
  172. IOMMUState *is = opaque;
  173. trace_sun4u_iommu_mem_write(addr, val, size);
  174. switch (addr) {
  175. case IOMMU_CTRL:
  176. if (size == 4) {
  177. is->regs[IOMMU_CTRL >> 3] &= 0xffffffffULL;
  178. is->regs[IOMMU_CTRL >> 3] |= val << 32;
  179. } else {
  180. is->regs[IOMMU_CTRL >> 3] = val;
  181. }
  182. break;
  183. case IOMMU_CTRL + 0x4:
  184. is->regs[IOMMU_CTRL >> 3] &= 0xffffffff00000000ULL;
  185. is->regs[IOMMU_CTRL >> 3] |= val & 0xffffffffULL;
  186. break;
  187. case IOMMU_BASE:
  188. if (size == 4) {
  189. is->regs[IOMMU_BASE >> 3] &= 0xffffffffULL;
  190. is->regs[IOMMU_BASE >> 3] |= val << 32;
  191. } else {
  192. is->regs[IOMMU_BASE >> 3] = val;
  193. }
  194. break;
  195. case IOMMU_BASE + 0x4:
  196. is->regs[IOMMU_BASE >> 3] &= 0xffffffff00000000ULL;
  197. is->regs[IOMMU_BASE >> 3] |= val & 0xffffffffULL;
  198. break;
  199. case IOMMU_FLUSH:
  200. case IOMMU_FLUSH + 0x4:
  201. break;
  202. default:
  203. qemu_log_mask(LOG_UNIMP,
  204. "sun4u-iommu: Unimplemented register write "
  205. "reg 0x%" HWADDR_PRIx " size 0x%x value 0x%" PRIx64 "\n",
  206. addr, size, val);
  207. break;
  208. }
  209. }
  210. static uint64_t iommu_mem_read(void *opaque, hwaddr addr, unsigned size)
  211. {
  212. IOMMUState *is = opaque;
  213. uint64_t val;
  214. switch (addr) {
  215. case IOMMU_CTRL:
  216. if (size == 4) {
  217. val = is->regs[IOMMU_CTRL >> 3] >> 32;
  218. } else {
  219. val = is->regs[IOMMU_CTRL >> 3];
  220. }
  221. break;
  222. case IOMMU_CTRL + 0x4:
  223. val = is->regs[IOMMU_CTRL >> 3] & 0xffffffffULL;
  224. break;
  225. case IOMMU_BASE:
  226. if (size == 4) {
  227. val = is->regs[IOMMU_BASE >> 3] >> 32;
  228. } else {
  229. val = is->regs[IOMMU_BASE >> 3];
  230. }
  231. break;
  232. case IOMMU_BASE + 0x4:
  233. val = is->regs[IOMMU_BASE >> 3] & 0xffffffffULL;
  234. break;
  235. case IOMMU_FLUSH:
  236. case IOMMU_FLUSH + 0x4:
  237. val = 0;
  238. break;
  239. default:
  240. qemu_log_mask(LOG_UNIMP,
  241. "sun4u-iommu: Unimplemented register read "
  242. "reg 0x%" HWADDR_PRIx " size 0x%x\n",
  243. addr, size);
  244. val = 0;
  245. break;
  246. }
  247. trace_sun4u_iommu_mem_read(addr, val, size);
  248. return val;
  249. }
  250. static const MemoryRegionOps iommu_mem_ops = {
  251. .read = iommu_mem_read,
  252. .write = iommu_mem_write,
  253. .endianness = DEVICE_BIG_ENDIAN,
  254. };
  255. static void iommu_reset(DeviceState *d)
  256. {
  257. IOMMUState *s = SUN4U_IOMMU(d);
  258. memset(s->regs, 0, IOMMU_NREGS * sizeof(uint64_t));
  259. }
  260. static void iommu_init(Object *obj)
  261. {
  262. IOMMUState *s = SUN4U_IOMMU(obj);
  263. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  264. memory_region_init_iommu(&s->iommu, sizeof(s->iommu),
  265. TYPE_SUN4U_IOMMU_MEMORY_REGION, OBJECT(s),
  266. "iommu-sun4u", UINT64_MAX);
  267. address_space_init(&s->iommu_as, MEMORY_REGION(&s->iommu), "iommu-as");
  268. memory_region_init_io(&s->iomem, obj, &iommu_mem_ops, s, "iommu",
  269. IOMMU_NREGS * sizeof(uint64_t));
  270. sysbus_init_mmio(sbd, &s->iomem);
  271. }
  272. static void iommu_class_init(ObjectClass *klass, void *data)
  273. {
  274. DeviceClass *dc = DEVICE_CLASS(klass);
  275. device_class_set_legacy_reset(dc, iommu_reset);
  276. }
  277. static const TypeInfo iommu_info = {
  278. .name = TYPE_SUN4U_IOMMU,
  279. .parent = TYPE_SYS_BUS_DEVICE,
  280. .instance_size = sizeof(IOMMUState),
  281. .instance_init = iommu_init,
  282. .class_init = iommu_class_init,
  283. };
  284. static void sun4u_iommu_memory_region_class_init(ObjectClass *klass, void *data)
  285. {
  286. IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_CLASS(klass);
  287. imrc->translate = sun4u_translate_iommu;
  288. }
  289. static const TypeInfo sun4u_iommu_memory_region_info = {
  290. .parent = TYPE_IOMMU_MEMORY_REGION,
  291. .name = TYPE_SUN4U_IOMMU_MEMORY_REGION,
  292. .class_init = sun4u_iommu_memory_region_class_init,
  293. };
  294. static void iommu_register_types(void)
  295. {
  296. type_register_static(&iommu_info);
  297. type_register_static(&sun4u_iommu_memory_region_info);
  298. }
  299. type_init(iommu_register_types)