xref: /freebsd/sys/dev/drm2/drm_buffer.c (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 /**************************************************************************
2  *
3  * Copyright 2010 Pauli Nieminen.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  *
27  **************************************************************************/
28 /*
29  * Multipart buffer for coping data which is larger than the page size.
30  *
31  * Authors:
32  * Pauli Nieminen <suokkos-at-gmail-dot-com>
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <dev/drm2/drm_buffer.h>
39 
40 /**
41  * Allocate the drm buffer object.
42  *
43  *   buf: Pointer to a pointer where the object is stored.
44  *   size: The number of bytes to allocate.
45  */
46 int drm_buffer_alloc(struct drm_buffer **buf, int size)
47 {
48 	int nr_pages = size / PAGE_SIZE + 1;
49 	int idx;
50 
51 	/* Allocating pointer table to end of structure makes drm_buffer
52 	 * variable sized */
53 	*buf = malloc(sizeof(struct drm_buffer) + nr_pages*sizeof(char *),
54 			DRM_MEM_DRIVER, M_ZERO | M_WAITOK);
55 
56 	if (*buf == NULL) {
57 		DRM_ERROR("Failed to allocate drm buffer object to hold"
58 				" %d bytes in %d pages.\n",
59 				size, nr_pages);
60 		return -ENOMEM;
61 	}
62 
63 	(*buf)->size = size;
64 
65 	for (idx = 0; idx < nr_pages; ++idx) {
66 
67 		(*buf)->data[idx] =
68 			malloc(min(PAGE_SIZE, size - idx * PAGE_SIZE),
69 				DRM_MEM_DRIVER, M_WAITOK);
70 
71 
72 		if ((*buf)->data[idx] == NULL) {
73 			DRM_ERROR("Failed to allocate %dth page for drm"
74 					" buffer with %d bytes and %d pages.\n",
75 					idx + 1, size, nr_pages);
76 			goto error_out;
77 		}
78 
79 	}
80 
81 	return 0;
82 
83 error_out:
84 
85 	/* Only last element can be null pointer so check for it first. */
86 	if ((*buf)->data[idx])
87 		free((*buf)->data[idx], DRM_MEM_DRIVER);
88 
89 	for (--idx; idx >= 0; --idx)
90 		free((*buf)->data[idx], DRM_MEM_DRIVER);
91 
92 	free(*buf, DRM_MEM_DRIVER);
93 	return -ENOMEM;
94 }
95 EXPORT_SYMBOL(drm_buffer_alloc);
96 
97 /**
98  * Copy the user data to the begin of the buffer and reset the processing
99  * iterator.
100  *
101  *   user_data: A pointer the data that is copied to the buffer.
102  *   size: The Number of bytes to copy.
103  */
104 int drm_buffer_copy_from_user(struct drm_buffer *buf,
105 			      void __user *user_data, int size)
106 {
107 	int nr_pages = size / PAGE_SIZE + 1;
108 	int idx;
109 
110 	if (size > buf->size) {
111 		DRM_ERROR("Requesting to copy %d bytes to a drm buffer with"
112 				" %d bytes space\n",
113 				size, buf->size);
114 		return -EFAULT;
115 	}
116 
117 	for (idx = 0; idx < nr_pages; ++idx) {
118 
119 		if (DRM_COPY_FROM_USER(buf->data[idx],
120 			(char *)user_data + idx * PAGE_SIZE,
121 			min(PAGE_SIZE, size - idx * PAGE_SIZE))) {
122 			DRM_ERROR("Failed to copy user data (%p) to drm buffer"
123 					" (%p) %dth page.\n",
124 					user_data, buf, idx);
125 			return -EFAULT;
126 
127 		}
128 	}
129 	buf->iterator = 0;
130 	return 0;
131 }
132 EXPORT_SYMBOL(drm_buffer_copy_from_user);
133 
134 /**
135  * Free the drm buffer object
136  */
137 void drm_buffer_free(struct drm_buffer *buf)
138 {
139 
140 	if (buf != NULL) {
141 
142 		int nr_pages = buf->size / PAGE_SIZE + 1;
143 		int idx;
144 		for (idx = 0; idx < nr_pages; ++idx)
145 			free(buf->data[idx], DRM_MEM_DRIVER);
146 
147 		free(buf, DRM_MEM_DRIVER);
148 	}
149 }
150 EXPORT_SYMBOL(drm_buffer_free);
151 
152 /**
153  * Read an object from buffer that may be split to multiple parts. If object
154  * is not split function just returns the pointer to object in buffer. But in
155  * case of split object data is copied to given stack object that is suplied
156  * by caller.
157  *
158  * The processing location of the buffer is also advanced to the next byte
159  * after the object.
160  *
161  *   objsize: The size of the objet in bytes.
162  *   stack_obj: A pointer to a memory location where object can be copied.
163  */
164 void *drm_buffer_read_object(struct drm_buffer *buf,
165 		int objsize, void *stack_obj)
166 {
167 	int idx = drm_buffer_index(buf);
168 	int page = drm_buffer_page(buf);
169 	void *obj = NULL;
170 
171 	if (idx + objsize <= PAGE_SIZE) {
172 		obj = &buf->data[page][idx];
173 	} else {
174 		/* The object is split which forces copy to temporary object.*/
175 		int beginsz = PAGE_SIZE - idx;
176 		memcpy(stack_obj, &buf->data[page][idx], beginsz);
177 
178 		memcpy((char *)stack_obj + beginsz, &buf->data[page + 1][0],
179 				objsize - beginsz);
180 
181 		obj = stack_obj;
182 	}
183 
184 	drm_buffer_advance(buf, objsize);
185 	return obj;
186 }
187 EXPORT_SYMBOL(drm_buffer_read_object);
188