Zephyr Project API  3.2.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_INTERRUPT 0x00080000U /* interrupt status */
248#define PCIE_CONF_CMDSTAT_CAPS 0x00100000U /* capabilities list */
249
250/*
251 * Configuration word 2 has additional function identification that
252 * we only care about for debug output (PCIe shell commands).
253 */
254
255#define PCIE_CONF_CLASSREV 2U /* class/revision register */
256
257#define PCIE_CONF_CLASSREV_CLASS(w) (((w) >> 24) & 0xFFU)
258#define PCIE_CONF_CLASSREV_SUBCLASS(w) (((w) >> 16) & 0xFFU)
259#define PCIE_CONF_CLASSREV_PROGIF(w) (((w) >> 8) & 0xFFU)
260#define PCIE_CONF_CLASSREV_REV(w) ((w) & 0xFFU)
261
262/*
263 * The only part of configuration word 3 that is of interest to us is
264 * the header type, as we use it to distinguish functional endpoints
265 * from bridges (which are, for our purposes, transparent).
266 */
267
268#define PCIE_CONF_TYPE 3U
269
270#define PCIE_CONF_MULTIFUNCTION(w) (((w) & 0x00800000U) != 0U)
271#define PCIE_CONF_TYPE_BRIDGE(w) (((w) & 0x007F0000U) != 0U)
272
273/*
274 * Words 4-9 are BARs are I/O or memory decoders. Memory decoders may
275 * be 64-bit decoders, in which case the next configuration word holds
276 * the high-order bits (and is, thus, not a BAR itself).
277 */
278
279#define PCIE_CONF_BAR0 4U
280#define PCIE_CONF_BAR1 5U
281#define PCIE_CONF_BAR2 6U
282#define PCIE_CONF_BAR3 7U
283#define PCIE_CONF_BAR4 8U
284#define PCIE_CONF_BAR5 9U
285
286#define PCIE_CONF_BAR_IO(w) (((w) & 0x00000001U) == 0x00000001U)
287#define PCIE_CONF_BAR_MEM(w) (((w) & 0x00000001U) != 0x00000001U)
288#define PCIE_CONF_BAR_64(w) (((w) & 0x00000006U) == 0x00000004U)
289#define PCIE_CONF_BAR_ADDR(w) ((w) & ~0xfUL)
290#define PCIE_CONF_BAR_IO_ADDR(w) ((w) & ~0x3UL)
291#define PCIE_CONF_BAR_FLAGS(w) ((w) & 0xfUL)
292#define PCIE_CONF_BAR_NONE 0U
293
294#define PCIE_CONF_BAR_INVAL 0xFFFFFFF0U
295#define PCIE_CONF_BAR_INVAL64 0xFFFFFFFFFFFFFFF0UL
296
297#define PCIE_CONF_BAR_INVAL_FLAGS(w) \
298 ((((w) & 0x00000006U) == 0x00000006U) || \
299 (((w) & 0x00000006U) == 0x00000002U))
300
301/*
302 * Type 1 Header has files related to bus management
303 */
304#define PCIE_BUS_NUMBER 6U
305
306#define PCIE_BUS_PRIMARY_NUMBER(w) ((w) & 0xffUL)
307#define PCIE_BUS_SECONDARY_NUMBER(w) (((w) >> 8) & 0xffUL)
308#define PCIE_BUS_SUBORDINATE_NUMBER(w) (((w) >> 16) & 0xffUL)
309#define PCIE_SECONDARY_LATENCY_TIMER(w) (((w) >> 24) & 0xffUL)
310
311#define PCIE_BUS_NUMBER_VAL(prim, sec, sub, lat) \
312 (((prim) & 0xffUL) | \
313 (((sec) & 0xffUL) << 8) | \
314 (((sub) & 0xffUL) << 16) | \
315 (((lat) & 0xffUL) << 24))
316
317/*
318 * Type 1 words 7 to 12 setups Bridge Memory base and limits
319 */
320#define PCIE_IO_SEC_STATUS 7U
321
322#define PCIE_IO_BASE(w) ((w) & 0xffUL)
323#define PCIE_IO_LIMIT(w) (((w) >> 8) & 0xffUL)
324#define PCIE_SEC_STATUS(w) (((w) >> 16) & 0xffffUL)
325
326#define PCIE_IO_SEC_STATUS_VAL(iob, iol, sec_status) \
327 (((iob) & 0xffUL) | \
328 (((iol) & 0xffUL) << 8) | \
329 (((sec_status) & 0xffffUL) << 16))
330
331#define PCIE_MEM_BASE_LIMIT 8U
332
333#define PCIE_MEM_BASE(w) ((w) & 0xffffUL)
334#define PCIE_MEM_LIMIT(w) (((w) >> 16) & 0xffffUL)
335
336#define PCIE_MEM_BASE_LIMIT_VAL(memb, meml) \
337 (((memb) & 0xffffUL) | \
338 (((meml) & 0xffffUL) << 16))
339
340#define PCIE_PREFETCH_BASE_LIMIT 9U
341
342#define PCIE_PREFETCH_BASE(w) ((w) & 0xffffUL)
343#define PCIE_PREFETCH_LIMIT(w) (((w) >> 16) & 0xffffUL)
344
345#define PCIE_PREFETCH_BASE_LIMIT_VAL(pmemb, pmeml) \
346 (((pmemb) & 0xffffUL) | \
347 (((pmeml) & 0xffffUL) << 16))
348
349#define PCIE_PREFETCH_BASE_UPPER 10U
350
351#define PCIE_PREFETCH_LIMIT_UPPER 11U
352
353#define PCIE_IO_BASE_LIMIT_UPPER 12U
354
355#define PCIE_IO_BASE_UPPER(w) ((w) & 0xffffUL)
356#define PCIE_IO_LIMIT_UPPER(w) (((w) >> 16) & 0xffffUL)
357
358#define PCIE_IO_BASE_LIMIT_UPPER_VAL(iobu, iolu) \
359 (((iobu) & 0xffffUL) | \
360 (((iolu) & 0xffffUL) << 16))
361
362/*
363 * Word 15 contains information related to interrupts.
364 *
365 * We're only interested in the low byte, which is [supposed to be] set by
366 * the firmware to indicate which wire IRQ the device interrupt is routed to.
367 */
368
369#define PCIE_CONF_INTR 15U
370
371#define PCIE_CONF_INTR_IRQ(w) ((w) & 0xFFU)
372#define PCIE_CONF_INTR_IRQ_NONE 0xFFU /* no interrupt routed */
373
374#define PCIE_MAX_BUS (0xFFFFFFFFU & PCIE_BDF_BUS_MASK)
375#define PCIE_MAX_DEV (0xFFFFFFFFU & PCIE_BDF_DEV_MASK)
376#define PCIE_MAX_FUNC (0xFFFFFFFFU & PCIE_BDF_FUNC_MASK)
377
392#define PCIE_IRQ_CONNECT(bdf_p, irq_p, priority_p, \
393 isr_p, isr_param_p, flags_p) \
394 ARCH_PCIE_IRQ_CONNECT(bdf_p, irq_p, priority_p, \
395 isr_p, isr_param_p, flags_p)
396
397#ifdef __cplusplus
398}
399#endif
400
401#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