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"
28 #include "hw/pci/msi.h"
29 #include "qemu/timer.h"
30 #include "qemu/main-loop.h" /* iothread mutex */
31 #include "qapi/visitor.h"
33 #define TYPE_PCI_EDU_DEVICE "edu"
34 #define EDU(obj) OBJECT_CHECK(EduState, obj, TYPE_PCI_EDU_DEVICE)
36 #define FACT_IRQ 0x00000001
37 #define DMA_IRQ 0x00000100
39 #define DMA_START 0x40000
53 #define EDU_STATUS_COMPUTING 0x01
54 #define EDU_STATUS_IRQFACT 0x80
59 #define EDU_DMA_RUN 0x1
60 #define EDU_DMA_DIR(cmd) (((cmd) & 0x2) >> 1)
61 # define EDU_DMA_FROM_PCI 0
62 # define EDU_DMA_TO_PCI 1
63 #define EDU_DMA_IRQ 0x4
71 char dma_buf
[DMA_SIZE
];
75 static bool edu_msi_enabled(EduState
*edu
)
77 return msi_enabled(&edu
->pdev
);
80 static void edu_raise_irq(EduState
*edu
, uint32_t val
)
82 edu
->irq_status
|= val
;
83 if (edu
->irq_status
) {
84 if (edu_msi_enabled(edu
)) {
85 msi_notify(&edu
->pdev
, 0);
87 pci_set_irq(&edu
->pdev
, 1);
92 static void edu_lower_irq(EduState
*edu
, uint32_t val
)
94 edu
->irq_status
&= ~val
;
96 if (!edu
->irq_status
&& !edu_msi_enabled(edu
)) {
97 pci_set_irq(&edu
->pdev
, 0);
101 static bool within(uint64_t addr
, uint64_t start
, uint64_t end
)
103 return start
<= addr
&& addr
< end
;
106 static void edu_check_range(uint64_t addr
, uint64_t size1
, uint64_t start
,
109 uint64_t end1
= addr
+ size1
;
110 uint64_t end2
= start
+ size2
;
112 if (within(addr
, start
, end2
) &&
113 end1
> addr
&& within(end1
, start
, end2
)) {
117 hw_error("EDU: DMA range 0x%016"PRIx64
"-0x%016"PRIx64
118 " out of bounds (0x%016"PRIx64
"-0x%016"PRIx64
")!",
119 addr
, end1
- 1, start
, end2
- 1);
122 static dma_addr_t
edu_clamp_addr(const EduState
*edu
, dma_addr_t addr
)
124 dma_addr_t res
= addr
& edu
->dma_mask
;
127 printf("EDU: clamping DMA %#.16"PRIx64
" to %#.16"PRIx64
"!\n", addr
, res
);
133 static void edu_dma_timer(void *opaque
)
135 EduState
*edu
= opaque
;
136 bool raise_irq
= false;
138 if (!(edu
->dma
.cmd
& EDU_DMA_RUN
)) {
142 if (EDU_DMA_DIR(edu
->dma
.cmd
) == EDU_DMA_FROM_PCI
) {
143 uint64_t dst
= edu
->dma
.dst
;
144 edu_check_range(dst
, edu
->dma
.cnt
, DMA_START
, DMA_SIZE
);
146 pci_dma_read(&edu
->pdev
, edu_clamp_addr(edu
, edu
->dma
.src
),
147 edu
->dma_buf
+ dst
, edu
->dma
.cnt
);
149 uint64_t src
= edu
->dma
.src
;
150 edu_check_range(src
, edu
->dma
.cnt
, DMA_START
, DMA_SIZE
);
152 pci_dma_write(&edu
->pdev
, edu_clamp_addr(edu
, edu
->dma
.dst
),
153 edu
->dma_buf
+ src
, edu
->dma
.cnt
);
156 edu
->dma
.cmd
&= ~EDU_DMA_RUN
;
157 if (edu
->dma
.cmd
& EDU_DMA_IRQ
) {
162 edu_raise_irq(edu
, DMA_IRQ
);
166 static void dma_rw(EduState
*edu
, bool write
, dma_addr_t
*val
, dma_addr_t
*dma
,
169 if (write
&& (edu
->dma
.cmd
& EDU_DMA_RUN
)) {
180 timer_mod(&edu
->dma_timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + 100);
184 static uint64_t edu_mmio_read(void *opaque
, hwaddr addr
, unsigned size
)
186 EduState
*edu
= opaque
;
187 uint64_t val
= ~0ULL;
189 if (addr
< 0x80 && size
!= 4) {
193 if (addr
>= 0x80 && size
!= 4 && size
!= 8) {
205 qemu_mutex_lock(&edu
->thr_mutex
);
207 qemu_mutex_unlock(&edu
->thr_mutex
);
210 val
= atomic_read(&edu
->status
);
213 val
= edu
->irq_status
;
216 dma_rw(edu
, false, &val
, &edu
->dma
.src
, false);
219 dma_rw(edu
, false, &val
, &edu
->dma
.dst
, false);
222 dma_rw(edu
, false, &val
, &edu
->dma
.cnt
, false);
225 dma_rw(edu
, false, &val
, &edu
->dma
.cmd
, false);
232 static void edu_mmio_write(void *opaque
, hwaddr addr
, uint64_t val
,
235 EduState
*edu
= opaque
;
237 if (addr
< 0x80 && size
!= 4) {
241 if (addr
>= 0x80 && size
!= 4 && size
!= 8) {
250 if (atomic_read(&edu
->status
) & EDU_STATUS_COMPUTING
) {
253 /* EDU_STATUS_COMPUTING cannot go 0->1 concurrently, because it is only
254 * set in this function and it is under the iothread mutex.
256 qemu_mutex_lock(&edu
->thr_mutex
);
258 atomic_or(&edu
->status
, EDU_STATUS_COMPUTING
);
259 qemu_cond_signal(&edu
->thr_cond
);
260 qemu_mutex_unlock(&edu
->thr_mutex
);
263 if (val
& EDU_STATUS_IRQFACT
) {
264 atomic_or(&edu
->status
, EDU_STATUS_IRQFACT
);
266 atomic_and(&edu
->status
, ~EDU_STATUS_IRQFACT
);
270 edu_raise_irq(edu
, val
);
273 edu_lower_irq(edu
, val
);
276 dma_rw(edu
, true, &val
, &edu
->dma
.src
, false);
279 dma_rw(edu
, true, &val
, &edu
->dma
.dst
, false);
282 dma_rw(edu
, true, &val
, &edu
->dma
.cnt
, false);
285 if (!(val
& EDU_DMA_RUN
)) {
288 dma_rw(edu
, true, &val
, &edu
->dma
.cmd
, true);
293 static const MemoryRegionOps edu_mmio_ops
= {
294 .read
= edu_mmio_read
,
295 .write
= edu_mmio_write
,
296 .endianness
= DEVICE_NATIVE_ENDIAN
,
298 .min_access_size
= 4,
299 .max_access_size
= 8,
302 .min_access_size
= 4,
303 .max_access_size
= 8,
309 * We purposely use a thread, so that users are forced to wait for the status
312 static void *edu_fact_thread(void *opaque
)
314 EduState
*edu
= opaque
;
317 uint32_t val
, ret
= 1;
319 qemu_mutex_lock(&edu
->thr_mutex
);
320 while ((atomic_read(&edu
->status
) & EDU_STATUS_COMPUTING
) == 0 &&
322 qemu_cond_wait(&edu
->thr_cond
, &edu
->thr_mutex
);
326 qemu_mutex_unlock(&edu
->thr_mutex
);
331 qemu_mutex_unlock(&edu
->thr_mutex
);
338 * We should sleep for a random period here, so that students are
339 * forced to check the status properly.
342 qemu_mutex_lock(&edu
->thr_mutex
);
344 qemu_mutex_unlock(&edu
->thr_mutex
);
345 atomic_and(&edu
->status
, ~EDU_STATUS_COMPUTING
);
347 if (atomic_read(&edu
->status
) & EDU_STATUS_IRQFACT
) {
348 qemu_mutex_lock_iothread();
349 edu_raise_irq(edu
, FACT_IRQ
);
350 qemu_mutex_unlock_iothread();
357 static void pci_edu_realize(PCIDevice
*pdev
, Error
**errp
)
359 EduState
*edu
= EDU(pdev
);
360 uint8_t *pci_conf
= pdev
->config
;
362 pci_config_set_interrupt_pin(pci_conf
, 1);
364 if (msi_init(pdev
, 0, 1, true, false, errp
)) {
368 timer_init_ms(&edu
->dma_timer
, QEMU_CLOCK_VIRTUAL
, edu_dma_timer
, edu
);
370 qemu_mutex_init(&edu
->thr_mutex
);
371 qemu_cond_init(&edu
->thr_cond
);
372 qemu_thread_create(&edu
->thread
, "edu", edu_fact_thread
,
373 edu
, QEMU_THREAD_JOINABLE
);
375 memory_region_init_io(&edu
->mmio
, OBJECT(edu
), &edu_mmio_ops
, edu
,
376 "edu-mmio", 1 * MiB
);
377 pci_register_bar(pdev
, 0, PCI_BASE_ADDRESS_SPACE_MEMORY
, &edu
->mmio
);
380 static void pci_edu_uninit(PCIDevice
*pdev
)
382 EduState
*edu
= EDU(pdev
);
384 qemu_mutex_lock(&edu
->thr_mutex
);
385 edu
->stopping
= true;
386 qemu_mutex_unlock(&edu
->thr_mutex
);
387 qemu_cond_signal(&edu
->thr_cond
);
388 qemu_thread_join(&edu
->thread
);
390 qemu_cond_destroy(&edu
->thr_cond
);
391 qemu_mutex_destroy(&edu
->thr_mutex
);
393 timer_del(&edu
->dma_timer
);
397 static void edu_obj_uint64(Object
*obj
, Visitor
*v
, const char *name
,
398 void *opaque
, Error
**errp
)
400 uint64_t *val
= opaque
;
402 visit_type_uint64(v
, name
, val
, errp
);
405 static void edu_instance_init(Object
*obj
)
407 EduState
*edu
= EDU(obj
);
409 edu
->dma_mask
= (1UL << 28) - 1;
410 object_property_add(obj
, "dma_mask", "uint64", edu_obj_uint64
,
411 edu_obj_uint64
, NULL
, &edu
->dma_mask
, NULL
);
414 static void edu_class_init(ObjectClass
*class, void *data
)
416 DeviceClass
*dc
= DEVICE_CLASS(class);
417 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(class);
419 k
->realize
= pci_edu_realize
;
420 k
->exit
= pci_edu_uninit
;
421 k
->vendor_id
= PCI_VENDOR_ID_QEMU
;
422 k
->device_id
= 0x11e8;
424 k
->class_id
= PCI_CLASS_OTHERS
;
425 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
428 static void pci_edu_register_types(void)
430 static InterfaceInfo interfaces
[] = {
431 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
434 static const TypeInfo edu_info
= {
435 .name
= TYPE_PCI_EDU_DEVICE
,
436 .parent
= TYPE_PCI_DEVICE
,
437 .instance_size
= sizeof(EduState
),
438 .instance_init
= edu_instance_init
,
439 .class_init
= edu_class_init
,
440 .interfaces
= interfaces
,
443 type_register_static(&edu_info
);
445 type_init(pci_edu_register_types
)