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