xref: /linux/drivers/gpu/drm/xe/xe_reg_sr.c (revision 437bcbab1023e06edd8dbca99f5c44e5d2b30133)
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"
22*437bcbabSGustavo Sousa #include "xe_gt_printk.h"
23dd08ebf6SMatthew Brost #include "xe_macros.h"
24dd08ebf6SMatthew Brost #include "xe_mmio.h"
25d855d224SLucas De Marchi #include "xe_reg_whitelist.h"
26ea9f879dSLucas De Marchi #include "xe_rtp_types.h"
27dd08ebf6SMatthew Brost 
28dd08ebf6SMatthew Brost #define XE_REG_SR_GROW_STEP_DEFAULT	16
29dd08ebf6SMatthew Brost 
30dd08ebf6SMatthew Brost static void reg_sr_fini(struct drm_device *drm, void *arg)
31dd08ebf6SMatthew Brost {
32dd08ebf6SMatthew Brost 	struct xe_reg_sr *sr = arg;
33dd08ebf6SMatthew Brost 
34dd08ebf6SMatthew Brost 	xa_destroy(&sr->xa);
35dd08ebf6SMatthew Brost 	kfree(sr->pool.arr);
36dd08ebf6SMatthew Brost 	memset(&sr->pool, 0, sizeof(sr->pool));
37dd08ebf6SMatthew Brost }
38dd08ebf6SMatthew Brost 
39dd08ebf6SMatthew Brost int xe_reg_sr_init(struct xe_reg_sr *sr, const char *name, struct xe_device *xe)
40dd08ebf6SMatthew Brost {
41dd08ebf6SMatthew Brost 	xa_init(&sr->xa);
42dd08ebf6SMatthew Brost 	memset(&sr->pool, 0, sizeof(sr->pool));
43dd08ebf6SMatthew Brost 	sr->pool.grow_step = XE_REG_SR_GROW_STEP_DEFAULT;
44dd08ebf6SMatthew Brost 	sr->name = name;
45dd08ebf6SMatthew Brost 
46dd08ebf6SMatthew Brost 	return drmm_add_action_or_reset(&xe->drm, reg_sr_fini, sr);
47dd08ebf6SMatthew Brost }
484cc04402SLucas De Marchi EXPORT_SYMBOL_IF_KUNIT(xe_reg_sr_init);
49dd08ebf6SMatthew Brost 
50dd08ebf6SMatthew Brost static struct xe_reg_sr_entry *alloc_entry(struct xe_reg_sr *sr)
51dd08ebf6SMatthew Brost {
52dd08ebf6SMatthew Brost 	if (sr->pool.used == sr->pool.allocated) {
53dd08ebf6SMatthew Brost 		struct xe_reg_sr_entry *arr;
54dd08ebf6SMatthew Brost 
55dd08ebf6SMatthew Brost 		arr = krealloc_array(sr->pool.arr,
56dd08ebf6SMatthew Brost 				     ALIGN(sr->pool.allocated + 1, sr->pool.grow_step),
57dd08ebf6SMatthew Brost 				     sizeof(*arr), GFP_KERNEL);
58dd08ebf6SMatthew Brost 		if (!arr)
59dd08ebf6SMatthew Brost 			return NULL;
60dd08ebf6SMatthew Brost 
61dd08ebf6SMatthew Brost 		sr->pool.arr = arr;
62dd08ebf6SMatthew Brost 		sr->pool.allocated += sr->pool.grow_step;
63dd08ebf6SMatthew Brost 	}
64dd08ebf6SMatthew Brost 
65dd08ebf6SMatthew Brost 	return &sr->pool.arr[sr->pool.used++];
66dd08ebf6SMatthew Brost }
67dd08ebf6SMatthew Brost 
68dd08ebf6SMatthew Brost static bool compatible_entries(const struct xe_reg_sr_entry *e1,
69dd08ebf6SMatthew Brost 			       const struct xe_reg_sr_entry *e2)
70dd08ebf6SMatthew Brost {
71dd08ebf6SMatthew Brost 	/*
72dd08ebf6SMatthew Brost 	 * Don't allow overwriting values: clr_bits/set_bits should be disjoint
73dd08ebf6SMatthew Brost 	 * when operating in the same register
74dd08ebf6SMatthew Brost 	 */
75dd08ebf6SMatthew Brost 	if (e1->clr_bits & e2->clr_bits || e1->set_bits & e2->set_bits ||
76dd08ebf6SMatthew Brost 	    e1->clr_bits & e2->set_bits || e1->set_bits & e2->clr_bits)
77dd08ebf6SMatthew Brost 		return false;
78dd08ebf6SMatthew Brost 
7907fbd1f8SLucas De Marchi 	if (e1->reg.raw != e2->reg.raw)
80dd08ebf6SMatthew Brost 		return false;
81dd08ebf6SMatthew Brost 
82dd08ebf6SMatthew Brost 	return true;
83dd08ebf6SMatthew Brost }
84dd08ebf6SMatthew Brost 
857bf350ecSLucas De Marchi static void reg_sr_inc_error(struct xe_reg_sr *sr)
867bf350ecSLucas De Marchi {
877bf350ecSLucas De Marchi #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST)
887bf350ecSLucas De Marchi 	sr->errors++;
897bf350ecSLucas De Marchi #endif
907bf350ecSLucas De Marchi }
917bf350ecSLucas De Marchi 
9207fbd1f8SLucas De Marchi int xe_reg_sr_add(struct xe_reg_sr *sr,
93*437bcbabSGustavo Sousa 		  const struct xe_reg_sr_entry *e,
94*437bcbabSGustavo Sousa 		  struct xe_gt *gt)
95dd08ebf6SMatthew Brost {
96ee21379aSLucas De Marchi 	unsigned long idx = e->reg.addr;
97dd08ebf6SMatthew Brost 	struct xe_reg_sr_entry *pentry = xa_load(&sr->xa, idx);
98dd08ebf6SMatthew Brost 	int ret;
99dd08ebf6SMatthew Brost 
100dd08ebf6SMatthew Brost 	if (pentry) {
101dd08ebf6SMatthew Brost 		if (!compatible_entries(pentry, e)) {
102dd08ebf6SMatthew Brost 			ret = -EINVAL;
103dd08ebf6SMatthew Brost 			goto fail;
104dd08ebf6SMatthew Brost 		}
105dd08ebf6SMatthew Brost 
106dd08ebf6SMatthew Brost 		pentry->clr_bits |= e->clr_bits;
107dd08ebf6SMatthew Brost 		pentry->set_bits |= e->set_bits;
108dd08ebf6SMatthew Brost 		pentry->read_mask |= e->read_mask;
109dd08ebf6SMatthew Brost 
110dd08ebf6SMatthew Brost 		return 0;
111dd08ebf6SMatthew Brost 	}
112dd08ebf6SMatthew Brost 
113dd08ebf6SMatthew Brost 	pentry = alloc_entry(sr);
114dd08ebf6SMatthew Brost 	if (!pentry) {
115dd08ebf6SMatthew Brost 		ret = -ENOMEM;
116dd08ebf6SMatthew Brost 		goto fail;
117dd08ebf6SMatthew Brost 	}
118dd08ebf6SMatthew Brost 
119dd08ebf6SMatthew Brost 	*pentry = *e;
120dd08ebf6SMatthew Brost 	ret = xa_err(xa_store(&sr->xa, idx, pentry, GFP_KERNEL));
121dd08ebf6SMatthew Brost 	if (ret)
122dd08ebf6SMatthew Brost 		goto fail;
123dd08ebf6SMatthew Brost 
124dd08ebf6SMatthew Brost 	return 0;
125dd08ebf6SMatthew Brost 
126dd08ebf6SMatthew Brost fail:
127*437bcbabSGustavo Sousa 	xe_gt_err(gt,
128*437bcbabSGustavo Sousa 		  "discarding save-restore reg %04lx (clear: %08x, set: %08x, masked: %s, mcr: %s): ret=%d\n",
129dd08ebf6SMatthew Brost 		  idx, e->clr_bits, e->set_bits,
13007fbd1f8SLucas De Marchi 		  str_yes_no(e->reg.masked),
13107fbd1f8SLucas De Marchi 		  str_yes_no(e->reg.mcr),
13207fbd1f8SLucas De Marchi 		  ret);
1337bf350ecSLucas De Marchi 	reg_sr_inc_error(sr);
134dd08ebf6SMatthew Brost 
135dd08ebf6SMatthew Brost 	return ret;
136dd08ebf6SMatthew Brost }
137dd08ebf6SMatthew Brost 
13807fbd1f8SLucas De Marchi /*
13907fbd1f8SLucas De Marchi  * Convert back from encoded value to type-safe, only to be used when reg.mcr
14007fbd1f8SLucas De Marchi  * is true
14107fbd1f8SLucas De Marchi  */
14207fbd1f8SLucas De Marchi static struct xe_reg_mcr to_xe_reg_mcr(const struct xe_reg reg)
14307fbd1f8SLucas De Marchi {
14407fbd1f8SLucas De Marchi 	return (const struct xe_reg_mcr){.__reg.raw = reg.raw };
14507fbd1f8SLucas De Marchi }
14607fbd1f8SLucas De Marchi 
14707fbd1f8SLucas De Marchi static void apply_one_mmio(struct xe_gt *gt, struct xe_reg_sr_entry *entry)
148dd08ebf6SMatthew Brost {
149dd08ebf6SMatthew Brost 	struct xe_device *xe = gt_to_xe(gt);
15007fbd1f8SLucas De Marchi 	struct xe_reg reg = entry->reg;
15107fbd1f8SLucas De Marchi 	struct xe_reg_mcr reg_mcr = to_xe_reg_mcr(reg);
152dd08ebf6SMatthew Brost 	u32 val;
153dd08ebf6SMatthew Brost 
154dd08ebf6SMatthew Brost 	/*
155dd08ebf6SMatthew Brost 	 * If this is a masked register, need to figure what goes on the upper
156dd08ebf6SMatthew Brost 	 * 16 bits: it's either the clr_bits (when using FIELD_SET and WR) or
157dd08ebf6SMatthew Brost 	 * the set_bits, when using SET.
158dd08ebf6SMatthew Brost 	 *
159dd08ebf6SMatthew Brost 	 * When it's not masked, we have to read it from hardware, unless we are
160dd08ebf6SMatthew Brost 	 * supposed to set all bits.
161dd08ebf6SMatthew Brost 	 */
16207fbd1f8SLucas De Marchi 	if (reg.masked)
16379f2432eSMatt Roper 		val = (entry->clr_bits ?: entry->set_bits) << 16;
164dd08ebf6SMatthew Brost 	else if (entry->clr_bits + 1)
16507fbd1f8SLucas De Marchi 		val = (reg.mcr ?
16607fbd1f8SLucas De Marchi 		       xe_gt_mcr_unicast_read_any(gt, reg_mcr) :
167ce8bf5bdSLucas De Marchi 		       xe_mmio_read32(gt, reg)) & (~entry->clr_bits);
168dd08ebf6SMatthew Brost 	else
169dd08ebf6SMatthew Brost 		val = 0;
170dd08ebf6SMatthew Brost 
171dd08ebf6SMatthew Brost 	/*
172dd08ebf6SMatthew Brost 	 * TODO: add selftest to validate all tables, regardless of platform:
173dd08ebf6SMatthew Brost 	 *   - Masked registers can't have set_bits with upper bits set
174dd08ebf6SMatthew Brost 	 *   - set_bits must be contained in clr_bits
175dd08ebf6SMatthew Brost 	 */
176dd08ebf6SMatthew Brost 	val |= entry->set_bits;
177dd08ebf6SMatthew Brost 
178ee21379aSLucas De Marchi 	drm_dbg(&xe->drm, "REG[0x%x] = 0x%08x", reg.addr, val);
179dd08ebf6SMatthew Brost 
18007fbd1f8SLucas De Marchi 	if (entry->reg.mcr)
18107fbd1f8SLucas De Marchi 		xe_gt_mcr_multicast_write(gt, reg_mcr, val);
182dd08ebf6SMatthew Brost 	else
183ce8bf5bdSLucas De Marchi 		xe_mmio_write32(gt, reg, val);
184dd08ebf6SMatthew Brost }
185dd08ebf6SMatthew Brost 
186dd08ebf6SMatthew Brost void xe_reg_sr_apply_mmio(struct xe_reg_sr *sr, struct xe_gt *gt)
187dd08ebf6SMatthew Brost {
188dd08ebf6SMatthew Brost 	struct xe_device *xe = gt_to_xe(gt);
189dd08ebf6SMatthew Brost 	struct xe_reg_sr_entry *entry;
190dd08ebf6SMatthew Brost 	unsigned long reg;
191dd08ebf6SMatthew Brost 	int err;
192dd08ebf6SMatthew Brost 
1935be84050SLucas De Marchi 	if (xa_empty(&sr->xa))
1945be84050SLucas De Marchi 		return;
1955be84050SLucas De Marchi 
196dd08ebf6SMatthew Brost 	drm_dbg(&xe->drm, "Applying %s save-restore MMIOs\n", sr->name);
197dd08ebf6SMatthew Brost 
198dd08ebf6SMatthew Brost 	err = xe_force_wake_get(&gt->mmio.fw, XE_FORCEWAKE_ALL);
199dd08ebf6SMatthew Brost 	if (err)
200dd08ebf6SMatthew Brost 		goto err_force_wake;
201dd08ebf6SMatthew Brost 
202dd08ebf6SMatthew Brost 	xa_for_each(&sr->xa, reg, entry)
20307fbd1f8SLucas De Marchi 		apply_one_mmio(gt, entry);
204dd08ebf6SMatthew Brost 
205dd08ebf6SMatthew Brost 	err = xe_force_wake_put(&gt->mmio.fw, XE_FORCEWAKE_ALL);
206dd08ebf6SMatthew Brost 	XE_WARN_ON(err);
207dd08ebf6SMatthew Brost 
208dd08ebf6SMatthew Brost 	return;
209dd08ebf6SMatthew Brost 
210dd08ebf6SMatthew Brost err_force_wake:
211dd08ebf6SMatthew Brost 	drm_err(&xe->drm, "Failed to apply, err=%d\n", err);
212dd08ebf6SMatthew Brost }
213dd08ebf6SMatthew Brost 
214dd08ebf6SMatthew Brost void xe_reg_sr_apply_whitelist(struct xe_reg_sr *sr, u32 mmio_base,
215dd08ebf6SMatthew Brost 			       struct xe_gt *gt)
216dd08ebf6SMatthew Brost {
217dd08ebf6SMatthew Brost 	struct xe_device *xe = gt_to_xe(gt);
218dd08ebf6SMatthew Brost 	struct xe_reg_sr_entry *entry;
219d855d224SLucas De Marchi 	struct drm_printer p;
220dd08ebf6SMatthew Brost 	unsigned long reg;
221dd08ebf6SMatthew Brost 	unsigned int slot = 0;
222dd08ebf6SMatthew Brost 	int err;
223dd08ebf6SMatthew Brost 
2245be84050SLucas De Marchi 	if (xa_empty(&sr->xa))
2255be84050SLucas De Marchi 		return;
2265be84050SLucas De Marchi 
227dd08ebf6SMatthew Brost 	drm_dbg(&xe->drm, "Whitelisting %s registers\n", sr->name);
228dd08ebf6SMatthew Brost 
229dd08ebf6SMatthew Brost 	err = xe_force_wake_get(&gt->mmio.fw, XE_FORCEWAKE_ALL);
230dd08ebf6SMatthew Brost 	if (err)
231dd08ebf6SMatthew Brost 		goto err_force_wake;
232dd08ebf6SMatthew Brost 
233d855d224SLucas De Marchi 	p = drm_debug_printer(KBUILD_MODNAME);
234dd08ebf6SMatthew Brost 	xa_for_each(&sr->xa, reg, entry) {
235d855d224SLucas De Marchi 		xe_reg_whitelist_print_entry(&p, 0, reg, entry);
236ce8bf5bdSLucas De Marchi 		xe_mmio_write32(gt, RING_FORCE_TO_NONPRIV(mmio_base, slot),
237dd08ebf6SMatthew Brost 				reg | entry->set_bits);
238dd08ebf6SMatthew Brost 		slot++;
239dd08ebf6SMatthew Brost 	}
240dd08ebf6SMatthew Brost 
241dd08ebf6SMatthew Brost 	/* And clear the rest just in case of garbage */
242ce8bf5bdSLucas De Marchi 	for (; slot < RING_MAX_NONPRIV_SLOTS; slot++) {
243ee21379aSLucas De Marchi 		u32 addr = RING_NOPID(mmio_base).addr;
244ce8bf5bdSLucas De Marchi 
245ce8bf5bdSLucas De Marchi 		xe_mmio_write32(gt, RING_FORCE_TO_NONPRIV(mmio_base, slot), addr);
246ce8bf5bdSLucas De Marchi 	}
247dd08ebf6SMatthew Brost 
248dd08ebf6SMatthew Brost 	err = xe_force_wake_put(&gt->mmio.fw, XE_FORCEWAKE_ALL);
249dd08ebf6SMatthew Brost 	XE_WARN_ON(err);
250dd08ebf6SMatthew Brost 
251dd08ebf6SMatthew Brost 	return;
252dd08ebf6SMatthew Brost 
253dd08ebf6SMatthew Brost err_force_wake:
254dd08ebf6SMatthew Brost 	drm_err(&xe->drm, "Failed to apply, err=%d\n", err);
255dd08ebf6SMatthew Brost }
2566647e2feSLucas De Marchi 
2576647e2feSLucas De Marchi /**
2586647e2feSLucas De Marchi  * xe_reg_sr_dump - print all save/restore entries
2596647e2feSLucas De Marchi  * @sr: Save/restore entries
2606647e2feSLucas De Marchi  * @p: DRM printer
2616647e2feSLucas De Marchi  */
2626647e2feSLucas De Marchi void xe_reg_sr_dump(struct xe_reg_sr *sr, struct drm_printer *p)
2636647e2feSLucas De Marchi {
2646647e2feSLucas De Marchi 	struct xe_reg_sr_entry *entry;
2656647e2feSLucas De Marchi 	unsigned long reg;
2666647e2feSLucas De Marchi 
2676647e2feSLucas De Marchi 	if (!sr->name || xa_empty(&sr->xa))
2686647e2feSLucas De Marchi 		return;
2696647e2feSLucas De Marchi 
2706647e2feSLucas De Marchi 	drm_printf(p, "%s\n", sr->name);
2716647e2feSLucas De Marchi 	xa_for_each(&sr->xa, reg, entry)
2726647e2feSLucas De Marchi 		drm_printf(p, "\tREG[0x%lx] clr=0x%08x set=0x%08x masked=%s mcr=%s\n",
2736647e2feSLucas De Marchi 			   reg, entry->clr_bits, entry->set_bits,
27407fbd1f8SLucas De Marchi 			   str_yes_no(entry->reg.masked),
27507fbd1f8SLucas De Marchi 			   str_yes_no(entry->reg.mcr));
2766647e2feSLucas De Marchi }
277