2 * QEMU educational PCI device
4 * Copyright (c) 2012-2015 Jiri Slaby
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "qemu/units.h"
27 #include "hw/pci/pci.h"
29 #include "hw/pci/msi.h"
30 #include "qemu/timer.h"
31 #include "qom/object.h"
32 #include "qemu/main-loop.h" /* iothread mutex */
33 #include "qemu/module.h"
34 #include "qapi/visitor.h"
36 #define TYPE_PCI_EDU_DEVICE "edu"
37 typedef struct EduState EduState
;
38 DECLARE_INSTANCE_CHECKER(EduState
, EDU
,
41 #define FACT_IRQ 0x00000001
42 #define DMA_IRQ 0x00000100
44 #define DMA_START 0x40000
58 #define EDU_STATUS_COMPUTING 0x01
59 #define EDU_STATUS_IRQFACT 0x80
64 #define EDU_DMA_RUN 0x1
65 #define EDU_DMA_DIR(cmd) (((cmd) & 0x2) >> 1)
66 # define EDU_DMA_FROM_PCI 0
67 # define EDU_DMA_TO_PCI 1
68 #define EDU_DMA_IRQ 0x4
76 char dma_buf
[DMA_SIZE
];
80 static bool edu_msi_enabled(EduState
*edu
)
82 return msi_enabled(&edu
->pdev
);
85 static void edu_raise_irq(EduState
*edu
, uint32_t val
)
87 edu
->irq_status
|= val
;
88 if (edu
->irq_status
) {
89 if (edu_msi_enabled(edu
)) {
90 msi_notify(&edu
->pdev
, 0);
92 pci_set_irq(&edu
->pdev
, 1);
97 static void edu_lower_irq(EduState
*edu
, uint32_t val
)
99 edu
->irq_status
&= ~val
;
101 if (!edu
->irq_status
&& !edu_msi_enabled(edu
)) {
102 pci_set_irq(&edu
->pdev
, 0);
106 static bool within(uint64_t addr
, uint64_t start
, uint64_t end
)
108 return start
<= addr
&& addr
< end
;
111 static void edu_check_range(uint64_t addr
, uint64_t size1
, uint64_t start
,
114 uint64_t end1
= addr
+ size1
;
115 uint64_t end2
= start
+ size2
;
117 if (within(addr
, start
, end2
) &&
118 end1
> addr
&& within(end1
, start
, end2
)) {
122 hw_error("EDU: DMA range 0x%016"PRIx64
"-0x%016"PRIx64
123 " out of bounds (0x%016"PRIx64
"-0x%016"PRIx64
")!",
124 addr
, end1
- 1, start
, end2
- 1);
127 static dma_addr_t
edu_clamp_addr(const EduState
*edu
, dma_addr_t addr
)
129 dma_addr_t res
= addr
& edu
->dma_mask
;
132 printf("EDU: clamping DMA %#.16"PRIx64
" to %#.16"PRIx64
"!\n", addr
, res
);
138 static void edu_dma_timer(void *opaque
)
140 EduState
*edu
= opaque
;
141 bool raise_irq
= false;
143 if (!(edu
->dma
.cmd
& EDU_DMA_RUN
)) {
147 if (EDU_DMA_DIR(edu
->dma
.cmd
) == EDU_DMA_FROM_PCI
) {
148 uint64_t dst
= edu
->dma
.dst
;
149 edu_check_range(dst
, edu
->dma
.cnt
, DMA_START
, DMA_SIZE
);
151 pci_dma_read(&edu
->pdev
, edu_clamp_addr(edu
, edu
->dma
.src
),
152 edu
->dma_buf
+ dst
, edu
->dma
.cnt
);
154 uint64_t src
= edu
->dma
.src
;
155 edu_check_range(src
, edu
->dma
.cnt
, DMA_START
, DMA_SIZE
);
157 pci_dma_write(&edu
->pdev
, edu_clamp_addr(edu
, edu
->dma
.dst
),
158 edu
->dma_buf
+ src
, edu
->dma
.cnt
);
161 edu
->dma
.cmd
&= ~EDU_DMA_RUN
;
162 if (edu
->dma
.cmd
& EDU_DMA_IRQ
) {
167 edu_raise_irq(edu
, DMA_IRQ
);
171 static void dma_rw(EduState
*edu
, bool write
, dma_addr_t
*val
, dma_addr_t
*dma
,
174 if (write
&& (edu
->dma
.cmd
& EDU_DMA_RUN
)) {
185 timer_mod(&edu
->dma_timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + 100);
189 static uint64_t edu_mmio_read(void *opaque
, hwaddr addr
, unsigned size
)
191 EduState
*edu
= opaque
;
192 uint64_t val
= ~0ULL;
194 if (addr
< 0x80 && size
!= 4) {
198 if (addr
>= 0x80 && size
!= 4 && size
!= 8) {
210 qemu_mutex_lock(&edu
->thr_mutex
);
212 qemu_mutex_unlock(&edu
->thr_mutex
);
215 val
= qatomic_read(&edu
->status
);
218 val
= edu
->irq_status
;
221 dma_rw(edu
, false, &val
, &edu
->dma
.src
, false);
224 dma_rw(edu
, false, &val
, &edu
->dma
.dst
, false);
227 dma_rw(edu
, false, &val
, &edu
->dma
.cnt
, false);
230 dma_rw(edu
, false, &val
, &edu
->dma
.cmd
, false);
237 static void edu_mmio_write(void *opaque
, hwaddr addr
, uint64_t val
,
240 EduState
*edu
= opaque
;
242 if (addr
< 0x80 && size
!= 4) {
246 if (addr
>= 0x80 && size
!= 4 && size
!= 8) {
255 if (qatomic_read(&edu
->status
) & EDU_STATUS_COMPUTING
) {
258 /* EDU_STATUS_COMPUTING cannot go 0->1 concurrently, because it is only
259 * set in this function and it is under the iothread mutex.
261 qemu_mutex_lock(&edu
->thr_mutex
);
263 qatomic_or(&edu
->status
, EDU_STATUS_COMPUTING
);
264 qemu_cond_signal(&edu
->thr_cond
);
265 qemu_mutex_unlock(&edu
->thr_mutex
);
268 if (val
& EDU_STATUS_IRQFACT
) {
269 qatomic_or(&edu
->status
, EDU_STATUS_IRQFACT
);
271 qatomic_and(&edu
->status
, ~EDU_STATUS_IRQFACT
);
275 edu_raise_irq(edu
, val
);
278 edu_lower_irq(edu
, val
);
281 dma_rw(edu
, true, &val
, &edu
->dma
.src
, false);
284 dma_rw(edu
, true, &val
, &edu
->dma
.dst
, false);
287 dma_rw(edu
, true, &val
, &edu
->dma
.cnt
, false);
290 if (!(val
& EDU_DMA_RUN
)) {
293 dma_rw(edu
, true, &val
, &edu
->dma
.cmd
, true);
298 static const MemoryRegionOps edu_mmio_ops
= {
299 .read
= edu_mmio_read
,
300 .write
= edu_mmio_write
,
301 .endianness
= DEVICE_NATIVE_ENDIAN
,
303 .min_access_size
= 4,
304 .max_access_size
= 8,
307 .min_access_size
= 4,
308 .max_access_size
= 8,
314 * We purposely use a thread, so that users are forced to wait for the status
317 static void *edu_fact_thread(void *opaque
)
319 EduState
*edu
= opaque
;
322 uint32_t val
, ret
= 1;
324 qemu_mutex_lock(&edu
->thr_mutex
);
325 while ((qatomic_read(&edu
->status
) & EDU_STATUS_COMPUTING
) == 0 &&
327 qemu_cond_wait(&edu
->thr_cond
, &edu
->thr_mutex
);
331 qemu_mutex_unlock(&edu
->thr_mutex
);
336 qemu_mutex_unlock(&edu
->thr_mutex
);
343 * We should sleep for a random period here, so that students are
344 * forced to check the status properly.
347 qemu_mutex_lock(&edu
->thr_mutex
);
349 qemu_mutex_unlock(&edu
->thr_mutex
);
350 qatomic_and(&edu
->status
, ~EDU_STATUS_COMPUTING
);
352 if (qatomic_read(&edu
->status
) & EDU_STATUS_IRQFACT
) {
353 qemu_mutex_lock_iothread();
354 edu_raise_irq(edu
, FACT_IRQ
);
355 qemu_mutex_unlock_iothread();
362 static void pci_edu_realize(PCIDevice
*pdev
, Error
**errp
)
364 EduState
*edu
= EDU(pdev
);
365 uint8_t *pci_conf
= pdev
->config
;
367 pci_config_set_interrupt_pin(pci_conf
, 1);
369 if (msi_init(pdev
, 0, 1, true, false, errp
)) {
373 timer_init_ms(&edu
->dma_timer
, QEMU_CLOCK_VIRTUAL
, edu_dma_timer
, edu
);
375 qemu_mutex_init(&edu
->thr_mutex
);
376 qemu_cond_init(&edu
->thr_cond
);
377 qemu_thread_create(&edu
->thread
, "edu", edu_fact_thread
,
378 edu
, QEMU_THREAD_JOINABLE
);
380 memory_region_init_io(&edu
->mmio
, OBJECT(edu
), &edu_mmio_ops
, edu
,
381 "edu-mmio", 1 * MiB
);
382 pci_register_bar(pdev
, 0, PCI_BASE_ADDRESS_SPACE_MEMORY
, &edu
->mmio
);
385 static void pci_edu_uninit(PCIDevice
*pdev
)
387 EduState
*edu
= EDU(pdev
);
389 qemu_mutex_lock(&edu
->thr_mutex
);
390 edu
->stopping
= true;
391 qemu_mutex_unlock(&edu
->thr_mutex
);
392 qemu_cond_signal(&edu
->thr_cond
);
393 qemu_thread_join(&edu
->thread
);
395 qemu_cond_destroy(&edu
->thr_cond
);
396 qemu_mutex_destroy(&edu
->thr_mutex
);
398 timer_del(&edu
->dma_timer
);
402 static void edu_instance_init(Object
*obj
)
404 EduState
*edu
= EDU(obj
);
406 edu
->dma_mask
= (1UL << 28) - 1;
407 object_property_add_uint64_ptr(obj
, "dma_mask",
408 &edu
->dma_mask
, OBJ_PROP_FLAG_READWRITE
);
411 static void edu_class_init(ObjectClass
*class, void *data
)
413 DeviceClass
*dc
= DEVICE_CLASS(class);
414 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(class);
416 k
->realize
= pci_edu_realize
;
417 k
->exit
= pci_edu_uninit
;
418 k
->vendor_id
= PCI_VENDOR_ID_QEMU
;
419 k
->device_id
= 0x11e8;
421 k
->class_id
= PCI_CLASS_OTHERS
;
422 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
425 static void pci_edu_register_types(void)
427 static InterfaceInfo interfaces
[] = {
428 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
431 static const TypeInfo edu_info
= {
432 .name
= TYPE_PCI_EDU_DEVICE
,
433 .parent
= TYPE_PCI_DEVICE
,
434 .instance_size
= sizeof(EduState
),
435 .instance_init
= edu_instance_init
,
436 .class_init
= edu_class_init
,
437 .interfaces
= interfaces
,
440 type_register_static(&edu_info
);
442 type_init(pci_edu_register_types
)