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