OSDN Git Service

nouveau/nv50: hack up initial channel context from current state
[android-x86/external-libdrm.git] / bsd-core / 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  */
31
32 #include "drmP.h"
33
34 #define DEBUG_SCATTER 0
35
36 void drm_sg_cleanup(drm_sg_mem_t *entry)
37 {
38         free((void *)entry->handle, M_DRM);
39         free(entry->busaddr, M_DRM);
40         free(entry, M_DRM);
41 }
42
43 int drm_sg_alloc(drm_device_t * dev, drm_scatter_gather_t * request)
44 {
45         drm_sg_mem_t *entry;
46         unsigned long pages;
47         int i;
48
49         if ( dev->sg )
50                 return EINVAL;
51
52         entry = malloc(sizeof(*entry), M_DRM, M_WAITOK | M_ZERO);
53         if ( !entry )
54                 return ENOMEM;
55
56         pages = round_page(request->size) / PAGE_SIZE;
57         DRM_DEBUG( "sg size=%ld pages=%ld\n", request->size, pages );
58
59         entry->pages = pages;
60
61         entry->busaddr = malloc(pages * sizeof(*entry->busaddr), M_DRM,
62             M_WAITOK | M_ZERO);
63         if ( !entry->busaddr ) {
64                 drm_sg_cleanup(entry);
65                 return ENOMEM;
66         }
67
68         entry->handle = (long)malloc(pages << PAGE_SHIFT, M_DRM,
69             M_WAITOK | M_ZERO);
70         if (entry->handle == 0) {
71                 drm_sg_cleanup(entry);
72                 return ENOMEM;
73         }
74
75         for (i = 0; i < pages; i++) {
76                 entry->busaddr[i] = vtophys(entry->handle + i * PAGE_SIZE);
77         }
78
79         DRM_DEBUG( "sg alloc handle  = %08lx\n", entry->handle );
80
81         entry->virtual = (void *)entry->handle;
82         request->handle = entry->handle;
83
84         DRM_LOCK();
85         if (dev->sg) {
86                 DRM_UNLOCK();
87                 drm_sg_cleanup(entry);
88                 return EINVAL;
89         }
90         dev->sg = entry;
91         DRM_UNLOCK();
92
93         return 0;
94 }
95
96 int drm_sg_alloc_ioctl(drm_device_t *dev, void *data, struct drm_file *file_priv)
97 {
98         drm_scatter_gather_t *request = data;
99         int ret;
100
101         DRM_DEBUG( "%s\n", __FUNCTION__ );
102
103         ret = drm_sg_alloc(dev, request);
104         return ret;
105 }
106
107 int drm_sg_free(drm_device_t *dev, void *data, struct drm_file *file_priv)
108 {
109         drm_scatter_gather_t *request = data;
110         drm_sg_mem_t *entry;
111
112         DRM_LOCK();
113         entry = dev->sg;
114         dev->sg = NULL;
115         DRM_UNLOCK();
116
117         if ( !entry || entry->handle != request->handle )
118                 return EINVAL;
119
120         DRM_DEBUG( "sg free virtual  = 0x%lx\n", entry->handle );
121
122         drm_sg_cleanup(entry);
123
124         return 0;
125 }