dma.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. if((ulong)va < KZERO
  129. || ((pa=PADDR(va))&0xFFFF0000) != ((pa+len)&0xFFFF0000)
  130. || pa >= 16*MB){
  131. if(xp->bva == nil)
  132. return -1;
  133. if(len > xp->blen)
  134. len = xp->blen;
  135. if(!isread)
  136. memmove(xp->bva, va, len);
  137. xp->va = va;
  138. xp->len = len;
  139. xp->isread = isread;
  140. pa = xp->bpa;
  141. }
  142. else
  143. xp->len = 0;
  144. /*
  145. * this setup must be atomic
  146. */
  147. ilock(dp);
  148. mode = (isread ? 0x44 : 0x48) | chan;
  149. outb(dp->mode, mode); /* single mode dma (give CPU a chance at mem) */
  150. outb(dp->page[chan], pa>>16);
  151. outb(dp->cbp, 0); /* set count & address to their first byte */
  152. outb(dp->addr[chan], pa>>dp->shift); /* set address */
  153. outb(dp->addr[chan], pa>>(8+dp->shift));
  154. outb(dp->count[chan], (len>>dp->shift)-1); /* set count */
  155. outb(dp->count[chan], ((len>>dp->shift)-1)>>8);
  156. outb(dp->sbm, chan); /* enable the channel */
  157. iunlock(dp);
  158. return len;
  159. }
  160. int
  161. dmadone(int chan)
  162. {
  163. DMA *dp;
  164. dp = &dma[(chan>>2)&1];
  165. chan = chan & 3;
  166. return inb(dp->cmd) & (1<<chan);
  167. }
  168. /*
  169. * this must be called after a dma has been completed.
  170. *
  171. * if a page has been allocated for the dma,
  172. * copy the data into the actual destination
  173. * and free the page.
  174. */
  175. void
  176. dmaend(int chan)
  177. {
  178. DMA *dp;
  179. DMAxfer *xp;
  180. dp = &dma[(chan>>2)&1];
  181. chan = chan & 3;
  182. /*
  183. * disable the channel
  184. */
  185. ilock(dp);
  186. outb(dp->sbm, 4|chan);
  187. iunlock(dp);
  188. xp = &dp->x[chan];
  189. if(xp->len == 0 || !xp->isread)
  190. return;
  191. /*
  192. * copy out of temporary page
  193. */
  194. memmove(xp->va, xp->bva, xp->len);
  195. xp->len = 0;
  196. }
  197. /*
  198. int
  199. dmacount(int chan)
  200. {
  201. int retval;
  202. DMA *dp;
  203. dp = &dma[(chan>>2)&1];
  204. outb(dp->cbp, 0);
  205. retval = inb(dp->count[chan]);
  206. retval |= inb(dp->count[chan]) << 8;
  207. return((retval<<dp->shift)+1);
  208. }
  209. */