perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / media / platform / omap3isp / ispstat.c
blob47353fee26c3297234502d6d9ce197ff5098eb0c
1 /*
2 * ispstat.c
4 * TI OMAP3 ISP - Statistics core
6 * Copyright (C) 2010 Nokia Corporation
7 * Copyright (C) 2009 Texas Instruments, Inc
9 * Contacts: David Cohen <dacohen@gmail.com>
10 * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
11 * Sakari Ailus <sakari.ailus@iki.fi>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
18 #include <linux/dma-mapping.h>
19 #include <linux/slab.h>
20 #include <linux/timekeeping.h>
21 #include <linux/uaccess.h>
23 #include "isp.h"
25 #define ISP_STAT_USES_DMAENGINE(stat) ((stat)->dma_ch != NULL)
28 * MAGIC_SIZE must always be the greatest common divisor of
29 * AEWB_PACKET_SIZE and AF_PAXEL_SIZE.
31 #define MAGIC_SIZE 16
32 #define MAGIC_NUM 0x55
34 /* HACK: AF module seems to be writing one more paxel data than it should. */
35 #define AF_EXTRA_DATA OMAP3ISP_AF_PAXEL_SIZE
38 * HACK: H3A modules go to an invalid state after have a SBL overflow. It makes
39 * the next buffer to start to be written in the same point where the overflow
40 * occurred instead of the configured address. The only known way to make it to
41 * go back to a valid state is having a valid buffer processing. Of course it
42 * requires at least a doubled buffer size to avoid an access to invalid memory
43 * region. But it does not fix everything. It may happen more than one
44 * consecutive SBL overflows. In that case, it might be unpredictable how many
45 * buffers the allocated memory should fit. For that case, a recover
46 * configuration was created. It produces the minimum buffer size for each H3A
47 * module and decrease the change for more SBL overflows. This recover state
48 * will be enabled every time a SBL overflow occur. As the output buffer size
49 * isn't big, it's possible to have an extra size able to fit many recover
50 * buffers making it extreamily unlikely to have an access to invalid memory
51 * region.
53 #define NUM_H3A_RECOVER_BUFS 10
56 * HACK: Because of HW issues the generic layer sometimes need to have
57 * different behaviour for different statistic modules.
59 #define IS_H3A_AF(stat) ((stat) == &(stat)->isp->isp_af)
60 #define IS_H3A_AEWB(stat) ((stat) == &(stat)->isp->isp_aewb)
61 #define IS_H3A(stat) (IS_H3A_AF(stat) || IS_H3A_AEWB(stat))
63 static void __isp_stat_buf_sync_magic(struct ispstat *stat,
64 struct ispstat_buffer *buf,
65 u32 buf_size, enum dma_data_direction dir,
66 void (*dma_sync)(struct device *,
67 dma_addr_t, unsigned long, size_t,
68 enum dma_data_direction))
70 /* Sync the initial and final magic words. */
71 dma_sync(stat->isp->dev, buf->dma_addr, 0, MAGIC_SIZE, dir);
72 dma_sync(stat->isp->dev, buf->dma_addr + (buf_size & PAGE_MASK),
73 buf_size & ~PAGE_MASK, MAGIC_SIZE, dir);
76 static void isp_stat_buf_sync_magic_for_device(struct ispstat *stat,
77 struct ispstat_buffer *buf,
78 u32 buf_size,
79 enum dma_data_direction dir)
81 if (ISP_STAT_USES_DMAENGINE(stat))
82 return;
84 __isp_stat_buf_sync_magic(stat, buf, buf_size, dir,
85 dma_sync_single_range_for_device);
88 static void isp_stat_buf_sync_magic_for_cpu(struct ispstat *stat,
89 struct ispstat_buffer *buf,
90 u32 buf_size,
91 enum dma_data_direction dir)
93 if (ISP_STAT_USES_DMAENGINE(stat))
94 return;
96 __isp_stat_buf_sync_magic(stat, buf, buf_size, dir,
97 dma_sync_single_range_for_cpu);
100 static int isp_stat_buf_check_magic(struct ispstat *stat,
101 struct ispstat_buffer *buf)
103 const u32 buf_size = IS_H3A_AF(stat) ?
104 buf->buf_size + AF_EXTRA_DATA : buf->buf_size;
105 u8 *w;
106 u8 *end;
107 int ret = -EINVAL;
109 isp_stat_buf_sync_magic_for_cpu(stat, buf, buf_size, DMA_FROM_DEVICE);
111 /* Checking initial magic numbers. They shouldn't be here anymore. */
112 for (w = buf->virt_addr, end = w + MAGIC_SIZE; w < end; w++)
113 if (likely(*w != MAGIC_NUM))
114 ret = 0;
116 if (ret) {
117 dev_dbg(stat->isp->dev,
118 "%s: beginning magic check does not match.\n",
119 stat->subdev.name);
120 return ret;
123 /* Checking magic numbers at the end. They must be still here. */
124 for (w = buf->virt_addr + buf_size, end = w + MAGIC_SIZE;
125 w < end; w++) {
126 if (unlikely(*w != MAGIC_NUM)) {
127 dev_dbg(stat->isp->dev,
128 "%s: ending magic check does not match.\n",
129 stat->subdev.name);
130 return -EINVAL;
134 isp_stat_buf_sync_magic_for_device(stat, buf, buf_size,
135 DMA_FROM_DEVICE);
137 return 0;
140 static void isp_stat_buf_insert_magic(struct ispstat *stat,
141 struct ispstat_buffer *buf)
143 const u32 buf_size = IS_H3A_AF(stat) ?
144 stat->buf_size + AF_EXTRA_DATA : stat->buf_size;
146 isp_stat_buf_sync_magic_for_cpu(stat, buf, buf_size, DMA_FROM_DEVICE);
149 * Inserting MAGIC_NUM at the beginning and end of the buffer.
150 * buf->buf_size is set only after the buffer is queued. For now the
151 * right buf_size for the current configuration is pointed by
152 * stat->buf_size.
154 memset(buf->virt_addr, MAGIC_NUM, MAGIC_SIZE);
155 memset(buf->virt_addr + buf_size, MAGIC_NUM, MAGIC_SIZE);
157 isp_stat_buf_sync_magic_for_device(stat, buf, buf_size,
158 DMA_BIDIRECTIONAL);
161 static void isp_stat_buf_sync_for_device(struct ispstat *stat,
162 struct ispstat_buffer *buf)
164 if (ISP_STAT_USES_DMAENGINE(stat))
165 return;
167 dma_sync_sg_for_device(stat->isp->dev, buf->sgt.sgl,
168 buf->sgt.nents, DMA_FROM_DEVICE);
171 static void isp_stat_buf_sync_for_cpu(struct ispstat *stat,
172 struct ispstat_buffer *buf)
174 if (ISP_STAT_USES_DMAENGINE(stat))
175 return;
177 dma_sync_sg_for_cpu(stat->isp->dev, buf->sgt.sgl,
178 buf->sgt.nents, DMA_FROM_DEVICE);
181 static void isp_stat_buf_clear(struct ispstat *stat)
183 int i;
185 for (i = 0; i < STAT_MAX_BUFS; i++)
186 stat->buf[i].empty = 1;
189 static struct ispstat_buffer *
190 __isp_stat_buf_find(struct ispstat *stat, int look_empty)
192 struct ispstat_buffer *found = NULL;
193 int i;
195 for (i = 0; i < STAT_MAX_BUFS; i++) {
196 struct ispstat_buffer *curr = &stat->buf[i];
199 * Don't select the buffer which is being copied to
200 * userspace or used by the module.
202 if (curr == stat->locked_buf || curr == stat->active_buf)
203 continue;
205 /* Don't select uninitialised buffers if it's not required */
206 if (!look_empty && curr->empty)
207 continue;
209 /* Pick uninitialised buffer over anything else if look_empty */
210 if (curr->empty) {
211 found = curr;
212 break;
215 /* Choose the oldest buffer */
216 if (!found ||
217 (s32)curr->frame_number - (s32)found->frame_number < 0)
218 found = curr;
221 return found;
224 static inline struct ispstat_buffer *
225 isp_stat_buf_find_oldest(struct ispstat *stat)
227 return __isp_stat_buf_find(stat, 0);
230 static inline struct ispstat_buffer *
231 isp_stat_buf_find_oldest_or_empty(struct ispstat *stat)
233 return __isp_stat_buf_find(stat, 1);
236 static int isp_stat_buf_queue(struct ispstat *stat)
238 if (!stat->active_buf)
239 return STAT_NO_BUF;
241 ktime_get_ts64(&stat->active_buf->ts);
243 stat->active_buf->buf_size = stat->buf_size;
244 if (isp_stat_buf_check_magic(stat, stat->active_buf)) {
245 dev_dbg(stat->isp->dev, "%s: data wasn't properly written.\n",
246 stat->subdev.name);
247 return STAT_NO_BUF;
249 stat->active_buf->config_counter = stat->config_counter;
250 stat->active_buf->frame_number = stat->frame_number;
251 stat->active_buf->empty = 0;
252 stat->active_buf = NULL;
254 return STAT_BUF_DONE;
257 /* Get next free buffer to write the statistics to and mark it active. */
258 static void isp_stat_buf_next(struct ispstat *stat)
260 if (unlikely(stat->active_buf))
261 /* Overwriting unused active buffer */
262 dev_dbg(stat->isp->dev,
263 "%s: new buffer requested without queuing active one.\n",
264 stat->subdev.name);
265 else
266 stat->active_buf = isp_stat_buf_find_oldest_or_empty(stat);
269 static void isp_stat_buf_release(struct ispstat *stat)
271 unsigned long flags;
273 isp_stat_buf_sync_for_device(stat, stat->locked_buf);
274 spin_lock_irqsave(&stat->isp->stat_lock, flags);
275 stat->locked_buf = NULL;
276 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
279 /* Get buffer to userspace. */
280 static struct ispstat_buffer *isp_stat_buf_get(struct ispstat *stat,
281 struct omap3isp_stat_data *data)
283 int rval = 0;
284 unsigned long flags;
285 struct ispstat_buffer *buf;
287 spin_lock_irqsave(&stat->isp->stat_lock, flags);
289 while (1) {
290 buf = isp_stat_buf_find_oldest(stat);
291 if (!buf) {
292 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
293 dev_dbg(stat->isp->dev, "%s: cannot find a buffer.\n",
294 stat->subdev.name);
295 return ERR_PTR(-EBUSY);
297 if (isp_stat_buf_check_magic(stat, buf)) {
298 dev_dbg(stat->isp->dev,
299 "%s: current buffer has corrupted data\n.",
300 stat->subdev.name);
301 /* Mark empty because it doesn't have valid data. */
302 buf->empty = 1;
303 } else {
304 /* Buffer isn't corrupted. */
305 break;
309 stat->locked_buf = buf;
311 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
313 if (buf->buf_size > data->buf_size) {
314 dev_warn(stat->isp->dev,
315 "%s: userspace's buffer size is not enough.\n",
316 stat->subdev.name);
317 isp_stat_buf_release(stat);
318 return ERR_PTR(-EINVAL);
321 isp_stat_buf_sync_for_cpu(stat, buf);
323 rval = copy_to_user(data->buf,
324 buf->virt_addr,
325 buf->buf_size);
327 if (rval) {
328 dev_info(stat->isp->dev,
329 "%s: failed copying %d bytes of stat data\n",
330 stat->subdev.name, rval);
331 buf = ERR_PTR(-EFAULT);
332 isp_stat_buf_release(stat);
335 return buf;
338 static void isp_stat_bufs_free(struct ispstat *stat)
340 struct device *dev = ISP_STAT_USES_DMAENGINE(stat)
341 ? NULL : stat->isp->dev;
342 unsigned int i;
344 for (i = 0; i < STAT_MAX_BUFS; i++) {
345 struct ispstat_buffer *buf = &stat->buf[i];
347 if (!buf->virt_addr)
348 continue;
350 sg_free_table(&buf->sgt);
352 dma_free_coherent(dev, stat->buf_alloc_size, buf->virt_addr,
353 buf->dma_addr);
355 buf->dma_addr = 0;
356 buf->virt_addr = NULL;
357 buf->empty = 1;
360 dev_dbg(stat->isp->dev, "%s: all buffers were freed.\n",
361 stat->subdev.name);
363 stat->buf_alloc_size = 0;
364 stat->active_buf = NULL;
367 static int isp_stat_bufs_alloc_one(struct device *dev,
368 struct ispstat_buffer *buf,
369 unsigned int size)
371 int ret;
373 buf->virt_addr = dma_alloc_coherent(dev, size, &buf->dma_addr,
374 GFP_KERNEL);
375 if (!buf->virt_addr)
376 return -ENOMEM;
378 ret = dma_get_sgtable(dev, &buf->sgt, buf->virt_addr, buf->dma_addr,
379 size);
380 if (ret < 0) {
381 dma_free_coherent(dev, size, buf->virt_addr, buf->dma_addr);
382 buf->virt_addr = NULL;
383 buf->dma_addr = 0;
384 return ret;
387 return 0;
391 * The device passed to the DMA API depends on whether the statistics block uses
392 * ISP DMA, external DMA or PIO to transfer data.
394 * The first case (for the AEWB and AF engines) passes the ISP device, resulting
395 * in the DMA buffers being mapped through the ISP IOMMU.
397 * The second case (for the histogram engine) should pass the DMA engine device.
398 * As that device isn't accessible through the OMAP DMA engine API the driver
399 * passes NULL instead, resulting in the buffers being mapped directly as
400 * physical pages.
402 * The third case (for the histogram engine) doesn't require any mapping. The
403 * buffers could be allocated with kmalloc/vmalloc, but we still use
404 * dma_alloc_coherent() for consistency purpose.
406 static int isp_stat_bufs_alloc(struct ispstat *stat, u32 size)
408 struct device *dev = ISP_STAT_USES_DMAENGINE(stat)
409 ? NULL : stat->isp->dev;
410 unsigned long flags;
411 unsigned int i;
413 spin_lock_irqsave(&stat->isp->stat_lock, flags);
415 BUG_ON(stat->locked_buf != NULL);
417 /* Are the old buffers big enough? */
418 if (stat->buf_alloc_size >= size) {
419 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
420 return 0;
423 if (stat->state != ISPSTAT_DISABLED || stat->buf_processing) {
424 dev_info(stat->isp->dev,
425 "%s: trying to allocate memory when busy\n",
426 stat->subdev.name);
427 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
428 return -EBUSY;
431 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
433 isp_stat_bufs_free(stat);
435 stat->buf_alloc_size = size;
437 for (i = 0; i < STAT_MAX_BUFS; i++) {
438 struct ispstat_buffer *buf = &stat->buf[i];
439 int ret;
441 ret = isp_stat_bufs_alloc_one(dev, buf, size);
442 if (ret < 0) {
443 dev_err(stat->isp->dev,
444 "%s: Failed to allocate DMA buffer %u\n",
445 stat->subdev.name, i);
446 isp_stat_bufs_free(stat);
447 return ret;
450 buf->empty = 1;
452 dev_dbg(stat->isp->dev,
453 "%s: buffer[%u] allocated. dma=%pad virt=%p",
454 stat->subdev.name, i, &buf->dma_addr, buf->virt_addr);
457 return 0;
460 static void isp_stat_queue_event(struct ispstat *stat, int err)
462 struct video_device *vdev = stat->subdev.devnode;
463 struct v4l2_event event;
464 struct omap3isp_stat_event_status *status = (void *)event.u.data;
466 memset(&event, 0, sizeof(event));
467 if (!err) {
468 status->frame_number = stat->frame_number;
469 status->config_counter = stat->config_counter;
470 } else {
471 status->buf_err = 1;
473 event.type = stat->event_type;
474 v4l2_event_queue(vdev, &event);
479 * omap3isp_stat_request_statistics - Request statistics.
480 * @data: Pointer to return statistics data.
482 * Returns 0 if successful.
484 int omap3isp_stat_request_statistics(struct ispstat *stat,
485 struct omap3isp_stat_data *data)
487 struct ispstat_buffer *buf;
489 if (stat->state != ISPSTAT_ENABLED) {
490 dev_dbg(stat->isp->dev, "%s: engine not enabled.\n",
491 stat->subdev.name);
492 return -EINVAL;
495 mutex_lock(&stat->ioctl_lock);
496 buf = isp_stat_buf_get(stat, data);
497 if (IS_ERR(buf)) {
498 mutex_unlock(&stat->ioctl_lock);
499 return PTR_ERR(buf);
502 data->ts.tv_sec = buf->ts.tv_sec;
503 data->ts.tv_usec = buf->ts.tv_nsec / NSEC_PER_USEC;
504 data->config_counter = buf->config_counter;
505 data->frame_number = buf->frame_number;
506 data->buf_size = buf->buf_size;
508 buf->empty = 1;
509 isp_stat_buf_release(stat);
510 mutex_unlock(&stat->ioctl_lock);
512 return 0;
515 int omap3isp_stat_request_statistics_time32(struct ispstat *stat,
516 struct omap3isp_stat_data_time32 *data)
518 struct omap3isp_stat_data data64;
519 int ret;
521 ret = omap3isp_stat_request_statistics(stat, &data64);
522 if (ret)
523 return ret;
525 data->ts.tv_sec = data64.ts.tv_sec;
526 data->ts.tv_usec = data64.ts.tv_usec;
527 memcpy(&data->buf, &data64.buf, sizeof(*data) - sizeof(data->ts));
529 return 0;
533 * omap3isp_stat_config - Receives new statistic engine configuration.
534 * @new_conf: Pointer to config structure.
536 * Returns 0 if successful, -EINVAL if new_conf pointer is NULL, -ENOMEM if
537 * was unable to allocate memory for the buffer, or other errors if parameters
538 * are invalid.
540 int omap3isp_stat_config(struct ispstat *stat, void *new_conf)
542 int ret;
543 unsigned long irqflags;
544 struct ispstat_generic_config *user_cfg = new_conf;
545 u32 buf_size = user_cfg->buf_size;
547 mutex_lock(&stat->ioctl_lock);
549 dev_dbg(stat->isp->dev,
550 "%s: configuring module with buffer size=0x%08lx\n",
551 stat->subdev.name, (unsigned long)buf_size);
553 ret = stat->ops->validate_params(stat, new_conf);
554 if (ret) {
555 mutex_unlock(&stat->ioctl_lock);
556 dev_dbg(stat->isp->dev, "%s: configuration values are invalid.\n",
557 stat->subdev.name);
558 return ret;
561 if (buf_size != user_cfg->buf_size)
562 dev_dbg(stat->isp->dev,
563 "%s: driver has corrected buffer size request to 0x%08lx\n",
564 stat->subdev.name,
565 (unsigned long)user_cfg->buf_size);
568 * Hack: H3A modules may need a doubled buffer size to avoid access
569 * to a invalid memory address after a SBL overflow.
570 * The buffer size is always PAGE_ALIGNED.
571 * Hack 2: MAGIC_SIZE is added to buf_size so a magic word can be
572 * inserted at the end to data integrity check purpose.
573 * Hack 3: AF module writes one paxel data more than it should, so
574 * the buffer allocation must consider it to avoid invalid memory
575 * access.
576 * Hack 4: H3A need to allocate extra space for the recover state.
578 if (IS_H3A(stat)) {
579 buf_size = user_cfg->buf_size * 2 + MAGIC_SIZE;
580 if (IS_H3A_AF(stat))
582 * Adding one extra paxel data size for each recover
583 * buffer + 2 regular ones.
585 buf_size += AF_EXTRA_DATA * (NUM_H3A_RECOVER_BUFS + 2);
586 if (stat->recover_priv) {
587 struct ispstat_generic_config *recover_cfg =
588 stat->recover_priv;
589 buf_size += recover_cfg->buf_size *
590 NUM_H3A_RECOVER_BUFS;
592 buf_size = PAGE_ALIGN(buf_size);
593 } else { /* Histogram */
594 buf_size = PAGE_ALIGN(user_cfg->buf_size + MAGIC_SIZE);
597 ret = isp_stat_bufs_alloc(stat, buf_size);
598 if (ret) {
599 mutex_unlock(&stat->ioctl_lock);
600 return ret;
603 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
604 stat->ops->set_params(stat, new_conf);
605 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
608 * Returning the right future config_counter for this setup, so
609 * userspace can *know* when it has been applied.
611 user_cfg->config_counter = stat->config_counter + stat->inc_config;
613 /* Module has a valid configuration. */
614 stat->configured = 1;
615 dev_dbg(stat->isp->dev,
616 "%s: module has been successfully configured.\n",
617 stat->subdev.name);
619 mutex_unlock(&stat->ioctl_lock);
621 return 0;
625 * isp_stat_buf_process - Process statistic buffers.
626 * @buf_state: points out if buffer is ready to be processed. It's necessary
627 * because histogram needs to copy the data from internal memory
628 * before be able to process the buffer.
630 static int isp_stat_buf_process(struct ispstat *stat, int buf_state)
632 int ret = STAT_NO_BUF;
634 if (!atomic_add_unless(&stat->buf_err, -1, 0) &&
635 buf_state == STAT_BUF_DONE && stat->state == ISPSTAT_ENABLED) {
636 ret = isp_stat_buf_queue(stat);
637 isp_stat_buf_next(stat);
640 return ret;
643 int omap3isp_stat_pcr_busy(struct ispstat *stat)
645 return stat->ops->busy(stat);
648 int omap3isp_stat_busy(struct ispstat *stat)
650 return omap3isp_stat_pcr_busy(stat) | stat->buf_processing |
651 (stat->state != ISPSTAT_DISABLED);
655 * isp_stat_pcr_enable - Disables/Enables statistic engines.
656 * @pcr_enable: 0/1 - Disables/Enables the engine.
658 * Must be called from ISP driver when the module is idle and synchronized
659 * with CCDC.
661 static void isp_stat_pcr_enable(struct ispstat *stat, u8 pcr_enable)
663 if ((stat->state != ISPSTAT_ENABLING &&
664 stat->state != ISPSTAT_ENABLED) && pcr_enable)
665 /* Userspace has disabled the module. Aborting. */
666 return;
668 stat->ops->enable(stat, pcr_enable);
669 if (stat->state == ISPSTAT_DISABLING && !pcr_enable)
670 stat->state = ISPSTAT_DISABLED;
671 else if (stat->state == ISPSTAT_ENABLING && pcr_enable)
672 stat->state = ISPSTAT_ENABLED;
675 void omap3isp_stat_suspend(struct ispstat *stat)
677 unsigned long flags;
679 spin_lock_irqsave(&stat->isp->stat_lock, flags);
681 if (stat->state != ISPSTAT_DISABLED)
682 stat->ops->enable(stat, 0);
683 if (stat->state == ISPSTAT_ENABLED)
684 stat->state = ISPSTAT_SUSPENDED;
686 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
689 void omap3isp_stat_resume(struct ispstat *stat)
691 /* Module will be re-enabled with its pipeline */
692 if (stat->state == ISPSTAT_SUSPENDED)
693 stat->state = ISPSTAT_ENABLING;
696 static void isp_stat_try_enable(struct ispstat *stat)
698 unsigned long irqflags;
700 if (stat->priv == NULL)
701 /* driver wasn't initialised */
702 return;
704 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
705 if (stat->state == ISPSTAT_ENABLING && !stat->buf_processing &&
706 stat->buf_alloc_size) {
708 * Userspace's requested to enable the engine but it wasn't yet.
709 * Let's do that now.
711 stat->update = 1;
712 isp_stat_buf_next(stat);
713 stat->ops->setup_regs(stat, stat->priv);
714 isp_stat_buf_insert_magic(stat, stat->active_buf);
717 * H3A module has some hw issues which forces the driver to
718 * ignore next buffers even if it was disabled in the meantime.
719 * On the other hand, Histogram shouldn't ignore buffers anymore
720 * if it's being enabled.
722 if (!IS_H3A(stat))
723 atomic_set(&stat->buf_err, 0);
725 isp_stat_pcr_enable(stat, 1);
726 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
727 dev_dbg(stat->isp->dev, "%s: module is enabled.\n",
728 stat->subdev.name);
729 } else {
730 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
734 void omap3isp_stat_isr_frame_sync(struct ispstat *stat)
736 isp_stat_try_enable(stat);
739 void omap3isp_stat_sbl_overflow(struct ispstat *stat)
741 unsigned long irqflags;
743 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
745 * Due to a H3A hw issue which prevents the next buffer to start from
746 * the correct memory address, 2 buffers must be ignored.
748 atomic_set(&stat->buf_err, 2);
751 * If more than one SBL overflow happen in a row, H3A module may access
752 * invalid memory region.
753 * stat->sbl_ovl_recover is set to tell to the driver to temporarily use
754 * a soft configuration which helps to avoid consecutive overflows.
756 if (stat->recover_priv)
757 stat->sbl_ovl_recover = 1;
758 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
762 * omap3isp_stat_enable - Disable/Enable statistic engine as soon as possible
763 * @enable: 0/1 - Disables/Enables the engine.
765 * Client should configure all the module registers before this.
766 * This function can be called from a userspace request.
768 int omap3isp_stat_enable(struct ispstat *stat, u8 enable)
770 unsigned long irqflags;
772 dev_dbg(stat->isp->dev, "%s: user wants to %s module.\n",
773 stat->subdev.name, enable ? "enable" : "disable");
775 /* Prevent enabling while configuring */
776 mutex_lock(&stat->ioctl_lock);
778 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
780 if (!stat->configured && enable) {
781 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
782 mutex_unlock(&stat->ioctl_lock);
783 dev_dbg(stat->isp->dev,
784 "%s: cannot enable module as it's never been successfully configured so far.\n",
785 stat->subdev.name);
786 return -EINVAL;
789 if (enable) {
790 if (stat->state == ISPSTAT_DISABLING)
791 /* Previous disabling request wasn't done yet */
792 stat->state = ISPSTAT_ENABLED;
793 else if (stat->state == ISPSTAT_DISABLED)
794 /* Module is now being enabled */
795 stat->state = ISPSTAT_ENABLING;
796 } else {
797 if (stat->state == ISPSTAT_ENABLING) {
798 /* Previous enabling request wasn't done yet */
799 stat->state = ISPSTAT_DISABLED;
800 } else if (stat->state == ISPSTAT_ENABLED) {
801 /* Module is now being disabled */
802 stat->state = ISPSTAT_DISABLING;
803 isp_stat_buf_clear(stat);
807 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
808 mutex_unlock(&stat->ioctl_lock);
810 return 0;
813 int omap3isp_stat_s_stream(struct v4l2_subdev *subdev, int enable)
815 struct ispstat *stat = v4l2_get_subdevdata(subdev);
817 if (enable) {
819 * Only set enable PCR bit if the module was previously
820 * enabled through ioctl.
822 isp_stat_try_enable(stat);
823 } else {
824 unsigned long flags;
825 /* Disable PCR bit and config enable field */
826 omap3isp_stat_enable(stat, 0);
827 spin_lock_irqsave(&stat->isp->stat_lock, flags);
828 stat->ops->enable(stat, 0);
829 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
832 * If module isn't busy, a new interrupt may come or not to
833 * set the state to DISABLED. As Histogram needs to read its
834 * internal memory to clear it, let interrupt handler
835 * responsible of changing state to DISABLED. If the last
836 * interrupt is coming, it's still safe as the handler will
837 * ignore the second time when state is already set to DISABLED.
838 * It's necessary to synchronize Histogram with streamoff, once
839 * the module may be considered idle before last SDMA transfer
840 * starts if we return here.
842 if (!omap3isp_stat_pcr_busy(stat))
843 omap3isp_stat_isr(stat);
845 dev_dbg(stat->isp->dev, "%s: module is being disabled\n",
846 stat->subdev.name);
849 return 0;
853 * __stat_isr - Interrupt handler for statistic drivers
855 static void __stat_isr(struct ispstat *stat, int from_dma)
857 int ret = STAT_BUF_DONE;
858 int buf_processing;
859 unsigned long irqflags;
860 struct isp_pipeline *pipe;
863 * stat->buf_processing must be set before disable module. It's
864 * necessary to not inform too early the buffers aren't busy in case
865 * of SDMA is going to be used.
867 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
868 if (stat->state == ISPSTAT_DISABLED) {
869 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
870 return;
872 buf_processing = stat->buf_processing;
873 stat->buf_processing = 1;
874 stat->ops->enable(stat, 0);
876 if (buf_processing && !from_dma) {
877 if (stat->state == ISPSTAT_ENABLED) {
878 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
879 dev_err(stat->isp->dev,
880 "%s: interrupt occurred when module was still processing a buffer.\n",
881 stat->subdev.name);
882 ret = STAT_NO_BUF;
883 goto out;
884 } else {
886 * Interrupt handler was called from streamoff when
887 * the module wasn't busy anymore to ensure it is being
888 * disabled after process last buffer. If such buffer
889 * processing has already started, no need to do
890 * anything else.
892 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
893 return;
896 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
898 /* If it's busy we can't process this buffer anymore */
899 if (!omap3isp_stat_pcr_busy(stat)) {
900 if (!from_dma && stat->ops->buf_process)
901 /* Module still need to copy data to buffer. */
902 ret = stat->ops->buf_process(stat);
903 if (ret == STAT_BUF_WAITING_DMA)
904 /* Buffer is not ready yet */
905 return;
907 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
910 * Histogram needs to read its internal memory to clear it
911 * before be disabled. For that reason, common statistic layer
912 * can return only after call stat's buf_process() operator.
914 if (stat->state == ISPSTAT_DISABLING) {
915 stat->state = ISPSTAT_DISABLED;
916 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
917 stat->buf_processing = 0;
918 return;
920 pipe = to_isp_pipeline(&stat->subdev.entity);
921 stat->frame_number = atomic_read(&pipe->frame_number);
924 * Before this point, 'ret' stores the buffer's status if it's
925 * ready to be processed. Afterwards, it holds the status if
926 * it was processed successfully.
928 ret = isp_stat_buf_process(stat, ret);
930 if (likely(!stat->sbl_ovl_recover)) {
931 stat->ops->setup_regs(stat, stat->priv);
932 } else {
934 * Using recover config to increase the chance to have
935 * a good buffer processing and make the H3A module to
936 * go back to a valid state.
938 stat->update = 1;
939 stat->ops->setup_regs(stat, stat->recover_priv);
940 stat->sbl_ovl_recover = 0;
943 * Set 'update' in case of the module needs to use
944 * regular configuration after next buffer.
946 stat->update = 1;
949 isp_stat_buf_insert_magic(stat, stat->active_buf);
952 * Hack: H3A modules may access invalid memory address or send
953 * corrupted data to userspace if more than 1 SBL overflow
954 * happens in a row without re-writing its buffer's start memory
955 * address in the meantime. Such situation is avoided if the
956 * module is not immediately re-enabled when the ISR misses the
957 * timing to process the buffer and to setup the registers.
958 * Because of that, pcr_enable(1) was moved to inside this 'if'
959 * block. But the next interruption will still happen as during
960 * pcr_enable(0) the module was busy.
962 isp_stat_pcr_enable(stat, 1);
963 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
964 } else {
966 * If a SBL overflow occurs and the H3A driver misses the timing
967 * to process the buffer, stat->buf_err is set and won't be
968 * cleared now. So the next buffer will be correctly ignored.
969 * It's necessary due to a hw issue which makes the next H3A
970 * buffer to start from the memory address where the previous
971 * one stopped, instead of start where it was configured to.
972 * Do not "stat->buf_err = 0" here.
975 if (stat->ops->buf_process)
977 * Driver may need to erase current data prior to
978 * process a new buffer. If it misses the timing, the
979 * next buffer might be wrong. So should be ignored.
980 * It happens only for Histogram.
982 atomic_set(&stat->buf_err, 1);
984 ret = STAT_NO_BUF;
985 dev_dbg(stat->isp->dev,
986 "%s: cannot process buffer, device is busy.\n",
987 stat->subdev.name);
990 out:
991 stat->buf_processing = 0;
992 isp_stat_queue_event(stat, ret != STAT_BUF_DONE);
995 void omap3isp_stat_isr(struct ispstat *stat)
997 __stat_isr(stat, 0);
1000 void omap3isp_stat_dma_isr(struct ispstat *stat)
1002 __stat_isr(stat, 1);
1005 int omap3isp_stat_subscribe_event(struct v4l2_subdev *subdev,
1006 struct v4l2_fh *fh,
1007 struct v4l2_event_subscription *sub)
1009 struct ispstat *stat = v4l2_get_subdevdata(subdev);
1011 if (sub->type != stat->event_type)
1012 return -EINVAL;
1014 return v4l2_event_subscribe(fh, sub, STAT_NEVENTS, NULL);
1017 int omap3isp_stat_unsubscribe_event(struct v4l2_subdev *subdev,
1018 struct v4l2_fh *fh,
1019 struct v4l2_event_subscription *sub)
1021 return v4l2_event_unsubscribe(fh, sub);
1024 void omap3isp_stat_unregister_entities(struct ispstat *stat)
1026 v4l2_device_unregister_subdev(&stat->subdev);
1029 int omap3isp_stat_register_entities(struct ispstat *stat,
1030 struct v4l2_device *vdev)
1032 return v4l2_device_register_subdev(vdev, &stat->subdev);
1035 static int isp_stat_init_entities(struct ispstat *stat, const char *name,
1036 const struct v4l2_subdev_ops *sd_ops)
1038 struct v4l2_subdev *subdev = &stat->subdev;
1039 struct media_entity *me = &subdev->entity;
1041 v4l2_subdev_init(subdev, sd_ops);
1042 snprintf(subdev->name, V4L2_SUBDEV_NAME_SIZE, "OMAP3 ISP %s", name);
1043 subdev->grp_id = 1 << 16; /* group ID for isp subdevs */
1044 subdev->flags |= V4L2_SUBDEV_FL_HAS_EVENTS | V4L2_SUBDEV_FL_HAS_DEVNODE;
1045 v4l2_set_subdevdata(subdev, stat);
1047 stat->pad.flags = MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
1048 me->ops = NULL;
1050 return media_entity_pads_init(me, 1, &stat->pad);
1053 int omap3isp_stat_init(struct ispstat *stat, const char *name,
1054 const struct v4l2_subdev_ops *sd_ops)
1056 int ret;
1058 stat->buf = kcalloc(STAT_MAX_BUFS, sizeof(*stat->buf), GFP_KERNEL);
1059 if (!stat->buf)
1060 return -ENOMEM;
1062 isp_stat_buf_clear(stat);
1063 mutex_init(&stat->ioctl_lock);
1064 atomic_set(&stat->buf_err, 0);
1066 ret = isp_stat_init_entities(stat, name, sd_ops);
1067 if (ret < 0) {
1068 mutex_destroy(&stat->ioctl_lock);
1069 kfree(stat->buf);
1072 return ret;
1075 void omap3isp_stat_cleanup(struct ispstat *stat)
1077 media_entity_cleanup(&stat->subdev.entity);
1078 mutex_destroy(&stat->ioctl_lock);
1079 isp_stat_bufs_free(stat);
1080 kfree(stat->buf);