blob: 3fbe93ac81dcd9f0392f4e7e6926b4c97b15ff6c [file] [log] [blame]
Michal Wajdeczkof9cda042017-01-13 17:41:57 +00001/*
2 * Copyright © 2014-2017 Intel Corporation
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 (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
Michal Wajdeczkod62e2bf2017-10-04 18:13:39 +000024
Michal Wajdeczkof9cda042017-01-13 17:41:57 +000025#include <linux/debugfs.h>
26#include <linux/relay.h>
Michal Wajdeczkod62e2bf2017-10-04 18:13:39 +000027
28#include "intel_guc_log.h"
Michal Wajdeczkof9cda042017-01-13 17:41:57 +000029#include "i915_drv.h"
30
31static void guc_log_capture_logs(struct intel_guc *guc);
32
33/**
34 * DOC: GuC firmware log
35 *
Michal Wajdeczko0ed87952018-01-11 15:24:40 +000036 * Firmware log is enabled by setting i915.guc_log_level to the positive level.
Michal Wajdeczkof9cda042017-01-13 17:41:57 +000037 * Log data is printed out via reading debugfs i915_guc_log_dump. Reading from
38 * i915_guc_load_status will print out firmware loading status and scratch
39 * registers value.
Michal Wajdeczkof9cda042017-01-13 17:41:57 +000040 */
41
42static int guc_log_flush_complete(struct intel_guc *guc)
43{
44 u32 action[] = {
45 INTEL_GUC_ACTION_LOG_BUFFER_FILE_FLUSH_COMPLETE
46 };
47
48 return intel_guc_send(guc, action, ARRAY_SIZE(action));
49}
50
51static int guc_log_flush(struct intel_guc *guc)
52{
53 u32 action[] = {
54 INTEL_GUC_ACTION_FORCE_LOG_BUFFER_FLUSH,
55 0
56 };
57
58 return intel_guc_send(guc, action, ARRAY_SIZE(action));
59}
60
Michal Wajdeczko35fe7032018-01-11 15:24:41 +000061static int guc_log_control(struct intel_guc *guc, bool enable, u32 verbosity)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +000062{
Michal Wajdeczko35fe7032018-01-11 15:24:41 +000063 union guc_log_control control_val = {
64 .logging_enabled = enable,
65 .verbosity = verbosity,
66 };
Michal Wajdeczkof9cda042017-01-13 17:41:57 +000067 u32 action[] = {
68 INTEL_GUC_ACTION_UK_LOG_ENABLE_LOGGING,
Michal Wajdeczko35fe7032018-01-11 15:24:41 +000069 control_val.value
Michal Wajdeczkof9cda042017-01-13 17:41:57 +000070 };
71
72 return intel_guc_send(guc, action, ARRAY_SIZE(action));
73}
74
Michal Wajdeczkof9cda042017-01-13 17:41:57 +000075/*
76 * Sub buffer switch callback. Called whenever relay has to switch to a new
77 * sub buffer, relay stays on the same sub buffer if 0 is returned.
78 */
79static int subbuf_start_callback(struct rchan_buf *buf,
80 void *subbuf,
81 void *prev_subbuf,
82 size_t prev_padding)
83{
Sagar Arun Kamble2fcf0682018-01-24 21:17:00 +053084 /*
85 * Use no-overwrite mode by default, where relay will stop accepting
Michal Wajdeczkof9cda042017-01-13 17:41:57 +000086 * new data if there are no empty sub buffers left.
87 * There is no strict synchronization enforced by relay between Consumer
88 * and Producer. In overwrite mode, there is a possibility of getting
89 * inconsistent/garbled data, the producer could be writing on to the
90 * same sub buffer from which Consumer is reading. This can't be avoided
91 * unless Consumer is fast enough and can always run in tandem with
92 * Producer.
93 */
94 if (relay_buf_full(buf))
95 return 0;
96
97 return 1;
98}
99
100/*
101 * file_create() callback. Creates relay file in debugfs.
102 */
103static struct dentry *create_buf_file_callback(const char *filename,
104 struct dentry *parent,
105 umode_t mode,
106 struct rchan_buf *buf,
107 int *is_global)
108{
109 struct dentry *buf_file;
110
Sagar Arun Kamble2fcf0682018-01-24 21:17:00 +0530111 /*
112 * This to enable the use of a single buffer for the relay channel and
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000113 * correspondingly have a single file exposed to User, through which
114 * it can collect the logs in order without any post-processing.
115 * Need to set 'is_global' even if parent is NULL for early logging.
116 */
117 *is_global = 1;
118
119 if (!parent)
120 return NULL;
121
Sagar Arun Kamble2fcf0682018-01-24 21:17:00 +0530122 /*
123 * Not using the channel filename passed as an argument, since for each
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000124 * channel relay appends the corresponding CPU number to the filename
125 * passed in relay_open(). This should be fine as relay just needs a
126 * dentry of the file associated with the channel buffer and that file's
127 * name need not be same as the filename passed as an argument.
128 */
129 buf_file = debugfs_create_file("guc_log", mode,
130 parent, buf, &relay_file_operations);
131 return buf_file;
132}
133
134/*
135 * file_remove() default callback. Removes relay file in debugfs.
136 */
137static int remove_buf_file_callback(struct dentry *dentry)
138{
139 debugfs_remove(dentry);
140 return 0;
141}
142
143/* relay channel callbacks */
144static struct rchan_callbacks relay_callbacks = {
145 .subbuf_start = subbuf_start_callback,
146 .create_buf_file = create_buf_file_callback,
147 .remove_buf_file = remove_buf_file_callback,
148};
149
Oscar Mateo3950bf32017-03-22 10:39:46 -0700150static int guc_log_relay_file_create(struct intel_guc *guc)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000151{
152 struct drm_i915_private *dev_priv = guc_to_i915(guc);
153 struct dentry *log_dir;
154 int ret;
155
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000156 if (!i915_modparams.guc_log_level)
Oscar Mateo3950bf32017-03-22 10:39:46 -0700157 return 0;
158
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530159 mutex_lock(&guc->log.runtime.relay_lock);
160
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000161 /* For now create the log file in /sys/kernel/debug/dri/0 dir */
162 log_dir = dev_priv->drm.primary->debugfs_root;
163
Sagar Arun Kamble2fcf0682018-01-24 21:17:00 +0530164 /*
165 * If /sys/kernel/debug/dri/0 location do not exist, then debugfs is
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000166 * not mounted and so can't create the relay file.
167 * The relay API seems to fit well with debugfs only, for availing relay
168 * there are 3 requirements which can be met for debugfs file only in a
169 * straightforward/clean manner :-
170 * i) Need the associated dentry pointer of the file, while opening the
171 * relay channel.
172 * ii) Should be able to use 'relay_file_operations' fops for the file.
173 * iii) Set the 'i_private' field of file's inode to the pointer of
174 * relay channel buffer.
175 */
176 if (!log_dir) {
177 DRM_ERROR("Debugfs dir not available yet for GuC log file\n");
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530178 ret = -ENODEV;
179 goto out_unlock;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000180 }
181
Oscar Mateoe7465472017-03-22 10:39:48 -0700182 ret = relay_late_setup_files(guc->log.runtime.relay_chan, "guc_log", log_dir);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700183 if (ret < 0 && ret != -EEXIST) {
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000184 DRM_ERROR("Couldn't associate relay chan with file %d\n", ret);
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530185 goto out_unlock;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000186 }
187
Sagar Arun Kambleb1852d32018-01-31 11:44:37 +0530188 ret = 0;
189
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530190out_unlock:
191 mutex_unlock(&guc->log.runtime.relay_lock);
192 return ret;
193}
194
195static bool guc_log_has_relay(struct intel_guc *guc)
196{
197 lockdep_assert_held(&guc->log.runtime.relay_lock);
198
199 return guc->log.runtime.relay_chan != NULL;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000200}
201
202static void guc_move_to_next_buf(struct intel_guc *guc)
203{
Sagar Arun Kamble2fcf0682018-01-24 21:17:00 +0530204 /*
205 * Make sure the updates made in the sub buffer are visible when
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000206 * Consumer sees the following update to offset inside the sub buffer.
207 */
208 smp_wmb();
209
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530210 if (!guc_log_has_relay(guc))
211 return;
212
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000213 /* All data has been written, so now move the offset of sub buffer. */
Oscar Mateoe7465472017-03-22 10:39:48 -0700214 relay_reserve(guc->log.runtime.relay_chan, guc->log.vma->obj->base.size);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000215
216 /* Switch to the next sub buffer */
Oscar Mateoe7465472017-03-22 10:39:48 -0700217 relay_flush(guc->log.runtime.relay_chan);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000218}
219
220static void *guc_get_write_buffer(struct intel_guc *guc)
221{
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530222 if (!guc_log_has_relay(guc))
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000223 return NULL;
224
Sagar Arun Kamble2fcf0682018-01-24 21:17:00 +0530225 /*
226 * Just get the base address of a new sub buffer and copy data into it
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000227 * ourselves. NULL will be returned in no-overwrite mode, if all sub
228 * buffers are full. Could have used the relay_write() to indirectly
229 * copy the data, but that would have been bit convoluted, as we need to
230 * write to only certain locations inside a sub buffer which cannot be
231 * done without using relay_reserve() along with relay_write(). So its
232 * better to use relay_reserve() alone.
233 */
Oscar Mateoe7465472017-03-22 10:39:48 -0700234 return relay_reserve(guc->log.runtime.relay_chan, 0);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000235}
236
237static bool guc_check_log_buf_overflow(struct intel_guc *guc,
238 enum guc_log_buffer_type type,
239 unsigned int full_cnt)
240{
241 unsigned int prev_full_cnt = guc->log.prev_overflow_count[type];
242 bool overflow = false;
243
244 if (full_cnt != prev_full_cnt) {
245 overflow = true;
246
247 guc->log.prev_overflow_count[type] = full_cnt;
248 guc->log.total_overflow_count[type] += full_cnt - prev_full_cnt;
249
250 if (full_cnt < prev_full_cnt) {
251 /* buffer_full_cnt is a 4 bit counter */
252 guc->log.total_overflow_count[type] += 16;
253 }
254 DRM_ERROR_RATELIMITED("GuC log buffer overflow\n");
255 }
256
257 return overflow;
258}
259
260static unsigned int guc_get_log_buffer_size(enum guc_log_buffer_type type)
261{
262 switch (type) {
263 case GUC_ISR_LOG_BUFFER:
264 return (GUC_LOG_ISR_PAGES + 1) * PAGE_SIZE;
265 case GUC_DPC_LOG_BUFFER:
266 return (GUC_LOG_DPC_PAGES + 1) * PAGE_SIZE;
267 case GUC_CRASH_DUMP_LOG_BUFFER:
268 return (GUC_LOG_CRASH_PAGES + 1) * PAGE_SIZE;
269 default:
270 MISSING_CASE(type);
271 }
272
273 return 0;
274}
275
276static void guc_read_update_log_buffer(struct intel_guc *guc)
277{
278 unsigned int buffer_size, read_offset, write_offset, bytes_to_copy, full_cnt;
279 struct guc_log_buffer_state *log_buf_state, *log_buf_snapshot_state;
280 struct guc_log_buffer_state log_buf_state_local;
281 enum guc_log_buffer_type type;
282 void *src_data, *dst_data;
283 bool new_overflow;
284
Oscar Mateoe7465472017-03-22 10:39:48 -0700285 if (WARN_ON(!guc->log.runtime.buf_addr))
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000286 return;
287
288 /* Get the pointer to shared GuC log buffer */
Oscar Mateoe7465472017-03-22 10:39:48 -0700289 log_buf_state = src_data = guc->log.runtime.buf_addr;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000290
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530291 mutex_lock(&guc->log.runtime.relay_lock);
292
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000293 /* Get the pointer to local buffer to store the logs */
294 log_buf_snapshot_state = dst_data = guc_get_write_buffer(guc);
295
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530296 if (unlikely(!log_buf_snapshot_state)) {
Sagar Arun Kamble2fcf0682018-01-24 21:17:00 +0530297 /*
298 * Used rate limited to avoid deluge of messages, logs might be
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530299 * getting consumed by User at a slow rate.
300 */
301 DRM_ERROR_RATELIMITED("no sub-buffer to capture logs\n");
302 guc->log.capture_miss_count++;
303 mutex_unlock(&guc->log.runtime.relay_lock);
304
305 return;
306 }
307
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000308 /* Actual logs are present from the 2nd page */
309 src_data += PAGE_SIZE;
310 dst_data += PAGE_SIZE;
311
312 for (type = GUC_ISR_LOG_BUFFER; type < GUC_MAX_LOG_BUFFER; type++) {
Sagar Arun Kamble2fcf0682018-01-24 21:17:00 +0530313 /*
314 * Make a copy of the state structure, inside GuC log buffer
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000315 * (which is uncached mapped), on the stack to avoid reading
316 * from it multiple times.
317 */
318 memcpy(&log_buf_state_local, log_buf_state,
319 sizeof(struct guc_log_buffer_state));
320 buffer_size = guc_get_log_buffer_size(type);
321 read_offset = log_buf_state_local.read_ptr;
322 write_offset = log_buf_state_local.sampled_write_ptr;
323 full_cnt = log_buf_state_local.buffer_full_cnt;
324
325 /* Bookkeeping stuff */
326 guc->log.flush_count[type] += log_buf_state_local.flush_to_file;
327 new_overflow = guc_check_log_buf_overflow(guc, type, full_cnt);
328
329 /* Update the state of shared log buffer */
330 log_buf_state->read_ptr = write_offset;
331 log_buf_state->flush_to_file = 0;
332 log_buf_state++;
333
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000334 /* First copy the state structure in snapshot buffer */
335 memcpy(log_buf_snapshot_state, &log_buf_state_local,
336 sizeof(struct guc_log_buffer_state));
337
Sagar Arun Kamble2fcf0682018-01-24 21:17:00 +0530338 /*
339 * The write pointer could have been updated by GuC firmware,
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000340 * after sending the flush interrupt to Host, for consistency
341 * set write pointer value to same value of sampled_write_ptr
342 * in the snapshot buffer.
343 */
344 log_buf_snapshot_state->write_ptr = write_offset;
345 log_buf_snapshot_state++;
346
347 /* Now copy the actual logs. */
348 if (unlikely(new_overflow)) {
349 /* copy the whole buffer in case of overflow */
350 read_offset = 0;
351 write_offset = buffer_size;
352 } else if (unlikely((read_offset > buffer_size) ||
353 (write_offset > buffer_size))) {
354 DRM_ERROR("invalid log buffer state\n");
355 /* copy whole buffer as offsets are unreliable */
356 read_offset = 0;
357 write_offset = buffer_size;
358 }
359
360 /* Just copy the newly written data */
361 if (read_offset > write_offset) {
362 i915_memcpy_from_wc(dst_data, src_data, write_offset);
363 bytes_to_copy = buffer_size - read_offset;
364 } else {
365 bytes_to_copy = write_offset - read_offset;
366 }
367 i915_memcpy_from_wc(dst_data + read_offset,
368 src_data + read_offset, bytes_to_copy);
369
370 src_data += buffer_size;
371 dst_data += buffer_size;
372 }
373
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530374 guc_move_to_next_buf(guc);
375
376 mutex_unlock(&guc->log.runtime.relay_lock);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000377}
378
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000379static void capture_logs_work(struct work_struct *work)
380{
381 struct intel_guc *guc =
Oscar Mateoe7465472017-03-22 10:39:48 -0700382 container_of(work, struct intel_guc, log.runtime.flush_work);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000383
384 guc_log_capture_logs(guc);
385}
386
Oscar Mateoe7465472017-03-22 10:39:48 -0700387static bool guc_log_has_runtime(struct intel_guc *guc)
Oscar Mateo3950bf32017-03-22 10:39:46 -0700388{
Oscar Mateoe7465472017-03-22 10:39:48 -0700389 return guc->log.runtime.buf_addr != NULL;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700390}
391
Oscar Mateoe7465472017-03-22 10:39:48 -0700392static int guc_log_runtime_create(struct intel_guc *guc)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000393{
394 struct drm_i915_private *dev_priv = guc_to_i915(guc);
395 void *vaddr;
Chris Wilsone22d8e32017-04-12 12:01:11 +0100396 int ret;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000397
398 lockdep_assert_held(&dev_priv->drm.struct_mutex);
399
Oscar Mateoe7465472017-03-22 10:39:48 -0700400 GEM_BUG_ON(guc_log_has_runtime(guc));
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000401
Chris Wilsone22d8e32017-04-12 12:01:11 +0100402 ret = i915_gem_object_set_to_wc_domain(guc->log.vma->obj, true);
403 if (ret)
404 return ret;
405
Sagar Arun Kamble2fcf0682018-01-24 21:17:00 +0530406 /*
407 * Create a WC (Uncached for read) vmalloc mapping of log
Oscar Mateo3950bf32017-03-22 10:39:46 -0700408 * buffer pages, so that we can directly get the data
409 * (up-to-date) from memory.
410 */
411 vaddr = i915_gem_object_pin_map(guc->log.vma->obj, I915_MAP_WC);
412 if (IS_ERR(vaddr)) {
413 DRM_ERROR("Couldn't map log buffer pages %d\n", ret);
414 return PTR_ERR(vaddr);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000415 }
416
Oscar Mateoe7465472017-03-22 10:39:48 -0700417 guc->log.runtime.buf_addr = vaddr;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700418
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530419 return 0;
420}
421
422static void guc_log_runtime_destroy(struct intel_guc *guc)
423{
424 /*
425 * It's possible that the runtime stuff was never allocated because
426 * GuC log was disabled at the boot time.
427 */
428 if (!guc_log_has_runtime(guc))
429 return;
430
431 i915_gem_object_unpin_map(guc->log.vma->obj);
432 guc->log.runtime.buf_addr = NULL;
433}
434
435void intel_guc_log_init_early(struct intel_guc *guc)
436{
437 mutex_init(&guc->log.runtime.relay_lock);
438 INIT_WORK(&guc->log.runtime.flush_work, capture_logs_work);
439}
440
441int intel_guc_log_relay_create(struct intel_guc *guc)
442{
443 struct drm_i915_private *dev_priv = guc_to_i915(guc);
444 struct rchan *guc_log_relay_chan;
445 size_t n_subbufs, subbuf_size;
446 int ret;
447
448 if (!i915_modparams.guc_log_level)
449 return 0;
450
451 mutex_lock(&guc->log.runtime.relay_lock);
452
453 GEM_BUG_ON(guc_log_has_relay(guc));
454
Oscar Mateo3950bf32017-03-22 10:39:46 -0700455 /* Keep the size of sub buffers same as shared log buffer */
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530456 subbuf_size = GUC_LOG_SIZE;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700457
Sagar Arun Kamble2fcf0682018-01-24 21:17:00 +0530458 /*
459 * Store up to 8 snapshots, which is large enough to buffer sufficient
Oscar Mateo3950bf32017-03-22 10:39:46 -0700460 * boot time logs and provides enough leeway to User, in terms of
461 * latency, for consuming the logs from relay. Also doesn't take
462 * up too much memory.
463 */
464 n_subbufs = 8;
465
Sagar Arun Kamble2fcf0682018-01-24 21:17:00 +0530466 /*
467 * Create a relay channel, so that we have buffers for storing
Oscar Mateo3950bf32017-03-22 10:39:46 -0700468 * the GuC firmware logs, the channel will be linked with a file
469 * later on when debugfs is registered.
470 */
471 guc_log_relay_chan = relay_open(NULL, NULL, subbuf_size,
472 n_subbufs, &relay_callbacks, dev_priv);
473 if (!guc_log_relay_chan) {
474 DRM_ERROR("Couldn't create relay chan for GuC logging\n");
475
476 ret = -ENOMEM;
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530477 goto err;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000478 }
479
Oscar Mateo3950bf32017-03-22 10:39:46 -0700480 GEM_BUG_ON(guc_log_relay_chan->subbuf_size < subbuf_size);
Oscar Mateoe7465472017-03-22 10:39:48 -0700481 guc->log.runtime.relay_chan = guc_log_relay_chan;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700482
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530483 mutex_unlock(&guc->log.runtime.relay_lock);
484
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000485 return 0;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700486
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530487err:
488 mutex_unlock(&guc->log.runtime.relay_lock);
489 /* logging will be off */
490 i915_modparams.guc_log_level = 0;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700491 return ret;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000492}
493
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530494void intel_guc_log_relay_destroy(struct intel_guc *guc)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000495{
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530496 mutex_lock(&guc->log.runtime.relay_lock);
497
Oscar Mateo3950bf32017-03-22 10:39:46 -0700498 /*
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530499 * It's possible that the relay was never allocated because
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000500 * GuC log was disabled at the boot time.
501 */
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530502 if (!guc_log_has_relay(guc))
503 goto out_unlock;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000504
Oscar Mateoe7465472017-03-22 10:39:48 -0700505 relay_close(guc->log.runtime.relay_chan);
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530506 guc->log.runtime.relay_chan = NULL;
507
508out_unlock:
509 mutex_unlock(&guc->log.runtime.relay_lock);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000510}
511
512static int guc_log_late_setup(struct intel_guc *guc)
513{
514 struct drm_i915_private *dev_priv = guc_to_i915(guc);
515 int ret;
516
Oscar Mateoe7465472017-03-22 10:39:48 -0700517 if (!guc_log_has_runtime(guc)) {
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000518 /*
519 * If log was disabled at boot time, then setup needed to handle
520 * log buffer flush interrupts would not have been done yet, so
521 * do that now.
Oscar Mateo3950bf32017-03-22 10:39:46 -0700522 */
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530523 ret = intel_guc_log_relay_create(guc);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700524 if (ret)
525 goto err;
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530526
527 mutex_lock(&dev_priv->drm.struct_mutex);
528 intel_runtime_pm_get(dev_priv);
529 ret = guc_log_runtime_create(guc);
530 intel_runtime_pm_put(dev_priv);
531 mutex_unlock(&dev_priv->drm.struct_mutex);
532
533 if (ret)
534 goto err_relay;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700535 }
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000536
Oscar Mateo3950bf32017-03-22 10:39:46 -0700537 ret = guc_log_relay_file_create(guc);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000538 if (ret)
Oscar Mateoe7465472017-03-22 10:39:48 -0700539 goto err_runtime;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000540
541 return 0;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700542
Oscar Mateoe7465472017-03-22 10:39:48 -0700543err_runtime:
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530544 mutex_lock(&dev_priv->drm.struct_mutex);
Oscar Mateoe7465472017-03-22 10:39:48 -0700545 guc_log_runtime_destroy(guc);
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530546 mutex_unlock(&dev_priv->drm.struct_mutex);
547err_relay:
548 intel_guc_log_relay_destroy(guc);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000549err:
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000550 /* logging will remain off */
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000551 i915_modparams.guc_log_level = 0;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000552 return ret;
553}
554
555static void guc_log_capture_logs(struct intel_guc *guc)
556{
557 struct drm_i915_private *dev_priv = guc_to_i915(guc);
558
559 guc_read_update_log_buffer(guc);
560
Sagar Arun Kamble2fcf0682018-01-24 21:17:00 +0530561 /*
562 * Generally device is expected to be active only at this
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000563 * time, so get/put should be really quick.
564 */
565 intel_runtime_pm_get(dev_priv);
566 guc_log_flush_complete(guc);
567 intel_runtime_pm_put(dev_priv);
568}
569
570static void guc_flush_logs(struct intel_guc *guc)
571{
572 struct drm_i915_private *dev_priv = guc_to_i915(guc);
573
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000574 if (!USES_GUC_SUBMISSION(dev_priv) || !i915_modparams.guc_log_level)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000575 return;
576
577 /* First disable the interrupts, will be renabled afterwards */
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530578 mutex_lock(&dev_priv->drm.struct_mutex);
579 intel_runtime_pm_get(dev_priv);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000580 gen9_disable_guc_interrupts(dev_priv);
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530581 intel_runtime_pm_put(dev_priv);
582 mutex_unlock(&dev_priv->drm.struct_mutex);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000583
Sagar Arun Kamble2fcf0682018-01-24 21:17:00 +0530584 /*
585 * Before initiating the forceful flush, wait for any pending/ongoing
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000586 * flush to complete otherwise forceful flush may not actually happen.
587 */
Oscar Mateoe7465472017-03-22 10:39:48 -0700588 flush_work(&guc->log.runtime.flush_work);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000589
590 /* Ask GuC to update the log buffer state */
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530591 intel_runtime_pm_get(dev_priv);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000592 guc_log_flush(guc);
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530593 intel_runtime_pm_put(dev_priv);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000594
595 /* GuC would have updated log buffer by now, so capture it */
596 guc_log_capture_logs(guc);
597}
598
Oscar Mateo3950bf32017-03-22 10:39:46 -0700599int intel_guc_log_create(struct intel_guc *guc)
600{
601 struct i915_vma *vma;
602 unsigned long offset;
Joonas Lahtinenfaf65482017-10-06 11:49:40 +0300603 u32 flags;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700604 int ret;
605
606 GEM_BUG_ON(guc->log.vma);
607
Sagar Arun Kamble2fcf0682018-01-24 21:17:00 +0530608 /*
609 * We require SSE 4.1 for fast reads from the GuC log buffer and
Oscar Mateo3950bf32017-03-22 10:39:46 -0700610 * it should be present on the chipsets supporting GuC based
611 * submisssions.
612 */
613 if (WARN_ON(!i915_has_memcpy_from_wc())) {
614 ret = -EINVAL;
615 goto err;
616 }
617
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530618 vma = intel_guc_allocate_vma(guc, GUC_LOG_SIZE);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700619 if (IS_ERR(vma)) {
620 ret = PTR_ERR(vma);
621 goto err;
622 }
623
624 guc->log.vma = vma;
625
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000626 if (i915_modparams.guc_log_level) {
Oscar Mateoe7465472017-03-22 10:39:48 -0700627 ret = guc_log_runtime_create(guc);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700628 if (ret < 0)
629 goto err_vma;
630 }
631
632 /* each allocated unit is a page */
633 flags = GUC_LOG_VALID | GUC_LOG_NOTIFY_ON_HALF_FULL |
634 (GUC_LOG_DPC_PAGES << GUC_LOG_DPC_SHIFT) |
635 (GUC_LOG_ISR_PAGES << GUC_LOG_ISR_SHIFT) |
636 (GUC_LOG_CRASH_PAGES << GUC_LOG_CRASH_SHIFT);
637
638 offset = guc_ggtt_offset(vma) >> PAGE_SHIFT; /* in pages */
639 guc->log.flags = (offset << GUC_LOG_BUF_ADDR_SHIFT) | flags;
640
641 return 0;
642
643err_vma:
644 i915_vma_unpin_and_release(&guc->log.vma);
645err:
646 /* logging will be off */
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000647 i915_modparams.guc_log_level = 0;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700648 return ret;
649}
650
651void intel_guc_log_destroy(struct intel_guc *guc)
652{
Oscar Mateoe7465472017-03-22 10:39:48 -0700653 guc_log_runtime_destroy(guc);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700654 i915_vma_unpin_and_release(&guc->log.vma);
655}
656
Sagar Arun Kamble065dd5a2018-01-24 21:16:59 +0530657int intel_guc_log_control(struct intel_guc *guc, u64 control_val)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000658{
Sagar Arun Kamble065dd5a2018-01-24 21:16:59 +0530659 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Michal Wajdeczko35fe7032018-01-11 15:24:41 +0000660 bool enable_logging = control_val > 0;
661 u32 verbosity;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000662 int ret;
663
Sagar Arun Kamble065dd5a2018-01-24 21:16:59 +0530664 if (!guc->log.vma)
665 return -ENODEV;
666
Michal Wajdeczko35fe7032018-01-11 15:24:41 +0000667 BUILD_BUG_ON(GUC_LOG_VERBOSITY_MIN);
668 if (control_val > 1 + GUC_LOG_VERBOSITY_MAX)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000669 return -EINVAL;
670
671 /* This combination doesn't make sense & won't have any effect */
Michal Wajdeczko35fe7032018-01-11 15:24:41 +0000672 if (!enable_logging && !i915_modparams.guc_log_level)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000673 return 0;
674
Michal Wajdeczko35fe7032018-01-11 15:24:41 +0000675 verbosity = enable_logging ? control_val - 1 : 0;
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530676
677 ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
678 if (ret)
679 return ret;
680 intel_runtime_pm_get(dev_priv);
Michal Wajdeczko35fe7032018-01-11 15:24:41 +0000681 ret = guc_log_control(guc, enable_logging, verbosity);
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530682 intel_runtime_pm_put(dev_priv);
683 mutex_unlock(&dev_priv->drm.struct_mutex);
684
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000685 if (ret < 0) {
686 DRM_DEBUG_DRIVER("guc_logging_control action failed %d\n", ret);
687 return ret;
688 }
689
Michal Wajdeczko35fe7032018-01-11 15:24:41 +0000690 if (enable_logging) {
691 i915_modparams.guc_log_level = 1 + verbosity;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000692
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000693 /*
694 * If log was disabled at boot time, then the relay channel file
695 * wouldn't have been created by now and interrupts also would
696 * not have been enabled. Try again now, just in case.
Oscar Mateo3950bf32017-03-22 10:39:46 -0700697 */
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000698 ret = guc_log_late_setup(guc);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700699 if (ret < 0) {
700 DRM_DEBUG_DRIVER("GuC log late setup failed %d\n", ret);
701 return ret;
702 }
703
704 /* GuC logging is currently the only user of Guc2Host interrupts */
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530705 mutex_lock(&dev_priv->drm.struct_mutex);
706 intel_runtime_pm_get(dev_priv);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700707 gen9_enable_guc_interrupts(dev_priv);
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530708 intel_runtime_pm_put(dev_priv);
709 mutex_unlock(&dev_priv->drm.struct_mutex);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700710 } else {
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000711 /*
712 * Once logging is disabled, GuC won't generate logs & send an
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000713 * interrupt. But there could be some data in the log buffer
714 * which is yet to be captured. So request GuC to update the log
715 * buffer state and then collect the left over logs.
716 */
717 guc_flush_logs(guc);
718
719 /* As logging is disabled, update log level to reflect that */
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000720 i915_modparams.guc_log_level = 0;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000721 }
722
723 return ret;
724}
725
726void i915_guc_log_register(struct drm_i915_private *dev_priv)
727{
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000728 if (!USES_GUC_SUBMISSION(dev_priv) || !i915_modparams.guc_log_level)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000729 return;
730
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000731 guc_log_late_setup(&dev_priv->guc);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000732}
733
734void i915_guc_log_unregister(struct drm_i915_private *dev_priv)
735{
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530736 struct intel_guc *guc = &dev_priv->guc;
737
Michal Wajdeczko93ffbe82017-12-06 13:53:12 +0000738 if (!USES_GUC_SUBMISSION(dev_priv))
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000739 return;
740
741 mutex_lock(&dev_priv->drm.struct_mutex);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700742 /* GuC logging is currently the only user of Guc2Host interrupts */
Sagar Arun Kamble1be333d2018-01-24 21:16:56 +0530743 intel_runtime_pm_get(dev_priv);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700744 gen9_disable_guc_interrupts(dev_priv);
Sagar Arun Kamble1be333d2018-01-24 21:16:56 +0530745 intel_runtime_pm_put(dev_priv);
746
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530747 guc_log_runtime_destroy(guc);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000748 mutex_unlock(&dev_priv->drm.struct_mutex);
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530749
750 intel_guc_log_relay_destroy(guc);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000751}