diff mbox

[RFC,09/16] x86: implement set value flow for MBA.

Message ID 1484034155-4521-10-git-send-email-yi.y.sun@linux.intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Yi Sun Jan. 10, 2017, 7:42 a.m. UTC
This patch implements set value flow for MBA including its callback
function and domctl interface.

Signed-off-by: Yi Sun <yi.y.sun@linux.intel.com>
---
 xen/arch/x86/domctl.c           |   6 ++
 xen/arch/x86/psr.c              | 152 +++++++++++++++++++++++++++++++++++++---
 xen/include/asm-x86/msr-index.h |   1 +
 xen/include/public/domctl.h     |   1 +
 4 files changed, 149 insertions(+), 11 deletions(-)
diff mbox

Patch

diff --git a/xen/arch/x86/domctl.c b/xen/arch/x86/domctl.c
index 6145cf4..a7cf690 100644
--- a/xen/arch/x86/domctl.c
+++ b/xen/arch/x86/domctl.c
@@ -1393,6 +1393,12 @@  long arch_do_domctl(
                               PSR_VAL_TYPE_L2);
             break;
 
+        case XEN_DOMCTL_PSR_MBA_OP_SET_THRTL:
+            ret = psr_set_val(d, domctl->u.psr_alloc_op.target,
+                              domctl->u.psr_alloc_op.data,
+                              PSR_VAL_TYPE_MBA);
+            break;
+
         case XEN_DOMCTL_PSR_CAT_OP_GET_L3_CBM:
             ret = psr_get_val(d, domctl->u.psr_alloc_op.target,
                               &domctl->u.psr_alloc_op.data,
diff --git a/xen/arch/x86/psr.c b/xen/arch/x86/psr.c
index fd95a6d..965ed17 100644
--- a/xen/arch/x86/psr.c
+++ b/xen/arch/x86/psr.c
@@ -332,6 +332,14 @@  static bool_t psr_check_cbm(unsigned int cbm_len, uint64_t cbm)
     return 1;
 }
 
+static bool_t psr_check_thrtl(unsigned int thrtl_max, uint64_t thrtl)
+{
+    if ( thrtl > thrtl_max )
+        return 0;
+
+    return 1;
+}
+
 /* L3 CAT callback functions implementation. */
 static void l3_cat_init_feature(unsigned int eax, unsigned int ebx,
                                 unsigned int ecx, unsigned int edx,
@@ -1012,11 +1020,121 @@  static bool mba_get_val(const struct feat_node *feat, unsigned int cos,
     return true;
 }
 
+static unsigned int mba_get_cos_num(const struct feat_node *feat)
+{
+    /* MBA uses one COS. */
+    return 1;
+}
+
+static int mba_get_old_val(uint64_t val[],
+                           const struct feat_node *feat,
+                           unsigned int old_cos)
+{
+    if ( old_cos > feat->info.mba_info.cos_max )
+        /* Use default value. */
+        old_cos = 0;
+
+    val[0] = feat->cos_reg_val[old_cos];
+
+    /* MBA uses one COS. */
+    return 1;
+}
+
+static int mba_set_new_val(uint64_t val[],
+                           const struct feat_node *feat,
+                           unsigned int old_cos,
+                           enum psr_val_type type,
+                           uint64_t m)
+{
+    if ( type != PSR_VAL_TYPE_MBA )
+        return 1;
+
+    if ( !psr_check_thrtl(feat->info.mba_info.thrtl_max, m) )
+        return -EINVAL;
+
+    val[0] = m;
+
+    /* MBA uses one COS. */
+    return 1;
+}
+
+static unsigned int mba_get_cos_max_from_type(const struct feat_node *feat,
+                                              enum psr_val_type type)
+{
+    if ( type != PSR_VAL_TYPE_MBA )
+        return 0;
+
+    return feat->info.mba_info.cos_max;
+}
+
+static int mba_compare_val(const uint64_t val[],
+                           const struct feat_node *feat,
+                           unsigned int cos, bool *found)
+{
+    uint64_t mba_def = 0;
+
+    if ( cos > feat->info.mba_info.cos_max )
+    {
+        if ( val[0] != mba_def )
+        {
+            *found = false;
+            return -ENOENT;
+        }
+        *found = true;
+    }
+    else
+        *found = (val[0] == feat->cos_reg_val[cos]);
+
+    /* MBA uses one COS. */
+    return 1;
+}
+
+static unsigned int mba_exceeds_cos_max(const uint64_t val[],
+                                        const struct feat_node *feat,
+                                        unsigned int cos)
+{
+    uint64_t mba_def = 0;
+
+    if ( cos > feat->info.mba_info.cos_max &&
+         val[0] != mba_def )
+            /*
+             * Exceed cos_max and value to set is not default,
+             * return error.
+             */
+            return 0;
+
+    /* MBA uses one COS. */
+    return 1;
+}
+
+static int mba_write_msr(unsigned int cos, const uint64_t val[],
+                         struct feat_node *feat)
+{
+    if ( cos > feat->info.mba_info.cos_max )
+        return 1;
+
+    if ( feat->cos_reg_val[cos] != val[0] )
+    {
+        wrmsrl(MSR_IA32_PSR_MBA_MASK(cos), val[0]);
+        feat->cos_reg_val[cos] = val[0];
+    }
+
+    /* MBA uses one COS. */
+    return 1;
+}
+
 struct feat_ops mba_ops = {
     .init_feature = mba_init_feature,
     .get_max_cos_max = mba_get_max_cos_max,
     .get_feat_info = mba_get_feat_info,
     .get_val = mba_get_val,
+    .get_cos_num = mba_get_cos_num,
+    .get_old_val = mba_get_old_val,
+    .set_new_val = mba_set_new_val,
+    .get_cos_max_from_type = mba_get_cos_max_from_type,
+    .compare_val = mba_compare_val,
+    .exceeds_cos_max = mba_exceeds_cos_max,
+    .write_msr = mba_write_msr,
 };
 
 static void __init parse_psr_bool(char *s, char *value, char *feature,
@@ -1519,20 +1637,10 @@  static int write_psr_msr(unsigned int socket, unsigned int cos,
     return 0;
 }
 
-int psr_set_val(struct domain *d, unsigned int socket,
-                uint64_t val, enum psr_val_type type)
+static uint32_t psr_val_type_to_feat_flag(enum psr_val_type type)
 {
-    unsigned int old_cos;
-    int cos, ret;
-    unsigned int *ref;
-    uint64_t *val_array;
-    struct psr_socket_info *info = get_socket_info(socket);
-    uint32_t array_len;
     uint32_t flag;
 
-    if ( IS_ERR(info) )
-        return PTR_ERR(info);
-
     /* Judge if feature is enabled. */
     switch ( type ) {
     case PSR_VAL_TYPE_L3:
@@ -1545,10 +1653,32 @@  int psr_set_val(struct domain *d, unsigned int socket,
     case PSR_VAL_TYPE_L2:
         flag = PSR_SOCKET_L2_CAT;
         break;
+    case PSR_VAL_TYPE_MBA:
+        flag = PSR_SOCKET_MBA;
+        break;
     default:
         flag = 0xFFFF;
         break;
     }
+
+    return flag;
+}
+
+int psr_set_val(struct domain *d, unsigned int socket,
+                uint64_t val, enum psr_val_type type)
+{
+    unsigned int old_cos;
+    int cos, ret;
+    unsigned int *ref;
+    uint64_t *val_array;
+    struct psr_socket_info *info = get_socket_info(socket);
+    uint32_t array_len;
+    uint32_t flag;
+
+    if ( IS_ERR(info) )
+        return PTR_ERR(info);
+
+    flag = psr_val_type_to_feat_flag(type);
     if ( !test_bit(flag, &info->feat_mask) )
         return -ENODEV;
 
diff --git a/xen/include/asm-x86/msr-index.h b/xen/include/asm-x86/msr-index.h
index a41e63a..2e97c6c 100644
--- a/xen/include/asm-x86/msr-index.h
+++ b/xen/include/asm-x86/msr-index.h
@@ -344,6 +344,7 @@ 
 #define MSR_IA32_PSR_L3_MASK_CODE(n)	(0x00000c90 + (n) * 2 + 1)
 #define MSR_IA32_PSR_L3_MASK_DATA(n)	(0x00000c90 + (n) * 2)
 #define MSR_IA32_PSR_L2_MASK(n)		(0x00000d10 + (n))
+#define MSR_IA32_PSR_MBA_MASK(n)	(0x00000d50 + (n))
 
 /* Intel Model 6 */
 #define MSR_P6_PERFCTR(n)		(0x000000c1 + (n))
diff --git a/xen/include/public/domctl.h b/xen/include/public/domctl.h
index 2de7214..11609bc 100644
--- a/xen/include/public/domctl.h
+++ b/xen/include/public/domctl.h
@@ -1140,6 +1140,7 @@  struct xen_domctl_psr_alloc_op {
 #define XEN_DOMCTL_PSR_CAT_OP_GET_L3_DATA    5
 #define XEN_DOMCTL_PSR_CAT_OP_SET_L2_CBM     6
 #define XEN_DOMCTL_PSR_CAT_OP_GET_L2_CBM     7
+#define XEN_DOMCTL_PSR_MBA_OP_SET_THRTL      8
 #define XEN_DOMCTL_PSR_MBA_OP_GET_THRTL      9
     uint32_t cmd;       /* IN: XEN_DOMCTL_PSR_*_OP_* */
     uint32_t target;    /* IN */