1 /* Copyright (c) 2013, The Linux Foundation. All rights reserved. 2 * 3 * Redistribution and use in source and binary forms, with or without 4 * modification, are permitted provided that the following conditions are 5 * met: 6 * * Redistributions of source code must retain the above copyright 7 * notice, this list of conditions and the following disclaimer. 8 * * Redistributions in binary form must reproduce the above 9 * copyright notice, this list of conditions and the following 10 * disclaimer in the documentation and/or other materials provided 11 * with the distribution. 12 * * Neither the name of The Linux Foundation nor the names of its 13 * contributors may be used to endorse or promote products derived 14 * from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 */ 29 30 #include <sys/ioctl.h> 31 #include <linux/msm_ion.h> 32 #include "mm_jpeg_ionbuf.h" 33 34 /** buffer_allocate: 35 * 36 * Arguments: 37 * @p_buffer: ION buffer 38 * 39 * Return: 40 * buffer address 41 * 42 * Description: 43 * allocates ION buffer 44 * 45 **/ 46 void *buffer_allocate(buffer_test_t *p_buffer) 47 { 48 void *l_buffer = NULL; 49 50 int lrc = 0; 51 struct ion_handle_data lhandle_data; 52 53 p_buffer->alloc.len = p_buffer->size; 54 p_buffer->alloc.align = 4096; 55 p_buffer->alloc.flags = ION_FLAG_CACHED; 56 p_buffer->alloc.heap_mask = 0x1 << ION_IOMMU_HEAP_ID; 57 58 p_buffer->ion_fd = open("/dev/ion", O_RDONLY); 59 if(p_buffer->ion_fd < 0) { 60 CDBG_ERROR("%s :Ion open failed", __func__); 61 goto ION_ALLOC_FAILED; 62 } 63 64 /* Make it page size aligned */ 65 p_buffer->alloc.len = (p_buffer->alloc.len + 4095) & (~4095); 66 lrc = ioctl(p_buffer->ion_fd, ION_IOC_ALLOC, &p_buffer->alloc); 67 if (lrc < 0) { 68 CDBG_ERROR("%s :ION allocation failed len %d", __func__, 69 p_buffer->alloc.len); 70 goto ION_ALLOC_FAILED; 71 } 72 73 p_buffer->ion_info_fd.handle = p_buffer->alloc.handle; 74 lrc = ioctl(p_buffer->ion_fd, ION_IOC_SHARE, 75 &p_buffer->ion_info_fd); 76 if (lrc < 0) { 77 CDBG_ERROR("%s :ION map failed %s", __func__, strerror(errno)); 78 goto ION_MAP_FAILED; 79 } 80 81 p_buffer->p_pmem_fd = p_buffer->ion_info_fd.fd; 82 83 l_buffer = mmap(NULL, p_buffer->alloc.len, PROT_READ | PROT_WRITE, 84 MAP_SHARED,p_buffer->p_pmem_fd, 0); 85 86 if (l_buffer == MAP_FAILED) { 87 CDBG_ERROR("%s :ION_MMAP_FAILED: %s (%d)", __func__, 88 strerror(errno), errno); 89 goto ION_MAP_FAILED; 90 } 91 92 return l_buffer; 93 94 ION_MAP_FAILED: 95 lhandle_data.handle = p_buffer->ion_info_fd.handle; 96 ioctl(p_buffer->ion_fd, ION_IOC_FREE, &lhandle_data); 97 return NULL; 98 ION_ALLOC_FAILED: 99 return NULL; 100 101 } 102 103 /** buffer_deallocate: 104 * 105 * Arguments: 106 * @p_buffer: ION buffer 107 * 108 * Return: 109 * buffer address 110 * 111 * Description: 112 * deallocates ION buffer 113 * 114 **/ 115 int buffer_deallocate(buffer_test_t *p_buffer) 116 { 117 int lrc = 0; 118 int lsize = (p_buffer->size + 4095) & (~4095); 119 120 struct ion_handle_data lhandle_data; 121 lrc = munmap(p_buffer->addr, lsize); 122 123 close(p_buffer->ion_info_fd.fd); 124 125 lhandle_data.handle = p_buffer->ion_info_fd.handle; 126 ioctl(p_buffer->ion_fd, ION_IOC_FREE, &lhandle_data); 127 128 close(p_buffer->ion_fd); 129 return lrc; 130 } 131 132 133