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> 35771fe6b9SJerome Glisse #include "radeon_reg.h" 36771fe6b9SJerome Glisse #include "radeon.h" 37771fe6b9SJerome Glisse #include "atom.h" 38771fe6b9SJerome Glisse 391b5331d9SJerome Glisse static const char radeon_family_name[][16] = { 401b5331d9SJerome Glisse "R100", 411b5331d9SJerome Glisse "RV100", 421b5331d9SJerome Glisse "RS100", 431b5331d9SJerome Glisse "RV200", 441b5331d9SJerome Glisse "RS200", 451b5331d9SJerome Glisse "R200", 461b5331d9SJerome Glisse "RV250", 471b5331d9SJerome Glisse "RS300", 481b5331d9SJerome Glisse "RV280", 491b5331d9SJerome Glisse "R300", 501b5331d9SJerome Glisse "R350", 511b5331d9SJerome Glisse "RV350", 521b5331d9SJerome Glisse "RV380", 531b5331d9SJerome Glisse "R420", 541b5331d9SJerome Glisse "R423", 551b5331d9SJerome Glisse "RV410", 561b5331d9SJerome Glisse "RS400", 571b5331d9SJerome Glisse "RS480", 581b5331d9SJerome Glisse "RS600", 591b5331d9SJerome Glisse "RS690", 601b5331d9SJerome Glisse "RS740", 611b5331d9SJerome Glisse "RV515", 621b5331d9SJerome Glisse "R520", 631b5331d9SJerome Glisse "RV530", 641b5331d9SJerome Glisse "RV560", 651b5331d9SJerome Glisse "RV570", 661b5331d9SJerome Glisse "R580", 671b5331d9SJerome Glisse "R600", 681b5331d9SJerome Glisse "RV610", 691b5331d9SJerome Glisse "RV630", 701b5331d9SJerome Glisse "RV670", 711b5331d9SJerome Glisse "RV620", 721b5331d9SJerome Glisse "RV635", 731b5331d9SJerome Glisse "RS780", 741b5331d9SJerome Glisse "RS880", 751b5331d9SJerome Glisse "RV770", 761b5331d9SJerome Glisse "RV730", 771b5331d9SJerome Glisse "RV710", 781b5331d9SJerome Glisse "RV740", 791b5331d9SJerome Glisse "CEDAR", 801b5331d9SJerome Glisse "REDWOOD", 811b5331d9SJerome Glisse "JUNIPER", 821b5331d9SJerome Glisse "CYPRESS", 831b5331d9SJerome Glisse "HEMLOCK", 84b08ebe7eSAlex Deucher "PALM", 851fe18305SAlex Deucher "BARTS", 861fe18305SAlex Deucher "TURKS", 871fe18305SAlex Deucher "CAICOS", 881b5331d9SJerome Glisse "LAST", 891b5331d9SJerome Glisse }; 901b5331d9SJerome Glisse 91771fe6b9SJerome Glisse /* 92b1e3a6d1SMichel Dänzer * Clear GPU surface registers. 93b1e3a6d1SMichel Dänzer */ 943ce0a23dSJerome Glisse void radeon_surface_init(struct radeon_device *rdev) 95b1e3a6d1SMichel Dänzer { 96b1e3a6d1SMichel Dänzer /* FIXME: check this out */ 97b1e3a6d1SMichel Dänzer if (rdev->family < CHIP_R600) { 98b1e3a6d1SMichel Dänzer int i; 99b1e3a6d1SMichel Dänzer 100550e2d92SDave Airlie for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) { 101550e2d92SDave Airlie if (rdev->surface_regs[i].bo) 102550e2d92SDave Airlie radeon_bo_get_surface_reg(rdev->surface_regs[i].bo); 103550e2d92SDave Airlie else 104550e2d92SDave Airlie radeon_clear_surface_reg(rdev, i); 105b1e3a6d1SMichel Dänzer } 106e024e110SDave Airlie /* enable surfaces */ 107e024e110SDave Airlie WREG32(RADEON_SURFACE_CNTL, 0); 108b1e3a6d1SMichel Dänzer } 109b1e3a6d1SMichel Dänzer } 110b1e3a6d1SMichel Dänzer 111b1e3a6d1SMichel Dänzer /* 112771fe6b9SJerome Glisse * GPU scratch registers helpers function. 113771fe6b9SJerome Glisse */ 1143ce0a23dSJerome Glisse void radeon_scratch_init(struct radeon_device *rdev) 115771fe6b9SJerome Glisse { 116771fe6b9SJerome Glisse int i; 117771fe6b9SJerome Glisse 118771fe6b9SJerome Glisse /* FIXME: check this out */ 119771fe6b9SJerome Glisse if (rdev->family < CHIP_R300) { 120771fe6b9SJerome Glisse rdev->scratch.num_reg = 5; 121771fe6b9SJerome Glisse } else { 122771fe6b9SJerome Glisse rdev->scratch.num_reg = 7; 123771fe6b9SJerome Glisse } 124724c80e1SAlex Deucher rdev->scratch.reg_base = RADEON_SCRATCH_REG0; 125771fe6b9SJerome Glisse for (i = 0; i < rdev->scratch.num_reg; i++) { 126771fe6b9SJerome Glisse rdev->scratch.free[i] = true; 127724c80e1SAlex Deucher rdev->scratch.reg[i] = rdev->scratch.reg_base + (i * 4); 128771fe6b9SJerome Glisse } 129771fe6b9SJerome Glisse } 130771fe6b9SJerome Glisse 131771fe6b9SJerome Glisse int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg) 132771fe6b9SJerome Glisse { 133771fe6b9SJerome Glisse int i; 134771fe6b9SJerome Glisse 135771fe6b9SJerome Glisse for (i = 0; i < rdev->scratch.num_reg; i++) { 136771fe6b9SJerome Glisse if (rdev->scratch.free[i]) { 137771fe6b9SJerome Glisse rdev->scratch.free[i] = false; 138771fe6b9SJerome Glisse *reg = rdev->scratch.reg[i]; 139771fe6b9SJerome Glisse return 0; 140771fe6b9SJerome Glisse } 141771fe6b9SJerome Glisse } 142771fe6b9SJerome Glisse return -EINVAL; 143771fe6b9SJerome Glisse } 144771fe6b9SJerome Glisse 145771fe6b9SJerome Glisse void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg) 146771fe6b9SJerome Glisse { 147771fe6b9SJerome Glisse int i; 148771fe6b9SJerome Glisse 149771fe6b9SJerome Glisse for (i = 0; i < rdev->scratch.num_reg; i++) { 150771fe6b9SJerome Glisse if (rdev->scratch.reg[i] == reg) { 151771fe6b9SJerome Glisse rdev->scratch.free[i] = true; 152771fe6b9SJerome Glisse return; 153771fe6b9SJerome Glisse } 154771fe6b9SJerome Glisse } 155771fe6b9SJerome Glisse } 156771fe6b9SJerome Glisse 157724c80e1SAlex Deucher void radeon_wb_disable(struct radeon_device *rdev) 158724c80e1SAlex Deucher { 159724c80e1SAlex Deucher int r; 160724c80e1SAlex Deucher 161724c80e1SAlex Deucher if (rdev->wb.wb_obj) { 162724c80e1SAlex Deucher r = radeon_bo_reserve(rdev->wb.wb_obj, false); 163724c80e1SAlex Deucher if (unlikely(r != 0)) 164724c80e1SAlex Deucher return; 165724c80e1SAlex Deucher radeon_bo_kunmap(rdev->wb.wb_obj); 166724c80e1SAlex Deucher radeon_bo_unpin(rdev->wb.wb_obj); 167724c80e1SAlex Deucher radeon_bo_unreserve(rdev->wb.wb_obj); 168724c80e1SAlex Deucher } 169724c80e1SAlex Deucher rdev->wb.enabled = false; 170724c80e1SAlex Deucher } 171724c80e1SAlex Deucher 172724c80e1SAlex Deucher void radeon_wb_fini(struct radeon_device *rdev) 173724c80e1SAlex Deucher { 174724c80e1SAlex Deucher radeon_wb_disable(rdev); 175724c80e1SAlex Deucher if (rdev->wb.wb_obj) { 176724c80e1SAlex Deucher radeon_bo_unref(&rdev->wb.wb_obj); 177724c80e1SAlex Deucher rdev->wb.wb = NULL; 178724c80e1SAlex Deucher rdev->wb.wb_obj = NULL; 179724c80e1SAlex Deucher } 180724c80e1SAlex Deucher } 181724c80e1SAlex Deucher 182724c80e1SAlex Deucher int radeon_wb_init(struct radeon_device *rdev) 183724c80e1SAlex Deucher { 184724c80e1SAlex Deucher int r; 185724c80e1SAlex Deucher 186724c80e1SAlex Deucher if (rdev->wb.wb_obj == NULL) { 187441921d5SDaniel Vetter r = radeon_bo_create(rdev, RADEON_GPU_PAGE_SIZE, PAGE_SIZE, true, 188724c80e1SAlex Deucher RADEON_GEM_DOMAIN_GTT, &rdev->wb.wb_obj); 189724c80e1SAlex Deucher if (r) { 190724c80e1SAlex Deucher dev_warn(rdev->dev, "(%d) create WB bo failed\n", r); 191724c80e1SAlex Deucher return r; 192724c80e1SAlex Deucher } 193724c80e1SAlex Deucher } 194724c80e1SAlex Deucher r = radeon_bo_reserve(rdev->wb.wb_obj, false); 195724c80e1SAlex Deucher if (unlikely(r != 0)) { 196724c80e1SAlex Deucher radeon_wb_fini(rdev); 197724c80e1SAlex Deucher return r; 198724c80e1SAlex Deucher } 199724c80e1SAlex Deucher r = radeon_bo_pin(rdev->wb.wb_obj, RADEON_GEM_DOMAIN_GTT, 200724c80e1SAlex Deucher &rdev->wb.gpu_addr); 201724c80e1SAlex Deucher if (r) { 202724c80e1SAlex Deucher radeon_bo_unreserve(rdev->wb.wb_obj); 203724c80e1SAlex Deucher dev_warn(rdev->dev, "(%d) pin WB bo failed\n", r); 204724c80e1SAlex Deucher radeon_wb_fini(rdev); 205724c80e1SAlex Deucher return r; 206724c80e1SAlex Deucher } 207724c80e1SAlex Deucher r = radeon_bo_kmap(rdev->wb.wb_obj, (void **)&rdev->wb.wb); 208724c80e1SAlex Deucher radeon_bo_unreserve(rdev->wb.wb_obj); 209724c80e1SAlex Deucher if (r) { 210724c80e1SAlex Deucher dev_warn(rdev->dev, "(%d) map WB bo failed\n", r); 211724c80e1SAlex Deucher radeon_wb_fini(rdev); 212724c80e1SAlex Deucher return r; 213724c80e1SAlex Deucher } 214724c80e1SAlex Deucher 215d0f8a854SAlex Deucher /* disable event_write fences */ 216d0f8a854SAlex Deucher rdev->wb.use_event = false; 217724c80e1SAlex Deucher /* disabled via module param */ 218724c80e1SAlex Deucher if (radeon_no_wb == 1) 219724c80e1SAlex Deucher rdev->wb.enabled = false; 220724c80e1SAlex Deucher else { 221724c80e1SAlex Deucher /* often unreliable on AGP */ 222724c80e1SAlex Deucher if (rdev->flags & RADEON_IS_AGP) { 223724c80e1SAlex Deucher rdev->wb.enabled = false; 224d0f8a854SAlex Deucher } else { 225724c80e1SAlex Deucher rdev->wb.enabled = true; 226d0f8a854SAlex Deucher /* event_write fences are only available on r600+ */ 227d0f8a854SAlex Deucher if (rdev->family >= CHIP_R600) 228d0f8a854SAlex Deucher rdev->wb.use_event = true; 229d0f8a854SAlex Deucher } 230724c80e1SAlex Deucher } 2317d52785dSAlex Deucher /* always use writeback/events on NI */ 2327d52785dSAlex Deucher if (ASIC_IS_DCE5(rdev)) { 2337d52785dSAlex Deucher rdev->wb.enabled = true; 2347d52785dSAlex Deucher rdev->wb.use_event = true; 2357d52785dSAlex Deucher } 236724c80e1SAlex Deucher 237724c80e1SAlex Deucher dev_info(rdev->dev, "WB %sabled\n", rdev->wb.enabled ? "en" : "dis"); 238724c80e1SAlex Deucher 239724c80e1SAlex Deucher return 0; 240724c80e1SAlex Deucher } 241724c80e1SAlex Deucher 242d594e46aSJerome Glisse /** 243d594e46aSJerome Glisse * radeon_vram_location - try to find VRAM location 244d594e46aSJerome Glisse * @rdev: radeon device structure holding all necessary informations 245d594e46aSJerome Glisse * @mc: memory controller structure holding memory informations 246d594e46aSJerome Glisse * @base: base address at which to put VRAM 247d594e46aSJerome Glisse * 248d594e46aSJerome Glisse * Function will place try to place VRAM at base address provided 249d594e46aSJerome Glisse * as parameter (which is so far either PCI aperture address or 250d594e46aSJerome Glisse * for IGP TOM base address). 251d594e46aSJerome Glisse * 252d594e46aSJerome Glisse * If there is not enough space to fit the unvisible VRAM in the 32bits 253d594e46aSJerome Glisse * address space then we limit the VRAM size to the aperture. 254d594e46aSJerome Glisse * 255d594e46aSJerome Glisse * If we are using AGP and if the AGP aperture doesn't allow us to have 256d594e46aSJerome Glisse * room for all the VRAM than we restrict the VRAM to the PCI aperture 257d594e46aSJerome Glisse * size and print a warning. 258d594e46aSJerome Glisse * 259d594e46aSJerome Glisse * This function will never fails, worst case are limiting VRAM. 260d594e46aSJerome Glisse * 261d594e46aSJerome Glisse * Note: GTT start, end, size should be initialized before calling this 262d594e46aSJerome Glisse * function on AGP platform. 263d594e46aSJerome Glisse * 264d594e46aSJerome Glisse * Note: We don't explictly enforce VRAM start to be aligned on VRAM size, 265d594e46aSJerome Glisse * this shouldn't be a problem as we are using the PCI aperture as a reference. 266d594e46aSJerome Glisse * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but 267d594e46aSJerome Glisse * not IGP. 268d594e46aSJerome Glisse * 269d594e46aSJerome Glisse * Note: we use mc_vram_size as on some board we need to program the mc to 270d594e46aSJerome Glisse * cover the whole aperture even if VRAM size is inferior to aperture size 271d594e46aSJerome Glisse * Novell bug 204882 + along with lots of ubuntu ones 272d594e46aSJerome Glisse * 273d594e46aSJerome Glisse * Note: when limiting vram it's safe to overwritte real_vram_size because 274d594e46aSJerome Glisse * we are not in case where real_vram_size is inferior to mc_vram_size (ie 275d594e46aSJerome Glisse * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu 276d594e46aSJerome Glisse * ones) 277d594e46aSJerome Glisse * 278d594e46aSJerome Glisse * Note: IGP TOM addr should be the same as the aperture addr, we don't 279d594e46aSJerome Glisse * explicitly check for that thought. 280d594e46aSJerome Glisse * 281d594e46aSJerome Glisse * FIXME: when reducing VRAM size align new size on power of 2. 282771fe6b9SJerome Glisse */ 283d594e46aSJerome Glisse void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base) 284771fe6b9SJerome Glisse { 285d594e46aSJerome Glisse mc->vram_start = base; 286d594e46aSJerome Glisse if (mc->mc_vram_size > (0xFFFFFFFF - base + 1)) { 287d594e46aSJerome Glisse dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n"); 288d594e46aSJerome Glisse mc->real_vram_size = mc->aper_size; 289d594e46aSJerome Glisse mc->mc_vram_size = mc->aper_size; 290771fe6b9SJerome Glisse } 291d594e46aSJerome Glisse mc->vram_end = mc->vram_start + mc->mc_vram_size - 1; 2922cbeb4efSJerome Glisse if (rdev->flags & RADEON_IS_AGP && mc->vram_end > mc->gtt_start && mc->vram_start <= mc->gtt_end) { 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; 298dd7cc55aSAlex Deucher dev_info(rdev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n", 299d594e46aSJerome Glisse mc->mc_vram_size >> 20, mc->vram_start, 300d594e46aSJerome Glisse mc->vram_end, mc->real_vram_size >> 20); 301771fe6b9SJerome Glisse } 302771fe6b9SJerome Glisse 303d594e46aSJerome Glisse /** 304d594e46aSJerome Glisse * radeon_gtt_location - try to find GTT location 305d594e46aSJerome Glisse * @rdev: radeon device structure holding all necessary informations 306d594e46aSJerome Glisse * @mc: memory controller structure holding memory informations 307d594e46aSJerome Glisse * 308d594e46aSJerome Glisse * Function will place try to place GTT before or after VRAM. 309d594e46aSJerome Glisse * 310d594e46aSJerome Glisse * If GTT size is bigger than space left then we ajust GTT size. 311d594e46aSJerome Glisse * Thus function will never fails. 312d594e46aSJerome Glisse * 313d594e46aSJerome Glisse * FIXME: when reducing GTT size align new size on power of 2. 314d594e46aSJerome Glisse */ 315d594e46aSJerome Glisse void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc) 316d594e46aSJerome Glisse { 317d594e46aSJerome Glisse u64 size_af, size_bf; 318d594e46aSJerome Glisse 3198d369bb1SAlex Deucher size_af = ((0xFFFFFFFF - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align; 3208d369bb1SAlex Deucher size_bf = mc->vram_start & ~mc->gtt_base_align; 321d594e46aSJerome Glisse if (size_bf > size_af) { 322d594e46aSJerome Glisse if (mc->gtt_size > size_bf) { 323d594e46aSJerome Glisse dev_warn(rdev->dev, "limiting GTT\n"); 324d594e46aSJerome Glisse mc->gtt_size = size_bf; 325d594e46aSJerome Glisse } 3268d369bb1SAlex Deucher mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size; 327d594e46aSJerome Glisse } else { 328d594e46aSJerome Glisse if (mc->gtt_size > size_af) { 329d594e46aSJerome Glisse dev_warn(rdev->dev, "limiting GTT\n"); 330d594e46aSJerome Glisse mc->gtt_size = size_af; 331d594e46aSJerome Glisse } 3328d369bb1SAlex Deucher mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align; 333d594e46aSJerome Glisse } 334d594e46aSJerome Glisse mc->gtt_end = mc->gtt_start + mc->gtt_size - 1; 335dd7cc55aSAlex Deucher dev_info(rdev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n", 336d594e46aSJerome Glisse mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end); 337d594e46aSJerome Glisse } 338771fe6b9SJerome Glisse 339771fe6b9SJerome Glisse /* 340771fe6b9SJerome Glisse * GPU helpers function. 341771fe6b9SJerome Glisse */ 3429f022ddfSJerome Glisse bool radeon_card_posted(struct radeon_device *rdev) 343771fe6b9SJerome Glisse { 344771fe6b9SJerome Glisse uint32_t reg; 345771fe6b9SJerome Glisse 346771fe6b9SJerome Glisse /* first check CRTCs */ 34718007401SAlex Deucher if (ASIC_IS_DCE41(rdev)) { 34818007401SAlex Deucher reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) | 34918007401SAlex Deucher RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET); 35018007401SAlex Deucher if (reg & EVERGREEN_CRTC_MASTER_EN) 35118007401SAlex Deucher return true; 35218007401SAlex Deucher } else if (ASIC_IS_DCE4(rdev)) { 353bcc1c2a1SAlex Deucher reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) | 354bcc1c2a1SAlex Deucher RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET) | 355bcc1c2a1SAlex Deucher RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) | 356bcc1c2a1SAlex Deucher RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET) | 357bcc1c2a1SAlex Deucher RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) | 358bcc1c2a1SAlex Deucher RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET); 359bcc1c2a1SAlex Deucher if (reg & EVERGREEN_CRTC_MASTER_EN) 360bcc1c2a1SAlex Deucher return true; 361bcc1c2a1SAlex Deucher } else if (ASIC_IS_AVIVO(rdev)) { 362771fe6b9SJerome Glisse reg = RREG32(AVIVO_D1CRTC_CONTROL) | 363771fe6b9SJerome Glisse RREG32(AVIVO_D2CRTC_CONTROL); 364771fe6b9SJerome Glisse if (reg & AVIVO_CRTC_EN) { 365771fe6b9SJerome Glisse return true; 366771fe6b9SJerome Glisse } 367771fe6b9SJerome Glisse } else { 368771fe6b9SJerome Glisse reg = RREG32(RADEON_CRTC_GEN_CNTL) | 369771fe6b9SJerome Glisse RREG32(RADEON_CRTC2_GEN_CNTL); 370771fe6b9SJerome Glisse if (reg & RADEON_CRTC_EN) { 371771fe6b9SJerome Glisse return true; 372771fe6b9SJerome Glisse } 373771fe6b9SJerome Glisse } 374771fe6b9SJerome Glisse 375771fe6b9SJerome Glisse /* then check MEM_SIZE, in case the crtcs are off */ 376771fe6b9SJerome Glisse if (rdev->family >= CHIP_R600) 377771fe6b9SJerome Glisse reg = RREG32(R600_CONFIG_MEMSIZE); 378771fe6b9SJerome Glisse else 379771fe6b9SJerome Glisse reg = RREG32(RADEON_CONFIG_MEMSIZE); 380771fe6b9SJerome Glisse 381771fe6b9SJerome Glisse if (reg) 382771fe6b9SJerome Glisse return true; 383771fe6b9SJerome Glisse 384771fe6b9SJerome Glisse return false; 385771fe6b9SJerome Glisse 386771fe6b9SJerome Glisse } 387771fe6b9SJerome Glisse 388f47299c5SAlex Deucher void radeon_update_bandwidth_info(struct radeon_device *rdev) 389f47299c5SAlex Deucher { 390f47299c5SAlex Deucher fixed20_12 a; 3918807286eSAlex Deucher u32 sclk = rdev->pm.current_sclk; 3928807286eSAlex Deucher u32 mclk = rdev->pm.current_mclk; 393f47299c5SAlex Deucher 3948807286eSAlex Deucher /* sclk/mclk in Mhz */ 39568adac5eSBen Skeggs a.full = dfixed_const(100); 39668adac5eSBen Skeggs rdev->pm.sclk.full = dfixed_const(sclk); 39768adac5eSBen Skeggs rdev->pm.sclk.full = dfixed_div(rdev->pm.sclk, a); 39868adac5eSBen Skeggs rdev->pm.mclk.full = dfixed_const(mclk); 39968adac5eSBen Skeggs rdev->pm.mclk.full = dfixed_div(rdev->pm.mclk, a); 400f47299c5SAlex Deucher 4018807286eSAlex Deucher if (rdev->flags & RADEON_IS_IGP) { 40268adac5eSBen Skeggs a.full = dfixed_const(16); 403f47299c5SAlex Deucher /* core_bandwidth = sclk(Mhz) * 16 */ 40468adac5eSBen Skeggs rdev->pm.core_bandwidth.full = dfixed_div(rdev->pm.sclk, a); 405f47299c5SAlex Deucher } 406f47299c5SAlex Deucher } 407f47299c5SAlex Deucher 40872542d77SDave Airlie bool radeon_boot_test_post_card(struct radeon_device *rdev) 40972542d77SDave Airlie { 41072542d77SDave Airlie if (radeon_card_posted(rdev)) 41172542d77SDave Airlie return true; 41272542d77SDave Airlie 41372542d77SDave Airlie if (rdev->bios) { 41472542d77SDave Airlie DRM_INFO("GPU not posted. posting now...\n"); 41572542d77SDave Airlie if (rdev->is_atom_bios) 41672542d77SDave Airlie atom_asic_init(rdev->mode_info.atom_context); 41772542d77SDave Airlie else 41872542d77SDave Airlie radeon_combios_asic_init(rdev->ddev); 41972542d77SDave Airlie return true; 42072542d77SDave Airlie } else { 42172542d77SDave Airlie dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n"); 42272542d77SDave Airlie return false; 42372542d77SDave Airlie } 42472542d77SDave Airlie } 42572542d77SDave Airlie 4263ce0a23dSJerome Glisse int radeon_dummy_page_init(struct radeon_device *rdev) 4273ce0a23dSJerome Glisse { 42882568565SDave Airlie if (rdev->dummy_page.page) 42982568565SDave Airlie return 0; 4303ce0a23dSJerome Glisse rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO); 4313ce0a23dSJerome Glisse if (rdev->dummy_page.page == NULL) 4323ce0a23dSJerome Glisse return -ENOMEM; 4333ce0a23dSJerome Glisse rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page, 4343ce0a23dSJerome Glisse 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); 435a30f6fb7SBenjamin Herrenschmidt if (pci_dma_mapping_error(rdev->pdev, rdev->dummy_page.addr)) { 436a30f6fb7SBenjamin Herrenschmidt dev_err(&rdev->pdev->dev, "Failed to DMA MAP the dummy page\n"); 4373ce0a23dSJerome Glisse __free_page(rdev->dummy_page.page); 4383ce0a23dSJerome Glisse rdev->dummy_page.page = NULL; 4393ce0a23dSJerome Glisse return -ENOMEM; 4403ce0a23dSJerome Glisse } 4413ce0a23dSJerome Glisse return 0; 4423ce0a23dSJerome Glisse } 4433ce0a23dSJerome Glisse 4443ce0a23dSJerome Glisse void radeon_dummy_page_fini(struct radeon_device *rdev) 4453ce0a23dSJerome Glisse { 4463ce0a23dSJerome Glisse if (rdev->dummy_page.page == NULL) 4473ce0a23dSJerome Glisse return; 4483ce0a23dSJerome Glisse pci_unmap_page(rdev->pdev, rdev->dummy_page.addr, 4493ce0a23dSJerome Glisse PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); 4503ce0a23dSJerome Glisse __free_page(rdev->dummy_page.page); 4513ce0a23dSJerome Glisse rdev->dummy_page.page = NULL; 4523ce0a23dSJerome Glisse } 4533ce0a23dSJerome Glisse 454771fe6b9SJerome Glisse 455771fe6b9SJerome Glisse /* ATOM accessor methods */ 456771fe6b9SJerome Glisse static uint32_t cail_pll_read(struct card_info *info, uint32_t reg) 457771fe6b9SJerome Glisse { 458771fe6b9SJerome Glisse struct radeon_device *rdev = info->dev->dev_private; 459771fe6b9SJerome Glisse uint32_t r; 460771fe6b9SJerome Glisse 461771fe6b9SJerome Glisse r = rdev->pll_rreg(rdev, reg); 462771fe6b9SJerome Glisse return r; 463771fe6b9SJerome Glisse } 464771fe6b9SJerome Glisse 465771fe6b9SJerome Glisse static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val) 466771fe6b9SJerome Glisse { 467771fe6b9SJerome Glisse struct radeon_device *rdev = info->dev->dev_private; 468771fe6b9SJerome Glisse 469771fe6b9SJerome Glisse rdev->pll_wreg(rdev, reg, val); 470771fe6b9SJerome Glisse } 471771fe6b9SJerome Glisse 472771fe6b9SJerome Glisse static uint32_t cail_mc_read(struct card_info *info, uint32_t reg) 473771fe6b9SJerome Glisse { 474771fe6b9SJerome Glisse struct radeon_device *rdev = info->dev->dev_private; 475771fe6b9SJerome Glisse uint32_t r; 476771fe6b9SJerome Glisse 477771fe6b9SJerome Glisse r = rdev->mc_rreg(rdev, reg); 478771fe6b9SJerome Glisse return r; 479771fe6b9SJerome Glisse } 480771fe6b9SJerome Glisse 481771fe6b9SJerome Glisse static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val) 482771fe6b9SJerome Glisse { 483771fe6b9SJerome Glisse struct radeon_device *rdev = info->dev->dev_private; 484771fe6b9SJerome Glisse 485771fe6b9SJerome Glisse rdev->mc_wreg(rdev, reg, val); 486771fe6b9SJerome Glisse } 487771fe6b9SJerome Glisse 488771fe6b9SJerome Glisse static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val) 489771fe6b9SJerome Glisse { 490771fe6b9SJerome Glisse struct radeon_device *rdev = info->dev->dev_private; 491771fe6b9SJerome Glisse 492771fe6b9SJerome Glisse WREG32(reg*4, val); 493771fe6b9SJerome Glisse } 494771fe6b9SJerome Glisse 495771fe6b9SJerome Glisse static uint32_t cail_reg_read(struct card_info *info, uint32_t reg) 496771fe6b9SJerome Glisse { 497771fe6b9SJerome Glisse struct radeon_device *rdev = info->dev->dev_private; 498771fe6b9SJerome Glisse uint32_t r; 499771fe6b9SJerome Glisse 500771fe6b9SJerome Glisse r = RREG32(reg*4); 501771fe6b9SJerome Glisse return r; 502771fe6b9SJerome Glisse } 503771fe6b9SJerome Glisse 504351a52a2SAlex Deucher static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val) 505351a52a2SAlex Deucher { 506351a52a2SAlex Deucher struct radeon_device *rdev = info->dev->dev_private; 507351a52a2SAlex Deucher 508351a52a2SAlex Deucher WREG32_IO(reg*4, val); 509351a52a2SAlex Deucher } 510351a52a2SAlex Deucher 511351a52a2SAlex Deucher static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg) 512351a52a2SAlex Deucher { 513351a52a2SAlex Deucher struct radeon_device *rdev = info->dev->dev_private; 514351a52a2SAlex Deucher uint32_t r; 515351a52a2SAlex Deucher 516351a52a2SAlex Deucher r = RREG32_IO(reg*4); 517351a52a2SAlex Deucher return r; 518351a52a2SAlex Deucher } 519351a52a2SAlex Deucher 520771fe6b9SJerome Glisse int radeon_atombios_init(struct radeon_device *rdev) 521771fe6b9SJerome Glisse { 52261c4b24bSMathias Fröhlich struct card_info *atom_card_info = 52361c4b24bSMathias Fröhlich kzalloc(sizeof(struct card_info), GFP_KERNEL); 52461c4b24bSMathias Fröhlich 52561c4b24bSMathias Fröhlich if (!atom_card_info) 52661c4b24bSMathias Fröhlich return -ENOMEM; 52761c4b24bSMathias Fröhlich 52861c4b24bSMathias Fröhlich rdev->mode_info.atom_card_info = atom_card_info; 52961c4b24bSMathias Fröhlich atom_card_info->dev = rdev->ddev; 53061c4b24bSMathias Fröhlich atom_card_info->reg_read = cail_reg_read; 53161c4b24bSMathias Fröhlich atom_card_info->reg_write = cail_reg_write; 532351a52a2SAlex Deucher /* needed for iio ops */ 533351a52a2SAlex Deucher if (rdev->rio_mem) { 534351a52a2SAlex Deucher atom_card_info->ioreg_read = cail_ioreg_read; 535351a52a2SAlex Deucher atom_card_info->ioreg_write = cail_ioreg_write; 536351a52a2SAlex Deucher } else { 537351a52a2SAlex Deucher DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n"); 538351a52a2SAlex Deucher atom_card_info->ioreg_read = cail_reg_read; 539351a52a2SAlex Deucher atom_card_info->ioreg_write = cail_reg_write; 540351a52a2SAlex Deucher } 54161c4b24bSMathias Fröhlich atom_card_info->mc_read = cail_mc_read; 54261c4b24bSMathias Fröhlich atom_card_info->mc_write = cail_mc_write; 54361c4b24bSMathias Fröhlich atom_card_info->pll_read = cail_pll_read; 54461c4b24bSMathias Fröhlich atom_card_info->pll_write = cail_pll_write; 54561c4b24bSMathias Fröhlich 54661c4b24bSMathias Fröhlich rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios); 547c31ad97fSRafał Miłecki mutex_init(&rdev->mode_info.atom_context->mutex); 548771fe6b9SJerome Glisse radeon_atom_initialize_bios_scratch_regs(rdev->ddev); 549d904ef9bSDave Airlie atom_allocate_fb_scratch(rdev->mode_info.atom_context); 550771fe6b9SJerome Glisse return 0; 551771fe6b9SJerome Glisse } 552771fe6b9SJerome Glisse 553771fe6b9SJerome Glisse void radeon_atombios_fini(struct radeon_device *rdev) 554771fe6b9SJerome Glisse { 5554a04a844SJerome Glisse if (rdev->mode_info.atom_context) { 556d904ef9bSDave Airlie kfree(rdev->mode_info.atom_context->scratch); 557771fe6b9SJerome Glisse kfree(rdev->mode_info.atom_context); 5584a04a844SJerome Glisse } 55961c4b24bSMathias Fröhlich kfree(rdev->mode_info.atom_card_info); 560771fe6b9SJerome Glisse } 561771fe6b9SJerome Glisse 562771fe6b9SJerome Glisse int radeon_combios_init(struct radeon_device *rdev) 563771fe6b9SJerome Glisse { 564771fe6b9SJerome Glisse radeon_combios_initialize_bios_scratch_regs(rdev->ddev); 565771fe6b9SJerome Glisse return 0; 566771fe6b9SJerome Glisse } 567771fe6b9SJerome Glisse 568771fe6b9SJerome Glisse void radeon_combios_fini(struct radeon_device *rdev) 569771fe6b9SJerome Glisse { 570771fe6b9SJerome Glisse } 571771fe6b9SJerome Glisse 57228d52043SDave Airlie /* if we get transitioned to only one device, tak VGA back */ 57328d52043SDave Airlie static unsigned int radeon_vga_set_decode(void *cookie, bool state) 57428d52043SDave Airlie { 57528d52043SDave Airlie struct radeon_device *rdev = cookie; 57628d52043SDave Airlie radeon_vga_set_state(rdev, state); 57728d52043SDave Airlie if (state) 57828d52043SDave Airlie return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM | 57928d52043SDave Airlie VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; 58028d52043SDave Airlie else 58128d52043SDave Airlie return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; 58228d52043SDave Airlie } 583c1176d6fSDave Airlie 58436421338SJerome Glisse void radeon_check_arguments(struct radeon_device *rdev) 58536421338SJerome Glisse { 58636421338SJerome Glisse /* vramlimit must be a power of two */ 58736421338SJerome Glisse switch (radeon_vram_limit) { 58836421338SJerome Glisse case 0: 58936421338SJerome Glisse case 4: 59036421338SJerome Glisse case 8: 59136421338SJerome Glisse case 16: 59236421338SJerome Glisse case 32: 59336421338SJerome Glisse case 64: 59436421338SJerome Glisse case 128: 59536421338SJerome Glisse case 256: 59636421338SJerome Glisse case 512: 59736421338SJerome Glisse case 1024: 59836421338SJerome Glisse case 2048: 59936421338SJerome Glisse case 4096: 60036421338SJerome Glisse break; 60136421338SJerome Glisse default: 60236421338SJerome Glisse dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n", 60336421338SJerome Glisse radeon_vram_limit); 60436421338SJerome Glisse radeon_vram_limit = 0; 60536421338SJerome Glisse break; 60636421338SJerome Glisse } 60736421338SJerome Glisse radeon_vram_limit = radeon_vram_limit << 20; 60836421338SJerome Glisse /* gtt size must be power of two and greater or equal to 32M */ 60936421338SJerome Glisse switch (radeon_gart_size) { 61036421338SJerome Glisse case 4: 61136421338SJerome Glisse case 8: 61236421338SJerome Glisse case 16: 61336421338SJerome Glisse dev_warn(rdev->dev, "gart size (%d) too small forcing to 512M\n", 61436421338SJerome Glisse radeon_gart_size); 61536421338SJerome Glisse radeon_gart_size = 512; 61636421338SJerome Glisse break; 61736421338SJerome Glisse case 32: 61836421338SJerome Glisse case 64: 61936421338SJerome Glisse case 128: 62036421338SJerome Glisse case 256: 62136421338SJerome Glisse case 512: 62236421338SJerome Glisse case 1024: 62336421338SJerome Glisse case 2048: 62436421338SJerome Glisse case 4096: 62536421338SJerome Glisse break; 62636421338SJerome Glisse default: 62736421338SJerome Glisse dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n", 62836421338SJerome Glisse radeon_gart_size); 62936421338SJerome Glisse radeon_gart_size = 512; 63036421338SJerome Glisse break; 63136421338SJerome Glisse } 63236421338SJerome Glisse rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024; 63336421338SJerome Glisse /* AGP mode can only be -1, 1, 2, 4, 8 */ 63436421338SJerome Glisse switch (radeon_agpmode) { 63536421338SJerome Glisse case -1: 63636421338SJerome Glisse case 0: 63736421338SJerome Glisse case 1: 63836421338SJerome Glisse case 2: 63936421338SJerome Glisse case 4: 64036421338SJerome Glisse case 8: 64136421338SJerome Glisse break; 64236421338SJerome Glisse default: 64336421338SJerome Glisse dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: " 64436421338SJerome Glisse "-1, 0, 1, 2, 4, 8)\n", radeon_agpmode); 64536421338SJerome Glisse radeon_agpmode = 0; 64636421338SJerome Glisse break; 64736421338SJerome Glisse } 64836421338SJerome Glisse } 64936421338SJerome Glisse 6506a9ee8afSDave Airlie static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state) 6516a9ee8afSDave Airlie { 6526a9ee8afSDave Airlie struct drm_device *dev = pci_get_drvdata(pdev); 6536a9ee8afSDave Airlie pm_message_t pmm = { .event = PM_EVENT_SUSPEND }; 6546a9ee8afSDave Airlie if (state == VGA_SWITCHEROO_ON) { 6556a9ee8afSDave Airlie printk(KERN_INFO "radeon: switched on\n"); 6566a9ee8afSDave Airlie /* don't suspend or resume card normally */ 6575bcf719bSDave Airlie dev->switch_power_state = DRM_SWITCH_POWER_CHANGING; 6586a9ee8afSDave Airlie radeon_resume_kms(dev); 6595bcf719bSDave Airlie dev->switch_power_state = DRM_SWITCH_POWER_ON; 660fbf81762SDave Airlie drm_kms_helper_poll_enable(dev); 6616a9ee8afSDave Airlie } else { 6626a9ee8afSDave Airlie printk(KERN_INFO "radeon: switched off\n"); 663fbf81762SDave Airlie drm_kms_helper_poll_disable(dev); 6645bcf719bSDave Airlie dev->switch_power_state = DRM_SWITCH_POWER_CHANGING; 6656a9ee8afSDave Airlie radeon_suspend_kms(dev, pmm); 6665bcf719bSDave Airlie dev->switch_power_state = DRM_SWITCH_POWER_OFF; 6676a9ee8afSDave Airlie } 6686a9ee8afSDave Airlie } 6696a9ee8afSDave Airlie 6706a9ee8afSDave Airlie static bool radeon_switcheroo_can_switch(struct pci_dev *pdev) 6716a9ee8afSDave Airlie { 6726a9ee8afSDave Airlie struct drm_device *dev = pci_get_drvdata(pdev); 6736a9ee8afSDave Airlie bool can_switch; 6746a9ee8afSDave Airlie 6756a9ee8afSDave Airlie spin_lock(&dev->count_lock); 6766a9ee8afSDave Airlie can_switch = (dev->open_count == 0); 6776a9ee8afSDave Airlie spin_unlock(&dev->count_lock); 6786a9ee8afSDave Airlie return can_switch; 6796a9ee8afSDave Airlie } 6806a9ee8afSDave Airlie 6816a9ee8afSDave Airlie 682771fe6b9SJerome Glisse int radeon_device_init(struct radeon_device *rdev, 683771fe6b9SJerome Glisse struct drm_device *ddev, 684771fe6b9SJerome Glisse struct pci_dev *pdev, 685771fe6b9SJerome Glisse uint32_t flags) 686771fe6b9SJerome Glisse { 687351a52a2SAlex Deucher int r, i; 688ad49f501SDave Airlie int dma_bits; 689771fe6b9SJerome Glisse 690771fe6b9SJerome Glisse rdev->shutdown = false; 6919f022ddfSJerome Glisse rdev->dev = &pdev->dev; 692771fe6b9SJerome Glisse rdev->ddev = ddev; 693771fe6b9SJerome Glisse rdev->pdev = pdev; 694771fe6b9SJerome Glisse rdev->flags = flags; 695771fe6b9SJerome Glisse rdev->family = flags & RADEON_FAMILY_MASK; 696771fe6b9SJerome Glisse rdev->is_atom_bios = false; 697771fe6b9SJerome Glisse rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT; 698771fe6b9SJerome Glisse rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024; 699771fe6b9SJerome Glisse rdev->gpu_lockup = false; 700733289c2SJerome Glisse rdev->accel_working = false; 7011b5331d9SJerome Glisse 7021b5331d9SJerome Glisse DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X).\n", 7031b5331d9SJerome Glisse radeon_family_name[rdev->family], pdev->vendor, pdev->device); 7041b5331d9SJerome Glisse 705771fe6b9SJerome Glisse /* mutex initialization are all done here so we 706771fe6b9SJerome Glisse * can recall function without having locking issues */ 707771fe6b9SJerome Glisse mutex_init(&rdev->cs_mutex); 708771fe6b9SJerome Glisse mutex_init(&rdev->ib_pool.mutex); 709771fe6b9SJerome Glisse mutex_init(&rdev->cp.mutex); 71040bacf16SAlex Deucher mutex_init(&rdev->dc_hw_i2c_mutex); 711d8f60cfcSAlex Deucher if (rdev->family >= CHIP_R600) 712d8f60cfcSAlex Deucher spin_lock_init(&rdev->ih.lock); 7134c788679SJerome Glisse mutex_init(&rdev->gem.mutex); 714c913e23aSRafał Miłecki mutex_init(&rdev->pm.mutex); 7155876dd24SMatthew Garrett mutex_init(&rdev->vram_mutex); 716771fe6b9SJerome Glisse rwlock_init(&rdev->fence_drv.lock); 7179f022ddfSJerome Glisse INIT_LIST_HEAD(&rdev->gem.objects); 71873a6d3fcSRafał Miłecki init_waitqueue_head(&rdev->irq.vblank_queue); 7192031f77cSAlex Deucher init_waitqueue_head(&rdev->irq.idle_queue); 720771fe6b9SJerome Glisse 7214aac0473SJerome Glisse /* Set asic functions */ 7224aac0473SJerome Glisse r = radeon_asic_init(rdev); 72336421338SJerome Glisse if (r) 7244aac0473SJerome Glisse return r; 72536421338SJerome Glisse radeon_check_arguments(rdev); 7264aac0473SJerome Glisse 727f95df9caSAlex Deucher /* all of the newer IGP chips have an internal gart 728f95df9caSAlex Deucher * However some rs4xx report as AGP, so remove that here. 729f95df9caSAlex Deucher */ 730f95df9caSAlex Deucher if ((rdev->family >= CHIP_RS400) && 731f95df9caSAlex Deucher (rdev->flags & RADEON_IS_IGP)) { 732f95df9caSAlex Deucher rdev->flags &= ~RADEON_IS_AGP; 733f95df9caSAlex Deucher } 734f95df9caSAlex Deucher 73530256a3fSJerome Glisse if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) { 736b574f251SJerome Glisse radeon_agp_disable(rdev); 737771fe6b9SJerome Glisse } 738771fe6b9SJerome Glisse 739ad49f501SDave Airlie /* set DMA mask + need_dma32 flags. 740ad49f501SDave Airlie * PCIE - can handle 40-bits. 741ad49f501SDave Airlie * IGP - can handle 40-bits (in theory) 742ad49f501SDave Airlie * AGP - generally dma32 is safest 743ad49f501SDave Airlie * PCI - only dma32 744ad49f501SDave Airlie */ 745ad49f501SDave Airlie rdev->need_dma32 = false; 746ad49f501SDave Airlie if (rdev->flags & RADEON_IS_AGP) 747ad49f501SDave Airlie rdev->need_dma32 = true; 748ad49f501SDave Airlie if (rdev->flags & RADEON_IS_PCI) 749ad49f501SDave Airlie rdev->need_dma32 = true; 750ad49f501SDave Airlie 751ad49f501SDave Airlie dma_bits = rdev->need_dma32 ? 32 : 40; 752ad49f501SDave Airlie r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits)); 753771fe6b9SJerome Glisse if (r) { 754771fe6b9SJerome Glisse printk(KERN_WARNING "radeon: No suitable DMA available.\n"); 755771fe6b9SJerome Glisse } 756771fe6b9SJerome Glisse 757771fe6b9SJerome Glisse /* Registers mapping */ 758771fe6b9SJerome Glisse /* TODO: block userspace mapping of io register */ 75901d73a69SJordan Crouse rdev->rmmio_base = pci_resource_start(rdev->pdev, 2); 76001d73a69SJordan Crouse rdev->rmmio_size = pci_resource_len(rdev->pdev, 2); 761771fe6b9SJerome Glisse rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size); 762771fe6b9SJerome Glisse if (rdev->rmmio == NULL) { 763771fe6b9SJerome Glisse return -ENOMEM; 764771fe6b9SJerome Glisse } 765771fe6b9SJerome Glisse DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base); 766771fe6b9SJerome Glisse DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size); 767771fe6b9SJerome Glisse 768351a52a2SAlex Deucher /* io port mapping */ 769351a52a2SAlex Deucher for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { 770351a52a2SAlex Deucher if (pci_resource_flags(rdev->pdev, i) & IORESOURCE_IO) { 771351a52a2SAlex Deucher rdev->rio_mem_size = pci_resource_len(rdev->pdev, i); 772351a52a2SAlex Deucher rdev->rio_mem = pci_iomap(rdev->pdev, i, rdev->rio_mem_size); 773351a52a2SAlex Deucher break; 774351a52a2SAlex Deucher } 775351a52a2SAlex Deucher } 776351a52a2SAlex Deucher if (rdev->rio_mem == NULL) 777351a52a2SAlex Deucher DRM_ERROR("Unable to find PCI I/O BAR\n"); 778351a52a2SAlex Deucher 77928d52043SDave Airlie /* if we have > 1 VGA cards, then disable the radeon VGA resources */ 78093239ea1SDave Airlie /* this will fail for cards that aren't VGA class devices, just 78193239ea1SDave Airlie * ignore it */ 78293239ea1SDave Airlie vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode); 7836a9ee8afSDave Airlie vga_switcheroo_register_client(rdev->pdev, 7846a9ee8afSDave Airlie radeon_switcheroo_set_state, 7858d608aa6SDave Airlie NULL, 7866a9ee8afSDave Airlie radeon_switcheroo_can_switch); 78728d52043SDave Airlie 7883ce0a23dSJerome Glisse r = radeon_init(rdev); 789b574f251SJerome Glisse if (r) 790b574f251SJerome Glisse return r; 791b1e3a6d1SMichel Dänzer 792b574f251SJerome Glisse if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) { 793b574f251SJerome Glisse /* Acceleration not working on AGP card try again 794b574f251SJerome Glisse * with fallback to PCI or PCIE GART 795b574f251SJerome Glisse */ 796a2d07b74SJerome Glisse radeon_asic_reset(rdev); 797b574f251SJerome Glisse radeon_fini(rdev); 798b574f251SJerome Glisse radeon_agp_disable(rdev); 799b574f251SJerome Glisse r = radeon_init(rdev); 8004aac0473SJerome Glisse if (r) 8014aac0473SJerome Glisse return r; 8023ce0a23dSJerome Glisse } 803ecc0b326SMichel Dänzer if (radeon_testing) { 804ecc0b326SMichel Dänzer radeon_test_moves(rdev); 805ecc0b326SMichel Dänzer } 806771fe6b9SJerome Glisse if (radeon_benchmarking) { 807771fe6b9SJerome Glisse radeon_benchmark(rdev); 808771fe6b9SJerome Glisse } 8096cf8a3f5SJerome Glisse return 0; 810771fe6b9SJerome Glisse } 811771fe6b9SJerome Glisse 812771fe6b9SJerome Glisse void radeon_device_fini(struct radeon_device *rdev) 813771fe6b9SJerome Glisse { 814771fe6b9SJerome Glisse DRM_INFO("radeon: finishing device.\n"); 815771fe6b9SJerome Glisse rdev->shutdown = true; 81690aca4d2SJerome Glisse /* evict vram memory */ 81790aca4d2SJerome Glisse radeon_bo_evict_vram(rdev); 8183ce0a23dSJerome Glisse radeon_fini(rdev); 8196a9ee8afSDave Airlie vga_switcheroo_unregister_client(rdev->pdev); 820c1176d6fSDave Airlie vga_client_register(rdev->pdev, NULL, NULL, NULL); 821e0a2ca73SAlex Deucher if (rdev->rio_mem) 822351a52a2SAlex Deucher pci_iounmap(rdev->pdev, rdev->rio_mem); 823351a52a2SAlex Deucher rdev->rio_mem = NULL; 824771fe6b9SJerome Glisse iounmap(rdev->rmmio); 825771fe6b9SJerome Glisse rdev->rmmio = NULL; 826771fe6b9SJerome Glisse } 827771fe6b9SJerome Glisse 828771fe6b9SJerome Glisse 829771fe6b9SJerome Glisse /* 830771fe6b9SJerome Glisse * Suspend & resume. 831771fe6b9SJerome Glisse */ 832771fe6b9SJerome Glisse int radeon_suspend_kms(struct drm_device *dev, pm_message_t state) 833771fe6b9SJerome Glisse { 834875c1866SDarren Jenkins struct radeon_device *rdev; 835771fe6b9SJerome Glisse struct drm_crtc *crtc; 836d8dcaa1dSAlex Deucher struct drm_connector *connector; 8374c788679SJerome Glisse int r; 838771fe6b9SJerome Glisse 839875c1866SDarren Jenkins if (dev == NULL || dev->dev_private == NULL) { 840771fe6b9SJerome Glisse return -ENODEV; 841771fe6b9SJerome Glisse } 842771fe6b9SJerome Glisse if (state.event == PM_EVENT_PRETHAW) { 843771fe6b9SJerome Glisse return 0; 844771fe6b9SJerome Glisse } 845875c1866SDarren Jenkins rdev = dev->dev_private; 846875c1866SDarren Jenkins 8475bcf719bSDave Airlie if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) 8486a9ee8afSDave Airlie return 0; 849d8dcaa1dSAlex Deucher 850d8dcaa1dSAlex Deucher /* turn off display hw */ 851d8dcaa1dSAlex Deucher list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 852d8dcaa1dSAlex Deucher drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF); 853d8dcaa1dSAlex Deucher } 854d8dcaa1dSAlex Deucher 855771fe6b9SJerome Glisse /* unpin the front buffers */ 856771fe6b9SJerome Glisse list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 857771fe6b9SJerome Glisse struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->fb); 8584c788679SJerome Glisse struct radeon_bo *robj; 859771fe6b9SJerome Glisse 860771fe6b9SJerome Glisse if (rfb == NULL || rfb->obj == NULL) { 861771fe6b9SJerome Glisse continue; 862771fe6b9SJerome Glisse } 863*7e4d15d9SDaniel Vetter robj = gem_to_radeon_bo(rfb->obj); 86438651674SDave Airlie /* don't unpin kernel fb objects */ 86538651674SDave Airlie if (!radeon_fbdev_robj_is_fb(rdev, robj)) { 8664c788679SJerome Glisse r = radeon_bo_reserve(robj, false); 86738651674SDave Airlie if (r == 0) { 8684c788679SJerome Glisse radeon_bo_unpin(robj); 8694c788679SJerome Glisse radeon_bo_unreserve(robj); 8704c788679SJerome Glisse } 871771fe6b9SJerome Glisse } 872771fe6b9SJerome Glisse } 873771fe6b9SJerome Glisse /* evict vram memory */ 8744c788679SJerome Glisse radeon_bo_evict_vram(rdev); 875771fe6b9SJerome Glisse /* wait for gpu to finish processing current batch */ 876771fe6b9SJerome Glisse radeon_fence_wait_last(rdev); 877771fe6b9SJerome Glisse 878f657c2a7SYang Zhao radeon_save_bios_scratch_regs(rdev); 879f657c2a7SYang Zhao 880ce8f5370SAlex Deucher radeon_pm_suspend(rdev); 8813ce0a23dSJerome Glisse radeon_suspend(rdev); 882d4877cf2SAlex Deucher radeon_hpd_fini(rdev); 883771fe6b9SJerome Glisse /* evict remaining vram memory */ 8844c788679SJerome Glisse radeon_bo_evict_vram(rdev); 885771fe6b9SJerome Glisse 88610b06122SJerome Glisse radeon_agp_suspend(rdev); 88710b06122SJerome Glisse 888771fe6b9SJerome Glisse pci_save_state(dev->pdev); 889771fe6b9SJerome Glisse if (state.event == PM_EVENT_SUSPEND) { 890771fe6b9SJerome Glisse /* Shut down the device */ 891771fe6b9SJerome Glisse pci_disable_device(dev->pdev); 892771fe6b9SJerome Glisse pci_set_power_state(dev->pdev, PCI_D3hot); 893771fe6b9SJerome Glisse } 894ac751efaSTorben Hohn console_lock(); 89538651674SDave Airlie radeon_fbdev_set_suspend(rdev, 1); 896ac751efaSTorben Hohn console_unlock(); 897771fe6b9SJerome Glisse return 0; 898771fe6b9SJerome Glisse } 899771fe6b9SJerome Glisse 900771fe6b9SJerome Glisse int radeon_resume_kms(struct drm_device *dev) 901771fe6b9SJerome Glisse { 90209bdf591SCedric Godin struct drm_connector *connector; 903771fe6b9SJerome Glisse struct radeon_device *rdev = dev->dev_private; 904771fe6b9SJerome Glisse 9055bcf719bSDave Airlie if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) 9066a9ee8afSDave Airlie return 0; 9076a9ee8afSDave Airlie 908ac751efaSTorben Hohn console_lock(); 909771fe6b9SJerome Glisse pci_set_power_state(dev->pdev, PCI_D0); 910771fe6b9SJerome Glisse pci_restore_state(dev->pdev); 911771fe6b9SJerome Glisse if (pci_enable_device(dev->pdev)) { 912ac751efaSTorben Hohn console_unlock(); 913771fe6b9SJerome Glisse return -1; 914771fe6b9SJerome Glisse } 915771fe6b9SJerome Glisse pci_set_master(dev->pdev); 9160ebf1717SDave Airlie /* resume AGP if in use */ 9170ebf1717SDave Airlie radeon_agp_resume(rdev); 9183ce0a23dSJerome Glisse radeon_resume(rdev); 919ce8f5370SAlex Deucher radeon_pm_resume(rdev); 920f657c2a7SYang Zhao radeon_restore_bios_scratch_regs(rdev); 92109bdf591SCedric Godin 92238651674SDave Airlie radeon_fbdev_set_suspend(rdev, 0); 923ac751efaSTorben Hohn console_unlock(); 924771fe6b9SJerome Glisse 925d4877cf2SAlex Deucher /* reset hpd state */ 926d4877cf2SAlex Deucher radeon_hpd_init(rdev); 927771fe6b9SJerome Glisse /* blat the mode back in */ 928771fe6b9SJerome Glisse drm_helper_resume_force_mode(dev); 929a93f344dSAlex Deucher /* turn on display hw */ 930a93f344dSAlex Deucher list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 931a93f344dSAlex Deucher drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON); 932a93f344dSAlex Deucher } 933771fe6b9SJerome Glisse return 0; 934771fe6b9SJerome Glisse } 935771fe6b9SJerome Glisse 93690aca4d2SJerome Glisse int radeon_gpu_reset(struct radeon_device *rdev) 93790aca4d2SJerome Glisse { 93890aca4d2SJerome Glisse int r; 93990aca4d2SJerome Glisse 94090aca4d2SJerome Glisse radeon_save_bios_scratch_regs(rdev); 94190aca4d2SJerome Glisse radeon_suspend(rdev); 94290aca4d2SJerome Glisse 94390aca4d2SJerome Glisse r = radeon_asic_reset(rdev); 94490aca4d2SJerome Glisse if (!r) { 94590aca4d2SJerome Glisse dev_info(rdev->dev, "GPU reset succeed\n"); 94690aca4d2SJerome Glisse radeon_resume(rdev); 94790aca4d2SJerome Glisse radeon_restore_bios_scratch_regs(rdev); 94890aca4d2SJerome Glisse drm_helper_resume_force_mode(rdev->ddev); 94990aca4d2SJerome Glisse return 0; 95090aca4d2SJerome Glisse } 95190aca4d2SJerome Glisse /* bad news, how to tell it to userspace ? */ 95290aca4d2SJerome Glisse dev_info(rdev->dev, "GPU reset failed\n"); 95390aca4d2SJerome Glisse return r; 95490aca4d2SJerome Glisse } 95590aca4d2SJerome Glisse 956771fe6b9SJerome Glisse 957771fe6b9SJerome Glisse /* 958771fe6b9SJerome Glisse * Debugfs 959771fe6b9SJerome Glisse */ 960771fe6b9SJerome Glisse struct radeon_debugfs { 961771fe6b9SJerome Glisse struct drm_info_list *files; 962771fe6b9SJerome Glisse unsigned num_files; 963771fe6b9SJerome Glisse }; 964771fe6b9SJerome Glisse static struct radeon_debugfs _radeon_debugfs[RADEON_DEBUGFS_MAX_NUM_FILES]; 965771fe6b9SJerome Glisse static unsigned _radeon_debugfs_count = 0; 966771fe6b9SJerome Glisse 967771fe6b9SJerome Glisse int radeon_debugfs_add_files(struct radeon_device *rdev, 968771fe6b9SJerome Glisse struct drm_info_list *files, 969771fe6b9SJerome Glisse unsigned nfiles) 970771fe6b9SJerome Glisse { 971771fe6b9SJerome Glisse unsigned i; 972771fe6b9SJerome Glisse 973771fe6b9SJerome Glisse for (i = 0; i < _radeon_debugfs_count; i++) { 974771fe6b9SJerome Glisse if (_radeon_debugfs[i].files == files) { 975771fe6b9SJerome Glisse /* Already registered */ 976771fe6b9SJerome Glisse return 0; 977771fe6b9SJerome Glisse } 978771fe6b9SJerome Glisse } 979771fe6b9SJerome Glisse if ((_radeon_debugfs_count + nfiles) > RADEON_DEBUGFS_MAX_NUM_FILES) { 980771fe6b9SJerome Glisse DRM_ERROR("Reached maximum number of debugfs files.\n"); 981771fe6b9SJerome Glisse DRM_ERROR("Report so we increase RADEON_DEBUGFS_MAX_NUM_FILES.\n"); 982771fe6b9SJerome Glisse return -EINVAL; 983771fe6b9SJerome Glisse } 984771fe6b9SJerome Glisse _radeon_debugfs[_radeon_debugfs_count].files = files; 985771fe6b9SJerome Glisse _radeon_debugfs[_radeon_debugfs_count].num_files = nfiles; 986771fe6b9SJerome Glisse _radeon_debugfs_count++; 987771fe6b9SJerome Glisse #if defined(CONFIG_DEBUG_FS) 988771fe6b9SJerome Glisse drm_debugfs_create_files(files, nfiles, 989771fe6b9SJerome Glisse rdev->ddev->control->debugfs_root, 990771fe6b9SJerome Glisse rdev->ddev->control); 991771fe6b9SJerome Glisse drm_debugfs_create_files(files, nfiles, 992771fe6b9SJerome Glisse rdev->ddev->primary->debugfs_root, 993771fe6b9SJerome Glisse rdev->ddev->primary); 994771fe6b9SJerome Glisse #endif 995771fe6b9SJerome Glisse return 0; 996771fe6b9SJerome Glisse } 997771fe6b9SJerome Glisse 998771fe6b9SJerome Glisse #if defined(CONFIG_DEBUG_FS) 999771fe6b9SJerome Glisse int radeon_debugfs_init(struct drm_minor *minor) 1000771fe6b9SJerome Glisse { 1001771fe6b9SJerome Glisse return 0; 1002771fe6b9SJerome Glisse } 1003771fe6b9SJerome Glisse 1004771fe6b9SJerome Glisse void radeon_debugfs_cleanup(struct drm_minor *minor) 1005771fe6b9SJerome Glisse { 1006771fe6b9SJerome Glisse unsigned i; 1007771fe6b9SJerome Glisse 1008771fe6b9SJerome Glisse for (i = 0; i < _radeon_debugfs_count; i++) { 1009771fe6b9SJerome Glisse drm_debugfs_remove_files(_radeon_debugfs[i].files, 1010771fe6b9SJerome Glisse _radeon_debugfs[i].num_files, minor); 1011771fe6b9SJerome Glisse } 1012771fe6b9SJerome Glisse } 1013771fe6b9SJerome Glisse #endif 1014