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 "dm_services.h"
27 
28 #include "include/irq_service_interface.h"
29 #include "include/logger_interface.h"
30 
31 #include "dce110/irq_service_dce110.h"
32 
33 
34 #include "dce80/irq_service_dce80.h"
35 
36 #include "dce120/irq_service_dce120.h"
37 
38 
39 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
40 #include "dcn10/irq_service_dcn10.h"
41 #endif
42 
43 #include "reg_helper.h"
44 #include "irq_service.h"
45 
46 
47 
48 #define CTX \
49 		irq_service->ctx
50 
51 bool dal_irq_service_construct(
52 	struct irq_service *irq_service,
53 	struct irq_service_init_data *init_data)
54 {
55 	if (!init_data || !init_data->ctx)
56 		return false;
57 
58 	irq_service->ctx = init_data->ctx;
59 	return true;
60 }
61 
62 void dal_irq_service_destroy(struct irq_service **irq_service)
63 {
64 	if (!irq_service || !*irq_service) {
65 		BREAK_TO_DEBUGGER();
66 		return;
67 	}
68 
69 	kfree(*irq_service);
70 
71 	*irq_service = NULL;
72 }
73 
74 const struct irq_source_info *find_irq_source_info(
75 	struct irq_service *irq_service,
76 	enum dc_irq_source source)
77 {
78 	if (source > DAL_IRQ_SOURCES_NUMBER || source < DC_IRQ_SOURCE_INVALID)
79 		return NULL;
80 
81 	return &irq_service->info[source];
82 }
83 
84 void dal_irq_service_set_generic(
85 	struct irq_service *irq_service,
86 	const struct irq_source_info *info,
87 	bool enable)
88 {
89 	uint32_t addr = info->enable_reg;
90 	uint32_t value = dm_read_reg(irq_service->ctx, addr);
91 
92 	value = (value & ~info->enable_mask) |
93 		(info->enable_value[enable ? 0 : 1] & info->enable_mask);
94 	dm_write_reg(irq_service->ctx, addr, value);
95 }
96 
97 bool dal_irq_service_set(
98 	struct irq_service *irq_service,
99 	enum dc_irq_source source,
100 	bool enable)
101 {
102 	const struct irq_source_info *info =
103 		find_irq_source_info(irq_service, source);
104 
105 	if (!info) {
106 		dm_logger_write(
107 			irq_service->ctx->logger, LOG_ERROR,
108 			"%s: cannot find irq info table entry for %d\n",
109 			__func__,
110 			source);
111 		return false;
112 	}
113 
114 	dal_irq_service_ack(irq_service, source);
115 
116 	if (info->funcs->set)
117 		return info->funcs->set(irq_service, info, enable);
118 
119 	dal_irq_service_set_generic(irq_service, info, enable);
120 
121 	return true;
122 }
123 
124 void dal_irq_service_ack_generic(
125 	struct irq_service *irq_service,
126 	const struct irq_source_info *info)
127 {
128 	uint32_t addr = info->ack_reg;
129 	uint32_t value = dm_read_reg(irq_service->ctx, addr);
130 
131 	value = (value & ~info->ack_mask) |
132 		(info->ack_value & info->ack_mask);
133 	dm_write_reg(irq_service->ctx, addr, value);
134 }
135 
136 bool dal_irq_service_ack(
137 	struct irq_service *irq_service,
138 	enum dc_irq_source source)
139 {
140 	const struct irq_source_info *info =
141 		find_irq_source_info(irq_service, source);
142 
143 	if (!info) {
144 		dm_logger_write(
145 			irq_service->ctx->logger, LOG_ERROR,
146 			"%s: cannot find irq info table entry for %d\n",
147 			__func__,
148 			source);
149 		return false;
150 	}
151 
152 	if (info->funcs->ack)
153 		return info->funcs->ack(irq_service, info);
154 
155 	dal_irq_service_ack_generic(irq_service, info);
156 
157 	return true;
158 }
159 
160 enum dc_irq_source dal_irq_service_to_irq_source(
161 		struct irq_service *irq_service,
162 		uint32_t src_id,
163 		uint32_t ext_id)
164 {
165 	return irq_service->funcs->to_dal_irq_source(
166 		irq_service,
167 		src_id,
168 		ext_id);
169 }
170