1771fe6b9SJerome Glisse /* 2771fe6b9SJerome Glisse * Copyright 2008 Advanced Micro Devices, Inc. 3771fe6b9SJerome Glisse * Copyright 2008 Red Hat Inc. 4771fe6b9SJerome Glisse * Copyright 2009 Jerome Glisse. 5771fe6b9SJerome Glisse * 6771fe6b9SJerome Glisse * Permission is hereby granted, free of charge, to any person obtaining a 7771fe6b9SJerome Glisse * copy of this software and associated documentation files (the "Software"), 8771fe6b9SJerome Glisse * to deal in the Software without restriction, including without limitation 9771fe6b9SJerome Glisse * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10771fe6b9SJerome Glisse * and/or sell copies of the Software, and to permit persons to whom the 11771fe6b9SJerome Glisse * Software is furnished to do so, subject to the following conditions: 12771fe6b9SJerome Glisse * 13771fe6b9SJerome Glisse * The above copyright notice and this permission notice shall be included in 14771fe6b9SJerome Glisse * all copies or substantial portions of the Software. 15771fe6b9SJerome Glisse * 16771fe6b9SJerome Glisse * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17771fe6b9SJerome Glisse * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18771fe6b9SJerome Glisse * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19771fe6b9SJerome Glisse * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20771fe6b9SJerome Glisse * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21771fe6b9SJerome Glisse * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22771fe6b9SJerome Glisse * OTHER DEALINGS IN THE SOFTWARE. 23771fe6b9SJerome Glisse * 24771fe6b9SJerome Glisse * Authors: Dave Airlie 25771fe6b9SJerome Glisse * Alex Deucher 26771fe6b9SJerome Glisse * Jerome Glisse 27771fe6b9SJerome Glisse */ 28771fe6b9SJerome Glisse #include <linux/console.h> 295a0e3ad6STejun Heo #include <linux/slab.h> 30771fe6b9SJerome Glisse #include <drm/drmP.h> 31771fe6b9SJerome Glisse #include <drm/drm_crtc_helper.h> 32771fe6b9SJerome Glisse #include <drm/radeon_drm.h> 3328d52043SDave Airlie #include <linux/vgaarb.h> 346a9ee8afSDave Airlie #include <linux/vga_switcheroo.h> 35bcc65fd8SMatthew Garrett #include <linux/efi.h> 36771fe6b9SJerome Glisse #include "radeon_reg.h" 37771fe6b9SJerome Glisse #include "radeon.h" 38771fe6b9SJerome Glisse #include "atom.h" 39771fe6b9SJerome Glisse 401b5331d9SJerome Glisse static const char radeon_family_name[][16] = { 411b5331d9SJerome Glisse "R100", 421b5331d9SJerome Glisse "RV100", 431b5331d9SJerome Glisse "RS100", 441b5331d9SJerome Glisse "RV200", 451b5331d9SJerome Glisse "RS200", 461b5331d9SJerome Glisse "R200", 471b5331d9SJerome Glisse "RV250", 481b5331d9SJerome Glisse "RS300", 491b5331d9SJerome Glisse "RV280", 501b5331d9SJerome Glisse "R300", 511b5331d9SJerome Glisse "R350", 521b5331d9SJerome Glisse "RV350", 531b5331d9SJerome Glisse "RV380", 541b5331d9SJerome Glisse "R420", 551b5331d9SJerome Glisse "R423", 561b5331d9SJerome Glisse "RV410", 571b5331d9SJerome Glisse "RS400", 581b5331d9SJerome Glisse "RS480", 591b5331d9SJerome Glisse "RS600", 601b5331d9SJerome Glisse "RS690", 611b5331d9SJerome Glisse "RS740", 621b5331d9SJerome Glisse "RV515", 631b5331d9SJerome Glisse "R520", 641b5331d9SJerome Glisse "RV530", 651b5331d9SJerome Glisse "RV560", 661b5331d9SJerome Glisse "RV570", 671b5331d9SJerome Glisse "R580", 681b5331d9SJerome Glisse "R600", 691b5331d9SJerome Glisse "RV610", 701b5331d9SJerome Glisse "RV630", 711b5331d9SJerome Glisse "RV670", 721b5331d9SJerome Glisse "RV620", 731b5331d9SJerome Glisse "RV635", 741b5331d9SJerome Glisse "RS780", 751b5331d9SJerome Glisse "RS880", 761b5331d9SJerome Glisse "RV770", 771b5331d9SJerome Glisse "RV730", 781b5331d9SJerome Glisse "RV710", 791b5331d9SJerome Glisse "RV740", 801b5331d9SJerome Glisse "CEDAR", 811b5331d9SJerome Glisse "REDWOOD", 821b5331d9SJerome Glisse "JUNIPER", 831b5331d9SJerome Glisse "CYPRESS", 841b5331d9SJerome Glisse "HEMLOCK", 85b08ebe7eSAlex Deucher "PALM", 864df64e65SAlex Deucher "SUMO", 874df64e65SAlex Deucher "SUMO2", 881fe18305SAlex Deucher "BARTS", 891fe18305SAlex Deucher "TURKS", 901fe18305SAlex Deucher "CAICOS", 91b7cfc9feSAlex Deucher "CAYMAN", 928848f759SAlex Deucher "ARUBA", 93cb28bb34SAlex Deucher "TAHITI", 94cb28bb34SAlex Deucher "PITCAIRN", 95cb28bb34SAlex Deucher "VERDE", 961b5331d9SJerome Glisse "LAST", 971b5331d9SJerome Glisse }; 981b5331d9SJerome Glisse 99771fe6b9SJerome Glisse /* 100b1e3a6d1SMichel Dänzer * Clear GPU surface registers. 101b1e3a6d1SMichel Dänzer */ 1023ce0a23dSJerome Glisse void radeon_surface_init(struct radeon_device *rdev) 103b1e3a6d1SMichel Dänzer { 104b1e3a6d1SMichel Dänzer /* FIXME: check this out */ 105b1e3a6d1SMichel Dänzer if (rdev->family < CHIP_R600) { 106b1e3a6d1SMichel Dänzer int i; 107b1e3a6d1SMichel Dänzer 108550e2d92SDave Airlie for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) { 109550e2d92SDave Airlie if (rdev->surface_regs[i].bo) 110550e2d92SDave Airlie radeon_bo_get_surface_reg(rdev->surface_regs[i].bo); 111550e2d92SDave Airlie else 112550e2d92SDave Airlie radeon_clear_surface_reg(rdev, i); 113b1e3a6d1SMichel Dänzer } 114e024e110SDave Airlie /* enable surfaces */ 115e024e110SDave Airlie WREG32(RADEON_SURFACE_CNTL, 0); 116b1e3a6d1SMichel Dänzer } 117b1e3a6d1SMichel Dänzer } 118b1e3a6d1SMichel Dänzer 119b1e3a6d1SMichel Dänzer /* 120771fe6b9SJerome Glisse * GPU scratch registers helpers function. 121771fe6b9SJerome Glisse */ 1223ce0a23dSJerome Glisse void radeon_scratch_init(struct radeon_device *rdev) 123771fe6b9SJerome Glisse { 124771fe6b9SJerome Glisse int i; 125771fe6b9SJerome Glisse 126771fe6b9SJerome Glisse /* FIXME: check this out */ 127771fe6b9SJerome Glisse if (rdev->family < CHIP_R300) { 128771fe6b9SJerome Glisse rdev->scratch.num_reg = 5; 129771fe6b9SJerome Glisse } else { 130771fe6b9SJerome Glisse rdev->scratch.num_reg = 7; 131771fe6b9SJerome Glisse } 132724c80e1SAlex Deucher rdev->scratch.reg_base = RADEON_SCRATCH_REG0; 133771fe6b9SJerome Glisse for (i = 0; i < rdev->scratch.num_reg; i++) { 134771fe6b9SJerome Glisse rdev->scratch.free[i] = true; 135724c80e1SAlex Deucher rdev->scratch.reg[i] = rdev->scratch.reg_base + (i * 4); 136771fe6b9SJerome Glisse } 137771fe6b9SJerome Glisse } 138771fe6b9SJerome Glisse 139771fe6b9SJerome Glisse int radeon_scratch_get(struct radeon_device *rdev, uint32_t *reg) 140771fe6b9SJerome Glisse { 141771fe6b9SJerome Glisse int i; 142771fe6b9SJerome Glisse 143771fe6b9SJerome Glisse for (i = 0; i < rdev->scratch.num_reg; i++) { 144771fe6b9SJerome Glisse if (rdev->scratch.free[i]) { 145771fe6b9SJerome Glisse rdev->scratch.free[i] = false; 146771fe6b9SJerome Glisse *reg = rdev->scratch.reg[i]; 147771fe6b9SJerome Glisse return 0; 148771fe6b9SJerome Glisse } 149771fe6b9SJerome Glisse } 150771fe6b9SJerome Glisse return -EINVAL; 151771fe6b9SJerome Glisse } 152771fe6b9SJerome Glisse 153771fe6b9SJerome Glisse void radeon_scratch_free(struct radeon_device *rdev, uint32_t reg) 154771fe6b9SJerome Glisse { 155771fe6b9SJerome Glisse int i; 156771fe6b9SJerome Glisse 157771fe6b9SJerome Glisse for (i = 0; i < rdev->scratch.num_reg; i++) { 158771fe6b9SJerome Glisse if (rdev->scratch.reg[i] == reg) { 159771fe6b9SJerome Glisse rdev->scratch.free[i] = true; 160771fe6b9SJerome Glisse return; 161771fe6b9SJerome Glisse } 162771fe6b9SJerome Glisse } 163771fe6b9SJerome Glisse } 164771fe6b9SJerome Glisse 165724c80e1SAlex Deucher void radeon_wb_disable(struct radeon_device *rdev) 166724c80e1SAlex Deucher { 167724c80e1SAlex Deucher int r; 168724c80e1SAlex Deucher 169724c80e1SAlex Deucher if (rdev->wb.wb_obj) { 170724c80e1SAlex Deucher r = radeon_bo_reserve(rdev->wb.wb_obj, false); 171724c80e1SAlex Deucher if (unlikely(r != 0)) 172724c80e1SAlex Deucher return; 173724c80e1SAlex Deucher radeon_bo_kunmap(rdev->wb.wb_obj); 174724c80e1SAlex Deucher radeon_bo_unpin(rdev->wb.wb_obj); 175724c80e1SAlex Deucher radeon_bo_unreserve(rdev->wb.wb_obj); 176724c80e1SAlex Deucher } 177724c80e1SAlex Deucher rdev->wb.enabled = false; 178724c80e1SAlex Deucher } 179724c80e1SAlex Deucher 180724c80e1SAlex Deucher void radeon_wb_fini(struct radeon_device *rdev) 181724c80e1SAlex Deucher { 182724c80e1SAlex Deucher radeon_wb_disable(rdev); 183724c80e1SAlex Deucher if (rdev->wb.wb_obj) { 184724c80e1SAlex Deucher radeon_bo_unref(&rdev->wb.wb_obj); 185724c80e1SAlex Deucher rdev->wb.wb = NULL; 186724c80e1SAlex Deucher rdev->wb.wb_obj = NULL; 187724c80e1SAlex Deucher } 188724c80e1SAlex Deucher } 189724c80e1SAlex Deucher 190724c80e1SAlex Deucher int radeon_wb_init(struct radeon_device *rdev) 191724c80e1SAlex Deucher { 192724c80e1SAlex Deucher int r; 193724c80e1SAlex Deucher 194724c80e1SAlex Deucher if (rdev->wb.wb_obj == NULL) { 195441921d5SDaniel Vetter r = radeon_bo_create(rdev, RADEON_GPU_PAGE_SIZE, PAGE_SIZE, true, 196724c80e1SAlex Deucher RADEON_GEM_DOMAIN_GTT, &rdev->wb.wb_obj); 197724c80e1SAlex Deucher if (r) { 198724c80e1SAlex Deucher dev_warn(rdev->dev, "(%d) create WB bo failed\n", r); 199724c80e1SAlex Deucher return r; 200724c80e1SAlex Deucher } 201724c80e1SAlex Deucher } 202724c80e1SAlex Deucher r = radeon_bo_reserve(rdev->wb.wb_obj, false); 203724c80e1SAlex Deucher if (unlikely(r != 0)) { 204724c80e1SAlex Deucher radeon_wb_fini(rdev); 205724c80e1SAlex Deucher return r; 206724c80e1SAlex Deucher } 207724c80e1SAlex Deucher r = radeon_bo_pin(rdev->wb.wb_obj, RADEON_GEM_DOMAIN_GTT, 208724c80e1SAlex Deucher &rdev->wb.gpu_addr); 209724c80e1SAlex Deucher if (r) { 210724c80e1SAlex Deucher radeon_bo_unreserve(rdev->wb.wb_obj); 211724c80e1SAlex Deucher dev_warn(rdev->dev, "(%d) pin WB bo failed\n", r); 212724c80e1SAlex Deucher radeon_wb_fini(rdev); 213724c80e1SAlex Deucher return r; 214724c80e1SAlex Deucher } 215724c80e1SAlex Deucher r = radeon_bo_kmap(rdev->wb.wb_obj, (void **)&rdev->wb.wb); 216724c80e1SAlex Deucher radeon_bo_unreserve(rdev->wb.wb_obj); 217724c80e1SAlex Deucher if (r) { 218724c80e1SAlex Deucher dev_warn(rdev->dev, "(%d) map WB bo failed\n", r); 219724c80e1SAlex Deucher radeon_wb_fini(rdev); 220724c80e1SAlex Deucher return r; 221724c80e1SAlex Deucher } 222724c80e1SAlex Deucher 223e6ba7599SAlex Deucher /* clear wb memory */ 224e6ba7599SAlex Deucher memset((char *)rdev->wb.wb, 0, RADEON_GPU_PAGE_SIZE); 225d0f8a854SAlex Deucher /* disable event_write fences */ 226d0f8a854SAlex Deucher rdev->wb.use_event = false; 227724c80e1SAlex Deucher /* disabled via module param */ 2283b7a2b24SJerome Glisse if (radeon_no_wb == 1) { 229724c80e1SAlex Deucher rdev->wb.enabled = false; 2303b7a2b24SJerome Glisse } else { 231724c80e1SAlex Deucher if (rdev->flags & RADEON_IS_AGP) { 23228eebb70SAlex Deucher /* often unreliable on AGP */ 23328eebb70SAlex Deucher rdev->wb.enabled = false; 23428eebb70SAlex Deucher } else if (rdev->family < CHIP_R300) { 23528eebb70SAlex Deucher /* often unreliable on pre-r300 */ 236724c80e1SAlex Deucher rdev->wb.enabled = false; 237d0f8a854SAlex Deucher } else { 238724c80e1SAlex Deucher rdev->wb.enabled = true; 239d0f8a854SAlex Deucher /* event_write fences are only available on r600+ */ 2403b7a2b24SJerome Glisse if (rdev->family >= CHIP_R600) { 241d0f8a854SAlex Deucher rdev->wb.use_event = true; 242d0f8a854SAlex Deucher } 243724c80e1SAlex Deucher } 2443b7a2b24SJerome Glisse } 245c994ead6SAlex Deucher /* always use writeback/events on NI, APUs */ 246c994ead6SAlex Deucher if (rdev->family >= CHIP_PALM) { 2477d52785dSAlex Deucher rdev->wb.enabled = true; 2487d52785dSAlex Deucher rdev->wb.use_event = true; 2497d52785dSAlex Deucher } 250724c80e1SAlex Deucher 251724c80e1SAlex Deucher dev_info(rdev->dev, "WB %sabled\n", rdev->wb.enabled ? "en" : "dis"); 252724c80e1SAlex Deucher 253724c80e1SAlex Deucher return 0; 254724c80e1SAlex Deucher } 255724c80e1SAlex Deucher 256d594e46aSJerome Glisse /** 257d594e46aSJerome Glisse * radeon_vram_location - try to find VRAM location 258d594e46aSJerome Glisse * @rdev: radeon device structure holding all necessary informations 259d594e46aSJerome Glisse * @mc: memory controller structure holding memory informations 260d594e46aSJerome Glisse * @base: base address at which to put VRAM 261d594e46aSJerome Glisse * 262d594e46aSJerome Glisse * Function will place try to place VRAM at base address provided 263d594e46aSJerome Glisse * as parameter (which is so far either PCI aperture address or 264d594e46aSJerome Glisse * for IGP TOM base address). 265d594e46aSJerome Glisse * 266d594e46aSJerome Glisse * If there is not enough space to fit the unvisible VRAM in the 32bits 267d594e46aSJerome Glisse * address space then we limit the VRAM size to the aperture. 268d594e46aSJerome Glisse * 269d594e46aSJerome Glisse * If we are using AGP and if the AGP aperture doesn't allow us to have 270d594e46aSJerome Glisse * room for all the VRAM than we restrict the VRAM to the PCI aperture 271d594e46aSJerome Glisse * size and print a warning. 272d594e46aSJerome Glisse * 273d594e46aSJerome Glisse * This function will never fails, worst case are limiting VRAM. 274d594e46aSJerome Glisse * 275d594e46aSJerome Glisse * Note: GTT start, end, size should be initialized before calling this 276d594e46aSJerome Glisse * function on AGP platform. 277d594e46aSJerome Glisse * 27825985edcSLucas De Marchi * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size, 279d594e46aSJerome Glisse * this shouldn't be a problem as we are using the PCI aperture as a reference. 280d594e46aSJerome Glisse * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but 281d594e46aSJerome Glisse * not IGP. 282d594e46aSJerome Glisse * 283d594e46aSJerome Glisse * Note: we use mc_vram_size as on some board we need to program the mc to 284d594e46aSJerome Glisse * cover the whole aperture even if VRAM size is inferior to aperture size 285d594e46aSJerome Glisse * Novell bug 204882 + along with lots of ubuntu ones 286d594e46aSJerome Glisse * 287d594e46aSJerome Glisse * Note: when limiting vram it's safe to overwritte real_vram_size because 288d594e46aSJerome Glisse * we are not in case where real_vram_size is inferior to mc_vram_size (ie 289d594e46aSJerome Glisse * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu 290d594e46aSJerome Glisse * ones) 291d594e46aSJerome Glisse * 292d594e46aSJerome Glisse * Note: IGP TOM addr should be the same as the aperture addr, we don't 293d594e46aSJerome Glisse * explicitly check for that thought. 294d594e46aSJerome Glisse * 295d594e46aSJerome Glisse * FIXME: when reducing VRAM size align new size on power of 2. 296771fe6b9SJerome Glisse */ 297d594e46aSJerome Glisse void radeon_vram_location(struct radeon_device *rdev, struct radeon_mc *mc, u64 base) 298771fe6b9SJerome Glisse { 299d594e46aSJerome Glisse mc->vram_start = base; 300d594e46aSJerome Glisse if (mc->mc_vram_size > (0xFFFFFFFF - base + 1)) { 301d594e46aSJerome Glisse dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n"); 302d594e46aSJerome Glisse mc->real_vram_size = mc->aper_size; 303d594e46aSJerome Glisse mc->mc_vram_size = mc->aper_size; 304771fe6b9SJerome Glisse } 305d594e46aSJerome Glisse mc->vram_end = mc->vram_start + mc->mc_vram_size - 1; 3062cbeb4efSJerome Glisse if (rdev->flags & RADEON_IS_AGP && mc->vram_end > mc->gtt_start && mc->vram_start <= mc->gtt_end) { 307d594e46aSJerome Glisse dev_warn(rdev->dev, "limiting VRAM to PCI aperture size\n"); 308d594e46aSJerome Glisse mc->real_vram_size = mc->aper_size; 309d594e46aSJerome Glisse mc->mc_vram_size = mc->aper_size; 310771fe6b9SJerome Glisse } 311d594e46aSJerome Glisse mc->vram_end = mc->vram_start + mc->mc_vram_size - 1; 312ba95c45aSMichel Dänzer if (radeon_vram_limit && radeon_vram_limit < mc->real_vram_size) 313ba95c45aSMichel Dänzer mc->real_vram_size = radeon_vram_limit; 314dd7cc55aSAlex Deucher dev_info(rdev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n", 315d594e46aSJerome Glisse mc->mc_vram_size >> 20, mc->vram_start, 316d594e46aSJerome Glisse mc->vram_end, mc->real_vram_size >> 20); 317771fe6b9SJerome Glisse } 318771fe6b9SJerome Glisse 319d594e46aSJerome Glisse /** 320d594e46aSJerome Glisse * radeon_gtt_location - try to find GTT location 321d594e46aSJerome Glisse * @rdev: radeon device structure holding all necessary informations 322d594e46aSJerome Glisse * @mc: memory controller structure holding memory informations 323d594e46aSJerome Glisse * 324d594e46aSJerome Glisse * Function will place try to place GTT before or after VRAM. 325d594e46aSJerome Glisse * 326d594e46aSJerome Glisse * If GTT size is bigger than space left then we ajust GTT size. 327d594e46aSJerome Glisse * Thus function will never fails. 328d594e46aSJerome Glisse * 329d594e46aSJerome Glisse * FIXME: when reducing GTT size align new size on power of 2. 330d594e46aSJerome Glisse */ 331d594e46aSJerome Glisse void radeon_gtt_location(struct radeon_device *rdev, struct radeon_mc *mc) 332d594e46aSJerome Glisse { 333d594e46aSJerome Glisse u64 size_af, size_bf; 334d594e46aSJerome Glisse 3358d369bb1SAlex Deucher size_af = ((0xFFFFFFFF - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align; 3368d369bb1SAlex Deucher size_bf = mc->vram_start & ~mc->gtt_base_align; 337d594e46aSJerome Glisse if (size_bf > size_af) { 338d594e46aSJerome Glisse if (mc->gtt_size > size_bf) { 339d594e46aSJerome Glisse dev_warn(rdev->dev, "limiting GTT\n"); 340d594e46aSJerome Glisse mc->gtt_size = size_bf; 341d594e46aSJerome Glisse } 3428d369bb1SAlex Deucher mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size; 343d594e46aSJerome Glisse } else { 344d594e46aSJerome Glisse if (mc->gtt_size > size_af) { 345d594e46aSJerome Glisse dev_warn(rdev->dev, "limiting GTT\n"); 346d594e46aSJerome Glisse mc->gtt_size = size_af; 347d594e46aSJerome Glisse } 3488d369bb1SAlex Deucher mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align; 349d594e46aSJerome Glisse } 350d594e46aSJerome Glisse mc->gtt_end = mc->gtt_start + mc->gtt_size - 1; 351dd7cc55aSAlex Deucher dev_info(rdev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n", 352d594e46aSJerome Glisse mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end); 353d594e46aSJerome Glisse } 354771fe6b9SJerome Glisse 355771fe6b9SJerome Glisse /* 356771fe6b9SJerome Glisse * GPU helpers function. 357771fe6b9SJerome Glisse */ 3589f022ddfSJerome Glisse bool radeon_card_posted(struct radeon_device *rdev) 359771fe6b9SJerome Glisse { 360771fe6b9SJerome Glisse uint32_t reg; 361771fe6b9SJerome Glisse 362bcc65fd8SMatthew Garrett if (efi_enabled && rdev->pdev->subsystem_vendor == PCI_VENDOR_ID_APPLE) 363bcc65fd8SMatthew Garrett return false; 364bcc65fd8SMatthew Garrett 365771fe6b9SJerome Glisse /* first check CRTCs */ 36618007401SAlex Deucher if (ASIC_IS_DCE41(rdev)) { 36718007401SAlex Deucher reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) | 36818007401SAlex Deucher RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET); 36918007401SAlex Deucher if (reg & EVERGREEN_CRTC_MASTER_EN) 37018007401SAlex Deucher return true; 37118007401SAlex Deucher } else if (ASIC_IS_DCE4(rdev)) { 372bcc1c2a1SAlex Deucher reg = RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC0_REGISTER_OFFSET) | 373bcc1c2a1SAlex Deucher RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC1_REGISTER_OFFSET) | 374bcc1c2a1SAlex Deucher RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC2_REGISTER_OFFSET) | 375bcc1c2a1SAlex Deucher RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC3_REGISTER_OFFSET) | 376bcc1c2a1SAlex Deucher RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC4_REGISTER_OFFSET) | 377bcc1c2a1SAlex Deucher RREG32(EVERGREEN_CRTC_CONTROL + EVERGREEN_CRTC5_REGISTER_OFFSET); 378bcc1c2a1SAlex Deucher if (reg & EVERGREEN_CRTC_MASTER_EN) 379bcc1c2a1SAlex Deucher return true; 380bcc1c2a1SAlex Deucher } else if (ASIC_IS_AVIVO(rdev)) { 381771fe6b9SJerome Glisse reg = RREG32(AVIVO_D1CRTC_CONTROL) | 382771fe6b9SJerome Glisse RREG32(AVIVO_D2CRTC_CONTROL); 383771fe6b9SJerome Glisse if (reg & AVIVO_CRTC_EN) { 384771fe6b9SJerome Glisse return true; 385771fe6b9SJerome Glisse } 386771fe6b9SJerome Glisse } else { 387771fe6b9SJerome Glisse reg = RREG32(RADEON_CRTC_GEN_CNTL) | 388771fe6b9SJerome Glisse RREG32(RADEON_CRTC2_GEN_CNTL); 389771fe6b9SJerome Glisse if (reg & RADEON_CRTC_EN) { 390771fe6b9SJerome Glisse return true; 391771fe6b9SJerome Glisse } 392771fe6b9SJerome Glisse } 393771fe6b9SJerome Glisse 394771fe6b9SJerome Glisse /* then check MEM_SIZE, in case the crtcs are off */ 395771fe6b9SJerome Glisse if (rdev->family >= CHIP_R600) 396771fe6b9SJerome Glisse reg = RREG32(R600_CONFIG_MEMSIZE); 397771fe6b9SJerome Glisse else 398771fe6b9SJerome Glisse reg = RREG32(RADEON_CONFIG_MEMSIZE); 399771fe6b9SJerome Glisse 400771fe6b9SJerome Glisse if (reg) 401771fe6b9SJerome Glisse return true; 402771fe6b9SJerome Glisse 403771fe6b9SJerome Glisse return false; 404771fe6b9SJerome Glisse 405771fe6b9SJerome Glisse } 406771fe6b9SJerome Glisse 407f47299c5SAlex Deucher void radeon_update_bandwidth_info(struct radeon_device *rdev) 408f47299c5SAlex Deucher { 409f47299c5SAlex Deucher fixed20_12 a; 4108807286eSAlex Deucher u32 sclk = rdev->pm.current_sclk; 4118807286eSAlex Deucher u32 mclk = rdev->pm.current_mclk; 412f47299c5SAlex Deucher 4138807286eSAlex Deucher /* sclk/mclk in Mhz */ 41468adac5eSBen Skeggs a.full = dfixed_const(100); 41568adac5eSBen Skeggs rdev->pm.sclk.full = dfixed_const(sclk); 41668adac5eSBen Skeggs rdev->pm.sclk.full = dfixed_div(rdev->pm.sclk, a); 41768adac5eSBen Skeggs rdev->pm.mclk.full = dfixed_const(mclk); 41868adac5eSBen Skeggs rdev->pm.mclk.full = dfixed_div(rdev->pm.mclk, a); 419f47299c5SAlex Deucher 4208807286eSAlex Deucher if (rdev->flags & RADEON_IS_IGP) { 42168adac5eSBen Skeggs a.full = dfixed_const(16); 422f47299c5SAlex Deucher /* core_bandwidth = sclk(Mhz) * 16 */ 42368adac5eSBen Skeggs rdev->pm.core_bandwidth.full = dfixed_div(rdev->pm.sclk, a); 424f47299c5SAlex Deucher } 425f47299c5SAlex Deucher } 426f47299c5SAlex Deucher 42772542d77SDave Airlie bool radeon_boot_test_post_card(struct radeon_device *rdev) 42872542d77SDave Airlie { 42972542d77SDave Airlie if (radeon_card_posted(rdev)) 43072542d77SDave Airlie return true; 43172542d77SDave Airlie 43272542d77SDave Airlie if (rdev->bios) { 43372542d77SDave Airlie DRM_INFO("GPU not posted. posting now...\n"); 43472542d77SDave Airlie if (rdev->is_atom_bios) 43572542d77SDave Airlie atom_asic_init(rdev->mode_info.atom_context); 43672542d77SDave Airlie else 43772542d77SDave Airlie radeon_combios_asic_init(rdev->ddev); 43872542d77SDave Airlie return true; 43972542d77SDave Airlie } else { 44072542d77SDave Airlie dev_err(rdev->dev, "Card not posted and no BIOS - ignoring\n"); 44172542d77SDave Airlie return false; 44272542d77SDave Airlie } 44372542d77SDave Airlie } 44472542d77SDave Airlie 4453ce0a23dSJerome Glisse int radeon_dummy_page_init(struct radeon_device *rdev) 4463ce0a23dSJerome Glisse { 44782568565SDave Airlie if (rdev->dummy_page.page) 44882568565SDave Airlie return 0; 4493ce0a23dSJerome Glisse rdev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO); 4503ce0a23dSJerome Glisse if (rdev->dummy_page.page == NULL) 4513ce0a23dSJerome Glisse return -ENOMEM; 4523ce0a23dSJerome Glisse rdev->dummy_page.addr = pci_map_page(rdev->pdev, rdev->dummy_page.page, 4533ce0a23dSJerome Glisse 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); 454a30f6fb7SBenjamin Herrenschmidt if (pci_dma_mapping_error(rdev->pdev, rdev->dummy_page.addr)) { 455a30f6fb7SBenjamin Herrenschmidt dev_err(&rdev->pdev->dev, "Failed to DMA MAP the dummy page\n"); 4563ce0a23dSJerome Glisse __free_page(rdev->dummy_page.page); 4573ce0a23dSJerome Glisse rdev->dummy_page.page = NULL; 4583ce0a23dSJerome Glisse return -ENOMEM; 4593ce0a23dSJerome Glisse } 4603ce0a23dSJerome Glisse return 0; 4613ce0a23dSJerome Glisse } 4623ce0a23dSJerome Glisse 4633ce0a23dSJerome Glisse void radeon_dummy_page_fini(struct radeon_device *rdev) 4643ce0a23dSJerome Glisse { 4653ce0a23dSJerome Glisse if (rdev->dummy_page.page == NULL) 4663ce0a23dSJerome Glisse return; 4673ce0a23dSJerome Glisse pci_unmap_page(rdev->pdev, rdev->dummy_page.addr, 4683ce0a23dSJerome Glisse PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); 4693ce0a23dSJerome Glisse __free_page(rdev->dummy_page.page); 4703ce0a23dSJerome Glisse rdev->dummy_page.page = NULL; 4713ce0a23dSJerome Glisse } 4723ce0a23dSJerome Glisse 473771fe6b9SJerome Glisse 474771fe6b9SJerome Glisse /* ATOM accessor methods */ 475771fe6b9SJerome Glisse static uint32_t cail_pll_read(struct card_info *info, uint32_t reg) 476771fe6b9SJerome Glisse { 477771fe6b9SJerome Glisse struct radeon_device *rdev = info->dev->dev_private; 478771fe6b9SJerome Glisse uint32_t r; 479771fe6b9SJerome Glisse 480771fe6b9SJerome Glisse r = rdev->pll_rreg(rdev, reg); 481771fe6b9SJerome Glisse return r; 482771fe6b9SJerome Glisse } 483771fe6b9SJerome Glisse 484771fe6b9SJerome Glisse static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val) 485771fe6b9SJerome Glisse { 486771fe6b9SJerome Glisse struct radeon_device *rdev = info->dev->dev_private; 487771fe6b9SJerome Glisse 488771fe6b9SJerome Glisse rdev->pll_wreg(rdev, reg, val); 489771fe6b9SJerome Glisse } 490771fe6b9SJerome Glisse 491771fe6b9SJerome Glisse static uint32_t cail_mc_read(struct card_info *info, uint32_t reg) 492771fe6b9SJerome Glisse { 493771fe6b9SJerome Glisse struct radeon_device *rdev = info->dev->dev_private; 494771fe6b9SJerome Glisse uint32_t r; 495771fe6b9SJerome Glisse 496771fe6b9SJerome Glisse r = rdev->mc_rreg(rdev, reg); 497771fe6b9SJerome Glisse return r; 498771fe6b9SJerome Glisse } 499771fe6b9SJerome Glisse 500771fe6b9SJerome Glisse static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val) 501771fe6b9SJerome Glisse { 502771fe6b9SJerome Glisse struct radeon_device *rdev = info->dev->dev_private; 503771fe6b9SJerome Glisse 504771fe6b9SJerome Glisse rdev->mc_wreg(rdev, reg, val); 505771fe6b9SJerome Glisse } 506771fe6b9SJerome Glisse 507771fe6b9SJerome Glisse static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val) 508771fe6b9SJerome Glisse { 509771fe6b9SJerome Glisse struct radeon_device *rdev = info->dev->dev_private; 510771fe6b9SJerome Glisse 511771fe6b9SJerome Glisse WREG32(reg*4, val); 512771fe6b9SJerome Glisse } 513771fe6b9SJerome Glisse 514771fe6b9SJerome Glisse static uint32_t cail_reg_read(struct card_info *info, uint32_t reg) 515771fe6b9SJerome Glisse { 516771fe6b9SJerome Glisse struct radeon_device *rdev = info->dev->dev_private; 517771fe6b9SJerome Glisse uint32_t r; 518771fe6b9SJerome Glisse 519771fe6b9SJerome Glisse r = RREG32(reg*4); 520771fe6b9SJerome Glisse return r; 521771fe6b9SJerome Glisse } 522771fe6b9SJerome Glisse 523351a52a2SAlex Deucher static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val) 524351a52a2SAlex Deucher { 525351a52a2SAlex Deucher struct radeon_device *rdev = info->dev->dev_private; 526351a52a2SAlex Deucher 527351a52a2SAlex Deucher WREG32_IO(reg*4, val); 528351a52a2SAlex Deucher } 529351a52a2SAlex Deucher 530351a52a2SAlex Deucher static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg) 531351a52a2SAlex Deucher { 532351a52a2SAlex Deucher struct radeon_device *rdev = info->dev->dev_private; 533351a52a2SAlex Deucher uint32_t r; 534351a52a2SAlex Deucher 535351a52a2SAlex Deucher r = RREG32_IO(reg*4); 536351a52a2SAlex Deucher return r; 537351a52a2SAlex Deucher } 538351a52a2SAlex Deucher 539771fe6b9SJerome Glisse int radeon_atombios_init(struct radeon_device *rdev) 540771fe6b9SJerome Glisse { 54161c4b24bSMathias Fröhlich struct card_info *atom_card_info = 54261c4b24bSMathias Fröhlich kzalloc(sizeof(struct card_info), GFP_KERNEL); 54361c4b24bSMathias Fröhlich 54461c4b24bSMathias Fröhlich if (!atom_card_info) 54561c4b24bSMathias Fröhlich return -ENOMEM; 54661c4b24bSMathias Fröhlich 54761c4b24bSMathias Fröhlich rdev->mode_info.atom_card_info = atom_card_info; 54861c4b24bSMathias Fröhlich atom_card_info->dev = rdev->ddev; 54961c4b24bSMathias Fröhlich atom_card_info->reg_read = cail_reg_read; 55061c4b24bSMathias Fröhlich atom_card_info->reg_write = cail_reg_write; 551351a52a2SAlex Deucher /* needed for iio ops */ 552351a52a2SAlex Deucher if (rdev->rio_mem) { 553351a52a2SAlex Deucher atom_card_info->ioreg_read = cail_ioreg_read; 554351a52a2SAlex Deucher atom_card_info->ioreg_write = cail_ioreg_write; 555351a52a2SAlex Deucher } else { 556351a52a2SAlex Deucher DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n"); 557351a52a2SAlex Deucher atom_card_info->ioreg_read = cail_reg_read; 558351a52a2SAlex Deucher atom_card_info->ioreg_write = cail_reg_write; 559351a52a2SAlex Deucher } 56061c4b24bSMathias Fröhlich atom_card_info->mc_read = cail_mc_read; 56161c4b24bSMathias Fröhlich atom_card_info->mc_write = cail_mc_write; 56261c4b24bSMathias Fröhlich atom_card_info->pll_read = cail_pll_read; 56361c4b24bSMathias Fröhlich atom_card_info->pll_write = cail_pll_write; 56461c4b24bSMathias Fröhlich 56561c4b24bSMathias Fröhlich rdev->mode_info.atom_context = atom_parse(atom_card_info, rdev->bios); 566c31ad97fSRafał Miłecki mutex_init(&rdev->mode_info.atom_context->mutex); 567771fe6b9SJerome Glisse radeon_atom_initialize_bios_scratch_regs(rdev->ddev); 568d904ef9bSDave Airlie atom_allocate_fb_scratch(rdev->mode_info.atom_context); 569771fe6b9SJerome Glisse return 0; 570771fe6b9SJerome Glisse } 571771fe6b9SJerome Glisse 572771fe6b9SJerome Glisse void radeon_atombios_fini(struct radeon_device *rdev) 573771fe6b9SJerome Glisse { 5744a04a844SJerome Glisse if (rdev->mode_info.atom_context) { 575d904ef9bSDave Airlie kfree(rdev->mode_info.atom_context->scratch); 576771fe6b9SJerome Glisse kfree(rdev->mode_info.atom_context); 5774a04a844SJerome Glisse } 57861c4b24bSMathias Fröhlich kfree(rdev->mode_info.atom_card_info); 579771fe6b9SJerome Glisse } 580771fe6b9SJerome Glisse 581771fe6b9SJerome Glisse int radeon_combios_init(struct radeon_device *rdev) 582771fe6b9SJerome Glisse { 583771fe6b9SJerome Glisse radeon_combios_initialize_bios_scratch_regs(rdev->ddev); 584771fe6b9SJerome Glisse return 0; 585771fe6b9SJerome Glisse } 586771fe6b9SJerome Glisse 587771fe6b9SJerome Glisse void radeon_combios_fini(struct radeon_device *rdev) 588771fe6b9SJerome Glisse { 589771fe6b9SJerome Glisse } 590771fe6b9SJerome Glisse 59128d52043SDave Airlie /* if we get transitioned to only one device, tak VGA back */ 59228d52043SDave Airlie static unsigned int radeon_vga_set_decode(void *cookie, bool state) 59328d52043SDave Airlie { 59428d52043SDave Airlie struct radeon_device *rdev = cookie; 59528d52043SDave Airlie radeon_vga_set_state(rdev, state); 59628d52043SDave Airlie if (state) 59728d52043SDave Airlie return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM | 59828d52043SDave Airlie VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; 59928d52043SDave Airlie else 60028d52043SDave Airlie return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; 60128d52043SDave Airlie } 602c1176d6fSDave Airlie 60336421338SJerome Glisse void radeon_check_arguments(struct radeon_device *rdev) 60436421338SJerome Glisse { 60536421338SJerome Glisse /* vramlimit must be a power of two */ 60636421338SJerome Glisse switch (radeon_vram_limit) { 60736421338SJerome Glisse case 0: 60836421338SJerome Glisse case 4: 60936421338SJerome Glisse case 8: 61036421338SJerome Glisse case 16: 61136421338SJerome Glisse case 32: 61236421338SJerome Glisse case 64: 61336421338SJerome Glisse case 128: 61436421338SJerome Glisse case 256: 61536421338SJerome Glisse case 512: 61636421338SJerome Glisse case 1024: 61736421338SJerome Glisse case 2048: 61836421338SJerome Glisse case 4096: 61936421338SJerome Glisse break; 62036421338SJerome Glisse default: 62136421338SJerome Glisse dev_warn(rdev->dev, "vram limit (%d) must be a power of 2\n", 62236421338SJerome Glisse radeon_vram_limit); 62336421338SJerome Glisse radeon_vram_limit = 0; 62436421338SJerome Glisse break; 62536421338SJerome Glisse } 62636421338SJerome Glisse radeon_vram_limit = radeon_vram_limit << 20; 62736421338SJerome Glisse /* gtt size must be power of two and greater or equal to 32M */ 62836421338SJerome Glisse switch (radeon_gart_size) { 62936421338SJerome Glisse case 4: 63036421338SJerome Glisse case 8: 63136421338SJerome Glisse case 16: 63236421338SJerome Glisse dev_warn(rdev->dev, "gart size (%d) too small forcing to 512M\n", 63336421338SJerome Glisse radeon_gart_size); 63436421338SJerome Glisse radeon_gart_size = 512; 63536421338SJerome Glisse break; 63636421338SJerome Glisse case 32: 63736421338SJerome Glisse case 64: 63836421338SJerome Glisse case 128: 63936421338SJerome Glisse case 256: 64036421338SJerome Glisse case 512: 64136421338SJerome Glisse case 1024: 64236421338SJerome Glisse case 2048: 64336421338SJerome Glisse case 4096: 64436421338SJerome Glisse break; 64536421338SJerome Glisse default: 64636421338SJerome Glisse dev_warn(rdev->dev, "gart size (%d) must be a power of 2\n", 64736421338SJerome Glisse radeon_gart_size); 64836421338SJerome Glisse radeon_gart_size = 512; 64936421338SJerome Glisse break; 65036421338SJerome Glisse } 65136421338SJerome Glisse rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024; 65236421338SJerome Glisse /* AGP mode can only be -1, 1, 2, 4, 8 */ 65336421338SJerome Glisse switch (radeon_agpmode) { 65436421338SJerome Glisse case -1: 65536421338SJerome Glisse case 0: 65636421338SJerome Glisse case 1: 65736421338SJerome Glisse case 2: 65836421338SJerome Glisse case 4: 65936421338SJerome Glisse case 8: 66036421338SJerome Glisse break; 66136421338SJerome Glisse default: 66236421338SJerome Glisse dev_warn(rdev->dev, "invalid AGP mode %d (valid mode: " 66336421338SJerome Glisse "-1, 0, 1, 2, 4, 8)\n", radeon_agpmode); 66436421338SJerome Glisse radeon_agpmode = 0; 66536421338SJerome Glisse break; 66636421338SJerome Glisse } 66736421338SJerome Glisse } 66836421338SJerome Glisse 6696a9ee8afSDave Airlie static void radeon_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state) 6706a9ee8afSDave Airlie { 6716a9ee8afSDave Airlie struct drm_device *dev = pci_get_drvdata(pdev); 6726a9ee8afSDave Airlie pm_message_t pmm = { .event = PM_EVENT_SUSPEND }; 6736a9ee8afSDave Airlie if (state == VGA_SWITCHEROO_ON) { 6746a9ee8afSDave Airlie printk(KERN_INFO "radeon: switched on\n"); 6756a9ee8afSDave Airlie /* don't suspend or resume card normally */ 6765bcf719bSDave Airlie dev->switch_power_state = DRM_SWITCH_POWER_CHANGING; 6776a9ee8afSDave Airlie radeon_resume_kms(dev); 6785bcf719bSDave Airlie dev->switch_power_state = DRM_SWITCH_POWER_ON; 679fbf81762SDave Airlie drm_kms_helper_poll_enable(dev); 6806a9ee8afSDave Airlie } else { 6816a9ee8afSDave Airlie printk(KERN_INFO "radeon: switched off\n"); 682fbf81762SDave Airlie drm_kms_helper_poll_disable(dev); 6835bcf719bSDave Airlie dev->switch_power_state = DRM_SWITCH_POWER_CHANGING; 6846a9ee8afSDave Airlie radeon_suspend_kms(dev, pmm); 6855bcf719bSDave Airlie dev->switch_power_state = DRM_SWITCH_POWER_OFF; 6866a9ee8afSDave Airlie } 6876a9ee8afSDave Airlie } 6886a9ee8afSDave Airlie 6896a9ee8afSDave Airlie static bool radeon_switcheroo_can_switch(struct pci_dev *pdev) 6906a9ee8afSDave Airlie { 6916a9ee8afSDave Airlie struct drm_device *dev = pci_get_drvdata(pdev); 6926a9ee8afSDave Airlie bool can_switch; 6936a9ee8afSDave Airlie 6946a9ee8afSDave Airlie spin_lock(&dev->count_lock); 6956a9ee8afSDave Airlie can_switch = (dev->open_count == 0); 6966a9ee8afSDave Airlie spin_unlock(&dev->count_lock); 6976a9ee8afSDave Airlie return can_switch; 6986a9ee8afSDave Airlie } 6996a9ee8afSDave Airlie 7006a9ee8afSDave Airlie 701771fe6b9SJerome Glisse int radeon_device_init(struct radeon_device *rdev, 702771fe6b9SJerome Glisse struct drm_device *ddev, 703771fe6b9SJerome Glisse struct pci_dev *pdev, 704771fe6b9SJerome Glisse uint32_t flags) 705771fe6b9SJerome Glisse { 706351a52a2SAlex Deucher int r, i; 707ad49f501SDave Airlie int dma_bits; 708771fe6b9SJerome Glisse 709771fe6b9SJerome Glisse rdev->shutdown = false; 7109f022ddfSJerome Glisse rdev->dev = &pdev->dev; 711771fe6b9SJerome Glisse rdev->ddev = ddev; 712771fe6b9SJerome Glisse rdev->pdev = pdev; 713771fe6b9SJerome Glisse rdev->flags = flags; 714771fe6b9SJerome Glisse rdev->family = flags & RADEON_FAMILY_MASK; 715771fe6b9SJerome Glisse rdev->is_atom_bios = false; 716771fe6b9SJerome Glisse rdev->usec_timeout = RADEON_MAX_USEC_TIMEOUT; 717771fe6b9SJerome Glisse rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024; 718733289c2SJerome Glisse rdev->accel_working = false; 7191b5331d9SJerome Glisse 720d522d9ccSThomas Reim DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X).\n", 721d522d9ccSThomas Reim radeon_family_name[rdev->family], pdev->vendor, pdev->device, 722d522d9ccSThomas Reim pdev->subsystem_vendor, pdev->subsystem_device); 7231b5331d9SJerome Glisse 724771fe6b9SJerome Glisse /* mutex initialization are all done here so we 725771fe6b9SJerome Glisse * can recall function without having locking issues */ 7267a1619b9SMichel Dänzer radeon_mutex_init(&rdev->cs_mutex); 7279fc04b50SJerome Glisse radeon_mutex_init(&rdev->ib_pool.mutex); 728d6999bc7SChristian König mutex_init(&rdev->ring_lock); 72940bacf16SAlex Deucher mutex_init(&rdev->dc_hw_i2c_mutex); 730d8f60cfcSAlex Deucher if (rdev->family >= CHIP_R600) 731d8f60cfcSAlex Deucher spin_lock_init(&rdev->ih.lock); 7324c788679SJerome Glisse mutex_init(&rdev->gem.mutex); 733c913e23aSRafał Miłecki mutex_init(&rdev->pm.mutex); 7345876dd24SMatthew Garrett mutex_init(&rdev->vram_mutex); 73515d3332fSChristian König rwlock_init(&rdev->semaphore_drv.lock); 7369f022ddfSJerome Glisse INIT_LIST_HEAD(&rdev->gem.objects); 73773a6d3fcSRafał Miłecki init_waitqueue_head(&rdev->irq.vblank_queue); 7382031f77cSAlex Deucher init_waitqueue_head(&rdev->irq.idle_queue); 739c1341e52SJerome Glisse INIT_LIST_HEAD(&rdev->semaphore_drv.bo); 740721604a1SJerome Glisse /* initialize vm here */ 741721604a1SJerome Glisse rdev->vm_manager.use_bitmap = 1; 742721604a1SJerome Glisse rdev->vm_manager.max_pfn = 1 << 20; 743721604a1SJerome Glisse INIT_LIST_HEAD(&rdev->vm_manager.lru_vm); 744771fe6b9SJerome Glisse 7454aac0473SJerome Glisse /* Set asic functions */ 7464aac0473SJerome Glisse r = radeon_asic_init(rdev); 74736421338SJerome Glisse if (r) 7484aac0473SJerome Glisse return r; 74936421338SJerome Glisse radeon_check_arguments(rdev); 7504aac0473SJerome Glisse 751f95df9caSAlex Deucher /* all of the newer IGP chips have an internal gart 752f95df9caSAlex Deucher * However some rs4xx report as AGP, so remove that here. 753f95df9caSAlex Deucher */ 754f95df9caSAlex Deucher if ((rdev->family >= CHIP_RS400) && 755f95df9caSAlex Deucher (rdev->flags & RADEON_IS_IGP)) { 756f95df9caSAlex Deucher rdev->flags &= ~RADEON_IS_AGP; 757f95df9caSAlex Deucher } 758f95df9caSAlex Deucher 75930256a3fSJerome Glisse if (rdev->flags & RADEON_IS_AGP && radeon_agpmode == -1) { 760b574f251SJerome Glisse radeon_agp_disable(rdev); 761771fe6b9SJerome Glisse } 762771fe6b9SJerome Glisse 763ad49f501SDave Airlie /* set DMA mask + need_dma32 flags. 764ad49f501SDave Airlie * PCIE - can handle 40-bits. 765005a83f1SAlex Deucher * IGP - can handle 40-bits 766ad49f501SDave Airlie * AGP - generally dma32 is safest 767005a83f1SAlex Deucher * PCI - dma32 for legacy pci gart, 40 bits on newer asics 768ad49f501SDave Airlie */ 769ad49f501SDave Airlie rdev->need_dma32 = false; 770ad49f501SDave Airlie if (rdev->flags & RADEON_IS_AGP) 771ad49f501SDave Airlie rdev->need_dma32 = true; 772005a83f1SAlex Deucher if ((rdev->flags & RADEON_IS_PCI) && 773005a83f1SAlex Deucher (rdev->family < CHIP_RS400)) 774ad49f501SDave Airlie rdev->need_dma32 = true; 775ad49f501SDave Airlie 776ad49f501SDave Airlie dma_bits = rdev->need_dma32 ? 32 : 40; 777ad49f501SDave Airlie r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits)); 778771fe6b9SJerome Glisse if (r) { 77962fff811SDaniel Haid rdev->need_dma32 = true; 780c52494f6SKonrad Rzeszutek Wilk dma_bits = 32; 781771fe6b9SJerome Glisse printk(KERN_WARNING "radeon: No suitable DMA available.\n"); 782771fe6b9SJerome Glisse } 783c52494f6SKonrad Rzeszutek Wilk r = pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits)); 784c52494f6SKonrad Rzeszutek Wilk if (r) { 785c52494f6SKonrad Rzeszutek Wilk pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(32)); 786c52494f6SKonrad Rzeszutek Wilk printk(KERN_WARNING "radeon: No coherent DMA available.\n"); 787c52494f6SKonrad Rzeszutek Wilk } 788771fe6b9SJerome Glisse 789771fe6b9SJerome Glisse /* Registers mapping */ 790771fe6b9SJerome Glisse /* TODO: block userspace mapping of io register */ 79101d73a69SJordan Crouse rdev->rmmio_base = pci_resource_start(rdev->pdev, 2); 79201d73a69SJordan Crouse rdev->rmmio_size = pci_resource_len(rdev->pdev, 2); 793771fe6b9SJerome Glisse rdev->rmmio = ioremap(rdev->rmmio_base, rdev->rmmio_size); 794771fe6b9SJerome Glisse if (rdev->rmmio == NULL) { 795771fe6b9SJerome Glisse return -ENOMEM; 796771fe6b9SJerome Glisse } 797771fe6b9SJerome Glisse DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)rdev->rmmio_base); 798771fe6b9SJerome Glisse DRM_INFO("register mmio size: %u\n", (unsigned)rdev->rmmio_size); 799771fe6b9SJerome Glisse 800351a52a2SAlex Deucher /* io port mapping */ 801351a52a2SAlex Deucher for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { 802351a52a2SAlex Deucher if (pci_resource_flags(rdev->pdev, i) & IORESOURCE_IO) { 803351a52a2SAlex Deucher rdev->rio_mem_size = pci_resource_len(rdev->pdev, i); 804351a52a2SAlex Deucher rdev->rio_mem = pci_iomap(rdev->pdev, i, rdev->rio_mem_size); 805351a52a2SAlex Deucher break; 806351a52a2SAlex Deucher } 807351a52a2SAlex Deucher } 808351a52a2SAlex Deucher if (rdev->rio_mem == NULL) 809351a52a2SAlex Deucher DRM_ERROR("Unable to find PCI I/O BAR\n"); 810351a52a2SAlex Deucher 81128d52043SDave Airlie /* if we have > 1 VGA cards, then disable the radeon VGA resources */ 81293239ea1SDave Airlie /* this will fail for cards that aren't VGA class devices, just 81393239ea1SDave Airlie * ignore it */ 81493239ea1SDave Airlie vga_client_register(rdev->pdev, rdev, NULL, radeon_vga_set_decode); 8156a9ee8afSDave Airlie vga_switcheroo_register_client(rdev->pdev, 8166a9ee8afSDave Airlie radeon_switcheroo_set_state, 8178d608aa6SDave Airlie NULL, 8186a9ee8afSDave Airlie radeon_switcheroo_can_switch); 81928d52043SDave Airlie 8203ce0a23dSJerome Glisse r = radeon_init(rdev); 821b574f251SJerome Glisse if (r) 822b574f251SJerome Glisse return r; 823b1e3a6d1SMichel Dänzer 824b574f251SJerome Glisse if (rdev->flags & RADEON_IS_AGP && !rdev->accel_working) { 825b574f251SJerome Glisse /* Acceleration not working on AGP card try again 826b574f251SJerome Glisse * with fallback to PCI or PCIE GART 827b574f251SJerome Glisse */ 828a2d07b74SJerome Glisse radeon_asic_reset(rdev); 829b574f251SJerome Glisse radeon_fini(rdev); 830b574f251SJerome Glisse radeon_agp_disable(rdev); 831b574f251SJerome Glisse r = radeon_init(rdev); 8324aac0473SJerome Glisse if (r) 8334aac0473SJerome Glisse return r; 8343ce0a23dSJerome Glisse } 83560a7e396SChristian König if ((radeon_testing & 1)) { 836ecc0b326SMichel Dänzer radeon_test_moves(rdev); 837ecc0b326SMichel Dänzer } 83860a7e396SChristian König if ((radeon_testing & 2)) { 83960a7e396SChristian König radeon_test_syncing(rdev); 84060a7e396SChristian König } 841771fe6b9SJerome Glisse if (radeon_benchmarking) { 842638dd7dbSIlija Hadzic radeon_benchmark(rdev, radeon_benchmarking); 843771fe6b9SJerome Glisse } 8446cf8a3f5SJerome Glisse return 0; 845771fe6b9SJerome Glisse } 846771fe6b9SJerome Glisse 8474d8bf9aeSChristian König static void radeon_debugfs_remove_files(struct radeon_device *rdev); 8484d8bf9aeSChristian König 849771fe6b9SJerome Glisse void radeon_device_fini(struct radeon_device *rdev) 850771fe6b9SJerome Glisse { 851771fe6b9SJerome Glisse DRM_INFO("radeon: finishing device.\n"); 852771fe6b9SJerome Glisse rdev->shutdown = true; 85390aca4d2SJerome Glisse /* evict vram memory */ 85490aca4d2SJerome Glisse radeon_bo_evict_vram(rdev); 8553ce0a23dSJerome Glisse radeon_fini(rdev); 8566a9ee8afSDave Airlie vga_switcheroo_unregister_client(rdev->pdev); 857c1176d6fSDave Airlie vga_client_register(rdev->pdev, NULL, NULL, NULL); 858e0a2ca73SAlex Deucher if (rdev->rio_mem) 859351a52a2SAlex Deucher pci_iounmap(rdev->pdev, rdev->rio_mem); 860351a52a2SAlex Deucher rdev->rio_mem = NULL; 861771fe6b9SJerome Glisse iounmap(rdev->rmmio); 862771fe6b9SJerome Glisse rdev->rmmio = NULL; 8634d8bf9aeSChristian König radeon_debugfs_remove_files(rdev); 864771fe6b9SJerome Glisse } 865771fe6b9SJerome Glisse 866771fe6b9SJerome Glisse 867771fe6b9SJerome Glisse /* 868771fe6b9SJerome Glisse * Suspend & resume. 869771fe6b9SJerome Glisse */ 870771fe6b9SJerome Glisse int radeon_suspend_kms(struct drm_device *dev, pm_message_t state) 871771fe6b9SJerome Glisse { 872875c1866SDarren Jenkins struct radeon_device *rdev; 873771fe6b9SJerome Glisse struct drm_crtc *crtc; 874d8dcaa1dSAlex Deucher struct drm_connector *connector; 8757465280cSAlex Deucher int i, r; 876771fe6b9SJerome Glisse 877875c1866SDarren Jenkins if (dev == NULL || dev->dev_private == NULL) { 878771fe6b9SJerome Glisse return -ENODEV; 879771fe6b9SJerome Glisse } 880771fe6b9SJerome Glisse if (state.event == PM_EVENT_PRETHAW) { 881771fe6b9SJerome Glisse return 0; 882771fe6b9SJerome Glisse } 883875c1866SDarren Jenkins rdev = dev->dev_private; 884875c1866SDarren Jenkins 8855bcf719bSDave Airlie if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) 8866a9ee8afSDave Airlie return 0; 887d8dcaa1dSAlex Deucher 88886698c20SSeth Forshee drm_kms_helper_poll_disable(dev); 88986698c20SSeth Forshee 890d8dcaa1dSAlex Deucher /* turn off display hw */ 891d8dcaa1dSAlex Deucher list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 892d8dcaa1dSAlex Deucher drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF); 893d8dcaa1dSAlex Deucher } 894d8dcaa1dSAlex Deucher 895771fe6b9SJerome Glisse /* unpin the front buffers */ 896771fe6b9SJerome Glisse list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 897771fe6b9SJerome Glisse struct radeon_framebuffer *rfb = to_radeon_framebuffer(crtc->fb); 8984c788679SJerome Glisse struct radeon_bo *robj; 899771fe6b9SJerome Glisse 900771fe6b9SJerome Glisse if (rfb == NULL || rfb->obj == NULL) { 901771fe6b9SJerome Glisse continue; 902771fe6b9SJerome Glisse } 9037e4d15d9SDaniel Vetter robj = gem_to_radeon_bo(rfb->obj); 90438651674SDave Airlie /* don't unpin kernel fb objects */ 90538651674SDave Airlie if (!radeon_fbdev_robj_is_fb(rdev, robj)) { 9064c788679SJerome Glisse r = radeon_bo_reserve(robj, false); 90738651674SDave Airlie if (r == 0) { 9084c788679SJerome Glisse radeon_bo_unpin(robj); 9094c788679SJerome Glisse radeon_bo_unreserve(robj); 9104c788679SJerome Glisse } 911771fe6b9SJerome Glisse } 912771fe6b9SJerome Glisse } 913771fe6b9SJerome Glisse /* evict vram memory */ 9144c788679SJerome Glisse radeon_bo_evict_vram(rdev); 915*8a47cc9eSChristian König 916*8a47cc9eSChristian König mutex_lock(&rdev->ring_lock); 917771fe6b9SJerome Glisse /* wait for gpu to finish processing current batch */ 9187465280cSAlex Deucher for (i = 0; i < RADEON_NUM_RINGS; i++) 919*8a47cc9eSChristian König radeon_fence_wait_empty_locked(rdev, i); 920*8a47cc9eSChristian König mutex_unlock(&rdev->ring_lock); 921771fe6b9SJerome Glisse 922f657c2a7SYang Zhao radeon_save_bios_scratch_regs(rdev); 923f657c2a7SYang Zhao 924ce8f5370SAlex Deucher radeon_pm_suspend(rdev); 9253ce0a23dSJerome Glisse radeon_suspend(rdev); 926d4877cf2SAlex Deucher radeon_hpd_fini(rdev); 927771fe6b9SJerome Glisse /* evict remaining vram memory */ 9284c788679SJerome Glisse radeon_bo_evict_vram(rdev); 929771fe6b9SJerome Glisse 93010b06122SJerome Glisse radeon_agp_suspend(rdev); 93110b06122SJerome Glisse 932771fe6b9SJerome Glisse pci_save_state(dev->pdev); 933771fe6b9SJerome Glisse if (state.event == PM_EVENT_SUSPEND) { 934771fe6b9SJerome Glisse /* Shut down the device */ 935771fe6b9SJerome Glisse pci_disable_device(dev->pdev); 936771fe6b9SJerome Glisse pci_set_power_state(dev->pdev, PCI_D3hot); 937771fe6b9SJerome Glisse } 938ac751efaSTorben Hohn console_lock(); 93938651674SDave Airlie radeon_fbdev_set_suspend(rdev, 1); 940ac751efaSTorben Hohn console_unlock(); 941771fe6b9SJerome Glisse return 0; 942771fe6b9SJerome Glisse } 943771fe6b9SJerome Glisse 944771fe6b9SJerome Glisse int radeon_resume_kms(struct drm_device *dev) 945771fe6b9SJerome Glisse { 94609bdf591SCedric Godin struct drm_connector *connector; 947771fe6b9SJerome Glisse struct radeon_device *rdev = dev->dev_private; 948771fe6b9SJerome Glisse 9495bcf719bSDave Airlie if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) 9506a9ee8afSDave Airlie return 0; 9516a9ee8afSDave Airlie 952ac751efaSTorben Hohn console_lock(); 953771fe6b9SJerome Glisse pci_set_power_state(dev->pdev, PCI_D0); 954771fe6b9SJerome Glisse pci_restore_state(dev->pdev); 955771fe6b9SJerome Glisse if (pci_enable_device(dev->pdev)) { 956ac751efaSTorben Hohn console_unlock(); 957771fe6b9SJerome Glisse return -1; 958771fe6b9SJerome Glisse } 9590ebf1717SDave Airlie /* resume AGP if in use */ 9600ebf1717SDave Airlie radeon_agp_resume(rdev); 9613ce0a23dSJerome Glisse radeon_resume(rdev); 962ce8f5370SAlex Deucher radeon_pm_resume(rdev); 963f657c2a7SYang Zhao radeon_restore_bios_scratch_regs(rdev); 96409bdf591SCedric Godin 96538651674SDave Airlie radeon_fbdev_set_suspend(rdev, 0); 966ac751efaSTorben Hohn console_unlock(); 967771fe6b9SJerome Glisse 9683fa47d9eSAlex Deucher /* init dig PHYs, disp eng pll */ 9693fa47d9eSAlex Deucher if (rdev->is_atom_bios) { 970ac89af1eSAlex Deucher radeon_atom_encoder_init(rdev); 971f3f1f03eSAlex Deucher radeon_atom_disp_eng_pll_init(rdev); 9723fa47d9eSAlex Deucher } 973d4877cf2SAlex Deucher /* reset hpd state */ 974d4877cf2SAlex Deucher radeon_hpd_init(rdev); 975771fe6b9SJerome Glisse /* blat the mode back in */ 976771fe6b9SJerome Glisse drm_helper_resume_force_mode(dev); 977a93f344dSAlex Deucher /* turn on display hw */ 978a93f344dSAlex Deucher list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 979a93f344dSAlex Deucher drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON); 980a93f344dSAlex Deucher } 98186698c20SSeth Forshee 98286698c20SSeth Forshee drm_kms_helper_poll_enable(dev); 983771fe6b9SJerome Glisse return 0; 984771fe6b9SJerome Glisse } 985771fe6b9SJerome Glisse 98690aca4d2SJerome Glisse int radeon_gpu_reset(struct radeon_device *rdev) 98790aca4d2SJerome Glisse { 98890aca4d2SJerome Glisse int r; 9898fd1b84cSDave Airlie int resched; 99090aca4d2SJerome Glisse 99190aca4d2SJerome Glisse radeon_save_bios_scratch_regs(rdev); 9928fd1b84cSDave Airlie /* block TTM */ 9938fd1b84cSDave Airlie resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev); 99490aca4d2SJerome Glisse radeon_suspend(rdev); 99590aca4d2SJerome Glisse 99690aca4d2SJerome Glisse r = radeon_asic_reset(rdev); 99790aca4d2SJerome Glisse if (!r) { 99890aca4d2SJerome Glisse dev_info(rdev->dev, "GPU reset succeed\n"); 99990aca4d2SJerome Glisse radeon_resume(rdev); 100090aca4d2SJerome Glisse radeon_restore_bios_scratch_regs(rdev); 100190aca4d2SJerome Glisse drm_helper_resume_force_mode(rdev->ddev); 10028fd1b84cSDave Airlie ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched); 100390aca4d2SJerome Glisse } 10047a1619b9SMichel Dänzer 10057a1619b9SMichel Dänzer if (r) { 100690aca4d2SJerome Glisse /* bad news, how to tell it to userspace ? */ 100790aca4d2SJerome Glisse dev_info(rdev->dev, "GPU reset failed\n"); 10087a1619b9SMichel Dänzer } 10097a1619b9SMichel Dänzer 101090aca4d2SJerome Glisse return r; 101190aca4d2SJerome Glisse } 101290aca4d2SJerome Glisse 1013771fe6b9SJerome Glisse 1014771fe6b9SJerome Glisse /* 1015771fe6b9SJerome Glisse * Debugfs 1016771fe6b9SJerome Glisse */ 1017771fe6b9SJerome Glisse int radeon_debugfs_add_files(struct radeon_device *rdev, 1018771fe6b9SJerome Glisse struct drm_info_list *files, 1019771fe6b9SJerome Glisse unsigned nfiles) 1020771fe6b9SJerome Glisse { 1021771fe6b9SJerome Glisse unsigned i; 1022771fe6b9SJerome Glisse 10234d8bf9aeSChristian König for (i = 0; i < rdev->debugfs_count; i++) { 10244d8bf9aeSChristian König if (rdev->debugfs[i].files == files) { 1025771fe6b9SJerome Glisse /* Already registered */ 1026771fe6b9SJerome Glisse return 0; 1027771fe6b9SJerome Glisse } 1028771fe6b9SJerome Glisse } 1029c245cb9eSMichael Witten 10304d8bf9aeSChristian König i = rdev->debugfs_count + 1; 1031c245cb9eSMichael Witten if (i > RADEON_DEBUGFS_MAX_COMPONENTS) { 1032c245cb9eSMichael Witten DRM_ERROR("Reached maximum number of debugfs components.\n"); 1033c245cb9eSMichael Witten DRM_ERROR("Report so we increase " 1034c245cb9eSMichael Witten "RADEON_DEBUGFS_MAX_COMPONENTS.\n"); 1035771fe6b9SJerome Glisse return -EINVAL; 1036771fe6b9SJerome Glisse } 10374d8bf9aeSChristian König rdev->debugfs[rdev->debugfs_count].files = files; 10384d8bf9aeSChristian König rdev->debugfs[rdev->debugfs_count].num_files = nfiles; 10394d8bf9aeSChristian König rdev->debugfs_count = i; 1040771fe6b9SJerome Glisse #if defined(CONFIG_DEBUG_FS) 1041771fe6b9SJerome Glisse drm_debugfs_create_files(files, nfiles, 1042771fe6b9SJerome Glisse rdev->ddev->control->debugfs_root, 1043771fe6b9SJerome Glisse rdev->ddev->control); 1044771fe6b9SJerome Glisse drm_debugfs_create_files(files, nfiles, 1045771fe6b9SJerome Glisse rdev->ddev->primary->debugfs_root, 1046771fe6b9SJerome Glisse rdev->ddev->primary); 1047771fe6b9SJerome Glisse #endif 1048771fe6b9SJerome Glisse return 0; 1049771fe6b9SJerome Glisse } 1050771fe6b9SJerome Glisse 10514d8bf9aeSChristian König static void radeon_debugfs_remove_files(struct radeon_device *rdev) 10524d8bf9aeSChristian König { 10534d8bf9aeSChristian König #if defined(CONFIG_DEBUG_FS) 10544d8bf9aeSChristian König unsigned i; 10554d8bf9aeSChristian König 10564d8bf9aeSChristian König for (i = 0; i < rdev->debugfs_count; i++) { 10574d8bf9aeSChristian König drm_debugfs_remove_files(rdev->debugfs[i].files, 10584d8bf9aeSChristian König rdev->debugfs[i].num_files, 10594d8bf9aeSChristian König rdev->ddev->control); 10604d8bf9aeSChristian König drm_debugfs_remove_files(rdev->debugfs[i].files, 10614d8bf9aeSChristian König rdev->debugfs[i].num_files, 10624d8bf9aeSChristian König rdev->ddev->primary); 10634d8bf9aeSChristian König } 10644d8bf9aeSChristian König #endif 10654d8bf9aeSChristian König } 10664d8bf9aeSChristian König 1067771fe6b9SJerome Glisse #if defined(CONFIG_DEBUG_FS) 1068771fe6b9SJerome Glisse int radeon_debugfs_init(struct drm_minor *minor) 1069771fe6b9SJerome Glisse { 1070771fe6b9SJerome Glisse return 0; 1071771fe6b9SJerome Glisse } 1072771fe6b9SJerome Glisse 1073771fe6b9SJerome Glisse void radeon_debugfs_cleanup(struct drm_minor *minor) 1074771fe6b9SJerome Glisse { 1075771fe6b9SJerome Glisse } 1076771fe6b9SJerome Glisse #endif 1077