sun4u_iommu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 "trace.h"
  32. #define IOMMU_PAGE_SIZE_8K (1ULL << 13)
  33. #define IOMMU_PAGE_MASK_8K (~(IOMMU_PAGE_SIZE_8K - 1))
  34. #define IOMMU_PAGE_SIZE_64K (1ULL << 16)
  35. #define IOMMU_PAGE_MASK_64K (~(IOMMU_PAGE_SIZE_64K - 1))
  36. #define IOMMU_CTRL 0x0
  37. #define IOMMU_CTRL_TBW_SIZE (1ULL << 2)
  38. #define IOMMU_CTRL_MMU_EN (1ULL)
  39. #define IOMMU_CTRL_TSB_SHIFT 16
  40. #define IOMMU_BASE 0x8
  41. #define IOMMU_FLUSH 0x10
  42. #define IOMMU_TTE_DATA_V (1ULL << 63)
  43. #define IOMMU_TTE_DATA_SIZE (1ULL << 61)
  44. #define IOMMU_TTE_DATA_W (1ULL << 1)
  45. #define IOMMU_TTE_PHYS_MASK_8K 0x1ffffffe000ULL
  46. #define IOMMU_TTE_PHYS_MASK_64K 0x1ffffff8000ULL
  47. #define IOMMU_TSB_8K_OFFSET_MASK_8M 0x00000000007fe000ULL
  48. #define IOMMU_TSB_8K_OFFSET_MASK_16M 0x0000000000ffe000ULL
  49. #define IOMMU_TSB_8K_OFFSET_MASK_32M 0x0000000001ffe000ULL
  50. #define IOMMU_TSB_8K_OFFSET_MASK_64M 0x0000000003ffe000ULL
  51. #define IOMMU_TSB_8K_OFFSET_MASK_128M 0x0000000007ffe000ULL
  52. #define IOMMU_TSB_8K_OFFSET_MASK_256M 0x000000000fffe000ULL
  53. #define IOMMU_TSB_8K_OFFSET_MASK_512M 0x000000001fffe000ULL
  54. #define IOMMU_TSB_8K_OFFSET_MASK_1G 0x000000003fffe000ULL
  55. #define IOMMU_TSB_64K_OFFSET_MASK_64M 0x0000000003ff0000ULL
  56. #define IOMMU_TSB_64K_OFFSET_MASK_128M 0x0000000007ff0000ULL
  57. #define IOMMU_TSB_64K_OFFSET_MASK_256M 0x000000000fff0000ULL
  58. #define IOMMU_TSB_64K_OFFSET_MASK_512M 0x000000001fff0000ULL
  59. #define IOMMU_TSB_64K_OFFSET_MASK_1G 0x000000003fff0000ULL
  60. #define IOMMU_TSB_64K_OFFSET_MASK_2G 0x000000007fff0000ULL
  61. /* Called from RCU critical section */
  62. static IOMMUTLBEntry sun4u_translate_iommu(IOMMUMemoryRegion *iommu,
  63. hwaddr addr,
  64. IOMMUAccessFlags flag, int iommu_idx)
  65. {
  66. IOMMUState *is = container_of(iommu, IOMMUState, iommu);
  67. hwaddr baseaddr, offset;
  68. uint64_t tte;
  69. uint32_t tsbsize;
  70. IOMMUTLBEntry ret = {
  71. .target_as = &address_space_memory,
  72. .iova = 0,
  73. .translated_addr = 0,
  74. .addr_mask = ~(hwaddr)0,
  75. .perm = IOMMU_NONE,
  76. };
  77. if (!(is->regs[IOMMU_CTRL >> 3] & IOMMU_CTRL_MMU_EN)) {
  78. /* IOMMU disabled, passthrough using standard 8K page */
  79. ret.iova = addr & IOMMU_PAGE_MASK_8K;
  80. ret.translated_addr = addr;
  81. ret.addr_mask = IOMMU_PAGE_MASK_8K;
  82. ret.perm = IOMMU_RW;
  83. return ret;
  84. }
  85. baseaddr = is->regs[IOMMU_BASE >> 3];
  86. tsbsize = (is->regs[IOMMU_CTRL >> 3] >> IOMMU_CTRL_TSB_SHIFT) & 0x7;
  87. if (is->regs[IOMMU_CTRL >> 3] & IOMMU_CTRL_TBW_SIZE) {
  88. /* 64K */
  89. switch (tsbsize) {
  90. case 0:
  91. offset = (addr & IOMMU_TSB_64K_OFFSET_MASK_64M) >> 13;
  92. break;
  93. case 1:
  94. offset = (addr & IOMMU_TSB_64K_OFFSET_MASK_128M) >> 13;
  95. break;
  96. case 2:
  97. offset = (addr & IOMMU_TSB_64K_OFFSET_MASK_256M) >> 13;
  98. break;
  99. case 3:
  100. offset = (addr & IOMMU_TSB_64K_OFFSET_MASK_512M) >> 13;
  101. break;
  102. case 4:
  103. offset = (addr & IOMMU_TSB_64K_OFFSET_MASK_1G) >> 13;
  104. break;
  105. case 5:
  106. offset = (addr & IOMMU_TSB_64K_OFFSET_MASK_2G) >> 13;
  107. break;
  108. default:
  109. /* Not implemented, error */
  110. return ret;
  111. }
  112. } else {
  113. /* 8K */
  114. switch (tsbsize) {
  115. case 0:
  116. offset = (addr & IOMMU_TSB_8K_OFFSET_MASK_8M) >> 10;
  117. break;
  118. case 1:
  119. offset = (addr & IOMMU_TSB_8K_OFFSET_MASK_16M) >> 10;
  120. break;
  121. case 2:
  122. offset = (addr & IOMMU_TSB_8K_OFFSET_MASK_32M) >> 10;
  123. break;
  124. case 3:
  125. offset = (addr & IOMMU_TSB_8K_OFFSET_MASK_64M) >> 10;
  126. break;
  127. case 4:
  128. offset = (addr & IOMMU_TSB_8K_OFFSET_MASK_128M) >> 10;
  129. break;
  130. case 5:
  131. offset = (addr & IOMMU_TSB_8K_OFFSET_MASK_256M) >> 10;
  132. break;
  133. case 6:
  134. offset = (addr & IOMMU_TSB_8K_OFFSET_MASK_512M) >> 10;
  135. break;
  136. case 7:
  137. offset = (addr & IOMMU_TSB_8K_OFFSET_MASK_1G) >> 10;
  138. break;
  139. }
  140. }
  141. tte = address_space_ldq_be(&address_space_memory, baseaddr + offset,
  142. MEMTXATTRS_UNSPECIFIED, NULL);
  143. if (!(tte & IOMMU_TTE_DATA_V)) {
  144. /* Invalid mapping */
  145. return ret;
  146. }
  147. if (tte & IOMMU_TTE_DATA_W) {
  148. /* Writeable */
  149. ret.perm = IOMMU_RW;
  150. } else {
  151. ret.perm = IOMMU_RO;
  152. }
  153. /* Extract phys */
  154. if (tte & IOMMU_TTE_DATA_SIZE) {
  155. /* 64K */
  156. ret.iova = addr & IOMMU_PAGE_MASK_64K;
  157. ret.translated_addr = tte & IOMMU_TTE_PHYS_MASK_64K;
  158. ret.addr_mask = (IOMMU_PAGE_SIZE_64K - 1);
  159. } else {
  160. /* 8K */
  161. ret.iova = addr & IOMMU_PAGE_MASK_8K;
  162. ret.translated_addr = tte & IOMMU_TTE_PHYS_MASK_8K;
  163. ret.addr_mask = (IOMMU_PAGE_SIZE_8K - 1);
  164. }
  165. trace_sun4u_iommu_translate(ret.iova, ret.translated_addr, tte);
  166. return ret;
  167. }
  168. static void iommu_mem_write(void *opaque, hwaddr addr,
  169. uint64_t val, unsigned size)
  170. {
  171. IOMMUState *is = opaque;
  172. trace_sun4u_iommu_mem_write(addr, val, size);
  173. switch (addr) {
  174. case IOMMU_CTRL:
  175. if (size == 4) {
  176. is->regs[IOMMU_CTRL >> 3] &= 0xffffffffULL;
  177. is->regs[IOMMU_CTRL >> 3] |= val << 32;
  178. } else {
  179. is->regs[IOMMU_CTRL >> 3] = val;
  180. }
  181. break;
  182. case IOMMU_CTRL + 0x4:
  183. is->regs[IOMMU_CTRL >> 3] &= 0xffffffff00000000ULL;
  184. is->regs[IOMMU_CTRL >> 3] |= val & 0xffffffffULL;
  185. break;
  186. case IOMMU_BASE:
  187. if (size == 4) {
  188. is->regs[IOMMU_BASE >> 3] &= 0xffffffffULL;
  189. is->regs[IOMMU_BASE >> 3] |= val << 32;
  190. } else {
  191. is->regs[IOMMU_BASE >> 3] = val;
  192. }
  193. break;
  194. case IOMMU_BASE + 0x4:
  195. is->regs[IOMMU_BASE >> 3] &= 0xffffffff00000000ULL;
  196. is->regs[IOMMU_BASE >> 3] |= val & 0xffffffffULL;
  197. break;
  198. case IOMMU_FLUSH:
  199. case IOMMU_FLUSH + 0x4:
  200. break;
  201. default:
  202. qemu_log_mask(LOG_UNIMP,
  203. "sun4u-iommu: Unimplemented register write "
  204. "reg 0x%" HWADDR_PRIx " size 0x%x value 0x%" PRIx64 "\n",
  205. addr, size, val);
  206. break;
  207. }
  208. }
  209. static uint64_t iommu_mem_read(void *opaque, hwaddr addr, unsigned size)
  210. {
  211. IOMMUState *is = opaque;
  212. uint64_t val;
  213. switch (addr) {
  214. case IOMMU_CTRL:
  215. if (size == 4) {
  216. val = is->regs[IOMMU_CTRL >> 3] >> 32;
  217. } else {
  218. val = is->regs[IOMMU_CTRL >> 3];
  219. }
  220. break;
  221. case IOMMU_CTRL + 0x4:
  222. val = is->regs[IOMMU_CTRL >> 3] & 0xffffffffULL;
  223. break;
  224. case IOMMU_BASE:
  225. if (size == 4) {
  226. val = is->regs[IOMMU_BASE >> 3] >> 32;
  227. } else {
  228. val = is->regs[IOMMU_BASE >> 3];
  229. }
  230. break;
  231. case IOMMU_BASE + 0x4:
  232. val = is->regs[IOMMU_BASE >> 3] & 0xffffffffULL;
  233. break;
  234. case IOMMU_FLUSH:
  235. case IOMMU_FLUSH + 0x4:
  236. val = 0;
  237. break;
  238. default:
  239. qemu_log_mask(LOG_UNIMP,
  240. "sun4u-iommu: Unimplemented register read "
  241. "reg 0x%" HWADDR_PRIx " size 0x%x\n",
  242. addr, size);
  243. val = 0;
  244. break;
  245. }
  246. trace_sun4u_iommu_mem_read(addr, val, size);
  247. return val;
  248. }
  249. static const MemoryRegionOps iommu_mem_ops = {
  250. .read = iommu_mem_read,
  251. .write = iommu_mem_write,
  252. .endianness = DEVICE_BIG_ENDIAN,
  253. };
  254. static void iommu_reset(DeviceState *d)
  255. {
  256. IOMMUState *s = SUN4U_IOMMU(d);
  257. memset(s->regs, 0, IOMMU_NREGS * sizeof(uint64_t));
  258. }
  259. static void iommu_init(Object *obj)
  260. {
  261. IOMMUState *s = SUN4U_IOMMU(obj);
  262. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  263. memory_region_init_iommu(&s->iommu, sizeof(s->iommu),
  264. TYPE_SUN4U_IOMMU_MEMORY_REGION, OBJECT(s),
  265. "iommu-sun4u", UINT64_MAX);
  266. address_space_init(&s->iommu_as, MEMORY_REGION(&s->iommu), "iommu-as");
  267. memory_region_init_io(&s->iomem, obj, &iommu_mem_ops, s, "iommu",
  268. IOMMU_NREGS * sizeof(uint64_t));
  269. sysbus_init_mmio(sbd, &s->iomem);
  270. }
  271. static void iommu_class_init(ObjectClass *klass, void *data)
  272. {
  273. DeviceClass *dc = DEVICE_CLASS(klass);
  274. dc->reset = iommu_reset;
  275. }
  276. static const TypeInfo iommu_info = {
  277. .name = TYPE_SUN4U_IOMMU,
  278. .parent = TYPE_SYS_BUS_DEVICE,
  279. .instance_size = sizeof(IOMMUState),
  280. .instance_init = iommu_init,
  281. .class_init = iommu_class_init,
  282. };
  283. static void sun4u_iommu_memory_region_class_init(ObjectClass *klass, void *data)
  284. {
  285. IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_CLASS(klass);
  286. imrc->translate = sun4u_translate_iommu;
  287. }
  288. static const TypeInfo sun4u_iommu_memory_region_info = {
  289. .parent = TYPE_IOMMU_MEMORY_REGION,
  290. .name = TYPE_SUN4U_IOMMU_MEMORY_REGION,
  291. .class_init = sun4u_iommu_memory_region_class_init,
  292. };
  293. static void iommu_register_types(void)
  294. {
  295. type_register_static(&iommu_info);
  296. type_register_static(&sun4u_iommu_memory_region_info);
  297. }
  298. type_init(iommu_register_types)