Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Surface Book (2 and later) hot-plug driver.
0004  *
0005  * Surface Book devices (can) have a hot-pluggable discrete GPU (dGPU). This
0006  * driver is responsible for out-of-band hot-plug event signaling on these
0007  * devices. It is specifically required when the hot-plug device is in D3cold
0008  * and can thus not generate PCIe hot-plug events itself.
0009  *
0010  * Event signaling is handled via ACPI, which will generate the appropriate
0011  * device-check notifications to be picked up by the PCIe hot-plug driver.
0012  *
0013  * Copyright (C) 2019-2022 Maximilian Luz <luzmaximilian@gmail.com>
0014  */
0015 
0016 #include <linux/acpi.h>
0017 #include <linux/gpio.h>
0018 #include <linux/interrupt.h>
0019 #include <linux/kernel.h>
0020 #include <linux/module.h>
0021 #include <linux/mutex.h>
0022 #include <linux/platform_device.h>
0023 
0024 static const struct acpi_gpio_params shps_base_presence_int   = { 0, 0, false };
0025 static const struct acpi_gpio_params shps_base_presence       = { 1, 0, false };
0026 static const struct acpi_gpio_params shps_device_power_int    = { 2, 0, false };
0027 static const struct acpi_gpio_params shps_device_power        = { 3, 0, false };
0028 static const struct acpi_gpio_params shps_device_presence_int = { 4, 0, false };
0029 static const struct acpi_gpio_params shps_device_presence     = { 5, 0, false };
0030 
0031 static const struct acpi_gpio_mapping shps_acpi_gpios[] = {
0032     { "base_presence-int-gpio",   &shps_base_presence_int,   1 },
0033     { "base_presence-gpio",       &shps_base_presence,       1 },
0034     { "device_power-int-gpio",    &shps_device_power_int,    1 },
0035     { "device_power-gpio",        &shps_device_power,        1 },
0036     { "device_presence-int-gpio", &shps_device_presence_int, 1 },
0037     { "device_presence-gpio",     &shps_device_presence,     1 },
0038     { },
0039 };
0040 
0041 /* 5515a847-ed55-4b27-8352-cd320e10360a */
0042 static const guid_t shps_dsm_guid =
0043     GUID_INIT(0x5515a847, 0xed55, 0x4b27, 0x83, 0x52, 0xcd, 0x32, 0x0e, 0x10, 0x36, 0x0a);
0044 
0045 #define SHPS_DSM_REVISION       1
0046 
0047 enum shps_dsm_fn {
0048     SHPS_DSM_FN_PCI_NUM_ENTRIES = 0x01,
0049     SHPS_DSM_FN_PCI_GET_ENTRIES = 0x02,
0050     SHPS_DSM_FN_IRQ_BASE_PRESENCE   = 0x03,
0051     SHPS_DSM_FN_IRQ_DEVICE_POWER    = 0x04,
0052     SHPS_DSM_FN_IRQ_DEVICE_PRESENCE = 0x05,
0053 };
0054 
0055 enum shps_irq_type {
0056     /* NOTE: Must be in order of enum shps_dsm_fn above. */
0057     SHPS_IRQ_TYPE_BASE_PRESENCE = 0,
0058     SHPS_IRQ_TYPE_DEVICE_POWER  = 1,
0059     SHPS_IRQ_TYPE_DEVICE_PRESENCE   = 2,
0060     SHPS_NUM_IRQS,
0061 };
0062 
0063 static const char *const shps_gpio_names[] = {
0064     [SHPS_IRQ_TYPE_BASE_PRESENCE]   = "base_presence",
0065     [SHPS_IRQ_TYPE_DEVICE_POWER]    = "device_power",
0066     [SHPS_IRQ_TYPE_DEVICE_PRESENCE] = "device_presence",
0067 };
0068 
0069 struct shps_device {
0070     struct mutex lock[SHPS_NUM_IRQS];  /* Protects update in shps_dsm_notify_irq() */
0071     struct gpio_desc *gpio[SHPS_NUM_IRQS];
0072     unsigned int irq[SHPS_NUM_IRQS];
0073 };
0074 
0075 #define SHPS_IRQ_NOT_PRESENT        ((unsigned int)-1)
0076 
0077 static enum shps_dsm_fn shps_dsm_fn_for_irq(enum shps_irq_type type)
0078 {
0079     return SHPS_DSM_FN_IRQ_BASE_PRESENCE + type;
0080 }
0081 
0082 static void shps_dsm_notify_irq(struct platform_device *pdev, enum shps_irq_type type)
0083 {
0084     struct shps_device *sdev = platform_get_drvdata(pdev);
0085     acpi_handle handle = ACPI_HANDLE(&pdev->dev);
0086     union acpi_object *result;
0087     union acpi_object param;
0088     int value;
0089 
0090     mutex_lock(&sdev->lock[type]);
0091 
0092     value = gpiod_get_value_cansleep(sdev->gpio[type]);
0093     if (value < 0) {
0094         mutex_unlock(&sdev->lock[type]);
0095         dev_err(&pdev->dev, "failed to get gpio: %d (irq=%d)\n", type, value);
0096         return;
0097     }
0098 
0099     dev_dbg(&pdev->dev, "IRQ notification via DSM (irq=%d, value=%d)\n", type, value);
0100 
0101     param.type = ACPI_TYPE_INTEGER;
0102     param.integer.value = value;
0103 
0104     result = acpi_evaluate_dsm(handle, &shps_dsm_guid, SHPS_DSM_REVISION,
0105                    shps_dsm_fn_for_irq(type), &param);
0106 
0107     if (!result) {
0108         dev_err(&pdev->dev, "IRQ notification via DSM failed (irq=%d, gpio=%d)\n",
0109             type, value);
0110 
0111     } else if (result->type != ACPI_TYPE_BUFFER) {
0112         dev_err(&pdev->dev,
0113             "IRQ notification via DSM failed: unexpected result type (irq=%d, gpio=%d)\n",
0114             type, value);
0115 
0116     } else if (result->buffer.length != 1 || result->buffer.pointer[0] != 0) {
0117         dev_err(&pdev->dev,
0118             "IRQ notification via DSM failed: unexpected result value (irq=%d, gpio=%d)\n",
0119             type, value);
0120     }
0121 
0122     mutex_unlock(&sdev->lock[type]);
0123 
0124     if (result)
0125         ACPI_FREE(result);
0126 }
0127 
0128 static irqreturn_t shps_handle_irq(int irq, void *data)
0129 {
0130     struct platform_device *pdev = data;
0131     struct shps_device *sdev = platform_get_drvdata(pdev);
0132     int type;
0133 
0134     /* Figure out which IRQ we're handling. */
0135     for (type = 0; type < SHPS_NUM_IRQS; type++)
0136         if (irq == sdev->irq[type])
0137             break;
0138 
0139     /* We should have found our interrupt, if not: this is a bug. */
0140     if (WARN(type >= SHPS_NUM_IRQS, "invalid IRQ number: %d\n", irq))
0141         return IRQ_HANDLED;
0142 
0143     /* Forward interrupt to ACPI via DSM. */
0144     shps_dsm_notify_irq(pdev, type);
0145     return IRQ_HANDLED;
0146 }
0147 
0148 static int shps_setup_irq(struct platform_device *pdev, enum shps_irq_type type)
0149 {
0150     unsigned long flags = IRQF_ONESHOT | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
0151     struct shps_device *sdev = platform_get_drvdata(pdev);
0152     struct gpio_desc *gpiod;
0153     acpi_handle handle = ACPI_HANDLE(&pdev->dev);
0154     const char *irq_name;
0155     const int dsm = shps_dsm_fn_for_irq(type);
0156     int status, irq;
0157 
0158     /*
0159      * Only set up interrupts that we actually need: The Surface Book 3
0160      * does not have a DSM for base presence, so don't set up an interrupt
0161      * for that.
0162      */
0163     if (!acpi_check_dsm(handle, &shps_dsm_guid, SHPS_DSM_REVISION, BIT(dsm))) {
0164         dev_dbg(&pdev->dev, "IRQ notification via DSM not present (irq=%d)\n", type);
0165         return 0;
0166     }
0167 
0168     gpiod = devm_gpiod_get(&pdev->dev, shps_gpio_names[type], GPIOD_ASIS);
0169     if (IS_ERR(gpiod))
0170         return PTR_ERR(gpiod);
0171 
0172     irq = gpiod_to_irq(gpiod);
0173     if (irq < 0)
0174         return irq;
0175 
0176     irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "shps-irq-%d", type);
0177     if (!irq_name)
0178         return -ENOMEM;
0179 
0180     status = devm_request_threaded_irq(&pdev->dev, irq, NULL, shps_handle_irq,
0181                        flags, irq_name, pdev);
0182     if (status)
0183         return status;
0184 
0185     dev_dbg(&pdev->dev, "set up irq %d as type %d\n", irq, type);
0186 
0187     sdev->gpio[type] = gpiod;
0188     sdev->irq[type] = irq;
0189 
0190     return 0;
0191 }
0192 
0193 static int surface_hotplug_remove(struct platform_device *pdev)
0194 {
0195     struct shps_device *sdev = platform_get_drvdata(pdev);
0196     int i;
0197 
0198     /* Ensure that IRQs have been fully handled and won't trigger any more. */
0199     for (i = 0; i < SHPS_NUM_IRQS; i++) {
0200         if (sdev->irq[i] != SHPS_IRQ_NOT_PRESENT)
0201             disable_irq(sdev->irq[i]);
0202 
0203         mutex_destroy(&sdev->lock[i]);
0204     }
0205 
0206     return 0;
0207 }
0208 
0209 static int surface_hotplug_probe(struct platform_device *pdev)
0210 {
0211     struct shps_device *sdev;
0212     int status, i;
0213 
0214     /*
0215      * The MSHW0153 device is also present on the Surface Laptop 3,
0216      * however that doesn't have a hot-pluggable PCIe device. It also
0217      * doesn't have any GPIO interrupts/pins under the MSHW0153, so filter
0218      * it out here.
0219      */
0220     if (gpiod_count(&pdev->dev, NULL) < 0)
0221         return -ENODEV;
0222 
0223     status = devm_acpi_dev_add_driver_gpios(&pdev->dev, shps_acpi_gpios);
0224     if (status)
0225         return status;
0226 
0227     sdev = devm_kzalloc(&pdev->dev, sizeof(*sdev), GFP_KERNEL);
0228     if (!sdev)
0229         return -ENOMEM;
0230 
0231     platform_set_drvdata(pdev, sdev);
0232 
0233     /*
0234      * Initialize IRQs so that we can safely call surface_hotplug_remove()
0235      * on errors.
0236      */
0237     for (i = 0; i < SHPS_NUM_IRQS; i++)
0238         sdev->irq[i] = SHPS_IRQ_NOT_PRESENT;
0239 
0240     /* Set up IRQs. */
0241     for (i = 0; i < SHPS_NUM_IRQS; i++) {
0242         mutex_init(&sdev->lock[i]);
0243 
0244         status = shps_setup_irq(pdev, i);
0245         if (status) {
0246             dev_err(&pdev->dev, "failed to set up IRQ %d: %d\n", i, status);
0247             goto err;
0248         }
0249     }
0250 
0251     /* Ensure everything is up-to-date. */
0252     for (i = 0; i < SHPS_NUM_IRQS; i++)
0253         if (sdev->irq[i] != SHPS_IRQ_NOT_PRESENT)
0254             shps_dsm_notify_irq(pdev, i);
0255 
0256     return 0;
0257 
0258 err:
0259     surface_hotplug_remove(pdev);
0260     return status;
0261 }
0262 
0263 static const struct acpi_device_id surface_hotplug_acpi_match[] = {
0264     { "MSHW0153", 0 },
0265     { },
0266 };
0267 MODULE_DEVICE_TABLE(acpi, surface_hotplug_acpi_match);
0268 
0269 static struct platform_driver surface_hotplug_driver = {
0270     .probe = surface_hotplug_probe,
0271     .remove = surface_hotplug_remove,
0272     .driver = {
0273         .name = "surface_hotplug",
0274         .acpi_match_table = surface_hotplug_acpi_match,
0275         .probe_type = PROBE_PREFER_ASYNCHRONOUS,
0276     },
0277 };
0278 module_platform_driver(surface_hotplug_driver);
0279 
0280 MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
0281 MODULE_DESCRIPTION("Surface Hot-Plug Signaling Driver for Surface Book Devices");
0282 MODULE_LICENSE("GPL");