1 // SPDX-License-Identifier: GPL-2.0
3 * Intel Speed Select Interface: Common functions
4 * Copyright (c) 2019, Intel Corporation.
7 * Author: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
10 #include <linux/cpufeature.h>
11 #include <linux/cpuhotplug.h>
13 #include <linux/hashtable.h>
14 #include <linux/miscdevice.h>
15 #include <linux/module.h>
16 #include <linux/pci.h>
17 #include <linux/sched/signal.h>
18 #include <linux/slab.h>
19 #include <linux/uaccess.h>
20 #include <uapi/linux/isst_if.h>
22 #include <asm/cpu_device_id.h>
23 #include <asm/intel-family.h>
25 #include "isst_if_common.h"
27 #define MSR_THREAD_ID_INFO 0x53
28 #define MSR_PM_LOGICAL_ID 0x54
29 #define MSR_CPU_BUS_NUMBER 0x128
31 static struct isst_if_cmd_cb punit_callbacks[ISST_IF_DEV_MAX];
33 static int punit_msr_white_list[] = {
34 MSR_TURBO_RATIO_LIMIT,
35 MSR_CONFIG_TDP_CONTROL,
36 MSR_TURBO_RATIO_LIMIT1,
37 MSR_TURBO_RATIO_LIMIT2,
41 struct isst_valid_cmd_ranges {
47 struct isst_cmd_set_req_type {
53 static const struct isst_valid_cmd_ranges isst_valid_cmds[] = {
62 static const struct isst_cmd_set_req_type isst_cmd_set_reqs[] = {
73 struct hlist_node hnode;
81 static bool isst_hpm_support;
83 static DECLARE_HASHTABLE(isst_hash, 8);
84 static DEFINE_MUTEX(isst_hash_lock);
86 static int isst_store_new_cmd(int cmd, u32 cpu, int mbox_cmd_type, u32 param,
89 struct isst_cmd *sst_cmd;
91 sst_cmd = kmalloc(sizeof(*sst_cmd), GFP_KERNEL);
97 sst_cmd->mbox_cmd_type = mbox_cmd_type;
98 sst_cmd->param = param;
101 hash_add(isst_hash, &sst_cmd->hnode, sst_cmd->cmd);
106 static void isst_delete_hash(void)
108 struct isst_cmd *sst_cmd;
109 struct hlist_node *tmp;
112 hash_for_each_safe(isst_hash, i, tmp, sst_cmd, hnode) {
113 hash_del(&sst_cmd->hnode);
119 * isst_store_cmd() - Store command to a hash table
120 * @cmd: Mailbox command.
121 * @sub_cmd: Mailbox sub-command or MSR id.
122 * @cpu: Target CPU for the command
123 * @mbox_cmd_type: Mailbox or MSR command.
124 * @param: Mailbox parameter.
125 * @data: Mailbox request data or MSR data.
127 * Stores the command to a hash table if there is no such command already
128 * stored. If already stored update the latest parameter and data for the
131 * Return: Return result of store to hash table, 0 for success, others for
134 int isst_store_cmd(int cmd, int sub_cmd, u32 cpu, int mbox_cmd_type,
137 struct isst_cmd *sst_cmd;
140 full_cmd = (cmd & GENMASK_ULL(15, 0)) << 16;
141 full_cmd |= (sub_cmd & GENMASK_ULL(15, 0));
142 mutex_lock(&isst_hash_lock);
143 hash_for_each_possible(isst_hash, sst_cmd, hnode, full_cmd) {
144 if (sst_cmd->cmd == full_cmd && sst_cmd->cpu == cpu &&
145 sst_cmd->mbox_cmd_type == mbox_cmd_type) {
146 sst_cmd->param = param;
147 sst_cmd->data = data;
148 mutex_unlock(&isst_hash_lock);
153 ret = isst_store_new_cmd(full_cmd, cpu, mbox_cmd_type, param, data);
154 mutex_unlock(&isst_hash_lock);
158 EXPORT_SYMBOL_GPL(isst_store_cmd);
160 static void isst_mbox_resume_command(struct isst_if_cmd_cb *cb,
161 struct isst_cmd *sst_cmd)
163 struct isst_if_mbox_cmd mbox_cmd;
166 mbox_cmd.command = (sst_cmd->cmd & GENMASK_ULL(31, 16)) >> 16;
167 mbox_cmd.sub_command = sst_cmd->cmd & GENMASK_ULL(15, 0);
168 mbox_cmd.parameter = sst_cmd->param;
169 mbox_cmd.req_data = sst_cmd->data;
170 mbox_cmd.logical_cpu = sst_cmd->cpu;
171 (cb->cmd_callback)((u8 *)&mbox_cmd, &wr_only, 1);
175 * isst_resume_common() - Process Resume request
177 * On resume replay all mailbox commands and MSRs.
181 void isst_resume_common(void)
183 struct isst_cmd *sst_cmd;
186 hash_for_each(isst_hash, i, sst_cmd, hnode) {
187 struct isst_if_cmd_cb *cb;
189 if (sst_cmd->mbox_cmd_type) {
190 cb = &punit_callbacks[ISST_IF_DEV_MBOX];
192 isst_mbox_resume_command(cb, sst_cmd);
194 wrmsrl_safe_on_cpu(sst_cmd->cpu, sst_cmd->cmd,
199 EXPORT_SYMBOL_GPL(isst_resume_common);
201 static void isst_restore_msr_local(int cpu)
203 struct isst_cmd *sst_cmd;
206 mutex_lock(&isst_hash_lock);
207 for (i = 0; i < ARRAY_SIZE(punit_msr_white_list); ++i) {
208 if (!punit_msr_white_list[i])
211 hash_for_each_possible(isst_hash, sst_cmd, hnode,
212 punit_msr_white_list[i]) {
213 if (!sst_cmd->mbox_cmd_type && sst_cmd->cpu == cpu)
214 wrmsrl_safe(sst_cmd->cmd, sst_cmd->data);
217 mutex_unlock(&isst_hash_lock);
221 * isst_if_mbox_cmd_invalid() - Check invalid mailbox commands
222 * @cmd: Pointer to the command structure to verify.
224 * Invalid command to PUNIT to may result in instability of the platform.
225 * This function has a whitelist of commands, which are allowed.
227 * Return: Return true if the command is invalid, else false.
229 bool isst_if_mbox_cmd_invalid(struct isst_if_mbox_cmd *cmd)
233 if (cmd->logical_cpu >= nr_cpu_ids)
236 for (i = 0; i < ARRAY_SIZE(isst_valid_cmds); ++i) {
237 if (cmd->command == isst_valid_cmds[i].cmd &&
238 (cmd->sub_command >= isst_valid_cmds[i].sub_cmd_beg &&
239 cmd->sub_command <= isst_valid_cmds[i].sub_cmd_end)) {
246 EXPORT_SYMBOL_GPL(isst_if_mbox_cmd_invalid);
249 * isst_if_mbox_cmd_set_req() - Check mailbox command is a set request
250 * @cmd: Pointer to the command structure to verify.
252 * Check if the given mail box level is set request and not a get request.
254 * Return: Return true if the command is set_req, else false.
256 bool isst_if_mbox_cmd_set_req(struct isst_if_mbox_cmd *cmd)
260 for (i = 0; i < ARRAY_SIZE(isst_cmd_set_reqs); ++i) {
261 if (cmd->command == isst_cmd_set_reqs[i].cmd &&
262 cmd->sub_command == isst_cmd_set_reqs[i].sub_cmd &&
263 cmd->parameter == isst_cmd_set_reqs[i].param) {
270 EXPORT_SYMBOL_GPL(isst_if_mbox_cmd_set_req);
272 static int isst_if_api_version;
274 static int isst_if_get_platform_info(void __user *argp)
276 struct isst_if_platform_info info;
278 info.api_version = isst_if_api_version;
279 info.driver_version = ISST_IF_DRIVER_VERSION;
280 info.max_cmds_per_ioctl = ISST_IF_CMD_LIMIT;
281 info.mbox_supported = punit_callbacks[ISST_IF_DEV_MBOX].registered;
282 info.mmio_supported = punit_callbacks[ISST_IF_DEV_MMIO].registered;
284 if (copy_to_user(argp, &info, sizeof(info)))
290 #define ISST_MAX_BUS_NUMBER 2
292 struct isst_if_cpu_info {
293 /* For BUS 0 and BUS 1 only, which we need for PUNIT interface */
294 int bus_info[ISST_MAX_BUS_NUMBER];
295 struct pci_dev *pci_dev[ISST_MAX_BUS_NUMBER];
300 struct isst_if_pkg_info {
301 struct pci_dev *pci_dev[ISST_MAX_BUS_NUMBER];
304 static struct isst_if_cpu_info *isst_cpu_info;
305 static struct isst_if_pkg_info *isst_pkg_info;
307 static struct pci_dev *_isst_if_get_pci_dev(int cpu, int bus_no, int dev, int fn)
309 struct pci_dev *matched_pci_dev = NULL;
310 struct pci_dev *pci_dev = NULL;
311 struct pci_dev *_pci_dev = NULL;
312 int no_matches = 0, pkg_id;
315 if (bus_no < 0 || bus_no >= ISST_MAX_BUS_NUMBER || cpu < 0 ||
316 cpu >= nr_cpu_ids || cpu >= num_possible_cpus())
319 pkg_id = topology_physical_package_id(cpu);
321 bus_number = isst_cpu_info[cpu].bus_info[bus_no];
325 for_each_pci_dev(_pci_dev) {
328 if (_pci_dev->bus->number != bus_number ||
329 _pci_dev->devfn != PCI_DEVFN(dev, fn))
333 if (!matched_pci_dev)
334 matched_pci_dev = _pci_dev;
336 node = dev_to_node(&_pci_dev->dev);
337 if (node == NUMA_NO_NODE) {
338 pr_info_once("Fail to get numa node for CPU:%d bus:%d dev:%d fn:%d\n",
339 cpu, bus_no, dev, fn);
343 if (node == isst_cpu_info[cpu].numa_node) {
344 isst_pkg_info[pkg_id].pci_dev[bus_no] = _pci_dev;
352 * If there is no numa matched pci_dev, then there can be following cases:
353 * 1. CONFIG_NUMA is not defined: In this case if there is only single device
354 * match, then we don't need numa information. Simply return last match.
355 * Othewise return NULL.
356 * 2. NUMA information is not exposed via _SEG method. In this case it is similar
358 * 3. Numa information doesn't match with CPU numa node and more than one match
361 if (!pci_dev && no_matches == 1)
362 pci_dev = matched_pci_dev;
364 /* Return pci_dev pointer for any matched CPU in the package */
366 pci_dev = isst_pkg_info[pkg_id].pci_dev[bus_no];
372 * isst_if_get_pci_dev() - Get the PCI device instance for a CPU
373 * @cpu: Logical CPU number.
374 * @bus_no: The bus number assigned by the hardware.
375 * @dev: The device number assigned by the hardware.
376 * @fn: The function number assigned by the hardware.
378 * Using cached bus information, find out the PCI device for a bus number,
379 * device and function.
381 * Return: Return pci_dev pointer or NULL.
383 struct pci_dev *isst_if_get_pci_dev(int cpu, int bus_no, int dev, int fn)
385 struct pci_dev *pci_dev;
387 if (bus_no < 0 || bus_no >= ISST_MAX_BUS_NUMBER || cpu < 0 ||
388 cpu >= nr_cpu_ids || cpu >= num_possible_cpus())
391 pci_dev = isst_cpu_info[cpu].pci_dev[bus_no];
393 if (pci_dev && pci_dev->devfn == PCI_DEVFN(dev, fn))
396 return _isst_if_get_pci_dev(cpu, bus_no, dev, fn);
398 EXPORT_SYMBOL_GPL(isst_if_get_pci_dev);
400 static int isst_if_cpu_online(unsigned int cpu)
405 isst_cpu_info[cpu].numa_node = cpu_to_node(cpu);
407 ret = rdmsrl_safe(MSR_CPU_BUS_NUMBER, &data);
409 /* This is not a fatal error on MSR mailbox only I/F */
410 isst_cpu_info[cpu].bus_info[0] = -1;
411 isst_cpu_info[cpu].bus_info[1] = -1;
413 isst_cpu_info[cpu].bus_info[0] = data & 0xff;
414 isst_cpu_info[cpu].bus_info[1] = (data >> 8) & 0xff;
415 isst_cpu_info[cpu].pci_dev[0] = _isst_if_get_pci_dev(cpu, 0, 0, 1);
416 isst_cpu_info[cpu].pci_dev[1] = _isst_if_get_pci_dev(cpu, 1, 30, 1);
419 if (isst_hpm_support) {
421 ret = rdmsrl_safe(MSR_PM_LOGICAL_ID, &data);
426 ret = rdmsrl_safe(MSR_THREAD_ID_INFO, &data);
428 isst_cpu_info[cpu].punit_cpu_id = -1;
433 isst_cpu_info[cpu].punit_cpu_id = data;
435 isst_restore_msr_local(cpu);
440 static int isst_if_online_id;
442 static int isst_if_cpu_info_init(void)
446 isst_cpu_info = kcalloc(num_possible_cpus(),
447 sizeof(*isst_cpu_info),
452 isst_pkg_info = kcalloc(topology_max_packages(),
453 sizeof(*isst_pkg_info),
455 if (!isst_pkg_info) {
456 kfree(isst_cpu_info);
460 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
461 "platform/x86/isst-if:online",
462 isst_if_cpu_online, NULL);
464 kfree(isst_pkg_info);
465 kfree(isst_cpu_info);
469 isst_if_online_id = ret;
474 static void isst_if_cpu_info_exit(void)
476 cpuhp_remove_state(isst_if_online_id);
477 kfree(isst_pkg_info);
478 kfree(isst_cpu_info);
481 static long isst_if_proc_phyid_req(u8 *cmd_ptr, int *write_only, int resume)
483 struct isst_if_cpu_map *cpu_map;
485 cpu_map = (struct isst_if_cpu_map *)cmd_ptr;
486 if (cpu_map->logical_cpu >= nr_cpu_ids ||
487 cpu_map->logical_cpu >= num_possible_cpus())
491 cpu_map->physical_cpu = isst_cpu_info[cpu_map->logical_cpu].punit_cpu_id;
496 static bool match_punit_msr_white_list(int msr)
500 for (i = 0; i < ARRAY_SIZE(punit_msr_white_list); ++i) {
501 if (punit_msr_white_list[i] == msr)
508 static long isst_if_msr_cmd_req(u8 *cmd_ptr, int *write_only, int resume)
510 struct isst_if_msr_cmd *msr_cmd;
513 msr_cmd = (struct isst_if_msr_cmd *)cmd_ptr;
515 if (!match_punit_msr_white_list(msr_cmd->msr))
518 if (msr_cmd->logical_cpu >= nr_cpu_ids)
521 if (msr_cmd->read_write) {
522 if (!capable(CAP_SYS_ADMIN))
525 ret = wrmsrl_safe_on_cpu(msr_cmd->logical_cpu,
530 ret = isst_store_cmd(0, msr_cmd->msr,
531 msr_cmd->logical_cpu,
532 0, 0, msr_cmd->data);
536 ret = rdmsrl_safe_on_cpu(msr_cmd->logical_cpu,
537 msr_cmd->msr, &data);
539 msr_cmd->data = data;
548 static long isst_if_exec_multi_cmd(void __user *argp, struct isst_if_cmd_cb *cb)
550 unsigned char __user *ptr;
556 /* Each multi command has u32 command count as the first field */
557 if (copy_from_user(&cmd_count, argp, sizeof(cmd_count)))
560 if (!cmd_count || cmd_count > ISST_IF_CMD_LIMIT)
563 cmd_ptr = kmalloc(cb->cmd_size, GFP_KERNEL);
567 /* cb->offset points to start of the command after the command count */
568 ptr = argp + cb->offset;
570 for (i = 0; i < cmd_count; ++i) {
573 if (signal_pending(current)) {
578 if (copy_from_user(cmd_ptr, ptr, cb->cmd_size)) {
583 ret = cb->cmd_callback(cmd_ptr, &wr_only, 0);
587 if (!wr_only && copy_to_user(ptr, cmd_ptr, cb->cmd_size)) {
600 static long isst_if_def_ioctl(struct file *file, unsigned int cmd,
603 void __user *argp = (void __user *)arg;
604 struct isst_if_cmd_cb cmd_cb;
605 struct isst_if_cmd_cb *cb;
610 case ISST_IF_GET_PLATFORM_INFO:
611 ret = isst_if_get_platform_info(argp);
613 case ISST_IF_GET_PHY_ID:
614 cmd_cb.cmd_size = sizeof(struct isst_if_cpu_map);
615 cmd_cb.offset = offsetof(struct isst_if_cpu_maps, cpu_map);
616 cmd_cb.cmd_callback = isst_if_proc_phyid_req;
617 ret = isst_if_exec_multi_cmd(argp, &cmd_cb);
620 cb = &punit_callbacks[ISST_IF_DEV_MMIO];
622 ret = isst_if_exec_multi_cmd(argp, cb);
624 case ISST_IF_MBOX_COMMAND:
625 cb = &punit_callbacks[ISST_IF_DEV_MBOX];
627 ret = isst_if_exec_multi_cmd(argp, cb);
629 case ISST_IF_MSR_COMMAND:
630 cmd_cb.cmd_size = sizeof(struct isst_if_msr_cmd);
631 cmd_cb.offset = offsetof(struct isst_if_msr_cmds, msr_cmd);
632 cmd_cb.cmd_callback = isst_if_msr_cmd_req;
633 ret = isst_if_exec_multi_cmd(argp, &cmd_cb);
636 for (i = 0; i < ISST_IF_DEV_MAX; ++i) {
637 struct isst_if_cmd_cb *cb = &punit_callbacks[i];
641 ret = cb->def_ioctl(file, cmd, arg);
652 /* Lock to prevent module registration when already opened by user space */
653 static DEFINE_MUTEX(punit_misc_dev_open_lock);
654 /* Lock to allow one shared misc device for all ISST interfaces */
655 static DEFINE_MUTEX(punit_misc_dev_reg_lock);
656 static int misc_usage_count;
657 static int misc_device_ret;
658 static int misc_device_open;
660 static int isst_if_open(struct inode *inode, struct file *file)
664 /* Fail open, if a module is going away */
665 mutex_lock(&punit_misc_dev_open_lock);
666 for (i = 0; i < ISST_IF_DEV_MAX; ++i) {
667 struct isst_if_cmd_cb *cb = &punit_callbacks[i];
669 if (cb->registered && !try_module_get(cb->owner)) {
677 for (j = 0; j < i; ++j) {
678 struct isst_if_cmd_cb *cb;
680 cb = &punit_callbacks[j];
682 module_put(cb->owner);
687 mutex_unlock(&punit_misc_dev_open_lock);
692 static int isst_if_relase(struct inode *inode, struct file *f)
696 mutex_lock(&punit_misc_dev_open_lock);
698 for (i = 0; i < ISST_IF_DEV_MAX; ++i) {
699 struct isst_if_cmd_cb *cb = &punit_callbacks[i];
702 module_put(cb->owner);
704 mutex_unlock(&punit_misc_dev_open_lock);
709 static const struct file_operations isst_if_char_driver_ops = {
710 .open = isst_if_open,
711 .unlocked_ioctl = isst_if_def_ioctl,
712 .release = isst_if_relase,
715 static struct miscdevice isst_if_char_driver = {
716 .minor = MISC_DYNAMIC_MINOR,
717 .name = "isst_interface",
718 .fops = &isst_if_char_driver_ops,
721 static const struct x86_cpu_id hpm_cpu_ids[] = {
722 X86_MATCH_INTEL_FAM6_MODEL(GRANITERAPIDS_X, NULL),
723 X86_MATCH_INTEL_FAM6_MODEL(ATOM_CRESTMONT_X, NULL),
727 static int isst_misc_reg(void)
729 mutex_lock(&punit_misc_dev_reg_lock);
733 if (!misc_usage_count) {
734 const struct x86_cpu_id *id;
736 id = x86_match_cpu(hpm_cpu_ids);
738 isst_hpm_support = true;
740 misc_device_ret = isst_if_cpu_info_init();
744 misc_device_ret = misc_register(&isst_if_char_driver);
745 if (misc_device_ret) {
746 isst_if_cpu_info_exit();
753 mutex_unlock(&punit_misc_dev_reg_lock);
755 return misc_device_ret;
758 static void isst_misc_unreg(void)
760 mutex_lock(&punit_misc_dev_reg_lock);
761 if (misc_usage_count)
763 if (!misc_usage_count && !misc_device_ret) {
764 misc_deregister(&isst_if_char_driver);
765 isst_if_cpu_info_exit();
767 mutex_unlock(&punit_misc_dev_reg_lock);
771 * isst_if_cdev_register() - Register callback for IOCTL
772 * @device_type: The device type this callback handling.
773 * @cb: Callback structure.
775 * This function registers a callback to device type. On very first call
776 * it will register a misc device, which is used for user kernel interface.
777 * Other calls simply increment ref count. Registry will fail, if the user
778 * already opened misc device for operation. Also if the misc device
779 * creation failed, then it will not try again and all callers will get
782 * Return: Return the return value from the misc creation device or -EINVAL
783 * for unsupported device type.
785 int isst_if_cdev_register(int device_type, struct isst_if_cmd_cb *cb)
789 if (device_type >= ISST_IF_DEV_MAX)
792 mutex_lock(&punit_misc_dev_open_lock);
793 /* Device is already open, we don't want to add new callbacks */
794 if (misc_device_open) {
795 mutex_unlock(&punit_misc_dev_open_lock);
798 if (!cb->api_version)
799 cb->api_version = ISST_IF_API_VERSION;
800 if (cb->api_version > isst_if_api_version)
801 isst_if_api_version = cb->api_version;
802 memcpy(&punit_callbacks[device_type], cb, sizeof(*cb));
803 punit_callbacks[device_type].registered = 1;
804 mutex_unlock(&punit_misc_dev_open_lock);
806 ret = isst_misc_reg();
809 * No need of mutex as the misc device register failed
810 * as no one can open device yet. Hence no contention.
812 punit_callbacks[device_type].registered = 0;
817 EXPORT_SYMBOL_GPL(isst_if_cdev_register);
820 * isst_if_cdev_unregister() - Unregister callback for IOCTL
821 * @device_type: The device type to unregister.
823 * This function unregisters the previously registered callback. If this
824 * is the last callback unregistering, then misc device is removed.
828 void isst_if_cdev_unregister(int device_type)
831 mutex_lock(&punit_misc_dev_open_lock);
832 punit_callbacks[device_type].def_ioctl = NULL;
833 punit_callbacks[device_type].registered = 0;
834 if (device_type == ISST_IF_DEV_MBOX)
836 mutex_unlock(&punit_misc_dev_open_lock);
838 EXPORT_SYMBOL_GPL(isst_if_cdev_unregister);
840 MODULE_LICENSE("GPL v2");