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