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