xref: /freebsd/sys/dev/drm2/drm_buffer.h (revision edf8578117e8844e02c0121147f45e4609b30680)
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 #ifndef _DRM_BUFFER_H_
37 #define _DRM_BUFFER_H_
38 
39 #include <dev/drm2/drmP.h>
40 
41 struct drm_buffer {
42 	int iterator;
43 	int size;
44 	char *data[];
45 };
46 
47 
48 /**
49  * Return the index of page that buffer is currently pointing at.
50  */
51 static inline int drm_buffer_page(struct drm_buffer *buf)
52 {
53 	return buf->iterator / PAGE_SIZE;
54 }
55 /**
56  * Return the index of the current byte in the page
57  */
58 static inline int drm_buffer_index(struct drm_buffer *buf)
59 {
60 	return buf->iterator & (PAGE_SIZE - 1);
61 }
62 /**
63  * Return number of bytes that is left to process
64  */
65 static inline int drm_buffer_unprocessed(struct drm_buffer *buf)
66 {
67 	return buf->size - buf->iterator;
68 }
69 
70 /**
71  * Advance the buffer iterator number of bytes that is given.
72  */
73 static inline void drm_buffer_advance(struct drm_buffer *buf, int bytes)
74 {
75 	buf->iterator += bytes;
76 }
77 
78 /**
79  * Allocate the drm buffer object.
80  *
81  *   buf: A pointer to a pointer where the object is stored.
82  *   size: The number of bytes to allocate.
83  */
84 extern int drm_buffer_alloc(struct drm_buffer **buf, int size);
85 
86 /**
87  * Copy the user data to the begin of the buffer and reset the processing
88  * iterator.
89  *
90  *   user_data: A pointer the data that is copied to the buffer.
91  *   size: The Number of bytes to copy.
92  */
93 extern int drm_buffer_copy_from_user(struct drm_buffer *buf,
94 		void __user *user_data, int size);
95 
96 /**
97  * Free the drm buffer object
98  */
99 extern void drm_buffer_free(struct drm_buffer *buf);
100 
101 /**
102  * Read an object from buffer that may be split to multiple parts. If object
103  * is not split function just returns the pointer to object in buffer. But in
104  * case of split object data is copied to given stack object that is suplied
105  * by caller.
106  *
107  * The processing location of the buffer is also advanced to the next byte
108  * after the object.
109  *
110  *   objsize: The size of the objet in bytes.
111  *   stack_obj: A pointer to a memory location where object can be copied.
112  */
113 extern void *drm_buffer_read_object(struct drm_buffer *buf,
114 		int objsize, void *stack_obj);
115 
116 /**
117  * Returns the pointer to the dword which is offset number of elements from the
118  * current processing location.
119  *
120  * Caller must make sure that dword is not split in the buffer. This
121  * requirement is easily met if all the sizes of objects in buffer are
122  * multiples of dword and PAGE_SIZE is multiple dword.
123  *
124  * Call to this function doesn't change the processing location.
125  *
126  *   offset: The index of the dword relative to the internat iterator.
127  */
128 static inline void *drm_buffer_pointer_to_dword(struct drm_buffer *buffer,
129 		int offset)
130 {
131 	int iter = buffer->iterator + offset * 4;
132 	return &buffer->data[iter / PAGE_SIZE][iter & (PAGE_SIZE - 1)];
133 }
134 /**
135  * Returns the pointer to the dword which is offset number of elements from
136  * the current processing location.
137  *
138  * Call to this function doesn't change the processing location.
139  *
140  *   offset: The index of the byte relative to the internat iterator.
141  */
142 static inline void *drm_buffer_pointer_to_byte(struct drm_buffer *buffer,
143 		int offset)
144 {
145 	int iter = buffer->iterator + offset;
146 	return &buffer->data[iter / PAGE_SIZE][iter & (PAGE_SIZE - 1)];
147 }
148 
149 #endif
150