Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * ACPI support for platform bus type.
0004  *
0005  * Copyright (C) 2012, Intel Corporation
0006  * Authors: Mika Westerberg <mika.westerberg@linux.intel.com>
0007  *          Mathias Nyman <mathias.nyman@linux.intel.com>
0008  *          Rafael J. Wysocki <rafael.j.wysocki@intel.com>
0009  */
0010 
0011 #include <linux/acpi.h>
0012 #include <linux/device.h>
0013 #include <linux/err.h>
0014 #include <linux/kernel.h>
0015 #include <linux/module.h>
0016 #include <linux/dma-mapping.h>
0017 #include <linux/pci.h>
0018 #include <linux/platform_device.h>
0019 
0020 #include "internal.h"
0021 
0022 static const struct acpi_device_id forbidden_id_list[] = {
0023     {"PNP0000",  0},    /* PIC */
0024     {"PNP0100",  0},    /* Timer */
0025     {"PNP0200",  0},    /* AT DMA Controller */
0026     {"ACPI0009", 0},    /* IOxAPIC */
0027     {"ACPI000A", 0},    /* IOAPIC */
0028     {"SMB0001",  0},    /* ACPI SMBUS virtual device */
0029     {"", 0},
0030 };
0031 
0032 static struct platform_device *acpi_platform_device_find_by_companion(struct acpi_device *adev)
0033 {
0034     struct device *dev;
0035 
0036     dev = bus_find_device_by_acpi_dev(&platform_bus_type, adev);
0037     return dev ? to_platform_device(dev) : NULL;
0038 }
0039 
0040 static int acpi_platform_device_remove_notify(struct notifier_block *nb,
0041                           unsigned long value, void *arg)
0042 {
0043     struct acpi_device *adev = arg;
0044     struct platform_device *pdev;
0045 
0046     switch (value) {
0047     case ACPI_RECONFIG_DEVICE_ADD:
0048         /* Nothing to do here */
0049         break;
0050     case ACPI_RECONFIG_DEVICE_REMOVE:
0051         if (!acpi_device_enumerated(adev))
0052             break;
0053 
0054         pdev = acpi_platform_device_find_by_companion(adev);
0055         if (!pdev)
0056             break;
0057 
0058         platform_device_unregister(pdev);
0059         put_device(&pdev->dev);
0060         break;
0061     }
0062 
0063     return NOTIFY_OK;
0064 }
0065 
0066 static struct notifier_block acpi_platform_notifier = {
0067     .notifier_call = acpi_platform_device_remove_notify,
0068 };
0069 
0070 static void acpi_platform_fill_resource(struct acpi_device *adev,
0071     const struct resource *src, struct resource *dest)
0072 {
0073     struct device *parent;
0074 
0075     *dest = *src;
0076 
0077     /*
0078      * If the device has parent we need to take its resources into
0079      * account as well because this device might consume part of those.
0080      */
0081     parent = acpi_get_first_physical_node(adev->parent);
0082     if (parent && dev_is_pci(parent))
0083         dest->parent = pci_find_resource(to_pci_dev(parent), dest);
0084 }
0085 
0086 /**
0087  * acpi_create_platform_device - Create platform device for ACPI device node
0088  * @adev: ACPI device node to create a platform device for.
0089  * @properties: Optional collection of build-in properties.
0090  *
0091  * Check if the given @adev can be represented as a platform device and, if
0092  * that's the case, create and register a platform device, populate its common
0093  * resources and returns a pointer to it.  Otherwise, return %NULL.
0094  *
0095  * Name of the platform device will be the same as @adev's.
0096  */
0097 struct platform_device *acpi_create_platform_device(struct acpi_device *adev,
0098                             const struct property_entry *properties)
0099 {
0100     struct platform_device *pdev = NULL;
0101     struct platform_device_info pdevinfo;
0102     struct resource_entry *rentry;
0103     struct list_head resource_list;
0104     struct resource *resources = NULL;
0105     int count;
0106 
0107     /* If the ACPI node already has a physical device attached, skip it. */
0108     if (adev->physical_node_count)
0109         return NULL;
0110 
0111     if (!acpi_match_device_ids(adev, forbidden_id_list))
0112         return ERR_PTR(-EINVAL);
0113 
0114     INIT_LIST_HEAD(&resource_list);
0115     count = acpi_dev_get_resources(adev, &resource_list, NULL, NULL);
0116     if (count < 0) {
0117         return NULL;
0118     } else if (count > 0) {
0119         resources = kcalloc(count, sizeof(struct resource),
0120                     GFP_KERNEL);
0121         if (!resources) {
0122             dev_err(&adev->dev, "No memory for resources\n");
0123             acpi_dev_free_resource_list(&resource_list);
0124             return ERR_PTR(-ENOMEM);
0125         }
0126         count = 0;
0127         list_for_each_entry(rentry, &resource_list, node)
0128             acpi_platform_fill_resource(adev, rentry->res,
0129                             &resources[count++]);
0130 
0131         acpi_dev_free_resource_list(&resource_list);
0132     }
0133 
0134     memset(&pdevinfo, 0, sizeof(pdevinfo));
0135     /*
0136      * If the ACPI node has a parent and that parent has a physical device
0137      * attached to it, that physical device should be the parent of the
0138      * platform device we are about to create.
0139      */
0140     pdevinfo.parent = adev->parent ?
0141         acpi_get_first_physical_node(adev->parent) : NULL;
0142     pdevinfo.name = dev_name(&adev->dev);
0143     pdevinfo.id = -1;
0144     pdevinfo.res = resources;
0145     pdevinfo.num_res = count;
0146     pdevinfo.fwnode = acpi_fwnode_handle(adev);
0147     pdevinfo.properties = properties;
0148 
0149     if (acpi_dma_supported(adev))
0150         pdevinfo.dma_mask = DMA_BIT_MASK(32);
0151     else
0152         pdevinfo.dma_mask = 0;
0153 
0154     pdev = platform_device_register_full(&pdevinfo);
0155     if (IS_ERR(pdev))
0156         dev_err(&adev->dev, "platform device creation failed: %ld\n",
0157             PTR_ERR(pdev));
0158     else {
0159         set_dev_node(&pdev->dev, acpi_get_node(adev->handle));
0160         dev_dbg(&adev->dev, "created platform device %s\n",
0161             dev_name(&pdev->dev));
0162     }
0163 
0164     kfree(resources);
0165 
0166     return pdev;
0167 }
0168 EXPORT_SYMBOL_GPL(acpi_create_platform_device);
0169 
0170 void __init acpi_platform_init(void)
0171 {
0172     acpi_reconfig_notifier_register(&acpi_platform_notifier);
0173 }