Zephyr API Documentation 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
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
12
13#ifndef ZEPHYR_INCLUDE_DRIVERS_PCIE_PCIE_H_
14#define ZEPHYR_INCLUDE_DRIVERS_PCIE_PCIE_H_
15
28
29#include <stddef.h>
30#include <zephyr/devicetree.h>
32#include <zephyr/types.h>
33#include <zephyr/kernel.h>
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
49
58
59/* Helper macro to exclude invalid PCIe identifiers. We should really only
60 * need to look for PCIE_ID_NONE, but because of some broken PCI host controllers
61 * we have try cases where both VID & DID are zero or just one of them is
62 * zero (0x0000) and the other is all ones (0xFFFF).
63 */
64#define PCIE_ID_IS_VALID(id) ((id != PCIE_ID_NONE) && \
65 (id != PCIE_ID(0x0000, 0x0000)) && \
66 (id != PCIE_ID(0xFFFF, 0x0000)) && \
67 (id != PCIE_ID(0x0000, 0xFFFF)))
68
75
76#define Z_DEVICE_PCIE_NAME(node_id) _CONCAT(pcie_dev_, DT_DEP_ORD(node_id))
77
84#define PCIE_DT_ID(node_id) PCIE_ID(DT_PROP_OR(node_id, vendor_id, 0xffff), \
85 DT_PROP_OR(node_id, device_id, 0xffff))
86
96#define PCIE_DT_INST_ID(inst) PCIE_DT_ID(DT_DRV_INST(inst))
97
106#define DEVICE_PCIE_DECLARE(node_id) \
107 STRUCT_SECTION_ITERABLE(pcie_dev, Z_DEVICE_PCIE_NAME(node_id)) = { \
108 .bdf = PCIE_BDF_NONE, \
109 .id = PCIE_DT_ID(node_id), \
110 .class_rev = DT_PROP_OR(node_id, class_rev, 0), \
111 .class_rev_mask = DT_PROP_OR(node_id, class_rev_mask, 0), \
112 }
113
122#define DEVICE_PCIE_INST_DECLARE(inst) DEVICE_PCIE_DECLARE(DT_DRV_INST(inst))
123
148#define DEVICE_PCIE_INIT(node_id, name) .name = &Z_DEVICE_PCIE_NAME(node_id)
149
159#define DEVICE_PCIE_INST_INIT(inst, name) \
160 DEVICE_PCIE_INIT(DT_DRV_INST(inst), name)
161
162struct pcie_bar {
164 size_t size;
165};
166
167/*
168 * These functions are arch-, board-, or SoC-specific.
169 */
170
180extern uint32_t pcie_conf_read(pcie_bdf_t bdf, unsigned int reg);
181
191extern void pcie_conf_write(pcie_bdf_t bdf, unsigned int reg, uint32_t data);
192
201typedef bool (*pcie_scan_cb_t)(pcie_bdf_t bdf, pcie_id_t id, void *cb_data);
202
203enum {
208};
209
224
232int pcie_scan(const struct pcie_scan_opt *opt);
233
241extern bool pcie_get_mbar(pcie_bdf_t bdf,
242 unsigned int bar_index,
243 struct pcie_bar *mbar);
244
259 unsigned int index,
260 struct pcie_bar *mbar);
261
270 unsigned int bar_index,
271 struct pcie_bar *iobar);
272
287 unsigned int index,
288 struct pcie_bar *iobar);
289
297extern void pcie_set_cmd(pcie_bdf_t bdf, uint32_t bits, bool on);
298
299#ifndef CONFIG_PCIE_CONTROLLER
313extern unsigned int pcie_alloc_irq(pcie_bdf_t bdf);
314#endif /* CONFIG_PCIE_CONTROLLER */
315
322extern unsigned int pcie_get_irq(pcie_bdf_t bdf);
323
336extern void pcie_irq_enable(pcie_bdf_t bdf, unsigned int irq);
337
346
355
368 unsigned int irq,
369 unsigned int priority,
370 void (*routine)(const void *parameter),
371 const void *parameter,
373
384#define PCIE_HOST_CONTROLLER(n) PCIE_BDF(0, 0, n)
385
386/*
387 * Configuration word 13 contains the head of the capabilities list.
388 */
389
390#define PCIE_CONF_CAPPTR 13U /* capabilities pointer */
391#define PCIE_CONF_CAPPTR_FIRST(w) (((w) >> 2) & 0x3FU)
392
393/*
394 * The first word of every capability contains a capability identifier,
395 * and a link to the next capability (or 0) in configuration space.
396 */
397
398#define PCIE_CONF_CAP_ID(w) ((w) & 0xFFU)
399#define PCIE_CONF_CAP_NEXT(w) (((w) >> 10) & 0x3FU)
400
401/*
402 * The extended PCI Express capabilities lie at the end of the PCI configuration space
403 */
404
405#define PCIE_CONF_EXT_CAPPTR 64U
406
407/*
408 * The first word of every capability contains an extended capability identifier,
409 * and a link to the next capability (or 0) in the extended configuration space.
410 */
411
412#define PCIE_CONF_EXT_CAP_ID(w) ((w) & 0xFFFFU)
413#define PCIE_CONF_EXT_CAP_VER(w) (((w) >> 16) & 0xFU)
414#define PCIE_CONF_EXT_CAP_NEXT(w) (((w) >> 20) & 0xFFFU)
415
416/*
417 * Configuration word 0 aligns directly with pcie_id_t.
418 */
419
420#define PCIE_CONF_ID 0U
421
422/*
423 * Configuration word 1 contains command and status bits.
424 */
425
426#define PCIE_CONF_CMDSTAT 1U /* command/status register */
427
428#define PCIE_CONF_CMDSTAT_IO 0x00000001U /* I/O access enable */
429#define PCIE_CONF_CMDSTAT_MEM 0x00000002U /* mem access enable */
430#define PCIE_CONF_CMDSTAT_MASTER 0x00000004U /* bus master enable */
431#define PCIE_CONF_CMDSTAT_SERR 0x00000100U /* SERR# enable */
432#define PCIE_CONF_CMDSTAT_INTERRUPT 0x00080000U /* interrupt status */
433#define PCIE_CONF_CMDSTAT_CAPS 0x00100000U /* capabilities list */
434
435/*
436 * Configuration word 2 has additional function identification that
437 * we only care about for debug output (PCIe shell commands).
438 */
439
440#define PCIE_CONF_CLASSREV 2U /* class/revision register */
441
442#define PCIE_CONF_CLASSREV_CLASS(w) (((w) >> 24) & 0xFFU)
443#define PCIE_CONF_CLASSREV_SUBCLASS(w) (((w) >> 16) & 0xFFU)
444#define PCIE_CONF_CLASSREV_PROGIF(w) (((w) >> 8) & 0xFFU)
445#define PCIE_CONF_CLASSREV_REV(w) ((w) & 0xFFU)
446
447/*
448 * The only part of configuration word 3 that is of interest to us is
449 * the header type, as we use it to distinguish functional endpoints
450 * from bridges (which are, for our purposes, transparent).
451 */
452
453#define PCIE_CONF_TYPE 3U
454
455#define PCIE_CONF_MULTIFUNCTION(w) (((w) & 0x00800000U) != 0U)
456#define PCIE_CONF_TYPE_BRIDGE(w) (((w) & 0x007F0000U) != 0U)
457#define PCIE_CONF_TYPE_GET(w) (((w) >> 16) & 0x7F)
458
459#define PCIE_CONF_TYPE_STANDARD 0x0U
460#define PCIE_CONF_TYPE_PCI_BRIDGE 0x1U
461#define PCIE_CONF_TYPE_CARDBUS_BRIDGE 0x2U
462
463/*
464 * Words 4-9 are BARs are I/O or memory decoders. Memory decoders may
465 * be 64-bit decoders, in which case the next configuration word holds
466 * the high-order bits (and is, thus, not a BAR itself).
467 */
468
469#define PCIE_CONF_BAR0 4U
470#define PCIE_CONF_BAR1 5U
471#define PCIE_CONF_BAR2 6U
472#define PCIE_CONF_BAR3 7U
473#define PCIE_CONF_BAR4 8U
474#define PCIE_CONF_BAR5 9U
475
476#define PCIE_CONF_BAR_IO(w) (((w) & 0x00000001U) == 0x00000001U)
477#define PCIE_CONF_BAR_MEM(w) (((w) & 0x00000001U) != 0x00000001U)
478#define PCIE_CONF_BAR_64(w) (((w) & 0x00000006U) == 0x00000004U)
479#define PCIE_CONF_BAR_ADDR(w) ((w) & ~0xfUL)
480#define PCIE_CONF_BAR_IO_ADDR(w) ((w) & ~0x3UL)
481#define PCIE_CONF_BAR_FLAGS(w) ((w) & 0xfUL)
482#define PCIE_CONF_BAR_NONE 0U
483
484#define PCIE_CONF_BAR_INVAL 0xFFFFFFF0U
485#define PCIE_CONF_BAR_INVAL64 0xFFFFFFFFFFFFFFF0UL
486
487#define PCIE_CONF_BAR_INVAL_FLAGS(w) \
488 ((((w) & 0x00000006U) == 0x00000006U) || \
489 (((w) & 0x00000006U) == 0x00000002U))
490
491/*
492 * Type 1 Header has files related to bus management
493 */
494#define PCIE_BUS_NUMBER 6U
495
496#define PCIE_BUS_PRIMARY_NUMBER(w) ((w) & 0xffUL)
497#define PCIE_BUS_SECONDARY_NUMBER(w) (((w) >> 8) & 0xffUL)
498#define PCIE_BUS_SUBORDINATE_NUMBER(w) (((w) >> 16) & 0xffUL)
499#define PCIE_SECONDARY_LATENCY_TIMER(w) (((w) >> 24) & 0xffUL)
500
501#define PCIE_BUS_NUMBER_VAL(prim, sec, sub, lat) \
502 (((prim) & 0xffUL) | \
503 (((sec) & 0xffUL) << 8) | \
504 (((sub) & 0xffUL) << 16) | \
505 (((lat) & 0xffUL) << 24))
506
507/*
508 * Type 1 words 7 to 12 setups Bridge Memory base and limits
509 */
510#define PCIE_IO_SEC_STATUS 7U
511
512#define PCIE_IO_BASE(w) ((w) & 0xffUL)
513#define PCIE_IO_LIMIT(w) (((w) >> 8) & 0xffUL)
514#define PCIE_SEC_STATUS(w) (((w) >> 16) & 0xffffUL)
515
516#define PCIE_IO_SEC_STATUS_VAL(iob, iol, sec_status) \
517 (((iob) & 0xffUL) | \
518 (((iol) & 0xffUL) << 8) | \
519 (((sec_status) & 0xffffUL) << 16))
520
521#define PCIE_MEM_BASE_LIMIT 8U
522
523#define PCIE_MEM_BASE(w) ((w) & 0xffffUL)
524#define PCIE_MEM_LIMIT(w) (((w) >> 16) & 0xffffUL)
525
526#define PCIE_MEM_BASE_LIMIT_VAL(memb, meml) \
527 (((memb) & 0xffffUL) | \
528 (((meml) & 0xffffUL) << 16))
529
530#define PCIE_PREFETCH_BASE_LIMIT 9U
531
532#define PCIE_PREFETCH_BASE(w) ((w) & 0xffffUL)
533#define PCIE_PREFETCH_LIMIT(w) (((w) >> 16) & 0xffffUL)
534
535#define PCIE_PREFETCH_BASE_LIMIT_VAL(pmemb, pmeml) \
536 (((pmemb) & 0xffffUL) | \
537 (((pmeml) & 0xffffUL) << 16))
538
539#define PCIE_PREFETCH_BASE_UPPER 10U
540
541#define PCIE_PREFETCH_LIMIT_UPPER 11U
542
543#define PCIE_IO_BASE_LIMIT_UPPER 12U
544
545#define PCIE_IO_BASE_UPPER(w) ((w) & 0xffffUL)
546#define PCIE_IO_LIMIT_UPPER(w) (((w) >> 16) & 0xffffUL)
547
548#define PCIE_IO_BASE_LIMIT_UPPER_VAL(iobu, iolu) \
549 (((iobu) & 0xffffUL) | \
550 (((iolu) & 0xffffUL) << 16))
551
552/*
553 * Word 15 contains information related to interrupts.
554 *
555 * We're only interested in the low byte, which is [supposed to be] set by
556 * the firmware to indicate which wire IRQ the device interrupt is routed to.
557 */
558
559#define PCIE_CONF_INTR 15U
560
561#define PCIE_CONF_INTR_IRQ(w) ((w) & 0xFFU)
562#define PCIE_CONF_INTR_IRQ_NONE 0xFFU /* no interrupt routed */
563
564#define PCIE_MAX_BUS (0xFFFFFFFFU & PCIE_BDF_BUS_MASK)
565#define PCIE_MAX_DEV (0xFFFFFFFFU & PCIE_BDF_DEV_MASK)
566#define PCIE_MAX_FUNC (0xFFFFFFFFU & PCIE_BDF_FUNC_MASK)
567
582#define PCIE_IRQ_CONNECT(bdf_p, irq_p, priority_p, \
583 isr_p, isr_param_p, flags_p) \
584 ARCH_PCIE_IRQ_CONNECT(bdf_p, irq_p, priority_p, \
585 isr_p, isr_param_p, flags_p)
586
587#ifdef __cplusplus
588}
589#endif
590
594
595#endif /* ZEPHYR_INCLUDE_DRIVERS_PCIE_PCIE_H_ */
Devicetree main header.
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.
uint32_t pcie_id_t
A unique PCI(e) identifier (vendor ID, device ID).
Definition pcie.h:57
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_get_iobar(pcie_bdf_t bdf, unsigned int bar_index, struct pcie_bar *iobar)
Get the I/O BAR 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.
bool(* pcie_scan_cb_t)(pcie_bdf_t bdf, pcie_id_t id, void *cb_data)
Callback type used for scanning for PCI endpoints.
Definition pcie.h:201
int pcie_scan(const struct pcie_scan_opt *opt)
Scan for PCIe devices.
bool pcie_probe_iobar(pcie_bdf_t bdf, unsigned int index, struct pcie_bar *iobar)
Probe the nth I/O BAR address assigned to an endpoint.
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_bar *mbar)
Probe the nth MMIO address assigned to an endpoint.
bool pcie_get_mbar(pcie_bdf_t bdf, unsigned int bar_index, struct pcie_bar *mbar)
Get the MBAR at a specific BAR index.
unsigned int pcie_get_irq(pcie_bdf_t bdf)
Return the IRQ assigned by the firmware/board to an endpoint.
uint32_t pcie_bdf_t
A unique PCI(e) endpoint (bus, device, function).
Definition pcie.h:48
void pcie_irq_enable(pcie_bdf_t bdf, unsigned int irq)
Enable the PCI(e) endpoint to generate the specified IRQ.
@ PCIE_SCAN_RECURSIVE
Scan all available PCI host controllers and sub-buses.
Definition pcie.h:205
@ PCIE_SCAN_CB_ALL
Do the callback for all endpoint types, including bridges.
Definition pcie.h:207
#define BIT(n)
Unsigned integer with bit position n set (signed in assembly language).
Definition util_macro.h:44
Public kernel APIs.
flags
Definition parser.h:97
#define bool
Definition stdbool.h:13
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:105
Definition pcie.h:162
uintptr_t phys_addr
Definition pcie.h:163
size_t size
Definition pcie.h:164
Definition pcie.h:69
pcie_id_t id
Definition pcie.h:71
uint32_t class_rev_mask
Definition pcie.h:73
pcie_bdf_t bdf
Definition pcie.h:70
uint32_t class_rev
Definition pcie.h:72
Options for performing a scan for PCI devices.
Definition pcie.h:211
uint8_t bus
Initial bus number to scan.
Definition pcie.h:213
void * cb_data
Custom data to pass to the scan callback.
Definition pcie.h:219
pcie_scan_cb_t cb
Function to call for each found endpoint.
Definition pcie.h:216
uint32_t flags
Scan flags.
Definition pcie.h:222
Iterable sections helpers.