9d6f08a7207b69efaeb1d43704ca4c8e7518b01d
[vpp.git] / vlib / vlib / pci / pci_config.h
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  * pci.h: PCI definitions.
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 #ifndef included_vlib_pci_config_h
41 #define included_vlib_pci_config_h
42
43 #include <vppinfra/byte_order.h>
44 #include <vppinfra/error.h>
45
46 typedef enum {
47   PCI_CLASS_NOT_DEFINED = 0x0000,
48   PCI_CLASS_NOT_DEFINED_VGA = 0x0001,
49
50   PCI_CLASS_STORAGE_SCSI = 0x0100,
51   PCI_CLASS_STORAGE_IDE = 0x0101,
52   PCI_CLASS_STORAGE_FLOPPY = 0x0102,
53   PCI_CLASS_STORAGE_IPI = 0x0103,
54   PCI_CLASS_STORAGE_RAID = 0x0104,
55   PCI_CLASS_STORAGE_OTHER = 0x0180,
56   PCI_CLASS_STORAGE = 0x0100,
57
58   PCI_CLASS_NETWORK_ETHERNET = 0x0200,
59   PCI_CLASS_NETWORK_TOKEN_RING = 0x0201,
60   PCI_CLASS_NETWORK_FDDI = 0x0202,
61   PCI_CLASS_NETWORK_ATM = 0x0203,
62   PCI_CLASS_NETWORK_OTHER = 0x0280,
63   PCI_CLASS_NETWORK = 0x0200,
64
65   PCI_CLASS_DISPLAY_VGA = 0x0300,
66   PCI_CLASS_DISPLAY_XGA = 0x0301,
67   PCI_CLASS_DISPLAY_3D = 0x0302,
68   PCI_CLASS_DISPLAY_OTHER = 0x0380,
69   PCI_CLASS_DISPLAY = 0x0300,
70
71   PCI_CLASS_MULTIMEDIA_VIDEO = 0x0400,
72   PCI_CLASS_MULTIMEDIA_AUDIO = 0x0401,
73   PCI_CLASS_MULTIMEDIA_PHONE = 0x0402,
74   PCI_CLASS_MULTIMEDIA_OTHER = 0x0480,
75   PCI_CLASS_MULTIMEDIA = 0x0400,
76
77   PCI_CLASS_MEMORY_RAM = 0x0500,
78   PCI_CLASS_MEMORY_FLASH = 0x0501,
79   PCI_CLASS_MEMORY_OTHER = 0x0580,
80   PCI_CLASS_MEMORY = 0x0500,
81
82   PCI_CLASS_BRIDGE_HOST = 0x0600,
83   PCI_CLASS_BRIDGE_ISA = 0x0601,
84   PCI_CLASS_BRIDGE_EISA = 0x0602,
85   PCI_CLASS_BRIDGE_MC = 0x0603,
86   PCI_CLASS_BRIDGE_PCI = 0x0604,
87   PCI_CLASS_BRIDGE_PCMCIA = 0x0605,
88   PCI_CLASS_BRIDGE_NUBUS = 0x0606,
89   PCI_CLASS_BRIDGE_CARDBUS = 0x0607,
90   PCI_CLASS_BRIDGE_RACEWAY = 0x0608,
91   PCI_CLASS_BRIDGE_OTHER = 0x0680,
92   PCI_CLASS_BRIDGE = 0x0600,
93
94   PCI_CLASS_COMMUNICATION_SERIAL = 0x0700,
95   PCI_CLASS_COMMUNICATION_PARALLEL = 0x0701,
96   PCI_CLASS_COMMUNICATION_MULTISERIAL = 0x0702,
97   PCI_CLASS_COMMUNICATION_MODEM = 0x0703,
98   PCI_CLASS_COMMUNICATION_OTHER = 0x0780,
99   PCI_CLASS_COMMUNICATION = 0x0700,
100
101   PCI_CLASS_SYSTEM_PIC = 0x0800,
102   PCI_CLASS_SYSTEM_DMA = 0x0801,
103   PCI_CLASS_SYSTEM_TIMER = 0x0802,
104   PCI_CLASS_SYSTEM_RTC = 0x0803,
105   PCI_CLASS_SYSTEM_PCI_HOTPLUG = 0x0804,
106   PCI_CLASS_SYSTEM_OTHER = 0x0880,
107   PCI_CLASS_SYSTEM = 0x0800,
108
109   PCI_CLASS_INPUT_KEYBOARD = 0x0900,
110   PCI_CLASS_INPUT_PEN = 0x0901,
111   PCI_CLASS_INPUT_MOUSE = 0x0902,
112   PCI_CLASS_INPUT_SCANNER = 0x0903,
113   PCI_CLASS_INPUT_GAMEPORT = 0x0904,
114   PCI_CLASS_INPUT_OTHER = 0x0980,
115   PCI_CLASS_INPUT = 0x0900,
116
117   PCI_CLASS_DOCKING_GENERIC = 0x0a00,
118   PCI_CLASS_DOCKING_OTHER = 0x0a80,
119   PCI_CLASS_DOCKING = 0x0a00,
120
121   PCI_CLASS_PROCESSOR_386 = 0x0b00,
122   PCI_CLASS_PROCESSOR_486 = 0x0b01,
123   PCI_CLASS_PROCESSOR_PENTIUM = 0x0b02,
124   PCI_CLASS_PROCESSOR_ALPHA = 0x0b10,
125   PCI_CLASS_PROCESSOR_POWERPC = 0x0b20,
126   PCI_CLASS_PROCESSOR_MIPS = 0x0b30,
127   PCI_CLASS_PROCESSOR_CO = 0x0b40,
128   PCI_CLASS_PROCESSOR = 0x0b00,
129
130   PCI_CLASS_SERIAL_FIREWIRE = 0x0c00,
131   PCI_CLASS_SERIAL_ACCESS = 0x0c01,
132   PCI_CLASS_SERIAL_SSA = 0x0c02,
133   PCI_CLASS_SERIAL_USB = 0x0c03,
134   PCI_CLASS_SERIAL_FIBER = 0x0c04,
135   PCI_CLASS_SERIAL_SMBUS = 0x0c05,
136   PCI_CLASS_SERIAL = 0x0c00,
137
138   PCI_CLASS_INTELLIGENT_I2O = 0x0e00,
139   PCI_CLASS_INTELLIGENT = 0x0e00,
140
141   PCI_CLASS_SATELLITE_TV = 0x0f00,
142   PCI_CLASS_SATELLITE_AUDIO = 0x0f01,
143   PCI_CLASS_SATELLITE_VOICE = 0x0f03,
144   PCI_CLASS_SATELLITE_DATA = 0x0f04,
145   PCI_CLASS_SATELLITE = 0x0f00,
146
147   PCI_CLASS_CRYPT_NETWORK = 0x1000,
148   PCI_CLASS_CRYPT_ENTERTAINMENT = 0x1001,
149   PCI_CLASS_CRYPT_OTHER = 0x1080,
150   PCI_CLASS_CRYPT = 0x1000,
151
152   PCI_CLASS_SP_DPIO = 0x1100,
153   PCI_CLASS_SP_OTHER = 0x1180,
154   PCI_CLASS_SP = 0x1100,
155 } pci_device_class_t;
156
157 static inline pci_device_class_t
158 pci_device_class_base (pci_device_class_t c)
159 { return c &~ 0xff; }
160
161 /*
162  * Under PCI, each device has 256 bytes of configuration address space,
163  * of which the first 64 bytes are standardized as follows:
164  */
165 typedef struct {
166   u16 vendor_id;
167   u16 device_id;
168
169   u16 command;
170 #define PCI_COMMAND_IO          (1 << 0) /* Enable response in I/O space */
171 #define PCI_COMMAND_MEMORY      (1 << 1) /* Enable response in Memory space */
172 #define PCI_COMMAND_BUS_MASTER  (1 << 2) /* Enable bus mastering */
173 #define PCI_COMMAND_SPECIAL     (1 << 3) /* Enable response to special cycles */
174 #define PCI_COMMAND_WRITE_INVALIDATE (1 << 4) /* Use memory write and invalidate */
175 #define PCI_COMMAND_VGA_PALETTE_SNOOP (1 << 5)
176 #define PCI_COMMAND_PARITY      (1 << 6)
177 #define PCI_COMMAND_WAIT        (1 << 7) /* Enable address/data stepping */
178 #define PCI_COMMAND_SERR        (1 << 8) /* Enable SERR */
179 #define PCI_COMMAND_BACK_TO_BACK_WRITE (1 << 9)
180 #define PCI_COMMAND_INTX_DISABLE (1 << 10) /* INTx Emulation Disable */
181
182   u16 status;
183 #define PCI_STATUS_INTX_PENDING (1 << 3)
184 #define PCI_STATUS_CAPABILITY_LIST (1 << 4)
185 #define PCI_STATUS_66MHZ        (1 << 5) /* Support 66 Mhz PCI 2.1 bus */
186 #define PCI_STATUS_UDF          (1 << 6) /* Support User Definable Features (obsolete) */
187 #define PCI_STATUS_BACK_TO_BACK_WRITE (1 << 7) /* Accept fast-back to back */
188 #define PCI_STATUS_PARITY_ERROR (1 << 8) /* Detected parity error */
189 #define PCI_STATUS_DEVSEL_GET(x) ((x >> 9) & 3) /* DEVSEL timing */
190 #define PCI_STATUS_DEVSEL_FAST (0 << 9)
191 #define PCI_STATUS_DEVSEL_MEDIUM (1 << 9)
192 #define PCI_STATUS_DEVSEL_SLOW (2 << 9)
193 #define PCI_STATUS_SIG_TARGET_ABORT (1 << 11) /* Set on target abort */
194 #define PCI_STATUS_REC_TARGET_ABORT (1 << 12) /* Master ack of " */
195 #define PCI_STATUS_REC_MASTER_ABORT (1 << 13) /* Set on master abort */
196 #define PCI_STATUS_SIG_SYSTEM_ERROR (1 << 14) /* Set when we drive SERR */
197 #define PCI_STATUS_DETECTED_PARITY_ERROR (1 << 15)
198
199   u8 revision_id;
200   u8 programming_interface_class; /* Reg. Level Programming Interface */
201
202   pci_device_class_t device_class : 16;
203
204   u8 cache_size;
205   u8 latency_timer;
206
207   u8 header_type;
208 #define PCI_HEADER_TYPE_NORMAL  0
209 #define PCI_HEADER_TYPE_BRIDGE 1
210 #define PCI_HEADER_TYPE_CARDBUS 2
211
212   u8 bist;
213 #define PCI_BIST_CODE_MASK      0x0f    /* Return result */
214 #define PCI_BIST_START          0x40    /* 1 to start BIST, 2 secs or less */
215 #define PCI_BIST_CAPABLE        0x80    /* 1 if BIST capable */
216 } pci_config_header_t;
217
218 /* Byte swap config header. */
219 always_inline void
220 pci_config_header_little_to_host (pci_config_header_t * r)
221 {
222   if (! CLIB_ARCH_IS_BIG_ENDIAN)
223     return;
224 #define _(f,t) r->f = clib_byte_swap_##t (r->f)
225   _ (vendor_id, u16);
226   _ (device_id, u16);
227   _ (command, u16);
228   _ (status, u16);
229   _ (device_class, u16);
230 #undef _
231 }
232
233 /* Header type 0 (normal devices) */
234 typedef struct {
235   pci_config_header_t header;
236
237   /*
238    * Base addresses specify locations in memory or I/O space.
239    * Decoded size can be determined by writing a value of 
240    * 0xffffffff to the register, and reading it back. Only 
241    * 1 bits are decoded.
242    */
243   u32 base_address[6];
244
245   u16 cardbus_cis;
246
247   u16 subsystem_vendor_id;
248   u16 subsystem_id;
249
250   u32 rom_address;
251 #define PCI_ROM_ADDRESS         0x30    /* Bits 31..11 are address, 10..1 reserved */
252 #define PCI_ROM_ADDRESS_ENABLE  0x01
253 #define PCI_ROM_ADDRESS_MASK    (~0x7ffUL)
254  
255   u8 first_capability_offset;
256   CLIB_PAD_FROM_TO (0x35, 0x3c);
257
258   u8 interrupt_line;
259   u8 interrupt_pin;
260   u8 min_grant;
261   u8 max_latency;
262
263   u8 capability_data[0];
264 } pci_config_type0_regs_t;
265
266 always_inline void
267 pci_config_type0_little_to_host (pci_config_type0_regs_t * r)
268 {
269   int i;
270   if (! CLIB_ARCH_IS_BIG_ENDIAN)
271     return;
272   pci_config_header_little_to_host (&r->header);
273 #define _(f,t) r->f = clib_byte_swap_##t (r->f)
274   for (i = 0; i < ARRAY_LEN (r->base_address); i++)
275     _ (base_address[i], u32);
276   _ (cardbus_cis, u16);
277   _ (subsystem_vendor_id, u16);
278   _ (subsystem_id, u16);
279   _ (rom_address, u32);
280 #undef _
281 }
282
283 /* Header type 1 (PCI-to-PCI bridges) */
284 typedef struct {
285   pci_config_header_t header;
286
287   u32 base_address[2];
288
289   /* Primary/secondary bus number. */
290   u8 primary_bus;
291   u8 secondary_bus;
292
293   /* Highest bus number behind the bridge */
294   u8 subordinate_bus;
295
296   u8 secondary_bus_latency_timer;
297
298   /* I/O range behind bridge. */
299   u8 io_base, io_limit;
300
301   /* Secondary status register, only bit 14 used */
302   u16 secondary_status;
303
304   /* Memory range behind bridge in units of 64k bytes. */
305   u16 memory_base, memory_limit;
306 #define PCI_MEMORY_RANGE_TYPE_MASK 0x0fUL
307 #define PCI_MEMORY_RANGE_MASK   (~0x0fUL)
308
309   u16 prefetchable_memory_base, prefetchable_memory_limit;
310 #define PCI_PREF_RANGE_TYPE_MASK 0x0fUL
311 #define PCI_PREF_RANGE_TYPE_32  0x00
312 #define PCI_PREF_RANGE_TYPE_64  0x01
313 #define PCI_PREF_RANGE_MASK     (~0x0fUL)
314
315   u32 prefetchable_memory_base_upper_32bits;
316   u32 prefetchable_memory_limit_upper_32bits;
317   u16 io_base_upper_16bits;
318   u16 io_limit_upper_16bits;
319
320   /* Same as for type 0. */
321   u8 capability_list_offset;
322   CLIB_PAD_FROM_TO (0x35, 0x37);
323
324   u32 rom_address;
325   CLIB_PAD_FROM_TO (0x3c, 0x3e);
326
327   u16 bridge_control;
328 #define PCI_BRIDGE_CTL_PARITY   0x01    /* Enable parity detection on secondary interface */
329 #define PCI_BRIDGE_CTL_SERR     0x02    /* The same for SERR forwarding */
330 #define PCI_BRIDGE_CTL_NO_ISA   0x04    /* Disable bridging of ISA ports */
331 #define PCI_BRIDGE_CTL_VGA      0x08    /* Forward VGA addresses */
332 #define PCI_BRIDGE_CTL_MASTER_ABORT 0x20 /* Report master aborts */
333 #define PCI_BRIDGE_CTL_BUS_RESET 0x40   /* Secondary bus reset */
334 #define PCI_BRIDGE_CTL_FAST_BACK 0x80   /* Fast Back2Back enabled on secondary interface */
335
336   u8 capability_data[0];
337 } pci_config_type1_regs_t;
338
339 always_inline void
340 pci_config_type1_little_to_host (pci_config_type1_regs_t * r)
341 {
342   int i;
343   if (! CLIB_ARCH_IS_BIG_ENDIAN)
344     return;
345   pci_config_header_little_to_host (&r->header);
346 #define _(f,t) r->f = clib_byte_swap_##t (r->f)
347   for (i = 0; i < ARRAY_LEN (r->base_address); i++)
348     _ (base_address[i], u32);
349   _ (secondary_status, u16);
350   _ (memory_base, u16);
351   _ (memory_limit, u16);
352   _ (prefetchable_memory_base, u16);
353   _ (prefetchable_memory_limit, u16);
354   _ (prefetchable_memory_base_upper_32bits, u32);
355   _ (prefetchable_memory_limit_upper_32bits, u32);
356   _ (io_base_upper_16bits, u16);
357   _ (io_limit_upper_16bits, u16);
358   _ (rom_address, u32);
359   _ (bridge_control, u16);
360 #undef _
361 }
362
363 /* Capabilities. */
364 typedef enum pci_capability_type {
365   /* Power Management */
366   PCI_CAP_ID_PM = 1,
367
368   /* Accelerated Graphics Port */
369   PCI_CAP_ID_AGP = 2,
370
371   /* Vital Product Data */
372   PCI_CAP_ID_VPD = 3,
373
374   /* Slot Identification */
375   PCI_CAP_ID_SLOTID = 4,
376
377   /* Message Signalled Interrupts */
378   PCI_CAP_ID_MSI = 5,
379
380   /* CompactPCI HotSwap */
381   PCI_CAP_ID_CHSWP = 6, 
382
383   /* PCI-X */
384   PCI_CAP_ID_PCIX = 7,
385
386   /* Hypertransport. */
387   PCI_CAP_ID_HYPERTRANSPORT = 8,
388
389   /* PCI Standard Hot-Plug Controller */
390   PCI_CAP_ID_SHPC = 0xc,
391
392   /* PCI Express */
393   PCI_CAP_ID_PCIE = 0x10,
394
395   /* MSI-X */
396   PCI_CAP_ID_MSIX = 0x11,
397 } pci_capability_type_t;
398
399 /* Common header for capabilities. */
400 typedef CLIB_PACKED (struct {
401   enum pci_capability_type type : 8;
402
403   u8 next_offset;
404 }) pci_capability_regs_t;
405
406 always_inline void *
407 pci_config_find_capability (pci_config_type0_regs_t * t, int cap_type)
408 {
409   pci_capability_regs_t * c;
410   u32 next_offset;
411   u32 ttl = 48;
412
413   if (! (t->header.status & PCI_STATUS_CAPABILITY_LIST))
414     return 0;
415
416   next_offset = t->first_capability_offset;
417   while (ttl-- && next_offset >= 0x40)
418     {
419       c = (void *) t + (next_offset &~ 3);
420       if ((u8) c->type == 0xff)
421         break;
422       if (c->type == cap_type)
423         return c;
424       next_offset = c->next_offset;
425     }
426   return 0;
427 }
428
429 /* Power Management Registers */
430 typedef CLIB_PACKED (struct {
431   pci_capability_regs_t header;
432
433   u16 capabilities;
434 #define PCI_PM_CAP_VER_MASK     0x0007  /* Version */
435 #define PCI_PM_CAP_PME_CLOCK    0x0008  /* PME clock required */
436 #define PCI_PM_CAP_RESERVED  0x0010 /* Reserved field */
437 #define PCI_PM_CAP_DSI          0x0020  /* Device specific initialization */
438 #define PCI_PM_CAP_AUX_POWER    0x01C0  /* Auxilliary power support mask */
439 #define PCI_PM_CAP_D1           0x0200  /* D1 power state support */
440 #define PCI_PM_CAP_D2           0x0400  /* D2 power state support */
441 #define PCI_PM_CAP_PME          0x0800  /* PME pin supported */
442 #define PCI_PM_CAP_PME_MASK  0xF800 /* PME Mask of all supported states */
443 #define PCI_PM_CAP_PME_D0   0x0800 /* PME# from D0 */
444 #define PCI_PM_CAP_PME_D1   0x1000 /* PME# from D1 */
445 #define PCI_PM_CAP_PME_D2   0x2000 /* PME# from D2 */
446 #define PCI_PM_CAP_PME_D3   0x4000 /* PME# from D3 (hot) */
447 #define PCI_PM_CAP_PME_D3cold 0x8000 /* PME# from D3 (cold) */
448  
449   u16 control;
450 #define PCI_PM_CTRL_STATE_MASK  0x0003  /* Current power state (D0 to D3) */
451 #define PCI_PM_CTRL_PME_ENABLE  0x0100  /* PME pin enable */
452 #define PCI_PM_CTRL_DATA_SEL_MASK       0x1e00  /* Data select (??) */
453 #define PCI_PM_CTRL_DATA_SCALE_MASK     0x6000  /* Data scale (??) */
454 #define PCI_PM_CTRL_PME_STATUS  0x8000  /* PME pin status */
455
456   u8 extensions;
457 #define PCI_PM_PPB_B2_B3        0x40    /* Stop clock when in D3hot (??) */
458 #define PCI_PM_BPCC_ENABLE      0x80    /* Bus power/clock control enable (??) */
459
460   u8 data;
461 }) pci_power_management_regs_t;
462
463 /* AGP registers */
464 typedef CLIB_PACKED (struct {
465   pci_capability_regs_t header;
466   u8 version;
467   u8 rest_of_capability_flags;
468
469   u32 status;
470   u32 command;
471   /* Command & status common bits. */
472 #define PCI_AGP_RQ_MASK 0xff000000 /* Maximum number of requests - 1 */
473 #define PCI_AGP_SBA     0x0200  /* Sideband addressing supported */
474 #define PCI_AGP_64BIT   0x0020  /* 64-bit addressing supported */
475 #define PCI_AGP_ALLOW_TRANSACTIONS 0x0100       /* Allow processing of AGP transactions */
476 #define PCI_AGP_FW      0x0010  /* FW transfers supported/forced */
477 #define PCI_AGP_RATE4   0x0004  /* 4x transfer rate supported */
478 #define PCI_AGP_RATE2   0x0002  /* 2x transfer rate supported */
479 #define PCI_AGP_RATE1   0x0001  /* 1x transfer rate supported */
480 }) pci_agp_regs_t;
481
482 /* Vital Product Data */
483 typedef CLIB_PACKED (struct {
484   pci_capability_regs_t header;
485   u16 address;
486 #define PCI_VPD_ADDR_MASK       0x7fff  /* Address mask */
487 #define PCI_VPD_ADDR_F          0x8000  /* Write 0, 1 indicates completion */
488
489   u32 data;
490 }) pci_vpd_regs_t;
491
492 /* Slot Identification */
493 typedef CLIB_PACKED (struct {
494   pci_capability_regs_t header;
495   u8 esr;
496 #define PCI_SID_ESR_NSLOTS      0x1f    /* Number of expansion slots available */
497 #define PCI_SID_ESR_FIC 0x20    /* First In Chassis Flag */
498   u8 chassis;
499 }) pci_sid_regs_t;
500
501 /* Message Signalled Interrupts registers */
502 typedef CLIB_PACKED (struct {
503   pci_capability_regs_t header;
504
505   u16 flags;
506 #define PCI_MSI_FLAGS_ENABLE    (1 << 0) /* MSI feature enabled */
507 #define PCI_MSI_FLAGS_GET_MAX_QUEUE_SIZE(x) ((x >> 1) & 0x7)
508 #define PCI_MSI_FLAGS_MAX_QUEUE_SIZE(x)     (((x) & 0x7) << 1)
509 #define PCI_MSI_FLAGS_GET_QUEUE_SIZE(x) ((x >> 4) & 0x7)
510 #define PCI_MSI_FLAGS_QUEUE_SIZE(x)     (((x) & 0x7) << 4)
511 #define PCI_MSI_FLAGS_64BIT     (1 << 7) /* 64-bit addresses allowed */
512 #define PCI_MSI_FLAGS_MASKBIT   (1 << 8) /* 64-bit mask bits allowed */
513
514   u32 address;
515   u32 data;
516   u32 mask_bits;
517 }) pci_msi32_regs_t;
518
519 typedef CLIB_PACKED (struct {
520   pci_capability_regs_t header;
521   u16 flags;
522   u32 address[2];
523   u32 data;
524   u32 mask_bits;
525 }) pci_msi64_regs_t;
526
527 /* CompactPCI Hotswap Register */
528 typedef CLIB_PACKED (struct {
529   pci_capability_regs_t header;
530
531   u16 control_status;
532 #define PCI_CHSWP_DHA           0x01    /* Device Hiding Arm */
533 #define PCI_CHSWP_EIM           0x02    /* ENUM# Signal Mask */
534 #define PCI_CHSWP_PIE           0x04    /* Pending Insert or Extract */
535 #define PCI_CHSWP_LOO           0x08    /* LED On / Off */
536 #define PCI_CHSWP_PI            0x30    /* Programming Interface */
537 #define PCI_CHSWP_EXT           0x40    /* ENUM# status - extraction */
538 #define PCI_CHSWP_INS           0x80    /* ENUM# status - insertion */
539 }) pci_chswp_regs_t;
540
541 /* PCIX registers */
542 typedef CLIB_PACKED (struct {
543   pci_capability_regs_t header;
544
545   u16 command;
546 #define PCIX_CMD_DPERR_E        0x0001  /* Data Parity Error Recovery Enable */
547 #define PCIX_CMD_ERO            0x0002  /* Enable Relaxed Ordering */
548 #define PCIX_CMD_MAX_READ       0x000c  /* Max Memory Read Byte Count */
549 #define PCIX_CMD_MAX_SPLIT      0x0070  /* Max Outstanding Split Transactions */
550 #define PCIX_CMD_VERSION(x)     (((x) >> 12) & 3) /* Version */
551
552   u32 status;
553 #define PCIX_STATUS_DEVFN       0x000000ff      /* A copy of devfn */
554 #define PCIX_STATUS_BUS 0x0000ff00      /* A copy of bus nr */
555 #define PCIX_STATUS_64BIT       0x00010000      /* 64-bit device */
556 #define PCIX_STATUS_133MHZ      0x00020000      /* 133 MHz capable */
557 #define PCIX_STATUS_SPL_DISC    0x00040000      /* Split Completion Discarded */
558 #define PCIX_STATUS_UNX_SPL     0x00080000      /* Unexpected Split Completion */
559 #define PCIX_STATUS_COMPLEX     0x00100000      /* Device Complexity */
560 #define PCIX_STATUS_MAX_READ    0x00600000      /* Designed Max Memory Read Count */
561 #define PCIX_STATUS_MAX_SPLIT   0x03800000      /* Designed Max Outstanding Split Transactions */
562 #define PCIX_STATUS_MAX_CUM     0x1c000000      /* Designed Max Cumulative Read Size */
563 #define PCIX_STATUS_SPL_ERR     0x20000000      /* Rcvd Split Completion Error Msg */
564 #define PCIX_STATUS_266MHZ      0x40000000      /* 266 MHz capable */
565 #define PCIX_STATUS_533MHZ      0x80000000      /* 533 MHz capable */
566 }) pcix_config_regs_t;
567
568 static inline int pcie_size_to_code (int bytes)
569 {
570   ASSERT (is_pow2 (bytes));
571   ASSERT (bytes <= 4096);
572   return min_log2 (bytes) - 7;
573 }
574
575 static inline int pcie_code_to_size (int code)
576 {
577   int size = 1 << (code + 7);
578   ASSERT (size <= 4096);
579   return size;
580 }
581
582 /* PCI Express capability registers */
583 typedef CLIB_PACKED (struct {
584   pci_capability_regs_t header;
585
586   u16 pcie_capabilities;
587 #define PCIE_CAP_VERSION(x)     (((x) >> 0) & 0xf)
588 #define PCIE_CAP_DEVICE_TYPE(x) (((x) >> 4) & 0xf)
589 #define PCIE_DEVICE_TYPE_ENDPOINT 0
590 #define PCIE_DEVICE_TYPE_LEGACY_ENDPOINT 1
591 #define PCIE_DEVICE_TYPE_ROOT_PORT 4
592   /* Upstream/downstream port of PCI Express switch. */
593 #define PCIE_DEVICE_TYPE_SWITCH_UPSTREAM 5
594 #define PCIE_DEVICE_TYPE_SWITCH_DOWNSTREAM 6
595 #define PCIE_DEVICE_TYPE_PCIE_TO_PCI_BRIDGE 7
596 #define PCIE_DEVICE_TYPE_PCI_TO_PCIE_BRIDGE 8
597   /* Root complex integrated endpoint. */
598 #define PCIE_DEVICE_TYPE_ROOT_COMPLEX_ENDPOINT 9
599 #define PCIE_DEVICE_TYPE_ROOT_COMPLEX_EVENT_COLLECTOR 10
600 #define PCIE_CAP_SLOW_IMPLEMENTED (1 << 8)
601 #define PCIE_CAP_MSI_IRQ(x) (((x) >> 9) & 0x1f)
602
603   u32 dev_capabilities;
604 #define PCIE_DEVCAP_MAX_PAYLOAD(x) (128 << (((x) >> 0) & 0x7))
605 #define PCIE_DEVCAP_PHANTOM_BITS(x) (((x) >> 3) & 0x3)
606 #define PCIE_DEVCAP_EXTENTED_TAG (1 << 5)
607 #define PCIE_DEVCAP_L0S 0x1c0   /* L0s Acceptable Latency */
608 #define PCIE_DEVCAP_L1  0xe00   /* L1 Acceptable Latency */
609 #define PCIE_DEVCAP_ATN_BUT     0x1000  /* Attention Button Present */
610 #define PCIE_DEVCAP_ATN_IND     0x2000  /* Attention Indicator Present */
611 #define PCIE_DEVCAP_PWR_IND     0x4000  /* Power Indicator Present */
612 #define PCIE_DEVCAP_PWR_VAL     0x3fc0000 /* Slot Power Limit Value */
613 #define PCIE_DEVCAP_PWR_SCL     0xc000000 /* Slot Power Limit Scale */
614
615   u16 dev_control;
616 #define PCIE_CTRL_CERE  0x0001  /* Correctable Error Reporting En. */
617 #define PCIE_CTRL_NFERE 0x0002  /* Non-Fatal Error Reporting Enable */
618 #define PCIE_CTRL_FERE  0x0004  /* Fatal Error Reporting Enable */
619 #define PCIE_CTRL_URRE  0x0008  /* Unsupported Request Reporting En. */
620 #define PCIE_CTRL_RELAX_EN 0x0010 /* Enable relaxed ordering */
621 #define PCIE_CTRL_MAX_PAYLOAD(n) (((n) & 7) << 5)
622 #define PCIE_CTRL_EXT_TAG       0x0100  /* Extended Tag Field Enable */
623 #define PCIE_CTRL_PHANTOM       0x0200  /* Phantom Functions Enable */
624 #define PCIE_CTRL_AUX_PME       0x0400  /* Auxiliary Power PM Enable */
625 #define PCIE_CTRL_NOSNOOP_EN    0x0800 /* Enable No Snoop */
626 #define PCIE_CTRL_MAX_READ_REQUEST(n) (((n) & 7) << 12)
627
628   u16 dev_status;
629 #define PCIE_DEVSTA_AUXPD       0x10    /* AUX Power Detected */
630 #define PCIE_DEVSTA_TRPND       0x20    /* Transactions Pending */
631
632   u32 link_capabilities;
633   u16 link_control;
634   u16 link_status;
635
636   u32 slot_capabilities;
637   u16 slot_control;
638   u16 slot_status;
639
640   u16 root_control;
641 #define PCIE_RTCTL_SECEE        0x01    /* System Error on Correctable Error */
642 #define PCIE_RTCTL_SENFEE       0x02    /* System Error on Non-Fatal Error */
643 #define PCIE_RTCTL_SEFEE        0x04    /* System Error on Fatal Error */
644 #define PCIE_RTCTL_PMEIE        0x08    /* PME Interrupt Enable */
645 #define PCIE_RTCTL_CRSSVE       0x10    /* CRS Software Visibility Enable */
646
647   u16 root_capabilities;
648   u32 root_status;
649
650   u32 dev_capabilities2;
651   u16 dev_control2;
652   u16 dev_status2;
653
654   u32 link_capabilities2;
655   u16 link_control2;
656   u16 link_status2;
657
658   u32 slot_capabilities2;
659   u16 slot_control2;
660   u16 slot_status2;
661 }) pcie_config_regs_t;
662
663 /* PCI express extended capabilities. */
664 typedef enum pcie_capability_type {
665   PCIE_CAP_ADVANCED_ERROR = 1,
666   PCIE_CAP_VC = 2,
667   PCIE_CAP_DSN = 3,
668   PCIE_CAP_PWR = 4,
669 } pcie_capability_type_t;
670
671 /* Common header for capabilities. */
672 typedef CLIB_PACKED (struct {
673   enum pcie_capability_type type : 16;
674
675   u16 version : 4;
676
677   u16 next_capability : 12;
678 }) pcie_capability_regs_t;
679
680 typedef CLIB_PACKED (struct {
681   pcie_capability_regs_t header;
682
683   u32 uncorrectable_status;
684 #define PCIE_ERROR_UNC_LINK_TRAINING            (1 << 0)
685 #define PCIE_ERROR_UNC_DATA_LINK_PROTOCOL       (1 << 4)
686 #define PCIE_ERROR_UNC_SURPRISE_DOWN            (1 << 5)
687 #define PCIE_ERROR_UNC_POISONED_TLP             (1 << 12)
688 #define PCIE_ERROR_UNC_FLOW_CONTROL             (1 << 13)
689 #define PCIE_ERROR_UNC_COMPLETION_TIMEOUT       (1 << 14)
690 #define PCIE_ERROR_UNC_COMPLETER_ABORT          (1 << 15)
691 #define PCIE_ERROR_UNC_UNEXPECTED_COMPLETION    (1 << 16)
692 #define PCIE_ERROR_UNC_RX_OVERFLOW              (1 << 17)
693 #define PCIE_ERROR_UNC_MALFORMED_TLP            (1 << 18)
694 #define PCIE_ERROR_UNC_CRC_ERROR                (1 << 19)
695 #define PCIE_ERROR_UNC_UNSUPPORTED_REQUEST      (1 << 20)
696   u32 uncorrectable_mask;
697   u32 uncorrectable_severity;
698
699   u32 correctable_status;
700 #define PCIE_ERROR_COR_RX_ERROR         (1 << 0)
701 #define PCIE_ERROR_COR_BAD_TLP          (1 << 6)
702 #define PCIE_ERROR_COR_BAD_DLLP         (1 << 7)
703 #define PCIE_ERROR_COR_REPLAY_ROLLOVER  (1 << 8)
704 #define PCIE_ERROR_COR_REPLAY_TIMER     (1 << 12)
705 #define PCIE_ERROR_COR_ADVISORY         (1 << 13)
706
707   u32 correctable_mask;
708   u32 control;
709   u32 log[4];
710
711   u32 root_command;
712
713   u32 root_status;
714   u16 correctable_error_source;
715   u16 error_source;
716 }) pcie_advanced_error_regs_t;
717
718 /* Virtual Channel */
719 #define PCI_VC_PORT_REG1        4
720 #define PCI_VC_PORT_REG2        8
721 #define PCI_VC_PORT_CTRL        12
722 #define PCI_VC_PORT_STATUS      14
723 #define PCI_VC_RES_CAP          16
724 #define PCI_VC_RES_CTRL         20
725 #define PCI_VC_RES_STATUS       26
726
727 /* Power Budgeting */
728 #define PCI_PWR_DSR             4       /* Data Select Register */
729 #define PCI_PWR_DATA            8       /* Data Register */
730 #define PCI_PWR_DATA_BASE(x)    ((x) & 0xff)      /* Base Power */
731 #define PCI_PWR_DATA_SCALE(x)   (((x) >> 8) & 3)  /* Data Scale */
732 #define PCI_PWR_DATA_PM_SUB(x)  (((x) >> 10) & 7)  /* PM Sub State */
733 #define PCI_PWR_DATA_PM_STATE(x) (((x) >> 13) & 3) /* PM State */
734 #define PCI_PWR_DATA_TYPE(x)    (((x) >> 15) & 7)  /* Type */
735 #define PCI_PWR_DATA_RAIL(x)    (((x) >> 18) & 7)  /* Power Rail */
736 #define PCI_PWR_CAP             12      /* Capability */
737 #define PCI_PWR_CAP_BUDGET(x)   ((x) & 1)       /* Included in system budget */
738
739 #endif /* included_vlib_pci_config_h */