File Index
Files and directories
Sign in
FILE

ispstat.c

File preview with syntax highlighting.

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