blob: 0f1da810cff0b1e81a6f3dc06db866dd429988c0 [file] [log] [blame]
Tomeu Vizoso731035f2016-12-12 13:29:48 +01001/*
2 * Copyright © 2013 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 * Author: Damien Lespiau <damien.lespiau@intel.com>
24 *
25 */
26
27#include <linux/seq_file.h>
28#include <linux/circ_buf.h>
29#include <linux/ctype.h>
30#include <linux/debugfs.h>
31#include "intel_drv.h"
32
33struct pipe_crc_info {
34 const char *name;
35 struct drm_i915_private *dev_priv;
36 enum pipe pipe;
37};
38
39/* As the drm_debugfs_init() routines are called before dev->dev_private is
40 * allocated we need to hook into the minor for release.
41 */
42static int drm_add_fake_info_node(struct drm_minor *minor,
43 struct dentry *ent, const void *key)
44{
45 struct drm_info_node *node;
46
47 node = kmalloc(sizeof(*node), GFP_KERNEL);
48 if (node == NULL) {
49 debugfs_remove(ent);
50 return -ENOMEM;
51 }
52
53 node->minor = minor;
54 node->dent = ent;
55 node->info_ent = (void *) key;
56
57 mutex_lock(&minor->debugfs_lock);
58 list_add(&node->list, &minor->debugfs_list);
59 mutex_unlock(&minor->debugfs_lock);
60
61 return 0;
62}
63
64static int i915_pipe_crc_open(struct inode *inode, struct file *filep)
65{
66 struct pipe_crc_info *info = inode->i_private;
67 struct drm_i915_private *dev_priv = info->dev_priv;
68 struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[info->pipe];
69
70 if (info->pipe >= INTEL_INFO(dev_priv)->num_pipes)
71 return -ENODEV;
72
73 spin_lock_irq(&pipe_crc->lock);
74
75 if (pipe_crc->opened) {
76 spin_unlock_irq(&pipe_crc->lock);
77 return -EBUSY; /* already open */
78 }
79
80 pipe_crc->opened = true;
81 filep->private_data = inode->i_private;
82
83 spin_unlock_irq(&pipe_crc->lock);
84
85 return 0;
86}
87
88static int i915_pipe_crc_release(struct inode *inode, struct file *filep)
89{
90 struct pipe_crc_info *info = inode->i_private;
91 struct drm_i915_private *dev_priv = info->dev_priv;
92 struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[info->pipe];
93
94 spin_lock_irq(&pipe_crc->lock);
95 pipe_crc->opened = false;
96 spin_unlock_irq(&pipe_crc->lock);
97
98 return 0;
99}
100
101/* (6 fields, 8 chars each, space separated (5) + '\n') */
102#define PIPE_CRC_LINE_LEN (6 * 8 + 5 + 1)
103/* account for \'0' */
104#define PIPE_CRC_BUFFER_LEN (PIPE_CRC_LINE_LEN + 1)
105
106static int pipe_crc_data_count(struct intel_pipe_crc *pipe_crc)
107{
108 assert_spin_locked(&pipe_crc->lock);
109 return CIRC_CNT(pipe_crc->head, pipe_crc->tail,
110 INTEL_PIPE_CRC_ENTRIES_NR);
111}
112
113static ssize_t
114i915_pipe_crc_read(struct file *filep, char __user *user_buf, size_t count,
115 loff_t *pos)
116{
117 struct pipe_crc_info *info = filep->private_data;
118 struct drm_i915_private *dev_priv = info->dev_priv;
119 struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[info->pipe];
120 char buf[PIPE_CRC_BUFFER_LEN];
121 int n_entries;
122 ssize_t bytes_read;
123
124 /*
125 * Don't allow user space to provide buffers not big enough to hold
126 * a line of data.
127 */
128 if (count < PIPE_CRC_LINE_LEN)
129 return -EINVAL;
130
131 if (pipe_crc->source == INTEL_PIPE_CRC_SOURCE_NONE)
132 return 0;
133
134 /* nothing to read */
135 spin_lock_irq(&pipe_crc->lock);
136 while (pipe_crc_data_count(pipe_crc) == 0) {
137 int ret;
138
139 if (filep->f_flags & O_NONBLOCK) {
140 spin_unlock_irq(&pipe_crc->lock);
141 return -EAGAIN;
142 }
143
144 ret = wait_event_interruptible_lock_irq(pipe_crc->wq,
145 pipe_crc_data_count(pipe_crc), pipe_crc->lock);
146 if (ret) {
147 spin_unlock_irq(&pipe_crc->lock);
148 return ret;
149 }
150 }
151
152 /* We now have one or more entries to read */
153 n_entries = count / PIPE_CRC_LINE_LEN;
154
155 bytes_read = 0;
156 while (n_entries > 0) {
157 struct intel_pipe_crc_entry *entry =
158 &pipe_crc->entries[pipe_crc->tail];
159
160 if (CIRC_CNT(pipe_crc->head, pipe_crc->tail,
161 INTEL_PIPE_CRC_ENTRIES_NR) < 1)
162 break;
163
164 BUILD_BUG_ON_NOT_POWER_OF_2(INTEL_PIPE_CRC_ENTRIES_NR);
165 pipe_crc->tail = (pipe_crc->tail + 1) &
166 (INTEL_PIPE_CRC_ENTRIES_NR - 1);
167
168 bytes_read += snprintf(buf, PIPE_CRC_BUFFER_LEN,
169 "%8u %8x %8x %8x %8x %8x\n",
170 entry->frame, entry->crc[0],
171 entry->crc[1], entry->crc[2],
172 entry->crc[3], entry->crc[4]);
173
174 spin_unlock_irq(&pipe_crc->lock);
175
176 if (copy_to_user(user_buf, buf, PIPE_CRC_LINE_LEN))
177 return -EFAULT;
178
179 user_buf += PIPE_CRC_LINE_LEN;
180 n_entries--;
181
182 spin_lock_irq(&pipe_crc->lock);
183 }
184
185 spin_unlock_irq(&pipe_crc->lock);
186
187 return bytes_read;
188}
189
190static const struct file_operations i915_pipe_crc_fops = {
191 .owner = THIS_MODULE,
192 .open = i915_pipe_crc_open,
193 .read = i915_pipe_crc_read,
194 .release = i915_pipe_crc_release,
195};
196
197static struct pipe_crc_info i915_pipe_crc_data[I915_MAX_PIPES] = {
198 {
199 .name = "i915_pipe_A_crc",
200 .pipe = PIPE_A,
201 },
202 {
203 .name = "i915_pipe_B_crc",
204 .pipe = PIPE_B,
205 },
206 {
207 .name = "i915_pipe_C_crc",
208 .pipe = PIPE_C,
209 },
210};
211
212static int i915_pipe_crc_create(struct dentry *root, struct drm_minor *minor,
213 enum pipe pipe)
214{
215 struct drm_i915_private *dev_priv = to_i915(minor->dev);
216 struct dentry *ent;
217 struct pipe_crc_info *info = &i915_pipe_crc_data[pipe];
218
219 info->dev_priv = dev_priv;
220 ent = debugfs_create_file(info->name, S_IRUGO, root, info,
221 &i915_pipe_crc_fops);
222 if (!ent)
223 return -ENOMEM;
224
225 return drm_add_fake_info_node(minor, ent, info);
226}
227
228static const char * const pipe_crc_sources[] = {
229 "none",
230 "plane1",
231 "plane2",
232 "pf",
233 "pipe",
234 "TV",
235 "DP-B",
236 "DP-C",
237 "DP-D",
238 "auto",
239};
240
241static const char *pipe_crc_source_name(enum intel_pipe_crc_source source)
242{
243 BUILD_BUG_ON(ARRAY_SIZE(pipe_crc_sources) != INTEL_PIPE_CRC_SOURCE_MAX);
244 return pipe_crc_sources[source];
245}
246
247static int display_crc_ctl_show(struct seq_file *m, void *data)
248{
249 struct drm_i915_private *dev_priv = m->private;
250 int i;
251
252 for (i = 0; i < I915_MAX_PIPES; i++)
253 seq_printf(m, "%c %s\n", pipe_name(i),
254 pipe_crc_source_name(dev_priv->pipe_crc[i].source));
255
256 return 0;
257}
258
259static int display_crc_ctl_open(struct inode *inode, struct file *file)
260{
261 return single_open(file, display_crc_ctl_show, inode->i_private);
262}
263
264static int i8xx_pipe_crc_ctl_reg(enum intel_pipe_crc_source *source,
265 uint32_t *val)
266{
267 if (*source == INTEL_PIPE_CRC_SOURCE_AUTO)
268 *source = INTEL_PIPE_CRC_SOURCE_PIPE;
269
270 switch (*source) {
271 case INTEL_PIPE_CRC_SOURCE_PIPE:
272 *val = PIPE_CRC_ENABLE | PIPE_CRC_INCLUDE_BORDER_I8XX;
273 break;
274 case INTEL_PIPE_CRC_SOURCE_NONE:
275 *val = 0;
276 break;
277 default:
278 return -EINVAL;
279 }
280
281 return 0;
282}
283
284static int i9xx_pipe_crc_auto_source(struct drm_i915_private *dev_priv,
285 enum pipe pipe,
286 enum intel_pipe_crc_source *source)
287{
288 struct drm_device *dev = &dev_priv->drm;
289 struct intel_encoder *encoder;
290 struct intel_crtc *crtc;
291 struct intel_digital_port *dig_port;
292 int ret = 0;
293
294 *source = INTEL_PIPE_CRC_SOURCE_PIPE;
295
296 drm_modeset_lock_all(dev);
297 for_each_intel_encoder(dev, encoder) {
298 if (!encoder->base.crtc)
299 continue;
300
301 crtc = to_intel_crtc(encoder->base.crtc);
302
303 if (crtc->pipe != pipe)
304 continue;
305
306 switch (encoder->type) {
307 case INTEL_OUTPUT_TVOUT:
308 *source = INTEL_PIPE_CRC_SOURCE_TV;
309 break;
310 case INTEL_OUTPUT_DP:
311 case INTEL_OUTPUT_EDP:
312 dig_port = enc_to_dig_port(&encoder->base);
313 switch (dig_port->port) {
314 case PORT_B:
315 *source = INTEL_PIPE_CRC_SOURCE_DP_B;
316 break;
317 case PORT_C:
318 *source = INTEL_PIPE_CRC_SOURCE_DP_C;
319 break;
320 case PORT_D:
321 *source = INTEL_PIPE_CRC_SOURCE_DP_D;
322 break;
323 default:
324 WARN(1, "nonexisting DP port %c\n",
325 port_name(dig_port->port));
326 break;
327 }
328 break;
329 default:
330 break;
331 }
332 }
333 drm_modeset_unlock_all(dev);
334
335 return ret;
336}
337
338static int vlv_pipe_crc_ctl_reg(struct drm_i915_private *dev_priv,
339 enum pipe pipe,
340 enum intel_pipe_crc_source *source,
341 uint32_t *val)
342{
343 bool need_stable_symbols = false;
344
345 if (*source == INTEL_PIPE_CRC_SOURCE_AUTO) {
346 int ret = i9xx_pipe_crc_auto_source(dev_priv, pipe, source);
347 if (ret)
348 return ret;
349 }
350
351 switch (*source) {
352 case INTEL_PIPE_CRC_SOURCE_PIPE:
353 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PIPE_VLV;
354 break;
355 case INTEL_PIPE_CRC_SOURCE_DP_B:
356 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_DP_B_VLV;
357 need_stable_symbols = true;
358 break;
359 case INTEL_PIPE_CRC_SOURCE_DP_C:
360 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_DP_C_VLV;
361 need_stable_symbols = true;
362 break;
363 case INTEL_PIPE_CRC_SOURCE_DP_D:
364 if (!IS_CHERRYVIEW(dev_priv))
365 return -EINVAL;
366 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_DP_D_VLV;
367 need_stable_symbols = true;
368 break;
369 case INTEL_PIPE_CRC_SOURCE_NONE:
370 *val = 0;
371 break;
372 default:
373 return -EINVAL;
374 }
375
376 /*
377 * When the pipe CRC tap point is after the transcoders we need
378 * to tweak symbol-level features to produce a deterministic series of
379 * symbols for a given frame. We need to reset those features only once
380 * a frame (instead of every nth symbol):
381 * - DC-balance: used to ensure a better clock recovery from the data
382 * link (SDVO)
383 * - DisplayPort scrambling: used for EMI reduction
384 */
385 if (need_stable_symbols) {
386 uint32_t tmp = I915_READ(PORT_DFT2_G4X);
387
388 tmp |= DC_BALANCE_RESET_VLV;
389 switch (pipe) {
390 case PIPE_A:
391 tmp |= PIPE_A_SCRAMBLE_RESET;
392 break;
393 case PIPE_B:
394 tmp |= PIPE_B_SCRAMBLE_RESET;
395 break;
396 case PIPE_C:
397 tmp |= PIPE_C_SCRAMBLE_RESET;
398 break;
399 default:
400 return -EINVAL;
401 }
402 I915_WRITE(PORT_DFT2_G4X, tmp);
403 }
404
405 return 0;
406}
407
408static int i9xx_pipe_crc_ctl_reg(struct drm_i915_private *dev_priv,
409 enum pipe pipe,
410 enum intel_pipe_crc_source *source,
411 uint32_t *val)
412{
413 bool need_stable_symbols = false;
414
415 if (*source == INTEL_PIPE_CRC_SOURCE_AUTO) {
416 int ret = i9xx_pipe_crc_auto_source(dev_priv, pipe, source);
417 if (ret)
418 return ret;
419 }
420
421 switch (*source) {
422 case INTEL_PIPE_CRC_SOURCE_PIPE:
423 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PIPE_I9XX;
424 break;
425 case INTEL_PIPE_CRC_SOURCE_TV:
426 if (!SUPPORTS_TV(dev_priv))
427 return -EINVAL;
428 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_TV_PRE;
429 break;
430 case INTEL_PIPE_CRC_SOURCE_DP_B:
431 if (!IS_G4X(dev_priv))
432 return -EINVAL;
433 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_DP_B_G4X;
434 need_stable_symbols = true;
435 break;
436 case INTEL_PIPE_CRC_SOURCE_DP_C:
437 if (!IS_G4X(dev_priv))
438 return -EINVAL;
439 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_DP_C_G4X;
440 need_stable_symbols = true;
441 break;
442 case INTEL_PIPE_CRC_SOURCE_DP_D:
443 if (!IS_G4X(dev_priv))
444 return -EINVAL;
445 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_DP_D_G4X;
446 need_stable_symbols = true;
447 break;
448 case INTEL_PIPE_CRC_SOURCE_NONE:
449 *val = 0;
450 break;
451 default:
452 return -EINVAL;
453 }
454
455 /*
456 * When the pipe CRC tap point is after the transcoders we need
457 * to tweak symbol-level features to produce a deterministic series of
458 * symbols for a given frame. We need to reset those features only once
459 * a frame (instead of every nth symbol):
460 * - DC-balance: used to ensure a better clock recovery from the data
461 * link (SDVO)
462 * - DisplayPort scrambling: used for EMI reduction
463 */
464 if (need_stable_symbols) {
465 uint32_t tmp = I915_READ(PORT_DFT2_G4X);
466
467 WARN_ON(!IS_G4X(dev_priv));
468
469 I915_WRITE(PORT_DFT_I9XX,
470 I915_READ(PORT_DFT_I9XX) | DC_BALANCE_RESET);
471
472 if (pipe == PIPE_A)
473 tmp |= PIPE_A_SCRAMBLE_RESET;
474 else
475 tmp |= PIPE_B_SCRAMBLE_RESET;
476
477 I915_WRITE(PORT_DFT2_G4X, tmp);
478 }
479
480 return 0;
481}
482
483static void vlv_undo_pipe_scramble_reset(struct drm_i915_private *dev_priv,
484 enum pipe pipe)
485{
486 uint32_t tmp = I915_READ(PORT_DFT2_G4X);
487
488 switch (pipe) {
489 case PIPE_A:
490 tmp &= ~PIPE_A_SCRAMBLE_RESET;
491 break;
492 case PIPE_B:
493 tmp &= ~PIPE_B_SCRAMBLE_RESET;
494 break;
495 case PIPE_C:
496 tmp &= ~PIPE_C_SCRAMBLE_RESET;
497 break;
498 default:
499 return;
500 }
501 if (!(tmp & PIPE_SCRAMBLE_RESET_MASK))
502 tmp &= ~DC_BALANCE_RESET_VLV;
503 I915_WRITE(PORT_DFT2_G4X, tmp);
504
505}
506
507static void g4x_undo_pipe_scramble_reset(struct drm_i915_private *dev_priv,
508 enum pipe pipe)
509{
510 uint32_t tmp = I915_READ(PORT_DFT2_G4X);
511
512 if (pipe == PIPE_A)
513 tmp &= ~PIPE_A_SCRAMBLE_RESET;
514 else
515 tmp &= ~PIPE_B_SCRAMBLE_RESET;
516 I915_WRITE(PORT_DFT2_G4X, tmp);
517
518 if (!(tmp & PIPE_SCRAMBLE_RESET_MASK)) {
519 I915_WRITE(PORT_DFT_I9XX,
520 I915_READ(PORT_DFT_I9XX) & ~DC_BALANCE_RESET);
521 }
522}
523
524static int ilk_pipe_crc_ctl_reg(enum intel_pipe_crc_source *source,
525 uint32_t *val)
526{
527 if (*source == INTEL_PIPE_CRC_SOURCE_AUTO)
528 *source = INTEL_PIPE_CRC_SOURCE_PIPE;
529
530 switch (*source) {
531 case INTEL_PIPE_CRC_SOURCE_PLANE1:
532 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PRIMARY_ILK;
533 break;
534 case INTEL_PIPE_CRC_SOURCE_PLANE2:
535 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_SPRITE_ILK;
536 break;
537 case INTEL_PIPE_CRC_SOURCE_PIPE:
538 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PIPE_ILK;
539 break;
540 case INTEL_PIPE_CRC_SOURCE_NONE:
541 *val = 0;
542 break;
543 default:
544 return -EINVAL;
545 }
546
547 return 0;
548}
549
550static void hsw_trans_edp_pipe_A_crc_wa(struct drm_i915_private *dev_priv,
551 bool enable)
552{
553 struct drm_device *dev = &dev_priv->drm;
554 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_A);
555 struct intel_crtc_state *pipe_config;
556 struct drm_atomic_state *state;
557 int ret = 0;
558
559 drm_modeset_lock_all(dev);
560 state = drm_atomic_state_alloc(dev);
561 if (!state) {
562 ret = -ENOMEM;
563 goto out;
564 }
565
566 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(&crtc->base);
567 pipe_config = intel_atomic_get_crtc_state(state, crtc);
568 if (IS_ERR(pipe_config)) {
569 ret = PTR_ERR(pipe_config);
570 goto out;
571 }
572
573 pipe_config->pch_pfit.force_thru = enable;
574 if (pipe_config->cpu_transcoder == TRANSCODER_EDP &&
575 pipe_config->pch_pfit.enabled != enable)
576 pipe_config->base.connectors_changed = true;
577
578 ret = drm_atomic_commit(state);
579out:
580 WARN(ret, "Toggling workaround to %i returns %i\n", enable, ret);
581 drm_modeset_unlock_all(dev);
582 drm_atomic_state_put(state);
583}
584
585static int ivb_pipe_crc_ctl_reg(struct drm_i915_private *dev_priv,
586 enum pipe pipe,
587 enum intel_pipe_crc_source *source,
588 uint32_t *val)
589{
590 if (*source == INTEL_PIPE_CRC_SOURCE_AUTO)
591 *source = INTEL_PIPE_CRC_SOURCE_PF;
592
593 switch (*source) {
594 case INTEL_PIPE_CRC_SOURCE_PLANE1:
595 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PRIMARY_IVB;
596 break;
597 case INTEL_PIPE_CRC_SOURCE_PLANE2:
598 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_SPRITE_IVB;
599 break;
600 case INTEL_PIPE_CRC_SOURCE_PF:
601 if (IS_HASWELL(dev_priv) && pipe == PIPE_A)
602 hsw_trans_edp_pipe_A_crc_wa(dev_priv, true);
603
604 *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PF_IVB;
605 break;
606 case INTEL_PIPE_CRC_SOURCE_NONE:
607 *val = 0;
608 break;
609 default:
610 return -EINVAL;
611 }
612
613 return 0;
614}
615
Tomeu Vizoso8c6b7092017-01-10 14:43:04 +0100616static int get_new_crc_ctl_reg(struct drm_i915_private *dev_priv,
617 enum pipe pipe,
618 enum intel_pipe_crc_source *source, u32 *val)
619{
620 if (IS_GEN2(dev_priv))
621 return i8xx_pipe_crc_ctl_reg(source, val);
622 else if (INTEL_GEN(dev_priv) < 5)
623 return i9xx_pipe_crc_ctl_reg(dev_priv, pipe, source, val);
624 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
625 return vlv_pipe_crc_ctl_reg(dev_priv, pipe, source, val);
626 else if (IS_GEN5(dev_priv) || IS_GEN6(dev_priv))
627 return ilk_pipe_crc_ctl_reg(source, val);
628 else
629 return ivb_pipe_crc_ctl_reg(dev_priv, pipe, source, val);
630}
631
Tomeu Vizoso731035f2016-12-12 13:29:48 +0100632static int pipe_crc_set_source(struct drm_i915_private *dev_priv,
633 enum pipe pipe,
634 enum intel_pipe_crc_source source)
635{
636 struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
637 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
638 enum intel_display_power_domain power_domain;
639 u32 val = 0; /* shut up gcc */
640 int ret;
641
642 if (pipe_crc->source == source)
643 return 0;
644
645 /* forbid changing the source without going back to 'none' */
646 if (pipe_crc->source && source)
647 return -EINVAL;
648
649 power_domain = POWER_DOMAIN_PIPE(pipe);
650 if (!intel_display_power_get_if_enabled(dev_priv, power_domain)) {
651 DRM_DEBUG_KMS("Trying to capture CRC while pipe is off\n");
652 return -EIO;
653 }
654
Tomeu Vizoso8c6b7092017-01-10 14:43:04 +0100655 ret = get_new_crc_ctl_reg(dev_priv, pipe, &source, &val);
Tomeu Vizoso731035f2016-12-12 13:29:48 +0100656 if (ret != 0)
657 goto out;
658
659 /* none -> real source transition */
660 if (source) {
661 struct intel_pipe_crc_entry *entries;
662
663 DRM_DEBUG_DRIVER("collecting CRCs for pipe %c, %s\n",
664 pipe_name(pipe), pipe_crc_source_name(source));
665
666 entries = kcalloc(INTEL_PIPE_CRC_ENTRIES_NR,
667 sizeof(pipe_crc->entries[0]),
668 GFP_KERNEL);
669 if (!entries) {
670 ret = -ENOMEM;
671 goto out;
672 }
673
674 /*
675 * When IPS gets enabled, the pipe CRC changes. Since IPS gets
676 * enabled and disabled dynamically based on package C states,
677 * user space can't make reliable use of the CRCs, so let's just
678 * completely disable it.
679 */
680 hsw_disable_ips(crtc);
681
682 spin_lock_irq(&pipe_crc->lock);
683 kfree(pipe_crc->entries);
684 pipe_crc->entries = entries;
685 pipe_crc->head = 0;
686 pipe_crc->tail = 0;
687 spin_unlock_irq(&pipe_crc->lock);
688 }
689
690 pipe_crc->source = source;
691
692 I915_WRITE(PIPE_CRC_CTL(pipe), val);
693 POSTING_READ(PIPE_CRC_CTL(pipe));
694
695 /* real source -> none transition */
Tomeu Vizoso8c6b7092017-01-10 14:43:04 +0100696 if (!source) {
Tomeu Vizoso731035f2016-12-12 13:29:48 +0100697 struct intel_pipe_crc_entry *entries;
698 struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
699 pipe);
700
701 DRM_DEBUG_DRIVER("stopping CRCs for pipe %c\n",
702 pipe_name(pipe));
703
704 drm_modeset_lock(&crtc->base.mutex, NULL);
705 if (crtc->base.state->active)
706 intel_wait_for_vblank(dev_priv, pipe);
707 drm_modeset_unlock(&crtc->base.mutex);
708
709 spin_lock_irq(&pipe_crc->lock);
710 entries = pipe_crc->entries;
711 pipe_crc->entries = NULL;
712 pipe_crc->head = 0;
713 pipe_crc->tail = 0;
714 spin_unlock_irq(&pipe_crc->lock);
715
716 kfree(entries);
717
718 if (IS_G4X(dev_priv))
719 g4x_undo_pipe_scramble_reset(dev_priv, pipe);
720 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
721 vlv_undo_pipe_scramble_reset(dev_priv, pipe);
722 else if (IS_HASWELL(dev_priv) && pipe == PIPE_A)
723 hsw_trans_edp_pipe_A_crc_wa(dev_priv, false);
724
725 hsw_enable_ips(crtc);
726 }
727
728 ret = 0;
729
730out:
731 intel_display_power_put(dev_priv, power_domain);
732
733 return ret;
734}
735
736/*
737 * Parse pipe CRC command strings:
738 * command: wsp* object wsp+ name wsp+ source wsp*
739 * object: 'pipe'
740 * name: (A | B | C)
741 * source: (none | plane1 | plane2 | pf)
742 * wsp: (#0x20 | #0x9 | #0xA)+
743 *
744 * eg.:
745 * "pipe A plane1" -> Start CRC computations on plane1 of pipe A
746 * "pipe A none" -> Stop CRC
747 */
748static int display_crc_ctl_tokenize(char *buf, char *words[], int max_words)
749{
750 int n_words = 0;
751
752 while (*buf) {
753 char *end;
754
755 /* skip leading white space */
756 buf = skip_spaces(buf);
757 if (!*buf)
758 break; /* end of buffer */
759
760 /* find end of word */
761 for (end = buf; *end && !isspace(*end); end++)
762 ;
763
764 if (n_words == max_words) {
765 DRM_DEBUG_DRIVER("too many words, allowed <= %d\n",
766 max_words);
767 return -EINVAL; /* ran out of words[] before bytes */
768 }
769
770 if (*end)
771 *end++ = '\0';
772 words[n_words++] = buf;
773 buf = end;
774 }
775
776 return n_words;
777}
778
779enum intel_pipe_crc_object {
780 PIPE_CRC_OBJECT_PIPE,
781};
782
783static const char * const pipe_crc_objects[] = {
784 "pipe",
785};
786
787static int
788display_crc_ctl_parse_object(const char *buf, enum intel_pipe_crc_object *o)
789{
790 int i;
791
792 for (i = 0; i < ARRAY_SIZE(pipe_crc_objects); i++)
793 if (!strcmp(buf, pipe_crc_objects[i])) {
794 *o = i;
795 return 0;
796 }
797
798 return -EINVAL;
799}
800
801static int display_crc_ctl_parse_pipe(const char *buf, enum pipe *pipe)
802{
803 const char name = buf[0];
804
805 if (name < 'A' || name >= pipe_name(I915_MAX_PIPES))
806 return -EINVAL;
807
808 *pipe = name - 'A';
809
810 return 0;
811}
812
813static int
814display_crc_ctl_parse_source(const char *buf, enum intel_pipe_crc_source *s)
815{
816 int i;
817
Tomeu Vizoso8c6b7092017-01-10 14:43:04 +0100818 if (!buf) {
819 *s = INTEL_PIPE_CRC_SOURCE_NONE;
820 return 0;
821 }
822
Tomeu Vizoso731035f2016-12-12 13:29:48 +0100823 for (i = 0; i < ARRAY_SIZE(pipe_crc_sources); i++)
824 if (!strcmp(buf, pipe_crc_sources[i])) {
825 *s = i;
826 return 0;
827 }
828
829 return -EINVAL;
830}
831
832static int display_crc_ctl_parse(struct drm_i915_private *dev_priv,
833 char *buf, size_t len)
834{
835#define N_WORDS 3
836 int n_words;
837 char *words[N_WORDS];
838 enum pipe pipe;
839 enum intel_pipe_crc_object object;
840 enum intel_pipe_crc_source source;
841
842 n_words = display_crc_ctl_tokenize(buf, words, N_WORDS);
843 if (n_words != N_WORDS) {
844 DRM_DEBUG_DRIVER("tokenize failed, a command is %d words\n",
845 N_WORDS);
846 return -EINVAL;
847 }
848
849 if (display_crc_ctl_parse_object(words[0], &object) < 0) {
850 DRM_DEBUG_DRIVER("unknown object %s\n", words[0]);
851 return -EINVAL;
852 }
853
854 if (display_crc_ctl_parse_pipe(words[1], &pipe) < 0) {
855 DRM_DEBUG_DRIVER("unknown pipe %s\n", words[1]);
856 return -EINVAL;
857 }
858
859 if (display_crc_ctl_parse_source(words[2], &source) < 0) {
860 DRM_DEBUG_DRIVER("unknown source %s\n", words[2]);
861 return -EINVAL;
862 }
863
864 return pipe_crc_set_source(dev_priv, pipe, source);
865}
866
867static ssize_t display_crc_ctl_write(struct file *file, const char __user *ubuf,
868 size_t len, loff_t *offp)
869{
870 struct seq_file *m = file->private_data;
871 struct drm_i915_private *dev_priv = m->private;
872 char *tmpbuf;
873 int ret;
874
875 if (len == 0)
876 return 0;
877
878 if (len > PAGE_SIZE - 1) {
879 DRM_DEBUG_DRIVER("expected <%lu bytes into pipe crc control\n",
880 PAGE_SIZE);
881 return -E2BIG;
882 }
883
884 tmpbuf = kmalloc(len + 1, GFP_KERNEL);
885 if (!tmpbuf)
886 return -ENOMEM;
887
888 if (copy_from_user(tmpbuf, ubuf, len)) {
889 ret = -EFAULT;
890 goto out;
891 }
892 tmpbuf[len] = '\0';
893
894 ret = display_crc_ctl_parse(dev_priv, tmpbuf, len);
895
896out:
897 kfree(tmpbuf);
898 if (ret < 0)
899 return ret;
900
901 *offp += len;
902 return len;
903}
904
905const struct file_operations i915_display_crc_ctl_fops = {
906 .owner = THIS_MODULE,
907 .open = display_crc_ctl_open,
908 .read = seq_read,
909 .llseek = seq_lseek,
910 .release = single_release,
911 .write = display_crc_ctl_write
912};
913
914void intel_display_crc_init(struct drm_i915_private *dev_priv)
915{
916 enum pipe pipe;
917
918 for_each_pipe(dev_priv, pipe) {
919 struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
920
921 pipe_crc->opened = false;
922 spin_lock_init(&pipe_crc->lock);
923 init_waitqueue_head(&pipe_crc->wq);
924 }
925}
926
927int intel_pipe_crc_create(struct drm_minor *minor)
928{
929 int ret, i;
930
931 for (i = 0; i < ARRAY_SIZE(i915_pipe_crc_data); i++) {
932 ret = i915_pipe_crc_create(minor->debugfs_root, minor, i);
933 if (ret)
934 return ret;
935 }
936
937 return 0;
938}
939
940void intel_pipe_crc_cleanup(struct drm_minor *minor)
941{
942 int i;
943
944 for (i = 0; i < ARRAY_SIZE(i915_pipe_crc_data); i++) {
945 struct drm_info_list *info_list =
946 (struct drm_info_list *)&i915_pipe_crc_data[i];
947
948 drm_debugfs_remove_files(info_list, 1, minor);
949 }
950}
Tomeu Vizoso8c6b7092017-01-10 14:43:04 +0100951
952int intel_crtc_set_crc_source(struct drm_crtc *crtc, const char *source_name,
953 size_t *values_cnt)
954{
955 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
956 struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[crtc->index];
957 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
958 enum intel_display_power_domain power_domain;
959 enum intel_pipe_crc_source source;
960 u32 val = 0; /* shut up gcc */
961 int ret = 0;
962
963 if (display_crc_ctl_parse_source(source_name, &source) < 0) {
964 DRM_DEBUG_DRIVER("unknown source %s\n", source_name);
965 return -EINVAL;
966 }
967
968 power_domain = POWER_DOMAIN_PIPE(crtc->index);
969 if (!intel_display_power_get_if_enabled(dev_priv, power_domain)) {
970 DRM_DEBUG_KMS("Trying to capture CRC while pipe is off\n");
971 return -EIO;
972 }
973
974 ret = get_new_crc_ctl_reg(dev_priv, crtc->index, &source, &val);
975 if (ret != 0)
976 goto out;
977
978 if (source) {
979 /*
980 * When IPS gets enabled, the pipe CRC changes. Since IPS gets
981 * enabled and disabled dynamically based on package C states,
982 * user space can't make reliable use of the CRCs, so let's just
983 * completely disable it.
984 */
985 hsw_disable_ips(intel_crtc);
986 }
987
988 I915_WRITE(PIPE_CRC_CTL(crtc->index), val);
989 POSTING_READ(PIPE_CRC_CTL(crtc->index));
990
991 if (!source) {
992 if (IS_G4X(dev_priv))
993 g4x_undo_pipe_scramble_reset(dev_priv, crtc->index);
994 else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
995 vlv_undo_pipe_scramble_reset(dev_priv, crtc->index);
996 else if (IS_HASWELL(dev_priv) && crtc->index == PIPE_A)
997 hsw_trans_edp_pipe_A_crc_wa(dev_priv, false);
998
999 hsw_enable_ips(intel_crtc);
1000 }
1001
1002 pipe_crc->skipped = 0;
1003 *values_cnt = 5;
1004
1005out:
1006 intel_display_power_put(dev_priv, power_domain);
1007
1008 return ret;
1009}