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