ispstat.c 30 KB

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