xref: /openbmc/qemu/target/avr/helper.c (revision 5e437d3c)
1 /*
2  * QEMU AVR CPU helpers
3  *
4  * Copyright (c) 2016-2020 Michael Rolnik
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see
18  * <http://www.gnu.org/licenses/lgpl-2.1.html>
19  */
20 
21 #include "qemu/osdep.h"
22 #include "cpu.h"
23 #include "hw/core/tcg-cpu-ops.h"
24 #include "exec/exec-all.h"
25 #include "exec/address-spaces.h"
26 #include "exec/helper-proto.h"
27 
28 bool avr_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
29 {
30     bool ret = false;
31     CPUClass *cc = CPU_GET_CLASS(cs);
32     AVRCPU *cpu = AVR_CPU(cs);
33     CPUAVRState *env = &cpu->env;
34 
35     if (interrupt_request & CPU_INTERRUPT_RESET) {
36         if (cpu_interrupts_enabled(env)) {
37             cs->exception_index = EXCP_RESET;
38             cc->tcg_ops->do_interrupt(cs);
39 
40             cs->interrupt_request &= ~CPU_INTERRUPT_RESET;
41 
42             ret = true;
43         }
44     }
45     if (interrupt_request & CPU_INTERRUPT_HARD) {
46         if (cpu_interrupts_enabled(env) && env->intsrc != 0) {
47             int index = ctz32(env->intsrc);
48             cs->exception_index = EXCP_INT(index);
49             cc->tcg_ops->do_interrupt(cs);
50 
51             env->intsrc &= env->intsrc - 1; /* clear the interrupt */
52             if (!env->intsrc) {
53                 cs->interrupt_request &= ~CPU_INTERRUPT_HARD;
54             }
55 
56             ret = true;
57         }
58     }
59     return ret;
60 }
61 
62 void avr_cpu_do_interrupt(CPUState *cs)
63 {
64     AVRCPU *cpu = AVR_CPU(cs);
65     CPUAVRState *env = &cpu->env;
66 
67     uint32_t ret = env->pc_w;
68     int vector = 0;
69     int size = avr_feature(env, AVR_FEATURE_JMP_CALL) ? 2 : 1;
70     int base = 0;
71 
72     if (cs->exception_index == EXCP_RESET) {
73         vector = 0;
74     } else if (env->intsrc != 0) {
75         vector = ctz32(env->intsrc) + 1;
76     }
77 
78     if (avr_feature(env, AVR_FEATURE_3_BYTE_PC)) {
79         cpu_stb_data(env, env->sp--, (ret & 0x0000ff));
80         cpu_stb_data(env, env->sp--, (ret & 0x00ff00) >> 8);
81         cpu_stb_data(env, env->sp--, (ret & 0xff0000) >> 16);
82     } else if (avr_feature(env, AVR_FEATURE_2_BYTE_PC)) {
83         cpu_stb_data(env, env->sp--, (ret & 0x0000ff));
84         cpu_stb_data(env, env->sp--, (ret & 0x00ff00) >> 8);
85     } else {
86         cpu_stb_data(env, env->sp--, (ret & 0x0000ff));
87     }
88 
89     env->pc_w = base + vector * size;
90     env->sregI = 0; /* clear Global Interrupt Flag */
91 
92     cs->exception_index = -1;
93 }
94 
95 int avr_cpu_memory_rw_debug(CPUState *cs, vaddr addr, uint8_t *buf,
96                             int len, bool is_write)
97 {
98     return cpu_memory_rw_debug(cs, addr, buf, len, is_write);
99 }
100 
101 hwaddr avr_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
102 {
103     return addr; /* I assume 1:1 address correspondence */
104 }
105 
106 bool avr_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
107                       MMUAccessType access_type, int mmu_idx,
108                       bool probe, uintptr_t retaddr)
109 {
110     int prot = 0;
111     MemTxAttrs attrs = {};
112     uint32_t paddr;
113 
114     address &= TARGET_PAGE_MASK;
115 
116     if (mmu_idx == MMU_CODE_IDX) {
117         /* access to code in flash */
118         paddr = OFFSET_CODE + address;
119         prot = PAGE_READ | PAGE_EXEC;
120         if (paddr + TARGET_PAGE_SIZE > OFFSET_DATA) {
121             error_report("execution left flash memory");
122             abort();
123         }
124     } else if (address < NUMBER_OF_CPU_REGISTERS + NUMBER_OF_IO_REGISTERS) {
125         /*
126          * access to CPU registers, exit and rebuilt this TB to use full access
127          * incase it touches specially handled registers like SREG or SP
128          */
129         AVRCPU *cpu = AVR_CPU(cs);
130         CPUAVRState *env = &cpu->env;
131         env->fullacc = 1;
132         cpu_loop_exit_restore(cs, retaddr);
133     } else {
134         /* access to memory. nothing special */
135         paddr = OFFSET_DATA + address;
136         prot = PAGE_READ | PAGE_WRITE;
137     }
138 
139     tlb_set_page_with_attrs(cs, address, paddr, attrs, prot,
140                             mmu_idx, TARGET_PAGE_SIZE);
141 
142     return true;
143 }
144 
145 /*
146  *  helpers
147  */
148 
149 void helper_sleep(CPUAVRState *env)
150 {
151     CPUState *cs = env_cpu(env);
152 
153     cs->exception_index = EXCP_HLT;
154     cpu_loop_exit(cs);
155 }
156 
157 void helper_unsupported(CPUAVRState *env)
158 {
159     CPUState *cs = env_cpu(env);
160 
161     /*
162      *  I count not find what happens on the real platform, so
163      *  it's EXCP_DEBUG for meanwhile
164      */
165     cs->exception_index = EXCP_DEBUG;
166     if (qemu_loglevel_mask(LOG_UNIMP)) {
167         qemu_log("UNSUPPORTED\n");
168         cpu_dump_state(cs, stderr, 0);
169     }
170     cpu_loop_exit(cs);
171 }
172 
173 void helper_debug(CPUAVRState *env)
174 {
175     CPUState *cs = env_cpu(env);
176 
177     cs->exception_index = EXCP_DEBUG;
178     cpu_loop_exit(cs);
179 }
180 
181 void helper_break(CPUAVRState *env)
182 {
183     CPUState *cs = env_cpu(env);
184 
185     cs->exception_index = EXCP_DEBUG;
186     cpu_loop_exit(cs);
187 }
188 
189 void helper_wdr(CPUAVRState *env)
190 {
191     CPUState *cs = env_cpu(env);
192 
193     /* WD is not implemented yet, placeholder */
194     cs->exception_index = EXCP_DEBUG;
195     cpu_loop_exit(cs);
196 }
197 
198 /*
199  * This function implements IN instruction
200  *
201  * It does the following
202  * a.  if an IO register belongs to CPU, its value is read and returned
203  * b.  otherwise io address is translated to mem address and physical memory
204  *     is read.
205  * c.  it caches the value for sake of SBI, SBIC, SBIS & CBI implementation
206  *
207  */
208 target_ulong helper_inb(CPUAVRState *env, uint32_t port)
209 {
210     target_ulong data = 0;
211 
212     switch (port) {
213     case 0x38: /* RAMPD */
214         data = 0xff & (env->rampD >> 16);
215         break;
216     case 0x39: /* RAMPX */
217         data = 0xff & (env->rampX >> 16);
218         break;
219     case 0x3a: /* RAMPY */
220         data = 0xff & (env->rampY >> 16);
221         break;
222     case 0x3b: /* RAMPZ */
223         data = 0xff & (env->rampZ >> 16);
224         break;
225     case 0x3c: /* EIND */
226         data = 0xff & (env->eind >> 16);
227         break;
228     case 0x3d: /* SPL */
229         data = env->sp & 0x00ff;
230         break;
231     case 0x3e: /* SPH */
232         data = env->sp >> 8;
233         break;
234     case 0x3f: /* SREG */
235         data = cpu_get_sreg(env);
236         break;
237     default:
238         /* not a special register, pass to normal memory access */
239         data = address_space_ldub(&address_space_memory,
240                                   OFFSET_IO_REGISTERS + port,
241                                   MEMTXATTRS_UNSPECIFIED, NULL);
242     }
243 
244     return data;
245 }
246 
247 /*
248  *  This function implements OUT instruction
249  *
250  *  It does the following
251  *  a.  if an IO register belongs to CPU, its value is written into the register
252  *  b.  otherwise io address is translated to mem address and physical memory
253  *      is written.
254  *  c.  it caches the value for sake of SBI, SBIC, SBIS & CBI implementation
255  *
256  */
257 void helper_outb(CPUAVRState *env, uint32_t port, uint32_t data)
258 {
259     data &= 0x000000ff;
260 
261     switch (port) {
262     case 0x38: /* RAMPD */
263         if (avr_feature(env, AVR_FEATURE_RAMPD)) {
264             env->rampD = (data & 0xff) << 16;
265         }
266         break;
267     case 0x39: /* RAMPX */
268         if (avr_feature(env, AVR_FEATURE_RAMPX)) {
269             env->rampX = (data & 0xff) << 16;
270         }
271         break;
272     case 0x3a: /* RAMPY */
273         if (avr_feature(env, AVR_FEATURE_RAMPY)) {
274             env->rampY = (data & 0xff) << 16;
275         }
276         break;
277     case 0x3b: /* RAMPZ */
278         if (avr_feature(env, AVR_FEATURE_RAMPZ)) {
279             env->rampZ = (data & 0xff) << 16;
280         }
281         break;
282     case 0x3c: /* EIDN */
283         env->eind = (data & 0xff) << 16;
284         break;
285     case 0x3d: /* SPL */
286         env->sp = (env->sp & 0xff00) | (data);
287         break;
288     case 0x3e: /* SPH */
289         if (avr_feature(env, AVR_FEATURE_2_BYTE_SP)) {
290             env->sp = (env->sp & 0x00ff) | (data << 8);
291         }
292         break;
293     case 0x3f: /* SREG */
294         cpu_set_sreg(env, data);
295         break;
296     default:
297         /* not a special register, pass to normal memory access */
298         address_space_stb(&address_space_memory, OFFSET_IO_REGISTERS + port,
299                           data, MEMTXATTRS_UNSPECIFIED, NULL);
300     }
301 }
302 
303 /*
304  *  this function implements LD instruction when there is a possibility to read
305  *  from a CPU register
306  */
307 target_ulong helper_fullrd(CPUAVRState *env, uint32_t addr)
308 {
309     uint8_t data;
310 
311     env->fullacc = false;
312 
313     if (addr < NUMBER_OF_CPU_REGISTERS) {
314         /* CPU registers */
315         data = env->r[addr];
316     } else if (addr < NUMBER_OF_CPU_REGISTERS + NUMBER_OF_IO_REGISTERS) {
317         /* IO registers */
318         data = helper_inb(env, addr - NUMBER_OF_CPU_REGISTERS);
319     } else {
320         /* memory */
321         data = address_space_ldub(&address_space_memory, OFFSET_DATA + addr,
322                                   MEMTXATTRS_UNSPECIFIED, NULL);
323     }
324     return data;
325 }
326 
327 /*
328  *  this function implements ST instruction when there is a possibility to write
329  *  into a CPU register
330  */
331 void helper_fullwr(CPUAVRState *env, uint32_t data, uint32_t addr)
332 {
333     env->fullacc = false;
334 
335     /* Following logic assumes this: */
336     assert(OFFSET_CPU_REGISTERS == OFFSET_DATA);
337     assert(OFFSET_IO_REGISTERS == OFFSET_CPU_REGISTERS +
338                                   NUMBER_OF_CPU_REGISTERS);
339 
340     if (addr < NUMBER_OF_CPU_REGISTERS) {
341         /* CPU registers */
342         env->r[addr] = data;
343     } else if (addr < NUMBER_OF_CPU_REGISTERS + NUMBER_OF_IO_REGISTERS) {
344         /* IO registers */
345         helper_outb(env, addr - NUMBER_OF_CPU_REGISTERS, data);
346     } else {
347         /* memory */
348         address_space_stb(&address_space_memory, OFFSET_DATA + addr, data,
349                           MEMTXATTRS_UNSPECIFIED, NULL);
350     }
351 }
352