blob: 18ac29c505cfe3c66f17e5f0de49f86abfefe73a [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
Dave Airlie550e2d92009-12-09 14:15:38 +100047 for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
48 if (rdev->surface_regs[i].bo)
49 radeon_bo_get_surface_reg(rdev->surface_regs[i].bo);
50 else
51 radeon_clear_surface_reg(rdev, i);
Michel Dänzerb1e3a6d2009-06-23 16:12:54 +020052 }
Dave Airliee024e112009-06-24 09:48:08 +100053 /* enable surfaces */
54 WREG32(RADEON_SURFACE_CNTL, 0);
Michel Dänzerb1e3a6d2009-06-23 16:12:54 +020055 }
56}
57
58/*
Jerome Glisse771fe6b2009-06-05 14:42:42 +020059 * GPU scratch registers helpers function.
60 */
Jerome Glisse3ce0a232009-09-08 10:10:24 +100061void radeon_scratch_init(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +020062{
63 int i;
64
65 /* FIXME: check this out */
66 if (rdev->family < CHIP_R300) {
67 rdev->scratch.num_reg = 5;
68 } else {
69 rdev->scratch.num_reg = 7;
70 }
71 for (i = 0; i < rdev->scratch.num_reg; i++) {
72 rdev->scratch.free[i] = true;
73 rdev->scratch.reg[i] = RADEON_SCRATCH_REG0 + (i * 4);
74 }
75}
76
77int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg)
78{
79 int i;
80
81 for (i = 0; i < rdev->scratch.num_reg; i++) {
82 if (rdev->scratch.free[i]) {
83 rdev->scratch.free[i] = false;
84 *reg = rdev->scratch.reg[i];
85 return 0;
86 }
87 }
88 return -EINVAL;
89}
90
91void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg)
92{
93 int i;
94
95 for (i = 0; i < rdev->scratch.num_reg; i++) {
96 if (rdev->scratch.reg[i] == reg) {
97 rdev->scratch.free[i] = true;
98 return;
99 }
100 }
101}
102
103/*
104 * MC common functions
105 */
106int radeon_mc_setup(struct radeon_device *rdev)
107{
108 uint32_t tmp;
109
110 /* Some chips have an "issue" with the memory controller, the
111 * location must be aligned to the size. We just align it down,
112 * too bad if we walk over the top of system memory, we don't
113 * use DMA without a remapped anyway.
114 * Affected chips are rv280, all r3xx, and all r4xx, but not IGP
115 */
116 /* FGLRX seems to setup like this, VRAM a 0, then GART.
117 */
118 /*
119 * Note: from R6xx the address space is 40bits but here we only
120 * use 32bits (still have to see a card which would exhaust 4G
121 * address space).
122 */
123 if (rdev->mc.vram_location != 0xFFFFFFFFUL) {
124 /* vram location was already setup try to put gtt after
125 * if it fits */
Dave Airlie7a50f012009-07-21 20:39:30 +1000126 tmp = rdev->mc.vram_location + rdev->mc.mc_vram_size;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200127 tmp = (tmp + rdev->mc.gtt_size - 1) & ~(rdev->mc.gtt_size - 1);
128 if ((0xFFFFFFFFUL - tmp) >= rdev->mc.gtt_size) {
129 rdev->mc.gtt_location = tmp;
130 } else {
131 if (rdev->mc.gtt_size >= rdev->mc.vram_location) {
132 printk(KERN_ERR "[drm] GTT too big to fit "
133 "before or after vram location.\n");
134 return -EINVAL;
135 }
136 rdev->mc.gtt_location = 0;
137 }
138 } else if (rdev->mc.gtt_location != 0xFFFFFFFFUL) {
139 /* gtt location was already setup try to put vram before
140 * if it fits */
Dave Airlie7a50f012009-07-21 20:39:30 +1000141 if (rdev->mc.mc_vram_size < rdev->mc.gtt_location) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200142 rdev->mc.vram_location = 0;
143 } else {
144 tmp = rdev->mc.gtt_location + rdev->mc.gtt_size;
Dave Airlie7a50f012009-07-21 20:39:30 +1000145 tmp += (rdev->mc.mc_vram_size - 1);
146 tmp &= ~(rdev->mc.mc_vram_size - 1);
147 if ((0xFFFFFFFFUL - tmp) >= rdev->mc.mc_vram_size) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200148 rdev->mc.vram_location = tmp;
149 } else {
150 printk(KERN_ERR "[drm] vram too big to fit "
151 "before or after GTT location.\n");
152 return -EINVAL;
153 }
154 }
155 } else {
156 rdev->mc.vram_location = 0;
Dave Airlie17332922009-08-07 11:03:26 +1000157 tmp = rdev->mc.mc_vram_size;
158 tmp = (tmp + rdev->mc.gtt_size - 1) & ~(rdev->mc.gtt_size - 1);
159 rdev->mc.gtt_location = tmp;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200160 }
Jerome Glisse9f022dd2009-09-11 15:35:22 +0200161 rdev->mc.vram_start = rdev->mc.vram_location;
162 rdev->mc.vram_end = rdev->mc.vram_location + rdev->mc.mc_vram_size - 1;
163 rdev->mc.gtt_start = rdev->mc.gtt_location;
164 rdev->mc.gtt_end = rdev->mc.gtt_location + rdev->mc.gtt_size - 1;
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000165 DRM_INFO("radeon: VRAM %uM\n", (unsigned)(rdev->mc.mc_vram_size >> 20));
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200166 DRM_INFO("radeon: VRAM from 0x%08X to 0x%08X\n",
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000167 (unsigned)rdev->mc.vram_location,
168 (unsigned)(rdev->mc.vram_location + rdev->mc.mc_vram_size - 1));
169 DRM_INFO("radeon: GTT %uM\n", (unsigned)(rdev->mc.gtt_size >> 20));
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200170 DRM_INFO("radeon: GTT from 0x%08X to 0x%08X\n",
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000171 (unsigned)rdev->mc.gtt_location,
172 (unsigned)(rdev->mc.gtt_location + rdev->mc.gtt_size - 1));
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200173 return 0;
174}
175
176
177/*
178 * GPU helpers function.
179 */
Jerome Glisse9f022dd2009-09-11 15:35:22 +0200180bool radeon_card_posted(struct radeon_device *rdev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200181{
182 uint32_t reg;
183
184 /* first check CRTCs */
185 if (ASIC_IS_AVIVO(rdev)) {
186 reg = RREG32(AVIVO_D1CRTC_CONTROL) |
187 RREG32(AVIVO_D2CRTC_CONTROL);
188 if (reg & AVIVO_CRTC_EN) {
189 return true;
190 }
191 } else {
192 reg = RREG32(RADEON_CRTC_GEN_CNTL) |
193 RREG32(RADEON_CRTC2_GEN_CNTL);
194 if (reg & RADEON_CRTC_EN) {
195 return true;
196 }
197 }
198
199 /* then check MEM_SIZE, in case the crtcs are off */
200 if (rdev->family >= CHIP_R600)
201 reg = RREG32(R600_CONFIG_MEMSIZE);
202 else
203 reg = RREG32(RADEON_CONFIG_MEMSIZE);
204
205 if (reg)
206 return true;
207
208 return false;
209
210}
211
Dave Airlie72542d72009-12-01 14:06:31 +1000212bool radeon_boot_test_post_card(struct radeon_device *rdev)
213{
214 if (radeon_card_posted(rdev))
215 return true;
216
217 if (rdev->bios) {
218 DRM_INFO("GPU not posted. posting now...\n");
219 if (rdev->is_atom_bios)
220 atom_asic_init(rdev->mode_info.atom_context);
221 else
222 radeon_combios_asic_init(rdev->ddev);
223 return true;
224 } else {
225 dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n");
226 return false;
227 }
228}
229
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000230int radeon_dummy_page_init(struct radeon_device *rdev)
231{
232 rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
233 if (rdev->dummy_page.page == NULL)
234 return -ENOMEM;
235 rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page,
236 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
237 if (!rdev->dummy_page.addr) {
238 __free_page(rdev->dummy_page.page);
239 rdev->dummy_page.page = NULL;
240 return -ENOMEM;
241 }
242 return 0;
243}
244
245void radeon_dummy_page_fini(struct radeon_device *rdev)
246{
247 if (rdev->dummy_page.page == NULL)
248 return;
249 pci_unmap_page(rdev->pdev, rdev->dummy_page.addr,
250 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
251 __free_page(rdev->dummy_page.page);
252 rdev->dummy_page.page = NULL;
253}
254
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200255
256/*
257 * Registers accessors functions.
258 */
259uint32_t radeon_invalid_rreg(struct radeon_device *rdev, uint32_t reg)
260{
261 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
262 BUG_ON(1);
263 return 0;
264}
265
266void radeon_invalid_wreg(struct radeon_device *rdev, uint32_t reg, uint32_t v)
267{
268 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
269 reg, v);
270 BUG_ON(1);
271}
272
273void radeon_register_accessor_init(struct radeon_device *rdev)
274{
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200275 rdev->mc_rreg = &radeon_invalid_rreg;
276 rdev->mc_wreg = &radeon_invalid_wreg;
277 rdev->pll_rreg = &radeon_invalid_rreg;
278 rdev->pll_wreg = &radeon_invalid_wreg;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200279 rdev->pciep_rreg = &radeon_invalid_rreg;
280 rdev->pciep_wreg = &radeon_invalid_wreg;
281
282 /* Don't change order as we are overridding accessor. */
283 if (rdev->family < CHIP_RV515) {
Dave Airliede1b2892009-08-12 18:43:14 +1000284 rdev->pcie_reg_mask = 0xff;
285 } else {
286 rdev->pcie_reg_mask = 0x7ff;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200287 }
288 /* FIXME: not sure here */
289 if (rdev->family <= CHIP_R580) {
290 rdev->pll_rreg = &r100_pll_rreg;
291 rdev->pll_wreg = &r100_pll_wreg;
292 }
Jerome Glisse905b6822009-09-09 22:24:20 +0200293 if (rdev->family >= CHIP_R420) {
294 rdev->mc_rreg = &r420_mc_rreg;
295 rdev->mc_wreg = &r420_mc_wreg;
296 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200297 if (rdev->family >= CHIP_RV515) {
298 rdev->mc_rreg = &rv515_mc_rreg;
299 rdev->mc_wreg = &rv515_mc_wreg;
300 }
301 if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480) {
302 rdev->mc_rreg = &rs400_mc_rreg;
303 rdev->mc_wreg = &rs400_mc_wreg;
304 }
305 if (rdev->family == CHIP_RS690 || rdev->family == CHIP_RS740) {
306 rdev->mc_rreg = &rs690_mc_rreg;
307 rdev->mc_wreg = &rs690_mc_wreg;
308 }
309 if (rdev->family == CHIP_RS600) {
310 rdev->mc_rreg = &rs600_mc_rreg;
311 rdev->mc_wreg = &rs600_mc_wreg;
312 }
313 if (rdev->family >= CHIP_R600) {
314 rdev->pciep_rreg = &r600_pciep_rreg;
315 rdev->pciep_wreg = &r600_pciep_wreg;
316 }
317}
318
319
320/*
321 * ASIC
322 */
323int radeon_asic_init(struct radeon_device *rdev)
324{
325 radeon_register_accessor_init(rdev);
326 switch (rdev->family) {
327 case CHIP_R100:
328 case CHIP_RV100:
329 case CHIP_RS100:
330 case CHIP_RV200:
331 case CHIP_RS200:
332 case CHIP_R200:
333 case CHIP_RV250:
334 case CHIP_RS300:
335 case CHIP_RV280:
336 rdev->asic = &r100_asic;
337 break;
338 case CHIP_R300:
339 case CHIP_R350:
340 case CHIP_RV350:
341 case CHIP_RV380:
342 rdev->asic = &r300_asic;
Jerome Glisse4aac0472009-09-14 18:29:49 +0200343 if (rdev->flags & RADEON_IS_PCIE) {
Jerome Glisse4aac0472009-09-14 18:29:49 +0200344 rdev->asic->gart_tlb_flush = &rv370_pcie_gart_tlb_flush;
345 rdev->asic->gart_set_page = &rv370_pcie_gart_set_page;
346 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200347 break;
348 case CHIP_R420:
349 case CHIP_R423:
350 case CHIP_RV410:
351 rdev->asic = &r420_asic;
352 break;
353 case CHIP_RS400:
354 case CHIP_RS480:
355 rdev->asic = &rs400_asic;
356 break;
357 case CHIP_RS600:
358 rdev->asic = &rs600_asic;
359 break;
360 case CHIP_RS690:
361 case CHIP_RS740:
362 rdev->asic = &rs690_asic;
363 break;
364 case CHIP_RV515:
365 rdev->asic = &rv515_asic;
366 break;
367 case CHIP_R520:
368 case CHIP_RV530:
369 case CHIP_RV560:
370 case CHIP_RV570:
371 case CHIP_R580:
372 rdev->asic = &r520_asic;
373 break;
374 case CHIP_R600:
375 case CHIP_RV610:
376 case CHIP_RV630:
377 case CHIP_RV620:
378 case CHIP_RV635:
379 case CHIP_RV670:
380 case CHIP_RS780:
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000381 case CHIP_RS880:
382 rdev->asic = &r600_asic;
383 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200384 case CHIP_RV770:
385 case CHIP_RV730:
386 case CHIP_RV710:
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000387 case CHIP_RV740:
388 rdev->asic = &rv770_asic;
389 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200390 default:
391 /* FIXME: not supported yet */
392 return -EINVAL;
393 }
Rafał Miłecki5ea597f2009-12-17 13:50:09 +0100394
395 if (rdev->flags & RADEON_IS_IGP) {
396 rdev->asic->get_memory_clock = NULL;
397 rdev->asic->set_memory_clock = NULL;
398 }
399
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200400 return 0;
401}
402
403
404/*
405 * Wrapper around modesetting bits.
406 */
407int radeon_clocks_init(struct radeon_device *rdev)
408{
409 int r;
410
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200411 r = radeon_static_clocks_init(rdev->ddev);
412 if (r) {
413 return r;
414 }
415 DRM_INFO("Clocks initialized !\n");
416 return 0;
417}
418
419void radeon_clocks_fini(struct radeon_device *rdev)
420{
421}
422
423/* ATOM accessor methods */
424static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
425{
426 struct radeon_device *rdev = info->dev->dev_private;
427 uint32_t r;
428
429 r = rdev->pll_rreg(rdev, reg);
430 return r;
431}
432
433static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
434{
435 struct radeon_device *rdev = info->dev->dev_private;
436
437 rdev->pll_wreg(rdev, reg, val);
438}
439
440static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
441{
442 struct radeon_device *rdev = info->dev->dev_private;
443 uint32_t r;
444
445 r = rdev->mc_rreg(rdev, reg);
446 return r;
447}
448
449static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
450{
451 struct radeon_device *rdev = info->dev->dev_private;
452
453 rdev->mc_wreg(rdev, reg, val);
454}
455
456static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
457{
458 struct radeon_device *rdev = info->dev->dev_private;
459
460 WREG32(reg*4, val);
461}
462
463static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
464{
465 struct radeon_device *rdev = info->dev->dev_private;
466 uint32_t r;
467
468 r = RREG32(reg*4);
469 return r;
470}
471
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200472int radeon_atombios_init(struct radeon_device *rdev)
473{
Mathias Fröhlich61c4b242009-10-27 15:08:01 -0400474 struct card_info *atom_card_info =
475 kzalloc(sizeof(struct card_info), GFP_KERNEL);
476
477 if (!atom_card_info)
478 return -ENOMEM;
479
480 rdev->mode_info.atom_card_info = atom_card_info;
481 atom_card_info->dev = rdev->ddev;
482 atom_card_info->reg_read = cail_reg_read;
483 atom_card_info->reg_write = cail_reg_write;
484 atom_card_info->mc_read = cail_mc_read;
485 atom_card_info->mc_write = cail_mc_write;
486 atom_card_info->pll_read = cail_pll_read;
487 atom_card_info->pll_write = cail_pll_write;
488
489 rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios);
Rafał Miłeckic31ad972009-12-17 00:00:46 +0100490 mutex_init(&rdev->mode_info.atom_context->mutex);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200491 radeon_atom_initialize_bios_scratch_regs(rdev->ddev);
Dave Airlied904ef92009-11-17 06:29:46 +1000492 atom_allocate_fb_scratch(rdev->mode_info.atom_context);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200493 return 0;
494}
495
496void radeon_atombios_fini(struct radeon_device *rdev)
497{
Jerome Glisse4a04a842009-12-09 17:39:16 +0100498 if (rdev->mode_info.atom_context) {
499 kfree(rdev->mode_info.atom_context->scratch);
500 kfree(rdev->mode_info.atom_context);
501 }
Mathias Fröhlich61c4b242009-10-27 15:08:01 -0400502 kfree(rdev->mode_info.atom_card_info);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200503}
504
505int radeon_combios_init(struct radeon_device *rdev)
506{
507 radeon_combios_initialize_bios_scratch_regs(rdev->ddev);
508 return 0;
509}
510
511void radeon_combios_fini(struct radeon_device *rdev)
512{
513}
514
Dave Airlie28d52042009-09-21 14:33:58 +1000515/* if we get transitioned to only one device, tak VGA back */
516static unsigned int radeon_vga_set_decode(void *cookie, bool state)
517{
518 struct radeon_device *rdev = cookie;
Dave Airlie28d52042009-09-21 14:33:58 +1000519 radeon_vga_set_state(rdev, state);
520 if (state)
521 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
522 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
523 else
524 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
525}
Dave Airliec1176d62009-10-08 14:03:05 +1000526
Jerome Glisseb574f252009-10-06 19:04:29 +0200527void radeon_agp_disable(struct radeon_device *rdev)
528{
529 rdev->flags &= ~RADEON_IS_AGP;
530 if (rdev->family >= CHIP_R600) {
531 DRM_INFO("Forcing AGP to PCIE mode\n");
532 rdev->flags |= RADEON_IS_PCIE;
533 } else if (rdev->family >= CHIP_RV515 ||
534 rdev->family == CHIP_RV380 ||
535 rdev->family == CHIP_RV410 ||
536 rdev->family == CHIP_R423) {
537 DRM_INFO("Forcing AGP to PCIE mode\n");
538 rdev->flags |= RADEON_IS_PCIE;
539 rdev->asic->gart_tlb_flush = &rv370_pcie_gart_tlb_flush;
540 rdev->asic->gart_set_page = &rv370_pcie_gart_set_page;
541 } else {
542 DRM_INFO("Forcing AGP to PCI mode\n");
543 rdev->flags |= RADEON_IS_PCI;
544 rdev->asic->gart_tlb_flush = &r100_pci_gart_tlb_flush;
545 rdev->asic->gart_set_page = &r100_pci_gart_set_page;
546 }
Jerome Glisse700a0cc2010-01-13 15:16:38 +0100547 rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
Jerome Glisseb574f252009-10-06 19:04:29 +0200548}
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200549
Jerome Glisse36421332009-12-11 21:18:34 +0100550void radeon_check_arguments(struct radeon_device *rdev)
551{
552 /* vramlimit must be a power of two */
553 switch (radeon_vram_limit) {
554 case 0:
555 case 4:
556 case 8:
557 case 16:
558 case 32:
559 case 64:
560 case 128:
561 case 256:
562 case 512:
563 case 1024:
564 case 2048:
565 case 4096:
566 break;
567 default:
568 dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n",
569 radeon_vram_limit);
570 radeon_vram_limit = 0;
571 break;
572 }
573 radeon_vram_limit = radeon_vram_limit << 20;
574 /* gtt size must be power of two and greater or equal to 32M */
575 switch (radeon_gart_size) {
576 case 4:
577 case 8:
578 case 16:
579 dev_warn(rdev->dev, "gart size (%d) too small forcing to 512M\n",
580 radeon_gart_size);
581 radeon_gart_size = 512;
582 break;
583 case 32:
584 case 64:
585 case 128:
586 case 256:
587 case 512:
588 case 1024:
589 case 2048:
590 case 4096:
591 break;
592 default:
593 dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n",
594 radeon_gart_size);
595 radeon_gart_size = 512;
596 break;
597 }
598 rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
599 /* AGP mode can only be -1, 1, 2, 4, 8 */
600 switch (radeon_agpmode) {
601 case -1:
602 case 0:
603 case 1:
604 case 2:
605 case 4:
606 case 8:
607 break;
608 default:
609 dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: "
610 "-1, 0, 1, 2, 4, 8)\n", radeon_agpmode);
611 radeon_agpmode = 0;
612 break;
613 }
614}
615
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200616int radeon_device_init(struct radeon_device *rdev,
617 struct drm_device *ddev,
618 struct pci_dev *pdev,
619 uint32_t flags)
620{
Jerome Glisse6cf8a3f2009-09-10 21:46:48 +0200621 int r;
Dave Airliead49f502009-07-10 22:36:26 +1000622 int dma_bits;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200623
624 DRM_INFO("radeon: Initializing kernel modesetting.\n");
625 rdev->shutdown = false;
Jerome Glisse9f022dd2009-09-11 15:35:22 +0200626 rdev->dev = &pdev->dev;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200627 rdev->ddev = ddev;
628 rdev->pdev = pdev;
629 rdev->flags = flags;
630 rdev->family = flags & RADEON_FAMILY_MASK;
631 rdev->is_atom_bios = false;
632 rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT;
633 rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
634 rdev->gpu_lockup = false;
Jerome Glisse733289c2009-09-16 15:24:21 +0200635 rdev->accel_working = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200636 /* mutex initialization are all done here so we
637 * can recall function without having locking issues */
638 mutex_init(&rdev->cs_mutex);
639 mutex_init(&rdev->ib_pool.mutex);
640 mutex_init(&rdev->cp.mutex);
Alex Deucher40bacf12009-12-23 03:23:21 -0500641 mutex_init(&rdev->dc_hw_i2c_mutex);
Alex Deucherd8f60cf2009-12-01 13:43:46 -0500642 if (rdev->family >= CHIP_R600)
643 spin_lock_init(&rdev->ih.lock);
Jerome Glisse4c788672009-11-20 14:29:23 +0100644 mutex_init(&rdev->gem.mutex);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200645 rwlock_init(&rdev->fence_drv.lock);
Jerome Glisse9f022dd2009-09-11 15:35:22 +0200646 INIT_LIST_HEAD(&rdev->gem.objects);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200647
Alex Deucherd4877cf2009-12-04 16:56:37 -0500648 /* setup workqueue */
649 rdev->wq = create_workqueue("radeon");
650 if (rdev->wq == NULL)
651 return -ENOMEM;
652
Jerome Glisse4aac0472009-09-14 18:29:49 +0200653 /* Set asic functions */
654 r = radeon_asic_init(rdev);
Jerome Glisse36421332009-12-11 21:18:34 +0100655 if (r)
Jerome Glisse4aac0472009-09-14 18:29:49 +0200656 return r;
Jerome Glisse36421332009-12-11 21:18:34 +0100657 radeon_check_arguments(rdev);
Jerome Glisse4aac0472009-09-14 18:29:49 +0200658
Jerome Glisse30256a32009-11-30 17:47:59 +0100659 if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) {
Jerome Glisseb574f252009-10-06 19:04:29 +0200660 radeon_agp_disable(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200661 }
662
Dave Airliead49f502009-07-10 22:36:26 +1000663 /* set DMA mask + need_dma32 flags.
664 * PCIE - can handle 40-bits.
665 * IGP - can handle 40-bits (in theory)
666 * AGP - generally dma32 is safest
667 * PCI - only dma32
668 */
669 rdev->need_dma32 = false;
670 if (rdev->flags & RADEON_IS_AGP)
671 rdev->need_dma32 = true;
672 if (rdev->flags & RADEON_IS_PCI)
673 rdev->need_dma32 = true;
674
675 dma_bits = rdev->need_dma32 ? 32 : 40;
676 r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200677 if (r) {
678 printk(KERN_WARNING "radeon: No suitable DMA available.\n");
679 }
680
681 /* Registers mapping */
682 /* TODO: block userspace mapping of io register */
683 rdev->rmmio_base = drm_get_resource_start(rdev->ddev, 2);
684 rdev->rmmio_size = drm_get_resource_len(rdev->ddev, 2);
685 rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size);
686 if (rdev->rmmio == NULL) {
687 return -ENOMEM;
688 }
689 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base);
690 DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size);
691
Dave Airlie28d52042009-09-21 14:33:58 +1000692 /* if we have > 1 VGA cards, then disable the radeon VGA resources */
Dave Airlie93239ea2009-10-28 11:09:58 +1000693 /* this will fail for cards that aren't VGA class devices, just
694 * ignore it */
695 vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode);
Dave Airlie28d52042009-09-21 14:33:58 +1000696
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000697 r = radeon_init(rdev);
Jerome Glisseb574f252009-10-06 19:04:29 +0200698 if (r)
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000699 return r;
Michel Dänzerb1e3a6d2009-06-23 16:12:54 +0200700
Jerome Glisseb574f252009-10-06 19:04:29 +0200701 if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) {
702 /* Acceleration not working on AGP card try again
703 * with fallback to PCI or PCIE GART
704 */
Jerome Glisse1a029b72009-10-06 19:04:30 +0200705 radeon_gpu_reset(rdev);
Jerome Glisseb574f252009-10-06 19:04:29 +0200706 radeon_fini(rdev);
707 radeon_agp_disable(rdev);
708 r = radeon_init(rdev);
Jerome Glisse4aac0472009-09-14 18:29:49 +0200709 if (r)
710 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200711 }
Michel Dänzerecc0b322009-07-21 11:23:57 +0200712 if (radeon_testing) {
713 radeon_test_moves(rdev);
714 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200715 if (radeon_benchmarking) {
716 radeon_benchmark(rdev);
717 }
Jerome Glisse6cf8a3f2009-09-10 21:46:48 +0200718 return 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200719}
720
721void radeon_device_fini(struct radeon_device *rdev)
722{
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200723 DRM_INFO("radeon: finishing device.\n");
724 rdev->shutdown = true;
Jerome Glisse62a8ea32009-10-01 18:02:11 +0200725 radeon_fini(rdev);
Alex Deucherd4877cf2009-12-04 16:56:37 -0500726 destroy_workqueue(rdev->wq);
Dave Airliec1176d62009-10-08 14:03:05 +1000727 vga_client_register(rdev->pdev, NULL, NULL, NULL);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200728 iounmap(rdev->rmmio);
729 rdev->rmmio = NULL;
730}
731
732
733/*
734 * Suspend & resume.
735 */
736int radeon_suspend_kms(struct drm_device *dev, pm_message_t state)
737{
Darren Jenkins875c1862009-12-30 12:18:30 +1100738 struct radeon_device *rdev;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200739 struct drm_crtc *crtc;
Jerome Glisse4c788672009-11-20 14:29:23 +0100740 int r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200741
Darren Jenkins875c1862009-12-30 12:18:30 +1100742 if (dev == NULL || dev->dev_private == NULL) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200743 return -ENODEV;
744 }
745 if (state.event == PM_EVENT_PRETHAW) {
746 return 0;
747 }
Darren Jenkins875c1862009-12-30 12:18:30 +1100748 rdev = dev->dev_private;
749
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200750 /* unpin the front buffers */
751 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
752 struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->fb);
Jerome Glisse4c788672009-11-20 14:29:23 +0100753 struct radeon_bo *robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200754
755 if (rfb == NULL || rfb->obj == NULL) {
756 continue;
757 }
758 robj = rfb->obj->driver_private;
Jerome Glisse4c788672009-11-20 14:29:23 +0100759 if (robj != rdev->fbdev_rbo) {
760 r = radeon_bo_reserve(robj, false);
761 if (unlikely(r == 0)) {
762 radeon_bo_unpin(robj);
763 radeon_bo_unreserve(robj);
764 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200765 }
766 }
767 /* evict vram memory */
Jerome Glisse4c788672009-11-20 14:29:23 +0100768 radeon_bo_evict_vram(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200769 /* wait for gpu to finish processing current batch */
770 radeon_fence_wait_last(rdev);
771
Yang Zhaof657c2a2009-09-15 12:21:01 +1000772 radeon_save_bios_scratch_regs(rdev);
773
Jerome Glisse62a8ea32009-10-01 18:02:11 +0200774 radeon_suspend(rdev);
Alex Deucherd4877cf2009-12-04 16:56:37 -0500775 radeon_hpd_fini(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200776 /* evict remaining vram memory */
Jerome Glisse4c788672009-11-20 14:29:23 +0100777 radeon_bo_evict_vram(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200778
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200779 pci_save_state(dev->pdev);
780 if (state.event == PM_EVENT_SUSPEND) {
781 /* Shut down the device */
782 pci_disable_device(dev->pdev);
783 pci_set_power_state(dev->pdev, PCI_D3hot);
784 }
785 acquire_console_sem();
786 fb_set_suspend(rdev->fbdev_info, 1);
787 release_console_sem();
788 return 0;
789}
790
791int radeon_resume_kms(struct drm_device *dev)
792{
793 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200794
795 acquire_console_sem();
796 pci_set_power_state(dev->pdev, PCI_D0);
797 pci_restore_state(dev->pdev);
798 if (pci_enable_device(dev->pdev)) {
799 release_console_sem();
800 return -1;
801 }
802 pci_set_master(dev->pdev);
Dave Airlie0ebf1712009-11-05 15:39:10 +1000803 /* resume AGP if in use */
804 radeon_agp_resume(rdev);
Jerome Glisse62a8ea32009-10-01 18:02:11 +0200805 radeon_resume(rdev);
Yang Zhaof657c2a2009-09-15 12:21:01 +1000806 radeon_restore_bios_scratch_regs(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200807 fb_set_suspend(rdev->fbdev_info, 0);
808 release_console_sem();
809
Alex Deucherd4877cf2009-12-04 16:56:37 -0500810 /* reset hpd state */
811 radeon_hpd_init(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200812 /* blat the mode back in */
813 drm_helper_resume_force_mode(dev);
814 return 0;
815}
816
817
818/*
819 * Debugfs
820 */
821struct radeon_debugfs {
822 struct drm_info_list *files;
823 unsigned num_files;
824};
825static struct radeon_debugfs _radeon_debugfs[RADEON_DEBUGFS_MAX_NUM_FILES];
826static unsigned _radeon_debugfs_count = 0;
827
828int radeon_debugfs_add_files(struct radeon_device *rdev,
829 struct drm_info_list *files,
830 unsigned nfiles)
831{
832 unsigned i;
833
834 for (i = 0; i < _radeon_debugfs_count; i++) {
835 if (_radeon_debugfs[i].files == files) {
836 /* Already registered */
837 return 0;
838 }
839 }
840 if ((_radeon_debugfs_count + nfiles) > RADEON_DEBUGFS_MAX_NUM_FILES) {
841 DRM_ERROR("Reached maximum number of debugfs files.\n");
842 DRM_ERROR("Report so we increase RADEON_DEBUGFS_MAX_NUM_FILES.\n");
843 return -EINVAL;
844 }
845 _radeon_debugfs[_radeon_debugfs_count].files = files;
846 _radeon_debugfs[_radeon_debugfs_count].num_files = nfiles;
847 _radeon_debugfs_count++;
848#if defined(CONFIG_DEBUG_FS)
849 drm_debugfs_create_files(files, nfiles,
850 rdev->ddev->control->debugfs_root,
851 rdev->ddev->control);
852 drm_debugfs_create_files(files, nfiles,
853 rdev->ddev->primary->debugfs_root,
854 rdev->ddev->primary);
855#endif
856 return 0;
857}
858
859#if defined(CONFIG_DEBUG_FS)
860int radeon_debugfs_init(struct drm_minor *minor)
861{
862 return 0;
863}
864
865void radeon_debugfs_cleanup(struct drm_minor *minor)
866{
867 unsigned i;
868
869 for (i = 0; i < _radeon_debugfs_count; i++) {
870 drm_debugfs_remove_files(_radeon_debugfs[i].files,
871 _radeon_debugfs[i].num_files, minor);
872 }
873}
874#endif