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