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