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