1 /* SPDX-License-Identifier: GPL-2.0-only
2  *
3  * Copyright (C) 2020-21 Intel Corporation.
4  */
5 
6 #ifndef IOSM_IPC_PCIE_H
7 #define IOSM_IPC_PCIE_H
8 
9 #include <linux/device.h>
10 #include <linux/pci.h>
11 #include <linux/skbuff.h>
12 
13 #include "iosm_ipc_irq.h"
14 
15 /* Device ID */
16 #define INTEL_CP_DEVICE_7560_ID 0x7560
17 
18 /* Define for BAR area usage */
19 #define IPC_DOORBELL_BAR0 0
20 #define IPC_SCRATCHPAD_BAR2 2
21 
22 /* Defines for DOORBELL registers information */
23 #define IPC_DOORBELL_CH_OFFSET BIT(5)
24 #define IPC_WRITE_PTR_REG_0 BIT(4)
25 #define IPC_CAPTURE_PTR_REG_0 BIT(3)
26 
27 /* Number of MSI used for IPC */
28 #define IPC_MSI_VECTORS 1
29 
30 /* Total number of Maximum IPC IRQ vectors used for IPC */
31 #define IPC_IRQ_VECTORS IPC_MSI_VECTORS
32 
33 /**
34  * enum ipc_pcie_sleep_state - Enum type to different sleep state transitions
35  * @IPC_PCIE_D0L12:	Put the sleep state in D0L12
36  * @IPC_PCIE_D3L2:	Put the sleep state in D3L2
37  */
38 enum ipc_pcie_sleep_state {
39 	IPC_PCIE_D0L12,
40 	IPC_PCIE_D3L2,
41 };
42 
43 /**
44  * struct iosm_pcie - IPC_PCIE struct.
45  * @pci:			Address of the device description
46  * @dev:			Pointer to generic device structure
47  * @ipc_regs:			Remapped CP doorbell address of the irq register
48  *				set, to fire the doorbell irq.
49  * @scratchpad:			Remapped CP scratchpad address, to send the
50  *				configuration. tuple and the IPC descriptors
51  *				to CP in the ROM phase. The config tuple
52  *				information are saved on the MSI scratchpad.
53  * @imem:			Pointer to imem data struct
54  * @ipc_regs_bar_nr:		BAR number to be used for IPC doorbell
55  * @scratchpad_bar_nr:		BAR number to be used for Scratchpad
56  * @nvec:			number of requested irq vectors
57  * @doorbell_reg_offset:	doorbell_reg_offset
58  * @doorbell_write:		doorbell write register
59  * @doorbell_capture:		doorbell capture resgister
60  * @suspend:			S2IDLE sleep/active
61  * @d3l2_support:		Read WWAN RTD3 BIOS setting for D3L2 support
62  */
63 struct iosm_pcie {
64 	struct pci_dev *pci;
65 	struct device *dev;
66 	void __iomem *ipc_regs;
67 	void __iomem *scratchpad;
68 	struct iosm_imem *imem;
69 	int ipc_regs_bar_nr;
70 	int scratchpad_bar_nr;
71 	int nvec;
72 	u32 doorbell_reg_offset;
73 	u32 doorbell_write;
74 	u32 doorbell_capture;
75 	unsigned long suspend;
76 	enum ipc_pcie_sleep_state d3l2_support;
77 };
78 
79 /**
80  * struct ipc_skb_cb - Struct definition of the socket buffer which is mapped to
81  *		       the cb field of sbk
82  * @mapping:	Store physical or IOVA mapped address of skb virtual add.
83  * @direction:	DMA direction
84  * @len:	Length of the DMA mapped region
85  * @op_type:    Expected values are defined about enum ipc_ul_usr_op.
86  */
87 struct ipc_skb_cb {
88 	dma_addr_t mapping;
89 	int direction;
90 	int len;
91 	u8 op_type;
92 };
93 
94 /**
95  * enum ipc_ul_usr_op - Control operation to execute the right action on
96  *			the user interface.
97  * @UL_USR_OP_BLOCKED:	The uplink app was blocked until CP confirms that the
98  *			uplink buffer was consumed triggered by the IRQ.
99  * @UL_MUX_OP_ADB:	In MUX mode the UL ADB shall be addedd to the free list.
100  * @UL_DEFAULT:		SKB in non muxing mode
101  */
102 enum ipc_ul_usr_op {
103 	UL_USR_OP_BLOCKED,
104 	UL_MUX_OP_ADB,
105 	UL_DEFAULT,
106 };
107 
108 /**
109  * ipc_pcie_addr_map - Maps the kernel's virtual address to either IOVA
110  *		       address space or Physical address space, the mapping is
111  *		       stored in the skb's cb.
112  * @ipc_pcie:	Pointer to struct iosm_pcie
113  * @data:	Skb mem containing data
114  * @size:	Data size
115  * @mapping:	Dma mapping address
116  * @direction:	Data direction
117  *
118  * Returns: 0 on success and failure value on error
119  */
120 int ipc_pcie_addr_map(struct iosm_pcie *ipc_pcie, unsigned char *data,
121 		      size_t size, dma_addr_t *mapping, int direction);
122 
123 /**
124  * ipc_pcie_addr_unmap - Unmaps the skb memory region from IOVA address space
125  * @ipc_pcie:	Pointer to struct iosm_pcie
126  * @size:	Data size
127  * @mapping:	Dma mapping address
128  * @direction:	Data direction
129  */
130 void ipc_pcie_addr_unmap(struct iosm_pcie *ipc_pcie, size_t size,
131 			 dma_addr_t mapping, int direction);
132 
133 /**
134  * ipc_pcie_alloc_skb - Allocate an uplink SKB for the given size.
135  * @ipc_pcie:	Pointer to struct iosm_pcie
136  * @size:	Size of the SKB required.
137  * @flags:	Allocation flags
138  * @mapping:	Copies either mapped IOVA add. or converted Phy address
139  * @direction:	DMA data direction
140  * @headroom:	Header data offset
141  *
142  * Returns: Pointer to ipc_skb on Success, NULL on failure.
143  */
144 struct sk_buff *ipc_pcie_alloc_skb(struct iosm_pcie *ipc_pcie, size_t size,
145 				   gfp_t flags, dma_addr_t *mapping,
146 				   int direction, size_t headroom);
147 
148 /**
149  * ipc_pcie_alloc_local_skb - Allocate a local SKB for the given size.
150  * @ipc_pcie:	Pointer to struct iosm_pcie
151  * @flags:	Allocation flags
152  * @size:	Size of the SKB required.
153  *
154  * Returns: Pointer to ipc_skb on Success, NULL on failure.
155  */
156 struct sk_buff *ipc_pcie_alloc_local_skb(struct iosm_pcie *ipc_pcie,
157 					 gfp_t flags, size_t size);
158 
159 /**
160  * ipc_pcie_kfree_skb - Free skb allocated by ipc_pcie_alloc_*_skb().
161  * @ipc_pcie:	Pointer to struct iosm_pcie
162  * @skb:	Pointer to the skb
163  */
164 void ipc_pcie_kfree_skb(struct iosm_pcie *ipc_pcie, struct sk_buff *skb);
165 
166 /**
167  * ipc_pcie_check_data_link_active - Check Data Link Layer Active
168  * @ipc_pcie:	Pointer to struct iosm_pcie
169  *
170  * Returns: true if active, otherwise false
171  */
172 bool ipc_pcie_check_data_link_active(struct iosm_pcie *ipc_pcie);
173 
174 /**
175  * ipc_pcie_suspend - Callback invoked by pm_runtime_suspend. It decrements
176  *		     the device's usage count then, carry out a suspend,
177  *		     either synchronous or asynchronous.
178  * @ipc_pcie:	Pointer to struct iosm_pcie
179  *
180  * Returns: 0 on success and failure value on error
181  */
182 int ipc_pcie_suspend(struct iosm_pcie *ipc_pcie);
183 
184 /**
185  * ipc_pcie_resume - Callback invoked by pm_runtime_resume. It increments
186  *		    the device's usage count then, carry out a resume,
187  *		    either synchronous or asynchronous.
188  * @ipc_pcie:	Pointer to struct iosm_pcie
189  *
190  * Returns: 0 on success and failure value on error
191  */
192 int ipc_pcie_resume(struct iosm_pcie *ipc_pcie);
193 
194 /**
195  * ipc_pcie_check_aspm_enabled - Check if ASPM L1 is already enabled
196  * @ipc_pcie:			 Pointer to struct iosm_pcie
197  * @parent:			 True if checking ASPM L1 for parent else false
198  *
199  * Returns: true if ASPM is already enabled else false
200  */
201 bool ipc_pcie_check_aspm_enabled(struct iosm_pcie *ipc_pcie,
202 				 bool parent);
203 /**
204  * ipc_pcie_config_aspm - Configure ASPM L1
205  * @ipc_pcie:	Pointer to struct iosm_pcie
206  */
207 void ipc_pcie_config_aspm(struct iosm_pcie *ipc_pcie);
208 
209 #endif
210