i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / gpu / drm / drm_buffer.c
blob08ccefedb327d9fb5819d719ef3e15cabf9568ee
1 /**************************************************************************
3 * Copyright 2010 Pauli Nieminen.
4 * All Rights Reserved.
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
16 * of the Software.
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.
31 * Authors:
32 * Pauli Nieminen <suokkos-at-gmail-dot-com>
35 #include <linux/export.h>
36 #include "drm_buffer.h"
38 /**
39 * Allocate the drm buffer object.
41 * buf: Pointer to a pointer where the object is stored.
42 * size: The number of bytes to allocate.
44 int drm_buffer_alloc(struct drm_buffer **buf, int size)
46 int nr_pages = size / PAGE_SIZE + 1;
47 int idx;
49 /* Allocating pointer table to end of structure makes drm_buffer
50 * variable sized */
51 *buf = kzalloc(sizeof(struct drm_buffer) + nr_pages*sizeof(char *),
52 GFP_KERNEL);
54 if (*buf == NULL) {
55 DRM_ERROR("Failed to allocate drm buffer object to hold"
56 " %d bytes in %d pages.\n",
57 size, nr_pages);
58 return -ENOMEM;
61 (*buf)->size = size;
63 for (idx = 0; idx < nr_pages; ++idx) {
65 (*buf)->data[idx] =
66 kmalloc(min(PAGE_SIZE, size - idx * PAGE_SIZE),
67 GFP_KERNEL);
70 if ((*buf)->data[idx] == NULL) {
71 DRM_ERROR("Failed to allocate %dth page for drm"
72 " buffer with %d bytes and %d pages.\n",
73 idx + 1, size, nr_pages);
74 goto error_out;
79 return 0;
81 error_out:
83 /* Only last element can be null pointer so check for it first. */
84 if ((*buf)->data[idx])
85 kfree((*buf)->data[idx]);
87 for (--idx; idx >= 0; --idx)
88 kfree((*buf)->data[idx]);
90 kfree(*buf);
91 return -ENOMEM;
93 EXPORT_SYMBOL(drm_buffer_alloc);
95 /**
96 * Copy the user data to the begin of the buffer and reset the processing
97 * iterator.
99 * user_data: A pointer the data that is copied to the buffer.
100 * size: The Number of bytes to copy.
102 int drm_buffer_copy_from_user(struct drm_buffer *buf,
103 void __user *user_data, int size)
105 int nr_pages = size / PAGE_SIZE + 1;
106 int idx;
108 if (size > buf->size) {
109 DRM_ERROR("Requesting to copy %d bytes to a drm buffer with"
110 " %d bytes space\n",
111 size, buf->size);
112 return -EFAULT;
115 for (idx = 0; idx < nr_pages; ++idx) {
117 if (DRM_COPY_FROM_USER(buf->data[idx],
118 user_data + idx * PAGE_SIZE,
119 min(PAGE_SIZE, size - idx * PAGE_SIZE))) {
120 DRM_ERROR("Failed to copy user data (%p) to drm buffer"
121 " (%p) %dth page.\n",
122 user_data, buf, idx);
123 return -EFAULT;
127 buf->iterator = 0;
128 return 0;
130 EXPORT_SYMBOL(drm_buffer_copy_from_user);
133 * Free the drm buffer object
135 void drm_buffer_free(struct drm_buffer *buf)
138 if (buf != NULL) {
140 int nr_pages = buf->size / PAGE_SIZE + 1;
141 int idx;
142 for (idx = 0; idx < nr_pages; ++idx)
143 kfree(buf->data[idx]);
145 kfree(buf);
148 EXPORT_SYMBOL(drm_buffer_free);
151 * Read an object from buffer that may be split to multiple parts. If object
152 * is not split function just returns the pointer to object in buffer. But in
153 * case of split object data is copied to given stack object that is suplied
154 * by caller.
156 * The processing location of the buffer is also advanced to the next byte
157 * after the object.
159 * objsize: The size of the objet in bytes.
160 * stack_obj: A pointer to a memory location where object can be copied.
162 void *drm_buffer_read_object(struct drm_buffer *buf,
163 int objsize, void *stack_obj)
165 int idx = drm_buffer_index(buf);
166 int page = drm_buffer_page(buf);
167 void *obj = NULL;
169 if (idx + objsize <= PAGE_SIZE) {
170 obj = &buf->data[page][idx];
171 } else {
172 /* The object is split which forces copy to temporary object.*/
173 int beginsz = PAGE_SIZE - idx;
174 memcpy(stack_obj, &buf->data[page][idx], beginsz);
176 memcpy(stack_obj + beginsz, &buf->data[page + 1][0],
177 objsize - beginsz);
179 obj = stack_obj;
182 drm_buffer_advance(buf, objsize);
183 return obj;
185 EXPORT_SYMBOL(drm_buffer_read_object);