Received: by 2002:ab2:6c55:0:b0:1fd:c486:4f03 with SMTP id v21csp197154lqp; Tue, 11 Jun 2024 20:54:29 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVSCOIo+hj5KB24+Gg8skgHl8TxiVjyUBpNRT5R8wvNGnE7RTQsTdP5PF68qu/D7b9DpMqF6/SCZBiqpKrS07WAYK1sX1iRRL3NQMYmfw== X-Google-Smtp-Source: AGHT+IHDG/7egEsDb2noiH/5JVGPNOK1SYpxm5scsZ7O3sBd/x9T7cJawPZXlZmnHZetVYQ4A2NR X-Received: by 2002:a17:90a:c506:b0:2c2:27a8:9a6a with SMTP id 98e67ed59e1d1-2c4a7639023mr770375a91.25.1718164469165; Tue, 11 Jun 2024 20:54:29 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718164469; cv=pass; d=google.com; s=arc-20160816; b=hEFZ9iC9TMMGCwL5gg7TnZrGYNc0T2rN+hu/pCDa67luH6rJX4csOeQYrghfmeIuGM sxwmuyv3tlw2lWtQfbWOKNXzEwbNOerV3uTEeYrZ4UsP9vTX7D/UehrcAQtJ/aBK/zPg cE+Za/vW2TkNYTarl8T4H4RNkqJTtMRTOrq17Mh/orgCn6Zr3w5kcqyV+eJ9udu3uXfD MdseEhZ9g4cNRL+xeeXxEs9IOBvRPNWnsz8XhowOHSL4jx9Q4fwOoPNZGR718Fb7d2ki TXoOlz209vhwnthLJ+vOTXJlMPRKko48Z6zgYKcgDtGnZvMl2GXbeqBSdFCmfUCKe5cn WP4w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=PLayiuzVPFFY465nUij3xesqSUoa7sXyXyNgZIO3Cn4=; fh=p4Pp/0nJNGwp82oEyWH0XiTPtlV16aDZ4lUo1BZ5wtg=; b=bKIPQRX1ZJjhc/SledBrYTWE33Rk7lbugl7rAzaHIb4kPWxzi+SK/ymdgkQrAGRnF2 +lR9Tk5TmcjZaL1dP92lmPZLJZHecFehM0fvh3MfxhMTfwBBNhJ1XYPkWXWEKZKct3yV RT8XPWPyAMawau/RwJpErGqj42g779+MjkaA+sd3Q/yUNB+PE5ozr0h2GdyrhPNuTr0G K14V3vj9GYquRnuwX5+5lo58oI91/52rpPwsd2GLnC2J5ksnlGEoLCAAwVkEr/j5WnJS VmibVpbzHKQ6YYl6PlHo7QPo2SpOuIdQzFFfhdHlb+csEkAyGDEWvQLIafZENPHWf4cS Rh6g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=H6qAIx3s; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-210893-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-210893-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id 98e67ed59e1d1-2c4a75f6909si605415a91.41.2024.06.11.20.54.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jun 2024 20:54:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-210893-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=H6qAIx3s; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-210893-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-210893-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id A69742863E3 for ; Wed, 12 Jun 2024 03:54:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D3C2D2868D; Wed, 12 Jun 2024 03:54:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="H6qAIx3s" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 802FC21362; Wed, 12 Jun 2024 03:54:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.9 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718164450; cv=none; b=bhDukZ7sRbavQN7mABYxMo1OM6FE9/+cWLIhSOsq/TxereODJY0go963b0bYdgoMXmQoNRaMbPFwpxnDtyIXdHYfVcLysTEQSV9Kh+zfo89z8zDcNdpCHoBOP0Iu0k1wJn1QDH5lBR/ZjHlkB75nGrBjBCXugyJk7/ckyct2MKM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718164450; c=relaxed/simple; bh=EwtZfeuYyvQEr/AjxRycPI/pvIPFOtH8D1SZ5xPbbAo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=nnNcVDFxrWvyGACtDMpaq4rnnIpL97J9DKSlEZ1poU5rYbDLJeziwFU+cNzFGHrZRarRnbixUGrafb0bxgDYZGFBLUegvFB+FifcTHVt2n8FeSAGZgzQVfHVu+9WsKgtgUbboiIv8kiDPEyzAvAjuJ0pzMx8c5vfiSkMU+jPt8M= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=H6qAIx3s; arc=none smtp.client-ip=198.175.65.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1718164448; x=1749700448; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=EwtZfeuYyvQEr/AjxRycPI/pvIPFOtH8D1SZ5xPbbAo=; b=H6qAIx3see4gbYdSoMqpFviHW4vyEQtjwlAuEXYd4SHiZGXVQYtJpvgs wo44nMNeC4Q857VXHZ5ljJ+KWbZvUVM/pXbT8kiU1lkOHSaxah/ynt4kU /oK/vJVPxFIyQP9dFYZEoGPbFZ8xb9TKkXqfVxh7zWDBEXYl9QKUosORZ D3WEmaiZy0McnGe7fGmIhoOypfESqxRRNXpD8RP7LEzptpHaDf8fFT1/n jizi78DzkGBiqbRgwfLdYQrUrRcyNSTJSTQ5qyZY0hQR6+KUYqF4ooSYa N4RbJx9UVaDlQn92jXtyvBC0I1Ep0l16gofzauPkkfPP41bVqmKY3dcPu w==; X-CSE-ConnectionGUID: vr0NTeVYTrqZomeRF43nvg== X-CSE-MsgGUID: DY5ioSDhQVylAifPgThKbg== X-IronPort-AV: E=McAfee;i="6600,9927,11100"; a="37433734" X-IronPort-AV: E=Sophos;i="6.08,232,1712646000"; d="scan'208";a="37433734" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Jun 2024 20:54:08 -0700 X-CSE-ConnectionGUID: tJTAxEC3TLOepkAqKaCAqg== X-CSE-MsgGUID: gjOsklThTQ2q9OPF1lp7ag== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,232,1712646000"; d="scan'208";a="39758570" Received: from inlubt0316.iind.intel.com ([10.191.20.213]) by fmviesa009.fm.intel.com with ESMTP; 11 Jun 2024 20:54:04 -0700 From: lakshmi.sowjanya.d@intel.com To: tglx@linutronix.de, giometti@enneenne.com, corbet@lwn.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org Cc: gregkh@linuxfoundation.org, andriy.shevchenko@linux.intel.com, eddie.dong@intel.com, christopher.s.hall@intel.com, pandith.n@intel.com, subramanian.mohan@intel.com, thejesh.reddy.t.r@intel.com, lakshmi.sowjanya.d@intel.com Subject: [PATCH v10 1/3] pps: generators: Add PPS Generator TIO Driver Date: Wed, 12 Jun 2024 09:23:57 +0530 Message-Id: <20240612035359.7307-2-lakshmi.sowjanya.d@intel.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20240612035359.7307-1-lakshmi.sowjanya.d@intel.com> References: <20240612035359.7307-1-lakshmi.sowjanya.d@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Lakshmi Sowjanya D The Intel Timed IO PPS generator driver outputs a PPS signal using dedicated hardware that is more accurate than software actuated PPS. The Timed IO hardware generates output events using the ART timer. The ART timer period varies based on platform type, but is less than 100 nanoseconds for all current platforms. Timed IO output accuracy is within 1 ART period. PPS output is enabled by writing '1' the 'enable' sysfs attribute. The driver uses hrtimers to schedule a wake-up 10 ms before each event (edge) target time. At wakeup, the driver converts the target time in terms of CLOCK_REALTIME to ART trigger time and writes this to the Timed IO hardware. The Timed IO hardware generates an event precisely at the requested system time without software involvement. Co-developed-by: Christopher Hall Signed-off-by: Christopher Hall Co-developed-by: Pandith N Signed-off-by: Pandith N Co-developed-by: Thejesh Reddy T R Signed-off-by: Thejesh Reddy T R Signed-off-by: Lakshmi Sowjanya D Reviewed-by: Eddie Dong Reviewed-by: Andy Shevchenko Acked-by: Rodolfo Giometti --- drivers/pps/generators/Kconfig | 16 ++ drivers/pps/generators/Makefile | 1 + drivers/pps/generators/pps_gen_tio.c | 264 +++++++++++++++++++++++++++ 3 files changed, 281 insertions(+) create mode 100644 drivers/pps/generators/pps_gen_tio.c diff --git a/drivers/pps/generators/Kconfig b/drivers/pps/generators/Kconfig index d615e640fcad..0f090932336f 100644 --- a/drivers/pps/generators/Kconfig +++ b/drivers/pps/generators/Kconfig @@ -12,3 +12,19 @@ config PPS_GENERATOR_PARPORT If you say yes here you get support for a PPS signal generator which utilizes STROBE pin of a parallel port to send PPS signals. It uses parport abstraction layer and hrtimers to precisely control the signal. + +config PPS_GENERATOR_TIO + tristate "TIO PPS signal generator" + depends on X86 && CPU_SUP_INTEL + help + If you say yes here you get support for a PPS TIO signal generator + which generates a pulse at a prescribed time based on the system clock. + It uses time translation and hrtimers to precisely generate a pulse. + This hardware is present on 2019 and newer Intel CPUs. However, this + driver is not useful without adding highly specialized hardware outside + the Linux system to observe these pulses. + + To compile this driver as a module, choose M here: the module + will be called pps_gen_tio. + + If unsure, say N. diff --git a/drivers/pps/generators/Makefile b/drivers/pps/generators/Makefile index 2589fd0f2481..714e847ae193 100644 --- a/drivers/pps/generators/Makefile +++ b/drivers/pps/generators/Makefile @@ -4,5 +4,6 @@ # obj-$(CONFIG_PPS_GENERATOR_PARPORT) += pps_gen_parport.o +obj-$(CONFIG_PPS_GENERATOR_TIO) += pps_gen_tio.o ccflags-$(CONFIG_PPS_DEBUG) := -DDEBUG diff --git a/drivers/pps/generators/pps_gen_tio.c b/drivers/pps/generators/pps_gen_tio.c new file mode 100644 index 000000000000..c4142aa8f356 --- /dev/null +++ b/drivers/pps/generators/pps_gen_tio.c @@ -0,0 +1,264 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Intel PPS signal Generator Driver + * + * Copyright (C) 2024 Intel Corporation + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#define TIOCTL 0x00 +#define TIOCOMPV 0x10 +#define TIOEC 0x30 + +/* Control Register */ +#define TIOCTL_EN BIT(0) +#define TIOCTL_DIR BIT(1) +#define TIOCTL_EP GENMASK(3, 2) +#define TIOCTL_EP_RISING_EDGE FIELD_PREP(TIOCTL_EP, 0) +#define TIOCTL_EP_FALLING_EDGE FIELD_PREP(TIOCTL_EP, 1) +#define TIOCTL_EP_TOGGLE_EDGE FIELD_PREP(TIOCTL_EP, 2) + +#define SAFE_TIME_NS (10 * NSEC_PER_MSEC) /* Safety time to set hrtimer early */ +#define MAGIC_CONST (NSEC_PER_SEC - SAFE_TIME_NS) +#define ART_HW_DELAY_CYCLES 2 + +struct pps_tio { + struct hrtimer timer; + struct device *dev; + spinlock_t lock; + struct attribute_group attrs; + void __iomem *base; + bool enabled; + u32 prev_count; +}; + +static inline u32 pps_tio_read(struct pps_tio *tio, u32 offset) +{ + return readl(tio->base + offset); +} + +static inline void pps_ctl_write(struct pps_tio *tio, u32 value) +{ + writel(value, tio->base + TIOCTL); +} + +/* For COMPV register, It's safer to write higher 32-bit followed by lower 32-bit */ +static inline void pps_compv_write(struct pps_tio *tio, u64 value) +{ + hi_lo_writeq(value, tio->base + TIOCOMPV); +} + +static inline ktime_t first_event(struct pps_tio *tio) +{ + return ktime_set(ktime_get_real_seconds() + 1, MAGIC_CONST); +} + +static u32 pps_tio_disable(struct pps_tio *tio) +{ + u32 ctrl; + + ctrl = pps_tio_read(tio, TIOCTL); + pps_compv_write(tio, 0); + + ctrl &= ~TIOCTL_EN; + pps_ctl_write(tio, ctrl); + tio->enabled = false; + tio->prev_count = 0; + + return ctrl; +} + +static void pps_tio_enable(struct pps_tio *tio) +{ + u32 ctrl; + + ctrl = pps_tio_read(tio, TIOCTL); + ctrl |= TIOCTL_EN; + pps_ctl_write(tio, ctrl); + tio->enabled = true; +} + +static void pps_tio_direction_output(struct pps_tio *tio) +{ + u32 ctrl; + + ctrl = pps_tio_disable(tio); + + /* We enable the device, be sure that the 'compare' value is invalid */ + pps_compv_write(tio, 0); + + ctrl &= ~(TIOCTL_DIR | TIOCTL_EP); + ctrl |= TIOCTL_EP_TOGGLE_EDGE; + pps_ctl_write(tio, ctrl); + pps_tio_enable(tio); +} + +static bool pps_generate_next_pulse(struct pps_tio *tio, ktime_t expires) +{ + u64 art; + + if (!ktime_real_to_base_clock(expires, CSID_X86_ART, &art)) { + pps_tio_disable(tio); + return false; + } + + pps_compv_write(tio, art - ART_HW_DELAY_CYCLES); + return true; +} + +static enum hrtimer_restart hrtimer_callback(struct hrtimer *timer) +{ + struct pps_tio *tio = container_of(timer, struct pps_tio, timer); + ktime_t expires, now; + u32 event_count; + + guard(spinlock)(&tio->lock); + + /* Check if any event is missed. If an event is missed, TIO will be disabled*/ + event_count = pps_tio_read(tio, TIOEC); + if (tio->prev_count && tio->prev_count == event_count) + goto err; + tio->prev_count = event_count; + expires = hrtimer_get_expires(timer); + now = ktime_get_real(); + + if (now - expires >= SAFE_TIME_NS) + goto err; + + tio->enabled = pps_generate_next_pulse(tio, expires + SAFE_TIME_NS); + if (!tio->enabled) + return HRTIMER_NORESTART; + + hrtimer_forward(timer, now, NSEC_PER_SEC / 2); + return HRTIMER_RESTART; +err: + dev_err(tio->dev, "Event missed, Disabling Timed I/O"); + pps_tio_disable(tio); + return HRTIMER_NORESTART; +} + +static ssize_t enable_store(struct device *dev, struct device_attribute *attr, const char *buf, + size_t count) +{ + struct pps_tio *tio = dev_get_drvdata(dev); + bool enable; + int err; + + if (!timekeeping_clocksource_has_base(CSID_X86_ART)) { + dev_err_once(dev, "PPS cannot be used as clock is not related to ART"); + return -ENODEV; + } + + err = kstrtobool(buf, &enable); + if (err) + return err; + + guard(spinlock_irqsave)(&tio->lock); + if (enable && !tio->enabled) { + pps_tio_direction_output(tio); + hrtimer_start(&tio->timer, first_event(tio), HRTIMER_MODE_ABS); + } else if (!enable && tio->enabled) { + hrtimer_cancel(&tio->timer); + pps_tio_disable(tio); + } + return count; +} + +static ssize_t enable_show(struct device *dev, struct device_attribute *devattr, char *buf) +{ + struct pps_tio *tio = dev_get_drvdata(dev); + + return sysfs_emit(buf, "%u\n", tio->enabled); +} +static DEVICE_ATTR_RW(enable); + +static struct attribute *pps_tio_attrs[] = { + &dev_attr_enable.attr, + NULL +}; +ATTRIBUTE_GROUPS(pps_tio); + +static int pps_gen_tio_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct pps_tio *tio; + + if (!(cpu_feature_enabled(X86_FEATURE_TSC_KNOWN_FREQ) && + cpu_feature_enabled(X86_FEATURE_ART))) { + dev_warn(dev, "TSC/ART is not enabled"); + return -ENODEV; + } + + tio = devm_kzalloc(dev, sizeof(*tio), GFP_KERNEL); + if (!tio) + return -ENOMEM; + + tio->dev = dev; + tio->base = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(tio->base)) + return PTR_ERR(tio->base); + + pps_tio_disable(tio); + hrtimer_init(&tio->timer, CLOCK_REALTIME, HRTIMER_MODE_ABS); + tio->timer.function = hrtimer_callback; + spin_lock_init(&tio->lock); + platform_set_drvdata(pdev, tio); + + return 0; +} + +static int pps_gen_tio_remove(struct platform_device *pdev) +{ + struct pps_tio *tio = platform_get_drvdata(pdev); + + hrtimer_cancel(&tio->timer); + pps_tio_disable(tio); + + return 0; +} + +static const struct acpi_device_id intel_pmc_tio_acpi_match[] = { + { "INTC1021" }, + { "INTC1022" }, + { "INTC1023" }, + { "INTC1024" }, + {} +}; +MODULE_DEVICE_TABLE(acpi, intel_pmc_tio_acpi_match); + +static struct platform_driver pps_gen_tio_driver = { + .probe = pps_gen_tio_probe, + .remove = pps_gen_tio_remove, + .driver = { + .name = "intel-pps-gen-tio", + .acpi_match_table = intel_pmc_tio_acpi_match, + .dev_groups = pps_tio_groups, + }, +}; +module_platform_driver(pps_gen_tio_driver); + +MODULE_AUTHOR("Lakshmi Sowjanya D "); +MODULE_AUTHOR("Christopher Hall "); +MODULE_AUTHOR("Pandith N "); +MODULE_AUTHOR("Thejesh Reddy T R "); +MODULE_DESCRIPTION("Intel PMC Time-Aware IO Generator Driver"); +MODULE_LICENSE("GPL"); -- 2.35.3