xref: /openbmc/qemu/hw/sparc/sun4m_iommu.c (revision 28ae3179fc52d2e4d870b635c4a412aab99759e7)
1ba51ef25SMark Cave-Ayland /*
2ba51ef25SMark Cave-Ayland  * QEMU Sun4m iommu emulation
3ba51ef25SMark Cave-Ayland  *
4ba51ef25SMark Cave-Ayland  * Copyright (c) 2003-2005 Fabrice Bellard
5ba51ef25SMark Cave-Ayland  *
6ba51ef25SMark Cave-Ayland  * Permission is hereby granted, free of charge, to any person obtaining a copy
7ba51ef25SMark Cave-Ayland  * of this software and associated documentation files (the "Software"), to deal
8ba51ef25SMark Cave-Ayland  * in the Software without restriction, including without limitation the rights
9ba51ef25SMark Cave-Ayland  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10ba51ef25SMark Cave-Ayland  * copies of the Software, and to permit persons to whom the Software is
11ba51ef25SMark Cave-Ayland  * furnished to do so, subject to the following conditions:
12ba51ef25SMark Cave-Ayland  *
13ba51ef25SMark Cave-Ayland  * The above copyright notice and this permission notice shall be included in
14ba51ef25SMark Cave-Ayland  * all copies or substantial portions of the Software.
15ba51ef25SMark Cave-Ayland  *
16ba51ef25SMark Cave-Ayland  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17ba51ef25SMark Cave-Ayland  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18ba51ef25SMark Cave-Ayland  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19ba51ef25SMark Cave-Ayland  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20ba51ef25SMark Cave-Ayland  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21ba51ef25SMark Cave-Ayland  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22ba51ef25SMark Cave-Ayland  * THE SOFTWARE.
23ba51ef25SMark Cave-Ayland  */
24ba51ef25SMark Cave-Ayland 
25ba51ef25SMark Cave-Ayland #include "qemu/osdep.h"
2664552b6bSMarkus Armbruster #include "hw/irq.h"
27a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
281527f488SMark Cave-Ayland #include "hw/sparc/sun4m_iommu.h"
29ba51ef25SMark Cave-Ayland #include "hw/sysbus.h"
30d6454270SMarkus Armbruster #include "migration/vmstate.h"
310b8fa32fSMarkus Armbruster #include "qemu/module.h"
32ba51ef25SMark Cave-Ayland #include "exec/address-spaces.h"
33ba51ef25SMark Cave-Ayland #include "trace.h"
34ba51ef25SMark Cave-Ayland 
35ba51ef25SMark Cave-Ayland /*
36ba51ef25SMark Cave-Ayland  * I/O MMU used by Sun4m systems
37ba51ef25SMark Cave-Ayland  *
38ba51ef25SMark Cave-Ayland  * Chipset docs:
39ba51ef25SMark Cave-Ayland  * "Sun-4M System Architecture (revision 2.0) by Chuck Narad", 950-1373-01,
40ba51ef25SMark Cave-Ayland  * http://mediacast.sun.com/users/Barton808/media/Sun4M_SystemArchitecture_edited2.pdf
41ba51ef25SMark Cave-Ayland  */
42ba51ef25SMark Cave-Ayland 
43ba51ef25SMark Cave-Ayland #define IOMMU_CTRL          (0x0000 >> 2)
44ba51ef25SMark Cave-Ayland #define IOMMU_CTRL_IMPL     0xf0000000 /* Implementation */
45ba51ef25SMark Cave-Ayland #define IOMMU_CTRL_VERS     0x0f000000 /* Version */
46ba51ef25SMark Cave-Ayland #define IOMMU_CTRL_RNGE     0x0000001c /* Mapping RANGE */
47ba51ef25SMark Cave-Ayland #define IOMMU_RNGE_16MB     0x00000000 /* 0xff000000 -> 0xffffffff */
48ba51ef25SMark Cave-Ayland #define IOMMU_RNGE_32MB     0x00000004 /* 0xfe000000 -> 0xffffffff */
49ba51ef25SMark Cave-Ayland #define IOMMU_RNGE_64MB     0x00000008 /* 0xfc000000 -> 0xffffffff */
50ba51ef25SMark Cave-Ayland #define IOMMU_RNGE_128MB    0x0000000c /* 0xf8000000 -> 0xffffffff */
51ba51ef25SMark Cave-Ayland #define IOMMU_RNGE_256MB    0x00000010 /* 0xf0000000 -> 0xffffffff */
52ba51ef25SMark Cave-Ayland #define IOMMU_RNGE_512MB    0x00000014 /* 0xe0000000 -> 0xffffffff */
53ba51ef25SMark Cave-Ayland #define IOMMU_RNGE_1GB      0x00000018 /* 0xc0000000 -> 0xffffffff */
54ba51ef25SMark Cave-Ayland #define IOMMU_RNGE_2GB      0x0000001c /* 0x80000000 -> 0xffffffff */
55ba51ef25SMark Cave-Ayland #define IOMMU_CTRL_ENAB     0x00000001 /* IOMMU Enable */
56ba51ef25SMark Cave-Ayland #define IOMMU_CTRL_MASK     0x0000001d
57ba51ef25SMark Cave-Ayland 
58ba51ef25SMark Cave-Ayland #define IOMMU_BASE          (0x0004 >> 2)
59ba51ef25SMark Cave-Ayland #define IOMMU_BASE_MASK     0x07fffc00
60ba51ef25SMark Cave-Ayland 
61ba51ef25SMark Cave-Ayland #define IOMMU_TLBFLUSH      (0x0014 >> 2)
62ba51ef25SMark Cave-Ayland #define IOMMU_TLBFLUSH_MASK 0xffffffff
63ba51ef25SMark Cave-Ayland 
64ba51ef25SMark Cave-Ayland #define IOMMU_PGFLUSH       (0x0018 >> 2)
65ba51ef25SMark Cave-Ayland #define IOMMU_PGFLUSH_MASK  0xffffffff
66ba51ef25SMark Cave-Ayland 
67ba51ef25SMark Cave-Ayland #define IOMMU_AFSR          (0x1000 >> 2)
68ba51ef25SMark Cave-Ayland #define IOMMU_AFSR_ERR      0x80000000 /* LE, TO, or BE asserted */
69ba51ef25SMark Cave-Ayland #define IOMMU_AFSR_LE       0x40000000 /* SBUS reports error after
70ba51ef25SMark Cave-Ayland                                           transaction */
71ba51ef25SMark Cave-Ayland #define IOMMU_AFSR_TO       0x20000000 /* Write access took more than
72ba51ef25SMark Cave-Ayland                                           12.8 us. */
73ba51ef25SMark Cave-Ayland #define IOMMU_AFSR_BE       0x10000000 /* Write access received error
74ba51ef25SMark Cave-Ayland                                           acknowledge */
75ba51ef25SMark Cave-Ayland #define IOMMU_AFSR_SIZE     0x0e000000 /* Size of transaction causing error */
76ba51ef25SMark Cave-Ayland #define IOMMU_AFSR_S        0x01000000 /* Sparc was in supervisor mode */
77ba51ef25SMark Cave-Ayland #define IOMMU_AFSR_RESV     0x00800000 /* Reserved, forced to 0x8 by
78ba51ef25SMark Cave-Ayland                                           hardware */
79ba51ef25SMark Cave-Ayland #define IOMMU_AFSR_ME       0x00080000 /* Multiple errors occurred */
80ba51ef25SMark Cave-Ayland #define IOMMU_AFSR_RD       0x00040000 /* A read operation was in progress */
81ba51ef25SMark Cave-Ayland #define IOMMU_AFSR_FAV      0x00020000 /* IOMMU afar has valid contents */
82ba51ef25SMark Cave-Ayland #define IOMMU_AFSR_MASK     0xff0fffff
83ba51ef25SMark Cave-Ayland 
84ba51ef25SMark Cave-Ayland #define IOMMU_AFAR          (0x1004 >> 2)
85ba51ef25SMark Cave-Ayland 
86ba51ef25SMark Cave-Ayland #define IOMMU_AER           (0x1008 >> 2) /* Arbiter Enable Register */
87ba51ef25SMark Cave-Ayland #define IOMMU_AER_EN_P0_ARB 0x00000001    /* MBus master 0x8 (Always 1) */
88ba51ef25SMark Cave-Ayland #define IOMMU_AER_EN_P1_ARB 0x00000002    /* MBus master 0x9 */
89ba51ef25SMark Cave-Ayland #define IOMMU_AER_EN_P2_ARB 0x00000004    /* MBus master 0xa */
90ba51ef25SMark Cave-Ayland #define IOMMU_AER_EN_P3_ARB 0x00000008    /* MBus master 0xb */
91ba51ef25SMark Cave-Ayland #define IOMMU_AER_EN_0      0x00010000    /* SBus slot 0 */
92ba51ef25SMark Cave-Ayland #define IOMMU_AER_EN_1      0x00020000    /* SBus slot 1 */
93ba51ef25SMark Cave-Ayland #define IOMMU_AER_EN_2      0x00040000    /* SBus slot 2 */
94ba51ef25SMark Cave-Ayland #define IOMMU_AER_EN_3      0x00080000    /* SBus slot 3 */
95ba51ef25SMark Cave-Ayland #define IOMMU_AER_EN_F      0x00100000    /* SBus on-board */
96ba51ef25SMark Cave-Ayland #define IOMMU_AER_SBW       0x80000000    /* S-to-M asynchronous writes */
97ba51ef25SMark Cave-Ayland #define IOMMU_AER_MASK      0x801f000f
98ba51ef25SMark Cave-Ayland 
998b81968cSMichael Tokarev #define IOMMU_SBCFG0        (0x1010 >> 2) /* SBUS configuration per-slot */
1008b81968cSMichael Tokarev #define IOMMU_SBCFG1        (0x1014 >> 2) /* SBUS configuration per-slot */
1018b81968cSMichael Tokarev #define IOMMU_SBCFG2        (0x1018 >> 2) /* SBUS configuration per-slot */
1028b81968cSMichael Tokarev #define IOMMU_SBCFG3        (0x101c >> 2) /* SBUS configuration per-slot */
103ba51ef25SMark Cave-Ayland #define IOMMU_SBCFG_SAB30   0x00010000 /* Phys-address bit 30 when
104ba51ef25SMark Cave-Ayland                                           bypass enabled */
105ba51ef25SMark Cave-Ayland #define IOMMU_SBCFG_BA16    0x00000004 /* Slave supports 16 byte bursts */
106ba51ef25SMark Cave-Ayland #define IOMMU_SBCFG_BA8     0x00000002 /* Slave supports 8 byte bursts */
107ba51ef25SMark Cave-Ayland #define IOMMU_SBCFG_BYPASS  0x00000001 /* Bypass IOMMU, treat all addresses
108ba51ef25SMark Cave-Ayland                                           produced by this device as pure
109ba51ef25SMark Cave-Ayland                                           physical. */
110ba51ef25SMark Cave-Ayland #define IOMMU_SBCFG_MASK    0x00010003
111ba51ef25SMark Cave-Ayland 
112ba51ef25SMark Cave-Ayland #define IOMMU_ARBEN         (0x2000 >> 2) /* SBUS arbitration enable */
113ba51ef25SMark Cave-Ayland #define IOMMU_ARBEN_MASK    0x001f0000
114ba51ef25SMark Cave-Ayland #define IOMMU_MID           0x00000008
115ba51ef25SMark Cave-Ayland 
116ba51ef25SMark Cave-Ayland #define IOMMU_MASK_ID       (0x3018 >> 2) /* Mask ID */
117ba51ef25SMark Cave-Ayland #define IOMMU_MASK_ID_MASK  0x00ffffff
118ba51ef25SMark Cave-Ayland 
119ba51ef25SMark Cave-Ayland #define IOMMU_MSII_MASK     0x26000000 /* microSPARC II mask number */
120ba51ef25SMark Cave-Ayland #define IOMMU_TS_MASK       0x23000000 /* turboSPARC mask number */
121ba51ef25SMark Cave-Ayland 
122ba51ef25SMark Cave-Ayland /* The format of an iopte in the page tables */
123ba51ef25SMark Cave-Ayland #define IOPTE_PAGE          0xffffff00 /* Physical page number (PA[35:12]) */
124ba51ef25SMark Cave-Ayland #define IOPTE_CACHE         0x00000080 /* Cached (in vme IOCACHE or
125ba51ef25SMark Cave-Ayland                                           Viking/MXCC) */
126ba51ef25SMark Cave-Ayland #define IOPTE_WRITE         0x00000004 /* Writable */
127ba51ef25SMark Cave-Ayland #define IOPTE_VALID         0x00000002 /* IOPTE is valid */
128ba51ef25SMark Cave-Ayland #define IOPTE_WAZ           0x00000001 /* Write as zeros */
129ba51ef25SMark Cave-Ayland 
130ba51ef25SMark Cave-Ayland #define IOMMU_PAGE_SHIFT    12
131ba51ef25SMark Cave-Ayland #define IOMMU_PAGE_SIZE     (1 << IOMMU_PAGE_SHIFT)
132ba51ef25SMark Cave-Ayland #define IOMMU_PAGE_MASK     (~(IOMMU_PAGE_SIZE - 1))
133ba51ef25SMark Cave-Ayland 
iommu_mem_read(void * opaque,hwaddr addr,unsigned size)134ba51ef25SMark Cave-Ayland static uint64_t iommu_mem_read(void *opaque, hwaddr addr,
135ba51ef25SMark Cave-Ayland                                unsigned size)
136ba51ef25SMark Cave-Ayland {
137ba51ef25SMark Cave-Ayland     IOMMUState *s = opaque;
138ba51ef25SMark Cave-Ayland     hwaddr saddr;
139ba51ef25SMark Cave-Ayland     uint32_t ret;
140ba51ef25SMark Cave-Ayland 
141ba51ef25SMark Cave-Ayland     saddr = addr >> 2;
142ba51ef25SMark Cave-Ayland     switch (saddr) {
143ba51ef25SMark Cave-Ayland     default:
144ba51ef25SMark Cave-Ayland         ret = s->regs[saddr];
145ba51ef25SMark Cave-Ayland         break;
146ba51ef25SMark Cave-Ayland     case IOMMU_AFAR:
147ba51ef25SMark Cave-Ayland     case IOMMU_AFSR:
148ba51ef25SMark Cave-Ayland         ret = s->regs[saddr];
149ba51ef25SMark Cave-Ayland         qemu_irq_lower(s->irq);
150ba51ef25SMark Cave-Ayland         break;
151ba51ef25SMark Cave-Ayland     }
152ba51ef25SMark Cave-Ayland     trace_sun4m_iommu_mem_readl(saddr, ret);
153ba51ef25SMark Cave-Ayland     return ret;
154ba51ef25SMark Cave-Ayland }
155ba51ef25SMark Cave-Ayland 
iommu_mem_write(void * opaque,hwaddr addr,uint64_t val,unsigned size)156ba51ef25SMark Cave-Ayland static void iommu_mem_write(void *opaque, hwaddr addr,
157ba51ef25SMark Cave-Ayland                             uint64_t val, unsigned size)
158ba51ef25SMark Cave-Ayland {
159ba51ef25SMark Cave-Ayland     IOMMUState *s = opaque;
160ba51ef25SMark Cave-Ayland     hwaddr saddr;
161ba51ef25SMark Cave-Ayland 
162ba51ef25SMark Cave-Ayland     saddr = addr >> 2;
163ba51ef25SMark Cave-Ayland     trace_sun4m_iommu_mem_writel(saddr, val);
164ba51ef25SMark Cave-Ayland     switch (saddr) {
165ba51ef25SMark Cave-Ayland     case IOMMU_CTRL:
166ba51ef25SMark Cave-Ayland         switch (val & IOMMU_CTRL_RNGE) {
167ba51ef25SMark Cave-Ayland         case IOMMU_RNGE_16MB:
168ba51ef25SMark Cave-Ayland             s->iostart = 0xffffffffff000000ULL;
169ba51ef25SMark Cave-Ayland             break;
170ba51ef25SMark Cave-Ayland         case IOMMU_RNGE_32MB:
171ba51ef25SMark Cave-Ayland             s->iostart = 0xfffffffffe000000ULL;
172ba51ef25SMark Cave-Ayland             break;
173ba51ef25SMark Cave-Ayland         case IOMMU_RNGE_64MB:
174ba51ef25SMark Cave-Ayland             s->iostart = 0xfffffffffc000000ULL;
175ba51ef25SMark Cave-Ayland             break;
176ba51ef25SMark Cave-Ayland         case IOMMU_RNGE_128MB:
177ba51ef25SMark Cave-Ayland             s->iostart = 0xfffffffff8000000ULL;
178ba51ef25SMark Cave-Ayland             break;
179ba51ef25SMark Cave-Ayland         case IOMMU_RNGE_256MB:
180ba51ef25SMark Cave-Ayland             s->iostart = 0xfffffffff0000000ULL;
181ba51ef25SMark Cave-Ayland             break;
182ba51ef25SMark Cave-Ayland         case IOMMU_RNGE_512MB:
183ba51ef25SMark Cave-Ayland             s->iostart = 0xffffffffe0000000ULL;
184ba51ef25SMark Cave-Ayland             break;
185ba51ef25SMark Cave-Ayland         case IOMMU_RNGE_1GB:
186ba51ef25SMark Cave-Ayland             s->iostart = 0xffffffffc0000000ULL;
187ba51ef25SMark Cave-Ayland             break;
188ba51ef25SMark Cave-Ayland         default:
189ba51ef25SMark Cave-Ayland         case IOMMU_RNGE_2GB:
190ba51ef25SMark Cave-Ayland             s->iostart = 0xffffffff80000000ULL;
191ba51ef25SMark Cave-Ayland             break;
192ba51ef25SMark Cave-Ayland         }
193ba51ef25SMark Cave-Ayland         trace_sun4m_iommu_mem_writel_ctrl(s->iostart);
194ba51ef25SMark Cave-Ayland         s->regs[saddr] = ((val & IOMMU_CTRL_MASK) | s->version);
195ba51ef25SMark Cave-Ayland         break;
196ba51ef25SMark Cave-Ayland     case IOMMU_BASE:
197ba51ef25SMark Cave-Ayland         s->regs[saddr] = val & IOMMU_BASE_MASK;
198ba51ef25SMark Cave-Ayland         break;
199ba51ef25SMark Cave-Ayland     case IOMMU_TLBFLUSH:
200ba51ef25SMark Cave-Ayland         trace_sun4m_iommu_mem_writel_tlbflush(val);
201ba51ef25SMark Cave-Ayland         s->regs[saddr] = val & IOMMU_TLBFLUSH_MASK;
202ba51ef25SMark Cave-Ayland         break;
203ba51ef25SMark Cave-Ayland     case IOMMU_PGFLUSH:
204ba51ef25SMark Cave-Ayland         trace_sun4m_iommu_mem_writel_pgflush(val);
205ba51ef25SMark Cave-Ayland         s->regs[saddr] = val & IOMMU_PGFLUSH_MASK;
206ba51ef25SMark Cave-Ayland         break;
207ba51ef25SMark Cave-Ayland     case IOMMU_AFAR:
208ba51ef25SMark Cave-Ayland         s->regs[saddr] = val;
209ba51ef25SMark Cave-Ayland         qemu_irq_lower(s->irq);
210ba51ef25SMark Cave-Ayland         break;
211ba51ef25SMark Cave-Ayland     case IOMMU_AER:
212ba51ef25SMark Cave-Ayland         s->regs[saddr] = (val & IOMMU_AER_MASK) | IOMMU_AER_EN_P0_ARB;
213ba51ef25SMark Cave-Ayland         break;
214ba51ef25SMark Cave-Ayland     case IOMMU_AFSR:
215ba51ef25SMark Cave-Ayland         s->regs[saddr] = (val & IOMMU_AFSR_MASK) | IOMMU_AFSR_RESV;
216ba51ef25SMark Cave-Ayland         qemu_irq_lower(s->irq);
217ba51ef25SMark Cave-Ayland         break;
218ba51ef25SMark Cave-Ayland     case IOMMU_SBCFG0:
219ba51ef25SMark Cave-Ayland     case IOMMU_SBCFG1:
220ba51ef25SMark Cave-Ayland     case IOMMU_SBCFG2:
221ba51ef25SMark Cave-Ayland     case IOMMU_SBCFG3:
222ba51ef25SMark Cave-Ayland         s->regs[saddr] = val & IOMMU_SBCFG_MASK;
223ba51ef25SMark Cave-Ayland         break;
224ba51ef25SMark Cave-Ayland     case IOMMU_ARBEN:
225ba51ef25SMark Cave-Ayland         /* XXX implement SBus probing: fault when reading unmapped
226ba51ef25SMark Cave-Ayland            addresses, fault cause and address stored to MMU/IOMMU */
227ba51ef25SMark Cave-Ayland         s->regs[saddr] = (val & IOMMU_ARBEN_MASK) | IOMMU_MID;
228ba51ef25SMark Cave-Ayland         break;
229ba51ef25SMark Cave-Ayland     case IOMMU_MASK_ID:
230ba51ef25SMark Cave-Ayland         s->regs[saddr] |= val & IOMMU_MASK_ID_MASK;
231ba51ef25SMark Cave-Ayland         break;
232ba51ef25SMark Cave-Ayland     default:
233ba51ef25SMark Cave-Ayland         s->regs[saddr] = val;
234ba51ef25SMark Cave-Ayland         break;
235ba51ef25SMark Cave-Ayland     }
236ba51ef25SMark Cave-Ayland }
237ba51ef25SMark Cave-Ayland 
238ba51ef25SMark Cave-Ayland static const MemoryRegionOps iommu_mem_ops = {
239ba51ef25SMark Cave-Ayland     .read = iommu_mem_read,
240ba51ef25SMark Cave-Ayland     .write = iommu_mem_write,
241ba51ef25SMark Cave-Ayland     .endianness = DEVICE_NATIVE_ENDIAN,
242ba51ef25SMark Cave-Ayland     .valid = {
243ba51ef25SMark Cave-Ayland         .min_access_size = 4,
244ba51ef25SMark Cave-Ayland         .max_access_size = 4,
245ba51ef25SMark Cave-Ayland     },
246ba51ef25SMark Cave-Ayland };
247ba51ef25SMark Cave-Ayland 
iommu_page_get_flags(IOMMUState * s,hwaddr addr)248ba51ef25SMark Cave-Ayland static uint32_t iommu_page_get_flags(IOMMUState *s, hwaddr addr)
249ba51ef25SMark Cave-Ayland {
250ba51ef25SMark Cave-Ayland     uint32_t ret;
251ba51ef25SMark Cave-Ayland     hwaddr iopte;
252ba51ef25SMark Cave-Ayland     hwaddr pa = addr;
253ba51ef25SMark Cave-Ayland 
254ba51ef25SMark Cave-Ayland     iopte = s->regs[IOMMU_BASE] << 4;
255ba51ef25SMark Cave-Ayland     addr &= ~s->iostart;
256ba51ef25SMark Cave-Ayland     iopte += (addr >> (IOMMU_PAGE_SHIFT - 2)) & ~3;
257ba51ef25SMark Cave-Ayland     ret = address_space_ldl_be(&address_space_memory, iopte,
258ba51ef25SMark Cave-Ayland                                MEMTXATTRS_UNSPECIFIED, NULL);
259ba51ef25SMark Cave-Ayland     trace_sun4m_iommu_page_get_flags(pa, iopte, ret);
260ba51ef25SMark Cave-Ayland     return ret;
261ba51ef25SMark Cave-Ayland }
262ba51ef25SMark Cave-Ayland 
iommu_translate_pa(hwaddr addr,uint32_t pte)263ba51ef25SMark Cave-Ayland static hwaddr iommu_translate_pa(hwaddr addr,
264ba51ef25SMark Cave-Ayland                                              uint32_t pte)
265ba51ef25SMark Cave-Ayland {
266ba51ef25SMark Cave-Ayland     hwaddr pa;
267ba51ef25SMark Cave-Ayland 
268ba51ef25SMark Cave-Ayland     pa = ((pte & IOPTE_PAGE) << 4) + (addr & ~IOMMU_PAGE_MASK);
269ba51ef25SMark Cave-Ayland     trace_sun4m_iommu_translate_pa(addr, pa, pte);
270ba51ef25SMark Cave-Ayland     return pa;
271ba51ef25SMark Cave-Ayland }
272ba51ef25SMark Cave-Ayland 
iommu_bad_addr(IOMMUState * s,hwaddr addr,int is_write)273ba51ef25SMark Cave-Ayland static void iommu_bad_addr(IOMMUState *s, hwaddr addr,
274ba51ef25SMark Cave-Ayland                            int is_write)
275ba51ef25SMark Cave-Ayland {
276ba51ef25SMark Cave-Ayland     trace_sun4m_iommu_bad_addr(addr);
277ba51ef25SMark Cave-Ayland     s->regs[IOMMU_AFSR] = IOMMU_AFSR_ERR | IOMMU_AFSR_LE | IOMMU_AFSR_RESV |
278ba51ef25SMark Cave-Ayland         IOMMU_AFSR_FAV;
279ba51ef25SMark Cave-Ayland     if (!is_write) {
280ba51ef25SMark Cave-Ayland         s->regs[IOMMU_AFSR] |= IOMMU_AFSR_RD;
281ba51ef25SMark Cave-Ayland     }
282ba51ef25SMark Cave-Ayland     s->regs[IOMMU_AFAR] = addr;
283ba51ef25SMark Cave-Ayland     qemu_irq_raise(s->irq);
284ba51ef25SMark Cave-Ayland }
285ba51ef25SMark Cave-Ayland 
286ba51ef25SMark Cave-Ayland /* Called from RCU critical section */
sun4m_translate_iommu(IOMMUMemoryRegion * iommu,hwaddr addr,IOMMUAccessFlags flags,int iommu_idx)287ba51ef25SMark Cave-Ayland static IOMMUTLBEntry sun4m_translate_iommu(IOMMUMemoryRegion *iommu,
288ba51ef25SMark Cave-Ayland                                            hwaddr addr,
2892c91bcf2SPeter Maydell                                            IOMMUAccessFlags flags,
2902c91bcf2SPeter Maydell                                            int iommu_idx)
291ba51ef25SMark Cave-Ayland {
292ba51ef25SMark Cave-Ayland     IOMMUState *is = container_of(iommu, IOMMUState, iommu);
293ba51ef25SMark Cave-Ayland     hwaddr page, pa;
294ba51ef25SMark Cave-Ayland     int is_write = (flags & IOMMU_WO) ? 1 : 0;
295ba51ef25SMark Cave-Ayland     uint32_t pte;
296ba51ef25SMark Cave-Ayland     IOMMUTLBEntry ret = {
297ba51ef25SMark Cave-Ayland         .target_as = &address_space_memory,
298ba51ef25SMark Cave-Ayland         .iova = 0,
299ba51ef25SMark Cave-Ayland         .translated_addr = 0,
300ba51ef25SMark Cave-Ayland         .addr_mask = ~(hwaddr)0,
301ba51ef25SMark Cave-Ayland         .perm = IOMMU_NONE,
302ba51ef25SMark Cave-Ayland     };
303ba51ef25SMark Cave-Ayland 
304ba51ef25SMark Cave-Ayland     page = addr & IOMMU_PAGE_MASK;
305ba51ef25SMark Cave-Ayland     pte = iommu_page_get_flags(is, page);
306ba51ef25SMark Cave-Ayland     if (!(pte & IOPTE_VALID)) {
307ba51ef25SMark Cave-Ayland         iommu_bad_addr(is, page, is_write);
308ba51ef25SMark Cave-Ayland         return ret;
309ba51ef25SMark Cave-Ayland     }
310ba51ef25SMark Cave-Ayland 
311ba51ef25SMark Cave-Ayland     pa = iommu_translate_pa(addr, pte);
312ba51ef25SMark Cave-Ayland     if (is_write && !(pte & IOPTE_WRITE)) {
313ba51ef25SMark Cave-Ayland         iommu_bad_addr(is, page, is_write);
314ba51ef25SMark Cave-Ayland         return ret;
315ba51ef25SMark Cave-Ayland     }
316ba51ef25SMark Cave-Ayland 
317ba51ef25SMark Cave-Ayland     if (pte & IOPTE_WRITE) {
318ba51ef25SMark Cave-Ayland         ret.perm = IOMMU_RW;
319ba51ef25SMark Cave-Ayland     } else {
320ba51ef25SMark Cave-Ayland         ret.perm = IOMMU_RO;
321ba51ef25SMark Cave-Ayland     }
322ba51ef25SMark Cave-Ayland 
323ba51ef25SMark Cave-Ayland     ret.iova = page;
324ba51ef25SMark Cave-Ayland     ret.translated_addr = pa;
325ba51ef25SMark Cave-Ayland     ret.addr_mask = ~IOMMU_PAGE_MASK;
326ba51ef25SMark Cave-Ayland 
327ba51ef25SMark Cave-Ayland     return ret;
328ba51ef25SMark Cave-Ayland }
329ba51ef25SMark Cave-Ayland 
330ba51ef25SMark Cave-Ayland static const VMStateDescription vmstate_iommu = {
331ba51ef25SMark Cave-Ayland     .name = "iommu",
332ba51ef25SMark Cave-Ayland     .version_id = 2,
333ba51ef25SMark Cave-Ayland     .minimum_version_id = 2,
334735e354aSRichard Henderson     .fields = (const VMStateField[]) {
335ba51ef25SMark Cave-Ayland         VMSTATE_UINT32_ARRAY(regs, IOMMUState, IOMMU_NREGS),
336ba51ef25SMark Cave-Ayland         VMSTATE_UINT64(iostart, IOMMUState),
337ba51ef25SMark Cave-Ayland         VMSTATE_END_OF_LIST()
338ba51ef25SMark Cave-Ayland     }
339ba51ef25SMark Cave-Ayland };
340ba51ef25SMark Cave-Ayland 
iommu_reset(DeviceState * d)341ba51ef25SMark Cave-Ayland static void iommu_reset(DeviceState *d)
342ba51ef25SMark Cave-Ayland {
343ba51ef25SMark Cave-Ayland     IOMMUState *s = SUN4M_IOMMU(d);
344ba51ef25SMark Cave-Ayland 
345ba51ef25SMark Cave-Ayland     memset(s->regs, 0, IOMMU_NREGS * 4);
346ba51ef25SMark Cave-Ayland     s->iostart = 0;
347ba51ef25SMark Cave-Ayland     s->regs[IOMMU_CTRL] = s->version;
348ba51ef25SMark Cave-Ayland     s->regs[IOMMU_ARBEN] = IOMMU_MID;
349ba51ef25SMark Cave-Ayland     s->regs[IOMMU_AFSR] = IOMMU_AFSR_RESV;
350ba51ef25SMark Cave-Ayland     s->regs[IOMMU_AER] = IOMMU_AER_EN_P0_ARB | IOMMU_AER_EN_P1_ARB;
351ba51ef25SMark Cave-Ayland     s->regs[IOMMU_MASK_ID] = IOMMU_TS_MASK;
352ba51ef25SMark Cave-Ayland }
353ba51ef25SMark Cave-Ayland 
iommu_init(Object * obj)354ba51ef25SMark Cave-Ayland static void iommu_init(Object *obj)
355ba51ef25SMark Cave-Ayland {
356ba51ef25SMark Cave-Ayland     IOMMUState *s = SUN4M_IOMMU(obj);
357ba51ef25SMark Cave-Ayland     SysBusDevice *dev = SYS_BUS_DEVICE(obj);
358ba51ef25SMark Cave-Ayland 
359ba51ef25SMark Cave-Ayland     memory_region_init_iommu(&s->iommu, sizeof(s->iommu),
360ba51ef25SMark Cave-Ayland                              TYPE_SUN4M_IOMMU_MEMORY_REGION, OBJECT(dev),
361ba51ef25SMark Cave-Ayland                              "iommu-sun4m", UINT64_MAX);
362ba51ef25SMark Cave-Ayland     address_space_init(&s->iommu_as, MEMORY_REGION(&s->iommu), "iommu-as");
363ba51ef25SMark Cave-Ayland 
364ba51ef25SMark Cave-Ayland     sysbus_init_irq(dev, &s->irq);
365ba51ef25SMark Cave-Ayland 
366ba51ef25SMark Cave-Ayland     memory_region_init_io(&s->iomem, obj, &iommu_mem_ops, s, "iommu",
367ba51ef25SMark Cave-Ayland                           IOMMU_NREGS * sizeof(uint32_t));
368ba51ef25SMark Cave-Ayland     sysbus_init_mmio(dev, &s->iomem);
369ba51ef25SMark Cave-Ayland }
370ba51ef25SMark Cave-Ayland 
371ba51ef25SMark Cave-Ayland static Property iommu_properties[] = {
372ba51ef25SMark Cave-Ayland     DEFINE_PROP_UINT32("version", IOMMUState, version, 0),
373ba51ef25SMark Cave-Ayland     DEFINE_PROP_END_OF_LIST(),
374ba51ef25SMark Cave-Ayland };
375ba51ef25SMark Cave-Ayland 
iommu_class_init(ObjectClass * klass,void * data)376ba51ef25SMark Cave-Ayland static void iommu_class_init(ObjectClass *klass, void *data)
377ba51ef25SMark Cave-Ayland {
378ba51ef25SMark Cave-Ayland     DeviceClass *dc = DEVICE_CLASS(klass);
379ba51ef25SMark Cave-Ayland 
380*e3d08143SPeter Maydell     device_class_set_legacy_reset(dc, iommu_reset);
381ba51ef25SMark Cave-Ayland     dc->vmsd = &vmstate_iommu;
3824f67d30bSMarc-André Lureau     device_class_set_props(dc, iommu_properties);
383ba51ef25SMark Cave-Ayland }
384ba51ef25SMark Cave-Ayland 
385ba51ef25SMark Cave-Ayland static const TypeInfo iommu_info = {
386ba51ef25SMark Cave-Ayland     .name          = TYPE_SUN4M_IOMMU,
387ba51ef25SMark Cave-Ayland     .parent        = TYPE_SYS_BUS_DEVICE,
388ba51ef25SMark Cave-Ayland     .instance_size = sizeof(IOMMUState),
389ba51ef25SMark Cave-Ayland     .instance_init = iommu_init,
390ba51ef25SMark Cave-Ayland     .class_init    = iommu_class_init,
391ba51ef25SMark Cave-Ayland };
392ba51ef25SMark Cave-Ayland 
sun4m_iommu_memory_region_class_init(ObjectClass * klass,void * data)393ba51ef25SMark Cave-Ayland static void sun4m_iommu_memory_region_class_init(ObjectClass *klass, void *data)
394ba51ef25SMark Cave-Ayland {
395ba51ef25SMark Cave-Ayland     IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_CLASS(klass);
396ba51ef25SMark Cave-Ayland 
397ba51ef25SMark Cave-Ayland     imrc->translate = sun4m_translate_iommu;
398ba51ef25SMark Cave-Ayland }
399ba51ef25SMark Cave-Ayland 
400ba51ef25SMark Cave-Ayland static const TypeInfo sun4m_iommu_memory_region_info = {
401ba51ef25SMark Cave-Ayland     .parent = TYPE_IOMMU_MEMORY_REGION,
402ba51ef25SMark Cave-Ayland     .name = TYPE_SUN4M_IOMMU_MEMORY_REGION,
403ba51ef25SMark Cave-Ayland     .class_init = sun4m_iommu_memory_region_class_init,
404ba51ef25SMark Cave-Ayland };
405ba51ef25SMark Cave-Ayland 
iommu_register_types(void)406ba51ef25SMark Cave-Ayland static void iommu_register_types(void)
407ba51ef25SMark Cave-Ayland {
408ba51ef25SMark Cave-Ayland     type_register_static(&iommu_info);
409ba51ef25SMark Cave-Ayland     type_register_static(&sun4m_iommu_memory_region_info);
410ba51ef25SMark Cave-Ayland }
411ba51ef25SMark Cave-Ayland 
412ba51ef25SMark Cave-Ayland type_init(iommu_register_types)
413