hc
2024-01-03 2f7c68cb55ecb7331f2381deb497c27155f32faf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// SPDX-License-Identifier: GPL-2.0
// Copyright (C) 2018 Intel Corporation
 
#include <linux/device.h>
 
#include "ipu3.h"
#include "ipu3-css-pool.h"
#include "ipu3-dmamap.h"
 
int imgu_css_dma_buffer_resize(struct imgu_device *imgu,
                  struct imgu_css_map *map, size_t size)
{
   if (map->size < size && map->vaddr) {
       dev_warn(&imgu->pci_dev->dev, "dma buf resized from %zu to %zu",
            map->size, size);
 
       imgu_dmamap_free(imgu, map);
       if (!imgu_dmamap_alloc(imgu, map, size))
           return -ENOMEM;
   }
 
   return 0;
}
 
void imgu_css_pool_cleanup(struct imgu_device *imgu, struct imgu_css_pool *pool)
{
   unsigned int i;
 
   for (i = 0; i < IPU3_CSS_POOL_SIZE; i++)
       imgu_dmamap_free(imgu, &pool->entry[i].param);
}
 
int imgu_css_pool_init(struct imgu_device *imgu, struct imgu_css_pool *pool,
              size_t size)
{
   unsigned int i;
 
   for (i = 0; i < IPU3_CSS_POOL_SIZE; i++) {
       pool->entry[i].valid = false;
       if (size == 0) {
           pool->entry[i].param.vaddr = NULL;
           continue;
       }
 
       if (!imgu_dmamap_alloc(imgu, &pool->entry[i].param, size))
           goto fail;
   }
 
   pool->last = IPU3_CSS_POOL_SIZE;
 
   return 0;
 
fail:
   imgu_css_pool_cleanup(imgu, pool);
   return -ENOMEM;
}
 
/*
 * Allocate a new parameter via recycling the oldest entry in the pool.
 */
void imgu_css_pool_get(struct imgu_css_pool *pool)
{
   /* Get the oldest entry */
   u32 n = (pool->last + 1) % IPU3_CSS_POOL_SIZE;
 
   pool->entry[n].valid = true;
   pool->last = n;
}
 
/*
 * Undo, for all practical purposes, the effect of pool_get().
 */
void imgu_css_pool_put(struct imgu_css_pool *pool)
{
   pool->entry[pool->last].valid = false;
   pool->last = (pool->last + IPU3_CSS_POOL_SIZE - 1) % IPU3_CSS_POOL_SIZE;
}
 
/**
 * imgu_css_pool_last - Retrieve the nth pool entry from last
 *
 * @pool: a pointer to &struct imgu_css_pool.
 * @n: the distance to the last index.
 *
 * Returns:
 *  The nth entry from last or null map to indicate no frame stored.
 */
const struct imgu_css_map *
imgu_css_pool_last(struct imgu_css_pool *pool, unsigned int n)
{
   static const struct imgu_css_map null_map = { 0 };
   int i = (pool->last + IPU3_CSS_POOL_SIZE - n) % IPU3_CSS_POOL_SIZE;
 
   WARN_ON(n >= IPU3_CSS_POOL_SIZE);
 
   if (!pool->entry[i].valid)
       return &null_map;
 
   return &pool->entry[i].param;
}