xref: /openbsd-src/usr.sbin/vmd/pci.h (revision 4e1ee0786f11cc571bd0be17d38e46f635c719fc)
1 /*	$OpenBSD: pci.h,v 1.9 2021/06/16 16:55:02 dv 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 #include <sys/types.h>
20 
21 #include <dev/pci/pcireg.h>
22 
23 #include "vmd.h"
24 
25 #ifndef _PCI_H_
26 #define _PCI_H_
27 
28 #define PCI_MODE1_ENABLE	0x80000000UL
29 #define PCI_MODE1_ADDRESS_REG	0x0cf8
30 #define PCI_MODE1_DATA_REG	0x0cfc
31 #define PCI_CONFIG_MAX_DEV	32
32 #define PCI_MAX_BARS		6
33 
34 #define PCI_BAR_TYPE_IO		0x0
35 #define PCI_BAR_TYPE_MMIO	0x1
36 
37 #define PCI_MAX_PIC_IRQS	10
38 
39 typedef int (*pci_cs_fn_t)(int dir, uint8_t reg, uint32_t *data);
40 typedef int (*pci_iobar_fn_t)(int dir, uint16_t reg, uint32_t *data, uint8_t *,
41     void *, uint8_t);
42 typedef int (*pci_mmiobar_fn_t)(int dir, uint32_t ofs, uint32_t *data);
43 
44 
45 struct pci_dev {
46 	union {
47 		uint32_t pd_cfg_space[PCI_CONFIG_SPACE_SIZE / 4];
48 		struct {
49 			uint16_t pd_vid;
50 			uint16_t pd_did;
51 			uint16_t pd_cmd;
52 			uint16_t pd_status;
53 			uint8_t pd_rev;
54 			uint8_t pd_prog_if;
55 			uint8_t pd_subclass;
56 			uint8_t pd_class;
57 			uint8_t pd_cache_size;
58 			uint8_t pd_lat_timer;
59 			uint8_t pd_header_type;
60 			uint8_t pd_bist;
61 			uint32_t pd_bar[PCI_MAX_BARS];
62 			uint32_t pd_cardbus_cis;
63 			uint16_t pd_subsys_vid;
64 			uint16_t pd_subsys_id;
65 			uint32_t pd_exp_rom_addr;
66 			uint8_t pd_cap;
67 			uint32_t pd_reserved0 : 24;
68 			uint32_t pd_reserved1;
69 			uint8_t pd_irq;
70 			uint8_t pd_int;
71 			uint8_t pd_min_grant;
72 			uint8_t pd_max_grant;
73 		} __packed;
74 	};
75 	uint8_t pd_bar_ct;
76 	pci_cs_fn_t pd_csfunc;
77 
78 	uint8_t pd_bartype[PCI_MAX_BARS];
79 	uint32_t pd_barsize[PCI_MAX_BARS];
80 	void *pd_barfunc[PCI_MAX_BARS];
81 	void *pd_bar_cookie[PCI_MAX_BARS];
82 };
83 
84 struct pci {
85 	uint8_t pci_dev_ct;
86 	uint64_t pci_next_mmio_bar;
87 	uint64_t pci_next_io_bar;
88 	uint8_t pci_next_pic_irq;
89 	uint32_t pci_addr_reg;
90 	uint32_t pci_data_reg;
91 
92 	struct pci_dev pci_devices[PCI_CONFIG_MAX_DEV];
93 };
94 
95 void pci_handle_address_reg(struct vm_run_params *);
96 void pci_handle_data_reg(struct vm_run_params *);
97 uint8_t pci_handle_io(struct vm_run_params *);
98 void pci_init(void);
99 int pci_add_device(uint8_t *, uint16_t, uint16_t, uint8_t, uint8_t, uint16_t,
100     uint16_t, uint8_t, pci_cs_fn_t);
101 int pci_add_bar(uint8_t, uint32_t, void *, void *);
102 int pci_set_bar_fn(uint8_t, uint8_t, void *, void *);
103 uint8_t pci_get_dev_irq(uint8_t);
104 int pci_dump(int);
105 int pci_restore(int);
106 
107 #endif /* _PCI_H_ */
108