linux/arch/x86/platform/uv/uv_time.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * SGI RTC clock/timer routines.
   4 *
   5 *  (C) Copyright 2020 Hewlett Packard Enterprise Development LP
   6 *  Copyright (c) 2009-2013 Silicon Graphics, Inc.  All Rights Reserved.
   7 *  Copyright (c) Dimitri Sivanich
   8 */
   9#include <linux/clockchips.h>
  10#include <linux/slab.h>
  11
  12#include <asm/uv/uv_mmrs.h>
  13#include <asm/uv/uv_hub.h>
  14#include <asm/uv/bios.h>
  15#include <asm/uv/uv.h>
  16#include <asm/apic.h>
  17#include <asm/cpu.h>
  18
  19#define RTC_NAME                "sgi_rtc"
  20
  21static u64 uv_read_rtc(struct clocksource *cs);
  22static int uv_rtc_next_event(unsigned long, struct clock_event_device *);
  23static int uv_rtc_shutdown(struct clock_event_device *evt);
  24
  25static struct clocksource clocksource_uv = {
  26        .name           = RTC_NAME,
  27        .rating         = 299,
  28        .read           = uv_read_rtc,
  29        .mask           = (u64)UVH_RTC_REAL_TIME_CLOCK_MASK,
  30        .flags          = CLOCK_SOURCE_IS_CONTINUOUS,
  31};
  32
  33static struct clock_event_device clock_event_device_uv = {
  34        .name                   = RTC_NAME,
  35        .features               = CLOCK_EVT_FEAT_ONESHOT,
  36        .shift                  = 20,
  37        .rating                 = 400,
  38        .irq                    = -1,
  39        .set_next_event         = uv_rtc_next_event,
  40        .set_state_shutdown     = uv_rtc_shutdown,
  41        .event_handler          = NULL,
  42};
  43
  44static DEFINE_PER_CPU(struct clock_event_device, cpu_ced);
  45
  46/* There is one of these allocated per node */
  47struct uv_rtc_timer_head {
  48        spinlock_t      lock;
  49        /* next cpu waiting for timer, local node relative: */
  50        int             next_cpu;
  51        /* number of cpus on this node: */
  52        int             ncpus;
  53        struct {
  54                int     lcpu;           /* systemwide logical cpu number */
  55                u64     expires;        /* next timer expiration for this cpu */
  56        } cpu[1];
  57};
  58
  59/*
  60 * Access to uv_rtc_timer_head via blade id.
  61 */
  62static struct uv_rtc_timer_head         **blade_info __read_mostly;
  63
  64static int                              uv_rtc_evt_enable;
  65
  66/*
  67 * Hardware interface routines
  68 */
  69
  70/* Send IPIs to another node */
  71static void uv_rtc_send_IPI(int cpu)
  72{
  73        unsigned long apicid, val;
  74        int pnode;
  75
  76        apicid = cpu_physical_id(cpu);
  77        pnode = uv_apicid_to_pnode(apicid);
  78        val = (1UL << UVH_IPI_INT_SEND_SHFT) |
  79              (apicid << UVH_IPI_INT_APIC_ID_SHFT) |
  80              (X86_PLATFORM_IPI_VECTOR << UVH_IPI_INT_VECTOR_SHFT);
  81
  82        uv_write_global_mmr64(pnode, UVH_IPI_INT, val);
  83}
  84
  85/* Check for an RTC interrupt pending */
  86static int uv_intr_pending(int pnode)
  87{
  88        return uv_read_global_mmr64(pnode, UVH_EVENT_OCCURRED2) &
  89                UVH_EVENT_OCCURRED2_RTC_1_MASK;
  90}
  91
  92/* Setup interrupt and return non-zero if early expiration occurred. */
  93static int uv_setup_intr(int cpu, u64 expires)
  94{
  95        u64 val;
  96        unsigned long apicid = cpu_physical_id(cpu);
  97        int pnode = uv_cpu_to_pnode(cpu);
  98
  99        uv_write_global_mmr64(pnode, UVH_RTC1_INT_CONFIG,
 100                UVH_RTC1_INT_CONFIG_M_MASK);
 101        uv_write_global_mmr64(pnode, UVH_INT_CMPB, -1L);
 102
 103        uv_write_global_mmr64(pnode, UVH_EVENT_OCCURRED2_ALIAS,
 104                              UVH_EVENT_OCCURRED2_RTC_1_MASK);
 105
 106        val = (X86_PLATFORM_IPI_VECTOR << UVH_RTC1_INT_CONFIG_VECTOR_SHFT) |
 107                ((u64)apicid << UVH_RTC1_INT_CONFIG_APIC_ID_SHFT);
 108
 109        /* Set configuration */
 110        uv_write_global_mmr64(pnode, UVH_RTC1_INT_CONFIG, val);
 111        /* Initialize comparator value */
 112        uv_write_global_mmr64(pnode, UVH_INT_CMPB, expires);
 113
 114        if (uv_read_rtc(NULL) <= expires)
 115                return 0;
 116
 117        return !uv_intr_pending(pnode);
 118}
 119
 120/*
 121 * Per-cpu timer tracking routines
 122 */
 123
 124static __init void uv_rtc_deallocate_timers(void)
 125{
 126        int bid;
 127
 128        for_each_possible_blade(bid) {
 129                kfree(blade_info[bid]);
 130        }
 131        kfree(blade_info);
 132}
 133
 134/* Allocate per-node list of cpu timer expiration times. */
 135static __init int uv_rtc_allocate_timers(void)
 136{
 137        int cpu;
 138
 139        blade_info = kcalloc(uv_possible_blades, sizeof(void *), GFP_KERNEL);
 140        if (!blade_info)
 141                return -ENOMEM;
 142
 143        for_each_present_cpu(cpu) {
 144                int nid = cpu_to_node(cpu);
 145                int bid = uv_cpu_to_blade_id(cpu);
 146                int bcpu = uv_cpu_blade_processor_id(cpu);
 147                struct uv_rtc_timer_head *head = blade_info[bid];
 148
 149                if (!head) {
 150                        head = kmalloc_node(sizeof(struct uv_rtc_timer_head) +
 151                                (uv_blade_nr_possible_cpus(bid) *
 152                                        2 * sizeof(u64)),
 153                                GFP_KERNEL, nid);
 154                        if (!head) {
 155                                uv_rtc_deallocate_timers();
 156                                return -ENOMEM;
 157                        }
 158                        spin_lock_init(&head->lock);
 159                        head->ncpus = uv_blade_nr_possible_cpus(bid);
 160                        head->next_cpu = -1;
 161                        blade_info[bid] = head;
 162                }
 163
 164                head->cpu[bcpu].lcpu = cpu;
 165                head->cpu[bcpu].expires = ULLONG_MAX;
 166        }
 167
 168        return 0;
 169}
 170
 171/* Find and set the next expiring timer.  */
 172static void uv_rtc_find_next_timer(struct uv_rtc_timer_head *head, int pnode)
 173{
 174        u64 lowest = ULLONG_MAX;
 175        int c, bcpu = -1;
 176
 177        head->next_cpu = -1;
 178        for (c = 0; c < head->ncpus; c++) {
 179                u64 exp = head->cpu[c].expires;
 180                if (exp < lowest) {
 181                        bcpu = c;
 182                        lowest = exp;
 183                }
 184        }
 185        if (bcpu >= 0) {
 186                head->next_cpu = bcpu;
 187                c = head->cpu[bcpu].lcpu;
 188                if (uv_setup_intr(c, lowest))
 189                        /* If we didn't set it up in time, trigger */
 190                        uv_rtc_send_IPI(c);
 191        } else {
 192                uv_write_global_mmr64(pnode, UVH_RTC1_INT_CONFIG,
 193                        UVH_RTC1_INT_CONFIG_M_MASK);
 194        }
 195}
 196
 197/*
 198 * Set expiration time for current cpu.
 199 *
 200 * Returns 1 if we missed the expiration time.
 201 */
 202static int uv_rtc_set_timer(int cpu, u64 expires)
 203{
 204        int pnode = uv_cpu_to_pnode(cpu);
 205        int bid = uv_cpu_to_blade_id(cpu);
 206        struct uv_rtc_timer_head *head = blade_info[bid];
 207        int bcpu = uv_cpu_blade_processor_id(cpu);
 208        u64 *t = &head->cpu[bcpu].expires;
 209        unsigned long flags;
 210        int next_cpu;
 211
 212        spin_lock_irqsave(&head->lock, flags);
 213
 214        next_cpu = head->next_cpu;
 215        *t = expires;
 216
 217        /* Will this one be next to go off? */
 218        if (next_cpu < 0 || bcpu == next_cpu ||
 219                        expires < head->cpu[next_cpu].expires) {
 220                head->next_cpu = bcpu;
 221                if (uv_setup_intr(cpu, expires)) {
 222                        *t = ULLONG_MAX;
 223                        uv_rtc_find_next_timer(head, pnode);
 224                        spin_unlock_irqrestore(&head->lock, flags);
 225                        return -ETIME;
 226                }
 227        }
 228
 229        spin_unlock_irqrestore(&head->lock, flags);
 230        return 0;
 231}
 232
 233/*
 234 * Unset expiration time for current cpu.
 235 *
 236 * Returns 1 if this timer was pending.
 237 */
 238static int uv_rtc_unset_timer(int cpu, int force)
 239{
 240        int pnode = uv_cpu_to_pnode(cpu);
 241        int bid = uv_cpu_to_blade_id(cpu);
 242        struct uv_rtc_timer_head *head = blade_info[bid];
 243        int bcpu = uv_cpu_blade_processor_id(cpu);
 244        u64 *t = &head->cpu[bcpu].expires;
 245        unsigned long flags;
 246        int rc = 0;
 247
 248        spin_lock_irqsave(&head->lock, flags);
 249
 250        if ((head->next_cpu == bcpu && uv_read_rtc(NULL) >= *t) || force)
 251                rc = 1;
 252
 253        if (rc) {
 254                *t = ULLONG_MAX;
 255                /* Was the hardware setup for this timer? */
 256                if (head->next_cpu == bcpu)
 257                        uv_rtc_find_next_timer(head, pnode);
 258        }
 259
 260        spin_unlock_irqrestore(&head->lock, flags);
 261
 262        return rc;
 263}
 264
 265
 266/*
 267 * Kernel interface routines.
 268 */
 269
 270/*
 271 * Read the RTC.
 272 *
 273 * Starting with HUB rev 2.0, the UV RTC register is replicated across all
 274 * cachelines of it's own page.  This allows faster simultaneous reads
 275 * from a given socket.
 276 */
 277static u64 uv_read_rtc(struct clocksource *cs)
 278{
 279        unsigned long offset;
 280
 281        if (uv_get_min_hub_revision_id() == 1)
 282                offset = 0;
 283        else
 284                offset = (uv_blade_processor_id() * L1_CACHE_BYTES) % PAGE_SIZE;
 285
 286        return (u64)uv_read_local_mmr(UVH_RTC | offset);
 287}
 288
 289/*
 290 * Program the next event, relative to now
 291 */
 292static int uv_rtc_next_event(unsigned long delta,
 293                             struct clock_event_device *ced)
 294{
 295        int ced_cpu = cpumask_first(ced->cpumask);
 296
 297        return uv_rtc_set_timer(ced_cpu, delta + uv_read_rtc(NULL));
 298}
 299
 300/*
 301 * Shutdown the RTC timer
 302 */
 303static int uv_rtc_shutdown(struct clock_event_device *evt)
 304{
 305        int ced_cpu = cpumask_first(evt->cpumask);
 306
 307        uv_rtc_unset_timer(ced_cpu, 1);
 308        return 0;
 309}
 310
 311static void uv_rtc_interrupt(void)
 312{
 313        int cpu = smp_processor_id();
 314        struct clock_event_device *ced = &per_cpu(cpu_ced, cpu);
 315
 316        if (!ced || !ced->event_handler)
 317                return;
 318
 319        if (uv_rtc_unset_timer(cpu, 0) != 1)
 320                return;
 321
 322        ced->event_handler(ced);
 323}
 324
 325static int __init uv_enable_evt_rtc(char *str)
 326{
 327        uv_rtc_evt_enable = 1;
 328
 329        return 1;
 330}
 331__setup("uvrtcevt", uv_enable_evt_rtc);
 332
 333static __init void uv_rtc_register_clockevents(struct work_struct *dummy)
 334{
 335        struct clock_event_device *ced = this_cpu_ptr(&cpu_ced);
 336
 337        *ced = clock_event_device_uv;
 338        ced->cpumask = cpumask_of(smp_processor_id());
 339        clockevents_register_device(ced);
 340}
 341
 342static __init int uv_rtc_setup_clock(void)
 343{
 344        int rc;
 345
 346        if (!is_uv_system())
 347                return -ENODEV;
 348
 349        rc = clocksource_register_hz(&clocksource_uv, sn_rtc_cycles_per_second);
 350        if (rc)
 351                printk(KERN_INFO "UV RTC clocksource failed rc %d\n", rc);
 352        else
 353                printk(KERN_INFO "UV RTC clocksource registered freq %lu MHz\n",
 354                        sn_rtc_cycles_per_second/(unsigned long)1E6);
 355
 356        if (rc || !uv_rtc_evt_enable || x86_platform_ipi_callback)
 357                return rc;
 358
 359        /* Setup and register clockevents */
 360        rc = uv_rtc_allocate_timers();
 361        if (rc)
 362                goto error;
 363
 364        x86_platform_ipi_callback = uv_rtc_interrupt;
 365
 366        clock_event_device_uv.mult = div_sc(sn_rtc_cycles_per_second,
 367                                NSEC_PER_SEC, clock_event_device_uv.shift);
 368
 369        clock_event_device_uv.min_delta_ns = NSEC_PER_SEC /
 370                                                sn_rtc_cycles_per_second;
 371        clock_event_device_uv.min_delta_ticks = 1;
 372
 373        clock_event_device_uv.max_delta_ns = clocksource_uv.mask *
 374                                (NSEC_PER_SEC / sn_rtc_cycles_per_second);
 375        clock_event_device_uv.max_delta_ticks = clocksource_uv.mask;
 376
 377        rc = schedule_on_each_cpu(uv_rtc_register_clockevents);
 378        if (rc) {
 379                x86_platform_ipi_callback = NULL;
 380                uv_rtc_deallocate_timers();
 381                goto error;
 382        }
 383
 384        printk(KERN_INFO "UV RTC clockevents registered\n");
 385
 386        return 0;
 387
 388error:
 389        clocksource_unregister(&clocksource_uv);
 390        printk(KERN_INFO "UV RTC clockevents failed rc %d\n", rc);
 391
 392        return rc;
 393}
 394arch_initcall(uv_rtc_setup_clock);
 395