📄 r200_ioctl.c
字号:
/*Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.The Weather Channel (TM) funded Tungsten Graphics to develop theinitial release of the Radeon 8500 driver under the XFree86 license.This notice must be preserved.Permission is hereby granted, free of charge, to any person obtaininga copy of this software and associated documentation files (the"Software"), to deal in the Software without restriction, includingwithout limitation the rights to use, copy, modify, merge, publish,distribute, sublicense, and/or sell copies of the Software, and topermit persons to whom the Software is furnished to do so, subject tothe following conditions:The above copyright notice and this permission notice (including thenext paragraph) shall be included in all copies or substantialportions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OFMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BELIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTIONOF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTIONWITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.**************************************************************************//* * Authors: * Keith Whitwell <keith@tungstengraphics.com> */ #include <sched.h>#include <errno.h>#include "glheader.h"#include "imports.h"#include "macros.h"#include "context.h"#include "swrast/swrast.h"#include "r200_context.h"#include "r200_state.h"#include "r200_ioctl.h"#include "r200_tcl.h"#include "r200_sanity.h"#include "radeon_reg.h"#include "drirenderbuffer.h"#include "vblank.h"#define R200_TIMEOUT 512#define R200_IDLE_RETRY 16static void r200WaitForIdle( r200ContextPtr rmesa );/* At this point we were in FlushCmdBufLocked but we had lost our context, so * we need to unwire our current cmdbuf, hook the one with the saved state in * it, flush it, and then put the current one back. This is so commands at the * start of a cmdbuf can rely on the state being kept from the previous one. */static void r200BackUpAndEmitLostStateLocked( r200ContextPtr rmesa ){ GLuint nr_released_bufs; struct r200_store saved_store; if (rmesa->backup_store.cmd_used == 0) return; if (R200_DEBUG & DEBUG_STATE) fprintf(stderr, "Emitting backup state on lost context\n"); rmesa->lost_context = GL_FALSE; nr_released_bufs = rmesa->dma.nr_released_bufs; saved_store = rmesa->store; rmesa->dma.nr_released_bufs = 0; rmesa->store = rmesa->backup_store; r200FlushCmdBufLocked( rmesa, __FUNCTION__ ); rmesa->dma.nr_released_bufs = nr_released_bufs; rmesa->store = saved_store;}int r200FlushCmdBufLocked( r200ContextPtr rmesa, const char * caller ){ int ret, i; drm_radeon_cmd_buffer_t cmd; if (rmesa->lost_context) r200BackUpAndEmitLostStateLocked( rmesa ); if (R200_DEBUG & DEBUG_IOCTL) { fprintf(stderr, "%s from %s\n", __FUNCTION__, caller); if (0 & R200_DEBUG & DEBUG_VERBOSE) for (i = 0 ; i < rmesa->store.cmd_used ; i += 4 ) fprintf(stderr, "%d: %x\n", i/4, *(int *)(&rmesa->store.cmd_buf[i])); } if (R200_DEBUG & DEBUG_DMA) fprintf(stderr, "%s: Releasing %d buffers\n", __FUNCTION__, rmesa->dma.nr_released_bufs); if (R200_DEBUG & DEBUG_SANITY) { if (rmesa->state.scissor.enabled) ret = r200SanityCmdBuffer( rmesa, rmesa->state.scissor.numClipRects, rmesa->state.scissor.pClipRects); else ret = r200SanityCmdBuffer( rmesa, rmesa->numClipRects, rmesa->pClipRects); if (ret) { fprintf(stderr, "drmSanityCommandWrite: %d\n", ret); goto out; } } if (R200_DEBUG & DEBUG_MEMORY) { if (! driValidateTextureHeaps( rmesa->texture_heaps, rmesa->nr_heaps, & rmesa->swapped ) ) { fprintf( stderr, "%s: texture memory is inconsistent - expect " "mangled textures\n", __FUNCTION__ ); } } cmd.bufsz = rmesa->store.cmd_used; cmd.buf = rmesa->store.cmd_buf; if (rmesa->state.scissor.enabled) { cmd.nbox = rmesa->state.scissor.numClipRects; cmd.boxes = (drm_clip_rect_t *)rmesa->state.scissor.pClipRects; } else { cmd.nbox = rmesa->numClipRects; cmd.boxes = (drm_clip_rect_t *)rmesa->pClipRects; } ret = drmCommandWrite( rmesa->dri.fd, DRM_RADEON_CMDBUF, &cmd, sizeof(cmd) ); if (ret) fprintf(stderr, "drmCommandWrite: %d\n", ret); if (R200_DEBUG & DEBUG_SYNC) { fprintf(stderr, "\nSyncing in %s\n\n", __FUNCTION__); r200WaitForIdleLocked( rmesa ); } out: rmesa->store.primnr = 0; rmesa->store.statenr = 0; rmesa->store.cmd_used = 0; rmesa->dma.nr_released_bufs = 0; rmesa->save_on_next_emit = 1; return ret;}/* Note: does not emit any commands to avoid recursion on * r200AllocCmdBuf. */void r200FlushCmdBuf( r200ContextPtr rmesa, const char *caller ){ int ret; LOCK_HARDWARE( rmesa ); ret = r200FlushCmdBufLocked( rmesa, caller ); UNLOCK_HARDWARE( rmesa ); if (ret) { fprintf(stderr, "drmRadeonCmdBuffer: %d (exiting)\n", ret); exit(ret); }}/* ============================================================= * Hardware vertex buffer handling */void r200RefillCurrentDmaRegion( r200ContextPtr rmesa ){ struct r200_dma_buffer *dmabuf; int fd = rmesa->dri.fd; int index = 0; int size = 0; drmDMAReq dma; int ret; if (R200_DEBUG & (DEBUG_IOCTL|DEBUG_DMA)) fprintf(stderr, "%s\n", __FUNCTION__); if (rmesa->dma.flush) { rmesa->dma.flush( rmesa ); } if (rmesa->dma.current.buf) r200ReleaseDmaRegion( rmesa, &rmesa->dma.current, __FUNCTION__ ); if (rmesa->dma.nr_released_bufs > 4) r200FlushCmdBuf( rmesa, __FUNCTION__ ); dma.context = rmesa->dri.hwContext; dma.send_count = 0; dma.send_list = NULL; dma.send_sizes = NULL; dma.flags = 0; dma.request_count = 1; dma.request_size = RADEON_BUFFER_SIZE; dma.request_list = &index; dma.request_sizes = &size; dma.granted_count = 0; LOCK_HARDWARE(rmesa); /* no need to validate */ while (1) { ret = drmDMA( fd, &dma ); if (ret == 0) break; if (rmesa->dma.nr_released_bufs) { r200FlushCmdBufLocked( rmesa, __FUNCTION__ ); } if (rmesa->do_usleeps) { UNLOCK_HARDWARE( rmesa ); DO_USLEEP( 1 ); LOCK_HARDWARE( rmesa ); } } UNLOCK_HARDWARE(rmesa); if (R200_DEBUG & DEBUG_DMA) fprintf(stderr, "Allocated buffer %d\n", index); dmabuf = CALLOC_STRUCT( r200_dma_buffer ); dmabuf->buf = &rmesa->r200Screen->buffers->list[index]; dmabuf->refcount = 1; rmesa->dma.current.buf = dmabuf; rmesa->dma.current.address = dmabuf->buf->address; rmesa->dma.current.end = dmabuf->buf->total; rmesa->dma.current.start = 0; rmesa->dma.current.ptr = 0;}void r200ReleaseDmaRegion( r200ContextPtr rmesa, struct r200_dma_region *region, const char *caller ){ if (R200_DEBUG & DEBUG_IOCTL) fprintf(stderr, "%s from %s\n", __FUNCTION__, caller); if (!region->buf) return; if (rmesa->dma.flush) rmesa->dma.flush( rmesa ); if (--region->buf->refcount == 0) { drm_radeon_cmd_header_t *cmd; if (R200_DEBUG & (DEBUG_IOCTL|DEBUG_DMA)) fprintf(stderr, "%s -- DISCARD BUF %d\n", __FUNCTION__, region->buf->buf->idx); cmd = (drm_radeon_cmd_header_t *)r200AllocCmdBuf( rmesa, sizeof(*cmd), __FUNCTION__ ); cmd->dma.cmd_type = RADEON_CMD_DMA_DISCARD; cmd->dma.buf_idx = region->buf->buf->idx; FREE(region->buf); rmesa->dma.nr_released_bufs++; } region->buf = NULL; region->start = 0;}/* Allocates a region from rmesa->dma.current. If there isn't enough * space in current, grab a new buffer (and discard what was left of current) */void r200AllocDmaRegion( r200ContextPtr rmesa, struct r200_dma_region *region, int bytes, int alignment ){ if (R200_DEBUG & DEBUG_IOCTL) fprintf(stderr, "%s %d\n", __FUNCTION__, bytes); if (rmesa->dma.flush) rmesa->dma.flush( rmesa ); if (region->buf) r200ReleaseDmaRegion( rmesa, region, __FUNCTION__ ); alignment--; rmesa->dma.current.start = rmesa->dma.current.ptr = (rmesa->dma.current.ptr + alignment) & ~alignment; if ( rmesa->dma.current.ptr + bytes > rmesa->dma.current.end ) r200RefillCurrentDmaRegion( rmesa ); region->start = rmesa->dma.current.start; region->ptr = rmesa->dma.current.start; region->end = rmesa->dma.current.start + bytes; region->address = rmesa->dma.current.address; region->buf = rmesa->dma.current.buf; region->buf->refcount++; rmesa->dma.current.ptr += bytes; /* bug - if alignment > 7 */ rmesa->dma.current.start = rmesa->dma.current.ptr = (rmesa->dma.current.ptr + 0x7) & ~0x7; assert( rmesa->dma.current.ptr <= rmesa->dma.current.end );}/* ================================================================ * SwapBuffers with client-side throttling */static u_int32_t r200GetLastFrame(r200ContextPtr rmesa){ drm_radeon_getparam_t gp; int ret; u_int32_t frame; gp.param = RADEON_PARAM_LAST_FRAME; gp.value = (int *)&frame; ret = drmCommandWriteRead( rmesa->dri.fd, DRM_RADEON_GETPARAM, &gp, sizeof(gp) ); if ( ret ) { fprintf( stderr, "%s: drmRadeonGetParam: %d\n", __FUNCTION__, ret ); exit(1); } return frame;}static void r200EmitIrqLocked( r200ContextPtr rmesa ){ drm_radeon_irq_emit_t ie; int ret; ie.irq_seq = &rmesa->iw.irq_seq; ret = drmCommandWriteRead( rmesa->dri.fd, DRM_RADEON_IRQ_EMIT, &ie, sizeof(ie) ); if ( ret ) { fprintf( stderr, "%s: drmRadeonIrqEmit: %d\n", __FUNCTION__, ret ); exit(1); }}static void r200WaitIrq( r200ContextPtr rmesa ){ int ret; do { ret = drmCommandWrite( rmesa->dri.fd, DRM_RADEON_IRQ_WAIT, &rmesa->iw, sizeof(rmesa->iw) ); } while (ret && (errno == EINTR || errno == EBUSY)); if ( ret ) { fprintf( stderr, "%s: drmRadeonIrqWait: %d\n", __FUNCTION__, ret ); exit(1); }}static void r200WaitForFrameCompletion( r200ContextPtr rmesa ){ drm_radeon_sarea_t *sarea = rmesa->sarea; if (rmesa->do_irqs) { if (r200GetLastFrame(rmesa) < sarea->last_frame) { if (!rmesa->irqsEmitted) { while (r200GetLastFrame (rmesa) < sarea->last_frame) ; } else { UNLOCK_HARDWARE( rmesa ); r200WaitIrq( rmesa ); LOCK_HARDWARE( rmesa ); } rmesa->irqsEmitted = 10; } if (rmesa->irqsEmitted) { r200EmitIrqLocked( rmesa ); rmesa->irqsEmitted--; } } else { while (r200GetLastFrame (rmesa) < sarea->last_frame) { UNLOCK_HARDWARE( rmesa ); if (rmesa->do_usleeps) DO_USLEEP( 1 ); LOCK_HARDWARE( rmesa ); } }}/* Copy the back color buffer to the front color buffer. */void r200CopyBuffer( __DRIdrawablePrivate *dPriv, const drm_clip_rect_t *rect){ r200ContextPtr rmesa; GLint nbox, i, ret; GLboolean missed_target; int64_t ust; __DRIscreenPrivate *psp = dPriv->driScreenPriv; assert(dPriv); assert(dPriv->driContextPriv); assert(dPriv->driContextPriv->driverPrivate); rmesa = (r200ContextPtr) dPriv->driContextPriv->driverPrivate; if ( R200_DEBUG & DEBUG_IOCTL ) { fprintf( stderr, "\n%s( %p )\n\n", __FUNCTION__, (void *)rmesa->glCtx ); } R200_FIREVERTICES( rmesa ); LOCK_HARDWARE( rmesa ); /* Throttle the frame rate -- only allow one pending swap buffers * request at a time. */ r200WaitForFrameCompletion( rmesa ); if (!rect) { UNLOCK_HARDWARE( rmesa ); driWaitForVBlank( dPriv, & missed_target ); LOCK_HARDWARE( rmesa ); } nbox = dPriv->numClipRects; /* must be in locked region */ for ( i = 0 ; i < nbox ; ) { GLint nr = MIN2( i + RADEON_NR_SAREA_CLIPRECTS , nbox ); drm_clip_rect_t *box = dPriv->pClipRects; drm_clip_rect_t *b = rmesa->sarea->boxes; GLint n = 0; for ( ; i < nr ; i++ ) { *b = box[i]; if (rect) { if (rect->x1 > b->x1) b->x1 = rect->x1; if (rect->y1 > b->y1) b->y1 = rect->y1; if (rect->x2 < b->x2) b->x2 = rect->x2; if (rect->y2 < b->y2) b->y2 = rect->y2; if (b->x1 >= b->x2 || b->y1 >= b->y2) continue; } b++; n++; } rmesa->sarea->nbox = n; if (!n) continue; ret = drmCommandNone( rmesa->dri.fd, DRM_RADEON_SWAP ); if ( ret ) { fprintf( stderr, "DRM_R200_SWAP_BUFFERS: return = %d\n", ret ); UNLOCK_HARDWARE( rmesa ); exit( 1 ); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -