xref: /netbsd/sys/external/bsd/drm2/include/linux/pci.h (revision 1a08e23f)
1 /*	$NetBSD: pci.h,v 1.43 2021/12/19 01:23:01 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Taylor R. Campbell.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef _LINUX_PCI_H_
33 #define _LINUX_PCI_H_
34 
35 #ifdef _KERNEL_OPT
36 #if defined(i386) || defined(amd64) || defined(__aarch64__)
37 #include "acpica.h"
38 #else	/* !(i386 || amd64) */
39 #define NACPICA	0
40 #endif	/* i386 || amd64 */
41 #endif
42 
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/bus.h>
46 #include <sys/cdefs.h>
47 #include <sys/kmem.h>
48 #include <sys/systm.h>
49 
50 #include <machine/limits.h>
51 
52 #include <dev/pci/pcidevs.h>
53 #include <dev/pci/pcireg.h>
54 #include <dev/pci/pcivar.h>
55 #include <dev/pci/agpvar.h>
56 
57 #if NACPICA > 0
58 #include <dev/acpi/acpivar.h>
59 #include <dev/acpi/acpi_pci.h>
60 #else
61 struct acpi_devnode;
62 #endif
63 
64 #include <linux/device.h>
65 #include <linux/dma-mapping.h>
66 #include <linux/errno.h>
67 #include <linux/io.h>
68 #include <linux/interrupt.h>
69 #include <linux/ioport.h>
70 #include <linux/kernel.h>
71 
72 struct pci_driver;
73 
74 struct pci_bus {
75 	/* NetBSD private members */
76 	pci_chipset_tag_t	pb_pc;
77 	device_t		pb_dev;
78 
79 	/* Linux API */
80 	u_int			number;
81 };
82 
83 struct pci_device_id {
84 	uint32_t	vendor;
85 	uint32_t	device;
86 	uint32_t	subvendor;
87 	uint32_t	subdevice;
88 	uint32_t	class;
89 	uint32_t	class_mask;
90 	unsigned long	driver_data;
91 };
92 
93 #define	PCI_ANY_ID		(~0)
94 
95 #define	PCI_BASE_CLASS_DISPLAY	PCI_CLASS_DISPLAY
96 
97 #define	PCI_CLASS_DISPLAY_VGA						\
98 	((PCI_CLASS_DISPLAY << 8) | PCI_SUBCLASS_DISPLAY_VGA)
99 #define	PCI_CLASS_BRIDGE_ISA						\
100 	((PCI_CLASS_BRIDGE << 8) | PCI_SUBCLASS_BRIDGE_ISA)
101 CTASSERT(PCI_CLASS_BRIDGE_ISA == 0x0601);
102 
103 /* XXX This is getting silly...  */
104 #define	PCI_VENDOR_ID_APPLE	PCI_VENDOR_APPLE
105 #define	PCI_VENDOR_ID_ASUSTEK	PCI_VENDOR_ASUSTEK
106 #define	PCI_VENDOR_ID_ATI	PCI_VENDOR_ATI
107 #define	PCI_VENDOR_ID_DELL	PCI_VENDOR_DELL
108 #define	PCI_VENDOR_ID_IBM	PCI_VENDOR_IBM
109 #define	PCI_VENDOR_ID_HP	PCI_VENDOR_HP
110 #define	PCI_VENDOR_ID_INTEL	PCI_VENDOR_INTEL
111 #define	PCI_VENDOR_ID_NVIDIA	PCI_VENDOR_NVIDIA
112 #define	PCI_VENDOR_ID_SI	PCI_VENDOR_SIS
113 #define	PCI_VENDOR_ID_SONY	PCI_VENDOR_SONY
114 #define	PCI_VENDOR_ID_VIA	PCI_VENDOR_VIATECH
115 
116 #define	PCI_SUBVENDOR_ID_REDHAT_QUMRANET	0x1af4
117 
118 #define	PCI_DEVICE_ID_ATI_RADEON_QY	PCI_PRODUCT_ATI_RADEON_RV100_QY
119 
120 #define	PCI_SUBDEVICE_ID_QEMU		0x1100
121 
122 #define	PCI_DEVFN(DEV, FN)						\
123 	(__SHIFTIN((DEV), __BITS(3, 7)) | __SHIFTIN((FN), __BITS(0, 2)))
124 #define	PCI_SLOT(DEVFN)		__SHIFTOUT((DEVFN), __BITS(3, 7))
125 #define	PCI_FUNC(DEVFN)		__SHIFTOUT((DEVFN), __BITS(0, 2))
126 
127 #define	PCI_NUM_RESOURCES	((PCI_MAPREG_END - PCI_MAPREG_START) / 4)
128 #define	DEVICE_COUNT_RESOURCE	PCI_NUM_RESOURCES
129 
130 #define	PCI_CAP_ID_AGP	PCI_CAP_AGP
131 
132 typedef int pci_power_t;
133 
134 #define	PCI_D0		0
135 #define	PCI_D1		1
136 #define	PCI_D2		2
137 #define	PCI_D3hot	3
138 #define	PCI_D3cold	4
139 
140 #define	__pci_iomem
141 
142 struct pci_dev {
143 	struct pci_attach_args	pd_pa;
144 	int			pd_kludges;	/* Gotta lose 'em...  */
145 #define	NBPCI_KLUDGE_GET_MUMBLE	0x01
146 #define	NBPCI_KLUDGE_MAP_ROM	0x02
147 	bus_space_tag_t		pd_rom_bst;
148 	bus_space_handle_t	pd_rom_bsh;
149 	bus_size_t		pd_rom_size;
150 	bus_space_handle_t	pd_rom_found_bsh;
151 	bus_size_t		pd_rom_found_size;
152 	void			*pd_rom_vaddr;
153 	device_t		pd_dev;
154 	void			*pd_drvdata;
155 	struct {
156 		pcireg_t		type;
157 		bus_addr_t		addr;
158 		bus_size_t		size;
159 		int			flags;
160 		bus_space_tag_t		bst;
161 		bus_space_handle_t	bsh;
162 		void __pci_iomem	*kva;
163 		bool			mapped;
164 	}			pd_resources[PCI_NUM_RESOURCES];
165 	struct pci_conf_state	*pd_saved_state;
166 	struct acpi_devnode	*pd_ad;
167 	pci_intr_handle_t	*pd_intr_handles;
168 	unsigned		pd_enablecnt;
169 
170 	/* Linx API only below */
171 	struct pci_bus		*bus;
172 	uint32_t		devfn;
173 	uint16_t		vendor;
174 	uint16_t		device;
175 	uint16_t		subsystem_vendor;
176 	uint16_t		subsystem_device;
177 	uint8_t			revision;
178 	uint32_t		class;
179 	bool			msi_enabled;
180 	bool			no_64bit_msi;
181 };
182 
183 #define	PCIBIOS_MIN_MEM	0x100000	/* XXX bogus x86 kludge bollocks */
184 
185 #define	__pci_rom_iomem
186 
187 /* Namespace.  */
188 #define	pci_bus_alloc_resource		linux_pci_bus_alloc_resource
189 #define	pci_bus_read_config_byte	linux_pci_bus_read_config_byte
190 #define	pci_bus_read_config_dword	linux_pci_bus_read_config_dword
191 #define	pci_bus_read_config_word	linux_pci_bus_read_config_word
192 #define	pci_bus_write_config_byte	linux_pci_bus_write_config_byte
193 #define	pci_bus_write_config_dword	linux_pci_bus_write_config_dword
194 #define	pci_bus_write_config_word	linux_pci_bus_write_config_word
195 #define	pci_clear_master		linux_pci_clear_master
196 #define	pci_dev_dev			linux_pci_dev_dev
197 #define	pci_dev_put			linux_pci_dev_put
198 #define	pci_disable_msi			linux_pci_disable_msi
199 #define	pci_disable_rom			linux_pci_disable_rom
200 #define	pci_dma_supported		linux_pci_dma_supported
201 #define	pci_domain_nr			linux_pci_domain_nr
202 #define	pci_enable_msi			linux_pci_enable_msi
203 #define	pci_enable_rom			linux_pci_enable_rom
204 #define	pci_find_capability		linux_pci_find_capability
205 #define	pci_get_class			linux_pci_get_class
206 #define	pci_get_domain_bus_and_slot	linux_pci_get_domain_bus_and_slot
207 #define	pci_get_drvdata			linux_pci_get_drvdata
208 #define	pci_iomap			linux_pci_iomap
209 #define	pci_iounmap			linux_pci_iounmap
210 #define	pci_is_pcie			linux_pci_is_pcie
211 #define	pci_is_root_bus			linux_pci_is_root_bus
212 #define	pci_map_rom			linux_pci_map_rom
213 #define	pci_platform_rom		linux_pci_platform_rom
214 #define	pci_read_config_byte		linux_pci_read_config_byte
215 #define	pci_read_config_dword		linux_pci_read_config_dword
216 #define	pci_read_config_word		linux_pci_read_config_word
217 #define	pci_resource_end		linux_pci_resource_end
218 #define	pci_resource_flags		linux_pci_resource_flags
219 #define	pci_resource_len		linux_pci_resource_len
220 #define	pci_resource_start		linux_pci_resource_start
221 #define	pci_restore_state		linux_pci_restore_state
222 #define	pci_save_state			linux_pci_save_state
223 #define	pci_set_drvdata			linux_pci_set_drvdata
224 #define	pci_set_master			linux_pci_set_master
225 #define	pci_unmap_rom			linux_pci_unmap_rom
226 #define	pci_write_config_byte		linux_pci_write_config_byte
227 #define	pci_write_config_dword		linux_pci_write_config_dword
228 #define	pci_write_config_word		linux_pci_write_config_word
229 #define	pcibios_align_resource		linux_pcibios_align_resource
230 
231 /* NetBSD local additions.  */
232 void		linux_pci_dev_init(struct pci_dev *, device_t, device_t,
233 		    const struct pci_attach_args *, int);
234 void		linux_pci_dev_destroy(struct pci_dev *);
235 
236 /* NetBSD no-renames because use requires review.  */
237 int		linux_pci_enable_device(struct pci_dev *);
238 void		linux_pci_disable_device(struct pci_dev *);
239 
240 bool		pci_is_root_bus(struct pci_bus *);
241 int		pci_domain_nr(struct pci_bus *);
242 
243 device_t	pci_dev_dev(struct pci_dev *);
244 void		pci_set_drvdata(struct pci_dev *, void *);
245 void *		pci_get_drvdata(struct pci_dev *);
246 
247 int		pci_find_capability(struct pci_dev *, int);
248 bool		pci_is_pcie(struct pci_dev *);
249 bool		pci_dma_supported(struct pci_dev *, uintmax_t);
250 
251 int		pci_read_config_dword(struct pci_dev *, int, uint32_t *);
252 int		pci_read_config_word(struct pci_dev *, int, uint16_t *);
253 int		pci_read_config_byte(struct pci_dev *, int, uint8_t *);
254 int		pci_write_config_dword(struct pci_dev *, int, uint32_t);
255 int		pci_write_config_word(struct pci_dev *, int, uint16_t);
256 int		pci_write_config_byte(struct pci_dev *, int, uint8_t);
257 
258 int		pci_bus_read_config_dword(struct pci_bus *, unsigned, int,
259 		    uint32_t *);
260 int		pci_bus_read_config_word(struct pci_bus *, unsigned, int,
261 		    uint16_t *);
262 int		pci_bus_read_config_byte(struct pci_bus *, unsigned, int,
263 		    uint8_t *);
264 int		pci_bus_write_config_dword(struct pci_bus *, unsigned, int,
265 		    uint32_t);
266 int		pci_bus_write_config_word(struct pci_bus *, unsigned, int,
267 		    uint16_t);
268 int		pci_bus_write_config_byte(struct pci_bus *, unsigned, int,
269 		    uint8_t);
270 
271 int		pci_enable_msi(struct pci_dev *);
272 void		pci_disable_msi(struct pci_dev *);
273 void		pci_set_master(struct pci_dev *);
274 void		pci_clear_master(struct pci_dev *);
275 
276 bus_addr_t	pcibios_align_resource(void *, const struct resource *,
277 		    bus_addr_t, bus_size_t);
278 int		pci_bus_alloc_resource(struct pci_bus *, struct resource *,
279 		    bus_size_t, bus_size_t, bus_addr_t, int,
280 		    bus_addr_t (*)(void *, const struct resource *, bus_addr_t,
281 			bus_size_t), struct pci_dev *);
282 
283 /* XXX Kludges only -- do not use without checking the implementation!  */
284 struct pci_dev *pci_get_domain_bus_and_slot(int, int, int);
285 struct pci_dev *pci_get_class(uint32_t, struct pci_dev *); /* i915 kludge */
286 void		pci_dev_put(struct pci_dev *);
287 
288 void __pci_rom_iomem *
289 		pci_map_rom(struct pci_dev *, size_t *);
290 void __pci_rom_iomem *
291 		pci_platform_rom(struct pci_dev *, size_t *);
292 void		pci_unmap_rom(struct pci_dev *, void __pci_rom_iomem *);
293 int		pci_enable_rom(struct pci_dev *);
294 void		pci_disable_rom(struct pci_dev *);
295 
296 bus_addr_t	pci_resource_start(struct pci_dev *, unsigned);
297 bus_size_t	pci_resource_len(struct pci_dev *, unsigned);
298 bus_addr_t	pci_resource_end(struct pci_dev *, unsigned);
299 int		pci_resource_flags(struct pci_dev *, unsigned);
300 
301 void __pci_iomem *
302 		pci_iomap(struct pci_dev *, unsigned, bus_size_t);
303 void		pci_iounmap(struct pci_dev *, void __pci_iomem *);
304 
305 void		pci_save_state(struct pci_dev *);
306 void		pci_restore_state(struct pci_dev *);
307 
308 #endif  /* _LINUX_PCI_H_ */
309