1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2014-2019 Intel Corporation
4  */
5 
6 #include <linux/debugfs.h>
7 #include <linux/string_helpers.h>
8 
9 #include "gt/intel_gt.h"
10 #include "i915_drv.h"
11 #include "i915_irq.h"
12 #include "i915_memcpy.h"
13 #include "intel_guc_capture.h"
14 #include "intel_guc_log.h"
15 
16 #if defined(CONFIG_DRM_I915_DEBUG_GUC)
17 #define GUC_LOG_DEFAULT_CRASH_BUFFER_SIZE	SZ_2M
18 #define GUC_LOG_DEFAULT_DEBUG_BUFFER_SIZE	SZ_16M
19 #define GUC_LOG_DEFAULT_CAPTURE_BUFFER_SIZE	SZ_4M
20 #elif defined(CONFIG_DRM_I915_DEBUG_GEM)
21 #define GUC_LOG_DEFAULT_CRASH_BUFFER_SIZE	SZ_1M
22 #define GUC_LOG_DEFAULT_DEBUG_BUFFER_SIZE	SZ_2M
23 #define GUC_LOG_DEFAULT_CAPTURE_BUFFER_SIZE	SZ_4M
24 #else
25 #define GUC_LOG_DEFAULT_CRASH_BUFFER_SIZE	SZ_8K
26 #define GUC_LOG_DEFAULT_DEBUG_BUFFER_SIZE	SZ_64K
27 #define GUC_LOG_DEFAULT_CAPTURE_BUFFER_SIZE	SZ_2M
28 #endif
29 
30 static void guc_log_copy_debuglogs_for_relay(struct intel_guc_log *log);
31 
32 struct guc_log_section {
33 	u32 max;
34 	u32 flag;
35 	u32 default_val;
36 	const char *name;
37 };
38 
39 static void _guc_log_init_sizes(struct intel_guc_log *log)
40 {
41 	struct intel_guc *guc = log_to_guc(log);
42 	struct drm_i915_private *i915 = guc_to_gt(guc)->i915;
43 	static const struct guc_log_section sections[GUC_LOG_SECTIONS_LIMIT] = {
44 		{
45 			GUC_LOG_CRASH_MASK >> GUC_LOG_CRASH_SHIFT,
46 			GUC_LOG_LOG_ALLOC_UNITS,
47 			GUC_LOG_DEFAULT_CRASH_BUFFER_SIZE,
48 			"crash dump"
49 		},
50 		{
51 			GUC_LOG_DEBUG_MASK >> GUC_LOG_DEBUG_SHIFT,
52 			GUC_LOG_LOG_ALLOC_UNITS,
53 			GUC_LOG_DEFAULT_DEBUG_BUFFER_SIZE,
54 			"debug",
55 		},
56 		{
57 			GUC_LOG_CAPTURE_MASK >> GUC_LOG_CAPTURE_SHIFT,
58 			GUC_LOG_CAPTURE_ALLOC_UNITS,
59 			GUC_LOG_DEFAULT_CAPTURE_BUFFER_SIZE,
60 			"capture",
61 		}
62 	};
63 	int i;
64 
65 	for (i = 0; i < GUC_LOG_SECTIONS_LIMIT; i++)
66 		log->sizes[i].bytes = sections[i].default_val;
67 
68 	/* If debug size > 1MB then bump default crash size to keep the same units */
69 	if (log->sizes[GUC_LOG_SECTIONS_DEBUG].bytes >= SZ_1M &&
70 	    GUC_LOG_DEFAULT_CRASH_BUFFER_SIZE < SZ_1M)
71 		log->sizes[GUC_LOG_SECTIONS_CRASH].bytes = SZ_1M;
72 
73 	/* Prepare the GuC API structure fields: */
74 	for (i = 0; i < GUC_LOG_SECTIONS_LIMIT; i++) {
75 		/* Convert to correct units */
76 		if ((log->sizes[i].bytes % SZ_1M) == 0) {
77 			log->sizes[i].units = SZ_1M;
78 			log->sizes[i].flag = sections[i].flag;
79 		} else {
80 			log->sizes[i].units = SZ_4K;
81 			log->sizes[i].flag = 0;
82 		}
83 
84 		if (!IS_ALIGNED(log->sizes[i].bytes, log->sizes[i].units))
85 			drm_err(&i915->drm, "Mis-aligned GuC log %s size: 0x%X vs 0x%X!",
86 				sections[i].name, log->sizes[i].bytes, log->sizes[i].units);
87 		log->sizes[i].count = log->sizes[i].bytes / log->sizes[i].units;
88 
89 		if (!log->sizes[i].count) {
90 			drm_err(&i915->drm, "Zero GuC log %s size!", sections[i].name);
91 		} else {
92 			/* Size is +1 unit */
93 			log->sizes[i].count--;
94 		}
95 
96 		/* Clip to field size */
97 		if (log->sizes[i].count > sections[i].max) {
98 			drm_err(&i915->drm, "GuC log %s size too large: %d vs %d!",
99 				sections[i].name, log->sizes[i].count + 1, sections[i].max + 1);
100 			log->sizes[i].count = sections[i].max;
101 		}
102 	}
103 
104 	if (log->sizes[GUC_LOG_SECTIONS_CRASH].units != log->sizes[GUC_LOG_SECTIONS_DEBUG].units) {
105 		drm_err(&i915->drm, "Unit mis-match for GuC log crash and debug sections: %d vs %d!",
106 			log->sizes[GUC_LOG_SECTIONS_CRASH].units,
107 			log->sizes[GUC_LOG_SECTIONS_DEBUG].units);
108 		log->sizes[GUC_LOG_SECTIONS_CRASH].units = log->sizes[GUC_LOG_SECTIONS_DEBUG].units;
109 		log->sizes[GUC_LOG_SECTIONS_CRASH].count = 0;
110 	}
111 
112 	log->sizes_initialised = true;
113 }
114 
115 static void guc_log_init_sizes(struct intel_guc_log *log)
116 {
117 	if (log->sizes_initialised)
118 		return;
119 
120 	_guc_log_init_sizes(log);
121 }
122 
123 static u32 intel_guc_log_section_size_crash(struct intel_guc_log *log)
124 {
125 	guc_log_init_sizes(log);
126 
127 	return log->sizes[GUC_LOG_SECTIONS_CRASH].bytes;
128 }
129 
130 static u32 intel_guc_log_section_size_debug(struct intel_guc_log *log)
131 {
132 	guc_log_init_sizes(log);
133 
134 	return log->sizes[GUC_LOG_SECTIONS_DEBUG].bytes;
135 }
136 
137 u32 intel_guc_log_section_size_capture(struct intel_guc_log *log)
138 {
139 	guc_log_init_sizes(log);
140 
141 	return log->sizes[GUC_LOG_SECTIONS_CAPTURE].bytes;
142 }
143 
144 static u32 intel_guc_log_size(struct intel_guc_log *log)
145 {
146 	/*
147 	 *  GuC Log buffer Layout:
148 	 *
149 	 *  NB: Ordering must follow "enum guc_log_buffer_type".
150 	 *
151 	 *  +===============================+ 00B
152 	 *  |      Debug state header       |
153 	 *  +-------------------------------+ 32B
154 	 *  |    Crash dump state header    |
155 	 *  +-------------------------------+ 64B
156 	 *  |     Capture state header      |
157 	 *  +-------------------------------+ 96B
158 	 *  |                               |
159 	 *  +===============================+ PAGE_SIZE (4KB)
160 	 *  |          Debug logs           |
161 	 *  +===============================+ + DEBUG_SIZE
162 	 *  |        Crash Dump logs        |
163 	 *  +===============================+ + CRASH_SIZE
164 	 *  |         Capture logs          |
165 	 *  +===============================+ + CAPTURE_SIZE
166 	 */
167 	return PAGE_SIZE +
168 		intel_guc_log_section_size_crash(log) +
169 		intel_guc_log_section_size_debug(log) +
170 		intel_guc_log_section_size_capture(log);
171 }
172 
173 /**
174  * DOC: GuC firmware log
175  *
176  * Firmware log is enabled by setting i915.guc_log_level to the positive level.
177  * Log data is printed out via reading debugfs i915_guc_log_dump. Reading from
178  * i915_guc_load_status will print out firmware loading status and scratch
179  * registers value.
180  */
181 
182 static int guc_action_flush_log_complete(struct intel_guc *guc)
183 {
184 	u32 action[] = {
185 		INTEL_GUC_ACTION_LOG_BUFFER_FILE_FLUSH_COMPLETE,
186 		GUC_DEBUG_LOG_BUFFER
187 	};
188 
189 	return intel_guc_send_nb(guc, action, ARRAY_SIZE(action), 0);
190 }
191 
192 static int guc_action_flush_log(struct intel_guc *guc)
193 {
194 	u32 action[] = {
195 		INTEL_GUC_ACTION_FORCE_LOG_BUFFER_FLUSH,
196 		0
197 	};
198 
199 	return intel_guc_send(guc, action, ARRAY_SIZE(action));
200 }
201 
202 static int guc_action_control_log(struct intel_guc *guc, bool enable,
203 				  bool default_logging, u32 verbosity)
204 {
205 	u32 action[] = {
206 		INTEL_GUC_ACTION_UK_LOG_ENABLE_LOGGING,
207 		(enable ? GUC_LOG_CONTROL_LOGGING_ENABLED : 0) |
208 		(verbosity << GUC_LOG_CONTROL_VERBOSITY_SHIFT) |
209 		(default_logging ? GUC_LOG_CONTROL_DEFAULT_LOGGING : 0)
210 	};
211 
212 	GEM_BUG_ON(verbosity > GUC_LOG_VERBOSITY_MAX);
213 
214 	return intel_guc_send(guc, action, ARRAY_SIZE(action));
215 }
216 
217 /*
218  * Sub buffer switch callback. Called whenever relay has to switch to a new
219  * sub buffer, relay stays on the same sub buffer if 0 is returned.
220  */
221 static int subbuf_start_callback(struct rchan_buf *buf,
222 				 void *subbuf,
223 				 void *prev_subbuf,
224 				 size_t prev_padding)
225 {
226 	/*
227 	 * Use no-overwrite mode by default, where relay will stop accepting
228 	 * new data if there are no empty sub buffers left.
229 	 * There is no strict synchronization enforced by relay between Consumer
230 	 * and Producer. In overwrite mode, there is a possibility of getting
231 	 * inconsistent/garbled data, the producer could be writing on to the
232 	 * same sub buffer from which Consumer is reading. This can't be avoided
233 	 * unless Consumer is fast enough and can always run in tandem with
234 	 * Producer.
235 	 */
236 	if (relay_buf_full(buf))
237 		return 0;
238 
239 	return 1;
240 }
241 
242 /*
243  * file_create() callback. Creates relay file in debugfs.
244  */
245 static struct dentry *create_buf_file_callback(const char *filename,
246 					       struct dentry *parent,
247 					       umode_t mode,
248 					       struct rchan_buf *buf,
249 					       int *is_global)
250 {
251 	struct dentry *buf_file;
252 
253 	/*
254 	 * This to enable the use of a single buffer for the relay channel and
255 	 * correspondingly have a single file exposed to User, through which
256 	 * it can collect the logs in order without any post-processing.
257 	 * Need to set 'is_global' even if parent is NULL for early logging.
258 	 */
259 	*is_global = 1;
260 
261 	if (!parent)
262 		return NULL;
263 
264 	buf_file = debugfs_create_file(filename, mode,
265 				       parent, buf, &relay_file_operations);
266 	if (IS_ERR(buf_file))
267 		return NULL;
268 
269 	return buf_file;
270 }
271 
272 /*
273  * file_remove() default callback. Removes relay file in debugfs.
274  */
275 static int remove_buf_file_callback(struct dentry *dentry)
276 {
277 	debugfs_remove(dentry);
278 	return 0;
279 }
280 
281 /* relay channel callbacks */
282 static const struct rchan_callbacks relay_callbacks = {
283 	.subbuf_start = subbuf_start_callback,
284 	.create_buf_file = create_buf_file_callback,
285 	.remove_buf_file = remove_buf_file_callback,
286 };
287 
288 static void guc_move_to_next_buf(struct intel_guc_log *log)
289 {
290 	/*
291 	 * Make sure the updates made in the sub buffer are visible when
292 	 * Consumer sees the following update to offset inside the sub buffer.
293 	 */
294 	smp_wmb();
295 
296 	/* All data has been written, so now move the offset of sub buffer. */
297 	relay_reserve(log->relay.channel, log->vma->obj->base.size -
298 					  intel_guc_log_section_size_capture(log));
299 
300 	/* Switch to the next sub buffer */
301 	relay_flush(log->relay.channel);
302 }
303 
304 static void *guc_get_write_buffer(struct intel_guc_log *log)
305 {
306 	/*
307 	 * Just get the base address of a new sub buffer and copy data into it
308 	 * ourselves. NULL will be returned in no-overwrite mode, if all sub
309 	 * buffers are full. Could have used the relay_write() to indirectly
310 	 * copy the data, but that would have been bit convoluted, as we need to
311 	 * write to only certain locations inside a sub buffer which cannot be
312 	 * done without using relay_reserve() along with relay_write(). So its
313 	 * better to use relay_reserve() alone.
314 	 */
315 	return relay_reserve(log->relay.channel, 0);
316 }
317 
318 bool intel_guc_check_log_buf_overflow(struct intel_guc_log *log,
319 				      enum guc_log_buffer_type type,
320 				      unsigned int full_cnt)
321 {
322 	unsigned int prev_full_cnt = log->stats[type].sampled_overflow;
323 	bool overflow = false;
324 
325 	if (full_cnt != prev_full_cnt) {
326 		overflow = true;
327 
328 		log->stats[type].overflow = full_cnt;
329 		log->stats[type].sampled_overflow += full_cnt - prev_full_cnt;
330 
331 		if (full_cnt < prev_full_cnt) {
332 			/* buffer_full_cnt is a 4 bit counter */
333 			log->stats[type].sampled_overflow += 16;
334 		}
335 
336 		dev_notice_ratelimited(guc_to_gt(log_to_guc(log))->i915->drm.dev,
337 				       "GuC log buffer overflow\n");
338 	}
339 
340 	return overflow;
341 }
342 
343 unsigned int intel_guc_get_log_buffer_size(struct intel_guc_log *log,
344 					   enum guc_log_buffer_type type)
345 {
346 	switch (type) {
347 	case GUC_DEBUG_LOG_BUFFER:
348 		return intel_guc_log_section_size_debug(log);
349 	case GUC_CRASH_DUMP_LOG_BUFFER:
350 		return intel_guc_log_section_size_crash(log);
351 	case GUC_CAPTURE_LOG_BUFFER:
352 		return intel_guc_log_section_size_capture(log);
353 	default:
354 		MISSING_CASE(type);
355 	}
356 
357 	return 0;
358 }
359 
360 size_t intel_guc_get_log_buffer_offset(struct intel_guc_log *log,
361 				       enum guc_log_buffer_type type)
362 {
363 	enum guc_log_buffer_type i;
364 	size_t offset = PAGE_SIZE;/* for the log_buffer_states */
365 
366 	for (i = GUC_DEBUG_LOG_BUFFER; i < GUC_MAX_LOG_BUFFER; ++i) {
367 		if (i == type)
368 			break;
369 		offset += intel_guc_get_log_buffer_size(log, i);
370 	}
371 
372 	return offset;
373 }
374 
375 static void _guc_log_copy_debuglogs_for_relay(struct intel_guc_log *log)
376 {
377 	unsigned int buffer_size, read_offset, write_offset, bytes_to_copy, full_cnt;
378 	struct guc_log_buffer_state *log_buf_state, *log_buf_snapshot_state;
379 	struct guc_log_buffer_state log_buf_state_local;
380 	enum guc_log_buffer_type type;
381 	void *src_data, *dst_data;
382 	bool new_overflow;
383 
384 	mutex_lock(&log->relay.lock);
385 
386 	if (WARN_ON(!intel_guc_log_relay_created(log)))
387 		goto out_unlock;
388 
389 	/* Get the pointer to shared GuC log buffer */
390 	src_data = log->buf_addr;
391 	log_buf_state = src_data;
392 
393 	/* Get the pointer to local buffer to store the logs */
394 	log_buf_snapshot_state = dst_data = guc_get_write_buffer(log);
395 
396 	if (unlikely(!log_buf_snapshot_state)) {
397 		/*
398 		 * Used rate limited to avoid deluge of messages, logs might be
399 		 * getting consumed by User at a slow rate.
400 		 */
401 		DRM_ERROR_RATELIMITED("no sub-buffer to copy general logs\n");
402 		log->relay.full_count++;
403 
404 		goto out_unlock;
405 	}
406 
407 	/* Actual logs are present from the 2nd page */
408 	src_data += PAGE_SIZE;
409 	dst_data += PAGE_SIZE;
410 
411 	/* For relay logging, we exclude error state capture */
412 	for (type = GUC_DEBUG_LOG_BUFFER; type <= GUC_CRASH_DUMP_LOG_BUFFER; type++) {
413 		/*
414 		 * Make a copy of the state structure, inside GuC log buffer
415 		 * (which is uncached mapped), on the stack to avoid reading
416 		 * from it multiple times.
417 		 */
418 		memcpy(&log_buf_state_local, log_buf_state,
419 		       sizeof(struct guc_log_buffer_state));
420 		buffer_size = intel_guc_get_log_buffer_size(log, type);
421 		read_offset = log_buf_state_local.read_ptr;
422 		write_offset = log_buf_state_local.sampled_write_ptr;
423 		full_cnt = log_buf_state_local.buffer_full_cnt;
424 
425 		/* Bookkeeping stuff */
426 		log->stats[type].flush += log_buf_state_local.flush_to_file;
427 		new_overflow = intel_guc_check_log_buf_overflow(log, type, full_cnt);
428 
429 		/* Update the state of shared log buffer */
430 		log_buf_state->read_ptr = write_offset;
431 		log_buf_state->flush_to_file = 0;
432 		log_buf_state++;
433 
434 		/* First copy the state structure in snapshot buffer */
435 		memcpy(log_buf_snapshot_state, &log_buf_state_local,
436 		       sizeof(struct guc_log_buffer_state));
437 
438 		/*
439 		 * The write pointer could have been updated by GuC firmware,
440 		 * after sending the flush interrupt to Host, for consistency
441 		 * set write pointer value to same value of sampled_write_ptr
442 		 * in the snapshot buffer.
443 		 */
444 		log_buf_snapshot_state->write_ptr = write_offset;
445 		log_buf_snapshot_state++;
446 
447 		/* Now copy the actual logs. */
448 		if (unlikely(new_overflow)) {
449 			/* copy the whole buffer in case of overflow */
450 			read_offset = 0;
451 			write_offset = buffer_size;
452 		} else if (unlikely((read_offset > buffer_size) ||
453 				    (write_offset > buffer_size))) {
454 			DRM_ERROR("invalid log buffer state\n");
455 			/* copy whole buffer as offsets are unreliable */
456 			read_offset = 0;
457 			write_offset = buffer_size;
458 		}
459 
460 		/* Just copy the newly written data */
461 		if (read_offset > write_offset) {
462 			i915_memcpy_from_wc(dst_data, src_data, write_offset);
463 			bytes_to_copy = buffer_size - read_offset;
464 		} else {
465 			bytes_to_copy = write_offset - read_offset;
466 		}
467 		i915_memcpy_from_wc(dst_data + read_offset,
468 				    src_data + read_offset, bytes_to_copy);
469 
470 		src_data += buffer_size;
471 		dst_data += buffer_size;
472 	}
473 
474 	guc_move_to_next_buf(log);
475 
476 out_unlock:
477 	mutex_unlock(&log->relay.lock);
478 }
479 
480 static void copy_debug_logs_work(struct work_struct *work)
481 {
482 	struct intel_guc_log *log =
483 		container_of(work, struct intel_guc_log, relay.flush_work);
484 
485 	guc_log_copy_debuglogs_for_relay(log);
486 }
487 
488 static int guc_log_relay_map(struct intel_guc_log *log)
489 {
490 	lockdep_assert_held(&log->relay.lock);
491 
492 	if (!log->vma || !log->buf_addr)
493 		return -ENODEV;
494 
495 	/*
496 	 * WC vmalloc mapping of log buffer pages was done at
497 	 * GuC Log Init time, but lets keep a ref for book-keeping
498 	 */
499 	i915_gem_object_get(log->vma->obj);
500 	log->relay.buf_in_use = true;
501 
502 	return 0;
503 }
504 
505 static void guc_log_relay_unmap(struct intel_guc_log *log)
506 {
507 	lockdep_assert_held(&log->relay.lock);
508 
509 	i915_gem_object_put(log->vma->obj);
510 	log->relay.buf_in_use = false;
511 }
512 
513 void intel_guc_log_init_early(struct intel_guc_log *log)
514 {
515 	mutex_init(&log->relay.lock);
516 	INIT_WORK(&log->relay.flush_work, copy_debug_logs_work);
517 	log->relay.started = false;
518 }
519 
520 static int guc_log_relay_create(struct intel_guc_log *log)
521 {
522 	struct intel_guc *guc = log_to_guc(log);
523 	struct drm_i915_private *dev_priv = guc_to_gt(guc)->i915;
524 	struct rchan *guc_log_relay_chan;
525 	size_t n_subbufs, subbuf_size;
526 	int ret;
527 
528 	lockdep_assert_held(&log->relay.lock);
529 	GEM_BUG_ON(!log->vma);
530 
531 	 /*
532 	  * Keep the size of sub buffers same as shared log buffer
533 	  * but GuC log-events excludes the error-state-capture logs
534 	  */
535 	subbuf_size = log->vma->size - intel_guc_log_section_size_capture(log);
536 
537 	/*
538 	 * Store up to 8 snapshots, which is large enough to buffer sufficient
539 	 * boot time logs and provides enough leeway to User, in terms of
540 	 * latency, for consuming the logs from relay. Also doesn't take
541 	 * up too much memory.
542 	 */
543 	n_subbufs = 8;
544 
545 	guc_log_relay_chan = relay_open("guc_log",
546 					dev_priv->drm.primary->debugfs_root,
547 					subbuf_size, n_subbufs,
548 					&relay_callbacks, dev_priv);
549 	if (!guc_log_relay_chan) {
550 		DRM_ERROR("Couldn't create relay chan for GuC logging\n");
551 
552 		ret = -ENOMEM;
553 		return ret;
554 	}
555 
556 	GEM_BUG_ON(guc_log_relay_chan->subbuf_size < subbuf_size);
557 	log->relay.channel = guc_log_relay_chan;
558 
559 	return 0;
560 }
561 
562 static void guc_log_relay_destroy(struct intel_guc_log *log)
563 {
564 	lockdep_assert_held(&log->relay.lock);
565 
566 	relay_close(log->relay.channel);
567 	log->relay.channel = NULL;
568 }
569 
570 static void guc_log_copy_debuglogs_for_relay(struct intel_guc_log *log)
571 {
572 	struct intel_guc *guc = log_to_guc(log);
573 	struct drm_i915_private *dev_priv = guc_to_gt(guc)->i915;
574 	intel_wakeref_t wakeref;
575 
576 	_guc_log_copy_debuglogs_for_relay(log);
577 
578 	/*
579 	 * Generally device is expected to be active only at this
580 	 * time, so get/put should be really quick.
581 	 */
582 	with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref)
583 		guc_action_flush_log_complete(guc);
584 }
585 
586 static u32 __get_default_log_level(struct intel_guc_log *log)
587 {
588 	struct intel_guc *guc = log_to_guc(log);
589 	struct drm_i915_private *i915 = guc_to_gt(guc)->i915;
590 
591 	/* A negative value means "use platform/config default" */
592 	if (i915->params.guc_log_level < 0) {
593 		return (IS_ENABLED(CONFIG_DRM_I915_DEBUG) ||
594 			IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) ?
595 			GUC_LOG_LEVEL_MAX : GUC_LOG_LEVEL_NON_VERBOSE;
596 	}
597 
598 	if (i915->params.guc_log_level > GUC_LOG_LEVEL_MAX) {
599 		DRM_WARN("Incompatible option detected: %s=%d, %s!\n",
600 			 "guc_log_level", i915->params.guc_log_level,
601 			 "verbosity too high");
602 		return (IS_ENABLED(CONFIG_DRM_I915_DEBUG) ||
603 			IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM)) ?
604 			GUC_LOG_LEVEL_MAX : GUC_LOG_LEVEL_DISABLED;
605 	}
606 
607 	GEM_BUG_ON(i915->params.guc_log_level < GUC_LOG_LEVEL_DISABLED);
608 	GEM_BUG_ON(i915->params.guc_log_level > GUC_LOG_LEVEL_MAX);
609 	return i915->params.guc_log_level;
610 }
611 
612 int intel_guc_log_create(struct intel_guc_log *log)
613 {
614 	struct intel_guc *guc = log_to_guc(log);
615 	struct i915_vma *vma;
616 	void *vaddr;
617 	u32 guc_log_size;
618 	int ret;
619 
620 	GEM_BUG_ON(log->vma);
621 
622 	guc_log_size = intel_guc_log_size(log);
623 
624 	vma = intel_guc_allocate_vma(guc, guc_log_size);
625 	if (IS_ERR(vma)) {
626 		ret = PTR_ERR(vma);
627 		goto err;
628 	}
629 
630 	log->vma = vma;
631 	/*
632 	 * Create a WC (Uncached for read) vmalloc mapping up front immediate access to
633 	 * data from memory during  critical events such as error capture
634 	 */
635 	vaddr = i915_gem_object_pin_map_unlocked(log->vma->obj, I915_MAP_WC);
636 	if (IS_ERR(vaddr)) {
637 		ret = PTR_ERR(vaddr);
638 		i915_vma_unpin_and_release(&log->vma, 0);
639 		goto err;
640 	}
641 	log->buf_addr = vaddr;
642 
643 	log->level = __get_default_log_level(log);
644 	DRM_DEBUG_DRIVER("guc_log_level=%d (%s, verbose:%s, verbosity:%d)\n",
645 			 log->level, str_enabled_disabled(log->level),
646 			 str_yes_no(GUC_LOG_LEVEL_IS_VERBOSE(log->level)),
647 			 GUC_LOG_LEVEL_TO_VERBOSITY(log->level));
648 
649 	return 0;
650 
651 err:
652 	DRM_ERROR("Failed to allocate or map GuC log buffer. %d\n", ret);
653 	return ret;
654 }
655 
656 void intel_guc_log_destroy(struct intel_guc_log *log)
657 {
658 	log->buf_addr = NULL;
659 	i915_vma_unpin_and_release(&log->vma, I915_VMA_RELEASE_MAP);
660 }
661 
662 int intel_guc_log_set_level(struct intel_guc_log *log, u32 level)
663 {
664 	struct intel_guc *guc = log_to_guc(log);
665 	struct drm_i915_private *dev_priv = guc_to_gt(guc)->i915;
666 	intel_wakeref_t wakeref;
667 	int ret = 0;
668 
669 	BUILD_BUG_ON(GUC_LOG_VERBOSITY_MIN != 0);
670 	GEM_BUG_ON(!log->vma);
671 
672 	/*
673 	 * GuC is recognizing log levels starting from 0 to max, we're using 0
674 	 * as indication that logging should be disabled.
675 	 */
676 	if (level < GUC_LOG_LEVEL_DISABLED || level > GUC_LOG_LEVEL_MAX)
677 		return -EINVAL;
678 
679 	mutex_lock(&dev_priv->drm.struct_mutex);
680 
681 	if (log->level == level)
682 		goto out_unlock;
683 
684 	with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref)
685 		ret = guc_action_control_log(guc,
686 					     GUC_LOG_LEVEL_IS_VERBOSE(level),
687 					     GUC_LOG_LEVEL_IS_ENABLED(level),
688 					     GUC_LOG_LEVEL_TO_VERBOSITY(level));
689 	if (ret) {
690 		DRM_DEBUG_DRIVER("guc_log_control action failed %d\n", ret);
691 		goto out_unlock;
692 	}
693 
694 	log->level = level;
695 
696 out_unlock:
697 	mutex_unlock(&dev_priv->drm.struct_mutex);
698 
699 	return ret;
700 }
701 
702 bool intel_guc_log_relay_created(const struct intel_guc_log *log)
703 {
704 	return log->buf_addr;
705 }
706 
707 int intel_guc_log_relay_open(struct intel_guc_log *log)
708 {
709 	int ret;
710 
711 	if (!log->vma)
712 		return -ENODEV;
713 
714 	mutex_lock(&log->relay.lock);
715 
716 	if (intel_guc_log_relay_created(log)) {
717 		ret = -EEXIST;
718 		goto out_unlock;
719 	}
720 
721 	/*
722 	 * We require SSE 4.1 for fast reads from the GuC log buffer and
723 	 * it should be present on the chipsets supporting GuC based
724 	 * submissions.
725 	 */
726 	if (!i915_has_memcpy_from_wc()) {
727 		ret = -ENXIO;
728 		goto out_unlock;
729 	}
730 
731 	ret = guc_log_relay_create(log);
732 	if (ret)
733 		goto out_unlock;
734 
735 	ret = guc_log_relay_map(log);
736 	if (ret)
737 		goto out_relay;
738 
739 	mutex_unlock(&log->relay.lock);
740 
741 	return 0;
742 
743 out_relay:
744 	guc_log_relay_destroy(log);
745 out_unlock:
746 	mutex_unlock(&log->relay.lock);
747 
748 	return ret;
749 }
750 
751 int intel_guc_log_relay_start(struct intel_guc_log *log)
752 {
753 	if (log->relay.started)
754 		return -EEXIST;
755 
756 	/*
757 	 * When GuC is logging without us relaying to userspace, we're ignoring
758 	 * the flush notification. This means that we need to unconditionally
759 	 * flush on relay enabling, since GuC only notifies us once.
760 	 */
761 	queue_work(system_highpri_wq, &log->relay.flush_work);
762 
763 	log->relay.started = true;
764 
765 	return 0;
766 }
767 
768 void intel_guc_log_relay_flush(struct intel_guc_log *log)
769 {
770 	struct intel_guc *guc = log_to_guc(log);
771 	intel_wakeref_t wakeref;
772 
773 	if (!log->relay.started)
774 		return;
775 
776 	/*
777 	 * Before initiating the forceful flush, wait for any pending/ongoing
778 	 * flush to complete otherwise forceful flush may not actually happen.
779 	 */
780 	flush_work(&log->relay.flush_work);
781 
782 	with_intel_runtime_pm(guc_to_gt(guc)->uncore->rpm, wakeref)
783 		guc_action_flush_log(guc);
784 
785 	/* GuC would have updated log buffer by now, so copy it */
786 	guc_log_copy_debuglogs_for_relay(log);
787 }
788 
789 /*
790  * Stops the relay log. Called from intel_guc_log_relay_close(), so no
791  * possibility of race with start/flush since relay_write cannot race
792  * relay_close.
793  */
794 static void guc_log_relay_stop(struct intel_guc_log *log)
795 {
796 	struct intel_guc *guc = log_to_guc(log);
797 	struct drm_i915_private *i915 = guc_to_gt(guc)->i915;
798 
799 	if (!log->relay.started)
800 		return;
801 
802 	intel_synchronize_irq(i915);
803 
804 	flush_work(&log->relay.flush_work);
805 
806 	log->relay.started = false;
807 }
808 
809 void intel_guc_log_relay_close(struct intel_guc_log *log)
810 {
811 	guc_log_relay_stop(log);
812 
813 	mutex_lock(&log->relay.lock);
814 	GEM_BUG_ON(!intel_guc_log_relay_created(log));
815 	guc_log_relay_unmap(log);
816 	guc_log_relay_destroy(log);
817 	mutex_unlock(&log->relay.lock);
818 }
819 
820 void intel_guc_log_handle_flush_event(struct intel_guc_log *log)
821 {
822 	if (log->relay.started)
823 		queue_work(system_highpri_wq, &log->relay.flush_work);
824 }
825 
826 static const char *
827 stringify_guc_log_type(enum guc_log_buffer_type type)
828 {
829 	switch (type) {
830 	case GUC_DEBUG_LOG_BUFFER:
831 		return "DEBUG";
832 	case GUC_CRASH_DUMP_LOG_BUFFER:
833 		return "CRASH";
834 	case GUC_CAPTURE_LOG_BUFFER:
835 		return "CAPTURE";
836 	default:
837 		MISSING_CASE(type);
838 	}
839 
840 	return "";
841 }
842 
843 /**
844  * intel_guc_log_info - dump information about GuC log relay
845  * @log: the GuC log
846  * @p: the &drm_printer
847  *
848  * Pretty printer for GuC log info
849  */
850 void intel_guc_log_info(struct intel_guc_log *log, struct drm_printer *p)
851 {
852 	enum guc_log_buffer_type type;
853 
854 	if (!intel_guc_log_relay_created(log)) {
855 		drm_puts(p, "GuC log relay not created\n");
856 		return;
857 	}
858 
859 	drm_puts(p, "GuC logging stats:\n");
860 
861 	drm_printf(p, "\tRelay full count: %u\n", log->relay.full_count);
862 
863 	for (type = GUC_DEBUG_LOG_BUFFER; type < GUC_MAX_LOG_BUFFER; type++) {
864 		drm_printf(p, "\t%s:\tflush count %10u, overflow count %10u\n",
865 			   stringify_guc_log_type(type),
866 			   log->stats[type].flush,
867 			   log->stats[type].sampled_overflow);
868 	}
869 }
870 
871 /**
872  * intel_guc_log_dump - dump the contents of the GuC log
873  * @log: the GuC log
874  * @p: the &drm_printer
875  * @dump_load_err: dump the log saved on GuC load error
876  *
877  * Pretty printer for the GuC log
878  */
879 int intel_guc_log_dump(struct intel_guc_log *log, struct drm_printer *p,
880 		       bool dump_load_err)
881 {
882 	struct intel_guc *guc = log_to_guc(log);
883 	struct intel_uc *uc = container_of(guc, struct intel_uc, guc);
884 	struct drm_i915_gem_object *obj = NULL;
885 	void *map;
886 	u32 *page;
887 	int i, j;
888 
889 	if (!intel_guc_is_supported(guc))
890 		return -ENODEV;
891 
892 	if (dump_load_err)
893 		obj = uc->load_err_log;
894 	else if (guc->log.vma)
895 		obj = guc->log.vma->obj;
896 
897 	if (!obj)
898 		return 0;
899 
900 	page = (u32 *)__get_free_page(GFP_KERNEL);
901 	if (!page)
902 		return -ENOMEM;
903 
904 	intel_guc_dump_time_info(guc, p);
905 
906 	map = i915_gem_object_pin_map_unlocked(obj, I915_MAP_WC);
907 	if (IS_ERR(map)) {
908 		DRM_DEBUG("Failed to pin object\n");
909 		drm_puts(p, "(log data unaccessible)\n");
910 		free_page((unsigned long)page);
911 		return PTR_ERR(map);
912 	}
913 
914 	for (i = 0; i < obj->base.size; i += PAGE_SIZE) {
915 		if (!i915_memcpy_from_wc(page, map + i, PAGE_SIZE))
916 			memcpy(page, map + i, PAGE_SIZE);
917 
918 		for (j = 0; j < PAGE_SIZE / sizeof(u32); j += 4)
919 			drm_printf(p, "0x%08x 0x%08x 0x%08x 0x%08x\n",
920 				   *(page + j + 0), *(page + j + 1),
921 				   *(page + j + 2), *(page + j + 3));
922 	}
923 
924 	drm_puts(p, "\n");
925 
926 	i915_gem_object_unpin_map(obj);
927 	free_page((unsigned long)page);
928 
929 	return 0;
930 }
931