xref: /linux/drivers/thermal/intel/intel_hfi.c (revision 4b99990cdf9560e8a071640baf19f312e6ae02f4)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Hardware Feedback Interface Driver
4  *
5  * Copyright (c) 2021, Intel Corporation.
6  *
7  * Authors: Aubrey Li <aubrey.li@linux.intel.com>
8  *          Ricardo Neri <ricardo.neri-calderon@linux.intel.com>
9  *
10  *
11  * The Hardware Feedback Interface provides a performance and energy efficiency
12  * capability information for each CPU in the system. Depending on the processor
13  * model, hardware may periodically update these capabilities as a result of
14  * changes in the operating conditions (e.g., power limits or thermal
15  * constraints). On other processor models, there is a single HFI update
16  * at boot.
17  *
18  * This file provides functionality to process HFI updates and relay these
19  * updates to userspace.
20  */
21 
22 #define pr_fmt(fmt)  "intel-hfi: " fmt
23 
24 #include <linux/bitops.h>
25 #include <linux/cpufeature.h>
26 #include <linux/cpumask.h>
27 #include <linux/delay.h>
28 #include <linux/gfp.h>
29 #include <linux/io.h>
30 #include <linux/kernel.h>
31 #include <linux/math.h>
32 #include <linux/mutex.h>
33 #include <linux/percpu-defs.h>
34 #include <linux/printk.h>
35 #include <linux/processor.h>
36 #include <linux/slab.h>
37 #include <linux/spinlock.h>
38 #include <linux/suspend.h>
39 #include <linux/string.h>
40 #include <linux/syscore_ops.h>
41 #include <linux/topology.h>
42 #include <linux/workqueue.h>
43 
44 #include <asm/cpuid/api.h>
45 #include <asm/msr.h>
46 
47 #include "intel_hfi.h"
48 #include "thermal_interrupt.h"
49 
50 #include "../thermal_netlink.h"
51 
52 /* Hardware Feedback Interface MSR configuration bits */
53 #define HW_FEEDBACK_PTR_VALID_BIT		BIT(0)
54 #define HW_FEEDBACK_CONFIG_HFI_ENABLE_BIT	BIT(0)
55 
56 /* CPUID detection and enumeration definitions for HFI */
57 
58 #define CPUID_HFI_LEAF 6
59 
60 union hfi_capabilities {
61 	struct {
62 		u8	performance:1;
63 		u8	energy_efficiency:1;
64 		u8	__reserved:6;
65 	} split;
66 	u8 bits;
67 };
68 
69 union cpuid6_edx {
70 	struct {
71 		union hfi_capabilities	capabilities;
72 		u32			table_pages:4;
73 		u32			__reserved:4;
74 		s32			index:16;
75 	} split;
76 	u32 full;
77 };
78 
79 /**
80  * struct hfi_cpu_data - HFI capabilities per CPU
81  * @perf_cap:		Performance capability
82  * @ee_cap:		Energy efficiency capability
83  *
84  * Capabilities of a logical processor in the HFI table. These capabilities are
85  * unitless.
86  */
87 struct hfi_cpu_data {
88 	u8	perf_cap;
89 	u8	ee_cap;
90 } __packed;
91 
92 /**
93  * struct hfi_hdr - Header of the HFI table
94  * @perf_updated:	Hardware updated performance capabilities
95  * @ee_updated:		Hardware updated energy efficiency capabilities
96  *
97  * Properties of the data in an HFI table.
98  */
99 struct hfi_hdr {
100 	u8	perf_updated;
101 	u8	ee_updated;
102 } __packed;
103 
104 /**
105  * struct hfi_instance - Representation of an HFI instance (i.e., a table)
106  * @local_table:	Base of the local copy of the HFI table
107  * @timestamp:		Timestamp of the last update of the local table.
108  *			Located at the base of the local table.
109  * @hdr:		Base address of the header of the local table
110  * @data:		Base address of the data of the local table
111  * @cpus:		CPUs represented in this HFI table instance
112  * @hw_table:		Pointer to the HFI table of this instance
113  * @update_work:	Delayed work to process HFI updates
114  * @table_lock:		Lock to protect acceses to the table of this instance
115  * @event_lock:		Lock to process HFI interrupts
116  *
117  * A set of parameters to parse and navigate a specific HFI table.
118  */
119 struct hfi_instance {
120 	union {
121 		void			*local_table;
122 		u64			*timestamp;
123 	};
124 	void			*hdr;
125 	void			*data;
126 	cpumask_var_t		cpus;
127 	void			*hw_table;
128 	struct delayed_work	update_work;
129 	raw_spinlock_t		table_lock;
130 	raw_spinlock_t		event_lock;
131 };
132 
133 /**
134  * struct hfi_features - Supported HFI features
135  * @nr_table_pages:	Size of the HFI table in 4KB pages
136  * @cpu_stride:		Stride size to locate the capability data of a logical
137  *			processor within the table (i.e., row stride)
138  * @hdr_size:		Size of the table header
139  *
140  * Parameters and supported features that are common to all HFI instances
141  */
142 struct hfi_features {
143 	size_t		nr_table_pages;
144 	unsigned int	cpu_stride;
145 	unsigned int	hdr_size;
146 };
147 
148 /**
149  * struct hfi_cpu_info - Per-CPU attributes to consume HFI data
150  * @index:		Row of this CPU in its HFI table
151  * @hfi_instance:	Attributes of the HFI table to which this CPU belongs
152  *
153  * Parameters to link a logical processor to an HFI table and a row within it.
154  */
155 struct hfi_cpu_info {
156 	s16			index;
157 	struct hfi_instance	*hfi_instance;
158 };
159 
160 static DEFINE_PER_CPU(struct hfi_cpu_info, hfi_cpu_info) = { .index = -1 };
161 
162 static int max_hfi_instances;
163 static int hfi_clients_nr;
164 static struct hfi_instance *hfi_instances;
165 
166 static struct hfi_features hfi_features;
167 static DEFINE_MUTEX(hfi_instance_lock);
168 
169 static struct workqueue_struct *hfi_updates_wq;
170 #define HFI_UPDATE_DELAY_MS		100
171 #define HFI_THERMNL_CAPS_PER_EVENT	64
172 
173 static void get_hfi_caps(struct hfi_instance *hfi_instance,
174 			 struct thermal_genl_cpu_caps *cpu_caps)
175 {
176 	int cpu, i = 0;
177 
178 	raw_spin_lock_irq(&hfi_instance->table_lock);
179 	for_each_cpu(cpu, hfi_instance->cpus) {
180 		struct hfi_cpu_data *caps;
181 		s16 index;
182 
183 		index = per_cpu(hfi_cpu_info, cpu).index;
184 		caps = hfi_instance->data + index * hfi_features.cpu_stride;
185 		cpu_caps[i].cpu = cpu;
186 
187 		/*
188 		 * Scale performance and energy efficiency to
189 		 * the [0, 1023] interval that thermal netlink uses.
190 		 */
191 		cpu_caps[i].performance = caps->perf_cap << 2;
192 		cpu_caps[i].efficiency = caps->ee_cap << 2;
193 
194 		++i;
195 	}
196 	raw_spin_unlock_irq(&hfi_instance->table_lock);
197 }
198 
199 /*
200  * Call update_capabilities() when there are changes in the HFI table.
201  */
202 static void update_capabilities(struct hfi_instance *hfi_instance)
203 {
204 	struct thermal_genl_cpu_caps *cpu_caps;
205 	int i = 0, cpu_count;
206 
207 	/* CPUs may come online/offline while processing an HFI update. */
208 	mutex_lock(&hfi_instance_lock);
209 
210 	cpu_count = cpumask_weight(hfi_instance->cpus);
211 
212 	/* No CPUs to report in this hfi_instance. */
213 	if (!cpu_count)
214 		goto out;
215 
216 	cpu_caps = kzalloc_objs(*cpu_caps, cpu_count);
217 	if (!cpu_caps)
218 		goto out;
219 
220 	get_hfi_caps(hfi_instance, cpu_caps);
221 
222 	if (cpu_count < HFI_THERMNL_CAPS_PER_EVENT)
223 		goto last_cmd;
224 
225 	/* Process complete chunks of HFI_THERMNL_CAPS_PER_EVENT capabilities. */
226 	for (i = 0;
227 	     (i + HFI_THERMNL_CAPS_PER_EVENT) <= cpu_count;
228 	     i += HFI_THERMNL_CAPS_PER_EVENT)
229 		thermal_genl_cpu_capability_event(HFI_THERMNL_CAPS_PER_EVENT,
230 						  &cpu_caps[i]);
231 
232 	cpu_count = cpu_count - i;
233 
234 last_cmd:
235 	/* Process the remaining capabilities if any. */
236 	if (cpu_count)
237 		thermal_genl_cpu_capability_event(cpu_count, &cpu_caps[i]);
238 
239 	kfree(cpu_caps);
240 out:
241 	mutex_unlock(&hfi_instance_lock);
242 }
243 
244 static void hfi_update_work_fn(struct work_struct *work)
245 {
246 	struct hfi_instance *hfi_instance;
247 
248 	hfi_instance = container_of(to_delayed_work(work), struct hfi_instance,
249 				    update_work);
250 
251 	update_capabilities(hfi_instance);
252 }
253 
254 void intel_hfi_process_event(__u64 pkg_therm_status_msr_val)
255 {
256 	struct hfi_instance *hfi_instance;
257 	int cpu = smp_processor_id();
258 	struct hfi_cpu_info *info;
259 	u64 new_timestamp, msr, hfi;
260 
261 	if (!pkg_therm_status_msr_val)
262 		return;
263 
264 	info = &per_cpu(hfi_cpu_info, cpu);
265 	if (!info)
266 		return;
267 
268 	/*
269 	 * A CPU is linked to its HFI instance before the thermal vector in the
270 	 * local APIC is unmasked. Hence, info->hfi_instance cannot be NULL
271 	 * when receiving an HFI event.
272 	 */
273 	hfi_instance = info->hfi_instance;
274 	if (unlikely(!hfi_instance)) {
275 		pr_debug("Received event on CPU %d but instance was null", cpu);
276 		return;
277 	}
278 
279 	/*
280 	 * On most systems, all CPUs in the package receive a package-level
281 	 * thermal interrupt when there is an HFI update. It is sufficient to
282 	 * let a single CPU to acknowledge the update and queue work to
283 	 * process it. The remaining CPUs can resume their work.
284 	 */
285 	if (!raw_spin_trylock(&hfi_instance->event_lock))
286 		return;
287 
288 	rdmsrq(MSR_IA32_PACKAGE_THERM_STATUS, msr);
289 	hfi = msr & PACKAGE_THERM_STATUS_HFI_UPDATED;
290 	if (!hfi) {
291 		raw_spin_unlock(&hfi_instance->event_lock);
292 		return;
293 	}
294 
295 	/*
296 	 * Ack duplicate update. Since there is an active HFI
297 	 * status from HW, it must be a new event, not a case
298 	 * where a lagging CPU entered the locked region.
299 	 */
300 	new_timestamp = *(u64 *)hfi_instance->hw_table;
301 	if (*hfi_instance->timestamp == new_timestamp) {
302 		thermal_clear_package_intr_status(PACKAGE_LEVEL, PACKAGE_THERM_STATUS_HFI_UPDATED);
303 		raw_spin_unlock(&hfi_instance->event_lock);
304 		return;
305 	}
306 
307 	raw_spin_lock(&hfi_instance->table_lock);
308 
309 	/*
310 	 * Copy the updated table into our local copy. This includes the new
311 	 * timestamp.
312 	 */
313 	memcpy(hfi_instance->local_table, hfi_instance->hw_table,
314 	       hfi_features.nr_table_pages << PAGE_SHIFT);
315 
316 	/*
317 	 * Let hardware know that we are done reading the HFI table and it is
318 	 * free to update it again.
319 	 */
320 	thermal_clear_package_intr_status(PACKAGE_LEVEL, PACKAGE_THERM_STATUS_HFI_UPDATED);
321 
322 	raw_spin_unlock(&hfi_instance->table_lock);
323 	raw_spin_unlock(&hfi_instance->event_lock);
324 
325 	queue_delayed_work(hfi_updates_wq, &hfi_instance->update_work,
326 			   msecs_to_jiffies(HFI_UPDATE_DELAY_MS));
327 }
328 
329 static void init_hfi_cpu_index(struct hfi_cpu_info *info)
330 {
331 	union cpuid6_edx edx;
332 
333 	/* Do not re-read @cpu's index if it has already been initialized. */
334 	if (info->index > -1)
335 		return;
336 
337 	edx.full = cpuid_edx(CPUID_HFI_LEAF);
338 	info->index = edx.split.index;
339 }
340 
341 /*
342  * The format of the HFI table depends on the number of capabilities that the
343  * hardware supports. Keep a data structure to navigate the table.
344  */
345 static void init_hfi_instance(struct hfi_instance *hfi_instance)
346 {
347 	/* The HFI header is below the time-stamp. */
348 	hfi_instance->hdr = hfi_instance->local_table +
349 			    sizeof(*hfi_instance->timestamp);
350 
351 	/* The HFI data starts below the header. */
352 	hfi_instance->data = hfi_instance->hdr + hfi_features.hdr_size;
353 }
354 
355 /* Caller must hold hfi_instance_lock. */
356 static void hfi_enable(void)
357 {
358 	u64 msr_val;
359 
360 	rdmsrq(MSR_IA32_HW_FEEDBACK_CONFIG, msr_val);
361 	msr_val |= HW_FEEDBACK_CONFIG_HFI_ENABLE_BIT;
362 	wrmsrq(MSR_IA32_HW_FEEDBACK_CONFIG, msr_val);
363 }
364 
365 static void hfi_set_hw_table(struct hfi_instance *hfi_instance)
366 {
367 	phys_addr_t hw_table_pa;
368 	u64 msr_val;
369 
370 	hw_table_pa = virt_to_phys(hfi_instance->hw_table);
371 	msr_val = hw_table_pa | HW_FEEDBACK_PTR_VALID_BIT;
372 	wrmsrq(MSR_IA32_HW_FEEDBACK_PTR, msr_val);
373 }
374 
375 /* Caller must hold hfi_instance_lock. */
376 static void hfi_disable(void)
377 {
378 	u64 msr_val;
379 	int i;
380 
381 	rdmsrq(MSR_IA32_HW_FEEDBACK_CONFIG, msr_val);
382 	msr_val &= ~HW_FEEDBACK_CONFIG_HFI_ENABLE_BIT;
383 	wrmsrq(MSR_IA32_HW_FEEDBACK_CONFIG, msr_val);
384 
385 	/*
386 	 * Wait for hardware to acknowledge the disabling of HFI. Some
387 	 * processors may not do it. Wait for ~2ms. This is a reasonable
388 	 * time for hardware to complete any pending actions on the HFI
389 	 * memory.
390 	 */
391 	for (i = 0; i < 2000; i++) {
392 		rdmsrq(MSR_IA32_PACKAGE_THERM_STATUS, msr_val);
393 		if (msr_val & PACKAGE_THERM_STATUS_HFI_UPDATED)
394 			break;
395 
396 		udelay(1);
397 		cpu_relax();
398 	}
399 }
400 
401 /**
402  * intel_hfi_online() - Enable HFI on @cpu
403  * @cpu:	CPU in which the HFI will be enabled
404  *
405  * Enable the HFI to be used in @cpu. The HFI is enabled at the package
406  * level. The first CPU in the package to come online does the full HFI
407  * initialization. Subsequent CPUs will just link themselves to the HFI
408  * instance of their package.
409  *
410  * This function is called before enabling the thermal vector in the local APIC
411  * in order to ensure that @cpu has an associated HFI instance when it receives
412  * an HFI event.
413  */
414 void intel_hfi_online(unsigned int cpu)
415 {
416 	struct hfi_instance *hfi_instance;
417 	struct hfi_cpu_info *info;
418 	u16 pkg_id;
419 
420 	/* Nothing to do if hfi_instances are missing. */
421 	if (!hfi_instances)
422 		return;
423 
424 	/*
425 	 * Link @cpu to the HFI instance of its package. It does not
426 	 * matter whether the instance has been initialized.
427 	 */
428 	info = &per_cpu(hfi_cpu_info, cpu);
429 	pkg_id = topology_logical_package_id(cpu);
430 	hfi_instance = info->hfi_instance;
431 	if (!hfi_instance) {
432 		if (pkg_id >= max_hfi_instances)
433 			return;
434 
435 		hfi_instance = &hfi_instances[pkg_id];
436 		info->hfi_instance = hfi_instance;
437 	}
438 
439 	init_hfi_cpu_index(info);
440 
441 	/*
442 	 * Now check if the HFI instance of the package of @cpu has been
443 	 * initialized (by checking its header). In such case, all we have to
444 	 * do is to add @cpu to this instance's cpumask and enable the instance
445 	 * if needed.
446 	 */
447 	mutex_lock(&hfi_instance_lock);
448 	if (hfi_instance->hdr)
449 		goto enable;
450 
451 	/*
452 	 * Hardware is programmed with the physical address of the first page
453 	 * frame of the table. Hence, the allocated memory must be page-aligned.
454 	 *
455 	 * Some processors do not forget the initial address of the HFI table
456 	 * even after having been reprogrammed. Keep using the same pages. Do
457 	 * not free them.
458 	 */
459 	hfi_instance->hw_table = alloc_pages_exact(hfi_features.nr_table_pages,
460 						   GFP_KERNEL | __GFP_ZERO);
461 	if (!hfi_instance->hw_table)
462 		goto unlock;
463 
464 	/*
465 	 * Allocate memory to keep a local copy of the table that
466 	 * hardware generates.
467 	 */
468 	hfi_instance->local_table = kzalloc(hfi_features.nr_table_pages << PAGE_SHIFT,
469 					    GFP_KERNEL);
470 	if (!hfi_instance->local_table)
471 		goto free_hw_table;
472 
473 	init_hfi_instance(hfi_instance);
474 
475 	INIT_DELAYED_WORK(&hfi_instance->update_work, hfi_update_work_fn);
476 	raw_spin_lock_init(&hfi_instance->table_lock);
477 	raw_spin_lock_init(&hfi_instance->event_lock);
478 
479 enable:
480 	cpumask_set_cpu(cpu, hfi_instance->cpus);
481 
482 	/*
483 	 * Enable this HFI instance if this is its first online CPU and
484 	 * there are user-space clients of thermal events.
485 	 */
486 	if (cpumask_weight(hfi_instance->cpus) == 1 && hfi_clients_nr > 0) {
487 		hfi_set_hw_table(hfi_instance);
488 		hfi_enable();
489 	}
490 
491 unlock:
492 	mutex_unlock(&hfi_instance_lock);
493 	return;
494 
495 free_hw_table:
496 	free_pages_exact(hfi_instance->hw_table, hfi_features.nr_table_pages);
497 	goto unlock;
498 }
499 
500 /**
501  * intel_hfi_offline() - Disable HFI on @cpu
502  * @cpu:	CPU in which the HFI will be disabled
503  *
504  * Remove @cpu from those covered by its HFI instance.
505  *
506  * On some processors, hardware remembers previous programming settings even
507  * after being reprogrammed. Thus, keep HFI enabled even if all CPUs in the
508  * package of @cpu are offline. See note in intel_hfi_online().
509  */
510 void intel_hfi_offline(unsigned int cpu)
511 {
512 	struct hfi_cpu_info *info = &per_cpu(hfi_cpu_info, cpu);
513 	struct hfi_instance *hfi_instance;
514 
515 	/*
516 	 * Check if @cpu as an associated, initialized (i.e., with a non-NULL
517 	 * header). Also, HFI instances are only initialized if X86_FEATURE_HFI
518 	 * is present.
519 	 */
520 	hfi_instance = info->hfi_instance;
521 	if (!hfi_instance)
522 		return;
523 
524 	if (!hfi_instance->hdr)
525 		return;
526 
527 	mutex_lock(&hfi_instance_lock);
528 	cpumask_clear_cpu(cpu, hfi_instance->cpus);
529 
530 	if (cpumask_empty(hfi_instance->cpus))
531 		hfi_disable();
532 
533 	mutex_unlock(&hfi_instance_lock);
534 }
535 
536 static __init int hfi_parse_features(void)
537 {
538 	unsigned int nr_capabilities;
539 	union cpuid6_edx edx;
540 
541 	if (!boot_cpu_has(X86_FEATURE_HFI))
542 		return -ENODEV;
543 
544 	/*
545 	 * If we are here we know that CPUID_HFI_LEAF exists. Parse the
546 	 * supported capabilities and the size of the HFI table.
547 	 */
548 	edx.full = cpuid_edx(CPUID_HFI_LEAF);
549 
550 	if (!edx.split.capabilities.split.performance) {
551 		pr_debug("Performance reporting not supported! Not using HFI\n");
552 		return -ENODEV;
553 	}
554 
555 	/*
556 	 * The number of supported capabilities determines the number of
557 	 * columns in the HFI table. Exclude the reserved bits.
558 	 */
559 	edx.split.capabilities.split.__reserved = 0;
560 	nr_capabilities = hweight8(edx.split.capabilities.bits);
561 
562 	/* The number of 4KB pages required by the table */
563 	hfi_features.nr_table_pages = edx.split.table_pages + 1;
564 
565 	/*
566 	 * The header contains change indications for each supported feature.
567 	 * The size of the table header is rounded up to be a multiple of 8
568 	 * bytes.
569 	 */
570 	hfi_features.hdr_size = DIV_ROUND_UP(nr_capabilities, 8) * 8;
571 
572 	/*
573 	 * Data of each logical processor is also rounded up to be a multiple
574 	 * of 8 bytes.
575 	 */
576 	hfi_features.cpu_stride = DIV_ROUND_UP(nr_capabilities, 8) * 8;
577 
578 	return 0;
579 }
580 
581 /*
582  * If concurrency is not prevented by other means, the HFI enable/disable
583  * routines must be called under hfi_instance_lock."
584  */
585 static void hfi_enable_instance(void *ptr)
586 {
587 	hfi_set_hw_table(ptr);
588 	hfi_enable();
589 }
590 
591 static void hfi_disable_instance(void *ptr)
592 {
593 	hfi_disable();
594 }
595 
596 static void hfi_syscore_resume(void *data)
597 {
598 	/* This code runs only on the boot CPU. */
599 	struct hfi_cpu_info *info = &per_cpu(hfi_cpu_info, 0);
600 	struct hfi_instance *hfi_instance = info->hfi_instance;
601 
602 	/* No locking needed. There is no concurrency with CPU online. */
603 	if (hfi_clients_nr > 0)
604 		hfi_enable_instance(hfi_instance);
605 }
606 
607 static int hfi_syscore_suspend(void *data)
608 {
609 	/* No locking needed. There is no concurrency with CPU offline. */
610 	hfi_disable();
611 
612 	return 0;
613 }
614 
615 static const struct syscore_ops hfi_pm_ops = {
616 	.resume = hfi_syscore_resume,
617 	.suspend = hfi_syscore_suspend,
618 };
619 
620 static struct syscore hfi_pm = {
621 	.ops = &hfi_pm_ops,
622 };
623 
624 static int hfi_thermal_notify(struct notifier_block *nb, unsigned long state,
625 			      void *_notify)
626 {
627 	struct thermal_genl_notify *notify = _notify;
628 	struct hfi_instance *hfi_instance;
629 	smp_call_func_t func = NULL;
630 	unsigned int cpu;
631 	int i;
632 
633 	if (notify->mcgrp != THERMAL_GENL_EVENT_GROUP)
634 		return NOTIFY_DONE;
635 
636 	if (state != THERMAL_NOTIFY_BIND && state != THERMAL_NOTIFY_UNBIND)
637 		return NOTIFY_DONE;
638 
639 	mutex_lock(&hfi_instance_lock);
640 
641 	switch (state) {
642 	case THERMAL_NOTIFY_BIND:
643 		if (++hfi_clients_nr == 1)
644 			func = hfi_enable_instance;
645 		break;
646 	case THERMAL_NOTIFY_UNBIND:
647 		if (--hfi_clients_nr == 0)
648 			func = hfi_disable_instance;
649 		break;
650 	}
651 
652 	if (!func)
653 		goto out;
654 
655 	for (i = 0; i < max_hfi_instances; i++) {
656 		hfi_instance = &hfi_instances[i];
657 		if (cpumask_empty(hfi_instance->cpus))
658 			continue;
659 
660 		cpu = cpumask_any(hfi_instance->cpus);
661 		smp_call_function_single(cpu, func, hfi_instance, true);
662 	}
663 
664 out:
665 	mutex_unlock(&hfi_instance_lock);
666 
667 	return NOTIFY_OK;
668 }
669 
670 static struct notifier_block hfi_thermal_nb = {
671 	.notifier_call = hfi_thermal_notify,
672 };
673 
674 void __init intel_hfi_init(void)
675 {
676 	struct hfi_instance *hfi_instance;
677 	int i, j;
678 
679 	if (hfi_parse_features())
680 		return;
681 
682 	/*
683 	 * Note: HFI resources are managed at the physical package scope.
684 	 * There could be platforms that enumerate packages as Linux dies.
685 	 * Special handling would be needed if this happens on an HFI-capable
686 	 * platform.
687 	 */
688 	max_hfi_instances = topology_max_packages();
689 
690 	/*
691 	 * This allocation may fail. CPU hotplug callbacks must check
692 	 * for a null pointer.
693 	 */
694 	hfi_instances = kzalloc_objs(*hfi_instances, max_hfi_instances);
695 	if (!hfi_instances)
696 		return;
697 
698 	for (i = 0; i < max_hfi_instances; i++) {
699 		hfi_instance = &hfi_instances[i];
700 		if (!zalloc_cpumask_var(&hfi_instance->cpus, GFP_KERNEL))
701 			goto err_nomem;
702 	}
703 
704 	hfi_updates_wq = create_singlethread_workqueue("hfi-updates");
705 	if (!hfi_updates_wq)
706 		goto err_nomem;
707 
708 	/*
709 	 * Both thermal core and Intel HFI can not be build as modules.
710 	 * As kernel build-in drivers they are initialized before user-space
711 	 * starts, hence we can not miss BIND/UNBIND events when applications
712 	 * add/remove thermal multicast group to/from a netlink socket.
713 	 */
714 	if (thermal_genl_register_notifier(&hfi_thermal_nb))
715 		goto err_nl_notif;
716 
717 	register_syscore(&hfi_pm);
718 
719 	return;
720 
721 err_nl_notif:
722 	destroy_workqueue(hfi_updates_wq);
723 
724 err_nomem:
725 	for (j = 0; j < i; ++j) {
726 		hfi_instance = &hfi_instances[j];
727 		free_cpumask_var(hfi_instance->cpus);
728 	}
729 
730 	kfree(hfi_instances);
731 	hfi_instances = NULL;
732 }
733