]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/gpu/drm/amd/amdgpu/amdgpu_device.c
drm: add drm_pcie_get_max_link_width helper (v2)
[mirror_ubuntu-artful-kernel.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_device.c
CommitLineData
d38ceaf9
AD
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#include <linux/console.h>
29#include <linux/slab.h>
30#include <linux/debugfs.h>
31#include <drm/drmP.h>
32#include <drm/drm_crtc_helper.h>
33#include <drm/amdgpu_drm.h>
34#include <linux/vgaarb.h>
35#include <linux/vga_switcheroo.h>
36#include <linux/efi.h>
37#include "amdgpu.h"
38#include "amdgpu_i2c.h"
39#include "atom.h"
40#include "amdgpu_atombios.h"
a2e73f56
AD
41#ifdef CONFIG_DRM_AMDGPU_CIK
42#include "cik.h"
43#endif
aaa36a97 44#include "vi.h"
d38ceaf9
AD
45#include "bif/bif_4_1_d.h"
46
47static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
48static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
49
50static const char *amdgpu_asic_name[] = {
51 "BONAIRE",
52 "KAVERI",
53 "KABINI",
54 "HAWAII",
55 "MULLINS",
56 "TOPAZ",
57 "TONGA",
48299f95 58 "FIJI",
d38ceaf9 59 "CARRIZO",
139f4917 60 "STONEY",
d38ceaf9
AD
61 "LAST",
62};
63
64bool amdgpu_device_is_px(struct drm_device *dev)
65{
66 struct amdgpu_device *adev = dev->dev_private;
67
2f7d10b3 68 if (adev->flags & AMD_IS_PX)
d38ceaf9
AD
69 return true;
70 return false;
71}
72
73/*
74 * MMIO register access helper functions.
75 */
76uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
77 bool always_indirect)
78{
79 if ((reg * 4) < adev->rmmio_size && !always_indirect)
80 return readl(((void __iomem *)adev->rmmio) + (reg * 4));
81 else {
82 unsigned long flags;
83 uint32_t ret;
84
85 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
86 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
87 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
88 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
89
90 return ret;
91 }
92}
93
94void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
95 bool always_indirect)
96{
97 if ((reg * 4) < adev->rmmio_size && !always_indirect)
98 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
99 else {
100 unsigned long flags;
101
102 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
103 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
104 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
105 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
106 }
107}
108
109u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
110{
111 if ((reg * 4) < adev->rio_mem_size)
112 return ioread32(adev->rio_mem + (reg * 4));
113 else {
114 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
115 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
116 }
117}
118
119void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
120{
121
122 if ((reg * 4) < adev->rio_mem_size)
123 iowrite32(v, adev->rio_mem + (reg * 4));
124 else {
125 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
126 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
127 }
128}
129
130/**
131 * amdgpu_mm_rdoorbell - read a doorbell dword
132 *
133 * @adev: amdgpu_device pointer
134 * @index: doorbell index
135 *
136 * Returns the value in the doorbell aperture at the
137 * requested doorbell index (CIK).
138 */
139u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
140{
141 if (index < adev->doorbell.num_doorbells) {
142 return readl(adev->doorbell.ptr + index);
143 } else {
144 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
145 return 0;
146 }
147}
148
149/**
150 * amdgpu_mm_wdoorbell - write a doorbell dword
151 *
152 * @adev: amdgpu_device pointer
153 * @index: doorbell index
154 * @v: value to write
155 *
156 * Writes @v to the doorbell aperture at the
157 * requested doorbell index (CIK).
158 */
159void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
160{
161 if (index < adev->doorbell.num_doorbells) {
162 writel(v, adev->doorbell.ptr + index);
163 } else {
164 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
165 }
166}
167
168/**
169 * amdgpu_invalid_rreg - dummy reg read function
170 *
171 * @adev: amdgpu device pointer
172 * @reg: offset of register
173 *
174 * Dummy register read function. Used for register blocks
175 * that certain asics don't have (all asics).
176 * Returns the value in the register.
177 */
178static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
179{
180 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
181 BUG();
182 return 0;
183}
184
185/**
186 * amdgpu_invalid_wreg - dummy reg write function
187 *
188 * @adev: amdgpu device pointer
189 * @reg: offset of register
190 * @v: value to write to the register
191 *
192 * Dummy register read function. Used for register blocks
193 * that certain asics don't have (all asics).
194 */
195static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
196{
197 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
198 reg, v);
199 BUG();
200}
201
202/**
203 * amdgpu_block_invalid_rreg - dummy reg read function
204 *
205 * @adev: amdgpu device pointer
206 * @block: offset of instance
207 * @reg: offset of register
208 *
209 * Dummy register read function. Used for register blocks
210 * that certain asics don't have (all asics).
211 * Returns the value in the register.
212 */
213static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
214 uint32_t block, uint32_t reg)
215{
216 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
217 reg, block);
218 BUG();
219 return 0;
220}
221
222/**
223 * amdgpu_block_invalid_wreg - dummy reg write function
224 *
225 * @adev: amdgpu device pointer
226 * @block: offset of instance
227 * @reg: offset of register
228 * @v: value to write to the register
229 *
230 * Dummy register read function. Used for register blocks
231 * that certain asics don't have (all asics).
232 */
233static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
234 uint32_t block,
235 uint32_t reg, uint32_t v)
236{
237 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
238 reg, block, v);
239 BUG();
240}
241
242static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
243{
244 int r;
245
246 if (adev->vram_scratch.robj == NULL) {
247 r = amdgpu_bo_create(adev, AMDGPU_GPU_PAGE_SIZE,
857d913d
AD
248 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
249 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
72d7668b 250 NULL, NULL, &adev->vram_scratch.robj);
d38ceaf9
AD
251 if (r) {
252 return r;
253 }
254 }
255
256 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
257 if (unlikely(r != 0))
258 return r;
259 r = amdgpu_bo_pin(adev->vram_scratch.robj,
260 AMDGPU_GEM_DOMAIN_VRAM, &adev->vram_scratch.gpu_addr);
261 if (r) {
262 amdgpu_bo_unreserve(adev->vram_scratch.robj);
263 return r;
264 }
265 r = amdgpu_bo_kmap(adev->vram_scratch.robj,
266 (void **)&adev->vram_scratch.ptr);
267 if (r)
268 amdgpu_bo_unpin(adev->vram_scratch.robj);
269 amdgpu_bo_unreserve(adev->vram_scratch.robj);
270
271 return r;
272}
273
274static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
275{
276 int r;
277
278 if (adev->vram_scratch.robj == NULL) {
279 return;
280 }
281 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
282 if (likely(r == 0)) {
283 amdgpu_bo_kunmap(adev->vram_scratch.robj);
284 amdgpu_bo_unpin(adev->vram_scratch.robj);
285 amdgpu_bo_unreserve(adev->vram_scratch.robj);
286 }
287 amdgpu_bo_unref(&adev->vram_scratch.robj);
288}
289
290/**
291 * amdgpu_program_register_sequence - program an array of registers.
292 *
293 * @adev: amdgpu_device pointer
294 * @registers: pointer to the register array
295 * @array_size: size of the register array
296 *
297 * Programs an array or registers with and and or masks.
298 * This is a helper for setting golden registers.
299 */
300void amdgpu_program_register_sequence(struct amdgpu_device *adev,
301 const u32 *registers,
302 const u32 array_size)
303{
304 u32 tmp, reg, and_mask, or_mask;
305 int i;
306
307 if (array_size % 3)
308 return;
309
310 for (i = 0; i < array_size; i +=3) {
311 reg = registers[i + 0];
312 and_mask = registers[i + 1];
313 or_mask = registers[i + 2];
314
315 if (and_mask == 0xffffffff) {
316 tmp = or_mask;
317 } else {
318 tmp = RREG32(reg);
319 tmp &= ~and_mask;
320 tmp |= or_mask;
321 }
322 WREG32(reg, tmp);
323 }
324}
325
326void amdgpu_pci_config_reset(struct amdgpu_device *adev)
327{
328 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
329}
330
331/*
332 * GPU doorbell aperture helpers function.
333 */
334/**
335 * amdgpu_doorbell_init - Init doorbell driver information.
336 *
337 * @adev: amdgpu_device pointer
338 *
339 * Init doorbell driver information (CIK)
340 * Returns 0 on success, error on failure.
341 */
342static int amdgpu_doorbell_init(struct amdgpu_device *adev)
343{
344 /* doorbell bar mapping */
345 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
346 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
347
348 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
349 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
350 if (adev->doorbell.num_doorbells == 0)
351 return -EINVAL;
352
353 adev->doorbell.ptr = ioremap(adev->doorbell.base, adev->doorbell.num_doorbells * sizeof(u32));
354 if (adev->doorbell.ptr == NULL) {
355 return -ENOMEM;
356 }
357 DRM_INFO("doorbell mmio base: 0x%08X\n", (uint32_t)adev->doorbell.base);
358 DRM_INFO("doorbell mmio size: %u\n", (unsigned)adev->doorbell.size);
359
360 return 0;
361}
362
363/**
364 * amdgpu_doorbell_fini - Tear down doorbell driver information.
365 *
366 * @adev: amdgpu_device pointer
367 *
368 * Tear down doorbell driver information (CIK)
369 */
370static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
371{
372 iounmap(adev->doorbell.ptr);
373 adev->doorbell.ptr = NULL;
374}
375
376/**
377 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
378 * setup amdkfd
379 *
380 * @adev: amdgpu_device pointer
381 * @aperture_base: output returning doorbell aperture base physical address
382 * @aperture_size: output returning doorbell aperture size in bytes
383 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
384 *
385 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
386 * takes doorbells required for its own rings and reports the setup to amdkfd.
387 * amdgpu reserved doorbells are at the start of the doorbell aperture.
388 */
389void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
390 phys_addr_t *aperture_base,
391 size_t *aperture_size,
392 size_t *start_offset)
393{
394 /*
395 * The first num_doorbells are used by amdgpu.
396 * amdkfd takes whatever's left in the aperture.
397 */
398 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
399 *aperture_base = adev->doorbell.base;
400 *aperture_size = adev->doorbell.size;
401 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
402 } else {
403 *aperture_base = 0;
404 *aperture_size = 0;
405 *start_offset = 0;
406 }
407}
408
409/*
410 * amdgpu_wb_*()
411 * Writeback is the the method by which the the GPU updates special pages
412 * in memory with the status of certain GPU events (fences, ring pointers,
413 * etc.).
414 */
415
416/**
417 * amdgpu_wb_fini - Disable Writeback and free memory
418 *
419 * @adev: amdgpu_device pointer
420 *
421 * Disables Writeback and frees the Writeback memory (all asics).
422 * Used at driver shutdown.
423 */
424static void amdgpu_wb_fini(struct amdgpu_device *adev)
425{
426 if (adev->wb.wb_obj) {
427 if (!amdgpu_bo_reserve(adev->wb.wb_obj, false)) {
428 amdgpu_bo_kunmap(adev->wb.wb_obj);
429 amdgpu_bo_unpin(adev->wb.wb_obj);
430 amdgpu_bo_unreserve(adev->wb.wb_obj);
431 }
432 amdgpu_bo_unref(&adev->wb.wb_obj);
433 adev->wb.wb = NULL;
434 adev->wb.wb_obj = NULL;
435 }
436}
437
438/**
439 * amdgpu_wb_init- Init Writeback driver info and allocate memory
440 *
441 * @adev: amdgpu_device pointer
442 *
443 * Disables Writeback and frees the Writeback memory (all asics).
444 * Used at driver startup.
445 * Returns 0 on success or an -error on failure.
446 */
447static int amdgpu_wb_init(struct amdgpu_device *adev)
448{
449 int r;
450
451 if (adev->wb.wb_obj == NULL) {
452 r = amdgpu_bo_create(adev, AMDGPU_MAX_WB * 4, PAGE_SIZE, true,
72d7668b
CK
453 AMDGPU_GEM_DOMAIN_GTT, 0, NULL, NULL,
454 &adev->wb.wb_obj);
d38ceaf9
AD
455 if (r) {
456 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
457 return r;
458 }
459 r = amdgpu_bo_reserve(adev->wb.wb_obj, false);
460 if (unlikely(r != 0)) {
461 amdgpu_wb_fini(adev);
462 return r;
463 }
464 r = amdgpu_bo_pin(adev->wb.wb_obj, AMDGPU_GEM_DOMAIN_GTT,
465 &adev->wb.gpu_addr);
466 if (r) {
467 amdgpu_bo_unreserve(adev->wb.wb_obj);
468 dev_warn(adev->dev, "(%d) pin WB bo failed\n", r);
469 amdgpu_wb_fini(adev);
470 return r;
471 }
472 r = amdgpu_bo_kmap(adev->wb.wb_obj, (void **)&adev->wb.wb);
473 amdgpu_bo_unreserve(adev->wb.wb_obj);
474 if (r) {
475 dev_warn(adev->dev, "(%d) map WB bo failed\n", r);
476 amdgpu_wb_fini(adev);
477 return r;
478 }
479
480 adev->wb.num_wb = AMDGPU_MAX_WB;
481 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
482
483 /* clear wb memory */
484 memset((char *)adev->wb.wb, 0, AMDGPU_GPU_PAGE_SIZE);
485 }
486
487 return 0;
488}
489
490/**
491 * amdgpu_wb_get - Allocate a wb entry
492 *
493 * @adev: amdgpu_device pointer
494 * @wb: wb index
495 *
496 * Allocate a wb slot for use by the driver (all asics).
497 * Returns 0 on success or -EINVAL on failure.
498 */
499int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
500{
501 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
502 if (offset < adev->wb.num_wb) {
503 __set_bit(offset, adev->wb.used);
504 *wb = offset;
505 return 0;
506 } else {
507 return -EINVAL;
508 }
509}
510
511/**
512 * amdgpu_wb_free - Free a wb entry
513 *
514 * @adev: amdgpu_device pointer
515 * @wb: wb index
516 *
517 * Free a wb slot allocated for use by the driver (all asics)
518 */
519void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
520{
521 if (wb < adev->wb.num_wb)
522 __clear_bit(wb, adev->wb.used);
523}
524
525/**
526 * amdgpu_vram_location - try to find VRAM location
527 * @adev: amdgpu device structure holding all necessary informations
528 * @mc: memory controller structure holding memory informations
529 * @base: base address at which to put VRAM
530 *
531 * Function will place try to place VRAM at base address provided
532 * as parameter (which is so far either PCI aperture address or
533 * for IGP TOM base address).
534 *
535 * If there is not enough space to fit the unvisible VRAM in the 32bits
536 * address space then we limit the VRAM size to the aperture.
537 *
538 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
539 * this shouldn't be a problem as we are using the PCI aperture as a reference.
540 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
541 * not IGP.
542 *
543 * Note: we use mc_vram_size as on some board we need to program the mc to
544 * cover the whole aperture even if VRAM size is inferior to aperture size
545 * Novell bug 204882 + along with lots of ubuntu ones
546 *
547 * Note: when limiting vram it's safe to overwritte real_vram_size because
548 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
549 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
550 * ones)
551 *
552 * Note: IGP TOM addr should be the same as the aperture addr, we don't
553 * explicitly check for that thought.
554 *
555 * FIXME: when reducing VRAM size align new size on power of 2.
556 */
557void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
558{
559 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
560
561 mc->vram_start = base;
562 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
563 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
564 mc->real_vram_size = mc->aper_size;
565 mc->mc_vram_size = mc->aper_size;
566 }
567 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
568 if (limit && limit < mc->real_vram_size)
569 mc->real_vram_size = limit;
570 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
571 mc->mc_vram_size >> 20, mc->vram_start,
572 mc->vram_end, mc->real_vram_size >> 20);
573}
574
575/**
576 * amdgpu_gtt_location - try to find GTT location
577 * @adev: amdgpu device structure holding all necessary informations
578 * @mc: memory controller structure holding memory informations
579 *
580 * Function will place try to place GTT before or after VRAM.
581 *
582 * If GTT size is bigger than space left then we ajust GTT size.
583 * Thus function will never fails.
584 *
585 * FIXME: when reducing GTT size align new size on power of 2.
586 */
587void amdgpu_gtt_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
588{
589 u64 size_af, size_bf;
590
591 size_af = ((adev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
592 size_bf = mc->vram_start & ~mc->gtt_base_align;
593 if (size_bf > size_af) {
594 if (mc->gtt_size > size_bf) {
595 dev_warn(adev->dev, "limiting GTT\n");
596 mc->gtt_size = size_bf;
597 }
598 mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
599 } else {
600 if (mc->gtt_size > size_af) {
601 dev_warn(adev->dev, "limiting GTT\n");
602 mc->gtt_size = size_af;
603 }
604 mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
605 }
606 mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
607 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
608 mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
609}
610
611/*
612 * GPU helpers function.
613 */
614/**
615 * amdgpu_card_posted - check if the hw has already been initialized
616 *
617 * @adev: amdgpu_device pointer
618 *
619 * Check if the asic has been initialized (all asics).
620 * Used at driver startup.
621 * Returns true if initialized or false if not.
622 */
623bool amdgpu_card_posted(struct amdgpu_device *adev)
624{
625 uint32_t reg;
626
627 /* then check MEM_SIZE, in case the crtcs are off */
628 reg = RREG32(mmCONFIG_MEMSIZE);
629
630 if (reg)
631 return true;
632
633 return false;
634
635}
636
637/**
638 * amdgpu_boot_test_post_card - check and possibly initialize the hw
639 *
640 * @adev: amdgpu_device pointer
641 *
642 * Check if the asic is initialized and if not, attempt to initialize
643 * it (all asics).
644 * Returns true if initialized or false if not.
645 */
646bool amdgpu_boot_test_post_card(struct amdgpu_device *adev)
647{
648 if (amdgpu_card_posted(adev))
649 return true;
650
651 if (adev->bios) {
652 DRM_INFO("GPU not posted. posting now...\n");
653 if (adev->is_atom_bios)
654 amdgpu_atom_asic_init(adev->mode_info.atom_context);
655 return true;
656 } else {
657 dev_err(adev->dev, "Card not posted and no BIOS - ignoring\n");
658 return false;
659 }
660}
661
662/**
663 * amdgpu_dummy_page_init - init dummy page used by the driver
664 *
665 * @adev: amdgpu_device pointer
666 *
667 * Allocate the dummy page used by the driver (all asics).
668 * This dummy page is used by the driver as a filler for gart entries
669 * when pages are taken out of the GART
670 * Returns 0 on sucess, -ENOMEM on failure.
671 */
672int amdgpu_dummy_page_init(struct amdgpu_device *adev)
673{
674 if (adev->dummy_page.page)
675 return 0;
676 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
677 if (adev->dummy_page.page == NULL)
678 return -ENOMEM;
679 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
680 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
681 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
682 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
683 __free_page(adev->dummy_page.page);
684 adev->dummy_page.page = NULL;
685 return -ENOMEM;
686 }
687 return 0;
688}
689
690/**
691 * amdgpu_dummy_page_fini - free dummy page used by the driver
692 *
693 * @adev: amdgpu_device pointer
694 *
695 * Frees the dummy page used by the driver (all asics).
696 */
697void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
698{
699 if (adev->dummy_page.page == NULL)
700 return;
701 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
702 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
703 __free_page(adev->dummy_page.page);
704 adev->dummy_page.page = NULL;
705}
706
707
708/* ATOM accessor methods */
709/*
710 * ATOM is an interpreted byte code stored in tables in the vbios. The
711 * driver registers callbacks to access registers and the interpreter
712 * in the driver parses the tables and executes then to program specific
713 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
714 * atombios.h, and atom.c
715 */
716
717/**
718 * cail_pll_read - read PLL register
719 *
720 * @info: atom card_info pointer
721 * @reg: PLL register offset
722 *
723 * Provides a PLL register accessor for the atom interpreter (r4xx+).
724 * Returns the value of the PLL register.
725 */
726static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
727{
728 return 0;
729}
730
731/**
732 * cail_pll_write - write PLL register
733 *
734 * @info: atom card_info pointer
735 * @reg: PLL register offset
736 * @val: value to write to the pll register
737 *
738 * Provides a PLL register accessor for the atom interpreter (r4xx+).
739 */
740static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
741{
742
743}
744
745/**
746 * cail_mc_read - read MC (Memory Controller) register
747 *
748 * @info: atom card_info pointer
749 * @reg: MC register offset
750 *
751 * Provides an MC register accessor for the atom interpreter (r4xx+).
752 * Returns the value of the MC register.
753 */
754static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
755{
756 return 0;
757}
758
759/**
760 * cail_mc_write - write MC (Memory Controller) register
761 *
762 * @info: atom card_info pointer
763 * @reg: MC register offset
764 * @val: value to write to the pll register
765 *
766 * Provides a MC register accessor for the atom interpreter (r4xx+).
767 */
768static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
769{
770
771}
772
773/**
774 * cail_reg_write - write MMIO register
775 *
776 * @info: atom card_info pointer
777 * @reg: MMIO register offset
778 * @val: value to write to the pll register
779 *
780 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
781 */
782static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
783{
784 struct amdgpu_device *adev = info->dev->dev_private;
785
786 WREG32(reg, val);
787}
788
789/**
790 * cail_reg_read - read MMIO register
791 *
792 * @info: atom card_info pointer
793 * @reg: MMIO register offset
794 *
795 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
796 * Returns the value of the MMIO register.
797 */
798static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
799{
800 struct amdgpu_device *adev = info->dev->dev_private;
801 uint32_t r;
802
803 r = RREG32(reg);
804 return r;
805}
806
807/**
808 * cail_ioreg_write - write IO register
809 *
810 * @info: atom card_info pointer
811 * @reg: IO register offset
812 * @val: value to write to the pll register
813 *
814 * Provides a IO register accessor for the atom interpreter (r4xx+).
815 */
816static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
817{
818 struct amdgpu_device *adev = info->dev->dev_private;
819
820 WREG32_IO(reg, val);
821}
822
823/**
824 * cail_ioreg_read - read IO register
825 *
826 * @info: atom card_info pointer
827 * @reg: IO register offset
828 *
829 * Provides an IO register accessor for the atom interpreter (r4xx+).
830 * Returns the value of the IO register.
831 */
832static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
833{
834 struct amdgpu_device *adev = info->dev->dev_private;
835 uint32_t r;
836
837 r = RREG32_IO(reg);
838 return r;
839}
840
841/**
842 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
843 *
844 * @adev: amdgpu_device pointer
845 *
846 * Frees the driver info and register access callbacks for the ATOM
847 * interpreter (r4xx+).
848 * Called at driver shutdown.
849 */
850static void amdgpu_atombios_fini(struct amdgpu_device *adev)
851{
852 if (adev->mode_info.atom_context)
853 kfree(adev->mode_info.atom_context->scratch);
854 kfree(adev->mode_info.atom_context);
855 adev->mode_info.atom_context = NULL;
856 kfree(adev->mode_info.atom_card_info);
857 adev->mode_info.atom_card_info = NULL;
858}
859
860/**
861 * amdgpu_atombios_init - init the driver info and callbacks for atombios
862 *
863 * @adev: amdgpu_device pointer
864 *
865 * Initializes the driver info and register access callbacks for the
866 * ATOM interpreter (r4xx+).
867 * Returns 0 on sucess, -ENOMEM on failure.
868 * Called at driver startup.
869 */
870static int amdgpu_atombios_init(struct amdgpu_device *adev)
871{
872 struct card_info *atom_card_info =
873 kzalloc(sizeof(struct card_info), GFP_KERNEL);
874
875 if (!atom_card_info)
876 return -ENOMEM;
877
878 adev->mode_info.atom_card_info = atom_card_info;
879 atom_card_info->dev = adev->ddev;
880 atom_card_info->reg_read = cail_reg_read;
881 atom_card_info->reg_write = cail_reg_write;
882 /* needed for iio ops */
883 if (adev->rio_mem) {
884 atom_card_info->ioreg_read = cail_ioreg_read;
885 atom_card_info->ioreg_write = cail_ioreg_write;
886 } else {
887 DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
888 atom_card_info->ioreg_read = cail_reg_read;
889 atom_card_info->ioreg_write = cail_reg_write;
890 }
891 atom_card_info->mc_read = cail_mc_read;
892 atom_card_info->mc_write = cail_mc_write;
893 atom_card_info->pll_read = cail_pll_read;
894 atom_card_info->pll_write = cail_pll_write;
895
896 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
897 if (!adev->mode_info.atom_context) {
898 amdgpu_atombios_fini(adev);
899 return -ENOMEM;
900 }
901
902 mutex_init(&adev->mode_info.atom_context->mutex);
903 amdgpu_atombios_scratch_regs_init(adev);
904 amdgpu_atom_allocate_fb_scratch(adev->mode_info.atom_context);
905 return 0;
906}
907
908/* if we get transitioned to only one device, take VGA back */
909/**
910 * amdgpu_vga_set_decode - enable/disable vga decode
911 *
912 * @cookie: amdgpu_device pointer
913 * @state: enable/disable vga decode
914 *
915 * Enable/disable vga decode (all asics).
916 * Returns VGA resource flags.
917 */
918static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
919{
920 struct amdgpu_device *adev = cookie;
921 amdgpu_asic_set_vga_state(adev, state);
922 if (state)
923 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
924 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
925 else
926 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
927}
928
929/**
930 * amdgpu_check_pot_argument - check that argument is a power of two
931 *
932 * @arg: value to check
933 *
934 * Validates that a certain argument is a power of two (all asics).
935 * Returns true if argument is valid.
936 */
937static bool amdgpu_check_pot_argument(int arg)
938{
939 return (arg & (arg - 1)) == 0;
940}
941
942/**
943 * amdgpu_check_arguments - validate module params
944 *
945 * @adev: amdgpu_device pointer
946 *
947 * Validates certain module parameters and updates
948 * the associated values used by the driver (all asics).
949 */
950static void amdgpu_check_arguments(struct amdgpu_device *adev)
951{
5b011235
CZ
952 if (amdgpu_sched_jobs < 4) {
953 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
954 amdgpu_sched_jobs);
955 amdgpu_sched_jobs = 4;
956 } else if (!amdgpu_check_pot_argument(amdgpu_sched_jobs)){
957 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
958 amdgpu_sched_jobs);
959 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
960 }
d38ceaf9
AD
961 /* vramlimit must be a power of two */
962 if (!amdgpu_check_pot_argument(amdgpu_vram_limit)) {
963 dev_warn(adev->dev, "vram limit (%d) must be a power of 2\n",
964 amdgpu_vram_limit);
965 amdgpu_vram_limit = 0;
966 }
967
968 if (amdgpu_gart_size != -1) {
969 /* gtt size must be power of two and greater or equal to 32M */
970 if (amdgpu_gart_size < 32) {
971 dev_warn(adev->dev, "gart size (%d) too small\n",
972 amdgpu_gart_size);
973 amdgpu_gart_size = -1;
974 } else if (!amdgpu_check_pot_argument(amdgpu_gart_size)) {
975 dev_warn(adev->dev, "gart size (%d) must be a power of 2\n",
976 amdgpu_gart_size);
977 amdgpu_gart_size = -1;
978 }
979 }
980
981 if (!amdgpu_check_pot_argument(amdgpu_vm_size)) {
982 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
983 amdgpu_vm_size);
8dacc127 984 amdgpu_vm_size = 8;
d38ceaf9
AD
985 }
986
987 if (amdgpu_vm_size < 1) {
988 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
989 amdgpu_vm_size);
8dacc127 990 amdgpu_vm_size = 8;
d38ceaf9
AD
991 }
992
993 /*
994 * Max GPUVM size for Cayman, SI and CI are 40 bits.
995 */
996 if (amdgpu_vm_size > 1024) {
997 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
998 amdgpu_vm_size);
8dacc127 999 amdgpu_vm_size = 8;
d38ceaf9
AD
1000 }
1001
1002 /* defines number of bits in page table versus page directory,
1003 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1004 * page table and the remaining bits are in the page directory */
1005 if (amdgpu_vm_block_size == -1) {
1006
1007 /* Total bits covered by PD + PTs */
1008 unsigned bits = ilog2(amdgpu_vm_size) + 18;
1009
1010 /* Make sure the PD is 4K in size up to 8GB address space.
1011 Above that split equal between PD and PTs */
1012 if (amdgpu_vm_size <= 8)
1013 amdgpu_vm_block_size = bits - 9;
1014 else
1015 amdgpu_vm_block_size = (bits + 3) / 2;
1016
1017 } else if (amdgpu_vm_block_size < 9) {
1018 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1019 amdgpu_vm_block_size);
1020 amdgpu_vm_block_size = 9;
1021 }
1022
1023 if (amdgpu_vm_block_size > 24 ||
1024 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1025 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1026 amdgpu_vm_block_size);
1027 amdgpu_vm_block_size = 9;
1028 }
1029}
1030
1031/**
1032 * amdgpu_switcheroo_set_state - set switcheroo state
1033 *
1034 * @pdev: pci dev pointer
1694467b 1035 * @state: vga_switcheroo state
d38ceaf9
AD
1036 *
1037 * Callback for the switcheroo driver. Suspends or resumes the
1038 * the asics before or after it is powered up using ACPI methods.
1039 */
1040static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1041{
1042 struct drm_device *dev = pci_get_drvdata(pdev);
1043
1044 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1045 return;
1046
1047 if (state == VGA_SWITCHEROO_ON) {
1048 unsigned d3_delay = dev->pdev->d3_delay;
1049
1050 printk(KERN_INFO "amdgpu: switched on\n");
1051 /* don't suspend or resume card normally */
1052 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1053
1054 amdgpu_resume_kms(dev, true, true);
1055
1056 dev->pdev->d3_delay = d3_delay;
1057
1058 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1059 drm_kms_helper_poll_enable(dev);
1060 } else {
1061 printk(KERN_INFO "amdgpu: switched off\n");
1062 drm_kms_helper_poll_disable(dev);
1063 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1064 amdgpu_suspend_kms(dev, true, true);
1065 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1066 }
1067}
1068
1069/**
1070 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1071 *
1072 * @pdev: pci dev pointer
1073 *
1074 * Callback for the switcheroo driver. Check of the switcheroo
1075 * state can be changed.
1076 * Returns true if the state can be changed, false if not.
1077 */
1078static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1079{
1080 struct drm_device *dev = pci_get_drvdata(pdev);
1081
1082 /*
1083 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1084 * locking inversion with the driver load path. And the access here is
1085 * completely racy anyway. So don't bother with locking for now.
1086 */
1087 return dev->open_count == 0;
1088}
1089
1090static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1091 .set_gpu_state = amdgpu_switcheroo_set_state,
1092 .reprobe = NULL,
1093 .can_switch = amdgpu_switcheroo_can_switch,
1094};
1095
1096int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
5fc3aeeb 1097 enum amd_ip_block_type block_type,
1098 enum amd_clockgating_state state)
d38ceaf9
AD
1099{
1100 int i, r = 0;
1101
1102 for (i = 0; i < adev->num_ip_blocks; i++) {
1103 if (adev->ip_blocks[i].type == block_type) {
5fc3aeeb 1104 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
d38ceaf9
AD
1105 state);
1106 if (r)
1107 return r;
1108 }
1109 }
1110 return r;
1111}
1112
1113int amdgpu_set_powergating_state(struct amdgpu_device *adev,
5fc3aeeb 1114 enum amd_ip_block_type block_type,
1115 enum amd_powergating_state state)
d38ceaf9
AD
1116{
1117 int i, r = 0;
1118
1119 for (i = 0; i < adev->num_ip_blocks; i++) {
1120 if (adev->ip_blocks[i].type == block_type) {
5fc3aeeb 1121 r = adev->ip_blocks[i].funcs->set_powergating_state((void *)adev,
d38ceaf9
AD
1122 state);
1123 if (r)
1124 return r;
1125 }
1126 }
1127 return r;
1128}
1129
1130const struct amdgpu_ip_block_version * amdgpu_get_ip_block(
1131 struct amdgpu_device *adev,
5fc3aeeb 1132 enum amd_ip_block_type type)
d38ceaf9
AD
1133{
1134 int i;
1135
1136 for (i = 0; i < adev->num_ip_blocks; i++)
1137 if (adev->ip_blocks[i].type == type)
1138 return &adev->ip_blocks[i];
1139
1140 return NULL;
1141}
1142
1143/**
1144 * amdgpu_ip_block_version_cmp
1145 *
1146 * @adev: amdgpu_device pointer
5fc3aeeb 1147 * @type: enum amd_ip_block_type
d38ceaf9
AD
1148 * @major: major version
1149 * @minor: minor version
1150 *
1151 * return 0 if equal or greater
1152 * return 1 if smaller or the ip_block doesn't exist
1153 */
1154int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
5fc3aeeb 1155 enum amd_ip_block_type type,
d38ceaf9
AD
1156 u32 major, u32 minor)
1157{
1158 const struct amdgpu_ip_block_version *ip_block;
1159 ip_block = amdgpu_get_ip_block(adev, type);
1160
1161 if (ip_block && ((ip_block->major > major) ||
1162 ((ip_block->major == major) &&
1163 (ip_block->minor >= minor))))
1164 return 0;
1165
1166 return 1;
1167}
1168
1169static int amdgpu_early_init(struct amdgpu_device *adev)
1170{
aaa36a97 1171 int i, r;
d38ceaf9
AD
1172
1173 switch (adev->asic_type) {
aaa36a97
AD
1174 case CHIP_TOPAZ:
1175 case CHIP_TONGA:
48299f95 1176 case CHIP_FIJI:
aaa36a97 1177 case CHIP_CARRIZO:
39bb0c92
SL
1178 case CHIP_STONEY:
1179 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
aaa36a97
AD
1180 adev->family = AMDGPU_FAMILY_CZ;
1181 else
1182 adev->family = AMDGPU_FAMILY_VI;
1183
1184 r = vi_set_ip_blocks(adev);
1185 if (r)
1186 return r;
1187 break;
a2e73f56
AD
1188#ifdef CONFIG_DRM_AMDGPU_CIK
1189 case CHIP_BONAIRE:
1190 case CHIP_HAWAII:
1191 case CHIP_KAVERI:
1192 case CHIP_KABINI:
1193 case CHIP_MULLINS:
1194 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1195 adev->family = AMDGPU_FAMILY_CI;
1196 else
1197 adev->family = AMDGPU_FAMILY_KV;
1198
1199 r = cik_set_ip_blocks(adev);
1200 if (r)
1201 return r;
1202 break;
1203#endif
d38ceaf9
AD
1204 default:
1205 /* FIXME: not supported yet */
1206 return -EINVAL;
1207 }
1208
8faf0e08
AD
1209 adev->ip_block_status = kcalloc(adev->num_ip_blocks,
1210 sizeof(struct amdgpu_ip_block_status), GFP_KERNEL);
1211 if (adev->ip_block_status == NULL)
d8d090b7 1212 return -ENOMEM;
d38ceaf9
AD
1213
1214 if (adev->ip_blocks == NULL) {
1215 DRM_ERROR("No IP blocks found!\n");
1216 return r;
1217 }
1218
1219 for (i = 0; i < adev->num_ip_blocks; i++) {
1220 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1221 DRM_ERROR("disabled ip block: %d\n", i);
8faf0e08 1222 adev->ip_block_status[i].valid = false;
d38ceaf9
AD
1223 } else {
1224 if (adev->ip_blocks[i].funcs->early_init) {
5fc3aeeb 1225 r = adev->ip_blocks[i].funcs->early_init((void *)adev);
2c1a2784 1226 if (r == -ENOENT) {
8faf0e08 1227 adev->ip_block_status[i].valid = false;
2c1a2784
AD
1228 } else if (r) {
1229 DRM_ERROR("early_init %d failed %d\n", i, r);
d38ceaf9 1230 return r;
2c1a2784 1231 } else {
8faf0e08 1232 adev->ip_block_status[i].valid = true;
2c1a2784 1233 }
974e6b64 1234 } else {
8faf0e08 1235 adev->ip_block_status[i].valid = true;
d38ceaf9 1236 }
d38ceaf9
AD
1237 }
1238 }
1239
1240 return 0;
1241}
1242
1243static int amdgpu_init(struct amdgpu_device *adev)
1244{
1245 int i, r;
1246
1247 for (i = 0; i < adev->num_ip_blocks; i++) {
8faf0e08 1248 if (!adev->ip_block_status[i].valid)
d38ceaf9 1249 continue;
5fc3aeeb 1250 r = adev->ip_blocks[i].funcs->sw_init((void *)adev);
2c1a2784
AD
1251 if (r) {
1252 DRM_ERROR("sw_init %d failed %d\n", i, r);
d38ceaf9 1253 return r;
2c1a2784 1254 }
8faf0e08 1255 adev->ip_block_status[i].sw = true;
d38ceaf9 1256 /* need to do gmc hw init early so we can allocate gpu mem */
5fc3aeeb 1257 if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC) {
d38ceaf9 1258 r = amdgpu_vram_scratch_init(adev);
2c1a2784
AD
1259 if (r) {
1260 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
d38ceaf9 1261 return r;
2c1a2784 1262 }
5fc3aeeb 1263 r = adev->ip_blocks[i].funcs->hw_init((void *)adev);
2c1a2784
AD
1264 if (r) {
1265 DRM_ERROR("hw_init %d failed %d\n", i, r);
d38ceaf9 1266 return r;
2c1a2784 1267 }
d38ceaf9 1268 r = amdgpu_wb_init(adev);
2c1a2784
AD
1269 if (r) {
1270 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
d38ceaf9 1271 return r;
2c1a2784 1272 }
8faf0e08 1273 adev->ip_block_status[i].hw = true;
d38ceaf9
AD
1274 }
1275 }
1276
1277 for (i = 0; i < adev->num_ip_blocks; i++) {
8faf0e08 1278 if (!adev->ip_block_status[i].sw)
d38ceaf9
AD
1279 continue;
1280 /* gmc hw init is done early */
5fc3aeeb 1281 if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC)
d38ceaf9 1282 continue;
5fc3aeeb 1283 r = adev->ip_blocks[i].funcs->hw_init((void *)adev);
2c1a2784
AD
1284 if (r) {
1285 DRM_ERROR("hw_init %d failed %d\n", i, r);
d38ceaf9 1286 return r;
2c1a2784 1287 }
8faf0e08 1288 adev->ip_block_status[i].hw = true;
d38ceaf9
AD
1289 }
1290
1291 return 0;
1292}
1293
1294static int amdgpu_late_init(struct amdgpu_device *adev)
1295{
1296 int i = 0, r;
1297
1298 for (i = 0; i < adev->num_ip_blocks; i++) {
8faf0e08 1299 if (!adev->ip_block_status[i].valid)
d38ceaf9
AD
1300 continue;
1301 /* enable clockgating to save power */
5fc3aeeb 1302 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1303 AMD_CG_STATE_GATE);
2c1a2784
AD
1304 if (r) {
1305 DRM_ERROR("set_clockgating_state(gate) %d failed %d\n", i, r);
d38ceaf9 1306 return r;
2c1a2784 1307 }
d38ceaf9 1308 if (adev->ip_blocks[i].funcs->late_init) {
5fc3aeeb 1309 r = adev->ip_blocks[i].funcs->late_init((void *)adev);
2c1a2784
AD
1310 if (r) {
1311 DRM_ERROR("late_init %d failed %d\n", i, r);
d38ceaf9 1312 return r;
2c1a2784 1313 }
d38ceaf9
AD
1314 }
1315 }
1316
1317 return 0;
1318}
1319
1320static int amdgpu_fini(struct amdgpu_device *adev)
1321{
1322 int i, r;
1323
1324 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
8faf0e08 1325 if (!adev->ip_block_status[i].hw)
d38ceaf9 1326 continue;
5fc3aeeb 1327 if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC) {
d38ceaf9
AD
1328 amdgpu_wb_fini(adev);
1329 amdgpu_vram_scratch_fini(adev);
1330 }
1331 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
5fc3aeeb 1332 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1333 AMD_CG_STATE_UNGATE);
2c1a2784
AD
1334 if (r) {
1335 DRM_ERROR("set_clockgating_state(ungate) %d failed %d\n", i, r);
d38ceaf9 1336 return r;
2c1a2784 1337 }
5fc3aeeb 1338 r = adev->ip_blocks[i].funcs->hw_fini((void *)adev);
d38ceaf9 1339 /* XXX handle errors */
2c1a2784
AD
1340 if (r) {
1341 DRM_DEBUG("hw_fini %d failed %d\n", i, r);
1342 }
8faf0e08 1343 adev->ip_block_status[i].hw = false;
d38ceaf9
AD
1344 }
1345
1346 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
8faf0e08 1347 if (!adev->ip_block_status[i].sw)
d38ceaf9 1348 continue;
5fc3aeeb 1349 r = adev->ip_blocks[i].funcs->sw_fini((void *)adev);
d38ceaf9 1350 /* XXX handle errors */
2c1a2784
AD
1351 if (r) {
1352 DRM_DEBUG("sw_fini %d failed %d\n", i, r);
1353 }
8faf0e08
AD
1354 adev->ip_block_status[i].sw = false;
1355 adev->ip_block_status[i].valid = false;
d38ceaf9
AD
1356 }
1357
1358 return 0;
1359}
1360
1361static int amdgpu_suspend(struct amdgpu_device *adev)
1362{
1363 int i, r;
1364
1365 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
8faf0e08 1366 if (!adev->ip_block_status[i].valid)
d38ceaf9
AD
1367 continue;
1368 /* ungate blocks so that suspend can properly shut them down */
5fc3aeeb 1369 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1370 AMD_CG_STATE_UNGATE);
2c1a2784
AD
1371 if (r) {
1372 DRM_ERROR("set_clockgating_state(ungate) %d failed %d\n", i, r);
1373 }
d38ceaf9
AD
1374 /* XXX handle errors */
1375 r = adev->ip_blocks[i].funcs->suspend(adev);
1376 /* XXX handle errors */
2c1a2784
AD
1377 if (r) {
1378 DRM_ERROR("suspend %d failed %d\n", i, r);
1379 }
d38ceaf9
AD
1380 }
1381
1382 return 0;
1383}
1384
1385static int amdgpu_resume(struct amdgpu_device *adev)
1386{
1387 int i, r;
1388
1389 for (i = 0; i < adev->num_ip_blocks; i++) {
8faf0e08 1390 if (!adev->ip_block_status[i].valid)
d38ceaf9
AD
1391 continue;
1392 r = adev->ip_blocks[i].funcs->resume(adev);
2c1a2784
AD
1393 if (r) {
1394 DRM_ERROR("resume %d failed %d\n", i, r);
d38ceaf9 1395 return r;
2c1a2784 1396 }
d38ceaf9
AD
1397 }
1398
1399 return 0;
1400}
1401
1402/**
1403 * amdgpu_device_init - initialize the driver
1404 *
1405 * @adev: amdgpu_device pointer
1406 * @pdev: drm dev pointer
1407 * @pdev: pci dev pointer
1408 * @flags: driver flags
1409 *
1410 * Initializes the driver info and hw (all asics).
1411 * Returns 0 for success or an error on failure.
1412 * Called at driver startup.
1413 */
1414int amdgpu_device_init(struct amdgpu_device *adev,
1415 struct drm_device *ddev,
1416 struct pci_dev *pdev,
1417 uint32_t flags)
1418{
1419 int r, i;
1420 bool runtime = false;
1421
1422 adev->shutdown = false;
1423 adev->dev = &pdev->dev;
1424 adev->ddev = ddev;
1425 adev->pdev = pdev;
1426 adev->flags = flags;
2f7d10b3 1427 adev->asic_type = flags & AMD_ASIC_MASK;
d38ceaf9
AD
1428 adev->is_atom_bios = false;
1429 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
1430 adev->mc.gtt_size = 512 * 1024 * 1024;
1431 adev->accel_working = false;
1432 adev->num_rings = 0;
1433 adev->mman.buffer_funcs = NULL;
1434 adev->mman.buffer_funcs_ring = NULL;
1435 adev->vm_manager.vm_pte_funcs = NULL;
1436 adev->vm_manager.vm_pte_funcs_ring = NULL;
1437 adev->gart.gart_funcs = NULL;
1438 adev->fence_context = fence_context_alloc(AMDGPU_MAX_RINGS);
1439
1440 adev->smc_rreg = &amdgpu_invalid_rreg;
1441 adev->smc_wreg = &amdgpu_invalid_wreg;
1442 adev->pcie_rreg = &amdgpu_invalid_rreg;
1443 adev->pcie_wreg = &amdgpu_invalid_wreg;
1444 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
1445 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
1446 adev->didt_rreg = &amdgpu_invalid_rreg;
1447 adev->didt_wreg = &amdgpu_invalid_wreg;
1448 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
1449 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
1450
3e39ab90
AD
1451 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1452 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
1453 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
d38ceaf9
AD
1454
1455 /* mutex initialization are all done here so we
1456 * can recall function without having locking issues */
1457 mutex_init(&adev->ring_lock);
1458 atomic_set(&adev->irq.ih.lock, 0);
1459 mutex_init(&adev->gem.mutex);
1460 mutex_init(&adev->pm.mutex);
1461 mutex_init(&adev->gfx.gpu_clock_mutex);
1462 mutex_init(&adev->srbm_mutex);
1463 mutex_init(&adev->grbm_idx_mutex);
d38ceaf9
AD
1464 mutex_init(&adev->mn_lock);
1465 hash_init(adev->mn_hash);
1466
1467 amdgpu_check_arguments(adev);
1468
1469 /* Registers mapping */
1470 /* TODO: block userspace mapping of io register */
1471 spin_lock_init(&adev->mmio_idx_lock);
1472 spin_lock_init(&adev->smc_idx_lock);
1473 spin_lock_init(&adev->pcie_idx_lock);
1474 spin_lock_init(&adev->uvd_ctx_idx_lock);
1475 spin_lock_init(&adev->didt_idx_lock);
1476 spin_lock_init(&adev->audio_endpt_idx_lock);
1477
1478 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
1479 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
1480 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
1481 if (adev->rmmio == NULL) {
1482 return -ENOMEM;
1483 }
1484 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
1485 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
1486
1487 /* doorbell bar mapping */
1488 amdgpu_doorbell_init(adev);
1489
1490 /* io port mapping */
1491 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1492 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
1493 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
1494 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
1495 break;
1496 }
1497 }
1498 if (adev->rio_mem == NULL)
1499 DRM_ERROR("Unable to find PCI I/O BAR\n");
1500
1501 /* early init functions */
1502 r = amdgpu_early_init(adev);
1503 if (r)
1504 return r;
1505
1506 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
1507 /* this will fail for cards that aren't VGA class devices, just
1508 * ignore it */
1509 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
1510
1511 if (amdgpu_runtime_pm == 1)
1512 runtime = true;
1513 if (amdgpu_device_is_px(ddev))
1514 runtime = true;
1515 vga_switcheroo_register_client(adev->pdev, &amdgpu_switcheroo_ops, runtime);
1516 if (runtime)
1517 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
1518
1519 /* Read BIOS */
1520 if (!amdgpu_get_bios(adev))
1521 return -EINVAL;
1522 /* Must be an ATOMBIOS */
1523 if (!adev->is_atom_bios) {
1524 dev_err(adev->dev, "Expecting atombios for GPU\n");
1525 return -EINVAL;
1526 }
1527 r = amdgpu_atombios_init(adev);
2c1a2784
AD
1528 if (r) {
1529 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
d38ceaf9 1530 return r;
2c1a2784 1531 }
d38ceaf9
AD
1532
1533 /* Post card if necessary */
1534 if (!amdgpu_card_posted(adev)) {
1535 if (!adev->bios) {
1536 dev_err(adev->dev, "Card not posted and no BIOS - ignoring\n");
1537 return -EINVAL;
1538 }
1539 DRM_INFO("GPU not posted. posting now...\n");
1540 amdgpu_atom_asic_init(adev->mode_info.atom_context);
1541 }
1542
1543 /* Initialize clocks */
1544 r = amdgpu_atombios_get_clock_info(adev);
2c1a2784
AD
1545 if (r) {
1546 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
d38ceaf9 1547 return r;
2c1a2784 1548 }
d38ceaf9
AD
1549 /* init i2c buses */
1550 amdgpu_atombios_i2c_init(adev);
1551
1552 /* Fence driver */
1553 r = amdgpu_fence_driver_init(adev);
2c1a2784
AD
1554 if (r) {
1555 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
d38ceaf9 1556 return r;
2c1a2784 1557 }
d38ceaf9
AD
1558
1559 /* init the mode config */
1560 drm_mode_config_init(adev->ddev);
1561
1562 r = amdgpu_init(adev);
1563 if (r) {
2c1a2784 1564 dev_err(adev->dev, "amdgpu_init failed\n");
d38ceaf9
AD
1565 amdgpu_fini(adev);
1566 return r;
1567 }
1568
1569 adev->accel_working = true;
1570
1571 amdgpu_fbdev_init(adev);
1572
1573 r = amdgpu_ib_pool_init(adev);
1574 if (r) {
1575 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
1576 return r;
1577 }
1578
d033a6de 1579 r = amdgpu_ctx_init(adev, AMD_SCHED_PRIORITY_KERNEL, &adev->kernel_ctx);
47f38501
CK
1580 if (r) {
1581 dev_err(adev->dev, "failed to create kernel context (%d).\n", r);
1582 return r;
23ca0e4e 1583 }
d38ceaf9
AD
1584 r = amdgpu_ib_ring_tests(adev);
1585 if (r)
1586 DRM_ERROR("ib ring test failed (%d).\n", r);
1587
1588 r = amdgpu_gem_debugfs_init(adev);
1589 if (r) {
1590 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
1591 }
1592
1593 r = amdgpu_debugfs_regs_init(adev);
1594 if (r) {
1595 DRM_ERROR("registering register debugfs failed (%d).\n", r);
1596 }
1597
1598 if ((amdgpu_testing & 1)) {
1599 if (adev->accel_working)
1600 amdgpu_test_moves(adev);
1601 else
1602 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
1603 }
1604 if ((amdgpu_testing & 2)) {
1605 if (adev->accel_working)
1606 amdgpu_test_syncing(adev);
1607 else
1608 DRM_INFO("amdgpu: acceleration disabled, skipping sync tests\n");
1609 }
1610 if (amdgpu_benchmarking) {
1611 if (adev->accel_working)
1612 amdgpu_benchmark(adev, amdgpu_benchmarking);
1613 else
1614 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
1615 }
1616
1617 /* enable clockgating, etc. after ib tests, etc. since some blocks require
1618 * explicit gating rather than handling it automatically.
1619 */
1620 r = amdgpu_late_init(adev);
2c1a2784
AD
1621 if (r) {
1622 dev_err(adev->dev, "amdgpu_late_init failed\n");
d38ceaf9 1623 return r;
2c1a2784 1624 }
d38ceaf9
AD
1625
1626 return 0;
1627}
1628
1629static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev);
1630
1631/**
1632 * amdgpu_device_fini - tear down the driver
1633 *
1634 * @adev: amdgpu_device pointer
1635 *
1636 * Tear down the driver info (all asics).
1637 * Called at driver shutdown.
1638 */
1639void amdgpu_device_fini(struct amdgpu_device *adev)
1640{
1641 int r;
1642
1643 DRM_INFO("amdgpu: finishing device.\n");
1644 adev->shutdown = true;
1645 /* evict vram memory */
1646 amdgpu_bo_evict_vram(adev);
47f38501 1647 amdgpu_ctx_fini(&adev->kernel_ctx);
d38ceaf9
AD
1648 amdgpu_ib_pool_fini(adev);
1649 amdgpu_fence_driver_fini(adev);
1650 amdgpu_fbdev_fini(adev);
1651 r = amdgpu_fini(adev);
8faf0e08
AD
1652 kfree(adev->ip_block_status);
1653 adev->ip_block_status = NULL;
d38ceaf9
AD
1654 adev->accel_working = false;
1655 /* free i2c buses */
1656 amdgpu_i2c_fini(adev);
1657 amdgpu_atombios_fini(adev);
1658 kfree(adev->bios);
1659 adev->bios = NULL;
1660 vga_switcheroo_unregister_client(adev->pdev);
1661 vga_client_register(adev->pdev, NULL, NULL, NULL);
1662 if (adev->rio_mem)
1663 pci_iounmap(adev->pdev, adev->rio_mem);
1664 adev->rio_mem = NULL;
1665 iounmap(adev->rmmio);
1666 adev->rmmio = NULL;
1667 amdgpu_doorbell_fini(adev);
1668 amdgpu_debugfs_regs_cleanup(adev);
1669 amdgpu_debugfs_remove_files(adev);
1670}
1671
1672
1673/*
1674 * Suspend & resume.
1675 */
1676/**
1677 * amdgpu_suspend_kms - initiate device suspend
1678 *
1679 * @pdev: drm dev pointer
1680 * @state: suspend state
1681 *
1682 * Puts the hw in the suspend state (all asics).
1683 * Returns 0 for success or an error on failure.
1684 * Called at driver suspend.
1685 */
1686int amdgpu_suspend_kms(struct drm_device *dev, bool suspend, bool fbcon)
1687{
1688 struct amdgpu_device *adev;
1689 struct drm_crtc *crtc;
1690 struct drm_connector *connector;
5ceb54c6 1691 int r;
d38ceaf9
AD
1692
1693 if (dev == NULL || dev->dev_private == NULL) {
1694 return -ENODEV;
1695 }
1696
1697 adev = dev->dev_private;
1698
1699 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1700 return 0;
1701
1702 drm_kms_helper_poll_disable(dev);
1703
1704 /* turn off display hw */
4c7fbc39 1705 drm_modeset_lock_all(dev);
d38ceaf9
AD
1706 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1707 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
1708 }
4c7fbc39 1709 drm_modeset_unlock_all(dev);
d38ceaf9 1710
756e6880 1711 /* unpin the front buffers and cursors */
d38ceaf9 1712 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
756e6880 1713 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
d38ceaf9
AD
1714 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
1715 struct amdgpu_bo *robj;
1716
756e6880
AD
1717 if (amdgpu_crtc->cursor_bo) {
1718 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
1719 r = amdgpu_bo_reserve(aobj, false);
1720 if (r == 0) {
1721 amdgpu_bo_unpin(aobj);
1722 amdgpu_bo_unreserve(aobj);
1723 }
1724 }
1725
d38ceaf9
AD
1726 if (rfb == NULL || rfb->obj == NULL) {
1727 continue;
1728 }
1729 robj = gem_to_amdgpu_bo(rfb->obj);
1730 /* don't unpin kernel fb objects */
1731 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
1732 r = amdgpu_bo_reserve(robj, false);
1733 if (r == 0) {
1734 amdgpu_bo_unpin(robj);
1735 amdgpu_bo_unreserve(robj);
1736 }
1737 }
1738 }
1739 /* evict vram memory */
1740 amdgpu_bo_evict_vram(adev);
1741
5ceb54c6 1742 amdgpu_fence_driver_suspend(adev);
d38ceaf9
AD
1743
1744 r = amdgpu_suspend(adev);
1745
1746 /* evict remaining vram memory */
1747 amdgpu_bo_evict_vram(adev);
1748
1749 pci_save_state(dev->pdev);
1750 if (suspend) {
1751 /* Shut down the device */
1752 pci_disable_device(dev->pdev);
1753 pci_set_power_state(dev->pdev, PCI_D3hot);
1754 }
1755
1756 if (fbcon) {
1757 console_lock();
1758 amdgpu_fbdev_set_suspend(adev, 1);
1759 console_unlock();
1760 }
1761 return 0;
1762}
1763
1764/**
1765 * amdgpu_resume_kms - initiate device resume
1766 *
1767 * @pdev: drm dev pointer
1768 *
1769 * Bring the hw back to operating state (all asics).
1770 * Returns 0 for success or an error on failure.
1771 * Called at driver resume.
1772 */
1773int amdgpu_resume_kms(struct drm_device *dev, bool resume, bool fbcon)
1774{
1775 struct drm_connector *connector;
1776 struct amdgpu_device *adev = dev->dev_private;
756e6880 1777 struct drm_crtc *crtc;
d38ceaf9
AD
1778 int r;
1779
1780 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1781 return 0;
1782
1783 if (fbcon) {
1784 console_lock();
1785 }
1786 if (resume) {
1787 pci_set_power_state(dev->pdev, PCI_D0);
1788 pci_restore_state(dev->pdev);
1789 if (pci_enable_device(dev->pdev)) {
1790 if (fbcon)
1791 console_unlock();
1792 return -1;
1793 }
1794 }
1795
1796 /* post card */
1797 amdgpu_atom_asic_init(adev->mode_info.atom_context);
1798
1799 r = amdgpu_resume(adev);
1800
5ceb54c6
AD
1801 amdgpu_fence_driver_resume(adev);
1802
d38ceaf9
AD
1803 r = amdgpu_ib_ring_tests(adev);
1804 if (r)
1805 DRM_ERROR("ib ring test failed (%d).\n", r);
1806
1807 r = amdgpu_late_init(adev);
1808 if (r)
1809 return r;
1810
756e6880
AD
1811 /* pin cursors */
1812 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1813 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1814
1815 if (amdgpu_crtc->cursor_bo) {
1816 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
1817 r = amdgpu_bo_reserve(aobj, false);
1818 if (r == 0) {
1819 r = amdgpu_bo_pin(aobj,
1820 AMDGPU_GEM_DOMAIN_VRAM,
1821 &amdgpu_crtc->cursor_addr);
1822 if (r != 0)
1823 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
1824 amdgpu_bo_unreserve(aobj);
1825 }
1826 }
1827 }
1828
d38ceaf9
AD
1829 /* blat the mode back in */
1830 if (fbcon) {
1831 drm_helper_resume_force_mode(dev);
1832 /* turn on display hw */
4c7fbc39 1833 drm_modeset_lock_all(dev);
d38ceaf9
AD
1834 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1835 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
1836 }
4c7fbc39 1837 drm_modeset_unlock_all(dev);
d38ceaf9
AD
1838 }
1839
1840 drm_kms_helper_poll_enable(dev);
54fb2a5c 1841 drm_helper_hpd_irq_event(dev);
d38ceaf9
AD
1842
1843 if (fbcon) {
1844 amdgpu_fbdev_set_suspend(adev, 0);
1845 console_unlock();
1846 }
1847
1848 return 0;
1849}
1850
1851/**
1852 * amdgpu_gpu_reset - reset the asic
1853 *
1854 * @adev: amdgpu device pointer
1855 *
1856 * Attempt the reset the GPU if it has hung (all asics).
1857 * Returns 0 for success or an error on failure.
1858 */
1859int amdgpu_gpu_reset(struct amdgpu_device *adev)
1860{
1861 unsigned ring_sizes[AMDGPU_MAX_RINGS];
1862 uint32_t *ring_data[AMDGPU_MAX_RINGS];
1863
1864 bool saved = false;
1865
1866 int i, r;
1867 int resched;
1868
d94aed5a 1869 atomic_inc(&adev->gpu_reset_counter);
d38ceaf9
AD
1870
1871 /* block TTM */
1872 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
1873
1874 r = amdgpu_suspend(adev);
1875
1876 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1877 struct amdgpu_ring *ring = adev->rings[i];
1878 if (!ring)
1879 continue;
1880
1881 ring_sizes[i] = amdgpu_ring_backup(ring, &ring_data[i]);
1882 if (ring_sizes[i]) {
1883 saved = true;
1884 dev_info(adev->dev, "Saved %d dwords of commands "
1885 "on ring %d.\n", ring_sizes[i], i);
1886 }
1887 }
1888
1889retry:
1890 r = amdgpu_asic_reset(adev);
1891 if (!r) {
1892 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
1893 r = amdgpu_resume(adev);
1894 }
1895
1896 if (!r) {
1897 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1898 struct amdgpu_ring *ring = adev->rings[i];
1899 if (!ring)
1900 continue;
1901
1902 amdgpu_ring_restore(ring, ring_sizes[i], ring_data[i]);
1903 ring_sizes[i] = 0;
1904 ring_data[i] = NULL;
1905 }
1906
1907 r = amdgpu_ib_ring_tests(adev);
1908 if (r) {
1909 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
1910 if (saved) {
1911 saved = false;
1912 r = amdgpu_suspend(adev);
1913 goto retry;
1914 }
1915 }
1916 } else {
1917 amdgpu_fence_driver_force_completion(adev);
1918 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1919 if (adev->rings[i])
1920 kfree(ring_data[i]);
1921 }
1922 }
1923
1924 drm_helper_resume_force_mode(adev->ddev);
1925
1926 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
1927 if (r) {
1928 /* bad news, how to tell it to userspace ? */
1929 dev_info(adev->dev, "GPU reset failed\n");
1930 }
1931
d38ceaf9
AD
1932 return r;
1933}
1934
1935
1936/*
1937 * Debugfs
1938 */
1939int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
1940 struct drm_info_list *files,
1941 unsigned nfiles)
1942{
1943 unsigned i;
1944
1945 for (i = 0; i < adev->debugfs_count; i++) {
1946 if (adev->debugfs[i].files == files) {
1947 /* Already registered */
1948 return 0;
1949 }
1950 }
1951
1952 i = adev->debugfs_count + 1;
1953 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
1954 DRM_ERROR("Reached maximum number of debugfs components.\n");
1955 DRM_ERROR("Report so we increase "
1956 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
1957 return -EINVAL;
1958 }
1959 adev->debugfs[adev->debugfs_count].files = files;
1960 adev->debugfs[adev->debugfs_count].num_files = nfiles;
1961 adev->debugfs_count = i;
1962#if defined(CONFIG_DEBUG_FS)
1963 drm_debugfs_create_files(files, nfiles,
1964 adev->ddev->control->debugfs_root,
1965 adev->ddev->control);
1966 drm_debugfs_create_files(files, nfiles,
1967 adev->ddev->primary->debugfs_root,
1968 adev->ddev->primary);
1969#endif
1970 return 0;
1971}
1972
1973static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev)
1974{
1975#if defined(CONFIG_DEBUG_FS)
1976 unsigned i;
1977
1978 for (i = 0; i < adev->debugfs_count; i++) {
1979 drm_debugfs_remove_files(adev->debugfs[i].files,
1980 adev->debugfs[i].num_files,
1981 adev->ddev->control);
1982 drm_debugfs_remove_files(adev->debugfs[i].files,
1983 adev->debugfs[i].num_files,
1984 adev->ddev->primary);
1985 }
1986#endif
1987}
1988
1989#if defined(CONFIG_DEBUG_FS)
1990
1991static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
1992 size_t size, loff_t *pos)
1993{
1994 struct amdgpu_device *adev = f->f_inode->i_private;
1995 ssize_t result = 0;
1996 int r;
1997
1998 if (size & 0x3 || *pos & 0x3)
1999 return -EINVAL;
2000
2001 while (size) {
2002 uint32_t value;
2003
2004 if (*pos > adev->rmmio_size)
2005 return result;
2006
2007 value = RREG32(*pos >> 2);
2008 r = put_user(value, (uint32_t *)buf);
2009 if (r)
2010 return r;
2011
2012 result += 4;
2013 buf += 4;
2014 *pos += 4;
2015 size -= 4;
2016 }
2017
2018 return result;
2019}
2020
2021static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
2022 size_t size, loff_t *pos)
2023{
2024 struct amdgpu_device *adev = f->f_inode->i_private;
2025 ssize_t result = 0;
2026 int r;
2027
2028 if (size & 0x3 || *pos & 0x3)
2029 return -EINVAL;
2030
2031 while (size) {
2032 uint32_t value;
2033
2034 if (*pos > adev->rmmio_size)
2035 return result;
2036
2037 r = get_user(value, (uint32_t *)buf);
2038 if (r)
2039 return r;
2040
2041 WREG32(*pos >> 2, value);
2042
2043 result += 4;
2044 buf += 4;
2045 *pos += 4;
2046 size -= 4;
2047 }
2048
2049 return result;
2050}
2051
2052static const struct file_operations amdgpu_debugfs_regs_fops = {
2053 .owner = THIS_MODULE,
2054 .read = amdgpu_debugfs_regs_read,
2055 .write = amdgpu_debugfs_regs_write,
2056 .llseek = default_llseek
2057};
2058
2059static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
2060{
2061 struct drm_minor *minor = adev->ddev->primary;
2062 struct dentry *ent, *root = minor->debugfs_root;
2063
2064 ent = debugfs_create_file("amdgpu_regs", S_IFREG | S_IRUGO, root,
2065 adev, &amdgpu_debugfs_regs_fops);
2066 if (IS_ERR(ent))
2067 return PTR_ERR(ent);
2068 i_size_write(ent->d_inode, adev->rmmio_size);
2069 adev->debugfs_regs = ent;
2070
2071 return 0;
2072}
2073
2074static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
2075{
2076 debugfs_remove(adev->debugfs_regs);
2077 adev->debugfs_regs = NULL;
2078}
2079
2080int amdgpu_debugfs_init(struct drm_minor *minor)
2081{
2082 return 0;
2083}
2084
2085void amdgpu_debugfs_cleanup(struct drm_minor *minor)
2086{
2087}
7cebc728
AK
2088#else
2089static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
2090{
2091 return 0;
2092}
2093static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
d38ceaf9 2094#endif