Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Idle functions for s390.
0004  *
0005  * Copyright IBM Corp. 2014
0006  *
0007  * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
0008  */
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/kernel_stat.h>
0012 #include <linux/notifier.h>
0013 #include <linux/init.h>
0014 #include <linux/cpu.h>
0015 #include <linux/sched/cputime.h>
0016 #include <trace/events/power.h>
0017 #include <asm/cpu_mf.h>
0018 #include <asm/nmi.h>
0019 #include <asm/smp.h>
0020 #include "entry.h"
0021 
0022 static DEFINE_PER_CPU(struct s390_idle_data, s390_idle);
0023 
0024 void account_idle_time_irq(void)
0025 {
0026     struct s390_idle_data *idle = this_cpu_ptr(&s390_idle);
0027     u64 cycles_new[8];
0028     int i;
0029 
0030     clear_cpu_flag(CIF_ENABLED_WAIT);
0031     if (smp_cpu_mtid) {
0032         stcctm(MT_DIAG, smp_cpu_mtid, cycles_new);
0033         for (i = 0; i < smp_cpu_mtid; i++)
0034             this_cpu_add(mt_cycles[i], cycles_new[i] - idle->mt_cycles_enter[i]);
0035     }
0036 
0037     idle->clock_idle_exit = S390_lowcore.int_clock;
0038     idle->timer_idle_exit = S390_lowcore.sys_enter_timer;
0039 
0040     S390_lowcore.steal_timer += idle->clock_idle_enter - S390_lowcore.last_update_clock;
0041     S390_lowcore.last_update_clock = idle->clock_idle_exit;
0042 
0043     S390_lowcore.system_timer += S390_lowcore.last_update_timer - idle->timer_idle_enter;
0044     S390_lowcore.last_update_timer = idle->timer_idle_exit;
0045 }
0046 
0047 void arch_cpu_idle(void)
0048 {
0049     struct s390_idle_data *idle = this_cpu_ptr(&s390_idle);
0050     unsigned long idle_time;
0051     unsigned long psw_mask;
0052 
0053     /* Wait for external, I/O or machine check interrupt. */
0054     psw_mask = PSW_KERNEL_BITS | PSW_MASK_WAIT | PSW_MASK_DAT |
0055         PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
0056     clear_cpu_flag(CIF_NOHZ_DELAY);
0057 
0058     /* psw_idle() returns with interrupts disabled. */
0059     psw_idle(idle, psw_mask);
0060 
0061     /* Account time spent with enabled wait psw loaded as idle time. */
0062     raw_write_seqcount_begin(&idle->seqcount);
0063     idle_time = idle->clock_idle_exit - idle->clock_idle_enter;
0064     idle->clock_idle_enter = idle->clock_idle_exit = 0ULL;
0065     idle->idle_time += idle_time;
0066     idle->idle_count++;
0067     account_idle_time(cputime_to_nsecs(idle_time));
0068     raw_write_seqcount_end(&idle->seqcount);
0069     raw_local_irq_enable();
0070 }
0071 
0072 static ssize_t show_idle_count(struct device *dev,
0073                 struct device_attribute *attr, char *buf)
0074 {
0075     struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
0076     unsigned long idle_count;
0077     unsigned int seq;
0078 
0079     do {
0080         seq = read_seqcount_begin(&idle->seqcount);
0081         idle_count = READ_ONCE(idle->idle_count);
0082         if (READ_ONCE(idle->clock_idle_enter))
0083             idle_count++;
0084     } while (read_seqcount_retry(&idle->seqcount, seq));
0085     return sprintf(buf, "%lu\n", idle_count);
0086 }
0087 DEVICE_ATTR(idle_count, 0444, show_idle_count, NULL);
0088 
0089 static ssize_t show_idle_time(struct device *dev,
0090                 struct device_attribute *attr, char *buf)
0091 {
0092     unsigned long now, idle_time, idle_enter, idle_exit, in_idle;
0093     struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
0094     unsigned int seq;
0095 
0096     do {
0097         seq = read_seqcount_begin(&idle->seqcount);
0098         idle_time = READ_ONCE(idle->idle_time);
0099         idle_enter = READ_ONCE(idle->clock_idle_enter);
0100         idle_exit = READ_ONCE(idle->clock_idle_exit);
0101     } while (read_seqcount_retry(&idle->seqcount, seq));
0102     in_idle = 0;
0103     now = get_tod_clock();
0104     if (idle_enter) {
0105         if (idle_exit) {
0106             in_idle = idle_exit - idle_enter;
0107         } else if (now > idle_enter) {
0108             in_idle = now - idle_enter;
0109         }
0110     }
0111     idle_time += in_idle;
0112     return sprintf(buf, "%lu\n", idle_time >> 12);
0113 }
0114 DEVICE_ATTR(idle_time_us, 0444, show_idle_time, NULL);
0115 
0116 u64 arch_cpu_idle_time(int cpu)
0117 {
0118     struct s390_idle_data *idle = &per_cpu(s390_idle, cpu);
0119     unsigned long now, idle_enter, idle_exit, in_idle;
0120     unsigned int seq;
0121 
0122     do {
0123         seq = read_seqcount_begin(&idle->seqcount);
0124         idle_enter = READ_ONCE(idle->clock_idle_enter);
0125         idle_exit = READ_ONCE(idle->clock_idle_exit);
0126     } while (read_seqcount_retry(&idle->seqcount, seq));
0127     in_idle = 0;
0128     now = get_tod_clock();
0129     if (idle_enter) {
0130         if (idle_exit) {
0131             in_idle = idle_exit - idle_enter;
0132         } else if (now > idle_enter) {
0133             in_idle = now - idle_enter;
0134         }
0135     }
0136     return cputime_to_nsecs(in_idle);
0137 }
0138 
0139 void arch_cpu_idle_enter(void)
0140 {
0141 }
0142 
0143 void arch_cpu_idle_exit(void)
0144 {
0145 }
0146 
0147 void arch_cpu_idle_dead(void)
0148 {
0149     cpu_die();
0150 }