pl330.c 49 KB

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