dma.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. typedef struct DMAport DMAport;
  7. typedef struct DMA DMA;
  8. typedef struct DMAxfer DMAxfer;
  9. /*
  10. * state of a dma transfer
  11. */
  12. struct DMAxfer
  13. {
  14. ulong bpa; /* bounce buffer physical address */
  15. void* bva; /* bounce buffer virtual address */
  16. int blen; /* bounce buffer length */
  17. void* va; /* virtual address destination/src */
  18. long len; /* bytes to be transferred */
  19. int isread;
  20. };
  21. /*
  22. * the dma controllers. the first half of this structure specifies
  23. * the I/O ports used by the DMA controllers.
  24. */
  25. struct DMAport
  26. {
  27. uchar addr[4]; /* current address (4 channels) */
  28. uchar count[4]; /* current count (4 channels) */
  29. uchar page[4]; /* page registers (4 channels) */
  30. uchar cmd; /* command status register */
  31. uchar req; /* request registers */
  32. uchar sbm; /* single bit mask register */
  33. uchar mode; /* mode register */
  34. uchar cbp; /* clear byte pointer */
  35. uchar mc; /* master clear */
  36. uchar cmask; /* clear mask register */
  37. uchar wam; /* write all mask register bit */
  38. };
  39. struct DMA
  40. {
  41. DMAport;
  42. int shift;
  43. Lock;
  44. DMAxfer x[4];
  45. };
  46. DMA dma[2] = {
  47. { 0x00, 0x02, 0x04, 0x06,
  48. 0x01, 0x03, 0x05, 0x07,
  49. 0x87, 0x83, 0x81, 0x82,
  50. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  51. 0 },
  52. { 0xc0, 0xc4, 0xc8, 0xcc,
  53. 0xc2, 0xc6, 0xca, 0xce,
  54. 0x8f, 0x8b, 0x89, 0x8a,
  55. 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde,
  56. 1 },
  57. };
  58. /*
  59. * DMA must be in the first 16MB. This gets called early by the
  60. * initialisation routines of any devices which require DMA to ensure
  61. * the allocated bounce buffers are below the 16MB limit.
  62. */
  63. int
  64. dmainit(int chan, int maxtransfer)
  65. {
  66. DMA *dp;
  67. DMAxfer *xp;
  68. static int once;
  69. if(once == 0){
  70. if(ioalloc(0x00, 0x10, 0, "dma") < 0
  71. || ioalloc(0x80, 0x10, 0, "dma") < 0
  72. || ioalloc(0xd0, 0x10, 0, "dma") < 0)
  73. panic("dmainit");
  74. once = 1;
  75. }
  76. if(maxtransfer > 64*1024)
  77. maxtransfer = 64*1024;
  78. dp = &dma[(chan>>2)&1];
  79. chan = chan & 3;
  80. xp = &dp->x[chan];
  81. if(xp->bva != nil){
  82. if(xp->blen < maxtransfer)
  83. return 1;
  84. return 0;
  85. }
  86. xp->bva = xspanalloc(maxtransfer, BY2PG, 64*1024);
  87. if(xp->bva == nil)
  88. return 1;
  89. xp->bpa = PADDR(xp->bva);
  90. if(xp->bpa >= 16*MB){
  91. /*
  92. * This will panic with the current
  93. * implementation of xspanalloc().
  94. xfree(xp->bva);
  95. */
  96. xp->bva = nil;
  97. return 1;
  98. }
  99. xp->blen = maxtransfer;
  100. xp->len = 0;
  101. xp->isread = 0;
  102. return 0;
  103. }
  104. /*
  105. * setup a dma transfer. if the destination is not in kernel
  106. * memory, allocate a page for the transfer.
  107. *
  108. * we assume BIOS has set up the command register before we
  109. * are booted.
  110. *
  111. * return the updated transfer length (we can't transfer across 64k
  112. * boundaries)
  113. */
  114. long
  115. dmasetup(int chan, void *va, long len, int isread)
  116. {
  117. DMA *dp;
  118. ulong pa;
  119. uchar mode;
  120. DMAxfer *xp;
  121. dp = &dma[(chan>>2)&1];
  122. chan = chan & 3;
  123. xp = &dp->x[chan];
  124. /*
  125. * if this isn't kernel memory or crossing 64k boundary or above 16 meg
  126. * use the bounce buffer.
  127. */
  128. pa = PADDR(va);
  129. if((((ulong)va)&0xF0000000) != KZERO
  130. || (pa&0xFFFF0000) != ((pa+len)&0xFFFF0000)
  131. || pa >= 16*MB) {
  132. if(xp->bva == nil)
  133. return -1;
  134. if(len > xp->blen)
  135. len = xp->blen;
  136. if(!isread)
  137. memmove(xp->bva, va, len);
  138. xp->va = va;
  139. xp->len = len;
  140. xp->isread = isread;
  141. pa = xp->bpa;
  142. }
  143. else
  144. xp->len = 0;
  145. /*
  146. * this setup must be atomic
  147. */
  148. ilock(dp);
  149. mode = (isread ? 0x44 : 0x48) | chan;
  150. outb(dp->mode, mode); /* single mode dma (give CPU a chance at mem) */
  151. outb(dp->page[chan], pa>>16);
  152. outb(dp->cbp, 0); /* set count & address to their first byte */
  153. outb(dp->addr[chan], pa>>dp->shift); /* set address */
  154. outb(dp->addr[chan], pa>>(8+dp->shift));
  155. outb(dp->count[chan], (len>>dp->shift)-1); /* set count */
  156. outb(dp->count[chan], ((len>>dp->shift)-1)>>8);
  157. outb(dp->sbm, chan); /* enable the channel */
  158. iunlock(dp);
  159. return len;
  160. }
  161. int
  162. dmadone(int chan)
  163. {
  164. DMA *dp;
  165. dp = &dma[(chan>>2)&1];
  166. chan = chan & 3;
  167. return inb(dp->cmd) & (1<<chan);
  168. }
  169. /*
  170. * this must be called after a dma has been completed.
  171. *
  172. * if a page has been allocated for the dma,
  173. * copy the data into the actual destination
  174. * and free the page.
  175. */
  176. void
  177. dmaend(int chan)
  178. {
  179. DMA *dp;
  180. DMAxfer *xp;
  181. dp = &dma[(chan>>2)&1];
  182. chan = chan & 3;
  183. /*
  184. * disable the channel
  185. */
  186. ilock(dp);
  187. outb(dp->sbm, 4|chan);
  188. iunlock(dp);
  189. xp = &dp->x[chan];
  190. if(xp->len == 0 || !xp->isread)
  191. return;
  192. /*
  193. * copy out of temporary page
  194. */
  195. memmove(xp->va, xp->bva, xp->len);
  196. xp->len = 0;
  197. }
  198. /*
  199. int
  200. dmacount(int chan)
  201. {
  202. int retval;
  203. DMA *dp;
  204. dp = &dma[(chan>>2)&1];
  205. outb(dp->cbp, 0);
  206. retval = inb(dp->count[chan]);
  207. retval |= inb(dp->count[chan]) << 8;
  208. return((retval<<dp->shift)+1);
  209. }
  210. */