1 /**************************************************************************
3 * Copyright 2010 Pauli Nieminen.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
27 **************************************************************************/
29 * Multipart buffer for coping data which is larger than the page size.
32 * Pauli Nieminen <suokkos-at-gmail-dot-com>
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #ifndef _DRM_BUFFER_H_
39 #define _DRM_BUFFER_H_
41 #include <dev/drm2/drmP.h>
51 * Return the index of page that buffer is currently pointing at.
53 static inline int drm_buffer_page(struct drm_buffer *buf)
55 return buf->iterator / PAGE_SIZE;
58 * Return the index of the current byte in the page
60 static inline int drm_buffer_index(struct drm_buffer *buf)
62 return buf->iterator & (PAGE_SIZE - 1);
65 * Return number of bytes that is left to process
67 static inline int drm_buffer_unprocessed(struct drm_buffer *buf)
69 return buf->size - buf->iterator;
73 * Advance the buffer iterator number of bytes that is given.
75 static inline void drm_buffer_advance(struct drm_buffer *buf, int bytes)
77 buf->iterator += bytes;
81 * Allocate the drm buffer object.
83 * buf: A pointer to a pointer where the object is stored.
84 * size: The number of bytes to allocate.
86 extern int drm_buffer_alloc(struct drm_buffer **buf, int size);
89 * Copy the user data to the begin of the buffer and reset the processing
92 * user_data: A pointer the data that is copied to the buffer.
93 * size: The Number of bytes to copy.
95 extern int drm_buffer_copy_from_user(struct drm_buffer *buf,
96 void __user *user_data, int size);
99 * Free the drm buffer object
101 extern void drm_buffer_free(struct drm_buffer *buf);
104 * Read an object from buffer that may be split to multiple parts. If object
105 * is not split function just returns the pointer to object in buffer. But in
106 * case of split object data is copied to given stack object that is suplied
109 * The processing location of the buffer is also advanced to the next byte
112 * objsize: The size of the objet in bytes.
113 * stack_obj: A pointer to a memory location where object can be copied.
115 extern void *drm_buffer_read_object(struct drm_buffer *buf,
116 int objsize, void *stack_obj);
119 * Returns the pointer to the dword which is offset number of elements from the
120 * current processing location.
122 * Caller must make sure that dword is not split in the buffer. This
123 * requirement is easily met if all the sizes of objects in buffer are
124 * multiples of dword and PAGE_SIZE is multiple dword.
126 * Call to this function doesn't change the processing location.
128 * offset: The index of the dword relative to the internat iterator.
130 static inline void *drm_buffer_pointer_to_dword(struct drm_buffer *buffer,
133 int iter = buffer->iterator + offset * 4;
134 return &buffer->data[iter / PAGE_SIZE][iter & (PAGE_SIZE - 1)];
137 * Returns the pointer to the dword which is offset number of elements from
138 * the current processing location.
140 * Call to this function doesn't change the processing location.
142 * offset: The index of the byte relative to the internat iterator.
144 static inline void *drm_buffer_pointer_to_byte(struct drm_buffer *buffer,
147 int iter = buffer->iterator + offset;
148 return &buffer->data[iter / PAGE_SIZE][iter & (PAGE_SIZE - 1)];