[go: nahoru, domu]

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