Zephyr Project API  3.1.0
A Scalable Open Source RTOS
pcie.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_DRIVERS_PCIE_PCIE_H_
8#define ZEPHYR_INCLUDE_DRIVERS_PCIE_PCIE_H_
9
10#include <stddef.h>
12#include <zephyr/types.h>
13
14#ifdef __cplusplus
15extern "C" {
16#endif
17
28
38
39struct pcie_mbar {
41 size_t size;
42};
43
44/*
45 * These functions are arch-, board-, or SoC-specific.
46 */
47
58
68extern uint32_t pcie_conf_read(pcie_bdf_t bdf, unsigned int reg);
69
79extern void pcie_conf_write(pcie_bdf_t bdf, unsigned int reg, uint32_t data);
80
88extern bool pcie_probe(pcie_bdf_t bdf, pcie_id_t id);
89
97extern bool pcie_get_mbar(pcie_bdf_t bdf,
98 unsigned int bar_index,
99 struct pcie_mbar *mbar);
100
115 unsigned int index,
116 struct pcie_mbar *mbar);
117
125extern void pcie_set_cmd(pcie_bdf_t bdf, uint32_t bits, bool on);
126
127#ifndef CONFIG_PCIE_CONTROLLER
141extern unsigned int pcie_alloc_irq(pcie_bdf_t bdf);
142#endif /* CONFIG_PCIE_CONTROLLER */
143
150extern unsigned int pcie_get_irq(pcie_bdf_t bdf);
151
164extern void pcie_irq_enable(pcie_bdf_t bdf, unsigned int irq);
165
174
183
196 unsigned int irq,
197 unsigned int priority,
198 void (*routine)(const void *parameter),
199 const void *parameter,
201
202/*
203 * Configuration word 13 contains the head of the capabilities list.
204 */
205
206#define PCIE_CONF_CAPPTR 13U /* capabilities pointer */
207#define PCIE_CONF_CAPPTR_FIRST(w) (((w) >> 2) & 0x3FU)
208
209/*
210 * The first word of every capability contains a capability identifier,
211 * and a link to the next capability (or 0) in configuration space.
212 */
213
214#define PCIE_CONF_CAP_ID(w) ((w) & 0xFFU)
215#define PCIE_CONF_CAP_NEXT(w) (((w) >> 10) & 0x3FU)
216
217/*
218 * The extended PCI Express capabilities lie at the end of the PCI configuration space
219 */
220
221#define PCIE_CONF_EXT_CAPPTR 64U
222
223/*
224 * The first word of every capability contains an extended capability identifier,
225 * and a link to the next capability (or 0) in the extended configuration space.
226 */
227
228#define PCIE_CONF_EXT_CAP_ID(w) ((w) & 0xFFFFU)
229#define PCIE_CONF_EXT_CAP_VER(w) (((w) >> 16) & 0xFU)
230#define PCIE_CONF_EXT_CAP_NEXT(w) (((w) >> 20) & 0xFFFU)
231
232/*
233 * Configuration word 0 aligns directly with pcie_id_t.
234 */
235
236#define PCIE_CONF_ID 0U
237
238/*
239 * Configuration word 1 contains command and status bits.
240 */
241
242#define PCIE_CONF_CMDSTAT 1U /* command/status register */
243
244#define PCIE_CONF_CMDSTAT_IO 0x00000001U /* I/O access enable */
245#define PCIE_CONF_CMDSTAT_MEM 0x00000002U /* mem access enable */
246#define PCIE_CONF_CMDSTAT_MASTER 0x00000004U /* bus master enable */
247#define PCIE_CONF_CMDSTAT_CAPS 0x00100000U /* capabilities list */
248
249/*
250 * Configuration word 2 has additional function identification that
251 * we only care about for debug output (PCIe shell commands).
252 */
253
254#define PCIE_CONF_CLASSREV 2U /* class/revision register */
255
256#define PCIE_CONF_CLASSREV_CLASS(w) (((w) >> 24) & 0xFFU)
257#define PCIE_CONF_CLASSREV_SUBCLASS(w) (((w) >> 16) & 0xFFU)
258#define PCIE_CONF_CLASSREV_PROGIF(w) (((w) >> 8) & 0xFFU)
259#define PCIE_CONF_CLASSREV_REV(w) ((w) & 0xFFU)
260
261/*
262 * The only part of configuration word 3 that is of interest to us is
263 * the header type, as we use it to distinguish functional endpoints
264 * from bridges (which are, for our purposes, transparent).
265 */
266
267#define PCIE_CONF_TYPE 3U
268
269#define PCIE_CONF_MULTIFUNCTION(w) (((w) & 0x00800000U) != 0U)
270#define PCIE_CONF_TYPE_BRIDGE(w) (((w) & 0x007F0000U) != 0U)
271
272/*
273 * Words 4-9 are BARs are I/O or memory decoders. Memory decoders may
274 * be 64-bit decoders, in which case the next configuration word holds
275 * the high-order bits (and is, thus, not a BAR itself).
276 */
277
278#define PCIE_CONF_BAR0 4U
279#define PCIE_CONF_BAR1 5U
280#define PCIE_CONF_BAR2 6U
281#define PCIE_CONF_BAR3 7U
282#define PCIE_CONF_BAR4 8U
283#define PCIE_CONF_BAR5 9U
284
285#define PCIE_CONF_BAR_IO(w) (((w) & 0x00000001U) == 0x00000001U)
286#define PCIE_CONF_BAR_MEM(w) (((w) & 0x00000001U) != 0x00000001U)
287#define PCIE_CONF_BAR_64(w) (((w) & 0x00000006U) == 0x00000004U)
288#define PCIE_CONF_BAR_ADDR(w) ((w) & ~0xfUL)
289#define PCIE_CONF_BAR_IO_ADDR(w) ((w) & ~0x3UL)
290#define PCIE_CONF_BAR_FLAGS(w) ((w) & 0xfUL)
291#define PCIE_CONF_BAR_NONE 0U
292
293#define PCIE_CONF_BAR_INVAL 0xFFFFFFF0U
294#define PCIE_CONF_BAR_INVAL64 0xFFFFFFFFFFFFFFF0UL
295
296#define PCIE_CONF_BAR_INVAL_FLAGS(w) \
297 ((((w) & 0x00000006U) == 0x00000006U) || \
298 (((w) & 0x00000006U) == 0x00000002U))
299
300/*
301 * Type 1 Header has files related to bus management
302 */
303#define PCIE_BUS_NUMBER 6U
304
305#define PCIE_BUS_PRIMARY_NUMBER(w) ((w) & 0xffUL)
306#define PCIE_BUS_SECONDARY_NUMBER(w) (((w) >> 8) & 0xffUL)
307#define PCIE_BUS_SUBORDINATE_NUMBER(w) (((w) >> 16) & 0xffUL)
308#define PCIE_SECONDARY_LATENCY_TIMER(w) (((w) >> 24) & 0xffUL)
309
310#define PCIE_BUS_NUMBER_VAL(prim, sec, sub, lat) \
311 (((prim) & 0xffUL) | \
312 (((sec) & 0xffUL) << 8) | \
313 (((sub) & 0xffUL) << 16) | \
314 (((lat) & 0xffUL) << 24))
315
316/*
317 * Type 1 words 7 to 12 setups Bridge Memory base and limits
318 */
319#define PCIE_IO_SEC_STATUS 7U
320
321#define PCIE_IO_BASE(w) ((w) & 0xffUL)
322#define PCIE_IO_LIMIT(w) (((w) >> 8) & 0xffUL)
323#define PCIE_SEC_STATUS(w) (((w) >> 16) & 0xffffUL)
324
325#define PCIE_IO_SEC_STATUS_VAL(iob, iol, sec_status) \
326 (((iob) & 0xffUL) | \
327 (((iol) & 0xffUL) << 8) | \
328 (((sec_status) & 0xffffUL) << 16))
329
330#define PCIE_MEM_BASE_LIMIT 8U
331
332#define PCIE_MEM_BASE(w) ((w) & 0xffffUL)
333#define PCIE_MEM_LIMIT(w) (((w) >> 16) & 0xffffUL)
334
335#define PCIE_MEM_BASE_LIMIT_VAL(memb, meml) \
336 (((memb) & 0xffffUL) | \
337 (((meml) & 0xffffUL) << 16))
338
339#define PCIE_PREFETCH_BASE_LIMIT 9U
340
341#define PCIE_PREFETCH_BASE(w) ((w) & 0xffffUL)
342#define PCIE_PREFETCH_LIMIT(w) (((w) >> 16) & 0xffffUL)
343
344#define PCIE_PREFETCH_BASE_LIMIT_VAL(pmemb, pmeml) \
345 (((pmemb) & 0xffffUL) | \
346 (((pmeml) & 0xffffUL) << 16))
347
348#define PCIE_PREFETCH_BASE_UPPER 10U
349
350#define PCIE_PREFETCH_LIMIT_UPPER 11U
351
352#define PCIE_IO_BASE_LIMIT_UPPER 12U
353
354#define PCIE_IO_BASE_UPPER(w) ((w) & 0xffffUL)
355#define PCIE_IO_LIMIT_UPPER(w) (((w) >> 16) & 0xffffUL)
356
357#define PCIE_IO_BASE_LIMIT_UPPER_VAL(iobu, iolu) \
358 (((iobu) & 0xffffUL) | \
359 (((iolu) & 0xffffUL) << 16))
360
361/*
362 * Word 15 contains information related to interrupts.
363 *
364 * We're only interested in the low byte, which is [supposed to be] set by
365 * the firmware to indicate which wire IRQ the device interrupt is routed to.
366 */
367
368#define PCIE_CONF_INTR 15U
369
370#define PCIE_CONF_INTR_IRQ(w) ((w) & 0xFFU)
371#define PCIE_CONF_INTR_IRQ_NONE 0xFFU /* no interrupt routed */
372
373#define PCIE_MAX_BUS (0xFFFFFFFF & PCIE_BDF_BUS_MASK)
374#define PCIE_MAX_DEV (0xFFFFFFFF & PCIE_BDF_DEV_MASK)
375#define PCIE_MAX_FUNC (0xFFFFFFFF & PCIE_BDF_FUNC_MASK)
376
391#define PCIE_IRQ_CONNECT(bdf_p, irq_p, priority_p, \
392 isr_p, isr_param_p, flags_p) \
393 ARCH_PCIE_IRQ_CONNECT(bdf_p, irq_p, priority_p, \
394 isr_p, isr_param_p, flags_p)
395
396#ifdef __cplusplus
397}
398#endif
399
400#endif /* ZEPHYR_INCLUDE_DRIVERS_PCIE_PCIE_H_ */
ZTEST_BMEM int index[(3)]
Definition: main.c:32
uint32_t pcie_conf_read(pcie_bdf_t bdf, unsigned int reg)
Read a 32-bit word from an endpoint's configuration space.
bool pcie_connect_dynamic_irq(pcie_bdf_t bdf, unsigned int irq, unsigned int priority, void(*routine)(const void *parameter), const void *parameter, uint32_t flags)
Dynamically connect a PCIe endpoint IRQ to an ISR handler.
void pcie_set_cmd(pcie_bdf_t bdf, uint32_t bits, bool on)
Set or reset bits in the endpoint command/status register.
bool pcie_probe(pcie_bdf_t bdf, pcie_id_t id)
Probe for the presence of a PCI(e) endpoint.
uint32_t pcie_id_t
A unique PCI(e) identifier (vendor ID, device ID).
Definition: pcie.h:37
bool pcie_get_mbar(pcie_bdf_t bdf, unsigned int bar_index, struct pcie_mbar *mbar)
Get the MBAR at a specific BAR index.
void pcie_conf_write(pcie_bdf_t bdf, unsigned int reg, uint32_t data)
Write a 32-bit word to an endpoint's configuration space.
uint32_t pcie_get_cap(pcie_bdf_t bdf, uint32_t cap_id)
Find a PCI(e) capability in an endpoint's configuration space.
uint32_t pcie_get_ext_cap(pcie_bdf_t bdf, uint32_t cap_id)
Find an Extended PCI(e) capability in an endpoint's configuration space.
uint32_t pcie_bdf_t
A unique PCI(e) endpoint (bus, device, function).
Definition: pcie.h:27
pcie_bdf_t pcie_bdf_lookup(pcie_id_t id)
Look up the BDF based on PCI(e) vendor & device ID.
unsigned int pcie_alloc_irq(pcie_bdf_t bdf)
Allocate an IRQ for an endpoint.
bool pcie_probe_mbar(pcie_bdf_t bdf, unsigned int index, struct pcie_mbar *mbar)
Probe the nth MMIO address assigned to an endpoint.
unsigned int pcie_get_irq(pcie_bdf_t bdf)
Return the IRQ assigned by the firmware/board to an endpoint.
void pcie_irq_enable(pcie_bdf_t bdf, unsigned int irq)
Enable the PCI(e) endpoint to generate the specified IRQ.
flags
Definition: http_parser.h:131
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__UINTPTR_TYPE__ uintptr_t
Definition: stdint.h:105
Definition: pcie.h:39
size_t size
Definition: pcie.h:41
uintptr_t phys_addr
Definition: pcie.h:40
static fdata_t data[2]
Definition: test_fifo_contexts.c:15