blob: 35c38237449dbc14cf1999b1ed1316811eae66b3 [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
260 do_gettimeofday(&stat->active_buf->ts);
261
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
540 data->ts = buf->ts;
541 data->config_counter = buf->config_counter;
542 data->frame_number = buf->frame_number;
543 data->buf_size = buf->buf_size;
544
545 buf->empty = 1;
546 isp_stat_buf_release(stat);
547 mutex_unlock(&stat->ioctl_lock);
548
549 return 0;
550}
551
552/*
553 * omap3isp_stat_config - Receives new statistic engine configuration.
554 * @new_conf: Pointer to config structure.
555 *
556 * Returns 0 if successful, -EINVAL if new_conf pointer is NULL, -ENOMEM if
557 * was unable to allocate memory for the buffer, or other errors if parameters
558 * are invalid.
559 */
560int omap3isp_stat_config(struct ispstat *stat, void *new_conf)
561{
562 int ret;
563 unsigned long irqflags;
564 struct ispstat_generic_config *user_cfg = new_conf;
565 u32 buf_size = user_cfg->buf_size;
566
567 if (!new_conf) {
568 dev_dbg(stat->isp->dev, "%s: configuration is NULL\n",
569 stat->subdev.name);
570 return -EINVAL;
571 }
572
573 mutex_lock(&stat->ioctl_lock);
574
575 dev_dbg(stat->isp->dev, "%s: configuring module with buffer "
576 "size=0x%08lx\n", stat->subdev.name, (unsigned long)buf_size);
577
578 ret = stat->ops->validate_params(stat, new_conf);
579 if (ret) {
580 mutex_unlock(&stat->ioctl_lock);
581 dev_dbg(stat->isp->dev, "%s: configuration values are "
582 "invalid.\n", stat->subdev.name);
583 return ret;
584 }
585
586 if (buf_size != user_cfg->buf_size)
587 dev_dbg(stat->isp->dev, "%s: driver has corrected buffer size "
588 "request to 0x%08lx\n", stat->subdev.name,
589 (unsigned long)user_cfg->buf_size);
590
591 /*
592 * Hack: H3A modules may need a doubled buffer size to avoid access
593 * to a invalid memory address after a SBL overflow.
594 * The buffer size is always PAGE_ALIGNED.
595 * Hack 2: MAGIC_SIZE is added to buf_size so a magic word can be
596 * inserted at the end to data integrity check purpose.
597 * Hack 3: AF module writes one paxel data more than it should, so
598 * the buffer allocation must consider it to avoid invalid memory
599 * access.
600 * Hack 4: H3A need to allocate extra space for the recover state.
601 */
602 if (IS_H3A(stat)) {
603 buf_size = user_cfg->buf_size * 2 + MAGIC_SIZE;
604 if (IS_H3A_AF(stat))
605 /*
606 * Adding one extra paxel data size for each recover
607 * buffer + 2 regular ones.
608 */
609 buf_size += AF_EXTRA_DATA * (NUM_H3A_RECOVER_BUFS + 2);
610 if (stat->recover_priv) {
611 struct ispstat_generic_config *recover_cfg =
612 stat->recover_priv;
613 buf_size += recover_cfg->buf_size *
614 NUM_H3A_RECOVER_BUFS;
615 }
616 buf_size = PAGE_ALIGN(buf_size);
617 } else { /* Histogram */
618 buf_size = PAGE_ALIGN(user_cfg->buf_size + MAGIC_SIZE);
619 }
620
621 ret = isp_stat_bufs_alloc(stat, buf_size);
622 if (ret) {
623 mutex_unlock(&stat->ioctl_lock);
624 return ret;
625 }
626
627 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
628 stat->ops->set_params(stat, new_conf);
629 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
630
631 /*
632 * Returning the right future config_counter for this setup, so
633 * userspace can *know* when it has been applied.
634 */
635 user_cfg->config_counter = stat->config_counter + stat->inc_config;
636
637 /* Module has a valid configuration. */
638 stat->configured = 1;
639 dev_dbg(stat->isp->dev, "%s: module has been successfully "
640 "configured.\n", stat->subdev.name);
641
642 mutex_unlock(&stat->ioctl_lock);
643
644 return 0;
645}
646
647/*
648 * isp_stat_buf_process - Process statistic buffers.
649 * @buf_state: points out if buffer is ready to be processed. It's necessary
650 * because histogram needs to copy the data from internal memory
651 * before be able to process the buffer.
652 */
653static int isp_stat_buf_process(struct ispstat *stat, int buf_state)
654{
655 int ret = STAT_NO_BUF;
656
657 if (!atomic_add_unless(&stat->buf_err, -1, 0) &&
658 buf_state == STAT_BUF_DONE && stat->state == ISPSTAT_ENABLED) {
659 ret = isp_stat_buf_queue(stat);
660 isp_stat_buf_next(stat);
661 }
662
663 return ret;
664}
665
666int omap3isp_stat_pcr_busy(struct ispstat *stat)
667{
668 return stat->ops->busy(stat);
669}
670
671int omap3isp_stat_busy(struct ispstat *stat)
672{
673 return omap3isp_stat_pcr_busy(stat) | stat->buf_processing |
674 (stat->state != ISPSTAT_DISABLED);
675}
676
677/*
678 * isp_stat_pcr_enable - Disables/Enables statistic engines.
679 * @pcr_enable: 0/1 - Disables/Enables the engine.
680 *
681 * Must be called from ISP driver when the module is idle and synchronized
682 * with CCDC.
683 */
684static void isp_stat_pcr_enable(struct ispstat *stat, u8 pcr_enable)
685{
686 if ((stat->state != ISPSTAT_ENABLING &&
687 stat->state != ISPSTAT_ENABLED) && pcr_enable)
688 /* Userspace has disabled the module. Aborting. */
689 return;
690
691 stat->ops->enable(stat, pcr_enable);
692 if (stat->state == ISPSTAT_DISABLING && !pcr_enable)
693 stat->state = ISPSTAT_DISABLED;
694 else if (stat->state == ISPSTAT_ENABLING && pcr_enable)
695 stat->state = ISPSTAT_ENABLED;
696}
697
698void omap3isp_stat_suspend(struct ispstat *stat)
699{
700 unsigned long flags;
701
702 spin_lock_irqsave(&stat->isp->stat_lock, flags);
703
704 if (stat->state != ISPSTAT_DISABLED)
705 stat->ops->enable(stat, 0);
706 if (stat->state == ISPSTAT_ENABLED)
707 stat->state = ISPSTAT_SUSPENDED;
708
709 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
710}
711
712void omap3isp_stat_resume(struct ispstat *stat)
713{
714 /* Module will be re-enabled with its pipeline */
715 if (stat->state == ISPSTAT_SUSPENDED)
716 stat->state = ISPSTAT_ENABLING;
717}
718
719static void isp_stat_try_enable(struct ispstat *stat)
720{
721 unsigned long irqflags;
722
723 if (stat->priv == NULL)
724 /* driver wasn't initialised */
725 return;
726
727 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
728 if (stat->state == ISPSTAT_ENABLING && !stat->buf_processing &&
729 stat->buf_alloc_size) {
730 /*
731 * Userspace's requested to enable the engine but it wasn't yet.
732 * Let's do that now.
733 */
734 stat->update = 1;
735 isp_stat_buf_next(stat);
736 stat->ops->setup_regs(stat, stat->priv);
737 isp_stat_buf_insert_magic(stat, stat->active_buf);
738
739 /*
740 * H3A module has some hw issues which forces the driver to
741 * ignore next buffers even if it was disabled in the meantime.
742 * On the other hand, Histogram shouldn't ignore buffers anymore
743 * if it's being enabled.
744 */
745 if (!IS_H3A(stat))
746 atomic_set(&stat->buf_err, 0);
747
748 isp_stat_pcr_enable(stat, 1);
749 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
750 dev_dbg(stat->isp->dev, "%s: module is enabled.\n",
751 stat->subdev.name);
752 } else {
753 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
754 }
755}
756
757void omap3isp_stat_isr_frame_sync(struct ispstat *stat)
758{
759 isp_stat_try_enable(stat);
760}
761
762void omap3isp_stat_sbl_overflow(struct ispstat *stat)
763{
764 unsigned long irqflags;
765
766 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
767 /*
768 * Due to a H3A hw issue which prevents the next buffer to start from
769 * the correct memory address, 2 buffers must be ignored.
770 */
771 atomic_set(&stat->buf_err, 2);
772
773 /*
774 * If more than one SBL overflow happen in a row, H3A module may access
775 * invalid memory region.
776 * stat->sbl_ovl_recover is set to tell to the driver to temporarily use
777 * a soft configuration which helps to avoid consecutive overflows.
778 */
779 if (stat->recover_priv)
780 stat->sbl_ovl_recover = 1;
781 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
782}
783
784/*
785 * omap3isp_stat_enable - Disable/Enable statistic engine as soon as possible
786 * @enable: 0/1 - Disables/Enables the engine.
787 *
788 * Client should configure all the module registers before this.
789 * This function can be called from a userspace request.
790 */
791int omap3isp_stat_enable(struct ispstat *stat, u8 enable)
792{
793 unsigned long irqflags;
794
795 dev_dbg(stat->isp->dev, "%s: user wants to %s module.\n",
796 stat->subdev.name, enable ? "enable" : "disable");
797
798 /* Prevent enabling while configuring */
799 mutex_lock(&stat->ioctl_lock);
800
801 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
802
803 if (!stat->configured && enable) {
804 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
805 mutex_unlock(&stat->ioctl_lock);
806 dev_dbg(stat->isp->dev, "%s: cannot enable module as it's "
807 "never been successfully configured so far.\n",
808 stat->subdev.name);
809 return -EINVAL;
810 }
811
812 if (enable) {
813 if (stat->state == ISPSTAT_DISABLING)
814 /* Previous disabling request wasn't done yet */
815 stat->state = ISPSTAT_ENABLED;
816 else if (stat->state == ISPSTAT_DISABLED)
817 /* Module is now being enabled */
818 stat->state = ISPSTAT_ENABLING;
819 } else {
820 if (stat->state == ISPSTAT_ENABLING) {
821 /* Previous enabling request wasn't done yet */
822 stat->state = ISPSTAT_DISABLED;
823 } else if (stat->state == ISPSTAT_ENABLED) {
824 /* Module is now being disabled */
825 stat->state = ISPSTAT_DISABLING;
826 isp_stat_buf_clear(stat);
827 }
828 }
829
830 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
831 mutex_unlock(&stat->ioctl_lock);
832
833 return 0;
834}
835
836int omap3isp_stat_s_stream(struct v4l2_subdev *subdev, int enable)
837{
838 struct ispstat *stat = v4l2_get_subdevdata(subdev);
839
840 if (enable) {
841 /*
842 * Only set enable PCR bit if the module was previously
843 * enabled through ioct.
844 */
845 isp_stat_try_enable(stat);
846 } else {
847 unsigned long flags;
848 /* Disable PCR bit and config enable field */
849 omap3isp_stat_enable(stat, 0);
850 spin_lock_irqsave(&stat->isp->stat_lock, flags);
851 stat->ops->enable(stat, 0);
852 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
853
854 /*
855 * If module isn't busy, a new interrupt may come or not to
856 * set the state to DISABLED. As Histogram needs to read its
857 * internal memory to clear it, let interrupt handler
858 * responsible of changing state to DISABLED. If the last
859 * interrupt is coming, it's still safe as the handler will
860 * ignore the second time when state is already set to DISABLED.
861 * It's necessary to synchronize Histogram with streamoff, once
862 * the module may be considered idle before last SDMA transfer
863 * starts if we return here.
864 */
865 if (!omap3isp_stat_pcr_busy(stat))
866 omap3isp_stat_isr(stat);
867
868 dev_dbg(stat->isp->dev, "%s: module is being disabled\n",
869 stat->subdev.name);
870 }
871
872 return 0;
873}
874
875/*
876 * __stat_isr - Interrupt handler for statistic drivers
877 */
878static void __stat_isr(struct ispstat *stat, int from_dma)
879{
880 int ret = STAT_BUF_DONE;
881 int buf_processing;
882 unsigned long irqflags;
883 struct isp_pipeline *pipe;
884
885 /*
886 * stat->buf_processing must be set before disable module. It's
887 * necessary to not inform too early the buffers aren't busy in case
888 * of SDMA is going to be used.
889 */
890 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
891 if (stat->state == ISPSTAT_DISABLED) {
892 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
893 return;
894 }
895 buf_processing = stat->buf_processing;
896 stat->buf_processing = 1;
897 stat->ops->enable(stat, 0);
898
899 if (buf_processing && !from_dma) {
900 if (stat->state == ISPSTAT_ENABLED) {
901 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
902 dev_err(stat->isp->dev,
903 "%s: interrupt occurred when module was still "
904 "processing a buffer.\n", stat->subdev.name);
905 ret = STAT_NO_BUF;
906 goto out;
907 } else {
908 /*
909 * Interrupt handler was called from streamoff when
910 * the module wasn't busy anymore to ensure it is being
911 * disabled after process last buffer. If such buffer
912 * processing has already started, no need to do
913 * anything else.
914 */
915 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
916 return;
917 }
918 }
919 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
920
921 /* If it's busy we can't process this buffer anymore */
922 if (!omap3isp_stat_pcr_busy(stat)) {
923 if (!from_dma && stat->ops->buf_process)
924 /* Module still need to copy data to buffer. */
925 ret = stat->ops->buf_process(stat);
926 if (ret == STAT_BUF_WAITING_DMA)
927 /* Buffer is not ready yet */
928 return;
929
930 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
931
932 /*
933 * Histogram needs to read its internal memory to clear it
934 * before be disabled. For that reason, common statistic layer
935 * can return only after call stat's buf_process() operator.
936 */
937 if (stat->state == ISPSTAT_DISABLING) {
938 stat->state = ISPSTAT_DISABLED;
939 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
940 stat->buf_processing = 0;
941 return;
942 }
943 pipe = to_isp_pipeline(&stat->subdev.entity);
944 stat->frame_number = atomic_read(&pipe->frame_number);
945
946 /*
947 * Before this point, 'ret' stores the buffer's status if it's
948 * ready to be processed. Afterwards, it holds the status if
949 * it was processed successfully.
950 */
951 ret = isp_stat_buf_process(stat, ret);
952
953 if (likely(!stat->sbl_ovl_recover)) {
954 stat->ops->setup_regs(stat, stat->priv);
955 } else {
956 /*
957 * Using recover config to increase the chance to have
958 * a good buffer processing and make the H3A module to
959 * go back to a valid state.
960 */
961 stat->update = 1;
962 stat->ops->setup_regs(stat, stat->recover_priv);
963 stat->sbl_ovl_recover = 0;
964
965 /*
966 * Set 'update' in case of the module needs to use
967 * regular configuration after next buffer.
968 */
969 stat->update = 1;
970 }
971
972 isp_stat_buf_insert_magic(stat, stat->active_buf);
973
974 /*
975 * Hack: H3A modules may access invalid memory address or send
976 * corrupted data to userspace if more than 1 SBL overflow
977 * happens in a row without re-writing its buffer's start memory
978 * address in the meantime. Such situation is avoided if the
979 * module is not immediately re-enabled when the ISR misses the
980 * timing to process the buffer and to setup the registers.
981 * Because of that, pcr_enable(1) was moved to inside this 'if'
982 * block. But the next interruption will still happen as during
983 * pcr_enable(0) the module was busy.
984 */
985 isp_stat_pcr_enable(stat, 1);
986 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
987 } else {
988 /*
989 * If a SBL overflow occurs and the H3A driver misses the timing
990 * to process the buffer, stat->buf_err is set and won't be
991 * cleared now. So the next buffer will be correctly ignored.
992 * It's necessary due to a hw issue which makes the next H3A
993 * buffer to start from the memory address where the previous
994 * one stopped, instead of start where it was configured to.
995 * Do not "stat->buf_err = 0" here.
996 */
997
998 if (stat->ops->buf_process)
999 /*
1000 * Driver may need to erase current data prior to
1001 * process a new buffer. If it misses the timing, the
1002 * next buffer might be wrong. So should be ignored.
1003 * It happens only for Histogram.
1004 */
1005 atomic_set(&stat->buf_err, 1);
1006
1007 ret = STAT_NO_BUF;
1008 dev_dbg(stat->isp->dev, "%s: cannot process buffer, "
1009 "device is busy.\n", stat->subdev.name);
1010 }
1011
1012out:
1013 stat->buf_processing = 0;
1014 isp_stat_queue_event(stat, ret != STAT_BUF_DONE);
1015}
1016
1017void omap3isp_stat_isr(struct ispstat *stat)
1018{
1019 __stat_isr(stat, 0);
1020}
1021
1022void omap3isp_stat_dma_isr(struct ispstat *stat)
1023{
1024 __stat_isr(stat, 1);
1025}
1026
David Cohen68e342b2011-02-12 18:05:06 -03001027int omap3isp_stat_subscribe_event(struct v4l2_subdev *subdev,
1028 struct v4l2_fh *fh,
Hans Verkuil85f5fe32012-09-04 11:46:09 -03001029 const struct v4l2_event_subscription *sub)
David Cohen68e342b2011-02-12 18:05:06 -03001030{
1031 struct ispstat *stat = v4l2_get_subdevdata(subdev);
1032
1033 if (sub->type != stat->event_type)
1034 return -EINVAL;
1035
Hans de Goedec53c2542012-04-08 12:59:46 -03001036 return v4l2_event_subscribe(fh, sub, STAT_NEVENTS, NULL);
David Cohen68e342b2011-02-12 18:05:06 -03001037}
1038
1039int omap3isp_stat_unsubscribe_event(struct v4l2_subdev *subdev,
1040 struct v4l2_fh *fh,
Hans Verkuil85f5fe32012-09-04 11:46:09 -03001041 const struct v4l2_event_subscription *sub)
David Cohen68e342b2011-02-12 18:05:06 -03001042{
1043 return v4l2_event_unsubscribe(fh, sub);
1044}
1045
1046void omap3isp_stat_unregister_entities(struct ispstat *stat)
1047{
David Cohen68e342b2011-02-12 18:05:06 -03001048 v4l2_device_unregister_subdev(&stat->subdev);
1049}
1050
1051int omap3isp_stat_register_entities(struct ispstat *stat,
1052 struct v4l2_device *vdev)
1053{
1054 return v4l2_device_register_subdev(vdev, &stat->subdev);
1055}
1056
Laurent Pinchart39099d02011-09-22 16:59:26 -03001057static int isp_stat_init_entities(struct ispstat *stat, const char *name,
1058 const struct v4l2_subdev_ops *sd_ops)
1059{
1060 struct v4l2_subdev *subdev = &stat->subdev;
1061 struct media_entity *me = &subdev->entity;
1062
1063 v4l2_subdev_init(subdev, sd_ops);
1064 snprintf(subdev->name, V4L2_SUBDEV_NAME_SIZE, "OMAP3 ISP %s", name);
1065 subdev->grp_id = 1 << 16; /* group ID for isp subdevs */
1066 subdev->flags |= V4L2_SUBDEV_FL_HAS_EVENTS | V4L2_SUBDEV_FL_HAS_DEVNODE;
1067 v4l2_set_subdevdata(subdev, stat);
1068
1069 stat->pad.flags = MEDIA_PAD_FL_SINK;
1070 me->ops = NULL;
1071
1072 return media_entity_init(me, 1, &stat->pad, 0);
1073}
1074
David Cohen68e342b2011-02-12 18:05:06 -03001075int omap3isp_stat_init(struct ispstat *stat, const char *name,
1076 const struct v4l2_subdev_ops *sd_ops)
1077{
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03001078 int ret;
1079
David Cohen68e342b2011-02-12 18:05:06 -03001080 stat->buf = kcalloc(STAT_MAX_BUFS, sizeof(*stat->buf), GFP_KERNEL);
1081 if (!stat->buf)
1082 return -ENOMEM;
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03001083
David Cohen68e342b2011-02-12 18:05:06 -03001084 isp_stat_buf_clear(stat);
1085 mutex_init(&stat->ioctl_lock);
1086 atomic_set(&stat->buf_err, 0);
1087
Laurent Pinchart9b6390b2011-09-22 17:10:30 -03001088 ret = isp_stat_init_entities(stat, name, sd_ops);
1089 if (ret < 0) {
1090 mutex_destroy(&stat->ioctl_lock);
1091 kfree(stat->buf);
1092 }
1093
1094 return ret;
David Cohen68e342b2011-02-12 18:05:06 -03001095}
1096
Laurent Pinchart63b4ca22011-09-22 16:54:34 -03001097void omap3isp_stat_cleanup(struct ispstat *stat)
David Cohen68e342b2011-02-12 18:05:06 -03001098{
Laurent Pinchart63b4ca22011-09-22 16:54:34 -03001099 media_entity_cleanup(&stat->subdev.entity);
Laurent Pincharted33ac82011-09-22 17:09:26 -03001100 mutex_destroy(&stat->ioctl_lock);
David Cohen68e342b2011-02-12 18:05:06 -03001101 isp_stat_bufs_free(stat);
1102 kfree(stat->buf);
1103}