Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'x86-timers-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip

Pull x86 timer updates from Thomas Gleixner:
"A rather large series consolidating the HPET code, which was triggered
by the attempt to bolt HPET NMI watchdog support on to the existing
maze with the usual duct tape and super glue approach.

This mainly removes two separate partially redundant storage layers
and consolidates them into a single one which provides a consistent
view of the different HPET channels and their usage and allows to
integrate HPET NMI watchdog support (if it turns out to be feasible)
in a non intrusive way"

* 'x86-timers-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: (29 commits)
x86/hpet: Use channel for legacy clockevent storage
x86/hpet: Use common init for legacy clockevent
x86/hpet: Carve out shareable parts of init_one_hpet_msi_clockevent()
x86/hpet: Consolidate clockevent functions
x86/hpet: Wrap legacy clockevent in hpet_channel
x86/hpet: Use cached info instead of extra flags
x86/hpet: Move clockevents into channels
x86/hpet: Rename variables to prepare for switching to channels
x86/hpet: Add function to select a /dev/hpet channel
x86/hpet: Add mode information to struct hpet_channel
x86/hpet: Use cached channel data
x86/hpet: Introduce struct hpet_base and struct hpet_channel
x86/hpet: Coding style cleanup
x86/hpet: Clean up comments
x86/hpet: Make naming consistent
x86/hpet: Remove not required includes
x86/hpet: Decapitalize and rename EVT_TO_HPET_DEV
x86/hpet: Simplify counter validation
x86/hpet: Separate counter check out of clocksource register code
x86/hpet: Shuffle code around for readability sake
...

+432 -524
+3 -4
arch/x86/include/asm/hpet.h
··· 75 75 extern void force_hpet_resume(void); 76 76 77 77 struct irq_data; 78 - struct hpet_dev; 78 + struct hpet_channel; 79 79 struct irq_domain; 80 80 81 81 extern void hpet_msi_unmask(struct irq_data *data); 82 82 extern void hpet_msi_mask(struct irq_data *data); 83 - extern void hpet_msi_write(struct hpet_dev *hdev, struct msi_msg *msg); 84 - extern void hpet_msi_read(struct hpet_dev *hdev, struct msi_msg *msg); 83 + extern void hpet_msi_write(struct hpet_channel *hc, struct msi_msg *msg); 85 84 extern struct irq_domain *hpet_create_irq_domain(int hpet_id); 86 85 extern int hpet_assign_irq(struct irq_domain *domain, 87 - struct hpet_dev *dev, int dev_num); 86 + struct hpet_channel *hc, int dev_num); 88 87 89 88 #ifdef CONFIG_HPET_EMULATE_RTC 90 89
+2 -2
arch/x86/kernel/apic/msi.c
··· 370 370 return d; 371 371 } 372 372 373 - int hpet_assign_irq(struct irq_domain *domain, struct hpet_dev *dev, 373 + int hpet_assign_irq(struct irq_domain *domain, struct hpet_channel *hc, 374 374 int dev_num) 375 375 { 376 376 struct irq_alloc_info info; 377 377 378 378 init_irq_alloc_info(&info, NULL); 379 379 info.type = X86_IRQ_ALLOC_TYPE_HPET; 380 - info.hpet_data = dev; 380 + info.hpet_data = hc; 381 381 info.hpet_id = hpet_dev_id(domain); 382 382 info.hpet_index = dev_num; 383 383
+427 -518
arch/x86/kernel/hpet.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0-only 2 - #include <linux/clocksource.h> 3 2 #include <linux/clockchips.h> 4 3 #include <linux/interrupt.h> 5 - #include <linux/irq.h> 6 4 #include <linux/export.h> 7 5 #include <linux/delay.h> 8 - #include <linux/errno.h> 9 - #include <linux/i8253.h> 10 - #include <linux/slab.h> 11 6 #include <linux/hpet.h> 12 - #include <linux/init.h> 13 7 #include <linux/cpu.h> 14 - #include <linux/pm.h> 15 - #include <linux/io.h> 8 + #include <linux/irq.h> 16 9 17 - #include <asm/cpufeature.h> 18 - #include <asm/irqdomain.h> 19 - #include <asm/fixmap.h> 20 10 #include <asm/hpet.h> 21 11 #include <asm/time.h> 22 12 23 - #define HPET_MASK CLOCKSOURCE_MASK(32) 13 + #undef pr_fmt 14 + #define pr_fmt(fmt) "hpet: " fmt 24 15 25 - #define HPET_DEV_USED_BIT 2 26 - #define HPET_DEV_USED (1 << HPET_DEV_USED_BIT) 27 - #define HPET_DEV_VALID 0x8 28 - #define HPET_DEV_FSB_CAP 0x1000 29 - #define HPET_DEV_PERI_CAP 0x2000 16 + enum hpet_mode { 17 + HPET_MODE_UNUSED, 18 + HPET_MODE_LEGACY, 19 + HPET_MODE_CLOCKEVT, 20 + HPET_MODE_DEVICE, 21 + }; 22 + 23 + struct hpet_channel { 24 + struct clock_event_device evt; 25 + unsigned int num; 26 + unsigned int cpu; 27 + unsigned int irq; 28 + unsigned int in_use; 29 + enum hpet_mode mode; 30 + unsigned int boot_cfg; 31 + char name[10]; 32 + }; 33 + 34 + struct hpet_base { 35 + unsigned int nr_channels; 36 + unsigned int nr_clockevents; 37 + unsigned int boot_cfg; 38 + struct hpet_channel *channels; 39 + }; 40 + 41 + #define HPET_MASK CLOCKSOURCE_MASK(32) 30 42 31 43 #define HPET_MIN_CYCLES 128 32 44 #define HPET_MIN_PROG_DELTA (HPET_MIN_CYCLES + (HPET_MIN_CYCLES >> 1)) ··· 51 39 bool hpet_msi_disable; 52 40 53 41 #ifdef CONFIG_PCI_MSI 54 - static unsigned int hpet_num_timers; 42 + static DEFINE_PER_CPU(struct hpet_channel *, cpu_hpet_channel); 43 + static struct irq_domain *hpet_domain; 55 44 #endif 45 + 56 46 static void __iomem *hpet_virt_address; 57 47 58 - struct hpet_dev { 59 - struct clock_event_device evt; 60 - unsigned int num; 61 - int cpu; 62 - unsigned int irq; 63 - unsigned int flags; 64 - char name[10]; 65 - }; 48 + static struct hpet_base hpet_base; 66 49 67 - static inline struct hpet_dev *EVT_TO_HPET_DEV(struct clock_event_device *evtdev) 50 + static bool hpet_legacy_int_enabled; 51 + static unsigned long hpet_freq; 52 + 53 + bool boot_hpet_disable; 54 + bool hpet_force_user; 55 + static bool hpet_verbose; 56 + 57 + static inline 58 + struct hpet_channel *clockevent_to_channel(struct clock_event_device *evt) 68 59 { 69 - return container_of(evtdev, struct hpet_dev, evt); 60 + return container_of(evt, struct hpet_channel, evt); 70 61 } 71 62 72 63 inline unsigned int hpet_readl(unsigned int a) ··· 81 66 { 82 67 writel(d, hpet_virt_address + a); 83 68 } 84 - 85 - #ifdef CONFIG_X86_64 86 - #include <asm/pgtable.h> 87 - #endif 88 69 89 70 static inline void hpet_set_mapping(void) 90 71 { ··· 96 85 /* 97 86 * HPET command line enable / disable 98 87 */ 99 - bool boot_hpet_disable; 100 - bool hpet_force_user; 101 - static bool hpet_verbose; 102 - 103 88 static int __init hpet_setup(char *str) 104 89 { 105 90 while (str) { ··· 127 120 return !boot_hpet_disable && hpet_address; 128 121 } 129 122 130 - /* 131 - * HPET timer interrupt enable / disable 132 - */ 133 - static bool hpet_legacy_int_enabled; 134 - 135 123 /** 136 - * is_hpet_enabled - check whether the hpet timer interrupt is enabled 124 + * is_hpet_enabled - Check whether the legacy HPET timer interrupt is enabled 137 125 */ 138 126 int is_hpet_enabled(void) 139 127 { ··· 138 136 139 137 static void _hpet_print_config(const char *function, int line) 140 138 { 141 - u32 i, timers, l, h; 142 - printk(KERN_INFO "hpet: %s(%d):\n", function, line); 143 - l = hpet_readl(HPET_ID); 144 - h = hpet_readl(HPET_PERIOD); 145 - timers = ((l & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1; 146 - printk(KERN_INFO "hpet: ID: 0x%x, PERIOD: 0x%x\n", l, h); 147 - l = hpet_readl(HPET_CFG); 148 - h = hpet_readl(HPET_STATUS); 149 - printk(KERN_INFO "hpet: CFG: 0x%x, STATUS: 0x%x\n", l, h); 139 + u32 i, id, period, cfg, status, channels, l, h; 140 + 141 + pr_info("%s(%d):\n", function, line); 142 + 143 + id = hpet_readl(HPET_ID); 144 + period = hpet_readl(HPET_PERIOD); 145 + pr_info("ID: 0x%x, PERIOD: 0x%x\n", id, period); 146 + 147 + cfg = hpet_readl(HPET_CFG); 148 + status = hpet_readl(HPET_STATUS); 149 + pr_info("CFG: 0x%x, STATUS: 0x%x\n", cfg, status); 150 + 150 151 l = hpet_readl(HPET_COUNTER); 151 152 h = hpet_readl(HPET_COUNTER+4); 152 - printk(KERN_INFO "hpet: COUNTER_l: 0x%x, COUNTER_h: 0x%x\n", l, h); 153 + pr_info("COUNTER_l: 0x%x, COUNTER_h: 0x%x\n", l, h); 153 154 154 - for (i = 0; i < timers; i++) { 155 + channels = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1; 156 + 157 + for (i = 0; i < channels; i++) { 155 158 l = hpet_readl(HPET_Tn_CFG(i)); 156 159 h = hpet_readl(HPET_Tn_CFG(i)+4); 157 - printk(KERN_INFO "hpet: T%d: CFG_l: 0x%x, CFG_h: 0x%x\n", 158 - i, l, h); 160 + pr_info("T%d: CFG_l: 0x%x, CFG_h: 0x%x\n", i, l, h); 161 + 159 162 l = hpet_readl(HPET_Tn_CMP(i)); 160 163 h = hpet_readl(HPET_Tn_CMP(i)+4); 161 - printk(KERN_INFO "hpet: T%d: CMP_l: 0x%x, CMP_h: 0x%x\n", 162 - i, l, h); 164 + pr_info("T%d: CMP_l: 0x%x, CMP_h: 0x%x\n", i, l, h); 165 + 163 166 l = hpet_readl(HPET_Tn_ROUTE(i)); 164 167 h = hpet_readl(HPET_Tn_ROUTE(i)+4); 165 - printk(KERN_INFO "hpet: T%d ROUTE_l: 0x%x, ROUTE_h: 0x%x\n", 166 - i, l, h); 168 + pr_info("T%d ROUTE_l: 0x%x, ROUTE_h: 0x%x\n", i, l, h); 167 169 } 168 170 } 169 171 ··· 178 172 } while (0) 179 173 180 174 /* 181 - * When the hpet driver (/dev/hpet) is enabled, we need to reserve 175 + * When the HPET driver (/dev/hpet) is enabled, we need to reserve 182 176 * timer 0 and timer 1 in case of RTC emulation. 183 177 */ 184 178 #ifdef CONFIG_HPET 185 179 186 - static void hpet_reserve_msi_timers(struct hpet_data *hd); 187 - 188 - static void hpet_reserve_platform_timers(unsigned int id) 180 + static void __init hpet_reserve_platform_timers(void) 189 181 { 190 - struct hpet __iomem *hpet = hpet_virt_address; 191 - struct hpet_timer __iomem *timer = &hpet->hpet_timers[2]; 192 - unsigned int nrtimers, i; 193 182 struct hpet_data hd; 194 - 195 - nrtimers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1; 183 + unsigned int i; 196 184 197 185 memset(&hd, 0, sizeof(hd)); 198 186 hd.hd_phys_address = hpet_address; 199 - hd.hd_address = hpet; 200 - hd.hd_nirqs = nrtimers; 201 - hpet_reserve_timer(&hd, 0); 202 - 203 - #ifdef CONFIG_HPET_EMULATE_RTC 204 - hpet_reserve_timer(&hd, 1); 205 - #endif 187 + hd.hd_address = hpet_virt_address; 188 + hd.hd_nirqs = hpet_base.nr_channels; 206 189 207 190 /* 208 191 * NOTE that hd_irq[] reflects IOAPIC input pins (LEGACY_8254 ··· 201 206 hd.hd_irq[0] = HPET_LEGACY_8254; 202 207 hd.hd_irq[1] = HPET_LEGACY_RTC; 203 208 204 - for (i = 2; i < nrtimers; timer++, i++) { 205 - hd.hd_irq[i] = (readl(&timer->hpet_config) & 206 - Tn_INT_ROUTE_CNF_MASK) >> Tn_INT_ROUTE_CNF_SHIFT; 209 + for (i = 0; i < hpet_base.nr_channels; i++) { 210 + struct hpet_channel *hc = hpet_base.channels + i; 211 + 212 + if (i >= 2) 213 + hd.hd_irq[i] = hc->irq; 214 + 215 + switch (hc->mode) { 216 + case HPET_MODE_UNUSED: 217 + case HPET_MODE_DEVICE: 218 + hc->mode = HPET_MODE_DEVICE; 219 + break; 220 + case HPET_MODE_CLOCKEVT: 221 + case HPET_MODE_LEGACY: 222 + hpet_reserve_timer(&hd, hc->num); 223 + break; 224 + } 207 225 } 208 226 209 - hpet_reserve_msi_timers(&hd); 210 - 211 227 hpet_alloc(&hd); 212 - 213 228 } 229 + 230 + static void __init hpet_select_device_channel(void) 231 + { 232 + int i; 233 + 234 + for (i = 0; i < hpet_base.nr_channels; i++) { 235 + struct hpet_channel *hc = hpet_base.channels + i; 236 + 237 + /* Associate the first unused channel to /dev/hpet */ 238 + if (hc->mode == HPET_MODE_UNUSED) { 239 + hc->mode = HPET_MODE_DEVICE; 240 + return; 241 + } 242 + } 243 + } 244 + 214 245 #else 215 - static void hpet_reserve_platform_timers(unsigned int id) { } 246 + static inline void hpet_reserve_platform_timers(void) { } 247 + static inline void hpet_select_device_channel(void) {} 216 248 #endif 217 249 218 - /* 219 - * Common hpet info 220 - */ 221 - static unsigned long hpet_freq; 222 - 223 - static struct clock_event_device hpet_clockevent; 224 - 250 + /* Common HPET functions */ 225 251 static void hpet_stop_counter(void) 226 252 { 227 253 u32 cfg = hpet_readl(HPET_CFG); 254 + 228 255 cfg &= ~HPET_CFG_ENABLE; 229 256 hpet_writel(cfg, HPET_CFG); 230 257 } ··· 260 243 static void hpet_start_counter(void) 261 244 { 262 245 unsigned int cfg = hpet_readl(HPET_CFG); 246 + 263 247 cfg |= HPET_CFG_ENABLE; 264 248 hpet_writel(cfg, HPET_CFG); 265 249 } ··· 292 274 hpet_legacy_int_enabled = true; 293 275 } 294 276 295 - static void hpet_legacy_clockevent_register(void) 277 + static int hpet_clkevt_set_state_periodic(struct clock_event_device *evt) 296 278 { 297 - /* Start HPET legacy interrupts */ 298 - hpet_enable_legacy_int(); 299 - 300 - /* 301 - * Start hpet with the boot cpu mask and make it 302 - * global after the IO_APIC has been initialized. 303 - */ 304 - hpet_clockevent.cpumask = cpumask_of(boot_cpu_data.cpu_index); 305 - clockevents_config_and_register(&hpet_clockevent, hpet_freq, 306 - HPET_MIN_PROG_DELTA, 0x7FFFFFFF); 307 - global_clock_event = &hpet_clockevent; 308 - printk(KERN_DEBUG "hpet clockevent registered\n"); 309 - } 310 - 311 - static int hpet_set_periodic(struct clock_event_device *evt, int timer) 312 - { 279 + unsigned int channel = clockevent_to_channel(evt)->num; 313 280 unsigned int cfg, cmp, now; 314 281 uint64_t delta; 315 282 ··· 303 300 delta >>= evt->shift; 304 301 now = hpet_readl(HPET_COUNTER); 305 302 cmp = now + (unsigned int)delta; 306 - cfg = hpet_readl(HPET_Tn_CFG(timer)); 303 + cfg = hpet_readl(HPET_Tn_CFG(channel)); 307 304 cfg |= HPET_TN_ENABLE | HPET_TN_PERIODIC | HPET_TN_SETVAL | 308 305 HPET_TN_32BIT; 309 - hpet_writel(cfg, HPET_Tn_CFG(timer)); 310 - hpet_writel(cmp, HPET_Tn_CMP(timer)); 306 + hpet_writel(cfg, HPET_Tn_CFG(channel)); 307 + hpet_writel(cmp, HPET_Tn_CMP(channel)); 311 308 udelay(1); 312 309 /* 313 310 * HPET on AMD 81xx needs a second write (with HPET_TN_SETVAL ··· 316 313 * (See AMD-8111 HyperTransport I/O Hub Data Sheet, 317 314 * Publication # 24674) 318 315 */ 319 - hpet_writel((unsigned int)delta, HPET_Tn_CMP(timer)); 316 + hpet_writel((unsigned int)delta, HPET_Tn_CMP(channel)); 320 317 hpet_start_counter(); 321 318 hpet_print_config(); 322 319 323 320 return 0; 324 321 } 325 322 326 - static int hpet_set_oneshot(struct clock_event_device *evt, int timer) 323 + static int hpet_clkevt_set_state_oneshot(struct clock_event_device *evt) 327 324 { 325 + unsigned int channel = clockevent_to_channel(evt)->num; 328 326 unsigned int cfg; 329 327 330 - cfg = hpet_readl(HPET_Tn_CFG(timer)); 328 + cfg = hpet_readl(HPET_Tn_CFG(channel)); 331 329 cfg &= ~HPET_TN_PERIODIC; 332 330 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT; 333 - hpet_writel(cfg, HPET_Tn_CFG(timer)); 331 + hpet_writel(cfg, HPET_Tn_CFG(channel)); 334 332 335 333 return 0; 336 334 } 337 335 338 - static int hpet_shutdown(struct clock_event_device *evt, int timer) 336 + static int hpet_clkevt_set_state_shutdown(struct clock_event_device *evt) 339 337 { 338 + unsigned int channel = clockevent_to_channel(evt)->num; 340 339 unsigned int cfg; 341 340 342 - cfg = hpet_readl(HPET_Tn_CFG(timer)); 341 + cfg = hpet_readl(HPET_Tn_CFG(channel)); 343 342 cfg &= ~HPET_TN_ENABLE; 344 - hpet_writel(cfg, HPET_Tn_CFG(timer)); 343 + hpet_writel(cfg, HPET_Tn_CFG(channel)); 345 344 346 345 return 0; 347 346 } 348 347 349 - static int hpet_resume(struct clock_event_device *evt) 348 + static int hpet_clkevt_legacy_resume(struct clock_event_device *evt) 350 349 { 351 350 hpet_enable_legacy_int(); 352 351 hpet_print_config(); 353 352 return 0; 354 353 } 355 354 356 - static int hpet_next_event(unsigned long delta, 357 - struct clock_event_device *evt, int timer) 355 + static int 356 + hpet_clkevt_set_next_event(unsigned long delta, struct clock_event_device *evt) 358 357 { 358 + unsigned int channel = clockevent_to_channel(evt)->num; 359 359 u32 cnt; 360 360 s32 res; 361 361 362 362 cnt = hpet_readl(HPET_COUNTER); 363 363 cnt += (u32) delta; 364 - hpet_writel(cnt, HPET_Tn_CMP(timer)); 364 + hpet_writel(cnt, HPET_Tn_CMP(channel)); 365 365 366 366 /* 367 367 * HPETs are a complete disaster. The compare register is ··· 393 387 return res < HPET_MIN_CYCLES ? -ETIME : 0; 394 388 } 395 389 396 - static int hpet_legacy_shutdown(struct clock_event_device *evt) 390 + static void hpet_init_clockevent(struct hpet_channel *hc, unsigned int rating) 397 391 { 398 - return hpet_shutdown(evt, 0); 392 + struct clock_event_device *evt = &hc->evt; 393 + 394 + evt->rating = rating; 395 + evt->irq = hc->irq; 396 + evt->name = hc->name; 397 + evt->cpumask = cpumask_of(hc->cpu); 398 + evt->set_state_oneshot = hpet_clkevt_set_state_oneshot; 399 + evt->set_next_event = hpet_clkevt_set_next_event; 400 + evt->set_state_shutdown = hpet_clkevt_set_state_shutdown; 401 + 402 + evt->features = CLOCK_EVT_FEAT_ONESHOT; 403 + if (hc->boot_cfg & HPET_TN_PERIODIC) { 404 + evt->features |= CLOCK_EVT_FEAT_PERIODIC; 405 + evt->set_state_periodic = hpet_clkevt_set_state_periodic; 406 + } 399 407 } 400 408 401 - static int hpet_legacy_set_oneshot(struct clock_event_device *evt) 409 + static void __init hpet_legacy_clockevent_register(struct hpet_channel *hc) 402 410 { 403 - return hpet_set_oneshot(evt, 0); 404 - } 411 + /* 412 + * Start HPET with the boot CPU's cpumask and make it global after 413 + * the IO_APIC has been initialized. 414 + */ 415 + hc->cpu = boot_cpu_data.cpu_index; 416 + strncpy(hc->name, "hpet", sizeof(hc->name)); 417 + hpet_init_clockevent(hc, 50); 405 418 406 - static int hpet_legacy_set_periodic(struct clock_event_device *evt) 407 - { 408 - return hpet_set_periodic(evt, 0); 409 - } 419 + hc->evt.tick_resume = hpet_clkevt_legacy_resume; 410 420 411 - static int hpet_legacy_resume(struct clock_event_device *evt) 412 - { 413 - return hpet_resume(evt); 414 - } 421 + /* 422 + * Legacy horrors and sins from the past. HPET used periodic mode 423 + * unconditionally forever on the legacy channel 0. Removing the 424 + * below hack and using the conditional in hpet_init_clockevent() 425 + * makes at least Qemu and one hardware machine fail to boot. 426 + * There are two issues which cause the boot failure: 427 + * 428 + * #1 After the timer delivery test in IOAPIC and the IOAPIC setup 429 + * the next interrupt is not delivered despite the HPET channel 430 + * being programmed correctly. Reprogramming the HPET after 431 + * switching to IOAPIC makes it work again. After fixing this, 432 + * the next issue surfaces: 433 + * 434 + * #2 Due to the unconditional periodic mode availability the Local 435 + * APIC timer calibration can hijack the global clockevents 436 + * event handler without causing damage. Using oneshot at this 437 + * stage makes if hang because the HPET does not get 438 + * reprogrammed due to the handler hijacking. Duh, stupid me! 439 + * 440 + * Both issues require major surgery and especially the kick HPET 441 + * again after enabling IOAPIC results in really nasty hackery. 442 + * This 'assume periodic works' magic has survived since HPET 443 + * support got added, so it's questionable whether this should be 444 + * fixed. Both Qemu and the failing hardware machine support 445 + * periodic mode despite the fact that both don't advertise it in 446 + * the configuration register and both need that extra kick after 447 + * switching to IOAPIC. Seems to be a feature... 448 + */ 449 + hc->evt.features |= CLOCK_EVT_FEAT_PERIODIC; 450 + hc->evt.set_state_periodic = hpet_clkevt_set_state_periodic; 415 451 416 - static int hpet_legacy_next_event(unsigned long delta, 417 - struct clock_event_device *evt) 418 - { 419 - return hpet_next_event(delta, evt, 0); 420 - } 452 + /* Start HPET legacy interrupts */ 453 + hpet_enable_legacy_int(); 421 454 422 - /* 423 - * The hpet clock event device 424 - */ 425 - static struct clock_event_device hpet_clockevent = { 426 - .name = "hpet", 427 - .features = CLOCK_EVT_FEAT_PERIODIC | 428 - CLOCK_EVT_FEAT_ONESHOT, 429 - .set_state_periodic = hpet_legacy_set_periodic, 430 - .set_state_oneshot = hpet_legacy_set_oneshot, 431 - .set_state_shutdown = hpet_legacy_shutdown, 432 - .tick_resume = hpet_legacy_resume, 433 - .set_next_event = hpet_legacy_next_event, 434 - .irq = 0, 435 - .rating = 50, 436 - }; 455 + clockevents_config_and_register(&hc->evt, hpet_freq, 456 + HPET_MIN_PROG_DELTA, 0x7FFFFFFF); 457 + global_clock_event = &hc->evt; 458 + pr_debug("Clockevent registered\n"); 459 + } 437 460 438 461 /* 439 462 * HPET MSI Support 440 463 */ 441 464 #ifdef CONFIG_PCI_MSI 442 465 443 - static DEFINE_PER_CPU(struct hpet_dev *, cpu_hpet_dev); 444 - static struct hpet_dev *hpet_devs; 445 - static struct irq_domain *hpet_domain; 446 - 447 466 void hpet_msi_unmask(struct irq_data *data) 448 467 { 449 - struct hpet_dev *hdev = irq_data_get_irq_handler_data(data); 468 + struct hpet_channel *hc = irq_data_get_irq_handler_data(data); 450 469 unsigned int cfg; 451 470 452 - /* unmask it */ 453 - cfg = hpet_readl(HPET_Tn_CFG(hdev->num)); 471 + cfg = hpet_readl(HPET_Tn_CFG(hc->num)); 454 472 cfg |= HPET_TN_ENABLE | HPET_TN_FSB; 455 - hpet_writel(cfg, HPET_Tn_CFG(hdev->num)); 473 + hpet_writel(cfg, HPET_Tn_CFG(hc->num)); 456 474 } 457 475 458 476 void hpet_msi_mask(struct irq_data *data) 459 477 { 460 - struct hpet_dev *hdev = irq_data_get_irq_handler_data(data); 478 + struct hpet_channel *hc = irq_data_get_irq_handler_data(data); 461 479 unsigned int cfg; 462 480 463 - /* mask it */ 464 - cfg = hpet_readl(HPET_Tn_CFG(hdev->num)); 481 + cfg = hpet_readl(HPET_Tn_CFG(hc->num)); 465 482 cfg &= ~(HPET_TN_ENABLE | HPET_TN_FSB); 466 - hpet_writel(cfg, HPET_Tn_CFG(hdev->num)); 483 + hpet_writel(cfg, HPET_Tn_CFG(hc->num)); 467 484 } 468 485 469 - void hpet_msi_write(struct hpet_dev *hdev, struct msi_msg *msg) 486 + void hpet_msi_write(struct hpet_channel *hc, struct msi_msg *msg) 470 487 { 471 - hpet_writel(msg->data, HPET_Tn_ROUTE(hdev->num)); 472 - hpet_writel(msg->address_lo, HPET_Tn_ROUTE(hdev->num) + 4); 488 + hpet_writel(msg->data, HPET_Tn_ROUTE(hc->num)); 489 + hpet_writel(msg->address_lo, HPET_Tn_ROUTE(hc->num) + 4); 473 490 } 474 491 475 - void hpet_msi_read(struct hpet_dev *hdev, struct msi_msg *msg) 492 + static int hpet_clkevt_msi_resume(struct clock_event_device *evt) 476 493 { 477 - msg->data = hpet_readl(HPET_Tn_ROUTE(hdev->num)); 478 - msg->address_lo = hpet_readl(HPET_Tn_ROUTE(hdev->num) + 4); 479 - msg->address_hi = 0; 480 - } 481 - 482 - static int hpet_msi_shutdown(struct clock_event_device *evt) 483 - { 484 - struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt); 485 - 486 - return hpet_shutdown(evt, hdev->num); 487 - } 488 - 489 - static int hpet_msi_set_oneshot(struct clock_event_device *evt) 490 - { 491 - struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt); 492 - 493 - return hpet_set_oneshot(evt, hdev->num); 494 - } 495 - 496 - static int hpet_msi_set_periodic(struct clock_event_device *evt) 497 - { 498 - struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt); 499 - 500 - return hpet_set_periodic(evt, hdev->num); 501 - } 502 - 503 - static int hpet_msi_resume(struct clock_event_device *evt) 504 - { 505 - struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt); 506 - struct irq_data *data = irq_get_irq_data(hdev->irq); 494 + struct hpet_channel *hc = clockevent_to_channel(evt); 495 + struct irq_data *data = irq_get_irq_data(hc->irq); 507 496 struct msi_msg msg; 508 497 509 498 /* Restore the MSI msg and unmask the interrupt */ 510 499 irq_chip_compose_msi_msg(data, &msg); 511 - hpet_msi_write(hdev, &msg); 500 + hpet_msi_write(hc, &msg); 512 501 hpet_msi_unmask(data); 513 502 return 0; 514 503 } 515 504 516 - static int hpet_msi_next_event(unsigned long delta, 517 - struct clock_event_device *evt) 505 + static irqreturn_t hpet_msi_interrupt_handler(int irq, void *data) 518 506 { 519 - struct hpet_dev *hdev = EVT_TO_HPET_DEV(evt); 520 - return hpet_next_event(delta, evt, hdev->num); 521 - } 507 + struct hpet_channel *hc = data; 508 + struct clock_event_device *evt = &hc->evt; 522 509 523 - static irqreturn_t hpet_interrupt_handler(int irq, void *data) 524 - { 525 - struct hpet_dev *dev = (struct hpet_dev *)data; 526 - struct clock_event_device *hevt = &dev->evt; 527 - 528 - if (!hevt->event_handler) { 529 - printk(KERN_INFO "Spurious HPET timer interrupt on HPET timer %d\n", 530 - dev->num); 510 + if (!evt->event_handler) { 511 + pr_info("Spurious interrupt HPET channel %d\n", hc->num); 531 512 return IRQ_HANDLED; 532 513 } 533 514 534 - hevt->event_handler(hevt); 515 + evt->event_handler(evt); 535 516 return IRQ_HANDLED; 536 517 } 537 518 538 - static int hpet_setup_irq(struct hpet_dev *dev) 519 + static int hpet_setup_msi_irq(struct hpet_channel *hc) 539 520 { 540 - 541 - if (request_irq(dev->irq, hpet_interrupt_handler, 521 + if (request_irq(hc->irq, hpet_msi_interrupt_handler, 542 522 IRQF_TIMER | IRQF_NOBALANCING, 543 - dev->name, dev)) 523 + hc->name, hc)) 544 524 return -1; 545 525 546 - disable_irq(dev->irq); 547 - irq_set_affinity(dev->irq, cpumask_of(dev->cpu)); 548 - enable_irq(dev->irq); 526 + disable_irq(hc->irq); 527 + irq_set_affinity(hc->irq, cpumask_of(hc->cpu)); 528 + enable_irq(hc->irq); 549 529 550 - printk(KERN_DEBUG "hpet: %s irq %d for MSI\n", 551 - dev->name, dev->irq); 530 + pr_debug("%s irq %u for MSI\n", hc->name, hc->irq); 552 531 553 532 return 0; 554 533 } 555 534 556 - /* This should be called in specific @cpu */ 557 - static void init_one_hpet_msi_clockevent(struct hpet_dev *hdev, int cpu) 535 + /* Invoked from the hotplug callback on @cpu */ 536 + static void init_one_hpet_msi_clockevent(struct hpet_channel *hc, int cpu) 558 537 { 559 - struct clock_event_device *evt = &hdev->evt; 538 + struct clock_event_device *evt = &hc->evt; 560 539 561 - WARN_ON(cpu != smp_processor_id()); 562 - if (!(hdev->flags & HPET_DEV_VALID)) 563 - return; 540 + hc->cpu = cpu; 541 + per_cpu(cpu_hpet_channel, cpu) = hc; 542 + hpet_setup_msi_irq(hc); 564 543 565 - hdev->cpu = cpu; 566 - per_cpu(cpu_hpet_dev, cpu) = hdev; 567 - evt->name = hdev->name; 568 - hpet_setup_irq(hdev); 569 - evt->irq = hdev->irq; 570 - 571 - evt->rating = 110; 572 - evt->features = CLOCK_EVT_FEAT_ONESHOT; 573 - if (hdev->flags & HPET_DEV_PERI_CAP) { 574 - evt->features |= CLOCK_EVT_FEAT_PERIODIC; 575 - evt->set_state_periodic = hpet_msi_set_periodic; 576 - } 577 - 578 - evt->set_state_shutdown = hpet_msi_shutdown; 579 - evt->set_state_oneshot = hpet_msi_set_oneshot; 580 - evt->tick_resume = hpet_msi_resume; 581 - evt->set_next_event = hpet_msi_next_event; 582 - evt->cpumask = cpumask_of(hdev->cpu); 544 + hpet_init_clockevent(hc, 110); 545 + evt->tick_resume = hpet_clkevt_msi_resume; 583 546 584 547 clockevents_config_and_register(evt, hpet_freq, HPET_MIN_PROG_DELTA, 585 548 0x7FFFFFFF); 586 549 } 587 550 588 - #ifdef CONFIG_HPET 589 - /* Reserve at least one timer for userspace (/dev/hpet) */ 590 - #define RESERVE_TIMERS 1 591 - #else 592 - #define RESERVE_TIMERS 0 593 - #endif 594 - 595 - static void hpet_msi_capability_lookup(unsigned int start_timer) 551 + static struct hpet_channel *hpet_get_unused_clockevent(void) 596 552 { 597 - unsigned int id; 598 - unsigned int num_timers; 599 - unsigned int num_timers_used = 0; 600 - int i, irq; 553 + int i; 601 554 602 - if (hpet_msi_disable) 555 + for (i = 0; i < hpet_base.nr_channels; i++) { 556 + struct hpet_channel *hc = hpet_base.channels + i; 557 + 558 + if (hc->mode != HPET_MODE_CLOCKEVT || hc->in_use) 559 + continue; 560 + hc->in_use = 1; 561 + return hc; 562 + } 563 + return NULL; 564 + } 565 + 566 + static int hpet_cpuhp_online(unsigned int cpu) 567 + { 568 + struct hpet_channel *hc = hpet_get_unused_clockevent(); 569 + 570 + if (hc) 571 + init_one_hpet_msi_clockevent(hc, cpu); 572 + return 0; 573 + } 574 + 575 + static int hpet_cpuhp_dead(unsigned int cpu) 576 + { 577 + struct hpet_channel *hc = per_cpu(cpu_hpet_channel, cpu); 578 + 579 + if (!hc) 580 + return 0; 581 + free_irq(hc->irq, hc); 582 + hc->in_use = 0; 583 + per_cpu(cpu_hpet_channel, cpu) = NULL; 584 + return 0; 585 + } 586 + 587 + static void __init hpet_select_clockevents(void) 588 + { 589 + unsigned int i; 590 + 591 + hpet_base.nr_clockevents = 0; 592 + 593 + /* No point if MSI is disabled or CPU has an Always Runing APIC Timer */ 594 + if (hpet_msi_disable || boot_cpu_has(X86_FEATURE_ARAT)) 603 595 return; 604 596 605 - if (boot_cpu_has(X86_FEATURE_ARAT)) 606 - return; 607 - id = hpet_readl(HPET_ID); 608 - 609 - num_timers = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT); 610 - num_timers++; /* Value read out starts from 0 */ 611 597 hpet_print_config(); 612 598 613 599 hpet_domain = hpet_create_irq_domain(hpet_blockid); 614 600 if (!hpet_domain) 615 601 return; 616 602 617 - hpet_devs = kcalloc(num_timers, sizeof(struct hpet_dev), GFP_KERNEL); 618 - if (!hpet_devs) 619 - return; 603 + for (i = 0; i < hpet_base.nr_channels; i++) { 604 + struct hpet_channel *hc = hpet_base.channels + i; 605 + int irq; 620 606 621 - hpet_num_timers = num_timers; 622 - 623 - for (i = start_timer; i < num_timers - RESERVE_TIMERS; i++) { 624 - struct hpet_dev *hdev = &hpet_devs[num_timers_used]; 625 - unsigned int cfg = hpet_readl(HPET_Tn_CFG(i)); 626 - 627 - /* Only consider HPET timer with MSI support */ 628 - if (!(cfg & HPET_TN_FSB_CAP)) 607 + if (hc->mode != HPET_MODE_UNUSED) 629 608 continue; 630 609 631 - hdev->flags = 0; 632 - if (cfg & HPET_TN_PERIODIC_CAP) 633 - hdev->flags |= HPET_DEV_PERI_CAP; 634 - sprintf(hdev->name, "hpet%d", i); 635 - hdev->num = i; 610 + /* Only consider HPET channel with MSI support */ 611 + if (!(hc->boot_cfg & HPET_TN_FSB_CAP)) 612 + continue; 636 613 637 - irq = hpet_assign_irq(hpet_domain, hdev, hdev->num); 614 + sprintf(hc->name, "hpet%d", i); 615 + 616 + irq = hpet_assign_irq(hpet_domain, hc, hc->num); 638 617 if (irq <= 0) 639 618 continue; 640 619 641 - hdev->irq = irq; 642 - hdev->flags |= HPET_DEV_FSB_CAP; 643 - hdev->flags |= HPET_DEV_VALID; 644 - num_timers_used++; 645 - if (num_timers_used == num_possible_cpus()) 620 + hc->irq = irq; 621 + hc->mode = HPET_MODE_CLOCKEVT; 622 + 623 + if (++hpet_base.nr_clockevents == num_possible_cpus()) 646 624 break; 647 625 } 648 626 649 - printk(KERN_INFO "HPET: %d timers in total, %d timers will be used for per-cpu timer\n", 650 - num_timers, num_timers_used); 627 + pr_info("%d channels of %d reserved for per-cpu timers\n", 628 + hpet_base.nr_channels, hpet_base.nr_clockevents); 651 629 } 652 630 653 - #ifdef CONFIG_HPET 654 - static void hpet_reserve_msi_timers(struct hpet_data *hd) 655 - { 656 - int i; 657 - 658 - if (!hpet_devs) 659 - return; 660 - 661 - for (i = 0; i < hpet_num_timers; i++) { 662 - struct hpet_dev *hdev = &hpet_devs[i]; 663 - 664 - if (!(hdev->flags & HPET_DEV_VALID)) 665 - continue; 666 - 667 - hd->hd_irq[hdev->num] = hdev->irq; 668 - hpet_reserve_timer(hd, hdev->num); 669 - } 670 - } 671 - #endif 672 - 673 - static struct hpet_dev *hpet_get_unused_timer(void) 674 - { 675 - int i; 676 - 677 - if (!hpet_devs) 678 - return NULL; 679 - 680 - for (i = 0; i < hpet_num_timers; i++) { 681 - struct hpet_dev *hdev = &hpet_devs[i]; 682 - 683 - if (!(hdev->flags & HPET_DEV_VALID)) 684 - continue; 685 - if (test_and_set_bit(HPET_DEV_USED_BIT, 686 - (unsigned long *)&hdev->flags)) 687 - continue; 688 - return hdev; 689 - } 690 - return NULL; 691 - } 692 - 693 - struct hpet_work_struct { 694 - struct delayed_work work; 695 - struct completion complete; 696 - }; 697 - 698 - static void hpet_work(struct work_struct *w) 699 - { 700 - struct hpet_dev *hdev; 701 - int cpu = smp_processor_id(); 702 - struct hpet_work_struct *hpet_work; 703 - 704 - hpet_work = container_of(w, struct hpet_work_struct, work.work); 705 - 706 - hdev = hpet_get_unused_timer(); 707 - if (hdev) 708 - init_one_hpet_msi_clockevent(hdev, cpu); 709 - 710 - complete(&hpet_work->complete); 711 - } 712 - 713 - static int hpet_cpuhp_online(unsigned int cpu) 714 - { 715 - struct hpet_work_struct work; 716 - 717 - INIT_DELAYED_WORK_ONSTACK(&work.work, hpet_work); 718 - init_completion(&work.complete); 719 - /* FIXME: add schedule_work_on() */ 720 - schedule_delayed_work_on(cpu, &work.work, 0); 721 - wait_for_completion(&work.complete); 722 - destroy_delayed_work_on_stack(&work.work); 723 - return 0; 724 - } 725 - 726 - static int hpet_cpuhp_dead(unsigned int cpu) 727 - { 728 - struct hpet_dev *hdev = per_cpu(cpu_hpet_dev, cpu); 729 - 730 - if (!hdev) 731 - return 0; 732 - free_irq(hdev->irq, hdev); 733 - hdev->flags &= ~HPET_DEV_USED; 734 - per_cpu(cpu_hpet_dev, cpu) = NULL; 735 - return 0; 736 - } 737 631 #else 738 632 739 - static void hpet_msi_capability_lookup(unsigned int start_timer) 740 - { 741 - return; 742 - } 743 - 744 - #ifdef CONFIG_HPET 745 - static void hpet_reserve_msi_timers(struct hpet_data *hd) 746 - { 747 - return; 748 - } 749 - #endif 633 + static inline void hpet_select_clockevents(void) { } 750 634 751 635 #define hpet_cpuhp_online NULL 752 636 #define hpet_cpuhp_dead NULL ··· 650 754 /* 651 755 * Reading the HPET counter is a very slow operation. If a large number of 652 756 * CPUs are trying to access the HPET counter simultaneously, it can cause 653 - * massive delay and slow down system performance dramatically. This may 757 + * massive delays and slow down system performance dramatically. This may 654 758 * happen when HPET is the default clock source instead of TSC. For a 655 759 * really large system with hundreds of CPUs, the slowdown may be so 656 - * severe that it may actually crash the system because of a NMI watchdog 760 + * severe, that it can actually crash the system because of a NMI watchdog 657 761 * soft lockup, for example. 658 762 * 659 763 * If multiple CPUs are trying to access the HPET counter at the same time, ··· 662 766 * 663 767 * This special feature is only enabled on x86-64 systems. It is unlikely 664 768 * that 32-bit x86 systems will have enough CPUs to require this feature 665 - * with its associated locking overhead. And we also need 64-bit atomic 666 - * read. 769 + * with its associated locking overhead. We also need 64-bit atomic read. 667 770 * 668 - * The lock and the hpet value are stored together and can be read in a 771 + * The lock and the HPET value are stored together and can be read in a 669 772 * single atomic 64-bit read. It is explicitly assumed that arch_spinlock_t 670 773 * is 32 bits in size. 671 774 */ ··· 753 858 .resume = hpet_resume_counter, 754 859 }; 755 860 756 - static int hpet_clocksource_register(void) 861 + /* 862 + * AMD SB700 based systems with spread spectrum enabled use a SMM based 863 + * HPET emulation to provide proper frequency setting. 864 + * 865 + * On such systems the SMM code is initialized with the first HPET register 866 + * access and takes some time to complete. During this time the config 867 + * register reads 0xffffffff. We check for max 1000 loops whether the 868 + * config register reads a non-0xffffffff value to make sure that the 869 + * HPET is up and running before we proceed any further. 870 + * 871 + * A counting loop is safe, as the HPET access takes thousands of CPU cycles. 872 + * 873 + * On non-SB700 based machines this check is only done once and has no 874 + * side effects. 875 + */ 876 + static bool __init hpet_cfg_working(void) 757 877 { 758 - u64 start, now; 759 - u64 t1; 878 + int i; 760 879 761 - /* Start the counter */ 880 + for (i = 0; i < 1000; i++) { 881 + if (hpet_readl(HPET_CFG) != 0xFFFFFFFF) 882 + return true; 883 + } 884 + 885 + pr_warn("Config register invalid. Disabling HPET\n"); 886 + return false; 887 + } 888 + 889 + static bool __init hpet_counting(void) 890 + { 891 + u64 start, now, t1; 892 + 762 893 hpet_restart_counter(); 763 894 764 - /* Verify whether hpet counter works */ 765 895 t1 = hpet_readl(HPET_COUNTER); 766 896 start = rdtsc(); 767 897 ··· 797 877 * 1 GHz == 200us 798 878 */ 799 879 do { 800 - rep_nop(); 880 + if (t1 != hpet_readl(HPET_COUNTER)) 881 + return true; 801 882 now = rdtsc(); 802 883 } while ((now - start) < 200000UL); 803 884 804 - if (t1 == hpet_readl(HPET_COUNTER)) { 805 - printk(KERN_WARNING 806 - "HPET counter not counting. HPET disabled\n"); 807 - return -ENODEV; 808 - } 809 - 810 - clocksource_register_hz(&clocksource_hpet, (u32)hpet_freq); 811 - return 0; 885 + pr_warn("Counter not counting. HPET disabled\n"); 886 + return false; 812 887 } 813 - 814 - static u32 *hpet_boot_cfg; 815 888 816 889 /** 817 890 * hpet_enable - Try to setup the HPET timer. Returns 1 on success. 818 891 */ 819 892 int __init hpet_enable(void) 820 893 { 821 - u32 hpet_period, cfg, id; 894 + u32 hpet_period, cfg, id, irq; 895 + unsigned int i, channels; 896 + struct hpet_channel *hc; 822 897 u64 freq; 823 - unsigned int i, last; 824 898 825 899 if (!is_hpet_capable()) 826 900 return 0; ··· 823 909 if (!hpet_virt_address) 824 910 return 0; 825 911 912 + /* Validate that the config register is working */ 913 + if (!hpet_cfg_working()) 914 + goto out_nohpet; 915 + 916 + /* Validate that the counter is counting */ 917 + if (!hpet_counting()) 918 + goto out_nohpet; 919 + 826 920 /* 827 921 * Read the period and check for a sane value: 828 922 */ 829 923 hpet_period = hpet_readl(HPET_PERIOD); 830 - 831 - /* 832 - * AMD SB700 based systems with spread spectrum enabled use a 833 - * SMM based HPET emulation to provide proper frequency 834 - * setting. The SMM code is initialized with the first HPET 835 - * register access and takes some time to complete. During 836 - * this time the config register reads 0xffffffff. We check 837 - * for max. 1000 loops whether the config register reads a non 838 - * 0xffffffff value to make sure that HPET is up and running 839 - * before we go further. A counting loop is safe, as the HPET 840 - * access takes thousands of CPU cycles. On non SB700 based 841 - * machines this check is only done once and has no side 842 - * effects. 843 - */ 844 - for (i = 0; hpet_readl(HPET_CFG) == 0xFFFFFFFF; i++) { 845 - if (i == 1000) { 846 - printk(KERN_WARNING 847 - "HPET config register value = 0xFFFFFFFF. " 848 - "Disabling HPET\n"); 849 - goto out_nohpet; 850 - } 851 - } 852 - 853 924 if (hpet_period < HPET_MIN_PERIOD || hpet_period > HPET_MAX_PERIOD) 854 925 goto out_nohpet; 855 926 856 - /* 857 - * The period is a femto seconds value. Convert it to a 858 - * frequency. 859 - */ 927 + /* The period is a femtoseconds value. Convert it to a frequency. */ 860 928 freq = FSEC_PER_SEC; 861 929 do_div(freq, hpet_period); 862 930 hpet_freq = freq; ··· 850 954 id = hpet_readl(HPET_ID); 851 955 hpet_print_config(); 852 956 853 - last = (id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT; 957 + /* This is the HPET channel number which is zero based */ 958 + channels = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT) + 1; 854 959 855 - #ifdef CONFIG_HPET_EMULATE_RTC 856 960 /* 857 961 * The legacy routing mode needs at least two channels, tick timer 858 962 * and the rtc emulation channel. 859 963 */ 860 - if (!last) 964 + if (IS_ENABLED(CONFIG_HPET_EMULATE_RTC) && channels < 2) 861 965 goto out_nohpet; 862 - #endif 863 966 967 + hc = kcalloc(channels, sizeof(*hc), GFP_KERNEL); 968 + if (!hc) { 969 + pr_warn("Disabling HPET.\n"); 970 + goto out_nohpet; 971 + } 972 + hpet_base.channels = hc; 973 + hpet_base.nr_channels = channels; 974 + 975 + /* Read, store and sanitize the global configuration */ 864 976 cfg = hpet_readl(HPET_CFG); 865 - hpet_boot_cfg = kmalloc_array(last + 2, sizeof(*hpet_boot_cfg), 866 - GFP_KERNEL); 867 - if (hpet_boot_cfg) 868 - *hpet_boot_cfg = cfg; 869 - else 870 - pr_warn("HPET initial state will not be saved\n"); 977 + hpet_base.boot_cfg = cfg; 871 978 cfg &= ~(HPET_CFG_ENABLE | HPET_CFG_LEGACY); 872 979 hpet_writel(cfg, HPET_CFG); 873 980 if (cfg) 874 - pr_warn("Unrecognized bits %#x set in global cfg\n", cfg); 981 + pr_warn("Global config: Unknown bits %#x\n", cfg); 875 982 876 - for (i = 0; i <= last; ++i) { 983 + /* Read, store and sanitize the per channel configuration */ 984 + for (i = 0; i < channels; i++, hc++) { 985 + hc->num = i; 986 + 877 987 cfg = hpet_readl(HPET_Tn_CFG(i)); 878 - if (hpet_boot_cfg) 879 - hpet_boot_cfg[i + 1] = cfg; 988 + hc->boot_cfg = cfg; 989 + irq = (cfg & Tn_INT_ROUTE_CNF_MASK) >> Tn_INT_ROUTE_CNF_SHIFT; 990 + hc->irq = irq; 991 + 880 992 cfg &= ~(HPET_TN_ENABLE | HPET_TN_LEVEL | HPET_TN_FSB); 881 993 hpet_writel(cfg, HPET_Tn_CFG(i)); 994 + 882 995 cfg &= ~(HPET_TN_PERIODIC | HPET_TN_PERIODIC_CAP 883 996 | HPET_TN_64BIT_CAP | HPET_TN_32BIT | HPET_TN_ROUTE 884 997 | HPET_TN_FSB | HPET_TN_FSB_CAP); 885 998 if (cfg) 886 - pr_warn("Unrecognized bits %#x set in cfg#%u\n", 887 - cfg, i); 999 + pr_warn("Channel #%u config: Unknown bits %#x\n", i, cfg); 888 1000 } 889 1001 hpet_print_config(); 890 1002 891 - if (hpet_clocksource_register()) 892 - goto out_nohpet; 1003 + clocksource_register_hz(&clocksource_hpet, (u32)hpet_freq); 893 1004 894 1005 if (id & HPET_ID_LEGSUP) { 895 - hpet_legacy_clockevent_register(); 1006 + hpet_legacy_clockevent_register(&hpet_base.channels[0]); 1007 + hpet_base.channels[0].mode = HPET_MODE_LEGACY; 1008 + if (IS_ENABLED(CONFIG_HPET_EMULATE_RTC)) 1009 + hpet_base.channels[1].mode = HPET_MODE_LEGACY; 896 1010 return 1; 897 1011 } 898 1012 return 0; 899 1013 900 1014 out_nohpet: 1015 + kfree(hpet_base.channels); 1016 + hpet_base.channels = NULL; 1017 + hpet_base.nr_channels = 0; 901 1018 hpet_clear_mapping(); 902 1019 hpet_address = 0; 903 1020 return 0; 904 1021 } 905 1022 906 1023 /* 907 - * Needs to be late, as the reserve_timer code calls kalloc ! 1024 + * The late initialization runs after the PCI quirks have been invoked 1025 + * which might have detected a system on which the HPET can be enforced. 908 1026 * 909 - * Not a problem on i386 as hpet_enable is called from late_time_init, 910 - * but on x86_64 it is necessary ! 1027 + * Also, the MSI machinery is not working yet when the HPET is initialized 1028 + * early. 1029 + * 1030 + * If the HPET is enabled, then: 1031 + * 1032 + * 1) Reserve one channel for /dev/hpet if CONFIG_HPET=y 1033 + * 2) Reserve up to num_possible_cpus() channels as per CPU clockevents 1034 + * 3) Setup /dev/hpet if CONFIG_HPET=y 1035 + * 4) Register hotplug callbacks when clockevents are available 911 1036 */ 912 1037 static __init int hpet_late_init(void) 913 1038 { 914 1039 int ret; 915 - 916 - if (boot_hpet_disable) 917 - return -ENODEV; 918 1040 919 1041 if (!hpet_address) { 920 1042 if (!force_hpet_address) ··· 945 1031 if (!hpet_virt_address) 946 1032 return -ENODEV; 947 1033 948 - if (hpet_readl(HPET_ID) & HPET_ID_LEGSUP) 949 - hpet_msi_capability_lookup(2); 950 - else 951 - hpet_msi_capability_lookup(0); 952 - 953 - hpet_reserve_platform_timers(hpet_readl(HPET_ID)); 1034 + hpet_select_device_channel(); 1035 + hpet_select_clockevents(); 1036 + hpet_reserve_platform_timers(); 954 1037 hpet_print_config(); 955 1038 956 - if (hpet_msi_disable) 1039 + if (!hpet_base.nr_clockevents) 957 1040 return 0; 958 1041 959 - if (boot_cpu_has(X86_FEATURE_ARAT)) 960 - return 0; 961 - 962 - /* This notifier should be called after workqueue is ready */ 963 1042 ret = cpuhp_setup_state(CPUHP_AP_X86_HPET_ONLINE, "x86/hpet:online", 964 1043 hpet_cpuhp_online, NULL); 965 1044 if (ret) ··· 971 1064 972 1065 void hpet_disable(void) 973 1066 { 974 - if (is_hpet_capable() && hpet_virt_address) { 975 - unsigned int cfg = hpet_readl(HPET_CFG), id, last; 1067 + unsigned int i; 1068 + u32 cfg; 976 1069 977 - if (hpet_boot_cfg) 978 - cfg = *hpet_boot_cfg; 979 - else if (hpet_legacy_int_enabled) { 980 - cfg &= ~HPET_CFG_LEGACY; 981 - hpet_legacy_int_enabled = false; 982 - } 983 - cfg &= ~HPET_CFG_ENABLE; 984 - hpet_writel(cfg, HPET_CFG); 1070 + if (!is_hpet_capable() || !hpet_virt_address) 1071 + return; 985 1072 986 - if (!hpet_boot_cfg) 987 - return; 1073 + /* Restore boot configuration with the enable bit cleared */ 1074 + cfg = hpet_base.boot_cfg; 1075 + cfg &= ~HPET_CFG_ENABLE; 1076 + hpet_writel(cfg, HPET_CFG); 988 1077 989 - id = hpet_readl(HPET_ID); 990 - last = ((id & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT); 1078 + /* Restore the channel boot configuration */ 1079 + for (i = 0; i < hpet_base.nr_channels; i++) 1080 + hpet_writel(hpet_base.channels[i].boot_cfg, HPET_Tn_CFG(i)); 991 1081 992 - for (id = 0; id <= last; ++id) 993 - hpet_writel(hpet_boot_cfg[id + 1], HPET_Tn_CFG(id)); 994 - 995 - if (*hpet_boot_cfg & HPET_CFG_ENABLE) 996 - hpet_writel(*hpet_boot_cfg, HPET_CFG); 997 - } 1082 + /* If the HPET was enabled at boot time, reenable it */ 1083 + if (hpet_base.boot_cfg & HPET_CFG_ENABLE) 1084 + hpet_writel(hpet_base.boot_cfg, HPET_CFG); 998 1085 } 999 1086 1000 1087 #ifdef CONFIG_HPET_EMULATE_RTC 1001 1088 1002 - /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET 1089 + /* 1090 + * HPET in LegacyReplacement mode eats up the RTC interrupt line. When HPET 1003 1091 * is enabled, we support RTC interrupt functionality in software. 1092 + * 1004 1093 * RTC has 3 kinds of interrupts: 1005 - * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock 1006 - * is updated 1007 - * 2) Alarm Interrupt - generate an interrupt at a specific time of day 1008 - * 3) Periodic Interrupt - generate periodic interrupt, with frequencies 1009 - * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2) 1010 - * (1) and (2) above are implemented using polling at a frequency of 1011 - * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt 1012 - * overhead. (DEFAULT_RTC_INT_FREQ) 1013 - * For (3), we use interrupts at 64Hz or user specified periodic 1014 - * frequency, whichever is higher. 1094 + * 1095 + * 1) Update Interrupt - generate an interrupt, every second, when the 1096 + * RTC clock is updated 1097 + * 2) Alarm Interrupt - generate an interrupt at a specific time of day 1098 + * 3) Periodic Interrupt - generate periodic interrupt, with frequencies 1099 + * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all frequencies in powers of 2) 1100 + * 1101 + * (1) and (2) above are implemented using polling at a frequency of 64 Hz: 1102 + * DEFAULT_RTC_INT_FREQ. 1103 + * 1104 + * The exact frequency is a tradeoff between accuracy and interrupt overhead. 1105 + * 1106 + * For (3), we use interrupts at 64 Hz, or the user specified periodic frequency, 1107 + * if it's higher. 1015 1108 */ 1016 1109 #include <linux/mc146818rtc.h> 1017 1110 #include <linux/rtc.h> ··· 1032 1125 static rtc_irq_handler irq_handler; 1033 1126 1034 1127 /* 1035 - * Check that the hpet counter c1 is ahead of the c2 1128 + * Check that the HPET counter c1 is ahead of c2 1036 1129 */ 1037 1130 static inline int hpet_cnt_ahead(u32 c1, u32 c2) 1038 1131 { ··· 1070 1163 EXPORT_SYMBOL_GPL(hpet_unregister_irq_handler); 1071 1164 1072 1165 /* 1073 - * Timer 1 for RTC emulation. We use one shot mode, as periodic mode 1074 - * is not supported by all HPET implementations for timer 1. 1166 + * Channel 1 for RTC emulation. We use one shot mode, as periodic mode 1167 + * is not supported by all HPET implementations for channel 1. 1075 1168 * 1076 1169 * hpet_rtc_timer_init() is called when the rtc is initialized. 1077 1170 */ ··· 1084 1177 return 0; 1085 1178 1086 1179 if (!hpet_default_delta) { 1180 + struct clock_event_device *evt = &hpet_base.channels[0].evt; 1087 1181 uint64_t clc; 1088 1182 1089 - clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC; 1090 - clc >>= hpet_clockevent.shift + DEFAULT_RTC_SHIFT; 1183 + clc = (uint64_t) evt->mult * NSEC_PER_SEC; 1184 + clc >>= evt->shift + DEFAULT_RTC_SHIFT; 1091 1185 hpet_default_delta = clc; 1092 1186 } 1093 1187 ··· 1117 1209 static void hpet_disable_rtc_channel(void) 1118 1210 { 1119 1211 u32 cfg = hpet_readl(HPET_T1_CFG); 1212 + 1120 1213 cfg &= ~HPET_TN_ENABLE; 1121 1214 hpet_writel(cfg, HPET_T1_CFG); 1122 1215 } ··· 1159 1250 } 1160 1251 EXPORT_SYMBOL_GPL(hpet_set_rtc_irq_bit); 1161 1252 1162 - int hpet_set_alarm_time(unsigned char hrs, unsigned char min, 1163 - unsigned char sec) 1253 + int hpet_set_alarm_time(unsigned char hrs, unsigned char min, unsigned char sec) 1164 1254 { 1165 1255 if (!is_hpet_enabled()) 1166 1256 return 0; ··· 1179 1271 if (!is_hpet_enabled()) 1180 1272 return 0; 1181 1273 1182 - if (freq <= DEFAULT_RTC_INT_FREQ) 1274 + if (freq <= DEFAULT_RTC_INT_FREQ) { 1183 1275 hpet_pie_limit = DEFAULT_RTC_INT_FREQ / freq; 1184 - else { 1185 - clc = (uint64_t) hpet_clockevent.mult * NSEC_PER_SEC; 1276 + } else { 1277 + struct clock_event_device *evt = &hpet_base.channels[0].evt; 1278 + 1279 + clc = (uint64_t) evt->mult * NSEC_PER_SEC; 1186 1280 do_div(clc, freq); 1187 - clc >>= hpet_clockevent.shift; 1281 + clc >>= evt->shift; 1188 1282 hpet_pie_delta = clc; 1189 1283 hpet_pie_limit = 0; 1190 1284 } 1285 + 1191 1286 return 1; 1192 1287 } 1193 1288 EXPORT_SYMBOL_GPL(hpet_set_periodic_freq); ··· 1228 1317 if (hpet_rtc_flags & RTC_PIE) 1229 1318 hpet_pie_count += lost_ints; 1230 1319 if (printk_ratelimit()) 1231 - printk(KERN_WARNING "hpet1: lost %d rtc interrupts\n", 1232 - lost_ints); 1320 + pr_warn("Lost %d RTC interrupts\n", lost_ints); 1233 1321 } 1234 1322 } 1235 1323 ··· 1250 1340 hpet_prev_update_sec = curr_time.tm_sec; 1251 1341 } 1252 1342 1253 - if (hpet_rtc_flags & RTC_PIE && 1254 - ++hpet_pie_count >= hpet_pie_limit) { 1343 + if (hpet_rtc_flags & RTC_PIE && ++hpet_pie_count >= hpet_pie_limit) { 1255 1344 rtc_int_flag |= RTC_PF; 1256 1345 hpet_pie_count = 0; 1257 1346 } ··· 1259 1350 (curr_time.tm_sec == hpet_alarm_time.tm_sec) && 1260 1351 (curr_time.tm_min == hpet_alarm_time.tm_min) && 1261 1352 (curr_time.tm_hour == hpet_alarm_time.tm_hour)) 1262 - rtc_int_flag |= RTC_AF; 1353 + rtc_int_flag |= RTC_AF; 1263 1354 1264 1355 if (rtc_int_flag) { 1265 1356 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));