ispstat.c 30 KB

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