Merge tag 'renesas-soc2-for-v4.8' of git://git.kernel.org/pub/scm/linux/kernel/git...
[cascardo/linux.git] / arch / x86 / events / intel / rapl.c
index 1705c9d..e30eef4 100644 (file)
  *       event: rapl_energy_dram
  *    perf code: 0x3
  *
- * dram counter: consumption of the builtin-gpu domain (client only)
+ * gpu counter: consumption of the builtin-gpu domain (client only)
  *       event: rapl_energy_gpu
  *    perf code: 0x4
  *
+ *  psys counter: consumption of the builtin-psys domain (client only)
+ *       event: rapl_energy_psys
+ *    perf code: 0x5
+ *
  * We manage those counters as free running (read-only). They may be
  * use simultaneously by other tools, such as turbostat.
  *
@@ -53,6 +57,8 @@
 #include <asm/cpu_device_id.h>
 #include "../perf_event.h"
 
+MODULE_LICENSE("GPL");
+
 /*
  * RAPL energy status counters
  */
 #define INTEL_RAPL_RAM         0x3     /* pseudo-encoding */
 #define RAPL_IDX_PP1_NRG_STAT  3       /* gpu */
 #define INTEL_RAPL_PP1         0x4     /* pseudo-encoding */
+#define RAPL_IDX_PSYS_NRG_STAT 4       /* psys */
+#define INTEL_RAPL_PSYS                0x5     /* pseudo-encoding */
 
-#define NR_RAPL_DOMAINS         0x4
+#define NR_RAPL_DOMAINS         0x5
 static const char *const rapl_domain_names[NR_RAPL_DOMAINS] __initconst = {
        "pp0-core",
        "package",
        "dram",
        "pp1-gpu",
+       "psys",
 };
 
 /* Clients have PP0, PKG */
@@ -89,6 +98,13 @@ static const char *const rapl_domain_names[NR_RAPL_DOMAINS] __initconst = {
                         1<<RAPL_IDX_RAM_NRG_STAT|\
                         1<<RAPL_IDX_PP1_NRG_STAT)
 
+/* SKL clients have PP0, PKG, RAM, PP1, PSYS */
+#define RAPL_IDX_SKL_CLN (1<<RAPL_IDX_PP0_NRG_STAT|\
+                         1<<RAPL_IDX_PKG_NRG_STAT|\
+                         1<<RAPL_IDX_RAM_NRG_STAT|\
+                         1<<RAPL_IDX_PP1_NRG_STAT|\
+                         1<<RAPL_IDX_PSYS_NRG_STAT)
+
 /* Knights Landing has PKG, RAM */
 #define RAPL_IDX_KNL   (1<<RAPL_IDX_PKG_NRG_STAT|\
                         1<<RAPL_IDX_RAM_NRG_STAT)
@@ -360,6 +376,10 @@ static int rapl_pmu_event_init(struct perf_event *event)
                bit = RAPL_IDX_PP1_NRG_STAT;
                msr = MSR_PP1_ENERGY_STATUS;
                break;
+       case INTEL_RAPL_PSYS:
+               bit = RAPL_IDX_PSYS_NRG_STAT;
+               msr = MSR_PLATFORM_ENERGY_STATUS;
+               break;
        default:
                return -EINVAL;
        }
@@ -414,11 +434,13 @@ RAPL_EVENT_ATTR_STR(energy-cores, rapl_cores, "event=0x01");
 RAPL_EVENT_ATTR_STR(energy-pkg  ,   rapl_pkg, "event=0x02");
 RAPL_EVENT_ATTR_STR(energy-ram  ,   rapl_ram, "event=0x03");
 RAPL_EVENT_ATTR_STR(energy-gpu  ,   rapl_gpu, "event=0x04");
+RAPL_EVENT_ATTR_STR(energy-psys,   rapl_psys, "event=0x05");
 
 RAPL_EVENT_ATTR_STR(energy-cores.unit, rapl_cores_unit, "Joules");
 RAPL_EVENT_ATTR_STR(energy-pkg.unit  ,   rapl_pkg_unit, "Joules");
 RAPL_EVENT_ATTR_STR(energy-ram.unit  ,   rapl_ram_unit, "Joules");
 RAPL_EVENT_ATTR_STR(energy-gpu.unit  ,   rapl_gpu_unit, "Joules");
+RAPL_EVENT_ATTR_STR(energy-psys.unit,   rapl_psys_unit, "Joules");
 
 /*
  * we compute in 0.23 nJ increments regardless of MSR
@@ -427,6 +449,7 @@ RAPL_EVENT_ATTR_STR(energy-cores.scale, rapl_cores_scale, "2.3283064365386962890
 RAPL_EVENT_ATTR_STR(energy-pkg.scale,     rapl_pkg_scale, "2.3283064365386962890625e-10");
 RAPL_EVENT_ATTR_STR(energy-ram.scale,     rapl_ram_scale, "2.3283064365386962890625e-10");
 RAPL_EVENT_ATTR_STR(energy-gpu.scale,     rapl_gpu_scale, "2.3283064365386962890625e-10");
+RAPL_EVENT_ATTR_STR(energy-psys.scale,   rapl_psys_scale, "2.3283064365386962890625e-10");
 
 static struct attribute *rapl_events_srv_attr[] = {
        EVENT_PTR(rapl_cores),
@@ -476,6 +499,27 @@ static struct attribute *rapl_events_hsw_attr[] = {
        NULL,
 };
 
+static struct attribute *rapl_events_skl_attr[] = {
+       EVENT_PTR(rapl_cores),
+       EVENT_PTR(rapl_pkg),
+       EVENT_PTR(rapl_gpu),
+       EVENT_PTR(rapl_ram),
+       EVENT_PTR(rapl_psys),
+
+       EVENT_PTR(rapl_cores_unit),
+       EVENT_PTR(rapl_pkg_unit),
+       EVENT_PTR(rapl_gpu_unit),
+       EVENT_PTR(rapl_ram_unit),
+       EVENT_PTR(rapl_psys_unit),
+
+       EVENT_PTR(rapl_cores_scale),
+       EVENT_PTR(rapl_pkg_scale),
+       EVENT_PTR(rapl_gpu_scale),
+       EVENT_PTR(rapl_ram_scale),
+       EVENT_PTR(rapl_psys_scale),
+       NULL,
+};
+
 static struct attribute *rapl_events_knl_attr[] = {
        EVENT_PTR(rapl_pkg),
        EVENT_PTR(rapl_ram),
@@ -592,6 +636,11 @@ static int rapl_cpu_notifier(struct notifier_block *self,
        return NOTIFY_OK;
 }
 
+static struct notifier_block rapl_cpu_nb = {
+       .notifier_call  = rapl_cpu_notifier,
+       .priority       = CPU_PRI_PERF + 1,
+};
+
 static int rapl_check_hw_unit(bool apply_quirk)
 {
        u64 msr_rapl_power_unit_bits;
@@ -660,12 +709,12 @@ static int __init rapl_prepare_cpus(void)
        return 0;
 }
 
-static void __init cleanup_rapl_pmus(void)
+static void cleanup_rapl_pmus(void)
 {
        int i;
 
        for (i = 0; i < rapl_pmus->maxpkg; i++)
-               kfree(rapl_pmus->pmus + i);
+               kfree(rapl_pmus->pmus[i]);
        kfree(rapl_pmus);
 }
 
@@ -691,52 +740,92 @@ static int __init init_rapl_pmus(void)
        return 0;
 }
 
+#define X86_RAPL_MODEL_MATCH(model, init)      \
+       { X86_VENDOR_INTEL, 6, model, X86_FEATURE_ANY, (unsigned long)&init }
+
+struct intel_rapl_init_fun {
+       bool apply_quirk;
+       int cntr_mask;
+       struct attribute **attrs;
+};
+
+static const struct intel_rapl_init_fun snb_rapl_init __initconst = {
+       .apply_quirk = false,
+       .cntr_mask = RAPL_IDX_CLN,
+       .attrs = rapl_events_cln_attr,
+};
+
+static const struct intel_rapl_init_fun hsx_rapl_init __initconst = {
+       .apply_quirk = true,
+       .cntr_mask = RAPL_IDX_SRV,
+       .attrs = rapl_events_srv_attr,
+};
+
+static const struct intel_rapl_init_fun hsw_rapl_init __initconst = {
+       .apply_quirk = false,
+       .cntr_mask = RAPL_IDX_HSW,
+       .attrs = rapl_events_hsw_attr,
+};
+
+static const struct intel_rapl_init_fun snbep_rapl_init __initconst = {
+       .apply_quirk = false,
+       .cntr_mask = RAPL_IDX_SRV,
+       .attrs = rapl_events_srv_attr,
+};
+
+static const struct intel_rapl_init_fun knl_rapl_init __initconst = {
+       .apply_quirk = true,
+       .cntr_mask = RAPL_IDX_KNL,
+       .attrs = rapl_events_knl_attr,
+};
+
+static const struct intel_rapl_init_fun skl_rapl_init __initconst = {
+       .apply_quirk = false,
+       .cntr_mask = RAPL_IDX_SKL_CLN,
+       .attrs = rapl_events_skl_attr,
+};
+
 static const struct x86_cpu_id rapl_cpu_match[] __initconst = {
-       [0] = { .vendor = X86_VENDOR_INTEL, .family = 6 },
-       [1] = {},
+       X86_RAPL_MODEL_MATCH(42, snb_rapl_init),        /* Sandy Bridge */
+       X86_RAPL_MODEL_MATCH(45, snbep_rapl_init),      /* Sandy Bridge-EP */
+
+       X86_RAPL_MODEL_MATCH(58, snb_rapl_init),        /* Ivy Bridge */
+       X86_RAPL_MODEL_MATCH(62, snbep_rapl_init),      /* IvyTown */
+
+       X86_RAPL_MODEL_MATCH(60, hsw_rapl_init),        /* Haswell */
+       X86_RAPL_MODEL_MATCH(63, hsx_rapl_init),        /* Haswell-Server */
+       X86_RAPL_MODEL_MATCH(69, hsw_rapl_init),        /* Haswell-Celeron */
+       X86_RAPL_MODEL_MATCH(70, hsw_rapl_init),        /* Haswell GT3e */
+
+       X86_RAPL_MODEL_MATCH(61, hsw_rapl_init),        /* Broadwell */
+       X86_RAPL_MODEL_MATCH(71, hsw_rapl_init),        /* Broadwell-H */
+       X86_RAPL_MODEL_MATCH(79, hsx_rapl_init),        /* Broadwell-Server */
+       X86_RAPL_MODEL_MATCH(86, hsx_rapl_init),        /* Broadwell Xeon D */
+
+       X86_RAPL_MODEL_MATCH(87, knl_rapl_init),        /* Knights Landing */
+
+       X86_RAPL_MODEL_MATCH(78, skl_rapl_init),        /* Skylake */
+       X86_RAPL_MODEL_MATCH(94, skl_rapl_init),        /* Skylake H/S */
+       {},
 };
 
+MODULE_DEVICE_TABLE(x86cpu, rapl_cpu_match);
+
 static int __init rapl_pmu_init(void)
 {
-       bool apply_quirk = false;
+       const struct x86_cpu_id *id;
+       struct intel_rapl_init_fun *rapl_init;
+       bool apply_quirk;
        int ret;
 
-       if (!x86_match_cpu(rapl_cpu_match))
+       id = x86_match_cpu(rapl_cpu_match);
+       if (!id)
                return -ENODEV;
 
-       switch (boot_cpu_data.x86_model) {
-       case 42: /* Sandy Bridge */
-       case 58: /* Ivy Bridge */
-               rapl_cntr_mask = RAPL_IDX_CLN;
-               rapl_pmu_events_group.attrs = rapl_events_cln_attr;
-               break;
-       case 63: /* Haswell-Server */
-       case 79: /* Broadwell-Server */
-               apply_quirk = true;
-               rapl_cntr_mask = RAPL_IDX_SRV;
-               rapl_pmu_events_group.attrs = rapl_events_srv_attr;
-               break;
-       case 60: /* Haswell */
-       case 69: /* Haswell-Celeron */
-       case 70: /* Haswell GT3e */
-       case 61: /* Broadwell */
-       case 71: /* Broadwell-H */
-               rapl_cntr_mask = RAPL_IDX_HSW;
-               rapl_pmu_events_group.attrs = rapl_events_hsw_attr;
-               break;
-       case 45: /* Sandy Bridge-EP */
-       case 62: /* IvyTown */
-               rapl_cntr_mask = RAPL_IDX_SRV;
-               rapl_pmu_events_group.attrs = rapl_events_srv_attr;
-               break;
-       case 87: /* Knights Landing */
-               apply_quirk = true;
-               rapl_cntr_mask = RAPL_IDX_KNL;
-               rapl_pmu_events_group.attrs = rapl_events_knl_attr;
-               break;
-       default:
-               return -ENODEV;
-       }
+       rapl_init = (struct intel_rapl_init_fun *)id->driver_data;
+       apply_quirk = rapl_init->apply_quirk;
+       rapl_cntr_mask = rapl_init->cntr_mask;
+       rapl_pmu_events_group.attrs = rapl_init->attrs;
 
        ret = rapl_check_hw_unit(apply_quirk);
        if (ret)
@@ -756,7 +845,7 @@ static int __init rapl_pmu_init(void)
        if (ret)
                goto out;
 
-       __perf_cpu_notifier(rapl_cpu_notifier);
+       __register_cpu_notifier(&rapl_cpu_nb);
        cpu_notifier_register_done();
        rapl_advertise();
        return 0;
@@ -767,4 +856,14 @@ out:
        cpu_notifier_register_done();
        return ret;
 }
-device_initcall(rapl_pmu_init);
+module_init(rapl_pmu_init);
+
+static void __exit intel_rapl_exit(void)
+{
+       cpu_notifier_register_begin();
+       __unregister_cpu_notifier(&rapl_cpu_nb);
+       perf_pmu_unregister(&rapl_pmus->pmu);
+       cleanup_rapl_pmus();
+       cpu_notifier_register_done();
+}
+module_exit(intel_rapl_exit);