ispstat.c 30 KB

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