mips_itu.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /*
  2. * Inter-Thread Communication Unit emulation.
  3. *
  4. * Copyright (c) 2016 Imagination Technologies
  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 "qemu/osdep.h"
  20. #include "qemu/units.h"
  21. #include "qemu/log.h"
  22. #include "qemu/module.h"
  23. #include "qapi/error.h"
  24. #include "cpu.h"
  25. #include "exec/exec-all.h"
  26. #include "hw/misc/mips_itu.h"
  27. #include "hw/qdev-properties.h"
  28. #define ITC_TAG_ADDRSPACE_SZ (ITC_ADDRESSMAP_NUM * 8)
  29. /* Initialize as 4kB area to fit all 32 cells with default 128B grain.
  30. Storage may be resized by the software. */
  31. #define ITC_STORAGE_ADDRSPACE_SZ 0x1000
  32. #define ITC_FIFO_NUM_MAX 16
  33. #define ITC_SEMAPH_NUM_MAX 16
  34. #define ITC_AM1_NUMENTRIES_OFS 20
  35. #define ITC_CELL_PV_MAX_VAL 0xFFFF
  36. #define ITC_CELL_TAG_FIFO_DEPTH 28
  37. #define ITC_CELL_TAG_FIFO_PTR 18
  38. #define ITC_CELL_TAG_FIFO 17
  39. #define ITC_CELL_TAG_T 16
  40. #define ITC_CELL_TAG_F 1
  41. #define ITC_CELL_TAG_E 0
  42. #define ITC_AM0_BASE_ADDRESS_MASK 0xFFFFFC00ULL
  43. #define ITC_AM0_EN_MASK 0x1
  44. #define ITC_AM1_ADDR_MASK_MASK 0x1FC00
  45. #define ITC_AM1_ENTRY_GRAIN_MASK 0x7
  46. typedef enum ITCView {
  47. ITCVIEW_BYPASS = 0,
  48. ITCVIEW_CONTROL = 1,
  49. ITCVIEW_EF_SYNC = 2,
  50. ITCVIEW_EF_TRY = 3,
  51. ITCVIEW_PV_SYNC = 4,
  52. ITCVIEW_PV_TRY = 5,
  53. ITCVIEW_PV_ICR0 = 15,
  54. } ITCView;
  55. #define ITC_ICR0_CELL_NUM 16
  56. #define ITC_ICR0_BLK_GRAIN 8
  57. #define ITC_ICR0_BLK_GRAIN_MASK 0x7
  58. #define ITC_ICR0_ERR_AXI 2
  59. #define ITC_ICR0_ERR_PARITY 1
  60. #define ITC_ICR0_ERR_EXEC 0
  61. MemoryRegion *mips_itu_get_tag_region(MIPSITUState *itu)
  62. {
  63. return &itu->tag_io;
  64. }
  65. static uint64_t itc_tag_read(void *opaque, hwaddr addr, unsigned size)
  66. {
  67. MIPSITUState *tag = (MIPSITUState *)opaque;
  68. uint64_t index = addr >> 3;
  69. if (index >= ITC_ADDRESSMAP_NUM) {
  70. qemu_log_mask(LOG_GUEST_ERROR, "Read 0x%" PRIx64 "\n", addr);
  71. return 0;
  72. }
  73. return tag->ITCAddressMap[index];
  74. }
  75. void itc_reconfigure(MIPSITUState *tag)
  76. {
  77. uint64_t *am = &tag->ITCAddressMap[0];
  78. MemoryRegion *mr = &tag->storage_io;
  79. hwaddr address = am[0] & ITC_AM0_BASE_ADDRESS_MASK;
  80. uint64_t size = (1 * KiB) + (am[1] & ITC_AM1_ADDR_MASK_MASK);
  81. bool is_enabled = (am[0] & ITC_AM0_EN_MASK) != 0;
  82. if (tag->saar_present) {
  83. address = ((*(uint64_t *) tag->saar) & 0xFFFFFFFFE000ULL) << 4;
  84. size = 1ULL << ((*(uint64_t *) tag->saar >> 1) & 0x1f);
  85. is_enabled = *(uint64_t *) tag->saar & 1;
  86. }
  87. memory_region_transaction_begin();
  88. if (!(size & (size - 1))) {
  89. memory_region_set_size(mr, size);
  90. }
  91. memory_region_set_address(mr, address);
  92. memory_region_set_enabled(mr, is_enabled);
  93. memory_region_transaction_commit();
  94. }
  95. static void itc_tag_write(void *opaque, hwaddr addr,
  96. uint64_t data, unsigned size)
  97. {
  98. MIPSITUState *tag = (MIPSITUState *)opaque;
  99. uint64_t *am = &tag->ITCAddressMap[0];
  100. uint64_t am_old, mask;
  101. uint64_t index = addr >> 3;
  102. switch (index) {
  103. case 0:
  104. mask = ITC_AM0_BASE_ADDRESS_MASK | ITC_AM0_EN_MASK;
  105. break;
  106. case 1:
  107. mask = ITC_AM1_ADDR_MASK_MASK | ITC_AM1_ENTRY_GRAIN_MASK;
  108. break;
  109. default:
  110. qemu_log_mask(LOG_GUEST_ERROR, "Bad write 0x%" PRIx64 "\n", addr);
  111. return;
  112. }
  113. am_old = am[index];
  114. am[index] = (data & mask) | (am_old & ~mask);
  115. if (am_old != am[index]) {
  116. itc_reconfigure(tag);
  117. }
  118. }
  119. static const MemoryRegionOps itc_tag_ops = {
  120. .read = itc_tag_read,
  121. .write = itc_tag_write,
  122. .impl = {
  123. .max_access_size = 8,
  124. },
  125. .endianness = DEVICE_NATIVE_ENDIAN,
  126. };
  127. static inline uint32_t get_num_cells(MIPSITUState *s)
  128. {
  129. return s->num_fifo + s->num_semaphores;
  130. }
  131. static inline ITCView get_itc_view(hwaddr addr)
  132. {
  133. return (addr >> 3) & 0xf;
  134. }
  135. static inline int get_cell_stride_shift(const MIPSITUState *s)
  136. {
  137. /* Minimum interval (for EntryGain = 0) is 128 B */
  138. if (s->saar_present) {
  139. return 7 + ((s->icr0 >> ITC_ICR0_BLK_GRAIN) &
  140. ITC_ICR0_BLK_GRAIN_MASK);
  141. } else {
  142. return 7 + (s->ITCAddressMap[1] & ITC_AM1_ENTRY_GRAIN_MASK);
  143. }
  144. }
  145. static inline ITCStorageCell *get_cell(MIPSITUState *s,
  146. hwaddr addr)
  147. {
  148. uint32_t cell_idx = addr >> get_cell_stride_shift(s);
  149. uint32_t num_cells = get_num_cells(s);
  150. if (cell_idx >= num_cells) {
  151. cell_idx = num_cells - 1;
  152. }
  153. return &s->cell[cell_idx];
  154. }
  155. static void wake_blocked_threads(ITCStorageCell *c)
  156. {
  157. CPUState *cs;
  158. CPU_FOREACH(cs) {
  159. if (cs->halted && (c->blocked_threads & (1ULL << cs->cpu_index))) {
  160. cpu_interrupt(cs, CPU_INTERRUPT_WAKE);
  161. }
  162. }
  163. c->blocked_threads = 0;
  164. }
  165. static void QEMU_NORETURN block_thread_and_exit(ITCStorageCell *c)
  166. {
  167. c->blocked_threads |= 1ULL << current_cpu->cpu_index;
  168. current_cpu->halted = 1;
  169. current_cpu->exception_index = EXCP_HLT;
  170. cpu_loop_exit_restore(current_cpu, current_cpu->mem_io_pc);
  171. }
  172. /* ITC Bypass View */
  173. static inline uint64_t view_bypass_read(ITCStorageCell *c)
  174. {
  175. if (c->tag.FIFO) {
  176. return c->data[c->fifo_out];
  177. } else {
  178. return c->data[0];
  179. }
  180. }
  181. static inline void view_bypass_write(ITCStorageCell *c, uint64_t val)
  182. {
  183. if (c->tag.FIFO && (c->tag.FIFOPtr > 0)) {
  184. int idx = (c->fifo_out + c->tag.FIFOPtr - 1) % ITC_CELL_DEPTH;
  185. c->data[idx] = val;
  186. }
  187. /* ignore a write to the semaphore cell */
  188. }
  189. /* ITC Control View */
  190. static inline uint64_t view_control_read(ITCStorageCell *c)
  191. {
  192. return ((uint64_t)c->tag.FIFODepth << ITC_CELL_TAG_FIFO_DEPTH) |
  193. (c->tag.FIFOPtr << ITC_CELL_TAG_FIFO_PTR) |
  194. (c->tag.FIFO << ITC_CELL_TAG_FIFO) |
  195. (c->tag.T << ITC_CELL_TAG_T) |
  196. (c->tag.E << ITC_CELL_TAG_E) |
  197. (c->tag.F << ITC_CELL_TAG_F);
  198. }
  199. static inline void view_control_write(ITCStorageCell *c, uint64_t val)
  200. {
  201. c->tag.T = (val >> ITC_CELL_TAG_T) & 1;
  202. c->tag.E = (val >> ITC_CELL_TAG_E) & 1;
  203. c->tag.F = (val >> ITC_CELL_TAG_F) & 1;
  204. if (c->tag.E) {
  205. c->tag.FIFOPtr = 0;
  206. }
  207. }
  208. /* ITC Empty/Full View */
  209. static uint64_t view_ef_common_read(ITCStorageCell *c, bool blocking)
  210. {
  211. uint64_t ret = 0;
  212. if (!c->tag.FIFO) {
  213. return 0;
  214. }
  215. c->tag.F = 0;
  216. if (blocking && c->tag.E) {
  217. block_thread_and_exit(c);
  218. }
  219. if (c->blocked_threads) {
  220. wake_blocked_threads(c);
  221. }
  222. if (c->tag.FIFOPtr > 0) {
  223. ret = c->data[c->fifo_out];
  224. c->fifo_out = (c->fifo_out + 1) % ITC_CELL_DEPTH;
  225. c->tag.FIFOPtr--;
  226. }
  227. if (c->tag.FIFOPtr == 0) {
  228. c->tag.E = 1;
  229. }
  230. return ret;
  231. }
  232. static uint64_t view_ef_sync_read(ITCStorageCell *c)
  233. {
  234. return view_ef_common_read(c, true);
  235. }
  236. static uint64_t view_ef_try_read(ITCStorageCell *c)
  237. {
  238. return view_ef_common_read(c, false);
  239. }
  240. static inline void view_ef_common_write(ITCStorageCell *c, uint64_t val,
  241. bool blocking)
  242. {
  243. if (!c->tag.FIFO) {
  244. return;
  245. }
  246. c->tag.E = 0;
  247. if (blocking && c->tag.F) {
  248. block_thread_and_exit(c);
  249. }
  250. if (c->blocked_threads) {
  251. wake_blocked_threads(c);
  252. }
  253. if (c->tag.FIFOPtr < ITC_CELL_DEPTH) {
  254. int idx = (c->fifo_out + c->tag.FIFOPtr) % ITC_CELL_DEPTH;
  255. c->data[idx] = val;
  256. c->tag.FIFOPtr++;
  257. }
  258. if (c->tag.FIFOPtr == ITC_CELL_DEPTH) {
  259. c->tag.F = 1;
  260. }
  261. }
  262. static void view_ef_sync_write(ITCStorageCell *c, uint64_t val)
  263. {
  264. view_ef_common_write(c, val, true);
  265. }
  266. static void view_ef_try_write(ITCStorageCell *c, uint64_t val)
  267. {
  268. view_ef_common_write(c, val, false);
  269. }
  270. /* ITC P/V View */
  271. static uint64_t view_pv_common_read(ITCStorageCell *c, bool blocking)
  272. {
  273. uint64_t ret = c->data[0];
  274. if (c->tag.FIFO) {
  275. return 0;
  276. }
  277. if (c->data[0] > 0) {
  278. c->data[0]--;
  279. } else if (blocking) {
  280. block_thread_and_exit(c);
  281. }
  282. return ret;
  283. }
  284. static uint64_t view_pv_sync_read(ITCStorageCell *c)
  285. {
  286. return view_pv_common_read(c, true);
  287. }
  288. static uint64_t view_pv_try_read(ITCStorageCell *c)
  289. {
  290. return view_pv_common_read(c, false);
  291. }
  292. static inline void view_pv_common_write(ITCStorageCell *c)
  293. {
  294. if (c->tag.FIFO) {
  295. return;
  296. }
  297. if (c->data[0] < ITC_CELL_PV_MAX_VAL) {
  298. c->data[0]++;
  299. }
  300. if (c->blocked_threads) {
  301. wake_blocked_threads(c);
  302. }
  303. }
  304. static void view_pv_sync_write(ITCStorageCell *c)
  305. {
  306. view_pv_common_write(c);
  307. }
  308. static void view_pv_try_write(ITCStorageCell *c)
  309. {
  310. view_pv_common_write(c);
  311. }
  312. static void raise_exception(int excp)
  313. {
  314. current_cpu->exception_index = excp;
  315. cpu_loop_exit(current_cpu);
  316. }
  317. static uint64_t itc_storage_read(void *opaque, hwaddr addr, unsigned size)
  318. {
  319. MIPSITUState *s = (MIPSITUState *)opaque;
  320. ITCStorageCell *cell = get_cell(s, addr);
  321. ITCView view = get_itc_view(addr);
  322. uint64_t ret = -1;
  323. switch (size) {
  324. case 1:
  325. case 2:
  326. s->icr0 |= 1 << ITC_ICR0_ERR_AXI;
  327. raise_exception(EXCP_DBE);
  328. return 0;
  329. }
  330. switch (view) {
  331. case ITCVIEW_BYPASS:
  332. ret = view_bypass_read(cell);
  333. break;
  334. case ITCVIEW_CONTROL:
  335. ret = view_control_read(cell);
  336. break;
  337. case ITCVIEW_EF_SYNC:
  338. ret = view_ef_sync_read(cell);
  339. break;
  340. case ITCVIEW_EF_TRY:
  341. ret = view_ef_try_read(cell);
  342. break;
  343. case ITCVIEW_PV_SYNC:
  344. ret = view_pv_sync_read(cell);
  345. break;
  346. case ITCVIEW_PV_TRY:
  347. ret = view_pv_try_read(cell);
  348. break;
  349. case ITCVIEW_PV_ICR0:
  350. ret = s->icr0;
  351. break;
  352. default:
  353. qemu_log_mask(LOG_GUEST_ERROR,
  354. "itc_storage_read: Bad ITC View %d\n", (int)view);
  355. break;
  356. }
  357. return ret;
  358. }
  359. static void itc_storage_write(void *opaque, hwaddr addr, uint64_t data,
  360. unsigned size)
  361. {
  362. MIPSITUState *s = (MIPSITUState *)opaque;
  363. ITCStorageCell *cell = get_cell(s, addr);
  364. ITCView view = get_itc_view(addr);
  365. switch (size) {
  366. case 1:
  367. case 2:
  368. s->icr0 |= 1 << ITC_ICR0_ERR_AXI;
  369. raise_exception(EXCP_DBE);
  370. return;
  371. }
  372. switch (view) {
  373. case ITCVIEW_BYPASS:
  374. view_bypass_write(cell, data);
  375. break;
  376. case ITCVIEW_CONTROL:
  377. view_control_write(cell, data);
  378. break;
  379. case ITCVIEW_EF_SYNC:
  380. view_ef_sync_write(cell, data);
  381. break;
  382. case ITCVIEW_EF_TRY:
  383. view_ef_try_write(cell, data);
  384. break;
  385. case ITCVIEW_PV_SYNC:
  386. view_pv_sync_write(cell);
  387. break;
  388. case ITCVIEW_PV_TRY:
  389. view_pv_try_write(cell);
  390. break;
  391. case ITCVIEW_PV_ICR0:
  392. if (data & 0x7) {
  393. /* clear ERROR bits */
  394. s->icr0 &= ~(data & 0x7);
  395. }
  396. /* set BLK_GRAIN */
  397. s->icr0 &= ~0x700;
  398. s->icr0 |= data & 0x700;
  399. break;
  400. default:
  401. qemu_log_mask(LOG_GUEST_ERROR,
  402. "itc_storage_write: Bad ITC View %d\n", (int)view);
  403. break;
  404. }
  405. }
  406. static const MemoryRegionOps itc_storage_ops = {
  407. .read = itc_storage_read,
  408. .write = itc_storage_write,
  409. .endianness = DEVICE_NATIVE_ENDIAN,
  410. };
  411. static void itc_reset_cells(MIPSITUState *s)
  412. {
  413. int i;
  414. memset(s->cell, 0, get_num_cells(s) * sizeof(s->cell[0]));
  415. for (i = 0; i < s->num_fifo; i++) {
  416. s->cell[i].tag.E = 1;
  417. s->cell[i].tag.FIFO = 1;
  418. s->cell[i].tag.FIFODepth = ITC_CELL_DEPTH_SHIFT;
  419. }
  420. }
  421. static void mips_itu_init(Object *obj)
  422. {
  423. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  424. MIPSITUState *s = MIPS_ITU(obj);
  425. memory_region_init_io(&s->storage_io, OBJECT(s), &itc_storage_ops, s,
  426. "mips-itc-storage", ITC_STORAGE_ADDRSPACE_SZ);
  427. sysbus_init_mmio(sbd, &s->storage_io);
  428. memory_region_init_io(&s->tag_io, OBJECT(s), &itc_tag_ops, s,
  429. "mips-itc-tag", ITC_TAG_ADDRSPACE_SZ);
  430. }
  431. static void mips_itu_realize(DeviceState *dev, Error **errp)
  432. {
  433. MIPSITUState *s = MIPS_ITU(dev);
  434. if (s->num_fifo > ITC_FIFO_NUM_MAX) {
  435. error_setg(errp, "Exceed maximum number of FIFO cells: %d",
  436. s->num_fifo);
  437. return;
  438. }
  439. if (s->num_semaphores > ITC_SEMAPH_NUM_MAX) {
  440. error_setg(errp, "Exceed maximum number of Semaphore cells: %d",
  441. s->num_semaphores);
  442. return;
  443. }
  444. s->cell = g_new(ITCStorageCell, get_num_cells(s));
  445. }
  446. static void mips_itu_reset(DeviceState *dev)
  447. {
  448. MIPSITUState *s = MIPS_ITU(dev);
  449. if (s->saar_present) {
  450. *(uint64_t *) s->saar = 0x11 << 1;
  451. s->icr0 = get_num_cells(s) << ITC_ICR0_CELL_NUM;
  452. } else {
  453. s->ITCAddressMap[0] = 0;
  454. s->ITCAddressMap[1] =
  455. ((ITC_STORAGE_ADDRSPACE_SZ - 1) & ITC_AM1_ADDR_MASK_MASK) |
  456. (get_num_cells(s) << ITC_AM1_NUMENTRIES_OFS);
  457. }
  458. itc_reconfigure(s);
  459. itc_reset_cells(s);
  460. }
  461. static Property mips_itu_properties[] = {
  462. DEFINE_PROP_INT32("num-fifo", MIPSITUState, num_fifo,
  463. ITC_FIFO_NUM_MAX),
  464. DEFINE_PROP_INT32("num-semaphores", MIPSITUState, num_semaphores,
  465. ITC_SEMAPH_NUM_MAX),
  466. DEFINE_PROP_BOOL("saar-present", MIPSITUState, saar_present, false),
  467. DEFINE_PROP_END_OF_LIST(),
  468. };
  469. static void mips_itu_class_init(ObjectClass *klass, void *data)
  470. {
  471. DeviceClass *dc = DEVICE_CLASS(klass);
  472. dc->props = mips_itu_properties;
  473. dc->realize = mips_itu_realize;
  474. dc->reset = mips_itu_reset;
  475. }
  476. static const TypeInfo mips_itu_info = {
  477. .name = TYPE_MIPS_ITU,
  478. .parent = TYPE_SYS_BUS_DEVICE,
  479. .instance_size = sizeof(MIPSITUState),
  480. .instance_init = mips_itu_init,
  481. .class_init = mips_itu_class_init,
  482. };
  483. static void mips_itu_register_types(void)
  484. {
  485. type_register_static(&mips_itu_info);
  486. }
  487. type_init(mips_itu_register_types)