drm_panic.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* SPDX-License-Identifier: GPL-2.0 or MIT */
  2. /*
  3. * Copyright (c) 2024 Intel
  4. * Copyright (c) 2024 Red Hat
  5. */
  6. #ifndef __DRM_PANIC_H__
  7. #define __DRM_PANIC_H__
  8. #include <linux/module.h>
  9. #include <linux/types.h>
  10. #include <linux/iosys-map.h>
  11. #include <drm/drm_device.h>
  12. #include <drm/drm_fourcc.h>
  13. /**
  14. * struct drm_scanout_buffer - DRM scanout buffer
  15. *
  16. * This structure holds the information necessary for drm_panic to draw the
  17. * panic screen, and display it.
  18. */
  19. struct drm_scanout_buffer {
  20. /**
  21. * @format:
  22. *
  23. * drm format of the scanout buffer.
  24. */
  25. const struct drm_format_info *format;
  26. /**
  27. * @map:
  28. *
  29. * Virtual address of the scanout buffer, either in memory or iomem.
  30. * The scanout buffer should be in linear format, and can be directly
  31. * sent to the display hardware. Tearing is not an issue for the panic
  32. * screen.
  33. */
  34. struct iosys_map map[DRM_FORMAT_MAX_PLANES];
  35. /**
  36. * @pages: Optional, if the scanout buffer is not mapped, set this field
  37. * to the array of pages of the scanout buffer. The panic code will use
  38. * kmap_local_page_try_from_panic() to map one page at a time to write
  39. * all the pixels. This array shouldn't be allocated from the
  40. * get_scanoutbuffer() callback.
  41. * The scanout buffer should be in linear format.
  42. */
  43. struct page **pages;
  44. /**
  45. * @width: Width of the scanout buffer, in pixels.
  46. */
  47. unsigned int width;
  48. /**
  49. * @height: Height of the scanout buffer, in pixels.
  50. */
  51. unsigned int height;
  52. /**
  53. * @pitch: Length in bytes between the start of two consecutive lines.
  54. */
  55. unsigned int pitch[DRM_FORMAT_MAX_PLANES];
  56. /**
  57. * @set_pixel: Optional function, to set a pixel color on the
  58. * framebuffer. It allows to handle special tiling format inside the
  59. * driver. It takes precedence over the @map and @pages fields.
  60. */
  61. void (*set_pixel)(struct drm_scanout_buffer *sb, unsigned int x,
  62. unsigned int y, u32 color);
  63. /**
  64. * @private: private pointer that you can use in the callbacks
  65. * set_pixel()
  66. */
  67. void *private;
  68. };
  69. #ifdef CONFIG_DRM_PANIC
  70. /**
  71. * drm_panic_trylock - try to enter the panic printing critical section
  72. * @dev: struct drm_device
  73. * @flags: unsigned long irq flags you need to pass to the unlock() counterpart
  74. *
  75. * This function must be called by any panic printing code. The panic printing
  76. * attempt must be aborted if the trylock fails.
  77. *
  78. * Panic printing code can make the following assumptions while holding the
  79. * panic lock:
  80. *
  81. * - Anything protected by drm_panic_lock() and drm_panic_unlock() pairs is safe
  82. * to access.
  83. *
  84. * - Furthermore the panic printing code only registers in drm_dev_unregister()
  85. * and gets removed in drm_dev_unregister(). This allows the panic code to
  86. * safely access any state which is invariant in between these two function
  87. * calls, like the list of planes &drm_mode_config.plane_list or most of the
  88. * struct drm_plane structure.
  89. *
  90. * Specifically thanks to the protection around plane updates in
  91. * drm_atomic_helper_swap_state() the following additional guarantees hold:
  92. *
  93. * - It is safe to deference the drm_plane.state pointer.
  94. *
  95. * - Anything in struct drm_plane_state or the driver's subclass thereof which
  96. * stays invariant after the atomic check code has finished is safe to access.
  97. * Specifically this includes the reference counted pointers to framebuffer
  98. * and buffer objects.
  99. *
  100. * - Anything set up by &drm_plane_helper_funcs.fb_prepare and cleaned up
  101. * &drm_plane_helper_funcs.fb_cleanup is safe to access, as long as it stays
  102. * invariant between these two calls. This also means that for drivers using
  103. * dynamic buffer management the framebuffer is pinned, and therefer all
  104. * relevant datastructures can be accessed without taking any further locks
  105. * (which would be impossible in panic context anyway).
  106. *
  107. * - Importantly, software and hardware state set up by
  108. * &drm_plane_helper_funcs.begin_fb_access and
  109. * &drm_plane_helper_funcs.end_fb_access is not safe to access.
  110. *
  111. * Drivers must not make any assumptions about the actual state of the hardware,
  112. * unless they explicitly protected these hardware access with drm_panic_lock()
  113. * and drm_panic_unlock().
  114. *
  115. * Return:
  116. * %0 when failing to acquire the raw spinlock, nonzero on success.
  117. */
  118. #define drm_panic_trylock(dev, flags) \
  119. raw_spin_trylock_irqsave(&(dev)->mode_config.panic_lock, flags)
  120. /**
  121. * drm_panic_lock - protect panic printing relevant state
  122. * @dev: struct drm_device
  123. * @flags: unsigned long irq flags you need to pass to the unlock() counterpart
  124. *
  125. * This function must be called to protect software and hardware state that the
  126. * panic printing code must be able to rely on. The protected sections must be
  127. * as small as possible. It uses the irqsave/irqrestore variant, and can be
  128. * called from irq handler. Examples include:
  129. *
  130. * - Access to peek/poke or other similar registers, if that is the way the
  131. * driver prints the pixels into the scanout buffer at panic time.
  132. *
  133. * - Updates to pointers like &drm_plane.state, allowing the panic handler to
  134. * safely deference these. This is done in drm_atomic_helper_swap_state().
  135. *
  136. * - An state that isn't invariant and that the driver must be able to access
  137. * during panic printing.
  138. */
  139. #define drm_panic_lock(dev, flags) \
  140. raw_spin_lock_irqsave(&(dev)->mode_config.panic_lock, flags)
  141. /**
  142. * drm_panic_unlock - end of the panic printing critical section
  143. * @dev: struct drm_device
  144. * @flags: irq flags that were returned when acquiring the lock
  145. *
  146. * Unlocks the raw spinlock acquired by either drm_panic_lock() or
  147. * drm_panic_trylock().
  148. */
  149. #define drm_panic_unlock(dev, flags) \
  150. raw_spin_unlock_irqrestore(&(dev)->mode_config.panic_lock, flags)
  151. #else
  152. static inline bool drm_panic_trylock(struct drm_device *dev, unsigned long flags)
  153. {
  154. return true;
  155. }
  156. static inline void drm_panic_lock(struct drm_device *dev, unsigned long flags) {}
  157. static inline void drm_panic_unlock(struct drm_device *dev, unsigned long flags) {}
  158. #endif
  159. #if defined(CONFIG_DRM_PANIC_SCREEN_QR_CODE)
  160. size_t drm_panic_qr_max_data_size(u8 version, size_t url_len);
  161. u8 drm_panic_qr_generate(const char *url, u8 *data, size_t data_len, size_t data_size,
  162. u8 *tmp, size_t tmp_size);
  163. #endif
  164. #endif /* __DRM_PANIC_H__ */