xref: /qemu/hw/alpha/dp264.c (revision fc524567087c2537b5103cdfc1d41e4f442892b6)
1 /*
2  * QEMU Alpha DP264/CLIPPER hardware system emulator.
3  *
4  * Choose CLIPPER IRQ mappings over, say, DP264, MONET, or WEBBRICK
5  * variants because CLIPPER doesn't have an SMC669 SuperIO controller
6  * that we need to emulate as well.
7  */
8 
9 #include "qemu/osdep.h"
10 #include "cpu.h"
11 #include "exec/target_page.h"
12 #include "elf.h"
13 #include "hw/loader.h"
14 #include "alpha_sys.h"
15 #include "qemu/error-report.h"
16 #include "hw/rtc/mc146818rtc.h"
17 #include "hw/ide/pci.h"
18 #include "hw/isa/superio.h"
19 #include "net/net.h"
20 #include "qemu/cutils.h"
21 #include "qemu/datadir.h"
22 
cpu_alpha_superpage_to_phys(void * opaque,uint64_t addr)23 static uint64_t cpu_alpha_superpage_to_phys(void *opaque, uint64_t addr)
24 {
25     if (((addr >> 41) & 3) == 2) {
26         addr &= 0xffffffffffull;
27     }
28     return addr;
29 }
30 
31 /* Note that there are at least 3 viewpoints of IRQ numbers on Alpha systems.
32     (0) The dev_irq_n lines into the cpu, which we totally ignore,
33     (1) The DRIR lines in the typhoon chipset,
34     (2) The "vector" aka mangled interrupt number reported by SRM PALcode,
35     (3) The interrupt number assigned by the kernel.
36    The following function is concerned with (1) only.  */
37 
clipper_pci_map_irq(PCIDevice * d,int irq_num)38 static int clipper_pci_map_irq(PCIDevice *d, int irq_num)
39 {
40     int slot = d->devfn >> 3;
41 
42     assert(irq_num >= 0 && irq_num <= 3);
43 
44     return (slot + 1) * 4 + irq_num;
45 }
46 
clipper_init(MachineState * machine)47 static void clipper_init(MachineState *machine)
48 {
49     ram_addr_t ram_size = machine->ram_size;
50     const char *kernel_filename = machine->kernel_filename;
51     const char *kernel_cmdline = machine->kernel_cmdline;
52     const char *initrd_filename = machine->initrd_filename;
53     MachineClass *mc = MACHINE_GET_CLASS(machine);
54     AlphaCPU *cpus[4];
55     PCIBus *pci_bus;
56     PCIDevice *pci_dev;
57     DeviceState *i82378_dev;
58     ISABus *isa_bus;
59     qemu_irq rtc_irq;
60     qemu_irq isa_irq;
61     long size, i;
62     char *palcode_filename;
63     uint64_t palcode_entry;
64     uint64_t kernel_entry, kernel_low;
65     unsigned int smp_cpus = machine->smp.cpus;
66 
67     /* Create up to 4 cpus.  */
68     memset(cpus, 0, sizeof(cpus));
69     for (i = 0; i < smp_cpus; ++i) {
70         cpus[i] = ALPHA_CPU(cpu_create(machine->cpu_type));
71     }
72 
73     /*
74      * arg0 -> memory size
75      * arg1 -> kernel entry point
76      * arg2 -> config word
77      *
78      * Config word: bits 0-5 -> ncpus
79      *              bit  6   -> nographics option (for HWRPB CTB)
80      *
81      * See init_hwrpb() in the PALcode.
82      */
83     cpus[0]->env.trap_arg0 = ram_size;
84     cpus[0]->env.trap_arg1 = 0;
85     cpus[0]->env.trap_arg2 = smp_cpus | (!machine->enable_graphics << 6);
86 
87     /*
88      * Init the chipset.  Because we're using CLIPPER IRQ mappings,
89      * the minimum PCI device IdSel is 1.
90      */
91     pci_bus = typhoon_init(machine->ram, &isa_irq, &rtc_irq, cpus,
92                            clipper_pci_map_irq, PCI_DEVFN(1, 0));
93 
94     /*
95      * Init the PCI -> ISA bridge.
96      *
97      * Technically, PCI-based Alphas shipped with one of three different
98      * PCI-ISA bridges:
99      *
100      * - Intel i82378 SIO
101      * - Cypress CY82c693UB
102      * - ALI M1533
103      *
104      * (An Intel i82375 PCI-EISA bridge was also used on some models.)
105      *
106      * For simplicity, we model an i82378 here, even though it wouldn't
107      * have been on any Tsunami/Typhoon systems; it's close enough, and
108      * we don't want to deal with modelling the CY82c693UB (which has
109      * incompatible edge/level control registers, plus other peripherals
110      * like IDE and USB) or the M1533 (which also has IDE and USB).
111      *
112      * Importantly, we need to provide a PCI device node for it, otherwise
113      * some operating systems won't notice there's an ISA bus to configure.
114      */
115     i82378_dev = DEVICE(pci_create_simple(pci_bus, PCI_DEVFN(7, 0), "i82378"));
116     isa_bus = ISA_BUS(qdev_get_child_bus(i82378_dev, "isa.0"));
117 
118     /* Connect the ISA PIC to the Typhoon IRQ used for ISA interrupts. */
119     qdev_connect_gpio_out(i82378_dev, 0, isa_irq);
120 
121     /* Since we have an SRM-compatible PALcode, use the SRM epoch.  */
122     mc146818_rtc_init(isa_bus, 1900, rtc_irq);
123 
124     /* VGA setup.  Don't bother loading the bios.  */
125     pci_vga_init(pci_bus);
126 
127     /* Network setup.  e1000 is good enough, failing Tulip support.  */
128     pci_init_nic_devices(pci_bus, mc->default_nic);
129 
130     /* Super I/O */
131     isa_create_simple(isa_bus, TYPE_SMC37C669_SUPERIO);
132 
133     /* IDE disk setup.  */
134     pci_dev = pci_create_simple(pci_bus, -1, "cmd646-ide");
135     pci_ide_create_devs(pci_dev);
136 
137     /* Load PALcode.  Given that this is not "real" cpu palcode,
138        but one explicitly written for the emulation, we might as
139        well load it directly from and ELF image.  */
140     palcode_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS,
141                                       machine->firmware ?: "palcode-clipper");
142     if (palcode_filename == NULL) {
143         error_report("no palcode provided");
144         exit(1);
145     }
146     size = load_elf(palcode_filename, NULL, cpu_alpha_superpage_to_phys,
147                     NULL, &palcode_entry, NULL, NULL, NULL,
148                     ELFDATA2LSB, EM_ALPHA, 0, 0);
149     if (size < 0) {
150         error_report("could not load palcode '%s'", palcode_filename);
151         exit(1);
152     }
153     g_free(palcode_filename);
154 
155     /* Start all cpus at the PALcode RESET entry point.  */
156     for (i = 0; i < smp_cpus; ++i) {
157         cpus[i]->env.pc = palcode_entry;
158         cpus[i]->env.palbr = palcode_entry;
159     }
160 
161     /* Load a kernel.  */
162     if (kernel_filename) {
163         uint64_t param_offset;
164 
165         size = load_elf(kernel_filename, NULL, cpu_alpha_superpage_to_phys,
166                         NULL, &kernel_entry, &kernel_low, NULL, NULL,
167                         ELFDATA2LSB, EM_ALPHA, 0, 0);
168         if (size < 0) {
169             error_report("could not load kernel '%s'", kernel_filename);
170             exit(1);
171         }
172 
173         cpus[0]->env.trap_arg1 = kernel_entry;
174 
175         param_offset = kernel_low - 0x6000;
176 
177         if (kernel_cmdline) {
178             pstrcpy_targphys("cmdline", param_offset, 0x100, kernel_cmdline);
179         }
180 
181         if (initrd_filename) {
182             long initrd_base;
183             int64_t initrd_size;
184 
185             initrd_size = get_image_size(initrd_filename);
186             if (initrd_size < 0) {
187                 error_report("could not load initial ram disk '%s'",
188                              initrd_filename);
189                 exit(1);
190             }
191 
192             /* Put the initrd image as high in memory as possible.  */
193             initrd_base = (ram_size - initrd_size) & TARGET_PAGE_MASK;
194             load_image_targphys(initrd_filename, initrd_base,
195                                 ram_size - initrd_base);
196 
197             address_space_stq(&address_space_memory, param_offset + 0x100,
198                               initrd_base + 0xfffffc0000000000ULL,
199                               MEMTXATTRS_UNSPECIFIED,
200                               NULL);
201             address_space_stq(&address_space_memory, param_offset + 0x108,
202                               initrd_size, MEMTXATTRS_UNSPECIFIED, NULL);
203         }
204     }
205 }
206 
clipper_machine_init(MachineClass * mc)207 static void clipper_machine_init(MachineClass *mc)
208 {
209     mc->desc = "Alpha DP264/CLIPPER";
210     mc->init = clipper_init;
211     mc->block_default_type = IF_IDE;
212     mc->max_cpus = 4;
213     mc->is_default = true;
214     mc->default_cpu_type = ALPHA_CPU_TYPE_NAME("ev67");
215     mc->default_ram_id = "ram";
216     mc->default_nic = "e1000";
217 }
218 
219 DEFINE_MACHINE("clipper", clipper_machine_init)
220