intel_guc_log.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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 "intel_guc_log.h"
  26. #include "i915_drv.h"
  27. static void guc_log_capture_logs(struct intel_guc_log *log);
  28. /**
  29. * DOC: GuC firmware log
  30. *
  31. * Firmware log is enabled by setting i915.guc_log_level to the positive level.
  32. * Log data is printed out via reading debugfs i915_guc_log_dump. Reading from
  33. * i915_guc_load_status will print out firmware loading status and scratch
  34. * registers value.
  35. */
  36. static int guc_action_flush_log_complete(struct intel_guc *guc)
  37. {
  38. u32 action[] = {
  39. INTEL_GUC_ACTION_LOG_BUFFER_FILE_FLUSH_COMPLETE
  40. };
  41. return intel_guc_send(guc, action, ARRAY_SIZE(action));
  42. }
  43. static int guc_action_flush_log(struct intel_guc *guc)
  44. {
  45. u32 action[] = {
  46. INTEL_GUC_ACTION_FORCE_LOG_BUFFER_FLUSH,
  47. 0
  48. };
  49. return intel_guc_send(guc, action, ARRAY_SIZE(action));
  50. }
  51. static int guc_action_control_log(struct intel_guc *guc, bool enable,
  52. bool default_logging, u32 verbosity)
  53. {
  54. u32 action[] = {
  55. INTEL_GUC_ACTION_UK_LOG_ENABLE_LOGGING,
  56. (enable ? GUC_LOG_CONTROL_LOGGING_ENABLED : 0) |
  57. (verbosity << GUC_LOG_CONTROL_VERBOSITY_SHIFT) |
  58. (default_logging ? GUC_LOG_CONTROL_DEFAULT_LOGGING : 0)
  59. };
  60. GEM_BUG_ON(verbosity > GUC_LOG_VERBOSITY_MAX);
  61. return intel_guc_send(guc, action, ARRAY_SIZE(action));
  62. }
  63. static inline struct intel_guc *log_to_guc(struct intel_guc_log *log)
  64. {
  65. return container_of(log, struct intel_guc, log);
  66. }
  67. static void guc_log_enable_flush_events(struct intel_guc_log *log)
  68. {
  69. intel_guc_enable_msg(log_to_guc(log),
  70. INTEL_GUC_RECV_MSG_FLUSH_LOG_BUFFER |
  71. INTEL_GUC_RECV_MSG_CRASH_DUMP_POSTED);
  72. }
  73. static void guc_log_disable_flush_events(struct intel_guc_log *log)
  74. {
  75. intel_guc_disable_msg(log_to_guc(log),
  76. INTEL_GUC_RECV_MSG_FLUSH_LOG_BUFFER |
  77. INTEL_GUC_RECV_MSG_CRASH_DUMP_POSTED);
  78. }
  79. /*
  80. * Sub buffer switch callback. Called whenever relay has to switch to a new
  81. * sub buffer, relay stays on the same sub buffer if 0 is returned.
  82. */
  83. static int subbuf_start_callback(struct rchan_buf *buf,
  84. void *subbuf,
  85. void *prev_subbuf,
  86. size_t prev_padding)
  87. {
  88. /*
  89. * Use no-overwrite mode by default, where relay will stop accepting
  90. * new data if there are no empty sub buffers left.
  91. * There is no strict synchronization enforced by relay between Consumer
  92. * and Producer. In overwrite mode, there is a possibility of getting
  93. * inconsistent/garbled data, the producer could be writing on to the
  94. * same sub buffer from which Consumer is reading. This can't be avoided
  95. * unless Consumer is fast enough and can always run in tandem with
  96. * Producer.
  97. */
  98. if (relay_buf_full(buf))
  99. return 0;
  100. return 1;
  101. }
  102. /*
  103. * file_create() callback. Creates relay file in debugfs.
  104. */
  105. static struct dentry *create_buf_file_callback(const char *filename,
  106. struct dentry *parent,
  107. umode_t mode,
  108. struct rchan_buf *buf,
  109. int *is_global)
  110. {
  111. struct dentry *buf_file;
  112. /*
  113. * This to enable the use of a single buffer for the relay channel and
  114. * correspondingly have a single file exposed to User, through which
  115. * it can collect the logs in order without any post-processing.
  116. * Need to set 'is_global' even if parent is NULL for early logging.
  117. */
  118. *is_global = 1;
  119. if (!parent)
  120. return NULL;
  121. buf_file = debugfs_create_file(filename, mode,
  122. parent, buf, &relay_file_operations);
  123. return buf_file;
  124. }
  125. /*
  126. * file_remove() default callback. Removes relay file in debugfs.
  127. */
  128. static int remove_buf_file_callback(struct dentry *dentry)
  129. {
  130. debugfs_remove(dentry);
  131. return 0;
  132. }
  133. /* relay channel callbacks */
  134. static struct rchan_callbacks relay_callbacks = {
  135. .subbuf_start = subbuf_start_callback,
  136. .create_buf_file = create_buf_file_callback,
  137. .remove_buf_file = remove_buf_file_callback,
  138. };
  139. static void guc_move_to_next_buf(struct intel_guc_log *log)
  140. {
  141. /*
  142. * Make sure the updates made in the sub buffer are visible when
  143. * Consumer sees the following update to offset inside the sub buffer.
  144. */
  145. smp_wmb();
  146. /* All data has been written, so now move the offset of sub buffer. */
  147. relay_reserve(log->relay.channel, log->vma->obj->base.size);
  148. /* Switch to the next sub buffer */
  149. relay_flush(log->relay.channel);
  150. }
  151. static void *guc_get_write_buffer(struct intel_guc_log *log)
  152. {
  153. /*
  154. * Just get the base address of a new sub buffer and copy data into it
  155. * ourselves. NULL will be returned in no-overwrite mode, if all sub
  156. * buffers are full. Could have used the relay_write() to indirectly
  157. * copy the data, but that would have been bit convoluted, as we need to
  158. * write to only certain locations inside a sub buffer which cannot be
  159. * done without using relay_reserve() along with relay_write(). So its
  160. * better to use relay_reserve() alone.
  161. */
  162. return relay_reserve(log->relay.channel, 0);
  163. }
  164. static bool guc_check_log_buf_overflow(struct intel_guc_log *log,
  165. enum guc_log_buffer_type type,
  166. unsigned int full_cnt)
  167. {
  168. unsigned int prev_full_cnt = log->stats[type].sampled_overflow;
  169. bool overflow = false;
  170. if (full_cnt != prev_full_cnt) {
  171. overflow = true;
  172. log->stats[type].overflow = full_cnt;
  173. log->stats[type].sampled_overflow += full_cnt - prev_full_cnt;
  174. if (full_cnt < prev_full_cnt) {
  175. /* buffer_full_cnt is a 4 bit counter */
  176. log->stats[type].sampled_overflow += 16;
  177. }
  178. DRM_ERROR_RATELIMITED("GuC log buffer overflow\n");
  179. }
  180. return overflow;
  181. }
  182. static unsigned int guc_get_log_buffer_size(enum guc_log_buffer_type type)
  183. {
  184. switch (type) {
  185. case GUC_ISR_LOG_BUFFER:
  186. return ISR_BUFFER_SIZE;
  187. case GUC_DPC_LOG_BUFFER:
  188. return DPC_BUFFER_SIZE;
  189. case GUC_CRASH_DUMP_LOG_BUFFER:
  190. return CRASH_BUFFER_SIZE;
  191. default:
  192. MISSING_CASE(type);
  193. }
  194. return 0;
  195. }
  196. static void guc_read_update_log_buffer(struct intel_guc_log *log)
  197. {
  198. unsigned int buffer_size, read_offset, write_offset, bytes_to_copy, full_cnt;
  199. struct guc_log_buffer_state *log_buf_state, *log_buf_snapshot_state;
  200. struct guc_log_buffer_state log_buf_state_local;
  201. enum guc_log_buffer_type type;
  202. void *src_data, *dst_data;
  203. bool new_overflow;
  204. mutex_lock(&log->relay.lock);
  205. if (WARN_ON(!intel_guc_log_relay_enabled(log)))
  206. goto out_unlock;
  207. /* Get the pointer to shared GuC log buffer */
  208. log_buf_state = src_data = log->relay.buf_addr;
  209. /* Get the pointer to local buffer to store the logs */
  210. log_buf_snapshot_state = dst_data = guc_get_write_buffer(log);
  211. if (unlikely(!log_buf_snapshot_state)) {
  212. /*
  213. * Used rate limited to avoid deluge of messages, logs might be
  214. * getting consumed by User at a slow rate.
  215. */
  216. DRM_ERROR_RATELIMITED("no sub-buffer to capture logs\n");
  217. log->relay.full_count++;
  218. goto out_unlock;
  219. }
  220. /* Actual logs are present from the 2nd page */
  221. src_data += PAGE_SIZE;
  222. dst_data += PAGE_SIZE;
  223. for (type = GUC_ISR_LOG_BUFFER; type < GUC_MAX_LOG_BUFFER; type++) {
  224. /*
  225. * Make a copy of the state structure, inside GuC log buffer
  226. * (which is uncached mapped), on the stack to avoid reading
  227. * from it multiple times.
  228. */
  229. memcpy(&log_buf_state_local, log_buf_state,
  230. sizeof(struct guc_log_buffer_state));
  231. buffer_size = guc_get_log_buffer_size(type);
  232. read_offset = log_buf_state_local.read_ptr;
  233. write_offset = log_buf_state_local.sampled_write_ptr;
  234. full_cnt = log_buf_state_local.buffer_full_cnt;
  235. /* Bookkeeping stuff */
  236. log->stats[type].flush += log_buf_state_local.flush_to_file;
  237. new_overflow = guc_check_log_buf_overflow(log, type, full_cnt);
  238. /* Update the state of shared log buffer */
  239. log_buf_state->read_ptr = write_offset;
  240. log_buf_state->flush_to_file = 0;
  241. log_buf_state++;
  242. /* First copy the state structure in snapshot buffer */
  243. memcpy(log_buf_snapshot_state, &log_buf_state_local,
  244. sizeof(struct guc_log_buffer_state));
  245. /*
  246. * The write pointer could have been updated by GuC firmware,
  247. * after sending the flush interrupt to Host, for consistency
  248. * set write pointer value to same value of sampled_write_ptr
  249. * in the snapshot buffer.
  250. */
  251. log_buf_snapshot_state->write_ptr = write_offset;
  252. log_buf_snapshot_state++;
  253. /* Now copy the actual logs. */
  254. if (unlikely(new_overflow)) {
  255. /* copy the whole buffer in case of overflow */
  256. read_offset = 0;
  257. write_offset = buffer_size;
  258. } else if (unlikely((read_offset > buffer_size) ||
  259. (write_offset > buffer_size))) {
  260. DRM_ERROR("invalid log buffer state\n");
  261. /* copy whole buffer as offsets are unreliable */
  262. read_offset = 0;
  263. write_offset = buffer_size;
  264. }
  265. /* Just copy the newly written data */
  266. if (read_offset > write_offset) {
  267. i915_memcpy_from_wc(dst_data, src_data, write_offset);
  268. bytes_to_copy = buffer_size - read_offset;
  269. } else {
  270. bytes_to_copy = write_offset - read_offset;
  271. }
  272. i915_memcpy_from_wc(dst_data + read_offset,
  273. src_data + read_offset, bytes_to_copy);
  274. src_data += buffer_size;
  275. dst_data += buffer_size;
  276. }
  277. guc_move_to_next_buf(log);
  278. out_unlock:
  279. mutex_unlock(&log->relay.lock);
  280. }
  281. static void capture_logs_work(struct work_struct *work)
  282. {
  283. struct intel_guc_log *log =
  284. container_of(work, struct intel_guc_log, relay.flush_work);
  285. guc_log_capture_logs(log);
  286. }
  287. static int guc_log_map(struct intel_guc_log *log)
  288. {
  289. struct intel_guc *guc = log_to_guc(log);
  290. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  291. void *vaddr;
  292. int ret;
  293. lockdep_assert_held(&log->relay.lock);
  294. if (!log->vma)
  295. return -ENODEV;
  296. mutex_lock(&dev_priv->drm.struct_mutex);
  297. ret = i915_gem_object_set_to_wc_domain(log->vma->obj, true);
  298. mutex_unlock(&dev_priv->drm.struct_mutex);
  299. if (ret)
  300. return ret;
  301. /*
  302. * Create a WC (Uncached for read) vmalloc mapping of log
  303. * buffer pages, so that we can directly get the data
  304. * (up-to-date) from memory.
  305. */
  306. vaddr = i915_gem_object_pin_map(log->vma->obj, I915_MAP_WC);
  307. if (IS_ERR(vaddr)) {
  308. DRM_ERROR("Couldn't map log buffer pages %d\n", ret);
  309. return PTR_ERR(vaddr);
  310. }
  311. log->relay.buf_addr = vaddr;
  312. return 0;
  313. }
  314. static void guc_log_unmap(struct intel_guc_log *log)
  315. {
  316. lockdep_assert_held(&log->relay.lock);
  317. i915_gem_object_unpin_map(log->vma->obj);
  318. log->relay.buf_addr = NULL;
  319. }
  320. void intel_guc_log_init_early(struct intel_guc_log *log)
  321. {
  322. mutex_init(&log->relay.lock);
  323. INIT_WORK(&log->relay.flush_work, capture_logs_work);
  324. }
  325. static int guc_log_relay_create(struct intel_guc_log *log)
  326. {
  327. struct intel_guc *guc = log_to_guc(log);
  328. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  329. struct rchan *guc_log_relay_chan;
  330. size_t n_subbufs, subbuf_size;
  331. int ret;
  332. lockdep_assert_held(&log->relay.lock);
  333. /* Keep the size of sub buffers same as shared log buffer */
  334. subbuf_size = log->vma->size;
  335. /*
  336. * Store up to 8 snapshots, which is large enough to buffer sufficient
  337. * boot time logs and provides enough leeway to User, in terms of
  338. * latency, for consuming the logs from relay. Also doesn't take
  339. * up too much memory.
  340. */
  341. n_subbufs = 8;
  342. guc_log_relay_chan = relay_open("guc_log",
  343. dev_priv->drm.primary->debugfs_root,
  344. subbuf_size, n_subbufs,
  345. &relay_callbacks, dev_priv);
  346. if (!guc_log_relay_chan) {
  347. DRM_ERROR("Couldn't create relay chan for GuC logging\n");
  348. ret = -ENOMEM;
  349. return ret;
  350. }
  351. GEM_BUG_ON(guc_log_relay_chan->subbuf_size < subbuf_size);
  352. log->relay.channel = guc_log_relay_chan;
  353. return 0;
  354. }
  355. static void guc_log_relay_destroy(struct intel_guc_log *log)
  356. {
  357. lockdep_assert_held(&log->relay.lock);
  358. relay_close(log->relay.channel);
  359. log->relay.channel = NULL;
  360. }
  361. static void guc_log_capture_logs(struct intel_guc_log *log)
  362. {
  363. struct intel_guc *guc = log_to_guc(log);
  364. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  365. guc_read_update_log_buffer(log);
  366. /*
  367. * Generally device is expected to be active only at this
  368. * time, so get/put should be really quick.
  369. */
  370. intel_runtime_pm_get(dev_priv);
  371. guc_action_flush_log_complete(guc);
  372. intel_runtime_pm_put(dev_priv);
  373. }
  374. int intel_guc_log_create(struct intel_guc_log *log)
  375. {
  376. struct intel_guc *guc = log_to_guc(log);
  377. struct i915_vma *vma;
  378. u32 guc_log_size;
  379. int ret;
  380. GEM_BUG_ON(log->vma);
  381. /*
  382. * GuC Log buffer Layout
  383. *
  384. * +===============================+ 00B
  385. * | Crash dump state header |
  386. * +-------------------------------+ 32B
  387. * | DPC state header |
  388. * +-------------------------------+ 64B
  389. * | ISR state header |
  390. * +-------------------------------+ 96B
  391. * | |
  392. * +===============================+ PAGE_SIZE (4KB)
  393. * | Crash Dump logs |
  394. * +===============================+ + CRASH_SIZE
  395. * | DPC logs |
  396. * +===============================+ + DPC_SIZE
  397. * | ISR logs |
  398. * +===============================+ + ISR_SIZE
  399. */
  400. guc_log_size = PAGE_SIZE + CRASH_BUFFER_SIZE + DPC_BUFFER_SIZE +
  401. ISR_BUFFER_SIZE;
  402. vma = intel_guc_allocate_vma(guc, guc_log_size);
  403. if (IS_ERR(vma)) {
  404. ret = PTR_ERR(vma);
  405. goto err;
  406. }
  407. log->vma = vma;
  408. log->level = i915_modparams.guc_log_level;
  409. return 0;
  410. err:
  411. DRM_ERROR("Failed to allocate GuC log buffer. %d\n", ret);
  412. return ret;
  413. }
  414. void intel_guc_log_destroy(struct intel_guc_log *log)
  415. {
  416. i915_vma_unpin_and_release(&log->vma, 0);
  417. }
  418. int intel_guc_log_set_level(struct intel_guc_log *log, u32 level)
  419. {
  420. struct intel_guc *guc = log_to_guc(log);
  421. struct drm_i915_private *dev_priv = guc_to_i915(guc);
  422. int ret;
  423. BUILD_BUG_ON(GUC_LOG_VERBOSITY_MIN != 0);
  424. GEM_BUG_ON(!log->vma);
  425. /*
  426. * GuC is recognizing log levels starting from 0 to max, we're using 0
  427. * as indication that logging should be disabled.
  428. */
  429. if (level < GUC_LOG_LEVEL_DISABLED || level > GUC_LOG_LEVEL_MAX)
  430. return -EINVAL;
  431. mutex_lock(&dev_priv->drm.struct_mutex);
  432. if (log->level == level) {
  433. ret = 0;
  434. goto out_unlock;
  435. }
  436. intel_runtime_pm_get(dev_priv);
  437. ret = guc_action_control_log(guc, GUC_LOG_LEVEL_IS_VERBOSE(level),
  438. GUC_LOG_LEVEL_IS_ENABLED(level),
  439. GUC_LOG_LEVEL_TO_VERBOSITY(level));
  440. intel_runtime_pm_put(dev_priv);
  441. if (ret) {
  442. DRM_DEBUG_DRIVER("guc_log_control action failed %d\n", ret);
  443. goto out_unlock;
  444. }
  445. log->level = level;
  446. out_unlock:
  447. mutex_unlock(&dev_priv->drm.struct_mutex);
  448. return ret;
  449. }
  450. bool intel_guc_log_relay_enabled(const struct intel_guc_log *log)
  451. {
  452. return log->relay.buf_addr;
  453. }
  454. int intel_guc_log_relay_open(struct intel_guc_log *log)
  455. {
  456. int ret;
  457. mutex_lock(&log->relay.lock);
  458. if (intel_guc_log_relay_enabled(log)) {
  459. ret = -EEXIST;
  460. goto out_unlock;
  461. }
  462. /*
  463. * We require SSE 4.1 for fast reads from the GuC log buffer and
  464. * it should be present on the chipsets supporting GuC based
  465. * submisssions.
  466. */
  467. if (!i915_has_memcpy_from_wc()) {
  468. ret = -ENXIO;
  469. goto out_unlock;
  470. }
  471. ret = guc_log_relay_create(log);
  472. if (ret)
  473. goto out_unlock;
  474. ret = guc_log_map(log);
  475. if (ret)
  476. goto out_relay;
  477. mutex_unlock(&log->relay.lock);
  478. guc_log_enable_flush_events(log);
  479. /*
  480. * When GuC is logging without us relaying to userspace, we're ignoring
  481. * the flush notification. This means that we need to unconditionally
  482. * flush on relay enabling, since GuC only notifies us once.
  483. */
  484. queue_work(log->relay.flush_wq, &log->relay.flush_work);
  485. return 0;
  486. out_relay:
  487. guc_log_relay_destroy(log);
  488. out_unlock:
  489. mutex_unlock(&log->relay.lock);
  490. return ret;
  491. }
  492. void intel_guc_log_relay_flush(struct intel_guc_log *log)
  493. {
  494. struct intel_guc *guc = log_to_guc(log);
  495. struct drm_i915_private *i915 = guc_to_i915(guc);
  496. /*
  497. * Before initiating the forceful flush, wait for any pending/ongoing
  498. * flush to complete otherwise forceful flush may not actually happen.
  499. */
  500. flush_work(&log->relay.flush_work);
  501. intel_runtime_pm_get(i915);
  502. guc_action_flush_log(guc);
  503. intel_runtime_pm_put(i915);
  504. /* GuC would have updated log buffer by now, so capture it */
  505. guc_log_capture_logs(log);
  506. }
  507. void intel_guc_log_relay_close(struct intel_guc_log *log)
  508. {
  509. guc_log_disable_flush_events(log);
  510. flush_work(&log->relay.flush_work);
  511. mutex_lock(&log->relay.lock);
  512. GEM_BUG_ON(!intel_guc_log_relay_enabled(log));
  513. guc_log_unmap(log);
  514. guc_log_relay_destroy(log);
  515. mutex_unlock(&log->relay.lock);
  516. }
  517. void intel_guc_log_handle_flush_event(struct intel_guc_log *log)
  518. {
  519. queue_work(log->relay.flush_wq, &log->relay.flush_work);
  520. }