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