1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2013-2021 Intel Corporation
4  */
5 
6 #include "i915_drv.h"
7 #include "i915_iosf_mbi.h"
8 #include "i915_reg.h"
9 #include "vlv_sideband.h"
10 
11 #include "display/intel_dpio_phy.h"
12 
13 /*
14  * IOSF sideband, see VLV2_SidebandMsg_HAS.docx and
15  * VLV_VLV2_PUNIT_HAS_0.8.docx
16  */
17 
18 /* Standard MMIO read, non-posted */
19 #define SB_MRD_NP	0x00
20 /* Standard MMIO write, non-posted */
21 #define SB_MWR_NP	0x01
22 /* Private register read, double-word addressing, non-posted */
23 #define SB_CRRDDA_NP	0x06
24 /* Private register write, double-word addressing, non-posted */
25 #define SB_CRWRDA_NP	0x07
26 
ping(void * info)27 static void ping(void *info)
28 {
29 }
30 
__vlv_punit_get(struct drm_i915_private * i915)31 static void __vlv_punit_get(struct drm_i915_private *i915)
32 {
33 	iosf_mbi_punit_acquire();
34 
35 	/*
36 	 * Prevent the cpu from sleeping while we use this sideband, otherwise
37 	 * the punit may cause a machine hang. The issue appears to be isolated
38 	 * with changing the power state of the CPU package while changing
39 	 * the power state via the punit, and we have only observed it
40 	 * reliably on 4-core Baytail systems suggesting the issue is in the
41 	 * power delivery mechanism and likely to be board/function
42 	 * specific. Hence we presume the workaround needs only be applied
43 	 * to the Valleyview P-unit and not all sideband communications.
44 	 */
45 	if (IS_VALLEYVIEW(i915)) {
46 		cpu_latency_qos_update_request(&i915->sb_qos, 0);
47 #ifdef notyet
48 		on_each_cpu(ping, NULL, 1);
49 #endif
50 	}
51 }
52 
__vlv_punit_put(struct drm_i915_private * i915)53 static void __vlv_punit_put(struct drm_i915_private *i915)
54 {
55 	if (IS_VALLEYVIEW(i915))
56 		cpu_latency_qos_update_request(&i915->sb_qos,
57 					       PM_QOS_DEFAULT_VALUE);
58 
59 	iosf_mbi_punit_release();
60 }
61 
vlv_iosf_sb_get(struct drm_i915_private * i915,unsigned long ports)62 void vlv_iosf_sb_get(struct drm_i915_private *i915, unsigned long ports)
63 {
64 	if (ports & BIT(VLV_IOSF_SB_PUNIT))
65 		__vlv_punit_get(i915);
66 
67 	mutex_lock(&i915->sb_lock);
68 }
69 
vlv_iosf_sb_put(struct drm_i915_private * i915,unsigned long ports)70 void vlv_iosf_sb_put(struct drm_i915_private *i915, unsigned long ports)
71 {
72 	mutex_unlock(&i915->sb_lock);
73 
74 	if (ports & BIT(VLV_IOSF_SB_PUNIT))
75 		__vlv_punit_put(i915);
76 }
77 
vlv_sideband_rw(struct drm_i915_private * i915,u32 devfn,u32 port,u32 opcode,u32 addr,u32 * val)78 static int vlv_sideband_rw(struct drm_i915_private *i915,
79 			   u32 devfn, u32 port, u32 opcode,
80 			   u32 addr, u32 *val)
81 {
82 	struct intel_uncore *uncore = &i915->uncore;
83 	const bool is_read = (opcode == SB_MRD_NP || opcode == SB_CRRDDA_NP);
84 	int err;
85 
86 	lockdep_assert_held(&i915->sb_lock);
87 	if (port == IOSF_PORT_PUNIT)
88 		iosf_mbi_assert_punit_acquired();
89 
90 	/* Flush the previous comms, just in case it failed last time. */
91 	if (intel_wait_for_register(uncore,
92 				    VLV_IOSF_DOORBELL_REQ, IOSF_SB_BUSY, 0,
93 				    5)) {
94 		drm_dbg(&i915->drm, "IOSF sideband idle wait (%s) timed out\n",
95 			is_read ? "read" : "write");
96 		return -EAGAIN;
97 	}
98 
99 	preempt_disable();
100 
101 	intel_uncore_write_fw(uncore, VLV_IOSF_ADDR, addr);
102 	intel_uncore_write_fw(uncore, VLV_IOSF_DATA, is_read ? 0 : *val);
103 	intel_uncore_write_fw(uncore, VLV_IOSF_DOORBELL_REQ,
104 			      (devfn << IOSF_DEVFN_SHIFT) |
105 			      (opcode << IOSF_OPCODE_SHIFT) |
106 			      (port << IOSF_PORT_SHIFT) |
107 			      (0xf << IOSF_BYTE_ENABLES_SHIFT) |
108 			      (0 << IOSF_BAR_SHIFT) |
109 			      IOSF_SB_BUSY);
110 
111 	if (__intel_wait_for_register_fw(uncore,
112 					 VLV_IOSF_DOORBELL_REQ, IOSF_SB_BUSY, 0,
113 					 10000, 0, NULL) == 0) {
114 		if (is_read)
115 			*val = intel_uncore_read_fw(uncore, VLV_IOSF_DATA);
116 		err = 0;
117 	} else {
118 		drm_dbg(&i915->drm, "IOSF sideband finish wait (%s) timed out\n",
119 			is_read ? "read" : "write");
120 		err = -ETIMEDOUT;
121 	}
122 
123 	preempt_enable();
124 
125 	return err;
126 }
127 
vlv_punit_read(struct drm_i915_private * i915,u32 addr)128 u32 vlv_punit_read(struct drm_i915_private *i915, u32 addr)
129 {
130 	u32 val = 0;
131 
132 	vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_PUNIT,
133 			SB_CRRDDA_NP, addr, &val);
134 
135 	return val;
136 }
137 
vlv_punit_write(struct drm_i915_private * i915,u32 addr,u32 val)138 int vlv_punit_write(struct drm_i915_private *i915, u32 addr, u32 val)
139 {
140 	return vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_PUNIT,
141 			       SB_CRWRDA_NP, addr, &val);
142 }
143 
vlv_bunit_read(struct drm_i915_private * i915,u32 reg)144 u32 vlv_bunit_read(struct drm_i915_private *i915, u32 reg)
145 {
146 	u32 val = 0;
147 
148 	vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_BUNIT,
149 			SB_CRRDDA_NP, reg, &val);
150 
151 	return val;
152 }
153 
vlv_bunit_write(struct drm_i915_private * i915,u32 reg,u32 val)154 void vlv_bunit_write(struct drm_i915_private *i915, u32 reg, u32 val)
155 {
156 	vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_BUNIT,
157 			SB_CRWRDA_NP, reg, &val);
158 }
159 
vlv_nc_read(struct drm_i915_private * i915,u8 addr)160 u32 vlv_nc_read(struct drm_i915_private *i915, u8 addr)
161 {
162 	u32 val = 0;
163 
164 	vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_NC,
165 			SB_CRRDDA_NP, addr, &val);
166 
167 	return val;
168 }
169 
vlv_cck_read(struct drm_i915_private * i915,u32 reg)170 u32 vlv_cck_read(struct drm_i915_private *i915, u32 reg)
171 {
172 	u32 val = 0;
173 
174 	vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_CCK,
175 			SB_CRRDDA_NP, reg, &val);
176 
177 	return val;
178 }
179 
vlv_cck_write(struct drm_i915_private * i915,u32 reg,u32 val)180 void vlv_cck_write(struct drm_i915_private *i915, u32 reg, u32 val)
181 {
182 	vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_CCK,
183 			SB_CRWRDA_NP, reg, &val);
184 }
185 
vlv_ccu_read(struct drm_i915_private * i915,u32 reg)186 u32 vlv_ccu_read(struct drm_i915_private *i915, u32 reg)
187 {
188 	u32 val = 0;
189 
190 	vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_CCU,
191 			SB_CRRDDA_NP, reg, &val);
192 
193 	return val;
194 }
195 
vlv_ccu_write(struct drm_i915_private * i915,u32 reg,u32 val)196 void vlv_ccu_write(struct drm_i915_private *i915, u32 reg, u32 val)
197 {
198 	vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_CCU,
199 			SB_CRWRDA_NP, reg, &val);
200 }
201 
vlv_dpio_phy_iosf_port(struct drm_i915_private * i915,enum dpio_phy phy)202 static u32 vlv_dpio_phy_iosf_port(struct drm_i915_private *i915, enum dpio_phy phy)
203 {
204 	/*
205 	 * IOSF_PORT_DPIO: VLV x2 PHY (DP/HDMI B and C), CHV x1 PHY (DP/HDMI D)
206 	 * IOSF_PORT_DPIO_2: CHV x2 PHY (DP/HDMI B and C)
207 	 */
208 	if (IS_CHERRYVIEW(i915))
209 		return phy == DPIO_PHY0 ? IOSF_PORT_DPIO_2 : IOSF_PORT_DPIO;
210 	else
211 		return IOSF_PORT_DPIO;
212 }
213 
vlv_dpio_read(struct drm_i915_private * i915,enum dpio_phy phy,int reg)214 u32 vlv_dpio_read(struct drm_i915_private *i915, enum dpio_phy phy, int reg)
215 {
216 	u32 port = vlv_dpio_phy_iosf_port(i915, phy);
217 	u32 val = 0;
218 
219 	vlv_sideband_rw(i915, DPIO_DEVFN, port, SB_MRD_NP, reg, &val);
220 
221 	/*
222 	 * FIXME: There might be some registers where all 1's is a valid value,
223 	 * so ideally we should check the register offset instead...
224 	 */
225 	drm_WARN(&i915->drm, val == 0xffffffff,
226 		 "DPIO PHY%d read reg 0x%x == 0x%x\n",
227 		 phy, reg, val);
228 
229 	return val;
230 }
231 
vlv_dpio_write(struct drm_i915_private * i915,enum dpio_phy phy,int reg,u32 val)232 void vlv_dpio_write(struct drm_i915_private *i915,
233 		    enum dpio_phy phy, int reg, u32 val)
234 {
235 	u32 port = vlv_dpio_phy_iosf_port(i915, phy);
236 
237 	vlv_sideband_rw(i915, DPIO_DEVFN, port, SB_MWR_NP, reg, &val);
238 }
239 
vlv_flisdsi_read(struct drm_i915_private * i915,u32 reg)240 u32 vlv_flisdsi_read(struct drm_i915_private *i915, u32 reg)
241 {
242 	u32 val = 0;
243 
244 	vlv_sideband_rw(i915, DPIO_DEVFN, IOSF_PORT_FLISDSI, SB_CRRDDA_NP,
245 			reg, &val);
246 	return val;
247 }
248 
vlv_flisdsi_write(struct drm_i915_private * i915,u32 reg,u32 val)249 void vlv_flisdsi_write(struct drm_i915_private *i915, u32 reg, u32 val)
250 {
251 	vlv_sideband_rw(i915, DPIO_DEVFN, IOSF_PORT_FLISDSI, SB_CRWRDA_NP,
252 			reg, &val);
253 }
254