29 #include <linux/slab.h>
34 #define IS_COHERENT_BUF(stat) ((stat)->dma_ch >= 0)
41 #define MAGIC_NUM 0x55
44 #define AF_EXTRA_DATA OMAP3ISP_AF_PAXEL_SIZE
62 #define NUM_H3A_RECOVER_BUFS 10
68 #define IS_H3A_AF(stat) ((stat) == &(stat)->isp->isp_af)
69 #define IS_H3A_AEWB(stat) ((stat) == &(stat)->isp->isp_aewb)
70 #define IS_H3A(stat) (IS_H3A_AF(stat) || IS_H3A_AEWB(stat))
72 static void __isp_stat_buf_sync_magic(
struct ispstat *
stat,
75 void (*dma_sync)(
struct device *,
93 dma_sync(dev, dma_addr, offset,
MAGIC_SIZE, dir);
96 static void isp_stat_buf_sync_magic_for_device(
struct ispstat *stat,
104 __isp_stat_buf_sync_magic(stat, buf, buf_size, dir,
108 static void isp_stat_buf_sync_magic_for_cpu(
struct ispstat *stat,
116 __isp_stat_buf_sync_magic(stat, buf, buf_size, dir,
120 static int isp_stat_buf_check_magic(
struct ispstat *stat,
137 dev_dbg(stat->
isp->dev,
"%s: beginning magic check does not "
138 "match.\n", stat->
subdev.name);
146 dev_dbg(stat->
isp->dev,
"%s: endding magic check does "
147 "not match.\n", stat->
subdev.name);
152 isp_stat_buf_sync_magic_for_device(stat, buf, buf_size,
158 static void isp_stat_buf_insert_magic(
struct ispstat *stat,
175 isp_stat_buf_sync_magic_for_device(stat, buf, buf_size,
179 static void isp_stat_buf_sync_for_device(
struct ispstat *stat,
189 static void isp_stat_buf_sync_for_cpu(
struct ispstat *stat,
199 static void isp_stat_buf_clear(
struct ispstat *stat)
204 stat->
buf[i].empty = 1;
208 __isp_stat_buf_find(
struct ispstat *stat,
int look_empty)
224 if (!look_empty && curr->
empty)
243 isp_stat_buf_find_oldest(
struct ispstat *stat)
245 return __isp_stat_buf_find(stat, 0);
249 isp_stat_buf_find_oldest_or_empty(
struct ispstat *stat)
251 return __isp_stat_buf_find(stat, 1);
254 static int isp_stat_buf_queue(
struct ispstat *stat)
262 if (isp_stat_buf_check_magic(stat, stat->
active_buf)) {
263 dev_dbg(stat->
isp->dev,
"%s: data wasn't properly written.\n",
276 static void isp_stat_buf_next(
struct ispstat *stat)
280 dev_dbg(stat->
isp->dev,
"%s: new buffer requested without "
281 "queuing active one.\n",
284 stat->
active_buf = isp_stat_buf_find_oldest_or_empty(stat);
287 static void isp_stat_buf_release(
struct ispstat *stat)
291 isp_stat_buf_sync_for_device(stat, stat->
locked_buf);
294 spin_unlock_irqrestore(&stat->
isp->stat_lock, flags);
308 buf = isp_stat_buf_find_oldest(stat);
310 spin_unlock_irqrestore(&stat->
isp->stat_lock, flags);
311 dev_dbg(stat->
isp->dev,
"%s: cannot find a buffer.\n",
313 return ERR_PTR(-
EBUSY);
315 if (isp_stat_buf_check_magic(stat, buf)) {
316 dev_dbg(stat->
isp->dev,
"%s: current buffer has "
317 "corrupted data\n.", stat->
subdev.name);
328 spin_unlock_irqrestore(&stat->
isp->stat_lock, flags);
331 dev_warn(stat->
isp->dev,
"%s: userspace's buffer size is "
332 "not enough.\n", stat->
subdev.name);
333 isp_stat_buf_release(stat);
337 isp_stat_buf_sync_for_cpu(stat, buf);
345 "%s: failed copying %d bytes of stat data\n",
348 isp_stat_buf_release(stat);
354 static void isp_stat_bufs_free(
struct ispstat *stat)
367 buf->
iovm->sgt->nents,
384 dev_dbg(stat->
isp->dev,
"%s: all buffers were freed.\n",
391 static int isp_stat_bufs_alloc_iommu(
struct ispstat *stat,
unsigned int size)
407 "%s: Can't acquire memory for "
408 "buffer %d\n", stat->
subdev.name, i);
409 isp_stat_bufs_free(stat);
417 isp_stat_bufs_free(stat);
425 dev_dbg(stat->
isp->dev,
"%s: buffer[%d] allocated."
426 "iommu_addr=0x%08lx virt_addr=0x%08lx",
434 static int isp_stat_bufs_alloc_dma(
struct ispstat *stat,
unsigned int size)
449 "%s: Can't acquire memory for "
450 "DMA buffer %d\n", stat->
subdev.name, i);
451 isp_stat_bufs_free(stat);
456 dev_dbg(stat->
isp->dev,
"%s: buffer[%d] allocated."
457 "dma_addr=0x%08lx virt_addr=0x%08lx\n",
465 static int isp_stat_bufs_alloc(
struct ispstat *stat,
u32 size)
475 spin_unlock_irqrestore(&stat->
isp->stat_lock, flags);
481 "%s: trying to allocate memory when busy\n",
483 spin_unlock_irqrestore(&stat->
isp->stat_lock, flags);
487 spin_unlock_irqrestore(&stat->
isp->stat_lock, flags);
489 isp_stat_bufs_free(stat);
492 return isp_stat_bufs_alloc_dma(stat, size);
494 return isp_stat_bufs_alloc_iommu(stat, size);
497 static void isp_stat_queue_event(
struct ispstat *stat,
int err)
527 dev_dbg(stat->
isp->dev,
"%s: engine not enabled.\n",
533 buf = isp_stat_buf_get(stat, data);
545 isp_stat_buf_release(stat);
562 unsigned long irqflags;
567 dev_dbg(stat->
isp->dev,
"%s: configuration is NULL\n",
574 dev_dbg(stat->
isp->dev,
"%s: configuring module with buffer "
575 "size=0x%08lx\n", stat->
subdev.name, (
unsigned long)buf_size);
577 ret = stat->
ops->validate_params(stat, new_conf);
580 dev_dbg(stat->
isp->dev,
"%s: configuration values are "
581 "invalid.\n", stat->
subdev.name);
586 dev_dbg(stat->
isp->dev,
"%s: driver has corrected buffer size "
587 "request to 0x%08lx\n", stat->
subdev.name,
620 ret = isp_stat_bufs_alloc(stat, buf_size);
627 stat->
ops->set_params(stat, new_conf);
628 spin_unlock_irqrestore(&stat->
isp->stat_lock, irqflags);
638 dev_dbg(stat->
isp->dev,
"%s: module has been successfully "
639 "configured.\n", stat->
subdev.name);
652 static int isp_stat_buf_process(
struct ispstat *stat,
int buf_state)
656 if (!atomic_add_unless(&stat->
buf_err, -1, 0) &&
658 ret = isp_stat_buf_queue(stat);
659 isp_stat_buf_next(stat);
667 return stat->
ops->busy(stat);
683 static void isp_stat_pcr_enable(
struct ispstat *stat,
u8 pcr_enable)
690 stat->
ops->enable(stat, pcr_enable);
704 stat->
ops->enable(stat, 0);
708 spin_unlock_irqrestore(&stat->
isp->stat_lock, flags);
718 static void isp_stat_try_enable(
struct ispstat *stat)
720 unsigned long irqflags;
734 isp_stat_buf_next(stat);
735 stat->
ops->setup_regs(stat, stat->
priv);
736 isp_stat_buf_insert_magic(stat, stat->
active_buf);
747 isp_stat_pcr_enable(stat, 1);
748 spin_unlock_irqrestore(&stat->
isp->stat_lock, irqflags);
749 dev_dbg(stat->
isp->dev,
"%s: module is enabled.\n",
752 spin_unlock_irqrestore(&stat->
isp->stat_lock, irqflags);
758 isp_stat_try_enable(stat);
763 unsigned long irqflags;
780 spin_unlock_irqrestore(&stat->
isp->stat_lock, irqflags);
792 unsigned long irqflags;
794 dev_dbg(stat->
isp->dev,
"%s: user wants to %s module.\n",
795 stat->
subdev.name, enable ?
"enable" :
"disable");
803 spin_unlock_irqrestore(&stat->
isp->stat_lock, irqflags);
805 dev_dbg(stat->
isp->dev,
"%s: cannot enable module as it's "
806 "never been successfully configured so far.\n",
825 isp_stat_buf_clear(stat);
829 spin_unlock_irqrestore(&stat->
isp->stat_lock, irqflags);
837 struct ispstat *stat = v4l2_get_subdevdata(subdev);
844 isp_stat_try_enable(stat);
850 stat->
ops->enable(stat, 0);
851 spin_unlock_irqrestore(&stat->
isp->stat_lock, flags);
867 dev_dbg(stat->
isp->dev,
"%s: module is being disabled\n",
877 static void __stat_isr(
struct ispstat *stat,
int from_dma)
881 unsigned long irqflags;
891 spin_unlock_irqrestore(&stat->
isp->stat_lock, irqflags);
896 stat->
ops->enable(stat, 0);
898 if (buf_processing && !from_dma) {
900 spin_unlock_irqrestore(&stat->
isp->stat_lock, irqflags);
902 "%s: interrupt occurred when module was still "
903 "processing a buffer.\n", stat->
subdev.name);
914 spin_unlock_irqrestore(&stat->
isp->stat_lock, irqflags);
918 spin_unlock_irqrestore(&stat->
isp->stat_lock, irqflags);
922 if (!from_dma && stat->
ops->buf_process)
924 ret = stat->
ops->buf_process(stat);
938 spin_unlock_irqrestore(&stat->
isp->stat_lock, irqflags);
942 pipe = to_isp_pipeline(&stat->
subdev.entity);
950 ret = isp_stat_buf_process(stat, ret);
953 stat->
ops->setup_regs(stat, stat->
priv);
971 isp_stat_buf_insert_magic(stat, stat->
active_buf);
984 isp_stat_pcr_enable(stat, 1);
985 spin_unlock_irqrestore(&stat->
isp->stat_lock, irqflags);
997 if (stat->
ops->buf_process)
1007 dev_dbg(stat->
isp->dev,
"%s: cannot process buffer, "
1008 "device is busy.\n", stat->
subdev.name);
1018 __stat_isr(stat, 0);
1023 __stat_isr(stat, 1);
1030 struct ispstat *stat = v4l2_get_subdevdata(subdev);
1056 static int isp_stat_init_entities(
struct ispstat *stat,
const char *
name,
1064 subdev->
grp_id = 1 << 16;
1066 v4l2_set_subdevdata(subdev, stat);
1083 isp_stat_buf_clear(stat);
1087 ret = isp_stat_init_entities(stat, name, sd_ops);
1100 isp_stat_bufs_free(stat);