blob: 6e9cbf0fddb6f86029e274839925eec5b028c362 [file] [log] [blame]
Kevin O'Connorfaa8b6f2008-03-21 13:32:34 -04001#ifndef __PCI_H
2#define __PCI_H
3
Kevin O'Connora0dc2962008-03-16 14:29:32 -04004#include "types.h" // u32
5
Kevin O'Connor5fdaa032008-08-31 11:06:27 -04006static inline u8 pci_bdf_to_bus(u16 bdf) {
7 return bdf >> 8;
8}
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -05009static inline u8 pci_bdf_to_devfn(u16 bdf) {
10 return bdf & 0xff;
11}
Kevin O'Connorb5bb9db2010-01-03 15:14:11 -050012static inline u16 pci_bdf_to_busdev(u16 bdf) {
13 return bdf & ~0x07;
14}
Kevin O'Connor5fdaa032008-08-31 11:06:27 -040015static inline u8 pci_bdf_to_dev(u16 bdf) {
16 return (bdf >> 3) & 0x1f;
17}
18static inline u8 pci_bdf_to_fn(u16 bdf) {
19 return bdf & 0x07;
20}
Kevin O'Connor423a04d2009-02-05 21:16:39 -050021static inline u16 pci_to_bdf(int bus, int dev, int fn) {
22 return (bus<<8) | (dev<<3) | fn;
23}
Isaku Yamahataedd99112010-06-22 17:57:46 +090024static inline u16 pci_bus_devfn_to_bdf(int bus, u16 devfn) {
25 return (bus << 8) | devfn;
26}
Kevin O'Connor0f803e42008-05-24 23:07:16 -040027
Kevin O'Connor49cc72b2010-05-23 10:22:23 -040028static inline u32 pci_vd(u16 vendor, u16 device) {
29 return (device << 16) | vendor;
30}
31static inline u16 pci_vd_to_ven(u32 vd) {
32 return vd & 0xffff;
33}
34static inline u16 pci_vd_to_dev(u32 vd) {
35 return vd >> 16;
36}
37
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050038void pci_config_writel(u16 bdf, u32 addr, u32 val);
39void pci_config_writew(u16 bdf, u32 addr, u16 val);
40void pci_config_writeb(u16 bdf, u32 addr, u8 val);
41u32 pci_config_readl(u16 bdf, u32 addr);
42u16 pci_config_readw(u16 bdf, u32 addr);
43u8 pci_config_readb(u16 bdf, u32 addr);
Kevin O'Connor59f02832009-10-12 10:09:15 -040044void pci_config_maskw(u16 bdf, u32 addr, u16 off, u16 on);
Kevin O'Connor0f803e42008-05-24 23:07:16 -040045
Kevin O'Connor1ca05b02010-01-03 17:43:37 -050046int pci_find_vga(void);
Kevin O'Connor4132e022008-12-04 19:39:10 -050047int pci_find_device(u16 vendid, u16 devid);
48int pci_find_class(u16 classid);
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050049
Kevin O'Connord08eb9c2011-01-10 00:48:45 -050050#define PP_ROOT (1<<17)
51#define PP_PCIBRIDGE (1<<18)
52extern int *PCIpaths;
53void pci_path_setup(void);
54
Kevin O'Connore6338322008-11-29 20:31:49 -050055int pci_next(int bdf, int *pmax);
Kevin O'Connorbaac6b62011-06-19 10:46:28 -040056#define foreachbdf(BDF, MAX) \
Kevin O'Connor4132e022008-12-04 19:39:10 -050057 for (MAX=0x0100, BDF=pci_next(0, &MAX) \
58 ; BDF >= 0 \
Kevin O'Connore6338322008-11-29 20:31:49 -050059 ; BDF=pci_next(BDF+1, &MAX))
Kevin O'Connorfaa8b6f2008-03-21 13:32:34 -040060
Kevin O'Connorbaac6b62011-06-19 10:46:28 -040061#define foreachbdf_in_bus(BDF, MAX, BUS) \
Isaku Yamahataedd99112010-06-22 17:57:46 +090062 for (MAX = pci_bus_devfn_to_bdf(BUS, 0) + 0x0100, \
63 BDF = pci_next(pci_bus_devfn_to_bdf(BUS, 0), &MAX) \
64 ; BDF >= 0 && BDF < pci_bus_devfn_to_bdf(BUS, 0) + 0x0100 \
65 ; MAX = pci_bus_devfn_to_bdf(BUS, 0) + 0x0100, \
66 BDF = pci_next(BDF + 1, &MAX))
67
Isaku Yamahata968d3a82010-07-07 12:14:01 +090068#define PCI_ANY_ID (~0)
69struct pci_device_id {
70 u32 vendid;
71 u32 devid;
72 u32 class;
73 u32 class_mask;
74 void (*func)(u16 bdf, void *arg);
75};
76
77#define PCI_DEVICE(vendor_id, device_id, init_func) \
78 { \
79 .vendid = (vendor_id), \
80 .devid = (device_id), \
81 .class = PCI_ANY_ID, \
82 .class_mask = 0, \
83 .func = (init_func) \
84 }
85
86#define PCI_DEVICE_CLASS(vendor_id, device_id, class_code, init_func) \
87 { \
88 .vendid = (vendor_id), \
89 .devid = (device_id), \
90 .class = (class_code), \
91 .class_mask = ~0, \
92 .func = (init_func) \
93 }
94
95#define PCI_DEVICE_END \
96 { \
97 .vendid = 0, \
98 }
99
100int pci_init_device(const struct pci_device_id *table, u16 bdf, void *arg);
Isaku Yamahata23173ac2010-07-20 16:37:15 +0900101int pci_find_init_device(const struct pci_device_id *ids, void *arg);
Kevin O'Connoradaf3732010-09-13 20:22:07 -0400102void pci_reboot(void);
Isaku Yamahata968d3a82010-07-07 12:14:01 +0900103
Gerd Hoffmannf1f18eb2010-11-29 09:42:10 +0100104// helper functions to access pci mmio bars from real mode
105u32 pci_readl(u32 addr);
106void pci_writel(u32 addr, u32 val);
107
Kevin O'Connord25810a2008-06-12 22:16:35 -0400108// pirtable.c
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500109void create_pirtable(void);
Kevin O'Connord25810a2008-06-12 22:16:35 -0400110
Kevin O'Connor061d1372008-06-11 22:39:46 -0400111
112/****************************************************************
113 * PIR table
114 ****************************************************************/
115
Kevin O'Connor51358db2008-12-28 21:50:29 -0500116extern u16 PirOffset;
117
Kevin O'Connor061d1372008-06-11 22:39:46 -0400118struct link_info {
119 u8 link;
120 u16 bitmap;
121} PACKED;
122
123struct pir_slot {
124 u8 bus;
125 u8 dev;
126 struct link_info links[4];
127 u8 slot_nr;
128 u8 reserved;
129} PACKED;
130
Kevin O'Connord25810a2008-06-12 22:16:35 -0400131struct pir_header {
132 u32 signature;
133 u16 version;
134 u16 size;
135 u8 router_bus;
136 u8 router_devfunc;
137 u16 exclusive_irqs;
138 u32 compatible_devid;
139 u32 miniport_data;
140 u8 reserved[11];
141 u8 checksum;
142 struct pir_slot slots[0];
143} PACKED;
144
145#define PIR_SIGNATURE 0x52495024 // $PIR
146
Kevin O'Connor061d1372008-06-11 22:39:46 -0400147
Kevin O'Connorfaa8b6f2008-03-21 13:32:34 -0400148#endif