1254794Sdumbbell/**************************************************************************
2254794Sdumbbell *
3254794Sdumbbell * Copyright 2010 Pauli Nieminen.
4254794Sdumbbell * All Rights Reserved.
5254794Sdumbbell *
6254794Sdumbbell * Permission is hereby granted, free of charge, to any person obtaining a
7254794Sdumbbell * copy of this software and associated documentation files (the
8254794Sdumbbell * "Software"), to deal in the Software without restriction, including
9254794Sdumbbell * without limitation the rights to use, copy, modify, merge, publish,
10254794Sdumbbell * distribute, sub license, and/or sell copies of the Software, and to
11254794Sdumbbell * permit persons to whom the Software is furnished to do so, subject to
12254794Sdumbbell * the following conditions:
13254794Sdumbbell *
14254794Sdumbbell * The above copyright notice and this permission notice (including the
15254794Sdumbbell * next paragraph) shall be included in all copies or substantial portions
16254794Sdumbbell * of the Software.
17254794Sdumbbell *
18254794Sdumbbell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19254794Sdumbbell * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20254794Sdumbbell * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21254794Sdumbbell * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22254794Sdumbbell * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23254794Sdumbbell * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24254794Sdumbbell * USE OR OTHER DEALINGS IN THE SOFTWARE.
25254794Sdumbbell *
26254794Sdumbbell *
27254794Sdumbbell **************************************************************************/
28254794Sdumbbell/*
29254794Sdumbbell * Multipart buffer for coping data which is larger than the page size.
30254794Sdumbbell *
31254794Sdumbbell * Authors:
32254794Sdumbbell * Pauli Nieminen <suokkos-at-gmail-dot-com>
33254794Sdumbbell */
34254794Sdumbbell
35254794Sdumbbell#include <sys/cdefs.h>
36254794Sdumbbell__FBSDID("$FreeBSD$");
37254794Sdumbbell
38254794Sdumbbell#ifndef _DRM_BUFFER_H_
39254794Sdumbbell#define _DRM_BUFFER_H_
40254794Sdumbbell
41254794Sdumbbell#include <dev/drm2/drmP.h>
42254794Sdumbbell
43254794Sdumbbellstruct drm_buffer {
44254794Sdumbbell	int iterator;
45254794Sdumbbell	int size;
46254794Sdumbbell	char *data[];
47254794Sdumbbell};
48254794Sdumbbell
49254794Sdumbbell
50254794Sdumbbell/**
51254794Sdumbbell * Return the index of page that buffer is currently pointing at.
52254794Sdumbbell */
53254794Sdumbbellstatic inline int drm_buffer_page(struct drm_buffer *buf)
54254794Sdumbbell{
55254794Sdumbbell	return buf->iterator / PAGE_SIZE;
56254794Sdumbbell}
57254794Sdumbbell/**
58254794Sdumbbell * Return the index of the current byte in the page
59254794Sdumbbell */
60254794Sdumbbellstatic inline int drm_buffer_index(struct drm_buffer *buf)
61254794Sdumbbell{
62254794Sdumbbell	return buf->iterator & (PAGE_SIZE - 1);
63254794Sdumbbell}
64254794Sdumbbell/**
65254794Sdumbbell * Return number of bytes that is left to process
66254794Sdumbbell */
67254794Sdumbbellstatic inline int drm_buffer_unprocessed(struct drm_buffer *buf)
68254794Sdumbbell{
69254794Sdumbbell	return buf->size - buf->iterator;
70254794Sdumbbell}
71254794Sdumbbell
72254794Sdumbbell/**
73254794Sdumbbell * Advance the buffer iterator number of bytes that is given.
74254794Sdumbbell */
75254794Sdumbbellstatic inline void drm_buffer_advance(struct drm_buffer *buf, int bytes)
76254794Sdumbbell{
77254794Sdumbbell	buf->iterator += bytes;
78254794Sdumbbell}
79254794Sdumbbell
80254794Sdumbbell/**
81254794Sdumbbell * Allocate the drm buffer object.
82254794Sdumbbell *
83254794Sdumbbell *   buf: A pointer to a pointer where the object is stored.
84254794Sdumbbell *   size: The number of bytes to allocate.
85254794Sdumbbell */
86254794Sdumbbellextern int drm_buffer_alloc(struct drm_buffer **buf, int size);
87254794Sdumbbell
88254794Sdumbbell/**
89254794Sdumbbell * Copy the user data to the begin of the buffer and reset the processing
90254794Sdumbbell * iterator.
91254794Sdumbbell *
92254794Sdumbbell *   user_data: A pointer the data that is copied to the buffer.
93254794Sdumbbell *   size: The Number of bytes to copy.
94254794Sdumbbell */
95254794Sdumbbellextern int drm_buffer_copy_from_user(struct drm_buffer *buf,
96254794Sdumbbell		void __user *user_data, int size);
97254794Sdumbbell
98254794Sdumbbell/**
99254794Sdumbbell * Free the drm buffer object
100254794Sdumbbell */
101254794Sdumbbellextern void drm_buffer_free(struct drm_buffer *buf);
102254794Sdumbbell
103254794Sdumbbell/**
104254794Sdumbbell * Read an object from buffer that may be split to multiple parts. If object
105254794Sdumbbell * is not split function just returns the pointer to object in buffer. But in
106254794Sdumbbell * case of split object data is copied to given stack object that is suplied
107254794Sdumbbell * by caller.
108254794Sdumbbell *
109254794Sdumbbell * The processing location of the buffer is also advanced to the next byte
110254794Sdumbbell * after the object.
111254794Sdumbbell *
112254794Sdumbbell *   objsize: The size of the objet in bytes.
113254794Sdumbbell *   stack_obj: A pointer to a memory location where object can be copied.
114254794Sdumbbell */
115254794Sdumbbellextern void *drm_buffer_read_object(struct drm_buffer *buf,
116254794Sdumbbell		int objsize, void *stack_obj);
117254794Sdumbbell
118254794Sdumbbell/**
119254794Sdumbbell * Returns the pointer to the dword which is offset number of elements from the
120254794Sdumbbell * current processing location.
121254794Sdumbbell *
122254794Sdumbbell * Caller must make sure that dword is not split in the buffer. This
123254794Sdumbbell * requirement is easily met if all the sizes of objects in buffer are
124254794Sdumbbell * multiples of dword and PAGE_SIZE is multiple dword.
125254794Sdumbbell *
126254794Sdumbbell * Call to this function doesn't change the processing location.
127254794Sdumbbell *
128254794Sdumbbell *   offset: The index of the dword relative to the internat iterator.
129254794Sdumbbell */
130254794Sdumbbellstatic inline void *drm_buffer_pointer_to_dword(struct drm_buffer *buffer,
131254794Sdumbbell		int offset)
132254794Sdumbbell{
133254794Sdumbbell	int iter = buffer->iterator + offset * 4;
134254794Sdumbbell	return &buffer->data[iter / PAGE_SIZE][iter & (PAGE_SIZE - 1)];
135254794Sdumbbell}
136254794Sdumbbell/**
137254794Sdumbbell * Returns the pointer to the dword which is offset number of elements from
138254794Sdumbbell * the current processing location.
139254794Sdumbbell *
140254794Sdumbbell * Call to this function doesn't change the processing location.
141254794Sdumbbell *
142254794Sdumbbell *   offset: The index of the byte relative to the internat iterator.
143254794Sdumbbell */
144254794Sdumbbellstatic inline void *drm_buffer_pointer_to_byte(struct drm_buffer *buffer,
145254794Sdumbbell		int offset)
146254794Sdumbbell{
147254794Sdumbbell	int iter = buffer->iterator + offset;
148254794Sdumbbell	return &buffer->data[iter / PAGE_SIZE][iter & (PAGE_SIZE - 1)];
149254794Sdumbbell}
150254794Sdumbbell
151254794Sdumbbell#endif
152