acpi_platform.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ACPI support for platform bus type.
  4. *
  5. * Copyright (C) 2012, Intel Corporation
  6. * Authors: Mika Westerberg <mika.westerberg@linux.intel.com>
  7. * Mathias Nyman <mathias.nyman@linux.intel.com>
  8. * Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  9. */
  10. #include <linux/acpi.h>
  11. #include <linux/bits.h>
  12. #include <linux/device.h>
  13. #include <linux/err.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/pci.h>
  18. #include <linux/platform_device.h>
  19. #include "internal.h"
  20. /* Exclude devices that have no _CRS resources provided */
  21. #define ACPI_ALLOW_WO_RESOURCES BIT(0)
  22. static const struct acpi_device_id forbidden_id_list[] = {
  23. {"ACPI0009", 0}, /* IOxAPIC */
  24. {"ACPI000A", 0}, /* IOAPIC */
  25. {"PNP0000", 0}, /* PIC */
  26. {"PNP0100", 0}, /* Timer */
  27. {"PNP0200", 0}, /* AT DMA Controller */
  28. {ACPI_SMBUS_MS_HID, ACPI_ALLOW_WO_RESOURCES}, /* ACPI SMBUS virtual device */
  29. { }
  30. };
  31. static struct platform_device *acpi_platform_device_find_by_companion(struct acpi_device *adev)
  32. {
  33. struct device *dev;
  34. dev = bus_find_device_by_acpi_dev(&platform_bus_type, adev);
  35. return dev ? to_platform_device(dev) : NULL;
  36. }
  37. static int acpi_platform_device_remove_notify(struct notifier_block *nb,
  38. unsigned long value, void *arg)
  39. {
  40. struct acpi_device *adev = arg;
  41. struct platform_device *pdev;
  42. switch (value) {
  43. case ACPI_RECONFIG_DEVICE_ADD:
  44. /* Nothing to do here */
  45. break;
  46. case ACPI_RECONFIG_DEVICE_REMOVE:
  47. if (!acpi_device_enumerated(adev))
  48. break;
  49. pdev = acpi_platform_device_find_by_companion(adev);
  50. if (!pdev)
  51. break;
  52. platform_device_unregister(pdev);
  53. put_device(&pdev->dev);
  54. break;
  55. }
  56. return NOTIFY_OK;
  57. }
  58. static struct notifier_block acpi_platform_notifier = {
  59. .notifier_call = acpi_platform_device_remove_notify,
  60. };
  61. static void acpi_platform_fill_resource(struct acpi_device *adev,
  62. const struct resource *src, struct resource *dest)
  63. {
  64. struct device *parent;
  65. *dest = *src;
  66. /*
  67. * If the device has parent we need to take its resources into
  68. * account as well because this device might consume part of those.
  69. */
  70. parent = acpi_get_first_physical_node(acpi_dev_parent(adev));
  71. if (parent && dev_is_pci(parent))
  72. dest->parent = pci_find_resource(to_pci_dev(parent), dest);
  73. }
  74. static unsigned int acpi_platform_resource_count(struct acpi_resource *ares, void *data)
  75. {
  76. bool *has_resources = data;
  77. *has_resources = true;
  78. return AE_CTRL_TERMINATE;
  79. }
  80. /**
  81. * acpi_create_platform_device - Create platform device for ACPI device node
  82. * @adev: ACPI device node to create a platform device for.
  83. * @properties: Optional collection of build-in properties.
  84. *
  85. * Check if the given @adev can be represented as a platform device and, if
  86. * that's the case, create and register a platform device, populate its common
  87. * resources and returns a pointer to it. Otherwise, return %NULL.
  88. *
  89. * Name of the platform device will be the same as @adev's.
  90. */
  91. struct platform_device *acpi_create_platform_device(struct acpi_device *adev,
  92. const struct property_entry *properties)
  93. {
  94. struct acpi_device *parent = acpi_dev_parent(adev);
  95. struct platform_device *pdev = NULL;
  96. struct platform_device_info pdevinfo;
  97. const struct acpi_device_id *match;
  98. struct resource *resources = NULL;
  99. int count = 0;
  100. /* If the ACPI node already has a physical device attached, skip it. */
  101. if (adev->physical_node_count && !adev->pnp.type.backlight)
  102. return NULL;
  103. match = acpi_match_acpi_device(forbidden_id_list, adev);
  104. if (match) {
  105. if (match->driver_data & ACPI_ALLOW_WO_RESOURCES) {
  106. bool has_resources = false;
  107. acpi_walk_resources(adev->handle, METHOD_NAME__CRS,
  108. acpi_platform_resource_count, &has_resources);
  109. if (has_resources)
  110. return ERR_PTR(-EINVAL);
  111. } else {
  112. return ERR_PTR(-EINVAL);
  113. }
  114. }
  115. if (adev->device_type == ACPI_BUS_TYPE_DEVICE) {
  116. LIST_HEAD(resource_list);
  117. count = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
  118. if (count < 0)
  119. return ERR_PTR(-ENODATA);
  120. if (count > 0) {
  121. struct resource_entry *rentry;
  122. resources = kzalloc_objs(*resources, count);
  123. if (!resources) {
  124. acpi_dev_free_resource_list(&resource_list);
  125. return ERR_PTR(-ENOMEM);
  126. }
  127. count = 0;
  128. list_for_each_entry(rentry, &resource_list, node)
  129. acpi_platform_fill_resource(adev, rentry->res,
  130. &resources[count++]);
  131. acpi_dev_free_resource_list(&resource_list);
  132. }
  133. }
  134. memset(&pdevinfo, 0, sizeof(pdevinfo));
  135. /*
  136. * If the ACPI node has a parent and that parent has a physical device
  137. * attached to it, that physical device should be the parent of the
  138. * platform device we are about to create.
  139. */
  140. pdevinfo.parent = parent ? acpi_get_first_physical_node(parent) : NULL;
  141. pdevinfo.name = dev_name(&adev->dev);
  142. pdevinfo.id = PLATFORM_DEVID_NONE;
  143. pdevinfo.res = resources;
  144. pdevinfo.num_res = count;
  145. pdevinfo.fwnode = acpi_fwnode_handle(adev);
  146. pdevinfo.properties = properties;
  147. if (acpi_dma_supported(adev))
  148. pdevinfo.dma_mask = DMA_BIT_MASK(32);
  149. else
  150. pdevinfo.dma_mask = 0;
  151. pdev = platform_device_register_full(&pdevinfo);
  152. if (IS_ERR(pdev))
  153. dev_err(&adev->dev, "platform device creation failed: %ld\n",
  154. PTR_ERR(pdev));
  155. else {
  156. set_dev_node(&pdev->dev, acpi_get_node(adev->handle));
  157. dev_dbg(&adev->dev, "created platform device %s\n",
  158. dev_name(&pdev->dev));
  159. }
  160. kfree(resources);
  161. return pdev;
  162. }
  163. EXPORT_SYMBOL_GPL(acpi_create_platform_device);
  164. void __init acpi_platform_init(void)
  165. {
  166. acpi_reconfig_notifier_register(&acpi_platform_notifier);
  167. }