blob: 410859ee112f9dd52169ab14e098afa00a0e5144 [file] [log] [blame]
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001/*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Dave Airlie
25 * Alex Deucher
26 * Jerome Glisse
27 */
28#include <linux/console.h>
29#include <drm/drmP.h>
30#include <drm/drm_crtc_helper.h>
31#include <drm/radeon_drm.h>
Dave Airlie28d52042009-09-21 14:33:58 +100032#include <linux/vgaarb.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020033#include "radeon_reg.h"
34#include "radeon.h"
35#include "radeon_asic.h"
36#include "atom.h"
37
38/*
Michel Dänzerb1e3a6d2009-06-23 16:12:54 +020039 * Clear GPU surface registers.
40 */
Jerome Glisse3ce0a232009-09-08 10:10:24 +100041void radeon_surface_init(struct radeon_device *rdev)
Michel Dänzerb1e3a6d2009-06-23 16:12:54 +020042{
43 /* FIXME: check this out */
44 if (rdev->family < CHIP_R600) {
45 int i;
46
47 for (i = 0; i < 8; i++) {
48 WREG32(RADEON_SURFACE0_INFO +
49 i * (RADEON_SURFACE1_INFO - RADEON_SURFACE0_INFO),
50 0);
51 }
Dave Airliee024e112009-06-24 09:48:08 +100052 /* enable surfaces */
53 WREG32(RADEON_SURFACE_CNTL, 0);
Michel Dänzerb1e3a6d2009-06-23 16:12:54 +020054 }
55}
56
57/*
Jerome Glisse771fe6b2009-06-05 14:42:42 +020058 * GPU scratch registers helpers function.
59 */
Jerome Glisse3ce0a232009-09-08 10:10:24 +100060void radeon_scratch_init(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +020061{
62 int i;
63
64 /* FIXME: check this out */
65 if (rdev->family < CHIP_R300) {
66 rdev->scratch.num_reg = 5;
67 } else {
68 rdev->scratch.num_reg = 7;
69 }
70 for (i = 0; i < rdev->scratch.num_reg; i++) {
71 rdev->scratch.free[i] = true;
72 rdev->scratch.reg[i] = RADEON_SCRATCH_REG0 + (i * 4);
73 }
74}
75
76int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg)
77{
78 int i;
79
80 for (i = 0; i < rdev->scratch.num_reg; i++) {
81 if (rdev->scratch.free[i]) {
82 rdev->scratch.free[i] = false;
83 *reg = rdev->scratch.reg[i];
84 return 0;
85 }
86 }
87 return -EINVAL;
88}
89
90void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg)
91{
92 int i;
93
94 for (i = 0; i < rdev->scratch.num_reg; i++) {
95 if (rdev->scratch.reg[i] == reg) {
96 rdev->scratch.free[i] = true;
97 return;
98 }
99 }
100}
101
102/*
103 * MC common functions
104 */
105int radeon_mc_setup(struct radeon_device *rdev)
106{
107 uint32_t tmp;
108
109 /* Some chips have an "issue" with the memory controller, the
110 * location must be aligned to the size. We just align it down,
111 * too bad if we walk over the top of system memory, we don't
112 * use DMA without a remapped anyway.
113 * Affected chips are rv280, all r3xx, and all r4xx, but not IGP
114 */
115 /* FGLRX seems to setup like this, VRAM a 0, then GART.
116 */
117 /*
118 * Note: from R6xx the address space is 40bits but here we only
119 * use 32bits (still have to see a card which would exhaust 4G
120 * address space).
121 */
122 if (rdev->mc.vram_location != 0xFFFFFFFFUL) {
123 /* vram location was already setup try to put gtt after
124 * if it fits */
Dave Airlie7a50f012009-07-21 20:39:30 +1000125 tmp = rdev->mc.vram_location + rdev->mc.mc_vram_size;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200126 tmp = (tmp + rdev->mc.gtt_size - 1) & ~(rdev->mc.gtt_size - 1);
127 if ((0xFFFFFFFFUL - tmp) >= rdev->mc.gtt_size) {
128 rdev->mc.gtt_location = tmp;
129 } else {
130 if (rdev->mc.gtt_size >= rdev->mc.vram_location) {
131 printk(KERN_ERR "[drm] GTT too big to fit "
132 "before or after vram location.\n");
133 return -EINVAL;
134 }
135 rdev->mc.gtt_location = 0;
136 }
137 } else if (rdev->mc.gtt_location != 0xFFFFFFFFUL) {
138 /* gtt location was already setup try to put vram before
139 * if it fits */
Dave Airlie7a50f012009-07-21 20:39:30 +1000140 if (rdev->mc.mc_vram_size < rdev->mc.gtt_location) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200141 rdev->mc.vram_location = 0;
142 } else {
143 tmp = rdev->mc.gtt_location + rdev->mc.gtt_size;
Dave Airlie7a50f012009-07-21 20:39:30 +1000144 tmp += (rdev->mc.mc_vram_size - 1);
145 tmp &= ~(rdev->mc.mc_vram_size - 1);
146 if ((0xFFFFFFFFUL - tmp) >= rdev->mc.mc_vram_size) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200147 rdev->mc.vram_location = tmp;
148 } else {
149 printk(KERN_ERR "[drm] vram too big to fit "
150 "before or after GTT location.\n");
151 return -EINVAL;
152 }
153 }
154 } else {
155 rdev->mc.vram_location = 0;
Dave Airlie17332922009-08-07 11:03:26 +1000156 tmp = rdev->mc.mc_vram_size;
157 tmp = (tmp + rdev->mc.gtt_size - 1) & ~(rdev->mc.gtt_size - 1);
158 rdev->mc.gtt_location = tmp;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200159 }
Jerome Glisse9f022dd2009-09-11 15:35:22 +0200160 rdev->mc.vram_start = rdev->mc.vram_location;
161 rdev->mc.vram_end = rdev->mc.vram_location + rdev->mc.mc_vram_size - 1;
162 rdev->mc.gtt_start = rdev->mc.gtt_location;
163 rdev->mc.gtt_end = rdev->mc.gtt_location + rdev->mc.gtt_size - 1;
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000164 DRM_INFO("radeon: VRAM %uM\n", (unsigned)(rdev->mc.mc_vram_size >> 20));
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200165 DRM_INFO("radeon: VRAM from 0x%08X to 0x%08X\n",
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000166 (unsigned)rdev->mc.vram_location,
167 (unsigned)(rdev->mc.vram_location + rdev->mc.mc_vram_size - 1));
168 DRM_INFO("radeon: GTT %uM\n", (unsigned)(rdev->mc.gtt_size >> 20));
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200169 DRM_INFO("radeon: GTT from 0x%08X to 0x%08X\n",
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000170 (unsigned)rdev->mc.gtt_location,
171 (unsigned)(rdev->mc.gtt_location + rdev->mc.gtt_size - 1));
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200172 return 0;
173}
174
175
176/*
177 * GPU helpers function.
178 */
Jerome Glisse9f022dd2009-09-11 15:35:22 +0200179bool radeon_card_posted(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200180{
181 uint32_t reg;
182
183 /* first check CRTCs */
184 if (ASIC_IS_AVIVO(rdev)) {
185 reg = RREG32(AVIVO_D1CRTC_CONTROL) |
186 RREG32(AVIVO_D2CRTC_CONTROL);
187 if (reg & AVIVO_CRTC_EN) {
188 return true;
189 }
190 } else {
191 reg = RREG32(RADEON_CRTC_GEN_CNTL) |
192 RREG32(RADEON_CRTC2_GEN_CNTL);
193 if (reg & RADEON_CRTC_EN) {
194 return true;
195 }
196 }
197
198 /* then check MEM_SIZE, in case the crtcs are off */
199 if (rdev->family >= CHIP_R600)
200 reg = RREG32(R600_CONFIG_MEMSIZE);
201 else
202 reg = RREG32(RADEON_CONFIG_MEMSIZE);
203
204 if (reg)
205 return true;
206
207 return false;
208
209}
210
Dave Airlie72542d72009-12-01 14:06:31 +1000211bool radeon_boot_test_post_card(struct radeon_device *rdev)
212{
213 if (radeon_card_posted(rdev))
214 return true;
215
216 if (rdev->bios) {
217 DRM_INFO("GPU not posted. posting now...\n");
218 if (rdev->is_atom_bios)
219 atom_asic_init(rdev->mode_info.atom_context);
220 else
221 radeon_combios_asic_init(rdev->ddev);
222 return true;
223 } else {
224 dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
225 return false;
226 }
227}
228
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000229int radeon_dummy_page_init(struct radeon_device *rdev)
230{
231 rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
232 if (rdev->dummy_page.page == NULL)
233 return -ENOMEM;
234 rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page,
235 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
236 if (!rdev->dummy_page.addr) {
237 __free_page(rdev->dummy_page.page);
238 rdev->dummy_page.page = NULL;
239 return -ENOMEM;
240 }
241 return 0;
242}
243
244void radeon_dummy_page_fini(struct radeon_device *rdev)
245{
246 if (rdev->dummy_page.page == NULL)
247 return;
248 pci_unmap_page(rdev->pdev, rdev->dummy_page.addr,
249 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
250 __free_page(rdev->dummy_page.page);
251 rdev->dummy_page.page = NULL;
252}
253
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200254
255/*
256 * Registers accessors functions.
257 */
258uint32_t radeon_invalid_rreg(struct radeon_device *rdev, uint32_t reg)
259{
260 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
261 BUG_ON(1);
262 return 0;
263}
264
265void radeon_invalid_wreg(struct radeon_device *rdev, uint32_t reg, uint32_t v)
266{
267 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
268 reg, v);
269 BUG_ON(1);
270}
271
272void radeon_register_accessor_init(struct radeon_device *rdev)
273{
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200274 rdev->mc_rreg = &radeon_invalid_rreg;
275 rdev->mc_wreg = &radeon_invalid_wreg;
276 rdev->pll_rreg = &radeon_invalid_rreg;
277 rdev->pll_wreg = &radeon_invalid_wreg;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200278 rdev->pciep_rreg = &radeon_invalid_rreg;
279 rdev->pciep_wreg = &radeon_invalid_wreg;
280
281 /* Don't change order as we are overridding accessor. */
282 if (rdev->family < CHIP_RV515) {
Dave Airliede1b2892009-08-12 18:43:14 +1000283 rdev->pcie_reg_mask = 0xff;
284 } else {
285 rdev->pcie_reg_mask = 0x7ff;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200286 }
287 /* FIXME: not sure here */
288 if (rdev->family <= CHIP_R580) {
289 rdev->pll_rreg = &r100_pll_rreg;
290 rdev->pll_wreg = &r100_pll_wreg;
291 }
Jerome Glisse905b6822009-09-09 22:24:20 +0200292 if (rdev->family >= CHIP_R420) {
293 rdev->mc_rreg = &r420_mc_rreg;
294 rdev->mc_wreg = &r420_mc_wreg;
295 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200296 if (rdev->family >= CHIP_RV515) {
297 rdev->mc_rreg = &rv515_mc_rreg;
298 rdev->mc_wreg = &rv515_mc_wreg;
299 }
300 if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480) {
301 rdev->mc_rreg = &rs400_mc_rreg;
302 rdev->mc_wreg = &rs400_mc_wreg;
303 }
304 if (rdev->family == CHIP_RS690 || rdev->family == CHIP_RS740) {
305 rdev->mc_rreg = &rs690_mc_rreg;
306 rdev->mc_wreg = &rs690_mc_wreg;
307 }
308 if (rdev->family == CHIP_RS600) {
309 rdev->mc_rreg = &rs600_mc_rreg;
310 rdev->mc_wreg = &rs600_mc_wreg;
311 }
312 if (rdev->family >= CHIP_R600) {
313 rdev->pciep_rreg = &r600_pciep_rreg;
314 rdev->pciep_wreg = &r600_pciep_wreg;
315 }
316}
317
318
319/*
320 * ASIC
321 */
322int radeon_asic_init(struct radeon_device *rdev)
323{
324 radeon_register_accessor_init(rdev);
325 switch (rdev->family) {
326 case CHIP_R100:
327 case CHIP_RV100:
328 case CHIP_RS100:
329 case CHIP_RV200:
330 case CHIP_RS200:
331 case CHIP_R200:
332 case CHIP_RV250:
333 case CHIP_RS300:
334 case CHIP_RV280:
335 rdev->asic = &r100_asic;
336 break;
337 case CHIP_R300:
338 case CHIP_R350:
339 case CHIP_RV350:
340 case CHIP_RV380:
341 rdev->asic = &r300_asic;
Jerome Glisse4aac0472009-09-14 18:29:49 +0200342 if (rdev->flags & RADEON_IS_PCIE) {
Jerome Glisse4aac0472009-09-14 18:29:49 +0200343 rdev->asic->gart_tlb_flush = &rv370_pcie_gart_tlb_flush;
344 rdev->asic->gart_set_page = &rv370_pcie_gart_set_page;
345 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200346 break;
347 case CHIP_R420:
348 case CHIP_R423:
349 case CHIP_RV410:
350 rdev->asic = &r420_asic;
351 break;
352 case CHIP_RS400:
353 case CHIP_RS480:
354 rdev->asic = &rs400_asic;
355 break;
356 case CHIP_RS600:
357 rdev->asic = &rs600_asic;
358 break;
359 case CHIP_RS690:
360 case CHIP_RS740:
361 rdev->asic = &rs690_asic;
362 break;
363 case CHIP_RV515:
364 rdev->asic = &rv515_asic;
365 break;
366 case CHIP_R520:
367 case CHIP_RV530:
368 case CHIP_RV560:
369 case CHIP_RV570:
370 case CHIP_R580:
371 rdev->asic = &r520_asic;
372 break;
373 case CHIP_R600:
374 case CHIP_RV610:
375 case CHIP_RV630:
376 case CHIP_RV620:
377 case CHIP_RV635:
378 case CHIP_RV670:
379 case CHIP_RS780:
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000380 case CHIP_RS880:
381 rdev->asic = &r600_asic;
382 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200383 case CHIP_RV770:
384 case CHIP_RV730:
385 case CHIP_RV710:
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000386 case CHIP_RV740:
387 rdev->asic = &rv770_asic;
388 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200389 default:
390 /* FIXME: not supported yet */
391 return -EINVAL;
392 }
393 return 0;
394}
395
396
397/*
398 * Wrapper around modesetting bits.
399 */
400int radeon_clocks_init(struct radeon_device *rdev)
401{
402 int r;
403
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200404 r = radeon_static_clocks_init(rdev->ddev);
405 if (r) {
406 return r;
407 }
408 DRM_INFO("Clocks initialized !\n");
409 return 0;
410}
411
412void radeon_clocks_fini(struct radeon_device *rdev)
413{
414}
415
416/* ATOM accessor methods */
417static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
418{
419 struct radeon_device *rdev = info->dev->dev_private;
420 uint32_t r;
421
422 r = rdev->pll_rreg(rdev, reg);
423 return r;
424}
425
426static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
427{
428 struct radeon_device *rdev = info->dev->dev_private;
429
430 rdev->pll_wreg(rdev, reg, val);
431}
432
433static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
434{
435 struct radeon_device *rdev = info->dev->dev_private;
436 uint32_t r;
437
438 r = rdev->mc_rreg(rdev, reg);
439 return r;
440}
441
442static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
443{
444 struct radeon_device *rdev = info->dev->dev_private;
445
446 rdev->mc_wreg(rdev, reg, val);
447}
448
449static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
450{
451 struct radeon_device *rdev = info->dev->dev_private;
452
453 WREG32(reg*4, val);
454}
455
456static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
457{
458 struct radeon_device *rdev = info->dev->dev_private;
459 uint32_t r;
460
461 r = RREG32(reg*4);
462 return r;
463}
464
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200465int radeon_atombios_init(struct radeon_device *rdev)
466{
Mathias Fröhlich61c4b242009-10-27 15:08:01 -0400467 struct card_info *atom_card_info =
468 kzalloc(sizeof(struct card_info), GFP_KERNEL);
469
470 if (!atom_card_info)
471 return -ENOMEM;
472
473 rdev->mode_info.atom_card_info = atom_card_info;
474 atom_card_info->dev = rdev->ddev;
475 atom_card_info->reg_read = cail_reg_read;
476 atom_card_info->reg_write = cail_reg_write;
477 atom_card_info->mc_read = cail_mc_read;
478 atom_card_info->mc_write = cail_mc_write;
479 atom_card_info->pll_read = cail_pll_read;
480 atom_card_info->pll_write = cail_pll_write;
481
482 rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200483 radeon_atom_initialize_bios_scratch_regs(rdev->ddev);
Dave Airlied904ef92009-11-17 06:29:46 +1000484 atom_allocate_fb_scratch(rdev->mode_info.atom_context);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200485 return 0;
486}
487
488void radeon_atombios_fini(struct radeon_device *rdev)
489{
Jerome Glisse4a04a842009-12-09 17:39:16 +0100490 if (rdev->mode_info.atom_context) {
491 kfree(rdev->mode_info.atom_context->scratch);
492 kfree(rdev->mode_info.atom_context);
493 }
Mathias Fröhlich61c4b242009-10-27 15:08:01 -0400494 kfree(rdev->mode_info.atom_card_info);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200495}
496
497int radeon_combios_init(struct radeon_device *rdev)
498{
499 radeon_combios_initialize_bios_scratch_regs(rdev->ddev);
500 return 0;
501}
502
503void radeon_combios_fini(struct radeon_device *rdev)
504{
505}
506
Dave Airlie28d52042009-09-21 14:33:58 +1000507/* if we get transitioned to only one device, tak VGA back */
508static unsigned int radeon_vga_set_decode(void *cookie, bool state)
509{
510 struct radeon_device *rdev = cookie;
Dave Airlie28d52042009-09-21 14:33:58 +1000511 radeon_vga_set_state(rdev, state);
512 if (state)
513 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
514 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
515 else
516 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
517}
Dave Airliec1176d62009-10-08 14:03:05 +1000518
Jerome Glisseb574f252009-10-06 19:04:29 +0200519void radeon_agp_disable(struct radeon_device *rdev)
520{
521 rdev->flags &= ~RADEON_IS_AGP;
522 if (rdev->family >= CHIP_R600) {
523 DRM_INFO("Forcing AGP to PCIE mode\n");
524 rdev->flags |= RADEON_IS_PCIE;
525 } else if (rdev->family >= CHIP_RV515 ||
526 rdev->family == CHIP_RV380 ||
527 rdev->family == CHIP_RV410 ||
528 rdev->family == CHIP_R423) {
529 DRM_INFO("Forcing AGP to PCIE mode\n");
530 rdev->flags |= RADEON_IS_PCIE;
531 rdev->asic->gart_tlb_flush = &rv370_pcie_gart_tlb_flush;
532 rdev->asic->gart_set_page = &rv370_pcie_gart_set_page;
533 } else {
534 DRM_INFO("Forcing AGP to PCI mode\n");
535 rdev->flags |= RADEON_IS_PCI;
536 rdev->asic->gart_tlb_flush = &r100_pci_gart_tlb_flush;
537 rdev->asic->gart_set_page = &r100_pci_gart_set_page;
538 }
539}
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200540
541/*
542 * Radeon device.
543 */
544int radeon_device_init(struct radeon_device *rdev,
545 struct drm_device *ddev,
546 struct pci_dev *pdev,
547 uint32_t flags)
548{
Jerome Glisse6cf8a3f52009-09-10 21:46:48 +0200549 int r;
Dave Airliead49f502009-07-10 22:36:26 +1000550 int dma_bits;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200551
552 DRM_INFO("radeon: Initializing kernel modesetting.\n");
553 rdev->shutdown = false;
Jerome Glisse9f022dd2009-09-11 15:35:22 +0200554 rdev->dev = &pdev->dev;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200555 rdev->ddev = ddev;
556 rdev->pdev = pdev;
557 rdev->flags = flags;
558 rdev->family = flags & RADEON_FAMILY_MASK;
559 rdev->is_atom_bios = false;
560 rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT;
561 rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
562 rdev->gpu_lockup = false;
Jerome Glisse733289c2009-09-16 15:24:21 +0200563 rdev->accel_working = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200564 /* mutex initialization are all done here so we
565 * can recall function without having locking issues */
566 mutex_init(&rdev->cs_mutex);
567 mutex_init(&rdev->ib_pool.mutex);
568 mutex_init(&rdev->cp.mutex);
Alex Deucherd8f60cf2009-12-01 13:43:46 -0500569 if (rdev->family >= CHIP_R600)
570 spin_lock_init(&rdev->ih.lock);
Jerome Glisse4c788672009-11-20 14:29:23 +0100571 mutex_init(&rdev->gem.mutex);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200572 rwlock_init(&rdev->fence_drv.lock);
Jerome Glisse9f022dd2009-09-11 15:35:22 +0200573 INIT_LIST_HEAD(&rdev->gem.objects);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200574
Alex Deucherd4877cf2009-12-04 16:56:37 -0500575 /* setup workqueue */
576 rdev->wq = create_workqueue("radeon");
577 if (rdev->wq == NULL)
578 return -ENOMEM;
579
Jerome Glisse4aac0472009-09-14 18:29:49 +0200580 /* Set asic functions */
581 r = radeon_asic_init(rdev);
582 if (r) {
583 return r;
584 }
585
Jerome Glisse30256a32009-11-30 17:47:59 +0100586 if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) {
Jerome Glisseb574f252009-10-06 19:04:29 +0200587 radeon_agp_disable(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200588 }
589
Dave Airliead49f502009-07-10 22:36:26 +1000590 /* set DMA mask + need_dma32 flags.
591 * PCIE - can handle 40-bits.
592 * IGP - can handle 40-bits (in theory)
593 * AGP - generally dma32 is safest
594 * PCI - only dma32
595 */
596 rdev->need_dma32 = false;
597 if (rdev->flags & RADEON_IS_AGP)
598 rdev->need_dma32 = true;
599 if (rdev->flags & RADEON_IS_PCI)
600 rdev->need_dma32 = true;
601
602 dma_bits = rdev->need_dma32 ? 32 : 40;
603 r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200604 if (r) {
605 printk(KERN_WARNING "radeon: No suitable DMA available.\n");
606 }
607
608 /* Registers mapping */
609 /* TODO: block userspace mapping of io register */
610 rdev->rmmio_base = drm_get_resource_start(rdev->ddev, 2);
611 rdev->rmmio_size = drm_get_resource_len(rdev->ddev, 2);
612 rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size);
613 if (rdev->rmmio == NULL) {
614 return -ENOMEM;
615 }
616 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base);
617 DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size);
618
Dave Airlie28d52042009-09-21 14:33:58 +1000619 /* if we have > 1 VGA cards, then disable the radeon VGA resources */
Dave Airlie93239ea2009-10-28 11:09:58 +1000620 /* this will fail for cards that aren't VGA class devices, just
621 * ignore it */
622 vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode);
Dave Airlie28d52042009-09-21 14:33:58 +1000623
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000624 r = radeon_init(rdev);
Jerome Glisseb574f252009-10-06 19:04:29 +0200625 if (r)
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000626 return r;
Michel Dänzerb1e3a6d2009-06-23 16:12:54 +0200627
Jerome Glisseb574f252009-10-06 19:04:29 +0200628 if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) {
629 /* Acceleration not working on AGP card try again
630 * with fallback to PCI or PCIE GART
631 */
Jerome Glisse1a029b72009-10-06 19:04:30 +0200632 radeon_gpu_reset(rdev);
Jerome Glisseb574f252009-10-06 19:04:29 +0200633 radeon_fini(rdev);
634 radeon_agp_disable(rdev);
635 r = radeon_init(rdev);
Jerome Glisse4aac0472009-09-14 18:29:49 +0200636 if (r)
637 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200638 }
Michel Dänzerecc0b322009-07-21 11:23:57 +0200639 if (radeon_testing) {
640 radeon_test_moves(rdev);
641 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200642 if (radeon_benchmarking) {
643 radeon_benchmark(rdev);
644 }
Jerome Glisse6cf8a3f52009-09-10 21:46:48 +0200645 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200646}
647
648void radeon_device_fini(struct radeon_device *rdev)
649{
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200650 DRM_INFO("radeon: finishing device.\n");
651 rdev->shutdown = true;
Jerome Glisse62a8ea32009-10-01 18:02:11 +0200652 radeon_fini(rdev);
Alex Deucherd4877cf2009-12-04 16:56:37 -0500653 destroy_workqueue(rdev->wq);
Dave Airliec1176d62009-10-08 14:03:05 +1000654 vga_client_register(rdev->pdev, NULL, NULL, NULL);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200655 iounmap(rdev->rmmio);
656 rdev->rmmio = NULL;
657}
658
659
660/*
661 * Suspend & resume.
662 */
663int radeon_suspend_kms(struct drm_device *dev, pm_message_t state)
664{
665 struct radeon_device *rdev = dev->dev_private;
666 struct drm_crtc *crtc;
Jerome Glisse4c788672009-11-20 14:29:23 +0100667 int r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200668
669 if (dev == NULL || rdev == NULL) {
670 return -ENODEV;
671 }
672 if (state.event == PM_EVENT_PRETHAW) {
673 return 0;
674 }
675 /* unpin the front buffers */
676 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
677 struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->fb);
Jerome Glisse4c788672009-11-20 14:29:23 +0100678 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200679
680 if (rfb == NULL || rfb->obj == NULL) {
681 continue;
682 }
683 robj = rfb->obj->driver_private;
Jerome Glisse4c788672009-11-20 14:29:23 +0100684 if (robj != rdev->fbdev_rbo) {
685 r = radeon_bo_reserve(robj, false);
686 if (unlikely(r == 0)) {
687 radeon_bo_unpin(robj);
688 radeon_bo_unreserve(robj);
689 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200690 }
691 }
692 /* evict vram memory */
Jerome Glisse4c788672009-11-20 14:29:23 +0100693 radeon_bo_evict_vram(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200694 /* wait for gpu to finish processing current batch */
695 radeon_fence_wait_last(rdev);
696
Yang Zhaof657c2a2009-09-15 12:21:01 +1000697 radeon_save_bios_scratch_regs(rdev);
698
Jerome Glisse62a8ea32009-10-01 18:02:11 +0200699 radeon_suspend(rdev);
Alex Deucherd4877cf2009-12-04 16:56:37 -0500700 radeon_hpd_fini(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200701 /* evict remaining vram memory */
Jerome Glisse4c788672009-11-20 14:29:23 +0100702 radeon_bo_evict_vram(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200703
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200704 pci_save_state(dev->pdev);
705 if (state.event == PM_EVENT_SUSPEND) {
706 /* Shut down the device */
707 pci_disable_device(dev->pdev);
708 pci_set_power_state(dev->pdev, PCI_D3hot);
709 }
710 acquire_console_sem();
711 fb_set_suspend(rdev->fbdev_info, 1);
712 release_console_sem();
713 return 0;
714}
715
716int radeon_resume_kms(struct drm_device *dev)
717{
718 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200719
720 acquire_console_sem();
721 pci_set_power_state(dev->pdev, PCI_D0);
722 pci_restore_state(dev->pdev);
723 if (pci_enable_device(dev->pdev)) {
724 release_console_sem();
725 return -1;
726 }
727 pci_set_master(dev->pdev);
Dave Airlie0ebf1712009-11-05 15:39:10 +1000728 /* resume AGP if in use */
729 radeon_agp_resume(rdev);
Jerome Glisse62a8ea32009-10-01 18:02:11 +0200730 radeon_resume(rdev);
Yang Zhaof657c2a2009-09-15 12:21:01 +1000731 radeon_restore_bios_scratch_regs(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200732 fb_set_suspend(rdev->fbdev_info, 0);
733 release_console_sem();
734
Alex Deucherd4877cf2009-12-04 16:56:37 -0500735 /* reset hpd state */
736 radeon_hpd_init(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200737 /* blat the mode back in */
738 drm_helper_resume_force_mode(dev);
739 return 0;
740}
741
742
743/*
744 * Debugfs
745 */
746struct radeon_debugfs {
747 struct drm_info_list *files;
748 unsigned num_files;
749};
750static struct radeon_debugfs _radeon_debugfs[RADEON_DEBUGFS_MAX_NUM_FILES];
751static unsigned _radeon_debugfs_count = 0;
752
753int radeon_debugfs_add_files(struct radeon_device *rdev,
754 struct drm_info_list *files,
755 unsigned nfiles)
756{
757 unsigned i;
758
759 for (i = 0; i < _radeon_debugfs_count; i++) {
760 if (_radeon_debugfs[i].files == files) {
761 /* Already registered */
762 return 0;
763 }
764 }
765 if ((_radeon_debugfs_count + nfiles) > RADEON_DEBUGFS_MAX_NUM_FILES) {
766 DRM_ERROR("Reached maximum number of debugfs files.\n");
767 DRM_ERROR("Report so we increase RADEON_DEBUGFS_MAX_NUM_FILES.\n");
768 return -EINVAL;
769 }
770 _radeon_debugfs[_radeon_debugfs_count].files = files;
771 _radeon_debugfs[_radeon_debugfs_count].num_files = nfiles;
772 _radeon_debugfs_count++;
773#if defined(CONFIG_DEBUG_FS)
774 drm_debugfs_create_files(files, nfiles,
775 rdev->ddev->control->debugfs_root,
776 rdev->ddev->control);
777 drm_debugfs_create_files(files, nfiles,
778 rdev->ddev->primary->debugfs_root,
779 rdev->ddev->primary);
780#endif
781 return 0;
782}
783
784#if defined(CONFIG_DEBUG_FS)
785int radeon_debugfs_init(struct drm_minor *minor)
786{
787 return 0;
788}
789
790void radeon_debugfs_cleanup(struct drm_minor *minor)
791{
792 unsigned i;
793
794 for (i = 0; i < _radeon_debugfs_count; i++) {
795 drm_debugfs_remove_files(_radeon_debugfs[i].files,
796 _radeon_debugfs[i].num_files, minor);
797 }
798}
799#endif