Home | History | Annotate | Line # | Download | only in display
      1  1.6  riastrad /*	$NetBSD: intel_hdcp.c,v 1.6 2021/12/19 12:32:15 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /* SPDX-License-Identifier: MIT */
      4  1.1  riastrad /*
      5  1.1  riastrad  * Copyright (C) 2017 Google, Inc.
      6  1.1  riastrad  * Copyright _ 2017-2019, Intel Corporation.
      7  1.1  riastrad  *
      8  1.1  riastrad  * Authors:
      9  1.1  riastrad  * Sean Paul <seanpaul (at) chromium.org>
     10  1.1  riastrad  * Ramalingam C <ramalingam.c (at) intel.com>
     11  1.1  riastrad  */
     12  1.1  riastrad 
     13  1.1  riastrad #include <sys/cdefs.h>
     14  1.6  riastrad __KERNEL_RCSID(0, "$NetBSD: intel_hdcp.c,v 1.6 2021/12/19 12:32:15 riastradh Exp $");
     15  1.1  riastrad 
     16  1.1  riastrad #include <linux/component.h>
     17  1.1  riastrad #include <linux/i2c.h>
     18  1.1  riastrad #include <linux/random.h>
     19  1.1  riastrad 
     20  1.1  riastrad #include <drm/drm_hdcp.h>
     21  1.1  riastrad #include <drm/i915_component.h>
     22  1.1  riastrad 
     23  1.1  riastrad #include "i915_reg.h"
     24  1.1  riastrad #include "intel_display_power.h"
     25  1.1  riastrad #include "intel_display_types.h"
     26  1.1  riastrad #include "intel_hdcp.h"
     27  1.1  riastrad #include "intel_sideband.h"
     28  1.1  riastrad #include "intel_connector.h"
     29  1.1  riastrad 
     30  1.5  riastrad #include <linux/nbsd-namespace.h>
     31  1.5  riastrad 
     32  1.1  riastrad #define KEY_LOAD_TRIES	5
     33  1.1  riastrad #define ENCRYPT_STATUS_CHANGE_TIMEOUT_MS	50
     34  1.1  riastrad #define HDCP2_LC_RETRY_CNT			3
     35  1.1  riastrad 
     36  1.1  riastrad static
     37  1.1  riastrad bool intel_hdcp_is_ksv_valid(u8 *ksv)
     38  1.1  riastrad {
     39  1.1  riastrad 	int i, ones = 0;
     40  1.1  riastrad 	/* KSV has 20 1's and 20 0's */
     41  1.1  riastrad 	for (i = 0; i < DRM_HDCP_KSV_LEN; i++)
     42  1.1  riastrad 		ones += hweight8(ksv[i]);
     43  1.1  riastrad 	if (ones != 20)
     44  1.1  riastrad 		return false;
     45  1.1  riastrad 
     46  1.1  riastrad 	return true;
     47  1.1  riastrad }
     48  1.1  riastrad 
     49  1.1  riastrad static
     50  1.1  riastrad int intel_hdcp_read_valid_bksv(struct intel_digital_port *intel_dig_port,
     51  1.1  riastrad 			       const struct intel_hdcp_shim *shim, u8 *bksv)
     52  1.1  riastrad {
     53  1.1  riastrad 	int ret, i, tries = 2;
     54  1.1  riastrad 
     55  1.1  riastrad 	/* HDCP spec states that we must retry the bksv if it is invalid */
     56  1.1  riastrad 	for (i = 0; i < tries; i++) {
     57  1.1  riastrad 		ret = shim->read_bksv(intel_dig_port, bksv);
     58  1.1  riastrad 		if (ret)
     59  1.1  riastrad 			return ret;
     60  1.1  riastrad 		if (intel_hdcp_is_ksv_valid(bksv))
     61  1.1  riastrad 			break;
     62  1.1  riastrad 	}
     63  1.1  riastrad 	if (i == tries) {
     64  1.1  riastrad 		DRM_DEBUG_KMS("Bksv is invalid\n");
     65  1.1  riastrad 		return -ENODEV;
     66  1.1  riastrad 	}
     67  1.1  riastrad 
     68  1.1  riastrad 	return 0;
     69  1.1  riastrad }
     70  1.1  riastrad 
     71  1.1  riastrad /* Is HDCP1.4 capable on Platform and Sink */
     72  1.1  riastrad bool intel_hdcp_capable(struct intel_connector *connector)
     73  1.1  riastrad {
     74  1.1  riastrad 	struct intel_digital_port *intel_dig_port = conn_to_dig_port(connector);
     75  1.1  riastrad 	const struct intel_hdcp_shim *shim = connector->hdcp.shim;
     76  1.1  riastrad 	bool capable = false;
     77  1.1  riastrad 	u8 bksv[5];
     78  1.1  riastrad 
     79  1.1  riastrad 	if (!shim)
     80  1.1  riastrad 		return capable;
     81  1.1  riastrad 
     82  1.1  riastrad 	if (shim->hdcp_capable) {
     83  1.1  riastrad 		shim->hdcp_capable(intel_dig_port, &capable);
     84  1.1  riastrad 	} else {
     85  1.1  riastrad 		if (!intel_hdcp_read_valid_bksv(intel_dig_port, shim, bksv))
     86  1.1  riastrad 			capable = true;
     87  1.1  riastrad 	}
     88  1.1  riastrad 
     89  1.1  riastrad 	return capable;
     90  1.1  riastrad }
     91  1.1  riastrad 
     92  1.1  riastrad /* Is HDCP2.2 capable on Platform and Sink */
     93  1.1  riastrad bool intel_hdcp2_capable(struct intel_connector *connector)
     94  1.1  riastrad {
     95  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
     96  1.1  riastrad 	struct intel_digital_port *intel_dig_port = conn_to_dig_port(connector);
     97  1.1  riastrad 	struct intel_hdcp *hdcp = &connector->hdcp;
     98  1.1  riastrad 	bool capable = false;
     99  1.1  riastrad 
    100  1.1  riastrad 	/* I915 support for HDCP2.2 */
    101  1.1  riastrad 	if (!hdcp->hdcp2_supported)
    102  1.1  riastrad 		return false;
    103  1.1  riastrad 
    104  1.1  riastrad 	/* MEI interface is solid */
    105  1.1  riastrad 	mutex_lock(&dev_priv->hdcp_comp_mutex);
    106  1.1  riastrad 	if (!dev_priv->hdcp_comp_added ||  !dev_priv->hdcp_master) {
    107  1.1  riastrad 		mutex_unlock(&dev_priv->hdcp_comp_mutex);
    108  1.1  riastrad 		return false;
    109  1.1  riastrad 	}
    110  1.1  riastrad 	mutex_unlock(&dev_priv->hdcp_comp_mutex);
    111  1.1  riastrad 
    112  1.1  riastrad 	/* Sink's capability for HDCP2.2 */
    113  1.1  riastrad 	hdcp->shim->hdcp_2_2_capable(intel_dig_port, &capable);
    114  1.1  riastrad 
    115  1.1  riastrad 	return capable;
    116  1.1  riastrad }
    117  1.1  riastrad 
    118  1.1  riastrad static inline
    119  1.1  riastrad bool intel_hdcp_in_use(struct drm_i915_private *dev_priv,
    120  1.1  riastrad 		       enum transcoder cpu_transcoder, enum port port)
    121  1.1  riastrad {
    122  1.1  riastrad 	return I915_READ(HDCP_STATUS(dev_priv, cpu_transcoder, port)) &
    123  1.1  riastrad 	       HDCP_STATUS_ENC;
    124  1.1  riastrad }
    125  1.1  riastrad 
    126  1.1  riastrad static inline
    127  1.1  riastrad bool intel_hdcp2_in_use(struct drm_i915_private *dev_priv,
    128  1.1  riastrad 			enum transcoder cpu_transcoder, enum port port)
    129  1.1  riastrad {
    130  1.1  riastrad 	return I915_READ(HDCP2_STATUS(dev_priv, cpu_transcoder, port)) &
    131  1.1  riastrad 	       LINK_ENCRYPTION_STATUS;
    132  1.1  riastrad }
    133  1.1  riastrad 
    134  1.1  riastrad static int intel_hdcp_poll_ksv_fifo(struct intel_digital_port *intel_dig_port,
    135  1.1  riastrad 				    const struct intel_hdcp_shim *shim)
    136  1.1  riastrad {
    137  1.1  riastrad 	int ret, read_ret;
    138  1.1  riastrad 	bool ksv_ready;
    139  1.1  riastrad 
    140  1.1  riastrad 	/* Poll for ksv list ready (spec says max time allowed is 5s) */
    141  1.1  riastrad 	ret = __wait_for(read_ret = shim->read_ksv_ready(intel_dig_port,
    142  1.1  riastrad 							 &ksv_ready),
    143  1.1  riastrad 			 read_ret || ksv_ready, 5 * 1000 * 1000, 1000,
    144  1.1  riastrad 			 100 * 1000);
    145  1.1  riastrad 	if (ret)
    146  1.1  riastrad 		return ret;
    147  1.1  riastrad 	if (read_ret)
    148  1.1  riastrad 		return read_ret;
    149  1.1  riastrad 	if (!ksv_ready)
    150  1.1  riastrad 		return -ETIMEDOUT;
    151  1.1  riastrad 
    152  1.1  riastrad 	return 0;
    153  1.1  riastrad }
    154  1.1  riastrad 
    155  1.1  riastrad static bool hdcp_key_loadable(struct drm_i915_private *dev_priv)
    156  1.1  riastrad {
    157  1.1  riastrad 	struct i915_power_domains *power_domains = &dev_priv->power_domains;
    158  1.1  riastrad 	struct i915_power_well *power_well;
    159  1.1  riastrad 	enum i915_power_well_id id;
    160  1.1  riastrad 	bool enabled = false;
    161  1.1  riastrad 
    162  1.1  riastrad 	/*
    163  1.1  riastrad 	 * On HSW and BDW, Display HW loads the Key as soon as Display resumes.
    164  1.1  riastrad 	 * On all BXT+, SW can load the keys only when the PW#1 is turned on.
    165  1.1  riastrad 	 */
    166  1.1  riastrad 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
    167  1.1  riastrad 		id = HSW_DISP_PW_GLOBAL;
    168  1.1  riastrad 	else
    169  1.1  riastrad 		id = SKL_DISP_PW_1;
    170  1.1  riastrad 
    171  1.1  riastrad 	mutex_lock(&power_domains->lock);
    172  1.1  riastrad 
    173  1.1  riastrad 	/* PG1 (power well #1) needs to be enabled */
    174  1.1  riastrad 	for_each_power_well(dev_priv, power_well) {
    175  1.1  riastrad 		if (power_well->desc->id == id) {
    176  1.1  riastrad 			enabled = power_well->desc->ops->is_enabled(dev_priv,
    177  1.1  riastrad 								    power_well);
    178  1.1  riastrad 			break;
    179  1.1  riastrad 		}
    180  1.1  riastrad 	}
    181  1.1  riastrad 	mutex_unlock(&power_domains->lock);
    182  1.1  riastrad 
    183  1.1  riastrad 	/*
    184  1.1  riastrad 	 * Another req for hdcp key loadability is enabled state of pll for
    185  1.1  riastrad 	 * cdclk. Without active crtc we wont land here. So we are assuming that
    186  1.1  riastrad 	 * cdclk is already on.
    187  1.1  riastrad 	 */
    188  1.1  riastrad 
    189  1.1  riastrad 	return enabled;
    190  1.1  riastrad }
    191  1.1  riastrad 
    192  1.1  riastrad static void intel_hdcp_clear_keys(struct drm_i915_private *dev_priv)
    193  1.1  riastrad {
    194  1.1  riastrad 	I915_WRITE(HDCP_KEY_CONF, HDCP_CLEAR_KEYS_TRIGGER);
    195  1.1  riastrad 	I915_WRITE(HDCP_KEY_STATUS, HDCP_KEY_LOAD_DONE | HDCP_KEY_LOAD_STATUS |
    196  1.1  riastrad 		   HDCP_FUSE_IN_PROGRESS | HDCP_FUSE_ERROR | HDCP_FUSE_DONE);
    197  1.1  riastrad }
    198  1.1  riastrad 
    199  1.1  riastrad static int intel_hdcp_load_keys(struct drm_i915_private *dev_priv)
    200  1.1  riastrad {
    201  1.1  riastrad 	int ret;
    202  1.1  riastrad 	u32 val;
    203  1.1  riastrad 
    204  1.1  riastrad 	val = I915_READ(HDCP_KEY_STATUS);
    205  1.1  riastrad 	if ((val & HDCP_KEY_LOAD_DONE) && (val & HDCP_KEY_LOAD_STATUS))
    206  1.1  riastrad 		return 0;
    207  1.1  riastrad 
    208  1.1  riastrad 	/*
    209  1.1  riastrad 	 * On HSW and BDW HW loads the HDCP1.4 Key when Display comes
    210  1.1  riastrad 	 * out of reset. So if Key is not already loaded, its an error state.
    211  1.1  riastrad 	 */
    212  1.1  riastrad 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
    213  1.1  riastrad 		if (!(I915_READ(HDCP_KEY_STATUS) & HDCP_KEY_LOAD_DONE))
    214  1.1  riastrad 			return -ENXIO;
    215  1.1  riastrad 
    216  1.1  riastrad 	/*
    217  1.1  riastrad 	 * Initiate loading the HDCP key from fuses.
    218  1.1  riastrad 	 *
    219  1.1  riastrad 	 * BXT+ platforms, HDCP key needs to be loaded by SW. Only Gen 9
    220  1.1  riastrad 	 * platforms except BXT and GLK, differ in the key load trigger process
    221  1.1  riastrad 	 * from other platforms. So GEN9_BC uses the GT Driver Mailbox i/f.
    222  1.1  riastrad 	 */
    223  1.1  riastrad 	if (IS_GEN9_BC(dev_priv)) {
    224  1.1  riastrad 		ret = sandybridge_pcode_write(dev_priv,
    225  1.1  riastrad 					      SKL_PCODE_LOAD_HDCP_KEYS, 1);
    226  1.1  riastrad 		if (ret) {
    227  1.1  riastrad 			DRM_ERROR("Failed to initiate HDCP key load (%d)\n",
    228  1.1  riastrad 			          ret);
    229  1.1  riastrad 			return ret;
    230  1.1  riastrad 		}
    231  1.1  riastrad 	} else {
    232  1.1  riastrad 		I915_WRITE(HDCP_KEY_CONF, HDCP_KEY_LOAD_TRIGGER);
    233  1.1  riastrad 	}
    234  1.1  riastrad 
    235  1.1  riastrad 	/* Wait for the keys to load (500us) */
    236  1.1  riastrad 	ret = __intel_wait_for_register(&dev_priv->uncore, HDCP_KEY_STATUS,
    237  1.1  riastrad 					HDCP_KEY_LOAD_DONE, HDCP_KEY_LOAD_DONE,
    238  1.1  riastrad 					10, 1, &val);
    239  1.1  riastrad 	if (ret)
    240  1.1  riastrad 		return ret;
    241  1.1  riastrad 	else if (!(val & HDCP_KEY_LOAD_STATUS))
    242  1.1  riastrad 		return -ENXIO;
    243  1.1  riastrad 
    244  1.1  riastrad 	/* Send Aksv over to PCH display for use in authentication */
    245  1.1  riastrad 	I915_WRITE(HDCP_KEY_CONF, HDCP_AKSV_SEND_TRIGGER);
    246  1.1  riastrad 
    247  1.1  riastrad 	return 0;
    248  1.1  riastrad }
    249  1.1  riastrad 
    250  1.1  riastrad /* Returns updated SHA-1 index */
    251  1.1  riastrad static int intel_write_sha_text(struct drm_i915_private *dev_priv, u32 sha_text)
    252  1.1  riastrad {
    253  1.1  riastrad 	I915_WRITE(HDCP_SHA_TEXT, sha_text);
    254  1.1  riastrad 	if (intel_de_wait_for_set(dev_priv, HDCP_REP_CTL, HDCP_SHA1_READY, 1)) {
    255  1.1  riastrad 		DRM_ERROR("Timed out waiting for SHA1 ready\n");
    256  1.1  riastrad 		return -ETIMEDOUT;
    257  1.1  riastrad 	}
    258  1.1  riastrad 	return 0;
    259  1.1  riastrad }
    260  1.1  riastrad 
    261  1.1  riastrad static
    262  1.1  riastrad u32 intel_hdcp_get_repeater_ctl(struct drm_i915_private *dev_priv,
    263  1.1  riastrad 				enum transcoder cpu_transcoder, enum port port)
    264  1.1  riastrad {
    265  1.1  riastrad 	if (INTEL_GEN(dev_priv) >= 12) {
    266  1.1  riastrad 		switch (cpu_transcoder) {
    267  1.1  riastrad 		case TRANSCODER_A:
    268  1.1  riastrad 			return HDCP_TRANSA_REP_PRESENT |
    269  1.1  riastrad 			       HDCP_TRANSA_SHA1_M0;
    270  1.1  riastrad 		case TRANSCODER_B:
    271  1.1  riastrad 			return HDCP_TRANSB_REP_PRESENT |
    272  1.1  riastrad 			       HDCP_TRANSB_SHA1_M0;
    273  1.1  riastrad 		case TRANSCODER_C:
    274  1.1  riastrad 			return HDCP_TRANSC_REP_PRESENT |
    275  1.1  riastrad 			       HDCP_TRANSC_SHA1_M0;
    276  1.1  riastrad 		case TRANSCODER_D:
    277  1.1  riastrad 			return HDCP_TRANSD_REP_PRESENT |
    278  1.1  riastrad 			       HDCP_TRANSD_SHA1_M0;
    279  1.1  riastrad 		default:
    280  1.1  riastrad 			DRM_ERROR("Unknown transcoder %d\n", cpu_transcoder);
    281  1.1  riastrad 			return -EINVAL;
    282  1.1  riastrad 		}
    283  1.1  riastrad 	}
    284  1.1  riastrad 
    285  1.1  riastrad 	switch (port) {
    286  1.1  riastrad 	case PORT_A:
    287  1.1  riastrad 		return HDCP_DDIA_REP_PRESENT | HDCP_DDIA_SHA1_M0;
    288  1.1  riastrad 	case PORT_B:
    289  1.1  riastrad 		return HDCP_DDIB_REP_PRESENT | HDCP_DDIB_SHA1_M0;
    290  1.1  riastrad 	case PORT_C:
    291  1.1  riastrad 		return HDCP_DDIC_REP_PRESENT | HDCP_DDIC_SHA1_M0;
    292  1.1  riastrad 	case PORT_D:
    293  1.1  riastrad 		return HDCP_DDID_REP_PRESENT | HDCP_DDID_SHA1_M0;
    294  1.1  riastrad 	case PORT_E:
    295  1.1  riastrad 		return HDCP_DDIE_REP_PRESENT | HDCP_DDIE_SHA1_M0;
    296  1.1  riastrad 	default:
    297  1.1  riastrad 		DRM_ERROR("Unknown port %d\n", port);
    298  1.1  riastrad 		return -EINVAL;
    299  1.1  riastrad 	}
    300  1.1  riastrad }
    301  1.1  riastrad 
    302  1.1  riastrad static
    303  1.1  riastrad int intel_hdcp_validate_v_prime(struct intel_connector *connector,
    304  1.1  riastrad 				const struct intel_hdcp_shim *shim,
    305  1.1  riastrad 				u8 *ksv_fifo, u8 num_downstream, u8 *bstatus)
    306  1.1  riastrad {
    307  1.1  riastrad 	struct intel_digital_port *intel_dig_port = conn_to_dig_port(connector);
    308  1.1  riastrad 	struct drm_i915_private *dev_priv;
    309  1.1  riastrad 	enum transcoder cpu_transcoder = connector->hdcp.cpu_transcoder;
    310  1.1  riastrad 	enum port port = intel_dig_port->base.port;
    311  1.1  riastrad 	u32 vprime, sha_text, sha_leftovers, rep_ctl;
    312  1.1  riastrad 	int ret, i, j, sha_idx;
    313  1.1  riastrad 
    314  1.1  riastrad 	dev_priv = intel_dig_port->base.base.dev->dev_private;
    315  1.1  riastrad 
    316  1.1  riastrad 	/* Process V' values from the receiver */
    317  1.1  riastrad 	for (i = 0; i < DRM_HDCP_V_PRIME_NUM_PARTS; i++) {
    318  1.1  riastrad 		ret = shim->read_v_prime_part(intel_dig_port, i, &vprime);
    319  1.1  riastrad 		if (ret)
    320  1.1  riastrad 			return ret;
    321  1.1  riastrad 		I915_WRITE(HDCP_SHA_V_PRIME(i), vprime);
    322  1.1  riastrad 	}
    323  1.1  riastrad 
    324  1.1  riastrad 	/*
    325  1.1  riastrad 	 * We need to write the concatenation of all device KSVs, BINFO (DP) ||
    326  1.1  riastrad 	 * BSTATUS (HDMI), and M0 (which is added via HDCP_REP_CTL). This byte
    327  1.1  riastrad 	 * stream is written via the HDCP_SHA_TEXT register in 32-bit
    328  1.1  riastrad 	 * increments. Every 64 bytes, we need to write HDCP_REP_CTL again. This
    329  1.1  riastrad 	 * index will keep track of our progress through the 64 bytes as well as
    330  1.1  riastrad 	 * helping us work the 40-bit KSVs through our 32-bit register.
    331  1.1  riastrad 	 *
    332  1.1  riastrad 	 * NOTE: data passed via HDCP_SHA_TEXT should be big-endian
    333  1.1  riastrad 	 */
    334  1.1  riastrad 	sha_idx = 0;
    335  1.1  riastrad 	sha_text = 0;
    336  1.1  riastrad 	sha_leftovers = 0;
    337  1.1  riastrad 	rep_ctl = intel_hdcp_get_repeater_ctl(dev_priv, cpu_transcoder, port);
    338  1.1  riastrad 	I915_WRITE(HDCP_REP_CTL, rep_ctl | HDCP_SHA1_TEXT_32);
    339  1.1  riastrad 	for (i = 0; i < num_downstream; i++) {
    340  1.1  riastrad 		unsigned int sha_empty;
    341  1.1  riastrad 		u8 *ksv = &ksv_fifo[i * DRM_HDCP_KSV_LEN];
    342  1.1  riastrad 
    343  1.1  riastrad 		/* Fill up the empty slots in sha_text and write it out */
    344  1.1  riastrad 		sha_empty = sizeof(sha_text) - sha_leftovers;
    345  1.1  riastrad 		for (j = 0; j < sha_empty; j++)
    346  1.1  riastrad 			sha_text |= ksv[j] << ((sizeof(sha_text) - j - 1) * 8);
    347  1.1  riastrad 
    348  1.1  riastrad 		ret = intel_write_sha_text(dev_priv, sha_text);
    349  1.1  riastrad 		if (ret < 0)
    350  1.1  riastrad 			return ret;
    351  1.1  riastrad 
    352  1.1  riastrad 		/* Programming guide writes this every 64 bytes */
    353  1.1  riastrad 		sha_idx += sizeof(sha_text);
    354  1.1  riastrad 		if (!(sha_idx % 64))
    355  1.1  riastrad 			I915_WRITE(HDCP_REP_CTL, rep_ctl | HDCP_SHA1_TEXT_32);
    356  1.1  riastrad 
    357  1.1  riastrad 		/* Store the leftover bytes from the ksv in sha_text */
    358  1.1  riastrad 		sha_leftovers = DRM_HDCP_KSV_LEN - sha_empty;
    359  1.1  riastrad 		sha_text = 0;
    360  1.1  riastrad 		for (j = 0; j < sha_leftovers; j++)
    361  1.1  riastrad 			sha_text |= ksv[sha_empty + j] <<
    362  1.1  riastrad 					((sizeof(sha_text) - j - 1) * 8);
    363  1.1  riastrad 
    364  1.1  riastrad 		/*
    365  1.1  riastrad 		 * If we still have room in sha_text for more data, continue.
    366  1.1  riastrad 		 * Otherwise, write it out immediately.
    367  1.1  riastrad 		 */
    368  1.1  riastrad 		if (sizeof(sha_text) > sha_leftovers)
    369  1.1  riastrad 			continue;
    370  1.1  riastrad 
    371  1.1  riastrad 		ret = intel_write_sha_text(dev_priv, sha_text);
    372  1.1  riastrad 		if (ret < 0)
    373  1.1  riastrad 			return ret;
    374  1.1  riastrad 		sha_leftovers = 0;
    375  1.1  riastrad 		sha_text = 0;
    376  1.1  riastrad 		sha_idx += sizeof(sha_text);
    377  1.1  riastrad 	}
    378  1.1  riastrad 
    379  1.1  riastrad 	/*
    380  1.1  riastrad 	 * We need to write BINFO/BSTATUS, and M0 now. Depending on how many
    381  1.1  riastrad 	 * bytes are leftover from the last ksv, we might be able to fit them
    382  1.1  riastrad 	 * all in sha_text (first 2 cases), or we might need to split them up
    383  1.1  riastrad 	 * into 2 writes (last 2 cases).
    384  1.1  riastrad 	 */
    385  1.1  riastrad 	if (sha_leftovers == 0) {
    386  1.1  riastrad 		/* Write 16 bits of text, 16 bits of M0 */
    387  1.1  riastrad 		I915_WRITE(HDCP_REP_CTL, rep_ctl | HDCP_SHA1_TEXT_16);
    388  1.1  riastrad 		ret = intel_write_sha_text(dev_priv,
    389  1.1  riastrad 					   bstatus[0] << 8 | bstatus[1]);
    390  1.1  riastrad 		if (ret < 0)
    391  1.1  riastrad 			return ret;
    392  1.1  riastrad 		sha_idx += sizeof(sha_text);
    393  1.1  riastrad 
    394  1.1  riastrad 		/* Write 32 bits of M0 */
    395  1.1  riastrad 		I915_WRITE(HDCP_REP_CTL, rep_ctl | HDCP_SHA1_TEXT_0);
    396  1.1  riastrad 		ret = intel_write_sha_text(dev_priv, 0);
    397  1.1  riastrad 		if (ret < 0)
    398  1.1  riastrad 			return ret;
    399  1.1  riastrad 		sha_idx += sizeof(sha_text);
    400  1.1  riastrad 
    401  1.1  riastrad 		/* Write 16 bits of M0 */
    402  1.1  riastrad 		I915_WRITE(HDCP_REP_CTL, rep_ctl | HDCP_SHA1_TEXT_16);
    403  1.1  riastrad 		ret = intel_write_sha_text(dev_priv, 0);
    404  1.1  riastrad 		if (ret < 0)
    405  1.1  riastrad 			return ret;
    406  1.1  riastrad 		sha_idx += sizeof(sha_text);
    407  1.1  riastrad 
    408  1.1  riastrad 	} else if (sha_leftovers == 1) {
    409  1.1  riastrad 		/* Write 24 bits of text, 8 bits of M0 */
    410  1.1  riastrad 		I915_WRITE(HDCP_REP_CTL, rep_ctl | HDCP_SHA1_TEXT_24);
    411  1.1  riastrad 		sha_text |= bstatus[0] << 16 | bstatus[1] << 8;
    412  1.1  riastrad 		/* Only 24-bits of data, must be in the LSB */
    413  1.1  riastrad 		sha_text = (sha_text & 0xffffff00) >> 8;
    414  1.1  riastrad 		ret = intel_write_sha_text(dev_priv, sha_text);
    415  1.1  riastrad 		if (ret < 0)
    416  1.1  riastrad 			return ret;
    417  1.1  riastrad 		sha_idx += sizeof(sha_text);
    418  1.1  riastrad 
    419  1.1  riastrad 		/* Write 32 bits of M0 */
    420  1.1  riastrad 		I915_WRITE(HDCP_REP_CTL, rep_ctl | HDCP_SHA1_TEXT_0);
    421  1.1  riastrad 		ret = intel_write_sha_text(dev_priv, 0);
    422  1.1  riastrad 		if (ret < 0)
    423  1.1  riastrad 			return ret;
    424  1.1  riastrad 		sha_idx += sizeof(sha_text);
    425  1.1  riastrad 
    426  1.1  riastrad 		/* Write 24 bits of M0 */
    427  1.1  riastrad 		I915_WRITE(HDCP_REP_CTL, rep_ctl | HDCP_SHA1_TEXT_8);
    428  1.1  riastrad 		ret = intel_write_sha_text(dev_priv, 0);
    429  1.1  riastrad 		if (ret < 0)
    430  1.1  riastrad 			return ret;
    431  1.1  riastrad 		sha_idx += sizeof(sha_text);
    432  1.1  riastrad 
    433  1.1  riastrad 	} else if (sha_leftovers == 2) {
    434  1.1  riastrad 		/* Write 32 bits of text */
    435  1.1  riastrad 		I915_WRITE(HDCP_REP_CTL, rep_ctl | HDCP_SHA1_TEXT_32);
    436  1.1  riastrad 		sha_text |= bstatus[0] << 24 | bstatus[1] << 16;
    437  1.1  riastrad 		ret = intel_write_sha_text(dev_priv, sha_text);
    438  1.1  riastrad 		if (ret < 0)
    439  1.1  riastrad 			return ret;
    440  1.1  riastrad 		sha_idx += sizeof(sha_text);
    441  1.1  riastrad 
    442  1.1  riastrad 		/* Write 64 bits of M0 */
    443  1.1  riastrad 		I915_WRITE(HDCP_REP_CTL, rep_ctl | HDCP_SHA1_TEXT_0);
    444  1.1  riastrad 		for (i = 0; i < 2; i++) {
    445  1.1  riastrad 			ret = intel_write_sha_text(dev_priv, 0);
    446  1.1  riastrad 			if (ret < 0)
    447  1.1  riastrad 				return ret;
    448  1.1  riastrad 			sha_idx += sizeof(sha_text);
    449  1.1  riastrad 		}
    450  1.1  riastrad 	} else if (sha_leftovers == 3) {
    451  1.1  riastrad 		/* Write 32 bits of text */
    452  1.1  riastrad 		I915_WRITE(HDCP_REP_CTL, rep_ctl | HDCP_SHA1_TEXT_32);
    453  1.1  riastrad 		sha_text |= bstatus[0] << 24;
    454  1.1  riastrad 		ret = intel_write_sha_text(dev_priv, sha_text);
    455  1.1  riastrad 		if (ret < 0)
    456  1.1  riastrad 			return ret;
    457  1.1  riastrad 		sha_idx += sizeof(sha_text);
    458  1.1  riastrad 
    459  1.1  riastrad 		/* Write 8 bits of text, 24 bits of M0 */
    460  1.1  riastrad 		I915_WRITE(HDCP_REP_CTL, rep_ctl | HDCP_SHA1_TEXT_8);
    461  1.1  riastrad 		ret = intel_write_sha_text(dev_priv, bstatus[1]);
    462  1.1  riastrad 		if (ret < 0)
    463  1.1  riastrad 			return ret;
    464  1.1  riastrad 		sha_idx += sizeof(sha_text);
    465  1.1  riastrad 
    466  1.1  riastrad 		/* Write 32 bits of M0 */
    467  1.1  riastrad 		I915_WRITE(HDCP_REP_CTL, rep_ctl | HDCP_SHA1_TEXT_0);
    468  1.1  riastrad 		ret = intel_write_sha_text(dev_priv, 0);
    469  1.1  riastrad 		if (ret < 0)
    470  1.1  riastrad 			return ret;
    471  1.1  riastrad 		sha_idx += sizeof(sha_text);
    472  1.1  riastrad 
    473  1.1  riastrad 		/* Write 8 bits of M0 */
    474  1.1  riastrad 		I915_WRITE(HDCP_REP_CTL, rep_ctl | HDCP_SHA1_TEXT_24);
    475  1.1  riastrad 		ret = intel_write_sha_text(dev_priv, 0);
    476  1.1  riastrad 		if (ret < 0)
    477  1.1  riastrad 			return ret;
    478  1.1  riastrad 		sha_idx += sizeof(sha_text);
    479  1.1  riastrad 	} else {
    480  1.1  riastrad 		DRM_DEBUG_KMS("Invalid number of leftovers %d\n",
    481  1.1  riastrad 			      sha_leftovers);
    482  1.1  riastrad 		return -EINVAL;
    483  1.1  riastrad 	}
    484  1.1  riastrad 
    485  1.1  riastrad 	I915_WRITE(HDCP_REP_CTL, rep_ctl | HDCP_SHA1_TEXT_32);
    486  1.1  riastrad 	/* Fill up to 64-4 bytes with zeros (leave the last write for length) */
    487  1.1  riastrad 	while ((sha_idx % 64) < (64 - sizeof(sha_text))) {
    488  1.1  riastrad 		ret = intel_write_sha_text(dev_priv, 0);
    489  1.1  riastrad 		if (ret < 0)
    490  1.1  riastrad 			return ret;
    491  1.1  riastrad 		sha_idx += sizeof(sha_text);
    492  1.1  riastrad 	}
    493  1.1  riastrad 
    494  1.1  riastrad 	/*
    495  1.1  riastrad 	 * Last write gets the length of the concatenation in bits. That is:
    496  1.1  riastrad 	 *  - 5 bytes per device
    497  1.1  riastrad 	 *  - 10 bytes for BINFO/BSTATUS(2), M0(8)
    498  1.1  riastrad 	 */
    499  1.1  riastrad 	sha_text = (num_downstream * 5 + 10) * 8;
    500  1.1  riastrad 	ret = intel_write_sha_text(dev_priv, sha_text);
    501  1.1  riastrad 	if (ret < 0)
    502  1.1  riastrad 		return ret;
    503  1.1  riastrad 
    504  1.1  riastrad 	/* Tell the HW we're done with the hash and wait for it to ACK */
    505  1.1  riastrad 	I915_WRITE(HDCP_REP_CTL, rep_ctl | HDCP_SHA1_COMPLETE_HASH);
    506  1.1  riastrad 	if (intel_de_wait_for_set(dev_priv, HDCP_REP_CTL,
    507  1.1  riastrad 				  HDCP_SHA1_COMPLETE, 1)) {
    508  1.1  riastrad 		DRM_ERROR("Timed out waiting for SHA1 complete\n");
    509  1.1  riastrad 		return -ETIMEDOUT;
    510  1.1  riastrad 	}
    511  1.1  riastrad 	if (!(I915_READ(HDCP_REP_CTL) & HDCP_SHA1_V_MATCH)) {
    512  1.1  riastrad 		DRM_DEBUG_KMS("SHA-1 mismatch, HDCP failed\n");
    513  1.1  riastrad 		return -ENXIO;
    514  1.1  riastrad 	}
    515  1.1  riastrad 
    516  1.1  riastrad 	return 0;
    517  1.1  riastrad }
    518  1.1  riastrad 
    519  1.1  riastrad /* Implements Part 2 of the HDCP authorization procedure */
    520  1.1  riastrad static
    521  1.1  riastrad int intel_hdcp_auth_downstream(struct intel_connector *connector)
    522  1.1  riastrad {
    523  1.1  riastrad 	struct intel_digital_port *intel_dig_port = conn_to_dig_port(connector);
    524  1.1  riastrad 	const struct intel_hdcp_shim *shim = connector->hdcp.shim;
    525  1.1  riastrad 	struct drm_device *dev = connector->base.dev;
    526  1.1  riastrad 	u8 bstatus[2], num_downstream, *ksv_fifo;
    527  1.1  riastrad 	int ret, i, tries = 3;
    528  1.1  riastrad 
    529  1.1  riastrad 	ret = intel_hdcp_poll_ksv_fifo(intel_dig_port, shim);
    530  1.1  riastrad 	if (ret) {
    531  1.1  riastrad 		DRM_DEBUG_KMS("KSV list failed to become ready (%d)\n", ret);
    532  1.1  riastrad 		return ret;
    533  1.1  riastrad 	}
    534  1.1  riastrad 
    535  1.1  riastrad 	ret = shim->read_bstatus(intel_dig_port, bstatus);
    536  1.1  riastrad 	if (ret)
    537  1.1  riastrad 		return ret;
    538  1.1  riastrad 
    539  1.1  riastrad 	if (DRM_HDCP_MAX_DEVICE_EXCEEDED(bstatus[0]) ||
    540  1.1  riastrad 	    DRM_HDCP_MAX_CASCADE_EXCEEDED(bstatus[1])) {
    541  1.1  riastrad 		DRM_DEBUG_KMS("Max Topology Limit Exceeded\n");
    542  1.1  riastrad 		return -EPERM;
    543  1.1  riastrad 	}
    544  1.1  riastrad 
    545  1.1  riastrad 	/*
    546  1.1  riastrad 	 * When repeater reports 0 device count, HDCP1.4 spec allows disabling
    547  1.1  riastrad 	 * the HDCP encryption. That implies that repeater can't have its own
    548  1.1  riastrad 	 * display. As there is no consumption of encrypted content in the
    549  1.1  riastrad 	 * repeater with 0 downstream devices, we are failing the
    550  1.1  riastrad 	 * authentication.
    551  1.1  riastrad 	 */
    552  1.1  riastrad 	num_downstream = DRM_HDCP_NUM_DOWNSTREAM(bstatus[0]);
    553  1.1  riastrad 	if (num_downstream == 0) {
    554  1.1  riastrad 		DRM_DEBUG_KMS("Repeater with zero downstream devices\n");
    555  1.1  riastrad 		return -EINVAL;
    556  1.1  riastrad 	}
    557  1.1  riastrad 
    558  1.1  riastrad 	ksv_fifo = kcalloc(DRM_HDCP_KSV_LEN, num_downstream, GFP_KERNEL);
    559  1.1  riastrad 	if (!ksv_fifo) {
    560  1.1  riastrad 		DRM_DEBUG_KMS("Out of mem: ksv_fifo\n");
    561  1.1  riastrad 		return -ENOMEM;
    562  1.1  riastrad 	}
    563  1.1  riastrad 
    564  1.1  riastrad 	ret = shim->read_ksv_fifo(intel_dig_port, num_downstream, ksv_fifo);
    565  1.1  riastrad 	if (ret)
    566  1.1  riastrad 		goto err;
    567  1.1  riastrad 
    568  1.1  riastrad 	if (drm_hdcp_check_ksvs_revoked(dev, ksv_fifo, num_downstream)) {
    569  1.1  riastrad 		DRM_ERROR("Revoked Ksv(s) in ksv_fifo\n");
    570  1.1  riastrad 		ret = -EPERM;
    571  1.1  riastrad 		goto err;
    572  1.1  riastrad 	}
    573  1.1  riastrad 
    574  1.1  riastrad 	/*
    575  1.1  riastrad 	 * When V prime mismatches, DP Spec mandates re-read of
    576  1.1  riastrad 	 * V prime atleast twice.
    577  1.1  riastrad 	 */
    578  1.1  riastrad 	for (i = 0; i < tries; i++) {
    579  1.1  riastrad 		ret = intel_hdcp_validate_v_prime(connector, shim,
    580  1.1  riastrad 						  ksv_fifo, num_downstream,
    581  1.1  riastrad 						  bstatus);
    582  1.1  riastrad 		if (!ret)
    583  1.1  riastrad 			break;
    584  1.1  riastrad 	}
    585  1.1  riastrad 
    586  1.1  riastrad 	if (i == tries) {
    587  1.1  riastrad 		DRM_DEBUG_KMS("V Prime validation failed.(%d)\n", ret);
    588  1.1  riastrad 		goto err;
    589  1.1  riastrad 	}
    590  1.1  riastrad 
    591  1.1  riastrad 	DRM_DEBUG_KMS("HDCP is enabled (%d downstream devices)\n",
    592  1.1  riastrad 		      num_downstream);
    593  1.1  riastrad 	ret = 0;
    594  1.1  riastrad err:
    595  1.1  riastrad 	kfree(ksv_fifo);
    596  1.1  riastrad 	return ret;
    597  1.1  riastrad }
    598  1.1  riastrad 
    599  1.1  riastrad /* Implements Part 1 of the HDCP authorization procedure */
    600  1.1  riastrad static int intel_hdcp_auth(struct intel_connector *connector)
    601  1.1  riastrad {
    602  1.1  riastrad 	struct intel_digital_port *intel_dig_port = conn_to_dig_port(connector);
    603  1.1  riastrad 	struct intel_hdcp *hdcp = &connector->hdcp;
    604  1.1  riastrad 	struct drm_device *dev = connector->base.dev;
    605  1.1  riastrad 	const struct intel_hdcp_shim *shim = hdcp->shim;
    606  1.1  riastrad 	struct drm_i915_private *dev_priv;
    607  1.1  riastrad 	enum transcoder cpu_transcoder = connector->hdcp.cpu_transcoder;
    608  1.1  riastrad 	enum port port;
    609  1.1  riastrad 	unsigned long r0_prime_gen_start;
    610  1.1  riastrad 	int ret, i, tries = 2;
    611  1.1  riastrad 	union {
    612  1.1  riastrad 		u32 reg[2];
    613  1.1  riastrad 		u8 shim[DRM_HDCP_AN_LEN];
    614  1.1  riastrad 	} an;
    615  1.1  riastrad 	union {
    616  1.1  riastrad 		u32 reg[2];
    617  1.1  riastrad 		u8 shim[DRM_HDCP_KSV_LEN];
    618  1.1  riastrad 	} bksv;
    619  1.1  riastrad 	union {
    620  1.1  riastrad 		u32 reg;
    621  1.1  riastrad 		u8 shim[DRM_HDCP_RI_LEN];
    622  1.1  riastrad 	} ri;
    623  1.1  riastrad 	bool repeater_present, hdcp_capable;
    624  1.1  riastrad 
    625  1.1  riastrad 	dev_priv = intel_dig_port->base.base.dev->dev_private;
    626  1.1  riastrad 
    627  1.1  riastrad 	port = intel_dig_port->base.port;
    628  1.1  riastrad 
    629  1.1  riastrad 	/*
    630  1.1  riastrad 	 * Detects whether the display is HDCP capable. Although we check for
    631  1.1  riastrad 	 * valid Bksv below, the HDCP over DP spec requires that we check
    632  1.1  riastrad 	 * whether the display supports HDCP before we write An. For HDMI
    633  1.1  riastrad 	 * displays, this is not necessary.
    634  1.1  riastrad 	 */
    635  1.1  riastrad 	if (shim->hdcp_capable) {
    636  1.1  riastrad 		ret = shim->hdcp_capable(intel_dig_port, &hdcp_capable);
    637  1.1  riastrad 		if (ret)
    638  1.1  riastrad 			return ret;
    639  1.1  riastrad 		if (!hdcp_capable) {
    640  1.1  riastrad 			DRM_DEBUG_KMS("Panel is not HDCP capable\n");
    641  1.1  riastrad 			return -EINVAL;
    642  1.1  riastrad 		}
    643  1.1  riastrad 	}
    644  1.1  riastrad 
    645  1.1  riastrad 	/* Initialize An with 2 random values and acquire it */
    646  1.1  riastrad 	for (i = 0; i < 2; i++)
    647  1.1  riastrad 		I915_WRITE(HDCP_ANINIT(dev_priv, cpu_transcoder, port),
    648  1.1  riastrad 			   get_random_u32());
    649  1.1  riastrad 	I915_WRITE(HDCP_CONF(dev_priv, cpu_transcoder, port),
    650  1.1  riastrad 		   HDCP_CONF_CAPTURE_AN);
    651  1.1  riastrad 
    652  1.1  riastrad 	/* Wait for An to be acquired */
    653  1.1  riastrad 	if (intel_de_wait_for_set(dev_priv,
    654  1.1  riastrad 				  HDCP_STATUS(dev_priv, cpu_transcoder, port),
    655  1.1  riastrad 				  HDCP_STATUS_AN_READY, 1)) {
    656  1.1  riastrad 		DRM_ERROR("Timed out waiting for An\n");
    657  1.1  riastrad 		return -ETIMEDOUT;
    658  1.1  riastrad 	}
    659  1.1  riastrad 
    660  1.1  riastrad 	an.reg[0] = I915_READ(HDCP_ANLO(dev_priv, cpu_transcoder, port));
    661  1.1  riastrad 	an.reg[1] = I915_READ(HDCP_ANHI(dev_priv, cpu_transcoder, port));
    662  1.1  riastrad 	ret = shim->write_an_aksv(intel_dig_port, an.shim);
    663  1.1  riastrad 	if (ret)
    664  1.1  riastrad 		return ret;
    665  1.1  riastrad 
    666  1.1  riastrad 	r0_prime_gen_start = jiffies;
    667  1.1  riastrad 
    668  1.1  riastrad 	memset(&bksv, 0, sizeof(bksv));
    669  1.1  riastrad 
    670  1.1  riastrad 	ret = intel_hdcp_read_valid_bksv(intel_dig_port, shim, bksv.shim);
    671  1.1  riastrad 	if (ret < 0)
    672  1.1  riastrad 		return ret;
    673  1.1  riastrad 
    674  1.1  riastrad 	if (drm_hdcp_check_ksvs_revoked(dev, bksv.shim, 1)) {
    675  1.1  riastrad 		DRM_ERROR("BKSV is revoked\n");
    676  1.1  riastrad 		return -EPERM;
    677  1.1  riastrad 	}
    678  1.1  riastrad 
    679  1.1  riastrad 	I915_WRITE(HDCP_BKSVLO(dev_priv, cpu_transcoder, port), bksv.reg[0]);
    680  1.1  riastrad 	I915_WRITE(HDCP_BKSVHI(dev_priv, cpu_transcoder, port), bksv.reg[1]);
    681  1.1  riastrad 
    682  1.1  riastrad 	ret = shim->repeater_present(intel_dig_port, &repeater_present);
    683  1.1  riastrad 	if (ret)
    684  1.1  riastrad 		return ret;
    685  1.1  riastrad 	if (repeater_present)
    686  1.1  riastrad 		I915_WRITE(HDCP_REP_CTL,
    687  1.1  riastrad 			   intel_hdcp_get_repeater_ctl(dev_priv, cpu_transcoder,
    688  1.1  riastrad 						       port));
    689  1.1  riastrad 
    690  1.1  riastrad 	ret = shim->toggle_signalling(intel_dig_port, true);
    691  1.1  riastrad 	if (ret)
    692  1.1  riastrad 		return ret;
    693  1.1  riastrad 
    694  1.1  riastrad 	I915_WRITE(HDCP_CONF(dev_priv, cpu_transcoder, port),
    695  1.1  riastrad 		   HDCP_CONF_AUTH_AND_ENC);
    696  1.1  riastrad 
    697  1.1  riastrad 	/* Wait for R0 ready */
    698  1.1  riastrad 	if (wait_for(I915_READ(HDCP_STATUS(dev_priv, cpu_transcoder, port)) &
    699  1.1  riastrad 		     (HDCP_STATUS_R0_READY | HDCP_STATUS_ENC), 1)) {
    700  1.1  riastrad 		DRM_ERROR("Timed out waiting for R0 ready\n");
    701  1.1  riastrad 		return -ETIMEDOUT;
    702  1.1  riastrad 	}
    703  1.1  riastrad 
    704  1.1  riastrad 	/*
    705  1.1  riastrad 	 * Wait for R0' to become available. The spec says 100ms from Aksv, but
    706  1.1  riastrad 	 * some monitors can take longer than this. We'll set the timeout at
    707  1.1  riastrad 	 * 300ms just to be sure.
    708  1.1  riastrad 	 *
    709  1.1  riastrad 	 * On DP, there's an R0_READY bit available but no such bit
    710  1.1  riastrad 	 * exists on HDMI. Since the upper-bound is the same, we'll just do
    711  1.1  riastrad 	 * the stupid thing instead of polling on one and not the other.
    712  1.1  riastrad 	 */
    713  1.1  riastrad 	wait_remaining_ms_from_jiffies(r0_prime_gen_start, 300);
    714  1.1  riastrad 
    715  1.1  riastrad 	tries = 3;
    716  1.1  riastrad 
    717  1.1  riastrad 	/*
    718  1.1  riastrad 	 * DP HDCP Spec mandates the two more reattempt to read R0, incase
    719  1.1  riastrad 	 * of R0 mismatch.
    720  1.1  riastrad 	 */
    721  1.1  riastrad 	for (i = 0; i < tries; i++) {
    722  1.1  riastrad 		ri.reg = 0;
    723  1.1  riastrad 		ret = shim->read_ri_prime(intel_dig_port, ri.shim);
    724  1.1  riastrad 		if (ret)
    725  1.1  riastrad 			return ret;
    726  1.1  riastrad 		I915_WRITE(HDCP_RPRIME(dev_priv, cpu_transcoder, port), ri.reg);
    727  1.1  riastrad 
    728  1.1  riastrad 		/* Wait for Ri prime match */
    729  1.1  riastrad 		if (!wait_for(I915_READ(HDCP_STATUS(dev_priv, cpu_transcoder,
    730  1.1  riastrad 						    port)) &
    731  1.1  riastrad 		    (HDCP_STATUS_RI_MATCH | HDCP_STATUS_ENC), 1))
    732  1.1  riastrad 			break;
    733  1.1  riastrad 	}
    734  1.1  riastrad 
    735  1.1  riastrad 	if (i == tries) {
    736  1.1  riastrad 		DRM_DEBUG_KMS("Timed out waiting for Ri prime match (%x)\n",
    737  1.1  riastrad 			      I915_READ(HDCP_STATUS(dev_priv, cpu_transcoder,
    738  1.1  riastrad 						    port)));
    739  1.1  riastrad 		return -ETIMEDOUT;
    740  1.1  riastrad 	}
    741  1.1  riastrad 
    742  1.1  riastrad 	/* Wait for encryption confirmation */
    743  1.1  riastrad 	if (intel_de_wait_for_set(dev_priv,
    744  1.1  riastrad 				  HDCP_STATUS(dev_priv, cpu_transcoder, port),
    745  1.1  riastrad 				  HDCP_STATUS_ENC,
    746  1.1  riastrad 				  ENCRYPT_STATUS_CHANGE_TIMEOUT_MS)) {
    747  1.1  riastrad 		DRM_ERROR("Timed out waiting for encryption\n");
    748  1.1  riastrad 		return -ETIMEDOUT;
    749  1.1  riastrad 	}
    750  1.1  riastrad 
    751  1.1  riastrad 	/*
    752  1.1  riastrad 	 * XXX: If we have MST-connected devices, we need to enable encryption
    753  1.1  riastrad 	 * on those as well.
    754  1.1  riastrad 	 */
    755  1.1  riastrad 
    756  1.1  riastrad 	if (repeater_present)
    757  1.1  riastrad 		return intel_hdcp_auth_downstream(connector);
    758  1.1  riastrad 
    759  1.1  riastrad 	DRM_DEBUG_KMS("HDCP is enabled (no repeater present)\n");
    760  1.1  riastrad 	return 0;
    761  1.1  riastrad }
    762  1.1  riastrad 
    763  1.1  riastrad static int _intel_hdcp_disable(struct intel_connector *connector)
    764  1.1  riastrad {
    765  1.1  riastrad 	struct intel_hdcp *hdcp = &connector->hdcp;
    766  1.1  riastrad 	struct drm_i915_private *dev_priv = connector->base.dev->dev_private;
    767  1.1  riastrad 	struct intel_digital_port *intel_dig_port = conn_to_dig_port(connector);
    768  1.1  riastrad 	enum port port = intel_dig_port->base.port;
    769  1.1  riastrad 	enum transcoder cpu_transcoder = hdcp->cpu_transcoder;
    770  1.1  riastrad 	int ret;
    771  1.1  riastrad 
    772  1.1  riastrad 	DRM_DEBUG_KMS("[%s:%d] HDCP is being disabled...\n",
    773  1.1  riastrad 		      connector->base.name, connector->base.base.id);
    774  1.1  riastrad 
    775  1.1  riastrad 	hdcp->hdcp_encrypted = false;
    776  1.1  riastrad 	I915_WRITE(HDCP_CONF(dev_priv, cpu_transcoder, port), 0);
    777  1.1  riastrad 	if (intel_de_wait_for_clear(dev_priv,
    778  1.1  riastrad 				    HDCP_STATUS(dev_priv, cpu_transcoder, port),
    779  1.1  riastrad 				    ~0, ENCRYPT_STATUS_CHANGE_TIMEOUT_MS)) {
    780  1.1  riastrad 		DRM_ERROR("Failed to disable HDCP, timeout clearing status\n");
    781  1.1  riastrad 		return -ETIMEDOUT;
    782  1.1  riastrad 	}
    783  1.1  riastrad 
    784  1.1  riastrad 	ret = hdcp->shim->toggle_signalling(intel_dig_port, false);
    785  1.1  riastrad 	if (ret) {
    786  1.1  riastrad 		DRM_ERROR("Failed to disable HDCP signalling\n");
    787  1.1  riastrad 		return ret;
    788  1.1  riastrad 	}
    789  1.1  riastrad 
    790  1.1  riastrad 	DRM_DEBUG_KMS("HDCP is disabled\n");
    791  1.1  riastrad 	return 0;
    792  1.1  riastrad }
    793  1.1  riastrad 
    794  1.1  riastrad static int _intel_hdcp_enable(struct intel_connector *connector)
    795  1.1  riastrad {
    796  1.1  riastrad 	struct intel_hdcp *hdcp = &connector->hdcp;
    797  1.1  riastrad 	struct drm_i915_private *dev_priv = connector->base.dev->dev_private;
    798  1.1  riastrad 	int i, ret, tries = 3;
    799  1.1  riastrad 
    800  1.1  riastrad 	DRM_DEBUG_KMS("[%s:%d] HDCP is being enabled...\n",
    801  1.1  riastrad 		      connector->base.name, connector->base.base.id);
    802  1.1  riastrad 
    803  1.1  riastrad 	if (!hdcp_key_loadable(dev_priv)) {
    804  1.1  riastrad 		DRM_ERROR("HDCP key Load is not possible\n");
    805  1.1  riastrad 		return -ENXIO;
    806  1.1  riastrad 	}
    807  1.1  riastrad 
    808  1.1  riastrad 	for (i = 0; i < KEY_LOAD_TRIES; i++) {
    809  1.1  riastrad 		ret = intel_hdcp_load_keys(dev_priv);
    810  1.1  riastrad 		if (!ret)
    811  1.1  riastrad 			break;
    812  1.1  riastrad 		intel_hdcp_clear_keys(dev_priv);
    813  1.1  riastrad 	}
    814  1.1  riastrad 	if (ret) {
    815  1.1  riastrad 		DRM_ERROR("Could not load HDCP keys, (%d)\n", ret);
    816  1.1  riastrad 		return ret;
    817  1.1  riastrad 	}
    818  1.1  riastrad 
    819  1.1  riastrad 	/* Incase of authentication failures, HDCP spec expects reauth. */
    820  1.1  riastrad 	for (i = 0; i < tries; i++) {
    821  1.1  riastrad 		ret = intel_hdcp_auth(connector);
    822  1.1  riastrad 		if (!ret) {
    823  1.1  riastrad 			hdcp->hdcp_encrypted = true;
    824  1.1  riastrad 			return 0;
    825  1.1  riastrad 		}
    826  1.1  riastrad 
    827  1.1  riastrad 		DRM_DEBUG_KMS("HDCP Auth failure (%d)\n", ret);
    828  1.1  riastrad 
    829  1.1  riastrad 		/* Ensuring HDCP encryption and signalling are stopped. */
    830  1.1  riastrad 		_intel_hdcp_disable(connector);
    831  1.1  riastrad 	}
    832  1.1  riastrad 
    833  1.1  riastrad 	DRM_DEBUG_KMS("HDCP authentication failed (%d tries/%d)\n", tries, ret);
    834  1.1  riastrad 	return ret;
    835  1.1  riastrad }
    836  1.1  riastrad 
    837  1.1  riastrad static inline
    838  1.1  riastrad struct intel_connector *intel_hdcp_to_connector(struct intel_hdcp *hdcp)
    839  1.1  riastrad {
    840  1.1  riastrad 	return container_of(hdcp, struct intel_connector, hdcp);
    841  1.1  riastrad }
    842  1.1  riastrad 
    843  1.1  riastrad /* Implements Part 3 of the HDCP authorization procedure */
    844  1.1  riastrad static int intel_hdcp_check_link(struct intel_connector *connector)
    845  1.1  riastrad {
    846  1.1  riastrad 	struct intel_hdcp *hdcp = &connector->hdcp;
    847  1.1  riastrad 	struct drm_i915_private *dev_priv = connector->base.dev->dev_private;
    848  1.1  riastrad 	struct intel_digital_port *intel_dig_port = conn_to_dig_port(connector);
    849  1.1  riastrad 	enum port port = intel_dig_port->base.port;
    850  1.1  riastrad 	enum transcoder cpu_transcoder;
    851  1.1  riastrad 	int ret = 0;
    852  1.1  riastrad 
    853  1.1  riastrad 	mutex_lock(&hdcp->mutex);
    854  1.1  riastrad 	cpu_transcoder = hdcp->cpu_transcoder;
    855  1.1  riastrad 
    856  1.1  riastrad 	/* Check_link valid only when HDCP1.4 is enabled */
    857  1.1  riastrad 	if (hdcp->value != DRM_MODE_CONTENT_PROTECTION_ENABLED ||
    858  1.1  riastrad 	    !hdcp->hdcp_encrypted) {
    859  1.1  riastrad 		ret = -EINVAL;
    860  1.1  riastrad 		goto out;
    861  1.1  riastrad 	}
    862  1.1  riastrad 
    863  1.1  riastrad 	if (WARN_ON(!intel_hdcp_in_use(dev_priv, cpu_transcoder, port))) {
    864  1.1  riastrad 		DRM_ERROR("%s:%d HDCP link stopped encryption,%x\n",
    865  1.1  riastrad 			  connector->base.name, connector->base.base.id,
    866  1.1  riastrad 			  I915_READ(HDCP_STATUS(dev_priv, cpu_transcoder,
    867  1.1  riastrad 						port)));
    868  1.1  riastrad 		ret = -ENXIO;
    869  1.1  riastrad 		hdcp->value = DRM_MODE_CONTENT_PROTECTION_DESIRED;
    870  1.1  riastrad 		schedule_work(&hdcp->prop_work);
    871  1.1  riastrad 		goto out;
    872  1.1  riastrad 	}
    873  1.1  riastrad 
    874  1.1  riastrad 	if (hdcp->shim->check_link(intel_dig_port)) {
    875  1.1  riastrad 		if (hdcp->value != DRM_MODE_CONTENT_PROTECTION_UNDESIRED) {
    876  1.1  riastrad 			hdcp->value = DRM_MODE_CONTENT_PROTECTION_ENABLED;
    877  1.1  riastrad 			schedule_work(&hdcp->prop_work);
    878  1.1  riastrad 		}
    879  1.1  riastrad 		goto out;
    880  1.1  riastrad 	}
    881  1.1  riastrad 
    882  1.1  riastrad 	DRM_DEBUG_KMS("[%s:%d] HDCP link failed, retrying authentication\n",
    883  1.1  riastrad 		      connector->base.name, connector->base.base.id);
    884  1.1  riastrad 
    885  1.1  riastrad 	ret = _intel_hdcp_disable(connector);
    886  1.1  riastrad 	if (ret) {
    887  1.1  riastrad 		DRM_ERROR("Failed to disable hdcp (%d)\n", ret);
    888  1.1  riastrad 		hdcp->value = DRM_MODE_CONTENT_PROTECTION_DESIRED;
    889  1.1  riastrad 		schedule_work(&hdcp->prop_work);
    890  1.1  riastrad 		goto out;
    891  1.1  riastrad 	}
    892  1.1  riastrad 
    893  1.1  riastrad 	ret = _intel_hdcp_enable(connector);
    894  1.1  riastrad 	if (ret) {
    895  1.1  riastrad 		DRM_ERROR("Failed to enable hdcp (%d)\n", ret);
    896  1.1  riastrad 		hdcp->value = DRM_MODE_CONTENT_PROTECTION_DESIRED;
    897  1.1  riastrad 		schedule_work(&hdcp->prop_work);
    898  1.1  riastrad 		goto out;
    899  1.1  riastrad 	}
    900  1.1  riastrad 
    901  1.1  riastrad out:
    902  1.1  riastrad 	mutex_unlock(&hdcp->mutex);
    903  1.1  riastrad 	return ret;
    904  1.1  riastrad }
    905  1.1  riastrad 
    906  1.1  riastrad static void intel_hdcp_prop_work(struct work_struct *work)
    907  1.1  riastrad {
    908  1.1  riastrad 	struct intel_hdcp *hdcp = container_of(work, struct intel_hdcp,
    909  1.1  riastrad 					       prop_work);
    910  1.1  riastrad 	struct intel_connector *connector = intel_hdcp_to_connector(hdcp);
    911  1.1  riastrad 	struct drm_device *dev = connector->base.dev;
    912  1.1  riastrad 
    913  1.1  riastrad 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
    914  1.1  riastrad 	mutex_lock(&hdcp->mutex);
    915  1.1  riastrad 
    916  1.1  riastrad 	/*
    917  1.1  riastrad 	 * This worker is only used to flip between ENABLED/DESIRED. Either of
    918  1.1  riastrad 	 * those to UNDESIRED is handled by core. If value == UNDESIRED,
    919  1.1  riastrad 	 * we're running just after hdcp has been disabled, so just exit
    920  1.1  riastrad 	 */
    921  1.1  riastrad 	if (hdcp->value != DRM_MODE_CONTENT_PROTECTION_UNDESIRED)
    922  1.1  riastrad 		drm_hdcp_update_content_protection(&connector->base,
    923  1.1  riastrad 						   hdcp->value);
    924  1.1  riastrad 
    925  1.1  riastrad 	mutex_unlock(&hdcp->mutex);
    926  1.1  riastrad 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
    927  1.1  riastrad }
    928  1.1  riastrad 
    929  1.1  riastrad bool is_hdcp_supported(struct drm_i915_private *dev_priv, enum port port)
    930  1.1  riastrad {
    931  1.1  riastrad 	/* PORT E doesn't have HDCP, and PORT F is disabled */
    932  1.1  riastrad 	return INTEL_INFO(dev_priv)->display.has_hdcp && port < PORT_E;
    933  1.1  riastrad }
    934  1.1  riastrad 
    935  1.1  riastrad static int
    936  1.1  riastrad hdcp2_prepare_ake_init(struct intel_connector *connector,
    937  1.1  riastrad 		       struct hdcp2_ake_init *ake_data)
    938  1.1  riastrad {
    939  1.1  riastrad 	struct hdcp_port_data *data = &connector->hdcp.port_data;
    940  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
    941  1.1  riastrad 	struct i915_hdcp_comp_master *comp;
    942  1.1  riastrad 	int ret;
    943  1.1  riastrad 
    944  1.1  riastrad 	mutex_lock(&dev_priv->hdcp_comp_mutex);
    945  1.1  riastrad 	comp = dev_priv->hdcp_master;
    946  1.1  riastrad 
    947  1.1  riastrad 	if (!comp || !comp->ops) {
    948  1.1  riastrad 		mutex_unlock(&dev_priv->hdcp_comp_mutex);
    949  1.1  riastrad 		return -EINVAL;
    950  1.1  riastrad 	}
    951  1.1  riastrad 
    952  1.1  riastrad 	ret = comp->ops->initiate_hdcp2_session(comp->mei_dev, data, ake_data);
    953  1.1  riastrad 	if (ret)
    954  1.1  riastrad 		DRM_DEBUG_KMS("Prepare_ake_init failed. %d\n", ret);
    955  1.1  riastrad 	mutex_unlock(&dev_priv->hdcp_comp_mutex);
    956  1.1  riastrad 
    957  1.1  riastrad 	return ret;
    958  1.1  riastrad }
    959  1.1  riastrad 
    960  1.1  riastrad static int
    961  1.1  riastrad hdcp2_verify_rx_cert_prepare_km(struct intel_connector *connector,
    962  1.1  riastrad 				struct hdcp2_ake_send_cert *rx_cert,
    963  1.1  riastrad 				bool *paired,
    964  1.1  riastrad 				struct hdcp2_ake_no_stored_km *ek_pub_km,
    965  1.1  riastrad 				size_t *msg_sz)
    966  1.1  riastrad {
    967  1.1  riastrad 	struct hdcp_port_data *data = &connector->hdcp.port_data;
    968  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
    969  1.1  riastrad 	struct i915_hdcp_comp_master *comp;
    970  1.1  riastrad 	int ret;
    971  1.1  riastrad 
    972  1.1  riastrad 	mutex_lock(&dev_priv->hdcp_comp_mutex);
    973  1.1  riastrad 	comp = dev_priv->hdcp_master;
    974  1.1  riastrad 
    975  1.1  riastrad 	if (!comp || !comp->ops) {
    976  1.1  riastrad 		mutex_unlock(&dev_priv->hdcp_comp_mutex);
    977  1.1  riastrad 		return -EINVAL;
    978  1.1  riastrad 	}
    979  1.1  riastrad 
    980  1.1  riastrad 	ret = comp->ops->verify_receiver_cert_prepare_km(comp->mei_dev, data,
    981  1.1  riastrad 							 rx_cert, paired,
    982  1.1  riastrad 							 ek_pub_km, msg_sz);
    983  1.1  riastrad 	if (ret < 0)
    984  1.1  riastrad 		DRM_DEBUG_KMS("Verify rx_cert failed. %d\n", ret);
    985  1.1  riastrad 	mutex_unlock(&dev_priv->hdcp_comp_mutex);
    986  1.1  riastrad 
    987  1.1  riastrad 	return ret;
    988  1.1  riastrad }
    989  1.1  riastrad 
    990  1.1  riastrad static int hdcp2_verify_hprime(struct intel_connector *connector,
    991  1.1  riastrad 			       struct hdcp2_ake_send_hprime *rx_hprime)
    992  1.1  riastrad {
    993  1.1  riastrad 	struct hdcp_port_data *data = &connector->hdcp.port_data;
    994  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
    995  1.1  riastrad 	struct i915_hdcp_comp_master *comp;
    996  1.1  riastrad 	int ret;
    997  1.1  riastrad 
    998  1.1  riastrad 	mutex_lock(&dev_priv->hdcp_comp_mutex);
    999  1.1  riastrad 	comp = dev_priv->hdcp_master;
   1000  1.1  riastrad 
   1001  1.1  riastrad 	if (!comp || !comp->ops) {
   1002  1.1  riastrad 		mutex_unlock(&dev_priv->hdcp_comp_mutex);
   1003  1.1  riastrad 		return -EINVAL;
   1004  1.1  riastrad 	}
   1005  1.1  riastrad 
   1006  1.1  riastrad 	ret = comp->ops->verify_hprime(comp->mei_dev, data, rx_hprime);
   1007  1.1  riastrad 	if (ret < 0)
   1008  1.1  riastrad 		DRM_DEBUG_KMS("Verify hprime failed. %d\n", ret);
   1009  1.1  riastrad 	mutex_unlock(&dev_priv->hdcp_comp_mutex);
   1010  1.1  riastrad 
   1011  1.1  riastrad 	return ret;
   1012  1.1  riastrad }
   1013  1.1  riastrad 
   1014  1.1  riastrad static int
   1015  1.1  riastrad hdcp2_store_pairing_info(struct intel_connector *connector,
   1016  1.1  riastrad 			 struct hdcp2_ake_send_pairing_info *pairing_info)
   1017  1.1  riastrad {
   1018  1.1  riastrad 	struct hdcp_port_data *data = &connector->hdcp.port_data;
   1019  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1020  1.1  riastrad 	struct i915_hdcp_comp_master *comp;
   1021  1.1  riastrad 	int ret;
   1022  1.1  riastrad 
   1023  1.1  riastrad 	mutex_lock(&dev_priv->hdcp_comp_mutex);
   1024  1.1  riastrad 	comp = dev_priv->hdcp_master;
   1025  1.1  riastrad 
   1026  1.1  riastrad 	if (!comp || !comp->ops) {
   1027  1.1  riastrad 		mutex_unlock(&dev_priv->hdcp_comp_mutex);
   1028  1.1  riastrad 		return -EINVAL;
   1029  1.1  riastrad 	}
   1030  1.1  riastrad 
   1031  1.1  riastrad 	ret = comp->ops->store_pairing_info(comp->mei_dev, data, pairing_info);
   1032  1.1  riastrad 	if (ret < 0)
   1033  1.1  riastrad 		DRM_DEBUG_KMS("Store pairing info failed. %d\n", ret);
   1034  1.1  riastrad 	mutex_unlock(&dev_priv->hdcp_comp_mutex);
   1035  1.1  riastrad 
   1036  1.1  riastrad 	return ret;
   1037  1.1  riastrad }
   1038  1.1  riastrad 
   1039  1.1  riastrad static int
   1040  1.1  riastrad hdcp2_prepare_lc_init(struct intel_connector *connector,
   1041  1.1  riastrad 		      struct hdcp2_lc_init *lc_init)
   1042  1.1  riastrad {
   1043  1.1  riastrad 	struct hdcp_port_data *data = &connector->hdcp.port_data;
   1044  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1045  1.1  riastrad 	struct i915_hdcp_comp_master *comp;
   1046  1.1  riastrad 	int ret;
   1047  1.1  riastrad 
   1048  1.1  riastrad 	mutex_lock(&dev_priv->hdcp_comp_mutex);
   1049  1.1  riastrad 	comp = dev_priv->hdcp_master;
   1050  1.1  riastrad 
   1051  1.1  riastrad 	if (!comp || !comp->ops) {
   1052  1.1  riastrad 		mutex_unlock(&dev_priv->hdcp_comp_mutex);
   1053  1.1  riastrad 		return -EINVAL;
   1054  1.1  riastrad 	}
   1055  1.1  riastrad 
   1056  1.1  riastrad 	ret = comp->ops->initiate_locality_check(comp->mei_dev, data, lc_init);
   1057  1.1  riastrad 	if (ret < 0)
   1058  1.1  riastrad 		DRM_DEBUG_KMS("Prepare lc_init failed. %d\n", ret);
   1059  1.1  riastrad 	mutex_unlock(&dev_priv->hdcp_comp_mutex);
   1060  1.1  riastrad 
   1061  1.1  riastrad 	return ret;
   1062  1.1  riastrad }
   1063  1.1  riastrad 
   1064  1.1  riastrad static int
   1065  1.1  riastrad hdcp2_verify_lprime(struct intel_connector *connector,
   1066  1.1  riastrad 		    struct hdcp2_lc_send_lprime *rx_lprime)
   1067  1.1  riastrad {
   1068  1.1  riastrad 	struct hdcp_port_data *data = &connector->hdcp.port_data;
   1069  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1070  1.1  riastrad 	struct i915_hdcp_comp_master *comp;
   1071  1.1  riastrad 	int ret;
   1072  1.1  riastrad 
   1073  1.1  riastrad 	mutex_lock(&dev_priv->hdcp_comp_mutex);
   1074  1.1  riastrad 	comp = dev_priv->hdcp_master;
   1075  1.1  riastrad 
   1076  1.1  riastrad 	if (!comp || !comp->ops) {
   1077  1.1  riastrad 		mutex_unlock(&dev_priv->hdcp_comp_mutex);
   1078  1.1  riastrad 		return -EINVAL;
   1079  1.1  riastrad 	}
   1080  1.1  riastrad 
   1081  1.1  riastrad 	ret = comp->ops->verify_lprime(comp->mei_dev, data, rx_lprime);
   1082  1.1  riastrad 	if (ret < 0)
   1083  1.1  riastrad 		DRM_DEBUG_KMS("Verify L_Prime failed. %d\n", ret);
   1084  1.1  riastrad 	mutex_unlock(&dev_priv->hdcp_comp_mutex);
   1085  1.1  riastrad 
   1086  1.1  riastrad 	return ret;
   1087  1.1  riastrad }
   1088  1.1  riastrad 
   1089  1.1  riastrad static int hdcp2_prepare_skey(struct intel_connector *connector,
   1090  1.1  riastrad 			      struct hdcp2_ske_send_eks *ske_data)
   1091  1.1  riastrad {
   1092  1.1  riastrad 	struct hdcp_port_data *data = &connector->hdcp.port_data;
   1093  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1094  1.1  riastrad 	struct i915_hdcp_comp_master *comp;
   1095  1.1  riastrad 	int ret;
   1096  1.1  riastrad 
   1097  1.1  riastrad 	mutex_lock(&dev_priv->hdcp_comp_mutex);
   1098  1.1  riastrad 	comp = dev_priv->hdcp_master;
   1099  1.1  riastrad 
   1100  1.1  riastrad 	if (!comp || !comp->ops) {
   1101  1.1  riastrad 		mutex_unlock(&dev_priv->hdcp_comp_mutex);
   1102  1.1  riastrad 		return -EINVAL;
   1103  1.1  riastrad 	}
   1104  1.1  riastrad 
   1105  1.1  riastrad 	ret = comp->ops->get_session_key(comp->mei_dev, data, ske_data);
   1106  1.1  riastrad 	if (ret < 0)
   1107  1.1  riastrad 		DRM_DEBUG_KMS("Get session key failed. %d\n", ret);
   1108  1.1  riastrad 	mutex_unlock(&dev_priv->hdcp_comp_mutex);
   1109  1.1  riastrad 
   1110  1.1  riastrad 	return ret;
   1111  1.1  riastrad }
   1112  1.1  riastrad 
   1113  1.1  riastrad static int
   1114  1.1  riastrad hdcp2_verify_rep_topology_prepare_ack(struct intel_connector *connector,
   1115  1.1  riastrad 				      struct hdcp2_rep_send_receiverid_list
   1116  1.1  riastrad 								*rep_topology,
   1117  1.1  riastrad 				      struct hdcp2_rep_send_ack *rep_send_ack)
   1118  1.1  riastrad {
   1119  1.1  riastrad 	struct hdcp_port_data *data = &connector->hdcp.port_data;
   1120  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1121  1.1  riastrad 	struct i915_hdcp_comp_master *comp;
   1122  1.1  riastrad 	int ret;
   1123  1.1  riastrad 
   1124  1.1  riastrad 	mutex_lock(&dev_priv->hdcp_comp_mutex);
   1125  1.1  riastrad 	comp = dev_priv->hdcp_master;
   1126  1.1  riastrad 
   1127  1.1  riastrad 	if (!comp || !comp->ops) {
   1128  1.1  riastrad 		mutex_unlock(&dev_priv->hdcp_comp_mutex);
   1129  1.1  riastrad 		return -EINVAL;
   1130  1.1  riastrad 	}
   1131  1.1  riastrad 
   1132  1.1  riastrad 	ret = comp->ops->repeater_check_flow_prepare_ack(comp->mei_dev, data,
   1133  1.1  riastrad 							 rep_topology,
   1134  1.1  riastrad 							 rep_send_ack);
   1135  1.1  riastrad 	if (ret < 0)
   1136  1.1  riastrad 		DRM_DEBUG_KMS("Verify rep topology failed. %d\n", ret);
   1137  1.1  riastrad 	mutex_unlock(&dev_priv->hdcp_comp_mutex);
   1138  1.1  riastrad 
   1139  1.1  riastrad 	return ret;
   1140  1.1  riastrad }
   1141  1.1  riastrad 
   1142  1.1  riastrad static int
   1143  1.1  riastrad hdcp2_verify_mprime(struct intel_connector *connector,
   1144  1.1  riastrad 		    struct hdcp2_rep_stream_ready *stream_ready)
   1145  1.1  riastrad {
   1146  1.1  riastrad 	struct hdcp_port_data *data = &connector->hdcp.port_data;
   1147  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1148  1.1  riastrad 	struct i915_hdcp_comp_master *comp;
   1149  1.1  riastrad 	int ret;
   1150  1.1  riastrad 
   1151  1.1  riastrad 	mutex_lock(&dev_priv->hdcp_comp_mutex);
   1152  1.1  riastrad 	comp = dev_priv->hdcp_master;
   1153  1.1  riastrad 
   1154  1.1  riastrad 	if (!comp || !comp->ops) {
   1155  1.1  riastrad 		mutex_unlock(&dev_priv->hdcp_comp_mutex);
   1156  1.1  riastrad 		return -EINVAL;
   1157  1.1  riastrad 	}
   1158  1.1  riastrad 
   1159  1.1  riastrad 	ret = comp->ops->verify_mprime(comp->mei_dev, data, stream_ready);
   1160  1.1  riastrad 	if (ret < 0)
   1161  1.1  riastrad 		DRM_DEBUG_KMS("Verify mprime failed. %d\n", ret);
   1162  1.1  riastrad 	mutex_unlock(&dev_priv->hdcp_comp_mutex);
   1163  1.1  riastrad 
   1164  1.1  riastrad 	return ret;
   1165  1.1  riastrad }
   1166  1.1  riastrad 
   1167  1.1  riastrad static int hdcp2_authenticate_port(struct intel_connector *connector)
   1168  1.1  riastrad {
   1169  1.1  riastrad 	struct hdcp_port_data *data = &connector->hdcp.port_data;
   1170  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1171  1.1  riastrad 	struct i915_hdcp_comp_master *comp;
   1172  1.1  riastrad 	int ret;
   1173  1.1  riastrad 
   1174  1.1  riastrad 	mutex_lock(&dev_priv->hdcp_comp_mutex);
   1175  1.1  riastrad 	comp = dev_priv->hdcp_master;
   1176  1.1  riastrad 
   1177  1.1  riastrad 	if (!comp || !comp->ops) {
   1178  1.1  riastrad 		mutex_unlock(&dev_priv->hdcp_comp_mutex);
   1179  1.1  riastrad 		return -EINVAL;
   1180  1.1  riastrad 	}
   1181  1.1  riastrad 
   1182  1.1  riastrad 	ret = comp->ops->enable_hdcp_authentication(comp->mei_dev, data);
   1183  1.1  riastrad 	if (ret < 0)
   1184  1.1  riastrad 		DRM_DEBUG_KMS("Enable hdcp auth failed. %d\n", ret);
   1185  1.1  riastrad 	mutex_unlock(&dev_priv->hdcp_comp_mutex);
   1186  1.1  riastrad 
   1187  1.1  riastrad 	return ret;
   1188  1.1  riastrad }
   1189  1.1  riastrad 
   1190  1.1  riastrad static int hdcp2_close_mei_session(struct intel_connector *connector)
   1191  1.1  riastrad {
   1192  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1193  1.1  riastrad 	struct i915_hdcp_comp_master *comp;
   1194  1.1  riastrad 	int ret;
   1195  1.1  riastrad 
   1196  1.1  riastrad 	mutex_lock(&dev_priv->hdcp_comp_mutex);
   1197  1.1  riastrad 	comp = dev_priv->hdcp_master;
   1198  1.1  riastrad 
   1199  1.1  riastrad 	if (!comp || !comp->ops) {
   1200  1.1  riastrad 		mutex_unlock(&dev_priv->hdcp_comp_mutex);
   1201  1.1  riastrad 		return -EINVAL;
   1202  1.1  riastrad 	}
   1203  1.1  riastrad 
   1204  1.1  riastrad 	ret = comp->ops->close_hdcp_session(comp->mei_dev,
   1205  1.1  riastrad 					     &connector->hdcp.port_data);
   1206  1.1  riastrad 	mutex_unlock(&dev_priv->hdcp_comp_mutex);
   1207  1.1  riastrad 
   1208  1.1  riastrad 	return ret;
   1209  1.1  riastrad }
   1210  1.1  riastrad 
   1211  1.1  riastrad static int hdcp2_deauthenticate_port(struct intel_connector *connector)
   1212  1.1  riastrad {
   1213  1.1  riastrad 	return hdcp2_close_mei_session(connector);
   1214  1.1  riastrad }
   1215  1.1  riastrad 
   1216  1.1  riastrad /* Authentication flow starts from here */
   1217  1.1  riastrad static int hdcp2_authentication_key_exchange(struct intel_connector *connector)
   1218  1.1  riastrad {
   1219  1.1  riastrad 	struct intel_digital_port *intel_dig_port = conn_to_dig_port(connector);
   1220  1.1  riastrad 	struct intel_hdcp *hdcp = &connector->hdcp;
   1221  1.1  riastrad 	struct drm_device *dev = connector->base.dev;
   1222  1.1  riastrad 	union {
   1223  1.1  riastrad 		struct hdcp2_ake_init ake_init;
   1224  1.1  riastrad 		struct hdcp2_ake_send_cert send_cert;
   1225  1.1  riastrad 		struct hdcp2_ake_no_stored_km no_stored_km;
   1226  1.1  riastrad 		struct hdcp2_ake_send_hprime send_hprime;
   1227  1.1  riastrad 		struct hdcp2_ake_send_pairing_info pairing_info;
   1228  1.1  riastrad 	} msgs;
   1229  1.1  riastrad 	const struct intel_hdcp_shim *shim = hdcp->shim;
   1230  1.1  riastrad 	size_t size;
   1231  1.1  riastrad 	int ret;
   1232  1.1  riastrad 
   1233  1.1  riastrad 	/* Init for seq_num */
   1234  1.1  riastrad 	hdcp->seq_num_v = 0;
   1235  1.1  riastrad 	hdcp->seq_num_m = 0;
   1236  1.1  riastrad 
   1237  1.1  riastrad 	ret = hdcp2_prepare_ake_init(connector, &msgs.ake_init);
   1238  1.1  riastrad 	if (ret < 0)
   1239  1.1  riastrad 		return ret;
   1240  1.1  riastrad 
   1241  1.1  riastrad 	ret = shim->write_2_2_msg(intel_dig_port, &msgs.ake_init,
   1242  1.1  riastrad 				  sizeof(msgs.ake_init));
   1243  1.1  riastrad 	if (ret < 0)
   1244  1.1  riastrad 		return ret;
   1245  1.1  riastrad 
   1246  1.1  riastrad 	ret = shim->read_2_2_msg(intel_dig_port, HDCP_2_2_AKE_SEND_CERT,
   1247  1.1  riastrad 				 &msgs.send_cert, sizeof(msgs.send_cert));
   1248  1.1  riastrad 	if (ret < 0)
   1249  1.1  riastrad 		return ret;
   1250  1.1  riastrad 
   1251  1.1  riastrad 	if (msgs.send_cert.rx_caps[0] != HDCP_2_2_RX_CAPS_VERSION_VAL) {
   1252  1.1  riastrad 		DRM_DEBUG_KMS("cert.rx_caps dont claim HDCP2.2\n");
   1253  1.1  riastrad 		return -EINVAL;
   1254  1.1  riastrad 	}
   1255  1.1  riastrad 
   1256  1.1  riastrad 	hdcp->is_repeater = HDCP_2_2_RX_REPEATER(msgs.send_cert.rx_caps[2]);
   1257  1.1  riastrad 
   1258  1.1  riastrad 	if (drm_hdcp_check_ksvs_revoked(dev, msgs.send_cert.cert_rx.receiver_id,
   1259  1.1  riastrad 					1)) {
   1260  1.1  riastrad 		DRM_ERROR("Receiver ID is revoked\n");
   1261  1.1  riastrad 		return -EPERM;
   1262  1.1  riastrad 	}
   1263  1.1  riastrad 
   1264  1.1  riastrad 	/*
   1265  1.1  riastrad 	 * Here msgs.no_stored_km will hold msgs corresponding to the km
   1266  1.1  riastrad 	 * stored also.
   1267  1.1  riastrad 	 */
   1268  1.1  riastrad 	ret = hdcp2_verify_rx_cert_prepare_km(connector, &msgs.send_cert,
   1269  1.1  riastrad 					      &hdcp->is_paired,
   1270  1.1  riastrad 					      &msgs.no_stored_km, &size);
   1271  1.1  riastrad 	if (ret < 0)
   1272  1.1  riastrad 		return ret;
   1273  1.1  riastrad 
   1274  1.1  riastrad 	ret = shim->write_2_2_msg(intel_dig_port, &msgs.no_stored_km, size);
   1275  1.1  riastrad 	if (ret < 0)
   1276  1.1  riastrad 		return ret;
   1277  1.1  riastrad 
   1278  1.1  riastrad 	ret = shim->read_2_2_msg(intel_dig_port, HDCP_2_2_AKE_SEND_HPRIME,
   1279  1.1  riastrad 				 &msgs.send_hprime, sizeof(msgs.send_hprime));
   1280  1.1  riastrad 	if (ret < 0)
   1281  1.1  riastrad 		return ret;
   1282  1.1  riastrad 
   1283  1.1  riastrad 	ret = hdcp2_verify_hprime(connector, &msgs.send_hprime);
   1284  1.1  riastrad 	if (ret < 0)
   1285  1.1  riastrad 		return ret;
   1286  1.1  riastrad 
   1287  1.1  riastrad 	if (!hdcp->is_paired) {
   1288  1.1  riastrad 		/* Pairing is required */
   1289  1.1  riastrad 		ret = shim->read_2_2_msg(intel_dig_port,
   1290  1.1  riastrad 					 HDCP_2_2_AKE_SEND_PAIRING_INFO,
   1291  1.1  riastrad 					 &msgs.pairing_info,
   1292  1.1  riastrad 					 sizeof(msgs.pairing_info));
   1293  1.1  riastrad 		if (ret < 0)
   1294  1.1  riastrad 			return ret;
   1295  1.1  riastrad 
   1296  1.1  riastrad 		ret = hdcp2_store_pairing_info(connector, &msgs.pairing_info);
   1297  1.1  riastrad 		if (ret < 0)
   1298  1.1  riastrad 			return ret;
   1299  1.1  riastrad 		hdcp->is_paired = true;
   1300  1.1  riastrad 	}
   1301  1.1  riastrad 
   1302  1.1  riastrad 	return 0;
   1303  1.1  riastrad }
   1304  1.1  riastrad 
   1305  1.1  riastrad static int hdcp2_locality_check(struct intel_connector *connector)
   1306  1.1  riastrad {
   1307  1.1  riastrad 	struct intel_digital_port *intel_dig_port = conn_to_dig_port(connector);
   1308  1.1  riastrad 	struct intel_hdcp *hdcp = &connector->hdcp;
   1309  1.1  riastrad 	union {
   1310  1.1  riastrad 		struct hdcp2_lc_init lc_init;
   1311  1.1  riastrad 		struct hdcp2_lc_send_lprime send_lprime;
   1312  1.1  riastrad 	} msgs;
   1313  1.1  riastrad 	const struct intel_hdcp_shim *shim = hdcp->shim;
   1314  1.1  riastrad 	int tries = HDCP2_LC_RETRY_CNT, ret, i;
   1315  1.1  riastrad 
   1316  1.1  riastrad 	for (i = 0; i < tries; i++) {
   1317  1.1  riastrad 		ret = hdcp2_prepare_lc_init(connector, &msgs.lc_init);
   1318  1.1  riastrad 		if (ret < 0)
   1319  1.1  riastrad 			continue;
   1320  1.1  riastrad 
   1321  1.1  riastrad 		ret = shim->write_2_2_msg(intel_dig_port, &msgs.lc_init,
   1322  1.1  riastrad 				      sizeof(msgs.lc_init));
   1323  1.1  riastrad 		if (ret < 0)
   1324  1.1  riastrad 			continue;
   1325  1.1  riastrad 
   1326  1.1  riastrad 		ret = shim->read_2_2_msg(intel_dig_port,
   1327  1.1  riastrad 					 HDCP_2_2_LC_SEND_LPRIME,
   1328  1.1  riastrad 					 &msgs.send_lprime,
   1329  1.1  riastrad 					 sizeof(msgs.send_lprime));
   1330  1.1  riastrad 		if (ret < 0)
   1331  1.1  riastrad 			continue;
   1332  1.1  riastrad 
   1333  1.1  riastrad 		ret = hdcp2_verify_lprime(connector, &msgs.send_lprime);
   1334  1.1  riastrad 		if (!ret)
   1335  1.1  riastrad 			break;
   1336  1.1  riastrad 	}
   1337  1.1  riastrad 
   1338  1.1  riastrad 	return ret;
   1339  1.1  riastrad }
   1340  1.1  riastrad 
   1341  1.1  riastrad static int hdcp2_session_key_exchange(struct intel_connector *connector)
   1342  1.1  riastrad {
   1343  1.1  riastrad 	struct intel_digital_port *intel_dig_port = conn_to_dig_port(connector);
   1344  1.1  riastrad 	struct intel_hdcp *hdcp = &connector->hdcp;
   1345  1.1  riastrad 	struct hdcp2_ske_send_eks send_eks;
   1346  1.1  riastrad 	int ret;
   1347  1.1  riastrad 
   1348  1.1  riastrad 	ret = hdcp2_prepare_skey(connector, &send_eks);
   1349  1.1  riastrad 	if (ret < 0)
   1350  1.1  riastrad 		return ret;
   1351  1.1  riastrad 
   1352  1.1  riastrad 	ret = hdcp->shim->write_2_2_msg(intel_dig_port, &send_eks,
   1353  1.1  riastrad 					sizeof(send_eks));
   1354  1.1  riastrad 	if (ret < 0)
   1355  1.1  riastrad 		return ret;
   1356  1.1  riastrad 
   1357  1.1  riastrad 	return 0;
   1358  1.1  riastrad }
   1359  1.1  riastrad 
   1360  1.1  riastrad static
   1361  1.1  riastrad int hdcp2_propagate_stream_management_info(struct intel_connector *connector)
   1362  1.1  riastrad {
   1363  1.1  riastrad 	struct intel_digital_port *intel_dig_port = conn_to_dig_port(connector);
   1364  1.1  riastrad 	struct intel_hdcp *hdcp = &connector->hdcp;
   1365  1.1  riastrad 	union {
   1366  1.1  riastrad 		struct hdcp2_rep_stream_manage stream_manage;
   1367  1.1  riastrad 		struct hdcp2_rep_stream_ready stream_ready;
   1368  1.1  riastrad 	} msgs;
   1369  1.1  riastrad 	const struct intel_hdcp_shim *shim = hdcp->shim;
   1370  1.1  riastrad 	int ret;
   1371  1.1  riastrad 
   1372  1.1  riastrad 	/* Prepare RepeaterAuth_Stream_Manage msg */
   1373  1.1  riastrad 	msgs.stream_manage.msg_id = HDCP_2_2_REP_STREAM_MANAGE;
   1374  1.1  riastrad 	drm_hdcp_cpu_to_be24(msgs.stream_manage.seq_num_m, hdcp->seq_num_m);
   1375  1.1  riastrad 
   1376  1.1  riastrad 	/* K no of streams is fixed as 1. Stored as big-endian. */
   1377  1.1  riastrad 	msgs.stream_manage.k = cpu_to_be16(1);
   1378  1.1  riastrad 
   1379  1.1  riastrad 	/* For HDMI this is forced to be 0x0. For DP SST also this is 0x0. */
   1380  1.1  riastrad 	msgs.stream_manage.streams[0].stream_id = 0;
   1381  1.1  riastrad 	msgs.stream_manage.streams[0].stream_type = hdcp->content_type;
   1382  1.1  riastrad 
   1383  1.1  riastrad 	/* Send it to Repeater */
   1384  1.1  riastrad 	ret = shim->write_2_2_msg(intel_dig_port, &msgs.stream_manage,
   1385  1.1  riastrad 				  sizeof(msgs.stream_manage));
   1386  1.1  riastrad 	if (ret < 0)
   1387  1.1  riastrad 		return ret;
   1388  1.1  riastrad 
   1389  1.1  riastrad 	ret = shim->read_2_2_msg(intel_dig_port, HDCP_2_2_REP_STREAM_READY,
   1390  1.1  riastrad 				 &msgs.stream_ready, sizeof(msgs.stream_ready));
   1391  1.1  riastrad 	if (ret < 0)
   1392  1.1  riastrad 		return ret;
   1393  1.1  riastrad 
   1394  1.1  riastrad 	hdcp->port_data.seq_num_m = hdcp->seq_num_m;
   1395  1.1  riastrad 	hdcp->port_data.streams[0].stream_type = hdcp->content_type;
   1396  1.1  riastrad 
   1397  1.1  riastrad 	ret = hdcp2_verify_mprime(connector, &msgs.stream_ready);
   1398  1.1  riastrad 	if (ret < 0)
   1399  1.1  riastrad 		return ret;
   1400  1.1  riastrad 
   1401  1.1  riastrad 	hdcp->seq_num_m++;
   1402  1.1  riastrad 
   1403  1.1  riastrad 	if (hdcp->seq_num_m > HDCP_2_2_SEQ_NUM_MAX) {
   1404  1.1  riastrad 		DRM_DEBUG_KMS("seq_num_m roll over.\n");
   1405  1.1  riastrad 		return -1;
   1406  1.1  riastrad 	}
   1407  1.1  riastrad 
   1408  1.1  riastrad 	return 0;
   1409  1.1  riastrad }
   1410  1.1  riastrad 
   1411  1.1  riastrad static
   1412  1.1  riastrad int hdcp2_authenticate_repeater_topology(struct intel_connector *connector)
   1413  1.1  riastrad {
   1414  1.1  riastrad 	struct intel_digital_port *intel_dig_port = conn_to_dig_port(connector);
   1415  1.1  riastrad 	struct intel_hdcp *hdcp = &connector->hdcp;
   1416  1.1  riastrad 	struct drm_device *dev = connector->base.dev;
   1417  1.1  riastrad 	union {
   1418  1.1  riastrad 		struct hdcp2_rep_send_receiverid_list recvid_list;
   1419  1.1  riastrad 		struct hdcp2_rep_send_ack rep_ack;
   1420  1.1  riastrad 	} msgs;
   1421  1.1  riastrad 	const struct intel_hdcp_shim *shim = hdcp->shim;
   1422  1.1  riastrad 	u32 seq_num_v, device_cnt;
   1423  1.1  riastrad 	u8 *rx_info;
   1424  1.1  riastrad 	int ret;
   1425  1.1  riastrad 
   1426  1.1  riastrad 	ret = shim->read_2_2_msg(intel_dig_port, HDCP_2_2_REP_SEND_RECVID_LIST,
   1427  1.1  riastrad 				 &msgs.recvid_list, sizeof(msgs.recvid_list));
   1428  1.1  riastrad 	if (ret < 0)
   1429  1.1  riastrad 		return ret;
   1430  1.1  riastrad 
   1431  1.1  riastrad 	rx_info = msgs.recvid_list.rx_info;
   1432  1.1  riastrad 
   1433  1.1  riastrad 	if (HDCP_2_2_MAX_CASCADE_EXCEEDED(rx_info[1]) ||
   1434  1.1  riastrad 	    HDCP_2_2_MAX_DEVS_EXCEEDED(rx_info[1])) {
   1435  1.1  riastrad 		DRM_DEBUG_KMS("Topology Max Size Exceeded\n");
   1436  1.1  riastrad 		return -EINVAL;
   1437  1.1  riastrad 	}
   1438  1.1  riastrad 
   1439  1.1  riastrad 	/* Converting and Storing the seq_num_v to local variable as DWORD */
   1440  1.1  riastrad 	seq_num_v =
   1441  1.1  riastrad 		drm_hdcp_be24_to_cpu((const u8 *)msgs.recvid_list.seq_num_v);
   1442  1.1  riastrad 
   1443  1.1  riastrad 	if (seq_num_v < hdcp->seq_num_v) {
   1444  1.1  riastrad 		/* Roll over of the seq_num_v from repeater. Reauthenticate. */
   1445  1.1  riastrad 		DRM_DEBUG_KMS("Seq_num_v roll over.\n");
   1446  1.1  riastrad 		return -EINVAL;
   1447  1.1  riastrad 	}
   1448  1.1  riastrad 
   1449  1.1  riastrad 	device_cnt = (HDCP_2_2_DEV_COUNT_HI(rx_info[0]) << 4 |
   1450  1.1  riastrad 		      HDCP_2_2_DEV_COUNT_LO(rx_info[1]));
   1451  1.1  riastrad 	if (drm_hdcp_check_ksvs_revoked(dev, msgs.recvid_list.receiver_ids,
   1452  1.1  riastrad 					device_cnt)) {
   1453  1.1  riastrad 		DRM_ERROR("Revoked receiver ID(s) is in list\n");
   1454  1.1  riastrad 		return -EPERM;
   1455  1.1  riastrad 	}
   1456  1.1  riastrad 
   1457  1.1  riastrad 	ret = hdcp2_verify_rep_topology_prepare_ack(connector,
   1458  1.1  riastrad 						    &msgs.recvid_list,
   1459  1.1  riastrad 						    &msgs.rep_ack);
   1460  1.1  riastrad 	if (ret < 0)
   1461  1.1  riastrad 		return ret;
   1462  1.1  riastrad 
   1463  1.1  riastrad 	hdcp->seq_num_v = seq_num_v;
   1464  1.1  riastrad 	ret = shim->write_2_2_msg(intel_dig_port, &msgs.rep_ack,
   1465  1.1  riastrad 				  sizeof(msgs.rep_ack));
   1466  1.1  riastrad 	if (ret < 0)
   1467  1.1  riastrad 		return ret;
   1468  1.1  riastrad 
   1469  1.1  riastrad 	return 0;
   1470  1.1  riastrad }
   1471  1.1  riastrad 
   1472  1.1  riastrad static int hdcp2_authenticate_repeater(struct intel_connector *connector)
   1473  1.1  riastrad {
   1474  1.1  riastrad 	int ret;
   1475  1.1  riastrad 
   1476  1.1  riastrad 	ret = hdcp2_authenticate_repeater_topology(connector);
   1477  1.1  riastrad 	if (ret < 0)
   1478  1.1  riastrad 		return ret;
   1479  1.1  riastrad 
   1480  1.1  riastrad 	return hdcp2_propagate_stream_management_info(connector);
   1481  1.1  riastrad }
   1482  1.1  riastrad 
   1483  1.1  riastrad static int hdcp2_authenticate_sink(struct intel_connector *connector)
   1484  1.1  riastrad {
   1485  1.1  riastrad 	struct intel_digital_port *intel_dig_port = conn_to_dig_port(connector);
   1486  1.1  riastrad 	struct intel_hdcp *hdcp = &connector->hdcp;
   1487  1.1  riastrad 	const struct intel_hdcp_shim *shim = hdcp->shim;
   1488  1.1  riastrad 	int ret;
   1489  1.1  riastrad 
   1490  1.1  riastrad 	ret = hdcp2_authentication_key_exchange(connector);
   1491  1.1  riastrad 	if (ret < 0) {
   1492  1.1  riastrad 		DRM_DEBUG_KMS("AKE Failed. Err : %d\n", ret);
   1493  1.1  riastrad 		return ret;
   1494  1.1  riastrad 	}
   1495  1.1  riastrad 
   1496  1.1  riastrad 	ret = hdcp2_locality_check(connector);
   1497  1.1  riastrad 	if (ret < 0) {
   1498  1.1  riastrad 		DRM_DEBUG_KMS("Locality Check failed. Err : %d\n", ret);
   1499  1.1  riastrad 		return ret;
   1500  1.1  riastrad 	}
   1501  1.1  riastrad 
   1502  1.1  riastrad 	ret = hdcp2_session_key_exchange(connector);
   1503  1.1  riastrad 	if (ret < 0) {
   1504  1.1  riastrad 		DRM_DEBUG_KMS("SKE Failed. Err : %d\n", ret);
   1505  1.1  riastrad 		return ret;
   1506  1.1  riastrad 	}
   1507  1.1  riastrad 
   1508  1.1  riastrad 	if (shim->config_stream_type) {
   1509  1.1  riastrad 		ret = shim->config_stream_type(intel_dig_port,
   1510  1.1  riastrad 					       hdcp->is_repeater,
   1511  1.1  riastrad 					       hdcp->content_type);
   1512  1.1  riastrad 		if (ret < 0)
   1513  1.1  riastrad 			return ret;
   1514  1.1  riastrad 	}
   1515  1.1  riastrad 
   1516  1.1  riastrad 	if (hdcp->is_repeater) {
   1517  1.1  riastrad 		ret = hdcp2_authenticate_repeater(connector);
   1518  1.1  riastrad 		if (ret < 0) {
   1519  1.1  riastrad 			DRM_DEBUG_KMS("Repeater Auth Failed. Err: %d\n", ret);
   1520  1.1  riastrad 			return ret;
   1521  1.1  riastrad 		}
   1522  1.1  riastrad 	}
   1523  1.1  riastrad 
   1524  1.1  riastrad 	hdcp->port_data.streams[0].stream_type = hdcp->content_type;
   1525  1.1  riastrad 	ret = hdcp2_authenticate_port(connector);
   1526  1.1  riastrad 	if (ret < 0)
   1527  1.1  riastrad 		return ret;
   1528  1.1  riastrad 
   1529  1.1  riastrad 	return ret;
   1530  1.1  riastrad }
   1531  1.1  riastrad 
   1532  1.1  riastrad static int hdcp2_enable_encryption(struct intel_connector *connector)
   1533  1.1  riastrad {
   1534  1.1  riastrad 	struct intel_digital_port *intel_dig_port = conn_to_dig_port(connector);
   1535  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1536  1.1  riastrad 	struct intel_hdcp *hdcp = &connector->hdcp;
   1537  1.1  riastrad 	enum port port = connector->encoder->port;
   1538  1.1  riastrad 	enum transcoder cpu_transcoder = hdcp->cpu_transcoder;
   1539  1.1  riastrad 	int ret;
   1540  1.1  riastrad 
   1541  1.1  riastrad 	WARN_ON(I915_READ(HDCP2_STATUS(dev_priv, cpu_transcoder, port)) &
   1542  1.1  riastrad 		LINK_ENCRYPTION_STATUS);
   1543  1.1  riastrad 	if (hdcp->shim->toggle_signalling) {
   1544  1.1  riastrad 		ret = hdcp->shim->toggle_signalling(intel_dig_port, true);
   1545  1.1  riastrad 		if (ret) {
   1546  1.1  riastrad 			DRM_ERROR("Failed to enable HDCP signalling. %d\n",
   1547  1.1  riastrad 				  ret);
   1548  1.1  riastrad 			return ret;
   1549  1.1  riastrad 		}
   1550  1.1  riastrad 	}
   1551  1.1  riastrad 
   1552  1.1  riastrad 	if (I915_READ(HDCP2_STATUS(dev_priv, cpu_transcoder, port)) &
   1553  1.1  riastrad 	    LINK_AUTH_STATUS) {
   1554  1.1  riastrad 		/* Link is Authenticated. Now set for Encryption */
   1555  1.1  riastrad 		I915_WRITE(HDCP2_CTL(dev_priv, cpu_transcoder, port),
   1556  1.1  riastrad 			   I915_READ(HDCP2_CTL(dev_priv, cpu_transcoder,
   1557  1.1  riastrad 					       port)) |
   1558  1.1  riastrad 			   CTL_LINK_ENCRYPTION_REQ);
   1559  1.1  riastrad 	}
   1560  1.1  riastrad 
   1561  1.1  riastrad 	ret = intel_de_wait_for_set(dev_priv,
   1562  1.1  riastrad 				    HDCP2_STATUS(dev_priv, cpu_transcoder,
   1563  1.1  riastrad 						 port),
   1564  1.1  riastrad 				    LINK_ENCRYPTION_STATUS,
   1565  1.1  riastrad 				    ENCRYPT_STATUS_CHANGE_TIMEOUT_MS);
   1566  1.1  riastrad 
   1567  1.1  riastrad 	return ret;
   1568  1.1  riastrad }
   1569  1.1  riastrad 
   1570  1.1  riastrad static int hdcp2_disable_encryption(struct intel_connector *connector)
   1571  1.1  riastrad {
   1572  1.1  riastrad 	struct intel_digital_port *intel_dig_port = conn_to_dig_port(connector);
   1573  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1574  1.1  riastrad 	struct intel_hdcp *hdcp = &connector->hdcp;
   1575  1.1  riastrad 	enum port port = connector->encoder->port;
   1576  1.1  riastrad 	enum transcoder cpu_transcoder = hdcp->cpu_transcoder;
   1577  1.1  riastrad 	int ret;
   1578  1.1  riastrad 
   1579  1.1  riastrad 	WARN_ON(!(I915_READ(HDCP2_STATUS(dev_priv, cpu_transcoder, port)) &
   1580  1.1  riastrad 			    LINK_ENCRYPTION_STATUS));
   1581  1.1  riastrad 
   1582  1.1  riastrad 	I915_WRITE(HDCP2_CTL(dev_priv, cpu_transcoder, port),
   1583  1.1  riastrad 		   I915_READ(HDCP2_CTL(dev_priv, cpu_transcoder, port)) &
   1584  1.1  riastrad 		   ~CTL_LINK_ENCRYPTION_REQ);
   1585  1.1  riastrad 
   1586  1.1  riastrad 	ret = intel_de_wait_for_clear(dev_priv,
   1587  1.1  riastrad 				      HDCP2_STATUS(dev_priv, cpu_transcoder,
   1588  1.1  riastrad 						   port),
   1589  1.1  riastrad 				      LINK_ENCRYPTION_STATUS,
   1590  1.1  riastrad 				      ENCRYPT_STATUS_CHANGE_TIMEOUT_MS);
   1591  1.1  riastrad 	if (ret == -ETIMEDOUT)
   1592  1.1  riastrad 		DRM_DEBUG_KMS("Disable Encryption Timedout");
   1593  1.1  riastrad 
   1594  1.1  riastrad 	if (hdcp->shim->toggle_signalling) {
   1595  1.1  riastrad 		ret = hdcp->shim->toggle_signalling(intel_dig_port, false);
   1596  1.1  riastrad 		if (ret) {
   1597  1.1  riastrad 			DRM_ERROR("Failed to disable HDCP signalling. %d\n",
   1598  1.1  riastrad 				  ret);
   1599  1.1  riastrad 			return ret;
   1600  1.1  riastrad 		}
   1601  1.1  riastrad 	}
   1602  1.1  riastrad 
   1603  1.1  riastrad 	return ret;
   1604  1.1  riastrad }
   1605  1.1  riastrad 
   1606  1.1  riastrad static int hdcp2_authenticate_and_encrypt(struct intel_connector *connector)
   1607  1.1  riastrad {
   1608  1.1  riastrad 	int ret, i, tries = 3;
   1609  1.1  riastrad 
   1610  1.1  riastrad 	for (i = 0; i < tries; i++) {
   1611  1.1  riastrad 		ret = hdcp2_authenticate_sink(connector);
   1612  1.1  riastrad 		if (!ret)
   1613  1.1  riastrad 			break;
   1614  1.1  riastrad 
   1615  1.1  riastrad 		/* Clearing the mei hdcp session */
   1616  1.1  riastrad 		DRM_DEBUG_KMS("HDCP2.2 Auth %d of %d Failed.(%d)\n",
   1617  1.1  riastrad 			      i + 1, tries, ret);
   1618  1.1  riastrad 		if (hdcp2_deauthenticate_port(connector) < 0)
   1619  1.1  riastrad 			DRM_DEBUG_KMS("Port deauth failed.\n");
   1620  1.1  riastrad 	}
   1621  1.1  riastrad 
   1622  1.1  riastrad 	if (i != tries) {
   1623  1.1  riastrad 		/*
   1624  1.1  riastrad 		 * Ensuring the required 200mSec min time interval between
   1625  1.1  riastrad 		 * Session Key Exchange and encryption.
   1626  1.1  riastrad 		 */
   1627  1.1  riastrad 		msleep(HDCP_2_2_DELAY_BEFORE_ENCRYPTION_EN);
   1628  1.1  riastrad 		ret = hdcp2_enable_encryption(connector);
   1629  1.1  riastrad 		if (ret < 0) {
   1630  1.1  riastrad 			DRM_DEBUG_KMS("Encryption Enable Failed.(%d)\n", ret);
   1631  1.1  riastrad 			if (hdcp2_deauthenticate_port(connector) < 0)
   1632  1.1  riastrad 				DRM_DEBUG_KMS("Port deauth failed.\n");
   1633  1.1  riastrad 		}
   1634  1.1  riastrad 	}
   1635  1.1  riastrad 
   1636  1.1  riastrad 	return ret;
   1637  1.1  riastrad }
   1638  1.1  riastrad 
   1639  1.1  riastrad static int _intel_hdcp2_enable(struct intel_connector *connector)
   1640  1.1  riastrad {
   1641  1.1  riastrad 	struct intel_hdcp *hdcp = &connector->hdcp;
   1642  1.1  riastrad 	int ret;
   1643  1.1  riastrad 
   1644  1.1  riastrad 	DRM_DEBUG_KMS("[%s:%d] HDCP2.2 is being enabled. Type: %d\n",
   1645  1.1  riastrad 		      connector->base.name, connector->base.base.id,
   1646  1.1  riastrad 		      hdcp->content_type);
   1647  1.1  riastrad 
   1648  1.1  riastrad 	ret = hdcp2_authenticate_and_encrypt(connector);
   1649  1.1  riastrad 	if (ret) {
   1650  1.1  riastrad 		DRM_DEBUG_KMS("HDCP2 Type%d  Enabling Failed. (%d)\n",
   1651  1.1  riastrad 			      hdcp->content_type, ret);
   1652  1.1  riastrad 		return ret;
   1653  1.1  riastrad 	}
   1654  1.1  riastrad 
   1655  1.1  riastrad 	DRM_DEBUG_KMS("[%s:%d] HDCP2.2 is enabled. Type %d\n",
   1656  1.1  riastrad 		      connector->base.name, connector->base.base.id,
   1657  1.1  riastrad 		      hdcp->content_type);
   1658  1.1  riastrad 
   1659  1.1  riastrad 	hdcp->hdcp2_encrypted = true;
   1660  1.1  riastrad 	return 0;
   1661  1.1  riastrad }
   1662  1.1  riastrad 
   1663  1.1  riastrad static int _intel_hdcp2_disable(struct intel_connector *connector)
   1664  1.1  riastrad {
   1665  1.1  riastrad 	int ret;
   1666  1.1  riastrad 
   1667  1.1  riastrad 	DRM_DEBUG_KMS("[%s:%d] HDCP2.2 is being Disabled\n",
   1668  1.1  riastrad 		      connector->base.name, connector->base.base.id);
   1669  1.1  riastrad 
   1670  1.1  riastrad 	ret = hdcp2_disable_encryption(connector);
   1671  1.1  riastrad 
   1672  1.1  riastrad 	if (hdcp2_deauthenticate_port(connector) < 0)
   1673  1.1  riastrad 		DRM_DEBUG_KMS("Port deauth failed.\n");
   1674  1.1  riastrad 
   1675  1.1  riastrad 	connector->hdcp.hdcp2_encrypted = false;
   1676  1.1  riastrad 
   1677  1.1  riastrad 	return ret;
   1678  1.1  riastrad }
   1679  1.1  riastrad 
   1680  1.1  riastrad /* Implements the Link Integrity Check for HDCP2.2 */
   1681  1.1  riastrad static int intel_hdcp2_check_link(struct intel_connector *connector)
   1682  1.1  riastrad {
   1683  1.1  riastrad 	struct intel_digital_port *intel_dig_port = conn_to_dig_port(connector);
   1684  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1685  1.1  riastrad 	struct intel_hdcp *hdcp = &connector->hdcp;
   1686  1.1  riastrad 	enum port port = connector->encoder->port;
   1687  1.1  riastrad 	enum transcoder cpu_transcoder;
   1688  1.1  riastrad 	int ret = 0;
   1689  1.1  riastrad 
   1690  1.1  riastrad 	mutex_lock(&hdcp->mutex);
   1691  1.1  riastrad 	cpu_transcoder = hdcp->cpu_transcoder;
   1692  1.1  riastrad 
   1693  1.1  riastrad 	/* hdcp2_check_link is expected only when HDCP2.2 is Enabled */
   1694  1.1  riastrad 	if (hdcp->value != DRM_MODE_CONTENT_PROTECTION_ENABLED ||
   1695  1.1  riastrad 	    !hdcp->hdcp2_encrypted) {
   1696  1.1  riastrad 		ret = -EINVAL;
   1697  1.1  riastrad 		goto out;
   1698  1.1  riastrad 	}
   1699  1.1  riastrad 
   1700  1.1  riastrad 	if (WARN_ON(!intel_hdcp2_in_use(dev_priv, cpu_transcoder, port))) {
   1701  1.1  riastrad 		DRM_ERROR("HDCP2.2 link stopped the encryption, %x\n",
   1702  1.1  riastrad 			  I915_READ(HDCP2_STATUS(dev_priv, cpu_transcoder,
   1703  1.1  riastrad 						 port)));
   1704  1.1  riastrad 		ret = -ENXIO;
   1705  1.1  riastrad 		hdcp->value = DRM_MODE_CONTENT_PROTECTION_DESIRED;
   1706  1.1  riastrad 		schedule_work(&hdcp->prop_work);
   1707  1.1  riastrad 		goto out;
   1708  1.1  riastrad 	}
   1709  1.1  riastrad 
   1710  1.1  riastrad 	ret = hdcp->shim->check_2_2_link(intel_dig_port);
   1711  1.1  riastrad 	if (ret == HDCP_LINK_PROTECTED) {
   1712  1.1  riastrad 		if (hdcp->value != DRM_MODE_CONTENT_PROTECTION_UNDESIRED) {
   1713  1.1  riastrad 			hdcp->value = DRM_MODE_CONTENT_PROTECTION_ENABLED;
   1714  1.1  riastrad 			schedule_work(&hdcp->prop_work);
   1715  1.1  riastrad 		}
   1716  1.1  riastrad 		goto out;
   1717  1.1  riastrad 	}
   1718  1.1  riastrad 
   1719  1.1  riastrad 	if (ret == HDCP_TOPOLOGY_CHANGE) {
   1720  1.1  riastrad 		if (hdcp->value == DRM_MODE_CONTENT_PROTECTION_UNDESIRED)
   1721  1.1  riastrad 			goto out;
   1722  1.1  riastrad 
   1723  1.1  riastrad 		DRM_DEBUG_KMS("HDCP2.2 Downstream topology change\n");
   1724  1.1  riastrad 		ret = hdcp2_authenticate_repeater_topology(connector);
   1725  1.1  riastrad 		if (!ret) {
   1726  1.1  riastrad 			hdcp->value = DRM_MODE_CONTENT_PROTECTION_ENABLED;
   1727  1.1  riastrad 			schedule_work(&hdcp->prop_work);
   1728  1.1  riastrad 			goto out;
   1729  1.1  riastrad 		}
   1730  1.1  riastrad 		DRM_DEBUG_KMS("[%s:%d] Repeater topology auth failed.(%d)\n",
   1731  1.1  riastrad 			      connector->base.name, connector->base.base.id,
   1732  1.1  riastrad 			      ret);
   1733  1.1  riastrad 	} else {
   1734  1.1  riastrad 		DRM_DEBUG_KMS("[%s:%d] HDCP2.2 link failed, retrying auth\n",
   1735  1.1  riastrad 			      connector->base.name, connector->base.base.id);
   1736  1.1  riastrad 	}
   1737  1.1  riastrad 
   1738  1.1  riastrad 	ret = _intel_hdcp2_disable(connector);
   1739  1.1  riastrad 	if (ret) {
   1740  1.1  riastrad 		DRM_ERROR("[%s:%d] Failed to disable hdcp2.2 (%d)\n",
   1741  1.1  riastrad 			  connector->base.name, connector->base.base.id, ret);
   1742  1.1  riastrad 		hdcp->value = DRM_MODE_CONTENT_PROTECTION_DESIRED;
   1743  1.1  riastrad 		schedule_work(&hdcp->prop_work);
   1744  1.1  riastrad 		goto out;
   1745  1.1  riastrad 	}
   1746  1.1  riastrad 
   1747  1.1  riastrad 	ret = _intel_hdcp2_enable(connector);
   1748  1.1  riastrad 	if (ret) {
   1749  1.1  riastrad 		DRM_DEBUG_KMS("[%s:%d] Failed to enable hdcp2.2 (%d)\n",
   1750  1.1  riastrad 			      connector->base.name, connector->base.base.id,
   1751  1.1  riastrad 			      ret);
   1752  1.1  riastrad 		hdcp->value = DRM_MODE_CONTENT_PROTECTION_DESIRED;
   1753  1.1  riastrad 		schedule_work(&hdcp->prop_work);
   1754  1.1  riastrad 		goto out;
   1755  1.1  riastrad 	}
   1756  1.1  riastrad 
   1757  1.1  riastrad out:
   1758  1.1  riastrad 	mutex_unlock(&hdcp->mutex);
   1759  1.1  riastrad 	return ret;
   1760  1.1  riastrad }
   1761  1.1  riastrad 
   1762  1.1  riastrad static void intel_hdcp_check_work(struct work_struct *work)
   1763  1.1  riastrad {
   1764  1.1  riastrad 	struct intel_hdcp *hdcp = container_of(to_delayed_work(work),
   1765  1.1  riastrad 					       struct intel_hdcp,
   1766  1.1  riastrad 					       check_work);
   1767  1.1  riastrad 	struct intel_connector *connector = intel_hdcp_to_connector(hdcp);
   1768  1.1  riastrad 
   1769  1.1  riastrad 	if (!intel_hdcp2_check_link(connector))
   1770  1.1  riastrad 		schedule_delayed_work(&hdcp->check_work,
   1771  1.1  riastrad 				      DRM_HDCP2_CHECK_PERIOD_MS);
   1772  1.1  riastrad 	else if (!intel_hdcp_check_link(connector))
   1773  1.1  riastrad 		schedule_delayed_work(&hdcp->check_work,
   1774  1.1  riastrad 				      DRM_HDCP_CHECK_PERIOD_MS);
   1775  1.1  riastrad }
   1776  1.1  riastrad 
   1777  1.4  riastrad #ifndef __NetBSD__		/* XXX i915 hdmi audio */
   1778  1.4  riastrad 
   1779  1.1  riastrad static int i915_hdcp_component_bind(struct device *i915_kdev,
   1780  1.1  riastrad 				    struct device *mei_kdev, void *data)
   1781  1.1  riastrad {
   1782  1.1  riastrad 	struct drm_i915_private *dev_priv = kdev_to_i915(i915_kdev);
   1783  1.1  riastrad 
   1784  1.1  riastrad 	DRM_DEBUG("I915 HDCP comp bind\n");
   1785  1.1  riastrad 	mutex_lock(&dev_priv->hdcp_comp_mutex);
   1786  1.1  riastrad 	dev_priv->hdcp_master = (struct i915_hdcp_comp_master *)data;
   1787  1.1  riastrad 	dev_priv->hdcp_master->mei_dev = mei_kdev;
   1788  1.1  riastrad 	mutex_unlock(&dev_priv->hdcp_comp_mutex);
   1789  1.1  riastrad 
   1790  1.1  riastrad 	return 0;
   1791  1.1  riastrad }
   1792  1.1  riastrad 
   1793  1.1  riastrad static void i915_hdcp_component_unbind(struct device *i915_kdev,
   1794  1.1  riastrad 				       struct device *mei_kdev, void *data)
   1795  1.1  riastrad {
   1796  1.1  riastrad 	struct drm_i915_private *dev_priv = kdev_to_i915(i915_kdev);
   1797  1.1  riastrad 
   1798  1.1  riastrad 	DRM_DEBUG("I915 HDCP comp unbind\n");
   1799  1.1  riastrad 	mutex_lock(&dev_priv->hdcp_comp_mutex);
   1800  1.1  riastrad 	dev_priv->hdcp_master = NULL;
   1801  1.1  riastrad 	mutex_unlock(&dev_priv->hdcp_comp_mutex);
   1802  1.1  riastrad }
   1803  1.1  riastrad 
   1804  1.1  riastrad static const struct component_ops i915_hdcp_component_ops = {
   1805  1.1  riastrad 	.bind   = i915_hdcp_component_bind,
   1806  1.1  riastrad 	.unbind = i915_hdcp_component_unbind,
   1807  1.1  riastrad };
   1808  1.1  riastrad 
   1809  1.4  riastrad #endif
   1810  1.4  riastrad 
   1811  1.1  riastrad static inline
   1812  1.1  riastrad enum mei_fw_ddi intel_get_mei_fw_ddi_index(enum port port)
   1813  1.1  riastrad {
   1814  1.1  riastrad 	switch (port) {
   1815  1.1  riastrad 	case PORT_A:
   1816  1.1  riastrad 		return MEI_DDI_A;
   1817  1.1  riastrad 	case PORT_B ... PORT_F:
   1818  1.1  riastrad 		return (enum mei_fw_ddi)port;
   1819  1.1  riastrad 	default:
   1820  1.1  riastrad 		return MEI_DDI_INVALID_PORT;
   1821  1.1  riastrad 	}
   1822  1.1  riastrad }
   1823  1.1  riastrad 
   1824  1.1  riastrad static inline
   1825  1.1  riastrad enum mei_fw_tc intel_get_mei_fw_tc(enum transcoder cpu_transcoder)
   1826  1.1  riastrad {
   1827  1.1  riastrad 	switch (cpu_transcoder) {
   1828  1.1  riastrad 	case TRANSCODER_A ... TRANSCODER_D:
   1829  1.1  riastrad 		return (enum mei_fw_tc)(cpu_transcoder | 0x10);
   1830  1.1  riastrad 	default: /* eDP, DSI TRANSCODERS are non HDCP capable */
   1831  1.1  riastrad 		return MEI_INVALID_TRANSCODER;
   1832  1.1  riastrad 	}
   1833  1.1  riastrad }
   1834  1.1  riastrad 
   1835  1.1  riastrad static inline int initialize_hdcp_port_data(struct intel_connector *connector,
   1836  1.1  riastrad 					    const struct intel_hdcp_shim *shim)
   1837  1.1  riastrad {
   1838  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1839  1.1  riastrad 	struct intel_hdcp *hdcp = &connector->hdcp;
   1840  1.1  riastrad 	struct hdcp_port_data *data = &hdcp->port_data;
   1841  1.1  riastrad 
   1842  1.1  riastrad 	if (INTEL_GEN(dev_priv) < 12)
   1843  1.1  riastrad 		data->fw_ddi =
   1844  1.1  riastrad 			intel_get_mei_fw_ddi_index(connector->encoder->port);
   1845  1.1  riastrad 	else
   1846  1.1  riastrad 		/*
   1847  1.1  riastrad 		 * As per ME FW API expectation, for GEN 12+, fw_ddi is filled
   1848  1.1  riastrad 		 * with zero(INVALID PORT index).
   1849  1.1  riastrad 		 */
   1850  1.1  riastrad 		data->fw_ddi = MEI_DDI_INVALID_PORT;
   1851  1.1  riastrad 
   1852  1.1  riastrad 	/*
   1853  1.1  riastrad 	 * As associated transcoder is set and modified at modeset, here fw_tc
   1854  1.1  riastrad 	 * is initialized to zero (invalid transcoder index). This will be
   1855  1.1  riastrad 	 * retained for <Gen12 forever.
   1856  1.1  riastrad 	 */
   1857  1.1  riastrad 	data->fw_tc = MEI_INVALID_TRANSCODER;
   1858  1.1  riastrad 
   1859  1.1  riastrad 	data->port_type = (u8)HDCP_PORT_TYPE_INTEGRATED;
   1860  1.1  riastrad 	data->protocol = (u8)shim->protocol;
   1861  1.1  riastrad 
   1862  1.1  riastrad 	data->k = 1;
   1863  1.1  riastrad 	if (!data->streams)
   1864  1.1  riastrad 		data->streams = kcalloc(data->k,
   1865  1.1  riastrad 					sizeof(struct hdcp2_streamid_type),
   1866  1.1  riastrad 					GFP_KERNEL);
   1867  1.1  riastrad 	if (!data->streams) {
   1868  1.1  riastrad 		DRM_ERROR("Out of Memory\n");
   1869  1.1  riastrad 		return -ENOMEM;
   1870  1.1  riastrad 	}
   1871  1.1  riastrad 
   1872  1.1  riastrad 	data->streams[0].stream_id = 0;
   1873  1.1  riastrad 	data->streams[0].stream_type = hdcp->content_type;
   1874  1.1  riastrad 
   1875  1.1  riastrad 	return 0;
   1876  1.1  riastrad }
   1877  1.1  riastrad 
   1878  1.1  riastrad static bool is_hdcp2_supported(struct drm_i915_private *dev_priv)
   1879  1.1  riastrad {
   1880  1.1  riastrad 	if (!IS_ENABLED(CONFIG_INTEL_MEI_HDCP))
   1881  1.1  riastrad 		return false;
   1882  1.1  riastrad 
   1883  1.1  riastrad 	return (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv) ||
   1884  1.1  riastrad 		IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv));
   1885  1.1  riastrad }
   1886  1.1  riastrad 
   1887  1.1  riastrad void intel_hdcp_component_init(struct drm_i915_private *dev_priv)
   1888  1.1  riastrad {
   1889  1.1  riastrad 	int ret;
   1890  1.1  riastrad 
   1891  1.1  riastrad 	if (!is_hdcp2_supported(dev_priv))
   1892  1.1  riastrad 		return;
   1893  1.1  riastrad 
   1894  1.1  riastrad 	mutex_lock(&dev_priv->hdcp_comp_mutex);
   1895  1.1  riastrad 	WARN_ON(dev_priv->hdcp_comp_added);
   1896  1.1  riastrad 
   1897  1.1  riastrad 	dev_priv->hdcp_comp_added = true;
   1898  1.1  riastrad 	mutex_unlock(&dev_priv->hdcp_comp_mutex);
   1899  1.4  riastrad #ifdef __NetBSD__		/* XXX i915 hdmi audio */
   1900  1.4  riastrad 	ret = 0;
   1901  1.4  riastrad #else
   1902  1.1  riastrad 	ret = component_add_typed(dev_priv->drm.dev, &i915_hdcp_component_ops,
   1903  1.1  riastrad 				  I915_COMPONENT_HDCP);
   1904  1.4  riastrad #endif
   1905  1.1  riastrad 	if (ret < 0) {
   1906  1.1  riastrad 		DRM_DEBUG_KMS("Failed at component add(%d)\n", ret);
   1907  1.1  riastrad 		mutex_lock(&dev_priv->hdcp_comp_mutex);
   1908  1.1  riastrad 		dev_priv->hdcp_comp_added = false;
   1909  1.1  riastrad 		mutex_unlock(&dev_priv->hdcp_comp_mutex);
   1910  1.1  riastrad 		return;
   1911  1.1  riastrad 	}
   1912  1.1  riastrad }
   1913  1.1  riastrad 
   1914  1.1  riastrad static void intel_hdcp2_init(struct intel_connector *connector,
   1915  1.1  riastrad 			     const struct intel_hdcp_shim *shim)
   1916  1.1  riastrad {
   1917  1.1  riastrad 	struct intel_hdcp *hdcp = &connector->hdcp;
   1918  1.1  riastrad 	int ret;
   1919  1.1  riastrad 
   1920  1.1  riastrad 	ret = initialize_hdcp_port_data(connector, shim);
   1921  1.1  riastrad 	if (ret) {
   1922  1.1  riastrad 		DRM_DEBUG_KMS("Mei hdcp data init failed\n");
   1923  1.1  riastrad 		return;
   1924  1.1  riastrad 	}
   1925  1.1  riastrad 
   1926  1.1  riastrad 	hdcp->hdcp2_supported = true;
   1927  1.1  riastrad }
   1928  1.1  riastrad 
   1929  1.1  riastrad int intel_hdcp_init(struct intel_connector *connector,
   1930  1.1  riastrad 		    const struct intel_hdcp_shim *shim)
   1931  1.1  riastrad {
   1932  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1933  1.1  riastrad 	struct intel_hdcp *hdcp = &connector->hdcp;
   1934  1.1  riastrad 	int ret;
   1935  1.1  riastrad 
   1936  1.1  riastrad 	if (!shim)
   1937  1.1  riastrad 		return -EINVAL;
   1938  1.1  riastrad 
   1939  1.1  riastrad 	if (is_hdcp2_supported(dev_priv))
   1940  1.1  riastrad 		intel_hdcp2_init(connector, shim);
   1941  1.1  riastrad 
   1942  1.1  riastrad 	ret =
   1943  1.1  riastrad 	drm_connector_attach_content_protection_property(&connector->base,
   1944  1.1  riastrad 							 hdcp->hdcp2_supported);
   1945  1.1  riastrad 	if (ret) {
   1946  1.1  riastrad 		hdcp->hdcp2_supported = false;
   1947  1.1  riastrad 		kfree(hdcp->port_data.streams);
   1948  1.1  riastrad 		return ret;
   1949  1.1  riastrad 	}
   1950  1.1  riastrad 
   1951  1.1  riastrad 	hdcp->shim = shim;
   1952  1.1  riastrad 	mutex_init(&hdcp->mutex);
   1953  1.1  riastrad 	INIT_DELAYED_WORK(&hdcp->check_work, intel_hdcp_check_work);
   1954  1.1  riastrad 	INIT_WORK(&hdcp->prop_work, intel_hdcp_prop_work);
   1955  1.3  riastrad 	DRM_INIT_WAITQUEUE(&hdcp->cp_irq_queue, "hdcpirq");
   1956  1.1  riastrad 
   1957  1.1  riastrad 	return 0;
   1958  1.1  riastrad }
   1959  1.1  riastrad 
   1960  1.1  riastrad int intel_hdcp_enable(struct intel_connector *connector,
   1961  1.1  riastrad 		      enum transcoder cpu_transcoder, u8 content_type)
   1962  1.1  riastrad {
   1963  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
   1964  1.1  riastrad 	struct intel_hdcp *hdcp = &connector->hdcp;
   1965  1.1  riastrad 	unsigned long check_link_interval = DRM_HDCP_CHECK_PERIOD_MS;
   1966  1.1  riastrad 	int ret = -EINVAL;
   1967  1.1  riastrad 
   1968  1.1  riastrad 	if (!hdcp->shim)
   1969  1.1  riastrad 		return -ENOENT;
   1970  1.1  riastrad 
   1971  1.1  riastrad 	mutex_lock(&hdcp->mutex);
   1972  1.1  riastrad 	WARN_ON(hdcp->value == DRM_MODE_CONTENT_PROTECTION_ENABLED);
   1973  1.1  riastrad 	hdcp->content_type = content_type;
   1974  1.1  riastrad 
   1975  1.1  riastrad 	if (INTEL_GEN(dev_priv) >= 12) {
   1976  1.1  riastrad 		hdcp->cpu_transcoder = cpu_transcoder;
   1977  1.1  riastrad 		hdcp->port_data.fw_tc = intel_get_mei_fw_tc(cpu_transcoder);
   1978  1.1  riastrad 	}
   1979  1.1  riastrad 
   1980  1.1  riastrad 	/*
   1981  1.1  riastrad 	 * Considering that HDCP2.2 is more secure than HDCP1.4, If the setup
   1982  1.1  riastrad 	 * is capable of HDCP2.2, it is preferred to use HDCP2.2.
   1983  1.1  riastrad 	 */
   1984  1.1  riastrad 	if (intel_hdcp2_capable(connector)) {
   1985  1.1  riastrad 		ret = _intel_hdcp2_enable(connector);
   1986  1.1  riastrad 		if (!ret)
   1987  1.1  riastrad 			check_link_interval = DRM_HDCP2_CHECK_PERIOD_MS;
   1988  1.1  riastrad 	}
   1989  1.1  riastrad 
   1990  1.1  riastrad 	/*
   1991  1.1  riastrad 	 * When HDCP2.2 fails and Content Type is not Type1, HDCP1.4 will
   1992  1.1  riastrad 	 * be attempted.
   1993  1.1  riastrad 	 */
   1994  1.1  riastrad 	if (ret && intel_hdcp_capable(connector) &&
   1995  1.1  riastrad 	    hdcp->content_type != DRM_MODE_HDCP_CONTENT_TYPE1) {
   1996  1.1  riastrad 		ret = _intel_hdcp_enable(connector);
   1997  1.1  riastrad 	}
   1998  1.1  riastrad 
   1999  1.1  riastrad 	if (!ret) {
   2000  1.1  riastrad 		schedule_delayed_work(&hdcp->check_work, check_link_interval);
   2001  1.1  riastrad 		hdcp->value = DRM_MODE_CONTENT_PROTECTION_ENABLED;
   2002  1.1  riastrad 		schedule_work(&hdcp->prop_work);
   2003  1.1  riastrad 	}
   2004  1.1  riastrad 
   2005  1.1  riastrad 	mutex_unlock(&hdcp->mutex);
   2006  1.1  riastrad 	return ret;
   2007  1.1  riastrad }
   2008  1.1  riastrad 
   2009  1.1  riastrad int intel_hdcp_disable(struct intel_connector *connector)
   2010  1.1  riastrad {
   2011  1.1  riastrad 	struct intel_hdcp *hdcp = &connector->hdcp;
   2012  1.1  riastrad 	int ret = 0;
   2013  1.1  riastrad 
   2014  1.1  riastrad 	if (!hdcp->shim)
   2015  1.1  riastrad 		return -ENOENT;
   2016  1.1  riastrad 
   2017  1.1  riastrad 	mutex_lock(&hdcp->mutex);
   2018  1.1  riastrad 
   2019  1.1  riastrad 	if (hdcp->value != DRM_MODE_CONTENT_PROTECTION_UNDESIRED) {
   2020  1.1  riastrad 		hdcp->value = DRM_MODE_CONTENT_PROTECTION_UNDESIRED;
   2021  1.1  riastrad 		if (hdcp->hdcp2_encrypted)
   2022  1.1  riastrad 			ret = _intel_hdcp2_disable(connector);
   2023  1.1  riastrad 		else if (hdcp->hdcp_encrypted)
   2024  1.1  riastrad 			ret = _intel_hdcp_disable(connector);
   2025  1.1  riastrad 	}
   2026  1.1  riastrad 
   2027  1.1  riastrad 	mutex_unlock(&hdcp->mutex);
   2028  1.1  riastrad 	cancel_delayed_work_sync(&hdcp->check_work);
   2029  1.1  riastrad 	return ret;
   2030  1.1  riastrad }
   2031  1.1  riastrad 
   2032  1.1  riastrad void intel_hdcp_component_fini(struct drm_i915_private *dev_priv)
   2033  1.1  riastrad {
   2034  1.1  riastrad 	mutex_lock(&dev_priv->hdcp_comp_mutex);
   2035  1.1  riastrad 	if (!dev_priv->hdcp_comp_added) {
   2036  1.1  riastrad 		mutex_unlock(&dev_priv->hdcp_comp_mutex);
   2037  1.1  riastrad 		return;
   2038  1.1  riastrad 	}
   2039  1.1  riastrad 
   2040  1.1  riastrad 	dev_priv->hdcp_comp_added = false;
   2041  1.1  riastrad 	mutex_unlock(&dev_priv->hdcp_comp_mutex);
   2042  1.1  riastrad 
   2043  1.5  riastrad #ifndef __NetBSD__		/* XXX i915 hdmi audio */
   2044  1.1  riastrad 	component_del(dev_priv->drm.dev, &i915_hdcp_component_ops);
   2045  1.5  riastrad #endif
   2046  1.1  riastrad }
   2047  1.1  riastrad 
   2048  1.1  riastrad void intel_hdcp_cleanup(struct intel_connector *connector)
   2049  1.1  riastrad {
   2050  1.1  riastrad 	if (!connector->hdcp.shim)
   2051  1.1  riastrad 		return;
   2052  1.1  riastrad 
   2053  1.1  riastrad 	mutex_lock(&connector->hdcp.mutex);
   2054  1.1  riastrad 	kfree(connector->hdcp.port_data.streams);
   2055  1.1  riastrad 	mutex_unlock(&connector->hdcp.mutex);
   2056  1.6  riastrad 
   2057  1.6  riastrad 	mutex_destroy(&connector->hdcp.mutex);
   2058  1.1  riastrad }
   2059  1.1  riastrad 
   2060  1.1  riastrad void intel_hdcp_atomic_check(struct drm_connector *connector,
   2061  1.1  riastrad 			     struct drm_connector_state *old_state,
   2062  1.1  riastrad 			     struct drm_connector_state *new_state)
   2063  1.1  riastrad {
   2064  1.1  riastrad 	u64 old_cp = old_state->content_protection;
   2065  1.1  riastrad 	u64 new_cp = new_state->content_protection;
   2066  1.1  riastrad 	struct drm_crtc_state *crtc_state;
   2067  1.1  riastrad 
   2068  1.1  riastrad 	if (!new_state->crtc) {
   2069  1.1  riastrad 		/*
   2070  1.1  riastrad 		 * If the connector is being disabled with CP enabled, mark it
   2071  1.1  riastrad 		 * desired so it's re-enabled when the connector is brought back
   2072  1.1  riastrad 		 */
   2073  1.1  riastrad 		if (old_cp == DRM_MODE_CONTENT_PROTECTION_ENABLED)
   2074  1.1  riastrad 			new_state->content_protection =
   2075  1.1  riastrad 				DRM_MODE_CONTENT_PROTECTION_DESIRED;
   2076  1.1  riastrad 		return;
   2077  1.1  riastrad 	}
   2078  1.1  riastrad 
   2079  1.1  riastrad 	/*
   2080  1.1  riastrad 	 * Nothing to do if the state didn't change, or HDCP was activated since
   2081  1.1  riastrad 	 * the last commit. And also no change in hdcp content type.
   2082  1.1  riastrad 	 */
   2083  1.1  riastrad 	if (old_cp == new_cp ||
   2084  1.1  riastrad 	    (old_cp == DRM_MODE_CONTENT_PROTECTION_DESIRED &&
   2085  1.1  riastrad 	     new_cp == DRM_MODE_CONTENT_PROTECTION_ENABLED)) {
   2086  1.1  riastrad 		if (old_state->hdcp_content_type ==
   2087  1.1  riastrad 				new_state->hdcp_content_type)
   2088  1.1  riastrad 			return;
   2089  1.1  riastrad 	}
   2090  1.1  riastrad 
   2091  1.1  riastrad 	crtc_state = drm_atomic_get_new_crtc_state(new_state->state,
   2092  1.1  riastrad 						   new_state->crtc);
   2093  1.1  riastrad 	crtc_state->mode_changed = true;
   2094  1.1  riastrad }
   2095  1.1  riastrad 
   2096  1.1  riastrad /* Handles the CP_IRQ raised from the DP HDCP sink */
   2097  1.1  riastrad void intel_hdcp_handle_cp_irq(struct intel_connector *connector)
   2098  1.1  riastrad {
   2099  1.1  riastrad 	struct intel_hdcp *hdcp = &connector->hdcp;
   2100  1.1  riastrad 
   2101  1.1  riastrad 	if (!hdcp->shim)
   2102  1.1  riastrad 		return;
   2103  1.1  riastrad 
   2104  1.3  riastrad 	unsigned long irqflags;
   2105  1.3  riastrad 	spin_lock_irqsave(&connector->hdcp.cp_irq_lock, irqflags);
   2106  1.1  riastrad 	atomic_inc(&connector->hdcp.cp_irq_count);
   2107  1.3  riastrad 	DRM_SPIN_WAKEUP_ALL(&connector->hdcp.cp_irq_queue,
   2108  1.3  riastrad 	    &connector->hdcp.cp_irq_lock);
   2109  1.3  riastrad 	spin_unlock_irqrestore(&connector->hdcp.cp_irq_lock, irqflags);
   2110  1.1  riastrad 
   2111  1.1  riastrad 	schedule_delayed_work(&hdcp->check_work, 0);
   2112  1.1  riastrad }
   2113