Use non-temporal writes to setup pagetables faster.

Signed-off-by: Daniel J Blueman <daniel@numascale.com>

diff --git a/arch/x86/include/asm/string_64.h b/arch/x86/include/asm/string_64.h
index e466119..ccaa4b0 100644
--- a/arch/x86/include/asm/string_64.h
+++ b/arch/x86/include/asm/string_64.h
@@ -55,6 +55,7 @@ extern void *memcpy(void *to, const void *from, size_t len);
 #define __HAVE_ARCH_MEMSET
 void *memset(void *s, int c, size_t n);
 void *__memset(void *s, int c, size_t n);
+void *memset_nocache(void *s, int c, size_t n);
 
 #define __HAVE_ARCH_MEMMOVE
 void *memmove(void *dest, const void *src, size_t count);
diff --git a/arch/x86/lib/memset_64.S b/arch/x86/lib/memset_64.S
index 2661fad..56e762d 100644
--- a/arch/x86/lib/memset_64.S
+++ b/arch/x86/lib/memset_64.S
@@ -45,6 +45,93 @@ ENDPROC(memset)
 ENDPROC(__memset)
 
 /*
+ * memset_nocache - set a memory block to zero. This function uses
+ * non-temporal writes in the fastpath
+ *
+ * rdi   destination
+ * rsi   value (char)
+ * rdx   count (bytes)
+ *
+ * rax   original destination
+ */
+
+ENTRY(memset_nocache)
+ENTRY(__memset_nocache)
+	movq %rdi,%r10
+
+	/* expand byte value */
+	movzbl %sil,%ecx
+	movabs $0x0101010101010101,%rax
+	imulq  %rcx,%rax
+
+	/* align dst */
+	movl  %edi,%r9d
+	andl  $7,%r9d
+	jnz  bad_alignment
+after_bad_alignment:
+
+	movq  %rdx,%rcx
+	shrq  $6,%rcx
+	jz	 handle_tail
+
+	.p2align 4
+loop_64:
+	decq  %rcx
+	movnti	%rax,(%rdi)
+	movnti	%rax,8(%rdi)
+	movnti	%rax,16(%rdi)
+	movnti	%rax,24(%rdi)
+	movnti	%rax,32(%rdi)
+	movnti	%rax,40(%rdi)
+	movnti	%rax,48(%rdi)
+	movnti	%rax,56(%rdi)
+	leaq  64(%rdi),%rdi
+	jnz    loop_64
+
+	/* Handle tail in loops; the loops should be faster than hard
+	   to predict jump tables */
+	.p2align 4
+handle_tail:
+	movl	%edx,%ecx
+	andl    $63&(~7),%ecx
+	jz 		handle_7
+	shrl	$3,%ecx
+	.p2align 4
+loop_8:
+	decl   %ecx
+	movnti %rax,(%rdi)
+	leaq  8(%rdi),%rdi
+	jnz    loop_8
+
+handle_7:
+	andl	$7,%edx
+	jz      ende
+	.p2align 4
+loop_1:
+	decl    %edx
+	movb	%al,(%rdi)
+	leaq	1(%rdi),%rdi
+	jnz     loop_1
+
+ende:
+	sfence
+	movq	%r10,%rax
+	ret
+
+bad_alignment:
+	cmpq $7,%rdx
+	jbe	handle_7
+	movnti %rax,(%rdi)	/* unaligned store */
+	movq $8,%r8
+	subq %r9,%r8
+	addq %r8,%rdi
+	subq %r8,%rdx
+	jmp after_bad_alignment
+final:
+ENDPROC(memset_nocache)
+ENDPROC(__memset_nocache)
+
+/*
  * ISO C memset - set a memory block to a byte value. This function uses
  * enhanced rep stosb to override the fast string function.
  * The code is simpler and shorter than the fast string function as well.
diff --git a/mm/memblock.c b/mm/memblock.c
index 87108e7..8b426ef 100644
--- a/mm/memblock.c
+++ b/mm/memblock.c
@@ -1300,7 +1300,7 @@ again:
 done:
 	memblock_reserve(alloc, size);
 	ptr = phys_to_virt(alloc);
-	memset(ptr, 0, size);
+	memset_nocache(ptr, 0, size);
 
 	/*
 	 * The min_count is set to 0 so that bootmem allocated blocks
