linux/kernel/time/tick-broadcast.c
<<
>>
Prefs
   1/*
   2 * linux/kernel/time/tick-broadcast.c
   3 *
   4 * This file contains functions which emulate a local clock-event
   5 * device via a broadcast event source.
   6 *
   7 * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
   8 * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
   9 * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner
  10 *
  11 * This code is licenced under the GPL version 2. For details see
  12 * kernel-base/COPYING.
  13 */
  14#include <linux/cpu.h>
  15#include <linux/err.h>
  16#include <linux/hrtimer.h>
  17#include <linux/interrupt.h>
  18#include <linux/percpu.h>
  19#include <linux/profile.h>
  20#include <linux/sched.h>
  21#include <linux/smp.h>
  22
  23#include "tick-internal.h"
  24
  25/*
  26 * Broadcast support for broken x86 hardware, where the local apic
  27 * timer stops in C3 state.
  28 */
  29
  30static struct tick_device tick_broadcast_device;
  31/* FIXME: Use cpumask_var_t. */
  32static DECLARE_BITMAP(tick_broadcast_mask, NR_CPUS);
  33static DECLARE_BITMAP(tmpmask, NR_CPUS);
  34static DEFINE_RAW_SPINLOCK(tick_broadcast_lock);
  35static int tick_broadcast_force;
  36
  37#ifdef CONFIG_TICK_ONESHOT
  38static void tick_broadcast_clear_oneshot(int cpu);
  39#else
  40static inline void tick_broadcast_clear_oneshot(int cpu) { }
  41#endif
  42
  43/*
  44 * Debugging: see timer_list.c
  45 */
  46struct tick_device *tick_get_broadcast_device(void)
  47{
  48        return &tick_broadcast_device;
  49}
  50
  51struct cpumask *tick_get_broadcast_mask(void)
  52{
  53        return to_cpumask(tick_broadcast_mask);
  54}
  55
  56/*
  57 * Start the device in periodic mode
  58 */
  59static void tick_broadcast_start_periodic(struct clock_event_device *bc)
  60{
  61        if (bc)
  62                tick_setup_periodic(bc, 1);
  63}
  64
  65/*
  66 * Check, if the device can be utilized as broadcast device:
  67 */
  68int tick_check_broadcast_device(struct clock_event_device *dev)
  69{
  70        struct clock_event_device *cur = tick_broadcast_device.evtdev;
  71
  72        if ((dev->features & CLOCK_EVT_FEAT_DUMMY) ||
  73            (tick_broadcast_device.evtdev &&
  74             tick_broadcast_device.evtdev->rating >= dev->rating) ||
  75             (dev->features & CLOCK_EVT_FEAT_C3STOP))
  76                return 0;
  77
  78        clockevents_exchange_device(tick_broadcast_device.evtdev, dev);
  79        if (cur)
  80                cur->event_handler = clockevents_handle_noop;
  81        tick_broadcast_device.evtdev = dev;
  82        if (!cpumask_empty(tick_get_broadcast_mask()))
  83                tick_broadcast_start_periodic(dev);
  84        return 1;
  85}
  86
  87/*
  88 * Check, if the device is the broadcast device
  89 */
  90int tick_is_broadcast_device(struct clock_event_device *dev)
  91{
  92        return (dev && tick_broadcast_device.evtdev == dev);
  93}
  94
  95static void err_broadcast(const struct cpumask *mask)
  96{
  97        pr_crit_once("Failed to broadcast timer tick. Some CPUs may be unresponsive.\n");
  98}
  99
 100static void tick_device_setup_broadcast_func(struct clock_event_device *dev)
 101{
 102        if (!dev->broadcast)
 103                dev->broadcast = tick_broadcast;
 104        if (!dev->broadcast) {
 105                pr_warn_once("%s depends on broadcast, but no broadcast function available\n",
 106                             dev->name);
 107                dev->broadcast = err_broadcast;
 108        }
 109}
 110
 111/*
 112 * Check, if the device is disfunctional and a place holder, which
 113 * needs to be handled by the broadcast device.
 114 */
 115int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
 116{
 117        unsigned long flags;
 118        int ret = 0;
 119
 120        raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
 121
 122        /*
 123         * Devices might be registered with both periodic and oneshot
 124         * mode disabled. This signals, that the device needs to be
 125         * operated from the broadcast device and is a placeholder for
 126         * the cpu local device.
 127         */
 128        if (!tick_device_is_functional(dev)) {
 129                dev->event_handler = tick_handle_periodic;
 130                tick_device_setup_broadcast_func(dev);
 131                cpumask_set_cpu(cpu, tick_get_broadcast_mask());
 132                tick_broadcast_start_periodic(tick_broadcast_device.evtdev);
 133                ret = 1;
 134        } else {
 135                /*
 136                 * When the new device is not affected by the stop
 137                 * feature and the cpu is marked in the broadcast mask
 138                 * then clear the broadcast bit.
 139                 */
 140                if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) {
 141                        int cpu = smp_processor_id();
 142                        cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
 143                        tick_broadcast_clear_oneshot(cpu);
 144                } else {
 145                        tick_device_setup_broadcast_func(dev);
 146                }
 147        }
 148        raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
 149        return ret;
 150}
 151
 152#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
 153int tick_receive_broadcast(void)
 154{
 155        struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
 156        struct clock_event_device *evt = td->evtdev;
 157
 158        if (!evt)
 159                return -ENODEV;
 160
 161        if (!evt->event_handler)
 162                return -EINVAL;
 163
 164        evt->event_handler(evt);
 165        return 0;
 166}
 167#endif
 168
 169/*
 170 * Broadcast the event to the cpus, which are set in the mask (mangled).
 171 */
 172static void tick_do_broadcast(struct cpumask *mask)
 173{
 174        int cpu = smp_processor_id();
 175        struct tick_device *td;
 176
 177        /*
 178         * Check, if the current cpu is in the mask
 179         */
 180        if (cpumask_test_cpu(cpu, mask)) {
 181                cpumask_clear_cpu(cpu, mask);
 182                td = &per_cpu(tick_cpu_device, cpu);
 183                td->evtdev->event_handler(td->evtdev);
 184        }
 185
 186        if (!cpumask_empty(mask)) {
 187                /*
 188                 * It might be necessary to actually check whether the devices
 189                 * have different broadcast functions. For now, just use the
 190                 * one of the first device. This works as long as we have this
 191                 * misfeature only on x86 (lapic)
 192                 */
 193                td = &per_cpu(tick_cpu_device, cpumask_first(mask));
 194                td->evtdev->broadcast(mask);
 195        }
 196}
 197
 198/*
 199 * Periodic broadcast:
 200 * - invoke the broadcast handlers
 201 */
 202static void tick_do_periodic_broadcast(void)
 203{
 204        raw_spin_lock(&tick_broadcast_lock);
 205
 206        cpumask_and(to_cpumask(tmpmask),
 207                    cpu_online_mask, tick_get_broadcast_mask());
 208        tick_do_broadcast(to_cpumask(tmpmask));
 209
 210        raw_spin_unlock(&tick_broadcast_lock);
 211}
 212
 213/*
 214 * Event handler for periodic broadcast ticks
 215 */
 216static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
 217{
 218        ktime_t next;
 219
 220        tick_do_periodic_broadcast();
 221
 222        /*
 223         * The device is in periodic mode. No reprogramming necessary:
 224         */
 225        if (dev->mode == CLOCK_EVT_MODE_PERIODIC)
 226                return;
 227
 228        /*
 229         * Setup the next period for devices, which do not have
 230         * periodic mode. We read dev->next_event first and add to it
 231         * when the event already expired. clockevents_program_event()
 232         * sets dev->next_event only when the event is really
 233         * programmed to the device.
 234         */
 235        for (next = dev->next_event; ;) {
 236                next = ktime_add(next, tick_period);
 237
 238                if (!clockevents_program_event(dev, next, false))
 239                        return;
 240                tick_do_periodic_broadcast();
 241        }
 242}
 243
 244/*
 245 * Powerstate information: The system enters/leaves a state, where
 246 * affected devices might stop
 247 */
 248static void tick_do_broadcast_on_off(unsigned long *reason)
 249{
 250        struct clock_event_device *bc, *dev;
 251        struct tick_device *td;
 252        unsigned long flags;
 253        int cpu, bc_stopped;
 254
 255        raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
 256
 257        cpu = smp_processor_id();
 258        td = &per_cpu(tick_cpu_device, cpu);
 259        dev = td->evtdev;
 260        bc = tick_broadcast_device.evtdev;
 261
 262        /*
 263         * Is the device not affected by the powerstate ?
 264         */
 265        if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
 266                goto out;
 267
 268        if (!tick_device_is_functional(dev))
 269                goto out;
 270
 271        bc_stopped = cpumask_empty(tick_get_broadcast_mask());
 272
 273        switch (*reason) {
 274        case CLOCK_EVT_NOTIFY_BROADCAST_ON:
 275        case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
 276                if (!cpumask_test_cpu(cpu, tick_get_broadcast_mask())) {
 277                        cpumask_set_cpu(cpu, tick_get_broadcast_mask());
 278                        if (tick_broadcast_device.mode ==
 279                            TICKDEV_MODE_PERIODIC)
 280                                clockevents_shutdown(dev);
 281                }
 282                if (*reason == CLOCK_EVT_NOTIFY_BROADCAST_FORCE)
 283                        tick_broadcast_force = 1;
 284                break;
 285        case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
 286                if (!tick_broadcast_force &&
 287                    cpumask_test_cpu(cpu, tick_get_broadcast_mask())) {
 288                        cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
 289                        if (tick_broadcast_device.mode ==
 290                            TICKDEV_MODE_PERIODIC)
 291                                tick_setup_periodic(dev, 0);
 292                }
 293                break;
 294        }
 295
 296        if (cpumask_empty(tick_get_broadcast_mask())) {
 297                if (!bc_stopped)
 298                        clockevents_shutdown(bc);
 299        } else if (bc_stopped) {
 300                if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
 301                        tick_broadcast_start_periodic(bc);
 302                else
 303                        tick_broadcast_setup_oneshot(bc);
 304        }
 305out:
 306        raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
 307}
 308
 309/*
 310 * Powerstate information: The system enters/leaves a state, where
 311 * affected devices might stop.
 312 */
 313void tick_broadcast_on_off(unsigned long reason, int *oncpu)
 314{
 315        if (!cpumask_test_cpu(*oncpu, cpu_online_mask))
 316                printk(KERN_ERR "tick-broadcast: ignoring broadcast for "
 317                       "offline CPU #%d\n", *oncpu);
 318        else
 319                tick_do_broadcast_on_off(&reason);
 320}
 321
 322/*
 323 * Set the periodic handler depending on broadcast on/off
 324 */
 325void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
 326{
 327        if (!broadcast)
 328                dev->event_handler = tick_handle_periodic;
 329        else
 330                dev->event_handler = tick_handle_periodic_broadcast;
 331}
 332
 333/*
 334 * Remove a CPU from broadcasting
 335 */
 336void tick_shutdown_broadcast(unsigned int *cpup)
 337{
 338        struct clock_event_device *bc;
 339        unsigned long flags;
 340        unsigned int cpu = *cpup;
 341
 342        raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
 343
 344        bc = tick_broadcast_device.evtdev;
 345        cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
 346
 347        if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
 348                if (bc && cpumask_empty(tick_get_broadcast_mask()))
 349                        clockevents_shutdown(bc);
 350        }
 351
 352        raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
 353}
 354
 355void tick_suspend_broadcast(void)
 356{
 357        struct clock_event_device *bc;
 358        unsigned long flags;
 359
 360        raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
 361
 362        bc = tick_broadcast_device.evtdev;
 363        if (bc)
 364                clockevents_shutdown(bc);
 365
 366        raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
 367}
 368
 369int tick_resume_broadcast(void)
 370{
 371        struct clock_event_device *bc;
 372        unsigned long flags;
 373        int broadcast = 0;
 374
 375        raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
 376
 377        bc = tick_broadcast_device.evtdev;
 378
 379        if (bc) {
 380                clockevents_set_mode(bc, CLOCK_EVT_MODE_RESUME);
 381
 382                switch (tick_broadcast_device.mode) {
 383                case TICKDEV_MODE_PERIODIC:
 384                        if (!cpumask_empty(tick_get_broadcast_mask()))
 385                                tick_broadcast_start_periodic(bc);
 386                        broadcast = cpumask_test_cpu(smp_processor_id(),
 387                                                     tick_get_broadcast_mask());
 388                        break;
 389                case TICKDEV_MODE_ONESHOT:
 390                        if (!cpumask_empty(tick_get_broadcast_mask()))
 391                                broadcast = tick_resume_broadcast_oneshot(bc);
 392                        break;
 393                }
 394        }
 395        raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
 396
 397        return broadcast;
 398}
 399
 400
 401#ifdef CONFIG_TICK_ONESHOT
 402
 403/* FIXME: use cpumask_var_t. */
 404static DECLARE_BITMAP(tick_broadcast_oneshot_mask, NR_CPUS);
 405
 406/*
 407 * Exposed for debugging: see timer_list.c
 408 */
 409struct cpumask *tick_get_broadcast_oneshot_mask(void)
 410{
 411        return to_cpumask(tick_broadcast_oneshot_mask);
 412}
 413
 414static int tick_broadcast_set_event(ktime_t expires, int force)
 415{
 416        struct clock_event_device *bc = tick_broadcast_device.evtdev;
 417
 418        if (bc->mode != CLOCK_EVT_MODE_ONESHOT)
 419                clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
 420
 421        return clockevents_program_event(bc, expires, force);
 422}
 423
 424int tick_resume_broadcast_oneshot(struct clock_event_device *bc)
 425{
 426        clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
 427        return 0;
 428}
 429
 430/*
 431 * Called from irq_enter() when idle was interrupted to reenable the
 432 * per cpu device.
 433 */
 434void tick_check_oneshot_broadcast(int cpu)
 435{
 436        if (cpumask_test_cpu(cpu, to_cpumask(tick_broadcast_oneshot_mask))) {
 437                struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
 438
 439                clockevents_set_mode(td->evtdev, CLOCK_EVT_MODE_ONESHOT);
 440        }
 441}
 442
 443/*
 444 * Handle oneshot mode broadcasting
 445 */
 446static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
 447{
 448        struct tick_device *td;
 449        ktime_t now, next_event;
 450        int cpu;
 451
 452        raw_spin_lock(&tick_broadcast_lock);
 453again:
 454        dev->next_event.tv64 = KTIME_MAX;
 455        next_event.tv64 = KTIME_MAX;
 456        cpumask_clear(to_cpumask(tmpmask));
 457        now = ktime_get();
 458        /* Find all expired events */
 459        for_each_cpu(cpu, tick_get_broadcast_oneshot_mask()) {
 460                td = &per_cpu(tick_cpu_device, cpu);
 461                if (td->evtdev->next_event.tv64 <= now.tv64)
 462                        cpumask_set_cpu(cpu, to_cpumask(tmpmask));
 463                else if (td->evtdev->next_event.tv64 < next_event.tv64)
 464                        next_event.tv64 = td->evtdev->next_event.tv64;
 465        }
 466
 467        /*
 468         * Wakeup the cpus which have an expired event.
 469         */
 470        tick_do_broadcast(to_cpumask(tmpmask));
 471
 472        /*
 473         * Two reasons for reprogram:
 474         *
 475         * - The global event did not expire any CPU local
 476         * events. This happens in dyntick mode, as the maximum PIT
 477         * delta is quite small.
 478         *
 479         * - There are pending events on sleeping CPUs which were not
 480         * in the event mask
 481         */
 482        if (next_event.tv64 != KTIME_MAX) {
 483                /*
 484                 * Rearm the broadcast device. If event expired,
 485                 * repeat the above
 486                 */
 487                if (tick_broadcast_set_event(next_event, 0))
 488                        goto again;
 489        }
 490        raw_spin_unlock(&tick_broadcast_lock);
 491}
 492
 493/*
 494 * Powerstate information: The system enters/leaves a state, where
 495 * affected devices might stop
 496 */
 497void tick_broadcast_oneshot_control(unsigned long reason)
 498{
 499        struct clock_event_device *bc, *dev;
 500        struct tick_device *td;
 501        unsigned long flags;
 502        int cpu;
 503
 504        /*
 505         * Periodic mode does not care about the enter/exit of power
 506         * states
 507         */
 508        if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
 509                return;
 510
 511        /*
 512         * We are called with preemtion disabled from the depth of the
 513         * idle code, so we can't be moved away.
 514         */
 515        cpu = smp_processor_id();
 516        td = &per_cpu(tick_cpu_device, cpu);
 517        dev = td->evtdev;
 518
 519        if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
 520                return;
 521
 522        bc = tick_broadcast_device.evtdev;
 523
 524        raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
 525        if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
 526                if (!cpumask_test_cpu(cpu, tick_get_broadcast_oneshot_mask())) {
 527                        cpumask_set_cpu(cpu, tick_get_broadcast_oneshot_mask());
 528                        clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
 529                        if (dev->next_event.tv64 < bc->next_event.tv64)
 530                                tick_broadcast_set_event(dev->next_event, 1);
 531                }
 532        } else {
 533                if (cpumask_test_cpu(cpu, tick_get_broadcast_oneshot_mask())) {
 534                        cpumask_clear_cpu(cpu,
 535                                          tick_get_broadcast_oneshot_mask());
 536                        clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
 537                        if (dev->next_event.tv64 != KTIME_MAX)
 538                                tick_program_event(dev->next_event, 1);
 539                }
 540        }
 541        raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
 542}
 543
 544/*
 545 * Reset the one shot broadcast for a cpu
 546 *
 547 * Called with tick_broadcast_lock held
 548 */
 549static void tick_broadcast_clear_oneshot(int cpu)
 550{
 551        cpumask_clear_cpu(cpu, tick_get_broadcast_oneshot_mask());
 552}
 553
 554static void tick_broadcast_init_next_event(struct cpumask *mask,
 555                                           ktime_t expires)
 556{
 557        struct tick_device *td;
 558        int cpu;
 559
 560        for_each_cpu(cpu, mask) {
 561                td = &per_cpu(tick_cpu_device, cpu);
 562                if (td->evtdev)
 563                        td->evtdev->next_event = expires;
 564        }
 565}
 566
 567/**
 568 * tick_broadcast_setup_oneshot - setup the broadcast device
 569 */
 570void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
 571{
 572        int cpu = smp_processor_id();
 573
 574        /* Set it up only once ! */
 575        if (bc->event_handler != tick_handle_oneshot_broadcast) {
 576                int was_periodic = bc->mode == CLOCK_EVT_MODE_PERIODIC;
 577
 578                bc->event_handler = tick_handle_oneshot_broadcast;
 579
 580                /* Take the do_timer update */
 581                tick_do_timer_cpu = cpu;
 582
 583                /*
 584                 * We must be careful here. There might be other CPUs
 585                 * waiting for periodic broadcast. We need to set the
 586                 * oneshot_mask bits for those and program the
 587                 * broadcast device to fire.
 588                 */
 589                cpumask_copy(to_cpumask(tmpmask), tick_get_broadcast_mask());
 590                cpumask_clear_cpu(cpu, to_cpumask(tmpmask));
 591                cpumask_or(tick_get_broadcast_oneshot_mask(),
 592                           tick_get_broadcast_oneshot_mask(),
 593                           to_cpumask(tmpmask));
 594
 595                if (was_periodic && !cpumask_empty(to_cpumask(tmpmask))) {
 596                        clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
 597                        tick_broadcast_init_next_event(to_cpumask(tmpmask),
 598                                                       tick_next_period);
 599                        tick_broadcast_set_event(tick_next_period, 1);
 600                } else
 601                        bc->next_event.tv64 = KTIME_MAX;
 602        } else {
 603                /*
 604                 * The first cpu which switches to oneshot mode sets
 605                 * the bit for all other cpus which are in the general
 606                 * (periodic) broadcast mask. So the bit is set and
 607                 * would prevent the first broadcast enter after this
 608                 * to program the bc device.
 609                 */
 610                tick_broadcast_clear_oneshot(cpu);
 611        }
 612}
 613
 614/*
 615 * Select oneshot operating mode for the broadcast device
 616 */
 617void tick_broadcast_switch_to_oneshot(void)
 618{
 619        struct clock_event_device *bc;
 620        unsigned long flags;
 621
 622        raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
 623
 624        tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
 625        bc = tick_broadcast_device.evtdev;
 626        if (bc)
 627                tick_broadcast_setup_oneshot(bc);
 628
 629        raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
 630}
 631
 632
 633/*
 634 * Remove a dead CPU from broadcasting
 635 */
 636void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
 637{
 638        unsigned long flags;
 639        unsigned int cpu = *cpup;
 640
 641        raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
 642
 643        /*
 644         * Clear the broadcast mask flag for the dead cpu, but do not
 645         * stop the broadcast device!
 646         */
 647        cpumask_clear_cpu(cpu, tick_get_broadcast_oneshot_mask());
 648
 649        raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
 650}
 651
 652/*
 653 * Check, whether the broadcast device is in one shot mode
 654 */
 655int tick_broadcast_oneshot_active(void)
 656{
 657        return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
 658}
 659
 660/*
 661 * Check whether the broadcast device supports oneshot.
 662 */
 663bool tick_broadcast_oneshot_available(void)
 664{
 665        struct clock_event_device *bc = tick_broadcast_device.evtdev;
 666
 667        return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
 668}
 669
 670#endif
 671
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.