OSDN Git Service

nouveau: add new interface to create a nouveau_device
[android-x86/external-libdrm.git] / nouveau / nouveau.c
1 /*
2  * Copyright 2012 Red Hat Inc.
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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 COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Ben Skeggs
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <string.h>
33 #include <strings.h>
34 #include <stdbool.h>
35 #include <assert.h>
36 #include <errno.h>
37 #include <fcntl.h>
38
39 #include <xf86drm.h>
40 #include <xf86atomic.h>
41 #include "libdrm_macros.h"
42 #include "libdrm_lists.h"
43 #include "nouveau_drm.h"
44
45 #include "nouveau.h"
46 #include "private.h"
47
48 #include "nvif/class.h"
49 #include "nvif/cl0080.h"
50 #include "nvif/unpack.h"
51
52 #ifdef DEBUG
53 drm_private uint32_t nouveau_debug = 0;
54
55 static void
56 debug_init(char *args)
57 {
58         if (args) {
59                 int n = strtol(args, NULL, 0);
60                 if (n >= 0)
61                         nouveau_debug = n;
62         }
63 }
64 #endif
65
66 int
67 nouveau_object_mthd(struct nouveau_object *obj,
68                     uint32_t mthd, void *data, uint32_t size)
69 {
70         return -ENODEV;
71 }
72
73 void
74 nouveau_object_sclass_put(struct nouveau_sclass **psclass)
75 {
76         free(*psclass);
77         *psclass = NULL;
78 }
79
80 int
81 nouveau_object_sclass_get(struct nouveau_object *obj,
82                           struct nouveau_sclass **psclass)
83 {
84         return abi16_sclass(obj, psclass);
85 }
86
87 int
88 nouveau_object_mclass(struct nouveau_object *obj,
89                       const struct nouveau_mclass *mclass)
90 {
91         struct nouveau_sclass *sclass;
92         int ret = -ENODEV;
93         int cnt, i, j;
94
95         cnt = nouveau_object_sclass_get(obj, &sclass);
96         if (cnt < 0)
97                 return cnt;
98
99         for (i = 0; ret < 0 && mclass[i].oclass; i++) {
100                 for (j = 0; j < cnt; j++) {
101                         if (mclass[i].oclass  == sclass[j].oclass &&
102                             mclass[i].version >= sclass[j].minver &&
103                             mclass[i].version <= sclass[j].maxver) {
104                                 ret = i;
105                                 break;
106                         }
107                 }
108         }
109
110         nouveau_object_sclass_put(&sclass);
111         return ret;
112 }
113
114 static void
115 nouveau_object_fini(struct nouveau_object *obj)
116 {
117         if (obj->data) {
118                 abi16_delete(obj);
119                 free(obj->data);
120                 obj->data = NULL;
121                 return;
122         }
123 }
124
125 static int
126 nouveau_object_init(struct nouveau_object *parent, uint32_t handle,
127                     int32_t oclass, void *data, uint32_t size,
128                     struct nouveau_object *obj)
129 {
130         int (*func)(struct nouveau_object *);
131         int ret = -ENOSYS;
132
133         obj->parent = parent;
134         obj->handle = handle;
135         obj->oclass = oclass;
136         obj->length = 0;
137         obj->data = NULL;
138
139         abi16_object(obj, &func);
140         if (func) {
141                 obj->length = size ? size : sizeof(struct nouveau_object *);
142                 if (!(obj->data = malloc(obj->length)))
143                         return -ENOMEM;
144                 if (data)
145                         memcpy(obj->data, data, obj->length);
146                 *(struct nouveau_object **)obj->data = obj;
147
148                 ret = func(obj);
149         }
150
151         if (ret) {
152                 nouveau_object_fini(obj);
153                 return ret;
154         }
155
156         return 0;
157 }
158
159 int
160 nouveau_object_new(struct nouveau_object *parent, uint64_t handle,
161                    uint32_t oclass, void *data, uint32_t length,
162                    struct nouveau_object **pobj)
163 {
164         struct nouveau_object *obj;
165         int ret;
166
167         if (!(obj = malloc(sizeof(*obj))))
168                 return -ENOMEM;
169
170         ret = nouveau_object_init(parent, handle, oclass, data, length, obj);
171         if (ret) {
172                 free(obj);
173                 return ret;
174         }
175
176         *pobj = obj;
177         return 0;
178 }
179
180 void
181 nouveau_object_del(struct nouveau_object **pobj)
182 {
183         struct nouveau_object *obj = *pobj;
184         if (obj) {
185                 nouveau_object_fini(obj);
186                 free(obj);
187                 *pobj = NULL;
188         }
189 }
190
191 void
192 nouveau_drm_del(struct nouveau_drm **pdrm)
193 {
194         free(*pdrm);
195         *pdrm = NULL;
196 }
197
198 int
199 nouveau_drm_new(int fd, struct nouveau_drm **pdrm)
200 {
201         struct nouveau_drm *drm;
202         drmVersionPtr ver;
203
204 #ifdef DEBUG
205         debug_init(getenv("NOUVEAU_LIBDRM_DEBUG"));
206 #endif
207
208         if (!(drm = calloc(1, sizeof(*drm))))
209                 return -ENOMEM;
210         drm->fd = fd;
211
212         if (!(ver = drmGetVersion(fd))) {
213                 nouveau_drm_del(&drm);
214                 return -EINVAL;
215         }
216         *pdrm = drm;
217
218         drm->version = (ver->version_major << 24) |
219                        (ver->version_minor << 8) |
220                         ver->version_patchlevel;
221         drm->nvif = false;
222         drmFreeVersion(ver);
223         return 0;
224 }
225
226 /* this is the old libdrm's version of nouveau_device_wrap(), the symbol
227  * is kept here to prevent AIGLX from crashing if the DDX is linked against
228  * the new libdrm, but the DRI driver against the old
229  */
230 int
231 nouveau_device_open_existing(struct nouveau_device **pdev, int close, int fd,
232                              drm_context_t ctx)
233 {
234         return -EACCES;
235 }
236
237 int
238 nouveau_device_new(struct nouveau_object *parent, int32_t oclass,
239                    void *data, uint32_t size, struct nouveau_device **pdev)
240 {
241         union {
242                 struct nv_device_v0 v0;
243         } *args = data;
244         struct nouveau_drm *drm = nouveau_drm(parent);
245         struct nouveau_device_priv *nvdev;
246         struct nouveau_device *dev;
247         uint64_t v;
248         char *tmp;
249         int ret = -ENOSYS;
250
251         if (oclass != NV_DEVICE ||
252             nvif_unpack(ret, &data, &size, args->v0, 0, 0, false))
253                 return ret;
254
255         if (!(nvdev = calloc(1, sizeof(*nvdev))))
256                 return -ENOMEM;
257         dev = *pdev = &nvdev->base;
258         dev->fd = -1;
259
260         if (args->v0.device == ~0ULL) {
261                 nvdev->base.object.parent = &drm->client;
262                 nvdev->base.object.handle = ~0ULL;
263                 nvdev->base.object.oclass = NOUVEAU_DEVICE_CLASS;
264                 nvdev->base.object.length = ~0;
265
266                 ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_CHIPSET_ID, &v);
267                 if (ret)
268                         goto done;
269                 nvdev->base.chipset = v;
270
271                 ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_HAS_BO_USAGE, &v);
272                 if (ret == 0)
273                         nvdev->have_bo_usage = (v != 0);
274         } else
275                 return -ENOSYS;
276
277         ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_FB_SIZE, &v);
278         if (ret)
279                 goto done;
280         nvdev->base.vram_size = v;
281
282         ret = nouveau_getparam(dev, NOUVEAU_GETPARAM_AGP_SIZE, &v);
283         if (ret)
284                 goto done;
285         nvdev->base.gart_size = v;
286
287         tmp = getenv("NOUVEAU_LIBDRM_VRAM_LIMIT_PERCENT");
288         if (tmp)
289                 nvdev->vram_limit_percent = atoi(tmp);
290         else
291                 nvdev->vram_limit_percent = 80;
292
293         nvdev->base.vram_limit =
294                 (nvdev->base.vram_size * nvdev->vram_limit_percent) / 100;
295
296         tmp = getenv("NOUVEAU_LIBDRM_GART_LIMIT_PERCENT");
297         if (tmp)
298                 nvdev->gart_limit_percent = atoi(tmp);
299         else
300                 nvdev->gart_limit_percent = 80;
301
302         nvdev->base.gart_limit =
303                 (nvdev->base.gart_size * nvdev->gart_limit_percent) / 100;
304
305         ret = pthread_mutex_init(&nvdev->lock, NULL);
306         DRMINITLISTHEAD(&nvdev->bo_list);
307 done:
308         if (ret)
309                 nouveau_device_del(pdev);
310         return ret;
311 }
312
313 int
314 nouveau_device_wrap(int fd, int close, struct nouveau_device **pdev)
315 {
316         struct nouveau_drm *drm;
317         struct nouveau_device_priv *nvdev;
318         int ret;
319
320         ret = nouveau_drm_new(fd, &drm);
321         if (ret)
322                 return ret;
323         drm->nvif = false;
324
325         ret = nouveau_device_new(&drm->client, NV_DEVICE,
326                                  &(struct nv_device_v0) {
327                                         .device = ~0ULL,
328                                  }, sizeof(struct nv_device_v0), pdev);
329         if (ret) {
330                 nouveau_drm_del(&drm);
331                 return ret;
332         }
333
334         nvdev = nouveau_device(*pdev);
335         nvdev->base.fd = drm->fd;
336         nvdev->base.drm_version = drm->drm_version;
337         nvdev->base.lib_version = drm->lib_version;
338         nvdev->close = close;
339         return 0;
340 }
341
342 int
343 nouveau_device_open(const char *busid, struct nouveau_device **pdev)
344 {
345         int ret = -ENODEV, fd = drmOpen("nouveau", busid);
346         if (fd >= 0) {
347                 ret = nouveau_device_wrap(fd, 1, pdev);
348                 if (ret)
349                         drmClose(fd);
350         }
351         return ret;
352 }
353
354 void
355 nouveau_device_del(struct nouveau_device **pdev)
356 {
357         struct nouveau_device_priv *nvdev = nouveau_device(*pdev);
358         if (nvdev) {
359                 free(nvdev->client);
360                 pthread_mutex_destroy(&nvdev->lock);
361                 if (nvdev->base.fd >= 0) {
362                         struct nouveau_drm *drm =
363                                 nouveau_drm(&nvdev->base.object);
364                         nouveau_drm_del(&drm);
365                         if (nvdev->close)
366                                 drmClose(nvdev->base.fd);
367                 }
368                 free(nvdev);
369                 *pdev = NULL;
370         }
371 }
372
373 int
374 nouveau_getparam(struct nouveau_device *dev, uint64_t param, uint64_t *value)
375 {
376         struct nouveau_drm *drm = nouveau_drm(&dev->object);
377         struct drm_nouveau_getparam r = { .param = param };
378         int fd = drm->fd, ret =
379                 drmCommandWriteRead(fd, DRM_NOUVEAU_GETPARAM, &r, sizeof(r));
380         *value = r.value;
381         return ret;
382 }
383
384 int
385 nouveau_setparam(struct nouveau_device *dev, uint64_t param, uint64_t value)
386 {
387         struct nouveau_drm *drm = nouveau_drm(&dev->object);
388         struct drm_nouveau_setparam r = { .param = param, .value = value };
389         return drmCommandWrite(drm->fd, DRM_NOUVEAU_SETPARAM, &r, sizeof(r));
390 }
391
392 int
393 nouveau_client_new(struct nouveau_device *dev, struct nouveau_client **pclient)
394 {
395         struct nouveau_device_priv *nvdev = nouveau_device(dev);
396         struct nouveau_client_priv *pcli;
397         int id = 0, i, ret = -ENOMEM;
398         uint32_t *clients;
399
400         pthread_mutex_lock(&nvdev->lock);
401
402         for (i = 0; i < nvdev->nr_client; i++) {
403                 id = ffs(nvdev->client[i]) - 1;
404                 if (id >= 0)
405                         goto out;
406         }
407
408         clients = realloc(nvdev->client, sizeof(uint32_t) * (i + 1));
409         if (!clients)
410                 goto unlock;
411         nvdev->client = clients;
412         nvdev->client[i] = 0;
413         nvdev->nr_client++;
414
415 out:
416         pcli = calloc(1, sizeof(*pcli));
417         if (pcli) {
418                 nvdev->client[i] |= (1 << id);
419                 pcli->base.device = dev;
420                 pcli->base.id = (i * 32) + id;
421                 ret = 0;
422         }
423
424         *pclient = &pcli->base;
425
426 unlock:
427         pthread_mutex_unlock(&nvdev->lock);
428         return ret;
429 }
430
431 void
432 nouveau_client_del(struct nouveau_client **pclient)
433 {
434         struct nouveau_client_priv *pcli = nouveau_client(*pclient);
435         struct nouveau_device_priv *nvdev;
436         if (pcli) {
437                 int id = pcli->base.id;
438                 nvdev = nouveau_device(pcli->base.device);
439                 pthread_mutex_lock(&nvdev->lock);
440                 nvdev->client[id / 32] &= ~(1 << (id % 32));
441                 pthread_mutex_unlock(&nvdev->lock);
442                 free(pcli->kref);
443                 free(pcli);
444         }
445 }
446
447 static void
448 nouveau_bo_del(struct nouveau_bo *bo)
449 {
450         struct nouveau_drm *drm = nouveau_drm(&bo->device->object);
451         struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
452         struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
453         struct drm_gem_close req = { .handle = bo->handle };
454
455         if (nvbo->head.next) {
456                 pthread_mutex_lock(&nvdev->lock);
457                 if (atomic_read(&nvbo->refcnt) == 0) {
458                         DRMLISTDEL(&nvbo->head);
459                         /*
460                          * This bo has to be closed with the lock held because
461                          * gem handles are not refcounted. If a shared bo is
462                          * closed and re-opened in another thread a race
463                          * against DRM_IOCTL_GEM_OPEN or drmPrimeFDToHandle
464                          * might cause the bo to be closed accidentally while
465                          * re-importing.
466                          */
467                         drmIoctl(drm->fd, DRM_IOCTL_GEM_CLOSE, &req);
468                 }
469                 pthread_mutex_unlock(&nvdev->lock);
470         } else {
471                 drmIoctl(drm->fd, DRM_IOCTL_GEM_CLOSE, &req);
472         }
473         if (bo->map)
474                 drm_munmap(bo->map, bo->size);
475         free(nvbo);
476 }
477
478 int
479 nouveau_bo_new(struct nouveau_device *dev, uint32_t flags, uint32_t align,
480                uint64_t size, union nouveau_bo_config *config,
481                struct nouveau_bo **pbo)
482 {
483         struct nouveau_bo_priv *nvbo = calloc(1, sizeof(*nvbo));
484         struct nouveau_bo *bo = &nvbo->base;
485         int ret;
486
487         if (!nvbo)
488                 return -ENOMEM;
489         atomic_set(&nvbo->refcnt, 1);
490         bo->device = dev;
491         bo->flags = flags;
492         bo->size = size;
493
494         ret = abi16_bo_init(bo, align, config);
495         if (ret) {
496                 free(nvbo);
497                 return ret;
498         }
499
500         *pbo = bo;
501         return 0;
502 }
503
504 static int
505 nouveau_bo_wrap_locked(struct nouveau_device *dev, uint32_t handle,
506                        struct nouveau_bo **pbo, int name)
507 {
508         struct nouveau_drm *drm = nouveau_drm(&dev->object);
509         struct nouveau_device_priv *nvdev = nouveau_device(dev);
510         struct drm_nouveau_gem_info req = { .handle = handle };
511         struct nouveau_bo_priv *nvbo;
512         int ret;
513
514         DRMLISTFOREACHENTRY(nvbo, &nvdev->bo_list, head) {
515                 if (nvbo->base.handle == handle) {
516                         if (atomic_inc_return(&nvbo->refcnt) == 1) {
517                                 /*
518                                  * Uh oh, this bo is dead and someone else
519                                  * will free it, but because refcnt is
520                                  * now non-zero fortunately they won't
521                                  * call the ioctl to close the bo.
522                                  *
523                                  * Remove this bo from the list so other
524                                  * calls to nouveau_bo_wrap_locked will
525                                  * see our replacement nvbo.
526                                  */
527                                 DRMLISTDEL(&nvbo->head);
528                                 if (!name)
529                                         name = nvbo->name;
530                                 break;
531                         }
532
533                         *pbo = &nvbo->base;
534                         return 0;
535                 }
536         }
537
538         ret = drmCommandWriteRead(drm->fd, DRM_NOUVEAU_GEM_INFO,
539                                   &req, sizeof(req));
540         if (ret)
541                 return ret;
542
543         nvbo = calloc(1, sizeof(*nvbo));
544         if (nvbo) {
545                 atomic_set(&nvbo->refcnt, 1);
546                 nvbo->base.device = dev;
547                 abi16_bo_info(&nvbo->base, &req);
548                 nvbo->name = name;
549                 DRMLISTADD(&nvbo->head, &nvdev->bo_list);
550                 *pbo = &nvbo->base;
551                 return 0;
552         }
553
554         return -ENOMEM;
555 }
556
557 static void
558 nouveau_bo_make_global(struct nouveau_bo_priv *nvbo)
559 {
560         if (!nvbo->head.next) {
561                 struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
562                 pthread_mutex_lock(&nvdev->lock);
563                 if (!nvbo->head.next)
564                         DRMLISTADD(&nvbo->head, &nvdev->bo_list);
565                 pthread_mutex_unlock(&nvdev->lock);
566         }
567 }
568
569 int
570 nouveau_bo_wrap(struct nouveau_device *dev, uint32_t handle,
571                 struct nouveau_bo **pbo)
572 {
573         struct nouveau_device_priv *nvdev = nouveau_device(dev);
574         int ret;
575         pthread_mutex_lock(&nvdev->lock);
576         ret = nouveau_bo_wrap_locked(dev, handle, pbo, 0);
577         pthread_mutex_unlock(&nvdev->lock);
578         return ret;
579 }
580
581 int
582 nouveau_bo_name_ref(struct nouveau_device *dev, uint32_t name,
583                     struct nouveau_bo **pbo)
584 {
585         struct nouveau_drm *drm = nouveau_drm(&dev->object);
586         struct nouveau_device_priv *nvdev = nouveau_device(dev);
587         struct nouveau_bo_priv *nvbo;
588         struct drm_gem_open req = { .name = name };
589         int ret;
590
591         pthread_mutex_lock(&nvdev->lock);
592         DRMLISTFOREACHENTRY(nvbo, &nvdev->bo_list, head) {
593                 if (nvbo->name == name) {
594                         ret = nouveau_bo_wrap_locked(dev, nvbo->base.handle,
595                                                      pbo, name);
596                         pthread_mutex_unlock(&nvdev->lock);
597                         return ret;
598                 }
599         }
600
601         ret = drmIoctl(drm->fd, DRM_IOCTL_GEM_OPEN, &req);
602         if (ret == 0) {
603                 ret = nouveau_bo_wrap_locked(dev, req.handle, pbo, name);
604         }
605
606         pthread_mutex_unlock(&nvdev->lock);
607         return ret;
608 }
609
610 int
611 nouveau_bo_name_get(struct nouveau_bo *bo, uint32_t *name)
612 {
613         struct drm_gem_flink req = { .handle = bo->handle };
614         struct nouveau_drm *drm = nouveau_drm(&bo->device->object);
615         struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
616
617         *name = nvbo->name;
618         if (!*name) {
619                 int ret = drmIoctl(drm->fd, DRM_IOCTL_GEM_FLINK, &req);
620
621                 if (ret) {
622                         *name = 0;
623                         return ret;
624                 }
625                 nvbo->name = *name = req.name;
626
627                 nouveau_bo_make_global(nvbo);
628         }
629         return 0;
630 }
631
632 void
633 nouveau_bo_ref(struct nouveau_bo *bo, struct nouveau_bo **pref)
634 {
635         struct nouveau_bo *ref = *pref;
636         if (bo) {
637                 atomic_inc(&nouveau_bo(bo)->refcnt);
638         }
639         if (ref) {
640                 if (atomic_dec_and_test(&nouveau_bo(ref)->refcnt))
641                         nouveau_bo_del(ref);
642         }
643         *pref = bo;
644 }
645
646 int
647 nouveau_bo_prime_handle_ref(struct nouveau_device *dev, int prime_fd,
648                             struct nouveau_bo **bo)
649 {
650         struct nouveau_drm *drm = nouveau_drm(&dev->object);
651         struct nouveau_device_priv *nvdev = nouveau_device(dev);
652         int ret;
653         unsigned int handle;
654
655         nouveau_bo_ref(NULL, bo);
656
657         pthread_mutex_lock(&nvdev->lock);
658         ret = drmPrimeFDToHandle(drm->fd, prime_fd, &handle);
659         if (ret == 0) {
660                 ret = nouveau_bo_wrap_locked(dev, handle, bo, 0);
661         }
662         pthread_mutex_unlock(&nvdev->lock);
663         return ret;
664 }
665
666 int
667 nouveau_bo_set_prime(struct nouveau_bo *bo, int *prime_fd)
668 {
669         struct nouveau_drm *drm = nouveau_drm(&bo->device->object);
670         struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
671         int ret;
672
673         ret = drmPrimeHandleToFD(drm->fd, nvbo->base.handle, DRM_CLOEXEC, prime_fd);
674         if (ret)
675                 return ret;
676
677         nouveau_bo_make_global(nvbo);
678         return 0;
679 }
680
681 int
682 nouveau_bo_wait(struct nouveau_bo *bo, uint32_t access,
683                 struct nouveau_client *client)
684 {
685         struct nouveau_drm *drm = nouveau_drm(&bo->device->object);
686         struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
687         struct drm_nouveau_gem_cpu_prep req;
688         struct nouveau_pushbuf *push;
689         int ret = 0;
690
691         if (!(access & NOUVEAU_BO_RDWR))
692                 return 0;
693
694         push = cli_push_get(client, bo);
695         if (push && push->channel)
696                 nouveau_pushbuf_kick(push, push->channel);
697
698         if (!nvbo->head.next && !(nvbo->access & NOUVEAU_BO_WR) &&
699                                 !(access & NOUVEAU_BO_WR))
700                 return 0;
701
702         req.handle = bo->handle;
703         req.flags = 0;
704         if (access & NOUVEAU_BO_WR)
705                 req.flags |= NOUVEAU_GEM_CPU_PREP_WRITE;
706         if (access & NOUVEAU_BO_NOBLOCK)
707                 req.flags |= NOUVEAU_GEM_CPU_PREP_NOWAIT;
708
709         ret = drmCommandWrite(drm->fd, DRM_NOUVEAU_GEM_CPU_PREP,
710                               &req, sizeof(req));
711         if (ret == 0)
712                 nvbo->access = 0;
713         return ret;
714 }
715
716 int
717 nouveau_bo_map(struct nouveau_bo *bo, uint32_t access,
718                struct nouveau_client *client)
719 {
720         struct nouveau_drm *drm = nouveau_drm(&bo->device->object);
721         struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
722         if (bo->map == NULL) {
723                 bo->map = drm_mmap(0, bo->size, PROT_READ | PROT_WRITE,
724                                MAP_SHARED, drm->fd, nvbo->map_handle);
725                 if (bo->map == MAP_FAILED) {
726                         bo->map = NULL;
727                         return -errno;
728                 }
729         }
730         return nouveau_bo_wait(bo, access, client);
731 }