xref: /openbsd/usr.sbin/vmd/pci.h (revision 09467b48)
1 /*	$OpenBSD: pci.h,v 1.7 2017/09/17 23:07:56 pd 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 union pci_dev {
36 	uint32_t pd_cfg_space[PCI_CONFIG_SPACE_SIZE / 4];
37 
38 	struct {
39 		uint16_t pd_vid;
40 		uint16_t pd_did;
41 		uint16_t pd_cmd;
42 		uint16_t pd_status;
43 		uint8_t pd_rev;
44 		uint8_t pd_prog_if;
45 		uint8_t pd_subclass;
46 		uint8_t pd_class;
47 		uint8_t pd_cache_size;
48 		uint8_t pd_lat_timer;
49 		uint8_t pd_header_type;
50 		uint8_t pd_bist;
51 		uint32_t pd_bar[PCI_MAX_BARS];
52 		uint32_t pd_cardbus_cis;
53 		uint16_t pd_subsys_vid;
54 		uint16_t pd_subsys_id;
55 		uint32_t pd_exp_rom_addr;
56 		uint8_t pd_cap;
57 		uint32_t pd_reserved0 : 24;
58 		uint32_t pd_reserved1;
59 		uint8_t pd_irq;
60 		uint8_t pd_int;
61 		uint8_t pd_min_grant;
62 		uint8_t pd_max_grant;
63 
64 		uint8_t pd_bar_ct;
65 		pci_cs_fn_t pd_csfunc;
66 
67 		uint8_t pd_bartype[PCI_MAX_BARS];
68 		uint32_t pd_barsize[PCI_MAX_BARS];
69 		void *pd_barfunc[PCI_MAX_BARS];
70 		void *pd_bar_cookie[PCI_MAX_BARS];
71 	} __packed;
72 };
73 
74 struct pci {
75 	uint8_t pci_dev_ct;
76 	uint64_t pci_next_mmio_bar;
77 	uint64_t pci_next_io_bar;
78 	uint8_t pci_next_pic_irq;
79 	uint32_t pci_addr_reg;
80 	uint32_t pci_data_reg;
81 
82 	union pci_dev pci_devices[PCI_CONFIG_MAX_DEV];
83 };
84 
85 void pci_handle_address_reg(struct vm_run_params *);
86 void pci_handle_data_reg(struct vm_run_params *);
87 uint8_t pci_handle_io(struct vm_run_params *);
88 void pci_init(void);
89 int pci_add_device(uint8_t *, uint16_t, uint16_t, uint8_t, uint8_t, uint16_t,
90     uint16_t, uint8_t, pci_cs_fn_t);
91 int pci_add_bar(uint8_t, uint32_t, void *, void *);
92 int pci_set_bar_fn(uint8_t, uint8_t, void *, void *);
93 uint8_t pci_get_dev_irq(uint8_t);
94 int pci_dump(int);
95 int pci_restore(int);
96