blob: a75407c3a726217981d40193b89919332a7919bf [file] [log] [blame]
David Cohen68e342b2011-02-12 18:05:06 -03001/*
2 * ispstat.c
3 *
4 * TI OMAP3 ISP - Statistics core
5 *
6 * Copyright (C) 2010 Nokia Corporation
7 * Copyright (C) 2009 Texas Instruments, Inc
8 *
9 * Contacts: David Cohen <dacohen@gmail.com>
10 * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
11 * Sakari Ailus <sakari.ailus@iki.fi>
12 *
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.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25 * 02110-1301 USA
26 */
27
28#include <linux/dma-mapping.h>
Tony Lindgrenc8d35c82012-11-02 12:24:03 -070029#include <linux/omap-iommu.h>
David Cohen68e342b2011-02-12 18:05:06 -030030#include <linux/slab.h>
31#include <linux/uaccess.h>
32
33#include "isp.h"
34
35#define IS_COHERENT_BUF(stat) ((stat)->dma_ch >= 0)
36
37/*
38 * MAGIC_SIZE must always be the greatest common divisor of
39 * AEWB_PACKET_SIZE and AF_PAXEL_SIZE.
40 */
41#define MAGIC_SIZE 16
42#define MAGIC_NUM 0x55
43
44/* HACK: AF module seems to be writing one more paxel data than it should. */
45#define AF_EXTRA_DATA OMAP3ISP_AF_PAXEL_SIZE
46
47/*
48 * HACK: H3A modules go to an invalid state after have a SBL overflow. It makes
49 * the next buffer to start to be written in the same point where the overflow
50 * occurred instead of the configured address. The only known way to make it to
51 * go back to a valid state is having a valid buffer processing. Of course it
52 * requires at least a doubled buffer size to avoid an access to invalid memory
53 * region. But it does not fix everything. It may happen more than one
54 * consecutive SBL overflows. In that case, it might be unpredictable how many
55 * buffers the allocated memory should fit. For that case, a recover
56 * configuration was created. It produces the minimum buffer size for each H3A
57 * module and decrease the change for more SBL overflows. This recover state
58 * will be enabled every time a SBL overflow occur. As the output buffer size
59 * isn't big, it's possible to have an extra size able to fit many recover
60 * buffers making it extreamily unlikely to have an access to invalid memory
61 * region.
62 */
63#define NUM_H3A_RECOVER_BUFS 10
64
65/*
66 * HACK: Because of HW issues the generic layer sometimes need to have
67 * different behaviour for different statistic modules.
68 */
69#define IS_H3A_AF(stat) ((stat) == &(stat)->isp->isp_af)
70#define IS_H3A_AEWB(stat) ((stat) == &(stat)->isp->isp_aewb)
71#define IS_H3A(stat) (IS_H3A_AF(stat) || IS_H3A_AEWB(stat))
72
73static void __isp_stat_buf_sync_magic(struct ispstat *stat,
74 struct ispstat_buffer *buf,
75 u32 buf_size, enum dma_data_direction dir,
76 void (*dma_sync)(struct device *,
77 dma_addr_t, unsigned long, size_t,
78 enum dma_data_direction))
79{
80 struct device *dev = stat->isp->dev;
81 struct page *pg;
82 dma_addr_t dma_addr;
83 u32 offset;
84
85 /* Initial magic words */
86 pg = vmalloc_to_page(buf->virt_addr);
87 dma_addr = pfn_to_dma(dev, page_to_pfn(pg));
88 dma_sync(dev, dma_addr, 0, MAGIC_SIZE, dir);
89
90 /* Final magic words */
91 pg = vmalloc_to_page(buf->virt_addr + buf_size);
92 dma_addr = pfn_to_dma(dev, page_to_pfn(pg));
93 offset = ((u32)buf->virt_addr + buf_size) & ~PAGE_MASK;
94 dma_sync(dev, dma_addr, offset, MAGIC_SIZE, dir);
95}
96
97static void isp_stat_buf_sync_magic_for_device(struct ispstat *stat,
98 struct ispstat_buffer *buf,
99 u32 buf_size,
100 enum dma_data_direction dir)
101{
102 if (IS_COHERENT_BUF(stat))
103 return;
104
105 __isp_stat_buf_sync_magic(stat, buf, buf_size, dir,
106 dma_sync_single_range_for_device);
107}
108
109static void isp_stat_buf_sync_magic_for_cpu(struct ispstat *stat,
110 struct ispstat_buffer *buf,
111 u32 buf_size,
112 enum dma_data_direction dir)
113{
114 if (IS_COHERENT_BUF(stat))
115 return;
116
117 __isp_stat_buf_sync_magic(stat, buf, buf_size, dir,
118 dma_sync_single_range_for_cpu);
119}
120
121static int isp_stat_buf_check_magic(struct ispstat *stat,
122 struct ispstat_buffer *buf)
123{
124 const u32 buf_size = IS_H3A_AF(stat) ?
125 buf->buf_size + AF_EXTRA_DATA : buf->buf_size;
126 u8 *w;
127 u8 *end;
128 int ret = -EINVAL;
129
130 isp_stat_buf_sync_magic_for_cpu(stat, buf, buf_size, DMA_FROM_DEVICE);
131
132 /* Checking initial magic numbers. They shouldn't be here anymore. */
133 for (w = buf->virt_addr, end = w + MAGIC_SIZE; w < end; w++)
134 if (likely(*w != MAGIC_NUM))
135 ret = 0;
136
137 if (ret) {
138 dev_dbg(stat->isp->dev, "%s: beginning magic check does not "
139 "match.\n", stat->subdev.name);
140 return ret;
141 }
142
143 /* Checking magic numbers at the end. They must be still here. */
144 for (w = buf->virt_addr + buf_size, end = w + MAGIC_SIZE;
145 w < end; w++) {
146 if (unlikely(*w != MAGIC_NUM)) {
147 dev_dbg(stat->isp->dev, "%s: endding magic check does "
148 "not match.\n", stat->subdev.name);
149 return -EINVAL;
150 }
151 }
152
153 isp_stat_buf_sync_magic_for_device(stat, buf, buf_size,
154 DMA_FROM_DEVICE);
155
156 return 0;
157}
158
159static void isp_stat_buf_insert_magic(struct ispstat *stat,
160 struct ispstat_buffer *buf)
161{
162 const u32 buf_size = IS_H3A_AF(stat) ?
163 stat->buf_size + AF_EXTRA_DATA : stat->buf_size;
164
165 isp_stat_buf_sync_magic_for_cpu(stat, buf, buf_size, DMA_FROM_DEVICE);
166
167 /*
168 * Inserting MAGIC_NUM at the beginning and end of the buffer.
169 * buf->buf_size is set only after the buffer is queued. For now the
170 * right buf_size for the current configuration is pointed by
171 * stat->buf_size.
172 */
173 memset(buf->virt_addr, MAGIC_NUM, MAGIC_SIZE);
174 memset(buf->virt_addr + buf_size, MAGIC_NUM, MAGIC_SIZE);
175
176 isp_stat_buf_sync_magic_for_device(stat, buf, buf_size,
177 DMA_BIDIRECTIONAL);
178}
179
180static void isp_stat_buf_sync_for_device(struct ispstat *stat,
181 struct ispstat_buffer *buf)
182{
183 if (IS_COHERENT_BUF(stat))
184 return;
185
186 dma_sync_sg_for_device(stat->isp->dev, buf->iovm->sgt->sgl,
187 buf->iovm->sgt->nents, DMA_FROM_DEVICE);
188}
189
190static void isp_stat_buf_sync_for_cpu(struct ispstat *stat,
191 struct ispstat_buffer *buf)
192{
193 if (IS_COHERENT_BUF(stat))
194 return;
195
196 dma_sync_sg_for_cpu(stat->isp->dev, buf->iovm->sgt->sgl,
197 buf->iovm->sgt->nents, DMA_FROM_DEVICE);
198}
199
200static void isp_stat_buf_clear(struct ispstat *stat)
201{
202 int i;
203
204 for (i = 0; i < STAT_MAX_BUFS; i++)
205 stat->buf[i].empty = 1;
206}
207
208static struct ispstat_buffer *
209__isp_stat_buf_find(struct ispstat *stat, int look_empty)
210{
211 struct ispstat_buffer *found = NULL;
212 int i;
213
214 for (i = 0; i < STAT_MAX_BUFS; i++) {
215 struct ispstat_buffer *curr = &stat->buf[i];
216
217 /*
218 * Don't select the buffer which is being copied to
219 * userspace or used by the module.
220 */
221 if (curr == stat->locked_buf || curr == stat->active_buf)
222 continue;
223
224 /* Don't select uninitialised buffers if it's not required */
225 if (!look_empty && curr->empty)
226 continue;
227
228 /* Pick uninitialised buffer over anything else if look_empty */
229 if (curr->empty) {
230 found = curr;
231 break;
232 }
233
234 /* Choose the oldest buffer */
235 if (!found ||
236 (s32)curr->frame_number - (s32)found->frame_number < 0)
237 found = curr;
238 }
239
240 return found;
241}
242
243static inline struct ispstat_buffer *
244isp_stat_buf_find_oldest(struct ispstat *stat)
245{
246 return __isp_stat_buf_find(stat, 0);
247}
248
249static inline struct ispstat_buffer *
250isp_stat_buf_find_oldest_or_empty(struct ispstat *stat)
251{
252 return __isp_stat_buf_find(stat, 1);
253}
254
255static int isp_stat_buf_queue(struct ispstat *stat)
256{
257 if (!stat->active_buf)
258 return STAT_NO_BUF;
259
Laurent Pinchart07d19e32012-09-12 22:10:40 -0300260 ktime_get_ts(&stat->active_buf->ts);
David Cohen68e342b2011-02-12 18:05:06 -0300261
262 stat->active_buf->buf_size = stat->buf_size;
263 if (isp_stat_buf_check_magic(stat, stat->active_buf)) {
264 dev_dbg(stat->isp->dev, "%s: data wasn't properly written.\n",
265 stat->subdev.name);
266 return STAT_NO_BUF;
267 }
268 stat->active_buf->config_counter = stat->config_counter;
269 stat->active_buf->frame_number = stat->frame_number;
270 stat->active_buf->empty = 0;
271 stat->active_buf = NULL;
272
273 return STAT_BUF_DONE;
274}
275
276/* Get next free buffer to write the statistics to and mark it active. */
277static void isp_stat_buf_next(struct ispstat *stat)
278{
279 if (unlikely(stat->active_buf))
280 /* Overwriting unused active buffer */
281 dev_dbg(stat->isp->dev, "%s: new buffer requested without "
282 "queuing active one.\n",
283 stat->subdev.name);
284 else
285 stat->active_buf = isp_stat_buf_find_oldest_or_empty(stat);
286}
287
288static void isp_stat_buf_release(struct ispstat *stat)
289{
290 unsigned long flags;
291
292 isp_stat_buf_sync_for_device(stat, stat->locked_buf);
293 spin_lock_irqsave(&stat->isp->stat_lock, flags);
294 stat->locked_buf = NULL;
295 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
296}
297
298/* Get buffer to userspace. */
299static struct ispstat_buffer *isp_stat_buf_get(struct ispstat *stat,
300 struct omap3isp_stat_data *data)
301{
302 int rval = 0;
303 unsigned long flags;
304 struct ispstat_buffer *buf;
305
306 spin_lock_irqsave(&stat->isp->stat_lock, flags);
307
308 while (1) {
309 buf = isp_stat_buf_find_oldest(stat);
310 if (!buf) {
311 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
312 dev_dbg(stat->isp->dev, "%s: cannot find a buffer.\n",
313 stat->subdev.name);
314 return ERR_PTR(-EBUSY);
315 }
316 if (isp_stat_buf_check_magic(stat, buf)) {
317 dev_dbg(stat->isp->dev, "%s: current buffer has "
318 "corrupted data\n.", stat->subdev.name);
319 /* Mark empty because it doesn't have valid data. */
320 buf->empty = 1;
321 } else {
322 /* Buffer isn't corrupted. */
323 break;
324 }
325 }
326
327 stat->locked_buf = buf;
328
329 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
330
331 if (buf->buf_size > data->buf_size) {
332 dev_warn(stat->isp->dev, "%s: userspace's buffer size is "
333 "not enough.\n", stat->subdev.name);
334 isp_stat_buf_release(stat);
335 return ERR_PTR(-EINVAL);
336 }
337
338 isp_stat_buf_sync_for_cpu(stat, buf);
339
340 rval = copy_to_user(data->buf,
341 buf->virt_addr,
342 buf->buf_size);
343
344 if (rval) {
345 dev_info(stat->isp->dev,
346 "%s: failed copying %d bytes of stat data\n",
347 stat->subdev.name, rval);
348 buf = ERR_PTR(-EFAULT);
349 isp_stat_buf_release(stat);
350 }
351
352 return buf;
353}
354
355static void isp_stat_bufs_free(struct ispstat *stat)
356{
357 struct isp_device *isp = stat->isp;
358 int i;
359
360 for (i = 0; i < STAT_MAX_BUFS; i++) {
361 struct ispstat_buffer *buf = &stat->buf[i];
362
363 if (!IS_COHERENT_BUF(stat)) {
364 if (IS_ERR_OR_NULL((void *)buf->iommu_addr))
365 continue;
366 if (buf->iovm)
367 dma_unmap_sg(isp->dev, buf->iovm->sgt->sgl,
368 buf->iovm->sgt->nents,
369 DMA_FROM_DEVICE);
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200370 omap_iommu_vfree(isp->domain, isp->dev,
Ohad Ben-Cohen6c32df42011-08-17 22:57:56 +0300371 buf->iommu_addr);
David Cohen68e342b2011-02-12 18:05:06 -0300372 } else {
373 if (!buf->virt_addr)
374 continue;
375 dma_free_coherent(stat->isp->dev, stat->buf_alloc_size,
376 buf->virt_addr, buf->dma_addr);
377 }
378 buf->iommu_addr = 0;
379 buf->iovm = NULL;
380 buf->dma_addr = 0;
381 buf->virt_addr = NULL;
382 buf->empty = 1;
383 }
384
385 dev_dbg(stat->isp->dev, "%s: all buffers were freed.\n",
386 stat->subdev.name);
387
388 stat->buf_alloc_size = 0;
389 stat->active_buf = NULL;
390}
391
392static int isp_stat_bufs_alloc_iommu(struct ispstat *stat, unsigned int size)
393{
394 struct isp_device *isp = stat->isp;
395 int i;
396
397 stat->buf_alloc_size = size;
398
399 for (i = 0; i < STAT_MAX_BUFS; i++) {
400 struct ispstat_buffer *buf = &stat->buf[i];
401 struct iovm_struct *iovm;
402
403 WARN_ON(buf->dma_addr);
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200404 buf->iommu_addr = omap_iommu_vmalloc(isp->domain, isp->dev, 0,
Ohad Ben-Cohenf626b522011-06-02 01:46:12 +0300405 size, IOMMU_FLAG);
David Cohen68e342b2011-02-12 18:05:06 -0300406 if (IS_ERR((void *)buf->iommu_addr)) {
407 dev_err(stat->isp->dev,
408 "%s: Can't acquire memory for "
409 "buffer %d\n", stat->subdev.name, i);
410 isp_stat_bufs_free(stat);
411 return -ENOMEM;
412 }
413
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200414 iovm = omap_find_iovm_area(isp->dev, buf->iommu_addr);
David Cohen68e342b2011-02-12 18:05:06 -0300415 if (!iovm ||
416 !dma_map_sg(isp->dev, iovm->sgt->sgl, iovm->sgt->nents,
417 DMA_FROM_DEVICE)) {
418 isp_stat_bufs_free(stat);
419 return -ENOMEM;
420 }
421 buf->iovm = iovm;
422
Ohad Ben-Cohenfabdbca2011-10-11 00:18:33 +0200423 buf->virt_addr = omap_da_to_va(stat->isp->dev,
David Cohen68e342b2011-02-12 18:05:06 -0300424 (u32)buf->iommu_addr);
425 buf->empty = 1;
426 dev_dbg(stat->isp->dev, "%s: buffer[%d] allocated."
427 "iommu_addr=0x%08lx virt_addr=0x%08lx",
428 stat->subdev.name, i, buf->iommu_addr,
429 (unsigned long)buf->virt_addr);
430 }
431
432 return 0;
433}
434
435static int isp_stat_bufs_alloc_dma(struct ispstat *stat, unsigned int size)
436{
437 int i;
438
439 stat->buf_alloc_size = size;
440
441 for (i = 0; i < STAT_MAX_BUFS; i++) {
442 struct ispstat_buffer *buf = &stat->buf[i];
443
444 WARN_ON(buf->iommu_addr);
445 buf->virt_addr = dma_alloc_coherent(stat->isp->dev, size,
446 &buf->dma_addr, GFP_KERNEL | GFP_DMA);
447
448 if (!buf->virt_addr || !buf->dma_addr) {
449 dev_info(stat->isp->dev,
450 "%s: Can't acquire memory for "
451 "DMA buffer %d\n", stat->subdev.name, i);
452 isp_stat_bufs_free(stat);
453 return -ENOMEM;
454 }
455 buf->empty = 1;
456
457 dev_dbg(stat->isp->dev, "%s: buffer[%d] allocated."
458 "dma_addr=0x%08lx virt_addr=0x%08lx\n",
459 stat->subdev.name, i, (unsigned long)buf->dma_addr,
460 (unsigned long)buf->virt_addr);
461 }
462
463 return 0;
464}
465
466static int isp_stat_bufs_alloc(struct ispstat *stat, u32 size)
467{
468 unsigned long flags;
469
470 spin_lock_irqsave(&stat->isp->stat_lock, flags);
471
472 BUG_ON(stat->locked_buf != NULL);
473
474 /* Are the old buffers big enough? */
475 if (stat->buf_alloc_size >= size) {
476 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
477 return 0;
478 }
479
480 if (stat->state != ISPSTAT_DISABLED || stat->buf_processing) {
481 dev_info(stat->isp->dev,
482 "%s: trying to allocate memory when busy\n",
483 stat->subdev.name);
484 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
485 return -EBUSY;
486 }
487
488 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
489
490 isp_stat_bufs_free(stat);
491
492 if (IS_COHERENT_BUF(stat))
493 return isp_stat_bufs_alloc_dma(stat, size);
494 else
495 return isp_stat_bufs_alloc_iommu(stat, size);
496}
497
498static void isp_stat_queue_event(struct ispstat *stat, int err)
499{
Laurent Pinchartc070e382011-11-16 10:54:02 -0300500 struct video_device *vdev = stat->subdev.devnode;
David Cohen68e342b2011-02-12 18:05:06 -0300501 struct v4l2_event event;
502 struct omap3isp_stat_event_status *status = (void *)event.u.data;
503
504 memset(&event, 0, sizeof(event));
505 if (!err) {
506 status->frame_number = stat->frame_number;
507 status->config_counter = stat->config_counter;
508 } else {
509 status->buf_err = 1;
510 }
511 event.type = stat->event_type;
512 v4l2_event_queue(vdev, &event);
513}
514
515
516/*
517 * omap3isp_stat_request_statistics - Request statistics.
518 * @data: Pointer to return statistics data.
519 *
520 * Returns 0 if successful.
521 */
522int omap3isp_stat_request_statistics(struct ispstat *stat,
523 struct omap3isp_stat_data *data)
524{
525 struct ispstat_buffer *buf;
526
527 if (stat->state != ISPSTAT_ENABLED) {
528 dev_dbg(stat->isp->dev, "%s: engine not enabled.\n",
529 stat->subdev.name);
530 return -EINVAL;
531 }
532
533 mutex_lock(&stat->ioctl_lock);
534 buf = isp_stat_buf_get(stat, data);
535 if (IS_ERR(buf)) {
536 mutex_unlock(&stat->ioctl_lock);
537 return PTR_ERR(buf);
538 }
539
Laurent Pinchart07d19e32012-09-12 22:10:40 -0300540 data->ts.tv_sec = buf->ts.tv_sec;
541 data->ts.tv_usec = buf->ts.tv_nsec / NSEC_PER_USEC;
David Cohen68e342b2011-02-12 18:05:06 -0300542 data->config_counter = buf->config_counter;
543 data->frame_number = buf->frame_number;
544 data->buf_size = buf->buf_size;
545
546 buf->empty = 1;
547 isp_stat_buf_release(stat);
548 mutex_unlock(&stat->ioctl_lock);
549
550 return 0;
551}
552
553/*
554 * omap3isp_stat_config - Receives new statistic engine configuration.
555 * @new_conf: Pointer to config structure.
556 *
557 * Returns 0 if successful, -EINVAL if new_conf pointer is NULL, -ENOMEM if
558 * was unable to allocate memory for the buffer, or other errors if parameters
559 * are invalid.
560 */
561int omap3isp_stat_config(struct ispstat *stat, void *new_conf)
562{
563 int ret;
564 unsigned long irqflags;
565 struct ispstat_generic_config *user_cfg = new_conf;
566 u32 buf_size = user_cfg->buf_size;
567
568 if (!new_conf) {
569 dev_dbg(stat->isp->dev, "%s: configuration is NULL\n",
570 stat->subdev.name);
571 return -EINVAL;
572 }
573
574 mutex_lock(&stat->ioctl_lock);
575
576 dev_dbg(stat->isp->dev, "%s: configuring module with buffer "
577 "size=0x%08lx\n", stat->subdev.name, (unsigned long)buf_size);
578
579 ret = stat->ops->validate_params(stat, new_conf);
580 if (ret) {
581 mutex_unlock(&stat->ioctl_lock);
582 dev_dbg(stat->isp->dev, "%s: configuration values are "
583 "invalid.\n", stat->subdev.name);
584 return ret;
585 }
586
587 if (buf_size != user_cfg->buf_size)
588 dev_dbg(stat->isp->dev, "%s: driver has corrected buffer size "
589 "request to 0x%08lx\n", stat->subdev.name,
590 (unsigned long)user_cfg->buf_size);
591
592 /*
593 * Hack: H3A modules may need a doubled buffer size to avoid access
594 * to a invalid memory address after a SBL overflow.
595 * The buffer size is always PAGE_ALIGNED.
596 * Hack 2: MAGIC_SIZE is added to buf_size so a magic word can be
597 * inserted at the end to data integrity check purpose.
598 * Hack 3: AF module writes one paxel data more than it should, so
599 * the buffer allocation must consider it to avoid invalid memory
600 * access.
601 * Hack 4: H3A need to allocate extra space for the recover state.
602 */
603 if (IS_H3A(stat)) {
604 buf_size = user_cfg->buf_size * 2 + MAGIC_SIZE;
605 if (IS_H3A_AF(stat))
606 /*
607 * Adding one extra paxel data size for each recover
608 * buffer + 2 regular ones.
609 */
610 buf_size += AF_EXTRA_DATA * (NUM_H3A_RECOVER_BUFS + 2);
611 if (stat->recover_priv) {
612 struct ispstat_generic_config *recover_cfg =
613 stat->recover_priv;
614 buf_size += recover_cfg->buf_size *
615 NUM_H3A_RECOVER_BUFS;
616 }
617 buf_size = PAGE_ALIGN(buf_size);
618 } else { /* Histogram */
619 buf_size = PAGE_ALIGN(user_cfg->buf_size + MAGIC_SIZE);
620 }
621
622 ret = isp_stat_bufs_alloc(stat, buf_size);
623 if (ret) {
624 mutex_unlock(&stat->ioctl_lock);
625 return ret;
626 }
627
628 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
629 stat->ops->set_params(stat, new_conf);
630 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
631
632 /*
633 * Returning the right future config_counter for this setup, so
634 * userspace can *know* when it has been applied.
635 */
636 user_cfg->config_counter = stat->config_counter + stat->inc_config;
637
638 /* Module has a valid configuration. */
639 stat->configured = 1;
640 dev_dbg(stat->isp->dev, "%s: module has been successfully "
641 "configured.\n", stat->subdev.name);
642
643 mutex_unlock(&stat->ioctl_lock);
644
645 return 0;
646}
647
648/*
649 * isp_stat_buf_process - Process statistic buffers.
650 * @buf_state: points out if buffer is ready to be processed. It's necessary
651 * because histogram needs to copy the data from internal memory
652 * before be able to process the buffer.
653 */
654static int isp_stat_buf_process(struct ispstat *stat, int buf_state)
655{
656 int ret = STAT_NO_BUF;
657
658 if (!atomic_add_unless(&stat->buf_err, -1, 0) &&
659 buf_state == STAT_BUF_DONE && stat->state == ISPSTAT_ENABLED) {
660 ret = isp_stat_buf_queue(stat);
661 isp_stat_buf_next(stat);
662 }
663
664 return ret;
665}
666
667int omap3isp_stat_pcr_busy(struct ispstat *stat)
668{
669 return stat->ops->busy(stat);
670}
671
672int omap3isp_stat_busy(struct ispstat *stat)
673{
674 return omap3isp_stat_pcr_busy(stat) | stat->buf_processing |
675 (stat->state != ISPSTAT_DISABLED);
676}
677
678/*
679 * isp_stat_pcr_enable - Disables/Enables statistic engines.
680 * @pcr_enable: 0/1 - Disables/Enables the engine.
681 *
682 * Must be called from ISP driver when the module is idle and synchronized
683 * with CCDC.
684 */
685static void isp_stat_pcr_enable(struct ispstat *stat, u8 pcr_enable)
686{
687 if ((stat->state != ISPSTAT_ENABLING &&
688 stat->state != ISPSTAT_ENABLED) && pcr_enable)
689 /* Userspace has disabled the module. Aborting. */
690 return;
691
692 stat->ops->enable(stat, pcr_enable);
693 if (stat->state == ISPSTAT_DISABLING && !pcr_enable)
694 stat->state = ISPSTAT_DISABLED;
695 else if (stat->state == ISPSTAT_ENABLING && pcr_enable)
696 stat->state = ISPSTAT_ENABLED;
697}
698
699void omap3isp_stat_suspend(struct ispstat *stat)
700{
701 unsigned long flags;
702
703 spin_lock_irqsave(&stat->isp->stat_lock, flags);
704
705 if (stat->state != ISPSTAT_DISABLED)
706 stat->ops->enable(stat, 0);
707 if (stat->state == ISPSTAT_ENABLED)
708 stat->state = ISPSTAT_SUSPENDED;
709
710 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
711}
712
713void omap3isp_stat_resume(struct ispstat *stat)
714{
715 /* Module will be re-enabled with its pipeline */
716 if (stat->state == ISPSTAT_SUSPENDED)
717 stat->state = ISPSTAT_ENABLING;
718}
719
720static void isp_stat_try_enable(struct ispstat *stat)
721{
722 unsigned long irqflags;
723
724 if (stat->priv == NULL)
725 /* driver wasn't initialised */
726 return;
727
728 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
729 if (stat->state == ISPSTAT_ENABLING && !stat->buf_processing &&
730 stat->buf_alloc_size) {
731 /*
732 * Userspace's requested to enable the engine but it wasn't yet.
733 * Let's do that now.
734 */
735 stat->update = 1;
736 isp_stat_buf_next(stat);
737 stat->ops->setup_regs(stat, stat->priv);
738 isp_stat_buf_insert_magic(stat, stat->active_buf);
739
740 /*
741 * H3A module has some hw issues which forces the driver to
742 * ignore next buffers even if it was disabled in the meantime.
743 * On the other hand, Histogram shouldn't ignore buffers anymore
744 * if it's being enabled.
745 */
746 if (!IS_H3A(stat))
747 atomic_set(&stat->buf_err, 0);
748
749 isp_stat_pcr_enable(stat, 1);
750 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
751 dev_dbg(stat->isp->dev, "%s: module is enabled.\n",
752 stat->subdev.name);
753 } else {
754 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
755 }
756}
757
758void omap3isp_stat_isr_frame_sync(struct ispstat *stat)
759{
760 isp_stat_try_enable(stat);
761}
762
763void omap3isp_stat_sbl_overflow(struct ispstat *stat)
764{
765 unsigned long irqflags;
766
767 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
768 /*
769 * Due to a H3A hw issue which prevents the next buffer to start from
770 * the correct memory address, 2 buffers must be ignored.
771 */
772 atomic_set(&stat->buf_err, 2);
773
774 /*
775 * If more than one SBL overflow happen in a row, H3A module may access
776 * invalid memory region.
777 * stat->sbl_ovl_recover is set to tell to the driver to temporarily use
778 * a soft configuration which helps to avoid consecutive overflows.
779 */
780 if (stat->recover_priv)
781 stat->sbl_ovl_recover = 1;
782 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
783}
784
785/*
786 * omap3isp_stat_enable - Disable/Enable statistic engine as soon as possible
787 * @enable: 0/1 - Disables/Enables the engine.
788 *
789 * Client should configure all the module registers before this.
790 * This function can be called from a userspace request.
791 */
792int omap3isp_stat_enable(struct ispstat *stat, u8 enable)
793{
794 unsigned long irqflags;
795
796 dev_dbg(stat->isp->dev, "%s: user wants to %s module.\n",
797 stat->subdev.name, enable ? "enable" : "disable");
798
799 /* Prevent enabling while configuring */
800 mutex_lock(&stat->ioctl_lock);
801
802 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
803
804 if (!stat->configured && enable) {
805 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
806 mutex_unlock(&stat->ioctl_lock);
807 dev_dbg(stat->isp->dev, "%s: cannot enable module as it's "
808 "never been successfully configured so far.\n",
809 stat->subdev.name);
810 return -EINVAL;
811 }
812
813 if (enable) {
814 if (stat->state == ISPSTAT_DISABLING)
815 /* Previous disabling request wasn't done yet */
816 stat->state = ISPSTAT_ENABLED;
817 else if (stat->state == ISPSTAT_DISABLED)
818 /* Module is now being enabled */
819 stat->state = ISPSTAT_ENABLING;
820 } else {
821 if (stat->state == ISPSTAT_ENABLING) {
822 /* Previous enabling request wasn't done yet */
823 stat->state = ISPSTAT_DISABLED;
824 } else if (stat->state == ISPSTAT_ENABLED) {
825 /* Module is now being disabled */
826 stat->state = ISPSTAT_DISABLING;
827 isp_stat_buf_clear(stat);
828 }
829 }
830
831 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
832 mutex_unlock(&stat->ioctl_lock);
833
834 return 0;
835}
836
837int omap3isp_stat_s_stream(struct v4l2_subdev *subdev, int enable)
838{
839 struct ispstat *stat = v4l2_get_subdevdata(subdev);
840
841 if (enable) {
842 /*
843 * Only set enable PCR bit if the module was previously
844 * enabled through ioct.
845 */
846 isp_stat_try_enable(stat);
847 } else {
848 unsigned long flags;
849 /* Disable PCR bit and config enable field */
850 omap3isp_stat_enable(stat, 0);
851 spin_lock_irqsave(&stat->isp->stat_lock, flags);
852 stat->ops->enable(stat, 0);
853 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
854
855 /*
856 * If module isn't busy, a new interrupt may come or not to
857 * set the state to DISABLED. As Histogram needs to read its
858 * internal memory to clear it, let interrupt handler
859 * responsible of changing state to DISABLED. If the last
860 * interrupt is coming, it's still safe as the handler will
861 * ignore the second time when state is already set to DISABLED.
862 * It's necessary to synchronize Histogram with streamoff, once
863 * the module may be considered idle before last SDMA transfer
864 * starts if we return here.
865 */
866 if (!omap3isp_stat_pcr_busy(stat))
867 omap3isp_stat_isr(stat);
868
869 dev_dbg(stat->isp->dev, "%s: module is being disabled\n",
870 stat->subdev.name);
871 }
872
873 return 0;
874}
875
876/*
877 * __stat_isr - Interrupt handler for statistic drivers
878 */
879static void __stat_isr(struct ispstat *stat, int from_dma)
880{
881 int ret = STAT_BUF_DONE;
882 int buf_processing;
883 unsigned long irqflags;
884 struct isp_pipeline *pipe;
885
886 /*
887 * stat->buf_processing must be set before disable module. It's
888 * necessary to not inform too early the buffers aren't busy in case
889 * of SDMA is going to be used.
890 */
891 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
892 if (stat->state == ISPSTAT_DISABLED) {
893 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
894 return;
895 }
896 buf_processing = stat->buf_processing;
897 stat->buf_processing = 1;
898 stat->ops->enable(stat, 0);
899
900 if (buf_processing && !from_dma) {
901 if (stat->state == ISPSTAT_ENABLED) {
902 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
903 dev_err(stat->isp->dev,
904 "%s: interrupt occurred when module was still "
905 "processing a buffer.\n", stat->subdev.name);
906 ret = STAT_NO_BUF;
907 goto out;
908 } else {
909 /*
910 * Interrupt handler was called from streamoff when
911 * the module wasn't busy anymore to ensure it is being
912 * disabled after process last buffer. If such buffer
913 * processing has already started, no need to do
914 * anything else.
915 */
916 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
917 return;
918 }
919 }
920 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
921
922 /* If it's busy we can't process this buffer anymore */
923 if (!omap3isp_stat_pcr_busy(stat)) {
924 if (!from_dma && stat->ops->buf_process)
925 /* Module still need to copy data to buffer. */
926 ret = stat->ops->buf_process(stat);
927 if (ret == STAT_BUF_WAITING_DMA)
928 /* Buffer is not ready yet */
929 return;
930
931 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
932
933 /*
934 * Histogram needs to read its internal memory to clear it
935 * before be disabled. For that reason, common statistic layer
936 * can return only after call stat's buf_process() operator.
937 */
938 if (stat->state == ISPSTAT_DISABLING) {
939 stat->state = ISPSTAT_DISABLED;
940 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
941 stat->buf_processing = 0;
942 return;
943 }
944 pipe = to_isp_pipeline(&stat->subdev.entity);
945 stat->frame_number = atomic_read(&pipe->frame_number);
946
947 /*
948 * Before this point, 'ret' stores the buffer's status if it's
949 * ready to be processed. Afterwards, it holds the status if
950 * it was processed successfully.
951 */
952 ret = isp_stat_buf_process(stat, ret);
953
954 if (likely(!stat->sbl_ovl_recover)) {
955 stat->ops->setup_regs(stat, stat->priv);
956 } else {
957 /*
958 * Using recover config to increase the chance to have
959 * a good buffer processing and make the H3A module to
960 * go back to a valid state.
961 */
962 stat->update = 1;
963 stat->ops->setup_regs(stat, stat->recover_priv);
964 stat->sbl_ovl_recover = 0;
965
966 /*
967 * Set 'update' in case of the module needs to use
968 * regular configuration after next buffer.
969 */
970 stat->update = 1;
971 }
972
973 isp_stat_buf_insert_magic(stat, stat->active_buf);
974
975 /*
976 * Hack: H3A modules may access invalid memory address or send
977 * corrupted data to userspace if more than 1 SBL overflow
978 * happens in a row without re-writing its buffer's start memory
979 * address in the meantime. Such situation is avoided if the
980 * module is not immediately re-enabled when the ISR misses the
981 * timing to process the buffer and to setup the registers.
982 * Because of that, pcr_enable(1) was moved to inside this 'if'
983 * block. But the next interruption will still happen as during
984 * pcr_enable(0) the module was busy.
985 */
986 isp_stat_pcr_enable(stat, 1);
987 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
988 } else {
989 /*
990 * If a SBL overflow occurs and the H3A driver misses the timing
991 * to process the buffer, stat->buf_err is set and won't be
992 * cleared now. So the next buffer will be correctly ignored.
993 * It's necessary due to a hw issue which makes the next H3A
994 * buffer to start from the memory address where the previous
995 * one stopped, instead of start where it was configured to.
996 * Do not "stat->buf_err = 0" here.
997 */
998
999 if (stat->ops->buf_process)
1000 /*
1001 * Driver may need to erase current data prior to
1002 * process a new buffer. If it misses the timing, the
1003 * next buffer might be wrong. So should be ignored.
1004 * It happens only for Histogram.
1005 */
1006 atomic_set(&stat->buf_err, 1);
1007
1008 ret = STAT_NO_BUF;
1009 dev_dbg(stat->isp->dev, "%s: cannot process buffer, "
1010 "device is busy.\n", stat->subdev.name);
1011 }
1012
1013out:
1014 stat->buf_processing = 0;
1015 isp_stat_queue_event(stat, ret != STAT_BUF_DONE);
1016}
1017
1018void omap3isp_stat_isr(struct ispstat *stat)
1019{
1020 __stat_isr(stat, 0);
1021}
1022
1023void omap3isp_stat_dma_isr(struct ispstat *stat)
1024{
1025 __stat_isr(stat, 1);
1026}
1027
David Cohen68e342b2011-02-12 18:05:06 -03001028int omap3isp_stat_subscribe_event(struct v4l2_subdev *subdev,
1029 struct v4l2_fh *fh,
Laurent Pinchart55c85042012-10-12 11:20:10 -03001030 struct v4l2_event_subscription *sub)
David Cohen68e342b2011-02-12 18:05:06 -03001031{
1032 struct ispstat *stat = v4l2_get_subdevdata(subdev);
1033
1034 if (sub->type != stat->event_type)
1035 return -EINVAL;
1036
Hans de Goedec53c2542012-04-08 12:59:46 -03001037 return v4l2_event_subscribe(fh, sub, STAT_NEVENTS, NULL);
David Cohen68e342b2011-02-12 18:05:06 -03001038}
1039
1040int omap3isp_stat_unsubscribe_event(struct v4l2_subdev *subdev,
1041 struct v4l2_fh *fh,
Laurent Pinchart55c85042012-10-12 11:20:10 -03001042 struct v4l2_event_subscription *sub)
David Cohen68e342b2011-02-12 18:05:06 -03001043{
1044 return v4l2_event_unsubscribe(fh, sub);
1045}
1046
1047void omap3isp_stat_unregister_entities(struct ispstat *stat)
1048{
David Cohen68e342b2011-02-12 18:05:06 -03001049 v4l2_device_unregister_subdev(&stat->subdev);
1050}
1051
1052int omap3isp_stat_register_entities(struct ispstat *stat,
1053 struct v4l2_device *vdev)
1054{
1055 return v4l2_device_register_subdev(vdev, &stat->subdev);
1056}
1057
Laurent Pinchart39099d02011-09-22 16:59:26 -03001058static int isp_stat_init_entities(struct ispstat *stat, const char *name,
1059 const struct v4l2_subdev_ops *sd_ops)
1060{
1061 struct v4l2_subdev *subdev = &stat->subdev;
1062 struct media_entity *me = &subdev->entity;
1063
1064 v4l2_subdev_init(subdev, sd_ops);
1065 snprintf(subdev->name, V4L2_SUBDEV_NAME_SIZE, "OMAP3 ISP %s", name);
1066 subdev->grp_id = 1 << 16; /* group ID for isp subdevs */
1067 subdev->flags |= V4L2_SUBDEV_FL_HAS_EVENTS | V4L2_SUBDEV_FL_HAS_DEVNODE;
1068 v4l2_set_subdevdata(subdev, stat);
1069
Sakari Ailus8dad9362013-10-02 20:17:52 -03001070 stat->pad.flags = MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
Laurent Pinchart39099d02011-09-22 16:59:26 -03001071 me->ops = NULL;
1072
1073 return media_entity_init(me, 1, &stat->pad, 0);
1074}
1075
David Cohen68e342b2011-02-12 18:05:06 -03001076int omap3isp_stat_init(struct ispstat *stat, const char *name,
1077 const struct v4l2_subdev_ops *sd_ops)
1078{
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03001079 int ret;
1080
David Cohen68e342b2011-02-12 18:05:06 -03001081 stat->buf = kcalloc(STAT_MAX_BUFS, sizeof(*stat->buf), GFP_KERNEL);
1082 if (!stat->buf)
1083 return -ENOMEM;
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03001084
David Cohen68e342b2011-02-12 18:05:06 -03001085 isp_stat_buf_clear(stat);
1086 mutex_init(&stat->ioctl_lock);
1087 atomic_set(&stat->buf_err, 0);
1088
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03001089 ret = isp_stat_init_entities(stat, name, sd_ops);
1090 if (ret < 0) {
1091 mutex_destroy(&stat->ioctl_lock);
1092 kfree(stat->buf);
1093 }
1094
1095 return ret;
David Cohen68e342b2011-02-12 18:05:06 -03001096}
1097
Laurent Pinchart63b4ca22011-09-22 16:54:34 -03001098void omap3isp_stat_cleanup(struct ispstat *stat)
David Cohen68e342b2011-02-12 18:05:06 -03001099{
Laurent Pinchart63b4ca22011-09-22 16:54:34 -03001100 media_entity_cleanup(&stat->subdev.entity);
Laurent Pincharted33ac82011-09-22 17:09:26 -03001101 mutex_destroy(&stat->ioctl_lock);
David Cohen68e342b2011-02-12 18:05:06 -03001102 isp_stat_bufs_free(stat);
1103 kfree(stat->buf);
1104}