drm: Rename device to dev in struct drm_device
[dragonfly.git] / sys / dev / 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  * $FreeBSD: head/sys/dev/drm2/radeon/radeon_device.c 255573 2013-09-14 17:24:41Z dumbbell $
29  */
30
31 #include <drm/drmP.h>
32 #include <drm/drm_crtc_helper.h>
33 #include <uapi_drm/radeon_drm.h>
34 #include "radeon_reg.h"
35 #include "radeon.h"
36 #include "atom.h"
37
38 static const char radeon_family_name[][16] = {
39         "R100",
40         "RV100",
41         "RS100",
42         "RV200",
43         "RS200",
44         "R200",
45         "RV250",
46         "RS300",
47         "RV280",
48         "R300",
49         "R350",
50         "RV350",
51         "RV380",
52         "R420",
53         "R423",
54         "RV410",
55         "RS400",
56         "RS480",
57         "RS600",
58         "RS690",
59         "RS740",
60         "RV515",
61         "R520",
62         "RV530",
63         "RV560",
64         "RV570",
65         "R580",
66         "R600",
67         "RV610",
68         "RV630",
69         "RV670",
70         "RV620",
71         "RV635",
72         "RS780",
73         "RS880",
74         "RV770",
75         "RV730",
76         "RV710",
77         "RV740",
78         "CEDAR",
79         "REDWOOD",
80         "JUNIPER",
81         "CYPRESS",
82         "HEMLOCK",
83         "PALM",
84         "SUMO",
85         "SUMO2",
86         "BARTS",
87         "TURKS",
88         "CAICOS",
89         "CAYMAN",
90         "ARUBA",
91         "TAHITI",
92         "PITCAIRN",
93         "VERDE",
94         "LAST",
95 };
96
97 /**
98  * radeon_surface_init - Clear GPU surface registers.
99  *
100  * @rdev: radeon_device pointer
101  *
102  * Clear GPU surface registers (r1xx-r5xx).
103  */
104 void radeon_surface_init(struct radeon_device *rdev)
105 {
106         /* FIXME: check this out */
107         if (rdev->family < CHIP_R600) {
108                 int i;
109
110                 for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
111                         if (rdev->surface_regs[i].bo)
112                                 radeon_bo_get_surface_reg(rdev->surface_regs[i].bo);
113                         else
114                                 radeon_clear_surface_reg(rdev, i);
115                 }
116                 /* enable surfaces */
117                 WREG32(RADEON_SURFACE_CNTL, 0);
118         }
119 }
120
121 /*
122  * GPU scratch registers helpers function.
123  */
124 /**
125  * radeon_scratch_init - Init scratch register driver information.
126  *
127  * @rdev: radeon_device pointer
128  *
129  * Init CP scratch register driver information (r1xx-r5xx)
130  */
131 void radeon_scratch_init(struct radeon_device *rdev)
132 {
133         int i;
134
135         /* FIXME: check this out */
136         if (rdev->family < CHIP_R300) {
137                 rdev->scratch.num_reg = 5;
138         } else {
139                 rdev->scratch.num_reg = 7;
140         }
141         rdev->scratch.reg_base = RADEON_SCRATCH_REG0;
142         for (i = 0; i < rdev->scratch.num_reg; i++) {
143                 rdev->scratch.free[i] = true;
144                 rdev->scratch.reg[i] = rdev->scratch.reg_base + (i * 4);
145         }
146 }
147
148 /**
149  * radeon_scratch_get - Allocate a scratch register
150  *
151  * @rdev: radeon_device pointer
152  * @reg: scratch register mmio offset
153  *
154  * Allocate a CP scratch register for use by the driver (all asics).
155  * Returns 0 on success or -EINVAL on failure.
156  */
157 int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg)
158 {
159         int i;
160
161         for (i = 0; i < rdev->scratch.num_reg; i++) {
162                 if (rdev->scratch.free[i]) {
163                         rdev->scratch.free[i] = false;
164                         *reg = rdev->scratch.reg[i];
165                         return 0;
166                 }
167         }
168         return -EINVAL;
169 }
170
171 /**
172  * radeon_scratch_free - Free a scratch register
173  *
174  * @rdev: radeon_device pointer
175  * @reg: scratch register mmio offset
176  *
177  * Free a CP scratch register allocated for use by the driver (all asics)
178  */
179 void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg)
180 {
181         int i;
182
183         for (i = 0; i < rdev->scratch.num_reg; i++) {
184                 if (rdev->scratch.reg[i] == reg) {
185                         rdev->scratch.free[i] = true;
186                         return;
187                 }
188         }
189 }
190
191 /*
192  * radeon_wb_*()
193  * Writeback is the the method by which the the GPU updates special pages
194  * in memory with the status of certain GPU events (fences, ring pointers,
195  * etc.).
196  */
197
198 /**
199  * radeon_wb_disable - Disable Writeback
200  *
201  * @rdev: radeon_device pointer
202  *
203  * Disables Writeback (all asics).  Used for suspend.
204  */
205 void radeon_wb_disable(struct radeon_device *rdev)
206 {
207         int r;
208
209         if (rdev->wb.wb_obj) {
210                 r = radeon_bo_reserve(rdev->wb.wb_obj, false);
211                 if (unlikely(r != 0))
212                         return;
213                 radeon_bo_kunmap(rdev->wb.wb_obj);
214                 radeon_bo_unpin(rdev->wb.wb_obj);
215                 radeon_bo_unreserve(rdev->wb.wb_obj);
216         }
217         rdev->wb.enabled = false;
218 }
219
220 /**
221  * radeon_wb_fini - Disable Writeback and free memory
222  *
223  * @rdev: radeon_device pointer
224  *
225  * Disables Writeback and frees the Writeback memory (all asics).
226  * Used at driver shutdown.
227  */
228 void radeon_wb_fini(struct radeon_device *rdev)
229 {
230         radeon_wb_disable(rdev);
231         if (rdev->wb.wb_obj) {
232                 radeon_bo_unref(&rdev->wb.wb_obj);
233                 rdev->wb.wb = NULL;
234                 rdev->wb.wb_obj = NULL;
235         }
236 }
237
238 /**
239  * radeon_wb_init- Init Writeback driver info and allocate memory
240  *
241  * @rdev: radeon_device pointer
242  *
243  * Disables Writeback and frees the Writeback memory (all asics).
244  * Used at driver startup.
245  * Returns 0 on success or an -error on failure.
246  */
247 int radeon_wb_init(struct radeon_device *rdev)
248 {
249         int r;
250         void *wb_ptr;
251
252         if (rdev->wb.wb_obj == NULL) {
253                 r = radeon_bo_create(rdev, RADEON_GPU_PAGE_SIZE, PAGE_SIZE, true,
254                                      RADEON_GEM_DOMAIN_GTT, NULL, &rdev->wb.wb_obj);
255                 if (r) {
256                         dev_warn(rdev->dev, "(%d) create WB bo failed\n", r);
257                         return r;
258                 }
259         }
260         r = radeon_bo_reserve(rdev->wb.wb_obj, false);
261         if (unlikely(r != 0)) {
262                 radeon_wb_fini(rdev);
263                 return r;
264         }
265         r = radeon_bo_pin(rdev->wb.wb_obj, RADEON_GEM_DOMAIN_GTT,
266                           &rdev->wb.gpu_addr);
267         if (r) {
268                 radeon_bo_unreserve(rdev->wb.wb_obj);
269                 dev_warn(rdev->dev, "(%d) pin WB bo failed\n", r);
270                 radeon_wb_fini(rdev);
271                 return r;
272         }
273         wb_ptr = &rdev->wb.wb;
274         r = radeon_bo_kmap(rdev->wb.wb_obj, wb_ptr);
275         radeon_bo_unreserve(rdev->wb.wb_obj);
276         if (r) {
277                 dev_warn(rdev->dev, "(%d) map WB bo failed\n", r);
278                 radeon_wb_fini(rdev);
279                 return r;
280         }
281
282         /* clear wb memory */
283         memset(*(void **)wb_ptr, 0, RADEON_GPU_PAGE_SIZE);
284         /* disable event_write fences */
285         rdev->wb.use_event = false;
286         /* disabled via module param */
287         if (radeon_no_wb == 1) {
288                 rdev->wb.enabled = false;
289         } else {
290                 if (rdev->flags & RADEON_IS_AGP) {
291                         /* often unreliable on AGP */
292                         rdev->wb.enabled = false;
293                 } else if (rdev->family < CHIP_R300) {
294                         /* often unreliable on pre-r300 */
295                         rdev->wb.enabled = false;
296                 } else {
297                         rdev->wb.enabled = true;
298                         /* event_write fences are only available on r600+ */
299                         if (rdev->family >= CHIP_R600) {
300                                 rdev->wb.use_event = true;
301                         }
302                 }
303         }
304         /* always use writeback/events on NI, APUs */
305         if (rdev->family >= CHIP_PALM) {
306                 rdev->wb.enabled = true;
307                 rdev->wb.use_event = true;
308         }
309
310         dev_info(rdev->dev, "WB %sabled\n", rdev->wb.enabled ? "en" : "dis");
311
312         return 0;
313 }
314
315 /**
316  * radeon_vram_location - try to find VRAM location
317  * @rdev: radeon device structure holding all necessary informations
318  * @mc: memory controller structure holding memory informations
319  * @base: base address at which to put VRAM
320  *
321  * Function will place try to place VRAM at base address provided
322  * as parameter (which is so far either PCI aperture address or
323  * for IGP TOM base address).
324  *
325  * If there is not enough space to fit the unvisible VRAM in the 32bits
326  * address space then we limit the VRAM size to the aperture.
327  *
328  * If we are using AGP and if the AGP aperture doesn't allow us to have
329  * room for all the VRAM than we restrict the VRAM to the PCI aperture
330  * size and print a warning.
331  *
332  * This function will never fails, worst case are limiting VRAM.
333  *
334  * Note: GTT start, end, size should be initialized before calling this
335  * function on AGP platform.
336  *
337  * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
338  * this shouldn't be a problem as we are using the PCI aperture as a reference.
339  * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
340  * not IGP.
341  *
342  * Note: we use mc_vram_size as on some board we need to program the mc to
343  * cover the whole aperture even if VRAM size is inferior to aperture size
344  * Novell bug 204882 + along with lots of ubuntu ones
345  *
346  * Note: when limiting vram it's safe to overwritte real_vram_size because
347  * we are not in case where real_vram_size is inferior to mc_vram_size (ie
348  * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
349  * ones)
350  *
351  * Note: IGP TOM addr should be the same as the aperture addr, we don't
352  * explicitly check for that thought.
353  *
354  * FIXME: when reducing VRAM size align new size on power of 2.
355  */
356 void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base)
357 {
358         uint64_t limit = (uint64_t)radeon_vram_limit << 20;
359
360         mc->vram_start = base;
361         if (mc->mc_vram_size > (0xFFFFFFFF - base + 1)) {
362                 dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
363                 mc->real_vram_size = mc->aper_size;
364                 mc->mc_vram_size = mc->aper_size;
365         }
366         mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
367         if (rdev->flags & RADEON_IS_AGP && mc->vram_end > mc->gtt_start && mc->vram_start <= mc->gtt_end) {
368                 dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n");
369                 mc->real_vram_size = mc->aper_size;
370                 mc->mc_vram_size = mc->aper_size;
371         }
372         mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
373         if (limit && limit < mc->real_vram_size)
374                 mc->real_vram_size = limit;
375         dev_info(rdev->dev, "VRAM: %juM 0x%016jX - 0x%016jX (%juM used)\n",
376                         (uintmax_t)mc->mc_vram_size >> 20, (uintmax_t)mc->vram_start,
377                         (uintmax_t)mc->vram_end, (uintmax_t)mc->real_vram_size >> 20);
378 }
379
380 /**
381  * radeon_gtt_location - try to find GTT location
382  * @rdev: radeon device structure holding all necessary informations
383  * @mc: memory controller structure holding memory informations
384  *
385  * Function will place try to place GTT before or after VRAM.
386  *
387  * If GTT size is bigger than space left then we ajust GTT size.
388  * Thus function will never fails.
389  *
390  * FIXME: when reducing GTT size align new size on power of 2.
391  */
392 void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc)
393 {
394         u64 size_af, size_bf;
395
396         size_af = ((0xFFFFFFFF - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
397         size_bf = mc->vram_start & ~mc->gtt_base_align;
398         if (size_bf > size_af) {
399                 if (mc->gtt_size > size_bf) {
400                         dev_warn(rdev->dev, "limiting GTT\n");
401                         mc->gtt_size = size_bf;
402                 }
403                 mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
404         } else {
405                 if (mc->gtt_size > size_af) {
406                         dev_warn(rdev->dev, "limiting GTT\n");
407                         mc->gtt_size = size_af;
408                 }
409                 mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
410         }
411         mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
412         dev_info(rdev->dev, "GTT: %juM 0x%016jX - 0x%016jX\n",
413                         (uintmax_t)mc->gtt_size >> 20, (uintmax_t)mc->gtt_start, (uintmax_t)mc->gtt_end);
414 }
415
416 /*
417  * GPU helpers function.
418  */
419 /**
420  * radeon_card_posted - check if the hw has already been initialized
421  *
422  * @rdev: radeon_device pointer
423  *
424  * Check if the asic has been initialized (all asics).
425  * Used at driver startup.
426  * Returns true if initialized or false if not.
427  */
428 bool radeon_card_posted(struct radeon_device *rdev)
429 {
430         uint32_t reg;
431
432 #ifdef DUMBBELL_WIP
433         if (efi_enabled(EFI_BOOT) &&
434             rdev->dev->pci_subvendor == PCI_VENDOR_ID_APPLE)
435                 return false;
436 #endif /* DUMBBELL_WIP */
437
438         /* first check CRTCs */
439         if (ASIC_IS_DCE41(rdev)) {
440                 reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
441                         RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET);
442                 if (reg & EVERGREEN_CRTC_MASTER_EN)
443                         return true;
444         } else if (ASIC_IS_DCE4(rdev)) {
445                 reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) |
446                         RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET) |
447                         RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) |
448                         RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET) |
449                         RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) |
450                         RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET);
451                 if (reg & EVERGREEN_CRTC_MASTER_EN)
452                         return true;
453         } else if (ASIC_IS_AVIVO(rdev)) {
454                 reg = RREG32(AVIVO_D1CRTC_CONTROL) |
455                       RREG32(AVIVO_D2CRTC_CONTROL);
456                 if (reg & AVIVO_CRTC_EN) {
457                         return true;
458                 }
459         } else {
460                 reg = RREG32(RADEON_CRTC_GEN_CNTL) |
461                       RREG32(RADEON_CRTC2_GEN_CNTL);
462                 if (reg & RADEON_CRTC_EN) {
463                         return true;
464                 }
465         }
466
467         /* then check MEM_SIZE, in case the crtcs are off */
468         if (rdev->family >= CHIP_R600)
469                 reg = RREG32(R600_CONFIG_MEMSIZE);
470         else
471                 reg = RREG32(RADEON_CONFIG_MEMSIZE);
472
473         if (reg)
474                 return true;
475
476         return false;
477
478 }
479
480 /**
481  * radeon_update_bandwidth_info - update display bandwidth params
482  *
483  * @rdev: radeon_device pointer
484  *
485  * Used when sclk/mclk are switched or display modes are set.
486  * params are used to calculate display watermarks (all asics)
487  */
488 void radeon_update_bandwidth_info(struct radeon_device *rdev)
489 {
490         fixed20_12 a;
491         u32 sclk = rdev->pm.current_sclk;
492         u32 mclk = rdev->pm.current_mclk;
493
494         /* sclk/mclk in Mhz */
495         a.full = dfixed_const(100);
496         rdev->pm.sclk.full = dfixed_const(sclk);
497         rdev->pm.sclk.full = dfixed_div(rdev->pm.sclk, a);
498         rdev->pm.mclk.full = dfixed_const(mclk);
499         rdev->pm.mclk.full = dfixed_div(rdev->pm.mclk, a);
500
501         if (rdev->flags & RADEON_IS_IGP) {
502                 a.full = dfixed_const(16);
503                 /* core_bandwidth = sclk(Mhz) * 16 */
504                 rdev->pm.core_bandwidth.full = dfixed_div(rdev->pm.sclk, a);
505         }
506 }
507
508 /**
509  * radeon_boot_test_post_card - check and possibly initialize the hw
510  *
511  * @rdev: radeon_device pointer
512  *
513  * Check if the asic is initialized and if not, attempt to initialize
514  * it (all asics).
515  * Returns true if initialized or false if not.
516  */
517 bool radeon_boot_test_post_card(struct radeon_device *rdev)
518 {
519         if (radeon_card_posted(rdev))
520                 return true;
521
522         if (rdev->bios) {
523                 DRM_INFO("GPU not posted. posting now...\n");
524                 if (rdev->is_atom_bios)
525                         atom_asic_init(rdev->mode_info.atom_context);
526                 else
527                         radeon_combios_asic_init(rdev->ddev);
528                 return true;
529         } else {
530                 dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
531                 return false;
532         }
533 }
534
535 /**
536  * radeon_dummy_page_init - init dummy page used by the driver
537  *
538  * @rdev: radeon_device pointer
539  *
540  * Allocate the dummy page used by the driver (all asics).
541  * This dummy page is used by the driver as a filler for gart entries
542  * when pages are taken out of the GART
543  * Returns 0 on sucess, -ENOMEM on failure.
544  */
545 int radeon_dummy_page_init(struct radeon_device *rdev)
546 {
547         if (rdev->dummy_page.dmah)
548                 return 0;
549         rdev->dummy_page.dmah = drm_pci_alloc(rdev->ddev,
550             PAGE_SIZE, PAGE_SIZE, ~0);
551         if (rdev->dummy_page.dmah == NULL)
552                 return -ENOMEM;
553         rdev->dummy_page.addr = (dma_addr_t)rdev->dummy_page.dmah->vaddr;
554         return 0;
555 }
556
557 /**
558  * radeon_dummy_page_fini - free dummy page used by the driver
559  *
560  * @rdev: radeon_device pointer
561  *
562  * Frees the dummy page used by the driver (all asics).
563  */
564 void radeon_dummy_page_fini(struct radeon_device *rdev)
565 {
566         if (rdev->dummy_page.dmah == NULL)
567                 return;
568         drm_pci_free(rdev->ddev, rdev->dummy_page.dmah);
569         rdev->dummy_page.dmah = NULL;
570         rdev->dummy_page.addr = 0;
571 }
572
573
574 /* ATOM accessor methods */
575 /*
576  * ATOM is an interpreted byte code stored in tables in the vbios.  The
577  * driver registers callbacks to access registers and the interpreter
578  * in the driver parses the tables and executes then to program specific
579  * actions (set display modes, asic init, etc.).  See radeon_atombios.c,
580  * atombios.h, and atom.c
581  */
582
583 /**
584  * cail_pll_read - read PLL register
585  *
586  * @info: atom card_info pointer
587  * @reg: PLL register offset
588  *
589  * Provides a PLL register accessor for the atom interpreter (r4xx+).
590  * Returns the value of the PLL register.
591  */
592 static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
593 {
594         struct radeon_device *rdev = info->dev->dev_private;
595         uint32_t r;
596
597         r = rdev->pll_rreg(rdev, reg);
598         return r;
599 }
600
601 /**
602  * cail_pll_write - write PLL register
603  *
604  * @info: atom card_info pointer
605  * @reg: PLL register offset
606  * @val: value to write to the pll register
607  *
608  * Provides a PLL register accessor for the atom interpreter (r4xx+).
609  */
610 static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
611 {
612         struct radeon_device *rdev = info->dev->dev_private;
613
614         rdev->pll_wreg(rdev, reg, val);
615 }
616
617 /**
618  * cail_mc_read - read MC (Memory Controller) register
619  *
620  * @info: atom card_info pointer
621  * @reg: MC register offset
622  *
623  * Provides an MC register accessor for the atom interpreter (r4xx+).
624  * Returns the value of the MC register.
625  */
626 static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
627 {
628         struct radeon_device *rdev = info->dev->dev_private;
629         uint32_t r;
630
631         r = rdev->mc_rreg(rdev, reg);
632         return r;
633 }
634
635 /**
636  * cail_mc_write - write MC (Memory Controller) register
637  *
638  * @info: atom card_info pointer
639  * @reg: MC register offset
640  * @val: value to write to the pll register
641  *
642  * Provides a MC register accessor for the atom interpreter (r4xx+).
643  */
644 static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
645 {
646         struct radeon_device *rdev = info->dev->dev_private;
647
648         rdev->mc_wreg(rdev, reg, val);
649 }
650
651 /**
652  * cail_reg_write - write MMIO register
653  *
654  * @info: atom card_info pointer
655  * @reg: MMIO register offset
656  * @val: value to write to the pll register
657  *
658  * Provides a MMIO register accessor for the atom interpreter (r4xx+).
659  */
660 static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
661 {
662         struct radeon_device *rdev = info->dev->dev_private;
663
664         WREG32(reg*4, val);
665 }
666
667 /**
668  * cail_reg_read - read MMIO register
669  *
670  * @info: atom card_info pointer
671  * @reg: MMIO register offset
672  *
673  * Provides an MMIO register accessor for the atom interpreter (r4xx+).
674  * Returns the value of the MMIO register.
675  */
676 static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
677 {
678         struct radeon_device *rdev = info->dev->dev_private;
679         uint32_t r;
680
681         r = RREG32(reg*4);
682         return r;
683 }
684
685 /**
686  * cail_ioreg_write - write IO register
687  *
688  * @info: atom card_info pointer
689  * @reg: IO register offset
690  * @val: value to write to the pll register
691  *
692  * Provides a IO register accessor for the atom interpreter (r4xx+).
693  */
694 static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
695 {
696         struct radeon_device *rdev = info->dev->dev_private;
697
698         WREG32_IO(reg*4, val);
699 }
700
701 /**
702  * cail_ioreg_read - read IO register
703  *
704  * @info: atom card_info pointer
705  * @reg: IO register offset
706  *
707  * Provides an IO register accessor for the atom interpreter (r4xx+).
708  * Returns the value of the IO register.
709  */
710 static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
711 {
712         struct radeon_device *rdev = info->dev->dev_private;
713         uint32_t r;
714
715         r = RREG32_IO(reg*4);
716         return r;
717 }
718
719 /**
720  * radeon_atombios_init - init the driver info and callbacks for atombios
721  *
722  * @rdev: radeon_device pointer
723  *
724  * Initializes the driver info and register access callbacks for the
725  * ATOM interpreter (r4xx+).
726  * Returns 0 on sucess, -ENOMEM on failure.
727  * Called at driver startup.
728  */
729 int radeon_atombios_init(struct radeon_device *rdev)
730 {
731         struct card_info *atom_card_info =
732             kmalloc(sizeof(struct card_info), DRM_MEM_DRIVER,
733                     M_ZERO | M_WAITOK);
734
735         if (!atom_card_info)
736                 return -ENOMEM;
737
738         rdev->mode_info.atom_card_info = atom_card_info;
739         atom_card_info->dev = rdev->ddev;
740         atom_card_info->reg_read = cail_reg_read;
741         atom_card_info->reg_write = cail_reg_write;
742         /* needed for iio ops */
743         if (rdev->rio_mem) {
744                 atom_card_info->ioreg_read = cail_ioreg_read;
745                 atom_card_info->ioreg_write = cail_ioreg_write;
746         } else {
747                 DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
748                 atom_card_info->ioreg_read = cail_reg_read;
749                 atom_card_info->ioreg_write = cail_reg_write;
750         }
751         atom_card_info->mc_read = cail_mc_read;
752         atom_card_info->mc_write = cail_mc_write;
753         atom_card_info->pll_read = cail_pll_read;
754         atom_card_info->pll_write = cail_pll_write;
755
756         rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios);
757         lockinit(&rdev->mode_info.atom_context->mutex, "rmiacmtx", 0,
758                  LK_CANRECURSE);
759         radeon_atom_initialize_bios_scratch_regs(rdev->ddev);
760         atom_allocate_fb_scratch(rdev->mode_info.atom_context);
761         return 0;
762 }
763
764 /**
765  * radeon_atombios_fini - free the driver info and callbacks for atombios
766  *
767  * @rdev: radeon_device pointer
768  *
769  * Frees the driver info and register access callbacks for the ATOM
770  * interpreter (r4xx+).
771  * Called at driver shutdown.
772  */
773 void radeon_atombios_fini(struct radeon_device *rdev)
774 {
775         if (rdev->mode_info.atom_context) {
776                 drm_free(rdev->mode_info.atom_context->scratch,
777                          DRM_MEM_DRIVER);
778                 atom_destroy(rdev->mode_info.atom_context);
779         }
780         drm_free(rdev->mode_info.atom_card_info, DRM_MEM_DRIVER);
781 }
782
783 /* COMBIOS */
784 /*
785  * COMBIOS is the bios format prior to ATOM. It provides
786  * command tables similar to ATOM, but doesn't have a unified
787  * parser.  See radeon_combios.c
788  */
789
790 /**
791  * radeon_combios_init - init the driver info for combios
792  *
793  * @rdev: radeon_device pointer
794  *
795  * Initializes the driver info for combios (r1xx-r3xx).
796  * Returns 0 on sucess.
797  * Called at driver startup.
798  */
799 int radeon_combios_init(struct radeon_device *rdev)
800 {
801         radeon_combios_initialize_bios_scratch_regs(rdev->ddev);
802         return 0;
803 }
804
805 /**
806  * radeon_combios_fini - free the driver info for combios
807  *
808  * @rdev: radeon_device pointer
809  *
810  * Frees the driver info for combios (r1xx-r3xx).
811  * Called at driver shutdown.
812  */
813 void radeon_combios_fini(struct radeon_device *rdev)
814 {
815 }
816
817 #ifdef DUMBBELL_WIP
818 /* if we get transitioned to only one device, take VGA back */
819 /**
820  * radeon_vga_set_decode - enable/disable vga decode
821  *
822  * @cookie: radeon_device pointer
823  * @state: enable/disable vga decode
824  *
825  * Enable/disable vga decode (all asics).
826  * Returns VGA resource flags.
827  */
828 static unsigned int radeon_vga_set_decode(void *cookie, bool state)
829 {
830         struct radeon_device *rdev = cookie;
831         radeon_vga_set_state(rdev, state);
832         if (state)
833                 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
834                        VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
835         else
836                 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
837 }
838 #endif /* DUMBBELL_WIP */
839
840 /**
841  * radeon_check_pot_argument - check that argument is a power of two
842  *
843  * @arg: value to check
844  *
845  * Validates that a certain argument is a power of two (all asics).
846  * Returns true if argument is valid.
847  */
848 static bool radeon_check_pot_argument(int arg)
849 {
850         return (arg & (arg - 1)) == 0;
851 }
852
853 /**
854  * radeon_check_arguments - validate module params
855  *
856  * @rdev: radeon_device pointer
857  *
858  * Validates certain module parameters and updates
859  * the associated values used by the driver (all asics).
860  */
861 static void radeon_check_arguments(struct radeon_device *rdev)
862 {
863         /* vramlimit must be a power of two */
864         if (!radeon_check_pot_argument(radeon_vram_limit)) {
865                 dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n",
866                                 radeon_vram_limit);
867                 radeon_vram_limit = 0;
868         }
869
870         /* gtt size must be power of two and greater or equal to 32M */
871         if (radeon_gart_size < 32) {
872                 dev_warn(rdev->dev, "gart size (%d) too small forcing to 512M\n",
873                                 radeon_gart_size);
874                 radeon_gart_size = 512;
875
876         } else if (!radeon_check_pot_argument(radeon_gart_size)) {
877                 dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n",
878                                 radeon_gart_size);
879                 radeon_gart_size = 512;
880         }
881         rdev->mc.gtt_size = (uint64_t)radeon_gart_size << 20;
882
883         /* AGP mode can only be -1, 1, 2, 4, 8 */
884         switch (radeon_agpmode) {
885         case -1:
886         case 0:
887         case 1:
888         case 2:
889         case 4:
890         case 8:
891                 break;
892         default:
893                 dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: "
894                                 "-1, 0, 1, 2, 4, 8)\n", radeon_agpmode);
895                 radeon_agpmode = 0;
896                 break;
897         }
898 }
899
900 /**
901  * radeon_switcheroo_quirk_long_wakeup - return true if longer d3 delay is
902  * needed for waking up.
903  *
904  * @pdev: pci dev pointer
905  */
906 #ifdef DUMBBELL_WIP
907 static bool radeon_switcheroo_quirk_long_wakeup(struct pci_dev *pdev)
908 {
909
910         /* 6600m in a macbook pro */
911         if (pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE &&
912             pdev->subsystem_device == 0x00e2) {
913                 printk(KERN_INFO "radeon: quirking longer d3 wakeup delay\n");
914                 return true;
915         }
916
917         return false;
918 }
919 #endif /* DUMBBELL_WIP */
920
921 /**
922  * radeon_switcheroo_set_state - set switcheroo state
923  *
924  * @pdev: pci dev pointer
925  * @state: vga switcheroo state
926  *
927  * Callback for the switcheroo driver.  Suspends or resumes the
928  * the asics before or after it is powered up using ACPI methods.
929  */
930 #ifdef DUMBBELL_WIP
931 static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
932 {
933         struct drm_device *dev = pci_get_drvdata(pdev);
934         pm_message_t pmm = { .event = PM_EVENT_SUSPEND };
935         if (state == VGA_SWITCHEROO_ON) {
936                 unsigned d3_delay = dev->pdev->d3_delay;
937
938                 printk(KERN_INFO "radeon: switched on\n");
939                 /* don't suspend or resume card normally */
940                 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
941
942                 if (d3_delay < 20 && radeon_switcheroo_quirk_long_wakeup(pdev))
943                         dev->pdev->d3_delay = 20;
944
945                 radeon_resume_kms(dev);
946
947                 dev->pdev->d3_delay = d3_delay;
948
949                 dev->switch_power_state = DRM_SWITCH_POWER_ON;
950                 drm_kms_helper_poll_enable(dev);
951         } else {
952                 printk(KERN_INFO "radeon: switched off\n");
953                 drm_kms_helper_poll_disable(dev);
954                 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
955                 radeon_suspend_kms(dev, pmm);
956                 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
957         }
958 }
959 #endif /* DUMBBELL_WIP */
960
961 /**
962  * radeon_switcheroo_can_switch - see if switcheroo state can change
963  *
964  * @pdev: pci dev pointer
965  *
966  * Callback for the switcheroo driver.  Check of the switcheroo
967  * state can be changed.
968  * Returns true if the state can be changed, false if not.
969  */
970 #ifdef DUMBBELL_WIP
971 static bool radeon_switcheroo_can_switch(struct pci_dev *pdev)
972 {
973         struct drm_device *dev = pci_get_drvdata(pdev);
974         bool can_switch;
975
976         spin_lock(&dev->count_lock);
977         can_switch = (dev->open_count == 0);
978         spin_unlock(&dev->count_lock);
979         return can_switch;
980 }
981
982 static const struct vga_switcheroo_client_ops radeon_switcheroo_ops = {
983         .set_gpu_state = radeon_switcheroo_set_state,
984         .reprobe = NULL,
985         .can_switch = radeon_switcheroo_can_switch,
986 };
987 #endif /* DUMBBELL_WIP */
988
989 /**
990  * radeon_device_init - initialize the driver
991  *
992  * @rdev: radeon_device pointer
993  * @pdev: drm dev pointer
994  * @flags: driver flags
995  *
996  * Initializes the driver info and hw (all asics).
997  * Returns 0 for success or an error on failure.
998  * Called at driver startup.
999  */
1000 int radeon_device_init(struct radeon_device *rdev,
1001                        struct drm_device *ddev,
1002                        uint32_t flags)
1003 {
1004         int r, i;
1005         int dma_bits;
1006
1007         rdev->shutdown = false;
1008         rdev->dev = ddev->dev;
1009         rdev->ddev = ddev;
1010         rdev->flags = flags;
1011         rdev->family = flags & RADEON_FAMILY_MASK;
1012         rdev->is_atom_bios = false;
1013         rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT;
1014         rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
1015         rdev->accel_working = false;
1016         rdev->fictitious_range_registered = false;
1017         /* set up ring ids */
1018         for (i = 0; i < RADEON_NUM_RINGS; i++) {
1019                 rdev->ring[i].idx = i;
1020         }
1021
1022         DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X).\n",
1023                 radeon_family_name[rdev->family], ddev->pci_vendor, ddev->pci_device,
1024                 ddev->pci_subvendor, ddev->pci_subdevice);
1025
1026         /* mutex initialization are all done here so we
1027          * can recall function without having locking issues */
1028         lockinit(&rdev->ring_lock, "drm__radeon_device__ring_lock", 0,
1029                  LK_CANRECURSE);
1030         lockinit(&rdev->dc_hw_i2c_mutex,
1031                  "drm__radeon_device__dc_hw_i2c_mutex", 0, LK_CANRECURSE);
1032         atomic_set(&rdev->ih.lock, 0);
1033         spin_init(&rdev->gem.mutex);
1034         lockinit(&rdev->pm.mutex, "drm__radeon_device__pm__mutex", 0,
1035                  LK_CANRECURSE);
1036         spin_init(&rdev->gpu_clock_mutex);
1037         lockinit(&rdev->pm.mclk_lock, "drm__radeon_device__pm__mclk_lock", 0,
1038                  LK_CANRECURSE);
1039         lockinit(&rdev->exclusive_lock, "drm__radeon_device__exclusive_lock",
1040                  0, LK_CANRECURSE);
1041         DRM_INIT_WAITQUEUE(&rdev->irq.vblank_queue);
1042         r = radeon_gem_init(rdev);
1043         if (r)
1044                 return r;
1045         /* initialize vm here */
1046         lockinit(&rdev->vm_manager.lock,
1047                  "drm__radeon_device__vm_manager__lock", 0, LK_CANRECURSE);
1048         /* Adjust VM size here.
1049          * Currently set to 4GB ((1 << 20) 4k pages).
1050          * Max GPUVM size for cayman and SI is 40 bits.
1051          */
1052         rdev->vm_manager.max_pfn = 1 << 20;
1053         INIT_LIST_HEAD(&rdev->vm_manager.lru_vm);
1054
1055         /* Set asic functions */
1056         r = radeon_asic_init(rdev);
1057         if (r)
1058                 return r;
1059         radeon_check_arguments(rdev);
1060
1061         /* all of the newer IGP chips have an internal gart
1062          * However some rs4xx report as AGP, so remove that here.
1063          */
1064         if ((rdev->family >= CHIP_RS400) &&
1065             (rdev->flags & RADEON_IS_IGP)) {
1066                 rdev->flags &= ~RADEON_IS_AGP;
1067         }
1068
1069         if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) {
1070                 radeon_agp_disable(rdev);
1071         }
1072
1073         /* set DMA mask + need_dma32 flags.
1074          * PCIE - can handle 40-bits.
1075          * IGP - can handle 40-bits
1076          * AGP - generally dma32 is safest
1077          * PCI - dma32 for legacy pci gart, 40 bits on newer asics
1078          */
1079         rdev->need_dma32 = false;
1080         if (rdev->flags & RADEON_IS_AGP)
1081                 rdev->need_dma32 = true;
1082         if ((rdev->flags & RADEON_IS_PCI) &&
1083             (rdev->family <= CHIP_RS740))
1084                 rdev->need_dma32 = true;
1085
1086         dma_bits = rdev->need_dma32 ? 32 : 40;
1087 #ifdef DUMBBELL_WIP
1088         r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
1089         if (r) {
1090                 rdev->need_dma32 = true;
1091                 dma_bits = 32;
1092                 printk(KERN_WARNING "radeon: No suitable DMA available.\n");
1093         }
1094         r = pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
1095         if (r) {
1096                 pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(32));
1097                 printk(KERN_WARNING "radeon: No coherent DMA available.\n");
1098         }
1099 #endif /* DUMBBELL_WIP */
1100
1101         /* Registers mapping */
1102         /* TODO: block userspace mapping of io register */
1103         spin_init(&rdev->mmio_idx_lock);
1104         rdev->rmmio_rid = PCIR_BAR(2);
1105         rdev->rmmio = bus_alloc_resource_any(rdev->dev, SYS_RES_MEMORY,
1106             &rdev->rmmio_rid, RF_ACTIVE | RF_SHAREABLE);
1107         if (rdev->rmmio == NULL) {
1108                 return -ENOMEM;
1109         }
1110         rdev->rmmio_base = rman_get_start(rdev->rmmio);
1111         rdev->rmmio_size = rman_get_size(rdev->rmmio);
1112         DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base);
1113         DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size);
1114
1115         /* io port mapping */
1116         for (i = 0; i < DRM_MAX_PCI_RESOURCE; i++) {
1117                 uint32_t data;
1118
1119                 data = pci_read_config(rdev->dev, PCIR_BAR(i), 4);
1120                 if (PCI_BAR_IO(data)) {
1121                         rdev->rio_rid = PCIR_BAR(i);
1122                         rdev->rio_mem = bus_alloc_resource_any(rdev->dev,
1123                             SYS_RES_IOPORT, &rdev->rio_rid,
1124                             RF_ACTIVE | RF_SHAREABLE);
1125                         break;
1126                 }
1127         }
1128         if (rdev->rio_mem == NULL)
1129                 DRM_ERROR("Unable to find PCI I/O BAR\n");
1130
1131         rdev->tq = taskqueue_create("radeonkms", M_WAITOK,
1132             taskqueue_thread_enqueue, &rdev->tq);
1133         taskqueue_start_threads(&rdev->tq, 1, 0, -1, "radeon taskq");
1134
1135 #ifdef DUMBBELL_WIP
1136         /* if we have > 1 VGA cards, then disable the radeon VGA resources */
1137         /* this will fail for cards that aren't VGA class devices, just
1138          * ignore it */
1139         vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode);
1140         vga_switcheroo_register_client(rdev->pdev, &radeon_switcheroo_ops);
1141 #endif /* DUMBBELL_WIP */
1142
1143         r = radeon_init(rdev);
1144         if (r)
1145                 return r;
1146
1147         r = radeon_ib_ring_tests(rdev);
1148         if (r)
1149                 DRM_ERROR("ib ring test failed (%d).\n", r);
1150
1151         if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) {
1152                 /* Acceleration not working on AGP card try again
1153                  * with fallback to PCI or PCIE GART
1154                  */
1155                 radeon_asic_reset(rdev);
1156                 radeon_fini(rdev);
1157                 radeon_agp_disable(rdev);
1158                 r = radeon_init(rdev);
1159                 if (r)
1160                         return r;
1161         }
1162
1163         DRM_INFO("%s: Taking over the fictitious range 0x%jx-0x%jx\n",
1164             __func__, (uintmax_t)rdev->mc.aper_base,
1165             (uintmax_t)rdev->mc.aper_base + rdev->mc.visible_vram_size);
1166         r = vm_phys_fictitious_reg_range(
1167             rdev->mc.aper_base,
1168             rdev->mc.aper_base + rdev->mc.visible_vram_size,
1169             VM_MEMATTR_WRITE_COMBINING);
1170         if (r != 0) {
1171                 DRM_ERROR("Failed to register fictitious range "
1172                     "0x%jx-0x%jx (%d).\n", (uintmax_t)rdev->mc.aper_base,
1173                     (uintmax_t)rdev->mc.aper_base + rdev->mc.visible_vram_size, r);
1174                 return (-r);
1175         }
1176         rdev->fictitious_range_registered = true;
1177
1178         if ((radeon_testing & 1)) {
1179                 radeon_test_moves(rdev);
1180         }
1181         if ((radeon_testing & 2)) {
1182                 radeon_test_syncing(rdev);
1183         }
1184         if (radeon_benchmarking) {
1185                 radeon_benchmark(rdev, radeon_benchmarking);
1186         }
1187         return 0;
1188 }
1189
1190 #ifdef DUMBBELL_WIP
1191 static void radeon_debugfs_remove_files(struct radeon_device *rdev);
1192 #endif /* DUMBBELL_WIP */
1193
1194 /**
1195  * radeon_device_fini - tear down the driver
1196  *
1197  * @rdev: radeon_device pointer
1198  *
1199  * Tear down the driver info (all asics).
1200  * Called at driver shutdown.
1201  */
1202 void radeon_device_fini(struct radeon_device *rdev)
1203 {
1204         DRM_INFO("radeon: finishing device.\n");
1205         rdev->shutdown = true;
1206         /* evict vram memory */
1207         radeon_bo_evict_vram(rdev);
1208
1209         if (rdev->fictitious_range_registered) {
1210                 vm_phys_fictitious_unreg_range(
1211                     rdev->mc.aper_base,
1212                     rdev->mc.aper_base + rdev->mc.visible_vram_size);
1213         }
1214
1215         radeon_fini(rdev);
1216 #ifdef DUMBBELL_WIP
1217         vga_switcheroo_unregister_client(rdev->pdev);
1218         vga_client_register(rdev->pdev, NULL, NULL, NULL);
1219 #endif /* DUMBBELL_WIP */
1220
1221         if (rdev->tq != NULL) {
1222                 taskqueue_free(rdev->tq);
1223                 rdev->tq = NULL;
1224         }
1225
1226         if (rdev->rio_mem)
1227                 bus_release_resource(rdev->dev, SYS_RES_IOPORT, rdev->rio_rid,
1228                     rdev->rio_mem);
1229         rdev->rio_mem = NULL;
1230         bus_release_resource(rdev->dev, SYS_RES_MEMORY, rdev->rmmio_rid,
1231             rdev->rmmio);
1232         rdev->rmmio = NULL;
1233 #ifdef DUMBBELL_WIP
1234         radeon_debugfs_remove_files(rdev);
1235 #endif /* DUMBBELL_WIP */
1236 }
1237
1238
1239 /*
1240  * Suspend & resume.
1241  */
1242 /**
1243  * radeon_suspend_kms - initiate device suspend
1244  *
1245  * @pdev: drm dev pointer
1246  * @state: suspend state
1247  *
1248  * Puts the hw in the suspend state (all asics).
1249  * Returns 0 for success or an error on failure.
1250  * Called at driver suspend.
1251  */
1252 int radeon_suspend_kms(struct drm_device *dev)
1253 {
1254         struct radeon_device *rdev;
1255         struct drm_crtc *crtc;
1256         struct drm_connector *connector;
1257         int i, r;
1258         bool force_completion = false;
1259
1260         if (dev == NULL || dev->dev_private == NULL) {
1261                 return -ENODEV;
1262         }
1263 #ifdef DUMBBELL_WIP
1264         if (state.event == PM_EVENT_PRETHAW) {
1265                 return 0;
1266         }
1267 #endif /* DUMBBELL_WIP */
1268         rdev = dev->dev_private;
1269
1270         if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1271                 return 0;
1272
1273         drm_kms_helper_poll_disable(dev);
1274
1275         /* turn off display hw */
1276         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1277                 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
1278         }
1279
1280         /* unpin the front buffers */
1281         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1282                 struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->fb);
1283                 struct radeon_bo *robj;
1284
1285                 if (rfb == NULL || rfb->obj == NULL) {
1286                         continue;
1287                 }
1288                 robj = gem_to_radeon_bo(rfb->obj);
1289                 /* don't unpin kernel fb objects */
1290                 if (!radeon_fbdev_robj_is_fb(rdev, robj)) {
1291                         r = radeon_bo_reserve(robj, false);
1292                         if (r == 0) {
1293                                 radeon_bo_unpin(robj);
1294                                 radeon_bo_unreserve(robj);
1295                         }
1296                 }
1297         }
1298         /* evict vram memory */
1299         radeon_bo_evict_vram(rdev);
1300
1301         lockmgr(&rdev->ring_lock, LK_EXCLUSIVE);
1302         /* wait for gpu to finish processing current batch */
1303         for (i = 0; i < RADEON_NUM_RINGS; i++) {
1304                 r = radeon_fence_wait_empty_locked(rdev, i);
1305                 if (r) {
1306                         /* delay GPU reset to resume */
1307                         force_completion = true;
1308                 }
1309         }
1310         if (force_completion) {
1311                 radeon_fence_driver_force_completion(rdev);
1312         }
1313         lockmgr(&rdev->ring_lock, LK_RELEASE);
1314
1315         radeon_save_bios_scratch_regs(rdev);
1316
1317         radeon_pm_suspend(rdev);
1318         radeon_suspend(rdev);
1319         radeon_hpd_fini(rdev);
1320         /* evict remaining vram memory */
1321         radeon_bo_evict_vram(rdev);
1322
1323         radeon_agp_suspend(rdev);
1324
1325         pci_save_state(device_get_parent(rdev->dev));
1326 #ifdef DUMBBELL_WIP
1327         if (state.event == PM_EVENT_SUSPEND) {
1328                 /* Shut down the device */
1329                 pci_disable_device(dev->pdev);
1330 #endif /* DUMBBELL_WIP */
1331                 pci_set_powerstate(dev->dev, PCI_POWERSTATE_D3);
1332 #ifdef DUMBBELL_WIP
1333         }
1334         console_lock();
1335 #endif /* DUMBBELL_WIP */
1336         radeon_fbdev_set_suspend(rdev, 1);
1337 #ifdef DUMBBELL_WIP
1338         console_unlock();
1339 #endif /* DUMBBELL_WIP */
1340         return 0;
1341 }
1342
1343 /**
1344  * radeon_resume_kms - initiate device resume
1345  *
1346  * @pdev: drm dev pointer
1347  *
1348  * Bring the hw back to operating state (all asics).
1349  * Returns 0 for success or an error on failure.
1350  * Called at driver resume.
1351  */
1352 int radeon_resume_kms(struct drm_device *dev)
1353 {
1354         struct drm_connector *connector;
1355         struct radeon_device *rdev = dev->dev_private;
1356         int r;
1357
1358         if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1359                 return 0;
1360
1361 #ifdef DUMBBELL_WIP
1362         console_lock();
1363 #endif /* DUMBBELL_WIP */
1364         pci_set_powerstate(dev->dev, PCI_POWERSTATE_D0);
1365         pci_restore_state(device_get_parent(rdev->dev));
1366 #ifdef DUMBBELL_WIP
1367         if (pci_enable_device(dev->pdev)) {
1368                 console_unlock();
1369                 return -1;
1370         }
1371 #endif /* DUMBBELL_WIP */
1372         /* resume AGP if in use */
1373         radeon_agp_resume(rdev);
1374         radeon_resume(rdev);
1375
1376         r = radeon_ib_ring_tests(rdev);
1377         if (r)
1378                 DRM_ERROR("ib ring test failed (%d).\n", r);
1379
1380         radeon_pm_resume(rdev);
1381         radeon_restore_bios_scratch_regs(rdev);
1382
1383         radeon_fbdev_set_suspend(rdev, 0);
1384 #ifdef DUMBBELL_WIP
1385         console_unlock();
1386 #endif /* DUMBBELL_WIP */
1387
1388         /* init dig PHYs, disp eng pll */
1389         if (rdev->is_atom_bios) {
1390                 radeon_atom_encoder_init(rdev);
1391                 radeon_atom_disp_eng_pll_init(rdev);
1392                 /* turn on the BL */
1393                 if (rdev->mode_info.bl_encoder) {
1394                         u8 bl_level = radeon_get_backlight_level(rdev,
1395                                                                  rdev->mode_info.bl_encoder);
1396                         radeon_set_backlight_level(rdev, rdev->mode_info.bl_encoder,
1397                                                    bl_level);
1398                 }
1399         }
1400         /* reset hpd state */
1401         radeon_hpd_init(rdev);
1402         /* blat the mode back in */
1403         drm_helper_resume_force_mode(dev);
1404         /* turn on display hw */
1405         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1406                 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
1407         }
1408
1409         drm_kms_helper_poll_enable(dev);
1410         return 0;
1411 }
1412
1413 /**
1414  * radeon_gpu_reset - reset the asic
1415  *
1416  * @rdev: radeon device pointer
1417  *
1418  * Attempt the reset the GPU if it has hung (all asics).
1419  * Returns 0 for success or an error on failure.
1420  */
1421 int radeon_gpu_reset(struct radeon_device *rdev)
1422 {
1423         unsigned ring_sizes[RADEON_NUM_RINGS];
1424         uint32_t *ring_data[RADEON_NUM_RINGS];
1425
1426         bool saved = false;
1427
1428         int i, r;
1429         int resched;
1430
1431         lockmgr(&rdev->exclusive_lock, LK_EXCLUSIVE);
1432         radeon_save_bios_scratch_regs(rdev);
1433         /* block TTM */
1434         resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
1435         radeon_suspend(rdev);
1436
1437         for (i = 0; i < RADEON_NUM_RINGS; ++i) {
1438                 ring_sizes[i] = radeon_ring_backup(rdev, &rdev->ring[i],
1439                                                    &ring_data[i]);
1440                 if (ring_sizes[i]) {
1441                         saved = true;
1442                         dev_info(rdev->dev, "Saved %d dwords of commands "
1443                                  "on ring %d.\n", ring_sizes[i], i);
1444                 }
1445         }
1446
1447 retry:
1448         r = radeon_asic_reset(rdev);
1449         if (!r) {
1450                 dev_info(rdev->dev, "GPU reset succeeded, trying to resume\n");
1451                 radeon_resume(rdev);
1452         }
1453
1454         radeon_restore_bios_scratch_regs(rdev);
1455
1456         if (!r) {
1457                 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
1458                         radeon_ring_restore(rdev, &rdev->ring[i],
1459                                             ring_sizes[i], ring_data[i]);
1460                         ring_sizes[i] = 0;
1461                         ring_data[i] = NULL;
1462                 }
1463
1464                 r = radeon_ib_ring_tests(rdev);
1465                 if (r) {
1466                         dev_err(rdev->dev, "ib ring test failed (%d).\n", r);
1467                         if (saved) {
1468                                 saved = false;
1469                                 radeon_suspend(rdev);
1470                                 goto retry;
1471                         }
1472                 }
1473         } else {
1474                 radeon_fence_driver_force_completion(rdev);
1475                 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
1476                         drm_free(ring_data[i], DRM_MEM_DRIVER);
1477                 }
1478         }
1479
1480         drm_helper_resume_force_mode(rdev->ddev);
1481
1482         ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
1483         if (r) {
1484                 /* bad news, how to tell it to userspace ? */
1485                 dev_info(rdev->dev, "GPU reset failed\n");
1486         }
1487
1488         lockmgr(&rdev->exclusive_lock, LK_RELEASE);
1489         return r;
1490 }
1491
1492
1493 /*
1494  * Debugfs
1495  */
1496 #ifdef DUMBBELL_WIP
1497 int radeon_debugfs_add_files(struct radeon_device *rdev,
1498                              struct drm_info_list *files,
1499                              unsigned nfiles)
1500 {
1501         unsigned i;
1502
1503         for (i = 0; i < rdev->debugfs_count; i++) {
1504                 if (rdev->debugfs[i].files == files) {
1505                         /* Already registered */
1506                         return 0;
1507                 }
1508         }
1509
1510         i = rdev->debugfs_count + 1;
1511         if (i > RADEON_DEBUGFS_MAX_COMPONENTS) {
1512                 DRM_ERROR("Reached maximum number of debugfs components.\n");
1513                 DRM_ERROR("Report so we increase "
1514                           "RADEON_DEBUGFS_MAX_COMPONENTS.\n");
1515                 return -EINVAL;
1516         }
1517         rdev->debugfs[rdev->debugfs_count].files = files;
1518         rdev->debugfs[rdev->debugfs_count].num_files = nfiles;
1519         rdev->debugfs_count = i;
1520 #if defined(CONFIG_DEBUG_FS)
1521         drm_debugfs_create_files(files, nfiles,
1522                                  rdev->ddev->control->debugfs_root,
1523                                  rdev->ddev->control);
1524         drm_debugfs_create_files(files, nfiles,
1525                                  rdev->ddev->primary->debugfs_root,
1526                                  rdev->ddev->primary);
1527 #endif
1528         return 0;
1529 }
1530
1531 static void radeon_debugfs_remove_files(struct radeon_device *rdev)
1532 {
1533 #if defined(CONFIG_DEBUG_FS)
1534         unsigned i;
1535
1536         for (i = 0; i < rdev->debugfs_count; i++) {
1537                 drm_debugfs_remove_files(rdev->debugfs[i].files,
1538                                          rdev->debugfs[i].num_files,
1539                                          rdev->ddev->control);
1540                 drm_debugfs_remove_files(rdev->debugfs[i].files,
1541                                          rdev->debugfs[i].num_files,
1542                                          rdev->ddev->primary);
1543         }
1544 #endif
1545 }
1546
1547 #if defined(CONFIG_DEBUG_FS)
1548 int radeon_debugfs_init(struct drm_minor *minor)
1549 {
1550         return 0;
1551 }
1552
1553 void radeon_debugfs_cleanup(struct drm_minor *minor)
1554 {
1555 }
1556 #endif /* DUMBBELL_WIP */
1557 #endif