Skip to contentSkip to conversation

x86/iommu: use bit structures for context_entry

PATCH In review Li, Zhen-Hua 1 participant 2 messages Jan 10, 2014 IOMMU

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/iommu: use bit structures for context_entry

There is a structure named context_entry used by intel iommu, and there are some bit operations on it. Use bit structure may make these operations easy.
Also the function context_set_address_root may cause problem because it uses
|= operation, not set the new value. And this patch can fix it.

---
 drivers/iommu/intel-iommu.c | 37 +++++++++++++++++++++++++++----------
 1 file changed, 27 insertions(+), 10 deletions(-)
drivers/iommu/intel-iommu.c +27 -10
index 43b9bfe..65cd480 100644
@@ -221,47 +221,64 @@ get_context_addr_from_root(struct root_entry *root)
  * 8-23: domain id
  */
 struct context_entry {
-	u64 lo;
-	u64 hi;
+	union {
+		struct {
+			u64	present:1,
+				fpd:1,
+				translation_type:2,
+				reserved_l:8,
+				asr:52;
+		};
+		u64 lo;
+	};
+	union {
+		struct {
+			u64	aw:3,
+				avail:4,
+				reserved_h1:1,
+				did:16,
+				reserved_h2:40;
+		};
+		u64 hi;
+	};
 };
 
 static inline bool context_present(struct context_entry *context)
 {
-	return (context->lo & 1);
+	return context->present;
 }
 static inline void context_set_present(struct context_entry *context)
 {
-	context->lo |= 1;
+	context->present = 1;
 }
 
 static inline void context_set_fault_enable(struct context_entry *context)
 {
-	context->lo &= (((u64)-1) << 2) | 1;
+	context->fpd = 1;
 }
 
 static inline void context_set_translation_type(struct context_entry *context,
 						unsigned long value)
 {
-	context->lo &= (((u64)-1) << 4) | 3;
-	context->lo |= (value & 3) << 2;
+	context->translation_type = value;
 }
 
 static inline void context_set_address_root(struct context_entry *context,
 					    unsigned long value)
 {
-	context->lo |= value & VTD_PAGE_MASK;
+	context->asr = value >> VTD_PAGE_SHIFT;
 }
 
 static inline void context_set_address_width(struct context_entry *context,
 					     unsigned long value)
 {
-	context->hi |= value & 7;
+	context->aw  = value;
 }
 
 static inline void context_set_domain_id(struct context_entry *context,
 					 unsigned long value)
 {
-	context->hi |= (value & ((1 << 16) - 1)) << 8;
+	context->did = value;
 }
 
 static inline void context_clear_entry(struct context_entry *context)
Signature · 1 line
1.8.4.3
#1 lore raw

This patch is sent again because a ';' was missed in my last mail.

Review

Quoted 1 line
(quoted diff) +1 -0
+	context->asr = value >> VTD_PAGE_SHIFT

I corrected it and created a new one. And it is tested . Please use the latest one.

Sorry for that.

ZhenHua

Quoted 91 lines · Zhen-Hua

There is a structure named context_entry used by intel iommu, and there are some bit operations on it. Use bit structure may make these operations easy.
Also the function context_set_address_root may cause problem because it uses
|= operation, not set the new value. And this patch can fix it.

---
  drivers/iommu/intel-iommu.c | 37 +++++++++++++++++++++++++++----------
  1 file changed, 27 insertions(+), 10 deletions(-)
drivers/iommu/intel-iommu.c +27 -10
index 43b9bfe..65cd480 100644
@@ -221,47 +221,64 @@ get_context_addr_from_root(struct root_entry *root)
   * 8-23: domain id
   */
  struct context_entry {
-	u64 lo;
-	u64 hi;
+	union {
+		struct {
+			u64	present:1,
+				fpd:1,
+				translation_type:2,
+				reserved_l:8,
+				asr:52;
+		};
+		u64 lo;
+	};
+	union {
+		struct {
+			u64	aw:3,
+				avail:4,
+				reserved_h1:1,
+				did:16,
+				reserved_h2:40;
+		};
+		u64 hi;
+	};
  };
 
  static inline bool context_present(struct context_entry *context)
  {
-	return (context->lo & 1);
+	return context->present;
  }
  static inline void context_set_present(struct context_entry *context)
  {
-	context->lo |= 1;
+	context->present = 1;
  }
 
  static inline void context_set_fault_enable(struct context_entry *context)
  {
-	context->lo &= (((u64)-1) << 2) | 1;
+	context->fpd = 1;
  }
 
  static inline void context_set_translation_type(struct context_entry *context,
  						unsigned long value)
  {
-	context->lo &= (((u64)-1) << 4) | 3;
-	context->lo |= (value & 3) << 2;
+	context->translation_type = value;
  }
 
  static inline void context_set_address_root(struct context_entry *context,
  					    unsigned long value)
  {
-	context->lo |= value & VTD_PAGE_MASK;
+	context->asr = value >> VTD_PAGE_SHIFT;
  }
 
  static inline void context_set_address_width(struct context_entry *context,
  					     unsigned long value)
  {
-	context->hi |= value & 7;
+	context->aw  = value;
  }
 
  static inline void context_set_domain_id(struct context_entry *context,
  					 unsigned long value)
  {
-	context->hi |= (value & ((1 << 16) - 1)) << 8;
+	context->did = value;
  }
 
  static inline void context_clear_entry(struct context_entry *context)