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: releng/10.3/sys/dev/drm2/drm_buffer.c 282199 2015-04-28 19:35:05Z dumbbell $"); 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} 95282199SdumbbellEXPORT_SYMBOL(drm_buffer_alloc); 96254794Sdumbbell 97254794Sdumbbell/** 98254794Sdumbbell * Copy the user data to the begin of the buffer and reset the processing 99254794Sdumbbell * iterator. 100254794Sdumbbell * 101254794Sdumbbell * user_data: A pointer the data that is copied to the buffer. 102254794Sdumbbell * size: The Number of bytes to copy. 103254794Sdumbbell */ 104254794Sdumbbellint drm_buffer_copy_from_user(struct drm_buffer *buf, 105254794Sdumbbell void __user *user_data, int size) 106254794Sdumbbell{ 107254794Sdumbbell int nr_pages = size / PAGE_SIZE + 1; 108254794Sdumbbell int idx; 109254794Sdumbbell 110254794Sdumbbell if (size > buf->size) { 111254794Sdumbbell DRM_ERROR("Requesting to copy %d bytes to a drm buffer with" 112254794Sdumbbell " %d bytes space\n", 113254794Sdumbbell size, buf->size); 114254794Sdumbbell return -EFAULT; 115254794Sdumbbell } 116254794Sdumbbell 117254794Sdumbbell for (idx = 0; idx < nr_pages; ++idx) { 118254794Sdumbbell 119254794Sdumbbell if (DRM_COPY_FROM_USER(buf->data[idx], 120254794Sdumbbell (char *)user_data + idx * PAGE_SIZE, 121254794Sdumbbell min(PAGE_SIZE, size - idx * PAGE_SIZE))) { 122254794Sdumbbell DRM_ERROR("Failed to copy user data (%p) to drm buffer" 123254794Sdumbbell " (%p) %dth page.\n", 124254794Sdumbbell user_data, buf, idx); 125254794Sdumbbell return -EFAULT; 126254794Sdumbbell 127254794Sdumbbell } 128254794Sdumbbell } 129254794Sdumbbell buf->iterator = 0; 130254794Sdumbbell return 0; 131254794Sdumbbell} 132282199SdumbbellEXPORT_SYMBOL(drm_buffer_copy_from_user); 133254794Sdumbbell 134254794Sdumbbell/** 135254794Sdumbbell * Free the drm buffer object 136254794Sdumbbell */ 137254794Sdumbbellvoid drm_buffer_free(struct drm_buffer *buf) 138254794Sdumbbell{ 139254794Sdumbbell 140254794Sdumbbell if (buf != NULL) { 141254794Sdumbbell 142254794Sdumbbell int nr_pages = buf->size / PAGE_SIZE + 1; 143254794Sdumbbell int idx; 144254794Sdumbbell for (idx = 0; idx < nr_pages; ++idx) 145254794Sdumbbell free(buf->data[idx], DRM_MEM_DRIVER); 146254794Sdumbbell 147254794Sdumbbell free(buf, DRM_MEM_DRIVER); 148254794Sdumbbell } 149254794Sdumbbell} 150282199SdumbbellEXPORT_SYMBOL(drm_buffer_free); 151254794Sdumbbell 152254794Sdumbbell/** 153254794Sdumbbell * Read an object from buffer that may be split to multiple parts. If object 154254794Sdumbbell * is not split function just returns the pointer to object in buffer. But in 155254794Sdumbbell * case of split object data is copied to given stack object that is suplied 156254794Sdumbbell * by caller. 157254794Sdumbbell * 158254794Sdumbbell * The processing location of the buffer is also advanced to the next byte 159254794Sdumbbell * after the object. 160254794Sdumbbell * 161254794Sdumbbell * objsize: The size of the objet in bytes. 162254794Sdumbbell * stack_obj: A pointer to a memory location where object can be copied. 163254794Sdumbbell */ 164254794Sdumbbellvoid *drm_buffer_read_object(struct drm_buffer *buf, 165254794Sdumbbell int objsize, void *stack_obj) 166254794Sdumbbell{ 167254794Sdumbbell int idx = drm_buffer_index(buf); 168254794Sdumbbell int page = drm_buffer_page(buf); 169254794Sdumbbell void *obj = NULL; 170254794Sdumbbell 171254794Sdumbbell if (idx + objsize <= PAGE_SIZE) { 172254794Sdumbbell obj = &buf->data[page][idx]; 173254794Sdumbbell } else { 174254794Sdumbbell /* The object is split which forces copy to temporary object.*/ 175254794Sdumbbell int beginsz = PAGE_SIZE - idx; 176254794Sdumbbell memcpy(stack_obj, &buf->data[page][idx], beginsz); 177254794Sdumbbell 178254794Sdumbbell memcpy((char *)stack_obj + beginsz, &buf->data[page + 1][0], 179254794Sdumbbell objsize - beginsz); 180254794Sdumbbell 181254794Sdumbbell obj = stack_obj; 182254794Sdumbbell } 183254794Sdumbbell 184254794Sdumbbell drm_buffer_advance(buf, objsize); 185254794Sdumbbell return obj; 186254794Sdumbbell} 187282199SdumbbellEXPORT_SYMBOL(drm_buffer_read_object); 188