1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
// SPDX-License-Identifier: MIT
/*
* Copyright 2025 Advanced Micro Devices, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include "ras.h"
#include "ras_process.h"
#define RAS_EVENT_FIFO_SIZE (128 * sizeof(struct ras_event_req))
#define RAS_POLLING_ECC_TIMEOUT 300
static int ras_process_put_event(struct ras_core_context *ras_core,
struct ras_event_req *req)
{
struct ras_process *ras_proc = &ras_core->ras_proc;
int ret;
ret = kfifo_in_spinlocked(&ras_proc->event_fifo,
req, sizeof(*req), &ras_proc->fifo_spinlock);
if (!ret) {
RAS_DEV_ERR(ras_core->dev, "Poison message fifo is full!\n");
return -ENOSPC;
}
return 0;
}
static int ras_process_add_reset_gpu_event(struct ras_core_context *ras_core,
uint32_t reset_cause)
{
struct ras_event_req req = {0};
req.reset = reset_cause;
return ras_process_put_event(ras_core, &req);
}
static int ras_process_get_event(struct ras_core_context *ras_core,
struct ras_event_req *req)
{
struct ras_process *ras_proc = &ras_core->ras_proc;
return kfifo_out_spinlocked(&ras_proc->event_fifo,
req, sizeof(*req), &ras_proc->fifo_spinlock);
}
static void ras_process_clear_event_fifo(struct ras_core_context *ras_core)
{
struct ras_event_req req;
int ret;
do {
ret = ras_process_get_event(ras_core, &req);
} while (ret);
}
#define AMDGPU_RAS_WAITING_DATA_READY 200
static int ras_process_umc_event(struct ras_core_context *ras_core,
uint32_t event_count)
{
struct ras_ecc_count ecc_data;
int ret = 0;
uint32_t timeout = 0;
uint32_t detected_de_count = 0;
do {
memset(&ecc_data, 0, sizeof(ecc_data));
ret = ras_core_update_ecc_info(ras_core);
if (ret)
return ret;
ret = ras_core_query_block_ecc_data(ras_core, RAS_BLOCK_ID__UMC, &ecc_data);
if (ret)
return ret;
if (ecc_data.new_de_count) {
detected_de_count += ecc_data.new_de_count;
timeout = 0;
} else {
if (!timeout && event_count)
timeout = AMDGPU_RAS_WAITING_DATA_READY;
if (timeout) {
if (!--timeout)
break;
msleep(1);
}
}
} while (detected_de_count < event_count);
if (detected_de_count && ras_core_gpu_is_rma(ras_core))
ras_process_add_reset_gpu_event(ras_core, GPU_RESET_CAUSE_RMA);
return 0;
}
static int ras_process_non_umc_event(struct ras_core_context *ras_core)
{
struct ras_process *ras_proc = &ras_core->ras_proc;
struct ras_event_req req;
uint32_t event_count = kfifo_len(&ras_proc->event_fifo);
uint32_t reset_flags = 0;
int ret = 0, i;
for (i = 0; i < event_count; i++) {
memset(&req, 0, sizeof(req));
ret = ras_process_get_event(ras_core, &req);
if (!ret)
continue;
ras_core_event_notify(ras_core,
RAS_EVENT_ID__POISON_CONSUMPTION, &req);
reset_flags |= req.reset;
if (req.reset == GPU_RESET_CAUSE_RMA)
continue;
if (req.reset)
RAS_DEV_INFO(ras_core->dev,
"{%llu} GPU reset for %s RAS poison consumption is issued!\n",
req.seqno, ras_core_get_ras_block_name(req.block));
else
RAS_DEV_INFO(ras_core->dev,
"{%llu} %s RAS poison consumption is issued!\n",
req.seqno, ras_core_get_ras_block_name(req.block));
}
if (reset_flags) {
ret = ras_core_event_notify(ras_core,
RAS_EVENT_ID__RESET_GPU, &reset_flags);
if (!ret && (reset_flags & GPU_RESET_CAUSE_RMA))
return -RAS_CORE_GPU_IN_MODE1_RESET;
}
return ret;
}
int ras_process_handle_ras_event(struct ras_core_context *ras_core)
{
struct ras_process *ras_proc = &ras_core->ras_proc;
uint32_t umc_event_count;
int ret;
ret = ras_core_event_notify(ras_core,
RAS_EVENT_ID__RAS_EVENT_PROC_BEGIN, NULL);
if (ret)
return ret;
ras_aca_clear_fatal_flag(ras_core);
ras_umc_log_pending_bad_bank(ras_core);
do {
umc_event_count = atomic_read(&ras_proc->umc_interrupt_count);
ret = ras_process_umc_event(ras_core, umc_event_count);
if (ret == -RAS_CORE_GPU_IN_MODE1_RESET)
break;
if (umc_event_count)
atomic_sub(umc_event_count, &ras_proc->umc_interrupt_count);
} while (atomic_read(&ras_proc->umc_interrupt_count));
if ((ret != -RAS_CORE_GPU_IN_MODE1_RESET) &&
(kfifo_len(&ras_proc->event_fifo)))
ret = ras_process_non_umc_event(ras_core);
if (ret == -RAS_CORE_GPU_IN_MODE1_RESET) {
/* Clear poison fifo */
ras_process_clear_event_fifo(ras_core);
atomic_set(&ras_proc->umc_interrupt_count, 0);
}
ras_core_event_notify(ras_core,
RAS_EVENT_ID__RAS_EVENT_PROC_END, NULL);
return ret;
}
static int thread_wait_condition(void *param)
{
struct ras_process *ras_proc = (struct ras_process *)param;
return (kthread_should_stop() ||
atomic_read(&ras_proc->ras_interrupt_req));
}
static int ras_process_thread(void *context)
{
struct ras_core_context *ras_core = (struct ras_core_context *)context;
struct ras_process *ras_proc = &ras_core->ras_proc;
while (!kthread_should_stop()) {
ras_wait_event_interruptible_timeout(&ras_proc->ras_process_wq,
thread_wait_condition, ras_proc,
msecs_to_jiffies(RAS_POLLING_ECC_TIMEOUT));
if (kthread_should_stop())
break;
if (!ras_core->is_initialized)
continue;
atomic_set(&ras_proc->ras_interrupt_req, 0);
if (ras_core_gpu_in_reset(ras_core))
continue;
if (ras_core->sys_fn && ras_core->sys_fn->async_handle_ras_event)
ras_core->sys_fn->async_handle_ras_event(ras_core, NULL);
else
ras_process_handle_ras_event(ras_core);
}
return 0;
}
int ras_process_init(struct ras_core_context *ras_core)
{
struct ras_process *ras_proc = &ras_core->ras_proc;
int ret;
ret = kfifo_alloc(&ras_proc->event_fifo, RAS_EVENT_FIFO_SIZE, GFP_KERNEL);
if (ret)
return ret;
spin_lock_init(&ras_proc->fifo_spinlock);
init_waitqueue_head(&ras_proc->ras_process_wq);
ras_proc->ras_process_thread = kthread_run(ras_process_thread,
(void *)ras_core, "ras_process_thread");
if (!ras_proc->ras_process_thread) {
RAS_DEV_ERR(ras_core->dev, "Failed to create ras_process_thread.\n");
ret = -ENOMEM;
goto err;
}
return 0;
err:
ras_process_fini(ras_core);
return ret;
}
int ras_process_fini(struct ras_core_context *ras_core)
{
struct ras_process *ras_proc = &ras_core->ras_proc;
if (ras_proc->ras_process_thread) {
kthread_stop(ras_proc->ras_process_thread);
ras_proc->ras_process_thread = NULL;
}
kfifo_free(&ras_proc->event_fifo);
return 0;
}
static int ras_process_add_umc_interrupt_req(struct ras_core_context *ras_core,
struct ras_event_req *req)
{
struct ras_process *ras_proc = &ras_core->ras_proc;
atomic_inc(&ras_proc->umc_interrupt_count);
atomic_inc(&ras_proc->ras_interrupt_req);
wake_up(&ras_proc->ras_process_wq);
return 0;
}
static int ras_process_add_non_umc_interrupt_req(struct ras_core_context *ras_core,
struct ras_event_req *req)
{
struct ras_process *ras_proc = &ras_core->ras_proc;
int ret;
ret = ras_process_put_event(ras_core, req);
if (!ret) {
atomic_inc(&ras_proc->ras_interrupt_req);
wake_up(&ras_proc->ras_process_wq);
}
return ret;
}
int ras_process_add_interrupt_req(struct ras_core_context *ras_core,
struct ras_event_req *req, bool is_umc)
{
int ret;
if (!ras_core)
return -EINVAL;
if (!ras_core->is_initialized)
return -EPERM;
if (is_umc)
ret = ras_process_add_umc_interrupt_req(ras_core, req);
else
ret = ras_process_add_non_umc_interrupt_req(ras_core, req);
return ret;
}
|