surface_hotplug.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 <[email protected]>
  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(handle, &shps_dsm_guid, SHPS_DSM_REVISION,
  88. shps_dsm_fn_for_irq(type), &param);
  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->type != ACPI_TYPE_BUFFER) {
  93. dev_err(&pdev->dev,
  94. "IRQ notification via DSM failed: unexpected result type (irq=%d, gpio=%d)\n",
  95. type, value);
  96. } else if (result->buffer.length != 1 || result->buffer.pointer[0] != 0) {
  97. dev_err(&pdev->dev,
  98. "IRQ notification via DSM failed: unexpected result value (irq=%d, gpio=%d)\n",
  99. type, value);
  100. }
  101. mutex_unlock(&sdev->lock[type]);
  102. if (result)
  103. ACPI_FREE(result);
  104. }
  105. static irqreturn_t shps_handle_irq(int irq, void *data)
  106. {
  107. struct platform_device *pdev = data;
  108. struct shps_device *sdev = platform_get_drvdata(pdev);
  109. int type;
  110. /* Figure out which IRQ we're handling. */
  111. for (type = 0; type < SHPS_NUM_IRQS; type++)
  112. if (irq == sdev->irq[type])
  113. break;
  114. /* We should have found our interrupt, if not: this is a bug. */
  115. if (WARN(type >= SHPS_NUM_IRQS, "invalid IRQ number: %d\n", irq))
  116. return IRQ_HANDLED;
  117. /* Forward interrupt to ACPI via DSM. */
  118. shps_dsm_notify_irq(pdev, type);
  119. return IRQ_HANDLED;
  120. }
  121. static int shps_setup_irq(struct platform_device *pdev, enum shps_irq_type type)
  122. {
  123. unsigned long flags = IRQF_ONESHOT | IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
  124. struct shps_device *sdev = platform_get_drvdata(pdev);
  125. struct gpio_desc *gpiod;
  126. acpi_handle handle = ACPI_HANDLE(&pdev->dev);
  127. const char *irq_name;
  128. const int dsm = shps_dsm_fn_for_irq(type);
  129. int status, irq;
  130. /*
  131. * Only set up interrupts that we actually need: The Surface Book 3
  132. * does not have a DSM for base presence, so don't set up an interrupt
  133. * for that.
  134. */
  135. if (!acpi_check_dsm(handle, &shps_dsm_guid, SHPS_DSM_REVISION, BIT(dsm))) {
  136. dev_dbg(&pdev->dev, "IRQ notification via DSM not present (irq=%d)\n", type);
  137. return 0;
  138. }
  139. gpiod = devm_gpiod_get(&pdev->dev, shps_gpio_names[type], GPIOD_ASIS);
  140. if (IS_ERR(gpiod))
  141. return PTR_ERR(gpiod);
  142. irq = gpiod_to_irq(gpiod);
  143. if (irq < 0)
  144. return irq;
  145. irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "shps-irq-%d", type);
  146. if (!irq_name)
  147. return -ENOMEM;
  148. status = devm_request_threaded_irq(&pdev->dev, irq, NULL, shps_handle_irq,
  149. flags, irq_name, pdev);
  150. if (status)
  151. return status;
  152. dev_dbg(&pdev->dev, "set up irq %d as type %d\n", irq, type);
  153. sdev->gpio[type] = gpiod;
  154. sdev->irq[type] = irq;
  155. return 0;
  156. }
  157. static int surface_hotplug_remove(struct platform_device *pdev)
  158. {
  159. struct shps_device *sdev = platform_get_drvdata(pdev);
  160. int i;
  161. /* Ensure that IRQs have been fully handled and won't trigger any more. */
  162. for (i = 0; i < SHPS_NUM_IRQS; i++) {
  163. if (sdev->irq[i] != SHPS_IRQ_NOT_PRESENT)
  164. disable_irq(sdev->irq[i]);
  165. mutex_destroy(&sdev->lock[i]);
  166. }
  167. return 0;
  168. }
  169. static int surface_hotplug_probe(struct platform_device *pdev)
  170. {
  171. struct shps_device *sdev;
  172. int status, i;
  173. /*
  174. * The MSHW0153 device is also present on the Surface Laptop 3,
  175. * however that doesn't have a hot-pluggable PCIe device. It also
  176. * doesn't have any GPIO interrupts/pins under the MSHW0153, so filter
  177. * it out here.
  178. */
  179. if (gpiod_count(&pdev->dev, NULL) < 0)
  180. return -ENODEV;
  181. status = devm_acpi_dev_add_driver_gpios(&pdev->dev, shps_acpi_gpios);
  182. if (status)
  183. return status;
  184. sdev = devm_kzalloc(&pdev->dev, sizeof(*sdev), GFP_KERNEL);
  185. if (!sdev)
  186. return -ENOMEM;
  187. platform_set_drvdata(pdev, sdev);
  188. /*
  189. * Initialize IRQs so that we can safely call surface_hotplug_remove()
  190. * on errors.
  191. */
  192. for (i = 0; i < SHPS_NUM_IRQS; i++)
  193. sdev->irq[i] = SHPS_IRQ_NOT_PRESENT;
  194. /* Set up IRQs. */
  195. for (i = 0; i < SHPS_NUM_IRQS; i++) {
  196. mutex_init(&sdev->lock[i]);
  197. status = shps_setup_irq(pdev, i);
  198. if (status) {
  199. dev_err(&pdev->dev, "failed to set up IRQ %d: %d\n", i, status);
  200. goto err;
  201. }
  202. }
  203. /* Ensure everything is up-to-date. */
  204. for (i = 0; i < SHPS_NUM_IRQS; i++)
  205. if (sdev->irq[i] != SHPS_IRQ_NOT_PRESENT)
  206. shps_dsm_notify_irq(pdev, i);
  207. return 0;
  208. err:
  209. surface_hotplug_remove(pdev);
  210. return status;
  211. }
  212. static const struct acpi_device_id surface_hotplug_acpi_match[] = {
  213. { "MSHW0153", 0 },
  214. { },
  215. };
  216. MODULE_DEVICE_TABLE(acpi, surface_hotplug_acpi_match);
  217. static struct platform_driver surface_hotplug_driver = {
  218. .probe = surface_hotplug_probe,
  219. .remove = surface_hotplug_remove,
  220. .driver = {
  221. .name = "surface_hotplug",
  222. .acpi_match_table = surface_hotplug_acpi_match,
  223. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  224. },
  225. };
  226. module_platform_driver(surface_hotplug_driver);
  227. MODULE_AUTHOR("Maximilian Luz <[email protected]>");
  228. MODULE_DESCRIPTION("Surface Hot-Plug Signaling Driver for Surface Book Devices");
  229. MODULE_LICENSE("GPL");