1 /*	$OpenBSD: pci.h,v 1.19 2025/02/07 03:03:31 jsg Exp $	*/
2 /*
3  * Copyright (c) 2015 Mark Kettenis
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #ifndef _LINUX_PCI_H_
19 #define _LINUX_PCI_H_
20 
21 #include <sys/types.h>
22 /* sparc64 cpu.h needs time.h and siginfo.h (indirect via param.h) */
23 #include <sys/param.h>
24 #include <machine/cpu.h>
25 
26 #include <dev/pci/pcireg.h>
27 #include <dev/pci/pcivar.h>
28 #include <dev/pci/pcidevs.h>
29 #include <uvm/uvm_extern.h>
30 
31 #include <linux/io.h>
32 #include <linux/ioport.h>
33 #include <linux/kobject.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/mod_devicetable.h>
36 #include <linux/device.h>
37 
38 struct pci_dev;
39 
40 struct pci_bus {
41 	pci_chipset_tag_t pc;
42 	unsigned char	number;
43 	int		domain_nr;
44 	pcitag_t	*bridgetag;
45 	struct pci_dev	*self;
46 };
47 
48 struct pci_acpi {
49 	struct aml_node	*node;
50 };
51 
52 struct pci_dev {
53 	struct pci_bus	_bus;
54 	struct pci_bus	*bus;
55 
56 	unsigned int	devfn;
57 	uint16_t	vendor;
58 	uint16_t	device;
59 	uint16_t	subsystem_vendor;
60 	uint16_t	subsystem_device;
61 	uint8_t		revision;
62 	uint32_t	class;		/* class:subclass:interface */
63 
64 	pci_chipset_tag_t pc;
65 	pcitag_t	tag;
66 	struct pci_softc *pci;
67 
68 	int		irq;
69 	int		msi_enabled;
70 	uint8_t		no_64bit_msi;
71 	uint8_t		ltr_path;
72 
73 	struct pci_acpi dev;
74 	struct device *_dev;
75 };
76 #define PCI_ANY_ID (uint16_t) (~0U)
77 
78 #define PCI_DEVICE(v, p)		\
79 	.vendor = (v),			\
80 	.device = (p),			\
81 	.subvendor = PCI_ANY_ID,	\
82 	.subdevice = PCI_ANY_ID
83 
84 #ifndef PCI_MEM_START
85 #define PCI_MEM_START	0
86 #endif
87 
88 #ifndef PCI_MEM_END
89 #define PCI_MEM_END	0xffffffff
90 #endif
91 
92 #ifndef PCI_MEM64_END
93 #define PCI_MEM64_END	0xffffffffffffffff
94 #endif
95 
96 #define PCI_VENDOR_ID_APPLE	PCI_VENDOR_APPLE
97 #define PCI_VENDOR_ID_ASUSTEK	PCI_VENDOR_ASUSTEK
98 #define PCI_VENDOR_ID_ATI	PCI_VENDOR_ATI
99 #define PCI_VENDOR_ID_DELL	PCI_VENDOR_DELL
100 #define PCI_VENDOR_ID_HP	PCI_VENDOR_HP
101 #define PCI_VENDOR_ID_IBM	PCI_VENDOR_IBM
102 #define PCI_VENDOR_ID_INTEL	PCI_VENDOR_INTEL
103 #define PCI_VENDOR_ID_SONY	PCI_VENDOR_SONY
104 #define PCI_VENDOR_ID_VIA	PCI_VENDOR_VIATECH
105 
106 #define PCI_DEVICE_ID_ATI_RADEON_QY	PCI_PRODUCT_ATI_RADEON_QY
107 
108 #define PCI_SUBVENDOR_ID_REDHAT_QUMRANET	0x1af4
109 #define PCI_SUBDEVICE_ID_QEMU			0x1100
110 
111 #define PCI_DEVFN(slot, func)	((slot) << 3 | (func))
112 #define PCI_SLOT(devfn)		((devfn) >> 3)
113 #define PCI_FUNC(devfn)		((devfn) & 0x7)
114 #define PCI_BUS_NUM(devfn)	(((devfn) >> 8) & 0xff)
115 
116 #define pci_dev_put(x)
117 
118 #define PCI_EXP_DEVSTA		0x0a
119 #define PCI_EXP_DEVSTA_TRPND	(1 << 5)
120 #define PCI_EXP_LNKCAP		0x0c
121 #define PCI_EXP_LNKCAP_CLKPM	(1 << 18)
122 #define PCI_EXP_LNKCTL		0x10
123 #define PCI_EXP_LNKCTL_HAWD	(1 << 9)
124 #define PCI_EXP_LNKSTA		0x12
125 #define PCI_EXP_DEVCTL2		0x28
126 #define PCI_EXP_DEVCTL2_LTR_EN	(1 << 10)
127 #define PCI_EXP_LNKCTL2		0x30
128 #define PCI_EXP_LNKCTL2_ENTER_COMP	(1 << 4)
129 #define PCI_EXP_LNKCTL2_TX_MARGIN	0x0380
130 #define PCI_EXP_LNKCTL2_TLS		PCI_PCIE_LCSR2_TLS
131 #define PCI_EXP_LNKCTL2_TLS_2_5GT	PCI_PCIE_LCSR2_TLS_2_5
132 #define PCI_EXP_LNKCTL2_TLS_5_0GT	PCI_PCIE_LCSR2_TLS_5
133 #define PCI_EXP_LNKCTL2_TLS_8_0GT	PCI_PCIE_LCSR2_TLS_8
134 
135 #define PCI_COMMAND		PCI_COMMAND_STATUS_REG
136 #define PCI_COMMAND_MEMORY	PCI_COMMAND_MEM_ENABLE
137 
138 #define PCI_PRIMARY_BUS		PCI_PRIBUS_1
139 
140 static inline int
pci_read_config_dword(struct pci_dev * pdev,int reg,u32 * val)141 pci_read_config_dword(struct pci_dev *pdev, int reg, u32 *val)
142 {
143 	*val = pci_conf_read(pdev->pc, pdev->tag, reg);
144 	return 0;
145 }
146 
147 static inline int
pci_read_config_word(struct pci_dev * pdev,int reg,u16 * val)148 pci_read_config_word(struct pci_dev *pdev, int reg, u16 *val)
149 {
150 	uint32_t v;
151 
152 	v = pci_conf_read(pdev->pc, pdev->tag, (reg & ~0x2));
153 	*val = (v >> ((reg & 0x2) * 8));
154 	return 0;
155 }
156 
157 static inline int
pci_read_config_byte(struct pci_dev * pdev,int reg,u8 * val)158 pci_read_config_byte(struct pci_dev *pdev, int reg, u8 *val)
159 {
160 	uint32_t v;
161 
162 	v = pci_conf_read(pdev->pc, pdev->tag, (reg & ~0x3));
163 	*val = (v >> ((reg & 0x3) * 8));
164 	return 0;
165 }
166 
167 static inline int
pci_write_config_dword(struct pci_dev * pdev,int reg,u32 val)168 pci_write_config_dword(struct pci_dev *pdev, int reg, u32 val)
169 {
170 	pci_conf_write(pdev->pc, pdev->tag, reg, val);
171 	return 0;
172 }
173 
174 static inline int
pci_write_config_word(struct pci_dev * pdev,int reg,u16 val)175 pci_write_config_word(struct pci_dev *pdev, int reg, u16 val)
176 {
177 	uint32_t v;
178 
179 	v = pci_conf_read(pdev->pc, pdev->tag, (reg & ~0x2));
180 	v &= ~(0xffff << ((reg & 0x2) * 8));
181 	v |= (val << ((reg & 0x2) * 8));
182 	pci_conf_write(pdev->pc, pdev->tag, (reg & ~0x2), v);
183 	return 0;
184 }
185 
186 static inline int
pci_write_config_byte(struct pci_dev * pdev,int reg,u8 val)187 pci_write_config_byte(struct pci_dev *pdev, int reg, u8 val)
188 {
189 	uint32_t v;
190 
191 	v = pci_conf_read(pdev->pc, pdev->tag, (reg & ~0x3));
192 	v &= ~(0xff << ((reg & 0x3) * 8));
193 	v |= (val << ((reg & 0x3) * 8));
194 	pci_conf_write(pdev->pc, pdev->tag, (reg & ~0x3), v);
195 	return 0;
196 }
197 
198 static inline int
pci_bus_read_config_word(struct pci_bus * bus,unsigned int devfn,int reg,u16 * val)199 pci_bus_read_config_word(struct pci_bus *bus, unsigned int devfn,
200     int reg, u16 *val)
201 {
202 	pcitag_t tag = pci_make_tag(bus->pc, bus->number,
203 	    PCI_SLOT(devfn), PCI_FUNC(devfn));
204 	uint32_t v;
205 
206 	v = pci_conf_read(bus->pc, tag, (reg & ~0x2));
207 	*val = (v >> ((reg & 0x2) * 8));
208 	return 0;
209 }
210 
211 static inline int
pci_bus_read_config_byte(struct pci_bus * bus,unsigned int devfn,int reg,u8 * val)212 pci_bus_read_config_byte(struct pci_bus *bus, unsigned int devfn,
213     int reg, u8 *val)
214 {
215 	pcitag_t tag = pci_make_tag(bus->pc, bus->number,
216 	    PCI_SLOT(devfn), PCI_FUNC(devfn));
217 	uint32_t v;
218 
219 	v = pci_conf_read(bus->pc, tag, (reg & ~0x3));
220 	*val = (v >> ((reg & 0x3) * 8));
221 	return 0;
222 }
223 
224 static inline int
pci_bus_write_config_byte(struct pci_bus * bus,unsigned int devfn,int reg,u8 val)225 pci_bus_write_config_byte(struct pci_bus *bus, unsigned int devfn,
226     int reg, u8 val)
227 {
228 	pcitag_t tag = pci_make_tag(bus->pc, bus->number,
229 	    PCI_SLOT(devfn), PCI_FUNC(devfn));
230 	uint32_t v;
231 
232 	v = pci_conf_read(bus->pc, tag, (reg & ~0x3));
233 	v &= ~(0xff << ((reg & 0x3) * 8));
234 	v |= (val << ((reg & 0x3) * 8));
235 	pci_conf_write(bus->pc, tag, (reg & ~0x3), v);
236 	return 0;
237 }
238 
239 static inline int
pci_pcie_cap(struct pci_dev * pdev)240 pci_pcie_cap(struct pci_dev *pdev)
241 {
242 	int pos;
243 	if (!pci_get_capability(pdev->pc, pdev->tag, PCI_CAP_PCIEXPRESS,
244 	    &pos, NULL))
245 		return -EINVAL;
246 	return pos;
247 }
248 
249 bool pcie_aspm_enabled(struct pci_dev *);
250 
251 static inline bool
pci_is_pcie(struct pci_dev * pdev)252 pci_is_pcie(struct pci_dev *pdev)
253 {
254 	return (pci_pcie_cap(pdev) > 0);
255 }
256 
257 static inline bool
pci_is_root_bus(struct pci_bus * pbus)258 pci_is_root_bus(struct pci_bus *pbus)
259 {
260 	return (pbus->bridgetag == NULL);
261 }
262 
263 static inline struct pci_dev *
pci_upstream_bridge(struct pci_dev * pdev)264 pci_upstream_bridge(struct pci_dev *pdev)
265 {
266 	if (pci_is_root_bus(pdev->bus))
267 		return NULL;
268 	return pdev->bus->self;
269 }
270 
271 /* XXX check for ACPI _PR3 */
272 static inline bool
pci_pr3_present(struct pci_dev * pdev)273 pci_pr3_present(struct pci_dev *pdev)
274 {
275 	return false;
276 }
277 
278 static inline int
pcie_capability_read_dword(struct pci_dev * pdev,int off,u32 * val)279 pcie_capability_read_dword(struct pci_dev *pdev, int off, u32 *val)
280 {
281 	int pos;
282 	if (!pci_get_capability(pdev->pc, pdev->tag, PCI_CAP_PCIEXPRESS,
283 	    &pos, NULL)) {
284 		*val = 0;
285 		return -EINVAL;
286 	}
287 	*val = pci_conf_read(pdev->pc, pdev->tag, pos + off);
288 	return 0;
289 }
290 
291 static inline int
pcie_capability_read_word(struct pci_dev * pdev,int off,u16 * val)292 pcie_capability_read_word(struct pci_dev *pdev, int off, u16 *val)
293 {
294 	int pos;
295 	if (!pci_get_capability(pdev->pc, pdev->tag, PCI_CAP_PCIEXPRESS,
296 	    &pos, NULL)) {
297 		*val = 0;
298 		return -EINVAL;
299 	}
300 	pci_read_config_word(pdev, pos + off, val);
301 	return 0;
302 }
303 
304 static inline int
pcie_capability_write_word(struct pci_dev * pdev,int off,u16 val)305 pcie_capability_write_word(struct pci_dev *pdev, int off, u16 val)
306 {
307 	int pos;
308 	if (!pci_get_capability(pdev->pc, pdev->tag, PCI_CAP_PCIEXPRESS,
309 	    &pos, NULL))
310 		return -EINVAL;
311 	pci_write_config_word(pdev, pos + off, val);
312 	return 0;
313 }
314 
315 static inline int
pcie_capability_set_word(struct pci_dev * pdev,int off,u16 val)316 pcie_capability_set_word(struct pci_dev *pdev, int off, u16 val)
317 {
318 	u16 r;
319 	pcie_capability_read_word(pdev, off, &r);
320 	r |= val;
321 	pcie_capability_write_word(pdev, off, r);
322 	return 0;
323 }
324 
325 static inline int
pcie_capability_clear_word(struct pci_dev * pdev,int off,u16 c)326 pcie_capability_clear_word(struct pci_dev *pdev, int off, u16 c)
327 {
328 	u16 r;
329 	pcie_capability_read_word(pdev, off, &r);
330 	r &= ~c;
331 	pcie_capability_write_word(pdev, off, r);
332 	return 0;
333 }
334 
335 static inline int
pcie_capability_clear_and_set_word(struct pci_dev * pdev,int off,u16 c,u16 s)336 pcie_capability_clear_and_set_word(struct pci_dev *pdev, int off, u16 c, u16 s)
337 {
338 	u16 r;
339 	pcie_capability_read_word(pdev, off, &r);
340 	r &= ~c;
341 	r |= s;
342 	pcie_capability_write_word(pdev, off, r);
343 	return 0;
344 }
345 
346 static inline int
pcie_get_readrq(struct pci_dev * pdev)347 pcie_get_readrq(struct pci_dev *pdev)
348 {
349 	uint16_t val;
350 
351 	pcie_capability_read_word(pdev, PCI_PCIE_DCSR, &val);
352 
353 	return 128 << ((val & PCI_PCIE_DCSR_MPS) >> 12);
354 }
355 
356 static inline int
pcie_set_readrq(struct pci_dev * pdev,int rrq)357 pcie_set_readrq(struct pci_dev *pdev, int rrq)
358 {
359 	uint16_t val;
360 
361 	pcie_capability_read_word(pdev, PCI_PCIE_DCSR, &val);
362 	val &= ~PCI_PCIE_DCSR_MPS;
363 	val |= (ffs(rrq) - 8) << 12;
364 	return pcie_capability_write_word(pdev, PCI_PCIE_DCSR, val);
365 }
366 
367 static inline void
pci_set_master(struct pci_dev * pdev)368 pci_set_master(struct pci_dev *pdev)
369 {
370 }
371 
372 static inline void
pci_clear_master(struct pci_dev * pdev)373 pci_clear_master(struct pci_dev *pdev)
374 {
375 }
376 
377 static inline void
pci_save_state(struct pci_dev * pdev)378 pci_save_state(struct pci_dev *pdev)
379 {
380 }
381 
382 static inline void
pci_restore_state(struct pci_dev * pdev)383 pci_restore_state(struct pci_dev *pdev)
384 {
385 }
386 
387 static inline int
pci_enable_msi(struct pci_dev * pdev)388 pci_enable_msi(struct pci_dev *pdev)
389 {
390 	return 0;
391 }
392 
393 static inline void
pci_disable_msi(struct pci_dev * pdev)394 pci_disable_msi(struct pci_dev *pdev)
395 {
396 }
397 
398 typedef enum {
399 	PCI_D0,
400 	PCI_D1,
401 	PCI_D2,
402 	PCI_D3hot,
403 	PCI_D3cold
404 } pci_power_t;
405 
406 enum pci_bus_speed {
407 	PCIE_SPEED_2_5GT,
408 	PCIE_SPEED_5_0GT,
409 	PCIE_SPEED_8_0GT,
410 	PCIE_SPEED_16_0GT,
411 	PCIE_SPEED_32_0GT,
412 	PCIE_SPEED_64_0GT,
413 	PCI_SPEED_UNKNOWN
414 };
415 
416 enum pcie_link_width {
417 	PCIE_LNK_X1	= 1,
418 	PCIE_LNK_X2	= 2,
419 	PCIE_LNK_X4	= 4,
420 	PCIE_LNK_X8	= 8,
421 	PCIE_LNK_X12	= 12,
422 	PCIE_LNK_X16	= 16,
423 	PCIE_LNK_X32	= 32,
424 	PCIE_LNK_WIDTH_UNKNOWN	= 0xff
425 };
426 
427 typedef unsigned int pci_ers_result_t;
428 typedef unsigned int pci_channel_state_t;
429 
430 #define PCI_ERS_RESULT_DISCONNECT	0
431 #define PCI_ERS_RESULT_RECOVERED	1
432 
433 enum pci_bus_speed pcie_get_speed_cap(struct pci_dev *);
434 enum pcie_link_width pcie_get_width_cap(struct pci_dev *);
435 int pci_resize_resource(struct pci_dev *, int, int);
436 
437 static inline void
pcie_bandwidth_available(struct pci_dev * pdev,struct pci_dev ** ldev,enum pci_bus_speed * speed,enum pcie_link_width * width)438 pcie_bandwidth_available(struct pci_dev *pdev, struct pci_dev **ldev,
439     enum pci_bus_speed *speed, enum pcie_link_width *width)
440 {
441 	struct pci_dev *bdev = pdev->bus->self;
442 	if (bdev == NULL)
443 		return;
444 
445 	if (speed)
446 		*speed = pcie_get_speed_cap(bdev);
447 	if (width)
448 		*width = pcie_get_width_cap(bdev);
449 }
450 
451 static inline int
pci_enable_device(struct pci_dev * pdev)452 pci_enable_device(struct pci_dev *pdev)
453 {
454 	return 0;
455 }
456 
457 static inline void
pci_disable_device(struct pci_dev * pdev)458 pci_disable_device(struct pci_dev *pdev)
459 {
460 }
461 
462 static inline int
pci_wait_for_pending_transaction(struct pci_dev * pdev)463 pci_wait_for_pending_transaction(struct pci_dev *pdev)
464 {
465 	return 0;
466 }
467 
468 static inline bool
pci_is_thunderbolt_attached(struct pci_dev * pdev)469 pci_is_thunderbolt_attached(struct pci_dev *pdev)
470 {
471 	return false;
472 }
473 
474 static inline void
pci_set_drvdata(struct pci_dev * pdev,void * data)475 pci_set_drvdata(struct pci_dev *pdev, void *data)
476 {
477 	dev_set_drvdata(pdev->_dev, data);
478 }
479 
480 static inline void *
pci_get_drvdata(struct pci_dev * pdev)481 pci_get_drvdata(struct pci_dev *pdev)
482 {
483 	return dev_get_drvdata(pdev->_dev);
484 }
485 
486 static inline int
pci_domain_nr(struct pci_bus * pbus)487 pci_domain_nr(struct pci_bus *pbus)
488 {
489 	return pbus->domain_nr;
490 }
491 
492 static inline int
pci_irq_vector(struct pci_dev * pdev,unsigned int num)493 pci_irq_vector(struct pci_dev *pdev, unsigned int num)
494 {
495 	return pdev->irq;
496 }
497 
498 static inline void
pci_free_irq_vectors(struct pci_dev * pdev)499 pci_free_irq_vectors(struct pci_dev *pdev)
500 {
501 }
502 
503 static inline int
pci_set_power_state(struct pci_dev * dev,int state)504 pci_set_power_state(struct pci_dev *dev, int state)
505 {
506 	return 0;
507 }
508 
509 struct pci_driver;
510 
511 static inline int
pci_register_driver(struct pci_driver * pci_drv)512 pci_register_driver(struct pci_driver *pci_drv)
513 {
514 	return 0;
515 }
516 
517 static inline void
pci_unregister_driver(void * d)518 pci_unregister_driver(void *d)
519 {
520 }
521 
522 static inline u16
pci_dev_id(struct pci_dev * dev)523 pci_dev_id(struct pci_dev *dev)
524 {
525 	return dev->devfn | (dev->bus->number << 8);
526 }
527 
528 static inline const struct pci_device_id *
pci_match_id(const struct pci_device_id * ids,struct pci_dev * pdev)529 pci_match_id(const struct pci_device_id *ids, struct pci_dev *pdev)
530 {
531 	int i = 0;
532 
533 	for (i = 0; ids[i].vendor != 0; i++) {
534 		if ((ids[i].vendor == pdev->vendor) &&
535 		    (ids[i].device == pdev->device ||
536 		     ids[i].device == PCI_ANY_ID) &&
537 		    (ids[i].subvendor == PCI_ANY_ID) &&
538 		    (ids[i].subdevice == PCI_ANY_ID))
539 			return &ids[i];
540 	}
541 	return NULL;
542 }
543 
544 #define PCI_CLASS_DISPLAY_VGA \
545     ((PCI_CLASS_DISPLAY << 8) | PCI_SUBCLASS_DISPLAY_VGA)
546 #define PCI_CLASS_DISPLAY_OTHER \
547     ((PCI_CLASS_DISPLAY << 8) | PCI_SUBCLASS_DISPLAY_MISC)
548 #define PCI_CLASS_ACCELERATOR_PROCESSING \
549     (PCI_CLASS_ACCELERATOR << 8)
550 
551 static inline int
pci_device_is_present(struct pci_dev * pdev)552 pci_device_is_present(struct pci_dev *pdev)
553 {
554 	return 1;
555 }
556 
557 static inline int
dev_is_pci(struct device * dev)558 dev_is_pci(struct device *dev)
559 {
560 	return 1;
561 }
562 #endif /* _LINUX_PCI_H_ */
563