mpic.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  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. #define MAX_CPU 32
  26. #define MAX_SRC 256
  27. #define MAX_TMR 4
  28. #define MAX_IPI 4
  29. #define MAX_MSI 8
  30. #define MAX_IRQ (MAX_SRC + MAX_IPI + MAX_TMR)
  31. #define VID 0x03 /* MPIC version ID */
  32. /* OpenPIC capability flags */
  33. #define OPENPIC_FLAG_IDR_CRIT (1 << 0)
  34. #define OPENPIC_FLAG_ILR (2 << 0)
  35. /* OpenPIC address map */
  36. #define OPENPIC_GLB_REG_START 0x0
  37. #define OPENPIC_GLB_REG_SIZE 0x10F0
  38. #define OPENPIC_TMR_REG_START 0x10F0
  39. #define OPENPIC_TMR_REG_SIZE 0x220
  40. #define OPENPIC_MSI_REG_START 0x1600
  41. #define OPENPIC_MSI_REG_SIZE 0x200
  42. #define OPENPIC_SUMMARY_REG_START 0x3800
  43. #define OPENPIC_SUMMARY_REG_SIZE 0x800
  44. #define OPENPIC_SRC_REG_START 0x10000
  45. #define OPENPIC_SRC_REG_SIZE (MAX_SRC * 0x20)
  46. #define OPENPIC_CPU_REG_START 0x20000
  47. #define OPENPIC_CPU_REG_SIZE 0x100 + ((MAX_CPU - 1) * 0x1000)
  48. typedef struct FslMpicInfo {
  49. int max_ext;
  50. } FslMpicInfo;
  51. static FslMpicInfo fsl_mpic_20 = {
  52. .max_ext = 12,
  53. };
  54. static FslMpicInfo fsl_mpic_42 = {
  55. .max_ext = 12,
  56. };
  57. #define FRR_NIRQ_SHIFT 16
  58. #define FRR_NCPU_SHIFT 8
  59. #define FRR_VID_SHIFT 0
  60. #define VID_REVISION_1_2 2
  61. #define VID_REVISION_1_3 3
  62. #define VIR_GENERIC 0x00000000 /* Generic Vendor ID */
  63. #define GCR_RESET 0x80000000
  64. #define GCR_MODE_PASS 0x00000000
  65. #define GCR_MODE_MIXED 0x20000000
  66. #define GCR_MODE_PROXY 0x60000000
  67. #define TBCR_CI 0x80000000 /* count inhibit */
  68. #define TCCR_TOG 0x80000000 /* toggles when decrement to zero */
  69. #define IDR_EP_SHIFT 31
  70. #define IDR_EP_MASK (1 << IDR_EP_SHIFT)
  71. #define IDR_CI0_SHIFT 30
  72. #define IDR_CI1_SHIFT 29
  73. #define IDR_P1_SHIFT 1
  74. #define IDR_P0_SHIFT 0
  75. #define ILR_INTTGT_MASK 0x000000ff
  76. #define ILR_INTTGT_INT 0x00
  77. #define ILR_INTTGT_CINT 0x01 /* critical */
  78. #define ILR_INTTGT_MCP 0x02 /* machine check */
  79. #define MSIIR_OFFSET 0x140
  80. #define MSIIR_SRS_SHIFT 29
  81. #define MSIIR_SRS_MASK (0x7 << MSIIR_SRS_SHIFT)
  82. #define MSIIR_IBS_SHIFT 24
  83. #define MSIIR_IBS_MASK (0x1f << MSIIR_IBS_SHIFT)
  84. static int get_current_cpu(void)
  85. {
  86. CPUState *cpu_single_cpu;
  87. if (!cpu_single_env) {
  88. return -1;
  89. }
  90. cpu_single_cpu = ENV_GET_CPU(cpu_single_env);
  91. return cpu_single_cpu->cpu_index;
  92. }
  93. static uint32_t openpic_cpu_read_internal(void *opaque, hwaddr addr, int idx);
  94. static void openpic_cpu_write_internal(void *opaque, hwaddr addr,
  95. uint32_t val, int idx);
  96. typedef enum IRQType {
  97. IRQ_TYPE_NORMAL = 0,
  98. IRQ_TYPE_FSLINT, /* FSL internal interrupt -- level only */
  99. IRQ_TYPE_FSLSPECIAL, /* FSL timer/IPI interrupt, edge, no polarity */
  100. } IRQType;
  101. typedef struct IRQQueue {
  102. /* Round up to the nearest 64 IRQs so that the queue length
  103. * won't change when moving between 32 and 64 bit hosts.
  104. */
  105. unsigned long queue[BITS_TO_LONGS((MAX_IRQ + 63) & ~63)];
  106. int next;
  107. int priority;
  108. } IRQQueue;
  109. typedef struct IRQSource {
  110. uint32_t ivpr; /* IRQ vector/priority register */
  111. uint32_t idr; /* IRQ destination register */
  112. uint32_t destmask; /* bitmap of CPU destinations */
  113. int last_cpu;
  114. int output; /* IRQ level, e.g. OPENPIC_OUTPUT_INT */
  115. int pending; /* TRUE if IRQ is pending */
  116. IRQType type;
  117. bool level:1; /* level-triggered */
  118. bool nomask:1; /* critical interrupts ignore mask on some FSL MPICs */
  119. } IRQSource;
  120. #define IVPR_MASK_SHIFT 31
  121. #define IVPR_MASK_MASK (1 << IVPR_MASK_SHIFT)
  122. #define IVPR_ACTIVITY_SHIFT 30
  123. #define IVPR_ACTIVITY_MASK (1 << IVPR_ACTIVITY_SHIFT)
  124. #define IVPR_MODE_SHIFT 29
  125. #define IVPR_MODE_MASK (1 << IVPR_MODE_SHIFT)
  126. #define IVPR_POLARITY_SHIFT 23
  127. #define IVPR_POLARITY_MASK (1 << IVPR_POLARITY_SHIFT)
  128. #define IVPR_SENSE_SHIFT 22
  129. #define IVPR_SENSE_MASK (1 << IVPR_SENSE_SHIFT)
  130. #define IVPR_PRIORITY_MASK (0xF << 16)
  131. #define IVPR_PRIORITY(_ivprr_) ((int)(((_ivprr_) & IVPR_PRIORITY_MASK) >> 16))
  132. #define IVPR_VECTOR(opp, _ivprr_) ((_ivprr_) & (opp)->vector_mask)
  133. /* IDR[EP/CI] are only for FSL MPIC prior to v4.0 */
  134. #define IDR_EP 0x80000000 /* external pin */
  135. #define IDR_CI 0x40000000 /* critical interrupt */
  136. typedef struct IRQDest {
  137. int32_t ctpr; /* CPU current task priority */
  138. IRQQueue raised;
  139. IRQQueue servicing;
  140. qemu_irq *irqs;
  141. /* Count of IRQ sources asserting on non-INT outputs */
  142. uint32_t outputs_active[OPENPIC_OUTPUT_NB];
  143. } IRQDest;
  144. typedef struct OpenPICState {
  145. SysBusDevice busdev;
  146. MemoryRegion mem;
  147. /* Behavior control */
  148. FslMpicInfo *fsl;
  149. uint32_t model;
  150. uint32_t flags;
  151. uint32_t nb_irqs;
  152. uint32_t vid;
  153. uint32_t vir; /* Vendor identification register */
  154. uint32_t vector_mask;
  155. uint32_t tfrr_reset;
  156. uint32_t ivpr_reset;
  157. uint32_t idr_reset;
  158. uint32_t brr1;
  159. uint32_t mpic_mode_mask;
  160. /* Sub-regions */
  161. MemoryRegion sub_io_mem[6];
  162. /* Global registers */
  163. uint32_t frr; /* Feature reporting register */
  164. uint32_t gcr; /* Global configuration register */
  165. uint32_t pir; /* Processor initialization register */
  166. uint32_t spve; /* Spurious vector register */
  167. uint32_t tfrr; /* Timer frequency reporting register */
  168. /* Source registers */
  169. IRQSource src[MAX_IRQ];
  170. /* Local registers per output pin */
  171. IRQDest dst[MAX_CPU];
  172. uint32_t nb_cpus;
  173. /* Timer registers */
  174. struct {
  175. uint32_t tccr; /* Global timer current count register */
  176. uint32_t tbcr; /* Global timer base count register */
  177. } timers[MAX_TMR];
  178. /* Shared MSI registers */
  179. struct {
  180. uint32_t msir; /* Shared Message Signaled Interrupt Register */
  181. } msi[MAX_MSI];
  182. uint32_t max_irq;
  183. uint32_t irq_ipi0;
  184. uint32_t irq_tim0;
  185. uint32_t irq_msi;
  186. } OpenPICState;
  187. static inline void IRQ_setbit(IRQQueue * q, int n_IRQ)
  188. {
  189. set_bit(n_IRQ, q->queue);
  190. }
  191. static inline void IRQ_resetbit(IRQQueue * q, int n_IRQ)
  192. {
  193. clear_bit(n_IRQ, q->queue);
  194. }
  195. static inline int IRQ_testbit(IRQQueue * q, int n_IRQ)
  196. {
  197. return test_bit(n_IRQ, q->queue);
  198. }
  199. static void IRQ_check(OpenPICState * opp, IRQQueue * q)
  200. {
  201. int irq = -1;
  202. int next = -1;
  203. int priority = -1;
  204. for (;;) {
  205. irq = find_next_bit(q->queue, opp->max_irq, irq + 1);
  206. if (irq == opp->max_irq) {
  207. break;
  208. }
  209. DPRINTF("IRQ_check: irq %d set ivpr_pr=%d pr=%d\n",
  210. irq, IVPR_PRIORITY(opp->src[irq].ivpr), priority);
  211. if (IVPR_PRIORITY(opp->src[irq].ivpr) > priority) {
  212. next = irq;
  213. priority = IVPR_PRIORITY(opp->src[irq].ivpr);
  214. }
  215. }
  216. q->next = next;
  217. q->priority = priority;
  218. }
  219. static int IRQ_get_next(OpenPICState * opp, IRQQueue * q)
  220. {
  221. /* XXX: optimize */
  222. IRQ_check(opp, q);
  223. return q->next;
  224. }
  225. static void IRQ_local_pipe(OpenPICState * opp, int n_CPU, int n_IRQ,
  226. bool active, bool was_active)
  227. {
  228. IRQDest *dst;
  229. IRQSource *src;
  230. int priority;
  231. dst = &opp->dst[n_CPU];
  232. src = &opp->src[n_IRQ];
  233. DPRINTF("%s: IRQ %d active %d was %d\n",
  234. __func__, n_IRQ, active, was_active);
  235. if (src->output != OPENPIC_OUTPUT_INT) {
  236. DPRINTF("%s: output %d irq %d active %d was %d count %d\n",
  237. __func__, src->output, n_IRQ, active, was_active,
  238. dst->outputs_active[src->output]);
  239. /* On Freescale MPIC, critical interrupts ignore priority,
  240. * IACK, EOI, etc. Before MPIC v4.1 they also ignore
  241. * masking.
  242. */
  243. if (active) {
  244. if (!was_active
  245. && dst->outputs_active[src->output]++ == 0) {
  246. DPRINTF
  247. ("%s: Raise OpenPIC output %d cpu %d irq %d\n",
  248. __func__, src->output, n_CPU, n_IRQ);
  249. qemu_irq_raise(dst->irqs[src->output]);
  250. }
  251. } else {
  252. if (was_active
  253. && --dst->outputs_active[src->output] == 0) {
  254. DPRINTF
  255. ("%s: Lower OpenPIC output %d cpu %d irq %d\n",
  256. __func__, src->output, n_CPU, n_IRQ);
  257. qemu_irq_lower(dst->irqs[src->output]);
  258. }
  259. }
  260. return;
  261. }
  262. priority = IVPR_PRIORITY(src->ivpr);
  263. /* Even if the interrupt doesn't have enough priority,
  264. * it is still raised, in case ctpr is lowered later.
  265. */
  266. if (active) {
  267. IRQ_setbit(&dst->raised, n_IRQ);
  268. } else {
  269. IRQ_resetbit(&dst->raised, n_IRQ);
  270. }
  271. IRQ_check(opp, &dst->raised);
  272. if (active && priority <= dst->ctpr) {
  273. DPRINTF
  274. ("%s: IRQ %d priority %d too low for ctpr %d on CPU %d\n",
  275. __func__, n_IRQ, priority, dst->ctpr, n_CPU);
  276. active = 0;
  277. }
  278. if (active) {
  279. if (IRQ_get_next(opp, &dst->servicing) >= 0 &&
  280. priority <= dst->servicing.priority) {
  281. DPRINTF
  282. ("%s: IRQ %d is hidden by servicing IRQ %d on CPU %d\n",
  283. __func__, n_IRQ, dst->servicing.next, n_CPU);
  284. } else {
  285. DPRINTF
  286. ("%s: Raise OpenPIC INT output cpu %d irq %d/%d\n",
  287. __func__, n_CPU, n_IRQ, dst->raised.next);
  288. qemu_irq_raise(opp->dst[n_CPU].
  289. irqs[OPENPIC_OUTPUT_INT]);
  290. }
  291. } else {
  292. IRQ_get_next(opp, &dst->servicing);
  293. if (dst->raised.priority > dst->ctpr &&
  294. dst->raised.priority > dst->servicing.priority) {
  295. DPRINTF
  296. ("%s: IRQ %d inactive, IRQ %d prio %d above %d/%d, CPU %d\n",
  297. __func__, n_IRQ, dst->raised.next,
  298. dst->raised.priority, dst->ctpr,
  299. dst->servicing.priority, n_CPU);
  300. /* IRQ line stays asserted */
  301. } else {
  302. DPRINTF
  303. ("%s: IRQ %d inactive, current prio %d/%d, CPU %d\n",
  304. __func__, n_IRQ, dst->ctpr,
  305. dst->servicing.priority, n_CPU);
  306. qemu_irq_lower(opp->dst[n_CPU].
  307. irqs[OPENPIC_OUTPUT_INT]);
  308. }
  309. }
  310. }
  311. /* update pic state because registers for n_IRQ have changed value */
  312. static void openpic_update_irq(OpenPICState * opp, int n_IRQ)
  313. {
  314. IRQSource *src;
  315. bool active, was_active;
  316. int i;
  317. src = &opp->src[n_IRQ];
  318. active = src->pending;
  319. if ((src->ivpr & IVPR_MASK_MASK) && !src->nomask) {
  320. /* Interrupt source is disabled */
  321. DPRINTF("%s: IRQ %d is disabled\n", __func__, n_IRQ);
  322. active = false;
  323. }
  324. was_active = ! !(src->ivpr & IVPR_ACTIVITY_MASK);
  325. /*
  326. * We don't have a similar check for already-active because
  327. * ctpr may have changed and we need to withdraw the interrupt.
  328. */
  329. if (!active && !was_active) {
  330. DPRINTF("%s: IRQ %d is already inactive\n", __func__, n_IRQ);
  331. return;
  332. }
  333. if (active) {
  334. src->ivpr |= IVPR_ACTIVITY_MASK;
  335. } else {
  336. src->ivpr &= ~IVPR_ACTIVITY_MASK;
  337. }
  338. if (src->destmask == 0) {
  339. /* No target */
  340. DPRINTF("%s: IRQ %d has no target\n", __func__, n_IRQ);
  341. return;
  342. }
  343. if (src->destmask == (1 << src->last_cpu)) {
  344. /* Only one CPU is allowed to receive this IRQ */
  345. IRQ_local_pipe(opp, src->last_cpu, n_IRQ, active, was_active);
  346. } else if (!(src->ivpr & IVPR_MODE_MASK)) {
  347. /* Directed delivery mode */
  348. for (i = 0; i < opp->nb_cpus; i++) {
  349. if (src->destmask & (1 << i)) {
  350. IRQ_local_pipe(opp, i, n_IRQ, active,
  351. was_active);
  352. }
  353. }
  354. } else {
  355. /* Distributed delivery mode */
  356. for (i = src->last_cpu + 1; i != src->last_cpu; i++) {
  357. if (i == opp->nb_cpus) {
  358. i = 0;
  359. }
  360. if (src->destmask & (1 << i)) {
  361. IRQ_local_pipe(opp, i, n_IRQ, active,
  362. was_active);
  363. src->last_cpu = i;
  364. break;
  365. }
  366. }
  367. }
  368. }
  369. static void openpic_set_irq(void *opaque, int n_IRQ, int level)
  370. {
  371. OpenPICState *opp = opaque;
  372. IRQSource *src;
  373. if (n_IRQ >= MAX_IRQ) {
  374. fprintf(stderr, "%s: IRQ %d out of range\n", __func__, n_IRQ);
  375. abort();
  376. }
  377. src = &opp->src[n_IRQ];
  378. DPRINTF("openpic: set irq %d = %d ivpr=0x%08x\n",
  379. n_IRQ, level, src->ivpr);
  380. if (src->level) {
  381. /* level-sensitive irq */
  382. src->pending = level;
  383. openpic_update_irq(opp, n_IRQ);
  384. } else {
  385. /* edge-sensitive irq */
  386. if (level) {
  387. src->pending = 1;
  388. openpic_update_irq(opp, n_IRQ);
  389. }
  390. if (src->output != OPENPIC_OUTPUT_INT) {
  391. /* Edge-triggered interrupts shouldn't be used
  392. * with non-INT delivery, but just in case,
  393. * try to make it do something sane rather than
  394. * cause an interrupt storm. This is close to
  395. * what you'd probably see happen in real hardware.
  396. */
  397. src->pending = 0;
  398. openpic_update_irq(opp, n_IRQ);
  399. }
  400. }
  401. }
  402. static void openpic_reset(DeviceState * d)
  403. {
  404. OpenPICState *opp = FROM_SYSBUS(typeof(*opp), SYS_BUS_DEVICE(d));
  405. int i;
  406. opp->gcr = GCR_RESET;
  407. /* Initialise controller registers */
  408. opp->frr = ((opp->nb_irqs - 1) << FRR_NIRQ_SHIFT) |
  409. ((opp->nb_cpus - 1) << FRR_NCPU_SHIFT) |
  410. (opp->vid << FRR_VID_SHIFT);
  411. opp->pir = 0;
  412. opp->spve = -1 & opp->vector_mask;
  413. opp->tfrr = opp->tfrr_reset;
  414. /* Initialise IRQ sources */
  415. for (i = 0; i < opp->max_irq; i++) {
  416. opp->src[i].ivpr = opp->ivpr_reset;
  417. opp->src[i].idr = opp->idr_reset;
  418. switch (opp->src[i].type) {
  419. case IRQ_TYPE_NORMAL:
  420. opp->src[i].level =
  421. ! !(opp->ivpr_reset & IVPR_SENSE_MASK);
  422. break;
  423. case IRQ_TYPE_FSLINT:
  424. opp->src[i].ivpr |= IVPR_POLARITY_MASK;
  425. break;
  426. case IRQ_TYPE_FSLSPECIAL:
  427. break;
  428. }
  429. }
  430. /* Initialise IRQ destinations */
  431. for (i = 0; i < MAX_CPU; i++) {
  432. opp->dst[i].ctpr = 15;
  433. memset(&opp->dst[i].raised, 0, sizeof(IRQQueue));
  434. opp->dst[i].raised.next = -1;
  435. memset(&opp->dst[i].servicing, 0, sizeof(IRQQueue));
  436. opp->dst[i].servicing.next = -1;
  437. }
  438. /* Initialise timers */
  439. for (i = 0; i < MAX_TMR; i++) {
  440. opp->timers[i].tccr = 0;
  441. opp->timers[i].tbcr = TBCR_CI;
  442. }
  443. /* Go out of RESET state */
  444. opp->gcr = 0;
  445. }
  446. static inline uint32_t read_IRQreg_idr(OpenPICState * opp, int n_IRQ)
  447. {
  448. return opp->src[n_IRQ].idr;
  449. }
  450. static inline uint32_t read_IRQreg_ilr(OpenPICState * opp, int n_IRQ)
  451. {
  452. if (opp->flags & OPENPIC_FLAG_ILR) {
  453. return output_to_inttgt(opp->src[n_IRQ].output);
  454. }
  455. return 0xffffffff;
  456. }
  457. static inline uint32_t read_IRQreg_ivpr(OpenPICState * opp, int n_IRQ)
  458. {
  459. return opp->src[n_IRQ].ivpr;
  460. }
  461. static inline void write_IRQreg_idr(OpenPICState * opp, int n_IRQ, uint32_t val)
  462. {
  463. IRQSource *src = &opp->src[n_IRQ];
  464. uint32_t normal_mask = (1UL << opp->nb_cpus) - 1;
  465. uint32_t crit_mask = 0;
  466. uint32_t mask = normal_mask;
  467. int crit_shift = IDR_EP_SHIFT - opp->nb_cpus;
  468. int i;
  469. if (opp->flags & OPENPIC_FLAG_IDR_CRIT) {
  470. crit_mask = mask << crit_shift;
  471. mask |= crit_mask | IDR_EP;
  472. }
  473. src->idr = val & mask;
  474. DPRINTF("Set IDR %d to 0x%08x\n", n_IRQ, src->idr);
  475. if (opp->flags & OPENPIC_FLAG_IDR_CRIT) {
  476. if (src->idr & crit_mask) {
  477. if (src->idr & normal_mask) {
  478. DPRINTF
  479. ("%s: IRQ configured for multiple output types, using "
  480. "critical\n", __func__);
  481. }
  482. src->output = OPENPIC_OUTPUT_CINT;
  483. src->nomask = true;
  484. src->destmask = 0;
  485. for (i = 0; i < opp->nb_cpus; i++) {
  486. int n_ci = IDR_CI0_SHIFT - i;
  487. if (src->idr & (1UL << n_ci)) {
  488. src->destmask |= 1UL << i;
  489. }
  490. }
  491. } else {
  492. src->output = OPENPIC_OUTPUT_INT;
  493. src->nomask = false;
  494. src->destmask = src->idr & normal_mask;
  495. }
  496. } else {
  497. src->destmask = src->idr;
  498. }
  499. }
  500. static inline void write_IRQreg_ilr(OpenPICState * opp, int n_IRQ, uint32_t val)
  501. {
  502. if (opp->flags & OPENPIC_FLAG_ILR) {
  503. IRQSource *src = &opp->src[n_IRQ];
  504. src->output = inttgt_to_output(val & ILR_INTTGT_MASK);
  505. DPRINTF("Set ILR %d to 0x%08x, output %d\n", n_IRQ, src->idr,
  506. src->output);
  507. /* TODO: on MPIC v4.0 only, set nomask for non-INT */
  508. }
  509. }
  510. static inline void write_IRQreg_ivpr(OpenPICState * opp, int n_IRQ,
  511. uint32_t val)
  512. {
  513. uint32_t mask;
  514. /* NOTE when implementing newer FSL MPIC models: starting with v4.0,
  515. * the polarity bit is read-only on internal interrupts.
  516. */
  517. mask = IVPR_MASK_MASK | IVPR_PRIORITY_MASK | IVPR_SENSE_MASK |
  518. IVPR_POLARITY_MASK | opp->vector_mask;
  519. /* ACTIVITY bit is read-only */
  520. opp->src[n_IRQ].ivpr =
  521. (opp->src[n_IRQ].ivpr & IVPR_ACTIVITY_MASK) | (val & mask);
  522. /* For FSL internal interrupts, The sense bit is reserved and zero,
  523. * and the interrupt is always level-triggered. Timers and IPIs
  524. * have no sense or polarity bits, and are edge-triggered.
  525. */
  526. switch (opp->src[n_IRQ].type) {
  527. case IRQ_TYPE_NORMAL:
  528. opp->src[n_IRQ].level =
  529. ! !(opp->src[n_IRQ].ivpr & IVPR_SENSE_MASK);
  530. break;
  531. case IRQ_TYPE_FSLINT:
  532. opp->src[n_IRQ].ivpr &= ~IVPR_SENSE_MASK;
  533. break;
  534. case IRQ_TYPE_FSLSPECIAL:
  535. opp->src[n_IRQ].ivpr &= ~(IVPR_POLARITY_MASK | IVPR_SENSE_MASK);
  536. break;
  537. }
  538. openpic_update_irq(opp, n_IRQ);
  539. DPRINTF("Set IVPR %d to 0x%08x -> 0x%08x\n", n_IRQ, val,
  540. opp->src[n_IRQ].ivpr);
  541. }
  542. static void openpic_gcr_write(OpenPICState * opp, uint64_t val)
  543. {
  544. bool mpic_proxy = false;
  545. if (val & GCR_RESET) {
  546. openpic_reset(&opp->busdev.qdev);
  547. return;
  548. }
  549. opp->gcr &= ~opp->mpic_mode_mask;
  550. opp->gcr |= val & opp->mpic_mode_mask;
  551. /* Set external proxy mode */
  552. if ((val & opp->mpic_mode_mask) == GCR_MODE_PROXY) {
  553. mpic_proxy = true;
  554. }
  555. ppce500_set_mpic_proxy(mpic_proxy);
  556. }
  557. static void openpic_gbl_write(void *opaque, hwaddr addr, uint64_t val,
  558. unsigned len)
  559. {
  560. OpenPICState *opp = opaque;
  561. IRQDest *dst;
  562. int idx;
  563. DPRINTF("%s: addr %#" HWADDR_PRIx " <= %08" PRIx64 "\n",
  564. __func__, addr, val);
  565. if (addr & 0xF) {
  566. return;
  567. }
  568. switch (addr) {
  569. case 0x00: /* Block Revision Register1 (BRR1) is Readonly */
  570. break;
  571. case 0x40:
  572. case 0x50:
  573. case 0x60:
  574. case 0x70:
  575. case 0x80:
  576. case 0x90:
  577. case 0xA0:
  578. case 0xB0:
  579. openpic_cpu_write_internal(opp, addr, val, get_current_cpu());
  580. break;
  581. case 0x1000: /* FRR */
  582. break;
  583. case 0x1020: /* GCR */
  584. openpic_gcr_write(opp, val);
  585. break;
  586. case 0x1080: /* VIR */
  587. break;
  588. case 0x1090: /* PIR */
  589. for (idx = 0; idx < opp->nb_cpus; idx++) {
  590. if ((val & (1 << idx)) && !(opp->pir & (1 << idx))) {
  591. DPRINTF
  592. ("Raise OpenPIC RESET output for CPU %d\n",
  593. idx);
  594. dst = &opp->dst[idx];
  595. qemu_irq_raise(dst->irqs[OPENPIC_OUTPUT_RESET]);
  596. } else if (!(val & (1 << idx))
  597. && (opp->pir & (1 << idx))) {
  598. DPRINTF
  599. ("Lower OpenPIC RESET output for CPU %d\n",
  600. idx);
  601. dst = &opp->dst[idx];
  602. qemu_irq_lower(dst->irqs[OPENPIC_OUTPUT_RESET]);
  603. }
  604. }
  605. opp->pir = val;
  606. break;
  607. case 0x10A0: /* IPI_IVPR */
  608. case 0x10B0:
  609. case 0x10C0:
  610. case 0x10D0:
  611. {
  612. int idx;
  613. idx = (addr - 0x10A0) >> 4;
  614. write_IRQreg_ivpr(opp, opp->irq_ipi0 + idx, val);
  615. }
  616. break;
  617. case 0x10E0: /* SPVE */
  618. opp->spve = val & opp->vector_mask;
  619. break;
  620. default:
  621. break;
  622. }
  623. }
  624. static uint64_t openpic_gbl_read(void *opaque, hwaddr addr, unsigned len)
  625. {
  626. OpenPICState *opp = opaque;
  627. uint32_t retval;
  628. DPRINTF("%s: addr %#" HWADDR_PRIx "\n", __func__, addr);
  629. retval = 0xFFFFFFFF;
  630. if (addr & 0xF) {
  631. return retval;
  632. }
  633. switch (addr) {
  634. case 0x1000: /* FRR */
  635. retval = opp->frr;
  636. break;
  637. case 0x1020: /* GCR */
  638. retval = opp->gcr;
  639. break;
  640. case 0x1080: /* VIR */
  641. retval = opp->vir;
  642. break;
  643. case 0x1090: /* PIR */
  644. retval = 0x00000000;
  645. break;
  646. case 0x00: /* Block Revision Register1 (BRR1) */
  647. retval = opp->brr1;
  648. break;
  649. case 0x40:
  650. case 0x50:
  651. case 0x60:
  652. case 0x70:
  653. case 0x80:
  654. case 0x90:
  655. case 0xA0:
  656. case 0xB0:
  657. retval =
  658. openpic_cpu_read_internal(opp, addr, get_current_cpu());
  659. break;
  660. case 0x10A0: /* IPI_IVPR */
  661. case 0x10B0:
  662. case 0x10C0:
  663. case 0x10D0:
  664. {
  665. int idx;
  666. idx = (addr - 0x10A0) >> 4;
  667. retval = read_IRQreg_ivpr(opp, opp->irq_ipi0 + idx);
  668. }
  669. break;
  670. case 0x10E0: /* SPVE */
  671. retval = opp->spve;
  672. break;
  673. default:
  674. break;
  675. }
  676. DPRINTF("%s: => 0x%08x\n", __func__, retval);
  677. return retval;
  678. }
  679. static void openpic_tmr_write(void *opaque, hwaddr addr, uint64_t val,
  680. unsigned len)
  681. {
  682. OpenPICState *opp = opaque;
  683. int idx;
  684. addr += 0x10f0;
  685. DPRINTF("%s: addr %#" HWADDR_PRIx " <= %08" PRIx64 "\n",
  686. __func__, addr, val);
  687. if (addr & 0xF) {
  688. return;
  689. }
  690. if (addr == 0x10f0) {
  691. /* TFRR */
  692. opp->tfrr = val;
  693. return;
  694. }
  695. idx = (addr >> 6) & 0x3;
  696. addr = addr & 0x30;
  697. switch (addr & 0x30) {
  698. case 0x00: /* TCCR */
  699. break;
  700. case 0x10: /* TBCR */
  701. if ((opp->timers[idx].tccr & TCCR_TOG) != 0 &&
  702. (val & TBCR_CI) == 0 &&
  703. (opp->timers[idx].tbcr & TBCR_CI) != 0) {
  704. opp->timers[idx].tccr &= ~TCCR_TOG;
  705. }
  706. opp->timers[idx].tbcr = val;
  707. break;
  708. case 0x20: /* TVPR */
  709. write_IRQreg_ivpr(opp, opp->irq_tim0 + idx, val);
  710. break;
  711. case 0x30: /* TDR */
  712. write_IRQreg_idr(opp, opp->irq_tim0 + idx, val);
  713. break;
  714. }
  715. }
  716. static uint64_t openpic_tmr_read(void *opaque, hwaddr addr, unsigned len)
  717. {
  718. OpenPICState *opp = opaque;
  719. uint32_t retval = -1;
  720. int idx;
  721. DPRINTF("%s: addr %#" HWADDR_PRIx "\n", __func__, addr);
  722. if (addr & 0xF) {
  723. goto out;
  724. }
  725. idx = (addr >> 6) & 0x3;
  726. if (addr == 0x0) {
  727. /* TFRR */
  728. retval = opp->tfrr;
  729. goto out;
  730. }
  731. switch (addr & 0x30) {
  732. case 0x00: /* TCCR */
  733. retval = opp->timers[idx].tccr;
  734. break;
  735. case 0x10: /* TBCR */
  736. retval = opp->timers[idx].tbcr;
  737. break;
  738. case 0x20: /* TIPV */
  739. retval = read_IRQreg_ivpr(opp, opp->irq_tim0 + idx);
  740. break;
  741. case 0x30: /* TIDE (TIDR) */
  742. retval = read_IRQreg_idr(opp, opp->irq_tim0 + idx);
  743. break;
  744. }
  745. out:
  746. DPRINTF("%s: => 0x%08x\n", __func__, retval);
  747. return retval;
  748. }
  749. static void openpic_src_write(void *opaque, hwaddr addr, uint64_t val,
  750. unsigned len)
  751. {
  752. OpenPICState *opp = opaque;
  753. int idx;
  754. DPRINTF("%s: addr %#" HWADDR_PRIx " <= %08" PRIx64 "\n",
  755. __func__, addr, val);
  756. addr = addr & 0xffff;
  757. idx = addr >> 5;
  758. switch (addr & 0x1f) {
  759. case 0x00:
  760. write_IRQreg_ivpr(opp, idx, val);
  761. break;
  762. case 0x10:
  763. write_IRQreg_idr(opp, idx, val);
  764. break;
  765. case 0x18:
  766. write_IRQreg_ilr(opp, idx, val);
  767. break;
  768. }
  769. }
  770. static uint64_t openpic_src_read(void *opaque, uint64_t addr, unsigned len)
  771. {
  772. OpenPICState *opp = opaque;
  773. uint32_t retval;
  774. int idx;
  775. DPRINTF("%s: addr %#" HWADDR_PRIx "\n", __func__, addr);
  776. retval = 0xFFFFFFFF;
  777. addr = addr & 0xffff;
  778. idx = addr >> 5;
  779. switch (addr & 0x1f) {
  780. case 0x00:
  781. retval = read_IRQreg_ivpr(opp, idx);
  782. break;
  783. case 0x10:
  784. retval = read_IRQreg_idr(opp, idx);
  785. break;
  786. case 0x18:
  787. retval = read_IRQreg_ilr(opp, idx);
  788. break;
  789. }
  790. DPRINTF("%s: => 0x%08x\n", __func__, retval);
  791. return retval;
  792. }
  793. static void openpic_msi_write(void *opaque, hwaddr addr, uint64_t val,
  794. unsigned size)
  795. {
  796. OpenPICState *opp = opaque;
  797. int idx = opp->irq_msi;
  798. int srs, ibs;
  799. DPRINTF("%s: addr %#" HWADDR_PRIx " <= 0x%08" PRIx64 "\n",
  800. __func__, addr, val);
  801. if (addr & 0xF) {
  802. return;
  803. }
  804. switch (addr) {
  805. case MSIIR_OFFSET:
  806. srs = val >> MSIIR_SRS_SHIFT;
  807. idx += srs;
  808. ibs = (val & MSIIR_IBS_MASK) >> MSIIR_IBS_SHIFT;
  809. opp->msi[srs].msir |= 1 << ibs;
  810. openpic_set_irq(opp, idx, 1);
  811. break;
  812. default:
  813. /* most registers are read-only, thus ignored */
  814. break;
  815. }
  816. }
  817. static uint64_t openpic_msi_read(void *opaque, hwaddr addr, unsigned size)
  818. {
  819. OpenPICState *opp = opaque;
  820. uint64_t r = 0;
  821. int i, srs;
  822. DPRINTF("%s: addr %#" HWADDR_PRIx "\n", __func__, addr);
  823. if (addr & 0xF) {
  824. return -1;
  825. }
  826. srs = addr >> 4;
  827. switch (addr) {
  828. case 0x00:
  829. case 0x10:
  830. case 0x20:
  831. case 0x30:
  832. case 0x40:
  833. case 0x50:
  834. case 0x60:
  835. case 0x70: /* MSIRs */
  836. r = opp->msi[srs].msir;
  837. /* Clear on read */
  838. opp->msi[srs].msir = 0;
  839. openpic_set_irq(opp, opp->irq_msi + srs, 0);
  840. break;
  841. case 0x120: /* MSISR */
  842. for (i = 0; i < MAX_MSI; i++) {
  843. r |= (opp->msi[i].msir ? 1 : 0) << i;
  844. }
  845. break;
  846. }
  847. return r;
  848. }
  849. static uint64_t openpic_summary_read(void *opaque, hwaddr addr, unsigned size)
  850. {
  851. uint64_t r = 0;
  852. DPRINTF("%s: addr %#" HWADDR_PRIx "\n", __func__, addr);
  853. /* TODO: EISR/EIMR */
  854. return r;
  855. }
  856. static void openpic_summary_write(void *opaque, hwaddr addr, uint64_t val,
  857. unsigned size)
  858. {
  859. DPRINTF("%s: addr %#" HWADDR_PRIx " <= 0x%08" PRIx64 "\n",
  860. __func__, addr, val);
  861. /* TODO: EISR/EIMR */
  862. }
  863. static void openpic_cpu_write_internal(void *opaque, hwaddr addr,
  864. uint32_t val, int idx)
  865. {
  866. OpenPICState *opp = opaque;
  867. IRQSource *src;
  868. IRQDest *dst;
  869. int s_IRQ, n_IRQ;
  870. DPRINTF("%s: cpu %d addr %#" HWADDR_PRIx " <= 0x%08x\n", __func__, idx,
  871. addr, val);
  872. if (idx < 0) {
  873. return;
  874. }
  875. if (addr & 0xF) {
  876. return;
  877. }
  878. dst = &opp->dst[idx];
  879. addr &= 0xFF0;
  880. switch (addr) {
  881. case 0x40: /* IPIDR */
  882. case 0x50:
  883. case 0x60:
  884. case 0x70:
  885. idx = (addr - 0x40) >> 4;
  886. /* we use IDE as mask which CPUs to deliver the IPI to still. */
  887. opp->src[opp->irq_ipi0 + idx].destmask |= val;
  888. openpic_set_irq(opp, opp->irq_ipi0 + idx, 1);
  889. openpic_set_irq(opp, opp->irq_ipi0 + idx, 0);
  890. break;
  891. case 0x80: /* CTPR */
  892. dst->ctpr = val & 0x0000000F;
  893. DPRINTF("%s: set CPU %d ctpr to %d, raised %d servicing %d\n",
  894. __func__, idx, dst->ctpr, dst->raised.priority,
  895. dst->servicing.priority);
  896. if (dst->raised.priority <= dst->ctpr) {
  897. DPRINTF
  898. ("%s: Lower OpenPIC INT output cpu %d due to ctpr\n",
  899. __func__, idx);
  900. qemu_irq_lower(dst->irqs[OPENPIC_OUTPUT_INT]);
  901. } else if (dst->raised.priority > dst->servicing.priority) {
  902. DPRINTF("%s: Raise OpenPIC INT output cpu %d irq %d\n",
  903. __func__, idx, dst->raised.next);
  904. qemu_irq_raise(dst->irqs[OPENPIC_OUTPUT_INT]);
  905. }
  906. break;
  907. case 0x90: /* WHOAMI */
  908. /* Read-only register */
  909. break;
  910. case 0xA0: /* IACK */
  911. /* Read-only register */
  912. break;
  913. case 0xB0: /* EOI */
  914. DPRINTF("EOI\n");
  915. s_IRQ = IRQ_get_next(opp, &dst->servicing);
  916. if (s_IRQ < 0) {
  917. DPRINTF("%s: EOI with no interrupt in service\n",
  918. __func__);
  919. break;
  920. }
  921. IRQ_resetbit(&dst->servicing, s_IRQ);
  922. /* Set up next servicing IRQ */
  923. s_IRQ = IRQ_get_next(opp, &dst->servicing);
  924. /* Check queued interrupts. */
  925. n_IRQ = IRQ_get_next(opp, &dst->raised);
  926. src = &opp->src[n_IRQ];
  927. if (n_IRQ != -1 &&
  928. (s_IRQ == -1 ||
  929. IVPR_PRIORITY(src->ivpr) > dst->servicing.priority)) {
  930. DPRINTF("Raise OpenPIC INT output cpu %d irq %d\n",
  931. idx, n_IRQ);
  932. qemu_irq_raise(opp->dst[idx].irqs[OPENPIC_OUTPUT_INT]);
  933. }
  934. break;
  935. default:
  936. break;
  937. }
  938. }
  939. static void openpic_cpu_write(void *opaque, hwaddr addr, uint64_t val,
  940. unsigned len)
  941. {
  942. openpic_cpu_write_internal(opaque, addr, val, (addr & 0x1f000) >> 12);
  943. }
  944. static uint32_t openpic_iack(OpenPICState * opp, IRQDest * dst, int cpu)
  945. {
  946. IRQSource *src;
  947. int retval, irq;
  948. DPRINTF("Lower OpenPIC INT output\n");
  949. qemu_irq_lower(dst->irqs[OPENPIC_OUTPUT_INT]);
  950. irq = IRQ_get_next(opp, &dst->raised);
  951. DPRINTF("IACK: irq=%d\n", irq);
  952. if (irq == -1) {
  953. /* No more interrupt pending */
  954. return opp->spve;
  955. }
  956. src = &opp->src[irq];
  957. if (!(src->ivpr & IVPR_ACTIVITY_MASK) ||
  958. !(IVPR_PRIORITY(src->ivpr) > dst->ctpr)) {
  959. fprintf(stderr, "%s: bad raised IRQ %d ctpr %d ivpr 0x%08x\n",
  960. __func__, irq, dst->ctpr, src->ivpr);
  961. openpic_update_irq(opp, irq);
  962. retval = opp->spve;
  963. } else {
  964. /* IRQ enter servicing state */
  965. IRQ_setbit(&dst->servicing, irq);
  966. retval = IVPR_VECTOR(opp, src->ivpr);
  967. }
  968. if (!src->level) {
  969. /* edge-sensitive IRQ */
  970. src->ivpr &= ~IVPR_ACTIVITY_MASK;
  971. src->pending = 0;
  972. IRQ_resetbit(&dst->raised, irq);
  973. }
  974. if ((irq >= opp->irq_ipi0) && (irq < (opp->irq_ipi0 + MAX_IPI))) {
  975. src->destmask &= ~(1 << cpu);
  976. if (src->destmask && !src->level) {
  977. /* trigger on CPUs that didn't know about it yet */
  978. openpic_set_irq(opp, irq, 1);
  979. openpic_set_irq(opp, irq, 0);
  980. /* if all CPUs knew about it, set active bit again */
  981. src->ivpr |= IVPR_ACTIVITY_MASK;
  982. }
  983. }
  984. return retval;
  985. }
  986. static uint32_t openpic_cpu_read_internal(void *opaque, hwaddr addr, int idx)
  987. {
  988. OpenPICState *opp = opaque;
  989. IRQDest *dst;
  990. uint32_t retval;
  991. DPRINTF("%s: cpu %d addr %#" HWADDR_PRIx "\n", __func__, idx, addr);
  992. retval = 0xFFFFFFFF;
  993. if (idx < 0) {
  994. return retval;
  995. }
  996. if (addr & 0xF) {
  997. return retval;
  998. }
  999. dst = &opp->dst[idx];
  1000. addr &= 0xFF0;
  1001. switch (addr) {
  1002. case 0x80: /* CTPR */
  1003. retval = dst->ctpr;
  1004. break;
  1005. case 0x90: /* WHOAMI */
  1006. retval = idx;
  1007. break;
  1008. case 0xA0: /* IACK */
  1009. retval = openpic_iack(opp, dst, idx);
  1010. break;
  1011. case 0xB0: /* EOI */
  1012. retval = 0;
  1013. break;
  1014. default:
  1015. break;
  1016. }
  1017. DPRINTF("%s: => 0x%08x\n", __func__, retval);
  1018. return retval;
  1019. }
  1020. static uint64_t openpic_cpu_read(void *opaque, hwaddr addr, unsigned len)
  1021. {
  1022. return openpic_cpu_read_internal(opaque, addr, (addr & 0x1f000) >> 12);
  1023. }
  1024. static const MemoryRegionOps openpic_glb_ops_be = {
  1025. .write = openpic_gbl_write,
  1026. .read = openpic_gbl_read,
  1027. };
  1028. static const MemoryRegionOps openpic_tmr_ops_be = {
  1029. .write = openpic_tmr_write,
  1030. .read = openpic_tmr_read,
  1031. };
  1032. static const MemoryRegionOps openpic_cpu_ops_be = {
  1033. .write = openpic_cpu_write,
  1034. .read = openpic_cpu_read,
  1035. };
  1036. static const MemoryRegionOps openpic_src_ops_be = {
  1037. .write = openpic_src_write,
  1038. .read = openpic_src_read,
  1039. };
  1040. static const MemoryRegionOps openpic_msi_ops_be = {
  1041. .read = openpic_msi_read,
  1042. .write = openpic_msi_write,
  1043. };
  1044. static const MemoryRegionOps openpic_summary_ops_be = {
  1045. .read = openpic_summary_read,
  1046. .write = openpic_summary_write,
  1047. };
  1048. typedef struct MemReg {
  1049. const char *name;
  1050. MemoryRegionOps const *ops;
  1051. hwaddr start_addr;
  1052. ram_addr_t size;
  1053. } MemReg;
  1054. static void fsl_common_init(OpenPICState * opp)
  1055. {
  1056. int i;
  1057. int virq = MAX_SRC;
  1058. opp->vid = VID_REVISION_1_2;
  1059. opp->vir = VIR_GENERIC;
  1060. opp->vector_mask = 0xFFFF;
  1061. opp->tfrr_reset = 0;
  1062. opp->ivpr_reset = IVPR_MASK_MASK;
  1063. opp->idr_reset = 1 << 0;
  1064. opp->max_irq = MAX_IRQ;
  1065. opp->irq_ipi0 = virq;
  1066. virq += MAX_IPI;
  1067. opp->irq_tim0 = virq;
  1068. virq += MAX_TMR;
  1069. assert(virq <= MAX_IRQ);
  1070. opp->irq_msi = 224;
  1071. msi_supported = true;
  1072. for (i = 0; i < opp->fsl->max_ext; i++) {
  1073. opp->src[i].level = false;
  1074. }
  1075. /* Internal interrupts, including message and MSI */
  1076. for (i = 16; i < MAX_SRC; i++) {
  1077. opp->src[i].type = IRQ_TYPE_FSLINT;
  1078. opp->src[i].level = true;
  1079. }
  1080. /* timers and IPIs */
  1081. for (i = MAX_SRC; i < virq; i++) {
  1082. opp->src[i].type = IRQ_TYPE_FSLSPECIAL;
  1083. opp->src[i].level = false;
  1084. }
  1085. }
  1086. static void map_list(OpenPICState * opp, const MemReg * list, int *count)
  1087. {
  1088. while (list->name) {
  1089. assert(*count < ARRAY_SIZE(opp->sub_io_mem));
  1090. memory_region_init_io(&opp->sub_io_mem[*count], list->ops, opp,
  1091. list->name, list->size);
  1092. memory_region_add_subregion(&opp->mem, list->start_addr,
  1093. &opp->sub_io_mem[*count]);
  1094. (*count)++;
  1095. list++;
  1096. }
  1097. }
  1098. static int openpic_init(SysBusDevice * dev)
  1099. {
  1100. OpenPICState *opp = FROM_SYSBUS(typeof(*opp), dev);
  1101. int i, j;
  1102. int list_count = 0;
  1103. static const MemReg list_le[] = {
  1104. {"glb", &openpic_glb_ops_le,
  1105. OPENPIC_GLB_REG_START, OPENPIC_GLB_REG_SIZE},
  1106. {"tmr", &openpic_tmr_ops_le,
  1107. OPENPIC_TMR_REG_START, OPENPIC_TMR_REG_SIZE},
  1108. {"src", &openpic_src_ops_le,
  1109. OPENPIC_SRC_REG_START, OPENPIC_SRC_REG_SIZE},
  1110. {"cpu", &openpic_cpu_ops_le,
  1111. OPENPIC_CPU_REG_START, OPENPIC_CPU_REG_SIZE},
  1112. {NULL}
  1113. };
  1114. static const MemReg list_be[] = {
  1115. {"glb", &openpic_glb_ops_be,
  1116. OPENPIC_GLB_REG_START, OPENPIC_GLB_REG_SIZE},
  1117. {"tmr", &openpic_tmr_ops_be,
  1118. OPENPIC_TMR_REG_START, OPENPIC_TMR_REG_SIZE},
  1119. {"src", &openpic_src_ops_be,
  1120. OPENPIC_SRC_REG_START, OPENPIC_SRC_REG_SIZE},
  1121. {"cpu", &openpic_cpu_ops_be,
  1122. OPENPIC_CPU_REG_START, OPENPIC_CPU_REG_SIZE},
  1123. {NULL}
  1124. };
  1125. static const MemReg list_fsl[] = {
  1126. {"msi", &openpic_msi_ops_be,
  1127. OPENPIC_MSI_REG_START, OPENPIC_MSI_REG_SIZE},
  1128. {"summary", &openpic_summary_ops_be,
  1129. OPENPIC_SUMMARY_REG_START, OPENPIC_SUMMARY_REG_SIZE},
  1130. {NULL}
  1131. };
  1132. memory_region_init(&opp->mem, "openpic", 0x40000);
  1133. switch (opp->model) {
  1134. case OPENPIC_MODEL_FSL_MPIC_20:
  1135. default:
  1136. opp->fsl = &fsl_mpic_20;
  1137. opp->brr1 = 0x00400200;
  1138. opp->flags |= OPENPIC_FLAG_IDR_CRIT;
  1139. opp->nb_irqs = 80;
  1140. opp->mpic_mode_mask = GCR_MODE_MIXED;
  1141. fsl_common_init(opp);
  1142. map_list(opp, list_be, &list_count);
  1143. map_list(opp, list_fsl, &list_count);
  1144. break;
  1145. case OPENPIC_MODEL_FSL_MPIC_42:
  1146. opp->fsl = &fsl_mpic_42;
  1147. opp->brr1 = 0x00400402;
  1148. opp->flags |= OPENPIC_FLAG_ILR;
  1149. opp->nb_irqs = 196;
  1150. opp->mpic_mode_mask = GCR_MODE_PROXY;
  1151. fsl_common_init(opp);
  1152. map_list(opp, list_be, &list_count);
  1153. map_list(opp, list_fsl, &list_count);
  1154. break;
  1155. }
  1156. return 0;
  1157. }