xref: /linux/drivers/gpu/drm/xe/xe_reg_sr.c (revision 1011812c642c664b254986fb34264c2ee8d2bb50)
1dd08ebf6SMatthew Brost // SPDX-License-Identifier: MIT
2dd08ebf6SMatthew Brost /*
3dd08ebf6SMatthew Brost  * Copyright © 2022 Intel Corporation
4dd08ebf6SMatthew Brost  */
5dd08ebf6SMatthew Brost 
6dd08ebf6SMatthew Brost #include "xe_reg_sr.h"
7dd08ebf6SMatthew Brost 
84cc04402SLucas De Marchi #include <kunit/visibility.h>
9dd08ebf6SMatthew Brost #include <linux/align.h>
10dd08ebf6SMatthew Brost #include <linux/string_helpers.h>
11dd08ebf6SMatthew Brost #include <linux/xarray.h>
12dd08ebf6SMatthew Brost 
13dd08ebf6SMatthew Brost #include <drm/drm_managed.h>
14ea9f879dSLucas De Marchi #include <drm/drm_print.h>
15dd08ebf6SMatthew Brost 
16b79e8fd9SLucas De Marchi #include "regs/xe_engine_regs.h"
17226bfec8SLucas De Marchi #include "regs/xe_gt_regs.h"
18dd08ebf6SMatthew Brost #include "xe_device_types.h"
19dd08ebf6SMatthew Brost #include "xe_force_wake.h"
20dd08ebf6SMatthew Brost #include "xe_gt.h"
21dd08ebf6SMatthew Brost #include "xe_gt_mcr.h"
22437bcbabSGustavo Sousa #include "xe_gt_printk.h"
23*1011812cSGustavo Sousa #include "xe_hw_engine_types.h"
24dd08ebf6SMatthew Brost #include "xe_macros.h"
25dd08ebf6SMatthew Brost #include "xe_mmio.h"
26d855d224SLucas De Marchi #include "xe_reg_whitelist.h"
27ea9f879dSLucas De Marchi #include "xe_rtp_types.h"
28dd08ebf6SMatthew Brost 
29dd08ebf6SMatthew Brost #define XE_REG_SR_GROW_STEP_DEFAULT	16
30dd08ebf6SMatthew Brost 
31dd08ebf6SMatthew Brost static void reg_sr_fini(struct drm_device *drm, void *arg)
32dd08ebf6SMatthew Brost {
33dd08ebf6SMatthew Brost 	struct xe_reg_sr *sr = arg;
34dd08ebf6SMatthew Brost 
35dd08ebf6SMatthew Brost 	xa_destroy(&sr->xa);
36dd08ebf6SMatthew Brost 	kfree(sr->pool.arr);
37dd08ebf6SMatthew Brost 	memset(&sr->pool, 0, sizeof(sr->pool));
38dd08ebf6SMatthew Brost }
39dd08ebf6SMatthew Brost 
40dd08ebf6SMatthew Brost int xe_reg_sr_init(struct xe_reg_sr *sr, const char *name, struct xe_device *xe)
41dd08ebf6SMatthew Brost {
42dd08ebf6SMatthew Brost 	xa_init(&sr->xa);
43dd08ebf6SMatthew Brost 	memset(&sr->pool, 0, sizeof(sr->pool));
44dd08ebf6SMatthew Brost 	sr->pool.grow_step = XE_REG_SR_GROW_STEP_DEFAULT;
45dd08ebf6SMatthew Brost 	sr->name = name;
46dd08ebf6SMatthew Brost 
47dd08ebf6SMatthew Brost 	return drmm_add_action_or_reset(&xe->drm, reg_sr_fini, sr);
48dd08ebf6SMatthew Brost }
494cc04402SLucas De Marchi EXPORT_SYMBOL_IF_KUNIT(xe_reg_sr_init);
50dd08ebf6SMatthew Brost 
51dd08ebf6SMatthew Brost static struct xe_reg_sr_entry *alloc_entry(struct xe_reg_sr *sr)
52dd08ebf6SMatthew Brost {
53dd08ebf6SMatthew Brost 	if (sr->pool.used == sr->pool.allocated) {
54dd08ebf6SMatthew Brost 		struct xe_reg_sr_entry *arr;
55dd08ebf6SMatthew Brost 
56dd08ebf6SMatthew Brost 		arr = krealloc_array(sr->pool.arr,
57dd08ebf6SMatthew Brost 				     ALIGN(sr->pool.allocated + 1, sr->pool.grow_step),
58dd08ebf6SMatthew Brost 				     sizeof(*arr), GFP_KERNEL);
59dd08ebf6SMatthew Brost 		if (!arr)
60dd08ebf6SMatthew Brost 			return NULL;
61dd08ebf6SMatthew Brost 
62dd08ebf6SMatthew Brost 		sr->pool.arr = arr;
63dd08ebf6SMatthew Brost 		sr->pool.allocated += sr->pool.grow_step;
64dd08ebf6SMatthew Brost 	}
65dd08ebf6SMatthew Brost 
66dd08ebf6SMatthew Brost 	return &sr->pool.arr[sr->pool.used++];
67dd08ebf6SMatthew Brost }
68dd08ebf6SMatthew Brost 
69dd08ebf6SMatthew Brost static bool compatible_entries(const struct xe_reg_sr_entry *e1,
70dd08ebf6SMatthew Brost 			       const struct xe_reg_sr_entry *e2)
71dd08ebf6SMatthew Brost {
72dd08ebf6SMatthew Brost 	/*
73dd08ebf6SMatthew Brost 	 * Don't allow overwriting values: clr_bits/set_bits should be disjoint
74dd08ebf6SMatthew Brost 	 * when operating in the same register
75dd08ebf6SMatthew Brost 	 */
76dd08ebf6SMatthew Brost 	if (e1->clr_bits & e2->clr_bits || e1->set_bits & e2->set_bits ||
77dd08ebf6SMatthew Brost 	    e1->clr_bits & e2->set_bits || e1->set_bits & e2->clr_bits)
78dd08ebf6SMatthew Brost 		return false;
79dd08ebf6SMatthew Brost 
8007fbd1f8SLucas De Marchi 	if (e1->reg.raw != e2->reg.raw)
81dd08ebf6SMatthew Brost 		return false;
82dd08ebf6SMatthew Brost 
83dd08ebf6SMatthew Brost 	return true;
84dd08ebf6SMatthew Brost }
85dd08ebf6SMatthew Brost 
867bf350ecSLucas De Marchi static void reg_sr_inc_error(struct xe_reg_sr *sr)
877bf350ecSLucas De Marchi {
887bf350ecSLucas De Marchi #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST)
897bf350ecSLucas De Marchi 	sr->errors++;
907bf350ecSLucas De Marchi #endif
917bf350ecSLucas De Marchi }
927bf350ecSLucas De Marchi 
9307fbd1f8SLucas De Marchi int xe_reg_sr_add(struct xe_reg_sr *sr,
94437bcbabSGustavo Sousa 		  const struct xe_reg_sr_entry *e,
95437bcbabSGustavo Sousa 		  struct xe_gt *gt)
96dd08ebf6SMatthew Brost {
97ee21379aSLucas De Marchi 	unsigned long idx = e->reg.addr;
98dd08ebf6SMatthew Brost 	struct xe_reg_sr_entry *pentry = xa_load(&sr->xa, idx);
99dd08ebf6SMatthew Brost 	int ret;
100dd08ebf6SMatthew Brost 
101dd08ebf6SMatthew Brost 	if (pentry) {
102dd08ebf6SMatthew Brost 		if (!compatible_entries(pentry, e)) {
103dd08ebf6SMatthew Brost 			ret = -EINVAL;
104dd08ebf6SMatthew Brost 			goto fail;
105dd08ebf6SMatthew Brost 		}
106dd08ebf6SMatthew Brost 
107dd08ebf6SMatthew Brost 		pentry->clr_bits |= e->clr_bits;
108dd08ebf6SMatthew Brost 		pentry->set_bits |= e->set_bits;
109dd08ebf6SMatthew Brost 		pentry->read_mask |= e->read_mask;
110dd08ebf6SMatthew Brost 
111dd08ebf6SMatthew Brost 		return 0;
112dd08ebf6SMatthew Brost 	}
113dd08ebf6SMatthew Brost 
114dd08ebf6SMatthew Brost 	pentry = alloc_entry(sr);
115dd08ebf6SMatthew Brost 	if (!pentry) {
116dd08ebf6SMatthew Brost 		ret = -ENOMEM;
117dd08ebf6SMatthew Brost 		goto fail;
118dd08ebf6SMatthew Brost 	}
119dd08ebf6SMatthew Brost 
120dd08ebf6SMatthew Brost 	*pentry = *e;
121dd08ebf6SMatthew Brost 	ret = xa_err(xa_store(&sr->xa, idx, pentry, GFP_KERNEL));
122dd08ebf6SMatthew Brost 	if (ret)
123dd08ebf6SMatthew Brost 		goto fail;
124dd08ebf6SMatthew Brost 
125dd08ebf6SMatthew Brost 	return 0;
126dd08ebf6SMatthew Brost 
127dd08ebf6SMatthew Brost fail:
128437bcbabSGustavo Sousa 	xe_gt_err(gt,
129437bcbabSGustavo Sousa 		  "discarding save-restore reg %04lx (clear: %08x, set: %08x, masked: %s, mcr: %s): ret=%d\n",
130dd08ebf6SMatthew Brost 		  idx, e->clr_bits, e->set_bits,
13107fbd1f8SLucas De Marchi 		  str_yes_no(e->reg.masked),
13207fbd1f8SLucas De Marchi 		  str_yes_no(e->reg.mcr),
13307fbd1f8SLucas De Marchi 		  ret);
1347bf350ecSLucas De Marchi 	reg_sr_inc_error(sr);
135dd08ebf6SMatthew Brost 
136dd08ebf6SMatthew Brost 	return ret;
137dd08ebf6SMatthew Brost }
138dd08ebf6SMatthew Brost 
13907fbd1f8SLucas De Marchi /*
14007fbd1f8SLucas De Marchi  * Convert back from encoded value to type-safe, only to be used when reg.mcr
14107fbd1f8SLucas De Marchi  * is true
14207fbd1f8SLucas De Marchi  */
14307fbd1f8SLucas De Marchi static struct xe_reg_mcr to_xe_reg_mcr(const struct xe_reg reg)
14407fbd1f8SLucas De Marchi {
14507fbd1f8SLucas De Marchi 	return (const struct xe_reg_mcr){.__reg.raw = reg.raw };
14607fbd1f8SLucas De Marchi }
14707fbd1f8SLucas De Marchi 
14807fbd1f8SLucas De Marchi static void apply_one_mmio(struct xe_gt *gt, struct xe_reg_sr_entry *entry)
149dd08ebf6SMatthew Brost {
150dd08ebf6SMatthew Brost 	struct xe_device *xe = gt_to_xe(gt);
15107fbd1f8SLucas De Marchi 	struct xe_reg reg = entry->reg;
15207fbd1f8SLucas De Marchi 	struct xe_reg_mcr reg_mcr = to_xe_reg_mcr(reg);
153dd08ebf6SMatthew Brost 	u32 val;
154dd08ebf6SMatthew Brost 
155dd08ebf6SMatthew Brost 	/*
156dd08ebf6SMatthew Brost 	 * If this is a masked register, need to figure what goes on the upper
157dd08ebf6SMatthew Brost 	 * 16 bits: it's either the clr_bits (when using FIELD_SET and WR) or
158dd08ebf6SMatthew Brost 	 * the set_bits, when using SET.
159dd08ebf6SMatthew Brost 	 *
160dd08ebf6SMatthew Brost 	 * When it's not masked, we have to read it from hardware, unless we are
161dd08ebf6SMatthew Brost 	 * supposed to set all bits.
162dd08ebf6SMatthew Brost 	 */
16307fbd1f8SLucas De Marchi 	if (reg.masked)
16479f2432eSMatt Roper 		val = (entry->clr_bits ?: entry->set_bits) << 16;
165dd08ebf6SMatthew Brost 	else if (entry->clr_bits + 1)
16607fbd1f8SLucas De Marchi 		val = (reg.mcr ?
16707fbd1f8SLucas De Marchi 		       xe_gt_mcr_unicast_read_any(gt, reg_mcr) :
168ce8bf5bdSLucas De Marchi 		       xe_mmio_read32(gt, reg)) & (~entry->clr_bits);
169dd08ebf6SMatthew Brost 	else
170dd08ebf6SMatthew Brost 		val = 0;
171dd08ebf6SMatthew Brost 
172dd08ebf6SMatthew Brost 	/*
173dd08ebf6SMatthew Brost 	 * TODO: add selftest to validate all tables, regardless of platform:
174dd08ebf6SMatthew Brost 	 *   - Masked registers can't have set_bits with upper bits set
175dd08ebf6SMatthew Brost 	 *   - set_bits must be contained in clr_bits
176dd08ebf6SMatthew Brost 	 */
177dd08ebf6SMatthew Brost 	val |= entry->set_bits;
178dd08ebf6SMatthew Brost 
179ee21379aSLucas De Marchi 	drm_dbg(&xe->drm, "REG[0x%x] = 0x%08x", reg.addr, val);
180dd08ebf6SMatthew Brost 
18107fbd1f8SLucas De Marchi 	if (entry->reg.mcr)
18207fbd1f8SLucas De Marchi 		xe_gt_mcr_multicast_write(gt, reg_mcr, val);
183dd08ebf6SMatthew Brost 	else
184ce8bf5bdSLucas De Marchi 		xe_mmio_write32(gt, reg, val);
185dd08ebf6SMatthew Brost }
186dd08ebf6SMatthew Brost 
187dd08ebf6SMatthew Brost void xe_reg_sr_apply_mmio(struct xe_reg_sr *sr, struct xe_gt *gt)
188dd08ebf6SMatthew Brost {
189dd08ebf6SMatthew Brost 	struct xe_device *xe = gt_to_xe(gt);
190dd08ebf6SMatthew Brost 	struct xe_reg_sr_entry *entry;
191dd08ebf6SMatthew Brost 	unsigned long reg;
192dd08ebf6SMatthew Brost 	int err;
193dd08ebf6SMatthew Brost 
1945be84050SLucas De Marchi 	if (xa_empty(&sr->xa))
1955be84050SLucas De Marchi 		return;
1965be84050SLucas De Marchi 
197dd08ebf6SMatthew Brost 	drm_dbg(&xe->drm, "Applying %s save-restore MMIOs\n", sr->name);
198dd08ebf6SMatthew Brost 
199dd08ebf6SMatthew Brost 	err = xe_force_wake_get(&gt->mmio.fw, XE_FORCEWAKE_ALL);
200dd08ebf6SMatthew Brost 	if (err)
201dd08ebf6SMatthew Brost 		goto err_force_wake;
202dd08ebf6SMatthew Brost 
203dd08ebf6SMatthew Brost 	xa_for_each(&sr->xa, reg, entry)
20407fbd1f8SLucas De Marchi 		apply_one_mmio(gt, entry);
205dd08ebf6SMatthew Brost 
206dd08ebf6SMatthew Brost 	err = xe_force_wake_put(&gt->mmio.fw, XE_FORCEWAKE_ALL);
207dd08ebf6SMatthew Brost 	XE_WARN_ON(err);
208dd08ebf6SMatthew Brost 
209dd08ebf6SMatthew Brost 	return;
210dd08ebf6SMatthew Brost 
211dd08ebf6SMatthew Brost err_force_wake:
212dd08ebf6SMatthew Brost 	drm_err(&xe->drm, "Failed to apply, err=%d\n", err);
213dd08ebf6SMatthew Brost }
214dd08ebf6SMatthew Brost 
215*1011812cSGustavo Sousa void xe_reg_sr_apply_whitelist(struct xe_hw_engine *hwe)
216dd08ebf6SMatthew Brost {
217*1011812cSGustavo Sousa 	struct xe_reg_sr *sr = &hwe->reg_whitelist;
218*1011812cSGustavo Sousa 	struct xe_gt *gt = hwe->gt;
219dd08ebf6SMatthew Brost 	struct xe_device *xe = gt_to_xe(gt);
220dd08ebf6SMatthew Brost 	struct xe_reg_sr_entry *entry;
221d855d224SLucas De Marchi 	struct drm_printer p;
222*1011812cSGustavo Sousa 	u32 mmio_base = hwe->mmio_base;
223dd08ebf6SMatthew Brost 	unsigned long reg;
224dd08ebf6SMatthew Brost 	unsigned int slot = 0;
225dd08ebf6SMatthew Brost 	int err;
226dd08ebf6SMatthew Brost 
2275be84050SLucas De Marchi 	if (xa_empty(&sr->xa))
2285be84050SLucas De Marchi 		return;
2295be84050SLucas De Marchi 
230dd08ebf6SMatthew Brost 	drm_dbg(&xe->drm, "Whitelisting %s registers\n", sr->name);
231dd08ebf6SMatthew Brost 
232dd08ebf6SMatthew Brost 	err = xe_force_wake_get(&gt->mmio.fw, XE_FORCEWAKE_ALL);
233dd08ebf6SMatthew Brost 	if (err)
234dd08ebf6SMatthew Brost 		goto err_force_wake;
235dd08ebf6SMatthew Brost 
236d855d224SLucas De Marchi 	p = drm_debug_printer(KBUILD_MODNAME);
237dd08ebf6SMatthew Brost 	xa_for_each(&sr->xa, reg, entry) {
238d855d224SLucas De Marchi 		xe_reg_whitelist_print_entry(&p, 0, reg, entry);
239ce8bf5bdSLucas De Marchi 		xe_mmio_write32(gt, RING_FORCE_TO_NONPRIV(mmio_base, slot),
240dd08ebf6SMatthew Brost 				reg | entry->set_bits);
241dd08ebf6SMatthew Brost 		slot++;
242dd08ebf6SMatthew Brost 	}
243dd08ebf6SMatthew Brost 
244dd08ebf6SMatthew Brost 	/* And clear the rest just in case of garbage */
245ce8bf5bdSLucas De Marchi 	for (; slot < RING_MAX_NONPRIV_SLOTS; slot++) {
246ee21379aSLucas De Marchi 		u32 addr = RING_NOPID(mmio_base).addr;
247ce8bf5bdSLucas De Marchi 
248ce8bf5bdSLucas De Marchi 		xe_mmio_write32(gt, RING_FORCE_TO_NONPRIV(mmio_base, slot), addr);
249ce8bf5bdSLucas De Marchi 	}
250dd08ebf6SMatthew Brost 
251dd08ebf6SMatthew Brost 	err = xe_force_wake_put(&gt->mmio.fw, XE_FORCEWAKE_ALL);
252dd08ebf6SMatthew Brost 	XE_WARN_ON(err);
253dd08ebf6SMatthew Brost 
254dd08ebf6SMatthew Brost 	return;
255dd08ebf6SMatthew Brost 
256dd08ebf6SMatthew Brost err_force_wake:
257dd08ebf6SMatthew Brost 	drm_err(&xe->drm, "Failed to apply, err=%d\n", err);
258dd08ebf6SMatthew Brost }
2596647e2feSLucas De Marchi 
2606647e2feSLucas De Marchi /**
2616647e2feSLucas De Marchi  * xe_reg_sr_dump - print all save/restore entries
2626647e2feSLucas De Marchi  * @sr: Save/restore entries
2636647e2feSLucas De Marchi  * @p: DRM printer
2646647e2feSLucas De Marchi  */
2656647e2feSLucas De Marchi void xe_reg_sr_dump(struct xe_reg_sr *sr, struct drm_printer *p)
2666647e2feSLucas De Marchi {
2676647e2feSLucas De Marchi 	struct xe_reg_sr_entry *entry;
2686647e2feSLucas De Marchi 	unsigned long reg;
2696647e2feSLucas De Marchi 
2706647e2feSLucas De Marchi 	if (!sr->name || xa_empty(&sr->xa))
2716647e2feSLucas De Marchi 		return;
2726647e2feSLucas De Marchi 
2736647e2feSLucas De Marchi 	drm_printf(p, "%s\n", sr->name);
2746647e2feSLucas De Marchi 	xa_for_each(&sr->xa, reg, entry)
2756647e2feSLucas De Marchi 		drm_printf(p, "\tREG[0x%lx] clr=0x%08x set=0x%08x masked=%s mcr=%s\n",
2766647e2feSLucas De Marchi 			   reg, entry->clr_bits, entry->set_bits,
27707fbd1f8SLucas De Marchi 			   str_yes_no(entry->reg.masked),
27807fbd1f8SLucas De Marchi 			   str_yes_no(entry->reg.mcr));
2796647e2feSLucas De Marchi }
280