1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2017 Intel Corporation 4 */ 5 6 #include "i915_selftest.h" 7 #include "gem/i915_gem_pm.h" 8 9 /* max doorbell number + negative test for each client type */ 10 #define ATTEMPTS (GUC_NUM_DOORBELLS + GUC_CLIENT_PRIORITY_NUM) 11 12 static struct intel_guc_client *clients[ATTEMPTS]; 13 14 static bool available_dbs(struct intel_guc *guc, u32 priority) 15 { 16 unsigned long offset; 17 unsigned long end; 18 u16 id; 19 20 /* first half is used for normal priority, second half for high */ 21 offset = 0; 22 end = GUC_NUM_DOORBELLS / 2; 23 if (priority <= GUC_CLIENT_PRIORITY_HIGH) { 24 offset = end; 25 end += offset; 26 } 27 28 id = find_next_zero_bit(guc->doorbell_bitmap, end, offset); 29 if (id < end) 30 return true; 31 32 return false; 33 } 34 35 static int check_all_doorbells(struct intel_guc *guc) 36 { 37 u16 db_id; 38 39 pr_info_once("Max number of doorbells: %d", GUC_NUM_DOORBELLS); 40 for (db_id = 0; db_id < GUC_NUM_DOORBELLS; ++db_id) { 41 if (!doorbell_ok(guc, db_id)) { 42 pr_err("doorbell %d, not ok\n", db_id); 43 return -EIO; 44 } 45 } 46 47 return 0; 48 } 49 50 static int ring_doorbell_nop(struct intel_guc_client *client) 51 { 52 struct guc_process_desc *desc = __get_process_desc(client); 53 int err; 54 55 client->use_nop_wqi = true; 56 57 spin_lock_irq(&client->wq_lock); 58 59 guc_wq_item_append(client, 0, 0, 0, 0); 60 guc_ring_doorbell(client); 61 62 spin_unlock_irq(&client->wq_lock); 63 64 client->use_nop_wqi = false; 65 66 /* if there are no issues GuC will update the WQ head and keep the 67 * WQ in active status 68 */ 69 err = wait_for(READ_ONCE(desc->head) == READ_ONCE(desc->tail), 10); 70 if (err) { 71 pr_err("doorbell %u ring failed!\n", client->doorbell_id); 72 return -EIO; 73 } 74 75 if (desc->wq_status != WQ_STATUS_ACTIVE) { 76 pr_err("doorbell %u ring put WQ in bad state (%u)!\n", 77 client->doorbell_id, desc->wq_status); 78 return -EIO; 79 } 80 81 return 0; 82 } 83 84 /* 85 * Basic client sanity check, handy to validate create_clients. 86 */ 87 static int validate_client(struct intel_guc_client *client, int client_priority) 88 { 89 if (client->priority != client_priority || 90 client->doorbell_id == GUC_DOORBELL_INVALID) 91 return -EINVAL; 92 else 93 return 0; 94 } 95 96 static bool client_doorbell_in_sync(struct intel_guc_client *client) 97 { 98 return !client || doorbell_ok(client->guc, client->doorbell_id); 99 } 100 101 /* 102 * Check that we're able to synchronize guc_clients with their doorbells 103 * 104 * We're creating clients and reserving doorbells once, at module load. During 105 * module lifetime, GuC, doorbell HW, and i915 state may go out of sync due to 106 * GuC being reset. In other words - GuC clients are still around, but the 107 * status of their doorbells may be incorrect. This is the reason behind 108 * validating that the doorbells status expected by the driver matches what the 109 * GuC/HW have. 110 */ 111 static int igt_guc_clients(void *arg) 112 { 113 struct intel_gt *gt = arg; 114 struct intel_guc *guc = >->uc.guc; 115 intel_wakeref_t wakeref; 116 int err = 0; 117 118 GEM_BUG_ON(!HAS_GT_UC(gt->i915)); 119 wakeref = intel_runtime_pm_get(gt->uncore->rpm); 120 121 err = check_all_doorbells(guc); 122 if (err) 123 goto unlock; 124 125 /* 126 * Get rid of clients created during driver load because the test will 127 * recreate them. 128 */ 129 guc_clients_disable(guc); 130 guc_clients_destroy(guc); 131 if (guc->execbuf_client) { 132 pr_err("guc_clients_destroy lied!\n"); 133 err = -EINVAL; 134 goto unlock; 135 } 136 137 err = guc_clients_create(guc); 138 if (err) { 139 pr_err("Failed to create clients\n"); 140 goto unlock; 141 } 142 GEM_BUG_ON(!guc->execbuf_client); 143 144 err = validate_client(guc->execbuf_client, 145 GUC_CLIENT_PRIORITY_KMD_NORMAL); 146 if (err) { 147 pr_err("execbug client validation failed\n"); 148 goto out; 149 } 150 151 /* the client should now have reserved a doorbell */ 152 if (!has_doorbell(guc->execbuf_client)) { 153 pr_err("guc_clients_create didn't reserve doorbells\n"); 154 err = -EINVAL; 155 goto out; 156 } 157 158 /* Now enable the clients */ 159 guc_clients_enable(guc); 160 161 /* each client should now have received a doorbell */ 162 if (!client_doorbell_in_sync(guc->execbuf_client)) { 163 pr_err("failed to initialize the doorbells\n"); 164 err = -EINVAL; 165 goto out; 166 } 167 168 /* 169 * Basic test - an attempt to reallocate a valid doorbell to the 170 * client it is currently assigned should not cause a failure. 171 */ 172 err = create_doorbell(guc->execbuf_client); 173 174 out: 175 /* 176 * Leave clean state for other test, plus the driver always destroy the 177 * clients during unload. 178 */ 179 guc_clients_disable(guc); 180 guc_clients_destroy(guc); 181 guc_clients_create(guc); 182 guc_clients_enable(guc); 183 unlock: 184 intel_runtime_pm_put(gt->uncore->rpm, wakeref); 185 return err; 186 } 187 188 /* 189 * Create as many clients as number of doorbells. Note that there's already 190 * client(s)/doorbell(s) created during driver load, but this test creates 191 * its own and do not interact with the existing ones. 192 */ 193 static int igt_guc_doorbells(void *arg) 194 { 195 struct intel_gt *gt = arg; 196 struct intel_guc *guc = >->uc.guc; 197 intel_wakeref_t wakeref; 198 int i, err = 0; 199 u16 db_id; 200 201 GEM_BUG_ON(!HAS_GT_UC(gt->i915)); 202 wakeref = intel_runtime_pm_get(gt->uncore->rpm); 203 204 err = check_all_doorbells(guc); 205 if (err) 206 goto unlock; 207 208 for (i = 0; i < ATTEMPTS; i++) { 209 clients[i] = guc_client_alloc(guc, i % GUC_CLIENT_PRIORITY_NUM); 210 211 if (!clients[i]) { 212 pr_err("[%d] No guc client\n", i); 213 err = -EINVAL; 214 goto out; 215 } 216 217 if (IS_ERR(clients[i])) { 218 if (PTR_ERR(clients[i]) != -ENOSPC) { 219 pr_err("[%d] unexpected error\n", i); 220 err = PTR_ERR(clients[i]); 221 goto out; 222 } 223 224 if (available_dbs(guc, i % GUC_CLIENT_PRIORITY_NUM)) { 225 pr_err("[%d] non-db related alloc fail\n", i); 226 err = -EINVAL; 227 goto out; 228 } 229 230 /* expected, ran out of dbs for this client type */ 231 continue; 232 } 233 234 /* 235 * The check below is only valid because we keep a doorbell 236 * assigned during the whole life of the client. 237 */ 238 if (clients[i]->stage_id >= GUC_NUM_DOORBELLS) { 239 pr_err("[%d] more clients than doorbells (%d >= %d)\n", 240 i, clients[i]->stage_id, GUC_NUM_DOORBELLS); 241 err = -EINVAL; 242 goto out; 243 } 244 245 err = validate_client(clients[i], i % GUC_CLIENT_PRIORITY_NUM); 246 if (err) { 247 pr_err("[%d] client_alloc sanity check failed!\n", i); 248 err = -EINVAL; 249 goto out; 250 } 251 252 db_id = clients[i]->doorbell_id; 253 254 err = __guc_client_enable(clients[i]); 255 if (err) { 256 pr_err("[%d] Failed to create a doorbell\n", i); 257 goto out; 258 } 259 260 /* doorbell id shouldn't change, we are holding the mutex */ 261 if (db_id != clients[i]->doorbell_id) { 262 pr_err("[%d] doorbell id changed (%d != %d)\n", 263 i, db_id, clients[i]->doorbell_id); 264 err = -EINVAL; 265 goto out; 266 } 267 268 err = check_all_doorbells(guc); 269 if (err) 270 goto out; 271 272 err = ring_doorbell_nop(clients[i]); 273 if (err) 274 goto out; 275 } 276 277 out: 278 for (i = 0; i < ATTEMPTS; i++) 279 if (!IS_ERR_OR_NULL(clients[i])) { 280 __guc_client_disable(clients[i]); 281 guc_client_free(clients[i]); 282 } 283 unlock: 284 intel_runtime_pm_put(gt->uncore->rpm, wakeref); 285 return err; 286 } 287 288 int intel_guc_live_selftest(struct drm_i915_private *i915) 289 { 290 static const struct i915_subtest tests[] = { 291 SUBTEST(igt_guc_clients), 292 SUBTEST(igt_guc_doorbells), 293 }; 294 295 if (!USES_GUC_SUBMISSION(i915)) 296 return 0; 297 298 return intel_gt_live_subtests(tests, &i915->gt); 299 } 300