2015-04-11 07:44:37 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013-2014 Wind River Systems, Inc.
|
|
|
|
*
|
2015-10-07 00:00:37 +08:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2015-04-11 07:44:37 +08:00
|
|
|
*
|
2015-10-07 00:00:37 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-04-11 07:44:37 +08:00
|
|
|
*
|
2015-10-07 00:00:37 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2015-04-11 07:44:37 +08:00
|
|
|
*/
|
|
|
|
|
2015-08-30 01:58:07 +08:00
|
|
|
/**
|
|
|
|
* @brief Task IRQ kernel services
|
|
|
|
*
|
|
|
|
* This module manages the interrupt functionality between a task level device
|
|
|
|
* driver and the kernel.
|
|
|
|
*
|
|
|
|
* A task level device driver allocates a specific task IRQ object by providing
|
|
|
|
* an IRQ and priority level; the registration process allocates an interrupt
|
|
|
|
* vector, sets up an ISR, and enables the associated interrupt. When an
|
|
|
|
* interrupt occurs, the ISR handler signals an event specific to the IRQ object.
|
|
|
|
* The task level device driver tests/waits on this event number to determine
|
|
|
|
* if/when the interrupt has occurred. As the ISR also disables the interrupt, the
|
|
|
|
* task level device driver subsequently make a request to have the interrupt
|
2016-01-22 02:59:46 +08:00
|
|
|
* enabled again.
|
2015-08-30 01:58:07 +08:00
|
|
|
*
|
|
|
|
* These routines perform error checking to ensure that an IRQ object can only be
|
|
|
|
* allocated by a single task, and that subsequent operations on that IRQ object
|
|
|
|
* are only performed by that task. This checking is necessary to ensure that
|
|
|
|
* a task cannot impact the operation of an IRQ object it does not own.
|
|
|
|
*
|
2015-07-02 05:22:39 +08:00
|
|
|
*/
|
2015-04-11 07:44:37 +08:00
|
|
|
|
|
|
|
#if (CONFIG_MAX_NUM_TASK_IRQS > 0)
|
|
|
|
|
2015-06-19 23:07:02 +08:00
|
|
|
#include <nano_private.h>
|
2015-04-11 07:44:37 +08:00
|
|
|
#include <microkernel.h>
|
2015-06-19 22:56:52 +08:00
|
|
|
#include <micro_private.h>
|
2015-04-11 07:44:37 +08:00
|
|
|
#include <misc/__assert.h>
|
|
|
|
|
|
|
|
#define MAX_TASK_IRQS CONFIG_MAX_NUM_TASK_IRQS
|
|
|
|
|
|
|
|
#define INVALID_TASK -1
|
|
|
|
|
|
|
|
/* task IRQ object registration request */
|
|
|
|
|
|
|
|
struct irq_obj_reg_arg {
|
|
|
|
kirq_t irq_obj; /* IRQ object identifier */
|
|
|
|
uint32_t irq; /* IRQ of device */
|
2015-08-25 04:35:19 +08:00
|
|
|
ktask_t task_id; /* requesting task */
|
2015-04-11 07:44:37 +08:00
|
|
|
};
|
|
|
|
|
2015-05-16 01:51:24 +08:00
|
|
|
/* task IRQ object type */
|
|
|
|
|
|
|
|
struct task_irq_info {
|
2015-08-25 04:35:19 +08:00
|
|
|
ktask_t task_id; /* task ID of task IRQ object's owner */
|
2015-05-16 01:51:24 +08:00
|
|
|
uint32_t irq; /* IRQ used by task IRQ object */
|
|
|
|
kevent_t event; /* event number assigned to task IRQ object */
|
|
|
|
uint32_t vector; /* interrupt vector assigned to task IRQ object */
|
|
|
|
};
|
|
|
|
|
2015-04-11 07:44:37 +08:00
|
|
|
/* task IRQ object array */
|
|
|
|
|
2015-05-19 01:46:02 +08:00
|
|
|
static struct task_irq_info task_irq_object[MAX_TASK_IRQS] = {
|
2015-08-25 04:35:19 +08:00
|
|
|
[0 ...(MAX_TASK_IRQS - 1)].task_id = INVALID_TASK
|
2015-04-11 07:44:37 +08:00
|
|
|
};
|
|
|
|
|
2015-10-22 02:04:58 +08:00
|
|
|
/* array of event id used by task IRQ objects */
|
|
|
|
extern const kevent_t _TaskIrqEvt_objIds[];
|
2015-04-11 07:44:37 +08:00
|
|
|
|
2015-07-02 05:22:39 +08:00
|
|
|
/**
|
|
|
|
*
|
2015-07-02 05:51:40 +08:00
|
|
|
* @brief ISR for task IRQ objects
|
2015-07-02 05:22:39 +08:00
|
|
|
*
|
|
|
|
* This ISR handles interrupts generated by registered task IRQ objects.
|
|
|
|
*
|
|
|
|
* The ISR triggers an event signal specified by the event number associated
|
|
|
|
* with a particular task IRQ object; the interrupt for the task IRQ object
|
|
|
|
* is then disabled. The parameter provided to the ISR is a structure that
|
|
|
|
* contains information about the objects's vector, IRQ, and event number.
|
|
|
|
*
|
|
|
|
* This ISR does not facilitate an int acknowledgment as it presumes that an
|
2015-08-06 04:39:27 +08:00
|
|
|
* End of Interrupt (EOI) routine is provided by the interrupt controller that
|
|
|
|
* is being used.
|
2015-07-02 05:22:39 +08:00
|
|
|
*
|
2015-08-30 01:58:07 +08:00
|
|
|
* @param parameter Pointer to task IRQ object
|
|
|
|
*
|
2015-07-02 05:29:04 +08:00
|
|
|
* @return N/A
|
2015-07-02 05:22:39 +08:00
|
|
|
*/
|
2015-08-30 01:58:07 +08:00
|
|
|
static void task_irq_int_handler(void *parameter)
|
2015-04-11 07:44:37 +08:00
|
|
|
{
|
|
|
|
struct task_irq_info *irq_obj_ptr = parameter;
|
|
|
|
|
|
|
|
isr_event_send(irq_obj_ptr->event);
|
|
|
|
irq_disable(irq_obj_ptr->irq);
|
|
|
|
}
|
|
|
|
|
2015-07-02 05:22:39 +08:00
|
|
|
/**
|
|
|
|
*
|
2015-07-02 05:51:40 +08:00
|
|
|
* @brief Re-enable a task IRQ object's interrupt
|
2015-07-02 05:22:39 +08:00
|
|
|
*
|
|
|
|
* This re-enables the interrupt for a task IRQ object.
|
2015-08-30 01:58:07 +08:00
|
|
|
* @param irq_obj IRQ object identifier
|
2015-10-21 00:42:33 +08:00
|
|
|
*
|
2015-07-02 05:29:04 +08:00
|
|
|
* @return N/A
|
2015-07-02 05:22:39 +08:00
|
|
|
*/
|
2015-08-30 01:58:07 +08:00
|
|
|
void task_irq_ack(kirq_t irq_obj)
|
2015-04-11 07:44:37 +08:00
|
|
|
{
|
|
|
|
__ASSERT(irq_obj < MAX_TASK_IRQS, "Invalid IRQ object");
|
2015-08-25 04:35:19 +08:00
|
|
|
__ASSERT(task_irq_object[irq_obj].task_id == task_id_get(),
|
2015-04-11 07:44:37 +08:00
|
|
|
"Incorrect Task ID");
|
|
|
|
|
|
|
|
irq_enable(task_irq_object[irq_obj].irq);
|
|
|
|
}
|
|
|
|
|
2015-12-08 04:06:07 +08:00
|
|
|
int task_irq_wait(kirq_t irq_obj, int32_t timeout)
|
2015-04-11 07:44:37 +08:00
|
|
|
{
|
|
|
|
__ASSERT(irq_obj < MAX_TASK_IRQS, "Invalid IRQ object");
|
2015-08-25 04:35:19 +08:00
|
|
|
__ASSERT(task_irq_object[irq_obj].task_id == task_id_get(),
|
2015-04-11 07:44:37 +08:00
|
|
|
"Incorrect Task ID");
|
|
|
|
|
2015-12-08 04:36:25 +08:00
|
|
|
return task_event_recv(task_irq_object[irq_obj].event, timeout);
|
2015-04-11 07:44:37 +08:00
|
|
|
}
|
|
|
|
|
2015-07-02 05:22:39 +08:00
|
|
|
/**
|
|
|
|
*
|
2015-07-02 05:51:40 +08:00
|
|
|
* @brief Allocate a task IRQ object
|
2015-07-02 05:22:39 +08:00
|
|
|
*
|
|
|
|
* This routine allocates a task IRQ object to a task.
|
2015-08-30 01:58:07 +08:00
|
|
|
* @param arg Pointer to registration request arguments
|
2015-10-21 00:42:33 +08:00
|
|
|
*
|
2015-07-02 05:29:04 +08:00
|
|
|
* @return ptr to allocated task IRQ object if successful, NULL if not
|
2015-07-02 05:22:39 +08:00
|
|
|
*/
|
2015-08-30 01:58:07 +08:00
|
|
|
static int _k_task_irq_alloc(void *arg)
|
2015-04-11 07:44:37 +08:00
|
|
|
{
|
|
|
|
struct irq_obj_reg_arg *argp = (struct irq_obj_reg_arg *)arg;
|
|
|
|
struct task_irq_info *irq_obj_ptr; /* ptr to task IRQ object */
|
|
|
|
int curr_irq_obj; /* IRQ object loop counter */
|
|
|
|
|
|
|
|
/* Fail if the requested IRQ object is already in use */
|
|
|
|
|
2015-08-25 04:35:19 +08:00
|
|
|
if (task_irq_object[argp->irq_obj].task_id != INVALID_TASK) {
|
2015-04-11 07:44:37 +08:00
|
|
|
return (int)NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fail if the requested IRQ is already in use */
|
|
|
|
|
|
|
|
for (curr_irq_obj = 0; curr_irq_obj < MAX_TASK_IRQS; curr_irq_obj++) {
|
|
|
|
if ((task_irq_object[curr_irq_obj].irq == argp->irq) &&
|
2015-08-25 04:35:19 +08:00
|
|
|
(task_irq_object[curr_irq_obj].task_id != INVALID_TASK)) {
|
2015-04-11 07:44:37 +08:00
|
|
|
return (int)NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Take ownership of specified IRQ object */
|
|
|
|
|
|
|
|
irq_obj_ptr = &task_irq_object[argp->irq_obj];
|
2015-08-25 04:35:19 +08:00
|
|
|
irq_obj_ptr->task_id = argp->task_id;
|
2015-04-11 07:44:37 +08:00
|
|
|
irq_obj_ptr->irq = argp->irq;
|
2015-10-22 02:04:58 +08:00
|
|
|
irq_obj_ptr->event = _TaskIrqEvt_objIds[argp->irq_obj];
|
2015-04-11 07:44:37 +08:00
|
|
|
irq_obj_ptr->vector = INVALID_VECTOR;
|
|
|
|
|
|
|
|
return (int)irq_obj_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-03 07:06:08 +08:00
|
|
|
uint32_t task_irq_alloc(kirq_t irq_obj, uint32_t irq, uint32_t priority,
|
|
|
|
uint32_t flags)
|
2015-04-11 07:44:37 +08:00
|
|
|
{
|
|
|
|
struct irq_obj_reg_arg arg; /* IRQ object registration request arguments */
|
|
|
|
struct task_irq_info *irq_obj_ptr; /* ptr to task IRQ object */
|
|
|
|
|
|
|
|
/* Allocate the desired IRQ object and IRQ */
|
|
|
|
|
|
|
|
arg.irq_obj = irq_obj;
|
|
|
|
arg.irq = irq;
|
2015-08-25 04:35:19 +08:00
|
|
|
arg.task_id = task_id_get();
|
2015-04-11 07:44:37 +08:00
|
|
|
|
2015-04-29 02:35:49 +08:00
|
|
|
irq_obj_ptr = (struct task_irq_info *)task_offload_to_fiber(_k_task_irq_alloc,
|
2015-04-11 07:44:37 +08:00
|
|
|
(void *)&arg);
|
|
|
|
if (irq_obj_ptr == NULL) {
|
|
|
|
return INVALID_VECTOR;
|
|
|
|
}
|
|
|
|
|
2016-01-08 16:46:14 +08:00
|
|
|
irq_obj_ptr->vector = irq_connect_dynamic(
|
2015-11-03 07:06:08 +08:00
|
|
|
irq, priority, task_irq_int_handler, (void *)irq_obj_ptr,
|
|
|
|
flags);
|
2015-04-11 07:44:37 +08:00
|
|
|
irq_enable(irq);
|
|
|
|
|
|
|
|
return irq_obj_ptr->vector;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* (CONFIG_MAX_NUM_TASK_IRQS > 0) */
|