xref: /kvm-unit-tests/lib/pci.c (revision 9ae19a633c0484a56df69cd2d4487242f3f27587)
1 /*
2  * Copyright (C) 2013, Red Hat Inc, Michael S. Tsirkin <mst@redhat.com>
3  *
4  * This work is licensed under the terms of the GNU LGPL, version 2.
5  */
6 #include <linux/pci_regs.h>
7 #include "pci.h"
8 #include "asm/pci.h"
9 
10 /* Scan bus look for a specific device. Only bus 0 scanned for now. */
11 pcidevaddr_t pci_find_dev(uint16_t vendor_id, uint16_t device_id)
12 {
13     unsigned dev;
14     for (dev = 0; dev < 256; ++dev) {
15     uint32_t id = pci_config_read(dev, 0);
16     if ((id & 0xFFFF) == vendor_id && (id >> 16) == device_id) {
17         return dev;
18     }
19     }
20     return PCIDEVADDR_INVALID;
21 }
22 
23 unsigned long pci_bar_addr(pcidevaddr_t dev, int bar_num)
24 {
25     uint32_t bar = pci_config_read(dev, PCI_BASE_ADDRESS_0 + bar_num * 4);
26     if (bar & PCI_BASE_ADDRESS_SPACE_IO) {
27         return bar & PCI_BASE_ADDRESS_IO_MASK;
28     } else {
29         return bar & PCI_BASE_ADDRESS_MEM_MASK;
30     }
31 }
32 
33 bool pci_bar_is_memory(pcidevaddr_t dev, int bar_num)
34 {
35     uint32_t bar = pci_config_read(dev, PCI_BASE_ADDRESS_0 + bar_num * 4);
36     return !(bar & PCI_BASE_ADDRESS_SPACE_IO);
37 }
38 
39 bool pci_bar_is_valid(pcidevaddr_t dev, int bar_num)
40 {
41     uint32_t bar = pci_config_read(dev, PCI_BASE_ADDRESS_0 + bar_num * 4);
42     return bar;
43 }
44