ispstat.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  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. do_gettimeofday(&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 = buf->ts;
  450. data->config_counter = buf->config_counter;
  451. data->frame_number = buf->frame_number;
  452. data->buf_size = buf->buf_size;
  453. buf->empty = 1;
  454. isp_stat_buf_release(stat);
  455. mutex_unlock(&stat->ioctl_lock);
  456. return 0;
  457. }
  458. /*
  459. * omap3isp_stat_config - Receives new statistic engine configuration.
  460. * @new_conf: Pointer to config structure.
  461. *
  462. * Returns 0 if successful, -EINVAL if new_conf pointer is NULL, -ENOMEM if
  463. * was unable to allocate memory for the buffer, or other errors if parameters
  464. * are invalid.
  465. */
  466. int omap3isp_stat_config(struct ispstat *stat, void *new_conf)
  467. {
  468. int ret;
  469. unsigned long irqflags;
  470. struct ispstat_generic_config *user_cfg = new_conf;
  471. u32 buf_size = user_cfg->buf_size;
  472. if (!new_conf) {
  473. dev_dbg(stat->isp->dev, "%s: configuration is NULL\n",
  474. stat->subdev.name);
  475. return -EINVAL;
  476. }
  477. mutex_lock(&stat->ioctl_lock);
  478. dev_dbg(stat->isp->dev, "%s: configuring module with buffer "
  479. "size=0x%08lx\n", stat->subdev.name, (unsigned long)buf_size);
  480. ret = stat->ops->validate_params(stat, new_conf);
  481. if (ret) {
  482. mutex_unlock(&stat->ioctl_lock);
  483. dev_dbg(stat->isp->dev, "%s: configuration values are "
  484. "invalid.\n", stat->subdev.name);
  485. return ret;
  486. }
  487. if (buf_size != user_cfg->buf_size)
  488. dev_dbg(stat->isp->dev, "%s: driver has corrected buffer size "
  489. "request to 0x%08lx\n", stat->subdev.name,
  490. (unsigned long)user_cfg->buf_size);
  491. /*
  492. * Hack: H3A modules may need a doubled buffer size to avoid access
  493. * to a invalid memory address after a SBL overflow.
  494. * The buffer size is always PAGE_ALIGNED.
  495. * Hack 2: MAGIC_SIZE is added to buf_size so a magic word can be
  496. * inserted at the end to data integrity check purpose.
  497. * Hack 3: AF module writes one paxel data more than it should, so
  498. * the buffer allocation must consider it to avoid invalid memory
  499. * access.
  500. * Hack 4: H3A need to allocate extra space for the recover state.
  501. */
  502. if (IS_H3A(stat)) {
  503. buf_size = user_cfg->buf_size * 2 + MAGIC_SIZE;
  504. if (IS_H3A_AF(stat))
  505. /*
  506. * Adding one extra paxel data size for each recover
  507. * buffer + 2 regular ones.
  508. */
  509. buf_size += AF_EXTRA_DATA * (NUM_H3A_RECOVER_BUFS + 2);
  510. if (stat->recover_priv) {
  511. struct ispstat_generic_config *recover_cfg =
  512. stat->recover_priv;
  513. buf_size += recover_cfg->buf_size *
  514. NUM_H3A_RECOVER_BUFS;
  515. }
  516. buf_size = PAGE_ALIGN(buf_size);
  517. } else { /* Histogram */
  518. buf_size = PAGE_ALIGN(user_cfg->buf_size + MAGIC_SIZE);
  519. }
  520. ret = isp_stat_bufs_alloc(stat, buf_size);
  521. if (ret) {
  522. mutex_unlock(&stat->ioctl_lock);
  523. return ret;
  524. }
  525. spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
  526. stat->ops->set_params(stat, new_conf);
  527. spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
  528. /*
  529. * Returning the right future config_counter for this setup, so
  530. * userspace can *know* when it has been applied.
  531. */
  532. user_cfg->config_counter = stat->config_counter + stat->inc_config;
  533. /* Module has a valid configuration. */
  534. stat->configured = 1;
  535. dev_dbg(stat->isp->dev, "%s: module has been successfully "
  536. "configured.\n", stat->subdev.name);
  537. mutex_unlock(&stat->ioctl_lock);
  538. return 0;
  539. }
  540. /*
  541. * isp_stat_buf_process - Process statistic buffers.
  542. * @buf_state: points out if buffer is ready to be processed. It's necessary
  543. * because histogram needs to copy the data from internal memory
  544. * before be able to process the buffer.
  545. */
  546. static int isp_stat_buf_process(struct ispstat *stat, int buf_state)
  547. {
  548. int ret = STAT_NO_BUF;
  549. if (!atomic_add_unless(&stat->buf_err, -1, 0) &&
  550. buf_state == STAT_BUF_DONE && stat->state == ISPSTAT_ENABLED) {
  551. ret = isp_stat_buf_queue(stat);
  552. isp_stat_buf_next(stat);
  553. }
  554. return ret;
  555. }
  556. int omap3isp_stat_pcr_busy(struct ispstat *stat)
  557. {
  558. return stat->ops->busy(stat);
  559. }
  560. int omap3isp_stat_busy(struct ispstat *stat)
  561. {
  562. return omap3isp_stat_pcr_busy(stat) | stat->buf_processing |
  563. (stat->state != ISPSTAT_DISABLED);
  564. }
  565. /*
  566. * isp_stat_pcr_enable - Disables/Enables statistic engines.
  567. * @pcr_enable: 0/1 - Disables/Enables the engine.
  568. *
  569. * Must be called from ISP driver when the module is idle and synchronized
  570. * with CCDC.
  571. */
  572. static void isp_stat_pcr_enable(struct ispstat *stat, u8 pcr_enable)
  573. {
  574. if ((stat->state != ISPSTAT_ENABLING &&
  575. stat->state != ISPSTAT_ENABLED) && pcr_enable)
  576. /* Userspace has disabled the module. Aborting. */
  577. return;
  578. stat->ops->enable(stat, pcr_enable);
  579. if (stat->state == ISPSTAT_DISABLING && !pcr_enable)
  580. stat->state = ISPSTAT_DISABLED;
  581. else if (stat->state == ISPSTAT_ENABLING && pcr_enable)
  582. stat->state = ISPSTAT_ENABLED;
  583. }
  584. void omap3isp_stat_suspend(struct ispstat *stat)
  585. {
  586. unsigned long flags;
  587. spin_lock_irqsave(&stat->isp->stat_lock, flags);
  588. if (stat->state != ISPSTAT_DISABLED)
  589. stat->ops->enable(stat, 0);
  590. if (stat->state == ISPSTAT_ENABLED)
  591. stat->state = ISPSTAT_SUSPENDED;
  592. spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
  593. }
  594. void omap3isp_stat_resume(struct ispstat *stat)
  595. {
  596. /* Module will be re-enabled with its pipeline */
  597. if (stat->state == ISPSTAT_SUSPENDED)
  598. stat->state = ISPSTAT_ENABLING;
  599. }
  600. static void isp_stat_try_enable(struct ispstat *stat)
  601. {
  602. unsigned long irqflags;
  603. if (stat->priv == NULL)
  604. /* driver wasn't initialised */
  605. return;
  606. spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
  607. if (stat->state == ISPSTAT_ENABLING && !stat->buf_processing &&
  608. stat->buf_alloc_size) {
  609. /*
  610. * Userspace's requested to enable the engine but it wasn't yet.
  611. * Let's do that now.
  612. */
  613. stat->update = 1;
  614. isp_stat_buf_next(stat);
  615. stat->ops->setup_regs(stat, stat->priv);
  616. isp_stat_buf_insert_magic(stat, stat->active_buf);
  617. /*
  618. * H3A module has some hw issues which forces the driver to
  619. * ignore next buffers even if it was disabled in the meantime.
  620. * On the other hand, Histogram shouldn't ignore buffers anymore
  621. * if it's being enabled.
  622. */
  623. if (!IS_H3A(stat))
  624. atomic_set(&stat->buf_err, 0);
  625. isp_stat_pcr_enable(stat, 1);
  626. spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
  627. dev_dbg(stat->isp->dev, "%s: module is enabled.\n",
  628. stat->subdev.name);
  629. } else {
  630. spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
  631. }
  632. }
  633. void omap3isp_stat_isr_frame_sync(struct ispstat *stat)
  634. {
  635. isp_stat_try_enable(stat);
  636. }
  637. void omap3isp_stat_sbl_overflow(struct ispstat *stat)
  638. {
  639. unsigned long irqflags;
  640. spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
  641. /*
  642. * Due to a H3A hw issue which prevents the next buffer to start from
  643. * the correct memory address, 2 buffers must be ignored.
  644. */
  645. atomic_set(&stat->buf_err, 2);
  646. /*
  647. * If more than one SBL overflow happen in a row, H3A module may access
  648. * invalid memory region.
  649. * stat->sbl_ovl_recover is set to tell to the driver to temporarily use
  650. * a soft configuration which helps to avoid consecutive overflows.
  651. */
  652. if (stat->recover_priv)
  653. stat->sbl_ovl_recover = 1;
  654. spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
  655. }
  656. /*
  657. * omap3isp_stat_enable - Disable/Enable statistic engine as soon as possible
  658. * @enable: 0/1 - Disables/Enables the engine.
  659. *
  660. * Client should configure all the module registers before this.
  661. * This function can be called from a userspace request.
  662. */
  663. int omap3isp_stat_enable(struct ispstat *stat, u8 enable)
  664. {
  665. unsigned long irqflags;
  666. dev_dbg(stat->isp->dev, "%s: user wants to %s module.\n",
  667. stat->subdev.name, enable ? "enable" : "disable");
  668. /* Prevent enabling while configuring */
  669. mutex_lock(&stat->ioctl_lock);
  670. spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
  671. if (!stat->configured && enable) {
  672. spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
  673. mutex_unlock(&stat->ioctl_lock);
  674. dev_dbg(stat->isp->dev, "%s: cannot enable module as it's "
  675. "never been successfully configured so far.\n",
  676. stat->subdev.name);
  677. return -EINVAL;
  678. }
  679. if (enable) {
  680. if (stat->state == ISPSTAT_DISABLING)
  681. /* Previous disabling request wasn't done yet */
  682. stat->state = ISPSTAT_ENABLED;
  683. else if (stat->state == ISPSTAT_DISABLED)
  684. /* Module is now being enabled */
  685. stat->state = ISPSTAT_ENABLING;
  686. } else {
  687. if (stat->state == ISPSTAT_ENABLING) {
  688. /* Previous enabling request wasn't done yet */
  689. stat->state = ISPSTAT_DISABLED;
  690. } else if (stat->state == ISPSTAT_ENABLED) {
  691. /* Module is now being disabled */
  692. stat->state = ISPSTAT_DISABLING;
  693. isp_stat_buf_clear(stat);
  694. }
  695. }
  696. spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
  697. mutex_unlock(&stat->ioctl_lock);
  698. return 0;
  699. }
  700. int omap3isp_stat_s_stream(struct v4l2_subdev *subdev, int enable)
  701. {
  702. struct ispstat *stat = v4l2_get_subdevdata(subdev);
  703. if (enable) {
  704. /*
  705. * Only set enable PCR bit if the module was previously
  706. * enabled through ioct.
  707. */
  708. isp_stat_try_enable(stat);
  709. } else {
  710. unsigned long flags;
  711. /* Disable PCR bit and config enable field */
  712. omap3isp_stat_enable(stat, 0);
  713. spin_lock_irqsave(&stat->isp->stat_lock, flags);
  714. stat->ops->enable(stat, 0);
  715. spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
  716. /*
  717. * If module isn't busy, a new interrupt may come or not to
  718. * set the state to DISABLED. As Histogram needs to read its
  719. * internal memory to clear it, let interrupt handler
  720. * responsible of changing state to DISABLED. If the last
  721. * interrupt is coming, it's still safe as the handler will
  722. * ignore the second time when state is already set to DISABLED.
  723. * It's necessary to synchronize Histogram with streamoff, once
  724. * the module may be considered idle before last SDMA transfer
  725. * starts if we return here.
  726. */
  727. if (!omap3isp_stat_pcr_busy(stat))
  728. omap3isp_stat_isr(stat);
  729. dev_dbg(stat->isp->dev, "%s: module is being disabled\n",
  730. stat->subdev.name);
  731. }
  732. return 0;
  733. }
  734. /*
  735. * __stat_isr - Interrupt handler for statistic drivers
  736. */
  737. static void __stat_isr(struct ispstat *stat, int from_dma)
  738. {
  739. int ret = STAT_BUF_DONE;
  740. int buf_processing;
  741. unsigned long irqflags;
  742. struct isp_pipeline *pipe;
  743. /*
  744. * stat->buf_processing must be set before disable module. It's
  745. * necessary to not inform too early the buffers aren't busy in case
  746. * of SDMA is going to be used.
  747. */
  748. spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
  749. if (stat->state == ISPSTAT_DISABLED) {
  750. spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
  751. return;
  752. }
  753. buf_processing = stat->buf_processing;
  754. stat->buf_processing = 1;
  755. stat->ops->enable(stat, 0);
  756. if (buf_processing && !from_dma) {
  757. if (stat->state == ISPSTAT_ENABLED) {
  758. spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
  759. dev_err(stat->isp->dev,
  760. "%s: interrupt occurred when module was still "
  761. "processing a buffer.\n", stat->subdev.name);
  762. ret = STAT_NO_BUF;
  763. goto out;
  764. } else {
  765. /*
  766. * Interrupt handler was called from streamoff when
  767. * the module wasn't busy anymore to ensure it is being
  768. * disabled after process last buffer. If such buffer
  769. * processing has already started, no need to do
  770. * anything else.
  771. */
  772. spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
  773. return;
  774. }
  775. }
  776. spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
  777. /* If it's busy we can't process this buffer anymore */
  778. if (!omap3isp_stat_pcr_busy(stat)) {
  779. if (!from_dma && stat->ops->buf_process)
  780. /* Module still need to copy data to buffer. */
  781. ret = stat->ops->buf_process(stat);
  782. if (ret == STAT_BUF_WAITING_DMA)
  783. /* Buffer is not ready yet */
  784. return;
  785. spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
  786. /*
  787. * Histogram needs to read its internal memory to clear it
  788. * before be disabled. For that reason, common statistic layer
  789. * can return only after call stat's buf_process() operator.
  790. */
  791. if (stat->state == ISPSTAT_DISABLING) {
  792. stat->state = ISPSTAT_DISABLED;
  793. spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
  794. stat->buf_processing = 0;
  795. return;
  796. }
  797. pipe = to_isp_pipeline(&stat->subdev.entity);
  798. stat->frame_number = atomic_read(&pipe->frame_number);
  799. /*
  800. * Before this point, 'ret' stores the buffer's status if it's
  801. * ready to be processed. Afterwards, it holds the status if
  802. * it was processed successfully.
  803. */
  804. ret = isp_stat_buf_process(stat, ret);
  805. if (likely(!stat->sbl_ovl_recover)) {
  806. stat->ops->setup_regs(stat, stat->priv);
  807. } else {
  808. /*
  809. * Using recover config to increase the chance to have
  810. * a good buffer processing and make the H3A module to
  811. * go back to a valid state.
  812. */
  813. stat->update = 1;
  814. stat->ops->setup_regs(stat, stat->recover_priv);
  815. stat->sbl_ovl_recover = 0;
  816. /*
  817. * Set 'update' in case of the module needs to use
  818. * regular configuration after next buffer.
  819. */
  820. stat->update = 1;
  821. }
  822. isp_stat_buf_insert_magic(stat, stat->active_buf);
  823. /*
  824. * Hack: H3A modules may access invalid memory address or send
  825. * corrupted data to userspace if more than 1 SBL overflow
  826. * happens in a row without re-writing its buffer's start memory
  827. * address in the meantime. Such situation is avoided if the
  828. * module is not immediately re-enabled when the ISR misses the
  829. * timing to process the buffer and to setup the registers.
  830. * Because of that, pcr_enable(1) was moved to inside this 'if'
  831. * block. But the next interruption will still happen as during
  832. * pcr_enable(0) the module was busy.
  833. */
  834. isp_stat_pcr_enable(stat, 1);
  835. spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
  836. } else {
  837. /*
  838. * If a SBL overflow occurs and the H3A driver misses the timing
  839. * to process the buffer, stat->buf_err is set and won't be
  840. * cleared now. So the next buffer will be correctly ignored.
  841. * It's necessary due to a hw issue which makes the next H3A
  842. * buffer to start from the memory address where the previous
  843. * one stopped, instead of start where it was configured to.
  844. * Do not "stat->buf_err = 0" here.
  845. */
  846. if (stat->ops->buf_process)
  847. /*
  848. * Driver may need to erase current data prior to
  849. * process a new buffer. If it misses the timing, the
  850. * next buffer might be wrong. So should be ignored.
  851. * It happens only for Histogram.
  852. */
  853. atomic_set(&stat->buf_err, 1);
  854. ret = STAT_NO_BUF;
  855. dev_dbg(stat->isp->dev, "%s: cannot process buffer, "
  856. "device is busy.\n", stat->subdev.name);
  857. }
  858. out:
  859. stat->buf_processing = 0;
  860. isp_stat_queue_event(stat, ret != STAT_BUF_DONE);
  861. }
  862. void omap3isp_stat_isr(struct ispstat *stat)
  863. {
  864. __stat_isr(stat, 0);
  865. }
  866. void omap3isp_stat_dma_isr(struct ispstat *stat)
  867. {
  868. __stat_isr(stat, 1);
  869. }
  870. int omap3isp_stat_subscribe_event(struct v4l2_subdev *subdev,
  871. struct v4l2_fh *fh,
  872. struct v4l2_event_subscription *sub)
  873. {
  874. struct ispstat *stat = v4l2_get_subdevdata(subdev);
  875. if (sub->type != stat->event_type)
  876. return -EINVAL;
  877. return v4l2_event_subscribe(fh, sub, STAT_NEVENTS, NULL);
  878. }
  879. int omap3isp_stat_unsubscribe_event(struct v4l2_subdev *subdev,
  880. struct v4l2_fh *fh,
  881. struct v4l2_event_subscription *sub)
  882. {
  883. return v4l2_event_unsubscribe(fh, sub);
  884. }
  885. void omap3isp_stat_unregister_entities(struct ispstat *stat)
  886. {
  887. v4l2_device_unregister_subdev(&stat->subdev);
  888. }
  889. int omap3isp_stat_register_entities(struct ispstat *stat,
  890. struct v4l2_device *vdev)
  891. {
  892. return v4l2_device_register_subdev(vdev, &stat->subdev);
  893. }
  894. static int isp_stat_init_entities(struct ispstat *stat, const char *name,
  895. const struct v4l2_subdev_ops *sd_ops)
  896. {
  897. struct v4l2_subdev *subdev = &stat->subdev;
  898. struct media_entity *me = &subdev->entity;
  899. v4l2_subdev_init(subdev, sd_ops);
  900. snprintf(subdev->name, V4L2_SUBDEV_NAME_SIZE, "OMAP3 ISP %s", name);
  901. subdev->grp_id = 1 << 16; /* group ID for isp subdevs */
  902. subdev->flags |= V4L2_SUBDEV_FL_HAS_EVENTS | V4L2_SUBDEV_FL_HAS_DEVNODE;
  903. v4l2_set_subdevdata(subdev, stat);
  904. stat->pad.flags = MEDIA_PAD_FL_SINK;
  905. me->ops = NULL;
  906. return media_entity_init(me, 1, &stat->pad, 0);
  907. }
  908. int omap3isp_stat_init(struct ispstat *stat, const char *name,
  909. const struct v4l2_subdev_ops *sd_ops)
  910. {
  911. int ret;
  912. stat->buf = kcalloc(STAT_MAX_BUFS, sizeof(*stat->buf), GFP_KERNEL);
  913. if (!stat->buf)
  914. return -ENOMEM;
  915. isp_stat_buf_clear(stat);
  916. mutex_init(&stat->ioctl_lock);
  917. atomic_set(&stat->buf_err, 0);
  918. ret = isp_stat_init_entities(stat, name, sd_ops);
  919. if (ret < 0) {
  920. mutex_destroy(&stat->ioctl_lock);
  921. kfree(stat->buf);
  922. }
  923. return ret;
  924. }
  925. void omap3isp_stat_cleanup(struct ispstat *stat)
  926. {
  927. media_entity_cleanup(&stat->subdev.entity);
  928. mutex_destroy(&stat->ioctl_lock);
  929. isp_stat_bufs_free(stat);
  930. kfree(stat->buf);
  931. }