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", 921b5331d9SJerome Glisse "LAST", 931b5331d9SJerome Glisse }; 941b5331d9SJerome Glisse 95771fe6b9SJerome Glisse /* 96b1e3a6d1SMichel Dänzer * Clear GPU surface registers. 97b1e3a6d1SMichel Dänzer */ 983ce0a23dSJerome Glisse void radeon_surface_init(struct radeon_device *rdev) 99b1e3a6d1SMichel Dänzer { 100b1e3a6d1SMichel Dänzer /* FIXME: check this out */ 101b1e3a6d1SMichel Dänzer if (rdev->family < CHIP_R600) { 102b1e3a6d1SMichel Dänzer int i; 103b1e3a6d1SMichel Dänzer 104550e2d92SDave Airlie for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) { 105550e2d92SDave Airlie if (rdev->surface_regs[i].bo) 106550e2d92SDave Airlie radeon_bo_get_surface_reg(rdev->surface_regs[i].bo); 107550e2d92SDave Airlie else 108550e2d92SDave Airlie radeon_clear_surface_reg(rdev, i); 109b1e3a6d1SMichel Dänzer } 110e024e110SDave Airlie /* enable surfaces */ 111e024e110SDave Airlie WREG32(RADEON_SURFACE_CNTL, 0); 112b1e3a6d1SMichel Dänzer } 113b1e3a6d1SMichel Dänzer } 114b1e3a6d1SMichel Dänzer 115b1e3a6d1SMichel Dänzer /* 116771fe6b9SJerome Glisse * GPU scratch registers helpers function. 117771fe6b9SJerome Glisse */ 1183ce0a23dSJerome Glisse void radeon_scratch_init(struct radeon_device *rdev) 119771fe6b9SJerome Glisse { 120771fe6b9SJerome Glisse int i; 121771fe6b9SJerome Glisse 122771fe6b9SJerome Glisse /* FIXME: check this out */ 123771fe6b9SJerome Glisse if (rdev->family < CHIP_R300) { 124771fe6b9SJerome Glisse rdev->scratch.num_reg = 5; 125771fe6b9SJerome Glisse } else { 126771fe6b9SJerome Glisse rdev->scratch.num_reg = 7; 127771fe6b9SJerome Glisse } 128724c80e1SAlex Deucher rdev->scratch.reg_base = RADEON_SCRATCH_REG0; 129771fe6b9SJerome Glisse for (i = 0; i < rdev->scratch.num_reg; i++) { 130771fe6b9SJerome Glisse rdev->scratch.free[i] = true; 131724c80e1SAlex Deucher rdev->scratch.reg[i] = rdev->scratch.reg_base + (i * 4); 132771fe6b9SJerome Glisse } 133771fe6b9SJerome Glisse } 134771fe6b9SJerome Glisse 135771fe6b9SJerome Glisse int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg) 136771fe6b9SJerome Glisse { 137771fe6b9SJerome Glisse int i; 138771fe6b9SJerome Glisse 139771fe6b9SJerome Glisse for (i = 0; i < rdev->scratch.num_reg; i++) { 140771fe6b9SJerome Glisse if (rdev->scratch.free[i]) { 141771fe6b9SJerome Glisse rdev->scratch.free[i] = false; 142771fe6b9SJerome Glisse *reg = rdev->scratch.reg[i]; 143771fe6b9SJerome Glisse return 0; 144771fe6b9SJerome Glisse } 145771fe6b9SJerome Glisse } 146771fe6b9SJerome Glisse return -EINVAL; 147771fe6b9SJerome Glisse } 148771fe6b9SJerome Glisse 149771fe6b9SJerome Glisse void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg) 150771fe6b9SJerome Glisse { 151771fe6b9SJerome Glisse int i; 152771fe6b9SJerome Glisse 153771fe6b9SJerome Glisse for (i = 0; i < rdev->scratch.num_reg; i++) { 154771fe6b9SJerome Glisse if (rdev->scratch.reg[i] == reg) { 155771fe6b9SJerome Glisse rdev->scratch.free[i] = true; 156771fe6b9SJerome Glisse return; 157771fe6b9SJerome Glisse } 158771fe6b9SJerome Glisse } 159771fe6b9SJerome Glisse } 160771fe6b9SJerome Glisse 161724c80e1SAlex Deucher void radeon_wb_disable(struct radeon_device *rdev) 162724c80e1SAlex Deucher { 163724c80e1SAlex Deucher int r; 164724c80e1SAlex Deucher 165724c80e1SAlex Deucher if (rdev->wb.wb_obj) { 166724c80e1SAlex Deucher r = radeon_bo_reserve(rdev->wb.wb_obj, false); 167724c80e1SAlex Deucher if (unlikely(r != 0)) 168724c80e1SAlex Deucher return; 169724c80e1SAlex Deucher radeon_bo_kunmap(rdev->wb.wb_obj); 170724c80e1SAlex Deucher radeon_bo_unpin(rdev->wb.wb_obj); 171724c80e1SAlex Deucher radeon_bo_unreserve(rdev->wb.wb_obj); 172724c80e1SAlex Deucher } 173724c80e1SAlex Deucher rdev->wb.enabled = false; 174724c80e1SAlex Deucher } 175724c80e1SAlex Deucher 176724c80e1SAlex Deucher void radeon_wb_fini(struct radeon_device *rdev) 177724c80e1SAlex Deucher { 178724c80e1SAlex Deucher radeon_wb_disable(rdev); 179724c80e1SAlex Deucher if (rdev->wb.wb_obj) { 180724c80e1SAlex Deucher radeon_bo_unref(&rdev->wb.wb_obj); 181724c80e1SAlex Deucher rdev->wb.wb = NULL; 182724c80e1SAlex Deucher rdev->wb.wb_obj = NULL; 183724c80e1SAlex Deucher } 184724c80e1SAlex Deucher } 185724c80e1SAlex Deucher 186724c80e1SAlex Deucher int radeon_wb_init(struct radeon_device *rdev) 187724c80e1SAlex Deucher { 188724c80e1SAlex Deucher int r; 189724c80e1SAlex Deucher 190724c80e1SAlex Deucher if (rdev->wb.wb_obj == NULL) { 191441921d5SDaniel Vetter r = radeon_bo_create(rdev, RADEON_GPU_PAGE_SIZE, PAGE_SIZE, true, 192724c80e1SAlex Deucher RADEON_GEM_DOMAIN_GTT, &rdev->wb.wb_obj); 193724c80e1SAlex Deucher if (r) { 194724c80e1SAlex Deucher dev_warn(rdev->dev, "(%d) create WB bo failed\n", r); 195724c80e1SAlex Deucher return r; 196724c80e1SAlex Deucher } 197724c80e1SAlex Deucher } 198724c80e1SAlex Deucher r = radeon_bo_reserve(rdev->wb.wb_obj, false); 199724c80e1SAlex Deucher if (unlikely(r != 0)) { 200724c80e1SAlex Deucher radeon_wb_fini(rdev); 201724c80e1SAlex Deucher return r; 202724c80e1SAlex Deucher } 203724c80e1SAlex Deucher r = radeon_bo_pin(rdev->wb.wb_obj, RADEON_GEM_DOMAIN_GTT, 204724c80e1SAlex Deucher &rdev->wb.gpu_addr); 205724c80e1SAlex Deucher if (r) { 206724c80e1SAlex Deucher radeon_bo_unreserve(rdev->wb.wb_obj); 207724c80e1SAlex Deucher dev_warn(rdev->dev, "(%d) pin WB bo failed\n", r); 208724c80e1SAlex Deucher radeon_wb_fini(rdev); 209724c80e1SAlex Deucher return r; 210724c80e1SAlex Deucher } 211724c80e1SAlex Deucher r = radeon_bo_kmap(rdev->wb.wb_obj, (void **)&rdev->wb.wb); 212724c80e1SAlex Deucher radeon_bo_unreserve(rdev->wb.wb_obj); 213724c80e1SAlex Deucher if (r) { 214724c80e1SAlex Deucher dev_warn(rdev->dev, "(%d) map WB bo failed\n", r); 215724c80e1SAlex Deucher radeon_wb_fini(rdev); 216724c80e1SAlex Deucher return r; 217724c80e1SAlex Deucher } 218724c80e1SAlex Deucher 219e6ba7599SAlex Deucher /* clear wb memory */ 220e6ba7599SAlex Deucher memset((char *)rdev->wb.wb, 0, RADEON_GPU_PAGE_SIZE); 221d0f8a854SAlex Deucher /* disable event_write fences */ 222d0f8a854SAlex Deucher rdev->wb.use_event = false; 223724c80e1SAlex Deucher /* disabled via module param */ 224724c80e1SAlex Deucher if (radeon_no_wb == 1) 225724c80e1SAlex Deucher rdev->wb.enabled = false; 226724c80e1SAlex Deucher else { 227724c80e1SAlex Deucher /* often unreliable on AGP */ 228724c80e1SAlex Deucher if (rdev->flags & RADEON_IS_AGP) { 229724c80e1SAlex Deucher rdev->wb.enabled = false; 230d0f8a854SAlex Deucher } else { 231724c80e1SAlex Deucher rdev->wb.enabled = true; 232d0f8a854SAlex Deucher /* event_write fences are only available on r600+ */ 233d0f8a854SAlex Deucher if (rdev->family >= CHIP_R600) 234d0f8a854SAlex Deucher rdev->wb.use_event = true; 235d0f8a854SAlex Deucher } 236724c80e1SAlex Deucher } 2377d52785dSAlex Deucher /* always use writeback/events on NI */ 2387d52785dSAlex Deucher if (ASIC_IS_DCE5(rdev)) { 2397d52785dSAlex Deucher rdev->wb.enabled = true; 2407d52785dSAlex Deucher rdev->wb.use_event = true; 2417d52785dSAlex Deucher } 242724c80e1SAlex Deucher 243724c80e1SAlex Deucher dev_info(rdev->dev, "WB %sabled\n", rdev->wb.enabled ? "en" : "dis"); 244724c80e1SAlex Deucher 245724c80e1SAlex Deucher return 0; 246724c80e1SAlex Deucher } 247724c80e1SAlex Deucher 248d594e46aSJerome Glisse /** 249d594e46aSJerome Glisse * radeon_vram_location - try to find VRAM location 250d594e46aSJerome Glisse * @rdev: radeon device structure holding all necessary informations 251d594e46aSJerome Glisse * @mc: memory controller structure holding memory informations 252d594e46aSJerome Glisse * @base: base address at which to put VRAM 253d594e46aSJerome Glisse * 254d594e46aSJerome Glisse * Function will place try to place VRAM at base address provided 255d594e46aSJerome Glisse * as parameter (which is so far either PCI aperture address or 256d594e46aSJerome Glisse * for IGP TOM base address). 257d594e46aSJerome Glisse * 258d594e46aSJerome Glisse * If there is not enough space to fit the unvisible VRAM in the 32bits 259d594e46aSJerome Glisse * address space then we limit the VRAM size to the aperture. 260d594e46aSJerome Glisse * 261d594e46aSJerome Glisse * If we are using AGP and if the AGP aperture doesn't allow us to have 262d594e46aSJerome Glisse * room for all the VRAM than we restrict the VRAM to the PCI aperture 263d594e46aSJerome Glisse * size and print a warning. 264d594e46aSJerome Glisse * 265d594e46aSJerome Glisse * This function will never fails, worst case are limiting VRAM. 266d594e46aSJerome Glisse * 267d594e46aSJerome Glisse * Note: GTT start, end, size should be initialized before calling this 268d594e46aSJerome Glisse * function on AGP platform. 269d594e46aSJerome Glisse * 27025985edcSLucas De Marchi * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size, 271d594e46aSJerome Glisse * this shouldn't be a problem as we are using the PCI aperture as a reference. 272d594e46aSJerome Glisse * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but 273d594e46aSJerome Glisse * not IGP. 274d594e46aSJerome Glisse * 275d594e46aSJerome Glisse * Note: we use mc_vram_size as on some board we need to program the mc to 276d594e46aSJerome Glisse * cover the whole aperture even if VRAM size is inferior to aperture size 277d594e46aSJerome Glisse * Novell bug 204882 + along with lots of ubuntu ones 278d594e46aSJerome Glisse * 279d594e46aSJerome Glisse * Note: when limiting vram it's safe to overwritte real_vram_size because 280d594e46aSJerome Glisse * we are not in case where real_vram_size is inferior to mc_vram_size (ie 281d594e46aSJerome Glisse * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu 282d594e46aSJerome Glisse * ones) 283d594e46aSJerome Glisse * 284d594e46aSJerome Glisse * Note: IGP TOM addr should be the same as the aperture addr, we don't 285d594e46aSJerome Glisse * explicitly check for that thought. 286d594e46aSJerome Glisse * 287d594e46aSJerome Glisse * FIXME: when reducing VRAM size align new size on power of 2. 288771fe6b9SJerome Glisse */ 289d594e46aSJerome Glisse void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base) 290771fe6b9SJerome Glisse { 291d594e46aSJerome Glisse mc->vram_start = base; 292d594e46aSJerome Glisse if (mc->mc_vram_size > (0xFFFFFFFF - base + 1)) { 293d594e46aSJerome Glisse dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n"); 294d594e46aSJerome Glisse mc->real_vram_size = mc->aper_size; 295d594e46aSJerome Glisse mc->mc_vram_size = mc->aper_size; 296771fe6b9SJerome Glisse } 297d594e46aSJerome Glisse mc->vram_end = mc->vram_start + mc->mc_vram_size - 1; 2982cbeb4efSJerome Glisse if (rdev->flags & RADEON_IS_AGP && mc->vram_end > mc->gtt_start && mc->vram_start <= mc->gtt_end) { 299d594e46aSJerome Glisse dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n"); 300d594e46aSJerome Glisse mc->real_vram_size = mc->aper_size; 301d594e46aSJerome Glisse mc->mc_vram_size = mc->aper_size; 302771fe6b9SJerome Glisse } 303d594e46aSJerome Glisse mc->vram_end = mc->vram_start + mc->mc_vram_size - 1; 304ba95c45aSMichel Dänzer if (radeon_vram_limit && radeon_vram_limit < mc->real_vram_size) 305ba95c45aSMichel Dänzer mc->real_vram_size = radeon_vram_limit; 306dd7cc55aSAlex Deucher dev_info(rdev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n", 307d594e46aSJerome Glisse mc->mc_vram_size >> 20, mc->vram_start, 308d594e46aSJerome Glisse mc->vram_end, mc->real_vram_size >> 20); 309771fe6b9SJerome Glisse } 310771fe6b9SJerome Glisse 311d594e46aSJerome Glisse /** 312d594e46aSJerome Glisse * radeon_gtt_location - try to find GTT location 313d594e46aSJerome Glisse * @rdev: radeon device structure holding all necessary informations 314d594e46aSJerome Glisse * @mc: memory controller structure holding memory informations 315d594e46aSJerome Glisse * 316d594e46aSJerome Glisse * Function will place try to place GTT before or after VRAM. 317d594e46aSJerome Glisse * 318d594e46aSJerome Glisse * If GTT size is bigger than space left then we ajust GTT size. 319d594e46aSJerome Glisse * Thus function will never fails. 320d594e46aSJerome Glisse * 321d594e46aSJerome Glisse * FIXME: when reducing GTT size align new size on power of 2. 322d594e46aSJerome Glisse */ 323d594e46aSJerome Glisse void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc) 324d594e46aSJerome Glisse { 325d594e46aSJerome Glisse u64 size_af, size_bf; 326d594e46aSJerome Glisse 3278d369bb1SAlex Deucher size_af = ((0xFFFFFFFF - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align; 3288d369bb1SAlex Deucher size_bf = mc->vram_start & ~mc->gtt_base_align; 329d594e46aSJerome Glisse if (size_bf > size_af) { 330d594e46aSJerome Glisse if (mc->gtt_size > size_bf) { 331d594e46aSJerome Glisse dev_warn(rdev->dev, "limiting GTT\n"); 332d594e46aSJerome Glisse mc->gtt_size = size_bf; 333d594e46aSJerome Glisse } 3348d369bb1SAlex Deucher mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size; 335d594e46aSJerome Glisse } else { 336d594e46aSJerome Glisse if (mc->gtt_size > size_af) { 337d594e46aSJerome Glisse dev_warn(rdev->dev, "limiting GTT\n"); 338d594e46aSJerome Glisse mc->gtt_size = size_af; 339d594e46aSJerome Glisse } 3408d369bb1SAlex Deucher mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align; 341d594e46aSJerome Glisse } 342d594e46aSJerome Glisse mc->gtt_end = mc->gtt_start + mc->gtt_size - 1; 343dd7cc55aSAlex Deucher dev_info(rdev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n", 344d594e46aSJerome Glisse mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end); 345d594e46aSJerome Glisse } 346771fe6b9SJerome Glisse 347771fe6b9SJerome Glisse /* 348771fe6b9SJerome Glisse * GPU helpers function. 349771fe6b9SJerome Glisse */ 3509f022ddfSJerome Glisse bool radeon_card_posted(struct radeon_device *rdev) 351771fe6b9SJerome Glisse { 352771fe6b9SJerome Glisse uint32_t reg; 353771fe6b9SJerome Glisse 354bcc65fd8SMatthew Garrett if (efi_enabled && rdev->pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE) 355bcc65fd8SMatthew Garrett return false; 356bcc65fd8SMatthew Garrett 357771fe6b9SJerome Glisse /* first check CRTCs */ 35818007401SAlex Deucher if (ASIC_IS_DCE41(rdev)) { 35918007401SAlex Deucher reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) | 36018007401SAlex Deucher RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET); 36118007401SAlex Deucher if (reg & EVERGREEN_CRTC_MASTER_EN) 36218007401SAlex Deucher return true; 36318007401SAlex Deucher } else if (ASIC_IS_DCE4(rdev)) { 364bcc1c2a1SAlex Deucher reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) | 365bcc1c2a1SAlex Deucher RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET) | 366bcc1c2a1SAlex Deucher RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) | 367bcc1c2a1SAlex Deucher RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET) | 368bcc1c2a1SAlex Deucher RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) | 369bcc1c2a1SAlex Deucher RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET); 370bcc1c2a1SAlex Deucher if (reg & EVERGREEN_CRTC_MASTER_EN) 371bcc1c2a1SAlex Deucher return true; 372bcc1c2a1SAlex Deucher } else if (ASIC_IS_AVIVO(rdev)) { 373771fe6b9SJerome Glisse reg = RREG32(AVIVO_D1CRTC_CONTROL) | 374771fe6b9SJerome Glisse RREG32(AVIVO_D2CRTC_CONTROL); 375771fe6b9SJerome Glisse if (reg & AVIVO_CRTC_EN) { 376771fe6b9SJerome Glisse return true; 377771fe6b9SJerome Glisse } 378771fe6b9SJerome Glisse } else { 379771fe6b9SJerome Glisse reg = RREG32(RADEON_CRTC_GEN_CNTL) | 380771fe6b9SJerome Glisse RREG32(RADEON_CRTC2_GEN_CNTL); 381771fe6b9SJerome Glisse if (reg & RADEON_CRTC_EN) { 382771fe6b9SJerome Glisse return true; 383771fe6b9SJerome Glisse } 384771fe6b9SJerome Glisse } 385771fe6b9SJerome Glisse 386771fe6b9SJerome Glisse /* then check MEM_SIZE, in case the crtcs are off */ 387771fe6b9SJerome Glisse if (rdev->family >= CHIP_R600) 388771fe6b9SJerome Glisse reg = RREG32(R600_CONFIG_MEMSIZE); 389771fe6b9SJerome Glisse else 390771fe6b9SJerome Glisse reg = RREG32(RADEON_CONFIG_MEMSIZE); 391771fe6b9SJerome Glisse 392771fe6b9SJerome Glisse if (reg) 393771fe6b9SJerome Glisse return true; 394771fe6b9SJerome Glisse 395771fe6b9SJerome Glisse return false; 396771fe6b9SJerome Glisse 397771fe6b9SJerome Glisse } 398771fe6b9SJerome Glisse 399f47299c5SAlex Deucher void radeon_update_bandwidth_info(struct radeon_device *rdev) 400f47299c5SAlex Deucher { 401f47299c5SAlex Deucher fixed20_12 a; 4028807286eSAlex Deucher u32 sclk = rdev->pm.current_sclk; 4038807286eSAlex Deucher u32 mclk = rdev->pm.current_mclk; 404f47299c5SAlex Deucher 4058807286eSAlex Deucher /* sclk/mclk in Mhz */ 40668adac5eSBen Skeggs a.full = dfixed_const(100); 40768adac5eSBen Skeggs rdev->pm.sclk.full = dfixed_const(sclk); 40868adac5eSBen Skeggs rdev->pm.sclk.full = dfixed_div(rdev->pm.sclk, a); 40968adac5eSBen Skeggs rdev->pm.mclk.full = dfixed_const(mclk); 41068adac5eSBen Skeggs rdev->pm.mclk.full = dfixed_div(rdev->pm.mclk, a); 411f47299c5SAlex Deucher 4128807286eSAlex Deucher if (rdev->flags & RADEON_IS_IGP) { 41368adac5eSBen Skeggs a.full = dfixed_const(16); 414f47299c5SAlex Deucher /* core_bandwidth = sclk(Mhz) * 16 */ 41568adac5eSBen Skeggs rdev->pm.core_bandwidth.full = dfixed_div(rdev->pm.sclk, a); 416f47299c5SAlex Deucher } 417f47299c5SAlex Deucher } 418f47299c5SAlex Deucher 41972542d77SDave Airlie bool radeon_boot_test_post_card(struct radeon_device *rdev) 42072542d77SDave Airlie { 42172542d77SDave Airlie if (radeon_card_posted(rdev)) 42272542d77SDave Airlie return true; 42372542d77SDave Airlie 42472542d77SDave Airlie if (rdev->bios) { 42572542d77SDave Airlie DRM_INFO("GPU not posted. posting now...\n"); 42672542d77SDave Airlie if (rdev->is_atom_bios) 42772542d77SDave Airlie atom_asic_init(rdev->mode_info.atom_context); 42872542d77SDave Airlie else 42972542d77SDave Airlie radeon_combios_asic_init(rdev->ddev); 43072542d77SDave Airlie return true; 43172542d77SDave Airlie } else { 43272542d77SDave Airlie dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n"); 43372542d77SDave Airlie return false; 43472542d77SDave Airlie } 43572542d77SDave Airlie } 43672542d77SDave Airlie 4373ce0a23dSJerome Glisse int radeon_dummy_page_init(struct radeon_device *rdev) 4383ce0a23dSJerome Glisse { 43982568565SDave Airlie if (rdev->dummy_page.page) 44082568565SDave Airlie return 0; 4413ce0a23dSJerome Glisse rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO); 4423ce0a23dSJerome Glisse if (rdev->dummy_page.page == NULL) 4433ce0a23dSJerome Glisse return -ENOMEM; 4443ce0a23dSJerome Glisse rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page, 4453ce0a23dSJerome Glisse 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); 446a30f6fb7SBenjamin Herrenschmidt if (pci_dma_mapping_error(rdev->pdev, rdev->dummy_page.addr)) { 447a30f6fb7SBenjamin Herrenschmidt dev_err(&rdev->pdev->dev, "Failed to DMA MAP the dummy page\n"); 4483ce0a23dSJerome Glisse __free_page(rdev->dummy_page.page); 4493ce0a23dSJerome Glisse rdev->dummy_page.page = NULL; 4503ce0a23dSJerome Glisse return -ENOMEM; 4513ce0a23dSJerome Glisse } 4523ce0a23dSJerome Glisse return 0; 4533ce0a23dSJerome Glisse } 4543ce0a23dSJerome Glisse 4553ce0a23dSJerome Glisse void radeon_dummy_page_fini(struct radeon_device *rdev) 4563ce0a23dSJerome Glisse { 4573ce0a23dSJerome Glisse if (rdev->dummy_page.page == NULL) 4583ce0a23dSJerome Glisse return; 4593ce0a23dSJerome Glisse pci_unmap_page(rdev->pdev, rdev->dummy_page.addr, 4603ce0a23dSJerome Glisse PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); 4613ce0a23dSJerome Glisse __free_page(rdev->dummy_page.page); 4623ce0a23dSJerome Glisse rdev->dummy_page.page = NULL; 4633ce0a23dSJerome Glisse } 4643ce0a23dSJerome Glisse 465771fe6b9SJerome Glisse 466771fe6b9SJerome Glisse /* ATOM accessor methods */ 467771fe6b9SJerome Glisse static uint32_t cail_pll_read(struct card_info *info, uint32_t reg) 468771fe6b9SJerome Glisse { 469771fe6b9SJerome Glisse struct radeon_device *rdev = info->dev->dev_private; 470771fe6b9SJerome Glisse uint32_t r; 471771fe6b9SJerome Glisse 472771fe6b9SJerome Glisse r = rdev->pll_rreg(rdev, reg); 473771fe6b9SJerome Glisse return r; 474771fe6b9SJerome Glisse } 475771fe6b9SJerome Glisse 476771fe6b9SJerome Glisse static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val) 477771fe6b9SJerome Glisse { 478771fe6b9SJerome Glisse struct radeon_device *rdev = info->dev->dev_private; 479771fe6b9SJerome Glisse 480771fe6b9SJerome Glisse rdev->pll_wreg(rdev, reg, val); 481771fe6b9SJerome Glisse } 482771fe6b9SJerome Glisse 483771fe6b9SJerome Glisse static uint32_t cail_mc_read(struct card_info *info, uint32_t reg) 484771fe6b9SJerome Glisse { 485771fe6b9SJerome Glisse struct radeon_device *rdev = info->dev->dev_private; 486771fe6b9SJerome Glisse uint32_t r; 487771fe6b9SJerome Glisse 488771fe6b9SJerome Glisse r = rdev->mc_rreg(rdev, reg); 489771fe6b9SJerome Glisse return r; 490771fe6b9SJerome Glisse } 491771fe6b9SJerome Glisse 492771fe6b9SJerome Glisse static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val) 493771fe6b9SJerome Glisse { 494771fe6b9SJerome Glisse struct radeon_device *rdev = info->dev->dev_private; 495771fe6b9SJerome Glisse 496771fe6b9SJerome Glisse rdev->mc_wreg(rdev, reg, val); 497771fe6b9SJerome Glisse } 498771fe6b9SJerome Glisse 499771fe6b9SJerome Glisse static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val) 500771fe6b9SJerome Glisse { 501771fe6b9SJerome Glisse struct radeon_device *rdev = info->dev->dev_private; 502771fe6b9SJerome Glisse 503771fe6b9SJerome Glisse WREG32(reg*4, val); 504771fe6b9SJerome Glisse } 505771fe6b9SJerome Glisse 506771fe6b9SJerome Glisse static uint32_t cail_reg_read(struct card_info *info, uint32_t reg) 507771fe6b9SJerome Glisse { 508771fe6b9SJerome Glisse struct radeon_device *rdev = info->dev->dev_private; 509771fe6b9SJerome Glisse uint32_t r; 510771fe6b9SJerome Glisse 511771fe6b9SJerome Glisse r = RREG32(reg*4); 512771fe6b9SJerome Glisse return r; 513771fe6b9SJerome Glisse } 514771fe6b9SJerome Glisse 515351a52a2SAlex Deucher static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val) 516351a52a2SAlex Deucher { 517351a52a2SAlex Deucher struct radeon_device *rdev = info->dev->dev_private; 518351a52a2SAlex Deucher 519351a52a2SAlex Deucher WREG32_IO(reg*4, val); 520351a52a2SAlex Deucher } 521351a52a2SAlex Deucher 522351a52a2SAlex Deucher static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg) 523351a52a2SAlex Deucher { 524351a52a2SAlex Deucher struct radeon_device *rdev = info->dev->dev_private; 525351a52a2SAlex Deucher uint32_t r; 526351a52a2SAlex Deucher 527351a52a2SAlex Deucher r = RREG32_IO(reg*4); 528351a52a2SAlex Deucher return r; 529351a52a2SAlex Deucher } 530351a52a2SAlex Deucher 531771fe6b9SJerome Glisse int radeon_atombios_init(struct radeon_device *rdev) 532771fe6b9SJerome Glisse { 53361c4b24bSMathias Fröhlich struct card_info *atom_card_info = 53461c4b24bSMathias Fröhlich kzalloc(sizeof(struct card_info), GFP_KERNEL); 53561c4b24bSMathias Fröhlich 53661c4b24bSMathias Fröhlich if (!atom_card_info) 53761c4b24bSMathias Fröhlich return -ENOMEM; 53861c4b24bSMathias Fröhlich 53961c4b24bSMathias Fröhlich rdev->mode_info.atom_card_info = atom_card_info; 54061c4b24bSMathias Fröhlich atom_card_info->dev = rdev->ddev; 54161c4b24bSMathias Fröhlich atom_card_info->reg_read = cail_reg_read; 54261c4b24bSMathias Fröhlich atom_card_info->reg_write = cail_reg_write; 543351a52a2SAlex Deucher /* needed for iio ops */ 544351a52a2SAlex Deucher if (rdev->rio_mem) { 545351a52a2SAlex Deucher atom_card_info->ioreg_read = cail_ioreg_read; 546351a52a2SAlex Deucher atom_card_info->ioreg_write = cail_ioreg_write; 547351a52a2SAlex Deucher } else { 548351a52a2SAlex Deucher DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n"); 549351a52a2SAlex Deucher atom_card_info->ioreg_read = cail_reg_read; 550351a52a2SAlex Deucher atom_card_info->ioreg_write = cail_reg_write; 551351a52a2SAlex Deucher } 55261c4b24bSMathias Fröhlich atom_card_info->mc_read = cail_mc_read; 55361c4b24bSMathias Fröhlich atom_card_info->mc_write = cail_mc_write; 55461c4b24bSMathias Fröhlich atom_card_info->pll_read = cail_pll_read; 55561c4b24bSMathias Fröhlich atom_card_info->pll_write = cail_pll_write; 55661c4b24bSMathias Fröhlich 55761c4b24bSMathias Fröhlich rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios); 558c31ad97fSRafał Miłecki mutex_init(&rdev->mode_info.atom_context->mutex); 559771fe6b9SJerome Glisse radeon_atom_initialize_bios_scratch_regs(rdev->ddev); 560d904ef9bSDave Airlie atom_allocate_fb_scratch(rdev->mode_info.atom_context); 561771fe6b9SJerome Glisse return 0; 562771fe6b9SJerome Glisse } 563771fe6b9SJerome Glisse 564771fe6b9SJerome Glisse void radeon_atombios_fini(struct radeon_device *rdev) 565771fe6b9SJerome Glisse { 5664a04a844SJerome Glisse if (rdev->mode_info.atom_context) { 567d904ef9bSDave Airlie kfree(rdev->mode_info.atom_context->scratch); 568771fe6b9SJerome Glisse kfree(rdev->mode_info.atom_context); 5694a04a844SJerome Glisse } 57061c4b24bSMathias Fröhlich kfree(rdev->mode_info.atom_card_info); 571771fe6b9SJerome Glisse } 572771fe6b9SJerome Glisse 573771fe6b9SJerome Glisse int radeon_combios_init(struct radeon_device *rdev) 574771fe6b9SJerome Glisse { 575771fe6b9SJerome Glisse radeon_combios_initialize_bios_scratch_regs(rdev->ddev); 576771fe6b9SJerome Glisse return 0; 577771fe6b9SJerome Glisse } 578771fe6b9SJerome Glisse 579771fe6b9SJerome Glisse void radeon_combios_fini(struct radeon_device *rdev) 580771fe6b9SJerome Glisse { 581771fe6b9SJerome Glisse } 582771fe6b9SJerome Glisse 58328d52043SDave Airlie /* if we get transitioned to only one device, tak VGA back */ 58428d52043SDave Airlie static unsigned int radeon_vga_set_decode(void *cookie, bool state) 58528d52043SDave Airlie { 58628d52043SDave Airlie struct radeon_device *rdev = cookie; 58728d52043SDave Airlie radeon_vga_set_state(rdev, state); 58828d52043SDave Airlie if (state) 58928d52043SDave Airlie return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM | 59028d52043SDave Airlie VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; 59128d52043SDave Airlie else 59228d52043SDave Airlie return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; 59328d52043SDave Airlie } 594c1176d6fSDave Airlie 59536421338SJerome Glisse void radeon_check_arguments(struct radeon_device *rdev) 59636421338SJerome Glisse { 59736421338SJerome Glisse /* vramlimit must be a power of two */ 59836421338SJerome Glisse switch (radeon_vram_limit) { 59936421338SJerome Glisse case 0: 60036421338SJerome Glisse case 4: 60136421338SJerome Glisse case 8: 60236421338SJerome Glisse case 16: 60336421338SJerome Glisse case 32: 60436421338SJerome Glisse case 64: 60536421338SJerome Glisse case 128: 60636421338SJerome Glisse case 256: 60736421338SJerome Glisse case 512: 60836421338SJerome Glisse case 1024: 60936421338SJerome Glisse case 2048: 61036421338SJerome Glisse case 4096: 61136421338SJerome Glisse break; 61236421338SJerome Glisse default: 61336421338SJerome Glisse dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n", 61436421338SJerome Glisse radeon_vram_limit); 61536421338SJerome Glisse radeon_vram_limit = 0; 61636421338SJerome Glisse break; 61736421338SJerome Glisse } 61836421338SJerome Glisse radeon_vram_limit = radeon_vram_limit << 20; 61936421338SJerome Glisse /* gtt size must be power of two and greater or equal to 32M */ 62036421338SJerome Glisse switch (radeon_gart_size) { 62136421338SJerome Glisse case 4: 62236421338SJerome Glisse case 8: 62336421338SJerome Glisse case 16: 62436421338SJerome Glisse dev_warn(rdev->dev, "gart size (%d) too small forcing to 512M\n", 62536421338SJerome Glisse radeon_gart_size); 62636421338SJerome Glisse radeon_gart_size = 512; 62736421338SJerome Glisse break; 62836421338SJerome Glisse case 32: 62936421338SJerome Glisse case 64: 63036421338SJerome Glisse case 128: 63136421338SJerome Glisse case 256: 63236421338SJerome Glisse case 512: 63336421338SJerome Glisse case 1024: 63436421338SJerome Glisse case 2048: 63536421338SJerome Glisse case 4096: 63636421338SJerome Glisse break; 63736421338SJerome Glisse default: 63836421338SJerome Glisse dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n", 63936421338SJerome Glisse radeon_gart_size); 64036421338SJerome Glisse radeon_gart_size = 512; 64136421338SJerome Glisse break; 64236421338SJerome Glisse } 64336421338SJerome Glisse rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024; 64436421338SJerome Glisse /* AGP mode can only be -1, 1, 2, 4, 8 */ 64536421338SJerome Glisse switch (radeon_agpmode) { 64636421338SJerome Glisse case -1: 64736421338SJerome Glisse case 0: 64836421338SJerome Glisse case 1: 64936421338SJerome Glisse case 2: 65036421338SJerome Glisse case 4: 65136421338SJerome Glisse case 8: 65236421338SJerome Glisse break; 65336421338SJerome Glisse default: 65436421338SJerome Glisse dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: " 65536421338SJerome Glisse "-1, 0, 1, 2, 4, 8)\n", radeon_agpmode); 65636421338SJerome Glisse radeon_agpmode = 0; 65736421338SJerome Glisse break; 65836421338SJerome Glisse } 65936421338SJerome Glisse } 66036421338SJerome Glisse 6616a9ee8afSDave Airlie static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state) 6626a9ee8afSDave Airlie { 6636a9ee8afSDave Airlie struct drm_device *dev = pci_get_drvdata(pdev); 6646a9ee8afSDave Airlie pm_message_t pmm = { .event = PM_EVENT_SUSPEND }; 6656a9ee8afSDave Airlie if (state == VGA_SWITCHEROO_ON) { 6666a9ee8afSDave Airlie printk(KERN_INFO "radeon: switched on\n"); 6676a9ee8afSDave Airlie /* don't suspend or resume card normally */ 6685bcf719bSDave Airlie dev->switch_power_state = DRM_SWITCH_POWER_CHANGING; 6696a9ee8afSDave Airlie radeon_resume_kms(dev); 6705bcf719bSDave Airlie dev->switch_power_state = DRM_SWITCH_POWER_ON; 671fbf81762SDave Airlie drm_kms_helper_poll_enable(dev); 6726a9ee8afSDave Airlie } else { 6736a9ee8afSDave Airlie printk(KERN_INFO "radeon: switched off\n"); 674fbf81762SDave Airlie drm_kms_helper_poll_disable(dev); 6755bcf719bSDave Airlie dev->switch_power_state = DRM_SWITCH_POWER_CHANGING; 6766a9ee8afSDave Airlie radeon_suspend_kms(dev, pmm); 6775bcf719bSDave Airlie dev->switch_power_state = DRM_SWITCH_POWER_OFF; 6786a9ee8afSDave Airlie } 6796a9ee8afSDave Airlie } 6806a9ee8afSDave Airlie 6816a9ee8afSDave Airlie static bool radeon_switcheroo_can_switch(struct pci_dev *pdev) 6826a9ee8afSDave Airlie { 6836a9ee8afSDave Airlie struct drm_device *dev = pci_get_drvdata(pdev); 6846a9ee8afSDave Airlie bool can_switch; 6856a9ee8afSDave Airlie 6866a9ee8afSDave Airlie spin_lock(&dev->count_lock); 6876a9ee8afSDave Airlie can_switch = (dev->open_count == 0); 6886a9ee8afSDave Airlie spin_unlock(&dev->count_lock); 6896a9ee8afSDave Airlie return can_switch; 6906a9ee8afSDave Airlie } 6916a9ee8afSDave Airlie 6926a9ee8afSDave Airlie 693771fe6b9SJerome Glisse int radeon_device_init(struct radeon_device *rdev, 694771fe6b9SJerome Glisse struct drm_device *ddev, 695771fe6b9SJerome Glisse struct pci_dev *pdev, 696771fe6b9SJerome Glisse uint32_t flags) 697771fe6b9SJerome Glisse { 698351a52a2SAlex Deucher int r, i; 699ad49f501SDave Airlie int dma_bits; 700771fe6b9SJerome Glisse 701771fe6b9SJerome Glisse rdev->shutdown = false; 7029f022ddfSJerome Glisse rdev->dev = &pdev->dev; 703771fe6b9SJerome Glisse rdev->ddev = ddev; 704771fe6b9SJerome Glisse rdev->pdev = pdev; 705771fe6b9SJerome Glisse rdev->flags = flags; 706771fe6b9SJerome Glisse rdev->family = flags & RADEON_FAMILY_MASK; 707771fe6b9SJerome Glisse rdev->is_atom_bios = false; 708771fe6b9SJerome Glisse rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT; 709771fe6b9SJerome Glisse rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024; 710771fe6b9SJerome Glisse rdev->gpu_lockup = false; 711733289c2SJerome Glisse rdev->accel_working = false; 7121b5331d9SJerome Glisse 713d522d9ccSThomas Reim DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X).\n", 714d522d9ccSThomas Reim radeon_family_name[rdev->family], pdev->vendor, pdev->device, 715d522d9ccSThomas Reim pdev->subsystem_vendor, pdev->subsystem_device); 7161b5331d9SJerome Glisse 717771fe6b9SJerome Glisse /* mutex initialization are all done here so we 718771fe6b9SJerome Glisse * can recall function without having locking issues */ 7197a1619b9SMichel Dänzer radeon_mutex_init(&rdev->cs_mutex); 720771fe6b9SJerome Glisse mutex_init(&rdev->ib_pool.mutex); 721bf852799SChristian König for (i = 0; i < RADEON_NUM_RINGS; ++i) 722bf852799SChristian König mutex_init(&rdev->cp[i].mutex); 72340bacf16SAlex Deucher mutex_init(&rdev->dc_hw_i2c_mutex); 724d8f60cfcSAlex Deucher if (rdev->family >= CHIP_R600) 725d8f60cfcSAlex Deucher spin_lock_init(&rdev->ih.lock); 7264c788679SJerome Glisse mutex_init(&rdev->gem.mutex); 727c913e23aSRafał Miłecki mutex_init(&rdev->pm.mutex); 7285876dd24SMatthew Garrett mutex_init(&rdev->vram_mutex); 7297465280cSAlex Deucher rwlock_init(&rdev->fence_lock); 73015d3332fSChristian König rwlock_init(&rdev->semaphore_drv.lock); 7319f022ddfSJerome Glisse INIT_LIST_HEAD(&rdev->gem.objects); 73273a6d3fcSRafał Miłecki init_waitqueue_head(&rdev->irq.vblank_queue); 7332031f77cSAlex Deucher init_waitqueue_head(&rdev->irq.idle_queue); 73415d3332fSChristian König INIT_LIST_HEAD(&rdev->semaphore_drv.free); 735771fe6b9SJerome Glisse 7364aac0473SJerome Glisse /* Set asic functions */ 7374aac0473SJerome Glisse r = radeon_asic_init(rdev); 73836421338SJerome Glisse if (r) 7394aac0473SJerome Glisse return r; 74036421338SJerome Glisse radeon_check_arguments(rdev); 7414aac0473SJerome Glisse 742f95df9caSAlex Deucher /* all of the newer IGP chips have an internal gart 743f95df9caSAlex Deucher * However some rs4xx report as AGP, so remove that here. 744f95df9caSAlex Deucher */ 745f95df9caSAlex Deucher if ((rdev->family >= CHIP_RS400) && 746f95df9caSAlex Deucher (rdev->flags & RADEON_IS_IGP)) { 747f95df9caSAlex Deucher rdev->flags &= ~RADEON_IS_AGP; 748f95df9caSAlex Deucher } 749f95df9caSAlex Deucher 75030256a3fSJerome Glisse if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) { 751b574f251SJerome Glisse radeon_agp_disable(rdev); 752771fe6b9SJerome Glisse } 753771fe6b9SJerome Glisse 754ad49f501SDave Airlie /* set DMA mask + need_dma32 flags. 755ad49f501SDave Airlie * PCIE - can handle 40-bits. 756005a83f1SAlex Deucher * IGP - can handle 40-bits 757ad49f501SDave Airlie * AGP - generally dma32 is safest 758005a83f1SAlex Deucher * PCI - dma32 for legacy pci gart, 40 bits on newer asics 759ad49f501SDave Airlie */ 760ad49f501SDave Airlie rdev->need_dma32 = false; 761ad49f501SDave Airlie if (rdev->flags & RADEON_IS_AGP) 762ad49f501SDave Airlie rdev->need_dma32 = true; 763005a83f1SAlex Deucher if ((rdev->flags & RADEON_IS_PCI) && 764005a83f1SAlex Deucher (rdev->family < CHIP_RS400)) 765ad49f501SDave Airlie rdev->need_dma32 = true; 766ad49f501SDave Airlie 767ad49f501SDave Airlie dma_bits = rdev->need_dma32 ? 32 : 40; 768ad49f501SDave Airlie r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits)); 769771fe6b9SJerome Glisse if (r) { 77062fff811SDaniel Haid rdev->need_dma32 = true; 771c52494f6SKonrad Rzeszutek Wilk dma_bits = 32; 772771fe6b9SJerome Glisse printk(KERN_WARNING "radeon: No suitable DMA available.\n"); 773771fe6b9SJerome Glisse } 774c52494f6SKonrad Rzeszutek Wilk r = pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits)); 775c52494f6SKonrad Rzeszutek Wilk if (r) { 776c52494f6SKonrad Rzeszutek Wilk pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(32)); 777c52494f6SKonrad Rzeszutek Wilk printk(KERN_WARNING "radeon: No coherent DMA available.\n"); 778c52494f6SKonrad Rzeszutek Wilk } 779771fe6b9SJerome Glisse 780771fe6b9SJerome Glisse /* Registers mapping */ 781771fe6b9SJerome Glisse /* TODO: block userspace mapping of io register */ 78201d73a69SJordan Crouse rdev->rmmio_base = pci_resource_start(rdev->pdev, 2); 78301d73a69SJordan Crouse rdev->rmmio_size = pci_resource_len(rdev->pdev, 2); 784771fe6b9SJerome Glisse rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size); 785771fe6b9SJerome Glisse if (rdev->rmmio == NULL) { 786771fe6b9SJerome Glisse return -ENOMEM; 787771fe6b9SJerome Glisse } 788771fe6b9SJerome Glisse DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base); 789771fe6b9SJerome Glisse DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size); 790771fe6b9SJerome Glisse 791351a52a2SAlex Deucher /* io port mapping */ 792351a52a2SAlex Deucher for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { 793351a52a2SAlex Deucher if (pci_resource_flags(rdev->pdev, i) & IORESOURCE_IO) { 794351a52a2SAlex Deucher rdev->rio_mem_size = pci_resource_len(rdev->pdev, i); 795351a52a2SAlex Deucher rdev->rio_mem = pci_iomap(rdev->pdev, i, rdev->rio_mem_size); 796351a52a2SAlex Deucher break; 797351a52a2SAlex Deucher } 798351a52a2SAlex Deucher } 799351a52a2SAlex Deucher if (rdev->rio_mem == NULL) 800351a52a2SAlex Deucher DRM_ERROR("Unable to find PCI I/O BAR\n"); 801351a52a2SAlex Deucher 80228d52043SDave Airlie /* if we have > 1 VGA cards, then disable the radeon VGA resources */ 80393239ea1SDave Airlie /* this will fail for cards that aren't VGA class devices, just 80493239ea1SDave Airlie * ignore it */ 80593239ea1SDave Airlie vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode); 8066a9ee8afSDave Airlie vga_switcheroo_register_client(rdev->pdev, 8076a9ee8afSDave Airlie radeon_switcheroo_set_state, 8088d608aa6SDave Airlie NULL, 8096a9ee8afSDave Airlie radeon_switcheroo_can_switch); 81028d52043SDave Airlie 8113ce0a23dSJerome Glisse r = radeon_init(rdev); 812b574f251SJerome Glisse if (r) 813b574f251SJerome Glisse return r; 814b1e3a6d1SMichel Dänzer 815b574f251SJerome Glisse if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) { 816b574f251SJerome Glisse /* Acceleration not working on AGP card try again 817b574f251SJerome Glisse * with fallback to PCI or PCIE GART 818b574f251SJerome Glisse */ 819a2d07b74SJerome Glisse radeon_asic_reset(rdev); 820b574f251SJerome Glisse radeon_fini(rdev); 821b574f251SJerome Glisse radeon_agp_disable(rdev); 822b574f251SJerome Glisse r = radeon_init(rdev); 8234aac0473SJerome Glisse if (r) 8244aac0473SJerome Glisse return r; 8253ce0a23dSJerome Glisse } 826*60a7e396SChristian König if ((radeon_testing & 1)) { 827ecc0b326SMichel Dänzer radeon_test_moves(rdev); 828ecc0b326SMichel Dänzer } 829*60a7e396SChristian König if ((radeon_testing & 2)) { 830*60a7e396SChristian König radeon_test_syncing(rdev); 831*60a7e396SChristian König } 832771fe6b9SJerome Glisse if (radeon_benchmarking) { 833638dd7dbSIlija Hadzic radeon_benchmark(rdev, radeon_benchmarking); 834771fe6b9SJerome Glisse } 8356cf8a3f5SJerome Glisse return 0; 836771fe6b9SJerome Glisse } 837771fe6b9SJerome Glisse 8384d8bf9aeSChristian König static void radeon_debugfs_remove_files(struct radeon_device *rdev); 8394d8bf9aeSChristian König 840771fe6b9SJerome Glisse void radeon_device_fini(struct radeon_device *rdev) 841771fe6b9SJerome Glisse { 842771fe6b9SJerome Glisse DRM_INFO("radeon: finishing device.\n"); 843771fe6b9SJerome Glisse rdev->shutdown = true; 84490aca4d2SJerome Glisse /* evict vram memory */ 84590aca4d2SJerome Glisse radeon_bo_evict_vram(rdev); 8463ce0a23dSJerome Glisse radeon_fini(rdev); 8476a9ee8afSDave Airlie vga_switcheroo_unregister_client(rdev->pdev); 848c1176d6fSDave Airlie vga_client_register(rdev->pdev, NULL, NULL, NULL); 849e0a2ca73SAlex Deucher if (rdev->rio_mem) 850351a52a2SAlex Deucher pci_iounmap(rdev->pdev, rdev->rio_mem); 851351a52a2SAlex Deucher rdev->rio_mem = NULL; 852771fe6b9SJerome Glisse iounmap(rdev->rmmio); 853771fe6b9SJerome Glisse rdev->rmmio = NULL; 8544d8bf9aeSChristian König radeon_debugfs_remove_files(rdev); 855771fe6b9SJerome Glisse } 856771fe6b9SJerome Glisse 857771fe6b9SJerome Glisse 858771fe6b9SJerome Glisse /* 859771fe6b9SJerome Glisse * Suspend & resume. 860771fe6b9SJerome Glisse */ 861771fe6b9SJerome Glisse int radeon_suspend_kms(struct drm_device *dev, pm_message_t state) 862771fe6b9SJerome Glisse { 863875c1866SDarren Jenkins struct radeon_device *rdev; 864771fe6b9SJerome Glisse struct drm_crtc *crtc; 865d8dcaa1dSAlex Deucher struct drm_connector *connector; 8667465280cSAlex Deucher int i, r; 867771fe6b9SJerome Glisse 868875c1866SDarren Jenkins if (dev == NULL || dev->dev_private == NULL) { 869771fe6b9SJerome Glisse return -ENODEV; 870771fe6b9SJerome Glisse } 871771fe6b9SJerome Glisse if (state.event == PM_EVENT_PRETHAW) { 872771fe6b9SJerome Glisse return 0; 873771fe6b9SJerome Glisse } 874875c1866SDarren Jenkins rdev = dev->dev_private; 875875c1866SDarren Jenkins 8765bcf719bSDave Airlie if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) 8776a9ee8afSDave Airlie return 0; 878d8dcaa1dSAlex Deucher 879d8dcaa1dSAlex Deucher /* turn off display hw */ 880d8dcaa1dSAlex Deucher list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 881d8dcaa1dSAlex Deucher drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF); 882d8dcaa1dSAlex Deucher } 883d8dcaa1dSAlex Deucher 884771fe6b9SJerome Glisse /* unpin the front buffers */ 885771fe6b9SJerome Glisse list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 886771fe6b9SJerome Glisse struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->fb); 8874c788679SJerome Glisse struct radeon_bo *robj; 888771fe6b9SJerome Glisse 889771fe6b9SJerome Glisse if (rfb == NULL || rfb->obj == NULL) { 890771fe6b9SJerome Glisse continue; 891771fe6b9SJerome Glisse } 8927e4d15d9SDaniel Vetter robj = gem_to_radeon_bo(rfb->obj); 89338651674SDave Airlie /* don't unpin kernel fb objects */ 89438651674SDave Airlie if (!radeon_fbdev_robj_is_fb(rdev, robj)) { 8954c788679SJerome Glisse r = radeon_bo_reserve(robj, false); 89638651674SDave Airlie if (r == 0) { 8974c788679SJerome Glisse radeon_bo_unpin(robj); 8984c788679SJerome Glisse radeon_bo_unreserve(robj); 8994c788679SJerome Glisse } 900771fe6b9SJerome Glisse } 901771fe6b9SJerome Glisse } 902771fe6b9SJerome Glisse /* evict vram memory */ 9034c788679SJerome Glisse radeon_bo_evict_vram(rdev); 904771fe6b9SJerome Glisse /* wait for gpu to finish processing current batch */ 9057465280cSAlex Deucher for (i = 0; i < RADEON_NUM_RINGS; i++) 9067465280cSAlex Deucher radeon_fence_wait_last(rdev, i); 907771fe6b9SJerome Glisse 908f657c2a7SYang Zhao radeon_save_bios_scratch_regs(rdev); 909f657c2a7SYang Zhao 910ce8f5370SAlex Deucher radeon_pm_suspend(rdev); 9113ce0a23dSJerome Glisse radeon_suspend(rdev); 912d4877cf2SAlex Deucher radeon_hpd_fini(rdev); 913771fe6b9SJerome Glisse /* evict remaining vram memory */ 9144c788679SJerome Glisse radeon_bo_evict_vram(rdev); 915771fe6b9SJerome Glisse 91610b06122SJerome Glisse radeon_agp_suspend(rdev); 91710b06122SJerome Glisse 918771fe6b9SJerome Glisse pci_save_state(dev->pdev); 919771fe6b9SJerome Glisse if (state.event == PM_EVENT_SUSPEND) { 920771fe6b9SJerome Glisse /* Shut down the device */ 921771fe6b9SJerome Glisse pci_disable_device(dev->pdev); 922771fe6b9SJerome Glisse pci_set_power_state(dev->pdev, PCI_D3hot); 923771fe6b9SJerome Glisse } 924ac751efaSTorben Hohn console_lock(); 92538651674SDave Airlie radeon_fbdev_set_suspend(rdev, 1); 926ac751efaSTorben Hohn console_unlock(); 927771fe6b9SJerome Glisse return 0; 928771fe6b9SJerome Glisse } 929771fe6b9SJerome Glisse 930771fe6b9SJerome Glisse int radeon_resume_kms(struct drm_device *dev) 931771fe6b9SJerome Glisse { 93209bdf591SCedric Godin struct drm_connector *connector; 933771fe6b9SJerome Glisse struct radeon_device *rdev = dev->dev_private; 934771fe6b9SJerome Glisse 9355bcf719bSDave Airlie if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) 9366a9ee8afSDave Airlie return 0; 9376a9ee8afSDave Airlie 938ac751efaSTorben Hohn console_lock(); 939771fe6b9SJerome Glisse pci_set_power_state(dev->pdev, PCI_D0); 940771fe6b9SJerome Glisse pci_restore_state(dev->pdev); 941771fe6b9SJerome Glisse if (pci_enable_device(dev->pdev)) { 942ac751efaSTorben Hohn console_unlock(); 943771fe6b9SJerome Glisse return -1; 944771fe6b9SJerome Glisse } 945771fe6b9SJerome Glisse pci_set_master(dev->pdev); 9460ebf1717SDave Airlie /* resume AGP if in use */ 9470ebf1717SDave Airlie radeon_agp_resume(rdev); 9483ce0a23dSJerome Glisse radeon_resume(rdev); 949ce8f5370SAlex Deucher radeon_pm_resume(rdev); 950f657c2a7SYang Zhao radeon_restore_bios_scratch_regs(rdev); 95109bdf591SCedric Godin 95238651674SDave Airlie radeon_fbdev_set_suspend(rdev, 0); 953ac751efaSTorben Hohn console_unlock(); 954771fe6b9SJerome Glisse 955ac89af1eSAlex Deucher /* init dig PHYs */ 956ac89af1eSAlex Deucher if (rdev->is_atom_bios) 957ac89af1eSAlex Deucher radeon_atom_encoder_init(rdev); 958d4877cf2SAlex Deucher /* reset hpd state */ 959d4877cf2SAlex Deucher radeon_hpd_init(rdev); 960771fe6b9SJerome Glisse /* blat the mode back in */ 961771fe6b9SJerome Glisse drm_helper_resume_force_mode(dev); 962a93f344dSAlex Deucher /* turn on display hw */ 963a93f344dSAlex Deucher list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 964a93f344dSAlex Deucher drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON); 965a93f344dSAlex Deucher } 966771fe6b9SJerome Glisse return 0; 967771fe6b9SJerome Glisse } 968771fe6b9SJerome Glisse 96990aca4d2SJerome Glisse int radeon_gpu_reset(struct radeon_device *rdev) 97090aca4d2SJerome Glisse { 97190aca4d2SJerome Glisse int r; 9728fd1b84cSDave Airlie int resched; 97390aca4d2SJerome Glisse 9747a1619b9SMichel Dänzer /* Prevent CS ioctl from interfering */ 9757a1619b9SMichel Dänzer radeon_mutex_lock(&rdev->cs_mutex); 9767a1619b9SMichel Dänzer 97790aca4d2SJerome Glisse radeon_save_bios_scratch_regs(rdev); 9788fd1b84cSDave Airlie /* block TTM */ 9798fd1b84cSDave Airlie resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev); 98090aca4d2SJerome Glisse radeon_suspend(rdev); 98190aca4d2SJerome Glisse 98290aca4d2SJerome Glisse r = radeon_asic_reset(rdev); 98390aca4d2SJerome Glisse if (!r) { 98490aca4d2SJerome Glisse dev_info(rdev->dev, "GPU reset succeed\n"); 98590aca4d2SJerome Glisse radeon_resume(rdev); 98690aca4d2SJerome Glisse radeon_restore_bios_scratch_regs(rdev); 98790aca4d2SJerome Glisse drm_helper_resume_force_mode(rdev->ddev); 9888fd1b84cSDave Airlie ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched); 98990aca4d2SJerome Glisse } 9907a1619b9SMichel Dänzer 9917a1619b9SMichel Dänzer radeon_mutex_unlock(&rdev->cs_mutex); 9927a1619b9SMichel Dänzer 9937a1619b9SMichel Dänzer if (r) { 99490aca4d2SJerome Glisse /* bad news, how to tell it to userspace ? */ 99590aca4d2SJerome Glisse dev_info(rdev->dev, "GPU reset failed\n"); 9967a1619b9SMichel Dänzer } 9977a1619b9SMichel Dänzer 99890aca4d2SJerome Glisse return r; 99990aca4d2SJerome Glisse } 100090aca4d2SJerome Glisse 1001771fe6b9SJerome Glisse 1002771fe6b9SJerome Glisse /* 1003771fe6b9SJerome Glisse * Debugfs 1004771fe6b9SJerome Glisse */ 1005771fe6b9SJerome Glisse int radeon_debugfs_add_files(struct radeon_device *rdev, 1006771fe6b9SJerome Glisse struct drm_info_list *files, 1007771fe6b9SJerome Glisse unsigned nfiles) 1008771fe6b9SJerome Glisse { 1009771fe6b9SJerome Glisse unsigned i; 1010771fe6b9SJerome Glisse 10114d8bf9aeSChristian König for (i = 0; i < rdev->debugfs_count; i++) { 10124d8bf9aeSChristian König if (rdev->debugfs[i].files == files) { 1013771fe6b9SJerome Glisse /* Already registered */ 1014771fe6b9SJerome Glisse return 0; 1015771fe6b9SJerome Glisse } 1016771fe6b9SJerome Glisse } 1017c245cb9eSMichael Witten 10184d8bf9aeSChristian König i = rdev->debugfs_count + 1; 1019c245cb9eSMichael Witten if (i > RADEON_DEBUGFS_MAX_COMPONENTS) { 1020c245cb9eSMichael Witten DRM_ERROR("Reached maximum number of debugfs components.\n"); 1021c245cb9eSMichael Witten DRM_ERROR("Report so we increase " 1022c245cb9eSMichael Witten "RADEON_DEBUGFS_MAX_COMPONENTS.\n"); 1023771fe6b9SJerome Glisse return -EINVAL; 1024771fe6b9SJerome Glisse } 10254d8bf9aeSChristian König rdev->debugfs[rdev->debugfs_count].files = files; 10264d8bf9aeSChristian König rdev->debugfs[rdev->debugfs_count].num_files = nfiles; 10274d8bf9aeSChristian König rdev->debugfs_count = i; 1028771fe6b9SJerome Glisse #if defined(CONFIG_DEBUG_FS) 1029771fe6b9SJerome Glisse drm_debugfs_create_files(files, nfiles, 1030771fe6b9SJerome Glisse rdev->ddev->control->debugfs_root, 1031771fe6b9SJerome Glisse rdev->ddev->control); 1032771fe6b9SJerome Glisse drm_debugfs_create_files(files, nfiles, 1033771fe6b9SJerome Glisse rdev->ddev->primary->debugfs_root, 1034771fe6b9SJerome Glisse rdev->ddev->primary); 1035771fe6b9SJerome Glisse #endif 1036771fe6b9SJerome Glisse return 0; 1037771fe6b9SJerome Glisse } 1038771fe6b9SJerome Glisse 10394d8bf9aeSChristian König static void radeon_debugfs_remove_files(struct radeon_device *rdev) 10404d8bf9aeSChristian König { 10414d8bf9aeSChristian König #if defined(CONFIG_DEBUG_FS) 10424d8bf9aeSChristian König unsigned i; 10434d8bf9aeSChristian König 10444d8bf9aeSChristian König for (i = 0; i < rdev->debugfs_count; i++) { 10454d8bf9aeSChristian König drm_debugfs_remove_files(rdev->debugfs[i].files, 10464d8bf9aeSChristian König rdev->debugfs[i].num_files, 10474d8bf9aeSChristian König rdev->ddev->control); 10484d8bf9aeSChristian König drm_debugfs_remove_files(rdev->debugfs[i].files, 10494d8bf9aeSChristian König rdev->debugfs[i].num_files, 10504d8bf9aeSChristian König rdev->ddev->primary); 10514d8bf9aeSChristian König } 10524d8bf9aeSChristian König #endif 10534d8bf9aeSChristian König } 10544d8bf9aeSChristian König 1055771fe6b9SJerome Glisse #if defined(CONFIG_DEBUG_FS) 1056771fe6b9SJerome Glisse int radeon_debugfs_init(struct drm_minor *minor) 1057771fe6b9SJerome Glisse { 1058771fe6b9SJerome Glisse return 0; 1059771fe6b9SJerome Glisse } 1060771fe6b9SJerome Glisse 1061771fe6b9SJerome Glisse void radeon_debugfs_cleanup(struct drm_minor *minor) 1062771fe6b9SJerome Glisse { 1063771fe6b9SJerome Glisse } 1064771fe6b9SJerome Glisse #endif 1065