intel_pipe_crc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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. int ret = 0;
  443. drm_modeset_lock_all(dev);
  444. state = drm_atomic_state_alloc(dev);
  445. if (!state) {
  446. ret = -ENOMEM;
  447. goto unlock;
  448. }
  449. state->acquire_ctx = crtc->base.dev->mode_config.acquire_ctx;
  450. pipe_config = intel_atomic_get_crtc_state(state, crtc);
  451. if (IS_ERR(pipe_config)) {
  452. ret = PTR_ERR(pipe_config);
  453. goto put_state;
  454. }
  455. pipe_config->pch_pfit.force_thru = enable;
  456. if (pipe_config->cpu_transcoder == TRANSCODER_EDP &&
  457. pipe_config->pch_pfit.enabled != enable)
  458. pipe_config->base.connectors_changed = true;
  459. ret = drm_atomic_commit(state);
  460. put_state:
  461. drm_atomic_state_put(state);
  462. unlock:
  463. WARN(ret, "Toggling workaround to %i returns %i\n", enable, ret);
  464. drm_modeset_unlock_all(dev);
  465. }
  466. static int ivb_pipe_crc_ctl_reg(struct drm_i915_private *dev_priv,
  467. enum pipe pipe,
  468. enum intel_pipe_crc_source *source,
  469. uint32_t *val)
  470. {
  471. if (*source == INTEL_PIPE_CRC_SOURCE_AUTO)
  472. *source = INTEL_PIPE_CRC_SOURCE_PF;
  473. switch (*source) {
  474. case INTEL_PIPE_CRC_SOURCE_PLANE1:
  475. *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PRIMARY_IVB;
  476. break;
  477. case INTEL_PIPE_CRC_SOURCE_PLANE2:
  478. *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_SPRITE_IVB;
  479. break;
  480. case INTEL_PIPE_CRC_SOURCE_PF:
  481. if (IS_HASWELL(dev_priv) && pipe == PIPE_A)
  482. hsw_trans_edp_pipe_A_crc_wa(dev_priv, true);
  483. *val = PIPE_CRC_ENABLE | PIPE_CRC_SOURCE_PF_IVB;
  484. break;
  485. case INTEL_PIPE_CRC_SOURCE_NONE:
  486. *val = 0;
  487. break;
  488. default:
  489. return -EINVAL;
  490. }
  491. return 0;
  492. }
  493. static int get_new_crc_ctl_reg(struct drm_i915_private *dev_priv,
  494. enum pipe pipe,
  495. enum intel_pipe_crc_source *source, u32 *val)
  496. {
  497. if (IS_GEN2(dev_priv))
  498. return i8xx_pipe_crc_ctl_reg(source, val);
  499. else if (INTEL_GEN(dev_priv) < 5)
  500. return i9xx_pipe_crc_ctl_reg(dev_priv, pipe, source, val);
  501. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  502. return vlv_pipe_crc_ctl_reg(dev_priv, pipe, source, val);
  503. else if (IS_GEN5(dev_priv) || IS_GEN6(dev_priv))
  504. return ilk_pipe_crc_ctl_reg(source, val);
  505. else
  506. return ivb_pipe_crc_ctl_reg(dev_priv, pipe, source, val);
  507. }
  508. static int pipe_crc_set_source(struct drm_i915_private *dev_priv,
  509. enum pipe pipe,
  510. enum intel_pipe_crc_source source)
  511. {
  512. struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
  513. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  514. enum intel_display_power_domain power_domain;
  515. u32 val = 0; /* shut up gcc */
  516. int ret;
  517. if (pipe_crc->source == source)
  518. return 0;
  519. /* forbid changing the source without going back to 'none' */
  520. if (pipe_crc->source && source)
  521. return -EINVAL;
  522. power_domain = POWER_DOMAIN_PIPE(pipe);
  523. if (!intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  524. DRM_DEBUG_KMS("Trying to capture CRC while pipe is off\n");
  525. return -EIO;
  526. }
  527. ret = get_new_crc_ctl_reg(dev_priv, pipe, &source, &val);
  528. if (ret != 0)
  529. goto out;
  530. /* none -> real source transition */
  531. if (source) {
  532. struct intel_pipe_crc_entry *entries;
  533. DRM_DEBUG_DRIVER("collecting CRCs for pipe %c, %s\n",
  534. pipe_name(pipe), pipe_crc_source_name(source));
  535. entries = kcalloc(INTEL_PIPE_CRC_ENTRIES_NR,
  536. sizeof(pipe_crc->entries[0]),
  537. GFP_KERNEL);
  538. if (!entries) {
  539. ret = -ENOMEM;
  540. goto out;
  541. }
  542. /*
  543. * When IPS gets enabled, the pipe CRC changes. Since IPS gets
  544. * enabled and disabled dynamically based on package C states,
  545. * user space can't make reliable use of the CRCs, so let's just
  546. * completely disable it.
  547. */
  548. hsw_disable_ips(crtc);
  549. spin_lock_irq(&pipe_crc->lock);
  550. kfree(pipe_crc->entries);
  551. pipe_crc->entries = entries;
  552. pipe_crc->head = 0;
  553. pipe_crc->tail = 0;
  554. spin_unlock_irq(&pipe_crc->lock);
  555. }
  556. pipe_crc->source = source;
  557. I915_WRITE(PIPE_CRC_CTL(pipe), val);
  558. POSTING_READ(PIPE_CRC_CTL(pipe));
  559. /* real source -> none transition */
  560. if (!source) {
  561. struct intel_pipe_crc_entry *entries;
  562. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  563. pipe);
  564. DRM_DEBUG_DRIVER("stopping CRCs for pipe %c\n",
  565. pipe_name(pipe));
  566. drm_modeset_lock(&crtc->base.mutex, NULL);
  567. if (crtc->base.state->active)
  568. intel_wait_for_vblank(dev_priv, pipe);
  569. drm_modeset_unlock(&crtc->base.mutex);
  570. spin_lock_irq(&pipe_crc->lock);
  571. entries = pipe_crc->entries;
  572. pipe_crc->entries = NULL;
  573. pipe_crc->head = 0;
  574. pipe_crc->tail = 0;
  575. spin_unlock_irq(&pipe_crc->lock);
  576. kfree(entries);
  577. if (IS_G4X(dev_priv))
  578. g4x_undo_pipe_scramble_reset(dev_priv, pipe);
  579. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  580. vlv_undo_pipe_scramble_reset(dev_priv, pipe);
  581. else if (IS_HASWELL(dev_priv) && pipe == PIPE_A)
  582. hsw_trans_edp_pipe_A_crc_wa(dev_priv, false);
  583. hsw_enable_ips(crtc);
  584. }
  585. ret = 0;
  586. out:
  587. intel_display_power_put(dev_priv, power_domain);
  588. return ret;
  589. }
  590. /*
  591. * Parse pipe CRC command strings:
  592. * command: wsp* object wsp+ name wsp+ source wsp*
  593. * object: 'pipe'
  594. * name: (A | B | C)
  595. * source: (none | plane1 | plane2 | pf)
  596. * wsp: (#0x20 | #0x9 | #0xA)+
  597. *
  598. * eg.:
  599. * "pipe A plane1" -> Start CRC computations on plane1 of pipe A
  600. * "pipe A none" -> Stop CRC
  601. */
  602. static int display_crc_ctl_tokenize(char *buf, char *words[], int max_words)
  603. {
  604. int n_words = 0;
  605. while (*buf) {
  606. char *end;
  607. /* skip leading white space */
  608. buf = skip_spaces(buf);
  609. if (!*buf)
  610. break; /* end of buffer */
  611. /* find end of word */
  612. for (end = buf; *end && !isspace(*end); end++)
  613. ;
  614. if (n_words == max_words) {
  615. DRM_DEBUG_DRIVER("too many words, allowed <= %d\n",
  616. max_words);
  617. return -EINVAL; /* ran out of words[] before bytes */
  618. }
  619. if (*end)
  620. *end++ = '\0';
  621. words[n_words++] = buf;
  622. buf = end;
  623. }
  624. return n_words;
  625. }
  626. enum intel_pipe_crc_object {
  627. PIPE_CRC_OBJECT_PIPE,
  628. };
  629. static const char * const pipe_crc_objects[] = {
  630. "pipe",
  631. };
  632. static int
  633. display_crc_ctl_parse_object(const char *buf, enum intel_pipe_crc_object *o)
  634. {
  635. int i;
  636. for (i = 0; i < ARRAY_SIZE(pipe_crc_objects); i++)
  637. if (!strcmp(buf, pipe_crc_objects[i])) {
  638. *o = i;
  639. return 0;
  640. }
  641. return -EINVAL;
  642. }
  643. static int display_crc_ctl_parse_pipe(const char *buf, enum pipe *pipe)
  644. {
  645. const char name = buf[0];
  646. if (name < 'A' || name >= pipe_name(I915_MAX_PIPES))
  647. return -EINVAL;
  648. *pipe = name - 'A';
  649. return 0;
  650. }
  651. static int
  652. display_crc_ctl_parse_source(const char *buf, enum intel_pipe_crc_source *s)
  653. {
  654. int i;
  655. if (!buf) {
  656. *s = INTEL_PIPE_CRC_SOURCE_NONE;
  657. return 0;
  658. }
  659. for (i = 0; i < ARRAY_SIZE(pipe_crc_sources); i++)
  660. if (!strcmp(buf, pipe_crc_sources[i])) {
  661. *s = i;
  662. return 0;
  663. }
  664. return -EINVAL;
  665. }
  666. static int display_crc_ctl_parse(struct drm_i915_private *dev_priv,
  667. char *buf, size_t len)
  668. {
  669. #define N_WORDS 3
  670. int n_words;
  671. char *words[N_WORDS];
  672. enum pipe pipe;
  673. enum intel_pipe_crc_object object;
  674. enum intel_pipe_crc_source source;
  675. n_words = display_crc_ctl_tokenize(buf, words, N_WORDS);
  676. if (n_words != N_WORDS) {
  677. DRM_DEBUG_DRIVER("tokenize failed, a command is %d words\n",
  678. N_WORDS);
  679. return -EINVAL;
  680. }
  681. if (display_crc_ctl_parse_object(words[0], &object) < 0) {
  682. DRM_DEBUG_DRIVER("unknown object %s\n", words[0]);
  683. return -EINVAL;
  684. }
  685. if (display_crc_ctl_parse_pipe(words[1], &pipe) < 0) {
  686. DRM_DEBUG_DRIVER("unknown pipe %s\n", words[1]);
  687. return -EINVAL;
  688. }
  689. if (display_crc_ctl_parse_source(words[2], &source) < 0) {
  690. DRM_DEBUG_DRIVER("unknown source %s\n", words[2]);
  691. return -EINVAL;
  692. }
  693. return pipe_crc_set_source(dev_priv, pipe, source);
  694. }
  695. static ssize_t display_crc_ctl_write(struct file *file, const char __user *ubuf,
  696. size_t len, loff_t *offp)
  697. {
  698. struct seq_file *m = file->private_data;
  699. struct drm_i915_private *dev_priv = m->private;
  700. char *tmpbuf;
  701. int ret;
  702. if (len == 0)
  703. return 0;
  704. if (len > PAGE_SIZE - 1) {
  705. DRM_DEBUG_DRIVER("expected <%lu bytes into pipe crc control\n",
  706. PAGE_SIZE);
  707. return -E2BIG;
  708. }
  709. tmpbuf = kmalloc(len + 1, GFP_KERNEL);
  710. if (!tmpbuf)
  711. return -ENOMEM;
  712. if (copy_from_user(tmpbuf, ubuf, len)) {
  713. ret = -EFAULT;
  714. goto out;
  715. }
  716. tmpbuf[len] = '\0';
  717. ret = display_crc_ctl_parse(dev_priv, tmpbuf, len);
  718. out:
  719. kfree(tmpbuf);
  720. if (ret < 0)
  721. return ret;
  722. *offp += len;
  723. return len;
  724. }
  725. const struct file_operations i915_display_crc_ctl_fops = {
  726. .owner = THIS_MODULE,
  727. .open = display_crc_ctl_open,
  728. .read = seq_read,
  729. .llseek = seq_lseek,
  730. .release = single_release,
  731. .write = display_crc_ctl_write
  732. };
  733. void intel_display_crc_init(struct drm_i915_private *dev_priv)
  734. {
  735. enum pipe pipe;
  736. for_each_pipe(dev_priv, pipe) {
  737. struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
  738. pipe_crc->opened = false;
  739. spin_lock_init(&pipe_crc->lock);
  740. init_waitqueue_head(&pipe_crc->wq);
  741. }
  742. }
  743. int intel_pipe_crc_create(struct drm_minor *minor)
  744. {
  745. struct drm_i915_private *dev_priv = to_i915(minor->dev);
  746. struct dentry *ent;
  747. int i;
  748. for (i = 0; i < ARRAY_SIZE(i915_pipe_crc_data); i++) {
  749. struct pipe_crc_info *info = &i915_pipe_crc_data[i];
  750. info->dev_priv = dev_priv;
  751. ent = debugfs_create_file(info->name, S_IRUGO,
  752. minor->debugfs_root, info,
  753. &i915_pipe_crc_fops);
  754. if (!ent)
  755. return -ENOMEM;
  756. }
  757. return 0;
  758. }
  759. int intel_crtc_set_crc_source(struct drm_crtc *crtc, const char *source_name,
  760. size_t *values_cnt)
  761. {
  762. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  763. struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[crtc->index];
  764. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  765. enum intel_display_power_domain power_domain;
  766. enum intel_pipe_crc_source source;
  767. u32 val = 0; /* shut up gcc */
  768. int ret = 0;
  769. if (display_crc_ctl_parse_source(source_name, &source) < 0) {
  770. DRM_DEBUG_DRIVER("unknown source %s\n", source_name);
  771. return -EINVAL;
  772. }
  773. power_domain = POWER_DOMAIN_PIPE(crtc->index);
  774. if (!intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  775. DRM_DEBUG_KMS("Trying to capture CRC while pipe is off\n");
  776. return -EIO;
  777. }
  778. ret = get_new_crc_ctl_reg(dev_priv, crtc->index, &source, &val);
  779. if (ret != 0)
  780. goto out;
  781. if (source) {
  782. /*
  783. * When IPS gets enabled, the pipe CRC changes. Since IPS gets
  784. * enabled and disabled dynamically based on package C states,
  785. * user space can't make reliable use of the CRCs, so let's just
  786. * completely disable it.
  787. */
  788. hsw_disable_ips(intel_crtc);
  789. }
  790. I915_WRITE(PIPE_CRC_CTL(crtc->index), val);
  791. POSTING_READ(PIPE_CRC_CTL(crtc->index));
  792. if (!source) {
  793. if (IS_G4X(dev_priv))
  794. g4x_undo_pipe_scramble_reset(dev_priv, crtc->index);
  795. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  796. vlv_undo_pipe_scramble_reset(dev_priv, crtc->index);
  797. else if (IS_HASWELL(dev_priv) && crtc->index == PIPE_A)
  798. hsw_trans_edp_pipe_A_crc_wa(dev_priv, false);
  799. hsw_enable_ips(intel_crtc);
  800. }
  801. pipe_crc->skipped = 0;
  802. *values_cnt = 5;
  803. out:
  804. intel_display_power_put(dev_priv, power_domain);
  805. return ret;
  806. }