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 rw_section_offset;
61 	size_t output_section_size;
62 	size_t output_section_offset;
63 #endif
64 };
65 
66 struct ivshmem_reg {
67 	uint32_t int_mask;
68 	uint32_t int_status;
69 	uint32_t iv_position;
70 	uint32_t doorbell;
71 };
72 
73 #ifdef CONFIG_IVSHMEM_V2
74 
75 struct ivshmem_v2_reg {
76 	uint32_t id;
77 	uint32_t max_peers;
78 	uint32_t int_control;
79 	uint32_t doorbell;
80 	uint32_t state;
81 };
82 
83 struct ivshmem_cfg {
84 	struct intx_info {
85 		uint32_t irq;
86 		uint32_t priority;
87 		uint32_t flags;
88 	} intx_info[PCIE_INTX_PIN_MAX];
89 };
90 
91 #endif /* CONFIG_IVSHMEM_V2 */
92 
93 #define IVSHMEM_GEN_DOORBELL(i, v) ((i << 16) | (v & 0xFFFF))
94 
95 #endif /* ZEPHYR_DRIVERS_VIRTUALIZATION_VIRT_IVSHMEM_H_ */
96