surface_hotplug.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Surface Book (2 and later) hot-plug driver.
  4. *
  5. * Surface Book devices (can) have a hot-pluggable discrete GPU (dGPU). This
  6. * driver is responsible for out-of-band hot-plug event signaling on these
  7. * devices. It is specifically required when the hot-plug device is in D3cold
  8. * and can thus not generate PCIe hot-plug events itself.
  9. *
  10. * Event signaling is handled via ACPI, which will generate the appropriate
  11. * device-check notifications to be picked up by the PCIe hot-plug driver.
  12. *
  13. * Copyright (C) 2019-2022 Maximilian Luz <luzmaximilian@gmail.com>
  14. */
  15. #include <linux/acpi.h>
  16. #include <linux/gpio.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/mutex.h>
  21. #include <linux/platform_device.h>
  22. static const struct acpi_gpio_params shps_base_presence_int = { 0, 0, false };
  23. static const struct acpi_gpio_params shps_base_presence = { 1, 0, false };
  24. static const struct acpi_gpio_params shps_device_power_int = { 2, 0, false };
  25. static const struct acpi_gpio_params shps_device_power = { 3, 0, false };
  26. static const struct acpi_gpio_params shps_device_presence_int = { 4, 0, false };
  27. static const struct acpi_gpio_params shps_device_presence = { 5, 0, false };
  28. static const struct acpi_gpio_mapping shps_acpi_gpios[] = {
  29. { "base_presence-int-gpio", &shps_base_presence_int, 1 },
  30. { "base_presence-gpio", &shps_base_presence, 1 },
  31. { "device_power-int-gpio", &shps_device_power_int, 1 },
  32. { "device_power-gpio", &shps_device_power, 1 },
  33. { "device_presence-int-gpio", &shps_device_presence_int, 1 },
  34. { "device_presence-gpio", &shps_device_presence, 1 },
  35. { },
  36. };
  37. /* 5515a847-ed55-4b27-8352-cd320e10360a */
  38. static const guid_t shps_dsm_guid =
  39. GUID_INIT(0x5515a847, 0xed55, 0x4b27, 0x83, 0x52, 0xcd, 0x32, 0x0e, 0x10, 0x36, 0x0a);
  40. #define SHPS_DSM_REVISION 1
  41. enum shps_dsm_fn {
  42. SHPS_DSM_FN_PCI_NUM_ENTRIES = 0x01,
  43. SHPS_DSM_FN_PCI_GET_ENTRIES = 0x02,
  44. SHPS_DSM_FN_IRQ_BASE_PRESENCE = 0x03,
  45. SHPS_DSM_FN_IRQ_DEVICE_POWER = 0x04,
  46. SHPS_DSM_FN_IRQ_DEVICE_PRESENCE = 0x05,
  47. };
  48. enum shps_irq_type {
  49. /* NOTE: Must be in order of enum shps_dsm_fn above. */
  50. SHPS_IRQ_TYPE_BASE_PRESENCE = 0,
  51. SHPS_IRQ_TYPE_DEVICE_POWER = 1,
  52. SHPS_IRQ_TYPE_DEVICE_PRESENCE = 2,
  53. SHPS_NUM_IRQS,
  54. };
  55. static const char *const shps_gpio_names[] = {
  56. [SHPS_IRQ_TYPE_BASE_PRESENCE] = "base_presence",
  57. [SHPS_IRQ_TYPE_DEVICE_POWER] = "device_power",
  58. [SHPS_IRQ_TYPE_DEVICE_PRESENCE] = "device_presence",
  59. };
  60. struct shps_device {
  61. struct mutex lock[SHPS_NUM_IRQS]; /* Protects update in shps_dsm_notify_irq() */
  62. struct gpio_desc *gpio[SHPS_NUM_IRQS];
  63. unsigned int irq[SHPS_NUM_IRQS];
  64. };
  65. #define SHPS_IRQ_NOT_PRESENT ((unsigned int)-1)
  66. static enum shps_dsm_fn shps_dsm_fn_for_irq(enum shps_irq_type type)
  67. {
  68. return SHPS_DSM_FN_IRQ_BASE_PRESENCE + type;
  69. }
  70. static void shps_dsm_notify_irq(struct platform_device *pdev, enum shps_irq_type type)
  71. {
  72. struct shps_device *sdev = platform_get_drvdata(pdev);
  73. acpi_handle handle = ACPI_HANDLE(&pdev->dev);
  74. union acpi_object *result;
  75. union acpi_object param;
  76. int value;
  77. mutex_lock(&sdev->lock[type]);
  78. value = gpiod_get_value_cansleep(sdev->gpio[type]);
  79. if (value < 0) {
  80. mutex_unlock(&sdev->lock[type]);
  81. dev_err(&pdev->dev, "failed to get gpio: %d (irq=%d)\n", type, value);
  82. return;
  83. }
  84. dev_dbg(&pdev->dev, "IRQ notification via DSM (irq=%d, value=%d)\n", type, value);
  85. param.type = ACPI_TYPE_INTEGER;
  86. param.integer.value = value;
  87. result = acpi_evaluate_dsm_typed(handle, &shps_dsm_guid, SHPS_DSM_REVISION,
  88. shps_dsm_fn_for_irq(type), &param, ACPI_TYPE_BUFFER);
  89. if (!result) {
  90. dev_err(&pdev->dev, "IRQ notification via DSM failed (irq=%d, gpio=%d)\n",
  91. type, value);
  92. } else if (result->buffer.length != 1 || result->buffer.pointer[0] != 0) {
  93. dev_err(&pdev->dev,
  94. "IRQ notification via DSM failed: unexpected result value (irq=%d, gpio=%d)\n",
  95. type, value);
  96. }
  97. mutex_unlock(&sdev->lock[type]);
  98. ACPI_FREE(result);
  99. }
  100. static irqreturn_t shps_handle_irq(int irq, void *data)
  101. {
  102. struct platform_device *pdev = data;
  103. struct shps_device *sdev = platform_get_drvdata(pdev);
  104. int type;
  105. /* Figure out which IRQ we're handling. */
  106. for (type = 0; type < SHPS_NUM_IRQS; type++)
  107. if (irq == sdev->irq[type])
  108. break;
  109. /* We should have found our interrupt, if not: this is a bug. */
  110. if (WARN(type >= SHPS_NUM_IRQS, "invalid IRQ number: %d\n", irq))
  111. return IRQ_HANDLED;
  112. /* Forward interrupt to ACPI via DSM. */
  113. shps_dsm_notify_irq(pdev, type);
  114. return IRQ_HANDLED;
  115. }
  116. static int shps_setup_irq(struct platform_device *pdev, enum shps_irq_type type)
  117. {
  118. unsigned long flags = IRQF_ONESHOT | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
  119. struct shps_device *sdev = platform_get_drvdata(pdev);
  120. struct gpio_desc *gpiod;
  121. acpi_handle handle = ACPI_HANDLE(&pdev->dev);
  122. const char *irq_name;
  123. const int dsm = shps_dsm_fn_for_irq(type);
  124. int status, irq;
  125. /*
  126. * Only set up interrupts that we actually need: The Surface Book 3
  127. * does not have a DSM for base presence, so don't set up an interrupt
  128. * for that.
  129. */
  130. if (!acpi_check_dsm(handle, &shps_dsm_guid, SHPS_DSM_REVISION, BIT(dsm))) {
  131. dev_dbg(&pdev->dev, "IRQ notification via DSM not present (irq=%d)\n", type);
  132. return 0;
  133. }
  134. gpiod = devm_gpiod_get(&pdev->dev, shps_gpio_names[type], GPIOD_ASIS);
  135. if (IS_ERR(gpiod))
  136. return PTR_ERR(gpiod);
  137. irq = gpiod_to_irq(gpiod);
  138. if (irq < 0)
  139. return irq;
  140. irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "shps-irq-%d", type);
  141. if (!irq_name)
  142. return -ENOMEM;
  143. status = devm_request_threaded_irq(&pdev->dev, irq, NULL, shps_handle_irq,
  144. flags, irq_name, pdev);
  145. if (status)
  146. return status;
  147. dev_dbg(&pdev->dev, "set up irq %d as type %d\n", irq, type);
  148. sdev->gpio[type] = gpiod;
  149. sdev->irq[type] = irq;
  150. return 0;
  151. }
  152. static void surface_hotplug_remove(struct platform_device *pdev)
  153. {
  154. struct shps_device *sdev = platform_get_drvdata(pdev);
  155. int i;
  156. /* Ensure that IRQs have been fully handled and won't trigger any more. */
  157. for (i = 0; i < SHPS_NUM_IRQS; i++) {
  158. if (sdev->irq[i] != SHPS_IRQ_NOT_PRESENT)
  159. disable_irq(sdev->irq[i]);
  160. mutex_destroy(&sdev->lock[i]);
  161. }
  162. }
  163. static int surface_hotplug_probe(struct platform_device *pdev)
  164. {
  165. struct shps_device *sdev;
  166. int status, i;
  167. /*
  168. * The MSHW0153 device is also present on the Surface Laptop 3,
  169. * however that doesn't have a hot-pluggable PCIe device. It also
  170. * doesn't have any GPIO interrupts/pins under the MSHW0153, so filter
  171. * it out here.
  172. */
  173. if (gpiod_count(&pdev->dev, NULL) < 0)
  174. return -ENODEV;
  175. status = devm_acpi_dev_add_driver_gpios(&pdev->dev, shps_acpi_gpios);
  176. if (status)
  177. return status;
  178. sdev = devm_kzalloc(&pdev->dev, sizeof(*sdev), GFP_KERNEL);
  179. if (!sdev)
  180. return -ENOMEM;
  181. platform_set_drvdata(pdev, sdev);
  182. /*
  183. * Initialize IRQs so that we can safely call surface_hotplug_remove()
  184. * on errors.
  185. */
  186. for (i = 0; i < SHPS_NUM_IRQS; i++)
  187. sdev->irq[i] = SHPS_IRQ_NOT_PRESENT;
  188. /* Set up IRQs. */
  189. for (i = 0; i < SHPS_NUM_IRQS; i++) {
  190. mutex_init(&sdev->lock[i]);
  191. status = shps_setup_irq(pdev, i);
  192. if (status) {
  193. dev_err(&pdev->dev, "failed to set up IRQ %d: %d\n", i, status);
  194. goto err;
  195. }
  196. }
  197. /* Ensure everything is up-to-date. */
  198. for (i = 0; i < SHPS_NUM_IRQS; i++)
  199. if (sdev->irq[i] != SHPS_IRQ_NOT_PRESENT)
  200. shps_dsm_notify_irq(pdev, i);
  201. return 0;
  202. err:
  203. surface_hotplug_remove(pdev);
  204. return status;
  205. }
  206. static const struct acpi_device_id surface_hotplug_acpi_match[] = {
  207. { "MSHW0153", 0 },
  208. { },
  209. };
  210. MODULE_DEVICE_TABLE(acpi, surface_hotplug_acpi_match);
  211. static struct platform_driver surface_hotplug_driver = {
  212. .probe = surface_hotplug_probe,
  213. .remove = surface_hotplug_remove,
  214. .driver = {
  215. .name = "surface_hotplug",
  216. .acpi_match_table = surface_hotplug_acpi_match,
  217. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  218. },
  219. };
  220. module_platform_driver(surface_hotplug_driver);
  221. MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
  222. MODULE_DESCRIPTION("Surface Hot-Plug Signaling Driver for Surface Book Devices");
  223. MODULE_LICENSE("GPL");