mga_dma.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. /* mga_dma.c -- DMA support for mga g200/g400 -*- linux-c -*-
  2. * Created: Mon Dec 13 01:50:01 1999 by jhartmann@precisioninsight.com
  3. *
  4. * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
  5. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  6. * All Rights Reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the next
  16. * paragraph) shall be included in all copies or substantial portions of the
  17. * Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  23. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  24. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  25. * DEALINGS IN THE SOFTWARE.
  26. */
  27. /**
  28. * \file mga_dma.c
  29. * DMA support for MGA G200 / G400.
  30. *
  31. * \author Rickard E. (Rik) Faith <faith@valinux.com>
  32. * \author Jeff Hartmann <jhartmann@valinux.com>
  33. * \author Keith Whitwell <keith@tungstengraphics.com>
  34. * \author Gareth Hughes <gareth@valinux.com>
  35. */
  36. #include "drmP.h"
  37. #include "drm.h"
  38. #include "drm_sarea.h"
  39. #include "mga_drm.h"
  40. #include "mga_drv.h"
  41. #define MGA_DEFAULT_USEC_TIMEOUT 10000
  42. #define MGA_FREELIST_DEBUG 0
  43. #define MINIMAL_CLEANUP 0
  44. #define FULL_CLEANUP 1
  45. static int mga_do_cleanup_dma(struct drm_device *dev, int full_cleanup);
  46. /* ================================================================
  47. * Engine control
  48. */
  49. int mga_do_wait_for_idle(drm_mga_private_t *dev_priv)
  50. {
  51. u32 status = 0;
  52. int i;
  53. DRM_DEBUG("\n");
  54. for (i = 0; i < dev_priv->usec_timeout; i++) {
  55. status = MGA_READ(MGA_STATUS) & MGA_ENGINE_IDLE_MASK;
  56. if (status == MGA_ENDPRDMASTS) {
  57. MGA_WRITE8(MGA_CRTC_INDEX, 0);
  58. return 0;
  59. }
  60. DRM_UDELAY(1);
  61. }
  62. #if MGA_DMA_DEBUG
  63. DRM_ERROR("failed!\n");
  64. DRM_INFO(" status=0x%08x\n", status);
  65. #endif
  66. return -EBUSY;
  67. }
  68. static int mga_do_dma_reset(drm_mga_private_t *dev_priv)
  69. {
  70. drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
  71. drm_mga_primary_buffer_t *primary = &dev_priv->prim;
  72. DRM_DEBUG("\n");
  73. /* The primary DMA stream should look like new right about now.
  74. */
  75. primary->tail = 0;
  76. primary->space = primary->size;
  77. primary->last_flush = 0;
  78. sarea_priv->last_wrap = 0;
  79. /* FIXME: Reset counters, buffer ages etc...
  80. */
  81. /* FIXME: What else do we need to reinitialize? WARP stuff?
  82. */
  83. return 0;
  84. }
  85. /* ================================================================
  86. * Primary DMA stream
  87. */
  88. void mga_do_dma_flush(drm_mga_private_t *dev_priv)
  89. {
  90. drm_mga_primary_buffer_t *primary = &dev_priv->prim;
  91. u32 head, tail;
  92. u32 status = 0;
  93. int i;
  94. DMA_LOCALS;
  95. DRM_DEBUG("\n");
  96. /* We need to wait so that we can do an safe flush */
  97. for (i = 0; i < dev_priv->usec_timeout; i++) {
  98. status = MGA_READ(MGA_STATUS) & MGA_ENGINE_IDLE_MASK;
  99. if (status == MGA_ENDPRDMASTS)
  100. break;
  101. DRM_UDELAY(1);
  102. }
  103. if (primary->tail == primary->last_flush) {
  104. DRM_DEBUG(" bailing out...\n");
  105. return;
  106. }
  107. tail = primary->tail + dev_priv->primary->offset;
  108. /* We need to pad the stream between flushes, as the card
  109. * actually (partially?) reads the first of these commands.
  110. * See page 4-16 in the G400 manual, middle of the page or so.
  111. */
  112. BEGIN_DMA(1);
  113. DMA_BLOCK(MGA_DMAPAD, 0x00000000,
  114. MGA_DMAPAD, 0x00000000,
  115. MGA_DMAPAD, 0x00000000, MGA_DMAPAD, 0x00000000);
  116. ADVANCE_DMA();
  117. primary->last_flush = primary->tail;
  118. head = MGA_READ(MGA_PRIMADDRESS);
  119. if (head <= tail)
  120. primary->space = primary->size - primary->tail;
  121. else
  122. primary->space = head - tail;
  123. DRM_DEBUG(" head = 0x%06lx\n", (unsigned long)(head - dev_priv->primary->offset));
  124. DRM_DEBUG(" tail = 0x%06lx\n", (unsigned long)(tail - dev_priv->primary->offset));
  125. DRM_DEBUG(" space = 0x%06x\n", primary->space);
  126. mga_flush_write_combine();
  127. MGA_WRITE(MGA_PRIMEND, tail | dev_priv->dma_access);
  128. DRM_DEBUG("done.\n");
  129. }
  130. void mga_do_dma_wrap_start(drm_mga_private_t *dev_priv)
  131. {
  132. drm_mga_primary_buffer_t *primary = &dev_priv->prim;
  133. u32 head, tail;
  134. DMA_LOCALS;
  135. DRM_DEBUG("\n");
  136. BEGIN_DMA_WRAP();
  137. DMA_BLOCK(MGA_DMAPAD, 0x00000000,
  138. MGA_DMAPAD, 0x00000000,
  139. MGA_DMAPAD, 0x00000000, MGA_DMAPAD, 0x00000000);
  140. ADVANCE_DMA();
  141. tail = primary->tail + dev_priv->primary->offset;
  142. primary->tail = 0;
  143. primary->last_flush = 0;
  144. primary->last_wrap++;
  145. head = MGA_READ(MGA_PRIMADDRESS);
  146. if (head == dev_priv->primary->offset)
  147. primary->space = primary->size;
  148. else
  149. primary->space = head - dev_priv->primary->offset;
  150. DRM_DEBUG(" head = 0x%06lx\n", (unsigned long)(head - dev_priv->primary->offset));
  151. DRM_DEBUG(" tail = 0x%06x\n", primary->tail);
  152. DRM_DEBUG(" wrap = %d\n", primary->last_wrap);
  153. DRM_DEBUG(" space = 0x%06x\n", primary->space);
  154. mga_flush_write_combine();
  155. MGA_WRITE(MGA_PRIMEND, tail | dev_priv->dma_access);
  156. set_bit(0, &primary->wrapped);
  157. DRM_DEBUG("done.\n");
  158. }
  159. void mga_do_dma_wrap_end(drm_mga_private_t *dev_priv)
  160. {
  161. drm_mga_primary_buffer_t *primary = &dev_priv->prim;
  162. drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
  163. u32 head = dev_priv->primary->offset;
  164. DRM_DEBUG("\n");
  165. sarea_priv->last_wrap++;
  166. DRM_DEBUG(" wrap = %d\n", sarea_priv->last_wrap);
  167. mga_flush_write_combine();
  168. MGA_WRITE(MGA_PRIMADDRESS, head | MGA_DMA_GENERAL);
  169. clear_bit(0, &primary->wrapped);
  170. DRM_DEBUG("done.\n");
  171. }
  172. /* ================================================================
  173. * Freelist management
  174. */
  175. #define MGA_BUFFER_USED (~0)
  176. #define MGA_BUFFER_FREE 0
  177. #if MGA_FREELIST_DEBUG
  178. static void mga_freelist_print(struct drm_device *dev)
  179. {
  180. drm_mga_private_t *dev_priv = dev->dev_private;
  181. drm_mga_freelist_t *entry;
  182. DRM_INFO("\n");
  183. DRM_INFO("current dispatch: last=0x%x done=0x%x\n",
  184. dev_priv->sarea_priv->last_dispatch,
  185. (unsigned int)(MGA_READ(MGA_PRIMADDRESS) -
  186. dev_priv->primary->offset));
  187. DRM_INFO("current freelist:\n");
  188. for (entry = dev_priv->head->next; entry; entry = entry->next) {
  189. DRM_INFO(" %p idx=%2d age=0x%x 0x%06lx\n",
  190. entry, entry->buf->idx, entry->age.head,
  191. (unsigned long)(entry->age.head - dev_priv->primary->offset));
  192. }
  193. DRM_INFO("\n");
  194. }
  195. #endif
  196. static int mga_freelist_init(struct drm_device *dev, drm_mga_private_t *dev_priv)
  197. {
  198. struct drm_device_dma *dma = dev->dma;
  199. struct drm_buf *buf;
  200. drm_mga_buf_priv_t *buf_priv;
  201. drm_mga_freelist_t *entry;
  202. int i;
  203. DRM_DEBUG("count=%d\n", dma->buf_count);
  204. dev_priv->head = kzalloc(sizeof(drm_mga_freelist_t), GFP_KERNEL);
  205. if (dev_priv->head == NULL)
  206. return -ENOMEM;
  207. SET_AGE(&dev_priv->head->age, MGA_BUFFER_USED, 0);
  208. for (i = 0; i < dma->buf_count; i++) {
  209. buf = dma->buflist[i];
  210. buf_priv = buf->dev_private;
  211. entry = kzalloc(sizeof(drm_mga_freelist_t), GFP_KERNEL);
  212. if (entry == NULL)
  213. return -ENOMEM;
  214. entry->next = dev_priv->head->next;
  215. entry->prev = dev_priv->head;
  216. SET_AGE(&entry->age, MGA_BUFFER_FREE, 0);
  217. entry->buf = buf;
  218. if (dev_priv->head->next != NULL)
  219. dev_priv->head->next->prev = entry;
  220. if (entry->next == NULL)
  221. dev_priv->tail = entry;
  222. buf_priv->list_entry = entry;
  223. buf_priv->discard = 0;
  224. buf_priv->dispatched = 0;
  225. dev_priv->head->next = entry;
  226. }
  227. return 0;
  228. }
  229. static void mga_freelist_cleanup(struct drm_device *dev)
  230. {
  231. drm_mga_private_t *dev_priv = dev->dev_private;
  232. drm_mga_freelist_t *entry;
  233. drm_mga_freelist_t *next;
  234. DRM_DEBUG("\n");
  235. entry = dev_priv->head;
  236. while (entry) {
  237. next = entry->next;
  238. kfree(entry);
  239. entry = next;
  240. }
  241. dev_priv->head = dev_priv->tail = NULL;
  242. }
  243. #if 0
  244. /* FIXME: Still needed?
  245. */
  246. static void mga_freelist_reset(struct drm_device *dev)
  247. {
  248. struct drm_device_dma *dma = dev->dma;
  249. struct drm_buf *buf;
  250. drm_mga_buf_priv_t *buf_priv;
  251. int i;
  252. for (i = 0; i < dma->buf_count; i++) {
  253. buf = dma->buflist[i];
  254. buf_priv = buf->dev_private;
  255. SET_AGE(&buf_priv->list_entry->age, MGA_BUFFER_FREE, 0);
  256. }
  257. }
  258. #endif
  259. static struct drm_buf *mga_freelist_get(struct drm_device * dev)
  260. {
  261. drm_mga_private_t *dev_priv = dev->dev_private;
  262. drm_mga_freelist_t *next;
  263. drm_mga_freelist_t *prev;
  264. drm_mga_freelist_t *tail = dev_priv->tail;
  265. u32 head, wrap;
  266. DRM_DEBUG("\n");
  267. head = MGA_READ(MGA_PRIMADDRESS);
  268. wrap = dev_priv->sarea_priv->last_wrap;
  269. DRM_DEBUG(" tail=0x%06lx %d\n",
  270. tail->age.head ?
  271. (unsigned long)(tail->age.head - dev_priv->primary->offset) : 0,
  272. tail->age.wrap);
  273. DRM_DEBUG(" head=0x%06lx %d\n",
  274. (unsigned long)(head - dev_priv->primary->offset), wrap);
  275. if (TEST_AGE(&tail->age, head, wrap)) {
  276. prev = dev_priv->tail->prev;
  277. next = dev_priv->tail;
  278. prev->next = NULL;
  279. next->prev = next->next = NULL;
  280. dev_priv->tail = prev;
  281. SET_AGE(&next->age, MGA_BUFFER_USED, 0);
  282. return next->buf;
  283. }
  284. DRM_DEBUG("returning NULL!\n");
  285. return NULL;
  286. }
  287. int mga_freelist_put(struct drm_device *dev, struct drm_buf *buf)
  288. {
  289. drm_mga_private_t *dev_priv = dev->dev_private;
  290. drm_mga_buf_priv_t *buf_priv = buf->dev_private;
  291. drm_mga_freelist_t *head, *entry, *prev;
  292. DRM_DEBUG("age=0x%06lx wrap=%d\n",
  293. (unsigned long)(buf_priv->list_entry->age.head -
  294. dev_priv->primary->offset),
  295. buf_priv->list_entry->age.wrap);
  296. entry = buf_priv->list_entry;
  297. head = dev_priv->head;
  298. if (buf_priv->list_entry->age.head == MGA_BUFFER_USED) {
  299. SET_AGE(&entry->age, MGA_BUFFER_FREE, 0);
  300. prev = dev_priv->tail;
  301. prev->next = entry;
  302. entry->prev = prev;
  303. entry->next = NULL;
  304. } else {
  305. prev = head->next;
  306. head->next = entry;
  307. prev->prev = entry;
  308. entry->prev = head;
  309. entry->next = prev;
  310. }
  311. return 0;
  312. }
  313. /* ================================================================
  314. * DMA initialization, cleanup
  315. */
  316. int mga_driver_load(struct drm_device *dev, unsigned long flags)
  317. {
  318. drm_mga_private_t *dev_priv;
  319. int ret;
  320. dev_priv = kzalloc(sizeof(drm_mga_private_t), GFP_KERNEL);
  321. if (!dev_priv)
  322. return -ENOMEM;
  323. dev->dev_private = (void *)dev_priv;
  324. dev_priv->usec_timeout = MGA_DEFAULT_USEC_TIMEOUT;
  325. dev_priv->chipset = flags;
  326. dev_priv->mmio_base = pci_resource_start(dev->pdev, 1);
  327. dev_priv->mmio_size = pci_resource_len(dev->pdev, 1);
  328. dev->counters += 3;
  329. dev->types[6] = _DRM_STAT_IRQ;
  330. dev->types[7] = _DRM_STAT_PRIMARY;
  331. dev->types[8] = _DRM_STAT_SECONDARY;
  332. ret = drm_vblank_init(dev, 1);
  333. if (ret) {
  334. (void) mga_driver_unload(dev);
  335. return ret;
  336. }
  337. return 0;
  338. }
  339. #if __OS_HAS_AGP
  340. /**
  341. * Bootstrap the driver for AGP DMA.
  342. *
  343. * \todo
  344. * Investigate whether there is any benefit to storing the WARP microcode in
  345. * AGP memory. If not, the microcode may as well always be put in PCI
  346. * memory.
  347. *
  348. * \todo
  349. * This routine needs to set dma_bs->agp_mode to the mode actually configured
  350. * in the hardware. Looking just at the Linux AGP driver code, I don't see
  351. * an easy way to determine this.
  352. *
  353. * \sa mga_do_dma_bootstrap, mga_do_pci_dma_bootstrap
  354. */
  355. static int mga_do_agp_dma_bootstrap(struct drm_device *dev,
  356. drm_mga_dma_bootstrap_t *dma_bs)
  357. {
  358. drm_mga_private_t *const dev_priv =
  359. (drm_mga_private_t *) dev->dev_private;
  360. unsigned int warp_size = MGA_WARP_UCODE_SIZE;
  361. int err;
  362. unsigned offset;
  363. const unsigned secondary_size = dma_bs->secondary_bin_count
  364. * dma_bs->secondary_bin_size;
  365. const unsigned agp_size = (dma_bs->agp_size << 20);
  366. struct drm_buf_desc req;
  367. struct drm_agp_mode mode;
  368. struct drm_agp_info info;
  369. struct drm_agp_buffer agp_req;
  370. struct drm_agp_binding bind_req;
  371. /* Acquire AGP. */
  372. err = drm_agp_acquire(dev);
  373. if (err) {
  374. DRM_ERROR("Unable to acquire AGP: %d\n", err);
  375. return err;
  376. }
  377. err = drm_agp_info(dev, &info);
  378. if (err) {
  379. DRM_ERROR("Unable to get AGP info: %d\n", err);
  380. return err;
  381. }
  382. mode.mode = (info.mode & ~0x07) | dma_bs->agp_mode;
  383. err = drm_agp_enable(dev, mode);
  384. if (err) {
  385. DRM_ERROR("Unable to enable AGP (mode = 0x%lx)\n", mode.mode);
  386. return err;
  387. }
  388. /* In addition to the usual AGP mode configuration, the G200 AGP cards
  389. * need to have the AGP mode "manually" set.
  390. */
  391. if (dev_priv->chipset == MGA_CARD_TYPE_G200) {
  392. if (mode.mode & 0x02)
  393. MGA_WRITE(MGA_AGP_PLL, MGA_AGP2XPLL_ENABLE);
  394. else
  395. MGA_WRITE(MGA_AGP_PLL, MGA_AGP2XPLL_DISABLE);
  396. }
  397. /* Allocate and bind AGP memory. */
  398. agp_req.size = agp_size;
  399. agp_req.type = 0;
  400. err = drm_agp_alloc(dev, &agp_req);
  401. if (err) {
  402. dev_priv->agp_size = 0;
  403. DRM_ERROR("Unable to allocate %uMB AGP memory\n",
  404. dma_bs->agp_size);
  405. return err;
  406. }
  407. dev_priv->agp_size = agp_size;
  408. dev_priv->agp_handle = agp_req.handle;
  409. bind_req.handle = agp_req.handle;
  410. bind_req.offset = 0;
  411. err = drm_agp_bind(dev, &bind_req);
  412. if (err) {
  413. DRM_ERROR("Unable to bind AGP memory: %d\n", err);
  414. return err;
  415. }
  416. /* Make drm_addbufs happy by not trying to create a mapping for less
  417. * than a page.
  418. */
  419. if (warp_size < PAGE_SIZE)
  420. warp_size = PAGE_SIZE;
  421. offset = 0;
  422. err = drm_addmap(dev, offset, warp_size,
  423. _DRM_AGP, _DRM_READ_ONLY, &dev_priv->warp);
  424. if (err) {
  425. DRM_ERROR("Unable to map WARP microcode: %d\n", err);
  426. return err;
  427. }
  428. offset += warp_size;
  429. err = drm_addmap(dev, offset, dma_bs->primary_size,
  430. _DRM_AGP, _DRM_READ_ONLY, &dev_priv->primary);
  431. if (err) {
  432. DRM_ERROR("Unable to map primary DMA region: %d\n", err);
  433. return err;
  434. }
  435. offset += dma_bs->primary_size;
  436. err = drm_addmap(dev, offset, secondary_size,
  437. _DRM_AGP, 0, &dev->agp_buffer_map);
  438. if (err) {
  439. DRM_ERROR("Unable to map secondary DMA region: %d\n", err);
  440. return err;
  441. }
  442. (void)memset(&req, 0, sizeof(req));
  443. req.count = dma_bs->secondary_bin_count;
  444. req.size = dma_bs->secondary_bin_size;
  445. req.flags = _DRM_AGP_BUFFER;
  446. req.agp_start = offset;
  447. err = drm_addbufs_agp(dev, &req);
  448. if (err) {
  449. DRM_ERROR("Unable to add secondary DMA buffers: %d\n", err);
  450. return err;
  451. }
  452. {
  453. struct drm_map_list *_entry;
  454. unsigned long agp_token = 0;
  455. list_for_each_entry(_entry, &dev->maplist, head) {
  456. if (_entry->map == dev->agp_buffer_map)
  457. agp_token = _entry->user_token;
  458. }
  459. if (!agp_token)
  460. return -EFAULT;
  461. dev->agp_buffer_token = agp_token;
  462. }
  463. offset += secondary_size;
  464. err = drm_addmap(dev, offset, agp_size - offset,
  465. _DRM_AGP, 0, &dev_priv->agp_textures);
  466. if (err) {
  467. DRM_ERROR("Unable to map AGP texture region %d\n", err);
  468. return err;
  469. }
  470. drm_core_ioremap(dev_priv->warp, dev);
  471. drm_core_ioremap(dev_priv->primary, dev);
  472. drm_core_ioremap(dev->agp_buffer_map, dev);
  473. if (!dev_priv->warp->handle ||
  474. !dev_priv->primary->handle || !dev->agp_buffer_map->handle) {
  475. DRM_ERROR("failed to ioremap agp regions! (%p, %p, %p)\n",
  476. dev_priv->warp->handle, dev_priv->primary->handle,
  477. dev->agp_buffer_map->handle);
  478. return -ENOMEM;
  479. }
  480. dev_priv->dma_access = MGA_PAGPXFER;
  481. dev_priv->wagp_enable = MGA_WAGP_ENABLE;
  482. DRM_INFO("Initialized card for AGP DMA.\n");
  483. return 0;
  484. }
  485. #else
  486. static int mga_do_agp_dma_bootstrap(struct drm_device *dev,
  487. drm_mga_dma_bootstrap_t *dma_bs)
  488. {
  489. return -EINVAL;
  490. }
  491. #endif
  492. /**
  493. * Bootstrap the driver for PCI DMA.
  494. *
  495. * \todo
  496. * The algorithm for decreasing the size of the primary DMA buffer could be
  497. * better. The size should be rounded up to the nearest page size, then
  498. * decrease the request size by a single page each pass through the loop.
  499. *
  500. * \todo
  501. * Determine whether the maximum address passed to drm_pci_alloc is correct.
  502. * The same goes for drm_addbufs_pci.
  503. *
  504. * \sa mga_do_dma_bootstrap, mga_do_agp_dma_bootstrap
  505. */
  506. static int mga_do_pci_dma_bootstrap(struct drm_device *dev,
  507. drm_mga_dma_bootstrap_t *dma_bs)
  508. {
  509. drm_mga_private_t *const dev_priv =
  510. (drm_mga_private_t *) dev->dev_private;
  511. unsigned int warp_size = MGA_WARP_UCODE_SIZE;
  512. unsigned int primary_size;
  513. unsigned int bin_count;
  514. int err;
  515. struct drm_buf_desc req;
  516. if (dev->dma == NULL) {
  517. DRM_ERROR("dev->dma is NULL\n");
  518. return -EFAULT;
  519. }
  520. /* Make drm_addbufs happy by not trying to create a mapping for less
  521. * than a page.
  522. */
  523. if (warp_size < PAGE_SIZE)
  524. warp_size = PAGE_SIZE;
  525. /* The proper alignment is 0x100 for this mapping */
  526. err = drm_addmap(dev, 0, warp_size, _DRM_CONSISTENT,
  527. _DRM_READ_ONLY, &dev_priv->warp);
  528. if (err != 0) {
  529. DRM_ERROR("Unable to create mapping for WARP microcode: %d\n",
  530. err);
  531. return err;
  532. }
  533. /* Other than the bottom two bits being used to encode other
  534. * information, there don't appear to be any restrictions on the
  535. * alignment of the primary or secondary DMA buffers.
  536. */
  537. for (primary_size = dma_bs->primary_size; primary_size != 0;
  538. primary_size >>= 1) {
  539. /* The proper alignment for this mapping is 0x04 */
  540. err = drm_addmap(dev, 0, primary_size, _DRM_CONSISTENT,
  541. _DRM_READ_ONLY, &dev_priv->primary);
  542. if (!err)
  543. break;
  544. }
  545. if (err != 0) {
  546. DRM_ERROR("Unable to allocate primary DMA region: %d\n", err);
  547. return -ENOMEM;
  548. }
  549. if (dev_priv->primary->size != dma_bs->primary_size) {
  550. DRM_INFO("Primary DMA buffer size reduced from %u to %u.\n",
  551. dma_bs->primary_size,
  552. (unsigned)dev_priv->primary->size);
  553. dma_bs->primary_size = dev_priv->primary->size;
  554. }
  555. for (bin_count = dma_bs->secondary_bin_count; bin_count > 0;
  556. bin_count--) {
  557. (void)memset(&req, 0, sizeof(req));
  558. req.count = bin_count;
  559. req.size = dma_bs->secondary_bin_size;
  560. err = drm_addbufs_pci(dev, &req);
  561. if (!err)
  562. break;
  563. }
  564. if (bin_count == 0) {
  565. DRM_ERROR("Unable to add secondary DMA buffers: %d\n", err);
  566. return err;
  567. }
  568. if (bin_count != dma_bs->secondary_bin_count) {
  569. DRM_INFO("Secondary PCI DMA buffer bin count reduced from %u "
  570. "to %u.\n", dma_bs->secondary_bin_count, bin_count);
  571. dma_bs->secondary_bin_count = bin_count;
  572. }
  573. dev_priv->dma_access = 0;
  574. dev_priv->wagp_enable = 0;
  575. dma_bs->agp_mode = 0;
  576. DRM_INFO("Initialized card for PCI DMA.\n");
  577. return 0;
  578. }
  579. static int mga_do_dma_bootstrap(struct drm_device *dev,
  580. drm_mga_dma_bootstrap_t *dma_bs)
  581. {
  582. const int is_agp = (dma_bs->agp_mode != 0) && drm_pci_device_is_agp(dev);
  583. int err;
  584. drm_mga_private_t *const dev_priv =
  585. (drm_mga_private_t *) dev->dev_private;
  586. dev_priv->used_new_dma_init = 1;
  587. /* The first steps are the same for both PCI and AGP based DMA. Map
  588. * the cards MMIO registers and map a status page.
  589. */
  590. err = drm_addmap(dev, dev_priv->mmio_base, dev_priv->mmio_size,
  591. _DRM_REGISTERS, _DRM_READ_ONLY, &dev_priv->mmio);
  592. if (err) {
  593. DRM_ERROR("Unable to map MMIO region: %d\n", err);
  594. return err;
  595. }
  596. err = drm_addmap(dev, 0, SAREA_MAX, _DRM_SHM,
  597. _DRM_READ_ONLY | _DRM_LOCKED | _DRM_KERNEL,
  598. &dev_priv->status);
  599. if (err) {
  600. DRM_ERROR("Unable to map status region: %d\n", err);
  601. return err;
  602. }
  603. /* The DMA initialization procedure is slightly different for PCI and
  604. * AGP cards. AGP cards just allocate a large block of AGP memory and
  605. * carve off portions of it for internal uses. The remaining memory
  606. * is returned to user-mode to be used for AGP textures.
  607. */
  608. if (is_agp)
  609. err = mga_do_agp_dma_bootstrap(dev, dma_bs);
  610. /* If we attempted to initialize the card for AGP DMA but failed,
  611. * clean-up any mess that may have been created.
  612. */
  613. if (err)
  614. mga_do_cleanup_dma(dev, MINIMAL_CLEANUP);
  615. /* Not only do we want to try and initialized PCI cards for PCI DMA,
  616. * but we also try to initialized AGP cards that could not be
  617. * initialized for AGP DMA. This covers the case where we have an AGP
  618. * card in a system with an unsupported AGP chipset. In that case the
  619. * card will be detected as AGP, but we won't be able to allocate any
  620. * AGP memory, etc.
  621. */
  622. if (!is_agp || err)
  623. err = mga_do_pci_dma_bootstrap(dev, dma_bs);
  624. return err;
  625. }
  626. int mga_dma_bootstrap(struct drm_device *dev, void *data,
  627. struct drm_file *file_priv)
  628. {
  629. drm_mga_dma_bootstrap_t *bootstrap = data;
  630. int err;
  631. static const int modes[] = { 0, 1, 2, 2, 4, 4, 4, 4 };
  632. const drm_mga_private_t *const dev_priv =
  633. (drm_mga_private_t *) dev->dev_private;
  634. err = mga_do_dma_bootstrap(dev, bootstrap);
  635. if (err) {
  636. mga_do_cleanup_dma(dev, FULL_CLEANUP);
  637. return err;
  638. }
  639. if (dev_priv->agp_textures != NULL) {
  640. bootstrap->texture_handle = dev_priv->agp_textures->offset;
  641. bootstrap->texture_size = dev_priv->agp_textures->size;
  642. } else {
  643. bootstrap->texture_handle = 0;
  644. bootstrap->texture_size = 0;
  645. }
  646. bootstrap->agp_mode = modes[bootstrap->agp_mode & 0x07];
  647. return err;
  648. }
  649. static int mga_do_init_dma(struct drm_device *dev, drm_mga_init_t *init)
  650. {
  651. drm_mga_private_t *dev_priv;
  652. int ret;
  653. DRM_DEBUG("\n");
  654. dev_priv = dev->dev_private;
  655. if (init->sgram)
  656. dev_priv->clear_cmd = MGA_DWGCTL_CLEAR | MGA_ATYPE_BLK;
  657. else
  658. dev_priv->clear_cmd = MGA_DWGCTL_CLEAR | MGA_ATYPE_RSTR;
  659. dev_priv->maccess = init->maccess;
  660. dev_priv->fb_cpp = init->fb_cpp;
  661. dev_priv->front_offset = init->front_offset;
  662. dev_priv->front_pitch = init->front_pitch;
  663. dev_priv->back_offset = init->back_offset;
  664. dev_priv->back_pitch = init->back_pitch;
  665. dev_priv->depth_cpp = init->depth_cpp;
  666. dev_priv->depth_offset = init->depth_offset;
  667. dev_priv->depth_pitch = init->depth_pitch;
  668. /* FIXME: Need to support AGP textures...
  669. */
  670. dev_priv->texture_offset = init->texture_offset[0];
  671. dev_priv->texture_size = init->texture_size[0];
  672. dev_priv->sarea = drm_getsarea(dev);
  673. if (!dev_priv->sarea) {
  674. DRM_ERROR("failed to find sarea!\n");
  675. return -EINVAL;
  676. }
  677. if (!dev_priv->used_new_dma_init) {
  678. dev_priv->dma_access = MGA_PAGPXFER;
  679. dev_priv->wagp_enable = MGA_WAGP_ENABLE;
  680. dev_priv->status = drm_core_findmap(dev, init->status_offset);
  681. if (!dev_priv->status) {
  682. DRM_ERROR("failed to find status page!\n");
  683. return -EINVAL;
  684. }
  685. dev_priv->mmio = drm_core_findmap(dev, init->mmio_offset);
  686. if (!dev_priv->mmio) {
  687. DRM_ERROR("failed to find mmio region!\n");
  688. return -EINVAL;
  689. }
  690. dev_priv->warp = drm_core_findmap(dev, init->warp_offset);
  691. if (!dev_priv->warp) {
  692. DRM_ERROR("failed to find warp microcode region!\n");
  693. return -EINVAL;
  694. }
  695. dev_priv->primary = drm_core_findmap(dev, init->primary_offset);
  696. if (!dev_priv->primary) {
  697. DRM_ERROR("failed to find primary dma region!\n");
  698. return -EINVAL;
  699. }
  700. dev->agp_buffer_token = init->buffers_offset;
  701. dev->agp_buffer_map =
  702. drm_core_findmap(dev, init->buffers_offset);
  703. if (!dev->agp_buffer_map) {
  704. DRM_ERROR("failed to find dma buffer region!\n");
  705. return -EINVAL;
  706. }
  707. drm_core_ioremap(dev_priv->warp, dev);
  708. drm_core_ioremap(dev_priv->primary, dev);
  709. drm_core_ioremap(dev->agp_buffer_map, dev);
  710. }
  711. dev_priv->sarea_priv =
  712. (drm_mga_sarea_t *) ((u8 *) dev_priv->sarea->handle +
  713. init->sarea_priv_offset);
  714. if (!dev_priv->warp->handle ||
  715. !dev_priv->primary->handle ||
  716. ((dev_priv->dma_access != 0) &&
  717. ((dev->agp_buffer_map == NULL) ||
  718. (dev->agp_buffer_map->handle == NULL)))) {
  719. DRM_ERROR("failed to ioremap agp regions!\n");
  720. return -ENOMEM;
  721. }
  722. ret = mga_warp_install_microcode(dev_priv);
  723. if (ret < 0) {
  724. DRM_ERROR("failed to install WARP ucode!: %d\n", ret);
  725. return ret;
  726. }
  727. ret = mga_warp_init(dev_priv);
  728. if (ret < 0) {
  729. DRM_ERROR("failed to init WARP engine!: %d\n", ret);
  730. return ret;
  731. }
  732. dev_priv->prim.status = (u32 *) dev_priv->status->handle;
  733. mga_do_wait_for_idle(dev_priv);
  734. /* Init the primary DMA registers.
  735. */
  736. MGA_WRITE(MGA_PRIMADDRESS, dev_priv->primary->offset | MGA_DMA_GENERAL);
  737. #if 0
  738. MGA_WRITE(MGA_PRIMPTR, virt_to_bus((void *)dev_priv->prim.status) | MGA_PRIMPTREN0 | /* Soft trap, SECEND, SETUPEND */
  739. MGA_PRIMPTREN1); /* DWGSYNC */
  740. #endif
  741. dev_priv->prim.start = (u8 *) dev_priv->primary->handle;
  742. dev_priv->prim.end = ((u8 *) dev_priv->primary->handle
  743. + dev_priv->primary->size);
  744. dev_priv->prim.size = dev_priv->primary->size;
  745. dev_priv->prim.tail = 0;
  746. dev_priv->prim.space = dev_priv->prim.size;
  747. dev_priv->prim.wrapped = 0;
  748. dev_priv->prim.last_flush = 0;
  749. dev_priv->prim.last_wrap = 0;
  750. dev_priv->prim.high_mark = 256 * DMA_BLOCK_SIZE;
  751. dev_priv->prim.status[0] = dev_priv->primary->offset;
  752. dev_priv->prim.status[1] = 0;
  753. dev_priv->sarea_priv->last_wrap = 0;
  754. dev_priv->sarea_priv->last_frame.head = 0;
  755. dev_priv->sarea_priv->last_frame.wrap = 0;
  756. if (mga_freelist_init(dev, dev_priv) < 0) {
  757. DRM_ERROR("could not initialize freelist\n");
  758. return -ENOMEM;
  759. }
  760. return 0;
  761. }
  762. static int mga_do_cleanup_dma(struct drm_device *dev, int full_cleanup)
  763. {
  764. int err = 0;
  765. DRM_DEBUG("\n");
  766. /* Make sure interrupts are disabled here because the uninstall ioctl
  767. * may not have been called from userspace and after dev_private
  768. * is freed, it's too late.
  769. */
  770. if (dev->irq_enabled)
  771. drm_irq_uninstall(dev);
  772. if (dev->dev_private) {
  773. drm_mga_private_t *dev_priv = dev->dev_private;
  774. if ((dev_priv->warp != NULL)
  775. && (dev_priv->warp->type != _DRM_CONSISTENT))
  776. drm_core_ioremapfree(dev_priv->warp, dev);
  777. if ((dev_priv->primary != NULL)
  778. && (dev_priv->primary->type != _DRM_CONSISTENT))
  779. drm_core_ioremapfree(dev_priv->primary, dev);
  780. if (dev->agp_buffer_map != NULL)
  781. drm_core_ioremapfree(dev->agp_buffer_map, dev);
  782. if (dev_priv->used_new_dma_init) {
  783. #if __OS_HAS_AGP
  784. if (dev_priv->agp_handle != 0) {
  785. struct drm_agp_binding unbind_req;
  786. struct drm_agp_buffer free_req;
  787. unbind_req.handle = dev_priv->agp_handle;
  788. drm_agp_unbind(dev, &unbind_req);
  789. free_req.handle = dev_priv->agp_handle;
  790. drm_agp_free(dev, &free_req);
  791. dev_priv->agp_textures = NULL;
  792. dev_priv->agp_size = 0;
  793. dev_priv->agp_handle = 0;
  794. }
  795. if ((dev->agp != NULL) && dev->agp->acquired)
  796. err = drm_agp_release(dev);
  797. #endif
  798. }
  799. dev_priv->warp = NULL;
  800. dev_priv->primary = NULL;
  801. dev_priv->sarea = NULL;
  802. dev_priv->sarea_priv = NULL;
  803. dev->agp_buffer_map = NULL;
  804. if (full_cleanup) {
  805. dev_priv->mmio = NULL;
  806. dev_priv->status = NULL;
  807. dev_priv->used_new_dma_init = 0;
  808. }
  809. memset(&dev_priv->prim, 0, sizeof(dev_priv->prim));
  810. dev_priv->warp_pipe = 0;
  811. memset(dev_priv->warp_pipe_phys, 0,
  812. sizeof(dev_priv->warp_pipe_phys));
  813. if (dev_priv->head != NULL)
  814. mga_freelist_cleanup(dev);
  815. }
  816. return err;
  817. }
  818. int mga_dma_init(struct drm_device *dev, void *data,
  819. struct drm_file *file_priv)
  820. {
  821. drm_mga_init_t *init = data;
  822. int err;
  823. LOCK_TEST_WITH_RETURN(dev, file_priv);
  824. switch (init->func) {
  825. case MGA_INIT_DMA:
  826. err = mga_do_init_dma(dev, init);
  827. if (err)
  828. (void)mga_do_cleanup_dma(dev, FULL_CLEANUP);
  829. return err;
  830. case MGA_CLEANUP_DMA:
  831. return mga_do_cleanup_dma(dev, FULL_CLEANUP);
  832. }
  833. return -EINVAL;
  834. }
  835. /* ================================================================
  836. * Primary DMA stream management
  837. */
  838. int mga_dma_flush(struct drm_device *dev, void *data,
  839. struct drm_file *file_priv)
  840. {
  841. drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
  842. struct drm_lock *lock = data;
  843. LOCK_TEST_WITH_RETURN(dev, file_priv);
  844. DRM_DEBUG("%s%s%s\n",
  845. (lock->flags & _DRM_LOCK_FLUSH) ? "flush, " : "",
  846. (lock->flags & _DRM_LOCK_FLUSH_ALL) ? "flush all, " : "",
  847. (lock->flags & _DRM_LOCK_QUIESCENT) ? "idle, " : "");
  848. WRAP_WAIT_WITH_RETURN(dev_priv);
  849. if (lock->flags & (_DRM_LOCK_FLUSH | _DRM_LOCK_FLUSH_ALL))
  850. mga_do_dma_flush(dev_priv);
  851. if (lock->flags & _DRM_LOCK_QUIESCENT) {
  852. #if MGA_DMA_DEBUG
  853. int ret = mga_do_wait_for_idle(dev_priv);
  854. if (ret < 0)
  855. DRM_INFO("-EBUSY\n");
  856. return ret;
  857. #else
  858. return mga_do_wait_for_idle(dev_priv);
  859. #endif
  860. } else {
  861. return 0;
  862. }
  863. }
  864. int mga_dma_reset(struct drm_device *dev, void *data,
  865. struct drm_file *file_priv)
  866. {
  867. drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
  868. LOCK_TEST_WITH_RETURN(dev, file_priv);
  869. return mga_do_dma_reset(dev_priv);
  870. }
  871. /* ================================================================
  872. * DMA buffer management
  873. */
  874. static int mga_dma_get_buffers(struct drm_device *dev,
  875. struct drm_file *file_priv, struct drm_dma *d)
  876. {
  877. struct drm_buf *buf;
  878. int i;
  879. for (i = d->granted_count; i < d->request_count; i++) {
  880. buf = mga_freelist_get(dev);
  881. if (!buf)
  882. return -EAGAIN;
  883. buf->file_priv = file_priv;
  884. if (DRM_COPY_TO_USER(&d->request_indices[i],
  885. &buf->idx, sizeof(buf->idx)))
  886. return -EFAULT;
  887. if (DRM_COPY_TO_USER(&d->request_sizes[i],
  888. &buf->total, sizeof(buf->total)))
  889. return -EFAULT;
  890. d->granted_count++;
  891. }
  892. return 0;
  893. }
  894. int mga_dma_buffers(struct drm_device *dev, void *data,
  895. struct drm_file *file_priv)
  896. {
  897. struct drm_device_dma *dma = dev->dma;
  898. drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
  899. struct drm_dma *d = data;
  900. int ret = 0;
  901. LOCK_TEST_WITH_RETURN(dev, file_priv);
  902. /* Please don't send us buffers.
  903. */
  904. if (d->send_count != 0) {
  905. DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n",
  906. DRM_CURRENTPID, d->send_count);
  907. return -EINVAL;
  908. }
  909. /* We'll send you buffers.
  910. */
  911. if (d->request_count < 0 || d->request_count > dma->buf_count) {
  912. DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n",
  913. DRM_CURRENTPID, d->request_count, dma->buf_count);
  914. return -EINVAL;
  915. }
  916. WRAP_TEST_WITH_RETURN(dev_priv);
  917. d->granted_count = 0;
  918. if (d->request_count)
  919. ret = mga_dma_get_buffers(dev, file_priv, d);
  920. return ret;
  921. }
  922. /**
  923. * Called just before the module is unloaded.
  924. */
  925. int mga_driver_unload(struct drm_device *dev)
  926. {
  927. kfree(dev->dev_private);
  928. dev->dev_private = NULL;
  929. return 0;
  930. }
  931. /**
  932. * Called when the last opener of the device is closed.
  933. */
  934. void mga_driver_lastclose(struct drm_device *dev)
  935. {
  936. mga_do_cleanup_dma(dev, FULL_CLEANUP);
  937. }
  938. int mga_driver_dma_quiescent(struct drm_device *dev)
  939. {
  940. drm_mga_private_t *dev_priv = dev->dev_private;
  941. return mga_do_wait_for_idle(dev_priv);
  942. }