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" 22dd08ebf6SMatthew Brost #include "xe_macros.h" 23dd08ebf6SMatthew Brost #include "xe_mmio.h" 24d855d224SLucas De Marchi #include "xe_reg_whitelist.h" 25ea9f879dSLucas De Marchi #include "xe_rtp_types.h" 26dd08ebf6SMatthew Brost 27dd08ebf6SMatthew Brost #define XE_REG_SR_GROW_STEP_DEFAULT 16 28dd08ebf6SMatthew Brost 29dd08ebf6SMatthew Brost static void reg_sr_fini(struct drm_device *drm, void *arg) 30dd08ebf6SMatthew Brost { 31dd08ebf6SMatthew Brost struct xe_reg_sr *sr = arg; 32dd08ebf6SMatthew Brost 33dd08ebf6SMatthew Brost xa_destroy(&sr->xa); 34dd08ebf6SMatthew Brost kfree(sr->pool.arr); 35dd08ebf6SMatthew Brost memset(&sr->pool, 0, sizeof(sr->pool)); 36dd08ebf6SMatthew Brost } 37dd08ebf6SMatthew Brost 38dd08ebf6SMatthew Brost int xe_reg_sr_init(struct xe_reg_sr *sr, const char *name, struct xe_device *xe) 39dd08ebf6SMatthew Brost { 40dd08ebf6SMatthew Brost xa_init(&sr->xa); 41dd08ebf6SMatthew Brost memset(&sr->pool, 0, sizeof(sr->pool)); 42dd08ebf6SMatthew Brost sr->pool.grow_step = XE_REG_SR_GROW_STEP_DEFAULT; 43dd08ebf6SMatthew Brost sr->name = name; 44dd08ebf6SMatthew Brost 45dd08ebf6SMatthew Brost return drmm_add_action_or_reset(&xe->drm, reg_sr_fini, sr); 46dd08ebf6SMatthew Brost } 474cc04402SLucas De Marchi EXPORT_SYMBOL_IF_KUNIT(xe_reg_sr_init); 48dd08ebf6SMatthew Brost 49dd08ebf6SMatthew Brost static struct xe_reg_sr_entry *alloc_entry(struct xe_reg_sr *sr) 50dd08ebf6SMatthew Brost { 51dd08ebf6SMatthew Brost if (sr->pool.used == sr->pool.allocated) { 52dd08ebf6SMatthew Brost struct xe_reg_sr_entry *arr; 53dd08ebf6SMatthew Brost 54dd08ebf6SMatthew Brost arr = krealloc_array(sr->pool.arr, 55dd08ebf6SMatthew Brost ALIGN(sr->pool.allocated + 1, sr->pool.grow_step), 56dd08ebf6SMatthew Brost sizeof(*arr), GFP_KERNEL); 57dd08ebf6SMatthew Brost if (!arr) 58dd08ebf6SMatthew Brost return NULL; 59dd08ebf6SMatthew Brost 60dd08ebf6SMatthew Brost sr->pool.arr = arr; 61dd08ebf6SMatthew Brost sr->pool.allocated += sr->pool.grow_step; 62dd08ebf6SMatthew Brost } 63dd08ebf6SMatthew Brost 64dd08ebf6SMatthew Brost return &sr->pool.arr[sr->pool.used++]; 65dd08ebf6SMatthew Brost } 66dd08ebf6SMatthew Brost 67dd08ebf6SMatthew Brost static bool compatible_entries(const struct xe_reg_sr_entry *e1, 68dd08ebf6SMatthew Brost const struct xe_reg_sr_entry *e2) 69dd08ebf6SMatthew Brost { 70dd08ebf6SMatthew Brost /* 71dd08ebf6SMatthew Brost * Don't allow overwriting values: clr_bits/set_bits should be disjoint 72dd08ebf6SMatthew Brost * when operating in the same register 73dd08ebf6SMatthew Brost */ 74dd08ebf6SMatthew Brost if (e1->clr_bits & e2->clr_bits || e1->set_bits & e2->set_bits || 75dd08ebf6SMatthew Brost e1->clr_bits & e2->set_bits || e1->set_bits & e2->clr_bits) 76dd08ebf6SMatthew Brost return false; 77dd08ebf6SMatthew Brost 7807fbd1f8SLucas De Marchi if (e1->reg.raw != e2->reg.raw) 79dd08ebf6SMatthew Brost return false; 80dd08ebf6SMatthew Brost 81dd08ebf6SMatthew Brost return true; 82dd08ebf6SMatthew Brost } 83dd08ebf6SMatthew Brost 847bf350ecSLucas De Marchi static void reg_sr_inc_error(struct xe_reg_sr *sr) 857bf350ecSLucas De Marchi { 867bf350ecSLucas De Marchi #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST) 877bf350ecSLucas De Marchi sr->errors++; 887bf350ecSLucas De Marchi #endif 897bf350ecSLucas De Marchi } 907bf350ecSLucas De Marchi 9107fbd1f8SLucas De Marchi int xe_reg_sr_add(struct xe_reg_sr *sr, 92dd08ebf6SMatthew Brost const struct xe_reg_sr_entry *e) 93dd08ebf6SMatthew Brost { 94*ee21379aSLucas De Marchi unsigned long idx = e->reg.addr; 95dd08ebf6SMatthew Brost struct xe_reg_sr_entry *pentry = xa_load(&sr->xa, idx); 96dd08ebf6SMatthew Brost int ret; 97dd08ebf6SMatthew Brost 98dd08ebf6SMatthew Brost if (pentry) { 99dd08ebf6SMatthew Brost if (!compatible_entries(pentry, e)) { 100dd08ebf6SMatthew Brost ret = -EINVAL; 101dd08ebf6SMatthew Brost goto fail; 102dd08ebf6SMatthew Brost } 103dd08ebf6SMatthew Brost 104dd08ebf6SMatthew Brost pentry->clr_bits |= e->clr_bits; 105dd08ebf6SMatthew Brost pentry->set_bits |= e->set_bits; 106dd08ebf6SMatthew Brost pentry->read_mask |= e->read_mask; 107dd08ebf6SMatthew Brost 108dd08ebf6SMatthew Brost return 0; 109dd08ebf6SMatthew Brost } 110dd08ebf6SMatthew Brost 111dd08ebf6SMatthew Brost pentry = alloc_entry(sr); 112dd08ebf6SMatthew Brost if (!pentry) { 113dd08ebf6SMatthew Brost ret = -ENOMEM; 114dd08ebf6SMatthew Brost goto fail; 115dd08ebf6SMatthew Brost } 116dd08ebf6SMatthew Brost 117dd08ebf6SMatthew Brost *pentry = *e; 118dd08ebf6SMatthew Brost ret = xa_err(xa_store(&sr->xa, idx, pentry, GFP_KERNEL)); 119dd08ebf6SMatthew Brost if (ret) 120dd08ebf6SMatthew Brost goto fail; 121dd08ebf6SMatthew Brost 122dd08ebf6SMatthew Brost return 0; 123dd08ebf6SMatthew Brost 124dd08ebf6SMatthew Brost fail: 12507fbd1f8SLucas De Marchi DRM_ERROR("Discarding save-restore reg %04lx (clear: %08x, set: %08x, masked: %s, mcr: %s): ret=%d\n", 126dd08ebf6SMatthew Brost idx, e->clr_bits, e->set_bits, 12707fbd1f8SLucas De Marchi str_yes_no(e->reg.masked), 12807fbd1f8SLucas De Marchi str_yes_no(e->reg.mcr), 12907fbd1f8SLucas De Marchi ret); 1307bf350ecSLucas De Marchi reg_sr_inc_error(sr); 131dd08ebf6SMatthew Brost 132dd08ebf6SMatthew Brost return ret; 133dd08ebf6SMatthew Brost } 134dd08ebf6SMatthew Brost 13507fbd1f8SLucas De Marchi /* 13607fbd1f8SLucas De Marchi * Convert back from encoded value to type-safe, only to be used when reg.mcr 13707fbd1f8SLucas De Marchi * is true 13807fbd1f8SLucas De Marchi */ 13907fbd1f8SLucas De Marchi static struct xe_reg_mcr to_xe_reg_mcr(const struct xe_reg reg) 14007fbd1f8SLucas De Marchi { 14107fbd1f8SLucas De Marchi return (const struct xe_reg_mcr){.__reg.raw = reg.raw }; 14207fbd1f8SLucas De Marchi } 14307fbd1f8SLucas De Marchi 14407fbd1f8SLucas De Marchi static void apply_one_mmio(struct xe_gt *gt, struct xe_reg_sr_entry *entry) 145dd08ebf6SMatthew Brost { 146dd08ebf6SMatthew Brost struct xe_device *xe = gt_to_xe(gt); 14707fbd1f8SLucas De Marchi struct xe_reg reg = entry->reg; 14807fbd1f8SLucas De Marchi struct xe_reg_mcr reg_mcr = to_xe_reg_mcr(reg); 149dd08ebf6SMatthew Brost u32 val; 150dd08ebf6SMatthew Brost 151dd08ebf6SMatthew Brost /* 152dd08ebf6SMatthew Brost * If this is a masked register, need to figure what goes on the upper 153dd08ebf6SMatthew Brost * 16 bits: it's either the clr_bits (when using FIELD_SET and WR) or 154dd08ebf6SMatthew Brost * the set_bits, when using SET. 155dd08ebf6SMatthew Brost * 156dd08ebf6SMatthew Brost * When it's not masked, we have to read it from hardware, unless we are 157dd08ebf6SMatthew Brost * supposed to set all bits. 158dd08ebf6SMatthew Brost */ 15907fbd1f8SLucas De Marchi if (reg.masked) 16079f2432eSMatt Roper val = (entry->clr_bits ?: entry->set_bits) << 16; 161dd08ebf6SMatthew Brost else if (entry->clr_bits + 1) 16207fbd1f8SLucas De Marchi val = (reg.mcr ? 16307fbd1f8SLucas De Marchi xe_gt_mcr_unicast_read_any(gt, reg_mcr) : 164ce8bf5bdSLucas De Marchi xe_mmio_read32(gt, reg)) & (~entry->clr_bits); 165dd08ebf6SMatthew Brost else 166dd08ebf6SMatthew Brost val = 0; 167dd08ebf6SMatthew Brost 168dd08ebf6SMatthew Brost /* 169dd08ebf6SMatthew Brost * TODO: add selftest to validate all tables, regardless of platform: 170dd08ebf6SMatthew Brost * - Masked registers can't have set_bits with upper bits set 171dd08ebf6SMatthew Brost * - set_bits must be contained in clr_bits 172dd08ebf6SMatthew Brost */ 173dd08ebf6SMatthew Brost val |= entry->set_bits; 174dd08ebf6SMatthew Brost 175*ee21379aSLucas De Marchi drm_dbg(&xe->drm, "REG[0x%x] = 0x%08x", reg.addr, val); 176dd08ebf6SMatthew Brost 17707fbd1f8SLucas De Marchi if (entry->reg.mcr) 17807fbd1f8SLucas De Marchi xe_gt_mcr_multicast_write(gt, reg_mcr, val); 179dd08ebf6SMatthew Brost else 180ce8bf5bdSLucas De Marchi xe_mmio_write32(gt, reg, val); 181dd08ebf6SMatthew Brost } 182dd08ebf6SMatthew Brost 183dd08ebf6SMatthew Brost void xe_reg_sr_apply_mmio(struct xe_reg_sr *sr, struct xe_gt *gt) 184dd08ebf6SMatthew Brost { 185dd08ebf6SMatthew Brost struct xe_device *xe = gt_to_xe(gt); 186dd08ebf6SMatthew Brost struct xe_reg_sr_entry *entry; 187dd08ebf6SMatthew Brost unsigned long reg; 188dd08ebf6SMatthew Brost int err; 189dd08ebf6SMatthew Brost 1905be84050SLucas De Marchi if (xa_empty(&sr->xa)) 1915be84050SLucas De Marchi return; 1925be84050SLucas De Marchi 193dd08ebf6SMatthew Brost drm_dbg(&xe->drm, "Applying %s save-restore MMIOs\n", sr->name); 194dd08ebf6SMatthew Brost 195dd08ebf6SMatthew Brost err = xe_force_wake_get(>->mmio.fw, XE_FORCEWAKE_ALL); 196dd08ebf6SMatthew Brost if (err) 197dd08ebf6SMatthew Brost goto err_force_wake; 198dd08ebf6SMatthew Brost 199dd08ebf6SMatthew Brost xa_for_each(&sr->xa, reg, entry) 20007fbd1f8SLucas De Marchi apply_one_mmio(gt, entry); 201dd08ebf6SMatthew Brost 202dd08ebf6SMatthew Brost err = xe_force_wake_put(>->mmio.fw, XE_FORCEWAKE_ALL); 203dd08ebf6SMatthew Brost XE_WARN_ON(err); 204dd08ebf6SMatthew Brost 205dd08ebf6SMatthew Brost return; 206dd08ebf6SMatthew Brost 207dd08ebf6SMatthew Brost err_force_wake: 208dd08ebf6SMatthew Brost drm_err(&xe->drm, "Failed to apply, err=%d\n", err); 209dd08ebf6SMatthew Brost } 210dd08ebf6SMatthew Brost 211dd08ebf6SMatthew Brost void xe_reg_sr_apply_whitelist(struct xe_reg_sr *sr, u32 mmio_base, 212dd08ebf6SMatthew Brost struct xe_gt *gt) 213dd08ebf6SMatthew Brost { 214dd08ebf6SMatthew Brost struct xe_device *xe = gt_to_xe(gt); 215dd08ebf6SMatthew Brost struct xe_reg_sr_entry *entry; 216d855d224SLucas De Marchi struct drm_printer p; 217dd08ebf6SMatthew Brost unsigned long reg; 218dd08ebf6SMatthew Brost unsigned int slot = 0; 219dd08ebf6SMatthew Brost int err; 220dd08ebf6SMatthew Brost 2215be84050SLucas De Marchi if (xa_empty(&sr->xa)) 2225be84050SLucas De Marchi return; 2235be84050SLucas De Marchi 224dd08ebf6SMatthew Brost drm_dbg(&xe->drm, "Whitelisting %s registers\n", sr->name); 225dd08ebf6SMatthew Brost 226dd08ebf6SMatthew Brost err = xe_force_wake_get(>->mmio.fw, XE_FORCEWAKE_ALL); 227dd08ebf6SMatthew Brost if (err) 228dd08ebf6SMatthew Brost goto err_force_wake; 229dd08ebf6SMatthew Brost 230d855d224SLucas De Marchi p = drm_debug_printer(KBUILD_MODNAME); 231dd08ebf6SMatthew Brost xa_for_each(&sr->xa, reg, entry) { 232d855d224SLucas De Marchi xe_reg_whitelist_print_entry(&p, 0, reg, entry); 233ce8bf5bdSLucas De Marchi xe_mmio_write32(gt, RING_FORCE_TO_NONPRIV(mmio_base, slot), 234dd08ebf6SMatthew Brost reg | entry->set_bits); 235dd08ebf6SMatthew Brost slot++; 236dd08ebf6SMatthew Brost } 237dd08ebf6SMatthew Brost 238dd08ebf6SMatthew Brost /* And clear the rest just in case of garbage */ 239ce8bf5bdSLucas De Marchi for (; slot < RING_MAX_NONPRIV_SLOTS; slot++) { 240*ee21379aSLucas De Marchi u32 addr = RING_NOPID(mmio_base).addr; 241ce8bf5bdSLucas De Marchi 242ce8bf5bdSLucas De Marchi xe_mmio_write32(gt, RING_FORCE_TO_NONPRIV(mmio_base, slot), addr); 243ce8bf5bdSLucas De Marchi } 244dd08ebf6SMatthew Brost 245dd08ebf6SMatthew Brost err = xe_force_wake_put(>->mmio.fw, XE_FORCEWAKE_ALL); 246dd08ebf6SMatthew Brost XE_WARN_ON(err); 247dd08ebf6SMatthew Brost 248dd08ebf6SMatthew Brost return; 249dd08ebf6SMatthew Brost 250dd08ebf6SMatthew Brost err_force_wake: 251dd08ebf6SMatthew Brost drm_err(&xe->drm, "Failed to apply, err=%d\n", err); 252dd08ebf6SMatthew Brost } 2536647e2feSLucas De Marchi 2546647e2feSLucas De Marchi /** 2556647e2feSLucas De Marchi * xe_reg_sr_dump - print all save/restore entries 2566647e2feSLucas De Marchi * @sr: Save/restore entries 2576647e2feSLucas De Marchi * @p: DRM printer 2586647e2feSLucas De Marchi */ 2596647e2feSLucas De Marchi void xe_reg_sr_dump(struct xe_reg_sr *sr, struct drm_printer *p) 2606647e2feSLucas De Marchi { 2616647e2feSLucas De Marchi struct xe_reg_sr_entry *entry; 2626647e2feSLucas De Marchi unsigned long reg; 2636647e2feSLucas De Marchi 2646647e2feSLucas De Marchi if (!sr->name || xa_empty(&sr->xa)) 2656647e2feSLucas De Marchi return; 2666647e2feSLucas De Marchi 2676647e2feSLucas De Marchi drm_printf(p, "%s\n", sr->name); 2686647e2feSLucas De Marchi xa_for_each(&sr->xa, reg, entry) 2696647e2feSLucas De Marchi drm_printf(p, "\tREG[0x%lx] clr=0x%08x set=0x%08x masked=%s mcr=%s\n", 2706647e2feSLucas De Marchi reg, entry->clr_bits, entry->set_bits, 27107fbd1f8SLucas De Marchi str_yes_no(entry->reg.masked), 27207fbd1f8SLucas De Marchi str_yes_no(entry->reg.mcr)); 2736647e2feSLucas De Marchi } 274