4 * Copyright (c) 2013 Kevin Wolf <kwolf@redhat.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * 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
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 #include "libqos/pci-pc.h"
33 #include "libqos/malloc-pc.h"
35 #include "qemu-common.h"
36 #include "hw/pci/pci_ids.h"
37 #include "hw/pci/pci_regs.h"
39 #define TEST_IMAGE_SIZE 64 * 1024 * 1024
42 #define IDE_PCI_FUNC 1
44 #define IDE_BASE 0x1f0
45 #define IDE_PRIMARY_IRQ 14
80 CMD_FLUSH_CACHE
= 0xe7,
89 BM_CMD_WRITE
= 0x8, /* write = from device to memory */
99 PRDT_EOT
= 0x80000000,
102 #define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask))
103 #define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0)
105 static QPCIBus
*pcibus
= NULL
;
106 static QGuestAllocator
*guest_malloc
;
108 static char tmp_path
[] = "/tmp/qtest.XXXXXX";
110 static void ide_test_start(const char *cmdline_fmt
, ...)
115 va_start(ap
, cmdline_fmt
);
116 cmdline
= g_strdup_vprintf(cmdline_fmt
, ap
);
119 qtest_start(cmdline
);
120 qtest_irq_intercept_in(global_qtest
, "ioapic");
121 guest_malloc
= pc_alloc_init();
124 static void ide_test_quit(void)
129 static QPCIDevice
*get_pci_device(uint16_t *bmdma_base
)
132 uint16_t vendor_id
, device_id
;
135 pcibus
= qpci_init_pc();
138 /* Find PCI device and verify it's the right one */
139 dev
= qpci_device_find(pcibus
, QPCI_DEVFN(IDE_PCI_DEV
, IDE_PCI_FUNC
));
140 g_assert(dev
!= NULL
);
142 vendor_id
= qpci_config_readw(dev
, PCI_VENDOR_ID
);
143 device_id
= qpci_config_readw(dev
, PCI_DEVICE_ID
);
144 g_assert(vendor_id
== PCI_VENDOR_ID_INTEL
);
145 g_assert(device_id
== PCI_DEVICE_ID_INTEL_82371SB_1
);
148 *bmdma_base
= (uint16_t)(uintptr_t) qpci_iomap(dev
, 4);
150 qpci_device_enable(dev
);
155 static void free_pci_device(QPCIDevice
*dev
)
157 /* libqos doesn't have a function for this, so free it manually */
161 typedef struct PrdtEntry
{
164 } QEMU_PACKED PrdtEntry
;
166 #define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask))
167 #define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0)
169 static int send_dma_request(int cmd
, uint64_t sector
, int nb_sectors
,
170 PrdtEntry
*prdt
, int prdt_entries
)
174 uintptr_t guest_prdt
;
180 dev
= get_pci_device(&bmdma_base
);
193 g_assert_not_reached();
196 if (flags
& CMDF_NO_BM
) {
197 qpci_config_writew(dev
, PCI_COMMAND
,
198 PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
);
201 /* Select device 0 */
202 outb(IDE_BASE
+ reg_device
, 0 | LBA
);
204 /* Stop any running transfer, clear any pending interrupt */
205 outb(bmdma_base
+ bmreg_cmd
, 0);
206 outb(bmdma_base
+ bmreg_status
, BM_STS_INTR
);
209 len
= sizeof(*prdt
) * prdt_entries
;
210 guest_prdt
= guest_alloc(guest_malloc
, len
);
211 memwrite(guest_prdt
, prdt
, len
);
212 outl(bmdma_base
+ bmreg_prdt
, guest_prdt
);
214 /* ATA DMA command */
215 outb(IDE_BASE
+ reg_nsectors
, nb_sectors
);
217 outb(IDE_BASE
+ reg_lba_low
, sector
& 0xff);
218 outb(IDE_BASE
+ reg_lba_middle
, (sector
>> 8) & 0xff);
219 outb(IDE_BASE
+ reg_lba_high
, (sector
>> 16) & 0xff);
221 outb(IDE_BASE
+ reg_command
, cmd
);
223 /* Start DMA transfer */
224 outb(bmdma_base
+ bmreg_cmd
, BM_CMD_START
| (from_dev
? BM_CMD_WRITE
: 0));
226 if (flags
& CMDF_ABORT
) {
227 outb(bmdma_base
+ bmreg_cmd
, 0);
230 /* Wait for the DMA transfer to complete */
232 status
= inb(bmdma_base
+ bmreg_status
);
233 } while ((status
& (BM_STS_ACTIVE
| BM_STS_INTR
)) == BM_STS_ACTIVE
);
235 g_assert_cmpint(get_irq(IDE_PRIMARY_IRQ
), ==, !!(status
& BM_STS_INTR
));
237 /* Check IDE status code */
238 assert_bit_set(inb(IDE_BASE
+ reg_status
), DRDY
);
239 assert_bit_clear(inb(IDE_BASE
+ reg_status
), BSY
| DRQ
);
241 /* Reading the status register clears the IRQ */
242 g_assert(!get_irq(IDE_PRIMARY_IRQ
));
244 /* Stop DMA transfer if still active */
245 if (status
& BM_STS_ACTIVE
) {
246 outb(bmdma_base
+ bmreg_cmd
, 0);
249 free_pci_device(dev
);
254 static void test_bmdma_simple_rw(void)
260 uintptr_t guest_buf
= guest_alloc(guest_malloc
, len
);
264 .addr
= cpu_to_le32(guest_buf
),
265 .size
= cpu_to_le32(len
| PRDT_EOT
),
270 cmpbuf
= g_malloc(len
);
272 /* Write 0x55 pattern to sector 0 */
273 memset(buf
, 0x55, len
);
274 memwrite(guest_buf
, buf
, len
);
276 status
= send_dma_request(CMD_WRITE_DMA
, 0, 1, prdt
, ARRAY_SIZE(prdt
));
277 g_assert_cmphex(status
, ==, BM_STS_INTR
);
278 assert_bit_clear(inb(IDE_BASE
+ reg_status
), DF
| ERR
);
280 /* Write 0xaa pattern to sector 1 */
281 memset(buf
, 0xaa, len
);
282 memwrite(guest_buf
, buf
, len
);
284 status
= send_dma_request(CMD_WRITE_DMA
, 1, 1, prdt
, ARRAY_SIZE(prdt
));
285 g_assert_cmphex(status
, ==, BM_STS_INTR
);
286 assert_bit_clear(inb(IDE_BASE
+ reg_status
), DF
| ERR
);
288 /* Read and verify 0x55 pattern in sector 0 */
289 memset(cmpbuf
, 0x55, len
);
291 status
= send_dma_request(CMD_READ_DMA
, 0, 1, prdt
, ARRAY_SIZE(prdt
));
292 g_assert_cmphex(status
, ==, BM_STS_INTR
);
293 assert_bit_clear(inb(IDE_BASE
+ reg_status
), DF
| ERR
);
295 memread(guest_buf
, buf
, len
);
296 g_assert(memcmp(buf
, cmpbuf
, len
) == 0);
298 /* Read and verify 0xaa pattern in sector 1 */
299 memset(cmpbuf
, 0xaa, len
);
301 status
= send_dma_request(CMD_READ_DMA
, 1, 1, prdt
, ARRAY_SIZE(prdt
));
302 g_assert_cmphex(status
, ==, BM_STS_INTR
);
303 assert_bit_clear(inb(IDE_BASE
+ reg_status
), DF
| ERR
);
305 memread(guest_buf
, buf
, len
);
306 g_assert(memcmp(buf
, cmpbuf
, len
) == 0);
313 static void test_bmdma_short_prdt(void)
320 .size
= cpu_to_le32(0x10 | PRDT_EOT
),
325 status
= send_dma_request(CMD_READ_DMA
, 0, 1,
326 prdt
, ARRAY_SIZE(prdt
));
327 g_assert_cmphex(status
, ==, 0);
328 assert_bit_clear(inb(IDE_BASE
+ reg_status
), DF
| ERR
);
330 /* Abort the request before it completes */
331 status
= send_dma_request(CMD_READ_DMA
| CMDF_ABORT
, 0, 1,
332 prdt
, ARRAY_SIZE(prdt
));
333 g_assert_cmphex(status
, ==, 0);
334 assert_bit_clear(inb(IDE_BASE
+ reg_status
), DF
| ERR
);
337 static void test_bmdma_long_prdt(void)
344 .size
= cpu_to_le32(0x1000 | PRDT_EOT
),
349 status
= send_dma_request(CMD_READ_DMA
, 0, 1,
350 prdt
, ARRAY_SIZE(prdt
));
351 g_assert_cmphex(status
, ==, BM_STS_ACTIVE
| BM_STS_INTR
);
352 assert_bit_clear(inb(IDE_BASE
+ reg_status
), DF
| ERR
);
354 /* Abort the request before it completes */
355 status
= send_dma_request(CMD_READ_DMA
| CMDF_ABORT
, 0, 1,
356 prdt
, ARRAY_SIZE(prdt
));
357 g_assert_cmphex(status
, ==, BM_STS_INTR
);
358 assert_bit_clear(inb(IDE_BASE
+ reg_status
), DF
| ERR
);
361 static void test_bmdma_no_busmaster(void)
365 /* No PRDT_EOT, each entry addr 0/size 64k, and in theory qemu shouldn't be
366 * able to access it anyway because the Bus Master bit in the PCI command
367 * register isn't set. This is complete nonsense, but it used to be pretty
368 * good at confusing and occasionally crashing qemu. */
369 PrdtEntry prdt
[4096] = { };
371 status
= send_dma_request(CMD_READ_DMA
| CMDF_NO_BM
, 0, 512,
372 prdt
, ARRAY_SIZE(prdt
));
374 /* Not entirely clear what the expected result is, but this is what we get
375 * in practice. At least we want to be aware of any changes. */
376 g_assert_cmphex(status
, ==, BM_STS_ACTIVE
| BM_STS_INTR
);
377 assert_bit_clear(inb(IDE_BASE
+ reg_status
), DF
| ERR
);
380 static void test_bmdma_setup(void)
384 "-drive file=%s,if=ide,serial=%s,cache=writeback "
385 "-global ide-hd.ver=%s",
386 tmp_path
, "testdisk", "version");
389 static void test_bmdma_teardown(void)
394 static void string_cpu_to_be16(uint16_t *s
, size_t bytes
)
396 g_assert((bytes
& 1) == 0);
400 *s
= cpu_to_be16(*s
);
405 static void test_identify(void)
414 "-drive file=%s,if=ide,serial=%s,cache=writeback "
415 "-global ide-hd.ver=%s",
416 tmp_path
, "testdisk", "version");
418 /* IDENTIFY command on device 0*/
419 outb(IDE_BASE
+ reg_device
, 0);
420 outb(IDE_BASE
+ reg_command
, CMD_IDENTIFY
);
422 /* Read in the IDENTIFY buffer and check registers */
423 data
= inb(IDE_BASE
+ reg_device
);
424 g_assert_cmpint(data
& DEV
, ==, 0);
426 for (i
= 0; i
< 256; i
++) {
427 data
= inb(IDE_BASE
+ reg_status
);
428 assert_bit_set(data
, DRDY
| DRQ
);
429 assert_bit_clear(data
, BSY
| DF
| ERR
);
431 ((uint16_t*) buf
)[i
] = inw(IDE_BASE
+ reg_data
);
434 data
= inb(IDE_BASE
+ reg_status
);
435 assert_bit_set(data
, DRDY
);
436 assert_bit_clear(data
, BSY
| DF
| ERR
| DRQ
);
438 /* Check serial number/version in the buffer */
439 string_cpu_to_be16(&buf
[10], 20);
440 ret
= memcmp(&buf
[10], "testdisk ", 20);
443 string_cpu_to_be16(&buf
[23], 8);
444 ret
= memcmp(&buf
[23], "version ", 8);
447 /* Write cache enabled bit */
448 assert_bit_set(buf
[85], 0x20);
453 static void test_flush(void)
459 "-drive file=blkdebug::%s,if=ide,cache=writeback",
462 /* Delay the completion of the flush request until we explicitly do it */
463 qmp_discard_response("{'execute':'human-monitor-command', 'arguments': {"
465 " 'qemu-io ide0-hd0 \"break flush_to_os A\"'} }");
467 /* FLUSH CACHE command on device 0*/
468 outb(IDE_BASE
+ reg_device
, 0);
469 outb(IDE_BASE
+ reg_command
, CMD_FLUSH_CACHE
);
471 /* Check status while request is in flight*/
472 data
= inb(IDE_BASE
+ reg_status
);
473 assert_bit_set(data
, BSY
| DRDY
);
474 assert_bit_clear(data
, DF
| ERR
| DRQ
);
476 /* Complete the command */
477 qmp_discard_response("{'execute':'human-monitor-command', 'arguments': {"
479 " 'qemu-io ide0-hd0 \"resume A\"'} }");
481 /* Check registers */
482 data
= inb(IDE_BASE
+ reg_device
);
483 g_assert_cmpint(data
& DEV
, ==, 0);
486 data
= inb(IDE_BASE
+ reg_status
);
487 } while (data
& BSY
);
489 assert_bit_set(data
, DRDY
);
490 assert_bit_clear(data
, BSY
| DF
| ERR
| DRQ
);
495 int main(int argc
, char **argv
)
497 const char *arch
= qtest_get_arch();
501 /* Check architecture */
502 if (strcmp(arch
, "i386") && strcmp(arch
, "x86_64")) {
503 g_test_message("Skipping test for non-x86\n");
507 /* Create a temporary raw image */
508 fd
= mkstemp(tmp_path
);
510 ret
= ftruncate(fd
, TEST_IMAGE_SIZE
);
515 g_test_init(&argc
, &argv
, NULL
);
517 qtest_add_func("/ide/identify", test_identify
);
519 qtest_add_func("/ide/bmdma/setup", test_bmdma_setup
);
520 qtest_add_func("/ide/bmdma/simple_rw", test_bmdma_simple_rw
);
521 qtest_add_func("/ide/bmdma/short_prdt", test_bmdma_short_prdt
);
522 qtest_add_func("/ide/bmdma/long_prdt", test_bmdma_long_prdt
);
523 qtest_add_func("/ide/bmdma/no_busmaster", test_bmdma_no_busmaster
);
524 qtest_add_func("/ide/bmdma/teardown", test_bmdma_teardown
);
526 qtest_add_func("/ide/flush", test_flush
);