Merge commit 'origin/gallium-0.1' into gallium-0.2
[mesa.git] / src / gallium / winsys / drm / nouveau / nouveau_lock.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "main/glheader.h"
29 #include "glapi/glthread.h"
30 #include <GL/internal/glcore.h>
31
32 #include "nouveau_context.h"
33 #include "nouveau_screen.h"
34
35 _glthread_DECLARE_STATIC_MUTEX( lockMutex );
36
37 static void
38 nouveau_contended_lock(struct nouveau_context *nv, GLuint flags)
39 {
40 __DRIdrawablePrivate *dPriv = nv->dri_drawable;
41 __DRIscreenPrivate *sPriv = nv->dri_screen;
42 struct nouveau_screen *nv_screen = nv->nv_screen;
43 struct nouveau_device *dev = nv_screen->device;
44 struct nouveau_device_priv *nvdev = nouveau_device(dev);
45
46 drmGetLock(nvdev->fd, nvdev->ctx, flags);
47
48 /* If the window moved, may need to set a new cliprect now.
49 *
50 * NOTE: This releases and regains the hw lock, so all state
51 * checking must be done *after* this call:
52 */
53 if (dPriv)
54 DRI_VALIDATE_DRAWABLE_INFO(sPriv, dPriv);
55 }
56
57 /* Lock the hardware and validate our state.
58 */
59 void
60 LOCK_HARDWARE(struct nouveau_context *nv)
61 {
62 struct nouveau_screen *nv_screen = nv->nv_screen;
63 struct nouveau_device *dev = nv_screen->device;
64 struct nouveau_device_priv *nvdev = nouveau_device(dev);
65 char __ret=0;
66
67 _glthread_LOCK_MUTEX(lockMutex);
68 assert(!nv->locked);
69
70 DRM_CAS(nvdev->lock, nvdev->ctx,
71 (DRM_LOCK_HELD | nvdev->ctx), __ret);
72
73 if (__ret)
74 nouveau_contended_lock(nv, 0);
75 nv->locked = GL_TRUE;
76 }
77
78
79 /* Unlock the hardware using the global current context
80 */
81 void
82 UNLOCK_HARDWARE(struct nouveau_context *nv)
83 {
84 struct nouveau_screen *nv_screen = nv->nv_screen;
85 struct nouveau_device *dev = nv_screen->device;
86 struct nouveau_device_priv *nvdev = nouveau_device(dev);
87
88 assert(nv->locked);
89 nv->locked = GL_FALSE;
90
91 DRM_UNLOCK(nvdev->fd, nvdev->lock, nvdev->ctx);
92
93 _glthread_UNLOCK_MUTEX(lockMutex);
94 }