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