GNU Linux-libre 4.14.330-gnu1
[releases.git] / drivers / gpu / drm / radeon / radeon_device.c
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 <drm/drmP.h>
31 #include <drm/drm_crtc_helper.h>
32 #include <drm/radeon_drm.h>
33 #include <linux/pm_runtime.h>
34 #include <linux/vgaarb.h>
35 #include <linux/vga_switcheroo.h>
36 #include <linux/efi.h>
37 #include "radeon_reg.h"
38 #include "radeon.h"
39 #include "atom.h"
40
41 static const char radeon_family_name[][16] = {
42         "R100",
43         "RV100",
44         "RS100",
45         "RV200",
46         "RS200",
47         "R200",
48         "RV250",
49         "RS300",
50         "RV280",
51         "R300",
52         "R350",
53         "RV350",
54         "RV380",
55         "R420",
56         "R423",
57         "RV410",
58         "RS400",
59         "RS480",
60         "RS600",
61         "RS690",
62         "RS740",
63         "RV515",
64         "R520",
65         "RV530",
66         "RV560",
67         "RV570",
68         "R580",
69         "R600",
70         "RV610",
71         "RV630",
72         "RV670",
73         "RV620",
74         "RV635",
75         "RS780",
76         "RS880",
77         "RV770",
78         "RV730",
79         "RV710",
80         "RV740",
81         "CEDAR",
82         "REDWOOD",
83         "JUNIPER",
84         "CYPRESS",
85         "HEMLOCK",
86         "PALM",
87         "SUMO",
88         "SUMO2",
89         "BARTS",
90         "TURKS",
91         "CAICOS",
92         "CAYMAN",
93         "ARUBA",
94         "TAHITI",
95         "PITCAIRN",
96         "VERDE",
97         "OLAND",
98         "HAINAN",
99         "BONAIRE",
100         "KAVERI",
101         "KABINI",
102         "HAWAII",
103         "MULLINS",
104         "LAST",
105 };
106
107 #if defined(CONFIG_VGA_SWITCHEROO)
108 bool radeon_has_atpx_dgpu_power_cntl(void);
109 bool radeon_is_atpx_hybrid(void);
110 #else
111 static inline bool radeon_has_atpx_dgpu_power_cntl(void) { return false; }
112 static inline bool radeon_is_atpx_hybrid(void) { return false; }
113 #endif
114
115 #define RADEON_PX_QUIRK_DISABLE_PX  (1 << 0)
116
117 struct radeon_px_quirk {
118         u32 chip_vendor;
119         u32 chip_device;
120         u32 subsys_vendor;
121         u32 subsys_device;
122         u32 px_quirk_flags;
123 };
124
125 static struct radeon_px_quirk radeon_px_quirk_list[] = {
126         /* Acer aspire 5560g (CPU: AMD A4-3305M; GPU: AMD Radeon HD 6480g + 7470m)
127          * https://bugzilla.kernel.org/show_bug.cgi?id=74551
128          */
129         { PCI_VENDOR_ID_ATI, 0x6760, 0x1025, 0x0672, RADEON_PX_QUIRK_DISABLE_PX },
130         /* Asus K73TA laptop with AMD A6-3400M APU and Radeon 6550 GPU
131          * https://bugzilla.kernel.org/show_bug.cgi?id=51381
132          */
133         { PCI_VENDOR_ID_ATI, 0x6741, 0x1043, 0x108c, RADEON_PX_QUIRK_DISABLE_PX },
134         /* Asus K53TK laptop with AMD A6-3420M APU and Radeon 7670m GPU
135          * https://bugzilla.kernel.org/show_bug.cgi?id=51381
136          */
137         { PCI_VENDOR_ID_ATI, 0x6840, 0x1043, 0x2122, RADEON_PX_QUIRK_DISABLE_PX },
138         /* Asus K53TK laptop with AMD A6-3420M APU and Radeon 7670m GPU
139          * https://bugs.freedesktop.org/show_bug.cgi?id=101491
140          */
141         { PCI_VENDOR_ID_ATI, 0x6741, 0x1043, 0x2122, RADEON_PX_QUIRK_DISABLE_PX },
142         /* Asus K73TK laptop with AMD A6-3420M APU and Radeon 7670m GPU
143          * https://bugzilla.kernel.org/show_bug.cgi?id=51381#c52
144          */
145         { PCI_VENDOR_ID_ATI, 0x6840, 0x1043, 0x2123, RADEON_PX_QUIRK_DISABLE_PX },
146         { 0, 0, 0, 0, 0 },
147 };
148
149 bool radeon_is_px(struct drm_device *dev)
150 {
151         struct radeon_device *rdev = dev->dev_private;
152
153         if (rdev->flags & RADEON_IS_PX)
154                 return true;
155         return false;
156 }
157
158 static void radeon_device_handle_px_quirks(struct radeon_device *rdev)
159 {
160         struct radeon_px_quirk *p = radeon_px_quirk_list;
161
162         /* Apply PX quirks */
163         while (p && p->chip_device != 0) {
164                 if (rdev->pdev->vendor == p->chip_vendor &&
165                     rdev->pdev->device == p->chip_device &&
166                     rdev->pdev->subsystem_vendor == p->subsys_vendor &&
167                     rdev->pdev->subsystem_device == p->subsys_device) {
168                         rdev->px_quirk_flags = p->px_quirk_flags;
169                         break;
170                 }
171                 ++p;
172         }
173
174         if (rdev->px_quirk_flags & RADEON_PX_QUIRK_DISABLE_PX)
175                 rdev->flags &= ~RADEON_IS_PX;
176
177         /* disable PX is the system doesn't support dGPU power control or hybrid gfx */
178         if (!radeon_is_atpx_hybrid() &&
179             !radeon_has_atpx_dgpu_power_cntl())
180                 rdev->flags &= ~RADEON_IS_PX;
181 }
182
183 /**
184  * radeon_program_register_sequence - program an array of registers.
185  *
186  * @rdev: radeon_device pointer
187  * @registers: pointer to the register array
188  * @array_size: size of the register array
189  *
190  * Programs an array or registers with and and or masks.
191  * This is a helper for setting golden registers.
192  */
193 void radeon_program_register_sequence(struct radeon_device *rdev,
194                                       const u32 *registers,
195                                       const u32 array_size)
196 {
197         u32 tmp, reg, and_mask, or_mask;
198         int i;
199
200         if (array_size % 3)
201                 return;
202
203         for (i = 0; i < array_size; i +=3) {
204                 reg = registers[i + 0];
205                 and_mask = registers[i + 1];
206                 or_mask = registers[i + 2];
207
208                 if (and_mask == 0xffffffff) {
209                         tmp = or_mask;
210                 } else {
211                         tmp = RREG32(reg);
212                         tmp &= ~and_mask;
213                         tmp |= or_mask;
214                 }
215                 WREG32(reg, tmp);
216         }
217 }
218
219 void radeon_pci_config_reset(struct radeon_device *rdev)
220 {
221         pci_write_config_dword(rdev->pdev, 0x7c, RADEON_ASIC_RESET_DATA);
222 }
223
224 /**
225  * radeon_surface_init - Clear GPU surface registers.
226  *
227  * @rdev: radeon_device pointer
228  *
229  * Clear GPU surface registers (r1xx-r5xx).
230  */
231 void radeon_surface_init(struct radeon_device *rdev)
232 {
233         /* FIXME: check this out */
234         if (rdev->family < CHIP_R600) {
235                 int i;
236
237                 for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
238                         if (rdev->surface_regs[i].bo)
239                                 radeon_bo_get_surface_reg(rdev->surface_regs[i].bo);
240                         else
241                                 radeon_clear_surface_reg(rdev, i);
242                 }
243                 /* enable surfaces */
244                 WREG32(RADEON_SURFACE_CNTL, 0);
245         }
246 }
247
248 /*
249  * GPU scratch registers helpers function.
250  */
251 /**
252  * radeon_scratch_init - Init scratch register driver information.
253  *
254  * @rdev: radeon_device pointer
255  *
256  * Init CP scratch register driver information (r1xx-r5xx)
257  */
258 void radeon_scratch_init(struct radeon_device *rdev)
259 {
260         int i;
261
262         /* FIXME: check this out */
263         if (rdev->family < CHIP_R300) {
264                 rdev->scratch.num_reg = 5;
265         } else {
266                 rdev->scratch.num_reg = 7;
267         }
268         rdev->scratch.reg_base = RADEON_SCRATCH_REG0;
269         for (i = 0; i < rdev->scratch.num_reg; i++) {
270                 rdev->scratch.free[i] = true;
271                 rdev->scratch.reg[i] = rdev->scratch.reg_base + (i * 4);
272         }
273 }
274
275 /**
276  * radeon_scratch_get - Allocate a scratch register
277  *
278  * @rdev: radeon_device pointer
279  * @reg: scratch register mmio offset
280  *
281  * Allocate a CP scratch register for use by the driver (all asics).
282  * Returns 0 on success or -EINVAL on failure.
283  */
284 int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg)
285 {
286         int i;
287
288         for (i = 0; i < rdev->scratch.num_reg; i++) {
289                 if (rdev->scratch.free[i]) {
290                         rdev->scratch.free[i] = false;
291                         *reg = rdev->scratch.reg[i];
292                         return 0;
293                 }
294         }
295         return -EINVAL;
296 }
297
298 /**
299  * radeon_scratch_free - Free a scratch register
300  *
301  * @rdev: radeon_device pointer
302  * @reg: scratch register mmio offset
303  *
304  * Free a CP scratch register allocated for use by the driver (all asics)
305  */
306 void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg)
307 {
308         int i;
309
310         for (i = 0; i < rdev->scratch.num_reg; i++) {
311                 if (rdev->scratch.reg[i] == reg) {
312                         rdev->scratch.free[i] = true;
313                         return;
314                 }
315         }
316 }
317
318 /*
319  * GPU doorbell aperture helpers function.
320  */
321 /**
322  * radeon_doorbell_init - Init doorbell driver information.
323  *
324  * @rdev: radeon_device pointer
325  *
326  * Init doorbell driver information (CIK)
327  * Returns 0 on success, error on failure.
328  */
329 static int radeon_doorbell_init(struct radeon_device *rdev)
330 {
331         /* doorbell bar mapping */
332         rdev->doorbell.base = pci_resource_start(rdev->pdev, 2);
333         rdev->doorbell.size = pci_resource_len(rdev->pdev, 2);
334
335         rdev->doorbell.num_doorbells = min_t(u32, rdev->doorbell.size / sizeof(u32), RADEON_MAX_DOORBELLS);
336         if (rdev->doorbell.num_doorbells == 0)
337                 return -EINVAL;
338
339         rdev->doorbell.ptr = ioremap(rdev->doorbell.base, rdev->doorbell.num_doorbells * sizeof(u32));
340         if (rdev->doorbell.ptr == NULL) {
341                 return -ENOMEM;
342         }
343         DRM_INFO("doorbell mmio base: 0x%08X\n", (uint32_t)rdev->doorbell.base);
344         DRM_INFO("doorbell mmio size: %u\n", (unsigned)rdev->doorbell.size);
345
346         memset(&rdev->doorbell.used, 0, sizeof(rdev->doorbell.used));
347
348         return 0;
349 }
350
351 /**
352  * radeon_doorbell_fini - Tear down doorbell driver information.
353  *
354  * @rdev: radeon_device pointer
355  *
356  * Tear down doorbell driver information (CIK)
357  */
358 static void radeon_doorbell_fini(struct radeon_device *rdev)
359 {
360         iounmap(rdev->doorbell.ptr);
361         rdev->doorbell.ptr = NULL;
362 }
363
364 /**
365  * radeon_doorbell_get - Allocate a doorbell entry
366  *
367  * @rdev: radeon_device pointer
368  * @doorbell: doorbell index
369  *
370  * Allocate a doorbell for use by the driver (all asics).
371  * Returns 0 on success or -EINVAL on failure.
372  */
373 int radeon_doorbell_get(struct radeon_device *rdev, u32 *doorbell)
374 {
375         unsigned long offset = find_first_zero_bit(rdev->doorbell.used, rdev->doorbell.num_doorbells);
376         if (offset < rdev->doorbell.num_doorbells) {
377                 __set_bit(offset, rdev->doorbell.used);
378                 *doorbell = offset;
379                 return 0;
380         } else {
381                 return -EINVAL;
382         }
383 }
384
385 /**
386  * radeon_doorbell_free - Free a doorbell entry
387  *
388  * @rdev: radeon_device pointer
389  * @doorbell: doorbell index
390  *
391  * Free a doorbell allocated for use by the driver (all asics)
392  */
393 void radeon_doorbell_free(struct radeon_device *rdev, u32 doorbell)
394 {
395         if (doorbell < rdev->doorbell.num_doorbells)
396                 __clear_bit(doorbell, rdev->doorbell.used);
397 }
398
399 /**
400  * radeon_doorbell_get_kfd_info - Report doorbell configuration required to
401  *                                setup KFD
402  *
403  * @rdev: radeon_device pointer
404  * @aperture_base: output returning doorbell aperture base physical address
405  * @aperture_size: output returning doorbell aperture size in bytes
406  * @start_offset: output returning # of doorbell bytes reserved for radeon.
407  *
408  * Radeon and the KFD share the doorbell aperture. Radeon sets it up,
409  * takes doorbells required for its own rings and reports the setup to KFD.
410  * Radeon reserved doorbells are at the start of the doorbell aperture.
411  */
412 void radeon_doorbell_get_kfd_info(struct radeon_device *rdev,
413                                   phys_addr_t *aperture_base,
414                                   size_t *aperture_size,
415                                   size_t *start_offset)
416 {
417         /* The first num_doorbells are used by radeon.
418          * KFD takes whatever's left in the aperture. */
419         if (rdev->doorbell.size > rdev->doorbell.num_doorbells * sizeof(u32)) {
420                 *aperture_base = rdev->doorbell.base;
421                 *aperture_size = rdev->doorbell.size;
422                 *start_offset = rdev->doorbell.num_doorbells * sizeof(u32);
423         } else {
424                 *aperture_base = 0;
425                 *aperture_size = 0;
426                 *start_offset = 0;
427         }
428 }
429
430 /*
431  * radeon_wb_*()
432  * Writeback is the the method by which the the GPU updates special pages
433  * in memory with the status of certain GPU events (fences, ring pointers,
434  * etc.).
435  */
436
437 /**
438  * radeon_wb_disable - Disable Writeback
439  *
440  * @rdev: radeon_device pointer
441  *
442  * Disables Writeback (all asics).  Used for suspend.
443  */
444 void radeon_wb_disable(struct radeon_device *rdev)
445 {
446         rdev->wb.enabled = false;
447 }
448
449 /**
450  * radeon_wb_fini - Disable Writeback and free memory
451  *
452  * @rdev: radeon_device pointer
453  *
454  * Disables Writeback and frees the Writeback memory (all asics).
455  * Used at driver shutdown.
456  */
457 void radeon_wb_fini(struct radeon_device *rdev)
458 {
459         radeon_wb_disable(rdev);
460         if (rdev->wb.wb_obj) {
461                 if (!radeon_bo_reserve(rdev->wb.wb_obj, false)) {
462                         radeon_bo_kunmap(rdev->wb.wb_obj);
463                         radeon_bo_unpin(rdev->wb.wb_obj);
464                         radeon_bo_unreserve(rdev->wb.wb_obj);
465                 }
466                 radeon_bo_unref(&rdev->wb.wb_obj);
467                 rdev->wb.wb = NULL;
468                 rdev->wb.wb_obj = NULL;
469         }
470 }
471
472 /**
473  * radeon_wb_init- Init Writeback driver info and allocate memory
474  *
475  * @rdev: radeon_device pointer
476  *
477  * Disables Writeback and frees the Writeback memory (all asics).
478  * Used at driver startup.
479  * Returns 0 on success or an -error on failure.
480  */
481 int radeon_wb_init(struct radeon_device *rdev)
482 {
483         int r;
484
485         if (rdev->wb.wb_obj == NULL) {
486                 r = radeon_bo_create(rdev, RADEON_GPU_PAGE_SIZE, PAGE_SIZE, true,
487                                      RADEON_GEM_DOMAIN_GTT, 0, NULL, NULL,
488                                      &rdev->wb.wb_obj);
489                 if (r) {
490                         dev_warn(rdev->dev, "(%d) create WB bo failed\n", r);
491                         return r;
492                 }
493                 r = radeon_bo_reserve(rdev->wb.wb_obj, false);
494                 if (unlikely(r != 0)) {
495                         radeon_wb_fini(rdev);
496                         return r;
497                 }
498                 r = radeon_bo_pin(rdev->wb.wb_obj, RADEON_GEM_DOMAIN_GTT,
499                                 &rdev->wb.gpu_addr);
500                 if (r) {
501                         radeon_bo_unreserve(rdev->wb.wb_obj);
502                         dev_warn(rdev->dev, "(%d) pin WB bo failed\n", r);
503                         radeon_wb_fini(rdev);
504                         return r;
505                 }
506                 r = radeon_bo_kmap(rdev->wb.wb_obj, (void **)&rdev->wb.wb);
507                 radeon_bo_unreserve(rdev->wb.wb_obj);
508                 if (r) {
509                         dev_warn(rdev->dev, "(%d) map WB bo failed\n", r);
510                         radeon_wb_fini(rdev);
511                         return r;
512                 }
513         }
514
515         /* clear wb memory */
516         memset((char *)rdev->wb.wb, 0, RADEON_GPU_PAGE_SIZE);
517         /* disable event_write fences */
518         rdev->wb.use_event = false;
519         /* disabled via module param */
520         if (radeon_no_wb == 1) {
521                 rdev->wb.enabled = false;
522         } else {
523                 if (rdev->flags & RADEON_IS_AGP) {
524                         /* often unreliable on AGP */
525                         rdev->wb.enabled = false;
526                 } else if (rdev->family < CHIP_R300) {
527                         /* often unreliable on pre-r300 */
528                         rdev->wb.enabled = false;
529                 } else {
530                         rdev->wb.enabled = true;
531                         /* event_write fences are only available on r600+ */
532                         if (rdev->family >= CHIP_R600) {
533                                 rdev->wb.use_event = true;
534                         }
535                 }
536         }
537         /* always use writeback/events on NI, APUs */
538         if (rdev->family >= CHIP_PALM) {
539                 rdev->wb.enabled = true;
540                 rdev->wb.use_event = true;
541         }
542
543         dev_info(rdev->dev, "WB %sabled\n", rdev->wb.enabled ? "en" : "dis");
544
545         return 0;
546 }
547
548 /**
549  * radeon_vram_location - try to find VRAM location
550  * @rdev: radeon device structure holding all necessary informations
551  * @mc: memory controller structure holding memory informations
552  * @base: base address at which to put VRAM
553  *
554  * Function will place try to place VRAM at base address provided
555  * as parameter (which is so far either PCI aperture address or
556  * for IGP TOM base address).
557  *
558  * If there is not enough space to fit the unvisible VRAM in the 32bits
559  * address space then we limit the VRAM size to the aperture.
560  *
561  * If we are using AGP and if the AGP aperture doesn't allow us to have
562  * room for all the VRAM than we restrict the VRAM to the PCI aperture
563  * size and print a warning.
564  *
565  * This function will never fails, worst case are limiting VRAM.
566  *
567  * Note: GTT start, end, size should be initialized before calling this
568  * function on AGP platform.
569  *
570  * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
571  * this shouldn't be a problem as we are using the PCI aperture as a reference.
572  * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
573  * not IGP.
574  *
575  * Note: we use mc_vram_size as on some board we need to program the mc to
576  * cover the whole aperture even if VRAM size is inferior to aperture size
577  * Novell bug 204882 + along with lots of ubuntu ones
578  *
579  * Note: when limiting vram it's safe to overwritte real_vram_size because
580  * we are not in case where real_vram_size is inferior to mc_vram_size (ie
581  * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
582  * ones)
583  *
584  * Note: IGP TOM addr should be the same as the aperture addr, we don't
585  * explicitly check for that thought.
586  *
587  * FIXME: when reducing VRAM size align new size on power of 2.
588  */
589 void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base)
590 {
591         uint64_t limit = (uint64_t)radeon_vram_limit << 20;
592
593         mc->vram_start = base;
594         if (mc->mc_vram_size > (rdev->mc.mc_mask - base + 1)) {
595                 dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
596                 mc->real_vram_size = mc->aper_size;
597                 mc->mc_vram_size = mc->aper_size;
598         }
599         mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
600         if (rdev->flags & RADEON_IS_AGP && mc->vram_end > mc->gtt_start && mc->vram_start <= mc->gtt_end) {
601                 dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
602                 mc->real_vram_size = mc->aper_size;
603                 mc->mc_vram_size = mc->aper_size;
604         }
605         mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
606         if (limit && limit < mc->real_vram_size)
607                 mc->real_vram_size = limit;
608         dev_info(rdev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
609                         mc->mc_vram_size >> 20, mc->vram_start,
610                         mc->vram_end, mc->real_vram_size >> 20);
611 }
612
613 /**
614  * radeon_gtt_location - try to find GTT location
615  * @rdev: radeon device structure holding all necessary informations
616  * @mc: memory controller structure holding memory informations
617  *
618  * Function will place try to place GTT before or after VRAM.
619  *
620  * If GTT size is bigger than space left then we ajust GTT size.
621  * Thus function will never fails.
622  *
623  * FIXME: when reducing GTT size align new size on power of 2.
624  */
625 void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc)
626 {
627         u64 size_af, size_bf;
628
629         size_af = ((rdev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
630         size_bf = mc->vram_start & ~mc->gtt_base_align;
631         if (size_bf > size_af) {
632                 if (mc->gtt_size > size_bf) {
633                         dev_warn(rdev->dev, "limiting GTT\n");
634                         mc->gtt_size = size_bf;
635                 }
636                 mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
637         } else {
638                 if (mc->gtt_size > size_af) {
639                         dev_warn(rdev->dev, "limiting GTT\n");
640                         mc->gtt_size = size_af;
641                 }
642                 mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
643         }
644         mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
645         dev_info(rdev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
646                         mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
647 }
648
649 /*
650  * GPU helpers function.
651  */
652
653 /**
654  * radeon_device_is_virtual - check if we are running is a virtual environment
655  *
656  * Check if the asic has been passed through to a VM (all asics).
657  * Used at driver startup.
658  * Returns true if virtual or false if not.
659  */
660 bool radeon_device_is_virtual(void)
661 {
662 #ifdef CONFIG_X86
663         return boot_cpu_has(X86_FEATURE_HYPERVISOR);
664 #else
665         return false;
666 #endif
667 }
668
669 /**
670  * radeon_card_posted - check if the hw has already been initialized
671  *
672  * @rdev: radeon_device pointer
673  *
674  * Check if the asic has been initialized (all asics).
675  * Used at driver startup.
676  * Returns true if initialized or false if not.
677  */
678 bool radeon_card_posted(struct radeon_device *rdev)
679 {
680         uint32_t reg;
681
682         /* for pass through, always force asic_init for CI */
683         if (rdev->family >= CHIP_BONAIRE &&
684             radeon_device_is_virtual())
685                 return false;
686
687         /* required for EFI mode on macbook2,1 which uses an r5xx asic */
688         if (efi_enabled(EFI_BOOT) &&
689             (rdev->pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE) &&
690             (rdev->family < CHIP_R600))
691                 return false;
692
693         if (ASIC_IS_NODCE(rdev))
694                 goto check_memsize;
695
696         /* first check CRTCs */
697         if (ASIC_IS_DCE4(rdev)) {
698                 reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
699                         RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET);
700                         if (rdev->num_crtc >= 4) {
701                                 reg |= RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) |
702                                         RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET);
703                         }
704                         if (rdev->num_crtc >= 6) {
705                                 reg |= RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) |
706                                         RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET);
707                         }
708                 if (reg & EVERGREEN_CRTC_MASTER_EN)
709                         return true;
710         } else if (ASIC_IS_AVIVO(rdev)) {
711                 reg = RREG32(AVIVO_D1CRTC_CONTROL) |
712                       RREG32(AVIVO_D2CRTC_CONTROL);
713                 if (reg & AVIVO_CRTC_EN) {
714                         return true;
715                 }
716         } else {
717                 reg = RREG32(RADEON_CRTC_GEN_CNTL) |
718                       RREG32(RADEON_CRTC2_GEN_CNTL);
719                 if (reg & RADEON_CRTC_EN) {
720                         return true;
721                 }
722         }
723
724 check_memsize:
725         /* then check MEM_SIZE, in case the crtcs are off */
726         if (rdev->family >= CHIP_R600)
727                 reg = RREG32(R600_CONFIG_MEMSIZE);
728         else
729                 reg = RREG32(RADEON_CONFIG_MEMSIZE);
730
731         if (reg)
732                 return true;
733
734         return false;
735
736 }
737
738 /**
739  * radeon_update_bandwidth_info - update display bandwidth params
740  *
741  * @rdev: radeon_device pointer
742  *
743  * Used when sclk/mclk are switched or display modes are set.
744  * params are used to calculate display watermarks (all asics)
745  */
746 void radeon_update_bandwidth_info(struct radeon_device *rdev)
747 {
748         fixed20_12 a;
749         u32 sclk = rdev->pm.current_sclk;
750         u32 mclk = rdev->pm.current_mclk;
751
752         /* sclk/mclk in Mhz */
753         a.full = dfixed_const(100);
754         rdev->pm.sclk.full = dfixed_const(sclk);
755         rdev->pm.sclk.full = dfixed_div(rdev->pm.sclk, a);
756         rdev->pm.mclk.full = dfixed_const(mclk);
757         rdev->pm.mclk.full = dfixed_div(rdev->pm.mclk, a);
758
759         if (rdev->flags & RADEON_IS_IGP) {
760                 a.full = dfixed_const(16);
761                 /* core_bandwidth = sclk(Mhz) * 16 */
762                 rdev->pm.core_bandwidth.full = dfixed_div(rdev->pm.sclk, a);
763         }
764 }
765
766 /**
767  * radeon_boot_test_post_card - check and possibly initialize the hw
768  *
769  * @rdev: radeon_device pointer
770  *
771  * Check if the asic is initialized and if not, attempt to initialize
772  * it (all asics).
773  * Returns true if initialized or false if not.
774  */
775 bool radeon_boot_test_post_card(struct radeon_device *rdev)
776 {
777         if (radeon_card_posted(rdev))
778                 return true;
779
780         if (rdev->bios) {
781                 DRM_INFO("GPU not posted. posting now...\n");
782                 if (rdev->is_atom_bios)
783                         atom_asic_init(rdev->mode_info.atom_context);
784                 else
785                         radeon_combios_asic_init(rdev->ddev);
786                 return true;
787         } else {
788                 dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
789                 return false;
790         }
791 }
792
793 /**
794  * radeon_dummy_page_init - init dummy page used by the driver
795  *
796  * @rdev: radeon_device pointer
797  *
798  * Allocate the dummy page used by the driver (all asics).
799  * This dummy page is used by the driver as a filler for gart entries
800  * when pages are taken out of the GART
801  * Returns 0 on sucess, -ENOMEM on failure.
802  */
803 int radeon_dummy_page_init(struct radeon_device *rdev)
804 {
805         if (rdev->dummy_page.page)
806                 return 0;
807         rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
808         if (rdev->dummy_page.page == NULL)
809                 return -ENOMEM;
810         rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page,
811                                         0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
812         if (pci_dma_mapping_error(rdev->pdev, rdev->dummy_page.addr)) {
813                 dev_err(&rdev->pdev->dev, "Failed to DMA MAP the dummy page\n");
814                 __free_page(rdev->dummy_page.page);
815                 rdev->dummy_page.page = NULL;
816                 return -ENOMEM;
817         }
818         rdev->dummy_page.entry = radeon_gart_get_page_entry(rdev->dummy_page.addr,
819                                                             RADEON_GART_PAGE_DUMMY);
820         return 0;
821 }
822
823 /**
824  * radeon_dummy_page_fini - free dummy page used by the driver
825  *
826  * @rdev: radeon_device pointer
827  *
828  * Frees the dummy page used by the driver (all asics).
829  */
830 void radeon_dummy_page_fini(struct radeon_device *rdev)
831 {
832         if (rdev->dummy_page.page == NULL)
833                 return;
834         pci_unmap_page(rdev->pdev, rdev->dummy_page.addr,
835                         PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
836         __free_page(rdev->dummy_page.page);
837         rdev->dummy_page.page = NULL;
838 }
839
840
841 /* ATOM accessor methods */
842 /*
843  * ATOM is an interpreted byte code stored in tables in the vbios.  The
844  * driver registers callbacks to access registers and the interpreter
845  * in the driver parses the tables and executes then to program specific
846  * actions (set display modes, asic init, etc.).  See radeon_atombios.c,
847  * atombios.h, and atom.c
848  */
849
850 /**
851  * cail_pll_read - read PLL register
852  *
853  * @info: atom card_info pointer
854  * @reg: PLL register offset
855  *
856  * Provides a PLL register accessor for the atom interpreter (r4xx+).
857  * Returns the value of the PLL register.
858  */
859 static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
860 {
861         struct radeon_device *rdev = info->dev->dev_private;
862         uint32_t r;
863
864         r = rdev->pll_rreg(rdev, reg);
865         return r;
866 }
867
868 /**
869  * cail_pll_write - write PLL register
870  *
871  * @info: atom card_info pointer
872  * @reg: PLL register offset
873  * @val: value to write to the pll register
874  *
875  * Provides a PLL register accessor for the atom interpreter (r4xx+).
876  */
877 static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
878 {
879         struct radeon_device *rdev = info->dev->dev_private;
880
881         rdev->pll_wreg(rdev, reg, val);
882 }
883
884 /**
885  * cail_mc_read - read MC (Memory Controller) register
886  *
887  * @info: atom card_info pointer
888  * @reg: MC register offset
889  *
890  * Provides an MC register accessor for the atom interpreter (r4xx+).
891  * Returns the value of the MC register.
892  */
893 static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
894 {
895         struct radeon_device *rdev = info->dev->dev_private;
896         uint32_t r;
897
898         r = rdev->mc_rreg(rdev, reg);
899         return r;
900 }
901
902 /**
903  * cail_mc_write - write MC (Memory Controller) register
904  *
905  * @info: atom card_info pointer
906  * @reg: MC register offset
907  * @val: value to write to the pll register
908  *
909  * Provides a MC register accessor for the atom interpreter (r4xx+).
910  */
911 static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
912 {
913         struct radeon_device *rdev = info->dev->dev_private;
914
915         rdev->mc_wreg(rdev, reg, val);
916 }
917
918 /**
919  * cail_reg_write - write MMIO register
920  *
921  * @info: atom card_info pointer
922  * @reg: MMIO register offset
923  * @val: value to write to the pll register
924  *
925  * Provides a MMIO register accessor for the atom interpreter (r4xx+).
926  */
927 static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
928 {
929         struct radeon_device *rdev = info->dev->dev_private;
930
931         WREG32(reg*4, val);
932 }
933
934 /**
935  * cail_reg_read - read MMIO register
936  *
937  * @info: atom card_info pointer
938  * @reg: MMIO register offset
939  *
940  * Provides an MMIO register accessor for the atom interpreter (r4xx+).
941  * Returns the value of the MMIO register.
942  */
943 static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
944 {
945         struct radeon_device *rdev = info->dev->dev_private;
946         uint32_t r;
947
948         r = RREG32(reg*4);
949         return r;
950 }
951
952 /**
953  * cail_ioreg_write - write IO register
954  *
955  * @info: atom card_info pointer
956  * @reg: IO register offset
957  * @val: value to write to the pll register
958  *
959  * Provides a IO register accessor for the atom interpreter (r4xx+).
960  */
961 static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
962 {
963         struct radeon_device *rdev = info->dev->dev_private;
964
965         WREG32_IO(reg*4, val);
966 }
967
968 /**
969  * cail_ioreg_read - read IO register
970  *
971  * @info: atom card_info pointer
972  * @reg: IO register offset
973  *
974  * Provides an IO register accessor for the atom interpreter (r4xx+).
975  * Returns the value of the IO register.
976  */
977 static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
978 {
979         struct radeon_device *rdev = info->dev->dev_private;
980         uint32_t r;
981
982         r = RREG32_IO(reg*4);
983         return r;
984 }
985
986 /**
987  * radeon_atombios_init - init the driver info and callbacks for atombios
988  *
989  * @rdev: radeon_device pointer
990  *
991  * Initializes the driver info and register access callbacks for the
992  * ATOM interpreter (r4xx+).
993  * Returns 0 on sucess, -ENOMEM on failure.
994  * Called at driver startup.
995  */
996 int radeon_atombios_init(struct radeon_device *rdev)
997 {
998         struct card_info *atom_card_info =
999             kzalloc(sizeof(struct card_info), GFP_KERNEL);
1000
1001         if (!atom_card_info)
1002                 return -ENOMEM;
1003
1004         rdev->mode_info.atom_card_info = atom_card_info;
1005         atom_card_info->dev = rdev->ddev;
1006         atom_card_info->reg_read = cail_reg_read;
1007         atom_card_info->reg_write = cail_reg_write;
1008         /* needed for iio ops */
1009         if (rdev->rio_mem) {
1010                 atom_card_info->ioreg_read = cail_ioreg_read;
1011                 atom_card_info->ioreg_write = cail_ioreg_write;
1012         } else {
1013                 DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
1014                 atom_card_info->ioreg_read = cail_reg_read;
1015                 atom_card_info->ioreg_write = cail_reg_write;
1016         }
1017         atom_card_info->mc_read = cail_mc_read;
1018         atom_card_info->mc_write = cail_mc_write;
1019         atom_card_info->pll_read = cail_pll_read;
1020         atom_card_info->pll_write = cail_pll_write;
1021
1022         rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios);
1023         if (!rdev->mode_info.atom_context) {
1024                 radeon_atombios_fini(rdev);
1025                 return -ENOMEM;
1026         }
1027
1028         mutex_init(&rdev->mode_info.atom_context->mutex);
1029         mutex_init(&rdev->mode_info.atom_context->scratch_mutex);
1030         radeon_atom_initialize_bios_scratch_regs(rdev->ddev);
1031         atom_allocate_fb_scratch(rdev->mode_info.atom_context);
1032         return 0;
1033 }
1034
1035 /**
1036  * radeon_atombios_fini - free the driver info and callbacks for atombios
1037  *
1038  * @rdev: radeon_device pointer
1039  *
1040  * Frees the driver info and register access callbacks for the ATOM
1041  * interpreter (r4xx+).
1042  * Called at driver shutdown.
1043  */
1044 void radeon_atombios_fini(struct radeon_device *rdev)
1045 {
1046         if (rdev->mode_info.atom_context) {
1047                 kfree(rdev->mode_info.atom_context->scratch);
1048                 kfree(rdev->mode_info.atom_context->iio);
1049         }
1050         kfree(rdev->mode_info.atom_context);
1051         rdev->mode_info.atom_context = NULL;
1052         kfree(rdev->mode_info.atom_card_info);
1053         rdev->mode_info.atom_card_info = NULL;
1054 }
1055
1056 /* COMBIOS */
1057 /*
1058  * COMBIOS is the bios format prior to ATOM. It provides
1059  * command tables similar to ATOM, but doesn't have a unified
1060  * parser.  See radeon_combios.c
1061  */
1062
1063 /**
1064  * radeon_combios_init - init the driver info for combios
1065  *
1066  * @rdev: radeon_device pointer
1067  *
1068  * Initializes the driver info for combios (r1xx-r3xx).
1069  * Returns 0 on sucess.
1070  * Called at driver startup.
1071  */
1072 int radeon_combios_init(struct radeon_device *rdev)
1073 {
1074         radeon_combios_initialize_bios_scratch_regs(rdev->ddev);
1075         return 0;
1076 }
1077
1078 /**
1079  * radeon_combios_fini - free the driver info for combios
1080  *
1081  * @rdev: radeon_device pointer
1082  *
1083  * Frees the driver info for combios (r1xx-r3xx).
1084  * Called at driver shutdown.
1085  */
1086 void radeon_combios_fini(struct radeon_device *rdev)
1087 {
1088 }
1089
1090 /* if we get transitioned to only one device, take VGA back */
1091 /**
1092  * radeon_vga_set_decode - enable/disable vga decode
1093  *
1094  * @cookie: radeon_device pointer
1095  * @state: enable/disable vga decode
1096  *
1097  * Enable/disable vga decode (all asics).
1098  * Returns VGA resource flags.
1099  */
1100 static unsigned int radeon_vga_set_decode(void *cookie, bool state)
1101 {
1102         struct radeon_device *rdev = cookie;
1103         radeon_vga_set_state(rdev, state);
1104         if (state)
1105                 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1106                        VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1107         else
1108                 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1109 }
1110
1111 /**
1112  * radeon_check_pot_argument - check that argument is a power of two
1113  *
1114  * @arg: value to check
1115  *
1116  * Validates that a certain argument is a power of two (all asics).
1117  * Returns true if argument is valid.
1118  */
1119 static bool radeon_check_pot_argument(int arg)
1120 {
1121         return (arg & (arg - 1)) == 0;
1122 }
1123
1124 /**
1125  * Determine a sensible default GART size according to ASIC family.
1126  *
1127  * @family ASIC family name
1128  */
1129 static int radeon_gart_size_auto(enum radeon_family family)
1130 {
1131         /* default to a larger gart size on newer asics */
1132         if (family >= CHIP_TAHITI)
1133                 return 2048;
1134         else if (family >= CHIP_RV770)
1135                 return 1024;
1136         else
1137                 return 512;
1138 }
1139
1140 /**
1141  * radeon_check_arguments - validate module params
1142  *
1143  * @rdev: radeon_device pointer
1144  *
1145  * Validates certain module parameters and updates
1146  * the associated values used by the driver (all asics).
1147  */
1148 static void radeon_check_arguments(struct radeon_device *rdev)
1149 {
1150         /* vramlimit must be a power of two */
1151         if (!radeon_check_pot_argument(radeon_vram_limit)) {
1152                 dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n",
1153                                 radeon_vram_limit);
1154                 radeon_vram_limit = 0;
1155         }
1156
1157         if (radeon_gart_size == -1) {
1158                 radeon_gart_size = radeon_gart_size_auto(rdev->family);
1159         }
1160         /* gtt size must be power of two and greater or equal to 32M */
1161         if (radeon_gart_size < 32) {
1162                 dev_warn(rdev->dev, "gart size (%d) too small\n",
1163                                 radeon_gart_size);
1164                 radeon_gart_size = radeon_gart_size_auto(rdev->family);
1165         } else if (!radeon_check_pot_argument(radeon_gart_size)) {
1166                 dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n",
1167                                 radeon_gart_size);
1168                 radeon_gart_size = radeon_gart_size_auto(rdev->family);
1169         }
1170         rdev->mc.gtt_size = (uint64_t)radeon_gart_size << 20;
1171
1172         /* AGP mode can only be -1, 1, 2, 4, 8 */
1173         switch (radeon_agpmode) {
1174         case -1:
1175         case 0:
1176         case 1:
1177         case 2:
1178         case 4:
1179         case 8:
1180                 break;
1181         default:
1182                 dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: "
1183                                 "-1, 0, 1, 2, 4, 8)\n", radeon_agpmode);
1184                 radeon_agpmode = 0;
1185                 break;
1186         }
1187
1188         if (!radeon_check_pot_argument(radeon_vm_size)) {
1189                 dev_warn(rdev->dev, "VM size (%d) must be a power of 2\n",
1190                          radeon_vm_size);
1191                 radeon_vm_size = 4;
1192         }
1193
1194         if (radeon_vm_size < 1) {
1195                 dev_warn(rdev->dev, "VM size (%d) too small, min is 1GB\n",
1196                          radeon_vm_size);
1197                 radeon_vm_size = 4;
1198         }
1199
1200         /*
1201          * Max GPUVM size for Cayman, SI and CI are 40 bits.
1202          */
1203         if (radeon_vm_size > 1024) {
1204                 dev_warn(rdev->dev, "VM size (%d) too large, max is 1TB\n",
1205                          radeon_vm_size);
1206                 radeon_vm_size = 4;
1207         }
1208
1209         /* defines number of bits in page table versus page directory,
1210          * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1211          * page table and the remaining bits are in the page directory */
1212         if (radeon_vm_block_size == -1) {
1213
1214                 /* Total bits covered by PD + PTs */
1215                 unsigned bits = ilog2(radeon_vm_size) + 18;
1216
1217                 /* Make sure the PD is 4K in size up to 8GB address space.
1218                    Above that split equal between PD and PTs */
1219                 if (radeon_vm_size <= 8)
1220                         radeon_vm_block_size = bits - 9;
1221                 else
1222                         radeon_vm_block_size = (bits + 3) / 2;
1223
1224         } else if (radeon_vm_block_size < 9) {
1225                 dev_warn(rdev->dev, "VM page table size (%d) too small\n",
1226                          radeon_vm_block_size);
1227                 radeon_vm_block_size = 9;
1228         }
1229
1230         if (radeon_vm_block_size > 24 ||
1231             (radeon_vm_size * 1024) < (1ull << radeon_vm_block_size)) {
1232                 dev_warn(rdev->dev, "VM page table size (%d) too large\n",
1233                          radeon_vm_block_size);
1234                 radeon_vm_block_size = 9;
1235         }
1236 }
1237
1238 /**
1239  * radeon_switcheroo_set_state - set switcheroo state
1240  *
1241  * @pdev: pci dev pointer
1242  * @state: vga_switcheroo state
1243  *
1244  * Callback for the switcheroo driver.  Suspends or resumes the
1245  * the asics before or after it is powered up using ACPI methods.
1246  */
1247 static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1248 {
1249         struct drm_device *dev = pci_get_drvdata(pdev);
1250
1251         if (radeon_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1252                 return;
1253
1254         if (state == VGA_SWITCHEROO_ON) {
1255                 pr_info("radeon: switched on\n");
1256                 /* don't suspend or resume card normally */
1257                 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1258
1259                 radeon_resume_kms(dev, true, true);
1260
1261                 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1262                 drm_kms_helper_poll_enable(dev);
1263         } else {
1264                 pr_info("radeon: switched off\n");
1265                 drm_kms_helper_poll_disable(dev);
1266                 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1267                 radeon_suspend_kms(dev, true, true, false);
1268                 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1269         }
1270 }
1271
1272 /**
1273  * radeon_switcheroo_can_switch - see if switcheroo state can change
1274  *
1275  * @pdev: pci dev pointer
1276  *
1277  * Callback for the switcheroo driver.  Check of the switcheroo
1278  * state can be changed.
1279  * Returns true if the state can be changed, false if not.
1280  */
1281 static bool radeon_switcheroo_can_switch(struct pci_dev *pdev)
1282 {
1283         struct drm_device *dev = pci_get_drvdata(pdev);
1284
1285         /*
1286          * FIXME: open_count is protected by drm_global_mutex but that would lead to
1287          * locking inversion with the driver load path. And the access here is
1288          * completely racy anyway. So don't bother with locking for now.
1289          */
1290         return dev->open_count == 0;
1291 }
1292
1293 static const struct vga_switcheroo_client_ops radeon_switcheroo_ops = {
1294         .set_gpu_state = radeon_switcheroo_set_state,
1295         .reprobe = NULL,
1296         .can_switch = radeon_switcheroo_can_switch,
1297 };
1298
1299 /**
1300  * radeon_device_init - initialize the driver
1301  *
1302  * @rdev: radeon_device pointer
1303  * @pdev: drm dev pointer
1304  * @pdev: pci dev pointer
1305  * @flags: driver flags
1306  *
1307  * Initializes the driver info and hw (all asics).
1308  * Returns 0 for success or an error on failure.
1309  * Called at driver startup.
1310  */
1311 int radeon_device_init(struct radeon_device *rdev,
1312                        struct drm_device *ddev,
1313                        struct pci_dev *pdev,
1314                        uint32_t flags)
1315 {
1316         int r, i;
1317         int dma_bits;
1318         bool runtime = false;
1319
1320         rdev->shutdown = false;
1321         rdev->dev = &pdev->dev;
1322         rdev->ddev = ddev;
1323         rdev->pdev = pdev;
1324         rdev->flags = flags;
1325         rdev->family = flags & RADEON_FAMILY_MASK;
1326         rdev->is_atom_bios = false;
1327         rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT;
1328         rdev->mc.gtt_size = 512 * 1024 * 1024;
1329         rdev->accel_working = false;
1330         /* set up ring ids */
1331         for (i = 0; i < RADEON_NUM_RINGS; i++) {
1332                 rdev->ring[i].idx = i;
1333         }
1334         rdev->fence_context = dma_fence_context_alloc(RADEON_NUM_RINGS);
1335
1336         DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1337                  radeon_family_name[rdev->family], pdev->vendor, pdev->device,
1338                  pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
1339
1340         /* mutex initialization are all done here so we
1341          * can recall function without having locking issues */
1342         mutex_init(&rdev->ring_lock);
1343         mutex_init(&rdev->dc_hw_i2c_mutex);
1344         atomic_set(&rdev->ih.lock, 0);
1345         mutex_init(&rdev->gem.mutex);
1346         mutex_init(&rdev->pm.mutex);
1347         mutex_init(&rdev->gpu_clock_mutex);
1348         mutex_init(&rdev->srbm_mutex);
1349         mutex_init(&rdev->grbm_idx_mutex);
1350         init_rwsem(&rdev->pm.mclk_lock);
1351         init_rwsem(&rdev->exclusive_lock);
1352         init_waitqueue_head(&rdev->irq.vblank_queue);
1353         mutex_init(&rdev->mn_lock);
1354         hash_init(rdev->mn_hash);
1355         r = radeon_gem_init(rdev);
1356         if (r)
1357                 return r;
1358
1359         radeon_check_arguments(rdev);
1360         /* Adjust VM size here.
1361          * Max GPUVM size for cayman+ is 40 bits.
1362          */
1363         rdev->vm_manager.max_pfn = radeon_vm_size << 18;
1364
1365         /* Set asic functions */
1366         r = radeon_asic_init(rdev);
1367         if (r)
1368                 return r;
1369
1370         /* all of the newer IGP chips have an internal gart
1371          * However some rs4xx report as AGP, so remove that here.
1372          */
1373         if ((rdev->family >= CHIP_RS400) &&
1374             (rdev->flags & RADEON_IS_IGP)) {
1375                 rdev->flags &= ~RADEON_IS_AGP;
1376         }
1377
1378         if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) {
1379                 radeon_agp_disable(rdev);
1380         }
1381
1382         /* Set the internal MC address mask
1383          * This is the max address of the GPU's
1384          * internal address space.
1385          */
1386         if (rdev->family >= CHIP_CAYMAN)
1387                 rdev->mc.mc_mask = 0xffffffffffULL; /* 40 bit MC */
1388         else if (rdev->family >= CHIP_CEDAR)
1389                 rdev->mc.mc_mask = 0xfffffffffULL; /* 36 bit MC */
1390         else
1391                 rdev->mc.mc_mask = 0xffffffffULL; /* 32 bit MC */
1392
1393         /* set DMA mask + need_dma32 flags.
1394          * PCIE - can handle 40-bits.
1395          * IGP - can handle 40-bits
1396          * AGP - generally dma32 is safest
1397          * PCI - dma32 for legacy pci gart, 40 bits on newer asics
1398          */
1399         rdev->need_dma32 = false;
1400         if (rdev->flags & RADEON_IS_AGP)
1401                 rdev->need_dma32 = true;
1402         if ((rdev->flags & RADEON_IS_PCI) &&
1403             (rdev->family <= CHIP_RS740))
1404                 rdev->need_dma32 = true;
1405 #ifdef CONFIG_PPC64
1406         if (rdev->family == CHIP_CEDAR)
1407                 rdev->need_dma32 = true;
1408 #endif
1409
1410         dma_bits = rdev->need_dma32 ? 32 : 40;
1411         r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
1412         if (r) {
1413                 rdev->need_dma32 = true;
1414                 dma_bits = 32;
1415                 pr_warn("radeon: No suitable DMA available\n");
1416         }
1417         r = pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
1418         if (r) {
1419                 pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(32));
1420                 pr_warn("radeon: No coherent DMA available\n");
1421         }
1422
1423         /* Registers mapping */
1424         /* TODO: block userspace mapping of io register */
1425         spin_lock_init(&rdev->mmio_idx_lock);
1426         spin_lock_init(&rdev->smc_idx_lock);
1427         spin_lock_init(&rdev->pll_idx_lock);
1428         spin_lock_init(&rdev->mc_idx_lock);
1429         spin_lock_init(&rdev->pcie_idx_lock);
1430         spin_lock_init(&rdev->pciep_idx_lock);
1431         spin_lock_init(&rdev->pif_idx_lock);
1432         spin_lock_init(&rdev->cg_idx_lock);
1433         spin_lock_init(&rdev->uvd_idx_lock);
1434         spin_lock_init(&rdev->rcu_idx_lock);
1435         spin_lock_init(&rdev->didt_idx_lock);
1436         spin_lock_init(&rdev->end_idx_lock);
1437         if (rdev->family >= CHIP_BONAIRE) {
1438                 rdev->rmmio_base = pci_resource_start(rdev->pdev, 5);
1439                 rdev->rmmio_size = pci_resource_len(rdev->pdev, 5);
1440         } else {
1441                 rdev->rmmio_base = pci_resource_start(rdev->pdev, 2);
1442                 rdev->rmmio_size = pci_resource_len(rdev->pdev, 2);
1443         }
1444         rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size);
1445         if (rdev->rmmio == NULL)
1446                 return -ENOMEM;
1447
1448         /* doorbell bar mapping */
1449         if (rdev->family >= CHIP_BONAIRE)
1450                 radeon_doorbell_init(rdev);
1451
1452         /* io port mapping */
1453         for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1454                 if (pci_resource_flags(rdev->pdev, i) & IORESOURCE_IO) {
1455                         rdev->rio_mem_size = pci_resource_len(rdev->pdev, i);
1456                         rdev->rio_mem = pci_iomap(rdev->pdev, i, rdev->rio_mem_size);
1457                         break;
1458                 }
1459         }
1460         if (rdev->rio_mem == NULL)
1461                 DRM_ERROR("Unable to find PCI I/O BAR\n");
1462
1463         if (rdev->flags & RADEON_IS_PX)
1464                 radeon_device_handle_px_quirks(rdev);
1465
1466         /* if we have > 1 VGA cards, then disable the radeon VGA resources */
1467         /* this will fail for cards that aren't VGA class devices, just
1468          * ignore it */
1469         vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode);
1470
1471         if (rdev->flags & RADEON_IS_PX)
1472                 runtime = true;
1473         if (!pci_is_thunderbolt_attached(rdev->pdev))
1474                 vga_switcheroo_register_client(rdev->pdev,
1475                                                &radeon_switcheroo_ops, runtime);
1476         if (runtime)
1477                 vga_switcheroo_init_domain_pm_ops(rdev->dev, &rdev->vga_pm_domain);
1478
1479         r = radeon_init(rdev);
1480         if (r)
1481                 goto failed;
1482
1483         r = radeon_gem_debugfs_init(rdev);
1484         if (r) {
1485                 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
1486         }
1487
1488         r = radeon_mst_debugfs_init(rdev);
1489         if (r) {
1490                 DRM_ERROR("registering mst debugfs failed (%d).\n", r);
1491         }
1492
1493         if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) {
1494                 /* Acceleration not working on AGP card try again
1495                  * with fallback to PCI or PCIE GART
1496                  */
1497                 radeon_asic_reset(rdev);
1498                 radeon_fini(rdev);
1499                 radeon_agp_disable(rdev);
1500                 r = radeon_init(rdev);
1501                 if (r)
1502                         goto failed;
1503         }
1504
1505         r = radeon_ib_ring_tests(rdev);
1506         if (r)
1507                 DRM_ERROR("ib ring test failed (%d).\n", r);
1508
1509         /*
1510          * Turks/Thames GPU will freeze whole laptop if DPM is not restarted
1511          * after the CP ring have chew one packet at least. Hence here we stop
1512          * and restart DPM after the radeon_ib_ring_tests().
1513          */
1514         if (rdev->pm.dpm_enabled &&
1515             (rdev->pm.pm_method == PM_METHOD_DPM) &&
1516             (rdev->family == CHIP_TURKS) &&
1517             (rdev->flags & RADEON_IS_MOBILITY)) {
1518                 mutex_lock(&rdev->pm.mutex);
1519                 radeon_dpm_disable(rdev);
1520                 radeon_dpm_enable(rdev);
1521                 mutex_unlock(&rdev->pm.mutex);
1522         }
1523
1524         if ((radeon_testing & 1)) {
1525                 if (rdev->accel_working)
1526                         radeon_test_moves(rdev);
1527                 else
1528                         DRM_INFO("radeon: acceleration disabled, skipping move tests\n");
1529         }
1530         if ((radeon_testing & 2)) {
1531                 if (rdev->accel_working)
1532                         radeon_test_syncing(rdev);
1533                 else
1534                         DRM_INFO("radeon: acceleration disabled, skipping sync tests\n");
1535         }
1536         if (radeon_benchmarking) {
1537                 if (rdev->accel_working)
1538                         radeon_benchmark(rdev, radeon_benchmarking);
1539                 else
1540                         DRM_INFO("radeon: acceleration disabled, skipping benchmarks\n");
1541         }
1542         return 0;
1543
1544 failed:
1545         /* balance pm_runtime_get_sync() in radeon_driver_unload_kms() */
1546         if (radeon_is_px(ddev))
1547                 pm_runtime_put_noidle(ddev->dev);
1548         if (runtime)
1549                 vga_switcheroo_fini_domain_pm_ops(rdev->dev);
1550         return r;
1551 }
1552
1553 /**
1554  * radeon_device_fini - tear down the driver
1555  *
1556  * @rdev: radeon_device pointer
1557  *
1558  * Tear down the driver info (all asics).
1559  * Called at driver shutdown.
1560  */
1561 void radeon_device_fini(struct radeon_device *rdev)
1562 {
1563         DRM_INFO("radeon: finishing device.\n");
1564         rdev->shutdown = true;
1565         /* evict vram memory */
1566         radeon_bo_evict_vram(rdev);
1567         radeon_fini(rdev);
1568         if (!pci_is_thunderbolt_attached(rdev->pdev))
1569                 vga_switcheroo_unregister_client(rdev->pdev);
1570         if (rdev->flags & RADEON_IS_PX)
1571                 vga_switcheroo_fini_domain_pm_ops(rdev->dev);
1572         vga_client_register(rdev->pdev, NULL, NULL, NULL);
1573         if (rdev->rio_mem)
1574                 pci_iounmap(rdev->pdev, rdev->rio_mem);
1575         rdev->rio_mem = NULL;
1576         iounmap(rdev->rmmio);
1577         rdev->rmmio = NULL;
1578         if (rdev->family >= CHIP_BONAIRE)
1579                 radeon_doorbell_fini(rdev);
1580 }
1581
1582
1583 /*
1584  * Suspend & resume.
1585  */
1586 /**
1587  * radeon_suspend_kms - initiate device suspend
1588  *
1589  * @pdev: drm dev pointer
1590  * @state: suspend state
1591  *
1592  * Puts the hw in the suspend state (all asics).
1593  * Returns 0 for success or an error on failure.
1594  * Called at driver suspend.
1595  */
1596 int radeon_suspend_kms(struct drm_device *dev, bool suspend,
1597                        bool fbcon, bool freeze)
1598 {
1599         struct radeon_device *rdev;
1600         struct drm_crtc *crtc;
1601         struct drm_connector *connector;
1602         int i, r;
1603
1604         if (dev == NULL || dev->dev_private == NULL) {
1605                 return -ENODEV;
1606         }
1607
1608         rdev = dev->dev_private;
1609
1610         if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1611                 return 0;
1612
1613         drm_kms_helper_poll_disable(dev);
1614
1615         drm_modeset_lock_all(dev);
1616         /* turn off display hw */
1617         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1618                 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
1619         }
1620         drm_modeset_unlock_all(dev);
1621
1622         /* unpin the front buffers and cursors */
1623         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1624                 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1625                 struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->primary->fb);
1626                 struct radeon_bo *robj;
1627
1628                 if (radeon_crtc->cursor_bo) {
1629                         struct radeon_bo *robj = gem_to_radeon_bo(radeon_crtc->cursor_bo);
1630                         r = radeon_bo_reserve(robj, false);
1631                         if (r == 0) {
1632                                 radeon_bo_unpin(robj);
1633                                 radeon_bo_unreserve(robj);
1634                         }
1635                 }
1636
1637                 if (rfb == NULL || rfb->obj == NULL) {
1638                         continue;
1639                 }
1640                 robj = gem_to_radeon_bo(rfb->obj);
1641                 /* don't unpin kernel fb objects */
1642                 if (!radeon_fbdev_robj_is_fb(rdev, robj)) {
1643                         r = radeon_bo_reserve(robj, false);
1644                         if (r == 0) {
1645                                 radeon_bo_unpin(robj);
1646                                 radeon_bo_unreserve(robj);
1647                         }
1648                 }
1649         }
1650         /* evict vram memory */
1651         radeon_bo_evict_vram(rdev);
1652
1653         /* wait for gpu to finish processing current batch */
1654         for (i = 0; i < RADEON_NUM_RINGS; i++) {
1655                 r = radeon_fence_wait_empty(rdev, i);
1656                 if (r) {
1657                         /* delay GPU reset to resume */
1658                         radeon_fence_driver_force_completion(rdev, i);
1659                 } else {
1660                         /* finish executing delayed work */
1661                         flush_delayed_work(&rdev->fence_drv[i].lockup_work);
1662                 }
1663         }
1664
1665         radeon_save_bios_scratch_regs(rdev);
1666
1667         radeon_suspend(rdev);
1668         radeon_hpd_fini(rdev);
1669         /* evict remaining vram memory
1670          * This second call to evict vram is to evict the gart page table
1671          * using the CPU.
1672          */
1673         radeon_bo_evict_vram(rdev);
1674
1675         radeon_agp_suspend(rdev);
1676
1677         pci_save_state(dev->pdev);
1678         if (freeze && rdev->family >= CHIP_CEDAR && !(rdev->flags & RADEON_IS_IGP)) {
1679                 rdev->asic->asic_reset(rdev, true);
1680                 pci_restore_state(dev->pdev);
1681         } else if (suspend) {
1682                 /* Shut down the device */
1683                 pci_disable_device(dev->pdev);
1684                 pci_set_power_state(dev->pdev, PCI_D3hot);
1685         }
1686
1687         if (fbcon) {
1688                 console_lock();
1689                 radeon_fbdev_set_suspend(rdev, 1);
1690                 console_unlock();
1691         }
1692         return 0;
1693 }
1694
1695 /**
1696  * radeon_resume_kms - initiate device resume
1697  *
1698  * @pdev: drm dev pointer
1699  *
1700  * Bring the hw back to operating state (all asics).
1701  * Returns 0 for success or an error on failure.
1702  * Called at driver resume.
1703  */
1704 int radeon_resume_kms(struct drm_device *dev, bool resume, bool fbcon)
1705 {
1706         struct drm_connector *connector;
1707         struct radeon_device *rdev = dev->dev_private;
1708         struct drm_crtc *crtc;
1709         int r;
1710
1711         if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1712                 return 0;
1713
1714         if (fbcon) {
1715                 console_lock();
1716         }
1717         if (resume) {
1718                 pci_set_power_state(dev->pdev, PCI_D0);
1719                 pci_restore_state(dev->pdev);
1720                 if (pci_enable_device(dev->pdev)) {
1721                         if (fbcon)
1722                                 console_unlock();
1723                         return -1;
1724                 }
1725         }
1726         /* resume AGP if in use */
1727         radeon_agp_resume(rdev);
1728         radeon_resume(rdev);
1729
1730         r = radeon_ib_ring_tests(rdev);
1731         if (r)
1732                 DRM_ERROR("ib ring test failed (%d).\n", r);
1733
1734         if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
1735                 /* do dpm late init */
1736                 r = radeon_pm_late_init(rdev);
1737                 if (r) {
1738                         rdev->pm.dpm_enabled = false;
1739                         DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
1740                 }
1741         } else {
1742                 /* resume old pm late */
1743                 radeon_pm_resume(rdev);
1744         }
1745
1746         radeon_restore_bios_scratch_regs(rdev);
1747
1748         /* pin cursors */
1749         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1750                 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
1751
1752                 if (radeon_crtc->cursor_bo) {
1753                         struct radeon_bo *robj = gem_to_radeon_bo(radeon_crtc->cursor_bo);
1754                         r = radeon_bo_reserve(robj, false);
1755                         if (r == 0) {
1756                                 /* Only 27 bit offset for legacy cursor */
1757                                 r = radeon_bo_pin_restricted(robj,
1758                                                              RADEON_GEM_DOMAIN_VRAM,
1759                                                              ASIC_IS_AVIVO(rdev) ?
1760                                                              0 : 1 << 27,
1761                                                              &radeon_crtc->cursor_addr);
1762                                 if (r != 0)
1763                                         DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
1764                                 radeon_bo_unreserve(robj);
1765                         }
1766                 }
1767         }
1768
1769         /* init dig PHYs, disp eng pll */
1770         if (rdev->is_atom_bios) {
1771                 radeon_atom_encoder_init(rdev);
1772                 radeon_atom_disp_eng_pll_init(rdev);
1773                 /* turn on the BL */
1774                 if (rdev->mode_info.bl_encoder) {
1775                         u8 bl_level = radeon_get_backlight_level(rdev,
1776                                                                  rdev->mode_info.bl_encoder);
1777                         radeon_set_backlight_level(rdev, rdev->mode_info.bl_encoder,
1778                                                    bl_level);
1779                 }
1780         }
1781         /* reset hpd state */
1782         radeon_hpd_init(rdev);
1783         /* blat the mode back in */
1784         if (fbcon) {
1785                 drm_helper_resume_force_mode(dev);
1786                 /* turn on display hw */
1787                 drm_modeset_lock_all(dev);
1788                 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1789                         drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
1790                 }
1791                 drm_modeset_unlock_all(dev);
1792         }
1793
1794         drm_kms_helper_poll_enable(dev);
1795
1796         /* set the power state here in case we are a PX system or headless */
1797         if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled)
1798                 radeon_pm_compute_clocks(rdev);
1799
1800         if (fbcon) {
1801                 radeon_fbdev_set_suspend(rdev, 0);
1802                 console_unlock();
1803         }
1804
1805         return 0;
1806 }
1807
1808 /**
1809  * radeon_gpu_reset - reset the asic
1810  *
1811  * @rdev: radeon device pointer
1812  *
1813  * Attempt the reset the GPU if it has hung (all asics).
1814  * Returns 0 for success or an error on failure.
1815  */
1816 int radeon_gpu_reset(struct radeon_device *rdev)
1817 {
1818         unsigned ring_sizes[RADEON_NUM_RINGS];
1819         uint32_t *ring_data[RADEON_NUM_RINGS];
1820
1821         bool saved = false;
1822
1823         int i, r;
1824         int resched;
1825
1826         down_write(&rdev->exclusive_lock);
1827
1828         if (!rdev->needs_reset) {
1829                 up_write(&rdev->exclusive_lock);
1830                 return 0;
1831         }
1832
1833         atomic_inc(&rdev->gpu_reset_counter);
1834
1835         radeon_save_bios_scratch_regs(rdev);
1836         /* block TTM */
1837         resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
1838         radeon_suspend(rdev);
1839         radeon_hpd_fini(rdev);
1840
1841         for (i = 0; i < RADEON_NUM_RINGS; ++i) {
1842                 ring_sizes[i] = radeon_ring_backup(rdev, &rdev->ring[i],
1843                                                    &ring_data[i]);
1844                 if (ring_sizes[i]) {
1845                         saved = true;
1846                         dev_info(rdev->dev, "Saved %d dwords of commands "
1847                                  "on ring %d.\n", ring_sizes[i], i);
1848                 }
1849         }
1850
1851         r = radeon_asic_reset(rdev);
1852         if (!r) {
1853                 dev_info(rdev->dev, "GPU reset succeeded, trying to resume\n");
1854                 radeon_resume(rdev);
1855         }
1856
1857         radeon_restore_bios_scratch_regs(rdev);
1858
1859         for (i = 0; i < RADEON_NUM_RINGS; ++i) {
1860                 if (!r && ring_data[i]) {
1861                         radeon_ring_restore(rdev, &rdev->ring[i],
1862                                             ring_sizes[i], ring_data[i]);
1863                 } else {
1864                         radeon_fence_driver_force_completion(rdev, i);
1865                         kfree(ring_data[i]);
1866                 }
1867         }
1868
1869         if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
1870                 /* do dpm late init */
1871                 r = radeon_pm_late_init(rdev);
1872                 if (r) {
1873                         rdev->pm.dpm_enabled = false;
1874                         DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
1875                 }
1876         } else {
1877                 /* resume old pm late */
1878                 radeon_pm_resume(rdev);
1879         }
1880
1881         /* init dig PHYs, disp eng pll */
1882         if (rdev->is_atom_bios) {
1883                 radeon_atom_encoder_init(rdev);
1884                 radeon_atom_disp_eng_pll_init(rdev);
1885                 /* turn on the BL */
1886                 if (rdev->mode_info.bl_encoder) {
1887                         u8 bl_level = radeon_get_backlight_level(rdev,
1888                                                                  rdev->mode_info.bl_encoder);
1889                         radeon_set_backlight_level(rdev, rdev->mode_info.bl_encoder,
1890                                                    bl_level);
1891                 }
1892         }
1893         /* reset hpd state */
1894         radeon_hpd_init(rdev);
1895
1896         ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
1897
1898         rdev->in_reset = true;
1899         rdev->needs_reset = false;
1900
1901         downgrade_write(&rdev->exclusive_lock);
1902
1903         drm_helper_resume_force_mode(rdev->ddev);
1904
1905         /* set the power state here in case we are a PX system or headless */
1906         if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled)
1907                 radeon_pm_compute_clocks(rdev);
1908
1909         if (!r) {
1910                 r = radeon_ib_ring_tests(rdev);
1911                 if (r && saved)
1912                         r = -EAGAIN;
1913         } else {
1914                 /* bad news, how to tell it to userspace ? */
1915                 dev_info(rdev->dev, "GPU reset failed\n");
1916         }
1917
1918         rdev->needs_reset = r == -EAGAIN;
1919         rdev->in_reset = false;
1920
1921         up_read(&rdev->exclusive_lock);
1922         return r;
1923 }
1924
1925
1926 /*
1927  * Debugfs
1928  */
1929 int radeon_debugfs_add_files(struct radeon_device *rdev,
1930                              struct drm_info_list *files,
1931                              unsigned nfiles)
1932 {
1933         unsigned i;
1934
1935         for (i = 0; i < rdev->debugfs_count; i++) {
1936                 if (rdev->debugfs[i].files == files) {
1937                         /* Already registered */
1938                         return 0;
1939                 }
1940         }
1941
1942         i = rdev->debugfs_count + 1;
1943         if (i > RADEON_DEBUGFS_MAX_COMPONENTS) {
1944                 DRM_ERROR("Reached maximum number of debugfs components.\n");
1945                 DRM_ERROR("Report so we increase "
1946                           "RADEON_DEBUGFS_MAX_COMPONENTS.\n");
1947                 return -EINVAL;
1948         }
1949         rdev->debugfs[rdev->debugfs_count].files = files;
1950         rdev->debugfs[rdev->debugfs_count].num_files = nfiles;
1951         rdev->debugfs_count = i;
1952 #if defined(CONFIG_DEBUG_FS)
1953         drm_debugfs_create_files(files, nfiles,
1954                                  rdev->ddev->primary->debugfs_root,
1955                                  rdev->ddev->primary);
1956 #endif
1957         return 0;
1958 }