From patchwork Thu Jan 3 10:08:05 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Janosch Frank X-Patchwork-Id: 10747129 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D560F6C5 for ; Thu, 3 Jan 2019 10:10:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C5864285D2 for ; Thu, 3 Jan 2019 10:10:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BA064285FD; Thu, 3 Jan 2019 10:10:07 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4DB98285D2 for ; Thu, 3 Jan 2019 10:10:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730784AbfACKKG (ORCPT ); Thu, 3 Jan 2019 05:10:06 -0500 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:42346 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730771AbfACKKF (ORCPT ); Thu, 3 Jan 2019 05:10:05 -0500 Received: from pps.filterd (m0098404.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id x03A9Kef075092 for ; Thu, 3 Jan 2019 05:10:04 -0500 Received: from e06smtp02.uk.ibm.com (e06smtp02.uk.ibm.com [195.75.94.98]) by mx0a-001b2d01.pphosted.com with ESMTP id 2pscfx0st6-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Thu, 03 Jan 2019 05:10:04 -0500 Received: from localhost by e06smtp02.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 3 Jan 2019 10:10:00 -0000 Received: from b06cxnps4076.portsmouth.uk.ibm.com (9.149.109.198) by e06smtp02.uk.ibm.com (192.168.101.132) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Thu, 3 Jan 2019 10:09:57 -0000 Received: from d06av23.portsmouth.uk.ibm.com (d06av23.portsmouth.uk.ibm.com [9.149.105.59]) by b06cxnps4076.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x03A9uEw60686496 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Thu, 3 Jan 2019 10:09:56 GMT Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C5130A405F; Thu, 3 Jan 2019 10:09:56 +0000 (GMT) Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 931C7A4055; Thu, 3 Jan 2019 10:09:56 +0000 (GMT) Received: from s38lp20.lnxne.boe (unknown [9.152.224.63]) by d06av23.portsmouth.uk.ibm.com (Postfix) with ESMTP; Thu, 3 Jan 2019 10:09:56 +0000 (GMT) From: Janosch Frank To: kvm@vger.kernel.org Cc: linux-s390@vger.kernel.org, david@redhat.com, thuth@redhat.com Subject: [kvm-unit-tests PATCH v4 12/13] s390x: Add time functions to lib Date: Thu, 3 Jan 2019 11:08:05 +0100 X-Mailer: git-send-email 2.14.3 In-Reply-To: <20190103100806.9039-1-frankja@linux.ibm.com> References: <20190103100806.9039-1-frankja@linux.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 19010310-0008-0000-0000-000002A9681E X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19010310-0009-0000-0000-000022157F7C Message-Id: <20190103100806.9039-13-frankja@linux.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-01-03_04:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=671 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1901030090 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Always good to know what time it is. Signed-off-by: Janosch Frank --- lib/s390x/time.c | 18 ++++++++++ lib/s390x/time.h | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ s390x/Makefile | 1 + 3 files changed, 127 insertions(+) create mode 100644 lib/s390x/time.c create mode 100644 lib/s390x/time.h diff --git a/lib/s390x/time.c b/lib/s390x/time.c new file mode 100644 index 0000000..e0fbcfc --- /dev/null +++ b/lib/s390x/time.c @@ -0,0 +1,18 @@ +#include +#include "time.h" + +uint64_t time_get_ns(void) +{ + unsigned long long tod; + + tod = stckf(); + return tod_to_ns(tod); +} + +uint64_t time_get_s(void) +{ + uint64_t ns; + + ns = time_get_ns(); + return ns / 1000000UL; +} diff --git a/lib/s390x/time.h b/lib/s390x/time.h new file mode 100644 index 0000000..96bb974 --- /dev/null +++ b/lib/s390x/time.h @@ -0,0 +1,108 @@ +/* + * Time related definitions. + * + * Mostly copied from arch/s390/include/asm/timex.h from the Linux + * kernel git. + * + * Copyright IBM Corp. 1999 + * + * Derived from "include/asm-i386/timex.h" + * Copyright (C) 1992, Linus Torvalds + * + * This work is licensed under the terms of the GNU GPL, version 2. + */ +#ifndef TIME_H +#define TIME_H + +/* The value of the TOD clock for 1.1.1970. */ +#define TOD_UNIX_EPOCH 0x7d91048bca000000ULL + +static inline void set_clock_comparator(uint64_t time) +{ + asm volatile("sckc %0" : : "Q" (time)); +} + +static inline void store_clock_comparator(uint64_t *time) +{ + asm volatile("stckc %0" : "=Q" (*time)); +} + +static inline int set_tod_clock(uint64_t time) +{ + int cc; + + asm volatile( + " sck %1\n" + " ipm %0\n" + " srl %0,28\n" + : "=d" (cc) : "Q" (time) : "cc"); + return cc; +} + +static inline uint64_t stck(void) +{ + uint64_t tod; + + asm volatile( + " stck %[tod]\n" + : [tod] "=Q" (tod) : : "cc" + ); + return tod; +} + +static inline uint64_t stckf(void) +{ + uint64_t tod; + + asm volatile( + " .machine push\n" + " .machine \"z9-109\"\n" + " stckf %[tod]\n" + " .machine pop\n" + : [tod] "=Q" (tod) + : : "cc"); + return tod; +} + +/* + * STCKE returns 16 bytes, which include the epoch, tod value and a + * programmable field. The value has to be trimmed to be able to work + * with it. As there's still some time to 2042, this code is not ready + * for multi epoch values. + */ +static inline void stcke(char *tod) +{ + asm volatile( + " stcke %[tod]\n" + : [tod] "=Q" (tod) + : : "cc"); +} + +/** + * tod_to_ns - convert a TOD format value to nanoseconds + * @todval: to be converted TOD format value + * Returns: number of nanoseconds that correspond to the TOD format value + * + * Converting a 64 Bit TOD format value to nanoseconds means that the value + * must be divided by 4.096. In order to achieve that we multiply with 125 + * and divide by 512: + * + * ns = (todval * 125) >> 9; + * + * In order to avoid an overflow with the multiplication we can rewrite this. + * With a split todval == 2^9 * th + tl (th upper 55 bits, tl lower 9 bits) + * we end up with + * + * ns = ((2^9 * th + tl) * 125 ) >> 9; + * -> ns = (th * 125) + ((tl * 125) >> 9); + * + */ +static inline uint64_t tod_to_ns(uint64_t todval) +{ + return ((todval >> 9) * 125) + (((todval & 0x1ff) * 125) >> 9); +} + + +uint64_t time_get_ns(void); +uint64_t time_get_s(void); +#endif diff --git a/s390x/Makefile b/s390x/Makefile index 82c4f15..afee4c9 100644 --- a/s390x/Makefile +++ b/s390x/Makefile @@ -43,6 +43,7 @@ cflatobjs += lib/s390x/sclp.o cflatobjs += lib/s390x/sclp-console.o cflatobjs += lib/s390x/interrupt.o cflatobjs += lib/s390x/mmu.o +cflatobjs += lib/s390x/time.o OBJDIRS += lib/s390x