2 * Copyright 2012-15 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
25 #include "dm_services.h"
26 #include "include/logger_interface.h"
30 #define NUM_ELEMENTS(a) (sizeof(a) / sizeof((a)[0]))
32 static const struct dc_log_type_info log_type_info_tbl
[] = {
34 {LOG_WARNING
, "Warning"},
36 {LOG_DC
, "DC_Interface"},
37 {LOG_SURFACE
, "Surface"},
38 {LOG_HW_HOTPLUG
, "HW_Hotplug"},
39 {LOG_HW_LINK_TRAINING
, "HW_LKTN"},
40 {LOG_HW_SET_MODE
, "HW_Mode"},
41 {LOG_HW_RESUME_S3
, "HW_Resume"},
42 {LOG_HW_AUDIO
, "HW_Audio"},
43 {LOG_HW_HPD_IRQ
, "HW_HPDIRQ"},
45 {LOG_SCALER
, "Scaler"},
47 {LOG_BANDWIDTH_CALCS
, "BWCalcs"},
48 {LOG_BANDWIDTH_VALIDATION
, "BWValidation"},
49 {LOG_I2C_AUX
, "I2C_AUX"},
51 {LOG_BACKLIGHT
, "Backlight"},
52 {LOG_FEATURE_OVERRIDE
, "Override"},
53 {LOG_DETECTION_EDID_PARSER
, "Edid"},
54 {LOG_DETECTION_DP_CAPS
, "DP_Caps"},
55 {LOG_RESOURCE
, "Resource"},
57 {LOG_EVENT_MODE_SET
, "Mode"},
58 {LOG_EVENT_DETECTION
, "Detect"},
59 {LOG_EVENT_LINK_TRAINING
, "LKTN"},
60 {LOG_EVENT_LINK_LOSS
, "LinkLoss"},
61 {LOG_EVENT_UNDERFLOW
, "Underflow"},
62 {LOG_IF_TRACE
, "InterfaceTrace"},
67 /* ----------- Object init and destruction ----------- */
68 static bool construct(struct dc_context
*ctx
, struct dal_logger
*logger
,
71 /* malloc buffer and init offsets */
72 logger
->log_buffer_size
= DAL_LOGGER_BUFFER_MAX_SIZE
;
73 logger
->log_buffer
= (char *)kzalloc(logger
->log_buffer_size
* sizeof(char),
76 if (!logger
->log_buffer
)
79 /* Initialize both offsets to start of buffer (empty) */
80 logger
->buffer_read_offset
= 0;
81 logger
->buffer_write_offset
= 0;
83 logger
->write_wrap_count
= 0;
84 logger
->read_wrap_count
= 0;
85 logger
->open_count
= 0;
87 logger
->flags
.bits
.ENABLE_CONSOLE
= 1;
88 logger
->flags
.bits
.ENABLE_BUFFER
= 0;
92 logger
->mask
= log_mask
;
97 static void destruct(struct dal_logger
*logger
)
99 if (logger
->log_buffer
) {
100 kfree(logger
->log_buffer
);
101 logger
->log_buffer
= NULL
;
105 struct dal_logger
*dal_logger_create(struct dc_context
*ctx
, uint32_t log_mask
)
108 struct dal_logger
*logger
= kzalloc(sizeof(struct dal_logger
),
113 if (!construct(ctx
, logger
, log_mask
)) {
121 uint32_t dal_logger_destroy(struct dal_logger
**logger
)
123 if (logger
== NULL
|| *logger
== NULL
)
132 /* ------------------------------------------------------------------------ */
135 static bool dal_logger_should_log(
136 struct dal_logger
*logger
,
137 enum dc_log_type log_type
)
139 if (logger
->mask
& (1 << log_type
))
145 static void log_to_debug_console(struct log_entry
*entry
)
147 struct dal_logger
*logger
= entry
->logger
;
149 if (logger
->flags
.bits
.ENABLE_CONSOLE
== 0)
152 if (entry
->buf_offset
) {
153 switch (entry
->type
) {
155 dm_error("%s", entry
->buf
);
158 dm_output_to_console("%s", entry
->buf
);
164 /* Print everything unread existing in log_buffer to debug console*/
165 static void flush_to_debug_console(struct dal_logger
*logger
)
167 int i
= logger
->buffer_read_offset
;
168 char *string_start
= &logger
->log_buffer
[i
];
170 dm_output_to_console(
171 "---------------- FLUSHING LOG BUFFER ----------------\n");
172 while (i
< logger
->buffer_write_offset
) {
174 if (logger
->log_buffer
[i
] == '\0') {
175 dm_output_to_console("%s", string_start
);
176 string_start
= (char *)logger
->log_buffer
+ i
+ 1;
180 dm_output_to_console(
181 "-------------- END FLUSHING LOG BUFFER --------------\n\n");
184 static void log_to_internal_buffer(struct log_entry
*entry
)
187 uint32_t size
= entry
->buf_offset
;
188 struct dal_logger
*logger
= entry
->logger
;
190 if (logger
->flags
.bits
.ENABLE_BUFFER
== 0)
193 if (logger
->log_buffer
== NULL
)
196 if (size
> 0 && size
< logger
->log_buffer_size
) {
198 int total_free_space
= 0;
199 int space_before_wrap
= 0;
201 if (logger
->buffer_write_offset
> logger
->buffer_read_offset
) {
202 total_free_space
= logger
->log_buffer_size
-
203 logger
->buffer_write_offset
+
204 logger
->buffer_read_offset
;
205 space_before_wrap
= logger
->log_buffer_size
-
206 logger
->buffer_write_offset
;
207 } else if (logger
->buffer_write_offset
<
208 logger
->buffer_read_offset
) {
209 total_free_space
= logger
->log_buffer_size
-
210 logger
->buffer_read_offset
+
211 logger
->buffer_write_offset
;
212 space_before_wrap
= total_free_space
;
213 } else if (logger
->write_wrap_count
!=
214 logger
->read_wrap_count
) {
215 /* Buffer is completely full already */
216 total_free_space
= 0;
217 space_before_wrap
= 0;
219 /* Buffer is empty, start writing at beginning */
220 total_free_space
= logger
->log_buffer_size
;
221 space_before_wrap
= logger
->log_buffer_size
;
222 logger
->buffer_write_offset
= 0;
223 logger
->buffer_read_offset
= 0;
226 if (space_before_wrap
> size
) {
227 /* No wrap around, copy 'size' bytes
228 * from 'entry->buf' to 'log_buffer'
230 memmove(logger
->log_buffer
+
231 logger
->buffer_write_offset
,
233 logger
->buffer_write_offset
+= size
;
235 } else if (total_free_space
> size
) {
236 /* We have enough room without flushing,
237 * but need to wrap around */
239 int space_after_wrap
= total_free_space
-
242 memmove(logger
->log_buffer
+
243 logger
->buffer_write_offset
,
244 entry
->buf
, space_before_wrap
);
245 memmove(logger
->log_buffer
, entry
->buf
+
246 space_before_wrap
, space_after_wrap
);
248 logger
->buffer_write_offset
= space_after_wrap
;
249 logger
->write_wrap_count
++;
252 /* Not enough room remaining, we should flush
255 /* Flush existing unread logs to console */
256 flush_to_debug_console(logger
);
258 /* Start writing to beginning of buffer */
259 memmove(logger
->log_buffer
, entry
->buf
, size
);
260 logger
->buffer_write_offset
= size
;
261 logger
->buffer_read_offset
= 0;
267 static void log_heading(struct log_entry
*entry
)
271 for (j
= 0; j
< NUM_ELEMENTS(log_type_info_tbl
); j
++) {
273 const struct dc_log_type_info
*info
= &log_type_info_tbl
[j
];
275 if (info
->type
== entry
->type
)
276 dm_logger_append(entry
, "[%s]\t", info
->name
);
280 static void append_entry(
281 struct log_entry
*entry
,
286 entry
->buf_offset
+ buf_size
> entry
->max_buf_bytes
292 /* Todo: check if off by 1 byte due to \0 anywhere */
293 memmove(entry
->buf
+ entry
->buf_offset
, buffer
, buf_size
);
294 entry
->buf_offset
+= buf_size
;
297 /* ------------------------------------------------------------------------ */
299 /* Warning: Be careful that 'msg' is null terminated and the total size is
300 * less than DAL_LOGGER_BUFFER_MAX_LOG_LINE_SIZE (256) including '\0'
302 void dm_logger_write(
303 struct dal_logger
*logger
,
304 enum dc_log_type log_type
,
308 if (logger
&& dal_logger_should_log(logger
, log_type
)) {
311 char buffer
[LOG_MAX_LINE_SIZE
];
312 struct log_entry entry
;
316 entry
.logger
= logger
;
320 entry
.buf_offset
= 0;
321 entry
.max_buf_bytes
= DAL_LOGGER_BUFFER_MAX_SIZE
* sizeof(char);
323 entry
.type
= log_type
;
327 size
= dm_log_to_buffer(
328 buffer
, LOG_MAX_LINE_SIZE
, msg
, args
);
330 entry
.buf_offset
+= size
;
332 /* --Flush log_entry buffer-- */
333 /* print to kernel console */
334 log_to_debug_console(&entry
);
335 /* log internally for dsat */
336 log_to_internal_buffer(&entry
);
342 /* Same as dm_logger_write, except without open() and close(), which must
343 * be done separately.
345 void dm_logger_append(
346 struct log_entry
*entry
,
350 struct dal_logger
*logger
;
357 logger
= entry
->logger
;
359 if (logger
&& logger
->open_count
> 0 &&
360 dal_logger_should_log(logger
, entry
->type
)) {
364 char buffer
[LOG_MAX_LINE_SIZE
];
368 size
= dm_log_to_buffer(
369 buffer
, LOG_MAX_LINE_SIZE
, msg
, args
);
371 if (size
< LOG_MAX_LINE_SIZE
- 1) {
372 append_entry(entry
, buffer
, size
);
374 append_entry(entry
, "LOG_ERROR, line too long\n", 27);
382 struct dal_logger
*logger
,
383 struct log_entry
*entry
, /* out */
384 enum dc_log_type log_type
)
391 entry
->type
= log_type
;
392 entry
->logger
= logger
;
394 entry
->buf
= kzalloc(DAL_LOGGER_BUFFER_MAX_SIZE
* sizeof(char),
397 entry
->buf_offset
= 0;
398 entry
->max_buf_bytes
= DAL_LOGGER_BUFFER_MAX_SIZE
* sizeof(char);
400 logger
->open_count
++;
405 void dm_logger_close(struct log_entry
*entry
)
407 struct dal_logger
*logger
= entry
->logger
;
409 if (logger
&& logger
->open_count
> 0) {
410 logger
->open_count
--;
416 /* --Flush log_entry buffer-- */
417 /* print to kernel console */
418 log_to_debug_console(entry
);
419 /* log internally for dsat */
420 log_to_internal_buffer(entry
);
422 /* TODO: Write end heading */
428 entry
->buf_offset
= 0;
429 entry
->max_buf_bytes
= 0;