]> rtime.felk.cvut.cz Git - jailhouse.git/blob - hypervisor/arch/arm/mmio.c
arm: Remove useless warning from arm_mmio_perform_access
[jailhouse.git] / hypervisor / arch / arm / mmio.c
1 /*
2  * Jailhouse, a Linux-based partitioning hypervisor
3  *
4  * Copyright (c) ARM Limited, 2014
5  *
6  * Authors:
7  *  Jean-Philippe Brucker <jean-philippe.brucker@arm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  */
12
13 #include <jailhouse/mmio.h>
14 #include <asm/bitops.h>
15 #include <asm/irqchip.h>
16 #include <asm/processor.h>
17 #include <asm/smp.h>
18 #include <asm/traps.h>
19
20 unsigned int arch_mmio_count_regions(struct cell *cell)
21 {
22         return irqchip_mmio_count_regions(cell) + smp_mmio_regions;
23 }
24
25 /* Taken from the ARM ARM pseudocode for taking a data abort */
26 static void arch_inject_dabt(struct trap_context *ctx, unsigned long addr)
27 {
28         unsigned int lr_offset;
29         unsigned long vbar;
30         bool is_thumb;
31         u32 sctlr, ttbcr;
32
33         arm_read_sysreg(SCTLR_EL1, sctlr);
34         arm_read_sysreg(TTBCR, ttbcr);
35
36         /* Set cpsr */
37         is_thumb = ctx->cpsr & PSR_T_BIT;
38         ctx->cpsr &= ~(PSR_MODE_MASK | PSR_IT_MASK(0xff) | PSR_T_BIT
39                         | PSR_J_BIT | PSR_E_BIT);
40         ctx->cpsr |= (PSR_ABT_MODE | PSR_I_BIT | PSR_A_BIT);
41         if (sctlr & SCTLR_TE_BIT)
42                 ctx->cpsr |= PSR_T_BIT;
43         if (sctlr & SCTLR_EE_BIT)
44                 ctx->cpsr |= PSR_E_BIT;
45
46         lr_offset = (is_thumb ? 4 : 0);
47         arm_write_banked_reg(LR_abt, ctx->pc + lr_offset);
48
49         /* Branch to dabt vector */
50         if (sctlr & SCTLR_V_BIT)
51                 vbar = 0xffff0000;
52         else
53                 arm_read_sysreg(VBAR, vbar);
54         ctx->pc = vbar + 0x10;
55
56         /* Signal a debug fault. DFSR layout depends on the LPAE bit */
57         if (ttbcr >> 31)
58                 arm_write_sysreg(DFSR, (1 << 9) | 0x22);
59         else
60                 arm_write_sysreg(DFSR, 0x2);
61         arm_write_sysreg(DFAR, addr);
62 }
63
64 void arm_mmio_perform_access(unsigned long base, struct mmio_access *mmio)
65 {
66         void *addr = (void *)(base + mmio->address);
67
68         if (mmio->is_write)
69                 switch (mmio->size) {
70                 case 1:
71                         mmio_write8(addr, mmio->value);
72                         return;
73                 case 2:
74                         mmio_write16(addr, mmio->value);
75                         return;
76                 case 4:
77                         mmio_write32(addr, mmio->value);
78                         return;
79                 }
80         else
81                 switch (mmio->size) {
82                 case 1:
83                         mmio->value = mmio_read8(addr);
84                         return;
85                 case 2:
86                         mmio->value = mmio_read16(addr);
87                         return;
88                 case 4:
89                         mmio->value = mmio_read32(addr);
90                         return;
91                 }
92 }
93
94 int arch_handle_dabt(struct trap_context *ctx)
95 {
96         enum mmio_result mmio_result;
97         struct mmio_access mmio;
98         unsigned long hpfar;
99         unsigned long hdfar;
100         /* Decode the syndrome fields */
101         u32 icc         = ESR_ICC(ctx->esr);
102         u32 isv         = icc >> 24;
103         u32 sas         = icc >> 22 & 0x3;
104         u32 sse         = icc >> 21 & 0x1;
105         u32 srt         = icc >> 16 & 0xf;
106         u32 ea          = icc >> 9 & 0x1;
107         u32 cm          = icc >> 8 & 0x1;
108         u32 s1ptw       = icc >> 7 & 0x1;
109         u32 is_write    = icc >> 6 & 0x1;
110         u32 size        = 1 << sas;
111
112         arm_read_sysreg(HPFAR, hpfar);
113         arm_read_sysreg(HDFAR, hdfar);
114         mmio.address = hpfar << 8;
115         mmio.address |= hdfar & 0xfff;
116
117         this_cpu_data()->stats[JAILHOUSE_CPU_STAT_VMEXITS_MMIO]++;
118
119         /*
120          * Invalid instruction syndrome means multiple access or writeback, there
121          * is nothing we can do.
122          */
123         if (!isv || size > sizeof(unsigned long))
124                 goto error_unhandled;
125
126         /* Re-inject abort during page walk, cache maintenance or external */
127         if (s1ptw || ea || cm) {
128                 arch_inject_dabt(ctx, hdfar);
129                 return TRAP_HANDLED;
130         }
131
132         if (is_write) {
133                 /* Load the value to write from the src register */
134                 access_cell_reg(ctx, srt, &mmio.value, true);
135                 if (sse)
136                         mmio.value = sign_extend(mmio.value, 8 * size);
137         } else {
138                 mmio.value = 0;
139         }
140         mmio.is_write = is_write;
141         mmio.size = size;
142
143         mmio_result = mmio_handle_access(&mmio);
144         if (mmio_result == MMIO_ERROR)
145                 return TRAP_FORBIDDEN;
146         if (mmio_result == MMIO_UNHANDLED)
147                 goto error_unhandled;
148
149         /* Put the read value into the dest register */
150         if (!is_write) {
151                 if (sse)
152                         mmio.value = sign_extend(mmio.value, 8 * size);
153                 access_cell_reg(ctx, srt, &mmio.value, false);
154         }
155
156         arch_skip_instruction(ctx);
157         return TRAP_HANDLED;
158
159 error_unhandled:
160         panic_printk("Unhandled data %s at 0x%x(%d)\n",
161                 (is_write ? "write" : "read"), mmio.address, size);
162
163         return TRAP_UNHANDLED;
164 }