]>
Commit | Line | Data |
---|---|---|
771fe6b9 JG |
1 | /* |
2 | * Copyright 2008 Advanced Micro Devices, Inc. | |
3 | * Copyright 2008 Red Hat Inc. | |
4 | * Copyright 2009 Jerome Glisse. | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a | |
7 | * copy of this software and associated documentation files (the "Software"), | |
8 | * to deal in the Software without restriction, including without limitation | |
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
10 | * and/or sell copies of the Software, and to permit persons to whom the | |
11 | * Software is furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
20 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
22 | * OTHER DEALINGS IN THE SOFTWARE. | |
23 | * | |
24 | * Authors: Dave Airlie | |
25 | * Alex Deucher | |
26 | * Jerome Glisse | |
27 | */ | |
28 | #include <linux/console.h> | |
29 | #include <drm/drmP.h> | |
30 | #include <drm/drm_crtc_helper.h> | |
31 | #include <drm/radeon_drm.h> | |
28d52043 | 32 | #include <linux/vgaarb.h> |
6a9ee8af | 33 | #include <linux/vga_switcheroo.h> |
771fe6b9 JG |
34 | #include "radeon_reg.h" |
35 | #include "radeon.h" | |
771fe6b9 JG |
36 | #include "atom.h" |
37 | ||
b1e3a6d1 MD |
38 | /* |
39 | * Clear GPU surface registers. | |
40 | */ | |
3ce0a23d | 41 | void radeon_surface_init(struct radeon_device *rdev) |
b1e3a6d1 MD |
42 | { |
43 | /* FIXME: check this out */ | |
44 | if (rdev->family < CHIP_R600) { | |
45 | int i; | |
46 | ||
550e2d92 DA |
47 | 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); | |
b1e3a6d1 | 52 | } |
e024e110 DA |
53 | /* enable surfaces */ |
54 | WREG32(RADEON_SURFACE_CNTL, 0); | |
b1e3a6d1 MD |
55 | } |
56 | } | |
57 | ||
771fe6b9 JG |
58 | /* |
59 | * GPU scratch registers helpers function. | |
60 | */ | |
3ce0a23d | 61 | void radeon_scratch_init(struct radeon_device *rdev) |
771fe6b9 JG |
62 | { |
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 | ||
77 | int 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 | ||
91 | void 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 | ||
d594e46a JG |
103 | /** |
104 | * radeon_vram_location - try to find VRAM location | |
105 | * @rdev: radeon device structure holding all necessary informations | |
106 | * @mc: memory controller structure holding memory informations | |
107 | * @base: base address at which to put VRAM | |
108 | * | |
109 | * Function will place try to place VRAM at base address provided | |
110 | * as parameter (which is so far either PCI aperture address or | |
111 | * for IGP TOM base address). | |
112 | * | |
113 | * If there is not enough space to fit the unvisible VRAM in the 32bits | |
114 | * address space then we limit the VRAM size to the aperture. | |
115 | * | |
116 | * If we are using AGP and if the AGP aperture doesn't allow us to have | |
117 | * room for all the VRAM than we restrict the VRAM to the PCI aperture | |
118 | * size and print a warning. | |
119 | * | |
120 | * This function will never fails, worst case are limiting VRAM. | |
121 | * | |
122 | * Note: GTT start, end, size should be initialized before calling this | |
123 | * function on AGP platform. | |
124 | * | |
125 | * Note: We don't explictly enforce VRAM start to be aligned on VRAM size, | |
126 | * this shouldn't be a problem as we are using the PCI aperture as a reference. | |
127 | * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but | |
128 | * not IGP. | |
129 | * | |
130 | * Note: we use mc_vram_size as on some board we need to program the mc to | |
131 | * cover the whole aperture even if VRAM size is inferior to aperture size | |
132 | * Novell bug 204882 + along with lots of ubuntu ones | |
133 | * | |
134 | * Note: when limiting vram it's safe to overwritte real_vram_size because | |
135 | * we are not in case where real_vram_size is inferior to mc_vram_size (ie | |
136 | * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu | |
137 | * ones) | |
138 | * | |
139 | * Note: IGP TOM addr should be the same as the aperture addr, we don't | |
140 | * explicitly check for that thought. | |
141 | * | |
142 | * FIXME: when reducing VRAM size align new size on power of 2. | |
771fe6b9 | 143 | */ |
d594e46a | 144 | void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base) |
771fe6b9 | 145 | { |
d594e46a JG |
146 | mc->vram_start = base; |
147 | if (mc->mc_vram_size > (0xFFFFFFFF - base + 1)) { | |
148 | dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n"); | |
149 | mc->real_vram_size = mc->aper_size; | |
150 | mc->mc_vram_size = mc->aper_size; | |
151 | } | |
152 | mc->vram_end = mc->vram_start + mc->mc_vram_size - 1; | |
153 | if (rdev->flags & RADEON_IS_AGP && mc->vram_end > mc->gtt_start && mc->vram_end <= mc->gtt_end) { | |
154 | dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n"); | |
155 | mc->real_vram_size = mc->aper_size; | |
156 | mc->mc_vram_size = mc->aper_size; | |
157 | } | |
158 | mc->vram_end = mc->vram_start + mc->mc_vram_size - 1; | |
159 | dev_info(rdev->dev, "VRAM: %lluM 0x%08llX - 0x%08llX (%lluM used)\n", | |
160 | mc->mc_vram_size >> 20, mc->vram_start, | |
161 | mc->vram_end, mc->real_vram_size >> 20); | |
162 | } | |
771fe6b9 | 163 | |
d594e46a JG |
164 | /** |
165 | * radeon_gtt_location - try to find GTT location | |
166 | * @rdev: radeon device structure holding all necessary informations | |
167 | * @mc: memory controller structure holding memory informations | |
168 | * | |
169 | * Function will place try to place GTT before or after VRAM. | |
170 | * | |
171 | * If GTT size is bigger than space left then we ajust GTT size. | |
172 | * Thus function will never fails. | |
173 | * | |
174 | * FIXME: when reducing GTT size align new size on power of 2. | |
175 | */ | |
176 | void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc) | |
177 | { | |
178 | u64 size_af, size_bf; | |
179 | ||
180 | size_af = 0xFFFFFFFF - mc->vram_end; | |
181 | size_bf = mc->vram_start; | |
182 | if (size_bf > size_af) { | |
183 | if (mc->gtt_size > size_bf) { | |
184 | dev_warn(rdev->dev, "limiting GTT\n"); | |
185 | mc->gtt_size = size_bf; | |
771fe6b9 | 186 | } |
d594e46a | 187 | mc->gtt_start = mc->vram_start - mc->gtt_size; |
771fe6b9 | 188 | } else { |
d594e46a JG |
189 | if (mc->gtt_size > size_af) { |
190 | dev_warn(rdev->dev, "limiting GTT\n"); | |
191 | mc->gtt_size = size_af; | |
192 | } | |
193 | mc->gtt_start = mc->vram_end + 1; | |
771fe6b9 | 194 | } |
d594e46a JG |
195 | mc->gtt_end = mc->gtt_start + mc->gtt_size - 1; |
196 | dev_info(rdev->dev, "GTT: %lluM 0x%08llX - 0x%08llX\n", | |
197 | mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end); | |
771fe6b9 JG |
198 | } |
199 | ||
771fe6b9 JG |
200 | /* |
201 | * GPU helpers function. | |
202 | */ | |
9f022ddf | 203 | bool radeon_card_posted(struct radeon_device *rdev) |
771fe6b9 JG |
204 | { |
205 | uint32_t reg; | |
206 | ||
207 | /* first check CRTCs */ | |
bcc1c2a1 AD |
208 | if (ASIC_IS_DCE4(rdev)) { |
209 | reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) | | |
210 | RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET) | | |
211 | RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) | | |
212 | RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET) | | |
213 | RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) | | |
214 | RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET); | |
215 | if (reg & EVERGREEN_CRTC_MASTER_EN) | |
216 | return true; | |
217 | } else if (ASIC_IS_AVIVO(rdev)) { | |
771fe6b9 JG |
218 | reg = RREG32(AVIVO_D1CRTC_CONTROL) | |
219 | RREG32(AVIVO_D2CRTC_CONTROL); | |
220 | if (reg & AVIVO_CRTC_EN) { | |
221 | return true; | |
222 | } | |
223 | } else { | |
224 | reg = RREG32(RADEON_CRTC_GEN_CNTL) | | |
225 | RREG32(RADEON_CRTC2_GEN_CNTL); | |
226 | if (reg & RADEON_CRTC_EN) { | |
227 | return true; | |
228 | } | |
229 | } | |
230 | ||
231 | /* then check MEM_SIZE, in case the crtcs are off */ | |
232 | if (rdev->family >= CHIP_R600) | |
233 | reg = RREG32(R600_CONFIG_MEMSIZE); | |
234 | else | |
235 | reg = RREG32(RADEON_CONFIG_MEMSIZE); | |
236 | ||
237 | if (reg) | |
238 | return true; | |
239 | ||
240 | return false; | |
241 | ||
242 | } | |
243 | ||
72542d77 DA |
244 | bool radeon_boot_test_post_card(struct radeon_device *rdev) |
245 | { | |
246 | if (radeon_card_posted(rdev)) | |
247 | return true; | |
248 | ||
249 | if (rdev->bios) { | |
250 | DRM_INFO("GPU not posted. posting now...\n"); | |
251 | if (rdev->is_atom_bios) | |
252 | atom_asic_init(rdev->mode_info.atom_context); | |
253 | else | |
254 | radeon_combios_asic_init(rdev->ddev); | |
255 | return true; | |
256 | } else { | |
257 | dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n"); | |
258 | return false; | |
259 | } | |
260 | } | |
261 | ||
3ce0a23d JG |
262 | int radeon_dummy_page_init(struct radeon_device *rdev) |
263 | { | |
82568565 DA |
264 | if (rdev->dummy_page.page) |
265 | return 0; | |
3ce0a23d JG |
266 | rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO); |
267 | if (rdev->dummy_page.page == NULL) | |
268 | return -ENOMEM; | |
269 | rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page, | |
270 | 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); | |
271 | if (!rdev->dummy_page.addr) { | |
272 | __free_page(rdev->dummy_page.page); | |
273 | rdev->dummy_page.page = NULL; | |
274 | return -ENOMEM; | |
275 | } | |
276 | return 0; | |
277 | } | |
278 | ||
279 | void radeon_dummy_page_fini(struct radeon_device *rdev) | |
280 | { | |
281 | if (rdev->dummy_page.page == NULL) | |
282 | return; | |
283 | pci_unmap_page(rdev->pdev, rdev->dummy_page.addr, | |
284 | PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); | |
285 | __free_page(rdev->dummy_page.page); | |
286 | rdev->dummy_page.page = NULL; | |
287 | } | |
288 | ||
771fe6b9 | 289 | |
771fe6b9 JG |
290 | /* ATOM accessor methods */ |
291 | static uint32_t cail_pll_read(struct card_info *info, uint32_t reg) | |
292 | { | |
293 | struct radeon_device *rdev = info->dev->dev_private; | |
294 | uint32_t r; | |
295 | ||
296 | r = rdev->pll_rreg(rdev, reg); | |
297 | return r; | |
298 | } | |
299 | ||
300 | static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val) | |
301 | { | |
302 | struct radeon_device *rdev = info->dev->dev_private; | |
303 | ||
304 | rdev->pll_wreg(rdev, reg, val); | |
305 | } | |
306 | ||
307 | static uint32_t cail_mc_read(struct card_info *info, uint32_t reg) | |
308 | { | |
309 | struct radeon_device *rdev = info->dev->dev_private; | |
310 | uint32_t r; | |
311 | ||
312 | r = rdev->mc_rreg(rdev, reg); | |
313 | return r; | |
314 | } | |
315 | ||
316 | static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val) | |
317 | { | |
318 | struct radeon_device *rdev = info->dev->dev_private; | |
319 | ||
320 | rdev->mc_wreg(rdev, reg, val); | |
321 | } | |
322 | ||
323 | static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val) | |
324 | { | |
325 | struct radeon_device *rdev = info->dev->dev_private; | |
326 | ||
327 | WREG32(reg*4, val); | |
328 | } | |
329 | ||
330 | static uint32_t cail_reg_read(struct card_info *info, uint32_t reg) | |
331 | { | |
332 | struct radeon_device *rdev = info->dev->dev_private; | |
333 | uint32_t r; | |
334 | ||
335 | r = RREG32(reg*4); | |
336 | return r; | |
337 | } | |
338 | ||
771fe6b9 JG |
339 | int radeon_atombios_init(struct radeon_device *rdev) |
340 | { | |
61c4b24b MF |
341 | struct card_info *atom_card_info = |
342 | kzalloc(sizeof(struct card_info), GFP_KERNEL); | |
343 | ||
344 | if (!atom_card_info) | |
345 | return -ENOMEM; | |
346 | ||
347 | rdev->mode_info.atom_card_info = atom_card_info; | |
348 | atom_card_info->dev = rdev->ddev; | |
349 | atom_card_info->reg_read = cail_reg_read; | |
350 | atom_card_info->reg_write = cail_reg_write; | |
351 | atom_card_info->mc_read = cail_mc_read; | |
352 | atom_card_info->mc_write = cail_mc_write; | |
353 | atom_card_info->pll_read = cail_pll_read; | |
354 | atom_card_info->pll_write = cail_pll_write; | |
355 | ||
356 | rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios); | |
c31ad97f | 357 | mutex_init(&rdev->mode_info.atom_context->mutex); |
771fe6b9 | 358 | radeon_atom_initialize_bios_scratch_regs(rdev->ddev); |
d904ef9b | 359 | atom_allocate_fb_scratch(rdev->mode_info.atom_context); |
771fe6b9 JG |
360 | return 0; |
361 | } | |
362 | ||
363 | void radeon_atombios_fini(struct radeon_device *rdev) | |
364 | { | |
4a04a844 JG |
365 | if (rdev->mode_info.atom_context) { |
366 | kfree(rdev->mode_info.atom_context->scratch); | |
367 | kfree(rdev->mode_info.atom_context); | |
368 | } | |
61c4b24b | 369 | kfree(rdev->mode_info.atom_card_info); |
771fe6b9 JG |
370 | } |
371 | ||
372 | int radeon_combios_init(struct radeon_device *rdev) | |
373 | { | |
374 | radeon_combios_initialize_bios_scratch_regs(rdev->ddev); | |
375 | return 0; | |
376 | } | |
377 | ||
378 | void radeon_combios_fini(struct radeon_device *rdev) | |
379 | { | |
380 | } | |
381 | ||
28d52043 DA |
382 | /* if we get transitioned to only one device, tak VGA back */ |
383 | static unsigned int radeon_vga_set_decode(void *cookie, bool state) | |
384 | { | |
385 | struct radeon_device *rdev = cookie; | |
28d52043 DA |
386 | radeon_vga_set_state(rdev, state); |
387 | if (state) | |
388 | return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM | | |
389 | VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; | |
390 | else | |
391 | return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; | |
392 | } | |
c1176d6f | 393 | |
36421338 JG |
394 | void radeon_check_arguments(struct radeon_device *rdev) |
395 | { | |
396 | /* vramlimit must be a power of two */ | |
397 | switch (radeon_vram_limit) { | |
398 | case 0: | |
399 | case 4: | |
400 | case 8: | |
401 | case 16: | |
402 | case 32: | |
403 | case 64: | |
404 | case 128: | |
405 | case 256: | |
406 | case 512: | |
407 | case 1024: | |
408 | case 2048: | |
409 | case 4096: | |
410 | break; | |
411 | default: | |
412 | dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n", | |
413 | radeon_vram_limit); | |
414 | radeon_vram_limit = 0; | |
415 | break; | |
416 | } | |
417 | radeon_vram_limit = radeon_vram_limit << 20; | |
418 | /* gtt size must be power of two and greater or equal to 32M */ | |
419 | switch (radeon_gart_size) { | |
420 | case 4: | |
421 | case 8: | |
422 | case 16: | |
423 | dev_warn(rdev->dev, "gart size (%d) too small forcing to 512M\n", | |
424 | radeon_gart_size); | |
425 | radeon_gart_size = 512; | |
426 | break; | |
427 | case 32: | |
428 | case 64: | |
429 | case 128: | |
430 | case 256: | |
431 | case 512: | |
432 | case 1024: | |
433 | case 2048: | |
434 | case 4096: | |
435 | break; | |
436 | default: | |
437 | dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n", | |
438 | radeon_gart_size); | |
439 | radeon_gart_size = 512; | |
440 | break; | |
441 | } | |
442 | rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024; | |
443 | /* AGP mode can only be -1, 1, 2, 4, 8 */ | |
444 | switch (radeon_agpmode) { | |
445 | case -1: | |
446 | case 0: | |
447 | case 1: | |
448 | case 2: | |
449 | case 4: | |
450 | case 8: | |
451 | break; | |
452 | default: | |
453 | dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: " | |
454 | "-1, 0, 1, 2, 4, 8)\n", radeon_agpmode); | |
455 | radeon_agpmode = 0; | |
456 | break; | |
457 | } | |
458 | } | |
459 | ||
6a9ee8af DA |
460 | static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state) |
461 | { | |
462 | struct drm_device *dev = pci_get_drvdata(pdev); | |
463 | struct radeon_device *rdev = dev->dev_private; | |
464 | pm_message_t pmm = { .event = PM_EVENT_SUSPEND }; | |
465 | if (state == VGA_SWITCHEROO_ON) { | |
466 | printk(KERN_INFO "radeon: switched on\n"); | |
467 | /* don't suspend or resume card normally */ | |
468 | rdev->powered_down = false; | |
469 | radeon_resume_kms(dev); | |
470 | } else { | |
471 | printk(KERN_INFO "radeon: switched off\n"); | |
472 | radeon_suspend_kms(dev, pmm); | |
473 | /* don't suspend or resume card normally */ | |
474 | rdev->powered_down = true; | |
475 | } | |
476 | } | |
477 | ||
478 | static bool radeon_switcheroo_can_switch(struct pci_dev *pdev) | |
479 | { | |
480 | struct drm_device *dev = pci_get_drvdata(pdev); | |
481 | bool can_switch; | |
482 | ||
483 | spin_lock(&dev->count_lock); | |
484 | can_switch = (dev->open_count == 0); | |
485 | spin_unlock(&dev->count_lock); | |
486 | return can_switch; | |
487 | } | |
488 | ||
489 | ||
771fe6b9 JG |
490 | int radeon_device_init(struct radeon_device *rdev, |
491 | struct drm_device *ddev, | |
492 | struct pci_dev *pdev, | |
493 | uint32_t flags) | |
494 | { | |
6cf8a3f5 | 495 | int r; |
ad49f501 | 496 | int dma_bits; |
771fe6b9 JG |
497 | |
498 | DRM_INFO("radeon: Initializing kernel modesetting.\n"); | |
499 | rdev->shutdown = false; | |
9f022ddf | 500 | rdev->dev = &pdev->dev; |
771fe6b9 JG |
501 | rdev->ddev = ddev; |
502 | rdev->pdev = pdev; | |
503 | rdev->flags = flags; | |
504 | rdev->family = flags & RADEON_FAMILY_MASK; | |
505 | rdev->is_atom_bios = false; | |
506 | rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT; | |
507 | rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024; | |
508 | rdev->gpu_lockup = false; | |
733289c2 | 509 | rdev->accel_working = false; |
771fe6b9 JG |
510 | /* mutex initialization are all done here so we |
511 | * can recall function without having locking issues */ | |
512 | mutex_init(&rdev->cs_mutex); | |
513 | mutex_init(&rdev->ib_pool.mutex); | |
514 | mutex_init(&rdev->cp.mutex); | |
40bacf16 | 515 | mutex_init(&rdev->dc_hw_i2c_mutex); |
d8f60cfc AD |
516 | if (rdev->family >= CHIP_R600) |
517 | spin_lock_init(&rdev->ih.lock); | |
4c788679 | 518 | mutex_init(&rdev->gem.mutex); |
c913e23a | 519 | mutex_init(&rdev->pm.mutex); |
771fe6b9 | 520 | rwlock_init(&rdev->fence_drv.lock); |
9f022ddf | 521 | INIT_LIST_HEAD(&rdev->gem.objects); |
73a6d3fc | 522 | init_waitqueue_head(&rdev->irq.vblank_queue); |
771fe6b9 | 523 | |
d4877cf2 AD |
524 | /* setup workqueue */ |
525 | rdev->wq = create_workqueue("radeon"); | |
526 | if (rdev->wq == NULL) | |
527 | return -ENOMEM; | |
528 | ||
4aac0473 JG |
529 | /* Set asic functions */ |
530 | r = radeon_asic_init(rdev); | |
36421338 | 531 | if (r) |
4aac0473 | 532 | return r; |
36421338 | 533 | radeon_check_arguments(rdev); |
4aac0473 | 534 | |
30256a3f | 535 | if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) { |
b574f251 | 536 | radeon_agp_disable(rdev); |
771fe6b9 JG |
537 | } |
538 | ||
ad49f501 DA |
539 | /* set DMA mask + need_dma32 flags. |
540 | * PCIE - can handle 40-bits. | |
541 | * IGP - can handle 40-bits (in theory) | |
542 | * AGP - generally dma32 is safest | |
543 | * PCI - only dma32 | |
544 | */ | |
545 | rdev->need_dma32 = false; | |
546 | if (rdev->flags & RADEON_IS_AGP) | |
547 | rdev->need_dma32 = true; | |
548 | if (rdev->flags & RADEON_IS_PCI) | |
549 | rdev->need_dma32 = true; | |
550 | ||
551 | dma_bits = rdev->need_dma32 ? 32 : 40; | |
552 | r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits)); | |
771fe6b9 JG |
553 | if (r) { |
554 | printk(KERN_WARNING "radeon: No suitable DMA available.\n"); | |
555 | } | |
556 | ||
557 | /* Registers mapping */ | |
558 | /* TODO: block userspace mapping of io register */ | |
559 | rdev->rmmio_base = drm_get_resource_start(rdev->ddev, 2); | |
560 | rdev->rmmio_size = drm_get_resource_len(rdev->ddev, 2); | |
561 | rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size); | |
562 | if (rdev->rmmio == NULL) { | |
563 | return -ENOMEM; | |
564 | } | |
565 | DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base); | |
566 | DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size); | |
567 | ||
28d52043 | 568 | /* if we have > 1 VGA cards, then disable the radeon VGA resources */ |
93239ea1 DA |
569 | /* this will fail for cards that aren't VGA class devices, just |
570 | * ignore it */ | |
571 | vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode); | |
6a9ee8af DA |
572 | vga_switcheroo_register_client(rdev->pdev, |
573 | radeon_switcheroo_set_state, | |
574 | radeon_switcheroo_can_switch); | |
28d52043 | 575 | |
3ce0a23d | 576 | r = radeon_init(rdev); |
b574f251 | 577 | if (r) |
3ce0a23d | 578 | return r; |
3ce0a23d | 579 | |
b574f251 JG |
580 | if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) { |
581 | /* Acceleration not working on AGP card try again | |
582 | * with fallback to PCI or PCIE GART | |
583 | */ | |
1a029b76 | 584 | radeon_gpu_reset(rdev); |
b574f251 JG |
585 | radeon_fini(rdev); |
586 | radeon_agp_disable(rdev); | |
587 | r = radeon_init(rdev); | |
4aac0473 JG |
588 | if (r) |
589 | return r; | |
771fe6b9 | 590 | } |
ecc0b326 MD |
591 | if (radeon_testing) { |
592 | radeon_test_moves(rdev); | |
593 | } | |
771fe6b9 JG |
594 | if (radeon_benchmarking) { |
595 | radeon_benchmark(rdev); | |
596 | } | |
6cf8a3f5 | 597 | return 0; |
771fe6b9 JG |
598 | } |
599 | ||
600 | void radeon_device_fini(struct radeon_device *rdev) | |
601 | { | |
771fe6b9 JG |
602 | DRM_INFO("radeon: finishing device.\n"); |
603 | rdev->shutdown = true; | |
62a8ea3f | 604 | radeon_fini(rdev); |
d4877cf2 | 605 | destroy_workqueue(rdev->wq); |
6a9ee8af | 606 | vga_switcheroo_unregister_client(rdev->pdev); |
c1176d6f | 607 | vga_client_register(rdev->pdev, NULL, NULL, NULL); |
771fe6b9 JG |
608 | iounmap(rdev->rmmio); |
609 | rdev->rmmio = NULL; | |
610 | } | |
611 | ||
612 | ||
613 | /* | |
614 | * Suspend & resume. | |
615 | */ | |
616 | int radeon_suspend_kms(struct drm_device *dev, pm_message_t state) | |
617 | { | |
875c1866 | 618 | struct radeon_device *rdev; |
771fe6b9 | 619 | struct drm_crtc *crtc; |
4c788679 | 620 | int r; |
771fe6b9 | 621 | |
875c1866 | 622 | if (dev == NULL || dev->dev_private == NULL) { |
771fe6b9 JG |
623 | return -ENODEV; |
624 | } | |
625 | if (state.event == PM_EVENT_PRETHAW) { | |
626 | return 0; | |
627 | } | |
875c1866 DJ |
628 | rdev = dev->dev_private; |
629 | ||
6a9ee8af DA |
630 | if (rdev->powered_down) |
631 | return 0; | |
771fe6b9 JG |
632 | /* unpin the front buffers */ |
633 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { | |
634 | struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->fb); | |
4c788679 | 635 | struct radeon_bo *robj; |
771fe6b9 JG |
636 | |
637 | if (rfb == NULL || rfb->obj == NULL) { | |
638 | continue; | |
639 | } | |
640 | robj = rfb->obj->driver_private; | |
4c788679 JG |
641 | if (robj != rdev->fbdev_rbo) { |
642 | r = radeon_bo_reserve(robj, false); | |
643 | if (unlikely(r == 0)) { | |
644 | radeon_bo_unpin(robj); | |
645 | radeon_bo_unreserve(robj); | |
646 | } | |
771fe6b9 JG |
647 | } |
648 | } | |
649 | /* evict vram memory */ | |
4c788679 | 650 | radeon_bo_evict_vram(rdev); |
771fe6b9 JG |
651 | /* wait for gpu to finish processing current batch */ |
652 | radeon_fence_wait_last(rdev); | |
653 | ||
f657c2a7 YZ |
654 | radeon_save_bios_scratch_regs(rdev); |
655 | ||
62a8ea3f | 656 | radeon_suspend(rdev); |
d4877cf2 | 657 | radeon_hpd_fini(rdev); |
771fe6b9 | 658 | /* evict remaining vram memory */ |
4c788679 | 659 | radeon_bo_evict_vram(rdev); |
771fe6b9 | 660 | |
771fe6b9 JG |
661 | pci_save_state(dev->pdev); |
662 | if (state.event == PM_EVENT_SUSPEND) { | |
663 | /* Shut down the device */ | |
664 | pci_disable_device(dev->pdev); | |
665 | pci_set_power_state(dev->pdev, PCI_D3hot); | |
666 | } | |
667 | acquire_console_sem(); | |
668 | fb_set_suspend(rdev->fbdev_info, 1); | |
669 | release_console_sem(); | |
670 | return 0; | |
671 | } | |
672 | ||
673 | int radeon_resume_kms(struct drm_device *dev) | |
674 | { | |
675 | struct radeon_device *rdev = dev->dev_private; | |
771fe6b9 | 676 | |
6a9ee8af DA |
677 | if (rdev->powered_down) |
678 | return 0; | |
679 | ||
771fe6b9 JG |
680 | acquire_console_sem(); |
681 | pci_set_power_state(dev->pdev, PCI_D0); | |
682 | pci_restore_state(dev->pdev); | |
683 | if (pci_enable_device(dev->pdev)) { | |
684 | release_console_sem(); | |
685 | return -1; | |
686 | } | |
687 | pci_set_master(dev->pdev); | |
0ebf1717 DA |
688 | /* resume AGP if in use */ |
689 | radeon_agp_resume(rdev); | |
62a8ea3f | 690 | radeon_resume(rdev); |
f657c2a7 | 691 | radeon_restore_bios_scratch_regs(rdev); |
771fe6b9 JG |
692 | fb_set_suspend(rdev->fbdev_info, 0); |
693 | release_console_sem(); | |
694 | ||
d4877cf2 AD |
695 | /* reset hpd state */ |
696 | radeon_hpd_init(rdev); | |
771fe6b9 JG |
697 | /* blat the mode back in */ |
698 | drm_helper_resume_force_mode(dev); | |
699 | return 0; | |
700 | } | |
701 | ||
702 | ||
703 | /* | |
704 | * Debugfs | |
705 | */ | |
706 | struct radeon_debugfs { | |
707 | struct drm_info_list *files; | |
708 | unsigned num_files; | |
709 | }; | |
710 | static struct radeon_debugfs _radeon_debugfs[RADEON_DEBUGFS_MAX_NUM_FILES]; | |
711 | static unsigned _radeon_debugfs_count = 0; | |
712 | ||
713 | int radeon_debugfs_add_files(struct radeon_device *rdev, | |
714 | struct drm_info_list *files, | |
715 | unsigned nfiles) | |
716 | { | |
717 | unsigned i; | |
718 | ||
719 | for (i = 0; i < _radeon_debugfs_count; i++) { | |
720 | if (_radeon_debugfs[i].files == files) { | |
721 | /* Already registered */ | |
722 | return 0; | |
723 | } | |
724 | } | |
725 | if ((_radeon_debugfs_count + nfiles) > RADEON_DEBUGFS_MAX_NUM_FILES) { | |
726 | DRM_ERROR("Reached maximum number of debugfs files.\n"); | |
727 | DRM_ERROR("Report so we increase RADEON_DEBUGFS_MAX_NUM_FILES.\n"); | |
728 | return -EINVAL; | |
729 | } | |
730 | _radeon_debugfs[_radeon_debugfs_count].files = files; | |
731 | _radeon_debugfs[_radeon_debugfs_count].num_files = nfiles; | |
732 | _radeon_debugfs_count++; | |
733 | #if defined(CONFIG_DEBUG_FS) | |
734 | drm_debugfs_create_files(files, nfiles, | |
735 | rdev->ddev->control->debugfs_root, | |
736 | rdev->ddev->control); | |
737 | drm_debugfs_create_files(files, nfiles, | |
738 | rdev->ddev->primary->debugfs_root, | |
739 | rdev->ddev->primary); | |
740 | #endif | |
741 | return 0; | |
742 | } | |
743 | ||
744 | #if defined(CONFIG_DEBUG_FS) | |
745 | int radeon_debugfs_init(struct drm_minor *minor) | |
746 | { | |
747 | return 0; | |
748 | } | |
749 | ||
750 | void radeon_debugfs_cleanup(struct drm_minor *minor) | |
751 | { | |
752 | unsigned i; | |
753 | ||
754 | for (i = 0; i < _radeon_debugfs_count; i++) { | |
755 | drm_debugfs_remove_files(_radeon_debugfs[i].files, | |
756 | _radeon_debugfs[i].num_files, minor); | |
757 | } | |
758 | } | |
759 | #endif |