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" 231011812cSGustavo 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 { 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 /* 155a2112949SLucas De Marchi * If this is a masked register, need to set the upper 16 bits. 156a2112949SLucas De Marchi * Set them to clr_bits since that is always a superset of the bits 157a2112949SLucas De Marchi * being modified. 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) 163a2112949SLucas De Marchi val = entry->clr_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 17846c63b64SLucas De Marchi xe_gt_dbg(gt, "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_reg_sr_entry *entry; 189dd08ebf6SMatthew Brost unsigned long reg; 190dd08ebf6SMatthew Brost int err; 191dd08ebf6SMatthew Brost 1925be84050SLucas De Marchi if (xa_empty(&sr->xa)) 1935be84050SLucas De Marchi return; 1945be84050SLucas De Marchi 19546c63b64SLucas De Marchi xe_gt_dbg(gt, "Applying %s save-restore MMIOs\n", sr->name); 196dd08ebf6SMatthew Brost 197dd08ebf6SMatthew Brost err = xe_force_wake_get(>->mmio.fw, XE_FORCEWAKE_ALL); 198dd08ebf6SMatthew Brost if (err) 199dd08ebf6SMatthew Brost goto err_force_wake; 200dd08ebf6SMatthew Brost 201dd08ebf6SMatthew Brost xa_for_each(&sr->xa, reg, entry) 20207fbd1f8SLucas De Marchi apply_one_mmio(gt, entry); 203dd08ebf6SMatthew Brost 204dd08ebf6SMatthew Brost err = xe_force_wake_put(>->mmio.fw, XE_FORCEWAKE_ALL); 205dd08ebf6SMatthew Brost XE_WARN_ON(err); 206dd08ebf6SMatthew Brost 207dd08ebf6SMatthew Brost return; 208dd08ebf6SMatthew Brost 209dd08ebf6SMatthew Brost err_force_wake: 21046c63b64SLucas De Marchi xe_gt_err(gt, "Failed to apply, err=%d\n", err); 211dd08ebf6SMatthew Brost } 212dd08ebf6SMatthew Brost 2131011812cSGustavo Sousa void xe_reg_sr_apply_whitelist(struct xe_hw_engine *hwe) 214dd08ebf6SMatthew Brost { 2151011812cSGustavo Sousa struct xe_reg_sr *sr = &hwe->reg_whitelist; 2161011812cSGustavo Sousa struct xe_gt *gt = hwe->gt; 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; 2201011812cSGustavo Sousa u32 mmio_base = hwe->mmio_base; 221dd08ebf6SMatthew Brost unsigned long reg; 222dd08ebf6SMatthew Brost unsigned int slot = 0; 223dd08ebf6SMatthew Brost int err; 224dd08ebf6SMatthew Brost 2255be84050SLucas De Marchi if (xa_empty(&sr->xa)) 2265be84050SLucas De Marchi return; 2275be84050SLucas De Marchi 228dd08ebf6SMatthew Brost drm_dbg(&xe->drm, "Whitelisting %s registers\n", sr->name); 229dd08ebf6SMatthew Brost 230dd08ebf6SMatthew Brost err = xe_force_wake_get(>->mmio.fw, XE_FORCEWAKE_ALL); 231dd08ebf6SMatthew Brost if (err) 232dd08ebf6SMatthew Brost goto err_force_wake; 233dd08ebf6SMatthew Brost 234*e7835e02SJani Nikula p = drm_dbg_printer(&xe->drm, DRM_UT_DRIVER, NULL); 235dd08ebf6SMatthew Brost xa_for_each(&sr->xa, reg, entry) { 2365eeb8b44SGustavo Sousa if (slot == RING_MAX_NONPRIV_SLOTS) { 2375eeb8b44SGustavo Sousa xe_gt_err(gt, 2385eeb8b44SGustavo Sousa "hwe %s: maximum register whitelist slots (%d) reached, refusing to add more\n", 2395eeb8b44SGustavo Sousa hwe->name, RING_MAX_NONPRIV_SLOTS); 2405eeb8b44SGustavo Sousa break; 2415eeb8b44SGustavo Sousa } 2425eeb8b44SGustavo Sousa 243d855d224SLucas De Marchi xe_reg_whitelist_print_entry(&p, 0, reg, entry); 244ce8bf5bdSLucas De Marchi xe_mmio_write32(gt, RING_FORCE_TO_NONPRIV(mmio_base, slot), 245dd08ebf6SMatthew Brost reg | entry->set_bits); 246dd08ebf6SMatthew Brost slot++; 247dd08ebf6SMatthew Brost } 248dd08ebf6SMatthew Brost 249dd08ebf6SMatthew Brost /* And clear the rest just in case of garbage */ 250ce8bf5bdSLucas De Marchi for (; slot < RING_MAX_NONPRIV_SLOTS; slot++) { 251ee21379aSLucas De Marchi u32 addr = RING_NOPID(mmio_base).addr; 252ce8bf5bdSLucas De Marchi 253ce8bf5bdSLucas De Marchi xe_mmio_write32(gt, RING_FORCE_TO_NONPRIV(mmio_base, slot), addr); 254ce8bf5bdSLucas De Marchi } 255dd08ebf6SMatthew Brost 256dd08ebf6SMatthew Brost err = xe_force_wake_put(>->mmio.fw, XE_FORCEWAKE_ALL); 257dd08ebf6SMatthew Brost XE_WARN_ON(err); 258dd08ebf6SMatthew Brost 259dd08ebf6SMatthew Brost return; 260dd08ebf6SMatthew Brost 261dd08ebf6SMatthew Brost err_force_wake: 262dd08ebf6SMatthew Brost drm_err(&xe->drm, "Failed to apply, err=%d\n", err); 263dd08ebf6SMatthew Brost } 2646647e2feSLucas De Marchi 2656647e2feSLucas De Marchi /** 2666647e2feSLucas De Marchi * xe_reg_sr_dump - print all save/restore entries 2676647e2feSLucas De Marchi * @sr: Save/restore entries 2686647e2feSLucas De Marchi * @p: DRM printer 2696647e2feSLucas De Marchi */ 2706647e2feSLucas De Marchi void xe_reg_sr_dump(struct xe_reg_sr *sr, struct drm_printer *p) 2716647e2feSLucas De Marchi { 2726647e2feSLucas De Marchi struct xe_reg_sr_entry *entry; 2736647e2feSLucas De Marchi unsigned long reg; 2746647e2feSLucas De Marchi 2756647e2feSLucas De Marchi if (!sr->name || xa_empty(&sr->xa)) 2766647e2feSLucas De Marchi return; 2776647e2feSLucas De Marchi 2786647e2feSLucas De Marchi drm_printf(p, "%s\n", sr->name); 2796647e2feSLucas De Marchi xa_for_each(&sr->xa, reg, entry) 2806647e2feSLucas De Marchi drm_printf(p, "\tREG[0x%lx] clr=0x%08x set=0x%08x masked=%s mcr=%s\n", 2816647e2feSLucas De Marchi reg, entry->clr_bits, entry->set_bits, 28207fbd1f8SLucas De Marchi str_yes_no(entry->reg.masked), 28307fbd1f8SLucas De Marchi str_yes_no(entry->reg.mcr)); 2846647e2feSLucas De Marchi } 285