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
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
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:
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
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.
30 * DMA support for MGA G200 / G400.
32 * \author Rickard E. (Rik) Faith <faith@valinux.com>
33 * \author Jeff Hartmann <jhartmann@valinux.com>
34 * \author Keith Whitwell <keith@tungstengraphics.com>
35 * \author Gareth Hughes <gareth@valinux.com>
40 #include "drm_sarea.h"
44 #define MGA_DEFAULT_USEC_TIMEOUT 10000
45 #define MGA_FREELIST_DEBUG 0
47 static int mga_do_cleanup_dma( drm_device_t *dev );
49 /* ================================================================
53 int mga_do_wait_for_idle( drm_mga_private_t *dev_priv )
59 for ( i = 0 ; i < dev_priv->usec_timeout ; i++ ) {
60 status = MGA_READ( MGA_STATUS ) & MGA_ENGINE_IDLE_MASK;
61 if ( status == MGA_ENDPRDMASTS ) {
62 MGA_WRITE8( MGA_CRTC_INDEX, 0 );
69 DRM_ERROR( "failed!\n" );
70 DRM_INFO( " status=0x%08x\n", status );
72 return DRM_ERR(EBUSY);
75 static int mga_do_dma_reset( drm_mga_private_t *dev_priv )
77 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
78 drm_mga_primary_buffer_t *primary = &dev_priv->prim;
82 /* The primary DMA stream should look like new right about now.
85 primary->space = primary->size;
86 primary->last_flush = 0;
88 sarea_priv->last_wrap = 0;
90 /* FIXME: Reset counters, buffer ages etc...
93 /* FIXME: What else do we need to reinitialize? WARP stuff?
99 /* ================================================================
103 void mga_do_dma_flush( drm_mga_private_t *dev_priv )
105 drm_mga_primary_buffer_t *primary = &dev_priv->prim;
112 /* We need to wait so that we can do an safe flush */
113 for ( i = 0 ; i < dev_priv->usec_timeout ; i++ ) {
114 status = MGA_READ( MGA_STATUS ) & MGA_ENGINE_IDLE_MASK;
115 if ( status == MGA_ENDPRDMASTS ) break;
119 if ( primary->tail == primary->last_flush ) {
120 DRM_DEBUG( " bailing out...\n" );
124 tail = primary->tail + dev_priv->primary->offset;
126 /* We need to pad the stream between flushes, as the card
127 * actually (partially?) reads the first of these commands.
128 * See page 4-16 in the G400 manual, middle of the page or so.
132 DMA_BLOCK( MGA_DMAPAD, 0x00000000,
133 MGA_DMAPAD, 0x00000000,
134 MGA_DMAPAD, 0x00000000,
135 MGA_DMAPAD, 0x00000000 );
139 primary->last_flush = primary->tail;
141 head = MGA_READ( MGA_PRIMADDRESS );
143 if ( head <= tail ) {
144 primary->space = primary->size - primary->tail;
146 primary->space = head - tail;
149 DRM_DEBUG( " head = 0x%06lx\n", head - dev_priv->primary->offset );
150 DRM_DEBUG( " tail = 0x%06lx\n", tail - dev_priv->primary->offset );
151 DRM_DEBUG( " space = 0x%06x\n", primary->space );
153 mga_flush_write_combine();
154 MGA_WRITE(MGA_PRIMEND, tail | dev_priv->dma_access);
156 DRM_DEBUG( "done.\n" );
159 void mga_do_dma_wrap_start( drm_mga_private_t *dev_priv )
161 drm_mga_primary_buffer_t *primary = &dev_priv->prim;
168 DMA_BLOCK( MGA_DMAPAD, 0x00000000,
169 MGA_DMAPAD, 0x00000000,
170 MGA_DMAPAD, 0x00000000,
171 MGA_DMAPAD, 0x00000000 );
175 tail = primary->tail + dev_priv->primary->offset;
178 primary->last_flush = 0;
179 primary->last_wrap++;
181 head = MGA_READ( MGA_PRIMADDRESS );
183 if ( head == dev_priv->primary->offset ) {
184 primary->space = primary->size;
186 primary->space = head - dev_priv->primary->offset;
189 DRM_DEBUG( " head = 0x%06lx\n",
190 head - dev_priv->primary->offset );
191 DRM_DEBUG( " tail = 0x%06x\n", primary->tail );
192 DRM_DEBUG( " wrap = %d\n", primary->last_wrap );
193 DRM_DEBUG( " space = 0x%06x\n", primary->space );
195 mga_flush_write_combine();
196 MGA_WRITE(MGA_PRIMEND, tail | dev_priv->dma_access);
198 set_bit( 0, &primary->wrapped );
199 DRM_DEBUG( "done.\n" );
202 void mga_do_dma_wrap_end( drm_mga_private_t *dev_priv )
204 drm_mga_primary_buffer_t *primary = &dev_priv->prim;
205 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
206 u32 head = dev_priv->primary->offset;
209 sarea_priv->last_wrap++;
210 DRM_DEBUG( " wrap = %d\n", sarea_priv->last_wrap );
212 mga_flush_write_combine();
213 MGA_WRITE( MGA_PRIMADDRESS, head | MGA_DMA_GENERAL );
215 clear_bit( 0, &primary->wrapped );
216 DRM_DEBUG( "done.\n" );
220 /* ================================================================
221 * Freelist management
224 #define MGA_BUFFER_USED ~0
225 #define MGA_BUFFER_FREE 0
227 #if MGA_FREELIST_DEBUG
228 static void mga_freelist_print( drm_device_t *dev )
230 drm_mga_private_t *dev_priv = dev->dev_private;
231 drm_mga_freelist_t *entry;
234 DRM_INFO( "current dispatch: last=0x%x done=0x%x\n",
235 dev_priv->sarea_priv->last_dispatch,
236 (unsigned int)(MGA_READ( MGA_PRIMADDRESS ) -
237 dev_priv->primary->offset) );
238 DRM_INFO( "current freelist:\n" );
240 for ( entry = dev_priv->head->next ; entry ; entry = entry->next ) {
241 DRM_INFO( " %p idx=%2d age=0x%x 0x%06lx\n",
242 entry, entry->buf->idx, entry->age.head,
243 entry->age.head - dev_priv->primary->offset );
249 static int mga_freelist_init( drm_device_t *dev, drm_mga_private_t *dev_priv )
251 drm_device_dma_t *dma = dev->dma;
253 drm_mga_buf_priv_t *buf_priv;
254 drm_mga_freelist_t *entry;
256 DRM_DEBUG( "count=%d\n", dma->buf_count );
258 dev_priv->head = drm_alloc( sizeof(drm_mga_freelist_t),
260 if ( dev_priv->head == NULL )
261 return DRM_ERR(ENOMEM);
263 memset( dev_priv->head, 0, sizeof(drm_mga_freelist_t) );
264 SET_AGE( &dev_priv->head->age, MGA_BUFFER_USED, 0 );
266 for ( i = 0 ; i < dma->buf_count ; i++ ) {
267 buf = dma->buflist[i];
268 buf_priv = buf->dev_private;
270 entry = drm_alloc( sizeof(drm_mga_freelist_t),
273 return DRM_ERR(ENOMEM);
275 memset( entry, 0, sizeof(drm_mga_freelist_t) );
277 entry->next = dev_priv->head->next;
278 entry->prev = dev_priv->head;
279 SET_AGE( &entry->age, MGA_BUFFER_FREE, 0 );
282 if ( dev_priv->head->next != NULL )
283 dev_priv->head->next->prev = entry;
284 if ( entry->next == NULL )
285 dev_priv->tail = entry;
287 buf_priv->list_entry = entry;
288 buf_priv->discard = 0;
289 buf_priv->dispatched = 0;
291 dev_priv->head->next = entry;
297 static void mga_freelist_cleanup( drm_device_t *dev )
299 drm_mga_private_t *dev_priv = dev->dev_private;
300 drm_mga_freelist_t *entry;
301 drm_mga_freelist_t *next;
304 entry = dev_priv->head;
307 drm_free( entry, sizeof(drm_mga_freelist_t), DRM_MEM_DRIVER );
311 dev_priv->head = dev_priv->tail = NULL;
315 /* FIXME: Still needed?
317 static void mga_freelist_reset( drm_device_t *dev )
319 drm_device_dma_t *dma = dev->dma;
321 drm_mga_buf_priv_t *buf_priv;
324 for ( i = 0 ; i < dma->buf_count ; i++ ) {
325 buf = dma->buflist[i];
326 buf_priv = buf->dev_private;
327 SET_AGE( &buf_priv->list_entry->age,
328 MGA_BUFFER_FREE, 0 );
333 static drm_buf_t *mga_freelist_get( drm_device_t *dev )
335 drm_mga_private_t *dev_priv = dev->dev_private;
336 drm_mga_freelist_t *next;
337 drm_mga_freelist_t *prev;
338 drm_mga_freelist_t *tail = dev_priv->tail;
342 head = MGA_READ( MGA_PRIMADDRESS );
343 wrap = dev_priv->sarea_priv->last_wrap;
345 DRM_DEBUG( " tail=0x%06lx %d\n",
347 tail->age.head - dev_priv->primary->offset : 0,
349 DRM_DEBUG( " head=0x%06lx %d\n",
350 head - dev_priv->primary->offset, wrap );
352 if ( TEST_AGE( &tail->age, head, wrap ) ) {
353 prev = dev_priv->tail->prev;
354 next = dev_priv->tail;
356 next->prev = next->next = NULL;
357 dev_priv->tail = prev;
358 SET_AGE( &next->age, MGA_BUFFER_USED, 0 );
362 DRM_DEBUG( "returning NULL!\n" );
366 int mga_freelist_put( drm_device_t *dev, drm_buf_t *buf )
368 drm_mga_private_t *dev_priv = dev->dev_private;
369 drm_mga_buf_priv_t *buf_priv = buf->dev_private;
370 drm_mga_freelist_t *head, *entry, *prev;
372 DRM_DEBUG( "age=0x%06lx wrap=%d\n",
373 buf_priv->list_entry->age.head -
374 dev_priv->primary->offset,
375 buf_priv->list_entry->age.wrap );
377 entry = buf_priv->list_entry;
378 head = dev_priv->head;
380 if ( buf_priv->list_entry->age.head == MGA_BUFFER_USED ) {
381 SET_AGE( &entry->age, MGA_BUFFER_FREE, 0 );
382 prev = dev_priv->tail;
398 /* ================================================================
399 * DMA initialization, cleanup
403 int mga_driver_preinit(drm_device_t *dev, unsigned long flags)
405 drm_mga_private_t * dev_priv;
407 dev_priv = drm_alloc(sizeof(drm_mga_private_t), DRM_MEM_DRIVER);
409 return DRM_ERR(ENOMEM);
411 dev->dev_private = (void *)dev_priv;
412 memset(dev_priv, 0, sizeof(drm_mga_private_t));
414 dev_priv->usec_timeout = MGA_DEFAULT_USEC_TIMEOUT;
415 dev_priv->chipset = flags;
422 * Bootstrap the driver for AGP DMA.
425 * Investigate whether there is any benifit to storing the WARP microcode in
426 * AGP memory. If not, the microcode may as well always be put in PCI
430 * This routine needs to set dma_bs->agp_mode to the mode actually configured
431 * in the hardware. Looking just at the Linux AGP driver code, I don't see
432 * an easy way to determine this.
434 * \sa mga_do_dma_bootstrap, mga_do_pci_dma_bootstrap
436 static int mga_do_agp_dma_bootstrap(drm_device_t * dev,
437 drm_mga_dma_bootstrap_t * dma_bs)
439 drm_mga_private_t * const dev_priv = (drm_mga_private_t *) dev->dev_private;
440 unsigned int warp_size = mga_warp_microcode_size(dev_priv);
443 const unsigned secondary_size = dma_bs->secondary_bin_count
444 * dma_bs->secondary_bin_size;
445 const unsigned agp_size = (dma_bs->agp_size << 20);
452 err = drm_agp_acquire(dev);
454 DRM_ERROR("Unable to acquire AGP\n");
458 err = drm_agp_info(dev, &info);
460 DRM_ERROR("Unable to get AGP info\n");
464 mode.mode = (info.mode & ~0x07) | dma_bs->agp_mode;
465 err = drm_agp_enable(dev, mode);
467 DRM_ERROR("Unable to enable AGP (mode = 0x%lx)\n", mode.mode);
472 /* In addition to the usual AGP mode configuration, the G200 AGP cards
473 * need to have the AGP mode "manually" set.
476 if (dev_priv->chipset == MGA_CARD_TYPE_G200) {
477 if (mode.mode & 0x02) {
478 MGA_WRITE(MGA_AGP_PLL, MGA_AGP2XPLL_ENABLE);
481 MGA_WRITE(MGA_AGP_PLL, MGA_AGP2XPLL_DISABLE);
486 /* Allocate and bind AGP memory. */
487 dev_priv->agp_pages = agp_size / PAGE_SIZE;
488 dev_priv->agp_mem = drm_alloc_agp( dev, dev_priv->agp_pages, 0 );
489 if (dev_priv->agp_mem == NULL) {
490 dev_priv->agp_pages = 0;
491 DRM_ERROR("Unable to allocate %uMB AGP memory\n",
493 return DRM_ERR(ENOMEM);
496 err = drm_bind_agp( dev_priv->agp_mem, 0 );
498 DRM_ERROR("Unable to bind AGP memory\n");
502 /* Make drm_addbufs happy by not trying to create a mapping for less
505 if (warp_size < PAGE_SIZE)
506 warp_size = PAGE_SIZE;
509 err = drm_addmap( dev, offset, warp_size,
510 _DRM_AGP, _DRM_READ_ONLY, & dev_priv->warp );
512 DRM_ERROR("Unable to map WARP microcode\n");
517 err = drm_addmap( dev, offset, dma_bs->primary_size,
518 _DRM_AGP, _DRM_READ_ONLY, & dev_priv->primary );
520 DRM_ERROR("Unable to map primary DMA region\n");
524 offset += dma_bs->primary_size;
525 err = drm_addmap( dev, offset, secondary_size,
526 _DRM_AGP, 0, & dev->agp_buffer_map );
528 DRM_ERROR("Unable to map secondary DMA region\n");
532 (void) memset( &req, 0, sizeof(req) );
533 req.count = dma_bs->secondary_bin_count;
534 req.size = dma_bs->secondary_bin_size;
535 req.flags = _DRM_AGP_BUFFER;
536 req.agp_start = offset;
538 err = drm_addbufs_agp( dev, & req );
540 DRM_ERROR("Unable to add secondary DMA buffers\n");
544 offset += secondary_size;
545 err = drm_addmap( dev, offset, agp_size - offset,
546 _DRM_AGP, 0, & dev_priv->agp_textures );
548 DRM_ERROR("Unable to map AGP texture region\n");
552 drm_core_ioremap(dev_priv->warp, dev);
553 drm_core_ioremap(dev_priv->primary, dev);
554 drm_core_ioremap(dev->agp_buffer_map, dev);
556 if (!dev_priv->warp->handle ||
557 !dev_priv->primary->handle || !dev->agp_buffer_map->handle) {
558 DRM_ERROR("failed to ioremap agp regions! (%p, %p, %p)\n",
559 dev_priv->warp->handle, dev_priv->primary->handle,
560 dev->agp_buffer_map->handle);
561 return DRM_ERR(ENOMEM);
564 dev_priv->dma_access = MGA_PAGPXFER;
565 dev_priv->wagp_enable = MGA_WAGP_ENABLE;
567 DRM_INFO("Initialized card for AGP DMA.\n");
571 static int mga_do_agp_dma_bootstrap(drm_device_t * dev,
572 drm_mga_dma_bootstrap_t * dma_bs)
579 * Bootstrap the driver for PCI DMA.
582 * The algorithm for decreasing the size of the primary DMA buffer could be
583 * better. The size should be rounded up to the nearest page size, then
584 * decrease the request size by a single page each pass through the loop.
587 * Determine whether the maximum address passed to drm_pci_alloc is correct.
588 * The same goes for drm_addbufs_pci.
590 * \sa mga_do_dma_bootstrap, mga_do_agp_dma_bootstrap
592 static int mga_do_pci_dma_bootstrap(drm_device_t * dev,
593 drm_mga_dma_bootstrap_t * dma_bs)
595 drm_mga_private_t * const dev_priv = (drm_mga_private_t *) dev->dev_private;
596 unsigned int warp_size = mga_warp_microcode_size(dev_priv);
597 unsigned int primary_size;
598 unsigned int bin_count;
603 if (dev->dma == NULL) {
604 DRM_ERROR("dev->dma is NULL\n");
605 return DRM_ERR(EFAULT);
608 /* Make drm_addbufs happy by not trying to create a mapping for less
611 if (warp_size < PAGE_SIZE)
612 warp_size = PAGE_SIZE;
614 /* The proper alignment is 0x100 for this mapping */
615 err = drm_addmap(dev, 0, warp_size, _DRM_CONSISTENT,
616 _DRM_READ_ONLY, &dev_priv->warp);
618 DRM_ERROR("Unable to create mapping for WARP microcode\n");
622 /* Other than the bottom two bits being used to encode other
623 * information, there don't appear to be any restrictions on the
624 * alignment of the primary or secondary DMA buffers.
627 for ( primary_size = dma_bs->primary_size
629 ; primary_size >>= 1 ) {
630 /* The proper alignment for this mapping is 0x04 */
631 err = drm_addmap(dev, 0, primary_size, _DRM_CONSISTENT,
632 _DRM_READ_ONLY, &dev_priv->primary);
638 DRM_ERROR("Unable to allocate primary DMA region\n");
639 return DRM_ERR(ENOMEM);
642 if (dev_priv->primary->size != dma_bs->primary_size) {
643 DRM_INFO("Primary DMA buffer size reduced from %u to %u.\n",
644 dma_bs->primary_size,
645 (unsigned) dev_priv->primary->size);
646 dma_bs->primary_size = dev_priv->primary->size;
649 for ( bin_count = dma_bs->secondary_bin_count
652 (void) memset( &req, 0, sizeof(req) );
653 req.count = bin_count;
654 req.size = dma_bs->secondary_bin_size;
656 err = drm_addbufs_pci( dev, & req );
662 if (bin_count == 0) {
663 DRM_ERROR("Unable to add secondary DMA buffers\n");
667 if (bin_count != dma_bs->secondary_bin_count) {
668 DRM_INFO("Secondary PCI DMA buffer bin count reduced from %u "
669 "to %u.\n", dma_bs->secondary_bin_count, bin_count);
671 dma_bs->secondary_bin_count = bin_count;
674 dev_priv->dma_access = 0;
675 dev_priv->wagp_enable = 0;
677 dma_bs->agp_mode = 0;
679 DRM_INFO("Initialized card for PCI DMA.\n");
684 static int mga_do_dma_bootstrap(drm_device_t * dev,
685 drm_mga_dma_bootstrap_t * dma_bs)
687 const int is_agp = (dma_bs->agp_mode != 0) && drm_device_is_agp(dev);
689 drm_mga_private_t * const dev_priv =
690 (drm_mga_private_t *) dev->dev_private;
693 dev_priv->used_new_dma_init = 1;
695 /* The first steps are the same for both PCI and AGP based DMA. Map
696 * the cards MMIO registers and map a status page.
698 err = drm_addmap( dev, dev_priv->mmio_base, dev_priv->mmio_size,
699 _DRM_REGISTERS, _DRM_READ_ONLY, & dev_priv->mmio );
701 DRM_ERROR("Unable to map MMIO region\n");
706 err = drm_addmap( dev, 0, SAREA_MAX, _DRM_SHM,
707 _DRM_READ_ONLY | _DRM_LOCKED | _DRM_KERNEL,
708 & dev_priv->status );
710 DRM_ERROR("Unable to map status region\n");
715 /* The DMA initialization procedure is slightly different for PCI and
716 * AGP cards. AGP cards just allocate a large block of AGP memory and
717 * carve off portions of it for internal uses. The remaining memory
718 * is returned to user-mode to be used for AGP textures.
721 err = mga_do_agp_dma_bootstrap(dev, dma_bs);
724 /* If we attempted to initialize the card for AGP DMA but failed,
725 * clean-up any mess that may have been created.
729 mga_do_cleanup_dma(dev);
733 /* Not only do we want to try and initialized PCI cards for PCI DMA,
734 * but we also try to initialized AGP cards that could not be
735 * initialized for AGP DMA. This covers the case where we have an AGP
736 * card in a system with an unsupported AGP chipset. In that case the
737 * card will be detected as AGP, but we won't be able to allocate any
741 if (!is_agp || err) {
742 err = mga_do_pci_dma_bootstrap(dev, dma_bs);
749 int mga_dma_bootstrap(DRM_IOCTL_ARGS)
752 drm_mga_dma_bootstrap_t bootstrap;
756 DRM_COPY_FROM_USER_IOCTL(bootstrap,
757 (drm_mga_dma_bootstrap_t __user *) data,
760 err = mga_do_dma_bootstrap(dev, & bootstrap);
762 static const int modes[] = { 0, 1, 2, 2, 4, 4, 4, 4 };
763 const drm_mga_private_t * const dev_priv =
764 (drm_mga_private_t *) dev->dev_private;
766 if (dev_priv->agp_textures != NULL) {
767 bootstrap.texture_handle = dev_priv->agp_textures->offset;
768 bootstrap.texture_size = dev_priv->agp_textures->size;
771 bootstrap.texture_handle = 0;
772 bootstrap.texture_size = 0;
775 bootstrap.agp_mode = modes[ bootstrap.agp_mode & 0x07 ];
776 if (DRM_COPY_TO_USER( (void __user *) data, & bootstrap,
777 sizeof(bootstrap))) {
778 err = DRM_ERR(EFAULT);
782 mga_do_cleanup_dma(dev);
788 static int mga_do_init_dma( drm_device_t *dev, drm_mga_init_t *init )
790 drm_mga_private_t *dev_priv;
795 dev_priv = dev->dev_private;
798 dev_priv->clear_cmd = MGA_DWGCTL_CLEAR | MGA_ATYPE_BLK;
800 dev_priv->clear_cmd = MGA_DWGCTL_CLEAR | MGA_ATYPE_RSTR;
802 dev_priv->maccess = init->maccess;
804 dev_priv->fb_cpp = init->fb_cpp;
805 dev_priv->front_offset = init->front_offset;
806 dev_priv->front_pitch = init->front_pitch;
807 dev_priv->back_offset = init->back_offset;
808 dev_priv->back_pitch = init->back_pitch;
810 dev_priv->depth_cpp = init->depth_cpp;
811 dev_priv->depth_offset = init->depth_offset;
812 dev_priv->depth_pitch = init->depth_pitch;
814 /* FIXME: Need to support AGP textures...
816 dev_priv->texture_offset = init->texture_offset[0];
817 dev_priv->texture_size = init->texture_size[0];
821 if (!dev_priv->sarea) {
822 DRM_ERROR("failed to find sarea!\n");
823 return DRM_ERR(EINVAL);
826 if (! dev_priv->used_new_dma_init) {
828 dev_priv->dma_access = MGA_PAGPXFER;
829 dev_priv->wagp_enable = MGA_WAGP_ENABLE;
831 dev_priv->status = drm_core_findmap(dev, init->status_offset);
832 if (!dev_priv->status) {
833 DRM_ERROR("failed to find status page!\n");
834 return DRM_ERR(EINVAL);
836 dev_priv->mmio = drm_core_findmap(dev, init->mmio_offset);
837 if (!dev_priv->mmio) {
838 DRM_ERROR("failed to find mmio region!\n");
839 return DRM_ERR(EINVAL);
841 dev_priv->warp = drm_core_findmap(dev, init->warp_offset);
842 if (!dev_priv->warp) {
843 DRM_ERROR("failed to find warp microcode region!\n");
844 return DRM_ERR(EINVAL);
846 dev_priv->primary = drm_core_findmap(dev, init->primary_offset);
847 if (!dev_priv->primary) {
848 DRM_ERROR("failed to find primary dma region!\n");
849 return DRM_ERR(EINVAL);
851 dev->agp_buffer_token = init->buffers_offset;
852 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
853 if (!dev->agp_buffer_map) {
854 DRM_ERROR("failed to find dma buffer region!\n");
855 return DRM_ERR(EINVAL);
858 drm_core_ioremap(dev_priv->warp, dev);
859 drm_core_ioremap(dev_priv->primary, dev);
860 drm_core_ioremap(dev->agp_buffer_map, dev);
863 dev_priv->sarea_priv =
864 (drm_mga_sarea_t *)((u8 *)dev_priv->sarea->handle +
865 init->sarea_priv_offset);
867 if (!dev_priv->warp->handle ||
868 !dev_priv->primary->handle ||
869 ((dev_priv->dma_access != 0) &&
870 ((dev->agp_buffer_map == NULL) ||
871 (dev->agp_buffer_map->handle == NULL)))) {
872 DRM_ERROR("failed to ioremap agp regions!\n");
873 return DRM_ERR(ENOMEM);
876 ret = mga_warp_install_microcode(dev_priv);
878 DRM_ERROR("failed to install WARP ucode!\n");
882 ret = mga_warp_init(dev_priv);
884 DRM_ERROR("failed to init WARP engine!\n");
888 dev_priv->prim.status = (u32 *)dev_priv->status->handle;
890 mga_do_wait_for_idle( dev_priv );
892 /* Init the primary DMA registers.
894 MGA_WRITE( MGA_PRIMADDRESS,
895 dev_priv->primary->offset | MGA_DMA_GENERAL );
897 MGA_WRITE( MGA_PRIMPTR,
898 virt_to_bus((void *)dev_priv->prim.status) |
899 MGA_PRIMPTREN0 | /* Soft trap, SECEND, SETUPEND */
900 MGA_PRIMPTREN1 ); /* DWGSYNC */
903 dev_priv->prim.start = (u8 *)dev_priv->primary->handle;
904 dev_priv->prim.end = ((u8 *)dev_priv->primary->handle
905 + dev_priv->primary->size);
906 dev_priv->prim.size = dev_priv->primary->size;
908 dev_priv->prim.tail = 0;
909 dev_priv->prim.space = dev_priv->prim.size;
910 dev_priv->prim.wrapped = 0;
912 dev_priv->prim.last_flush = 0;
913 dev_priv->prim.last_wrap = 0;
915 dev_priv->prim.high_mark = 256 * DMA_BLOCK_SIZE;
917 dev_priv->prim.status[0] = dev_priv->primary->offset;
918 dev_priv->prim.status[1] = 0;
920 dev_priv->sarea_priv->last_wrap = 0;
921 dev_priv->sarea_priv->last_frame.head = 0;
922 dev_priv->sarea_priv->last_frame.wrap = 0;
924 if (mga_freelist_init(dev, dev_priv) < 0) {
925 DRM_ERROR("could not initialize freelist\n");
926 return DRM_ERR(ENOMEM);
932 static int mga_do_cleanup_dma( drm_device_t *dev )
937 /* Make sure interrupts are disabled here because the uninstall ioctl
938 * may not have been called from userspace and after dev_private
939 * is freed, it's too late.
941 if ( dev->irq_enabled ) drm_irq_uninstall(dev);
943 if ( dev->dev_private ) {
944 drm_mga_private_t *dev_priv = dev->dev_private;
946 if ((dev_priv->warp != NULL)
947 && (dev_priv->warp->type != _DRM_CONSISTENT))
948 drm_core_ioremapfree(dev_priv->warp, dev);
950 if ((dev_priv->primary != NULL)
951 && (dev_priv->primary->type != _DRM_CONSISTENT))
952 drm_core_ioremapfree(dev_priv->primary, dev);
954 if (dev->agp_buffer_map != NULL)
955 drm_core_ioremapfree(dev->agp_buffer_map, dev);
957 if (dev_priv->used_new_dma_init) {
959 if (dev_priv->agp_mem != NULL) {
960 dev_priv->agp_textures = NULL;
961 drm_unbind_agp(dev_priv->agp_mem);
963 drm_free_agp(dev_priv->agp_mem, dev_priv->agp_pages);
964 dev_priv->agp_pages = 0;
965 dev_priv->agp_mem = NULL;
968 if ((dev->agp != NULL) && dev->agp->acquired) {
969 err = drm_agp_release(dev);
972 dev_priv->used_new_dma_init = 0;
975 dev_priv->warp = NULL;
976 dev_priv->primary = NULL;
977 dev_priv->mmio = NULL;
978 dev_priv->status = NULL;
979 dev_priv->sarea = NULL;
980 dev_priv->sarea_priv = NULL;
981 dev->agp_buffer_map = NULL;
983 memset(&dev_priv->prim, 0, sizeof(dev_priv->prim));
984 dev_priv->warp_pipe = 0;
985 memset(dev_priv->warp_pipe_phys, 0, sizeof(dev_priv->warp_pipe_phys));
987 if (dev_priv->head != NULL) {
988 mga_freelist_cleanup(dev);
995 int mga_dma_init( DRM_IOCTL_ARGS )
1001 LOCK_TEST_WITH_RETURN( dev, filp );
1003 DRM_COPY_FROM_USER_IOCTL(init, (drm_mga_init_t __user *) data,
1006 switch ( init.func ) {
1008 err = mga_do_init_dma(dev, &init);
1010 (void) mga_do_cleanup_dma(dev);
1013 case MGA_CLEANUP_DMA:
1014 return mga_do_cleanup_dma( dev );
1017 return DRM_ERR(EINVAL);
1021 /* ================================================================
1022 * Primary DMA stream management
1025 int mga_dma_flush( DRM_IOCTL_ARGS )
1028 drm_mga_private_t *dev_priv = (drm_mga_private_t *)dev->dev_private;
1031 LOCK_TEST_WITH_RETURN( dev, filp );
1033 DRM_COPY_FROM_USER_IOCTL( lock, (drm_lock_t __user *)data, sizeof(lock) );
1035 DRM_DEBUG( "%s%s%s\n",
1036 (lock.flags & _DRM_LOCK_FLUSH) ? "flush, " : "",
1037 (lock.flags & _DRM_LOCK_FLUSH_ALL) ? "flush all, " : "",
1038 (lock.flags & _DRM_LOCK_QUIESCENT) ? "idle, " : "" );
1040 WRAP_WAIT_WITH_RETURN( dev_priv );
1042 if ( lock.flags & (_DRM_LOCK_FLUSH | _DRM_LOCK_FLUSH_ALL) ) {
1043 mga_do_dma_flush( dev_priv );
1046 if ( lock.flags & _DRM_LOCK_QUIESCENT ) {
1048 int ret = mga_do_wait_for_idle( dev_priv );
1050 DRM_INFO( "%s: -EBUSY\n", __FUNCTION__ );
1053 return mga_do_wait_for_idle( dev_priv );
1060 int mga_dma_reset( DRM_IOCTL_ARGS )
1063 drm_mga_private_t *dev_priv = (drm_mga_private_t *)dev->dev_private;
1065 LOCK_TEST_WITH_RETURN( dev, filp );
1067 return mga_do_dma_reset( dev_priv );
1071 /* ================================================================
1072 * DMA buffer management
1075 static int mga_dma_get_buffers( DRMFILE filp,
1076 drm_device_t *dev, drm_dma_t *d )
1081 for ( i = d->granted_count ; i < d->request_count ; i++ ) {
1082 buf = mga_freelist_get( dev );
1083 if ( !buf ) return DRM_ERR(EAGAIN);
1087 if ( DRM_COPY_TO_USER( &d->request_indices[i],
1088 &buf->idx, sizeof(buf->idx) ) )
1089 return DRM_ERR(EFAULT);
1090 if ( DRM_COPY_TO_USER( &d->request_sizes[i],
1091 &buf->total, sizeof(buf->total) ) )
1092 return DRM_ERR(EFAULT);
1099 int mga_dma_buffers( DRM_IOCTL_ARGS )
1102 drm_device_dma_t *dma = dev->dma;
1103 drm_mga_private_t *dev_priv = (drm_mga_private_t *)dev->dev_private;
1104 drm_dma_t __user *argp = (void __user *)data;
1108 LOCK_TEST_WITH_RETURN( dev, filp );
1110 DRM_COPY_FROM_USER_IOCTL( d, argp, sizeof(d) );
1112 /* Please don't send us buffers.
1114 if ( d.send_count != 0 ) {
1115 DRM_ERROR( "Process %d trying to send %d buffers via drmDMA\n",
1116 DRM_CURRENTPID, d.send_count );
1117 return DRM_ERR(EINVAL);
1120 /* We'll send you buffers.
1122 if ( d.request_count < 0 || d.request_count > dma->buf_count ) {
1123 DRM_ERROR( "Process %d trying to get %d buffers (of %d max)\n",
1124 DRM_CURRENTPID, d.request_count, dma->buf_count );
1125 return DRM_ERR(EINVAL);
1128 WRAP_TEST_WITH_RETURN( dev_priv );
1130 d.granted_count = 0;
1132 if ( d.request_count ) {
1133 ret = mga_dma_get_buffers( filp, dev, &d );
1136 DRM_COPY_TO_USER_IOCTL( argp, d, sizeof(d) );
1142 * Called just before the module is unloaded.
1144 int mga_driver_postcleanup(drm_device_t * dev)
1146 drm_free(dev->dev_private, sizeof(drm_mga_private_t), DRM_MEM_DRIVER);
1147 dev->dev_private = NULL;
1153 * Called when the last opener of the device is closed.
1155 void mga_driver_pretakedown(drm_device_t * dev)
1157 mga_do_cleanup_dma( dev );
1160 int mga_driver_dma_quiescent(drm_device_t *dev)
1162 drm_mga_private_t *dev_priv = dev->dev_private;
1163 return mga_do_wait_for_idle( dev_priv );