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