1771fe6b9SJerome Glisse /* 2771fe6b9SJerome Glisse * Copyright 2008 Advanced Micro Devices, Inc. 3771fe6b9SJerome Glisse * Copyright 2008 Red Hat Inc. 4771fe6b9SJerome Glisse * Copyright 2009 Jerome Glisse. 5771fe6b9SJerome Glisse * 6771fe6b9SJerome Glisse * Permission is hereby granted, free of charge, to any person obtaining a 7771fe6b9SJerome Glisse * copy of this software and associated documentation files (the "Software"), 8771fe6b9SJerome Glisse * to deal in the Software without restriction, including without limitation 9771fe6b9SJerome Glisse * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10771fe6b9SJerome Glisse * and/or sell copies of the Software, and to permit persons to whom the 11771fe6b9SJerome Glisse * Software is furnished to do so, subject to the following conditions: 12771fe6b9SJerome Glisse * 13771fe6b9SJerome Glisse * The above copyright notice and this permission notice shall be included in 14771fe6b9SJerome Glisse * all copies or substantial portions of the Software. 15771fe6b9SJerome Glisse * 16771fe6b9SJerome Glisse * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17771fe6b9SJerome Glisse * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18771fe6b9SJerome Glisse * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19771fe6b9SJerome Glisse * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20771fe6b9SJerome Glisse * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21771fe6b9SJerome Glisse * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22771fe6b9SJerome Glisse * OTHER DEALINGS IN THE SOFTWARE. 23771fe6b9SJerome Glisse * 24771fe6b9SJerome Glisse * Authors: Dave Airlie 25771fe6b9SJerome Glisse * Alex Deucher 26771fe6b9SJerome Glisse * Jerome Glisse 27771fe6b9SJerome Glisse */ 28771fe6b9SJerome Glisse #include <linux/console.h> 295a0e3ad6STejun Heo #include <linux/slab.h> 30771fe6b9SJerome Glisse #include <drm/drmP.h> 31771fe6b9SJerome Glisse #include <drm/drm_crtc_helper.h> 32771fe6b9SJerome Glisse #include <drm/radeon_drm.h> 3328d52043SDave Airlie #include <linux/vgaarb.h> 346a9ee8afSDave Airlie #include <linux/vga_switcheroo.h> 35bcc65fd8SMatthew Garrett #include <linux/efi.h> 36771fe6b9SJerome Glisse #include "radeon_reg.h" 37771fe6b9SJerome Glisse #include "radeon.h" 38771fe6b9SJerome Glisse #include "atom.h" 39771fe6b9SJerome Glisse 401b5331d9SJerome Glisse static const char radeon_family_name[][16] = { 411b5331d9SJerome Glisse "R100", 421b5331d9SJerome Glisse "RV100", 431b5331d9SJerome Glisse "RS100", 441b5331d9SJerome Glisse "RV200", 451b5331d9SJerome Glisse "RS200", 461b5331d9SJerome Glisse "R200", 471b5331d9SJerome Glisse "RV250", 481b5331d9SJerome Glisse "RS300", 491b5331d9SJerome Glisse "RV280", 501b5331d9SJerome Glisse "R300", 511b5331d9SJerome Glisse "R350", 521b5331d9SJerome Glisse "RV350", 531b5331d9SJerome Glisse "RV380", 541b5331d9SJerome Glisse "R420", 551b5331d9SJerome Glisse "R423", 561b5331d9SJerome Glisse "RV410", 571b5331d9SJerome Glisse "RS400", 581b5331d9SJerome Glisse "RS480", 591b5331d9SJerome Glisse "RS600", 601b5331d9SJerome Glisse "RS690", 611b5331d9SJerome Glisse "RS740", 621b5331d9SJerome Glisse "RV515", 631b5331d9SJerome Glisse "R520", 641b5331d9SJerome Glisse "RV530", 651b5331d9SJerome Glisse "RV560", 661b5331d9SJerome Glisse "RV570", 671b5331d9SJerome Glisse "R580", 681b5331d9SJerome Glisse "R600", 691b5331d9SJerome Glisse "RV610", 701b5331d9SJerome Glisse "RV630", 711b5331d9SJerome Glisse "RV670", 721b5331d9SJerome Glisse "RV620", 731b5331d9SJerome Glisse "RV635", 741b5331d9SJerome Glisse "RS780", 751b5331d9SJerome Glisse "RS880", 761b5331d9SJerome Glisse "RV770", 771b5331d9SJerome Glisse "RV730", 781b5331d9SJerome Glisse "RV710", 791b5331d9SJerome Glisse "RV740", 801b5331d9SJerome Glisse "CEDAR", 811b5331d9SJerome Glisse "REDWOOD", 821b5331d9SJerome Glisse "JUNIPER", 831b5331d9SJerome Glisse "CYPRESS", 841b5331d9SJerome Glisse "HEMLOCK", 85b08ebe7eSAlex Deucher "PALM", 864df64e65SAlex Deucher "SUMO", 874df64e65SAlex Deucher "SUMO2", 881fe18305SAlex Deucher "BARTS", 891fe18305SAlex Deucher "TURKS", 901fe18305SAlex Deucher "CAICOS", 91b7cfc9feSAlex Deucher "CAYMAN", 928848f759SAlex Deucher "ARUBA", 93cb28bb34SAlex Deucher "TAHITI", 94cb28bb34SAlex Deucher "PITCAIRN", 95cb28bb34SAlex Deucher "VERDE", 961b5331d9SJerome Glisse "LAST", 971b5331d9SJerome Glisse }; 981b5331d9SJerome Glisse 990c195119SAlex Deucher /** 1000c195119SAlex Deucher * radeon_surface_init - Clear GPU surface registers. 1010c195119SAlex Deucher * 1020c195119SAlex Deucher * @rdev: radeon_device pointer 1030c195119SAlex Deucher * 1040c195119SAlex Deucher * Clear GPU surface registers (r1xx-r5xx). 105b1e3a6d1SMichel Dänzer */ 1063ce0a23dSJerome Glisse void radeon_surface_init(struct radeon_device *rdev) 107b1e3a6d1SMichel Dänzer { 108b1e3a6d1SMichel Dänzer /* FIXME: check this out */ 109b1e3a6d1SMichel Dänzer if (rdev->family < CHIP_R600) { 110b1e3a6d1SMichel Dänzer int i; 111b1e3a6d1SMichel Dänzer 112550e2d92SDave Airlie for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) { 113550e2d92SDave Airlie if (rdev->surface_regs[i].bo) 114550e2d92SDave Airlie radeon_bo_get_surface_reg(rdev->surface_regs[i].bo); 115550e2d92SDave Airlie else 116550e2d92SDave Airlie radeon_clear_surface_reg(rdev, i); 117b1e3a6d1SMichel Dänzer } 118e024e110SDave Airlie /* enable surfaces */ 119e024e110SDave Airlie WREG32(RADEON_SURFACE_CNTL, 0); 120b1e3a6d1SMichel Dänzer } 121b1e3a6d1SMichel Dänzer } 122b1e3a6d1SMichel Dänzer 123b1e3a6d1SMichel Dänzer /* 124771fe6b9SJerome Glisse * GPU scratch registers helpers function. 125771fe6b9SJerome Glisse */ 1260c195119SAlex Deucher /** 1270c195119SAlex Deucher * radeon_scratch_init - Init scratch register driver information. 1280c195119SAlex Deucher * 1290c195119SAlex Deucher * @rdev: radeon_device pointer 1300c195119SAlex Deucher * 1310c195119SAlex Deucher * Init CP scratch register driver information (r1xx-r5xx) 1320c195119SAlex Deucher */ 1333ce0a23dSJerome Glisse void radeon_scratch_init(struct radeon_device *rdev) 134771fe6b9SJerome Glisse { 135771fe6b9SJerome Glisse int i; 136771fe6b9SJerome Glisse 137771fe6b9SJerome Glisse /* FIXME: check this out */ 138771fe6b9SJerome Glisse if (rdev->family < CHIP_R300) { 139771fe6b9SJerome Glisse rdev->scratch.num_reg = 5; 140771fe6b9SJerome Glisse } else { 141771fe6b9SJerome Glisse rdev->scratch.num_reg = 7; 142771fe6b9SJerome Glisse } 143724c80e1SAlex Deucher rdev->scratch.reg_base = RADEON_SCRATCH_REG0; 144771fe6b9SJerome Glisse for (i = 0; i < rdev->scratch.num_reg; i++) { 145771fe6b9SJerome Glisse rdev->scratch.free[i] = true; 146724c80e1SAlex Deucher rdev->scratch.reg[i] = rdev->scratch.reg_base + (i * 4); 147771fe6b9SJerome Glisse } 148771fe6b9SJerome Glisse } 149771fe6b9SJerome Glisse 1500c195119SAlex Deucher /** 1510c195119SAlex Deucher * radeon_scratch_get - Allocate a scratch register 1520c195119SAlex Deucher * 1530c195119SAlex Deucher * @rdev: radeon_device pointer 1540c195119SAlex Deucher * @reg: scratch register mmio offset 1550c195119SAlex Deucher * 1560c195119SAlex Deucher * Allocate a CP scratch register for use by the driver (all asics). 1570c195119SAlex Deucher * Returns 0 on success or -EINVAL on failure. 1580c195119SAlex Deucher */ 159771fe6b9SJerome Glisse int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg) 160771fe6b9SJerome Glisse { 161771fe6b9SJerome Glisse int i; 162771fe6b9SJerome Glisse 163771fe6b9SJerome Glisse for (i = 0; i < rdev->scratch.num_reg; i++) { 164771fe6b9SJerome Glisse if (rdev->scratch.free[i]) { 165771fe6b9SJerome Glisse rdev->scratch.free[i] = false; 166771fe6b9SJerome Glisse *reg = rdev->scratch.reg[i]; 167771fe6b9SJerome Glisse return 0; 168771fe6b9SJerome Glisse } 169771fe6b9SJerome Glisse } 170771fe6b9SJerome Glisse return -EINVAL; 171771fe6b9SJerome Glisse } 172771fe6b9SJerome Glisse 1730c195119SAlex Deucher /** 1740c195119SAlex Deucher * radeon_scratch_free - Free a scratch register 1750c195119SAlex Deucher * 1760c195119SAlex Deucher * @rdev: radeon_device pointer 1770c195119SAlex Deucher * @reg: scratch register mmio offset 1780c195119SAlex Deucher * 1790c195119SAlex Deucher * Free a CP scratch register allocated for use by the driver (all asics) 1800c195119SAlex Deucher */ 181771fe6b9SJerome Glisse void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg) 182771fe6b9SJerome Glisse { 183771fe6b9SJerome Glisse int i; 184771fe6b9SJerome Glisse 185771fe6b9SJerome Glisse for (i = 0; i < rdev->scratch.num_reg; i++) { 186771fe6b9SJerome Glisse if (rdev->scratch.reg[i] == reg) { 187771fe6b9SJerome Glisse rdev->scratch.free[i] = true; 188771fe6b9SJerome Glisse return; 189771fe6b9SJerome Glisse } 190771fe6b9SJerome Glisse } 191771fe6b9SJerome Glisse } 192771fe6b9SJerome Glisse 1930c195119SAlex Deucher /* 1940c195119SAlex Deucher * radeon_wb_*() 1950c195119SAlex Deucher * Writeback is the the method by which the the GPU updates special pages 1960c195119SAlex Deucher * in memory with the status of certain GPU events (fences, ring pointers, 1970c195119SAlex Deucher * etc.). 1980c195119SAlex Deucher */ 1990c195119SAlex Deucher 2000c195119SAlex Deucher /** 2010c195119SAlex Deucher * radeon_wb_disable - Disable Writeback 2020c195119SAlex Deucher * 2030c195119SAlex Deucher * @rdev: radeon_device pointer 2040c195119SAlex Deucher * 2050c195119SAlex Deucher * Disables Writeback (all asics). Used for suspend. 2060c195119SAlex Deucher */ 207724c80e1SAlex Deucher void radeon_wb_disable(struct radeon_device *rdev) 208724c80e1SAlex Deucher { 209724c80e1SAlex Deucher int r; 210724c80e1SAlex Deucher 211724c80e1SAlex Deucher if (rdev->wb.wb_obj) { 212724c80e1SAlex Deucher r = radeon_bo_reserve(rdev->wb.wb_obj, false); 213724c80e1SAlex Deucher if (unlikely(r != 0)) 214724c80e1SAlex Deucher return; 215724c80e1SAlex Deucher radeon_bo_kunmap(rdev->wb.wb_obj); 216724c80e1SAlex Deucher radeon_bo_unpin(rdev->wb.wb_obj); 217724c80e1SAlex Deucher radeon_bo_unreserve(rdev->wb.wb_obj); 218724c80e1SAlex Deucher } 219724c80e1SAlex Deucher rdev->wb.enabled = false; 220724c80e1SAlex Deucher } 221724c80e1SAlex Deucher 2220c195119SAlex Deucher /** 2230c195119SAlex Deucher * radeon_wb_fini - Disable Writeback and free memory 2240c195119SAlex Deucher * 2250c195119SAlex Deucher * @rdev: radeon_device pointer 2260c195119SAlex Deucher * 2270c195119SAlex Deucher * Disables Writeback and frees the Writeback memory (all asics). 2280c195119SAlex Deucher * Used at driver shutdown. 2290c195119SAlex Deucher */ 230724c80e1SAlex Deucher void radeon_wb_fini(struct radeon_device *rdev) 231724c80e1SAlex Deucher { 232724c80e1SAlex Deucher radeon_wb_disable(rdev); 233724c80e1SAlex Deucher if (rdev->wb.wb_obj) { 234724c80e1SAlex Deucher radeon_bo_unref(&rdev->wb.wb_obj); 235724c80e1SAlex Deucher rdev->wb.wb = NULL; 236724c80e1SAlex Deucher rdev->wb.wb_obj = NULL; 237724c80e1SAlex Deucher } 238724c80e1SAlex Deucher } 239724c80e1SAlex Deucher 2400c195119SAlex Deucher /** 2410c195119SAlex Deucher * radeon_wb_init- Init Writeback driver info and allocate memory 2420c195119SAlex Deucher * 2430c195119SAlex Deucher * @rdev: radeon_device pointer 2440c195119SAlex Deucher * 2450c195119SAlex Deucher * Disables Writeback and frees the Writeback memory (all asics). 2460c195119SAlex Deucher * Used at driver startup. 2470c195119SAlex Deucher * Returns 0 on success or an -error on failure. 2480c195119SAlex Deucher */ 249724c80e1SAlex Deucher int radeon_wb_init(struct radeon_device *rdev) 250724c80e1SAlex Deucher { 251724c80e1SAlex Deucher int r; 252724c80e1SAlex Deucher 253724c80e1SAlex Deucher if (rdev->wb.wb_obj == NULL) { 254441921d5SDaniel Vetter r = radeon_bo_create(rdev, RADEON_GPU_PAGE_SIZE, PAGE_SIZE, true, 25540f5cf99SAlex Deucher RADEON_GEM_DOMAIN_GTT, NULL, &rdev->wb.wb_obj); 256724c80e1SAlex Deucher if (r) { 257724c80e1SAlex Deucher dev_warn(rdev->dev, "(%d) create WB bo failed\n", r); 258724c80e1SAlex Deucher return r; 259724c80e1SAlex Deucher } 260724c80e1SAlex Deucher } 261724c80e1SAlex Deucher r = radeon_bo_reserve(rdev->wb.wb_obj, false); 262724c80e1SAlex Deucher if (unlikely(r != 0)) { 263724c80e1SAlex Deucher radeon_wb_fini(rdev); 264724c80e1SAlex Deucher return r; 265724c80e1SAlex Deucher } 266724c80e1SAlex Deucher r = radeon_bo_pin(rdev->wb.wb_obj, RADEON_GEM_DOMAIN_GTT, 267724c80e1SAlex Deucher &rdev->wb.gpu_addr); 268724c80e1SAlex Deucher if (r) { 269724c80e1SAlex Deucher radeon_bo_unreserve(rdev->wb.wb_obj); 270724c80e1SAlex Deucher dev_warn(rdev->dev, "(%d) pin WB bo failed\n", r); 271724c80e1SAlex Deucher radeon_wb_fini(rdev); 272724c80e1SAlex Deucher return r; 273724c80e1SAlex Deucher } 274724c80e1SAlex Deucher r = radeon_bo_kmap(rdev->wb.wb_obj, (void **)&rdev->wb.wb); 275724c80e1SAlex Deucher radeon_bo_unreserve(rdev->wb.wb_obj); 276724c80e1SAlex Deucher if (r) { 277724c80e1SAlex Deucher dev_warn(rdev->dev, "(%d) map WB bo failed\n", r); 278724c80e1SAlex Deucher radeon_wb_fini(rdev); 279724c80e1SAlex Deucher return r; 280724c80e1SAlex Deucher } 281724c80e1SAlex Deucher 282e6ba7599SAlex Deucher /* clear wb memory */ 283e6ba7599SAlex Deucher memset((char *)rdev->wb.wb, 0, RADEON_GPU_PAGE_SIZE); 284d0f8a854SAlex Deucher /* disable event_write fences */ 285d0f8a854SAlex Deucher rdev->wb.use_event = false; 286724c80e1SAlex Deucher /* disabled via module param */ 2873b7a2b24SJerome Glisse if (radeon_no_wb == 1) { 288724c80e1SAlex Deucher rdev->wb.enabled = false; 2893b7a2b24SJerome Glisse } else { 290724c80e1SAlex Deucher if (rdev->flags & RADEON_IS_AGP) { 29128eebb70SAlex Deucher /* often unreliable on AGP */ 29228eebb70SAlex Deucher rdev->wb.enabled = false; 29328eebb70SAlex Deucher } else if (rdev->family < CHIP_R300) { 29428eebb70SAlex Deucher /* often unreliable on pre-r300 */ 295724c80e1SAlex Deucher rdev->wb.enabled = false; 296d0f8a854SAlex Deucher } else { 297724c80e1SAlex Deucher rdev->wb.enabled = true; 298d0f8a854SAlex Deucher /* event_write fences are only available on r600+ */ 2993b7a2b24SJerome Glisse if (rdev->family >= CHIP_R600) { 300d0f8a854SAlex Deucher rdev->wb.use_event = true; 301d0f8a854SAlex Deucher } 302724c80e1SAlex Deucher } 3033b7a2b24SJerome Glisse } 304c994ead6SAlex Deucher /* always use writeback/events on NI, APUs */ 305c994ead6SAlex Deucher if (rdev->family >= CHIP_PALM) { 3067d52785dSAlex Deucher rdev->wb.enabled = true; 3077d52785dSAlex Deucher rdev->wb.use_event = true; 3087d52785dSAlex Deucher } 309724c80e1SAlex Deucher 310724c80e1SAlex Deucher dev_info(rdev->dev, "WB %sabled\n", rdev->wb.enabled ? "en" : "dis"); 311724c80e1SAlex Deucher 312724c80e1SAlex Deucher return 0; 313724c80e1SAlex Deucher } 314724c80e1SAlex Deucher 315d594e46aSJerome Glisse /** 316d594e46aSJerome Glisse * radeon_vram_location - try to find VRAM location 317d594e46aSJerome Glisse * @rdev: radeon device structure holding all necessary informations 318d594e46aSJerome Glisse * @mc: memory controller structure holding memory informations 319d594e46aSJerome Glisse * @base: base address at which to put VRAM 320d594e46aSJerome Glisse * 321d594e46aSJerome Glisse * Function will place try to place VRAM at base address provided 322d594e46aSJerome Glisse * as parameter (which is so far either PCI aperture address or 323d594e46aSJerome Glisse * for IGP TOM base address). 324d594e46aSJerome Glisse * 325d594e46aSJerome Glisse * If there is not enough space to fit the unvisible VRAM in the 32bits 326d594e46aSJerome Glisse * address space then we limit the VRAM size to the aperture. 327d594e46aSJerome Glisse * 328d594e46aSJerome Glisse * If we are using AGP and if the AGP aperture doesn't allow us to have 329d594e46aSJerome Glisse * room for all the VRAM than we restrict the VRAM to the PCI aperture 330d594e46aSJerome Glisse * size and print a warning. 331d594e46aSJerome Glisse * 332d594e46aSJerome Glisse * This function will never fails, worst case are limiting VRAM. 333d594e46aSJerome Glisse * 334d594e46aSJerome Glisse * Note: GTT start, end, size should be initialized before calling this 335d594e46aSJerome Glisse * function on AGP platform. 336d594e46aSJerome Glisse * 33725985edcSLucas De Marchi * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size, 338d594e46aSJerome Glisse * this shouldn't be a problem as we are using the PCI aperture as a reference. 339d594e46aSJerome Glisse * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but 340d594e46aSJerome Glisse * not IGP. 341d594e46aSJerome Glisse * 342d594e46aSJerome Glisse * Note: we use mc_vram_size as on some board we need to program the mc to 343d594e46aSJerome Glisse * cover the whole aperture even if VRAM size is inferior to aperture size 344d594e46aSJerome Glisse * Novell bug 204882 + along with lots of ubuntu ones 345d594e46aSJerome Glisse * 346d594e46aSJerome Glisse * Note: when limiting vram it's safe to overwritte real_vram_size because 347d594e46aSJerome Glisse * we are not in case where real_vram_size is inferior to mc_vram_size (ie 348d594e46aSJerome Glisse * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu 349d594e46aSJerome Glisse * ones) 350d594e46aSJerome Glisse * 351d594e46aSJerome Glisse * Note: IGP TOM addr should be the same as the aperture addr, we don't 352d594e46aSJerome Glisse * explicitly check for that thought. 353d594e46aSJerome Glisse * 354d594e46aSJerome Glisse * FIXME: when reducing VRAM size align new size on power of 2. 355771fe6b9SJerome Glisse */ 356d594e46aSJerome Glisse void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base) 357771fe6b9SJerome Glisse { 358d594e46aSJerome Glisse mc->vram_start = base; 359d594e46aSJerome Glisse if (mc->mc_vram_size > (0xFFFFFFFF - base + 1)) { 360d594e46aSJerome Glisse dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n"); 361d594e46aSJerome Glisse mc->real_vram_size = mc->aper_size; 362d594e46aSJerome Glisse mc->mc_vram_size = mc->aper_size; 363771fe6b9SJerome Glisse } 364d594e46aSJerome Glisse mc->vram_end = mc->vram_start + mc->mc_vram_size - 1; 3652cbeb4efSJerome Glisse if (rdev->flags & RADEON_IS_AGP && mc->vram_end > mc->gtt_start && mc->vram_start <= mc->gtt_end) { 366d594e46aSJerome Glisse dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n"); 367d594e46aSJerome Glisse mc->real_vram_size = mc->aper_size; 368d594e46aSJerome Glisse mc->mc_vram_size = mc->aper_size; 369771fe6b9SJerome Glisse } 370d594e46aSJerome Glisse mc->vram_end = mc->vram_start + mc->mc_vram_size - 1; 371ba95c45aSMichel Dänzer if (radeon_vram_limit && radeon_vram_limit < mc->real_vram_size) 372ba95c45aSMichel Dänzer mc->real_vram_size = radeon_vram_limit; 373dd7cc55aSAlex Deucher dev_info(rdev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n", 374d594e46aSJerome Glisse mc->mc_vram_size >> 20, mc->vram_start, 375d594e46aSJerome Glisse mc->vram_end, mc->real_vram_size >> 20); 376771fe6b9SJerome Glisse } 377771fe6b9SJerome Glisse 378d594e46aSJerome Glisse /** 379d594e46aSJerome Glisse * radeon_gtt_location - try to find GTT location 380d594e46aSJerome Glisse * @rdev: radeon device structure holding all necessary informations 381d594e46aSJerome Glisse * @mc: memory controller structure holding memory informations 382d594e46aSJerome Glisse * 383d594e46aSJerome Glisse * Function will place try to place GTT before or after VRAM. 384d594e46aSJerome Glisse * 385d594e46aSJerome Glisse * If GTT size is bigger than space left then we ajust GTT size. 386d594e46aSJerome Glisse * Thus function will never fails. 387d594e46aSJerome Glisse * 388d594e46aSJerome Glisse * FIXME: when reducing GTT size align new size on power of 2. 389d594e46aSJerome Glisse */ 390d594e46aSJerome Glisse void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc) 391d594e46aSJerome Glisse { 392d594e46aSJerome Glisse u64 size_af, size_bf; 393d594e46aSJerome Glisse 3948d369bb1SAlex Deucher size_af = ((0xFFFFFFFF - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align; 3958d369bb1SAlex Deucher size_bf = mc->vram_start & ~mc->gtt_base_align; 396d594e46aSJerome Glisse if (size_bf > size_af) { 397d594e46aSJerome Glisse if (mc->gtt_size > size_bf) { 398d594e46aSJerome Glisse dev_warn(rdev->dev, "limiting GTT\n"); 399d594e46aSJerome Glisse mc->gtt_size = size_bf; 400d594e46aSJerome Glisse } 4018d369bb1SAlex Deucher mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size; 402d594e46aSJerome Glisse } else { 403d594e46aSJerome Glisse if (mc->gtt_size > size_af) { 404d594e46aSJerome Glisse dev_warn(rdev->dev, "limiting GTT\n"); 405d594e46aSJerome Glisse mc->gtt_size = size_af; 406d594e46aSJerome Glisse } 4078d369bb1SAlex Deucher mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align; 408d594e46aSJerome Glisse } 409d594e46aSJerome Glisse mc->gtt_end = mc->gtt_start + mc->gtt_size - 1; 410dd7cc55aSAlex Deucher dev_info(rdev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n", 411d594e46aSJerome Glisse mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end); 412d594e46aSJerome Glisse } 413771fe6b9SJerome Glisse 414771fe6b9SJerome Glisse /* 415771fe6b9SJerome Glisse * GPU helpers function. 416771fe6b9SJerome Glisse */ 4170c195119SAlex Deucher /** 4180c195119SAlex Deucher * radeon_card_posted - check if the hw has already been initialized 4190c195119SAlex Deucher * 4200c195119SAlex Deucher * @rdev: radeon_device pointer 4210c195119SAlex Deucher * 4220c195119SAlex Deucher * Check if the asic has been initialized (all asics). 4230c195119SAlex Deucher * Used at driver startup. 4240c195119SAlex Deucher * Returns true if initialized or false if not. 4250c195119SAlex Deucher */ 4269f022ddfSJerome Glisse bool radeon_card_posted(struct radeon_device *rdev) 427771fe6b9SJerome Glisse { 428771fe6b9SJerome Glisse uint32_t reg; 429771fe6b9SJerome Glisse 430bcc65fd8SMatthew Garrett if (efi_enabled && rdev->pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE) 431bcc65fd8SMatthew Garrett return false; 432bcc65fd8SMatthew Garrett 433771fe6b9SJerome Glisse /* first check CRTCs */ 43418007401SAlex Deucher if (ASIC_IS_DCE41(rdev)) { 43518007401SAlex Deucher reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) | 43618007401SAlex Deucher RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET); 43718007401SAlex Deucher if (reg & EVERGREEN_CRTC_MASTER_EN) 43818007401SAlex Deucher return true; 43918007401SAlex Deucher } else if (ASIC_IS_DCE4(rdev)) { 440bcc1c2a1SAlex Deucher reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) | 441bcc1c2a1SAlex Deucher RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET) | 442bcc1c2a1SAlex Deucher RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) | 443bcc1c2a1SAlex Deucher RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET) | 444bcc1c2a1SAlex Deucher RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) | 445bcc1c2a1SAlex Deucher RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET); 446bcc1c2a1SAlex Deucher if (reg & EVERGREEN_CRTC_MASTER_EN) 447bcc1c2a1SAlex Deucher return true; 448bcc1c2a1SAlex Deucher } else if (ASIC_IS_AVIVO(rdev)) { 449771fe6b9SJerome Glisse reg = RREG32(AVIVO_D1CRTC_CONTROL) | 450771fe6b9SJerome Glisse RREG32(AVIVO_D2CRTC_CONTROL); 451771fe6b9SJerome Glisse if (reg & AVIVO_CRTC_EN) { 452771fe6b9SJerome Glisse return true; 453771fe6b9SJerome Glisse } 454771fe6b9SJerome Glisse } else { 455771fe6b9SJerome Glisse reg = RREG32(RADEON_CRTC_GEN_CNTL) | 456771fe6b9SJerome Glisse RREG32(RADEON_CRTC2_GEN_CNTL); 457771fe6b9SJerome Glisse if (reg & RADEON_CRTC_EN) { 458771fe6b9SJerome Glisse return true; 459771fe6b9SJerome Glisse } 460771fe6b9SJerome Glisse } 461771fe6b9SJerome Glisse 462771fe6b9SJerome Glisse /* then check MEM_SIZE, in case the crtcs are off */ 463771fe6b9SJerome Glisse if (rdev->family >= CHIP_R600) 464771fe6b9SJerome Glisse reg = RREG32(R600_CONFIG_MEMSIZE); 465771fe6b9SJerome Glisse else 466771fe6b9SJerome Glisse reg = RREG32(RADEON_CONFIG_MEMSIZE); 467771fe6b9SJerome Glisse 468771fe6b9SJerome Glisse if (reg) 469771fe6b9SJerome Glisse return true; 470771fe6b9SJerome Glisse 471771fe6b9SJerome Glisse return false; 472771fe6b9SJerome Glisse 473771fe6b9SJerome Glisse } 474771fe6b9SJerome Glisse 4750c195119SAlex Deucher /** 4760c195119SAlex Deucher * radeon_update_bandwidth_info - update display bandwidth params 4770c195119SAlex Deucher * 4780c195119SAlex Deucher * @rdev: radeon_device pointer 4790c195119SAlex Deucher * 4800c195119SAlex Deucher * Used when sclk/mclk are switched or display modes are set. 4810c195119SAlex Deucher * params are used to calculate display watermarks (all asics) 4820c195119SAlex Deucher */ 483f47299c5SAlex Deucher void radeon_update_bandwidth_info(struct radeon_device *rdev) 484f47299c5SAlex Deucher { 485f47299c5SAlex Deucher fixed20_12 a; 4868807286eSAlex Deucher u32 sclk = rdev->pm.current_sclk; 4878807286eSAlex Deucher u32 mclk = rdev->pm.current_mclk; 488f47299c5SAlex Deucher 4898807286eSAlex Deucher /* sclk/mclk in Mhz */ 49068adac5eSBen Skeggs a.full = dfixed_const(100); 49168adac5eSBen Skeggs rdev->pm.sclk.full = dfixed_const(sclk); 49268adac5eSBen Skeggs rdev->pm.sclk.full = dfixed_div(rdev->pm.sclk, a); 49368adac5eSBen Skeggs rdev->pm.mclk.full = dfixed_const(mclk); 49468adac5eSBen Skeggs rdev->pm.mclk.full = dfixed_div(rdev->pm.mclk, a); 495f47299c5SAlex Deucher 4968807286eSAlex Deucher if (rdev->flags & RADEON_IS_IGP) { 49768adac5eSBen Skeggs a.full = dfixed_const(16); 498f47299c5SAlex Deucher /* core_bandwidth = sclk(Mhz) * 16 */ 49968adac5eSBen Skeggs rdev->pm.core_bandwidth.full = dfixed_div(rdev->pm.sclk, a); 500f47299c5SAlex Deucher } 501f47299c5SAlex Deucher } 502f47299c5SAlex Deucher 5030c195119SAlex Deucher /** 5040c195119SAlex Deucher * radeon_boot_test_post_card - check and possibly initialize the hw 5050c195119SAlex Deucher * 5060c195119SAlex Deucher * @rdev: radeon_device pointer 5070c195119SAlex Deucher * 5080c195119SAlex Deucher * Check if the asic is initialized and if not, attempt to initialize 5090c195119SAlex Deucher * it (all asics). 5100c195119SAlex Deucher * Returns true if initialized or false if not. 5110c195119SAlex Deucher */ 51272542d77SDave Airlie bool radeon_boot_test_post_card(struct radeon_device *rdev) 51372542d77SDave Airlie { 51472542d77SDave Airlie if (radeon_card_posted(rdev)) 51572542d77SDave Airlie return true; 51672542d77SDave Airlie 51772542d77SDave Airlie if (rdev->bios) { 51872542d77SDave Airlie DRM_INFO("GPU not posted. posting now...\n"); 51972542d77SDave Airlie if (rdev->is_atom_bios) 52072542d77SDave Airlie atom_asic_init(rdev->mode_info.atom_context); 52172542d77SDave Airlie else 52272542d77SDave Airlie radeon_combios_asic_init(rdev->ddev); 52372542d77SDave Airlie return true; 52472542d77SDave Airlie } else { 52572542d77SDave Airlie dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n"); 52672542d77SDave Airlie return false; 52772542d77SDave Airlie } 52872542d77SDave Airlie } 52972542d77SDave Airlie 5300c195119SAlex Deucher /** 5310c195119SAlex Deucher * radeon_dummy_page_init - init dummy page used by the driver 5320c195119SAlex Deucher * 5330c195119SAlex Deucher * @rdev: radeon_device pointer 5340c195119SAlex Deucher * 5350c195119SAlex Deucher * Allocate the dummy page used by the driver (all asics). 5360c195119SAlex Deucher * This dummy page is used by the driver as a filler for gart entries 5370c195119SAlex Deucher * when pages are taken out of the GART 5380c195119SAlex Deucher * Returns 0 on sucess, -ENOMEM on failure. 5390c195119SAlex Deucher */ 5403ce0a23dSJerome Glisse int radeon_dummy_page_init(struct radeon_device *rdev) 5413ce0a23dSJerome Glisse { 54282568565SDave Airlie if (rdev->dummy_page.page) 54382568565SDave Airlie return 0; 5443ce0a23dSJerome Glisse rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO); 5453ce0a23dSJerome Glisse if (rdev->dummy_page.page == NULL) 5463ce0a23dSJerome Glisse return -ENOMEM; 5473ce0a23dSJerome Glisse rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page, 5483ce0a23dSJerome Glisse 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); 549a30f6fb7SBenjamin Herrenschmidt if (pci_dma_mapping_error(rdev->pdev, rdev->dummy_page.addr)) { 550a30f6fb7SBenjamin Herrenschmidt dev_err(&rdev->pdev->dev, "Failed to DMA MAP the dummy page\n"); 5513ce0a23dSJerome Glisse __free_page(rdev->dummy_page.page); 5523ce0a23dSJerome Glisse rdev->dummy_page.page = NULL; 5533ce0a23dSJerome Glisse return -ENOMEM; 5543ce0a23dSJerome Glisse } 5553ce0a23dSJerome Glisse return 0; 5563ce0a23dSJerome Glisse } 5573ce0a23dSJerome Glisse 5580c195119SAlex Deucher /** 5590c195119SAlex Deucher * radeon_dummy_page_fini - free dummy page used by the driver 5600c195119SAlex Deucher * 5610c195119SAlex Deucher * @rdev: radeon_device pointer 5620c195119SAlex Deucher * 5630c195119SAlex Deucher * Frees the dummy page used by the driver (all asics). 5640c195119SAlex Deucher */ 5653ce0a23dSJerome Glisse void radeon_dummy_page_fini(struct radeon_device *rdev) 5663ce0a23dSJerome Glisse { 5673ce0a23dSJerome Glisse if (rdev->dummy_page.page == NULL) 5683ce0a23dSJerome Glisse return; 5693ce0a23dSJerome Glisse pci_unmap_page(rdev->pdev, rdev->dummy_page.addr, 5703ce0a23dSJerome Glisse PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); 5713ce0a23dSJerome Glisse __free_page(rdev->dummy_page.page); 5723ce0a23dSJerome Glisse rdev->dummy_page.page = NULL; 5733ce0a23dSJerome Glisse } 5743ce0a23dSJerome Glisse 575771fe6b9SJerome Glisse 576771fe6b9SJerome Glisse /* ATOM accessor methods */ 5770c195119SAlex Deucher /* 5780c195119SAlex Deucher * ATOM is an interpreted byte code stored in tables in the vbios. The 5790c195119SAlex Deucher * driver registers callbacks to access registers and the interpreter 5800c195119SAlex Deucher * in the driver parses the tables and executes then to program specific 5810c195119SAlex Deucher * actions (set display modes, asic init, etc.). See radeon_atombios.c, 5820c195119SAlex Deucher * atombios.h, and atom.c 5830c195119SAlex Deucher */ 5840c195119SAlex Deucher 5850c195119SAlex Deucher /** 5860c195119SAlex Deucher * cail_pll_read - read PLL register 5870c195119SAlex Deucher * 5880c195119SAlex Deucher * @info: atom card_info pointer 5890c195119SAlex Deucher * @reg: PLL register offset 5900c195119SAlex Deucher * 5910c195119SAlex Deucher * Provides a PLL register accessor for the atom interpreter (r4xx+). 5920c195119SAlex Deucher * Returns the value of the PLL register. 5930c195119SAlex Deucher */ 594771fe6b9SJerome Glisse static uint32_t cail_pll_read(struct card_info *info, uint32_t reg) 595771fe6b9SJerome Glisse { 596771fe6b9SJerome Glisse struct radeon_device *rdev = info->dev->dev_private; 597771fe6b9SJerome Glisse uint32_t r; 598771fe6b9SJerome Glisse 599771fe6b9SJerome Glisse r = rdev->pll_rreg(rdev, reg); 600771fe6b9SJerome Glisse return r; 601771fe6b9SJerome Glisse } 602771fe6b9SJerome Glisse 6030c195119SAlex Deucher /** 6040c195119SAlex Deucher * cail_pll_write - write PLL register 6050c195119SAlex Deucher * 6060c195119SAlex Deucher * @info: atom card_info pointer 6070c195119SAlex Deucher * @reg: PLL register offset 6080c195119SAlex Deucher * @val: value to write to the pll register 6090c195119SAlex Deucher * 6100c195119SAlex Deucher * Provides a PLL register accessor for the atom interpreter (r4xx+). 6110c195119SAlex Deucher */ 612771fe6b9SJerome Glisse static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val) 613771fe6b9SJerome Glisse { 614771fe6b9SJerome Glisse struct radeon_device *rdev = info->dev->dev_private; 615771fe6b9SJerome Glisse 616771fe6b9SJerome Glisse rdev->pll_wreg(rdev, reg, val); 617771fe6b9SJerome Glisse } 618771fe6b9SJerome Glisse 6190c195119SAlex Deucher /** 6200c195119SAlex Deucher * cail_mc_read - read MC (Memory Controller) register 6210c195119SAlex Deucher * 6220c195119SAlex Deucher * @info: atom card_info pointer 6230c195119SAlex Deucher * @reg: MC register offset 6240c195119SAlex Deucher * 6250c195119SAlex Deucher * Provides an MC register accessor for the atom interpreter (r4xx+). 6260c195119SAlex Deucher * Returns the value of the MC register. 6270c195119SAlex Deucher */ 628771fe6b9SJerome Glisse static uint32_t cail_mc_read(struct card_info *info, uint32_t reg) 629771fe6b9SJerome Glisse { 630771fe6b9SJerome Glisse struct radeon_device *rdev = info->dev->dev_private; 631771fe6b9SJerome Glisse uint32_t r; 632771fe6b9SJerome Glisse 633771fe6b9SJerome Glisse r = rdev->mc_rreg(rdev, reg); 634771fe6b9SJerome Glisse return r; 635771fe6b9SJerome Glisse } 636771fe6b9SJerome Glisse 6370c195119SAlex Deucher /** 6380c195119SAlex Deucher * cail_mc_write - write MC (Memory Controller) register 6390c195119SAlex Deucher * 6400c195119SAlex Deucher * @info: atom card_info pointer 6410c195119SAlex Deucher * @reg: MC register offset 6420c195119SAlex Deucher * @val: value to write to the pll register 6430c195119SAlex Deucher * 6440c195119SAlex Deucher * Provides a MC register accessor for the atom interpreter (r4xx+). 6450c195119SAlex Deucher */ 646771fe6b9SJerome Glisse static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val) 647771fe6b9SJerome Glisse { 648771fe6b9SJerome Glisse struct radeon_device *rdev = info->dev->dev_private; 649771fe6b9SJerome Glisse 650771fe6b9SJerome Glisse rdev->mc_wreg(rdev, reg, val); 651771fe6b9SJerome Glisse } 652771fe6b9SJerome Glisse 6530c195119SAlex Deucher /** 6540c195119SAlex Deucher * cail_reg_write - write MMIO register 6550c195119SAlex Deucher * 6560c195119SAlex Deucher * @info: atom card_info pointer 6570c195119SAlex Deucher * @reg: MMIO register offset 6580c195119SAlex Deucher * @val: value to write to the pll register 6590c195119SAlex Deucher * 6600c195119SAlex Deucher * Provides a MMIO register accessor for the atom interpreter (r4xx+). 6610c195119SAlex Deucher */ 662771fe6b9SJerome Glisse static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val) 663771fe6b9SJerome Glisse { 664771fe6b9SJerome Glisse struct radeon_device *rdev = info->dev->dev_private; 665771fe6b9SJerome Glisse 666771fe6b9SJerome Glisse WREG32(reg*4, val); 667771fe6b9SJerome Glisse } 668771fe6b9SJerome Glisse 6690c195119SAlex Deucher /** 6700c195119SAlex Deucher * cail_reg_read - read MMIO register 6710c195119SAlex Deucher * 6720c195119SAlex Deucher * @info: atom card_info pointer 6730c195119SAlex Deucher * @reg: MMIO register offset 6740c195119SAlex Deucher * 6750c195119SAlex Deucher * Provides an MMIO register accessor for the atom interpreter (r4xx+). 6760c195119SAlex Deucher * Returns the value of the MMIO register. 6770c195119SAlex Deucher */ 678771fe6b9SJerome Glisse static uint32_t cail_reg_read(struct card_info *info, uint32_t reg) 679771fe6b9SJerome Glisse { 680771fe6b9SJerome Glisse struct radeon_device *rdev = info->dev->dev_private; 681771fe6b9SJerome Glisse uint32_t r; 682771fe6b9SJerome Glisse 683771fe6b9SJerome Glisse r = RREG32(reg*4); 684771fe6b9SJerome Glisse return r; 685771fe6b9SJerome Glisse } 686771fe6b9SJerome Glisse 6870c195119SAlex Deucher /** 6880c195119SAlex Deucher * cail_ioreg_write - write IO register 6890c195119SAlex Deucher * 6900c195119SAlex Deucher * @info: atom card_info pointer 6910c195119SAlex Deucher * @reg: IO register offset 6920c195119SAlex Deucher * @val: value to write to the pll register 6930c195119SAlex Deucher * 6940c195119SAlex Deucher * Provides a IO register accessor for the atom interpreter (r4xx+). 6950c195119SAlex Deucher */ 696351a52a2SAlex Deucher static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val) 697351a52a2SAlex Deucher { 698351a52a2SAlex Deucher struct radeon_device *rdev = info->dev->dev_private; 699351a52a2SAlex Deucher 700351a52a2SAlex Deucher WREG32_IO(reg*4, val); 701351a52a2SAlex Deucher } 702351a52a2SAlex Deucher 7030c195119SAlex Deucher /** 7040c195119SAlex Deucher * cail_ioreg_read - read IO register 7050c195119SAlex Deucher * 7060c195119SAlex Deucher * @info: atom card_info pointer 7070c195119SAlex Deucher * @reg: IO register offset 7080c195119SAlex Deucher * 7090c195119SAlex Deucher * Provides an IO register accessor for the atom interpreter (r4xx+). 7100c195119SAlex Deucher * Returns the value of the IO register. 7110c195119SAlex Deucher */ 712351a52a2SAlex Deucher static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg) 713351a52a2SAlex Deucher { 714351a52a2SAlex Deucher struct radeon_device *rdev = info->dev->dev_private; 715351a52a2SAlex Deucher uint32_t r; 716351a52a2SAlex Deucher 717351a52a2SAlex Deucher r = RREG32_IO(reg*4); 718351a52a2SAlex Deucher return r; 719351a52a2SAlex Deucher } 720351a52a2SAlex Deucher 7210c195119SAlex Deucher /** 7220c195119SAlex Deucher * radeon_atombios_init - init the driver info and callbacks for atombios 7230c195119SAlex Deucher * 7240c195119SAlex Deucher * @rdev: radeon_device pointer 7250c195119SAlex Deucher * 7260c195119SAlex Deucher * Initializes the driver info and register access callbacks for the 7270c195119SAlex Deucher * ATOM interpreter (r4xx+). 7280c195119SAlex Deucher * Returns 0 on sucess, -ENOMEM on failure. 7290c195119SAlex Deucher * Called at driver startup. 7300c195119SAlex Deucher */ 731771fe6b9SJerome Glisse int radeon_atombios_init(struct radeon_device *rdev) 732771fe6b9SJerome Glisse { 73361c4b24bSMathias Fröhlich struct card_info *atom_card_info = 73461c4b24bSMathias Fröhlich kzalloc(sizeof(struct card_info), GFP_KERNEL); 73561c4b24bSMathias Fröhlich 73661c4b24bSMathias Fröhlich if (!atom_card_info) 73761c4b24bSMathias Fröhlich return -ENOMEM; 73861c4b24bSMathias Fröhlich 73961c4b24bSMathias Fröhlich rdev->mode_info.atom_card_info = atom_card_info; 74061c4b24bSMathias Fröhlich atom_card_info->dev = rdev->ddev; 74161c4b24bSMathias Fröhlich atom_card_info->reg_read = cail_reg_read; 74261c4b24bSMathias Fröhlich atom_card_info->reg_write = cail_reg_write; 743351a52a2SAlex Deucher /* needed for iio ops */ 744351a52a2SAlex Deucher if (rdev->rio_mem) { 745351a52a2SAlex Deucher atom_card_info->ioreg_read = cail_ioreg_read; 746351a52a2SAlex Deucher atom_card_info->ioreg_write = cail_ioreg_write; 747351a52a2SAlex Deucher } else { 748351a52a2SAlex Deucher DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n"); 749351a52a2SAlex Deucher atom_card_info->ioreg_read = cail_reg_read; 750351a52a2SAlex Deucher atom_card_info->ioreg_write = cail_reg_write; 751351a52a2SAlex Deucher } 75261c4b24bSMathias Fröhlich atom_card_info->mc_read = cail_mc_read; 75361c4b24bSMathias Fröhlich atom_card_info->mc_write = cail_mc_write; 75461c4b24bSMathias Fröhlich atom_card_info->pll_read = cail_pll_read; 75561c4b24bSMathias Fröhlich atom_card_info->pll_write = cail_pll_write; 75661c4b24bSMathias Fröhlich 75761c4b24bSMathias Fröhlich rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios); 758c31ad97fSRafał Miłecki mutex_init(&rdev->mode_info.atom_context->mutex); 759771fe6b9SJerome Glisse radeon_atom_initialize_bios_scratch_regs(rdev->ddev); 760d904ef9bSDave Airlie atom_allocate_fb_scratch(rdev->mode_info.atom_context); 761771fe6b9SJerome Glisse return 0; 762771fe6b9SJerome Glisse } 763771fe6b9SJerome Glisse 7640c195119SAlex Deucher /** 7650c195119SAlex Deucher * radeon_atombios_fini - free the driver info and callbacks for atombios 7660c195119SAlex Deucher * 7670c195119SAlex Deucher * @rdev: radeon_device pointer 7680c195119SAlex Deucher * 7690c195119SAlex Deucher * Frees the driver info and register access callbacks for the ATOM 7700c195119SAlex Deucher * interpreter (r4xx+). 7710c195119SAlex Deucher * Called at driver shutdown. 7720c195119SAlex Deucher */ 773771fe6b9SJerome Glisse void radeon_atombios_fini(struct radeon_device *rdev) 774771fe6b9SJerome Glisse { 7754a04a844SJerome Glisse if (rdev->mode_info.atom_context) { 776d904ef9bSDave Airlie kfree(rdev->mode_info.atom_context->scratch); 777771fe6b9SJerome Glisse kfree(rdev->mode_info.atom_context); 7784a04a844SJerome Glisse } 77961c4b24bSMathias Fröhlich kfree(rdev->mode_info.atom_card_info); 780771fe6b9SJerome Glisse } 781771fe6b9SJerome Glisse 7820c195119SAlex Deucher /* COMBIOS */ 7830c195119SAlex Deucher /* 7840c195119SAlex Deucher * COMBIOS is the bios format prior to ATOM. It provides 7850c195119SAlex Deucher * command tables similar to ATOM, but doesn't have a unified 7860c195119SAlex Deucher * parser. See radeon_combios.c 7870c195119SAlex Deucher */ 7880c195119SAlex Deucher 7890c195119SAlex Deucher /** 7900c195119SAlex Deucher * radeon_combios_init - init the driver info for combios 7910c195119SAlex Deucher * 7920c195119SAlex Deucher * @rdev: radeon_device pointer 7930c195119SAlex Deucher * 7940c195119SAlex Deucher * Initializes the driver info for combios (r1xx-r3xx). 7950c195119SAlex Deucher * Returns 0 on sucess. 7960c195119SAlex Deucher * Called at driver startup. 7970c195119SAlex Deucher */ 798771fe6b9SJerome Glisse int radeon_combios_init(struct radeon_device *rdev) 799771fe6b9SJerome Glisse { 800771fe6b9SJerome Glisse radeon_combios_initialize_bios_scratch_regs(rdev->ddev); 801771fe6b9SJerome Glisse return 0; 802771fe6b9SJerome Glisse } 803771fe6b9SJerome Glisse 8040c195119SAlex Deucher /** 8050c195119SAlex Deucher * radeon_combios_fini - free the driver info for combios 8060c195119SAlex Deucher * 8070c195119SAlex Deucher * @rdev: radeon_device pointer 8080c195119SAlex Deucher * 8090c195119SAlex Deucher * Frees the driver info for combios (r1xx-r3xx). 8100c195119SAlex Deucher * Called at driver shutdown. 8110c195119SAlex Deucher */ 812771fe6b9SJerome Glisse void radeon_combios_fini(struct radeon_device *rdev) 813771fe6b9SJerome Glisse { 814771fe6b9SJerome Glisse } 815771fe6b9SJerome Glisse 8160c195119SAlex Deucher /* if we get transitioned to only one device, take VGA back */ 8170c195119SAlex Deucher /** 8180c195119SAlex Deucher * radeon_vga_set_decode - enable/disable vga decode 8190c195119SAlex Deucher * 8200c195119SAlex Deucher * @cookie: radeon_device pointer 8210c195119SAlex Deucher * @state: enable/disable vga decode 8220c195119SAlex Deucher * 8230c195119SAlex Deucher * Enable/disable vga decode (all asics). 8240c195119SAlex Deucher * Returns VGA resource flags. 8250c195119SAlex Deucher */ 82628d52043SDave Airlie static unsigned int radeon_vga_set_decode(void *cookie, bool state) 82728d52043SDave Airlie { 82828d52043SDave Airlie struct radeon_device *rdev = cookie; 82928d52043SDave Airlie radeon_vga_set_state(rdev, state); 83028d52043SDave Airlie if (state) 83128d52043SDave Airlie return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM | 83228d52043SDave Airlie VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; 83328d52043SDave Airlie else 83428d52043SDave Airlie return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; 83528d52043SDave Airlie } 836c1176d6fSDave Airlie 8370c195119SAlex Deucher /** 8380c195119SAlex Deucher * radeon_check_arguments - validate module params 8390c195119SAlex Deucher * 8400c195119SAlex Deucher * @rdev: radeon_device pointer 8410c195119SAlex Deucher * 8420c195119SAlex Deucher * Validates certain module parameters and updates 8430c195119SAlex Deucher * the associated values used by the driver (all asics). 8440c195119SAlex Deucher */ 8451109ca09SLauri Kasanen static void radeon_check_arguments(struct radeon_device *rdev) 84636421338SJerome Glisse { 84736421338SJerome Glisse /* vramlimit must be a power of two */ 84836421338SJerome Glisse switch (radeon_vram_limit) { 84936421338SJerome Glisse case 0: 85036421338SJerome Glisse case 4: 85136421338SJerome Glisse case 8: 85236421338SJerome Glisse case 16: 85336421338SJerome Glisse case 32: 85436421338SJerome Glisse case 64: 85536421338SJerome Glisse case 128: 85636421338SJerome Glisse case 256: 85736421338SJerome Glisse case 512: 85836421338SJerome Glisse case 1024: 85936421338SJerome Glisse case 2048: 86036421338SJerome Glisse case 4096: 86136421338SJerome Glisse break; 86236421338SJerome Glisse default: 86336421338SJerome Glisse dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n", 86436421338SJerome Glisse radeon_vram_limit); 86536421338SJerome Glisse radeon_vram_limit = 0; 86636421338SJerome Glisse break; 86736421338SJerome Glisse } 86836421338SJerome Glisse radeon_vram_limit = radeon_vram_limit << 20; 86936421338SJerome Glisse /* gtt size must be power of two and greater or equal to 32M */ 87036421338SJerome Glisse switch (radeon_gart_size) { 87136421338SJerome Glisse case 4: 87236421338SJerome Glisse case 8: 87336421338SJerome Glisse case 16: 87436421338SJerome Glisse dev_warn(rdev->dev, "gart size (%d) too small forcing to 512M\n", 87536421338SJerome Glisse radeon_gart_size); 87636421338SJerome Glisse radeon_gart_size = 512; 87736421338SJerome Glisse break; 87836421338SJerome Glisse case 32: 87936421338SJerome Glisse case 64: 88036421338SJerome Glisse case 128: 88136421338SJerome Glisse case 256: 88236421338SJerome Glisse case 512: 88336421338SJerome Glisse case 1024: 88436421338SJerome Glisse case 2048: 88536421338SJerome Glisse case 4096: 88636421338SJerome Glisse break; 88736421338SJerome Glisse default: 88836421338SJerome Glisse dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n", 88936421338SJerome Glisse radeon_gart_size); 89036421338SJerome Glisse radeon_gart_size = 512; 89136421338SJerome Glisse break; 89236421338SJerome Glisse } 89336421338SJerome Glisse rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024; 89436421338SJerome Glisse /* AGP mode can only be -1, 1, 2, 4, 8 */ 89536421338SJerome Glisse switch (radeon_agpmode) { 89636421338SJerome Glisse case -1: 89736421338SJerome Glisse case 0: 89836421338SJerome Glisse case 1: 89936421338SJerome Glisse case 2: 90036421338SJerome Glisse case 4: 90136421338SJerome Glisse case 8: 90236421338SJerome Glisse break; 90336421338SJerome Glisse default: 90436421338SJerome Glisse dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: " 90536421338SJerome Glisse "-1, 0, 1, 2, 4, 8)\n", radeon_agpmode); 90636421338SJerome Glisse radeon_agpmode = 0; 90736421338SJerome Glisse break; 90836421338SJerome Glisse } 90936421338SJerome Glisse } 91036421338SJerome Glisse 9110c195119SAlex Deucher /** 9120c195119SAlex Deucher * radeon_switcheroo_set_state - set switcheroo state 9130c195119SAlex Deucher * 9140c195119SAlex Deucher * @pdev: pci dev pointer 9150c195119SAlex Deucher * @state: vga switcheroo state 9160c195119SAlex Deucher * 9170c195119SAlex Deucher * Callback for the switcheroo driver. Suspends or resumes the 9180c195119SAlex Deucher * the asics before or after it is powered up using ACPI methods. 9190c195119SAlex Deucher */ 9206a9ee8afSDave Airlie static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state) 9216a9ee8afSDave Airlie { 9226a9ee8afSDave Airlie struct drm_device *dev = pci_get_drvdata(pdev); 9236a9ee8afSDave Airlie pm_message_t pmm = { .event = PM_EVENT_SUSPEND }; 9246a9ee8afSDave Airlie if (state == VGA_SWITCHEROO_ON) { 9256a9ee8afSDave Airlie printk(KERN_INFO "radeon: switched on\n"); 9266a9ee8afSDave Airlie /* don't suspend or resume card normally */ 9275bcf719bSDave Airlie dev->switch_power_state = DRM_SWITCH_POWER_CHANGING; 9286a9ee8afSDave Airlie radeon_resume_kms(dev); 9295bcf719bSDave Airlie dev->switch_power_state = DRM_SWITCH_POWER_ON; 930fbf81762SDave Airlie drm_kms_helper_poll_enable(dev); 9316a9ee8afSDave Airlie } else { 9326a9ee8afSDave Airlie printk(KERN_INFO "radeon: switched off\n"); 933fbf81762SDave Airlie drm_kms_helper_poll_disable(dev); 9345bcf719bSDave Airlie dev->switch_power_state = DRM_SWITCH_POWER_CHANGING; 9356a9ee8afSDave Airlie radeon_suspend_kms(dev, pmm); 9365bcf719bSDave Airlie dev->switch_power_state = DRM_SWITCH_POWER_OFF; 9376a9ee8afSDave Airlie } 9386a9ee8afSDave Airlie } 9396a9ee8afSDave Airlie 9400c195119SAlex Deucher /** 9410c195119SAlex Deucher * radeon_switcheroo_can_switch - see if switcheroo state can change 9420c195119SAlex Deucher * 9430c195119SAlex Deucher * @pdev: pci dev pointer 9440c195119SAlex Deucher * 9450c195119SAlex Deucher * Callback for the switcheroo driver. Check of the switcheroo 9460c195119SAlex Deucher * state can be changed. 9470c195119SAlex Deucher * Returns true if the state can be changed, false if not. 9480c195119SAlex Deucher */ 9496a9ee8afSDave Airlie static bool radeon_switcheroo_can_switch(struct pci_dev *pdev) 9506a9ee8afSDave Airlie { 9516a9ee8afSDave Airlie struct drm_device *dev = pci_get_drvdata(pdev); 9526a9ee8afSDave Airlie bool can_switch; 9536a9ee8afSDave Airlie 9546a9ee8afSDave Airlie spin_lock(&dev->count_lock); 9556a9ee8afSDave Airlie can_switch = (dev->open_count == 0); 9566a9ee8afSDave Airlie spin_unlock(&dev->count_lock); 9576a9ee8afSDave Airlie return can_switch; 9586a9ee8afSDave Airlie } 9596a9ee8afSDave Airlie 96026ec685fSTakashi Iwai static const struct vga_switcheroo_client_ops radeon_switcheroo_ops = { 96126ec685fSTakashi Iwai .set_gpu_state = radeon_switcheroo_set_state, 96226ec685fSTakashi Iwai .reprobe = NULL, 96326ec685fSTakashi Iwai .can_switch = radeon_switcheroo_can_switch, 96426ec685fSTakashi Iwai }; 9656a9ee8afSDave Airlie 9660c195119SAlex Deucher /** 9670c195119SAlex Deucher * radeon_device_init - initialize the driver 9680c195119SAlex Deucher * 9690c195119SAlex Deucher * @rdev: radeon_device pointer 9700c195119SAlex Deucher * @pdev: drm dev pointer 9710c195119SAlex Deucher * @pdev: pci dev pointer 9720c195119SAlex Deucher * @flags: driver flags 9730c195119SAlex Deucher * 9740c195119SAlex Deucher * Initializes the driver info and hw (all asics). 9750c195119SAlex Deucher * Returns 0 for success or an error on failure. 9760c195119SAlex Deucher * Called at driver startup. 9770c195119SAlex Deucher */ 978771fe6b9SJerome Glisse int radeon_device_init(struct radeon_device *rdev, 979771fe6b9SJerome Glisse struct drm_device *ddev, 980771fe6b9SJerome Glisse struct pci_dev *pdev, 981771fe6b9SJerome Glisse uint32_t flags) 982771fe6b9SJerome Glisse { 983351a52a2SAlex Deucher int r, i; 984ad49f501SDave Airlie int dma_bits; 985771fe6b9SJerome Glisse 986771fe6b9SJerome Glisse rdev->shutdown = false; 9879f022ddfSJerome Glisse rdev->dev = &pdev->dev; 988771fe6b9SJerome Glisse rdev->ddev = ddev; 989771fe6b9SJerome Glisse rdev->pdev = pdev; 990771fe6b9SJerome Glisse rdev->flags = flags; 991771fe6b9SJerome Glisse rdev->family = flags & RADEON_FAMILY_MASK; 992771fe6b9SJerome Glisse rdev->is_atom_bios = false; 993771fe6b9SJerome Glisse rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT; 994771fe6b9SJerome Glisse rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024; 995733289c2SJerome Glisse rdev->accel_working = false; 9968b25ed34SAlex Deucher /* set up ring ids */ 9978b25ed34SAlex Deucher for (i = 0; i < RADEON_NUM_RINGS; i++) { 9988b25ed34SAlex Deucher rdev->ring[i].idx = i; 9998b25ed34SAlex Deucher } 10001b5331d9SJerome Glisse 1001d522d9ccSThomas Reim DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X).\n", 1002d522d9ccSThomas Reim radeon_family_name[rdev->family], pdev->vendor, pdev->device, 1003d522d9ccSThomas Reim pdev->subsystem_vendor, pdev->subsystem_device); 10041b5331d9SJerome Glisse 1005771fe6b9SJerome Glisse /* mutex initialization are all done here so we 1006771fe6b9SJerome Glisse * can recall function without having locking issues */ 1007d6999bc7SChristian König mutex_init(&rdev->ring_lock); 100840bacf16SAlex Deucher mutex_init(&rdev->dc_hw_i2c_mutex); 1009c20dc369SChristian Koenig atomic_set(&rdev->ih.lock, 0); 10104c788679SJerome Glisse mutex_init(&rdev->gem.mutex); 1011c913e23aSRafał Miłecki mutex_init(&rdev->pm.mutex); 10126759a0a7SMarek Olšák mutex_init(&rdev->gpu_clock_mutex); 1013db7fce39SChristian König init_rwsem(&rdev->pm.mclk_lock); 1014dee53e7fSJerome Glisse init_rwsem(&rdev->exclusive_lock); 101573a6d3fcSRafał Miłecki init_waitqueue_head(&rdev->irq.vblank_queue); 10161b9c3dd0SAlex Deucher r = radeon_gem_init(rdev); 10171b9c3dd0SAlex Deucher if (r) 10181b9c3dd0SAlex Deucher return r; 1019721604a1SJerome Glisse /* initialize vm here */ 102036ff39c4SChristian König mutex_init(&rdev->vm_manager.lock); 1021721604a1SJerome Glisse rdev->vm_manager.max_pfn = 1 << 20; 1022721604a1SJerome Glisse INIT_LIST_HEAD(&rdev->vm_manager.lru_vm); 1023771fe6b9SJerome Glisse 10244aac0473SJerome Glisse /* Set asic functions */ 10254aac0473SJerome Glisse r = radeon_asic_init(rdev); 102636421338SJerome Glisse if (r) 10274aac0473SJerome Glisse return r; 102836421338SJerome Glisse radeon_check_arguments(rdev); 10294aac0473SJerome Glisse 1030f95df9caSAlex Deucher /* all of the newer IGP chips have an internal gart 1031f95df9caSAlex Deucher * However some rs4xx report as AGP, so remove that here. 1032f95df9caSAlex Deucher */ 1033f95df9caSAlex Deucher if ((rdev->family >= CHIP_RS400) && 1034f95df9caSAlex Deucher (rdev->flags & RADEON_IS_IGP)) { 1035f95df9caSAlex Deucher rdev->flags &= ~RADEON_IS_AGP; 1036f95df9caSAlex Deucher } 1037f95df9caSAlex Deucher 103830256a3fSJerome Glisse if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) { 1039b574f251SJerome Glisse radeon_agp_disable(rdev); 1040771fe6b9SJerome Glisse } 1041771fe6b9SJerome Glisse 1042ad49f501SDave Airlie /* set DMA mask + need_dma32 flags. 1043ad49f501SDave Airlie * PCIE - can handle 40-bits. 1044005a83f1SAlex Deucher * IGP - can handle 40-bits 1045ad49f501SDave Airlie * AGP - generally dma32 is safest 1046005a83f1SAlex Deucher * PCI - dma32 for legacy pci gart, 40 bits on newer asics 1047ad49f501SDave Airlie */ 1048ad49f501SDave Airlie rdev->need_dma32 = false; 1049ad49f501SDave Airlie if (rdev->flags & RADEON_IS_AGP) 1050ad49f501SDave Airlie rdev->need_dma32 = true; 1051005a83f1SAlex Deucher if ((rdev->flags & RADEON_IS_PCI) && 10524a2b6662SJerome Glisse (rdev->family <= CHIP_RS740)) 1053ad49f501SDave Airlie rdev->need_dma32 = true; 1054ad49f501SDave Airlie 1055ad49f501SDave Airlie dma_bits = rdev->need_dma32 ? 32 : 40; 1056ad49f501SDave Airlie r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits)); 1057771fe6b9SJerome Glisse if (r) { 105862fff811SDaniel Haid rdev->need_dma32 = true; 1059c52494f6SKonrad Rzeszutek Wilk dma_bits = 32; 1060771fe6b9SJerome Glisse printk(KERN_WARNING "radeon: No suitable DMA available.\n"); 1061771fe6b9SJerome Glisse } 1062c52494f6SKonrad Rzeszutek Wilk r = pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits)); 1063c52494f6SKonrad Rzeszutek Wilk if (r) { 1064c52494f6SKonrad Rzeszutek Wilk pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(32)); 1065c52494f6SKonrad Rzeszutek Wilk printk(KERN_WARNING "radeon: No coherent DMA available.\n"); 1066c52494f6SKonrad Rzeszutek Wilk } 1067771fe6b9SJerome Glisse 1068771fe6b9SJerome Glisse /* Registers mapping */ 1069771fe6b9SJerome Glisse /* TODO: block userspace mapping of io register */ 107001d73a69SJordan Crouse rdev->rmmio_base = pci_resource_start(rdev->pdev, 2); 107101d73a69SJordan Crouse rdev->rmmio_size = pci_resource_len(rdev->pdev, 2); 1072771fe6b9SJerome Glisse rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size); 1073771fe6b9SJerome Glisse if (rdev->rmmio == NULL) { 1074771fe6b9SJerome Glisse return -ENOMEM; 1075771fe6b9SJerome Glisse } 1076771fe6b9SJerome Glisse DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base); 1077771fe6b9SJerome Glisse DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size); 1078771fe6b9SJerome Glisse 1079351a52a2SAlex Deucher /* io port mapping */ 1080351a52a2SAlex Deucher for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { 1081351a52a2SAlex Deucher if (pci_resource_flags(rdev->pdev, i) & IORESOURCE_IO) { 1082351a52a2SAlex Deucher rdev->rio_mem_size = pci_resource_len(rdev->pdev, i); 1083351a52a2SAlex Deucher rdev->rio_mem = pci_iomap(rdev->pdev, i, rdev->rio_mem_size); 1084351a52a2SAlex Deucher break; 1085351a52a2SAlex Deucher } 1086351a52a2SAlex Deucher } 1087351a52a2SAlex Deucher if (rdev->rio_mem == NULL) 1088351a52a2SAlex Deucher DRM_ERROR("Unable to find PCI I/O BAR\n"); 1089351a52a2SAlex Deucher 109028d52043SDave Airlie /* if we have > 1 VGA cards, then disable the radeon VGA resources */ 109193239ea1SDave Airlie /* this will fail for cards that aren't VGA class devices, just 109293239ea1SDave Airlie * ignore it */ 109393239ea1SDave Airlie vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode); 109426ec685fSTakashi Iwai vga_switcheroo_register_client(rdev->pdev, &radeon_switcheroo_ops); 109528d52043SDave Airlie 10963ce0a23dSJerome Glisse r = radeon_init(rdev); 1097b574f251SJerome Glisse if (r) 1098b574f251SJerome Glisse return r; 1099b1e3a6d1SMichel Dänzer 110004eb2206SChristian König r = radeon_ib_ring_tests(rdev); 110104eb2206SChristian König if (r) 110204eb2206SChristian König DRM_ERROR("ib ring test failed (%d).\n", r); 110304eb2206SChristian König 1104b574f251SJerome Glisse if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) { 1105b574f251SJerome Glisse /* Acceleration not working on AGP card try again 1106b574f251SJerome Glisse * with fallback to PCI or PCIE GART 1107b574f251SJerome Glisse */ 1108a2d07b74SJerome Glisse radeon_asic_reset(rdev); 1109b574f251SJerome Glisse radeon_fini(rdev); 1110b574f251SJerome Glisse radeon_agp_disable(rdev); 1111b574f251SJerome Glisse r = radeon_init(rdev); 11124aac0473SJerome Glisse if (r) 11134aac0473SJerome Glisse return r; 11143ce0a23dSJerome Glisse } 111560a7e396SChristian König if ((radeon_testing & 1)) { 1116ecc0b326SMichel Dänzer radeon_test_moves(rdev); 1117ecc0b326SMichel Dänzer } 111860a7e396SChristian König if ((radeon_testing & 2)) { 111960a7e396SChristian König radeon_test_syncing(rdev); 112060a7e396SChristian König } 1121771fe6b9SJerome Glisse if (radeon_benchmarking) { 1122638dd7dbSIlija Hadzic radeon_benchmark(rdev, radeon_benchmarking); 1123771fe6b9SJerome Glisse } 11246cf8a3f5SJerome Glisse return 0; 1125771fe6b9SJerome Glisse } 1126771fe6b9SJerome Glisse 11274d8bf9aeSChristian König static void radeon_debugfs_remove_files(struct radeon_device *rdev); 11284d8bf9aeSChristian König 11290c195119SAlex Deucher /** 11300c195119SAlex Deucher * radeon_device_fini - tear down the driver 11310c195119SAlex Deucher * 11320c195119SAlex Deucher * @rdev: radeon_device pointer 11330c195119SAlex Deucher * 11340c195119SAlex Deucher * Tear down the driver info (all asics). 11350c195119SAlex Deucher * Called at driver shutdown. 11360c195119SAlex Deucher */ 1137771fe6b9SJerome Glisse void radeon_device_fini(struct radeon_device *rdev) 1138771fe6b9SJerome Glisse { 1139771fe6b9SJerome Glisse DRM_INFO("radeon: finishing device.\n"); 1140771fe6b9SJerome Glisse rdev->shutdown = true; 114190aca4d2SJerome Glisse /* evict vram memory */ 114290aca4d2SJerome Glisse radeon_bo_evict_vram(rdev); 11433ce0a23dSJerome Glisse radeon_fini(rdev); 11446a9ee8afSDave Airlie vga_switcheroo_unregister_client(rdev->pdev); 1145c1176d6fSDave Airlie vga_client_register(rdev->pdev, NULL, NULL, NULL); 1146e0a2ca73SAlex Deucher if (rdev->rio_mem) 1147351a52a2SAlex Deucher pci_iounmap(rdev->pdev, rdev->rio_mem); 1148351a52a2SAlex Deucher rdev->rio_mem = NULL; 1149771fe6b9SJerome Glisse iounmap(rdev->rmmio); 1150771fe6b9SJerome Glisse rdev->rmmio = NULL; 11514d8bf9aeSChristian König radeon_debugfs_remove_files(rdev); 1152771fe6b9SJerome Glisse } 1153771fe6b9SJerome Glisse 1154771fe6b9SJerome Glisse 1155771fe6b9SJerome Glisse /* 1156771fe6b9SJerome Glisse * Suspend & resume. 1157771fe6b9SJerome Glisse */ 11580c195119SAlex Deucher /** 11590c195119SAlex Deucher * radeon_suspend_kms - initiate device suspend 11600c195119SAlex Deucher * 11610c195119SAlex Deucher * @pdev: drm dev pointer 11620c195119SAlex Deucher * @state: suspend state 11630c195119SAlex Deucher * 11640c195119SAlex Deucher * Puts the hw in the suspend state (all asics). 11650c195119SAlex Deucher * Returns 0 for success or an error on failure. 11660c195119SAlex Deucher * Called at driver suspend. 11670c195119SAlex Deucher */ 1168771fe6b9SJerome Glisse int radeon_suspend_kms(struct drm_device *dev, pm_message_t state) 1169771fe6b9SJerome Glisse { 1170875c1866SDarren Jenkins struct radeon_device *rdev; 1171771fe6b9SJerome Glisse struct drm_crtc *crtc; 1172d8dcaa1dSAlex Deucher struct drm_connector *connector; 11737465280cSAlex Deucher int i, r; 1174771fe6b9SJerome Glisse 1175875c1866SDarren Jenkins if (dev == NULL || dev->dev_private == NULL) { 1176771fe6b9SJerome Glisse return -ENODEV; 1177771fe6b9SJerome Glisse } 1178771fe6b9SJerome Glisse if (state.event == PM_EVENT_PRETHAW) { 1179771fe6b9SJerome Glisse return 0; 1180771fe6b9SJerome Glisse } 1181875c1866SDarren Jenkins rdev = dev->dev_private; 1182875c1866SDarren Jenkins 11835bcf719bSDave Airlie if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) 11846a9ee8afSDave Airlie return 0; 1185d8dcaa1dSAlex Deucher 118686698c20SSeth Forshee drm_kms_helper_poll_disable(dev); 118786698c20SSeth Forshee 1188d8dcaa1dSAlex Deucher /* turn off display hw */ 1189d8dcaa1dSAlex Deucher list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 1190d8dcaa1dSAlex Deucher drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF); 1191d8dcaa1dSAlex Deucher } 1192d8dcaa1dSAlex Deucher 1193771fe6b9SJerome Glisse /* unpin the front buffers */ 1194771fe6b9SJerome Glisse list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 1195771fe6b9SJerome Glisse struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->fb); 11964c788679SJerome Glisse struct radeon_bo *robj; 1197771fe6b9SJerome Glisse 1198771fe6b9SJerome Glisse if (rfb == NULL || rfb->obj == NULL) { 1199771fe6b9SJerome Glisse continue; 1200771fe6b9SJerome Glisse } 12017e4d15d9SDaniel Vetter robj = gem_to_radeon_bo(rfb->obj); 120238651674SDave Airlie /* don't unpin kernel fb objects */ 120338651674SDave Airlie if (!radeon_fbdev_robj_is_fb(rdev, robj)) { 12044c788679SJerome Glisse r = radeon_bo_reserve(robj, false); 120538651674SDave Airlie if (r == 0) { 12064c788679SJerome Glisse radeon_bo_unpin(robj); 12074c788679SJerome Glisse radeon_bo_unreserve(robj); 12084c788679SJerome Glisse } 1209771fe6b9SJerome Glisse } 1210771fe6b9SJerome Glisse } 1211771fe6b9SJerome Glisse /* evict vram memory */ 12124c788679SJerome Glisse radeon_bo_evict_vram(rdev); 12138a47cc9eSChristian König 12148a47cc9eSChristian König mutex_lock(&rdev->ring_lock); 1215771fe6b9SJerome Glisse /* wait for gpu to finish processing current batch */ 12167465280cSAlex Deucher for (i = 0; i < RADEON_NUM_RINGS; i++) 12178a47cc9eSChristian König radeon_fence_wait_empty_locked(rdev, i); 12188a47cc9eSChristian König mutex_unlock(&rdev->ring_lock); 1219771fe6b9SJerome Glisse 1220f657c2a7SYang Zhao radeon_save_bios_scratch_regs(rdev); 1221f657c2a7SYang Zhao 1222ce8f5370SAlex Deucher radeon_pm_suspend(rdev); 12233ce0a23dSJerome Glisse radeon_suspend(rdev); 1224d4877cf2SAlex Deucher radeon_hpd_fini(rdev); 1225771fe6b9SJerome Glisse /* evict remaining vram memory */ 12264c788679SJerome Glisse radeon_bo_evict_vram(rdev); 1227771fe6b9SJerome Glisse 122810b06122SJerome Glisse radeon_agp_suspend(rdev); 122910b06122SJerome Glisse 1230771fe6b9SJerome Glisse pci_save_state(dev->pdev); 1231771fe6b9SJerome Glisse if (state.event == PM_EVENT_SUSPEND) { 1232771fe6b9SJerome Glisse /* Shut down the device */ 1233771fe6b9SJerome Glisse pci_disable_device(dev->pdev); 1234771fe6b9SJerome Glisse pci_set_power_state(dev->pdev, PCI_D3hot); 1235771fe6b9SJerome Glisse } 1236ac751efaSTorben Hohn console_lock(); 123738651674SDave Airlie radeon_fbdev_set_suspend(rdev, 1); 1238ac751efaSTorben Hohn console_unlock(); 1239771fe6b9SJerome Glisse return 0; 1240771fe6b9SJerome Glisse } 1241771fe6b9SJerome Glisse 12420c195119SAlex Deucher /** 12430c195119SAlex Deucher * radeon_resume_kms - initiate device resume 12440c195119SAlex Deucher * 12450c195119SAlex Deucher * @pdev: drm dev pointer 12460c195119SAlex Deucher * 12470c195119SAlex Deucher * Bring the hw back to operating state (all asics). 12480c195119SAlex Deucher * Returns 0 for success or an error on failure. 12490c195119SAlex Deucher * Called at driver resume. 12500c195119SAlex Deucher */ 1251771fe6b9SJerome Glisse int radeon_resume_kms(struct drm_device *dev) 1252771fe6b9SJerome Glisse { 125309bdf591SCedric Godin struct drm_connector *connector; 1254771fe6b9SJerome Glisse struct radeon_device *rdev = dev->dev_private; 125504eb2206SChristian König int r; 1256771fe6b9SJerome Glisse 12575bcf719bSDave Airlie if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) 12586a9ee8afSDave Airlie return 0; 12596a9ee8afSDave Airlie 1260ac751efaSTorben Hohn console_lock(); 1261771fe6b9SJerome Glisse pci_set_power_state(dev->pdev, PCI_D0); 1262771fe6b9SJerome Glisse pci_restore_state(dev->pdev); 1263771fe6b9SJerome Glisse if (pci_enable_device(dev->pdev)) { 1264ac751efaSTorben Hohn console_unlock(); 1265771fe6b9SJerome Glisse return -1; 1266771fe6b9SJerome Glisse } 12670ebf1717SDave Airlie /* resume AGP if in use */ 12680ebf1717SDave Airlie radeon_agp_resume(rdev); 12693ce0a23dSJerome Glisse radeon_resume(rdev); 127004eb2206SChristian König 127104eb2206SChristian König r = radeon_ib_ring_tests(rdev); 127204eb2206SChristian König if (r) 127304eb2206SChristian König DRM_ERROR("ib ring test failed (%d).\n", r); 127404eb2206SChristian König 1275ce8f5370SAlex Deucher radeon_pm_resume(rdev); 1276f657c2a7SYang Zhao radeon_restore_bios_scratch_regs(rdev); 127709bdf591SCedric Godin 127838651674SDave Airlie radeon_fbdev_set_suspend(rdev, 0); 1279ac751efaSTorben Hohn console_unlock(); 1280771fe6b9SJerome Glisse 12813fa47d9eSAlex Deucher /* init dig PHYs, disp eng pll */ 12823fa47d9eSAlex Deucher if (rdev->is_atom_bios) { 1283ac89af1eSAlex Deucher radeon_atom_encoder_init(rdev); 1284f3f1f03eSAlex Deucher radeon_atom_disp_eng_pll_init(rdev); 1285*bced76f2SAlex Deucher /* turn on the BL */ 1286*bced76f2SAlex Deucher if (rdev->mode_info.bl_encoder) { 1287*bced76f2SAlex Deucher u8 bl_level = radeon_get_backlight_level(rdev, 1288*bced76f2SAlex Deucher rdev->mode_info.bl_encoder); 1289*bced76f2SAlex Deucher radeon_set_backlight_level(rdev, rdev->mode_info.bl_encoder, 1290*bced76f2SAlex Deucher bl_level); 1291*bced76f2SAlex Deucher } 12923fa47d9eSAlex Deucher } 1293d4877cf2SAlex Deucher /* reset hpd state */ 1294d4877cf2SAlex Deucher radeon_hpd_init(rdev); 1295771fe6b9SJerome Glisse /* blat the mode back in */ 1296771fe6b9SJerome Glisse drm_helper_resume_force_mode(dev); 1297a93f344dSAlex Deucher /* turn on display hw */ 1298a93f344dSAlex Deucher list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 1299a93f344dSAlex Deucher drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON); 1300a93f344dSAlex Deucher } 130186698c20SSeth Forshee 130286698c20SSeth Forshee drm_kms_helper_poll_enable(dev); 1303771fe6b9SJerome Glisse return 0; 1304771fe6b9SJerome Glisse } 1305771fe6b9SJerome Glisse 13060c195119SAlex Deucher /** 13070c195119SAlex Deucher * radeon_gpu_reset - reset the asic 13080c195119SAlex Deucher * 13090c195119SAlex Deucher * @rdev: radeon device pointer 13100c195119SAlex Deucher * 13110c195119SAlex Deucher * Attempt the reset the GPU if it has hung (all asics). 13120c195119SAlex Deucher * Returns 0 for success or an error on failure. 13130c195119SAlex Deucher */ 131490aca4d2SJerome Glisse int radeon_gpu_reset(struct radeon_device *rdev) 131590aca4d2SJerome Glisse { 131655d7c221SChristian König unsigned ring_sizes[RADEON_NUM_RINGS]; 131755d7c221SChristian König uint32_t *ring_data[RADEON_NUM_RINGS]; 131855d7c221SChristian König 131955d7c221SChristian König bool saved = false; 132055d7c221SChristian König 132155d7c221SChristian König int i, r; 13228fd1b84cSDave Airlie int resched; 132390aca4d2SJerome Glisse 1324dee53e7fSJerome Glisse down_write(&rdev->exclusive_lock); 132590aca4d2SJerome Glisse radeon_save_bios_scratch_regs(rdev); 13268fd1b84cSDave Airlie /* block TTM */ 13278fd1b84cSDave Airlie resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev); 132890aca4d2SJerome Glisse radeon_suspend(rdev); 132990aca4d2SJerome Glisse 133055d7c221SChristian König for (i = 0; i < RADEON_NUM_RINGS; ++i) { 133155d7c221SChristian König ring_sizes[i] = radeon_ring_backup(rdev, &rdev->ring[i], 133255d7c221SChristian König &ring_data[i]); 133355d7c221SChristian König if (ring_sizes[i]) { 133455d7c221SChristian König saved = true; 133555d7c221SChristian König dev_info(rdev->dev, "Saved %d dwords of commands " 133655d7c221SChristian König "on ring %d.\n", ring_sizes[i], i); 133755d7c221SChristian König } 133855d7c221SChristian König } 133955d7c221SChristian König 134055d7c221SChristian König retry: 134190aca4d2SJerome Glisse r = radeon_asic_reset(rdev); 134290aca4d2SJerome Glisse if (!r) { 134355d7c221SChristian König dev_info(rdev->dev, "GPU reset succeeded, trying to resume\n"); 134490aca4d2SJerome Glisse radeon_resume(rdev); 134555d7c221SChristian König } 134604eb2206SChristian König 134790aca4d2SJerome Glisse radeon_restore_bios_scratch_regs(rdev); 134890aca4d2SJerome Glisse drm_helper_resume_force_mode(rdev->ddev); 134955d7c221SChristian König 135055d7c221SChristian König if (!r) { 135155d7c221SChristian König for (i = 0; i < RADEON_NUM_RINGS; ++i) { 135255d7c221SChristian König radeon_ring_restore(rdev, &rdev->ring[i], 135355d7c221SChristian König ring_sizes[i], ring_data[i]); 1354f54b350dSChristian König ring_sizes[i] = 0; 1355f54b350dSChristian König ring_data[i] = NULL; 135690aca4d2SJerome Glisse } 13577a1619b9SMichel Dänzer 135855d7c221SChristian König r = radeon_ib_ring_tests(rdev); 135955d7c221SChristian König if (r) { 136055d7c221SChristian König dev_err(rdev->dev, "ib ring test failed (%d).\n", r); 136155d7c221SChristian König if (saved) { 1362f54b350dSChristian König saved = false; 136355d7c221SChristian König radeon_suspend(rdev); 136455d7c221SChristian König goto retry; 136555d7c221SChristian König } 136655d7c221SChristian König } 136755d7c221SChristian König } else { 136855d7c221SChristian König for (i = 0; i < RADEON_NUM_RINGS; ++i) { 136955d7c221SChristian König kfree(ring_data[i]); 137055d7c221SChristian König } 137155d7c221SChristian König } 137255d7c221SChristian König 137355d7c221SChristian König ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched); 13747a1619b9SMichel Dänzer if (r) { 137590aca4d2SJerome Glisse /* bad news, how to tell it to userspace ? */ 137690aca4d2SJerome Glisse dev_info(rdev->dev, "GPU reset failed\n"); 13777a1619b9SMichel Dänzer } 13787a1619b9SMichel Dänzer 1379dee53e7fSJerome Glisse up_write(&rdev->exclusive_lock); 138090aca4d2SJerome Glisse return r; 138190aca4d2SJerome Glisse } 138290aca4d2SJerome Glisse 1383771fe6b9SJerome Glisse 1384771fe6b9SJerome Glisse /* 1385771fe6b9SJerome Glisse * Debugfs 1386771fe6b9SJerome Glisse */ 1387771fe6b9SJerome Glisse int radeon_debugfs_add_files(struct radeon_device *rdev, 1388771fe6b9SJerome Glisse struct drm_info_list *files, 1389771fe6b9SJerome Glisse unsigned nfiles) 1390771fe6b9SJerome Glisse { 1391771fe6b9SJerome Glisse unsigned i; 1392771fe6b9SJerome Glisse 13934d8bf9aeSChristian König for (i = 0; i < rdev->debugfs_count; i++) { 13944d8bf9aeSChristian König if (rdev->debugfs[i].files == files) { 1395771fe6b9SJerome Glisse /* Already registered */ 1396771fe6b9SJerome Glisse return 0; 1397771fe6b9SJerome Glisse } 1398771fe6b9SJerome Glisse } 1399c245cb9eSMichael Witten 14004d8bf9aeSChristian König i = rdev->debugfs_count + 1; 1401c245cb9eSMichael Witten if (i > RADEON_DEBUGFS_MAX_COMPONENTS) { 1402c245cb9eSMichael Witten DRM_ERROR("Reached maximum number of debugfs components.\n"); 1403c245cb9eSMichael Witten DRM_ERROR("Report so we increase " 1404c245cb9eSMichael Witten "RADEON_DEBUGFS_MAX_COMPONENTS.\n"); 1405771fe6b9SJerome Glisse return -EINVAL; 1406771fe6b9SJerome Glisse } 14074d8bf9aeSChristian König rdev->debugfs[rdev->debugfs_count].files = files; 14084d8bf9aeSChristian König rdev->debugfs[rdev->debugfs_count].num_files = nfiles; 14094d8bf9aeSChristian König rdev->debugfs_count = i; 1410771fe6b9SJerome Glisse #if defined(CONFIG_DEBUG_FS) 1411771fe6b9SJerome Glisse drm_debugfs_create_files(files, nfiles, 1412771fe6b9SJerome Glisse rdev->ddev->control->debugfs_root, 1413771fe6b9SJerome Glisse rdev->ddev->control); 1414771fe6b9SJerome Glisse drm_debugfs_create_files(files, nfiles, 1415771fe6b9SJerome Glisse rdev->ddev->primary->debugfs_root, 1416771fe6b9SJerome Glisse rdev->ddev->primary); 1417771fe6b9SJerome Glisse #endif 1418771fe6b9SJerome Glisse return 0; 1419771fe6b9SJerome Glisse } 1420771fe6b9SJerome Glisse 14214d8bf9aeSChristian König static void radeon_debugfs_remove_files(struct radeon_device *rdev) 14224d8bf9aeSChristian König { 14234d8bf9aeSChristian König #if defined(CONFIG_DEBUG_FS) 14244d8bf9aeSChristian König unsigned i; 14254d8bf9aeSChristian König 14264d8bf9aeSChristian König for (i = 0; i < rdev->debugfs_count; i++) { 14274d8bf9aeSChristian König drm_debugfs_remove_files(rdev->debugfs[i].files, 14284d8bf9aeSChristian König rdev->debugfs[i].num_files, 14294d8bf9aeSChristian König rdev->ddev->control); 14304d8bf9aeSChristian König drm_debugfs_remove_files(rdev->debugfs[i].files, 14314d8bf9aeSChristian König rdev->debugfs[i].num_files, 14324d8bf9aeSChristian König rdev->ddev->primary); 14334d8bf9aeSChristian König } 14344d8bf9aeSChristian König #endif 14354d8bf9aeSChristian König } 14364d8bf9aeSChristian König 1437771fe6b9SJerome Glisse #if defined(CONFIG_DEBUG_FS) 1438771fe6b9SJerome Glisse int radeon_debugfs_init(struct drm_minor *minor) 1439771fe6b9SJerome Glisse { 1440771fe6b9SJerome Glisse return 0; 1441771fe6b9SJerome Glisse } 1442771fe6b9SJerome Glisse 1443771fe6b9SJerome Glisse void radeon_debugfs_cleanup(struct drm_minor *minor) 1444771fe6b9SJerome Glisse { 1445771fe6b9SJerome Glisse } 1446771fe6b9SJerome Glisse #endif 1447