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