|
| 1 | +/* SPDX-License-Identifier: GPL-2.0 or MIT */ |
| 2 | +#ifndef __DRM_PANIC_H__ |
| 3 | +#define __DRM_PANIC_H__ |
| 4 | + |
| 5 | +#include <drm/drm_device.h> |
| 6 | +/* |
| 7 | + * Copyright (c) 2024 Intel |
| 8 | + */ |
| 9 | + |
| 10 | +/** |
| 11 | + * drm_panic_trylock - try to enter the panic printing critical section |
| 12 | + * @dev: struct drm_device |
| 13 | + * @flags: unsigned long irq flags you need to pass to the unlock() counterpart |
| 14 | + * |
| 15 | + * This function must be called by any panic printing code. The panic printing |
| 16 | + * attempt must be aborted if the trylock fails. |
| 17 | + * |
| 18 | + * Panic printing code can make the following assumptions while holding the |
| 19 | + * panic lock: |
| 20 | + * |
| 21 | + * - Anything protected by drm_panic_lock() and drm_panic_unlock() pairs is safe |
| 22 | + * to access. |
| 23 | + * |
| 24 | + * - Furthermore the panic printing code only registers in drm_dev_unregister() |
| 25 | + * and gets removed in drm_dev_unregister(). This allows the panic code to |
| 26 | + * safely access any state which is invariant in between these two function |
| 27 | + * calls, like the list of planes &drm_mode_config.plane_list or most of the |
| 28 | + * struct drm_plane structure. |
| 29 | + * |
| 30 | + * Specifically thanks to the protection around plane updates in |
| 31 | + * drm_atomic_helper_swap_state() the following additional guarantees hold: |
| 32 | + * |
| 33 | + * - It is safe to deference the drm_plane.state pointer. |
| 34 | + * |
| 35 | + * - Anything in struct drm_plane_state or the driver's subclass thereof which |
| 36 | + * stays invariant after the atomic check code has finished is safe to access. |
| 37 | + * Specifically this includes the reference counted pointers to framebuffer |
| 38 | + * and buffer objects. |
| 39 | + * |
| 40 | + * - Anything set up by &drm_plane_helper_funcs.fb_prepare and cleaned up |
| 41 | + * &drm_plane_helper_funcs.fb_cleanup is safe to access, as long as it stays |
| 42 | + * invariant between these two calls. This also means that for drivers using |
| 43 | + * dynamic buffer management the framebuffer is pinned, and therefer all |
| 44 | + * relevant datastructures can be accessed without taking any further locks |
| 45 | + * (which would be impossible in panic context anyway). |
| 46 | + * |
| 47 | + * - Importantly, software and hardware state set up by |
| 48 | + * &drm_plane_helper_funcs.begin_fb_access and |
| 49 | + * &drm_plane_helper_funcs.end_fb_access is not safe to access. |
| 50 | + * |
| 51 | + * Drivers must not make any assumptions about the actual state of the hardware, |
| 52 | + * unless they explicitly protected these hardware access with drm_panic_lock() |
| 53 | + * and drm_panic_unlock(). |
| 54 | + * |
| 55 | + * Return: |
| 56 | + * %0 when failing to acquire the raw spinlock, nonzero on success. |
| 57 | + */ |
| 58 | +#define drm_panic_trylock(dev, flags) \ |
| 59 | + raw_spin_trylock_irqsave(&(dev)->mode_config.panic_lock, flags) |
| 60 | + |
| 61 | +/** |
| 62 | + * drm_panic_lock - protect panic printing relevant state |
| 63 | + * @dev: struct drm_device |
| 64 | + * @flags: unsigned long irq flags you need to pass to the unlock() counterpart |
| 65 | + * |
| 66 | + * This function must be called to protect software and hardware state that the |
| 67 | + * panic printing code must be able to rely on. The protected sections must be |
| 68 | + * as small as possible. It uses the irqsave/irqrestore variant, and can be |
| 69 | + * called from irq handler. Examples include: |
| 70 | + * |
| 71 | + * - Access to peek/poke or other similar registers, if that is the way the |
| 72 | + * driver prints the pixels into the scanout buffer at panic time. |
| 73 | + * |
| 74 | + * - Updates to pointers like &drm_plane.state, allowing the panic handler to |
| 75 | + * safely deference these. This is done in drm_atomic_helper_swap_state(). |
| 76 | + * |
| 77 | + * - An state that isn't invariant and that the driver must be able to access |
| 78 | + * during panic printing. |
| 79 | + */ |
| 80 | + |
| 81 | +#define drm_panic_lock(dev, flags) \ |
| 82 | + raw_spin_lock_irqsave(&(dev)->mode_config.panic_lock, flags) |
| 83 | + |
| 84 | +/** |
| 85 | + * drm_panic_unlock - end of the panic printing critical section |
| 86 | + * @dev: struct drm_device |
| 87 | + * @flags: irq flags that were returned when acquiring the lock |
| 88 | + * |
| 89 | + * Unlocks the raw spinlock acquired by either drm_panic_lock() or |
| 90 | + * drm_panic_trylock(). |
| 91 | + */ |
| 92 | +#define drm_panic_unlock(dev, flags) \ |
| 93 | + raw_spin_unlock_irqrestore(&(dev)->mode_config.panic_lock, flags) |
| 94 | + |
| 95 | +#endif /* __DRM_PANIC_H__ */ |
0 commit comments