1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * drivers/media/platform/samsung/s5p-mfc/s5p_mfc_opr.c 4 * 5 * Samsung MFC (Multi Function Codec - FIMV) driver 6 * This file contains hw related functions. 7 * 8 * Kamil Debski, Copyright (c) 2012 Samsung Electronics Co., Ltd. 9 * http://www.samsung.com/ 10 */ 11 12 #include "s5p_mfc_debug.h" 13 #include "s5p_mfc_opr.h" 14 #include "s5p_mfc_opr_v5.h" 15 #include "s5p_mfc_opr_v6.h" 16 17 void s5p_mfc_init_hw_ops(struct s5p_mfc_dev *dev) 18 { 19 if (IS_MFCV6_PLUS(dev)) { 20 dev->mfc_ops = s5p_mfc_init_hw_ops_v6(); 21 dev->warn_start = S5P_FIMV_ERR_WARNINGS_START_V6; 22 } else { 23 dev->mfc_ops = s5p_mfc_init_hw_ops_v5(); 24 dev->warn_start = S5P_FIMV_ERR_WARNINGS_START; 25 } 26 } 27 28 void s5p_mfc_init_regs(struct s5p_mfc_dev *dev) 29 { 30 if (IS_MFCV6_PLUS(dev)) 31 dev->mfc_regs = s5p_mfc_init_regs_v6_plus(dev); 32 } 33 34 int s5p_mfc_alloc_priv_buf(struct s5p_mfc_dev *dev, unsigned int mem_ctx, 35 struct s5p_mfc_priv_buf *b) 36 { 37 unsigned int bits = dev->mem_size >> PAGE_SHIFT; 38 unsigned int count = b->size >> PAGE_SHIFT; 39 unsigned int align = (SZ_64K >> PAGE_SHIFT) - 1; 40 unsigned int start, offset; 41 42 mfc_debug(3, "Allocating priv: %zu\n", b->size); 43 44 if (dev->mem_virt) { 45 start = bitmap_find_next_zero_area(dev->mem_bitmap, bits, 0, count, align); 46 if (start > bits) 47 goto no_mem; 48 49 bitmap_set(dev->mem_bitmap, start, count); 50 offset = start << PAGE_SHIFT; 51 b->virt = dev->mem_virt + offset; 52 b->dma = dev->mem_base + offset; 53 } else { 54 struct device *mem_dev = dev->mem_dev[mem_ctx]; 55 dma_addr_t base = dev->dma_base[mem_ctx]; 56 57 b->ctx = mem_ctx; 58 b->virt = dma_alloc_coherent(mem_dev, b->size, &b->dma, GFP_KERNEL); 59 if (!b->virt) 60 goto no_mem; 61 if (b->dma < base) { 62 mfc_err("Invalid memory configuration - buffer (%pad) is below base memory address(%pad)\n", 63 &b->dma, &base); 64 dma_free_coherent(mem_dev, b->size, b->virt, b->dma); 65 return -ENOMEM; 66 } 67 } 68 69 mfc_debug(3, "Allocated addr %p %pad\n", b->virt, &b->dma); 70 return 0; 71 no_mem: 72 mfc_err("Allocating private buffer of size %zu failed\n", b->size); 73 return -ENOMEM; 74 } 75 76 int s5p_mfc_alloc_generic_buf(struct s5p_mfc_dev *dev, unsigned int mem_ctx, 77 struct s5p_mfc_priv_buf *b) 78 { 79 struct device *mem_dev = dev->mem_dev[mem_ctx]; 80 81 mfc_debug(3, "Allocating generic buf: %zu\n", b->size); 82 83 b->ctx = mem_ctx; 84 b->virt = dma_alloc_coherent(mem_dev, b->size, &b->dma, GFP_KERNEL); 85 if (!b->virt) 86 goto no_mem; 87 88 mfc_debug(3, "Allocated addr %p %pad\n", b->virt, &b->dma); 89 return 0; 90 no_mem: 91 mfc_err("Allocating generic buffer of size %zu failed\n", b->size); 92 return -ENOMEM; 93 } 94 95 void s5p_mfc_release_priv_buf(struct s5p_mfc_dev *dev, 96 struct s5p_mfc_priv_buf *b) 97 { 98 if (dev->mem_virt) { 99 unsigned int start = (b->dma - dev->mem_base) >> PAGE_SHIFT; 100 unsigned int count = b->size >> PAGE_SHIFT; 101 102 bitmap_clear(dev->mem_bitmap, start, count); 103 } else { 104 struct device *mem_dev = dev->mem_dev[b->ctx]; 105 106 dma_free_coherent(mem_dev, b->size, b->virt, b->dma); 107 } 108 b->virt = NULL; 109 b->dma = 0; 110 b->size = 0; 111 } 112 113 void s5p_mfc_release_generic_buf(struct s5p_mfc_dev *dev, 114 struct s5p_mfc_priv_buf *b) 115 { 116 struct device *mem_dev = dev->mem_dev[b->ctx]; 117 dma_free_coherent(mem_dev, b->size, b->virt, b->dma); 118 b->virt = NULL; 119 b->dma = 0; 120 b->size = 0; 121 } 122