NOTES 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. Memory Addressing
  2. =================
  3. There is 3 types of addresses: virtual, physical, and bus. For DMA a bus
  4. address is used. However, on x86 physical and bus addresses are the same (on
  5. other architectures it is not guaranteed). Anyway, this assumption is still
  6. used by xdma driver, it uses phiscal address for DMA access. I have ported
  7. in the same way. Now, we need to provide additionaly bus-addresses in kmem
  8. abstraction and use it in NWL DMA implementation.
  9. DMA Access Synchronization
  10. ==========================
  11. - At driver level, few types of buffers are supported:
  12. * SIMPLE - non-reusable buffers, the use infomation can be used for cleanup
  13. after crashed applications.
  14. * EXCLUSIVE - reusable buffers which can be mmaped by a single appliction
  15. only. There is two modes of these buffers:
  16. + Buffers in a STANDARD mode are created for a single DMA operation and
  17. if such buffer is detected while trying to reuse, the last operation
  18. has failed and reset is needed.
  19. + Buffers in a PERSISTENT mode are preserved between invocations of
  20. control application and cleaned up only after the PERSISTENT flag is
  21. removed
  22. * SHARED - reusable buffers shared by multiple processes. Not really
  23. needed at the moment.
  24. KMEM_FLAG_HW - indicates that buffer can be used by hardware, acually this
  25. means that DMA will be enabled afterwards. The driver is not able to check
  26. if it really was enable and therefore will block any attempt to release
  27. buffer until KMEM_HW_FLAG is passed to kmem_free routine as well. The later
  28. should only called with KMEM_HW_FLAG after the DMA engine is stopped. Then,
  29. the driver can be realesd by kmem_free if ref count reaches 0.
  30. KMEM_FLAG_EXCLUSIVE - prevents multiple processes mmaping the buffer
  31. simultaneously. This is used to prevent multiple processes use the same
  32. DMA engine at the same time. When passed to kmem_free, allows to clean
  33. buffers with lost clients even for shared buffers.
  34. KMEM_FLAG_REUSE - requires reuse of existing buffer. If reusable buffer is
  35. found (non-reusable buffers, i.e. allocated without KMEM_FLAG_REUSE are
  36. ignored), it is returned instead of allocation. Three types of usage
  37. counters are used. At moment of allocation, the HW reference is set if
  38. neccessary. The usage counter is increased by kmem_alloc function and
  39. decreased by kmem_free. Finally, the reference is obtained at returned
  40. during mmap/munmap. So, on kmem_free, we do not clean
  41. a) buffers with reference count above zero or hardware reference set.
  42. REUSE flag should be supplied, overwise the error is returned
  43. b) PERSISTENT buffer. REUSE flash should be supplied, overwise the
  44. error is returned
  45. c) non-exclusive buffers with usage counter above zero (For exclusive
  46. buffer the value of usage counter above zero just means that application
  47. have failed without cleaning buffers first. There is no easy way to
  48. detect that for shared buffers, so it is left as manual operation in
  49. this case)
  50. d) any buffer if KMEM_FLAG_REUSE was provided to function
  51. During module unload, only buffers with references can prevent cleanup. In
  52. this case the only possiblity to free the driver is to call kmem_free
  53. passing FORCE flags.
  54. KMEM_FLAG_PERSISTENT - if passed to allocation routine, changes mode of
  55. buffer to PERSISTENT, if passed to free routine, vice-versa changes mode
  56. of buffer to NORMAL. Basically, if we call 'pci --dma-start' this flag
  57. should be passed to alloc and if we call 'pci --dma-stop' it should be
  58. passed to free. In other case, the flag should not be present.
  59. If application crashed, the munmap while be still called cleaning software
  60. references. However, the hardware reference will stay since it is not clear
  61. if hardware channel was closed or not. To lift hardware reference, the
  62. application can be re-executed (or dma_stop called, for instance).
  63. * If there is no hardware reference, the buffers will be reused by next
  64. call to application and for EXCLUSIVE buffer cleaned at the end. For SHARED
  65. buffers they will be cleaned during module cleanup only (no active
  66. references).
  67. * The buffer will be reused by next call which can result in wrong behaviour
  68. if buffer left in incoherent stage. This should be handled on upper level.
  69. - At pcilib/kmem level synchronization of multiple buffers is performed
  70. * The HW reference and following modes should be consistent between member
  71. parts: REUSABLE, PERSISTENT, EXCLUSIVE (only HW reference and PERSISTENT
  72. mode should be checked, others are handled on dirver level)
  73. * It is fine if only part of buffers are reused and others are newly
  74. allocated. However, on higher level this can be checked and resulting
  75. in failure.
  76. Treatment of inconsistencies:
  77. * Buffers are in PRESISTENT mode, but newly allocated, OK
  78. * Buffers are reused, but are not in PERSISTENT mode (for EXCLUSIVE buffers
  79. this means that application has crashed during the last execution), OK
  80. * Some of buffers are reused (not just REUSABLE, but actually reused),
  81. others - not, OK until
  82. a) either PERSISTENT flag is set or reused buffers are non-PERSISTENT
  83. b) either HW flag is set or reused buffers does not hold HW reference
  84. * PERSISTENT mode inconsistency, FAIL (even if we are going to set
  85. PERSISTENT mode anyway)
  86. * HW reference inconsistency, FAIL (even if we are going to set
  87. HW flag anyway)
  88. On allocation error at some of the buffer, call clean routine and
  89. * Preserve PERSISTENT mode and HW reference if buffers held them before
  90. unsuccessful kmem initialization. Until the last failed block, the blocks
  91. of kmem should be consistent. The HW/PERSISTENT flags should be removed
  92. if all reused blocks were in HW/PERSISTENT mode. The last block needs
  93. special treatment. The flags may be removed for the block if it was
  94. HW/PERSISTENT state (and others not).
  95. * Remove REUSE flag, we want to clean if allowed by current buffer status
  96. * EXCLUSIVE flag is not important for kmem_free routine.
  97. - At DMA level
  98. There is 4 components of DMA access:
  99. * DMA engine enabled/disabled
  100. * DMA engine IRQs enabled/disabled - always enabled at startup
  101. * Memory buffers
  102. * Ring start/stop pointers
  103. To prevent multiple processes accessing DMA engine in parallel, the first
  104. action is buffer initialization which will fail if buffers already used
  105. * Always with REUSE, EXCLUSIVE, and HW flags
  106. * Optionally with PERSISTENT flag (if DMA_PERSISTENT flag is set)
  107. If another DMA app is running, the buffer allocation will fail (no dma_stop
  108. is executed in this case)
  109. Depending on PRESERVE flag, kmem_free will be called with REUSE flag
  110. keeping buffer in memory (this is redundant since HW flag is enough) or HW
  111. flag indicating that DMA engine is stopped and buffer could be cleaned.
  112. PERSISTENT flag is defined by DMA_PERSISTENT flag passed to stop routine.
  113. PRESERVE flag is enforced if DMA_PERSISTENT is not passed to dma_stop
  114. routine and either it:
  115. a) Explicitely set by DMA_PERMANENT flag passed to dma_start
  116. function
  117. b) Implicitely set if DMA engine is already enabled during dma_start,
  118. all buffers are reused, and are in persistent mode.
  119. If PRESERVE flag is on, the engine will not be stopped at the end of
  120. execution (and buffers will stay because of HW flag).
  121. If buffers are reused and are already in PERSISTENT mode, DMA engine was on
  122. before dma_start (PRESERVE flag is ignored, because it can be enforced),
  123. ring pointers are calculated from LAST_BD and states of ring elements.
  124. If previous application crashed (i.e. buffers may be corrupted). Two
  125. cases are possible:
  126. * If during the call buffers were in non-PERSISTENT mode, it can be
  127. easily detected - buffers are reused, but are not in PERSISTENT mode
  128. (or at least was not before we set them to). In this case we just
  129. reinitialize all buffers.
  130. * If during the call buffers were in PERSISTENT mode, it is up to
  131. user to check their consistency and restart DMA engine.]
  132. IRQs are enabled and disabled at each call
  133. DMA Reads
  134. =========
  135. standard: default reading mode, reads a single full packet
  136. multipacket: reads all available packets
  137. waiting multipacket: reads all available packets, after finishing the
  138. last one waiting if new data arrives
  139. exact read: read exactly specified number of bytes (should be
  140. only supported if it is multiple of packets, otherwise
  141. error should be returned)
  142. ignore packets: autoterminate each buffer, depends on engine
  143. configuration
  144. To handle differnt cases, the value returned by callback function instructs
  145. the DMA library how long to wait for the next data to appear before timing
  146. out. The following variants are possible:
  147. terminate: just bail out
  148. check: no timeout, just check if there is data, otherwise
  149. terminate
  150. timeout: standard DMA timeout, normaly used while receiving
  151. fragments of packet: in this case it is expected
  152. that device has already prepared data and only
  153. the performance of DMA engine limits transfer speed
  154. wait: wait until the data is prepared by the device, this
  155. timeout is specified as argument to the dma_stream
  156. function (standard DMA timeout is used by default)
  157. first | new_pkt | bufer
  158. --------------------------
  159. standard wait | term | timeout
  160. multiple packets wait | check | timeout - DMA_READ_FLAG_MULTIPACKET
  161. waiting multipacket wait | wait | timeout - DMA_READ_FLAG_WAIT
  162. exact wait | wait/term | timeout - limited by size parameter
  163. ignore packets wait | wait/check| wait/check - just autoterminated
  164. Shall we do a special handling in case of overflow?
  165. Buffering
  166. =========
  167. The DMA addresses are limited to 32 bits (~4GB for everything). This means we
  168. can't really use DMA pages are sole buffers. Therefore, a second thread, with
  169. a realtime scheduling policy if possible, will be spawned and will copy the
  170. data from the DMA pages into the allocated buffers. On expiration of duration
  171. or number of events set by autostop call, this thread will be stopped but
  172. processing in streaming mode will continue until all copyied data is passed
  173. to the callbacks.
  174. To avoid stalls, the IPECamera requires data to be read continuously read out.
  175. For this reason, there is no locks in the readout thread. It will simplify
  176. overwrite the old frames if data is not copied out timely. To handle this case
  177. after getting the data and processing it, the calling application should use
  178. return_data function and check return code. This function may return error
  179. indicating that the data was overwritten meanwhile. Hence, the data is
  180. corrupted and shoud be droped by the application. The copy_data function
  181. performs this check and user application can be sure it get coherent data
  182. in this case.
  183. There is a way to avoid this problem. For raw data, the rawdata callback
  184. can be requested. This callback blocks execution of readout thread and
  185. data may be treated safely by calling application. However, this may
  186. cause problems to electronics. Therefore, only memcpy should be performed
  187. on the data normally.
  188. The reconstructed data, however, may be safely accessed. As described above,
  189. the raw data will be continuously overwritten by the reader thread. However,
  190. reconstructed data, upon the get_data call, will be protected by the mutex.
  191. Register Access Synchronization
  192. ===============================
  193. We need to serialize access to the registers by the different running
  194. applications and handle case when registers are accessed indirectly by
  195. writting PCI BARs (DMA implementations, for instance).
  196. - Module-assisted locking:
  197. * During initialization the locking context is created (which is basicaly
  198. a kmem_handle of type LOCK_PAGE.
  199. * This locking context is passed to the kernel module along with lock type
  200. (LOCK_BANK) and lock item (BANK ADDRESS). If lock context is already owns
  201. lock on the specified bank, just reference number is increased, otherwise
  202. we are trying to obtain new lock.
  203. * Kernel module just iterates over all registered lock pages and checks if
  204. any holds the specified lock. if not, the lock is obtained and registered
  205. in the our lock page.
  206. * This allows to share access between multiple threads of single application
  207. (by using the same lock page) or protect (by using own lock pages by each of
  208. the threads)
  209. * Either on application cleanup or if application crashed, the memory mapping
  210. of lock page is removed and, hence, locks are freed.
  211. - Multiple-ways of accessing registers
  212. Because of reference counting, we can successfully obtain locks multiple
  213. times if necessary. The following locks are protecting register access:
  214. a) Global register_read/write lock bank before executing implementation
  215. b) DMA bank is locked by global DMA functions. So we can access the
  216. registers using plain PCI bar read/write.
  217. c) Sequence of register operations can be protected with pcilib_lock_bank
  218. function
  219. Reading raw register space or PCI bank is not locked.
  220. * Ok. We can detect banks which will be affected by PCI read/write and
  221. lock them. But shall we do it?
  222. Register/DMA Configuration
  223. ==========================
  224. - XML description of registers
  225. - Formal XML-based (or non XML-based) language for DMA implementation.
  226. a) Writting/Reading register values
  227. b) Wait until <register1>=<value> on <register2>=<value> report error
  228. c) ... ?
  229. IRQ Handling
  230. ============
  231. IRQ types: DMA IRQ, Event IRQ, other types
  232. IRQ hardware source: To allow purely user-space implementation, as general
  233. rule, only a single (standard) source should be used.
  234. IRQ source: The dma/event engines, however, may detail this hardware source
  235. and produce real IRQ source basing on the values of registers. For example,
  236. for DMA IRQs the source may present engine number and for Event IRQs the
  237. source may present event type.
  238. Only types can be enabled or disabled. The sources are enabled/disabled
  239. by enabling/disabling correspondent DMA engines or Event types. The expected
  240. workflow is following:
  241. * We enabling IRQs in user-space (normally setting some registers). Normally,
  242. just an Event IRQs, the DMA if necessary will be managed by DMA engine itself.
  243. * We waiting for standard IRQ from hardware (driver)
  244. * In the user space, we are checking registers to find out the real source
  245. of IRQ (driver reports us just hardware source), generating appropriate
  246. events, and acknowledge IRQ. This is dependent on implementation and should
  247. be managed inside event API.
  248. I.e. the driver implements just two methods pcilib_wait_irq(hw_source),
  249. pcilib_clear_irq(hw_source). Only a few hardware IRQ sources are defined.
  250. In most cirstumances, the IRQ_SOURCE_DEFAULT is used.
  251. The DMA engine may provide 3 additional methods, to enable, disable,
  252. and acknowledge IRQ.
  253. ... To be decided in details upon the need...
  254. Updating Firmware
  255. =================
  256. - JTag should be connected to USB connector on the board (next to Ethernet)
  257. - The computer should be tourned off and on before programming
  258. - The environment variable should be loaded
  259. . /home/uros/.bashrc
  260. - The application is called 'impact'
  261. No project is needed, cancel initial proposals (No/Cancel)
  262. Double-click on "Boundary Scan"
  263. Right click in the right window and select "Init Chain"
  264. We don't want to select bit file now (Yes and, then, click Cancel)
  265. Right click on second (right) item and choose "Assign new CF file"
  266. Select a bit file. Answer No, we don't want to attach SPI to SPI Prom
  267. Select xv6vlx240t and program it
  268. - Shutdown and start computer
  269. Firmware are in
  270. v.2: /home/uros/Repo/UFO2_last_good_version_UFO2.bit
  271. v.3: /home/uros/Repo/UFO3
  272. Step5 - best working revision
  273. Step6 - last revision