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