1 /*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Dave Airlie
25 * Alex Deucher
26 * Jerome Glisse
27 */
28
29 #include <linux/aperture.h>
30 #include <linux/power_supply.h>
31 #include <linux/kthread.h>
32 #include <linux/module.h>
33 #include <linux/console.h>
34 #include <linux/slab.h>
35 #include <linux/iommu.h>
36 #include <linux/pci.h>
37 #include <linux/pci-p2pdma.h>
38 #include <linux/apple-gmux.h>
39
40 #include <drm/drm_atomic_helper.h>
41 #include <drm/drm_client_event.h>
42 #include <drm/drm_crtc_helper.h>
43 #include <drm/drm_probe_helper.h>
44 #include <drm/amdgpu_drm.h>
45 #include <linux/device.h>
46 #include <linux/vgaarb.h>
47 #include <linux/vga_switcheroo.h>
48 #include <linux/efi.h>
49 #include "amdgpu.h"
50 #include "amdgpu_trace.h"
51 #include "amdgpu_i2c.h"
52 #include "atom.h"
53 #include "amdgpu_atombios.h"
54 #include "amdgpu_atomfirmware.h"
55 #include "amd_pcie.h"
56 #ifdef CONFIG_DRM_AMDGPU_SI
57 #include "si.h"
58 #endif
59 #ifdef CONFIG_DRM_AMDGPU_CIK
60 #include "cik.h"
61 #endif
62 #include "vi.h"
63 #include "soc15.h"
64 #include "nv.h"
65 #include "bif/bif_4_1_d.h"
66 #include <linux/firmware.h>
67 #include "amdgpu_vf_error.h"
68
69 #include "amdgpu_amdkfd.h"
70 #include "amdgpu_pm.h"
71
72 #include "amdgpu_xgmi.h"
73 #include "amdgpu_ras.h"
74 #include "amdgpu_pmu.h"
75 #include "amdgpu_fru_eeprom.h"
76 #include "amdgpu_reset.h"
77 #include "amdgpu_virt.h"
78 #include "amdgpu_dev_coredump.h"
79
80 #include <linux/suspend.h>
81 #include <drm/task_barrier.h>
82 #include <linux/pm_runtime.h>
83
84 #include <drm/drm_drv.h>
85
86 #if IS_ENABLED(CONFIG_X86)
87 #include <asm/intel-family.h>
88 #include <asm/cpu_device_id.h>
89 #endif
90
91 MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
92 MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
93 MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
94 MODULE_FIRMWARE("amdgpu/picasso_gpu_info.bin");
95 MODULE_FIRMWARE("amdgpu/raven2_gpu_info.bin");
96 MODULE_FIRMWARE("amdgpu/arcturus_gpu_info.bin");
97 MODULE_FIRMWARE("amdgpu/navi12_gpu_info.bin");
98 MODULE_FIRMWARE("amdgpu/cyan_skillfish_gpu_info.bin");
99
100 #define AMDGPU_RESUME_MS 2000
101 #define AMDGPU_MAX_RETRY_LIMIT 2
102 #define AMDGPU_RETRY_SRIOV_RESET(r) ((r) == -EBUSY || (r) == -ETIMEDOUT || (r) == -EINVAL)
103 #define AMDGPU_PCIE_INDEX_FALLBACK (0x38 >> 2)
104 #define AMDGPU_PCIE_INDEX_HI_FALLBACK (0x44 >> 2)
105 #define AMDGPU_PCIE_DATA_FALLBACK (0x3C >> 2)
106
107 #define AMDGPU_VBIOS_SKIP (1U << 0)
108 #define AMDGPU_VBIOS_OPTIONAL (1U << 1)
109
110 static const struct drm_driver amdgpu_kms_driver;
111
112 const char *amdgpu_asic_name[] = {
113 "TAHITI",
114 "PITCAIRN",
115 "VERDE",
116 "OLAND",
117 "HAINAN",
118 "BONAIRE",
119 "KAVERI",
120 "KABINI",
121 "HAWAII",
122 "MULLINS",
123 "TOPAZ",
124 "TONGA",
125 "FIJI",
126 "CARRIZO",
127 "STONEY",
128 "POLARIS10",
129 "POLARIS11",
130 "POLARIS12",
131 "VEGAM",
132 "VEGA10",
133 "VEGA12",
134 "VEGA20",
135 "RAVEN",
136 "ARCTURUS",
137 "RENOIR",
138 "ALDEBARAN",
139 "NAVI10",
140 "CYAN_SKILLFISH",
141 "NAVI14",
142 "NAVI12",
143 "SIENNA_CICHLID",
144 "NAVY_FLOUNDER",
145 "VANGOGH",
146 "DIMGREY_CAVEFISH",
147 "BEIGE_GOBY",
148 "YELLOW_CARP",
149 "IP DISCOVERY",
150 "LAST",
151 };
152
153 #define AMDGPU_IP_BLK_MASK_ALL GENMASK(AMD_IP_BLOCK_TYPE_NUM - 1, 0)
154 /*
155 * Default init level where all blocks are expected to be initialized. This is
156 * the level of initialization expected by default and also after a full reset
157 * of the device.
158 */
159 struct amdgpu_init_level amdgpu_init_default = {
160 .level = AMDGPU_INIT_LEVEL_DEFAULT,
161 .hwini_ip_block_mask = AMDGPU_IP_BLK_MASK_ALL,
162 };
163
164 struct amdgpu_init_level amdgpu_init_recovery = {
165 .level = AMDGPU_INIT_LEVEL_RESET_RECOVERY,
166 .hwini_ip_block_mask = AMDGPU_IP_BLK_MASK_ALL,
167 };
168
169 /*
170 * Minimal blocks needed to be initialized before a XGMI hive can be reset. This
171 * is used for cases like reset on initialization where the entire hive needs to
172 * be reset before first use.
173 */
174 struct amdgpu_init_level amdgpu_init_minimal_xgmi = {
175 .level = AMDGPU_INIT_LEVEL_MINIMAL_XGMI,
176 .hwini_ip_block_mask =
177 BIT(AMD_IP_BLOCK_TYPE_GMC) | BIT(AMD_IP_BLOCK_TYPE_SMC) |
178 BIT(AMD_IP_BLOCK_TYPE_COMMON) | BIT(AMD_IP_BLOCK_TYPE_IH) |
179 BIT(AMD_IP_BLOCK_TYPE_PSP)
180 };
181
182 static void amdgpu_device_load_switch_state(struct amdgpu_device *adev);
183
amdgpu_ip_member_of_hwini(struct amdgpu_device * adev,enum amd_ip_block_type block)184 static inline bool amdgpu_ip_member_of_hwini(struct amdgpu_device *adev,
185 enum amd_ip_block_type block)
186 {
187 return (adev->init_lvl->hwini_ip_block_mask & (1U << block)) != 0;
188 }
189
amdgpu_set_init_level(struct amdgpu_device * adev,enum amdgpu_init_lvl_id lvl)190 void amdgpu_set_init_level(struct amdgpu_device *adev,
191 enum amdgpu_init_lvl_id lvl)
192 {
193 switch (lvl) {
194 case AMDGPU_INIT_LEVEL_MINIMAL_XGMI:
195 adev->init_lvl = &amdgpu_init_minimal_xgmi;
196 break;
197 case AMDGPU_INIT_LEVEL_RESET_RECOVERY:
198 adev->init_lvl = &amdgpu_init_recovery;
199 break;
200 case AMDGPU_INIT_LEVEL_DEFAULT:
201 fallthrough;
202 default:
203 adev->init_lvl = &amdgpu_init_default;
204 break;
205 }
206 }
207
208 static inline void amdgpu_device_stop_pending_resets(struct amdgpu_device *adev);
209 static int amdgpu_device_pm_notifier(struct notifier_block *nb, unsigned long mode,
210 void *data);
211
212 /**
213 * DOC: pcie_replay_count
214 *
215 * The amdgpu driver provides a sysfs API for reporting the total number
216 * of PCIe replays (NAKs).
217 * The file pcie_replay_count is used for this and returns the total
218 * number of replays as a sum of the NAKs generated and NAKs received.
219 */
220
amdgpu_device_get_pcie_replay_count(struct device * dev,struct device_attribute * attr,char * buf)221 static ssize_t amdgpu_device_get_pcie_replay_count(struct device *dev,
222 struct device_attribute *attr, char *buf)
223 {
224 struct drm_device *ddev = dev_get_drvdata(dev);
225 struct amdgpu_device *adev = drm_to_adev(ddev);
226 uint64_t cnt = amdgpu_asic_get_pcie_replay_count(adev);
227
228 return sysfs_emit(buf, "%llu\n", cnt);
229 }
230
231 static DEVICE_ATTR(pcie_replay_count, 0444,
232 amdgpu_device_get_pcie_replay_count, NULL);
233
amdgpu_device_attr_sysfs_init(struct amdgpu_device * adev)234 static int amdgpu_device_attr_sysfs_init(struct amdgpu_device *adev)
235 {
236 int ret = 0;
237
238 if (amdgpu_nbio_is_replay_cnt_supported(adev))
239 ret = sysfs_create_file(&adev->dev->kobj,
240 &dev_attr_pcie_replay_count.attr);
241
242 return ret;
243 }
244
amdgpu_device_attr_sysfs_fini(struct amdgpu_device * adev)245 static void amdgpu_device_attr_sysfs_fini(struct amdgpu_device *adev)
246 {
247 if (amdgpu_nbio_is_replay_cnt_supported(adev))
248 sysfs_remove_file(&adev->dev->kobj,
249 &dev_attr_pcie_replay_count.attr);
250 }
251
amdgpu_sysfs_reg_state_get(struct file * f,struct kobject * kobj,const struct bin_attribute * attr,char * buf,loff_t ppos,size_t count)252 static ssize_t amdgpu_sysfs_reg_state_get(struct file *f, struct kobject *kobj,
253 const struct bin_attribute *attr, char *buf,
254 loff_t ppos, size_t count)
255 {
256 struct device *dev = kobj_to_dev(kobj);
257 struct drm_device *ddev = dev_get_drvdata(dev);
258 struct amdgpu_device *adev = drm_to_adev(ddev);
259 ssize_t bytes_read;
260
261 switch (ppos) {
262 case AMDGPU_SYS_REG_STATE_XGMI:
263 bytes_read = amdgpu_asic_get_reg_state(
264 adev, AMDGPU_REG_STATE_TYPE_XGMI, buf, count);
265 break;
266 case AMDGPU_SYS_REG_STATE_WAFL:
267 bytes_read = amdgpu_asic_get_reg_state(
268 adev, AMDGPU_REG_STATE_TYPE_WAFL, buf, count);
269 break;
270 case AMDGPU_SYS_REG_STATE_PCIE:
271 bytes_read = amdgpu_asic_get_reg_state(
272 adev, AMDGPU_REG_STATE_TYPE_PCIE, buf, count);
273 break;
274 case AMDGPU_SYS_REG_STATE_USR:
275 bytes_read = amdgpu_asic_get_reg_state(
276 adev, AMDGPU_REG_STATE_TYPE_USR, buf, count);
277 break;
278 case AMDGPU_SYS_REG_STATE_USR_1:
279 bytes_read = amdgpu_asic_get_reg_state(
280 adev, AMDGPU_REG_STATE_TYPE_USR_1, buf, count);
281 break;
282 default:
283 return -EINVAL;
284 }
285
286 return bytes_read;
287 }
288
289 static const BIN_ATTR(reg_state, 0444, amdgpu_sysfs_reg_state_get, NULL,
290 AMDGPU_SYS_REG_STATE_END);
291
amdgpu_reg_state_sysfs_init(struct amdgpu_device * adev)292 int amdgpu_reg_state_sysfs_init(struct amdgpu_device *adev)
293 {
294 int ret;
295
296 if (!amdgpu_asic_get_reg_state_supported(adev))
297 return 0;
298
299 ret = sysfs_create_bin_file(&adev->dev->kobj, &bin_attr_reg_state);
300
301 return ret;
302 }
303
amdgpu_reg_state_sysfs_fini(struct amdgpu_device * adev)304 void amdgpu_reg_state_sysfs_fini(struct amdgpu_device *adev)
305 {
306 if (!amdgpu_asic_get_reg_state_supported(adev))
307 return;
308 sysfs_remove_bin_file(&adev->dev->kobj, &bin_attr_reg_state);
309 }
310
amdgpu_ip_block_suspend(struct amdgpu_ip_block * ip_block)311 int amdgpu_ip_block_suspend(struct amdgpu_ip_block *ip_block)
312 {
313 int r;
314
315 if (ip_block->version->funcs->suspend) {
316 r = ip_block->version->funcs->suspend(ip_block);
317 if (r) {
318 dev_err(ip_block->adev->dev,
319 "suspend of IP block <%s> failed %d\n",
320 ip_block->version->funcs->name, r);
321 return r;
322 }
323 }
324
325 ip_block->status.hw = false;
326 return 0;
327 }
328
amdgpu_ip_block_resume(struct amdgpu_ip_block * ip_block)329 int amdgpu_ip_block_resume(struct amdgpu_ip_block *ip_block)
330 {
331 int r;
332
333 if (ip_block->version->funcs->resume) {
334 r = ip_block->version->funcs->resume(ip_block);
335 if (r) {
336 dev_err(ip_block->adev->dev,
337 "resume of IP block <%s> failed %d\n",
338 ip_block->version->funcs->name, r);
339 return r;
340 }
341 }
342
343 ip_block->status.hw = true;
344 return 0;
345 }
346
347 /**
348 * DOC: board_info
349 *
350 * The amdgpu driver provides a sysfs API for giving board related information.
351 * It provides the form factor information in the format
352 *
353 * type : form factor
354 *
355 * Possible form factor values
356 *
357 * - "cem" - PCIE CEM card
358 * - "oam" - Open Compute Accelerator Module
359 * - "unknown" - Not known
360 *
361 */
362
amdgpu_device_get_board_info(struct device * dev,struct device_attribute * attr,char * buf)363 static ssize_t amdgpu_device_get_board_info(struct device *dev,
364 struct device_attribute *attr,
365 char *buf)
366 {
367 struct drm_device *ddev = dev_get_drvdata(dev);
368 struct amdgpu_device *adev = drm_to_adev(ddev);
369 enum amdgpu_pkg_type pkg_type = AMDGPU_PKG_TYPE_CEM;
370 const char *pkg;
371
372 if (adev->smuio.funcs && adev->smuio.funcs->get_pkg_type)
373 pkg_type = adev->smuio.funcs->get_pkg_type(adev);
374
375 switch (pkg_type) {
376 case AMDGPU_PKG_TYPE_CEM:
377 pkg = "cem";
378 break;
379 case AMDGPU_PKG_TYPE_OAM:
380 pkg = "oam";
381 break;
382 default:
383 pkg = "unknown";
384 break;
385 }
386
387 return sysfs_emit(buf, "%s : %s\n", "type", pkg);
388 }
389
390 static DEVICE_ATTR(board_info, 0444, amdgpu_device_get_board_info, NULL);
391
392 static struct attribute *amdgpu_board_attrs[] = {
393 &dev_attr_board_info.attr,
394 NULL,
395 };
396
amdgpu_board_attrs_is_visible(struct kobject * kobj,struct attribute * attr,int n)397 static umode_t amdgpu_board_attrs_is_visible(struct kobject *kobj,
398 struct attribute *attr, int n)
399 {
400 struct device *dev = kobj_to_dev(kobj);
401 struct drm_device *ddev = dev_get_drvdata(dev);
402 struct amdgpu_device *adev = drm_to_adev(ddev);
403
404 if (adev->flags & AMD_IS_APU)
405 return 0;
406
407 return attr->mode;
408 }
409
410 static const struct attribute_group amdgpu_board_attrs_group = {
411 .attrs = amdgpu_board_attrs,
412 .is_visible = amdgpu_board_attrs_is_visible
413 };
414
415 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev);
416
417 /**
418 * amdgpu_device_supports_px - Is the device a dGPU with ATPX power control
419 *
420 * @adev: amdgpu device pointer
421 *
422 * Returns true if the device is a dGPU with ATPX power control,
423 * otherwise return false.
424 */
amdgpu_device_supports_px(struct amdgpu_device * adev)425 bool amdgpu_device_supports_px(struct amdgpu_device *adev)
426 {
427 if ((adev->flags & AMD_IS_PX) && !amdgpu_is_atpx_hybrid())
428 return true;
429 return false;
430 }
431
432 /**
433 * amdgpu_device_supports_boco - Is the device a dGPU with ACPI power resources
434 *
435 * @adev: amdgpu device pointer
436 *
437 * Returns true if the device is a dGPU with ACPI power control,
438 * otherwise return false.
439 */
amdgpu_device_supports_boco(struct amdgpu_device * adev)440 bool amdgpu_device_supports_boco(struct amdgpu_device *adev)
441 {
442 if (!IS_ENABLED(CONFIG_HOTPLUG_PCI_PCIE))
443 return false;
444
445 if (adev->has_pr3 ||
446 ((adev->flags & AMD_IS_PX) && amdgpu_is_atpx_hybrid()))
447 return true;
448 return false;
449 }
450
451 /**
452 * amdgpu_device_supports_baco - Does the device support BACO
453 *
454 * @adev: amdgpu device pointer
455 *
456 * Return:
457 * 1 if the device supports BACO;
458 * 3 if the device supports MACO (only works if BACO is supported)
459 * otherwise return 0.
460 */
amdgpu_device_supports_baco(struct amdgpu_device * adev)461 int amdgpu_device_supports_baco(struct amdgpu_device *adev)
462 {
463 return amdgpu_asic_supports_baco(adev);
464 }
465
amdgpu_device_detect_runtime_pm_mode(struct amdgpu_device * adev)466 void amdgpu_device_detect_runtime_pm_mode(struct amdgpu_device *adev)
467 {
468 int bamaco_support;
469
470 adev->pm.rpm_mode = AMDGPU_RUNPM_NONE;
471 bamaco_support = amdgpu_device_supports_baco(adev);
472
473 switch (amdgpu_runtime_pm) {
474 case 2:
475 if (bamaco_support & MACO_SUPPORT) {
476 adev->pm.rpm_mode = AMDGPU_RUNPM_BAMACO;
477 dev_info(adev->dev, "Forcing BAMACO for runtime pm\n");
478 } else if (bamaco_support == BACO_SUPPORT) {
479 adev->pm.rpm_mode = AMDGPU_RUNPM_BACO;
480 dev_info(adev->dev, "Requested mode BAMACO not available,fallback to use BACO\n");
481 }
482 break;
483 case 1:
484 if (bamaco_support & BACO_SUPPORT) {
485 adev->pm.rpm_mode = AMDGPU_RUNPM_BACO;
486 dev_info(adev->dev, "Forcing BACO for runtime pm\n");
487 }
488 break;
489 case -1:
490 case -2:
491 if (amdgpu_device_supports_px(adev)) {
492 /* enable PX as runtime mode */
493 adev->pm.rpm_mode = AMDGPU_RUNPM_PX;
494 dev_info(adev->dev, "Using ATPX for runtime pm\n");
495 } else if (amdgpu_device_supports_boco(adev)) {
496 /* enable boco as runtime mode */
497 adev->pm.rpm_mode = AMDGPU_RUNPM_BOCO;
498 dev_info(adev->dev, "Using BOCO for runtime pm\n");
499 } else {
500 if (!bamaco_support)
501 goto no_runtime_pm;
502
503 switch (adev->asic_type) {
504 case CHIP_VEGA20:
505 case CHIP_ARCTURUS:
506 /* BACO are not supported on vega20 and arctrus */
507 break;
508 case CHIP_VEGA10:
509 /* enable BACO as runpm mode if noretry=0 */
510 if (!adev->gmc.noretry && !amdgpu_passthrough(adev))
511 adev->pm.rpm_mode = AMDGPU_RUNPM_BACO;
512 break;
513 default:
514 /* enable BACO as runpm mode on CI+ */
515 if (!amdgpu_passthrough(adev))
516 adev->pm.rpm_mode = AMDGPU_RUNPM_BACO;
517 break;
518 }
519
520 if (adev->pm.rpm_mode == AMDGPU_RUNPM_BACO) {
521 if (bamaco_support & MACO_SUPPORT) {
522 adev->pm.rpm_mode = AMDGPU_RUNPM_BAMACO;
523 dev_info(adev->dev, "Using BAMACO for runtime pm\n");
524 } else {
525 dev_info(adev->dev, "Using BACO for runtime pm\n");
526 }
527 }
528 }
529 break;
530 case 0:
531 dev_info(adev->dev, "runtime pm is manually disabled\n");
532 break;
533 default:
534 break;
535 }
536
537 no_runtime_pm:
538 if (adev->pm.rpm_mode == AMDGPU_RUNPM_NONE)
539 dev_info(adev->dev, "Runtime PM not available\n");
540 }
541 /**
542 * amdgpu_device_supports_smart_shift - Is the device dGPU with
543 * smart shift support
544 *
545 * @adev: amdgpu device pointer
546 *
547 * Returns true if the device is a dGPU with Smart Shift support,
548 * otherwise returns false.
549 */
amdgpu_device_supports_smart_shift(struct amdgpu_device * adev)550 bool amdgpu_device_supports_smart_shift(struct amdgpu_device *adev)
551 {
552 return (amdgpu_device_supports_boco(adev) &&
553 amdgpu_acpi_is_power_shift_control_supported());
554 }
555
556 /*
557 * VRAM access helper functions
558 */
559
560 /**
561 * amdgpu_device_mm_access - access vram by MM_INDEX/MM_DATA
562 *
563 * @adev: amdgpu_device pointer
564 * @pos: offset of the buffer in vram
565 * @buf: virtual address of the buffer in system memory
566 * @size: read/write size, sizeof(@buf) must > @size
567 * @write: true - write to vram, otherwise - read from vram
568 */
amdgpu_device_mm_access(struct amdgpu_device * adev,loff_t pos,void * buf,size_t size,bool write)569 void amdgpu_device_mm_access(struct amdgpu_device *adev, loff_t pos,
570 void *buf, size_t size, bool write)
571 {
572 unsigned long flags;
573 uint32_t hi = ~0, tmp = 0;
574 uint32_t *data = buf;
575 uint64_t last;
576 int idx;
577
578 if (!drm_dev_enter(adev_to_drm(adev), &idx))
579 return;
580
581 BUG_ON(!IS_ALIGNED(pos, 4) || !IS_ALIGNED(size, 4));
582
583 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
584 for (last = pos + size; pos < last; pos += 4) {
585 tmp = pos >> 31;
586
587 WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)pos) | 0x80000000);
588 if (tmp != hi) {
589 WREG32_NO_KIQ(mmMM_INDEX_HI, tmp);
590 hi = tmp;
591 }
592 if (write)
593 WREG32_NO_KIQ(mmMM_DATA, *data++);
594 else
595 *data++ = RREG32_NO_KIQ(mmMM_DATA);
596 }
597
598 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
599 drm_dev_exit(idx);
600 }
601
602 /**
603 * amdgpu_device_aper_access - access vram by vram aperture
604 *
605 * @adev: amdgpu_device pointer
606 * @pos: offset of the buffer in vram
607 * @buf: virtual address of the buffer in system memory
608 * @size: read/write size, sizeof(@buf) must > @size
609 * @write: true - write to vram, otherwise - read from vram
610 *
611 * The return value means how many bytes have been transferred.
612 */
amdgpu_device_aper_access(struct amdgpu_device * adev,loff_t pos,void * buf,size_t size,bool write)613 size_t amdgpu_device_aper_access(struct amdgpu_device *adev, loff_t pos,
614 void *buf, size_t size, bool write)
615 {
616 #ifdef CONFIG_64BIT
617 void __iomem *addr;
618 size_t count = 0;
619 uint64_t last;
620
621 if (!adev->mman.aper_base_kaddr)
622 return 0;
623
624 last = min(pos + size, adev->gmc.visible_vram_size);
625 if (last > pos) {
626 addr = adev->mman.aper_base_kaddr + pos;
627 count = last - pos;
628
629 if (write) {
630 memcpy_toio(addr, buf, count);
631 /* Make sure HDP write cache flush happens without any reordering
632 * after the system memory contents are sent over PCIe device
633 */
634 mb();
635 amdgpu_device_flush_hdp(adev, NULL);
636 } else {
637 amdgpu_device_invalidate_hdp(adev, NULL);
638 /* Make sure HDP read cache is invalidated before issuing a read
639 * to the PCIe device
640 */
641 mb();
642 memcpy_fromio(buf, addr, count);
643 }
644
645 }
646
647 return count;
648 #else
649 return 0;
650 #endif
651 }
652
653 /**
654 * amdgpu_device_vram_access - read/write a buffer in vram
655 *
656 * @adev: amdgpu_device pointer
657 * @pos: offset of the buffer in vram
658 * @buf: virtual address of the buffer in system memory
659 * @size: read/write size, sizeof(@buf) must > @size
660 * @write: true - write to vram, otherwise - read from vram
661 */
amdgpu_device_vram_access(struct amdgpu_device * adev,loff_t pos,void * buf,size_t size,bool write)662 void amdgpu_device_vram_access(struct amdgpu_device *adev, loff_t pos,
663 void *buf, size_t size, bool write)
664 {
665 size_t count;
666
667 /* try to using vram apreature to access vram first */
668 count = amdgpu_device_aper_access(adev, pos, buf, size, write);
669 size -= count;
670 if (size) {
671 /* using MM to access rest vram */
672 pos += count;
673 buf += count;
674 amdgpu_device_mm_access(adev, pos, buf, size, write);
675 }
676 }
677
678 /*
679 * register access helper functions.
680 */
681
682 /* Check if hw access should be skipped because of hotplug or device error */
amdgpu_device_skip_hw_access(struct amdgpu_device * adev)683 bool amdgpu_device_skip_hw_access(struct amdgpu_device *adev)
684 {
685 if (adev->no_hw_access)
686 return true;
687
688 #ifdef CONFIG_LOCKDEP
689 /*
690 * This is a bit complicated to understand, so worth a comment. What we assert
691 * here is that the GPU reset is not running on another thread in parallel.
692 *
693 * For this we trylock the read side of the reset semaphore, if that succeeds
694 * we know that the reset is not running in parallel.
695 *
696 * If the trylock fails we assert that we are either already holding the read
697 * side of the lock or are the reset thread itself and hold the write side of
698 * the lock.
699 */
700 if (in_task()) {
701 if (down_read_trylock(&adev->reset_domain->sem))
702 up_read(&adev->reset_domain->sem);
703 else
704 lockdep_assert_held(&adev->reset_domain->sem);
705 }
706 #endif
707 return false;
708 }
709
710 /**
711 * amdgpu_device_rreg - read a memory mapped IO or indirect register
712 *
713 * @adev: amdgpu_device pointer
714 * @reg: dword aligned register offset
715 * @acc_flags: access flags which require special behavior
716 *
717 * Returns the 32 bit value from the offset specified.
718 */
amdgpu_device_rreg(struct amdgpu_device * adev,uint32_t reg,uint32_t acc_flags)719 uint32_t amdgpu_device_rreg(struct amdgpu_device *adev,
720 uint32_t reg, uint32_t acc_flags)
721 {
722 uint32_t ret;
723
724 if (amdgpu_device_skip_hw_access(adev))
725 return 0;
726
727 if ((reg * 4) < adev->rmmio_size) {
728 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
729 amdgpu_sriov_runtime(adev) &&
730 down_read_trylock(&adev->reset_domain->sem)) {
731 ret = amdgpu_kiq_rreg(adev, reg, 0);
732 up_read(&adev->reset_domain->sem);
733 } else {
734 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
735 }
736 } else {
737 ret = adev->pcie_rreg(adev, reg * 4);
738 }
739
740 trace_amdgpu_device_rreg(adev->pdev->device, reg, ret);
741
742 return ret;
743 }
744
745 /*
746 * MMIO register read with bytes helper functions
747 * @offset:bytes offset from MMIO start
748 */
749
750 /**
751 * amdgpu_mm_rreg8 - read a memory mapped IO register
752 *
753 * @adev: amdgpu_device pointer
754 * @offset: byte aligned register offset
755 *
756 * Returns the 8 bit value from the offset specified.
757 */
amdgpu_mm_rreg8(struct amdgpu_device * adev,uint32_t offset)758 uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset)
759 {
760 if (amdgpu_device_skip_hw_access(adev))
761 return 0;
762
763 if (offset < adev->rmmio_size)
764 return (readb(adev->rmmio + offset));
765 BUG();
766 }
767
768
769 /**
770 * amdgpu_device_xcc_rreg - read a memory mapped IO or indirect register with specific XCC
771 *
772 * @adev: amdgpu_device pointer
773 * @reg: dword aligned register offset
774 * @acc_flags: access flags which require special behavior
775 * @xcc_id: xcc accelerated compute core id
776 *
777 * Returns the 32 bit value from the offset specified.
778 */
amdgpu_device_xcc_rreg(struct amdgpu_device * adev,uint32_t reg,uint32_t acc_flags,uint32_t xcc_id)779 uint32_t amdgpu_device_xcc_rreg(struct amdgpu_device *adev,
780 uint32_t reg, uint32_t acc_flags,
781 uint32_t xcc_id)
782 {
783 uint32_t ret, rlcg_flag;
784
785 if (amdgpu_device_skip_hw_access(adev))
786 return 0;
787
788 if ((reg * 4) < adev->rmmio_size) {
789 if (amdgpu_sriov_vf(adev) &&
790 !amdgpu_sriov_runtime(adev) &&
791 adev->gfx.rlc.rlcg_reg_access_supported &&
792 amdgpu_virt_get_rlcg_reg_access_flag(adev, acc_flags,
793 GC_HWIP, false,
794 &rlcg_flag)) {
795 ret = amdgpu_virt_rlcg_reg_rw(adev, reg, 0, rlcg_flag, GET_INST(GC, xcc_id));
796 } else if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
797 amdgpu_sriov_runtime(adev) &&
798 down_read_trylock(&adev->reset_domain->sem)) {
799 ret = amdgpu_kiq_rreg(adev, reg, xcc_id);
800 up_read(&adev->reset_domain->sem);
801 } else {
802 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
803 }
804 } else {
805 ret = adev->pcie_rreg(adev, reg * 4);
806 }
807
808 return ret;
809 }
810
811 /*
812 * MMIO register write with bytes helper functions
813 * @offset:bytes offset from MMIO start
814 * @value: the value want to be written to the register
815 */
816
817 /**
818 * amdgpu_mm_wreg8 - read a memory mapped IO register
819 *
820 * @adev: amdgpu_device pointer
821 * @offset: byte aligned register offset
822 * @value: 8 bit value to write
823 *
824 * Writes the value specified to the offset specified.
825 */
amdgpu_mm_wreg8(struct amdgpu_device * adev,uint32_t offset,uint8_t value)826 void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value)
827 {
828 if (amdgpu_device_skip_hw_access(adev))
829 return;
830
831 if (offset < adev->rmmio_size)
832 writeb(value, adev->rmmio + offset);
833 else
834 BUG();
835 }
836
837 /**
838 * amdgpu_device_wreg - write to a memory mapped IO or indirect register
839 *
840 * @adev: amdgpu_device pointer
841 * @reg: dword aligned register offset
842 * @v: 32 bit value to write to the register
843 * @acc_flags: access flags which require special behavior
844 *
845 * Writes the value specified to the offset specified.
846 */
amdgpu_device_wreg(struct amdgpu_device * adev,uint32_t reg,uint32_t v,uint32_t acc_flags)847 void amdgpu_device_wreg(struct amdgpu_device *adev,
848 uint32_t reg, uint32_t v,
849 uint32_t acc_flags)
850 {
851 if (amdgpu_device_skip_hw_access(adev))
852 return;
853
854 if ((reg * 4) < adev->rmmio_size) {
855 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
856 amdgpu_sriov_runtime(adev) &&
857 down_read_trylock(&adev->reset_domain->sem)) {
858 amdgpu_kiq_wreg(adev, reg, v, 0);
859 up_read(&adev->reset_domain->sem);
860 } else {
861 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
862 }
863 } else {
864 adev->pcie_wreg(adev, reg * 4, v);
865 }
866
867 trace_amdgpu_device_wreg(adev->pdev->device, reg, v);
868 }
869
870 /**
871 * amdgpu_mm_wreg_mmio_rlc - write register either with direct/indirect mmio or with RLC path if in range
872 *
873 * @adev: amdgpu_device pointer
874 * @reg: mmio/rlc register
875 * @v: value to write
876 * @xcc_id: xcc accelerated compute core id
877 *
878 * this function is invoked only for the debugfs register access
879 */
amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device * adev,uint32_t reg,uint32_t v,uint32_t xcc_id)880 void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
881 uint32_t reg, uint32_t v,
882 uint32_t xcc_id)
883 {
884 if (amdgpu_device_skip_hw_access(adev))
885 return;
886
887 if (amdgpu_sriov_fullaccess(adev) &&
888 adev->gfx.rlc.funcs &&
889 adev->gfx.rlc.funcs->is_rlcg_access_range) {
890 if (adev->gfx.rlc.funcs->is_rlcg_access_range(adev, reg))
891 return amdgpu_sriov_wreg(adev, reg, v, 0, 0, xcc_id);
892 } else if ((reg * 4) >= adev->rmmio_size) {
893 adev->pcie_wreg(adev, reg * 4, v);
894 } else {
895 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
896 }
897 }
898
899 /**
900 * amdgpu_device_xcc_wreg - write to a memory mapped IO or indirect register with specific XCC
901 *
902 * @adev: amdgpu_device pointer
903 * @reg: dword aligned register offset
904 * @v: 32 bit value to write to the register
905 * @acc_flags: access flags which require special behavior
906 * @xcc_id: xcc accelerated compute core id
907 *
908 * Writes the value specified to the offset specified.
909 */
amdgpu_device_xcc_wreg(struct amdgpu_device * adev,uint32_t reg,uint32_t v,uint32_t acc_flags,uint32_t xcc_id)910 void amdgpu_device_xcc_wreg(struct amdgpu_device *adev,
911 uint32_t reg, uint32_t v,
912 uint32_t acc_flags, uint32_t xcc_id)
913 {
914 uint32_t rlcg_flag;
915
916 if (amdgpu_device_skip_hw_access(adev))
917 return;
918
919 if ((reg * 4) < adev->rmmio_size) {
920 if (amdgpu_sriov_vf(adev) &&
921 !amdgpu_sriov_runtime(adev) &&
922 adev->gfx.rlc.rlcg_reg_access_supported &&
923 amdgpu_virt_get_rlcg_reg_access_flag(adev, acc_flags,
924 GC_HWIP, true,
925 &rlcg_flag)) {
926 amdgpu_virt_rlcg_reg_rw(adev, reg, v, rlcg_flag, GET_INST(GC, xcc_id));
927 } else if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
928 amdgpu_sriov_runtime(adev) &&
929 down_read_trylock(&adev->reset_domain->sem)) {
930 amdgpu_kiq_wreg(adev, reg, v, xcc_id);
931 up_read(&adev->reset_domain->sem);
932 } else {
933 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
934 }
935 } else {
936 adev->pcie_wreg(adev, reg * 4, v);
937 }
938 }
939
940 /**
941 * amdgpu_device_indirect_rreg - read an indirect register
942 *
943 * @adev: amdgpu_device pointer
944 * @reg_addr: indirect register address to read from
945 *
946 * Returns the value of indirect register @reg_addr
947 */
amdgpu_device_indirect_rreg(struct amdgpu_device * adev,u32 reg_addr)948 u32 amdgpu_device_indirect_rreg(struct amdgpu_device *adev,
949 u32 reg_addr)
950 {
951 unsigned long flags, pcie_index, pcie_data;
952 void __iomem *pcie_index_offset;
953 void __iomem *pcie_data_offset;
954 u32 r;
955
956 pcie_index = adev->nbio.funcs->get_pcie_index_offset(adev);
957 pcie_data = adev->nbio.funcs->get_pcie_data_offset(adev);
958
959 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
960 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
961 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
962
963 writel(reg_addr, pcie_index_offset);
964 readl(pcie_index_offset);
965 r = readl(pcie_data_offset);
966 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
967
968 return r;
969 }
970
amdgpu_device_indirect_rreg_ext(struct amdgpu_device * adev,u64 reg_addr)971 u32 amdgpu_device_indirect_rreg_ext(struct amdgpu_device *adev,
972 u64 reg_addr)
973 {
974 unsigned long flags, pcie_index, pcie_index_hi, pcie_data;
975 u32 r;
976 void __iomem *pcie_index_offset;
977 void __iomem *pcie_index_hi_offset;
978 void __iomem *pcie_data_offset;
979
980 if (unlikely(!adev->nbio.funcs)) {
981 pcie_index = AMDGPU_PCIE_INDEX_FALLBACK;
982 pcie_data = AMDGPU_PCIE_DATA_FALLBACK;
983 } else {
984 pcie_index = adev->nbio.funcs->get_pcie_index_offset(adev);
985 pcie_data = adev->nbio.funcs->get_pcie_data_offset(adev);
986 }
987
988 if (reg_addr >> 32) {
989 if (unlikely(!adev->nbio.funcs))
990 pcie_index_hi = AMDGPU_PCIE_INDEX_HI_FALLBACK;
991 else
992 pcie_index_hi = adev->nbio.funcs->get_pcie_index_hi_offset(adev);
993 } else {
994 pcie_index_hi = 0;
995 }
996
997 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
998 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
999 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
1000 if (pcie_index_hi != 0)
1001 pcie_index_hi_offset = (void __iomem *)adev->rmmio +
1002 pcie_index_hi * 4;
1003
1004 writel(reg_addr, pcie_index_offset);
1005 readl(pcie_index_offset);
1006 if (pcie_index_hi != 0) {
1007 writel((reg_addr >> 32) & 0xff, pcie_index_hi_offset);
1008 readl(pcie_index_hi_offset);
1009 }
1010 r = readl(pcie_data_offset);
1011
1012 /* clear the high bits */
1013 if (pcie_index_hi != 0) {
1014 writel(0, pcie_index_hi_offset);
1015 readl(pcie_index_hi_offset);
1016 }
1017
1018 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
1019
1020 return r;
1021 }
1022
1023 /**
1024 * amdgpu_device_indirect_rreg64 - read a 64bits indirect register
1025 *
1026 * @adev: amdgpu_device pointer
1027 * @reg_addr: indirect register address to read from
1028 *
1029 * Returns the value of indirect register @reg_addr
1030 */
amdgpu_device_indirect_rreg64(struct amdgpu_device * adev,u32 reg_addr)1031 u64 amdgpu_device_indirect_rreg64(struct amdgpu_device *adev,
1032 u32 reg_addr)
1033 {
1034 unsigned long flags, pcie_index, pcie_data;
1035 void __iomem *pcie_index_offset;
1036 void __iomem *pcie_data_offset;
1037 u64 r;
1038
1039 pcie_index = adev->nbio.funcs->get_pcie_index_offset(adev);
1040 pcie_data = adev->nbio.funcs->get_pcie_data_offset(adev);
1041
1042 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
1043 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
1044 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
1045
1046 /* read low 32 bits */
1047 writel(reg_addr, pcie_index_offset);
1048 readl(pcie_index_offset);
1049 r = readl(pcie_data_offset);
1050 /* read high 32 bits */
1051 writel(reg_addr + 4, pcie_index_offset);
1052 readl(pcie_index_offset);
1053 r |= ((u64)readl(pcie_data_offset) << 32);
1054 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
1055
1056 return r;
1057 }
1058
amdgpu_device_indirect_rreg64_ext(struct amdgpu_device * adev,u64 reg_addr)1059 u64 amdgpu_device_indirect_rreg64_ext(struct amdgpu_device *adev,
1060 u64 reg_addr)
1061 {
1062 unsigned long flags, pcie_index, pcie_data;
1063 unsigned long pcie_index_hi = 0;
1064 void __iomem *pcie_index_offset;
1065 void __iomem *pcie_index_hi_offset;
1066 void __iomem *pcie_data_offset;
1067 u64 r;
1068
1069 pcie_index = adev->nbio.funcs->get_pcie_index_offset(adev);
1070 pcie_data = adev->nbio.funcs->get_pcie_data_offset(adev);
1071 if ((reg_addr >> 32) && (adev->nbio.funcs->get_pcie_index_hi_offset))
1072 pcie_index_hi = adev->nbio.funcs->get_pcie_index_hi_offset(adev);
1073
1074 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
1075 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
1076 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
1077 if (pcie_index_hi != 0)
1078 pcie_index_hi_offset = (void __iomem *)adev->rmmio +
1079 pcie_index_hi * 4;
1080
1081 /* read low 32 bits */
1082 writel(reg_addr, pcie_index_offset);
1083 readl(pcie_index_offset);
1084 if (pcie_index_hi != 0) {
1085 writel((reg_addr >> 32) & 0xff, pcie_index_hi_offset);
1086 readl(pcie_index_hi_offset);
1087 }
1088 r = readl(pcie_data_offset);
1089 /* read high 32 bits */
1090 writel(reg_addr + 4, pcie_index_offset);
1091 readl(pcie_index_offset);
1092 if (pcie_index_hi != 0) {
1093 writel((reg_addr >> 32) & 0xff, pcie_index_hi_offset);
1094 readl(pcie_index_hi_offset);
1095 }
1096 r |= ((u64)readl(pcie_data_offset) << 32);
1097
1098 /* clear the high bits */
1099 if (pcie_index_hi != 0) {
1100 writel(0, pcie_index_hi_offset);
1101 readl(pcie_index_hi_offset);
1102 }
1103
1104 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
1105
1106 return r;
1107 }
1108
1109 /**
1110 * amdgpu_device_indirect_wreg - write an indirect register address
1111 *
1112 * @adev: amdgpu_device pointer
1113 * @reg_addr: indirect register offset
1114 * @reg_data: indirect register data
1115 *
1116 */
amdgpu_device_indirect_wreg(struct amdgpu_device * adev,u32 reg_addr,u32 reg_data)1117 void amdgpu_device_indirect_wreg(struct amdgpu_device *adev,
1118 u32 reg_addr, u32 reg_data)
1119 {
1120 unsigned long flags, pcie_index, pcie_data;
1121 void __iomem *pcie_index_offset;
1122 void __iomem *pcie_data_offset;
1123
1124 pcie_index = adev->nbio.funcs->get_pcie_index_offset(adev);
1125 pcie_data = adev->nbio.funcs->get_pcie_data_offset(adev);
1126
1127 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
1128 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
1129 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
1130
1131 writel(reg_addr, pcie_index_offset);
1132 readl(pcie_index_offset);
1133 writel(reg_data, pcie_data_offset);
1134 readl(pcie_data_offset);
1135 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
1136 }
1137
amdgpu_device_indirect_wreg_ext(struct amdgpu_device * adev,u64 reg_addr,u32 reg_data)1138 void amdgpu_device_indirect_wreg_ext(struct amdgpu_device *adev,
1139 u64 reg_addr, u32 reg_data)
1140 {
1141 unsigned long flags, pcie_index, pcie_index_hi, pcie_data;
1142 void __iomem *pcie_index_offset;
1143 void __iomem *pcie_index_hi_offset;
1144 void __iomem *pcie_data_offset;
1145
1146 pcie_index = adev->nbio.funcs->get_pcie_index_offset(adev);
1147 pcie_data = adev->nbio.funcs->get_pcie_data_offset(adev);
1148 if ((reg_addr >> 32) && (adev->nbio.funcs->get_pcie_index_hi_offset))
1149 pcie_index_hi = adev->nbio.funcs->get_pcie_index_hi_offset(adev);
1150 else
1151 pcie_index_hi = 0;
1152
1153 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
1154 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
1155 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
1156 if (pcie_index_hi != 0)
1157 pcie_index_hi_offset = (void __iomem *)adev->rmmio +
1158 pcie_index_hi * 4;
1159
1160 writel(reg_addr, pcie_index_offset);
1161 readl(pcie_index_offset);
1162 if (pcie_index_hi != 0) {
1163 writel((reg_addr >> 32) & 0xff, pcie_index_hi_offset);
1164 readl(pcie_index_hi_offset);
1165 }
1166 writel(reg_data, pcie_data_offset);
1167 readl(pcie_data_offset);
1168
1169 /* clear the high bits */
1170 if (pcie_index_hi != 0) {
1171 writel(0, pcie_index_hi_offset);
1172 readl(pcie_index_hi_offset);
1173 }
1174
1175 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
1176 }
1177
1178 /**
1179 * amdgpu_device_indirect_wreg64 - write a 64bits indirect register address
1180 *
1181 * @adev: amdgpu_device pointer
1182 * @reg_addr: indirect register offset
1183 * @reg_data: indirect register data
1184 *
1185 */
amdgpu_device_indirect_wreg64(struct amdgpu_device * adev,u32 reg_addr,u64 reg_data)1186 void amdgpu_device_indirect_wreg64(struct amdgpu_device *adev,
1187 u32 reg_addr, u64 reg_data)
1188 {
1189 unsigned long flags, pcie_index, pcie_data;
1190 void __iomem *pcie_index_offset;
1191 void __iomem *pcie_data_offset;
1192
1193 pcie_index = adev->nbio.funcs->get_pcie_index_offset(adev);
1194 pcie_data = adev->nbio.funcs->get_pcie_data_offset(adev);
1195
1196 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
1197 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
1198 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
1199
1200 /* write low 32 bits */
1201 writel(reg_addr, pcie_index_offset);
1202 readl(pcie_index_offset);
1203 writel((u32)(reg_data & 0xffffffffULL), pcie_data_offset);
1204 readl(pcie_data_offset);
1205 /* write high 32 bits */
1206 writel(reg_addr + 4, pcie_index_offset);
1207 readl(pcie_index_offset);
1208 writel((u32)(reg_data >> 32), pcie_data_offset);
1209 readl(pcie_data_offset);
1210 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
1211 }
1212
amdgpu_device_indirect_wreg64_ext(struct amdgpu_device * adev,u64 reg_addr,u64 reg_data)1213 void amdgpu_device_indirect_wreg64_ext(struct amdgpu_device *adev,
1214 u64 reg_addr, u64 reg_data)
1215 {
1216 unsigned long flags, pcie_index, pcie_data;
1217 unsigned long pcie_index_hi = 0;
1218 void __iomem *pcie_index_offset;
1219 void __iomem *pcie_index_hi_offset;
1220 void __iomem *pcie_data_offset;
1221
1222 pcie_index = adev->nbio.funcs->get_pcie_index_offset(adev);
1223 pcie_data = adev->nbio.funcs->get_pcie_data_offset(adev);
1224 if ((reg_addr >> 32) && (adev->nbio.funcs->get_pcie_index_hi_offset))
1225 pcie_index_hi = adev->nbio.funcs->get_pcie_index_hi_offset(adev);
1226
1227 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
1228 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
1229 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
1230 if (pcie_index_hi != 0)
1231 pcie_index_hi_offset = (void __iomem *)adev->rmmio +
1232 pcie_index_hi * 4;
1233
1234 /* write low 32 bits */
1235 writel(reg_addr, pcie_index_offset);
1236 readl(pcie_index_offset);
1237 if (pcie_index_hi != 0) {
1238 writel((reg_addr >> 32) & 0xff, pcie_index_hi_offset);
1239 readl(pcie_index_hi_offset);
1240 }
1241 writel((u32)(reg_data & 0xffffffffULL), pcie_data_offset);
1242 readl(pcie_data_offset);
1243 /* write high 32 bits */
1244 writel(reg_addr + 4, pcie_index_offset);
1245 readl(pcie_index_offset);
1246 if (pcie_index_hi != 0) {
1247 writel((reg_addr >> 32) & 0xff, pcie_index_hi_offset);
1248 readl(pcie_index_hi_offset);
1249 }
1250 writel((u32)(reg_data >> 32), pcie_data_offset);
1251 readl(pcie_data_offset);
1252
1253 /* clear the high bits */
1254 if (pcie_index_hi != 0) {
1255 writel(0, pcie_index_hi_offset);
1256 readl(pcie_index_hi_offset);
1257 }
1258
1259 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
1260 }
1261
1262 /**
1263 * amdgpu_device_get_rev_id - query device rev_id
1264 *
1265 * @adev: amdgpu_device pointer
1266 *
1267 * Return device rev_id
1268 */
amdgpu_device_get_rev_id(struct amdgpu_device * adev)1269 u32 amdgpu_device_get_rev_id(struct amdgpu_device *adev)
1270 {
1271 return adev->nbio.funcs->get_rev_id(adev);
1272 }
1273
1274 /**
1275 * amdgpu_invalid_rreg - dummy reg read function
1276 *
1277 * @adev: amdgpu_device pointer
1278 * @reg: offset of register
1279 *
1280 * Dummy register read function. Used for register blocks
1281 * that certain asics don't have (all asics).
1282 * Returns the value in the register.
1283 */
amdgpu_invalid_rreg(struct amdgpu_device * adev,uint32_t reg)1284 static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
1285 {
1286 dev_err(adev->dev, "Invalid callback to read register 0x%04X\n", reg);
1287 BUG();
1288 return 0;
1289 }
1290
amdgpu_invalid_rreg_ext(struct amdgpu_device * adev,uint64_t reg)1291 static uint32_t amdgpu_invalid_rreg_ext(struct amdgpu_device *adev, uint64_t reg)
1292 {
1293 dev_err(adev->dev, "Invalid callback to read register 0x%llX\n", reg);
1294 BUG();
1295 return 0;
1296 }
1297
1298 /**
1299 * amdgpu_invalid_wreg - dummy reg write function
1300 *
1301 * @adev: amdgpu_device pointer
1302 * @reg: offset of register
1303 * @v: value to write to the register
1304 *
1305 * Dummy register read function. Used for register blocks
1306 * that certain asics don't have (all asics).
1307 */
amdgpu_invalid_wreg(struct amdgpu_device * adev,uint32_t reg,uint32_t v)1308 static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
1309 {
1310 dev_err(adev->dev,
1311 "Invalid callback to write register 0x%04X with 0x%08X\n", reg,
1312 v);
1313 BUG();
1314 }
1315
amdgpu_invalid_wreg_ext(struct amdgpu_device * adev,uint64_t reg,uint32_t v)1316 static void amdgpu_invalid_wreg_ext(struct amdgpu_device *adev, uint64_t reg, uint32_t v)
1317 {
1318 dev_err(adev->dev,
1319 "Invalid callback to write register 0x%llX with 0x%08X\n", reg,
1320 v);
1321 BUG();
1322 }
1323
1324 /**
1325 * amdgpu_invalid_rreg64 - dummy 64 bit reg read function
1326 *
1327 * @adev: amdgpu_device pointer
1328 * @reg: offset of register
1329 *
1330 * Dummy register read function. Used for register blocks
1331 * that certain asics don't have (all asics).
1332 * Returns the value in the register.
1333 */
amdgpu_invalid_rreg64(struct amdgpu_device * adev,uint32_t reg)1334 static uint64_t amdgpu_invalid_rreg64(struct amdgpu_device *adev, uint32_t reg)
1335 {
1336 dev_err(adev->dev, "Invalid callback to read 64 bit register 0x%04X\n",
1337 reg);
1338 BUG();
1339 return 0;
1340 }
1341
amdgpu_invalid_rreg64_ext(struct amdgpu_device * adev,uint64_t reg)1342 static uint64_t amdgpu_invalid_rreg64_ext(struct amdgpu_device *adev, uint64_t reg)
1343 {
1344 dev_err(adev->dev, "Invalid callback to read register 0x%llX\n", reg);
1345 BUG();
1346 return 0;
1347 }
1348
1349 /**
1350 * amdgpu_invalid_wreg64 - dummy reg write function
1351 *
1352 * @adev: amdgpu_device pointer
1353 * @reg: offset of register
1354 * @v: value to write to the register
1355 *
1356 * Dummy register read function. Used for register blocks
1357 * that certain asics don't have (all asics).
1358 */
amdgpu_invalid_wreg64(struct amdgpu_device * adev,uint32_t reg,uint64_t v)1359 static void amdgpu_invalid_wreg64(struct amdgpu_device *adev, uint32_t reg, uint64_t v)
1360 {
1361 dev_err(adev->dev,
1362 "Invalid callback to write 64 bit register 0x%04X with 0x%08llX\n",
1363 reg, v);
1364 BUG();
1365 }
1366
amdgpu_invalid_wreg64_ext(struct amdgpu_device * adev,uint64_t reg,uint64_t v)1367 static void amdgpu_invalid_wreg64_ext(struct amdgpu_device *adev, uint64_t reg, uint64_t v)
1368 {
1369 dev_err(adev->dev,
1370 "Invalid callback to write 64 bit register 0x%llX with 0x%08llX\n",
1371 reg, v);
1372 BUG();
1373 }
1374
1375 /**
1376 * amdgpu_block_invalid_rreg - dummy reg read function
1377 *
1378 * @adev: amdgpu_device pointer
1379 * @block: offset of instance
1380 * @reg: offset of register
1381 *
1382 * Dummy register read function. Used for register blocks
1383 * that certain asics don't have (all asics).
1384 * Returns the value in the register.
1385 */
amdgpu_block_invalid_rreg(struct amdgpu_device * adev,uint32_t block,uint32_t reg)1386 static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
1387 uint32_t block, uint32_t reg)
1388 {
1389 dev_err(adev->dev,
1390 "Invalid callback to read register 0x%04X in block 0x%04X\n",
1391 reg, block);
1392 BUG();
1393 return 0;
1394 }
1395
1396 /**
1397 * amdgpu_block_invalid_wreg - dummy reg write function
1398 *
1399 * @adev: amdgpu_device pointer
1400 * @block: offset of instance
1401 * @reg: offset of register
1402 * @v: value to write to the register
1403 *
1404 * Dummy register read function. Used for register blocks
1405 * that certain asics don't have (all asics).
1406 */
amdgpu_block_invalid_wreg(struct amdgpu_device * adev,uint32_t block,uint32_t reg,uint32_t v)1407 static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
1408 uint32_t block,
1409 uint32_t reg, uint32_t v)
1410 {
1411 dev_err(adev->dev,
1412 "Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
1413 reg, block, v);
1414 BUG();
1415 }
1416
amdgpu_device_get_vbios_flags(struct amdgpu_device * adev)1417 static uint32_t amdgpu_device_get_vbios_flags(struct amdgpu_device *adev)
1418 {
1419 if (hweight32(adev->aid_mask) && (adev->flags & AMD_IS_APU))
1420 return AMDGPU_VBIOS_SKIP;
1421
1422 if (hweight32(adev->aid_mask) && amdgpu_passthrough(adev))
1423 return AMDGPU_VBIOS_OPTIONAL;
1424
1425 return 0;
1426 }
1427
1428 /**
1429 * amdgpu_device_asic_init - Wrapper for atom asic_init
1430 *
1431 * @adev: amdgpu_device pointer
1432 *
1433 * Does any asic specific work and then calls atom asic init.
1434 */
amdgpu_device_asic_init(struct amdgpu_device * adev)1435 static int amdgpu_device_asic_init(struct amdgpu_device *adev)
1436 {
1437 uint32_t flags;
1438 bool optional;
1439 int ret;
1440
1441 amdgpu_asic_pre_asic_init(adev);
1442 flags = amdgpu_device_get_vbios_flags(adev);
1443 optional = !!(flags & (AMDGPU_VBIOS_OPTIONAL | AMDGPU_VBIOS_SKIP));
1444
1445 if (amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 4, 3) ||
1446 amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 4, 4) ||
1447 amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 5, 0) ||
1448 amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(11, 0, 0)) {
1449 amdgpu_psp_wait_for_bootloader(adev);
1450 if (optional && !adev->bios)
1451 return 0;
1452
1453 ret = amdgpu_atomfirmware_asic_init(adev, true);
1454 return ret;
1455 } else {
1456 if (optional && !adev->bios)
1457 return 0;
1458
1459 return amdgpu_atom_asic_init(adev->mode_info.atom_context);
1460 }
1461
1462 return 0;
1463 }
1464
1465 /**
1466 * amdgpu_device_mem_scratch_init - allocate the VRAM scratch page
1467 *
1468 * @adev: amdgpu_device pointer
1469 *
1470 * Allocates a scratch page of VRAM for use by various things in the
1471 * driver.
1472 */
amdgpu_device_mem_scratch_init(struct amdgpu_device * adev)1473 static int amdgpu_device_mem_scratch_init(struct amdgpu_device *adev)
1474 {
1475 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE, PAGE_SIZE,
1476 AMDGPU_GEM_DOMAIN_VRAM |
1477 AMDGPU_GEM_DOMAIN_GTT,
1478 &adev->mem_scratch.robj,
1479 &adev->mem_scratch.gpu_addr,
1480 (void **)&adev->mem_scratch.ptr);
1481 }
1482
1483 /**
1484 * amdgpu_device_mem_scratch_fini - Free the VRAM scratch page
1485 *
1486 * @adev: amdgpu_device pointer
1487 *
1488 * Frees the VRAM scratch page.
1489 */
amdgpu_device_mem_scratch_fini(struct amdgpu_device * adev)1490 static void amdgpu_device_mem_scratch_fini(struct amdgpu_device *adev)
1491 {
1492 amdgpu_bo_free_kernel(&adev->mem_scratch.robj, NULL, NULL);
1493 }
1494
1495 /**
1496 * amdgpu_device_program_register_sequence - program an array of registers.
1497 *
1498 * @adev: amdgpu_device pointer
1499 * @registers: pointer to the register array
1500 * @array_size: size of the register array
1501 *
1502 * Programs an array or registers with and or masks.
1503 * This is a helper for setting golden registers.
1504 */
amdgpu_device_program_register_sequence(struct amdgpu_device * adev,const u32 * registers,const u32 array_size)1505 void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
1506 const u32 *registers,
1507 const u32 array_size)
1508 {
1509 u32 tmp, reg, and_mask, or_mask;
1510 int i;
1511
1512 if (array_size % 3)
1513 return;
1514
1515 for (i = 0; i < array_size; i += 3) {
1516 reg = registers[i + 0];
1517 and_mask = registers[i + 1];
1518 or_mask = registers[i + 2];
1519
1520 if (and_mask == 0xffffffff) {
1521 tmp = or_mask;
1522 } else {
1523 tmp = RREG32(reg);
1524 tmp &= ~and_mask;
1525 if (adev->family >= AMDGPU_FAMILY_AI)
1526 tmp |= (or_mask & and_mask);
1527 else
1528 tmp |= or_mask;
1529 }
1530 WREG32(reg, tmp);
1531 }
1532 }
1533
1534 /**
1535 * amdgpu_device_pci_config_reset - reset the GPU
1536 *
1537 * @adev: amdgpu_device pointer
1538 *
1539 * Resets the GPU using the pci config reset sequence.
1540 * Only applicable to asics prior to vega10.
1541 */
amdgpu_device_pci_config_reset(struct amdgpu_device * adev)1542 void amdgpu_device_pci_config_reset(struct amdgpu_device *adev)
1543 {
1544 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
1545 }
1546
1547 /**
1548 * amdgpu_device_pci_reset - reset the GPU using generic PCI means
1549 *
1550 * @adev: amdgpu_device pointer
1551 *
1552 * Resets the GPU using generic pci reset interfaces (FLR, SBR, etc.).
1553 */
amdgpu_device_pci_reset(struct amdgpu_device * adev)1554 int amdgpu_device_pci_reset(struct amdgpu_device *adev)
1555 {
1556 return pci_reset_function(adev->pdev);
1557 }
1558
1559 /*
1560 * amdgpu_device_wb_*()
1561 * Writeback is the method by which the GPU updates special pages in memory
1562 * with the status of certain GPU events (fences, ring pointers,etc.).
1563 */
1564
1565 /**
1566 * amdgpu_device_wb_fini - Disable Writeback and free memory
1567 *
1568 * @adev: amdgpu_device pointer
1569 *
1570 * Disables Writeback and frees the Writeback memory (all asics).
1571 * Used at driver shutdown.
1572 */
amdgpu_device_wb_fini(struct amdgpu_device * adev)1573 static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
1574 {
1575 if (adev->wb.wb_obj) {
1576 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
1577 &adev->wb.gpu_addr,
1578 (void **)&adev->wb.wb);
1579 adev->wb.wb_obj = NULL;
1580 }
1581 }
1582
1583 /**
1584 * amdgpu_device_wb_init - Init Writeback driver info and allocate memory
1585 *
1586 * @adev: amdgpu_device pointer
1587 *
1588 * Initializes writeback and allocates writeback memory (all asics).
1589 * Used at driver startup.
1590 * Returns 0 on success or an -error on failure.
1591 */
amdgpu_device_wb_init(struct amdgpu_device * adev)1592 static int amdgpu_device_wb_init(struct amdgpu_device *adev)
1593 {
1594 int r;
1595
1596 if (adev->wb.wb_obj == NULL) {
1597 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
1598 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
1599 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
1600 &adev->wb.wb_obj, &adev->wb.gpu_addr,
1601 (void **)&adev->wb.wb);
1602 if (r) {
1603 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
1604 return r;
1605 }
1606
1607 adev->wb.num_wb = AMDGPU_MAX_WB;
1608 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
1609
1610 /* clear wb memory */
1611 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8);
1612 }
1613
1614 return 0;
1615 }
1616
1617 /**
1618 * amdgpu_device_wb_get - Allocate a wb entry
1619 *
1620 * @adev: amdgpu_device pointer
1621 * @wb: wb index
1622 *
1623 * Allocate a wb slot for use by the driver (all asics).
1624 * Returns 0 on success or -EINVAL on failure.
1625 */
amdgpu_device_wb_get(struct amdgpu_device * adev,u32 * wb)1626 int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb)
1627 {
1628 unsigned long flags, offset;
1629
1630 spin_lock_irqsave(&adev->wb.lock, flags);
1631 offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
1632 if (offset < adev->wb.num_wb) {
1633 __set_bit(offset, adev->wb.used);
1634 spin_unlock_irqrestore(&adev->wb.lock, flags);
1635 *wb = offset << 3; /* convert to dw offset */
1636 return 0;
1637 } else {
1638 spin_unlock_irqrestore(&adev->wb.lock, flags);
1639 return -EINVAL;
1640 }
1641 }
1642
1643 /**
1644 * amdgpu_device_wb_free - Free a wb entry
1645 *
1646 * @adev: amdgpu_device pointer
1647 * @wb: wb index
1648 *
1649 * Free a wb slot allocated for use by the driver (all asics)
1650 */
amdgpu_device_wb_free(struct amdgpu_device * adev,u32 wb)1651 void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb)
1652 {
1653 unsigned long flags;
1654
1655 wb >>= 3;
1656 spin_lock_irqsave(&adev->wb.lock, flags);
1657 if (wb < adev->wb.num_wb)
1658 __clear_bit(wb, adev->wb.used);
1659 spin_unlock_irqrestore(&adev->wb.lock, flags);
1660 }
1661
1662 /**
1663 * amdgpu_device_resize_fb_bar - try to resize FB BAR
1664 *
1665 * @adev: amdgpu_device pointer
1666 *
1667 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
1668 * to fail, but if any of the BARs is not accessible after the size we abort
1669 * driver loading by returning -ENODEV.
1670 */
amdgpu_device_resize_fb_bar(struct amdgpu_device * adev)1671 int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
1672 {
1673 int rbar_size = pci_rebar_bytes_to_size(adev->gmc.real_vram_size);
1674 struct pci_bus *root;
1675 struct resource *res;
1676 unsigned int i;
1677 u16 cmd;
1678 int r;
1679
1680 if (!IS_ENABLED(CONFIG_PHYS_ADDR_T_64BIT))
1681 return 0;
1682
1683 /* Bypass for VF */
1684 if (amdgpu_sriov_vf(adev))
1685 return 0;
1686
1687 if (!amdgpu_rebar)
1688 return 0;
1689
1690 /* resizing on Dell G5 SE platforms causes problems with runtime pm */
1691 if ((amdgpu_runtime_pm != 0) &&
1692 adev->pdev->vendor == PCI_VENDOR_ID_ATI &&
1693 adev->pdev->device == 0x731f &&
1694 adev->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
1695 return 0;
1696
1697 /* PCI_EXT_CAP_ID_VNDR extended capability is located at 0x100 */
1698 if (!pci_find_ext_capability(adev->pdev, PCI_EXT_CAP_ID_VNDR))
1699 dev_warn(
1700 adev->dev,
1701 "System can't access extended configuration space, please check!!\n");
1702
1703 /* skip if the bios has already enabled large BAR */
1704 if (adev->gmc.real_vram_size &&
1705 (pci_resource_len(adev->pdev, 0) >= adev->gmc.real_vram_size))
1706 return 0;
1707
1708 /* Check if the root BUS has 64bit memory resources */
1709 root = adev->pdev->bus;
1710 while (root->parent)
1711 root = root->parent;
1712
1713 pci_bus_for_each_resource(root, res, i) {
1714 if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
1715 res->start > 0x100000000ull)
1716 break;
1717 }
1718
1719 /* Trying to resize is pointless without a root hub window above 4GB */
1720 if (!res)
1721 return 0;
1722
1723 /* Limit the BAR size to what is available */
1724 rbar_size = min(fls(pci_rebar_get_possible_sizes(adev->pdev, 0)) - 1,
1725 rbar_size);
1726
1727 /* Disable memory decoding while we change the BAR addresses and size */
1728 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
1729 pci_write_config_word(adev->pdev, PCI_COMMAND,
1730 cmd & ~PCI_COMMAND_MEMORY);
1731
1732 /* Free the VRAM and doorbell BAR, we most likely need to move both. */
1733 amdgpu_doorbell_fini(adev);
1734 if (adev->asic_type >= CHIP_BONAIRE)
1735 pci_release_resource(adev->pdev, 2);
1736
1737 pci_release_resource(adev->pdev, 0);
1738
1739 r = pci_resize_resource(adev->pdev, 0, rbar_size);
1740 if (r == -ENOSPC)
1741 dev_info(adev->dev,
1742 "Not enough PCI address space for a large BAR.");
1743 else if (r && r != -ENOTSUPP)
1744 dev_err(adev->dev, "Problem resizing BAR0 (%d).", r);
1745
1746 pci_assign_unassigned_bus_resources(adev->pdev->bus);
1747
1748 /* When the doorbell or fb BAR isn't available we have no chance of
1749 * using the device.
1750 */
1751 r = amdgpu_doorbell_init(adev);
1752 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
1753 return -ENODEV;
1754
1755 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
1756
1757 return 0;
1758 }
1759
1760 /*
1761 * GPU helpers function.
1762 */
1763 /**
1764 * amdgpu_device_need_post - check if the hw need post or not
1765 *
1766 * @adev: amdgpu_device pointer
1767 *
1768 * Check if the asic has been initialized (all asics) at driver startup
1769 * or post is needed if hw reset is performed.
1770 * Returns true if need or false if not.
1771 */
amdgpu_device_need_post(struct amdgpu_device * adev)1772 bool amdgpu_device_need_post(struct amdgpu_device *adev)
1773 {
1774 uint32_t reg, flags;
1775
1776 if (amdgpu_sriov_vf(adev))
1777 return false;
1778
1779 flags = amdgpu_device_get_vbios_flags(adev);
1780 if (flags & AMDGPU_VBIOS_SKIP)
1781 return false;
1782 if ((flags & AMDGPU_VBIOS_OPTIONAL) && !adev->bios)
1783 return false;
1784
1785 if (amdgpu_passthrough(adev)) {
1786 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
1787 * some old smc fw still need driver do vPost otherwise gpu hang, while
1788 * those smc fw version above 22.15 doesn't have this flaw, so we force
1789 * vpost executed for smc version below 22.15
1790 */
1791 if (adev->asic_type == CHIP_FIJI) {
1792 int err;
1793 uint32_t fw_ver;
1794
1795 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
1796 /* force vPost if error occurred */
1797 if (err)
1798 return true;
1799
1800 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
1801 release_firmware(adev->pm.fw);
1802 if (fw_ver < 0x00160e00)
1803 return true;
1804 }
1805 }
1806
1807 /* Don't post if we need to reset whole hive on init */
1808 if (adev->init_lvl->level == AMDGPU_INIT_LEVEL_MINIMAL_XGMI)
1809 return false;
1810
1811 if (adev->has_hw_reset) {
1812 adev->has_hw_reset = false;
1813 return true;
1814 }
1815
1816 /* bios scratch used on CIK+ */
1817 if (adev->asic_type >= CHIP_BONAIRE)
1818 return amdgpu_atombios_scratch_need_asic_init(adev);
1819
1820 /* check MEM_SIZE for older asics */
1821 reg = amdgpu_asic_get_config_memsize(adev);
1822
1823 if ((reg != 0) && (reg != 0xffffffff))
1824 return false;
1825
1826 return true;
1827 }
1828
1829 /*
1830 * Check whether seamless boot is supported.
1831 *
1832 * So far we only support seamless boot on DCE 3.0 or later.
1833 * If users report that it works on older ASICS as well, we may
1834 * loosen this.
1835 */
amdgpu_device_seamless_boot_supported(struct amdgpu_device * adev)1836 bool amdgpu_device_seamless_boot_supported(struct amdgpu_device *adev)
1837 {
1838 switch (amdgpu_seamless) {
1839 case -1:
1840 break;
1841 case 1:
1842 return true;
1843 case 0:
1844 return false;
1845 default:
1846 dev_err(adev->dev, "Invalid value for amdgpu.seamless: %d\n",
1847 amdgpu_seamless);
1848 return false;
1849 }
1850
1851 if (!(adev->flags & AMD_IS_APU))
1852 return false;
1853
1854 if (adev->mman.keep_stolen_vga_memory)
1855 return false;
1856
1857 return amdgpu_ip_version(adev, DCE_HWIP, 0) >= IP_VERSION(3, 0, 0);
1858 }
1859
1860 /*
1861 * Intel hosts such as Rocket Lake, Alder Lake, Raptor Lake and Sapphire Rapids
1862 * don't support dynamic speed switching. Until we have confirmation from Intel
1863 * that a specific host supports it, it's safer that we keep it disabled for all.
1864 *
1865 * https://edc.intel.com/content/www/us/en/design/products/platforms/details/raptor-lake-s/13th-generation-core-processors-datasheet-volume-1-of-2/005/pci-express-support/
1866 * https://gitlab.freedesktop.org/drm/amd/-/issues/2663
1867 */
amdgpu_device_pcie_dynamic_switching_supported(struct amdgpu_device * adev)1868 static bool amdgpu_device_pcie_dynamic_switching_supported(struct amdgpu_device *adev)
1869 {
1870 #if IS_ENABLED(CONFIG_X86)
1871 struct cpuinfo_x86 *c = &cpu_data(0);
1872
1873 /* eGPU change speeds based on USB4 fabric conditions */
1874 if (dev_is_removable(adev->dev))
1875 return true;
1876
1877 if (c->x86_vendor == X86_VENDOR_INTEL)
1878 return false;
1879 #endif
1880 return true;
1881 }
1882
amdgpu_device_aspm_support_quirk(struct amdgpu_device * adev)1883 static bool amdgpu_device_aspm_support_quirk(struct amdgpu_device *adev)
1884 {
1885 #if IS_ENABLED(CONFIG_X86)
1886 struct cpuinfo_x86 *c = &cpu_data(0);
1887
1888 if (!(amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(12, 0, 0) ||
1889 amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(12, 0, 1)))
1890 return false;
1891
1892 if (c->x86 == 6 &&
1893 adev->pm.pcie_gen_mask & CAIL_PCIE_LINK_SPEED_SUPPORT_GEN5) {
1894 switch (c->x86_model) {
1895 case VFM_MODEL(INTEL_ALDERLAKE):
1896 case VFM_MODEL(INTEL_ALDERLAKE_L):
1897 case VFM_MODEL(INTEL_RAPTORLAKE):
1898 case VFM_MODEL(INTEL_RAPTORLAKE_P):
1899 case VFM_MODEL(INTEL_RAPTORLAKE_S):
1900 return true;
1901 default:
1902 return false;
1903 }
1904 } else {
1905 return false;
1906 }
1907 #else
1908 return false;
1909 #endif
1910 }
1911
1912 /**
1913 * amdgpu_device_should_use_aspm - check if the device should program ASPM
1914 *
1915 * @adev: amdgpu_device pointer
1916 *
1917 * Confirm whether the module parameter and pcie bridge agree that ASPM should
1918 * be set for this device.
1919 *
1920 * Returns true if it should be used or false if not.
1921 */
amdgpu_device_should_use_aspm(struct amdgpu_device * adev)1922 bool amdgpu_device_should_use_aspm(struct amdgpu_device *adev)
1923 {
1924 switch (amdgpu_aspm) {
1925 case -1:
1926 break;
1927 case 0:
1928 return false;
1929 case 1:
1930 return true;
1931 default:
1932 return false;
1933 }
1934 if (adev->flags & AMD_IS_APU)
1935 return false;
1936 if (amdgpu_device_aspm_support_quirk(adev))
1937 return false;
1938 return pcie_aspm_enabled(adev->pdev);
1939 }
1940
1941 /* if we get transitioned to only one device, take VGA back */
1942 /**
1943 * amdgpu_device_vga_set_decode - enable/disable vga decode
1944 *
1945 * @pdev: PCI device pointer
1946 * @state: enable/disable vga decode
1947 *
1948 * Enable/disable vga decode (all asics).
1949 * Returns VGA resource flags.
1950 */
amdgpu_device_vga_set_decode(struct pci_dev * pdev,bool state)1951 static unsigned int amdgpu_device_vga_set_decode(struct pci_dev *pdev,
1952 bool state)
1953 {
1954 struct amdgpu_device *adev = drm_to_adev(pci_get_drvdata(pdev));
1955
1956 amdgpu_asic_set_vga_state(adev, state);
1957 if (state)
1958 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1959 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1960 else
1961 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1962 }
1963
1964 /**
1965 * amdgpu_device_check_block_size - validate the vm block size
1966 *
1967 * @adev: amdgpu_device pointer
1968 *
1969 * Validates the vm block size specified via module parameter.
1970 * The vm block size defines number of bits in page table versus page directory,
1971 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1972 * page table and the remaining bits are in the page directory.
1973 */
amdgpu_device_check_block_size(struct amdgpu_device * adev)1974 static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
1975 {
1976 /* defines number of bits in page table versus page directory,
1977 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1978 * page table and the remaining bits are in the page directory
1979 */
1980 if (amdgpu_vm_block_size == -1)
1981 return;
1982
1983 if (amdgpu_vm_block_size < 9) {
1984 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1985 amdgpu_vm_block_size);
1986 amdgpu_vm_block_size = -1;
1987 }
1988 }
1989
1990 /**
1991 * amdgpu_device_check_vm_size - validate the vm size
1992 *
1993 * @adev: amdgpu_device pointer
1994 *
1995 * Validates the vm size in GB specified via module parameter.
1996 * The VM size is the size of the GPU virtual memory space in GB.
1997 */
amdgpu_device_check_vm_size(struct amdgpu_device * adev)1998 static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
1999 {
2000 /* no need to check the default value */
2001 if (amdgpu_vm_size == -1)
2002 return;
2003
2004 if (amdgpu_vm_size < 1) {
2005 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
2006 amdgpu_vm_size);
2007 amdgpu_vm_size = -1;
2008 }
2009 }
2010
amdgpu_device_check_smu_prv_buffer_size(struct amdgpu_device * adev)2011 static void amdgpu_device_check_smu_prv_buffer_size(struct amdgpu_device *adev)
2012 {
2013 struct sysinfo si;
2014 bool is_os_64 = (sizeof(void *) == 8);
2015 uint64_t total_memory;
2016 uint64_t dram_size_seven_GB = 0x1B8000000;
2017 uint64_t dram_size_three_GB = 0xB8000000;
2018
2019 if (amdgpu_smu_memory_pool_size == 0)
2020 return;
2021
2022 if (!is_os_64) {
2023 dev_warn(adev->dev, "Not 64-bit OS, feature not supported\n");
2024 goto def_value;
2025 }
2026 si_meminfo(&si);
2027 total_memory = (uint64_t)si.totalram * si.mem_unit;
2028
2029 if ((amdgpu_smu_memory_pool_size == 1) ||
2030 (amdgpu_smu_memory_pool_size == 2)) {
2031 if (total_memory < dram_size_three_GB)
2032 goto def_value1;
2033 } else if ((amdgpu_smu_memory_pool_size == 4) ||
2034 (amdgpu_smu_memory_pool_size == 8)) {
2035 if (total_memory < dram_size_seven_GB)
2036 goto def_value1;
2037 } else {
2038 dev_warn(adev->dev, "Smu memory pool size not supported\n");
2039 goto def_value;
2040 }
2041 adev->pm.smu_prv_buffer_size = amdgpu_smu_memory_pool_size << 28;
2042
2043 return;
2044
2045 def_value1:
2046 dev_warn(adev->dev, "No enough system memory\n");
2047 def_value:
2048 adev->pm.smu_prv_buffer_size = 0;
2049 }
2050
amdgpu_device_init_apu_flags(struct amdgpu_device * adev)2051 static int amdgpu_device_init_apu_flags(struct amdgpu_device *adev)
2052 {
2053 if (!(adev->flags & AMD_IS_APU) ||
2054 adev->asic_type < CHIP_RAVEN)
2055 return 0;
2056
2057 switch (adev->asic_type) {
2058 case CHIP_RAVEN:
2059 if (adev->pdev->device == 0x15dd)
2060 adev->apu_flags |= AMD_APU_IS_RAVEN;
2061 if (adev->pdev->device == 0x15d8)
2062 adev->apu_flags |= AMD_APU_IS_PICASSO;
2063 break;
2064 case CHIP_RENOIR:
2065 if ((adev->pdev->device == 0x1636) ||
2066 (adev->pdev->device == 0x164c))
2067 adev->apu_flags |= AMD_APU_IS_RENOIR;
2068 else
2069 adev->apu_flags |= AMD_APU_IS_GREEN_SARDINE;
2070 break;
2071 case CHIP_VANGOGH:
2072 adev->apu_flags |= AMD_APU_IS_VANGOGH;
2073 break;
2074 case CHIP_YELLOW_CARP:
2075 break;
2076 case CHIP_CYAN_SKILLFISH:
2077 if ((adev->pdev->device == 0x13FE) ||
2078 (adev->pdev->device == 0x143F))
2079 adev->apu_flags |= AMD_APU_IS_CYAN_SKILLFISH2;
2080 break;
2081 default:
2082 break;
2083 }
2084
2085 return 0;
2086 }
2087
2088 /**
2089 * amdgpu_device_check_arguments - validate module params
2090 *
2091 * @adev: amdgpu_device pointer
2092 *
2093 * Validates certain module parameters and updates
2094 * the associated values used by the driver (all asics).
2095 */
amdgpu_device_check_arguments(struct amdgpu_device * adev)2096 static int amdgpu_device_check_arguments(struct amdgpu_device *adev)
2097 {
2098 int i;
2099
2100 if (amdgpu_sched_jobs < 4) {
2101 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
2102 amdgpu_sched_jobs);
2103 amdgpu_sched_jobs = 4;
2104 } else if (!is_power_of_2(amdgpu_sched_jobs)) {
2105 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
2106 amdgpu_sched_jobs);
2107 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
2108 }
2109
2110 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
2111 /* gart size must be greater or equal to 32M */
2112 dev_warn(adev->dev, "gart size (%d) too small\n",
2113 amdgpu_gart_size);
2114 amdgpu_gart_size = -1;
2115 }
2116
2117 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
2118 /* gtt size must be greater or equal to 32M */
2119 dev_warn(adev->dev, "gtt size (%d) too small\n",
2120 amdgpu_gtt_size);
2121 amdgpu_gtt_size = -1;
2122 }
2123
2124 /* valid range is between 4 and 9 inclusive */
2125 if (amdgpu_vm_fragment_size != -1 &&
2126 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
2127 dev_warn(adev->dev, "valid range is between 4 and 9\n");
2128 amdgpu_vm_fragment_size = -1;
2129 }
2130
2131 if (amdgpu_sched_hw_submission < 2) {
2132 dev_warn(adev->dev, "sched hw submission jobs (%d) must be at least 2\n",
2133 amdgpu_sched_hw_submission);
2134 amdgpu_sched_hw_submission = 2;
2135 } else if (!is_power_of_2(amdgpu_sched_hw_submission)) {
2136 dev_warn(adev->dev, "sched hw submission jobs (%d) must be a power of 2\n",
2137 amdgpu_sched_hw_submission);
2138 amdgpu_sched_hw_submission = roundup_pow_of_two(amdgpu_sched_hw_submission);
2139 }
2140
2141 if (amdgpu_reset_method < -1 || amdgpu_reset_method > 4) {
2142 dev_warn(adev->dev, "invalid option for reset method, reverting to default\n");
2143 amdgpu_reset_method = -1;
2144 }
2145
2146 amdgpu_device_check_smu_prv_buffer_size(adev);
2147
2148 amdgpu_device_check_vm_size(adev);
2149
2150 amdgpu_device_check_block_size(adev);
2151
2152 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
2153
2154 for (i = 0; i < MAX_XCP; i++) {
2155 switch (amdgpu_enforce_isolation) {
2156 case -1:
2157 case 0:
2158 default:
2159 /* disable */
2160 adev->enforce_isolation[i] = AMDGPU_ENFORCE_ISOLATION_DISABLE;
2161 break;
2162 case 1:
2163 /* enable */
2164 adev->enforce_isolation[i] =
2165 AMDGPU_ENFORCE_ISOLATION_ENABLE;
2166 break;
2167 case 2:
2168 /* enable legacy mode */
2169 adev->enforce_isolation[i] =
2170 AMDGPU_ENFORCE_ISOLATION_ENABLE_LEGACY;
2171 break;
2172 case 3:
2173 /* enable only process isolation without submitting cleaner shader */
2174 adev->enforce_isolation[i] =
2175 AMDGPU_ENFORCE_ISOLATION_NO_CLEANER_SHADER;
2176 break;
2177 }
2178 }
2179
2180 return 0;
2181 }
2182
2183 /**
2184 * amdgpu_switcheroo_set_state - set switcheroo state
2185 *
2186 * @pdev: pci dev pointer
2187 * @state: vga_switcheroo state
2188 *
2189 * Callback for the switcheroo driver. Suspends or resumes
2190 * the asics before or after it is powered up using ACPI methods.
2191 */
amdgpu_switcheroo_set_state(struct pci_dev * pdev,enum vga_switcheroo_state state)2192 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev,
2193 enum vga_switcheroo_state state)
2194 {
2195 struct drm_device *dev = pci_get_drvdata(pdev);
2196 int r;
2197
2198 if (amdgpu_device_supports_px(drm_to_adev(dev)) &&
2199 state == VGA_SWITCHEROO_OFF)
2200 return;
2201
2202 if (state == VGA_SWITCHEROO_ON) {
2203 pr_info("switched on\n");
2204 /* don't suspend or resume card normally */
2205 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
2206
2207 pci_set_power_state(pdev, PCI_D0);
2208 amdgpu_device_load_pci_state(pdev);
2209 r = pci_enable_device(pdev);
2210 if (r)
2211 dev_warn(&pdev->dev, "pci_enable_device failed (%d)\n",
2212 r);
2213 amdgpu_device_resume(dev, true);
2214
2215 dev->switch_power_state = DRM_SWITCH_POWER_ON;
2216 } else {
2217 dev_info(&pdev->dev, "switched off\n");
2218 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
2219 amdgpu_device_prepare(dev);
2220 amdgpu_device_suspend(dev, true);
2221 amdgpu_device_cache_pci_state(pdev);
2222 /* Shut down the device */
2223 pci_disable_device(pdev);
2224 pci_set_power_state(pdev, PCI_D3cold);
2225 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
2226 }
2227 }
2228
2229 /**
2230 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
2231 *
2232 * @pdev: pci dev pointer
2233 *
2234 * Callback for the switcheroo driver. Check of the switcheroo
2235 * state can be changed.
2236 * Returns true if the state can be changed, false if not.
2237 */
amdgpu_switcheroo_can_switch(struct pci_dev * pdev)2238 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
2239 {
2240 struct drm_device *dev = pci_get_drvdata(pdev);
2241
2242 /*
2243 * FIXME: open_count is protected by drm_global_mutex but that would lead to
2244 * locking inversion with the driver load path. And the access here is
2245 * completely racy anyway. So don't bother with locking for now.
2246 */
2247 return atomic_read(&dev->open_count) == 0;
2248 }
2249
2250 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
2251 .set_gpu_state = amdgpu_switcheroo_set_state,
2252 .reprobe = NULL,
2253 .can_switch = amdgpu_switcheroo_can_switch,
2254 };
2255
2256 /**
2257 * amdgpu_device_ip_set_clockgating_state - set the CG state
2258 *
2259 * @dev: amdgpu_device pointer
2260 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
2261 * @state: clockgating state (gate or ungate)
2262 *
2263 * Sets the requested clockgating state for all instances of
2264 * the hardware IP specified.
2265 * Returns the error code from the last instance.
2266 */
amdgpu_device_ip_set_clockgating_state(void * dev,enum amd_ip_block_type block_type,enum amd_clockgating_state state)2267 int amdgpu_device_ip_set_clockgating_state(void *dev,
2268 enum amd_ip_block_type block_type,
2269 enum amd_clockgating_state state)
2270 {
2271 struct amdgpu_device *adev = dev;
2272 int i, r = 0;
2273
2274 for (i = 0; i < adev->num_ip_blocks; i++) {
2275 if (!adev->ip_blocks[i].status.valid)
2276 continue;
2277 if (adev->ip_blocks[i].version->type != block_type)
2278 continue;
2279 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
2280 continue;
2281 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
2282 &adev->ip_blocks[i], state);
2283 if (r)
2284 dev_err(adev->dev,
2285 "set_clockgating_state of IP block <%s> failed %d\n",
2286 adev->ip_blocks[i].version->funcs->name, r);
2287 }
2288 return r;
2289 }
2290
2291 /**
2292 * amdgpu_device_ip_set_powergating_state - set the PG state
2293 *
2294 * @dev: amdgpu_device pointer
2295 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
2296 * @state: powergating state (gate or ungate)
2297 *
2298 * Sets the requested powergating state for all instances of
2299 * the hardware IP specified.
2300 * Returns the error code from the last instance.
2301 */
amdgpu_device_ip_set_powergating_state(void * dev,enum amd_ip_block_type block_type,enum amd_powergating_state state)2302 int amdgpu_device_ip_set_powergating_state(void *dev,
2303 enum amd_ip_block_type block_type,
2304 enum amd_powergating_state state)
2305 {
2306 struct amdgpu_device *adev = dev;
2307 int i, r = 0;
2308
2309 for (i = 0; i < adev->num_ip_blocks; i++) {
2310 if (!adev->ip_blocks[i].status.valid)
2311 continue;
2312 if (adev->ip_blocks[i].version->type != block_type)
2313 continue;
2314 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
2315 continue;
2316 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
2317 &adev->ip_blocks[i], state);
2318 if (r)
2319 dev_err(adev->dev,
2320 "set_powergating_state of IP block <%s> failed %d\n",
2321 adev->ip_blocks[i].version->funcs->name, r);
2322 }
2323 return r;
2324 }
2325
2326 /**
2327 * amdgpu_device_ip_get_clockgating_state - get the CG state
2328 *
2329 * @adev: amdgpu_device pointer
2330 * @flags: clockgating feature flags
2331 *
2332 * Walks the list of IPs on the device and updates the clockgating
2333 * flags for each IP.
2334 * Updates @flags with the feature flags for each hardware IP where
2335 * clockgating is enabled.
2336 */
amdgpu_device_ip_get_clockgating_state(struct amdgpu_device * adev,u64 * flags)2337 void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
2338 u64 *flags)
2339 {
2340 int i;
2341
2342 for (i = 0; i < adev->num_ip_blocks; i++) {
2343 if (!adev->ip_blocks[i].status.valid)
2344 continue;
2345 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
2346 adev->ip_blocks[i].version->funcs->get_clockgating_state(
2347 &adev->ip_blocks[i], flags);
2348 }
2349 }
2350
2351 /**
2352 * amdgpu_device_ip_wait_for_idle - wait for idle
2353 *
2354 * @adev: amdgpu_device pointer
2355 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
2356 *
2357 * Waits for the request hardware IP to be idle.
2358 * Returns 0 for success or a negative error code on failure.
2359 */
amdgpu_device_ip_wait_for_idle(struct amdgpu_device * adev,enum amd_ip_block_type block_type)2360 int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
2361 enum amd_ip_block_type block_type)
2362 {
2363 int i, r;
2364
2365 for (i = 0; i < adev->num_ip_blocks; i++) {
2366 if (!adev->ip_blocks[i].status.valid)
2367 continue;
2368 if (adev->ip_blocks[i].version->type == block_type) {
2369 if (adev->ip_blocks[i].version->funcs->wait_for_idle) {
2370 r = adev->ip_blocks[i].version->funcs->wait_for_idle(
2371 &adev->ip_blocks[i]);
2372 if (r)
2373 return r;
2374 }
2375 break;
2376 }
2377 }
2378 return 0;
2379
2380 }
2381
2382 /**
2383 * amdgpu_device_ip_is_valid - is the hardware IP enabled
2384 *
2385 * @adev: amdgpu_device pointer
2386 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
2387 *
2388 * Check if the hardware IP is enable or not.
2389 * Returns true if it the IP is enable, false if not.
2390 */
amdgpu_device_ip_is_valid(struct amdgpu_device * adev,enum amd_ip_block_type block_type)2391 bool amdgpu_device_ip_is_valid(struct amdgpu_device *adev,
2392 enum amd_ip_block_type block_type)
2393 {
2394 int i;
2395
2396 for (i = 0; i < adev->num_ip_blocks; i++) {
2397 if (adev->ip_blocks[i].version->type == block_type)
2398 return adev->ip_blocks[i].status.valid;
2399 }
2400 return false;
2401
2402 }
2403
2404 /**
2405 * amdgpu_device_ip_get_ip_block - get a hw IP pointer
2406 *
2407 * @adev: amdgpu_device pointer
2408 * @type: Type of hardware IP (SMU, GFX, UVD, etc.)
2409 *
2410 * Returns a pointer to the hardware IP block structure
2411 * if it exists for the asic, otherwise NULL.
2412 */
2413 struct amdgpu_ip_block *
amdgpu_device_ip_get_ip_block(struct amdgpu_device * adev,enum amd_ip_block_type type)2414 amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
2415 enum amd_ip_block_type type)
2416 {
2417 int i;
2418
2419 for (i = 0; i < adev->num_ip_blocks; i++)
2420 if (adev->ip_blocks[i].version->type == type)
2421 return &adev->ip_blocks[i];
2422
2423 return NULL;
2424 }
2425
2426 /**
2427 * amdgpu_device_ip_block_version_cmp
2428 *
2429 * @adev: amdgpu_device pointer
2430 * @type: enum amd_ip_block_type
2431 * @major: major version
2432 * @minor: minor version
2433 *
2434 * return 0 if equal or greater
2435 * return 1 if smaller or the ip_block doesn't exist
2436 */
amdgpu_device_ip_block_version_cmp(struct amdgpu_device * adev,enum amd_ip_block_type type,u32 major,u32 minor)2437 int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
2438 enum amd_ip_block_type type,
2439 u32 major, u32 minor)
2440 {
2441 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
2442
2443 if (ip_block && ((ip_block->version->major > major) ||
2444 ((ip_block->version->major == major) &&
2445 (ip_block->version->minor >= minor))))
2446 return 0;
2447
2448 return 1;
2449 }
2450
2451 static const char *ip_block_names[] = {
2452 [AMD_IP_BLOCK_TYPE_COMMON] = "common",
2453 [AMD_IP_BLOCK_TYPE_GMC] = "gmc",
2454 [AMD_IP_BLOCK_TYPE_IH] = "ih",
2455 [AMD_IP_BLOCK_TYPE_SMC] = "smu",
2456 [AMD_IP_BLOCK_TYPE_PSP] = "psp",
2457 [AMD_IP_BLOCK_TYPE_DCE] = "dce",
2458 [AMD_IP_BLOCK_TYPE_GFX] = "gfx",
2459 [AMD_IP_BLOCK_TYPE_SDMA] = "sdma",
2460 [AMD_IP_BLOCK_TYPE_UVD] = "uvd",
2461 [AMD_IP_BLOCK_TYPE_VCE] = "vce",
2462 [AMD_IP_BLOCK_TYPE_ACP] = "acp",
2463 [AMD_IP_BLOCK_TYPE_VCN] = "vcn",
2464 [AMD_IP_BLOCK_TYPE_MES] = "mes",
2465 [AMD_IP_BLOCK_TYPE_JPEG] = "jpeg",
2466 [AMD_IP_BLOCK_TYPE_VPE] = "vpe",
2467 [AMD_IP_BLOCK_TYPE_UMSCH_MM] = "umsch_mm",
2468 [AMD_IP_BLOCK_TYPE_ISP] = "isp",
2469 };
2470
ip_block_name(struct amdgpu_device * adev,enum amd_ip_block_type type)2471 static const char *ip_block_name(struct amdgpu_device *adev, enum amd_ip_block_type type)
2472 {
2473 int idx = (int)type;
2474
2475 return idx < ARRAY_SIZE(ip_block_names) ? ip_block_names[idx] : "unknown";
2476 }
2477
2478 /**
2479 * amdgpu_device_ip_block_add
2480 *
2481 * @adev: amdgpu_device pointer
2482 * @ip_block_version: pointer to the IP to add
2483 *
2484 * Adds the IP block driver information to the collection of IPs
2485 * on the asic.
2486 */
amdgpu_device_ip_block_add(struct amdgpu_device * adev,const struct amdgpu_ip_block_version * ip_block_version)2487 int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
2488 const struct amdgpu_ip_block_version *ip_block_version)
2489 {
2490 if (!ip_block_version)
2491 return -EINVAL;
2492
2493 switch (ip_block_version->type) {
2494 case AMD_IP_BLOCK_TYPE_VCN:
2495 if (adev->harvest_ip_mask & AMD_HARVEST_IP_VCN_MASK)
2496 return 0;
2497 break;
2498 case AMD_IP_BLOCK_TYPE_JPEG:
2499 if (adev->harvest_ip_mask & AMD_HARVEST_IP_JPEG_MASK)
2500 return 0;
2501 break;
2502 default:
2503 break;
2504 }
2505
2506 dev_info(adev->dev, "detected ip block number %d <%s_v%d_%d_%d> (%s)\n",
2507 adev->num_ip_blocks,
2508 ip_block_name(adev, ip_block_version->type),
2509 ip_block_version->major,
2510 ip_block_version->minor,
2511 ip_block_version->rev,
2512 ip_block_version->funcs->name);
2513
2514 adev->ip_blocks[adev->num_ip_blocks].adev = adev;
2515
2516 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
2517
2518 return 0;
2519 }
2520
2521 /**
2522 * amdgpu_device_enable_virtual_display - enable virtual display feature
2523 *
2524 * @adev: amdgpu_device pointer
2525 *
2526 * Enabled the virtual display feature if the user has enabled it via
2527 * the module parameter virtual_display. This feature provides a virtual
2528 * display hardware on headless boards or in virtualized environments.
2529 * This function parses and validates the configuration string specified by
2530 * the user and configures the virtual display configuration (number of
2531 * virtual connectors, crtcs, etc.) specified.
2532 */
amdgpu_device_enable_virtual_display(struct amdgpu_device * adev)2533 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
2534 {
2535 adev->enable_virtual_display = false;
2536
2537 if (amdgpu_virtual_display) {
2538 const char *pci_address_name = pci_name(adev->pdev);
2539 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
2540
2541 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
2542 pciaddstr_tmp = pciaddstr;
2543 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
2544 pciaddname = strsep(&pciaddname_tmp, ",");
2545 if (!strcmp("all", pciaddname)
2546 || !strcmp(pci_address_name, pciaddname)) {
2547 long num_crtc;
2548 int res = -1;
2549
2550 adev->enable_virtual_display = true;
2551
2552 if (pciaddname_tmp)
2553 res = kstrtol(pciaddname_tmp, 10,
2554 &num_crtc);
2555
2556 if (!res) {
2557 if (num_crtc < 1)
2558 num_crtc = 1;
2559 if (num_crtc > 6)
2560 num_crtc = 6;
2561 adev->mode_info.num_crtc = num_crtc;
2562 } else {
2563 adev->mode_info.num_crtc = 1;
2564 }
2565 break;
2566 }
2567 }
2568
2569 dev_info(
2570 adev->dev,
2571 "virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
2572 amdgpu_virtual_display, pci_address_name,
2573 adev->enable_virtual_display, adev->mode_info.num_crtc);
2574
2575 kfree(pciaddstr);
2576 }
2577 }
2578
amdgpu_device_set_sriov_virtual_display(struct amdgpu_device * adev)2579 void amdgpu_device_set_sriov_virtual_display(struct amdgpu_device *adev)
2580 {
2581 if (amdgpu_sriov_vf(adev) && !adev->enable_virtual_display) {
2582 adev->mode_info.num_crtc = 1;
2583 adev->enable_virtual_display = true;
2584 dev_info(adev->dev, "virtual_display:%d, num_crtc:%d\n",
2585 adev->enable_virtual_display,
2586 adev->mode_info.num_crtc);
2587 }
2588 }
2589
2590 /**
2591 * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware
2592 *
2593 * @adev: amdgpu_device pointer
2594 *
2595 * Parses the asic configuration parameters specified in the gpu info
2596 * firmware and makes them available to the driver for use in configuring
2597 * the asic.
2598 * Returns 0 on success, -EINVAL on failure.
2599 */
amdgpu_device_parse_gpu_info_fw(struct amdgpu_device * adev)2600 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
2601 {
2602 const char *chip_name;
2603 int err;
2604 const struct gpu_info_firmware_header_v1_0 *hdr;
2605
2606 adev->firmware.gpu_info_fw = NULL;
2607
2608 switch (adev->asic_type) {
2609 default:
2610 return 0;
2611 case CHIP_VEGA10:
2612 chip_name = "vega10";
2613 break;
2614 case CHIP_VEGA12:
2615 chip_name = "vega12";
2616 break;
2617 case CHIP_RAVEN:
2618 if (adev->apu_flags & AMD_APU_IS_RAVEN2)
2619 chip_name = "raven2";
2620 else if (adev->apu_flags & AMD_APU_IS_PICASSO)
2621 chip_name = "picasso";
2622 else
2623 chip_name = "raven";
2624 break;
2625 case CHIP_ARCTURUS:
2626 chip_name = "arcturus";
2627 break;
2628 case CHIP_NAVI12:
2629 if (adev->mman.discovery_bin)
2630 return 0;
2631 chip_name = "navi12";
2632 break;
2633 case CHIP_CYAN_SKILLFISH:
2634 chip_name = "cyan_skillfish";
2635 break;
2636 }
2637
2638 err = amdgpu_ucode_request(adev, &adev->firmware.gpu_info_fw,
2639 AMDGPU_UCODE_OPTIONAL,
2640 "amdgpu/%s_gpu_info.bin", chip_name);
2641 if (err) {
2642 dev_err(adev->dev,
2643 "Failed to get gpu_info firmware \"%s_gpu_info.bin\"\n",
2644 chip_name);
2645 goto out;
2646 }
2647
2648 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
2649 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
2650
2651 switch (hdr->version_major) {
2652 case 1:
2653 {
2654 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
2655 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
2656 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2657
2658 /*
2659 * Should be dropped when DAL no longer needs it.
2660 */
2661 if (adev->asic_type == CHIP_NAVI12)
2662 goto parse_soc_bounding_box;
2663
2664 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
2665 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
2666 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
2667 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
2668 adev->gfx.config.max_texture_channel_caches =
2669 le32_to_cpu(gpu_info_fw->gc_num_tccs);
2670 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
2671 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
2672 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
2673 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
2674 adev->gfx.config.double_offchip_lds_buf =
2675 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
2676 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
2677 adev->gfx.cu_info.max_waves_per_simd =
2678 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
2679 adev->gfx.cu_info.max_scratch_slots_per_cu =
2680 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
2681 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
2682 if (hdr->version_minor >= 1) {
2683 const struct gpu_info_firmware_v1_1 *gpu_info_fw =
2684 (const struct gpu_info_firmware_v1_1 *)(adev->firmware.gpu_info_fw->data +
2685 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2686 adev->gfx.config.num_sc_per_sh =
2687 le32_to_cpu(gpu_info_fw->num_sc_per_sh);
2688 adev->gfx.config.num_packer_per_sc =
2689 le32_to_cpu(gpu_info_fw->num_packer_per_sc);
2690 }
2691
2692 parse_soc_bounding_box:
2693 /*
2694 * soc bounding box info is not integrated in disocovery table,
2695 * we always need to parse it from gpu info firmware if needed.
2696 */
2697 if (hdr->version_minor == 2) {
2698 const struct gpu_info_firmware_v1_2 *gpu_info_fw =
2699 (const struct gpu_info_firmware_v1_2 *)(adev->firmware.gpu_info_fw->data +
2700 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2701 adev->dm.soc_bounding_box = &gpu_info_fw->soc_bounding_box;
2702 }
2703 break;
2704 }
2705 default:
2706 dev_err(adev->dev,
2707 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
2708 err = -EINVAL;
2709 goto out;
2710 }
2711 out:
2712 return err;
2713 }
2714
amdgpu_uid_init(struct amdgpu_device * adev)2715 static void amdgpu_uid_init(struct amdgpu_device *adev)
2716 {
2717 /* Initialize the UID for the device */
2718 adev->uid_info = kzalloc(sizeof(struct amdgpu_uid), GFP_KERNEL);
2719 if (!adev->uid_info) {
2720 dev_warn(adev->dev, "Failed to allocate memory for UID\n");
2721 return;
2722 }
2723 adev->uid_info->adev = adev;
2724 }
2725
amdgpu_uid_fini(struct amdgpu_device * adev)2726 static void amdgpu_uid_fini(struct amdgpu_device *adev)
2727 {
2728 /* Free the UID memory */
2729 kfree(adev->uid_info);
2730 adev->uid_info = NULL;
2731 }
2732
2733 /**
2734 * amdgpu_device_ip_early_init - run early init for hardware IPs
2735 *
2736 * @adev: amdgpu_device pointer
2737 *
2738 * Early initialization pass for hardware IPs. The hardware IPs that make
2739 * up each asic are discovered each IP's early_init callback is run. This
2740 * is the first stage in initializing the asic.
2741 * Returns 0 on success, negative error code on failure.
2742 */
amdgpu_device_ip_early_init(struct amdgpu_device * adev)2743 static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
2744 {
2745 struct amdgpu_ip_block *ip_block;
2746 struct pci_dev *parent;
2747 bool total, skip_bios;
2748 uint32_t bios_flags;
2749 int i, r;
2750
2751 amdgpu_device_enable_virtual_display(adev);
2752
2753 if (amdgpu_sriov_vf(adev)) {
2754 r = amdgpu_virt_request_full_gpu(adev, true);
2755 if (r)
2756 return r;
2757 }
2758
2759 switch (adev->asic_type) {
2760 #ifdef CONFIG_DRM_AMDGPU_SI
2761 case CHIP_VERDE:
2762 case CHIP_TAHITI:
2763 case CHIP_PITCAIRN:
2764 case CHIP_OLAND:
2765 case CHIP_HAINAN:
2766 adev->family = AMDGPU_FAMILY_SI;
2767 r = si_set_ip_blocks(adev);
2768 if (r)
2769 return r;
2770 break;
2771 #endif
2772 #ifdef CONFIG_DRM_AMDGPU_CIK
2773 case CHIP_BONAIRE:
2774 case CHIP_HAWAII:
2775 case CHIP_KAVERI:
2776 case CHIP_KABINI:
2777 case CHIP_MULLINS:
2778 if (adev->flags & AMD_IS_APU)
2779 adev->family = AMDGPU_FAMILY_KV;
2780 else
2781 adev->family = AMDGPU_FAMILY_CI;
2782
2783 r = cik_set_ip_blocks(adev);
2784 if (r)
2785 return r;
2786 break;
2787 #endif
2788 case CHIP_TOPAZ:
2789 case CHIP_TONGA:
2790 case CHIP_FIJI:
2791 case CHIP_POLARIS10:
2792 case CHIP_POLARIS11:
2793 case CHIP_POLARIS12:
2794 case CHIP_VEGAM:
2795 case CHIP_CARRIZO:
2796 case CHIP_STONEY:
2797 if (adev->flags & AMD_IS_APU)
2798 adev->family = AMDGPU_FAMILY_CZ;
2799 else
2800 adev->family = AMDGPU_FAMILY_VI;
2801
2802 r = vi_set_ip_blocks(adev);
2803 if (r)
2804 return r;
2805 break;
2806 default:
2807 r = amdgpu_discovery_set_ip_blocks(adev);
2808 if (r)
2809 return r;
2810 break;
2811 }
2812
2813 /* Check for IP version 9.4.3 with A0 hardware */
2814 if (amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 4, 3) &&
2815 !amdgpu_device_get_rev_id(adev)) {
2816 dev_err(adev->dev, "Unsupported A0 hardware\n");
2817 return -ENODEV; /* device unsupported - no device error */
2818 }
2819
2820 if (amdgpu_has_atpx() &&
2821 (amdgpu_is_atpx_hybrid() ||
2822 amdgpu_has_atpx_dgpu_power_cntl()) &&
2823 ((adev->flags & AMD_IS_APU) == 0) &&
2824 !dev_is_removable(&adev->pdev->dev))
2825 adev->flags |= AMD_IS_PX;
2826
2827 if (!(adev->flags & AMD_IS_APU)) {
2828 parent = pcie_find_root_port(adev->pdev);
2829 adev->has_pr3 = parent ? pci_pr3_present(parent) : false;
2830 }
2831
2832 adev->pm.pp_feature = amdgpu_pp_feature_mask;
2833 if (amdgpu_sriov_vf(adev) || sched_policy == KFD_SCHED_POLICY_NO_HWS)
2834 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
2835 if (amdgpu_sriov_vf(adev) && adev->asic_type == CHIP_SIENNA_CICHLID)
2836 adev->pm.pp_feature &= ~PP_OVERDRIVE_MASK;
2837 if (!amdgpu_device_pcie_dynamic_switching_supported(adev))
2838 adev->pm.pp_feature &= ~PP_PCIE_DPM_MASK;
2839
2840 adev->virt.is_xgmi_node_migrate_enabled = false;
2841 if (amdgpu_sriov_vf(adev)) {
2842 adev->virt.is_xgmi_node_migrate_enabled =
2843 amdgpu_ip_version((adev), GC_HWIP, 0) == IP_VERSION(9, 4, 4);
2844 }
2845
2846 total = true;
2847 for (i = 0; i < adev->num_ip_blocks; i++) {
2848 ip_block = &adev->ip_blocks[i];
2849
2850 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
2851 dev_warn(adev->dev, "disabled ip block: %d <%s>\n", i,
2852 adev->ip_blocks[i].version->funcs->name);
2853 adev->ip_blocks[i].status.valid = false;
2854 } else if (ip_block->version->funcs->early_init) {
2855 r = ip_block->version->funcs->early_init(ip_block);
2856 if (r == -ENOENT) {
2857 adev->ip_blocks[i].status.valid = false;
2858 } else if (r) {
2859 dev_err(adev->dev,
2860 "early_init of IP block <%s> failed %d\n",
2861 adev->ip_blocks[i].version->funcs->name,
2862 r);
2863 total = false;
2864 } else {
2865 adev->ip_blocks[i].status.valid = true;
2866 }
2867 } else {
2868 adev->ip_blocks[i].status.valid = true;
2869 }
2870 /* get the vbios after the asic_funcs are set up */
2871 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON) {
2872 r = amdgpu_device_parse_gpu_info_fw(adev);
2873 if (r)
2874 return r;
2875
2876 bios_flags = amdgpu_device_get_vbios_flags(adev);
2877 skip_bios = !!(bios_flags & AMDGPU_VBIOS_SKIP);
2878 /* Read BIOS */
2879 if (!skip_bios) {
2880 bool optional =
2881 !!(bios_flags & AMDGPU_VBIOS_OPTIONAL);
2882 if (!amdgpu_get_bios(adev) && !optional)
2883 return -EINVAL;
2884
2885 if (optional && !adev->bios)
2886 dev_info(
2887 adev->dev,
2888 "VBIOS image optional, proceeding without VBIOS image");
2889
2890 if (adev->bios) {
2891 r = amdgpu_atombios_init(adev);
2892 if (r) {
2893 dev_err(adev->dev,
2894 "amdgpu_atombios_init failed\n");
2895 amdgpu_vf_error_put(
2896 adev,
2897 AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL,
2898 0, 0);
2899 return r;
2900 }
2901 }
2902 }
2903
2904 /*get pf2vf msg info at it's earliest time*/
2905 if (amdgpu_sriov_vf(adev))
2906 amdgpu_virt_init_data_exchange(adev);
2907
2908 }
2909 }
2910 if (!total)
2911 return -ENODEV;
2912
2913 if (adev->gmc.xgmi.supported)
2914 amdgpu_xgmi_early_init(adev);
2915
2916 if (amdgpu_is_multi_aid(adev))
2917 amdgpu_uid_init(adev);
2918 ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
2919 if (ip_block->status.valid != false)
2920 amdgpu_amdkfd_device_probe(adev);
2921
2922 adev->cg_flags &= amdgpu_cg_mask;
2923 adev->pg_flags &= amdgpu_pg_mask;
2924
2925 return 0;
2926 }
2927
amdgpu_device_ip_hw_init_phase1(struct amdgpu_device * adev)2928 static int amdgpu_device_ip_hw_init_phase1(struct amdgpu_device *adev)
2929 {
2930 int i, r;
2931
2932 for (i = 0; i < adev->num_ip_blocks; i++) {
2933 if (!adev->ip_blocks[i].status.sw)
2934 continue;
2935 if (adev->ip_blocks[i].status.hw)
2936 continue;
2937 if (!amdgpu_ip_member_of_hwini(
2938 adev, adev->ip_blocks[i].version->type))
2939 continue;
2940 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2941 (amdgpu_sriov_vf(adev) && (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)) ||
2942 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
2943 r = adev->ip_blocks[i].version->funcs->hw_init(&adev->ip_blocks[i]);
2944 if (r) {
2945 dev_err(adev->dev,
2946 "hw_init of IP block <%s> failed %d\n",
2947 adev->ip_blocks[i].version->funcs->name,
2948 r);
2949 return r;
2950 }
2951 adev->ip_blocks[i].status.hw = true;
2952 }
2953 }
2954
2955 return 0;
2956 }
2957
amdgpu_device_ip_hw_init_phase2(struct amdgpu_device * adev)2958 static int amdgpu_device_ip_hw_init_phase2(struct amdgpu_device *adev)
2959 {
2960 int i, r;
2961
2962 for (i = 0; i < adev->num_ip_blocks; i++) {
2963 if (!adev->ip_blocks[i].status.sw)
2964 continue;
2965 if (adev->ip_blocks[i].status.hw)
2966 continue;
2967 if (!amdgpu_ip_member_of_hwini(
2968 adev, adev->ip_blocks[i].version->type))
2969 continue;
2970 r = adev->ip_blocks[i].version->funcs->hw_init(&adev->ip_blocks[i]);
2971 if (r) {
2972 dev_err(adev->dev,
2973 "hw_init of IP block <%s> failed %d\n",
2974 adev->ip_blocks[i].version->funcs->name, r);
2975 return r;
2976 }
2977 adev->ip_blocks[i].status.hw = true;
2978 }
2979
2980 return 0;
2981 }
2982
amdgpu_device_fw_loading(struct amdgpu_device * adev)2983 static int amdgpu_device_fw_loading(struct amdgpu_device *adev)
2984 {
2985 int r = 0;
2986 int i;
2987 uint32_t smu_version;
2988
2989 if (adev->asic_type >= CHIP_VEGA10) {
2990 for (i = 0; i < adev->num_ip_blocks; i++) {
2991 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_PSP)
2992 continue;
2993
2994 if (!amdgpu_ip_member_of_hwini(adev,
2995 AMD_IP_BLOCK_TYPE_PSP))
2996 break;
2997
2998 if (!adev->ip_blocks[i].status.sw)
2999 continue;
3000
3001 /* no need to do the fw loading again if already done*/
3002 if (adev->ip_blocks[i].status.hw == true)
3003 break;
3004
3005 if (amdgpu_in_reset(adev) || adev->in_suspend) {
3006 r = amdgpu_ip_block_resume(&adev->ip_blocks[i]);
3007 if (r)
3008 return r;
3009 } else {
3010 r = adev->ip_blocks[i].version->funcs->hw_init(&adev->ip_blocks[i]);
3011 if (r) {
3012 dev_err(adev->dev,
3013 "hw_init of IP block <%s> failed %d\n",
3014 adev->ip_blocks[i]
3015 .version->funcs->name,
3016 r);
3017 return r;
3018 }
3019 adev->ip_blocks[i].status.hw = true;
3020 }
3021 break;
3022 }
3023 }
3024
3025 if (!amdgpu_sriov_vf(adev) || adev->asic_type == CHIP_TONGA)
3026 r = amdgpu_pm_load_smu_firmware(adev, &smu_version);
3027
3028 return r;
3029 }
3030
amdgpu_device_init_schedulers(struct amdgpu_device * adev)3031 static int amdgpu_device_init_schedulers(struct amdgpu_device *adev)
3032 {
3033 struct drm_sched_init_args args = {
3034 .ops = &amdgpu_sched_ops,
3035 .num_rqs = DRM_SCHED_PRIORITY_COUNT,
3036 .timeout_wq = adev->reset_domain->wq,
3037 .dev = adev->dev,
3038 };
3039 long timeout;
3040 int r, i;
3041
3042 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3043 struct amdgpu_ring *ring = adev->rings[i];
3044
3045 /* No need to setup the GPU scheduler for rings that don't need it */
3046 if (!ring || ring->no_scheduler)
3047 continue;
3048
3049 switch (ring->funcs->type) {
3050 case AMDGPU_RING_TYPE_GFX:
3051 timeout = adev->gfx_timeout;
3052 break;
3053 case AMDGPU_RING_TYPE_COMPUTE:
3054 timeout = adev->compute_timeout;
3055 break;
3056 case AMDGPU_RING_TYPE_SDMA:
3057 timeout = adev->sdma_timeout;
3058 break;
3059 default:
3060 timeout = adev->video_timeout;
3061 break;
3062 }
3063
3064 args.timeout = timeout;
3065 args.credit_limit = ring->num_hw_submission;
3066 args.score = ring->sched_score;
3067 args.name = ring->name;
3068
3069 r = drm_sched_init(&ring->sched, &args);
3070 if (r) {
3071 dev_err(adev->dev,
3072 "Failed to create scheduler on ring %s.\n",
3073 ring->name);
3074 return r;
3075 }
3076 r = amdgpu_uvd_entity_init(adev, ring);
3077 if (r) {
3078 dev_err(adev->dev,
3079 "Failed to create UVD scheduling entity on ring %s.\n",
3080 ring->name);
3081 return r;
3082 }
3083 r = amdgpu_vce_entity_init(adev, ring);
3084 if (r) {
3085 dev_err(adev->dev,
3086 "Failed to create VCE scheduling entity on ring %s.\n",
3087 ring->name);
3088 return r;
3089 }
3090 }
3091
3092 if (adev->xcp_mgr)
3093 amdgpu_xcp_update_partition_sched_list(adev);
3094
3095 return 0;
3096 }
3097
3098
3099 /**
3100 * amdgpu_device_ip_init - run init for hardware IPs
3101 *
3102 * @adev: amdgpu_device pointer
3103 *
3104 * Main initialization pass for hardware IPs. The list of all the hardware
3105 * IPs that make up the asic is walked and the sw_init and hw_init callbacks
3106 * are run. sw_init initializes the software state associated with each IP
3107 * and hw_init initializes the hardware associated with each IP.
3108 * Returns 0 on success, negative error code on failure.
3109 */
amdgpu_device_ip_init(struct amdgpu_device * adev)3110 static int amdgpu_device_ip_init(struct amdgpu_device *adev)
3111 {
3112 bool init_badpage;
3113 int i, r;
3114
3115 r = amdgpu_ras_init(adev);
3116 if (r)
3117 return r;
3118
3119 for (i = 0; i < adev->num_ip_blocks; i++) {
3120 if (!adev->ip_blocks[i].status.valid)
3121 continue;
3122 if (adev->ip_blocks[i].version->funcs->sw_init) {
3123 r = adev->ip_blocks[i].version->funcs->sw_init(&adev->ip_blocks[i]);
3124 if (r) {
3125 dev_err(adev->dev,
3126 "sw_init of IP block <%s> failed %d\n",
3127 adev->ip_blocks[i].version->funcs->name,
3128 r);
3129 goto init_failed;
3130 }
3131 }
3132 adev->ip_blocks[i].status.sw = true;
3133
3134 if (!amdgpu_ip_member_of_hwini(
3135 adev, adev->ip_blocks[i].version->type))
3136 continue;
3137
3138 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON) {
3139 /* need to do common hw init early so everything is set up for gmc */
3140 r = adev->ip_blocks[i].version->funcs->hw_init(&adev->ip_blocks[i]);
3141 if (r) {
3142 dev_err(adev->dev, "hw_init %d failed %d\n", i,
3143 r);
3144 goto init_failed;
3145 }
3146 adev->ip_blocks[i].status.hw = true;
3147 } else if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
3148 /* need to do gmc hw init early so we can allocate gpu mem */
3149 /* Try to reserve bad pages early */
3150 if (amdgpu_sriov_vf(adev))
3151 amdgpu_virt_exchange_data(adev);
3152
3153 r = amdgpu_device_mem_scratch_init(adev);
3154 if (r) {
3155 dev_err(adev->dev,
3156 "amdgpu_mem_scratch_init failed %d\n",
3157 r);
3158 goto init_failed;
3159 }
3160 r = adev->ip_blocks[i].version->funcs->hw_init(&adev->ip_blocks[i]);
3161 if (r) {
3162 dev_err(adev->dev, "hw_init %d failed %d\n", i,
3163 r);
3164 goto init_failed;
3165 }
3166 r = amdgpu_device_wb_init(adev);
3167 if (r) {
3168 dev_err(adev->dev,
3169 "amdgpu_device_wb_init failed %d\n", r);
3170 goto init_failed;
3171 }
3172 adev->ip_blocks[i].status.hw = true;
3173
3174 /* right after GMC hw init, we create CSA */
3175 if (adev->gfx.mcbp) {
3176 r = amdgpu_allocate_static_csa(adev, &adev->virt.csa_obj,
3177 AMDGPU_GEM_DOMAIN_VRAM |
3178 AMDGPU_GEM_DOMAIN_GTT,
3179 AMDGPU_CSA_SIZE);
3180 if (r) {
3181 dev_err(adev->dev,
3182 "allocate CSA failed %d\n", r);
3183 goto init_failed;
3184 }
3185 }
3186
3187 r = amdgpu_seq64_init(adev);
3188 if (r) {
3189 dev_err(adev->dev, "allocate seq64 failed %d\n",
3190 r);
3191 goto init_failed;
3192 }
3193 }
3194 }
3195
3196 if (amdgpu_sriov_vf(adev))
3197 amdgpu_virt_init_data_exchange(adev);
3198
3199 r = amdgpu_ib_pool_init(adev);
3200 if (r) {
3201 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
3202 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
3203 goto init_failed;
3204 }
3205
3206 r = amdgpu_ucode_create_bo(adev); /* create ucode bo when sw_init complete*/
3207 if (r)
3208 goto init_failed;
3209
3210 r = amdgpu_device_ip_hw_init_phase1(adev);
3211 if (r)
3212 goto init_failed;
3213
3214 r = amdgpu_device_fw_loading(adev);
3215 if (r)
3216 goto init_failed;
3217
3218 r = amdgpu_device_ip_hw_init_phase2(adev);
3219 if (r)
3220 goto init_failed;
3221
3222 /*
3223 * retired pages will be loaded from eeprom and reserved here,
3224 * it should be called after amdgpu_device_ip_hw_init_phase2 since
3225 * for some ASICs the RAS EEPROM code relies on SMU fully functioning
3226 * for I2C communication which only true at this point.
3227 *
3228 * amdgpu_ras_recovery_init may fail, but the upper only cares the
3229 * failure from bad gpu situation and stop amdgpu init process
3230 * accordingly. For other failed cases, it will still release all
3231 * the resource and print error message, rather than returning one
3232 * negative value to upper level.
3233 *
3234 * Note: theoretically, this should be called before all vram allocations
3235 * to protect retired page from abusing
3236 */
3237 init_badpage = (adev->init_lvl->level != AMDGPU_INIT_LEVEL_MINIMAL_XGMI);
3238 r = amdgpu_ras_recovery_init(adev, init_badpage);
3239 if (r)
3240 goto init_failed;
3241
3242 /**
3243 * In case of XGMI grab extra reference for reset domain for this device
3244 */
3245 if (adev->gmc.xgmi.num_physical_nodes > 1) {
3246 if (amdgpu_xgmi_add_device(adev) == 0) {
3247 if (!amdgpu_sriov_vf(adev)) {
3248 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
3249
3250 if (WARN_ON(!hive)) {
3251 r = -ENOENT;
3252 goto init_failed;
3253 }
3254
3255 if (!hive->reset_domain ||
3256 !amdgpu_reset_get_reset_domain(hive->reset_domain)) {
3257 r = -ENOENT;
3258 amdgpu_put_xgmi_hive(hive);
3259 goto init_failed;
3260 }
3261
3262 /* Drop the early temporary reset domain we created for device */
3263 amdgpu_reset_put_reset_domain(adev->reset_domain);
3264 adev->reset_domain = hive->reset_domain;
3265 amdgpu_put_xgmi_hive(hive);
3266 }
3267 }
3268 }
3269
3270 r = amdgpu_device_init_schedulers(adev);
3271 if (r)
3272 goto init_failed;
3273
3274 if (adev->mman.buffer_funcs_ring->sched.ready)
3275 amdgpu_ttm_set_buffer_funcs_status(adev, true);
3276
3277 /* Don't init kfd if whole hive need to be reset during init */
3278 if (adev->init_lvl->level != AMDGPU_INIT_LEVEL_MINIMAL_XGMI) {
3279 kgd2kfd_init_zone_device(adev);
3280 amdgpu_amdkfd_device_init(adev);
3281 }
3282
3283 amdgpu_fru_get_product_info(adev);
3284
3285 if (!amdgpu_sriov_vf(adev) || amdgpu_sriov_ras_cper_en(adev))
3286 r = amdgpu_cper_init(adev);
3287
3288 init_failed:
3289
3290 return r;
3291 }
3292
3293 /**
3294 * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
3295 *
3296 * @adev: amdgpu_device pointer
3297 *
3298 * Writes a reset magic value to the gart pointer in VRAM. The driver calls
3299 * this function before a GPU reset. If the value is retained after a
3300 * GPU reset, VRAM has not been lost. Some GPU resets may destroy VRAM contents.
3301 */
amdgpu_device_fill_reset_magic(struct amdgpu_device * adev)3302 static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
3303 {
3304 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
3305 }
3306
3307 /**
3308 * amdgpu_device_check_vram_lost - check if vram is valid
3309 *
3310 * @adev: amdgpu_device pointer
3311 *
3312 * Checks the reset magic value written to the gart pointer in VRAM.
3313 * The driver calls this after a GPU reset to see if the contents of
3314 * VRAM is lost or now.
3315 * returns true if vram is lost, false if not.
3316 */
amdgpu_device_check_vram_lost(struct amdgpu_device * adev)3317 static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
3318 {
3319 if (memcmp(adev->gart.ptr, adev->reset_magic,
3320 AMDGPU_RESET_MAGIC_NUM))
3321 return true;
3322
3323 if (!amdgpu_in_reset(adev))
3324 return false;
3325
3326 /*
3327 * For all ASICs with baco/mode1 reset, the VRAM is
3328 * always assumed to be lost.
3329 */
3330 switch (amdgpu_asic_reset_method(adev)) {
3331 case AMD_RESET_METHOD_LEGACY:
3332 case AMD_RESET_METHOD_LINK:
3333 case AMD_RESET_METHOD_BACO:
3334 case AMD_RESET_METHOD_MODE1:
3335 return true;
3336 default:
3337 return false;
3338 }
3339 }
3340
3341 /**
3342 * amdgpu_device_set_cg_state - set clockgating for amdgpu device
3343 *
3344 * @adev: amdgpu_device pointer
3345 * @state: clockgating state (gate or ungate)
3346 *
3347 * The list of all the hardware IPs that make up the asic is walked and the
3348 * set_clockgating_state callbacks are run.
3349 * Late initialization pass enabling clockgating for hardware IPs.
3350 * Fini or suspend, pass disabling clockgating for hardware IPs.
3351 * Returns 0 on success, negative error code on failure.
3352 */
3353
amdgpu_device_set_cg_state(struct amdgpu_device * adev,enum amd_clockgating_state state)3354 int amdgpu_device_set_cg_state(struct amdgpu_device *adev,
3355 enum amd_clockgating_state state)
3356 {
3357 int i, j, r;
3358
3359 if (amdgpu_emu_mode == 1)
3360 return 0;
3361
3362 for (j = 0; j < adev->num_ip_blocks; j++) {
3363 i = state == AMD_CG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
3364 if (!adev->ip_blocks[i].status.late_initialized)
3365 continue;
3366 /* skip CG for GFX, SDMA on S0ix */
3367 if (adev->in_s0ix &&
3368 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX ||
3369 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SDMA))
3370 continue;
3371 /* skip CG for VCE/UVD, it's handled specially */
3372 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
3373 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
3374 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
3375 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG &&
3376 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
3377 /* enable clockgating to save power */
3378 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(&adev->ip_blocks[i],
3379 state);
3380 if (r) {
3381 dev_err(adev->dev,
3382 "set_clockgating_state(gate) of IP block <%s> failed %d\n",
3383 adev->ip_blocks[i].version->funcs->name,
3384 r);
3385 return r;
3386 }
3387 }
3388 }
3389
3390 return 0;
3391 }
3392
amdgpu_device_set_pg_state(struct amdgpu_device * adev,enum amd_powergating_state state)3393 int amdgpu_device_set_pg_state(struct amdgpu_device *adev,
3394 enum amd_powergating_state state)
3395 {
3396 int i, j, r;
3397
3398 if (amdgpu_emu_mode == 1)
3399 return 0;
3400
3401 for (j = 0; j < adev->num_ip_blocks; j++) {
3402 i = state == AMD_PG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
3403 if (!adev->ip_blocks[i].status.late_initialized)
3404 continue;
3405 /* skip PG for GFX, SDMA on S0ix */
3406 if (adev->in_s0ix &&
3407 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX ||
3408 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SDMA))
3409 continue;
3410 /* skip CG for VCE/UVD, it's handled specially */
3411 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
3412 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
3413 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
3414 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG &&
3415 adev->ip_blocks[i].version->funcs->set_powergating_state) {
3416 /* enable powergating to save power */
3417 r = adev->ip_blocks[i].version->funcs->set_powergating_state(&adev->ip_blocks[i],
3418 state);
3419 if (r) {
3420 dev_err(adev->dev,
3421 "set_powergating_state(gate) of IP block <%s> failed %d\n",
3422 adev->ip_blocks[i].version->funcs->name,
3423 r);
3424 return r;
3425 }
3426 }
3427 }
3428 return 0;
3429 }
3430
amdgpu_device_enable_mgpu_fan_boost(void)3431 static int amdgpu_device_enable_mgpu_fan_boost(void)
3432 {
3433 struct amdgpu_gpu_instance *gpu_ins;
3434 struct amdgpu_device *adev;
3435 int i, ret = 0;
3436
3437 mutex_lock(&mgpu_info.mutex);
3438
3439 /*
3440 * MGPU fan boost feature should be enabled
3441 * only when there are two or more dGPUs in
3442 * the system
3443 */
3444 if (mgpu_info.num_dgpu < 2)
3445 goto out;
3446
3447 for (i = 0; i < mgpu_info.num_dgpu; i++) {
3448 gpu_ins = &(mgpu_info.gpu_ins[i]);
3449 adev = gpu_ins->adev;
3450 if (!(adev->flags & AMD_IS_APU || amdgpu_sriov_multi_vf_mode(adev)) &&
3451 !gpu_ins->mgpu_fan_enabled) {
3452 ret = amdgpu_dpm_enable_mgpu_fan_boost(adev);
3453 if (ret)
3454 break;
3455
3456 gpu_ins->mgpu_fan_enabled = 1;
3457 }
3458 }
3459
3460 out:
3461 mutex_unlock(&mgpu_info.mutex);
3462
3463 return ret;
3464 }
3465
3466 /**
3467 * amdgpu_device_ip_late_init - run late init for hardware IPs
3468 *
3469 * @adev: amdgpu_device pointer
3470 *
3471 * Late initialization pass for hardware IPs. The list of all the hardware
3472 * IPs that make up the asic is walked and the late_init callbacks are run.
3473 * late_init covers any special initialization that an IP requires
3474 * after all of the have been initialized or something that needs to happen
3475 * late in the init process.
3476 * Returns 0 on success, negative error code on failure.
3477 */
amdgpu_device_ip_late_init(struct amdgpu_device * adev)3478 static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
3479 {
3480 struct amdgpu_gpu_instance *gpu_instance;
3481 int i = 0, r;
3482
3483 for (i = 0; i < adev->num_ip_blocks; i++) {
3484 if (!adev->ip_blocks[i].status.hw)
3485 continue;
3486 if (adev->ip_blocks[i].version->funcs->late_init) {
3487 r = adev->ip_blocks[i].version->funcs->late_init(&adev->ip_blocks[i]);
3488 if (r) {
3489 dev_err(adev->dev,
3490 "late_init of IP block <%s> failed %d\n",
3491 adev->ip_blocks[i].version->funcs->name,
3492 r);
3493 return r;
3494 }
3495 }
3496 adev->ip_blocks[i].status.late_initialized = true;
3497 }
3498
3499 r = amdgpu_ras_late_init(adev);
3500 if (r) {
3501 dev_err(adev->dev, "amdgpu_ras_late_init failed %d", r);
3502 return r;
3503 }
3504
3505 if (!amdgpu_reset_in_recovery(adev))
3506 amdgpu_ras_set_error_query_ready(adev, true);
3507
3508 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_GATE);
3509 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_GATE);
3510
3511 amdgpu_device_fill_reset_magic(adev);
3512
3513 r = amdgpu_device_enable_mgpu_fan_boost();
3514 if (r)
3515 dev_err(adev->dev, "enable mgpu fan boost failed (%d).\n", r);
3516
3517 /* For passthrough configuration on arcturus and aldebaran, enable special handling SBR */
3518 if (amdgpu_passthrough(adev) &&
3519 ((adev->asic_type == CHIP_ARCTURUS && adev->gmc.xgmi.num_physical_nodes > 1) ||
3520 adev->asic_type == CHIP_ALDEBARAN))
3521 amdgpu_dpm_handle_passthrough_sbr(adev, true);
3522
3523 if (adev->gmc.xgmi.num_physical_nodes > 1) {
3524 mutex_lock(&mgpu_info.mutex);
3525
3526 /*
3527 * Reset device p-state to low as this was booted with high.
3528 *
3529 * This should be performed only after all devices from the same
3530 * hive get initialized.
3531 *
3532 * However, it's unknown how many device in the hive in advance.
3533 * As this is counted one by one during devices initializations.
3534 *
3535 * So, we wait for all XGMI interlinked devices initialized.
3536 * This may bring some delays as those devices may come from
3537 * different hives. But that should be OK.
3538 */
3539 if (mgpu_info.num_dgpu == adev->gmc.xgmi.num_physical_nodes) {
3540 for (i = 0; i < mgpu_info.num_gpu; i++) {
3541 gpu_instance = &(mgpu_info.gpu_ins[i]);
3542 if (gpu_instance->adev->flags & AMD_IS_APU)
3543 continue;
3544
3545 r = amdgpu_xgmi_set_pstate(gpu_instance->adev,
3546 AMDGPU_XGMI_PSTATE_MIN);
3547 if (r) {
3548 dev_err(adev->dev,
3549 "pstate setting failed (%d).\n",
3550 r);
3551 break;
3552 }
3553 }
3554 }
3555
3556 mutex_unlock(&mgpu_info.mutex);
3557 }
3558
3559 return 0;
3560 }
3561
amdgpu_ip_block_hw_fini(struct amdgpu_ip_block * ip_block)3562 static void amdgpu_ip_block_hw_fini(struct amdgpu_ip_block *ip_block)
3563 {
3564 struct amdgpu_device *adev = ip_block->adev;
3565 int r;
3566
3567 if (!ip_block->version->funcs->hw_fini) {
3568 dev_err(adev->dev, "hw_fini of IP block <%s> not defined\n",
3569 ip_block->version->funcs->name);
3570 } else {
3571 r = ip_block->version->funcs->hw_fini(ip_block);
3572 /* XXX handle errors */
3573 if (r) {
3574 dev_dbg(adev->dev,
3575 "hw_fini of IP block <%s> failed %d\n",
3576 ip_block->version->funcs->name, r);
3577 }
3578 }
3579
3580 ip_block->status.hw = false;
3581 }
3582
3583 /**
3584 * amdgpu_device_smu_fini_early - smu hw_fini wrapper
3585 *
3586 * @adev: amdgpu_device pointer
3587 *
3588 * For ASICs need to disable SMC first
3589 */
amdgpu_device_smu_fini_early(struct amdgpu_device * adev)3590 static void amdgpu_device_smu_fini_early(struct amdgpu_device *adev)
3591 {
3592 int i;
3593
3594 if (amdgpu_ip_version(adev, GC_HWIP, 0) > IP_VERSION(9, 0, 0))
3595 return;
3596
3597 for (i = 0; i < adev->num_ip_blocks; i++) {
3598 if (!adev->ip_blocks[i].status.hw)
3599 continue;
3600 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
3601 amdgpu_ip_block_hw_fini(&adev->ip_blocks[i]);
3602 break;
3603 }
3604 }
3605 }
3606
amdgpu_device_ip_fini_early(struct amdgpu_device * adev)3607 static int amdgpu_device_ip_fini_early(struct amdgpu_device *adev)
3608 {
3609 int i, r;
3610
3611 for (i = 0; i < adev->num_ip_blocks; i++) {
3612 if (!adev->ip_blocks[i].version->funcs->early_fini)
3613 continue;
3614
3615 r = adev->ip_blocks[i].version->funcs->early_fini(&adev->ip_blocks[i]);
3616 if (r) {
3617 dev_dbg(adev->dev,
3618 "early_fini of IP block <%s> failed %d\n",
3619 adev->ip_blocks[i].version->funcs->name, r);
3620 }
3621 }
3622
3623 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
3624 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
3625
3626 amdgpu_amdkfd_suspend(adev, true);
3627 amdgpu_userq_suspend(adev);
3628
3629 /* Workaround for ASICs need to disable SMC first */
3630 amdgpu_device_smu_fini_early(adev);
3631
3632 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
3633 if (!adev->ip_blocks[i].status.hw)
3634 continue;
3635
3636 amdgpu_ip_block_hw_fini(&adev->ip_blocks[i]);
3637 }
3638
3639 if (amdgpu_sriov_vf(adev)) {
3640 if (amdgpu_virt_release_full_gpu(adev, false))
3641 dev_err(adev->dev,
3642 "failed to release exclusive mode on fini\n");
3643 }
3644
3645 return 0;
3646 }
3647
3648 /**
3649 * amdgpu_device_ip_fini - run fini for hardware IPs
3650 *
3651 * @adev: amdgpu_device pointer
3652 *
3653 * Main teardown pass for hardware IPs. The list of all the hardware
3654 * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
3655 * are run. hw_fini tears down the hardware associated with each IP
3656 * and sw_fini tears down any software state associated with each IP.
3657 * Returns 0 on success, negative error code on failure.
3658 */
amdgpu_device_ip_fini(struct amdgpu_device * adev)3659 static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
3660 {
3661 int i, r;
3662
3663 amdgpu_cper_fini(adev);
3664
3665 if (amdgpu_sriov_vf(adev) && adev->virt.ras_init_done)
3666 amdgpu_virt_release_ras_err_handler_data(adev);
3667
3668 if (adev->gmc.xgmi.num_physical_nodes > 1)
3669 amdgpu_xgmi_remove_device(adev);
3670
3671 amdgpu_amdkfd_device_fini_sw(adev);
3672
3673 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
3674 if (!adev->ip_blocks[i].status.sw)
3675 continue;
3676
3677 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
3678 amdgpu_ucode_free_bo(adev);
3679 amdgpu_free_static_csa(&adev->virt.csa_obj);
3680 amdgpu_device_wb_fini(adev);
3681 amdgpu_device_mem_scratch_fini(adev);
3682 amdgpu_ib_pool_fini(adev);
3683 amdgpu_seq64_fini(adev);
3684 amdgpu_doorbell_fini(adev);
3685 }
3686 if (adev->ip_blocks[i].version->funcs->sw_fini) {
3687 r = adev->ip_blocks[i].version->funcs->sw_fini(&adev->ip_blocks[i]);
3688 /* XXX handle errors */
3689 if (r) {
3690 dev_dbg(adev->dev,
3691 "sw_fini of IP block <%s> failed %d\n",
3692 adev->ip_blocks[i].version->funcs->name,
3693 r);
3694 }
3695 }
3696 adev->ip_blocks[i].status.sw = false;
3697 adev->ip_blocks[i].status.valid = false;
3698 }
3699
3700 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
3701 if (!adev->ip_blocks[i].status.late_initialized)
3702 continue;
3703 if (adev->ip_blocks[i].version->funcs->late_fini)
3704 adev->ip_blocks[i].version->funcs->late_fini(&adev->ip_blocks[i]);
3705 adev->ip_blocks[i].status.late_initialized = false;
3706 }
3707
3708 amdgpu_ras_fini(adev);
3709 amdgpu_uid_fini(adev);
3710
3711 return 0;
3712 }
3713
3714 /**
3715 * amdgpu_device_delayed_init_work_handler - work handler for IB tests
3716 *
3717 * @work: work_struct.
3718 */
amdgpu_device_delayed_init_work_handler(struct work_struct * work)3719 static void amdgpu_device_delayed_init_work_handler(struct work_struct *work)
3720 {
3721 struct amdgpu_device *adev =
3722 container_of(work, struct amdgpu_device, delayed_init_work.work);
3723 int r;
3724
3725 r = amdgpu_ib_ring_tests(adev);
3726 if (r)
3727 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
3728 }
3729
amdgpu_device_delay_enable_gfx_off(struct work_struct * work)3730 static void amdgpu_device_delay_enable_gfx_off(struct work_struct *work)
3731 {
3732 struct amdgpu_device *adev =
3733 container_of(work, struct amdgpu_device, gfx.gfx_off_delay_work.work);
3734
3735 WARN_ON_ONCE(adev->gfx.gfx_off_state);
3736 WARN_ON_ONCE(adev->gfx.gfx_off_req_count);
3737
3738 if (!amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, true, 0))
3739 adev->gfx.gfx_off_state = true;
3740 }
3741
3742 /**
3743 * amdgpu_device_ip_suspend_phase1 - run suspend for hardware IPs (phase 1)
3744 *
3745 * @adev: amdgpu_device pointer
3746 *
3747 * Main suspend function for hardware IPs. The list of all the hardware
3748 * IPs that make up the asic is walked, clockgating is disabled and the
3749 * suspend callbacks are run. suspend puts the hardware and software state
3750 * in each IP into a state suitable for suspend.
3751 * Returns 0 on success, negative error code on failure.
3752 */
amdgpu_device_ip_suspend_phase1(struct amdgpu_device * adev)3753 static int amdgpu_device_ip_suspend_phase1(struct amdgpu_device *adev)
3754 {
3755 int i, r;
3756
3757 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
3758 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
3759
3760 /*
3761 * Per PMFW team's suggestion, driver needs to handle gfxoff
3762 * and df cstate features disablement for gpu reset(e.g. Mode1Reset)
3763 * scenario. Add the missing df cstate disablement here.
3764 */
3765 if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_DISALLOW))
3766 dev_warn(adev->dev, "Failed to disallow df cstate");
3767
3768 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
3769 if (!adev->ip_blocks[i].status.valid)
3770 continue;
3771
3772 /* displays are handled separately */
3773 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_DCE)
3774 continue;
3775
3776 /* XXX handle errors */
3777 r = amdgpu_ip_block_suspend(&adev->ip_blocks[i]);
3778 if (r)
3779 return r;
3780 }
3781
3782 return 0;
3783 }
3784
3785 /**
3786 * amdgpu_device_ip_suspend_phase2 - run suspend for hardware IPs (phase 2)
3787 *
3788 * @adev: amdgpu_device pointer
3789 *
3790 * Main suspend function for hardware IPs. The list of all the hardware
3791 * IPs that make up the asic is walked, clockgating is disabled and the
3792 * suspend callbacks are run. suspend puts the hardware and software state
3793 * in each IP into a state suitable for suspend.
3794 * Returns 0 on success, negative error code on failure.
3795 */
amdgpu_device_ip_suspend_phase2(struct amdgpu_device * adev)3796 static int amdgpu_device_ip_suspend_phase2(struct amdgpu_device *adev)
3797 {
3798 int i, r;
3799
3800 if (adev->in_s0ix)
3801 amdgpu_dpm_gfx_state_change(adev, sGpuChangeState_D3Entry);
3802
3803 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
3804 if (!adev->ip_blocks[i].status.valid)
3805 continue;
3806 /* displays are handled in phase1 */
3807 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)
3808 continue;
3809 /* PSP lost connection when err_event_athub occurs */
3810 if (amdgpu_ras_intr_triggered() &&
3811 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
3812 adev->ip_blocks[i].status.hw = false;
3813 continue;
3814 }
3815
3816 /* skip unnecessary suspend if we do not initialize them yet */
3817 if (!amdgpu_ip_member_of_hwini(
3818 adev, adev->ip_blocks[i].version->type))
3819 continue;
3820
3821 /* Since we skip suspend for S0i3, we need to cancel the delayed
3822 * idle work here as the suspend callback never gets called.
3823 */
3824 if (adev->in_s0ix &&
3825 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX &&
3826 amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(10, 0, 0))
3827 cancel_delayed_work_sync(&adev->gfx.idle_work);
3828 /* skip suspend of gfx/mes and psp for S0ix
3829 * gfx is in gfxoff state, so on resume it will exit gfxoff just
3830 * like at runtime. PSP is also part of the always on hardware
3831 * so no need to suspend it.
3832 */
3833 if (adev->in_s0ix &&
3834 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP ||
3835 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX ||
3836 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_MES))
3837 continue;
3838
3839 /* SDMA 5.x+ is part of GFX power domain so it's covered by GFXOFF */
3840 if (adev->in_s0ix &&
3841 (amdgpu_ip_version(adev, SDMA0_HWIP, 0) >=
3842 IP_VERSION(5, 0, 0)) &&
3843 (adev->ip_blocks[i].version->type ==
3844 AMD_IP_BLOCK_TYPE_SDMA))
3845 continue;
3846
3847 /* Once swPSP provides the IMU, RLC FW binaries to TOS during cold-boot.
3848 * These are in TMR, hence are expected to be reused by PSP-TOS to reload
3849 * from this location and RLC Autoload automatically also gets loaded
3850 * from here based on PMFW -> PSP message during re-init sequence.
3851 * Therefore, the psp suspend & resume should be skipped to avoid destroy
3852 * the TMR and reload FWs again for IMU enabled APU ASICs.
3853 */
3854 if (amdgpu_in_reset(adev) &&
3855 (adev->flags & AMD_IS_APU) && adev->gfx.imu.funcs &&
3856 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)
3857 continue;
3858
3859 /* XXX handle errors */
3860 r = amdgpu_ip_block_suspend(&adev->ip_blocks[i]);
3861 adev->ip_blocks[i].status.hw = false;
3862
3863 /* handle putting the SMC in the appropriate state */
3864 if (!amdgpu_sriov_vf(adev)) {
3865 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
3866 r = amdgpu_dpm_set_mp1_state(adev, adev->mp1_state);
3867 if (r) {
3868 dev_err(adev->dev,
3869 "SMC failed to set mp1 state %d, %d\n",
3870 adev->mp1_state, r);
3871 return r;
3872 }
3873 }
3874 }
3875 }
3876
3877 return 0;
3878 }
3879
3880 /**
3881 * amdgpu_device_ip_suspend - run suspend for hardware IPs
3882 *
3883 * @adev: amdgpu_device pointer
3884 *
3885 * Main suspend function for hardware IPs. The list of all the hardware
3886 * IPs that make up the asic is walked, clockgating is disabled and the
3887 * suspend callbacks are run. suspend puts the hardware and software state
3888 * in each IP into a state suitable for suspend.
3889 * Returns 0 on success, negative error code on failure.
3890 */
amdgpu_device_ip_suspend(struct amdgpu_device * adev)3891 int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
3892 {
3893 int r;
3894
3895 if (amdgpu_sriov_vf(adev)) {
3896 amdgpu_virt_fini_data_exchange(adev);
3897 amdgpu_virt_request_full_gpu(adev, false);
3898 }
3899
3900 amdgpu_ttm_set_buffer_funcs_status(adev, false);
3901
3902 r = amdgpu_device_ip_suspend_phase1(adev);
3903 if (r)
3904 return r;
3905 r = amdgpu_device_ip_suspend_phase2(adev);
3906
3907 if (amdgpu_sriov_vf(adev))
3908 amdgpu_virt_release_full_gpu(adev, false);
3909
3910 return r;
3911 }
3912
amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device * adev)3913 static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
3914 {
3915 int i, r;
3916
3917 static enum amd_ip_block_type ip_order[] = {
3918 AMD_IP_BLOCK_TYPE_COMMON,
3919 AMD_IP_BLOCK_TYPE_GMC,
3920 AMD_IP_BLOCK_TYPE_PSP,
3921 AMD_IP_BLOCK_TYPE_IH,
3922 };
3923
3924 for (i = 0; i < adev->num_ip_blocks; i++) {
3925 int j;
3926 struct amdgpu_ip_block *block;
3927
3928 block = &adev->ip_blocks[i];
3929 block->status.hw = false;
3930
3931 for (j = 0; j < ARRAY_SIZE(ip_order); j++) {
3932
3933 if (block->version->type != ip_order[j] ||
3934 !block->status.valid)
3935 continue;
3936
3937 r = block->version->funcs->hw_init(&adev->ip_blocks[i]);
3938 if (r) {
3939 dev_err(adev->dev, "RE-INIT-early: %s failed\n",
3940 block->version->funcs->name);
3941 return r;
3942 }
3943 block->status.hw = true;
3944 }
3945 }
3946
3947 return 0;
3948 }
3949
amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device * adev)3950 static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
3951 {
3952 struct amdgpu_ip_block *block;
3953 int i, r = 0;
3954
3955 static enum amd_ip_block_type ip_order[] = {
3956 AMD_IP_BLOCK_TYPE_SMC,
3957 AMD_IP_BLOCK_TYPE_DCE,
3958 AMD_IP_BLOCK_TYPE_GFX,
3959 AMD_IP_BLOCK_TYPE_SDMA,
3960 AMD_IP_BLOCK_TYPE_MES,
3961 AMD_IP_BLOCK_TYPE_UVD,
3962 AMD_IP_BLOCK_TYPE_VCE,
3963 AMD_IP_BLOCK_TYPE_VCN,
3964 AMD_IP_BLOCK_TYPE_JPEG
3965 };
3966
3967 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
3968 block = amdgpu_device_ip_get_ip_block(adev, ip_order[i]);
3969
3970 if (!block)
3971 continue;
3972
3973 if (block->status.valid && !block->status.hw) {
3974 if (block->version->type == AMD_IP_BLOCK_TYPE_SMC) {
3975 r = amdgpu_ip_block_resume(block);
3976 } else {
3977 r = block->version->funcs->hw_init(block);
3978 }
3979
3980 if (r) {
3981 dev_err(adev->dev, "RE-INIT-late: %s failed\n",
3982 block->version->funcs->name);
3983 break;
3984 }
3985 block->status.hw = true;
3986 }
3987 }
3988
3989 return r;
3990 }
3991
3992 /**
3993 * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
3994 *
3995 * @adev: amdgpu_device pointer
3996 *
3997 * First resume function for hardware IPs. The list of all the hardware
3998 * IPs that make up the asic is walked and the resume callbacks are run for
3999 * COMMON, GMC, and IH. resume puts the hardware into a functional state
4000 * after a suspend and updates the software state as necessary. This
4001 * function is also used for restoring the GPU after a GPU reset.
4002 * Returns 0 on success, negative error code on failure.
4003 */
amdgpu_device_ip_resume_phase1(struct amdgpu_device * adev)4004 static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
4005 {
4006 int i, r;
4007
4008 for (i = 0; i < adev->num_ip_blocks; i++) {
4009 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
4010 continue;
4011 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
4012 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
4013 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
4014 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP && amdgpu_sriov_vf(adev))) {
4015
4016 r = amdgpu_ip_block_resume(&adev->ip_blocks[i]);
4017 if (r)
4018 return r;
4019 }
4020 }
4021
4022 return 0;
4023 }
4024
4025 /**
4026 * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
4027 *
4028 * @adev: amdgpu_device pointer
4029 *
4030 * Second resume function for hardware IPs. The list of all the hardware
4031 * IPs that make up the asic is walked and the resume callbacks are run for
4032 * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
4033 * functional state after a suspend and updates the software state as
4034 * necessary. This function is also used for restoring the GPU after a GPU
4035 * reset.
4036 * Returns 0 on success, negative error code on failure.
4037 */
amdgpu_device_ip_resume_phase2(struct amdgpu_device * adev)4038 static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
4039 {
4040 int i, r;
4041
4042 for (i = 0; i < adev->num_ip_blocks; i++) {
4043 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
4044 continue;
4045 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
4046 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
4047 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
4048 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE ||
4049 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)
4050 continue;
4051 r = amdgpu_ip_block_resume(&adev->ip_blocks[i]);
4052 if (r)
4053 return r;
4054 }
4055
4056 return 0;
4057 }
4058
4059 /**
4060 * amdgpu_device_ip_resume_phase3 - run resume for hardware IPs
4061 *
4062 * @adev: amdgpu_device pointer
4063 *
4064 * Third resume function for hardware IPs. The list of all the hardware
4065 * IPs that make up the asic is walked and the resume callbacks are run for
4066 * all DCE. resume puts the hardware into a functional state after a suspend
4067 * and updates the software state as necessary. This function is also used
4068 * for restoring the GPU after a GPU reset.
4069 *
4070 * Returns 0 on success, negative error code on failure.
4071 */
amdgpu_device_ip_resume_phase3(struct amdgpu_device * adev)4072 static int amdgpu_device_ip_resume_phase3(struct amdgpu_device *adev)
4073 {
4074 int i, r;
4075
4076 for (i = 0; i < adev->num_ip_blocks; i++) {
4077 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
4078 continue;
4079 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) {
4080 r = amdgpu_ip_block_resume(&adev->ip_blocks[i]);
4081 if (r)
4082 return r;
4083 }
4084 }
4085
4086 return 0;
4087 }
4088
4089 /**
4090 * amdgpu_device_ip_resume - run resume for hardware IPs
4091 *
4092 * @adev: amdgpu_device pointer
4093 *
4094 * Main resume function for hardware IPs. The hardware IPs
4095 * are split into two resume functions because they are
4096 * also used in recovering from a GPU reset and some additional
4097 * steps need to be take between them. In this case (S3/S4) they are
4098 * run sequentially.
4099 * Returns 0 on success, negative error code on failure.
4100 */
amdgpu_device_ip_resume(struct amdgpu_device * adev)4101 static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
4102 {
4103 int r;
4104
4105 r = amdgpu_device_ip_resume_phase1(adev);
4106 if (r)
4107 return r;
4108
4109 r = amdgpu_device_fw_loading(adev);
4110 if (r)
4111 return r;
4112
4113 r = amdgpu_device_ip_resume_phase2(adev);
4114
4115 if (adev->mman.buffer_funcs_ring->sched.ready)
4116 amdgpu_ttm_set_buffer_funcs_status(adev, true);
4117
4118 if (r)
4119 return r;
4120
4121 amdgpu_fence_driver_hw_init(adev);
4122
4123 r = amdgpu_device_ip_resume_phase3(adev);
4124
4125 return r;
4126 }
4127
4128 /**
4129 * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
4130 *
4131 * @adev: amdgpu_device pointer
4132 *
4133 * Query the VBIOS data tables to determine if the board supports SR-IOV.
4134 */
amdgpu_device_detect_sriov_bios(struct amdgpu_device * adev)4135 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
4136 {
4137 if (amdgpu_sriov_vf(adev)) {
4138 if (adev->is_atom_fw) {
4139 if (amdgpu_atomfirmware_gpu_virtualization_supported(adev))
4140 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
4141 } else {
4142 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
4143 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
4144 }
4145
4146 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
4147 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
4148 }
4149 }
4150
4151 /**
4152 * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
4153 *
4154 * @pdev : pci device context
4155 * @asic_type: AMD asic type
4156 *
4157 * Check if there is DC (new modesetting infrastructre) support for an asic.
4158 * returns true if DC has support, false if not.
4159 */
amdgpu_device_asic_has_dc_support(struct pci_dev * pdev,enum amd_asic_type asic_type)4160 bool amdgpu_device_asic_has_dc_support(struct pci_dev *pdev,
4161 enum amd_asic_type asic_type)
4162 {
4163 switch (asic_type) {
4164 #ifdef CONFIG_DRM_AMDGPU_SI
4165 case CHIP_HAINAN:
4166 #endif
4167 case CHIP_TOPAZ:
4168 /* chips with no display hardware */
4169 return false;
4170 #if defined(CONFIG_DRM_AMD_DC)
4171 case CHIP_TAHITI:
4172 case CHIP_PITCAIRN:
4173 case CHIP_VERDE:
4174 case CHIP_OLAND:
4175 /*
4176 * We have systems in the wild with these ASICs that require
4177 * LVDS and VGA support which is not supported with DC.
4178 *
4179 * Fallback to the non-DC driver here by default so as not to
4180 * cause regressions.
4181 */
4182 #if defined(CONFIG_DRM_AMD_DC_SI)
4183 return amdgpu_dc > 0;
4184 #else
4185 return false;
4186 #endif
4187 case CHIP_BONAIRE:
4188 case CHIP_KAVERI:
4189 case CHIP_KABINI:
4190 case CHIP_MULLINS:
4191 /*
4192 * We have systems in the wild with these ASICs that require
4193 * VGA support which is not supported with DC.
4194 *
4195 * Fallback to the non-DC driver here by default so as not to
4196 * cause regressions.
4197 */
4198 return amdgpu_dc > 0;
4199 default:
4200 return amdgpu_dc != 0;
4201 #else
4202 default:
4203 if (amdgpu_dc > 0)
4204 dev_info_once(
4205 &pdev->dev,
4206 "Display Core has been requested via kernel parameter but isn't supported by ASIC, ignoring\n");
4207 return false;
4208 #endif
4209 }
4210 }
4211
4212 /**
4213 * amdgpu_device_has_dc_support - check if dc is supported
4214 *
4215 * @adev: amdgpu_device pointer
4216 *
4217 * Returns true for supported, false for not supported
4218 */
amdgpu_device_has_dc_support(struct amdgpu_device * adev)4219 bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
4220 {
4221 if (adev->enable_virtual_display ||
4222 (adev->harvest_ip_mask & AMD_HARVEST_IP_DMU_MASK))
4223 return false;
4224
4225 return amdgpu_device_asic_has_dc_support(adev->pdev, adev->asic_type);
4226 }
4227
amdgpu_device_xgmi_reset_func(struct work_struct * __work)4228 static void amdgpu_device_xgmi_reset_func(struct work_struct *__work)
4229 {
4230 struct amdgpu_device *adev =
4231 container_of(__work, struct amdgpu_device, xgmi_reset_work);
4232 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
4233
4234 /* It's a bug to not have a hive within this function */
4235 if (WARN_ON(!hive))
4236 return;
4237
4238 /*
4239 * Use task barrier to synchronize all xgmi reset works across the
4240 * hive. task_barrier_enter and task_barrier_exit will block
4241 * until all the threads running the xgmi reset works reach
4242 * those points. task_barrier_full will do both blocks.
4243 */
4244 if (amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) {
4245
4246 task_barrier_enter(&hive->tb);
4247 adev->asic_reset_res = amdgpu_device_baco_enter(adev);
4248
4249 if (adev->asic_reset_res)
4250 goto fail;
4251
4252 task_barrier_exit(&hive->tb);
4253 adev->asic_reset_res = amdgpu_device_baco_exit(adev);
4254
4255 if (adev->asic_reset_res)
4256 goto fail;
4257
4258 amdgpu_ras_reset_error_count(adev, AMDGPU_RAS_BLOCK__MMHUB);
4259 } else {
4260
4261 task_barrier_full(&hive->tb);
4262 adev->asic_reset_res = amdgpu_asic_reset(adev);
4263 }
4264
4265 fail:
4266 if (adev->asic_reset_res)
4267 dev_warn(adev->dev,
4268 "ASIC reset failed with error, %d for drm dev, %s",
4269 adev->asic_reset_res, adev_to_drm(adev)->unique);
4270 amdgpu_put_xgmi_hive(hive);
4271 }
4272
amdgpu_device_get_job_timeout_settings(struct amdgpu_device * adev)4273 static int amdgpu_device_get_job_timeout_settings(struct amdgpu_device *adev)
4274 {
4275 char *input = amdgpu_lockup_timeout;
4276 char *timeout_setting = NULL;
4277 int index = 0;
4278 long timeout;
4279 int ret = 0;
4280
4281 /*
4282 * By default timeout for jobs is 10 sec
4283 */
4284 adev->compute_timeout = adev->gfx_timeout = msecs_to_jiffies(10000);
4285 adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout;
4286
4287 if (strnlen(input, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) {
4288 while ((timeout_setting = strsep(&input, ",")) &&
4289 strnlen(timeout_setting, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) {
4290 ret = kstrtol(timeout_setting, 0, &timeout);
4291 if (ret)
4292 return ret;
4293
4294 if (timeout == 0) {
4295 index++;
4296 continue;
4297 } else if (timeout < 0) {
4298 timeout = MAX_SCHEDULE_TIMEOUT;
4299 dev_warn(adev->dev, "lockup timeout disabled");
4300 add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
4301 } else {
4302 timeout = msecs_to_jiffies(timeout);
4303 }
4304
4305 switch (index++) {
4306 case 0:
4307 adev->gfx_timeout = timeout;
4308 break;
4309 case 1:
4310 adev->compute_timeout = timeout;
4311 break;
4312 case 2:
4313 adev->sdma_timeout = timeout;
4314 break;
4315 case 3:
4316 adev->video_timeout = timeout;
4317 break;
4318 default:
4319 break;
4320 }
4321 }
4322 /*
4323 * There is only one value specified and
4324 * it should apply to all non-compute jobs.
4325 */
4326 if (index == 1) {
4327 adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout;
4328 if (amdgpu_sriov_vf(adev) || amdgpu_passthrough(adev))
4329 adev->compute_timeout = adev->gfx_timeout;
4330 }
4331 }
4332
4333 return ret;
4334 }
4335
4336 /**
4337 * amdgpu_device_check_iommu_direct_map - check if RAM direct mapped to GPU
4338 *
4339 * @adev: amdgpu_device pointer
4340 *
4341 * RAM direct mapped to GPU if IOMMU is not enabled or is pass through mode
4342 */
amdgpu_device_check_iommu_direct_map(struct amdgpu_device * adev)4343 static void amdgpu_device_check_iommu_direct_map(struct amdgpu_device *adev)
4344 {
4345 struct iommu_domain *domain;
4346
4347 domain = iommu_get_domain_for_dev(adev->dev);
4348 if (!domain || domain->type == IOMMU_DOMAIN_IDENTITY)
4349 adev->ram_is_direct_mapped = true;
4350 }
4351
4352 #if defined(CONFIG_HSA_AMD_P2P)
4353 /**
4354 * amdgpu_device_check_iommu_remap - Check if DMA remapping is enabled.
4355 *
4356 * @adev: amdgpu_device pointer
4357 *
4358 * return if IOMMU remapping bar address
4359 */
amdgpu_device_check_iommu_remap(struct amdgpu_device * adev)4360 static bool amdgpu_device_check_iommu_remap(struct amdgpu_device *adev)
4361 {
4362 struct iommu_domain *domain;
4363
4364 domain = iommu_get_domain_for_dev(adev->dev);
4365 if (domain && (domain->type == IOMMU_DOMAIN_DMA ||
4366 domain->type == IOMMU_DOMAIN_DMA_FQ))
4367 return true;
4368
4369 return false;
4370 }
4371 #endif
4372
amdgpu_device_set_mcbp(struct amdgpu_device * adev)4373 static void amdgpu_device_set_mcbp(struct amdgpu_device *adev)
4374 {
4375 if (amdgpu_mcbp == 1)
4376 adev->gfx.mcbp = true;
4377 else if (amdgpu_mcbp == 0)
4378 adev->gfx.mcbp = false;
4379
4380 if (amdgpu_sriov_vf(adev))
4381 adev->gfx.mcbp = true;
4382
4383 if (adev->gfx.mcbp)
4384 dev_info(adev->dev, "MCBP is enabled\n");
4385 }
4386
4387 /**
4388 * amdgpu_device_init - initialize the driver
4389 *
4390 * @adev: amdgpu_device pointer
4391 * @flags: driver flags
4392 *
4393 * Initializes the driver info and hw (all asics).
4394 * Returns 0 for success or an error on failure.
4395 * Called at driver startup.
4396 */
amdgpu_device_init(struct amdgpu_device * adev,uint32_t flags)4397 int amdgpu_device_init(struct amdgpu_device *adev,
4398 uint32_t flags)
4399 {
4400 struct pci_dev *pdev = adev->pdev;
4401 int r, i;
4402 bool px = false;
4403 u32 max_MBps;
4404 int tmp;
4405
4406 adev->shutdown = false;
4407 adev->flags = flags;
4408
4409 if (amdgpu_force_asic_type >= 0 && amdgpu_force_asic_type < CHIP_LAST)
4410 adev->asic_type = amdgpu_force_asic_type;
4411 else
4412 adev->asic_type = flags & AMD_ASIC_MASK;
4413
4414 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
4415 if (amdgpu_emu_mode == 1)
4416 adev->usec_timeout *= 10;
4417 adev->gmc.gart_size = 512 * 1024 * 1024;
4418 adev->accel_working = false;
4419 adev->num_rings = 0;
4420 RCU_INIT_POINTER(adev->gang_submit, dma_fence_get_stub());
4421 adev->mman.buffer_funcs = NULL;
4422 adev->mman.buffer_funcs_ring = NULL;
4423 adev->vm_manager.vm_pte_funcs = NULL;
4424 adev->vm_manager.vm_pte_num_scheds = 0;
4425 adev->gmc.gmc_funcs = NULL;
4426 adev->harvest_ip_mask = 0x0;
4427 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
4428 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
4429
4430 adev->smc_rreg = &amdgpu_invalid_rreg;
4431 adev->smc_wreg = &amdgpu_invalid_wreg;
4432 adev->pcie_rreg = &amdgpu_invalid_rreg;
4433 adev->pcie_wreg = &amdgpu_invalid_wreg;
4434 adev->pcie_rreg_ext = &amdgpu_invalid_rreg_ext;
4435 adev->pcie_wreg_ext = &amdgpu_invalid_wreg_ext;
4436 adev->pciep_rreg = &amdgpu_invalid_rreg;
4437 adev->pciep_wreg = &amdgpu_invalid_wreg;
4438 adev->pcie_rreg64 = &amdgpu_invalid_rreg64;
4439 adev->pcie_wreg64 = &amdgpu_invalid_wreg64;
4440 adev->pcie_rreg64_ext = &amdgpu_invalid_rreg64_ext;
4441 adev->pcie_wreg64_ext = &amdgpu_invalid_wreg64_ext;
4442 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
4443 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
4444 adev->didt_rreg = &amdgpu_invalid_rreg;
4445 adev->didt_wreg = &amdgpu_invalid_wreg;
4446 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
4447 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
4448 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
4449 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
4450
4451 dev_info(
4452 adev->dev,
4453 "initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
4454 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
4455 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
4456
4457 /* mutex initialization are all done here so we
4458 * can recall function without having locking issues
4459 */
4460 mutex_init(&adev->firmware.mutex);
4461 mutex_init(&adev->pm.mutex);
4462 mutex_init(&adev->gfx.gpu_clock_mutex);
4463 mutex_init(&adev->srbm_mutex);
4464 mutex_init(&adev->gfx.pipe_reserve_mutex);
4465 mutex_init(&adev->gfx.gfx_off_mutex);
4466 mutex_init(&adev->gfx.partition_mutex);
4467 mutex_init(&adev->grbm_idx_mutex);
4468 mutex_init(&adev->mn_lock);
4469 mutex_init(&adev->virt.vf_errors.lock);
4470 hash_init(adev->mn_hash);
4471 mutex_init(&adev->psp.mutex);
4472 mutex_init(&adev->notifier_lock);
4473 mutex_init(&adev->pm.stable_pstate_ctx_lock);
4474 mutex_init(&adev->benchmark_mutex);
4475 mutex_init(&adev->gfx.reset_sem_mutex);
4476 /* Initialize the mutex for cleaner shader isolation between GFX and compute processes */
4477 mutex_init(&adev->enforce_isolation_mutex);
4478 for (i = 0; i < MAX_XCP; ++i) {
4479 adev->isolation[i].spearhead = dma_fence_get_stub();
4480 amdgpu_sync_create(&adev->isolation[i].active);
4481 amdgpu_sync_create(&adev->isolation[i].prev);
4482 }
4483 mutex_init(&adev->gfx.userq_sch_mutex);
4484 mutex_init(&adev->gfx.workload_profile_mutex);
4485 mutex_init(&adev->vcn.workload_profile_mutex);
4486 mutex_init(&adev->userq_mutex);
4487
4488 amdgpu_device_init_apu_flags(adev);
4489
4490 r = amdgpu_device_check_arguments(adev);
4491 if (r)
4492 return r;
4493
4494 spin_lock_init(&adev->mmio_idx_lock);
4495 spin_lock_init(&adev->smc_idx_lock);
4496 spin_lock_init(&adev->pcie_idx_lock);
4497 spin_lock_init(&adev->uvd_ctx_idx_lock);
4498 spin_lock_init(&adev->didt_idx_lock);
4499 spin_lock_init(&adev->gc_cac_idx_lock);
4500 spin_lock_init(&adev->se_cac_idx_lock);
4501 spin_lock_init(&adev->audio_endpt_idx_lock);
4502 spin_lock_init(&adev->mm_stats.lock);
4503 spin_lock_init(&adev->virt.rlcg_reg_lock);
4504 spin_lock_init(&adev->wb.lock);
4505
4506 xa_init_flags(&adev->userq_xa, XA_FLAGS_LOCK_IRQ);
4507
4508 INIT_LIST_HEAD(&adev->reset_list);
4509
4510 INIT_LIST_HEAD(&adev->ras_list);
4511
4512 INIT_LIST_HEAD(&adev->pm.od_kobj_list);
4513
4514 INIT_LIST_HEAD(&adev->userq_mgr_list);
4515
4516 INIT_DELAYED_WORK(&adev->delayed_init_work,
4517 amdgpu_device_delayed_init_work_handler);
4518 INIT_DELAYED_WORK(&adev->gfx.gfx_off_delay_work,
4519 amdgpu_device_delay_enable_gfx_off);
4520 /*
4521 * Initialize the enforce_isolation work structures for each XCP
4522 * partition. This work handler is responsible for enforcing shader
4523 * isolation on AMD GPUs. It counts the number of emitted fences for
4524 * each GFX and compute ring. If there are any fences, it schedules
4525 * the `enforce_isolation_work` to be run after a delay. If there are
4526 * no fences, it signals the Kernel Fusion Driver (KFD) to resume the
4527 * runqueue.
4528 */
4529 for (i = 0; i < MAX_XCP; i++) {
4530 INIT_DELAYED_WORK(&adev->gfx.enforce_isolation[i].work,
4531 amdgpu_gfx_enforce_isolation_handler);
4532 adev->gfx.enforce_isolation[i].adev = adev;
4533 adev->gfx.enforce_isolation[i].xcp_id = i;
4534 }
4535
4536 INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
4537
4538 adev->gfx.gfx_off_req_count = 1;
4539 adev->gfx.gfx_off_residency = 0;
4540 adev->gfx.gfx_off_entrycount = 0;
4541 adev->pm.ac_power = power_supply_is_system_supplied() > 0;
4542
4543 atomic_set(&adev->throttling_logging_enabled, 1);
4544 /*
4545 * If throttling continues, logging will be performed every minute
4546 * to avoid log flooding. "-1" is subtracted since the thermal
4547 * throttling interrupt comes every second. Thus, the total logging
4548 * interval is 59 seconds(retelimited printk interval) + 1(waiting
4549 * for throttling interrupt) = 60 seconds.
4550 */
4551 ratelimit_state_init(&adev->throttling_logging_rs, (60 - 1) * HZ, 1);
4552
4553 ratelimit_set_flags(&adev->throttling_logging_rs, RATELIMIT_MSG_ON_RELEASE);
4554
4555 /* Registers mapping */
4556 /* TODO: block userspace mapping of io register */
4557 if (adev->asic_type >= CHIP_BONAIRE) {
4558 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
4559 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
4560 } else {
4561 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
4562 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
4563 }
4564
4565 for (i = 0; i < AMD_IP_BLOCK_TYPE_NUM; i++)
4566 atomic_set(&adev->pm.pwr_state[i], POWER_STATE_UNKNOWN);
4567
4568 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
4569 if (!adev->rmmio)
4570 return -ENOMEM;
4571
4572 dev_info(adev->dev, "register mmio base: 0x%08X\n",
4573 (uint32_t)adev->rmmio_base);
4574 dev_info(adev->dev, "register mmio size: %u\n",
4575 (unsigned int)adev->rmmio_size);
4576
4577 /*
4578 * Reset domain needs to be present early, before XGMI hive discovered
4579 * (if any) and initialized to use reset sem and in_gpu reset flag
4580 * early on during init and before calling to RREG32.
4581 */
4582 adev->reset_domain = amdgpu_reset_create_reset_domain(SINGLE_DEVICE, "amdgpu-reset-dev");
4583 if (!adev->reset_domain)
4584 return -ENOMEM;
4585
4586 /* detect hw virtualization here */
4587 amdgpu_virt_init(adev);
4588
4589 amdgpu_device_get_pcie_info(adev);
4590
4591 r = amdgpu_device_get_job_timeout_settings(adev);
4592 if (r) {
4593 dev_err(adev->dev, "invalid lockup_timeout parameter syntax\n");
4594 return r;
4595 }
4596
4597 amdgpu_device_set_mcbp(adev);
4598
4599 /*
4600 * By default, use default mode where all blocks are expected to be
4601 * initialized. At present a 'swinit' of blocks is required to be
4602 * completed before the need for a different level is detected.
4603 */
4604 amdgpu_set_init_level(adev, AMDGPU_INIT_LEVEL_DEFAULT);
4605 /* early init functions */
4606 r = amdgpu_device_ip_early_init(adev);
4607 if (r)
4608 return r;
4609
4610 /*
4611 * No need to remove conflicting FBs for non-display class devices.
4612 * This prevents the sysfb from being freed accidently.
4613 */
4614 if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA ||
4615 (pdev->class >> 8) == PCI_CLASS_DISPLAY_OTHER) {
4616 /* Get rid of things like offb */
4617 r = aperture_remove_conflicting_pci_devices(adev->pdev, amdgpu_kms_driver.name);
4618 if (r)
4619 return r;
4620 }
4621
4622 /* Enable TMZ based on IP_VERSION */
4623 amdgpu_gmc_tmz_set(adev);
4624
4625 if (amdgpu_sriov_vf(adev) &&
4626 amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(10, 3, 0))
4627 /* VF MMIO access (except mailbox range) from CPU
4628 * will be blocked during sriov runtime
4629 */
4630 adev->virt.caps |= AMDGPU_VF_MMIO_ACCESS_PROTECT;
4631
4632 amdgpu_gmc_noretry_set(adev);
4633 /* Need to get xgmi info early to decide the reset behavior*/
4634 if (adev->gmc.xgmi.supported) {
4635 r = adev->gfxhub.funcs->get_xgmi_info(adev);
4636 if (r)
4637 return r;
4638 }
4639
4640 /* enable PCIE atomic ops */
4641 if (amdgpu_sriov_vf(adev)) {
4642 if (adev->virt.fw_reserve.p_pf2vf)
4643 adev->have_atomics_support = ((struct amd_sriov_msg_pf2vf_info *)
4644 adev->virt.fw_reserve.p_pf2vf)->pcie_atomic_ops_support_flags ==
4645 (PCI_EXP_DEVCAP2_ATOMIC_COMP32 | PCI_EXP_DEVCAP2_ATOMIC_COMP64);
4646 /* APUs w/ gfx9 onwards doesn't reply on PCIe atomics, rather it is a
4647 * internal path natively support atomics, set have_atomics_support to true.
4648 */
4649 } else if ((adev->flags & AMD_IS_APU) &&
4650 (amdgpu_ip_version(adev, GC_HWIP, 0) >
4651 IP_VERSION(9, 0, 0))) {
4652 adev->have_atomics_support = true;
4653 } else {
4654 adev->have_atomics_support =
4655 !pci_enable_atomic_ops_to_root(adev->pdev,
4656 PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
4657 PCI_EXP_DEVCAP2_ATOMIC_COMP64);
4658 }
4659
4660 if (!adev->have_atomics_support)
4661 dev_info(adev->dev, "PCIE atomic ops is not supported\n");
4662
4663 /* doorbell bar mapping and doorbell index init*/
4664 amdgpu_doorbell_init(adev);
4665
4666 if (amdgpu_emu_mode == 1) {
4667 /* post the asic on emulation mode */
4668 emu_soc_asic_init(adev);
4669 goto fence_driver_init;
4670 }
4671
4672 amdgpu_reset_init(adev);
4673
4674 /* detect if we are with an SRIOV vbios */
4675 if (adev->bios)
4676 amdgpu_device_detect_sriov_bios(adev);
4677
4678 /* check if we need to reset the asic
4679 * E.g., driver was not cleanly unloaded previously, etc.
4680 */
4681 if (!amdgpu_sriov_vf(adev) && amdgpu_asic_need_reset_on_init(adev)) {
4682 if (adev->gmc.xgmi.num_physical_nodes) {
4683 dev_info(adev->dev, "Pending hive reset.\n");
4684 amdgpu_set_init_level(adev,
4685 AMDGPU_INIT_LEVEL_MINIMAL_XGMI);
4686 } else if (amdgpu_ip_version(adev, MP1_HWIP, 0) == IP_VERSION(13, 0, 10) &&
4687 !amdgpu_device_has_display_hardware(adev)) {
4688 r = psp_gpu_reset(adev);
4689 } else {
4690 tmp = amdgpu_reset_method;
4691 /* It should do a default reset when loading or reloading the driver,
4692 * regardless of the module parameter reset_method.
4693 */
4694 amdgpu_reset_method = AMD_RESET_METHOD_NONE;
4695 r = amdgpu_asic_reset(adev);
4696 amdgpu_reset_method = tmp;
4697 }
4698
4699 if (r) {
4700 dev_err(adev->dev, "asic reset on init failed\n");
4701 goto failed;
4702 }
4703 }
4704
4705 /* Post card if necessary */
4706 if (amdgpu_device_need_post(adev)) {
4707 if (!adev->bios) {
4708 dev_err(adev->dev, "no vBIOS found\n");
4709 r = -EINVAL;
4710 goto failed;
4711 }
4712 dev_info(adev->dev, "GPU posting now...\n");
4713 r = amdgpu_device_asic_init(adev);
4714 if (r) {
4715 dev_err(adev->dev, "gpu post error!\n");
4716 goto failed;
4717 }
4718 }
4719
4720 if (adev->bios) {
4721 if (adev->is_atom_fw) {
4722 /* Initialize clocks */
4723 r = amdgpu_atomfirmware_get_clock_info(adev);
4724 if (r) {
4725 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
4726 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
4727 goto failed;
4728 }
4729 } else {
4730 /* Initialize clocks */
4731 r = amdgpu_atombios_get_clock_info(adev);
4732 if (r) {
4733 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
4734 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
4735 goto failed;
4736 }
4737 /* init i2c buses */
4738 amdgpu_i2c_init(adev);
4739 }
4740 }
4741
4742 fence_driver_init:
4743 /* Fence driver */
4744 r = amdgpu_fence_driver_sw_init(adev);
4745 if (r) {
4746 dev_err(adev->dev, "amdgpu_fence_driver_sw_init failed\n");
4747 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
4748 goto failed;
4749 }
4750
4751 /* init the mode config */
4752 drm_mode_config_init(adev_to_drm(adev));
4753
4754 r = amdgpu_device_ip_init(adev);
4755 if (r) {
4756 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
4757 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
4758 goto release_ras_con;
4759 }
4760
4761 amdgpu_fence_driver_hw_init(adev);
4762
4763 dev_info(adev->dev,
4764 "SE %d, SH per SE %d, CU per SH %d, active_cu_number %d\n",
4765 adev->gfx.config.max_shader_engines,
4766 adev->gfx.config.max_sh_per_se,
4767 adev->gfx.config.max_cu_per_sh,
4768 adev->gfx.cu_info.number);
4769
4770 adev->accel_working = true;
4771
4772 amdgpu_vm_check_compute_bug(adev);
4773
4774 /* Initialize the buffer migration limit. */
4775 if (amdgpu_moverate >= 0)
4776 max_MBps = amdgpu_moverate;
4777 else
4778 max_MBps = 8; /* Allow 8 MB/s. */
4779 /* Get a log2 for easy divisions. */
4780 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
4781
4782 /*
4783 * Register gpu instance before amdgpu_device_enable_mgpu_fan_boost.
4784 * Otherwise the mgpu fan boost feature will be skipped due to the
4785 * gpu instance is counted less.
4786 */
4787 amdgpu_register_gpu_instance(adev);
4788
4789 /* enable clockgating, etc. after ib tests, etc. since some blocks require
4790 * explicit gating rather than handling it automatically.
4791 */
4792 if (adev->init_lvl->level != AMDGPU_INIT_LEVEL_MINIMAL_XGMI) {
4793 r = amdgpu_device_ip_late_init(adev);
4794 if (r) {
4795 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
4796 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
4797 goto release_ras_con;
4798 }
4799 /* must succeed. */
4800 amdgpu_ras_resume(adev);
4801 queue_delayed_work(system_wq, &adev->delayed_init_work,
4802 msecs_to_jiffies(AMDGPU_RESUME_MS));
4803 }
4804
4805 if (amdgpu_sriov_vf(adev)) {
4806 amdgpu_virt_release_full_gpu(adev, true);
4807 flush_delayed_work(&adev->delayed_init_work);
4808 }
4809
4810 /*
4811 * Place those sysfs registering after `late_init`. As some of those
4812 * operations performed in `late_init` might affect the sysfs
4813 * interfaces creating.
4814 */
4815 r = amdgpu_atombios_sysfs_init(adev);
4816 if (r)
4817 drm_err(&adev->ddev,
4818 "registering atombios sysfs failed (%d).\n", r);
4819
4820 r = amdgpu_pm_sysfs_init(adev);
4821 if (r)
4822 dev_err(adev->dev, "registering pm sysfs failed (%d).\n", r);
4823
4824 r = amdgpu_ucode_sysfs_init(adev);
4825 if (r) {
4826 adev->ucode_sysfs_en = false;
4827 dev_err(adev->dev, "Creating firmware sysfs failed (%d).\n", r);
4828 } else
4829 adev->ucode_sysfs_en = true;
4830
4831 r = amdgpu_device_attr_sysfs_init(adev);
4832 if (r)
4833 dev_err(adev->dev, "Could not create amdgpu device attr\n");
4834
4835 r = devm_device_add_group(adev->dev, &amdgpu_board_attrs_group);
4836 if (r)
4837 dev_err(adev->dev,
4838 "Could not create amdgpu board attributes\n");
4839
4840 amdgpu_fru_sysfs_init(adev);
4841 amdgpu_reg_state_sysfs_init(adev);
4842 amdgpu_xcp_sysfs_init(adev);
4843
4844 if (IS_ENABLED(CONFIG_PERF_EVENTS))
4845 r = amdgpu_pmu_init(adev);
4846 if (r)
4847 dev_err(adev->dev, "amdgpu_pmu_init failed\n");
4848
4849 /* Have stored pci confspace at hand for restore in sudden PCI error */
4850 if (amdgpu_device_cache_pci_state(adev->pdev))
4851 pci_restore_state(pdev);
4852
4853 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
4854 /* this will fail for cards that aren't VGA class devices, just
4855 * ignore it
4856 */
4857 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
4858 vga_client_register(adev->pdev, amdgpu_device_vga_set_decode);
4859
4860 px = amdgpu_device_supports_px(adev);
4861
4862 if (px || (!dev_is_removable(&adev->pdev->dev) &&
4863 apple_gmux_detect(NULL, NULL)))
4864 vga_switcheroo_register_client(adev->pdev,
4865 &amdgpu_switcheroo_ops, px);
4866
4867 if (px)
4868 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
4869
4870 if (adev->init_lvl->level == AMDGPU_INIT_LEVEL_MINIMAL_XGMI)
4871 amdgpu_xgmi_reset_on_init(adev);
4872
4873 amdgpu_device_check_iommu_direct_map(adev);
4874
4875 adev->pm_nb.notifier_call = amdgpu_device_pm_notifier;
4876 r = register_pm_notifier(&adev->pm_nb);
4877 if (r)
4878 goto failed;
4879
4880 return 0;
4881
4882 release_ras_con:
4883 if (amdgpu_sriov_vf(adev))
4884 amdgpu_virt_release_full_gpu(adev, true);
4885
4886 /* failed in exclusive mode due to timeout */
4887 if (amdgpu_sriov_vf(adev) &&
4888 !amdgpu_sriov_runtime(adev) &&
4889 amdgpu_virt_mmio_blocked(adev) &&
4890 !amdgpu_virt_wait_reset(adev)) {
4891 dev_err(adev->dev, "VF exclusive mode timeout\n");
4892 /* Don't send request since VF is inactive. */
4893 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
4894 adev->virt.ops = NULL;
4895 r = -EAGAIN;
4896 }
4897 amdgpu_release_ras_context(adev);
4898
4899 failed:
4900 amdgpu_vf_error_trans_all(adev);
4901
4902 return r;
4903 }
4904
amdgpu_device_unmap_mmio(struct amdgpu_device * adev)4905 static void amdgpu_device_unmap_mmio(struct amdgpu_device *adev)
4906 {
4907
4908 /* Clear all CPU mappings pointing to this device */
4909 unmap_mapping_range(adev->ddev.anon_inode->i_mapping, 0, 0, 1);
4910
4911 /* Unmap all mapped bars - Doorbell, registers and VRAM */
4912 amdgpu_doorbell_fini(adev);
4913
4914 iounmap(adev->rmmio);
4915 adev->rmmio = NULL;
4916 if (adev->mman.aper_base_kaddr)
4917 iounmap(adev->mman.aper_base_kaddr);
4918 adev->mman.aper_base_kaddr = NULL;
4919
4920 /* Memory manager related */
4921 if (!adev->gmc.xgmi.connected_to_cpu && !adev->gmc.is_app_apu) {
4922 arch_phys_wc_del(adev->gmc.vram_mtrr);
4923 arch_io_free_memtype_wc(adev->gmc.aper_base, adev->gmc.aper_size);
4924 }
4925 }
4926
4927 /**
4928 * amdgpu_device_fini_hw - tear down the driver
4929 *
4930 * @adev: amdgpu_device pointer
4931 *
4932 * Tear down the driver info (all asics).
4933 * Called at driver shutdown.
4934 */
amdgpu_device_fini_hw(struct amdgpu_device * adev)4935 void amdgpu_device_fini_hw(struct amdgpu_device *adev)
4936 {
4937 dev_info(adev->dev, "amdgpu: finishing device.\n");
4938 flush_delayed_work(&adev->delayed_init_work);
4939
4940 if (adev->mman.initialized)
4941 drain_workqueue(adev->mman.bdev.wq);
4942 adev->shutdown = true;
4943
4944 unregister_pm_notifier(&adev->pm_nb);
4945
4946 /* make sure IB test finished before entering exclusive mode
4947 * to avoid preemption on IB test
4948 */
4949 if (amdgpu_sriov_vf(adev)) {
4950 amdgpu_virt_request_full_gpu(adev, false);
4951 amdgpu_virt_fini_data_exchange(adev);
4952 }
4953
4954 /* disable all interrupts */
4955 amdgpu_irq_disable_all(adev);
4956 if (adev->mode_info.mode_config_initialized) {
4957 if (!drm_drv_uses_atomic_modeset(adev_to_drm(adev)))
4958 drm_helper_force_disable_all(adev_to_drm(adev));
4959 else
4960 drm_atomic_helper_shutdown(adev_to_drm(adev));
4961 }
4962 amdgpu_fence_driver_hw_fini(adev);
4963
4964 if (adev->pm.sysfs_initialized)
4965 amdgpu_pm_sysfs_fini(adev);
4966 if (adev->ucode_sysfs_en)
4967 amdgpu_ucode_sysfs_fini(adev);
4968 amdgpu_device_attr_sysfs_fini(adev);
4969 amdgpu_fru_sysfs_fini(adev);
4970
4971 amdgpu_reg_state_sysfs_fini(adev);
4972 amdgpu_xcp_sysfs_fini(adev);
4973
4974 /* disable ras feature must before hw fini */
4975 amdgpu_ras_pre_fini(adev);
4976
4977 amdgpu_ttm_set_buffer_funcs_status(adev, false);
4978
4979 amdgpu_device_ip_fini_early(adev);
4980
4981 amdgpu_irq_fini_hw(adev);
4982
4983 if (adev->mman.initialized)
4984 ttm_device_clear_dma_mappings(&adev->mman.bdev);
4985
4986 amdgpu_gart_dummy_page_fini(adev);
4987
4988 if (drm_dev_is_unplugged(adev_to_drm(adev)))
4989 amdgpu_device_unmap_mmio(adev);
4990
4991 }
4992
amdgpu_device_fini_sw(struct amdgpu_device * adev)4993 void amdgpu_device_fini_sw(struct amdgpu_device *adev)
4994 {
4995 int i, idx;
4996 bool px;
4997
4998 amdgpu_device_ip_fini(adev);
4999 amdgpu_fence_driver_sw_fini(adev);
5000 amdgpu_ucode_release(&adev->firmware.gpu_info_fw);
5001 adev->accel_working = false;
5002 dma_fence_put(rcu_dereference_protected(adev->gang_submit, true));
5003 for (i = 0; i < MAX_XCP; ++i) {
5004 dma_fence_put(adev->isolation[i].spearhead);
5005 amdgpu_sync_free(&adev->isolation[i].active);
5006 amdgpu_sync_free(&adev->isolation[i].prev);
5007 }
5008
5009 amdgpu_reset_fini(adev);
5010
5011 /* free i2c buses */
5012 amdgpu_i2c_fini(adev);
5013
5014 if (adev->bios) {
5015 if (amdgpu_emu_mode != 1)
5016 amdgpu_atombios_fini(adev);
5017 amdgpu_bios_release(adev);
5018 }
5019
5020 kfree(adev->fru_info);
5021 adev->fru_info = NULL;
5022
5023 kfree(adev->xcp_mgr);
5024 adev->xcp_mgr = NULL;
5025
5026 px = amdgpu_device_supports_px(adev);
5027
5028 if (px || (!dev_is_removable(&adev->pdev->dev) &&
5029 apple_gmux_detect(NULL, NULL)))
5030 vga_switcheroo_unregister_client(adev->pdev);
5031
5032 if (px)
5033 vga_switcheroo_fini_domain_pm_ops(adev->dev);
5034
5035 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
5036 vga_client_unregister(adev->pdev);
5037
5038 if (drm_dev_enter(adev_to_drm(adev), &idx)) {
5039
5040 iounmap(adev->rmmio);
5041 adev->rmmio = NULL;
5042 drm_dev_exit(idx);
5043 }
5044
5045 if (IS_ENABLED(CONFIG_PERF_EVENTS))
5046 amdgpu_pmu_fini(adev);
5047 if (adev->mman.discovery_bin)
5048 amdgpu_discovery_fini(adev);
5049
5050 amdgpu_reset_put_reset_domain(adev->reset_domain);
5051 adev->reset_domain = NULL;
5052
5053 kfree(adev->pci_state);
5054 kfree(adev->pcie_reset_ctx.swds_pcistate);
5055 kfree(adev->pcie_reset_ctx.swus_pcistate);
5056 }
5057
5058 /**
5059 * amdgpu_device_evict_resources - evict device resources
5060 * @adev: amdgpu device object
5061 *
5062 * Evicts all ttm device resources(vram BOs, gart table) from the lru list
5063 * of the vram memory type. Mainly used for evicting device resources
5064 * at suspend time.
5065 *
5066 */
amdgpu_device_evict_resources(struct amdgpu_device * adev)5067 static int amdgpu_device_evict_resources(struct amdgpu_device *adev)
5068 {
5069 int ret;
5070
5071 /* No need to evict vram on APUs unless going to S4 */
5072 if (!adev->in_s4 && (adev->flags & AMD_IS_APU))
5073 return 0;
5074
5075 /* No need to evict when going to S5 through S4 callbacks */
5076 if (system_state == SYSTEM_POWER_OFF)
5077 return 0;
5078
5079 ret = amdgpu_ttm_evict_resources(adev, TTM_PL_VRAM);
5080 if (ret) {
5081 dev_warn(adev->dev, "evicting device resources failed\n");
5082 return ret;
5083 }
5084
5085 if (adev->in_s4) {
5086 ret = ttm_device_prepare_hibernation(&adev->mman.bdev);
5087 if (ret)
5088 dev_err(adev->dev, "prepare hibernation failed, %d\n", ret);
5089 }
5090 return ret;
5091 }
5092
5093 /*
5094 * Suspend & resume.
5095 */
5096 /**
5097 * amdgpu_device_pm_notifier - Notification block for Suspend/Hibernate events
5098 * @nb: notifier block
5099 * @mode: suspend mode
5100 * @data: data
5101 *
5102 * This function is called when the system is about to suspend or hibernate.
5103 * It is used to set the appropriate flags so that eviction can be optimized
5104 * in the pm prepare callback.
5105 */
amdgpu_device_pm_notifier(struct notifier_block * nb,unsigned long mode,void * data)5106 static int amdgpu_device_pm_notifier(struct notifier_block *nb, unsigned long mode,
5107 void *data)
5108 {
5109 struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, pm_nb);
5110
5111 switch (mode) {
5112 case PM_HIBERNATION_PREPARE:
5113 adev->in_s4 = true;
5114 break;
5115 case PM_POST_HIBERNATION:
5116 adev->in_s4 = false;
5117 break;
5118 }
5119
5120 return NOTIFY_DONE;
5121 }
5122
5123 /**
5124 * amdgpu_device_prepare - prepare for device suspend
5125 *
5126 * @dev: drm dev pointer
5127 *
5128 * Prepare to put the hw in the suspend state (all asics).
5129 * Returns 0 for success or an error on failure.
5130 * Called at driver suspend.
5131 */
amdgpu_device_prepare(struct drm_device * dev)5132 int amdgpu_device_prepare(struct drm_device *dev)
5133 {
5134 struct amdgpu_device *adev = drm_to_adev(dev);
5135 int i, r;
5136
5137 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
5138 return 0;
5139
5140 /* Evict the majority of BOs before starting suspend sequence */
5141 r = amdgpu_device_evict_resources(adev);
5142 if (r)
5143 return r;
5144
5145 flush_delayed_work(&adev->gfx.gfx_off_delay_work);
5146
5147 for (i = 0; i < adev->num_ip_blocks; i++) {
5148 if (!adev->ip_blocks[i].status.valid)
5149 continue;
5150 if (!adev->ip_blocks[i].version->funcs->prepare_suspend)
5151 continue;
5152 r = adev->ip_blocks[i].version->funcs->prepare_suspend(&adev->ip_blocks[i]);
5153 if (r)
5154 return r;
5155 }
5156
5157 return 0;
5158 }
5159
5160 /**
5161 * amdgpu_device_complete - complete power state transition
5162 *
5163 * @dev: drm dev pointer
5164 *
5165 * Undo the changes from amdgpu_device_prepare. This will be
5166 * called on all resume transitions, including those that failed.
5167 */
amdgpu_device_complete(struct drm_device * dev)5168 void amdgpu_device_complete(struct drm_device *dev)
5169 {
5170 struct amdgpu_device *adev = drm_to_adev(dev);
5171 int i;
5172
5173 for (i = 0; i < adev->num_ip_blocks; i++) {
5174 if (!adev->ip_blocks[i].status.valid)
5175 continue;
5176 if (!adev->ip_blocks[i].version->funcs->complete)
5177 continue;
5178 adev->ip_blocks[i].version->funcs->complete(&adev->ip_blocks[i]);
5179 }
5180 }
5181
5182 /**
5183 * amdgpu_device_suspend - initiate device suspend
5184 *
5185 * @dev: drm dev pointer
5186 * @notify_clients: notify in-kernel DRM clients
5187 *
5188 * Puts the hw in the suspend state (all asics).
5189 * Returns 0 for success or an error on failure.
5190 * Called at driver suspend.
5191 */
amdgpu_device_suspend(struct drm_device * dev,bool notify_clients)5192 int amdgpu_device_suspend(struct drm_device *dev, bool notify_clients)
5193 {
5194 struct amdgpu_device *adev = drm_to_adev(dev);
5195 int r = 0;
5196
5197 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
5198 return 0;
5199
5200 adev->in_suspend = true;
5201
5202 if (amdgpu_sriov_vf(adev)) {
5203 if (!adev->in_runpm)
5204 amdgpu_amdkfd_suspend_process(adev);
5205 amdgpu_virt_fini_data_exchange(adev);
5206 r = amdgpu_virt_request_full_gpu(adev, false);
5207 if (r)
5208 return r;
5209 }
5210
5211 if (amdgpu_acpi_smart_shift_update(adev, AMDGPU_SS_DEV_D3))
5212 dev_warn(adev->dev, "smart shift update failed\n");
5213
5214 if (notify_clients)
5215 drm_client_dev_suspend(adev_to_drm(adev), false);
5216
5217 cancel_delayed_work_sync(&adev->delayed_init_work);
5218
5219 amdgpu_ras_suspend(adev);
5220
5221 amdgpu_device_ip_suspend_phase1(adev);
5222
5223 amdgpu_amdkfd_suspend(adev, !amdgpu_sriov_vf(adev) && !adev->in_runpm);
5224 amdgpu_userq_suspend(adev);
5225
5226 r = amdgpu_device_evict_resources(adev);
5227 if (r)
5228 return r;
5229
5230 amdgpu_ttm_set_buffer_funcs_status(adev, false);
5231
5232 amdgpu_fence_driver_hw_fini(adev);
5233
5234 amdgpu_device_ip_suspend_phase2(adev);
5235
5236 if (amdgpu_sriov_vf(adev))
5237 amdgpu_virt_release_full_gpu(adev, false);
5238
5239 r = amdgpu_dpm_notify_rlc_state(adev, false);
5240 if (r)
5241 return r;
5242
5243 return 0;
5244 }
5245
amdgpu_virt_resume(struct amdgpu_device * adev)5246 static inline int amdgpu_virt_resume(struct amdgpu_device *adev)
5247 {
5248 int r;
5249 unsigned int prev_physical_node_id = adev->gmc.xgmi.physical_node_id;
5250
5251 /* During VM resume, QEMU programming of VF MSIX table (register GFXMSIX_VECT0_ADDR_LO)
5252 * may not work. The access could be blocked by nBIF protection as VF isn't in
5253 * exclusive access mode. Exclusive access is enabled now, disable/enable MSIX
5254 * so that QEMU reprograms MSIX table.
5255 */
5256 amdgpu_restore_msix(adev);
5257
5258 r = adev->gfxhub.funcs->get_xgmi_info(adev);
5259 if (r)
5260 return r;
5261
5262 dev_info(adev->dev, "xgmi node, old id %d, new id %d\n",
5263 prev_physical_node_id, adev->gmc.xgmi.physical_node_id);
5264
5265 adev->vm_manager.vram_base_offset = adev->gfxhub.funcs->get_mc_fb_offset(adev);
5266 adev->vm_manager.vram_base_offset +=
5267 adev->gmc.xgmi.physical_node_id * adev->gmc.xgmi.node_segment_size;
5268
5269 return 0;
5270 }
5271
5272 /**
5273 * amdgpu_device_resume - initiate device resume
5274 *
5275 * @dev: drm dev pointer
5276 * @notify_clients: notify in-kernel DRM clients
5277 *
5278 * Bring the hw back to operating state (all asics).
5279 * Returns 0 for success or an error on failure.
5280 * Called at driver resume.
5281 */
amdgpu_device_resume(struct drm_device * dev,bool notify_clients)5282 int amdgpu_device_resume(struct drm_device *dev, bool notify_clients)
5283 {
5284 struct amdgpu_device *adev = drm_to_adev(dev);
5285 int r = 0;
5286
5287 if (amdgpu_sriov_vf(adev)) {
5288 r = amdgpu_virt_request_full_gpu(adev, true);
5289 if (r)
5290 return r;
5291 }
5292
5293 if (amdgpu_virt_xgmi_migrate_enabled(adev)) {
5294 r = amdgpu_virt_resume(adev);
5295 if (r)
5296 goto exit;
5297 }
5298
5299 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
5300 return 0;
5301
5302 if (adev->in_s0ix)
5303 amdgpu_dpm_gfx_state_change(adev, sGpuChangeState_D0Entry);
5304
5305 /* post card */
5306 if (amdgpu_device_need_post(adev)) {
5307 r = amdgpu_device_asic_init(adev);
5308 if (r)
5309 dev_err(adev->dev, "amdgpu asic init failed\n");
5310 }
5311
5312 r = amdgpu_device_ip_resume(adev);
5313
5314 if (r) {
5315 dev_err(adev->dev, "amdgpu_device_ip_resume failed (%d).\n", r);
5316 goto exit;
5317 }
5318
5319 r = amdgpu_amdkfd_resume(adev, !amdgpu_sriov_vf(adev) && !adev->in_runpm);
5320 if (r)
5321 goto exit;
5322
5323 r = amdgpu_userq_resume(adev);
5324 if (r)
5325 goto exit;
5326
5327 r = amdgpu_device_ip_late_init(adev);
5328 if (r)
5329 goto exit;
5330
5331 queue_delayed_work(system_wq, &adev->delayed_init_work,
5332 msecs_to_jiffies(AMDGPU_RESUME_MS));
5333 exit:
5334 if (amdgpu_sriov_vf(adev)) {
5335 amdgpu_virt_init_data_exchange(adev);
5336 amdgpu_virt_release_full_gpu(adev, true);
5337
5338 if (!r && !adev->in_runpm)
5339 r = amdgpu_amdkfd_resume_process(adev);
5340 }
5341
5342 if (r)
5343 return r;
5344
5345 /* Make sure IB tests flushed */
5346 flush_delayed_work(&adev->delayed_init_work);
5347
5348 if (notify_clients)
5349 drm_client_dev_resume(adev_to_drm(adev), false);
5350
5351 amdgpu_ras_resume(adev);
5352
5353 if (adev->mode_info.num_crtc) {
5354 /*
5355 * Most of the connector probing functions try to acquire runtime pm
5356 * refs to ensure that the GPU is powered on when connector polling is
5357 * performed. Since we're calling this from a runtime PM callback,
5358 * trying to acquire rpm refs will cause us to deadlock.
5359 *
5360 * Since we're guaranteed to be holding the rpm lock, it's safe to
5361 * temporarily disable the rpm helpers so this doesn't deadlock us.
5362 */
5363 #ifdef CONFIG_PM
5364 dev->dev->power.disable_depth++;
5365 #endif
5366 if (!adev->dc_enabled)
5367 drm_helper_hpd_irq_event(dev);
5368 else
5369 drm_kms_helper_hotplug_event(dev);
5370 #ifdef CONFIG_PM
5371 dev->dev->power.disable_depth--;
5372 #endif
5373 }
5374
5375 amdgpu_vram_mgr_clear_reset_blocks(adev);
5376 adev->in_suspend = false;
5377
5378 if (amdgpu_acpi_smart_shift_update(adev, AMDGPU_SS_DEV_D0))
5379 dev_warn(adev->dev, "smart shift update failed\n");
5380
5381 return 0;
5382 }
5383
5384 /**
5385 * amdgpu_device_ip_check_soft_reset - did soft reset succeed
5386 *
5387 * @adev: amdgpu_device pointer
5388 *
5389 * The list of all the hardware IPs that make up the asic is walked and
5390 * the check_soft_reset callbacks are run. check_soft_reset determines
5391 * if the asic is still hung or not.
5392 * Returns true if any of the IPs are still in a hung state, false if not.
5393 */
amdgpu_device_ip_check_soft_reset(struct amdgpu_device * adev)5394 static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
5395 {
5396 int i;
5397 bool asic_hang = false;
5398
5399 if (amdgpu_sriov_vf(adev))
5400 return true;
5401
5402 if (amdgpu_asic_need_full_reset(adev))
5403 return true;
5404
5405 for (i = 0; i < adev->num_ip_blocks; i++) {
5406 if (!adev->ip_blocks[i].status.valid)
5407 continue;
5408 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
5409 adev->ip_blocks[i].status.hang =
5410 adev->ip_blocks[i].version->funcs->check_soft_reset(
5411 &adev->ip_blocks[i]);
5412 if (adev->ip_blocks[i].status.hang) {
5413 dev_info(adev->dev, "IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
5414 asic_hang = true;
5415 }
5416 }
5417 return asic_hang;
5418 }
5419
5420 /**
5421 * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
5422 *
5423 * @adev: amdgpu_device pointer
5424 *
5425 * The list of all the hardware IPs that make up the asic is walked and the
5426 * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
5427 * handles any IP specific hardware or software state changes that are
5428 * necessary for a soft reset to succeed.
5429 * Returns 0 on success, negative error code on failure.
5430 */
amdgpu_device_ip_pre_soft_reset(struct amdgpu_device * adev)5431 static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
5432 {
5433 int i, r = 0;
5434
5435 for (i = 0; i < adev->num_ip_blocks; i++) {
5436 if (!adev->ip_blocks[i].status.valid)
5437 continue;
5438 if (adev->ip_blocks[i].status.hang &&
5439 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
5440 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(&adev->ip_blocks[i]);
5441 if (r)
5442 return r;
5443 }
5444 }
5445
5446 return 0;
5447 }
5448
5449 /**
5450 * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
5451 *
5452 * @adev: amdgpu_device pointer
5453 *
5454 * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
5455 * reset is necessary to recover.
5456 * Returns true if a full asic reset is required, false if not.
5457 */
amdgpu_device_ip_need_full_reset(struct amdgpu_device * adev)5458 static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
5459 {
5460 int i;
5461
5462 if (amdgpu_asic_need_full_reset(adev))
5463 return true;
5464
5465 for (i = 0; i < adev->num_ip_blocks; i++) {
5466 if (!adev->ip_blocks[i].status.valid)
5467 continue;
5468 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
5469 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
5470 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
5471 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
5472 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
5473 if (adev->ip_blocks[i].status.hang) {
5474 dev_info(adev->dev, "Some block need full reset!\n");
5475 return true;
5476 }
5477 }
5478 }
5479 return false;
5480 }
5481
5482 /**
5483 * amdgpu_device_ip_soft_reset - do a soft reset
5484 *
5485 * @adev: amdgpu_device pointer
5486 *
5487 * The list of all the hardware IPs that make up the asic is walked and the
5488 * soft_reset callbacks are run if the block is hung. soft_reset handles any
5489 * IP specific hardware or software state changes that are necessary to soft
5490 * reset the IP.
5491 * Returns 0 on success, negative error code on failure.
5492 */
amdgpu_device_ip_soft_reset(struct amdgpu_device * adev)5493 static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
5494 {
5495 int i, r = 0;
5496
5497 for (i = 0; i < adev->num_ip_blocks; i++) {
5498 if (!adev->ip_blocks[i].status.valid)
5499 continue;
5500 if (adev->ip_blocks[i].status.hang &&
5501 adev->ip_blocks[i].version->funcs->soft_reset) {
5502 r = adev->ip_blocks[i].version->funcs->soft_reset(&adev->ip_blocks[i]);
5503 if (r)
5504 return r;
5505 }
5506 }
5507
5508 return 0;
5509 }
5510
5511 /**
5512 * amdgpu_device_ip_post_soft_reset - clean up from soft reset
5513 *
5514 * @adev: amdgpu_device pointer
5515 *
5516 * The list of all the hardware IPs that make up the asic is walked and the
5517 * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
5518 * handles any IP specific hardware or software state changes that are
5519 * necessary after the IP has been soft reset.
5520 * Returns 0 on success, negative error code on failure.
5521 */
amdgpu_device_ip_post_soft_reset(struct amdgpu_device * adev)5522 static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
5523 {
5524 int i, r = 0;
5525
5526 for (i = 0; i < adev->num_ip_blocks; i++) {
5527 if (!adev->ip_blocks[i].status.valid)
5528 continue;
5529 if (adev->ip_blocks[i].status.hang &&
5530 adev->ip_blocks[i].version->funcs->post_soft_reset)
5531 r = adev->ip_blocks[i].version->funcs->post_soft_reset(&adev->ip_blocks[i]);
5532 if (r)
5533 return r;
5534 }
5535
5536 return 0;
5537 }
5538
5539 /**
5540 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
5541 *
5542 * @adev: amdgpu_device pointer
5543 * @reset_context: amdgpu reset context pointer
5544 *
5545 * do VF FLR and reinitialize Asic
5546 * return 0 means succeeded otherwise failed
5547 */
amdgpu_device_reset_sriov(struct amdgpu_device * adev,struct amdgpu_reset_context * reset_context)5548 static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
5549 struct amdgpu_reset_context *reset_context)
5550 {
5551 int r;
5552 struct amdgpu_hive_info *hive = NULL;
5553
5554 if (test_bit(AMDGPU_HOST_FLR, &reset_context->flags)) {
5555 if (!amdgpu_ras_get_fed_status(adev))
5556 amdgpu_virt_ready_to_reset(adev);
5557 amdgpu_virt_wait_reset(adev);
5558 clear_bit(AMDGPU_HOST_FLR, &reset_context->flags);
5559 r = amdgpu_virt_request_full_gpu(adev, true);
5560 } else {
5561 r = amdgpu_virt_reset_gpu(adev);
5562 }
5563 if (r)
5564 return r;
5565
5566 amdgpu_ras_clear_err_state(adev);
5567 amdgpu_irq_gpu_reset_resume_helper(adev);
5568
5569 /* some sw clean up VF needs to do before recover */
5570 amdgpu_virt_post_reset(adev);
5571
5572 /* Resume IP prior to SMC */
5573 r = amdgpu_device_ip_reinit_early_sriov(adev);
5574 if (r)
5575 return r;
5576
5577 amdgpu_virt_init_data_exchange(adev);
5578
5579 r = amdgpu_device_fw_loading(adev);
5580 if (r)
5581 return r;
5582
5583 /* now we are okay to resume SMC/CP/SDMA */
5584 r = amdgpu_device_ip_reinit_late_sriov(adev);
5585 if (r)
5586 return r;
5587
5588 hive = amdgpu_get_xgmi_hive(adev);
5589 /* Update PSP FW topology after reset */
5590 if (hive && adev->gmc.xgmi.num_physical_nodes > 1)
5591 r = amdgpu_xgmi_update_topology(hive, adev);
5592 if (hive)
5593 amdgpu_put_xgmi_hive(hive);
5594 if (r)
5595 return r;
5596
5597 r = amdgpu_ib_ring_tests(adev);
5598 if (r)
5599 return r;
5600
5601 if (adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST)
5602 amdgpu_inc_vram_lost(adev);
5603
5604 /* need to be called during full access so we can't do it later like
5605 * bare-metal does.
5606 */
5607 amdgpu_amdkfd_post_reset(adev);
5608 amdgpu_virt_release_full_gpu(adev, true);
5609
5610 /* Aldebaran and gfx_11_0_3 support ras in SRIOV, so need resume ras during reset */
5611 if (amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 4, 2) ||
5612 amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 4, 3) ||
5613 amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 4, 4) ||
5614 amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 5, 0) ||
5615 amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(11, 0, 3))
5616 amdgpu_ras_resume(adev);
5617
5618 amdgpu_virt_ras_telemetry_post_reset(adev);
5619
5620 return 0;
5621 }
5622
5623 /**
5624 * amdgpu_device_has_job_running - check if there is any unfinished job
5625 *
5626 * @adev: amdgpu_device pointer
5627 *
5628 * check if there is any job running on the device when guest driver receives
5629 * FLR notification from host driver. If there are still jobs running, then
5630 * the guest driver will not respond the FLR reset. Instead, let the job hit
5631 * the timeout and guest driver then issue the reset request.
5632 */
amdgpu_device_has_job_running(struct amdgpu_device * adev)5633 bool amdgpu_device_has_job_running(struct amdgpu_device *adev)
5634 {
5635 int i;
5636
5637 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5638 struct amdgpu_ring *ring = adev->rings[i];
5639
5640 if (!amdgpu_ring_sched_ready(ring))
5641 continue;
5642
5643 if (amdgpu_fence_count_emitted(ring))
5644 return true;
5645 }
5646 return false;
5647 }
5648
5649 /**
5650 * amdgpu_device_should_recover_gpu - check if we should try GPU recovery
5651 *
5652 * @adev: amdgpu_device pointer
5653 *
5654 * Check amdgpu_gpu_recovery and SRIOV status to see if we should try to recover
5655 * a hung GPU.
5656 */
amdgpu_device_should_recover_gpu(struct amdgpu_device * adev)5657 bool amdgpu_device_should_recover_gpu(struct amdgpu_device *adev)
5658 {
5659
5660 if (amdgpu_gpu_recovery == 0)
5661 goto disabled;
5662
5663 /* Skip soft reset check in fatal error mode */
5664 if (!amdgpu_ras_is_poison_mode_supported(adev))
5665 return true;
5666
5667 if (amdgpu_sriov_vf(adev))
5668 return true;
5669
5670 if (amdgpu_gpu_recovery == -1) {
5671 switch (adev->asic_type) {
5672 #ifdef CONFIG_DRM_AMDGPU_SI
5673 case CHIP_VERDE:
5674 case CHIP_TAHITI:
5675 case CHIP_PITCAIRN:
5676 case CHIP_OLAND:
5677 case CHIP_HAINAN:
5678 #endif
5679 #ifdef CONFIG_DRM_AMDGPU_CIK
5680 case CHIP_KAVERI:
5681 case CHIP_KABINI:
5682 case CHIP_MULLINS:
5683 #endif
5684 case CHIP_CARRIZO:
5685 case CHIP_STONEY:
5686 case CHIP_CYAN_SKILLFISH:
5687 goto disabled;
5688 default:
5689 break;
5690 }
5691 }
5692
5693 return true;
5694
5695 disabled:
5696 dev_info(adev->dev, "GPU recovery disabled.\n");
5697 return false;
5698 }
5699
amdgpu_device_mode1_reset(struct amdgpu_device * adev)5700 int amdgpu_device_mode1_reset(struct amdgpu_device *adev)
5701 {
5702 u32 i;
5703 int ret = 0;
5704
5705 if (adev->bios)
5706 amdgpu_atombios_scratch_regs_engine_hung(adev, true);
5707
5708 dev_info(adev->dev, "GPU mode1 reset\n");
5709
5710 /* Cache the state before bus master disable. The saved config space
5711 * values are used in other cases like restore after mode-2 reset.
5712 */
5713 amdgpu_device_cache_pci_state(adev->pdev);
5714
5715 /* disable BM */
5716 pci_clear_master(adev->pdev);
5717
5718 if (amdgpu_dpm_is_mode1_reset_supported(adev)) {
5719 dev_info(adev->dev, "GPU smu mode1 reset\n");
5720 ret = amdgpu_dpm_mode1_reset(adev);
5721 } else {
5722 dev_info(adev->dev, "GPU psp mode1 reset\n");
5723 ret = psp_gpu_reset(adev);
5724 }
5725
5726 if (ret)
5727 goto mode1_reset_failed;
5728
5729 amdgpu_device_load_pci_state(adev->pdev);
5730 ret = amdgpu_psp_wait_for_bootloader(adev);
5731 if (ret)
5732 goto mode1_reset_failed;
5733
5734 /* wait for asic to come out of reset */
5735 for (i = 0; i < adev->usec_timeout; i++) {
5736 u32 memsize = adev->nbio.funcs->get_memsize(adev);
5737
5738 if (memsize != 0xffffffff)
5739 break;
5740 udelay(1);
5741 }
5742
5743 if (i >= adev->usec_timeout) {
5744 ret = -ETIMEDOUT;
5745 goto mode1_reset_failed;
5746 }
5747
5748 if (adev->bios)
5749 amdgpu_atombios_scratch_regs_engine_hung(adev, false);
5750
5751 return 0;
5752
5753 mode1_reset_failed:
5754 dev_err(adev->dev, "GPU mode1 reset failed\n");
5755 return ret;
5756 }
5757
amdgpu_device_link_reset(struct amdgpu_device * adev)5758 int amdgpu_device_link_reset(struct amdgpu_device *adev)
5759 {
5760 int ret = 0;
5761
5762 dev_info(adev->dev, "GPU link reset\n");
5763
5764 if (!amdgpu_reset_in_dpc(adev))
5765 ret = amdgpu_dpm_link_reset(adev);
5766
5767 if (ret)
5768 goto link_reset_failed;
5769
5770 ret = amdgpu_psp_wait_for_bootloader(adev);
5771 if (ret)
5772 goto link_reset_failed;
5773
5774 return 0;
5775
5776 link_reset_failed:
5777 dev_err(adev->dev, "GPU link reset failed\n");
5778 return ret;
5779 }
5780
amdgpu_device_pre_asic_reset(struct amdgpu_device * adev,struct amdgpu_reset_context * reset_context)5781 int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
5782 struct amdgpu_reset_context *reset_context)
5783 {
5784 int i, r = 0;
5785 struct amdgpu_job *job = NULL;
5786 struct amdgpu_device *tmp_adev = reset_context->reset_req_dev;
5787 bool need_full_reset =
5788 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
5789
5790 if (reset_context->reset_req_dev == adev)
5791 job = reset_context->job;
5792
5793 if (amdgpu_sriov_vf(adev))
5794 amdgpu_virt_pre_reset(adev);
5795
5796 amdgpu_fence_driver_isr_toggle(adev, true);
5797
5798 /* block all schedulers and reset given job's ring */
5799 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5800 struct amdgpu_ring *ring = adev->rings[i];
5801
5802 if (!amdgpu_ring_sched_ready(ring))
5803 continue;
5804
5805 /* Clear job fence from fence drv to avoid force_completion
5806 * leave NULL and vm flush fence in fence drv
5807 */
5808 amdgpu_fence_driver_clear_job_fences(ring);
5809
5810 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
5811 amdgpu_fence_driver_force_completion(ring);
5812 }
5813
5814 amdgpu_fence_driver_isr_toggle(adev, false);
5815
5816 if (job && job->vm)
5817 drm_sched_increase_karma(&job->base);
5818
5819 r = amdgpu_reset_prepare_hwcontext(adev, reset_context);
5820 /* If reset handler not implemented, continue; otherwise return */
5821 if (r == -EOPNOTSUPP)
5822 r = 0;
5823 else
5824 return r;
5825
5826 /* Don't suspend on bare metal if we are not going to HW reset the ASIC */
5827 if (!amdgpu_sriov_vf(adev)) {
5828
5829 if (!need_full_reset)
5830 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
5831
5832 if (!need_full_reset && amdgpu_gpu_recovery &&
5833 amdgpu_device_ip_check_soft_reset(adev)) {
5834 amdgpu_device_ip_pre_soft_reset(adev);
5835 r = amdgpu_device_ip_soft_reset(adev);
5836 amdgpu_device_ip_post_soft_reset(adev);
5837 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
5838 dev_info(adev->dev, "soft reset failed, will fallback to full reset!\n");
5839 need_full_reset = true;
5840 }
5841 }
5842
5843 if (!test_bit(AMDGPU_SKIP_COREDUMP, &reset_context->flags)) {
5844 dev_info(tmp_adev->dev, "Dumping IP State\n");
5845 /* Trigger ip dump before we reset the asic */
5846 for (i = 0; i < tmp_adev->num_ip_blocks; i++)
5847 if (tmp_adev->ip_blocks[i].version->funcs->dump_ip_state)
5848 tmp_adev->ip_blocks[i].version->funcs
5849 ->dump_ip_state((void *)&tmp_adev->ip_blocks[i]);
5850 dev_info(tmp_adev->dev, "Dumping IP State Completed\n");
5851 }
5852
5853 if (need_full_reset)
5854 r = amdgpu_device_ip_suspend(adev);
5855 if (need_full_reset)
5856 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
5857 else
5858 clear_bit(AMDGPU_NEED_FULL_RESET,
5859 &reset_context->flags);
5860 }
5861
5862 return r;
5863 }
5864
amdgpu_device_reinit_after_reset(struct amdgpu_reset_context * reset_context)5865 int amdgpu_device_reinit_after_reset(struct amdgpu_reset_context *reset_context)
5866 {
5867 struct list_head *device_list_handle;
5868 bool full_reset, vram_lost = false;
5869 struct amdgpu_device *tmp_adev;
5870 int r, init_level;
5871
5872 device_list_handle = reset_context->reset_device_list;
5873
5874 if (!device_list_handle)
5875 return -EINVAL;
5876
5877 full_reset = test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
5878
5879 /**
5880 * If it's reset on init, it's default init level, otherwise keep level
5881 * as recovery level.
5882 */
5883 if (reset_context->method == AMD_RESET_METHOD_ON_INIT)
5884 init_level = AMDGPU_INIT_LEVEL_DEFAULT;
5885 else
5886 init_level = AMDGPU_INIT_LEVEL_RESET_RECOVERY;
5887
5888 r = 0;
5889 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5890 amdgpu_set_init_level(tmp_adev, init_level);
5891 if (full_reset) {
5892 /* post card */
5893 amdgpu_reset_set_dpc_status(tmp_adev, false);
5894 amdgpu_ras_clear_err_state(tmp_adev);
5895 r = amdgpu_device_asic_init(tmp_adev);
5896 if (r) {
5897 dev_warn(tmp_adev->dev, "asic atom init failed!");
5898 } else {
5899 dev_info(tmp_adev->dev, "GPU reset succeeded, trying to resume\n");
5900
5901 r = amdgpu_device_ip_resume_phase1(tmp_adev);
5902 if (r)
5903 goto out;
5904
5905 vram_lost = amdgpu_device_check_vram_lost(tmp_adev);
5906
5907 if (!test_bit(AMDGPU_SKIP_COREDUMP, &reset_context->flags))
5908 amdgpu_coredump(tmp_adev, false, vram_lost, reset_context->job);
5909
5910 if (vram_lost) {
5911 dev_info(
5912 tmp_adev->dev,
5913 "VRAM is lost due to GPU reset!\n");
5914 amdgpu_inc_vram_lost(tmp_adev);
5915 }
5916
5917 r = amdgpu_device_fw_loading(tmp_adev);
5918 if (r)
5919 return r;
5920
5921 r = amdgpu_xcp_restore_partition_mode(
5922 tmp_adev->xcp_mgr);
5923 if (r)
5924 goto out;
5925
5926 r = amdgpu_device_ip_resume_phase2(tmp_adev);
5927 if (r)
5928 goto out;
5929
5930 if (tmp_adev->mman.buffer_funcs_ring->sched.ready)
5931 amdgpu_ttm_set_buffer_funcs_status(tmp_adev, true);
5932
5933 r = amdgpu_device_ip_resume_phase3(tmp_adev);
5934 if (r)
5935 goto out;
5936
5937 if (vram_lost)
5938 amdgpu_device_fill_reset_magic(tmp_adev);
5939
5940 /*
5941 * Add this ASIC as tracked as reset was already
5942 * complete successfully.
5943 */
5944 amdgpu_register_gpu_instance(tmp_adev);
5945
5946 if (!reset_context->hive &&
5947 tmp_adev->gmc.xgmi.num_physical_nodes > 1)
5948 amdgpu_xgmi_add_device(tmp_adev);
5949
5950 r = amdgpu_device_ip_late_init(tmp_adev);
5951 if (r)
5952 goto out;
5953
5954 drm_client_dev_resume(adev_to_drm(tmp_adev), false);
5955
5956 /*
5957 * The GPU enters bad state once faulty pages
5958 * by ECC has reached the threshold, and ras
5959 * recovery is scheduled next. So add one check
5960 * here to break recovery if it indeed exceeds
5961 * bad page threshold, and remind user to
5962 * retire this GPU or setting one bigger
5963 * bad_page_threshold value to fix this once
5964 * probing driver again.
5965 */
5966 if (!amdgpu_ras_is_rma(tmp_adev)) {
5967 /* must succeed. */
5968 amdgpu_ras_resume(tmp_adev);
5969 } else {
5970 r = -EINVAL;
5971 goto out;
5972 }
5973
5974 /* Update PSP FW topology after reset */
5975 if (reset_context->hive &&
5976 tmp_adev->gmc.xgmi.num_physical_nodes > 1)
5977 r = amdgpu_xgmi_update_topology(
5978 reset_context->hive, tmp_adev);
5979 }
5980 }
5981
5982 out:
5983 if (!r) {
5984 /* IP init is complete now, set level as default */
5985 amdgpu_set_init_level(tmp_adev,
5986 AMDGPU_INIT_LEVEL_DEFAULT);
5987 amdgpu_irq_gpu_reset_resume_helper(tmp_adev);
5988 r = amdgpu_ib_ring_tests(tmp_adev);
5989 if (r) {
5990 dev_err(tmp_adev->dev, "ib ring test failed (%d).\n", r);
5991 r = -EAGAIN;
5992 goto end;
5993 }
5994 }
5995
5996 if (r)
5997 tmp_adev->asic_reset_res = r;
5998 }
5999
6000 end:
6001 return r;
6002 }
6003
amdgpu_do_asic_reset(struct list_head * device_list_handle,struct amdgpu_reset_context * reset_context)6004 int amdgpu_do_asic_reset(struct list_head *device_list_handle,
6005 struct amdgpu_reset_context *reset_context)
6006 {
6007 struct amdgpu_device *tmp_adev = NULL;
6008 bool need_full_reset, skip_hw_reset;
6009 int r = 0;
6010
6011 /* Try reset handler method first */
6012 tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
6013 reset_list);
6014
6015 reset_context->reset_device_list = device_list_handle;
6016 r = amdgpu_reset_perform_reset(tmp_adev, reset_context);
6017 /* If reset handler not implemented, continue; otherwise return */
6018 if (r == -EOPNOTSUPP)
6019 r = 0;
6020 else
6021 return r;
6022
6023 /* Reset handler not implemented, use the default method */
6024 need_full_reset =
6025 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
6026 skip_hw_reset = test_bit(AMDGPU_SKIP_HW_RESET, &reset_context->flags);
6027
6028 /*
6029 * ASIC reset has to be done on all XGMI hive nodes ASAP
6030 * to allow proper links negotiation in FW (within 1 sec)
6031 */
6032 if (!skip_hw_reset && need_full_reset) {
6033 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
6034 /* For XGMI run all resets in parallel to speed up the process */
6035 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
6036 if (!queue_work(system_unbound_wq,
6037 &tmp_adev->xgmi_reset_work))
6038 r = -EALREADY;
6039 } else
6040 r = amdgpu_asic_reset(tmp_adev);
6041
6042 if (r) {
6043 dev_err(tmp_adev->dev,
6044 "ASIC reset failed with error, %d for drm dev, %s",
6045 r, adev_to_drm(tmp_adev)->unique);
6046 goto out;
6047 }
6048 }
6049
6050 /* For XGMI wait for all resets to complete before proceed */
6051 if (!r) {
6052 list_for_each_entry(tmp_adev, device_list_handle,
6053 reset_list) {
6054 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
6055 flush_work(&tmp_adev->xgmi_reset_work);
6056 r = tmp_adev->asic_reset_res;
6057 if (r)
6058 break;
6059 }
6060 }
6061 }
6062 }
6063
6064 if (!r && amdgpu_ras_intr_triggered()) {
6065 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
6066 amdgpu_ras_reset_error_count(tmp_adev,
6067 AMDGPU_RAS_BLOCK__MMHUB);
6068 }
6069
6070 amdgpu_ras_intr_cleared();
6071 }
6072
6073 r = amdgpu_device_reinit_after_reset(reset_context);
6074 if (r == -EAGAIN)
6075 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
6076 else
6077 clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
6078
6079 out:
6080 return r;
6081 }
6082
amdgpu_device_set_mp1_state(struct amdgpu_device * adev)6083 static void amdgpu_device_set_mp1_state(struct amdgpu_device *adev)
6084 {
6085
6086 switch (amdgpu_asic_reset_method(adev)) {
6087 case AMD_RESET_METHOD_MODE1:
6088 case AMD_RESET_METHOD_LINK:
6089 adev->mp1_state = PP_MP1_STATE_SHUTDOWN;
6090 break;
6091 case AMD_RESET_METHOD_MODE2:
6092 adev->mp1_state = PP_MP1_STATE_RESET;
6093 break;
6094 default:
6095 adev->mp1_state = PP_MP1_STATE_NONE;
6096 break;
6097 }
6098 }
6099
amdgpu_device_unset_mp1_state(struct amdgpu_device * adev)6100 static void amdgpu_device_unset_mp1_state(struct amdgpu_device *adev)
6101 {
6102 amdgpu_vf_error_trans_all(adev);
6103 adev->mp1_state = PP_MP1_STATE_NONE;
6104 }
6105
amdgpu_device_resume_display_audio(struct amdgpu_device * adev)6106 static void amdgpu_device_resume_display_audio(struct amdgpu_device *adev)
6107 {
6108 struct pci_dev *p = NULL;
6109
6110 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
6111 adev->pdev->bus->number, 1);
6112 if (p) {
6113 pm_runtime_enable(&(p->dev));
6114 pm_runtime_resume(&(p->dev));
6115 }
6116
6117 pci_dev_put(p);
6118 }
6119
amdgpu_device_suspend_display_audio(struct amdgpu_device * adev)6120 static int amdgpu_device_suspend_display_audio(struct amdgpu_device *adev)
6121 {
6122 enum amd_reset_method reset_method;
6123 struct pci_dev *p = NULL;
6124 u64 expires;
6125
6126 /*
6127 * For now, only BACO and mode1 reset are confirmed
6128 * to suffer the audio issue without proper suspended.
6129 */
6130 reset_method = amdgpu_asic_reset_method(adev);
6131 if ((reset_method != AMD_RESET_METHOD_BACO) &&
6132 (reset_method != AMD_RESET_METHOD_MODE1))
6133 return -EINVAL;
6134
6135 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
6136 adev->pdev->bus->number, 1);
6137 if (!p)
6138 return -ENODEV;
6139
6140 expires = pm_runtime_autosuspend_expiration(&(p->dev));
6141 if (!expires)
6142 /*
6143 * If we cannot get the audio device autosuspend delay,
6144 * a fixed 4S interval will be used. Considering 3S is
6145 * the audio controller default autosuspend delay setting.
6146 * 4S used here is guaranteed to cover that.
6147 */
6148 expires = ktime_get_mono_fast_ns() + NSEC_PER_SEC * 4ULL;
6149
6150 while (!pm_runtime_status_suspended(&(p->dev))) {
6151 if (!pm_runtime_suspend(&(p->dev)))
6152 break;
6153
6154 if (expires < ktime_get_mono_fast_ns()) {
6155 dev_warn(adev->dev, "failed to suspend display audio\n");
6156 pci_dev_put(p);
6157 /* TODO: abort the succeeding gpu reset? */
6158 return -ETIMEDOUT;
6159 }
6160 }
6161
6162 pm_runtime_disable(&(p->dev));
6163
6164 pci_dev_put(p);
6165 return 0;
6166 }
6167
amdgpu_device_stop_pending_resets(struct amdgpu_device * adev)6168 static inline void amdgpu_device_stop_pending_resets(struct amdgpu_device *adev)
6169 {
6170 struct amdgpu_ras *con = amdgpu_ras_get_context(adev);
6171
6172 #if defined(CONFIG_DEBUG_FS)
6173 if (!amdgpu_sriov_vf(adev))
6174 cancel_work(&adev->reset_work);
6175 #endif
6176
6177 if (adev->kfd.dev)
6178 cancel_work(&adev->kfd.reset_work);
6179
6180 if (amdgpu_sriov_vf(adev))
6181 cancel_work(&adev->virt.flr_work);
6182
6183 if (con && adev->ras_enabled)
6184 cancel_work(&con->recovery_work);
6185
6186 }
6187
amdgpu_device_health_check(struct list_head * device_list_handle)6188 static int amdgpu_device_health_check(struct list_head *device_list_handle)
6189 {
6190 struct amdgpu_device *tmp_adev;
6191 int ret = 0;
6192
6193 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
6194 ret |= amdgpu_device_bus_status_check(tmp_adev);
6195 }
6196
6197 return ret;
6198 }
6199
amdgpu_device_recovery_prepare(struct amdgpu_device * adev,struct list_head * device_list,struct amdgpu_hive_info * hive)6200 static void amdgpu_device_recovery_prepare(struct amdgpu_device *adev,
6201 struct list_head *device_list,
6202 struct amdgpu_hive_info *hive)
6203 {
6204 struct amdgpu_device *tmp_adev = NULL;
6205
6206 /*
6207 * Build list of devices to reset.
6208 * In case we are in XGMI hive mode, resort the device list
6209 * to put adev in the 1st position.
6210 */
6211 if (!amdgpu_sriov_vf(adev) && (adev->gmc.xgmi.num_physical_nodes > 1) && hive) {
6212 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
6213 list_add_tail(&tmp_adev->reset_list, device_list);
6214 if (adev->shutdown)
6215 tmp_adev->shutdown = true;
6216 if (amdgpu_reset_in_dpc(adev))
6217 tmp_adev->pcie_reset_ctx.in_link_reset = true;
6218 }
6219 if (!list_is_first(&adev->reset_list, device_list))
6220 list_rotate_to_front(&adev->reset_list, device_list);
6221 } else {
6222 list_add_tail(&adev->reset_list, device_list);
6223 }
6224 }
6225
amdgpu_device_recovery_get_reset_lock(struct amdgpu_device * adev,struct list_head * device_list)6226 static void amdgpu_device_recovery_get_reset_lock(struct amdgpu_device *adev,
6227 struct list_head *device_list)
6228 {
6229 struct amdgpu_device *tmp_adev = NULL;
6230
6231 if (list_empty(device_list))
6232 return;
6233 tmp_adev =
6234 list_first_entry(device_list, struct amdgpu_device, reset_list);
6235 amdgpu_device_lock_reset_domain(tmp_adev->reset_domain);
6236 }
6237
amdgpu_device_recovery_put_reset_lock(struct amdgpu_device * adev,struct list_head * device_list)6238 static void amdgpu_device_recovery_put_reset_lock(struct amdgpu_device *adev,
6239 struct list_head *device_list)
6240 {
6241 struct amdgpu_device *tmp_adev = NULL;
6242
6243 if (list_empty(device_list))
6244 return;
6245 tmp_adev =
6246 list_first_entry(device_list, struct amdgpu_device, reset_list);
6247 amdgpu_device_unlock_reset_domain(tmp_adev->reset_domain);
6248 }
6249
amdgpu_device_halt_activities(struct amdgpu_device * adev,struct amdgpu_job * job,struct amdgpu_reset_context * reset_context,struct list_head * device_list,struct amdgpu_hive_info * hive,bool need_emergency_restart)6250 static void amdgpu_device_halt_activities(struct amdgpu_device *adev,
6251 struct amdgpu_job *job,
6252 struct amdgpu_reset_context *reset_context,
6253 struct list_head *device_list,
6254 struct amdgpu_hive_info *hive,
6255 bool need_emergency_restart)
6256 {
6257 struct amdgpu_device *tmp_adev = NULL;
6258 int i;
6259
6260 /* block all schedulers and reset given job's ring */
6261 list_for_each_entry(tmp_adev, device_list, reset_list) {
6262 amdgpu_device_set_mp1_state(tmp_adev);
6263
6264 /*
6265 * Try to put the audio codec into suspend state
6266 * before gpu reset started.
6267 *
6268 * Due to the power domain of the graphics device
6269 * is shared with AZ power domain. Without this,
6270 * we may change the audio hardware from behind
6271 * the audio driver's back. That will trigger
6272 * some audio codec errors.
6273 */
6274 if (!amdgpu_device_suspend_display_audio(tmp_adev))
6275 tmp_adev->pcie_reset_ctx.audio_suspended = true;
6276
6277 amdgpu_ras_set_error_query_ready(tmp_adev, false);
6278
6279 cancel_delayed_work_sync(&tmp_adev->delayed_init_work);
6280
6281 amdgpu_amdkfd_pre_reset(tmp_adev, reset_context);
6282
6283 /*
6284 * Mark these ASICs to be reset as untracked first
6285 * And add them back after reset completed
6286 */
6287 amdgpu_unregister_gpu_instance(tmp_adev);
6288
6289 drm_client_dev_suspend(adev_to_drm(tmp_adev), false);
6290
6291 /* disable ras on ALL IPs */
6292 if (!need_emergency_restart && !amdgpu_reset_in_dpc(adev) &&
6293 amdgpu_device_ip_need_full_reset(tmp_adev))
6294 amdgpu_ras_suspend(tmp_adev);
6295
6296 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
6297 struct amdgpu_ring *ring = tmp_adev->rings[i];
6298
6299 if (!amdgpu_ring_sched_ready(ring))
6300 continue;
6301
6302 drm_sched_stop(&ring->sched, job ? &job->base : NULL);
6303
6304 if (need_emergency_restart)
6305 amdgpu_job_stop_all_jobs_on_sched(&ring->sched);
6306 }
6307 atomic_inc(&tmp_adev->gpu_reset_counter);
6308 }
6309 }
6310
amdgpu_device_asic_reset(struct amdgpu_device * adev,struct list_head * device_list,struct amdgpu_reset_context * reset_context)6311 static int amdgpu_device_asic_reset(struct amdgpu_device *adev,
6312 struct list_head *device_list,
6313 struct amdgpu_reset_context *reset_context)
6314 {
6315 struct amdgpu_device *tmp_adev = NULL;
6316 int retry_limit = AMDGPU_MAX_RETRY_LIMIT;
6317 int r = 0;
6318
6319 retry: /* Rest of adevs pre asic reset from XGMI hive. */
6320 list_for_each_entry(tmp_adev, device_list, reset_list) {
6321 r = amdgpu_device_pre_asic_reset(tmp_adev, reset_context);
6322 /*TODO Should we stop ?*/
6323 if (r) {
6324 dev_err(tmp_adev->dev, "GPU pre asic reset failed with err, %d for drm dev, %s ",
6325 r, adev_to_drm(tmp_adev)->unique);
6326 tmp_adev->asic_reset_res = r;
6327 }
6328 }
6329
6330 /* Actual ASIC resets if needed.*/
6331 /* Host driver will handle XGMI hive reset for SRIOV */
6332 if (amdgpu_sriov_vf(adev)) {
6333
6334 /* Bail out of reset early */
6335 if (amdgpu_ras_is_rma(adev))
6336 return -ENODEV;
6337
6338 if (amdgpu_ras_get_fed_status(adev) || amdgpu_virt_rcvd_ras_interrupt(adev)) {
6339 dev_dbg(adev->dev, "Detected RAS error, wait for FLR completion\n");
6340 amdgpu_ras_set_fed(adev, true);
6341 set_bit(AMDGPU_HOST_FLR, &reset_context->flags);
6342 }
6343
6344 r = amdgpu_device_reset_sriov(adev, reset_context);
6345 if (AMDGPU_RETRY_SRIOV_RESET(r) && (retry_limit--) > 0) {
6346 amdgpu_virt_release_full_gpu(adev, true);
6347 goto retry;
6348 }
6349 if (r)
6350 adev->asic_reset_res = r;
6351 } else {
6352 r = amdgpu_do_asic_reset(device_list, reset_context);
6353 if (r && r == -EAGAIN)
6354 goto retry;
6355 }
6356
6357 list_for_each_entry(tmp_adev, device_list, reset_list) {
6358 /*
6359 * Drop any pending non scheduler resets queued before reset is done.
6360 * Any reset scheduled after this point would be valid. Scheduler resets
6361 * were already dropped during drm_sched_stop and no new ones can come
6362 * in before drm_sched_start.
6363 */
6364 amdgpu_device_stop_pending_resets(tmp_adev);
6365 }
6366
6367 return r;
6368 }
6369
amdgpu_device_sched_resume(struct list_head * device_list,struct amdgpu_reset_context * reset_context,bool job_signaled)6370 static int amdgpu_device_sched_resume(struct list_head *device_list,
6371 struct amdgpu_reset_context *reset_context,
6372 bool job_signaled)
6373 {
6374 struct amdgpu_device *tmp_adev = NULL;
6375 int i, r = 0;
6376
6377 /* Post ASIC reset for all devs .*/
6378 list_for_each_entry(tmp_adev, device_list, reset_list) {
6379
6380 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
6381 struct amdgpu_ring *ring = tmp_adev->rings[i];
6382
6383 if (!amdgpu_ring_sched_ready(ring))
6384 continue;
6385
6386 drm_sched_start(&ring->sched, 0);
6387 }
6388
6389 if (!drm_drv_uses_atomic_modeset(adev_to_drm(tmp_adev)) && !job_signaled)
6390 drm_helper_resume_force_mode(adev_to_drm(tmp_adev));
6391
6392 if (tmp_adev->asic_reset_res)
6393 r = tmp_adev->asic_reset_res;
6394
6395 tmp_adev->asic_reset_res = 0;
6396
6397 if (r) {
6398 /* bad news, how to tell it to userspace ?
6399 * for ras error, we should report GPU bad status instead of
6400 * reset failure
6401 */
6402 if (reset_context->src != AMDGPU_RESET_SRC_RAS ||
6403 !amdgpu_ras_eeprom_check_err_threshold(tmp_adev))
6404 dev_info(tmp_adev->dev, "GPU reset(%d) failed\n",
6405 atomic_read(&tmp_adev->gpu_reset_counter));
6406 amdgpu_vf_error_put(tmp_adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
6407 } else {
6408 dev_info(tmp_adev->dev, "GPU reset(%d) succeeded!\n", atomic_read(&tmp_adev->gpu_reset_counter));
6409 if (amdgpu_acpi_smart_shift_update(tmp_adev,
6410 AMDGPU_SS_DEV_D0))
6411 dev_warn(tmp_adev->dev,
6412 "smart shift update failed\n");
6413 }
6414 }
6415
6416 return r;
6417 }
6418
amdgpu_device_gpu_resume(struct amdgpu_device * adev,struct list_head * device_list,bool need_emergency_restart)6419 static void amdgpu_device_gpu_resume(struct amdgpu_device *adev,
6420 struct list_head *device_list,
6421 bool need_emergency_restart)
6422 {
6423 struct amdgpu_device *tmp_adev = NULL;
6424
6425 list_for_each_entry(tmp_adev, device_list, reset_list) {
6426 /* unlock kfd: SRIOV would do it separately */
6427 if (!need_emergency_restart && !amdgpu_sriov_vf(tmp_adev))
6428 amdgpu_amdkfd_post_reset(tmp_adev);
6429
6430 /* kfd_post_reset will do nothing if kfd device is not initialized,
6431 * need to bring up kfd here if it's not be initialized before
6432 */
6433 if (!adev->kfd.init_complete)
6434 amdgpu_amdkfd_device_init(adev);
6435
6436 if (tmp_adev->pcie_reset_ctx.audio_suspended)
6437 amdgpu_device_resume_display_audio(tmp_adev);
6438
6439 amdgpu_device_unset_mp1_state(tmp_adev);
6440
6441 amdgpu_ras_set_error_query_ready(tmp_adev, true);
6442
6443 }
6444 }
6445
6446
6447 /**
6448 * amdgpu_device_gpu_recover - reset the asic and recover scheduler
6449 *
6450 * @adev: amdgpu_device pointer
6451 * @job: which job trigger hang
6452 * @reset_context: amdgpu reset context pointer
6453 *
6454 * Attempt to reset the GPU if it has hung (all asics).
6455 * Attempt to do soft-reset or full-reset and reinitialize Asic
6456 * Returns 0 for success or an error on failure.
6457 */
6458
amdgpu_device_gpu_recover(struct amdgpu_device * adev,struct amdgpu_job * job,struct amdgpu_reset_context * reset_context)6459 int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
6460 struct amdgpu_job *job,
6461 struct amdgpu_reset_context *reset_context)
6462 {
6463 struct list_head device_list;
6464 bool job_signaled = false;
6465 struct amdgpu_hive_info *hive = NULL;
6466 int r = 0;
6467 bool need_emergency_restart = false;
6468
6469 /*
6470 * If it reaches here because of hang/timeout and a RAS error is
6471 * detected at the same time, let RAS recovery take care of it.
6472 */
6473 if (amdgpu_ras_is_err_state(adev, AMDGPU_RAS_BLOCK__ANY) &&
6474 !amdgpu_sriov_vf(adev) &&
6475 reset_context->src != AMDGPU_RESET_SRC_RAS) {
6476 dev_dbg(adev->dev,
6477 "Gpu recovery from source: %d yielding to RAS error recovery handling",
6478 reset_context->src);
6479 return 0;
6480 }
6481
6482 /*
6483 * Special case: RAS triggered and full reset isn't supported
6484 */
6485 need_emergency_restart = amdgpu_ras_need_emergency_restart(adev);
6486
6487 /*
6488 * Flush RAM to disk so that after reboot
6489 * the user can read log and see why the system rebooted.
6490 */
6491 if (need_emergency_restart && amdgpu_ras_get_context(adev) &&
6492 amdgpu_ras_get_context(adev)->reboot) {
6493 dev_warn(adev->dev, "Emergency reboot.");
6494
6495 ksys_sync_helper();
6496 emergency_restart();
6497 }
6498
6499 dev_info(adev->dev, "GPU %s begin!. Source: %d\n",
6500 need_emergency_restart ? "jobs stop" : "reset",
6501 reset_context->src);
6502
6503 if (!amdgpu_sriov_vf(adev))
6504 hive = amdgpu_get_xgmi_hive(adev);
6505 if (hive)
6506 mutex_lock(&hive->hive_lock);
6507
6508 reset_context->job = job;
6509 reset_context->hive = hive;
6510 INIT_LIST_HEAD(&device_list);
6511
6512 amdgpu_device_recovery_prepare(adev, &device_list, hive);
6513
6514 if (!amdgpu_sriov_vf(adev)) {
6515 r = amdgpu_device_health_check(&device_list);
6516 if (r)
6517 goto end_reset;
6518 }
6519
6520 /* We need to lock reset domain only once both for XGMI and single device */
6521 amdgpu_device_recovery_get_reset_lock(adev, &device_list);
6522
6523 amdgpu_device_halt_activities(adev, job, reset_context, &device_list,
6524 hive, need_emergency_restart);
6525 if (need_emergency_restart)
6526 goto skip_sched_resume;
6527 /*
6528 * Must check guilty signal here since after this point all old
6529 * HW fences are force signaled.
6530 *
6531 * job->base holds a reference to parent fence
6532 */
6533 if (job && dma_fence_is_signaled(&job->hw_fence.base)) {
6534 job_signaled = true;
6535 dev_info(adev->dev, "Guilty job already signaled, skipping HW reset");
6536 goto skip_hw_reset;
6537 }
6538
6539 r = amdgpu_device_asic_reset(adev, &device_list, reset_context);
6540 if (r)
6541 goto reset_unlock;
6542 skip_hw_reset:
6543 r = amdgpu_device_sched_resume(&device_list, reset_context, job_signaled);
6544 if (r)
6545 goto reset_unlock;
6546 skip_sched_resume:
6547 amdgpu_device_gpu_resume(adev, &device_list, need_emergency_restart);
6548 reset_unlock:
6549 amdgpu_device_recovery_put_reset_lock(adev, &device_list);
6550 end_reset:
6551 if (hive) {
6552 mutex_unlock(&hive->hive_lock);
6553 amdgpu_put_xgmi_hive(hive);
6554 }
6555
6556 if (r)
6557 dev_info(adev->dev, "GPU reset end with ret = %d\n", r);
6558
6559 atomic_set(&adev->reset_domain->reset_res, r);
6560
6561 if (!r) {
6562 struct amdgpu_task_info *ti = NULL;
6563
6564 if (job)
6565 ti = amdgpu_vm_get_task_info_pasid(adev, job->pasid);
6566
6567 drm_dev_wedged_event(adev_to_drm(adev), DRM_WEDGE_RECOVERY_NONE,
6568 ti ? &ti->task : NULL);
6569
6570 amdgpu_vm_put_task_info(ti);
6571 }
6572
6573 return r;
6574 }
6575
6576 /**
6577 * amdgpu_device_partner_bandwidth - find the bandwidth of appropriate partner
6578 *
6579 * @adev: amdgpu_device pointer
6580 * @speed: pointer to the speed of the link
6581 * @width: pointer to the width of the link
6582 *
6583 * Evaluate the hierarchy to find the speed and bandwidth capabilities of the
6584 * first physical partner to an AMD dGPU.
6585 * This will exclude any virtual switches and links.
6586 */
amdgpu_device_partner_bandwidth(struct amdgpu_device * adev,enum pci_bus_speed * speed,enum pcie_link_width * width)6587 static void amdgpu_device_partner_bandwidth(struct amdgpu_device *adev,
6588 enum pci_bus_speed *speed,
6589 enum pcie_link_width *width)
6590 {
6591 struct pci_dev *parent = adev->pdev;
6592
6593 if (!speed || !width)
6594 return;
6595
6596 *speed = PCI_SPEED_UNKNOWN;
6597 *width = PCIE_LNK_WIDTH_UNKNOWN;
6598
6599 if (amdgpu_device_pcie_dynamic_switching_supported(adev)) {
6600 while ((parent = pci_upstream_bridge(parent))) {
6601 /* skip upstream/downstream switches internal to dGPU*/
6602 if (parent->vendor == PCI_VENDOR_ID_ATI)
6603 continue;
6604 *speed = pcie_get_speed_cap(parent);
6605 *width = pcie_get_width_cap(parent);
6606 break;
6607 }
6608 } else {
6609 /* use the current speeds rather than max if switching is not supported */
6610 pcie_bandwidth_available(adev->pdev, NULL, speed, width);
6611 }
6612 }
6613
6614 /**
6615 * amdgpu_device_gpu_bandwidth - find the bandwidth of the GPU
6616 *
6617 * @adev: amdgpu_device pointer
6618 * @speed: pointer to the speed of the link
6619 * @width: pointer to the width of the link
6620 *
6621 * Evaluate the hierarchy to find the speed and bandwidth capabilities of the
6622 * AMD dGPU which may be a virtual upstream bridge.
6623 */
amdgpu_device_gpu_bandwidth(struct amdgpu_device * adev,enum pci_bus_speed * speed,enum pcie_link_width * width)6624 static void amdgpu_device_gpu_bandwidth(struct amdgpu_device *adev,
6625 enum pci_bus_speed *speed,
6626 enum pcie_link_width *width)
6627 {
6628 struct pci_dev *parent = adev->pdev;
6629
6630 if (!speed || !width)
6631 return;
6632
6633 parent = pci_upstream_bridge(parent);
6634 if (parent && parent->vendor == PCI_VENDOR_ID_ATI) {
6635 /* use the upstream/downstream switches internal to dGPU */
6636 *speed = pcie_get_speed_cap(parent);
6637 *width = pcie_get_width_cap(parent);
6638 while ((parent = pci_upstream_bridge(parent))) {
6639 if (parent->vendor == PCI_VENDOR_ID_ATI) {
6640 /* use the upstream/downstream switches internal to dGPU */
6641 *speed = pcie_get_speed_cap(parent);
6642 *width = pcie_get_width_cap(parent);
6643 }
6644 }
6645 } else {
6646 /* use the device itself */
6647 *speed = pcie_get_speed_cap(adev->pdev);
6648 *width = pcie_get_width_cap(adev->pdev);
6649 }
6650 }
6651
6652 /**
6653 * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
6654 *
6655 * @adev: amdgpu_device pointer
6656 *
6657 * Fetches and stores in the driver the PCIE capabilities (gen speed
6658 * and lanes) of the slot the device is in. Handles APUs and
6659 * virtualized environments where PCIE config space may not be available.
6660 */
amdgpu_device_get_pcie_info(struct amdgpu_device * adev)6661 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
6662 {
6663 enum pci_bus_speed speed_cap, platform_speed_cap;
6664 enum pcie_link_width platform_link_width, link_width;
6665
6666 if (amdgpu_pcie_gen_cap)
6667 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
6668
6669 if (amdgpu_pcie_lane_cap)
6670 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
6671
6672 /* covers APUs as well */
6673 if (pci_is_root_bus(adev->pdev->bus) && !amdgpu_passthrough(adev)) {
6674 if (adev->pm.pcie_gen_mask == 0)
6675 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
6676 if (adev->pm.pcie_mlw_mask == 0)
6677 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
6678 return;
6679 }
6680
6681 if (adev->pm.pcie_gen_mask && adev->pm.pcie_mlw_mask)
6682 return;
6683
6684 amdgpu_device_partner_bandwidth(adev, &platform_speed_cap,
6685 &platform_link_width);
6686 amdgpu_device_gpu_bandwidth(adev, &speed_cap, &link_width);
6687
6688 if (adev->pm.pcie_gen_mask == 0) {
6689 /* asic caps */
6690 if (speed_cap == PCI_SPEED_UNKNOWN) {
6691 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
6692 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
6693 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
6694 } else {
6695 if (speed_cap == PCIE_SPEED_32_0GT)
6696 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
6697 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
6698 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
6699 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4 |
6700 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN5);
6701 else if (speed_cap == PCIE_SPEED_16_0GT)
6702 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
6703 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
6704 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
6705 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4);
6706 else if (speed_cap == PCIE_SPEED_8_0GT)
6707 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
6708 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
6709 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
6710 else if (speed_cap == PCIE_SPEED_5_0GT)
6711 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
6712 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2);
6713 else
6714 adev->pm.pcie_gen_mask |= CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1;
6715 }
6716 /* platform caps */
6717 if (platform_speed_cap == PCI_SPEED_UNKNOWN) {
6718 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
6719 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
6720 } else {
6721 if (platform_speed_cap == PCIE_SPEED_32_0GT)
6722 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
6723 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
6724 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
6725 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4 |
6726 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN5);
6727 else if (platform_speed_cap == PCIE_SPEED_16_0GT)
6728 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
6729 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
6730 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
6731 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4);
6732 else if (platform_speed_cap == PCIE_SPEED_8_0GT)
6733 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
6734 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
6735 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3);
6736 else if (platform_speed_cap == PCIE_SPEED_5_0GT)
6737 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
6738 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
6739 else
6740 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
6741
6742 }
6743 }
6744 if (adev->pm.pcie_mlw_mask == 0) {
6745 /* asic caps */
6746 if (link_width == PCIE_LNK_WIDTH_UNKNOWN) {
6747 adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_ASIC_PCIE_MLW_MASK;
6748 } else {
6749 switch (link_width) {
6750 case PCIE_LNK_X32:
6751 adev->pm.pcie_mlw_mask |= (CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X32 |
6752 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X16 |
6753 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X12 |
6754 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X8 |
6755 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X4 |
6756 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X2 |
6757 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X1);
6758 break;
6759 case PCIE_LNK_X16:
6760 adev->pm.pcie_mlw_mask |= (CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X16 |
6761 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X12 |
6762 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X8 |
6763 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X4 |
6764 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X2 |
6765 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X1);
6766 break;
6767 case PCIE_LNK_X12:
6768 adev->pm.pcie_mlw_mask |= (CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X12 |
6769 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X8 |
6770 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X4 |
6771 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X2 |
6772 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X1);
6773 break;
6774 case PCIE_LNK_X8:
6775 adev->pm.pcie_mlw_mask |= (CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X8 |
6776 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X4 |
6777 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X2 |
6778 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X1);
6779 break;
6780 case PCIE_LNK_X4:
6781 adev->pm.pcie_mlw_mask |= (CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X4 |
6782 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X2 |
6783 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X1);
6784 break;
6785 case PCIE_LNK_X2:
6786 adev->pm.pcie_mlw_mask |= (CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X2 |
6787 CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X1);
6788 break;
6789 case PCIE_LNK_X1:
6790 adev->pm.pcie_mlw_mask |= CAIL_ASIC_PCIE_LINK_WIDTH_SUPPORT_X1;
6791 break;
6792 default:
6793 break;
6794 }
6795 }
6796 /* platform caps */
6797 if (platform_link_width == PCIE_LNK_WIDTH_UNKNOWN) {
6798 adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_PCIE_MLW_MASK;
6799 } else {
6800 switch (platform_link_width) {
6801 case PCIE_LNK_X32:
6802 adev->pm.pcie_mlw_mask |= (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
6803 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
6804 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
6805 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
6806 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
6807 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
6808 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
6809 break;
6810 case PCIE_LNK_X16:
6811 adev->pm.pcie_mlw_mask |= (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
6812 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
6813 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
6814 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
6815 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
6816 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
6817 break;
6818 case PCIE_LNK_X12:
6819 adev->pm.pcie_mlw_mask |= (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
6820 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
6821 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
6822 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
6823 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
6824 break;
6825 case PCIE_LNK_X8:
6826 adev->pm.pcie_mlw_mask |= (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
6827 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
6828 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
6829 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
6830 break;
6831 case PCIE_LNK_X4:
6832 adev->pm.pcie_mlw_mask |= (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
6833 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
6834 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
6835 break;
6836 case PCIE_LNK_X2:
6837 adev->pm.pcie_mlw_mask |= (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
6838 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
6839 break;
6840 case PCIE_LNK_X1:
6841 adev->pm.pcie_mlw_mask |= CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
6842 break;
6843 default:
6844 break;
6845 }
6846 }
6847 }
6848 }
6849
6850 /**
6851 * amdgpu_device_is_peer_accessible - Check peer access through PCIe BAR
6852 *
6853 * @adev: amdgpu_device pointer
6854 * @peer_adev: amdgpu_device pointer for peer device trying to access @adev
6855 *
6856 * Return true if @peer_adev can access (DMA) @adev through the PCIe
6857 * BAR, i.e. @adev is "large BAR" and the BAR matches the DMA mask of
6858 * @peer_adev.
6859 */
amdgpu_device_is_peer_accessible(struct amdgpu_device * adev,struct amdgpu_device * peer_adev)6860 bool amdgpu_device_is_peer_accessible(struct amdgpu_device *adev,
6861 struct amdgpu_device *peer_adev)
6862 {
6863 #ifdef CONFIG_HSA_AMD_P2P
6864 bool p2p_access =
6865 !adev->gmc.xgmi.connected_to_cpu &&
6866 !(pci_p2pdma_distance(adev->pdev, peer_adev->dev, false) < 0);
6867 if (!p2p_access)
6868 dev_info(adev->dev, "PCIe P2P access from peer device %s is not supported by the chipset\n",
6869 pci_name(peer_adev->pdev));
6870
6871 bool is_large_bar = adev->gmc.visible_vram_size &&
6872 adev->gmc.real_vram_size == adev->gmc.visible_vram_size;
6873 bool p2p_addressable = amdgpu_device_check_iommu_remap(peer_adev);
6874
6875 if (!p2p_addressable) {
6876 uint64_t address_mask = peer_adev->dev->dma_mask ?
6877 ~*peer_adev->dev->dma_mask : ~((1ULL << 32) - 1);
6878 resource_size_t aper_limit =
6879 adev->gmc.aper_base + adev->gmc.aper_size - 1;
6880
6881 p2p_addressable = !(adev->gmc.aper_base & address_mask ||
6882 aper_limit & address_mask);
6883 }
6884 return pcie_p2p && is_large_bar && p2p_access && p2p_addressable;
6885 #else
6886 return false;
6887 #endif
6888 }
6889
amdgpu_device_baco_enter(struct amdgpu_device * adev)6890 int amdgpu_device_baco_enter(struct amdgpu_device *adev)
6891 {
6892 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
6893
6894 if (!amdgpu_device_supports_baco(adev))
6895 return -ENOTSUPP;
6896
6897 if (ras && adev->ras_enabled &&
6898 adev->nbio.funcs->enable_doorbell_interrupt)
6899 adev->nbio.funcs->enable_doorbell_interrupt(adev, false);
6900
6901 return amdgpu_dpm_baco_enter(adev);
6902 }
6903
amdgpu_device_baco_exit(struct amdgpu_device * adev)6904 int amdgpu_device_baco_exit(struct amdgpu_device *adev)
6905 {
6906 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
6907 int ret = 0;
6908
6909 if (!amdgpu_device_supports_baco(adev))
6910 return -ENOTSUPP;
6911
6912 ret = amdgpu_dpm_baco_exit(adev);
6913 if (ret)
6914 return ret;
6915
6916 if (ras && adev->ras_enabled &&
6917 adev->nbio.funcs->enable_doorbell_interrupt)
6918 adev->nbio.funcs->enable_doorbell_interrupt(adev, true);
6919
6920 if (amdgpu_passthrough(adev) && adev->nbio.funcs &&
6921 adev->nbio.funcs->clear_doorbell_interrupt)
6922 adev->nbio.funcs->clear_doorbell_interrupt(adev);
6923
6924 return 0;
6925 }
6926
6927 /**
6928 * amdgpu_pci_error_detected - Called when a PCI error is detected.
6929 * @pdev: PCI device struct
6930 * @state: PCI channel state
6931 *
6932 * Description: Called when a PCI error is detected.
6933 *
6934 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT.
6935 */
amdgpu_pci_error_detected(struct pci_dev * pdev,pci_channel_state_t state)6936 pci_ers_result_t amdgpu_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
6937 {
6938 struct drm_device *dev = pci_get_drvdata(pdev);
6939 struct amdgpu_device *adev = drm_to_adev(dev);
6940 struct amdgpu_hive_info *hive __free(xgmi_put_hive) =
6941 amdgpu_get_xgmi_hive(adev);
6942 struct amdgpu_reset_context reset_context;
6943 struct list_head device_list;
6944
6945 dev_info(adev->dev, "PCI error: detected callback!!\n");
6946
6947 adev->pci_channel_state = state;
6948
6949 switch (state) {
6950 case pci_channel_io_normal:
6951 dev_info(adev->dev, "pci_channel_io_normal: state(%d)!!\n", state);
6952 return PCI_ERS_RESULT_CAN_RECOVER;
6953 case pci_channel_io_frozen:
6954 /* Fatal error, prepare for slot reset */
6955 dev_info(adev->dev, "pci_channel_io_frozen: state(%d)!!\n", state);
6956 if (hive) {
6957 /* Hive devices should be able to support FW based
6958 * link reset on other devices, if not return.
6959 */
6960 if (!amdgpu_dpm_is_link_reset_supported(adev)) {
6961 dev_warn(adev->dev,
6962 "No support for XGMI hive yet...\n");
6963 return PCI_ERS_RESULT_DISCONNECT;
6964 }
6965 /* Set dpc status only if device is part of hive
6966 * Non-hive devices should be able to recover after
6967 * link reset.
6968 */
6969 amdgpu_reset_set_dpc_status(adev, true);
6970
6971 mutex_lock(&hive->hive_lock);
6972 }
6973 memset(&reset_context, 0, sizeof(reset_context));
6974 INIT_LIST_HEAD(&device_list);
6975
6976 amdgpu_device_recovery_prepare(adev, &device_list, hive);
6977 amdgpu_device_recovery_get_reset_lock(adev, &device_list);
6978 amdgpu_device_halt_activities(adev, NULL, &reset_context, &device_list,
6979 hive, false);
6980 if (hive)
6981 mutex_unlock(&hive->hive_lock);
6982 return PCI_ERS_RESULT_NEED_RESET;
6983 case pci_channel_io_perm_failure:
6984 /* Permanent error, prepare for device removal */
6985 dev_info(adev->dev, "pci_channel_io_perm_failure: state(%d)!!\n", state);
6986 return PCI_ERS_RESULT_DISCONNECT;
6987 }
6988
6989 return PCI_ERS_RESULT_NEED_RESET;
6990 }
6991
6992 /**
6993 * amdgpu_pci_mmio_enabled - Enable MMIO and dump debug registers
6994 * @pdev: pointer to PCI device
6995 */
amdgpu_pci_mmio_enabled(struct pci_dev * pdev)6996 pci_ers_result_t amdgpu_pci_mmio_enabled(struct pci_dev *pdev)
6997 {
6998 struct drm_device *dev = pci_get_drvdata(pdev);
6999 struct amdgpu_device *adev = drm_to_adev(dev);
7000
7001 dev_info(adev->dev, "PCI error: mmio enabled callback!!\n");
7002
7003 /* TODO - dump whatever for debugging purposes */
7004
7005 /* This called only if amdgpu_pci_error_detected returns
7006 * PCI_ERS_RESULT_CAN_RECOVER. Read/write to the device still
7007 * works, no need to reset slot.
7008 */
7009
7010 return PCI_ERS_RESULT_RECOVERED;
7011 }
7012
7013 /**
7014 * amdgpu_pci_slot_reset - Called when PCI slot has been reset.
7015 * @pdev: PCI device struct
7016 *
7017 * Description: This routine is called by the pci error recovery
7018 * code after the PCI slot has been reset, just before we
7019 * should resume normal operations.
7020 */
amdgpu_pci_slot_reset(struct pci_dev * pdev)7021 pci_ers_result_t amdgpu_pci_slot_reset(struct pci_dev *pdev)
7022 {
7023 struct drm_device *dev = pci_get_drvdata(pdev);
7024 struct amdgpu_device *adev = drm_to_adev(dev);
7025 struct amdgpu_reset_context reset_context;
7026 struct amdgpu_device *tmp_adev;
7027 struct amdgpu_hive_info *hive;
7028 struct list_head device_list;
7029 struct pci_dev *link_dev;
7030 int r = 0, i, timeout;
7031 u32 memsize;
7032 u16 status;
7033
7034 dev_info(adev->dev, "PCI error: slot reset callback!!\n");
7035
7036 memset(&reset_context, 0, sizeof(reset_context));
7037
7038 if (adev->pcie_reset_ctx.swus)
7039 link_dev = adev->pcie_reset_ctx.swus;
7040 else
7041 link_dev = adev->pdev;
7042 /* wait for asic to come out of reset, timeout = 10s */
7043 timeout = 10000;
7044 do {
7045 usleep_range(10000, 10500);
7046 r = pci_read_config_word(link_dev, PCI_VENDOR_ID, &status);
7047 timeout -= 10;
7048 } while (timeout > 0 && (status != PCI_VENDOR_ID_ATI) &&
7049 (status != PCI_VENDOR_ID_AMD));
7050
7051 if ((status != PCI_VENDOR_ID_ATI) && (status != PCI_VENDOR_ID_AMD)) {
7052 r = -ETIME;
7053 goto out;
7054 }
7055
7056 amdgpu_device_load_switch_state(adev);
7057 /* Restore PCI confspace */
7058 amdgpu_device_load_pci_state(pdev);
7059
7060 /* confirm ASIC came out of reset */
7061 for (i = 0; i < adev->usec_timeout; i++) {
7062 memsize = amdgpu_asic_get_config_memsize(adev);
7063
7064 if (memsize != 0xffffffff)
7065 break;
7066 udelay(1);
7067 }
7068 if (memsize == 0xffffffff) {
7069 r = -ETIME;
7070 goto out;
7071 }
7072
7073 reset_context.method = AMD_RESET_METHOD_NONE;
7074 reset_context.reset_req_dev = adev;
7075 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
7076 set_bit(AMDGPU_SKIP_COREDUMP, &reset_context.flags);
7077 INIT_LIST_HEAD(&device_list);
7078
7079 hive = amdgpu_get_xgmi_hive(adev);
7080 if (hive) {
7081 mutex_lock(&hive->hive_lock);
7082 reset_context.hive = hive;
7083 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
7084 tmp_adev->pcie_reset_ctx.in_link_reset = true;
7085 list_add_tail(&tmp_adev->reset_list, &device_list);
7086 }
7087 } else {
7088 set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
7089 list_add_tail(&adev->reset_list, &device_list);
7090 }
7091
7092 r = amdgpu_device_asic_reset(adev, &device_list, &reset_context);
7093 out:
7094 if (!r) {
7095 if (amdgpu_device_cache_pci_state(adev->pdev))
7096 pci_restore_state(adev->pdev);
7097 dev_info(adev->dev, "PCIe error recovery succeeded\n");
7098 } else {
7099 dev_err(adev->dev, "PCIe error recovery failed, err:%d\n", r);
7100 if (hive) {
7101 list_for_each_entry(tmp_adev, &device_list, reset_list)
7102 amdgpu_device_unset_mp1_state(tmp_adev);
7103 }
7104 amdgpu_device_recovery_put_reset_lock(adev, &device_list);
7105 }
7106
7107 if (hive) {
7108 mutex_unlock(&hive->hive_lock);
7109 amdgpu_put_xgmi_hive(hive);
7110 }
7111
7112 return r ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
7113 }
7114
7115 /**
7116 * amdgpu_pci_resume() - resume normal ops after PCI reset
7117 * @pdev: pointer to PCI device
7118 *
7119 * Called when the error recovery driver tells us that its
7120 * OK to resume normal operation.
7121 */
amdgpu_pci_resume(struct pci_dev * pdev)7122 void amdgpu_pci_resume(struct pci_dev *pdev)
7123 {
7124 struct drm_device *dev = pci_get_drvdata(pdev);
7125 struct amdgpu_device *adev = drm_to_adev(dev);
7126 struct list_head device_list;
7127 struct amdgpu_hive_info *hive = NULL;
7128 struct amdgpu_device *tmp_adev = NULL;
7129
7130 dev_info(adev->dev, "PCI error: resume callback!!\n");
7131
7132 /* Only continue execution for the case of pci_channel_io_frozen */
7133 if (adev->pci_channel_state != pci_channel_io_frozen)
7134 return;
7135
7136 INIT_LIST_HEAD(&device_list);
7137
7138 hive = amdgpu_get_xgmi_hive(adev);
7139 if (hive) {
7140 mutex_lock(&hive->hive_lock);
7141 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
7142 tmp_adev->pcie_reset_ctx.in_link_reset = false;
7143 list_add_tail(&tmp_adev->reset_list, &device_list);
7144 }
7145 } else
7146 list_add_tail(&adev->reset_list, &device_list);
7147
7148 amdgpu_device_sched_resume(&device_list, NULL, NULL);
7149 amdgpu_device_gpu_resume(adev, &device_list, false);
7150 amdgpu_device_recovery_put_reset_lock(adev, &device_list);
7151
7152 if (hive) {
7153 mutex_unlock(&hive->hive_lock);
7154 amdgpu_put_xgmi_hive(hive);
7155 }
7156 }
7157
amdgpu_device_cache_switch_state(struct amdgpu_device * adev)7158 static void amdgpu_device_cache_switch_state(struct amdgpu_device *adev)
7159 {
7160 struct pci_dev *parent = pci_upstream_bridge(adev->pdev);
7161 int r;
7162
7163 if (!parent || parent->vendor != PCI_VENDOR_ID_ATI)
7164 return;
7165
7166 /* If already saved, return */
7167 if (adev->pcie_reset_ctx.swus)
7168 return;
7169 /* Upstream bridge is ATI, assume it's SWUS/DS architecture */
7170 r = pci_save_state(parent);
7171 if (r)
7172 return;
7173 adev->pcie_reset_ctx.swds_pcistate = pci_store_saved_state(parent);
7174
7175 parent = pci_upstream_bridge(parent);
7176 r = pci_save_state(parent);
7177 if (r)
7178 return;
7179 adev->pcie_reset_ctx.swus_pcistate = pci_store_saved_state(parent);
7180
7181 adev->pcie_reset_ctx.swus = parent;
7182 }
7183
amdgpu_device_load_switch_state(struct amdgpu_device * adev)7184 static void amdgpu_device_load_switch_state(struct amdgpu_device *adev)
7185 {
7186 struct pci_dev *pdev;
7187 int r;
7188
7189 if (!adev->pcie_reset_ctx.swds_pcistate ||
7190 !adev->pcie_reset_ctx.swus_pcistate)
7191 return;
7192
7193 pdev = adev->pcie_reset_ctx.swus;
7194 r = pci_load_saved_state(pdev, adev->pcie_reset_ctx.swus_pcistate);
7195 if (!r) {
7196 pci_restore_state(pdev);
7197 } else {
7198 dev_warn(adev->dev, "Failed to load SWUS state, err:%d\n", r);
7199 return;
7200 }
7201
7202 pdev = pci_upstream_bridge(adev->pdev);
7203 r = pci_load_saved_state(pdev, adev->pcie_reset_ctx.swds_pcistate);
7204 if (!r)
7205 pci_restore_state(pdev);
7206 else
7207 dev_warn(adev->dev, "Failed to load SWDS state, err:%d\n", r);
7208 }
7209
amdgpu_device_cache_pci_state(struct pci_dev * pdev)7210 bool amdgpu_device_cache_pci_state(struct pci_dev *pdev)
7211 {
7212 struct drm_device *dev = pci_get_drvdata(pdev);
7213 struct amdgpu_device *adev = drm_to_adev(dev);
7214 int r;
7215
7216 if (amdgpu_sriov_vf(adev))
7217 return false;
7218
7219 r = pci_save_state(pdev);
7220 if (!r) {
7221 kfree(adev->pci_state);
7222
7223 adev->pci_state = pci_store_saved_state(pdev);
7224
7225 if (!adev->pci_state) {
7226 dev_err(adev->dev, "Failed to store PCI saved state");
7227 return false;
7228 }
7229 } else {
7230 dev_warn(adev->dev, "Failed to save PCI state, err:%d\n", r);
7231 return false;
7232 }
7233
7234 amdgpu_device_cache_switch_state(adev);
7235
7236 return true;
7237 }
7238
amdgpu_device_load_pci_state(struct pci_dev * pdev)7239 bool amdgpu_device_load_pci_state(struct pci_dev *pdev)
7240 {
7241 struct drm_device *dev = pci_get_drvdata(pdev);
7242 struct amdgpu_device *adev = drm_to_adev(dev);
7243 int r;
7244
7245 if (!adev->pci_state)
7246 return false;
7247
7248 r = pci_load_saved_state(pdev, adev->pci_state);
7249
7250 if (!r) {
7251 pci_restore_state(pdev);
7252 } else {
7253 dev_warn(adev->dev, "Failed to load PCI state, err:%d\n", r);
7254 return false;
7255 }
7256
7257 return true;
7258 }
7259
amdgpu_device_flush_hdp(struct amdgpu_device * adev,struct amdgpu_ring * ring)7260 void amdgpu_device_flush_hdp(struct amdgpu_device *adev,
7261 struct amdgpu_ring *ring)
7262 {
7263 #ifdef CONFIG_X86_64
7264 if ((adev->flags & AMD_IS_APU) && !amdgpu_passthrough(adev))
7265 return;
7266 #endif
7267 if (adev->gmc.xgmi.connected_to_cpu)
7268 return;
7269
7270 if (ring && ring->funcs->emit_hdp_flush)
7271 amdgpu_ring_emit_hdp_flush(ring);
7272 else
7273 amdgpu_asic_flush_hdp(adev, ring);
7274 }
7275
amdgpu_device_invalidate_hdp(struct amdgpu_device * adev,struct amdgpu_ring * ring)7276 void amdgpu_device_invalidate_hdp(struct amdgpu_device *adev,
7277 struct amdgpu_ring *ring)
7278 {
7279 #ifdef CONFIG_X86_64
7280 if ((adev->flags & AMD_IS_APU) && !amdgpu_passthrough(adev))
7281 return;
7282 #endif
7283 if (adev->gmc.xgmi.connected_to_cpu)
7284 return;
7285
7286 amdgpu_asic_invalidate_hdp(adev, ring);
7287 }
7288
amdgpu_in_reset(struct amdgpu_device * adev)7289 int amdgpu_in_reset(struct amdgpu_device *adev)
7290 {
7291 return atomic_read(&adev->reset_domain->in_gpu_reset);
7292 }
7293
7294 /**
7295 * amdgpu_device_halt() - bring hardware to some kind of halt state
7296 *
7297 * @adev: amdgpu_device pointer
7298 *
7299 * Bring hardware to some kind of halt state so that no one can touch it
7300 * any more. It will help to maintain error context when error occurred.
7301 * Compare to a simple hang, the system will keep stable at least for SSH
7302 * access. Then it should be trivial to inspect the hardware state and
7303 * see what's going on. Implemented as following:
7304 *
7305 * 1. drm_dev_unplug() makes device inaccessible to user space(IOCTLs, etc),
7306 * clears all CPU mappings to device, disallows remappings through page faults
7307 * 2. amdgpu_irq_disable_all() disables all interrupts
7308 * 3. amdgpu_fence_driver_hw_fini() signals all HW fences
7309 * 4. set adev->no_hw_access to avoid potential crashes after setp 5
7310 * 5. amdgpu_device_unmap_mmio() clears all MMIO mappings
7311 * 6. pci_disable_device() and pci_wait_for_pending_transaction()
7312 * flush any in flight DMA operations
7313 */
amdgpu_device_halt(struct amdgpu_device * adev)7314 void amdgpu_device_halt(struct amdgpu_device *adev)
7315 {
7316 struct pci_dev *pdev = adev->pdev;
7317 struct drm_device *ddev = adev_to_drm(adev);
7318
7319 amdgpu_xcp_dev_unplug(adev);
7320 drm_dev_unplug(ddev);
7321
7322 amdgpu_irq_disable_all(adev);
7323
7324 amdgpu_fence_driver_hw_fini(adev);
7325
7326 adev->no_hw_access = true;
7327
7328 amdgpu_device_unmap_mmio(adev);
7329
7330 pci_disable_device(pdev);
7331 pci_wait_for_pending_transaction(pdev);
7332 }
7333
amdgpu_device_pcie_port_rreg(struct amdgpu_device * adev,u32 reg)7334 u32 amdgpu_device_pcie_port_rreg(struct amdgpu_device *adev,
7335 u32 reg)
7336 {
7337 unsigned long flags, address, data;
7338 u32 r;
7339
7340 address = adev->nbio.funcs->get_pcie_port_index_offset(adev);
7341 data = adev->nbio.funcs->get_pcie_port_data_offset(adev);
7342
7343 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
7344 WREG32(address, reg * 4);
7345 (void)RREG32(address);
7346 r = RREG32(data);
7347 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
7348 return r;
7349 }
7350
amdgpu_device_pcie_port_wreg(struct amdgpu_device * adev,u32 reg,u32 v)7351 void amdgpu_device_pcie_port_wreg(struct amdgpu_device *adev,
7352 u32 reg, u32 v)
7353 {
7354 unsigned long flags, address, data;
7355
7356 address = adev->nbio.funcs->get_pcie_port_index_offset(adev);
7357 data = adev->nbio.funcs->get_pcie_port_data_offset(adev);
7358
7359 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
7360 WREG32(address, reg * 4);
7361 (void)RREG32(address);
7362 WREG32(data, v);
7363 (void)RREG32(data);
7364 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
7365 }
7366
7367 /**
7368 * amdgpu_device_get_gang - return a reference to the current gang
7369 * @adev: amdgpu_device pointer
7370 *
7371 * Returns: A new reference to the current gang leader.
7372 */
amdgpu_device_get_gang(struct amdgpu_device * adev)7373 struct dma_fence *amdgpu_device_get_gang(struct amdgpu_device *adev)
7374 {
7375 struct dma_fence *fence;
7376
7377 rcu_read_lock();
7378 fence = dma_fence_get_rcu_safe(&adev->gang_submit);
7379 rcu_read_unlock();
7380 return fence;
7381 }
7382
7383 /**
7384 * amdgpu_device_switch_gang - switch to a new gang
7385 * @adev: amdgpu_device pointer
7386 * @gang: the gang to switch to
7387 *
7388 * Try to switch to a new gang.
7389 * Returns: NULL if we switched to the new gang or a reference to the current
7390 * gang leader.
7391 */
amdgpu_device_switch_gang(struct amdgpu_device * adev,struct dma_fence * gang)7392 struct dma_fence *amdgpu_device_switch_gang(struct amdgpu_device *adev,
7393 struct dma_fence *gang)
7394 {
7395 struct dma_fence *old = NULL;
7396
7397 dma_fence_get(gang);
7398 do {
7399 dma_fence_put(old);
7400 old = amdgpu_device_get_gang(adev);
7401 if (old == gang)
7402 break;
7403
7404 if (!dma_fence_is_signaled(old)) {
7405 dma_fence_put(gang);
7406 return old;
7407 }
7408
7409 } while (cmpxchg((struct dma_fence __force **)&adev->gang_submit,
7410 old, gang) != old);
7411
7412 /*
7413 * Drop it once for the exchanged reference in adev and once for the
7414 * thread local reference acquired in amdgpu_device_get_gang().
7415 */
7416 dma_fence_put(old);
7417 dma_fence_put(old);
7418 return NULL;
7419 }
7420
7421 /**
7422 * amdgpu_device_enforce_isolation - enforce HW isolation
7423 * @adev: the amdgpu device pointer
7424 * @ring: the HW ring the job is supposed to run on
7425 * @job: the job which is about to be pushed to the HW ring
7426 *
7427 * Makes sure that only one client at a time can use the GFX block.
7428 * Returns: The dependency to wait on before the job can be pushed to the HW.
7429 * The function is called multiple times until NULL is returned.
7430 */
amdgpu_device_enforce_isolation(struct amdgpu_device * adev,struct amdgpu_ring * ring,struct amdgpu_job * job)7431 struct dma_fence *amdgpu_device_enforce_isolation(struct amdgpu_device *adev,
7432 struct amdgpu_ring *ring,
7433 struct amdgpu_job *job)
7434 {
7435 struct amdgpu_isolation *isolation = &adev->isolation[ring->xcp_id];
7436 struct drm_sched_fence *f = job->base.s_fence;
7437 struct dma_fence *dep;
7438 void *owner;
7439 int r;
7440
7441 /*
7442 * For now enforce isolation only for the GFX block since we only need
7443 * the cleaner shader on those rings.
7444 */
7445 if (ring->funcs->type != AMDGPU_RING_TYPE_GFX &&
7446 ring->funcs->type != AMDGPU_RING_TYPE_COMPUTE)
7447 return NULL;
7448
7449 /*
7450 * All submissions where enforce isolation is false are handled as if
7451 * they come from a single client. Use ~0l as the owner to distinct it
7452 * from kernel submissions where the owner is NULL.
7453 */
7454 owner = job->enforce_isolation ? f->owner : (void *)~0l;
7455
7456 mutex_lock(&adev->enforce_isolation_mutex);
7457
7458 /*
7459 * The "spearhead" submission is the first one which changes the
7460 * ownership to its client. We always need to wait for it to be
7461 * pushed to the HW before proceeding with anything.
7462 */
7463 if (&f->scheduled != isolation->spearhead &&
7464 !dma_fence_is_signaled(isolation->spearhead)) {
7465 dep = isolation->spearhead;
7466 goto out_grab_ref;
7467 }
7468
7469 if (isolation->owner != owner) {
7470
7471 /*
7472 * Wait for any gang to be assembled before switching to a
7473 * different owner or otherwise we could deadlock the
7474 * submissions.
7475 */
7476 if (!job->gang_submit) {
7477 dep = amdgpu_device_get_gang(adev);
7478 if (!dma_fence_is_signaled(dep))
7479 goto out_return_dep;
7480 dma_fence_put(dep);
7481 }
7482
7483 dma_fence_put(isolation->spearhead);
7484 isolation->spearhead = dma_fence_get(&f->scheduled);
7485 amdgpu_sync_move(&isolation->active, &isolation->prev);
7486 trace_amdgpu_isolation(isolation->owner, owner);
7487 isolation->owner = owner;
7488 }
7489
7490 /*
7491 * Specifying the ring here helps to pipeline submissions even when
7492 * isolation is enabled. If that is not desired for testing NULL can be
7493 * used instead of the ring to enforce a CPU round trip while switching
7494 * between clients.
7495 */
7496 dep = amdgpu_sync_peek_fence(&isolation->prev, ring);
7497 r = amdgpu_sync_fence(&isolation->active, &f->finished, GFP_NOWAIT);
7498 if (r)
7499 dev_warn(adev->dev, "OOM tracking isolation\n");
7500
7501 out_grab_ref:
7502 dma_fence_get(dep);
7503 out_return_dep:
7504 mutex_unlock(&adev->enforce_isolation_mutex);
7505 return dep;
7506 }
7507
amdgpu_device_has_display_hardware(struct amdgpu_device * adev)7508 bool amdgpu_device_has_display_hardware(struct amdgpu_device *adev)
7509 {
7510 switch (adev->asic_type) {
7511 #ifdef CONFIG_DRM_AMDGPU_SI
7512 case CHIP_HAINAN:
7513 #endif
7514 case CHIP_TOPAZ:
7515 /* chips with no display hardware */
7516 return false;
7517 #ifdef CONFIG_DRM_AMDGPU_SI
7518 case CHIP_TAHITI:
7519 case CHIP_PITCAIRN:
7520 case CHIP_VERDE:
7521 case CHIP_OLAND:
7522 #endif
7523 #ifdef CONFIG_DRM_AMDGPU_CIK
7524 case CHIP_BONAIRE:
7525 case CHIP_HAWAII:
7526 case CHIP_KAVERI:
7527 case CHIP_KABINI:
7528 case CHIP_MULLINS:
7529 #endif
7530 case CHIP_TONGA:
7531 case CHIP_FIJI:
7532 case CHIP_POLARIS10:
7533 case CHIP_POLARIS11:
7534 case CHIP_POLARIS12:
7535 case CHIP_VEGAM:
7536 case CHIP_CARRIZO:
7537 case CHIP_STONEY:
7538 /* chips with display hardware */
7539 return true;
7540 default:
7541 /* IP discovery */
7542 if (!amdgpu_ip_version(adev, DCE_HWIP, 0) ||
7543 (adev->harvest_ip_mask & AMD_HARVEST_IP_DMU_MASK))
7544 return false;
7545 return true;
7546 }
7547 }
7548
amdgpu_device_wait_on_rreg(struct amdgpu_device * adev,uint32_t inst,uint32_t reg_addr,char reg_name[],uint32_t expected_value,uint32_t mask)7549 uint32_t amdgpu_device_wait_on_rreg(struct amdgpu_device *adev,
7550 uint32_t inst, uint32_t reg_addr, char reg_name[],
7551 uint32_t expected_value, uint32_t mask)
7552 {
7553 uint32_t ret = 0;
7554 uint32_t old_ = 0;
7555 uint32_t tmp_ = RREG32(reg_addr);
7556 uint32_t loop = adev->usec_timeout;
7557
7558 while ((tmp_ & (mask)) != (expected_value)) {
7559 if (old_ != tmp_) {
7560 loop = adev->usec_timeout;
7561 old_ = tmp_;
7562 } else
7563 udelay(1);
7564 tmp_ = RREG32(reg_addr);
7565 loop--;
7566 if (!loop) {
7567 dev_warn(
7568 adev->dev,
7569 "Register(%d) [%s] failed to reach value 0x%08x != 0x%08xn",
7570 inst, reg_name, (uint32_t)expected_value,
7571 (uint32_t)(tmp_ & (mask)));
7572 ret = -ETIMEDOUT;
7573 break;
7574 }
7575 }
7576 return ret;
7577 }
7578
amdgpu_get_soft_full_reset_mask(struct amdgpu_ring * ring)7579 ssize_t amdgpu_get_soft_full_reset_mask(struct amdgpu_ring *ring)
7580 {
7581 ssize_t size = 0;
7582
7583 if (!ring || !ring->adev)
7584 return size;
7585
7586 if (amdgpu_device_should_recover_gpu(ring->adev))
7587 size |= AMDGPU_RESET_TYPE_FULL;
7588
7589 if (unlikely(!ring->adev->debug_disable_soft_recovery) &&
7590 !amdgpu_sriov_vf(ring->adev) && ring->funcs->soft_recovery)
7591 size |= AMDGPU_RESET_TYPE_SOFT_RESET;
7592
7593 return size;
7594 }
7595
amdgpu_show_reset_mask(char * buf,uint32_t supported_reset)7596 ssize_t amdgpu_show_reset_mask(char *buf, uint32_t supported_reset)
7597 {
7598 ssize_t size = 0;
7599
7600 if (supported_reset == 0) {
7601 size += sysfs_emit_at(buf, size, "unsupported");
7602 size += sysfs_emit_at(buf, size, "\n");
7603 return size;
7604
7605 }
7606
7607 if (supported_reset & AMDGPU_RESET_TYPE_SOFT_RESET)
7608 size += sysfs_emit_at(buf, size, "soft ");
7609
7610 if (supported_reset & AMDGPU_RESET_TYPE_PER_QUEUE)
7611 size += sysfs_emit_at(buf, size, "queue ");
7612
7613 if (supported_reset & AMDGPU_RESET_TYPE_PER_PIPE)
7614 size += sysfs_emit_at(buf, size, "pipe ");
7615
7616 if (supported_reset & AMDGPU_RESET_TYPE_FULL)
7617 size += sysfs_emit_at(buf, size, "full ");
7618
7619 size += sysfs_emit_at(buf, size, "\n");
7620 return size;
7621 }
7622
amdgpu_device_set_uid(struct amdgpu_uid * uid_info,enum amdgpu_uid_type type,uint8_t inst,uint64_t uid)7623 void amdgpu_device_set_uid(struct amdgpu_uid *uid_info,
7624 enum amdgpu_uid_type type, uint8_t inst,
7625 uint64_t uid)
7626 {
7627 if (!uid_info)
7628 return;
7629
7630 if (type >= AMDGPU_UID_TYPE_MAX) {
7631 dev_err_once(uid_info->adev->dev, "Invalid UID type %d\n",
7632 type);
7633 return;
7634 }
7635
7636 if (inst >= AMDGPU_UID_INST_MAX) {
7637 dev_err_once(uid_info->adev->dev, "Invalid UID instance %d\n",
7638 inst);
7639 return;
7640 }
7641
7642 if (uid_info->uid[type][inst] != 0) {
7643 dev_warn_once(
7644 uid_info->adev->dev,
7645 "Overwriting existing UID %llu for type %d instance %d\n",
7646 uid_info->uid[type][inst], type, inst);
7647 }
7648
7649 uid_info->uid[type][inst] = uid;
7650 }
7651
amdgpu_device_get_uid(struct amdgpu_uid * uid_info,enum amdgpu_uid_type type,uint8_t inst)7652 u64 amdgpu_device_get_uid(struct amdgpu_uid *uid_info,
7653 enum amdgpu_uid_type type, uint8_t inst)
7654 {
7655 if (!uid_info)
7656 return 0;
7657
7658 if (type >= AMDGPU_UID_TYPE_MAX) {
7659 dev_err_once(uid_info->adev->dev, "Invalid UID type %d\n",
7660 type);
7661 return 0;
7662 }
7663
7664 if (inst >= AMDGPU_UID_INST_MAX) {
7665 dev_err_once(uid_info->adev->dev, "Invalid UID instance %d\n",
7666 inst);
7667 return 0;
7668 }
7669
7670 return uid_info->uid[type][inst];
7671 }
7672