1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
5 #define pr_fmt(fmt) "[drm:%s:%d] " fmt, __func__, __LINE__
7 #include <linux/mutex.h>
8 #include <linux/errno.h>
9 #include <linux/slab.h>
11 #include "dpu_hw_mdss.h"
12 #include "dpu_hw_blk.h"
14 /* Serialization lock for dpu_hw_blk_list */
15 static DEFINE_MUTEX(dpu_hw_blk_lock
);
17 /* List of all hw block objects */
18 static LIST_HEAD(dpu_hw_blk_list
);
21 * dpu_hw_blk_init - initialize hw block object
22 * @type: hw block type - enum dpu_hw_blk_type
23 * @id: instance id of the hw block
24 * @ops: Pointer to block operations
26 void dpu_hw_blk_init(struct dpu_hw_blk
*hw_blk
, u32 type
, int id
,
27 struct dpu_hw_blk_ops
*ops
)
29 INIT_LIST_HEAD(&hw_blk
->list
);
32 atomic_set(&hw_blk
->refcount
, 0);
37 mutex_lock(&dpu_hw_blk_lock
);
38 list_add(&hw_blk
->list
, &dpu_hw_blk_list
);
39 mutex_unlock(&dpu_hw_blk_lock
);
43 * dpu_hw_blk_destroy - destroy hw block object.
44 * @hw_blk: pointer to hw block object
47 void dpu_hw_blk_destroy(struct dpu_hw_blk
*hw_blk
)
50 pr_err("invalid parameters\n");
54 if (atomic_read(&hw_blk
->refcount
))
55 pr_err("hw_blk:%d.%d invalid refcount\n", hw_blk
->type
,
58 mutex_lock(&dpu_hw_blk_lock
);
59 list_del(&hw_blk
->list
);
60 mutex_unlock(&dpu_hw_blk_lock
);
64 * dpu_hw_blk_get - get hw_blk from free pool
65 * @hw_blk: if specified, increment reference count only
66 * @type: if hw_blk is not specified, allocate the next available of this type
67 * @id: if specified (>= 0), allocate the given instance of the above type
68 * return: pointer to hw block object
70 struct dpu_hw_blk
*dpu_hw_blk_get(struct dpu_hw_blk
*hw_blk
, u32 type
, int id
)
72 struct dpu_hw_blk
*curr
;
76 mutex_lock(&dpu_hw_blk_lock
);
77 list_for_each_entry(curr
, &dpu_hw_blk_list
, list
) {
78 if ((curr
->type
!= type
) ||
79 (id
>= 0 && curr
->id
!= id
) ||
81 atomic_read(&curr
->refcount
)))
87 mutex_unlock(&dpu_hw_blk_lock
);
91 pr_debug("no hw_blk:%d\n", type
);
95 refcount
= atomic_inc_return(&hw_blk
->refcount
);
97 if (refcount
== 1 && hw_blk
->ops
.start
) {
98 rc
= hw_blk
->ops
.start(hw_blk
);
100 pr_err("failed to start hw_blk:%d rc:%d\n", type
, rc
);
105 pr_debug("hw_blk:%d.%d refcount:%d\n", hw_blk
->type
,
106 hw_blk
->id
, refcount
);
110 dpu_hw_blk_put(hw_blk
);
115 * dpu_hw_blk_put - put hw_blk to free pool if decremented refcount is zero
116 * @hw_blk: hw block to be freed
117 * @free_blk: function to be called when reference count goes to zero
119 void dpu_hw_blk_put(struct dpu_hw_blk
*hw_blk
)
122 pr_err("invalid parameters\n");
126 pr_debug("hw_blk:%d.%d refcount:%d\n", hw_blk
->type
, hw_blk
->id
,
127 atomic_read(&hw_blk
->refcount
));
129 if (!atomic_read(&hw_blk
->refcount
)) {
130 pr_err("hw_blk:%d.%d invalid put\n", hw_blk
->type
, hw_blk
->id
);
134 if (atomic_dec_return(&hw_blk
->refcount
))
137 if (hw_blk
->ops
.stop
)
138 hw_blk
->ops
.stop(hw_blk
);