openpic.c 46 KB

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