Skip to content

Commit c4b325e

Browse files
committed
Replace BAR flags with unified layout parameter
This change updates pci_set_bar() to accept a single `layout` bitmask instead of separate boolean flags, simplifying BAR configuration. The new `layout` argument encodes I/O vs. memory space, 32-/64-bit decoding, and prefetchable settings via standard PCI_BASE_ADDRESS_* macros. Existing callers can now pass any combination of: - PCI_BASE_ADDRESS_SPACE_IO or PCI_BASE_ADDRESS_SPACE_MEMORY - PCI_BASE_ADDRESS_MEM_TYPE_32 or PCI_BASE_ADDRESS_MEM_TYPE_64 - PCI_BASE_ADDRESS_MEM_PREFETCH The function writes the full layout to the BAR, derives `bar_is_io_space` from bit[0], and initializes the region with the provided callback. Docstrings updated with Doxygen examples illustrating MMIO and port I/O. BREAKING CHANGE: pci_set_bar() signature changed; call sites must be updated to pass the new `layout` parameter rather than separate flags.
1 parent 93f1fee commit c4b325e

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

src/pci.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,13 @@ static void pci_mmio_io(void *owner,
145145
void pci_set_bar(struct pci_dev *dev,
146146
uint8_t bar,
147147
uint32_t bar_size,
148-
bool is_io_space,
148+
uint32_t layout,
149149
dev_io_fn do_io)
150150
{
151-
/* TODO: mem type, prefetch */
152151
/* FIXME: bar_size must be power of 2 */
153-
PCI_HDR_WRITE(dev->hdr, PCI_BAR_OFFSET(bar), is_io_space, 32);
152+
PCI_HDR_WRITE(dev->hdr, PCI_BAR_OFFSET(bar), layout, 32);
154153
dev->bar_size[bar] = bar_size;
155-
dev->bar_is_io_space[bar] = is_io_space;
154+
dev->bar_is_io_space[bar] = layout & 0x1U; // Get the bit[0] of layout
156155
dev_init(&dev->space_dev[bar], 0, bar_size, dev, do_io);
157156
}
158157

src/pci.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,39 @@ struct pci {
4646
struct dev pci_mmio_dev;
4747
};
4848

49+
/**
50+
* @brief Configure and initialize a PCI Base Address Register (BAR).
51+
*
52+
* This writes the caller-provided layout bitmask into the BAR register
53+
* in the PCI configuration header, records the region size and I/O type,
54+
* and sets up the address space (MMIO or port I/O) with the specified
55+
* callback.
56+
*
57+
* @param dev Pointer to the pci_dev representing the device.
58+
* @param bar BAR index to program (0–5 in a standard PCI header).
59+
* @param bar_size Size of the BAR region in bytes (must be a power of two).
60+
* @param layout Bitmask of PCI_BASE_ADDRESS_* flags defined in
61+
* `/usr/include/linux/pci_regs.h`:
62+
* - Bit 0: I/O space (1) vs. memory space (0)
63+
* (`PCI_BASE_ADDRESS_SPACE_IO` or
64+
* `PCI_BASE_ADDRESS_SPACE_MEMORY`)
65+
* - Bits [2:1]: Memory decoding type
66+
* (`PCI_BASE_ADDRESS_MEM_TYPE_32` or
67+
* `PCI_BASE_ADDRESS_MEM_TYPE_64`)
68+
* - Bit 3: Prefetchable flag for memory
69+
* (`PCI_BASE_ADDRESS_MEM_PREFETCH`)
70+
* @param do_io Callback (dev_io_fn) invoked on accesses within
71+
* the BAR region.
72+
*
73+
* @note bar_size must be a power of two for correct decoding by the
74+
* PCI framework.
75+
* @note For 64-bit memory BARs, callers must reserve the next BAR index
76+
* (n+1) for the high 32 bits if required by the platform.
77+
*/
4978
void pci_set_bar(struct pci_dev *dev,
5079
uint8_t bar,
5180
uint32_t bar_size,
52-
bool is_io_space,
81+
uint32_t is_io_space,
5382
dev_io_fn do_io);
5483
void pci_set_status(struct pci_dev *dev, uint16_t status);
5584
void pci_dev_register(struct pci_dev *dev);

src/virtio-pci.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,9 @@ void virtio_pci_init(struct virtio_pci_dev *dev,
268268
PCI_HDR_WRITE(dev->pci_dev.hdr, PCI_HEADER_TYPE, PCI_HEADER_TYPE_NORMAL, 8);
269269
PCI_HDR_WRITE(dev->pci_dev.hdr, PCI_INTERRUPT_PIN, 1, 8);
270270
pci_set_status(&dev->pci_dev, PCI_STATUS_CAP_LIST | PCI_STATUS_INTERRUPT);
271-
pci_set_bar(&dev->pci_dev, 0, 0x100, PCI_BASE_ADDRESS_SPACE_MEMORY,
271+
pci_set_bar(&dev->pci_dev, 0, 0x100,
272+
PCI_BASE_ADDRESS_SPACE_MEMORY | PCI_BASE_ADDRESS_MEM_TYPE_32
273+
/* | PCI_BASE_ADDRESS_MEM_PREFETCH */,
272274
virtio_pci_space_io);
273275
virtio_pci_set_cap(dev, cap_list);
274276
dev->device_feature |=

0 commit comments

Comments
 (0)