blob: cb8e94d1a2b204f0da02e27654c80a1d48de44a2 [file] [log] [blame]
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001/*
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 "drmP.h"
29#include "drm_sarea.h"
30#include "radeon.h"
31#include "radeon_drm.h"
Marek Olšák6759a0a2012-08-09 16:34:17 +020032#include "radeon_asic.h"
Jerome Glisse771fe6b2009-06-05 14:42:42 +020033
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100034#include <linux/vga_switcheroo.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100036
Alex Deucherf482a142012-07-17 14:02:34 -040037/**
38 * radeon_driver_unload_kms - Main unload function for KMS.
39 *
40 * @dev: drm dev pointer
41 *
42 * This is the main unload function for KMS (all asics).
43 * It calls radeon_modeset_fini() to tear down the
44 * displays, and radeon_device_fini() to tear down
45 * the rest of the device (CP, writeback, etc.).
46 * Returns 0 on success.
47 */
Jerome Glissecf0fe452009-12-09 18:21:55 +010048int radeon_driver_unload_kms(struct drm_device *dev)
49{
50 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020051
Jerome Glissecf0fe452009-12-09 18:21:55 +010052 if (rdev == NULL)
53 return 0;
Alex Deucherc4917072012-07-31 17:14:35 -040054 radeon_acpi_fini(rdev);
Jerome Glissecf0fe452009-12-09 18:21:55 +010055 radeon_modeset_fini(rdev);
56 radeon_device_fini(rdev);
57 kfree(rdev);
58 dev->dev_private = NULL;
59 return 0;
60}
61
Alex Deucherf482a142012-07-17 14:02:34 -040062/**
63 * radeon_driver_load_kms - Main load function for KMS.
64 *
65 * @dev: drm dev pointer
66 * @flags: device flags
67 *
68 * This is the main load function for KMS (all asics).
69 * It calls radeon_device_init() to set up the non-display
70 * parts of the chip (asic init, CP, writeback, etc.), and
71 * radeon_modeset_init() to set up the display parts
72 * (crtcs, encoders, hotplug detect, etc.).
73 * Returns 0 on success, error on failure.
74 */
Jerome Glisse771fe6b2009-06-05 14:42:42 +020075int radeon_driver_load_kms(struct drm_device *dev, unsigned long flags)
76{
77 struct radeon_device *rdev;
Alberto Miloned7a29522010-07-06 11:40:24 -040078 int r, acpi_status;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020079
80 rdev = kzalloc(sizeof(struct radeon_device), GFP_KERNEL);
81 if (rdev == NULL) {
82 return -ENOMEM;
83 }
84 dev->dev_private = (void *)rdev;
85
86 /* update BUS flag */
Dave Airlie8410ea32010-12-15 03:16:38 +100087 if (drm_pci_device_is_agp(dev)) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +020088 flags |= RADEON_IS_AGP;
Jon Mason58b65422011-06-27 16:07:50 +000089 } else if (pci_is_pcie(dev->pdev)) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +020090 flags |= RADEON_IS_PCIE;
91 } else {
92 flags |= RADEON_IS_PCI;
93 }
94
Jerome Glisse6cf8a3f2009-09-10 21:46:48 +020095 /* radeon_device_init should report only fatal error
96 * like memory allocation failure or iomapping failure,
97 * or memory manager initialization failure, it must
98 * properly initialize the GPU MC controller and permit
99 * VRAM allocation
100 */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200101 r = radeon_device_init(rdev, dev, dev->pdev, flags);
102 if (r) {
Jerome Glissecf0fe452009-12-09 18:21:55 +0100103 dev_err(&dev->pdev->dev, "Fatal error during GPU init\n");
104 goto out;
Jerome Glisse6cf8a3f2009-09-10 21:46:48 +0200105 }
Alberto Miloned7a29522010-07-06 11:40:24 -0400106
Jerome Glisse6cf8a3f2009-09-10 21:46:48 +0200107 /* Again modeset_init should fail only on fatal error
108 * otherwise it should provide enough functionalities
109 * for shadowfb to run
110 */
111 r = radeon_modeset_init(rdev);
Jerome Glissecf0fe452009-12-09 18:21:55 +0100112 if (r)
113 dev_err(&dev->pdev->dev, "Fatal error during modeset init\n");
Luca Tettamantifda4b252012-07-30 21:20:35 +0200114
115 /* Call ACPI methods: require modeset init
116 * but failure is not fatal
117 */
118 if (!r) {
119 acpi_status = radeon_acpi_init(rdev);
120 if (acpi_status)
121 dev_dbg(&dev->pdev->dev,
122 "Error during ACPI methods call\n");
123 }
124
Jerome Glissecf0fe452009-12-09 18:21:55 +0100125out:
126 if (r)
127 radeon_driver_unload_kms(dev);
128 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200129}
130
Alex Deucherf482a142012-07-17 14:02:34 -0400131/**
132 * radeon_set_filp_rights - Set filp right.
133 *
134 * @dev: drm dev pointer
135 * @owner: drm file
136 * @applier: drm file
137 * @value: value
138 *
139 * Sets the filp rights for the device (all asics).
140 */
Marek Olšák9eba4a92011-01-05 05:46:48 +0100141static void radeon_set_filp_rights(struct drm_device *dev,
142 struct drm_file **owner,
143 struct drm_file *applier,
144 uint32_t *value)
145{
146 mutex_lock(&dev->struct_mutex);
147 if (*value == 1) {
148 /* wants rights */
149 if (!*owner)
150 *owner = applier;
151 } else if (*value == 0) {
152 /* revokes rights */
153 if (*owner == applier)
154 *owner = NULL;
155 }
156 *value = *owner == applier ? 1 : 0;
157 mutex_unlock(&dev->struct_mutex);
158}
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200159
160/*
Marek Olšák9eba4a92011-01-05 05:46:48 +0100161 * Userspace get information ioctl
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200162 */
Alex Deucherf482a142012-07-17 14:02:34 -0400163/**
164 * radeon_info_ioctl - answer a device specific request.
165 *
166 * @rdev: radeon device pointer
167 * @data: request object
168 * @filp: drm filp
169 *
170 * This function is used to pass device specific parameters to the userspace
171 * drivers. Examples include: pci device id, pipeline parms, tiling params,
172 * etc. (all asics).
173 * Returns 0 on success, -EINVAL on failure.
174 */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200175int radeon_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
176{
177 struct radeon_device *rdev = dev->dev_private;
Marek Olšák6759a0a2012-08-09 16:34:17 +0200178 struct drm_radeon_info *info = data;
Jerome Glissebc35afd2010-05-12 18:01:13 +0200179 struct radeon_mode_info *minfo = &rdev->mode_info;
Marek Olšák6759a0a2012-08-09 16:34:17 +0200180 uint32_t value, *value_ptr;
181 uint64_t value64, *value_ptr64;
Jerome Glissebc35afd2010-05-12 18:01:13 +0200182 struct drm_crtc *crtc;
183 int i, found;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200184
Marek Olšák6759a0a2012-08-09 16:34:17 +0200185 /* TIMESTAMP is a 64-bit value, needs special handling. */
186 if (info->request == RADEON_INFO_TIMESTAMP) {
187 if (rdev->family >= CHIP_R600) {
188 value_ptr64 = (uint64_t*)((unsigned long)info->value);
189 if (rdev->family >= CHIP_TAHITI) {
190 value64 = si_get_gpu_clock(rdev);
191 } else {
192 value64 = r600_get_gpu_clock(rdev);
193 }
194
195 if (DRM_COPY_TO_USER(value_ptr64, &value64, sizeof(value64))) {
196 DRM_ERROR("copy_to_user %s:%u\n", __func__, __LINE__);
197 return -EFAULT;
198 }
199 return 0;
200 } else {
201 DRM_DEBUG_KMS("timestamp is r6xx+ only!\n");
202 return -EINVAL;
203 }
204 }
205
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200206 value_ptr = (uint32_t *)((unsigned long)info->value);
Marek Olšák6759a0a2012-08-09 16:34:17 +0200207 if (DRM_COPY_FROM_USER(&value, value_ptr, sizeof(value))) {
208 DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
Dr. David Alan Gilbertd8ab3552010-08-02 09:43:52 +1000209 return -EFAULT;
Marek Olšák6759a0a2012-08-09 16:34:17 +0200210 }
Dr. David Alan Gilbertd8ab3552010-08-02 09:43:52 +1000211
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200212 switch (info->request) {
213 case RADEON_INFO_DEVICE_ID:
214 value = dev->pci_device;
215 break;
216 case RADEON_INFO_NUM_GB_PIPES:
217 value = rdev->num_gb_pipes;
218 break;
Alex Deucherf779b3e2009-08-19 19:11:39 -0400219 case RADEON_INFO_NUM_Z_PIPES:
220 value = rdev->num_z_pipes;
221 break;
Jerome Glisse733289c2009-09-16 15:24:21 +0200222 case RADEON_INFO_ACCEL_WORKING:
Alex Deucher148a03b2010-06-03 19:00:03 -0400223 /* xf86-video-ati 6.13.0 relies on this being false for evergreen */
224 if ((rdev->family >= CHIP_CEDAR) && (rdev->family <= CHIP_HEMLOCK))
225 value = false;
226 else
227 value = rdev->accel_working;
Jerome Glisse733289c2009-09-16 15:24:21 +0200228 break;
Jerome Glissebc35afd2010-05-12 18:01:13 +0200229 case RADEON_INFO_CRTC_FROM_ID:
230 for (i = 0, found = 0; i < rdev->num_crtc; i++) {
231 crtc = (struct drm_crtc *)minfo->crtcs[i];
232 if (crtc && crtc->base.id == value) {
Alex Deucher0baf2d82010-07-21 14:05:35 -0400233 struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
234 value = radeon_crtc->crtc_id;
Jerome Glissebc35afd2010-05-12 18:01:13 +0200235 found = 1;
236 break;
237 }
238 }
239 if (!found) {
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000240 DRM_DEBUG_KMS("unknown crtc id %d\n", value);
Jerome Glissebc35afd2010-05-12 18:01:13 +0200241 return -EINVAL;
242 }
243 break;
Alex Deucher148a03b2010-06-03 19:00:03 -0400244 case RADEON_INFO_ACCEL_WORKING2:
245 value = rdev->accel_working;
246 break;
Alex Deuchere7aeeba2010-06-04 13:10:12 -0400247 case RADEON_INFO_TILING_CONFIG:
Michel Dänzerc1b2f692012-03-20 17:18:26 -0400248 if (rdev->family >= CHIP_TAHITI)
249 value = rdev->config.si.tile_config;
250 else if (rdev->family >= CHIP_CAYMAN)
Alex Deucherfecf1d02011-03-02 20:07:29 -0500251 value = rdev->config.cayman.tile_config;
252 else if (rdev->family >= CHIP_CEDAR)
Alex Deuchere7aeeba2010-06-04 13:10:12 -0400253 value = rdev->config.evergreen.tile_config;
254 else if (rdev->family >= CHIP_RV770)
255 value = rdev->config.rv770.tile_config;
256 else if (rdev->family >= CHIP_R600)
257 value = rdev->config.r600.tile_config;
258 else {
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000259 DRM_DEBUG_KMS("tiling config is r6xx+ only!\n");
Alex Deuchere7aeeba2010-06-04 13:10:12 -0400260 return -EINVAL;
261 }
Alex Deucherb824b362010-08-12 08:25:47 -0400262 break;
Dave Airlieab9e1f52010-07-13 11:11:11 +1000263 case RADEON_INFO_WANT_HYPERZ:
Marek Olšák43861f72010-08-07 03:36:34 +0200264 /* The "value" here is both an input and output parameter.
265 * If the input value is 1, filp requests hyper-z access.
266 * If the input value is 0, filp revokes its hyper-z access.
267 *
268 * When returning, the value is 1 if filp owns hyper-z access,
269 * 0 otherwise. */
270 if (value >= 2) {
271 DRM_DEBUG_KMS("WANT_HYPERZ: invalid value %d\n", value);
272 return -EINVAL;
Dave Airlieab9e1f52010-07-13 11:11:11 +1000273 }
Marek Olšák9eba4a92011-01-05 05:46:48 +0100274 radeon_set_filp_rights(dev, &rdev->hyperz_filp, filp, &value);
275 break;
276 case RADEON_INFO_WANT_CMASK:
277 /* The same logic as Hyper-Z. */
278 if (value >= 2) {
279 DRM_DEBUG_KMS("WANT_CMASK: invalid value %d\n", value);
280 return -EINVAL;
Marek Olšák43861f72010-08-07 03:36:34 +0200281 }
Marek Olšák9eba4a92011-01-05 05:46:48 +0100282 radeon_set_filp_rights(dev, &rdev->cmask_filp, filp, &value);
Alex Deuchere7aeeba2010-06-04 13:10:12 -0400283 break;
Alex Deucher58bbf012011-01-24 17:14:26 -0500284 case RADEON_INFO_CLOCK_CRYSTAL_FREQ:
285 /* return clock value in KHz */
286 value = rdev->clock.spll.reference_freq * 10;
287 break;
Dave Airlie486af182011-03-01 14:32:27 +1000288 case RADEON_INFO_NUM_BACKENDS:
Michel Dänzerc1b2f692012-03-20 17:18:26 -0400289 if (rdev->family >= CHIP_TAHITI)
290 value = rdev->config.si.max_backends_per_se *
291 rdev->config.si.max_shader_engines;
292 else if (rdev->family >= CHIP_CAYMAN)
Alex Deucherfecf1d02011-03-02 20:07:29 -0500293 value = rdev->config.cayman.max_backends_per_se *
294 rdev->config.cayman.max_shader_engines;
295 else if (rdev->family >= CHIP_CEDAR)
Dave Airlie486af182011-03-01 14:32:27 +1000296 value = rdev->config.evergreen.max_backends;
297 else if (rdev->family >= CHIP_RV770)
298 value = rdev->config.rv770.max_backends;
299 else if (rdev->family >= CHIP_R600)
300 value = rdev->config.r600.max_backends;
301 else {
302 return -EINVAL;
303 }
304 break;
Alex Deucher65659452011-04-26 13:27:43 -0400305 case RADEON_INFO_NUM_TILE_PIPES:
Michel Dänzerc1b2f692012-03-20 17:18:26 -0400306 if (rdev->family >= CHIP_TAHITI)
307 value = rdev->config.si.max_tile_pipes;
308 else if (rdev->family >= CHIP_CAYMAN)
Alex Deucher65659452011-04-26 13:27:43 -0400309 value = rdev->config.cayman.max_tile_pipes;
310 else if (rdev->family >= CHIP_CEDAR)
311 value = rdev->config.evergreen.max_tile_pipes;
312 else if (rdev->family >= CHIP_RV770)
313 value = rdev->config.rv770.max_tile_pipes;
314 else if (rdev->family >= CHIP_R600)
315 value = rdev->config.r600.max_tile_pipes;
316 else {
317 return -EINVAL;
318 }
319 break;
Alex Deucher8aeb96f2011-05-03 19:28:02 -0400320 case RADEON_INFO_FUSION_GART_WORKING:
321 value = 1;
322 break;
Alex Deuchere55b9422011-07-15 19:53:52 +0000323 case RADEON_INFO_BACKEND_MAP:
Michel Dänzerc1b2f692012-03-20 17:18:26 -0400324 if (rdev->family >= CHIP_TAHITI)
325 value = rdev->config.si.backend_map;
326 else if (rdev->family >= CHIP_CAYMAN)
Alex Deuchere55b9422011-07-15 19:53:52 +0000327 value = rdev->config.cayman.backend_map;
328 else if (rdev->family >= CHIP_CEDAR)
329 value = rdev->config.evergreen.backend_map;
330 else if (rdev->family >= CHIP_RV770)
331 value = rdev->config.rv770.backend_map;
332 else if (rdev->family >= CHIP_R600)
333 value = rdev->config.r600.backend_map;
334 else {
335 return -EINVAL;
336 }
337 break;
Jerome Glisse721604a2012-01-05 22:11:05 -0500338 case RADEON_INFO_VA_START:
339 /* this is where we report if vm is supported or not */
340 if (rdev->family < CHIP_CAYMAN)
341 return -EINVAL;
342 value = RADEON_VA_RESERVED_SIZE;
343 break;
344 case RADEON_INFO_IB_VM_MAX_SIZE:
345 /* this is where we report if vm is supported or not */
346 if (rdev->family < CHIP_CAYMAN)
347 return -EINVAL;
348 value = RADEON_IB_VM_MAX_SIZE;
349 break;
Tom Stellard609c1e12012-03-20 17:17:55 -0400350 case RADEON_INFO_MAX_PIPES:
Michel Dänzerc1b2f692012-03-20 17:18:26 -0400351 if (rdev->family >= CHIP_TAHITI)
Alex Deucher1a8ca752012-06-01 18:58:22 -0400352 value = rdev->config.si.max_cu_per_sh;
Michel Dänzerc1b2f692012-03-20 17:18:26 -0400353 else if (rdev->family >= CHIP_CAYMAN)
Tom Stellard609c1e12012-03-20 17:17:55 -0400354 value = rdev->config.cayman.max_pipes_per_simd;
355 else if (rdev->family >= CHIP_CEDAR)
356 value = rdev->config.evergreen.max_pipes;
357 else if (rdev->family >= CHIP_RV770)
358 value = rdev->config.rv770.max_pipes;
359 else if (rdev->family >= CHIP_R600)
360 value = rdev->config.r600.max_pipes;
361 else {
362 return -EINVAL;
363 }
364 break;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200365 default:
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000366 DRM_DEBUG_KMS("Invalid request %d\n", info->request);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200367 return -EINVAL;
368 }
369 if (DRM_COPY_TO_USER(value_ptr, &value, sizeof(uint32_t))) {
Marek Olšák6759a0a2012-08-09 16:34:17 +0200370 DRM_ERROR("copy_to_user %s:%u\n", __func__, __LINE__);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200371 return -EFAULT;
372 }
373 return 0;
374}
375
376
377/*
378 * Outdated mess for old drm with Xorg being in charge (void function now).
379 */
Alex Deucherf482a142012-07-17 14:02:34 -0400380/**
381 * radeon_driver_firstopen_kms - drm callback for first open
382 *
383 * @dev: drm dev pointer
384 *
385 * Nothing to be done for KMS (all asics).
386 * Returns 0 on success.
387 */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200388int radeon_driver_firstopen_kms(struct drm_device *dev)
389{
390 return 0;
391}
392
Alex Deucherf482a142012-07-17 14:02:34 -0400393/**
394 * radeon_driver_firstopen_kms - drm callback for last close
395 *
396 * @dev: drm dev pointer
397 *
398 * Switch vga switcheroo state after last close (all asics).
399 */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200400void radeon_driver_lastclose_kms(struct drm_device *dev)
401{
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000402 vga_switcheroo_process_delayed_switch();
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200403}
404
Alex Deucherf482a142012-07-17 14:02:34 -0400405/**
406 * radeon_driver_open_kms - drm callback for open
407 *
408 * @dev: drm dev pointer
409 * @file_priv: drm file
410 *
411 * On device open, init vm on cayman+ (all asics).
412 * Returns 0 on success, error on failure.
413 */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200414int radeon_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
415{
Jerome Glisse721604a2012-01-05 22:11:05 -0500416 struct radeon_device *rdev = dev->dev_private;
417
418 file_priv->driver_priv = NULL;
419
420 /* new gpu have virtual address space support */
421 if (rdev->family >= CHIP_CAYMAN) {
422 struct radeon_fpriv *fpriv;
423 int r;
424
425 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
426 if (unlikely(!fpriv)) {
427 return -ENOMEM;
428 }
429
430 r = radeon_vm_init(rdev, &fpriv->vm);
431 if (r) {
432 radeon_vm_fini(rdev, &fpriv->vm);
433 kfree(fpriv);
434 return r;
435 }
436
437 file_priv->driver_priv = fpriv;
438 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200439 return 0;
440}
441
Alex Deucherf482a142012-07-17 14:02:34 -0400442/**
443 * radeon_driver_postclose_kms - drm callback for post close
444 *
445 * @dev: drm dev pointer
446 * @file_priv: drm file
447 *
448 * On device post close, tear down vm on cayman+ (all asics).
449 */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200450void radeon_driver_postclose_kms(struct drm_device *dev,
451 struct drm_file *file_priv)
452{
Jerome Glisse721604a2012-01-05 22:11:05 -0500453 struct radeon_device *rdev = dev->dev_private;
454
455 /* new gpu have virtual address space support */
456 if (rdev->family >= CHIP_CAYMAN && file_priv->driver_priv) {
457 struct radeon_fpriv *fpriv = file_priv->driver_priv;
458
459 radeon_vm_fini(rdev, &fpriv->vm);
460 kfree(fpriv);
461 file_priv->driver_priv = NULL;
462 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200463}
464
Alex Deucherf482a142012-07-17 14:02:34 -0400465/**
466 * radeon_driver_preclose_kms - drm callback for pre close
467 *
468 * @dev: drm dev pointer
469 * @file_priv: drm file
470 *
471 * On device pre close, tear down hyperz and cmask filps on r1xx-r5xx
472 * (all asics).
473 */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200474void radeon_driver_preclose_kms(struct drm_device *dev,
475 struct drm_file *file_priv)
476{
Dave Airlieab9e1f52010-07-13 11:11:11 +1000477 struct radeon_device *rdev = dev->dev_private;
478 if (rdev->hyperz_filp == file_priv)
479 rdev->hyperz_filp = NULL;
Marek Olšákdca0d612011-01-27 22:46:15 +0100480 if (rdev->cmask_filp == file_priv)
481 rdev->cmask_filp = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200482}
483
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200484/*
485 * VBlank related functions.
486 */
Alex Deucherf482a142012-07-17 14:02:34 -0400487/**
488 * radeon_get_vblank_counter_kms - get frame count
489 *
490 * @dev: drm dev pointer
491 * @crtc: crtc to get the frame count from
492 *
493 * Gets the frame count on the requested crtc (all asics).
494 * Returns frame count on success, -EINVAL on failure.
495 */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200496u32 radeon_get_vblank_counter_kms(struct drm_device *dev, int crtc)
497{
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200498 struct radeon_device *rdev = dev->dev_private;
499
Dave Airlie9c950a42010-04-23 13:21:58 +1000500 if (crtc < 0 || crtc >= rdev->num_crtc) {
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200501 DRM_ERROR("Invalid crtc %d\n", crtc);
502 return -EINVAL;
503 }
504
505 return radeon_get_vblank_counter(rdev, crtc);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200506}
507
Alex Deucherf482a142012-07-17 14:02:34 -0400508/**
509 * radeon_enable_vblank_kms - enable vblank interrupt
510 *
511 * @dev: drm dev pointer
512 * @crtc: crtc to enable vblank interrupt for
513 *
514 * Enable the interrupt on the requested crtc (all asics).
515 * Returns 0 on success, -EINVAL on failure.
516 */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200517int radeon_enable_vblank_kms(struct drm_device *dev, int crtc)
518{
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200519 struct radeon_device *rdev = dev->dev_private;
Christian Koenigfb982572012-05-17 01:33:30 +0200520 unsigned long irqflags;
521 int r;
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200522
Dave Airlie9c950a42010-04-23 13:21:58 +1000523 if (crtc < 0 || crtc >= rdev->num_crtc) {
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200524 DRM_ERROR("Invalid crtc %d\n", crtc);
525 return -EINVAL;
526 }
527
Christian Koenigfb982572012-05-17 01:33:30 +0200528 spin_lock_irqsave(&rdev->irq.lock, irqflags);
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200529 rdev->irq.crtc_vblank_int[crtc] = true;
Christian Koenigfb982572012-05-17 01:33:30 +0200530 r = radeon_irq_set(rdev);
531 spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
532 return r;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200533}
534
Alex Deucherf482a142012-07-17 14:02:34 -0400535/**
536 * radeon_disable_vblank_kms - disable vblank interrupt
537 *
538 * @dev: drm dev pointer
539 * @crtc: crtc to disable vblank interrupt for
540 *
541 * Disable the interrupt on the requested crtc (all asics).
542 */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200543void radeon_disable_vblank_kms(struct drm_device *dev, int crtc)
544{
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200545 struct radeon_device *rdev = dev->dev_private;
Christian Koenigfb982572012-05-17 01:33:30 +0200546 unsigned long irqflags;
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200547
Dave Airlie9c950a42010-04-23 13:21:58 +1000548 if (crtc < 0 || crtc >= rdev->num_crtc) {
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200549 DRM_ERROR("Invalid crtc %d\n", crtc);
550 return;
551 }
552
Christian Koenigfb982572012-05-17 01:33:30 +0200553 spin_lock_irqsave(&rdev->irq.lock, irqflags);
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200554 rdev->irq.crtc_vblank_int[crtc] = false;
Michel Dänzer7ed220d2009-08-13 11:10:51 +0200555 radeon_irq_set(rdev);
Christian Koenigfb982572012-05-17 01:33:30 +0200556 spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200557}
558
Alex Deucherf482a142012-07-17 14:02:34 -0400559/**
560 * radeon_get_vblank_timestamp_kms - get vblank timestamp
561 *
562 * @dev: drm dev pointer
563 * @crtc: crtc to get the timestamp for
564 * @max_error: max error
565 * @vblank_time: time value
566 * @flags: flags passed to the driver
567 *
568 * Gets the timestamp on the requested crtc based on the
569 * scanout position. (all asics).
570 * Returns postive status flags on success, negative error on failure.
571 */
Mario Kleinerf5a80202010-10-23 04:42:17 +0200572int radeon_get_vblank_timestamp_kms(struct drm_device *dev, int crtc,
573 int *max_error,
574 struct timeval *vblank_time,
575 unsigned flags)
576{
577 struct drm_crtc *drmcrtc;
578 struct radeon_device *rdev = dev->dev_private;
579
580 if (crtc < 0 || crtc >= dev->num_crtcs) {
581 DRM_ERROR("Invalid crtc %d\n", crtc);
582 return -EINVAL;
583 }
584
585 /* Get associated drm_crtc: */
586 drmcrtc = &rdev->mode_info.crtcs[crtc]->base;
587
588 /* Helper routine in DRM core does all the work: */
589 return drm_calc_vbltimestamp_from_scanoutpos(dev, crtc, max_error,
590 vblank_time, flags,
591 drmcrtc);
592}
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200593
594/*
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200595 * IOCTL.
596 */
597int radeon_dma_ioctl_kms(struct drm_device *dev, void *data,
598 struct drm_file *file_priv)
599{
600 /* Not valid in KMS. */
601 return -EINVAL;
602}
603
604#define KMS_INVALID_IOCTL(name) \
605int name(struct drm_device *dev, void *data, struct drm_file *file_priv)\
606{ \
607 DRM_ERROR("invalid ioctl with kms %s\n", __func__); \
608 return -EINVAL; \
609}
610
611/*
612 * All these ioctls are invalid in kms world.
613 */
614KMS_INVALID_IOCTL(radeon_cp_init_kms)
615KMS_INVALID_IOCTL(radeon_cp_start_kms)
616KMS_INVALID_IOCTL(radeon_cp_stop_kms)
617KMS_INVALID_IOCTL(radeon_cp_reset_kms)
618KMS_INVALID_IOCTL(radeon_cp_idle_kms)
619KMS_INVALID_IOCTL(radeon_cp_resume_kms)
620KMS_INVALID_IOCTL(radeon_engine_reset_kms)
621KMS_INVALID_IOCTL(radeon_fullscreen_kms)
622KMS_INVALID_IOCTL(radeon_cp_swap_kms)
623KMS_INVALID_IOCTL(radeon_cp_clear_kms)
624KMS_INVALID_IOCTL(radeon_cp_vertex_kms)
625KMS_INVALID_IOCTL(radeon_cp_indices_kms)
626KMS_INVALID_IOCTL(radeon_cp_texture_kms)
627KMS_INVALID_IOCTL(radeon_cp_stipple_kms)
628KMS_INVALID_IOCTL(radeon_cp_indirect_kms)
629KMS_INVALID_IOCTL(radeon_cp_vertex2_kms)
630KMS_INVALID_IOCTL(radeon_cp_cmdbuf_kms)
631KMS_INVALID_IOCTL(radeon_cp_getparam_kms)
632KMS_INVALID_IOCTL(radeon_cp_flip_kms)
633KMS_INVALID_IOCTL(radeon_mem_alloc_kms)
634KMS_INVALID_IOCTL(radeon_mem_free_kms)
635KMS_INVALID_IOCTL(radeon_mem_init_heap_kms)
636KMS_INVALID_IOCTL(radeon_irq_emit_kms)
637KMS_INVALID_IOCTL(radeon_irq_wait_kms)
638KMS_INVALID_IOCTL(radeon_cp_setparam_kms)
639KMS_INVALID_IOCTL(radeon_surface_alloc_kms)
640KMS_INVALID_IOCTL(radeon_surface_free_kms)
641
642
643struct drm_ioctl_desc radeon_ioctls_kms[] = {
Dave Airlie1b2f1482010-08-14 20:20:34 +1000644 DRM_IOCTL_DEF_DRV(RADEON_CP_INIT, radeon_cp_init_kms, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
645 DRM_IOCTL_DEF_DRV(RADEON_CP_START, radeon_cp_start_kms, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
646 DRM_IOCTL_DEF_DRV(RADEON_CP_STOP, radeon_cp_stop_kms, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
647 DRM_IOCTL_DEF_DRV(RADEON_CP_RESET, radeon_cp_reset_kms, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
648 DRM_IOCTL_DEF_DRV(RADEON_CP_IDLE, radeon_cp_idle_kms, DRM_AUTH),
649 DRM_IOCTL_DEF_DRV(RADEON_CP_RESUME, radeon_cp_resume_kms, DRM_AUTH),
650 DRM_IOCTL_DEF_DRV(RADEON_RESET, radeon_engine_reset_kms, DRM_AUTH),
651 DRM_IOCTL_DEF_DRV(RADEON_FULLSCREEN, radeon_fullscreen_kms, DRM_AUTH),
652 DRM_IOCTL_DEF_DRV(RADEON_SWAP, radeon_cp_swap_kms, DRM_AUTH),
653 DRM_IOCTL_DEF_DRV(RADEON_CLEAR, radeon_cp_clear_kms, DRM_AUTH),
654 DRM_IOCTL_DEF_DRV(RADEON_VERTEX, radeon_cp_vertex_kms, DRM_AUTH),
655 DRM_IOCTL_DEF_DRV(RADEON_INDICES, radeon_cp_indices_kms, DRM_AUTH),
656 DRM_IOCTL_DEF_DRV(RADEON_TEXTURE, radeon_cp_texture_kms, DRM_AUTH),
657 DRM_IOCTL_DEF_DRV(RADEON_STIPPLE, radeon_cp_stipple_kms, DRM_AUTH),
658 DRM_IOCTL_DEF_DRV(RADEON_INDIRECT, radeon_cp_indirect_kms, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
659 DRM_IOCTL_DEF_DRV(RADEON_VERTEX2, radeon_cp_vertex2_kms, DRM_AUTH),
660 DRM_IOCTL_DEF_DRV(RADEON_CMDBUF, radeon_cp_cmdbuf_kms, DRM_AUTH),
661 DRM_IOCTL_DEF_DRV(RADEON_GETPARAM, radeon_cp_getparam_kms, DRM_AUTH),
662 DRM_IOCTL_DEF_DRV(RADEON_FLIP, radeon_cp_flip_kms, DRM_AUTH),
663 DRM_IOCTL_DEF_DRV(RADEON_ALLOC, radeon_mem_alloc_kms, DRM_AUTH),
664 DRM_IOCTL_DEF_DRV(RADEON_FREE, radeon_mem_free_kms, DRM_AUTH),
665 DRM_IOCTL_DEF_DRV(RADEON_INIT_HEAP, radeon_mem_init_heap_kms, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
666 DRM_IOCTL_DEF_DRV(RADEON_IRQ_EMIT, radeon_irq_emit_kms, DRM_AUTH),
667 DRM_IOCTL_DEF_DRV(RADEON_IRQ_WAIT, radeon_irq_wait_kms, DRM_AUTH),
668 DRM_IOCTL_DEF_DRV(RADEON_SETPARAM, radeon_cp_setparam_kms, DRM_AUTH),
669 DRM_IOCTL_DEF_DRV(RADEON_SURF_ALLOC, radeon_surface_alloc_kms, DRM_AUTH),
670 DRM_IOCTL_DEF_DRV(RADEON_SURF_FREE, radeon_surface_free_kms, DRM_AUTH),
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200671 /* KMS */
Dave Airlie1b2f1482010-08-14 20:20:34 +1000672 DRM_IOCTL_DEF_DRV(RADEON_GEM_INFO, radeon_gem_info_ioctl, DRM_AUTH|DRM_UNLOCKED),
673 DRM_IOCTL_DEF_DRV(RADEON_GEM_CREATE, radeon_gem_create_ioctl, DRM_AUTH|DRM_UNLOCKED),
674 DRM_IOCTL_DEF_DRV(RADEON_GEM_MMAP, radeon_gem_mmap_ioctl, DRM_AUTH|DRM_UNLOCKED),
675 DRM_IOCTL_DEF_DRV(RADEON_GEM_SET_DOMAIN, radeon_gem_set_domain_ioctl, DRM_AUTH|DRM_UNLOCKED),
676 DRM_IOCTL_DEF_DRV(RADEON_GEM_PREAD, radeon_gem_pread_ioctl, DRM_AUTH|DRM_UNLOCKED),
677 DRM_IOCTL_DEF_DRV(RADEON_GEM_PWRITE, radeon_gem_pwrite_ioctl, DRM_AUTH|DRM_UNLOCKED),
678 DRM_IOCTL_DEF_DRV(RADEON_GEM_WAIT_IDLE, radeon_gem_wait_idle_ioctl, DRM_AUTH|DRM_UNLOCKED),
679 DRM_IOCTL_DEF_DRV(RADEON_CS, radeon_cs_ioctl, DRM_AUTH|DRM_UNLOCKED),
680 DRM_IOCTL_DEF_DRV(RADEON_INFO, radeon_info_ioctl, DRM_AUTH|DRM_UNLOCKED),
681 DRM_IOCTL_DEF_DRV(RADEON_GEM_SET_TILING, radeon_gem_set_tiling_ioctl, DRM_AUTH|DRM_UNLOCKED),
682 DRM_IOCTL_DEF_DRV(RADEON_GEM_GET_TILING, radeon_gem_get_tiling_ioctl, DRM_AUTH|DRM_UNLOCKED),
683 DRM_IOCTL_DEF_DRV(RADEON_GEM_BUSY, radeon_gem_busy_ioctl, DRM_AUTH|DRM_UNLOCKED),
Jerome Glisse721604a2012-01-05 22:11:05 -0500684 DRM_IOCTL_DEF_DRV(RADEON_GEM_VA, radeon_gem_va_ioctl, DRM_AUTH|DRM_UNLOCKED),
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200685};
686int radeon_max_kms_ioctl = DRM_ARRAY_SIZE(radeon_ioctls_kms);