1 /*
2  * Copyright (c) 2020 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_VIRTUALIZATION_VIRT_IVSHMEM_H_
8 #define ZEPHYR_DRIVERS_VIRTUALIZATION_VIRT_IVSHMEM_H_
9 
10 #include <zephyr/drivers/pcie/pcie.h>
11 
12 #ifdef CONFIG_IVSHMEM_DOORBELL
13 #include <zephyr/drivers/pcie/msi.h>
14 #endif
15 
16 #define PCIE_CONF_CMDSTAT_INTX_DISABLE	0x0400
17 #define PCIE_CONF_INTR_PIN(x)		(((x) >> 8) & 0xFFu)
18 
19 #define IVSHMEM_CFG_ID			0x00
20 #define IVSHMEM_CFG_NEXT_CAP		0x01
21 #define IVSHMEM_CFG_LENGTH		0x02
22 #define IVSHMEM_CFG_PRIV_CNTL		0x03
23 #define IVSHMEM_PRIV_CNTL_ONESHOT_INT	BIT(0)
24 #define IVSHMEM_CFG_STATE_TAB_SZ	0x04
25 #define IVSHMEM_CFG_RW_SECTION_SZ	0x08
26 #define IVSHMEM_CFG_OUTPUT_SECTION_SZ	0x10
27 #define IVSHMEM_CFG_ADDRESS		0x18
28 
29 #define IVSHMEM_INT_ENABLE		BIT(0)
30 
31 #define IVSHMEM_PCIE_REG_BAR_IDX	0
32 #define IVSHMEM_PCIE_MSI_X_BAR_IDX	1
33 #define IVSHMEM_PCIE_SHMEM_BAR_IDX	2
34 
35 #define PCIE_INTX_PIN_MIN		1
36 #define PCIE_INTX_PIN_MAX		4
37 
38 #define INTX_IRQ_UNUSED			UINT32_MAX
39 
40 struct ivshmem_param {
41 	const struct device *dev;
42 	struct k_poll_signal *signal;
43 	uint8_t vector;
44 };
45 
46 struct ivshmem {
47 	DEVICE_MMIO_RAM;
48 	struct pcie_dev *pcie;
49 	uintptr_t shmem;
50 	size_t size;
51 #ifdef CONFIG_IVSHMEM_DOORBELL
52 	msi_vector_t vectors[CONFIG_IVSHMEM_MSI_X_VECTORS];
53 	struct ivshmem_param params[CONFIG_IVSHMEM_MSI_X_VECTORS];
54 	uint16_t n_vectors;
55 #endif
56 #ifdef CONFIG_IVSHMEM_V2
57 	bool ivshmem_v2;
58 	uint32_t max_peers;
59 	size_t rw_section_size;
60 	size_t output_section_size;
61 	uintptr_t state_table_shmem;
62 	uintptr_t rw_section_shmem;
63 	uintptr_t output_section_shmem[CONFIG_IVSHMEM_V2_MAX_PEERS];
64 #endif
65 };
66 
67 struct ivshmem_reg {
68 	uint32_t int_mask;
69 	uint32_t int_status;
70 	uint32_t iv_position;
71 	uint32_t doorbell;
72 };
73 
74 #ifdef CONFIG_IVSHMEM_V2
75 
76 struct ivshmem_v2_reg {
77 	uint32_t id;
78 	uint32_t max_peers;
79 	uint32_t int_control;
80 	uint32_t doorbell;
81 	uint32_t state;
82 };
83 
84 struct ivshmem_cfg {
85 	struct intx_info {
86 		uint32_t irq;
87 		uint32_t priority;
88 		uint32_t flags;
89 	} intx_info[PCIE_INTX_PIN_MAX];
90 };
91 
92 #endif /* CONFIG_IVSHMEM_V2 */
93 
94 #define IVSHMEM_GEN_DOORBELL(i, v) ((i << 16) | (v & 0xFFFF))
95 
96 #endif /* ZEPHYR_DRIVERS_VIRTUALIZATION_VIRT_IVSHMEM_H_ */
97