198253a54SJiri Olsa // SPDX-License-Identifier: GPL-2.0
298253a54SJiri Olsa #include <linux/export.h>
398253a54SJiri Olsa #include <linux/types.h>
498253a54SJiri Olsa #include <linux/bits.h>
598253a54SJiri Olsa #include "probe.h"
698253a54SJiri Olsa
798253a54SJiri Olsa static umode_t
not_visible(struct kobject * kobj,struct attribute * attr,int i)898253a54SJiri Olsa not_visible(struct kobject *kobj, struct attribute *attr, int i)
998253a54SJiri Olsa {
1098253a54SJiri Olsa return 0;
1198253a54SJiri Olsa }
1298253a54SJiri Olsa
134c953f87SStephane Eranian /*
144c953f87SStephane Eranian * Accepts msr[] array with non populated entries as long as either
154c953f87SStephane Eranian * msr[i].msr is 0 or msr[i].grp is NULL. Note that the default sysfs
164c953f87SStephane Eranian * visibility is visible when group->is_visible callback is set.
174c953f87SStephane Eranian */
1898253a54SJiri Olsa unsigned long
perf_msr_probe(struct perf_msr * msr,int cnt,bool zero,void * data)1998253a54SJiri Olsa perf_msr_probe(struct perf_msr *msr, int cnt, bool zero, void *data)
2098253a54SJiri Olsa {
2198253a54SJiri Olsa unsigned long avail = 0;
2298253a54SJiri Olsa unsigned int bit;
2398253a54SJiri Olsa u64 val;
2498253a54SJiri Olsa
2598253a54SJiri Olsa if (cnt >= BITS_PER_LONG)
2698253a54SJiri Olsa return 0;
2798253a54SJiri Olsa
2898253a54SJiri Olsa for (bit = 0; bit < cnt; bit++) {
2998253a54SJiri Olsa if (!msr[bit].no_check) {
3098253a54SJiri Olsa struct attribute_group *grp = msr[bit].grp;
31*ffb20c2eSZhang Rui u64 mask;
3298253a54SJiri Olsa
334c953f87SStephane Eranian /* skip entry with no group */
344c953f87SStephane Eranian if (!grp)
354c953f87SStephane Eranian continue;
364c953f87SStephane Eranian
3798253a54SJiri Olsa grp->is_visible = not_visible;
3898253a54SJiri Olsa
394c953f87SStephane Eranian /* skip unpopulated entry */
404c953f87SStephane Eranian if (!msr[bit].msr)
414c953f87SStephane Eranian continue;
424c953f87SStephane Eranian
4398253a54SJiri Olsa if (msr[bit].test && !msr[bit].test(bit, data))
4498253a54SJiri Olsa continue;
4598253a54SJiri Olsa /* Virt sucks; you cannot tell if a R/O MSR is present :/ */
4698253a54SJiri Olsa if (rdmsrl_safe(msr[bit].msr, &val))
4798253a54SJiri Olsa continue;
48*ffb20c2eSZhang Rui
49*ffb20c2eSZhang Rui mask = msr[bit].mask;
50*ffb20c2eSZhang Rui if (!mask)
51*ffb20c2eSZhang Rui mask = ~0ULL;
5298253a54SJiri Olsa /* Disable zero counters if requested. */
53*ffb20c2eSZhang Rui if (!zero && !(val & mask))
5498253a54SJiri Olsa continue;
5598253a54SJiri Olsa
5698253a54SJiri Olsa grp->is_visible = NULL;
5798253a54SJiri Olsa }
5898253a54SJiri Olsa avail |= BIT(bit);
5998253a54SJiri Olsa }
6098253a54SJiri Olsa
6198253a54SJiri Olsa return avail;
6298253a54SJiri Olsa }
6398253a54SJiri Olsa EXPORT_SYMBOL_GPL(perf_msr_probe);
64