1 /*
2  * Copyright 2012-15 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25 
26 #include <linux/slab.h>
27 
28 #include "dm_services.h"
29 
30 #include "include/irq_service_interface.h"
31 #include "include/logger_interface.h"
32 
33 #include "dce110/irq_service_dce110.h"
34 
35 
36 #include "dce80/irq_service_dce80.h"
37 
38 #include "dce120/irq_service_dce120.h"
39 
40 
41 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
42 #include "dcn10/irq_service_dcn10.h"
43 #endif
44 
45 #include "reg_helper.h"
46 #include "irq_service.h"
47 
48 
49 
50 #define CTX \
51 		irq_service->ctx
52 #define DC_LOGGER \
53 	irq_service->ctx->logger
54 
55 void dal_irq_service_construct(
56 	struct irq_service *irq_service,
57 	struct irq_service_init_data *init_data)
58 {
59 	if (!init_data || !init_data->ctx) {
60 		BREAK_TO_DEBUGGER();
61 		return;
62 	}
63 
64 	irq_service->ctx = init_data->ctx;
65 }
66 
67 void dal_irq_service_destroy(struct irq_service **irq_service)
68 {
69 	if (!irq_service || !*irq_service) {
70 		BREAK_TO_DEBUGGER();
71 		return;
72 	}
73 
74 	kfree(*irq_service);
75 
76 	*irq_service = NULL;
77 }
78 
79 const struct irq_source_info *find_irq_source_info(
80 	struct irq_service *irq_service,
81 	enum dc_irq_source source)
82 {
83 	if (source >= DAL_IRQ_SOURCES_NUMBER || source < DC_IRQ_SOURCE_INVALID)
84 		return NULL;
85 
86 	return &irq_service->info[source];
87 }
88 
89 void dal_irq_service_set_generic(
90 	struct irq_service *irq_service,
91 	const struct irq_source_info *info,
92 	bool enable)
93 {
94 	uint32_t addr = info->enable_reg;
95 	uint32_t value = dm_read_reg(irq_service->ctx, addr);
96 
97 	value = (value & ~info->enable_mask) |
98 		(info->enable_value[enable ? 0 : 1] & info->enable_mask);
99 	dm_write_reg(irq_service->ctx, addr, value);
100 }
101 
102 bool dal_irq_service_set(
103 	struct irq_service *irq_service,
104 	enum dc_irq_source source,
105 	bool enable)
106 {
107 	const struct irq_source_info *info =
108 		find_irq_source_info(irq_service, source);
109 
110 	if (!info) {
111 		DC_LOG_ERROR("%s: cannot find irq info table entry for %d\n",
112 			__func__,
113 			source);
114 		return false;
115 	}
116 
117 	dal_irq_service_ack(irq_service, source);
118 
119 	if (info->funcs->set)
120 		return info->funcs->set(irq_service, info, enable);
121 
122 	dal_irq_service_set_generic(irq_service, info, enable);
123 
124 	return true;
125 }
126 
127 void dal_irq_service_ack_generic(
128 	struct irq_service *irq_service,
129 	const struct irq_source_info *info)
130 {
131 	uint32_t addr = info->ack_reg;
132 	uint32_t value = dm_read_reg(irq_service->ctx, addr);
133 
134 	value = (value & ~info->ack_mask) |
135 		(info->ack_value & info->ack_mask);
136 	dm_write_reg(irq_service->ctx, addr, value);
137 }
138 
139 bool dal_irq_service_ack(
140 	struct irq_service *irq_service,
141 	enum dc_irq_source source)
142 {
143 	const struct irq_source_info *info =
144 		find_irq_source_info(irq_service, source);
145 
146 	if (!info) {
147 		DC_LOG_ERROR("%s: cannot find irq info table entry for %d\n",
148 			__func__,
149 			source);
150 		return false;
151 	}
152 
153 	if (info->funcs->ack)
154 		return info->funcs->ack(irq_service, info);
155 
156 	dal_irq_service_ack_generic(irq_service, info);
157 
158 	return true;
159 }
160 
161 enum dc_irq_source dal_irq_service_to_irq_source(
162 		struct irq_service *irq_service,
163 		uint32_t src_id,
164 		uint32_t ext_id)
165 {
166 	return irq_service->funcs->to_dal_irq_source(
167 		irq_service,
168 		src_id,
169 		ext_id);
170 }
171