Separate address types from memory management.

This allows extracting some code from the architecture specific headers.

Change-Id: I37f7d9955a10025ef491c4e2ca76a6ffaf123a6b
diff --git a/inc/addr.h b/inc/addr.h
new file mode 100644
index 0000000..24a910c
--- /dev/null
+++ b/inc/addr.h
@@ -0,0 +1,114 @@
+#ifndef _ADDR_H
+#define _ADDR_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include "arch_addr.h"
+
+/* An opaque type for a physical address. */
+typedef struct {
+	uintpaddr_t pa;
+} paddr_t;
+
+/* An opaque type for an intermediate physical address. */
+typedef struct {
+	uintpaddr_t ipa;
+} ipaddr_t;
+
+/* An opaque type for a virtual address. */
+typedef struct {
+	uintvaddr_t va;
+} vaddr_t;
+
+/**
+ * Initializes a physical address.
+ */
+static inline paddr_t pa_init(uintpaddr_t p)
+{
+	return (paddr_t){.pa = p};
+}
+
+/**
+ * Extracts the absolute physical address.
+ */
+static inline uintpaddr_t pa_addr(paddr_t pa)
+{
+	return pa.pa;
+}
+
+/**
+ * Initializes an intermeditate physical address.
+ */
+static inline ipaddr_t ipa_init(uintvaddr_t v)
+{
+	return (ipaddr_t){.ipa = v};
+}
+
+/**
+ * Extracts the absolute intermediate physical address.
+ */
+static inline uintpaddr_t ipa_addr(ipaddr_t ipa)
+{
+	return ipa.ipa;
+}
+
+/**
+ * Initializes a virtual address.
+ */
+static inline vaddr_t va_init(uintvaddr_t v)
+{
+	return (vaddr_t){.va = v};
+}
+
+/**
+ * Extracts the absolute virtual address.
+ */
+static inline uintvaddr_t va_addr(vaddr_t va)
+{
+	return va.va;
+}
+
+/**
+ * Advances a virtual address.
+ */
+static inline vaddr_t va_add(vaddr_t va, size_t n)
+{
+	return va_init(va_addr(va) + n);
+}
+
+/**
+ * Casts a physical address to a virtual address.
+ */
+static inline vaddr_t va_from_pa(paddr_t pa)
+{
+	return va_init(pa_addr(pa));
+}
+
+/**
+ * Casts a virtual address to a physical address.
+ */
+static inline paddr_t pa_from_va(vaddr_t va)
+{
+	return pa_init(va_addr(va));
+}
+
+/**
+ * Casts a pointer to a virtual address.
+ */
+static inline vaddr_t va_from_ptr(const void *p)
+{
+	return (vaddr_t){.va = (uintvaddr_t)p};
+}
+
+/**
+ * Casts a virtual address to a pointer. Only use when the virtual address is
+ * mapped for the calling context.
+ * TODO: check the mapping for a range and return a memiter?
+ */
+static inline void *ptr_from_va(vaddr_t va)
+{
+	return (void *)va_addr(va);
+}
+
+#endif /* _ADDR_H */
diff --git a/inc/mm.h b/inc/mm.h
index 1e00310..74158a7 100644
--- a/inc/mm.h
+++ b/inc/mm.h
@@ -4,6 +4,7 @@
 #include <stdbool.h>
 #include <stdint.h>
 
+#include "addr.h"
 #include "arch_mm.h"
 
 struct mm_ptable {
@@ -11,11 +12,6 @@
 	uint32_t id;
 };
 
-/* An opaque type for an intermediate physical address from a VM. */
-typedef struct {
-	uintpaddr_t ipa;
-} ipaddr_t;
-
 #define PAGE_SIZE (1 << PAGE_BITS)
 
 /* The following are arch-independent page mapping modes. */
@@ -61,46 +57,12 @@
 void mm_defrag(void);
 
 /**
- * Initializes an intermeditate physical address.
- */
-static inline ipaddr_t ipa_init(uintvaddr_t v)
-{
-	return (ipaddr_t){.ipa = v};
-}
-
-/**
- * Extracts the absolute intermediate physical address.
- */
-static inline uintpaddr_t ipa_addr(ipaddr_t ipa)
-{
-	return ipa.ipa;
-}
-
-/**
- * Converts a physical address to a virtual address. Addresses are currently
- * identity mapped so this is a simple type convertion.
- */
-static inline vaddr_t mm_va_from_pa(paddr_t pa)
-{
-	return va_init(pa_addr(pa));
-}
-
-/**
- * Converts a virtual address to a physical address. Addresses are currently
- * identity mapped so this is a simple type convertion.
- */
-static inline paddr_t mm_pa_from_va(vaddr_t va)
-{
-	return pa_init(va_addr(va));
-}
-
-/**
  * Converts an intermediate physical address to a physical address. Addresses
  * are currently identity mapped so this is a simple type convertion. Returns
  * true if the address was mapped in the table and the address was converted.
  */
-static inline bool mm_pa_from_ipa(struct mm_ptable *t, ipaddr_t ipa,
-				  paddr_t *pa)
+static inline bool mm_ptable_translate_ipa(struct mm_ptable *t, ipaddr_t ipa,
+					   paddr_t *pa)
 {
 	/* TODO: the ptable functions map physical to virtual addresses but they
 	 * should really be mapping to intermediate physical addresses.
@@ -115,13 +77,4 @@
 	return false;
 }
 
-/**
- * Converts a virtual address to a pointer. Only use when the virtual address
- * is mapped for the calling context.
- */
-static inline void *mm_ptr_from_va(vaddr_t va)
-{
-	return (void *)va_addr(va);
-}
-
 #endif /* _MM_H */