Srinu Gorle | cf8c675 | 2018-01-19 18:36:13 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <asm/dma-iommu.h> |
| 15 | #include <linux/dma-buf.h> |
| 16 | #include <linux/dma-direction.h> |
| 17 | #include <linux/iommu.h> |
| 18 | #include <linux/msm_dma_iommu_mapping.h> |
| 19 | #include <linux/msm_ion.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/types.h> |
| 22 | #include "media/msm_vidc.h" |
| 23 | #include "msm_vidc_debug.h" |
| 24 | #include "msm_vidc_resources.h" |
| 25 | |
| 26 | struct smem_client { |
| 27 | int mem_type; |
| 28 | void *clnt; |
| 29 | struct msm_vidc_platform_resources *res; |
| 30 | enum session_type session_type; |
| 31 | }; |
| 32 | |
| 33 | static int get_device_address(struct smem_client *smem_client, |
| 34 | struct ion_handle *hndl, unsigned long align, |
| 35 | ion_phys_addr_t *iova, unsigned long *buffer_size, |
| 36 | unsigned long flags, enum hal_buffer buffer_type, |
| 37 | struct dma_mapping_info *mapping_info) |
| 38 | { |
| 39 | int rc = 0; |
| 40 | struct ion_client *clnt = NULL; |
| 41 | struct dma_buf *buf = NULL; |
| 42 | struct dma_buf_attachment *attach; |
| 43 | struct sg_table *table = NULL; |
| 44 | struct context_bank_info *cb = NULL; |
| 45 | |
| 46 | if (!iova || !buffer_size || !hndl || !smem_client || !mapping_info) { |
| 47 | dprintk(VIDC_ERR, "Invalid params: %pK, %pK, %pK, %pK\n", |
| 48 | smem_client, hndl, iova, buffer_size); |
| 49 | return -EINVAL; |
| 50 | } |
| 51 | |
| 52 | clnt = smem_client->clnt; |
| 53 | if (!clnt) { |
| 54 | dprintk(VIDC_ERR, "Invalid client\n"); |
| 55 | return -EINVAL; |
| 56 | } |
| 57 | |
| 58 | if (is_iommu_present(smem_client->res)) { |
| 59 | cb = msm_smem_get_context_bank(smem_client, flags & SMEM_SECURE, |
| 60 | buffer_type); |
| 61 | if (!cb) { |
| 62 | dprintk(VIDC_ERR, |
| 63 | "%s: Failed to get context bank device\n", |
| 64 | __func__); |
| 65 | rc = -EIO; |
| 66 | goto mem_map_failed; |
| 67 | } |
| 68 | |
| 69 | /* Convert an Ion handle to a dma buf */ |
| 70 | buf = ion_share_dma_buf(clnt, hndl); |
| 71 | if (IS_ERR_OR_NULL(buf)) { |
| 72 | rc = PTR_ERR(buf) ?: -ENOMEM; |
| 73 | dprintk(VIDC_ERR, "Share ION buf to DMA failed\n"); |
| 74 | goto mem_map_failed; |
| 75 | } |
| 76 | |
| 77 | /* Prepare a dma buf for dma on the given device */ |
| 78 | attach = dma_buf_attach(buf, cb->dev); |
| 79 | if (IS_ERR_OR_NULL(attach)) { |
| 80 | rc = PTR_ERR(attach) ?: -ENOMEM; |
| 81 | dprintk(VIDC_ERR, "Failed to attach dmabuf\n"); |
| 82 | goto mem_buf_attach_failed; |
| 83 | } |
| 84 | |
| 85 | /* Get the scatterlist for the given attachment */ |
| 86 | table = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL); |
| 87 | if (IS_ERR_OR_NULL(table)) { |
| 88 | rc = PTR_ERR(table) ?: -ENOMEM; |
| 89 | dprintk(VIDC_ERR, "Failed to map table\n"); |
| 90 | goto mem_map_table_failed; |
| 91 | } |
| 92 | |
| 93 | /* debug trace's need to be updated later */ |
| 94 | trace_msm_smem_buffer_iommu_op_start("MAP", 0, 0, |
| 95 | align, *iova, *buffer_size); |
| 96 | |
| 97 | /* Map a scatterlist into an SMMU */ |
| 98 | rc = msm_dma_map_sg_lazy(cb->dev, table->sgl, table->nents, |
| 99 | DMA_BIDIRECTIONAL, buf); |
| 100 | if (rc != table->nents) { |
| 101 | dprintk(VIDC_ERR, |
| 102 | "Mapping failed with rc(%d), expected rc(%d)\n", |
| 103 | rc, table->nents); |
| 104 | rc = -ENOMEM; |
| 105 | goto mem_map_sg_failed; |
| 106 | } |
| 107 | if (table->sgl) { |
| 108 | dprintk(VIDC_DBG, |
| 109 | "%s: CB : %s, DMA buf: %pK, device: %pK, attach: %pK, table: %pK, table sgl: %pK, rc: %d, dma_address: %pa\n", |
| 110 | __func__, cb->name, buf, cb->dev, attach, |
| 111 | table, table->sgl, rc, |
| 112 | &table->sgl->dma_address); |
| 113 | |
| 114 | *iova = table->sgl->dma_address; |
| 115 | *buffer_size = table->sgl->dma_length; |
| 116 | } else { |
| 117 | dprintk(VIDC_ERR, "sgl is NULL\n"); |
| 118 | rc = -ENOMEM; |
| 119 | goto mem_map_sg_failed; |
| 120 | } |
| 121 | |
| 122 | mapping_info->dev = cb->dev; |
| 123 | mapping_info->mapping = cb->mapping; |
| 124 | mapping_info->table = table; |
| 125 | mapping_info->attach = attach; |
| 126 | mapping_info->buf = buf; |
| 127 | |
| 128 | trace_msm_smem_buffer_iommu_op_end("MAP", 0, 0, |
| 129 | align, *iova, *buffer_size); |
| 130 | } else { |
| 131 | dprintk(VIDC_DBG, "Using physical memory address\n"); |
| 132 | rc = ion_phys(clnt, hndl, iova, (size_t *)buffer_size); |
| 133 | if (rc) { |
| 134 | dprintk(VIDC_ERR, "ion memory map failed - %d\n", rc); |
| 135 | goto mem_map_failed; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | dprintk(VIDC_DBG, "mapped ion handle %pK to %pa\n", hndl, iova); |
| 140 | return 0; |
| 141 | mem_map_sg_failed: |
| 142 | dma_buf_unmap_attachment(attach, table, DMA_BIDIRECTIONAL); |
| 143 | mem_map_table_failed: |
| 144 | dma_buf_detach(buf, attach); |
| 145 | mem_buf_attach_failed: |
| 146 | dma_buf_put(buf); |
| 147 | mem_map_failed: |
| 148 | return rc; |
| 149 | } |
| 150 | |
| 151 | static void put_device_address(struct smem_client *smem_client, |
| 152 | struct ion_handle *hndl, u32 flags, |
| 153 | struct dma_mapping_info *mapping_info, |
| 154 | enum hal_buffer buffer_type) |
| 155 | { |
| 156 | struct ion_client *clnt = NULL; |
| 157 | |
| 158 | if (!hndl || !smem_client || !mapping_info) { |
| 159 | dprintk(VIDC_WARN, "Invalid params: %pK, %pK\n", |
| 160 | smem_client, hndl); |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | if (!mapping_info->dev || !mapping_info->table || |
| 165 | !mapping_info->buf || !mapping_info->attach) { |
| 166 | dprintk(VIDC_WARN, "Invalid params:\n"); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | clnt = smem_client->clnt; |
| 171 | if (!clnt) { |
| 172 | dprintk(VIDC_WARN, "Invalid client\n"); |
| 173 | return; |
| 174 | } |
| 175 | if (is_iommu_present(smem_client->res)) { |
| 176 | dprintk(VIDC_DBG, |
| 177 | "Calling dma_unmap_sg - device: %pK, address: %pa, buf: %pK, table: %pK, attach: %pK\n", |
| 178 | mapping_info->dev, |
| 179 | &mapping_info->table->sgl->dma_address, |
| 180 | mapping_info->buf, mapping_info->table, |
| 181 | mapping_info->attach); |
| 182 | |
| 183 | trace_msm_smem_buffer_iommu_op_start("UNMAP", 0, 0, 0, 0, 0); |
| 184 | msm_dma_unmap_sg(mapping_info->dev, mapping_info->table->sgl, |
| 185 | mapping_info->table->nents, DMA_BIDIRECTIONAL, |
| 186 | mapping_info->buf); |
| 187 | dma_buf_unmap_attachment(mapping_info->attach, |
| 188 | mapping_info->table, DMA_BIDIRECTIONAL); |
| 189 | dma_buf_detach(mapping_info->buf, mapping_info->attach); |
| 190 | dma_buf_put(mapping_info->buf); |
| 191 | trace_msm_smem_buffer_iommu_op_end("UNMAP", 0, 0, 0, 0, 0); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | static int ion_user_to_kernel(struct smem_client *client, int fd, u32 offset, |
| 196 | struct msm_smem *mem, enum hal_buffer buffer_type) |
| 197 | { |
| 198 | struct ion_handle *hndl; |
| 199 | ion_phys_addr_t iova = 0; |
| 200 | unsigned long buffer_size = 0; |
| 201 | int rc = 0; |
| 202 | unsigned long align = SZ_4K; |
| 203 | unsigned long ion_flags = 0; |
| 204 | |
| 205 | hndl = ion_import_dma_buf_fd(client->clnt, fd); |
| 206 | dprintk(VIDC_DBG, "%s ion handle: %pK\n", __func__, hndl); |
| 207 | if (IS_ERR_OR_NULL(hndl)) { |
| 208 | dprintk(VIDC_ERR, "Failed to get handle: %pK, %d, %d, %pK\n", |
| 209 | client, fd, offset, hndl); |
| 210 | rc = -ENOMEM; |
| 211 | goto fail_import_fd; |
| 212 | } |
| 213 | mem->kvaddr = NULL; |
| 214 | rc = ion_handle_get_flags(client->clnt, hndl, &ion_flags); |
| 215 | if (rc) { |
| 216 | dprintk(VIDC_ERR, "Failed to get ion flags: %d\n", rc); |
| 217 | goto fail_device_address; |
| 218 | } |
| 219 | |
| 220 | mem->buffer_type = buffer_type; |
| 221 | if (ion_flags & ION_FLAG_CACHED) |
| 222 | mem->flags |= SMEM_CACHED; |
| 223 | |
| 224 | if (ion_flags & ION_FLAG_SECURE) |
| 225 | mem->flags |= SMEM_SECURE; |
| 226 | |
| 227 | rc = get_device_address(client, hndl, align, &iova, &buffer_size, |
| 228 | mem->flags, buffer_type, &mem->mapping_info); |
| 229 | if (rc) { |
| 230 | dprintk(VIDC_ERR, "Failed to get device address: %d\n", rc); |
| 231 | goto fail_device_address; |
| 232 | } |
| 233 | |
| 234 | mem->mem_type = client->mem_type; |
| 235 | mem->smem_priv = hndl; |
| 236 | mem->device_addr = iova; |
| 237 | mem->size = buffer_size; |
| 238 | if ((u32)mem->device_addr != iova) { |
| 239 | dprintk(VIDC_ERR, "iova(%pa) truncated to %#x", |
| 240 | &iova, (u32)mem->device_addr); |
| 241 | goto fail_device_address; |
| 242 | } |
| 243 | dprintk(VIDC_DBG, |
| 244 | "%s: ion_handle = %pK, fd = %d, device_addr = %pa, size = %u, kvaddr = %pK, buffer_type = %d, flags = %#lx\n", |
| 245 | __func__, mem->smem_priv, fd, &mem->device_addr, mem->size, |
| 246 | mem->kvaddr, mem->buffer_type, mem->flags); |
| 247 | return rc; |
| 248 | fail_device_address: |
| 249 | ion_free(client->clnt, hndl); |
| 250 | fail_import_fd: |
| 251 | return rc; |
| 252 | } |
| 253 | |
| 254 | static int get_secure_flag_for_buffer_type( |
| 255 | struct smem_client *client, enum hal_buffer buffer_type) |
| 256 | { |
| 257 | |
| 258 | if (!client) { |
| 259 | dprintk(VIDC_ERR, "%s - invalid params\n", __func__); |
| 260 | return -EINVAL; |
| 261 | } |
| 262 | |
| 263 | switch (buffer_type) { |
| 264 | case HAL_BUFFER_INPUT: |
| 265 | if (client->session_type == MSM_VIDC_ENCODER) |
| 266 | return ION_FLAG_CP_PIXEL; |
| 267 | else |
| 268 | return ION_FLAG_CP_BITSTREAM; |
| 269 | case HAL_BUFFER_OUTPUT: |
| 270 | case HAL_BUFFER_OUTPUT2: |
| 271 | if (client->session_type == MSM_VIDC_ENCODER) |
| 272 | return ION_FLAG_CP_BITSTREAM; |
| 273 | else |
| 274 | return ION_FLAG_CP_PIXEL; |
| 275 | case HAL_BUFFER_INTERNAL_SCRATCH: |
| 276 | return ION_FLAG_CP_BITSTREAM; |
| 277 | case HAL_BUFFER_INTERNAL_SCRATCH_1: |
| 278 | return ION_FLAG_CP_NON_PIXEL; |
| 279 | case HAL_BUFFER_INTERNAL_SCRATCH_2: |
| 280 | return ION_FLAG_CP_PIXEL; |
| 281 | case HAL_BUFFER_INTERNAL_PERSIST: |
| 282 | return ION_FLAG_CP_BITSTREAM; |
| 283 | case HAL_BUFFER_INTERNAL_PERSIST_1: |
| 284 | return ION_FLAG_CP_NON_PIXEL; |
| 285 | default: |
| 286 | WARN(1, "No matching secure flag for buffer type : %x\n", |
| 287 | buffer_type); |
| 288 | return -EINVAL; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | static int alloc_ion_mem(struct smem_client *client, size_t size, u32 align, |
| 293 | u32 flags, enum hal_buffer buffer_type, struct msm_smem *mem, |
| 294 | int map_kernel) |
| 295 | { |
| 296 | struct ion_handle *hndl; |
| 297 | ion_phys_addr_t iova = 0; |
| 298 | unsigned long buffer_size = 0; |
| 299 | unsigned long heap_mask = 0; |
| 300 | int rc = 0; |
| 301 | int ion_flags = 0; |
| 302 | |
| 303 | align = ALIGN(align, SZ_4K); |
| 304 | size = ALIGN(size, SZ_4K); |
| 305 | |
| 306 | if (is_iommu_present(client->res)) { |
| 307 | heap_mask = ION_HEAP(ION_IOMMU_HEAP_ID); |
| 308 | } else { |
| 309 | dprintk(VIDC_DBG, |
| 310 | "allocate shared memory from adsp heap size %zx align %d\n", |
| 311 | size, align); |
| 312 | heap_mask = ION_HEAP(ION_ADSP_HEAP_ID); |
| 313 | } |
| 314 | |
| 315 | if (flags & SMEM_CACHED) |
| 316 | ion_flags |= ION_FLAG_CACHED; |
| 317 | |
| 318 | if (flags & SMEM_SECURE) { |
| 319 | int secure_flag = |
| 320 | get_secure_flag_for_buffer_type(client, buffer_type); |
| 321 | if (secure_flag < 0) { |
| 322 | rc = secure_flag; |
| 323 | goto fail_shared_mem_alloc; |
| 324 | } |
| 325 | |
| 326 | ion_flags |= ION_FLAG_SECURE | secure_flag; |
| 327 | heap_mask = ION_HEAP(ION_SECURE_HEAP_ID); |
| 328 | |
| 329 | if (client->res->slave_side_cp) { |
| 330 | heap_mask = ION_HEAP(ION_CP_MM_HEAP_ID); |
| 331 | size = ALIGN(size, SZ_1M); |
| 332 | align = ALIGN(size, SZ_1M); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | trace_msm_smem_buffer_ion_op_start("ALLOC", (u32)buffer_type, |
| 337 | heap_mask, size, align, flags, map_kernel); |
| 338 | hndl = ion_alloc(client->clnt, size, align, heap_mask, ion_flags); |
| 339 | if (IS_ERR_OR_NULL(hndl)) { |
| 340 | dprintk(VIDC_ERR, |
| 341 | "Failed to allocate shared memory = %pK, %zx, %d, %#x\n", |
| 342 | client, size, align, flags); |
| 343 | rc = -ENOMEM; |
| 344 | goto fail_shared_mem_alloc; |
| 345 | } |
| 346 | trace_msm_smem_buffer_ion_op_end("ALLOC", (u32)buffer_type, |
| 347 | heap_mask, size, align, flags, map_kernel); |
| 348 | mem->mem_type = client->mem_type; |
| 349 | mem->smem_priv = hndl; |
| 350 | mem->flags = flags; |
| 351 | mem->buffer_type = buffer_type; |
| 352 | if (map_kernel) { |
| 353 | mem->kvaddr = ion_map_kernel(client->clnt, hndl); |
| 354 | if (IS_ERR_OR_NULL(mem->kvaddr)) { |
| 355 | dprintk(VIDC_ERR, |
| 356 | "Failed to map shared mem in kernel\n"); |
| 357 | rc = -EIO; |
| 358 | goto fail_map; |
| 359 | } |
| 360 | } else { |
| 361 | mem->kvaddr = NULL; |
| 362 | } |
| 363 | |
| 364 | rc = get_device_address(client, hndl, align, &iova, &buffer_size, |
| 365 | flags, buffer_type, &mem->mapping_info); |
| 366 | if (rc) { |
| 367 | dprintk(VIDC_ERR, "Failed to get device address: %d\n", |
| 368 | rc); |
| 369 | goto fail_device_address; |
| 370 | } |
| 371 | mem->device_addr = iova; |
| 372 | if ((u32)mem->device_addr != iova) { |
| 373 | dprintk(VIDC_ERR, "iova(%pa) truncated to %#x", |
| 374 | &iova, (u32)mem->device_addr); |
| 375 | goto fail_device_address; |
| 376 | } |
| 377 | mem->size = size; |
| 378 | dprintk(VIDC_DBG, |
| 379 | "%s: ion_handle = %pK, device_addr = %pa, size = %u, kvaddr = %pK, buffer_type = %#x, flags = %#lx\n", |
| 380 | __func__, mem->smem_priv, &mem->device_addr, |
| 381 | mem->size, mem->kvaddr, mem->buffer_type, mem->flags); |
| 382 | return rc; |
| 383 | fail_device_address: |
| 384 | if (mem->kvaddr) |
| 385 | ion_unmap_kernel(client->clnt, hndl); |
| 386 | fail_map: |
| 387 | ion_free(client->clnt, hndl); |
| 388 | fail_shared_mem_alloc: |
| 389 | return rc; |
| 390 | } |
| 391 | |
| 392 | static void free_ion_mem(struct smem_client *client, struct msm_smem *mem) |
| 393 | { |
| 394 | dprintk(VIDC_DBG, |
| 395 | "%s: ion_handle = %pK, device_addr = %pa, size = %u, kvaddr = %pK, buffer_type = %#x\n", |
| 396 | __func__, mem->smem_priv, &mem->device_addr, |
| 397 | mem->size, mem->kvaddr, mem->buffer_type); |
| 398 | |
| 399 | if (mem->device_addr) |
| 400 | put_device_address(client, mem->smem_priv, mem->flags, |
| 401 | &mem->mapping_info, mem->buffer_type); |
| 402 | |
| 403 | if (mem->kvaddr) |
| 404 | ion_unmap_kernel(client->clnt, mem->smem_priv); |
| 405 | if (mem->smem_priv) { |
| 406 | trace_msm_smem_buffer_ion_op_start("FREE", |
| 407 | (u32)mem->buffer_type, -1, mem->size, -1, |
| 408 | mem->flags, -1); |
| 409 | dprintk(VIDC_DBG, |
| 410 | "%s: Freeing handle %pK, client: %pK\n", |
| 411 | __func__, mem->smem_priv, client->clnt); |
| 412 | ion_free(client->clnt, mem->smem_priv); |
| 413 | trace_msm_smem_buffer_ion_op_end("FREE", (u32)mem->buffer_type, |
| 414 | -1, mem->size, -1, mem->flags, -1); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | static void *ion_new_client(void) |
| 419 | { |
| 420 | struct ion_client *client = NULL; |
| 421 | |
| 422 | client = msm_ion_client_create("video_client"); |
| 423 | if (!client) |
| 424 | dprintk(VIDC_ERR, "Failed to create smem client\n"); |
| 425 | return client; |
| 426 | }; |
| 427 | |
| 428 | static void ion_delete_client(struct smem_client *client) |
| 429 | { |
| 430 | ion_client_destroy(client->clnt); |
| 431 | } |
| 432 | |
| 433 | struct msm_smem *msm_smem_user_to_kernel(void *clt, int fd, u32 offset, |
| 434 | enum hal_buffer buffer_type) |
| 435 | { |
| 436 | struct smem_client *client = clt; |
| 437 | int rc = 0; |
| 438 | struct msm_smem *mem; |
| 439 | |
| 440 | if (fd < 0) { |
| 441 | dprintk(VIDC_ERR, "Invalid fd: %d\n", fd); |
| 442 | return NULL; |
| 443 | } |
| 444 | mem = kzalloc(sizeof(*mem), GFP_KERNEL); |
| 445 | if (!mem) { |
| 446 | dprintk(VIDC_ERR, "Failed to allocate shared mem\n"); |
| 447 | return NULL; |
| 448 | } |
| 449 | switch (client->mem_type) { |
| 450 | case SMEM_ION: |
| 451 | rc = ion_user_to_kernel(clt, fd, offset, mem, buffer_type); |
| 452 | break; |
| 453 | default: |
| 454 | dprintk(VIDC_ERR, "Mem type not supported\n"); |
| 455 | rc = -EINVAL; |
| 456 | break; |
| 457 | } |
| 458 | if (rc) { |
| 459 | dprintk(VIDC_ERR, "Failed to allocate shared memory\n"); |
| 460 | kfree(mem); |
| 461 | mem = NULL; |
| 462 | } |
| 463 | return mem; |
| 464 | } |
| 465 | |
| 466 | bool msm_smem_compare_buffers(void *clt, int fd, void *priv) |
| 467 | { |
| 468 | struct smem_client *client = clt; |
| 469 | struct ion_handle *handle = NULL; |
| 470 | bool ret = false; |
| 471 | |
| 472 | if (!clt || !priv) { |
| 473 | dprintk(VIDC_ERR, "Invalid params: %pK, %pK\n", |
| 474 | clt, priv); |
| 475 | return false; |
| 476 | } |
| 477 | handle = ion_import_dma_buf_fd(client->clnt, fd); |
| 478 | ret = handle == priv; |
| 479 | handle ? ion_free(client->clnt, handle) : 0; |
| 480 | return ret; |
| 481 | } |
| 482 | |
| 483 | static int ion_cache_operations(struct smem_client *client, |
| 484 | struct msm_smem *mem, enum smem_cache_ops cache_op) |
| 485 | { |
| 486 | unsigned long ionflag = 0; |
| 487 | int rc = 0; |
| 488 | int msm_cache_ops = 0; |
| 489 | |
| 490 | if (!mem || !client) { |
| 491 | dprintk(VIDC_ERR, "Invalid params: %pK, %pK\n", |
| 492 | mem, client); |
| 493 | return -EINVAL; |
| 494 | } |
| 495 | rc = ion_handle_get_flags(client->clnt, mem->smem_priv, |
| 496 | &ionflag); |
| 497 | if (rc) { |
| 498 | dprintk(VIDC_ERR, |
| 499 | "ion_handle_get_flags failed: %d\n", rc); |
| 500 | goto cache_op_failed; |
| 501 | } |
| 502 | if (ION_IS_CACHED(ionflag)) { |
| 503 | switch (cache_op) { |
| 504 | case SMEM_CACHE_CLEAN: |
| 505 | msm_cache_ops = ION_IOC_CLEAN_CACHES; |
| 506 | break; |
| 507 | case SMEM_CACHE_INVALIDATE: |
| 508 | msm_cache_ops = ION_IOC_INV_CACHES; |
| 509 | break; |
| 510 | case SMEM_CACHE_CLEAN_INVALIDATE: |
| 511 | msm_cache_ops = ION_IOC_CLEAN_INV_CACHES; |
| 512 | break; |
| 513 | default: |
| 514 | dprintk(VIDC_ERR, "cache operation not supported\n"); |
| 515 | rc = -EINVAL; |
| 516 | goto cache_op_failed; |
| 517 | } |
| 518 | rc = msm_ion_do_cache_op(client->clnt, |
| 519 | (struct ion_handle *)mem->smem_priv, |
| 520 | 0, (unsigned long)mem->size, |
| 521 | msm_cache_ops); |
| 522 | if (rc) { |
| 523 | dprintk(VIDC_ERR, |
| 524 | "cache operation failed %d\n", rc); |
| 525 | goto cache_op_failed; |
| 526 | } |
| 527 | } |
| 528 | cache_op_failed: |
| 529 | return rc; |
| 530 | } |
| 531 | |
| 532 | int msm_smem_cache_operations(void *clt, struct msm_smem *mem, |
| 533 | enum smem_cache_ops cache_op) |
| 534 | { |
| 535 | struct smem_client *client = clt; |
| 536 | int rc = 0; |
| 537 | |
| 538 | if (!client) { |
| 539 | dprintk(VIDC_ERR, "Invalid params: %pK\n", |
| 540 | client); |
| 541 | return -EINVAL; |
| 542 | } |
| 543 | switch (client->mem_type) { |
| 544 | case SMEM_ION: |
| 545 | rc = ion_cache_operations(client, mem, cache_op); |
| 546 | if (rc) |
| 547 | dprintk(VIDC_ERR, |
| 548 | "Failed cache operations: %d\n", rc); |
| 549 | break; |
| 550 | default: |
| 551 | dprintk(VIDC_ERR, "Mem type not supported\n"); |
| 552 | break; |
| 553 | } |
| 554 | return rc; |
| 555 | } |
| 556 | |
| 557 | void *msm_smem_new_client(enum smem_type mtype, |
| 558 | void *platform_resources, enum session_type stype) |
| 559 | { |
| 560 | struct smem_client *client = NULL; |
| 561 | void *clnt = NULL; |
| 562 | struct msm_vidc_platform_resources *res = platform_resources; |
| 563 | |
| 564 | switch (mtype) { |
| 565 | case SMEM_ION: |
| 566 | clnt = ion_new_client(); |
| 567 | break; |
| 568 | default: |
| 569 | dprintk(VIDC_ERR, "Mem type not supported\n"); |
| 570 | break; |
| 571 | } |
| 572 | if (clnt) { |
| 573 | client = kzalloc(sizeof(*client), GFP_KERNEL); |
| 574 | if (client) { |
| 575 | client->mem_type = mtype; |
| 576 | client->clnt = clnt; |
| 577 | client->res = res; |
| 578 | client->session_type = stype; |
| 579 | } |
| 580 | } else { |
| 581 | dprintk(VIDC_ERR, "Failed to create new client: mtype = %d\n", |
| 582 | mtype); |
| 583 | } |
| 584 | return client; |
| 585 | } |
| 586 | |
| 587 | struct msm_smem *msm_smem_alloc(void *clt, size_t size, u32 align, u32 flags, |
| 588 | enum hal_buffer buffer_type, int map_kernel) |
| 589 | { |
| 590 | struct smem_client *client; |
| 591 | int rc = 0; |
| 592 | struct msm_smem *mem; |
| 593 | |
| 594 | client = clt; |
| 595 | if (!client) { |
| 596 | dprintk(VIDC_ERR, "Invalid client passed\n"); |
| 597 | return NULL; |
| 598 | } |
| 599 | if (!size) { |
| 600 | dprintk(VIDC_ERR, "No need to allocate memory of size: %zx\n", |
| 601 | size); |
| 602 | return NULL; |
| 603 | } |
| 604 | mem = kzalloc(sizeof(*mem), GFP_KERNEL); |
| 605 | if (!mem) { |
| 606 | dprintk(VIDC_ERR, "Failed to allocate shared mem\n"); |
| 607 | return NULL; |
| 608 | } |
| 609 | switch (client->mem_type) { |
| 610 | case SMEM_ION: |
| 611 | rc = alloc_ion_mem(client, size, align, flags, buffer_type, |
| 612 | mem, map_kernel); |
| 613 | break; |
| 614 | default: |
| 615 | dprintk(VIDC_ERR, "Mem type not supported\n"); |
| 616 | rc = -EINVAL; |
| 617 | break; |
| 618 | } |
| 619 | if (rc) { |
| 620 | dprintk(VIDC_ERR, "Failed to allocate shared memory\n"); |
| 621 | kfree(mem); |
| 622 | mem = NULL; |
| 623 | } |
| 624 | return mem; |
| 625 | } |
| 626 | |
| 627 | void msm_smem_free(void *clt, struct msm_smem *mem) |
| 628 | { |
| 629 | struct smem_client *client = clt; |
| 630 | |
| 631 | if (!client || !mem) { |
| 632 | dprintk(VIDC_ERR, "Invalid client/handle passed\n"); |
| 633 | return; |
| 634 | } |
| 635 | switch (client->mem_type) { |
| 636 | case SMEM_ION: |
| 637 | free_ion_mem(client, mem); |
| 638 | break; |
| 639 | default: |
| 640 | dprintk(VIDC_ERR, "Mem type not supported\n"); |
| 641 | break; |
| 642 | } |
| 643 | kfree(mem); |
| 644 | }; |
| 645 | |
| 646 | void msm_smem_delete_client(void *clt) |
| 647 | { |
| 648 | struct smem_client *client = clt; |
| 649 | |
| 650 | if (!client) { |
| 651 | dprintk(VIDC_ERR, "Invalid client passed\n"); |
| 652 | return; |
| 653 | } |
| 654 | switch (client->mem_type) { |
| 655 | case SMEM_ION: |
| 656 | ion_delete_client(client); |
| 657 | break; |
| 658 | default: |
| 659 | dprintk(VIDC_ERR, "Mem type not supported\n"); |
| 660 | break; |
| 661 | } |
| 662 | kfree(client); |
| 663 | } |
| 664 | |
| 665 | struct context_bank_info *msm_smem_get_context_bank(void *clt, |
| 666 | bool is_secure, enum hal_buffer buffer_type) |
| 667 | { |
| 668 | struct smem_client *client = clt; |
| 669 | struct context_bank_info *cb = NULL, *match = NULL; |
| 670 | |
| 671 | if (!clt) { |
| 672 | dprintk(VIDC_ERR, "%s - invalid params\n", __func__); |
| 673 | return NULL; |
| 674 | } |
| 675 | |
| 676 | /* |
| 677 | * HAL_BUFFER_INPUT is directly mapped to bitstream CB in DT |
| 678 | * as the buffer type structure was initially designed |
| 679 | * just for decoder. For Encoder, input should be mapped to |
| 680 | * pixel_CB. So swap the buffer types just in this local scope. |
| 681 | */ |
| 682 | if (is_secure && client->session_type == MSM_VIDC_ENCODER) { |
| 683 | if (buffer_type == HAL_BUFFER_INPUT) |
| 684 | buffer_type = HAL_BUFFER_OUTPUT; |
| 685 | else if (buffer_type == HAL_BUFFER_OUTPUT) |
| 686 | buffer_type = HAL_BUFFER_INPUT; |
| 687 | } |
| 688 | |
| 689 | list_for_each_entry(cb, &client->res->context_banks, list) { |
| 690 | if (cb->is_secure == is_secure && |
| 691 | cb->buffer_type & buffer_type) { |
| 692 | match = cb; |
| 693 | dprintk(VIDC_DBG, |
| 694 | "context bank found for CB : %s, device: %pK mapping: %pK\n", |
| 695 | match->name, match->dev, match->mapping); |
| 696 | break; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | return match; |
| 701 | } |