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