Home | History | Annotate | Line # | Download | only in display
      1  1.1  riastrad /*	$NetBSD: intel_hotplug.c,v 1.2 2021/12/18 23:45:30 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright  2015 Intel Corporation
      5  1.1  riastrad  *
      6  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      8  1.1  riastrad  * to deal in the Software without restriction, including without limitation
      9  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     11  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     12  1.1  riastrad  *
     13  1.1  riastrad  * The above copyright notice and this permission notice (including the next
     14  1.1  riastrad  * paragraph) shall be included in all copies or substantial portions of the
     15  1.1  riastrad  * Software.
     16  1.1  riastrad  *
     17  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20  1.1  riastrad  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     21  1.1  riastrad  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     22  1.1  riastrad  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     23  1.1  riastrad  * IN THE SOFTWARE.
     24  1.1  riastrad  */
     25  1.1  riastrad 
     26  1.1  riastrad #include <sys/cdefs.h>
     27  1.1  riastrad __KERNEL_RCSID(0, "$NetBSD: intel_hotplug.c,v 1.2 2021/12/18 23:45:30 riastradh Exp $");
     28  1.1  riastrad 
     29  1.1  riastrad #include <linux/kernel.h>
     30  1.1  riastrad 
     31  1.1  riastrad #include <drm/i915_drm.h>
     32  1.1  riastrad 
     33  1.1  riastrad #include "i915_drv.h"
     34  1.1  riastrad #include "intel_display_types.h"
     35  1.1  riastrad #include "intel_hotplug.h"
     36  1.1  riastrad 
     37  1.1  riastrad /**
     38  1.1  riastrad  * DOC: Hotplug
     39  1.1  riastrad  *
     40  1.1  riastrad  * Simply put, hotplug occurs when a display is connected to or disconnected
     41  1.1  riastrad  * from the system. However, there may be adapters and docking stations and
     42  1.1  riastrad  * Display Port short pulses and MST devices involved, complicating matters.
     43  1.1  riastrad  *
     44  1.1  riastrad  * Hotplug in i915 is handled in many different levels of abstraction.
     45  1.1  riastrad  *
     46  1.1  riastrad  * The platform dependent interrupt handling code in i915_irq.c enables,
     47  1.1  riastrad  * disables, and does preliminary handling of the interrupts. The interrupt
     48  1.1  riastrad  * handlers gather the hotplug detect (HPD) information from relevant registers
     49  1.1  riastrad  * into a platform independent mask of hotplug pins that have fired.
     50  1.1  riastrad  *
     51  1.1  riastrad  * The platform independent interrupt handler intel_hpd_irq_handler() in
     52  1.1  riastrad  * intel_hotplug.c does hotplug irq storm detection and mitigation, and passes
     53  1.1  riastrad  * further processing to appropriate bottom halves (Display Port specific and
     54  1.1  riastrad  * regular hotplug).
     55  1.1  riastrad  *
     56  1.1  riastrad  * The Display Port work function i915_digport_work_func() calls into
     57  1.1  riastrad  * intel_dp_hpd_pulse() via hooks, which handles DP short pulses and DP MST long
     58  1.1  riastrad  * pulses, with failures and non-MST long pulses triggering regular hotplug
     59  1.1  riastrad  * processing on the connector.
     60  1.1  riastrad  *
     61  1.1  riastrad  * The regular hotplug work function i915_hotplug_work_func() calls connector
     62  1.1  riastrad  * detect hooks, and, if connector status changes, triggers sending of hotplug
     63  1.1  riastrad  * uevent to userspace via drm_kms_helper_hotplug_event().
     64  1.1  riastrad  *
     65  1.1  riastrad  * Finally, the userspace is responsible for triggering a modeset upon receiving
     66  1.1  riastrad  * the hotplug uevent, disabling or enabling the crtc as needed.
     67  1.1  riastrad  *
     68  1.1  riastrad  * The hotplug interrupt storm detection and mitigation code keeps track of the
     69  1.1  riastrad  * number of interrupts per hotplug pin per a period of time, and if the number
     70  1.1  riastrad  * of interrupts exceeds a certain threshold, the interrupt is disabled for a
     71  1.1  riastrad  * while before being re-enabled. The intention is to mitigate issues raising
     72  1.1  riastrad  * from broken hardware triggering massive amounts of interrupts and grinding
     73  1.1  riastrad  * the system to a halt.
     74  1.1  riastrad  *
     75  1.1  riastrad  * Current implementation expects that hotplug interrupt storm will not be
     76  1.1  riastrad  * seen when display port sink is connected, hence on platforms whose DP
     77  1.1  riastrad  * callback is handled by i915_digport_work_func reenabling of hpd is not
     78  1.1  riastrad  * performed (it was never expected to be disabled in the first place ;) )
     79  1.1  riastrad  * this is specific to DP sinks handled by this routine and any other display
     80  1.1  riastrad  * such as HDMI or DVI enabled on the same port will have proper logic since
     81  1.1  riastrad  * it will use i915_hotplug_work_func where this logic is handled.
     82  1.1  riastrad  */
     83  1.1  riastrad 
     84  1.1  riastrad /**
     85  1.1  riastrad  * intel_hpd_pin_default - return default pin associated with certain port.
     86  1.1  riastrad  * @dev_priv: private driver data pointer
     87  1.1  riastrad  * @port: the hpd port to get associated pin
     88  1.1  riastrad  *
     89  1.1  riastrad  * It is only valid and used by digital port encoder.
     90  1.1  riastrad  *
     91  1.1  riastrad  * Return pin that is associatade with @port and HDP_NONE if no pin is
     92  1.1  riastrad  * hard associated with that @port.
     93  1.1  riastrad  */
     94  1.1  riastrad enum hpd_pin intel_hpd_pin_default(struct drm_i915_private *dev_priv,
     95  1.1  riastrad 				   enum port port)
     96  1.1  riastrad {
     97  1.1  riastrad 	switch (port) {
     98  1.1  riastrad 	case PORT_A:
     99  1.1  riastrad 		return HPD_PORT_A;
    100  1.1  riastrad 	case PORT_B:
    101  1.1  riastrad 		return HPD_PORT_B;
    102  1.1  riastrad 	case PORT_C:
    103  1.1  riastrad 		return HPD_PORT_C;
    104  1.1  riastrad 	case PORT_D:
    105  1.1  riastrad 		return HPD_PORT_D;
    106  1.1  riastrad 	case PORT_E:
    107  1.1  riastrad 		return HPD_PORT_E;
    108  1.1  riastrad 	case PORT_F:
    109  1.1  riastrad 		if (IS_CNL_WITH_PORT_F(dev_priv))
    110  1.1  riastrad 			return HPD_PORT_E;
    111  1.1  riastrad 		return HPD_PORT_F;
    112  1.1  riastrad 	case PORT_G:
    113  1.1  riastrad 		return HPD_PORT_G;
    114  1.1  riastrad 	case PORT_H:
    115  1.1  riastrad 		return HPD_PORT_H;
    116  1.1  riastrad 	case PORT_I:
    117  1.1  riastrad 		return HPD_PORT_I;
    118  1.1  riastrad 	default:
    119  1.1  riastrad 		MISSING_CASE(port);
    120  1.1  riastrad 		return HPD_NONE;
    121  1.1  riastrad 	}
    122  1.1  riastrad }
    123  1.1  riastrad 
    124  1.1  riastrad #define HPD_STORM_DETECT_PERIOD		1000
    125  1.1  riastrad #define HPD_STORM_REENABLE_DELAY	(2 * 60 * 1000)
    126  1.1  riastrad #define HPD_RETRY_DELAY			1000
    127  1.1  riastrad 
    128  1.1  riastrad /**
    129  1.1  riastrad  * intel_hpd_irq_storm_detect - gather stats and detect HPD IRQ storm on a pin
    130  1.1  riastrad  * @dev_priv: private driver data pointer
    131  1.1  riastrad  * @pin: the pin to gather stats on
    132  1.1  riastrad  * @long_hpd: whether the HPD IRQ was long or short
    133  1.1  riastrad  *
    134  1.1  riastrad  * Gather stats about HPD IRQs from the specified @pin, and detect IRQ
    135  1.1  riastrad  * storms. Only the pin specific stats and state are changed, the caller is
    136  1.1  riastrad  * responsible for further action.
    137  1.1  riastrad  *
    138  1.1  riastrad  * The number of IRQs that are allowed within @HPD_STORM_DETECT_PERIOD is
    139  1.1  riastrad  * stored in @dev_priv->hotplug.hpd_storm_threshold which defaults to
    140  1.1  riastrad  * @HPD_STORM_DEFAULT_THRESHOLD. Long IRQs count as +10 to this threshold, and
    141  1.1  riastrad  * short IRQs count as +1. If this threshold is exceeded, it's considered an
    142  1.1  riastrad  * IRQ storm and the IRQ state is set to @HPD_MARK_DISABLED.
    143  1.1  riastrad  *
    144  1.1  riastrad  * By default, most systems will only count long IRQs towards
    145  1.1  riastrad  * &dev_priv->hotplug.hpd_storm_threshold. However, some older systems also
    146  1.1  riastrad  * suffer from short IRQ storms and must also track these. Because short IRQ
    147  1.1  riastrad  * storms are naturally caused by sideband interactions with DP MST devices,
    148  1.1  riastrad  * short IRQ detection is only enabled for systems without DP MST support.
    149  1.1  riastrad  * Systems which are new enough to support DP MST are far less likely to
    150  1.1  riastrad  * suffer from IRQ storms at all, so this is fine.
    151  1.1  riastrad  *
    152  1.1  riastrad  * The HPD threshold can be controlled through i915_hpd_storm_ctl in debugfs,
    153  1.1  riastrad  * and should only be adjusted for automated hotplug testing.
    154  1.1  riastrad  *
    155  1.1  riastrad  * Return true if an IRQ storm was detected on @pin.
    156  1.1  riastrad  */
    157  1.1  riastrad static bool intel_hpd_irq_storm_detect(struct drm_i915_private *dev_priv,
    158  1.1  riastrad 				       enum hpd_pin pin, bool long_hpd)
    159  1.1  riastrad {
    160  1.1  riastrad 	struct i915_hotplug *hpd = &dev_priv->hotplug;
    161  1.1  riastrad 	unsigned long start = hpd->stats[pin].last_jiffies;
    162  1.1  riastrad 	unsigned long end = start + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD);
    163  1.1  riastrad 	const int increment = long_hpd ? 10 : 1;
    164  1.1  riastrad 	const int threshold = hpd->hpd_storm_threshold;
    165  1.1  riastrad 	bool storm = false;
    166  1.1  riastrad 
    167  1.1  riastrad 	if (!threshold ||
    168  1.1  riastrad 	    (!long_hpd && !dev_priv->hotplug.hpd_short_storm_enabled))
    169  1.1  riastrad 		return false;
    170  1.1  riastrad 
    171  1.1  riastrad 	if (!time_in_range(jiffies, start, end)) {
    172  1.1  riastrad 		hpd->stats[pin].last_jiffies = jiffies;
    173  1.1  riastrad 		hpd->stats[pin].count = 0;
    174  1.1  riastrad 	}
    175  1.1  riastrad 
    176  1.1  riastrad 	hpd->stats[pin].count += increment;
    177  1.1  riastrad 	if (hpd->stats[pin].count > threshold) {
    178  1.1  riastrad 		hpd->stats[pin].state = HPD_MARK_DISABLED;
    179  1.1  riastrad 		DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", pin);
    180  1.1  riastrad 		storm = true;
    181  1.1  riastrad 	} else {
    182  1.1  riastrad 		DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", pin,
    183  1.1  riastrad 			      hpd->stats[pin].count);
    184  1.1  riastrad 	}
    185  1.1  riastrad 
    186  1.1  riastrad 	return storm;
    187  1.1  riastrad }
    188  1.1  riastrad 
    189  1.1  riastrad static void
    190  1.1  riastrad intel_hpd_irq_storm_switch_to_polling(struct drm_i915_private *dev_priv)
    191  1.1  riastrad {
    192  1.1  riastrad 	struct drm_device *dev = &dev_priv->drm;
    193  1.1  riastrad 	struct intel_connector *intel_connector;
    194  1.1  riastrad 	struct intel_encoder *intel_encoder;
    195  1.1  riastrad 	struct drm_connector *connector;
    196  1.1  riastrad 	struct drm_connector_list_iter conn_iter;
    197  1.1  riastrad 	enum hpd_pin pin;
    198  1.1  riastrad 	bool hpd_disabled = false;
    199  1.1  riastrad 
    200  1.1  riastrad 	lockdep_assert_held(&dev_priv->irq_lock);
    201  1.1  riastrad 
    202  1.1  riastrad 	drm_connector_list_iter_begin(dev, &conn_iter);
    203  1.1  riastrad 	drm_for_each_connector_iter(connector, &conn_iter) {
    204  1.1  riastrad 		if (connector->polled != DRM_CONNECTOR_POLL_HPD)
    205  1.1  riastrad 			continue;
    206  1.1  riastrad 
    207  1.1  riastrad 		intel_connector = to_intel_connector(connector);
    208  1.1  riastrad 		intel_encoder = intel_connector->encoder;
    209  1.1  riastrad 		if (!intel_encoder)
    210  1.1  riastrad 			continue;
    211  1.1  riastrad 
    212  1.1  riastrad 		pin = intel_encoder->hpd_pin;
    213  1.1  riastrad 		if (pin == HPD_NONE ||
    214  1.1  riastrad 		    dev_priv->hotplug.stats[pin].state != HPD_MARK_DISABLED)
    215  1.1  riastrad 			continue;
    216  1.1  riastrad 
    217  1.1  riastrad 		DRM_INFO("HPD interrupt storm detected on connector %s: "
    218  1.1  riastrad 			 "switching from hotplug detection to polling\n",
    219  1.1  riastrad 			 connector->name);
    220  1.1  riastrad 
    221  1.1  riastrad 		dev_priv->hotplug.stats[pin].state = HPD_DISABLED;
    222  1.1  riastrad 		connector->polled = DRM_CONNECTOR_POLL_CONNECT
    223  1.1  riastrad 			| DRM_CONNECTOR_POLL_DISCONNECT;
    224  1.1  riastrad 		hpd_disabled = true;
    225  1.1  riastrad 	}
    226  1.1  riastrad 	drm_connector_list_iter_end(&conn_iter);
    227  1.1  riastrad 
    228  1.1  riastrad 	/* Enable polling and queue hotplug re-enabling. */
    229  1.1  riastrad 	if (hpd_disabled) {
    230  1.1  riastrad 		drm_kms_helper_poll_enable(dev);
    231  1.1  riastrad 		mod_delayed_work(system_wq, &dev_priv->hotplug.reenable_work,
    232  1.1  riastrad 				 msecs_to_jiffies(HPD_STORM_REENABLE_DELAY));
    233  1.1  riastrad 	}
    234  1.1  riastrad }
    235  1.1  riastrad 
    236  1.1  riastrad static void intel_hpd_irq_storm_reenable_work(struct work_struct *work)
    237  1.1  riastrad {
    238  1.1  riastrad 	struct drm_i915_private *dev_priv =
    239  1.1  riastrad 		container_of(work, typeof(*dev_priv),
    240  1.1  riastrad 			     hotplug.reenable_work.work);
    241  1.1  riastrad 	struct drm_device *dev = &dev_priv->drm;
    242  1.1  riastrad 	intel_wakeref_t wakeref;
    243  1.1  riastrad 	enum hpd_pin pin;
    244  1.1  riastrad 
    245  1.1  riastrad 	wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
    246  1.1  riastrad 
    247  1.1  riastrad 	spin_lock_irq(&dev_priv->irq_lock);
    248  1.1  riastrad 	for_each_hpd_pin(pin) {
    249  1.1  riastrad 		struct drm_connector *connector;
    250  1.1  riastrad 		struct drm_connector_list_iter conn_iter;
    251  1.1  riastrad 
    252  1.1  riastrad 		if (dev_priv->hotplug.stats[pin].state != HPD_DISABLED)
    253  1.1  riastrad 			continue;
    254  1.1  riastrad 
    255  1.1  riastrad 		dev_priv->hotplug.stats[pin].state = HPD_ENABLED;
    256  1.1  riastrad 
    257  1.1  riastrad 		drm_connector_list_iter_begin(dev, &conn_iter);
    258  1.1  riastrad 		drm_for_each_connector_iter(connector, &conn_iter) {
    259  1.1  riastrad 			struct intel_connector *intel_connector = to_intel_connector(connector);
    260  1.1  riastrad 
    261  1.1  riastrad 			/* Don't check MST ports, they don't have pins */
    262  1.1  riastrad 			if (!intel_connector->mst_port &&
    263  1.1  riastrad 			    intel_connector->encoder->hpd_pin == pin) {
    264  1.1  riastrad 				if (connector->polled != intel_connector->polled)
    265  1.1  riastrad 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
    266  1.1  riastrad 							 connector->name);
    267  1.1  riastrad 				connector->polled = intel_connector->polled;
    268  1.1  riastrad 				if (!connector->polled)
    269  1.1  riastrad 					connector->polled = DRM_CONNECTOR_POLL_HPD;
    270  1.1  riastrad 			}
    271  1.1  riastrad 		}
    272  1.1  riastrad 		drm_connector_list_iter_end(&conn_iter);
    273  1.1  riastrad 	}
    274  1.1  riastrad 	if (dev_priv->display_irqs_enabled && dev_priv->display.hpd_irq_setup)
    275  1.1  riastrad 		dev_priv->display.hpd_irq_setup(dev_priv);
    276  1.1  riastrad 	spin_unlock_irq(&dev_priv->irq_lock);
    277  1.1  riastrad 
    278  1.1  riastrad 	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
    279  1.1  riastrad }
    280  1.1  riastrad 
    281  1.1  riastrad enum intel_hotplug_state
    282  1.1  riastrad intel_encoder_hotplug(struct intel_encoder *encoder,
    283  1.1  riastrad 		      struct intel_connector *connector,
    284  1.1  riastrad 		      bool irq_received)
    285  1.1  riastrad {
    286  1.1  riastrad 	struct drm_device *dev = connector->base.dev;
    287  1.1  riastrad 	enum drm_connector_status old_status;
    288  1.1  riastrad 
    289  1.1  riastrad 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
    290  1.1  riastrad 	old_status = connector->base.status;
    291  1.1  riastrad 
    292  1.1  riastrad 	connector->base.status =
    293  1.1  riastrad 		drm_helper_probe_detect(&connector->base, NULL, false);
    294  1.1  riastrad 
    295  1.1  riastrad 	if (old_status == connector->base.status)
    296  1.1  riastrad 		return INTEL_HOTPLUG_UNCHANGED;
    297  1.1  riastrad 
    298  1.1  riastrad 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
    299  1.1  riastrad 		      connector->base.base.id,
    300  1.1  riastrad 		      connector->base.name,
    301  1.1  riastrad 		      drm_get_connector_status_name(old_status),
    302  1.1  riastrad 		      drm_get_connector_status_name(connector->base.status));
    303  1.1  riastrad 
    304  1.1  riastrad 	return INTEL_HOTPLUG_CHANGED;
    305  1.1  riastrad }
    306  1.1  riastrad 
    307  1.1  riastrad static bool intel_encoder_has_hpd_pulse(struct intel_encoder *encoder)
    308  1.1  riastrad {
    309  1.1  riastrad 	return intel_encoder_is_dig_port(encoder) &&
    310  1.1  riastrad 		enc_to_dig_port(encoder)->hpd_pulse != NULL;
    311  1.1  riastrad }
    312  1.1  riastrad 
    313  1.1  riastrad static void i915_digport_work_func(struct work_struct *work)
    314  1.1  riastrad {
    315  1.1  riastrad 	struct drm_i915_private *dev_priv =
    316  1.1  riastrad 		container_of(work, struct drm_i915_private, hotplug.dig_port_work);
    317  1.1  riastrad 	u32 long_port_mask, short_port_mask;
    318  1.1  riastrad 	struct intel_encoder *encoder;
    319  1.1  riastrad 	u32 old_bits = 0;
    320  1.1  riastrad 
    321  1.1  riastrad 	spin_lock_irq(&dev_priv->irq_lock);
    322  1.1  riastrad 	long_port_mask = dev_priv->hotplug.long_port_mask;
    323  1.1  riastrad 	dev_priv->hotplug.long_port_mask = 0;
    324  1.1  riastrad 	short_port_mask = dev_priv->hotplug.short_port_mask;
    325  1.1  riastrad 	dev_priv->hotplug.short_port_mask = 0;
    326  1.1  riastrad 	spin_unlock_irq(&dev_priv->irq_lock);
    327  1.1  riastrad 
    328  1.1  riastrad 	for_each_intel_encoder(&dev_priv->drm, encoder) {
    329  1.1  riastrad 		struct intel_digital_port *dig_port;
    330  1.1  riastrad 		enum port port = encoder->port;
    331  1.1  riastrad 		bool long_hpd, short_hpd;
    332  1.1  riastrad 		enum irqreturn ret;
    333  1.1  riastrad 
    334  1.1  riastrad 		if (!intel_encoder_has_hpd_pulse(encoder))
    335  1.1  riastrad 			continue;
    336  1.1  riastrad 
    337  1.1  riastrad 		long_hpd = long_port_mask & BIT(port);
    338  1.1  riastrad 		short_hpd = short_port_mask & BIT(port);
    339  1.1  riastrad 
    340  1.1  riastrad 		if (!long_hpd && !short_hpd)
    341  1.1  riastrad 			continue;
    342  1.1  riastrad 
    343  1.1  riastrad 		dig_port = enc_to_dig_port(encoder);
    344  1.1  riastrad 
    345  1.1  riastrad 		ret = dig_port->hpd_pulse(dig_port, long_hpd);
    346  1.1  riastrad 		if (ret == IRQ_NONE) {
    347  1.1  riastrad 			/* fall back to old school hpd */
    348  1.1  riastrad 			old_bits |= BIT(encoder->hpd_pin);
    349  1.1  riastrad 		}
    350  1.1  riastrad 	}
    351  1.1  riastrad 
    352  1.1  riastrad 	if (old_bits) {
    353  1.1  riastrad 		spin_lock_irq(&dev_priv->irq_lock);
    354  1.1  riastrad 		dev_priv->hotplug.event_bits |= old_bits;
    355  1.1  riastrad 		spin_unlock_irq(&dev_priv->irq_lock);
    356  1.1  riastrad 		queue_delayed_work(system_wq, &dev_priv->hotplug.hotplug_work, 0);
    357  1.1  riastrad 	}
    358  1.1  riastrad }
    359  1.1  riastrad 
    360  1.1  riastrad /*
    361  1.1  riastrad  * Handle hotplug events outside the interrupt handler proper.
    362  1.1  riastrad  */
    363  1.1  riastrad static void i915_hotplug_work_func(struct work_struct *work)
    364  1.1  riastrad {
    365  1.1  riastrad 	struct drm_i915_private *dev_priv =
    366  1.1  riastrad 		container_of(work, struct drm_i915_private,
    367  1.1  riastrad 			     hotplug.hotplug_work.work);
    368  1.1  riastrad 	struct drm_device *dev = &dev_priv->drm;
    369  1.1  riastrad 	struct intel_connector *intel_connector;
    370  1.1  riastrad 	struct intel_encoder *intel_encoder;
    371  1.1  riastrad 	struct drm_connector *connector;
    372  1.1  riastrad 	struct drm_connector_list_iter conn_iter;
    373  1.1  riastrad 	u32 changed = 0, retry = 0;
    374  1.1  riastrad 	u32 hpd_event_bits;
    375  1.1  riastrad 	u32 hpd_retry_bits;
    376  1.1  riastrad 
    377  1.1  riastrad 	mutex_lock(&dev->mode_config.mutex);
    378  1.1  riastrad 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
    379  1.1  riastrad 
    380  1.1  riastrad 	spin_lock_irq(&dev_priv->irq_lock);
    381  1.1  riastrad 
    382  1.1  riastrad 	hpd_event_bits = dev_priv->hotplug.event_bits;
    383  1.1  riastrad 	dev_priv->hotplug.event_bits = 0;
    384  1.1  riastrad 	hpd_retry_bits = dev_priv->hotplug.retry_bits;
    385  1.1  riastrad 	dev_priv->hotplug.retry_bits = 0;
    386  1.1  riastrad 
    387  1.1  riastrad 	/* Enable polling for connectors which had HPD IRQ storms */
    388  1.1  riastrad 	intel_hpd_irq_storm_switch_to_polling(dev_priv);
    389  1.1  riastrad 
    390  1.1  riastrad 	spin_unlock_irq(&dev_priv->irq_lock);
    391  1.1  riastrad 
    392  1.1  riastrad 	drm_connector_list_iter_begin(dev, &conn_iter);
    393  1.1  riastrad 	drm_for_each_connector_iter(connector, &conn_iter) {
    394  1.1  riastrad 		u32 hpd_bit;
    395  1.1  riastrad 
    396  1.1  riastrad 		intel_connector = to_intel_connector(connector);
    397  1.1  riastrad 		if (!intel_connector->encoder)
    398  1.1  riastrad 			continue;
    399  1.1  riastrad 		intel_encoder = intel_connector->encoder;
    400  1.1  riastrad 		hpd_bit = BIT(intel_encoder->hpd_pin);
    401  1.1  riastrad 		if ((hpd_event_bits | hpd_retry_bits) & hpd_bit) {
    402  1.1  riastrad 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
    403  1.1  riastrad 				      connector->name, intel_encoder->hpd_pin);
    404  1.1  riastrad 
    405  1.1  riastrad 			switch (intel_encoder->hotplug(intel_encoder,
    406  1.1  riastrad 						       intel_connector,
    407  1.1  riastrad 						       hpd_event_bits & hpd_bit)) {
    408  1.1  riastrad 			case INTEL_HOTPLUG_UNCHANGED:
    409  1.1  riastrad 				break;
    410  1.1  riastrad 			case INTEL_HOTPLUG_CHANGED:
    411  1.1  riastrad 				changed |= hpd_bit;
    412  1.1  riastrad 				break;
    413  1.1  riastrad 			case INTEL_HOTPLUG_RETRY:
    414  1.1  riastrad 				retry |= hpd_bit;
    415  1.1  riastrad 				break;
    416  1.1  riastrad 			}
    417  1.1  riastrad 		}
    418  1.1  riastrad 	}
    419  1.1  riastrad 	drm_connector_list_iter_end(&conn_iter);
    420  1.1  riastrad 	mutex_unlock(&dev->mode_config.mutex);
    421  1.1  riastrad 
    422  1.1  riastrad 	if (changed)
    423  1.1  riastrad 		drm_kms_helper_hotplug_event(dev);
    424  1.1  riastrad 
    425  1.1  riastrad 	/* Remove shared HPD pins that have changed */
    426  1.1  riastrad 	retry &= ~changed;
    427  1.1  riastrad 	if (retry) {
    428  1.1  riastrad 		spin_lock_irq(&dev_priv->irq_lock);
    429  1.1  riastrad 		dev_priv->hotplug.retry_bits |= retry;
    430  1.1  riastrad 		spin_unlock_irq(&dev_priv->irq_lock);
    431  1.1  riastrad 
    432  1.1  riastrad 		mod_delayed_work(system_wq, &dev_priv->hotplug.hotplug_work,
    433  1.1  riastrad 				 msecs_to_jiffies(HPD_RETRY_DELAY));
    434  1.1  riastrad 	}
    435  1.1  riastrad }
    436  1.1  riastrad 
    437  1.1  riastrad 
    438  1.1  riastrad /**
    439  1.1  riastrad  * intel_hpd_irq_handler - main hotplug irq handler
    440  1.1  riastrad  * @dev_priv: drm_i915_private
    441  1.1  riastrad  * @pin_mask: a mask of hpd pins that have triggered the irq
    442  1.1  riastrad  * @long_mask: a mask of hpd pins that may be long hpd pulses
    443  1.1  riastrad  *
    444  1.1  riastrad  * This is the main hotplug irq handler for all platforms. The platform specific
    445  1.1  riastrad  * irq handlers call the platform specific hotplug irq handlers, which read and
    446  1.1  riastrad  * decode the appropriate registers into bitmasks about hpd pins that have
    447  1.1  riastrad  * triggered (@pin_mask), and which of those pins may be long pulses
    448  1.1  riastrad  * (@long_mask). The @long_mask is ignored if the port corresponding to the pin
    449  1.1  riastrad  * is not a digital port.
    450  1.1  riastrad  *
    451  1.1  riastrad  * Here, we do hotplug irq storm detection and mitigation, and pass further
    452  1.1  riastrad  * processing to appropriate bottom halves.
    453  1.1  riastrad  */
    454  1.1  riastrad void intel_hpd_irq_handler(struct drm_i915_private *dev_priv,
    455  1.1  riastrad 			   u32 pin_mask, u32 long_mask)
    456  1.1  riastrad {
    457  1.1  riastrad 	struct intel_encoder *encoder;
    458  1.1  riastrad 	bool storm_detected = false;
    459  1.1  riastrad 	bool queue_dig = false, queue_hp = false;
    460  1.1  riastrad 	u32 long_hpd_pulse_mask = 0;
    461  1.1  riastrad 	u32 short_hpd_pulse_mask = 0;
    462  1.1  riastrad 	enum hpd_pin pin;
    463  1.1  riastrad 
    464  1.1  riastrad 	if (!pin_mask)
    465  1.1  riastrad 		return;
    466  1.1  riastrad 
    467  1.1  riastrad 	spin_lock(&dev_priv->irq_lock);
    468  1.1  riastrad 
    469  1.1  riastrad 	/*
    470  1.1  riastrad 	 * Determine whether ->hpd_pulse() exists for each pin, and
    471  1.1  riastrad 	 * whether we have a short or a long pulse. This is needed
    472  1.1  riastrad 	 * as each pin may have up to two encoders (HDMI and DP) and
    473  1.1  riastrad 	 * only the one of them (DP) will have ->hpd_pulse().
    474  1.1  riastrad 	 */
    475  1.1  riastrad 	for_each_intel_encoder(&dev_priv->drm, encoder) {
    476  1.1  riastrad 		bool has_hpd_pulse = intel_encoder_has_hpd_pulse(encoder);
    477  1.1  riastrad 		enum port port = encoder->port;
    478  1.1  riastrad 		bool long_hpd;
    479  1.1  riastrad 
    480  1.1  riastrad 		pin = encoder->hpd_pin;
    481  1.1  riastrad 		if (!(BIT(pin) & pin_mask))
    482  1.1  riastrad 			continue;
    483  1.1  riastrad 
    484  1.1  riastrad 		if (!has_hpd_pulse)
    485  1.1  riastrad 			continue;
    486  1.1  riastrad 
    487  1.1  riastrad 		long_hpd = long_mask & BIT(pin);
    488  1.1  riastrad 
    489  1.1  riastrad 		DRM_DEBUG_DRIVER("digital hpd on [ENCODER:%d:%s] - %s\n",
    490  1.1  riastrad 				 encoder->base.base.id, encoder->base.name,
    491  1.1  riastrad 				 long_hpd ? "long" : "short");
    492  1.1  riastrad 		queue_dig = true;
    493  1.1  riastrad 
    494  1.1  riastrad 		if (long_hpd) {
    495  1.1  riastrad 			long_hpd_pulse_mask |= BIT(pin);
    496  1.1  riastrad 			dev_priv->hotplug.long_port_mask |= BIT(port);
    497  1.1  riastrad 		} else {
    498  1.1  riastrad 			short_hpd_pulse_mask |= BIT(pin);
    499  1.1  riastrad 			dev_priv->hotplug.short_port_mask |= BIT(port);
    500  1.1  riastrad 		}
    501  1.1  riastrad 	}
    502  1.1  riastrad 
    503  1.1  riastrad 	/* Now process each pin just once */
    504  1.1  riastrad 	for_each_hpd_pin(pin) {
    505  1.1  riastrad 		bool long_hpd;
    506  1.1  riastrad 
    507  1.1  riastrad 		if (!(BIT(pin) & pin_mask))
    508  1.1  riastrad 			continue;
    509  1.1  riastrad 
    510  1.1  riastrad 		if (dev_priv->hotplug.stats[pin].state == HPD_DISABLED) {
    511  1.1  riastrad 			/*
    512  1.1  riastrad 			 * On GMCH platforms the interrupt mask bits only
    513  1.1  riastrad 			 * prevent irq generation, not the setting of the
    514  1.1  riastrad 			 * hotplug bits itself. So only WARN about unexpected
    515  1.1  riastrad 			 * interrupts on saner platforms.
    516  1.1  riastrad 			 */
    517  1.1  riastrad 			WARN_ONCE(!HAS_GMCH(dev_priv),
    518  1.1  riastrad 				  "Received HPD interrupt on pin %d although disabled\n", pin);
    519  1.1  riastrad 			continue;
    520  1.1  riastrad 		}
    521  1.1  riastrad 
    522  1.1  riastrad 		if (dev_priv->hotplug.stats[pin].state != HPD_ENABLED)
    523  1.1  riastrad 			continue;
    524  1.1  riastrad 
    525  1.1  riastrad 		/*
    526  1.1  riastrad 		 * Delegate to ->hpd_pulse() if one of the encoders for this
    527  1.1  riastrad 		 * pin has it, otherwise let the hotplug_work deal with this
    528  1.1  riastrad 		 * pin directly.
    529  1.1  riastrad 		 */
    530  1.1  riastrad 		if (((short_hpd_pulse_mask | long_hpd_pulse_mask) & BIT(pin))) {
    531  1.1  riastrad 			long_hpd = long_hpd_pulse_mask & BIT(pin);
    532  1.1  riastrad 		} else {
    533  1.1  riastrad 			dev_priv->hotplug.event_bits |= BIT(pin);
    534  1.1  riastrad 			long_hpd = true;
    535  1.1  riastrad 			queue_hp = true;
    536  1.1  riastrad 		}
    537  1.1  riastrad 
    538  1.1  riastrad 		if (intel_hpd_irq_storm_detect(dev_priv, pin, long_hpd)) {
    539  1.1  riastrad 			dev_priv->hotplug.event_bits &= ~BIT(pin);
    540  1.1  riastrad 			storm_detected = true;
    541  1.1  riastrad 			queue_hp = true;
    542  1.1  riastrad 		}
    543  1.1  riastrad 	}
    544  1.1  riastrad 
    545  1.1  riastrad 	/*
    546  1.1  riastrad 	 * Disable any IRQs that storms were detected on. Polling enablement
    547  1.1  riastrad 	 * happens later in our hotplug work.
    548  1.1  riastrad 	 */
    549  1.1  riastrad 	if (storm_detected && dev_priv->display_irqs_enabled)
    550  1.1  riastrad 		dev_priv->display.hpd_irq_setup(dev_priv);
    551  1.1  riastrad 	spin_unlock(&dev_priv->irq_lock);
    552  1.1  riastrad 
    553  1.1  riastrad 	/*
    554  1.1  riastrad 	 * Our hotplug handler can grab modeset locks (by calling down into the
    555  1.1  riastrad 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
    556  1.1  riastrad 	 * queue for otherwise the flush_work in the pageflip code will
    557  1.1  riastrad 	 * deadlock.
    558  1.1  riastrad 	 */
    559  1.1  riastrad 	if (queue_dig)
    560  1.1  riastrad 		queue_work(dev_priv->hotplug.dp_wq, &dev_priv->hotplug.dig_port_work);
    561  1.1  riastrad 	if (queue_hp)
    562  1.1  riastrad 		queue_delayed_work(system_wq, &dev_priv->hotplug.hotplug_work, 0);
    563  1.1  riastrad }
    564  1.1  riastrad 
    565  1.1  riastrad /**
    566  1.1  riastrad  * intel_hpd_init - initializes and enables hpd support
    567  1.1  riastrad  * @dev_priv: i915 device instance
    568  1.1  riastrad  *
    569  1.1  riastrad  * This function enables the hotplug support. It requires that interrupts have
    570  1.1  riastrad  * already been enabled with intel_irq_init_hw(). From this point on hotplug and
    571  1.1  riastrad  * poll request can run concurrently to other code, so locking rules must be
    572  1.1  riastrad  * obeyed.
    573  1.1  riastrad  *
    574  1.1  riastrad  * This is a separate step from interrupt enabling to simplify the locking rules
    575  1.1  riastrad  * in the driver load and resume code.
    576  1.1  riastrad  *
    577  1.1  riastrad  * Also see: intel_hpd_poll_init(), which enables connector polling
    578  1.1  riastrad  */
    579  1.1  riastrad void intel_hpd_init(struct drm_i915_private *dev_priv)
    580  1.1  riastrad {
    581  1.1  riastrad 	int i;
    582  1.1  riastrad 
    583  1.1  riastrad 	for_each_hpd_pin(i) {
    584  1.1  riastrad 		dev_priv->hotplug.stats[i].count = 0;
    585  1.1  riastrad 		dev_priv->hotplug.stats[i].state = HPD_ENABLED;
    586  1.1  riastrad 	}
    587  1.1  riastrad 
    588  1.1  riastrad 	WRITE_ONCE(dev_priv->hotplug.poll_enabled, false);
    589  1.1  riastrad 	schedule_work(&dev_priv->hotplug.poll_init_work);
    590  1.1  riastrad 
    591  1.1  riastrad 	/*
    592  1.1  riastrad 	 * Interrupt setup is already guaranteed to be single-threaded, this is
    593  1.1  riastrad 	 * just to make the assert_spin_locked checks happy.
    594  1.1  riastrad 	 */
    595  1.1  riastrad 	if (dev_priv->display_irqs_enabled && dev_priv->display.hpd_irq_setup) {
    596  1.1  riastrad 		spin_lock_irq(&dev_priv->irq_lock);
    597  1.1  riastrad 		if (dev_priv->display_irqs_enabled)
    598  1.1  riastrad 			dev_priv->display.hpd_irq_setup(dev_priv);
    599  1.1  riastrad 		spin_unlock_irq(&dev_priv->irq_lock);
    600  1.1  riastrad 	}
    601  1.1  riastrad }
    602  1.1  riastrad 
    603  1.1  riastrad static void i915_hpd_poll_init_work(struct work_struct *work)
    604  1.1  riastrad {
    605  1.1  riastrad 	struct drm_i915_private *dev_priv =
    606  1.1  riastrad 		container_of(work, struct drm_i915_private,
    607  1.1  riastrad 			     hotplug.poll_init_work);
    608  1.1  riastrad 	struct drm_device *dev = &dev_priv->drm;
    609  1.1  riastrad 	struct drm_connector *connector;
    610  1.1  riastrad 	struct drm_connector_list_iter conn_iter;
    611  1.1  riastrad 	bool enabled;
    612  1.1  riastrad 
    613  1.1  riastrad 	mutex_lock(&dev->mode_config.mutex);
    614  1.1  riastrad 
    615  1.1  riastrad 	enabled = READ_ONCE(dev_priv->hotplug.poll_enabled);
    616  1.1  riastrad 
    617  1.1  riastrad 	drm_connector_list_iter_begin(dev, &conn_iter);
    618  1.1  riastrad 	drm_for_each_connector_iter(connector, &conn_iter) {
    619  1.1  riastrad 		struct intel_connector *intel_connector =
    620  1.1  riastrad 			to_intel_connector(connector);
    621  1.1  riastrad 		connector->polled = intel_connector->polled;
    622  1.1  riastrad 
    623  1.1  riastrad 		/* MST has a dynamic intel_connector->encoder and it's reprobing
    624  1.1  riastrad 		 * is all handled by the MST helpers. */
    625  1.1  riastrad 		if (intel_connector->mst_port)
    626  1.1  riastrad 			continue;
    627  1.1  riastrad 
    628  1.1  riastrad 		if (!connector->polled && I915_HAS_HOTPLUG(dev_priv) &&
    629  1.1  riastrad 		    intel_connector->encoder->hpd_pin > HPD_NONE) {
    630  1.1  riastrad 			connector->polled = enabled ?
    631  1.1  riastrad 				DRM_CONNECTOR_POLL_CONNECT |
    632  1.1  riastrad 				DRM_CONNECTOR_POLL_DISCONNECT :
    633  1.1  riastrad 				DRM_CONNECTOR_POLL_HPD;
    634  1.1  riastrad 		}
    635  1.1  riastrad 	}
    636  1.1  riastrad 	drm_connector_list_iter_end(&conn_iter);
    637  1.1  riastrad 
    638  1.1  riastrad 	if (enabled)
    639  1.1  riastrad 		drm_kms_helper_poll_enable(dev);
    640  1.1  riastrad 
    641  1.1  riastrad 	mutex_unlock(&dev->mode_config.mutex);
    642  1.1  riastrad 
    643  1.1  riastrad 	/*
    644  1.1  riastrad 	 * We might have missed any hotplugs that happened while we were
    645  1.1  riastrad 	 * in the middle of disabling polling
    646  1.1  riastrad 	 */
    647  1.1  riastrad 	if (!enabled)
    648  1.1  riastrad 		drm_helper_hpd_irq_event(dev);
    649  1.1  riastrad }
    650  1.1  riastrad 
    651  1.1  riastrad /**
    652  1.1  riastrad  * intel_hpd_poll_init - enables/disables polling for connectors with hpd
    653  1.1  riastrad  * @dev_priv: i915 device instance
    654  1.1  riastrad  *
    655  1.1  riastrad  * This function enables polling for all connectors, regardless of whether or
    656  1.1  riastrad  * not they support hotplug detection. Under certain conditions HPD may not be
    657  1.1  riastrad  * functional. On most Intel GPUs, this happens when we enter runtime suspend.
    658  1.1  riastrad  * On Valleyview and Cherryview systems, this also happens when we shut off all
    659  1.1  riastrad  * of the powerwells.
    660  1.1  riastrad  *
    661  1.1  riastrad  * Since this function can get called in contexts where we're already holding
    662  1.1  riastrad  * dev->mode_config.mutex, we do the actual hotplug enabling in a seperate
    663  1.1  riastrad  * worker.
    664  1.1  riastrad  *
    665  1.1  riastrad  * Also see: intel_hpd_init(), which restores hpd handling.
    666  1.1  riastrad  */
    667  1.1  riastrad void intel_hpd_poll_init(struct drm_i915_private *dev_priv)
    668  1.1  riastrad {
    669  1.1  riastrad 	WRITE_ONCE(dev_priv->hotplug.poll_enabled, true);
    670  1.1  riastrad 
    671  1.1  riastrad 	/*
    672  1.1  riastrad 	 * We might already be holding dev->mode_config.mutex, so do this in a
    673  1.1  riastrad 	 * seperate worker
    674  1.1  riastrad 	 * As well, there's no issue if we race here since we always reschedule
    675  1.1  riastrad 	 * this worker anyway
    676  1.1  riastrad 	 */
    677  1.1  riastrad 	schedule_work(&dev_priv->hotplug.poll_init_work);
    678  1.1  riastrad }
    679  1.1  riastrad 
    680  1.1  riastrad void intel_hpd_init_work(struct drm_i915_private *dev_priv)
    681  1.1  riastrad {
    682  1.1  riastrad 	INIT_DELAYED_WORK(&dev_priv->hotplug.hotplug_work,
    683  1.1  riastrad 			  i915_hotplug_work_func);
    684  1.1  riastrad 	INIT_WORK(&dev_priv->hotplug.dig_port_work, i915_digport_work_func);
    685  1.1  riastrad 	INIT_WORK(&dev_priv->hotplug.poll_init_work, i915_hpd_poll_init_work);
    686  1.1  riastrad 	INIT_DELAYED_WORK(&dev_priv->hotplug.reenable_work,
    687  1.1  riastrad 			  intel_hpd_irq_storm_reenable_work);
    688  1.1  riastrad }
    689  1.1  riastrad 
    690  1.1  riastrad void intel_hpd_cancel_work(struct drm_i915_private *dev_priv)
    691  1.1  riastrad {
    692  1.1  riastrad 	spin_lock_irq(&dev_priv->irq_lock);
    693  1.1  riastrad 
    694  1.1  riastrad 	dev_priv->hotplug.long_port_mask = 0;
    695  1.1  riastrad 	dev_priv->hotplug.short_port_mask = 0;
    696  1.1  riastrad 	dev_priv->hotplug.event_bits = 0;
    697  1.1  riastrad 	dev_priv->hotplug.retry_bits = 0;
    698  1.1  riastrad 
    699  1.1  riastrad 	spin_unlock_irq(&dev_priv->irq_lock);
    700  1.1  riastrad 
    701  1.1  riastrad 	cancel_work_sync(&dev_priv->hotplug.dig_port_work);
    702  1.1  riastrad 	cancel_delayed_work_sync(&dev_priv->hotplug.hotplug_work);
    703  1.1  riastrad 	cancel_work_sync(&dev_priv->hotplug.poll_init_work);
    704  1.1  riastrad 	cancel_delayed_work_sync(&dev_priv->hotplug.reenable_work);
    705  1.1  riastrad }
    706  1.1  riastrad 
    707  1.1  riastrad bool intel_hpd_disable(struct drm_i915_private *dev_priv, enum hpd_pin pin)
    708  1.1  riastrad {
    709  1.1  riastrad 	bool ret = false;
    710  1.1  riastrad 
    711  1.1  riastrad 	if (pin == HPD_NONE)
    712  1.1  riastrad 		return false;
    713  1.1  riastrad 
    714  1.1  riastrad 	spin_lock_irq(&dev_priv->irq_lock);
    715  1.1  riastrad 	if (dev_priv->hotplug.stats[pin].state == HPD_ENABLED) {
    716  1.1  riastrad 		dev_priv->hotplug.stats[pin].state = HPD_DISABLED;
    717  1.1  riastrad 		ret = true;
    718  1.1  riastrad 	}
    719  1.1  riastrad 	spin_unlock_irq(&dev_priv->irq_lock);
    720  1.1  riastrad 
    721  1.1  riastrad 	return ret;
    722  1.1  riastrad }
    723  1.1  riastrad 
    724  1.1  riastrad void intel_hpd_enable(struct drm_i915_private *dev_priv, enum hpd_pin pin)
    725  1.1  riastrad {
    726  1.1  riastrad 	if (pin == HPD_NONE)
    727  1.1  riastrad 		return;
    728  1.1  riastrad 
    729  1.1  riastrad 	spin_lock_irq(&dev_priv->irq_lock);
    730  1.1  riastrad 	dev_priv->hotplug.stats[pin].state = HPD_ENABLED;
    731  1.1  riastrad 	spin_unlock_irq(&dev_priv->irq_lock);
    732  1.1  riastrad }
    733