Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / drm / drm_scatter.h
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  *
29  * $FreeBSD: src/sys/dev/drm/drm_scatter.h,v 1.4.2.1 2003/04/26 07:05:28 anholt Exp $
30  */
31
32 #include "dev/drm/drmP.h"
33
34 #define DEBUG_SCATTER 0
35
36 #if __REALLY_HAVE_SG
37
38 void DRM(sg_cleanup)( drm_sg_mem_t *entry )
39 {
40         free( entry->virtual, DRM(M_DRM) );
41
42         DRM(free)( entry->busaddr,
43                    entry->pages * sizeof(*entry->busaddr),
44                    DRM_MEM_PAGES );
45         DRM(free)( entry,
46                    sizeof(*entry),
47                    DRM_MEM_SGLISTS );
48 }
49
50 int DRM(sg_alloc)( DRM_IOCTL_ARGS )
51 {
52         DRM_DEVICE;
53         drm_scatter_gather_t request;
54         drm_sg_mem_t *entry;
55         unsigned long pages;
56
57         DRM_DEBUG( "%s\n", __FUNCTION__ );
58
59         if ( dev->sg )
60                 return EINVAL;
61
62         DRM_COPY_FROM_USER_IOCTL(request, (drm_scatter_gather_t *)data,
63                              sizeof(request) );
64
65         entry = DRM(alloc)( sizeof(*entry), DRM_MEM_SGLISTS );
66         if ( !entry )
67                 return ENOMEM;
68
69         bzero( entry, sizeof(*entry) );
70
71         pages = round_page(request.size) / PAGE_SIZE;
72         DRM_DEBUG( "sg size=%ld pages=%ld\n", request.size, pages );
73
74         entry->pages = pages;
75
76         entry->busaddr = DRM(alloc)( pages * sizeof(*entry->busaddr),
77                                      DRM_MEM_PAGES );
78         if ( !entry->busaddr ) {
79                 DRM(free)( entry,
80                            sizeof(*entry),
81                            DRM_MEM_SGLISTS );
82                 return ENOMEM;
83         }
84         bzero( (void *)entry->busaddr, pages * sizeof(*entry->busaddr) );
85
86         entry->virtual = malloc( pages << PAGE_SHIFT, DRM(M_DRM), M_WAITOK );
87         if ( !entry->virtual ) {
88                 DRM(free)( entry->busaddr,
89                            entry->pages * sizeof(*entry->busaddr),
90                            DRM_MEM_PAGES );
91                 DRM(free)( entry,
92                            sizeof(*entry),
93                            DRM_MEM_SGLISTS );
94                 return ENOMEM;
95         }
96
97         bzero( entry->virtual, pages << PAGE_SHIFT );
98
99         entry->handle = (unsigned long)entry->virtual;
100
101         DRM_DEBUG( "sg alloc handle  = %08lx\n", entry->handle );
102         DRM_DEBUG( "sg alloc virtual = %p\n", entry->virtual );
103
104         request.handle = entry->handle;
105
106         DRM_COPY_TO_USER_IOCTL( (drm_scatter_gather_t *)data,
107                            request,
108                            sizeof(request) );
109
110         dev->sg = entry;
111
112         return 0;
113
114         DRM(sg_cleanup)( entry );
115         return ENOMEM;
116 }
117
118 int DRM(sg_free)( DRM_IOCTL_ARGS )
119 {
120         DRM_DEVICE;
121         drm_scatter_gather_t request;
122         drm_sg_mem_t *entry;
123
124         DRM_COPY_FROM_USER_IOCTL( request, (drm_scatter_gather_t *)data,
125                              sizeof(request) );
126
127         entry = dev->sg;
128         dev->sg = NULL;
129
130         if ( !entry || entry->handle != request.handle )
131                 return EINVAL;
132
133         DRM_DEBUG( "sg free virtual  = %p\n", entry->virtual );
134
135         DRM(sg_cleanup)( entry );
136
137         return 0;
138 }
139
140 #else /* __REALLY_HAVE_SG */
141
142 int DRM(sg_alloc)( DRM_IOCTL_ARGS )
143 {
144         return DRM_ERR(EINVAL);
145 }
146 int DRM(sg_free)( DRM_IOCTL_ARGS )
147 {
148         return DRM_ERR(EINVAL);
149 }
150
151 #endif