blob: cb10f7aeb04250898e6737bdceb5d657f28096bf [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{
84 /* Use no-overwrite mode by default, where relay will stop accepting
85 * new data if there are no empty sub buffers left.
86 * There is no strict synchronization enforced by relay between Consumer
87 * and Producer. In overwrite mode, there is a possibility of getting
88 * inconsistent/garbled data, the producer could be writing on to the
89 * same sub buffer from which Consumer is reading. This can't be avoided
90 * unless Consumer is fast enough and can always run in tandem with
91 * Producer.
92 */
93 if (relay_buf_full(buf))
94 return 0;
95
96 return 1;
97}
98
99/*
100 * file_create() callback. Creates relay file in debugfs.
101 */
102static struct dentry *create_buf_file_callback(const char *filename,
103 struct dentry *parent,
104 umode_t mode,
105 struct rchan_buf *buf,
106 int *is_global)
107{
108 struct dentry *buf_file;
109
110 /* This to enable the use of a single buffer for the relay channel and
111 * correspondingly have a single file exposed to User, through which
112 * it can collect the logs in order without any post-processing.
113 * Need to set 'is_global' even if parent is NULL for early logging.
114 */
115 *is_global = 1;
116
117 if (!parent)
118 return NULL;
119
120 /* Not using the channel filename passed as an argument, since for each
121 * channel relay appends the corresponding CPU number to the filename
122 * passed in relay_open(). This should be fine as relay just needs a
123 * dentry of the file associated with the channel buffer and that file's
124 * name need not be same as the filename passed as an argument.
125 */
126 buf_file = debugfs_create_file("guc_log", mode,
127 parent, buf, &relay_file_operations);
128 return buf_file;
129}
130
131/*
132 * file_remove() default callback. Removes relay file in debugfs.
133 */
134static int remove_buf_file_callback(struct dentry *dentry)
135{
136 debugfs_remove(dentry);
137 return 0;
138}
139
140/* relay channel callbacks */
141static struct rchan_callbacks relay_callbacks = {
142 .subbuf_start = subbuf_start_callback,
143 .create_buf_file = create_buf_file_callback,
144 .remove_buf_file = remove_buf_file_callback,
145};
146
Oscar Mateo3950bf32017-03-22 10:39:46 -0700147static int guc_log_relay_file_create(struct intel_guc *guc)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000148{
149 struct drm_i915_private *dev_priv = guc_to_i915(guc);
150 struct dentry *log_dir;
151 int ret;
152
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000153 if (!i915_modparams.guc_log_level)
Oscar Mateo3950bf32017-03-22 10:39:46 -0700154 return 0;
155
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530156 mutex_lock(&guc->log.runtime.relay_lock);
157
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000158 /* For now create the log file in /sys/kernel/debug/dri/0 dir */
159 log_dir = dev_priv->drm.primary->debugfs_root;
160
161 /* If /sys/kernel/debug/dri/0 location do not exist, then debugfs is
162 * not mounted and so can't create the relay file.
163 * The relay API seems to fit well with debugfs only, for availing relay
164 * there are 3 requirements which can be met for debugfs file only in a
165 * straightforward/clean manner :-
166 * i) Need the associated dentry pointer of the file, while opening the
167 * relay channel.
168 * ii) Should be able to use 'relay_file_operations' fops for the file.
169 * iii) Set the 'i_private' field of file's inode to the pointer of
170 * relay channel buffer.
171 */
172 if (!log_dir) {
173 DRM_ERROR("Debugfs dir not available yet for GuC log file\n");
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530174 ret = -ENODEV;
175 goto out_unlock;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000176 }
177
Oscar Mateoe7465472017-03-22 10:39:48 -0700178 ret = relay_late_setup_files(guc->log.runtime.relay_chan, "guc_log", log_dir);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700179 if (ret < 0 && ret != -EEXIST) {
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000180 DRM_ERROR("Couldn't associate relay chan with file %d\n", ret);
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530181 goto out_unlock;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000182 }
183
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530184out_unlock:
185 mutex_unlock(&guc->log.runtime.relay_lock);
186 return ret;
187}
188
189static bool guc_log_has_relay(struct intel_guc *guc)
190{
191 lockdep_assert_held(&guc->log.runtime.relay_lock);
192
193 return guc->log.runtime.relay_chan != NULL;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000194}
195
196static void guc_move_to_next_buf(struct intel_guc *guc)
197{
198 /* Make sure the updates made in the sub buffer are visible when
199 * Consumer sees the following update to offset inside the sub buffer.
200 */
201 smp_wmb();
202
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530203 if (!guc_log_has_relay(guc))
204 return;
205
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000206 /* All data has been written, so now move the offset of sub buffer. */
Oscar Mateoe7465472017-03-22 10:39:48 -0700207 relay_reserve(guc->log.runtime.relay_chan, guc->log.vma->obj->base.size);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000208
209 /* Switch to the next sub buffer */
Oscar Mateoe7465472017-03-22 10:39:48 -0700210 relay_flush(guc->log.runtime.relay_chan);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000211}
212
213static void *guc_get_write_buffer(struct intel_guc *guc)
214{
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530215 if (!guc_log_has_relay(guc))
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000216 return NULL;
217
218 /* Just get the base address of a new sub buffer and copy data into it
219 * ourselves. NULL will be returned in no-overwrite mode, if all sub
220 * buffers are full. Could have used the relay_write() to indirectly
221 * copy the data, but that would have been bit convoluted, as we need to
222 * write to only certain locations inside a sub buffer which cannot be
223 * done without using relay_reserve() along with relay_write(). So its
224 * better to use relay_reserve() alone.
225 */
Oscar Mateoe7465472017-03-22 10:39:48 -0700226 return relay_reserve(guc->log.runtime.relay_chan, 0);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000227}
228
229static bool guc_check_log_buf_overflow(struct intel_guc *guc,
230 enum guc_log_buffer_type type,
231 unsigned int full_cnt)
232{
233 unsigned int prev_full_cnt = guc->log.prev_overflow_count[type];
234 bool overflow = false;
235
236 if (full_cnt != prev_full_cnt) {
237 overflow = true;
238
239 guc->log.prev_overflow_count[type] = full_cnt;
240 guc->log.total_overflow_count[type] += full_cnt - prev_full_cnt;
241
242 if (full_cnt < prev_full_cnt) {
243 /* buffer_full_cnt is a 4 bit counter */
244 guc->log.total_overflow_count[type] += 16;
245 }
246 DRM_ERROR_RATELIMITED("GuC log buffer overflow\n");
247 }
248
249 return overflow;
250}
251
252static unsigned int guc_get_log_buffer_size(enum guc_log_buffer_type type)
253{
254 switch (type) {
255 case GUC_ISR_LOG_BUFFER:
256 return (GUC_LOG_ISR_PAGES + 1) * PAGE_SIZE;
257 case GUC_DPC_LOG_BUFFER:
258 return (GUC_LOG_DPC_PAGES + 1) * PAGE_SIZE;
259 case GUC_CRASH_DUMP_LOG_BUFFER:
260 return (GUC_LOG_CRASH_PAGES + 1) * PAGE_SIZE;
261 default:
262 MISSING_CASE(type);
263 }
264
265 return 0;
266}
267
268static void guc_read_update_log_buffer(struct intel_guc *guc)
269{
270 unsigned int buffer_size, read_offset, write_offset, bytes_to_copy, full_cnt;
271 struct guc_log_buffer_state *log_buf_state, *log_buf_snapshot_state;
272 struct guc_log_buffer_state log_buf_state_local;
273 enum guc_log_buffer_type type;
274 void *src_data, *dst_data;
275 bool new_overflow;
276
Oscar Mateoe7465472017-03-22 10:39:48 -0700277 if (WARN_ON(!guc->log.runtime.buf_addr))
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000278 return;
279
280 /* Get the pointer to shared GuC log buffer */
Oscar Mateoe7465472017-03-22 10:39:48 -0700281 log_buf_state = src_data = guc->log.runtime.buf_addr;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000282
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530283 mutex_lock(&guc->log.runtime.relay_lock);
284
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000285 /* Get the pointer to local buffer to store the logs */
286 log_buf_snapshot_state = dst_data = guc_get_write_buffer(guc);
287
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530288 if (unlikely(!log_buf_snapshot_state)) {
289 /* Used rate limited to avoid deluge of messages, logs might be
290 * getting consumed by User at a slow rate.
291 */
292 DRM_ERROR_RATELIMITED("no sub-buffer to capture logs\n");
293 guc->log.capture_miss_count++;
294 mutex_unlock(&guc->log.runtime.relay_lock);
295
296 return;
297 }
298
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000299 /* Actual logs are present from the 2nd page */
300 src_data += PAGE_SIZE;
301 dst_data += PAGE_SIZE;
302
303 for (type = GUC_ISR_LOG_BUFFER; type < GUC_MAX_LOG_BUFFER; type++) {
304 /* Make a copy of the state structure, inside GuC log buffer
305 * (which is uncached mapped), on the stack to avoid reading
306 * from it multiple times.
307 */
308 memcpy(&log_buf_state_local, log_buf_state,
309 sizeof(struct guc_log_buffer_state));
310 buffer_size = guc_get_log_buffer_size(type);
311 read_offset = log_buf_state_local.read_ptr;
312 write_offset = log_buf_state_local.sampled_write_ptr;
313 full_cnt = log_buf_state_local.buffer_full_cnt;
314
315 /* Bookkeeping stuff */
316 guc->log.flush_count[type] += log_buf_state_local.flush_to_file;
317 new_overflow = guc_check_log_buf_overflow(guc, type, full_cnt);
318
319 /* Update the state of shared log buffer */
320 log_buf_state->read_ptr = write_offset;
321 log_buf_state->flush_to_file = 0;
322 log_buf_state++;
323
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000324 /* First copy the state structure in snapshot buffer */
325 memcpy(log_buf_snapshot_state, &log_buf_state_local,
326 sizeof(struct guc_log_buffer_state));
327
328 /* The write pointer could have been updated by GuC firmware,
329 * after sending the flush interrupt to Host, for consistency
330 * set write pointer value to same value of sampled_write_ptr
331 * in the snapshot buffer.
332 */
333 log_buf_snapshot_state->write_ptr = write_offset;
334 log_buf_snapshot_state++;
335
336 /* Now copy the actual logs. */
337 if (unlikely(new_overflow)) {
338 /* copy the whole buffer in case of overflow */
339 read_offset = 0;
340 write_offset = buffer_size;
341 } else if (unlikely((read_offset > buffer_size) ||
342 (write_offset > buffer_size))) {
343 DRM_ERROR("invalid log buffer state\n");
344 /* copy whole buffer as offsets are unreliable */
345 read_offset = 0;
346 write_offset = buffer_size;
347 }
348
349 /* Just copy the newly written data */
350 if (read_offset > write_offset) {
351 i915_memcpy_from_wc(dst_data, src_data, write_offset);
352 bytes_to_copy = buffer_size - read_offset;
353 } else {
354 bytes_to_copy = write_offset - read_offset;
355 }
356 i915_memcpy_from_wc(dst_data + read_offset,
357 src_data + read_offset, bytes_to_copy);
358
359 src_data += buffer_size;
360 dst_data += buffer_size;
361 }
362
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530363 guc_move_to_next_buf(guc);
364
365 mutex_unlock(&guc->log.runtime.relay_lock);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000366}
367
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000368static void capture_logs_work(struct work_struct *work)
369{
370 struct intel_guc *guc =
Oscar Mateoe7465472017-03-22 10:39:48 -0700371 container_of(work, struct intel_guc, log.runtime.flush_work);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000372
373 guc_log_capture_logs(guc);
374}
375
Oscar Mateoe7465472017-03-22 10:39:48 -0700376static bool guc_log_has_runtime(struct intel_guc *guc)
Oscar Mateo3950bf32017-03-22 10:39:46 -0700377{
Oscar Mateoe7465472017-03-22 10:39:48 -0700378 return guc->log.runtime.buf_addr != NULL;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700379}
380
Oscar Mateoe7465472017-03-22 10:39:48 -0700381static int guc_log_runtime_create(struct intel_guc *guc)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000382{
383 struct drm_i915_private *dev_priv = guc_to_i915(guc);
384 void *vaddr;
Chris Wilsone22d8e32017-04-12 12:01:11 +0100385 int ret;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000386
387 lockdep_assert_held(&dev_priv->drm.struct_mutex);
388
Oscar Mateoe7465472017-03-22 10:39:48 -0700389 GEM_BUG_ON(guc_log_has_runtime(guc));
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000390
Chris Wilsone22d8e32017-04-12 12:01:11 +0100391 ret = i915_gem_object_set_to_wc_domain(guc->log.vma->obj, true);
392 if (ret)
393 return ret;
394
Oscar Mateo3950bf32017-03-22 10:39:46 -0700395 /* Create a WC (Uncached for read) vmalloc mapping of log
396 * buffer pages, so that we can directly get the data
397 * (up-to-date) from memory.
398 */
399 vaddr = i915_gem_object_pin_map(guc->log.vma->obj, I915_MAP_WC);
400 if (IS_ERR(vaddr)) {
401 DRM_ERROR("Couldn't map log buffer pages %d\n", ret);
402 return PTR_ERR(vaddr);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000403 }
404
Oscar Mateoe7465472017-03-22 10:39:48 -0700405 guc->log.runtime.buf_addr = vaddr;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700406
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530407 return 0;
408}
409
410static void guc_log_runtime_destroy(struct intel_guc *guc)
411{
412 /*
413 * It's possible that the runtime stuff was never allocated because
414 * GuC log was disabled at the boot time.
415 */
416 if (!guc_log_has_runtime(guc))
417 return;
418
419 i915_gem_object_unpin_map(guc->log.vma->obj);
420 guc->log.runtime.buf_addr = NULL;
421}
422
423void intel_guc_log_init_early(struct intel_guc *guc)
424{
425 mutex_init(&guc->log.runtime.relay_lock);
426 INIT_WORK(&guc->log.runtime.flush_work, capture_logs_work);
427}
428
429int intel_guc_log_relay_create(struct intel_guc *guc)
430{
431 struct drm_i915_private *dev_priv = guc_to_i915(guc);
432 struct rchan *guc_log_relay_chan;
433 size_t n_subbufs, subbuf_size;
434 int ret;
435
436 if (!i915_modparams.guc_log_level)
437 return 0;
438
439 mutex_lock(&guc->log.runtime.relay_lock);
440
441 GEM_BUG_ON(guc_log_has_relay(guc));
442
Oscar Mateo3950bf32017-03-22 10:39:46 -0700443 /* Keep the size of sub buffers same as shared log buffer */
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530444 subbuf_size = GUC_LOG_SIZE;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700445
446 /* Store up to 8 snapshots, which is large enough to buffer sufficient
447 * boot time logs and provides enough leeway to User, in terms of
448 * latency, for consuming the logs from relay. Also doesn't take
449 * up too much memory.
450 */
451 n_subbufs = 8;
452
453 /* Create a relay channel, so that we have buffers for storing
454 * the GuC firmware logs, the channel will be linked with a file
455 * later on when debugfs is registered.
456 */
457 guc_log_relay_chan = relay_open(NULL, NULL, subbuf_size,
458 n_subbufs, &relay_callbacks, dev_priv);
459 if (!guc_log_relay_chan) {
460 DRM_ERROR("Couldn't create relay chan for GuC logging\n");
461
462 ret = -ENOMEM;
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530463 goto err;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000464 }
465
Oscar Mateo3950bf32017-03-22 10:39:46 -0700466 GEM_BUG_ON(guc_log_relay_chan->subbuf_size < subbuf_size);
Oscar Mateoe7465472017-03-22 10:39:48 -0700467 guc->log.runtime.relay_chan = guc_log_relay_chan;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700468
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530469 mutex_unlock(&guc->log.runtime.relay_lock);
470
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000471 return 0;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700472
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530473err:
474 mutex_unlock(&guc->log.runtime.relay_lock);
475 /* logging will be off */
476 i915_modparams.guc_log_level = 0;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700477 return ret;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000478}
479
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530480void intel_guc_log_relay_destroy(struct intel_guc *guc)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000481{
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530482 mutex_lock(&guc->log.runtime.relay_lock);
483
Oscar Mateo3950bf32017-03-22 10:39:46 -0700484 /*
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530485 * It's possible that the relay was never allocated because
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000486 * GuC log was disabled at the boot time.
487 */
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530488 if (!guc_log_has_relay(guc))
489 goto out_unlock;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000490
Oscar Mateoe7465472017-03-22 10:39:48 -0700491 relay_close(guc->log.runtime.relay_chan);
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530492 guc->log.runtime.relay_chan = NULL;
493
494out_unlock:
495 mutex_unlock(&guc->log.runtime.relay_lock);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000496}
497
498static int guc_log_late_setup(struct intel_guc *guc)
499{
500 struct drm_i915_private *dev_priv = guc_to_i915(guc);
501 int ret;
502
Oscar Mateoe7465472017-03-22 10:39:48 -0700503 if (!guc_log_has_runtime(guc)) {
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000504 /*
505 * If log was disabled at boot time, then setup needed to handle
506 * log buffer flush interrupts would not have been done yet, so
507 * do that now.
Oscar Mateo3950bf32017-03-22 10:39:46 -0700508 */
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530509 ret = intel_guc_log_relay_create(guc);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700510 if (ret)
511 goto err;
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530512
513 mutex_lock(&dev_priv->drm.struct_mutex);
514 intel_runtime_pm_get(dev_priv);
515 ret = guc_log_runtime_create(guc);
516 intel_runtime_pm_put(dev_priv);
517 mutex_unlock(&dev_priv->drm.struct_mutex);
518
519 if (ret)
520 goto err_relay;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700521 }
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000522
Oscar Mateo3950bf32017-03-22 10:39:46 -0700523 ret = guc_log_relay_file_create(guc);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000524 if (ret)
Oscar Mateoe7465472017-03-22 10:39:48 -0700525 goto err_runtime;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000526
527 return 0;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700528
Oscar Mateoe7465472017-03-22 10:39:48 -0700529err_runtime:
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530530 mutex_lock(&dev_priv->drm.struct_mutex);
Oscar Mateoe7465472017-03-22 10:39:48 -0700531 guc_log_runtime_destroy(guc);
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530532 mutex_unlock(&dev_priv->drm.struct_mutex);
533err_relay:
534 intel_guc_log_relay_destroy(guc);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000535err:
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000536 /* logging will remain off */
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000537 i915_modparams.guc_log_level = 0;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000538 return ret;
539}
540
541static void guc_log_capture_logs(struct intel_guc *guc)
542{
543 struct drm_i915_private *dev_priv = guc_to_i915(guc);
544
545 guc_read_update_log_buffer(guc);
546
547 /* Generally device is expected to be active only at this
548 * time, so get/put should be really quick.
549 */
550 intel_runtime_pm_get(dev_priv);
551 guc_log_flush_complete(guc);
552 intel_runtime_pm_put(dev_priv);
553}
554
555static void guc_flush_logs(struct intel_guc *guc)
556{
557 struct drm_i915_private *dev_priv = guc_to_i915(guc);
558
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000559 if (!USES_GUC_SUBMISSION(dev_priv) || !i915_modparams.guc_log_level)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000560 return;
561
562 /* First disable the interrupts, will be renabled afterwards */
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530563 mutex_lock(&dev_priv->drm.struct_mutex);
564 intel_runtime_pm_get(dev_priv);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000565 gen9_disable_guc_interrupts(dev_priv);
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530566 intel_runtime_pm_put(dev_priv);
567 mutex_unlock(&dev_priv->drm.struct_mutex);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000568
569 /* Before initiating the forceful flush, wait for any pending/ongoing
570 * flush to complete otherwise forceful flush may not actually happen.
571 */
Oscar Mateoe7465472017-03-22 10:39:48 -0700572 flush_work(&guc->log.runtime.flush_work);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000573
574 /* Ask GuC to update the log buffer state */
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530575 intel_runtime_pm_get(dev_priv);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000576 guc_log_flush(guc);
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530577 intel_runtime_pm_put(dev_priv);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000578
579 /* GuC would have updated log buffer by now, so capture it */
580 guc_log_capture_logs(guc);
581}
582
Oscar Mateo3950bf32017-03-22 10:39:46 -0700583int intel_guc_log_create(struct intel_guc *guc)
584{
585 struct i915_vma *vma;
586 unsigned long offset;
Joonas Lahtinenfaf65482017-10-06 11:49:40 +0300587 u32 flags;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700588 int ret;
589
590 GEM_BUG_ON(guc->log.vma);
591
Oscar Mateo3950bf32017-03-22 10:39:46 -0700592 /* We require SSE 4.1 for fast reads from the GuC log buffer and
593 * it should be present on the chipsets supporting GuC based
594 * submisssions.
595 */
596 if (WARN_ON(!i915_has_memcpy_from_wc())) {
597 ret = -EINVAL;
598 goto err;
599 }
600
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530601 vma = intel_guc_allocate_vma(guc, GUC_LOG_SIZE);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700602 if (IS_ERR(vma)) {
603 ret = PTR_ERR(vma);
604 goto err;
605 }
606
607 guc->log.vma = vma;
608
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000609 if (i915_modparams.guc_log_level) {
Oscar Mateoe7465472017-03-22 10:39:48 -0700610 ret = guc_log_runtime_create(guc);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700611 if (ret < 0)
612 goto err_vma;
613 }
614
615 /* each allocated unit is a page */
616 flags = GUC_LOG_VALID | GUC_LOG_NOTIFY_ON_HALF_FULL |
617 (GUC_LOG_DPC_PAGES << GUC_LOG_DPC_SHIFT) |
618 (GUC_LOG_ISR_PAGES << GUC_LOG_ISR_SHIFT) |
619 (GUC_LOG_CRASH_PAGES << GUC_LOG_CRASH_SHIFT);
620
621 offset = guc_ggtt_offset(vma) >> PAGE_SHIFT; /* in pages */
622 guc->log.flags = (offset << GUC_LOG_BUF_ADDR_SHIFT) | flags;
623
624 return 0;
625
626err_vma:
627 i915_vma_unpin_and_release(&guc->log.vma);
628err:
629 /* logging will be off */
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000630 i915_modparams.guc_log_level = 0;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700631 return ret;
632}
633
634void intel_guc_log_destroy(struct intel_guc *guc)
635{
Oscar Mateoe7465472017-03-22 10:39:48 -0700636 guc_log_runtime_destroy(guc);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700637 i915_vma_unpin_and_release(&guc->log.vma);
638}
639
Sagar Arun Kamble065dd5a2018-01-24 21:16:59 +0530640int intel_guc_log_control(struct intel_guc *guc, u64 control_val)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000641{
Sagar Arun Kamble065dd5a2018-01-24 21:16:59 +0530642 struct drm_i915_private *dev_priv = guc_to_i915(guc);
Michal Wajdeczko35fe7032018-01-11 15:24:41 +0000643 bool enable_logging = control_val > 0;
644 u32 verbosity;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000645 int ret;
646
Sagar Arun Kamble065dd5a2018-01-24 21:16:59 +0530647 if (!guc->log.vma)
648 return -ENODEV;
649
Michal Wajdeczko35fe7032018-01-11 15:24:41 +0000650 BUILD_BUG_ON(GUC_LOG_VERBOSITY_MIN);
651 if (control_val > 1 + GUC_LOG_VERBOSITY_MAX)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000652 return -EINVAL;
653
654 /* This combination doesn't make sense & won't have any effect */
Michal Wajdeczko35fe7032018-01-11 15:24:41 +0000655 if (!enable_logging && !i915_modparams.guc_log_level)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000656 return 0;
657
Michal Wajdeczko35fe7032018-01-11 15:24:41 +0000658 verbosity = enable_logging ? control_val - 1 : 0;
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530659
660 ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
661 if (ret)
662 return ret;
663 intel_runtime_pm_get(dev_priv);
Michal Wajdeczko35fe7032018-01-11 15:24:41 +0000664 ret = guc_log_control(guc, enable_logging, verbosity);
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530665 intel_runtime_pm_put(dev_priv);
666 mutex_unlock(&dev_priv->drm.struct_mutex);
667
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000668 if (ret < 0) {
669 DRM_DEBUG_DRIVER("guc_logging_control action failed %d\n", ret);
670 return ret;
671 }
672
Michal Wajdeczko35fe7032018-01-11 15:24:41 +0000673 if (enable_logging) {
674 i915_modparams.guc_log_level = 1 + verbosity;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000675
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000676 /*
677 * If log was disabled at boot time, then the relay channel file
678 * wouldn't have been created by now and interrupts also would
679 * not have been enabled. Try again now, just in case.
Oscar Mateo3950bf32017-03-22 10:39:46 -0700680 */
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000681 ret = guc_log_late_setup(guc);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700682 if (ret < 0) {
683 DRM_DEBUG_DRIVER("GuC log late setup failed %d\n", ret);
684 return ret;
685 }
686
687 /* GuC logging is currently the only user of Guc2Host interrupts */
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530688 mutex_lock(&dev_priv->drm.struct_mutex);
689 intel_runtime_pm_get(dev_priv);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700690 gen9_enable_guc_interrupts(dev_priv);
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530691 intel_runtime_pm_put(dev_priv);
692 mutex_unlock(&dev_priv->drm.struct_mutex);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700693 } else {
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000694 /*
695 * Once logging is disabled, GuC won't generate logs & send an
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000696 * interrupt. But there could be some data in the log buffer
697 * which is yet to be captured. So request GuC to update the log
698 * buffer state and then collect the left over logs.
699 */
700 guc_flush_logs(guc);
701
702 /* As logging is disabled, update log level to reflect that */
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000703 i915_modparams.guc_log_level = 0;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000704 }
705
706 return ret;
707}
708
709void i915_guc_log_register(struct drm_i915_private *dev_priv)
710{
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000711 if (!USES_GUC_SUBMISSION(dev_priv) || !i915_modparams.guc_log_level)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000712 return;
713
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000714 guc_log_late_setup(&dev_priv->guc);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000715}
716
717void i915_guc_log_unregister(struct drm_i915_private *dev_priv)
718{
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530719 struct intel_guc *guc = &dev_priv->guc;
720
Michal Wajdeczko93ffbe82017-12-06 13:53:12 +0000721 if (!USES_GUC_SUBMISSION(dev_priv))
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000722 return;
723
724 mutex_lock(&dev_priv->drm.struct_mutex);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700725 /* GuC logging is currently the only user of Guc2Host interrupts */
Sagar Arun Kamble1be333d2018-01-24 21:16:56 +0530726 intel_runtime_pm_get(dev_priv);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700727 gen9_disable_guc_interrupts(dev_priv);
Sagar Arun Kamble1be333d2018-01-24 21:16:56 +0530728 intel_runtime_pm_put(dev_priv);
729
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530730 guc_log_runtime_destroy(guc);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000731 mutex_unlock(&dev_priv->drm.struct_mutex);
Sagar Arun Kamble70deead2018-01-24 21:16:58 +0530732
733 intel_guc_log_relay_destroy(guc);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000734}