xref: /freebsd/sys/dev/drm2/drm_buffer.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
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 #include <dev/drm2/drm_buffer.h>
37 
38 /**
39  * Allocate the drm buffer object.
40  *
41  *   buf: Pointer to a pointer where the object is stored.
42  *   size: The number of bytes to allocate.
43  */
44 int drm_buffer_alloc(struct drm_buffer **buf, int size)
45 {
46 	int nr_pages = size / PAGE_SIZE + 1;
47 	int idx;
48 
49 	/* Allocating pointer table to end of structure makes drm_buffer
50 	 * variable sized */
51 	*buf = malloc(sizeof(struct drm_buffer) + nr_pages*sizeof(char *),
52 			DRM_MEM_DRIVER, M_ZERO | M_WAITOK);
53 	(*buf)->size = size;
54 
55 	for (idx = 0; idx < nr_pages; ++idx) {
56 		(*buf)->data[idx] =
57 			malloc(min(PAGE_SIZE, size - idx * PAGE_SIZE),
58 				DRM_MEM_DRIVER, M_WAITOK);
59 	}
60 
61 	return 0;
62 }
63 EXPORT_SYMBOL(drm_buffer_alloc);
64 
65 /**
66  * Copy the user data to the begin of the buffer and reset the processing
67  * iterator.
68  *
69  *   user_data: A pointer the data that is copied to the buffer.
70  *   size: The Number of bytes to copy.
71  */
72 int drm_buffer_copy_from_user(struct drm_buffer *buf,
73 			      void __user *user_data, int size)
74 {
75 	int nr_pages = size / PAGE_SIZE + 1;
76 	int idx;
77 
78 	if (size > buf->size) {
79 		DRM_ERROR("Requesting to copy %d bytes to a drm buffer with"
80 				" %d bytes space\n",
81 				size, buf->size);
82 		return -EFAULT;
83 	}
84 
85 	for (idx = 0; idx < nr_pages; ++idx) {
86 
87 		if (DRM_COPY_FROM_USER(buf->data[idx],
88 			(char *)user_data + idx * PAGE_SIZE,
89 			min(PAGE_SIZE, size - idx * PAGE_SIZE))) {
90 			DRM_ERROR("Failed to copy user data (%p) to drm buffer"
91 					" (%p) %dth page.\n",
92 					user_data, buf, idx);
93 			return -EFAULT;
94 
95 		}
96 	}
97 	buf->iterator = 0;
98 	return 0;
99 }
100 EXPORT_SYMBOL(drm_buffer_copy_from_user);
101 
102 /**
103  * Free the drm buffer object
104  */
105 void drm_buffer_free(struct drm_buffer *buf)
106 {
107 
108 	if (buf != NULL) {
109 
110 		int nr_pages = buf->size / PAGE_SIZE + 1;
111 		int idx;
112 		for (idx = 0; idx < nr_pages; ++idx)
113 			free(buf->data[idx], DRM_MEM_DRIVER);
114 
115 		free(buf, DRM_MEM_DRIVER);
116 	}
117 }
118 EXPORT_SYMBOL(drm_buffer_free);
119 
120 /**
121  * Read an object from buffer that may be split to multiple parts. If object
122  * is not split function just returns the pointer to object in buffer. But in
123  * case of split object data is copied to given stack object that is suplied
124  * by caller.
125  *
126  * The processing location of the buffer is also advanced to the next byte
127  * after the object.
128  *
129  *   objsize: The size of the objet in bytes.
130  *   stack_obj: A pointer to a memory location where object can be copied.
131  */
132 void *drm_buffer_read_object(struct drm_buffer *buf,
133 		int objsize, void *stack_obj)
134 {
135 	int idx = drm_buffer_index(buf);
136 	int page = drm_buffer_page(buf);
137 	void *obj = NULL;
138 
139 	if (idx + objsize <= PAGE_SIZE) {
140 		obj = &buf->data[page][idx];
141 	} else {
142 		/* The object is split which forces copy to temporary object.*/
143 		int beginsz = PAGE_SIZE - idx;
144 		memcpy(stack_obj, &buf->data[page][idx], beginsz);
145 
146 		memcpy((char *)stack_obj + beginsz, &buf->data[page + 1][0],
147 				objsize - beginsz);
148 
149 		obj = stack_obj;
150 	}
151 
152 	drm_buffer_advance(buf, objsize);
153 	return obj;
154 }
155 EXPORT_SYMBOL(drm_buffer_read_object);
156