xref: /openbsd/usr.sbin/vmd/pci.h (revision 097a140d)
1 /*	$OpenBSD: pci.h,v 1.8 2020/09/08 20:09:43 kettenis Exp $	*/
2 
3 /*
4  * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #define PCI_MODE1_ENABLE	0x80000000UL
20 #define PCI_MODE1_ADDRESS_REG	0x0cf8
21 #define PCI_MODE1_DATA_REG	0x0cfc
22 #define PCI_CONFIG_MAX_DEV	32
23 #define PCI_MAX_BARS		6
24 
25 #define PCI_BAR_TYPE_IO		0x0
26 #define PCI_BAR_TYPE_MMIO	0x1
27 
28 #define PCI_MAX_PIC_IRQS	10
29 
30 typedef int (*pci_cs_fn_t)(int dir, uint8_t reg, uint32_t *data);
31 typedef int (*pci_iobar_fn_t)(int dir, uint16_t reg, uint32_t *data, uint8_t *,
32     void *, uint8_t);
33 typedef int (*pci_mmiobar_fn_t)(int dir, uint32_t ofs, uint32_t *data);
34 
35 
36 struct pci_dev {
37 	union {
38 		uint32_t pd_cfg_space[PCI_CONFIG_SPACE_SIZE / 4];
39 		struct {
40 			uint16_t pd_vid;
41 			uint16_t pd_did;
42 			uint16_t pd_cmd;
43 			uint16_t pd_status;
44 			uint8_t pd_rev;
45 			uint8_t pd_prog_if;
46 			uint8_t pd_subclass;
47 			uint8_t pd_class;
48 			uint8_t pd_cache_size;
49 			uint8_t pd_lat_timer;
50 			uint8_t pd_header_type;
51 			uint8_t pd_bist;
52 			uint32_t pd_bar[PCI_MAX_BARS];
53 			uint32_t pd_cardbus_cis;
54 			uint16_t pd_subsys_vid;
55 			uint16_t pd_subsys_id;
56 			uint32_t pd_exp_rom_addr;
57 			uint8_t pd_cap;
58 			uint32_t pd_reserved0 : 24;
59 			uint32_t pd_reserved1;
60 			uint8_t pd_irq;
61 			uint8_t pd_int;
62 			uint8_t pd_min_grant;
63 			uint8_t pd_max_grant;
64 		} __packed;
65 	};
66 	uint8_t pd_bar_ct;
67 	pci_cs_fn_t pd_csfunc;
68 
69 	uint8_t pd_bartype[PCI_MAX_BARS];
70 	uint32_t pd_barsize[PCI_MAX_BARS];
71 	void *pd_barfunc[PCI_MAX_BARS];
72 	void *pd_bar_cookie[PCI_MAX_BARS];
73 };
74 
75 struct pci {
76 	uint8_t pci_dev_ct;
77 	uint64_t pci_next_mmio_bar;
78 	uint64_t pci_next_io_bar;
79 	uint8_t pci_next_pic_irq;
80 	uint32_t pci_addr_reg;
81 	uint32_t pci_data_reg;
82 
83 	struct pci_dev pci_devices[PCI_CONFIG_MAX_DEV];
84 };
85 
86 void pci_handle_address_reg(struct vm_run_params *);
87 void pci_handle_data_reg(struct vm_run_params *);
88 uint8_t pci_handle_io(struct vm_run_params *);
89 void pci_init(void);
90 int pci_add_device(uint8_t *, uint16_t, uint16_t, uint8_t, uint8_t, uint16_t,
91     uint16_t, uint8_t, pci_cs_fn_t);
92 int pci_add_bar(uint8_t, uint32_t, void *, void *);
93 int pci_set_bar_fn(uint8_t, uint8_t, void *, void *);
94 uint8_t pci_get_dev_irq(uint8_t);
95 int pci_dump(int);
96 int pci_restore(int);
97