Merge branch 'core-objtool-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / drivers / gpu / drm / i915 / intel_sideband.c
1 /*
2  * Copyright © 2013 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  */
24
25 #include <asm/iosf_mbi.h>
26
27 #include "i915_drv.h"
28 #include "intel_sideband.h"
29
30 /*
31  * IOSF sideband, see VLV2_SidebandMsg_HAS.docx and
32  * VLV_VLV2_PUNIT_HAS_0.8.docx
33  */
34
35 /* Standard MMIO read, non-posted */
36 #define SB_MRD_NP       0x00
37 /* Standard MMIO write, non-posted */
38 #define SB_MWR_NP       0x01
39 /* Private register read, double-word addressing, non-posted */
40 #define SB_CRRDDA_NP    0x06
41 /* Private register write, double-word addressing, non-posted */
42 #define SB_CRWRDA_NP    0x07
43
44 static void ping(void *info)
45 {
46 }
47
48 static void __vlv_punit_get(struct drm_i915_private *i915)
49 {
50         iosf_mbi_punit_acquire();
51
52         /*
53          * Prevent the cpu from sleeping while we use this sideband, otherwise
54          * the punit may cause a machine hang. The issue appears to be isolated
55          * with changing the power state of the CPU package while changing
56          * the power state via the punit, and we have only observed it
57          * reliably on 4-core Baytail systems suggesting the issue is in the
58          * power delivery mechanism and likely to be be board/function
59          * specific. Hence we presume the workaround needs only be applied
60          * to the Valleyview P-unit and not all sideband communications.
61          */
62         if (IS_VALLEYVIEW(i915)) {
63                 cpu_latency_qos_update_request(&i915->sb_qos, 0);
64                 on_each_cpu(ping, NULL, 1);
65         }
66 }
67
68 static void __vlv_punit_put(struct drm_i915_private *i915)
69 {
70         if (IS_VALLEYVIEW(i915))
71                 cpu_latency_qos_update_request(&i915->sb_qos,
72                                                PM_QOS_DEFAULT_VALUE);
73
74         iosf_mbi_punit_release();
75 }
76
77 void vlv_iosf_sb_get(struct drm_i915_private *i915, unsigned long ports)
78 {
79         if (ports & BIT(VLV_IOSF_SB_PUNIT))
80                 __vlv_punit_get(i915);
81
82         mutex_lock(&i915->sb_lock);
83 }
84
85 void vlv_iosf_sb_put(struct drm_i915_private *i915, unsigned long ports)
86 {
87         mutex_unlock(&i915->sb_lock);
88
89         if (ports & BIT(VLV_IOSF_SB_PUNIT))
90                 __vlv_punit_put(i915);
91 }
92
93 static int vlv_sideband_rw(struct drm_i915_private *i915,
94                            u32 devfn, u32 port, u32 opcode,
95                            u32 addr, u32 *val)
96 {
97         struct intel_uncore *uncore = &i915->uncore;
98         const bool is_read = (opcode == SB_MRD_NP || opcode == SB_CRRDDA_NP);
99         int err;
100
101         lockdep_assert_held(&i915->sb_lock);
102         if (port == IOSF_PORT_PUNIT)
103                 iosf_mbi_assert_punit_acquired();
104
105         /* Flush the previous comms, just in case it failed last time. */
106         if (intel_wait_for_register(uncore,
107                                     VLV_IOSF_DOORBELL_REQ, IOSF_SB_BUSY, 0,
108                                     5)) {
109                 drm_dbg(&i915->drm, "IOSF sideband idle wait (%s) timed out\n",
110                         is_read ? "read" : "write");
111                 return -EAGAIN;
112         }
113
114         preempt_disable();
115
116         intel_uncore_write_fw(uncore, VLV_IOSF_ADDR, addr);
117         intel_uncore_write_fw(uncore, VLV_IOSF_DATA, is_read ? 0 : *val);
118         intel_uncore_write_fw(uncore, VLV_IOSF_DOORBELL_REQ,
119                               (devfn << IOSF_DEVFN_SHIFT) |
120                               (opcode << IOSF_OPCODE_SHIFT) |
121                               (port << IOSF_PORT_SHIFT) |
122                               (0xf << IOSF_BYTE_ENABLES_SHIFT) |
123                               (0 << IOSF_BAR_SHIFT) |
124                               IOSF_SB_BUSY);
125
126         if (__intel_wait_for_register_fw(uncore,
127                                          VLV_IOSF_DOORBELL_REQ, IOSF_SB_BUSY, 0,
128                                          10000, 0, NULL) == 0) {
129                 if (is_read)
130                         *val = intel_uncore_read_fw(uncore, VLV_IOSF_DATA);
131                 err = 0;
132         } else {
133                 drm_dbg(&i915->drm, "IOSF sideband finish wait (%s) timed out\n",
134                         is_read ? "read" : "write");
135                 err = -ETIMEDOUT;
136         }
137
138         preempt_enable();
139
140         return err;
141 }
142
143 u32 vlv_punit_read(struct drm_i915_private *i915, u32 addr)
144 {
145         u32 val = 0;
146
147         vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_PUNIT,
148                         SB_CRRDDA_NP, addr, &val);
149
150         return val;
151 }
152
153 int vlv_punit_write(struct drm_i915_private *i915, u32 addr, u32 val)
154 {
155         return vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_PUNIT,
156                                SB_CRWRDA_NP, addr, &val);
157 }
158
159 u32 vlv_bunit_read(struct drm_i915_private *i915, u32 reg)
160 {
161         u32 val = 0;
162
163         vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_BUNIT,
164                         SB_CRRDDA_NP, reg, &val);
165
166         return val;
167 }
168
169 void vlv_bunit_write(struct drm_i915_private *i915, u32 reg, u32 val)
170 {
171         vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_BUNIT,
172                         SB_CRWRDA_NP, reg, &val);
173 }
174
175 u32 vlv_nc_read(struct drm_i915_private *i915, u8 addr)
176 {
177         u32 val = 0;
178
179         vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_NC,
180                         SB_CRRDDA_NP, addr, &val);
181
182         return val;
183 }
184
185 u32 vlv_iosf_sb_read(struct drm_i915_private *i915, u8 port, u32 reg)
186 {
187         u32 val = 0;
188
189         vlv_sideband_rw(i915, PCI_DEVFN(0, 0), port,
190                         SB_CRRDDA_NP, reg, &val);
191
192         return val;
193 }
194
195 void vlv_iosf_sb_write(struct drm_i915_private *i915,
196                        u8 port, u32 reg, u32 val)
197 {
198         vlv_sideband_rw(i915, PCI_DEVFN(0, 0), port,
199                         SB_CRWRDA_NP, reg, &val);
200 }
201
202 u32 vlv_cck_read(struct drm_i915_private *i915, u32 reg)
203 {
204         u32 val = 0;
205
206         vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_CCK,
207                         SB_CRRDDA_NP, reg, &val);
208
209         return val;
210 }
211
212 void vlv_cck_write(struct drm_i915_private *i915, u32 reg, u32 val)
213 {
214         vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_CCK,
215                         SB_CRWRDA_NP, reg, &val);
216 }
217
218 u32 vlv_ccu_read(struct drm_i915_private *i915, u32 reg)
219 {
220         u32 val = 0;
221
222         vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_CCU,
223                         SB_CRRDDA_NP, reg, &val);
224
225         return val;
226 }
227
228 void vlv_ccu_write(struct drm_i915_private *i915, u32 reg, u32 val)
229 {
230         vlv_sideband_rw(i915, PCI_DEVFN(0, 0), IOSF_PORT_CCU,
231                         SB_CRWRDA_NP, reg, &val);
232 }
233
234 u32 vlv_dpio_read(struct drm_i915_private *i915, enum pipe pipe, int reg)
235 {
236         int port = i915->dpio_phy_iosf_port[DPIO_PHY(pipe)];
237         u32 val = 0;
238
239         vlv_sideband_rw(i915, DPIO_DEVFN, port, SB_MRD_NP, reg, &val);
240
241         /*
242          * FIXME: There might be some registers where all 1's is a valid value,
243          * so ideally we should check the register offset instead...
244          */
245         WARN(val == 0xffffffff, "DPIO read pipe %c reg 0x%x == 0x%x\n",
246              pipe_name(pipe), reg, val);
247
248         return val;
249 }
250
251 void vlv_dpio_write(struct drm_i915_private *i915,
252                     enum pipe pipe, int reg, u32 val)
253 {
254         int port = i915->dpio_phy_iosf_port[DPIO_PHY(pipe)];
255
256         vlv_sideband_rw(i915, DPIO_DEVFN, port, SB_MWR_NP, reg, &val);
257 }
258
259 u32 vlv_flisdsi_read(struct drm_i915_private *i915, u32 reg)
260 {
261         u32 val = 0;
262
263         vlv_sideband_rw(i915, DPIO_DEVFN, IOSF_PORT_FLISDSI, SB_CRRDDA_NP,
264                         reg, &val);
265         return val;
266 }
267
268 void vlv_flisdsi_write(struct drm_i915_private *i915, u32 reg, u32 val)
269 {
270         vlv_sideband_rw(i915, DPIO_DEVFN, IOSF_PORT_FLISDSI, SB_CRWRDA_NP,
271                         reg, &val);
272 }
273
274 /* SBI access */
275 static int intel_sbi_rw(struct drm_i915_private *i915, u16 reg,
276                         enum intel_sbi_destination destination,
277                         u32 *val, bool is_read)
278 {
279         struct intel_uncore *uncore = &i915->uncore;
280         u32 cmd;
281
282         lockdep_assert_held(&i915->sb_lock);
283
284         if (intel_wait_for_register_fw(uncore,
285                                        SBI_CTL_STAT, SBI_BUSY, 0,
286                                        100)) {
287                 drm_err(&i915->drm,
288                         "timeout waiting for SBI to become ready\n");
289                 return -EBUSY;
290         }
291
292         intel_uncore_write_fw(uncore, SBI_ADDR, (u32)reg << 16);
293         intel_uncore_write_fw(uncore, SBI_DATA, is_read ? 0 : *val);
294
295         if (destination == SBI_ICLK)
296                 cmd = SBI_CTL_DEST_ICLK | SBI_CTL_OP_CRRD;
297         else
298                 cmd = SBI_CTL_DEST_MPHY | SBI_CTL_OP_IORD;
299         if (!is_read)
300                 cmd |= BIT(8);
301         intel_uncore_write_fw(uncore, SBI_CTL_STAT, cmd | SBI_BUSY);
302
303         if (__intel_wait_for_register_fw(uncore,
304                                          SBI_CTL_STAT, SBI_BUSY, 0,
305                                          100, 100, &cmd)) {
306                 drm_err(&i915->drm,
307                         "timeout waiting for SBI to complete read\n");
308                 return -ETIMEDOUT;
309         }
310
311         if (cmd & SBI_RESPONSE_FAIL) {
312                 drm_err(&i915->drm, "error during SBI read of reg %x\n", reg);
313                 return -ENXIO;
314         }
315
316         if (is_read)
317                 *val = intel_uncore_read_fw(uncore, SBI_DATA);
318
319         return 0;
320 }
321
322 u32 intel_sbi_read(struct drm_i915_private *i915, u16 reg,
323                    enum intel_sbi_destination destination)
324 {
325         u32 result = 0;
326
327         intel_sbi_rw(i915, reg, destination, &result, true);
328
329         return result;
330 }
331
332 void intel_sbi_write(struct drm_i915_private *i915, u16 reg, u32 value,
333                      enum intel_sbi_destination destination)
334 {
335         intel_sbi_rw(i915, reg, destination, &value, false);
336 }
337
338 static inline int gen6_check_mailbox_status(u32 mbox)
339 {
340         switch (mbox & GEN6_PCODE_ERROR_MASK) {
341         case GEN6_PCODE_SUCCESS:
342                 return 0;
343         case GEN6_PCODE_UNIMPLEMENTED_CMD:
344                 return -ENODEV;
345         case GEN6_PCODE_ILLEGAL_CMD:
346                 return -ENXIO;
347         case GEN6_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
348         case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
349                 return -EOVERFLOW;
350         case GEN6_PCODE_TIMEOUT:
351                 return -ETIMEDOUT;
352         default:
353                 MISSING_CASE(mbox & GEN6_PCODE_ERROR_MASK);
354                 return 0;
355         }
356 }
357
358 static inline int gen7_check_mailbox_status(u32 mbox)
359 {
360         switch (mbox & GEN6_PCODE_ERROR_MASK) {
361         case GEN6_PCODE_SUCCESS:
362                 return 0;
363         case GEN6_PCODE_ILLEGAL_CMD:
364                 return -ENXIO;
365         case GEN7_PCODE_TIMEOUT:
366                 return -ETIMEDOUT;
367         case GEN7_PCODE_ILLEGAL_DATA:
368                 return -EINVAL;
369         case GEN7_PCODE_MIN_FREQ_TABLE_GT_RATIO_OUT_OF_RANGE:
370                 return -EOVERFLOW;
371         default:
372                 MISSING_CASE(mbox & GEN6_PCODE_ERROR_MASK);
373                 return 0;
374         }
375 }
376
377 static int __sandybridge_pcode_rw(struct drm_i915_private *i915,
378                                   u32 mbox, u32 *val, u32 *val1,
379                                   int fast_timeout_us,
380                                   int slow_timeout_ms,
381                                   bool is_read)
382 {
383         struct intel_uncore *uncore = &i915->uncore;
384
385         lockdep_assert_held(&i915->sb_lock);
386
387         /*
388          * GEN6_PCODE_* are outside of the forcewake domain, we can
389          * use te fw I915_READ variants to reduce the amount of work
390          * required when reading/writing.
391          */
392
393         if (intel_uncore_read_fw(uncore, GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY)
394                 return -EAGAIN;
395
396         intel_uncore_write_fw(uncore, GEN6_PCODE_DATA, *val);
397         intel_uncore_write_fw(uncore, GEN6_PCODE_DATA1, val1 ? *val1 : 0);
398         intel_uncore_write_fw(uncore,
399                               GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
400
401         if (__intel_wait_for_register_fw(uncore,
402                                          GEN6_PCODE_MAILBOX,
403                                          GEN6_PCODE_READY, 0,
404                                          fast_timeout_us,
405                                          slow_timeout_ms,
406                                          &mbox))
407                 return -ETIMEDOUT;
408
409         if (is_read)
410                 *val = intel_uncore_read_fw(uncore, GEN6_PCODE_DATA);
411         if (is_read && val1)
412                 *val1 = intel_uncore_read_fw(uncore, GEN6_PCODE_DATA1);
413
414         if (INTEL_GEN(i915) > 6)
415                 return gen7_check_mailbox_status(mbox);
416         else
417                 return gen6_check_mailbox_status(mbox);
418 }
419
420 int sandybridge_pcode_read(struct drm_i915_private *i915, u32 mbox,
421                            u32 *val, u32 *val1)
422 {
423         int err;
424
425         mutex_lock(&i915->sb_lock);
426         err = __sandybridge_pcode_rw(i915, mbox, val, val1,
427                                      500, 0,
428                                      true);
429         mutex_unlock(&i915->sb_lock);
430
431         if (err) {
432                 drm_dbg(&i915->drm,
433                         "warning: pcode (read from mbox %x) mailbox access failed for %ps: %d\n",
434                         mbox, __builtin_return_address(0), err);
435         }
436
437         return err;
438 }
439
440 int sandybridge_pcode_write_timeout(struct drm_i915_private *i915,
441                                     u32 mbox, u32 val,
442                                     int fast_timeout_us,
443                                     int slow_timeout_ms)
444 {
445         int err;
446
447         mutex_lock(&i915->sb_lock);
448         err = __sandybridge_pcode_rw(i915, mbox, &val, NULL,
449                                      fast_timeout_us, slow_timeout_ms,
450                                      false);
451         mutex_unlock(&i915->sb_lock);
452
453         if (err) {
454                 drm_dbg(&i915->drm,
455                         "warning: pcode (write of 0x%08x to mbox %x) mailbox access failed for %ps: %d\n",
456                         val, mbox, __builtin_return_address(0), err);
457         }
458
459         return err;
460 }
461
462 static bool skl_pcode_try_request(struct drm_i915_private *i915, u32 mbox,
463                                   u32 request, u32 reply_mask, u32 reply,
464                                   u32 *status)
465 {
466         *status = __sandybridge_pcode_rw(i915, mbox, &request, NULL,
467                                          500, 0,
468                                          true);
469
470         return *status || ((request & reply_mask) == reply);
471 }
472
473 /**
474  * skl_pcode_request - send PCODE request until acknowledgment
475  * @i915: device private
476  * @mbox: PCODE mailbox ID the request is targeted for
477  * @request: request ID
478  * @reply_mask: mask used to check for request acknowledgment
479  * @reply: value used to check for request acknowledgment
480  * @timeout_base_ms: timeout for polling with preemption enabled
481  *
482  * Keep resending the @request to @mbox until PCODE acknowledges it, PCODE
483  * reports an error or an overall timeout of @timeout_base_ms+50 ms expires.
484  * The request is acknowledged once the PCODE reply dword equals @reply after
485  * applying @reply_mask. Polling is first attempted with preemption enabled
486  * for @timeout_base_ms and if this times out for another 50 ms with
487  * preemption disabled.
488  *
489  * Returns 0 on success, %-ETIMEDOUT in case of a timeout, <0 in case of some
490  * other error as reported by PCODE.
491  */
492 int skl_pcode_request(struct drm_i915_private *i915, u32 mbox, u32 request,
493                       u32 reply_mask, u32 reply, int timeout_base_ms)
494 {
495         u32 status;
496         int ret;
497
498         mutex_lock(&i915->sb_lock);
499
500 #define COND \
501         skl_pcode_try_request(i915, mbox, request, reply_mask, reply, &status)
502
503         /*
504          * Prime the PCODE by doing a request first. Normally it guarantees
505          * that a subsequent request, at most @timeout_base_ms later, succeeds.
506          * _wait_for() doesn't guarantee when its passed condition is evaluated
507          * first, so send the first request explicitly.
508          */
509         if (COND) {
510                 ret = 0;
511                 goto out;
512         }
513         ret = _wait_for(COND, timeout_base_ms * 1000, 10, 10);
514         if (!ret)
515                 goto out;
516
517         /*
518          * The above can time out if the number of requests was low (2 in the
519          * worst case) _and_ PCODE was busy for some reason even after a
520          * (queued) request and @timeout_base_ms delay. As a workaround retry
521          * the poll with preemption disabled to maximize the number of
522          * requests. Increase the timeout from @timeout_base_ms to 50ms to
523          * account for interrupts that could reduce the number of these
524          * requests, and for any quirks of the PCODE firmware that delays
525          * the request completion.
526          */
527         drm_dbg_kms(&i915->drm,
528                     "PCODE timeout, retrying with preemption disabled\n");
529         WARN_ON_ONCE(timeout_base_ms > 3);
530         preempt_disable();
531         ret = wait_for_atomic(COND, 50);
532         preempt_enable();
533
534 out:
535         mutex_unlock(&i915->sb_lock);
536         return ret ? ret : status;
537 #undef COND
538 }