]>
Commit | Line | Data |
---|---|---|
7a9f0dd9 PN |
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 | ||
2d1a8a48 | 35 | #include <linux/export.h> |
760285e7 | 36 | #include <drm/drm_buffer.h> |
7a9f0dd9 PN |
37 | |
38 | /** | |
39 | * Allocate the drm buffer object. | |
40 | * | |
41 | * buf: Pointer to a pointer where the object is stored. | |
42 | * size: The number of bytes to allocate. | |
43 | */ | |
44 | int drm_buffer_alloc(struct drm_buffer **buf, int size) | |
45 | { | |
46 | int nr_pages = size / PAGE_SIZE + 1; | |
47 | int idx; | |
48 | ||
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); | |
53 | ||
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; | |
59 | } | |
60 | ||
61 | (*buf)->size = size; | |
62 | ||
63 | for (idx = 0; idx < nr_pages; ++idx) { | |
64 | ||
65 | (*buf)->data[idx] = | |
66 | kmalloc(min(PAGE_SIZE, size - idx * PAGE_SIZE), | |
67 | GFP_KERNEL); | |
68 | ||
69 | ||
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; | |
75 | } | |
76 | ||
77 | } | |
78 | ||
79 | return 0; | |
80 | ||
81 | error_out: | |
82 | ||
74f20788 | 83 | for (; idx >= 0; --idx) |
7a9f0dd9 PN |
84 | kfree((*buf)->data[idx]); |
85 | ||
86 | kfree(*buf); | |
87 | return -ENOMEM; | |
88 | } | |
89 | EXPORT_SYMBOL(drm_buffer_alloc); | |
90 | ||
91 | /** | |
92 | * Copy the user data to the begin of the buffer and reset the processing | |
93 | * iterator. | |
94 | * | |
95 | * user_data: A pointer the data that is copied to the buffer. | |
96 | * size: The Number of bytes to copy. | |
97 | */ | |
d3147e86 DB |
98 | int drm_buffer_copy_from_user(struct drm_buffer *buf, |
99 | void __user *user_data, int size) | |
7a9f0dd9 PN |
100 | { |
101 | int nr_pages = size / PAGE_SIZE + 1; | |
102 | int idx; | |
103 | ||
104 | if (size > buf->size) { | |
105 | DRM_ERROR("Requesting to copy %d bytes to a drm buffer with" | |
106 | " %d bytes space\n", | |
107 | size, buf->size); | |
108 | return -EFAULT; | |
109 | } | |
110 | ||
111 | for (idx = 0; idx < nr_pages; ++idx) { | |
112 | ||
1d6ac185 | 113 | if (copy_from_user(buf->data[idx], |
7a9f0dd9 PN |
114 | user_data + idx * PAGE_SIZE, |
115 | min(PAGE_SIZE, size - idx * PAGE_SIZE))) { | |
116 | DRM_ERROR("Failed to copy user data (%p) to drm buffer" | |
117 | " (%p) %dth page.\n", | |
118 | user_data, buf, idx); | |
119 | return -EFAULT; | |
120 | ||
121 | } | |
122 | } | |
123 | buf->iterator = 0; | |
124 | return 0; | |
125 | } | |
126 | EXPORT_SYMBOL(drm_buffer_copy_from_user); | |
127 | ||
128 | /** | |
129 | * Free the drm buffer object | |
130 | */ | |
131 | void drm_buffer_free(struct drm_buffer *buf) | |
132 | { | |
133 | ||
134 | if (buf != NULL) { | |
135 | ||
136 | int nr_pages = buf->size / PAGE_SIZE + 1; | |
137 | int idx; | |
138 | for (idx = 0; idx < nr_pages; ++idx) | |
139 | kfree(buf->data[idx]); | |
140 | ||
141 | kfree(buf); | |
142 | } | |
143 | } | |
144 | EXPORT_SYMBOL(drm_buffer_free); | |
145 | ||
146 | /** | |
147 | * Read an object from buffer that may be split to multiple parts. If object | |
148 | * is not split function just returns the pointer to object in buffer. But in | |
149 | * case of split object data is copied to given stack object that is suplied | |
150 | * by caller. | |
151 | * | |
152 | * The processing location of the buffer is also advanced to the next byte | |
153 | * after the object. | |
154 | * | |
155 | * objsize: The size of the objet in bytes. | |
156 | * stack_obj: A pointer to a memory location where object can be copied. | |
157 | */ | |
158 | void *drm_buffer_read_object(struct drm_buffer *buf, | |
159 | int objsize, void *stack_obj) | |
160 | { | |
161 | int idx = drm_buffer_index(buf); | |
162 | int page = drm_buffer_page(buf); | |
d3147e86 | 163 | void *obj = NULL; |
7a9f0dd9 PN |
164 | |
165 | if (idx + objsize <= PAGE_SIZE) { | |
166 | obj = &buf->data[page][idx]; | |
167 | } else { | |
168 | /* The object is split which forces copy to temporary object.*/ | |
169 | int beginsz = PAGE_SIZE - idx; | |
170 | memcpy(stack_obj, &buf->data[page][idx], beginsz); | |
171 | ||
172 | memcpy(stack_obj + beginsz, &buf->data[page + 1][0], | |
173 | objsize - beginsz); | |
174 | ||
175 | obj = stack_obj; | |
176 | } | |
177 | ||
178 | drm_buffer_advance(buf, objsize); | |
179 | return obj; | |
180 | } | |
181 | EXPORT_SYMBOL(drm_buffer_read_object); |