pl330.c 49 KB

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