blob: e6d59bf9dae12fc1efbcacc9594ba8dfdaa3c365 [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
61static int guc_log_control(struct intel_guc *guc, u32 control_val)
62{
63 u32 action[] = {
64 INTEL_GUC_ACTION_UK_LOG_ENABLE_LOGGING,
65 control_val
66 };
67
68 return intel_guc_send(guc, action, ARRAY_SIZE(action));
69}
70
Michal Wajdeczkof9cda042017-01-13 17:41:57 +000071/*
72 * Sub buffer switch callback. Called whenever relay has to switch to a new
73 * sub buffer, relay stays on the same sub buffer if 0 is returned.
74 */
75static int subbuf_start_callback(struct rchan_buf *buf,
76 void *subbuf,
77 void *prev_subbuf,
78 size_t prev_padding)
79{
80 /* Use no-overwrite mode by default, where relay will stop accepting
81 * new data if there are no empty sub buffers left.
82 * There is no strict synchronization enforced by relay between Consumer
83 * and Producer. In overwrite mode, there is a possibility of getting
84 * inconsistent/garbled data, the producer could be writing on to the
85 * same sub buffer from which Consumer is reading. This can't be avoided
86 * unless Consumer is fast enough and can always run in tandem with
87 * Producer.
88 */
89 if (relay_buf_full(buf))
90 return 0;
91
92 return 1;
93}
94
95/*
96 * file_create() callback. Creates relay file in debugfs.
97 */
98static struct dentry *create_buf_file_callback(const char *filename,
99 struct dentry *parent,
100 umode_t mode,
101 struct rchan_buf *buf,
102 int *is_global)
103{
104 struct dentry *buf_file;
105
106 /* This to enable the use of a single buffer for the relay channel and
107 * correspondingly have a single file exposed to User, through which
108 * it can collect the logs in order without any post-processing.
109 * Need to set 'is_global' even if parent is NULL for early logging.
110 */
111 *is_global = 1;
112
113 if (!parent)
114 return NULL;
115
116 /* Not using the channel filename passed as an argument, since for each
117 * channel relay appends the corresponding CPU number to the filename
118 * passed in relay_open(). This should be fine as relay just needs a
119 * dentry of the file associated with the channel buffer and that file's
120 * name need not be same as the filename passed as an argument.
121 */
122 buf_file = debugfs_create_file("guc_log", mode,
123 parent, buf, &relay_file_operations);
124 return buf_file;
125}
126
127/*
128 * file_remove() default callback. Removes relay file in debugfs.
129 */
130static int remove_buf_file_callback(struct dentry *dentry)
131{
132 debugfs_remove(dentry);
133 return 0;
134}
135
136/* relay channel callbacks */
137static struct rchan_callbacks relay_callbacks = {
138 .subbuf_start = subbuf_start_callback,
139 .create_buf_file = create_buf_file_callback,
140 .remove_buf_file = remove_buf_file_callback,
141};
142
Oscar Mateo3950bf32017-03-22 10:39:46 -0700143static int guc_log_relay_file_create(struct intel_guc *guc)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000144{
145 struct drm_i915_private *dev_priv = guc_to_i915(guc);
146 struct dentry *log_dir;
147 int ret;
148
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000149 if (!i915_modparams.guc_log_level)
Oscar Mateo3950bf32017-03-22 10:39:46 -0700150 return 0;
151
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000152 /* For now create the log file in /sys/kernel/debug/dri/0 dir */
153 log_dir = dev_priv->drm.primary->debugfs_root;
154
155 /* If /sys/kernel/debug/dri/0 location do not exist, then debugfs is
156 * not mounted and so can't create the relay file.
157 * The relay API seems to fit well with debugfs only, for availing relay
158 * there are 3 requirements which can be met for debugfs file only in a
159 * straightforward/clean manner :-
160 * i) Need the associated dentry pointer of the file, while opening the
161 * relay channel.
162 * ii) Should be able to use 'relay_file_operations' fops for the file.
163 * iii) Set the 'i_private' field of file's inode to the pointer of
164 * relay channel buffer.
165 */
166 if (!log_dir) {
167 DRM_ERROR("Debugfs dir not available yet for GuC log file\n");
168 return -ENODEV;
169 }
170
Oscar Mateoe7465472017-03-22 10:39:48 -0700171 ret = relay_late_setup_files(guc->log.runtime.relay_chan, "guc_log", log_dir);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700172 if (ret < 0 && ret != -EEXIST) {
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000173 DRM_ERROR("Couldn't associate relay chan with file %d\n", ret);
174 return ret;
175 }
176
177 return 0;
178}
179
180static void guc_move_to_next_buf(struct intel_guc *guc)
181{
182 /* Make sure the updates made in the sub buffer are visible when
183 * Consumer sees the following update to offset inside the sub buffer.
184 */
185 smp_wmb();
186
187 /* All data has been written, so now move the offset of sub buffer. */
Oscar Mateoe7465472017-03-22 10:39:48 -0700188 relay_reserve(guc->log.runtime.relay_chan, guc->log.vma->obj->base.size);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000189
190 /* Switch to the next sub buffer */
Oscar Mateoe7465472017-03-22 10:39:48 -0700191 relay_flush(guc->log.runtime.relay_chan);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000192}
193
194static void *guc_get_write_buffer(struct intel_guc *guc)
195{
Oscar Mateoe7465472017-03-22 10:39:48 -0700196 if (!guc->log.runtime.relay_chan)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000197 return NULL;
198
199 /* Just get the base address of a new sub buffer and copy data into it
200 * ourselves. NULL will be returned in no-overwrite mode, if all sub
201 * buffers are full. Could have used the relay_write() to indirectly
202 * copy the data, but that would have been bit convoluted, as we need to
203 * write to only certain locations inside a sub buffer which cannot be
204 * done without using relay_reserve() along with relay_write(). So its
205 * better to use relay_reserve() alone.
206 */
Oscar Mateoe7465472017-03-22 10:39:48 -0700207 return relay_reserve(guc->log.runtime.relay_chan, 0);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000208}
209
210static bool guc_check_log_buf_overflow(struct intel_guc *guc,
211 enum guc_log_buffer_type type,
212 unsigned int full_cnt)
213{
214 unsigned int prev_full_cnt = guc->log.prev_overflow_count[type];
215 bool overflow = false;
216
217 if (full_cnt != prev_full_cnt) {
218 overflow = true;
219
220 guc->log.prev_overflow_count[type] = full_cnt;
221 guc->log.total_overflow_count[type] += full_cnt - prev_full_cnt;
222
223 if (full_cnt < prev_full_cnt) {
224 /* buffer_full_cnt is a 4 bit counter */
225 guc->log.total_overflow_count[type] += 16;
226 }
227 DRM_ERROR_RATELIMITED("GuC log buffer overflow\n");
228 }
229
230 return overflow;
231}
232
233static unsigned int guc_get_log_buffer_size(enum guc_log_buffer_type type)
234{
235 switch (type) {
236 case GUC_ISR_LOG_BUFFER:
237 return (GUC_LOG_ISR_PAGES + 1) * PAGE_SIZE;
238 case GUC_DPC_LOG_BUFFER:
239 return (GUC_LOG_DPC_PAGES + 1) * PAGE_SIZE;
240 case GUC_CRASH_DUMP_LOG_BUFFER:
241 return (GUC_LOG_CRASH_PAGES + 1) * PAGE_SIZE;
242 default:
243 MISSING_CASE(type);
244 }
245
246 return 0;
247}
248
249static void guc_read_update_log_buffer(struct intel_guc *guc)
250{
251 unsigned int buffer_size, read_offset, write_offset, bytes_to_copy, full_cnt;
252 struct guc_log_buffer_state *log_buf_state, *log_buf_snapshot_state;
253 struct guc_log_buffer_state log_buf_state_local;
254 enum guc_log_buffer_type type;
255 void *src_data, *dst_data;
256 bool new_overflow;
257
Oscar Mateoe7465472017-03-22 10:39:48 -0700258 if (WARN_ON(!guc->log.runtime.buf_addr))
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000259 return;
260
261 /* Get the pointer to shared GuC log buffer */
Oscar Mateoe7465472017-03-22 10:39:48 -0700262 log_buf_state = src_data = guc->log.runtime.buf_addr;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000263
264 /* Get the pointer to local buffer to store the logs */
265 log_buf_snapshot_state = dst_data = guc_get_write_buffer(guc);
266
267 /* Actual logs are present from the 2nd page */
268 src_data += PAGE_SIZE;
269 dst_data += PAGE_SIZE;
270
271 for (type = GUC_ISR_LOG_BUFFER; type < GUC_MAX_LOG_BUFFER; type++) {
272 /* Make a copy of the state structure, inside GuC log buffer
273 * (which is uncached mapped), on the stack to avoid reading
274 * from it multiple times.
275 */
276 memcpy(&log_buf_state_local, log_buf_state,
277 sizeof(struct guc_log_buffer_state));
278 buffer_size = guc_get_log_buffer_size(type);
279 read_offset = log_buf_state_local.read_ptr;
280 write_offset = log_buf_state_local.sampled_write_ptr;
281 full_cnt = log_buf_state_local.buffer_full_cnt;
282
283 /* Bookkeeping stuff */
284 guc->log.flush_count[type] += log_buf_state_local.flush_to_file;
285 new_overflow = guc_check_log_buf_overflow(guc, type, full_cnt);
286
287 /* Update the state of shared log buffer */
288 log_buf_state->read_ptr = write_offset;
289 log_buf_state->flush_to_file = 0;
290 log_buf_state++;
291
292 if (unlikely(!log_buf_snapshot_state))
293 continue;
294
295 /* First copy the state structure in snapshot buffer */
296 memcpy(log_buf_snapshot_state, &log_buf_state_local,
297 sizeof(struct guc_log_buffer_state));
298
299 /* The write pointer could have been updated by GuC firmware,
300 * after sending the flush interrupt to Host, for consistency
301 * set write pointer value to same value of sampled_write_ptr
302 * in the snapshot buffer.
303 */
304 log_buf_snapshot_state->write_ptr = write_offset;
305 log_buf_snapshot_state++;
306
307 /* Now copy the actual logs. */
308 if (unlikely(new_overflow)) {
309 /* copy the whole buffer in case of overflow */
310 read_offset = 0;
311 write_offset = buffer_size;
312 } else if (unlikely((read_offset > buffer_size) ||
313 (write_offset > buffer_size))) {
314 DRM_ERROR("invalid log buffer state\n");
315 /* copy whole buffer as offsets are unreliable */
316 read_offset = 0;
317 write_offset = buffer_size;
318 }
319
320 /* Just copy the newly written data */
321 if (read_offset > write_offset) {
322 i915_memcpy_from_wc(dst_data, src_data, write_offset);
323 bytes_to_copy = buffer_size - read_offset;
324 } else {
325 bytes_to_copy = write_offset - read_offset;
326 }
327 i915_memcpy_from_wc(dst_data + read_offset,
328 src_data + read_offset, bytes_to_copy);
329
330 src_data += buffer_size;
331 dst_data += buffer_size;
332 }
333
334 if (log_buf_snapshot_state)
335 guc_move_to_next_buf(guc);
336 else {
337 /* Used rate limited to avoid deluge of messages, logs might be
338 * getting consumed by User at a slow rate.
339 */
340 DRM_ERROR_RATELIMITED("no sub-buffer to capture logs\n");
341 guc->log.capture_miss_count++;
342 }
343}
344
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000345static void capture_logs_work(struct work_struct *work)
346{
347 struct intel_guc *guc =
Oscar Mateoe7465472017-03-22 10:39:48 -0700348 container_of(work, struct intel_guc, log.runtime.flush_work);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000349
350 guc_log_capture_logs(guc);
351}
352
Oscar Mateoe7465472017-03-22 10:39:48 -0700353static bool guc_log_has_runtime(struct intel_guc *guc)
Oscar Mateo3950bf32017-03-22 10:39:46 -0700354{
Oscar Mateoe7465472017-03-22 10:39:48 -0700355 return guc->log.runtime.buf_addr != NULL;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700356}
357
Oscar Mateoe7465472017-03-22 10:39:48 -0700358static int guc_log_runtime_create(struct intel_guc *guc)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000359{
360 struct drm_i915_private *dev_priv = guc_to_i915(guc);
361 void *vaddr;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700362 struct rchan *guc_log_relay_chan;
363 size_t n_subbufs, subbuf_size;
Chris Wilsone22d8e32017-04-12 12:01:11 +0100364 int ret;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000365
366 lockdep_assert_held(&dev_priv->drm.struct_mutex);
367
Oscar Mateoe7465472017-03-22 10:39:48 -0700368 GEM_BUG_ON(guc_log_has_runtime(guc));
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000369
Chris Wilsone22d8e32017-04-12 12:01:11 +0100370 ret = i915_gem_object_set_to_wc_domain(guc->log.vma->obj, true);
371 if (ret)
372 return ret;
373
Oscar Mateo3950bf32017-03-22 10:39:46 -0700374 /* Create a WC (Uncached for read) vmalloc mapping of log
375 * buffer pages, so that we can directly get the data
376 * (up-to-date) from memory.
377 */
378 vaddr = i915_gem_object_pin_map(guc->log.vma->obj, I915_MAP_WC);
379 if (IS_ERR(vaddr)) {
380 DRM_ERROR("Couldn't map log buffer pages %d\n", ret);
381 return PTR_ERR(vaddr);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000382 }
383
Oscar Mateoe7465472017-03-22 10:39:48 -0700384 guc->log.runtime.buf_addr = vaddr;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700385
386 /* Keep the size of sub buffers same as shared log buffer */
387 subbuf_size = guc->log.vma->obj->base.size;
388
389 /* Store up to 8 snapshots, which is large enough to buffer sufficient
390 * boot time logs and provides enough leeway to User, in terms of
391 * latency, for consuming the logs from relay. Also doesn't take
392 * up too much memory.
393 */
394 n_subbufs = 8;
395
396 /* Create a relay channel, so that we have buffers for storing
397 * the GuC firmware logs, the channel will be linked with a file
398 * later on when debugfs is registered.
399 */
400 guc_log_relay_chan = relay_open(NULL, NULL, subbuf_size,
401 n_subbufs, &relay_callbacks, dev_priv);
402 if (!guc_log_relay_chan) {
403 DRM_ERROR("Couldn't create relay chan for GuC logging\n");
404
405 ret = -ENOMEM;
406 goto err_vaddr;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000407 }
408
Oscar Mateo3950bf32017-03-22 10:39:46 -0700409 GEM_BUG_ON(guc_log_relay_chan->subbuf_size < subbuf_size);
Oscar Mateoe7465472017-03-22 10:39:48 -0700410 guc->log.runtime.relay_chan = guc_log_relay_chan;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700411
Oscar Mateoe7465472017-03-22 10:39:48 -0700412 INIT_WORK(&guc->log.runtime.flush_work, capture_logs_work);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000413 return 0;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700414
Oscar Mateo3950bf32017-03-22 10:39:46 -0700415err_vaddr:
416 i915_gem_object_unpin_map(guc->log.vma->obj);
Oscar Mateoe7465472017-03-22 10:39:48 -0700417 guc->log.runtime.buf_addr = NULL;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700418 return ret;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000419}
420
Oscar Mateoe7465472017-03-22 10:39:48 -0700421static void guc_log_runtime_destroy(struct intel_guc *guc)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000422{
Oscar Mateo3950bf32017-03-22 10:39:46 -0700423 /*
Oscar Mateoe7465472017-03-22 10:39:48 -0700424 * It's possible that the runtime stuff was never allocated because
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000425 * GuC log was disabled at the boot time.
426 */
Oscar Mateoe7465472017-03-22 10:39:48 -0700427 if (!guc_log_has_runtime(guc))
Oscar Mateo3950bf32017-03-22 10:39:46 -0700428 return;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000429
Oscar Mateoe7465472017-03-22 10:39:48 -0700430 relay_close(guc->log.runtime.relay_chan);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700431 i915_gem_object_unpin_map(guc->log.vma->obj);
Oscar Mateoe7465472017-03-22 10:39:48 -0700432 guc->log.runtime.buf_addr = NULL;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000433}
434
435static int guc_log_late_setup(struct intel_guc *guc)
436{
437 struct drm_i915_private *dev_priv = guc_to_i915(guc);
438 int ret;
439
440 lockdep_assert_held(&dev_priv->drm.struct_mutex);
441
Oscar Mateoe7465472017-03-22 10:39:48 -0700442 if (!guc_log_has_runtime(guc)) {
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000443 /*
444 * If log was disabled at boot time, then setup needed to handle
445 * log buffer flush interrupts would not have been done yet, so
446 * do that now.
Oscar Mateo3950bf32017-03-22 10:39:46 -0700447 */
Oscar Mateoe7465472017-03-22 10:39:48 -0700448 ret = guc_log_runtime_create(guc);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700449 if (ret)
450 goto err;
451 }
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000452
Oscar Mateo3950bf32017-03-22 10:39:46 -0700453 ret = guc_log_relay_file_create(guc);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000454 if (ret)
Oscar Mateoe7465472017-03-22 10:39:48 -0700455 goto err_runtime;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000456
457 return 0;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700458
Oscar Mateoe7465472017-03-22 10:39:48 -0700459err_runtime:
460 guc_log_runtime_destroy(guc);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000461err:
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000462 /* logging will remain off */
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000463 i915_modparams.guc_log_level = 0;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000464 return ret;
465}
466
467static void guc_log_capture_logs(struct intel_guc *guc)
468{
469 struct drm_i915_private *dev_priv = guc_to_i915(guc);
470
471 guc_read_update_log_buffer(guc);
472
473 /* Generally device is expected to be active only at this
474 * time, so get/put should be really quick.
475 */
476 intel_runtime_pm_get(dev_priv);
477 guc_log_flush_complete(guc);
478 intel_runtime_pm_put(dev_priv);
479}
480
481static void guc_flush_logs(struct intel_guc *guc)
482{
483 struct drm_i915_private *dev_priv = guc_to_i915(guc);
484
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000485 if (!USES_GUC_SUBMISSION(dev_priv) || !i915_modparams.guc_log_level)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000486 return;
487
488 /* First disable the interrupts, will be renabled afterwards */
489 gen9_disable_guc_interrupts(dev_priv);
490
491 /* Before initiating the forceful flush, wait for any pending/ongoing
492 * flush to complete otherwise forceful flush may not actually happen.
493 */
Oscar Mateoe7465472017-03-22 10:39:48 -0700494 flush_work(&guc->log.runtime.flush_work);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000495
496 /* Ask GuC to update the log buffer state */
497 guc_log_flush(guc);
498
499 /* GuC would have updated log buffer by now, so capture it */
500 guc_log_capture_logs(guc);
501}
502
Oscar Mateo3950bf32017-03-22 10:39:46 -0700503int intel_guc_log_create(struct intel_guc *guc)
504{
505 struct i915_vma *vma;
506 unsigned long offset;
Joonas Lahtinenfaf65482017-10-06 11:49:40 +0300507 u32 flags;
508 u32 size;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700509 int ret;
510
511 GEM_BUG_ON(guc->log.vma);
512
Oscar Mateo3950bf32017-03-22 10:39:46 -0700513 /* The first page is to save log buffer state. Allocate one
514 * extra page for others in case for overlap */
515 size = (1 + GUC_LOG_DPC_PAGES + 1 +
516 GUC_LOG_ISR_PAGES + 1 +
517 GUC_LOG_CRASH_PAGES + 1) << PAGE_SHIFT;
518
519 /* We require SSE 4.1 for fast reads from the GuC log buffer and
520 * it should be present on the chipsets supporting GuC based
521 * submisssions.
522 */
523 if (WARN_ON(!i915_has_memcpy_from_wc())) {
524 ret = -EINVAL;
525 goto err;
526 }
527
528 vma = intel_guc_allocate_vma(guc, size);
529 if (IS_ERR(vma)) {
530 ret = PTR_ERR(vma);
531 goto err;
532 }
533
534 guc->log.vma = vma;
535
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000536 if (i915_modparams.guc_log_level) {
Oscar Mateoe7465472017-03-22 10:39:48 -0700537 ret = guc_log_runtime_create(guc);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700538 if (ret < 0)
539 goto err_vma;
540 }
541
542 /* each allocated unit is a page */
543 flags = GUC_LOG_VALID | GUC_LOG_NOTIFY_ON_HALF_FULL |
544 (GUC_LOG_DPC_PAGES << GUC_LOG_DPC_SHIFT) |
545 (GUC_LOG_ISR_PAGES << GUC_LOG_ISR_SHIFT) |
546 (GUC_LOG_CRASH_PAGES << GUC_LOG_CRASH_SHIFT);
547
548 offset = guc_ggtt_offset(vma) >> PAGE_SHIFT; /* in pages */
549 guc->log.flags = (offset << GUC_LOG_BUF_ADDR_SHIFT) | flags;
550
551 return 0;
552
553err_vma:
554 i915_vma_unpin_and_release(&guc->log.vma);
555err:
556 /* logging will be off */
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000557 i915_modparams.guc_log_level = 0;
Oscar Mateo3950bf32017-03-22 10:39:46 -0700558 return ret;
559}
560
561void intel_guc_log_destroy(struct intel_guc *guc)
562{
Oscar Mateoe7465472017-03-22 10:39:48 -0700563 guc_log_runtime_destroy(guc);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700564 i915_vma_unpin_and_release(&guc->log.vma);
565}
566
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000567int i915_guc_log_control(struct drm_i915_private *dev_priv, u64 control_val)
568{
569 struct intel_guc *guc = &dev_priv->guc;
570
571 union guc_log_control log_param;
572 int ret;
573
574 log_param.value = control_val;
575
576 if (log_param.verbosity < GUC_LOG_VERBOSITY_MIN ||
577 log_param.verbosity > GUC_LOG_VERBOSITY_MAX)
578 return -EINVAL;
579
580 /* This combination doesn't make sense & won't have any effect */
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000581 if (!log_param.logging_enabled && !i915_modparams.guc_log_level)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000582 return 0;
583
584 ret = guc_log_control(guc, log_param.value);
585 if (ret < 0) {
586 DRM_DEBUG_DRIVER("guc_logging_control action failed %d\n", ret);
587 return ret;
588 }
589
Oscar Mateo3950bf32017-03-22 10:39:46 -0700590 if (log_param.logging_enabled) {
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000591 i915_modparams.guc_log_level = 1 + log_param.verbosity;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000592
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000593 /*
594 * If log was disabled at boot time, then the relay channel file
595 * wouldn't have been created by now and interrupts also would
596 * not have been enabled. Try again now, just in case.
Oscar Mateo3950bf32017-03-22 10:39:46 -0700597 */
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000598 ret = guc_log_late_setup(guc);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700599 if (ret < 0) {
600 DRM_DEBUG_DRIVER("GuC log late setup failed %d\n", ret);
601 return ret;
602 }
603
604 /* GuC logging is currently the only user of Guc2Host interrupts */
605 gen9_enable_guc_interrupts(dev_priv);
606 } else {
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000607 /*
608 * Once logging is disabled, GuC won't generate logs & send an
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000609 * interrupt. But there could be some data in the log buffer
610 * which is yet to be captured. So request GuC to update the log
611 * buffer state and then collect the left over logs.
612 */
613 guc_flush_logs(guc);
614
615 /* As logging is disabled, update log level to reflect that */
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000616 i915_modparams.guc_log_level = 0;
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000617 }
618
619 return ret;
620}
621
622void i915_guc_log_register(struct drm_i915_private *dev_priv)
623{
Michal Wajdeczko0ed87952018-01-11 15:24:40 +0000624 if (!USES_GUC_SUBMISSION(dev_priv) || !i915_modparams.guc_log_level)
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000625 return;
626
627 mutex_lock(&dev_priv->drm.struct_mutex);
628 guc_log_late_setup(&dev_priv->guc);
629 mutex_unlock(&dev_priv->drm.struct_mutex);
630}
631
632void i915_guc_log_unregister(struct drm_i915_private *dev_priv)
633{
Michal Wajdeczko93ffbe82017-12-06 13:53:12 +0000634 if (!USES_GUC_SUBMISSION(dev_priv))
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000635 return;
636
637 mutex_lock(&dev_priv->drm.struct_mutex);
Oscar Mateo3950bf32017-03-22 10:39:46 -0700638 /* GuC logging is currently the only user of Guc2Host interrupts */
639 gen9_disable_guc_interrupts(dev_priv);
Oscar Mateoe7465472017-03-22 10:39:48 -0700640 guc_log_runtime_destroy(&dev_priv->guc);
Michal Wajdeczkof9cda042017-01-13 17:41:57 +0000641 mutex_unlock(&dev_priv->drm.struct_mutex);
642}