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