drm_scatter.c revision 1.1 1 1.1 riastrad /**
2 1.1 riastrad * \file drm_scatter.c
3 1.1 riastrad * IOCTLs to manage scatter/gather memory
4 1.1 riastrad *
5 1.1 riastrad * \author Gareth Hughes <gareth (at) valinux.com>
6 1.1 riastrad */
7 1.1 riastrad
8 1.1 riastrad /*
9 1.1 riastrad * Created: Mon Dec 18 23:20:54 2000 by gareth (at) valinux.com
10 1.1 riastrad *
11 1.1 riastrad * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12 1.1 riastrad * All Rights Reserved.
13 1.1 riastrad *
14 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a
15 1.1 riastrad * copy of this software and associated documentation files (the "Software"),
16 1.1 riastrad * to deal in the Software without restriction, including without limitation
17 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the
19 1.1 riastrad * Software is furnished to do so, subject to the following conditions:
20 1.1 riastrad *
21 1.1 riastrad * The above copyright notice and this permission notice (including the next
22 1.1 riastrad * paragraph) shall be included in all copies or substantial portions of the
23 1.1 riastrad * Software.
24 1.1 riastrad *
25 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 1.1 riastrad * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31 1.1 riastrad * DEALINGS IN THE SOFTWARE.
32 1.1 riastrad */
33 1.1 riastrad
34 1.1 riastrad #include <linux/vmalloc.h>
35 1.1 riastrad #include <linux/slab.h>
36 1.1 riastrad #include <drm/drmP.h>
37 1.1 riastrad
38 1.1 riastrad #define DEBUG_SCATTER 0
39 1.1 riastrad
40 1.1 riastrad static inline void *drm_vmalloc_dma(unsigned long size)
41 1.1 riastrad {
42 1.1 riastrad #if defined(__powerpc__) && defined(CONFIG_NOT_COHERENT_CACHE)
43 1.1 riastrad return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL | _PAGE_NO_CACHE);
44 1.1 riastrad #else
45 1.1 riastrad return vmalloc_32(size);
46 1.1 riastrad #endif
47 1.1 riastrad }
48 1.1 riastrad
49 1.1 riastrad void drm_sg_cleanup(struct drm_sg_mem * entry)
50 1.1 riastrad {
51 1.1 riastrad struct page *page;
52 1.1 riastrad int i;
53 1.1 riastrad
54 1.1 riastrad for (i = 0; i < entry->pages; i++) {
55 1.1 riastrad page = entry->pagelist[i];
56 1.1 riastrad if (page)
57 1.1 riastrad ClearPageReserved(page);
58 1.1 riastrad }
59 1.1 riastrad
60 1.1 riastrad vfree(entry->virtual);
61 1.1 riastrad
62 1.1 riastrad kfree(entry->busaddr);
63 1.1 riastrad kfree(entry->pagelist);
64 1.1 riastrad kfree(entry);
65 1.1 riastrad }
66 1.1 riastrad
67 1.1 riastrad #ifdef _LP64
68 1.1 riastrad # define ScatterHandle(x) (unsigned int)((x >> 32) + (x & ((1L << 32) - 1)))
69 1.1 riastrad #else
70 1.1 riastrad # define ScatterHandle(x) (unsigned int)(x)
71 1.1 riastrad #endif
72 1.1 riastrad
73 1.1 riastrad int drm_sg_alloc(struct drm_device *dev, struct drm_scatter_gather * request)
74 1.1 riastrad {
75 1.1 riastrad struct drm_sg_mem *entry;
76 1.1 riastrad unsigned long pages, i, j;
77 1.1 riastrad
78 1.1 riastrad DRM_DEBUG("\n");
79 1.1 riastrad
80 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_SG))
81 1.1 riastrad return -EINVAL;
82 1.1 riastrad
83 1.1 riastrad if (dev->sg)
84 1.1 riastrad return -EINVAL;
85 1.1 riastrad
86 1.1 riastrad entry = kzalloc(sizeof(*entry), GFP_KERNEL);
87 1.1 riastrad if (!entry)
88 1.1 riastrad return -ENOMEM;
89 1.1 riastrad
90 1.1 riastrad pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
91 1.1 riastrad DRM_DEBUG("size=%ld pages=%ld\n", request->size, pages);
92 1.1 riastrad
93 1.1 riastrad entry->pages = pages;
94 1.1 riastrad entry->pagelist = kcalloc(pages, sizeof(*entry->pagelist), GFP_KERNEL);
95 1.1 riastrad if (!entry->pagelist) {
96 1.1 riastrad kfree(entry);
97 1.1 riastrad return -ENOMEM;
98 1.1 riastrad }
99 1.1 riastrad
100 1.1 riastrad entry->busaddr = kcalloc(pages, sizeof(*entry->busaddr), GFP_KERNEL);
101 1.1 riastrad if (!entry->busaddr) {
102 1.1 riastrad kfree(entry->pagelist);
103 1.1 riastrad kfree(entry);
104 1.1 riastrad return -ENOMEM;
105 1.1 riastrad }
106 1.1 riastrad
107 1.1 riastrad entry->virtual = drm_vmalloc_dma(pages << PAGE_SHIFT);
108 1.1 riastrad if (!entry->virtual) {
109 1.1 riastrad kfree(entry->busaddr);
110 1.1 riastrad kfree(entry->pagelist);
111 1.1 riastrad kfree(entry);
112 1.1 riastrad return -ENOMEM;
113 1.1 riastrad }
114 1.1 riastrad
115 1.1 riastrad /* This also forces the mapping of COW pages, so our page list
116 1.1 riastrad * will be valid. Please don't remove it...
117 1.1 riastrad */
118 1.1 riastrad memset(entry->virtual, 0, pages << PAGE_SHIFT);
119 1.1 riastrad
120 1.1 riastrad entry->handle = ScatterHandle((unsigned long)entry->virtual);
121 1.1 riastrad
122 1.1 riastrad DRM_DEBUG("handle = %08lx\n", entry->handle);
123 1.1 riastrad DRM_DEBUG("virtual = %p\n", entry->virtual);
124 1.1 riastrad
125 1.1 riastrad for (i = (unsigned long)entry->virtual, j = 0; j < pages;
126 1.1 riastrad i += PAGE_SIZE, j++) {
127 1.1 riastrad entry->pagelist[j] = vmalloc_to_page((void *)i);
128 1.1 riastrad if (!entry->pagelist[j])
129 1.1 riastrad goto failed;
130 1.1 riastrad SetPageReserved(entry->pagelist[j]);
131 1.1 riastrad }
132 1.1 riastrad
133 1.1 riastrad request->handle = entry->handle;
134 1.1 riastrad
135 1.1 riastrad dev->sg = entry;
136 1.1 riastrad
137 1.1 riastrad #if DEBUG_SCATTER
138 1.1 riastrad /* Verify that each page points to its virtual address, and vice
139 1.1 riastrad * versa.
140 1.1 riastrad */
141 1.1 riastrad {
142 1.1 riastrad int error = 0;
143 1.1 riastrad
144 1.1 riastrad for (i = 0; i < pages; i++) {
145 1.1 riastrad unsigned long *tmp;
146 1.1 riastrad
147 1.1 riastrad tmp = page_address(entry->pagelist[i]);
148 1.1 riastrad for (j = 0;
149 1.1 riastrad j < PAGE_SIZE / sizeof(unsigned long);
150 1.1 riastrad j++, tmp++) {
151 1.1 riastrad *tmp = 0xcafebabe;
152 1.1 riastrad }
153 1.1 riastrad tmp = (unsigned long *)((u8 *) entry->virtual +
154 1.1 riastrad (PAGE_SIZE * i));
155 1.1 riastrad for (j = 0;
156 1.1 riastrad j < PAGE_SIZE / sizeof(unsigned long);
157 1.1 riastrad j++, tmp++) {
158 1.1 riastrad if (*tmp != 0xcafebabe && error == 0) {
159 1.1 riastrad error = 1;
160 1.1 riastrad DRM_ERROR("Scatter allocation error, "
161 1.1 riastrad "pagelist does not match "
162 1.1 riastrad "virtual mapping\n");
163 1.1 riastrad }
164 1.1 riastrad }
165 1.1 riastrad tmp = page_address(entry->pagelist[i]);
166 1.1 riastrad for (j = 0;
167 1.1 riastrad j < PAGE_SIZE / sizeof(unsigned long);
168 1.1 riastrad j++, tmp++) {
169 1.1 riastrad *tmp = 0;
170 1.1 riastrad }
171 1.1 riastrad }
172 1.1 riastrad if (error == 0)
173 1.1 riastrad DRM_ERROR("Scatter allocation matches pagelist\n");
174 1.1 riastrad }
175 1.1 riastrad #endif
176 1.1 riastrad
177 1.1 riastrad return 0;
178 1.1 riastrad
179 1.1 riastrad failed:
180 1.1 riastrad drm_sg_cleanup(entry);
181 1.1 riastrad return -ENOMEM;
182 1.1 riastrad }
183 1.1 riastrad
184 1.1 riastrad int drm_sg_alloc_ioctl(struct drm_device *dev, void *data,
185 1.1 riastrad struct drm_file *file_priv)
186 1.1 riastrad {
187 1.1 riastrad struct drm_scatter_gather *request = data;
188 1.1 riastrad
189 1.1 riastrad return drm_sg_alloc(dev, request);
190 1.1 riastrad
191 1.1 riastrad }
192 1.1 riastrad
193 1.1 riastrad int drm_sg_free(struct drm_device *dev, void *data,
194 1.1 riastrad struct drm_file *file_priv)
195 1.1 riastrad {
196 1.1 riastrad struct drm_scatter_gather *request = data;
197 1.1 riastrad struct drm_sg_mem *entry;
198 1.1 riastrad
199 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_SG))
200 1.1 riastrad return -EINVAL;
201 1.1 riastrad
202 1.1 riastrad entry = dev->sg;
203 1.1 riastrad dev->sg = NULL;
204 1.1 riastrad
205 1.1 riastrad if (!entry || entry->handle != request->handle)
206 1.1 riastrad return -EINVAL;
207 1.1 riastrad
208 1.1 riastrad DRM_DEBUG("virtual = %p\n", entry->virtual);
209 1.1 riastrad
210 1.1 riastrad drm_sg_cleanup(entry);
211 1.1 riastrad
212 1.1 riastrad return 0;
213 1.1 riastrad }
214