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 "qemu/main-loop.h" /* iothread mutex */
32 #include "qemu/module.h"
33 #include "qapi/visitor.h"
35 #define TYPE_PCI_EDU_DEVICE "edu"
36 #define EDU(obj) OBJECT_CHECK(EduState, obj, TYPE_PCI_EDU_DEVICE)
38 #define FACT_IRQ 0x00000001
39 #define DMA_IRQ 0x00000100
41 #define DMA_START 0x40000
55 #define EDU_STATUS_COMPUTING 0x01
56 #define EDU_STATUS_IRQFACT 0x80
61 #define EDU_DMA_RUN 0x1
62 #define EDU_DMA_DIR(cmd) (((cmd) & 0x2) >> 1)
63 # define EDU_DMA_FROM_PCI 0
64 # define EDU_DMA_TO_PCI 1
65 #define EDU_DMA_IRQ 0x4
73 char dma_buf
[DMA_SIZE
];
77 static bool edu_msi_enabled(EduState
*edu
)
79 return msi_enabled(&edu
->pdev
);
82 static void edu_raise_irq(EduState
*edu
, uint32_t val
)
84 edu
->irq_status
|= val
;
85 if (edu
->irq_status
) {
86 if (edu_msi_enabled(edu
)) {
87 msi_notify(&edu
->pdev
, 0);
89 pci_set_irq(&edu
->pdev
, 1);
94 static void edu_lower_irq(EduState
*edu
, uint32_t val
)
96 edu
->irq_status
&= ~val
;
98 if (!edu
->irq_status
&& !edu_msi_enabled(edu
)) {
99 pci_set_irq(&edu
->pdev
, 0);
103 static bool within(uint64_t addr
, uint64_t start
, uint64_t end
)
105 return start
<= addr
&& addr
< end
;
108 static void edu_check_range(uint64_t addr
, uint64_t size1
, uint64_t start
,
111 uint64_t end1
= addr
+ size1
;
112 uint64_t end2
= start
+ size2
;
114 if (within(addr
, start
, end2
) &&
115 end1
> addr
&& within(end1
, start
, end2
)) {
119 hw_error("EDU: DMA range 0x%016"PRIx64
"-0x%016"PRIx64
120 " out of bounds (0x%016"PRIx64
"-0x%016"PRIx64
")!",
121 addr
, end1
- 1, start
, end2
- 1);
124 static dma_addr_t
edu_clamp_addr(const EduState
*edu
, dma_addr_t addr
)
126 dma_addr_t res
= addr
& edu
->dma_mask
;
129 printf("EDU: clamping DMA %#.16"PRIx64
" to %#.16"PRIx64
"!\n", addr
, res
);
135 static void edu_dma_timer(void *opaque
)
137 EduState
*edu
= opaque
;
138 bool raise_irq
= false;
140 if (!(edu
->dma
.cmd
& EDU_DMA_RUN
)) {
144 if (EDU_DMA_DIR(edu
->dma
.cmd
) == EDU_DMA_FROM_PCI
) {
145 uint64_t dst
= edu
->dma
.dst
;
146 edu_check_range(dst
, edu
->dma
.cnt
, DMA_START
, DMA_SIZE
);
148 pci_dma_read(&edu
->pdev
, edu_clamp_addr(edu
, edu
->dma
.src
),
149 edu
->dma_buf
+ dst
, edu
->dma
.cnt
);
151 uint64_t src
= edu
->dma
.src
;
152 edu_check_range(src
, edu
->dma
.cnt
, DMA_START
, DMA_SIZE
);
154 pci_dma_write(&edu
->pdev
, edu_clamp_addr(edu
, edu
->dma
.dst
),
155 edu
->dma_buf
+ src
, edu
->dma
.cnt
);
158 edu
->dma
.cmd
&= ~EDU_DMA_RUN
;
159 if (edu
->dma
.cmd
& EDU_DMA_IRQ
) {
164 edu_raise_irq(edu
, DMA_IRQ
);
168 static void dma_rw(EduState
*edu
, bool write
, dma_addr_t
*val
, dma_addr_t
*dma
,
171 if (write
&& (edu
->dma
.cmd
& EDU_DMA_RUN
)) {
182 timer_mod(&edu
->dma_timer
, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + 100);
186 static uint64_t edu_mmio_read(void *opaque
, hwaddr addr
, unsigned size
)
188 EduState
*edu
= opaque
;
189 uint64_t val
= ~0ULL;
191 if (addr
< 0x80 && size
!= 4) {
195 if (addr
>= 0x80 && size
!= 4 && size
!= 8) {
207 qemu_mutex_lock(&edu
->thr_mutex
);
209 qemu_mutex_unlock(&edu
->thr_mutex
);
212 val
= atomic_read(&edu
->status
);
215 val
= edu
->irq_status
;
218 dma_rw(edu
, false, &val
, &edu
->dma
.src
, false);
221 dma_rw(edu
, false, &val
, &edu
->dma
.dst
, false);
224 dma_rw(edu
, false, &val
, &edu
->dma
.cnt
, false);
227 dma_rw(edu
, false, &val
, &edu
->dma
.cmd
, false);
234 static void edu_mmio_write(void *opaque
, hwaddr addr
, uint64_t val
,
237 EduState
*edu
= opaque
;
239 if (addr
< 0x80 && size
!= 4) {
243 if (addr
>= 0x80 && size
!= 4 && size
!= 8) {
252 if (atomic_read(&edu
->status
) & EDU_STATUS_COMPUTING
) {
255 /* EDU_STATUS_COMPUTING cannot go 0->1 concurrently, because it is only
256 * set in this function and it is under the iothread mutex.
258 qemu_mutex_lock(&edu
->thr_mutex
);
260 atomic_or(&edu
->status
, EDU_STATUS_COMPUTING
);
261 qemu_cond_signal(&edu
->thr_cond
);
262 qemu_mutex_unlock(&edu
->thr_mutex
);
265 if (val
& EDU_STATUS_IRQFACT
) {
266 atomic_or(&edu
->status
, EDU_STATUS_IRQFACT
);
268 atomic_and(&edu
->status
, ~EDU_STATUS_IRQFACT
);
272 edu_raise_irq(edu
, val
);
275 edu_lower_irq(edu
, val
);
278 dma_rw(edu
, true, &val
, &edu
->dma
.src
, false);
281 dma_rw(edu
, true, &val
, &edu
->dma
.dst
, false);
284 dma_rw(edu
, true, &val
, &edu
->dma
.cnt
, false);
287 if (!(val
& EDU_DMA_RUN
)) {
290 dma_rw(edu
, true, &val
, &edu
->dma
.cmd
, true);
295 static const MemoryRegionOps edu_mmio_ops
= {
296 .read
= edu_mmio_read
,
297 .write
= edu_mmio_write
,
298 .endianness
= DEVICE_NATIVE_ENDIAN
,
300 .min_access_size
= 4,
301 .max_access_size
= 8,
304 .min_access_size
= 4,
305 .max_access_size
= 8,
311 * We purposely use a thread, so that users are forced to wait for the status
314 static void *edu_fact_thread(void *opaque
)
316 EduState
*edu
= opaque
;
319 uint32_t val
, ret
= 1;
321 qemu_mutex_lock(&edu
->thr_mutex
);
322 while ((atomic_read(&edu
->status
) & EDU_STATUS_COMPUTING
) == 0 &&
324 qemu_cond_wait(&edu
->thr_cond
, &edu
->thr_mutex
);
328 qemu_mutex_unlock(&edu
->thr_mutex
);
333 qemu_mutex_unlock(&edu
->thr_mutex
);
340 * We should sleep for a random period here, so that students are
341 * forced to check the status properly.
344 qemu_mutex_lock(&edu
->thr_mutex
);
346 qemu_mutex_unlock(&edu
->thr_mutex
);
347 atomic_and(&edu
->status
, ~EDU_STATUS_COMPUTING
);
349 if (atomic_read(&edu
->status
) & EDU_STATUS_IRQFACT
) {
350 qemu_mutex_lock_iothread();
351 edu_raise_irq(edu
, FACT_IRQ
);
352 qemu_mutex_unlock_iothread();
359 static void pci_edu_realize(PCIDevice
*pdev
, Error
**errp
)
361 EduState
*edu
= EDU(pdev
);
362 uint8_t *pci_conf
= pdev
->config
;
364 pci_config_set_interrupt_pin(pci_conf
, 1);
366 if (msi_init(pdev
, 0, 1, true, false, errp
)) {
370 timer_init_ms(&edu
->dma_timer
, QEMU_CLOCK_VIRTUAL
, edu_dma_timer
, edu
);
372 qemu_mutex_init(&edu
->thr_mutex
);
373 qemu_cond_init(&edu
->thr_cond
);
374 qemu_thread_create(&edu
->thread
, "edu", edu_fact_thread
,
375 edu
, QEMU_THREAD_JOINABLE
);
377 memory_region_init_io(&edu
->mmio
, OBJECT(edu
), &edu_mmio_ops
, edu
,
378 "edu-mmio", 1 * MiB
);
379 pci_register_bar(pdev
, 0, PCI_BASE_ADDRESS_SPACE_MEMORY
, &edu
->mmio
);
382 static void pci_edu_uninit(PCIDevice
*pdev
)
384 EduState
*edu
= EDU(pdev
);
386 qemu_mutex_lock(&edu
->thr_mutex
);
387 edu
->stopping
= true;
388 qemu_mutex_unlock(&edu
->thr_mutex
);
389 qemu_cond_signal(&edu
->thr_cond
);
390 qemu_thread_join(&edu
->thread
);
392 qemu_cond_destroy(&edu
->thr_cond
);
393 qemu_mutex_destroy(&edu
->thr_mutex
);
395 timer_del(&edu
->dma_timer
);
399 static void edu_instance_init(Object
*obj
)
401 EduState
*edu
= EDU(obj
);
403 edu
->dma_mask
= (1UL << 28) - 1;
404 object_property_add_uint64_ptr(obj
, "dma_mask",
405 &edu
->dma_mask
, OBJ_PROP_FLAG_READWRITE
);
408 static void edu_class_init(ObjectClass
*class, void *data
)
410 DeviceClass
*dc
= DEVICE_CLASS(class);
411 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(class);
413 k
->realize
= pci_edu_realize
;
414 k
->exit
= pci_edu_uninit
;
415 k
->vendor_id
= PCI_VENDOR_ID_QEMU
;
416 k
->device_id
= 0x11e8;
418 k
->class_id
= PCI_CLASS_OTHERS
;
419 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
422 static void pci_edu_register_types(void)
424 static InterfaceInfo interfaces
[] = {
425 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
428 static const TypeInfo edu_info
= {
429 .name
= TYPE_PCI_EDU_DEVICE
,
430 .parent
= TYPE_PCI_DEVICE
,
431 .instance_size
= sizeof(EduState
),
432 .instance_init
= edu_instance_init
,
433 .class_init
= edu_class_init
,
434 .interfaces
= interfaces
,
437 type_register_static(&edu_info
);
439 type_init(pci_edu_register_types
)