pl330.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  1. /*
  2. * ARM PrimeCell PL330 DMA Controller
  3. *
  4. * Copyright (c) 2009 Samsung Electronics.
  5. * Contributed by Kirill Batuzov <batuzovk@ispras.ru>
  6. * Copyright (c) 2012 Peter A.G. Crosthwaite (peter.crosthwaite@petalogix.com)
  7. * Copyright (c) 2012 PetaLogix Pty Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; version 2 or later.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "qemu/osdep.h"
  17. #include "hw/irq.h"
  18. #include "hw/qdev-properties.h"
  19. #include "hw/sysbus.h"
  20. #include "migration/vmstate.h"
  21. #include "qapi/error.h"
  22. #include "qemu/timer.h"
  23. #include "sysemu/dma.h"
  24. #include "qemu/log.h"
  25. #include "qemu/module.h"
  26. #include "trace.h"
  27. #include "qom/object.h"
  28. #ifndef PL330_ERR_DEBUG
  29. #define PL330_ERR_DEBUG 0
  30. #endif
  31. #define PL330_PERIPH_NUM 32
  32. #define PL330_MAX_BURST_LEN 128
  33. #define PL330_INSN_MAXSIZE 6
  34. #define PL330_FIFO_OK 0
  35. #define PL330_FIFO_STALL 1
  36. #define PL330_FIFO_ERR (-1)
  37. #define PL330_FAULT_UNDEF_INSTR (1 << 0)
  38. #define PL330_FAULT_OPERAND_INVALID (1 << 1)
  39. #define PL330_FAULT_DMAGO_ERR (1 << 4)
  40. #define PL330_FAULT_EVENT_ERR (1 << 5)
  41. #define PL330_FAULT_CH_PERIPH_ERR (1 << 6)
  42. #define PL330_FAULT_CH_RDWR_ERR (1 << 7)
  43. #define PL330_FAULT_ST_DATA_UNAVAILABLE (1 << 12)
  44. #define PL330_FAULT_FIFOEMPTY_ERR (1 << 13)
  45. #define PL330_FAULT_INSTR_FETCH_ERR (1 << 16)
  46. #define PL330_FAULT_DATA_WRITE_ERR (1 << 17)
  47. #define PL330_FAULT_DATA_READ_ERR (1 << 18)
  48. #define PL330_FAULT_DBG_INSTR (1 << 30)
  49. #define PL330_FAULT_LOCKUP_ERR (1 << 31)
  50. #define PL330_UNTAGGED 0xff
  51. #define PL330_SINGLE 0x0
  52. #define PL330_BURST 0x1
  53. #define PL330_WATCHDOG_LIMIT 1024
  54. /* IOMEM mapped registers */
  55. #define PL330_REG_DSR 0x000
  56. #define PL330_REG_DPC 0x004
  57. #define PL330_REG_INTEN 0x020
  58. #define PL330_REG_INT_EVENT_RIS 0x024
  59. #define PL330_REG_INTMIS 0x028
  60. #define PL330_REG_INTCLR 0x02C
  61. #define PL330_REG_FSRD 0x030
  62. #define PL330_REG_FSRC 0x034
  63. #define PL330_REG_FTRD 0x038
  64. #define PL330_REG_FTR_BASE 0x040
  65. #define PL330_REG_CSR_BASE 0x100
  66. #define PL330_REG_CPC_BASE 0x104
  67. #define PL330_REG_CHANCTRL 0x400
  68. #define PL330_REG_DBGSTATUS 0xD00
  69. #define PL330_REG_DBGCMD 0xD04
  70. #define PL330_REG_DBGINST0 0xD08
  71. #define PL330_REG_DBGINST1 0xD0C
  72. #define PL330_REG_CR0_BASE 0xE00
  73. #define PL330_REG_PERIPH_ID 0xFE0
  74. #define PL330_IOMEM_SIZE 0x1000
  75. #define CFG_BOOT_ADDR 2
  76. #define CFG_INS 3
  77. #define CFG_PNS 4
  78. #define CFG_CRD 5
  79. static const uint32_t pl330_id[] = {
  80. 0x30, 0x13, 0x24, 0x00, 0x0D, 0xF0, 0x05, 0xB1
  81. };
  82. /* DMA channel states as they are described in PL330 Technical Reference Manual
  83. * Most of them will not be used in emulation.
  84. */
  85. typedef enum {
  86. pl330_chan_stopped = 0,
  87. pl330_chan_executing = 1,
  88. pl330_chan_cache_miss = 2,
  89. pl330_chan_updating_pc = 3,
  90. pl330_chan_waiting_event = 4,
  91. pl330_chan_at_barrier = 5,
  92. pl330_chan_queue_busy = 6,
  93. pl330_chan_waiting_periph = 7,
  94. pl330_chan_killing = 8,
  95. pl330_chan_completing = 9,
  96. pl330_chan_fault_completing = 14,
  97. pl330_chan_fault = 15,
  98. } PL330ChanState;
  99. typedef struct PL330State PL330State;
  100. typedef struct PL330Chan {
  101. uint32_t src;
  102. uint32_t dst;
  103. uint32_t pc;
  104. uint32_t control;
  105. uint32_t status;
  106. uint32_t lc[2];
  107. uint32_t fault_type;
  108. uint32_t watchdog_timer;
  109. bool ns;
  110. uint8_t request_flag;
  111. uint8_t wakeup;
  112. uint8_t wfp_sbp;
  113. uint8_t state;
  114. uint8_t stall;
  115. bool is_manager;
  116. PL330State *parent;
  117. uint8_t tag;
  118. } PL330Chan;
  119. static const VMStateDescription vmstate_pl330_chan = {
  120. .name = "pl330_chan",
  121. .version_id = 1,
  122. .minimum_version_id = 1,
  123. .fields = (VMStateField[]) {
  124. VMSTATE_UINT32(src, PL330Chan),
  125. VMSTATE_UINT32(dst, PL330Chan),
  126. VMSTATE_UINT32(pc, PL330Chan),
  127. VMSTATE_UINT32(control, PL330Chan),
  128. VMSTATE_UINT32(status, PL330Chan),
  129. VMSTATE_UINT32_ARRAY(lc, PL330Chan, 2),
  130. VMSTATE_UINT32(fault_type, PL330Chan),
  131. VMSTATE_UINT32(watchdog_timer, PL330Chan),
  132. VMSTATE_BOOL(ns, PL330Chan),
  133. VMSTATE_UINT8(request_flag, PL330Chan),
  134. VMSTATE_UINT8(wakeup, PL330Chan),
  135. VMSTATE_UINT8(wfp_sbp, PL330Chan),
  136. VMSTATE_UINT8(state, PL330Chan),
  137. VMSTATE_UINT8(stall, PL330Chan),
  138. VMSTATE_END_OF_LIST()
  139. }
  140. };
  141. typedef struct PL330Fifo {
  142. uint8_t *buf;
  143. uint8_t *tag;
  144. uint32_t head;
  145. uint32_t num;
  146. uint32_t buf_size;
  147. } PL330Fifo;
  148. static const VMStateDescription vmstate_pl330_fifo = {
  149. .name = "pl330_chan",
  150. .version_id = 1,
  151. .minimum_version_id = 1,
  152. .fields = (VMStateField[]) {
  153. VMSTATE_VBUFFER_UINT32(buf, PL330Fifo, 1, NULL, buf_size),
  154. VMSTATE_VBUFFER_UINT32(tag, PL330Fifo, 1, NULL, buf_size),
  155. VMSTATE_UINT32(head, PL330Fifo),
  156. VMSTATE_UINT32(num, PL330Fifo),
  157. VMSTATE_UINT32(buf_size, PL330Fifo),
  158. VMSTATE_END_OF_LIST()
  159. }
  160. };
  161. typedef struct PL330QueueEntry {
  162. uint32_t addr;
  163. uint32_t len;
  164. uint8_t n;
  165. bool inc;
  166. bool z;
  167. uint8_t tag;
  168. uint8_t seqn;
  169. } PL330QueueEntry;
  170. static const VMStateDescription vmstate_pl330_queue_entry = {
  171. .name = "pl330_queue_entry",
  172. .version_id = 1,
  173. .minimum_version_id = 1,
  174. .fields = (VMStateField[]) {
  175. VMSTATE_UINT32(addr, PL330QueueEntry),
  176. VMSTATE_UINT32(len, PL330QueueEntry),
  177. VMSTATE_UINT8(n, PL330QueueEntry),
  178. VMSTATE_BOOL(inc, PL330QueueEntry),
  179. VMSTATE_BOOL(z, PL330QueueEntry),
  180. VMSTATE_UINT8(tag, PL330QueueEntry),
  181. VMSTATE_UINT8(seqn, PL330QueueEntry),
  182. VMSTATE_END_OF_LIST()
  183. }
  184. };
  185. typedef struct PL330Queue {
  186. PL330State *parent;
  187. PL330QueueEntry *queue;
  188. uint32_t queue_size;
  189. } PL330Queue;
  190. static const VMStateDescription vmstate_pl330_queue = {
  191. .name = "pl330_queue",
  192. .version_id = 2,
  193. .minimum_version_id = 2,
  194. .fields = (VMStateField[]) {
  195. VMSTATE_STRUCT_VARRAY_POINTER_UINT32(queue, PL330Queue, queue_size,
  196. vmstate_pl330_queue_entry,
  197. PL330QueueEntry),
  198. VMSTATE_END_OF_LIST()
  199. }
  200. };
  201. struct PL330State {
  202. SysBusDevice parent_obj;
  203. MemoryRegion iomem;
  204. qemu_irq irq_abort;
  205. qemu_irq *irq;
  206. /* Config registers. cfg[5] = CfgDn. */
  207. uint32_t cfg[6];
  208. #define EVENT_SEC_STATE 3
  209. #define PERIPH_SEC_STATE 4
  210. /* cfg 0 bits and pieces */
  211. uint32_t num_chnls;
  212. uint8_t num_periph_req;
  213. uint8_t num_events;
  214. uint8_t mgr_ns_at_rst;
  215. /* cfg 1 bits and pieces */
  216. uint8_t i_cache_len;
  217. uint8_t num_i_cache_lines;
  218. /* CRD bits and pieces */
  219. uint8_t data_width;
  220. uint8_t wr_cap;
  221. uint8_t wr_q_dep;
  222. uint8_t rd_cap;
  223. uint8_t rd_q_dep;
  224. uint16_t data_buffer_dep;
  225. PL330Chan manager;
  226. PL330Chan *chan;
  227. PL330Fifo fifo;
  228. PL330Queue read_queue;
  229. PL330Queue write_queue;
  230. uint8_t *lo_seqn;
  231. uint8_t *hi_seqn;
  232. QEMUTimer *timer; /* is used for restore dma. */
  233. uint32_t inten;
  234. uint32_t int_status;
  235. uint32_t ev_status;
  236. uint32_t dbg[2];
  237. uint8_t debug_status;
  238. uint8_t num_faulting;
  239. uint8_t periph_busy[PL330_PERIPH_NUM];
  240. /* Memory region that DMA operation access */
  241. MemoryRegion *mem_mr;
  242. AddressSpace *mem_as;
  243. };
  244. #define TYPE_PL330 "pl330"
  245. OBJECT_DECLARE_SIMPLE_TYPE(PL330State, PL330)
  246. static const VMStateDescription vmstate_pl330 = {
  247. .name = "pl330",
  248. .version_id = 2,
  249. .minimum_version_id = 2,
  250. .fields = (VMStateField[]) {
  251. VMSTATE_STRUCT(manager, PL330State, 0, vmstate_pl330_chan, PL330Chan),
  252. VMSTATE_STRUCT_VARRAY_POINTER_UINT32(chan, PL330State, num_chnls,
  253. vmstate_pl330_chan, PL330Chan),
  254. VMSTATE_VBUFFER_UINT32(lo_seqn, PL330State, 1, NULL, num_chnls),
  255. VMSTATE_VBUFFER_UINT32(hi_seqn, PL330State, 1, NULL, num_chnls),
  256. VMSTATE_STRUCT(fifo, PL330State, 0, vmstate_pl330_fifo, PL330Fifo),
  257. VMSTATE_STRUCT(read_queue, PL330State, 0, vmstate_pl330_queue,
  258. PL330Queue),
  259. VMSTATE_STRUCT(write_queue, PL330State, 0, vmstate_pl330_queue,
  260. PL330Queue),
  261. VMSTATE_TIMER_PTR(timer, PL330State),
  262. VMSTATE_UINT32(inten, PL330State),
  263. VMSTATE_UINT32(int_status, PL330State),
  264. VMSTATE_UINT32(ev_status, PL330State),
  265. VMSTATE_UINT32_ARRAY(dbg, PL330State, 2),
  266. VMSTATE_UINT8(debug_status, PL330State),
  267. VMSTATE_UINT8(num_faulting, PL330State),
  268. VMSTATE_UINT8_ARRAY(periph_busy, PL330State, PL330_PERIPH_NUM),
  269. VMSTATE_END_OF_LIST()
  270. }
  271. };
  272. typedef struct PL330InsnDesc {
  273. /* OPCODE of the instruction */
  274. uint8_t opcode;
  275. /* Mask so we can select several sibling instructions, such as
  276. DMALD, DMALDS and DMALDB */
  277. uint8_t opmask;
  278. /* Size of instruction in bytes */
  279. uint8_t size;
  280. /* Interpreter */
  281. void (*exec)(PL330Chan *, uint8_t opcode, uint8_t *args, int len);
  282. } PL330InsnDesc;
  283. static void pl330_hexdump(uint8_t *buf, size_t size)
  284. {
  285. unsigned int b, i, len;
  286. char tmpbuf[80];
  287. for (b = 0; b < size; b += 16) {
  288. len = size - b;
  289. if (len > 16) {
  290. len = 16;
  291. }
  292. tmpbuf[0] = '\0';
  293. for (i = 0; i < len; i++) {
  294. if ((i % 4) == 0) {
  295. strcat(tmpbuf, " ");
  296. }
  297. sprintf(tmpbuf + strlen(tmpbuf), " %02x", buf[b + i]);
  298. }
  299. trace_pl330_hexdump(b, tmpbuf);
  300. }
  301. }
  302. /* MFIFO Implementation
  303. *
  304. * MFIFO is implemented as a cyclic buffer of BUF_SIZE size. Tagged bytes are
  305. * stored in this buffer. Data is stored in BUF field, tags - in the
  306. * corresponding array elements of TAG field.
  307. */
  308. /* Initialize queue. */
  309. static void pl330_fifo_init(PL330Fifo *s, uint32_t size)
  310. {
  311. s->buf = g_malloc0(size);
  312. s->tag = g_malloc0(size);
  313. s->buf_size = size;
  314. }
  315. /* Cyclic increment */
  316. static inline int pl330_fifo_inc(PL330Fifo *s, int x)
  317. {
  318. return (x + 1) % s->buf_size;
  319. }
  320. /* Number of empty bytes in MFIFO */
  321. static inline int pl330_fifo_num_free(PL330Fifo *s)
  322. {
  323. return s->buf_size - s->num;
  324. }
  325. /* Push LEN bytes of data stored in BUF to MFIFO and tag it with TAG.
  326. * Zero returned on success, PL330_FIFO_STALL if there is no enough free
  327. * space in MFIFO to store requested amount of data. If push was unsuccessful
  328. * no data is stored to MFIFO.
  329. */
  330. static int pl330_fifo_push(PL330Fifo *s, uint8_t *buf, int len, uint8_t tag)
  331. {
  332. int i;
  333. if (s->buf_size - s->num < len) {
  334. return PL330_FIFO_STALL;
  335. }
  336. for (i = 0; i < len; i++) {
  337. int push_idx = (s->head + s->num + i) % s->buf_size;
  338. s->buf[push_idx] = buf[i];
  339. s->tag[push_idx] = tag;
  340. }
  341. s->num += len;
  342. return PL330_FIFO_OK;
  343. }
  344. /* Get LEN bytes of data from MFIFO and store it to BUF. Tag value of each
  345. * byte is verified. Zero returned on success, PL330_FIFO_ERR on tag mismatch
  346. * and PL330_FIFO_STALL if there is no enough data in MFIFO. If get was
  347. * unsuccessful no data is removed from MFIFO.
  348. */
  349. static int pl330_fifo_get(PL330Fifo *s, uint8_t *buf, int len, uint8_t tag)
  350. {
  351. int i;
  352. if (s->num < len) {
  353. return PL330_FIFO_STALL;
  354. }
  355. for (i = 0; i < len; i++) {
  356. if (s->tag[s->head] == tag) {
  357. int get_idx = (s->head + i) % s->buf_size;
  358. buf[i] = s->buf[get_idx];
  359. } else { /* Tag mismatch - Rollback transaction */
  360. return PL330_FIFO_ERR;
  361. }
  362. }
  363. s->head = (s->head + len) % s->buf_size;
  364. s->num -= len;
  365. return PL330_FIFO_OK;
  366. }
  367. /* Reset MFIFO. This completely erases all data in it. */
  368. static inline void pl330_fifo_reset(PL330Fifo *s)
  369. {
  370. s->head = 0;
  371. s->num = 0;
  372. }
  373. /* Return tag of the first byte stored in MFIFO. If MFIFO is empty
  374. * PL330_UNTAGGED is returned.
  375. */
  376. static inline uint8_t pl330_fifo_tag(PL330Fifo *s)
  377. {
  378. return (!s->num) ? PL330_UNTAGGED : s->tag[s->head];
  379. }
  380. /* Returns non-zero if tag TAG is present in fifo or zero otherwise */
  381. static int pl330_fifo_has_tag(PL330Fifo *s, uint8_t tag)
  382. {
  383. int i, n;
  384. i = s->head;
  385. for (n = 0; n < s->num; n++) {
  386. if (s->tag[i] == tag) {
  387. return 1;
  388. }
  389. i = pl330_fifo_inc(s, i);
  390. }
  391. return 0;
  392. }
  393. /* Remove all entry tagged with TAG from MFIFO */
  394. static void pl330_fifo_tagged_remove(PL330Fifo *s, uint8_t tag)
  395. {
  396. int i, t, n;
  397. t = i = s->head;
  398. for (n = 0; n < s->num; n++) {
  399. if (s->tag[i] != tag) {
  400. s->buf[t] = s->buf[i];
  401. s->tag[t] = s->tag[i];
  402. t = pl330_fifo_inc(s, t);
  403. } else {
  404. s->num = s->num - 1;
  405. }
  406. i = pl330_fifo_inc(s, i);
  407. }
  408. }
  409. /* Read-Write Queue implementation
  410. *
  411. * A Read-Write Queue stores up to QUEUE_SIZE instructions (loads or stores).
  412. * Each instruction is described by source (for loads) or destination (for
  413. * stores) address ADDR, width of data to be loaded/stored LEN, number of
  414. * stores/loads to be performed N, INC bit, Z bit and TAG to identify channel
  415. * this instruction belongs to. Queue does not store any information about
  416. * nature of the instruction: is it load or store. PL330 has different queues
  417. * for loads and stores so this is already known at the top level where it
  418. * matters.
  419. *
  420. * Queue works as FIFO for instructions with equivalent tags, but can issue
  421. * instructions with different tags in arbitrary order. SEQN field attached to
  422. * each instruction helps to achieve this. For each TAG queue contains
  423. * instructions with consecutive SEQN values ranging from LO_SEQN[TAG] to
  424. * HI_SEQN[TAG]-1 inclusive. SEQN is 8-bit unsigned integer, so SEQN=255 is
  425. * followed by SEQN=0.
  426. *
  427. * Z bit indicates that zeroes should be stored. No MFIFO fetches are performed
  428. * in this case.
  429. */
  430. static void pl330_queue_reset(PL330Queue *s)
  431. {
  432. int i;
  433. for (i = 0; i < s->queue_size; i++) {
  434. s->queue[i].tag = PL330_UNTAGGED;
  435. }
  436. }
  437. /* Initialize queue */
  438. static void pl330_queue_init(PL330Queue *s, int size, PL330State *parent)
  439. {
  440. s->parent = parent;
  441. s->queue = g_new0(PL330QueueEntry, size);
  442. s->queue_size = size;
  443. }
  444. /* Returns pointer to an empty slot or NULL if queue is full */
  445. static PL330QueueEntry *pl330_queue_find_empty(PL330Queue *s)
  446. {
  447. int i;
  448. for (i = 0; i < s->queue_size; i++) {
  449. if (s->queue[i].tag == PL330_UNTAGGED) {
  450. return &s->queue[i];
  451. }
  452. }
  453. return NULL;
  454. }
  455. /* Put instruction in queue.
  456. * Return value:
  457. * - zero - OK
  458. * - non-zero - queue is full
  459. */
  460. static int pl330_queue_put_insn(PL330Queue *s, uint32_t addr,
  461. int len, int n, bool inc, bool z, uint8_t tag)
  462. {
  463. PL330QueueEntry *entry = pl330_queue_find_empty(s);
  464. if (!entry) {
  465. return 1;
  466. }
  467. entry->tag = tag;
  468. entry->addr = addr;
  469. entry->len = len;
  470. entry->n = n;
  471. entry->z = z;
  472. entry->inc = inc;
  473. entry->seqn = s->parent->hi_seqn[tag];
  474. s->parent->hi_seqn[tag]++;
  475. return 0;
  476. }
  477. /* Returns a pointer to queue slot containing instruction which satisfies
  478. * following conditions:
  479. * - it has valid tag value (not PL330_UNTAGGED)
  480. * - if enforce_seq is set it has to be issuable without violating queue
  481. * logic (see above)
  482. * - if TAG argument is not PL330_UNTAGGED this instruction has tag value
  483. * equivalent to the argument TAG value.
  484. * If such instruction cannot be found NULL is returned.
  485. */
  486. static PL330QueueEntry *pl330_queue_find_insn(PL330Queue *s, uint8_t tag,
  487. bool enforce_seq)
  488. {
  489. int i;
  490. for (i = 0; i < s->queue_size; i++) {
  491. if (s->queue[i].tag != PL330_UNTAGGED) {
  492. if ((!enforce_seq ||
  493. s->queue[i].seqn == s->parent->lo_seqn[s->queue[i].tag]) &&
  494. (s->queue[i].tag == tag || tag == PL330_UNTAGGED ||
  495. s->queue[i].z)) {
  496. return &s->queue[i];
  497. }
  498. }
  499. }
  500. return NULL;
  501. }
  502. /* Removes instruction from queue. */
  503. static inline void pl330_queue_remove_insn(PL330Queue *s, PL330QueueEntry *e)
  504. {
  505. s->parent->lo_seqn[e->tag]++;
  506. e->tag = PL330_UNTAGGED;
  507. }
  508. /* Removes all instructions tagged with TAG from queue. */
  509. static inline void pl330_queue_remove_tagged(PL330Queue *s, uint8_t tag)
  510. {
  511. int i;
  512. for (i = 0; i < s->queue_size; i++) {
  513. if (s->queue[i].tag == tag) {
  514. s->queue[i].tag = PL330_UNTAGGED;
  515. }
  516. }
  517. }
  518. /* DMA instruction execution engine */
  519. /* Moves DMA channel to the FAULT state and updates it's status. */
  520. static inline void pl330_fault(PL330Chan *ch, uint32_t flags)
  521. {
  522. trace_pl330_fault(ch, flags);
  523. ch->fault_type |= flags;
  524. if (ch->state == pl330_chan_fault) {
  525. return;
  526. }
  527. ch->state = pl330_chan_fault;
  528. ch->parent->num_faulting++;
  529. if (ch->parent->num_faulting == 1) {
  530. trace_pl330_fault_abort();
  531. qemu_irq_raise(ch->parent->irq_abort);
  532. }
  533. }
  534. /*
  535. * For information about instructions see PL330 Technical Reference Manual.
  536. *
  537. * Arguments:
  538. * CH - channel executing the instruction
  539. * OPCODE - opcode
  540. * ARGS - array of 8-bit arguments
  541. * LEN - number of elements in ARGS array
  542. */
  543. static void pl330_dmaadxh(PL330Chan *ch, uint8_t *args, bool ra, bool neg)
  544. {
  545. uint32_t im = (args[1] << 8) | args[0];
  546. if (neg) {
  547. im |= 0xffffu << 16;
  548. }
  549. if (ch->is_manager) {
  550. pl330_fault(ch, PL330_FAULT_UNDEF_INSTR);
  551. return;
  552. }
  553. if (ra) {
  554. ch->dst += im;
  555. } else {
  556. ch->src += im;
  557. }
  558. }
  559. static void pl330_dmaaddh(PL330Chan *ch, uint8_t opcode, uint8_t *args, int len)
  560. {
  561. pl330_dmaadxh(ch, args, extract32(opcode, 1, 1), false);
  562. }
  563. static void pl330_dmaadnh(PL330Chan *ch, uint8_t opcode, uint8_t *args, int len)
  564. {
  565. pl330_dmaadxh(ch, args, extract32(opcode, 1, 1), true);
  566. }
  567. static void pl330_dmaend(PL330Chan *ch, uint8_t opcode,
  568. uint8_t *args, int len)
  569. {
  570. PL330State *s = ch->parent;
  571. if (ch->state == pl330_chan_executing && !ch->is_manager) {
  572. /* Wait for all transfers to complete */
  573. if (pl330_fifo_has_tag(&s->fifo, ch->tag) ||
  574. pl330_queue_find_insn(&s->read_queue, ch->tag, false) != NULL ||
  575. pl330_queue_find_insn(&s->write_queue, ch->tag, false) != NULL) {
  576. ch->stall = 1;
  577. return;
  578. }
  579. }
  580. trace_pl330_dmaend();
  581. pl330_fifo_tagged_remove(&s->fifo, ch->tag);
  582. pl330_queue_remove_tagged(&s->read_queue, ch->tag);
  583. pl330_queue_remove_tagged(&s->write_queue, ch->tag);
  584. ch->state = pl330_chan_stopped;
  585. }
  586. static void pl330_dmaflushp(PL330Chan *ch, uint8_t opcode,
  587. uint8_t *args, int len)
  588. {
  589. uint8_t periph_id;
  590. if (args[0] & 7) {
  591. pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
  592. return;
  593. }
  594. periph_id = (args[0] >> 3) & 0x1f;
  595. if (periph_id >= ch->parent->num_periph_req) {
  596. pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
  597. return;
  598. }
  599. if (ch->ns && !(ch->parent->cfg[CFG_PNS] & (1 << periph_id))) {
  600. pl330_fault(ch, PL330_FAULT_CH_PERIPH_ERR);
  601. return;
  602. }
  603. /* Do nothing */
  604. }
  605. static void pl330_dmago(PL330Chan *ch, uint8_t opcode, uint8_t *args, int len)
  606. {
  607. uint8_t chan_id;
  608. uint8_t ns;
  609. uint32_t pc;
  610. PL330Chan *s;
  611. trace_pl330_dmago();
  612. if (!ch->is_manager) {
  613. pl330_fault(ch, PL330_FAULT_UNDEF_INSTR);
  614. return;
  615. }
  616. ns = !!(opcode & 2);
  617. chan_id = args[0] & 7;
  618. if ((args[0] >> 3)) {
  619. pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
  620. return;
  621. }
  622. if (chan_id >= ch->parent->num_chnls) {
  623. pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
  624. return;
  625. }
  626. pc = (((uint32_t)args[4]) << 24) | (((uint32_t)args[3]) << 16) |
  627. (((uint32_t)args[2]) << 8) | (((uint32_t)args[1]));
  628. if (ch->parent->chan[chan_id].state != pl330_chan_stopped) {
  629. pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
  630. return;
  631. }
  632. if (ch->ns && !ns) {
  633. pl330_fault(ch, PL330_FAULT_DMAGO_ERR);
  634. return;
  635. }
  636. s = &ch->parent->chan[chan_id];
  637. s->ns = ns;
  638. s->pc = pc;
  639. s->state = pl330_chan_executing;
  640. }
  641. static void pl330_dmald(PL330Chan *ch, uint8_t opcode, uint8_t *args, int len)
  642. {
  643. uint8_t bs = opcode & 3;
  644. uint32_t size, num;
  645. bool inc;
  646. if (bs == 2) {
  647. pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
  648. return;
  649. }
  650. if ((bs == 1 && ch->request_flag == PL330_BURST) ||
  651. (bs == 3 && ch->request_flag == PL330_SINGLE)) {
  652. /* Perform NOP */
  653. return;
  654. }
  655. if (bs == 1 && ch->request_flag == PL330_SINGLE) {
  656. num = 1;
  657. } else {
  658. num = ((ch->control >> 4) & 0xf) + 1;
  659. }
  660. size = (uint32_t)1 << ((ch->control >> 1) & 0x7);
  661. inc = !!(ch->control & 1);
  662. ch->stall = pl330_queue_put_insn(&ch->parent->read_queue, ch->src,
  663. size, num, inc, 0, ch->tag);
  664. if (!ch->stall) {
  665. trace_pl330_dmald(ch->tag, ch->src, size, num, inc ? 'Y' : 'N');
  666. ch->src += inc ? size * num - (ch->src & (size - 1)) : 0;
  667. }
  668. }
  669. static void pl330_dmaldp(PL330Chan *ch, uint8_t opcode, uint8_t *args, int len)
  670. {
  671. uint8_t periph_id;
  672. if (args[0] & 7) {
  673. pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
  674. return;
  675. }
  676. periph_id = (args[0] >> 3) & 0x1f;
  677. if (periph_id >= ch->parent->num_periph_req) {
  678. pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
  679. return;
  680. }
  681. if (ch->ns && !(ch->parent->cfg[CFG_PNS] & (1 << periph_id))) {
  682. pl330_fault(ch, PL330_FAULT_CH_PERIPH_ERR);
  683. return;
  684. }
  685. pl330_dmald(ch, opcode, args, len);
  686. }
  687. static void pl330_dmalp(PL330Chan *ch, uint8_t opcode, uint8_t *args, int len)
  688. {
  689. uint8_t lc = (opcode & 2) >> 1;
  690. ch->lc[lc] = args[0];
  691. }
  692. static void pl330_dmakill(PL330Chan *ch, uint8_t opcode, uint8_t *args, int len)
  693. {
  694. if (ch->state == pl330_chan_fault ||
  695. ch->state == pl330_chan_fault_completing) {
  696. /* This is the only way for a channel to leave the faulting state */
  697. ch->fault_type = 0;
  698. ch->parent->num_faulting--;
  699. if (ch->parent->num_faulting == 0) {
  700. trace_pl330_dmakill();
  701. qemu_irq_lower(ch->parent->irq_abort);
  702. }
  703. }
  704. ch->state = pl330_chan_killing;
  705. pl330_fifo_tagged_remove(&ch->parent->fifo, ch->tag);
  706. pl330_queue_remove_tagged(&ch->parent->read_queue, ch->tag);
  707. pl330_queue_remove_tagged(&ch->parent->write_queue, ch->tag);
  708. ch->state = pl330_chan_stopped;
  709. }
  710. static void pl330_dmalpend(PL330Chan *ch, uint8_t opcode,
  711. uint8_t *args, int len)
  712. {
  713. uint8_t nf = (opcode & 0x10) >> 4;
  714. uint8_t bs = opcode & 3;
  715. uint8_t lc = (opcode & 4) >> 2;
  716. trace_pl330_dmalpend(nf, bs, lc, ch->lc[lc], ch->request_flag);
  717. if (bs == 2) {
  718. pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
  719. return;
  720. }
  721. if ((bs == 1 && ch->request_flag == PL330_BURST) ||
  722. (bs == 3 && ch->request_flag == PL330_SINGLE)) {
  723. /* Perform NOP */
  724. return;
  725. }
  726. if (!nf || ch->lc[lc]) {
  727. if (nf) {
  728. ch->lc[lc]--;
  729. }
  730. trace_pl330_dmalpiter();
  731. ch->pc -= args[0];
  732. ch->pc -= len + 1;
  733. /* "ch->pc -= args[0] + len + 1" is incorrect when args[0] == 256 */
  734. } else {
  735. trace_pl330_dmalpfallthrough();
  736. }
  737. }
  738. static void pl330_dmamov(PL330Chan *ch, uint8_t opcode, uint8_t *args, int len)
  739. {
  740. uint8_t rd = args[0] & 7;
  741. uint32_t im;
  742. if ((args[0] >> 3)) {
  743. pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
  744. return;
  745. }
  746. im = (((uint32_t)args[4]) << 24) | (((uint32_t)args[3]) << 16) |
  747. (((uint32_t)args[2]) << 8) | (((uint32_t)args[1]));
  748. switch (rd) {
  749. case 0:
  750. ch->src = im;
  751. break;
  752. case 1:
  753. ch->control = im;
  754. break;
  755. case 2:
  756. ch->dst = im;
  757. break;
  758. default:
  759. pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
  760. return;
  761. }
  762. }
  763. static void pl330_dmanop(PL330Chan *ch, uint8_t opcode,
  764. uint8_t *args, int len)
  765. {
  766. /* NOP is NOP. */
  767. }
  768. static void pl330_dmarmb(PL330Chan *ch, uint8_t opcode, uint8_t *args, int len)
  769. {
  770. if (pl330_queue_find_insn(&ch->parent->read_queue, ch->tag, false)) {
  771. ch->state = pl330_chan_at_barrier;
  772. ch->stall = 1;
  773. return;
  774. } else {
  775. ch->state = pl330_chan_executing;
  776. }
  777. }
  778. static void pl330_dmasev(PL330Chan *ch, uint8_t opcode, uint8_t *args, int len)
  779. {
  780. uint8_t ev_id;
  781. if (args[0] & 7) {
  782. pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
  783. return;
  784. }
  785. ev_id = (args[0] >> 3) & 0x1f;
  786. if (ev_id >= ch->parent->num_events) {
  787. pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
  788. return;
  789. }
  790. if (ch->ns && !(ch->parent->cfg[CFG_INS] & (1 << ev_id))) {
  791. pl330_fault(ch, PL330_FAULT_EVENT_ERR);
  792. return;
  793. }
  794. if (ch->parent->inten & (1 << ev_id)) {
  795. ch->parent->int_status |= (1 << ev_id);
  796. trace_pl330_dmasev_evirq(ev_id);
  797. qemu_irq_raise(ch->parent->irq[ev_id]);
  798. }
  799. trace_pl330_dmasev_event(ev_id);
  800. ch->parent->ev_status |= (1 << ev_id);
  801. }
  802. static void pl330_dmast(PL330Chan *ch, uint8_t opcode, uint8_t *args, int len)
  803. {
  804. uint8_t bs = opcode & 3;
  805. uint32_t size, num;
  806. bool inc;
  807. if (bs == 2) {
  808. pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
  809. return;
  810. }
  811. if ((bs == 1 && ch->request_flag == PL330_BURST) ||
  812. (bs == 3 && ch->request_flag == PL330_SINGLE)) {
  813. /* Perform NOP */
  814. return;
  815. }
  816. num = ((ch->control >> 18) & 0xf) + 1;
  817. size = (uint32_t)1 << ((ch->control >> 15) & 0x7);
  818. inc = !!((ch->control >> 14) & 1);
  819. ch->stall = pl330_queue_put_insn(&ch->parent->write_queue, ch->dst,
  820. size, num, inc, 0, ch->tag);
  821. if (!ch->stall) {
  822. trace_pl330_dmast(ch->tag, ch->dst, size, num, inc ? 'Y' : 'N');
  823. ch->dst += inc ? size * num - (ch->dst & (size - 1)) : 0;
  824. }
  825. }
  826. static void pl330_dmastp(PL330Chan *ch, uint8_t opcode,
  827. uint8_t *args, int len)
  828. {
  829. uint8_t periph_id;
  830. if (args[0] & 7) {
  831. pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
  832. return;
  833. }
  834. periph_id = (args[0] >> 3) & 0x1f;
  835. if (periph_id >= ch->parent->num_periph_req) {
  836. pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
  837. return;
  838. }
  839. if (ch->ns && !(ch->parent->cfg[CFG_PNS] & (1 << periph_id))) {
  840. pl330_fault(ch, PL330_FAULT_CH_PERIPH_ERR);
  841. return;
  842. }
  843. pl330_dmast(ch, opcode, args, len);
  844. }
  845. static void pl330_dmastz(PL330Chan *ch, uint8_t opcode,
  846. uint8_t *args, int len)
  847. {
  848. uint32_t size, num;
  849. bool inc;
  850. num = ((ch->control >> 18) & 0xf) + 1;
  851. size = (uint32_t)1 << ((ch->control >> 15) & 0x7);
  852. inc = !!((ch->control >> 14) & 1);
  853. ch->stall = pl330_queue_put_insn(&ch->parent->write_queue, ch->dst,
  854. size, num, inc, 1, ch->tag);
  855. if (inc) {
  856. ch->dst += size * num;
  857. }
  858. }
  859. static void pl330_dmawfe(PL330Chan *ch, uint8_t opcode,
  860. uint8_t *args, int len)
  861. {
  862. uint8_t ev_id;
  863. int i;
  864. if (args[0] & 5) {
  865. pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
  866. return;
  867. }
  868. ev_id = (args[0] >> 3) & 0x1f;
  869. if (ev_id >= ch->parent->num_events) {
  870. pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
  871. return;
  872. }
  873. if (ch->ns && !(ch->parent->cfg[CFG_INS] & (1 << ev_id))) {
  874. pl330_fault(ch, PL330_FAULT_EVENT_ERR);
  875. return;
  876. }
  877. ch->wakeup = ev_id;
  878. ch->state = pl330_chan_waiting_event;
  879. if (~ch->parent->inten & ch->parent->ev_status & 1 << ev_id) {
  880. ch->state = pl330_chan_executing;
  881. /* If anyone else is currently waiting on the same event, let them
  882. * clear the ev_status so they pick up event as well
  883. */
  884. for (i = 0; i < ch->parent->num_chnls; ++i) {
  885. PL330Chan *peer = &ch->parent->chan[i];
  886. if (peer->state == pl330_chan_waiting_event &&
  887. peer->wakeup == ev_id) {
  888. return;
  889. }
  890. }
  891. ch->parent->ev_status &= ~(1 << ev_id);
  892. trace_pl330_dmawfe(ev_id);
  893. } else {
  894. ch->stall = 1;
  895. }
  896. }
  897. static void pl330_dmawfp(PL330Chan *ch, uint8_t opcode,
  898. uint8_t *args, int len)
  899. {
  900. uint8_t bs = opcode & 3;
  901. uint8_t periph_id;
  902. if (args[0] & 7) {
  903. pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
  904. return;
  905. }
  906. periph_id = (args[0] >> 3) & 0x1f;
  907. if (periph_id >= ch->parent->num_periph_req) {
  908. pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
  909. return;
  910. }
  911. if (ch->ns && !(ch->parent->cfg[CFG_PNS] & (1 << periph_id))) {
  912. pl330_fault(ch, PL330_FAULT_CH_PERIPH_ERR);
  913. return;
  914. }
  915. switch (bs) {
  916. case 0: /* S */
  917. ch->request_flag = PL330_SINGLE;
  918. ch->wfp_sbp = 0;
  919. break;
  920. case 1: /* P */
  921. ch->request_flag = PL330_BURST;
  922. ch->wfp_sbp = 2;
  923. break;
  924. case 2: /* B */
  925. ch->request_flag = PL330_BURST;
  926. ch->wfp_sbp = 1;
  927. break;
  928. default:
  929. pl330_fault(ch, PL330_FAULT_OPERAND_INVALID);
  930. return;
  931. }
  932. if (ch->parent->periph_busy[periph_id]) {
  933. ch->state = pl330_chan_waiting_periph;
  934. ch->stall = 1;
  935. } else if (ch->state == pl330_chan_waiting_periph) {
  936. ch->state = pl330_chan_executing;
  937. }
  938. }
  939. static void pl330_dmawmb(PL330Chan *ch, uint8_t opcode,
  940. uint8_t *args, int len)
  941. {
  942. if (pl330_queue_find_insn(&ch->parent->write_queue, ch->tag, false)) {
  943. ch->state = pl330_chan_at_barrier;
  944. ch->stall = 1;
  945. return;
  946. } else {
  947. ch->state = pl330_chan_executing;
  948. }
  949. }
  950. /* NULL terminated array of the instruction descriptions. */
  951. static const PL330InsnDesc insn_desc[] = {
  952. { .opcode = 0x54, .opmask = 0xFD, .size = 3, .exec = pl330_dmaaddh, },
  953. { .opcode = 0x5c, .opmask = 0xFD, .size = 3, .exec = pl330_dmaadnh, },
  954. { .opcode = 0x00, .opmask = 0xFF, .size = 1, .exec = pl330_dmaend, },
  955. { .opcode = 0x35, .opmask = 0xFF, .size = 2, .exec = pl330_dmaflushp, },
  956. { .opcode = 0xA0, .opmask = 0xFD, .size = 6, .exec = pl330_dmago, },
  957. { .opcode = 0x04, .opmask = 0xFC, .size = 1, .exec = pl330_dmald, },
  958. { .opcode = 0x25, .opmask = 0xFD, .size = 2, .exec = pl330_dmaldp, },
  959. { .opcode = 0x20, .opmask = 0xFD, .size = 2, .exec = pl330_dmalp, },
  960. /* dmastp must be before dmalpend in this list, because their maps
  961. * are overlapping
  962. */
  963. { .opcode = 0x29, .opmask = 0xFD, .size = 2, .exec = pl330_dmastp, },
  964. { .opcode = 0x28, .opmask = 0xE8, .size = 2, .exec = pl330_dmalpend, },
  965. { .opcode = 0x01, .opmask = 0xFF, .size = 1, .exec = pl330_dmakill, },
  966. { .opcode = 0xBC, .opmask = 0xFF, .size = 6, .exec = pl330_dmamov, },
  967. { .opcode = 0x18, .opmask = 0xFF, .size = 1, .exec = pl330_dmanop, },
  968. { .opcode = 0x12, .opmask = 0xFF, .size = 1, .exec = pl330_dmarmb, },
  969. { .opcode = 0x34, .opmask = 0xFF, .size = 2, .exec = pl330_dmasev, },
  970. { .opcode = 0x08, .opmask = 0xFC, .size = 1, .exec = pl330_dmast, },
  971. { .opcode = 0x0C, .opmask = 0xFF, .size = 1, .exec = pl330_dmastz, },
  972. { .opcode = 0x36, .opmask = 0xFF, .size = 2, .exec = pl330_dmawfe, },
  973. { .opcode = 0x30, .opmask = 0xFC, .size = 2, .exec = pl330_dmawfp, },
  974. { .opcode = 0x13, .opmask = 0xFF, .size = 1, .exec = pl330_dmawmb, },
  975. { .opcode = 0x00, .opmask = 0x00, .size = 0, .exec = NULL, }
  976. };
  977. /* Instructions which can be issued via debug registers. */
  978. static const PL330InsnDesc debug_insn_desc[] = {
  979. { .opcode = 0xA0, .opmask = 0xFD, .size = 6, .exec = pl330_dmago, },
  980. { .opcode = 0x01, .opmask = 0xFF, .size = 1, .exec = pl330_dmakill, },
  981. { .opcode = 0x34, .opmask = 0xFF, .size = 2, .exec = pl330_dmasev, },
  982. { .opcode = 0x00, .opmask = 0x00, .size = 0, .exec = NULL, }
  983. };
  984. static inline const PL330InsnDesc *pl330_fetch_insn(PL330Chan *ch)
  985. {
  986. uint8_t opcode;
  987. int i;
  988. dma_memory_read(ch->parent->mem_as, ch->pc, &opcode, 1,
  989. MEMTXATTRS_UNSPECIFIED);
  990. for (i = 0; insn_desc[i].size; i++) {
  991. if ((opcode & insn_desc[i].opmask) == insn_desc[i].opcode) {
  992. return &insn_desc[i];
  993. }
  994. }
  995. return NULL;
  996. }
  997. static inline void pl330_exec_insn(PL330Chan *ch, const PL330InsnDesc *insn)
  998. {
  999. uint8_t buf[PL330_INSN_MAXSIZE];
  1000. assert(insn->size <= PL330_INSN_MAXSIZE);
  1001. dma_memory_read(ch->parent->mem_as, ch->pc, buf, insn->size,
  1002. MEMTXATTRS_UNSPECIFIED);
  1003. insn->exec(ch, buf[0], &buf[1], insn->size - 1);
  1004. }
  1005. static inline void pl330_update_pc(PL330Chan *ch,
  1006. const PL330InsnDesc *insn)
  1007. {
  1008. ch->pc += insn->size;
  1009. }
  1010. /* Try to execute current instruction in channel CH. Number of executed
  1011. instructions is returned (0 or 1). */
  1012. static int pl330_chan_exec(PL330Chan *ch)
  1013. {
  1014. const PL330InsnDesc *insn;
  1015. if (ch->state != pl330_chan_executing &&
  1016. ch->state != pl330_chan_waiting_periph &&
  1017. ch->state != pl330_chan_at_barrier &&
  1018. ch->state != pl330_chan_waiting_event) {
  1019. return 0;
  1020. }
  1021. ch->stall = 0;
  1022. insn = pl330_fetch_insn(ch);
  1023. if (!insn) {
  1024. trace_pl330_chan_exec_undef();
  1025. pl330_fault(ch, PL330_FAULT_UNDEF_INSTR);
  1026. return 0;
  1027. }
  1028. pl330_exec_insn(ch, insn);
  1029. if (!ch->stall) {
  1030. pl330_update_pc(ch, insn);
  1031. ch->watchdog_timer = 0;
  1032. return 1;
  1033. /* WDT only active in exec state */
  1034. } else if (ch->state == pl330_chan_executing) {
  1035. ch->watchdog_timer++;
  1036. if (ch->watchdog_timer >= PL330_WATCHDOG_LIMIT) {
  1037. pl330_fault(ch, PL330_FAULT_LOCKUP_ERR);
  1038. }
  1039. }
  1040. return 0;
  1041. }
  1042. /* Try to execute 1 instruction in each channel, one instruction from read
  1043. queue and one instruction from write queue. Number of successfully executed
  1044. instructions is returned. */
  1045. static int pl330_exec_cycle(PL330Chan *channel)
  1046. {
  1047. PL330State *s = channel->parent;
  1048. PL330QueueEntry *q;
  1049. int i;
  1050. int num_exec = 0;
  1051. int fifo_res = 0;
  1052. uint8_t buf[PL330_MAX_BURST_LEN];
  1053. /* Execute one instruction in each channel */
  1054. num_exec += pl330_chan_exec(channel);
  1055. /* Execute one instruction from read queue */
  1056. q = pl330_queue_find_insn(&s->read_queue, PL330_UNTAGGED, true);
  1057. if (q != NULL && q->len <= pl330_fifo_num_free(&s->fifo)) {
  1058. int len = q->len - (q->addr & (q->len - 1));
  1059. dma_memory_read(s->mem_as, q->addr, buf, len,
  1060. MEMTXATTRS_UNSPECIFIED);
  1061. trace_pl330_exec_cycle(q->addr, len);
  1062. if (trace_event_get_state_backends(TRACE_PL330_HEXDUMP)) {
  1063. pl330_hexdump(buf, len);
  1064. }
  1065. fifo_res = pl330_fifo_push(&s->fifo, buf, len, q->tag);
  1066. if (fifo_res == PL330_FIFO_OK) {
  1067. if (q->inc) {
  1068. q->addr += len;
  1069. }
  1070. q->n--;
  1071. if (!q->n) {
  1072. pl330_queue_remove_insn(&s->read_queue, q);
  1073. }
  1074. num_exec++;
  1075. }
  1076. }
  1077. /* Execute one instruction from write queue. */
  1078. q = pl330_queue_find_insn(&s->write_queue, pl330_fifo_tag(&s->fifo), true);
  1079. if (q != NULL) {
  1080. int len = q->len - (q->addr & (q->len - 1));
  1081. if (q->z) {
  1082. for (i = 0; i < len; i++) {
  1083. buf[i] = 0;
  1084. }
  1085. } else {
  1086. fifo_res = pl330_fifo_get(&s->fifo, buf, len, q->tag);
  1087. }
  1088. if (fifo_res == PL330_FIFO_OK || q->z) {
  1089. dma_memory_write(s->mem_as, q->addr, buf, len,
  1090. MEMTXATTRS_UNSPECIFIED);
  1091. trace_pl330_exec_cycle(q->addr, len);
  1092. if (trace_event_get_state_backends(TRACE_PL330_HEXDUMP)) {
  1093. pl330_hexdump(buf, len);
  1094. }
  1095. if (q->inc) {
  1096. q->addr += len;
  1097. }
  1098. num_exec++;
  1099. } else if (fifo_res == PL330_FIFO_STALL) {
  1100. pl330_fault(&channel->parent->chan[q->tag],
  1101. PL330_FAULT_FIFOEMPTY_ERR);
  1102. }
  1103. q->n--;
  1104. if (!q->n) {
  1105. pl330_queue_remove_insn(&s->write_queue, q);
  1106. }
  1107. }
  1108. return num_exec;
  1109. }
  1110. static int pl330_exec_channel(PL330Chan *channel)
  1111. {
  1112. int insr_exec = 0;
  1113. /* TODO: Is it all right to execute everything or should we do per-cycle
  1114. simulation? */
  1115. while (pl330_exec_cycle(channel)) {
  1116. insr_exec++;
  1117. }
  1118. /* Detect deadlock */
  1119. if (channel->state == pl330_chan_executing) {
  1120. pl330_fault(channel, PL330_FAULT_LOCKUP_ERR);
  1121. }
  1122. /* Situation when one of the queues has deadlocked but all channels
  1123. * have finished their programs should be impossible.
  1124. */
  1125. return insr_exec;
  1126. }
  1127. static inline void pl330_exec(PL330State *s)
  1128. {
  1129. int i, insr_exec;
  1130. trace_pl330_exec();
  1131. do {
  1132. insr_exec = pl330_exec_channel(&s->manager);
  1133. for (i = 0; i < s->num_chnls; i++) {
  1134. insr_exec += pl330_exec_channel(&s->chan[i]);
  1135. }
  1136. } while (insr_exec);
  1137. }
  1138. static void pl330_exec_cycle_timer(void *opaque)
  1139. {
  1140. PL330State *s = (PL330State *)opaque;
  1141. pl330_exec(s);
  1142. }
  1143. /* Stop or restore dma operations */
  1144. static void pl330_dma_stop_irq(void *opaque, int irq, int level)
  1145. {
  1146. PL330State *s = (PL330State *)opaque;
  1147. if (s->periph_busy[irq] != level) {
  1148. s->periph_busy[irq] = level;
  1149. timer_mod(s->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
  1150. }
  1151. }
  1152. static void pl330_debug_exec(PL330State *s)
  1153. {
  1154. uint8_t args[5];
  1155. uint8_t opcode;
  1156. uint8_t chan_id;
  1157. int i;
  1158. PL330Chan *ch;
  1159. const PL330InsnDesc *insn;
  1160. s->debug_status = 1;
  1161. chan_id = (s->dbg[0] >> 8) & 0x07;
  1162. opcode = (s->dbg[0] >> 16) & 0xff;
  1163. args[0] = (s->dbg[0] >> 24) & 0xff;
  1164. args[1] = (s->dbg[1] >> 0) & 0xff;
  1165. args[2] = (s->dbg[1] >> 8) & 0xff;
  1166. args[3] = (s->dbg[1] >> 16) & 0xff;
  1167. args[4] = (s->dbg[1] >> 24) & 0xff;
  1168. trace_pl330_debug_exec(chan_id);
  1169. if (s->dbg[0] & 1) {
  1170. ch = &s->chan[chan_id];
  1171. } else {
  1172. ch = &s->manager;
  1173. }
  1174. insn = NULL;
  1175. for (i = 0; debug_insn_desc[i].size; i++) {
  1176. if ((opcode & debug_insn_desc[i].opmask) == debug_insn_desc[i].opcode) {
  1177. insn = &debug_insn_desc[i];
  1178. }
  1179. }
  1180. if (!insn) {
  1181. pl330_fault(ch, PL330_FAULT_UNDEF_INSTR | PL330_FAULT_DBG_INSTR);
  1182. return ;
  1183. }
  1184. ch->stall = 0;
  1185. insn->exec(ch, opcode, args, insn->size - 1);
  1186. if (ch->fault_type) {
  1187. ch->fault_type |= PL330_FAULT_DBG_INSTR;
  1188. }
  1189. if (ch->stall) {
  1190. trace_pl330_debug_exec_stall();
  1191. qemu_log_mask(LOG_UNIMP, "pl330: stall of debug instruction not "
  1192. "implemented\n");
  1193. }
  1194. s->debug_status = 0;
  1195. }
  1196. /* IOMEM mapped registers */
  1197. static void pl330_iomem_write(void *opaque, hwaddr offset,
  1198. uint64_t value, unsigned size)
  1199. {
  1200. PL330State *s = (PL330State *) opaque;
  1201. int i;
  1202. trace_pl330_iomem_write((unsigned)offset, (unsigned)value);
  1203. switch (offset) {
  1204. case PL330_REG_INTEN:
  1205. s->inten = value;
  1206. break;
  1207. case PL330_REG_INTCLR:
  1208. for (i = 0; i < s->num_events; i++) {
  1209. if (s->int_status & s->inten & value & (1 << i)) {
  1210. trace_pl330_iomem_write_clr(i);
  1211. qemu_irq_lower(s->irq[i]);
  1212. }
  1213. }
  1214. s->ev_status &= ~(value & s->inten);
  1215. s->int_status &= ~(value & s->inten);
  1216. break;
  1217. case PL330_REG_DBGCMD:
  1218. if ((value & 3) == 0) {
  1219. pl330_debug_exec(s);
  1220. pl330_exec(s);
  1221. } else {
  1222. qemu_log_mask(LOG_GUEST_ERROR, "pl330: write of illegal value %u "
  1223. "for offset " TARGET_FMT_plx "\n", (unsigned)value,
  1224. offset);
  1225. }
  1226. break;
  1227. case PL330_REG_DBGINST0:
  1228. s->dbg[0] = value;
  1229. break;
  1230. case PL330_REG_DBGINST1:
  1231. s->dbg[1] = value;
  1232. break;
  1233. default:
  1234. qemu_log_mask(LOG_GUEST_ERROR, "pl330: bad write offset " TARGET_FMT_plx
  1235. "\n", offset);
  1236. break;
  1237. }
  1238. }
  1239. static inline uint32_t pl330_iomem_read_imp(void *opaque,
  1240. hwaddr offset)
  1241. {
  1242. PL330State *s = (PL330State *)opaque;
  1243. int chan_id;
  1244. int i;
  1245. uint32_t res;
  1246. if (offset >= PL330_REG_PERIPH_ID && offset < PL330_REG_PERIPH_ID + 32) {
  1247. return pl330_id[(offset - PL330_REG_PERIPH_ID) >> 2];
  1248. }
  1249. if (offset >= PL330_REG_CR0_BASE && offset < PL330_REG_CR0_BASE + 24) {
  1250. return s->cfg[(offset - PL330_REG_CR0_BASE) >> 2];
  1251. }
  1252. if (offset >= PL330_REG_CHANCTRL && offset < PL330_REG_DBGSTATUS) {
  1253. offset -= PL330_REG_CHANCTRL;
  1254. chan_id = offset >> 5;
  1255. if (chan_id >= s->num_chnls) {
  1256. qemu_log_mask(LOG_GUEST_ERROR, "pl330: bad read offset "
  1257. TARGET_FMT_plx "\n", offset);
  1258. return 0;
  1259. }
  1260. switch (offset & 0x1f) {
  1261. case 0x00:
  1262. return s->chan[chan_id].src;
  1263. case 0x04:
  1264. return s->chan[chan_id].dst;
  1265. case 0x08:
  1266. return s->chan[chan_id].control;
  1267. case 0x0C:
  1268. return s->chan[chan_id].lc[0];
  1269. case 0x10:
  1270. return s->chan[chan_id].lc[1];
  1271. default:
  1272. qemu_log_mask(LOG_GUEST_ERROR, "pl330: bad read offset "
  1273. TARGET_FMT_plx "\n", offset);
  1274. return 0;
  1275. }
  1276. }
  1277. if (offset >= PL330_REG_CSR_BASE && offset < 0x400) {
  1278. offset -= PL330_REG_CSR_BASE;
  1279. chan_id = offset >> 3;
  1280. if (chan_id >= s->num_chnls) {
  1281. qemu_log_mask(LOG_GUEST_ERROR, "pl330: bad read offset "
  1282. TARGET_FMT_plx "\n", offset);
  1283. return 0;
  1284. }
  1285. switch ((offset >> 2) & 1) {
  1286. case 0x0:
  1287. res = (s->chan[chan_id].ns << 21) |
  1288. (s->chan[chan_id].wakeup << 4) |
  1289. (s->chan[chan_id].state) |
  1290. (s->chan[chan_id].wfp_sbp << 14);
  1291. return res;
  1292. case 0x1:
  1293. return s->chan[chan_id].pc;
  1294. default:
  1295. qemu_log_mask(LOG_GUEST_ERROR, "pl330: read error\n");
  1296. return 0;
  1297. }
  1298. }
  1299. if (offset >= PL330_REG_FTR_BASE && offset < 0x100) {
  1300. offset -= PL330_REG_FTR_BASE;
  1301. chan_id = offset >> 2;
  1302. if (chan_id >= s->num_chnls) {
  1303. qemu_log_mask(LOG_GUEST_ERROR, "pl330: bad read offset "
  1304. TARGET_FMT_plx "\n", offset);
  1305. return 0;
  1306. }
  1307. return s->chan[chan_id].fault_type;
  1308. }
  1309. switch (offset) {
  1310. case PL330_REG_DSR:
  1311. return (s->manager.ns << 9) | (s->manager.wakeup << 4) |
  1312. (s->manager.state & 0xf);
  1313. case PL330_REG_DPC:
  1314. return s->manager.pc;
  1315. case PL330_REG_INTEN:
  1316. return s->inten;
  1317. case PL330_REG_INT_EVENT_RIS:
  1318. return s->ev_status;
  1319. case PL330_REG_INTMIS:
  1320. return s->int_status;
  1321. case PL330_REG_INTCLR:
  1322. /* Documentation says that we can't read this register
  1323. * but linux kernel does it
  1324. */
  1325. return 0;
  1326. case PL330_REG_FSRD:
  1327. return s->manager.state ? 1 : 0;
  1328. case PL330_REG_FSRC:
  1329. res = 0;
  1330. for (i = 0; i < s->num_chnls; i++) {
  1331. if (s->chan[i].state == pl330_chan_fault ||
  1332. s->chan[i].state == pl330_chan_fault_completing) {
  1333. res |= 1 << i;
  1334. }
  1335. }
  1336. return res;
  1337. case PL330_REG_FTRD:
  1338. return s->manager.fault_type;
  1339. case PL330_REG_DBGSTATUS:
  1340. return s->debug_status;
  1341. default:
  1342. qemu_log_mask(LOG_GUEST_ERROR, "pl330: bad read offset "
  1343. TARGET_FMT_plx "\n", offset);
  1344. }
  1345. return 0;
  1346. }
  1347. static uint64_t pl330_iomem_read(void *opaque, hwaddr offset,
  1348. unsigned size)
  1349. {
  1350. uint32_t ret = pl330_iomem_read_imp(opaque, offset);
  1351. trace_pl330_iomem_read((uint32_t)offset, ret);
  1352. return ret;
  1353. }
  1354. static const MemoryRegionOps pl330_ops = {
  1355. .read = pl330_iomem_read,
  1356. .write = pl330_iomem_write,
  1357. .endianness = DEVICE_NATIVE_ENDIAN,
  1358. .impl = {
  1359. .min_access_size = 4,
  1360. .max_access_size = 4,
  1361. }
  1362. };
  1363. /* Controller logic and initialization */
  1364. static void pl330_chan_reset(PL330Chan *ch)
  1365. {
  1366. ch->src = 0;
  1367. ch->dst = 0;
  1368. ch->pc = 0;
  1369. ch->state = pl330_chan_stopped;
  1370. ch->watchdog_timer = 0;
  1371. ch->stall = 0;
  1372. ch->control = 0;
  1373. ch->status = 0;
  1374. ch->fault_type = 0;
  1375. }
  1376. static void pl330_reset(DeviceState *d)
  1377. {
  1378. int i;
  1379. PL330State *s = PL330(d);
  1380. s->inten = 0;
  1381. s->int_status = 0;
  1382. s->ev_status = 0;
  1383. s->debug_status = 0;
  1384. s->num_faulting = 0;
  1385. s->manager.ns = s->mgr_ns_at_rst;
  1386. pl330_fifo_reset(&s->fifo);
  1387. pl330_queue_reset(&s->read_queue);
  1388. pl330_queue_reset(&s->write_queue);
  1389. for (i = 0; i < s->num_chnls; i++) {
  1390. pl330_chan_reset(&s->chan[i]);
  1391. }
  1392. for (i = 0; i < s->num_periph_req; i++) {
  1393. s->periph_busy[i] = 0;
  1394. }
  1395. timer_del(s->timer);
  1396. }
  1397. static void pl330_realize(DeviceState *dev, Error **errp)
  1398. {
  1399. int i;
  1400. PL330State *s = PL330(dev);
  1401. sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->irq_abort);
  1402. memory_region_init_io(&s->iomem, OBJECT(s), &pl330_ops, s,
  1403. "dma", PL330_IOMEM_SIZE);
  1404. sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem);
  1405. if (!s->mem_mr) {
  1406. error_setg(errp, "'memory' link is not set");
  1407. return;
  1408. } else if (s->mem_mr == get_system_memory()) {
  1409. /* Avoid creating new AS for system memory. */
  1410. s->mem_as = &address_space_memory;
  1411. } else {
  1412. s->mem_as = g_new0(AddressSpace, 1);
  1413. address_space_init(s->mem_as, s->mem_mr,
  1414. memory_region_name(s->mem_mr));
  1415. }
  1416. s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, pl330_exec_cycle_timer, s);
  1417. s->cfg[0] = (s->mgr_ns_at_rst ? 0x4 : 0) |
  1418. (s->num_periph_req > 0 ? 1 : 0) |
  1419. ((s->num_chnls - 1) & 0x7) << 4 |
  1420. ((s->num_periph_req - 1) & 0x1f) << 12 |
  1421. ((s->num_events - 1) & 0x1f) << 17;
  1422. switch (s->i_cache_len) {
  1423. case (4):
  1424. s->cfg[1] |= 2;
  1425. break;
  1426. case (8):
  1427. s->cfg[1] |= 3;
  1428. break;
  1429. case (16):
  1430. s->cfg[1] |= 4;
  1431. break;
  1432. case (32):
  1433. s->cfg[1] |= 5;
  1434. break;
  1435. default:
  1436. error_setg(errp, "Bad value for i-cache_len property: %" PRIx8,
  1437. s->i_cache_len);
  1438. return;
  1439. }
  1440. s->cfg[1] |= ((s->num_i_cache_lines - 1) & 0xf) << 4;
  1441. s->chan = g_new0(PL330Chan, s->num_chnls);
  1442. s->hi_seqn = g_new0(uint8_t, s->num_chnls);
  1443. s->lo_seqn = g_new0(uint8_t, s->num_chnls);
  1444. for (i = 0; i < s->num_chnls; i++) {
  1445. s->chan[i].parent = s;
  1446. s->chan[i].tag = (uint8_t)i;
  1447. }
  1448. s->manager.parent = s;
  1449. s->manager.tag = s->num_chnls;
  1450. s->manager.is_manager = true;
  1451. s->irq = g_new0(qemu_irq, s->num_events);
  1452. for (i = 0; i < s->num_events; i++) {
  1453. sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->irq[i]);
  1454. }
  1455. qdev_init_gpio_in(dev, pl330_dma_stop_irq, PL330_PERIPH_NUM);
  1456. switch (s->data_width) {
  1457. case (32):
  1458. s->cfg[CFG_CRD] |= 0x2;
  1459. break;
  1460. case (64):
  1461. s->cfg[CFG_CRD] |= 0x3;
  1462. break;
  1463. case (128):
  1464. s->cfg[CFG_CRD] |= 0x4;
  1465. break;
  1466. default:
  1467. error_setg(errp, "Bad value for data_width property: %" PRIx8,
  1468. s->data_width);
  1469. return;
  1470. }
  1471. s->cfg[CFG_CRD] |= ((s->wr_cap - 1) & 0x7) << 4 |
  1472. ((s->wr_q_dep - 1) & 0xf) << 8 |
  1473. ((s->rd_cap - 1) & 0x7) << 12 |
  1474. ((s->rd_q_dep - 1) & 0xf) << 16 |
  1475. ((s->data_buffer_dep - 1) & 0x1ff) << 20;
  1476. pl330_queue_init(&s->read_queue, s->rd_q_dep, s);
  1477. pl330_queue_init(&s->write_queue, s->wr_q_dep, s);
  1478. pl330_fifo_init(&s->fifo, s->data_width / 4 * s->data_buffer_dep);
  1479. }
  1480. static Property pl330_properties[] = {
  1481. /* CR0 */
  1482. DEFINE_PROP_UINT32("num_chnls", PL330State, num_chnls, 8),
  1483. DEFINE_PROP_UINT8("num_periph_req", PL330State, num_periph_req, 4),
  1484. DEFINE_PROP_UINT8("num_events", PL330State, num_events, 16),
  1485. DEFINE_PROP_UINT8("mgr_ns_at_rst", PL330State, mgr_ns_at_rst, 0),
  1486. /* CR1 */
  1487. DEFINE_PROP_UINT8("i-cache_len", PL330State, i_cache_len, 4),
  1488. DEFINE_PROP_UINT8("num_i-cache_lines", PL330State, num_i_cache_lines, 8),
  1489. /* CR2-4 */
  1490. DEFINE_PROP_UINT32("boot_addr", PL330State, cfg[CFG_BOOT_ADDR], 0),
  1491. DEFINE_PROP_UINT32("INS", PL330State, cfg[CFG_INS], 0),
  1492. DEFINE_PROP_UINT32("PNS", PL330State, cfg[CFG_PNS], 0),
  1493. /* CRD */
  1494. DEFINE_PROP_UINT8("data_width", PL330State, data_width, 64),
  1495. DEFINE_PROP_UINT8("wr_cap", PL330State, wr_cap, 8),
  1496. DEFINE_PROP_UINT8("wr_q_dep", PL330State, wr_q_dep, 16),
  1497. DEFINE_PROP_UINT8("rd_cap", PL330State, rd_cap, 8),
  1498. DEFINE_PROP_UINT8("rd_q_dep", PL330State, rd_q_dep, 16),
  1499. DEFINE_PROP_UINT16("data_buffer_dep", PL330State, data_buffer_dep, 256),
  1500. DEFINE_PROP_LINK("memory", PL330State, mem_mr,
  1501. TYPE_MEMORY_REGION, MemoryRegion *),
  1502. DEFINE_PROP_END_OF_LIST(),
  1503. };
  1504. static void pl330_class_init(ObjectClass *klass, void *data)
  1505. {
  1506. DeviceClass *dc = DEVICE_CLASS(klass);
  1507. dc->realize = pl330_realize;
  1508. dc->reset = pl330_reset;
  1509. device_class_set_props(dc, pl330_properties);
  1510. dc->vmsd = &vmstate_pl330;
  1511. }
  1512. static const TypeInfo pl330_type_info = {
  1513. .name = TYPE_PL330,
  1514. .parent = TYPE_SYS_BUS_DEVICE,
  1515. .instance_size = sizeof(PL330State),
  1516. .class_init = pl330_class_init,
  1517. };
  1518. static void pl330_register_types(void)
  1519. {
  1520. type_register_static(&pl330_type_info);
  1521. }
  1522. type_init(pl330_register_types)