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