]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/drm/drm_scatter.c
This commit was generated by cvs2svn to compensate for changes in r150974,
[FreeBSD/FreeBSD.git] / sys / dev / drm / drm_scatter.c
1 /* drm_scatter.h -- IOCTLs to manage scatter/gather memory -*- linux-c -*-
2  * Created: Mon Dec 18 23:20:54 2000 by gareth@valinux.com */
3 /*-
4  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the next
15  * paragraph) shall be included in all copies or substantial portions of the
16  * Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  *
26  * Authors:
27  *   Gareth Hughes <gareth@valinux.com>
28  *   Eric Anholt <anholt@FreeBSD.org>
29  *
30  * $FreeBSD$
31  */
32
33 #include "dev/drm/drmP.h"
34
35 #define DEBUG_SCATTER 0
36
37 void drm_sg_cleanup(drm_sg_mem_t *entry)
38 {
39         free(entry->virtual, M_DRM);
40         free(entry->busaddr, M_DRM);
41         free(entry, M_DRM);
42 }
43
44 int drm_sg_alloc(DRM_IOCTL_ARGS)
45 {
46         DRM_DEVICE;
47         drm_scatter_gather_t request;
48         drm_sg_mem_t *entry;
49         unsigned long pages;
50
51         DRM_DEBUG( "%s\n", __FUNCTION__ );
52
53         if ( dev->sg )
54                 return EINVAL;
55
56         DRM_COPY_FROM_USER_IOCTL(request, (drm_scatter_gather_t *)data,
57                              sizeof(request) );
58
59         entry = malloc(sizeof(*entry), M_DRM, M_WAITOK | M_ZERO);
60         if ( !entry )
61                 return ENOMEM;
62
63         pages = round_page(request.size) / PAGE_SIZE;
64         DRM_DEBUG( "sg size=%ld pages=%ld\n", request.size, pages );
65
66         entry->pages = pages;
67
68         entry->busaddr = malloc(pages * sizeof(*entry->busaddr), M_DRM,
69             M_WAITOK | M_ZERO);
70         if ( !entry->busaddr ) {
71                 drm_sg_cleanup(entry);
72                 return ENOMEM;
73         }
74
75         entry->virtual = malloc(pages << PAGE_SHIFT, M_DRM, M_WAITOK | M_ZERO);
76         if ( !entry->virtual ) {
77                 drm_sg_cleanup(entry);
78                 return ENOMEM;
79         }
80
81         entry->handle = (unsigned long)entry->virtual;
82
83         DRM_DEBUG( "sg alloc handle  = %08lx\n", entry->handle );
84         DRM_DEBUG( "sg alloc virtual = %p\n", entry->virtual );
85
86         request.handle = entry->handle;
87
88         DRM_COPY_TO_USER_IOCTL( (drm_scatter_gather_t *)data,
89                            request,
90                            sizeof(request) );
91
92         DRM_LOCK();
93         if (dev->sg) {
94                 DRM_UNLOCK();
95                 drm_sg_cleanup(entry);
96                 return EINVAL;
97         }
98         dev->sg = entry;
99         DRM_UNLOCK();
100
101         return 0;
102 }
103
104 int drm_sg_free(DRM_IOCTL_ARGS)
105 {
106         DRM_DEVICE;
107         drm_scatter_gather_t request;
108         drm_sg_mem_t *entry;
109
110         DRM_COPY_FROM_USER_IOCTL( request, (drm_scatter_gather_t *)data,
111                              sizeof(request) );
112
113         DRM_LOCK();
114         entry = dev->sg;
115         dev->sg = NULL;
116         DRM_UNLOCK();
117
118         if ( !entry || entry->handle != request.handle )
119                 return EINVAL;
120
121         DRM_DEBUG( "sg free virtual  = %p\n", entry->virtual );
122
123         drm_sg_cleanup(entry);
124
125         return 0;
126 }