Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3448985rwb; Tue, 16 Aug 2022 03:18:58 -0700 (PDT) X-Google-Smtp-Source: AA6agR5tKazp4THimPhmzz39gjgsW24FyfMRdUK35KVSVoc1d28pY64UMgAXxUcZUtg2mfTIwDSx X-Received: by 2002:a17:907:94ca:b0:731:8395:d526 with SMTP id dn10-20020a17090794ca00b007318395d526mr12624133ejc.389.1660645137814; Tue, 16 Aug 2022 03:18:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660645137; cv=none; d=google.com; s=arc-20160816; b=nuv4bA9SmSou9KerClDKnIM1lLddRiS1/UdUuzO17QDPkB4zTAviL9ZmxaU45uEh1X xgUEoxW7dY8MFNR7whpnOdgK+/OGFSyUe53V4hB17MsSGt4JJ2mnV8paLAuJt3lhFjjD oLF8YEqMhFXVDyXja+S2I1o1YZ7Xc5SpQnN8YMasMl4afGi3ClbLmzLFLxGZehymUXTv Fdm4KsZUBhgdhWzHySoabFqxTt/F7vpYC1dhcilSKhhpGhg/4TBUdooI4BUG8t58S1ud VtXyhNx65jrCpVhCs5GLfBaLU6Qj0nza3ZW0RP9Mn/D16kjKcoUfxgG75VYTcRT4z3o5 kYvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:feedback-id:dkim-signature :dkim-signature; bh=8a1Us8uLfYqD/kfLMEoeT8k8OknxiZLX1kGLg+OrAM4=; b=S8FvczW8b/yCG3ryIc2LLEJlfNJTf66Tin0rBM90aOG+4POv1ENKJl9Unbi9Bg6JaS ZwrLz7EKJuHOL6zoUzDsaqTzKX97RJVw+L68njmK8rPLym2PQnCXWWU2xJp2WuDT4w43 7O+dtzCNhGFCIeZAYPCPN9U6JDKf8Ou9Lf7Mqgv3JfM7Rv1ARdkNFhVsISJPX419M+oY QdeS+mX4hp4QZ1RODj7Xst+A51B26IEiN+3irMVREbu++ifSJgkz85/04zH73ppsP8oH 4KoDg9MdC8I2zO8rXTmZsZ29AuqRP7wfXBrGLsc4QjSWqQuEc5dbePYYjfIBFbKAo22j dG4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@flygoat.com header.s=fm2 header.b=r6H+8a3y; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=wrYGYkzD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=flygoat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y20-20020a056402359400b0043bb89f1c11si10272911edc.553.2022.08.16.03.18.32; Tue, 16 Aug 2022 03:18:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@flygoat.com header.s=fm2 header.b=r6H+8a3y; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=wrYGYkzD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=flygoat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229658AbiHPKHL (ORCPT + 99 others); Tue, 16 Aug 2022 06:07:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54460 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234312AbiHPKGs (ORCPT ); Tue, 16 Aug 2022 06:06:48 -0400 Received: from out5-smtp.messagingengine.com (out5-smtp.messagingengine.com [66.111.4.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C10D6C13B; Tue, 16 Aug 2022 02:13:09 -0700 (PDT) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id B97AD5C023C; Tue, 16 Aug 2022 05:13:06 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Tue, 16 Aug 2022 05:13:06 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=flygoat.com; h= cc:cc:content-transfer-encoding:date:date:from:from:in-reply-to :message-id:mime-version:reply-to:sender:subject:subject:to:to; s=fm2; t=1660641186; x=1660727586; bh=8a1Us8uLfYqD/kfLMEoeT8k8O knxiZLX1kGLg+OrAM4=; b=r6H+8a3y5yj7F0UBxWS3xmNGJ4AZCoV08iA9aYSiG iIUMD0BVkiArJeidyP3+THHDgukkHtiYS2rV2+5jEczyQJWOJxa+ZTlU/LZywIWS jIn0WD0vtvdAsrpKUe75BD5Vx/SgInIM8oyeFBWYjnul23Yq+Jc1VhQXQITkEX9X 8zs6nw70KAkOukTTHFd4WAytOG+3ydSI1JQuJTvF9oLI9+cHiDr15mPHkj3ROJsn GzolNxC7i1+u7A9MSZNbGTYdtofb7F5t4LMWGpcCg3ovJpYAwi+ENSePxTE1uFI4 GcG0mtNLrBJAjou8YRD3LVd+XUzGW770AE4pH7tIXRvrA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding:date:date :feedback-id:feedback-id:from:from:in-reply-to:message-id :mime-version:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t= 1660641186; x=1660727586; bh=8a1Us8uLfYqD/kfLMEoeT8k8OknxiZLX1kG Lg+OrAM4=; b=wrYGYkzDiCXY2jq9yVfNlwFSOW1/NZDsNT4xf02gU7Drm1Ei1QJ NJiG9y7Vqv95PxaclaFAxVInyTQjKlZScb4tpZip+4HPtydJDOre9VjpLJjZQUbY vMaNQm3i3OrdILpnDNqtBLyKJ/gnptxLxMo8UZ+Nnc8RB4EDuTbaat4f7Eha2+VK dbVzXpccV2JY2643CcTGcd97TU5fz1msF9n5JBFo6E7VgKk92FrTeX/vREcE6mr1 1TzcvdmZPxzFpDa3F/Piz+LcbnzQ3zrQbljtlPw7k4Ds7tGE8TVCoYa8lFtP6c65 qBsGmMExD8Tcxq8R3qp4HMEQbsmvRQnt9dw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedrvdehgedgudegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvfevufffkffoggfgsedtkeertd ertddtnecuhfhrohhmpeflihgrgihunhcujggrnhhguceojhhirgiguhhnrdihrghnghes fhhlhihgohgrthdrtghomheqnecuggftrfgrthhtvghrnhephfetuddtudevieeljeejte ffheeujeduhefgffejudfhueelleduffefgfffveeknecuvehluhhsthgvrhfuihiivgep tdenucfrrghrrghmpehmrghilhhfrhhomhepjhhirgiguhhnrdihrghnghesfhhlhihgoh grthdrtghomh X-ME-Proxy: Feedback-ID: ifd894703:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 16 Aug 2022 05:13:05 -0400 (EDT) From: Jiaxun Yang To: linux-mips@vger.kernel.org Cc: linux-kernel@vger.kernel.org, tsbogend@alpha.franken.de, linux-api@vger.kernel.org, greg@kroah.com, f.fainelli@gmail.com, Jiaxun Yang Subject: [PATCH v4] MIPS: Expose prid and globalnumber to sysfs Date: Tue, 16 Aug 2022 09:12:58 +0000 Message-Id: <20220816091258.9571-1-jiaxun.yang@flygoat.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Some application would like to know precise model and rev of processor to do errata workaround or optimization. Expose them in sysfs as: /sys/devices/system/cpu/cpuX/regs/identification/prid /sys/devices/system/cpu/cpuX/regs/identification/globalnumber Reusing AArch64 CPU registers directory. Signed-off-by: Jiaxun Yang --- v2: Drop static qualifier for kobj (gregkh) v3: Use kzalloc to allocate struct cpuregs. note: When Greg mentioned about static I was thinking about static qualifier of percpu variable. After reading documents again it turns out kobjs should be allocated at runtime. Arm64's cpuinfo kobj is also on a percpu variable... I guess that was a intentional use? v4: Properly handle err of kobj creation. (gregkh) --- .../ABI/testing/sysfs-devices-system-cpu | 11 ++ arch/mips/kernel/topology.c | 103 ++++++++++++++++++ 2 files changed, 114 insertions(+) diff --git a/Documentation/ABI/testing/sysfs-devices-system-cpu b/Documentation/ABI/testing/sysfs-devices-system-cpu index 5bf61881f012..adf855e7bb9b 100644 --- a/Documentation/ABI/testing/sysfs-devices-system-cpu +++ b/Documentation/ABI/testing/sysfs-devices-system-cpu @@ -512,6 +512,17 @@ Description: information about CPUs heterogeneity. cpu_capacity: capacity of cpuX. +What: /sys/devices/system/cpu/cpuX/regs/ + /sys/devices/system/cpu/cpuX/regs/identification/ + /sys/devices/system/cpu/cpuX/regs/identification/prid + /sys/devices/system/cpu/cpuX/regs/identification/globalnumber +Date: Augest 2022 +Contact: Linux MIPS Kernel Mailing list +Description: MIPS CPU registers + + 'identification' directory exposes the Processor ID and Global Number + registers for identifying model and revision of the CPU. + What: /sys/devices/system/cpu/vulnerabilities /sys/devices/system/cpu/vulnerabilities/meltdown /sys/devices/system/cpu/vulnerabilities/spectre_v1 diff --git a/arch/mips/kernel/topology.c b/arch/mips/kernel/topology.c index 9429d85a4703..45a15eda651a 100644 --- a/arch/mips/kernel/topology.c +++ b/arch/mips/kernel/topology.c @@ -5,6 +5,8 @@ #include #include #include +#include +#include static DEFINE_PER_CPU(struct cpu, cpu_devices); @@ -26,3 +28,104 @@ static int __init topology_init(void) } subsys_initcall(topology_init); + +static struct kobj_type cpuregs_kobj_type = { + .sysfs_ops = &kobj_sysfs_ops, +}; + +struct cpureg { + struct kobject kobj; + struct cpuinfo_mips *info; +}; +static DEFINE_PER_CPU(struct cpureg *, cpuregs); + +#define kobj_to_cpureg(kobj) container_of(kobj, struct cpureg, kobj) +#define CPUREGS_ATTR_RO(_name, _field) \ + static ssize_t _name##_show(struct kobject *kobj, \ + struct kobj_attribute *attr, char *buf) \ + { \ + struct cpuinfo_mips *info = kobj_to_cpureg(kobj)->info; \ + \ + return sprintf(buf, "0x%08x\n", info->_field); \ + } \ + static struct kobj_attribute cpuregs_attr_##_name = __ATTR_RO(_name) + +CPUREGS_ATTR_RO(prid, processor_id); +CPUREGS_ATTR_RO(globalnumber, globalnumber); + +static struct attribute *cpuregs_id_attrs[] = { + &cpuregs_attr_prid.attr, + &cpuregs_attr_globalnumber.attr, + NULL +}; + +static const struct attribute_group cpuregs_attr_group = { + .attrs = cpuregs_id_attrs, + .name = "identification" +}; + +static int cpuregs_cpu_online(unsigned int cpu) +{ + int rc; + struct device *dev; + struct cpureg *reg; + + dev = get_cpu_device(cpu); + if (!dev) { + rc = -ENODEV; + goto out; + } + reg = kzalloc(sizeof(struct cpureg), GFP_KERNEL); + if (!reg) { + rc = -ENOMEM; + goto out; + } + rc = kobject_init_and_add(®->kobj, &cpuregs_kobj_type, + &dev->kobj, "regs"); + if (rc) + goto out_kfree; + rc = sysfs_create_group(®->kobj, &cpuregs_attr_group); + if (rc) + goto out_kobj; + + return 0; +out_kobj: + kobject_put(®->kobj); +out_kfree: + kfree(reg); +out: + return rc; +} + +static int cpuregs_cpu_offline(unsigned int cpu) +{ + struct device *dev; + struct cpureg *reg = per_cpu(cpuregs, cpu); + + dev = get_cpu_device(cpu); + if (!dev || !reg) + return -ENODEV; + if (reg->kobj.parent) { + sysfs_remove_group(®->kobj, &cpuregs_attr_group); + kobject_put(®->kobj); + } + kfree(reg); + + return 0; +} + +static int __init cpuinfo_regs_init(void) +{ + int ret; + + + ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "mips/topology:online", + cpuregs_cpu_online, cpuregs_cpu_offline); + if (ret < 0) { + pr_err("cpuinfo: failed to register hotplug callbacks.\n"); + return ret; + } + return 0; +} + +device_initcall(cpuinfo_regs_init); -- 2.34.1