Merge tag 'sound-fix-5.11-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai...
[linux-2.6-microblaze.git] / drivers / gpu / drm / msm / disp / dpu1 / dpu_hw_blk.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
3  */
4
5 #define pr_fmt(fmt)     "[drm:%s:%d] " fmt, __func__, __LINE__
6
7 #include <linux/mutex.h>
8 #include <linux/errno.h>
9 #include <linux/slab.h>
10
11 #include "dpu_hw_mdss.h"
12 #include "dpu_hw_blk.h"
13
14 /* Serialization lock for dpu_hw_blk_list */
15 static DEFINE_MUTEX(dpu_hw_blk_lock);
16
17 /* List of all hw block objects */
18 static LIST_HEAD(dpu_hw_blk_list);
19
20 /**
21  * dpu_hw_blk_init - initialize hw block object
22  * @hw_blk: pointer to hw block object
23  * @type: hw block type - enum dpu_hw_blk_type
24  * @id: instance id of the hw block
25  * @ops: Pointer to block operations
26  */
27 void dpu_hw_blk_init(struct dpu_hw_blk *hw_blk, u32 type, int id,
28                 struct dpu_hw_blk_ops *ops)
29 {
30         INIT_LIST_HEAD(&hw_blk->list);
31         hw_blk->type = type;
32         hw_blk->id = id;
33         atomic_set(&hw_blk->refcount, 0);
34
35         if (ops)
36                 hw_blk->ops = *ops;
37
38         mutex_lock(&dpu_hw_blk_lock);
39         list_add(&hw_blk->list, &dpu_hw_blk_list);
40         mutex_unlock(&dpu_hw_blk_lock);
41 }
42
43 /**
44  * dpu_hw_blk_destroy - destroy hw block object.
45  * @hw_blk:  pointer to hw block object
46  * return: none
47  */
48 void dpu_hw_blk_destroy(struct dpu_hw_blk *hw_blk)
49 {
50         if (!hw_blk) {
51                 pr_err("invalid parameters\n");
52                 return;
53         }
54
55         if (atomic_read(&hw_blk->refcount))
56                 pr_err("hw_blk:%d.%d invalid refcount\n", hw_blk->type,
57                                 hw_blk->id);
58
59         mutex_lock(&dpu_hw_blk_lock);
60         list_del(&hw_blk->list);
61         mutex_unlock(&dpu_hw_blk_lock);
62 }
63
64 /**
65  * dpu_hw_blk_get - get hw_blk from free pool
66  * @hw_blk: if specified, increment reference count only
67  * @type: if hw_blk is not specified, allocate the next available of this type
68  * @id: if specified (>= 0), allocate the given instance of the above type
69  * return: pointer to hw block object
70  */
71 struct dpu_hw_blk *dpu_hw_blk_get(struct dpu_hw_blk *hw_blk, u32 type, int id)
72 {
73         struct dpu_hw_blk *curr;
74         int rc, refcount;
75
76         if (!hw_blk) {
77                 mutex_lock(&dpu_hw_blk_lock);
78                 list_for_each_entry(curr, &dpu_hw_blk_list, list) {
79                         if ((curr->type != type) ||
80                                         (id >= 0 && curr->id != id) ||
81                                         (id < 0 &&
82                                                 atomic_read(&curr->refcount)))
83                                 continue;
84
85                         hw_blk = curr;
86                         break;
87                 }
88                 mutex_unlock(&dpu_hw_blk_lock);
89         }
90
91         if (!hw_blk) {
92                 pr_debug("no hw_blk:%d\n", type);
93                 return NULL;
94         }
95
96         refcount = atomic_inc_return(&hw_blk->refcount);
97
98         if (refcount == 1 && hw_blk->ops.start) {
99                 rc = hw_blk->ops.start(hw_blk);
100                 if (rc) {
101                         pr_err("failed to start  hw_blk:%d rc:%d\n", type, rc);
102                         goto error_start;
103                 }
104         }
105
106         pr_debug("hw_blk:%d.%d refcount:%d\n", hw_blk->type,
107                         hw_blk->id, refcount);
108         return hw_blk;
109
110 error_start:
111         dpu_hw_blk_put(hw_blk);
112         return ERR_PTR(rc);
113 }
114
115 /**
116  * dpu_hw_blk_put - put hw_blk to free pool if decremented refcount is zero
117  * @hw_blk: hw block to be freed
118  */
119 void dpu_hw_blk_put(struct dpu_hw_blk *hw_blk)
120 {
121         if (!hw_blk) {
122                 pr_err("invalid parameters\n");
123                 return;
124         }
125
126         pr_debug("hw_blk:%d.%d refcount:%d\n", hw_blk->type, hw_blk->id,
127                         atomic_read(&hw_blk->refcount));
128
129         if (!atomic_read(&hw_blk->refcount)) {
130                 pr_err("hw_blk:%d.%d invalid put\n", hw_blk->type, hw_blk->id);
131                 return;
132         }
133
134         if (atomic_dec_return(&hw_blk->refcount))
135                 return;
136
137         if (hw_blk->ops.stop)
138                 hw_blk->ops.stop(hw_blk);
139 }