AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
pci.h
Go to the documentation of this file.
1#ifndef _DEV_PCI_H
2#define _DEV_PCI_H
3
4#include <core/system.h>
5
6struct pci_dev;
7
8#include <dev/dev.h>
9#include <cpu/io.h>
10#include <dev/irq.h>
11
12/*
13 * The PCI bus (dev/pci/): finding devices, their configuration space,
14 * their BARs, and their interrupts. See docs/kernel/devices.md.
15 */
16
23
24extern struct dev pcidev;
25
32struct pci_host {
33 const char *name;
34
35 /* the configuration space: ports (mechanism #1, conf1.c), or memory
36 * (ECAM, ecam.c: `size` bytes from `config`, which pci_host_set()
37 * maps) */
38 struct ioaddr config;
39 size_t size;
40 uint32_t (*read)(struct pci_host *host, struct pci_dev *dev, uint16_t reg);
41 void (*write)(struct pci_host *host, struct pci_dev *dev, uint16_t reg, uint32_t val);
42
43 /* the IRQs that INTA# to INTD# are routed to, each device's pins
44 * rotated by its slot; with none (0), a device's IRQ is what the
45 * firmware wrote in its interrupt line register */
46 int nirq;
47 unsigned irq[4];
48
49 /* where the BARs that nothing assigned go (when no firmware ran, as
50 * on the virt machine), in bus addresses; a size of 0 means the
51 * firmware assigned them */
54};
55
56/* Sets the platform's host bridge. */
57int pci_host_set(struct pci_host *host);
58
59/* dev/pci/conf1.c: configuration mechanism #1, through two ports (0xCF8) */
60uint32_t pci_conf1_read(struct pci_host *host, struct pci_dev *dev, uint16_t reg);
61void pci_conf1_write(struct pci_host *host, struct pci_dev *dev, uint16_t reg, uint32_t val);
62
63/* dev/pci/ecam.c: the Enhanced Configuration Access Mechanism, in memory */
64uint32_t pci_ecam_read(struct pci_host *host, struct pci_dev *dev, uint16_t reg);
65void pci_ecam_write(struct pci_host *host, struct pci_dev *dev, uint16_t reg, uint32_t val);
66
67/* Finding devices, and reading and writing their configuration space. */
68
69int pci_scan_device(uint8_t class, uint8_t subclass, struct pci_dev *_dev, size_t nr);
70int pci_device_scan(uint16_t vendor_id, uint16_t device_id, struct pci_dev *ref);
74void pci_reg8_write(struct pci_dev *dev, uint8_t off, uint8_t val);
75void pci_reg16_write(struct pci_dev *dev, uint8_t off, uint16_t val);
76void pci_reg32_write(struct pci_dev *dev, uint8_t off, uint32_t val);
78
81struct pci_bar {
84 int io; /* I/O ports, else memory */
85};
86
87/* Reads a BAR; turns on a device's decoding and bus mastering; returns
88 * its IRQ. */
89int pci_bar(struct pci_dev *dev, int id, struct pci_bar *bar);
90void pci_enable(struct pci_dev *dev);
91int pci_irq(struct pci_dev *dev);
92
93/* Capabilities' IDs: MSI, MSI-X. */
94#define PCI_CAP_MSI 0x05
95#define PCI_CAP_MSIX 0x11
96
97/* Finds a capability; makes the device's interrupts messages (MSI). */
99int pci_msi_enable(struct pci_dev *dev, irq_handler_t handler, void *arg);
100
101#endif /* ! _DEV_PCI_H */
void pci_conf1_write(struct pci_host *host, struct pci_dev *dev, uint16_t reg, uint32_t val)
Writes a 32-bit register of a function's configuration space.
Definition conf1.c:73
uint32_t pci_conf1_read(struct pci_host *host, struct pci_dev *dev, uint16_t reg)
Reads a 32-bit register of a function's configuration space.
Definition conf1.c:55
void pci_ecam_write(struct pci_host *host, struct pci_dev *dev, uint16_t reg, uint32_t val)
Writes a 32-bit register of a function's configuration space.
Definition ecam.c:51
uint32_t pci_ecam_read(struct pci_host *host, struct pci_dev *dev, uint16_t reg)
Reads a 32-bit register of a function's configuration space.
Definition ecam.c:32
void(*) irq_handler_t(void *arg)
Definition irq.h:21
int pci_irq(struct pci_dev *dev)
Finds the IRQ that a function's interrupt pin comes to.
Definition pci.c:367
uint16_t pci_reg16_read(struct pci_dev *dev, uint8_t off)
Reads a 16-bit configuration register.
Definition pci.c:130
uint32_t pci_reg32_read(struct pci_dev *dev, uint8_t off)
Reads a 32-bit configuration register.
Definition pci.c:142
void pci_reg32_write(struct pci_dev *dev, uint8_t off, uint32_t val)
Writes a 32-bit configuration register.
Definition pci.c:184
int pci_device_scan(uint16_t vendor_id, uint16_t device_id, struct pci_dev *ref)
Finds a device by its vendor and device IDs, on bus 0.
Definition pci.c:87
int pci_scan_device(uint8_t class, uint8_t subclass, struct pci_dev *_dev, size_t nr)
Finds the functions of a class and subclass, on bus 0.
Definition pci.c:244
int pci_msi_enable(struct pci_dev *dev, irq_handler_t handler, void *arg)
Has a function send its interrupts as messages (MSI) instead of on its interrupt line: one message,...
Definition pci.c:517
uint32_t pci_read_bar(struct pci_dev *dev, uint8_t id)
Reads a base address register as it is, flag bits included.
Definition pci.c:285
int pci_host_set(struct pci_host *h)
Sets the platform's host bridge, from platform_init().
Definition pci.c:443
void pci_enable(struct pci_dev *dev)
Turns a function's memory and port decoding on, and bus mastering, so that it can do DMA.
Definition pci.c:349
void pci_reg8_write(struct pci_dev *dev, uint8_t off, uint8_t val)
Writes an 8-bit configuration register: the 32-bit register it's in is read, changed and written back...
Definition pci.c:155
void pci_reg16_write(struct pci_dev *dev, uint8_t off, uint16_t val)
Writes a 16-bit configuration register: the 32-bit register it's in is read, changed and written back...
Definition pci.c:170
struct dev pcidev
Definition pci.c:225
uint8_t pci_cap_find(struct pci_dev *dev, uint8_t id)
Finds one of a function's capabilities.
Definition pci.c:478
uint8_t pci_reg8_read(struct pci_dev *dev, uint8_t off)
Reads an 8-bit configuration register.
Definition pci.c:118
unsigned int uint32_t
Definition stddef.h:22
unsigned char uint8_t
Definition stddef.h:20
unsigned long long int uint64_t
Definition stddef.h:23
unsigned short int uint16_t
Definition stddef.h:21
A device driver: its operations, any of which may be NULL.
Definition dev.h:28
An installed handler, on its interrupt's list.
Definition irq.c:19
Where a device's registers are.
Definition io.h:95
A base address register (BAR): where the device's registers are, how big they are,...
Definition pci.h:81
uint64_t addr
Definition pci.h:82
uint64_t size
Definition pci.h:83
int io
Definition pci.h:84
A PCI function: its bus, device (slot) and function numbers.
Definition pci.h:18
uint8_t dev
Definition pci.h:20
uint8_t func
Definition pci.h:21
uint8_t bus
Definition pci.h:19
The host bridge, which the platform describes (platform_init() sets it): how its configuration space ...
Definition pci.h:32
const char * name
Definition pci.h:33
uint64_t io_size
Definition pci.h:53
uint64_t io_base
Definition pci.h:53
uint64_t mem_size
Definition pci.h:52
struct ioaddr config
Definition pci.h:38
uint32_t(*) read(struct pci_host *host, struct pci_dev *dev, uint16_t reg)
Definition pci.h:40
unsigned irq[4]
Definition pci.h:47
void(*) write(struct pci_host *host, struct pci_dev *dev, uint16_t reg, uint32_t val)
Definition pci.h:41
size_t size
Definition pci.h:39
int nirq
Definition pci.h:46
uint64_t mem_base
Definition pci.h:52