Sync usage with man page.
[netbsd-mini2440.git] / sys / external / bsd / drm / dist / libdrm / nouveau / nouveau_notifier.c
blob8f1d535e9f3f253e8c48ab3abb32b7fc83b19020
1 /*
2 * Copyright 2007 Nouveau Project
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <sys/time.h>
27 #include "nouveau_private.h"
29 #define NOTIFIER(__v) \
30 struct nouveau_notifier_priv *nvnotify = nouveau_notifier(notifier); \
31 volatile uint32_t *__v = (uint32_t *)((char *)nvnotify->map + (id * 32))
33 int
34 nouveau_notifier_alloc(struct nouveau_channel *chan, uint32_t handle,
35 int count, struct nouveau_notifier **notifier)
37 struct nouveau_notifier_priv *nvnotify;
38 int ret;
40 if (!chan || !notifier || *notifier)
41 return -EINVAL;
43 nvnotify = calloc(1, sizeof(struct nouveau_notifier_priv));
44 if (!nvnotify)
45 return -ENOMEM;
46 nvnotify->base.channel = chan;
47 nvnotify->base.handle = handle;
49 nvnotify->drm.channel = chan->id;
50 nvnotify->drm.handle = handle;
51 nvnotify->drm.count = count;
52 if ((ret = drmCommandWriteRead(nouveau_device(chan->device)->fd,
53 DRM_NOUVEAU_NOTIFIEROBJ_ALLOC,
54 &nvnotify->drm,
55 sizeof(nvnotify->drm)))) {
56 nouveau_notifier_free((void *)&nvnotify);
57 return ret;
60 nvnotify->map = (char *)nouveau_channel(chan)->notifier_block +
61 nvnotify->drm.offset;
62 *notifier = &nvnotify->base;
63 return 0;
66 void
67 nouveau_notifier_free(struct nouveau_notifier **notifier)
70 struct nouveau_notifier_priv *nvnotify;
71 struct nouveau_channel_priv *nvchan;
72 struct nouveau_device_priv *nvdev;
73 struct drm_nouveau_gpuobj_free f;
75 if (!notifier || !*notifier)
76 return;
77 nvnotify = nouveau_notifier(*notifier);
78 *notifier = NULL;
80 nvchan = nouveau_channel(nvnotify->base.channel);
81 nvdev = nouveau_device(nvchan->base.device);
83 f.channel = nvchan->drm.channel;
84 f.handle = nvnotify->base.handle;
85 drmCommandWrite(nvdev->fd, DRM_NOUVEAU_GPUOBJ_FREE, &f, sizeof(f));
86 free(nvnotify);
89 void
90 nouveau_notifier_reset(struct nouveau_notifier *notifier, int id)
92 NOTIFIER(n);
94 n[NV_NOTIFY_TIME_0 /4] = 0x00000000;
95 n[NV_NOTIFY_TIME_1 /4] = 0x00000000;
96 n[NV_NOTIFY_RETURN_VALUE/4] = 0x00000000;
97 n[NV_NOTIFY_STATE /4] = (NV_NOTIFY_STATE_STATUS_IN_PROCESS <<
98 NV_NOTIFY_STATE_STATUS_SHIFT);
101 uint32_t
102 nouveau_notifier_status(struct nouveau_notifier *notifier, int id)
104 NOTIFIER(n);
106 return n[NV_NOTIFY_STATE/4] >> NV_NOTIFY_STATE_STATUS_SHIFT;
109 uint32_t
110 nouveau_notifier_return_val(struct nouveau_notifier *notifier, int id)
112 NOTIFIER(n);
114 return n[NV_NOTIFY_RETURN_VALUE/4];
117 static inline double
118 gettime(void)
120 struct timeval tv;
122 gettimeofday(&tv, NULL);
123 return (double)tv.tv_sec + tv.tv_usec / 1000000.0;
127 nouveau_notifier_wait_status(struct nouveau_notifier *notifier, int id,
128 uint32_t status, double timeout)
130 NOTIFIER(n);
131 double time = 0, t_start = gettime();
133 while (time <= timeout) {
134 uint32_t v;
136 v = n[NV_NOTIFY_STATE/4] >> NV_NOTIFY_STATE_STATUS_SHIFT;
137 if (v == status)
138 return 0;
140 if (timeout)
141 time = gettime() - t_start;
144 return -EBUSY;