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