linux/drivers/gpu/drm/radeon/radeon_kms.c
<<
>>
Prefs
   1/*
   2 * Copyright 2008 Advanced Micro Devices, Inc.
   3 * Copyright 2008 Red Hat Inc.
   4 * Copyright 2009 Jerome Glisse.
   5 *
   6 * Permission is hereby granted, free of charge, to any person obtaining a
   7 * copy of this software and associated documentation files (the "Software"),
   8 * to deal in the Software without restriction, including without limitation
   9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10 * and/or sell copies of the Software, and to permit persons to whom the
  11 * Software is furnished to do so, subject to the following conditions:
  12 *
  13 * The above copyright notice and this permission notice shall be included in
  14 * all copies or substantial portions of the Software.
  15 *
  16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22 * OTHER DEALINGS IN THE SOFTWARE.
  23 *
  24 * Authors: Dave Airlie
  25 *          Alex Deucher
  26 *          Jerome Glisse
  27 */
  28#include <drm/drmP.h>
  29#include "radeon.h"
  30#include <drm/radeon_drm.h>
  31#include "radeon_asic.h"
  32
  33#include <linux/vga_switcheroo.h>
  34#include <linux/slab.h>
  35#include <linux/pm_runtime.h>
  36
  37#include "radeon_kfd.h"
  38
  39#if defined(CONFIG_VGA_SWITCHEROO)
  40bool radeon_has_atpx(void);
  41#else
  42static inline bool radeon_has_atpx(void) { return false; }
  43#endif
  44
  45/**
  46 * radeon_driver_unload_kms - Main unload function for KMS.
  47 *
  48 * @dev: drm dev pointer
  49 *
  50 * This is the main unload function for KMS (all asics).
  51 * It calls radeon_modeset_fini() to tear down the
  52 * displays, and radeon_device_fini() to tear down
  53 * the rest of the device (CP, writeback, etc.).
  54 * Returns 0 on success.
  55 */
  56void radeon_driver_unload_kms(struct drm_device *dev)
  57{
  58        struct radeon_device *rdev = dev->dev_private;
  59
  60        if (rdev == NULL)
  61                return;
  62
  63        if (rdev->rmmio == NULL)
  64                goto done_free;
  65
  66        if (radeon_is_px(dev)) {
  67                pm_runtime_get_sync(dev->dev);
  68                pm_runtime_forbid(dev->dev);
  69        }
  70
  71        radeon_kfd_device_fini(rdev);
  72
  73        radeon_acpi_fini(rdev);
  74        
  75        radeon_modeset_fini(rdev);
  76        radeon_device_fini(rdev);
  77
  78done_free:
  79        kfree(rdev);
  80        dev->dev_private = NULL;
  81}
  82
  83/**
  84 * radeon_driver_load_kms - Main load function for KMS.
  85 *
  86 * @dev: drm dev pointer
  87 * @flags: device flags
  88 *
  89 * This is the main load function for KMS (all asics).
  90 * It calls radeon_device_init() to set up the non-display
  91 * parts of the chip (asic init, CP, writeback, etc.), and
  92 * radeon_modeset_init() to set up the display parts
  93 * (crtcs, encoders, hotplug detect, etc.).
  94 * Returns 0 on success, error on failure.
  95 */
  96int radeon_driver_load_kms(struct drm_device *dev, unsigned long flags)
  97{
  98        struct radeon_device *rdev;
  99        int r, acpi_status;
 100
 101        if (!radeon_si_support) {
 102                switch (flags & RADEON_FAMILY_MASK) {
 103                case CHIP_TAHITI:
 104                case CHIP_PITCAIRN:
 105                case CHIP_VERDE:
 106                case CHIP_OLAND:
 107                case CHIP_HAINAN:
 108                        dev_info(dev->dev,
 109                                 "SI support disabled by module param\n");
 110                        return -ENODEV;
 111                }
 112        }
 113        if (!radeon_cik_support) {
 114                switch (flags & RADEON_FAMILY_MASK) {
 115                case CHIP_KAVERI:
 116                case CHIP_BONAIRE:
 117                case CHIP_HAWAII:
 118                case CHIP_KABINI:
 119                case CHIP_MULLINS:
 120                        dev_info(dev->dev,
 121                                 "CIK support disabled by module param\n");
 122                        return -ENODEV;
 123                }
 124        }
 125
 126        rdev = kzalloc(sizeof(struct radeon_device), GFP_KERNEL);
 127        if (rdev == NULL) {
 128                return -ENOMEM;
 129        }
 130        dev->dev_private = (void *)rdev;
 131
 132        /* update BUS flag */
 133        if (pci_find_capability(dev->pdev, PCI_CAP_ID_AGP)) {
 134                flags |= RADEON_IS_AGP;
 135        } else if (pci_is_pcie(dev->pdev)) {
 136                flags |= RADEON_IS_PCIE;
 137        } else {
 138                flags |= RADEON_IS_PCI;
 139        }
 140
 141        if ((radeon_runtime_pm != 0) &&
 142            radeon_has_atpx() &&
 143            ((flags & RADEON_IS_IGP) == 0) &&
 144            !pci_is_thunderbolt_attached(dev->pdev))
 145                flags |= RADEON_IS_PX;
 146
 147        /* radeon_device_init should report only fatal error
 148         * like memory allocation failure or iomapping failure,
 149         * or memory manager initialization failure, it must
 150         * properly initialize the GPU MC controller and permit
 151         * VRAM allocation
 152         */
 153        r = radeon_device_init(rdev, dev, dev->pdev, flags);
 154        if (r) {
 155                dev_err(&dev->pdev->dev, "Fatal error during GPU init\n");
 156                goto out;
 157        }
 158
 159        /* Again modeset_init should fail only on fatal error
 160         * otherwise it should provide enough functionalities
 161         * for shadowfb to run
 162         */
 163        r = radeon_modeset_init(rdev);
 164        if (r)
 165                dev_err(&dev->pdev->dev, "Fatal error during modeset init\n");
 166
 167        /* Call ACPI methods: require modeset init
 168         * but failure is not fatal
 169         */
 170        if (!r) {
 171                acpi_status = radeon_acpi_init(rdev);
 172                if (acpi_status)
 173                dev_dbg(&dev->pdev->dev,
 174                                "Error during ACPI methods call\n");
 175        }
 176
 177        radeon_kfd_device_probe(rdev);
 178        radeon_kfd_device_init(rdev);
 179
 180        if (radeon_is_px(dev)) {
 181                pm_runtime_use_autosuspend(dev->dev);
 182                pm_runtime_set_autosuspend_delay(dev->dev, 5000);
 183                pm_runtime_set_active(dev->dev);
 184                pm_runtime_allow(dev->dev);
 185                pm_runtime_mark_last_busy(dev->dev);
 186                pm_runtime_put_autosuspend(dev->dev);
 187        }
 188
 189out:
 190        if (r)
 191                radeon_driver_unload_kms(dev);
 192
 193
 194        return r;
 195}
 196
 197/**
 198 * radeon_set_filp_rights - Set filp right.
 199 *
 200 * @dev: drm dev pointer
 201 * @owner: drm file
 202 * @applier: drm file
 203 * @value: value
 204 *
 205 * Sets the filp rights for the device (all asics).
 206 */
 207static void radeon_set_filp_rights(struct drm_device *dev,
 208                                   struct drm_file **owner,
 209                                   struct drm_file *applier,
 210                                   uint32_t *value)
 211{
 212        struct radeon_device *rdev = dev->dev_private;
 213
 214        mutex_lock(&rdev->gem.mutex);
 215        if (*value == 1) {
 216                /* wants rights */
 217                if (!*owner)
 218                        *owner = applier;
 219        } else if (*value == 0) {
 220                /* revokes rights */
 221                if (*owner == applier)
 222                        *owner = NULL;
 223        }
 224        *value = *owner == applier ? 1 : 0;
 225        mutex_unlock(&rdev->gem.mutex);
 226}
 227
 228/*
 229 * Userspace get information ioctl
 230 */
 231/**
 232 * radeon_info_ioctl - answer a device specific request.
 233 *
 234 * @rdev: radeon device pointer
 235 * @data: request object
 236 * @filp: drm filp
 237 *
 238 * This function is used to pass device specific parameters to the userspace
 239 * drivers.  Examples include: pci device id, pipeline parms, tiling params,
 240 * etc. (all asics).
 241 * Returns 0 on success, -EINVAL on failure.
 242 */
 243static int radeon_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
 244{
 245        struct radeon_device *rdev = dev->dev_private;
 246        struct drm_radeon_info *info = data;
 247        struct radeon_mode_info *minfo = &rdev->mode_info;
 248        uint32_t *value, value_tmp, *value_ptr, value_size;
 249        uint64_t value64;
 250        struct drm_crtc *crtc;
 251        int i, found;
 252
 253        value_ptr = (uint32_t *)((unsigned long)info->value);
 254        value = &value_tmp;
 255        value_size = sizeof(uint32_t);
 256
 257        switch (info->request) {
 258        case RADEON_INFO_DEVICE_ID:
 259                *value = dev->pdev->device;
 260                break;
 261        case RADEON_INFO_NUM_GB_PIPES:
 262                *value = rdev->num_gb_pipes;
 263                break;
 264        case RADEON_INFO_NUM_Z_PIPES:
 265                *value = rdev->num_z_pipes;
 266                break;
 267        case RADEON_INFO_ACCEL_WORKING:
 268                /* xf86-video-ati 6.13.0 relies on this being false for evergreen */
 269                if ((rdev->family >= CHIP_CEDAR) && (rdev->family <= CHIP_HEMLOCK))
 270                        *value = false;
 271                else
 272                        *value = rdev->accel_working;
 273                break;
 274        case RADEON_INFO_CRTC_FROM_ID:
 275                if (copy_from_user(value, value_ptr, sizeof(uint32_t))) {
 276                        DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
 277                        return -EFAULT;
 278                }
 279                for (i = 0, found = 0; i < rdev->num_crtc; i++) {
 280                        crtc = (struct drm_crtc *)minfo->crtcs[i];
 281                        if (crtc && crtc->base.id == *value) {
 282                                struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
 283                                *value = radeon_crtc->crtc_id;
 284                                found = 1;
 285                                break;
 286                        }
 287                }
 288                if (!found) {
 289                        DRM_DEBUG_KMS("unknown crtc id %d\n", *value);
 290                        return -EINVAL;
 291                }
 292                break;
 293        case RADEON_INFO_ACCEL_WORKING2:
 294                if (rdev->family == CHIP_HAWAII) {
 295                        if (rdev->accel_working) {
 296                                if (rdev->new_fw)
 297                                        *value = 3;
 298                                else
 299                                        *value = 2;
 300                        } else {
 301                                *value = 0;
 302                        }
 303                } else {
 304                        *value = rdev->accel_working;
 305                }
 306                break;
 307        case RADEON_INFO_TILING_CONFIG:
 308                if (rdev->family >= CHIP_BONAIRE)
 309                        *value = rdev->config.cik.tile_config;
 310                else if (rdev->family >= CHIP_TAHITI)
 311                        *value = rdev->config.si.tile_config;
 312                else if (rdev->family >= CHIP_CAYMAN)
 313                        *value = rdev->config.cayman.tile_config;
 314                else if (rdev->family >= CHIP_CEDAR)
 315                        *value = rdev->config.evergreen.tile_config;
 316                else if (rdev->family >= CHIP_RV770)
 317                        *value = rdev->config.rv770.tile_config;
 318                else if (rdev->family >= CHIP_R600)
 319                        *value = rdev->config.r600.tile_config;
 320                else {
 321                        DRM_DEBUG_KMS("tiling config is r6xx+ only!\n");
 322                        return -EINVAL;
 323                }
 324                break;
 325        case RADEON_INFO_WANT_HYPERZ:
 326                /* The "value" here is both an input and output parameter.
 327                 * If the input value is 1, filp requests hyper-z access.
 328                 * If the input value is 0, filp revokes its hyper-z access.
 329                 *
 330                 * When returning, the value is 1 if filp owns hyper-z access,
 331                 * 0 otherwise. */
 332                if (copy_from_user(value, value_ptr, sizeof(uint32_t))) {
 333                        DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
 334                        return -EFAULT;
 335                }
 336                if (*value >= 2) {
 337                        DRM_DEBUG_KMS("WANT_HYPERZ: invalid value %d\n", *value);
 338                        return -EINVAL;
 339                }
 340                radeon_set_filp_rights(dev, &rdev->hyperz_filp, filp, value);
 341                break;
 342        case RADEON_INFO_WANT_CMASK:
 343                /* The same logic as Hyper-Z. */
 344                if (copy_from_user(value, value_ptr, sizeof(uint32_t))) {
 345                        DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
 346                        return -EFAULT;
 347                }
 348                if (*value >= 2) {
 349                        DRM_DEBUG_KMS("WANT_CMASK: invalid value %d\n", *value);
 350                        return -EINVAL;
 351                }
 352                radeon_set_filp_rights(dev, &rdev->cmask_filp, filp, value);
 353                break;
 354        case RADEON_INFO_CLOCK_CRYSTAL_FREQ:
 355                /* return clock value in KHz */
 356                if (rdev->asic->get_xclk)
 357                        *value = radeon_get_xclk(rdev) * 10;
 358                else
 359                        *value = rdev->clock.spll.reference_freq * 10;
 360                break;
 361        case RADEON_INFO_NUM_BACKENDS:
 362                if (rdev->family >= CHIP_BONAIRE)
 363                        *value = rdev->config.cik.max_backends_per_se *
 364                                rdev->config.cik.max_shader_engines;
 365                else if (rdev->family >= CHIP_TAHITI)
 366                        *value = rdev->config.si.max_backends_per_se *
 367                                rdev->config.si.max_shader_engines;
 368                else if (rdev->family >= CHIP_CAYMAN)
 369                        *value = rdev->config.cayman.max_backends_per_se *
 370                                rdev->config.cayman.max_shader_engines;
 371                else if (rdev->family >= CHIP_CEDAR)
 372                        *value = rdev->config.evergreen.max_backends;
 373                else if (rdev->family >= CHIP_RV770)
 374                        *value = rdev->config.rv770.max_backends;
 375                else if (rdev->family >= CHIP_R600)
 376                        *value = rdev->config.r600.max_backends;
 377                else {
 378                        return -EINVAL;
 379                }
 380                break;
 381        case RADEON_INFO_NUM_TILE_PIPES:
 382                if (rdev->family >= CHIP_BONAIRE)
 383                        *value = rdev->config.cik.max_tile_pipes;
 384                else if (rdev->family >= CHIP_TAHITI)
 385                        *value = rdev->config.si.max_tile_pipes;
 386                else if (rdev->family >= CHIP_CAYMAN)
 387                        *value = rdev->config.cayman.max_tile_pipes;
 388                else if (rdev->family >= CHIP_CEDAR)
 389                        *value = rdev->config.evergreen.max_tile_pipes;
 390                else if (rdev->family >= CHIP_RV770)
 391                        *value = rdev->config.rv770.max_tile_pipes;
 392                else if (rdev->family >= CHIP_R600)
 393                        *value = rdev->config.r600.max_tile_pipes;
 394                else {
 395                        return -EINVAL;
 396                }
 397                break;
 398        case RADEON_INFO_FUSION_GART_WORKING:
 399                *value = 1;
 400                break;
 401        case RADEON_INFO_BACKEND_MAP:
 402                if (rdev->family >= CHIP_BONAIRE)
 403                        *value = rdev->config.cik.backend_map;
 404                else if (rdev->family >= CHIP_TAHITI)
 405                        *value = rdev->config.si.backend_map;
 406                else if (rdev->family >= CHIP_CAYMAN)
 407                        *value = rdev->config.cayman.backend_map;
 408                else if (rdev->family >= CHIP_CEDAR)
 409                        *value = rdev->config.evergreen.backend_map;
 410                else if (rdev->family >= CHIP_RV770)
 411                        *value = rdev->config.rv770.backend_map;
 412                else if (rdev->family >= CHIP_R600)
 413                        *value = rdev->config.r600.backend_map;
 414                else {
 415                        return -EINVAL;
 416                }
 417                break;
 418        case RADEON_INFO_VA_START:
 419                /* this is where we report if vm is supported or not */
 420                if (rdev->family < CHIP_CAYMAN)
 421                        return -EINVAL;
 422                *value = RADEON_VA_RESERVED_SIZE;
 423                break;
 424        case RADEON_INFO_IB_VM_MAX_SIZE:
 425                /* this is where we report if vm is supported or not */
 426                if (rdev->family < CHIP_CAYMAN)
 427                        return -EINVAL;
 428                *value = RADEON_IB_VM_MAX_SIZE;
 429                break;
 430        case RADEON_INFO_MAX_PIPES:
 431                if (rdev->family >= CHIP_BONAIRE)
 432                        *value = rdev->config.cik.max_cu_per_sh;
 433                else if (rdev->family >= CHIP_TAHITI)
 434                        *value = rdev->config.si.max_cu_per_sh;
 435                else if (rdev->family >= CHIP_CAYMAN)
 436                        *value = rdev->config.cayman.max_pipes_per_simd;
 437                else if (rdev->family >= CHIP_CEDAR)
 438                        *value = rdev->config.evergreen.max_pipes;
 439                else if (rdev->family >= CHIP_RV770)
 440                        *value = rdev->config.rv770.max_pipes;
 441                else if (rdev->family >= CHIP_R600)
 442                        *value = rdev->config.r600.max_pipes;
 443                else {
 444                        return -EINVAL;
 445                }
 446                break;
 447        case RADEON_INFO_TIMESTAMP:
 448                if (rdev->family < CHIP_R600) {
 449                        DRM_DEBUG_KMS("timestamp is r6xx+ only!\n");
 450                        return -EINVAL;
 451                }
 452                value = (uint32_t*)&value64;
 453                value_size = sizeof(uint64_t);
 454                value64 = radeon_get_gpu_clock_counter(rdev);
 455                break;
 456        case RADEON_INFO_MAX_SE:
 457                if (rdev->family >= CHIP_BONAIRE)
 458                        *value = rdev->config.cik.max_shader_engines;
 459                else if (rdev->family >= CHIP_TAHITI)
 460                        *value = rdev->config.si.max_shader_engines;
 461                else if (rdev->family >= CHIP_CAYMAN)
 462                        *value = rdev->config.cayman.max_shader_engines;
 463                else if (rdev->family >= CHIP_CEDAR)
 464                        *value = rdev->config.evergreen.num_ses;
 465                else
 466                        *value = 1;
 467                break;
 468        case RADEON_INFO_MAX_SH_PER_SE:
 469                if (rdev->family >= CHIP_BONAIRE)
 470                        *value = rdev->config.cik.max_sh_per_se;
 471                else if (rdev->family >= CHIP_TAHITI)
 472                        *value = rdev->config.si.max_sh_per_se;
 473                else
 474                        return -EINVAL;
 475                break;
 476        case RADEON_INFO_FASTFB_WORKING:
 477                *value = rdev->fastfb_working;
 478                break;
 479        case RADEON_INFO_RING_WORKING:
 480                if (copy_from_user(value, value_ptr, sizeof(uint32_t))) {
 481                        DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
 482                        return -EFAULT;
 483                }
 484                switch (*value) {
 485                case RADEON_CS_RING_GFX:
 486                case RADEON_CS_RING_COMPUTE:
 487                        *value = rdev->ring[RADEON_RING_TYPE_GFX_INDEX].ready;
 488                        break;
 489                case RADEON_CS_RING_DMA:
 490                        *value = rdev->ring[R600_RING_TYPE_DMA_INDEX].ready;
 491                        *value |= rdev->ring[CAYMAN_RING_TYPE_DMA1_INDEX].ready;
 492                        break;
 493                case RADEON_CS_RING_UVD:
 494                        *value = rdev->ring[R600_RING_TYPE_UVD_INDEX].ready;
 495                        break;
 496                case RADEON_CS_RING_VCE:
 497                        *value = rdev->ring[TN_RING_TYPE_VCE1_INDEX].ready;
 498                        break;
 499                default:
 500                        return -EINVAL;
 501                }
 502                break;
 503        case RADEON_INFO_SI_TILE_MODE_ARRAY:
 504                if (rdev->family >= CHIP_BONAIRE) {
 505                        value = rdev->config.cik.tile_mode_array;
 506                        value_size = sizeof(uint32_t)*32;
 507                } else if (rdev->family >= CHIP_TAHITI) {
 508                        value = rdev->config.si.tile_mode_array;
 509                        value_size = sizeof(uint32_t)*32;
 510                } else {
 511                        DRM_DEBUG_KMS("tile mode array is si+ only!\n");
 512                        return -EINVAL;
 513                }
 514                break;
 515        case RADEON_INFO_CIK_MACROTILE_MODE_ARRAY:
 516                if (rdev->family >= CHIP_BONAIRE) {
 517                        value = rdev->config.cik.macrotile_mode_array;
 518                        value_size = sizeof(uint32_t)*16;
 519                } else {
 520                        DRM_DEBUG_KMS("macrotile mode array is cik+ only!\n");
 521                        return -EINVAL;
 522                }
 523                break;
 524        case RADEON_INFO_SI_CP_DMA_COMPUTE:
 525                *value = 1;
 526                break;
 527        case RADEON_INFO_SI_BACKEND_ENABLED_MASK:
 528                if (rdev->family >= CHIP_BONAIRE) {
 529                        *value = rdev->config.cik.backend_enable_mask;
 530                } else if (rdev->family >= CHIP_TAHITI) {
 531                        *value = rdev->config.si.backend_enable_mask;
 532                } else {
 533                        DRM_DEBUG_KMS("BACKEND_ENABLED_MASK is si+ only!\n");
 534                }
 535                break;
 536        case RADEON_INFO_MAX_SCLK:
 537                if ((rdev->pm.pm_method == PM_METHOD_DPM) &&
 538                    rdev->pm.dpm_enabled)
 539                        *value = rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac.sclk * 10;
 540                else
 541                        *value = rdev->pm.default_sclk * 10;
 542                break;
 543        case RADEON_INFO_VCE_FW_VERSION:
 544                *value = rdev->vce.fw_version;
 545                break;
 546        case RADEON_INFO_VCE_FB_VERSION:
 547                *value = rdev->vce.fb_version;
 548                break;
 549        case RADEON_INFO_NUM_BYTES_MOVED:
 550                value = (uint32_t*)&value64;
 551                value_size = sizeof(uint64_t);
 552                value64 = atomic64_read(&rdev->num_bytes_moved);
 553                break;
 554        case RADEON_INFO_VRAM_USAGE:
 555                value = (uint32_t*)&value64;
 556                value_size = sizeof(uint64_t);
 557                value64 = atomic64_read(&rdev->vram_usage);
 558                break;
 559        case RADEON_INFO_GTT_USAGE:
 560                value = (uint32_t*)&value64;
 561                value_size = sizeof(uint64_t);
 562                value64 = atomic64_read(&rdev->gtt_usage);
 563                break;
 564        case RADEON_INFO_ACTIVE_CU_COUNT:
 565                if (rdev->family >= CHIP_BONAIRE)
 566                        *value = rdev->config.cik.active_cus;
 567                else if (rdev->family >= CHIP_TAHITI)
 568                        *value = rdev->config.si.active_cus;
 569                else if (rdev->family >= CHIP_CAYMAN)
 570                        *value = rdev->config.cayman.active_simds;
 571                else if (rdev->family >= CHIP_CEDAR)
 572                        *value = rdev->config.evergreen.active_simds;
 573                else if (rdev->family >= CHIP_RV770)
 574                        *value = rdev->config.rv770.active_simds;
 575                else if (rdev->family >= CHIP_R600)
 576                        *value = rdev->config.r600.active_simds;
 577                else
 578                        *value = 1;
 579                break;
 580        case RADEON_INFO_CURRENT_GPU_TEMP:
 581                /* get temperature in millidegrees C */
 582                if (rdev->asic->pm.get_temperature)
 583                        *value = radeon_get_temperature(rdev);
 584                else
 585                        *value = 0;
 586                break;
 587        case RADEON_INFO_CURRENT_GPU_SCLK:
 588                /* get sclk in Mhz */
 589                if (rdev->pm.dpm_enabled)
 590                        *value = radeon_dpm_get_current_sclk(rdev) / 100;
 591                else
 592                        *value = rdev->pm.current_sclk / 100;
 593                break;
 594        case RADEON_INFO_CURRENT_GPU_MCLK:
 595                /* get mclk in Mhz */
 596                if (rdev->pm.dpm_enabled)
 597                        *value = radeon_dpm_get_current_mclk(rdev) / 100;
 598                else
 599                        *value = rdev->pm.current_mclk / 100;
 600                break;
 601        case RADEON_INFO_READ_REG:
 602                if (copy_from_user(value, value_ptr, sizeof(uint32_t))) {
 603                        DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
 604                        return -EFAULT;
 605                }
 606                if (radeon_get_allowed_info_register(rdev, *value, value))
 607                        return -EINVAL;
 608                break;
 609        case RADEON_INFO_VA_UNMAP_WORKING:
 610                *value = true;
 611                break;
 612        case RADEON_INFO_GPU_RESET_COUNTER:
 613                *value = atomic_read(&rdev->gpu_reset_counter);
 614                break;
 615        default:
 616                DRM_DEBUG_KMS("Invalid request %d\n", info->request);
 617                return -EINVAL;
 618        }
 619        if (copy_to_user(value_ptr, (char*)value, value_size)) {
 620                DRM_ERROR("copy_to_user %s:%u\n", __func__, __LINE__);
 621                return -EFAULT;
 622        }
 623        return 0;
 624}
 625
 626
 627/*
 628 * Outdated mess for old drm with Xorg being in charge (void function now).
 629 */
 630/**
 631 * radeon_driver_lastclose_kms - drm callback for last close
 632 *
 633 * @dev: drm dev pointer
 634 *
 635 * Switch vga_switcheroo state after last close (all asics).
 636 */
 637void radeon_driver_lastclose_kms(struct drm_device *dev)
 638{
 639        struct radeon_device *rdev = dev->dev_private;
 640
 641        radeon_fbdev_restore_mode(rdev);
 642        vga_switcheroo_process_delayed_switch();
 643}
 644
 645/**
 646 * radeon_driver_open_kms - drm callback for open
 647 *
 648 * @dev: drm dev pointer
 649 * @file_priv: drm file
 650 *
 651 * On device open, init vm on cayman+ (all asics).
 652 * Returns 0 on success, error on failure.
 653 */
 654int radeon_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
 655{
 656        struct radeon_device *rdev = dev->dev_private;
 657        int r;
 658
 659        file_priv->driver_priv = NULL;
 660
 661        r = pm_runtime_get_sync(dev->dev);
 662        if (r < 0)
 663                return r;
 664
 665        /* new gpu have virtual address space support */
 666        if (rdev->family >= CHIP_CAYMAN) {
 667                struct radeon_fpriv *fpriv;
 668                struct radeon_vm *vm;
 669
 670                fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
 671                if (unlikely(!fpriv)) {
 672                        r = -ENOMEM;
 673                        goto out_suspend;
 674                }
 675
 676                if (rdev->accel_working) {
 677                        vm = &fpriv->vm;
 678                        r = radeon_vm_init(rdev, vm);
 679                        if (r) {
 680                                kfree(fpriv);
 681                                goto out_suspend;
 682                        }
 683
 684                        r = radeon_bo_reserve(rdev->ring_tmp_bo.bo, false);
 685                        if (r) {
 686                                radeon_vm_fini(rdev, vm);
 687                                kfree(fpriv);
 688                                goto out_suspend;
 689                        }
 690
 691                        /* map the ib pool buffer read only into
 692                         * virtual address space */
 693                        vm->ib_bo_va = radeon_vm_bo_add(rdev, vm,
 694                                                        rdev->ring_tmp_bo.bo);
 695                        r = radeon_vm_bo_set_addr(rdev, vm->ib_bo_va,
 696                                                  RADEON_VA_IB_OFFSET,
 697                                                  RADEON_VM_PAGE_READABLE |
 698                                                  RADEON_VM_PAGE_SNOOPED);
 699                        if (r) {
 700                                radeon_vm_fini(rdev, vm);
 701                                kfree(fpriv);
 702                                goto out_suspend;
 703                        }
 704                }
 705                file_priv->driver_priv = fpriv;
 706        }
 707
 708out_suspend:
 709        pm_runtime_mark_last_busy(dev->dev);
 710        pm_runtime_put_autosuspend(dev->dev);
 711        return r;
 712}
 713
 714/**
 715 * radeon_driver_postclose_kms - drm callback for post close
 716 *
 717 * @dev: drm dev pointer
 718 * @file_priv: drm file
 719 *
 720 * On device close, tear down hyperz and cmask filps on r1xx-r5xx
 721 * (all asics).  And tear down vm on cayman+ (all asics).
 722 */
 723void radeon_driver_postclose_kms(struct drm_device *dev,
 724                                 struct drm_file *file_priv)
 725{
 726        struct radeon_device *rdev = dev->dev_private;
 727
 728        pm_runtime_get_sync(dev->dev);
 729
 730        mutex_lock(&rdev->gem.mutex);
 731        if (rdev->hyperz_filp == file_priv)
 732                rdev->hyperz_filp = NULL;
 733        if (rdev->cmask_filp == file_priv)
 734                rdev->cmask_filp = NULL;
 735        mutex_unlock(&rdev->gem.mutex);
 736
 737        radeon_uvd_free_handles(rdev, file_priv);
 738        radeon_vce_free_handles(rdev, file_priv);
 739
 740        /* new gpu have virtual address space support */
 741        if (rdev->family >= CHIP_CAYMAN && file_priv->driver_priv) {
 742                struct radeon_fpriv *fpriv = file_priv->driver_priv;
 743                struct radeon_vm *vm = &fpriv->vm;
 744                int r;
 745
 746                if (rdev->accel_working) {
 747                        r = radeon_bo_reserve(rdev->ring_tmp_bo.bo, false);
 748                        if (!r) {
 749                                if (vm->ib_bo_va)
 750                                        radeon_vm_bo_rmv(rdev, vm->ib_bo_va);
 751                                radeon_bo_unreserve(rdev->ring_tmp_bo.bo);
 752                        }
 753                        radeon_vm_fini(rdev, vm);
 754                }
 755
 756                kfree(fpriv);
 757                file_priv->driver_priv = NULL;
 758        }
 759        pm_runtime_mark_last_busy(dev->dev);
 760        pm_runtime_put_autosuspend(dev->dev);
 761}
 762
 763/*
 764 * VBlank related functions.
 765 */
 766/**
 767 * radeon_get_vblank_counter_kms - get frame count
 768 *
 769 * @dev: drm dev pointer
 770 * @pipe: crtc to get the frame count from
 771 *
 772 * Gets the frame count on the requested crtc (all asics).
 773 * Returns frame count on success, -EINVAL on failure.
 774 */
 775u32 radeon_get_vblank_counter_kms(struct drm_device *dev, unsigned int pipe)
 776{
 777        int vpos, hpos, stat;
 778        u32 count;
 779        struct radeon_device *rdev = dev->dev_private;
 780
 781        if (pipe >= rdev->num_crtc) {
 782                DRM_ERROR("Invalid crtc %u\n", pipe);
 783                return -EINVAL;
 784        }
 785
 786        /* The hw increments its frame counter at start of vsync, not at start
 787         * of vblank, as is required by DRM core vblank counter handling.
 788         * Cook the hw count here to make it appear to the caller as if it
 789         * incremented at start of vblank. We measure distance to start of
 790         * vblank in vpos. vpos therefore will be >= 0 between start of vblank
 791         * and start of vsync, so vpos >= 0 means to bump the hw frame counter
 792         * result by 1 to give the proper appearance to caller.
 793         */
 794        if (rdev->mode_info.crtcs[pipe]) {
 795                /* Repeat readout if needed to provide stable result if
 796                 * we cross start of vsync during the queries.
 797                 */
 798                do {
 799                        count = radeon_get_vblank_counter(rdev, pipe);
 800                        /* Ask radeon_get_crtc_scanoutpos to return vpos as
 801                         * distance to start of vblank, instead of regular
 802                         * vertical scanout pos.
 803                         */
 804                        stat = radeon_get_crtc_scanoutpos(
 805                                dev, pipe, GET_DISTANCE_TO_VBLANKSTART,
 806                                &vpos, &hpos, NULL, NULL,
 807                                &rdev->mode_info.crtcs[pipe]->base.hwmode);
 808                } while (count != radeon_get_vblank_counter(rdev, pipe));
 809
 810                if (((stat & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE)) !=
 811                    (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE))) {
 812                        DRM_DEBUG_VBL("Query failed! stat %d\n", stat);
 813                }
 814                else {
 815                        DRM_DEBUG_VBL("crtc %u: dist from vblank start %d\n",
 816                                      pipe, vpos);
 817
 818                        /* Bump counter if we are at >= leading edge of vblank,
 819                         * but before vsync where vpos would turn negative and
 820                         * the hw counter really increments.
 821                         */
 822                        if (vpos >= 0)
 823                                count++;
 824                }
 825        }
 826        else {
 827            /* Fallback to use value as is. */
 828            count = radeon_get_vblank_counter(rdev, pipe);
 829            DRM_DEBUG_VBL("NULL mode info! Returned count may be wrong.\n");
 830        }
 831
 832        return count;
 833}
 834
 835/**
 836 * radeon_enable_vblank_kms - enable vblank interrupt
 837 *
 838 * @dev: drm dev pointer
 839 * @crtc: crtc to enable vblank interrupt for
 840 *
 841 * Enable the interrupt on the requested crtc (all asics).
 842 * Returns 0 on success, -EINVAL on failure.
 843 */
 844int radeon_enable_vblank_kms(struct drm_device *dev, int crtc)
 845{
 846        struct radeon_device *rdev = dev->dev_private;
 847        unsigned long irqflags;
 848        int r;
 849
 850        if (crtc < 0 || crtc >= rdev->num_crtc) {
 851                DRM_ERROR("Invalid crtc %d\n", crtc);
 852                return -EINVAL;
 853        }
 854
 855        spin_lock_irqsave(&rdev->irq.lock, irqflags);
 856        rdev->irq.crtc_vblank_int[crtc] = true;
 857        r = radeon_irq_set(rdev);
 858        spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
 859        return r;
 860}
 861
 862/**
 863 * radeon_disable_vblank_kms - disable vblank interrupt
 864 *
 865 * @dev: drm dev pointer
 866 * @crtc: crtc to disable vblank interrupt for
 867 *
 868 * Disable the interrupt on the requested crtc (all asics).
 869 */
 870void radeon_disable_vblank_kms(struct drm_device *dev, int crtc)
 871{
 872        struct radeon_device *rdev = dev->dev_private;
 873        unsigned long irqflags;
 874
 875        if (crtc < 0 || crtc >= rdev->num_crtc) {
 876                DRM_ERROR("Invalid crtc %d\n", crtc);
 877                return;
 878        }
 879
 880        spin_lock_irqsave(&rdev->irq.lock, irqflags);
 881        rdev->irq.crtc_vblank_int[crtc] = false;
 882        radeon_irq_set(rdev);
 883        spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
 884}
 885
 886const struct drm_ioctl_desc radeon_ioctls_kms[] = {
 887        DRM_IOCTL_DEF_DRV(RADEON_CP_INIT, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
 888        DRM_IOCTL_DEF_DRV(RADEON_CP_START, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
 889        DRM_IOCTL_DEF_DRV(RADEON_CP_STOP, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
 890        DRM_IOCTL_DEF_DRV(RADEON_CP_RESET, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
 891        DRM_IOCTL_DEF_DRV(RADEON_CP_IDLE, drm_invalid_op, DRM_AUTH),
 892        DRM_IOCTL_DEF_DRV(RADEON_CP_RESUME, drm_invalid_op, DRM_AUTH),
 893        DRM_IOCTL_DEF_DRV(RADEON_RESET, drm_invalid_op, DRM_AUTH),
 894        DRM_IOCTL_DEF_DRV(RADEON_FULLSCREEN, drm_invalid_op, DRM_AUTH),
 895        DRM_IOCTL_DEF_DRV(RADEON_SWAP, drm_invalid_op, DRM_AUTH),
 896        DRM_IOCTL_DEF_DRV(RADEON_CLEAR, drm_invalid_op, DRM_AUTH),
 897        DRM_IOCTL_DEF_DRV(RADEON_VERTEX, drm_invalid_op, DRM_AUTH),
 898        DRM_IOCTL_DEF_DRV(RADEON_INDICES, drm_invalid_op, DRM_AUTH),
 899        DRM_IOCTL_DEF_DRV(RADEON_TEXTURE, drm_invalid_op, DRM_AUTH),
 900        DRM_IOCTL_DEF_DRV(RADEON_STIPPLE, drm_invalid_op, DRM_AUTH),
 901        DRM_IOCTL_DEF_DRV(RADEON_INDIRECT, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
 902        DRM_IOCTL_DEF_DRV(RADEON_VERTEX2, drm_invalid_op, DRM_AUTH),
 903        DRM_IOCTL_DEF_DRV(RADEON_CMDBUF, drm_invalid_op, DRM_AUTH),
 904        DRM_IOCTL_DEF_DRV(RADEON_GETPARAM, drm_invalid_op, DRM_AUTH),
 905        DRM_IOCTL_DEF_DRV(RADEON_FLIP, drm_invalid_op, DRM_AUTH),
 906        DRM_IOCTL_DEF_DRV(RADEON_ALLOC, drm_invalid_op, DRM_AUTH),
 907        DRM_IOCTL_DEF_DRV(RADEON_FREE, drm_invalid_op, DRM_AUTH),
 908        DRM_IOCTL_DEF_DRV(RADEON_INIT_HEAP, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
 909        DRM_IOCTL_DEF_DRV(RADEON_IRQ_EMIT, drm_invalid_op, DRM_AUTH),
 910        DRM_IOCTL_DEF_DRV(RADEON_IRQ_WAIT, drm_invalid_op, DRM_AUTH),
 911        DRM_IOCTL_DEF_DRV(RADEON_SETPARAM, drm_invalid_op, DRM_AUTH),
 912        DRM_IOCTL_DEF_DRV(RADEON_SURF_ALLOC, drm_invalid_op, DRM_AUTH),
 913        DRM_IOCTL_DEF_DRV(RADEON_SURF_FREE, drm_invalid_op, DRM_AUTH),
 914        /* KMS */
 915        DRM_IOCTL_DEF_DRV(RADEON_GEM_INFO, radeon_gem_info_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 916        DRM_IOCTL_DEF_DRV(RADEON_GEM_CREATE, radeon_gem_create_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 917        DRM_IOCTL_DEF_DRV(RADEON_GEM_MMAP, radeon_gem_mmap_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 918        DRM_IOCTL_DEF_DRV(RADEON_GEM_SET_DOMAIN, radeon_gem_set_domain_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 919        DRM_IOCTL_DEF_DRV(RADEON_GEM_PREAD, radeon_gem_pread_ioctl, DRM_AUTH),
 920        DRM_IOCTL_DEF_DRV(RADEON_GEM_PWRITE, radeon_gem_pwrite_ioctl, DRM_AUTH),
 921        DRM_IOCTL_DEF_DRV(RADEON_GEM_WAIT_IDLE, radeon_gem_wait_idle_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 922        DRM_IOCTL_DEF_DRV(RADEON_CS, radeon_cs_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 923        DRM_IOCTL_DEF_DRV(RADEON_INFO, radeon_info_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 924        DRM_IOCTL_DEF_DRV(RADEON_GEM_SET_TILING, radeon_gem_set_tiling_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 925        DRM_IOCTL_DEF_DRV(RADEON_GEM_GET_TILING, radeon_gem_get_tiling_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 926        DRM_IOCTL_DEF_DRV(RADEON_GEM_BUSY, radeon_gem_busy_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 927        DRM_IOCTL_DEF_DRV(RADEON_GEM_VA, radeon_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 928        DRM_IOCTL_DEF_DRV(RADEON_GEM_OP, radeon_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 929        DRM_IOCTL_DEF_DRV(RADEON_GEM_USERPTR, radeon_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 930};
 931int radeon_max_kms_ioctl = ARRAY_SIZE(radeon_ioctls_kms);
 932