kernel/timeout: Enable 64 bit timeout precision

Add a CONFIG_TIMEOUT_64BIT kconfig that, when selected, makes the
k_ticks_t used in timeout computations pervasively 64 bit.  This will
allow much longer timeouts and much faster (i.e. more precise) tick
rates.  It also enables the use of absolute (not delta) timeouts in an
upcoming commit.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1ece51e..80b8964 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -604,6 +604,10 @@
   set(SYSCALL_LONG_REGISTERS_ARG --long-registers)
 endif()
 
+if(CONFIG_TIMEOUT_64BIT)
+  set(SYSCALL_SPLIT_TIMEOUT_ARG --split-type k_timeout_t)
+endif()
+
 add_custom_command(OUTPUT include/generated/syscall_dispatch.c ${syscall_list_h}
   # Also, some files are written to include/generated/syscalls/
   COMMAND
@@ -614,6 +618,7 @@
   --syscall-dispatch include/generated/syscall_dispatch.c # Write this file
   --syscall-list     ${syscall_list_h}
   ${SYSCALL_LONG_REGISTERS_ARG}
+  ${SYSCALL_SPLIT_TIMEOUT_ARG}
   WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
   DEPENDS ${PARSE_SYSCALLS_TARGET}
   ${syscalls_json}
diff --git a/include/sys_clock.h b/include/sys_clock.h
index 123ad82..69b7357 100644
--- a/include/sys_clock.h
+++ b/include/sys_clock.h
@@ -33,7 +33,20 @@
  * @{
  */
 
+/**
+ * @brief Tick precision used in timeout APIs
+ *
+ * This type defines the word size of the timeout values used in
+ * k_timeout_t objects, and thus defines an upper bound on maximum
+ * timeout length (or equivalently minimum tick duration).  Note that
+ * this does not affect the size of the system uptime counter, which
+ * is always a 64 bit count of ticks.
+ */
+#ifdef CONFIG_TIMEOUT_64BIT
+typedef s64_t k_ticks_t;
+#else
 typedef u32_t k_ticks_t;
+#endif
 
 #define K_TICKS_FOREVER ((k_ticks_t) -1)
 
diff --git a/include/timeout_q.h b/include/timeout_q.h
index 061be6d..7acae45 100644
--- a/include/timeout_q.h
+++ b/include/timeout_q.h
@@ -66,7 +66,7 @@
 #define z_init_thread_timeout(t) do {} while (false)
 #define z_abort_thread_timeout(t) (0)
 #define z_is_inactive_timeout(t) 0
-#define z_get_next_timeout_expiry() (K_TICKS_FOREVER)
+#define z_get_next_timeout_expiry() ((s32_t) K_TICKS_FOREVER)
 #define z_set_timeout_expiry(t, i) do {} while (false)
 
 static inline void z_add_thread_timeout(struct k_thread *th, k_timeout_t ticks)
diff --git a/kernel/Kconfig b/kernel/Kconfig
index 90125a6..5261b9c 100644
--- a/kernel/Kconfig
+++ b/kernel/Kconfig
@@ -579,6 +579,16 @@
 	  (which were s32_t counts of milliseconds), at the cost of
 	  not being able to use new features.
 
+config TIMEOUT_64BIT
+	bool
+	depends on !LEGACY_TIMEOUT_API
+	default y
+	help
+	  When this option is true, the k_ticks_t values passed to
+	  kernel APIs will be a 64 bit quantity, allowing the use of
+	  larger values (and higher precision tick rates) without fear
+	  of overflowing the 32 bit word.
+
 config XIP
 	bool "Execute in place"
 	help
diff --git a/kernel/sched.c b/kernel/sched.c
index d4a5594..f531548 100644
--- a/kernel/sched.c
+++ b/kernel/sched.c
@@ -1202,7 +1202,7 @@
 
 	if (K_TIMEOUT_EQ(timeout, K_FOREVER)) {
 		k_thread_suspend(_current);
-		return K_TICKS_FOREVER;
+		return (s32_t) K_TICKS_FOREVER;
 	}
 
 #ifdef CONFIG_LEGACY_TIMEOUT_API
diff --git a/kernel/timeout.c b/kernel/timeout.c
index 11aeed7..6f8efa4 100644
--- a/kernel/timeout.c
+++ b/kernel/timeout.c
@@ -157,7 +157,7 @@
 
 s32_t z_get_next_timeout_expiry(void)
 {
-	s32_t ret = K_TICKS_FOREVER;
+	s32_t ret = (s32_t) K_TICKS_FOREVER;
 
 	LOCKED(&timeout_lock) {
 		ret = next_timeout();
diff --git a/tests/kernel/lifo/lifo_usage/src/main.c b/tests/kernel/lifo/lifo_usage/src/main.c
index d5ea87a..d40f892 100644
--- a/tests/kernel/lifo/lifo_usage/src/main.c
+++ b/tests/kernel/lifo/lifo_usage/src/main.c
@@ -134,7 +134,8 @@
 
 		if (data->timeout_order == ii) {
 			TC_PRINT(" thread (q order: %d, t/o: %d, lifo %p)\n",
-				data->q_order, data->timeout, data->klifo);
+				 data->q_order, (int) data->timeout,
+				 data->klifo);
 		} else {
 			zassert_equal(data->timeout_order, ii, " *** thread %d "
 				      "woke up, expected %d\n",