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
87 BM_CMD_WRITE
= 0x8, /* write = from device to memory */
97 PRDT_EOT
= 0x80000000,
100 #define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask))
101 #define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0)
103 static QPCIBus
*pcibus
= NULL
;
104 static QGuestAllocator
*guest_malloc
;
106 static char tmp_path
[] = "/tmp/qtest.XXXXXX";
108 static void ide_test_start(const char *cmdline_fmt
, ...)
113 va_start(ap
, cmdline_fmt
);
114 cmdline
= g_strdup_vprintf(cmdline_fmt
, ap
);
117 qtest_start(cmdline
);
118 qtest_irq_intercept_in(global_qtest
, "ioapic");
119 guest_malloc
= pc_alloc_init();
122 static void ide_test_quit(void)
124 qtest_quit(global_qtest
);
127 static QPCIDevice
*get_pci_device(uint16_t *bmdma_base
)
130 uint16_t vendor_id
, device_id
;
133 pcibus
= qpci_init_pc();
136 /* Find PCI device and verify it's the right one */
137 dev
= qpci_device_find(pcibus
, QPCI_DEVFN(IDE_PCI_DEV
, IDE_PCI_FUNC
));
138 g_assert(dev
!= NULL
);
140 vendor_id
= qpci_config_readw(dev
, PCI_VENDOR_ID
);
141 device_id
= qpci_config_readw(dev
, PCI_DEVICE_ID
);
142 g_assert(vendor_id
== PCI_VENDOR_ID_INTEL
);
143 g_assert(device_id
== PCI_DEVICE_ID_INTEL_82371SB_1
);
146 *bmdma_base
= (uint16_t)(uintptr_t) qpci_iomap(dev
, 4);
148 qpci_device_enable(dev
);
153 static void free_pci_device(QPCIDevice
*dev
)
155 /* libqos doesn't have a function for this, so free it manually */
159 typedef struct PrdtEntry
{
162 } QEMU_PACKED PrdtEntry
;
164 #define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask))
165 #define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0)
167 static int send_dma_request(int cmd
, uint64_t sector
, int nb_sectors
,
168 PrdtEntry
*prdt
, int prdt_entries
)
172 uintptr_t guest_prdt
;
178 dev
= get_pci_device(&bmdma_base
);
191 g_assert_not_reached();
194 /* Select device 0 */
195 outb(IDE_BASE
+ reg_device
, 0 | LBA
);
197 /* Stop any running transfer, clear any pending interrupt */
198 outb(bmdma_base
+ bmreg_cmd
, 0);
199 outb(bmdma_base
+ bmreg_status
, BM_STS_INTR
);
202 len
= sizeof(*prdt
) * prdt_entries
;
203 guest_prdt
= guest_alloc(guest_malloc
, len
);
204 memwrite(guest_prdt
, prdt
, len
);
205 outl(bmdma_base
+ bmreg_prdt
, guest_prdt
);
207 /* ATA DMA command */
208 outb(IDE_BASE
+ reg_nsectors
, nb_sectors
);
210 outb(IDE_BASE
+ reg_lba_low
, sector
& 0xff);
211 outb(IDE_BASE
+ reg_lba_middle
, (sector
>> 8) & 0xff);
212 outb(IDE_BASE
+ reg_lba_high
, (sector
>> 16) & 0xff);
214 outb(IDE_BASE
+ reg_command
, cmd
);
216 /* Start DMA transfer */
217 outb(bmdma_base
+ bmreg_cmd
, BM_CMD_START
| (from_dev
? BM_CMD_WRITE
: 0));
219 if (flags
& CMDF_ABORT
) {
220 outb(bmdma_base
+ bmreg_cmd
, 0);
223 /* Wait for the DMA transfer to complete */
225 status
= inb(bmdma_base
+ bmreg_status
);
226 } while ((status
& (BM_STS_ACTIVE
| BM_STS_INTR
)) == BM_STS_ACTIVE
);
228 g_assert_cmpint(get_irq(IDE_PRIMARY_IRQ
), ==, !!(status
& BM_STS_INTR
));
230 /* Check IDE status code */
231 assert_bit_set(inb(IDE_BASE
+ reg_status
), DRDY
);
232 assert_bit_clear(inb(IDE_BASE
+ reg_status
), BSY
| DRQ
);
234 /* Reading the status register clears the IRQ */
235 g_assert(!get_irq(IDE_PRIMARY_IRQ
));
237 /* Stop DMA transfer if still active */
238 if (status
& BM_STS_ACTIVE
) {
239 outb(bmdma_base
+ bmreg_cmd
, 0);
242 free_pci_device(dev
);
247 static void test_bmdma_simple_rw(void)
253 uintptr_t guest_buf
= guest_alloc(guest_malloc
, len
);
257 .addr
= cpu_to_le32(guest_buf
),
258 .size
= cpu_to_le32(len
| PRDT_EOT
),
263 cmpbuf
= g_malloc(len
);
265 /* Write 0x55 pattern to sector 0 */
266 memset(buf
, 0x55, len
);
267 memwrite(guest_buf
, buf
, len
);
269 status
= send_dma_request(CMD_WRITE_DMA
, 0, 1, prdt
, ARRAY_SIZE(prdt
));
270 g_assert_cmphex(status
, ==, BM_STS_INTR
);
271 assert_bit_clear(inb(IDE_BASE
+ reg_status
), DF
| ERR
);
273 /* Write 0xaa pattern to sector 1 */
274 memset(buf
, 0xaa, len
);
275 memwrite(guest_buf
, buf
, len
);
277 status
= send_dma_request(CMD_WRITE_DMA
, 1, 1, prdt
, ARRAY_SIZE(prdt
));
278 g_assert_cmphex(status
, ==, BM_STS_INTR
);
279 assert_bit_clear(inb(IDE_BASE
+ reg_status
), DF
| ERR
);
281 /* Read and verify 0x55 pattern in sector 0 */
282 memset(cmpbuf
, 0x55, len
);
284 status
= send_dma_request(CMD_READ_DMA
, 0, 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 memread(guest_buf
, buf
, len
);
289 g_assert(memcmp(buf
, cmpbuf
, len
) == 0);
291 /* Read and verify 0xaa pattern in sector 1 */
292 memset(cmpbuf
, 0xaa, len
);
294 status
= send_dma_request(CMD_READ_DMA
, 1, 1, prdt
, ARRAY_SIZE(prdt
));
295 g_assert_cmphex(status
, ==, BM_STS_INTR
);
296 assert_bit_clear(inb(IDE_BASE
+ reg_status
), DF
| ERR
);
298 memread(guest_buf
, buf
, len
);
299 g_assert(memcmp(buf
, cmpbuf
, len
) == 0);
306 static void test_bmdma_short_prdt(void)
313 .size
= cpu_to_le32(0x10 | PRDT_EOT
),
318 status
= send_dma_request(CMD_READ_DMA
, 0, 1,
319 prdt
, ARRAY_SIZE(prdt
));
320 g_assert_cmphex(status
, ==, 0);
321 assert_bit_clear(inb(IDE_BASE
+ reg_status
), DF
| ERR
);
323 /* Abort the request before it completes */
324 status
= send_dma_request(CMD_READ_DMA
| CMDF_ABORT
, 0, 1,
325 prdt
, ARRAY_SIZE(prdt
));
326 g_assert_cmphex(status
, ==, 0);
327 assert_bit_clear(inb(IDE_BASE
+ reg_status
), DF
| ERR
);
330 static void test_bmdma_long_prdt(void)
337 .size
= cpu_to_le32(0x1000 | PRDT_EOT
),
342 status
= send_dma_request(CMD_READ_DMA
, 0, 1,
343 prdt
, ARRAY_SIZE(prdt
));
344 g_assert_cmphex(status
, ==, BM_STS_ACTIVE
| BM_STS_INTR
);
345 assert_bit_clear(inb(IDE_BASE
+ reg_status
), DF
| ERR
);
347 /* Abort the request before it completes */
348 status
= send_dma_request(CMD_READ_DMA
| CMDF_ABORT
, 0, 1,
349 prdt
, ARRAY_SIZE(prdt
));
350 g_assert_cmphex(status
, ==, BM_STS_INTR
);
351 assert_bit_clear(inb(IDE_BASE
+ reg_status
), DF
| ERR
);
354 static void test_bmdma_setup(void)
358 "-drive file=%s,if=ide,serial=%s,cache=writeback "
359 "-global ide-hd.ver=%s",
360 tmp_path
, "testdisk", "version");
363 static void test_bmdma_teardown(void)
368 static void string_cpu_to_be16(uint16_t *s
, size_t bytes
)
370 g_assert((bytes
& 1) == 0);
374 *s
= cpu_to_be16(*s
);
379 static void test_identify(void)
388 "-drive file=%s,if=ide,serial=%s,cache=writeback "
389 "-global ide-hd.ver=%s",
390 tmp_path
, "testdisk", "version");
392 /* IDENTIFY command on device 0*/
393 outb(IDE_BASE
+ reg_device
, 0);
394 outb(IDE_BASE
+ reg_command
, CMD_IDENTIFY
);
396 /* Read in the IDENTIFY buffer and check registers */
397 data
= inb(IDE_BASE
+ reg_device
);
398 g_assert_cmpint(data
& DEV
, ==, 0);
400 for (i
= 0; i
< 256; i
++) {
401 data
= inb(IDE_BASE
+ reg_status
);
402 assert_bit_set(data
, DRDY
| DRQ
);
403 assert_bit_clear(data
, BSY
| DF
| ERR
);
405 ((uint16_t*) buf
)[i
] = inw(IDE_BASE
+ reg_data
);
408 data
= inb(IDE_BASE
+ reg_status
);
409 assert_bit_set(data
, DRDY
);
410 assert_bit_clear(data
, BSY
| DF
| ERR
| DRQ
);
412 /* Check serial number/version in the buffer */
413 string_cpu_to_be16(&buf
[10], 20);
414 ret
= memcmp(&buf
[10], "testdisk ", 20);
417 string_cpu_to_be16(&buf
[23], 8);
418 ret
= memcmp(&buf
[23], "version ", 8);
421 /* Write cache enabled bit */
422 assert_bit_set(buf
[85], 0x20);
427 int main(int argc
, char **argv
)
429 const char *arch
= qtest_get_arch();
433 /* Check architecture */
434 if (strcmp(arch
, "i386") && strcmp(arch
, "x86_64")) {
435 g_test_message("Skipping test for non-x86\n");
439 /* Create a temporary raw image */
440 fd
= mkstemp(tmp_path
);
442 ret
= ftruncate(fd
, TEST_IMAGE_SIZE
);
447 g_test_init(&argc
, &argv
, NULL
);
449 qtest_add_func("/ide/identify", test_identify
);
451 qtest_add_func("/ide/bmdma/setup", test_bmdma_setup
);
452 qtest_add_func("/ide/bmdma/simple_rw", test_bmdma_simple_rw
);
453 qtest_add_func("/ide/bmdma/short_prdt", test_bmdma_short_prdt
);
454 qtest_add_func("/ide/bmdma/long_prdt", test_bmdma_long_prdt
);
455 qtest_add_func("/ide/bmdma/teardown", test_bmdma_teardown
);