Skip to content

Commit

Permalink
media: pisp-be: Split jobs creation and scheduling
Browse files Browse the repository at this point in the history
Currently the 'pispbe_schedule()' function does two things:

1) Tries to assemble a job by inspecting all the video node queues
   to make sure all the required buffers are available
2) Submit the job to the hardware

The pispbe_schedule() function is called at:

- video device start_streaming() time
- video device qbuf() time
- irq handler

As assembling a job requires inspecting all queues, it is a rather
time consuming operation which is better not run in IRQ context.

To avoid the executing the time consuming job creation in interrupt
context split the job creation and job scheduling in two distinct
operations. When a well-formed job is created, append it to the
newly introduced 'pispbe->job_queue' where it will be dequeued from
by the scheduling routine.

At start_streaming() and qbuf() time immediately try to schedule a job
if one has been created as the irq handler routing is only called when
a job has completed, and we can't solely rely on it for scheduling new
jobs.

Signed-off-by: Jacopo Mondi <[email protected]>
  • Loading branch information
Jacopo Mondi committed Aug 5, 2024
1 parent 3c319a4 commit 1114c40
Showing 1 changed file with 94 additions and 62 deletions.
156 changes: 94 additions & 62 deletions drivers/media/platform/raspberrypi/pisp_be/pisp_be.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,12 @@ struct pispbe_hw_enables {

/* Records a job configuration and memory addresses. */
struct pispbe_job_descriptor {
struct pispbe_buffer *buf[PISPBE_NUM_NODES];
struct pispbe_node_group *node_group;
dma_addr_t hw_dma_addrs[N_HW_ADDRESSES];
struct pisp_be_tiles_config *config;
struct pispbe_hw_enables hw_enables;
struct list_head queue;
dma_addr_t tiles;
};

Expand All @@ -235,8 +238,10 @@ struct pispbe_dev {
struct clk *clk;
struct pispbe_node_group node_group[PISPBE_NUM_NODE_GROUPS];
struct pispbe_job queued_job, running_job;
spinlock_t hw_lock; /* protects "hw_busy" flag and streaming_map */
/* protects "hw_busy" flag, streaming_map and job queue*/
spinlock_t hw_lock;
bool hw_busy; /* non-zero if a job is queued or is being started */
struct list_head job_queue;
int irq;
u32 hw_version;
u8 done, started;
Expand Down Expand Up @@ -463,42 +468,49 @@ static void pispbe_xlate_addrs(struct pispbe_job_descriptor *job,
* For Output0, Output1, Tdn and Stitch, a buffer only needs to be
* available if the blocks are enabled in the config.
*
* Needs to be called with hw_lock held.
* If all the buffers required to form a job are available, append the
* job descriptor to the job queue to be later queued to the HW.
*
* Returns 0 if a job has been successfully prepared, < 0 otherwise.
*/
static int pispbe_prepare_job(struct pispbe_node_group *node_group,
struct pispbe_job_descriptor *job)
static int pispbe_prepare_job(struct pispbe_dev *pispbe,
struct pispbe_node_group *node_group)
{
struct pispbe_buffer *buf[PISPBE_NUM_NODES] = {};
struct pispbe_dev *pispbe = node_group->pispbe;
struct pispbe_job_descriptor *job;
unsigned int streaming_map;
unsigned int config_index;
struct pispbe_node *node;
unsigned long flags;

lockdep_assert_held(&pispbe->hw_lock);

memset(job, 0, sizeof(struct pispbe_job_descriptor));
spin_lock_irqsave(&pispbe->hw_lock, flags);
streaming_map = node_group->streaming_map;
spin_unlock_irqrestore(&pispbe->hw_lock, flags);

if (((BIT(CONFIG_NODE) | BIT(MAIN_INPUT_NODE)) &
node_group->streaming_map) !=
(BIT(CONFIG_NODE) | BIT(MAIN_INPUT_NODE)))
if (((BIT(CONFIG_NODE) | BIT(MAIN_INPUT_NODE)) & streaming_map) !=
(BIT(CONFIG_NODE) | BIT(MAIN_INPUT_NODE)))
return -ENODEV;

job = kzalloc(sizeof(*job), GFP_KERNEL);
if (!job)
return -ENOMEM;

node = &node_group->node[CONFIG_NODE];
spin_lock_irqsave(&node->ready_lock, flags);
buf[CONFIG_NODE] = list_first_entry_or_null(&node->ready_queue,
struct pispbe_buffer,
ready_list);
if (buf[CONFIG_NODE]) {
list_del(&buf[CONFIG_NODE]->ready_list);
pispbe->queued_job.buf[CONFIG_NODE] = buf[CONFIG_NODE];
job->buf[CONFIG_NODE] = buf[CONFIG_NODE];
}
spin_unlock_irqrestore(&node->ready_lock, flags);

/* Exit early if no config buffer has been queued. */
if (!buf[CONFIG_NODE])
if (!buf[CONFIG_NODE]) {
kfree(job);
return -ENODEV;
}

config_index = buf[CONFIG_NODE]->vb.vb2_buf.index;
job->config = &node_group->config[config_index];
Expand All @@ -519,7 +531,7 @@ static int pispbe_prepare_job(struct pispbe_node_group *node_group,
continue;

buf[i] = NULL;
if (!(node_group->streaming_map & BIT(i)))
if (!(streaming_map & BIT(i)))
continue;

if ((!(rgb_en & PISP_BE_RGB_ENABLE_OUTPUT0) &&
Expand Down Expand Up @@ -552,19 +564,23 @@ static int pispbe_prepare_job(struct pispbe_node_group *node_group,
ready_list);
if (buf[i]) {
list_del(&buf[i]->ready_list);
pispbe->queued_job.buf[i] = buf[i];
job->buf[i] = buf[i];
}
spin_unlock_irqrestore(&node->ready_lock, flags);

if (!buf[i] && !ignore_buffers)
goto err_return_buffers;
}

pispbe->queued_job.node_group = node_group;
job->node_group = node_group;

/* Convert buffers to DMA addresses for the hardware */
pispbe_xlate_addrs(job, buf, node_group);

spin_lock_irqsave(&pispbe->hw_lock, flags);
list_add_tail(&job->queue, &pispbe->job_queue);
spin_unlock_irqrestore(&pispbe->hw_lock, flags);

return 0;

err_return_buffers:
Expand All @@ -580,16 +596,15 @@ static int pispbe_prepare_job(struct pispbe_node_group *node_group,
spin_unlock_irqrestore(&n->ready_lock, flags);
}

memset(&pispbe->queued_job, 0, sizeof(pispbe->queued_job));
kfree(job);

return -ENODEV;
}

static void pispbe_schedule(struct pispbe_dev *pispbe,
struct pispbe_node_group *node_group,
bool clear_hw_busy)
{
struct pispbe_job_descriptor job;
struct pispbe_job_descriptor *job;
unsigned long flags;

spin_lock_irqsave(&pispbe->hw_lock, flags);
Expand All @@ -600,53 +615,51 @@ static void pispbe_schedule(struct pispbe_dev *pispbe,
if (pispbe->hw_busy)
goto unlock_and_return;

for (unsigned int i = 0; i < PISPBE_NUM_NODE_GROUPS; i++) {
int ret;
job = list_first_entry_or_null(&pispbe->job_queue,
struct pispbe_job_descriptor,
queue);
if (!job)
goto unlock_and_return;

/* Schedule jobs only for a specific group. */
if (node_group && &pispbe->node_group[i] != node_group)
continue;
list_del(&job->queue);

/*
* Prepare a job for this group, if the group is not ready
* continue and try with the next one.
*/
ret = pispbe_prepare_job(&pispbe->node_group[i], &job);
if (ret)
continue;
for (unsigned int i = 0; i < PISPBE_NUM_NODES; i++)
pispbe->queued_job.buf[i] = job->buf[i];
pispbe->queued_job.node_group = job->node_group;

pispbe->hw_busy = true;

/*
* We can kick the job off without the hw_lock, as this can
* never run again until hw_busy is cleared, which will happen
* only when the following job has been queued and an interrupt
* is rised.
*/
spin_unlock_irqrestore(&pispbe->hw_lock, flags);

if (job->config->num_tiles <= 0 ||
job->config->num_tiles > PISP_BACK_END_NUM_TILES ||
!((job->hw_enables.bayer_enables |
job->hw_enables.rgb_enables) &
PISP_BE_BAYER_ENABLE_INPUT)) {
/*
* We can kick the job off without the hw_lock, as this can
* never run again until hw_busy is cleared, which will happen
* only when the following job has been queued and an interrupt
* is rised.
* Bad job. We can't let it proceed as it could lock up
* the hardware, or worse!
*
* For now, just force num_tiles to 0, which causes the
* H/W to do something bizarre but survivable. It
* increments (started,done) counters by more than 1,
* but we seem to survive...
*/
pispbe->hw_busy = true;
spin_unlock_irqrestore(&pispbe->hw_lock, flags);

if (job.config->num_tiles <= 0 ||
job.config->num_tiles > PISP_BACK_END_NUM_TILES ||
!((job.hw_enables.bayer_enables |
job.hw_enables.rgb_enables) &
PISP_BE_BAYER_ENABLE_INPUT)) {
/*
* Bad job. We can't let it proceed as it could lock up
* the hardware, or worse!
*
* For now, just force num_tiles to 0, which causes the
* H/W to do something bizarre but survivable. It
* increments (started,done) counters by more than 1,
* but we seem to survive...
*/
dev_dbg(pispbe->dev, "Bad job: invalid number of tiles: %u\n",
job.config->num_tiles);
job.config->num_tiles = 0;
}
dev_dbg(pispbe->dev, "Bad job: invalid number of tiles: %u\n",
job->config->num_tiles);
job->config->num_tiles = 0;
}

pispbe_queue_job(pispbe, &job);
pispbe_queue_job(pispbe, job);
kfree(job);

return;
}
return;

unlock_and_return:
/* No job has been queued, just release the lock and return. */
Expand Down Expand Up @@ -721,7 +734,7 @@ static irqreturn_t pispbe_isr(int irq, void *dev)
}

/* check if there's more to do before going to sleep */
pispbe_schedule(pispbe, NULL, can_queue_another);
pispbe_schedule(pispbe, can_queue_another);

return IRQ_HANDLED;
}
Expand Down Expand Up @@ -907,7 +920,8 @@ static void pispbe_node_buffer_queue(struct vb2_buffer *buf)
* Every time we add a buffer, check if there's now some work for the hw
* to do, but only for this client.
*/
pispbe_schedule(node_group->pispbe, node_group, false);
if (!pispbe_prepare_job(pispbe, node_group))
pispbe_schedule(pispbe, false);
}

static int pispbe_node_start_streaming(struct vb2_queue *q, unsigned int count)
Expand All @@ -934,7 +948,8 @@ static int pispbe_node_start_streaming(struct vb2_queue *q, unsigned int count)
node->node_group->streaming_map);

/* Maybe we're ready to run. */
pispbe_schedule(node_group->pispbe, node_group, false);
if (!pispbe_prepare_job(pispbe, node_group))
pispbe_schedule(pispbe, false);

return 0;

Expand Down Expand Up @@ -987,6 +1002,21 @@ static void pispbe_node_stop_streaming(struct vb2_queue *q)

spin_lock_irqsave(&pispbe->hw_lock, flags);
node_group->streaming_map &= ~BIT(node->id);

/* Release all jobs once all nodes have stopped streaming. */
if (node_group->streaming_map == 0) {
struct pispbe_job_descriptor *job;

do {
job = list_first_entry_or_null(&pispbe->job_queue,
struct pispbe_job_descriptor,
queue);
if (job) {
list_del(&job->queue);
kfree(job);
}
} while (!list_empty(&pispbe->job_queue));
}
spin_unlock_irqrestore(&pispbe->hw_lock, flags);

pm_runtime_mark_last_busy(pispbe->dev);
Expand Down Expand Up @@ -1746,6 +1776,8 @@ static int pispbe_probe(struct platform_device *pdev)
if (!pispbe)
return -ENOMEM;

INIT_LIST_HEAD(&pispbe->job_queue);

dev_set_drvdata(&pdev->dev, pispbe);
pispbe->dev = &pdev->dev;
platform_set_drvdata(pdev, pispbe);
Expand Down

0 comments on commit 1114c40

Please sign in to comment.