1 /* Copyright (c) 2017-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 #define pr_fmt(fmt)	"[drm:%s:%d] " fmt, __func__, __LINE__
14 
15 #include <linux/mutex.h>
16 #include <linux/errno.h>
17 #include <linux/slab.h>
18 
19 #include "dpu_hw_mdss.h"
20 #include "dpu_hw_blk.h"
21 
22 /* Serialization lock for dpu_hw_blk_list */
23 static DEFINE_MUTEX(dpu_hw_blk_lock);
24 
25 /* List of all hw block objects */
26 static LIST_HEAD(dpu_hw_blk_list);
27 
28 /**
29  * dpu_hw_blk_init - initialize hw block object
30  * @type: hw block type - enum dpu_hw_blk_type
31  * @id: instance id of the hw block
32  * @ops: Pointer to block operations
33  * return: 0 if success; error code otherwise
34  */
dpu_hw_blk_init(struct dpu_hw_blk * hw_blk,u32 type,int id,struct dpu_hw_blk_ops * ops)35 int dpu_hw_blk_init(struct dpu_hw_blk *hw_blk, u32 type, int id,
36 		struct dpu_hw_blk_ops *ops)
37 {
38 	if (!hw_blk) {
39 		pr_err("invalid parameters\n");
40 		return -EINVAL;
41 	}
42 
43 	INIT_LIST_HEAD(&hw_blk->list);
44 	hw_blk->type = type;
45 	hw_blk->id = id;
46 	atomic_set(&hw_blk->refcount, 0);
47 
48 	if (ops)
49 		hw_blk->ops = *ops;
50 
51 	mutex_lock(&dpu_hw_blk_lock);
52 	list_add(&hw_blk->list, &dpu_hw_blk_list);
53 	mutex_unlock(&dpu_hw_blk_lock);
54 
55 	return 0;
56 }
57 
58 /**
59  * dpu_hw_blk_destroy - destroy hw block object.
60  * @hw_blk:  pointer to hw block object
61  * return: none
62  */
dpu_hw_blk_destroy(struct dpu_hw_blk * hw_blk)63 void dpu_hw_blk_destroy(struct dpu_hw_blk *hw_blk)
64 {
65 	if (!hw_blk) {
66 		pr_err("invalid parameters\n");
67 		return;
68 	}
69 
70 	if (atomic_read(&hw_blk->refcount))
71 		pr_err("hw_blk:%d.%d invalid refcount\n", hw_blk->type,
72 				hw_blk->id);
73 
74 	mutex_lock(&dpu_hw_blk_lock);
75 	list_del(&hw_blk->list);
76 	mutex_unlock(&dpu_hw_blk_lock);
77 }
78 
79 /**
80  * dpu_hw_blk_get - get hw_blk from free pool
81  * @hw_blk: if specified, increment reference count only
82  * @type: if hw_blk is not specified, allocate the next available of this type
83  * @id: if specified (>= 0), allocate the given instance of the above type
84  * return: pointer to hw block object
85  */
dpu_hw_blk_get(struct dpu_hw_blk * hw_blk,u32 type,int id)86 struct dpu_hw_blk *dpu_hw_blk_get(struct dpu_hw_blk *hw_blk, u32 type, int id)
87 {
88 	struct dpu_hw_blk *curr;
89 	int rc, refcount;
90 
91 	if (!hw_blk) {
92 		mutex_lock(&dpu_hw_blk_lock);
93 		list_for_each_entry(curr, &dpu_hw_blk_list, list) {
94 			if ((curr->type != type) ||
95 					(id >= 0 && curr->id != id) ||
96 					(id < 0 &&
97 						atomic_read(&curr->refcount)))
98 				continue;
99 
100 			hw_blk = curr;
101 			break;
102 		}
103 		mutex_unlock(&dpu_hw_blk_lock);
104 	}
105 
106 	if (!hw_blk) {
107 		pr_debug("no hw_blk:%d\n", type);
108 		return NULL;
109 	}
110 
111 	refcount = atomic_inc_return(&hw_blk->refcount);
112 
113 	if (refcount == 1 && hw_blk->ops.start) {
114 		rc = hw_blk->ops.start(hw_blk);
115 		if (rc) {
116 			pr_err("failed to start  hw_blk:%d rc:%d\n", type, rc);
117 			goto error_start;
118 		}
119 	}
120 
121 	pr_debug("hw_blk:%d.%d refcount:%d\n", hw_blk->type,
122 			hw_blk->id, refcount);
123 	return hw_blk;
124 
125 error_start:
126 	dpu_hw_blk_put(hw_blk);
127 	return ERR_PTR(rc);
128 }
129 
130 /**
131  * dpu_hw_blk_put - put hw_blk to free pool if decremented refcount is zero
132  * @hw_blk: hw block to be freed
133  * @free_blk: function to be called when reference count goes to zero
134  */
dpu_hw_blk_put(struct dpu_hw_blk * hw_blk)135 void dpu_hw_blk_put(struct dpu_hw_blk *hw_blk)
136 {
137 	if (!hw_blk) {
138 		pr_err("invalid parameters\n");
139 		return;
140 	}
141 
142 	pr_debug("hw_blk:%d.%d refcount:%d\n", hw_blk->type, hw_blk->id,
143 			atomic_read(&hw_blk->refcount));
144 
145 	if (!atomic_read(&hw_blk->refcount)) {
146 		pr_err("hw_blk:%d.%d invalid put\n", hw_blk->type, hw_blk->id);
147 		return;
148 	}
149 
150 	if (atomic_dec_return(&hw_blk->refcount))
151 		return;
152 
153 	if (hw_blk->ops.stop)
154 		hw_blk->ops.stop(hw_blk);
155 }
156