[Cbe-oss-dev] [PATCH 10/28]MARS/base: kernel cleanup

Yuji Mano yuji.mano at am.sony.com
Fri Feb 6 13:31:21 EST 2009


This cleans up some of the kernel code and also adds a state check when calling
module_workload_wait_set and module_workload_signal_set.

Signed-off-by: Yuji Mano <yuji.mano at am.sony.com>
---
 base/src/common/kernel_internal_types.h |    3 
 base/src/mpu/kernel/kernel.c            |  130 ++++++++++++++++----------------
 base/src/mpu/lib/module.c               |   10 --
 3 files changed, 71 insertions(+), 72 deletions(-)

--- a/base/src/common/kernel_internal_types.h
+++ b/base/src/common/kernel_internal_types.h
@@ -61,6 +61,7 @@ struct mars_kernel_syscalls {
 	struct mars_workload_context *	(*get_workload)(void);
 	struct mars_workload_context *	(*get_workload_by_id)(uint16_t id);
 
+	void	(*workload_exit)(uint8_t state);
 	int	(*workload_query)(uint16_t id, int query);
 	int	(*workload_wait_set)(uint16_t id);
 	int	(*workload_wait_reset)(void);
@@ -72,8 +73,6 @@ struct mars_kernel_syscalls {
 	int	(*workload_schedule_cancel)(uint16_t id);
 
 	int	(*host_signal_send)(uint64_t watch_point_ea);
-
-	void	(*exit)(uint8_t state);
 };
 
 /* mars kernel ticks */
--- a/base/src/mpu/kernel/kernel.c
+++ b/base/src/mpu/kernel/kernel.c
@@ -105,17 +105,17 @@ static void kernel_memcpy(void *dst, con
 	}
 }
 
-static void dma_put(void *ls, uint64_t ea, uint32_t size)
+static void dma_get(void *ls, uint64_t ea, uint32_t size)
 {
-	mfc_put((volatile void *)ls, ea, size, MARS_KERNEL_DMA_TAG, 0, 0);
+	mfc_get((volatile void *)ls, ea, size, MARS_KERNEL_DMA_TAG, 0, 0);
 	mfc_write_tag_mask(1 << MARS_KERNEL_DMA_TAG);
 	mfc_write_tag_update_all();
 	mfc_read_tag_status();
 }
 
-static void dma_get(void *ls, uint64_t ea, uint32_t size)
+static void dma_put(void *ls, uint64_t ea, uint32_t size)
 {
-	mfc_get((volatile void *)ls, ea, size, MARS_KERNEL_DMA_TAG, 0, 0);
+	mfc_put((volatile void *)ls, ea, size, MARS_KERNEL_DMA_TAG, 0, 0);
 	mfc_write_tag_mask(1 << MARS_KERNEL_DMA_TAG);
 	mfc_write_tag_update_all();
 	mfc_read_tag_status();
@@ -167,6 +167,21 @@ static struct mars_workload_context *get
 	return &ret_workload;
 }
 
+static void __attribute__((noinline)) save_workload_state(uint8_t state)
+{
+	workload_state = state;
+}
+
+static void workload_exit(uint8_t state)
+{
+	register void *sp asm("$sp");
+
+	save_workload_state(state);
+
+	/* restore kernel stack pointer */
+	sp = __kernel_stack;
+}
+
 static uint64_t get_block_ea(int block)
 {
 	return queue_header.queue_ea +
@@ -203,7 +218,7 @@ static uint64_t get_block_bits(uint16_t 
 	return block_bits;
 }
 
-static int module_workload_query(uint16_t id, int query)
+static int workload_query(uint16_t id, int query)
 {
 	uint64_t bits = get_block_bits(id);
 
@@ -286,6 +301,11 @@ static int check_state_bits(uint64_t bit
 	return (MARS_BITS_GET(&bits, STATE) == state);
 }
 
+static int check_state_bits_not(uint64_t bits, uint64_t state)
+{
+	return (MARS_BITS_GET(&bits, WORKLOAD_STATE) != state);
+}
+
 static uint64_t set_state_bits(uint64_t bits, uint64_t state)
 {
 	MARS_BITS_SET(&bits, STATE, state);
@@ -311,18 +331,18 @@ static uint64_t set_wait_id_bits(uint64_
 	return bits;
 }
 
-static int module_workload_wait_set(uint16_t id)
+static int workload_wait_set(uint16_t id)
 {
 	if (id == workload_id)
 		return MARS_ERROR_PARAMS;
 
 	return change_bits(workload_id,
-			   NULL, 0,
+			   check_state_bits_not, MARS_WORKLOAD_STATE_NONE,
 			   set_wait_id_bits, id,
 			   NULL);
 }
 
-static int module_workload_wait_reset(void)
+static int workload_wait_reset(void)
 {
 	return change_bits(workload_id,
 			   NULL, 0,
@@ -337,18 +357,18 @@ static uint64_t set_signal_bits(uint64_t
 	return bits;
 }
 
-static int module_workload_signal_set(uint16_t id)
+static int workload_signal_set(uint16_t id)
 {
 	if (id == workload_id)
 		return MARS_ERROR_PARAMS;
 
 	return change_bits(id,
-			   NULL, 0,
+			   check_state_bits_not, MARS_WORKLOAD_STATE_NONE,
 			   set_signal_bits, MARS_WORKLOAD_SIGNAL_ON,
 			   NULL);
 }
 
-static int module_workload_signal_reset(void)
+static int workload_signal_reset(void)
 {
 	return change_bits(workload_id,
 			   NULL, 0,
@@ -358,6 +378,7 @@ static int module_workload_signal_reset(
 
 static uint64_t set_schedule_bits(uint64_t bits, uint64_t priority)
 {
+	/* set the info bits inside queue block for this workload */
 	MARS_BITS_SET(&bits, STATE, MARS_WORKLOAD_STATE_SCHEDULING);
 	MARS_BITS_SET(&bits, PRIORITY, priority);
 	MARS_BITS_SET(&bits, COUNTER, MARS_WORKLOAD_COUNTER_MIN);
@@ -377,8 +398,8 @@ static void schedule_begin_callback(uint
 		sizeof(struct mars_workload_context));
 }
 
-static int module_workload_schedule_begin(uint16_t id, uint8_t priority,
-					struct mars_workload_context **workload)
+static int workload_schedule_begin(uint16_t id, uint8_t priority,
+				   struct mars_workload_context **workload)
 {
 	int ret;
 
@@ -411,7 +432,7 @@ static void schedule_end_callback(uint16
 		sizeof(struct mars_workload_context));
 }
 
-static int module_workload_schedule_end(uint16_t id)
+static int workload_schedule_end(uint16_t id)
 {
 	if (id != schedule_workload_id)
 		return MARS_ERROR_PARAMS;
@@ -422,7 +443,7 @@ static int module_workload_schedule_end(
 			    schedule_end_callback);
 }
 
-static int module_workload_schedule_cancel(uint16_t id)
+static int workload_schedule_cancel(uint16_t id)
 {
 	if (id != schedule_workload_id)
 		return MARS_ERROR_PARAMS;
@@ -433,7 +454,7 @@ static int module_workload_schedule_canc
 			    NULL);
 }
 
-static int module_host_signal_send(uint64_t watch_point_ea)
+static int host_signal_send(uint64_t watch_point_ea)
 {
 #ifdef ENABLE_COND_WAIT_FUTEX
 	spu_write_out_mbox((uint32_t)(watch_point_ea >> 32));
@@ -443,21 +464,6 @@ static int module_host_signal_send(uint6
 	return MARS_SUCCESS;
 }
 
-static void __attribute__((noinline)) save_workload_state(uint8_t state)
-{
-	workload_state = state;
-}
-
-static void module_exit(uint8_t state)
-{
-	register void *sp asm("$sp");
-
-	save_workload_state(state);
-
-	/* restore kernel stack pointer */
-	sp = __kernel_stack;
-}
-
 static struct mars_kernel_syscalls kernel_syscalls =
 {
 	get_ticks,
@@ -466,38 +472,36 @@ static struct mars_kernel_syscalls kerne
 	get_workload_id,
 	get_workload,
 	get_workload_by_id,
-	module_workload_query,
-	module_workload_wait_set,
-	module_workload_wait_reset,
-	module_workload_signal_set,
-	module_workload_signal_reset,
-	module_workload_schedule_begin,
-	module_workload_schedule_end,
-	module_workload_schedule_cancel,
-	module_host_signal_send,
-	module_exit
+	workload_exit,
+	workload_query,
+	workload_wait_set,
+	workload_wait_reset,
+	workload_signal_set,
+	workload_signal_reset,
+	workload_schedule_begin,
+	workload_schedule_end,
+	workload_schedule_cancel,
+	host_signal_send
 };
 
 static int search_block(int block)
 {
 	int i;
 	int index = -1;
+	uint8_t max_priority = 0;
 	uint16_t max_counter = 0;
-	uint16_t max_priority = 0;
 
-	/* search through workload queue for next workload to run
-	 * while incrementing wait counter for all waiting workloads
-	 * and pick the workload that has been waiting the longest
-	 */
+	/* search through all workloads in block */
 	for (i = 0; i < MARS_WORKLOAD_PER_BLOCK; i++) {
-		uint64_t *bits = &queue_block.bits[i];
-		uint8_t signal = MARS_BITS_GET(bits, SIGNAL);
-		uint8_t priority = MARS_BITS_GET(bits, PRIORITY);
-		uint16_t wait_id = MARS_BITS_GET(bits, WAIT_ID);
-		uint16_t counter = MARS_BITS_GET(bits, COUNTER);
+		uint64_t *bits   = &queue_block.bits[i];
+		uint8_t state    = MARS_BITS_GET(bits, WORKLOAD_STATE);
+		uint8_t priority = MARS_BITS_GET(bits, WORKLOAD_PRIORITY);
+		uint8_t signal   = MARS_BITS_GET(bits, WORKLOAD_SIGNAL);
+		uint16_t wait_id = MARS_BITS_GET(bits, WORKLOAD_WAIT_ID);
+		uint16_t counter = MARS_BITS_GET(bits, WORKLOAD_COUNTER);
 
-		switch (MARS_BITS_GET(bits, STATE)) {
-		case MARS_WORKLOAD_STATE_READY:
+		/* found workload in ready state */
+		if (state == MARS_WORKLOAD_STATE_READY) {
 			/* compare priority and counter with previous ones */
 			if (index < 0 || priority > max_priority ||
 			  (priority == max_priority && counter > max_counter)) {
@@ -505,11 +509,12 @@ static int search_block(int block)
 				max_counter = counter;
 				max_priority = priority;
 			}
+
 			/* increment wait counter without overflowing */
 			if (counter < MARS_WORKLOAD_COUNTER_MAX)
 				MARS_BITS_SET(bits, COUNTER, counter + 1);
-			break;
-		case MARS_WORKLOAD_STATE_WAITING:
+		/* found workload in waiting state */
+		} else if (state == MARS_WORKLOAD_STATE_WAITING) {
 			/* waiting for workload to finish so check status */
 			if (wait_id != MARS_WORKLOAD_ID_NONE) {
 				struct mars_workload_queue_block wait_block;
@@ -532,17 +537,18 @@ static int search_block(int block)
 					p_wait_block = &queue_block;
 				}
 
+				/* get state of workload its waiting for */
 				wait_state =
 					MARS_BITS_GET(&p_wait_block->bits[id],
 						      STATE);
 
 				/* check if workload is finished and reset */
 				if (wait_state ==
-					MARS_WORKLOAD_STATE_FINISHED) {
+				    MARS_WORKLOAD_STATE_FINISHED) {
 					MARS_BITS_SET(bits, WAIT_ID,
-					     MARS_WORKLOAD_ID_NONE);
+						MARS_WORKLOAD_ID_NONE);
 					MARS_BITS_SET(bits, STATE,
-					     MARS_WORKLOAD_STATE_READY);
+						MARS_WORKLOAD_STATE_READY);
 				}
 			/* waiting for signal so check signal bit and reset */
 			} else if (signal == MARS_WORKLOAD_SIGNAL_ON) {
@@ -550,11 +556,7 @@ static int search_block(int block)
 					      MARS_WORKLOAD_SIGNAL_OFF);
 				MARS_BITS_SET(bits, STATE,
 					      MARS_WORKLOAD_STATE_READY);
-				i--;
 			}
-			break;
-		default:
-			break;
 		}
 	}
 
@@ -593,7 +595,7 @@ static void notify_host_bits(uint64_t bl
 		offsetof(struct mars_workload_queue_block, bits) +
 		sizeof(uint64_t) * index;
 
-	module_host_signal_send(bits_ea);
+	host_signal_send(bits_ea);
 }
 
 static void release_block(int block, int index)
@@ -797,7 +799,7 @@ int main(unsigned long long mpu_context_
 		}
 	}
 
-	module_host_signal_send(MARS_HOST_SIGNAL_EXIT);
+	host_signal_send(MARS_HOST_SIGNAL_EXIT);
 
 	return MARS_SUCCESS;
 }
--- a/base/src/mpu/lib/module.c
+++ b/base/src/mpu/lib/module.c
@@ -35,8 +35,6 @@
  * LIBRARY OR THE USE OR OTHER DEALINGS IN THE LIBRARY.
  */
 
-#include <stddef.h>
-
 #include "config.h"
 
 #include "mars/module.h"
@@ -73,7 +71,7 @@ void mars_module_entry(struct mars_kerne
 
 	mars_module_main();
 
-	(*kernel_syscalls->exit)(MARS_WORKLOAD_STATE_FINISHED);
+	(*kernel_syscalls->workload_exit)(MARS_WORKLOAD_STATE_FINISHED);
 }
 
 uint32_t mars_module_get_ticks(void)
@@ -151,17 +149,17 @@ int mars_module_workload_schedule_cancel
 
 void mars_module_workload_wait(void)
 {
-	(*kernel_syscalls->exit)(MARS_WORKLOAD_STATE_WAITING);
+	(*kernel_syscalls->workload_exit)(MARS_WORKLOAD_STATE_WAITING);
 }
 
 void mars_module_workload_yield(void)
 {
-	(*kernel_syscalls->exit)(MARS_WORKLOAD_STATE_READY);
+	(*kernel_syscalls->workload_exit)(MARS_WORKLOAD_STATE_READY);
 }
 
 void mars_module_workload_finish(void)
 {
-	(*kernel_syscalls->exit)(MARS_WORKLOAD_STATE_FINISHED);
+	(*kernel_syscalls->workload_exit)(MARS_WORKLOAD_STATE_FINISHED);
 }
 
 int mars_module_host_signal_send(uint64_t watch_point_ea)







More information about the cbe-oss-dev mailing list