openpic.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644
  1. /*
  2. * OpenPIC emulation
  3. *
  4. * Copyright (c) 2004 Jocelyn Mayer
  5. * 2011 Alexander Graf
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. /*
  26. *
  27. * Based on OpenPic implementations:
  28. * - Motorola MPC8245 & MPC8540 user manuals.
  29. * - Motorola Harrier programmer manual
  30. *
  31. */
  32. #include "qemu/osdep.h"
  33. #include "hw/irq.h"
  34. #include "hw/pci/pci.h"
  35. #include "hw/ppc/openpic.h"
  36. #include "hw/ppc/ppc_e500.h"
  37. #include "hw/qdev-properties.h"
  38. #include "hw/sysbus.h"
  39. #include "migration/vmstate.h"
  40. #include "hw/pci/msi.h"
  41. #include "qapi/error.h"
  42. #include "qemu/bitops.h"
  43. #include "qapi/qmp/qerror.h"
  44. #include "qemu/module.h"
  45. #include "qemu/timer.h"
  46. #include "qemu/error-report.h"
  47. /* #define DEBUG_OPENPIC */
  48. #ifdef DEBUG_OPENPIC
  49. static const int debug_openpic = 1;
  50. #else
  51. static const int debug_openpic = 0;
  52. #endif
  53. static int get_current_cpu(void);
  54. #define DPRINTF(fmt, ...) do { \
  55. if (debug_openpic) { \
  56. info_report("Core%d: " fmt, get_current_cpu(), ## __VA_ARGS__); \
  57. } \
  58. } while (0)
  59. /* OpenPIC capability flags */
  60. #define OPENPIC_FLAG_IDR_CRIT (1 << 0)
  61. #define OPENPIC_FLAG_ILR (2 << 0)
  62. /* OpenPIC address map */
  63. #define OPENPIC_GLB_REG_START 0x0
  64. #define OPENPIC_GLB_REG_SIZE 0x10F0
  65. #define OPENPIC_TMR_REG_START 0x10F0
  66. #define OPENPIC_TMR_REG_SIZE 0x220
  67. #define OPENPIC_MSI_REG_START 0x1600
  68. #define OPENPIC_MSI_REG_SIZE 0x200
  69. #define OPENPIC_SUMMARY_REG_START 0x3800
  70. #define OPENPIC_SUMMARY_REG_SIZE 0x800
  71. #define OPENPIC_SRC_REG_START 0x10000
  72. #define OPENPIC_SRC_REG_SIZE (OPENPIC_MAX_SRC * 0x20)
  73. #define OPENPIC_CPU_REG_START 0x20000
  74. #define OPENPIC_CPU_REG_SIZE 0x100 + ((MAX_CPU - 1) * 0x1000)
  75. static FslMpicInfo fsl_mpic_20 = {
  76. .max_ext = 12,
  77. };
  78. static FslMpicInfo fsl_mpic_42 = {
  79. .max_ext = 12,
  80. };
  81. #define FRR_NIRQ_SHIFT 16
  82. #define FRR_NCPU_SHIFT 8
  83. #define FRR_VID_SHIFT 0
  84. #define VID_REVISION_1_2 2
  85. #define VID_REVISION_1_3 3
  86. #define VIR_GENERIC 0x00000000 /* Generic Vendor ID */
  87. #define VIR_MPIC2A 0x00004614 /* IBM MPIC-2A */
  88. #define GCR_RESET 0x80000000
  89. #define GCR_MODE_PASS 0x00000000
  90. #define GCR_MODE_MIXED 0x20000000
  91. #define GCR_MODE_PROXY 0x60000000
  92. #define TBCR_CI 0x80000000 /* count inhibit */
  93. #define TCCR_TOG 0x80000000 /* toggles when decrement to zero */
  94. #define IDR_EP_SHIFT 31
  95. #define IDR_EP_MASK (1U << IDR_EP_SHIFT)
  96. #define IDR_CI0_SHIFT 30
  97. #define IDR_CI1_SHIFT 29
  98. #define IDR_P1_SHIFT 1
  99. #define IDR_P0_SHIFT 0
  100. #define ILR_INTTGT_MASK 0x000000ff
  101. #define ILR_INTTGT_INT 0x00
  102. #define ILR_INTTGT_CINT 0x01 /* critical */
  103. #define ILR_INTTGT_MCP 0x02 /* machine check */
  104. /*
  105. * The currently supported INTTGT values happen to be the same as QEMU's
  106. * openpic output codes, but don't depend on this. The output codes
  107. * could change (unlikely, but...) or support could be added for
  108. * more INTTGT values.
  109. */
  110. static const int inttgt_output[][2] = {
  111. { ILR_INTTGT_INT, OPENPIC_OUTPUT_INT },
  112. { ILR_INTTGT_CINT, OPENPIC_OUTPUT_CINT },
  113. { ILR_INTTGT_MCP, OPENPIC_OUTPUT_MCK },
  114. };
  115. static int inttgt_to_output(int inttgt)
  116. {
  117. int i;
  118. for (i = 0; i < ARRAY_SIZE(inttgt_output); i++) {
  119. if (inttgt_output[i][0] == inttgt) {
  120. return inttgt_output[i][1];
  121. }
  122. }
  123. error_report("%s: unsupported inttgt %d", __func__, inttgt);
  124. return OPENPIC_OUTPUT_INT;
  125. }
  126. static int output_to_inttgt(int output)
  127. {
  128. int i;
  129. for (i = 0; i < ARRAY_SIZE(inttgt_output); i++) {
  130. if (inttgt_output[i][1] == output) {
  131. return inttgt_output[i][0];
  132. }
  133. }
  134. abort();
  135. }
  136. #define MSIIR_OFFSET 0x140
  137. #define MSIIR_SRS_SHIFT 29
  138. #define MSIIR_SRS_MASK (0x7 << MSIIR_SRS_SHIFT)
  139. #define MSIIR_IBS_SHIFT 24
  140. #define MSIIR_IBS_MASK (0x1f << MSIIR_IBS_SHIFT)
  141. static int get_current_cpu(void)
  142. {
  143. if (!current_cpu) {
  144. return -1;
  145. }
  146. return current_cpu->cpu_index;
  147. }
  148. static uint32_t openpic_cpu_read_internal(void *opaque, hwaddr addr,
  149. int idx);
  150. static void openpic_cpu_write_internal(void *opaque, hwaddr addr,
  151. uint32_t val, int idx);
  152. static void openpic_reset(DeviceState *d);
  153. /*
  154. * Convert between openpic clock ticks and nanosecs. In the hardware the clock
  155. * frequency is driven by board inputs to the PIC which the PIC would then
  156. * divide by 4 or 8. For now hard code to 25MZ.
  157. */
  158. #define OPENPIC_TIMER_FREQ_MHZ 25
  159. #define OPENPIC_TIMER_NS_PER_TICK (1000 / OPENPIC_TIMER_FREQ_MHZ)
  160. static inline uint64_t ns_to_ticks(uint64_t ns)
  161. {
  162. return ns / OPENPIC_TIMER_NS_PER_TICK;
  163. }
  164. static inline uint64_t ticks_to_ns(uint64_t ticks)
  165. {
  166. return ticks * OPENPIC_TIMER_NS_PER_TICK;
  167. }
  168. static inline void IRQ_setbit(IRQQueue *q, int n_IRQ)
  169. {
  170. set_bit(n_IRQ, q->queue);
  171. }
  172. static inline void IRQ_resetbit(IRQQueue *q, int n_IRQ)
  173. {
  174. clear_bit(n_IRQ, q->queue);
  175. }
  176. static void IRQ_check(OpenPICState *opp, IRQQueue *q)
  177. {
  178. int irq = -1;
  179. int next = -1;
  180. int priority = -1;
  181. for (;;) {
  182. irq = find_next_bit(q->queue, opp->max_irq, irq + 1);
  183. if (irq == opp->max_irq) {
  184. break;
  185. }
  186. DPRINTF("IRQ_check: irq %d set ivpr_pr=%d pr=%d",
  187. irq, IVPR_PRIORITY(opp->src[irq].ivpr), priority);
  188. if (IVPR_PRIORITY(opp->src[irq].ivpr) > priority) {
  189. next = irq;
  190. priority = IVPR_PRIORITY(opp->src[irq].ivpr);
  191. }
  192. }
  193. q->next = next;
  194. q->priority = priority;
  195. }
  196. static int IRQ_get_next(OpenPICState *opp, IRQQueue *q)
  197. {
  198. /* XXX: optimize */
  199. IRQ_check(opp, q);
  200. return q->next;
  201. }
  202. static void IRQ_local_pipe(OpenPICState *opp, int n_CPU, int n_IRQ,
  203. bool active, bool was_active)
  204. {
  205. IRQDest *dst;
  206. IRQSource *src;
  207. int priority;
  208. dst = &opp->dst[n_CPU];
  209. src = &opp->src[n_IRQ];
  210. DPRINTF("%s: IRQ %d active %d was %d",
  211. __func__, n_IRQ, active, was_active);
  212. if (src->output != OPENPIC_OUTPUT_INT) {
  213. DPRINTF("%s: output %d irq %d active %d was %d count %d",
  214. __func__, src->output, n_IRQ, active, was_active,
  215. dst->outputs_active[src->output]);
  216. /*
  217. * On Freescale MPIC, critical interrupts ignore priority,
  218. * IACK, EOI, etc. Before MPIC v4.1 they also ignore
  219. * masking.
  220. */
  221. if (active) {
  222. if (!was_active && dst->outputs_active[src->output]++ == 0) {
  223. DPRINTF("%s: Raise OpenPIC output %d cpu %d irq %d",
  224. __func__, src->output, n_CPU, n_IRQ);
  225. qemu_irq_raise(dst->irqs[src->output]);
  226. }
  227. } else {
  228. if (was_active && --dst->outputs_active[src->output] == 0) {
  229. DPRINTF("%s: Lower OpenPIC output %d cpu %d irq %d",
  230. __func__, src->output, n_CPU, n_IRQ);
  231. qemu_irq_lower(dst->irqs[src->output]);
  232. }
  233. }
  234. return;
  235. }
  236. priority = IVPR_PRIORITY(src->ivpr);
  237. /*
  238. * Even if the interrupt doesn't have enough priority,
  239. * it is still raised, in case ctpr is lowered later.
  240. */
  241. if (active) {
  242. IRQ_setbit(&dst->raised, n_IRQ);
  243. } else {
  244. IRQ_resetbit(&dst->raised, n_IRQ);
  245. }
  246. IRQ_check(opp, &dst->raised);
  247. if (active && priority <= dst->ctpr) {
  248. DPRINTF("%s: IRQ %d priority %d too low for ctpr %d on CPU %d",
  249. __func__, n_IRQ, priority, dst->ctpr, n_CPU);
  250. active = 0;
  251. }
  252. if (active) {
  253. if (IRQ_get_next(opp, &dst->servicing) >= 0 &&
  254. priority <= dst->servicing.priority) {
  255. DPRINTF("%s: IRQ %d is hidden by servicing IRQ %d on CPU %d",
  256. __func__, n_IRQ, dst->servicing.next, n_CPU);
  257. } else {
  258. DPRINTF("%s: Raise OpenPIC INT output cpu %d irq %d/%d",
  259. __func__, n_CPU, n_IRQ, dst->raised.next);
  260. qemu_irq_raise(opp->dst[n_CPU].irqs[OPENPIC_OUTPUT_INT]);
  261. }
  262. } else {
  263. IRQ_get_next(opp, &dst->servicing);
  264. if (dst->raised.priority > dst->ctpr &&
  265. dst->raised.priority > dst->servicing.priority) {
  266. DPRINTF("%s: IRQ %d inactive, IRQ %d prio %d above %d/%d, CPU %d",
  267. __func__, n_IRQ, dst->raised.next, dst->raised.priority,
  268. dst->ctpr, dst->servicing.priority, n_CPU);
  269. /* IRQ line stays asserted */
  270. } else {
  271. DPRINTF("%s: IRQ %d inactive, current prio %d/%d, CPU %d",
  272. __func__, n_IRQ, dst->ctpr, dst->servicing.priority, n_CPU);
  273. qemu_irq_lower(opp->dst[n_CPU].irqs[OPENPIC_OUTPUT_INT]);
  274. }
  275. }
  276. }
  277. /* update pic state because registers for n_IRQ have changed value */
  278. static void openpic_update_irq(OpenPICState *opp, int n_IRQ)
  279. {
  280. IRQSource *src;
  281. bool active, was_active;
  282. int i;
  283. src = &opp->src[n_IRQ];
  284. active = src->pending;
  285. if ((src->ivpr & IVPR_MASK_MASK) && !src->nomask) {
  286. /* Interrupt source is disabled */
  287. DPRINTF("%s: IRQ %d is disabled", __func__, n_IRQ);
  288. active = false;
  289. }
  290. was_active = !!(src->ivpr & IVPR_ACTIVITY_MASK);
  291. /*
  292. * We don't have a similar check for already-active because
  293. * ctpr may have changed and we need to withdraw the interrupt.
  294. */
  295. if (!active && !was_active) {
  296. DPRINTF("%s: IRQ %d is already inactive", __func__, n_IRQ);
  297. return;
  298. }
  299. if (active) {
  300. src->ivpr |= IVPR_ACTIVITY_MASK;
  301. } else {
  302. src->ivpr &= ~IVPR_ACTIVITY_MASK;
  303. }
  304. if (src->destmask == 0) {
  305. /* No target */
  306. DPRINTF("%s: IRQ %d has no target", __func__, n_IRQ);
  307. return;
  308. }
  309. if (src->destmask == (1 << src->last_cpu)) {
  310. /* Only one CPU is allowed to receive this IRQ */
  311. IRQ_local_pipe(opp, src->last_cpu, n_IRQ, active, was_active);
  312. } else if (!(src->ivpr & IVPR_MODE_MASK)) {
  313. /* Directed delivery mode */
  314. for (i = 0; i < opp->nb_cpus; i++) {
  315. if (src->destmask & (1 << i)) {
  316. IRQ_local_pipe(opp, i, n_IRQ, active, was_active);
  317. }
  318. }
  319. } else {
  320. /* Distributed delivery mode */
  321. for (i = src->last_cpu + 1; i != src->last_cpu; i++) {
  322. if (i == opp->nb_cpus) {
  323. i = 0;
  324. }
  325. if (src->destmask & (1 << i)) {
  326. IRQ_local_pipe(opp, i, n_IRQ, active, was_active);
  327. src->last_cpu = i;
  328. break;
  329. }
  330. }
  331. }
  332. }
  333. static void openpic_set_irq(void *opaque, int n_IRQ, int level)
  334. {
  335. OpenPICState *opp = opaque;
  336. IRQSource *src;
  337. if (n_IRQ >= OPENPIC_MAX_IRQ) {
  338. error_report("%s: IRQ %d out of range", __func__, n_IRQ);
  339. abort();
  340. }
  341. src = &opp->src[n_IRQ];
  342. DPRINTF("openpic: set irq %d = %d ivpr=0x%08x",
  343. n_IRQ, level, src->ivpr);
  344. if (src->level) {
  345. /* level-sensitive irq */
  346. src->pending = level;
  347. openpic_update_irq(opp, n_IRQ);
  348. } else {
  349. /* edge-sensitive irq */
  350. if (level) {
  351. src->pending = 1;
  352. openpic_update_irq(opp, n_IRQ);
  353. }
  354. if (src->output != OPENPIC_OUTPUT_INT) {
  355. /*
  356. * Edge-triggered interrupts shouldn't be used
  357. * with non-INT delivery, but just in case,
  358. * try to make it do something sane rather than
  359. * cause an interrupt storm. This is close to
  360. * what you'd probably see happen in real hardware.
  361. */
  362. src->pending = 0;
  363. openpic_update_irq(opp, n_IRQ);
  364. }
  365. }
  366. }
  367. static inline uint32_t read_IRQreg_idr(OpenPICState *opp, int n_IRQ)
  368. {
  369. return opp->src[n_IRQ].idr;
  370. }
  371. static inline uint32_t read_IRQreg_ilr(OpenPICState *opp, int n_IRQ)
  372. {
  373. if (opp->flags & OPENPIC_FLAG_ILR) {
  374. return output_to_inttgt(opp->src[n_IRQ].output);
  375. }
  376. return 0xffffffff;
  377. }
  378. static inline uint32_t read_IRQreg_ivpr(OpenPICState *opp, int n_IRQ)
  379. {
  380. return opp->src[n_IRQ].ivpr;
  381. }
  382. static inline void write_IRQreg_idr(OpenPICState *opp, int n_IRQ, uint32_t val)
  383. {
  384. IRQSource *src = &opp->src[n_IRQ];
  385. uint32_t normal_mask = (1UL << opp->nb_cpus) - 1;
  386. uint32_t crit_mask = 0;
  387. uint32_t mask = normal_mask;
  388. int crit_shift = IDR_EP_SHIFT - opp->nb_cpus;
  389. int i;
  390. if (opp->flags & OPENPIC_FLAG_IDR_CRIT) {
  391. crit_mask = mask << crit_shift;
  392. mask |= crit_mask | IDR_EP;
  393. }
  394. src->idr = val & mask;
  395. DPRINTF("Set IDR %d to 0x%08x", n_IRQ, src->idr);
  396. if (opp->flags & OPENPIC_FLAG_IDR_CRIT) {
  397. if (src->idr & crit_mask) {
  398. if (src->idr & normal_mask) {
  399. DPRINTF("%s: IRQ configured for multiple output types, using "
  400. "critical", __func__);
  401. }
  402. src->output = OPENPIC_OUTPUT_CINT;
  403. src->nomask = true;
  404. src->destmask = 0;
  405. for (i = 0; i < opp->nb_cpus; i++) {
  406. int n_ci = IDR_CI0_SHIFT - i;
  407. if (src->idr & (1UL << n_ci)) {
  408. src->destmask |= 1UL << i;
  409. }
  410. }
  411. } else {
  412. src->output = OPENPIC_OUTPUT_INT;
  413. src->nomask = false;
  414. src->destmask = src->idr & normal_mask;
  415. }
  416. } else {
  417. src->destmask = src->idr;
  418. }
  419. }
  420. static inline void write_IRQreg_ilr(OpenPICState *opp, int n_IRQ, uint32_t val)
  421. {
  422. if (opp->flags & OPENPIC_FLAG_ILR) {
  423. IRQSource *src = &opp->src[n_IRQ];
  424. src->output = inttgt_to_output(val & ILR_INTTGT_MASK);
  425. DPRINTF("Set ILR %d to 0x%08x, output %d", n_IRQ, src->idr,
  426. src->output);
  427. /* TODO: on MPIC v4.0 only, set nomask for non-INT */
  428. }
  429. }
  430. static inline void write_IRQreg_ivpr(OpenPICState *opp, int n_IRQ, uint32_t val)
  431. {
  432. uint32_t mask;
  433. /*
  434. * NOTE when implementing newer FSL MPIC models: starting with v4.0,
  435. * the polarity bit is read-only on internal interrupts.
  436. */
  437. mask = IVPR_MASK_MASK | IVPR_PRIORITY_MASK | IVPR_SENSE_MASK |
  438. IVPR_POLARITY_MASK | opp->vector_mask;
  439. /* ACTIVITY bit is read-only */
  440. opp->src[n_IRQ].ivpr =
  441. (opp->src[n_IRQ].ivpr & IVPR_ACTIVITY_MASK) | (val & mask);
  442. /*
  443. * For FSL internal interrupts, The sense bit is reserved and zero,
  444. * and the interrupt is always level-triggered. Timers and IPIs
  445. * have no sense or polarity bits, and are edge-triggered.
  446. */
  447. switch (opp->src[n_IRQ].type) {
  448. case IRQ_TYPE_NORMAL:
  449. opp->src[n_IRQ].level = !!(opp->src[n_IRQ].ivpr & IVPR_SENSE_MASK);
  450. break;
  451. case IRQ_TYPE_FSLINT:
  452. opp->src[n_IRQ].ivpr &= ~IVPR_SENSE_MASK;
  453. break;
  454. case IRQ_TYPE_FSLSPECIAL:
  455. opp->src[n_IRQ].ivpr &= ~(IVPR_POLARITY_MASK | IVPR_SENSE_MASK);
  456. break;
  457. }
  458. openpic_update_irq(opp, n_IRQ);
  459. DPRINTF("Set IVPR %d to 0x%08x -> 0x%08x", n_IRQ, val,
  460. opp->src[n_IRQ].ivpr);
  461. }
  462. static void openpic_gcr_write(OpenPICState *opp, uint64_t val)
  463. {
  464. bool mpic_proxy = false;
  465. if (val & GCR_RESET) {
  466. openpic_reset(DEVICE(opp));
  467. return;
  468. }
  469. opp->gcr &= ~opp->mpic_mode_mask;
  470. opp->gcr |= val & opp->mpic_mode_mask;
  471. /* Set external proxy mode */
  472. if ((val & opp->mpic_mode_mask) == GCR_MODE_PROXY) {
  473. mpic_proxy = true;
  474. }
  475. ppce500_set_mpic_proxy(mpic_proxy);
  476. }
  477. static void openpic_gbl_write(void *opaque, hwaddr addr, uint64_t val,
  478. unsigned len)
  479. {
  480. OpenPICState *opp = opaque;
  481. IRQDest *dst;
  482. int idx;
  483. DPRINTF("%s: addr %#" HWADDR_PRIx " <= %08" PRIx64,
  484. __func__, addr, val);
  485. if (addr & 0xF) {
  486. return;
  487. }
  488. switch (addr) {
  489. case 0x00: /* Block Revision Register1 (BRR1) is Readonly */
  490. break;
  491. case 0x40:
  492. case 0x50:
  493. case 0x60:
  494. case 0x70:
  495. case 0x80:
  496. case 0x90:
  497. case 0xA0:
  498. case 0xB0:
  499. openpic_cpu_write_internal(opp, addr, val, get_current_cpu());
  500. break;
  501. case 0x1000: /* FRR */
  502. break;
  503. case 0x1020: /* GCR */
  504. openpic_gcr_write(opp, val);
  505. break;
  506. case 0x1080: /* VIR */
  507. break;
  508. case 0x1090: /* PIR */
  509. for (idx = 0; idx < opp->nb_cpus; idx++) {
  510. if ((val & (1 << idx)) && !(opp->pir & (1 << idx))) {
  511. DPRINTF("Raise OpenPIC RESET output for CPU %d", idx);
  512. dst = &opp->dst[idx];
  513. qemu_irq_raise(dst->irqs[OPENPIC_OUTPUT_RESET]);
  514. } else if (!(val & (1 << idx)) && (opp->pir & (1 << idx))) {
  515. DPRINTF("Lower OpenPIC RESET output for CPU %d", idx);
  516. dst = &opp->dst[idx];
  517. qemu_irq_lower(dst->irqs[OPENPIC_OUTPUT_RESET]);
  518. }
  519. }
  520. opp->pir = val;
  521. break;
  522. case 0x10A0: /* IPI_IVPR */
  523. case 0x10B0:
  524. case 0x10C0:
  525. case 0x10D0:
  526. {
  527. int idx;
  528. idx = (addr - 0x10A0) >> 4;
  529. write_IRQreg_ivpr(opp, opp->irq_ipi0 + idx, val);
  530. }
  531. break;
  532. case 0x10E0: /* SPVE */
  533. opp->spve = val & opp->vector_mask;
  534. break;
  535. default:
  536. break;
  537. }
  538. }
  539. static uint64_t openpic_gbl_read(void *opaque, hwaddr addr, unsigned len)
  540. {
  541. OpenPICState *opp = opaque;
  542. uint32_t retval;
  543. DPRINTF("%s: addr %#" HWADDR_PRIx, __func__, addr);
  544. retval = 0xFFFFFFFF;
  545. if (addr & 0xF) {
  546. return retval;
  547. }
  548. switch (addr) {
  549. case 0x1000: /* FRR */
  550. retval = opp->frr;
  551. break;
  552. case 0x1020: /* GCR */
  553. retval = opp->gcr;
  554. break;
  555. case 0x1080: /* VIR */
  556. retval = opp->vir;
  557. break;
  558. case 0x1090: /* PIR */
  559. retval = 0x00000000;
  560. break;
  561. case 0x00: /* Block Revision Register1 (BRR1) */
  562. retval = opp->brr1;
  563. break;
  564. case 0x40:
  565. case 0x50:
  566. case 0x60:
  567. case 0x70:
  568. case 0x80:
  569. case 0x90:
  570. case 0xA0:
  571. case 0xB0:
  572. retval = openpic_cpu_read_internal(opp, addr, get_current_cpu());
  573. break;
  574. case 0x10A0: /* IPI_IVPR */
  575. case 0x10B0:
  576. case 0x10C0:
  577. case 0x10D0:
  578. {
  579. int idx;
  580. idx = (addr - 0x10A0) >> 4;
  581. retval = read_IRQreg_ivpr(opp, opp->irq_ipi0 + idx);
  582. }
  583. break;
  584. case 0x10E0: /* SPVE */
  585. retval = opp->spve;
  586. break;
  587. default:
  588. break;
  589. }
  590. DPRINTF("%s: => 0x%08x", __func__, retval);
  591. return retval;
  592. }
  593. static void openpic_tmr_set_tmr(OpenPICTimer *tmr, uint32_t val, bool enabled);
  594. static void qemu_timer_cb(void *opaque)
  595. {
  596. OpenPICTimer *tmr = opaque;
  597. OpenPICState *opp = tmr->opp;
  598. uint32_t n_IRQ = tmr->n_IRQ;
  599. uint32_t val = tmr->tbcr & ~TBCR_CI;
  600. uint32_t tog = ((tmr->tccr & TCCR_TOG) ^ TCCR_TOG); /* invert toggle. */
  601. DPRINTF("%s n_IRQ=%d", __func__, n_IRQ);
  602. /* Reload current count from base count and setup timer. */
  603. tmr->tccr = val | tog;
  604. openpic_tmr_set_tmr(tmr, val, /*enabled=*/true);
  605. /* Raise the interrupt. */
  606. opp->src[n_IRQ].destmask = read_IRQreg_idr(opp, n_IRQ);
  607. openpic_set_irq(opp, n_IRQ, 1);
  608. openpic_set_irq(opp, n_IRQ, 0);
  609. }
  610. /*
  611. * If enabled is true, arranges for an interrupt to be raised val clocks into
  612. * the future, if enabled is false cancels the timer.
  613. */
  614. static void openpic_tmr_set_tmr(OpenPICTimer *tmr, uint32_t val, bool enabled)
  615. {
  616. uint64_t ns = ticks_to_ns(val & ~TCCR_TOG);
  617. /*
  618. * A count of zero causes a timer to be set to expire immediately. This
  619. * effectively stops the simulation since the timer is constantly expiring
  620. * which prevents guest code execution, so we don't honor that
  621. * configuration. On real hardware, this situation would generate an
  622. * interrupt on every clock cycle if the interrupt was unmasked.
  623. */
  624. if ((ns == 0) || !enabled) {
  625. tmr->qemu_timer_active = false;
  626. tmr->tccr = tmr->tccr & TCCR_TOG;
  627. timer_del(tmr->qemu_timer); /* set timer to never expire. */
  628. } else {
  629. tmr->qemu_timer_active = true;
  630. uint64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  631. tmr->origin_time = now;
  632. timer_mod(tmr->qemu_timer, now + ns); /* set timer expiration. */
  633. }
  634. }
  635. /*
  636. * Returns the current tccr value, i.e., timer value (in clocks) with
  637. * appropriate TOG.
  638. */
  639. static uint64_t openpic_tmr_get_timer(OpenPICTimer *tmr)
  640. {
  641. uint64_t retval;
  642. if (!tmr->qemu_timer_active) {
  643. retval = tmr->tccr;
  644. } else {
  645. uint64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  646. uint64_t used = now - tmr->origin_time; /* nsecs */
  647. uint32_t used_ticks = (uint32_t)ns_to_ticks(used);
  648. uint32_t count = (tmr->tccr & ~TCCR_TOG) - used_ticks;
  649. retval = (uint32_t)((tmr->tccr & TCCR_TOG) | (count & ~TCCR_TOG));
  650. }
  651. return retval;
  652. }
  653. static void openpic_tmr_write(void *opaque, hwaddr addr, uint64_t val,
  654. unsigned len)
  655. {
  656. OpenPICState *opp = opaque;
  657. int idx;
  658. DPRINTF("%s: addr %#" HWADDR_PRIx " <= %08" PRIx64,
  659. __func__, (addr + 0x10f0), val);
  660. if (addr & 0xF) {
  661. return;
  662. }
  663. if (addr == 0) {
  664. /* TFRR */
  665. opp->tfrr = val;
  666. return;
  667. }
  668. addr -= 0x10; /* correct for TFRR */
  669. idx = (addr >> 6) & 0x3;
  670. switch (addr & 0x30) {
  671. case 0x00: /* TCCR */
  672. break;
  673. case 0x10: /* TBCR */
  674. /* Did the enable status change? */
  675. if ((opp->timers[idx].tbcr & TBCR_CI) != (val & TBCR_CI)) {
  676. /* Did "Count Inhibit" transition from 1 to 0? */
  677. if ((val & TBCR_CI) == 0) {
  678. opp->timers[idx].tccr = val & ~TCCR_TOG;
  679. }
  680. openpic_tmr_set_tmr(&opp->timers[idx],
  681. (val & ~TBCR_CI),
  682. /*enabled=*/((val & TBCR_CI) == 0));
  683. }
  684. opp->timers[idx].tbcr = val;
  685. break;
  686. case 0x20: /* TVPR */
  687. write_IRQreg_ivpr(opp, opp->irq_tim0 + idx, val);
  688. break;
  689. case 0x30: /* TDR */
  690. write_IRQreg_idr(opp, opp->irq_tim0 + idx, val);
  691. break;
  692. }
  693. }
  694. static uint64_t openpic_tmr_read(void *opaque, hwaddr addr, unsigned len)
  695. {
  696. OpenPICState *opp = opaque;
  697. uint32_t retval = -1;
  698. int idx;
  699. DPRINTF("%s: addr %#" HWADDR_PRIx, __func__, addr + 0x10f0);
  700. if (addr & 0xF) {
  701. goto out;
  702. }
  703. if (addr == 0) {
  704. /* TFRR */
  705. retval = opp->tfrr;
  706. goto out;
  707. }
  708. addr -= 0x10; /* correct for TFRR */
  709. idx = (addr >> 6) & 0x3;
  710. switch (addr & 0x30) {
  711. case 0x00: /* TCCR */
  712. retval = openpic_tmr_get_timer(&opp->timers[idx]);
  713. break;
  714. case 0x10: /* TBCR */
  715. retval = opp->timers[idx].tbcr;
  716. break;
  717. case 0x20: /* TVPR */
  718. retval = read_IRQreg_ivpr(opp, opp->irq_tim0 + idx);
  719. break;
  720. case 0x30: /* TDR */
  721. retval = read_IRQreg_idr(opp, opp->irq_tim0 + idx);
  722. break;
  723. }
  724. out:
  725. DPRINTF("%s: => 0x%08x", __func__, retval);
  726. return retval;
  727. }
  728. static void openpic_src_write(void *opaque, hwaddr addr, uint64_t val,
  729. unsigned len)
  730. {
  731. OpenPICState *opp = opaque;
  732. int idx;
  733. DPRINTF("%s: addr %#" HWADDR_PRIx " <= %08" PRIx64,
  734. __func__, addr, val);
  735. addr = addr & 0xffff;
  736. idx = addr >> 5;
  737. switch (addr & 0x1f) {
  738. case 0x00:
  739. write_IRQreg_ivpr(opp, idx, val);
  740. break;
  741. case 0x10:
  742. write_IRQreg_idr(opp, idx, val);
  743. break;
  744. case 0x18:
  745. write_IRQreg_ilr(opp, idx, val);
  746. break;
  747. }
  748. }
  749. static uint64_t openpic_src_read(void *opaque, uint64_t addr, unsigned len)
  750. {
  751. OpenPICState *opp = opaque;
  752. uint32_t retval;
  753. int idx;
  754. DPRINTF("%s: addr %#" HWADDR_PRIx, __func__, addr);
  755. retval = 0xFFFFFFFF;
  756. addr = addr & 0xffff;
  757. idx = addr >> 5;
  758. switch (addr & 0x1f) {
  759. case 0x00:
  760. retval = read_IRQreg_ivpr(opp, idx);
  761. break;
  762. case 0x10:
  763. retval = read_IRQreg_idr(opp, idx);
  764. break;
  765. case 0x18:
  766. retval = read_IRQreg_ilr(opp, idx);
  767. break;
  768. }
  769. DPRINTF("%s: => 0x%08x", __func__, retval);
  770. return retval;
  771. }
  772. static void openpic_msi_write(void *opaque, hwaddr addr, uint64_t val,
  773. unsigned size)
  774. {
  775. OpenPICState *opp = opaque;
  776. int idx = opp->irq_msi;
  777. int srs, ibs;
  778. DPRINTF("%s: addr %#" HWADDR_PRIx " <= 0x%08" PRIx64,
  779. __func__, addr, val);
  780. if (addr & 0xF) {
  781. return;
  782. }
  783. switch (addr) {
  784. case MSIIR_OFFSET:
  785. srs = val >> MSIIR_SRS_SHIFT;
  786. idx += srs;
  787. ibs = (val & MSIIR_IBS_MASK) >> MSIIR_IBS_SHIFT;
  788. opp->msi[srs].msir |= 1 << ibs;
  789. openpic_set_irq(opp, idx, 1);
  790. break;
  791. default:
  792. /* most registers are read-only, thus ignored */
  793. break;
  794. }
  795. }
  796. static uint64_t openpic_msi_read(void *opaque, hwaddr addr, unsigned size)
  797. {
  798. OpenPICState *opp = opaque;
  799. uint64_t r = 0;
  800. int i, srs;
  801. DPRINTF("%s: addr %#" HWADDR_PRIx, __func__, addr);
  802. if (addr & 0xF) {
  803. return -1;
  804. }
  805. srs = addr >> 4;
  806. switch (addr) {
  807. case 0x00:
  808. case 0x10:
  809. case 0x20:
  810. case 0x30:
  811. case 0x40:
  812. case 0x50:
  813. case 0x60:
  814. case 0x70: /* MSIRs */
  815. r = opp->msi[srs].msir;
  816. /* Clear on read */
  817. opp->msi[srs].msir = 0;
  818. openpic_set_irq(opp, opp->irq_msi + srs, 0);
  819. break;
  820. case 0x120: /* MSISR */
  821. for (i = 0; i < MAX_MSI; i++) {
  822. r |= (opp->msi[i].msir ? 1 : 0) << i;
  823. }
  824. break;
  825. }
  826. return r;
  827. }
  828. static uint64_t openpic_summary_read(void *opaque, hwaddr addr, unsigned size)
  829. {
  830. uint64_t r = 0;
  831. DPRINTF("%s: addr %#" HWADDR_PRIx, __func__, addr);
  832. /* TODO: EISR/EIMR */
  833. return r;
  834. }
  835. static void openpic_summary_write(void *opaque, hwaddr addr, uint64_t val,
  836. unsigned size)
  837. {
  838. DPRINTF("%s: addr %#" HWADDR_PRIx " <= 0x%08" PRIx64,
  839. __func__, addr, val);
  840. /* TODO: EISR/EIMR */
  841. }
  842. static void openpic_cpu_write_internal(void *opaque, hwaddr addr,
  843. uint32_t val, int idx)
  844. {
  845. OpenPICState *opp = opaque;
  846. IRQSource *src;
  847. IRQDest *dst;
  848. int s_IRQ, n_IRQ;
  849. DPRINTF("%s: cpu %d addr %#" HWADDR_PRIx " <= 0x%08x", __func__, idx,
  850. addr, val);
  851. if (idx < 0 || idx >= opp->nb_cpus) {
  852. return;
  853. }
  854. if (addr & 0xF) {
  855. return;
  856. }
  857. dst = &opp->dst[idx];
  858. addr &= 0xFF0;
  859. switch (addr) {
  860. case 0x40: /* IPIDR */
  861. case 0x50:
  862. case 0x60:
  863. case 0x70:
  864. idx = (addr - 0x40) >> 4;
  865. /* we use IDE as mask which CPUs to deliver the IPI to still. */
  866. opp->src[opp->irq_ipi0 + idx].destmask |= val;
  867. openpic_set_irq(opp, opp->irq_ipi0 + idx, 1);
  868. openpic_set_irq(opp, opp->irq_ipi0 + idx, 0);
  869. break;
  870. case 0x80: /* CTPR */
  871. dst->ctpr = val & 0x0000000F;
  872. DPRINTF("%s: set CPU %d ctpr to %d, raised %d servicing %d",
  873. __func__, idx, dst->ctpr, dst->raised.priority,
  874. dst->servicing.priority);
  875. if (dst->raised.priority <= dst->ctpr) {
  876. DPRINTF("%s: Lower OpenPIC INT output cpu %d due to ctpr",
  877. __func__, idx);
  878. qemu_irq_lower(dst->irqs[OPENPIC_OUTPUT_INT]);
  879. } else if (dst->raised.priority > dst->servicing.priority) {
  880. DPRINTF("%s: Raise OpenPIC INT output cpu %d irq %d",
  881. __func__, idx, dst->raised.next);
  882. qemu_irq_raise(dst->irqs[OPENPIC_OUTPUT_INT]);
  883. }
  884. break;
  885. case 0x90: /* WHOAMI */
  886. /* Read-only register */
  887. break;
  888. case 0xA0: /* IACK */
  889. /* Read-only register */
  890. break;
  891. case 0xB0: /* EOI */
  892. DPRINTF("EOI");
  893. s_IRQ = IRQ_get_next(opp, &dst->servicing);
  894. if (s_IRQ < 0) {
  895. DPRINTF("%s: EOI with no interrupt in service", __func__);
  896. break;
  897. }
  898. IRQ_resetbit(&dst->servicing, s_IRQ);
  899. /* Set up next servicing IRQ */
  900. s_IRQ = IRQ_get_next(opp, &dst->servicing);
  901. /* Check queued interrupts. */
  902. n_IRQ = IRQ_get_next(opp, &dst->raised);
  903. src = &opp->src[n_IRQ];
  904. if (n_IRQ != -1 &&
  905. (s_IRQ == -1 ||
  906. IVPR_PRIORITY(src->ivpr) > dst->servicing.priority)) {
  907. DPRINTF("Raise OpenPIC INT output cpu %d irq %d",
  908. idx, n_IRQ);
  909. qemu_irq_raise(opp->dst[idx].irqs[OPENPIC_OUTPUT_INT]);
  910. }
  911. break;
  912. default:
  913. break;
  914. }
  915. }
  916. static void openpic_cpu_write(void *opaque, hwaddr addr, uint64_t val,
  917. unsigned len)
  918. {
  919. openpic_cpu_write_internal(opaque, addr, val, (addr & 0x1f000) >> 12);
  920. }
  921. static uint32_t openpic_iack(OpenPICState *opp, IRQDest *dst, int cpu)
  922. {
  923. IRQSource *src;
  924. int retval, irq;
  925. DPRINTF("Lower OpenPIC INT output");
  926. qemu_irq_lower(dst->irqs[OPENPIC_OUTPUT_INT]);
  927. irq = IRQ_get_next(opp, &dst->raised);
  928. DPRINTF("IACK: irq=%d", irq);
  929. if (irq == -1) {
  930. /* No more interrupt pending */
  931. return opp->spve;
  932. }
  933. src = &opp->src[irq];
  934. if (!(src->ivpr & IVPR_ACTIVITY_MASK) ||
  935. !(IVPR_PRIORITY(src->ivpr) > dst->ctpr)) {
  936. error_report("%s: bad raised IRQ %d ctpr %d ivpr 0x%08x",
  937. __func__, irq, dst->ctpr, src->ivpr);
  938. openpic_update_irq(opp, irq);
  939. retval = opp->spve;
  940. } else {
  941. /* IRQ enter servicing state */
  942. IRQ_setbit(&dst->servicing, irq);
  943. retval = IVPR_VECTOR(opp, src->ivpr);
  944. }
  945. if (!src->level) {
  946. /* edge-sensitive IRQ */
  947. src->ivpr &= ~IVPR_ACTIVITY_MASK;
  948. src->pending = 0;
  949. IRQ_resetbit(&dst->raised, irq);
  950. }
  951. /* Timers and IPIs support multicast. */
  952. if (((irq >= opp->irq_ipi0) && (irq < (opp->irq_ipi0 + OPENPIC_MAX_IPI))) ||
  953. ((irq >= opp->irq_tim0) && (irq < (opp->irq_tim0 + OPENPIC_MAX_TMR)))) {
  954. DPRINTF("irq is IPI or TMR");
  955. src->destmask &= ~(1 << cpu);
  956. if (src->destmask && !src->level) {
  957. /* trigger on CPUs that didn't know about it yet */
  958. openpic_set_irq(opp, irq, 1);
  959. openpic_set_irq(opp, irq, 0);
  960. /* if all CPUs knew about it, set active bit again */
  961. src->ivpr |= IVPR_ACTIVITY_MASK;
  962. }
  963. }
  964. return retval;
  965. }
  966. static uint32_t openpic_cpu_read_internal(void *opaque, hwaddr addr,
  967. int idx)
  968. {
  969. OpenPICState *opp = opaque;
  970. IRQDest *dst;
  971. uint32_t retval;
  972. DPRINTF("%s: cpu %d addr %#" HWADDR_PRIx, __func__, idx, addr);
  973. retval = 0xFFFFFFFF;
  974. if (idx < 0 || idx >= opp->nb_cpus) {
  975. return retval;
  976. }
  977. if (addr & 0xF) {
  978. return retval;
  979. }
  980. dst = &opp->dst[idx];
  981. addr &= 0xFF0;
  982. switch (addr) {
  983. case 0x80: /* CTPR */
  984. retval = dst->ctpr;
  985. break;
  986. case 0x90: /* WHOAMI */
  987. retval = idx;
  988. break;
  989. case 0xA0: /* IACK */
  990. retval = openpic_iack(opp, dst, idx);
  991. break;
  992. case 0xB0: /* EOI */
  993. retval = 0;
  994. break;
  995. default:
  996. break;
  997. }
  998. DPRINTF("%s: => 0x%08x", __func__, retval);
  999. return retval;
  1000. }
  1001. static uint64_t openpic_cpu_read(void *opaque, hwaddr addr, unsigned len)
  1002. {
  1003. return openpic_cpu_read_internal(opaque, addr, (addr & 0x1f000) >> 12);
  1004. }
  1005. static const MemoryRegionOps openpic_glb_ops_le = {
  1006. .write = openpic_gbl_write,
  1007. .read = openpic_gbl_read,
  1008. .endianness = DEVICE_LITTLE_ENDIAN,
  1009. .impl = {
  1010. .min_access_size = 4,
  1011. .max_access_size = 4,
  1012. },
  1013. };
  1014. static const MemoryRegionOps openpic_glb_ops_be = {
  1015. .write = openpic_gbl_write,
  1016. .read = openpic_gbl_read,
  1017. .endianness = DEVICE_BIG_ENDIAN,
  1018. .impl = {
  1019. .min_access_size = 4,
  1020. .max_access_size = 4,
  1021. },
  1022. };
  1023. static const MemoryRegionOps openpic_tmr_ops_le = {
  1024. .write = openpic_tmr_write,
  1025. .read = openpic_tmr_read,
  1026. .endianness = DEVICE_LITTLE_ENDIAN,
  1027. .impl = {
  1028. .min_access_size = 4,
  1029. .max_access_size = 4,
  1030. },
  1031. };
  1032. static const MemoryRegionOps openpic_tmr_ops_be = {
  1033. .write = openpic_tmr_write,
  1034. .read = openpic_tmr_read,
  1035. .endianness = DEVICE_BIG_ENDIAN,
  1036. .impl = {
  1037. .min_access_size = 4,
  1038. .max_access_size = 4,
  1039. },
  1040. };
  1041. static const MemoryRegionOps openpic_cpu_ops_le = {
  1042. .write = openpic_cpu_write,
  1043. .read = openpic_cpu_read,
  1044. .endianness = DEVICE_LITTLE_ENDIAN,
  1045. .impl = {
  1046. .min_access_size = 4,
  1047. .max_access_size = 4,
  1048. },
  1049. };
  1050. static const MemoryRegionOps openpic_cpu_ops_be = {
  1051. .write = openpic_cpu_write,
  1052. .read = openpic_cpu_read,
  1053. .endianness = DEVICE_BIG_ENDIAN,
  1054. .impl = {
  1055. .min_access_size = 4,
  1056. .max_access_size = 4,
  1057. },
  1058. };
  1059. static const MemoryRegionOps openpic_src_ops_le = {
  1060. .write = openpic_src_write,
  1061. .read = openpic_src_read,
  1062. .endianness = DEVICE_LITTLE_ENDIAN,
  1063. .impl = {
  1064. .min_access_size = 4,
  1065. .max_access_size = 4,
  1066. },
  1067. };
  1068. static const MemoryRegionOps openpic_src_ops_be = {
  1069. .write = openpic_src_write,
  1070. .read = openpic_src_read,
  1071. .endianness = DEVICE_BIG_ENDIAN,
  1072. .impl = {
  1073. .min_access_size = 4,
  1074. .max_access_size = 4,
  1075. },
  1076. };
  1077. static const MemoryRegionOps openpic_msi_ops_be = {
  1078. .read = openpic_msi_read,
  1079. .write = openpic_msi_write,
  1080. .endianness = DEVICE_BIG_ENDIAN,
  1081. .impl = {
  1082. .min_access_size = 4,
  1083. .max_access_size = 4,
  1084. },
  1085. };
  1086. static const MemoryRegionOps openpic_summary_ops_be = {
  1087. .read = openpic_summary_read,
  1088. .write = openpic_summary_write,
  1089. .endianness = DEVICE_BIG_ENDIAN,
  1090. .impl = {
  1091. .min_access_size = 4,
  1092. .max_access_size = 4,
  1093. },
  1094. };
  1095. static void openpic_reset(DeviceState *d)
  1096. {
  1097. OpenPICState *opp = OPENPIC(d);
  1098. int i;
  1099. opp->gcr = GCR_RESET;
  1100. /* Initialise controller registers */
  1101. opp->frr = ((opp->nb_irqs - 1) << FRR_NIRQ_SHIFT) |
  1102. ((opp->nb_cpus - 1) << FRR_NCPU_SHIFT) |
  1103. (opp->vid << FRR_VID_SHIFT);
  1104. opp->pir = 0;
  1105. opp->spve = -1 & opp->vector_mask;
  1106. opp->tfrr = opp->tfrr_reset;
  1107. /* Initialise IRQ sources */
  1108. for (i = 0; i < opp->max_irq; i++) {
  1109. opp->src[i].ivpr = opp->ivpr_reset;
  1110. switch (opp->src[i].type) {
  1111. case IRQ_TYPE_NORMAL:
  1112. opp->src[i].level = !!(opp->ivpr_reset & IVPR_SENSE_MASK);
  1113. break;
  1114. case IRQ_TYPE_FSLINT:
  1115. opp->src[i].ivpr |= IVPR_POLARITY_MASK;
  1116. break;
  1117. case IRQ_TYPE_FSLSPECIAL:
  1118. break;
  1119. }
  1120. /* Mask all IPI interrupts for Freescale OpenPIC */
  1121. if ((opp->model == OPENPIC_MODEL_FSL_MPIC_20) ||
  1122. (opp->model == OPENPIC_MODEL_FSL_MPIC_42)) {
  1123. if (i >= opp->irq_ipi0 && i < opp->irq_tim0) {
  1124. write_IRQreg_idr(opp, i, 0);
  1125. continue;
  1126. }
  1127. }
  1128. write_IRQreg_idr(opp, i, opp->idr_reset);
  1129. }
  1130. /* Initialise IRQ destinations */
  1131. for (i = 0; i < opp->nb_cpus; i++) {
  1132. opp->dst[i].ctpr = 15;
  1133. opp->dst[i].raised.next = -1;
  1134. opp->dst[i].raised.priority = 0;
  1135. bitmap_clear(opp->dst[i].raised.queue, 0, IRQQUEUE_SIZE_BITS);
  1136. opp->dst[i].servicing.next = -1;
  1137. opp->dst[i].servicing.priority = 0;
  1138. bitmap_clear(opp->dst[i].servicing.queue, 0, IRQQUEUE_SIZE_BITS);
  1139. }
  1140. /* Initialise timers */
  1141. for (i = 0; i < OPENPIC_MAX_TMR; i++) {
  1142. opp->timers[i].tccr = 0;
  1143. opp->timers[i].tbcr = TBCR_CI;
  1144. if (opp->timers[i].qemu_timer_active) {
  1145. timer_del(opp->timers[i].qemu_timer); /* Inhibit timer */
  1146. opp->timers[i].qemu_timer_active = false;
  1147. }
  1148. }
  1149. /* Go out of RESET state */
  1150. opp->gcr = 0;
  1151. }
  1152. typedef struct MemReg {
  1153. const char *name;
  1154. MemoryRegionOps const *ops;
  1155. hwaddr start_addr;
  1156. ram_addr_t size;
  1157. } MemReg;
  1158. static void fsl_common_init(OpenPICState *opp)
  1159. {
  1160. int i;
  1161. int virq = OPENPIC_MAX_SRC;
  1162. opp->vid = VID_REVISION_1_2;
  1163. opp->vir = VIR_GENERIC;
  1164. opp->vector_mask = 0xFFFF;
  1165. opp->tfrr_reset = 0;
  1166. opp->ivpr_reset = IVPR_MASK_MASK;
  1167. opp->idr_reset = 1 << 0;
  1168. opp->max_irq = OPENPIC_MAX_IRQ;
  1169. opp->irq_ipi0 = virq;
  1170. virq += OPENPIC_MAX_IPI;
  1171. opp->irq_tim0 = virq;
  1172. virq += OPENPIC_MAX_TMR;
  1173. assert(virq <= OPENPIC_MAX_IRQ);
  1174. opp->irq_msi = 224;
  1175. msi_nonbroken = true;
  1176. for (i = 0; i < opp->fsl->max_ext; i++) {
  1177. opp->src[i].level = false;
  1178. }
  1179. /* Internal interrupts, including message and MSI */
  1180. for (i = 16; i < OPENPIC_MAX_SRC; i++) {
  1181. opp->src[i].type = IRQ_TYPE_FSLINT;
  1182. opp->src[i].level = true;
  1183. }
  1184. /* timers and IPIs */
  1185. for (i = OPENPIC_MAX_SRC; i < virq; i++) {
  1186. opp->src[i].type = IRQ_TYPE_FSLSPECIAL;
  1187. opp->src[i].level = false;
  1188. }
  1189. for (i = 0; i < OPENPIC_MAX_TMR; i++) {
  1190. opp->timers[i].n_IRQ = opp->irq_tim0 + i;
  1191. opp->timers[i].qemu_timer_active = false;
  1192. opp->timers[i].qemu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
  1193. &qemu_timer_cb,
  1194. &opp->timers[i]);
  1195. opp->timers[i].opp = opp;
  1196. }
  1197. }
  1198. static void map_list(OpenPICState *opp, const MemReg *list, int *count)
  1199. {
  1200. while (list->name) {
  1201. assert(*count < ARRAY_SIZE(opp->sub_io_mem));
  1202. memory_region_init_io(&opp->sub_io_mem[*count], OBJECT(opp), list->ops,
  1203. opp, list->name, list->size);
  1204. memory_region_add_subregion(&opp->mem, list->start_addr,
  1205. &opp->sub_io_mem[*count]);
  1206. (*count)++;
  1207. list++;
  1208. }
  1209. }
  1210. static const VMStateDescription vmstate_openpic_irq_queue = {
  1211. .name = "openpic_irq_queue",
  1212. .version_id = 0,
  1213. .minimum_version_id = 0,
  1214. .fields = (VMStateField[]) {
  1215. VMSTATE_BITMAP(queue, IRQQueue, 0, queue_size),
  1216. VMSTATE_INT32(next, IRQQueue),
  1217. VMSTATE_INT32(priority, IRQQueue),
  1218. VMSTATE_END_OF_LIST()
  1219. }
  1220. };
  1221. static const VMStateDescription vmstate_openpic_irqdest = {
  1222. .name = "openpic_irqdest",
  1223. .version_id = 0,
  1224. .minimum_version_id = 0,
  1225. .fields = (VMStateField[]) {
  1226. VMSTATE_INT32(ctpr, IRQDest),
  1227. VMSTATE_STRUCT(raised, IRQDest, 0, vmstate_openpic_irq_queue,
  1228. IRQQueue),
  1229. VMSTATE_STRUCT(servicing, IRQDest, 0, vmstate_openpic_irq_queue,
  1230. IRQQueue),
  1231. VMSTATE_UINT32_ARRAY(outputs_active, IRQDest, OPENPIC_OUTPUT_NB),
  1232. VMSTATE_END_OF_LIST()
  1233. }
  1234. };
  1235. static const VMStateDescription vmstate_openpic_irqsource = {
  1236. .name = "openpic_irqsource",
  1237. .version_id = 0,
  1238. .minimum_version_id = 0,
  1239. .fields = (VMStateField[]) {
  1240. VMSTATE_UINT32(ivpr, IRQSource),
  1241. VMSTATE_UINT32(idr, IRQSource),
  1242. VMSTATE_UINT32(destmask, IRQSource),
  1243. VMSTATE_INT32(last_cpu, IRQSource),
  1244. VMSTATE_INT32(pending, IRQSource),
  1245. VMSTATE_END_OF_LIST()
  1246. }
  1247. };
  1248. static const VMStateDescription vmstate_openpic_timer = {
  1249. .name = "openpic_timer",
  1250. .version_id = 0,
  1251. .minimum_version_id = 0,
  1252. .fields = (VMStateField[]) {
  1253. VMSTATE_UINT32(tccr, OpenPICTimer),
  1254. VMSTATE_UINT32(tbcr, OpenPICTimer),
  1255. VMSTATE_END_OF_LIST()
  1256. }
  1257. };
  1258. static const VMStateDescription vmstate_openpic_msi = {
  1259. .name = "openpic_msi",
  1260. .version_id = 0,
  1261. .minimum_version_id = 0,
  1262. .fields = (VMStateField[]) {
  1263. VMSTATE_UINT32(msir, OpenPICMSI),
  1264. VMSTATE_END_OF_LIST()
  1265. }
  1266. };
  1267. static int openpic_post_load(void *opaque, int version_id)
  1268. {
  1269. OpenPICState *opp = (OpenPICState *)opaque;
  1270. int i;
  1271. /* Update internal ivpr and idr variables */
  1272. for (i = 0; i < opp->max_irq; i++) {
  1273. write_IRQreg_idr(opp, i, opp->src[i].idr);
  1274. write_IRQreg_ivpr(opp, i, opp->src[i].ivpr);
  1275. }
  1276. return 0;
  1277. }
  1278. static const VMStateDescription vmstate_openpic = {
  1279. .name = "openpic",
  1280. .version_id = 3,
  1281. .minimum_version_id = 3,
  1282. .post_load = openpic_post_load,
  1283. .fields = (VMStateField[]) {
  1284. VMSTATE_UINT32(gcr, OpenPICState),
  1285. VMSTATE_UINT32(vir, OpenPICState),
  1286. VMSTATE_UINT32(pir, OpenPICState),
  1287. VMSTATE_UINT32(spve, OpenPICState),
  1288. VMSTATE_UINT32(tfrr, OpenPICState),
  1289. VMSTATE_UINT32(max_irq, OpenPICState),
  1290. VMSTATE_STRUCT_VARRAY_UINT32(src, OpenPICState, max_irq, 0,
  1291. vmstate_openpic_irqsource, IRQSource),
  1292. VMSTATE_UINT32_EQUAL(nb_cpus, OpenPICState, NULL),
  1293. VMSTATE_STRUCT_VARRAY_UINT32(dst, OpenPICState, nb_cpus, 0,
  1294. vmstate_openpic_irqdest, IRQDest),
  1295. VMSTATE_STRUCT_ARRAY(timers, OpenPICState, OPENPIC_MAX_TMR, 0,
  1296. vmstate_openpic_timer, OpenPICTimer),
  1297. VMSTATE_STRUCT_ARRAY(msi, OpenPICState, MAX_MSI, 0,
  1298. vmstate_openpic_msi, OpenPICMSI),
  1299. VMSTATE_UINT32(irq_ipi0, OpenPICState),
  1300. VMSTATE_UINT32(irq_tim0, OpenPICState),
  1301. VMSTATE_UINT32(irq_msi, OpenPICState),
  1302. VMSTATE_END_OF_LIST()
  1303. }
  1304. };
  1305. static void openpic_init(Object *obj)
  1306. {
  1307. OpenPICState *opp = OPENPIC(obj);
  1308. memory_region_init(&opp->mem, obj, "openpic", 0x40000);
  1309. }
  1310. static void openpic_realize(DeviceState *dev, Error **errp)
  1311. {
  1312. SysBusDevice *d = SYS_BUS_DEVICE(dev);
  1313. OpenPICState *opp = OPENPIC(dev);
  1314. int i, j;
  1315. int list_count = 0;
  1316. static const MemReg list_le[] = {
  1317. {"glb", &openpic_glb_ops_le,
  1318. OPENPIC_GLB_REG_START, OPENPIC_GLB_REG_SIZE},
  1319. {"tmr", &openpic_tmr_ops_le,
  1320. OPENPIC_TMR_REG_START, OPENPIC_TMR_REG_SIZE},
  1321. {"src", &openpic_src_ops_le,
  1322. OPENPIC_SRC_REG_START, OPENPIC_SRC_REG_SIZE},
  1323. {"cpu", &openpic_cpu_ops_le,
  1324. OPENPIC_CPU_REG_START, OPENPIC_CPU_REG_SIZE},
  1325. {NULL}
  1326. };
  1327. static const MemReg list_be[] = {
  1328. {"glb", &openpic_glb_ops_be,
  1329. OPENPIC_GLB_REG_START, OPENPIC_GLB_REG_SIZE},
  1330. {"tmr", &openpic_tmr_ops_be,
  1331. OPENPIC_TMR_REG_START, OPENPIC_TMR_REG_SIZE},
  1332. {"src", &openpic_src_ops_be,
  1333. OPENPIC_SRC_REG_START, OPENPIC_SRC_REG_SIZE},
  1334. {"cpu", &openpic_cpu_ops_be,
  1335. OPENPIC_CPU_REG_START, OPENPIC_CPU_REG_SIZE},
  1336. {NULL}
  1337. };
  1338. static const MemReg list_fsl[] = {
  1339. {"msi", &openpic_msi_ops_be,
  1340. OPENPIC_MSI_REG_START, OPENPIC_MSI_REG_SIZE},
  1341. {"summary", &openpic_summary_ops_be,
  1342. OPENPIC_SUMMARY_REG_START, OPENPIC_SUMMARY_REG_SIZE},
  1343. {NULL}
  1344. };
  1345. if (opp->nb_cpus > MAX_CPU) {
  1346. error_setg(errp, QERR_PROPERTY_VALUE_OUT_OF_RANGE,
  1347. TYPE_OPENPIC, "nb_cpus", (uint64_t)opp->nb_cpus,
  1348. (uint64_t)0, (uint64_t)MAX_CPU);
  1349. return;
  1350. }
  1351. switch (opp->model) {
  1352. case OPENPIC_MODEL_FSL_MPIC_20:
  1353. default:
  1354. opp->fsl = &fsl_mpic_20;
  1355. opp->brr1 = 0x00400200;
  1356. opp->flags |= OPENPIC_FLAG_IDR_CRIT;
  1357. opp->nb_irqs = 80;
  1358. opp->mpic_mode_mask = GCR_MODE_MIXED;
  1359. fsl_common_init(opp);
  1360. map_list(opp, list_be, &list_count);
  1361. map_list(opp, list_fsl, &list_count);
  1362. break;
  1363. case OPENPIC_MODEL_FSL_MPIC_42:
  1364. opp->fsl = &fsl_mpic_42;
  1365. opp->brr1 = 0x00400402;
  1366. opp->flags |= OPENPIC_FLAG_ILR;
  1367. opp->nb_irqs = 196;
  1368. opp->mpic_mode_mask = GCR_MODE_PROXY;
  1369. fsl_common_init(opp);
  1370. map_list(opp, list_be, &list_count);
  1371. map_list(opp, list_fsl, &list_count);
  1372. break;
  1373. case OPENPIC_MODEL_KEYLARGO:
  1374. opp->nb_irqs = KEYLARGO_MAX_EXT;
  1375. opp->vid = VID_REVISION_1_2;
  1376. opp->vir = VIR_GENERIC;
  1377. opp->vector_mask = 0xFF;
  1378. opp->tfrr_reset = 4160000;
  1379. opp->ivpr_reset = IVPR_MASK_MASK | IVPR_MODE_MASK;
  1380. opp->idr_reset = 0;
  1381. opp->max_irq = KEYLARGO_MAX_IRQ;
  1382. opp->irq_ipi0 = KEYLARGO_IPI_IRQ;
  1383. opp->irq_tim0 = KEYLARGO_TMR_IRQ;
  1384. opp->brr1 = -1;
  1385. opp->mpic_mode_mask = GCR_MODE_MIXED;
  1386. if (opp->nb_cpus != 1) {
  1387. error_setg(errp, "Only UP supported today");
  1388. return;
  1389. }
  1390. map_list(opp, list_le, &list_count);
  1391. break;
  1392. }
  1393. for (i = 0; i < opp->nb_cpus; i++) {
  1394. opp->dst[i].irqs = g_new0(qemu_irq, OPENPIC_OUTPUT_NB);
  1395. for (j = 0; j < OPENPIC_OUTPUT_NB; j++) {
  1396. sysbus_init_irq(d, &opp->dst[i].irqs[j]);
  1397. }
  1398. opp->dst[i].raised.queue_size = IRQQUEUE_SIZE_BITS;
  1399. opp->dst[i].raised.queue = bitmap_new(IRQQUEUE_SIZE_BITS);
  1400. opp->dst[i].servicing.queue_size = IRQQUEUE_SIZE_BITS;
  1401. opp->dst[i].servicing.queue = bitmap_new(IRQQUEUE_SIZE_BITS);
  1402. }
  1403. sysbus_init_mmio(d, &opp->mem);
  1404. qdev_init_gpio_in(dev, openpic_set_irq, opp->max_irq);
  1405. }
  1406. static Property openpic_properties[] = {
  1407. DEFINE_PROP_UINT32("model", OpenPICState, model, OPENPIC_MODEL_FSL_MPIC_20),
  1408. DEFINE_PROP_UINT32("nb_cpus", OpenPICState, nb_cpus, 1),
  1409. DEFINE_PROP_END_OF_LIST(),
  1410. };
  1411. static void openpic_class_init(ObjectClass *oc, void *data)
  1412. {
  1413. DeviceClass *dc = DEVICE_CLASS(oc);
  1414. dc->realize = openpic_realize;
  1415. device_class_set_props(dc, openpic_properties);
  1416. dc->reset = openpic_reset;
  1417. dc->vmsd = &vmstate_openpic;
  1418. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  1419. }
  1420. static const TypeInfo openpic_info = {
  1421. .name = TYPE_OPENPIC,
  1422. .parent = TYPE_SYS_BUS_DEVICE,
  1423. .instance_size = sizeof(OpenPICState),
  1424. .instance_init = openpic_init,
  1425. .class_init = openpic_class_init,
  1426. };
  1427. static void openpic_register_types(void)
  1428. {
  1429. type_register_static(&openpic_info);
  1430. }
  1431. type_init(openpic_register_types)