From patchwork Sat Oct 6 15:27:10 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiang Liu X-Patchwork-Id: 1557891 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Original-To: patchwork-linux-pci@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id 4C30D3FD56 for ; Sat, 6 Oct 2012 15:30:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752323Ab2JFP3q (ORCPT ); Sat, 6 Oct 2012 11:29:46 -0400 Received: from mail-pb0-f46.google.com ([209.85.160.46]:63060 "EHLO mail-pb0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751691Ab2JFP3o (ORCPT ); Sat, 6 Oct 2012 11:29:44 -0400 Received: by mail-pb0-f46.google.com with SMTP id rr4so2814954pbb.19 for ; Sat, 06 Oct 2012 08:29:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=0+prTZGLv4dgkd3VdmIBkW3u4QAY0dFFHxPKNuqmXJI=; b=LBMJzUgDj9HmJkZvG9xaiiH0L5SznX/91hPM4iWqhRLH9UzAopPFzV1vP+PYaV+dzH A174sgjWdfy9BWP1ow80CAOVJ1NhLb+vbBumVhF0hFY8x64ITDvcpOxyKy5YLeWfPTHg 3XRcZ9p6ci0YyQrrFAEAHmAcqswSE/H59KSpGFYUybuXHEGwigUg844LwnaGnFgoN7XY o2ZyBRT7P9QMLg4unNy8u6T9tM4tLHw3tOrYDueky/XkPFXh1L88i3kxFsLkshIJCbDy WGmLcWvyHTgvcoOIpOMZoUCpr47Hk3R2/vyaJp0CWmM1YeyqOk+NZb6vU71rkzVEGQPh d/vw== Received: by 10.66.89.37 with SMTP id bl5mr30110721pab.55.1349537383816; Sat, 06 Oct 2012 08:29:43 -0700 (PDT) Received: from localhost.localdomain ([221.221.24.247]) by mx.google.com with ESMTPS id vz8sm7785292pbc.63.2012.10.06.08.29.29 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 06 Oct 2012 08:29:43 -0700 (PDT) From: Jiang Liu To: Yinghai Lu , Yasuaki Ishimatsu , Kenji Kaneshige , Wen Congyang , Tang Chen , Taku Izumi Cc: Hanjun Guo , Yijing Wang , Gong Chen , Jiang Liu , Tony Luck , Huang Ying , Bob Moore , Len Brown , "Srivatsa S . Bhat" , Bjorn Helgaas , linux-kernel@vger.kernel.org, linux-acpi@vger.kernel.org, linux-pci@vger.kernel.org, Gaohuai Han Subject: [RFC PATCH v3 02/28] ACPIHP: ACPI system device hotplug slot enumerator driver Date: Sat, 6 Oct 2012 23:27:10 +0800 Message-Id: <1349537256-21670-3-git-send-email-jiang.liu@huawei.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1349537256-21670-1-git-send-email-jiang.liu@huawei.com> References: <1349537256-21670-1-git-send-email-jiang.liu@huawei.com> Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org An ACPI hotplug slot is an abstraction of receptacles, where a group of system devices could be connected to. This patch implements the skeleton of the ACPI system device hotplug slot enumerator. On loading, it scans the whole ACPI namespace for hotplug slots and creates a device node for each hotplug slot found. Every hotplug slot is associated with a device class named acpihp_slot_class. Later hotplug drivers will register onto acpihp_slot_class to manage all hotplug slots. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Signed-off-by: Jiang Liu Signed-off-by: Gaohuai Han --- drivers/acpi/Kconfig | 14 ++ drivers/acpi/hotplug/Makefile | 3 + drivers/acpi/hotplug/core.c | 2 +- drivers/acpi/hotplug/slot.c | 417 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 435 insertions(+), 1 deletion(-) create mode 100644 drivers/acpi/hotplug/slot.c diff --git a/drivers/acpi/Kconfig b/drivers/acpi/Kconfig index 01b87c7..915a25e 100644 --- a/drivers/acpi/Kconfig +++ b/drivers/acpi/Kconfig @@ -334,6 +334,20 @@ menuconfig ACPI_HOTPLUG If your hardware and firmware do not support dynamic reconfiguration of system devices at runtime, you need not to enable this option. +config ACPI_HOTPLUG_SLOT + tristate "Hotplug Slot Enumerator" + depends on ACPI_HOTPLUG + default y + help + System devices could be dynamically configured at runtime through + ACPI interfaces. This driver abstracts, creates and controls each + system device dynamic configuration control point as an ACPI hotplug + slot. And ACPI system device hotplug drivers will be used to manage + those hotplug slots created by this driver. + + To compile this driver as a module, choose M here: + the module will be called acpihp_slot. + config ACPI_CONTAINER tristate "Container and Module Devices (EXPERIMENTAL)" depends on EXPERIMENTAL diff --git a/drivers/acpi/hotplug/Makefile b/drivers/acpi/hotplug/Makefile index 5e7790f..2cbb03c 100644 --- a/drivers/acpi/hotplug/Makefile +++ b/drivers/acpi/hotplug/Makefile @@ -4,3 +4,6 @@ obj-$(CONFIG_ACPI_HOTPLUG) += acpihp.o acpihp-y = core.o + +obj-$(CONFIG_ACPI_HOTPLUG_SLOT) += acpihp_slot.o +acpihp_slot-y = slot.o diff --git a/drivers/acpi/hotplug/core.c b/drivers/acpi/hotplug/core.c index 086f4e5..081b27f 100644 --- a/drivers/acpi/hotplug/core.c +++ b/drivers/acpi/hotplug/core.c @@ -360,7 +360,7 @@ acpi_status acpihp_slot_get_status(struct acpihp_slot *slot, u64 *status) ACPIHP_SLOT_DEBUG(slot, "operation not supported.\n"); return AE_SUPPORT; } else if (slot->slot_ops->get_status) - return slot->slot_ops->get_status(slot->handle, status); + return slot->slot_ops->get_status(slot, status); rc = acpi_evaluate_integer(slot->handle, METHOD_NAME__UID, NULL, status); diff --git a/drivers/acpi/hotplug/slot.c b/drivers/acpi/hotplug/slot.c new file mode 100644 index 0000000..a230fc7 --- /dev/null +++ b/drivers/acpi/hotplug/slot.c @@ -0,0 +1,417 @@ +/* + * Copyright (C) 2012 Huawei Tech. Co., Ltd. + * Copyright (C) 2012 Jiang Liu + * Copyright (C) 2012 Gaohuai Han + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +#include +#include +#include +#include +#include "acpihp.h" + +struct acpihp_slot_id { + struct list_head node; + enum acpihp_slot_type type; + unsigned long instance_id; +}; + +static LIST_HEAD(slot_list); +static LIST_HEAD(slot_id_list); +static struct acpihp_slot_ops *slot_ops_curr; + +/* + * Platform dependent slot drivers should be sorted in descending order. + * The first entry whose init() method returns success will be used. + */ +static struct acpihp_slot_ops *slot_ops_array[] = { + NULL +}; + +static acpi_status __init +acpihp_slot_get_dev_type(acpi_handle handle, u32 lvl, void *context, void **rv) +{ + u32 *tp = (u32 *)rv; + acpi_status status = AE_OK; + enum acpihp_dev_type type; + + /* Only care about CPU, memory, PCI host bridge and CONTAINER */ + if (!acpihp_dev_get_type(handle, &type)) { + switch (type) { + case ACPIHP_DEV_TYPE_CPU: + *tp |= 0x0001; + status = AE_CTRL_DEPTH; + break; + case ACPIHP_DEV_TYPE_MEM: + *tp |= 0x0002; + status = AE_CTRL_DEPTH; + break; + case ACPIHP_DEV_TYPE_HOST_BRIDGE: + *tp |= 0x0004; + status = AE_CTRL_DEPTH; + break; + case ACPIHP_DEV_TYPE_CONTAINER: + *tp |= 0x0008; + break; + default: + break; + } + } + + return status; +} + +static enum acpihp_slot_type __init acpihp_slot_get_type_child(u32 dev_types) +{ + BUG_ON(dev_types > 15); + + switch (dev_types) { + case 0: + /* Generic CONTAINER */ + return ACPIHP_SLOT_TYPE_COMMON; + case 1: + /* Physical processor with logical CPUs */ + return ACPIHP_SLOT_TYPE_CPU; + case 2: + /* Memory board/box with memory devices */ + return ACPIHP_SLOT_TYPE_MEM; + case 3: + /* Physical processor with CPUs and memory controllers */ + return ACPIHP_SLOT_TYPE_CPU; + case 4: + /* IO eXtension board/box with IO host bridges */ + return ACPIHP_SLOT_TYPE_IOX; + case 7: + /* Physical processor with CPUs, IO host bridges and MCs. */ + return ACPIHP_SLOT_TYPE_CPU; + case 8: + /* Generic CONTAINER */ + return ACPIHP_SLOT_TYPE_COMMON; + case 9: + /* System board with physical processors */ + return ACPIHP_SLOT_TYPE_SYSTEM_BOARD; + case 11: + /* System board with physical processors and memory */ + return ACPIHP_SLOT_TYPE_SYSTEM_BOARD; + case 15: + /* Node with processor, memory and IO host bridge */ + return ACPIHP_SLOT_TYPE_NODE; + default: + return ACPIHP_SLOT_TYPE_UNKNOWN; + } +} + +static enum acpihp_slot_type __init +acpihp_slot_get_type_self(struct acpihp_slot *slot) +{ + enum acpihp_dev_type type; + + if (acpihp_dev_get_type(slot->handle, &type)) + return ACPIHP_SLOT_TYPE_UNKNOWN; + + switch (type) { + case ACPIHP_DEV_TYPE_CPU: + /* Logical CPU used in virtualization environment */ + return ACPIHP_SLOT_TYPE_CPU; + case ACPIHP_DEV_TYPE_MEM: + /* Memory board with single memory device */ + return ACPIHP_SLOT_TYPE_MEM; + case ACPIHP_DEV_TYPE_HOST_BRIDGE: + /* IO eXtension board/box with single IO host bridge */ + return ACPIHP_SLOT_TYPE_IOX; + default: + return ACPIHP_SLOT_TYPE_UNKNOWN; + } +} + +/* + * Generate a meaningful name for a hotplug slot according to devices + * connecting to it. + */ +static int __init acpihp_slot_generate_name(struct acpihp_slot *slot) +{ + int found = 0; + u32 child_types = 0; + unsigned long long uid; + struct acpihp_slot_id *slot_id; + + /* + * Figure out slot type by checking ACPI device type of the slot. + * Check child ACPI device types if the slot is a CONTAINER. + */ + slot->type = acpihp_slot_get_type_self(slot); + if (slot->type == ACPIHP_SLOT_TYPE_UNKNOWN) { + acpi_walk_namespace(ACPI_TYPE_DEVICE, slot->handle, + ACPI_UINT32_MAX, acpihp_slot_get_dev_type, + NULL, NULL, (void **)&child_types); + acpi_walk_namespace(ACPI_TYPE_PROCESSOR, slot->handle, + ACPI_UINT32_MAX, acpihp_slot_get_dev_type, + NULL, NULL, (void **)&child_types); + slot->type = acpihp_slot_get_type_child(child_types); + } + + /* Respect firmware settings if ACPI _UID returns a valid value. */ + if (ACPI_SUCCESS(acpi_evaluate_integer(slot->handle, METHOD_NAME__UID, + NULL, &uid))) + goto set_name; + + list_for_each_entry(slot_id, &slot_id_list, node) + if (slot_id->type == slot->type) { + found = 1; + break; + } + if (!found) { + slot_id = kzalloc(sizeof(struct acpihp_slot_id), GFP_KERNEL); + if (!slot_id) { + ACPIHP_SLOT_WARN(slot, + "fails to allocate slot instance ID.\n"); + return -ENOMEM; + } + slot_id->type = slot->type; + list_add_tail(&slot_id->node, &slot_id_list); + } + + uid = slot_id->instance_id++; + +set_name: + snprintf(slot->name, sizeof(slot->name) - 1, "%s%02llx", + acpihp_get_slot_type_name(slot->type), uid); + dev_set_name(&slot->dev, "%s", slot->name); + + return 0; +} + +static int __init acpihp_slot_get_parent(struct acpihp_slot *slot) +{ + acpi_handle handle, root_handle; + struct acpihp_slot *tmp; + + slot->parent = NULL; + handle = slot->handle; + if (ACPI_FAILURE(acpi_get_handle(NULL, ACPI_NS_ROOT_PATH, + &root_handle))) { + ACPIHP_SLOT_DEBUG(slot, "fails to get ACPI root device.\n"); + return -ENODEV; + } + + do { + if (ACPI_FAILURE(acpi_get_parent(handle, &handle))) { + ACPIHP_SLOT_DEBUG(slot, + "fails to get parent device.\n"); + return -ENODEV; + } + list_for_each_entry(tmp, &slot_list, slot_list) + if (tmp->handle == handle) { + slot->parent = tmp; + return 0; + } + } while (handle != root_handle); + + return 0; +} + +static int __init acpihp_slot_get_state(struct acpihp_slot *slot) +{ + unsigned long long sta; + + /* Hotplug slots must implement ACPI _STA method. */ + if (ACPI_FAILURE(acpihp_slot_get_status(slot, &sta))) { + ACPIHP_SLOT_DEBUG(slot, "fails to execute _STA method.\n"); + return -EINVAL; + } + + if (!(sta & ACPI_STA_DEVICE_PRESENT)) + slot->state = ACPIHP_SLOT_STATE_ABSENT; + else if ((sta & ACPI_STA_DEVICE_ENABLED) || + (sta & ACPI_STA_DEVICE_FUNCTIONING)) + slot->state = ACPIHP_SLOT_STATE_POWERED; + else + slot->state = ACPIHP_SLOT_STATE_PRESENT; + + return 0; +} + +static int __init acpihp_slot_create(acpi_handle handle) +{ + struct acpihp_slot *slot; + + slot = acpihp_create_slot(handle, NULL); + if (!slot) { + ACPIHP_DEBUG("fails to allocate memory for slot %p.\n", handle); + return -ENOMEM; + } + + slot->slot_ops = slot_ops_curr; + if (ACPI_FAILURE(slot_ops_curr->create(slot))) { + ACPIHP_DEBUG("fails to create slot for %p.\n", handle); + goto out_free; + } + if (acpihp_slot_get_parent(slot)) + goto out_destroy; + if (acpihp_slot_get_state(slot)) + goto out_destroy; + if (acpihp_slot_generate_name(slot)) + goto out_destroy; + + if (acpihp_register_slot(slot)) { + ACPIHP_SLOT_DEBUG(slot, "fails to register slot.\n"); + goto out_destroy; + } + if (ACPI_FAILURE(acpihp_mark_slot(handle, slot))) { + ACPIHP_SLOT_DEBUG(slot, + "fails to attach slot to ACPI device object.\n"); + goto out_unregister; + } + + list_add_tail(&slot->slot_list, &slot_list); + + return 0; + +out_unregister: + acpihp_unregister_slot(slot); +out_destroy: + if (slot_ops_curr->destroy) + slot_ops_curr->destroy(slot); +out_free: + acpihp_slot_put(slot); + return -ENODEV; +} + +static acpi_status __init acpihp_slot_scan(acpi_handle handle, u32 lvl, + void *context, void **rv) +{ + enum acpihp_dev_type type; + + if (acpihp_dev_get_type(handle, &type) || + type == ACPIHP_DEV_TYPE_UNKNOWN) + return AE_OK; + + if (ACPI_SUCCESS(slot_ops_curr->check(handle))) + acpihp_slot_create(handle); + + /* + * Don't scan hotplug slots under PCI host bridges, they should be + * handled by acpiphp or pciehp drivers. + */ + if (type == ACPIHP_DEV_TYPE_HOST_BRIDGE) + return AE_CTRL_DEPTH; + + return AE_OK; +} + +static int __init acpihp_slot_scan_slots(void) +{ + acpi_status status; + + status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, + ACPI_UINT32_MAX, acpihp_slot_scan, + NULL, NULL, NULL); + if (!ACPI_SUCCESS(status)) + goto out_err; + + status = acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT, + ACPI_UINT32_MAX, acpihp_slot_scan, + NULL, NULL, NULL); + if (ACPI_SUCCESS(status)) + return 0; + +out_err: + ACPIHP_DEBUG("fails to scan ACPI hotplug slots.\n"); + + return -ENODEV; +} + +static void acpihp_slot_cleanup(void) +{ + struct acpihp_slot *slot, *tmp; + struct acpihp_slot_id *slot_id, *tmp_id; + + list_for_each_entry_safe(slot, tmp, &slot_list, slot_list) { + acpihp_unmark_slot(slot->handle); + acpihp_unregister_slot(slot); + if (slot->slot_ops && slot->slot_ops->destroy) + slot->slot_ops->destroy(slot); + acpihp_slot_put(slot); + } + + list_for_each_entry_safe(slot_id, tmp_id, &slot_id_list, node) { + list_del(&slot_id->node); + kfree(slot_id); + } +} + +static int __init acpihp_slot_init(void) +{ + int i, retval; + + /* probe for available platform specific slot driver. */ + for (i = 0; slot_ops_array[i]; i++) + if (ACPI_SUCCESS(slot_ops_array[i]->init())) { + slot_ops_curr = slot_ops_array[i]; + break; + } + if (slot_ops_curr == NULL) { + ACPIHP_DEBUG("no platform specific slot driver available.\n"); + return -ENODEV; + } + + retval = acpihp_register_class(); + if (retval != 0) { + ACPIHP_DEBUG("fails to register ACPI hotplug slot class.\n"); + goto out_fini; + } + + if (acpihp_slot_scan_slots() != 0) + ACPIHP_DEBUG("fails to enumerate ACPI hotplug slots.\n"); + + /* Back out if no ACPI hotplug slot found. */ + if (list_empty(&slot_list)) { + ACPIHP_DEBUG("no ACPI hotplug slot available.\n"); + retval = -ENODEV; + goto out_unregister_class; + } + + return 0; + +out_unregister_class: + acpihp_unregister_class(); +out_fini: + if (slot_ops_curr && slot_ops_curr->fini) + slot_ops_curr->fini(); + ACPIHP_DEBUG("fails to initialize ACPI hotplug slot driver.\n"); + + return retval; +} + +static void __exit acpihp_slot_exit(void) +{ + acpihp_slot_cleanup(); + acpihp_unregister_class(); + if (slot_ops_curr && slot_ops_curr->fini) + slot_ops_curr->fini(); +} + +module_init(acpihp_slot_init); +module_exit(acpihp_slot_exit); + +MODULE_LICENSE("GPL v2"); +MODULE_AUTHOR("Jiang Liu "); +MODULE_AUTHOR("Gaohuai Han ");