xref: /linux/drivers/gpu/drm/xe/display/xe_panic.c (revision b0a3a811634aa7422f8a92196db47a06a70bda16)
1 // SPDX-License-Identifier: MIT
2 /* Copyright © 2025 Intel Corporation */
3 
4 #include <drm/drm_cache.h>
5 #include <drm/drm_panic.h>
6 #include <drm/intel/display_parent_interface.h>
7 
8 #include "xe_bo.h"
9 #include "xe_panic.h"
10 #include "xe_res_cursor.h"
11 
12 struct intel_panic {
13 	struct xe_res_cursor res;
14 	struct iosys_map vmap;
15 
16 	int page;
17 
18 	struct xe_bo *bo;
19 	unsigned int (*tiling)(unsigned int x, unsigned int y, unsigned int width);
20 };
21 
22 static void xe_panic_kunmap(struct intel_panic *panic)
23 {
24 	if (!panic->vmap.is_iomem && iosys_map_is_set(&panic->vmap)) {
25 		drm_clflush_virt_range(panic->vmap.vaddr, PAGE_SIZE);
26 		kunmap_local(panic->vmap.vaddr);
27 	}
28 	iosys_map_clear(&panic->vmap);
29 	panic->page = -1;
30 }
31 
32 /*
33  * The scanout buffer pages are not mapped, so for each pixel,
34  * use kmap_local_page_try_from_panic() to map the page, and write the pixel.
35  * Try to keep the map from the previous pixel, to avoid too much map/unmap.
36  */
37 static void xe_panic_page_set_pixel(struct drm_scanout_buffer *sb, unsigned int x,
38 				    unsigned int y, u32 color)
39 {
40 	struct intel_panic *panic = sb->private;
41 	struct xe_bo *bo = panic->bo;
42 	unsigned int new_page;
43 	unsigned int offset;
44 
45 	if (panic->tiling)
46 		offset = panic->tiling(sb->width, x, y);
47 	else
48 		offset = y * sb->pitch[0] + x * sb->format->cpp[0];
49 
50 	new_page = offset >> PAGE_SHIFT;
51 	offset = offset % PAGE_SIZE;
52 	if (new_page != panic->page) {
53 		if (xe_bo_is_vram(bo)) {
54 			/* Display is always mapped on root tile */
55 			struct xe_vram_region *vram =
56 				xe_device_get_root_tile(xe_bo_device(bo))->mem.vram;
57 
58 			if (panic->page < 0 || new_page < panic->page) {
59 				xe_res_first(bo->ttm.resource, new_page * PAGE_SIZE,
60 					     bo->ttm.base.size - new_page * PAGE_SIZE, &panic->res);
61 			} else {
62 				xe_res_next(&panic->res, PAGE_SIZE * (new_page - panic->page));
63 			}
64 			iosys_map_set_vaddr_iomem(&panic->vmap,
65 						  vram->mapping + panic->res.start);
66 		} else {
67 			xe_panic_kunmap(panic);
68 			iosys_map_set_vaddr(&panic->vmap,
69 					    ttm_bo_kmap_try_from_panic(&bo->ttm,
70 								       new_page));
71 		}
72 		panic->page = new_page;
73 	}
74 
75 	if (iosys_map_is_set(&panic->vmap))
76 		iosys_map_wr(&panic->vmap, offset, u32, color);
77 }
78 
79 static struct intel_panic *xe_panic_alloc(void)
80 {
81 	struct intel_panic *panic;
82 
83 	panic = kzalloc_obj(*panic);
84 
85 	return panic;
86 }
87 
88 static int xe_panic_setup(struct intel_panic *panic, struct drm_scanout_buffer *sb,
89 			  struct drm_gem_object *obj,
90 			  unsigned int (*tiling)(unsigned int x, unsigned int y, unsigned int width))
91 {
92 	struct xe_bo *bo = gem_to_xe_bo(obj);
93 
94 	if (xe_bo_is_vram(bo) && !xe_bo_is_visible_vram(bo))
95 		return -ENODEV;
96 
97 	panic->page = -1;
98 	panic->bo = bo;
99 	panic->tiling = tiling;
100 
101 	sb->private = panic;
102 	sb->set_pixel = xe_panic_page_set_pixel;
103 
104 	return 0;
105 }
106 
107 const struct intel_display_panic_interface xe_display_panic_interface = {
108 	.alloc = xe_panic_alloc,
109 	.setup = xe_panic_setup,
110 	.finish = xe_panic_kunmap,
111 };
112