2 * QEMU Sparc32 DMA controller emulation
4 * Copyright (c) 2006 Fabrice Bellard
7 * 2010-Feb-14 Artyom Tarasenko : reworked irq generation
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #include "qemu/osdep.h"
30 #include "hw/qdev-properties.h"
31 #include "hw/sparc/sparc32_dma.h"
32 #include "hw/sparc/sun4m_iommu.h"
33 #include "hw/sysbus.h"
34 #include "migration/vmstate.h"
35 #include "sysemu/dma.h"
36 #include "qapi/error.h"
37 #include "qemu/module.h"
41 * This is the DMA controller part of chip STP2000 (Master I/O), also
42 * produced as NCR89C100. See
43 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C100.txt
45 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/DMA2.txt
48 #define DMA_SIZE (4 * sizeof(uint32_t))
49 /* We need the mask, because one instance of the device is not page
50 aligned (ledma, start address 0x0010) */
51 #define DMA_MASK (DMA_SIZE - 1)
52 /* OBP says 0x20 bytes for ledma, the extras are aliased to espdma */
53 #define DMA_ETH_SIZE (8 * sizeof(uint32_t))
54 #define DMA_MAX_REG_OFFSET (2 * DMA_SIZE - 1)
56 #define DMA_VER 0xa0000000
58 #define DMA_INTREN 0x10
59 #define DMA_WRITE_MEM 0x100
61 #define DMA_LOADED 0x04000000
62 #define DMA_DRAIN_FIFO 0x40
63 #define DMA_RESET 0x80
65 /* XXX SCSI and ethernet should have different read-only bit masks */
66 #define DMA_CSR_RO_MASK 0xfe000007
73 /* Note: on sparc, the lance 16 bit bus is swapped */
74 void ledma_memory_read(void *opaque
, hwaddr addr
,
75 uint8_t *buf
, int len
, int do_bswap
)
77 DMADeviceState
*s
= opaque
;
78 IOMMUState
*is
= (IOMMUState
*)s
->iommu
;
81 addr
|= s
->dmaregs
[3];
82 trace_ledma_memory_read(addr
, len
);
84 dma_memory_read(&is
->iommu_as
, addr
, buf
, len
);
88 dma_memory_read(&is
->iommu_as
, addr
, buf
, len
);
89 for(i
= 0; i
< len
; i
+= 2) {
90 bswap16s((uint16_t *)(buf
+ i
));
95 void ledma_memory_write(void *opaque
, hwaddr addr
,
96 uint8_t *buf
, int len
, int do_bswap
)
98 DMADeviceState
*s
= opaque
;
99 IOMMUState
*is
= (IOMMUState
*)s
->iommu
;
101 uint16_t tmp_buf
[32];
103 addr
|= s
->dmaregs
[3];
104 trace_ledma_memory_write(addr
, len
);
106 dma_memory_write(&is
->iommu_as
, addr
, buf
, len
);
112 if (l
> sizeof(tmp_buf
))
114 for(i
= 0; i
< l
; i
+= 2) {
115 tmp_buf
[i
>> 1] = bswap16(*(uint16_t *)(buf
+ i
));
117 dma_memory_write(&is
->iommu_as
, addr
, tmp_buf
, l
);
125 static void dma_set_irq(void *opaque
, int irq
, int level
)
127 DMADeviceState
*s
= opaque
;
129 s
->dmaregs
[0] |= DMA_INTR
;
130 if (s
->dmaregs
[0] & DMA_INTREN
) {
131 trace_sparc32_dma_set_irq_raise();
132 qemu_irq_raise(s
->irq
);
135 if (s
->dmaregs
[0] & DMA_INTR
) {
136 s
->dmaregs
[0] &= ~DMA_INTR
;
137 if (s
->dmaregs
[0] & DMA_INTREN
) {
138 trace_sparc32_dma_set_irq_lower();
139 qemu_irq_lower(s
->irq
);
145 void espdma_memory_read(void *opaque
, uint8_t *buf
, int len
)
147 DMADeviceState
*s
= opaque
;
148 IOMMUState
*is
= (IOMMUState
*)s
->iommu
;
150 trace_espdma_memory_read(s
->dmaregs
[1], len
);
151 dma_memory_read(&is
->iommu_as
, s
->dmaregs
[1], buf
, len
);
152 s
->dmaregs
[1] += len
;
155 void espdma_memory_write(void *opaque
, uint8_t *buf
, int len
)
157 DMADeviceState
*s
= opaque
;
158 IOMMUState
*is
= (IOMMUState
*)s
->iommu
;
160 trace_espdma_memory_write(s
->dmaregs
[1], len
);
161 dma_memory_write(&is
->iommu_as
, s
->dmaregs
[1], buf
, len
);
162 s
->dmaregs
[1] += len
;
165 static uint64_t dma_mem_read(void *opaque
, hwaddr addr
,
168 DMADeviceState
*s
= opaque
;
171 saddr
= (addr
& DMA_MASK
) >> 2;
172 trace_sparc32_dma_mem_readl(addr
, s
->dmaregs
[saddr
]);
173 return s
->dmaregs
[saddr
];
176 static void dma_mem_write(void *opaque
, hwaddr addr
,
177 uint64_t val
, unsigned size
)
179 DMADeviceState
*s
= opaque
;
182 saddr
= (addr
& DMA_MASK
) >> 2;
183 trace_sparc32_dma_mem_writel(addr
, s
->dmaregs
[saddr
], val
);
186 if (val
& DMA_INTREN
) {
187 if (s
->dmaregs
[0] & DMA_INTR
) {
188 trace_sparc32_dma_set_irq_raise();
189 qemu_irq_raise(s
->irq
);
192 if (s
->dmaregs
[0] & (DMA_INTR
| DMA_INTREN
)) {
193 trace_sparc32_dma_set_irq_lower();
194 qemu_irq_lower(s
->irq
);
197 if (val
& DMA_RESET
) {
198 qemu_irq_raise(s
->gpio
[GPIO_RESET
]);
199 qemu_irq_lower(s
->gpio
[GPIO_RESET
]);
200 } else if (val
& DMA_DRAIN_FIFO
) {
201 val
&= ~DMA_DRAIN_FIFO
;
203 val
= DMA_DRAIN_FIFO
;
205 if (val
& DMA_EN
&& !(s
->dmaregs
[0] & DMA_EN
)) {
206 trace_sparc32_dma_enable_raise();
207 qemu_irq_raise(s
->gpio
[GPIO_DMA
]);
208 } else if (!(val
& DMA_EN
) && !!(s
->dmaregs
[0] & DMA_EN
)) {
209 trace_sparc32_dma_enable_lower();
210 qemu_irq_lower(s
->gpio
[GPIO_DMA
]);
213 val
&= ~DMA_CSR_RO_MASK
;
215 s
->dmaregs
[0] = (s
->dmaregs
[0] & DMA_CSR_RO_MASK
) | val
;
218 s
->dmaregs
[0] |= DMA_LOADED
;
221 s
->dmaregs
[saddr
] = val
;
226 static const MemoryRegionOps dma_mem_ops
= {
227 .read
= dma_mem_read
,
228 .write
= dma_mem_write
,
229 .endianness
= DEVICE_NATIVE_ENDIAN
,
231 .min_access_size
= 4,
232 .max_access_size
= 4,
236 static void sparc32_dma_device_reset(DeviceState
*d
)
238 DMADeviceState
*s
= SPARC32_DMA_DEVICE(d
);
240 memset(s
->dmaregs
, 0, DMA_SIZE
);
241 s
->dmaregs
[0] = DMA_VER
;
244 static const VMStateDescription vmstate_sparc32_dma_device
= {
245 .name
="sparc32_dma",
247 .minimum_version_id
= 2,
248 .fields
= (VMStateField
[]) {
249 VMSTATE_UINT32_ARRAY(dmaregs
, DMADeviceState
, DMA_REGS
),
250 VMSTATE_END_OF_LIST()
254 static void sparc32_dma_device_init(Object
*obj
)
256 DeviceState
*dev
= DEVICE(obj
);
257 DMADeviceState
*s
= SPARC32_DMA_DEVICE(obj
);
258 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
260 sysbus_init_irq(sbd
, &s
->irq
);
262 sysbus_init_mmio(sbd
, &s
->iomem
);
264 object_property_add_link(OBJECT(dev
), "iommu", TYPE_SUN4M_IOMMU
,
265 (Object
**) &s
->iommu
,
266 qdev_prop_allow_set_link_before_realize
,
269 qdev_init_gpio_in(dev
, dma_set_irq
, 1);
270 qdev_init_gpio_out(dev
, s
->gpio
, 2);
273 static void sparc32_dma_device_class_init(ObjectClass
*klass
, void *data
)
275 DeviceClass
*dc
= DEVICE_CLASS(klass
);
277 dc
->reset
= sparc32_dma_device_reset
;
278 dc
->vmsd
= &vmstate_sparc32_dma_device
;
281 static const TypeInfo sparc32_dma_device_info
= {
282 .name
= TYPE_SPARC32_DMA_DEVICE
,
283 .parent
= TYPE_SYS_BUS_DEVICE
,
285 .instance_size
= sizeof(DMADeviceState
),
286 .instance_init
= sparc32_dma_device_init
,
287 .class_init
= sparc32_dma_device_class_init
,
290 static void sparc32_espdma_device_init(Object
*obj
)
292 DMADeviceState
*s
= SPARC32_DMA_DEVICE(obj
);
293 ESPDMADeviceState
*es
= SPARC32_ESPDMA_DEVICE(obj
);
295 memory_region_init_io(&s
->iomem
, OBJECT(s
), &dma_mem_ops
, s
,
296 "espdma-mmio", DMA_SIZE
);
298 object_initialize_child(obj
, "esp", &es
->esp
, TYPE_ESP
);
301 static void sparc32_espdma_device_realize(DeviceState
*dev
, Error
**errp
)
303 ESPDMADeviceState
*es
= SPARC32_ESPDMA_DEVICE(dev
);
304 SysBusESPState
*sysbus
= ESP(&es
->esp
);
305 ESPState
*esp
= &sysbus
->esp
;
307 esp
->dma_memory_read
= espdma_memory_read
;
308 esp
->dma_memory_write
= espdma_memory_write
;
309 esp
->dma_opaque
= SPARC32_DMA_DEVICE(dev
);
310 sysbus
->it_shift
= 2;
311 esp
->dma_enabled
= 1;
312 sysbus_realize(SYS_BUS_DEVICE(sysbus
), &error_fatal
);
315 static void sparc32_espdma_device_class_init(ObjectClass
*klass
, void *data
)
317 DeviceClass
*dc
= DEVICE_CLASS(klass
);
319 dc
->realize
= sparc32_espdma_device_realize
;
322 static const TypeInfo sparc32_espdma_device_info
= {
323 .name
= TYPE_SPARC32_ESPDMA_DEVICE
,
324 .parent
= TYPE_SPARC32_DMA_DEVICE
,
325 .instance_size
= sizeof(ESPDMADeviceState
),
326 .instance_init
= sparc32_espdma_device_init
,
327 .class_init
= sparc32_espdma_device_class_init
,
330 static void sparc32_ledma_device_init(Object
*obj
)
332 DMADeviceState
*s
= SPARC32_DMA_DEVICE(obj
);
333 LEDMADeviceState
*ls
= SPARC32_LEDMA_DEVICE(obj
);
335 memory_region_init_io(&s
->iomem
, OBJECT(s
), &dma_mem_ops
, s
,
336 "ledma-mmio", DMA_SIZE
);
338 object_initialize_child(obj
, "lance", &ls
->lance
, TYPE_LANCE
);
341 static void sparc32_ledma_device_realize(DeviceState
*dev
, Error
**errp
)
343 LEDMADeviceState
*s
= SPARC32_LEDMA_DEVICE(dev
);
344 SysBusPCNetState
*lance
= SYSBUS_PCNET(&s
->lance
);
346 object_property_set_link(OBJECT(lance
), "dma", OBJECT(dev
), &error_abort
);
347 sysbus_realize(SYS_BUS_DEVICE(lance
), &error_fatal
);
350 static void sparc32_ledma_device_class_init(ObjectClass
*klass
, void *data
)
352 DeviceClass
*dc
= DEVICE_CLASS(klass
);
354 dc
->realize
= sparc32_ledma_device_realize
;
357 static const TypeInfo sparc32_ledma_device_info
= {
358 .name
= TYPE_SPARC32_LEDMA_DEVICE
,
359 .parent
= TYPE_SPARC32_DMA_DEVICE
,
360 .instance_size
= sizeof(LEDMADeviceState
),
361 .instance_init
= sparc32_ledma_device_init
,
362 .class_init
= sparc32_ledma_device_class_init
,
365 static void sparc32_dma_realize(DeviceState
*dev
, Error
**errp
)
367 SPARC32DMAState
*s
= SPARC32_DMA(dev
);
368 DeviceState
*espdma
, *esp
, *ledma
, *lance
;
372 iommu
= object_resolve_path_type("", TYPE_SUN4M_IOMMU
, NULL
);
374 error_setg(errp
, "unable to locate sun4m IOMMU device");
378 espdma
= DEVICE(&s
->espdma
);
379 object_property_set_link(OBJECT(espdma
), "iommu", iommu
, &error_abort
);
380 sysbus_realize(SYS_BUS_DEVICE(espdma
), &error_fatal
);
382 esp
= DEVICE(object_resolve_path_component(OBJECT(espdma
), "esp"));
383 sbd
= SYS_BUS_DEVICE(esp
);
384 sysbus_connect_irq(sbd
, 0, qdev_get_gpio_in(espdma
, 0));
385 qdev_connect_gpio_out(espdma
, 0, qdev_get_gpio_in(esp
, 0));
386 qdev_connect_gpio_out(espdma
, 1, qdev_get_gpio_in(esp
, 1));
388 sbd
= SYS_BUS_DEVICE(espdma
);
389 memory_region_add_subregion(&s
->dmamem
, 0x0,
390 sysbus_mmio_get_region(sbd
, 0));
392 ledma
= DEVICE(&s
->ledma
);
393 object_property_set_link(OBJECT(ledma
), "iommu", iommu
, &error_abort
);
394 sysbus_realize(SYS_BUS_DEVICE(ledma
), &error_fatal
);
396 lance
= DEVICE(object_resolve_path_component(OBJECT(ledma
), "lance"));
397 sbd
= SYS_BUS_DEVICE(lance
);
398 sysbus_connect_irq(sbd
, 0, qdev_get_gpio_in(ledma
, 0));
399 qdev_connect_gpio_out(ledma
, 0, qdev_get_gpio_in(lance
, 0));
401 sbd
= SYS_BUS_DEVICE(ledma
);
402 memory_region_add_subregion(&s
->dmamem
, 0x10,
403 sysbus_mmio_get_region(sbd
, 0));
405 /* Add ledma alias to handle SunOS 5.7 - Solaris 9 invalid access bug */
406 memory_region_init_alias(&s
->ledma_alias
, OBJECT(dev
), "ledma-alias",
407 sysbus_mmio_get_region(sbd
, 0), 0x4, 0x4);
408 memory_region_add_subregion(&s
->dmamem
, 0x20, &s
->ledma_alias
);
411 static void sparc32_dma_init(Object
*obj
)
413 SPARC32DMAState
*s
= SPARC32_DMA(obj
);
414 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
416 memory_region_init(&s
->dmamem
, OBJECT(s
), "dma", DMA_SIZE
+ DMA_ETH_SIZE
);
417 sysbus_init_mmio(sbd
, &s
->dmamem
);
419 object_initialize_child(obj
, "espdma", &s
->espdma
,
420 TYPE_SPARC32_ESPDMA_DEVICE
);
421 object_initialize_child(obj
, "ledma", &s
->ledma
,
422 TYPE_SPARC32_LEDMA_DEVICE
);
425 static void sparc32_dma_class_init(ObjectClass
*klass
, void *data
)
427 DeviceClass
*dc
= DEVICE_CLASS(klass
);
429 dc
->realize
= sparc32_dma_realize
;
432 static const TypeInfo sparc32_dma_info
= {
433 .name
= TYPE_SPARC32_DMA
,
434 .parent
= TYPE_SYS_BUS_DEVICE
,
435 .instance_size
= sizeof(SPARC32DMAState
),
436 .instance_init
= sparc32_dma_init
,
437 .class_init
= sparc32_dma_class_init
,
441 static void sparc32_dma_register_types(void)
443 type_register_static(&sparc32_dma_device_info
);
444 type_register_static(&sparc32_espdma_device_info
);
445 type_register_static(&sparc32_ledma_device_info
);
446 type_register_static(&sparc32_dma_info
);
449 type_init(sparc32_dma_register_types
)