xref: /openbmc/qemu/hw/alpha/dp264.c (revision d27532e4)
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 "elf.h"
12 #include "hw/loader.h"
13 #include "alpha_sys.h"
14 #include "qemu/error-report.h"
15 #include "hw/rtc/mc146818rtc.h"
16 #include "hw/ide/pci.h"
17 #include "hw/isa/superio.h"
18 #include "net/net.h"
19 #include "qemu/cutils.h"
20 #include "qemu/datadir.h"
21 
22 static uint64_t cpu_alpha_superpage_to_phys(void *opaque, uint64_t addr)
23 {
24     if (((addr >> 41) & 3) == 2) {
25         addr &= 0xffffffffffull;
26     }
27     return addr;
28 }
29 
30 /* Note that there are at least 3 viewpoints of IRQ numbers on Alpha systems.
31     (0) The dev_irq_n lines into the cpu, which we totally ignore,
32     (1) The DRIR lines in the typhoon chipset,
33     (2) The "vector" aka mangled interrupt number reported by SRM PALcode,
34     (3) The interrupt number assigned by the kernel.
35    The following function is concerned with (1) only.  */
36 
37 static int clipper_pci_map_irq(PCIDevice *d, int irq_num)
38 {
39     int slot = d->devfn >> 3;
40 
41     assert(irq_num >= 0 && irq_num <= 3);
42 
43     return (slot + 1) * 4 + irq_num;
44 }
45 
46 static void clipper_init(MachineState *machine)
47 {
48     ram_addr_t ram_size = machine->ram_size;
49     const char *kernel_filename = machine->kernel_filename;
50     const char *kernel_cmdline = machine->kernel_cmdline;
51     const char *initrd_filename = machine->initrd_filename;
52     AlphaCPU *cpus[4];
53     PCIBus *pci_bus;
54     PCIDevice *pci_dev;
55     DeviceState *i82378_dev;
56     ISABus *isa_bus;
57     qemu_irq rtc_irq;
58     qemu_irq isa_irq;
59     long size, i;
60     char *palcode_filename;
61     uint64_t palcode_entry;
62     uint64_t kernel_entry, kernel_low;
63     unsigned int smp_cpus = machine->smp.cpus;
64 
65     /* Create up to 4 cpus.  */
66     memset(cpus, 0, sizeof(cpus));
67     for (i = 0; i < smp_cpus; ++i) {
68         cpus[i] = ALPHA_CPU(cpu_create(machine->cpu_type));
69     }
70 
71     /*
72      * arg0 -> memory size
73      * arg1 -> kernel entry point
74      * arg2 -> config word
75      *
76      * Config word: bits 0-5 -> ncpus
77      *              bit  6   -> nographics option (for HWRPB CTB)
78      *
79      * See init_hwrpb() in the PALcode.
80      */
81     cpus[0]->env.trap_arg0 = ram_size;
82     cpus[0]->env.trap_arg1 = 0;
83     cpus[0]->env.trap_arg2 = smp_cpus | (!machine->enable_graphics << 6);
84 
85     /*
86      * Init the chipset.  Because we're using CLIPPER IRQ mappings,
87      * the minimum PCI device IdSel is 1.
88      */
89     pci_bus = typhoon_init(machine->ram, &isa_irq, &rtc_irq, cpus,
90                            clipper_pci_map_irq, PCI_DEVFN(1, 0));
91 
92     /*
93      * Init the PCI -> ISA bridge.
94      *
95      * Technically, PCI-based Alphas shipped with one of three different
96      * PCI-ISA bridges:
97      *
98      * - Intel i82378 SIO
99      * - Cypress CY82c693UB
100      * - ALI M1533
101      *
102      * (An Intel i82375 PCI-EISA bridge was also used on some models.)
103      *
104      * For simplicity, we model an i82378 here, even though it wouldn't
105      * have been on any Tsunami/Typhoon systems; it's close enough, and
106      * we don't want to deal with modelling the CY82c693UB (which has
107      * incompatible edge/level control registers, plus other peripherals
108      * like IDE and USB) or the M1533 (which also has IDE and USB).
109      *
110      * Importantly, we need to provide a PCI device node for it, otherwise
111      * some operating systems won't notice there's an ISA bus to configure.
112      */
113     i82378_dev = DEVICE(pci_create_simple(pci_bus, PCI_DEVFN(7, 0), "i82378"));
114     isa_bus = ISA_BUS(qdev_get_child_bus(i82378_dev, "isa.0"));
115 
116     /* Connect the ISA PIC to the Typhoon IRQ used for ISA interrupts. */
117     qdev_connect_gpio_out(i82378_dev, 0, isa_irq);
118 
119     /* Since we have an SRM-compatible PALcode, use the SRM epoch.  */
120     mc146818_rtc_init(isa_bus, 1900, rtc_irq);
121 
122     /* VGA setup.  Don't bother loading the bios.  */
123     pci_vga_init(pci_bus);
124 
125     /* Network setup.  e1000 is good enough, failing Tulip support.  */
126     for (i = 0; i < nb_nics; i++) {
127         pci_nic_init_nofail(&nd_table[i], pci_bus, "e1000", NULL);
128     }
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                     0, 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                         0, 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 
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 }
217 
218 DEFINE_MACHINE("clipper", clipper_machine_init)
219