From patchwork Wed Jan 22 01:58:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bobby Eshleman X-Patchwork-Id: 11345005 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D49CE1398 for ; Wed, 22 Jan 2020 05:15:09 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id B0F8324656 for ; Wed, 22 Jan 2020 05:15:09 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="VvjTNnNQ" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B0F8324656 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iu8L8-00057W-5k; Wed, 22 Jan 2020 05:14:06 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iu5KO-00067f-4W for xen-devel@lists.xenproject.org; Wed, 22 Jan 2020 02:01:08 +0000 X-Inumbo-ID: fde2556e-3cba-11ea-9fd7-bc764e2007e4 Received: from mail-yw1-xc42.google.com (unknown [2607:f8b0:4864:20::c42]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id fde2556e-3cba-11ea-9fd7-bc764e2007e4; Wed, 22 Jan 2020 02:00:41 +0000 (UTC) Received: by mail-yw1-xc42.google.com with SMTP id n184so2500730ywc.3 for ; Tue, 21 Jan 2020 18:00:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=SItJ1bZfQlObrrROlgGvFE93btikGDcQRvWQduZ0QeA=; b=VvjTNnNQA900PJjXN8SsvTFd3lu8nWOgpd0u9s9BKRyfH30gu/OQ2qjSAHBCdtcQmz 7RvwB+EDE7tcYqt8bv942sIjxylQ1G1woql3a/zUqnY6Wg6ST0M+By9R5eEu0FpllbG9 qu7+oF70wHJppehx4Gv5ja0FL+DMzjIzY2tqJ2EjLI3MwxsRcN4+WVhukjySc+jqBc3L pEVgkEqOd4HSYUSAnoFgNqFbavKf4TaNlGlPPYQjSkiGAfL3m/0alK5hBxkmEja6nwmV pJqd1xXxlYhKMGPA9pd7aFBRLtnI4UvPHg6yFIHsZmB7XTrF2X+w5AFoErRSjZvxGgsQ JaYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=SItJ1bZfQlObrrROlgGvFE93btikGDcQRvWQduZ0QeA=; b=mBQI8zXUiUUhwjYltfnWE0SlL1mo3+AMWbggz39NTSsJL+qpqUW4KImEUSTytxvhtm VRPsHlGSmyJKq6kvg/IjPadrjUdB3fWG53cdXZz1Crv8EvJ354rQoQWrZYNGya4aLwf8 WvL116L1hSiQ668QKOGmaU5LuTTmFM6MMru4A9LbNNbn2h8vmVZhqfUhChkYPZcfLs9f EfWEK6QOIPPWGDdXduyGpnXA0SjFUYXQWzBQzPJSb0H0EEvgvtrtTh6UiFvL3GLu19vj ySA18xn4q8h3VQ5YCZvNFcIzj7NmROYgMlJPAmEWrGUaJK1mzpXAY4Nsim4N9y5vLldi ijUg== X-Gm-Message-State: APjAAAWjCGEG2Kzzhxk3Xl5w0z8xRJDhqAExgtrR34uilXIrnzKjwoGO URkdGAz+xaO19PVTdJJxT6Uu8ohxBX3aoA== X-Google-Smtp-Source: APXvYqyIEGrsBaGpK6UCL9CnzFxQ/HPUlkuYWQnvgS3qMOvymaznkCuUP4UprvhylDbFvQ1Xz9xyeg== X-Received: by 2002:a0d:f204:: with SMTP id b4mr390343ywf.476.1579658440265; Tue, 21 Jan 2020 18:00:40 -0800 (PST) Received: from bobbye-pc.knology.net ([216.186.244.35]) by smtp.gmail.com with ESMTPSA id q185sm17504248ywh.61.2020.01.21.18.00.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Jan 2020 18:00:39 -0800 (PST) From: Bobby Eshleman To: xen-devel@lists.xenproject.org Date: Tue, 21 Jan 2020 19:58:47 -0600 Message-Id: X-Mailer: git-send-email 2.25.0 In-Reply-To: References: MIME-Version: 1.0 X-Mailman-Approved-At: Wed, 22 Jan 2020 05:14:01 +0000 Subject: [Xen-devel] [RFC XEN PATCH 08/23] riscv: Add delay.c X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Bobby Eshleman , Stefano Stabellini , Julien Grall , Wei Liu , Konrad Rzeszutek Wilk , George Dunlap , Andrew Cooper , Ian Jackson , Bobby Eshleman , Dan Robertson , Alistair Francis Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" From: Alistair Francis Signed-off-by: Alistair Francis --- xen/arch/riscv/delay.c | 114 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 xen/arch/riscv/delay.c diff --git a/xen/arch/riscv/delay.c b/xen/arch/riscv/delay.c new file mode 100644 index 0000000000..fe8e7b0575 --- /dev/null +++ b/xen/arch/riscv/delay.c @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2012 Regents of the University of California + * + * 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, version 2. + * + * 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. + */ + +#include +#include +#include +#include + +/* + * This is copies from arch/arm/include/asm/delay.h + * + * Loop (or tick) based delay: + * + * loops = loops_per_jiffy * jiffies_per_sec * delay_us / us_per_sec + * + * where: + * + * jiffies_per_sec = HZ + * us_per_sec = 1000000 + * + * Therefore the constant part is HZ / 1000000 which is a small + * fractional number. To make this usable with integer math, we + * scale up this constant by 2^31, perform the actual multiplication, + * and scale the result back down by 2^31 with a simple shift: + * + * loops = (loops_per_jiffy * delay_us * UDELAY_MULT) >> 31 + * + * where: + * + * UDELAY_MULT = 2^31 * HZ / 1000000 + * = (2^31 / 1000000) * HZ + * = 2147.483648 * HZ + * = 2147 * HZ + 483648 * HZ / 1000000 + * + * 31 is the biggest scale shift value that won't overflow 32 bits for + * delay_us * UDELAY_MULT assuming HZ <= 1000 and delay_us <= 2000. + */ +#define HZ 1000 +#define MAX_UDELAY_US 2000 +#define MAX_UDELAY_HZ 1000 +#define UDELAY_MULT (2147UL * HZ + 483648UL * HZ / 1000000UL) +#define UDELAY_SHIFT 31 + +#if HZ > MAX_UDELAY_HZ +#error "HZ > MAX_UDELAY_HZ" +#endif + +unsigned long riscv_timebase = 1000000; + +/* + * RISC-V supports both UDELAY and NDELAY. This is largely the same as above, + * but with different constants. I added 10 bits to the shift to get this, but + * the result is that I need a 64-bit multiply, which is slow on 32-bit + * platforms. + * + * NDELAY_MULT = 2^41 * HZ / 1000000000 + * = (2^41 / 1000000000) * HZ + * = 2199.02325555 * HZ + * = 2199 * HZ + 23255550 * HZ / 1000000000 + * + * The maximum here is to avoid 64-bit overflow, but it isn't checked as it + * won't happen. + */ +#define MAX_NDELAY_NS (1ULL << 42) +#define MAX_NDELAY_HZ MAX_UDELAY_HZ +#define NDELAY_MULT ((unsigned long long)(2199ULL * HZ + 23255550ULL * HZ / 1000000000ULL)) +#define NDELAY_SHIFT 41 + +#if HZ > MAX_NDELAY_HZ +#error "HZ > MAX_NDELAY_HZ" +#endif + +void __delay(unsigned long cycles) +{ + u64 t0 = get_cycles(); + + while ((unsigned long)(get_cycles() - t0) < cycles) + cpu_relax(); +} +EXPORT_SYMBOL(__delay); + +void udelay(unsigned long usecs) +{ + unsigned long ucycles = usecs * riscv_timebase * UDELAY_MULT / HZ; + + if (unlikely(usecs > MAX_UDELAY_US)) { + __delay((u64)usecs * riscv_timebase / 1000000ULL); + return; + } + + __delay(ucycles >> UDELAY_SHIFT); +} +EXPORT_SYMBOL(udelay); + +void ndelay(unsigned long nsecs) +{ + /* + * This doesn't bother checking for overflow, as it won't happen (it's + * an hour) of delay. + */ + unsigned long long ncycles = nsecs * riscv_timebase * NDELAY_MULT / HZ; + __delay(ncycles >> NDELAY_SHIFT); +} +EXPORT_SYMBOL(ndelay);