intel_guc_log.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /*
  2. * Copyright © 2014-2017 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. */
  24. #include <linux/debugfs.h>
  25. #include <linux/relay.h>
  26. #include "intel_guc_log.h"
  27. #include "i915_drv.h"
  28. static void guc_log_capture_logs(struct intel_guc *guc);
  29. /**
  30. * DOC: GuC firmware log
  31. *
  32. * Firmware log is enabled by setting i915.guc_log_level to the positive level.
  33. * Log data is printed out via reading debugfs i915_guc_log_dump. Reading from
  34. * i915_guc_load_status will print out firmware loading status and scratch
  35. * registers value.
  36. */
  37. static int guc_log_flush_complete(struct intel_guc *guc)
  38. {
  39. u32 action[] = {
  40. INTEL_GUC_ACTION_LOG_BUFFER_FILE_FLUSH_COMPLETE
  41. };
  42. return intel_guc_send(guc, action, ARRAY_SIZE(action));
  43. }
  44. static int guc_log_flush(struct intel_guc *guc)
  45. {
  46. u32 action[] = {
  47. INTEL_GUC_ACTION_FORCE_LOG_BUFFER_FLUSH,
  48. 0
  49. };
  50. return intel_guc_send(guc, action, ARRAY_SIZE(action));
  51. }
  52. static int guc_log_control(struct intel_guc *guc, bool enable, u32 verbosity)
  53. {
  54. union guc_log_control control_val = {
  55. {
  56. .logging_enabled = enable,
  57. .verbosity = verbosity,
  58. },
  59. };
  60. u32 action[] = {
  61. INTEL_GUC_ACTION_UK_LOG_ENABLE_LOGGING,
  62. control_val.value
  63. };
  64. return intel_guc_send(guc, action, ARRAY_SIZE(action));
  65. }
  66. /*
  67. * Sub buffer switch callback. Called whenever relay has to switch to a new
  68. * sub buffer, relay stays on the same sub buffer if 0 is returned.
  69. */
  70. static int subbuf_start_callback(struct rchan_buf *buf,
  71. void *subbuf,
  72. void *prev_subbuf,
  73. size_t prev_padding)
  74. {
  75. /*
  76. * Use no-overwrite mode by default, where relay will stop accepting
  77. * new data if there are no empty sub buffers left.
  78. * There is no strict synchronization enforced by relay between Consumer
  79. * and Producer. In overwrite mode, there is a possibility of getting
  80. * inconsistent/garbled data, the producer could be writing on to the
  81. * same sub buffer from which Consumer is reading. This can't be avoided
  82. * unless Consumer is fast enough and can always run in tandem with
  83. * Producer.
  84. */
  85. if (relay_buf_full(buf))
  86. return 0;
  87. return 1;
  88. }
  89. /*
  90. * file_create() callback. Creates relay file in debugfs.
  91. */
  92. static struct dentry *create_buf_file_callback(const char *filename,
  93. struct dentry *parent,
  94. umode_t mode,
  95. struct rchan_buf *buf,
  96. int *is_global)
  97. {
  98. struct dentry *buf_file;
  99. /*
  100. * This to enable the use of a single buffer for the relay channel and
  101. * correspondingly have a single file exposed to User, through which
  102. * it can collect the logs in order without any post-processing.
  103. * Need to set 'is_global' even if parent is NULL for early logging.
  104. */
  105. *is_global = 1;
  106. if (!parent)
  107. return NULL;
  108. /*
  109. * Not using the channel filename passed as an argument, since for each
  110. * channel relay appends the corresponding CPU number to the filename
  111. * passed in relay_open(). This should be fine as relay just needs a
  112. * dentry of the file associated with the channel buffer and that file's
  113. * name need not be same as the filename passed as an argument.
  114. */
  115. buf_file = debugfs_create_file("guc_log", mode,
  116. parent, buf, &relay_file_operations);
  117. return buf_file;
  118. }
  119. /*
  120. * file_remove() default callback. Removes relay file in debugfs.
  121. */
  122. static int remove_buf_file_callback(struct dentry *dentry)
  123. {
  124. debugfs_remove(dentry);
  125. return 0;
  126. }
  127. /* relay channel callbacks */
  128. static struct rchan_callbacks relay_callbacks = {
  129. .subbuf_start = subbuf_start_callback,
  130. .create_buf_file = create_buf_file_callback,
  131. .remove_buf_file = remove_buf_file_callback,
  132. };
  133. static int guc_log_relay_file_create(struct intel_guc *guc)
  134. {
  135. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  136. struct dentry *log_dir;
  137. int ret;
  138. if (!i915_modparams.guc_log_level)
  139. return 0;
  140. mutex_lock(&guc->log.runtime.relay_lock);
  141. /* For now create the log file in /sys/kernel/debug/dri/0 dir */
  142. log_dir = dev_priv->drm.primary->debugfs_root;
  143. /*
  144. * If /sys/kernel/debug/dri/0 location do not exist, then debugfs is
  145. * not mounted and so can't create the relay file.
  146. * The relay API seems to fit well with debugfs only, for availing relay
  147. * there are 3 requirements which can be met for debugfs file only in a
  148. * straightforward/clean manner :-
  149. * i) Need the associated dentry pointer of the file, while opening the
  150. * relay channel.
  151. * ii) Should be able to use 'relay_file_operations' fops for the file.
  152. * iii) Set the 'i_private' field of file's inode to the pointer of
  153. * relay channel buffer.
  154. */
  155. if (!log_dir) {
  156. DRM_ERROR("Debugfs dir not available yet for GuC log file\n");
  157. ret = -ENODEV;
  158. goto out_unlock;
  159. }
  160. ret = relay_late_setup_files(guc->log.runtime.relay_chan, "guc_log", log_dir);
  161. if (ret < 0 && ret != -EEXIST) {
  162. DRM_ERROR("Couldn't associate relay chan with file %d\n", ret);
  163. goto out_unlock;
  164. }
  165. ret = 0;
  166. out_unlock:
  167. mutex_unlock(&guc->log.runtime.relay_lock);
  168. return ret;
  169. }
  170. static bool guc_log_has_relay(struct intel_guc *guc)
  171. {
  172. lockdep_assert_held(&guc->log.runtime.relay_lock);
  173. return guc->log.runtime.relay_chan != NULL;
  174. }
  175. static void guc_move_to_next_buf(struct intel_guc *guc)
  176. {
  177. /*
  178. * Make sure the updates made in the sub buffer are visible when
  179. * Consumer sees the following update to offset inside the sub buffer.
  180. */
  181. smp_wmb();
  182. if (!guc_log_has_relay(guc))
  183. return;
  184. /* All data has been written, so now move the offset of sub buffer. */
  185. relay_reserve(guc->log.runtime.relay_chan, guc->log.vma->obj->base.size);
  186. /* Switch to the next sub buffer */
  187. relay_flush(guc->log.runtime.relay_chan);
  188. }
  189. static void *guc_get_write_buffer(struct intel_guc *guc)
  190. {
  191. if (!guc_log_has_relay(guc))
  192. return NULL;
  193. /*
  194. * Just get the base address of a new sub buffer and copy data into it
  195. * ourselves. NULL will be returned in no-overwrite mode, if all sub
  196. * buffers are full. Could have used the relay_write() to indirectly
  197. * copy the data, but that would have been bit convoluted, as we need to
  198. * write to only certain locations inside a sub buffer which cannot be
  199. * done without using relay_reserve() along with relay_write(). So its
  200. * better to use relay_reserve() alone.
  201. */
  202. return relay_reserve(guc->log.runtime.relay_chan, 0);
  203. }
  204. static bool guc_check_log_buf_overflow(struct intel_guc *guc,
  205. enum guc_log_buffer_type type,
  206. unsigned int full_cnt)
  207. {
  208. unsigned int prev_full_cnt = guc->log.prev_overflow_count[type];
  209. bool overflow = false;
  210. if (full_cnt != prev_full_cnt) {
  211. overflow = true;
  212. guc->log.prev_overflow_count[type] = full_cnt;
  213. guc->log.total_overflow_count[type] += full_cnt - prev_full_cnt;
  214. if (full_cnt < prev_full_cnt) {
  215. /* buffer_full_cnt is a 4 bit counter */
  216. guc->log.total_overflow_count[type] += 16;
  217. }
  218. DRM_ERROR_RATELIMITED("GuC log buffer overflow\n");
  219. }
  220. return overflow;
  221. }
  222. static unsigned int guc_get_log_buffer_size(enum guc_log_buffer_type type)
  223. {
  224. switch (type) {
  225. case GUC_ISR_LOG_BUFFER:
  226. return (GUC_LOG_ISR_PAGES + 1) * PAGE_SIZE;
  227. case GUC_DPC_LOG_BUFFER:
  228. return (GUC_LOG_DPC_PAGES + 1) * PAGE_SIZE;
  229. case GUC_CRASH_DUMP_LOG_BUFFER:
  230. return (GUC_LOG_CRASH_PAGES + 1) * PAGE_SIZE;
  231. default:
  232. MISSING_CASE(type);
  233. }
  234. return 0;
  235. }
  236. static void guc_read_update_log_buffer(struct intel_guc *guc)
  237. {
  238. unsigned int buffer_size, read_offset, write_offset, bytes_to_copy, full_cnt;
  239. struct guc_log_buffer_state *log_buf_state, *log_buf_snapshot_state;
  240. struct guc_log_buffer_state log_buf_state_local;
  241. enum guc_log_buffer_type type;
  242. void *src_data, *dst_data;
  243. bool new_overflow;
  244. if (WARN_ON(!guc->log.runtime.buf_addr))
  245. return;
  246. /* Get the pointer to shared GuC log buffer */
  247. log_buf_state = src_data = guc->log.runtime.buf_addr;
  248. mutex_lock(&guc->log.runtime.relay_lock);
  249. /* Get the pointer to local buffer to store the logs */
  250. log_buf_snapshot_state = dst_data = guc_get_write_buffer(guc);
  251. if (unlikely(!log_buf_snapshot_state)) {
  252. /*
  253. * Used rate limited to avoid deluge of messages, logs might be
  254. * getting consumed by User at a slow rate.
  255. */
  256. DRM_ERROR_RATELIMITED("no sub-buffer to capture logs\n");
  257. guc->log.capture_miss_count++;
  258. mutex_unlock(&guc->log.runtime.relay_lock);
  259. return;
  260. }
  261. /* Actual logs are present from the 2nd page */
  262. src_data += PAGE_SIZE;
  263. dst_data += PAGE_SIZE;
  264. for (type = GUC_ISR_LOG_BUFFER; type < GUC_MAX_LOG_BUFFER; type++) {
  265. /*
  266. * Make a copy of the state structure, inside GuC log buffer
  267. * (which is uncached mapped), on the stack to avoid reading
  268. * from it multiple times.
  269. */
  270. memcpy(&log_buf_state_local, log_buf_state,
  271. sizeof(struct guc_log_buffer_state));
  272. buffer_size = guc_get_log_buffer_size(type);
  273. read_offset = log_buf_state_local.read_ptr;
  274. write_offset = log_buf_state_local.sampled_write_ptr;
  275. full_cnt = log_buf_state_local.buffer_full_cnt;
  276. /* Bookkeeping stuff */
  277. guc->log.flush_count[type] += log_buf_state_local.flush_to_file;
  278. new_overflow = guc_check_log_buf_overflow(guc, type, full_cnt);
  279. /* Update the state of shared log buffer */
  280. log_buf_state->read_ptr = write_offset;
  281. log_buf_state->flush_to_file = 0;
  282. log_buf_state++;
  283. /* First copy the state structure in snapshot buffer */
  284. memcpy(log_buf_snapshot_state, &log_buf_state_local,
  285. sizeof(struct guc_log_buffer_state));
  286. /*
  287. * The write pointer could have been updated by GuC firmware,
  288. * after sending the flush interrupt to Host, for consistency
  289. * set write pointer value to same value of sampled_write_ptr
  290. * in the snapshot buffer.
  291. */
  292. log_buf_snapshot_state->write_ptr = write_offset;
  293. log_buf_snapshot_state++;
  294. /* Now copy the actual logs. */
  295. if (unlikely(new_overflow)) {
  296. /* copy the whole buffer in case of overflow */
  297. read_offset = 0;
  298. write_offset = buffer_size;
  299. } else if (unlikely((read_offset > buffer_size) ||
  300. (write_offset > buffer_size))) {
  301. DRM_ERROR("invalid log buffer state\n");
  302. /* copy whole buffer as offsets are unreliable */
  303. read_offset = 0;
  304. write_offset = buffer_size;
  305. }
  306. /* Just copy the newly written data */
  307. if (read_offset > write_offset) {
  308. i915_memcpy_from_wc(dst_data, src_data, write_offset);
  309. bytes_to_copy = buffer_size - read_offset;
  310. } else {
  311. bytes_to_copy = write_offset - read_offset;
  312. }
  313. i915_memcpy_from_wc(dst_data + read_offset,
  314. src_data + read_offset, bytes_to_copy);
  315. src_data += buffer_size;
  316. dst_data += buffer_size;
  317. }
  318. guc_move_to_next_buf(guc);
  319. mutex_unlock(&guc->log.runtime.relay_lock);
  320. }
  321. static void capture_logs_work(struct work_struct *work)
  322. {
  323. struct intel_guc *guc =
  324. container_of(work, struct intel_guc, log.runtime.flush_work);
  325. guc_log_capture_logs(guc);
  326. }
  327. static bool guc_log_has_runtime(struct intel_guc *guc)
  328. {
  329. return guc->log.runtime.buf_addr != NULL;
  330. }
  331. static int guc_log_runtime_create(struct intel_guc *guc)
  332. {
  333. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  334. void *vaddr;
  335. int ret;
  336. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  337. if (!guc->log.vma)
  338. return -ENODEV;
  339. GEM_BUG_ON(guc_log_has_runtime(guc));
  340. ret = i915_gem_object_set_to_wc_domain(guc->log.vma->obj, true);
  341. if (ret)
  342. return ret;
  343. /*
  344. * Create a WC (Uncached for read) vmalloc mapping of log
  345. * buffer pages, so that we can directly get the data
  346. * (up-to-date) from memory.
  347. */
  348. vaddr = i915_gem_object_pin_map(guc->log.vma->obj, I915_MAP_WC);
  349. if (IS_ERR(vaddr)) {
  350. DRM_ERROR("Couldn't map log buffer pages %d\n", ret);
  351. return PTR_ERR(vaddr);
  352. }
  353. guc->log.runtime.buf_addr = vaddr;
  354. return 0;
  355. }
  356. static void guc_log_runtime_destroy(struct intel_guc *guc)
  357. {
  358. /*
  359. * It's possible that the runtime stuff was never allocated because
  360. * GuC log was disabled at the boot time.
  361. */
  362. if (!guc_log_has_runtime(guc))
  363. return;
  364. i915_gem_object_unpin_map(guc->log.vma->obj);
  365. guc->log.runtime.buf_addr = NULL;
  366. }
  367. void intel_guc_log_init_early(struct intel_guc *guc)
  368. {
  369. mutex_init(&guc->log.runtime.relay_lock);
  370. INIT_WORK(&guc->log.runtime.flush_work, capture_logs_work);
  371. }
  372. int intel_guc_log_relay_create(struct intel_guc *guc)
  373. {
  374. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  375. struct rchan *guc_log_relay_chan;
  376. size_t n_subbufs, subbuf_size;
  377. int ret;
  378. if (!i915_modparams.guc_log_level)
  379. return 0;
  380. mutex_lock(&guc->log.runtime.relay_lock);
  381. GEM_BUG_ON(guc_log_has_relay(guc));
  382. /* Keep the size of sub buffers same as shared log buffer */
  383. subbuf_size = GUC_LOG_SIZE;
  384. /*
  385. * Store up to 8 snapshots, which is large enough to buffer sufficient
  386. * boot time logs and provides enough leeway to User, in terms of
  387. * latency, for consuming the logs from relay. Also doesn't take
  388. * up too much memory.
  389. */
  390. n_subbufs = 8;
  391. /*
  392. * Create a relay channel, so that we have buffers for storing
  393. * the GuC firmware logs, the channel will be linked with a file
  394. * later on when debugfs is registered.
  395. */
  396. guc_log_relay_chan = relay_open(NULL, NULL, subbuf_size,
  397. n_subbufs, &relay_callbacks, dev_priv);
  398. if (!guc_log_relay_chan) {
  399. DRM_ERROR("Couldn't create relay chan for GuC logging\n");
  400. ret = -ENOMEM;
  401. goto err;
  402. }
  403. GEM_BUG_ON(guc_log_relay_chan->subbuf_size < subbuf_size);
  404. guc->log.runtime.relay_chan = guc_log_relay_chan;
  405. mutex_unlock(&guc->log.runtime.relay_lock);
  406. return 0;
  407. err:
  408. mutex_unlock(&guc->log.runtime.relay_lock);
  409. /* logging will be off */
  410. i915_modparams.guc_log_level = 0;
  411. return ret;
  412. }
  413. void intel_guc_log_relay_destroy(struct intel_guc *guc)
  414. {
  415. mutex_lock(&guc->log.runtime.relay_lock);
  416. /*
  417. * It's possible that the relay was never allocated because
  418. * GuC log was disabled at the boot time.
  419. */
  420. if (!guc_log_has_relay(guc))
  421. goto out_unlock;
  422. relay_close(guc->log.runtime.relay_chan);
  423. guc->log.runtime.relay_chan = NULL;
  424. out_unlock:
  425. mutex_unlock(&guc->log.runtime.relay_lock);
  426. }
  427. static int guc_log_late_setup(struct intel_guc *guc)
  428. {
  429. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  430. int ret;
  431. if (!guc_log_has_runtime(guc)) {
  432. /*
  433. * If log was disabled at boot time, then setup needed to handle
  434. * log buffer flush interrupts would not have been done yet, so
  435. * do that now.
  436. */
  437. ret = intel_guc_log_relay_create(guc);
  438. if (ret)
  439. goto err;
  440. mutex_lock(&dev_priv->drm.struct_mutex);
  441. intel_runtime_pm_get(dev_priv);
  442. ret = guc_log_runtime_create(guc);
  443. intel_runtime_pm_put(dev_priv);
  444. mutex_unlock(&dev_priv->drm.struct_mutex);
  445. if (ret)
  446. goto err_relay;
  447. }
  448. ret = guc_log_relay_file_create(guc);
  449. if (ret)
  450. goto err_runtime;
  451. return 0;
  452. err_runtime:
  453. mutex_lock(&dev_priv->drm.struct_mutex);
  454. guc_log_runtime_destroy(guc);
  455. mutex_unlock(&dev_priv->drm.struct_mutex);
  456. err_relay:
  457. intel_guc_log_relay_destroy(guc);
  458. err:
  459. /* logging will remain off */
  460. i915_modparams.guc_log_level = 0;
  461. return ret;
  462. }
  463. static void guc_log_capture_logs(struct intel_guc *guc)
  464. {
  465. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  466. guc_read_update_log_buffer(guc);
  467. /*
  468. * Generally device is expected to be active only at this
  469. * time, so get/put should be really quick.
  470. */
  471. intel_runtime_pm_get(dev_priv);
  472. guc_log_flush_complete(guc);
  473. intel_runtime_pm_put(dev_priv);
  474. }
  475. static void guc_flush_logs(struct intel_guc *guc)
  476. {
  477. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  478. if (!USES_GUC_SUBMISSION(dev_priv) || !i915_modparams.guc_log_level)
  479. return;
  480. /* First disable the interrupts, will be renabled afterwards */
  481. mutex_lock(&dev_priv->drm.struct_mutex);
  482. intel_runtime_pm_get(dev_priv);
  483. gen9_disable_guc_interrupts(dev_priv);
  484. intel_runtime_pm_put(dev_priv);
  485. mutex_unlock(&dev_priv->drm.struct_mutex);
  486. /*
  487. * Before initiating the forceful flush, wait for any pending/ongoing
  488. * flush to complete otherwise forceful flush may not actually happen.
  489. */
  490. flush_work(&guc->log.runtime.flush_work);
  491. /* Ask GuC to update the log buffer state */
  492. intel_runtime_pm_get(dev_priv);
  493. guc_log_flush(guc);
  494. intel_runtime_pm_put(dev_priv);
  495. /* GuC would have updated log buffer by now, so capture it */
  496. guc_log_capture_logs(guc);
  497. }
  498. int intel_guc_log_create(struct intel_guc *guc)
  499. {
  500. struct i915_vma *vma;
  501. unsigned long offset;
  502. u32 flags;
  503. int ret;
  504. GEM_BUG_ON(guc->log.vma);
  505. /*
  506. * We require SSE 4.1 for fast reads from the GuC log buffer and
  507. * it should be present on the chipsets supporting GuC based
  508. * submisssions.
  509. */
  510. if (WARN_ON(!i915_has_memcpy_from_wc())) {
  511. ret = -EINVAL;
  512. goto err;
  513. }
  514. vma = intel_guc_allocate_vma(guc, GUC_LOG_SIZE);
  515. if (IS_ERR(vma)) {
  516. ret = PTR_ERR(vma);
  517. goto err;
  518. }
  519. guc->log.vma = vma;
  520. if (i915_modparams.guc_log_level) {
  521. ret = guc_log_runtime_create(guc);
  522. if (ret < 0)
  523. goto err_vma;
  524. }
  525. /* each allocated unit is a page */
  526. flags = GUC_LOG_VALID | GUC_LOG_NOTIFY_ON_HALF_FULL |
  527. (GUC_LOG_DPC_PAGES << GUC_LOG_DPC_SHIFT) |
  528. (GUC_LOG_ISR_PAGES << GUC_LOG_ISR_SHIFT) |
  529. (GUC_LOG_CRASH_PAGES << GUC_LOG_CRASH_SHIFT);
  530. offset = guc_ggtt_offset(vma) >> PAGE_SHIFT; /* in pages */
  531. guc->log.flags = (offset << GUC_LOG_BUF_ADDR_SHIFT) | flags;
  532. return 0;
  533. err_vma:
  534. i915_vma_unpin_and_release(&guc->log.vma);
  535. err:
  536. /* logging will be off */
  537. i915_modparams.guc_log_level = 0;
  538. return ret;
  539. }
  540. void intel_guc_log_destroy(struct intel_guc *guc)
  541. {
  542. guc_log_runtime_destroy(guc);
  543. i915_vma_unpin_and_release(&guc->log.vma);
  544. }
  545. int intel_guc_log_control(struct intel_guc *guc, u64 control_val)
  546. {
  547. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  548. bool enable_logging = control_val > 0;
  549. u32 verbosity;
  550. int ret;
  551. if (!guc->log.vma)
  552. return -ENODEV;
  553. BUILD_BUG_ON(GUC_LOG_VERBOSITY_MIN);
  554. if (control_val > 1 + GUC_LOG_VERBOSITY_MAX)
  555. return -EINVAL;
  556. /* This combination doesn't make sense & won't have any effect */
  557. if (!enable_logging && !i915_modparams.guc_log_level)
  558. return 0;
  559. verbosity = enable_logging ? control_val - 1 : 0;
  560. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  561. if (ret)
  562. return ret;
  563. intel_runtime_pm_get(dev_priv);
  564. ret = guc_log_control(guc, enable_logging, verbosity);
  565. intel_runtime_pm_put(dev_priv);
  566. mutex_unlock(&dev_priv->drm.struct_mutex);
  567. if (ret < 0) {
  568. DRM_DEBUG_DRIVER("guc_logging_control action failed %d\n", ret);
  569. return ret;
  570. }
  571. if (enable_logging) {
  572. i915_modparams.guc_log_level = 1 + verbosity;
  573. /*
  574. * If log was disabled at boot time, then the relay channel file
  575. * wouldn't have been created by now and interrupts also would
  576. * not have been enabled. Try again now, just in case.
  577. */
  578. ret = guc_log_late_setup(guc);
  579. if (ret < 0) {
  580. DRM_DEBUG_DRIVER("GuC log late setup failed %d\n", ret);
  581. return ret;
  582. }
  583. /* GuC logging is currently the only user of Guc2Host interrupts */
  584. mutex_lock(&dev_priv->drm.struct_mutex);
  585. intel_runtime_pm_get(dev_priv);
  586. gen9_enable_guc_interrupts(dev_priv);
  587. intel_runtime_pm_put(dev_priv);
  588. mutex_unlock(&dev_priv->drm.struct_mutex);
  589. } else {
  590. /*
  591. * Once logging is disabled, GuC won't generate logs & send an
  592. * interrupt. But there could be some data in the log buffer
  593. * which is yet to be captured. So request GuC to update the log
  594. * buffer state and then collect the left over logs.
  595. */
  596. guc_flush_logs(guc);
  597. /* As logging is disabled, update log level to reflect that */
  598. i915_modparams.guc_log_level = 0;
  599. }
  600. return ret;
  601. }
  602. void i915_guc_log_register(struct drm_i915_private *dev_priv)
  603. {
  604. if (!USES_GUC_SUBMISSION(dev_priv) || !i915_modparams.guc_log_level)
  605. return;
  606. guc_log_late_setup(&dev_priv->guc);
  607. }
  608. void i915_guc_log_unregister(struct drm_i915_private *dev_priv)
  609. {
  610. struct intel_guc *guc = &dev_priv->guc;
  611. if (!USES_GUC_SUBMISSION(dev_priv))
  612. return;
  613. mutex_lock(&dev_priv->drm.struct_mutex);
  614. /* GuC logging is currently the only user of Guc2Host interrupts */
  615. intel_runtime_pm_get(dev_priv);
  616. gen9_disable_guc_interrupts(dev_priv);
  617. intel_runtime_pm_put(dev_priv);
  618. guc_log_runtime_destroy(guc);
  619. mutex_unlock(&dev_priv->drm.struct_mutex);
  620. intel_guc_log_relay_destroy(guc);
  621. }