ispstat.c 29 KB

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