blob: 7b5074e2120c25a5a8d6341951ba8f01a12c65dd [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
Michal Wajdeczko28647b52018-01-31 17:32:39 +0000400 if (!guc->log.vma)
401 return -ENODEV;
402
Oscar Mateoe7465472017-03-22 10:39:48 -0700403 GEM_BUG_ON(guc_log_has_runtime(guc));
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000404
Chris Wilsone22d8e32017-04-12 12:01:11 +0100405 ret = i915_gem_object_set_to_wc_domain(guc->log.vma->obj, true);
406 if (ret)
407 return ret;
408
Sagar Arun Kamble2fcf0682018-01-24 21:17:00 +0530409 /*
410 * Create a WC (Uncached for read) vmalloc mapping of log
Oscar Mateo3950bf32017-03-22 10:39:46 -0700411 * buffer pages, so that we can directly get the data
412 * (up-to-date) from memory.
413 */
414 vaddr = i915_gem_object_pin_map(guc->log.vma->obj, I915_MAP_WC);
415 if (IS_ERR(vaddr)) {
416 DRM_ERROR("Couldn't map log buffer pages %d\n", ret);
417 return PTR_ERR(vaddr);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000418 }
419
Oscar Mateoe7465472017-03-22 10:39:48 -0700420 guc->log.runtime.buf_addr = vaddr;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700421
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530422 return 0;
423}
424
425static void guc_log_runtime_destroy(struct intel_guc *guc)
426{
427 /*
428 * It's possible that the runtime stuff was never allocated because
429 * GuC log was disabled at the boot time.
430 */
431 if (!guc_log_has_runtime(guc))
432 return;
433
434 i915_gem_object_unpin_map(guc->log.vma->obj);
435 guc->log.runtime.buf_addr = NULL;
436}
437
438void intel_guc_log_init_early(struct intel_guc *guc)
439{
440 mutex_init(&guc->log.runtime.relay_lock);
441 INIT_WORK(&guc->log.runtime.flush_work, capture_logs_work);
442}
443
444int intel_guc_log_relay_create(struct intel_guc *guc)
445{
446 struct drm_i915_private *dev_priv = guc_to_i915(guc);
447 struct rchan *guc_log_relay_chan;
448 size_t n_subbufs, subbuf_size;
449 int ret;
450
451 if (!i915_modparams.guc_log_level)
452 return 0;
453
454 mutex_lock(&guc->log.runtime.relay_lock);
455
456 GEM_BUG_ON(guc_log_has_relay(guc));
457
Oscar Mateo3950bf32017-03-22 10:39:46 -0700458 /* Keep the size of sub buffers same as shared log buffer */
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530459 subbuf_size = GUC_LOG_SIZE;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700460
Sagar Arun Kamble2fcf0682018-01-24 21:17:00 +0530461 /*
462 * Store up to 8 snapshots, which is large enough to buffer sufficient
Oscar Mateo3950bf32017-03-22 10:39:46 -0700463 * boot time logs and provides enough leeway to User, in terms of
464 * latency, for consuming the logs from relay. Also doesn't take
465 * up too much memory.
466 */
467 n_subbufs = 8;
468
Sagar Arun Kamble2fcf0682018-01-24 21:17:00 +0530469 /*
470 * Create a relay channel, so that we have buffers for storing
Oscar Mateo3950bf32017-03-22 10:39:46 -0700471 * the GuC firmware logs, the channel will be linked with a file
472 * later on when debugfs is registered.
473 */
474 guc_log_relay_chan = relay_open(NULL, NULL, subbuf_size,
475 n_subbufs, &relay_callbacks, dev_priv);
476 if (!guc_log_relay_chan) {
477 DRM_ERROR("Couldn't create relay chan for GuC logging\n");
478
479 ret = -ENOMEM;
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530480 goto err;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000481 }
482
Oscar Mateo3950bf32017-03-22 10:39:46 -0700483 GEM_BUG_ON(guc_log_relay_chan->subbuf_size < subbuf_size);
Oscar Mateoe7465472017-03-22 10:39:48 -0700484 guc->log.runtime.relay_chan = guc_log_relay_chan;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700485
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530486 mutex_unlock(&guc->log.runtime.relay_lock);
487
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000488 return 0;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700489
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530490err:
491 mutex_unlock(&guc->log.runtime.relay_lock);
492 /* logging will be off */
493 i915_modparams.guc_log_level = 0;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700494 return ret;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000495}
496
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530497void intel_guc_log_relay_destroy(struct intel_guc *guc)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000498{
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530499 mutex_lock(&guc->log.runtime.relay_lock);
500
Oscar Mateo3950bf32017-03-22 10:39:46 -0700501 /*
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530502 * It's possible that the relay was never allocated because
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000503 * GuC log was disabled at the boot time.
504 */
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530505 if (!guc_log_has_relay(guc))
506 goto out_unlock;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000507
Oscar Mateoe7465472017-03-22 10:39:48 -0700508 relay_close(guc->log.runtime.relay_chan);
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530509 guc->log.runtime.relay_chan = NULL;
510
511out_unlock:
512 mutex_unlock(&guc->log.runtime.relay_lock);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000513}
514
515static int guc_log_late_setup(struct intel_guc *guc)
516{
517 struct drm_i915_private *dev_priv = guc_to_i915(guc);
518 int ret;
519
Oscar Mateoe7465472017-03-22 10:39:48 -0700520 if (!guc_log_has_runtime(guc)) {
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000521 /*
522 * If log was disabled at boot time, then setup needed to handle
523 * log buffer flush interrupts would not have been done yet, so
524 * do that now.
Oscar Mateo3950bf32017-03-22 10:39:46 -0700525 */
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530526 ret = intel_guc_log_relay_create(guc);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700527 if (ret)
528 goto err;
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530529
530 mutex_lock(&dev_priv->drm.struct_mutex);
531 intel_runtime_pm_get(dev_priv);
532 ret = guc_log_runtime_create(guc);
533 intel_runtime_pm_put(dev_priv);
534 mutex_unlock(&dev_priv->drm.struct_mutex);
535
536 if (ret)
537 goto err_relay;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700538 }
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000539
Oscar Mateo3950bf32017-03-22 10:39:46 -0700540 ret = guc_log_relay_file_create(guc);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000541 if (ret)
Oscar Mateoe7465472017-03-22 10:39:48 -0700542 goto err_runtime;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000543
544 return 0;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700545
Oscar Mateoe7465472017-03-22 10:39:48 -0700546err_runtime:
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530547 mutex_lock(&dev_priv->drm.struct_mutex);
Oscar Mateoe7465472017-03-22 10:39:48 -0700548 guc_log_runtime_destroy(guc);
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530549 mutex_unlock(&dev_priv->drm.struct_mutex);
550err_relay:
551 intel_guc_log_relay_destroy(guc);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000552err:
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000553 /* logging will remain off */
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000554 i915_modparams.guc_log_level = 0;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000555 return ret;
556}
557
558static void guc_log_capture_logs(struct intel_guc *guc)
559{
560 struct drm_i915_private *dev_priv = guc_to_i915(guc);
561
562 guc_read_update_log_buffer(guc);
563
Sagar Arun Kamble2fcf0682018-01-24 21:17:00 +0530564 /*
565 * Generally device is expected to be active only at this
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000566 * time, so get/put should be really quick.
567 */
568 intel_runtime_pm_get(dev_priv);
569 guc_log_flush_complete(guc);
570 intel_runtime_pm_put(dev_priv);
571}
572
573static void guc_flush_logs(struct intel_guc *guc)
574{
575 struct drm_i915_private *dev_priv = guc_to_i915(guc);
576
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000577 if (!USES_GUC_SUBMISSION(dev_priv) || !i915_modparams.guc_log_level)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000578 return;
579
580 /* First disable the interrupts, will be renabled afterwards */
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530581 mutex_lock(&dev_priv->drm.struct_mutex);
582 intel_runtime_pm_get(dev_priv);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000583 gen9_disable_guc_interrupts(dev_priv);
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530584 intel_runtime_pm_put(dev_priv);
585 mutex_unlock(&dev_priv->drm.struct_mutex);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000586
Sagar Arun Kamble2fcf0682018-01-24 21:17:00 +0530587 /*
588 * Before initiating the forceful flush, wait for any pending/ongoing
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000589 * flush to complete otherwise forceful flush may not actually happen.
590 */
Oscar Mateoe7465472017-03-22 10:39:48 -0700591 flush_work(&guc->log.runtime.flush_work);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000592
593 /* Ask GuC to update the log buffer state */
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530594 intel_runtime_pm_get(dev_priv);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000595 guc_log_flush(guc);
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530596 intel_runtime_pm_put(dev_priv);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000597
598 /* GuC would have updated log buffer by now, so capture it */
599 guc_log_capture_logs(guc);
600}
601
Oscar Mateo3950bf32017-03-22 10:39:46 -0700602int intel_guc_log_create(struct intel_guc *guc)
603{
604 struct i915_vma *vma;
605 unsigned long offset;
Joonas Lahtinenfaf65482017-10-06 11:49:40 +0300606 u32 flags;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700607 int ret;
608
609 GEM_BUG_ON(guc->log.vma);
610
Sagar Arun Kamble2fcf0682018-01-24 21:17:00 +0530611 /*
612 * We require SSE 4.1 for fast reads from the GuC log buffer and
Oscar Mateo3950bf32017-03-22 10:39:46 -0700613 * it should be present on the chipsets supporting GuC based
614 * submisssions.
615 */
616 if (WARN_ON(!i915_has_memcpy_from_wc())) {
617 ret = -EINVAL;
618 goto err;
619 }
620
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530621 vma = intel_guc_allocate_vma(guc, GUC_LOG_SIZE);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700622 if (IS_ERR(vma)) {
623 ret = PTR_ERR(vma);
624 goto err;
625 }
626
627 guc->log.vma = vma;
628
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000629 if (i915_modparams.guc_log_level) {
Oscar Mateoe7465472017-03-22 10:39:48 -0700630 ret = guc_log_runtime_create(guc);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700631 if (ret < 0)
632 goto err_vma;
633 }
634
635 /* each allocated unit is a page */
636 flags = GUC_LOG_VALID | GUC_LOG_NOTIFY_ON_HALF_FULL |
637 (GUC_LOG_DPC_PAGES << GUC_LOG_DPC_SHIFT) |
638 (GUC_LOG_ISR_PAGES << GUC_LOG_ISR_SHIFT) |
639 (GUC_LOG_CRASH_PAGES << GUC_LOG_CRASH_SHIFT);
640
641 offset = guc_ggtt_offset(vma) >> PAGE_SHIFT; /* in pages */
642 guc->log.flags = (offset << GUC_LOG_BUF_ADDR_SHIFT) | flags;
643
644 return 0;
645
646err_vma:
647 i915_vma_unpin_and_release(&guc->log.vma);
648err:
649 /* logging will be off */
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000650 i915_modparams.guc_log_level = 0;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700651 return ret;
652}
653
654void intel_guc_log_destroy(struct intel_guc *guc)
655{
Oscar Mateoe7465472017-03-22 10:39:48 -0700656 guc_log_runtime_destroy(guc);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700657 i915_vma_unpin_and_release(&guc->log.vma);
658}
659
Sagar Arun Kamble065dd5a2018-01-24 21:16:59 +0530660int intel_guc_log_control(struct intel_guc *guc, u64 control_val)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000661{
Sagar Arun Kamble065dd5a2018-01-24 21:16:59 +0530662 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Michal Wajdeczko35fe7032018-01-11 15:24:41 +0000663 bool enable_logging = control_val > 0;
664 u32 verbosity;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000665 int ret;
666
Sagar Arun Kamble065dd5a2018-01-24 21:16:59 +0530667 if (!guc->log.vma)
668 return -ENODEV;
669
Michal Wajdeczko35fe7032018-01-11 15:24:41 +0000670 BUILD_BUG_ON(GUC_LOG_VERBOSITY_MIN);
671 if (control_val > 1 + GUC_LOG_VERBOSITY_MAX)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000672 return -EINVAL;
673
674 /* This combination doesn't make sense & won't have any effect */
Michal Wajdeczko35fe7032018-01-11 15:24:41 +0000675 if (!enable_logging && !i915_modparams.guc_log_level)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000676 return 0;
677
Michal Wajdeczko35fe7032018-01-11 15:24:41 +0000678 verbosity = enable_logging ? control_val - 1 : 0;
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530679
680 ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
681 if (ret)
682 return ret;
683 intel_runtime_pm_get(dev_priv);
Michal Wajdeczko35fe7032018-01-11 15:24:41 +0000684 ret = guc_log_control(guc, enable_logging, verbosity);
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530685 intel_runtime_pm_put(dev_priv);
686 mutex_unlock(&dev_priv->drm.struct_mutex);
687
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000688 if (ret < 0) {
689 DRM_DEBUG_DRIVER("guc_logging_control action failed %d\n", ret);
690 return ret;
691 }
692
Michal Wajdeczko35fe7032018-01-11 15:24:41 +0000693 if (enable_logging) {
694 i915_modparams.guc_log_level = 1 + verbosity;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000695
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000696 /*
697 * If log was disabled at boot time, then the relay channel file
698 * wouldn't have been created by now and interrupts also would
699 * not have been enabled. Try again now, just in case.
Oscar Mateo3950bf32017-03-22 10:39:46 -0700700 */
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000701 ret = guc_log_late_setup(guc);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700702 if (ret < 0) {
703 DRM_DEBUG_DRIVER("GuC log late setup failed %d\n", ret);
704 return ret;
705 }
706
707 /* GuC logging is currently the only user of Guc2Host interrupts */
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530708 mutex_lock(&dev_priv->drm.struct_mutex);
709 intel_runtime_pm_get(dev_priv);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700710 gen9_enable_guc_interrupts(dev_priv);
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530711 intel_runtime_pm_put(dev_priv);
712 mutex_unlock(&dev_priv->drm.struct_mutex);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700713 } else {
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000714 /*
715 * Once logging is disabled, GuC won't generate logs & send an
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000716 * interrupt. But there could be some data in the log buffer
717 * which is yet to be captured. So request GuC to update the log
718 * buffer state and then collect the left over logs.
719 */
720 guc_flush_logs(guc);
721
722 /* As logging is disabled, update log level to reflect that */
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000723 i915_modparams.guc_log_level = 0;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000724 }
725
726 return ret;
727}
728
729void i915_guc_log_register(struct drm_i915_private *dev_priv)
730{
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000731 if (!USES_GUC_SUBMISSION(dev_priv) || !i915_modparams.guc_log_level)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000732 return;
733
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000734 guc_log_late_setup(&dev_priv->guc);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000735}
736
737void i915_guc_log_unregister(struct drm_i915_private *dev_priv)
738{
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530739 struct intel_guc *guc = &dev_priv->guc;
740
Michal Wajdeczko93ffbe82017-12-06 13:53:12 +0000741 if (!USES_GUC_SUBMISSION(dev_priv))
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000742 return;
743
744 mutex_lock(&dev_priv->drm.struct_mutex);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700745 /* GuC logging is currently the only user of Guc2Host interrupts */
Sagar Arun Kamble1be333d2018-01-24 21:16:56 +0530746 intel_runtime_pm_get(dev_priv);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700747 gen9_disable_guc_interrupts(dev_priv);
Sagar Arun Kamble1be333d2018-01-24 21:16:56 +0530748 intel_runtime_pm_put(dev_priv);
749
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530750 guc_log_runtime_destroy(guc);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000751 mutex_unlock(&dev_priv->drm.struct_mutex);
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530752
753 intel_guc_log_relay_destroy(guc);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000754}