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#include <dev/drm2/drm_buffer.h> 39254794Sdumbbell 40254794Sdumbbell/** 41254794Sdumbbell * Allocate the drm buffer object. 42254794Sdumbbell * 43254794Sdumbbell * buf: Pointer to a pointer where the object is stored. 44254794Sdumbbell * size: The number of bytes to allocate. 45254794Sdumbbell */ 46254794Sdumbbellint drm_buffer_alloc(struct drm_buffer **buf, int size) 47254794Sdumbbell{ 48254794Sdumbbell int nr_pages = size / PAGE_SIZE + 1; 49254794Sdumbbell int idx; 50254794Sdumbbell 51254794Sdumbbell /* Allocating pointer table to end of structure makes drm_buffer 52254794Sdumbbell * variable sized */ 53254794Sdumbbell *buf = malloc(sizeof(struct drm_buffer) + nr_pages*sizeof(char *), 54254794Sdumbbell DRM_MEM_DRIVER, M_ZERO | M_WAITOK); 55254794Sdumbbell 56254794Sdumbbell if (*buf == NULL) { 57254794Sdumbbell DRM_ERROR("Failed to allocate drm buffer object to hold" 58254794Sdumbbell " %d bytes in %d pages.\n", 59254794Sdumbbell size, nr_pages); 60254794Sdumbbell return -ENOMEM; 61254794Sdumbbell } 62254794Sdumbbell 63254794Sdumbbell (*buf)->size = size; 64254794Sdumbbell 65254794Sdumbbell for (idx = 0; idx < nr_pages; ++idx) { 66254794Sdumbbell 67254794Sdumbbell (*buf)->data[idx] = 68254794Sdumbbell malloc(min(PAGE_SIZE, size - idx * PAGE_SIZE), 69254794Sdumbbell DRM_MEM_DRIVER, M_WAITOK); 70254794Sdumbbell 71254794Sdumbbell 72254794Sdumbbell if ((*buf)->data[idx] == NULL) { 73254794Sdumbbell DRM_ERROR("Failed to allocate %dth page for drm" 74254794Sdumbbell " buffer with %d bytes and %d pages.\n", 75254794Sdumbbell idx + 1, size, nr_pages); 76254794Sdumbbell goto error_out; 77254794Sdumbbell } 78254794Sdumbbell 79254794Sdumbbell } 80254794Sdumbbell 81254794Sdumbbell return 0; 82254794Sdumbbell 83254794Sdumbbellerror_out: 84254794Sdumbbell 85254794Sdumbbell /* Only last element can be null pointer so check for it first. */ 86254794Sdumbbell if ((*buf)->data[idx]) 87254794Sdumbbell free((*buf)->data[idx], DRM_MEM_DRIVER); 88254794Sdumbbell 89254794Sdumbbell for (--idx; idx >= 0; --idx) 90254794Sdumbbell free((*buf)->data[idx], DRM_MEM_DRIVER); 91254794Sdumbbell 92254794Sdumbbell free(*buf, DRM_MEM_DRIVER); 93254794Sdumbbell return -ENOMEM; 94254794Sdumbbell} 95254794Sdumbbell 96254794Sdumbbell/** 97254794Sdumbbell * Copy the user data to the begin of the buffer and reset the processing 98254794Sdumbbell * iterator. 99254794Sdumbbell * 100254794Sdumbbell * user_data: A pointer the data that is copied to the buffer. 101254794Sdumbbell * size: The Number of bytes to copy. 102254794Sdumbbell */ 103254794Sdumbbellint drm_buffer_copy_from_user(struct drm_buffer *buf, 104254794Sdumbbell void __user *user_data, int size) 105254794Sdumbbell{ 106254794Sdumbbell int nr_pages = size / PAGE_SIZE + 1; 107254794Sdumbbell int idx; 108254794Sdumbbell 109254794Sdumbbell if (size > buf->size) { 110254794Sdumbbell DRM_ERROR("Requesting to copy %d bytes to a drm buffer with" 111254794Sdumbbell " %d bytes space\n", 112254794Sdumbbell size, buf->size); 113254794Sdumbbell return -EFAULT; 114254794Sdumbbell } 115254794Sdumbbell 116254794Sdumbbell for (idx = 0; idx < nr_pages; ++idx) { 117254794Sdumbbell 118254794Sdumbbell if (DRM_COPY_FROM_USER(buf->data[idx], 119254794Sdumbbell (char *)user_data + idx * PAGE_SIZE, 120254794Sdumbbell min(PAGE_SIZE, size - idx * PAGE_SIZE))) { 121254794Sdumbbell DRM_ERROR("Failed to copy user data (%p) to drm buffer" 122254794Sdumbbell " (%p) %dth page.\n", 123254794Sdumbbell user_data, buf, idx); 124254794Sdumbbell return -EFAULT; 125254794Sdumbbell 126254794Sdumbbell } 127254794Sdumbbell } 128254794Sdumbbell buf->iterator = 0; 129254794Sdumbbell return 0; 130254794Sdumbbell} 131254794Sdumbbell 132254794Sdumbbell/** 133254794Sdumbbell * Free the drm buffer object 134254794Sdumbbell */ 135254794Sdumbbellvoid drm_buffer_free(struct drm_buffer *buf) 136254794Sdumbbell{ 137254794Sdumbbell 138254794Sdumbbell if (buf != NULL) { 139254794Sdumbbell 140254794Sdumbbell int nr_pages = buf->size / PAGE_SIZE + 1; 141254794Sdumbbell int idx; 142254794Sdumbbell for (idx = 0; idx < nr_pages; ++idx) 143254794Sdumbbell free(buf->data[idx], DRM_MEM_DRIVER); 144254794Sdumbbell 145254794Sdumbbell free(buf, DRM_MEM_DRIVER); 146254794Sdumbbell } 147254794Sdumbbell} 148254794Sdumbbell 149254794Sdumbbell/** 150254794Sdumbbell * Read an object from buffer that may be split to multiple parts. If object 151254794Sdumbbell * is not split function just returns the pointer to object in buffer. But in 152254794Sdumbbell * case of split object data is copied to given stack object that is suplied 153254794Sdumbbell * by caller. 154254794Sdumbbell * 155254794Sdumbbell * The processing location of the buffer is also advanced to the next byte 156254794Sdumbbell * after the object. 157254794Sdumbbell * 158254794Sdumbbell * objsize: The size of the objet in bytes. 159254794Sdumbbell * stack_obj: A pointer to a memory location where object can be copied. 160254794Sdumbbell */ 161254794Sdumbbellvoid *drm_buffer_read_object(struct drm_buffer *buf, 162254794Sdumbbell int objsize, void *stack_obj) 163254794Sdumbbell{ 164254794Sdumbbell int idx = drm_buffer_index(buf); 165254794Sdumbbell int page = drm_buffer_page(buf); 166254794Sdumbbell void *obj = NULL; 167254794Sdumbbell 168254794Sdumbbell if (idx + objsize <= PAGE_SIZE) { 169254794Sdumbbell obj = &buf->data[page][idx]; 170254794Sdumbbell } else { 171254794Sdumbbell /* The object is split which forces copy to temporary object.*/ 172254794Sdumbbell int beginsz = PAGE_SIZE - idx; 173254794Sdumbbell memcpy(stack_obj, &buf->data[page][idx], beginsz); 174254794Sdumbbell 175254794Sdumbbell memcpy((char *)stack_obj + beginsz, &buf->data[page + 1][0], 176254794Sdumbbell objsize - beginsz); 177254794Sdumbbell 178254794Sdumbbell obj = stack_obj; 179254794Sdumbbell } 180254794Sdumbbell 181254794Sdumbbell drm_buffer_advance(buf, objsize); 182254794Sdumbbell return obj; 183254794Sdumbbell} 184