intel_hfi.c 19 KB

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