fix(clang): fix performance-no-int-to-ptr

Allow the cast from an address originating from a p_addr since
these don't suffer from the ptr -> int -> ptr conversions the check
is trying to prevent.

Signed-off-by: Daniel Boulby <daniel.boulby@arm.com>
Change-Id: I24a7a29fd70599c065d74248dd7fc4cc5c28fa11
diff --git a/src/arch/aarch64/hypervisor/handler.c b/src/arch/aarch64/hypervisor/handler.c
index 7b00158..4f75aec 100644
--- a/src/arch/aarch64/hypervisor/handler.c
+++ b/src/arch/aarch64/hypervisor/handler.c
@@ -61,6 +61,7 @@
  */
 static struct vcpu *current(void)
 {
+	// NOLINTNEXTLINE(performance-no-int-to-ptr)
 	return (struct vcpu *)read_msr(tpidr_el2);
 }
 
diff --git a/src/arch/aarch64/qemuloader/fwcfg.c b/src/arch/aarch64/qemuloader/fwcfg.c
index a483dca..c59279e 100644
--- a/src/arch/aarch64/qemuloader/fwcfg.c
+++ b/src/arch/aarch64/qemuloader/fwcfg.c
@@ -39,14 +39,13 @@
 	return io_read32(FW_CFG_DATA32);
 }
 
-void fw_cfg_read_bytes(uint16_t key, uintptr_t destination, uint32_t length)
+void fw_cfg_read_bytes(uint16_t key, uint8_t *destination, uint32_t length)
 {
-	uint8_t *dest = (uint8_t *)destination;
 	size_t i;
 
 	io_write16(FW_CFG_SELECTOR, htobe16(key));
 	for (i = 0; i < length; ++i) {
-		dest[i] = io_read8(FW_CFG_DATA8);
+		destination[i] = io_read8(FW_CFG_DATA8);
 	}
 }
 
diff --git a/src/arch/aarch64/qemuloader/fwcfg.h b/src/arch/aarch64/qemuloader/fwcfg.h
index e7b1346..05c4334 100644
--- a/src/arch/aarch64/qemuloader/fwcfg.h
+++ b/src/arch/aarch64/qemuloader/fwcfg.h
@@ -16,5 +16,5 @@
 #define FW_CFG_INITRD_DATA 0x12
 
 uint32_t fw_cfg_read_uint32(uint16_t key);
-void fw_cfg_read_bytes(uint16_t key, uintptr_t destination, uint32_t length);
+void fw_cfg_read_bytes(uint16_t key, uint8_t *destination, uint32_t length);
 bool fw_cfg_read_dma(uint16_t key, uintptr_t destination, uint32_t length);
diff --git a/src/arch/aarch64/qemuloader/loader.c b/src/arch/aarch64/qemuloader/loader.c
index 551161e..d8bce30 100644
--- a/src/arch/aarch64/qemuloader/loader.c
+++ b/src/arch/aarch64/qemuloader/loader.c
@@ -27,10 +27,8 @@
 typedef void entry_point(struct fdt_header *, uint64_t, uint64_t, uint64_t);
 
 static noreturn void jump_to_kernel(struct fdt_header *fdt,
-				    uintptr_t kernel_start)
+				    entry_point *kernel_entry)
 {
-	entry_point *kernel_entry = (entry_point *)kernel_start;
-
 	kernel_entry(fdt, 0, 0, 0);
 
 	/* This should never be reached. */
@@ -97,7 +95,10 @@
 	uint32_t initrd_size = fw_cfg_read_uint32(FW_CFG_INITRD_SIZE);
 
 	dlog_info("Initrd start %#x, size %#x\n", initrd_start, initrd_size);
-	fw_cfg_read_bytes(FW_CFG_INITRD_DATA, initrd_start, initrd_size);
+	/* Since the address was calculated from pa_addr above allow the cast */
+	// NOLINTNEXTLINE(performance-no-int-to-ptr)
+	fw_cfg_read_bytes(FW_CFG_INITRD_DATA, (uint8_t *)initrd_start,
+			  initrd_size);
 
 	/*
 	 * Load the kernel after the initrd. Follow Linux alignment conventions
@@ -107,7 +108,10 @@
 		       LINUX_OFFSET;
 	kernel_size = fw_cfg_read_uint32(FW_CFG_KERNEL_SIZE);
 	dlog_info("Kernel start %#x, size %#x\n", kernel_start, kernel_size);
-	fw_cfg_read_bytes(FW_CFG_KERNEL_DATA, kernel_start, kernel_size);
+	/* Since the address was calculated from pa_addr above allow the cast */
+	// NOLINTNEXTLINE(performance-no-int-to-ptr)
+	fw_cfg_read_bytes(FW_CFG_KERNEL_DATA, (uint8_t *)kernel_start,
+			  kernel_size);
 
 	/* Update FDT to point to initrd. */
 	if (initrd_size > 0) {
@@ -119,5 +123,7 @@
 	}
 
 	/* Jump to the kernel. */
-	jump_to_kernel(fdt, kernel_start);
+	/* Since the address was calculated from pa_addr above allow the cast */
+	// NOLINTNEXTLINE(performance-no-int-to-ptr)
+	jump_to_kernel(fdt, (entry_point *)kernel_start);
 }
diff --git a/src/manifest.c b/src/manifest.c
index 1d2d37a..6f366e2 100644
--- a/src/manifest.c
+++ b/src/manifest.c
@@ -811,6 +811,9 @@
 	}
 
 	sp_dtb_addr = pa_add(sp_pkg_start, sp_pkg->pm_offset);
+
+	/* Since the address is from pa_addr allow the cast */
+	// NOLINTNEXTLINE(performance-no-int-to-ptr)
 	if (!fdt_init_from_ptr(&sp_fdt, (void *)pa_addr(sp_dtb_addr),
 			       sp_pkg->pm_size)) {
 		dlog_error("FDT failed validation.\n");
diff --git a/src/mpool.c b/src/mpool.c
index 119b81f..0ee7cb8 100644
--- a/src/mpool.c
+++ b/src/mpool.c
@@ -10,6 +10,8 @@
 
 #include <stdbool.h>
 
+#include "hf/arch/std.h"
+
 struct mpool_chunk {
 	struct mpool_chunk *next_chunk;
 	struct mpool_chunk *limit;
@@ -149,13 +151,12 @@
 bool mpool_add_chunk(struct mpool *p, void *begin, size_t size)
 {
 	struct mpool_chunk *chunk;
-	uintptr_t new_begin;
-	uintptr_t new_end;
+	char *new_begin;
+	char *new_end;
 
 	/* Round begin address up, and end address down. */
-	new_begin = ((uintptr_t)begin + p->entry_size - 1) / p->entry_size *
-		    p->entry_size;
-	new_end = ((uintptr_t)begin + size) / p->entry_size * p->entry_size;
+	new_begin = (void *)align_up((char *)begin, p->entry_size);
+	new_end = (void *)align_down((char *)begin + size, p->entry_size);
 
 	/* Nothing to do if there isn't enough room for an entry. */
 	if (new_begin >= new_end || new_end - new_begin < p->entry_size) {
@@ -201,7 +202,7 @@
 		goto exit;
 	}
 
-	new_chunk = (struct mpool_chunk *)((uintptr_t)chunk + p->entry_size);
+	new_chunk = (struct mpool_chunk *)((char *)chunk + p->entry_size);
 	if (new_chunk >= chunk->limit) {
 		p->chunk_list = chunk->next_chunk;
 	} else {
@@ -274,12 +275,12 @@
 	 */
 	prev = &p->chunk_list;
 	while (*prev != NULL) {
-		uintptr_t start;
+		char *start;
 		struct mpool_chunk *new_chunk;
 		struct mpool_chunk *chunk = *prev;
 
 		/* Round start address up to the required alignment. */
-		start = (((uintptr_t)chunk + align - 1) / align) * align;
+		start = (void *)align_up((char *)chunk, align);
 
 		/*
 		 * Calculate where the new chunk would be if we consume the
@@ -301,7 +302,7 @@
 			 * Add back the space consumed by the alignment
 			 * requirement, if it's big enough to fit an entry.
 			 */
-			if (start - (uintptr_t)chunk >= p->entry_size) {
+			if (start - (char *)chunk >= p->entry_size) {
 				chunk->next_chunk = *prev;
 				*prev = chunk;
 				chunk->limit = (struct mpool_chunk *)start;