intel_pipe_crc.c 24 KB

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