]> git.proxmox.com Git - mirror_qemu.git/blame - hw/alpha/typhoon.c
iommu: add is_write as a parameter to the translate function of MemoryRegionIOMMUOps
[mirror_qemu.git] / hw / alpha / typhoon.c
CommitLineData
80bb2ff7
RH
1/*
2 * DEC 21272 (TSUNAMI/TYPHOON) chipset emulation.
3 *
4 * Written by Richard Henderson.
5 *
6 * This work is licensed under the GNU GPL license version 2 or later.
7 */
8
9#include "cpu.h"
83c9f4ca 10#include "hw/hw.h"
bd2be150 11#include "hw/devices.h"
9c17d615 12#include "sysemu/sysemu.h"
47b43a1f 13#include "alpha_sys.h"
022c62cb 14#include "exec/address-spaces.h"
80bb2ff7
RH
15
16
94dd91d6
AF
17#define TYPE_TYPHOON_PCI_HOST_BRIDGE "typhoon-pcihost"
18
80bb2ff7
RH
19typedef struct TyphoonCchip {
20 MemoryRegion region;
21 uint64_t misc;
22 uint64_t drir;
23 uint64_t dim[4];
24 uint32_t iic[4];
ad601177 25 AlphaCPU *cpu[4];
80bb2ff7
RH
26} TyphoonCchip;
27
28typedef struct TyphoonWindow {
b83c4db8
RH
29 uint64_t wba;
30 uint64_t wsm;
31 uint64_t tba;
80bb2ff7
RH
32} TyphoonWindow;
33
34typedef struct TyphoonPchip {
35 MemoryRegion region;
36 MemoryRegion reg_iack;
37 MemoryRegion reg_mem;
38 MemoryRegion reg_io;
39 MemoryRegion reg_conf;
b83c4db8
RH
40
41 AddressSpace iommu_as;
42 MemoryRegion iommu;
43
80bb2ff7
RH
44 uint64_t ctl;
45 TyphoonWindow win[4];
46} TyphoonPchip;
47
94dd91d6
AF
48#define TYPHOON_PCI_HOST_BRIDGE(obj) \
49 OBJECT_CHECK(TyphoonState, (obj), TYPE_TYPHOON_PCI_HOST_BRIDGE)
50
80bb2ff7 51typedef struct TyphoonState {
67c332fd 52 PCIHostState parent_obj;
94dd91d6 53
80bb2ff7
RH
54 TyphoonCchip cchip;
55 TyphoonPchip pchip;
56 MemoryRegion dchip_region;
57 MemoryRegion ram_region;
80bb2ff7
RH
58} TyphoonState;
59
60/* Called when one of DRIR or DIM changes. */
ad601177 61static void cpu_irq_change(AlphaCPU *cpu, uint64_t req)
80bb2ff7
RH
62{
63 /* If there are any non-masked interrupts, tell the cpu. */
ad601177 64 if (cpu != NULL) {
d8ed887b 65 CPUState *cs = CPU(cpu);
80bb2ff7 66 if (req) {
c3affe56 67 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
80bb2ff7 68 } else {
d8ed887b 69 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
80bb2ff7
RH
70 }
71 }
72}
73
a8170e5e 74static uint64_t cchip_read(void *opaque, hwaddr addr, unsigned size)
80bb2ff7 75{
4917cf44 76 CPUState *cpu = current_cpu;
80bb2ff7
RH
77 TyphoonState *s = opaque;
78 uint64_t ret = 0;
79
80bb2ff7
RH
80 switch (addr) {
81 case 0x0000:
82 /* CSC: Cchip System Configuration Register. */
83 /* All sorts of data here; probably the only thing relevant is
84 PIP<14> Pchip 1 Present = 0. */
85 break;
86
87 case 0x0040:
88 /* MTR: Memory Timing Register. */
89 /* All sorts of stuff related to real DRAM. */
90 break;
91
92 case 0x0080:
93 /* MISC: Miscellaneous Register. */
55e5c285 94 ret = s->cchip.misc | (cpu->cpu_index & 3);
80bb2ff7
RH
95 break;
96
97 case 0x00c0:
98 /* MPD: Memory Presence Detect Register. */
99 break;
100
101 case 0x0100: /* AAR0 */
102 case 0x0140: /* AAR1 */
103 case 0x0180: /* AAR2 */
104 case 0x01c0: /* AAR3 */
105 /* AAR: Array Address Register. */
106 /* All sorts of information about DRAM. */
107 break;
108
109 case 0x0200:
110 /* DIM0: Device Interrupt Mask Register, CPU0. */
111 ret = s->cchip.dim[0];
112 break;
113 case 0x0240:
114 /* DIM1: Device Interrupt Mask Register, CPU1. */
115 ret = s->cchip.dim[1];
116 break;
117 case 0x0280:
118 /* DIR0: Device Interrupt Request Register, CPU0. */
119 ret = s->cchip.dim[0] & s->cchip.drir;
120 break;
121 case 0x02c0:
122 /* DIR1: Device Interrupt Request Register, CPU1. */
123 ret = s->cchip.dim[1] & s->cchip.drir;
124 break;
125 case 0x0300:
126 /* DRIR: Device Raw Interrupt Request Register. */
127 ret = s->cchip.drir;
128 break;
129
130 case 0x0340:
131 /* PRBEN: Probe Enable Register. */
132 break;
133
134 case 0x0380:
135 /* IIC0: Interval Ignore Count Register, CPU0. */
136 ret = s->cchip.iic[0];
137 break;
138 case 0x03c0:
139 /* IIC1: Interval Ignore Count Register, CPU1. */
140 ret = s->cchip.iic[1];
141 break;
142
143 case 0x0400: /* MPR0 */
144 case 0x0440: /* MPR1 */
145 case 0x0480: /* MPR2 */
146 case 0x04c0: /* MPR3 */
147 /* MPR: Memory Programming Register. */
148 break;
149
150 case 0x0580:
151 /* TTR: TIGbus Timing Register. */
152 /* All sorts of stuff related to interrupt delivery timings. */
153 break;
154 case 0x05c0:
155 /* TDR: TIGbug Device Timing Register. */
156 break;
157
158 case 0x0600:
159 /* DIM2: Device Interrupt Mask Register, CPU2. */
160 ret = s->cchip.dim[2];
161 break;
162 case 0x0640:
163 /* DIM3: Device Interrupt Mask Register, CPU3. */
164 ret = s->cchip.dim[3];
165 break;
166 case 0x0680:
167 /* DIR2: Device Interrupt Request Register, CPU2. */
168 ret = s->cchip.dim[2] & s->cchip.drir;
169 break;
170 case 0x06c0:
171 /* DIR3: Device Interrupt Request Register, CPU3. */
172 ret = s->cchip.dim[3] & s->cchip.drir;
173 break;
174
175 case 0x0700:
176 /* IIC2: Interval Ignore Count Register, CPU2. */
177 ret = s->cchip.iic[2];
178 break;
179 case 0x0740:
180 /* IIC3: Interval Ignore Count Register, CPU3. */
181 ret = s->cchip.iic[3];
182 break;
183
184 case 0x0780:
185 /* PWR: Power Management Control. */
186 break;
187
188 case 0x0c00: /* CMONCTLA */
189 case 0x0c40: /* CMONCTLB */
190 case 0x0c80: /* CMONCNT01 */
191 case 0x0cc0: /* CMONCNT23 */
192 break;
193
194 default:
c658b94f 195 cpu_unassigned_access(cpu, addr, false, false, 0, size);
80bb2ff7
RH
196 return -1;
197 }
198
80bb2ff7
RH
199 return ret;
200}
201
a8170e5e 202static uint64_t dchip_read(void *opaque, hwaddr addr, unsigned size)
80bb2ff7
RH
203{
204 /* Skip this. It's all related to DRAM timing and setup. */
205 return 0;
206}
207
a8170e5e 208static uint64_t pchip_read(void *opaque, hwaddr addr, unsigned size)
80bb2ff7
RH
209{
210 TyphoonState *s = opaque;
211 uint64_t ret = 0;
212
80bb2ff7
RH
213 switch (addr) {
214 case 0x0000:
215 /* WSBA0: Window Space Base Address Register. */
b83c4db8 216 ret = s->pchip.win[0].wba;
80bb2ff7
RH
217 break;
218 case 0x0040:
219 /* WSBA1 */
b83c4db8 220 ret = s->pchip.win[1].wba;
80bb2ff7
RH
221 break;
222 case 0x0080:
223 /* WSBA2 */
b83c4db8 224 ret = s->pchip.win[2].wba;
80bb2ff7
RH
225 break;
226 case 0x00c0:
227 /* WSBA3 */
b83c4db8 228 ret = s->pchip.win[3].wba;
80bb2ff7
RH
229 break;
230
231 case 0x0100:
232 /* WSM0: Window Space Mask Register. */
b83c4db8 233 ret = s->pchip.win[0].wsm;
80bb2ff7
RH
234 break;
235 case 0x0140:
236 /* WSM1 */
b83c4db8 237 ret = s->pchip.win[1].wsm;
80bb2ff7
RH
238 break;
239 case 0x0180:
240 /* WSM2 */
b83c4db8 241 ret = s->pchip.win[2].wsm;
80bb2ff7
RH
242 break;
243 case 0x01c0:
244 /* WSM3 */
b83c4db8 245 ret = s->pchip.win[3].wsm;
80bb2ff7
RH
246 break;
247
248 case 0x0200:
249 /* TBA0: Translated Base Address Register. */
b83c4db8 250 ret = s->pchip.win[0].tba;
80bb2ff7
RH
251 break;
252 case 0x0240:
253 /* TBA1 */
b83c4db8 254 ret = s->pchip.win[1].tba;
80bb2ff7
RH
255 break;
256 case 0x0280:
257 /* TBA2 */
b83c4db8 258 ret = s->pchip.win[2].tba;
80bb2ff7
RH
259 break;
260 case 0x02c0:
261 /* TBA3 */
b83c4db8 262 ret = s->pchip.win[3].tba;
80bb2ff7
RH
263 break;
264
265 case 0x0300:
266 /* PCTL: Pchip Control Register. */
267 ret = s->pchip.ctl;
268 break;
269 case 0x0340:
270 /* PLAT: Pchip Master Latency Register. */
271 break;
272 case 0x03c0:
273 /* PERROR: Pchip Error Register. */
274 break;
275 case 0x0400:
276 /* PERRMASK: Pchip Error Mask Register. */
277 break;
278 case 0x0440:
279 /* PERRSET: Pchip Error Set Register. */
280 break;
281 case 0x0480:
282 /* TLBIV: Translation Buffer Invalidate Virtual Register (WO). */
283 break;
284 case 0x04c0:
285 /* TLBIA: Translation Buffer Invalidate All Register (WO). */
286 break;
287 case 0x0500: /* PMONCTL */
288 case 0x0540: /* PMONCNT */
289 case 0x0800: /* SPRST */
290 break;
291
292 default:
4917cf44 293 cpu_unassigned_access(current_cpu, addr, false, false, 0, size);
80bb2ff7
RH
294 return -1;
295 }
296
80bb2ff7
RH
297 return ret;
298}
299
a8170e5e 300static void cchip_write(void *opaque, hwaddr addr,
67842165 301 uint64_t val, unsigned size)
80bb2ff7
RH
302{
303 TyphoonState *s = opaque;
67842165 304 uint64_t oldval, newval;
80bb2ff7
RH
305
306 switch (addr) {
307 case 0x0000:
308 /* CSC: Cchip System Configuration Register. */
309 /* All sorts of data here; nothing relevant RW. */
310 break;
311
312 case 0x0040:
313 /* MTR: Memory Timing Register. */
314 /* All sorts of stuff related to real DRAM. */
315 break;
316
317 case 0x0080:
318 /* MISC: Miscellaneous Register. */
319 newval = oldval = s->cchip.misc;
320 newval &= ~(val & 0x10000ff0); /* W1C fields */
321 if (val & 0x100000) {
322 newval &= ~0xff0000ull; /* ACL clears ABT and ABW */
323 } else {
324 newval |= val & 0x00f00000; /* ABT field is W1S */
325 if ((newval & 0xf0000) == 0) {
326 newval |= val & 0xf0000; /* ABW field is W1S iff zero */
327 }
328 }
329 newval |= (val & 0xf000) >> 4; /* IPREQ field sets IPINTR. */
330
331 newval &= ~0xf0000000000ull; /* WO and RW fields */
332 newval |= val & 0xf0000000000ull;
333 s->cchip.misc = newval;
334
335 /* Pass on changes to IPI and ITI state. */
336 if ((newval ^ oldval) & 0xff0) {
337 int i;
338 for (i = 0; i < 4; ++i) {
ad601177
AF
339 AlphaCPU *cpu = s->cchip.cpu[i];
340 if (cpu != NULL) {
d8ed887b 341 CPUState *cs = CPU(cpu);
80bb2ff7
RH
342 /* IPI can be either cleared or set by the write. */
343 if (newval & (1 << (i + 8))) {
c3affe56 344 cpu_interrupt(cs, CPU_INTERRUPT_SMP);
80bb2ff7 345 } else {
d8ed887b 346 cpu_reset_interrupt(cs, CPU_INTERRUPT_SMP);
80bb2ff7
RH
347 }
348
349 /* ITI can only be cleared by the write. */
350 if ((newval & (1 << (i + 4))) == 0) {
d8ed887b 351 cpu_reset_interrupt(cs, CPU_INTERRUPT_TIMER);
80bb2ff7
RH
352 }
353 }
354 }
355 }
356 break;
357
358 case 0x00c0:
359 /* MPD: Memory Presence Detect Register. */
360 break;
361
362 case 0x0100: /* AAR0 */
363 case 0x0140: /* AAR1 */
364 case 0x0180: /* AAR2 */
365 case 0x01c0: /* AAR3 */
366 /* AAR: Array Address Register. */
367 /* All sorts of information about DRAM. */
368 break;
369
370 case 0x0200: /* DIM0 */
371 /* DIM: Device Interrupt Mask Register, CPU0. */
372 s->cchip.dim[0] = val;
373 cpu_irq_change(s->cchip.cpu[0], val & s->cchip.drir);
374 break;
375 case 0x0240: /* DIM1 */
376 /* DIM: Device Interrupt Mask Register, CPU1. */
377 s->cchip.dim[0] = val;
378 cpu_irq_change(s->cchip.cpu[1], val & s->cchip.drir);
379 break;
380
381 case 0x0280: /* DIR0 (RO) */
382 case 0x02c0: /* DIR1 (RO) */
383 case 0x0300: /* DRIR (RO) */
384 break;
385
386 case 0x0340:
387 /* PRBEN: Probe Enable Register. */
388 break;
389
390 case 0x0380: /* IIC0 */
391 s->cchip.iic[0] = val & 0xffffff;
392 break;
393 case 0x03c0: /* IIC1 */
394 s->cchip.iic[1] = val & 0xffffff;
395 break;
396
397 case 0x0400: /* MPR0 */
398 case 0x0440: /* MPR1 */
399 case 0x0480: /* MPR2 */
400 case 0x04c0: /* MPR3 */
401 /* MPR: Memory Programming Register. */
402 break;
403
404 case 0x0580:
405 /* TTR: TIGbus Timing Register. */
406 /* All sorts of stuff related to interrupt delivery timings. */
407 break;
408 case 0x05c0:
409 /* TDR: TIGbug Device Timing Register. */
410 break;
411
412 case 0x0600:
413 /* DIM2: Device Interrupt Mask Register, CPU2. */
414 s->cchip.dim[2] = val;
415 cpu_irq_change(s->cchip.cpu[2], val & s->cchip.drir);
416 break;
417 case 0x0640:
418 /* DIM3: Device Interrupt Mask Register, CPU3. */
419 s->cchip.dim[3] = val;
420 cpu_irq_change(s->cchip.cpu[3], val & s->cchip.drir);
421 break;
422
423 case 0x0680: /* DIR2 (RO) */
424 case 0x06c0: /* DIR3 (RO) */
425 break;
426
427 case 0x0700: /* IIC2 */
428 s->cchip.iic[2] = val & 0xffffff;
429 break;
430 case 0x0740: /* IIC3 */
431 s->cchip.iic[3] = val & 0xffffff;
432 break;
433
434 case 0x0780:
435 /* PWR: Power Management Control. */
436 break;
437
438 case 0x0c00: /* CMONCTLA */
439 case 0x0c40: /* CMONCTLB */
440 case 0x0c80: /* CMONCNT01 */
441 case 0x0cc0: /* CMONCNT23 */
442 break;
443
444 default:
4917cf44 445 cpu_unassigned_access(current_cpu, addr, true, false, 0, size);
80bb2ff7
RH
446 return;
447 }
448}
449
a8170e5e 450static void dchip_write(void *opaque, hwaddr addr,
80bb2ff7
RH
451 uint64_t val, unsigned size)
452{
453 /* Skip this. It's all related to DRAM timing and setup. */
454}
455
a8170e5e 456static void pchip_write(void *opaque, hwaddr addr,
67842165 457 uint64_t val, unsigned size)
80bb2ff7
RH
458{
459 TyphoonState *s = opaque;
67842165 460 uint64_t oldval;
80bb2ff7
RH
461
462 switch (addr) {
463 case 0x0000:
464 /* WSBA0: Window Space Base Address Register. */
b83c4db8 465 s->pchip.win[0].wba = val & 0xfff00003u;
80bb2ff7
RH
466 break;
467 case 0x0040:
468 /* WSBA1 */
b83c4db8 469 s->pchip.win[1].wba = val & 0xfff00003u;
80bb2ff7
RH
470 break;
471 case 0x0080:
472 /* WSBA2 */
b83c4db8 473 s->pchip.win[2].wba = val & 0xfff00003u;
80bb2ff7
RH
474 break;
475 case 0x00c0:
476 /* WSBA3 */
b83c4db8 477 s->pchip.win[3].wba = (val & 0x80fff00001ull) | 2;
80bb2ff7
RH
478 break;
479
480 case 0x0100:
481 /* WSM0: Window Space Mask Register. */
b83c4db8 482 s->pchip.win[0].wsm = val & 0xfff00000u;
80bb2ff7
RH
483 break;
484 case 0x0140:
485 /* WSM1 */
b83c4db8 486 s->pchip.win[1].wsm = val & 0xfff00000u;
80bb2ff7
RH
487 break;
488 case 0x0180:
489 /* WSM2 */
b83c4db8 490 s->pchip.win[2].wsm = val & 0xfff00000u;
80bb2ff7
RH
491 break;
492 case 0x01c0:
493 /* WSM3 */
b83c4db8 494 s->pchip.win[3].wsm = val & 0xfff00000u;
80bb2ff7
RH
495 break;
496
497 case 0x0200:
498 /* TBA0: Translated Base Address Register. */
b83c4db8 499 s->pchip.win[0].tba = val & 0x7fffffc00ull;
80bb2ff7
RH
500 break;
501 case 0x0240:
502 /* TBA1 */
b83c4db8 503 s->pchip.win[1].tba = val & 0x7fffffc00ull;
80bb2ff7
RH
504 break;
505 case 0x0280:
506 /* TBA2 */
b83c4db8 507 s->pchip.win[2].tba = val & 0x7fffffc00ull;
80bb2ff7
RH
508 break;
509 case 0x02c0:
510 /* TBA3 */
b83c4db8 511 s->pchip.win[3].tba = val & 0x7fffffc00ull;
80bb2ff7
RH
512 break;
513
514 case 0x0300:
515 /* PCTL: Pchip Control Register. */
516 oldval = s->pchip.ctl;
517 oldval &= ~0x00001cff0fc7ffull; /* RW fields */
518 oldval |= val & 0x00001cff0fc7ffull;
80bb2ff7
RH
519 s->pchip.ctl = oldval;
520 break;
521
522 case 0x0340:
523 /* PLAT: Pchip Master Latency Register. */
524 break;
525 case 0x03c0:
526 /* PERROR: Pchip Error Register. */
527 break;
528 case 0x0400:
529 /* PERRMASK: Pchip Error Mask Register. */
530 break;
531 case 0x0440:
532 /* PERRSET: Pchip Error Set Register. */
533 break;
534
535 case 0x0480:
536 /* TLBIV: Translation Buffer Invalidate Virtual Register. */
537 break;
538
539 case 0x04c0:
540 /* TLBIA: Translation Buffer Invalidate All Register (WO). */
541 break;
542
543 case 0x0500:
544 /* PMONCTL */
545 case 0x0540:
546 /* PMONCNT */
547 case 0x0800:
548 /* SPRST */
549 break;
550
551 default:
4917cf44 552 cpu_unassigned_access(current_cpu, addr, true, false, 0, size);
80bb2ff7
RH
553 return;
554 }
555}
556
557static const MemoryRegionOps cchip_ops = {
558 .read = cchip_read,
559 .write = cchip_write,
560 .endianness = DEVICE_LITTLE_ENDIAN,
561 .valid = {
67842165 562 .min_access_size = 8,
80bb2ff7
RH
563 .max_access_size = 8,
564 },
565 .impl = {
67842165
RH
566 .min_access_size = 8,
567 .max_access_size = 8,
80bb2ff7
RH
568 },
569};
570
571static const MemoryRegionOps dchip_ops = {
572 .read = dchip_read,
573 .write = dchip_write,
574 .endianness = DEVICE_LITTLE_ENDIAN,
575 .valid = {
67842165 576 .min_access_size = 8,
80bb2ff7
RH
577 .max_access_size = 8,
578 },
579 .impl = {
67842165 580 .min_access_size = 8,
80bb2ff7
RH
581 .max_access_size = 8,
582 },
583};
584
585static const MemoryRegionOps pchip_ops = {
586 .read = pchip_read,
587 .write = pchip_write,
588 .endianness = DEVICE_LITTLE_ENDIAN,
589 .valid = {
67842165 590 .min_access_size = 8,
80bb2ff7
RH
591 .max_access_size = 8,
592 },
593 .impl = {
67842165
RH
594 .min_access_size = 8,
595 .max_access_size = 8,
80bb2ff7
RH
596 },
597};
598
b83c4db8
RH
599/* A subroutine of typhoon_translate_iommu that builds an IOMMUTLBEntry
600 using the given translated address and mask. */
601static bool make_iommu_tlbe(hwaddr taddr, hwaddr mask, IOMMUTLBEntry *ret)
602{
603 *ret = (IOMMUTLBEntry) {
604 .target_as = &address_space_memory,
605 .translated_addr = taddr,
606 .addr_mask = mask,
607 .perm = IOMMU_RW,
608 };
609 return true;
610}
611
612/* A subroutine of typhoon_translate_iommu that handles scatter-gather
613 translation, given the address of the PTE. */
614static bool pte_translate(hwaddr pte_addr, IOMMUTLBEntry *ret)
615{
2c17449b 616 uint64_t pte = ldq_phys(&address_space_memory, pte_addr);
b83c4db8
RH
617
618 /* Check valid bit. */
619 if ((pte & 1) == 0) {
620 return false;
621 }
622
623 return make_iommu_tlbe((pte & 0x3ffffe) << 12, 0x1fff, ret);
624}
625
626/* A subroutine of typhoon_translate_iommu that handles one of the
627 four single-address-cycle translation windows. */
628static bool window_translate(TyphoonWindow *win, hwaddr addr,
629 IOMMUTLBEntry *ret)
630{
631 uint32_t wba = win->wba;
632 uint64_t wsm = win->wsm;
633 uint64_t tba = win->tba;
634 uint64_t wsm_ext = wsm | 0xfffff;
635
636 /* Check for window disabled. */
637 if ((wba & 1) == 0) {
638 return false;
639 }
640
641 /* Check for window hit. */
642 if ((addr & ~wsm_ext) != (wba & 0xfff00000u)) {
643 return false;
644 }
645
646 if (wba & 2) {
647 /* Scatter-gather translation. */
648 hwaddr pte_addr;
649
650 /* See table 10-6, Generating PTE address for PCI DMA Address. */
651 pte_addr = tba & ~(wsm >> 10);
652 pte_addr |= (addr & (wsm | 0xfe000)) >> 10;
653 return pte_translate(pte_addr, ret);
654 } else {
655 /* Direct-mapped translation. */
656 return make_iommu_tlbe(tba & ~wsm_ext, wsm_ext, ret);
657 }
658}
659
660/* Handle PCI-to-system address translation. */
661/* TODO: A translation failure here ought to set PCI error codes on the
662 Pchip and generate a machine check interrupt. */
8d7b8cb9
LT
663static IOMMUTLBEntry typhoon_translate_iommu(MemoryRegion *iommu, hwaddr addr,
664 bool is_write)
b83c4db8
RH
665{
666 TyphoonPchip *pchip = container_of(iommu, TyphoonPchip, iommu);
667 IOMMUTLBEntry ret;
668 int i;
669
670 if (addr <= 0xffffffffu) {
671 /* Single-address cycle. */
672
673 /* Check for the Window Hole, inhibiting matching. */
674 if ((pchip->ctl & 0x20)
675 && addr >= 0x80000
676 && addr <= 0xfffff) {
677 goto failure;
678 }
679
680 /* Check the first three windows. */
681 for (i = 0; i < 3; ++i) {
682 if (window_translate(&pchip->win[i], addr, &ret)) {
683 goto success;
684 }
685 }
686
687 /* Check the fourth window for DAC disable. */
688 if ((pchip->win[3].wba & 0x80000000000ull) == 0
689 && window_translate(&pchip->win[3], addr, &ret)) {
690 goto success;
691 }
692 } else {
693 /* Double-address cycle. */
694
695 if (addr >= 0x10000000000ull && addr < 0x20000000000ull) {
696 /* Check for the DMA monster window. */
697 if (pchip->ctl & 0x40) {
698 /* See 10.1.4.4; in particular <39:35> is ignored. */
699 make_iommu_tlbe(0, 0x007ffffffffull, &ret);
700 goto success;
701 }
702 }
703
9b2caaf4 704 if (addr >= 0x80000000000ull && addr <= 0xfffffffffffull) {
b83c4db8
RH
705 /* Check the fourth window for DAC enable and window enable. */
706 if ((pchip->win[3].wba & 0x80000000001ull) == 0x80000000001ull) {
707 uint64_t pte_addr;
708
709 pte_addr = pchip->win[3].tba & 0x7ffc00000ull;
710 pte_addr |= (addr & 0xffffe000u) >> 10;
711 if (pte_translate(pte_addr, &ret)) {
712 goto success;
713 }
714 }
715 }
716 }
717
718 failure:
719 ret = (IOMMUTLBEntry) { .perm = IOMMU_NONE };
720 success:
721 return ret;
722}
723
724static const MemoryRegionIOMMUOps typhoon_iommu_ops = {
725 .translate = typhoon_translate_iommu,
726};
727
728static AddressSpace *typhoon_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn)
729{
730 TyphoonState *s = opaque;
731 return &s->pchip.iommu_as;
732}
733
80bb2ff7
RH
734static void typhoon_set_irq(void *opaque, int irq, int level)
735{
736 TyphoonState *s = opaque;
737 uint64_t drir;
738 int i;
739
740 /* Set/Reset the bit in CCHIP.DRIR based on IRQ+LEVEL. */
741 drir = s->cchip.drir;
742 if (level) {
743 drir |= 1ull << irq;
744 } else {
745 drir &= ~(1ull << irq);
746 }
747 s->cchip.drir = drir;
748
749 for (i = 0; i < 4; ++i) {
750 cpu_irq_change(s->cchip.cpu[i], s->cchip.dim[i] & drir);
751 }
752}
753
754static void typhoon_set_isa_irq(void *opaque, int irq, int level)
755{
756 typhoon_set_irq(opaque, 55, level);
757}
758
759static void typhoon_set_timer_irq(void *opaque, int irq, int level)
760{
761 TyphoonState *s = opaque;
762 int i;
763
764 /* Thankfully, the mc146818rtc code doesn't track the IRQ state,
765 and so we don't have to worry about missing interrupts just
766 because we never actually ACK the interrupt. Just ignore any
767 case of the interrupt level going low. */
768 if (level == 0) {
769 return;
770 }
771
772 /* Deliver the interrupt to each CPU, considering each CPU's IIC. */
773 for (i = 0; i < 4; ++i) {
ad601177
AF
774 AlphaCPU *cpu = s->cchip.cpu[i];
775 if (cpu != NULL) {
80bb2ff7
RH
776 uint32_t iic = s->cchip.iic[i];
777
778 /* ??? The verbage in Section 10.2.2.10 isn't 100% clear.
779 Bit 24 is the OverFlow bit, RO, and set when the count
780 decrements past 0. When is OF cleared? My guess is that
781 OF is actually cleared when the IIC is written, and that
782 the ICNT field always decrements. At least, that's an
783 interpretation that makes sense, and "allows the CPU to
784 determine exactly how mant interval timer ticks were
785 skipped". At least within the next 4M ticks... */
786
787 iic = ((iic - 1) & 0x1ffffff) | (iic & 0x1000000);
788 s->cchip.iic[i] = iic;
789
790 if (iic & 0x1000000) {
791 /* Set the ITI bit for this cpu. */
792 s->cchip.misc |= 1 << (i + 4);
793 /* And signal the interrupt. */
c3affe56 794 cpu_interrupt(CPU(cpu), CPU_INTERRUPT_TIMER);
80bb2ff7
RH
795 }
796 }
797 }
798}
799
c781cf96
RH
800static void typhoon_alarm_timer(void *opaque)
801{
802 TyphoonState *s = (TyphoonState *)((uintptr_t)opaque & ~3);
803 int cpu = (uintptr_t)opaque & 3;
804
805 /* Set the ITI bit for this cpu. */
806 s->cchip.misc |= 1 << (cpu + 4);
c3affe56 807 cpu_interrupt(CPU(s->cchip.cpu[cpu]), CPU_INTERRUPT_TIMER);
c781cf96
RH
808}
809
71baa303
HP
810PCIBus *typhoon_init(ram_addr_t ram_size, ISABus **isa_bus,
811 qemu_irq *p_rtc_irq,
ad601177 812 AlphaCPU *cpus[4], pci_map_irq_fn sys_map_irq)
80bb2ff7
RH
813{
814 const uint64_t MB = 1024 * 1024;
815 const uint64_t GB = 1024 * MB;
816 MemoryRegion *addr_space = get_system_memory();
80bb2ff7 817 DeviceState *dev;
80bb2ff7 818 TyphoonState *s;
94dd91d6 819 PCIHostState *phb;
80bb2ff7 820 PCIBus *b;
c781cf96 821 int i;
80bb2ff7 822
94dd91d6 823 dev = qdev_create(NULL, TYPE_TYPHOON_PCI_HOST_BRIDGE);
80bb2ff7
RH
824 qdev_init_nofail(dev);
825
94dd91d6 826 s = TYPHOON_PCI_HOST_BRIDGE(dev);
8558d942 827 phb = PCI_HOST_BRIDGE(dev);
80bb2ff7 828
b83c4db8
RH
829 s->cchip.misc = 0x800000000ull; /* Revision: Typhoon. */
830 s->pchip.win[3].wba = 2; /* Window 3 SG always enabled. */
831
80bb2ff7 832 /* Remember the CPUs so that we can deliver interrupts to them. */
c781cf96 833 for (i = 0; i < 4; i++) {
ad601177
AF
834 AlphaCPU *cpu = cpus[i];
835 s->cchip.cpu[i] = cpu;
836 if (cpu != NULL) {
bc72ad67 837 cpu->alarm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
c781cf96
RH
838 typhoon_alarm_timer,
839 (void *)((uintptr_t)s + i));
840 }
841 }
80bb2ff7
RH
842
843 *p_rtc_irq = *qemu_allocate_irqs(typhoon_set_timer_irq, s, 1);
844
845 /* Main memory region, 0x00.0000.0000. Real hardware supports 32GB,
846 but the address space hole reserved at this point is 8TB. */
64bde0f3 847 memory_region_init_ram(&s->ram_region, OBJECT(s), "ram", ram_size);
c5705a77 848 vmstate_register_ram_global(&s->ram_region);
80bb2ff7
RH
849 memory_region_add_subregion(addr_space, 0, &s->ram_region);
850
851 /* TIGbus, 0x801.0000.0000, 1GB. */
852 /* ??? The TIGbus is used for delivering interrupts, and access to
853 the flash ROM. I'm not sure that we need to implement it at all. */
854
855 /* Pchip0 CSRs, 0x801.8000.0000, 256MB. */
64bde0f3
PB
856 memory_region_init_io(&s->pchip.region, OBJECT(s), &pchip_ops, s, "pchip0",
857 256*MB);
02d6516c
SW
858 memory_region_add_subregion(addr_space, 0x80180000000ULL,
859 &s->pchip.region);
80bb2ff7
RH
860
861 /* Cchip CSRs, 0x801.A000.0000, 256MB. */
64bde0f3
PB
862 memory_region_init_io(&s->cchip.region, OBJECT(s), &cchip_ops, s, "cchip0",
863 256*MB);
02d6516c
SW
864 memory_region_add_subregion(addr_space, 0x801a0000000ULL,
865 &s->cchip.region);
80bb2ff7
RH
866
867 /* Dchip CSRs, 0x801.B000.0000, 256MB. */
64bde0f3
PB
868 memory_region_init_io(&s->dchip_region, OBJECT(s), &dchip_ops, s, "dchip0",
869 256*MB);
02d6516c
SW
870 memory_region_add_subregion(addr_space, 0x801b0000000ULL,
871 &s->dchip_region);
80bb2ff7
RH
872
873 /* Pchip0 PCI memory, 0x800.0000.0000, 4GB. */
64bde0f3 874 memory_region_init(&s->pchip.reg_mem, OBJECT(s), "pci0-mem", 4*GB);
02d6516c
SW
875 memory_region_add_subregion(addr_space, 0x80000000000ULL,
876 &s->pchip.reg_mem);
80bb2ff7
RH
877
878 /* Pchip0 PCI I/O, 0x801.FC00.0000, 32MB. */
3661049f
RH
879 memory_region_init_io(&s->pchip.reg_io, OBJECT(s), &alpha_pci_ignore_ops,
880 NULL, "pci0-io", 32*MB);
02d6516c
SW
881 memory_region_add_subregion(addr_space, 0x801fc000000ULL,
882 &s->pchip.reg_io);
80bb2ff7 883
94dd91d6 884 b = pci_register_bus(dev, "pci",
80bb2ff7 885 typhoon_set_irq, sys_map_irq, s,
056e6bae
RH
886 &s->pchip.reg_mem, &s->pchip.reg_io,
887 0, 64, TYPE_PCI_BUS);
94dd91d6 888 phb->bus = b;
80bb2ff7 889
b83c4db8
RH
890 /* Host memory as seen from the PCI side, via the IOMMU. */
891 memory_region_init_iommu(&s->pchip.iommu, OBJECT(s), &typhoon_iommu_ops,
892 "iommu-typhoon", UINT64_MAX);
893 address_space_init(&s->pchip.iommu_as, &s->pchip.iommu, "pchip0-pci");
894 pci_setup_iommu(b, typhoon_pci_dma_iommu, s);
895
80bb2ff7 896 /* Pchip0 PCI special/interrupt acknowledge, 0x801.F800.0000, 64MB. */
056e6bae
RH
897 memory_region_init_io(&s->pchip.reg_iack, OBJECT(s), &alpha_pci_iack_ops,
898 b, "pci0-iack", 64*MB);
02d6516c
SW
899 memory_region_add_subregion(addr_space, 0x801f8000000ULL,
900 &s->pchip.reg_iack);
80bb2ff7
RH
901
902 /* Pchip0 PCI configuration, 0x801.FE00.0000, 16MB. */
056e6bae
RH
903 memory_region_init_io(&s->pchip.reg_conf, OBJECT(s), &alpha_pci_conf1_ops,
904 b, "pci0-conf", 16*MB);
02d6516c
SW
905 memory_region_add_subregion(addr_space, 0x801fe000000ULL,
906 &s->pchip.reg_conf);
80bb2ff7
RH
907
908 /* For the record, these are the mappings for the second PCI bus.
909 We can get away with not implementing them because we indicate
910 via the Cchip.CSC<PIP> bit that Pchip1 is not present. */
911 /* Pchip1 PCI memory, 0x802.0000.0000, 4GB. */
912 /* Pchip1 CSRs, 0x802.8000.0000, 256MB. */
913 /* Pchip1 PCI special/interrupt acknowledge, 0x802.F800.0000, 64MB. */
914 /* Pchip1 PCI I/O, 0x802.FC00.0000, 32MB. */
915 /* Pchip1 PCI configuration, 0x802.FE00.0000, 16MB. */
916
917 /* Init the ISA bus. */
918 /* ??? Technically there should be a cy82c693ub pci-isa bridge. */
919 {
920 qemu_irq isa_pci_irq, *isa_irqs;
921
056e6bae 922 *isa_bus = isa_bus_new(NULL, &s->pchip.reg_io);
80bb2ff7 923 isa_pci_irq = *qemu_allocate_irqs(typhoon_set_isa_irq, s, 1);
71baa303
HP
924 isa_irqs = i8259_init(*isa_bus, isa_pci_irq);
925 isa_bus_irqs(*isa_bus, isa_irqs);
80bb2ff7
RH
926 }
927
928 return b;
929}
930
931static int typhoon_pcihost_init(SysBusDevice *dev)
932{
933 return 0;
934}
935
999e12bb
AL
936static void typhoon_pcihost_class_init(ObjectClass *klass, void *data)
937{
938 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
939
940 k->init = typhoon_pcihost_init;
941}
942
4240abff 943static const TypeInfo typhoon_pcihost_info = {
94dd91d6 944 .name = TYPE_TYPHOON_PCI_HOST_BRIDGE,
8558d942 945 .parent = TYPE_PCI_HOST_BRIDGE,
39bffca2
AL
946 .instance_size = sizeof(TyphoonState),
947 .class_init = typhoon_pcihost_class_init,
80bb2ff7
RH
948};
949
83f7d43a 950static void typhoon_register_types(void)
80bb2ff7 951{
39bffca2 952 type_register_static(&typhoon_pcihost_info);
80bb2ff7 953}
83f7d43a
AF
954
955type_init(typhoon_register_types)