root / hw / sun4m_iommu.c @ 2c50e26e
History | View | Annotate | Download (13.1 kB)
1 | 420557e8 | bellard | /*
|
---|---|---|---|
2 | 93c5a32f | Blue Swirl | * QEMU Sun4m iommu emulation
|
3 | 420557e8 | bellard | *
|
4 | 66321a11 | bellard | * Copyright (c) 2003-2005 Fabrice Bellard
|
5 | 5fafdf24 | ths | *
|
6 | 420557e8 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 420557e8 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 420557e8 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 420557e8 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 420557e8 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 420557e8 | bellard | * furnished to do so, subject to the following conditions:
|
12 | 420557e8 | bellard | *
|
13 | 420557e8 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 420557e8 | bellard | * all copies or substantial portions of the Software.
|
15 | 420557e8 | bellard | *
|
16 | 420557e8 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 420557e8 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 420557e8 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 420557e8 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 420557e8 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 420557e8 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 420557e8 | bellard | * THE SOFTWARE.
|
23 | 420557e8 | bellard | */
|
24 | 5f750b2e | Blue Swirl | |
25 | 87ecb68b | pbrook | #include "sun4m.h" |
26 | 5f750b2e | Blue Swirl | #include "sysbus.h" |
27 | 420557e8 | bellard | |
28 | 420557e8 | bellard | /* debug iommu */
|
29 | 420557e8 | bellard | //#define DEBUG_IOMMU
|
30 | 420557e8 | bellard | |
31 | 66321a11 | bellard | #ifdef DEBUG_IOMMU
|
32 | 001faf32 | Blue Swirl | #define DPRINTF(fmt, ...) \
|
33 | 001faf32 | Blue Swirl | do { printf("IOMMU: " fmt , ## __VA_ARGS__); } while (0) |
34 | 66321a11 | bellard | #else
|
35 | 001faf32 | Blue Swirl | #define DPRINTF(fmt, ...)
|
36 | 66321a11 | bellard | #endif
|
37 | 420557e8 | bellard | |
38 | 93c5a32f | Blue Swirl | /*
|
39 | 93c5a32f | Blue Swirl | * I/O MMU used by Sun4m systems
|
40 | 93c5a32f | Blue Swirl | *
|
41 | 93c5a32f | Blue Swirl | * Chipset docs:
|
42 | 93c5a32f | Blue Swirl | * "Sun-4M System Architecture (revision 2.0) by Chuck Narad", 950-1373-01,
|
43 | 93c5a32f | Blue Swirl | * http://mediacast.sun.com/users/Barton808/media/Sun4M_SystemArchitecture_edited2.pdf
|
44 | 93c5a32f | Blue Swirl | */
|
45 | 93c5a32f | Blue Swirl | |
46 | e5e38121 | blueswir1 | #define IOMMU_NREGS (4*4096/4) |
47 | 4e3b1ea1 | bellard | #define IOMMU_CTRL (0x0000 >> 2) |
48 | 420557e8 | bellard | #define IOMMU_CTRL_IMPL 0xf0000000 /* Implementation */ |
49 | 420557e8 | bellard | #define IOMMU_CTRL_VERS 0x0f000000 /* Version */ |
50 | 420557e8 | bellard | #define IOMMU_CTRL_RNGE 0x0000001c /* Mapping RANGE */ |
51 | 420557e8 | bellard | #define IOMMU_RNGE_16MB 0x00000000 /* 0xff000000 -> 0xffffffff */ |
52 | 420557e8 | bellard | #define IOMMU_RNGE_32MB 0x00000004 /* 0xfe000000 -> 0xffffffff */ |
53 | 420557e8 | bellard | #define IOMMU_RNGE_64MB 0x00000008 /* 0xfc000000 -> 0xffffffff */ |
54 | 420557e8 | bellard | #define IOMMU_RNGE_128MB 0x0000000c /* 0xf8000000 -> 0xffffffff */ |
55 | 420557e8 | bellard | #define IOMMU_RNGE_256MB 0x00000010 /* 0xf0000000 -> 0xffffffff */ |
56 | 420557e8 | bellard | #define IOMMU_RNGE_512MB 0x00000014 /* 0xe0000000 -> 0xffffffff */ |
57 | 420557e8 | bellard | #define IOMMU_RNGE_1GB 0x00000018 /* 0xc0000000 -> 0xffffffff */ |
58 | 420557e8 | bellard | #define IOMMU_RNGE_2GB 0x0000001c /* 0x80000000 -> 0xffffffff */ |
59 | 420557e8 | bellard | #define IOMMU_CTRL_ENAB 0x00000001 /* IOMMU Enable */ |
60 | 4e3b1ea1 | bellard | #define IOMMU_CTRL_MASK 0x0000001d |
61 | 4e3b1ea1 | bellard | |
62 | 4e3b1ea1 | bellard | #define IOMMU_BASE (0x0004 >> 2) |
63 | 4e3b1ea1 | bellard | #define IOMMU_BASE_MASK 0x07fffc00 |
64 | 4e3b1ea1 | bellard | |
65 | 4e3b1ea1 | bellard | #define IOMMU_TLBFLUSH (0x0014 >> 2) |
66 | 4e3b1ea1 | bellard | #define IOMMU_TLBFLUSH_MASK 0xffffffff |
67 | 4e3b1ea1 | bellard | |
68 | 4e3b1ea1 | bellard | #define IOMMU_PGFLUSH (0x0018 >> 2) |
69 | 4e3b1ea1 | bellard | #define IOMMU_PGFLUSH_MASK 0xffffffff |
70 | 4e3b1ea1 | bellard | |
71 | 225d4be7 | blueswir1 | #define IOMMU_AFSR (0x1000 >> 2) |
72 | 225d4be7 | blueswir1 | #define IOMMU_AFSR_ERR 0x80000000 /* LE, TO, or BE asserted */ |
73 | 5ad6bb97 | blueswir1 | #define IOMMU_AFSR_LE 0x40000000 /* SBUS reports error after |
74 | 5ad6bb97 | blueswir1 | transaction */
|
75 | 5ad6bb97 | blueswir1 | #define IOMMU_AFSR_TO 0x20000000 /* Write access took more than |
76 | 5ad6bb97 | blueswir1 | 12.8 us. */
|
77 | 5ad6bb97 | blueswir1 | #define IOMMU_AFSR_BE 0x10000000 /* Write access received error |
78 | 5ad6bb97 | blueswir1 | acknowledge */
|
79 | 225d4be7 | blueswir1 | #define IOMMU_AFSR_SIZE 0x0e000000 /* Size of transaction causing error */ |
80 | 225d4be7 | blueswir1 | #define IOMMU_AFSR_S 0x01000000 /* Sparc was in supervisor mode */ |
81 | 5ad6bb97 | blueswir1 | #define IOMMU_AFSR_RESV 0x00800000 /* Reserved, forced to 0x8 by |
82 | 5ad6bb97 | blueswir1 | hardware */
|
83 | 225d4be7 | blueswir1 | #define IOMMU_AFSR_ME 0x00080000 /* Multiple errors occurred */ |
84 | 225d4be7 | blueswir1 | #define IOMMU_AFSR_RD 0x00040000 /* A read operation was in progress */ |
85 | 225d4be7 | blueswir1 | #define IOMMU_AFSR_FAV 0x00020000 /* IOMMU afar has valid contents */ |
86 | c52428fc | blueswir1 | #define IOMMU_AFSR_MASK 0xff0fffff |
87 | 225d4be7 | blueswir1 | |
88 | 225d4be7 | blueswir1 | #define IOMMU_AFAR (0x1004 >> 2) |
89 | 225d4be7 | blueswir1 | |
90 | 7b169687 | blueswir1 | #define IOMMU_AER (0x1008 >> 2) /* Arbiter Enable Register */ |
91 | 7b169687 | blueswir1 | #define IOMMU_AER_EN_P0_ARB 0x00000001 /* MBus master 0x8 (Always 1) */ |
92 | 7b169687 | blueswir1 | #define IOMMU_AER_EN_P1_ARB 0x00000002 /* MBus master 0x9 */ |
93 | 7b169687 | blueswir1 | #define IOMMU_AER_EN_P2_ARB 0x00000004 /* MBus master 0xa */ |
94 | 7b169687 | blueswir1 | #define IOMMU_AER_EN_P3_ARB 0x00000008 /* MBus master 0xb */ |
95 | 7b169687 | blueswir1 | #define IOMMU_AER_EN_0 0x00010000 /* SBus slot 0 */ |
96 | 7b169687 | blueswir1 | #define IOMMU_AER_EN_1 0x00020000 /* SBus slot 1 */ |
97 | 7b169687 | blueswir1 | #define IOMMU_AER_EN_2 0x00040000 /* SBus slot 2 */ |
98 | 7b169687 | blueswir1 | #define IOMMU_AER_EN_3 0x00080000 /* SBus slot 3 */ |
99 | 7b169687 | blueswir1 | #define IOMMU_AER_EN_F 0x00100000 /* SBus on-board */ |
100 | 7b169687 | blueswir1 | #define IOMMU_AER_SBW 0x80000000 /* S-to-M asynchronous writes */ |
101 | 7b169687 | blueswir1 | #define IOMMU_AER_MASK 0x801f000f |
102 | 7b169687 | blueswir1 | |
103 | 4e3b1ea1 | bellard | #define IOMMU_SBCFG0 (0x1010 >> 2) /* SBUS configration per-slot */ |
104 | 4e3b1ea1 | bellard | #define IOMMU_SBCFG1 (0x1014 >> 2) /* SBUS configration per-slot */ |
105 | 4e3b1ea1 | bellard | #define IOMMU_SBCFG2 (0x1018 >> 2) /* SBUS configration per-slot */ |
106 | 4e3b1ea1 | bellard | #define IOMMU_SBCFG3 (0x101c >> 2) /* SBUS configration per-slot */ |
107 | 5ad6bb97 | blueswir1 | #define IOMMU_SBCFG_SAB30 0x00010000 /* Phys-address bit 30 when |
108 | 5ad6bb97 | blueswir1 | bypass enabled */
|
109 | 4e3b1ea1 | bellard | #define IOMMU_SBCFG_BA16 0x00000004 /* Slave supports 16 byte bursts */ |
110 | 4e3b1ea1 | bellard | #define IOMMU_SBCFG_BA8 0x00000002 /* Slave supports 8 byte bursts */ |
111 | 4e3b1ea1 | bellard | #define IOMMU_SBCFG_BYPASS 0x00000001 /* Bypass IOMMU, treat all addresses |
112 | f930d07e | blueswir1 | produced by this device as pure
|
113 | 4e3b1ea1 | bellard | physical. */
|
114 | 4e3b1ea1 | bellard | #define IOMMU_SBCFG_MASK 0x00010003 |
115 | 4e3b1ea1 | bellard | |
116 | 4e3b1ea1 | bellard | #define IOMMU_ARBEN (0x2000 >> 2) /* SBUS arbitration enable */ |
117 | 4e3b1ea1 | bellard | #define IOMMU_ARBEN_MASK 0x001f0000 |
118 | 4e3b1ea1 | bellard | #define IOMMU_MID 0x00000008 |
119 | 420557e8 | bellard | |
120 | e5e38121 | blueswir1 | #define IOMMU_MASK_ID (0x3018 >> 2) /* Mask ID */ |
121 | e5e38121 | blueswir1 | #define IOMMU_MASK_ID_MASK 0x00ffffff |
122 | e5e38121 | blueswir1 | |
123 | e5e38121 | blueswir1 | #define IOMMU_MSII_MASK 0x26000000 /* microSPARC II mask number */ |
124 | e5e38121 | blueswir1 | #define IOMMU_TS_MASK 0x23000000 /* turboSPARC mask number */ |
125 | e5e38121 | blueswir1 | |
126 | 420557e8 | bellard | /* The format of an iopte in the page tables */
|
127 | 498fbd8a | blueswir1 | #define IOPTE_PAGE 0xffffff00 /* Physical page number (PA[35:12]) */ |
128 | 5ad6bb97 | blueswir1 | #define IOPTE_CACHE 0x00000080 /* Cached (in vme IOCACHE or |
129 | 5ad6bb97 | blueswir1 | Viking/MXCC) */
|
130 | 420557e8 | bellard | #define IOPTE_WRITE 0x00000004 /* Writeable */ |
131 | 420557e8 | bellard | #define IOPTE_VALID 0x00000002 /* IOPTE is valid */ |
132 | 420557e8 | bellard | #define IOPTE_WAZ 0x00000001 /* Write as zeros */ |
133 | 420557e8 | bellard | |
134 | 8b0de438 | blueswir1 | #define IOMMU_PAGE_SHIFT 12 |
135 | 8b0de438 | blueswir1 | #define IOMMU_PAGE_SIZE (1 << IOMMU_PAGE_SHIFT) |
136 | 8b0de438 | blueswir1 | #define IOMMU_PAGE_MASK ~(IOMMU_PAGE_SIZE - 1) |
137 | 420557e8 | bellard | |
138 | 420557e8 | bellard | typedef struct IOMMUState { |
139 | 5f750b2e | Blue Swirl | SysBusDevice busdev; |
140 | 66321a11 | bellard | uint32_t regs[IOMMU_NREGS]; |
141 | c227f099 | Anthony Liguori | target_phys_addr_t iostart; |
142 | 7fbfb139 | blueswir1 | uint32_t version; |
143 | ff403da6 | blueswir1 | qemu_irq irq; |
144 | 420557e8 | bellard | } IOMMUState; |
145 | 420557e8 | bellard | |
146 | c227f099 | Anthony Liguori | static uint32_t iommu_mem_readl(void *opaque, target_phys_addr_t addr) |
147 | 420557e8 | bellard | { |
148 | 420557e8 | bellard | IOMMUState *s = opaque; |
149 | c227f099 | Anthony Liguori | target_phys_addr_t saddr; |
150 | ff403da6 | blueswir1 | uint32_t ret; |
151 | 420557e8 | bellard | |
152 | 8da3ff18 | pbrook | saddr = addr >> 2;
|
153 | 420557e8 | bellard | switch (saddr) {
|
154 | 420557e8 | bellard | default:
|
155 | ff403da6 | blueswir1 | ret = s->regs[saddr]; |
156 | ff403da6 | blueswir1 | break;
|
157 | ff403da6 | blueswir1 | case IOMMU_AFAR:
|
158 | ff403da6 | blueswir1 | case IOMMU_AFSR:
|
159 | ff403da6 | blueswir1 | ret = s->regs[saddr]; |
160 | ff403da6 | blueswir1 | qemu_irq_lower(s->irq); |
161 | f930d07e | blueswir1 | break;
|
162 | 420557e8 | bellard | } |
163 | ff403da6 | blueswir1 | DPRINTF("read reg[%d] = %x\n", (int)saddr, ret); |
164 | ff403da6 | blueswir1 | return ret;
|
165 | 420557e8 | bellard | } |
166 | 420557e8 | bellard | |
167 | c227f099 | Anthony Liguori | static void iommu_mem_writel(void *opaque, target_phys_addr_t addr, |
168 | 5ad6bb97 | blueswir1 | uint32_t val) |
169 | 420557e8 | bellard | { |
170 | 420557e8 | bellard | IOMMUState *s = opaque; |
171 | c227f099 | Anthony Liguori | target_phys_addr_t saddr; |
172 | 420557e8 | bellard | |
173 | 8da3ff18 | pbrook | saddr = addr >> 2;
|
174 | 981a2e99 | blueswir1 | DPRINTF("write reg[%d] = %x\n", (int)saddr, val); |
175 | 420557e8 | bellard | switch (saddr) {
|
176 | 4e3b1ea1 | bellard | case IOMMU_CTRL:
|
177 | f930d07e | blueswir1 | switch (val & IOMMU_CTRL_RNGE) {
|
178 | f930d07e | blueswir1 | case IOMMU_RNGE_16MB:
|
179 | f930d07e | blueswir1 | s->iostart = 0xffffffffff000000ULL;
|
180 | f930d07e | blueswir1 | break;
|
181 | f930d07e | blueswir1 | case IOMMU_RNGE_32MB:
|
182 | f930d07e | blueswir1 | s->iostart = 0xfffffffffe000000ULL;
|
183 | f930d07e | blueswir1 | break;
|
184 | f930d07e | blueswir1 | case IOMMU_RNGE_64MB:
|
185 | f930d07e | blueswir1 | s->iostart = 0xfffffffffc000000ULL;
|
186 | f930d07e | blueswir1 | break;
|
187 | f930d07e | blueswir1 | case IOMMU_RNGE_128MB:
|
188 | f930d07e | blueswir1 | s->iostart = 0xfffffffff8000000ULL;
|
189 | f930d07e | blueswir1 | break;
|
190 | f930d07e | blueswir1 | case IOMMU_RNGE_256MB:
|
191 | f930d07e | blueswir1 | s->iostart = 0xfffffffff0000000ULL;
|
192 | f930d07e | blueswir1 | break;
|
193 | f930d07e | blueswir1 | case IOMMU_RNGE_512MB:
|
194 | f930d07e | blueswir1 | s->iostart = 0xffffffffe0000000ULL;
|
195 | f930d07e | blueswir1 | break;
|
196 | f930d07e | blueswir1 | case IOMMU_RNGE_1GB:
|
197 | f930d07e | blueswir1 | s->iostart = 0xffffffffc0000000ULL;
|
198 | f930d07e | blueswir1 | break;
|
199 | f930d07e | blueswir1 | default:
|
200 | f930d07e | blueswir1 | case IOMMU_RNGE_2GB:
|
201 | f930d07e | blueswir1 | s->iostart = 0xffffffff80000000ULL;
|
202 | f930d07e | blueswir1 | break;
|
203 | f930d07e | blueswir1 | } |
204 | f930d07e | blueswir1 | DPRINTF("iostart = " TARGET_FMT_plx "\n", s->iostart); |
205 | 7fbfb139 | blueswir1 | s->regs[saddr] = ((val & IOMMU_CTRL_MASK) | s->version); |
206 | f930d07e | blueswir1 | break;
|
207 | 4e3b1ea1 | bellard | case IOMMU_BASE:
|
208 | f930d07e | blueswir1 | s->regs[saddr] = val & IOMMU_BASE_MASK; |
209 | f930d07e | blueswir1 | break;
|
210 | 4e3b1ea1 | bellard | case IOMMU_TLBFLUSH:
|
211 | f930d07e | blueswir1 | DPRINTF("tlb flush %x\n", val);
|
212 | f930d07e | blueswir1 | s->regs[saddr] = val & IOMMU_TLBFLUSH_MASK; |
213 | f930d07e | blueswir1 | break;
|
214 | 4e3b1ea1 | bellard | case IOMMU_PGFLUSH:
|
215 | f930d07e | blueswir1 | DPRINTF("page flush %x\n", val);
|
216 | f930d07e | blueswir1 | s->regs[saddr] = val & IOMMU_PGFLUSH_MASK; |
217 | f930d07e | blueswir1 | break;
|
218 | ff403da6 | blueswir1 | case IOMMU_AFAR:
|
219 | ff403da6 | blueswir1 | s->regs[saddr] = val; |
220 | ff403da6 | blueswir1 | qemu_irq_lower(s->irq); |
221 | ff403da6 | blueswir1 | break;
|
222 | 7b169687 | blueswir1 | case IOMMU_AER:
|
223 | 7b169687 | blueswir1 | s->regs[saddr] = (val & IOMMU_AER_MASK) | IOMMU_AER_EN_P0_ARB; |
224 | 7b169687 | blueswir1 | break;
|
225 | c52428fc | blueswir1 | case IOMMU_AFSR:
|
226 | c52428fc | blueswir1 | s->regs[saddr] = (val & IOMMU_AFSR_MASK) | IOMMU_AFSR_RESV; |
227 | ff403da6 | blueswir1 | qemu_irq_lower(s->irq); |
228 | c52428fc | blueswir1 | break;
|
229 | 4e3b1ea1 | bellard | case IOMMU_SBCFG0:
|
230 | 4e3b1ea1 | bellard | case IOMMU_SBCFG1:
|
231 | 4e3b1ea1 | bellard | case IOMMU_SBCFG2:
|
232 | 4e3b1ea1 | bellard | case IOMMU_SBCFG3:
|
233 | f930d07e | blueswir1 | s->regs[saddr] = val & IOMMU_SBCFG_MASK; |
234 | f930d07e | blueswir1 | break;
|
235 | 4e3b1ea1 | bellard | case IOMMU_ARBEN:
|
236 | 4e3b1ea1 | bellard | // XXX implement SBus probing: fault when reading unmapped
|
237 | 4e3b1ea1 | bellard | // addresses, fault cause and address stored to MMU/IOMMU
|
238 | f930d07e | blueswir1 | s->regs[saddr] = (val & IOMMU_ARBEN_MASK) | IOMMU_MID; |
239 | f930d07e | blueswir1 | break;
|
240 | e5e38121 | blueswir1 | case IOMMU_MASK_ID:
|
241 | e5e38121 | blueswir1 | s->regs[saddr] |= val & IOMMU_MASK_ID_MASK; |
242 | e5e38121 | blueswir1 | break;
|
243 | 420557e8 | bellard | default:
|
244 | f930d07e | blueswir1 | s->regs[saddr] = val; |
245 | f930d07e | blueswir1 | break;
|
246 | 420557e8 | bellard | } |
247 | 420557e8 | bellard | } |
248 | 420557e8 | bellard | |
249 | d60efc6b | Blue Swirl | static CPUReadMemoryFunc * const iommu_mem_read[3] = { |
250 | 7c560456 | blueswir1 | NULL,
|
251 | 7c560456 | blueswir1 | NULL,
|
252 | 7c560456 | blueswir1 | iommu_mem_readl, |
253 | 420557e8 | bellard | }; |
254 | 420557e8 | bellard | |
255 | d60efc6b | Blue Swirl | static CPUWriteMemoryFunc * const iommu_mem_write[3] = { |
256 | 7c560456 | blueswir1 | NULL,
|
257 | 7c560456 | blueswir1 | NULL,
|
258 | 7c560456 | blueswir1 | iommu_mem_writel, |
259 | 420557e8 | bellard | }; |
260 | 420557e8 | bellard | |
261 | c227f099 | Anthony Liguori | static uint32_t iommu_page_get_flags(IOMMUState *s, target_phys_addr_t addr)
|
262 | 420557e8 | bellard | { |
263 | 5e3b100b | blueswir1 | uint32_t ret; |
264 | c227f099 | Anthony Liguori | target_phys_addr_t iopte; |
265 | 981a2e99 | blueswir1 | #ifdef DEBUG_IOMMU
|
266 | c227f099 | Anthony Liguori | target_phys_addr_t pa = addr; |
267 | 981a2e99 | blueswir1 | #endif
|
268 | 420557e8 | bellard | |
269 | 981a2e99 | blueswir1 | iopte = s->regs[IOMMU_BASE] << 4;
|
270 | 66321a11 | bellard | addr &= ~s->iostart; |
271 | 8b0de438 | blueswir1 | iopte += (addr >> (IOMMU_PAGE_SHIFT - 2)) & ~3; |
272 | 5e3b100b | blueswir1 | cpu_physical_memory_read(iopte, (uint8_t *)&ret, 4);
|
273 | 748e4993 | blueswir1 | tswap32s(&ret); |
274 | 5e3b100b | blueswir1 | DPRINTF("get flags addr " TARGET_FMT_plx " => pte " TARGET_FMT_plx |
275 | 5e3b100b | blueswir1 | ", *pte = %x\n", pa, iopte, ret);
|
276 | 981a2e99 | blueswir1 | |
277 | 981a2e99 | blueswir1 | return ret;
|
278 | a917d384 | pbrook | } |
279 | a917d384 | pbrook | |
280 | c227f099 | Anthony Liguori | static target_phys_addr_t iommu_translate_pa(target_phys_addr_t addr,
|
281 | 5dcb6b91 | blueswir1 | uint32_t pte) |
282 | a917d384 | pbrook | { |
283 | c227f099 | Anthony Liguori | target_phys_addr_t pa; |
284 | 5dcb6b91 | blueswir1 | |
285 | 8b0de438 | blueswir1 | pa = ((pte & IOPTE_PAGE) << 4) + (addr & ~IOMMU_PAGE_MASK);
|
286 | 5dcb6b91 | blueswir1 | DPRINTF("xlate dva " TARGET_FMT_plx " => pa " TARGET_FMT_plx |
287 | f368a3ce | Blue Swirl | " (iopte = %x)\n", addr, pa, pte);
|
288 | a917d384 | pbrook | |
289 | 66321a11 | bellard | return pa;
|
290 | 420557e8 | bellard | } |
291 | 420557e8 | bellard | |
292 | c227f099 | Anthony Liguori | static void iommu_bad_addr(IOMMUState *s, target_phys_addr_t addr, |
293 | 5ad6bb97 | blueswir1 | int is_write)
|
294 | 225d4be7 | blueswir1 | { |
295 | 225d4be7 | blueswir1 | DPRINTF("bad addr " TARGET_FMT_plx "\n", addr); |
296 | 5ad6bb97 | blueswir1 | s->regs[IOMMU_AFSR] = IOMMU_AFSR_ERR | IOMMU_AFSR_LE | IOMMU_AFSR_RESV | |
297 | 225d4be7 | blueswir1 | IOMMU_AFSR_FAV; |
298 | 225d4be7 | blueswir1 | if (!is_write)
|
299 | 225d4be7 | blueswir1 | s->regs[IOMMU_AFSR] |= IOMMU_AFSR_RD; |
300 | 225d4be7 | blueswir1 | s->regs[IOMMU_AFAR] = addr; |
301 | ff403da6 | blueswir1 | qemu_irq_raise(s->irq); |
302 | 225d4be7 | blueswir1 | } |
303 | 225d4be7 | blueswir1 | |
304 | c227f099 | Anthony Liguori | void sparc_iommu_memory_rw(void *opaque, target_phys_addr_t addr, |
305 | 67e999be | bellard | uint8_t *buf, int len, int is_write) |
306 | a917d384 | pbrook | { |
307 | 5dcb6b91 | blueswir1 | int l;
|
308 | 5dcb6b91 | blueswir1 | uint32_t flags; |
309 | c227f099 | Anthony Liguori | target_phys_addr_t page, phys_addr; |
310 | a917d384 | pbrook | |
311 | a917d384 | pbrook | while (len > 0) { |
312 | 8b0de438 | blueswir1 | page = addr & IOMMU_PAGE_MASK; |
313 | 8b0de438 | blueswir1 | l = (page + IOMMU_PAGE_SIZE) - addr; |
314 | a917d384 | pbrook | if (l > len)
|
315 | a917d384 | pbrook | l = len; |
316 | a917d384 | pbrook | flags = iommu_page_get_flags(opaque, page); |
317 | 225d4be7 | blueswir1 | if (!(flags & IOPTE_VALID)) {
|
318 | 225d4be7 | blueswir1 | iommu_bad_addr(opaque, page, is_write); |
319 | a917d384 | pbrook | return;
|
320 | 225d4be7 | blueswir1 | } |
321 | 22548760 | blueswir1 | phys_addr = iommu_translate_pa(addr, flags); |
322 | a917d384 | pbrook | if (is_write) {
|
323 | 225d4be7 | blueswir1 | if (!(flags & IOPTE_WRITE)) {
|
324 | 225d4be7 | blueswir1 | iommu_bad_addr(opaque, page, is_write); |
325 | a917d384 | pbrook | return;
|
326 | 225d4be7 | blueswir1 | } |
327 | a5cdf952 | blueswir1 | cpu_physical_memory_write(phys_addr, buf, l); |
328 | a917d384 | pbrook | } else {
|
329 | a5cdf952 | blueswir1 | cpu_physical_memory_read(phys_addr, buf, l); |
330 | a917d384 | pbrook | } |
331 | a917d384 | pbrook | len -= l; |
332 | a917d384 | pbrook | buf += l; |
333 | a917d384 | pbrook | addr += l; |
334 | a917d384 | pbrook | } |
335 | a917d384 | pbrook | } |
336 | a917d384 | pbrook | |
337 | db3c9e08 | Blue Swirl | static const VMStateDescription vmstate_iommu = { |
338 | db3c9e08 | Blue Swirl | .name ="iommu",
|
339 | db3c9e08 | Blue Swirl | .version_id = 2,
|
340 | db3c9e08 | Blue Swirl | .minimum_version_id = 2,
|
341 | db3c9e08 | Blue Swirl | .minimum_version_id_old = 2,
|
342 | db3c9e08 | Blue Swirl | .fields = (VMStateField []) { |
343 | db3c9e08 | Blue Swirl | VMSTATE_UINT32_ARRAY(regs, IOMMUState, IOMMU_NREGS), |
344 | db3c9e08 | Blue Swirl | VMSTATE_UINT64(iostart, IOMMUState), |
345 | db3c9e08 | Blue Swirl | VMSTATE_END_OF_LIST() |
346 | db3c9e08 | Blue Swirl | } |
347 | db3c9e08 | Blue Swirl | }; |
348 | e80cfcfc | bellard | |
349 | 1a522e8a | Blue Swirl | static void iommu_reset(DeviceState *d) |
350 | e80cfcfc | bellard | { |
351 | 1a522e8a | Blue Swirl | IOMMUState *s = container_of(d, IOMMUState, busdev.qdev); |
352 | e80cfcfc | bellard | |
353 | 66321a11 | bellard | memset(s->regs, 0, IOMMU_NREGS * 4); |
354 | e80cfcfc | bellard | s->iostart = 0;
|
355 | 7fbfb139 | blueswir1 | s->regs[IOMMU_CTRL] = s->version; |
356 | 7fbfb139 | blueswir1 | s->regs[IOMMU_ARBEN] = IOMMU_MID; |
357 | 5ad6bb97 | blueswir1 | s->regs[IOMMU_AFSR] = IOMMU_AFSR_RESV; |
358 | 7b169687 | blueswir1 | s->regs[IOMMU_AER] = IOMMU_AER_EN_P0_ARB | IOMMU_AER_EN_P1_ARB; |
359 | e5e38121 | blueswir1 | s->regs[IOMMU_MASK_ID] = IOMMU_TS_MASK; |
360 | e80cfcfc | bellard | } |
361 | e80cfcfc | bellard | |
362 | 81a322d4 | Gerd Hoffmann | static int iommu_init1(SysBusDevice *dev) |
363 | 5f750b2e | Blue Swirl | { |
364 | 5f750b2e | Blue Swirl | IOMMUState *s = FROM_SYSBUS(IOMMUState, dev); |
365 | 5f750b2e | Blue Swirl | int io;
|
366 | 420557e8 | bellard | |
367 | 5f750b2e | Blue Swirl | sysbus_init_irq(dev, &s->irq); |
368 | 420557e8 | bellard | |
369 | 5f750b2e | Blue Swirl | io = cpu_register_io_memory(iommu_mem_read, iommu_mem_write, s); |
370 | 5f750b2e | Blue Swirl | sysbus_init_mmio(dev, IOMMU_NREGS * sizeof(uint32_t), io);
|
371 | 3b46e624 | ths | |
372 | 81a322d4 | Gerd Hoffmann | return 0; |
373 | 420557e8 | bellard | } |
374 | 5f750b2e | Blue Swirl | |
375 | 5f750b2e | Blue Swirl | static SysBusDeviceInfo iommu_info = {
|
376 | 5f750b2e | Blue Swirl | .init = iommu_init1, |
377 | 5f750b2e | Blue Swirl | .qdev.name = "iommu",
|
378 | 5f750b2e | Blue Swirl | .qdev.size = sizeof(IOMMUState),
|
379 | 1a522e8a | Blue Swirl | .qdev.vmsd = &vmstate_iommu, |
380 | 1a522e8a | Blue Swirl | .qdev.reset = iommu_reset, |
381 | ee6847d1 | Gerd Hoffmann | .qdev.props = (Property[]) { |
382 | 668724a7 | Gerd Hoffmann | DEFINE_PROP_HEX32("version", IOMMUState, version, 0), |
383 | 668724a7 | Gerd Hoffmann | DEFINE_PROP_END_OF_LIST(), |
384 | 5f750b2e | Blue Swirl | } |
385 | 5f750b2e | Blue Swirl | }; |
386 | 5f750b2e | Blue Swirl | |
387 | 5f750b2e | Blue Swirl | static void iommu_register_devices(void) |
388 | 5f750b2e | Blue Swirl | { |
389 | 5f750b2e | Blue Swirl | sysbus_register_withprop(&iommu_info); |
390 | 5f750b2e | Blue Swirl | } |
391 | 5f750b2e | Blue Swirl | |
392 | 5f750b2e | Blue Swirl | device_init(iommu_register_devices) |