xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_umc.c (revision 90d32e92011eaae8e70a9169b4e7acf4ca8f9d3a)
1 /*
2  * Copyright 2019 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 
24 #include <linux/sort.h>
25 #include "amdgpu.h"
26 #include "umc_v6_7.h"
27 #define MAX_UMC_POISON_POLLING_TIME_SYNC   20  //ms
28 
29 #define MAX_UMC_HASH_STRING_SIZE  256
30 
31 static int amdgpu_umc_convert_error_address(struct amdgpu_device *adev,
32 				    struct ras_err_data *err_data, uint64_t err_addr,
33 				    uint32_t ch_inst, uint32_t umc_inst)
34 {
35 	switch (amdgpu_ip_version(adev, UMC_HWIP, 0)) {
36 	case IP_VERSION(6, 7, 0):
37 		umc_v6_7_convert_error_address(adev,
38 				err_data, err_addr, ch_inst, umc_inst);
39 		break;
40 	default:
41 		dev_warn(adev->dev,
42 			 "UMC address to Physical address translation is not supported\n");
43 		return AMDGPU_RAS_FAIL;
44 	}
45 
46 	return AMDGPU_RAS_SUCCESS;
47 }
48 
49 int amdgpu_umc_page_retirement_mca(struct amdgpu_device *adev,
50 			uint64_t err_addr, uint32_t ch_inst, uint32_t umc_inst)
51 {
52 	struct ras_err_data err_data;
53 	int ret;
54 
55 	ret = amdgpu_ras_error_data_init(&err_data);
56 	if (ret)
57 		return ret;
58 
59 	err_data.err_addr =
60 		kcalloc(adev->umc.max_ras_err_cnt_per_query,
61 			sizeof(struct eeprom_table_record), GFP_KERNEL);
62 	if (!err_data.err_addr) {
63 		dev_warn(adev->dev,
64 			"Failed to alloc memory for umc error record in MCA notifier!\n");
65 		ret = AMDGPU_RAS_FAIL;
66 		goto out_fini_err_data;
67 	}
68 
69 	err_data.err_addr_len = adev->umc.max_ras_err_cnt_per_query;
70 
71 	/*
72 	 * Translate UMC channel address to Physical address
73 	 */
74 	ret = amdgpu_umc_convert_error_address(adev, &err_data, err_addr,
75 					ch_inst, umc_inst);
76 	if (ret)
77 		goto out_free_err_addr;
78 
79 	if (amdgpu_bad_page_threshold != 0) {
80 		amdgpu_ras_add_bad_pages(adev, err_data.err_addr,
81 						err_data.err_addr_cnt);
82 		amdgpu_ras_save_bad_pages(adev, NULL);
83 	}
84 
85 out_free_err_addr:
86 	kfree(err_data.err_addr);
87 
88 out_fini_err_data:
89 	amdgpu_ras_error_data_fini(&err_data);
90 
91 	return ret;
92 }
93 
94 void amdgpu_umc_handle_bad_pages(struct amdgpu_device *adev,
95 			void *ras_error_status)
96 {
97 	struct ras_err_data *err_data = (struct ras_err_data *)ras_error_status;
98 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
99 	unsigned int error_query_mode;
100 	int ret = 0;
101 	unsigned long err_count;
102 
103 	amdgpu_ras_get_error_query_mode(adev, &error_query_mode);
104 
105 	mutex_lock(&con->page_retirement_lock);
106 	ret = amdgpu_dpm_get_ecc_info(adev, (void *)&(con->umc_ecc));
107 	if (ret == -EOPNOTSUPP &&
108 	    error_query_mode == AMDGPU_RAS_DIRECT_ERROR_QUERY) {
109 		if (adev->umc.ras && adev->umc.ras->ras_block.hw_ops &&
110 		    adev->umc.ras->ras_block.hw_ops->query_ras_error_count)
111 		    adev->umc.ras->ras_block.hw_ops->query_ras_error_count(adev, ras_error_status);
112 
113 		if (adev->umc.ras && adev->umc.ras->ras_block.hw_ops &&
114 		    adev->umc.ras->ras_block.hw_ops->query_ras_error_address &&
115 		    adev->umc.max_ras_err_cnt_per_query) {
116 			err_data->err_addr =
117 				kcalloc(adev->umc.max_ras_err_cnt_per_query,
118 					sizeof(struct eeprom_table_record), GFP_KERNEL);
119 
120 			/* still call query_ras_error_address to clear error status
121 			 * even NOMEM error is encountered
122 			 */
123 			if(!err_data->err_addr)
124 				dev_warn(adev->dev, "Failed to alloc memory for "
125 						"umc error address record!\n");
126 			else
127 				err_data->err_addr_len = adev->umc.max_ras_err_cnt_per_query;
128 
129 			/* umc query_ras_error_address is also responsible for clearing
130 			 * error status
131 			 */
132 			adev->umc.ras->ras_block.hw_ops->query_ras_error_address(adev, ras_error_status);
133 		}
134 	} else if (error_query_mode == AMDGPU_RAS_FIRMWARE_ERROR_QUERY ||
135 	    (!ret && error_query_mode == AMDGPU_RAS_DIRECT_ERROR_QUERY)) {
136 		if (adev->umc.ras &&
137 		    adev->umc.ras->ecc_info_query_ras_error_count)
138 		    adev->umc.ras->ecc_info_query_ras_error_count(adev, ras_error_status);
139 
140 		if (adev->umc.ras &&
141 		    adev->umc.ras->ecc_info_query_ras_error_address &&
142 		    adev->umc.max_ras_err_cnt_per_query) {
143 			err_data->err_addr =
144 				kcalloc(adev->umc.max_ras_err_cnt_per_query,
145 					sizeof(struct eeprom_table_record), GFP_KERNEL);
146 
147 			/* still call query_ras_error_address to clear error status
148 			 * even NOMEM error is encountered
149 			 */
150 			if(!err_data->err_addr)
151 				dev_warn(adev->dev, "Failed to alloc memory for "
152 						"umc error address record!\n");
153 			else
154 				err_data->err_addr_len = adev->umc.max_ras_err_cnt_per_query;
155 
156 			/* umc query_ras_error_address is also responsible for clearing
157 			 * error status
158 			 */
159 			adev->umc.ras->ecc_info_query_ras_error_address(adev, ras_error_status);
160 		}
161 	}
162 
163 	/* only uncorrectable error needs gpu reset */
164 	if (err_data->ue_count || err_data->de_count) {
165 		err_count = err_data->ue_count + err_data->de_count;
166 		if ((amdgpu_bad_page_threshold != 0) &&
167 			err_data->err_addr_cnt) {
168 			amdgpu_ras_add_bad_pages(adev, err_data->err_addr,
169 						err_data->err_addr_cnt);
170 			amdgpu_ras_save_bad_pages(adev, &err_count);
171 
172 			amdgpu_dpm_send_hbm_bad_pages_num(adev, con->eeprom_control.ras_num_recs);
173 
174 			if (con->update_channel_flag == true) {
175 				amdgpu_dpm_send_hbm_bad_channel_flag(adev, con->eeprom_control.bad_channel_bitmap);
176 				con->update_channel_flag = false;
177 			}
178 		}
179 	}
180 
181 	kfree(err_data->err_addr);
182 	err_data->err_addr = NULL;
183 
184 	mutex_unlock(&con->page_retirement_lock);
185 }
186 
187 static int amdgpu_umc_do_page_retirement(struct amdgpu_device *adev,
188 		void *ras_error_status,
189 		struct amdgpu_iv_entry *entry,
190 		uint32_t reset)
191 {
192 	struct ras_err_data *err_data = (struct ras_err_data *)ras_error_status;
193 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
194 
195 	kgd2kfd_set_sram_ecc_flag(adev->kfd.dev);
196 	amdgpu_umc_handle_bad_pages(adev, ras_error_status);
197 
198 	if (err_data->ue_count && reset) {
199 		con->gpu_reset_flags |= reset;
200 		amdgpu_ras_reset_gpu(adev);
201 	}
202 
203 	return AMDGPU_RAS_SUCCESS;
204 }
205 
206 int amdgpu_umc_bad_page_polling_timeout(struct amdgpu_device *adev,
207 			uint32_t reset, uint32_t timeout_ms)
208 {
209 	struct ras_err_data err_data;
210 	struct ras_common_if head = {
211 		.block = AMDGPU_RAS_BLOCK__UMC,
212 	};
213 	struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head);
214 	uint32_t timeout = timeout_ms;
215 
216 	memset(&err_data, 0, sizeof(err_data));
217 	amdgpu_ras_error_data_init(&err_data);
218 
219 	do {
220 
221 		amdgpu_umc_handle_bad_pages(adev, &err_data);
222 
223 		if (timeout && !err_data.de_count) {
224 			msleep(1);
225 			timeout--;
226 		}
227 
228 	} while (timeout && !err_data.de_count);
229 
230 	if (!timeout)
231 		dev_warn(adev->dev, "Can't find bad pages\n");
232 
233 	if (err_data.de_count)
234 		dev_info(adev->dev, "%ld new deferred hardware errors detected\n", err_data.de_count);
235 
236 	if (obj) {
237 		obj->err_data.ue_count += err_data.ue_count;
238 		obj->err_data.ce_count += err_data.ce_count;
239 		obj->err_data.de_count += err_data.de_count;
240 	}
241 
242 	amdgpu_ras_error_data_fini(&err_data);
243 
244 	kgd2kfd_set_sram_ecc_flag(adev->kfd.dev);
245 
246 	if (reset) {
247 		struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
248 
249 		con->gpu_reset_flags |= reset;
250 		amdgpu_ras_reset_gpu(adev);
251 	}
252 
253 	return 0;
254 }
255 
256 int amdgpu_umc_pasid_poison_handler(struct amdgpu_device *adev,
257 			enum amdgpu_ras_block block, uint16_t pasid,
258 			pasid_notify pasid_fn, void *data, uint32_t reset)
259 {
260 	int ret = AMDGPU_RAS_SUCCESS;
261 
262 	if (adev->gmc.xgmi.connected_to_cpu ||
263 		adev->gmc.is_app_apu) {
264 		if (reset) {
265 			/* MCA poison handler is only responsible for GPU reset,
266 			 * let MCA notifier do page retirement.
267 			 */
268 			kgd2kfd_set_sram_ecc_flag(adev->kfd.dev);
269 			amdgpu_ras_reset_gpu(adev);
270 		}
271 		return ret;
272 	}
273 
274 	if (!amdgpu_sriov_vf(adev)) {
275 		if (amdgpu_ip_version(adev, UMC_HWIP, 0) < IP_VERSION(12, 0, 0)) {
276 			struct ras_err_data err_data;
277 			struct ras_common_if head = {
278 				.block = AMDGPU_RAS_BLOCK__UMC,
279 			};
280 			struct ras_manager *obj = amdgpu_ras_find_obj(adev, &head);
281 
282 			ret = amdgpu_ras_error_data_init(&err_data);
283 			if (ret)
284 				return ret;
285 
286 			ret = amdgpu_umc_do_page_retirement(adev, &err_data, NULL, reset);
287 
288 			if (ret == AMDGPU_RAS_SUCCESS && obj) {
289 				obj->err_data.ue_count += err_data.ue_count;
290 				obj->err_data.ce_count += err_data.ce_count;
291 				obj->err_data.de_count += err_data.de_count;
292 			}
293 
294 			amdgpu_ras_error_data_fini(&err_data);
295 		} else {
296 				struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
297 
298 				amdgpu_ras_put_poison_req(adev,
299 					block, pasid, pasid_fn, data, reset);
300 
301 				atomic_inc(&con->page_retirement_req_cnt);
302 
303 				wake_up(&con->page_retirement_wq);
304 		}
305 	} else {
306 		if (adev->virt.ops && adev->virt.ops->ras_poison_handler)
307 			adev->virt.ops->ras_poison_handler(adev, block);
308 		else
309 			dev_warn(adev->dev,
310 				"No ras_poison_handler interface in SRIOV!\n");
311 	}
312 
313 	return ret;
314 }
315 
316 int amdgpu_umc_poison_handler(struct amdgpu_device *adev,
317 			enum amdgpu_ras_block block, uint32_t reset)
318 {
319 	return amdgpu_umc_pasid_poison_handler(adev,
320 				block, 0, NULL, NULL, reset);
321 }
322 
323 int amdgpu_umc_process_ras_data_cb(struct amdgpu_device *adev,
324 		void *ras_error_status,
325 		struct amdgpu_iv_entry *entry)
326 {
327 	return amdgpu_umc_do_page_retirement(adev, ras_error_status, entry,
328 				AMDGPU_RAS_GPU_RESET_MODE1_RESET);
329 }
330 
331 int amdgpu_umc_ras_sw_init(struct amdgpu_device *adev)
332 {
333 	int err;
334 	struct amdgpu_umc_ras *ras;
335 
336 	if (!adev->umc.ras)
337 		return 0;
338 
339 	ras = adev->umc.ras;
340 
341 	err = amdgpu_ras_register_ras_block(adev, &ras->ras_block);
342 	if (err) {
343 		dev_err(adev->dev, "Failed to register umc ras block!\n");
344 		return err;
345 	}
346 
347 	strcpy(adev->umc.ras->ras_block.ras_comm.name, "umc");
348 	ras->ras_block.ras_comm.block = AMDGPU_RAS_BLOCK__UMC;
349 	ras->ras_block.ras_comm.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
350 	adev->umc.ras_if = &ras->ras_block.ras_comm;
351 
352 	if (!ras->ras_block.ras_late_init)
353 		ras->ras_block.ras_late_init = amdgpu_umc_ras_late_init;
354 
355 	if (!ras->ras_block.ras_cb)
356 		ras->ras_block.ras_cb = amdgpu_umc_process_ras_data_cb;
357 
358 	return 0;
359 }
360 
361 int amdgpu_umc_ras_late_init(struct amdgpu_device *adev, struct ras_common_if *ras_block)
362 {
363 	int r;
364 
365 	r = amdgpu_ras_block_late_init(adev, ras_block);
366 	if (r)
367 		return r;
368 
369 	if (amdgpu_ras_is_supported(adev, ras_block->block)) {
370 		r = amdgpu_irq_get(adev, &adev->gmc.ecc_irq, 0);
371 		if (r)
372 			goto late_fini;
373 	}
374 
375 	/* ras init of specific umc version */
376 	if (adev->umc.ras &&
377 	    adev->umc.ras->err_cnt_init)
378 		adev->umc.ras->err_cnt_init(adev);
379 
380 	return 0;
381 
382 late_fini:
383 	amdgpu_ras_block_late_fini(adev, ras_block);
384 	return r;
385 }
386 
387 int amdgpu_umc_process_ecc_irq(struct amdgpu_device *adev,
388 		struct amdgpu_irq_src *source,
389 		struct amdgpu_iv_entry *entry)
390 {
391 	struct ras_common_if *ras_if = adev->umc.ras_if;
392 	struct ras_dispatch_if ih_data = {
393 		.entry = entry,
394 	};
395 
396 	if (!ras_if)
397 		return 0;
398 
399 	ih_data.head = *ras_if;
400 
401 	amdgpu_ras_interrupt_dispatch(adev, &ih_data);
402 	return 0;
403 }
404 
405 int amdgpu_umc_fill_error_record(struct ras_err_data *err_data,
406 		uint64_t err_addr,
407 		uint64_t retired_page,
408 		uint32_t channel_index,
409 		uint32_t umc_inst)
410 {
411 	struct eeprom_table_record *err_rec;
412 
413 	if (!err_data ||
414 	    !err_data->err_addr ||
415 	    (err_data->err_addr_cnt >= err_data->err_addr_len))
416 		return -EINVAL;
417 
418 	err_rec = &err_data->err_addr[err_data->err_addr_cnt];
419 
420 	err_rec->address = err_addr;
421 	/* page frame address is saved */
422 	err_rec->retired_page = retired_page >> AMDGPU_GPU_PAGE_SHIFT;
423 	err_rec->ts = (uint64_t)ktime_get_real_seconds();
424 	err_rec->err_type = AMDGPU_RAS_EEPROM_ERR_NON_RECOVERABLE;
425 	err_rec->cu = 0;
426 	err_rec->mem_channel = channel_index;
427 	err_rec->mcumc_id = umc_inst;
428 
429 	err_data->err_addr_cnt++;
430 
431 	return 0;
432 }
433 
434 int amdgpu_umc_loop_channels(struct amdgpu_device *adev,
435 			umc_func func, void *data)
436 {
437 	uint32_t node_inst       = 0;
438 	uint32_t umc_inst        = 0;
439 	uint32_t ch_inst         = 0;
440 	int ret = 0;
441 
442 	if (adev->umc.node_inst_num) {
443 		LOOP_UMC_EACH_NODE_INST_AND_CH(node_inst, umc_inst, ch_inst) {
444 			ret = func(adev, node_inst, umc_inst, ch_inst, data);
445 			if (ret) {
446 				dev_err(adev->dev, "Node %d umc %d ch %d func returns %d\n",
447 					node_inst, umc_inst, ch_inst, ret);
448 				return ret;
449 			}
450 		}
451 	} else {
452 		LOOP_UMC_INST_AND_CH(umc_inst, ch_inst) {
453 			ret = func(adev, 0, umc_inst, ch_inst, data);
454 			if (ret) {
455 				dev_err(adev->dev, "Umc %d ch %d func returns %d\n",
456 					umc_inst, ch_inst, ret);
457 				return ret;
458 			}
459 		}
460 	}
461 
462 	return 0;
463 }
464 
465 int amdgpu_umc_update_ecc_status(struct amdgpu_device *adev,
466 				uint64_t status, uint64_t ipid, uint64_t addr)
467 {
468 	if (adev->umc.ras->update_ecc_status)
469 		return adev->umc.ras->update_ecc_status(adev,
470 					status, ipid, addr);
471 	return 0;
472 }
473 
474 static int amdgpu_umc_uint64_cmp(const void *a, const void *b)
475 {
476 	uint64_t *addr_a = (uint64_t *)a;
477 	uint64_t *addr_b = (uint64_t *)b;
478 
479 	if (*addr_a > *addr_b)
480 		return 1;
481 	else if (*addr_a < *addr_b)
482 		return -1;
483 	else
484 		return 0;
485 }
486 
487 /* Use string hash to avoid logging the same bad pages repeatedly */
488 int amdgpu_umc_build_pages_hash(struct amdgpu_device *adev,
489 		uint64_t *pfns, int len, uint64_t *val)
490 {
491 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
492 	char buf[MAX_UMC_HASH_STRING_SIZE] = {0};
493 	int offset = 0, i = 0;
494 	uint64_t hash_val;
495 
496 	if (!pfns || !len)
497 		return -EINVAL;
498 
499 	sort(pfns, len, sizeof(uint64_t), amdgpu_umc_uint64_cmp, NULL);
500 
501 	for (i = 0; i < len; i++)
502 		offset += snprintf(&buf[offset], sizeof(buf) - offset, "%llx", pfns[i]);
503 
504 	hash_val = siphash(buf, offset, &con->umc_ecc_log.ecc_key);
505 
506 	*val = hash_val;
507 
508 	return 0;
509 }
510 
511 int amdgpu_umc_logs_ecc_err(struct amdgpu_device *adev,
512 		struct radix_tree_root *ecc_tree, struct ras_ecc_err *ecc_err)
513 {
514 	struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
515 	struct ras_ecc_log_info *ecc_log;
516 	int ret;
517 
518 	ecc_log = &con->umc_ecc_log;
519 
520 	mutex_lock(&ecc_log->lock);
521 	ret = radix_tree_insert(ecc_tree, ecc_err->hash_index, ecc_err);
522 	if (!ret) {
523 		struct ras_err_pages *err_pages = &ecc_err->err_pages;
524 		int i;
525 
526 		/* Reserve memory */
527 		for (i = 0; i < err_pages->count; i++)
528 			amdgpu_ras_reserve_page(adev, err_pages->pfn[i]);
529 
530 		radix_tree_tag_set(ecc_tree,
531 			ecc_err->hash_index, UMC_ECC_NEW_DETECTED_TAG);
532 	}
533 	mutex_unlock(&ecc_log->lock);
534 
535 	return ret;
536 }
537