intel_pipe_crc.c 23 KB

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