From patchwork Tue Dec 18 09:26:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Janosch Frank X-Patchwork-Id: 10735263 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 9DEB513A4 for ; Tue, 18 Dec 2018 09:29:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8DDEF29AAE for ; Tue, 18 Dec 2018 09:29:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8231F29B3F; Tue, 18 Dec 2018 09:29:36 +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 158DD29B59 for ; Tue, 18 Dec 2018 09:29:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726651AbeLRJ3d (ORCPT ); Tue, 18 Dec 2018 04:29:33 -0500 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:39508 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726650AbeLRJ3c (ORCPT ); Tue, 18 Dec 2018 04:29:32 -0500 Received: from pps.filterd (m0098416.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id wBI9SUI6083786 for ; Tue, 18 Dec 2018 04:29:30 -0500 Received: from e06smtp01.uk.ibm.com (e06smtp01.uk.ibm.com [195.75.94.97]) by mx0b-001b2d01.pphosted.com with ESMTP id 2pew6j36xn-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 18 Dec 2018 04:29:30 -0500 Received: from localhost by e06smtp01.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 18 Dec 2018 09:29:29 -0000 Received: from b06cxnps4076.portsmouth.uk.ibm.com (9.149.109.198) by e06smtp01.uk.ibm.com (192.168.101.131) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Tue, 18 Dec 2018 09:29:27 -0000 Received: from d06av24.portsmouth.uk.ibm.com (d06av24.portsmouth.uk.ibm.com [9.149.105.60]) by b06cxnps4076.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id wBI9TQuG6685086 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 18 Dec 2018 09:29:26 GMT Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 0F83942042; Tue, 18 Dec 2018 09:29:26 +0000 (GMT) Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D2EA642045; Tue, 18 Dec 2018 09:29:25 +0000 (GMT) Received: from s38lp20.lnxne.boe (unknown [9.152.224.44]) by d06av24.portsmouth.uk.ibm.com (Postfix) with ESMTP; Tue, 18 Dec 2018 09:29:25 +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 RFC v3 12/13] s390x: Add time functions to lib Date: Tue, 18 Dec 2018 10:26:56 +0100 X-Mailer: git-send-email 2.14.3 In-Reply-To: <20181218092657.46466-1-frankja@linux.ibm.com> References: <20181218092657.46466-1-frankja@linux.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 18121809-4275-0000-0000-000002F2311D X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18121809-4276-0000-0000-000038003228 Message-Id: <20181218092657.46466-13-frankja@linux.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-12-18_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=633 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1812180085 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 | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ s390x/Makefile | 1 + 3 files changed, 123 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..26060bd --- /dev/null +++ b/lib/s390x/time.h @@ -0,0 +1,104 @@ +/* + * 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 LGPL, version 2. + */ + +#ifndef TIME_H +#define TIME_H + +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 unsigned long long stck(void) +{ + unsigned long long tod; + + asm volatile( + " stck %[tod]\n" + : [tod] "=Q" (tod) : : "cc" + ); + return tod; +} + +static inline unsigned long long stckf(void) +{ + unsigned long long tod; + + asm volatile( + " .machine push\n" + " .machine \"z9-109\"\n" + " stckf %[tod]\n" + " .machine pop\n" + : [tod] "=Q" (tod) + : : "cc"); + return tod; +} + +static inline unsigned long long stcke(void) +{ + unsigned long long tod; + + asm volatile( + " stcke %[tod]\n" + : [tod] "=Q" (tod) + : : "cc"); + return tod; +} + + +/** + * 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 unsigned long long tod_to_ns(unsigned long long 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 eaad18c..f41d5be 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