Replace dlog_nosync with lock enabling.

This avoids confusion over which version of dlog needs to be used and
matches the mpool approach.

Change-Id: I8cf2f6204489d0a4cd2bf8ee9bdfc7cc81003e41
diff --git a/inc/hf/dlog.h b/inc/hf/dlog.h
index 18872ae..6b408ee 100644
--- a/inc/hf/dlog.h
+++ b/inc/hf/dlog.h
@@ -19,13 +19,11 @@
 #include <stdarg.h>
 
 #if DEBUG
-void dlog_nosync(const char *fmt, ...);
-void vdlog_nosync(const char *fmt, va_list args);
+void dlog_enable_lock(void);
 void dlog(const char *fmt, ...);
 void vdlog(const char *fmt, va_list args);
 #else
-#define dlog_nosync(...)
-#define vdlog_nosync(fmt, args)
+#define dlog_enable_lock()
 #define dlog(...)
 #define vdlog(fmt, args)
 #endif
diff --git a/src/arch/aarch64/mm.c b/src/arch/aarch64/mm.c
index b960ff5..168ce24 100644
--- a/src/arch/aarch64/mm.c
+++ b/src/arch/aarch64/mm.c
@@ -426,21 +426,19 @@
 
 	/* Check that 4KB granules are supported. */
 	if ((features >> 28) & 0xf) {
-		dlog_nosync("4KB granules are not supported\n");
+		dlog("4KB granules are not supported\n");
 		return false;
 	}
 
 	/* Check the physical address range. */
 	if (!pa_bits) {
-		dlog_nosync(
-			"Unsupported value of id_aa64mmfr0_el1.PARange: %x\n",
-			features & 0xf);
+		dlog("Unsupported value of id_aa64mmfr0_el1.PARange: %x\n",
+		     features & 0xf);
 		return false;
 	}
 
 	if (first) {
-		dlog_nosync("Supported bits in physical address: %d\n",
-			    pa_bits);
+		dlog("Supported bits in physical address: %d\n", pa_bits);
 	}
 
 	/*
diff --git a/src/dlog.c b/src/dlog.c
index 9737805..9471cea 100644
--- a/src/dlog.c
+++ b/src/dlog.c
@@ -36,6 +36,16 @@
 
 /* clang-format on */
 
+static bool dlog_lock_enabled = false;
+
+/**
+ * Enables the lock protecting the serial device.
+ */
+void dlog_enable_lock(void)
+{
+	dlog_lock_enabled = true;
+}
+
 /**
  * Prints a raw string to the debug log and returns its length.
  */
@@ -176,17 +186,20 @@
 }
 
 /**
- * Same as "dlog_nosync", except that arguments are passed as a va_list. This
- * must only be used before the MMU has been initialized so that the atomic
- * operations needed for locking are operational.
+ * Same as "dlog", except that arguments are passed as a va_list
  */
-void vdlog_nosync(const char *fmt, va_list args)
+void vdlog(const char *fmt, va_list args)
 {
+	static struct spinlock sl = SPINLOCK_INIT;
 	const char *p;
 	size_t w;
 	int flags;
 	char buf[2];
 
+	if (dlog_lock_enabled) {
+		sl_lock(&sl);
+	}
+
 	for (p = fmt; *p; p++) {
 		switch (*p) {
 		default:
@@ -280,32 +293,10 @@
 			break;
 		}
 	}
-}
 
-/**
- * Prints the given format string to the debug log without locking. This must
- * only be used before the MMU has been initialized so that the atomic
- * operations needed for locking are operational.
- */
-void dlog_nosync(const char *fmt, ...)
-{
-	va_list args;
-
-	va_start(args, fmt);
-	vdlog_nosync(fmt, args);
-	va_end(args);
-}
-
-/**
- * Same as "dlog", except that arguments are passed as a va_list.
- */
-void vdlog(const char *fmt, va_list args)
-{
-	static struct spinlock sl = SPINLOCK_INIT;
-
-	sl_lock(&sl);
-	vdlog_nosync(fmt, args);
-	sl_unlock(&sl);
+	if (dlog_lock_enabled) {
+		sl_unlock(&sl);
+	}
 }
 
 /**
diff --git a/src/main.c b/src/main.c
index 1ea4ed9..bb19e5a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -46,13 +46,13 @@
 
 	/* TODO: Block all CPUs. */
 
-	dlog_nosync("Panic: ");
+	dlog("Panic: ");
 
 	va_start(args, fmt);
-	vdlog_nosync(fmt, args);
+	vdlog(fmt, args);
 	va_end(args);
 
-	dlog_nosync("\n");
+	dlog("\n");
 
 	for (;;) {
 	}
@@ -71,7 +71,7 @@
 	size_t i;
 	struct mpool ppool;
 
-	dlog_nosync("Initialising hafnium\n");
+	dlog("Initialising hafnium\n");
 
 	mpool_init(&ppool, sizeof(struct mm_page_table));
 	mpool_add_chunk(&ppool, ptable_buf, sizeof(ptable_buf));
@@ -83,6 +83,7 @@
 	}
 
 	/* Enable locks now that mm is initialised. */
+	dlog_enable_lock();
 	mpool_enable_locks();
 
 	if (!plat_get_boot_params(&params, &ppool)) {
diff --git a/src/mm.c b/src/mm.c
index ea239d0..fc4adde 100644
--- a/src/mm.c
+++ b/src/mm.c
@@ -836,15 +836,15 @@
  */
 bool mm_init(struct mpool *ppool)
 {
-	dlog_nosync("text: 0x%x - 0x%x\n", pa_addr(layout_text_begin()),
-		    pa_addr(layout_text_end()));
-	dlog_nosync("rodata: 0x%x - 0x%x\n", pa_addr(layout_rodata_begin()),
-		    pa_addr(layout_rodata_end()));
-	dlog_nosync("data: 0x%x - 0x%x\n", pa_addr(layout_data_begin()),
-		    pa_addr(layout_data_end()));
+	dlog("text: 0x%x - 0x%x\n", pa_addr(layout_text_begin()),
+	     pa_addr(layout_text_end()));
+	dlog("rodata: 0x%x - 0x%x\n", pa_addr(layout_rodata_begin()),
+	     pa_addr(layout_rodata_end()));
+	dlog("data: 0x%x - 0x%x\n", pa_addr(layout_data_begin()),
+	     pa_addr(layout_data_end()));
 
 	if (!mm_ptable_init(&ptable, MM_MODE_STAGE1, ppool)) {
-		dlog_nosync("Unable to allocate memory for page table.\n");
+		dlog("Unable to allocate memory for page table.\n");
 		return false;
 	}